Try to get endpoint from "dhcp-server-identifier"

This commit is contained in:
Reyk Floeter 2018-01-10 15:10:58 +01:00
parent ac66f160e0
commit ee473a4bd6
5 changed files with 74 additions and 64 deletions

View file

@ -51,7 +51,6 @@ static struct httpget
static int azure_keys(struct system_config *); static int azure_keys(struct system_config *);
static int azure_getpubkeys(struct system_config *); static int azure_getpubkeys(struct system_config *);
static int azure_getendpoint(struct system_config *);
static int azure_getovfenv(struct system_config *); static int azure_getovfenv(struct system_config *);
static int azure_versions(struct system_config *); static int azure_versions(struct system_config *);
static int azure_goalstate(struct system_config *); static int azure_goalstate(struct system_config *);
@ -78,7 +77,7 @@ azure(struct system_config *sc)
goto done; goto done;
} }
if (azure_getendpoint(sc) != 0) { if (dhcp_getendpoint(sc) != 0) {
log_warnx("failed to get endpoint"); log_warnx("failed to get endpoint");
goto done; goto done;
} }
@ -794,53 +793,3 @@ azure_getovfenv(struct system_config *sc)
xml_free(&xml); xml_free(&xml);
return (ret); return (ret);
} }
static int
azure_getendpoint(struct system_config *sc)
{
char path[PATH_MAX], buf[BUFSIZ], *ep = NULL;
int a[4];
FILE *fp;
if ((size_t)snprintf(path, sizeof(path), "/var/db/dhclient.leases.%s",
sc->sc_interface) >= sizeof(path)) {
log_debug("%s: invalid path", __func__);
return (-1);
}
if ((fp = fopen(path, "r")) == NULL) {
log_debug("%s: failed to open %s", __func__, path);
return (-1);
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
buf[strcspn(buf, ";\n")] = '\0';
/* Find last occurence of option-245 */
if (sscanf(buf, " option option-245 %x:%x:%x:%x",
&a[0], &a[1], &a[2], &a[3]) == 4) {
free(ep);
if (asprintf(&ep, "%d.%d.%d.%d",
a[0], a[1], a[2], a[3]) == -1) {
log_debug("%s: asprintf", __func__);
fclose(fp);
return (-1);
}
}
}
fclose(fp);
if (ep == NULL) {
log_debug("%s: endpoint not found", __func__);
return (-1);
}
sc->sc_endpoint = ep;
sc->sc_addr.ip = sc->sc_endpoint;
sc->sc_addr.family = 4;
log_debug("%s: %s", __func__, ep);
return (0);
}

View file

