]> git.sur5r.net Git - openldap/blob - clients/finger/main.c
Initial revision
[openldap] / clients / finger / main.c
1 /*
2  * Copyright (c) 1990,1994 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "lber.h"
14 #include "ldap.h"
15 #include "disptmpl.h"
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <syslog.h>
26 #include <sys/resource.h>
27 #include <sys/wait.h>
28 #ifdef aix
29 #include <sys/select.h>
30 #endif /* aix */
31 #include <signal.h>
32 #include "portable.h"
33 #include "ldapconfig.h"
34
35 #ifdef USE_SYSCONF
36 #include <unistd.h>
37 #endif /* USE_SYSCONF */
38
39 int     dosyslog = 1;
40 char    *ldaphost = LDAPHOST;
41 int     ldapport = LDAP_PORT;
42 char    *base = FINGER_BASE;
43 int     deref;
44 char    *filterfile = FILTERFILE;
45 char    *templatefile = TEMPLATEFILE;
46 int     rdncount = FINGER_RDNCOUNT;
47
48 static do_query();
49 static do_search();
50 static do_read();
51 static print_attr();
52
53 static usage( name )
54 char    *name;
55 {
56         fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
57         exit( 1 );
58 }
59
60 main (argc, argv)
61 int     argc;
62 char    **argv;
63 {
64         int                     i;
65         char                    *myname;
66         unsigned long           mypeer = -1;
67         struct hostent          *hp;
68         struct sockaddr_in      peername;
69         int                     peernamelen;
70         int                     interactive = 0;
71         extern char             *optarg;
72
73         deref = FINGER_DEREF;
74         while ( (i = getopt( argc, argv, "f:ilp:t:x:p:c:" )) != EOF ) {
75                 switch( i ) {
76                 case 'f':       /* ldap filter file */
77                         filterfile = strdup( optarg );
78                         break;
79
80                 case 'i':       /* interactive */
81                         interactive = 1;
82                         break;
83
84                 case 'l':       /* don't do syslogging */
85                         dosyslog = 0;
86                         break;
87
88                 case 't':       /* ldap template file */
89                         templatefile = strdup( optarg );
90                         break;
91
92                 case 'x':       /* specify ldap host */
93                         ldaphost = strdup( optarg );
94                         break;
95
96                 case 'p':       /* specify ldap port */
97                         ldapport = atoi( optarg );
98                         break;
99
100                 case 'c':       /* specify number of DN components to show */
101                         rdncount = atoi( optarg );
102                         break;
103
104                 default:
105                         usage( argv[0] );
106                 }
107         }
108
109         if ( !interactive ) {
110                 peernamelen = sizeof(peername);
111                 if ( getpeername( 0, (struct sockaddr *)&peername,
112                     &peernamelen ) != 0 ) {
113                         perror( "getpeername" );
114                         exit( 1 );
115                 }
116                 mypeer = (unsigned long) peername.sin_addr.s_addr;
117         }
118
119 #ifdef FINGER_BANNER
120         if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
121                 printf( FINGER_BANNER );
122                 fflush( stdout );
123         }
124 #endif
125
126         if ( (myname = strrchr( argv[0], '/' )) == NULL )
127                 myname = strdup( argv[0] );
128         else
129                 myname = strdup( myname + 1 );
130
131         if ( dosyslog ) {
132 #ifdef LOG_LOCAL4
133                 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
134 #else
135                 openlog( myname, OPENLOG_OPTIONS );
136 #endif
137         }
138
139         if ( dosyslog && mypeer != -1 ) {
140                 struct in_addr  addr;
141
142                 hp = gethostbyaddr( (char *) &mypeer, sizeof(mypeer), AF_INET );
143                 addr.s_addr = mypeer;
144                 syslog( LOG_INFO, "connection from %s (%s)", (hp == NULL) ?
145                     "unknown" : hp->h_name, inet_ntoa( addr ) );
146         }
147
148         do_query();
149
150         return( 0 );
151 }
152
153 static do_query()
154 {
155         char            buf[256];
156         int             len, rc, tblsize;
157         struct timeval  timeout;
158         fd_set          readfds;
159         LDAP            *ld;
160
161         if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
162                 fprintf( stderr, FINGER_UNAVAILABLE );
163                 perror( "ldap_open" );
164                 exit( 1 );
165         }
166         ld->ld_sizelimit = FINGER_SIZELIMIT;
167         ld->ld_deref = deref;
168
169         if ( ldap_simple_bind_s( ld, FINGER_BINDDN, NULL ) != LDAP_SUCCESS ) {
170                 fprintf( stderr, FINGER_UNAVAILABLE );
171                 ldap_perror( ld, "ldap_simple_bind_s" );
172                 exit( 1 );
173         }
174
175 #ifdef USE_SYSCONF
176         tblsize = sysconf( _SC_OPEN_MAX );
177 #else /* USE_SYSCONF */
178         tblsize = getdtablesize();
179 #endif /* USE_SYSCONF */
180
181         timeout.tv_sec = FINGER_TIMEOUT;
182         timeout.tv_usec = 0;
183         FD_ZERO( &readfds );
184         FD_SET( fileno( stdin ), &readfds );
185
186         if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
187                 if ( rc < 0 )
188                         perror( "select" );
189                 else
190                         fprintf( stderr, "connection timed out on input\r\n" );
191                 exit( 1 );
192         }
193
194         if ( fgets( buf, sizeof(buf), stdin ) == NULL )
195                 exit( 1 );
196
197         len = strlen( buf );
198
199         /* strip off \r \n */
200         if ( buf[len - 1] == '\n' ) {
201                 buf[len - 1] = '\0';
202                 len--;
203         }
204         if ( buf[len - 1] == '\r' ) {
205                 buf[len - 1] = '\0';
206                 len--;
207         }
208
209         if ( len == 0 ) {
210                 printf( "No campus-wide login information available.  Info for this machine only:\r\n" );
211                 fflush( stdout );
212                 execl( FINGER_CMD, FINGER_CMD, NULL );
213         } else {
214                 char    *p;
215
216                 /* skip and ignore stinking /w */
217                 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
218                         p = buf + 2;
219                 } else {
220                         p = buf;
221                 }
222
223                 for ( ; *p && isspace( *p ); p++ )
224                         ;       /* NULL */
225
226                 do_search( ld, p );
227         }
228 }
229
230 static void
231 spaces2dots( s )
232     char        *s;
233 {
234         for ( ; *s; s++ ) {
235                 if ( *s == ' ' ) {
236                         *s = '.';
237                 }
238         }
239 }
240
241 static do_search( ld, buf )
242 LDAP    *ld;
243 char    *buf;
244 {
245         char            *dn, *rdn;
246         char            **title;
247         int             rc, matches, i, ufn;
248         struct timeval  tv;
249         LDAPFiltInfo    *fi;
250         LDAPMessage     *result, *e;
251         static char     *attrs[] = { "cn", "title", "objectClass", "joinable",
252 #ifdef FINGER_SORT_ATTR
253                                         FINGER_SORT_ATTR,
254 #endif
255                                         0 };
256         extern int      strcasecmp();
257
258         ufn = 0;
259 #ifdef FINGER_UFN
260         if ( strchr( buf, ',' ) != NULL ) {
261                 ldap_ufn_setprefix( ld, base );
262                 tv.tv_sec = FINGER_TIMEOUT;
263                 tv.tv_usec = 0;
264                 ldap_ufn_timeout( (void *) &tv );
265
266                 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
267                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
268                         fprintf( stderr, FINGER_UNAVAILABLE );
269                         ldap_perror( ld, "ldap_search_st" );
270                         exit( 1 );
271                 }
272
273                 matches = ldap_count_entries( ld, result );
274                 ufn = 1;
275         } else {
276 #endif
277                 if ( (ld->ld_filtd = ldap_init_getfilter( filterfile ))
278                     == NULL ) {
279                         fprintf( stderr, "Cannot open filter file (%s)\n",
280                             filterfile );
281                         exit( 1 );
282                 }
283
284                 for ( fi = ldap_getfirstfilter( ld->ld_filtd, "finger", buf );
285                     fi != NULL;
286                     fi = ldap_getnextfilter( ld->ld_filtd ) )
287                 {
288                         tv.tv_sec = FINGER_TIMEOUT;
289                         tv.tv_usec = 0;
290                         if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
291                             fi->lfi_filter, attrs, 0, &tv, &result ))
292                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
293                             && rc != LDAP_TIMELIMIT_EXCEEDED )
294                         {
295                                 fprintf( stderr, FINGER_UNAVAILABLE );
296                                 ldap_perror( ld, "ldap_search_st" );
297                                 exit( 1 );
298                         }
299
300                         if ( (matches = ldap_count_entries( ld, result )) != 0 )
301                                 break;
302
303                         ldap_msgfree( result );
304                         result = NULL;
305                 }
306 #ifdef FINGER_UFN
307         }
308 #endif
309
310         if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
311                 printf( "(Partial results - a size limit was exceeded)\r\n" );
312         } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
313                 printf( "(Partial results - a time limit was exceeded)\r\n" );
314         }
315
316         if ( matches == 0 ) {
317                 printf( FINGER_NOMATCH );
318                 fflush( stdout );
319         } else if ( matches < 0 ) {
320                 fprintf( stderr, "error return from ldap_count_entries\r\n" );
321                 exit( 1 );
322         } else if ( matches <= FINGER_LISTLIMIT ) {
323                 printf( "%d %s match%s found for \"%s\":\r\n", matches,
324                     ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
325                 fflush( stdout );
326
327                 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
328                         do_read( ld, e );
329                         e = ldap_next_entry( ld, e );
330                         if ( e != NULL ) {
331                                 printf( "--------------------\r\n" );
332                         }
333                 }
334         } else {
335                 printf( "%d %s matches for \"%s\":\r\n", matches,
336                     ufn ? "UFN" : fi->lfi_desc, buf );
337                 fflush( stdout );
338
339 #ifdef FINGER_SORT_ATTR
340                 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
341 #endif
342
343                 for ( e = ldap_first_entry( ld, result ); e != NULL;
344                     e = ldap_next_entry( ld, e ) ) {
345                         char    *p;
346
347                         dn = ldap_get_dn( ld, e );
348                         rdn = dn;
349                         if ( (p = strchr( dn, ',' )) != NULL )
350                                 *p = '\0';
351                         while ( *rdn && *rdn != '=' )
352                                 rdn++;
353                         if ( *rdn )
354                                 rdn++;
355
356                         /* hack attack */
357                         for ( i = 0; buf[i] != '\0'; i++ ) {
358                                 if ( buf[i] == '.' || buf[i] == '_' )
359                                         buf[i] = ' ';
360                         }
361                         if ( strcasecmp( rdn, buf ) == 0 ) {
362                                 char    **cn;
363                                 int     i, last;
364
365                                 cn = ldap_get_values( ld, e, "cn" );
366                                 for ( i = 0; cn[i] != NULL; i++ ) {
367                                         last = strlen( cn[i] ) - 1;
368                                         if ( isdigit( cn[i][last] ) ) {
369                                                 rdn = strdup( cn[i] );
370                                                 break;
371                                         }
372                                 }
373                         }
374                                         
375                         title = ldap_get_values( ld, e, "title" );
376
377                         spaces2dots( rdn );
378                         printf( "  %-20s    %s\r\n", rdn,
379                             title ? title[0] : "" );
380                         if ( title != NULL ) {
381                                 for ( i = 1; title[i] != NULL; i++ )
382                                         printf( "  %-20s    %s\r\n", "",
383                                             title[i] );
384                         }
385                         fflush( stdout );
386
387                         if ( title != NULL )
388                                 ldap_value_free( title );
389
390                         free( dn );
391                 }
392         }
393
394         if ( result != NULL ) {
395                 ldap_msgfree( result );
396         }
397         ldap_unbind( ld );
398 }
399
400
401 static int
402 entry2textwrite( void *fp, char *buf, int len )
403 {
404         return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
405 }
406
407
408 static do_read( ld, e )
409 LDAP            *ld;
410 LDAPMessage     *e;
411 {
412         static struct ldap_disptmpl *tmpllist;
413         static char     *defattrs[] = { "mail", NULL };
414         static char     *mailvals[] = FINGER_NOEMAIL;
415         static char     **defvals[] = { mailvals, NULL };
416
417         ldap_init_templates( templatefile, &tmpllist );
418
419         if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
420             defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
421             LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
422                 ldap_perror( ld, "ldap_entry2text_search" );
423                 exit( 1 );
424         }
425
426         if ( tmpllist != NULL ) {
427             ldap_free_templates( tmpllist );
428         }
429 }