3 # $Id: search.cgi,v 1.6 2000/03/26 22:13:25 tausq Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
12 use Net::LDAP qw(:all);
15 my %config = &Util::ReadConfigFile;
18 my $id = $query->param('id');
19 my $authtoken = $query->param('authtoken');
20 my $password = &Util::CheckAuthToken($authtoken);
21 my $dosearch = $query->param('dosearch');
22 my $searchdn = $query->param('searchdn');
25 my $proto = ($ENV{HTTPS} ? "https" : "http");
28 $ldap->unbind if (defined($ldap));
31 $SIG{__DIE__} = \&DieHandler;
34 # No action yet, send back the search form...
35 print "Content-type: text/html\n\n";
36 open (F, "<$config{websearchhtml}") || &Util::HTMLError($!);
39 s/~authtoken~/$authtoken/g;
44 # Go ahead and construct the search terms
46 cn => { fuzzy => 'cnfuzzy', formname => 'cn' }, # First name
47 mn => { fuzzy => 'mnfuzzy', formname => 'mn' }, # Middle name
48 sn => { fuzzy => 'snfuzzy', formname => 'sn' }, # Last name
49 email => { fuzzy => 'emailfuzzy', formname => 'email' }, # email
50 uid => { fuzzy => 'uidfuzzy', formname => 'uid' }, # Login name
51 ircnick => { fuzzy => 'ircfuzzy', formname => 'ircnick' }, # IRC nickname
52 keyfingerprint => { fuzzy => 'fpfuzzy', formname => 'fingerprint' }, # PGP/GPG fingerprint
53 c => { formname => 'country'}, # Country
56 # Do a little preprocessing - strip the spaces out of the fingerprint
57 my $temp = $query->param('fingerprint');
58 $temp =~ s/ //g; $query->param('fingerprint', $temp);
60 # go through %searchdata and pull out all the search criteria the user
63 foreach (keys(%searchdata)) {
64 if ($query->param($searchdata{$_}{formname})) {
65 if ($query->param($searchdata{$_}{fuzzy})) {
67 $filter .= "($_~=".$query->param($searchdata{$_}{formname}).")";
69 $filter .= "($_=".$query->param($searchdata{$_}{formname}).")";
74 # Vacation is a special case
75 $filter .= "(onvacation=*)" if ($query->param('vacation'));
77 # AND all the search terms together
78 $filter = "(&$filter)";
80 # Read in the result template...
81 my ($lineref, $dataspecref) = ParseResult($config{websearchresulthtml});
83 # Now, we are ready to connect to the LDAP server.
84 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
88 if ($id && $password) {
89 $mesg = $ldap->bind("uid=$id,$config{basedn}", password => $password);
91 $auth = ($mesg->code == LDAP_SUCCESS);
94 if (!$auth) { # Not authenticated - either the above failed, or no password supplied
98 # &Util::HTMLPrint("Searching in $config{basedn} for $filter...\n");
100 $mesg = $ldap->search(base => ($searchdn ? $searchdn : $config{basedn}),
101 filter => ($searchdn ? "(uid=*)" : $filter));
102 $mesg->code && &Util::HTMLError($mesg->error);
104 my %outsub; # this hash will contain all the substitution tokens in the output
105 $outsub{count} = $mesg->count; # Count number of requests, also ensures we're done with the search
106 $outsub{auth} = $authtoken;
107 $outsub{authtoken} = $authtoken; # alias
109 $outsub{searchresults} = undef;
111 my $entries = $mesg->as_struct; # entries contain a hashref to all the search results
112 my ($dn, $attr, $data);
114 # Format the output....
115 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
116 $data = $entries->{$dn};
118 # These are local variables.. i have enough global vars as it is... <sigh>
119 my ($ufdn, $login, $name, $icquin, $email, $fingerprint, $address, $latlong, $vacation, $created, $modified, $lastseen) = undef;
121 $ufdn = $dn; # Net::LDAP does not have a dn2ufn function, but this is close enough :)
123 # Assemble name, attach web page link if present.
124 $name = $data->{cn}->[0]." ".$data->{mn}->[0]." ".$data->{sn}->[0];
125 if (my $url = $data->{labeledurl}->[0]) {
126 $name = "<a href=\"$url\">$name</a>";
129 # Add links to all email addresses
130 foreach (@{$data->{emailforward}}) {
131 $email .= "<br>" if ($email);
132 $email .= "<a href=\"mailto:$_\">$_</a>";
136 if ($data->{icquin}->[0]) {
137 $icquin = sprintf("<a href=\"http://wwp.icq.com/?icqnumber=%s\">%s</a>", $data->{icquin}->[0], $data->{icquin}->[0]);
140 # Format PGP/GPG key fingerprints
142 foreach (@{$data->{keyfingerprint}}) {
143 $fingerprint .= "<br>" if ($fingerprint);
144 $fingerprint .= sprintf("%d:- <a href=\"fetchkey.cgi?fingerprint=%s\">%s</a>", ++$fi, $_, &Util::FormatFingerPrint($_));
148 $address = $data->{postaladdress}->[0] || "- unlisted -";
149 $address =~ s/\$/<br>/g;
150 $address .= "<br>".$data->{l}->[0]."<br>".&Util::LookupCountry($data->{c}->[0])."<br>".$data->{postalcode}->[0];
152 # Assemble latitude/longitude
153 $latlong = $data->{latitude}->[0] || "none";
155 $latlong .= $data->{longitude}->[0] || "none";
157 # Modified/created time. TODO: maybe add is the name of the creator/modifier
158 $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
159 $created = &Util::FormatTimestamp($data->{createtimestamp}->[0]);
161 # Last seen information (Echelon)
162 $lastseen = &Util::FormatLastSeen($data->{"activity-pgp"}->[0],
163 $data->{"activity-from"}->[0]);
165 # Link in the debian login id
166 $login = $data->{uid}->[0]."\@debian.org";
167 $login = "<a href=\"mailto:$login\">$login</a>";
169 # See if the user has a vacation message
170 $vacation = $data->{onvacation}->[0];
172 # OK, now generate output... (i.e. put the output into the buffer )
173 $outsub{searchresults} .= '<table border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
174 $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
175 $outsub{searchresults} .= "($ufdn)</th></tr>\n";
178 $outsub{searchresults} .= "<tr><td colspan=2 align=center><b>$vacation</b></td></tr>\n";
181 $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
182 $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
183 $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
184 $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
186 $outsub{searchresults} .= FormatEntry($dataspecref->{icquin}, $icquin);
190 # Some data should only be available to authorized users...
191 if ($id eq $data->{uid}->[0]) {
192 $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
194 $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
195 $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
196 $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
197 $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
198 $outsub{searchresults} .= FormatEntry($dataspecref->{lastseen}, $lastseen);
200 $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
201 $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
203 $outsub{searchresults} .= "</table>";
205 # If this is ourselves, present a link to do mods
206 if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
207 $outsub{searchresults} .= "<a href=\"$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id&authtoken=$authtoken&editdn=".uri_escape($dn, "\x00-\x40\x7f-\xff")."\">Edit my settings</a>\n";
210 $outsub{searchresults} .= "<br><br><br>\n";
213 # Finally, we can write the output... yuck...
214 &Util::HTMLSendHeader;
215 foreach (@$lineref) {
216 if (/<\?ifauth(.+?)\?>/) {
217 $_ = ($auth ? $1 : "");
218 } elsif (/<\?ifnoauth(.+?)\?>/) {
219 $_ = ($auth ? "" : $1);
221 s/~(.+?)~/$outsub{$1}/g;
229 # Reads the output html file and find out how the output should be named
230 # -- this gives us a way to do translations more easily
231 # Returns the contents of the template (w/o the searchresult portion) and
232 # the output specification
238 open (F, "<$fn") || &Util::HTMLError("$fn: $!");
241 if (/<\?searchresults/i) {
243 push(@lines, "~searchresults~\n"); # Leave token so we know where to put the result
248 if (/searchresults\?>/i) {
253 s/\) *$//; # remove leading/trailing () and spaces
255 my ($desc, $attr) = split(/, /, $_, 2);
256 $hash{$attr} = $desc;
262 return (\@lines, \%hash);
266 my ($key, $val) = @_;
268 return "<tr><td align=right><b>$key:</b></td><td> $val</td></tr>\n";