Remove building with NOCRYPTO option
[minix.git] / external / bsd / blacklist / bin / run.c
blob2f1a3c718e4704f6f7ef45da8108905e70de1780
1 /* $NetBSD: run.c,v 1.13 2015/06/02 14:02:10 christos Exp $ */
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: run.c,v 1.13 2015/06/02 14:02:10 christos Exp $");
38 #include <stdio.h>
39 #ifdef HAVE_UTIL_H
40 #include <util.h>
41 #endif
42 #include <stdarg.h>
43 #include <limits.h>
44 #include <stdlib.h>
45 #include <inttypes.h>
46 #include <syslog.h>
47 #include <string.h>
48 #include <netinet/in.h>
50 #include "run.h"
51 #include "conf.h"
52 #include "internal.h"
54 extern char **environ;
56 static char *
57 run(const char *cmd, const char *name, ...)
59 const char *argv[20];
60 size_t i;
61 va_list ap;
62 FILE *fp;
63 char buf[10240], *res;
65 argv[0] = "control";
66 argv[1] = cmd;
67 argv[2] = name;
68 va_start(ap, name);
69 for (i = 3; i < __arraycount(argv) &&
70 (argv[i] = va_arg(ap, char *)) != NULL; i++)
71 continue;
72 va_end(ap);
74 if (debug) {
75 size_t z;
76 int r;
78 r = snprintf(buf, sizeof(buf), "run %s [", controlprog);
79 if (r == -1 || (z = (size_t)r) >= sizeof(buf))
80 z = sizeof(buf);
81 for (i = 0; argv[i]; i++) {
82 r = snprintf(buf + z, sizeof(buf) - z, "%s%s",
83 argv[i], argv[i + 1] ? " " : "");
84 if (r == -1 || (z += (size_t)r) >= sizeof(buf))
85 z = sizeof(buf);
87 (*lfun)(LOG_DEBUG, "%s]", buf);
90 fp = popenve(controlprog, __UNCONST(argv), environ, "r");
91 if (fp == NULL) {
92 (*lfun)(LOG_ERR, "popen %s failed (%m)", controlprog);
93 return NULL;
95 if (fgets(buf, sizeof(buf), fp) != NULL)
96 res = strdup(buf);
97 else
98 res = NULL;
99 pclose(fp);
100 if (debug)
101 (*lfun)(LOG_DEBUG, "%s returns %s", cmd, res);
102 return res;
105 void
106 run_flush(const struct conf *c)
108 free(run("flush", c->c_name, NULL));
112 run_change(const char *how, const struct conf *c, char *id, size_t len)
114 const char *prname;
115 char poname[64], adname[128], maskname[32], *rv;
116 size_t off;
118 switch (c->c_proto) {
119 case -1:
120 prname = "";
121 break;
122 case IPPROTO_TCP:
123 prname = "tcp";
124 break;
125 case IPPROTO_UDP:
126 prname = "udp";
127 break;
128 default:
129 (*lfun)(LOG_ERR, "%s: bad protocol %d", __func__, c->c_proto);
130 return -1;
133 if (c->c_port != -1)
134 snprintf(poname, sizeof(poname), "%d", c->c_port);
135 else
136 poname[0] = '\0';
138 snprintf(maskname, sizeof(maskname), "%d", c->c_lmask);
139 sockaddr_snprintf(adname, sizeof(adname), "%a", (const void *)&c->c_ss);
141 rv = run(how, c->c_name, prname, adname, maskname, poname, id, NULL);
142 if (rv == NULL)
143 return -1;
144 if (len != 0) {
145 rv[strcspn(rv, "\n")] = '\0';
146 off = strncmp(rv, "OK ", 3) == 0 ? 3 : 0;
147 strlcpy(id, rv + off, len);
149 free(rv);
150 return 0;