3 * Copyright (c) 1990,1994 Regents of the University of Michigan.
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.
18 #include <ac/stdlib.h>
21 #include <ac/signal.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
36 #include "ldap_defaults.h"
40 char *ldaphost = NULL;
44 char *filterfile = FILTERFILE;
45 char *templatefile = TEMPLATEFILE;
46 int rdncount = FINGER_RDNCOUNT;
48 static void do_query(void);
49 static void do_search(LDAP *ld, char *buf);
50 static void do_read(LDAP *ld, LDAPMessage *e);
55 fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-b searchbase] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
60 main( int argc, char **argv )
65 struct sockaddr_in peername;
66 socklen_t peernamelen;
70 while ( (i = getopt( argc, argv, "f:ilp:t:x:p:b:c:" )) != EOF ) {
72 case 'f': /* ldap filter file */
73 filterfile = strdup( optarg );
76 case 'i': /* interactive */
80 case 'l': /* don't do syslogging */
84 case 't': /* ldap template file */
85 templatefile = strdup( optarg );
88 case 'x': /* specify ldap host */
89 ldaphost = strdup( optarg );
92 case 'p': /* specify ldap port */
93 ldapport = atoi( optarg );
96 case 'b': /* specify search base */
97 base = strdup( optarg );
100 case 'c': /* specify number of DN components to show */
101 rdncount = atoi( optarg );
109 if ( !interactive ) {
110 peernamelen = sizeof(peername);
111 if ( getpeername( 0, (struct sockaddr *)&peername,
112 &peernamelen ) != 0 ) {
113 perror( "getpeername" );
114 exit( EXIT_FAILURE );
119 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
120 printf( FINGER_BANNER );
125 if ( (myname = strrchr( argv[0], '/' )) == NULL )
126 myname = strdup( argv[0] );
128 myname = strdup( myname + 1 );
131 (void) SIGNAL( SIGPIPE, SIG_IGN );
136 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
138 openlog( myname, OPENLOG_OPTIONS );
142 if ( dosyslog && !interactive ) {
143 hp = gethostbyaddr( (char *) &peername.sin_addr,
144 sizeof(peername.sin_addr), AF_INET );
145 syslog( LOG_INFO, "connection from %s (%s)",
146 (hp == NULL) ? "unknown" : hp->h_name,
147 inet_ntoa( peername.sin_addr ) );
159 int len, rc, tblsize;
160 struct timeval timeout;
164 if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) {
165 fprintf( stderr, FINGER_UNAVAILABLE );
166 perror( "ldap_init" );
167 exit( EXIT_FAILURE );
171 int limit = FINGER_SIZELIMIT;
172 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &limit);
174 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
176 if ( ldap_simple_bind_s( ld, NULL, NULL )
179 fprintf( stderr, FINGER_UNAVAILABLE );
180 ldap_perror( ld, "ldap_simple_bind_s" );
181 exit( EXIT_FAILURE );
185 tblsize = sysconf( _SC_OPEN_MAX );
186 #elif HAVE_GETDTABLESIZE
187 tblsize = getdtablesize();
189 tblsize = FD_SETSIZE;
193 if (tblsize > FD_SETSIZE) {
194 tblsize = FD_SETSIZE;
196 #endif /* FD_SETSIZE*/
198 timeout.tv_sec = FINGER_TIMEOUT;
201 FD_SET( fileno( stdin ), &readfds );
203 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
207 fprintf( stderr, "connection timed out on input\r\n" );
208 exit( EXIT_FAILURE );
211 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
212 exit( EXIT_FAILURE );
216 /* strip off \r \n */
217 if ( buf[len - 1] == '\n' ) {
221 if ( buf[len - 1] == '\r' ) {
227 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
229 execl( FINGER_CMD, FINGER_CMD, NULL );
233 /* skip and ignore stinking /w */
234 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
240 for ( ; *p && isspace( (unsigned char) *p ); p++ )
248 spaces2dots( char *s )
258 do_search( LDAP *ld, char *buf )
262 int rc, matches, i, ufn;
266 LDAPMessage *result, *e;
267 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
268 #ifdef FINGER_SORT_ATTR
275 if ( strchr( buf, ',' ) != NULL ) {
276 ldap_ufn_setprefix( ld, base );
277 tv.tv_sec = FINGER_TIMEOUT;
279 ldap_ufn_timeout( (void *) &tv );
281 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
282 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
283 fprintf( stderr, FINGER_UNAVAILABLE );
284 ldap_perror( ld, "ldap_search_st" );
285 exit( EXIT_FAILURE );
288 matches = ldap_count_entries( ld, result );
292 if ( (fd = ldap_init_getfilter( filterfile ))
294 fprintf( stderr, "Cannot open filter file (%s)\n",
296 exit( EXIT_FAILURE );
299 for ( fi = ldap_getfirstfilter( fd, "finger", buf );
301 fi = ldap_getnextfilter( fd ) )
303 tv.tv_sec = FINGER_TIMEOUT;
305 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
306 fi->lfi_filter, attrs, 0, &tv, &result ))
307 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
308 && rc != LDAP_TIMELIMIT_EXCEEDED )
310 fprintf( stderr, FINGER_UNAVAILABLE );
311 ldap_perror( ld, "ldap_search_st" );
312 exit( EXIT_FAILURE );
315 if ( (matches = ldap_count_entries( ld, result )) != 0 )
318 ldap_msgfree( result );
325 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
326 printf( "(Partial results - a size limit was exceeded)\r\n" );
327 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
328 printf( "(Partial results - a time limit was exceeded)\r\n" );
331 if ( matches == 0 ) {
332 printf( FINGER_NOMATCH );
334 } else if ( matches < 0 ) {
335 fprintf( stderr, "error return from ldap_count_entries\r\n" );
336 exit( EXIT_FAILURE );
337 } else if ( matches <= FINGER_LISTLIMIT ) {
338 printf( "%d %s match%s found for \"%s\":\r\n", matches,
339 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
342 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
344 e = ldap_next_entry( ld, e );
346 printf( "--------------------\r\n" );
350 printf( "%d %s matches for \"%s\":\r\n", matches,
351 ufn ? "UFN" : fi->lfi_desc, buf );
354 #ifdef FINGER_SORT_ATTR
355 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
358 for ( e = ldap_first_entry( ld, result ); e != NULL;
359 e = ldap_next_entry( ld, e ) ) {
362 dn = ldap_get_dn( ld, e );
364 if ( (p = strchr( dn, ',' )) != NULL )
366 while ( *rdn && *rdn != '=' )
372 for ( i = 0; buf[i] != '\0'; i++ ) {
373 if ( buf[i] == '.' || buf[i] == '_' )
376 if ( strcasecmp( rdn, buf ) == 0 ) {
380 cn = ldap_get_values( ld, e, "cn" );
381 for ( i = 0; cn[i] != NULL; i++ ) {
382 last = strlen( cn[i] ) - 1;
383 if (isdigit((unsigned char) cn[i][last])) {
384 rdn = strdup( cn[i] );
390 title = ldap_get_values( ld, e, "title" );
393 printf( " %-20s %s\r\n", rdn,
394 title ? title[0] : "" );
395 if ( title != NULL ) {
396 for ( i = 1; title[i] != NULL; i++ )
397 printf( " %-20s %s\r\n", "",
403 ldap_value_free( title );
409 if ( result != NULL ) {
410 ldap_msgfree( result );
417 entry2textwrite( void *fp, char *buf, ber_len_t len )
419 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
424 do_read( LDAP *ld, LDAPMessage *e )
426 static struct ldap_disptmpl *tmpllist;
427 static char *defattrs[] = { "mail", NULL };
428 static char *mailvals[] = FINGER_NOEMAIL;
429 static char **defvals[] = { mailvals, NULL };
431 ldap_init_templates( templatefile, &tmpllist );
433 if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
434 defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
435 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
436 ldap_perror( ld, "ldap_entry2text_search" );
437 exit( EXIT_FAILURE );
440 if ( tmpllist != NULL ) {
441 ldap_free_templates( tmpllist );