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