etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / nscomm.c
blob19435a60c1362def57774e5adbf7695c4eb0f474
1 /* $NetBSD: nscomm.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
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.
11 ** Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions
13 ** are met:
15 ** Redistributions of source code must retain the above copyright notice,
16 ** this list of conditions and the following disclaimer.
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.
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.
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.
38 *****************************************************************/
39 # include <stdio.h>
41 #ifdef HAVE_CONFIG_H
42 # include <config.h>
43 #endif
45 #include "config_zkt.h"
46 #include "zconf.h"
47 #define extern
48 #include "nscomm.h"
49 #undef extern
52 /*****************************************************************
53 ** dyn_update_freeze ()
54 *****************************************************************/
55 int dyn_update_freeze (const char *domain, const zconf_t *z, int freeze)
57 char cmdline[254+1];
58 char str[254+1];
59 char *action;
60 FILE *fp;
62 assert (z != NULL);
63 if ( freeze )
64 action = "freeze";
65 else
66 action = "thaw";
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);
73 lg_mesg (LG_NOTICE, "%s: %s dynamic zone", str, action);
74 verbmesg (1, z, "\t%s dynamic zone %s\n", action, str);
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);
81 verbmesg (2, z, "\t Run cmd \"%s\"\n", cmdline);
82 *str = '\0';
83 if ( z->noexec == 0 )
85 if ( (fp = popen (cmdline, "r")) == NULL || fgets (str, sizeof str, fp) == NULL )
86 return -1;
87 pclose (fp);
90 verbmesg (2, z, "\t rndc %s return: \"%s\"\n", action, str_chop (str, '\n'));
92 return 0;
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 *****************************************************************/
101 int dist_and_reload (const zone_t *zp, int what)
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;
110 assert (zp != NULL);
111 assert (zp->conf->dist_cmd != NULL);
112 assert ( what == 1 || what == 2 );
114 if ( zp->conf->dist_cmd == NULL )
115 return 0;
117 if ( !is_exec_ok (zp->conf->dist_cmd) )
119 char *mesg;
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";
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);
129 return -1;
132 view = ""; /* default is an empty view string */
133 if ( zp->conf->view )
135 snprintf (zone, sizeof (zone), "\"%s\" in view \"%s\"", zp->zone, zp->conf->view);
136 view = zp->conf->view;
138 else
139 snprintf (zone, sizeof (zone), "\"%s\"", zp->zone);
142 if ( what == 2 )
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 )
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'));
158 return 0;
161 pathname (path, sizeof (path), zp->dir, zp->sfile, NULL);
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);
167 *str = '\0';
168 if ( zp->conf->noexec == 0 )
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'));
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);
182 *str = '\0';
183 if ( zp->conf->noexec == 0 )
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'));
192 return 0;
195 /*****************************************************************
196 ** reload a zone via "rndc"
197 *****************************************************************/
198 int reload_zone (const char *domain, const zconf_t *z)
200 char cmdline[254+1];
201 char str[254+1];
202 FILE *fp;
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);
211 lg_mesg (LG_NOTICE, "%s: reload triggered", str);
212 verbmesg (1, z, "\tReload zone %s\n", str);
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);
219 *str = '\0';
220 if ( z->noexec == 0 )
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'));
229 return 0;