Hide hosts on website whose status starts with unlisted.
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.12 2006/12/27 23:00:04 rmurray 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 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
7 # (c) 2008 Martin Zobel-Helas. Licensed under the GPL. <zobel@debian.org>
8
9 use lib '.';
10 use strict vars;
11 #use Apache::Registry;
12 use CGI;
13 use Util;
14 use Net::LDAP qw(:all);
15 use Fcntl;
16 use POSIX;
17 use MIME::Base64;
18 use Digest::MD5 qw(md5_hex);
19
20 my (%attrs, @attrorder, %summaryattrs, @summaryorder, %summarylistitems);
21
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',
27           'access' => 'Access',
28           'sponsor' => 'Sponsor',
29           'sponsor-admin' => 'Sponsor admin',
30           'location' => 'Location',
31           'machine' => 'Processor',
32           'memory' => 'Memory',
33           'disk' => 'Disk space',
34           'bandwidth' => 'Bandwidth',
35           'status' => 'Status',
36           'notes' => 'Notes',
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'
43          );
44
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
49                 description purpose);
50
51 # ditto for summary
52 %summaryattrs = ('hostname' => 'Host name',
53                  'host'     => 'just for a link',
54                  'architecture' => 'Architecture',
55                  'status' => 'Status',
56                  'access' => 'Access',
57                  'sponsor' => 'Sponsor',
58                  'purpose' => 'Purpose');
59 @summaryorder = qw{hostname architecture sponsor purpose status access};
60 %summarylistitems = map {$_=>1} qw{purpose sponsor};
61
62 # Global settings...
63 my %config = &Util::ReadConfigFile;
64
65 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
66 sub DieHandler {
67   $ldap->unbind if (defined($ldap));
68 }
69
70 # human readable fingerprint
71 sub sshfingerprint {
72     my $key = shift;
73
74     return '' if (!$key);
75
76     my @field = split(/ /, $key);
77     return '' if $field[0] ne 'ssh-dss' and $field[0] ne 'ssh-rsa';
78     return '' if !$field[1];
79     my $fpr = md5_hex(decode_base64($field[1]));
80     my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
81     while (length $fpr > 0) {
82        $hrfpr .= ':' . substr($fpr,0,2,"");
83     }
84     return $hrfpr;
85 }
86
87 sub wiki_link($) {
88         my ($in) = @_;
89         $in =~ s#\[\[(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
90         $in =~ s#\[\[(.*?)\]\]#<a href="http://$1">$1</a>#g;
91         return $in;
92 }
93
94 sub item_uplist($) {
95         my ($items) = @_;
96         my $out = undef;
97
98         if (scalar @$items >= 1) {
99                 $out = "<ul>".
100                         join("", map { 
101                                 "<li>".wiki_link($_)."</li>\n";
102                           } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]]//g; $B =~ s/[\[\]]//g; $A cmp $B} @{$items}
103                         ).
104                         "</ul>";
105         }
106         return $out;
107 }
108
109 $SIG{__DIE__} = \&DieHandler;
110
111 my $query = new CGI;
112 my $host = lc($query->param('host'));
113 my $sortby = lc($query->param('sortby')) || "host";
114 my $sortorder = lc($query->param('sortorder')) || "asc";
115
116 &Util::HTMLSendHeader;
117 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
118 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
119 $mesg;
120 $ldap->bind;
121
122 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
123 $mesg->code && &Util::HTMLError($mesg->error);
124 $entries = $mesg->as_struct;
125
126 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
127   $data = $entries->{$dn};
128
129   my $thishost = $data->{host}->[0];
130   $selected = "";
131   
132   if (lc($thishost) eq $host) {
133     $output{havehostdata} = 1;
134
135     foreach $key (keys(%attrs)) {
136       $output{$key} = $data->{$key}->[0];
137     }
138   
139     $output{hostname} = undef;
140     foreach my $hostname (@{$data->{hostname}}) {
141       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
142     }
143
144     # Modified/created time. TODO: maybe add is the name of the creator/modifier
145     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
146     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
147     
148     # Format email addresses
149     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
150     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
151
152     $output{sshrsahostkey} = undef;
153     foreach $key (@{$data->{sshrsahostkey}}) {
154       $output{sshrsahostkey} .= $key . "<br>";
155     }
156
157     foreach $key (@{$data->{sshrsahostkey}}) {
158       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
159     }
160     
161     my $sponsor = item_uplist($data->{sponsor});
162     $output{sponsor} = $sponsor if defined $sponsor;
163     my $purpose = item_uplist($data->{purpose});
164     $output{purpose} = $purpose if defined $purpose;
165
166     $selected = " selected ";
167   }
168
169   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n" unless ($data->{status}->[0] =~ /^unlisted/);
170
171   # collect summary info
172   foreach $key (keys(%summaryattrs)) {
173     if (exists $summarylistitems{$key}) {
174       my $v = item_uplist($data->{$key});
175       $summary{$thishost}{$key} = $v if defined $v;
176     } else {
177       $summary{$thishost}{$key} = $data->{$key}->[0];
178     }
179   }
180
181   $summary{$thishost}{hostname} = undef;
182   foreach my $hostname (@{$data->{hostname}}) {
183     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
184   }
185 }
186 $ldap->unbind;
187
188 if ($output{havehostdata}) {
189   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
190   $hostdetails .= "<ul>\n";
191   foreach $key (@attrorder) {
192     if ($output{$key}) {
193       $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
194     }
195   }
196   $hostdetails .= "</ul>\n";
197 } else {
198   # display summary info
199   $hostdetails = "<h1>Summary</h1>\n";
200   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
201   foreach $key (@summaryorder) {
202     if ($sortby ne $key) {
203       $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
204     } else {
205       if ($sortorder ne "dsc") {
206         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
207       } else {
208         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
209       }
210     }
211   }
212   $hostdetails .= "</tr>\n";
213   
214   my @sorted;
215   if ($sortorder eq "asc") {
216      @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
217   } else {
218      @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
219   }
220   foreach $host (@sorted) {
221     next if $summary{$host}{status} =~ /^unlisted/;
222     $hostdetails .= "<tr>";
223     foreach $key (@summaryorder) {
224       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
225     }
226     $hostdetails .= "</tr>\n";
227   }
228   $hostdetails .= "</table>\n";
229 }
230
231 # Finally, we can write the output... yuck...
232 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
233 while (<F>) {
234   s/~hostlist~/$hostlist/;
235   s/~hostdetails~/$hostdetails/;
236   print;
237 }
238 close F;