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/string.h>
21 #include <ac/socket.h>
22 #include <ac/syslog.h>
23 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
30 #include <sys/resource.h>
32 #include "ldapconfig.h"
42 char *ldaphost = LDAPHOST;
43 char *base = GO500_BASE;
44 int rdncount = GO500_RDNCOUNT;
45 char *filterfile = FILTERFILE;
46 char *templatefile = TEMPLATEFILE;
48 char myhost[MAXHOSTNAMELEN];
52 static RETSIGTYPE wait4child();
57 extern int strcasecmp();
62 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 );
76 struct sockaddr_in from;
78 RETSIGTYPE wait4child();
83 /* for setproctitle */
87 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
89 case 'b': /* searchbase */
90 base = strdup( optarg );
93 case 'd': /* debug level */
94 debug = atoi( optarg );
97 case 'f': /* ldap filter file */
98 filterfile = strdup( optarg );
101 case 'l': /* log via LOG_LOCAL3 */
105 case 'p': /* port to listen to */
106 port = atoi( optarg );
109 case 'c': /* number of DN components to show */
110 rdncount = atoi( optarg );
113 case 't': /* ldap template file */
114 templatefile = strdup( optarg );
117 case 'x': /* ldap server hostname */
118 ldaphost = strdup( optarg );
121 case 'I': /* run from inetd */
130 #ifdef GO500_HOSTNAME
131 strcpy( myhost, GO500_HOSTNAME );
133 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
135 perror( "gethostname" );
141 dtblsize = sysconf( _SC_OPEN_MAX );
142 #else /* USE_SYSCONF */
143 dtblsize = getdtablesize();
144 #endif /* USE_SYSCONF */
147 if (dtblsize > FD_SETSIZE) {
148 dtblsize = FD_SETSIZE;
150 #endif /* FD_SETSIZE*/
153 /* detach if stderr is redirected or no debugging */
155 (void) detach( debug );
157 if ( (myname = strrchr( argv[0], '/' )) == NULL )
158 myname = strdup( argv[0] );
160 myname = strdup( myname + 1 );
164 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
166 openlog( myname, OPENLOG_OPTIONS );
170 syslog( LOG_INFO, "initializing" );
172 /* set up the socket to listen on */
174 s = set_socket( port );
176 /* arrange to reap children */
177 (void) signal( SIGCHLD, (void *) wait4child );
181 fromlen = sizeof(from);
182 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
184 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
185 sizeof(from.sin_addr.s_addr), AF_INET );
186 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
187 (hp == NULL) ? "unknown" : hp->h_name,
188 inet_ntoa( from.sin_addr ), 0 );
191 syslog( LOG_INFO, "connection from %s (%s)",
192 (hp == NULL) ? "unknown" : hp->h_name,
193 inet_ntoa( from.sin_addr ) );
196 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
207 FD_SET( s, &readfds );
209 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
210 if ( debug ) perror( "select" );
212 } else if ( rc == 0 ) {
216 if ( ! FD_ISSET( s, &readfds ) )
219 fromlen = sizeof(from);
220 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
222 if ( debug ) perror( "accept" );
226 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
227 sizeof(from.sin_addr.s_addr), AF_INET );
230 syslog( LOG_INFO, "TCP connection from %s (%s)",
231 (hp == NULL) ? "unknown" : hp->h_name,
232 inet_ntoa( from.sin_addr ) );
235 switch( pid = fork() ) {
241 case -1: /* failed */
245 default: /* parent */
248 fprintf( stderr, "forked child %d\n", pid );
260 struct sockaddr_in addr;
266 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
271 /* set option so clients can't keep us from coming back up */
273 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
274 sizeof(one) ) < 0 ) {
275 perror( "setsockopt" );
280 addr.sin_family = AF_INET;
281 addr.sin_addr.s_addr = INADDR_ANY;
282 addr.sin_port = htons( port );
283 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
288 /* listen for connections */
289 if ( listen( s, 5 ) == -1 ) {
294 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
303 WAITSTATUSTYPE status;
306 if ( debug ) printf( "parent: catching child status\n" );
309 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
312 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
316 (void) signal( SIGCHLD, (void *) wait4child );
323 char buf[1024], *query;
327 struct timeval timeout;
331 if ( (fp = fdopen( s, "a+")) == NULL ) {
335 timeout.tv_sec = GO500_TIMEOUT;
338 FD_SET( fileno( fp ), &readfds );
340 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
343 if ( fgets( buf, sizeof(buf), fp ) == NULL )
348 fprintf( stderr, "got %d bytes\n", len );
350 lber_bprint( buf, len );
355 if ( buf[len - 1] == '\n' )
358 if ( buf[len - 1] == '\r' )
364 /* strip off leading white space */
365 while ( isspace( *query )) {
372 if ( *query != '~' && *query != '@' ) {
373 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
375 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
376 LDAP_SERVER_DOWN, myhost, myport );
377 fprintf( fp, ".\r\n" );
382 ld->ld_deref = GO500_DEREF;
383 if ( (rc = ldap_simple_bind_s( ld, GO500_BINDDN, GO500_BIND_CRED ))
386 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
387 rc, myhost, myport );
388 fprintf( fp, ".\r\n" );
396 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
397 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
398 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
399 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
400 fprintf( fp, ".\r\n" );
404 do_read( ld, fp, ++query );
408 do_error( fp, ++query );
412 do_search( ld, fp, query );
416 fprintf( fp, ".\r\n" );
433 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
434 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
435 fprintf( fp, "No additional information is available\r\n" );
436 fprintf( fp, ".\r\n" );
440 do_search( ld, fp, buf )
451 LDAPMessage *e, *res;
452 static char *attrs[] = { "title", 0 };
455 if ( strchr( buf, ',' ) != NULL ) {
456 ldap_ufn_setprefix( ld, base );
457 tv.tv_sec = GO500_TIMEOUT;
459 ldap_ufn_timeout( (void *) &tv );
461 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
462 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
464 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
465 rc, myhost, myport );
469 matches = ldap_count_entries( ld, res );
472 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
473 fprintf( stderr, "Cannot open filter file (%s)\n",
478 tv.tv_sec = GO500_TIMEOUT;
480 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
482 fi = ldap_getnextfilter( filtd ) )
484 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
485 fi->lfi_filter, attrs, 0, &tv, &res ))
486 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
487 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
488 rc, myhost, myport );
489 ldap_getfilter_free( filtd );
493 if ( (matches = ldap_count_entries( ld, res )) != 0 )
496 ldap_getfilter_free( filtd );
501 if ( matches <= 0 ) {
505 #ifdef GO500_SORT_ATTR
506 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
509 for ( e = ldap_first_entry( ld, res ); e != NULL;
510 e = ldap_next_entry( ld, e ) ) {
513 dn = ldap_get_dn( ld, e );
515 if ( (s = strchr( rdn, ',' )) != NULL )
518 if ( (s = strchr( rdn, '=' )) == NULL )
523 title = ldap_get_values( ld, e, "title" );
525 if ( title != NULL ) {
528 for ( p = title[0]; *p; p++ ) {
534 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
535 title ? title[0] : "", dn, myhost, myport );
538 ldap_value_free( title );
544 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
545 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
551 entry2textwrite( void *fp, char *buf, int len )
553 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
557 do_read( ld, fp, dn )
562 static struct ldap_disptmpl *tmpllist;
564 ldap_init_templates( templatefile, &tmpllist );
566 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
567 entry2textwrite, (void *) fp, "\r\n", rdncount,
568 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
569 ldap_perror( ld, "ldap_entry2text_search" );
573 if ( tmpllist != NULL ) {
574 ldap_free_templates( tmpllist );