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;
110 int i, pid, socktype;
114 struct sockaddr_in from;
118 #ifdef LDAP_PROCTITLE
123 /* Pick up socket from inetd-type server on VMS */
124 if ( (ns = socket_from_server( NULL )) > 0 )
127 /* Socket from inetd is usually 0 */
132 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
136 dsapargv[0] = argv[0];
142 kerberos_keyfile = "";
145 /* process command line arguments */
146 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
148 case 'c': /* specify dsa to contact */
149 dsapargv[1] = "-call";
150 dsapargv[2] = strdup( optarg );
154 case 'd': /* set debug level and 'do not detach' flag */
157 ldap_debug = atoi( optarg );
158 if ( ldap_debug & LDAP_DEBUG_PACKETS )
159 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
161 if ( atoi( optarg ) != 0 )
162 fputs( "Not compiled with -DLDAP_DEBUG!\n", stderr );
166 case 'l': /* do syslogging */
170 case 'p': /* specify port number */
171 myport = atoi( optarg );
174 case 'r': /* timeout for referral connections */
175 referral_connection_timeout = atoi( optarg );
178 case 't': /* timeout for idle connections */
179 idletime = atoi( optarg );
183 case 'f': /* kerberos key file */
184 kerberos_keyfile = strdup( optarg );
187 case 'i': /* x500 dsa kerberos instance */
188 if ( krb_x500_instance != NULL )
189 free( krb_x500_instance );
190 krb_x500_instance = strdup( optarg );
194 case 'I': /* Run from inetd */
198 #ifdef LDAP_CONNECTIONLESS
199 case 'U': /* UDP only (no TCP) */
205 case 'u': /* allow UDP requests (CLDAP) */
210 #endif /* LDAP_CONNECTIONLESS */
218 if ( optind < argc ) {
223 #ifdef LDAP_CONNECTIONLESS
224 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
230 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
233 dtblsize = sysconf( _SC_OPEN_MAX );
234 #elif HAVE_GETDTABLESIZE
235 dtblsize = getdtablesize();
237 dtblsize = FD_SETSIZE;
241 if( dtblsize > FD_SETSIZE ) {
242 dtblsize = FD_SETSIZE;
244 #endif /* FD_SETSIZE */
246 #if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
247 /* for setproctitle */
252 if ( (myname = strrchr( argv[0], '/' )) == NULL )
253 myname = strdup( argv[0] );
255 myname = strdup( myname + 1 );
258 * detach from the terminal if stderr is redirected or no
259 * debugging is wanted, and then arrange to reap children
263 #ifdef LDAP_PROCTITLE
264 setproctitle( "initializing" );
267 lutil_detach( no_detach, 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 ber_pvt_sb_init( &sb );
518 ber_pvt_sb_set_desc( &sb, clientsock );
519 ber_pvt_sb_set_io( &sb, (udp) ? &ber_pvt_sb_io_udp :
520 &ber_pvt_sb_io_tcp, NULL );
521 timeout.tv_sec = idletime;
524 struct conn *dsaconn;
527 FD_SET( clientsock, &readfds );
528 conn_setfds( &readfds );
531 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
533 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
534 for ( i = 0; i < dtblsize; i++ ) {
535 if ( FD_ISSET( i, &readfds ) ) {
536 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
540 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
545 * hack - because of lber buffering, there might be stuff
546 * already waiting for us on the client sock.
549 if ( ! ber_pvt_sb_data_ready( &sb ) ) {
550 if ( (rc = select( dtblsize, &readfds, 0, 0,
551 udp ? 0 : &timeout )) < 1 ) {
553 if ( ldap_debug ) perror( "do_queries select" );
556 log_and_exit( 0 ); /* idle timeout */
558 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
561 /* client gone away - we can too */
562 if ( isclosed( clientsock ) )
566 * check if a dsa conn has gone away -
575 if ( ber_pvt_sb_data_ready( &sb ) ||
576 FD_ISSET( clientsock, &readfds ) ) {
577 client_request( &sb, conns, udp );
579 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
580 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
585 dsa_response( dsaconn, &sb );
594 int udp /* UDP port? */
598 struct sockaddr_in addr;
600 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
606 /* set option so clients can't keep us from coming back up */
608 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
611 perror( "setsockopt" );
616 /* enable keep alives */
618 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (void *) &i, sizeof(i) )
621 perror( "setsockopt" );
627 (void)memset( (void *)&addr, '\0', sizeof( addr ));
628 addr.sin_family = AF_INET;
629 addr.sin_addr.s_addr = htonl(INADDR_ANY);
630 addr.sin_port = htons( port );
631 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
637 /* listen for connections */
638 if ( listen( s, 5 ) == -1 ) {
644 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
645 udp ? "udp" : "tcp", port, 0 );
651 wait4child( int sig )
654 WAITSTATUSTYPE status;
657 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
660 while( waitpid( (pid_t) -1, (int *) NULL, WAIT_FLAGS ) > 0 )
663 while ( wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
667 (void) SIGNAL( SIGCHLD, wait4child );
672 log_and_exit( int exitcode )
677 if ( conn_start_tv.tv_sec == 0 ) {
678 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
680 gettimeofday( &tv, (struct timezone *)NULL );
681 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
682 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
690 #ifdef LDAP_CONNECTIONLESS
701 s = set_socket( port, 1 );
704 conns->c_dn = strdup("");
705 conns->c_cred = strdup("");
706 conns->c_credlen = 0;
707 conns->c_method = LDAP_AUTH_SIMPLE;
709 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
711 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
712 dsa_address : "NULL" );
715 conns->c_paddr = psap_cpy(conns->c_paddr);
718 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
719 fprintf(stderr, "Cannot bind to directory\n");
722 if ( matched != NULL )
725 return( createsocket ? s : 0 );