Include accountname in totp url
[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 # (c) 2008 Martin Zobel-Helas. Licensed under the GPL. <zobel@debian.org>
8
9 use lib '.';
10 use strict vars;
11 #use Apache::Registry;
12 use CGI;
13 use Util;
14 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
15 use Fcntl;
16 use POSIX;
17 use MIME::Base64;
18 use Digest::MD5 qw(md5_hex);
19
20 my (%attrs, @attrorder, %summaryattrs, @summaryorder, %summarylistitems);
21
22 # This defines the description of the fields, and which fields are retrieved
23 %attrs = ('hostname' => 'Host name',
24           'admin' => 'Admin contact',
25           'architecture' => 'Architecture',
26           'distribution' => 'Distribution',
27           'access' => 'Access',
28           'sponsor' => 'Sponsor',
29           'sponsor-admin' => 'Sponsor admin',
30           'location' => 'Location',
31           'machine' => 'Processor',
32           'memory' => 'Memory',
33           'disk' => 'Disk space',
34           'bandwidth' => 'Bandwidth',
35           'status' => 'Status',
36           'notes' => 'Notes',
37           'sshrsahostkey' => 'SSH host key',
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                  'description' => 'Description',
54                  'architecture' => 'Architecture',
55                  'status' => 'Status',
56                  'access' => 'Access',
57                  'sponsor' => 'Sponsor',
58                  'purpose' => 'Purpose');
59 @summaryorder = qw{hostname description architecture sponsor purpose status access};
60 %summarylistitems = map {$_=>1} qw{purpose sponsor};
61
62 # Global settings...
63 my %config = &Util::ReadConfigFile;
64
65 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
66 sub DieHandler {
67   $ldap->unbind if (defined($ldap));
68 }
69
70 sub wiki_link($) {
71         my ($in) = @_;
72         # [[hostname|text]] makes a link
73         # [[hostname]] makes a link too
74         # if you add a * after [[ it's still the same, only not used for ssh_known_hosts in ud-generate
75         # [[-hostname]] are not links, but get added to known_hosts.  we should drop the [[- ]] tho
76         $in =~ s#\[\[-(.*?)\]\]#$1#g;
77         $in =~ s#\[\[\*?(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
78         $in =~ s#\[\[\*?(.*?)\]\]#<a href="http://$1">$1</a>#g;
79         return $in;
80 }
81
82 # in the purpose field [[host|some other text]] (where some other text is optional)
83 # makes a hyperlink on the web thing. we now also add these hosts to the ssh known_hosts
84 # file.  But so that we don't have to add everything we link we can add an asterisk
85 # and say [[*... to ignore it.  In order to be able to add stuff to ssh without
86 # http linking it we also support [[-hostname]] entries.
87 #
88 # sponsors are also wikified like purpose.  maybe others as well
89 sub item_uplist($) {
90         my ($items) = @_;
91         my $out = undef;
92         my(@tmp) = @$items;
93
94         if (scalar @tmp>= 1) {
95                 $out = "<ul>".
96                         join("", map { 
97                                 "<li>".wiki_link($_)."</li>\n";
98                           } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]\*]//g; $B =~ s/[\[\]\*]//g; $A cmp $B} @tmp
99                         ).
100                         "</ul>";
101         }
102         return $out;
103 }
104
105 #$SIG{__DIE__} = \&DieHandler;
106
107 my $query = new CGI;
108 my $host = lc($query->param('host'));
109 my $sortby = lc($query->param('sortby')) || "host";
110 my $sortorder = lc($query->param('sortorder')) || "asc";
111
112
113 &Util::HTMLSendHeader;
114 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
115 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
116 $mesg;
117 $ldap->bind;
118
119 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
120 $mesg->code && &Util::HTMLError($mesg->error);
121 $entries = $mesg->as_struct;
122
123 foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]} keys(%$entries)) {
124   $data = $entries->{$dn};
125
126   my $thishost = $data->{host}->[0];
127   $selected = "";
128   
129   if (lc($thishost) eq $host) {
130     $output{havehostdata} = 1;
131
132     foreach $key (keys(%attrs)) {
133       $output{$key} = $data->{$key}->[0];
134     }
135   
136     $output{hostname} = undef;
137     foreach my $hostname (@{$data->{hostname}}) {
138       $output{hostname} .= sprintf("%s%s", ($output{hostname} ? ', ' : ''), $hostname);
139     }
140
141     # Modified/created time. TODO: maybe add is the name of the creator/modifier
142     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
143     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
144     
145     # Format email addresses
146     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
147     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
148
149     $output{sshrsahostkey} = undef;
150     foreach $key (@{$data->{sshrsahostkey}}) {
151       $output{sshrsahostkey} .= $key . "<br>";
152     }
153
154     my $sponsor = item_uplist($data->{sponsor});
155     $output{sponsor} = $sponsor if defined $sponsor;
156     my $purpose = item_uplist($data->{purpose});
157     $output{purpose} = $purpose if defined $purpose;
158
159     $selected = " selected ";
160   }
161
162   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n" unless ($data->{status}->[0] =~ /^unlisted/);
163
164   # collect summary info
165   foreach $key (keys(%summaryattrs)) {
166     if (exists $summarylistitems{$key}) {
167       my $v = item_uplist($data->{$key});
168       $summary{$thishost}{$key} = $v if defined $v;
169     } else {
170       $summary{$thishost}{$key} = $data->{$key}->[0];
171     }
172   }
173
174   $summary{$thishost}{hostname} = undef;
175   foreach my $hostname (@{$data->{hostname}}) {
176     $summary{$thishost}{hostname} .= sprintf("%s<a href=\"machines.cgi?host=%s\">%s</a>", ($summary{$thishost}{hostname} ? '<br>' : ''), $summary{$thishost}{host}, $hostname);
177   }
178 }
179 $ldap->unbind;
180
181 if ($output{havehostdata}) {
182   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
183   $hostdetails .= "<ul>\n";
184   foreach $key (@attrorder) {
185     if ($output{$key}) {
186       $hostdetails .= "<li><b>$attrs{$key}</b>: $output{$key}\n";
187     }
188   }
189   $hostdetails .= "</ul>\n";
190 } else {
191   # display summary info
192   $hostdetails = "<h1>Summary</h1>\n";
193   $hostdetails .= "<table id=\"machines\" class=\"tablesorter\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\">\n<thead>\n<tr>";
194   foreach $key (@summaryorder) {
195     if ($sortby ne $key) {
196       $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
197     } else {
198       if ($sortorder ne "dsc") {
199         $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
200       } else {
201         $hostdetails .= "<th><a class=\"sort\" href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
202       }
203     }
204   }
205   $hostdetails .= "</tr>\n</thead>\n<tbody>\n";
206   
207   my @sorted;
208   if ($sortorder eq "asc") {
209      @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
210   } else {
211      @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
212   }
213   foreach $host (@sorted) {
214     next if $summary{$host}{status} =~ /^unlisted/;
215     $hostdetails .= "<tr>";
216     foreach $key (@summaryorder) {
217       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
218     }
219     $hostdetails .= "</tr>\n";
220   }
221   $hostdetails .= "</tbody>\n</table>\n";
222 }
223
224 # Finally, we can write the output... yuck...
225 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
226 while (<F>) {
227   s/~hostlist~/$hostlist/;
228   s/~hostdetails~/$hostdetails/;
229   print;
230 }
231 close F;