display all host keys, remove DSA key support
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.10 2001/12/03 05:02:13 rmurray Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '.';
7 use strict vars;
8 #use Apache::Registry;
9 use CGI;
10 use Util;
11 use Net::LDAP qw(:all);
12
13 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
14
15 # This defines the description of the fields, and which fields are retrieved
16 %attrs = ('hostname' => 'Host name',
17           'admin' => 'Admin contact',
18           'architecture' => 'Architecture',
19           'distribution' => 'Distribution',
20           'access' => 'Access',
21           'sponsor' => 'Sponsor',
22           'sponsor-admin' => 'Sponsor admin',
23           'location' => 'Location',
24           'machine' => 'Processor',
25           'memory' => 'Memory',
26           'disk' => 'Disk space',
27           'bandwidth' => 'Bandwidth',
28           'status' => 'Status',
29           'notes' => 'Notes',
30           'sshrsahostkey' => 'SSH host key',
31           'description' => 'Description',
32           'createtimestamp' => 'Entry created',
33           'modifytimestamp' => 'Entry modified'
34          );
35
36 # This defines what fields are displayed, and in what order
37 @attrorder = qw(hostname admin architecture distribution access
38                 sponsor sponsor-admin location machine memory
39                 disk bandwidth status notes sshrsahostkey
40                 description createtimestamp modifytimestamp);
41
42 # ditto for summary
43 %summaryattrs = ('hostname' => 'Host name',
44                  'host'     => 'just for a link',
45                  'architecture' => 'Architecture',
46                  'distribution' => 'Distribution',
47                  'status' => 'Status',
48                  'access' => 'Access');
49                  
50 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');                
51
52 # Global settings...
53 my %config = &Util::ReadConfigFile;
54
55 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
56 sub DieHandler {
57   $ldap->unbind if (defined($ldap));
58 }
59
60 $SIG{__DIE__} = \&DieHandler;
61
62 my $query = new CGI;
63 my $host = lc($query->param('host'));
64
65 &Util::HTMLSendHeader;
66 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
67 $mesg;
68 $ldap->bind;
69
70 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
71 $mesg->code && &Util::HTMLError($mesg->error);
72 $entries = $mesg->as_struct;
73
74 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
75   $data = $entries->{$dn};
76
77   my $thishost = $data->{host}->[0];
78   $selected = "";
79   
80   if (lc($thishost) eq $host) {
81     $output{havehostdata} = 1;
82
83     foreach $key (keys(%attrs)) {
84       $output{$key} = $data->{$key}->[0];
85     }
86   
87     $output{hostname} = undef;
88     foreach my $hostname (@{$data->{hostname}}) {
89       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
90     }
91
92     # Modified/created time. TODO: maybe add is the name of the creator/modifier
93     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
94     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
95     
96     # Format email addresses
97     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
98     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
99
100     $output{sshrsahostkey} = undef;
101     foreach $key (@{$data->{sshrsahostkey}}) {
102       $output{sshrsahostkey} .= $key . "<br>";
103     }
104     
105     # URL
106     my ($sponsor, $url) = undef;
107     $output{sponsor} = undef;
108     foreach $sponsor (@{$data->{sponsor}}) {
109       $sponsor =~ m#((http|ftp)://\S+)#i;
110       $url = $1;
111       $sponsor =~ s/$url//;
112       $output{sponsor} .= "<br>" if ($output{sponsor});
113       if ($url) {
114         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
115       } else {
116         $output{sponsor} .= $sponsor;
117       }
118     }
119     
120     $selected = " selected ";    
121   }
122   
123   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
124   
125   # collect summary info
126   foreach $key (keys(%summaryattrs)) {
127     $summary{$thishost}{$key} = $data->{$key}->[0];
128   }
129   
130   $summary{$thishost}{hostname} = undef;
131   foreach my $hostname (@{$data->{hostname}}) {
132     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
133   }
134 }
135 $ldap->unbind;
136
137 if ($output{havehostdata}) {
138   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
139   $hostdetails .= "<ul>\n";
140   foreach $key (@attrorder) {
141     if ($output{$key}) {
142       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
143     }
144   }
145   $hostdetails .= "</ul>\n";
146 } else {
147   # display summary info
148   $hostdetails = "<h1>Summary</h1>\n";
149   $hostdetails .= "<table border=1 width=90%>\n<tr>";
150   foreach $key (@summaryorder) {
151     $hostdetails .= "<th>$summaryattrs{$key}</th>";
152   }
153   $hostdetails .= "</tr>\n";
154   
155   foreach $host (sort(keys(%summary))) {
156     $hostdetails .= "<tr>";
157     foreach $key (@summaryorder) {
158       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
159     }
160     $hostdetails .= "</tr>\n";
161   }
162   $hostdetails .= "</table>\n";
163 }
164
165 # Finally, we can write the output... yuck...
166 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
167 while (<F>) {
168   s/~hostlist~/$hostlist/;
169   s/~hostdetails~/$hostdetails/;
170   print;
171 }
172 close F;