Update packaging based on lintian output
[mirror/userdir-ldap-cgi.git] / machines.cgi
index 1b2c022..de4d30f 100755 (executable)
@@ -1,7 +1,8 @@
 #!/usr/bin/perl
-# $Id: machines.cgi,v 1.3 1999/09/30 06:21:28 tausq Exp $
+# $Id: machines.cgi,v 1.11 2004/11/18 14:27:46 joey Exp $
 
 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
+# (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
 
 use lib '.';
 use strict vars;
@@ -9,6 +10,8 @@ use strict vars;
 use CGI;
 use Util;
 use Net::LDAP qw(:all);
+use Fcntl;
+use POSIX;
 
 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
 
@@ -25,23 +28,30 @@ my (%attrs, @attrorder, %summaryattrs, @summaryorder);
          'memory' => 'Memory',
          'disk' => 'Disk space',
          'bandwidth' => 'Bandwidth',
+         'status' => 'Status',
          'notes' => 'Notes',
+         'sshrsahostkey' => 'SSH host key',
+         'sshrsahostfprint' => 'SSH host fingerprint',
+         'description' => 'Description',
          'createtimestamp' => 'Entry created',
          'modifytimestamp' => 'Entry modified'
         );
 
 # This defines what fields are displayed, and in what order
-@attrorder = ('hostname', 'admin', 'architecture', 'distribution', 'access',
-              'sponsor', 'sponsor-admin', 'location', 'machine', 'memory',
-             'disk', 'bandwidth', 'notes', 'createtimestamp', 'modifytimestamp');
+@attrorder = qw(hostname admin architecture distribution access
+                sponsor sponsor-admin location machine memory
+               disk bandwidth status notes sshrsahostkey sshrsahostfprint
+               description createtimestamp modifytimestamp);
 
 # ditto for summary
 %summaryattrs = ('hostname' => 'Host name',
                  'host'     => 'just for a link',
                  'architecture' => 'Architecture',
+                'distribution' => 'Distribution',
+                'status' => 'Status',
                 'access' => 'Access');
                 
-@summaryorder = ('hostname', 'architecture', 'access');                 
+@summaryorder = ('hostname', 'architecture', 'distribution', 'status', 'access');               
 
 # Global settings...
 my %config = &Util::ReadConfigFile;
@@ -51,6 +61,30 @@ sub DieHandler {
   $ldap->unbind if (defined($ldap));
 }
 
+sub sshfingerprint {
+    my $keys = shift;
+    my $res = '';
+    my $fn = '';
+
+    return '' if (!$keys);
+    do {
+       $fn = tmpnam();
+    } until sysopen(FH, $fn, O_RDWR|O_CREAT|O_EXCL, 0600);
+
+    foreach my $key (split(/<br>/, $keys)) {
+       printf FH "foo %s\n", $key;
+    }
+    close (FH);
+
+    if (open (S, "ssh-keygen -l -f $fn|")) {
+       $res = join("\n", <S>);
+       close (S);
+    }
+    $res =~ s/\n/<br>/g;
+    unlink ($fn);
+    return $res;
+}
+
 $SIG{__DIE__} = \&DieHandler;
 
 my $query = new CGI;
@@ -65,7 +99,7 @@ $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
 $mesg->code && &Util::HTMLError($mesg->error);
 $entries = $mesg->as_struct;
 
-foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]} keys(%$entries)) {
+foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
   $data = $entries->{$dn};
 
   my $thishost = $data->{host}->[0];
@@ -77,6 +111,11 @@ foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]}
     foreach $key (keys(%attrs)) {
       $output{$key} = $data->{$key}->[0];
     }
+  
+    $output{hostname} = undef;
+    foreach my $hostname (@{$data->{hostname}}) {
+      $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
+    }
 
     # Modified/created time. TODO: maybe add is the name of the creator/modifier
     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
@@ -85,18 +124,26 @@ foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]}
     # Format email addresses
     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
+
+    $output{sshrsahostkey} = undef;
+    foreach $key (@{$data->{sshrsahostkey}}) {
+      $output{sshrsahostkey} .= $key . "<br>";
+    }
+
+    $output{sshrsahostfprint} = sshfingerprint($output{sshrsahostkey});
     
     # URL
-    my $sponsor;
+    my ($sponsor, $url) = undef;
     $output{sponsor} = undef;
     foreach $sponsor (@{$data->{sponsor}}) {
-      print "<!-- $sponsor -->\n";
-      $sponsor =~ /(.*)\s*(http.*)?/i;
+      $sponsor =~ m#((http|ftp)://\S+)#i;
+      $url = $1;
+      $sponsor =~ s/$url//;
       $output{sponsor} .= "<br>" if ($output{sponsor});
-      if ($2) {
-        $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $1, $2);
+      if ($url) {
+        $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
       } else {
-        $output{sponsor} .= $1;
+        $output{sponsor} .= $sponsor;
       }
     }
     
@@ -110,8 +157,10 @@ foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]}
     $summary{$thishost}{$key} = $data->{$key}->[0];
   }
   
-  $summary{$thishost}{hostname} = sprintf("<a href=\"machines.cgi?host=%s\">%s</a>",
-                                          $summary{$thishost}{host}, $summary{$thishost}{hostname});
+  $summary{$thishost}{hostname} = undef;
+  foreach my $hostname (@{$data->{hostname}}) {
+    $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
+  }
 }
 $ldap->unbind;
 
@@ -127,11 +176,11 @@ if ($output{havehostdata}) {
 } else {
   # display summary info
   $hostdetails = "<h1>Summary</h1>\n";
-  $hostdetails .= "<table border=1 width=90%>\n<th>";
+  $hostdetails .= "<table border=1 width=90%>\n<tr>";
   foreach $key (@summaryorder) {
-    $hostdetails .= "<td>$summaryattrs{$key}</td>";
+    $hostdetails .= "<th>$summaryattrs{$key}</th>";
   }
-  $hostdetails .= "</th>\n";
+  $hostdetails .= "</tr>\n";
   
   foreach $host (sort(keys(%summary))) {
     $hostdetails .= "<tr>";