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 */
245 if( dtblsize > FD_SETSIZE ) {
246 dtblsize = FD_SETSIZE;
248 #endif /* FD_SETSIZE */
250 #ifndef NOSETPROCTITLE
251 /* for setproctitle */
256 if ( (myname = strrchr( argv[0], '/' )) == NULL )
257 myname = strdup( argv[0] );
259 myname = strdup( myname + 1 );
262 * detach from the terminal if stderr is redirected or no
263 * debugging is wanted, and then arrange to reap children
267 #ifndef NOSETPROCTITLE
268 setproctitle( "initializing" );
273 (void) SIGNAL( SIGCHLD, (void *) wait4child );
274 (void) SIGNAL( SIGINT, (void *) log_and_exit );
278 * set up syslogging (if desired)
282 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
284 openlog( myname, OPENLOG_OPTIONS );
289 * load the syntax handlers, oidtables, and initialize some stuff,
290 * then start listening
293 (void) quipu_syntaxes();
295 (void) pp_quipu_init( argv[0] );
297 #if ISODEPACKAGE == IC
299 dsa_operation_syntaxes();
302 (void) dsap_init( &dsapargc, &dsapargv );
303 (void) get_syntaxes();
305 len = sizeof( socktype );
306 getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
307 if ( socktype == SOCK_DGRAM ) {
309 Debug( LDAP_DEBUG_ARGS,
310 "CLDAP request from unknown (%s)\n",
311 inet_ntoa( from.sin_addr ), 0, 0 );
312 conn_start_tv.tv_sec = 0;
316 Debug( LDAP_DEBUG_ARGS,
317 "Compile with -DCLDAP for UDP support\n",0,0,0 );
323 if ( getpeername( ns, (struct sockaddr *) &from, &len )
325 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
326 sizeof(from.sin_addr.s_addr), AF_INET );
327 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
328 (hp == NULL) ? "unknown" : hp->h_name,
329 inet_ntoa( from.sin_addr ), 0 );
332 syslog( LOG_INFO, "connection from %s (%s)",
333 (hp == NULL) ? "unknown" : hp->h_name,
334 inet_ntoa( from.sin_addr ) );
337 #ifndef NOSETPROCTITLE
338 sprintf( title, "%s %d\n", hp == NULL ?
339 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
340 setproctitle( title );
343 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
350 tcps = set_socket( myport, 0 );
354 udps = udp_init( myport, 1 );
358 * loop, wait for a connection, then fork off a child to handle it
359 * if we are doing CLDAP as well, handle those requests on the fly
362 #ifndef NOSETPROCTITLE
364 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
365 do_udp ? "udp" : "", myport );
367 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
369 setproctitle( title );
375 FD_SET( tcps, &readfds );
378 FD_SET( udps, &readfds );
381 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
383 if ( ldap_debug ) perror( "main select" );
389 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
390 do_queries( udps, 1 );
394 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
399 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
402 if ( ldap_debug ) perror( "accept" );
407 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
408 sizeof(from.sin_addr.s_addr), AF_INET );
411 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
412 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
414 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
415 (hp == NULL) ? "unknown" : hp->h_name,
416 inet_ntoa( from.sin_addr ), 0 );
419 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
420 (hp == NULL) ? "unknown" : hp->h_name,
421 inet_ntoa( from.sin_addr ) );
427 #endif /* TCP_WRAPPERS */
429 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
430 (hp == NULL) ? "unknown" : hp->h_name,
431 inet_ntoa( from.sin_addr ), 0 );
435 syslog( LOG_INFO, "connection from %s (%s)",
436 (hp == NULL) ? "unknown" : hp->h_name,
437 inet_ntoa( from.sin_addr ) );
441 /* This is for debug on terminal on VMS */
443 #ifndef NOSETPROCTITLE
444 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
447 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
448 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
454 switch( pid = fork() ) {
457 #ifndef NOSETPROCTITLE
458 sprintf( title, "%s (%d)\n", hp == NULL ?
459 inet_ntoa( from.sin_addr ) : hp->h_name,
461 setproctitle( title );
463 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
464 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
469 case -1: /* failed */
471 if ( ldap_debug ) perror( "fork" );
474 syslog( LOG_ERR, "fork failed %m" );
475 /* let things cool off */
479 default: /* parent */
481 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
492 int udp /* is this a UDP (CLDAP) request? */
497 struct timeval timeout;
500 struct sockaddr saddr, faddr;
501 struct sockaddr *saddrlist[ 1 ];
504 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
505 udp ? " udp" : "", 0, 0 );
508 * Loop, wait for a request from the client or a response from
509 * a dsa, then handle it. Dsap_ad is always a connection to the
510 * "default" dsa. Other connections can be made as a result of
511 * a referral being chased down. These association descriptors
512 * are kept track of with the message that caused the referral.
513 * The set_dsa_fds() routine traverses the list of outstanding
514 * messages, setting the appropriate bits in readfds.
521 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
522 sb.sb_sd = clientsock;
523 sb.sb_naddr = ( udp ) ? 1 : 0;
525 sb.sb_addrs = (void **)saddrlist;
526 sb.sb_fromaddr = &faddr;
527 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
529 sb.sb_ber.ber_buf = NULL;
530 sb.sb_ber.ber_ptr = NULL;
531 sb.sb_ber.ber_end = NULL;
533 timeout.tv_sec = idletime;
536 struct conn *dsaconn;
537 extern struct conn *conns;
540 FD_SET( clientsock, &readfds );
541 conn_setfds( &readfds );
544 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
545 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
546 for ( i = 0; i < dtblsize; i++ ) {
547 if ( FD_ISSET( i, &readfds ) ) {
548 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
552 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
557 * hack - because of lber buffering, there might be stuff
558 * already waiting for us on the client sock.
561 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
562 if ( (rc = select( dtblsize, &readfds, 0, 0,
563 udp ? 0 : &timeout )) < 1 ) {
565 if ( ldap_debug ) perror( "do_queries select" );
568 log_and_exit( 0 ); /* idle timeout */
570 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
573 /* client gone away - we can too */
574 if ( isclosed( clientsock ) )
578 * check if a dsa conn has gone away -
587 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
588 FD_ISSET( clientsock, &readfds ) ) {
589 client_request( &sb, conns, udp );
591 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
592 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
597 dsa_response( dsaconn, &sb );
605 int udp /* UDP port? */
609 struct sockaddr_in addr;
611 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
616 /* set option so clients can't keep us from coming back up */
618 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
620 perror( "setsockopt" );
625 (void)memset( (void *)&addr, '\0', sizeof( addr ));
626 addr.sin_family = AF_INET;
627 addr.sin_addr.s_addr = INADDR_ANY;
628 addr.sin_port = htons( port );
629 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
635 /* listen for connections */
636 if ( listen( s, 5 ) == -1 ) {
642 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
643 udp ? "udp" : "tcp", port, 0 );
648 static SIG_FN wait4child()
650 WAITSTATUSTYPE status;
652 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
655 while( waitpid( (pid_t) -1, 0, WAIT_FLAGS ) > 0 )
658 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
662 (void) SIGNAL( SIGCHLD, (void *) wait4child );
667 log_and_exit( int exitcode )
672 if ( conn_start_tv.tv_sec == 0 ) {
673 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
675 gettimeofday( &tv, (struct timezone *)NULL );
676 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
677 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
694 extern char *dsa_address;
695 extern struct PSAPaddr *psap_cpy();
696 extern struct conn *conns;
699 s = set_socket( port, 1 );
702 conns->c_dn = strdup("");
703 conns->c_cred = strdup("");
704 conns->c_credlen = 0;
705 conns->c_method = LDAP_AUTH_SIMPLE;
707 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
709 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
710 dsa_address : "NULL" );
713 conns->c_paddr = psap_cpy(conns->c_paddr);
716 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
717 fprintf(stderr, "Cannot bind to directory\n");
720 if ( matched != NULL )
723 return( createsocket ? s : 0 );