forked from NicTool/NicTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zone.cgi
executable file
·1389 lines (1223 loc) · 46.4 KB
/
zone.cgi
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# NicTool v2.00-rc1 Copyright 2001 Damon Edwards, Abe Shelton & Greg Schueler
# NicTool v2.01+ Copyright 2004-2008 The Network People, Inc.
#
# NicTool is free software; you can redistribute it and/or modify it under
# the terms of the Affero General Public License as published by Affero,
# Inc.; either version 1 of the License, or any later version.
#
# NicTool is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Affero GPL for details.
#
# You should have received a copy of the Affero General Public License
# along with this program; if not, write to Affero Inc., 521 Third St,
# Suite 225, San Francisco, CA 94107, USA
#
use strict;
require 'nictoolclient.conf';
main();
sub main {
my $q = new CGI();
my $nt_obj = new NicToolClient($q);
return if $nt_obj->check_setup ne 'OK';
my $user = $nt_obj->verify_session();
if ($user && ref $user) {
print $q->header;
display( $nt_obj, $q, $user );
}
}
sub display {
my ( $nt_obj, $q, $user ) = @_;
$nt_obj->parse_template($NicToolClient::start_html_template);
$nt_obj->parse_template(
$NicToolClient::body_frame_start_template,
username => $user->{'username'},
groupname => $user->{'groupname'},
userid => $user->{'nt_user_id'},
);
my $zone = $nt_obj->get_zone( nt_zone_id => $q->param('nt_zone_id') );
my $level = $nt_obj->display_group_tree(
$user,
$user->{'nt_group_id'},
$zone->{'delegated_by_id'} ? '' : $q->param('nt_group_id'), 0
);
if ( $zone->{'error_code'} ne 200 ) {
$nt_obj->display_nice_error( $zone, 'Get Zone' );
return;
}
$nt_obj->display_zone_list_options( $user, $q->param('nt_group_id'), $level, 0 );
$nt_obj->display_zone_options( $user, $zone, $level + 1, 0 ); #1);
$zone = display_zone( $nt_obj, $user, $q, $zone );
display_nameservers( $nt_obj, $user, $q, $zone );
display_zone_records( $nt_obj, $user, $q, $zone );
$nt_obj->parse_template($NicToolClient::end_html_template);
}
sub display_zone {
my ( $nt_obj, $user, $q, $zone ) = @_;
my $r = do_edit_zone( $nt_obj, $user, $q, $zone );
$zone = $r if ref $r; # refresh zone
$r = do_new_zone( $nt_obj, $user, $q, $zone );
$zone = $r if ref $r; # refresh zone
do_delete_delegation( $nt_obj, $q );
display_zone_deleted() if $zone->{'deleted'};
my $isdelegate = exists $zone->{'delegated_by_id'};
if ($isdelegate) {
display_zone_delegate( $nt_obj, $q, $user, $zone );
}
else {
display_zone_delegation( $nt_obj, $q, $user, $zone );
};
display_zone_properties( $nt_obj, $q, $zone, $user );
return $zone;
}
sub do_delete_delegation {
my ($nt_obj, $q ) = @_;
return if ! $q->param('deletedelegate');
return if ! $q->param('delegate_group_id');
if ( $q->param('type') ne 'record' && $q->param('nt_zone_id') ) {
my $error = $nt_obj->delete_zone_delegation(
nt_zone_id => $q->param('nt_zone_id'),
nt_group_id => $q->param('delegate_group_id')
);
if ( $error->{'error_code'} != 200 ) {
$nt_obj->display_nice_error( $error, "Delete Zone Delegation" );
}
else {
$nt_obj->display_nice_message(
"The zone delegation was successfully removed.",
"Delegation Removed" );
}
}
elsif ( $q->param('type') eq 'record' && $q->param('nt_zone_record_id') ) {
my $error = $nt_obj->delete_zone_record_delegation(
nt_zone_record_id => $q->param('nt_zone_record_id'),
nt_group_id => $q->param('delegate_group_id')
);
if ( $error->{'error_code'} != 200 ) {
$nt_obj->display_nice_error( $error,
"Delete Zone Record Delegation" );
}
else {
$nt_obj->display_nice_message(
"The resource record delegation was successfully removed.",
"Delegation Removed" );
}
}
};
sub do_edit_zone {
my ( $nt_obj, $user, $q, $zone ) = @_;
return if !$q->param('edit_zone');
return if $q->param('Cancel');
if ( ! $q->param('Save') ) {
display_edit_zone( $nt_obj, $user, $q, '', $zone, 'edit' );
return;
};
my @fields = qw/ nt_zone_id nt_group_id zone description
mailaddr serial refresh retry expire ttl minimum /;
my %data;
foreach ( @fields ) {
next if ! defined $q->param($_);
$data{$_} = $q->param($_);
};
$data{'nameservers'} = join( ',', $q->param('nameservers') );
if ( $q->param('undelete') ) {
$data{'deleted'} = 0;
};
my $error = $nt_obj->edit_zone(%data);
if ( $error->{'error_code'} != 200 ) {
display_edit_zone( $nt_obj, $user, $q, $error, $zone, 'edit' );
return;
};
return $nt_obj->get_zone(
nt_group_id => $q->param('nt_group_id'),
nt_zone_id => $q->param('nt_zone_id')
);
};
sub do_new_zone {
my ( $nt_obj, $user, $q, $zone ) = @_;
return if !$q->param('new_zone');
return if $q->param('Cancel');
if ( ! $q->param('Create') ) {
display_edit_zone( $nt_obj, $user, $q, '', $zone, 'new' );
return;
};
my @fields = qw/ nt_group_id zone nameservers description mailaddr
serial refresh retry expire ttl minimum/;
my %data = map { $_ => $q->param($_) } @fields;
$data{'nameservers'} = join( ',', $q->param('nameservers') );
my $error = $nt_obj->new_zone(%data);
if ( $error->{'error_code'} != 200 ) {
display_edit_zone( $nt_obj, $user, $q, $error, $zone, 'new' );
return;
};
my $zid = $error->{'nt_zone_id'};
if ($NicToolClient::edit_after_new_zone) {
$q->param( -name => 'object', -value => 'zone' );
$q->param( -name => 'obj_id', -value => $zid );
return $nt_obj->redirect_from_log($q);
}
$zone = $nt_obj->get_zone(
nt_group_id => $q->param('nt_group_id'),
nt_zone_id => $zid,
);
if ( $zone->{'error_code'} != 200 ) {
$nt_obj->display_nice_error( $zone, 'Get Zone' );
}
return $zone;
}
sub display_zone_deleted {
print qq[
<table id="zoneDeletedError" class="fat">
<tr class=dark_grey_bg>
<td class="fat light_grey_bg nowrap">
This zone may not be modified because it is deleted.
</td>
</tr>
</table>
];
}
sub display_zone_delegate {
my ( $nt_obj, $q, $user, $zone ) = @_;
print qq[
<table id="delegationInfo" class="fat">
<tr class=dark_grey_bg><td class="no_pad fat bold">Delegation</td></tr>
</table>
<table id="unSure" class="fat" style="spacing:0;">
<tr>
<td class="top">
<table id="delegatedBy" class="fat">];
if ( !$zone->{'pseudo'} ) {
print qq[
<tr class="light_grey_bg">
<td class="nowrap"> Delegated by: </td>
<td class="fat middle">
<img src="$NicToolClient::image_dir/user.gif" alt="user"> $zone->{'delegated_by_name'}
</td>
</tr>];
}
else {
print qq[
<tr class=light_hilite_bg>
<td class="nowrap" colspan=2> This zone is visible because some of its records have been delegated to you.</td>
</tr>];
}
print qq[
<tr class=light_grey_bg>
<td class="nowrap"> Belonging to group: </td>
<td class="fat">
<table>
<tr>
<td class=middle><img src="$NicToolClient::image_dir/group.gif" alt="group"></td>
<td class=middle> $zone->{'group_name'}</td>
</tr>
</table>
</td>
</tr>];
if ( !$zone->{'pseudo'} ) {
print qq[
<tr class=light_grey_bg>
<td class="nowrap"> With Permissions: </td>
<td class="fat">
<table>
<tr class=light_grey_bg>];
my %perms = (
'write' => "Write",
'delete' => "Remove Delegation",
'delegate' => "Re-delegate",
'add_records' => "Add Records",
'delete_records' => "Delete Records"
);
foreach ( qw/ write delete delegate add_records delete_records / ) {
my $check = $zone->{"delegate_$_"} ? 'checked' : 'unchecked';
print qq[
<td><img src="$NicToolClient::image_dir/perm-$check.gif" alt="permssion $check"> $perms{$_}</td>];
}
print qq[
</tr>
</table>
</td>
</tr>];
}
print qq[
</table>
</td>
</tr>
</table>];
};
sub display_zone_delegation {
my ( $nt_obj, $q, $user, $zone ) = @_;
my $delegates = $nt_obj->get_zone_delegates( nt_zone_id => $zone->{nt_zone_id} );
if ( $delegates->{error_code} ne 200 ) {
warn "error get_zone_delegates: "
. $delegates->{'error_code'} . " "
. $delegates->{'error_msg'};
return;
}
return if @{ $delegates->{'delegates'} } == 0;
my $gid = $q->param('nt_group_id');
my $per_link = $nt_obj->help_link('delperms');
print qq[
<div id="zoneDelegateHeadline" class="dark_grey_bg bold side_pad">Zone Delegates</div>
<table id="zoneDelegationTable" class="fat">
<tr id="delegateHeader" class="light_grey_bg fat">
<td class="nowrap side_pad">Delegated To Group</td>
<td class="nowrap side_pad">Delegated By</td>
<td class="nowrap side_pad" colspan=3>Access Permissions $per_link</td>
</tr>];
foreach my $del ( @{ $delegates->{'delegates'} } ) {
print qq[
<tr class=light_grey_bg>
<td class="nowrap middle side_pad"><a href="group.cgi?nt_group_id=$del->{'nt_group_id'}"> <img src="$NicToolClient::image_dir/group.gif" alt="group">$del->{'group_name'}</a> </td>
<td class="nowrap middle side_pad"><a href="user.cgi?nt_user_id=$del->{'delegated_by_id'}"> <img src="$NicToolClient::image_dir/user.gif" alt="user"> $del->{'delegated_by_name'} </a> </td>
<td class="nowrap side_pad">
<img src="$NicToolClient::image_dir/perm-]
. ( $del->{delegate_write} ? 'checked' : 'unchecked' )
. qq[.gif" alt=""> Write <img src="$NicToolClient::image_dir/perm-]
. ( $del->{delegate_delete} ? "checked" : "unchecked" )
. qq[.gif" alt=""> Remove <img src="$NicToolClient::image_dir/perm-]
. ( $del->{delegate_delegate} ? 'checked' : 'unchecked' )
. qq[.gif" alt=""> Re-delegate <img src="$NicToolClient::image_dir/perm-]
. ( $del->{delegate_add_records} ? 'checked' : 'unchecked' )
. qq[.gif" alt=""> Add Records <img src="$NicToolClient::image_dir/perm-]
. ( $del->{delegate_delete_records} ? 'checked' : 'unchecked' )
. qq[.gif" alt=""> Delete Records
</td>
<td class="nowrap width1">];
if ( !$zone->{'deleted'} && $user->{zone_delegate} ) {
print qq[<a href="javascript:void window.open('delegate_zones.cgi?obj_list=$zone->{'nt_zone_id'}&nt_group_id=$del->{'nt_group_id'}&edit=1', 'delegate_win', 'width=640,height=480,scrollbars,resizable=yes')">Edit</a>];
}
else {
print "<span class=disabled>Edit</span>";
}
print qq[
</td>
<td class="nowrap center width1" title="Remove Delegation">];
if ( !$zone->{'deleted'} && $user->{zone_delegate} ) {
print qq[<a href="zone.cgi?nt_zone_id=$zone->{'nt_zone_id'}&nt_group_id=$gid&delegate_group_id=$del->{'nt_group_id'}&deletedelegate=1" onClick="return confirm('Are you sure you want to remove the delegation of zone $zone->{'zone'} to group $del->{'group_name'}?');"><img src="$NicToolClient::image_dir/trash-delegate.gif" alt="Remove Delegation"></a>];
}
else {
print qq[<img src="$NicToolClient::image_dir/trash-delegate-disabled.gif" alt="disabled">];
}
print qq[
</td>
</tr>];
}
print qq[
</table>];
};
sub display_zone_properties {
my ($nt_obj, $q, $zone, $user) = @_;
my $state = 'nt_group_id=' . $q->param('nt_group_id');
foreach ( @{ $nt_obj->paging_fields } ) {
next if ! $q->param($_);
$state .= "&$_=" . $q->escape( $q->param($_) );
}
$state .= "&nt_zone_id=$zone->{'nt_zone_id'}&edit_zone=1";
my $isdelegate = exists $zone->{'delegated_by_id'};
my $edit_opt = qq[<li class="disabled">Edit</li>];
if ( !$zone->{'deleted'}
&& $user->{'zone_write'}
&& ( $isdelegate ? $zone->{'delegate_write'} : 1 ) )
{
$edit_opt = qq[<li><a href="zone.cgi?$state">Edit</a></li>];
}
print qq[
<div id="propertiesHeader" class="side_pad dark_grey_bg">
<b>Properties</b>
<ul class="menu_r">
<li class=first id="zpHide" onClick="\$('zonePropertiesDiv').hide(); \$('zpHide').hide(); \$('zpShow').show();">Hide</li>
<li class=first id="zpShow" style="display:none;" onClick="\$('zonePropertiesDiv').show(); \$('zpHide').show(); \$('zpShow').hide();">Show</li>
$edit_opt
</ul>
</div>
<div id="zonePropertiesDiv">
<table id="propertiesDetail" class="fat">
<tr>
<td class="width50">
<table class="fat">];
foreach ( qw/ mailaddr description serial minimum / ) {
print qq[
<tr class=light_grey_bg>
<td class="nowrap pad2">$_:</td>
<td class="fat pad2">$zone->{$_}</td>
</tr>],
}
print qq[
</table>
</td>
<td class="width50 top">
<table class="fat">];
foreach ( qw/ refresh retry expire ttl / ) {
print qq[
<tr class=light_grey_bg>
<td class="nowrap pad2">$_:</td>
<td class="fat pad2">$zone->{$_}</td>
</tr>];
}
print qq[
</table>
</td>
</tr>
</table>
</div>];
};
sub display_nameservers {
my ( $nt_obj, $user, $q, $zone ) = @_;
my $isdelegate = exists $zone->{'delegated_by_id'};
my @fields = qw/ name address description /;
print qq[
<div id="zoneNameservers" class="no_pad">
<div id="zoneNameserverHeader" class="dark_grey_bg margin0">
<span class="bold">Nameservers</span>
<ul class="menu_r">
<li class=first id="znsHide" onClick="(\$'zoneNameserverListDiv').hide(); \$('znsHide').hide(); \$('znsShow').show();">Hide</li>
<li class=first id="znsShow" style="display:none;" onClick="\$('zoneNameserverListDiv').show(); \$('znsHide').show(); \$('znsShow').hide();">Show</li>];
if ( !$zone->{'deleted'} && $user->{'zone_write'}
&& ( $isdelegate ? $zone->{'delegate_write'} : 1 ) )
{
my $state = 'nt_group_id=' . $q->param('nt_group_id');
foreach ( @{ $nt_obj->paging_fields } ) {
next if ! $q->param($_);
$state .= "&$_=" . $q->escape( $q->param($_) );
}
$state .= "&nt_zone_id=$zone->{'nt_zone_id'}&edit_zone=1";
print qq[<li><a href="zone.cgi?$state">Edit</a></li>];
}
else {
print qq[<li class="disabled">Edit</li>];
}
print qq[
</ul>
</div>
<div id="zoneNameserverListDiv">
<table id="zoneNameserverList" class="pad1 fat">
<tr class="dark_grey_bg pad2">
<td class=center>name</td>
<td class=center>address</td>
<td class=center>description</td>
</tr>];
my $x = 1;
foreach my $ns ( @{ $zone->{'nameservers'} } ) {
my $bgcolor = $x++ % 2 == 0 ? 'light_grey_bg' : 'white_bg';
print qq[
<tr class="$bgcolor">
<td><img class="no_pad" src="$NicToolClient::image_dir/nameserver.gif" alt="nameserver">$ns->{name}</td>
<td>$ns->{address}</td>
<td>$ns->{description}</td>
</tr>];
}
print qq[
</table>
</div>
</div>];
}
sub display_zone_records {
my ( $nt_obj, $user, $q, $zone ) = @_;
my $group = $nt_obj->get_group( nt_group_id => $user->{'nt_group_id'} );
# process submitted form actions
display_zone_records_new( $nt_obj, $user, $q, $zone );
display_zone_records_edit( $nt_obj, $user, $q, $zone );
display_zone_records_delete( $nt_obj, $q );
my @columns = qw/ name type address ttl weight priority other description/;
my %labels = map { $_ => ucfirst($_) } @columns;
$labels{ttl} = 'TTL';
$labels{other} = 'Port';
if ( $q->param('edit_sortorder') ) {
$nt_obj->display_sort_options( $q, \@columns, \%labels, 'zone.cgi',
[ 'nt_group_id', 'nt_zone_id' ] );
};
if ( $q->param('edit_search') ) {
$nt_obj->display_advanced_search( $q, \@columns, \%labels, 'zone.cgi',
[ 'nt_group_id', 'nt_zone_id' ] );
};
my %params = ( nt_zone_id => $q->param('nt_zone_id') );
my %sort_fields;
$nt_obj->prepare_search_params( $q, \%labels, \%params, \%sort_fields, 50 );
if ( ! %sort_fields ) {
$sort_fields{'name'} = { 'order' => 1, 'mod' => 'Ascending' };
};
my $rv = $nt_obj->get_zone_records(%params);
return $nt_obj->display_nice_error( $rv, "Get Zone Records" )
if $rv->{'error_code'} != '200';
my $zone_records = $rv->{'records'};
my @state_fields;
foreach ( @{ $nt_obj->paging_fields } ) {
next if ! $q->param($_);
push @state_fields, "$_=" . $q->escape( $q->param($_) );
}
my $state_string = join('&', @state_fields);
# Display the RR header: Resource Records New Resource Record | View Resource Record Log
my $gid = $q->param('nt_group_id');
my $zid = $q->param('nt_zone_id');
my $options = qq[<li class=first><a href="zone_record_log.cgi?nt_group_id=$gid&nt_zone_id=$zid">View Resource Record Log</a></li>
];
my $zonedelegate = exists $zone->{'delegated_by_id'};
my $has_dperm = $zonedelegate ? $zone->{'delegate_write'} && $zone->{'delegate_add_records'} : 1;
if ( !$zone->{'deleted'} && $user->{'zonerecord_create'} && $has_dperm ) {
$options .= qq[<li><a href="zone.cgi?nt_group_id=$gid&nt_zone_id=$zid&$state_string&new_record=1#RECORD">New Resource Record</a></li>];
}
else {
$options .= qq[<li class=disabled>New Resource Record</li>];
}
print qq[
<hr class="side_pad">
<div class="dark_grey_bg side_pad">
<b>Resource Records</b>
<ul class="menu_r">
$options
</ul>
</div>];
$nt_obj->display_search_rows( $q, $rv, \%params, 'zone.cgi', [ 'nt_group_id', 'nt_zone_id' ] );
return if ! scalar @$zone_records;
# show only columns used in the records in this zone
@columns = qw/ name type address ttl /;
my %has_type;
foreach my $r_record (@$zone_records) {
$has_type{ $r_record->{'type'} }++;
}
if ( $has_type{'MX'} || $has_type{'SRV'} ) {
push @columns, 'weight';
}
if ( $has_type{'SRV'} ) {
push @columns, 'priority', 'other';
}
push @columns, 'description';
print qq[
<table id="zoneRecordTable" class="fat">
<tr id="zoneRecordHeaderRow" class=dark_grey_bg>];
foreach (@columns) {
if ( $sort_fields{$_} ) {
my $dir = uc( $sort_fields{$_}->{'mod'} ) eq 'ASCENDING' ? 'up' : 'down';
print qq[
<td class="dark_bg center">
$labels{$_} $sort_fields{$_}->{'order'}
<img src="$NicToolClient::image_dir/$dir.gif" alt="sort order">
</td>];
}
else {
print qq[\n <td class=center>$labels{$_}</td>];
}
}
print qq[
<td class="center width1"></td>
<td class="center width1"></td>
</tr>];
my $x = 0;
my ( $isdelegate, $img );
foreach my $r_record (@$zone_records) {
$isdelegate = exists $r_record->{'delegated_by_id'};
$img = $isdelegate ? '-delegated' : '';
$img = "$NicToolClient::image_dir/r_record$img.gif";
my $bgclass = $x++ % 2 == 0 ? 'light_grey_bg' : 'white_bg';
my $hilite = $x % 2 == 0 ? 'light_hilite_bg' : 'dark_hilite_bg';
if ( $r_record->{'nt_zone_record_id'} eq $q->param('new_record_id') ) {
$bgclass = $hilite;
};
print qq[
<tr class=$bgclass>];
$r_record->{name} = "@ ($zone->{'zone'})" if $r_record->{name} eq "@";
if ( $r_record->{type} !~ /^MX|SRV$/i ) {
$r_record->{weight} = '';
}
# shorten the max width of the address field (workaround for
# display formatting problem with DomainKey entries.
if ( length $r_record->{address} > 45 ) {
if ( $r_record->{type} =~ /^(?:DNSKEY|RRSIG)$/ ) {
$r_record->{title} = $r_record->{address};
$r_record->{address} = substr($r_record->{address}, 0, 35) . ' ...<br>(tip: hover over address)';
}
elsif ( $r_record->{type} =~ /^(?:TXT)$/ && length $r_record->{address} > 100 ) {
$r_record->{title} = $r_record->{address};
$r_record->{address} = substr($r_record->{address}, 0, 35) . ' ...<br>(tip: hover over address)';
}
else {
my $max = 0;
my @lines = ();
while ( $max < length $r_record->{address} ) {
push @lines, substr( $r_record->{address}, $max, 40 );
$max += 40;
}
$r_record->{address} = join "<br>", @lines;
};
}
if ( $r_record->{type} eq 'IPSECKEY' ) {
$r_record->{description} = substr( $r_record->{description}, 0, 10 ) . ' ...';
};
if ( $r_record->{type} eq 'AAAA' ) {
$r_record->{address} =~ s/:[0]+/:/g; # compress leading zeros
};
foreach (@columns) {
if ( $_ eq 'name' ) {
print qq[
<td>];
my $edit_url = "zone.cgi?$state_string&nt_zone_record_id=$r_record->{'nt_zone_record_id'}&nt_zone_id=$zone->{'nt_zone_id'}&nt_group_id=$gid&edit_record=1#RECORD";
if ( !$zone->{'deleted'} ) {
print qq[<a href="$edit_url"><img src="$img" alt="rr"></a>];
print qq[<a href="$edit_url">$r_record->{$_}</a>];
}
else {
print qq[<img src="$img" alt="resource record">];
print $r_record->{$_};
}
if ( $r_record->{'delegated_by_id'} ) {
my $write = $r_record->{'delegate_write'} ? 'write' : 'nowrite';
print qq[ <img src="$NicToolClient::image_dir/perm-$write.gif" alt="delegate write permission">];
}
print qq[
</td>];
}
elsif ( $_ =~ /address|ttl|weight|priority|other/i ) {
print qq[
<td class="right" title="$r_record->{title}"> $r_record->{$_} </td>];
}
else {
print qq[
<td class="center"> $r_record->{$_} </td>];
}
}
print qq[
<td class=center>];
if ( !$zone->{'deleted'}
&& $group->{'has_children'}
&& $user->{'zonerecord_delegate'}
&& ( $isdelegate
? $r_record->{'delegate_delegate'}
: ( $zonedelegate ? $zone->{'delegate_delegate'} : 1 )
)
)
{
print qq[<a href="javascript:void window.open('delegate_zones.cgi?type=record&obj_list=$r_record->{'nt_zone_record_id'}&nt_zone_id=$r_record->{'nt_zone_id'}', 'delegate_win', 'width=640,height=480,scrollbars,resizable=yes')">
<img src="$NicToolClient::image_dir/delegate.gif" alt="Delegate Resource Record"></a></td>];
}
else {
print qq[<img src="$NicToolClient::image_dir/delegate-disabled.gif" alt="delegate disabled"></td>];
}
if ( !$zone->{'deleted'}
&& $user->{'zonerecord_delete'}
&& !$isdelegate
&& ( $zonedelegate ? $zone->{'delegate_delete_records'} : 1 )
)
{
my $quoted_address = $r_record->{'address'};
$quoted_address =~ s/["']//g; # remove " or ' from TXT/SPF records
$quoted_address =~ s/<br>//g; # remove <br> inserted 64 lines above
print qq[
<td class=center>
<a href="zone.cgi?$state_string&nt_zone_id=$zone->{'nt_zone_id'}&nt_group_id=$gid&nt_zone_record_id=$r_record->{'nt_zone_record_id'}&delete_record=$r_record->{'nt_zone_record_id'}" onClick=\"return confirm('Are you sure you want to delete $zone->{'zone'} $r_record->{'type'} record $r_record->{'name'} that points to $quoted_address ?')">
<img src="$NicToolClient::image_dir/trash.gif" alt="trash"></a></td>];
}
else {
$img = $isdelegate ? '-delegate' : '';
print qq[
<td class=center><img src="$NicToolClient::image_dir/trash$img-disabled.gif" alt="disabled"></td>];
}
print qq[\n </tr>];
}
print qq[\n</table>];
}
sub display_zone_records_new {
my ( $nt_obj, $user, $q, $zone ) = @_;
return if ! $q->param('new_record');
return if $q->param('Cancel'); # do nothing
return display_edit_record( $nt_obj, $user, $q, '', $zone, 'new' )
if ! $q->param('Create');
my @fields = qw/ nt_group_id nt_zone_id name type address
weight priority other ttl description /;
my %data = map { $_ => $q->param($_) } @fields;
my $error = $nt_obj->new_zone_record(%data);
if ( $error->{'error_code'} != 200 ) {
display_edit_record( $nt_obj, $user, $q, $error, $zone, 'new' );
return;
};
$q->param( -name => 'new_record_id',
-value => $error->{'nt_zone_record_id'} );
$nt_obj->display_nice_message(
"New Zone Record '$data{name}' Created", "New Zone Record" );
};
sub display_zone_records_edit {
my ( $nt_obj, $user, $q, $zone ) = @_;
return if ! $q->param('edit_record');
return if $q->param('Cancel'); # do nothing
return display_edit_record( $nt_obj, $user, $q, '', $zone, 'edit' )
if ! $q->param('Save');
my @fields = qw( nt_group_id nt_zone_id nt_zone_record_id name type
address weight priority other ttl description deleted );
my %data;
foreach my $x (@fields) {
$data{$x} = $q->param($x);
}
my $error = $nt_obj->edit_zone_record(%data);
if ( $error->{'error_code'} != 200 ) {
return display_edit_record( $nt_obj, $user, $q, $error, $zone, 'edit' );
}
$nt_obj->display_nice_message(
"Zone Record successfully modified.", "Edit Zone Record" );
};
sub display_zone_records_delete {
my ( $nt_obj, $q ) = @_;
return if ! $q->param('delete_record');
my $error = $nt_obj->delete_zone_record(
nt_group_id => $q->param('nt_group_id'),
nt_zone_record_id => $q->param('nt_zone_record_id')
);
if ( $error->{'error_code'} != 200 ) {
$nt_obj->display_nice_error( $error, "Delete Zone Record" );
}
else {
$nt_obj->display_nice_message(
"Zone Record successfully deleted.",
"Delete Zone Record" );
}
};
sub display_edit_record {
my ( $nt_obj, $user, $q, $message, $zone, $edit ) = @_;
my $zone_record = { 'ttl' => $NicToolClient::default_zone_record_ttl };
my $action = 'New';
my $message2;
# is this a Save or Edit operation?
if ( $q->param('nt_zone_record_id') && !$q->param('Save') ) {
if ( $q->param('nt_zone_record_log_id') ) {
$action = 'Recover';
$zone_record = $nt_obj->get_zone_record_log_entry(
nt_zone_record_id => $q->param('nt_zone_record_id'),
nt_zone_record_log_id => $q->param('nt_zone_record_log_id')
);
}
else {
$action = 'Edit';
$zone_record = $nt_obj->get_zone_record(
nt_group_id => $q->param('nt_group_id'),
nt_zone_id => $q->param('nt_zone_id'),
nt_zone_record_id => $q->param('nt_zone_record_id')
);
}
if ( $zone_record->{'error_code'} != 200 ) {
$message2 = $zone_record;
}
else {
$zone_record->{'name'} = $zone->{'zone'} . "."
if $zone_record->{'name'} eq "@" && $zone->{'zone'};
$zone_record->{'address'} = $zone->{'zone'} . "."
if $zone_record->{'address'} eq "@" && $zone->{'zone'};
}
}
my $isdelegate = exists $zone_record->{'delegated_by_id'};
my $pseudo = $zone_record->{'pseudo'};
# does user have Edit permissions?
my $modifyperm = !$isdelegate && $user->{'zonerecord_write'}
|| $isdelegate
&& $user->{'zonerecord_write'}
&& $zone_record->{'delegate_write'};
my $default_record_type = $zone_record->{'type'};
$default_record_type = 'PTR' if $zone->{'zone'} =~ /(in-addr|ip6)\.arpa/;
my $rr_type_popup = _build_rr_type_menu( $nt_obj, $q, $zone, $zone_record, $default_record_type, $modifyperm );
if ($modifyperm) {
print qq[
<form method="post" action="zone.cgi" name="rr_edit">],
$q->hidden( -name => $edit . '_record' ), "\n",
$q->hidden( -name => 'nt_group_id' ), "\n",
$q->hidden( -name => 'nt_zone_id' ), "\n";
print $q->hidden( -name => 'nt_zone_record_id' ) if $edit eq 'edit';
print $q->hidden( -name => 'nt_zone_record_log_id' ), "\n";
print $q->hidden( -name => 'deleted', -value => 0 ) if $action eq 'Recover';
foreach ( @{ $nt_obj->paging_fields } ) {
print $q->hidden( -name => $_ ), "\n";
}
}
else {
$action = 'View' if $action eq 'Edit';
}
$nt_obj->display_nice_error($message) if $message;
$nt_obj->display_nice_error($message2) if $message2;
print qq[
<a name="RECORD" id="RECORD"></a>
<div class="dark_bg">$action Resource Record</div>];
# display delegation information
if ( !$isdelegate && $edit ne 'new' ) {
my $delegates = $nt_obj->get_zone_record_delegates(
nt_zone_record_id => $zone_record->{'nt_zone_record_id'} );
if ( $delegates->{error_code} ne 200 ) {
warn "error get_zone_record_delegates(nt_zone_record_id=>$zone_record->{'nt_zone_record_id'}: "
. $delegates->{'error_code'} . " "
. $delegates->{'error_msg'};
}
elsif ( @{ $delegates->{'delegates'} } gt 0 ) {
display_edit_record_delegates( $nt_obj, $q, $user, $zone_record, $delegates );
}
}
elsif ( $edit ne 'new' && !$pseudo ) {
display_new_record_delegates( $user, $zone_record, $q );
};
print qq[
<table class="fat">
<tr id=name class="light_grey_bg">
<td class="right"> Name:</td>
<td class="fat">],
$modifyperm ? $q->textfield(
-name => 'name',
-size => 40,
-maxlength => 127,
-default => $zone_record->{'name'},
) : $zone_record->{'name'},
( $zone_record->{'name'} ne "$zone->{'zone'}." ? "<b>.$zone->{'zone'}.</b>" : ''),
qq[
</td>
</tr>
<tr id=type class="light_grey_bg">
<td id=type_label class=right> Type:</td>
<td id=type class="fixedwidth fat"> $rr_type_popup </td>
</tr>
<tr id=address class="light_grey_bg">
<td id=address_label class="right">Address:</td>
<td id=address class="fat">], _build_rr_address( $q, $zone_record, $modifyperm ), $nt_obj->help_link('rraddress'), qq[
</td>
</tr>
<tr id=tr_weight class="light_grey_bg">
<td id=weight_label class="right"> Weight:</td>
<td id=weight class="fat">], $modifyperm ? $q->textfield(
-id => 'weight',
-name => 'weight',
-size => 5,
-maxlength => 10,
-default => '10',
-default => $zone_record->{'weight'}
)
: $zone_record->{'weight'},
qq[
</td>
</tr>
<tr id="tr_priority" class="light_grey_bg">
<td id=priority_label class="right"> Priority:</td>
<td id=priority class="fat">], $modifyperm
? $q->textfield(
-id => 'priority',
-name => 'priority',
-size => 5,
-maxlength => 10,
-default => '10',
-default => $zone_record->{'priority'}
)
: $zone_record->{'priority'}, qq[
</td>
</tr>
<tr id="tr_other" class="light_grey_bg">
<td id=other_label class="right"> Port:</td>
<td id=other class="fat">], $modifyperm ? $q->textfield(
-id => 'other',
-name => 'other',
-size => 5,
-maxlength => 10,
-default => '10',
-default => $zone_record->{'other'}
)
: $zone_record->{'other'},
qq[
</td>
</tr>
<tr id=ttl class="light_grey_bg">
<td class="right"> TTL:</td>
<td class="fat">], $modifyperm ? $q->textfield(
-name => 'ttl',
-size => 5,
-maxlength => 10,
-default => $zone_record->{'ttl'}
)
: $zone_record->{'ttl'}, qq[
</td>
</tr>
<tr id=description class="light_grey_bg">
<td id=description_label class="right"> Description:</td>
<td id=description class="fat">], $modifyperm ? $q->textfield(
-id => 'description',
-name => 'description',
-size => 60,
-maxlength => 128,
-default => $zone_record->{'description'}
)
: $zone_record->{'description'} || " ",
qq[
</td>
</tr>
<tr id=submit class="dark_grey_bg">
<td colspan="2" class="center">],
$modifyperm ? $q->submit( $edit eq 'edit' ? 'Save' : 'Create' )
. $q->submit('Cancel')
: ' ', qq[
</td>
</tr>
</table>
<script>
\$(document).ready(function(){
selectedRRType('$default_record_type');
});
</script>];
print $q->end_form if $modifyperm;
}
sub _build_rr_type_menu {
my ( $nt_obj, $q, $zone, $zone_record, $default_record_type, $modifyperm ) = @_;
my ( $type_values, $type_labels );
my $rr_types = $nt_obj->rr_types;
$q->autoEscape(undef) if $modifyperm;
# present RR types appropriate for the type of zone
if ( $zone->{'zone'} =~ /(in-addr|ip6)\.arpa$/ ) {
my %reverse = map {
my $spa = ' ' x (11-length $_->{name});
$_->{name} => $_->{name} . ${spa} . $_->{description} }
grep( $_->{reverse} == 1, @$rr_types);
$type_values = [ sort keys %reverse ];