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 */
53 static int set_socket();
54 static void do_queries();
55 static RETSIGTYPE wait4child();
56 #ifdef LDAP_CONNECTIONLESS
57 static int udp_init();
69 #ifdef LDAP_CONNECTIONLESS
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 );
92 #ifdef LDAP_CONNECTIONLESS
93 fprintf( stderr, " [ -U | -t timeout ]" );
95 fprintf( stderr, " [ -t timeout ]" );
97 fprintf( stderr, " [-I]" );
99 fprintf( stderr, " [-i dsainstance]" );
101 fprintf( stderr, "\n" );
110 #ifdef LDAP_CONNECTIONLESS
113 int myport = LDAP_PORT;
114 int i, pid, socktype;
118 struct sockaddr_in from;
122 RETSIGTYPE wait4child();
123 #ifdef LDAP_PROCTITLE
130 /* Pick up socket from inetd-type server on VMS */
131 if ( (ns = socket_from_server( NULL )) > 0 )
134 /* Socket from inetd is usually 0 */
139 if ( (dsapargv = (char **) malloc( 4 * sizeof(char *) )) == NULL ) {
143 dsapargv[0] = argv[0];
149 kerberos_keyfile = "";
152 /* process command line arguments */
153 while ( (i = getopt( argc, argv, "d:lp:f:i:c:r:t:IuU" )) != EOF ) {
155 case 'c': /* specify dsa to contact */
156 dsapargv[1] = "-call";
157 dsapargv[2] = strdup( optarg );
161 case 'd': /* turn on debugging */
163 ldap_debug = atoi( optarg );
164 if ( ldap_debug & LDAP_DEBUG_PACKETS )
165 lber_debug = ldap_debug;
167 fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
171 case 'l': /* do syslogging */
175 case 'p': /* specify port number */
176 myport = atoi( optarg );
179 case 'r': /* timeout for referral connections */
180 referral_connection_timeout = atoi( optarg );
183 case 't': /* timeout for idle connections */
184 idletime = atoi( optarg );
188 case 'f': /* kerberos key file */
189 kerberos_keyfile = strdup( optarg );
192 case 'i': /* x500 dsa kerberos instance */
193 if ( krb_x500_instance != NULL )
194 free( krb_x500_instance );
195 krb_x500_instance = strdup( optarg );
199 case 'I': /* Run from inetd */
203 #ifdef LDAP_CONNECTIONLESS
204 case 'U': /* UDP only (no TCP) */
210 case 'u': /* allow UDP requests (CLDAP) */
215 #endif /* LDAP_CONNECTIONLESS */
223 if ( optind < argc ) {
228 #ifdef LDAP_CONNECTIONLESS
229 if ( do_udp && !do_tcp && idletime != DEFAULT_TIMEOUT ) {
235 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
238 dtblsize = sysconf( _SC_OPEN_MAX );
239 #elif HAVE_GETDTABLESIZE
240 dtblsize = getdtablesize();
242 dtblsize = FD_SETSIZE;
246 if( dtblsize > FD_SETSIZE ) {
247 dtblsize = FD_SETSIZE;
249 #endif /* FD_SETSIZE */
251 #if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
252 /* for setproctitle */
257 if ( (myname = strrchr( argv[0], '/' )) == NULL )
258 myname = strdup( argv[0] );
260 myname = strdup( myname + 1 );
263 * detach from the terminal if stderr is redirected or no
264 * debugging is wanted, and then arrange to reap children
268 #ifdef LDAP_PROCTITLE
269 setproctitle( "initializing" );
273 lutil_detach( ldap_debug, 1 );
275 lutil_detach( 0, 1 );
278 (void) SIGNAL( SIGCHLD, (void *) wait4child );
279 (void) SIGNAL( SIGINT, (void *) log_and_exit );
283 * set up syslogging (if desired)
287 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
289 openlog( myname, OPENLOG_OPTIONS );
294 * load the syntax handlers, oidtables, and initialize some stuff,
295 * then start listening
298 (void) quipu_syntaxes();
300 (void) pp_quipu_init( argv[0] );
302 #if ISODEPACKAGE == IC
304 dsa_operation_syntaxes();
307 (void) dsap_init( &dsapargc, &dsapargv );
308 (void) get_syntaxes();
310 len = sizeof( socktype );
311 getsockopt( ns, SOL_SOCKET, SO_TYPE, (char *)&socktype, &len );
312 if ( socktype == SOCK_DGRAM ) {
313 #ifdef LDAP_CONNECTIONLESS
314 Debug( LDAP_DEBUG_ARGS,
315 "CLDAP request from unknown (%s)\n",
316 inet_ntoa( from.sin_addr ), 0, 0 );
317 conn_start_tv.tv_sec = 0;
320 #else /* LDAP_CONNECTIONLESS */
321 Debug( LDAP_DEBUG_ARGS,
322 "Compile with -DLDAP_CONNECTIONLESS for UDP support\n",0,0,0 );
323 #endif /* LDAP_CONNECTIONLESS */
328 if ( getpeername( ns, (struct sockaddr *) &from, &len )
330 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
331 sizeof(from.sin_addr.s_addr), AF_INET );
332 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
333 (hp == NULL) ? "unknown" : hp->h_name,
334 inet_ntoa( from.sin_addr ), 0 );
337 syslog( LOG_INFO, "connection from %s (%s)",
338 (hp == NULL) ? "unknown" : hp->h_name,
339 inet_ntoa( from.sin_addr ) );
342 #ifdef LDAP_PROCTITLE
343 sprintf( title, "%s %d\n", hp == NULL ?
344 inet_ntoa( from.sin_addr ) : hp->h_name, myport );
345 setproctitle( title );
348 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
355 tcps = set_socket( myport, 0 );
357 #ifdef LDAP_CONNECTIONLESS
359 udps = udp_init( myport, 1 );
363 * loop, wait for a connection, then fork off a child to handle it
364 * if we are doing CLDAP as well, handle those requests on the fly
367 #ifdef LDAP_PROCTITLE
368 #ifdef LDAP_CONNECTIONLESS
369 sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
370 do_udp ? "udp" : "", myport );
372 sprintf( title, "listening %s %d", do_tcp ? "tcp" : "", myport );
374 setproctitle( title );
380 FD_SET( tcps, &readfds );
381 #ifdef LDAP_CONNECTIONLESS
383 FD_SET( udps, &readfds );
386 if ( select( dtblsize, &readfds, 0, 0, 0 ) < 1 ) {
388 if ( ldap_debug ) perror( "main select" );
393 #ifdef LDAP_CONNECTIONLESS
394 if ( do_udp && FD_ISSET( udps, &readfds ) ) {
395 do_queries( udps, 1 );
399 if ( !do_tcp || ! FD_ISSET( tcps, &readfds ) ) {
404 if ( (ns = accept( tcps, (struct sockaddr *) &from, &len ))
407 if ( ldap_debug ) perror( "accept" );
412 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
413 sizeof(from.sin_addr.s_addr), AF_INET );
416 if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
417 inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
419 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
420 (hp == NULL) ? "unknown" : hp->h_name,
421 inet_ntoa( from.sin_addr ), 0 );
424 syslog( LOG_NOTICE, "connection from %s (%s) denied.",
425 (hp == NULL) ? "unknown" : hp->h_name,
426 inet_ntoa( from.sin_addr ) );
432 #endif /* TCP_WRAPPERS */
434 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
435 (hp == NULL) ? "unknown" : hp->h_name,
436 inet_ntoa( from.sin_addr ), 0 );
440 syslog( LOG_INFO, "connection from %s (%s)",
441 (hp == NULL) ? "unknown" : hp->h_name,
442 inet_ntoa( from.sin_addr ) );
446 /* This is for debug on terminal on VMS */
448 #ifdef LDAP_PROCTITLE
449 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
452 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
453 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
459 switch( pid = fork() ) {
462 #ifdef LDAP_PROCTITLE
463 sprintf( title, "%s (%d)\n", hp == NULL ?
464 inet_ntoa( from.sin_addr ) : hp->h_name,
466 setproctitle( title );
468 gettimeofday( &conn_start_tv, (struct timezone *) NULL );
469 (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
474 case -1: /* failed */
476 if ( ldap_debug ) perror( "fork" );
479 syslog( LOG_ERR, "fork failed %m" );
480 /* let things cool off */
484 default: /* parent */
486 Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
497 int udp /* is this a UDP (CLDAP) request? */
502 struct timeval timeout;
504 #ifdef LDAP_CONNECTIONLESS
505 struct sockaddr saddr, faddr;
506 struct sockaddr *saddrlist[ 1 ];
507 #endif /* LDAP_CONNECTIONLESS */
509 Debug( LDAP_DEBUG_TRACE, "do_queries%s\n",
510 udp ? " udp" : "", 0, 0 );
513 * Loop, wait for a request from the client or a response from
514 * a dsa, then handle it. Dsap_ad is always a connection to the
515 * "default" dsa. Other connections can be made as a result of
516 * a referral being chased down. These association descriptors
517 * are kept track of with the message that caused the referral.
518 * The set_dsa_fds() routine traverses the list of outstanding
519 * messages, setting the appropriate bits in readfds.
526 (void) memset( (void *) &sb, '\0', sizeof( sb ) );
527 sb.sb_sd = clientsock;
528 sb.sb_naddr = ( udp ) ? 1 : 0;
529 #ifdef LDAP_CONNECTIONLESS
530 sb.sb_addrs = (void **)saddrlist;
531 sb.sb_fromaddr = &faddr;
532 sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
534 sb.sb_ber.ber_buf = NULL;
535 sb.sb_ber.ber_ptr = NULL;
536 sb.sb_ber.ber_end = NULL;
538 timeout.tv_sec = idletime;
541 struct conn *dsaconn;
542 extern struct conn *conns;
545 FD_SET( clientsock, &readfds );
546 conn_setfds( &readfds );
549 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
551 Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
552 for ( i = 0; i < dtblsize; i++ ) {
553 if ( FD_ISSET( i, &readfds ) ) {
554 Debug( LDAP_DEBUG_CONNS, " %d", i, 0,
558 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
563 * hack - because of lber buffering, there might be stuff
564 * already waiting for us on the client sock.
567 if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
568 if ( (rc = select( dtblsize, &readfds, 0, 0,
569 udp ? 0 : &timeout )) < 1 ) {
571 if ( ldap_debug ) perror( "do_queries select" );
574 log_and_exit( 0 ); /* idle timeout */
576 Debug( LDAP_DEBUG_ANY, "select returns %d!\n",
579 /* client gone away - we can too */
580 if ( isclosed( clientsock ) )
584 * check if a dsa conn has gone away -
593 if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
594 FD_ISSET( clientsock, &readfds ) ) {
595 client_request( &sb, conns, udp );
597 if ( (dsaconn = conn_getfd( &readfds )) == NULL ) {
598 Debug( LDAP_DEBUG_ANY, "No DSA activity!\n",
603 dsa_response( dsaconn, &sb );
612 int udp /* UDP port? */
616 struct sockaddr_in addr;
618 if ( (s = socket( AF_INET, udp ? SOCK_DGRAM:SOCK_STREAM, 0 )) == -1 ) {
623 /* set option so clients can't keep us from coming back up */
625 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
627 perror( "setsockopt" );
632 (void)memset( (void *)&addr, '\0', sizeof( addr ));
633 addr.sin_family = AF_INET;
634 addr.sin_addr.s_addr = INADDR_ANY;
635 addr.sin_port = htons( port );
636 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
642 /* listen for connections */
643 if ( listen( s, 5 ) == -1 ) {
649 Debug( LDAP_DEBUG_TRACE, "listening on %s port %d\n",
650 udp ? "udp" : "tcp", port, 0 );
655 static RETSIGTYPE wait4child()
658 WAITSTATUSTYPE status;
661 Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
664 while( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) > 0 )
667 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
671 (void) SIGNAL( SIGCHLD, (void *) wait4child );
676 log_and_exit( int exitcode )
681 if ( conn_start_tv.tv_sec == 0 ) {
682 syslog( LOG_INFO, "UDP exit(%d)", exitcode );
684 gettimeofday( &tv, (struct timezone *)NULL );
685 syslog( LOG_INFO, "TCP closed %d seconds, exit(%d)",
686 tv.tv_sec - conn_start_tv.tv_sec, exitcode );
694 #ifdef LDAP_CONNECTIONLESS
703 extern char *dsa_address;
704 extern struct PSAPaddr *psap_cpy();
705 extern struct conn *conns;
708 s = set_socket( port, 1 );
711 conns->c_dn = strdup("");
712 conns->c_cred = strdup("");
713 conns->c_credlen = 0;
714 conns->c_method = LDAP_AUTH_SIMPLE;
716 if ( dsa_address == NULL || (conns->c_paddr = str2paddr( dsa_address ))
718 fprintf(stderr, "Bad DSA address (%s)\n", dsa_address ?
719 dsa_address : "NULL" );
722 conns->c_paddr = psap_cpy(conns->c_paddr);
725 if ( do_bind_real(conns, &bound, &matched) != LDAP_SUCCESS) {
726 fprintf(stderr, "Cannot bind to directory\n");
729 if ( matched != NULL )
732 return( createsocket ? s : 0 );