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