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>
42 #include "lutil.h" /* Get lutil_detach() */
47 int allow_severity = LOG_INFO;
48 int deny_severity = LOG_NOTICE;
49 #endif /* TCP_WRAPPERS */
51 static int set_socket( int port, int udp );
52 static void do_queries( int clientsock, int udp );
53 static RETSIGTYPE wait4child( int sig );
54 #ifdef LDAP_CONNECTIONLESS
55 static int udp_init( int port, int createsocket );
67 #ifdef LDAP_CONNECTIONLESS
70 int idletime = DEFAULT_TIMEOUT;
71 int referral_connection_timeout = DEFAULT_REFERRAL_TIMEOUT;
72 struct timeval conn_start_tv;
74 char *krb_ldap_service = "ldapserver";
75 char *krb_x500_service = "x500dsa";
76 char *krb_x500_instance;
78 char *kerberos_keyfile;
87 fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
88 #ifdef LDAP_CONNECTIONLESS
89 fprintf( stderr, " [ -U | -t timeout ]" );
91 fprintf( stderr, " [ -t timeout ]" );
93 fprintf( stderr, " [-I]" );
95 fprintf( stderr, " [-i dsainstance]" );
97 fprintf( stderr, "\n" );
101 main( int argc, char **argv )
104 #ifdef LDAP_CONNECTIONLESS
107 int myport = LDAP_PORT;
108 int i, pid, socktype;
112 struct sockaddr_in from;
116 #ifdef LDAP_PROCTITLE
121 /* Pick up socket from inetd-type server on VMS */
122 if ( (ns = socket_from_server( NULL )) > 0 )
125 /* Socket from inetd is usually 0 */
130 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
134 dsapargv[0] = argv[0];
140 kerberos_keyfile = "";
143 /* process command line arguments */
144 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
146 case 'c': /* specify dsa to contact */
147 dsapargv[1] = "-call";
148 dsapargv[2] = strdup( optarg );
152 case 'd': /* turn on debugging */
154 ldap_debug = atoi( optarg );
155 if ( ldap_debug & LDAP_DEBUG_PACKETS )
156 lber_debug = ldap_debug;
158 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
162 case 'l': /* do syslogging */
166 case 'p': /* specify port number */
167 myport = atoi( optarg );
170 case 'r': /* timeout for referral connections */
171 referral_connection_timeout = atoi( optarg );
174 case 't': /* timeout for idle connections */
175 idletime = atoi( optarg );
179 case 'f': /* kerberos key file */
180 kerberos_keyfile = strdup( optarg );
183 case 'i': /* x500 dsa kerberos instance */
184 if ( krb_x500_instance != NULL )
185 free( krb_x500_instance );
186 krb_x500_instance = strdup( optarg );
190 case 'I': /* Run from inetd */
194 #ifdef LDAP_CONNECTIONLESS
195 case 'U': /* UDP only (no TCP) */
201 case 'u': /* allow UDP requests (CLDAP) */
206 #endif /* LDAP_CONNECTIONLESS */
214 if ( optind < argc ) {
219 #ifdef LDAP_CONNECTIONLESS
220 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
226 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
229 dtblsize = sysconf( _SC_OPEN_MAX );
230 #elif HAVE_GETDTABLESIZE
231 dtblsize = getdtablesize();
233 dtblsize = FD_SETSIZE;
237 if( dtblsize > FD_SETSIZE ) {
238 dtblsize = FD_SETSIZE;
240 #endif /* FD_SETSIZE */
242 #if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
243 /* for setproctitle */
248 if ( (myname = strrchr( argv[0], '/' )) == NULL )
249 myname = strdup( argv[0] );
251 myname = strdup( myname + 1 );
254 * detach from the terminal if stderr is redirected or no
255 * debugging is wanted, and then arrange to reap children
259 #ifdef LDAP_PROCTITLE
260 setproctitle( "initializing" );
264 lutil_detach( ldap_debug, 1 );
266 lutil_detach( 0, 1 );
269 (void) SIGNAL( SIGCHLD, wait4child );
270 (void) SIGNAL( SIGINT, log_and_exit );
274 * set up syslogging (if desired)
278 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
280 openlog( myname, OPENLOG_OPTIONS );
285 * load the syntax handlers, oidtables, and initialize some stuff,
286 * then start listening
289 (void) quipu_syntaxes();
291 (void) pp_quipu_init( argv[0] );
293 #if ISODEPACKAGE == IC
295 dsa_operation_syntaxes();
298 (void) dsap_init( &dsapargc, &dsapargv );
299 (void) get_syntaxes();
301 len = sizeof( socktype );
302 getsockopt( ns, SOL_SOCKET, SO_TYPE, (char *)&socktype, &len );
303 if ( socktype == SOCK_DGRAM ) {
304 #ifdef LDAP_CONNECTIONLESS
305 Debug( LDAP_DEBUG_ARGS,
306 "CLDAP request from unknown (%s)\n",
307 inet_ntoa( from.sin_addr ), 0, 0 );
308 conn_start_tv.tv_sec = 0;
311 #else /* LDAP_CONNECTIONLESS */
312 Debug( LDAP_DEBUG_ARGS,
313 "Compile with -DLDAP_CONNECTIONLESS for UDP support\n",0,0,0 );
314 #endif /* LDAP_CONNECTIONLESS */
319 if ( getpeername( ns, (struct sockaddr *) &from, &len )
321 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
322 sizeof(from.sin_addr.s_addr), AF_INET );
323 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
324 (hp == NULL) ? "unknown" : hp->h_name,
325 inet_ntoa( from.sin_addr ), 0 );
328 syslog( LOG_INFO, "connection from %s (%s)",
329 (hp == NULL) ? "unknown" : hp->h_name,
330 inet_ntoa( from.sin_addr ) );
333 #ifdef LDAP_PROCTITLE
334 sprintf( title, "%s %d\n", hp == NULL ?
335 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
336 setproctitle( title );
339 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
346 tcps = set_socket( myport, 0 );
348 #ifdef LDAP_CONNECTIONLESS
350 udps = udp_init( myport, 1 );
354 * loop, wait for a connection, then fork off a child to handle it
355 * if we are doing CLDAP as well, handle those requests on the fly
358 #ifdef LDAP_PROCTITLE
359 #ifdef LDAP_CONNECTIONLESS
360 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
361 do_udp ? "udp" : "", myport );
363 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
365 setproctitle( title );
371 FD_SET( tcps, &readfds );
372 #ifdef LDAP_CONNECTIONLESS
374 FD_SET( udps, &readfds );
377 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
379 if ( ldap_debug ) perror( "main select" );
384 #ifdef LDAP_CONNECTIONLESS
385 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
386 do_queries( udps, 1 );
390 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
395 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
398 if ( ldap_debug ) perror( "accept" );
403 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
404 sizeof(from.sin_addr.s_addr), AF_INET );
407 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
408 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
410 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
411 (hp == NULL) ? "unknown" : hp->h_name,
412 inet_ntoa( from.sin_addr ), 0 );
415 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
416 (hp == NULL) ? "unknown" : hp->h_name,
417 inet_ntoa( from.sin_addr ) );
423 #endif /* TCP_WRAPPERS */
425 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
426 (hp == NULL) ? "unknown" : hp->h_name,
427 inet_ntoa( from.sin_addr ), 0 );
431 syslog( LOG_INFO, "connection from %s (%s)",
432 (hp == NULL) ? "unknown" : hp->h_name,
433 inet_ntoa( from.sin_addr ) );
437 /* This is for debug on terminal on VMS */
439 #ifdef LDAP_PROCTITLE
440 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
443 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
444 (void) SIGNAL( SIGPIPE, log_and_exit );
450 switch( pid = fork() ) {
453 #ifdef LDAP_PROCTITLE
454 sprintf( title, "%s (%d)\n", hp == NULL ?
455 inet_ntoa( from.sin_addr ) : hp->h_name,
457 setproctitle( title );
459 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
460 (void) SIGNAL( SIGPIPE, log_and_exit );
465 case -1: /* failed */
467 if ( ldap_debug ) perror( "fork" );
470 syslog( LOG_ERR, "fork failed %m" );
471 /* let things cool off */
475 default: /* parent */
477 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
488 int udp /* is this a UDP (CLDAP) request? */
493 struct timeval timeout;
495 #ifdef LDAP_CONNECTIONLESS
496 struct sockaddr saddr, faddr;
497 struct sockaddr *saddrlist[ 1 ];
498 #endif /* LDAP_CONNECTIONLESS */
500 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
501 udp ? " udp" : "", 0, 0 );
504 * Loop, wait for a request from the client or a response from
505 * a dsa, then handle it. Dsap_ad is always a connection to the
506 * "default" dsa. Other connections can be made as a result of
507 * a referral being chased down. These association descriptors
508 * are kept track of with the message that caused the referral.
509 * The set_dsa_fds() routine traverses the list of outstanding
510 * messages, setting the appropriate bits in readfds.
517 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
518 sb.sb_sd = clientsock;
519 sb.sb_naddr = ( udp ) ? 1 : 0;
520 #ifdef LDAP_CONNECTIONLESS
521 sb.sb_addrs = (void **)saddrlist;
522 sb.sb_fromaddr = &faddr;
523 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
525 sb.sb_ber.ber_buf = NULL;
526 sb.sb_ber.ber_ptr = NULL;
527 sb.sb_ber.ber_end = NULL;
529 timeout.tv_sec = idletime;
532 struct conn *dsaconn;
535 FD_SET( clientsock, &readfds );
536 conn_setfds( &readfds );
539 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
541 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
542 for ( i = 0; i < dtblsize; i++ ) {
543 if ( FD_ISSET( i, &readfds ) ) {
544 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
548 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
553 * hack - because of lber buffering, there might be stuff
554 * already waiting for us on the client sock.
557 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
558 if ( (rc = select( dtblsize, &readfds, 0, 0,
559 udp ? 0 : &timeout )) < 1 ) {
561 if ( ldap_debug ) perror( "do_queries select" );
564 log_and_exit( 0 ); /* idle timeout */
566 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
569 /* client gone away - we can too */
570 if ( isclosed( clientsock ) )
574 * check if a dsa conn has gone away -
583 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
584 FD_ISSET( clientsock, &readfds ) ) {
585 client_request( &sb, conns, udp );
587 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
588 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
593 dsa_response( dsaconn, &sb );
602 int udp /* UDP port? */
606 struct sockaddr_in addr;
608 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
613 /* set option so clients can't keep us from coming back up */
615 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
617 perror( "setsockopt" );
622 (void)memset( (void *)&addr, '\0', sizeof( addr ));
623 addr.sin_family = AF_INET;
624 addr.sin_addr.s_addr = INADDR_ANY;
625 addr.sin_port = htons( port );
626 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
632 /* listen for connections */
633 if ( listen( s, 5 ) == -1 ) {
639 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
640 udp ? "udp" : "tcp", port, 0 );
646 wait4child( int sig )
649 WAITSTATUSTYPE status;
652 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
655 while( waitpid( (pid_t) -1, (int *) NULL, WAIT_FLAGS ) > 0 )
658 while ( wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
662 (void) SIGNAL( SIGCHLD, 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 );
685 #ifdef LDAP_CONNECTIONLESS
696 s = set_socket( port, 1 );
699 conns->c_dn = strdup("");
700 conns->c_cred = strdup("");
701 conns->c_credlen = 0;
702 conns->c_method = LDAP_AUTH_SIMPLE;
704 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
706 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
707 dsa_address : "NULL" );
710 conns->c_paddr = psap_cpy(conns->c_paddr);
713 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
714 fprintf(stderr, "Cannot bind to directory\n");
717 if ( matched != NULL )
720 return( createsocket ? s : 0 );