2 # $Id: machines.cgi,v 1.12 2006/12/27 23:00:04 rmurray Exp $
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>
10 #use Apache::Registry;
13 use Net::LDAP qw(:all);
17 use Digest::MD5 qw(md5_hex);
19 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
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',
27 'sponsor' => 'Sponsor',
28 'sponsor-admin' => 'Sponsor admin',
29 'location' => 'Location',
30 'machine' => 'Processor',
32 'disk' => 'Disk space',
33 'bandwidth' => 'Bandwidth',
36 'sshrsahostkey' => 'SSH host key',
37 'sshrsahostfprint' => 'SSH host fingerprint',
38 'description' => 'Description',
39 # 'createtimestamp' => 'Entry created',
40 # 'modifytimestamp' => 'Entry modified'
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
50 %summaryattrs = ('hostname' => 'Host name',
51 'host' => 'just for a link',
52 'architecture' => 'Architecture',
53 'distribution' => 'Distribution',
55 'access' => 'Access');
57 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
60 my %config = &Util::ReadConfigFile;
62 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
64 $ldap->unbind if (defined($ldap));
67 # human readable fingerprint
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,"");
84 $SIG{__DIE__} = \&DieHandler;
87 my $host = lc($query->param('host'));
89 &Util::HTMLSendHeader;
90 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
94 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
95 $mesg->code && &Util::HTMLError($mesg->error);
96 $entries = $mesg->as_struct;
98 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
99 $data = $entries->{$dn};
101 my $thishost = $data->{host}->[0];
104 if (lc($thishost) eq $host) {
105 $output{havehostdata} = 1;
107 foreach $key (keys(%attrs)) {
108 $output{$key} = $data->{$key}->[0];
111 $output{hostname} = undef;
112 foreach my $hostname (@{$data->{hostname}}) {
113 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
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});
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'});
124 $output{sshrsahostkey} = undef;
125 foreach $key (@{$data->{sshrsahostkey}}) {
126 $output{sshrsahostkey} .= $key . "<br>";
129 foreach $key (@{$data->{sshrsahostkey}}) {
130 $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
134 my ($sponsor, $url) = undef;
135 $output{sponsor} = undef;
136 foreach $sponsor (@{$data->{sponsor}}) {
137 $sponsor =~ m#((http|ftp)://\S+)#i;
139 $sponsor =~ s/\s*$url\s*//;
140 $output{sponsor} .= "<br>" if ($output{sponsor});
142 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
144 $output{sponsor} .= $sponsor;
148 $selected = " selected ";
151 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
153 # collect summary info
154 foreach $key (keys(%summaryattrs)) {
155 $summary{$thishost}{$key} = $data->{$key}->[0];
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);
165 if ($output{havehostdata}) {
166 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
167 $hostdetails .= "<ul>\n";
168 foreach $key (@attrorder) {
170 $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
173 $hostdetails .= "</ul>\n";
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>";
181 $hostdetails .= "</tr>\n";
183 foreach $host (sort(keys(%summary))) {
184 $hostdetails .= "<tr>";
185 foreach $key (@summaryorder) {
186 $hostdetails .= "<td>$summary{$host}{$key} </td>";
188 $hostdetails .= "</tr>\n";
190 $hostdetails .= "</table>\n";
193 # Finally, we can write the output... yuck...
194 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
196 s/~hostlist~/$hostlist/;
197 s/~hostdetails~/$hostdetails/;