3 # $Id: search.cgi,v 1.14 2006/12/22 08:58:50 rmurray 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>
6 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
10 #use Apache::Registry;
14 use Net::LDAP qw(:all);
17 my %config = &Util::ReadConfigFile;
20 my $id = $query->param('id');
21 my $authtoken = $query->param('authtoken');
22 my $password = &Util::CheckAuthToken($authtoken);
23 my $dosearch = $query->param('dosearch');
24 my $searchdn = $query->param('searchdn');
27 my $proto = ($ENV{HTTPS} ? "https" : "http");
30 $ldap->unbind if (defined($ldap));
33 $SIG{__DIE__} = \&DieHandler;
36 # No action yet, send back the search form...
37 &Util::HTMLSendHeader;
38 open (F, "<$config{websearchhtml}") || &Util::HTMLError($!);
41 s/~authtoken~/$authtoken/g;
46 # Go ahead and construct the search terms
48 cn => { fuzzy => 'cnfuzzy', formname => 'cn' }, # First name
49 mn => { fuzzy => 'mnfuzzy', formname => 'mn' }, # Middle name
50 sn => { fuzzy => 'snfuzzy', formname => 'sn' }, # Last name
51 email => { fuzzy => 'emailfuzzy', formname => 'email' }, # email
52 uid => { fuzzy => 'uidfuzzy', formname => 'uid' }, # Login name
53 ircnick => { fuzzy => 'ircfuzzy', formname => 'ircnick' }, # IRC nickname
54 keyfingerprint => { fuzzy => 'fpfuzzy', formname => 'fingerprint' }, # PGP/GPG fingerprint
55 c => { formname => 'country'}, # Country
58 # Do a little preprocessing - strip the spaces out of the fingerprint
59 my $temp = $query->param('fingerprint');
60 $temp =~ s/ //g; $query->param('fingerprint', $temp);
62 # go through %searchdata and pull out all the search criteria the user
64 my $filter = "(objectclass=inetOrgPerson)";
65 foreach (keys(%searchdata)) {
66 if ($query->param($searchdata{$_}{formname})) {
67 if ($query->param($searchdata{$_}{fuzzy})) {
69 $filter .= "($_~=".$query->param($searchdata{$_}{formname}).")";
71 $filter .= "($_=".$query->param($searchdata{$_}{formname}).")";
76 # Vacation is a special case, support it only when user is authenticated
77 $filter .= "(onvacation=*)" if ($query->param('vacation') && $authtoken && $id);
79 # AND all the search terms together
80 $filter = "(&$filter)";
82 # Read in the result template...
83 my ($lineref, $dataspecref) = ParseResult($config{websearchresulthtml});
85 # Now, we are ready to connect to the LDAP server.
86 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
90 if ($id && $password) {
91 $mesg = $ldap->bind("uid=$id,$config{basedn}", password => $password);
93 $auth = ($mesg->code == LDAP_SUCCESS);
96 if (!$auth) { # Not authenticated - either the above failed, or no password supplied
100 # &Util::HTMLPrint("Searching in $config{basedn} for $filter...\n");
102 $mesg = $ldap->search(base => ($searchdn ? $searchdn : $config{basedn}),
103 filter => ($searchdn ? "(uid=*)" : $filter));
104 $mesg->code && &Util::HTMLError($mesg->error);
106 my %outsub; # this hash will contain all the substitution tokens in the output
107 $outsub{count} = $mesg->count; # Count number of requests, also ensures we're done with the search
108 $outsub{auth} = $authtoken;
109 $outsub{authtoken} = $authtoken; # alias
111 $outsub{searchresults} = undef;
113 my $entries = $mesg->as_struct; # entries contain a hashref to all the search results
114 my ($dn, $attr, $data);
116 # Format the output....
117 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
119 $data = $entries->{$dn};
121 # These are local variables.. i have enough global vars as it is... <sigh>
122 my ($ufdn, $login, $name, $icquin, $jabberjid, $email, $fingerprint,
123 $address, $latlong, $vacation, $created, $modified, $lastseen, $gender) = undef;
125 $ufdn = $dn; # Net::LDAP does not have a dn2ufn function, but this is close enough :)
127 # Assemble name, attach web page link if present.
128 $name = $data->{cn}->[0]." ".$data->{mn}->[0]." ".$data->{sn}->[0];
129 if (my $url = $data->{labeleduri}->[0]) {
130 $name = "<a href=\"$url\">$name</a>";
133 # Add links to all email addresses
134 foreach (@{$data->{emailforward}}) {
135 $email .= "<br>" if ($email);
136 $email .= "<a href=\"mailto:$_\">$_</a>";
140 if ($data->{icquin}->[0]) {
141 $icquin = sprintf("<a href=\"http://wwp.icq.com/%s\">%s</a>", $data->{icquin}->[0], $data->{icquin}->[0]);
144 # Format PGP/GPG key fingerprints
146 foreach (@{$data->{keyfingerprint}}) {
147 $fingerprint .= "<br>" if ($fingerprint);
148 $fingerprint .= sprintf("%d:- <a href=\"fetchkey.cgi?fingerprint=%s\">%s</a>", ++$fi, $_, &Util::FormatFingerPrint($_));
152 $address = $data->{postaladdress}->[0] || "- unlisted -";
153 $address =~ s/\$/<br>/g;
154 $address .= "<br>".$data->{l}->[0]."<br>".&Util::LookupCountry($data->{c}->[0])."<br>".$data->{postalcode}->[0];
156 # Assemble latitude/longitude
157 $latlong = $data->{latitude}->[0] || "none";
159 $latlong .= $data->{longitude}->[0] || "none";
162 if ($data->{gender}->[0]) {
163 if ($data->{gender}->[0] == 1) {
164 $gender = $dataspecref->{male};
165 } elsif ($data->{gender}->[0] == 2) {
166 $gender = $dataspecref->{female};
168 $gender = $dataspecref->{unspecified};
171 $gender = $dataspecref->{unspecified};
174 # Modified/created time. TODO: maybe add is the name of the creator/modifier
175 $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
176 $created = &Util::FormatTimestamp($data->{createtimestamp}->[0]);
178 # Last seen information (Echelon)
179 $lastseen = &Util::FormatLastSeen($data->{"activity-pgp"}->[0],
180 $data->{"activity-from"}->[0]);
182 # Link in the debian login id
183 $login = $data->{uid}->[0];
186 # See if the user has a vacation message, non-public
187 $vacation = $data->{onvacation}->[0] if ($authtoken && $id);
189 # OK, now generate output... (i.e. put the output into the buffer )
190 $outsub{searchresults} .= '<table border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
191 $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
192 $outsub{searchresults} .= "($ufdn)</th></tr>\n";
195 $outsub{searchresults} .= "<tr><td colspan=2 align=center><b>$vacation</b></td></tr>\n";
198 $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
199 $outsub{searchresults} .= FormatEntry($dataspecref->{gender}, $gender);
200 if ($data->{ircnick}->[0]) {
201 $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
203 if ($data->{jabberjid}->[0]) {
204 $outsub{searchresults} .= FormatEntry($dataspecref->{jabberjid}, $data->{jabberjid}->[0]);
207 $outsub{searchresults} .= FormatEntry($dataspecref->{icquin}, $icquin);
209 $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
210 $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
211 if ($data->{maildisablemessage}->[0]) {
212 $outsub{searchresults} .= FormatEntry($dataspecref->{maildisablemessage}, $data->{maildisablemessage}->[0]);
216 # Some data should only be available to authorized users...
217 if ($id eq $data->{uid}->[0]) {
218 $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
220 $outsub{searchresults} .= FormatEntry($dataspecref->{birthdate}, $data->{birthdate}->[0]);
221 $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
222 $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
223 $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
224 $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
225 $outsub{searchresults} .= FormatEntry($dataspecref->{VoIP}, $data->{VoIP}->[0] || "- unlisted -");
226 $outsub{searchresults} .= FormatEntry($dataspecref->{lastseen}, $lastseen);
227 # $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
228 # $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
231 $outsub{searchresults} .= "</table>";
233 # If this is ourselves, present a link to do mods
234 if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
235 $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";
238 $outsub{searchresults} .= "<br><br><br>\n";
241 # Finally, we can write the output... yuck...
242 &Util::HTMLSendHeader;
243 foreach (@$lineref) {
244 if (/<\?ifauth(.+?)\?>/) {
245 $_ = ($auth ? $1 : "");
246 } elsif (/<\?ifnoauth(.+?)\?>/) {
247 $_ = ($auth ? "" : $1);
249 s/~(.+?)~/$outsub{$1}/g;
257 # Reads the output html file and find out how the output should be named
258 # -- this gives us a way to do translations more easily
259 # Returns the contents of the template (w/o the searchresult portion) and
260 # the output specification
266 open (F, "<$fn") || &Util::HTMLError("$fn: $!");
269 if (/<\?searchresults/i) {
271 push(@lines, "~searchresults~\n"); # Leave token so we know where to put the result
276 if (/searchresults\?>/i) {
281 s/\) *$//; # remove leading/trailing () and spaces
283 my ($desc, $attr) = split(/, /, $_, 2);
284 $hash{$attr} = $desc;
290 return (\@lines, \%hash);
294 my ($key, $val) = @_;
296 return "<tr><td align=right><b>$key:</b></td><td> $val</td></tr>\n";