Include accountname in totp url
[mirror/userdir-ldap-cgi.git] / search.cgi
index 3afb03a..bbdec50 100755 (executable)
@@ -1,7 +1,10 @@
 #!/usr/bin/perl
 
-# $Id: search.cgi,v 1.4 2000/01/11 04:52:53 tausq Exp $
+# $Id: search.cgi,v 1.14 2006/12/22 08:58:50 rmurray Exp $
 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
+# (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
+# (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
+# Copyright (c) 2008, 2011, 2013, 2015 Peter Palfrader
 
 use lib '.';
 use strict vars;
@@ -9,7 +12,7 @@ use strict vars;
 use CGI;
 use Util;
 use URI::Escape;
-use Net::LDAP qw(:all);
+use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
 
 # Global settings...
 my %config = &Util::ReadConfigFile;
@@ -17,10 +20,19 @@ my %config = &Util::ReadConfigFile;
 my $query = new CGI;
 my $id = $query->param('id');
 my $authtoken = $query->param('authtoken');
-my $password = &Util::CheckAuthToken($authtoken);
-my $dosearch = $query->param('dosearch');
-my $searchdn = $query->param('searchdn');
+my $dosearch = uri_escape($query->param('dosearch'));
+my $searchdn = uri_escape($query->param('searchdn'));
+
 my $ldap = undef;
+my $password = undef;
+
+if ($authtoken || $id) {
+  $password = Util::TouchAuthToken($authtoken, $id);
+} else {
+  $password = '';
+  $id = '';
+  $authtoken = '';
+}
 
 my $proto = ($ENV{HTTPS} ? "https" : "http");
 
@@ -28,11 +40,11 @@ sub DieHandler {
   $ldap->unbind if (defined($ldap));
 }
 
