2 * Copyright (c) 1990 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.
17 #include <ac/stdlib.h>
20 #include <ac/signal.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/syslog.h>
25 #include <ac/unistd.h>
28 #include <ac/setproctitle.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 #include <sys/resource.h>
41 #include "ldap_defaults.h"
43 #define ldap_debug debug
52 int ldap_syslog_level;
56 char *ldaphost = NULL;
58 int rdncount = GO500_RDNCOUNT;
59 char *filterfile = FILTERFILE;
60 char *templatefile = TEMPLATEFILE;
62 char myhost[MAXHOSTNAMELEN];
65 static void usage ( char *name );
66 static int set_socket (int port);
67 static RETSIGTYPE wait4child(int sig);
68 static void do_queries (int s);
69 static void do_error (FILE *fp, char *s);
70 static void do_search (LDAP *ld, FILE *fp, char *buf);
71 static void do_read (LDAP *ld, FILE *fp, char *dn);
76 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 );
81 main( int argc, char **argv )
89 struct sockaddr_in from;
92 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
93 /* for setproctitle */
98 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
100 case 'b': /* searchbase */
101 base = strdup( optarg );
104 case 'd': /* debug level */
105 debug |= atoi( optarg );
108 case 'f': /* ldap filter file */
109 filterfile = strdup( optarg );
112 case 'l': /* log via LOG_LOCAL3 */
116 case 'p': /* port to listen to */
117 port = atoi( optarg );
120 case 'c': /* number of DN components to show */
121 rdncount = atoi( optarg );
124 case 't': /* ldap template file */
125 templatefile = strdup( optarg );
128 case 'x': /* ldap server hostname */
129 ldaphost = strdup( optarg );
132 case 'I': /* run from inetd */
141 #ifdef GO500_HOSTNAME
142 strcpy( myhost, GO500_HOSTNAME );
144 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
146 perror( "gethostname" );
147 exit( EXIT_FAILURE );
152 dtblsize = sysconf( _SC_OPEN_MAX );
153 #elif HAVE_GETDTABLESIZE
154 dtblsize = getdtablesize();
156 dtblsize = FD_SETSIZE;
160 if (dtblsize > FD_SETSIZE) {
161 dtblsize = FD_SETSIZE;
163 #endif /* FD_SETSIZE*/
165 /* detach if stderr is redirected or no debugging */
167 lutil_detach( debug && !isatty( 1 ), 1 );
169 if ( (myname = strrchr( argv[0], '/' )) == NULL )
170 myname = strdup( argv[0] );
172 myname = strdup( myname + 1 );
175 ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
176 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
180 (void) SIGNAL( SIGPIPE, SIG_IGN );
185 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
187 openlog( myname, OPENLOG_OPTIONS );
191 syslog( LOG_INFO, "initializing" );
193 /* set up the socket to listen on */
195 s = set_socket( port );
197 /* arrange to reap children */
198 (void) SIGNAL( SIGCHLD, wait4child );
202 fromlen = sizeof(from);
203 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
205 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
206 sizeof(from.sin_addr.s_addr), AF_INET );
207 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
208 (hp == NULL) ? "unknown" : hp->h_name,
209 inet_ntoa( from.sin_addr ), 0 );
212 syslog( LOG_INFO, "connection from %s (%s)",
213 (hp == NULL) ? "unknown" : hp->h_name,
214 inet_ntoa( from.sin_addr ) );
217 #ifdef LDAP_PROCTITLE
218 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
225 exit( EXIT_SUCCESS );
230 FD_SET( s, &readfds );
232 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
233 if ( debug ) perror( "select" );
235 } else if ( rc == 0 ) {
239 if ( ! FD_ISSET( s, &readfds ) )
242 fromlen = sizeof(from);
243 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
245 if ( debug ) perror( "accept" );
246 exit( EXIT_FAILURE );
249 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
250 sizeof(from.sin_addr.s_addr), AF_INET );
253 syslog( LOG_INFO, "TCP connection from %s (%s)",
254 (hp == NULL) ? "unknown" : hp->h_name,
255 inet_ntoa( from.sin_addr ) );
258 switch( pid = fork() ) {
264 case -1: /* failed */
268 default: /* parent */
271 fprintf( stderr, "forked child %d\n", pid );
279 set_socket( int port )
282 struct sockaddr_in addr;
288 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
290 exit( EXIT_FAILURE );
294 /* set option so clients can't keep us from coming back up */
296 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
297 sizeof(one) ) < 0 ) {
298 perror( "setsockopt" );
299 exit( EXIT_FAILURE );
303 /* enable keep alives */
305 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
306 sizeof(one) ) < 0 ) {
307 perror( "setsockopt" );
308 exit( EXIT_FAILURE );
313 addr.sin_family = AF_INET;
314 addr.sin_addr.s_addr = htonl(INADDR_ANY);
315 addr.sin_port = htons( port );
316 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
318 exit( EXIT_FAILURE );
321 /* listen for connections */
322 if ( listen( s, 5 ) == -1 ) {
324 exit( EXIT_FAILURE );
327 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
333 wait4child( int sig )
336 WAITSTATUSTYPE status;
339 if ( debug ) printf( "parent: catching child status\n" );
342 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
345 while ( wait4((pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
349 (void) SIGNAL( SIGCHLD, wait4child );
355 char buf[1024], *query;
359 struct timeval timeout;
363 if ( (fp = fdopen( s, "a+")) == NULL ) {
364 exit( EXIT_FAILURE );
367 timeout.tv_sec = GO500_TIMEOUT;
370 FD_SET( fileno( fp ), &readfds );
372 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
373 exit( EXIT_FAILURE );
375 if ( fgets( buf, sizeof(buf), fp ) == NULL )
376 exit( EXIT_FAILURE );
380 fprintf( stderr, "got %d bytes\n", len );
382 ber_bprint( buf, len );
387 if ( buf[len - 1] == '\n' )
390 if ( buf[len - 1] == '\r' )
396 /* strip off leading white space */
397 while ( isspace( (unsigned char) *query )) {
404 if ( *query == '~' || *query == '@' ) {
406 } else if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
408 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
409 LDAP_SERVER_DOWN, myhost, myport );
410 fprintf( fp, ".\r\n" );
412 exit( EXIT_FAILURE );
414 int deref = GO500_DEREF;
415 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
417 rc = ldap_simple_bind_s( ld, NULL, NULL );
418 if ( rc != LDAP_SUCCESS ) {
420 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
421 rc, myhost, myport );
422 fprintf( fp, ".\r\n" );
424 exit( EXIT_FAILURE );
430 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
431 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
432 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
433 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
434 fprintf( fp, ".\r\n" );
438 do_read( ld, fp, ++query );
442 do_error( fp, ++query );
446 do_search( ld, fp, query );
450 fprintf( fp, ".\r\n" );
457 exit( EXIT_FAILURE );
462 do_error( FILE *fp, char *s )
468 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
469 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
470 fprintf( fp, "No additional information is available\r\n" );
471 fprintf( fp, ".\r\n" );
475 do_search( LDAP *ld, FILE *fp, char *buf )
483 LDAPMessage *e, *res;
484 static char *attrs[] = { "title", 0 };
487 if ( strchr( buf, ',' ) != NULL ) {
488 ldap_ufn_setprefix( ld, base );
489 tv.tv_sec = GO500_TIMEOUT;
491 ldap_ufn_timeout( (void *) &tv );
493 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
494 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
496 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
497 rc, myhost, myport );
501 matches = ldap_count_entries( ld, res );
504 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
505 fprintf( stderr, "Cannot open filter file (%s)\n",
507 exit( EXIT_FAILURE );
510 tv.tv_sec = GO500_TIMEOUT;
512 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
514 fi = ldap_getnextfilter( filtd ) )
516 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
517 fi->lfi_filter, attrs, 0, &tv, &res ))
518 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
519 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
520 rc, myhost, myport );
521 ldap_getfilter_free( filtd );
525 if ( (matches = ldap_count_entries( ld, res )) != 0 )
528 ldap_getfilter_free( filtd );
533 if ( matches <= 0 ) {
537 #ifdef GO500_SORT_ATTR
538 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
541 for ( e = ldap_first_entry( ld, res ); e != NULL;
542 e = ldap_next_entry( ld, e ) ) {
545 dn = ldap_get_dn( ld, e );
547 if ( (s = strchr( rdn, ',' )) != NULL )
550 if ( (s = strchr( rdn, '=' )) == NULL )
555 title = ldap_get_values( ld, e, "title" );
557 if ( title != NULL ) {
560 for ( p = title[0]; *p; p++ ) {
566 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
567 title ? title[0] : "", dn, myhost, myport );
570 ldap_value_free( title );
576 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
577 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
583 entry2textwrite( void *fp, char *buf, ber_len_t len )
585 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
589 do_read( LDAP *ld, FILE *fp, char *dn )
591 static struct ldap_disptmpl *tmpllist;
593 ldap_init_templates( templatefile, &tmpllist );
595 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
596 entry2textwrite, (void *) fp, "\r\n", rdncount,
597 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
598 ldap_perror( ld, "ldap_entry2text_search" );
599 exit( EXIT_FAILURE );
602 if ( tmpllist != NULL ) {
603 ldap_free_templates( tmpllist );