2 * Copyright (c) 1990-1996 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.
13 * Some code fragments to run from inetd stolen from the University
14 * of Minnesota gopher distribution, which had this copyright on it:
16 * Part of the Internet Gopher program, copyright (C) 1991
17 * University of Minnesota Microcomputer Workstation and Networks Center
24 #include <ac/signal.h>
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/syslog.h>
29 #include <ac/unistd.h>
32 #include <quipu/commonarg.h>
33 #include <quipu/ds_error.h>
42 int allow_severity = LOG_INFO;
43 int deny_severity = LOG_NOTICE;
44 #endif /* TCP_WRAPPERS */
49 static RETSIGTYPE wait4child();
50 #ifdef LDAP_CONNECTIONLESS
63 #ifdef LDAP_CONNECTIONLESS
66 int idletime = DEFAULT_TIMEOUT;
67 int referral_connection_timeout = DEFAULT_REFERRAL_TIMEOUT;
68 struct timeval conn_start_tv;
70 char *krb_ldap_service = "ldapserver";
71 char *krb_x500_service = "x500dsa";
72 char *krb_x500_instance;
74 char *kerberos_keyfile;
80 extern char Versionstr[];
85 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
86 #ifdef LDAP_CONNECTIONLESS
87 fprintf( stderr, " [ -U | -t timeout ]" );
89 fprintf( stderr, " [ -t timeout ]" );
91 fprintf( stderr, " [-I]" );
93 fprintf( stderr, " [-i dsainstance]" );
95 fprintf( stderr, "\n" );
103 #ifdef LDAP_CONNECTIONLESS
106 int myport = LDAP_PORT;
107 int i, pid, socktype;
111 struct sockaddr_in from;
115 RETSIGTYPE wait4child();
116 #ifndef NOSETPROCTITLE
125 /* Pick up socket from inetd-type server on VMS */
126 if ( (ns = socket_from_server( NULL )) > 0 )
129 /* Socket from inetd is usually 0 */
134 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
138 dsapargv[0] = argv[0];
144 kerberos_keyfile = "";
147 /* process command line arguments */
148 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
150 case 'c': /* specify dsa to contact */
151 dsapargv[1] = "-call";
152 dsapargv[2] = strdup( optarg );
156 case 'd': /* turn on debugging */
158 ldap_debug = atoi( optarg );
159 if ( ldap_debug & LDAP_DEBUG_PACKETS )
160 lber_debug = ldap_debug;
162 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
166 case 'l': /* do syslogging */
170 case 'p': /* specify port number */
171 myport = atoi( optarg );
174 case 'r': /* timeout for referral connections */
175 referral_connection_timeout = atoi( optarg );
178 case 't': /* timeout for idle connections */
179 idletime = atoi( optarg );
183 case 'f': /* kerberos key file */
184 kerberos_keyfile = strdup( optarg );
187 case 'i': /* x500 dsa kerberos instance */
188 if ( krb_x500_instance != NULL )
189 free( krb_x500_instance );
190 krb_x500_instance = strdup( optarg );
194 case 'I': /* Run from inetd */
198 #ifdef LDAP_CONNECTIONLESS
199 case 'U': /* UDP only (no TCP) */
205 case 'u': /* allow UDP requests (CLDAP) */
210 #endif /* LDAP_CONNECTIONLESS */
218 if ( optind < argc ) {
223 #ifdef LDAP_CONNECTIONLESS
224 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
230 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
233 dtblsize = sysconf( _SC_OPEN_MAX );
234 #elif HAVE_GETDTABLESIZE
235 dtblsize = getdtablesize();
237 dtblsize = FD_SETSIZE;
241 if( dtblsize > FD_SETSIZE ) {
242 dtblsize = FD_SETSIZE;
244 #endif /* FD_SETSIZE */
246 #ifndef NOSETPROCTITLE
247 /* for setproctitle */
252 if ( (myname = strrchr( argv[0], '/' )) == NULL )
253 myname = strdup( argv[0] );
255 myname = strdup( myname + 1 );
258 * detach from the terminal if stderr is redirected or no
259 * debugging is wanted, and then arrange to reap children
263 #ifndef NOSETPROCTITLE
264 setproctitle( "initializing" );
269 (void) SIGNAL( SIGCHLD, (void *) wait4child );
270 (void) SIGNAL( SIGINT, (void *) log_and_exit );
274 * set up syslogging (if desired)
278 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
280 openlog( myname, OPENLOG_OPTIONS );
285 * load the syntax handlers, oidtables, and initialize some stuff,
286 * then start listening
289 (void) quipu_syntaxes();
291 (void) pp_quipu_init( argv[0] );
293 #if ISODEPACKAGE == IC
295 dsa_operation_syntaxes();
298 (void) dsap_init( &dsapargc, &dsapargv );
299 (void) get_syntaxes();
301 len = sizeof( socktype );
302 getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
303 if ( socktype == SOCK_DGRAM ) {
304 #ifdef LDAP_CONNECTIONLESS
305 Debug( LDAP_DEBUG_ARGS,
306 "CLDAP request from unknown (%s)\n",
307 inet_ntoa( from.sin_addr ), 0, 0 );
308 conn_start_tv.tv_sec = 0;
311 #else /* LDAP_CONNECTIONLESS */
312 Debug( LDAP_DEBUG_ARGS,
313 "Compile with -DLDAP_CONNECTIONLESS for UDP support\n",0,0,0 );
314 #endif /* LDAP_CONNECTIONLESS */
319 if ( getpeername( ns, (struct sockaddr *) &from, &len )
321 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
322 sizeof(from.sin_addr.s_addr), AF_INET );
323 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
324 (hp == NULL) ? "unknown" : hp->h_name,
325 inet_ntoa( from.sin_addr ), 0 );
328 syslog( LOG_INFO, "connection from %s (%s)",
329 (hp == NULL) ? "unknown" : hp->h_name,
330 inet_ntoa( from.sin_addr ) );
333 #ifndef NOSETPROCTITLE
334 sprintf( title, "%s %d\n", hp == NULL ?
335 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
336 setproctitle( title );
339 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
346 tcps = set_socket( myport, 0 );
348 #ifdef LDAP_CONNECTIONLESS
350 udps = udp_init( myport, 1 );
354 * loop, wait for a connection, then fork off a child to handle it
355 * if we are doing CLDAP as well, handle those requests on the fly
358 #ifndef NOSETPROCTITLE
359 #ifdef LDAP_CONNECTIONLESS
360 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
361 do_udp ? "udp" : "", myport );
363 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
365 setproctitle( title );
371 FD_SET( tcps, &readfds );
372 #ifdef LDAP_CONNECTIONLESS
374 FD_SET( udps, &readfds );
377 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
379 if ( ldap_debug ) perror( "main select" );
384 #ifdef LDAP_CONNECTIONLESS
385 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
386 do_queries( udps, 1 );
390 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
395 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
398 if ( ldap_debug ) perror( "accept" );
403 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
404 sizeof(from.sin_addr.s_addr), AF_INET );
407 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
408 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
410 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
411 (hp == NULL) ? "unknown" : hp->h_name,
412 inet_ntoa( from.sin_addr ), 0 );
415 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
416 (hp == NULL) ? "unknown" : hp->h_name,
417 inet_ntoa( from.sin_addr ) );
423 #endif /* TCP_WRAPPERS */
425 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
426 (hp == NULL) ? "unknown" : hp->h_name,
427 inet_ntoa( from.sin_addr ), 0 );
431 syslog( LOG_INFO, "connection from %s (%s)",
432 (hp == NULL) ? "unknown" : hp->h_name,
433 inet_ntoa( from.sin_addr ) );
437 /* This is for debug on terminal on VMS */
439 #ifndef NOSETPROCTITLE
440 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
443 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
444 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
450 switch( pid = fork() ) {
453 #ifndef NOSETPROCTITLE
454 sprintf( title, "%s (%d)\n", hp == NULL ?
455 inet_ntoa( from.sin_addr ) : hp->h_name,
457 setproctitle( title );
459 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
460 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
465 case -1: /* failed */
467 if ( ldap_debug ) perror( "fork" );
470 syslog( LOG_ERR, "fork failed %m" );
471 /* let things cool off */
475 default: /* parent */
477 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
488 int udp /* is this a UDP (CLDAP) request? */
493 struct timeval timeout;
495 #ifdef LDAP_CONNECTIONLESS
496 struct sockaddr saddr, faddr;
497 struct sockaddr *saddrlist[ 1 ];
498 #endif /* LDAP_CONNECTIONLESS */
500 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
501 udp ? " udp" : "", 0, 0 );
504 * Loop, wait for a request from the client or a response from
505 * a dsa, then handle it. Dsap_ad is always a connection to the
506 * "default" dsa. Other connections can be made as a result of
507 * a referral being chased down. These association descriptors
508 * are kept track of with the message that caused the referral.
509 * The set_dsa_fds() routine traverses the list of outstanding
510 * messages, setting the appropriate bits in readfds.
517 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
518 sb.sb_sd = clientsock;
519 sb.sb_naddr = ( udp ) ? 1 : 0;
520 #ifdef LDAP_CONNECTIONLESS
521 sb.sb_addrs = (void **)saddrlist;
522 sb.sb_fromaddr = &faddr;
523 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
525 sb.sb_ber.ber_buf = NULL;
526 sb.sb_ber.ber_ptr = NULL;
527 sb.sb_ber.ber_end = NULL;
529 timeout.tv_sec = idletime;
532 struct conn *dsaconn;
533 extern struct conn *conns;
536 FD_SET( clientsock, &readfds );
537 conn_setfds( &readfds );
540 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
541 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
542 for ( i = 0; i < dtblsize; i++ ) {
543 if ( FD_ISSET( i, &readfds ) ) {
544 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
548 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
553 * hack - because of lber buffering, there might be stuff
554 * already waiting for us on the client sock.
557 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
558 if ( (rc = select( dtblsize, &readfds, 0, 0,
559 udp ? 0 : &timeout )) < 1 ) {
561 if ( ldap_debug ) perror( "do_queries select" );
564 log_and_exit( 0 ); /* idle timeout */
566 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
569 /* client gone away - we can too */
570 if ( isclosed( clientsock ) )
574 * check if a dsa conn has gone away -
583 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
584 FD_ISSET( clientsock, &readfds ) ) {
585 client_request( &sb, conns, udp );
587 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
588 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
593 dsa_response( dsaconn, &sb );
601 int udp /* UDP port? */
605 struct sockaddr_in addr;
607 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
612 /* set option so clients can't keep us from coming back up */
614 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
616 perror( "setsockopt" );
621 (void)memset( (void *)&addr, '\0', sizeof( addr ));
622 addr.sin_family = AF_INET;
623 addr.sin_addr.s_addr = INADDR_ANY;
624 addr.sin_port = htons( port );
625 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
631 /* listen for connections */
632 if ( listen( s, 5 ) == -1 ) {
638 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
639 udp ? "udp" : "tcp", port, 0 );
644 static RETSIGTYPE wait4child()
647 WAITSTATUSTYPE status;
650 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
653 while( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) > 0 )
656 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
660 (void) SIGNAL( SIGCHLD, (void *) wait4child );
665 log_and_exit( int exitcode )
670 if ( conn_start_tv.tv_sec == 0 ) {
671 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
673 gettimeofday( &tv, (struct timezone *)NULL );
674 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
675 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
683 #ifdef LDAP_CONNECTIONLESS
692 extern char *dsa_address;
693 extern struct PSAPaddr *psap_cpy();
694 extern struct conn *conns;
697 s = set_socket( port, 1 );
700 conns->c_dn = strdup("");
701 conns->c_cred = strdup("");
702 conns->c_credlen = 0;
703 conns->c_method = LDAP_AUTH_SIMPLE;
705 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
707 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
708 dsa_address : "NULL" );
711 conns->c_paddr = psap_cpy(conns->c_paddr);
714 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
715 fprintf(stderr, "Cannot bind to directory\n");
718 if ( matched != NULL )
721 return( createsocket ? s : 0 );