|
|
|
@ -362,6 +362,7 @@ agent_free(struct system_config *sc)
|
|
|
|
|
log_debug("%s: unmounted %s", __func__, sc->sc_cdrom); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
free(sc->sc_args); |
|
|
|
|
free(sc->sc_hostname); |
|
|
|
|
free(sc->sc_username); |
|
|
|
|
free(sc->sc_password); |
|
|
|
@ -445,6 +446,9 @@ agent_getnetaddr(struct system_config *sc, struct net_addr *net)
|
|
|
|
|
continue; |
|
|
|
|
if (na->net_ifunit != net->net_ifunit) |
|
|
|
|
continue; |
|
|
|
|
if (na->net_type == NET_DNS_DOMAIN && |
|
|
|
|
strcasecmp(na->net_value, net->net_value) != 0) |
|
|
|
|
continue; |
|
|
|
|
if (net->net_addr.ss_family != AF_UNSPEC) { |
|
|
|
|
if (na->net_addr.ss_family != |
|
|
|
|
net->net_addr.ss_family) |
|
|
|
@ -476,6 +480,13 @@ agent_addnetaddr(struct system_config *sc, unsigned int unit,
|
|
|
|
|
net->net_type = type; |
|
|
|
|
|
|
|
|
|
switch (type) { |
|
|
|
|
case NET_DNS_DOMAIN: |
|
|
|
|
if (strlen(value) >= NI_MAXHOST) { |
|
|
|
|
log_debug("%s: if%u domain %s", __func__, unit, value); |
|
|
|
|
free(net); |
|
|
|
|
return (-1); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case NET_MAC: |
|
|
|
|
if (ether_aton(value) == NULL) { |
|
|
|
|
log_debug("%s: if%u mac %s", __func__, unit, value); |
|
|
|
@ -516,15 +527,16 @@ agent_addnetaddr(struct system_config *sc, unsigned int unit,
|
|
|
|
|
net->net_addr.ss_family = res->ai_family; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Address already exists, ignore new entry */ |
|
|
|
|
if ((na = agent_getnetaddr(sc, net)) != NULL) { |
|
|
|
|
if ((net->net_value = strdup(value)) == NULL) { |
|
|
|
|
free(net); |
|
|
|
|
return (0); |
|
|
|
|
return (-1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((net->net_value = strdup(value)) == NULL) { |
|
|
|
|
/* Address already exists, ignore new entry */ |
|
|
|
|
if ((na = agent_getnetaddr(sc, net)) != NULL) { |
|
|
|
|
free(net->net_value); |
|
|
|
|
free(net); |
|
|
|
|
return (-1); |
|
|
|
|
return (0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&sc->sc_netaddrs, net, net_entry); |
|
|
|
@ -794,6 +806,10 @@ agent_network(struct system_config *sc)
|
|
|
|
|
struct net_addr *net; |
|
|
|
|
char ift[16], ifname[16], line[1024], path[PATH_MAX]; |
|
|
|
|
const char *family; |
|
|
|
|
char domain[(NI_MAXHOST + 1) * 6 + 8]; /* up to 6 domains */ |
|
|
|
|
int has_domain = 0; |
|
|
|
|
char ifidx[UINT16_MAX]; |
|
|
|
|
const char *comment = "# Generated by cloud-agent"; |
|
|
|
|
|
|
|
|
|
if (!sc->sc_network) |
|
|
|
|
return (0); |
|
|
|
@ -802,21 +818,36 @@ agent_network(struct system_config *sc)
|
|
|
|
|
return (-1); |
|
|
|
|
ift[strcspn(ift, "0123456789")] = '\0'; |
|
|
|
|
|
|
|
|
|
memset(ifidx, 0, sizeof(ifidx)); |
|
|
|
|
snprintf(domain, sizeof(domain), "search "); |
|
|
|
|
fileout(comment, "w", "/etc/mygate"); |
|
|
|
|
fileout(comment, "w", "/etc/resolv.conf"); |
|
|
|
|
|
|
|
|
|
TAILQ_FOREACH(net, &sc->sc_netaddrs, net_entry) { |
|
|
|
|
snprintf(ifname, sizeof(ifname), "%s%u", ift, net->net_ifunit); |
|
|
|
|
switch (net->net_type) { |
|
|
|
|
case NET_IP: |
|
|
|
|
family = net->net_addr.ss_family == AF_INET ? |
|
|
|
|
"inet" : "inet6"; |
|
|
|
|
/* XXX prefix */ |
|
|
|
|
/* XXX prefix or mask */ |
|
|
|
|
|
|
|
|
|
/* hostname.if startup configuration */ |
|
|
|
|
snprintf(line, sizeof(line), "%s alias %s", |
|
|
|
|
family, net->net_value); |
|
|
|
|
snprintf(path, sizeof(path), |
|
|
|
|
"/etc/hostname.%s", ifname); |
|
|
|
|
if (!ifidx[net->net_ifunit]) |
|
|
|
|
fileout(comment, "w", path); |
|
|
|
|
|
|
|
|
|
snprintf(line, sizeof(line), "%s alias %s", |
|
|
|
|
family, net->net_value); |
|
|
|
|
fileout(line, "a", path); |
|
|
|
|
|
|
|
|
|
if (!ifidx[net->net_ifunit]++ && |
|
|
|
|
net->net_ifunit == 0) { |
|
|
|
|
snprintf(line, sizeof(line), |
|
|
|
|
"!%s", sc->sc_args); |
|
|
|
|
fileout(line, "a", path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* runtime configuration */ |
|
|
|
|
(void)shell("ifconfig", ifname, family, |
|
|
|
|
"alias", net->net_value, NULL); |
|
|
|
@ -829,11 +860,24 @@ agent_network(struct system_config *sc)
|
|
|
|
|
net->net_value); |
|
|
|
|
fileout(line, "a", "/etc/resolv.conf"); |
|
|
|
|
break; |
|
|
|
|
case NET_DNS_DOMAIN: |
|
|
|
|
if (!has_domain++) { |
|
|
|
|
/* use the first search domain as our own */ |
|
|
|
|
snprintf(line, sizeof(line), "domain %s", |
|
|
|
|
net->net_value); |
|
|
|
|
fileout(line, "a", "/etc/resolv.conf"); |
|
|
|
|
} else |
|
|
|
|
(void)strlcat(domain, " ", sizeof(domain)); |
|
|
|
|
(void)strlcat(domain, net->net_value, sizeof(domain)); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (has_domain) |
|
|
|
|
fileout(domain, "a", "/etc/resolv.conf"); |
|
|
|
|
|
|
|
|
|
return (0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1049,6 +1093,36 @@ usage(void)
|
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static char * |
|
|
|
|
get_args(int argc, char *const *argv) |
|
|
|
|
{ |
|
|
|
|
char *args, path[PATH_MAX]; |
|
|
|
|
size_t argslen = 0; |
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
/* Store args in a string */ |
|
|
|
|
for (i = 0; i < argc; i++) { |
|
|
|
|
if (i == 0) { |
|
|
|
|
realpath(argv[0], path); |
|
|
|
|
argslen += strlen(path) + 1; |
|
|
|
|
} else { |
|
|
|
|
argslen += strlen(argv[i]) + 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ((args = calloc(1, argslen + 1)) == NULL) |
|
|
|
|
return (NULL); |
|
|
|
|
for (i = 0; i < argc; i++) { |
|
|
|
|
if (i == 0) |
|
|
|
|
strlcat(args, path, argslen); |
|
|
|
|
else { |
|
|
|
|
strlcat(args, " ", argslen); |
|
|
|
|
strlcat(args, argv[i], argslen); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return (args); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
main(int argc, char *const *argv) |
|
|
|
|
{ |
|
|
|
@ -1056,6 +1130,10 @@ main(int argc, char *const *argv)
|
|
|
|
|
int verbose = 0, dryrun = 0, unconfigure = 0; |
|
|
|
|
int ch, ret, timeout = CONNECT_TIMEOUT; |
|
|
|
|
const char *error = NULL; |
|
|
|
|
char *args; |
|
|
|
|
|
|
|
|
|
if ((args = get_args(argc, argv)) == NULL) |
|
|
|
|
fatalx("failed to save args"); |
|
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "nvt:u")) != -1) { |
|
|
|
|
switch (ch) { |
|
|
|
@ -1098,6 +1176,7 @@ main(int argc, char *const *argv)
|
|
|
|
|
|
|
|
|
|
if ((sc = agent_init(argv[0], dryrun, timeout)) == NULL) |
|
|
|
|
fatalx("agent"); |
|
|
|
|
sc->sc_args = args; |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* XXX Detect cloud with help from hostctl and sysctl |
|
|
|
|