1*5f4c09ddSEd Maste /* $NetBSD: run.c,v 1.14 2016/04/04 15:52:56 christos Exp $ */
2*5f4c09ddSEd Maste
3*5f4c09ddSEd Maste /*-
4*5f4c09ddSEd Maste * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*5f4c09ddSEd Maste * All rights reserved.
6*5f4c09ddSEd Maste *
7*5f4c09ddSEd Maste * This code is derived from software contributed to The NetBSD Foundation
8*5f4c09ddSEd Maste * by Christos Zoulas.
9*5f4c09ddSEd Maste *
10*5f4c09ddSEd Maste * Redistribution and use in source and binary forms, with or without
11*5f4c09ddSEd Maste * modification, are permitted provided that the following conditions
12*5f4c09ddSEd Maste * are met:
13*5f4c09ddSEd Maste * 1. Redistributions of source code must retain the above copyright
14*5f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer.
15*5f4c09ddSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
16*5f4c09ddSEd Maste * notice, this list of conditions and the following disclaimer in the
17*5f4c09ddSEd Maste * documentation and/or other materials provided with the distribution.
18*5f4c09ddSEd Maste *
19*5f4c09ddSEd Maste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*5f4c09ddSEd Maste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*5f4c09ddSEd Maste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*5f4c09ddSEd Maste * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*5f4c09ddSEd Maste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*5f4c09ddSEd Maste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*5f4c09ddSEd Maste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*5f4c09ddSEd Maste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*5f4c09ddSEd Maste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*5f4c09ddSEd Maste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*5f4c09ddSEd Maste * POSSIBILITY OF SUCH DAMAGE.
30*5f4c09ddSEd Maste */
31*5f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
32*5f4c09ddSEd Maste #include "config.h"
33*5f4c09ddSEd Maste #endif
34*5f4c09ddSEd Maste
35*5f4c09ddSEd Maste #include <sys/cdefs.h>
36*5f4c09ddSEd Maste __RCSID("$NetBSD: run.c,v 1.14 2016/04/04 15:52:56 christos Exp $");
37*5f4c09ddSEd Maste
38*5f4c09ddSEd Maste #include <stdio.h>
39*5f4c09ddSEd Maste #ifdef HAVE_LIBUTIL_H
40*5f4c09ddSEd Maste #include <libutil.h>
41*5f4c09ddSEd Maste #endif
42*5f4c09ddSEd Maste #ifdef HAVE_UTIL_H
43*5f4c09ddSEd Maste #include <util.h>
44*5f4c09ddSEd Maste #endif
45*5f4c09ddSEd Maste #include <stdarg.h>
46*5f4c09ddSEd Maste #include <limits.h>
47*5f4c09ddSEd Maste #include <stdlib.h>
48*5f4c09ddSEd Maste #include <inttypes.h>
49*5f4c09ddSEd Maste #include <syslog.h>
50*5f4c09ddSEd Maste #include <string.h>
51*5f4c09ddSEd Maste #include <netinet/in.h>
52*5f4c09ddSEd Maste #include <net/if.h>
53*5f4c09ddSEd Maste
54*5f4c09ddSEd Maste #include "run.h"
55*5f4c09ddSEd Maste #include "conf.h"
56*5f4c09ddSEd Maste #include "internal.h"
57*5f4c09ddSEd Maste #include "support.h"
58*5f4c09ddSEd Maste
59*5f4c09ddSEd Maste extern char **environ;
60*5f4c09ddSEd Maste
61*5f4c09ddSEd Maste static char *
run(const char * cmd,const char * name,...)62*5f4c09ddSEd Maste run(const char *cmd, const char *name, ...)
63*5f4c09ddSEd Maste {
64*5f4c09ddSEd Maste const char *argv[20];
65*5f4c09ddSEd Maste size_t i;
66*5f4c09ddSEd Maste va_list ap;
67*5f4c09ddSEd Maste FILE *fp;
68*5f4c09ddSEd Maste char buf[10240], *res;
69*5f4c09ddSEd Maste
70*5f4c09ddSEd Maste argv[0] = "control";
71*5f4c09ddSEd Maste argv[1] = cmd;
72*5f4c09ddSEd Maste argv[2] = name;
73*5f4c09ddSEd Maste va_start(ap, name);
74*5f4c09ddSEd Maste for (i = 3; i < __arraycount(argv) &&
75*5f4c09ddSEd Maste (argv[i] = va_arg(ap, char *)) != NULL; i++)
76*5f4c09ddSEd Maste continue;
77*5f4c09ddSEd Maste va_end(ap);
78*5f4c09ddSEd Maste
79*5f4c09ddSEd Maste if (debug) {
80*5f4c09ddSEd Maste size_t z;
81*5f4c09ddSEd Maste int r;
82*5f4c09ddSEd Maste
83*5f4c09ddSEd Maste r = snprintf(buf, sizeof(buf), "run %s [", controlprog);
84*5f4c09ddSEd Maste if (r == -1 || (z = (size_t)r) >= sizeof(buf))
85*5f4c09ddSEd Maste z = sizeof(buf);
86*5f4c09ddSEd Maste for (i = 0; argv[i]; i++) {
87*5f4c09ddSEd Maste r = snprintf(buf + z, sizeof(buf) - z, "%s%s",
88*5f4c09ddSEd Maste argv[i], argv[i + 1] ? " " : "");
89*5f4c09ddSEd Maste if (r == -1 || (z += (size_t)r) >= sizeof(buf))
90*5f4c09ddSEd Maste z = sizeof(buf);
91*5f4c09ddSEd Maste }
92*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "%s]", buf);
93*5f4c09ddSEd Maste }
94*5f4c09ddSEd Maste
95*5f4c09ddSEd Maste fp = popenve(controlprog, __UNCONST(argv), environ, "r");
96*5f4c09ddSEd Maste if (fp == NULL) {
97*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "popen %s failed (%m)", controlprog);
98*5f4c09ddSEd Maste return NULL;
99*5f4c09ddSEd Maste }
100*5f4c09ddSEd Maste if (fgets(buf, sizeof(buf), fp) != NULL)
101*5f4c09ddSEd Maste res = strdup(buf);
102*5f4c09ddSEd Maste else
103*5f4c09ddSEd Maste res = NULL;
104*5f4c09ddSEd Maste pclose(fp);
105*5f4c09ddSEd Maste if (debug)
106*5f4c09ddSEd Maste (*lfun)(LOG_DEBUG, "%s returns %s", cmd, res);
107*5f4c09ddSEd Maste return res;
108*5f4c09ddSEd Maste }
109*5f4c09ddSEd Maste
110*5f4c09ddSEd Maste void
run_flush(const struct conf * c)111*5f4c09ddSEd Maste run_flush(const struct conf *c)
112*5f4c09ddSEd Maste {
113*5f4c09ddSEd Maste free(run("flush", c->c_name, NULL));
114*5f4c09ddSEd Maste }
115*5f4c09ddSEd Maste
116*5f4c09ddSEd Maste int
run_change(const char * how,const struct conf * c,char * id,size_t len)117*5f4c09ddSEd Maste run_change(const char *how, const struct conf *c, char *id, size_t len)
118*5f4c09ddSEd Maste {
119*5f4c09ddSEd Maste const char *prname;
120*5f4c09ddSEd Maste char poname[64], adname[128], maskname[32], *rv;
121*5f4c09ddSEd Maste size_t off;
122*5f4c09ddSEd Maste
123*5f4c09ddSEd Maste switch (c->c_proto) {
124*5f4c09ddSEd Maste case -1:
125*5f4c09ddSEd Maste prname = "";
126*5f4c09ddSEd Maste break;
127*5f4c09ddSEd Maste case IPPROTO_TCP:
128*5f4c09ddSEd Maste prname = "tcp";
129*5f4c09ddSEd Maste break;
130*5f4c09ddSEd Maste case IPPROTO_UDP:
131*5f4c09ddSEd Maste prname = "udp";
132*5f4c09ddSEd Maste break;
133*5f4c09ddSEd Maste default:
134*5f4c09ddSEd Maste (*lfun)(LOG_ERR, "%s: bad protocol %d", __func__, c->c_proto);
135*5f4c09ddSEd Maste return -1;
136*5f4c09ddSEd Maste }
137*5f4c09ddSEd Maste
138*5f4c09ddSEd Maste if (c->c_port != -1)
139*5f4c09ddSEd Maste snprintf(poname, sizeof(poname), "%d", c->c_port);
140*5f4c09ddSEd Maste else
141*5f4c09ddSEd Maste poname[0] = '\0';
142*5f4c09ddSEd Maste
143*5f4c09ddSEd Maste snprintf(maskname, sizeof(maskname), "%d", c->c_lmask);
144*5f4c09ddSEd Maste sockaddr_snprintf(adname, sizeof(adname), "%a", (const void *)&c->c_ss);
145*5f4c09ddSEd Maste
146*5f4c09ddSEd Maste rv = run(how, c->c_name, prname, adname, maskname, poname, id, NULL);
147*5f4c09ddSEd Maste if (rv == NULL)
148*5f4c09ddSEd Maste return -1;
149*5f4c09ddSEd Maste if (len != 0) {
150*5f4c09ddSEd Maste rv[strcspn(rv, "\n")] = '\0';
151*5f4c09ddSEd Maste off = strncmp(rv, "OK ", 3) == 0 ? 3 : 0;
152*5f4c09ddSEd Maste strlcpy(id, rv + off, len);
153*5f4c09ddSEd Maste }
154*5f4c09ddSEd Maste free(rv);
155*5f4c09ddSEd Maste return 0;
156*5f4c09ddSEd Maste }
157