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>
7 # (c) 2008 Martin Zobel-Helas. Licensed under the GPL. <zobel@debian.org>
11 #use Apache::Registry;
14 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
18 use Digest::MD5 qw(md5_hex);
20 my (%attrs, @attrorder, %summaryattrs, @summaryorder, %summarylistitems);
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',
28 'sponsor' => 'Sponsor',
29 'sponsor-admin' => 'Sponsor admin',
30 'location' => 'Location',
31 'machine' => 'Processor',
33 'disk' => 'Disk space',
34 'bandwidth' => 'Bandwidth',
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'
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
52 %summaryattrs = ('hostname' => 'Host name',
53 'host' => 'just for a link',
54 'architecture' => 'Architecture',
57 'sponsor' => 'Sponsor',
58 'purpose' => 'Purpose');
59 @summaryorder = qw{hostname architecture sponsor purpose status access};
60 %summarylistitems = map {$_=>1} qw{purpose sponsor};
63 my %config = &Util::ReadConfigFile;
65 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
67 $ldap->unbind if (defined($ldap));
70 # human readable fingerprint
76 my @field = split(/ /, $key);
77 my %keytypes = map {$_=>1} (qw{ssh-dss ssh-rsa ecdsa-sha2-nistp256});
78 return '' unless $keytypes{$field[0]};
79 return '' if !$field[1];
80 my $fpr = md5_hex(decode_base64($field[1]));
81 my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
82 while (length $fpr > 0) {
83 $hrfpr .= ':' . substr($fpr,0,2,"");
90 # [[hostname|text]] makes a link
91 # [[hostname]] makes a link too
92 # if you add a * after [[ it's still the same, only not used for ssh_known_hosts in ud-generate
93 # [[-hostname]] are not links, but get added to known_hosts. we should drop the [[- ]] tho
94 $in =~ s#\[\[-(.*?)\]\]#$1#g;
95 $in =~ s#\[\[\*?(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
96 $in =~ s#\[\[\*?(.*?)\]\]#<a href="http://$1">$1</a>#g;
100 # in the purpose field [[host|some other text]] (where some other text is optional)
101 # makes a hyperlink on the web thing. we now also add these hosts to the ssh known_hosts
102 # file. But so that we don't have to add everything we link we can add an asterisk
103 # and say [[*... to ignore it. In order to be able to add stuff to ssh without
104 # http linking it we also support [[-hostname]] entries.
106 # sponsors are also wikified like purpose. maybe others as well
112 if (scalar @tmp>= 1) {
115 "<li>".wiki_link($_)."</li>\n";
116 } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]\*]//g; $B =~ s/[\[\]\*]//g; $A cmp $B} @tmp
123 $SIG{__DIE__} = \&DieHandler;
126 my $host = lc($query->param('host'));
127 my $sortby = lc($query->param('sortby')) || "host";
128 my $sortorder = lc($query->param('sortorder')) || "asc";
131 &Util::HTMLSendHeader;
132 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
133 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
137 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
138 $mesg->code && &Util::HTMLError($mesg->error);
139 $entries = $mesg->as_struct;
141 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
142 $data = $entries->{$dn};
144 my $thishost = $data->{host}->[0];
147 if (lc($thishost) eq $host) {
148 $output{havehostdata} = 1;
150 foreach $key (keys(%attrs)) {
151 $output{$key} = $data->{$key}->[0];
154 $output{hostname} = undef;
155 foreach my $hostname (@{$data->{hostname}}) {
156 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
159 # Modified/created time. TODO: maybe add is the name of the creator/modifier
160 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
161 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
163 # Format email addresses
164 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
165 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
167 $output{sshrsahostkey} = undef;
168 foreach $key (@{$data->{sshrsahostkey}}) {
169 $output{sshrsahostkey} .= $key . "<br>";
172 foreach $key (@{$data->{sshrsahostkey}}) {
173 $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
176 my $sponsor = item_uplist($data->{sponsor});
177 $output{sponsor} = $sponsor if defined $sponsor;
178 my $purpose = item_uplist($data->{purpose});
179 $output{purpose} = $purpose if defined $purpose;
181 $selected = " selected ";
184 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n" unless ($data->{status}->[0] =~ /^unlisted/);
186 # collect summary info
187 foreach $key (keys(%summaryattrs)) {
188 if (exists $summarylistitems{$key}) {
189 my $v = item_uplist($data->{$key});
190 $summary{$thishost}{$key} = $v if defined $v;
192 $summary{$thishost}{$key} = $data->{$key}->[0];
196 $summary{$thishost}{hostname} = undef;
197 foreach my $hostname (@{$data->{hostname}}) {
198 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
203 if ($output{havehostdata}) {
204 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
205 $hostdetails .= "<ul>\n";
206 foreach $key (@attrorder) {
208 $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
211 $hostdetails .= "</ul>\n";
213 # display summary info
214 $hostdetails = "<h1>Summary</h1>\n";
215 $hostdetails .= "<table id=\"machines\" class=\"tablesorter\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\">\n<thead>\n<tr>";
216 foreach $key (@summaryorder) {
217 if ($sortby ne $key) {
218 $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
220 if ($sortorder ne "dsc") {
221 $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
223 $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
227 $hostdetails .= "</tr>\n</thead>\n<tbody>\n";
230 if ($sortorder eq "asc") {
231 @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
233 @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
235 foreach $host (@sorted) {
236 next if $summary{$host}{status} =~ /^unlisted/;
237 $hostdetails .= "<tr>";
238 foreach $key (@summaryorder) {
239 $hostdetails .= "<td>$summary{$host}{$key} </td>";
241 $hostdetails .= "</tr>\n";
243 $hostdetails .= "</tbody>\n</table>\n";
246 # Finally, we can write the output... yuck...
247 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
249 s/~hostlist~/$hostlist/;
250 s/~hostdetails~/$hostdetails/;