run.c revision 302408
1227825Stheraven/*	$NetBSD: run.c,v 1.14 2016/04/04 15:52:56 christos Exp $	*/
2227825Stheraven
3227825Stheraven/*-
4227825Stheraven * Copyright (c) 2015 The NetBSD Foundation, Inc.
5227825Stheraven * All rights reserved.
6227825Stheraven *
7227825Stheraven * This code is derived from software contributed to The NetBSD Foundation
8227825Stheraven * by Christos Zoulas.
9227825Stheraven *
10227825Stheraven * Redistribution and use in source and binary forms, with or without
11227825Stheraven * modification, are permitted provided that the following conditions
12227825Stheraven * are met:
13227825Stheraven * 1. Redistributions of source code must retain the above copyright
14227825Stheraven *    notice, this list of conditions and the following disclaimer.
15227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright
16227825Stheraven *    notice, this list of conditions and the following disclaimer in the
17227825Stheraven *    documentation and/or other materials provided with the distribution.
18227825Stheraven *
19227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20227825Stheraven * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21227825Stheraven * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22227825Stheraven * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23227825Stheraven * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24227825Stheraven * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25227825Stheraven * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26227825Stheraven * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27227825Stheraven * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28227825Stheraven * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29227825Stheraven * POSSIBILITY OF SUCH DAMAGE.
30227825Stheraven */
31262801Sdim#ifdef HAVE_CONFIG_H
32227825Stheraven#include "config.h"
33227825Stheraven#endif
34227825Stheraven
35227825Stheraven#include <sys/cdefs.h>
36227825Stheraven__RCSID("$NetBSD: run.c,v 1.14 2016/04/04 15:52:56 christos Exp $");
37227825Stheraven
38227825Stheraven#include <stdio.h>
39227825Stheraven#ifdef HAVE_LIBUTIL_H
40227825Stheraven#include <libutil.h>
41227825Stheraven#endif
42227825Stheraven#ifdef HAVE_UTIL_H
43227825Stheraven#include <util.h>
44227825Stheraven#endif
45227825Stheraven#include <stdarg.h>
46227825Stheraven#include <limits.h>
47227825Stheraven#include <stdlib.h>
48227825Stheraven#include <inttypes.h>
49227825Stheraven#include <syslog.h>
50227825Stheraven#include <string.h>
51227825Stheraven#include <netinet/in.h>
52227825Stheraven#include <net/if.h>
53227825Stheraven
54227825Stheraven#include "run.h"
55227825Stheraven#include "conf.h"
56227825Stheraven#include "internal.h"
57227825Stheraven#include "support.h"
58227825Stheraven
59227825Stheravenextern char **environ;
60227825Stheraven
61227825Stheravenstatic char *
62227825Stheravenrun(const char *cmd, const char *name, ...)
63227825Stheraven{
64227825Stheraven	const char *argv[20];
65227825Stheraven	size_t i;
66227825Stheraven	va_list ap;
67227825Stheraven	FILE *fp;
68227825Stheraven	char buf[10240], *res;
69227825Stheraven
70227825Stheraven	argv[0] = "control";
71227825Stheraven	argv[1] = cmd;
72227825Stheraven	argv[2] = name;
73227825Stheraven	va_start(ap, name);
74227825Stheraven	for (i = 3; i < __arraycount(argv) &&
75227825Stheraven	    (argv[i] = va_arg(ap, char *)) != NULL; i++)
76227825Stheraven		continue;
77227825Stheraven	va_end(ap);
78227825Stheraven
79227825Stheraven	if (debug) {
80227825Stheraven		size_t z;
81227825Stheraven		int r;
82227825Stheraven
83227825Stheraven		r = snprintf(buf, sizeof(buf), "run %s [", controlprog);
84227825Stheraven		if (r == -1 || (z = (size_t)r) >= sizeof(buf))
85227825Stheraven			z = sizeof(buf);
86227825Stheraven		for (i = 0; argv[i]; i++) {
87227825Stheraven			r = snprintf(buf + z, sizeof(buf) - z, "%s%s",
88227825Stheraven			    argv[i], argv[i + 1] ? " " : "");
89227825Stheraven			if (r == -1 || (z += (size_t)r) >= sizeof(buf))
90227825Stheraven				z = sizeof(buf);
91227825Stheraven		}
92227825Stheraven		(*lfun)(LOG_DEBUG, "%s]", buf);
93227825Stheraven	}
94278724Sdim
95227825Stheraven	fp = popenve(controlprog, __UNCONST(argv), environ, "r");
96227825Stheraven	if (fp == NULL) {
97227825Stheraven		(*lfun)(LOG_ERR, "popen %s failed (%m)", controlprog);
98227825Stheraven		return NULL;
99227825Stheraven	}
100227825Stheraven	if (fgets(buf, sizeof(buf), fp) != NULL)
101227825Stheraven		res = strdup(buf);
102227825Stheraven	else
103227825Stheraven		res = NULL;
104227825Stheraven	pclose(fp);
105227825Stheraven	if (debug)
106227825Stheraven		(*lfun)(LOG_DEBUG, "%s returns %s", cmd, res);
107227825Stheraven	return res;
108227825Stheraven}
109227825Stheraven
110227825Stheravenvoid
111227825Stheravenrun_flush(const struct conf *c)
112227825Stheraven{
113227825Stheraven	free(run("flush", c->c_name, NULL));
114227825Stheraven}
115227825Stheraven
116227825Stheravenint
117227825Stheravenrun_change(const char *how, const struct conf *c, char *id, size_t len)
118227825Stheraven{
119227825Stheraven	const char *prname;
120227825Stheraven	char poname[64], adname[128], maskname[32], *rv;
121227825Stheraven	size_t off;
122227825Stheraven
123227825Stheraven	switch (c->c_proto) {
124227825Stheraven	case -1:
125227825Stheraven		prname = "";
126227825Stheraven		break;
127227825Stheraven	case IPPROTO_TCP:
128227825Stheraven		prname = "tcp";
129227825Stheraven		break;
130227825Stheraven	case IPPROTO_UDP:
131227825Stheraven		prname = "udp";
132227825Stheraven		break;
133227825Stheraven	default:
134249998Sdim		(*lfun)(LOG_ERR, "%s: bad protocol %d", __func__, c->c_proto);
135227825Stheraven		return -1;
136227825Stheraven	}
137227825Stheraven
138227825Stheraven	if (c->c_port != -1)
139227825Stheraven		snprintf(poname, sizeof(poname), "%d", c->c_port);
140227825Stheraven	else
141227825Stheraven		poname[0] = '\0';
142253159Stheraven
143253159Stheraven	snprintf(maskname, sizeof(maskname), "%d", c->c_lmask);
144253159Stheraven	sockaddr_snprintf(adname, sizeof(adname), "%a", (const void *)&c->c_ss);
145253159Stheraven
146253159Stheraven	rv = run(how, c->c_name, prname, adname, maskname, poname, id, NULL);
147253159Stheraven	if (rv == NULL)
148253159Stheraven		return -1;
149253159Stheraven	if (len != 0) {
150253159Stheraven		rv[strcspn(rv, "\n")] = '\0';
151253159Stheraven		off = strncmp(rv, "OK ", 3) == 0 ? 3 : 0;
152253159Stheraven		strlcpy(id, rv + off, len);
153253159Stheraven	}
154253159Stheraven	free(rv);
155253159Stheraven	return 0;
156253159Stheraven}
157253159Stheraven