Show purposes as a bullet list, support [[wikistylelinks]]
[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 purposes_uplist($) {
87         my ($purposes) = @_;
88         my $out = undef;
89
90         if (scalar @$purposes >= 1) {
91                 $out = "<ul>".
92                         join("", map { 
93                                 s#\[\[(.*?)\]\]#<a href="http://$1">$1</a>#g;
94                                 "<li>$_</li>\n";
95                           } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]]//g; $B =~ s/[\[\]]//g; $A cmp $B} @{$data->{purpose}}
96                         ).
97                         "</ul>";
98         }
99         return $out;
100 }
101
102 $SIG{__DIE__} = \&DieHandler;
103
104 my $query = new CGI;
105 my $host = lc($query->param('host'));
106 my $sortby = lc($query->param('sortby')) || "host";
107 my $sortorder = lc($query->param('sortorder')) || "asc";
108
109 &Util::HTMLSendHeader;
110 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
111 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
112 $mesg;
113 $ldap->bind;
114
115 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
116 $mesg->code && &Util::HTMLError($mesg->error);
117 $entries = $mesg->as_struct;
118
119 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
120   $data = $entries->{$dn};
121
122   my $thishost = $data->{host}->[0];
123   $selected = "";
124   
125   if (lc($thishost) eq $host) {
126     $output{havehostdata} = 1;
127
128     foreach $key (keys(%attrs)) {
129       $output{$key} = $data->{$key}->[0];
130     }
131   
132     $output{hostname} = undef;
133     foreach my $hostname (@{$data->{hostname}}) {
134       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
135     }
136
137     # Modified/created time. TODO: maybe add is the name of the creator/modifier
138     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
139     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
140     
141     # Format email addresses
142     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
143     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
144
145     $output{sshrsahostkey} = undef;
146     foreach $key (@{$data->{sshrsahostkey}}) {
147       $output{sshrsahostkey} .= $key . "<br>";
148     }
149
150     foreach $key (@{$data->{sshrsahostkey}}) {
151       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
152     }
153     
154     # URL
155     my ($sponsor, $url) = undef;
156     $output{sponsor} = undef;
157     foreach $sponsor (@{$data->{sponsor}}) {
158       $sponsor =~ m#((http|ftp)://\S+)#i;
159       $url = $1;
160       $sponsor =~ s/\s*$url\s*//;
161       $output{sponsor} .= "<br>" if ($output{sponsor});
162       if ($url) {
163         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
164       } else {
165         $output{sponsor} .= $sponsor;
166       }
167     }
168
169     my $purpose = purposes_uplist($data->{purpose});
170     $output{purpose} = $purpose if defined $purpose;
171
172     $selected = " selected ";    
173   }
174   
175   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
176   
177   # collect summary info
178   foreach $key (keys(%summaryattrs)) {
179     $summary{$thishost}{$key} = $data->{$key}->[0];
180   }
181   
182   $summary{$thishost}{hostname} = undef;
183   foreach my $hostname (@{$data->{hostname}}) {
184     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
185   }
186 }
187 $ldap->unbind;
188
189 if ($output{havehostdata}) {
190   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
191   $hostdetails .= "<ul>\n";
192   foreach $key (@attrorder) {
193     if ($output{$key}) {
194       $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
195     }
196   }
197   $hostdetails .= "</ul>\n";
198 } else {
199   # display summary info
200   $hostdetails = "<h1>Summary</h1>\n";
201   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
202   foreach $key (@summaryorder) {
203     if ($sortby ne $key) {
204       $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
205     } else {
206       if ($sortorder ne "dsc") {
207         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
208       } else {
209         $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
210       }
211     }
212   }
213   $hostdetails .= "</tr>\n";
214   
215   my @sorted;
216   if ($sortorder eq "asc") {
217      @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
218   } else {
219      @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
220   }
221   foreach $host (@sorted) {
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;