summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorremi <remi@openbsd.org>2018-12-27 20:23:24 +0000
committerremi <remi@openbsd.org>2018-12-27 20:23:24 +0000
commit1adf61599be2d615e0edf9cc267a722004132276 (patch)
treee98c61c04c2fed34062ba02454379852d2d81761
parentWhen netbooting a vm using the `-B net' option, set the hostname DHCP (diff)
downloadwireguard-openbsd-1adf61599be2d615e0edf9cc267a722004132276.tar.xz
wireguard-openbsd-1adf61599be2d615e0edf9cc267a722004132276.zip
Check if a control socket or address is already in use befor using it.
If it is used abort startup or let a reload fail. Sockets are now not unlinked anymore on regular shutdown. This helps a lot when one tries to do a config check without -n. Inputs and OK claudio@
-rw-r--r--usr.sbin/bgpd/bgpd.c26
-rw-r--r--usr.sbin/bgpd/bgpd.h3
-rw-r--r--usr.sbin/bgpd/config.c8
-rw-r--r--usr.sbin/bgpd/control.c35
-rw-r--r--usr.sbin/bgpd/session.h5
5 files changed, 53 insertions, 24 deletions
diff --git a/usr.sbin/bgpd/bgpd.c b/usr.sbin/bgpd/bgpd.c
index 0c4fad69c0d..711551e5cdc 100644
--- a/usr.sbin/bgpd/bgpd.c
+++ b/usr.sbin/bgpd/bgpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bgpd.c,v 1.204 2018/09/29 08:11:11 claudio Exp $ */
+/* $OpenBSD: bgpd.c,v 1.205 2018/12/27 20:23:24 remi Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -348,8 +348,6 @@ BROKEN if (pledge("stdio rpath wpath cpath fattr unix route recvfd sendfd",
free(p);
}
- control_cleanup(conf->csock);
- control_cleanup(conf->rcsock);
carp_demote_shutdown();
kr_shutdown(conf->fib_priority, conf->default_tableid);
pftable_clear_all();
@@ -453,10 +451,20 @@ reconfigure(char *conffile, struct bgpd_config *conf, struct peer **peer_l)
reconfpending = 0;
return (1);
}
+
+ if (prepare_listeners(conf) == -1) {
+ reconfpending = 0;
+ return (1);
+ }
+
+ if (control_setup(conf) == -1) {
+ reconfpending = 0;
+ return (1);
+ }
+
expand_networks(conf);
cflags = conf->flags;
- prepare_listeners(conf);
/* start reconfiguration */
if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 0, -1,
@@ -473,9 +481,6 @@ reconfigure(char *conffile, struct bgpd_config *conf, struct peer **peer_l)
la->fd = -1;
}
- if (control_setup(conf) == -1)
- return (-1);
-
/* adjust fib syncing on reload */
ktable_preload();
@@ -934,11 +939,12 @@ control_setup(struct bgpd_config *conf)
/* control socket is outside chroot */
if (!cname || strcmp(cname, conf->csock)) {
if (cname) {
- control_cleanup(cname);
free(cname);
}
if ((cname = strdup(conf->csock)) == NULL)
fatal("strdup");
+ if (control_check(cname) == -1)
+ return (-1);
if ((fd = control_init(0, cname)) == -1)
fatalx("control socket setup failed");
if (control_listen(fd) == -1)
@@ -950,16 +956,16 @@ control_setup(struct bgpd_config *conf)
}
if (!conf->rcsock) {
/* remove restricted socket */
- control_cleanup(rcname);
free(rcname);
rcname = NULL;
} else if (!rcname || strcmp(rcname, conf->rcsock)) {
if (rcname) {
- control_cleanup(rcname);
free(rcname);
}
if ((rcname = strdup(conf->rcsock)) == NULL)
fatal("strdup");
+ if (control_check(rcname) == -1)
+ return (-1);
if ((fd = control_init(1, rcname)) == -1)
fatalx("control socket setup failed");
if (control_listen(fd) == -1)
diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h
index 7f981155023..322fb047d25 100644
--- a/usr.sbin/bgpd/bgpd.h
+++ b/usr.sbin/bgpd/bgpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: bgpd.h,v 1.359 2018/12/22 16:12:40 claudio Exp $ */
+/* $OpenBSD: bgpd.h,v 1.360 2018/12/27 20:23:24 remi Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -1127,7 +1127,6 @@ void set_pollfd(struct pollfd *, struct imsgbuf *);
int handle_pollfd(struct pollfd *, struct imsgbuf *);
/* control.c */
-void control_cleanup(const char *);
int control_imsg_relay(struct imsg *);
/* config.c */
diff --git a/usr.sbin/bgpd/config.c b/usr.sbin/bgpd/config.c
index 6cc77d2d1c6..d0546dfaebe 100644
--- a/usr.sbin/bgpd/config.c
+++ b/usr.sbin/bgpd/config.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: config.c,v 1.78 2018/11/14 17:24:01 mestre Exp $ */
+/* $OpenBSD: config.c,v 1.79 2018/12/27 20:23:24 remi Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org>
@@ -384,11 +384,12 @@ host_ip(const char *s, struct bgpd_addr *h, u_int8_t *len)
return (1);
}
-void
+int
prepare_listeners(struct bgpd_config *conf)
{
struct listen_addr *la, *next;
int opt = 1;
+ int r = 0;
if (TAILQ_EMPTY(conf->listen_addrs)) {
if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
@@ -459,9 +460,12 @@ prepare_listeners(struct bgpd_config *conf)
close(la->fd);
TAILQ_REMOVE(conf->listen_addrs, la, entry);
free(la);
+ r = -1;
continue;
}
}
+
+ return (r);
}
int
diff --git a/usr.sbin/bgpd/control.c b/usr.sbin/bgpd/control.c
index 0e2b1813eb8..1239b5e811f 100644
--- a/usr.sbin/bgpd/control.c
+++ b/usr.sbin/bgpd/control.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.c,v 1.92 2018/12/22 16:12:40 claudio Exp $ */
+/* $OpenBSD: control.c,v 1.93 2018/12/27 20:23:24 remi Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -38,6 +38,32 @@ void control_result(struct ctl_conn *, u_int);
ssize_t imsg_read_nofd(struct imsgbuf *);
int
+control_check(char *path)
+{
+ struct sockaddr_un sun;
+ int fd;
+
+ bzero(&sun, sizeof(sun));
+ sun.sun_family = AF_UNIX;
+ strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+ log_warn("%s: socket", __func__);
+ return (-1);
+ }
+
+ if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
+ log_warnx("control socket %s already in use", path);
+ close(fd);
+ return (-1);
+ }
+
+ close(fd);
+
+ return (0);
+}
+
+int
control_init(int restricted, char *path)
{
struct sockaddr_un sun;
@@ -110,13 +136,6 @@ control_shutdown(int fd)
close(fd);
}
-void
-control_cleanup(const char *path)
-{
- if (path)
- unlink(path);
-}
-
unsigned int
control_accept(int listenfd, int restricted)
{
diff --git a/usr.sbin/bgpd/session.h b/usr.sbin/bgpd/session.h
index 1c87025d7ef..5a90f294236 100644
--- a/usr.sbin/bgpd/session.h
+++ b/usr.sbin/bgpd/session.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.h,v 1.126 2018/12/22 16:12:40 claudio Exp $ */
+/* $OpenBSD: session.h,v 1.127 2018/12/27 20:23:24 remi Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -247,10 +247,11 @@ int carp_demote_set(char *, int);
/* config.c */
int merge_config(struct bgpd_config *, struct bgpd_config *,
struct peer *);
-void prepare_listeners(struct bgpd_config *);
+int prepare_listeners(struct bgpd_config *);
int get_mpe_label(struct rdomain *);
/* control.c */
+int control_check(char *);
int control_init(int, char *);
int control_listen(int);
void control_shutdown(int);