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 )
65 struct sockaddr_in peername;
70 while ( (i = getopt( argc, argv, "f:ilp:t:x:p: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 'c': /* specify number of DN components to show */
97 rdncount = atoi( optarg );
105 if ( !interactive ) {
106 peernamelen = sizeof(peername);
107 if ( getpeername( 0, (struct sockaddr *)&peername,
108 &peernamelen ) != 0 ) {
109 perror( "getpeername" );
115 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
116 printf( FINGER_BANNER );
121 if ( (myname = strrchr( argv[0], '/' )) == NULL )
122 myname = strdup( argv[0] );
124 myname = strdup( myname + 1 );
128 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
130 openlog( myname, OPENLOG_OPTIONS );
134 if ( dosyslog && !interactive ) {
135 hp = gethostbyaddr( (char *) &peername.sin_addr.s_addr,
136 sizeof(peername.sin_addr.s_addr), AF_INET );
137 syslog( LOG_INFO, "connection from %s (%s)",
138 (hp == NULL) ? "unknown" : hp->h_name,
139 inet_ntoa( peername.sin_addr ) );
151 int len, rc, tblsize;
152 struct timeval timeout;
156 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
157 fprintf( stderr, FINGER_UNAVAILABLE );
158 perror( "ldap_open" );
163 int limit = FINGER_SIZELIMIT;
164 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &limit);
166 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
168 if ( ldap_simple_bind_s( ld, NULL, NULL )
171 fprintf( stderr, FINGER_UNAVAILABLE );
172 ldap_perror( ld, "ldap_simple_bind_s" );
177 tblsize = sysconf( _SC_OPEN_MAX );
178 #elif HAVE_GETDTABLESIZE
179 tblsize = getdtablesize();
181 tblsize = FD_SETSIZE;
185 if (tblsize > FD_SETSIZE) {
186 tblsize = FD_SETSIZE;
188 #endif /* FD_SETSIZE*/
190 timeout.tv_sec = FINGER_TIMEOUT;
193 FD_SET( fileno( stdin ), &readfds );
195 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
199 fprintf( stderr, "connection timed out on input\r\n" );
203 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
208 /* strip off \r \n */
209 if ( buf[len - 1] == '\n' ) {
213 if ( buf[len - 1] == '\r' ) {
219 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
221 execl( FINGER_CMD, FINGER_CMD, NULL );
225 /* skip and ignore stinking /w */
226 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
232 for ( ; *p && isspace( *p ); p++ )
240 spaces2dots( char *s )
250 do_search( LDAP *ld, char *buf )
254 int rc, matches, i, ufn;
258 LDAPMessage *result, *e;
259 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
260 #ifdef FINGER_SORT_ATTR
267 if ( strchr( buf, ',' ) != NULL ) {
268 ldap_ufn_setprefix( ld, base );
269 tv.tv_sec = FINGER_TIMEOUT;
271 ldap_ufn_timeout( (void *) &tv );
273 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
274 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
275 fprintf( stderr, FINGER_UNAVAILABLE );
276 ldap_perror( ld, "ldap_search_st" );
280 matches = ldap_count_entries( ld, result );
284 if ( (fd = ldap_init_getfilter( filterfile ))
286 fprintf( stderr, "Cannot open filter file (%s)\n",
291 for ( fi = ldap_getfirstfilter( fd, "finger", buf );
293 fi = ldap_getnextfilter( fd ) )
295 tv.tv_sec = FINGER_TIMEOUT;
297 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
298 fi->lfi_filter, attrs, 0, &tv, &result ))
299 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
300 && rc != LDAP_TIMELIMIT_EXCEEDED )
302 fprintf( stderr, FINGER_UNAVAILABLE );
303 ldap_perror( ld, "ldap_search_st" );
307 if ( (matches = ldap_count_entries( ld, result )) != 0 )
310 ldap_msgfree( result );
317 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
318 printf( "(Partial results - a size limit was exceeded)\r\n" );
319 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
320 printf( "(Partial results - a time limit was exceeded)\r\n" );
323 if ( matches == 0 ) {
324 printf( FINGER_NOMATCH );
326 } else if ( matches < 0 ) {
327 fprintf( stderr, "error return from ldap_count_entries\r\n" );
329 } else if ( matches <= FINGER_LISTLIMIT ) {
330 printf( "%d %s match%s found for \"%s\":\r\n", matches,
331 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
334 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
336 e = ldap_next_entry( ld, e );
338 printf( "--------------------\r\n" );
342 printf( "%d %s matches for \"%s\":\r\n", matches,
343 ufn ? "UFN" : fi->lfi_desc, buf );
346 #ifdef FINGER_SORT_ATTR
347 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
350 for ( e = ldap_first_entry( ld, result ); e != NULL;
351 e = ldap_next_entry( ld, e ) ) {
354 dn = ldap_get_dn( ld, e );
356 if ( (p = strchr( dn, ',' )) != NULL )
358 while ( *rdn && *rdn != '=' )
364 for ( i = 0; buf[i] != '\0'; i++ ) {
365 if ( buf[i] == '.' || buf[i] == '_' )
368 if ( strcasecmp( rdn, buf ) == 0 ) {
372 cn = ldap_get_values( ld, e, "cn" );
373 for ( i = 0; cn[i] != NULL; i++ ) {
374 last = strlen( cn[i] ) - 1;
375 if ( isdigit( cn[i][last] ) ) {
376 rdn = strdup( cn[i] );
382 title = ldap_get_values( ld, e, "title" );
385 printf( " %-20s %s\r\n", rdn,
386 title ? title[0] : "" );
387 if ( title != NULL ) {
388 for ( i = 1; title[i] != NULL; i++ )
389 printf( " %-20s %s\r\n", "",
395 ldap_value_free( title );
401 if ( result != NULL ) {
402 ldap_msgfree( result );
409 entry2textwrite( void *fp, char *buf, int len )
411 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
416 do_read( LDAP *ld, LDAPMessage *e )
418 static struct ldap_disptmpl *tmpllist;
419 static char *defattrs[] = { "mail", NULL };
420 static char *mailvals[] = FINGER_NOEMAIL;
421 static char **defvals[] = { mailvals, NULL };
423 ldap_init_templates( templatefile, &tmpllist );
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" );
432 if ( tmpllist != NULL ) {
433 ldap_free_templates( tmpllist );