Improve sshfingerprint implementation, remove unavailable fields from being displayed
[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
8 use lib '.';
9 use strict vars;
10 #use Apache::Registry;
11 use CGI;
12 use Util;
13 use Net::LDAP qw(:all);
14 use Fcntl;
15 use POSIX;
16 use MIME::Base64;
17 use Digest::MD5 qw(md5_hex);
18
19 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
20
21 # This defines the description of the fields, and which fields are retrieved
22 %attrs = ('hostname' => 'Host name',
23           'admin' => 'Admin contact',
24           'architecture' => 'Architecture',
25           'distribution' => 'Distribution',
26           'access' => 'Access',
27           'sponsor' => 'Sponsor',
28           'sponsor-admin' => 'Sponsor admin',
29           'location' => 'Location',
30           'machine' => 'Processor',
31           'memory' => 'Memory',
32           'disk' => 'Disk space',
33           'bandwidth' => 'Bandwidth',
34           'status' => 'Status',
35           'notes' => 'Notes',
36           'sshrsahostkey' => 'SSH host key',
37           'sshrsahostfprint' => 'SSH host fingerprint',
38           'description' => 'Description',
39 #         'createtimestamp' => 'Entry created',
40 #         'modifytimestamp' => 'Entry modified'
41          );
42
43 # This defines what fields are displayed, and in what order
44 @attrorder = qw(hostname admin architecture distribution access
45                 sponsor sponsor-admin location machine memory
46                 disk bandwidth status notes sshrsahostkey sshrsahostfprint
47                 description);
48
49 # ditto for summary
50 %summaryattrs = ('hostname' => 'Host name',
51                  'host'     => 'just for a link',
52                  'architecture' => 'Architecture',
53                  'distribution' => 'Distribution',
54                  'status' => 'Status',
55                  'access' => 'Access');
56                  
57 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
58
59 # Global settings...
60 my %config = &Util::ReadConfigFile;
61
62 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
63 sub DieHandler {
64   $ldap->unbind if (defined($ldap));
65 }
66
67 # human readable fingerprint
68 sub sshfingerprint {
69     my $key = shift;
70
71     return '' if (!$key);
72
73     my @field = split(/ /, $key);
74     return '' if $field[0] ne 'ssh-dss' and $field[0] ne 'ssh-rsa';
75     return '' if !$field[1];
76     my $fpr = md5_hex(decode_base64($field[1]));
77     my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
78     while (length $fpr > 0) {
79        $hrfpr .= ':' . substr($fpr,0,2,"");
80     }
81     return $hrfpr;
82 }
83
84 $SIG{__DIE__} = \&DieHandler;
85
86 my $query = new CGI;
87 my $host = lc($query->param('host'));
88
89 &Util::HTMLSendHeader;
90 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
91 $mesg;
92 $ldap->bind;
93
94 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
95 $mesg->code && &Util::HTMLError($mesg->error);
96 $entries = $mesg->as_struct;
97
98 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
99   $data = $entries->{$dn};
100
101   my $thishost = $data->{host}->[0];
102   $selected = "";
103   
104   if (lc($thishost) eq $host) {
105     $output{havehostdata} = 1;
106
107     foreach $key (keys(%attrs)) {
108       $output{$key} = $data->{$key}->[0];
109     }
110   
111     $output{hostname} = undef;
112     foreach my $hostname (@{$data->{hostname}}) {
113       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
114     }
115
116     # Modified/created time. TODO: maybe add is the name of the creator/modifier
117     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
118     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
119     
120     # Format email addresses
121     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
122     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
123
124     $output{sshrsahostkey} = undef;
125     foreach $key (@{$data->{sshrsahostkey}}) {
126       $output{sshrsahostkey} .= $key . "<br>";
127     }
128
129     foreach $key (@{$data->{sshrsahostkey}}) {
130       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
131     }
132     
133     # URL
134     my ($sponsor, $url) = undef;
135     $output{sponsor} = undef;
136     foreach $sponsor (@{$data->{sponsor}}) {
137       $sponsor =~ m#((http|ftp)://\S+)#i;
138       $url = $1;
139       $sponsor =~ s/\s*$url\s*//;
140       $output{sponsor} .= "<br>" if ($output{sponsor});
141       if ($url) {
142         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
143       } else {
144         $output{sponsor} .= $sponsor;
145       }
146     }
147     
148     $selected = " selected ";    
149   }
150   
151   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
152   
153   # collect summary info
154   foreach $key (keys(%summaryattrs)) {
155     $summary{$thishost}{$key} = $data->{$key}->[0];
156   }
157   
158   $summary{$thishost}{hostname} = undef;
159   foreach my $hostname (@{$data->{hostname}}) {
160     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
161   }
162 }
163 $ldap->unbind;
164
165 if ($output{havehostdata}) {
166   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
167   $hostdetails .= "<ul>\n";
168   foreach $key (@attrorder) {
169     if ($output{$key}) {
170       $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
171     }
172   }
173   $hostdetails .= "</ul>\n";
174 } else {
175   # display summary info
176   $hostdetails = "<h1>Summary</h1>\n";
177   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
178   foreach $key (@summaryorder) {
179     $hostdetails .= "<th>$summaryattrs{$key}</th>";
180   }
181   $hostdetails .= "</tr>\n";
182   
183   foreach $host (sort(keys(%summary))) {
184     $hostdetails .= "<tr>";
185     foreach $key (@summaryorder) {
186       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
187     }
188     $hostdetails .= "</tr>\n";
189   }
190   $hostdetails .= "</table>\n";
191 }
192
193 # Finally, we can write the output... yuck...
194 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
195 while (<F>) {
196   s/~hostlist~/$hostlist/;
197   s/~hostdetails~/$hostdetails/;
198   print;
199 }
200 close F;