]> git.sur5r.net Git - openldap/blob - clients/finger/main.c
Changed FD_SETSIZE checks for consistency. Added checks where needed.
[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 #ifdef FD_SETSIZE
182         if (tblsize > FD_SETSIZE) {
183                 tblsize = FD_SETSIZE;
184         }
185 #endif  /* FD_SETSIZE*/
186
187         timeout.tv_sec = FINGER_TIMEOUT;
188         timeout.tv_usec = 0;
189         FD_ZERO( &readfds );
190         FD_SET( fileno( stdin ), &readfds );
191
192         if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
193                 if ( rc < 0 )
194                         perror( "select" );
195                 else
196                         fprintf( stderr, "connection timed out on input\r\n" );
197                 exit( 1 );
198         }
199
200         if ( fgets( buf, sizeof(buf), stdin ) == NULL )
201                 exit( 1 );
202
203         len = strlen( buf );
204
205         /* strip off \r \n */
206         if ( buf[len - 1] == '\n' ) {
207                 buf[len - 1] = '\0';
208                 len--;
209         }
210         if ( buf[len - 1] == '\r' ) {
211                 buf[len - 1] = '\0';
212                 len--;
213         }
214
215         if ( len == 0 ) {
216                 printf( "No campus-wide login information available.  Info for this machine only:\r\n" );
217                 fflush( stdout );
218                 execl( FINGER_CMD, FINGER_CMD, NULL );
219         } else {
220                 char    *p;
221
222                 /* skip and ignore stinking /w */
223                 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
224                         p = buf + 2;
225                 } else {
226                         p = buf;
227                 }
228
229                 for ( ; *p && isspace( *p ); p++ )
230                         ;       /* NULL */
231
232                 do_search( ld, p );
233         }
234 }
235
236 static void
237 spaces2dots( s )
238     char        *s;
239 {
240         for ( ; *s; s++ ) {
241                 if ( *s == ' ' ) {
242                         *s = '.';
243                 }
244         }
245 }
246
247 static do_search( ld, buf )
248 LDAP    *ld;
249 char    *buf;
250 {
251         char            *dn, *rdn;
252         char            **title;
253         int             rc, matches, i, ufn;
254         struct timeval  tv;
255         LDAPFiltInfo    *fi;
256         LDAPMessage     *result, *e;
257         static char     *attrs[] = { "cn", "title", "objectClass", "joinable",
258 #ifdef FINGER_SORT_ATTR
259                                         FINGER_SORT_ATTR,
260 #endif
261                                         0 };
262         extern int      strcasecmp();
263
264         ufn = 0;
265 #ifdef FINGER_UFN
266         if ( strchr( buf, ',' ) != NULL ) {
267                 ldap_ufn_setprefix( ld, base );
268                 tv.tv_sec = FINGER_TIMEOUT;
269                 tv.tv_usec = 0;
270                 ldap_ufn_timeout( (void *) &tv );
271
272                 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
273                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
274                         fprintf( stderr, FINGER_UNAVAILABLE );
275                         ldap_perror( ld, "ldap_search_st" );
276                         exit( 1 );
277                 }
278
279                 matches = ldap_count_entries( ld, result );
280                 ufn = 1;
281         } else {
282 #endif
283                 if ( (ld->ld_filtd = ldap_init_getfilter( filterfile ))
284                     == NULL ) {
285                         fprintf( stderr, "Cannot open filter file (%s)\n",
286                             filterfile );
287                         exit( 1 );
288                 }
289
290                 for ( fi = ldap_getfirstfilter( ld->ld_filtd, "finger", buf );
291                     fi != NULL;
292                     fi = ldap_getnextfilter( ld->ld_filtd ) )
293                 {
294                         tv.tv_sec = FINGER_TIMEOUT;
295                         tv.tv_usec = 0;
296                         if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
297                             fi->lfi_filter, attrs, 0, &tv, &result ))
298                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
299                             && rc != LDAP_TIMELIMIT_EXCEEDED )
300                         {
301                                 fprintf( stderr, FINGER_UNAVAILABLE );
302                                 ldap_perror( ld, "ldap_search_st" );
303                                 exit( 1 );
304                         }
305
306                         if ( (matches = ldap_count_entries( ld, result )) != 0 )
307                                 break;
308
309                         ldap_msgfree( result );
310                         result = NULL;
311                 }
312 #ifdef FINGER_UFN
313         }
314 #endif
315
316         if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
317                 printf( "(Partial results - a size limit was exceeded)\r\n" );
318         } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
319                 printf( "(Partial results - a time limit was exceeded)\r\n" );
320         }
321
322         if ( matches == 0 ) {
323                 printf( FINGER_NOMATCH );
324                 fflush( stdout );
325         } else if ( matches < 0 ) {
326                 fprintf( stderr, "error return from ldap_count_entries\r\n" );
327                 exit( 1 );
328         } else if ( matches <= FINGER_LISTLIMIT ) {
329                 printf( "%d %s match%s found for \"%s\":\r\n", matches,
330                     ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
331                 fflush( stdout );
332
333                 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
334                         do_read( ld, e );
335                         e = ldap_next_entry( ld, e );
336                         if ( e != NULL ) {
337                                 printf( "--------------------\r\n" );
338                         }
339                 }
340         } else {
341                 printf( "%d %s matches for \"%s\":\r\n", matches,
342                     ufn ? "UFN" : fi->lfi_desc, buf );
343                 fflush( stdout );
344
345 #ifdef FINGER_SORT_ATTR
346                 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
347 #endif
348
349                 for ( e = ldap_first_entry( ld, result ); e != NULL;
350                     e = ldap_next_entry( ld, e ) ) {
351                         char    *p;
352
353                         dn = ldap_get_dn( ld, e );
354                         rdn = dn;
355                         if ( (p = strchr( dn, ',' )) != NULL )
356                                 *p = '\0';
357                         while ( *rdn && *rdn != '=' )
358                                 rdn++;
359                         if ( *rdn )
360                                 rdn++;
361
362                         /* hack attack */
363                         for ( i = 0; buf[i] != '\0'; i++ ) {
364                                 if ( buf[i] == '.' || buf[i] == '_' )
365                                         buf[i] = ' ';
366                         }
367                         if ( strcasecmp( rdn, buf ) == 0 ) {
368                                 char    **cn;
369                                 int     i, last;
370
371                                 cn = ldap_get_values( ld, e, "cn" );
372                                 for ( i = 0; cn[i] != NULL; i++ ) {
373                                         last = strlen( cn[i] ) - 1;
374                                         if ( isdigit( cn[i][last] ) ) {
375                                                 rdn = strdup( cn[i] );
376                                                 break;
377                                         }
378                                 }
379                         }
380                                         
381                         title = ldap_get_values( ld, e, "title" );
382
383                         spaces2dots( rdn );
384                         printf( "  %-20s    %s\r\n", rdn,
385                             title ? title[0] : "" );
386                         if ( title != NULL ) {
387                                 for ( i = 1; title[i] != NULL; i++ )
388                                         printf( "  %-20s    %s\r\n", "",
389                                             title[i] );
390                         }
391                         fflush( stdout );
392
393                         if ( title != NULL )
394                                 ldap_value_free( title );
395
396                         free( dn );
397                 }
398         }
399
400         if ( result != NULL ) {
401                 ldap_msgfree( result );
402         }
403         ldap_unbind( ld );
404 }
405
406
407 static int
408 entry2textwrite( void *fp, char *buf, int len )
409 {
410         return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
411 }
412
413
414 static do_read( ld, e )
415 LDAP            *ld;
416 LDAPMessage     *e;
417 {
418         static struct ldap_disptmpl *tmpllist;
419         static char     *defattrs[] = { "mail", NULL };
420         static char     *mailvals[] = FINGER_NOEMAIL;
421         static char     **defvals[] = { mailvals, NULL };
422
423         ldap_init_templates( templatefile, &tmpllist );
424
425         if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
426             defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
427             LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
428                 ldap_perror( ld, "ldap_entry2text_search" );
429                 exit( 1 );
430         }
431
432         if ( tmpllist != NULL ) {
433             ldap_free_templates( tmpllist );
434         }
435 }