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
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
31 #include <sys/select.h>
34 #include <quipu/commonarg.h>
35 #include <quipu/ds_error.h>
43 #endif /* USE_SYSCONF */
48 static SIG_FN wait4child();
65 int idletime = DEFAULT_TIMEOUT;
66 int referral_connection_timeout = DEFAULT_REFERRAL_TIMEOUT;
67 struct timeval conn_start_tv;
69 char *krb_ldap_service = "ldapserver";
70 char *krb_x500_service = "x500dsa";
71 char *krb_x500_instance;
73 char *kerberos_keyfile;
79 extern char Versionstr[];
84 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
86 fprintf( stderr, " [ -U | -t timeout ]" );
88 fprintf( stderr, " [ -t timeout ]" );
90 fprintf( stderr, " [-I]" );
92 fprintf( stderr, " [-i dsainstance]" );
94 fprintf( stderr, "\n" );
105 int myport = LDAP_PORT;
106 int i, pid, socktype;
110 struct sockaddr_in from;
115 #ifndef NOSETPROCTITLE
124 /* Pick up socket from inetd-type server on VMS */
125 if ( (ns = socket_from_server( NULL )) > 0 )
128 /* Socket from inetd is usually 0 */
133 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
137 dsapargv[0] = argv[0];
143 kerberos_keyfile = "";
146 /* process command line arguments */
147 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
149 case 'c': /* specify dsa to contact */
150 dsapargv[1] = "-call";
151 dsapargv[2] = strdup( optarg );
155 case 'd': /* turn on debugging */
157 ldap_debug = atoi( optarg );
158 if ( ldap_debug & LDAP_DEBUG_PACKETS )
159 lber_debug = ldap_debug;
161 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
165 case 'l': /* do syslogging */
169 case 'p': /* specify port number */
170 myport = atoi( optarg );
173 case 'r': /* timeout for referral connections */
174 referral_connection_timeout = atoi( optarg );
177 case 't': /* timeout for idle connections */
178 idletime = atoi( optarg );
182 case 'f': /* kerberos key file */
183 kerberos_keyfile = strdup( optarg );
186 case 'i': /* x500 dsa kerberos instance */
187 if ( krb_x500_instance != NULL )
188 free( krb_x500_instance );
189 krb_x500_instance = strdup( optarg );
193 case 'I': /* Run from inetd */
198 case 'U': /* UDP only (no TCP) */
204 case 'u': /* allow UDP requests (CLDAP) */
217 if ( optind < argc ) {
223 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
229 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
232 dtblsize = sysconf( _SC_OPEN_MAX );
233 #else /* USE_SYSCONF */
234 dtblsize = getdtablesize();
235 #endif /* USE_SYSCONF */
237 #ifndef NOSETPROCTITLE
238 /* for setproctitle */
243 if ( (myname = strrchr( argv[0], '/' )) == NULL )
244 myname = strdup( argv[0] );
246 myname = strdup( myname + 1 );
249 * detach from the terminal if stderr is redirected or no
250 * debugging is wanted, and then arrange to reap children
254 #ifndef NOSETPROCTITLE
255 setproctitle( "initializing" );
260 (void) SIGNAL( SIGCHLD, (void *) wait4child );
261 (void) SIGNAL( SIGINT, (void *) log_and_exit );
265 * set up syslogging (if desired)
269 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
271 openlog( myname, OPENLOG_OPTIONS );
276 * load the syntax handlers, oidtables, and initialize some stuff,
277 * then start listening
280 (void) quipu_syntaxes();
282 (void) pp_quipu_init( argv[0] );
284 #if ISODEPACKAGE == IC
286 dsa_operation_syntaxes();
289 (void) dsap_init( &dsapargc, &dsapargv );
290 (void) get_syntaxes();
292 len = sizeof( socktype );
293 getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
294 if ( socktype == SOCK_DGRAM ) {
296 Debug( LDAP_DEBUG_ARGS,
297 "CLDAP request from unknown (%s)\n",
298 inet_ntoa( from.sin_addr ), 0, 0 );
299 conn_start_tv.tv_sec = 0;
303 Debug( LDAP_DEBUG_ARGS,
304 "Compile with -DCLDAP for UDP support\n",0,0,0 );
310 if ( getpeername( ns, (struct sockaddr *) &from, &len )
312 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
313 sizeof(from.sin_addr.s_addr), AF_INET );
314 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
315 (hp == NULL) ? "unknown" : hp->h_name,
316 inet_ntoa( from.sin_addr ), 0 );
319 syslog( LOG_INFO, "connection from %s (%s)",
320 (hp == NULL) ? "unknown" : hp->h_name,
321 inet_ntoa( from.sin_addr ) );
324 #ifndef NOSETPROCTITLE
325 sprintf( title, "%s %d\n", hp == NULL ?
326 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
327 setproctitle( title );
330 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
337 tcps = set_socket( myport, 0 );
341 udps = udp_init( myport, 1 );
345 * loop, wait for a connection, then fork off a child to handle it
346 * if we are doing CLDAP as well, handle those requests on the fly
349 #ifndef NOSETPROCTITLE
351 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
352 do_udp ? "udp" : "", myport );
354 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
356 setproctitle( title );
362 FD_SET( tcps, &readfds );
365 FD_SET( udps, &readfds );
368 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
370 if ( ldap_debug ) perror( "main select" );
376 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
377 do_queries( udps, 1 );
381 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
386 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
389 if ( ldap_debug ) perror( "accept" );
394 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
395 sizeof(from.sin_addr.s_addr), AF_INET );
396 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
397 (hp == NULL) ? "unknown" : hp->h_name,
398 inet_ntoa( from.sin_addr ), 0 );
401 syslog( LOG_INFO, "connection from %s (%s)",
402 (hp == NULL) ? "unknown" : hp->h_name,
403 inet_ntoa( from.sin_addr ) );
407 /* This is for debug on terminal on VMS */
409 #ifndef NOSETPROCTITLE
410 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
413 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
414 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
420 switch( pid = fork() ) {
423 #ifndef NOSETPROCTITLE
424 sprintf( title, "%s (%d)\n", hp == NULL ?
425 inet_ntoa( from.sin_addr ) : hp->h_name,
427 setproctitle( title );
429 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
430 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
435 case -1: /* failed */
437 if ( ldap_debug ) perror( "fork" );
440 syslog( LOG_ERR, "fork failed %m" );
441 /* let things cool off */
445 default: /* parent */
447 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
458 int udp /* is this a UDP (CLDAP) request? */
463 struct timeval timeout;
466 struct sockaddr saddr, faddr;
467 struct sockaddr *saddrlist[ 1 ];
470 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
471 udp ? " udp" : "", 0, 0 );
474 * Loop, wait for a request from the client or a response from
475 * a dsa, then handle it. Dsap_ad is always a connection to the
476 * "default" dsa. Other connections can be made as a result of
477 * a referral being chased down. These association descriptors
478 * are kept track of with the message that caused the referral.
479 * The set_dsa_fds() routine traverses the list of outstanding
480 * messages, setting the appropriate bits in readfds.
487 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
488 sb.sb_sd = clientsock;
489 sb.sb_naddr = ( udp ) ? 1 : 0;
491 sb.sb_addrs = (void **)saddrlist;
492 sb.sb_fromaddr = &faddr;
493 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
495 sb.sb_ber.ber_buf = NULL;
496 sb.sb_ber.ber_ptr = NULL;
497 sb.sb_ber.ber_end = NULL;
499 timeout.tv_sec = idletime;
502 struct conn *dsaconn;
503 extern struct conn *conns;
506 FD_SET( clientsock, &readfds );
507 conn_setfds( &readfds );
510 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
511 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
512 for ( i = 0; i < dtblsize; i++ ) {
513 if ( FD_ISSET( i, &readfds ) ) {
514 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
518 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
523 * hack - because of lber buffering, there might be stuff
524 * already waiting for us on the client sock.
527 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
528 if ( (rc = select( dtblsize, &readfds, 0, 0,
529 udp ? 0 : &timeout )) < 1 ) {
531 if ( ldap_debug ) perror( "do_queries select" );
534 log_and_exit( 0 ); /* idle timeout */
536 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
539 /* client gone away - we can too */
540 if ( isclosed( clientsock ) )
544 * check if a dsa conn has gone away -
553 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
554 FD_ISSET( clientsock, &readfds ) ) {
555 client_request( &sb, conns, udp );
557 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
558 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
563 dsa_response( dsaconn, &sb );
571 int udp /* UDP port? */
575 struct sockaddr_in addr;
577 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
582 /* set option so clients can't keep us from coming back up */
584 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
586 perror( "setsockopt" );
591 (void)memset( (void *)&addr, '\0', sizeof( addr ));
592 addr.sin_family = AF_INET;
593 addr.sin_addr.s_addr = INADDR_ANY;
594 addr.sin_port = htons( port );
595 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
601 /* listen for connections */
602 if ( listen( s, 5 ) == -1 ) {
608 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
609 udp ? "udp" : "tcp", port, 0 );
614 static SIG_FN wait4child()
616 WAITSTATUSTYPE status;
618 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
621 while( waitpid( (pid_t) -1, 0, WAIT_FLAGS ) > 0 )
624 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
628 (void) SIGNAL( SIGCHLD, (void *) wait4child );
633 log_and_exit( int exitcode )
638 if ( conn_start_tv.tv_sec == 0 ) {
639 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
641 gettimeofday( &tv, (struct timezone *)NULL );
642 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
643 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
660 extern char *dsa_address;
661 extern struct PSAPaddr *psap_cpy();
662 extern struct conn *conns;
665 s = set_socket( port, 1 );
668 conns->c_dn = strdup("");
669 conns->c_cred = strdup("");
670 conns->c_credlen = 0;
671 conns->c_method = LDAP_AUTH_SIMPLE;
673 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
675 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
676 dsa_address : "NULL" );
679 conns->c_paddr = psap_cpy(conns->c_paddr);
682 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
683 fprintf(stderr, "Cannot bind to directory\n");
686 if ( matched != NULL )
689 return( createsocket ? s : 0 );