2 # $Id: machines.cgi,v 1.11 2004/11/18 14:27:46 joey Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5 # (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
12 use Net::LDAP qw(:all);
16 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
18 # This defines the description of the fields, and which fields are retrieved
19 %attrs = ('hostname' => 'Host name',
20 'admin' => 'Admin contact',
21 'architecture' => 'Architecture',
22 'distribution' => 'Distribution',
24 'sponsor' => 'Sponsor',
25 'sponsor-admin' => 'Sponsor admin',
26 'location' => 'Location',
27 'machine' => 'Processor',
29 'disk' => 'Disk space',
30 'bandwidth' => 'Bandwidth',
33 'sshrsahostkey' => 'SSH host key',
34 'sshrsahostfprint' => 'SSH host fingerprint',
35 'description' => 'Description',
36 'createtimestamp' => 'Entry created',
37 'modifytimestamp' => 'Entry modified'
40 # This defines what fields are displayed, and in what order
41 @attrorder = qw(hostname admin architecture distribution access
42 sponsor sponsor-admin location machine memory
43 disk bandwidth status notes sshrsahostkey sshrsahostfprint
44 description createtimestamp modifytimestamp);
47 %summaryattrs = ('hostname' => 'Host name',
48 'host' => 'just for a link',
49 'architecture' => 'Architecture',
50 'distribution' => 'Distribution',
52 'access' => 'Access');
54 @summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');
57 my %config = &Util::ReadConfigFile;
59 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
61 $ldap->unbind if (defined($ldap));
69 return '' if (!$keys);
72 } until sysopen(FH, $fn, O_RDWR|O_CREAT|O_EXCL, 0600);
74 foreach my $key (split(/<br>/, $keys)) {
75 printf FH "foo %s\n", $key;
79 if (open (S, "ssh-keygen -l -f $fn|")) {
80 $res = join("\n", <S>);
88 $SIG{__DIE__} = \&DieHandler;
91 my $host = lc($query->param('host'));
93 &Util::HTMLSendHeader;
94 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
98 $mesg = $ldap->search(base => $config{hostbasedn}, filter => 'host=*');
99 $mesg->code && &Util::HTMLError($mesg->error);
100 $entries = $mesg->as_struct;
102 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
103 $data = $entries->{$dn};
105 my $thishost = $data->{host}->[0];
108 if (lc($thishost) eq $host) {
109 $output{havehostdata} = 1;
111 foreach $key (keys(%attrs)) {
112 $output{$key} = $data->{$key}->[0];
115 $output{hostname} = undef;
116 foreach my $hostname (@{$data->{hostname}}) {
117 $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
120 # Modified/created time. TODO: maybe add is the name of the creator/modifier
121 $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
122 $output{createtimestamp} = &Util::FormatTimestamp($output{createtimestamp});
124 # Format email addresses
125 $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
126 $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
128 $output{sshrsahostkey} = undef;
129 foreach $key (@{$data->{sshrsahostkey}}) {
130 $output{sshrsahostkey} .= $key . "<br>";
133 $output{sshrsahostfprint} = sshfingerprint($output{sshrsahostkey});
136 my ($sponsor, $url) = undef;
137 $output{sponsor} = undef;
138 foreach $sponsor (@{$data->{sponsor}}) {
139 $sponsor =~ m#((http|ftp)://\S+)#i;
141 $sponsor =~ s/$url//;
142 $output{sponsor} .= "<br>" if ($output{sponsor});
144 $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
146 $output{sponsor} .= $sponsor;
150 $selected = " selected ";
153 $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
155 # collect summary info
156 foreach $key (keys(%summaryattrs)) {
157 $summary{$thishost}{$key} = $data->{$key}->[0];
160 $summary{$thishost}{hostname} = undef;
161 foreach my $hostname (@{$data->{hostname}}) {
162 $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
167 if ($output{havehostdata}) {
168 $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
169 $hostdetails .= "<ul>\n";
170 foreach $key (@attrorder) {
172 $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
175 $hostdetails .= "</ul>\n";
177 # display summary info
178 $hostdetails = "<h1>Summary</h1>\n";
179 $hostdetails .= "<table border=1 width=90%>\n<tr>";
180 foreach $key (@summaryorder) {
181 $hostdetails .= "<th>$summaryattrs{$key}</th>";
183 $hostdetails .= "</tr>\n";
185 foreach $host (sort(keys(%summary))) {
186 $hostdetails .= "<tr>";
187 foreach $key (@summaryorder) {
188 $hostdetails .= "<td>$summary{$host}{$key} </td>";
190 $hostdetails .= "</tr>\n";
192 $hostdetails .= "</table>\n";
195 # Finally, we can write the output... yuck...
196 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
198 s/~hostlist~/$hostlist/;
199 s/~hostdetails~/$hostdetails/;