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.
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
24 #include <ac/unistd.h>
27 #include <ac/setproctitle.h>
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
37 #include "ldapconfig.h"
41 #define ldap_debug debug
53 char *ldaphost = NULL;
55 int rdncount = GO500_RDNCOUNT;
56 char *filterfile = FILTERFILE;
57 char *templatefile = TEMPLATEFILE;
59 char myhost[MAXHOSTNAMELEN];
62 static void usage ( char *name );
63 static int set_socket (int port);
64 static RETSIGTYPE wait4child(int sig);
65 static void do_queries (int s);
66 static void do_error (FILE *fp, char *s);
67 static void do_search (LDAP *ld, FILE *fp, char *buf);
68 static void do_read (LDAP *ld, FILE *fp, char *dn);
73 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 );
78 main( int argc, char **argv )
86 struct sockaddr_in from;
89 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
90 /* for setproctitle */
95 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
97 case 'b': /* searchbase */
98 base = strdup( optarg );
101 case 'd': /* debug level */
102 debug |= atoi( optarg );
105 case 'f': /* ldap filter file */
106 filterfile = strdup( optarg );
109 case 'l': /* log via LOG_LOCAL3 */
113 case 'p': /* port to listen to */
114 port = atoi( optarg );
117 case 'c': /* number of DN components to show */
118 rdncount = atoi( optarg );
121 case 't': /* ldap template file */
122 templatefile = strdup( optarg );
125 case 'x': /* ldap server hostname */
126 ldaphost = strdup( optarg );
129 case 'I': /* run from inetd */
138 #ifdef GO500_HOSTNAME
139 strcpy( myhost, GO500_HOSTNAME );
141 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
143 perror( "gethostname" );
149 dtblsize = sysconf( _SC_OPEN_MAX );
150 #elif HAVE_GETDTABLESIZE
151 dtblsize = getdtablesize();
153 dtblsize = FD_SETSIZE;
157 if (dtblsize > FD_SETSIZE) {
158 dtblsize = FD_SETSIZE;
160 #endif /* FD_SETSIZE*/
162 /* detach if stderr is redirected or no debugging */
164 lutil_detach( debug && !isatty( 1 ), 1 );
166 if ( (myname = strrchr( argv[0], '/' )) == NULL )
167 myname = strdup( argv[0] );
169 myname = strdup( myname + 1 );
172 lber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
173 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
178 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
180 openlog( myname, OPENLOG_OPTIONS );
184 syslog( LOG_INFO, "initializing" );
186 /* set up the socket to listen on */
188 s = set_socket( port );
190 /* arrange to reap children */
191 (void) SIGNAL( SIGCHLD, wait4child );
195 fromlen = sizeof(from);
196 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
198 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
199 sizeof(from.sin_addr.s_addr), AF_INET );
200 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
201 (hp == NULL) ? "unknown" : hp->h_name,
202 inet_ntoa( from.sin_addr ), 0 );
205 syslog( LOG_INFO, "connection from %s (%s)",
206 (hp == NULL) ? "unknown" : hp->h_name,
207 inet_ntoa( from.sin_addr ) );
210 #ifdef LDAP_PROCTITLE
211 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
223 FD_SET( s, &readfds );
225 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
226 if ( debug ) perror( "select" );
228 } else if ( rc == 0 ) {
232 if ( ! FD_ISSET( s, &readfds ) )
235 fromlen = sizeof(from);
236 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
238 if ( debug ) perror( "accept" );
242 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
243 sizeof(from.sin_addr.s_addr), AF_INET );
246 syslog( LOG_INFO, "TCP connection from %s (%s)",
247 (hp == NULL) ? "unknown" : hp->h_name,
248 inet_ntoa( from.sin_addr ) );
251 switch( pid = fork() ) {
257 case -1: /* failed */
261 default: /* parent */
264 fprintf( stderr, "forked child %d\n", pid );
272 set_socket( int port )
275 struct sockaddr_in addr;
281 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
286 /* set option so clients can't keep us from coming back up */
288 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
289 sizeof(one) ) < 0 ) {
290 perror( "setsockopt" );
295 addr.sin_family = AF_INET;
296 addr.sin_addr.s_addr = INADDR_ANY;
297 addr.sin_port = htons( port );
298 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
303 /* listen for connections */
304 if ( listen( s, 5 ) == -1 ) {
309 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
315 wait4child( int sig )
318 WAITSTATUSTYPE status;
321 if ( debug ) printf( "parent: catching child status\n" );
324 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
327 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
331 (void) SIGNAL( SIGCHLD, wait4child );
337 char buf[1024], *query;
341 struct timeval timeout;
345 if ( (fp = fdopen( s, "a+")) == NULL ) {
349 timeout.tv_sec = GO500_TIMEOUT;
352 FD_SET( fileno( fp ), &readfds );
354 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
357 if ( fgets( buf, sizeof(buf), fp ) == NULL )
362 fprintf( stderr, "got %d bytes\n", len );
364 lber_bprint( buf, len );
369 if ( buf[len - 1] == '\n' )
372 if ( buf[len - 1] == '\r' )
378 /* strip off leading white space */
379 while ( isspace( *query )) {
386 if ( *query == '~' || *query == '@' ) {
388 } else if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
390 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
391 LDAP_SERVER_DOWN, myhost, myport );
392 fprintf( fp, ".\r\n" );
396 int deref = GO500_DEREF;
397 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
399 rc = ldap_simple_bind_s( ld, NULL, NULL );
400 if ( rc != LDAP_SUCCESS ) {
402 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
403 rc, myhost, myport );
404 fprintf( fp, ".\r\n" );
412 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
413 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
414 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
415 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
416 fprintf( fp, ".\r\n" );
420 do_read( ld, fp, ++query );
424 do_error( fp, ++query );
428 do_search( ld, fp, query );
432 fprintf( fp, ".\r\n" );
444 do_error( FILE *fp, char *s )
450 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
451 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
452 fprintf( fp, "No additional information is available\r\n" );
453 fprintf( fp, ".\r\n" );
457 do_search( LDAP *ld, FILE *fp, char *buf )
465 LDAPMessage *e, *res;
466 static char *attrs[] = { "title", 0 };
469 if ( strchr( buf, ',' ) != NULL ) {
470 ldap_ufn_setprefix( ld, base );
471 tv.tv_sec = GO500_TIMEOUT;
473 ldap_ufn_timeout( (void *) &tv );
475 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
476 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
478 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
479 rc, myhost, myport );
483 matches = ldap_count_entries( ld, res );
486 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
487 fprintf( stderr, "Cannot open filter file (%s)\n",
492 tv.tv_sec = GO500_TIMEOUT;
494 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
496 fi = ldap_getnextfilter( filtd ) )
498 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
499 fi->lfi_filter, attrs, 0, &tv, &res ))
500 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
501 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
502 rc, myhost, myport );
503 ldap_getfilter_free( filtd );
507 if ( (matches = ldap_count_entries( ld, res )) != 0 )
510 ldap_getfilter_free( filtd );
515 if ( matches <= 0 ) {
519 #ifdef GO500_SORT_ATTR
520 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
523 for ( e = ldap_first_entry( ld, res ); e != NULL;
524 e = ldap_next_entry( ld, e ) ) {
527 dn = ldap_get_dn( ld, e );
529 if ( (s = strchr( rdn, ',' )) != NULL )
532 if ( (s = strchr( rdn, '=' )) == NULL )
537 title = ldap_get_values( ld, e, "title" );
539 if ( title != NULL ) {
542 for ( p = title[0]; *p; p++ ) {
548 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
549 title ? title[0] : "", dn, myhost, myport );
552 ldap_value_free( title );
558 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
559 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
565 entry2textwrite( void *fp, char *buf, int len )
567 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
571 do_read( LDAP *ld, FILE *fp, char *dn )
573 static struct ldap_disptmpl *tmpllist;
575 ldap_init_templates( templatefile, &tmpllist );
577 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
578 entry2textwrite, (void *) fp, "\r\n", rdncount,
579 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
580 ldap_perror( ld, "ldap_entry2text_search" );
584 if ( tmpllist != NULL ) {
585 ldap_free_templates( tmpllist );