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.
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <sys/resource.h>
27 #include <sys/select.h>
31 #include "ldapconfig.h"
38 #endif /* USE_SYSCONF */
45 char *ldaphost = LDAPHOST;
46 char *base = GO500_BASE;
47 int rdncount = GO500_RDNCOUNT;
48 char *filterfile = FILTERFILE;
49 char *templatefile = TEMPLATEFILE;
51 char myhost[MAXHOSTNAMELEN];
55 static SIG_FN wait4child();
60 extern int strcasecmp();
65 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 struct sockaddr_in from;
86 /* for setproctitle */
90 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
92 case 'b': /* searchbase */
93 base = strdup( optarg );
96 case 'd': /* debug level */
97 debug = atoi( optarg );
100 case 'f': /* ldap filter file */
101 filterfile = strdup( optarg );
104 case 'l': /* log via LOG_LOCAL3 */
108 case 'p': /* port to listen to */
109 port = atoi( optarg );
112 case 'c': /* number of DN components to show */
113 rdncount = atoi( optarg );
116 case 't': /* ldap template file */
117 templatefile = strdup( optarg );
120 case 'x': /* ldap server hostname */
121 ldaphost = strdup( optarg );
124 case 'I': /* run from inetd */
133 #ifdef GO500_HOSTNAME
134 strcpy( myhost, GO500_HOSTNAME );
136 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
138 perror( "gethostname" );
144 dtblsize = sysconf( _SC_OPEN_MAX );
145 #else /* USE_SYSCONF */
146 dtblsize = getdtablesize();
147 #endif /* USE_SYSCONF */
149 /* detach if stderr is redirected or no debugging */
151 (void) detach( debug );
153 if ( (myname = strrchr( argv[0], '/' )) == NULL )
154 myname = strdup( argv[0] );
156 myname = strdup( myname + 1 );
160 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
162 openlog( myname, OPENLOG_OPTIONS );
166 syslog( LOG_INFO, "initializing" );
168 /* set up the socket to listen on */
170 s = set_socket( port );
172 /* arrange to reap children */
173 (void) signal( SIGCHLD, (void *) wait4child );
177 fromlen = sizeof(from);
178 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
180 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
181 sizeof(from.sin_addr.s_addr), AF_INET );
182 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
183 (hp == NULL) ? "unknown" : hp->h_name,
184 inet_ntoa( from.sin_addr ), 0 );
187 syslog( LOG_INFO, "connection from %s (%s)",
188 (hp == NULL) ? "unknown" : hp->h_name,
189 inet_ntoa( from.sin_addr ) );
192 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
203 FD_SET( s, &readfds );
205 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
206 if ( debug ) perror( "select" );
208 } else if ( rc == 0 ) {
212 if ( ! FD_ISSET( s, &readfds ) )
215 fromlen = sizeof(from);
216 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
218 if ( debug ) perror( "accept" );
222 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
223 sizeof(from.sin_addr.s_addr), AF_INET );
226 syslog( LOG_INFO, "TCP connection from %s (%s)",
227 (hp == NULL) ? "unknown" : hp->h_name,
228 inet_ntoa( from.sin_addr ) );
231 switch( pid = fork() ) {
237 case -1: /* failed */
241 default: /* parent */
244 fprintf( stderr, "forked child %d\n", pid );
256 struct sockaddr_in addr;
262 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
267 /* set option so clients can't keep us from coming back up */
269 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
270 sizeof(one) ) < 0 ) {
271 perror( "setsockopt" );
276 addr.sin_family = AF_INET;
277 addr.sin_addr.s_addr = INADDR_ANY;
278 addr.sin_port = htons( port );
279 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
284 /* listen for connections */
285 if ( listen( s, 5 ) == -1 ) {
290 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
298 WAITSTATUSTYPE status;
300 if ( debug ) printf( "parent: catching child status\n" );
302 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
303 #else /* USE_WAITPID */
304 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
305 #endif /* USE_WAITPID */
308 (void) signal( SIGCHLD, (void *) wait4child );
315 char buf[1024], *query;
319 struct timeval timeout;
323 if ( (fp = fdopen( s, "a+")) == NULL ) {
327 timeout.tv_sec = GO500_TIMEOUT;
330 FD_SET( fileno( fp ), &readfds );
332 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
335 if ( fgets( buf, sizeof(buf), fp ) == NULL )
340 fprintf( stderr, "got %d bytes\n", len );
342 lber_bprint( buf, len );
347 if ( buf[len - 1] == '\n' )
350 if ( buf[len - 1] == '\r' )
356 /* strip off leading white space */
357 while ( isspace( *query )) {
364 if ( *query != '~' && *query != '@' ) {
365 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
367 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
368 LDAP_SERVER_DOWN, myhost, myport );
369 fprintf( fp, ".\r\n" );
374 ld->ld_deref = GO500_DEREF;
375 if ( (rc = ldap_simple_bind_s( ld, GO500_BINDDN, NULL ))
378 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
379 rc, myhost, myport );
380 fprintf( fp, ".\r\n" );
388 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
389 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
390 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
391 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
392 fprintf( fp, ".\r\n" );
396 do_read( ld, fp, ++query );
400 do_error( fp, ++query );
404 do_search( ld, fp, query );
408 fprintf( fp, ".\r\n" );
425 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
426 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
427 fprintf( fp, "No additional information is available\r\n" );
428 fprintf( fp, ".\r\n" );
432 do_search( ld, fp, buf )
443 LDAPMessage *e, *res;
444 static char *attrs[] = { "title", 0 };
447 if ( strchr( buf, ',' ) != NULL ) {
448 ldap_ufn_setprefix( ld, base );
449 tv.tv_sec = GO500_TIMEOUT;
451 ldap_ufn_timeout( (void *) &tv );
453 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
454 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
456 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
457 rc, myhost, myport );
461 matches = ldap_count_entries( ld, res );
464 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
465 fprintf( stderr, "Cannot open filter file (%s)\n",
470 tv.tv_sec = GO500_TIMEOUT;
472 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
474 fi = ldap_getnextfilter( filtd ) )
476 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
477 fi->lfi_filter, attrs, 0, &tv, &res ))
478 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
479 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
480 rc, myhost, myport );
481 ldap_getfilter_free( filtd );
485 if ( (matches = ldap_count_entries( ld, res )) != 0 )
488 ldap_getfilter_free( filtd );
493 if ( matches <= 0 ) {
497 #ifdef GO500_SORT_ATTR
498 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
501 for ( e = ldap_first_entry( ld, res ); e != NULL;
502 e = ldap_next_entry( ld, e ) ) {
505 dn = ldap_get_dn( ld, e );
507 if ( (s = strchr( rdn, ',' )) != NULL )
510 if ( (s = strchr( rdn, '=' )) == NULL )
515 title = ldap_get_values( ld, e, "title" );
517 if ( title != NULL ) {
520 for ( p = title[0]; *p; p++ ) {
526 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
527 title ? title[0] : "", dn, myhost, myport );
530 ldap_value_free( title );
536 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
537 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
543 entry2textwrite( void *fp, char *buf, int len )
545 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
549 do_read( ld, fp, dn )
554 static struct ldap_disptmpl *tmpllist;
556 ldap_init_templates( templatefile, &tmpllist );
558 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
559 entry2textwrite, (void *) fp, "\r\n", rdncount,
560 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
561 ldap_perror( ld, "ldap_entry2text_search" );
565 if ( tmpllist != NULL ) {
566 ldap_free_templates( tmpllist );