static admin *admins;
static admin *a_dead;
static sel_file sock;
-static const char *sockname;
+static const char *sockname = 0;
static sym_table a_svcs;
static unsigned flags = 0;
static admin *a_stdin = 0;
*
* * "?PEER" PEER -- peer's name
*
- * * "?ERRNO" ERRNO -- system error code
+ * * "?ERR" CODE -- system error code
+ *
+ * * "?ERRNO" -- system error code from @errno@
*
* * "[!]..." ... -- @dstr_putf@-like string as single token
*/
while (*av) u_quotify(d, *av++);
} else if (strcmp(fmt, "?PEER") == 0)
u_quotify(d, p_name(va_arg(*ap, peer *)));
- else if (strcmp(fmt, "?ERRNO") == 0) {
+ else if (strcmp(fmt, "?ERR") == 0) {
+ int e = va_arg(*ap, int);
+ dstr_putf(d, " E%d", e);
+ u_quotify(d, strerror(e));
+ } else if (strcmp(fmt, "?ERRNO") == 0) {
dstr_putf(d, " E%d", errno);
u_quotify(d, strerror(errno));
} else
va_end(ap);
}
-/* --- @a_quit@ --- *
- *
- * Arguments: ---
- *
- * Returns: ---
- *
- * Use: Shuts things down nicely.
- */
-
-void a_quit(void)
-{
- close(sock.fd);
- unlink(sockname);
- FOREACH_PEER(p, { p_destroy(p, 1); });
- ps_quit();
- exit(0);
-}
-
/* --- @a_sigdie@ --- *
*
* Arguments: @int sig@ = signal number
break;
}
a_warn("SERVER", "quit", "signal", "%s", p, A_END);
- a_quit();
+ lp_end();
}
/* --- @a_sighup@ --- *
{ fam = "ANY"; af = AF_UNSPEC; i++; }
else for (j = 0; j < NADDRFAM; j++) {
if (mystrieq(av[i], aftab[j].name)) {
- if (udpsock[j].fd < 0) {
+ if (udpsock[j].sf.fd < 0) {
a_fail(a, "disabled-address-family", "%s", aftab[j].name, A_END);
goto fail;
}
aihint.ai_flags = AI_NUMERICHOST;
if (!getaddrinfo(av[i], 0, &aihint, &ailist)) {
for (ai = ailist; ai; ai = ai->ai_next) {
- if ((j = afix(ai->ai_family)) >= 0 && udpsock[j].fd >= 0)
+ if ((j = afix(ai->ai_family)) >= 0 && udpsock[j].sf.fd >= 0)
break;
}
if (!ai) {
#ifdef HAVE_LIBADNS
qf = adns_qf_search;
for (j = 0; j < NADDRFAM; j++) {
- if ((af == AF_UNSPEC || af == aftab[i].af) && udpsock[j].fd >= 0)
+ if ((af == AF_UNSPEC || af == aftab[i].af) && udpsock[j].sf.fd >= 0)
qf |= aftab[j].qf;
}
if ((err = adns_submit(ads, r->addr, adns_r_addr, qf, r, &r->q)) != 0) {
a_bgfail(&r->bg, "resolve-error", "%s", r->addr, A_END);
goto fail_release;
}
- if (udpsock[AFIX_INET].fd < 0) {
+ if (udpsock[AFIX_INET].sf.fd < 0) {
a_bgfail(&r->bg, "disabled-address-family", "INET", A_END);
goto fail_release;
}
{
const char *tag = 0;
admin_addop *add;
+ const tunnel_ops *tops;
/* --- Set stuff up --- */
add->peer.privtag = 0;
add->peer.knock = 0;
add->peer.t_ka = 0;
- add->peer.tops = tun_default;
+ add->peer.tops = p_dflttun();
add->peer.f = 0;
/* --- Parse options --- */
OPTIONS(ac, av, {
OPTARG("-background", arg, { tag = arg; })
OPTARG("-tunnel", arg, {
- unsigned i;
- for (i = 0;; i++) {
- if (!tunnels[i]) {
- a_fail(a, "unknown-tunnel", "%s", arg, A_END);
- goto fail;
- }
- if (mystrieq(arg, tunnels[i]->name)) {
- add->peer.tops = tunnels[i];
- break;
- }
- }
+ if ((tops = p_findtun(arg)) == 0)
+ { a_fail(a, "unknown-tunnel", "%s", arg, A_END); goto fail; }
+ add->peer.tops = tops;
})
OPTTIME("-keepalive", t, { add->peer.t_ka = t; })
OPT("-cork", { add->peer.f |= KXF_CORK; })
a_fail(a, "unknown-address-family", "%s", av[0], A_END);
return;
found:
- if (udpsock[i].fd < 0) {
+ if (udpsock[i].sf.fd < 0) {
a_fail(a, "disabled-address-family", "%s", aftab[i].name, A_END);
return;
}
} else {
for (i = 0; i < NADDRFAM; i++)
- if (udpsock[i].fd >= 0) goto found;
+ if (udpsock[i].sf.fd >= 0) goto found;
abort();
}
- a_info(a, "%u", p_port(i), A_END);
+ a_info(a, "%u", udpsock[i].port, A_END);
a_ok(a);
}
{
a_warn("SERVER", "quit", "admin-request", A_END);
a_ok(a);
- a_quit();
+ lp_end();
}
static void acmd_version(admin *a, unsigned ac, char *av[])
static void acmd_tunnels(admin *a, unsigned ac, char *av[])
{
- int i;
-
- for (i = 0; tunnels[i]; i++)
- a_info(a, "%s", tunnels[i]->name, A_END);
+ FOREACH_TUN(tops, { a_info(a, "%s", tops->name, A_END); });
a_ok(a);
}
if (a->f & AF_FOREGROUND) {
T( trace(T_ADMIN, "admin: foreground client quit: shutting down"); )
a_warn("SERVER", "quit", "foreground-eof", A_END);
- a_quit();
+ lp_end();
}
/* --- Abort any background jobs in progress --- */
*
* Returns: ---
*
- * Use: Creates a new admin connection.
+ * Use: Creates a new admin connection. It's safe to call this
+ * before @a_init@ -- and, indeed, this makes sense if you also
+ * call @a_switcherr@ to report initialization errors through
+ * the administration machinery.
*/
void a_create(int fd_in, int fd_out, unsigned f)
void a_daemon(void) { flags |= F_DAEMON; }
-/* --- @a_init@ --- *
+/* --- @a_listen@ --- *
*
* Arguments: @const char *name@ = socket name to create
* @uid_t u@ = user to own the socket
* @gid_t g@ = group to own the socket
* @mode_t m@ = permissions to set on the socket
*
- * Returns: ---
+ * Returns: Zero on success, @-1@ on failure.
*
* Use: Creates the admin listening socket.
*/
-void a_init(const char *name, uid_t u, gid_t g, mode_t m)
+int a_listen(const char *name, uid_t u, gid_t g, mode_t m)
{
int fd;
int n = 5;
struct sockaddr_un sun;
- struct sigaction sa;
size_t sz;
mode_t omask;
-#ifdef HAVE_LIBADNS
- int err;
-#endif
-
- /* --- Create services table --- */
-
- sym_create(&a_svcs);
/* --- Set up the socket address --- */
sz = strlen(name) + 1;
- if (sz > sizeof(sun.sun_path))
- die(EXIT_FAILURE, "socket name `%s' too long", name);
+ if (sz > sizeof(sun.sun_path)) {
+ a_warn("ADMIN", "admin-socket", "%s", name, "name-too-long", A_END);
+ goto fail_0;
+ }
BURN(sun);
sun.sun_family = AF_UNIX;
memcpy(sun.sun_path, name, sz);
omask = umask(0077);
again:
- if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
- die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno));
+ if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "create-failed", "?ERRNO", A_END);
+ goto fail_1;
+ }
if (bind(fd, (struct sockaddr *)&sun, sz) < 0) {
struct stat st;
int e = errno;
if (errno != EADDRINUSE) {
- die(EXIT_FAILURE, "couldn't bind to address `%s': %s",
- sun.sun_path, strerror(e));
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "bind-failed", "?ERRNO", A_END);
+ goto fail_2;
+ }
+ if (!n) {
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "too-many-retries", A_END);
+ goto fail_2;
}
- if (!n)
- die(EXIT_FAILURE, "too many retries; giving up");
n--;
if (!connect(fd, (struct sockaddr *)&sun, sz)) {
- die(EXIT_FAILURE, "server already listening on admin socket `%s'",
- sun.sun_path);
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "already-in-use", A_END);
+ goto fail_2;
+ }
+ if (errno != ECONNREFUSED) {
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "bind-failed", "?ERR", e, A_END);
+ goto fail_2;
}
- if (errno != ECONNREFUSED)
- die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e));
if (stat(sun.sun_path, &st)) {
- die(EXIT_FAILURE, "couldn't stat `%s': %s",
- sun.sun_path, strerror(errno));
+ if (errno == ENOENT) { close(fd); goto again; }
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "stat-failed", "?ERRNO", A_END);
+ goto fail_2;
+ }
+ if (!S_ISSOCK(st.st_mode)) {
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "not-a-socket", A_END);
+ goto fail_2;
}
- if (!S_ISSOCK(st.st_mode))
- die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path);
T( trace(T_ADMIN, "admin: stale socket found; removing it"); )
unlink(sun.sun_path);
close(fd);
goto again;
}
if (chown(sun.sun_path, u, g)) {
- die(EXIT_FAILURE, "failed to set socket owner: %s",
- strerror(errno));
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "chown-failed", "?ERRNO", A_END);
+ goto fail_3;
}
if (chmod(sun.sun_path, m)) {
- die(EXIT_FAILURE, "failed to set socket permissions: %s",
- strerror(errno));
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "chmod-failed", "?ERRNO", A_END);
+ goto fail_3;
}
- umask(omask);
fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
- if (listen(fd, 5))
- die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno));
+ if (listen(fd, 5)) {
+ a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+ "listen-failed", "?ERRNO", A_END);
+ goto fail_3;
+ }
+ umask(omask);
/* --- Listen to the socket --- */
sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0);
sel_addfile(&sock);
sockname = name;
+
+ return (0);
+
+ /* --- Clean up if things go sideways --- */
+
+fail_3:
+ unlink(sun.sun_path);
+fail_2:
+ close(fd);
+fail_1:
+ umask(omask);
+fail_0:
+ return (-1);
+}
+
+/* --- @a_unlisten@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Stops listening to the administration socket and removes it.
+ */
+
+void a_unlisten(void)
+{
+ if (!sockname) return;
+ sel_rmfile(&sock);
+ unlink(sockname);
+ close(sock.fd);
+}
+
+/* --- @a_switcherr@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Arrange to report warnings, trace messages, etc. to
+ * administration clients rather than the standard-error stream.
+ *
+ * Obviously this makes no sense unless there is at least one
+ * client established. Calling @a_listen@ won't help with this,
+ * because the earliest a new client can connect is during the
+ * first select-loop iteration, which is too late: some initial
+ * client must have been added manually using @a_create@.
+ */
+
+void a_switcherr(void)
+{
+ T( trace_custom(a_trace, 0);
+ trace(T_ADMIN, "admin: enabled custom tracing"); )
+ flags |= F_INIT;
+}
+
+/* --- @a_signals@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Establishes handlers for the obvious signals.
+ */
+
+void a_signals(void)
+{
+ struct sigaction sa;
+
+ sig_add(&s_term, SIGTERM, a_sigdie, 0);
+ sig_add(&s_hup, SIGHUP, a_sighup, 0);
+ sigaction(SIGINT, 0, &sa);
+ if (sa.sa_handler != SIG_IGN)
+ sig_add(&s_int, SIGINT, a_sigdie, 0);
+}
+
+/* --- @a_init@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Zero on success, @-1@ on failure.
+ *
+ * Use: Creates the admin listening socket.
+ */
+
+int a_init(void)
+{
+#ifdef HAVE_LIBADNS
+ int err;
+#endif
+
+ /* --- Prepare the background name resolver --- */
+
#ifdef HAVE_LIBADNS
if ((err = adns_init(&ads,
(adns_if_permit_ipv4 | adns_if_permit_ipv6 |
adns_if_noserverwarn | adns_if_nosigpipe |
adns_if_noautosys),
- 0)) != 0)
- die(EXIT_FAILURE, "failed to initialize ADNS: %s", strerror(errno));
+ 0)) != 0) {
+ a_warn("ADMIN", "adns-init-failed", "?ERRNO", A_END);
+ return (-1);
+ }
sel_addhook(&sel, &hook, before_select, after_select, 0);
#else
bres_init(&sel);
#endif
- T( trace_custom(a_trace, 0);
- trace(T_ADMIN, "admin: enabled custom tracing"); )
- flags |= F_INIT;
- /* --- Set up signal handlers --- */
+ /* --- Create services table --- */
- sig_add(&s_term, SIGTERM, a_sigdie, 0);
- sig_add(&s_hup, SIGHUP, a_sighup, 0);
- sigaction(SIGINT, 0, &sa);
- if (sa.sa_handler != SIG_IGN)
- sig_add(&s_int, SIGINT, a_sigdie, 0);
+ sym_create(&a_svcs);
+
+ /* --- All done --- */
+
+ return (0);
}
/*----- That's all, folks -------------------------------------------------*/