3 # (c) 1999 Debian and Randolph Chung. Licensed under the GPL. <tausq@debian.org>
11 use Net::LDAP qw(:all);
14 my %config = &Util::ReadConfigFile;
17 my $id = $query->param('id');
18 my $authtoken = $query->param('authtoken');
19 my $password = &Util::CheckAuthToken($authtoken);
20 my $dosearch = $query->param('dosearch');
21 my $searchdn = $query->param('searchdn');
24 my $proto = ($ENV{HTTPS} ? "https" : "http");
27 $ldap->unbind if (defined($ldap));
30 $SIG{__DIE__} = \&DieHandler;
33 # No action yet, send back the search form...
34 print "Content-type: text/html\n\n";
35 open (F, "<$config{websearchhtml}") || &Util::HTMLError($!);
38 s/~authtoken~/$authtoken/g;
43 # Go ahead and construct the search terms
45 cn => { fuzzy => 'cnfuzzy', formname => 'cn' }, # First name
46 mn => { fuzzy => 'mnfuzzy', formname => 'mn' }, # Middle name
47 sn => { fuzzy => 'snfuzzy', formname => 'sn' }, # Last name
48 email => { fuzzy => 'emailfuzzy', formname => 'email' }, # email
49 uid => { fuzzy => 'uidfuzzy', formname => 'uid' }, # Login name
50 ircnick => { fuzzy => 'ircfuzzy', formname => 'ircnick' }, # IRC nickname
51 keyfingerprint => { fuzzy => 'fpfuzzy', formname => 'fingerprint' }, # PGP/GPG fingerprint
52 c => { formname => 'country'}, # Country
55 # Do a little preprocessing - strip the spaces out of the fingerprint
56 my $temp = $query->param('fingerprint');
57 $temp =~ s/ //g; $query->param('fingerprint', $temp);
59 # go through %searchdata and pull out all the search criteria the user
62 foreach (keys(%searchdata)) {
63 if ($query->param($searchdata{$_}{formname})) {
64 if ($query->param($searchdata{$_}{fuzzy})) {
66 $filter .= "($_~=".$query->param($searchdata{$_}{formname}).")";
68 $filter .= "($_=".$query->param($searchdata{$_}{formname}).")";
73 # Vacation is a special case
74 $filter .= "(onvacation=*)" if ($query->param('vacation'));
76 # AND all the search terms together
77 $filter = "(&$filter)";
79 # Read in the result template...
80 my ($lineref, $dataspecref) = ParseResult($config{websearchresulthtml});
82 # Now, we are ready to connect to the LDAP server.
83 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
87 if ($id && $password) {
88 $mesg = $ldap->bind("uid=$id,$config{basedn}", password => $password);
90 $auth = ($mesg->code == LDAP_SUCCESS);
93 if (!$auth) { # Not authenticated - either the above failed, or no password supplied
97 # &Util::HTMLPrint("Searching in $config{basedn} for $filter...\n");
99 $mesg = $ldap->search(base => ($searchdn ? $searchdn : $config{basedn}),
100 filter => ($searchdn ? "(uid=*)" : $filter));
101 $mesg->code && &Util::HTMLError($mesg->error);
103 my %outsub; # this hash will contain all the substitution tokens in the output
104 $outsub{count} = $mesg->count; # Count number of requests, also ensures we're done with the search
105 $outsub{auth} = $authtoken;
106 $outsub{authtoken} = $authtoken; # alias
108 $outsub{searchresults} = undef;
110 my $entries = $mesg->as_struct; # entries contain a hashref to all the search results
111 my ($dn, $attr, $data);
113 # Format the output....
114 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
115 $data = $entries->{$dn};
117 # These are local variables.. i have enough global vars as it is... <sigh>
118 my ($ufdn, $login, $name, $email, $fingerprint, $address, $latlong, $vacation, $created, $modified) = undef;
120 $ufdn = $dn; # Net::LDAP does not have a dn2ufn function, but this is close enough :)
122 # Assemble name, attach web page link if present.
123 $name = $data->{cn}->[0]." ".$data->{mn}->[0]." ".$data->{sn}->[0];
124 if (my $url = $data->{labeledurl}->[0]) {
125 $name = "<a href=\"$url\">$name</a>";
128 # Add links to all email addresses
129 foreach (@{$data->{emailforward}}) {
130 $email .= "<br>" if ($email);
131 $email .= "<a href=\"mailto:$_\">$_</a>";
134 # Format PGP/GPG key fingerprints
136 foreach (@{$data->{keyfingerprint}}) {
137 $fingerprint .= "<br>" if ($fingerprint);
138 $fingerprint .= sprintf("%d:- <a href=\"fetchkey.cgi?fingerprint=%s\">%s</a>", ++$fi, $_, &Util::FormatFingerPrint($_));
142 $address = $data->{postaladdress}->[0] || "- unlisted -";
143 $address =~ s/\$/<br>/g;
144 $address .= "<br>".$data->{l}->[0]."<br>".&Util::LookupCountry($data->{c}->[0])."<br>".$data->{postalcode}->[0];
146 # Assemble latitude/longitude
147 $latlong = $data->{latitude}->[0] || "none";
149 $latlong .= $data->{longitude}->[0] || "none";
151 # Modified/created time. TODO: maybe add is the name of the creator/modifier
152 $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
153 $created = &Util::FormatTimestamp($data->{createtimestamp}->[0]);
155 # Link in the debian login id
156 $login = $data->{uid}->[0]."\@debian.org";
157 $login = "<a href=\"mailto:$login\">$login</a>";
159 # See if the user has a vacation message
160 $vacation = $data->{onvacation}->[0];
162 # OK, now generate output... (i.e. put the output into the buffer )
163 $outsub{searchresults} .= '<table border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
164 $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
165 $outsub{searchresults} .= "($ufdn)</th></tr>\n";
168 $outsub{searchresults} .= "<tr><td colspan=2 align=center><b>$vacation</b></td></tr>\n";
171 $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
172 $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
173 $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
174 $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
177 # Some data should only be available to authorized users...
178 if ($id eq $data->{uid}->[0]) {
179 $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
181 $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
182 $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
183 $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
184 $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
186 $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
187 $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
189 $outsub{searchresults} .= "</table>";
191 # If this is ourselves, present a link to do mods
192 if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
193 $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";
196 $outsub{searchresults} .= "<br><br><br>\n";
199 # Finally, we can write the output... yuck...
200 &Util::HTMLSendHeader;
201 foreach (@$lineref) {
202 if (/<\?ifauth(.+?)\?>/) {
203 $_ = ($auth ? $1 : "");
204 } elsif (/<\?ifnoauth(.+?)\?>/) {
205 $_ = ($auth ? "" : $1);
207 s/~(.+?)~/$outsub{$1}/g;
215 # Reads the output html file and find out how the output should be named
216 # -- this gives us a way to do translations more easily
217 # Returns the contents of the template (w/o the searchresult portion) and
218 # the output specification
224 open (F, "<$fn") || &Util::HTMLError("$fn: $!");
227 if (/<\?searchresults/i) {
229 push(@lines, "~searchresults~\n"); # Leave token so we know where to put the result
234 if (/searchresults\?>/i) {
239 s/\) *$//; # remove leading/trailing () and spaces
241 my ($desc, $attr) = split(/, /, $_, 2);
242 $hash{$attr} = $desc;
248 return (\@lines, \%hash);
252 my ($key, $val) = @_;
254 return "<tr><td align=right><b>$key:</b></td><td> $val</td></tr>\n";