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