3 # $Id: search.cgi,v 1.2 1999/09/26 01:20:39 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, $email, $fingerprint, $address, $latlong, $vacation, $created, $modified) = 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>";
135 # Format PGP/GPG key fingerprints
137 foreach (@{$data->{keyfingerprint}}) {
138 $fingerprint .= "<br>" if ($fingerprint);
139 $fingerprint .= sprintf("%d:- <a href=\"fetchkey.cgi?fingerprint=%s\">%s</a>", ++$fi, $_, &Util::FormatFingerPrint($_));
143 $address = $data->{postaladdress}->[0] || "- unlisted -";
144 $address =~ s/\$/<br>/g;
145 $address .= "<br>".$data->{l}->[0]."<br>".&Util::LookupCountry($data->{c}->[0])."<br>".$data->{postalcode}->[0];
147 # Assemble latitude/longitude
148 $latlong = $data->{latitude}->[0] || "none";
150 $latlong .= $data->{longitude}->[0] || "none";
152 # Modified/created time. TODO: maybe add is the name of the creator/modifier
153 $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
154 $created = &Util::FormatTimestamp($data->{createtimestamp}->[0]);
156 # Link in the debian login id
157 $login = $data->{uid}->[0]."\@debian.org";
158 $login = "<a href=\"mailto:$login\">$login</a>";
160 # See if the user has a vacation message
161 $vacation = $data->{onvacation}->[0];
163 # OK, now generate output... (i.e. put the output into the buffer )
164 $outsub{searchresults} .= '<table border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
165 $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
166 $outsub{searchresults} .= "($ufdn)</th></tr>\n";
169 $outsub{searchresults} .= "<tr><td colspan=2 align=center><b>$vacation</b></td></tr>\n";
172 $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
173 $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
174 $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
175 $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
178 # Some data should only be available to authorized users...
179 if ($id eq $data->{uid}->[0]) {
180 $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
182 $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
183 $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
184 $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
185 $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
187 $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
188 $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
190 $outsub{searchresults} .= "</table>";
192 # If this is ourselves, present a link to do mods
193 if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
194 $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";
197 $outsub{searchresults} .= "<br><br><br>\n";
200 # Finally, we can write the output... yuck...
201 &Util::HTMLSendHeader;
202 foreach (@$lineref) {
203 if (/<\?ifauth(.+?)\?>/) {
204 $_ = ($auth ? $1 : "");
205 } elsif (/<\?ifnoauth(.+?)\?>/) {
206 $_ = ($auth ? "" : $1);
208 s/~(.+?)~/$outsub{$1}/g;
216 # Reads the output html file and find out how the output should be named
217 # -- this gives us a way to do translations more easily
218 # Returns the contents of the template (w/o the searchresult portion) and
219 # the output specification
225 open (F, "<$fn") || &Util::HTMLError("$fn: $!");
228 if (/<\?searchresults/i) {
230 push(@lines, "~searchresults~\n"); # Leave token so we know where to put the result
235 if (/searchresults\?>/i) {
240 s/\) *$//; # remove leading/trailing () and spaces
242 my ($desc, $attr) = split(/, /, $_, 2);
243 $hash{$attr} = $desc;
249 return (\@lines, \%hash);
253 my ($key, $val) = @_;
255 return "<tr><td align=right><b>$key:</b></td><td> $val</td></tr>\n";