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>
35 #include "ldapconfig.h"
39 char *ldaphost = NULL;
43 char *filterfile = FILTERFILE;
44 char *templatefile = TEMPLATEFILE;
45 int rdncount = FINGER_RDNCOUNT;
47 static void do_query(void);
48 static void do_search(LDAP *ld, char *buf);
49 static void do_read(LDAP *ld, LDAPMessage *e);
54 fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
59 main( int argc, char **argv )
64 struct sockaddr_in peername;
69 while ( (i = getopt( argc, argv, "f:ilp:t:x:p:c:" )) != EOF ) {
71 case 'f': /* ldap filter file */
72 filterfile = strdup( optarg );
75 case 'i': /* interactive */
79 case 'l': /* don't do syslogging */
83 case 't': /* ldap template file */
84 templatefile = strdup( optarg );
87 case 'x': /* specify ldap host */
88 ldaphost = strdup( optarg );
91 case 'p': /* specify ldap port */
92 ldapport = atoi( optarg );
95 case 'c': /* specify number of DN components to show */
96 rdncount = atoi( optarg );
104 if ( !interactive ) {
105 peernamelen = sizeof(peername);
106 if ( getpeername( 0, (struct sockaddr *)&peername,
107 &peernamelen ) != 0 ) {
108 perror( "getpeername" );
114 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
115 printf( FINGER_BANNER );
120 if ( (myname = strrchr( argv[0], '/' )) == NULL )
121 myname = strdup( argv[0] );
123 myname = strdup( myname + 1 );
126 (void) SIGNAL( SIGPIPE, SIG_IGN );
131 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
133 openlog( myname, OPENLOG_OPTIONS );
137 if ( dosyslog && !interactive ) {
138 hp = gethostbyaddr( (char *) &peername.sin_addr,
139 sizeof(peername.sin_addr), AF_INET );
140 syslog( LOG_INFO, "connection from %s (%s)",
141 (hp == NULL) ? "unknown" : hp->h_name,
142 inet_ntoa( peername.sin_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" );
164 ld->ld_sizelimit = FINGER_SIZELIMIT;
165 ld->ld_deref = deref;
167 if ( ldap_simple_bind_s( ld, NULL, NULL )
170 fprintf( stderr, FINGER_UNAVAILABLE );
171 ldap_perror( ld, "ldap_simple_bind_s" );
176 tblsize = sysconf( _SC_OPEN_MAX );
177 #elif HAVE_GETDTABLESIZE
178 tblsize = getdtablesize();
180 tblsize = FD_SETSIZE;
184 if (tblsize > FD_SETSIZE) {
185 tblsize = FD_SETSIZE;
187 #endif /* FD_SETSIZE*/
189 timeout.tv_sec = FINGER_TIMEOUT;
192 FD_SET( fileno( stdin ), &readfds );
194 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
198 fprintf( stderr, "connection timed out on input\r\n" );
202 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
207 /* strip off \r \n */
208 if ( buf[len - 1] == '\n' ) {
212 if ( buf[len - 1] == '\r' ) {
218 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
220 execl( FINGER_CMD, FINGER_CMD, NULL );
224 /* skip and ignore stinking /w */
225 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
231 for ( ; *p && isspace( (unsigned char) *p ); p++ )
239 spaces2dots( char *s )
249 do_search( LDAP *ld, char *buf )
253 int rc, matches, i, ufn;
256 LDAPMessage *result, *e;
257 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
258 #ifdef FINGER_SORT_ATTR
265 if ( strchr( buf, ',' ) != NULL ) {
266 ldap_ufn_setprefix( ld, base );
267 tv.tv_sec = FINGER_TIMEOUT;
269 ldap_ufn_timeout( (void *) &tv );
271 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
272 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
273 fprintf( stderr, FINGER_UNAVAILABLE );
274 ldap_perror( ld, "ldap_search_st" );
278 matches = ldap_count_entries( ld, result );
282 if ( (ld->ld_filtd = ldap_init_getfilter( filterfile ))
284 fprintf( stderr, "Cannot open filter file (%s)\n",
289 for ( fi = ldap_getfirstfilter( ld->ld_filtd, "finger", buf );
291 fi = ldap_getnextfilter( ld->ld_filtd ) )
293 tv.tv_sec = FINGER_TIMEOUT;
295 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
296 fi->lfi_filter, attrs, 0, &tv, &result ))
297 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
298 && rc != LDAP_TIMELIMIT_EXCEEDED )
300 fprintf( stderr, FINGER_UNAVAILABLE );
301 ldap_perror( ld, "ldap_search_st" );
305 if ( (matches = ldap_count_entries( ld, result )) != 0 )
308 ldap_msgfree( result );
315 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
316 printf( "(Partial results - a size limit was exceeded)\r\n" );
317 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
318 printf( "(Partial results - a time limit was exceeded)\r\n" );
321 if ( matches == 0 ) {
322 printf( FINGER_NOMATCH );
324 } else if ( matches < 0 ) {
325 fprintf( stderr, "error return from ldap_count_entries\r\n" );
327 } else if ( matches <= FINGER_LISTLIMIT ) {
328 printf( "%d %s match%s found for \"%s\":\r\n", matches,
329 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
332 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
334 e = ldap_next_entry( ld, e );
336 printf( "--------------------\r\n" );
340 printf( "%d %s matches for \"%s\":\r\n", matches,
341 ufn ? "UFN" : fi->lfi_desc, buf );
344 #ifdef FINGER_SORT_ATTR
345 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
348 for ( e = ldap_first_entry( ld, result ); e != NULL;
349 e = ldap_next_entry( ld, e ) ) {
352 dn = ldap_get_dn( ld, e );
354 if ( (p = strchr( dn, ',' )) != NULL )
356 while ( *rdn && *rdn != '=' )
362 for ( i = 0; buf[i] != '\0'; i++ ) {
363 if ( buf[i] == '.' || buf[i] == '_' )
366 if ( strcasecmp( rdn, buf ) == 0 ) {
370 cn = ldap_get_values( ld, e, "cn" );
371 for ( i = 0; cn[i] != NULL; i++ ) {
372 last = strlen( cn[i] ) - 1;
373 if (isdigit((unsigned char) cn[i][last])) {
374 rdn = strdup( cn[i] );
380 title = ldap_get_values( ld, e, "title" );
383 printf( " %-20s %s\r\n", rdn,
384 title ? title[0] : "" );
385 if ( title != NULL ) {
386 for ( i = 1; title[i] != NULL; i++ )
387 printf( " %-20s %s\r\n", "",
393 ldap_value_free( title );
399 if ( result != NULL ) {
400 ldap_msgfree( result );
407 entry2textwrite( void *fp, char *buf, int len )
409 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
414 do_read( LDAP *ld, LDAPMessage *e )
416 static struct ldap_disptmpl *tmpllist;
417 static char *defattrs[] = { "mail", NULL };
418 static char *mailvals[] = FINGER_NOEMAIL;
419 static char **defvals[] = { mailvals, NULL };
421 ldap_init_templates( templatefile, &tmpllist );
423 if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
424 defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
425 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
426 ldap_perror( ld, "ldap_entry2text_search" );
430 if ( tmpllist != NULL ) {
431 ldap_free_templates( tmpllist );