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