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