Make machines.cgi display the purpose attribute from LDAP
[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
8 use lib '.';
9 use strict vars;
10 #use Apache::Registry;
11 use CGI;
12 use Util;
13 use Net::LDAP qw(:all);
14 use Fcntl;
15 use POSIX;
16 use MIME::Base64;
17 use Digest::MD5 qw(md5_hex);
18
19 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
20
21 # This defines the description of the fields, and which fields are retrieved
22 %attrs = ('hostname' => 'Host name',
23           'admin' => 'Admin contact',
24           'architecture' => 'Architecture',
25           'distribution' => 'Distribution',
26           'access' => 'Access',
27           'sponsor' => 'Sponsor',
28           'sponsor-admin' => 'Sponsor admin',
29           'location' => 'Location',
30           'machine' => 'Processor',
31           'memory' => 'Memory',
32           'disk' => 'Disk space',
33           'bandwidth' => 'Bandwidth',
34           'status' => 'Status',
35           'notes' => 'Notes',
36           'sshrsahostkey' => 'SSH host key',
37           'sshrsahostfprint' => 'SSH host fingerprint',
38           'description' => 'Description',
39           'purpose' => 'purposes of this server',
40 #         'createtimestamp' => 'Entry created',
41 #         'modifytimestamp' => 'Entry modified'
42          );
43
44 # This defines what fields are displayed, and in what order
45 @attrorder = qw(hostname admin architecture distribution access
46                 sponsor sponsor-admin location machine memory
47                 disk bandwidth status notes sshrsahostkey sshrsahostfprint
48                 description purpose);
49
50 # ditto for summary
51 %summaryattrs = ('hostname' => 'Host name',
52                  'host'     => 'just for a link',
53                  'architecture' => 'Architecture',
54                  'distribution' => 'Distribution',
55                  'status' => 'Status',
56                  'access' => 'Access');
57                  
58 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
59
60 # Global settings...
61 my %config = &Util::ReadConfigFile;
62
63 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
64 sub DieHandler {
65   $ldap->unbind if (defined($ldap));
66 }
67
68 # human readable fingerprint
69 sub sshfingerprint {
70     my $key = shift;
71
72     return '' if (!$key);
73
74     my @field = split(/ /, $key);
75     return '' if $field[0] ne 'ssh-dss' and $field[0] ne 'ssh-rsa';
76     return '' if !$field[1];
77     my $fpr = md5_hex(decode_base64($field[1]));
78     my $hrfpr = $field[0] . " " . substr($fpr,0,2,"");
79     while (length $fpr > 0) {
80        $hrfpr .= ':' . substr($fpr,0,2,"");
81     }
82     return $hrfpr;
83 }
84
85 $SIG{__DIE__} = \&DieHandler;
86
87 my $query = new CGI;
88 my $host = lc($query->param('host'));
89
90 &Util::HTMLSendHeader;
91 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
92 $mesg;
93 $ldap->bind;
94
95 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
96 $mesg->code && &Util::HTMLError($mesg->error);
97 $entries = $mesg->as_struct;
98
99 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
100   $data = $entries->{$dn};
101
102   my $thishost = $data->{host}->[0];
103   $selected = "";
104   
105   if (lc($thishost) eq $host) {
106     $output{havehostdata} = 1;
107
108     foreach $key (keys(%attrs)) {
109       $output{$key} = $data->{$key}->[0];
110     }
111   
112     $output{hostname} = undef;
113     foreach my $hostname (@{$data->{hostname}}) {
114       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
115     }
116
117     # Modified/created time. TODO: maybe add is the name of the creator/modifier
118     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
119     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
120     
121     # Format email addresses
122     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
123     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
124
125     $output{sshrsahostkey} = undef;
126     foreach $key (@{$data->{sshrsahostkey}}) {
127       $output{sshrsahostkey} .= $key . "<br>";
128     }
129
130     foreach $key (@{$data->{sshrsahostkey}}) {
131       $output{sshrsahostfprint} .= sshfingerprint($key) . "<br>";
132     }
133     
134     # URL
135     my ($sponsor, $url) = undef;
136     $output{sponsor} = undef;
137     foreach $sponsor (@{$data->{sponsor}}) {
138       $sponsor =~ m#((http|ftp)://\S+)#i;
139       $url = $1;
140       $sponsor =~ s/\s*$url\s*//;
141       $output{sponsor} .= "<br>" if ($output{sponsor});
142       if ($url) {
143         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
144       } else {
145         $output{sponsor} .= $sponsor;
146       }
147     }
148
149         #Reformat purposes to be pleasing for the human eye:
150         $output{purpose} = join(",", @{$data->{purpose}});
151     
152     $selected = " selected ";    
153   }
154   
155   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
156   
157   # collect summary info
158   foreach $key (keys(%summaryattrs)) {
159     $summary{$thishost}{$key} = $data->{$key}->[0];
160   }
161   
162   $summary{$thishost}{hostname} = undef;
163   foreach my $hostname (@{$data->{hostname}}) {
164     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
165   }
166 }
167 $ldap->unbind;
168
169 if ($output{havehostdata}) {
170   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
171   $hostdetails .= "<ul>\n";
172   foreach $key (@attrorder) {
173     if ($output{$key}) {
174       $hostdetails .= "<li><b>$attrs{$key}:</b>$output{$key}\n";
175     }
176   }
177   $hostdetails .= "</ul>\n";
178 } else {
179   # display summary info
180   $hostdetails = "<h1>Summary</h1>\n";
181   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
182   foreach $key (@summaryorder) {
183     $hostdetails .= "<th>$summaryattrs{$key}</th>";
184   }
185   $hostdetails .= "</tr>\n";
186   
187   foreach $host (sort(keys(%summary))) {
188     $hostdetails .= "<tr>";
189     foreach $key (@summaryorder) {
190       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
191     }
192     $hostdetails .= "</tr>\n";
193   }
194   $hostdetails .= "</table>\n";
195 }
196
197 # Finally, we can write the output... yuck...
198 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
199 while (<F>) {
200   s/~hostlist~/$hostlist/;
201   s/~hostdetails~/$hostdetails/;
202   print;
203 }
204 close F;