3 * Copyright (c) 1990 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>
30 #include <ac/setproctitle.h>
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
39 #include "ldap_defaults.h"
41 #define ldap_debug debug
50 int ldap_syslog_level;
54 char *ldaphost = NULL;
56 int rdncount = GO500_RDNCOUNT;
57 char *filterfile = FILTERFILE;
58 char *templatefile = TEMPLATEFILE;
60 char myhost[MAXHOSTNAMELEN];
63 static void usage ( char *name ) LDAP_GCCATTR((noreturn));
64 static int set_socket (int port);
65 static RETSIGTYPE wait4child(int sig);
66 static void do_queries (int s) LDAP_GCCATTR((noreturn));
67 static void do_error (FILE *fp, char *s);
68 static void do_search (LDAP *ld, FILE *fp, char *buf);
69 static void do_read (LDAP *ld, FILE *fp, char *dn);
74 fprintf( stderr, "usage: %s [-d debuglevel] [-f filterfile] [-t templatefile]\r\n\t[-a] [-l] [-p port] [-x ldaphost] [-b searchbase] [-c rdncount]\r\n", name );
79 main( int argc, char **argv )
87 struct sockaddr_in from;
90 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
91 /* for setproctitle */
96 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
98 case 'b': /* searchbase */
99 base = strdup( optarg );
102 case 'd': /* debug level */
103 debug |= atoi( optarg );
106 case 'f': /* ldap filter file */
107 filterfile = strdup( optarg );
110 case 'l': /* log via LOG_LOCAL3 */
114 case 'p': /* port to listen to */
115 port = atoi( optarg );
118 case 'c': /* number of DN components to show */
119 rdncount = atoi( optarg );
122 case 't': /* ldap template file */
123 templatefile = strdup( optarg );
126 case 'x': /* ldap server hostname */
127 ldaphost = strdup( optarg );
130 case 'I': /* run from inetd */
139 #ifdef GO500_HOSTNAME
140 strcpy( myhost, GO500_HOSTNAME );
142 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost)-1 )
144 perror( "gethostname" );
145 exit( EXIT_FAILURE );
147 myhost[sizeof(myhost)-1] = '\0';
151 dtblsize = sysconf( _SC_OPEN_MAX );
152 #elif HAVE_GETDTABLESIZE
153 dtblsize = getdtablesize();
155 dtblsize = FD_SETSIZE;
159 if (dtblsize > FD_SETSIZE) {
160 dtblsize = FD_SETSIZE;
162 #endif /* FD_SETSIZE*/
164 /* detach if stderr is redirected or no debugging */
166 lutil_detach( debug && !isatty( 1 ), 1 );
168 if ( (myname = strrchr( argv[0], '/' )) == NULL )
169 myname = strdup( argv[0] );
171 myname = strdup( myname + 1 );
174 ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
175 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
179 (void) SIGNAL( SIGPIPE, SIG_IGN );
184 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
186 openlog( myname, OPENLOG_OPTIONS );
190 syslog( LOG_INFO, "initializing" );
192 /* set up the socket to listen on */
194 s = set_socket( port );
196 /* arrange to reap children */
197 (void) SIGNAL( SIGCHLD, wait4child );
201 fromlen = sizeof(from);
202 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
204 hp = gethostbyaddr( (char *) &(from.sin_addr),
205 sizeof(from.sin_addr), AF_INET );
206 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
207 (hp == NULL) ? "unknown" : hp->h_name,
208 inet_ntoa( from.sin_addr ), 0 );
211 syslog( LOG_INFO, "connection from %s (%s)",
212 (hp == NULL) ? "unknown" : hp->h_name,
213 inet_ntoa( from.sin_addr ) );
216 #ifdef LDAP_PROCTITLE
217 setproctitle( "%s", hp == NULL ? inet_ntoa( from.sin_addr ) :
224 exit( EXIT_SUCCESS );
229 FD_SET( s, &readfds );
231 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
232 if ( debug ) perror( "select" );
234 } else if ( rc == 0 ) {
238 if ( ! FD_ISSET( s, &readfds ) )
241 fromlen = sizeof(from);
242 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
244 if ( debug ) perror( "accept" );
245 exit( EXIT_FAILURE );
248 hp = gethostbyaddr( (char *) &(from.sin_addr),
249 sizeof(from.sin_addr), AF_INET );
252 syslog( LOG_INFO, "TCP connection from %s (%s)",
253 (hp == NULL) ? "unknown" : hp->h_name,
254 inet_ntoa( from.sin_addr ) );
257 switch( pid = fork() ) {
263 case -1: /* failed */
267 default: /* parent */
270 fprintf( stderr, "forked child %d\n", pid );
278 set_socket( int port )
281 struct sockaddr_in addr;
287 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
289 exit( EXIT_FAILURE );
293 /* set option so clients can't keep us from coming back up */
295 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
296 sizeof(one) ) < 0 ) {
297 perror( "setsockopt" );
298 exit( EXIT_FAILURE );
302 /* enable keep alives */
304 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
305 sizeof(one) ) < 0 ) {
306 perror( "setsockopt" );
307 exit( EXIT_FAILURE );
312 addr.sin_family = AF_INET;
313 addr.sin_addr.s_addr = htonl(INADDR_ANY);
314 addr.sin_port = htons( port );
315 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
317 exit( EXIT_FAILURE );
320 /* listen for connections */
321 if ( listen( s, 5 ) == -1 ) {
323 exit( EXIT_FAILURE );
326 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
332 wait4child( int sig )
335 WAITSTATUSTYPE status;
338 if ( debug ) printf( "parent: catching child status\n" );
341 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
344 while ( wait4((pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
348 (void) SIGNAL_REINSTALL ( SIGCHLD, wait4child );
354 char buf[1024], *query;
358 struct timeval timeout;
362 if ( (fp = fdopen( s, "a+")) == NULL ) {
363 exit( EXIT_FAILURE );
366 timeout.tv_sec = GO500_TIMEOUT;
369 FD_SET( fileno( fp ), &readfds );
371 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
372 exit( EXIT_FAILURE );
374 if ( fgets( buf, sizeof(buf), fp ) == NULL )
375 exit( EXIT_FAILURE );
379 fprintf( stderr, "got %d bytes\n", len );
381 ber_bprint( buf, len );
386 if ( buf[len - 1] == '\n' )
389 if ( buf[len - 1] == '\r' )
395 /* strip off leading white space */
396 while ( isspace( (unsigned char) *query )) {
403 if ( *query == '~' || *query == '@' ) {
405 } else if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
407 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
408 LDAP_SERVER_DOWN, myhost, myport );
409 fprintf( fp, ".\r\n" );
411 exit( EXIT_FAILURE );
413 int deref = GO500_DEREF;
414 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
416 rc = ldap_simple_bind_s( ld, NULL, NULL );
417 if ( rc != LDAP_SUCCESS ) {
419 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
420 rc, myhost, myport );
421 fprintf( fp, ".\r\n" );
423 exit( EXIT_FAILURE );
429 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
430 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
431 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
432 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
433 fprintf( fp, ".\r\n" );
437 do_read( ld, fp, ++query );
441 do_error( fp, ++query );
445 do_search( ld, fp, query );
449 fprintf( fp, ".\r\n" );
456 exit( EXIT_FAILURE );
461 do_error( FILE *fp, char *s )
467 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
468 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
469 fprintf( fp, "No additional information is available\r\n" );
470 fprintf( fp, ".\r\n" );
474 do_search( LDAP *ld, FILE *fp, char *buf )
482 LDAPMessage *e, *res;
483 static char *attrs[] = { "title", 0 };
486 if ( strchr( buf, ',' ) != NULL ) {
487 ldap_ufn_setprefix( ld, base );
488 tv.tv_sec = GO500_TIMEOUT;
490 ldap_ufn_timeout( (void *) &tv );
492 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
493 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
495 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
496 rc, myhost, myport );
500 matches = ldap_count_entries( ld, res );
503 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
504 fprintf( stderr, "Cannot open filter file (%s)\n",
506 exit( EXIT_FAILURE );
509 tv.tv_sec = GO500_TIMEOUT;
511 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
513 fi = ldap_getnextfilter( filtd ) )
515 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
516 fi->lfi_filter, attrs, 0, &tv, &res ))
517 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
518 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
519 rc, myhost, myport );
520 ldap_getfilter_free( filtd );
524 if ( (matches = ldap_count_entries( ld, res )) != 0 )
527 ldap_getfilter_free( filtd );
532 if ( matches <= 0 ) {
536 #ifdef GO500_SORT_ATTR
537 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
540 for ( e = ldap_first_entry( ld, res ); e != NULL;
541 e = ldap_next_entry( ld, e ) ) {
544 dn = ldap_get_dn( ld, e );
546 if ( (s = strchr( rdn, ',' )) != NULL )
549 if ( (s = strchr( rdn, '=' )) == NULL )
554 title = ldap_get_values( ld, e, "title" );
556 if ( title != NULL ) {
559 for ( p = title[0]; *p; p++ ) {
565 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
566 title ? title[0] : "", dn, myhost, myport );
569 ldap_value_free( title );
575 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
576 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
582 entry2textwrite( void *fp, char *buf, ber_len_t len )
584 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
588 do_read( LDAP *ld, FILE *fp, char *dn )
590 static struct ldap_disptmpl *tmpllist;
592 ldap_init_templates( templatefile, &tmpllist );
594 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
595 entry2textwrite, (void *) fp, "\r\n", rdncount,
596 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
597 ldap_perror( ld, "ldap_entry2text_search" );
598 exit( EXIT_FAILURE );
601 if ( tmpllist != NULL ) {
602 ldap_free_templates( tmpllist );