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
51 int ldap_syslog_level;
55 char *ldaphost = NULL;
57 int rdncount = GO500_RDNCOUNT;
58 char *filterfile = FILTERFILE;
59 char *templatefile = TEMPLATEFILE;
61 char myhost[MAXHOSTNAMELEN];
64 static void usage ( char *name );
65 static int set_socket (int port);
66 static RETSIGTYPE wait4child(int sig);
67 static void do_queries (int s);
68 static void do_error (FILE *fp, char *s);
69 static void do_search (LDAP *ld, FILE *fp, char *buf);
70 static void do_read (LDAP *ld, FILE *fp, char *dn);
75 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 );
80 main( int argc, char **argv )
88 struct sockaddr_in from;
91 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
92 /* for setproctitle */
97 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
99 case 'b': /* searchbase */
100 base = strdup( optarg );
103 case 'd': /* debug level */
104 debug |= atoi( optarg );
107 case 'f': /* ldap filter file */
108 filterfile = strdup( optarg );
111 case 'l': /* log via LOG_LOCAL3 */
115 case 'p': /* port to listen to */
116 port = atoi( optarg );
119 case 'c': /* number of DN components to show */
120 rdncount = atoi( optarg );
123 case 't': /* ldap template file */
124 templatefile = strdup( optarg );
127 case 'x': /* ldap server hostname */
128 ldaphost = strdup( optarg );
131 case 'I': /* run from inetd */
140 #ifdef GO500_HOSTNAME
141 strcpy( myhost, GO500_HOSTNAME );
143 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
145 perror( "gethostname" );
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.s_addr),
205 sizeof(from.sin_addr.s_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( hp == NULL ? inet_ntoa( from.sin_addr ) :
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" );
248 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
249 sizeof(from.sin_addr.s_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 ) {
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" );
301 addr.sin_family = AF_INET;
302 addr.sin_addr.s_addr = INADDR_ANY;
303 addr.sin_port = htons( port );
304 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
309 /* listen for connections */
310 if ( listen( s, 5 ) == -1 ) {
315 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
321 wait4child( int sig )
324 WAITSTATUSTYPE status;
327 if ( debug ) printf( "parent: catching child status\n" );
330 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
333 while ( wait4((pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
337 (void) SIGNAL( SIGCHLD, wait4child );
343 char buf[1024], *query;
347 struct timeval timeout;
351 if ( (fp = fdopen( s, "a+")) == NULL ) {
355 timeout.tv_sec = GO500_TIMEOUT;
358 FD_SET( fileno( fp ), &readfds );
360 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
363 if ( fgets( buf, sizeof(buf), fp ) == NULL )
368 fprintf( stderr, "got %d bytes\n", len );
370 lber_bprint( buf, len );
375 if ( buf[len - 1] == '\n' )
378 if ( buf[len - 1] == '\r' )
384 /* strip off leading white space */
385 while ( isspace( (unsigned char) *query )) {
392 if ( *query == '~' || *query == '@' ) {
394 } else if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
396 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
397 LDAP_SERVER_DOWN, myhost, myport );
398 fprintf( fp, ".\r\n" );
402 int deref = GO500_DEREF;
403 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
405 rc = ldap_simple_bind_s( ld, NULL, NULL );
406 if ( rc != LDAP_SUCCESS ) {
408 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
409 rc, myhost, myport );
410 fprintf( fp, ".\r\n" );
418 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
419 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
420 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
421 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
422 fprintf( fp, ".\r\n" );
426 do_read( ld, fp, ++query );
430 do_error( fp, ++query );
434 do_search( ld, fp, query );
438 fprintf( fp, ".\r\n" );
450 do_error( FILE *fp, char *s )
456 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
457 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
458 fprintf( fp, "No additional information is available\r\n" );
459 fprintf( fp, ".\r\n" );
463 do_search( LDAP *ld, FILE *fp, char *buf )
471 LDAPMessage *e, *res;
472 static char *attrs[] = { "title", 0 };
475 if ( strchr( buf, ',' ) != NULL ) {
476 ldap_ufn_setprefix( ld, base );
477 tv.tv_sec = GO500_TIMEOUT;
479 ldap_ufn_timeout( (void *) &tv );
481 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
482 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
484 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
485 rc, myhost, myport );
489 matches = ldap_count_entries( ld, res );
492 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
493 fprintf( stderr, "Cannot open filter file (%s)\n",
498 tv.tv_sec = GO500_TIMEOUT;
500 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
502 fi = ldap_getnextfilter( filtd ) )
504 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
505 fi->lfi_filter, attrs, 0, &tv, &res ))
506 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
507 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
508 rc, myhost, myport );
509 ldap_getfilter_free( filtd );
513 if ( (matches = ldap_count_entries( ld, res )) != 0 )
516 ldap_getfilter_free( filtd );
521 if ( matches <= 0 ) {
525 #ifdef GO500_SORT_ATTR
526 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
529 for ( e = ldap_first_entry( ld, res ); e != NULL;
530 e = ldap_next_entry( ld, e ) ) {
533 dn = ldap_get_dn( ld, e );
535 if ( (s = strchr( rdn, ',' )) != NULL )
538 if ( (s = strchr( rdn, '=' )) == NULL )
543 title = ldap_get_values( ld, e, "title" );
545 if ( title != NULL ) {
548 for ( p = title[0]; *p; p++ ) {
554 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
555 title ? title[0] : "", dn, myhost, myport );
558 ldap_value_free( title );
564 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
565 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
571 entry2textwrite( void *fp, char *buf, int len )
573 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
577 do_read( LDAP *ld, FILE *fp, char *dn )
579 static struct ldap_disptmpl *tmpllist;
581 ldap_init_templates( templatefile, &tmpllist );
583 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
584 entry2textwrite, (void *) fp, "\r\n", rdncount,
585 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
586 ldap_perror( ld, "ldap_entry2text_search" );
590 if ( tmpllist != NULL ) {
591 ldap_free_templates( tmpllist );