home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / go500 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-28  |  9.0 KB  |  418 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of Michigan.
  3.  * All rights reserved.
  4.  *
  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.
  11.  */
  12.  
  13. #include "dixie.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/time.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <netdb.h>
  22. #include <sys/syslog.h>
  23. #include <sys/wait.h>
  24. #include <signal.h>
  25.  
  26. #ifndef FD_SET
  27. #define NFDBITS         32
  28. #define FD_SETSIZE      32
  29. #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  30. #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  31. #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  32. #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
  33. #endif
  34.  
  35. /*#define DAPUSER            "c=US@o=University of Minnesota@ou=Miscellaneous Servers@cn=Go500"*/
  36. #define DAPUSER NULL
  37. #define DEFAULT_BASE        "c=US@o=University of Minnesota"
  38. #define DEFAULT_DIXIEHOST    "x500.tc.umn.edu"
  39. #define DEFAULT_GO500PORT    5555
  40.  
  41. int    debug;
  42. int    dosyslog;
  43.  
  44. char    *dixiehost = DEFAULT_DIXIEHOST;
  45. char    *base = DEFAULT_BASE;
  46. int    strip = 3;
  47. int    searchaliases = 1;
  48.  
  49. char    *myhost = "mudhoney.micro.umn.edu";
  50. int    myport;
  51.  
  52. static usage( name )
  53. char    *name;
  54. {
  55.     fprintf( stderr, "usage: %s [-d debuglevel] [-p port] [-l] [-x dixiehost] [-b searchbase] [-a]\n" );
  56.     exit( 1 );
  57. }
  58.  
  59. main (argc, argv)
  60. int    argc;
  61. char    **argv;
  62. {
  63.     int            s, ns, rc;
  64.     int            port = -1;
  65.     int            tblsize;
  66.     int            i, pid;
  67.     char            *myname;
  68.     fd_set            readfds;
  69.     struct hostent        *hp;
  70.     struct sockaddr_in    from;
  71.     int            fromlen;
  72.     int            wait4child();
  73.     extern char        *optarg;
  74.  
  75.     while ( (i = getopt( argc, argv, "ab:d:lp:s:x:" )) != EOF ) {
  76.         switch( i ) {
  77.         case 'a':
  78.             searchaliases = 0;
  79.             break;
  80.  
  81.         case 'b':
  82.             base = strdup( optarg );
  83.             break;
  84.  
  85.         case 'd':
  86.             debug = atoi( optarg );
  87.             break;
  88.  
  89.         case 'l':
  90.             dosyslog = 1;
  91.             break;
  92.  
  93.         case 'p':
  94.             port = atoi( optarg );
  95.             break;
  96.  
  97.         case 's':
  98.             strip = atoi( optarg );
  99.             break;
  100.  
  101.         case 'x':
  102.             dixiehost = strdup( optarg );
  103.             break;
  104.  
  105.         default:
  106.             usage( argv[0] );
  107.         }
  108.     }
  109.  
  110.     /* detach if stderr is redirected or no debugging */
  111.     (void) detach( debug );
  112.  
  113.     if ( (myname = strrchr( argv[0], '/' )) == NULL )
  114.         myname = strdup( argv[0] );
  115.     else
  116.         myname = strdup( myname + 1 );
  117.  
  118.     if ( dosyslog && openlog( myname, LOG_PID | LOG_NOWAIT, LOG_LOCAL3 )
  119.         < 0 ) {
  120.         perror("openlog");
  121.     }
  122.     if ( dosyslog ) syslog( LOG_INFO, "initializing" );
  123.  
  124.     /* set up the socket to listen on */
  125.     s = set_socket( port );
  126.  
  127.     /* arrange to reap children */
  128.     (void) signal( SIGCHLD, wait4child );
  129.  
  130.     tblsize = getdtablesize();
  131.     for ( ;; ) {
  132.         FD_ZERO( &readfds );
  133.         FD_SET( s, &readfds );
  134.  
  135.         if ( (rc = select( tblsize, &readfds, 0, 0 ,0 )) == -1 ) {
  136.             if ( debug ) perror( "select" );
  137.             continue;
  138.         } else if ( rc == 0 ) {
  139.             continue;
  140.         }
  141.  
  142.         if ( ! FD_ISSET( s, &readfds ) )
  143.             continue;
  144.  
  145.         fromlen = sizeof(from);
  146.         if ( (ns = accept( s, &from, &fromlen )) == -1 ) {
  147.             if ( debug ) perror( "accept" );
  148.             exit( 1 );
  149.         }
  150.  
  151.         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
  152.             sizeof(from.sin_addr.s_addr), AF_INET );
  153.         syslog( LOG_INFO, "TCP connection from %s (%s)",
  154.             (hp == NULL) ? "unknown" : hp->h_name,
  155.             inet_ntoa( from.sin_addr ) );
  156.  
  157.         switch( pid = fork() ) {
  158.         case 0:        /* child */
  159.             close( s );
  160.             do_queries( ns );
  161.             break;
  162.  
  163.         case -1:    /* failed */
  164.             perror( "fork" );
  165.             break;
  166.  
  167.         default:    /* parent */
  168.             close( ns );
  169.             if ( debug )
  170.                 fprintf( stderr, "forked child %d\n", pid );
  171.             break;
  172.         }
  173.     }
  174.     /* NOT REACHED */
  175. }
  176.  
  177. static set_socket( port )
  178. int    port;
  179. {
  180.     int            s;
  181.     struct sockaddr_in    addr;
  182.  
  183.     if ( port == -1 )
  184.         port = DEFAULT_GO500PORT;
  185.     myport = port;
  186.  
  187.     if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
  188.                 perror( "socket" );
  189.                 exit( 1 );
  190.         }
  191.  
  192.         /* set option so clients can't keep us from coming back up */
  193.         if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, 0, 0 ) < 0 ) {
  194.                 perror( "setsockopt" );
  195.                 exit( 1 );
  196.         }
  197.  
  198.         /* bind to a name */
  199.         addr.sin_family = AF_INET;
  200.         addr.sin_addr.s_addr = INADDR_ANY;
  201.         addr.sin_port = htons( port );
  202.         if ( bind( s, &addr, sizeof(addr) ) ) {
  203.                 perror( "bind" );
  204.                 exit( 1 );
  205.         }
  206.  
  207.     /* listen for connections */
  208.         if ( listen( s, 5 ) == -1 ) {
  209.                 perror( "listen" );
  210.                 exit( 1 );
  211.         }
  212.  
  213.         if ( debug ) printf("tcp socket allocated, bound, and listening\n");
  214.  
  215.     return( s );
  216. }
  217.  
  218. static wait4child()
  219. {
  220. #if defined(SunOS) && SunOS < 40
  221.         union wait status;
  222. #else
  223.         int     status;
  224. #endif
  225.  
  226.         if ( debug ) printf( "parent: catching child status\n" );
  227.         while ( wait3( &status, WNOHANG | WUNTRACED, 0 ) > 0 )
  228.                 ;       /* NULL */
  229. }
  230.  
  231. static char    dixie_buffer[MAX_PKT_SIZE];
  232.  
  233. static do_queries( s )
  234. int    s;
  235. {
  236.     char        buf[256];
  237.     int        len;
  238.     FILE        *fp;
  239.     int        rc, tblsize;
  240.     struct timeval    timeout;
  241.     fd_set        readfds;
  242.  
  243.     if ( dxi_init( dixiehost, 0, 0 ) == -1 ) {
  244.         if ( debug ) perror( "dxi_init" );
  245.         exit( 1 );
  246.     }
  247.     if ( !searchaliases )
  248.         dxi_options |= DXI_OPT_DONTSEARCHALIASES;
  249.  
  250.     if ( dxi_bind( DAPUSER, NULL ) == -1 ) {
  251.         if ( debug ) dxi_perror( "dxi_bind" );
  252.         exit( 1 );
  253.     }
  254.     dxi_not61 = 1;
  255.  
  256.     if ( (fp = fdopen( s, "a+")) == NULL ) {
  257.         perror( "fdopen" );
  258.         exit( 1 );
  259.     }
  260.  
  261.     tblsize = getdtablesize();
  262.     timeout.tv_sec = 300;
  263.     timeout.tv_usec = 0;
  264.     FD_ZERO( &readfds );
  265.     FD_SET( fileno( fp ), &readfds );
  266.  
  267.     if ( (rc = select( tblsize, &readfds, 0, 0, &timeout )) <= 0 )
  268.         exit( 1 );
  269.  
  270.     if ( fgets( buf, sizeof(buf), fp ) == NULL )
  271.         exit( 1 );
  272.  
  273.     len = strlen( buf );
  274.     if ( debug ) {
  275.         fprintf( stderr, "got %d bytes\n", len );
  276.         dxi_bprint( buf, len );
  277.     }
  278.  
  279.     /* strip of \r \n */
  280.     if ( buf[len - 1] == '\n' )
  281.         buf[len - 1] = '\0';
  282.     len--;
  283.     if ( buf[len - 1] == '\r' )
  284.         buf[len - 1] = '\0';
  285.     len--;
  286.  
  287.     rewind(fp);
  288.     if ( strchr( buf, '=' ) )
  289.         do_read( fp, buf );
  290.     else
  291.         do_search( fp, buf );
  292.     fprintf( fp, ".\r\n" );
  293.     rewind(fp);
  294.  
  295.     exit( 1 );
  296.     /* NOT REACHED */
  297. }
  298.  
  299. static do_search( fp, buf )
  300. FILE    *fp;
  301. char    *buf;
  302. {
  303.     char    filter[1024];
  304.     char    *dn, *e, *rdn;
  305.     char    **title;
  306.     static char    *attrs[] = { "title", 0 };
  307.  
  308.     sprintf( filter, "uid=%s|sn=%s|cn=%s", buf, buf, buf );
  309.  
  310.     if ( dxi_search( base, DXI_SRCH_SUBTREE, filter, attrs, dixie_buffer )
  311.         == -1 ) {
  312.         dxi_perror( "dxi_search" );
  313.         exit( 1 );
  314.     }
  315.  
  316.     for ( e = dxi_firstentry( dixie_buffer ); e != NULL;
  317.         e = dxi_nextentry() ) {
  318.         dn = dxi_getdn( e );
  319.         rdn = dxi_lastdncomp( dn );
  320.         title = dxi_getvalues( e, "title" );
  321.  
  322.         fprintf( fp, "0%-20s    %s\t%s\t%s\t%d\r\n", rdn,
  323.             title ? title[0] : "", dn, myhost, myport );
  324.  
  325.         dxi_valuefree( title );
  326.         free( dn );
  327.     }
  328. }
  329.  
  330. static print_attr( fp, label, attr, e, multiline )
  331. FILE    *fp;
  332. char    *label;
  333. char    *attr;
  334. char    *e;
  335. int    multiline;
  336. {
  337.     char    **val;
  338.     int    i, gotone = 0, firstline = 1;
  339.  
  340.     if ( (val = dxi_getvalues( e, attr )) == NULL )
  341.         return;
  342.  
  343.     fprintf( fp, "%-19s\r\n", label );
  344.     for ( i = 0; val[i] != NULL; i++ ) {
  345.         if ( multiline ) {
  346.             char    *s, *p;
  347.  
  348.             if ( gotone )
  349.                 fprintf( fp, "%-19s\r\n", label );
  350.             p = s = val[i];
  351.             while ( s = strchr( s, '$' ) ) {
  352.                 *s++ = '\0';
  353.                 while ( isspace( *s ) )
  354.                     s++;
  355.                 if ( firstline ) {
  356.                     fprintf( fp, "                    %s\r\n", p );
  357.                     firstline = 0;
  358.                 } else {
  359.                     fprintf( fp, "                    %s\r\n", p );
  360.                 }
  361.                 p = s;
  362.             }
  363.             if ( firstline ) {
  364.                 fprintf( fp, "                    %s\r\n", p );
  365.             } else {
  366.                 fprintf( fp, "                    %s\r\n", p );
  367.             }
  368.             gotone = 1;
  369.             firstline = 1;
  370.         } else {
  371.             if ( firstline ) {
  372.                 fprintf( fp, "                    %s\r\n", val[i] );
  373.                 firstline = 0;
  374.             } else {
  375.                 fprintf( fp, "                    %s\r\n", val[i] );
  376.             }
  377.         }
  378.     }
  379.     dxi_valuefree( val );
  380. }
  381.  
  382. static do_read( fp, buf )
  383. FILE    *fp;
  384. char    *buf;
  385. {
  386.     char        *dn, *e, *ufn;
  387.     int        i;
  388.     char        *s;
  389.     char        **cn, **paddr, **tnumber, **uid, **title, **mail;
  390.     static char    *attrs[] = { "cn", "postalAddress", "uid", "title",
  391.                      "mail", "telephoneNumber",
  392.                      "facsimileTelephoneNumber", 0 };
  393.  
  394.     if ( dxi_read( buf, attrs, dixie_buffer ) == -1 ) {
  395.         if ( debug ) dxi_perror( "dxi_read" );
  396.         exit( 1 );
  397.     }
  398.  
  399.     e = dxi_getreadentry( dixie_buffer );
  400.     dn = dxi_getdn( e );
  401.     ufn = dxi_dn2ufn( dn );
  402.     for ( i = 0; i < strip; i++ ) {
  403.         if ( (s = strrchr( ufn, ',' )) == NULL )
  404.             break;
  405.         *s = '\0';
  406.     }
  407.     fprintf( fp, "\"%s\"\r\n", ufn );
  408.     print_attr( fp, "  Also known as:", "cn", e, 0 );
  409.     print_attr( fp, "  E-mail address:", "mail", e, 0 );
  410.     print_attr( fp, "  Fax number:", "facsimileTelephoneNumber", e, 0 );
  411.     print_attr( fp, "  Business phone:", "telephoneNumber", e, 0 );
  412.     print_attr( fp, "  Business address:", "postalAddress", e, 1 );
  413.     print_attr( fp, "  Title:", "title", e, 0 );
  414.     print_attr( fp, "  Uniqname:", "uid", e, 0 );
  415.  
  416.     free( dn );
  417. }
  418.