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>
38 #include "ldap_defaults.h"
42 #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" );
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 ) :
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" );
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 ) {
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" );
302 addr.sin_family = AF_INET;
303 addr.sin_addr.s_addr = INADDR_ANY;
304 addr.sin_port = htons( port );
305 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
310 /* listen for connections */
311 if ( listen( s, 5 ) == -1 ) {
316 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
322 wait4child( int sig )
325 WAITSTATUSTYPE status;
328 if ( debug ) printf( "parent: catching child status\n" );
331 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
334 while ( wait4((pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
338 (void) SIGNAL( SIGCHLD, wait4child );
344 char buf[1024], *query;
348 struct timeval timeout;
352 if ( (fp = fdopen( s, "a+")) == NULL ) {
356 timeout.tv_sec = GO500_TIMEOUT;
359 FD_SET( fileno( fp ), &readfds );
361 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
364 if ( fgets( buf, sizeof(buf), fp ) == NULL )
369 fprintf( stderr, "got %d bytes\n", len );
371 ber_bprint( buf, len );
376 if ( buf[len - 1] == '\n' )
379 if ( buf[len - 1] == '\r' )
385 /* strip off leading white space */
386 while ( isspace( (unsigned char) *query )) {
393 if ( *query == '~' || *query == '@' ) {
395 } else if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
397 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
398 LDAP_SERVER_DOWN, myhost, myport );
399 fprintf( fp, ".\r\n" );
403 int deref = GO500_DEREF;
404 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
406 rc = ldap_simple_bind_s( ld, NULL, NULL );
407 if ( rc != LDAP_SUCCESS ) {
409 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
410 rc, myhost, myport );
411 fprintf( fp, ".\r\n" );
419 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
420 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
421 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
422 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
423 fprintf( fp, ".\r\n" );
427 do_read( ld, fp, ++query );
431 do_error( fp, ++query );
435 do_search( ld, fp, query );
439 fprintf( fp, ".\r\n" );
451 do_error( FILE *fp, char *s )
457 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
458 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
459 fprintf( fp, "No additional information is available\r\n" );
460 fprintf( fp, ".\r\n" );
464 do_search( LDAP *ld, FILE *fp, char *buf )
472 LDAPMessage *e, *res;
473 static char *attrs[] = { "title", 0 };
476 if ( strchr( buf, ',' ) != NULL ) {
477 ldap_ufn_setprefix( ld, base );
478 tv.tv_sec = GO500_TIMEOUT;
480 ldap_ufn_timeout( (void *) &tv );
482 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
483 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
485 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
486 rc, myhost, myport );
490 matches = ldap_count_entries( ld, res );
493 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
494 fprintf( stderr, "Cannot open filter file (%s)\n",
499 tv.tv_sec = GO500_TIMEOUT;
501 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
503 fi = ldap_getnextfilter( filtd ) )
505 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
506 fi->lfi_filter, attrs, 0, &tv, &res ))
507 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
508 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
509 rc, myhost, myport );
510 ldap_getfilter_free( filtd );
514 if ( (matches = ldap_count_entries( ld, res )) != 0 )
517 ldap_getfilter_free( filtd );
522 if ( matches <= 0 ) {
526 #ifdef GO500_SORT_ATTR
527 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
530 for ( e = ldap_first_entry( ld, res ); e != NULL;
531 e = ldap_next_entry( ld, e ) ) {
534 dn = ldap_get_dn( ld, e );
536 if ( (s = strchr( rdn, ',' )) != NULL )
539 if ( (s = strchr( rdn, '=' )) == NULL )
544 title = ldap_get_values( ld, e, "title" );
546 if ( title != NULL ) {
549 for ( p = title[0]; *p; p++ ) {
555 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
556 title ? title[0] : "", dn, myhost, myport );
559 ldap_value_free( title );
565 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
566 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
572 entry2textwrite( void *fp, char *buf, ber_len_t len )
574 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
578 do_read( LDAP *ld, FILE *fp, char *dn )
580 static struct ldap_disptmpl *tmpllist;
582 ldap_init_templates( templatefile, &tmpllist );
584 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
585 entry2textwrite, (void *) fp, "\r\n", rdncount,
586 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
587 ldap_perror( ld, "ldap_entry2text_search" );
591 if ( tmpllist != NULL ) {
592 ldap_free_templates( tmpllist );