2 * Copyright (c) 1990,1994 Regents of the University of Michigan.
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.
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
24 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
36 #include "ldapconfig.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] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
60 main( int argc, char **argv )
64 unsigned long mypeer = -1;
66 struct sockaddr_in peername;
71 while ( (i = getopt( argc, argv, "f:ilp:t:x:p:c:" )) != EOF ) {
73 case 'f': /* ldap filter file */
74 filterfile = strdup( optarg );
77 case 'i': /* interactive */
81 case 'l': /* don't do syslogging */
85 case 't': /* ldap template file */
86 templatefile = strdup( optarg );
89 case 'x': /* specify ldap host */
90 ldaphost = strdup( optarg );
93 case 'p': /* specify ldap port */
94 ldapport = atoi( optarg );
97 case 'c': /* specify number of DN components to show */
98 rdncount = atoi( optarg );
106 if ( !interactive ) {
107 peernamelen = sizeof(peername);
108 if ( getpeername( 0, (struct sockaddr *)&peername,
109 &peernamelen ) != 0 ) {
110 perror( "getpeername" );
113 mypeer = (unsigned long) peername.sin_addr.s_addr;
117 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
118 printf( FINGER_BANNER );
123 if ( (myname = strrchr( argv[0], '/' )) == NULL )
124 myname = strdup( argv[0] );
126 myname = strdup( myname + 1 );
130 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
132 openlog( myname, OPENLOG_OPTIONS );
136 if ( dosyslog && mypeer != (unsigned long) -1 ) {
139 hp = gethostbyaddr( (char *) &mypeer, sizeof(mypeer), AF_INET );
140 addr.s_addr = mypeer;
141 syslog( LOG_INFO, "connection from %s (%s)", (hp == NULL) ?
142 "unknown" : hp->h_name, inet_ntoa( addr ) );
154 int len, rc, tblsize;
155 struct timeval timeout;
159 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
160 fprintf( stderr, FINGER_UNAVAILABLE );
161 perror( "ldap_open" );
166 int limit = FINGER_SIZELIMIT;
167 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &limit);
169 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
171 if ( ldap_simple_bind_s( ld, NULL, NULL )
174 fprintf( stderr, FINGER_UNAVAILABLE );
175 ldap_perror( ld, "ldap_simple_bind_s" );
180 tblsize = sysconf( _SC_OPEN_MAX );
181 #elif HAVE_GETDTABLESIZE
182 tblsize = getdtablesize();
184 tblsize = FD_SETSIZE;
188 if (tblsize > FD_SETSIZE) {
189 tblsize = FD_SETSIZE;
191 #endif /* FD_SETSIZE*/
193 timeout.tv_sec = FINGER_TIMEOUT;
196 FD_SET( fileno( stdin ), &readfds );
198 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
202 fprintf( stderr, "connection timed out on input\r\n" );
206 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
211 /* strip off \r \n */
212 if ( buf[len - 1] == '\n' ) {
216 if ( buf[len - 1] == '\r' ) {
222 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
224 execl( FINGER_CMD, FINGER_CMD, NULL );
228 /* skip and ignore stinking /w */
229 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
235 for ( ; *p && isspace( *p ); p++ )
243 spaces2dots( char *s )
253 do_search( LDAP *ld, char *buf )
257 int rc, matches, i, ufn;
261 LDAPMessage *result, *e;
262 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
263 #ifdef FINGER_SORT_ATTR
270 if ( strchr( buf, ',' ) != NULL ) {
271 ldap_ufn_setprefix( ld, base );
272 tv.tv_sec = FINGER_TIMEOUT;
274 ldap_ufn_timeout( (void *) &tv );
276 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
277 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
278 fprintf( stderr, FINGER_UNAVAILABLE );
279 ldap_perror( ld, "ldap_search_st" );
283 matches = ldap_count_entries( ld, result );
287 if ( (fd = ldap_init_getfilter( filterfile ))
289 fprintf( stderr, "Cannot open filter file (%s)\n",
294 for ( fi = ldap_getfirstfilter( fd, "finger", buf );
296 fi = ldap_getnextfilter( fd ) )
298 tv.tv_sec = FINGER_TIMEOUT;
300 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
301 fi->lfi_filter, attrs, 0, &tv, &result ))
302 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
303 && rc != LDAP_TIMELIMIT_EXCEEDED )
305 fprintf( stderr, FINGER_UNAVAILABLE );
306 ldap_perror( ld, "ldap_search_st" );
310 if ( (matches = ldap_count_entries( ld, result )) != 0 )
313 ldap_msgfree( result );
320 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
321 printf( "(Partial results - a size limit was exceeded)\r\n" );
322 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
323 printf( "(Partial results - a time limit was exceeded)\r\n" );
326 if ( matches == 0 ) {
327 printf( FINGER_NOMATCH );
329 } else if ( matches < 0 ) {
330 fprintf( stderr, "error return from ldap_count_entries\r\n" );
332 } else if ( matches <= FINGER_LISTLIMIT ) {
333 printf( "%d %s match%s found for \"%s\":\r\n", matches,
334 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
337 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
339 e = ldap_next_entry( ld, e );
341 printf( "--------------------\r\n" );
345 printf( "%d %s matches for \"%s\":\r\n", matches,
346 ufn ? "UFN" : fi->lfi_desc, buf );
349 #ifdef FINGER_SORT_ATTR
350 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
353 for ( e = ldap_first_entry( ld, result ); e != NULL;
354 e = ldap_next_entry( ld, e ) ) {
357 dn = ldap_get_dn( ld, e );
359 if ( (p = strchr( dn, ',' )) != NULL )
361 while ( *rdn && *rdn != '=' )
367 for ( i = 0; buf[i] != '\0'; i++ ) {
368 if ( buf[i] == '.' || buf[i] == '_' )
371 if ( strcasecmp( rdn, buf ) == 0 ) {
375 cn = ldap_get_values( ld, e, "cn" );
376 for ( i = 0; cn[i] != NULL; i++ ) {
377 last = strlen( cn[i] ) - 1;
378 if ( isdigit( cn[i][last] ) ) {
379 rdn = strdup( cn[i] );
385 title = ldap_get_values( ld, e, "title" );
388 printf( " %-20s %s\r\n", rdn,
389 title ? title[0] : "" );
390 if ( title != NULL ) {
391 for ( i = 1; title[i] != NULL; i++ )
392 printf( " %-20s %s\r\n", "",
398 ldap_value_free( title );
404 if ( result != NULL ) {
405 ldap_msgfree( result );
412 entry2textwrite( void *fp, char *buf, int len )
414 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
419 do_read( LDAP *ld, LDAPMessage *e )
421 static struct ldap_disptmpl *tmpllist;
422 static char *defattrs[] = { "mail", NULL };
423 static char *mailvals[] = FINGER_NOEMAIL;
424 static char **defvals[] = { mailvals, NULL };
426 ldap_init_templates( templatefile, &tmpllist );
428 if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
429 defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
430 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
431 ldap_perror( ld, "ldap_entry2text_search" );
435 if ( tmpllist != NULL ) {
436 ldap_free_templates( tmpllist );