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 'purpose' => 'purposes of this server',
40 # 'createtimestamp' => 'Entry created',
41 # 'modifytimestamp' => 'Entry modified'
44 # This defines what fields are displayed, and in what order
45 @attrorder = qw(hostname admin architecture distribution access
46 sponsor sponsor-admin location machine memory
47 disk bandwidth status notes sshrsahostkey sshrsahostfprint
51 %summaryattrs = ('hostname' => 'Host name',
52 'host' => 'just for a link',
53 'architecture' => 'Architecture',
54 'distribution' => 'Distribution',
56 'access' => 'Access');
58 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
61 my %config = &Util::ReadConfigFile;
63 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
65 $ldap->unbind if (defined($ldap));
68 # human readable fingerprint
74 my @field = split(/ /, $key);
75 return '' if $field[0] ne 'ssh-dss' and $field[0] ne 'ssh-rsa';
76 return '' if !$field[1];
77 my $fpr = md5_hex(decode_base64($field[1]));
78 my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
79 while (length $fpr > 0) {
80 $hrfpr .= ':' . substr($fpr,0,2,"");
85 $SIG{__DIE__} = \&DieHandler;
88 my $host = lc($query->param('host'));
90 &Util::HTMLSendHeader;
91 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
95 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
96 $mesg->code && &Util::HTMLError($mesg->error);
97 $entries = $mesg->as_struct;
99 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
100 $data = $entries->{$dn};
102 my $thishost = $data->{host}->[0];
105 if (lc($thishost) eq $host) {
106 $output{havehostdata} = 1;
108 foreach $key (keys(%attrs)) {
109 $output{$key} = $data->{$key}->[0];
112 $output{hostname} = undef;
113 foreach my $hostname (@{$data->{hostname}}) {
114 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
117 # Modified/created time. TODO: maybe add is the name of the creator/modifier
118 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
119 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
121 # Format email addresses
122 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
123 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
125 $output{sshrsahostkey} = undef;
126 foreach $key (@{$data->{sshrsahostkey}}) {
127 $output{sshrsahostkey} .= $key . "<br>";
130 foreach $key (@{$data->{sshrsahostkey}}) {
131 $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
135 my ($sponsor, $url) = undef;
136 $output{sponsor} = undef;
137 foreach $sponsor (@{$data->{sponsor}}) {
138 $sponsor =~ m#((http|ftp)://\S+)#i;
140 $sponsor =~ s/\s*$url\s*//;
141 $output{sponsor} .= "<br>" if ($output{sponsor});
143 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
145 $output{sponsor} .= $sponsor;
149 #Reformat purposes to be pleasing for the human eye:
150 $output{purpose} = join(",", @{$data->{purpose}});
152 $selected = " selected ";
155 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
157 # collect summary info
158 foreach $key (keys(%summaryattrs)) {
159 $summary{$thishost}{$key} = $data->{$key}->[0];
162 $summary{$thishost}{hostname} = undef;
163 foreach my $hostname (@{$data->{hostname}}) {
164 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
169 if ($output{havehostdata}) {
170 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
171 $hostdetails .= "<ul>\n";
172 foreach $key (@attrorder) {
174 $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
177 $hostdetails .= "</ul>\n";
179 # display summary info
180 $hostdetails = "<h1>Summary</h1>\n";
181 $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
182 foreach $key (@summaryorder) {
183 $hostdetails .= "<th>$summaryattrs{$key}</th>";
185 $hostdetails .= "</tr>\n";
187 foreach $host (sort(keys(%summary))) {
188 $hostdetails .= "<tr>";
189 foreach $key (@summaryorder) {
190 $hostdetails .= "<td>$summary{$host}{$key} </td>";
192 $hostdetails .= "</tr>\n";
194 $hostdetails .= "</table>\n";
197 # Finally, we can write the output... yuck...
198 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
200 s/~hostlist~/$hostlist/;
201 s/~hostdetails~/$hostdetails/;