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) )
144 perror( "gethostname" );
145 exit( EXIT_FAILURE );
150 dtblsize = sysconf( _SC_OPEN_MAX );
151 #elif HAVE_GETDTABLESIZE
152 dtblsize = getdtablesize();
154 dtblsize = FD_SETSIZE;
158 if (dtblsize > FD_SETSIZE) {
159 dtblsize = FD_SETSIZE;
161 #endif /* FD_SETSIZE*/
163 /* detach if stderr is redirected or no debugging */
165 lutil_detach( debug && !isatty( 1 ), 1 );
167 if ( (myname = strrchr( argv[0], '/' )) == NULL )
168 myname = strdup( argv[0] );
170 myname = strdup( myname + 1 );
173 ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
174 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
178 (void) SIGNAL( SIGPIPE, SIG_IGN );
183 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
185 openlog( myname, OPENLOG_OPTIONS );
189 syslog( LOG_INFO, "initializing" );
191 /* set up the socket to listen on */
193 s = set_socket( port );
195 /* arrange to reap children */
196 (void) SIGNAL( SIGCHLD, wait4child );
200 fromlen = sizeof(from);
201 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
203 hp = gethostbyaddr( (char *) &(from.sin_addr),
204 sizeof(from.sin_addr), AF_INET );
205 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
206 (hp == NULL) ? "unknown" : hp->h_name,
207 inet_ntoa( from.sin_addr ), 0 );
210 syslog( LOG_INFO, "connection from %s (%s)",
211 (hp == NULL) ? "unknown" : hp->h_name,
212 inet_ntoa( from.sin_addr ) );
215 #ifdef LDAP_PROCTITLE
216 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
223 exit( EXIT_SUCCESS );
228 FD_SET( s, &readfds );
230 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
231 if ( debug ) perror( "select" );
233 } else if ( rc == 0 ) {
237 if ( ! FD_ISSET( s, &readfds ) )
240 fromlen = sizeof(from);
241 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
243 if ( debug ) perror( "accept" );
244 exit( EXIT_FAILURE );
247 hp = gethostbyaddr( (char *) &(from.sin_addr),
248 sizeof(from.sin_addr), AF_INET );
251 syslog( LOG_INFO, "TCP connection from %s (%s)",
252 (hp == NULL) ? "unknown" : hp->h_name,
253 inet_ntoa( from.sin_addr ) );
256 switch( pid = fork() ) {
262 case -1: /* failed */
266 default: /* parent */
269 fprintf( stderr, "forked child %d\n", pid );
277 set_socket( int port )
280 struct sockaddr_in addr;
286 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
288 exit( EXIT_FAILURE );
292 /* set option so clients can't keep us from coming back up */
294 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
295 sizeof(one) ) < 0 ) {
296 perror( "setsockopt" );
297 exit( EXIT_FAILURE );
301 /* enable keep alives */
303 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
304 sizeof(one) ) < 0 ) {
305 perror( "setsockopt" );
306 exit( EXIT_FAILURE );
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = htonl(INADDR_ANY);
313 addr.sin_port = htons( port );
314 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
316 exit( EXIT_FAILURE );
319 /* listen for connections */
320 if ( listen( s, 5 ) == -1 ) {
322 exit( EXIT_FAILURE );
325 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
331 wait4child( int sig )
334 WAITSTATUSTYPE status;
337 if ( debug ) printf( "parent: catching child status\n" );
340 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
343 while ( wait4((pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
347 (void) SIGNAL( SIGCHLD, wait4child );
353 char buf[1024], *query;
357 struct timeval timeout;
361 if ( (fp = fdopen( s, "a+")) == NULL ) {
362 exit( EXIT_FAILURE );
365 timeout.tv_sec = GO500_TIMEOUT;
368 FD_SET( fileno( fp ), &readfds );
370 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
371 exit( EXIT_FAILURE );
373 if ( fgets( buf, sizeof(buf), fp ) == NULL )
374 exit( EXIT_FAILURE );
378 fprintf( stderr, "got %d bytes\n", len );
380 ber_bprint( buf, len );
385 if ( buf[len - 1] == '\n' )
388 if ( buf[len - 1] == '\r' )
394 /* strip off leading white space */
395 while ( isspace( (unsigned char) *query )) {
402 if ( *query == '~' || *query == '@' ) {
404 } else if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
406 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
407 LDAP_SERVER_DOWN, myhost, myport );
408 fprintf( fp, ".\r\n" );
410 exit( EXIT_FAILURE );
412 int deref = GO500_DEREF;
413 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
415 rc = ldap_simple_bind_s( ld, NULL, NULL );
416 if ( rc != LDAP_SUCCESS ) {
418 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
419 rc, myhost, myport );
420 fprintf( fp, ".\r\n" );
422 exit( EXIT_FAILURE );
428 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
429 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
430 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
431 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
432 fprintf( fp, ".\r\n" );
436 do_read( ld, fp, ++query );
440 do_error( fp, ++query );
444 do_search( ld, fp, query );
448 fprintf( fp, ".\r\n" );
455 exit( EXIT_FAILURE );
460 do_error( FILE *fp, char *s )
466 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
467 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
468 fprintf( fp, "No additional information is available\r\n" );
469 fprintf( fp, ".\r\n" );
473 do_search( LDAP *ld, FILE *fp, char *buf )
481 LDAPMessage *e, *res;
482 static char *attrs[] = { "title", 0 };
485 if ( strchr( buf, ',' ) != NULL ) {
486 ldap_ufn_setprefix( ld, base );
487 tv.tv_sec = GO500_TIMEOUT;
489 ldap_ufn_timeout( (void *) &tv );
491 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
492 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
494 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
495 rc, myhost, myport );
499 matches = ldap_count_entries( ld, res );
502 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
503 fprintf( stderr, "Cannot open filter file (%s)\n",
505 exit( EXIT_FAILURE );
508 tv.tv_sec = GO500_TIMEOUT;
510 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
512 fi = ldap_getnextfilter( filtd ) )
514 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
515 fi->lfi_filter, attrs, 0, &tv, &res ))
516 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
517 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
518 rc, myhost, myport );
519 ldap_getfilter_free( filtd );
523 if ( (matches = ldap_count_entries( ld, res )) != 0 )
526 ldap_getfilter_free( filtd );
531 if ( matches <= 0 ) {
535 #ifdef GO500_SORT_ATTR
536 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
539 for ( e = ldap_first_entry( ld, res ); e != NULL;
540 e = ldap_next_entry( ld, e ) ) {
543 dn = ldap_get_dn( ld, e );
545 if ( (s = strchr( rdn, ',' )) != NULL )
548 if ( (s = strchr( rdn, '=' )) == NULL )
553 title = ldap_get_values( ld, e, "title" );
555 if ( title != NULL ) {
558 for ( p = title[0]; *p; p++ ) {
564 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
565 title ? title[0] : "", dn, myhost, myport );
568 ldap_value_free( title );
574 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
575 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
581 entry2textwrite( void *fp, char *buf, ber_len_t len )
583 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
587 do_read( LDAP *ld, FILE *fp, char *dn )
589 static struct ldap_disptmpl *tmpllist;
591 ldap_init_templates( templatefile, &tmpllist );
593 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
594 entry2textwrite, (void *) fp, "\r\n", rdncount,
595 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
596 ldap_perror( ld, "ldap_entry2text_search" );
597 exit( EXIT_FAILURE );
600 if ( tmpllist != NULL ) {
601 ldap_free_templates( tmpllist );