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>
7 # Copyright (c) 2008, 2011, 2013, 2015 Peter Palfrader
11 #use Apache::Registry;
15 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
18 my %config = &Util::ReadConfigFile;
21 my $id = $query->param('id');
22 my $authtoken = $query->param('authtoken');
23 my $dosearch = uri_escape($query->param('dosearch'));
24 my $searchdn = uri_escape($query->param('searchdn'));
29 if ($authtoken || $id) {
30 $password = Util::TouchAuthToken($authtoken, $id);
37 my $proto = ($ENV{HTTPS} ? "https" : "http");
40 $ldap->unbind if (defined($ldap));
43 #$SIG{__DIE__} = \&DieHandler;
46 # No action yet, send back the search form...
47 &Util::HTMLSendHeader;
48 open (F, "<$config{websearchhtml}") || &Util::HTMLError($!);
51 s/~authtoken~/$authtoken/g;
56 # Go ahead and construct the search terms
58 cn => { fuzzy => 'cnfuzzy', formname => 'cn' }, # First name
59 mn => { fuzzy => 'mnfuzzy', formname => 'mn' }, # Middle name
60 sn => { fuzzy => 'snfuzzy', formname => 'sn' }, # Last name
61 email => { fuzzy => 'emailfuzzy', formname => 'email' }, # email
62 uid => { fuzzy => 'uidfuzzy', formname => 'uid' }, # Login name
63 ircnick => { fuzzy => 'ircfuzzy', formname => 'ircnick' }, # IRC nickname
64 keyfingerprint => { fuzzy => 'fpfuzzy', formname => 'fingerprint' }, # PGP/GPG fingerprint
65 c => { formname => 'country'}, # Country
68 # Do a little preprocessing - strip the spaces out of the fingerprint
69 my $temp = $query->param('fingerprint');
70 $temp =~ s/ //g; $query->param('fingerprint', $temp);
72 # go through %searchdata and pull out all the search criteria the user
74 my $filter = "(objectclass=inetOrgPerson)(!(accountStatus=*))";
75 foreach (keys(%searchdata)) {
76 if ($query->param($searchdata{$_}{formname})) {
77 if ($query->param($searchdata{$_}{fuzzy})) {
79 $filter .= "($_~=".$query->param($searchdata{$_}{formname}).")";
81 $filter .= "($_=".$query->param($searchdata{$_}{formname}).")";
86 # Vacation is a special case, support it only when user is authenticated
87 $filter .= "(onvacation=*)" if ($query->param('vacation') && $authtoken && $id);
89 # AND all the search terms together
90 $filter = "(&$filter)";
92 # Read in the result template...
93 my ($lineref, $dataspecref) = ParseResult($config{websearchresulthtml});
95 # Now, we are ready to connect to the LDAP server.
96 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
97 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
101 if ($id && $password) {
102 $mesg = $ldap->bind("uid=$id,$config{basedn}", password => $password);
104 $auth = ($mesg->code == LDAP_SUCCESS);
107 if (!$auth) { # Not authenticated - either the above failed, or no password supplied
111 # &Util::HTMLPrint("Searching in $config{basedn} for $filter...\n");
113 $mesg = $ldap->search(base => ($searchdn ? $searchdn : $config{basedn}),
114 filter => ($searchdn ? "(uid=*)" : $filter));
115 $mesg->code && &Util::HTMLError($mesg->error);
117 my %outsub; # this hash will contain all the substitution tokens in the output
118 $outsub{count} = $mesg->count; # Count number of requests, also ensures we're done with the search
119 $outsub{auth} = $authtoken;
120 $outsub{authtoken} = $authtoken; # alias
122 $outsub{searchresults} = undef;
124 my $entries = $mesg->as_struct; # entries contain a hashref to all the search results
125 my ($dn, $attr, $data);
127 # Format the output....
128 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
130 # These are local variables.. i have enough global vars as it is... <sigh>
131 my ($ufdn, $login, $name, $icquin, $jabberjid, $email, $fingerprint,
132 $address, $latlong, $vacation, $created, $modified, $lastseen, $gender) = undef;
134 # Last seen information (Echelon)
135 $lastseen = &Util::FormatLastSeen($entries->{$dn}->{"activity-pgp"}->[0],
136 $entries->{$dn}->{"activity-from"}->[0]);
138 $data = $entries->{$dn};
139 for my $key (keys %{$data}) {
140 @{$data->{$key}} = map { CGI::escapeHTML($_); } @{$data->{$key}};
143 $ufdn = $dn; # Net::LDAP does not have a dn2ufn function, but this is close enough :)
145 # Assemble name, attach web page link if present.
146 $name = $data->{cn}->[0]." ".$data->{mn}->[0]." ".$data->{sn}->[0];
147 if (my $url = $data->{labeleduri}->[0]) {
148 $name = "<a href=\"$url\">$name</a>";
151 # Add links to all email addresses
152 foreach (@{$data->{emailforward}}) {
153 $email .= "<br>" if ($email);
154 $email .= "<a href=\"mailto:$_\">$_</a>";
158 if ($data->{icquin}->[0]) {
159 $icquin = sprintf("<a href=\"http://wwp.icq.com/%s\">%s</a>", $data->{icquin}->[0], $data->{icquin}->[0]);
162 # Format PGP/GPG key fingerprints
164 foreach (@{$data->{keyfingerprint}}) {
165 $fingerprint .= "<br>" if ($fingerprint);
166 $fingerprint .= sprintf("%d:- <a href=\"fetchkey.cgi?fingerprint=%s\">%s</a>", ++$fi, $_, &Util::FormatFingerPrint($_));
170 $address = $data->{postaladdress}->[0] || "- unlisted -";
171 $address =~ s/\$/<br>/g;
172 $address .= "<br>".$data->{l}->[0]."<br>".&Util::LookupCountry($data->{c}->[0])."<br>".$data->{postalcode}->[0];
174 # Assemble latitude/longitude
175 $latlong = $data->{latitude}->[0] || "none";
177 $latlong .= $data->{longitude}->[0] || "none";
180 if ($data->{gender}->[0]) {
181 if ($data->{gender}->[0] == 1) {
182 $gender = $dataspecref->{male};
183 } elsif ($data->{gender}->[0] == 2) {
184 $gender = $dataspecref->{female};
186 $gender = $dataspecref->{unspecified};
189 $gender = $dataspecref->{unspecified};
192 # Modified/created time. TODO: maybe add is the name of the creator/modifier
193 $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
194 $created = &Util::FormatTimestamp($data->{createtimestamp}->[0]);
196 # Link in the debian login id
197 $login = $data->{uid}->[0];
200 # See if the user has a vacation message, non-public
201 $vacation = $data->{onvacation}->[0] if ($authtoken && $id);
203 # OK, now generate output... (i.e. put the output into the buffer )
204 $outsub{searchresults} .= '<table class="debform" border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
205 $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
206 $outsub{searchresults} .= "($ufdn)</th></tr>\n";
209 $outsub{searchresults} .= "<tr><td colspan=2 align=center><b>$vacation</b></td></tr>\n";
212 $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
213 $outsub{searchresults} .= FormatEntry($dataspecref->{gender}, $gender);
214 if ($data->{ircnick}->[0]) {
215 $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
217 if ($data->{jabberjid}->[0]) {
218 $outsub{searchresults} .= FormatEntry($dataspecref->{jabberjid}, $data->{jabberjid}->[0]);
221 $outsub{searchresults} .= FormatEntry($dataspecref->{icquin}, $icquin);
223 $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
224 $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
225 if ($data->{maildisablemessage}->[0]) {
226 $outsub{searchresults} .= FormatEntry($dataspecref->{maildisablemessage}, $data->{maildisablemessage}->[0]);
230 # Some data should only be available to authorized users...
231 if ($id eq $data->{uid}->[0]) {
232 $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
234 $outsub{searchresults} .= FormatEntry($dataspecref->{birthdate}, $data->{birthdate}->[0]);
235 $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
236 $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
237 $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
238 $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
239 $outsub{searchresults} .= FormatEntry($dataspecref->{VoIP}, $data->{voip}->[0] || "- unlisted -");
240 $outsub{searchresults} .= FormatEntry($dataspecref->{lastseen}, $lastseen);
241 # $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
242 # $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
245 $outsub{searchresults} .= "</table>";
247 # If this is ourselves, present a link to do mods
248 if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
249 $outsub{searchresults} .= "<a href=\"$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id;authtoken=$authtoken\">Edit my settings</a>\n";
252 $outsub{searchresults} .= "<br><br><br>\n";
255 # Finally, we can write the output... yuck...
256 &Util::HTMLSendHeader;
257 foreach (@$lineref) {
258 if (/<\?ifauth(.+?)\?>/) {
259 $_ = ($auth ? $1 : "");
260 } elsif (/<\?ifnoauth(.+?)\?>/) {
261 $_ = ($auth ? "" : $1);
263 s/~(.+?)~/$outsub{$1}/g;
271 # Reads the output html file and find out how the output should be named
272 # -- this gives us a way to do translations more easily
273 # Returns the contents of the template (w/o the searchresult portion) and
274 # the output specification
280 open (F, "<$fn") || &Util::HTMLError("$fn: $!");
283 if (/<\?searchresults/i) {
285 push(@lines, "~searchresults~\n"); # Leave token so we know where to put the result
290 if (/searchresults\?>/i) {
295 s/\) *$//; # remove leading/trailing () and spaces
297 my ($desc, $attr) = split(/, /, $_, 2);
298 $hash{$attr} = $desc;
304 return (\@lines, \%hash);
308 my ($key, $val) = @_;
310 return "<tr><td align=right><b>$key:</b></td><td> $val</td></tr>\n";