1 /*	$NetBSD: nscomm.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $	*/
2 
3 /*****************************************************************
4 **
5 **	@(#) nscomm.c  (c) 2005 - 2009  Holger Zuleger  hznet.de
6 **
7 **	Copyright (c) 2005 - 2009, Holger Zuleger HZnet. All rights reserved.
8 **
9 **	This software is open source.
10 **
11 **	Redistribution and use in source and binary forms, with or without
12 **	modification, are permitted provided that the following conditions
13 **	are met:
14 **
15 **	Redistributions of source code must retain the above copyright notice,
16 **	this list of conditions and the following disclaimer.
17 **
18 **	Redistributions in binary form must reproduce the above copyright notice,
19 **	this list of conditions and the following disclaimer in the documentation
20 **	and/or other materials provided with the distribution.
21 **
22 **	Neither the name of Holger Zuleger HZnet nor the names of its contributors may
23 **	be used to endorse or promote products derived from this software without
24 **	specific prior written permission.
25 **
26 **	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 **	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 **	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 **	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 **	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 **	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 **	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 **	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 **	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 **	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 **	POSSIBILITY OF SUCH DAMAGE.
37 **
38 *****************************************************************/
39 # include <stdio.h>
40 
41 #ifdef HAVE_CONFIG_H
42 # include <config.h>
43 #endif
44 
45 #include "config_zkt.h"
46 #include "zconf.h"
47 #define extern
48 #include "nscomm.h"
49 #undef extern
50 
51 
52 /*****************************************************************
53 **	dyn_update_freeze ()
54 *****************************************************************/
dyn_update_freeze(const char * domain,const zconf_t * z,int freeze)55 int	dyn_update_freeze (const char *domain, const zconf_t *z, int freeze)
56 {
57 	char	cmdline[254+1];
58 	char	str[254+1];
59 	char	*action;
60 	FILE	*fp;
61 
62 	assert (z != NULL);
63 	if ( freeze )
64 		action = "freeze";
65 	else
66 		action = "thaw";
67 
68 	if ( z->view )
69 		snprintf (str, sizeof (str), "\"%s\" in view \"%s\"", domain, z->view);
70 	else
71 		snprintf (str, sizeof (str), "\"%s\"", domain);
72 
73 	lg_mesg (LG_NOTICE, "%s: %s dynamic zone", str, action);
74 	verbmesg (1, z, "\t%s dynamic zone %s\n", action, str);
75 
76 	if ( z->view )
77 		snprintf (cmdline, sizeof (cmdline), "%s %s %s IN %s", RELOADCMD, action, domain, z->view);
78 	else
79 		snprintf (cmdline, sizeof (cmdline), "%s %s %s", RELOADCMD, action, domain);
80 
81 	verbmesg (2, z, "\t  Run cmd \"%s\"\n", cmdline);
82 	*str = '\0';
83 	if ( z->noexec == 0 )
84 	{
85 		if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
86 			return -1;
87 		pclose (fp);
88 	}
89 
90 	verbmesg (2, z, "\t  rndc %s return: \"%s\"\n", action, str_chop (str, '\n'));
91 
92 	return 0;
93 }
94 
95 /*****************************************************************
96 **	distribute and reload a zone via "distribute_command"
97 **	what is
98 **		1 for zone distribution and relaod
99 **		2 for key distribution (used by dynamic zoes)
100 *****************************************************************/
dist_and_reload(const zone_t * zp,int what)101 int	dist_and_reload (const zone_t *zp, int what)
102 {
103 	char	path[MAX_PATHSIZE+1];
104 	char	cmdline[254+1];
105 	char	zone[254+1];
106 	char	str[254+1];
107 	char	*view;
108 	FILE	*fp;
109 
110 	assert (zp != NULL);
111 	assert (zp->conf->dist_cmd != NULL);
112 	assert ( what == 1 || what == 2 );
113 
114 	if ( zp->conf->dist_cmd == NULL )
115 		return 0;
116 
117 	if ( !is_exec_ok (zp->conf->dist_cmd) )
118 	{
119 		char	*mesg;
120 
121 		if ( getuid () == 0 )
122 			mesg = "\tDistribution command %s not run as root\n";
123 		else
124 			mesg = "\tDistribution command %s not run due to strange file mode settings\n";
125 
126 		verbmesg (1, zp->conf, mesg, zp->conf->dist_cmd);
127 		lg_mesg (LG_ERROR, "exec of distribution command %s disabled due to security reasons", zp->conf->dist_cmd);
128 
129 		return -1;
130 	}
131 
132 	view = "";	/* default is an empty view string */
133 	if ( zp->conf->view )
134 	{
135 		snprintf (zone, sizeof (zone), "\"%s\" in view \"%s\"", zp->zone, zp->conf->view);
136 		view = zp->conf->view;
137 	}
138 	else
139 		snprintf (zone, sizeof (zone), "\"%s\"", zp->zone);
140 
141 
142 	if ( what == 2 )
143 	{
144 		lg_mesg (LG_NOTICE, "%s: key distribution triggered", zone);
145 		verbmesg (1, zp->conf, "\tDistribute keys for zone %s\n", zone);
146 		snprintf (cmdline, sizeof (cmdline), "%s distkeys %s %s %s",
147 					zp->conf->dist_cmd, zp->zone, path, view);
148 		*str = '\0';
149 		if ( zp->conf->noexec == 0 )
150 		{
151 			verbmesg (2, zp->conf, "\t  Run cmd \"%s\"\n", cmdline);
152 			if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
153 				return -2;
154 			pclose (fp);
155 			verbmesg (2, zp->conf, "\t  %s distribute return: \"%s\"\n", zp->conf->dist_cmd, str_chop (str, '\n'));
156 		}
157 
158 		return 0;
159 	}
160 
161 	pathname (path, sizeof (path), zp->dir, zp->sfile, NULL);
162 
163 	lg_mesg (LG_NOTICE, "%s: distribution triggered", zone);
164 	verbmesg (1, zp->conf, "\tDistribute zone %s\n", zone);
165 	snprintf (cmdline, sizeof (cmdline), "%s distribute %s %s %s", zp->conf->dist_cmd, zp->zone, path, view);
166 
167 	*str = '\0';
168 	if ( zp->conf->noexec == 0 )
169 	{
170 		verbmesg (2, zp->conf, "\t  Run cmd \"%s\"\n", cmdline);
171 		if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
172 			return -2;
173 		pclose (fp);
174 		verbmesg (2, zp->conf, "\t  %s distribute return: \"%s\"\n", zp->conf->dist_cmd, str_chop (str, '\n'));
175 	}
176 
177 
178 	lg_mesg (LG_NOTICE, "%s: reload triggered", zone);
179 	verbmesg (1, zp->conf, "\tReload zone %s\n", zone);
180 	snprintf (cmdline, sizeof (cmdline), "%s reload %s %s %s", zp->conf->dist_cmd, zp->zone, path, view);
181 
182 	*str = '\0';
183 	if ( zp->conf->noexec == 0 )
184 	{
185 		verbmesg (2, zp->conf, "\t  Run cmd \"%s\"\n", cmdline);
186 		if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
187 			return -2;
188 		pclose (fp);
189 		verbmesg (2, zp->conf, "\t  %s reload return: \"%s\"\n", zp->conf->dist_cmd, str_chop (str, '\n'));
190 	}
191 
192 	return 0;
193 }
194 
195 /*****************************************************************
196 **	reload a zone via "rndc"
197 *****************************************************************/
reload_zone(const char * domain,const zconf_t * z)198 int	reload_zone (const char *domain, const zconf_t *z)
199 {
200 	char	cmdline[254+1];
201 	char	str[254+1];
202 	FILE	*fp;
203 
204 	assert (z != NULL);
205 	dbg_val3 ("reload_zone %d :%s: :%s:\n", z->verbosity, domain, z->view);
206 	if ( z->view )
207 		snprintf (str, sizeof (str), "\"%s\" in view \"%s\"", domain, z->view);
208 	else
209 		snprintf (str, sizeof (str), "\"%s\"", domain);
210 
211 	lg_mesg (LG_NOTICE, "%s: reload triggered", str);
212 	verbmesg (1, z, "\tReload zone %s\n", str);
213 
214 	if ( z->view )
215 		snprintf (cmdline, sizeof (cmdline), "%s reload %s IN %s", RELOADCMD, domain, z->view);
216 	else
217 		snprintf (cmdline, sizeof (cmdline), "%s reload %s", RELOADCMD, domain);
218 
219 	*str = '\0';
220 	if ( z->noexec == 0 )
221 	{
222 		verbmesg (2, z, "\t  Run cmd \"%s\"\n", cmdline);
223 		if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
224 			return -1;
225 		pclose (fp);
226 		verbmesg (2, z, "\t  rndc reload return: \"%s\"\n", str_chop (str, '\n'));
227 	}
228 
229 	return 0;
230 }
231