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] [-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: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" );
110 exit( EXIT_FAILURE );
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 );
127 (void) SIGNAL( SIGPIPE, SIG_IGN );
132 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
134 openlog( myname, OPENLOG_OPTIONS );
138 if ( dosyslog && !interactive ) {
139 hp = gethostbyaddr( (char *) &peername.sin_addr,
140 sizeof(peername.sin_addr), AF_INET );
141 syslog( LOG_INFO, "connection from %s (%s)",
142 (hp == NULL) ? "unknown" : hp->h_name,
143 inet_ntoa( peername.sin_addr ) );
155 int len, rc, tblsize;
156 struct timeval timeout;
160 if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) {
161 fprintf( stderr, FINGER_UNAVAILABLE );
162 perror( "ldap_init" );
163 exit( EXIT_FAILURE );
167 int limit = FINGER_SIZELIMIT;
168 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &limit);
170 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
172 if ( ldap_simple_bind_s( ld, NULL, NULL )
175 fprintf( stderr, FINGER_UNAVAILABLE );
176 ldap_perror( ld, "ldap_simple_bind_s" );
177 exit( EXIT_FAILURE );
181 tblsize = sysconf( _SC_OPEN_MAX );
182 #elif HAVE_GETDTABLESIZE
183 tblsize = getdtablesize();
185 tblsize = FD_SETSIZE;
189 if (tblsize > FD_SETSIZE) {
190 tblsize = FD_SETSIZE;
192 #endif /* FD_SETSIZE*/
194 timeout.tv_sec = FINGER_TIMEOUT;
197 FD_SET( fileno( stdin ), &readfds );
199 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
203 fprintf( stderr, "connection timed out on input\r\n" );
204 exit( EXIT_FAILURE );
207 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
208 exit( EXIT_FAILURE );
212 /* strip off \r \n */
213 if ( buf[len - 1] == '\n' ) {
217 if ( buf[len - 1] == '\r' ) {
223 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
225 execl( FINGER_CMD, FINGER_CMD, NULL );
229 /* skip and ignore stinking /w */
230 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
236 for ( ; *p && isspace( (unsigned char) *p ); p++ )
244 spaces2dots( char *s )
254 do_search( LDAP *ld, char *buf )
258 int rc, matches, i, ufn;
262 LDAPMessage *result, *e;
263 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
264 #ifdef FINGER_SORT_ATTR
271 if ( strchr( buf, ',' ) != NULL ) {
272 ldap_ufn_setprefix( ld, base );
273 tv.tv_sec = FINGER_TIMEOUT;
275 ldap_ufn_timeout( (void *) &tv );
277 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
278 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
279 fprintf( stderr, FINGER_UNAVAILABLE );
280 ldap_perror( ld, "ldap_search_st" );
281 exit( EXIT_FAILURE );
284 matches = ldap_count_entries( ld, result );
288 if ( (fd = ldap_init_getfilter( filterfile ))
290 fprintf( stderr, "Cannot open filter file (%s)\n",
292 exit( EXIT_FAILURE );
295 for ( fi = ldap_getfirstfilter( fd, "finger", buf );
297 fi = ldap_getnextfilter( fd ) )
299 tv.tv_sec = FINGER_TIMEOUT;
301 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
302 fi->lfi_filter, attrs, 0, &tv, &result ))
303 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
304 && rc != LDAP_TIMELIMIT_EXCEEDED )
306 fprintf( stderr, FINGER_UNAVAILABLE );
307 ldap_perror( ld, "ldap_search_st" );
308 exit( EXIT_FAILURE );
311 if ( (matches = ldap_count_entries( ld, result )) != 0 )
314 ldap_msgfree( result );
321 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
322 printf( "(Partial results - a size limit was exceeded)\r\n" );
323 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
324 printf( "(Partial results - a time limit was exceeded)\r\n" );
327 if ( matches == 0 ) {
328 printf( FINGER_NOMATCH );
330 } else if ( matches < 0 ) {
331 fprintf( stderr, "error return from ldap_count_entries\r\n" );
332 exit( EXIT_FAILURE );
333 } else if ( matches <= FINGER_LISTLIMIT ) {
334 printf( "%d %s match%s found for \"%s\":\r\n", matches,
335 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
338 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
340 e = ldap_next_entry( ld, e );
342 printf( "--------------------\r\n" );
346 printf( "%d %s matches for \"%s\":\r\n", matches,
347 ufn ? "UFN" : fi->lfi_desc, buf );
350 #ifdef FINGER_SORT_ATTR
351 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
354 for ( e = ldap_first_entry( ld, result ); e != NULL;
355 e = ldap_next_entry( ld, e ) ) {
358 dn = ldap_get_dn( ld, e );
360 if ( (p = strchr( dn, ',' )) != NULL )
362 while ( *rdn && *rdn != '=' )
368 for ( i = 0; buf[i] != '\0'; i++ ) {
369 if ( buf[i] == '.' || buf[i] == '_' )
372 if ( strcasecmp( rdn, buf ) == 0 ) {
376 cn = ldap_get_values( ld, e, "cn" );
377 for ( i = 0; cn[i] != NULL; i++ ) {
378 last = strlen( cn[i] ) - 1;
379 if (isdigit((unsigned char) cn[i][last])) {
380 rdn = strdup( cn[i] );
386 title = ldap_get_values( ld, e, "title" );
389 printf( " %-20s %s\r\n", rdn,
390 title ? title[0] : "" );
391 if ( title != NULL ) {
392 for ( i = 1; title[i] != NULL; i++ )
393 printf( " %-20s %s\r\n", "",
399 ldap_value_free( title );
405 if ( result != NULL ) {
406 ldap_msgfree( result );
413 entry2textwrite( void *fp, char *buf, ber_len_t len )
415 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
420 do_read( LDAP *ld, LDAPMessage *e )
422 static struct ldap_disptmpl *tmpllist;
423 static char *defattrs[] = { "mail", NULL };
424 static char *mailvals[] = FINGER_NOEMAIL;
425 static char **defvals[] = { mailvals, NULL };
427 ldap_init_templates( templatefile, &tmpllist );
429 if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
430 defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
431 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
432 ldap_perror( ld, "ldap_entry2text_search" );
433 exit( EXIT_FAILURE );
436 if ( tmpllist != NULL ) {
437 ldap_free_templates( tmpllist );