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