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.
18 #include <ac/signal.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #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"
36 #if LDAP_VERSION < LDAP_VERSION3
37 /* quick fix until we have ldap_set_options */
38 #include "../libraries/libldap/ldap-int.h"
48 char *ldaphost = LDAPHOST;
49 char *base = GO500_BASE;
50 int rdncount = GO500_RDNCOUNT;
51 char *filterfile = FILTERFILE;
52 char *templatefile = TEMPLATEFILE;
54 char myhost[MAXHOSTNAMELEN];
58 static RETSIGTYPE wait4child();
63 extern int strcasecmp();
68 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 );
82 struct sockaddr_in from;
84 RETSIGTYPE wait4child();
89 /* for setproctitle */
93 while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
95 case 'b': /* searchbase */
96 base = strdup( optarg );
99 case 'd': /* debug level */
100 debug = atoi( optarg );
103 case 'f': /* ldap filter file */
104 filterfile = strdup( optarg );
107 case 'l': /* log via LOG_LOCAL3 */
111 case 'p': /* port to listen to */
112 port = atoi( optarg );
115 case 'c': /* number of DN components to show */
116 rdncount = atoi( optarg );
119 case 't': /* ldap template file */
120 templatefile = strdup( optarg );
123 case 'x': /* ldap server hostname */
124 ldaphost = strdup( optarg );
127 case 'I': /* run from inetd */
136 #ifdef GO500_HOSTNAME
137 strcpy( myhost, GO500_HOSTNAME );
139 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
141 perror( "gethostname" );
147 dtblsize = sysconf( _SC_OPEN_MAX );
148 #elif HAVE_GETDTABLESIZE
149 dtblsize = getdtablesize();
151 dtblsize = FD_SETSIZE;
155 if (dtblsize > FD_SETSIZE) {
156 dtblsize = FD_SETSIZE;
158 #endif /* FD_SETSIZE*/
161 /* detach if stderr is redirected or no debugging */
163 (void) detach( debug );
165 if ( (myname = strrchr( argv[0], '/' )) == NULL )
166 myname = strdup( argv[0] );
168 myname = strdup( myname + 1 );
172 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
174 openlog( myname, OPENLOG_OPTIONS );
178 syslog( LOG_INFO, "initializing" );
180 /* set up the socket to listen on */
182 s = set_socket( port );
184 /* arrange to reap children */
185 (void) SIGNAL( SIGCHLD, wait4child );
189 fromlen = sizeof(from);
190 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
192 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
193 sizeof(from.sin_addr.s_addr), AF_INET );
194 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
195 (hp == NULL) ? "unknown" : hp->h_name,
196 inet_ntoa( from.sin_addr ), 0 );
199 syslog( LOG_INFO, "connection from %s (%s)",
200 (hp == NULL) ? "unknown" : hp->h_name,
201 inet_ntoa( from.sin_addr ) );
204 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
215 FD_SET( s, &readfds );
217 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
218 if ( debug ) perror( "select" );
220 } else if ( rc == 0 ) {
224 if ( ! FD_ISSET( s, &readfds ) )
227 fromlen = sizeof(from);
228 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
230 if ( debug ) perror( "accept" );
234 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
235 sizeof(from.sin_addr.s_addr), AF_INET );
238 syslog( LOG_INFO, "TCP connection from %s (%s)",
239 (hp == NULL) ? "unknown" : hp->h_name,
240 inet_ntoa( from.sin_addr ) );
243 switch( pid = fork() ) {
249 case -1: /* failed */
253 default: /* parent */
256 fprintf( stderr, "forked child %d\n", pid );
268 struct sockaddr_in addr;
274 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
279 /* set option so clients can't keep us from coming back up */
281 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
282 sizeof(one) ) < 0 ) {
283 perror( "setsockopt" );
288 addr.sin_family = AF_INET;
289 addr.sin_addr.s_addr = INADDR_ANY;
290 addr.sin_port = htons( port );
291 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
296 /* listen for connections */
297 if ( listen( s, 5 ) == -1 ) {
302 if ( debug ) printf("tcp socket allocated, bound, and listening\n");
311 WAITSTATUSTYPE status;
314 if ( debug ) printf( "parent: catching child status\n" );
317 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
320 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
324 (void) SIGNAL( SIGCHLD, wait4child );
331 char buf[1024], *query;
335 struct timeval timeout;
339 if ( (fp = fdopen( s, "a+")) == NULL ) {
343 timeout.tv_sec = GO500_TIMEOUT;
346 FD_SET( fileno( fp ), &readfds );
348 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
351 if ( fgets( buf, sizeof(buf), fp ) == NULL )
356 fprintf( stderr, "got %d bytes\n", len );
358 lber_bprint( buf, len );
363 if ( buf[len - 1] == '\n' )
366 if ( buf[len - 1] == '\r' )
372 /* strip off leading white space */
373 while ( isspace( *query )) {
380 if ( *query != '~' && *query != '@' ) {
381 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
383 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
384 LDAP_SERVER_DOWN, myhost, myport );
385 fprintf( fp, ".\r\n" );
390 ld->ld_deref = GO500_DEREF;
391 if ( (rc = ldap_simple_bind_s( ld, GO500_BINDDN, GO500_BIND_CRED ))
394 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
395 rc, myhost, myport );
396 fprintf( fp, ".\r\n" );
404 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
405 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
406 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
407 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
408 fprintf( fp, ".\r\n" );
412 do_read( ld, fp, ++query );
416 do_error( fp, ++query );
420 do_search( ld, fp, query );
424 fprintf( fp, ".\r\n" );
441 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
442 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
443 fprintf( fp, "No additional information is available\r\n" );
444 fprintf( fp, ".\r\n" );
448 do_search( ld, fp, buf )
459 LDAPMessage *e, *res;
460 static char *attrs[] = { "title", 0 };
463 if ( strchr( buf, ',' ) != NULL ) {
464 ldap_ufn_setprefix( ld, base );
465 tv.tv_sec = GO500_TIMEOUT;
467 ldap_ufn_timeout( (void *) &tv );
469 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
470 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
472 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
473 rc, myhost, myport );
477 matches = ldap_count_entries( ld, res );
480 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
481 fprintf( stderr, "Cannot open filter file (%s)\n",
486 tv.tv_sec = GO500_TIMEOUT;
488 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
490 fi = ldap_getnextfilter( filtd ) )
492 if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
493 fi->lfi_filter, attrs, 0, &tv, &res ))
494 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
495 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
496 rc, myhost, myport );
497 ldap_getfilter_free( filtd );
501 if ( (matches = ldap_count_entries( ld, res )) != 0 )
504 ldap_getfilter_free( filtd );
509 if ( matches <= 0 ) {
513 #ifdef GO500_SORT_ATTR
514 ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
517 for ( e = ldap_first_entry( ld, res ); e != NULL;
518 e = ldap_next_entry( ld, e ) ) {
521 dn = ldap_get_dn( ld, e );
523 if ( (s = strchr( rdn, ',' )) != NULL )
526 if ( (s = strchr( rdn, '=' )) == NULL )
531 title = ldap_get_values( ld, e, "title" );
533 if ( title != NULL ) {
536 for ( p = title[0]; *p; p++ ) {
542 fprintf( fp, "0%-20s %s\t=%s\t%s\t%d\r\n", s,
543 title ? title[0] : "", dn, myhost, myport );
546 ldap_value_free( title );
552 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
553 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
559 entry2textwrite( void *fp, char *buf, int len )
561 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
565 do_read( ld, fp, dn )
570 static struct ldap_disptmpl *tmpllist;
572 ldap_init_templates( templatefile, &tmpllist );
574 if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
575 entry2textwrite, (void *) fp, "\r\n", rdncount,
576 LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
577 ldap_perror( ld, "ldap_entry2text_search" );
581 if ( tmpllist != NULL ) {
582 ldap_free_templates( tmpllist );