Allow dashes in hostnames for sudo passwords (RT #5785)
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.12 2006/12/27 23:00:04 rmurray Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5 # (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
6 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
7 # (c) 2008 Martin Zobel-Helas. Licensed under the GPL. <zobel@debian.org>
8
9 use lib '.';
10 use strict vars;
11 #use Apache::Registry;
12 use CGI;
13 use Util;
14 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
15 use Fcntl;
16 use POSIX;
17 use MIME::Base64;
18 use Digest::MD5 qw(md5_hex);
19
20 my (%attrs, @attrorder, %summaryattrs, @summaryorder, %summarylistitems);
21
22 # This defines the description of the fields, and which fields are retrieved
23 %attrs = ('hostname' => 'Host name',
24           'admin' => 'Admin contact',
25           'architecture' => 'Architecture',
26           'distribution' => 'Distribution',
27           'access' => 'Access',
28           'sponsor' => 'Sponsor',
29           'sponsor-admin' => 'Sponsor admin',
30           'location' => 'Location',
31           'machine' => 'Processor',
32           'memory' => 'Memory',
33           'disk' => 'Disk space',
34           'bandwidth' => 'Bandwidth',
35           'status' => 'Status',
36           'notes' => 'Notes',
37           'sshrsahostkey' => 'SSH host key',
38           'sshrsahostfprint' => 'SSH host fingerprint',
39           'description' => 'Description',
40           'purpose' => 'purposes of this server',
41 #         'createtimestamp' => 'Entry created',
42 #         'modifytimestamp' => 'Entry modified'
43          );
44
45 # This defines what fields are displayed, and in what order
46 @attrorder = qw(hostname admin architecture distribution access
47                 sponsor sponsor-admin location machine memory
48                 disk bandwidth status notes sshrsahostkey sshrsahostfprint
49                 description purpose);
50
51 # ditto for summary
52 %summaryattrs = ('hostname' => 'Host name',
53                  'host'     => 'just for a link',
54                  'description' => 'Description',
55                  'architecture' => 'Architecture',
56                  'status' => 'Status',
57                  'access' => 'Access',
58                  'sponsor' => 'Sponsor',
59                  'purpose' => 'Purpose');
60 @summaryorder = qw{hostname description architecture sponsor purpose status access};
61 %summarylistitems = map {$_=>1} qw{purpose sponsor};
62
63 # Global settings...
64 my %config = &Util::ReadConfigFile;
65
66 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
67 sub DieHandler {
68   $ldap->unbind if (defined($ldap));
69 }
70
71 # human readable fingerprint
72 sub sshfingerprint {
73     my $key = shift;
74
75     return '' if (!$key);
76
77     my @field = split(/ /, $key);
78     my %keytypes = map {$_=>1} (qw{ssh-dss ssh-rsa ecdsa-sha2-nistp256 ssh-ed25519});
79     return '' unless $keytypes{$field[0]};
80     return '' if !$field[1];
81     my $fpr = md5_hex(decode_base64($field[1]));
82     my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
83     while (length $fpr > 0) {
84        $hrfpr .= ':' . substr($fpr,0,2,"");
85     }
86     return $hrfpr;
87 }
88
89 sub wiki_link($) {
90         my ($in) = @_;
91         # [[hostname|text]] makes a link
92         # [[hostname]] makes a link too
93         # if you add a * after [[ it's still the same, only not used for ssh_known_hosts in ud-generate
94         # [[-hostname]] are not links, but get added to known_hosts.  we should drop the [[- ]] tho
95         $in =~ s#\[\[-(.*?)\]\]#$1#g;
96         $in =~ s#\[\[\*?(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
97         $in =~ s#\[\[\*?(.*?)\]\]#<a href="http://$1">$1</a>#g;
98         return $in;
99 }
100
101 # in the purpose field [[host|some other text]] (where some other text is optional)
102 # makes a hyperlink on the web thing. we now also add these hosts to the ssh known_hosts
103 # file.  But so that we don't have to add everything we link we can add an asterisk
104 # and say [[*... to ignore it.  In order to be able to add stuff to ssh without
105 # http linking it we also support [[-hostname]] entries.
106 #
107 # sponsors are also wikified like purpose.  maybe others as well
108 sub item_uplist($) {
109         my ($items) = @_;
110         my $out = undef;
111         my(@tmp) = @$items;
112
113         if (scalar @tmp>= 1) {
114                 $out = "<ul>".
115                         join("", map { 
116                                 "<li>".wiki_link($_)."</li>\n";
117                           } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]\*]//g; $B =~ s/[\[\]\*]//g; $A cmp $B} @tmp
118                         ).
119                         "</ul>";
120         }
121         return $out;
122 }
123
124 #$SIG{__DIE__} = \&DieHandler;
125
126 my $query = new CGI;
127 my $host = lc($query->param('host'));
128 my $sortby = lc($query->param('sortby')) || "host";
129 my $sortorder = lc($query->param('sortorder')) || "asc";
130
131
132 &Util::HTMLSendHeader;
133 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
134 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
135 $mesg;
136 $ldap->bind;
137
138 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
139 $mesg->code && &Util::HTMLError($mesg->error);
140 $entries = $mesg->as_struct;
141
142 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
143   $data = $entries->{$dn};
144
145   my $thishost = $data->{host}->[0];
146   $selected = "";
147   
148   if (lc($thishost) eq $host) {
149     $output{havehostdata} = 1;
150
151     foreach $key (keys(%attrs)) {
152       $output{$key} = $data->{$key}->[0];
153     }
154   
155     $output{hostname} = undef;
156     foreach my $hostname (@{$data->{hostname}}) {
157       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
158     }
159
160     # Modified/created time. TODO: maybe add is the name of the creator/modifier
161     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
162     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
163     
164     # Format email addresses
165     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
166     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
167
168     $output{sshrsahostkey} = undef;
169     foreach $key (@{$data->{sshrsahostkey}}) {
170       $output{sshrsahostkey} .= $key . "<br>";
171     }
172
173     foreach $key (@{$data->{sshrsahostkey}}) {
174       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
175     }
176     
177     my $sponsor = item_uplist($data->{sponsor});
178     $output{sponsor} = $sponsor if defined $sponsor;
179     my $purpose = item_uplist($data->{purpose});
180     $output{purpose} = $purpose if defined $purpose;
181
182     $selected = " selected ";
183   }
184
185   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n" unless ($data->{status}->[0] =~ /^unlisted/);
186
187   # collect summary info
188   foreach $key (keys(%summaryattrs)) {
189     if (exists $summarylistitems{$key}) {
190       my $v = item_uplist($data->{$key});
191       $summary{$thishost}{$key} = $v if defined $v;
192     } else {
193       $summary{$thishost}{$key} = $data->{$key}->[0];
194     }
195   }
196
197   $summary{$thishost}{hostname} = undef;
198   foreach my $hostname (@{$data->{hostname}}) {
199     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
200   }
201 }
202 $ldap->unbind;
203
204 if ($output{havehostdata}) {
205   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
206   $hostdetails .= "<ul>\n";
207   foreach $key (@attrorder) {
208     if ($output{$key}) {
209       $hostdetails .= "<li><b>$attrs{$key}</b>: $output{$key}\n";
210     }
211   }
212   $hostdetails .= "</ul>\n";
213 } else {
214   # display summary info
215   $hostdetails = "<h1>Summary</h1>\n";
216   $hostdetails .= "<table id=\"machines\" class=\"tablesorter\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\">\n<thead>\n<tr>";
217   foreach $key (@summaryorder) {
218     if ($sortby ne $key) {
219       $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
220     } else {
221       if ($sortorder ne "dsc") {
222         $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
223       } else {
224         $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
225       }
226     }
227   }
228   $hostdetails .= "</tr>\n</thead>\n<tbody>\n";
229   
230   my @sorted;
231   if ($sortorder eq "asc") {
232      @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
233   } else {
234      @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
235   }
236   foreach $host (@sorted) {
237     next if $summary{$host}{status} =~ /^unlisted/;
238     $hostdetails .= "<tr>";
239     foreach $key (@summaryorder) {
240       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
241     }
242     $hostdetails .= "</tr>\n";
243   }
244   $hostdetails .= "</tbody>\n</table>\n";
245 }
246
247 # Finally, we can write the output... yuck...
248 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
249 while (<F>) {
250   s/~hostlist~/$hostlist/;
251   s/~hostdetails~/$hostdetails/;
252   print;
253 }
254 close F;