]> git.sur5r.net Git - openldap/blob - clients/finger/main.c
merged with autoconf branch
[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 #include "disptmpl.h"
31
32 #include "ldapconfig.h"
33
34
35 int     dosyslog = 1;
36 char    *ldaphost = LDAPHOST;
37 int     ldapport = LDAP_PORT;
38 char    *base = FINGER_BASE;
39 int     deref;
40 char    *filterfile = FILTERFILE;
41 char    *templatefile = TEMPLATEFILE;
42 int     rdncount = FINGER_RDNCOUNT;
43
44 static do_query();
45 static do_search();
46 static do_read();
47 static print_attr();
48
49 static usage( name )
50 char    *name;
51 {
52         fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
53         exit( 1 );
54 }
55
56 main (argc, argv)
57 int     argc;
58 char    **argv;
59 {
60         int                     i;
61         char                    *myname;
62         unsigned long           mypeer = -1;
63         struct hostent          *hp;
64         struct sockaddr_in      peername;
65         int                     peernamelen;
66         int                     interactive = 0;
67         extern char             *optarg;
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( 1 );
111                 }
112                 mypeer = (unsigned long) peername.sin_addr.s_addr;
113         }
114
115 #ifdef FINGER_BANNER
116         if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
117                 printf( FINGER_BANNER );
118                 fflush( stdout );
119         }
120 #endif
121
122         if ( (myname = strrchr( argv[0], '/' )) == NULL )
123                 myname = strdup( argv[0] );
124         else
125                 myname = strdup( myname + 1 );
126
127         if ( dosyslog ) {
128 #ifdef LOG_LOCAL4
129                 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
130 #else
131                 openlog( myname, OPENLOG_OPTIONS );
132 #endif
133         }
134
135         if ( dosyslog && mypeer != -1 ) {
136                 struct in_addr  addr;
137
138                 hp = gethostbyaddr( (char *) &mypeer, sizeof(mypeer), AF_INET );
139                 addr.s_addr = mypeer;
140                 syslog( LOG_INFO, "connection from %s (%s)", (hp == NULL) ?
141                     "unknown" : hp->h_name, inet_ntoa( addr ) );
142         }
143
144         do_query();
145
146         return( 0 );
147 }
148
149 static do_query()
150 {
151         char            buf[256];
152         int             len, rc, tblsize;
153         struct timeval  timeout;
154         fd_set          readfds;
155         LDAP            *ld;
156
157         if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
158                 fprintf( stderr, FINGER_UNAVAILABLE );
159                 perror( "ldap_open" );
160                 exit( 1 );
161         }
162         ld->ld_sizelimit = FINGER_SIZELIMIT;
163         ld->ld_deref = deref;
164
165         if ( ldap_simple_bind_s( ld, FINGER_BINDDN, FINGER_BIND_CRED )
166                 != LDAP_SUCCESS )
167         {
168                 fprintf( stderr, FINGER_UNAVAILABLE );
169                 ldap_perror( ld, "ldap_simple_bind_s" );
170                 exit( 1 );
171         }
172
173 #ifdef HAVE_SYSCONF
174         tblsize = sysconf( _SC_OPEN_MAX );
175 #elif HAVE_GETDTABLESIZE
176         tblsize = getdtablesize();
177 #else
178         tblsize = FD_SETSIZE;
179 #endif
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 }