@ -36,7 +36,7 @@ ec2(struct system_config *sc)
{ {
free(sc->sc_username); free(sc->sc_username);
if ((sc->sc_username = strdup("ec2-user")) == NULL || if ((sc->sc_username = strdup("ec2-user")) == NULL ||
(sc->sc_endpoint = strdup("169.254.169.254")) == NULL) { (sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) {
log_warnx("failed to set defaults"); log_warnx("failed to set defaults");
return (-1); return (-1);
} }
@ -47,8 +47,8 @@ ec2(struct system_config *sc)
int int
cloudinit(struct system_config *sc) cloudinit(struct system_config *sc)
{ {
/* XXX get endpoint from DHCP lease file */ if ((dhcp_getendpoint(sc) == -1) &&
if ((sc->sc_endpoint = strdup("169.254.169.254")) == NULL) { (sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) {
log_warnx("failed to set defaults"); log_warnx("failed to set defaults");
return (-1); return (-1);
} }
@ -89,9 +89,8 @@ cloudinit_fetch(struct system_config *sc)
str = NULL; str = NULL;
} }
/* userdata */ /* userdata (optional) */
if ((sc->sc_userdata = metadata(sc, "/latest/user-data", TEXT)) == NULL) sc->sc_userdata = metadata(sc, "/latest/user-data", TEXT);
goto fail;
ret = 0; ret = 0;
fail: fail:

View file

@ -17,6 +17,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <syslog.h> #include <syslog.h>
#include <stdlib.h> #include <stdlib.h>
@ -717,7 +718,67 @@ connect_wait(int s, const struct sockaddr *name, socklen_t namelen)
return (-1); return (-1);
} }
log_debug("%s:%d error %d", __func__, __LINE__, error); return (0);
}
int
dhcp_getendpoint(struct system_config *sc)
{
char path[PATH_MAX], buf[BUFSIZ], *ep = NULL;
int a[4], has245 = 0;
size_t sz;
FILE *fp;
if ((size_t)snprintf(path, sizeof(path), "/var/db/dhclient.leases.%s",
sc->sc_interface) >= sizeof(path)) {
log_debug("%s: invalid path", __func__);
return (-1);
}
if ((fp = fopen(path, "r")) == NULL) {
log_debug("%s: failed to open %s", __func__, path);
return (-1);
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
buf[strcspn(buf, ";\n")] = '\0';
/* Find last occurence of dhcp-server-identifier */
sz = strlen(" option dhcp-server-identifier ");
if (!has245 &&
strncmp(buf, " option dhcp-server-identifier ", sz) == 0) {
free(ep);
if ((ep = strdup(buf + sz)) == NULL) {
log_debug("%s: strdup", __func__);
fclose(fp);
return (-1);
}
}
/* Find last occurence of option-245 (only on Azure) */
if (sscanf(buf, " option option-245 %x:%x:%x:%x",
&a[0], &a[1], &a[2], &a[3]) == 4) {
has245 = 1;
free(ep);
if (asprintf(&ep, "%d.%d.%d.%d",
a[0], a[1], a[2], a[3]) == -1) {
log_debug("%s: asprintf", __func__);
fclose(fp);
return (-1);
}
}
}
fclose(fp);
if (ep == NULL)
return (-1);
sc->sc_endpoint = ep;
sc->sc_addr.ip = sc->sc_endpoint;
sc->sc_addr.family = 4;
log_debug("%s: %s", __func__, ep);
return (0); return (0);
} }

View file

@ -26,6 +26,7 @@
#include "http.h" #include "http.h"
#include "jsmn.h" #include "jsmn.h"
#define DEFAULT_ENDPOINT "169.254.169.254"
#define CONNECT_TIMEOUT 10 /* in seconds */ #define CONNECT_TIMEOUT 10 /* in seconds */
enum strtype { enum strtype {
@ -107,6 +108,7 @@ int agent_setpubkey(struct system_config *, const char *, const char *);
char *metadata(struct system_config *, const char *, enum strtype); char *metadata(struct system_config *, const char *, enum strtype);
char *metadata_file(struct system_config *, const char *, enum strtype); char *metadata_file(struct system_config *, const char *, enum strtype);
int connect_wait(int, const struct sockaddr *, socklen_t); int connect_wait(int, const struct sockaddr *, socklen_t);
int dhcp_getendpoint(struct system_config *);
/* log.c */ /* log.c */
void log_init(int, int); void log_init(int, int);

View file

@ -34,7 +34,8 @@ static int openstack_fetch(struct system_config *);
int int
openstack(struct system_config *sc) openstack(struct system_config *sc)
{ {
if ((sc->sc_endpoint = strdup("169.254.169.254")) == NULL) { if ((dhcp_getendpoint(sc) == -1) &&
(sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) {
log_warnx("failed to set defaults"); log_warnx("failed to set defaults");
return (-1); return (-1);
} }
@ -88,10 +89,8 @@ openstack_fetch(struct system_config *sc)
free(str); free(str);
} }
/* userdata */ /* userdata (optional) */
if ((sc->sc_userdata = sc->sc_userdata = metadata(sc, "/openstack/latest/user_data", TEXT);
metadata(sc, "/openstack/latest/user-data", TEXT)) == NULL)
goto fail;
ret = 0; ret = 0;
fail: fail: