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