Also support [[link|wiki links with alternate link text]].
[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);
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                  'distribution' => 'Distribution',
56                  'status' => 'Status',
57                  'access' => 'Access');
58                  
59 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
60
61 # Global settings...
62 my %config = &Util::ReadConfigFile;
63
64 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
65 sub DieHandler {
66   $ldap->unbind if (defined($ldap));
67 }
68
69 # human readable fingerprint
70 sub sshfingerprint {
71     my $key = shift;
72
73     return '' if (!$key);
74
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,"");
82     }
83     return $hrfpr;
84 }
85
86 sub wiki_link($) {
87         my ($in) = @_;
88         $in =~ s#\[\[(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
89         $in =~ s#\[\[(.*?)\]\]#<a href="http://$1">$1</a>#g;
90         return $in;
91 }
92
93 sub purposes_uplist($) {
94         my ($purposes) = @_;
95         my $out = undef;
96
97         if (scalar @$purposes >= 1) {
98                 $out = "<ul>".
99                         join("", map { 
100                                 "<li>".wiki_link($_)."</li>\n";
101                           } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]]//g; $B =~ s/[\[\]]//g; $A cmp $B} @{$data->{purpose}}
102                         ).
103                         "</ul>";
104         }
105         return $out;
106 }
107
108 $SIG{__DIE__} = \&DieHandler;
109
110 my $query = new CGI;
111 my $host = lc($query->param('host'));
112 my $sortby = lc($query->param('sortby')) || "host";
113 my $sortorder = lc($query->param('sortorder')) || "asc";
114
115 &Util::HTMLSendHeader;
116 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
117 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
118 $mesg;
119 $ldap->bind;
120
121 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
122 $mesg->code && &Util::HTMLError($mesg->error);
123 $entries = $mesg->as_struct;
124
125 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
126   $data = $entries->{$dn};
127
128   my $thishost = $data->{host}->[0];
129   $selected = "";
130   
131   if (lc($thishost) eq $host) {
132     $output{havehostdata} = 1;
133
134     foreach $key (keys(%attrs)) {
135       $output{$key} = $data->{$key}->[0];
136     }
137   
138     $output{hostname} = undef;
139     foreach my $hostname (@{$data->{hostname}}) {
140       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
141     }
142
143     # Modified/created time. TODO: maybe add is the name of the creator/modifier
144     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
145     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
146     
147     # Format email addresses
148     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
149     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
150
151     $output{sshrsahostkey} = undef;
152     foreach $key (@{$data->{sshrsahostkey}}) {
153       $output{sshrsahostkey} .= $key . "<br>";
154     }
155
156     foreach $key (@{$data->{sshrsahostkey}}) {
157       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
158     }
159     
160     # URL
161     my ($sponsor, $url) = undef;
162     $output{sponsor} = undef;
163     foreach $sponsor (@{$data->{sponsor}}) {
164       $sponsor =~ m#((http|ftp)://\S+)#i;
165       $url = $1;
166       $sponsor =~ s/\s*$url\s*//;
167       $output{sponsor} .= "<br>" if ($output{sponsor});
168       if ($url) {
169         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
170       } else {
171         $output{sponsor} .= $sponsor;
172       }
173     }
174
175     my $purpose = purposes_uplist($data->{purpose});
176     $output{purpose} = $purpose if defined $purpose;
177
178     $selected = " selected ";    
179   }
180   
181   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
182   
183   # collect summary info
184   foreach $key (keys(%summaryattrs)) {
185     $summary{$thishost}{$key} = $data->{$key}->[0];
186   }
187   
188   $summary{$thishost}{hostname} = undef;
189   foreach my $hostname (@{$data->{hostname}}) {
190     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
191   }
192 }
193 $ldap->unbind;
194
195 if ($output{havehostdata}) {
196   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
197   $hostdetails .= "<ul>\n";
198   foreach $key (@attrorder) {
199     if ($output{$key}) {
200       $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
201     }
202   }
203   $hostdetails .= "</ul>\n";
204 } else {
205   # display summary info
206   $hostdetails = "<h1>Summary</h1>\n";
207   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
208   foreach $key (@summaryorder) {
209     if ($sortby ne $key) {
210       $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
211     } else {
212       if ($sortorder ne "dsc") {
213         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
214       } else {
215         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
216       }
217     }
218   }
219   $hostdetails .= "</tr>\n";
220   
221   my @sorted;
222   if ($sortorder eq "asc") {
223      @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
224   } else {
225      @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
226   }
227   foreach $host (@sorted) {
228     $hostdetails .= "<tr>";
229     foreach $key (@summaryorder) {
230       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
231     }
232     $hostdetails .= "</tr>\n";
233   }
234   $hostdetails .= "</table>\n";
235 }
236
237 # Finally, we can write the output... yuck...
238 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
239 while (<F>) {
240   s/~hostlist~/$hostlist/;
241   s/~hostdetails~/$hostdetails/;
242   print;
243 }
244 close F;