Skip to content

Commit 5de42d6

Browse files
committed
user-data is not always base64-encoded.
1 parent f48b2bc commit 5de42d6

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

agent/main.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ agent_configure(struct system_config *sc)
493493
{
494494
struct ssh_pubkey *ssh;
495495
char *str1, *str2;
496-
unsigned char *userdata;
497-
size_t len;
498496

499497
/* Skip configuration on the same instance */
500498
if ((str1 = filein("r", "/var/db/cloud-instance")) != NULL) {
@@ -572,18 +570,9 @@ agent_configure(struct system_config *sc)
572570
}
573571

574572
if (sc->sc_userdata) {
575-
/*
576-
* The decoded base64 string is smaller than the
577-
* userdata; it is safe to allocate the same length.
578-
*/
579-
len = strlen(sc->sc_userdata);
580-
if ((userdata = calloc(1, len + 1)) == NULL)
581-
log_warnx("failed to allocate user-data");
582-
else if ((len = b64_pton(sc->sc_userdata, userdata, len)) < 1)
583-
log_warnx("failed to decode user-data");
584-
else
585-
(void)agent_userdata(userdata, len);
586-
free(userdata);
573+
if (agent_userdata(sc->sc_userdata,
574+
strlen(sc->sc_userdata)) != 0)
575+
log_warnx("user-data failed");
587576
}
588577

589578
log_debug("%s: %s", __func__, "/etc/rc.firsttime");
@@ -608,10 +597,28 @@ agent_userdata(const unsigned char *userdata, size_t len)
608597
const char *file;
609598
int ret = -1;
610599

611-
/* XXX add support for gzip-encoded user-data */
612-
if ((shebang = get_line(userdata, len)) == NULL) {
613-
log_warnx("failed to decode shebang from user-data");
600+
if (len <= 2) {
601+
log_warnx("user-data too short");
602+
goto fail;
603+
}
604+
605+
if (userdata[0] == 0x1f && userdata[1] == 0x8b) {
606+
log_warnx("gzip-compressed user-data is not supported");
614607
goto fail;
608+
} else if (userdata[0] == '#') {
609+
if ((shebang = get_line(userdata, len)) == NULL) {
610+
log_warnx("failed to decode shebang from user-data");
611+
goto fail;
612+
}
613+
} else if (isprint(userdata[0]) && isprint(userdata[1])) {
614+
/* Decode user-data and call the function again */
615+
if ((str = calloc(1, len + 1)) == NULL ||
616+
(len = b64_pton(userdata, str, len)) < 1 ||
617+
agent_userdata(str, len) != 0) {
618+
log_warnx("failed to decode user-data");
619+
goto fail;
620+
}
621+
goto done;
615622
}
616623

617624
log_debug("%s: user-data: %s", __func__, shebang);
@@ -641,6 +648,7 @@ agent_userdata(const unsigned char *userdata, size_t len)
641648
fileout(line, "a", "/etc/rc.firsttime") != 0)
642649
log_warnx("failed to add user-data script");
643650

651+
done:
644652
ret = 0;
645653
fail:
646654
free(line);

0 commit comments

Comments
 (0)