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/socket.h>
20 #include <ac/string.h>
21 #include <ac/syslog.h>
23 #include <ac/unistd.h>
26 #include <sys/resource.h>
32 #include "ldapconfig.h"
36 char *ldaphost = LDAPHOST;
37 int ldapport = LDAP_PORT;
38 char *base = FINGER_BASE;
40 char *filterfile = FILTERFILE;
41 char *templatefile = TEMPLATEFILE;
42 int rdncount = FINGER_RDNCOUNT;
52 fprintf( stderr, "usage: %s [-l] [-x ldaphost] [-p ldapport] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
62 unsigned long mypeer = -1;
64 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" );
112 mypeer = (unsigned long) peername.sin_addr.s_addr;
116 if ( FINGER_BANNER != NULL && strcmp( FINGER_BANNER, "" ) != 0 ) {
117 printf( FINGER_BANNER );
122 if ( (myname = strrchr( argv[0], '/' )) == NULL )
123 myname = strdup( argv[0] );
125 myname = strdup( myname + 1 );
129 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
131 openlog( myname, OPENLOG_OPTIONS );
135 if ( dosyslog && mypeer != -1 ) {
138 hp = gethostbyaddr( (char *) &mypeer, sizeof(mypeer), AF_INET );
139 addr.s_addr = mypeer;
140 syslog( LOG_INFO, "connection from %s (%s)", (hp == NULL) ?
141 "unknown" : hp->h_name, inet_ntoa( addr ) );
152 int len, rc, tblsize;
153 struct timeval timeout;
157 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
158 fprintf( stderr, FINGER_UNAVAILABLE );
159 perror( "ldap_open" );
162 ld->ld_sizelimit = FINGER_SIZELIMIT;
163 ld->ld_deref = deref;
165 if ( ldap_simple_bind_s( ld, FINGER_BINDDN, FINGER_BIND_CRED )
168 fprintf( stderr, FINGER_UNAVAILABLE );
169 ldap_perror( ld, "ldap_simple_bind_s" );
174 tblsize = sysconf( _SC_OPEN_MAX );
175 #else /* USE_SYSCONF */
176 tblsize = getdtablesize();
177 #endif /* USE_SYSCONF */
180 if (tblsize > FD_SETSIZE) {
181 tblsize = FD_SETSIZE;
183 #endif /* FD_SETSIZE*/
185 timeout.tv_sec = FINGER_TIMEOUT;
188 FD_SET( fileno( stdin ), &readfds );
190 if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 ) {
194 fprintf( stderr, "connection timed out on input\r\n" );
198 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
203 /* strip off \r \n */
204 if ( buf[len - 1] == '\n' ) {
208 if ( buf[len - 1] == '\r' ) {
214 printf( "No campus-wide login information available. Info for this machine only:\r\n" );
216 execl( FINGER_CMD, FINGER_CMD, NULL );
220 /* skip and ignore stinking /w */
221 if ( strncmp( buf, "/W ", 2 ) == 0 ) {
227 for ( ; *p && isspace( *p ); p++ )
245 static do_search( ld, buf )
251 int rc, matches, i, ufn;
254 LDAPMessage *result, *e;
255 static char *attrs[] = { "cn", "title", "objectClass", "joinable",
256 #ifdef FINGER_SORT_ATTR
260 extern int strcasecmp();
264 if ( strchr( buf, ',' ) != NULL ) {
265 ldap_ufn_setprefix( ld, base );
266 tv.tv_sec = FINGER_TIMEOUT;
268 ldap_ufn_timeout( (void *) &tv );
270 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &result ))
271 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
272 fprintf( stderr, FINGER_UNAVAILABLE );
273 ldap_perror( ld, "ldap_search_st" );
277 matches = ldap_count_entries( ld, result );
281 if ( (ld->ld_filtd = ldap_init_getfilter( filterfile ))
283 fprintf( stderr, "Cannot open filter file (%s)\n",
288 for ( fi = ldap_getfirstfilter( ld->ld_filtd, "finger", buf );
290 fi = ldap_getnextfilter( ld->ld_filtd ) )
292 tv.tv_sec = FINGER_TIMEOUT;
294 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
295 fi->lfi_filter, attrs, 0, &tv, &result ))
296 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
297 && rc != LDAP_TIMELIMIT_EXCEEDED )
299 fprintf( stderr, FINGER_UNAVAILABLE );
300 ldap_perror( ld, "ldap_search_st" );
304 if ( (matches = ldap_count_entries( ld, result )) != 0 )
307 ldap_msgfree( result );
314 if ( rc == LDAP_SIZELIMIT_EXCEEDED ) {
315 printf( "(Partial results - a size limit was exceeded)\r\n" );
316 } else if ( rc == LDAP_TIMELIMIT_EXCEEDED ) {
317 printf( "(Partial results - a time limit was exceeded)\r\n" );
320 if ( matches == 0 ) {
321 printf( FINGER_NOMATCH );
323 } else if ( matches < 0 ) {
324 fprintf( stderr, "error return from ldap_count_entries\r\n" );
326 } else if ( matches <= FINGER_LISTLIMIT ) {
327 printf( "%d %s match%s found for \"%s\":\r\n", matches,
328 ufn ? "UFN" : fi->lfi_desc, matches > 1 ? "es" : "", buf );
331 for ( e = ldap_first_entry( ld, result ); e != NULL; ) {
333 e = ldap_next_entry( ld, e );
335 printf( "--------------------\r\n" );
339 printf( "%d %s matches for \"%s\":\r\n", matches,
340 ufn ? "UFN" : fi->lfi_desc, buf );
343 #ifdef FINGER_SORT_ATTR
344 ldap_sort_entries( ld, &result, FINGER_SORT_ATTR, strcasecmp );
347 for ( e = ldap_first_entry( ld, result ); e != NULL;
348 e = ldap_next_entry( ld, e ) ) {
351 dn = ldap_get_dn( ld, e );
353 if ( (p = strchr( dn, ',' )) != NULL )
355 while ( *rdn && *rdn != '=' )
361 for ( i = 0; buf[i] != '\0'; i++ ) {
362 if ( buf[i] == '.' || buf[i] == '_' )
365 if ( strcasecmp( rdn, buf ) == 0 ) {
369 cn = ldap_get_values( ld, e, "cn" );
370 for ( i = 0; cn[i] != NULL; i++ ) {
371 last = strlen( cn[i] ) - 1;
372 if ( isdigit( cn[i][last] ) ) {
373 rdn = strdup( cn[i] );
379 title = ldap_get_values( ld, e, "title" );
382 printf( " %-20s %s\r\n", rdn,
383 title ? title[0] : "" );
384 if ( title != NULL ) {
385 for ( i = 1; title[i] != NULL; i++ )
386 printf( " %-20s %s\r\n", "",
392 ldap_value_free( title );
398 if ( result != NULL ) {
399 ldap_msgfree( result );
406 entry2textwrite( void *fp, char *buf, int len )
408 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
412 static do_read( ld, 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 );