xref: /freebsd/contrib/blocklist/bin/run.c (revision 3494f7c0)
1 /*	$NetBSD: run.c,v 1.14 2016/04/04 15:52:56 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 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: run.c,v 1.14 2016/04/04 15:52:56 christos Exp $");
37 
38 #include <stdio.h>
39 #ifdef HAVE_LIBUTIL_H
40 #include <libutil.h>
41 #endif
42 #ifdef HAVE_UTIL_H
43 #include <util.h>
44 #endif
45 #include <stdarg.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <inttypes.h>
49 #include <syslog.h>
50 #include <string.h>
51 #include <netinet/in.h>
52 #include <net/if.h>
53 
54 #include "run.h"
55 #include "conf.h"
56 #include "internal.h"
57 #include "support.h"
58 
59 extern char **environ;
60 
61 static char *
62 run(const char *cmd, const char *name, ...)
63 {
64 	const char *argv[20];
65 	size_t i;
66 	va_list ap;
67 	FILE *fp;
68 	char buf[10240], *res;
69 
70 	argv[0] = "control";
71 	argv[1] = cmd;
72 	argv[2] = name;
73 	va_start(ap, name);
74 	for (i = 3; i < __arraycount(argv) &&
75 	    (argv[i] = va_arg(ap, char *)) != NULL; i++)
76 		continue;
77 	va_end(ap);
78 
79 	if (debug) {
80 		size_t z;
81 		int r;
82 
83 		r = snprintf(buf, sizeof(buf), "run %s [", controlprog);
84 		if (r == -1 || (z = (size_t)r) >= sizeof(buf))
85 			z = sizeof(buf);
86 		for (i = 0; argv[i]; i++) {
87 			r = snprintf(buf + z, sizeof(buf) - z, "%s%s",
88 			    argv[i], argv[i + 1] ? " " : "");
89 			if (r == -1 || (z += (size_t)r) >= sizeof(buf))
90 				z = sizeof(buf);
91 		}
92 		(*lfun)(LOG_DEBUG, "%s]", buf);
93 	}
94 
95 	fp = popenve(controlprog, __UNCONST(argv), environ, "r");
96 	if (fp == NULL) {
97 		(*lfun)(LOG_ERR, "popen %s failed (%m)", controlprog);
98 		return NULL;
99 	}
100 	if (fgets(buf, sizeof(buf), fp) != NULL)
101 		res = strdup(buf);
102 	else
103 		res = NULL;
104 	pclose(fp);
105 	if (debug)
106 		(*lfun)(LOG_DEBUG, "%s returns %s", cmd, res);
107 	return res;
108 }
109 
110 void
111 run_flush(const struct conf *c)
112 {
113 	free(run("flush", c->c_name, NULL));
114 }
115 
116 int
117 run_change(const char *how, const struct conf *c, char *id, size_t len)
118 {
119 	const char *prname;
120 	char poname[64], adname[128], maskname[32], *rv;
121 	size_t off;
122 
123 	switch (c->c_proto) {
124 	case -1:
125 		prname = "";
126 		break;
127 	case IPPROTO_TCP:
128 		prname = "tcp";
129 		break;
130 	case IPPROTO_UDP:
131 		prname = "udp";
132 		break;
133 	default:
134 		(*lfun)(LOG_ERR, "%s: bad protocol %d", __func__, c->c_proto);
135 		return -1;
136 	}
137 
138 	if (c->c_port != -1)
139 		snprintf(poname, sizeof(poname), "%d", c->c_port);
140 	else
141 		poname[0] = '\0';
142 
143 	snprintf(maskname, sizeof(maskname), "%d", c->c_lmask);
144 	sockaddr_snprintf(adname, sizeof(adname), "%a", (const void *)&c->c_ss);
145 
146 	rv = run(how, c->c_name, prname, adname, maskname, poname, id, NULL);
147 	if (rv == NULL)
148 		return -1;
149 	if (len != 0) {
150 		rv[strcspn(rv, "\n")] = '\0';
151 		off = strncmp(rv, "OK ", 3) == 0 ? 3 : 0;
152 		strlcpy(id, rv + off, len);
153 	}
154 	free(rv);
155 	return 0;
156 }
157