2017-06-29 09:40:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Reyk Floeter <reyk@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "http.h"
|
|
|
|
#include "xml.h"
|
|
|
|
|
|
|
|
static int cloudinit_fetch(struct system_config *);
|
|
|
|
|
2019-06-02 00:41:36 +00:00
|
|
|
int
|
|
|
|
tryendpoint(struct system_config *sc,
|
|
|
|
int (fetch)(struct system_config *),
|
|
|
|
int (next)(struct system_config *))
|
|
|
|
{
|
2019-06-02 01:13:58 +00:00
|
|
|
int errfd = -1, ret;
|
|
|
|
|
2019-06-02 00:41:36 +00:00
|
|
|
free(sc->sc_endpoint);
|
|
|
|
sc->sc_endpoint = NULL;
|
|
|
|
|
|
|
|
switch (sc->sc_dhcpendpoint) {
|
|
|
|
case 0:
|
|
|
|
sc->sc_dhcpendpoint = 1;
|
|
|
|
if (dhcp_getendpoint(sc) == -1)
|
|
|
|
return tryendpoint(sc, fetch, next);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sc->sc_dhcpendpoint = 2;
|
|
|
|
if ((sc->sc_endpoint = strdup(DEFAULT_ENDPOINT)) == NULL) {
|
|
|
|
log_warnx("failed to set defaults");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (next == NULL)
|
|
|
|
return (-1);
|
|
|
|
sc->sc_dhcpendpoint = 0;
|
|
|
|
return (*next)(sc);
|
|
|
|
}
|
|
|
|
|
2019-06-02 01:13:58 +00:00
|
|
|
errfd = disable_output(sc, STDERR_FILENO);
|
|
|
|
ret = (*fetch)(sc);
|
|
|
|
enable_output(sc, STDERR_FILENO, errfd);
|
|
|
|
if (ret != 0)
|
2019-06-02 00:41:36 +00:00
|
|
|
return tryendpoint(sc, fetch, next);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2017-06-29 09:40:48 +00:00
|
|
|
int
|
|
|
|
ec2(struct system_config *sc)
|
|
|
|
{
|
2018-01-10 13:13:49 +00:00
|
|
|
free(sc->sc_username);
|
2019-06-02 00:41:36 +00:00
|
|
|
if ((sc->sc_username = strdup("ec2-user")) == NULL) {
|
|
|
|
log_warnx("failed to set default user");
|
2017-06-29 09:40:48 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:30:41 +00:00
|
|
|
sc->sc_stack = "ec2";
|
2019-06-02 00:41:36 +00:00
|
|
|
sc->sc_dhcpendpoint = 1;
|
|
|
|
return tryendpoint(sc, cloudinit_fetch, NULL);
|
2017-06-29 09:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudinit(struct system_config *sc)
|
|
|
|
{
|
2018-08-13 17:30:41 +00:00
|
|
|
sc->sc_stack = "cloudinit";
|
2019-06-02 00:41:36 +00:00
|
|
|
sc->sc_dhcpendpoint = 0;
|
|
|
|
return tryendpoint(sc, cloudinit_fetch, NULL);
|
2017-06-29 09:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cloudinit_fetch(struct system_config *sc)
|
|
|
|
{
|
2018-05-08 07:48:30 +00:00
|
|
|
int ret = -1;
|
2017-06-29 09:40:48 +00:00
|
|
|
char *str = NULL;
|
|
|
|
|
|
|
|
sc->sc_addr.ip = sc->sc_endpoint;
|
|
|
|
sc->sc_addr.family = 4;
|
|
|
|
|
2017-06-30 16:29:23 +00:00
|
|
|
/* instance-id */
|
2018-01-10 13:13:49 +00:00
|
|
|
if ((sc->sc_instance = metadata(sc,
|
2017-06-30 19:30:05 +00:00
|
|
|
"/latest/meta-data/instance-id", WORD)) == NULL)
|
2017-06-29 09:40:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* hostname */
|
2018-01-10 13:13:49 +00:00
|
|
|
if ((sc->sc_hostname = metadata(sc,
|
2017-06-30 19:30:05 +00:00
|
|
|
"/latest/meta-data/local-hostname", WORD)) == NULL)
|
2017-06-29 09:40:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
2018-05-08 08:03:38 +00:00
|
|
|
/* optional pubkey */
|
2018-01-10 13:13:49 +00:00
|
|
|
if ((str = metadata(sc,
|
2018-05-07 16:10:27 +00:00
|
|
|
"/latest/meta-data/public-keys/0/openssh-key", LINE)) == NULL &&
|
|
|
|
(str = metadata(sc,
|
|
|
|
"/latest/meta-data/public-keys", LINE)) == NULL)
|
2018-05-08 08:03:38 +00:00
|
|
|
log_warnx("failed to get public key");
|
|
|
|
else if (agent_addpubkey(sc, str, NULL) != 0)
|
2017-06-29 09:40:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
2017-06-30 16:29:23 +00:00
|
|
|
/* optional username - this is an extension by meta-data(8) */
|
2018-01-10 13:13:49 +00:00
|
|
|
if ((str = metadata(sc, "/latest/meta-data/username", WORD)) != NULL) {
|
2017-06-30 16:29:23 +00:00
|
|
|
free(sc->sc_username);
|
|
|
|
sc->sc_username = str;
|
2017-06-30 17:29:42 +00:00
|
|
|
str = NULL;
|
2017-06-30 16:29:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:10:58 +00:00
|
|
|
/* userdata (optional) */
|
|
|
|
sc->sc_userdata = metadata(sc, "/latest/user-data", TEXT);
|
2017-06-29 09:40:48 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
free(str);
|
|
|
|
return (ret);
|
|
|
|
}
|