silly paths
[mirror/userdir-ldap.git] / ud-fingerserv
1 #!/usr/bin/perl
2 # $Id: ud-fingerserv,v 1.10 1999/10/17 02:21:48 tausq Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '/var/www/userdir-ldap/';
7 #use lib '/home/randolph/projects/userdir-ldap/web';
8 use strict vars;
9 #use Apache::Registry;
10 use IO::Handle;
11 use IO::Socket;
12 use POSIX qw(:sys_wait_h);
13 use Getopt::Std;
14 use Util;
15 use Net::LDAP qw(:all);
16
17 # Global settings...
18 my %config = &Util::ReadConfigFile;
19 my %opts;
20 getopts("iqhv", \%opts);
21 my $use_inetd = $config{use_inetd} || $opts{i}; 
22 $| = 1;
23
24 my %attrs = (
25   'cn' => 'First name',
26   'mn' => 'Middle name',
27   'sn' => 'Last name',
28   'email' => 'Email',
29   'keyfingerprint' => 'Fingerprint',
30   'key' => 'Key block',
31   'ircnick' => 'IRC nickname'
32 );
33
34 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'ircnick', 'keyfingerprint', 'key');
35
36 $SIG{__DIE__} = \&DieHandler;
37 $SIG{INT} = \&DieHandler;
38 $SIG{CHLD} = \&Reaper;
39
40 &help if (defined($opts{h}));
41 #my $logfh = STDOUT; #TODO
42
43 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
44 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; 
45 $ldap->bind;
46
47 if (!$use_inetd) {
48   &log("Binding to port 79") if (defined($opts{v}));
49   my $server = IO::Socket::INET->new(Proto => 'tcp', 
50                                      LocalPort => 'finger(79)',
51                                      Listen => SOMAXCONN,
52                                      Reuse => 1);
53
54   die "Cannot listen on finger port" unless $server;
55   &log("[Server listening for connections]");
56
57   my ($pid, $client, $hostinfo);
58
59   while ($client = $server->accept()) {
60     &log("Forking to handle client request") if (defined($opts{v}));
61     next if $pid = fork; # parent
62     die "fork: $!" unless defined $pid;
63   
64     # child
65     $client->autoflush(1);
66     my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
67     &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
68     my $query = <$client>;
69     &ProcessQuery($client, $query);
70     $client->close;
71     exit;
72   } continue {
73     $client->close;
74   }
75 } else { # inetd
76   &log("inetd mode");
77   my $sockaddr = getpeername(STDIN);
78   my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
79   &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
80   my $query = <STDIN>;
81   &ProcessQuery(\*STDOUT, $query);
82   exit;
83 }
84
85 $ldap->unbind;
86
87 sub DieHandler {
88   $ldap->unbind if (defined($ldap));
89   exit 0;
90 }
91
92 sub Reaper {
93   1 until (-1 == waitpid(-1, WNOHANG));
94   $SIG{CHLD} = \&Reaper;
95 }
96
97 sub ProcessQuery {
98   my $client = shift;
99   my $query = shift;
100   
101   my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
102
103   $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
104   my ($uid, $fields) = split(/\//, $query, 2);
105   
106   if ($uid =~ /^help$/i) {
107     &sendhelp($client);
108     return;
109   }
110   
111   &log("Looking up $uid at $config{basedn}, uid=$uid");
112
113   $mesg = $ldap->search(base  => $config{basedn}, filter => "uid=$uid");
114   $mesg->code && die $mesg->error;
115   $entries = $mesg->as_struct;
116   
117   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
118     $data = $entries->{$dn};
119
120     $data->{key} = [];    
121     foreach (@{$data->{keyfingerprint}}) {
122       push (@{$data->{key}}, "\n".&Util::FetchKey($_));
123     }
124     
125     $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
126                                   $data->{mn}->[0], $data->{sn}->[0],
127                                   $data->{uid}->[0]."\@$config{emailappend}");
128                                   
129     $data->{email}->[0] =~ s/\s+/ /g;                             
130  
131     print $client "$dn\n";
132     if (!$fields) {
133       foreach $key (@summarykeys) {
134         foreach (@{$data->{$key}}) {
135           print $client "$attrs{$key}: ";
136           print $client "$_\n";
137         }
138       }
139     } else {
140   #     print "$fields\n";
141       foreach $key (split(/,/, $fields)) {
142         foreach (@{$data->{$key}}) {
143           print $client "$attrs{$key}: ";
144           print $client "$_\n";
145         }
146       }
147     }
148   }
149 }  
150
151 sub help {
152   print "fingerserv [-i | -q | -v | -h]\n";
153   print "-i = inetd mode; otherwise runs standalone\n";
154   print "-q = quiet mode; no output\n";
155   print "-v = verbose mode\n";
156   print "-h = this help message\n";
157   exit 0;
158 }
159
160 sub log {
161   my $msg = shift;
162   return if (defined($opts{q}));
163   
164   my $time = localtime;
165   print STDERR "$time $msg\n";
166 }
167
168 sub sendhelp {
169   my $client = shift;
170   
171   print $client "userdir-ldap finger daemon\n";
172   print $client "--------------------------\n";
173   print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
174   print $client "  where uid is the user id of the user\n";
175   print $client "  the optional attributes parameter specifies what to return\n";
176   print $client "    if nothing is specified, all attributes are returned.\n";
177   print $client "    The following attributes are current supported:\n";
178   foreach (@summarykeys) {
179     print $client "      $_ : $attrs{$_}\n";
180   }
181   print $client "    Multiple attributes can be separated by commas, like this:\n";
182   print $client "    finger tux/email,key\@db.debian.org\n";
183 }