forked from jas-/phpDHCPAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.hosts.php
644 lines (579 loc) · 35.1 KB
/
manage.hosts.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?PHP
/*
* phpDHCPAdmin
* Jason Gerfen [[email protected]]
*
* hosts.search.php - DHCPD Search for static host
*/
// load our config data
if( file_exists( "scripts/inc.config.php" ) ) {
require 'scripts/inc.config.php';
// ensure we are being called from our configured host
if( $defined['hostname'] === $_SERVER['SERVER_NAME'] ) {
// Initialize classes
$db = new dbConn;
$err = new GenerateErrors;
$tpl = new Template;
$skin = new PageSkinner;
$val = new ValidateStrings;
$menu = new GenerateNavMenu;
$auth = new Authenticate;
$encrypt = new Encryption;
$level = new AccessLevels;
$misc = new MiscFunctions;
$debug = new DebugData;
// initialize a db connection handle
$dbconn = $db->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
// ensure our sessions are present
if( empty( $_SESSION['token'] ) ) {
$sessions = new dbSession;
}
//define the template and cache directories
$tpl->strTemplateDir = $defined['virpath'] . 'templates';
$tpl->strCacheDir = '/tmp';
if( ( !empty( $_GET ) ) || ( !empty( $_POST ) ) ) { $flag = "TRUE"; } else { $flag = "FALSE"; }
// setup our template style data
if( ( $val->ValidateString( $_GET['skin'] ) === -1 ) || ( empty( $_GET['skin'] ) ) ) {
$style = $defined['templates'] . "/black";
if( !empty( $_GET['skin'] ) ) {
$skin_err = $err->GenerateErrorLink( "help/help.php", "#val_xss", $defined['error_small'], $errors['val_xss'], NULL, NULL );
}
} else {
$style = $skin->SelectSkin( $defined['templates'], $_GET['skin'], $_COOKIE['skin'] );
}
// call our header file and pass it some variables
$tpl->assign( 'TITLE', $defined['title'], NULL, NULL );
$tpl->assign( 'DESCRIPTION', "Manage Static Hosts", NULL, NULL );
$tpl->assign( 'STYLE', $style, NULL, NULL );
// authentication template
$FILE = "auth.tpl";
// default is no error just diplay login form
if( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -1 ) {
// well looks like at least one login attempt has been processed, show empty field error
if( $_SESSION['x']++ >= 1 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#missing", $defined['error'], $errors['auth_e'], NULL, NULL );
}
// timeout with authentication token
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -2 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#timeout", $defined['error'], $errors['auth_to'], NULL, NULL );
$misc->ExitApplication( $_SESSION['token'] );
// error in validation of authentication data
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -3 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#alphanum", $defined['error'], $errors['val_alp'], NULL, NULL );
// authentication data not found in database
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -4 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#user", $defined['error'], $errors['auth_n'], NULL, NULL );
// error in database query
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -5 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['undef_sql'], NULL, NULL );
// valid user found
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === 0 ) {
// perform permissions check with access level and group data
if( ( $level->ChkLevel( $_SESSION['token'] ) === "admin" ) || ( $level->ChkLevel( $_SESSION['token'] ) === "user" ) ) {
// define some variables for the template etc.
$JS = " hidediv('perms');";
$FILE = "manage.hosts.tpl";
// initialize a db connection handle
$dbconn = $db->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
// provide count of online users
$online = "SELECT * FROM `admin_sessions`";
$ret = $db->dbQuery( $val->ValidateSQL( $online, $dbconn ), $dbconn );
$usersoline = $db->dbNumRows( $ret );
// decode our authentication token to get our group membership
$user_details = $encrypt->DecodeAuthToken( $_SESSION['token'] );
$group = base64_decode( $user_details[3] );
// Look for a GET id post to edit existing host records
if( !empty( $_GET['id'] ) ) {
if( $val->ValidateInteger( $_GET['id'] ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['val_num'], NULL, NULL );
} else {
// populate the form with database information if already configured
if( ( $group === "admin" ) || ( !empty( $_GET['allow'] ) ) && ( $val->ValidateInteger( $_GET['allow'] ) === 0 ) ) {
$query = "SELECT * FROM `conf_hosts` WHERE `id` = \"" . $_GET['id'] . "\" LIMIT 1";
} else {
$query = "SELECT * FROM `conf_hosts` WHERE `group` = \"" . $group . "\" AND `id` = \"" . $_GET['id'] . "\" LIMIT 1";
}
if( ( $value = $db->dbQuery( $val->ValidateSQL( $query, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['db_select'], NULL, NULL );
} else {
$data = $db->dbArrayResultsAssoc( $value );
/* check resource permissions */
if( $group !== "admin" ) {
$resource = "SELECT * FROM `auth_groups_perms` WHERE ( `group` = \"" . $group . "\" OR `allowed` = \"" . $group . "\" ) AND `type` = \"host\" AND `resource` = \"" . $data[0]['mac-address'] . "\"";
} else {
$resource = "SELECT * FROM `auth_groups_perms` WHERE `resource` = \"" . $data[0]['mac-address'] . "\"";
}
if( ( $value = $db->dbQuery( $val->ValidateSQL( $resource, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#config_subnets", $defined['error'], $errors['db_select'], NULL, NULL );
} else {
$ch = $db->dbArrayResultsAssoc( $value );
if( ( count( $ch ) > 0 ) || ( $data[0]['group'] === $group ) ) {
$id = $data[0]['id'];
$hostname = $data[0]['hostname'];
$mac_address = $data[0]['mac-address'];
$ip_address = $data[0]['ip-address'];
$subnet_name = $data[0]['subnet-name'];
$pxe_group = $data[0]['pxe-group'];
$ex_group = $data[0]['group'];
$select_groups = $ch;
if( count( $select_groups ) > 0 ) { $JS = " showdiv( 'perms' );"; }
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], "You are currently editing record #" . $id, NULL, NULL );
} else {
$error = $err->GenerateErrorLink( "help/help.html", "#config_subnets", $defined['error'], $errors['auth_res'], NULL, NULL );
}
}
}
}
}
// begin our validation on submitted data
if( !empty( $_POST ) ) {
// re-assign vars for processing and template assignment
$id = $_POST['id'];
$hostname = $_POST['hostname'];
$mac_address = $_POST['mac_address'];
$ip_address = $_POST['ip_address'];
$subnet_name = $_POST['subnet_name'];
$pxe_group = $_POST['pxe_group'];
$search = $_POST['search'];
$modify = $_POST['modify'];
$permissions = $_POST['select_groups'];
$groups = $_POST['groups'];
// perform search if not empty
if( ( empty( $search ) ) && ( !empty( $_POST['srch'] ) ) ) {
$search_err = $err->GenerateErrorImg( $defined['error'], "help/help.html#host_search", '800', '800' );
$list = "<ol><li>Search string is empty. Allowed formats:<br>MAC Address: xx:xx:xx:xx:xx<br>IPv4 Address: xxx.xxx.xxx.xxx<br>Hostname: [0-9a-z]</li></ol>";
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['val_host'] . $list, NULL, NULL );
} elseif( ( !empty( $search ) ) && ( !empty( $_POST['srch'] ) ) ) {
// perform validation on search string
if( ( $val->ValidateIPv4( $search ) !== -1 ) || ( $val->ValidateMACFormats( $search ) !== -1 ) || ( $val->ValidateParagraph( $search ) !== -1 ) ) {
// Gather all records belonging to other groups but where this user is allowed access
$sql = "SELECT * FROM `auth_group_perms` WHERE `resource` = \"hosts\"";
if( ( $z = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
}
// define our search query
if( $group === "admin" ) {
$query = "SELECT * FROM `conf_hosts` WHERE `hostname` LIKE \"" . $search . "\" OR `mac-address` LIKE \"" . $search . "\" OR `ip-address` LIKE \"" . $search . "\" ORDER BY `hostname` ASC";
} else {
$query = "SELECT * FROM `conf_hosts` WHERE `hostname` LIKE \"" . $search . "\" OR `mac-address` LIKE \"" . $search . "\" OR `ip-address` LIKE \"" . $search . "\" HAVING `group` = \"" . $group . "\" ORDER BY `hostname` ASC";
}
// process our query
if( ( $value = $db->dbQuery( $val->ValidateSQL( $query, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['db_search_err'], NULL, NULL );
} else {
// process results of search
if( $db->dbNumRows( $value ) >= 1 ) {
$data = $db->dbArrayResultsAssoc( $value );
if( $db->dbNumRows( $value ) === 1 ) {
$id = $data[0]['id'];
$hostname = $data[0]['hostname'];
$mac_address = $data[0]['mac-address'];
$ip_address = $data[0]['ip-address'];
$subnet_name = $data[0]['subnet-name'];
$pxe_group = $data[0]['pxe-group'];
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], "Your search for '" . $val->ValidateXSS( $search ) . "' returned '" . $db->dbNumRows( $value ) . "' results. The form has been populated for you to edit record #" . $id, NULL, NULL );
} else {
$hosts_list = $misc->GenJumpMenuBoxHOSTS( $data, 'hosts_list', $_GET['skin'], NULL );
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], "Your search for '" . $val->ValidateXSS( $search ) . "' returned '" . $db->dbNumRows( $value ) . "' results. Please select the host you wish to edit from the select box below", NULL, NULL );
}
} else {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['sql_empty'], NULL, NULL );
}
}
} else {
$search_err = $err->GenerateErrorImg( $defined['error'], "help/help.html#host_search", '800', '800' );
$list = "<ol><li>Search string is invalid. Allowed formats:<br>MAC Address: xx:xx:xx:xx:xx<br>IPv4 Address: xxx.xxx.xxx.xxx<br>Hostname: [0-9a-z]</li></ol>";
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['val_host'] . $list, NULL, NULL );
}
} else {
$sql_success = -1;
// check each post element
if( ( !empty( $hostname ) ) && ( !empty( $mac_address ) ) && ( !empty( $ip_address ) ) && ( !empty( $subnet_name ) ) ) {
// perform lookup of available scopes to prevent overlaps between static hosts and scope address
$sql = "SELECT `scope-range-1`,`scope-range-2` FROM `conf_subnets` WHERE `scope-range-1` > '' AND `scope-range-2` > ''";
if( ( $value = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
if( $db->dbNumRows( $value ) !== 0 ) {
$scopes = $db->dbArrayResultsAssoc( $value );
}
}
// begin validation of configuration options
if( ( $val->ValidateHostname( $hostname ) !== -1 ) && ( $val->ValidateMACFormats( $mac_address ) !== -1 ) && ( $val->ValidateIPv4( $ip_address ) !== -1 ) && ( ( $val->ValidateParagraph( $subnet_name ) !== -1 ) || ( $val->ValidateParagraph( $pxe_group ) !== -1 ) ) && ( $val->ValidateIPvsScope( $scopes, $ip_address ) !== -1 ) ) {
// since no errors for the mac address were recieved assign the *possibly fixed value
$mac_address = $val->ValidateMACFormats( $mac_address );
// define our sql statements (if group is admin, discard that field)
if( $group === "admin" ) {
$insert = "INSERT INTO `conf_hosts` ( `hostname`, `mac-address`, `ip-address`, `subnet-name`, `pxe-group` ) VALUES ( \"" . $hostname . "\",\"" . $mac_address . "\", \"" . $ip_address . "\", \"" . $subnet_name . "\", \"" . $pxe_group . "\" )";
$update = "UPDATE `conf_hosts` SET `hostname` = \"" . $hostname . "\", `mac-address` = \"" . $mac_address . "\", `ip-address` = \"" . $ip_address . "\", `subnet-name` = \"" . $subnet_name . "\", `pxe-group` = \"" . $pxe_group . "\" WHERE `id` = \"" . $id . "\" LIMIT 1";
} else {
$insert = "INSERT INTO `conf_hosts` ( `hostname`, `mac-address`, `ip-address`, `subnet-name`, `pxe-group`, `group` ) VALUES ( \"" . $hostname . "\",\"" . $mac_address . "\", \"" . $ip_address . "\", \"" . $subnet_name . "\", \"" . $pxe_group . "\", \"" . $group . "\" )";
if( !empty( $_POST['ex_group'] ) ) {
$update = "UPDATE `conf_hosts` SET `hostname` = \"" . $hostname . "\", `mac-address` = \"" . $mac_address . "\", `ip-address` = \"" . $ip_address . "\", `subnet-name` = \"" . $subnet_name . "\", `pxe-group` = \"" . $pxe_group . "\", `group` = \"" . $group . "\" WHERE `id` = \"" . $id . "\" LIMIT 1";
} else {
$update = "UPDATE `conf_hosts` SET `hostname` = \"" . $hostname . "\", `mac-address` = \"" . $mac_address . "\", `ip-address` = \"" . $ip_address . "\", `subnet-name` = \"" . $subnet_name . "\", `pxe-group` = \"" . $pxe_group . "\", WHERE `id` = \"" . $id . "\" LIMIT 1";
}
}
$delete = "DELETE FROM `conf_hosts` WHERE `id` = \"" . $id . "\" LIMIT 1";
// determine which button was clicked
if( !empty( $_POST['AddHosts'] ) ) { $query = $insert; $db_msg_good = $errors['db_insert']; $db_msg_err = $errors['db_insert_err']; }
if( !empty( $_POST['EditHosts'] ) ) { $query = $update; $db_msg_good = $errors['db_edit']; $db_msg_err = $errors['db_edit_err']; }
if( !empty( $_POST['DelHosts'] ) ) { $query = $delete; $db_msg_good = $errors['db_del']; $db_msg_err = $errors['db_del_err']; }
// process our query
if( ( $value = $db->dbQuery( $val->ValidateSQL( $query, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $db_msg_err, NULL, NULL );
// does the record exist? a duplicate mac, ip or hostname is not allowed
if( ( eregi( "duplicate", $db->dbCatchError() ) ) && ( !empty( $id ) ) || ( !empty( $modify ) ) && ( $modify === "modify" ) ) {
// look at error and grab the existing record
@preg_match( '/.*\'(.*)\'.*/', $db->dbCatchError(), $dup );
// make sure users can't edit other group records
if( $group !== "admin" ) {
$sql = "SELECT `id` FROM `conf_hosts` WHERE `ip-address` LIKE \"" . $dup[1] . "\" OR `hostname` LIKE \"" . $dup[1] . "\" OR `mac-address` LIKE \"" . $dup[1] . "\" HAVING `group` = \"" . $group . "\"";
} else {
$sql = "SELECT `id` FROM `conf_hosts` WHERE `ip-address` LIKE \"" . $dup[1] . "\" OR `hostname` LIKE \"" . $dup[1] . "\" OR `mac-address` LIKE \"" . $dup[1] . "\"";
}
// execute the database query for our matching record
if( ( $value = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$ids = $db->dbArrayResults( $value );
}
// create new `update` sql statement with matching id to overwrite record as per requested
if( $group === "admin" ) {
$update = "UPDATE `conf_hosts` SET `hostname` = \"" . $hostname . "\", `mac-address` = \"" . $mac_address . "\", `ip-address` = \"" . $ip_address . "\", `subnet-name` = \"" . $subnet_name . "\", `pxe-group` = \"" . $pxe_group . "\", `group` = \"" . $group . "\" WHERE `id` = \"" . $ids[0]['id'] . "\" LIMIT 1";
} else {
$update = "UPDATE `conf_hosts` SET `hostname` = \"" . $hostname . "\", `mac-address` = \"" . $mac_address . "\", `ip-address` = \"" . $ip_address . "\", `subnet-name` = \"" . $subnet_name . "\", `pxe-group` = \"" . $pxe_group . "\" WHERE `id` = \"" . $ids[0]['id'] . "\" LIMIT 1";
}
// perform update on record
if( ( $value = $db->dbQuery( $val->ValidateSQL( $update, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['db_edit_err'], NULL, NULL );
} else {
// was anything updated?
if( $db->dbAffectedRows( $dbconn ) === 0 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['db_edit_err'], NULL, NULL );
} else {
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], $errors['db_edit'], NULL, NULL );
}
}
} else {
// we will perform a search for the existing record and allow the user to proceed on `updating` duplicate
@preg_match( '/.*\"(.*)\".*/', $db->dbCatchError(), $dup );
if( ( $group !== "admin" ) || ( empty( $_POST['allow'] ) ) ) {
$sql = "SELECT * FROM `conf_hosts` WHERE `id` = \"" . $dup[1] . "\" AND `group` = \"" . $group . "\"";
} else {
$sql = "SELECT * FROM `conf_hosts` WHERE `id` = \"" . $dup[1] . "\"";
}
// execute the database query for our matching record
if( ( $value = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$ids = $db->dbArrayResults( $value );
}
// determine which fields match and assign to an array of errors
$e = $err->GenerateErrorImg( $defined['error'], "help/help.html#host_duplicate", '800', '800' );
if( count( $ids ) > 0 ) {
foreach( $ids as $key => $value ) {
if( $value['hostname'] === $hostname ) { $duperrs['hostname'] = $e; $hostname_err = $e; }
if( $value['ip-address'] === $ip_address ) { $duperrs['ip-address'] = $e; $ip_address_err = $e; }
if( $value['mac-address'] === $mac_address ) { $duperrs['mac-address'] = $e; $mac_address_err = $e; }
}
}
// assign to a table generator & error about duplicate record
$duplicate = $misc->GenTableFromAssocArrayDuplicateHost( $ids[0], $duperrs );
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $db_msg_err . " A record exists in the database that matches at least one field being processed", NULL, NULL );
}
} else {
// database update/insert/delete sucessful
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], $db_msg_good, NULL, NULL );
$sql_success = 0;
}
} else {
// find validation errors
$e = $err->GenerateErrorImg( $defined['error'], "help/help.html#host_search", '800', '800' );
$list .= "<ol>";
if( $val->ValidateHostname( $hostname ) === -1 ) { $list .= "<li>" . $errors['val_hostname'] . "</li>"; $hostname_err = $e; }
if( $val->ValidateMACFormats( $mac_address ) === -1 ) { $list .= "<li>" . $errors['val_mac'] . "</li>"; $mac_address_err = $e; }
if( $val->ValidateIPv4( $ip_address ) === -1 ) { $list .= "<li>" . $errors['val_ipaddr'] . "</li>"; $ip_address_err = $e; }
if( $val->ValidateParagraph( $subnet_name ) === -1 ) { $list .= "<li>Subnet Name field is invalid</li>"; $subnet_name_err = $e; }
if( $val->ValidateIPvsScope( $scopes, $ip_address ) === -1 ) { $list .= "<li>IP Address is in use within a currently defined scope for dynamic addressing</li>"; $ip_address_err = $e; }
$list .= "</ol>";
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], "An error occured with form data. Please correct syntax or modify entry" . $list, NULL, NULL );
}
} else {
// look to see which fields were empty
$e = $err->GenerateErrorImg( $defined['error'], "help/help.html#host_search", '800', '800' );
$list .= "<ol>";
if( empty( $hostname ) ) { $list .= "<li>Hostname field is missing</li>"; $hostname_err = $e; }
if( empty( $mac_address ) ) { $list .= "<li>MAC Address Field is missing</li>"; $mac_address_err = $e; }
if( empty( $ip_address ) ) { $list .= "<li>IP Address Field is missing</li>"; $ip_address_err = $e; }
if( empty( $subnet_name ) ) { $list .= "<li>Subnet Name Field is missing</li>"; $subnet_name_err = $e; }
$list .= "</ol>";
$error = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['error'], $errors['val_missing'] . $list, NULL, NULL );
}
}
}
/* grab the additional host records if permissions allow first */
if( $group !== "admin" ) {
$sql = "SELECT `resource` FROM `auth_groups_perms` WHERE ( `group` = \"" . $group . "\" OR `allowed` = \"" . $group . "\" ) AND `type` = \"host\"";
} else {
$sql = "SELECT `resource` FROM `auth_groups_perms` WHERE `type` = \"host\"";
}
if( ( $r = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
if( $db->dbNumRows( $r ) !== 0 ) {
$add_rec = $db->dbArrayResultsAssoc( $r );
foreach( $add_rec as $key => $value ) {
$sqls[] = "SELECT * FROM `conf_hosts` WHERE `mac-address` = \"" . $value['resource'] . "\" LIMIT 1";
foreach( $sqls as $k => $v ) {
if( ( $r = $db->dbQuery( $val->ValidateSQL( $v, $dbconn ), $dbconn ) ) !== -1 ) {
$hostslist_additional[] = $db->dbArrayResultsAssoc( $r );
$allow = 1;
}
}
}
}
}
// clean it up first
if( count( $hostslist_additional ) > 0 ) {
$hostslist_fixed = array();
foreach( $hostslist_additional as $key => $value ) {
$hostslist_fixed = array_merge( $hostslist_fixed, $value );
}
}
// create current list of static hosts if $hosts_list is empty
if( count( $hosts_list ) === 0 ) {
if( $group === "admin" ) {
$hostsquery = "SELECT * FROM `conf_hosts` ORDER BY `hostname` ASC";
} else {
$hostsquery = "SELECT * FROM `conf_hosts` WHERE `group` = \"" . $group . "\" OR `group` = '' ORDER BY `hostname` ASC";
}
if( ( $current = $db->dbQuery( $val->ValidateSQL( $hostsquery, $dbconn ), $dbconn ) ) !== -1 ) {
$hostslist = $db->dbArrayResultsAssoc( $current );
if( count( $hostslist_additional ) > 0 ) {
$hostslist = array_merge( $hostslist, $hostslist_fixed );
}
}
$hosts_list = $misc->GenJumpMenuBoxHOSTS( $hostslist, 'hosts_list', $_GET['skin'], $allow );
}
/* check for additional subnets this group can access */
if( $group === "admin" ) {
$sql = "SELECT `resource`, `allowed` FROM `auth_groups_perms` WHERE `type` = \"subnet\"";
} else {
$sql = "SELECT `resource`, `allowed` FROM `auth_groups_perms` WHERE `type` = \"subnet\" AND `allowed` = \"" . $group . "\" OR `group` = \"" . $group . "\"";
}
if( ( $w = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$extra = $db->dbArrayResultsAssoc( $w );
if( count( $extra ) > 0 ) {
$subnetlist_remainder = array();
foreach( $extra as $key => $value ) {
$sql = "SELECT * FROM `conf_subnets` WHERE `subnet-name` = \"" . $value['resource'] . "\" LIMIT 1";
if( ( $s = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$subnetlist_remainder = array_merge( $subnetlist_remainder, $db->dbArrayResultsAssoc( $s ) );
$subnetlist_remainder = $subnetlist_remainder;
if( $group === "admin" ) {
$sql = "SELECT * FROM `conf_subnets` ORDER BY `subnet-name` ASC";
} else {
$sql = "SELECT * FROM `conf_subnets` WHERE `group` = \"" . $group . "\" ORDER BY `subnet-name` ASC";
}
if( ( $s = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$subnetlist_remainder = array_merge( $subnetlist_remainder, $db->dbArrayResultsAssoc( $s ) );
$subnetlist_remainder = $misc->EliminiateDuplicates( $subnetlist_remainder );
}
}
}
} else {
if( $group !== "admin" ) {
$sql = "SELECT * FROM `conf_subnets` WHERE `group` = \"" . $group . "\"";
} else {
$sql = "SELECT * FROM `conf_subnets` ORDER BY `subnet-name` ASC";
}
if( ( $s = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$subnetlist_remainder = /*array_merge( $subnetlist_remainder,*/ $db->dbArrayResultsAssoc( $s );// );
}
}
}
if( count( $subnet_name ) > 0 ) {
$subnet_name = $misc->GenDropMenuWSelectedSubnets( $subnetlist_remainder, $subnet_name, 'subnet_name' );
} else {
$subnet_name = "No Subnets defined";
}
// populate our pxe group list
if( $group === "admin" ) {
$px = "SELECT `pxe-group-name` FROM `conf_pxe_groups` ORDER BY `pxe-group-name` ASC";
} else {
$px = "SELECT `pxe-group-name` FROM `conf_pxe_groups` WHERE `group` = \"" . $group . "\" OR `group` = '' ORDER BY `pxe-group-name` ASC";
}
if( ( $return = $db->dbQuery( $val->ValidateSQL( $px, $dbconn ), $dbconn ) ) !== -1 ) {
$pxeg = $db->dbArrayResultsAssoc( $return );
}
if( count( $pxeg ) === 0 ) {
$pxe_group = "No PXE Groups defined";
} else {
$pxe_group = $misc->GenDropMenuWSelectedPXE( $pxeg, $pxe_group, 'pxe_group' );
}
// do some calculations on the list of available subnets
$sql = "SELECT * FROM `conf_subnets`";
if( ( $return = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$data = $db->dbArrayResultsAssoc( $return );
}
if( count( $data ) !== 0 ) {
foreach( $data as $key => $value ) {
if( ( $value['enable-scope'] === "true" ) && ( !empty( $value['scope-range-1'] ) ) && ( !empty( $value['scope-range-2'] ) ) ) {
$ip_counts[] = $misc->GetAvailableIPAddresses( $value, $value['subnet-name'] );
} else {
if( !empty( $value['pool'] ) ) {
// process assigned pool IP addresses minus IP's engaged in `conf_leases` table
$sql = "SELECT * FROM `conf_pools` WHERE `pool-name` = \"" . $value['pool'] . "\"";
if( ( $return = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$pool_array = $db->dbArrayResultsAssoc( $return );
foreach( $pool_array as $k => $v ) {
$ip_counts[] = $misc->GetAvailableIPAddresses( $v, $value['subnet-name'] );
}
}
} else {
// look at broadcast and mask to determine range first
// process everything else by looking up all static hosts and comparing to broadcast and subnet mask
$sql = "SELECT * FROM `conf_hosts` WHERE `subnet-name` = \"" . $value['subnet-name'] . "\"";
if( ( $return = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$hosts_array = $db->dbArrayResultsAssoc( $return );
$ip_counts[] = $misc->GetAvailableIPAddressesStatic( $hosts_array, $value['subnet-name'] );
}
}
}
}
if( count( $ip_counts ) !== 0 ) {
$x = 1;
foreach( $ip_counts as $key => $value ) {
foreach( $value as $k => $v ) {
if( $x <= 6 ) {
// pass each array to a format specialist
$available .= $misc->GenDivHiddenContent( $k, $v );
} else {
$available .= "</ul><br><br><ul>" . $misc->GenDivHiddenContent( $k, $v );
$x = 1;
}
$x++;
}
}
}
}
// check for additional subnets this group can access
$sql = "SELECT `resource`, `allowed` FROM `auth_groups_perms` WHERE `type` = \"host\" AND `allowed` = \"" . $group . "\" AND `group` != \"" . $group . "\"";
if( ( $extra = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
$extra = $db->dbArrayResultsAssoc( $extra );
foreach( $extra as $key => $value ) {
$sql = "SELECT * FROM `conf_subnets` WHERE `subnet-name` = \"" . $value['resource'] . "\" LIMIT 1";
if( ( $s = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) !== -1 ) {
if( ( $db->dbNumRows( $s ) !== -1 ) && ( $db->dbNumRows( $s ) > 0 ) ) {
$subnetlist_remainder = array_merge( $misc->EliminiateDuplicates( $db->dbArrayResultsAssoc( $s ) ) );
}
}
}
}
// do the checkboxes for available groups
$groupsquery = "SELECT * FROM `auth_groups` WHERE `group` != \"admin\" AND `group` != \"" . $group . "\" ORDER BY `group` ASC";
if( ( $res = $db->dbQuery( $val->ValidateSQL( $groupsquery, $dbconn ), $dbconn ) ) !== -1 ) {
$groups = $db->dbArrayResultsAssoc( $res );
$groups = $misc->EliminiateDuplicates( $groups );
$sql = "SELECT * FROM `auth_groups_perms` WHERE `resource` = \"" . $mac_address . "\"";
$sql_res = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn );
$select_groups = $db->dbArrayResultsAssoc( $sql_res );
$chk_grp = $select_groups;
if( count( $groups ) !== 0 ) {
$select_groups = $misc->GenGroupsCheckBoxes( $groups, 'select_groups', $_GET['skin'], $select_groups, $group );
} else {
$select_groups = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], "No groups defined", NULL, NULL );
}
} else {
$select_groups = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['db_select_err'], NULL, NULL );
}
if( $sql_success === 0 ) {
// do the permissions bit
foreach( $permissions as $key => $value ) {
if( !empty( $value['group'] ) ) {
if( !in_array( $value['resource'], $chk_grp ) ) {
$set_permissions[] = "INSERT INTO `auth_groups_perms` ( `group`, `resource`, `type`, `allowed` ) VALUES ( \"" . $group . "\", \"" . $mac_address . "\", \"host\", \"" . $value['group'] . "\" )";
} else {
$set_permissions[] = "UPDATE `auth_groups_perms` SET `allowed` = \"" . $value['group'] . "\" WHERE `resource` = \"" . $mac_address . "\"";
$delete_permissions[] = "DELETE FROM `auth_groups_perms` WHERE `id` = \"" . $value['id'] . "\" LIMIT 1";
}
}
}
// which sql query do we run?
if( !empty( $_POST['AddHosts'] ) ) { $grp_prms = $set_permissions; }
if( !empty( $_POST['EditHosts'] ) ) { $grp_prms = $set_permissions; }
if( !empty( $_POST['DelHosts'] ) ) { $grp_prms = $set_permissions; }
// work on the permissions table
if( count( $grp_prms ) > 0 ) {
foreach( $grp_prms as $key => $value ) {
if( ( $value = $db->dbQuery( $val->ValidateSQL( $value, $dbconn ), $dbconn ) ) === -1 ) {
$error = $err->GenerateErrorLink( "help/help.html", "#config_subnet", $defined['error'], $db_msg_err, NULL, NULL );
} else {
$error = $err->GenerateErrorLink( "help/help.html", "#config_subnet", $defined['good'], $db_msg_good, NULL, NULL );
}
}
}
}
// Assign form variables
$tpl->assign( 'error', $error, NULL, NULL );
$tpl->assign( 'id', $val->ValidateXSS( $id ), NULL, NULL );
$tpl->assign( 'hostname', $val->ValidateXSS( $hostname ), NULL, NULL );
$tpl->assign( 'mac_address', $val->ValidateXSS( $mac_address ), NULL, NULL );
$tpl->assign( 'ip_address', $val->ValidateXSS( $ip_address ), NULL, NULL );
$tpl->assign( 'subnet_name', $subnet_name, NULL, NULL );
$tpl->assign( 'pxe_group', $pxe_group, NULL, NULL );
$tpl->assign( 'hosts_list', $hosts_list, NULL, NULL );
$tpl->assign( 'duplicate', $duplicate, NULL, NULL );
$tpl->assign( 'available', $available, NULL, NULL );
$tpl->assign( 'select_groups', $select_groups, NULL, NULL );
$tpl->assign( 'ex_group', $val->ValidateXSS( $ex_group ), NULL, NULL );
$tpl->assign( 'allow', $val->ValidateXSS( $allow ), NULL, NULL );
$tpl->assign( 'host_count', count( $hostslist ), NULL, NULL );
// assign error messages
$tpl->assign( 'hostname_err', $hostname_err, NULL, NULL );
$tpl->assign( 'mac_address_err', $mac_address_err, NULL, NULL );
$tpl->assign( 'ip_address_err', $ip_address_err, NULL, NULL );
$tpl->assign( 'subnet_name_err', $subnet_name_err, NULL, NULL );
$tpl->assign( 'pxe_group_err', $pxe_group_err, NULL, NULL );
$tpl->assign( 'hosts_list_err', $hosts_list_err, NULL, NULL );
$tpl->assign( 'search_err', $search_err, NULL, NULL );
$tpl->assign( 'select_groups_err', $select_groups_err, NULL, NULL );
// Do some cleaning before leaving
$misc->CleanUpVars( $_POST, NULL );
// Perform analyze, repair and optimize on used tables
$db->dbFixTable( "conf_hosts", $dbconn );
$db->dbFixTable( "auth_groups_perms", $dbconn );
// Free db handle and close connection(s)
$db->dbFreeData( $dbconn );
$db->dbCloseConn( $dbconn );
} else {
// page view restricted by access level
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['level'], NULL, NULL );
}
} else {
// general authentication error
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['auth_n'], NULL, NULL );
}
} else {
// Possible XSS attack
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['xss_config'], NULL, NULL );
}
} else {
// File is missing for configuration params
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['config_file'], NULL, NULL );
}
// include our header file
$tpl->assign( 'JS', $JS, NULL, NULL );
$tpl->display( 'header.tpl', $flag, NULL );
// determine our menu system
$MENU = $menu->CreateNav( $_SESSION['token'], $_GET['skin'] );
$tpl->assign( 'MENU', $tpl->assign( 'SKIN', preg_replace( '/templates\//', '', $style ), $MENU, $flag ), NULL, NULL );
// assign some vars to our main template
$tpl->assign( 'URL', $_SERVER['PHP_SELF'] . "?skin=" . $_GET['skin'], NULL, NULL );
$tpl->assign( 'IP_ADDRESS', $_SERVER['REMOTE_ADDR'], NULL, NULL );
$tpl->assign( 'ERROR', $ERROR, NULL, NULL );
$tpl->assign( 'STYLE', $style, NULL, NULL );
$tpl->assign( 'usersonline', "<b>Users:</b> " . $usersoline . " online", NULL, NULL );
// call our main body template data
$tpl->assign( 'DATA', $tpl->assign( NULL, NULL, $FILE, $flag ), NULL, NULL );
$tpl->display( 'main.tpl', $flag, NULL );
// call our footer file
$tpl->assign( 'DISCLAIMER', $defined['disclaimer'], NULL, NULL );
$tpl->assign( 'SKIN_MENU', $skin->GenSkinMenu( $_GET['skin'], $defined['templates'] ), NULL, NULL );
$tpl->assign( 'SKIN_MENU_ERR', $skin_err, NULL, NULL );
$tpl->display( 'footer.tpl', $flag, NULL );
// show some debugging if enabled
if( $defined['debug'] === "TRUE" ) { $debug->ShowDebug( $_GET, $_POST, $_REQUEST, $_SESSION ); }
?>