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 <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
26 #include <sys/resource.h>
29 #include <sys/select.h>
33 #include "ldapconfig.h"
37 #endif /* USE_SYSCONF */
40 char *ldaphost = LDAPHOST;
41 int ldapport = LDAP_PORT;
42 char *base = FINGER_BASE;
44 char *filterfile = FILTERFILE;
45 char *templatefile = TEMPLATEFILE;
46 int rdncount = FINGER_RDNCOUNT;
56 fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
66 unsigned long mypeer = -1;
68 struct sockaddr_in peername;
74 while ( (i = getopt( argc, argv, "f:ilp:t:x:p:c:" )) != EOF ) {
76 case 'f': /* ldap filter file */
77 filterfile = strdup( optarg );
80 case 'i': /* interactive */
84 case 'l': /* don't do syslogging */
88 case 't': /* ldap template file */
89 templatefile = strdup( optarg );
92 case 'x': /* specify ldap host */
93 ldaphost = strdup( optarg );
96 case 'p': /* specify ldap port */
97 ldapport = atoi( 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" );
116 mypeer = (unsigned long) peername.sin_addr.s_addr;
120 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
121 printf( FINGER_BANNER );
126 if ( (myname = strrchr( argv[0], '/' )) == NULL )
127 myname = strdup( argv[0] );
129 myname = strdup( myname + 1 );
133 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
135 openlog( myname, OPENLOG_OPTIONS );
139 if ( dosyslog && mypeer != -1 ) {
142 hp = gethostbyaddr( (char *) &mypeer, sizeof(mypeer), AF_INET );
143 addr.s_addr = mypeer;
144 syslog( LOG_INFO, "connection from %s (%s)", (hp == NULL) ?
145 "unknown" : hp->h_name, inet_ntoa( addr ) );
156 int len, rc, tblsize;
157 struct timeval timeout;
161 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
162 fprintf( stderr, FINGER_UNAVAILABLE );
163 perror( "ldap_open" );
166 ld->ld_sizelimit = FINGER_SIZELIMIT;
167 ld->ld_deref = deref;
169 if ( ldap_simple_bind_s( ld, FINGER_BINDDN, NULL ) != LDAP_SUCCESS ) {
170 fprintf( stderr, FINGER_UNAVAILABLE );
171 ldap_perror( ld, "ldap_simple_bind_s" );
176 tblsize = sysconf( _SC_OPEN_MAX );
177 #else /* USE_SYSCONF */
178 tblsize = getdtablesize();
179 #endif /* USE_SYSCONF */
181 timeout.tv_sec = FINGER_TIMEOUT;
184 FD_SET( fileno( stdin ), &readfds );
186 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
190 fprintf( stderr, "connection timed out on input\r\n" );
194 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
199 /* strip off \r \n */
200 if ( buf[len - 1] == '\n' ) {
204 if ( buf[len - 1] == '\r' ) {
210 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
212 execl( FINGER_CMD, FINGER_CMD, NULL );
216 /* skip and ignore stinking /w */
217 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
223 for ( ; *p && isspace( *p ); p++ )
241 static do_search( ld, buf )
247 int rc, matches, i, ufn;
250 LDAPMessage *result, *e;
251 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
252 #ifdef FINGER_SORT_ATTR
256 extern int strcasecmp();
260 if ( strchr( buf, ',' ) != NULL ) {
261 ldap_ufn_setprefix( ld, base );
262 tv.tv_sec = FINGER_TIMEOUT;
264 ldap_ufn_timeout( (void *) &tv );
266 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
267 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
268 fprintf( stderr, FINGER_UNAVAILABLE );
269 ldap_perror( ld, "ldap_search_st" );
273 matches = ldap_count_entries( ld, result );
277 if ( (ld->ld_filtd = ldap_init_getfilter( filterfile ))
279 fprintf( stderr, "Cannot open filter file (%s)\n",
284 for ( fi = ldap_getfirstfilter( ld->ld_filtd, "finger", buf );
286 fi = ldap_getnextfilter( ld->ld_filtd ) )
288 tv.tv_sec = FINGER_TIMEOUT;
290 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
291 fi->lfi_filter, attrs, 0, &tv, &result ))
292 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
293 && rc != LDAP_TIMELIMIT_EXCEEDED )
295 fprintf( stderr, FINGER_UNAVAILABLE );
296 ldap_perror( ld, "ldap_search_st" );
300 if ( (matches = ldap_count_entries( ld, result )) != 0 )
303 ldap_msgfree( result );
310 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
311 printf( "(Partial results - a size limit was exceeded)\r\n" );
312 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
313 printf( "(Partial results - a time limit was exceeded)\r\n" );
316 if ( matches == 0 ) {
317 printf( FINGER_NOMATCH );
319 } else if ( matches < 0 ) {
320 fprintf( stderr, "error return from ldap_count_entries\r\n" );
322 } else if ( matches <= FINGER_LISTLIMIT ) {
323 printf( "%d %s match%s found for \"%s\":\r\n", matches,
324 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
327 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
329 e = ldap_next_entry( ld, e );
331 printf( "--------------------\r\n" );
335 printf( "%d %s matches for \"%s\":\r\n", matches,
336 ufn ? "UFN" : fi->lfi_desc, buf );
339 #ifdef FINGER_SORT_ATTR
340 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
343 for ( e = ldap_first_entry( ld, result ); e != NULL;
344 e = ldap_next_entry( ld, e ) ) {
347 dn = ldap_get_dn( ld, e );
349 if ( (p = strchr( dn, ',' )) != NULL )
351 while ( *rdn && *rdn != '=' )
357 for ( i = 0; buf[i] != '\0'; i++ ) {
358 if ( buf[i] == '.' || buf[i] == '_' )
361 if ( strcasecmp( rdn, buf ) == 0 ) {
365 cn = ldap_get_values( ld, e, "cn" );
366 for ( i = 0; cn[i] != NULL; i++ ) {
367 last = strlen( cn[i] ) - 1;
368 if ( isdigit( cn[i][last] ) ) {
369 rdn = strdup( cn[i] );
375 title = ldap_get_values( ld, e, "title" );
378 printf( " %-20s %s\r\n", rdn,
379 title ? title[0] : "" );
380 if ( title != NULL ) {
381 for ( i = 1; title[i] != NULL; i++ )
382 printf( " %-20s %s\r\n", "",
388 ldap_value_free( title );
394 if ( result != NULL ) {
395 ldap_msgfree( result );
402 entry2textwrite( void *fp, char *buf, int len )
404 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
408 static do_read( ld, e )
412 static struct ldap_disptmpl *tmpllist;
413 static char *defattrs[] = { "mail", NULL };
414 static char *mailvals[] = FINGER_NOEMAIL;
415 static char **defvals[] = { mailvals, NULL };
417 ldap_init_templates( templatefile, &tmpllist );
419 if ( ldap_entry2text_search( ld, NULL, base, e, tmpllist, defattrs,
420 defvals, entry2textwrite, (void *)stdout, "\r\n", rdncount,
421 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
422 ldap_perror( ld, "ldap_entry2text_search" );
426 if ( tmpllist != NULL ) {
427 ldap_free_templates( tmpllist );