-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate.php
523 lines (438 loc) · 13.4 KB
/
update.php
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
require 'inc/lib.inc';
if ($INTERFACE!=="stable") require 'header.php';
$start_frame = '
<HTML>
<HEAD>
<TITLE>Add Zone</TITLE>
</HEAD>
<FRAMESET rows="12,60%,*" frameborder="5" border="2" framespacing="0">
<FRAME src="topshadow.html" name="topshadow" noresize scrolling=no frameborder ="0" border="0" framespacing="0" marginheight="0" marginwidth="0">
<FRAME src="update.php?frame=MAIN" name="MAIN" scrolling=auto frameborder="5" border="3" framespacing="3" marginheight="0" marginwidth="10">
<FRAME src="update.php?frame=BLANK" name="VIEW" scrolling=auto frameborder="3" border="3" framespacing="3" marginheight="3" marginwidth="10">
</FRAMESET>
</HTML>
';
$html_top = '
<HTML><HEAD>
<TITLE>Proventum Push DNS Updates</TITLE>
<META http-equiv="Pragma" content="no-cache">
<LINK rel="stylesheet" href="style.css" type="text/css">
</HEAD><BODY bgcolor="cccc99" background="images/BG-shadowleft.gif">
<TABLE width="100%">
<TR>
<TD align=left><H3>Pushing DNS updates to the servers</H3></TD>
<TH align=right><A HREF="manual.html#push">Help</A></TH>
</TR>
</TABLE>
<HR>
';
$html_top1 = '
<HTML><HEAD>
<TITLE>Update LOGS</TITLE>
<META http-equiv="Pragma" content="no-cache">
<LINK rel="stylesheet" href="style.css" type="text/css">
</HEAD><BODY bgcolor="dddd99" background="images/BG-shadowtop.gif">
';
$html_bottom = '
</BODY>
</HTML>
';
$html_in_progress = '
<HTML><HEAD>
<TITLE>Push in progress</TITLE>
<META http-equiv="Pragma" content="no-cache">
<LINK rel="stylesheet" href="style.css" type="text/css">
</HEAD><BODY bgcolor="dddd99" background="images/BG-shadowtop.gif">
<TABLE width="100%">
<TR>
<TD align=left><H3>Pushing DNS updates to the servers</H3></TD>
</TR>
</TABLE>
<HR>
<CENTER>
<TABLE width="40%" bgcolor=RED border=10>
<TR><TD><IMAGE src="images/u0.gif" width=400 height=8></TD></TR>
<TR>
<TD ALIGN=CENTER><span style="color:#ffffff; font-size: 24px;"><strong>Push in progress.<br />Do not cancel.</strong></span></TD>
</TR>
<TR><TD><IMAGE src="images/u0.gif" width=400 height=8></TD></TR>
</TABLE>
</BODY></HTML>
';
$push_in_progress = "
<UL>
<B><BLINK>
%s is already running a push operation. The database is
locked until that process completes. Please be patient, as
a big update can take many minutes to complete.
</BLINK></B>
</UL>
<P>
If this condition persists, or you are otherwise convinced that
an error has occurred, then you can clear the lock condition
on the settings menu.
";
function main_update_menu($input)
{
global $TWO_STEP_UPDATE;
global $HOST_URL;
$db = new DB_probind;
adjust_serials();
$db->query("SELECT id FROM zones WHERE updated ");
$zones = $db->num_rows();
$db->query("SELECT id,hostname,ipno,type,state FROM servers WHERE pushupdates = 1");
print "<FORM action=\"update.php\"><INPUT type=\"HIDDEN\" name=\"frame\" value=\"VIEW\">\n";
$res = "<TABLE BORDER=2 width=\"70%\">\n";
$res .= "<TR><TH>name</TH><TH>ip address</TH><TH>type</TH><TH>state</TH><TH>Do not apply</TH><TH>View</TH><TH>Test</TH></TR>\n";
$gen_c = "";
$push_c = "";
$conf_c = "";
$appl_c = "CHECKED";
while ($db->next_record()) {
extract($db->Record);
$T = $state;
$B = "";
$skip_c = "";
switch ($state) {
case 'OK':
$T = "<B>OK</B>";
break;
case 'OUT':
$T = "<B>need update</B>";
$B = " bgcolor=yellow";
$gen_c="CHECKED";
$push_c = "CHECKED";
$conf_c = "CHECKED";
break;
case 'CHG':
$T = "<B>need push</B>";
$B = " bgcolor=yellow";
$push_c = "CHECKED";
$conf_c = "CHECKED";
break;
case 'CFG':
$T = "<B>need reconfig</B>";
$B = " bgcolor=yellow";
$conf_c = "CHECKED";
break;
case 'ERR':
$T = "<FONT COLOR=WHITE><BLINK>Update error</BLINK></FONT>";
$B = " bgcolor=red";
$skip_c = "CHECKED";
$gen_c = "CHECKED";
$push_c = "CHECKED";
break;
default: break;
}
$res .= "<TR>\n";
$res .= "\t<TD>$hostname</TD>\n";
$res .= "\t<TD>$ipno</TD>\n";
$res .= "\t<TD align=center>$type</TD>\n";
$res .= "\t<TD $B>$T</TD>\n";
$res .= "\t<TD align=center><INPUT TYPE=\"CHECKBOX\" $skip_c name=\"skip_$id\"></TD>\n";
$res .= "\t<TD align=center>";
$res .= "<A href=\"view.php?file=$hostname/named.conf\">named.conf</A>,";
$res .= "<A href=\"$HOST_URL/$hostname/\">files</A>";
$res .= "</TD>\n";
$res .= "\t<TD align=center><A href=\"test.php?id=$id\"><img src=\"images/greenbutton.gif\" border=0 high=16 width=24></A></TD>\n";
$res .= "</TR>\n";
}
$res .= "</TABLE>\n";
$up_text = "START UPDATE";
if ($TWO_STEP_UPDATE && ($gen_c || $push_c)) {
$conf_c = "";
}
if ($TWO_STEP_UPDATE && $conf_c)
$up_text = "FINISH UPDATE";
print "<TABLE width=\"70%\" border=\"1\">\n";
print "\t<TD>Generate files ($zones): <INPUT type=CHECKBOX name=\"gen\" $gen_c></TD>\n";
print "\t<TD>Push files: <INPUT type=CHECKBOX name=\"push\" $push_c></TD>\n";
print "\t<TD>Reconfig server: <INPUT type=CHECKBOX name=\"conf\" $conf_c></TD>\n";
print "</TR>\n";
print "<TR><TD colspan=3 align=center>";
print "<INPUT type=submit value=\"$up_text\" class=\"button\" onmouseover=\"this.className='buttonwarning'\" onmouseout=\"this.className='button'\">\n";
print "</TD></TR>\n";
print "</TABLE>\n";
print $res;
print "</FORM>\n";
exit;
}
function generate_files($input)
{
global $UPDATE_LOG;
global $A_LOG;
global $A_LOGE;
global $HOST_DIR;
global $HOST_URL;
global $TOP;
global $BIN;
global $SERVER_TYPES;
global $HOSTMASTER;
$skipped = 0;
$ret = '';
$error = 0;
$err = '';
$db = new DB_probind;
$db->prepare("SELECT id, domain, master, zonefile FROM zones WHERE updated AND domain != 'TEMPLATE' ORDER BY domain");
$db->execute();
$zones = $db->fetchAll();
$db->prepare("SELECT domain, zonefile FROM deleted_domains");
$db->execute();
$deleted_domains = $db->fetchAll();
$db->query("SELECT id as servid, hostname as server, type, zonedir FROM servers ".
"WHERE (state = 'OUT' OR state = 'ERR') AND pushupdates");
while ($db->next_record()) {
extract($db->Record);
if (!empty($input["skip_$servid"])) {
print "<FONT COLOR=BROWN>Skipping $server</FONT><BR>\n";
$skipped = 1;
continue;
}
print "<H4>Updating $server (as ".$SERVER_TYPES[$type].")</H4>\n";
# This server need real file update
if (!file_exists("$HOST_DIR/$server")) mkdir("$HOST_DIR/$server");
chdir("$HOST_DIR/$server") || die("$!: $HOST_DIR/$server<P>\n");
$cmd = "TOP=$TOP $BIN/mknamed.conf $server named.conf ";
print $cmd;
passthru("$cmd >> $UPDATE_LOG 2>& 1", $ret);
if ($ret != 0) {
print "<A href=\"view.php?file=$server/named.conf\"><FONT color=RED>mknamed.conf</FONT></A> failure, see $A_LOGE<BR>\n";
$error = 1;
}
else
print "<A href=\"view.php?file=$server/named.conf\"><FONT color=GREEN>named.conf</FONT></A> updated, see $A_LOG<BR>\n";
if ($type == 'N') {
foreach($zones as $zone) {
update_powerdns_zone($zone["id"],$server,$HOSTMASTER);
}
}
if ($type == 'M') {
print "<UL>\n";
foreach($deleted_domains as $trash) {
$zonefile = $trash['zonefile'];
$domain = $trash['domain'];
$cmd = "mkdir -p DELETED;rm -f DELETED/'$zonefile'; mv '$zonefile' DELETED/. && echo '$zonefile' >> deleted_files";
exec($cmd);
print "<LI><A href=\"view.php?file=$server/DELETED/$zonefile\">$domain deleted</A>\n";
}
$out= "";
$list = "";
foreach($zones as $zone) {
$domain = $zone['domain'];
if (!$zone['master'])
$domains[] = $zone['domain'];
$zonefile = $zone['zonefile'];
if (count($domains) >= 64) {
$cmd = "TOP=$TOP $BIN/mkzonefile -d $HOST_DIR/$server -u ".join(" ", $domains);
passthru("$cmd 2>&1 >>$UPDATE_LOG", $ret);
$domains = array();
if ($ret != 99) {
$error = 1;
print "</UL><H3><FONT color=RED>Error in mkzonefile, see $A_LOG</FONT></H3><UL>\n";
//print $err;
}
else
print $out;
$out = "";
$err = "";
}
$out .= "<LI><A href=\"view.php?file=$server/$zonefile\">$domain</A> updated\n";
$err .= "<LI><A href=\"view.php?file=$server/$zonefile\">$domain</A> <FONT color=RED>error</FONT>\n";
if (count($domains)) {
$cmd = "TOP=$TOP $BIN/mkzonefile -d $HOST_DIR/$server -u ".join(" ", $domains);
// print "<I>$cmd</I><BR>\n";
passthru("$cmd 2>&1 >>$UPDATE_LOG", $ret);
$domains = array();
if ($ret != 99) {
$error = 1;
print "</UL><H3><FONT color=RED>Error in mkzonefile, see $A_LOG</FONT></H3><UL>\n";
print $err;
}
else
print $out;
}
}
print "</UL>\n";
//----------------------------------------------------//
}
if ($error)
$text = "<FONT color=RED>ERROR:</FONT>";
else
$text = "<FONT color=RED>UPDATED:</FONT>";
print "$text<A href=\"$HOST_URL/$server/\">$server</A><HR>\n";
if ($error) {
$db->prepare("UPDATE servers SET state='ERR' WHERE id = ?");
$db->execute($servid);
break;
}
else {
$db->prepare("UPDATE servers SET state='CHG' WHERE id = ?");
$db->execute($servid);
}
};
if (!$error) {
patient_enter_crit('INTERNAL1', 'DOMAIN');
if (!$skipped)
updates_completed();
$db->query("DELETE FROM deleted_domains");
leave_crit('DOMAIN');
};
if ($error)
$err_text="&error=1";
else {
$err_text="";
}
return ($error? "file update failure" : "");
}
function run_scripts($input, $push, $conf)
{
global $UPDATE_LOG, $UPDATE_LOG_NAME;
global $A_LOG;
global $A_LOGE;
global $HOST_DIR;
global $TOP;
global $SBIN;
$ret = '';
$error = 0;
if (!$push && !$conf)
return;
$db = new DB_probind;
$db->query("
SELECT id as servid, hostname as server, ipno, type, zonedir, chrootbase, script, state
FROM servers
WHERE pushupdates != 0 and type<>'N'");
while ($db->next_record()) {
extract($db->Record);
$cmd = "";
if ( ($state == 'CHG' || $state == 'ERR') && $push ) {
$cmd = " -PUSH";
if ($conf) {
$cmd .= " -CONF";
$new = 'OK';
}
else
$new = 'CFG';
}
else if ( ($state == 'CFG' || $state == 'ERR') && $conf ) {
$cmd = "-CONF";
$new = 'OK';
}
if (!empty($input["skip_$servid"]) || $cmd == "" ) {
continue;
}
$cmd = "TOP=$TOP $SBIN/$script $cmd $ipno $chrootbase$zonedir";
print "<BR>$server:\n";
# This server need real file update
chdir("$HOST_DIR/$server") || die("$!: $HOST_DIR/$server<P>\n");
passthru("$cmd >> $UPDATE_LOG 2>& 1", $ret);
if ($ret != 0) {
print "<FONT color=RED><B>script failure, see $A_LOGE.</B></FONT>Command: <FONT size=-2>$cmd</FONT><BR>\n";
$error = 1;
}
else {
print "script completed, see $A_LOG. Command: <FONT size=-2>$cmd</FONT><BR>\n";
}
if ($error) {
$db->prepare("UPDATE servers SET state='ERR' WHERE id = ?");
$db->execute($servid);
break;
}
else {
$db->prepare("UPDATE servers SET state=? WHERE id = ?");
$db->execute($new,$servid);
}
};
if ($error)
$err_text="&error=1";
else
$err_text="";
// print "<H4>Log file: <A href=\"view.php?base=LOGS&file=$UPDATE_LOG_NAME$err_text\">$UPDATE_LOG</A></H4>\n";
return ($error? "script failure" : "");
}
#
# MAIN
#
get_input();
$err = '';
#
# 1. Update serial numbers if necessary. Decision about generating zone will be done
# by comparing server serial id and zone zerial id
# gen=1 - generate zones; sync = 1 - syncronyze3 zones, conf = 1 - configure zones,
# id_%d = 1 - server id for the operation (id_ALL=1 means ALL servers)
#
# If no operation is specified, serials are updated and overall view is presented
#
$gen = empty($INPUT_VARS['gen']) ? '' : $INPUT_VARS['gen'];
$push = empty($INPUT_VARS['push']) ? '' : $INPUT_VARS['push'];
$conf = empty($INPUT_VARS['conf']) ? '' : $INPUT_VARS['conf'];
$frame = empty($INPUT_VARS['frame']) ? '' : $INPUT_VARS['frame'];
#
# Set up frame structure
#
if (!$frame) {
$frame = 'MAIN';
}
if ($frame == 'MAIN' ) {
print $html_top;
print main_update_menu($INPUT_VARS);
exit;
}
if ($frame == 'PROGRESS' ) {
print $html_in_progress;
exit;
}
if ($frame == 'BLANK') {
print $html_top1;
print $html_bottom;
exit;
}
print $html_top1;
if ( !$LOG_DIR || !opendir($LOG_DIR) ) {
die("<H3><FONT color=\"red\">Can not open log directory: $LOG_DIR</FONT></H3>\n");
};
closedir();
$UPDATE_LOG_NAME= date("YmdHis").".log";
$UPDATE_LOG = "$LOG_DIR/$UPDATE_LOG_NAME";
$A_LOG="<A href=\"view.php?base=LOGS&file=$UPDATE_LOG_NAME\">LOG</A>";
$A_LOGE="<A href=\"view.php?base=LOGS&file=$UPDATE_LOG_NAME&error=1\">LOG</A>";
#print "<SCRIPT>open('update.php?frame=PROGRESS','MAIN');</SCRIPT><HR>\n";
#for($i = 0; $i < 512; $i++)
# print "<B></B>\n";
if ($user = patient_enter_crit($auth->auth["uname"], 'PUSH')) {
print sprintf($push_in_progress, ucfirst($user));
exit();
}
if ($gen) {
$err = generate_files($INPUT_VARS);
if ($err) {
print "<H3><FONT color=RED>Update interrupted due to the error</FONT></H3>\n";
print "$err <br>\n";
}
}
if ( !$err && ($push || $conf)) {
$err = run_scripts($INPUT_VARS, $push, $conf);
if ($err) {
print "<H3><FONT color=RED>Update interrupted due to the error</FONT></H3>\n";
print "$err <br>\n";
}
}
if ($err) {
print "<H3><FONT color=RED>Errors";
if (file_exists($UPDATE_LOG)) {
print ", see logs ";
print "<A href=\"view.php?base=LOGS&file=$UPDATE_LOG_NAME\">here</A></FONT></H3>\n";
}
}
else {
print "<H3><FONT color=GREEN>Completed";
if (file_exists($UPDATE_LOG)) {
print ", see logs ";
print "<A href=\"view.php?base=LOGS&file=$UPDATE_LOG_NAME\">here</A></FONT></H3>\n";
}
};
leave_crit('PUSH');
close_database();
#print "<SCRIPT>open('update.php?frame=MAIN','MAIN');</SCRIPT><HR>\n";
print $html_bottom;
?>