-$SIG{__DIE__} = \&DieHandler;
+#$SIG{__DIE__} = \&DieHandler;
 
 if (!$dosearch) {
   # No action yet, send back the search form...
-  print "Content-type: text/html\n\n";
+  &Util::HTMLSendHeader;
   open (F, "<$config{websearchhtml}") || &Util::HTMLError($!);
   while (<F>) {
     s/~id~/$id/g;
@@ -59,7 +71,7 @@ if (!$dosearch) {
 
   # go through %searchdata and pull out all the search criteria the user
   # specified...
-  my $filter = undef;
+  my $filter = "(objectclass=inetOrgPerson)(!(accountStatus=*))";
   foreach (keys(%searchdata)) {
     if ($query->param($searchdata{$_}{formname})) {    
       if ($query->param($searchdata{$_}{fuzzy})) {
@@ -71,8 +83,8 @@ if (!$dosearch) {
     }
   }
   
-  # Vacation is a special case
-  $filter .= "(onvacation=*)" if ($query->param('vacation'));
+  # Vacation is a special case, support it only when user is authenticated
+  $filter .= "(onvacation=*)" if ($query->param('vacation') && $authtoken && $id);
 
   # AND all the search terms together
   $filter = "(&$filter)";
@@ -82,6 +94,7 @@ if (!$dosearch) {
 
   # Now, we are ready to connect to the LDAP server.
   $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
+  &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
   my $auth = 0;
   my $mesg;
 
@@ -113,16 +126,25 @@ if (!$dosearch) {
 
   # Format the output....
   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
-    $data = $entries->{$dn};
-
+    my $ok = 0;
     # These are local variables.. i have enough global vars as it is... <sigh>
-    my ($ufdn, $login, $name, $email, $fingerprint, $address, $latlong, $vacation, $created, $modified, $lastseen) = undef;
+    my ($ufdn, $login, $name, $icquin, $jabberjid, $email, $fingerprint,
+       $address, $latlong, $vacation, $created, $modified, $lastseen) = undef;
+
+    # Last seen information (Echelon)
+    $lastseen = &Util::FormatLastSeen($entries->{$dn}->{"activity-pgp"}->[0],
+                                      $entries->{$dn}->{"activity-from"}->[0]);
+
+    $data = $entries->{$dn};
+    for my $key (keys %{$data}) {
+      @{$data->{$key}} = map { CGI::escapeHTML($_); } @{$data->{$key}};
+    }
     
     $ufdn = $dn; # Net::LDAP does not have a dn2ufn function, but this is close enough :)
     
     # Assemble name, attach web page link if present.
     $name = $data->{cn}->[0]." ".$data->{mn}->[0]." ".$data->{sn}->[0];
-    if (my $url = $data->{labeledurl}->[0]) {
+    if (my $url = $data->{labeleduri}->[0]) {
       $name = "<a href=\"$url\">$name</a>";
     }
     
@@ -132,6 +154,11 @@ if (!$dosearch) {
       $email .= "<a href=\"mailto:$_\">$_</a>";
     }
 
+    # ICQ 
+    if ($data->{icquin}->[0]) {
+      $icquin = sprintf("<a href=\"http://wwp.icq.com/%s\">%s</a>", $data->{icquin}->[0], $data->{icquin}->[0]);
+    }
+    
     # Format PGP/GPG key fingerprints
     my $fi;
     foreach (@{$data->{keyfingerprint}}) {
@@ -153,19 +180,15 @@ if (!$dosearch) {
     $modified = &Util::FormatTimestamp($data->{modifytimestamp}->[0]);
     $created =  &Util::FormatTimestamp($data->{createtimestamp}->[0]);
 
-    # Last seen information (Echelon)
-    $lastseen = &Util::FormatLastSeen($data->{"activity-pgp"}->[0] || 
-                                      $data->{"activity-from"}->[0]);
-
     # Link in the debian login id 
-    $login = $data->{uid}->[0]."\@debian.org";
-    $login = "<a href=\"mailto:$login\">$login</a>";
+    $login = $data->{uid}->[0];
+    $login = "$login";
     
-    # See if the user has a vacation message
-    $vacation = $data->{onvacation}->[0];
+    # See if the user has a vacation message, non-public
+    $vacation = $data->{onvacation}->[0] if ($authtoken && $id);
 
     # OK, now generate output... (i.e. put the output into the buffer )
-    $outsub{searchresults} .= '<table border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
+    $outsub{searchresults} .= '<table class="debform" border=2 cellpadding=2 cellspacing=0 bgcolor="#DDDDDD" width="80%">';
     $outsub{searchresults} .= '<tr><th bgcolor="#44CCCC" colspan=2><font size=+1>'."$name</font> ";
     $outsub{searchresults} .= "($ufdn)</th></tr>\n";
     
@@ -174,29 +197,42 @@ if (!$dosearch) {
     }
 
     $outsub{searchresults} .= FormatEntry($dataspecref->{uid}, $login);
-    $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
+    if ($data->{ircnick}->[0]) {
+      $outsub{searchresults} .= FormatEntry($dataspecref->{ircnick}, $data->{ircnick}->[0]);
+    }
+    if ($data->{jabberjid}->[0]) {
+      $outsub{searchresults} .= FormatEntry($dataspecref->{jabberjid}, $data->{jabberjid}->[0]);
+    }
+    if ($icquin) {
+      $outsub{searchresults} .= FormatEntry($dataspecref->{icquin}, $icquin);
+    }
     $outsub{searchresults} .= FormatEntry($dataspecref->{loginshell}, $data->{loginshell}->[0]);
     $outsub{searchresults} .= FormatEntry($dataspecref->{fingerprint}, $fingerprint);
-    
+    if ($data->{maildisablemessage}->[0]) {
+      $outsub{searchresults} .= FormatEntry($dataspecref->{maildisablemessage}, $data->{maildisablemessage}->[0]);
+    }
+
     if ($auth) {
       # Some data should only be available to authorized users...
       if ($id eq $data->{uid}->[0]) {
         $outsub{searchresults} .= FormatEntry($dataspecref->{email}, $email);
       }
+      $outsub{searchresults} .= FormatEntry($dataspecref->{birthdate}, $data->{birthdate}->[0]);
       $outsub{searchresults} .= FormatEntry($dataspecref->{address}, $address);
       $outsub{searchresults} .= FormatEntry($dataspecref->{latlong}, $latlong);
       $outsub{searchresults} .= FormatEntry($dataspecref->{phone}, $data->{telephonenumber}->[0] || "- unlisted -");
       $outsub{searchresults} .= FormatEntry($dataspecref->{fax}, $data->{fascimiletelephonenumber}->[0] || "- unlisted -");
+      $outsub{searchresults} .= FormatEntry($dataspecref->{VoIP}, $data->{voip}->[0] || "- unlisted -");
       $outsub{searchresults} .= FormatEntry($dataspecref->{lastseen}, $lastseen);
+#     $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
+#     $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
     }
-    $outsub{searchresults} .= FormatEntry($dataspecref->{created}, $created);
-    $outsub{searchresults} .= FormatEntry($dataspecref->{modified}, $modified);
     
     $outsub{searchresults} .= "</table>";
     
     # If this is ourselves, present a link to do mods
     if ($auth && ($id eq $data->{uid}->[0])) { #TODO: extract this string into a url for translation...
-      $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";
+      $outsub{searchresults} .= "<a href=\"$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$id;authtoken=$authtoken\">Edit my settings</a>\n";
     }
     
     $outsub{searchresults} .= "<br><br><br>\n";