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>
33 #include <ac/setproctitle.h>
36 #include <quipu/commonarg.h>
37 #include <quipu/ds_error.h>
40 #include "../../libraries/liblber/lber-int.h" /* get struct sockbuf */
47 int allow_severity = LOG_INFO;
48 int deny_severity = LOG_NOTICE;
49 #endif /* TCP_WRAPPERS */
54 static RETSIGTYPE wait4child();
55 #ifdef LDAP_CONNECTIONLESS
68 #ifdef LDAP_CONNECTIONLESS
71 int idletime = DEFAULT_TIMEOUT;
72 int referral_connection_timeout = DEFAULT_REFERRAL_TIMEOUT;
73 struct timeval conn_start_tv;
75 char *krb_ldap_service = "ldapserver";
76 char *krb_x500_service = "x500dsa";
77 char *krb_x500_instance;
79 char *kerberos_keyfile;
85 extern char Versionstr[];
90 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
91 #ifdef LDAP_CONNECTIONLESS
92 fprintf( stderr, " [ -U | -t timeout ]" );
94 fprintf( stderr, " [ -t timeout ]" );
96 fprintf( stderr, " [-I]" );
98 fprintf( stderr, " [-i dsainstance]" );
100 fprintf( stderr, "\n" );
108 #ifdef LDAP_CONNECTIONLESS
111 int myport = LDAP_PORT;
112 int i, pid, socktype;
116 struct sockaddr_in from;
120 RETSIGTYPE wait4child();
121 #ifdef LDAP_PROCTITLE
128 /* Pick up socket from inetd-type server on VMS */
129 if ( (ns = socket_from_server( NULL )) > 0 )
132 /* Socket from inetd is usually 0 */
137 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
141 dsapargv[0] = argv[0];
147 kerberos_keyfile = "";
150 /* process command line arguments */
151 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
153 case 'c': /* specify dsa to contact */
154 dsapargv[1] = "-call";
155 dsapargv[2] = strdup( optarg );
159 case 'd': /* turn on debugging */
161 ldap_debug = atoi( optarg );
162 if ( ldap_debug & LDAP_DEBUG_PACKETS )
163 lber_debug = ldap_debug;
165 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
169 case 'l': /* do syslogging */
173 case 'p': /* specify port number */
174 myport = atoi( optarg );
177 case 'r': /* timeout for referral connections */
178 referral_connection_timeout = atoi( optarg );
181 case 't': /* timeout for idle connections */
182 idletime = atoi( optarg );
186 case 'f': /* kerberos key file */
187 kerberos_keyfile = strdup( optarg );
190 case 'i': /* x500 dsa kerberos instance */
191 if ( krb_x500_instance != NULL )
192 free( krb_x500_instance );
193 krb_x500_instance = strdup( optarg );
197 case 'I': /* Run from inetd */
201 #ifdef LDAP_CONNECTIONLESS
202 case 'U': /* UDP only (no TCP) */
208 case 'u': /* allow UDP requests (CLDAP) */
213 #endif /* LDAP_CONNECTIONLESS */
221 if ( optind < argc ) {
226 #ifdef LDAP_CONNECTIONLESS
227 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
233 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
236 dtblsize = sysconf( _SC_OPEN_MAX );
237 #elif HAVE_GETDTABLESIZE
238 dtblsize = getdtablesize();
240 dtblsize = FD_SETSIZE;
244 if( dtblsize > FD_SETSIZE ) {
245 dtblsize = FD_SETSIZE;
247 #endif /* FD_SETSIZE */
249 #if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
250 /* for setproctitle */
255 if ( (myname = strrchr( argv[0], '/' )) == NULL )
256 myname = strdup( argv[0] );
258 myname = strdup( myname + 1 );
261 * detach from the terminal if stderr is redirected or no
262 * debugging is wanted, and then arrange to reap children
266 #ifdef LDAP_SETPROCTITLE
267 setproctitle( "initializing" );
272 (void) SIGNAL( SIGCHLD, (void *) wait4child );
273 (void) SIGNAL( SIGINT, (void *) log_and_exit );
277 * set up syslogging (if desired)
281 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
283 openlog( myname, OPENLOG_OPTIONS );
288 * load the syntax handlers, oidtables, and initialize some stuff,
289 * then start listening
292 (void) quipu_syntaxes();
294 (void) pp_quipu_init( argv[0] );
296 #if ISODEPACKAGE == IC
298 dsa_operation_syntaxes();
301 (void) dsap_init( &dsapargc, &dsapargv );
302 (void) get_syntaxes();
304 len = sizeof( socktype );
305 getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
306 if ( socktype == SOCK_DGRAM ) {
307 #ifdef LDAP_CONNECTIONLESS
308 Debug( LDAP_DEBUG_ARGS,
309 "CLDAP request from unknown (%s)\n",
310 inet_ntoa( from.sin_addr ), 0, 0 );
311 conn_start_tv.tv_sec = 0;
314 #else /* LDAP_CONNECTIONLESS */
315 Debug( LDAP_DEBUG_ARGS,
316 "Compile with -DLDAP_CONNECTIONLESS for UDP support\n",0,0,0 );
317 #endif /* LDAP_CONNECTIONLESS */
322 if ( getpeername( ns, (struct sockaddr *) &from, &len )
324 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
325 sizeof(from.sin_addr.s_addr), AF_INET );
326 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
327 (hp == NULL) ? "unknown" : hp->h_name,
328 inet_ntoa( from.sin_addr ), 0 );
331 syslog( LOG_INFO, "connection from %s (%s)",
332 (hp == NULL) ? "unknown" : hp->h_name,
333 inet_ntoa( from.sin_addr ) );
336 #ifdef LDAP_SETPROCTITLE
337 sprintf( title, "%s %d\n", hp == NULL ?
338 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
339 setproctitle( title );
342 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
349 tcps = set_socket( myport, 0 );
351 #ifdef LDAP_CONNECTIONLESS
353 udps = udp_init( myport, 1 );
357 * loop, wait for a connection, then fork off a child to handle it
358 * if we are doing CLDAP as well, handle those requests on the fly
361 #ifdef LDAP_SETPROCTITLE
362 #ifdef LDAP_CONNECTIONLESS
363 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
364 do_udp ? "udp" : "", myport );
366 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
368 setproctitle( title );
374 FD_SET( tcps, &readfds );
375 #ifdef LDAP_CONNECTIONLESS
377 FD_SET( udps, &readfds );
380 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
382 if ( ldap_debug ) perror( "main select" );
387 #ifdef LDAP_CONNECTIONLESS
388 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
389 do_queries( udps, 1 );
393 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
398 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
401 if ( ldap_debug ) perror( "accept" );
406 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
407 sizeof(from.sin_addr.s_addr), AF_INET );
410 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
411 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
413 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
414 (hp == NULL) ? "unknown" : hp->h_name,
415 inet_ntoa( from.sin_addr ), 0 );
418 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
419 (hp == NULL) ? "unknown" : hp->h_name,
420 inet_ntoa( from.sin_addr ) );
426 #endif /* TCP_WRAPPERS */
428 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
429 (hp == NULL) ? "unknown" : hp->h_name,
430 inet_ntoa( from.sin_addr ), 0 );
434 syslog( LOG_INFO, "connection from %s (%s)",
435 (hp == NULL) ? "unknown" : hp->h_name,
436 inet_ntoa( from.sin_addr ) );
440 /* This is for debug on terminal on VMS */
442 #ifdef LDAP_SETPROCTITLE
443 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
446 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
447 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
453 switch( pid = fork() ) {
456 #ifdef LDAP_SETPROCTITLE
457 sprintf( title, "%s (%d)\n", hp == NULL ?
458 inet_ntoa( from.sin_addr ) : hp->h_name,
460 setproctitle( title );
462 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
463 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
468 case -1: /* failed */
470 if ( ldap_debug ) perror( "fork" );
473 syslog( LOG_ERR, "fork failed %m" );
474 /* let things cool off */
478 default: /* parent */
480 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
491 int udp /* is this a UDP (CLDAP) request? */
496 struct timeval timeout;
498 #ifdef LDAP_CONNECTIONLESS
499 struct sockaddr saddr, faddr;
500 struct sockaddr *saddrlist[ 1 ];
501 #endif /* LDAP_CONNECTIONLESS */
503 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
504 udp ? " udp" : "", 0, 0 );
507 * Loop, wait for a request from the client or a response from
508 * a dsa, then handle it. Dsap_ad is always a connection to the
509 * "default" dsa. Other connections can be made as a result of
510 * a referral being chased down. These association descriptors
511 * are kept track of with the message that caused the referral.
512 * The set_dsa_fds() routine traverses the list of outstanding
513 * messages, setting the appropriate bits in readfds.
520 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
521 sb.sb_sd = clientsock;
522 sb.sb_naddr = ( udp ) ? 1 : 0;
523 #ifdef LDAP_CONNECTIONLESS
524 sb.sb_addrs = (void **)saddrlist;
525 sb.sb_fromaddr = &faddr;
526 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
528 sb.sb_ber.ber_buf = NULL;
529 sb.sb_ber.ber_ptr = NULL;
530 sb.sb_ber.ber_end = NULL;
532 timeout.tv_sec = idletime;
535 struct conn *dsaconn;
536 extern struct conn *conns;
539 FD_SET( clientsock, &readfds );
540 conn_setfds( &readfds );
543 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
544 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
545 for ( i = 0; i < dtblsize; i++ ) {
546 if ( FD_ISSET( i, &readfds ) ) {
547 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
551 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
556 * hack - because of lber buffering, there might be stuff
557 * already waiting for us on the client sock.
560 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
561 if ( (rc = select( dtblsize, &readfds, 0, 0,
562 udp ? 0 : &timeout )) < 1 ) {
564 if ( ldap_debug ) perror( "do_queries select" );
567 log_and_exit( 0 ); /* idle timeout */
569 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
572 /* client gone away - we can too */
573 if ( isclosed( clientsock ) )
577 * check if a dsa conn has gone away -
586 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
587 FD_ISSET( clientsock, &readfds ) ) {
588 client_request( &sb, conns, udp );
590 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
591 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
596 dsa_response( dsaconn, &sb );
604 int udp /* UDP port? */
608 struct sockaddr_in addr;
610 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
615 /* set option so clients can't keep us from coming back up */
617 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
619 perror( "setsockopt" );
624 (void)memset( (void *)&addr, '\0', sizeof( addr ));
625 addr.sin_family = AF_INET;
626 addr.sin_addr.s_addr = INADDR_ANY;
627 addr.sin_port = htons( port );
628 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
634 /* listen for connections */
635 if ( listen( s, 5 ) == -1 ) {
641 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
642 udp ? "udp" : "tcp", port, 0 );
647 static RETSIGTYPE wait4child()
650 WAITSTATUSTYPE status;
653 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
656 while( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) > 0 )
659 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
663 (void) SIGNAL( SIGCHLD, (void *) wait4child );
668 log_and_exit( int exitcode )
673 if ( conn_start_tv.tv_sec == 0 ) {
674 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
676 gettimeofday( &tv, (struct timezone *)NULL );
677 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
678 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
686 #ifdef LDAP_CONNECTIONLESS
695 extern char *dsa_address;
696 extern struct PSAPaddr *psap_cpy();
697 extern struct conn *conns;
700 s = set_socket( port, 1 );
703 conns->c_dn = strdup("");
704 conns->c_cred = strdup("");
705 conns->c_credlen = 0;
706 conns->c_method = LDAP_AUTH_SIMPLE;
708 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
710 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
711 dsa_address : "NULL" );
714 conns->c_paddr = psap_cpy(conns->c_paddr);
717 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
718 fprintf(stderr, "Cannot bind to directory\n");
721 if ( matched != NULL )
724 return( createsocket ? s : 0 );