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 */
43 #include "lutil.h" /* Get lutil_detach() */
48 int allow_severity = LOG_INFO;
49 int deny_severity = LOG_NOTICE;
50 #endif /* TCP_WRAPPERS */
52 static int set_socket( int port, int udp );
53 static void do_queries( int clientsock, int udp );
54 static RETSIGTYPE wait4child( int sig );
55 #ifdef LDAP_CONNECTIONLESS
56 static int udp_init( int port, int createsocket );
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;
88 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
89 #ifdef LDAP_CONNECTIONLESS
90 fprintf( stderr, " [ -U | -t timeout ]" );
92 fprintf( stderr, " [ -t timeout ]" );
94 fprintf( stderr, " [-I]" );
96 fprintf( stderr, " [-i dsainstance]" );
98 fprintf( stderr, "\n" );
102 main( int argc, char **argv )
105 #ifdef LDAP_CONNECTIONLESS
108 int myport = LDAP_PORT;
109 int i, pid, socktype;
113 struct sockaddr_in from;
117 #ifdef LDAP_PROCTITLE
122 /* Pick up socket from inetd-type server on VMS */
123 if ( (ns = socket_from_server( NULL )) > 0 )
126 /* Socket from inetd is usually 0 */
131 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
135 dsapargv[0] = argv[0];
141 kerberos_keyfile = "";
144 /* process command line arguments */
145 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
147 case 'c': /* specify dsa to contact */
148 dsapargv[1] = "-call";
149 dsapargv[2] = strdup( optarg );
153 case 'd': /* turn on debugging */
155 ldap_debug = atoi( optarg );
156 if ( ldap_debug & LDAP_DEBUG_PACKETS )
157 lber_debug = ldap_debug;
159 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
163 case 'l': /* do syslogging */
167 case 'p': /* specify port number */
168 myport = atoi( optarg );
171 case 'r': /* timeout for referral connections */
172 referral_connection_timeout = atoi( optarg );
175 case 't': /* timeout for idle connections */
176 idletime = atoi( optarg );
180 case 'f': /* kerberos key file */
181 kerberos_keyfile = strdup( optarg );
184 case 'i': /* x500 dsa kerberos instance */
185 if ( krb_x500_instance != NULL )
186 free( krb_x500_instance );
187 krb_x500_instance = strdup( optarg );
191 case 'I': /* Run from inetd */
195 #ifdef LDAP_CONNECTIONLESS
196 case 'U': /* UDP only (no TCP) */
202 case 'u': /* allow UDP requests (CLDAP) */
207 #endif /* LDAP_CONNECTIONLESS */
215 if ( optind < argc ) {
220 #ifdef LDAP_CONNECTIONLESS
221 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
227 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
230 dtblsize = sysconf( _SC_OPEN_MAX );
231 #elif HAVE_GETDTABLESIZE
232 dtblsize = getdtablesize();
234 dtblsize = FD_SETSIZE;
238 if( dtblsize > FD_SETSIZE ) {
239 dtblsize = FD_SETSIZE;
241 #endif /* FD_SETSIZE */
243 #if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
244 /* for setproctitle */
249 if ( (myname = strrchr( argv[0], '/' )) == NULL )
250 myname = strdup( argv[0] );
252 myname = strdup( myname + 1 );
255 * detach from the terminal if stderr is redirected or no
256 * debugging is wanted, and then arrange to reap children
260 #ifdef LDAP_PROCTITLE
261 setproctitle( "initializing" );
265 lutil_detach( ldap_debug, 1 );
267 lutil_detach( 0, 1 );
270 (void) SIGNAL( SIGCHLD, wait4child );
271 (void) SIGNAL( SIGINT, log_and_exit );
275 * set up syslogging (if desired)
279 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
281 openlog( myname, OPENLOG_OPTIONS );
286 * load the syntax handlers, oidtables, and initialize some stuff,
287 * then start listening
290 (void) quipu_syntaxes();
292 (void) pp_quipu_init( argv[0] );
294 #if ISODEPACKAGE == IC
296 dsa_operation_syntaxes();
299 (void) dsap_init( &dsapargc, &dsapargv );
300 (void) get_syntaxes();
302 len = sizeof( socktype );
303 getsockopt( ns, SOL_SOCKET, SO_TYPE, (char *)&socktype, &len );
304 if ( socktype == SOCK_DGRAM ) {
305 #ifdef LDAP_CONNECTIONLESS
306 Debug( LDAP_DEBUG_ARGS,
307 "CLDAP request from unknown (%s)\n",
308 inet_ntoa( from.sin_addr ), 0, 0 );
309 conn_start_tv.tv_sec = 0;
312 #else /* LDAP_CONNECTIONLESS */
313 Debug( LDAP_DEBUG_ARGS,
314 "Compile with -DLDAP_CONNECTIONLESS for UDP support\n",0,0,0 );
315 #endif /* LDAP_CONNECTIONLESS */
320 if ( getpeername( ns, (struct sockaddr *) &from, &len )
322 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
323 sizeof(from.sin_addr.s_addr), AF_INET );
324 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
325 (hp == NULL) ? "unknown" : hp->h_name,
326 inet_ntoa( from.sin_addr ), 0 );
329 syslog( LOG_INFO, "connection from %s (%s)",
330 (hp == NULL) ? "unknown" : hp->h_name,
331 inet_ntoa( from.sin_addr ) );
334 #ifdef LDAP_PROCTITLE
335 sprintf( title, "%s %d\n", hp == NULL ?
336 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
337 setproctitle( title );
340 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
347 tcps = set_socket( myport, 0 );
349 #ifdef LDAP_CONNECTIONLESS
351 udps = udp_init( myport, 1 );
355 * loop, wait for a connection, then fork off a child to handle it
356 * if we are doing CLDAP as well, handle those requests on the fly
359 #ifdef LDAP_PROCTITLE
360 #ifdef LDAP_CONNECTIONLESS
361 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
362 do_udp ? "udp" : "", myport );
364 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
366 setproctitle( title );
372 FD_SET( tcps, &readfds );
373 #ifdef LDAP_CONNECTIONLESS
375 FD_SET( udps, &readfds );
378 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
380 if ( ldap_debug ) perror( "main select" );
385 #ifdef LDAP_CONNECTIONLESS
386 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
387 do_queries( udps, 1 );
391 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
396 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
399 if ( ldap_debug ) perror( "accept" );
404 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
405 sizeof(from.sin_addr.s_addr), AF_INET );
408 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
409 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
411 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
412 (hp == NULL) ? "unknown" : hp->h_name,
413 inet_ntoa( from.sin_addr ), 0 );
416 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
417 (hp == NULL) ? "unknown" : hp->h_name,
418 inet_ntoa( from.sin_addr ) );
424 #endif /* TCP_WRAPPERS */
426 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
427 (hp == NULL) ? "unknown" : hp->h_name,
428 inet_ntoa( from.sin_addr ), 0 );
432 syslog( LOG_INFO, "connection from %s (%s)",
433 (hp == NULL) ? "unknown" : hp->h_name,
434 inet_ntoa( from.sin_addr ) );
438 /* This is for debug on terminal on VMS */
440 #ifdef LDAP_PROCTITLE
441 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
444 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
445 (void) SIGNAL( SIGPIPE, log_and_exit );
451 switch( pid = fork() ) {
454 #ifdef LDAP_PROCTITLE
455 sprintf( title, "%s (%d)\n", hp == NULL ?
456 inet_ntoa( from.sin_addr ) : hp->h_name,
458 setproctitle( title );
460 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
461 (void) SIGNAL( SIGPIPE, log_and_exit );
466 case -1: /* failed */
468 if ( ldap_debug ) perror( "fork" );
471 syslog( LOG_ERR, "fork failed %m" );
472 /* let things cool off */
476 default: /* parent */
478 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
489 int udp /* is this a UDP (CLDAP) request? */
494 struct timeval timeout;
496 #ifdef LDAP_CONNECTIONLESS
497 struct sockaddr saddr, faddr;
498 struct sockaddr *saddrlist[ 1 ];
499 #endif /* LDAP_CONNECTIONLESS */
501 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
502 udp ? " udp" : "", 0, 0 );
505 * Loop, wait for a request from the client or a response from
506 * a dsa, then handle it. Dsap_ad is always a connection to the
507 * "default" dsa. Other connections can be made as a result of
508 * a referral being chased down. These association descriptors
509 * are kept track of with the message that caused the referral.
510 * The set_dsa_fds() routine traverses the list of outstanding
511 * messages, setting the appropriate bits in readfds.
518 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
519 sb.sb_sd = clientsock;
520 sb.sb_naddr = ( udp ) ? 1 : 0;
521 #ifdef LDAP_CONNECTIONLESS
522 sb.sb_addrs = (void **)saddrlist;
523 sb.sb_fromaddr = &faddr;
524 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
526 sb.sb_ber.ber_buf = NULL;
527 sb.sb_ber.ber_ptr = NULL;
528 sb.sb_ber.ber_end = NULL;
530 timeout.tv_sec = idletime;
533 struct conn *dsaconn;
536 FD_SET( clientsock, &readfds );
537 conn_setfds( &readfds );
540 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
542 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
543 for ( i = 0; i < dtblsize; i++ ) {
544 if ( FD_ISSET( i, &readfds ) ) {
545 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
549 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
554 * hack - because of lber buffering, there might be stuff
555 * already waiting for us on the client sock.
558 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
559 if ( (rc = select( dtblsize, &readfds, 0, 0,
560 udp ? 0 : &timeout )) < 1 ) {
562 if ( ldap_debug ) perror( "do_queries select" );
565 log_and_exit( 0 ); /* idle timeout */
567 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
570 /* client gone away - we can too */
571 if ( isclosed( clientsock ) )
575 * check if a dsa conn has gone away -
584 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
585 FD_ISSET( clientsock, &readfds ) ) {
586 client_request( &sb, conns, udp );
588 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
589 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
594 dsa_response( dsaconn, &sb );
603 int udp /* UDP port? */
607 struct sockaddr_in addr;
609 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
614 /* set option so clients can't keep us from coming back up */
616 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
618 perror( "setsockopt" );
623 (void)memset( (void *)&addr, '\0', sizeof( addr ));
624 addr.sin_family = AF_INET;
625 addr.sin_addr.s_addr = INADDR_ANY;
626 addr.sin_port = htons( port );
627 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
633 /* listen for connections */
634 if ( listen( s, 5 ) == -1 ) {
640 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
641 udp ? "udp" : "tcp", port, 0 );
647 wait4child( int sig )
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, 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
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 );