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(:all);
18 use Digest::MD5 qw(md5_hex);
20 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
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',
55 'distribution' => 'Distribution',
57 'access' => 'Access');
59 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
62 my %config = &Util::ReadConfigFile;
64 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
66 $ldap->unbind if (defined($ldap));
69 # human readable fingerprint
75 my @field = split(/ /, $key);
76 return '' if $field[0] ne 'ssh-dss' and $field[0] ne 'ssh-rsa';
77 return '' if !$field[1];
78 my $fpr = md5_hex(decode_base64($field[1]));
79 my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
80 while (length $fpr > 0) {
81 $hrfpr .= ':' . substr($fpr,0,2,"");
86 $SIG{__DIE__} = \&DieHandler;
89 my $host = lc($query->param('host'));
90 my $sortby = lc($query->param('sortby')) || "host";
91 my $sortorder = lc($query->param('sortorder')) || "asc";
93 &Util::HTMLSendHeader;
94 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
95 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
99 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
100 $mesg->code && &Util::HTMLError($mesg->error);
101 $entries = $mesg->as_struct;
103 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
104 $data = $entries->{$dn};
106 my $thishost = $data->{host}->[0];
109 if (lc($thishost) eq $host) {
110 $output{havehostdata} = 1;
112 foreach $key (keys(%attrs)) {
113 $output{$key} = $data->{$key}->[0];
116 $output{hostname} = undef;
117 foreach my $hostname (@{$data->{hostname}}) {
118 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
121 # Modified/created time. TODO: maybe add is the name of the creator/modifier
122 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
123 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
125 # Format email addresses
126 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
127 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
129 $output{sshrsahostkey} = undef;
130 foreach $key (@{$data->{sshrsahostkey}}) {
131 $output{sshrsahostkey} .= $key . "<br>";
134 foreach $key (@{$data->{sshrsahostkey}}) {
135 $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
139 my ($sponsor, $url) = undef;
140 $output{sponsor} = undef;
141 foreach $sponsor (@{$data->{sponsor}}) {
142 $sponsor =~ m#((http|ftp)://\S+)#i;
144 $sponsor =~ s/\s*$url\s*//;
145 $output{sponsor} .= "<br>" if ($output{sponsor});
147 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
149 $output{sponsor} .= $sponsor;
153 #Reformat purposes to be pleasing for the human eye:
154 $output{purpose} = join(",", @{$data->{purpose}});
156 $selected = " selected ";
159 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
161 # collect summary info
162 foreach $key (keys(%summaryattrs)) {
163 $summary{$thishost}{$key} = $data->{$key}->[0];
166 $summary{$thishost}{hostname} = undef;
167 foreach my $hostname (@{$data->{hostname}}) {
168 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
173 if ($output{havehostdata}) {
174 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
175 $hostdetails .= "<ul>\n";
176 foreach $key (@attrorder) {
178 $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
181 $hostdetails .= "</ul>\n";
183 # display summary info
184 $hostdetails = "<h1>Summary</h1>\n";
185 $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
186 foreach $key (@summaryorder) {
187 if ($sortby ne $key) {
188 $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
190 if ($sortorder ne "dsc") {
191 $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
193 $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
197 $hostdetails .= "</tr>\n";
200 if ($sortorder eq "asc") {
201 @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
203 @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
205 foreach $host (@sorted) {
206 $hostdetails .= "<tr>";
207 foreach $key (@summaryorder) {
208 $hostdetails .= "<td>$summary{$host}{$key} </td>";
210 $hostdetails .= "</tr>\n";
212 $hostdetails .= "</table>\n";
215 # Finally, we can write the output... yuck...
216 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
218 s/~hostlist~/$hostlist/;
219 s/~hostdetails~/$hostdetails/;