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 int allow_severity = LOG_INFO;
49 int deny_severity = LOG_NOTICE;
50 #endif /* TCP_WRAPPERS */
55 static SIG_FN wait4child();
72 int idletime = DEFAULT_TIMEOUT;
73 int referral_connection_timeout = DEFAULT_REFERRAL_TIMEOUT;
74 struct timeval conn_start_tv;
76 char *krb_ldap_service = "ldapserver";
77 char *krb_x500_service = "x500dsa";
78 char *krb_x500_instance;
80 char *kerberos_keyfile;
86 extern char Versionstr[];
91 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
93 fprintf( stderr, " [ -U | -t timeout ]" );
95 fprintf( stderr, " [ -t timeout ]" );
97 fprintf( stderr, " [-I]" );
99 fprintf( stderr, " [-i dsainstance]" );
101 fprintf( stderr, "\n" );
112 int myport = LDAP_PORT;
113 int i, pid, socktype;
117 struct sockaddr_in from;
122 #ifndef NOSETPROCTITLE
131 /* Pick up socket from inetd-type server on VMS */
132 if ( (ns = socket_from_server( NULL )) > 0 )
135 /* Socket from inetd is usually 0 */
140 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
144 dsapargv[0] = argv[0];
150 kerberos_keyfile = "";
153 /* process command line arguments */
154 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
156 case 'c': /* specify dsa to contact */
157 dsapargv[1] = "-call";
158 dsapargv[2] = strdup( optarg );
162 case 'd': /* turn on debugging */
164 ldap_debug = atoi( optarg );
165 if ( ldap_debug & LDAP_DEBUG_PACKETS )
166 lber_debug = ldap_debug;
168 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
172 case 'l': /* do syslogging */
176 case 'p': /* specify port number */
177 myport = atoi( optarg );
180 case 'r': /* timeout for referral connections */
181 referral_connection_timeout = atoi( optarg );
184 case 't': /* timeout for idle connections */
185 idletime = atoi( optarg );
189 case 'f': /* kerberos key file */
190 kerberos_keyfile = strdup( optarg );
193 case 'i': /* x500 dsa kerberos instance */
194 if ( krb_x500_instance != NULL )
195 free( krb_x500_instance );
196 krb_x500_instance = strdup( optarg );
200 case 'I': /* Run from inetd */
205 case 'U': /* UDP only (no TCP) */
211 case 'u': /* allow UDP requests (CLDAP) */
224 if ( optind < argc ) {
230 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
236 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
239 dtblsize = sysconf( _SC_OPEN_MAX );
240 #else /* USE_SYSCONF */
241 dtblsize = getdtablesize();
242 #endif /* USE_SYSCONF */
244 #ifndef NOSETPROCTITLE
245 /* for setproctitle */
250 if ( (myname = strrchr( argv[0], '/' )) == NULL )
251 myname = strdup( argv[0] );
253 myname = strdup( myname + 1 );
256 * detach from the terminal if stderr is redirected or no
257 * debugging is wanted, and then arrange to reap children
261 #ifndef NOSETPROCTITLE
262 setproctitle( "initializing" );
267 (void) SIGNAL( SIGCHLD, (void *) wait4child );
268 (void) SIGNAL( SIGINT, (void *) log_and_exit );
272 * set up syslogging (if desired)
276 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
278 openlog( myname, OPENLOG_OPTIONS );
283 * load the syntax handlers, oidtables, and initialize some stuff,
284 * then start listening
287 (void) quipu_syntaxes();
289 (void) pp_quipu_init( argv[0] );
291 #if ISODEPACKAGE == IC
293 dsa_operation_syntaxes();
296 (void) dsap_init( &dsapargc, &dsapargv );
297 (void) get_syntaxes();
299 len = sizeof( socktype );
300 getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
301 if ( socktype == SOCK_DGRAM ) {
303 Debug( LDAP_DEBUG_ARGS,
304 "CLDAP request from unknown (%s)\n",
305 inet_ntoa( from.sin_addr ), 0, 0 );
306 conn_start_tv.tv_sec = 0;
310 Debug( LDAP_DEBUG_ARGS,
311 "Compile with -DCLDAP for UDP support\n",0,0,0 );
317 if ( getpeername( ns, (struct sockaddr *) &from, &len )
319 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
320 sizeof(from.sin_addr.s_addr), AF_INET );
321 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
322 (hp == NULL) ? "unknown" : hp->h_name,
323 inet_ntoa( from.sin_addr ), 0 );
326 syslog( LOG_INFO, "connection from %s (%s)",
327 (hp == NULL) ? "unknown" : hp->h_name,
328 inet_ntoa( from.sin_addr ) );
331 #ifndef NOSETPROCTITLE
332 sprintf( title, "%s %d\n", hp == NULL ?
333 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
334 setproctitle( title );
337 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
344 tcps = set_socket( myport, 0 );
348 udps = udp_init( myport, 1 );
352 * loop, wait for a connection, then fork off a child to handle it
353 * if we are doing CLDAP as well, handle those requests on the fly
356 #ifndef NOSETPROCTITLE
358 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
359 do_udp ? "udp" : "", myport );
361 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
363 setproctitle( title );
369 FD_SET( tcps, &readfds );
372 FD_SET( udps, &readfds );
375 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
377 if ( ldap_debug ) perror( "main select" );
383 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
384 do_queries( udps, 1 );
388 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
393 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
396 if ( ldap_debug ) perror( "accept" );
401 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
402 sizeof(from.sin_addr.s_addr), AF_INET );
405 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
406 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
408 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
409 (hp == NULL) ? "unknown" : hp->h_name,
410 inet_ntoa( from.sin_addr ), 0 );
413 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
414 (hp == NULL) ? "unknown" : hp->h_name,
415 inet_ntoa( from.sin_addr ) );
421 #endif /* TCP_WRAPPERS */
423 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
424 (hp == NULL) ? "unknown" : hp->h_name,
425 inet_ntoa( from.sin_addr ), 0 );
429 syslog( LOG_INFO, "connection from %s (%s)",
430 (hp == NULL) ? "unknown" : hp->h_name,
431 inet_ntoa( from.sin_addr ) );
435 /* This is for debug on terminal on VMS */
437 #ifndef NOSETPROCTITLE
438 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
441 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
442 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
448 switch( pid = fork() ) {
451 #ifndef NOSETPROCTITLE
452 sprintf( title, "%s (%d)\n", hp == NULL ?
453 inet_ntoa( from.sin_addr ) : hp->h_name,
455 setproctitle( title );
457 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
458 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
463 case -1: /* failed */
465 if ( ldap_debug ) perror( "fork" );
468 syslog( LOG_ERR, "fork failed %m" );
469 /* let things cool off */
473 default: /* parent */
475 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
486 int udp /* is this a UDP (CLDAP) request? */
491 struct timeval timeout;
494 struct sockaddr saddr, faddr;
495 struct sockaddr *saddrlist[ 1 ];
498 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
499 udp ? " udp" : "", 0, 0 );
502 * Loop, wait for a request from the client or a response from
503 * a dsa, then handle it. Dsap_ad is always a connection to the
504 * "default" dsa. Other connections can be made as a result of
505 * a referral being chased down. These association descriptors
506 * are kept track of with the message that caused the referral.
507 * The set_dsa_fds() routine traverses the list of outstanding
508 * messages, setting the appropriate bits in readfds.
515 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
516 sb.sb_sd = clientsock;
517 sb.sb_naddr = ( udp ) ? 1 : 0;
519 sb.sb_addrs = (void **)saddrlist;
520 sb.sb_fromaddr = &faddr;
521 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
523 sb.sb_ber.ber_buf = NULL;
524 sb.sb_ber.ber_ptr = NULL;
525 sb.sb_ber.ber_end = NULL;
527 timeout.tv_sec = idletime;
530 struct conn *dsaconn;
531 extern struct conn *conns;
534 FD_SET( clientsock, &readfds );
535 conn_setfds( &readfds );
538 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
539 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
540 for ( i = 0; i < dtblsize; i++ ) {
541 if ( FD_ISSET( i, &readfds ) ) {
542 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
546 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
551 * hack - because of lber buffering, there might be stuff
552 * already waiting for us on the client sock.
555 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
556 if ( (rc = select( dtblsize, &readfds, 0, 0,
557 udp ? 0 : &timeout )) < 1 ) {
559 if ( ldap_debug ) perror( "do_queries select" );
562 log_and_exit( 0 ); /* idle timeout */
564 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
567 /* client gone away - we can too */
568 if ( isclosed( clientsock ) )
572 * check if a dsa conn has gone away -
581 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
582 FD_ISSET( clientsock, &readfds ) ) {
583 client_request( &sb, conns, udp );
585 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
586 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
591 dsa_response( dsaconn, &sb );
599 int udp /* UDP port? */
603 struct sockaddr_in addr;
605 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
610 /* set option so clients can't keep us from coming back up */
612 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
614 perror( "setsockopt" );
619 (void)memset( (void *)&addr, '\0', sizeof( addr ));
620 addr.sin_family = AF_INET;
621 addr.sin_addr.s_addr = INADDR_ANY;
622 addr.sin_port = htons( port );
623 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
629 /* listen for connections */
630 if ( listen( s, 5 ) == -1 ) {
636 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
637 udp ? "udp" : "tcp", port, 0 );
642 static SIG_FN wait4child()
644 WAITSTATUSTYPE status;
646 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
649 while( waitpid( (pid_t) -1, 0, WAIT_FLAGS ) > 0 )
652 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
656 (void) SIGNAL( SIGCHLD, (void *) wait4child );
661 log_and_exit( int exitcode )
666 if ( conn_start_tv.tv_sec == 0 ) {
667 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
669 gettimeofday( &tv, (struct timezone *)NULL );
670 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
671 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
688 extern char *dsa_address;
689 extern struct PSAPaddr *psap_cpy();
690 extern struct conn *conns;
693 s = set_socket( port, 1 );
696 conns->c_dn = strdup("");
697 conns->c_cred = strdup("");
698 conns->c_credlen = 0;
699 conns->c_method = LDAP_AUTH_SIMPLE;
701 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
703 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
704 dsa_address : "NULL" );
707 conns->c_paddr = psap_cpy(conns->c_paddr);
710 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
711 fprintf(stderr, "Cannot bind to directory\n");
714 if ( matched != NULL )
717 return( createsocket ? s : 0 );