home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part05 / args.c next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  9.6 KB  |  386 lines

  1. /*
  2.  * args.c -- process arguments for finger / whoj
  3.  * January 1986  -- Year of the Commet
  4.  *
  5.  * Copyright (C) 1986, 1990  Philip L. Budne
  6.  *
  7.  * This file is part of "Phil's Finger Program".
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 1, or (at your option)
  12.  * any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  */
  24.  
  25. # ifndef lint
  26. static char *rcsid = "$Id: args.c,v 3.0 90/07/06 13:10:18 budd Rel $";
  27. # endif /* lint not defined */
  28.  
  29. # include <sys/types.h>
  30. # include <strings.h>
  31. # include <stdio.h>
  32. # include <ctype.h>
  33. # include <netdb.h>            /* to get cannonical name */
  34.  
  35. # include "args.h"
  36. # include "remote.h"
  37. # include "finger.h"
  38. # include "inquire.h"
  39.  
  40. extern dowhoj(), dofinger();        /* from output.c */
  41. extern void ini_select(), addlocal();    /* from select.c */
  42. extern BOOL have_local();        /* from select.c */
  43. extern void ini_tsel(), addtty();    /* from getent.c */
  44. extern char *ttyname();            /* from libc */
  45. extern void doswitch();            /* from switch.c */
  46.  
  47. LOCAL RHOST *host_chain, *end_host_chain; /* chain of 4n hosts */
  48. GLOBAL int netfinger;            /* input from socket */
  49. GLOBAL BOOL useinquire;            /* have inquire database */
  50.  
  51. LOCAL void finger();            /* forward */
  52. LOCAL int isasocket();            /* forward */
  53. GLOBAL void add4n();            /* forward */
  54.  
  55. GLOBAL char localhost[ MAXHOSTLEN+1 ];    /* system /bin/hostname */
  56. GLOBAL char officialhostname[ MAXHOSTLEN*2 ]; /* official name from host table*/
  57. GLOBAL char OFFICIALhostname[ MAXHOSTLEN*2 ]; /* upper case copy */
  58. GLOBAL char **aliases;            /* aliases for this host */
  59.  
  60. GLOBAL int main( argc, argv )
  61. int argc;
  62. char *argv[];
  63. {
  64.     char *prog;
  65.     
  66.     netfinger = isasocket(0) && isasocket(1); /* are stdin and
  67.                            * stdout sockets (pipes)? */
  68.  
  69.     if( !netfinger && getuid() == 0 ) {    /* if superuser */
  70.     nice( -120 );            /* run fast */
  71.     nice( 20 - 10 );        /* (absolute -10) */
  72.     } /* superuser */
  73.  
  74.     if( gethostname( localhost, MAXHOSTLEN ) == 0 ) {
  75.     struct hostent *h;
  76.     if( (h = gethostbyname( localhost )) != NULL ) {
  77.         int c;
  78.         char **a, **b;
  79.  
  80.         strcpy( officialhostname, h->h_name );
  81.         strupcpy( OFFICIALhostname, officialhostname );
  82.  
  83.         c = 0;
  84.         a = h->h_aliases;
  85.         while( *a++ != NULL )    /* count aliases */
  86.         c++;
  87.  
  88.         a = h->h_aliases;
  89.         aliases = b = (char **) calloc( c + 1, sizeof( char * ) );
  90.         while( (*b++ = savestr( *a++ )) != NULL )
  91.         ;
  92.     } /* got host entry */
  93.     } /* gethostname */
  94.  
  95.     getterm();                /* get terminal stuff */
  96.  
  97.     host_chain = end_host_chain = NULL;    /* clear 4n host chain */
  98.  
  99.     if( (prog = rindex(argv[0], '/')) != NULL ) /* isolate last part of path */
  100.     prog++;                /* skip slash */
  101.     else
  102.     prog = argv[0];
  103.  
  104.     init_tsel();            /* clear tty selectors */
  105.  
  106.     finger(prog, argc, argv);
  107.     return( 0 );            /* ANSI conforming */
  108. } /* main */
  109.  
  110. LOCAL int isasocket( s )
  111. int s;
  112. {
  113.     int l;
  114.     char sa[ 200 ];            /* bogus sockaddr struct */
  115.  
  116.     l = sizeof( sa );            /* pass length by reference */
  117.     if( getpeername( s, sa, &l ) < 0 )
  118.     return( FALSE );
  119.     else
  120.     return( TRUE );
  121. } /* isasocket */
  122.  
  123. LOCAL void unswitch( str )
  124.     char *str;
  125. {
  126.     register char *cp;
  127.     if( (cp = index(str, '/')) != NULL ) {
  128.        *cp++ = EOS;
  129.        doswitch( cp );
  130.    } /* found a slash */
  131. } /* unswitch */
  132.  
  133.  
  134. LOCAL void adduser( user )
  135. char *user;
  136. {
  137.     char *tp;                /* char pointer */
  138.     struct switches SavedSw;
  139.  
  140.     for( ; user != NULL; user = rindex(user, ',') ) {
  141.  
  142.     if( !skipwhite( &user ) )        /* remove leading white */
  143.         continue;
  144.  
  145.     tp = user;            /* remove trailing white */
  146.     if( skipblack( &tp ) )        /* skip token. find EOS? */
  147.         *tp = EOS;            /* found white -- nuke it! */
  148.  
  149.     tp = rindex(user, '@');        /* any ATs? (reverse -- for routing) */
  150.     if( tp != NULL ) {        /* oh goody! */
  151.         *tp++ = EOS;        /* blast at */
  152.         add4n( user, tp );        /* add to list of aliens */
  153.         continue;
  154.     }
  155.  
  156.     SavedSw = Sw;            /* save flags */
  157.     unswitch( user );        /* remove and process switches */
  158.     addlocal( user );        /* process local user */
  159.     Sw = SavedSw;            /* per user switches */
  160.     } /* for user */
  161. } /* adduser */
  162.  
  163. LOCAL void finger(prog, argc, argv)
  164. char *prog;
  165. int argc;
  166. char *argv[];
  167. {
  168.     static char mytty[ 20 ];
  169.     register char *cp, *tp;
  170.  
  171.     if( strcmp(prog, "whoj") == 0 )
  172.     sw_jobs = TRUE;
  173.  
  174.     ini_select();            /* init selector tree etc.. */
  175.  
  176. # ifdef INQUIRE
  177.     useinquire = TRUE;
  178.     if( !Mapin( NULL ) ) {        /* map in inquire database */
  179.     useinquire = FALSE;
  180.     fprintf( stderr, "%s\n", inq_error );
  181.     } /* Mapin failed */
  182. # endif /* INQUIRE defined */
  183.  
  184.     tp = ttyname( 0 );            /* get device */
  185.     if( tp != NULL ) {
  186.     if( strncmp( tp, "/dev/", 5 ) == 0 )
  187.         tp += 5;
  188.     strcpy( mytty, tp );
  189.     }
  190.     else
  191.     mytty[0] = EOS;
  192.  
  193. # ifdef NETBERK
  194.     if( netfinger )            /* from network? */
  195.     sw_berkeley = TRUE;        /* be verbose if asked by name */
  196.                     /* I hate it, but it helps aliens */
  197.                     /* with lousy finger programs */
  198. # endif /* NETBERK defined */
  199.  
  200.     if( strcmp(prog, "whois") == 0 )    /* invoked as whois? */
  201.         sw_whois = TRUE;        /* yes, set flag now */
  202.     else if( strcmp(prog, "xf") == 0 )    /* xf? */
  203.     sw_nosave = TRUE;        /* be nice (don't write nm file) */
  204.  
  205.     argv++;
  206.     argc--;
  207.     while( argc > 0 ) {            /* while more args */
  208.     struct switches SavedSw;
  209.  
  210.     switch( argv[0][0] ) {
  211.     case '-':
  212.     case '/':            /* switch */
  213.         doswitch( &argv[0][1] );
  214.         break;
  215.  
  216.     case '+':            /* tty */
  217.         SavedSw = Sw;
  218.         unswitch( &argv[0][1] );
  219.         addtty( &argv[0][1] );
  220.         Sw = SavedSw;        /* per user switches */
  221.         break;
  222.  
  223.     case '.':            /* just dot */
  224.         SavedSw = Sw;
  225.         unswitch( &argv[0][1] );
  226.         if( argv[0][1] != EOS )
  227.         fprintf( stderr, "%%Garbage after DOT: %s\n", &argv[0][1] );
  228.  
  229.         if( mytty[0] != EOS )
  230.         addtty( mytty );    /* do us. */
  231.         else
  232.         fprintf( stderr, "%%Could not get own terminal for '.'\n" );
  233.         Sw = SavedSw;        /* per user switches!! */
  234.         break;
  235.  
  236.     case '\\':            /* quoting against local switch */
  237.                     /* expansion? */
  238.         adduser( argv[0]+1 );
  239.         break;
  240.  
  241.     default:            /* must be a user */
  242.         adduser( argv[0] );
  243.         break;
  244.     } /* switch */
  245.     argv++;
  246.     argc--;
  247.     } /* while argc */
  248.  
  249.     read_conf();            /* read finger.conf */
  250.                     /* after switches!! */
  251.  
  252.     if( sw_jobs ) {
  253.     dowhoj();
  254.     return;
  255.     }
  256.  
  257.     if( !have_locals() && host_chain == NULL ) /* no hard work */
  258.     dofinger( NULL );        /* show logged in users */
  259.  
  260.     if( have_locals() ) {        /* /follow may have added locals! */
  261.     struct switches SavedSw;
  262.     sw_t SavedWhois;
  263.  
  264.     SavedSw = Sw;        /* save flags */
  265.     SavedWhois = sw_whois;
  266.     if( sw_berkeley ) {        /* in berkley emulation mode? */
  267.         sw_whois = TRUE;        /* yes, be verbose */
  268.         Sw.sw_plan = !Sw.sw_noplan;
  269.     }
  270.  
  271. # ifdef NETFOLLOW
  272.     if( netfinger )            /* only follow if network AND people */
  273.         sw_follow = TRUE;
  274. # endif /* NETFOLLOW defined */
  275.  
  276.     select_go();            /* run locals */
  277.  
  278.     Sw = SavedSw;            /* restore flags */
  279.     sw_whois = SavedWhois;        /* before doing remote!! */
  280.     } /* else */
  281.  
  282.     if( host_chain != NULL )
  283.     doremote( host_chain );
  284.  
  285. } /* finger */
  286.  
  287. LOCAL RHOST *mkrhost( name )
  288. char *name;
  289. {
  290.     register RHOST *rh;
  291.     if( (rh = (RHOST *)malloc( sizeof( RHOST ) )) == NULL ) {
  292.     perror("mkrhost malloc failed");
  293.     exit( 1 );
  294.     } /* malloc failed */
  295.  
  296.     rh->rh_name = name;
  297.     rh->rh_next = NULL;
  298.     rh->rh_user = rh->rh_endu = NULL;
  299.  
  300.     return( rh );
  301. } /* mkrhost */
  302.  
  303. LOCAL RUSER *mkruser( name )
  304. char *name;
  305. {
  306.     register RUSER *ru;
  307.     if( (ru = (RUSER *)malloc( sizeof( RUSER ) )) == NULL ) {
  308.     perror("mkruser malloc failed");
  309.     exit( 1 );
  310.     } /* malloc failed */
  311.  
  312.     ru->ru_name = name;
  313.     ru->ru_next = NULL;
  314.     return( ru );
  315. } /* mkruser */
  316.  
  317. GLOBAL void add4n( user, host )
  318. char *user, *host;
  319. {
  320.     if( *host == EOS ) {
  321.     fprintf(stderr, "?Empty hostname\n");
  322.     }
  323.     else {                /* has host */
  324.     register RHOST *hp;        /* host pointer */
  325.  
  326.     for( hp = host_chain; hp != NULL; hp = hp->rh_next )
  327.         if( strcmp(hp->rh_name, host) == 0 )
  328.             break;
  329.  
  330.     if( hp == NULL ) {        /* search failed */
  331.         if( host_chain == NULL )    /* empty chain? */
  332.             host_chain = end_host_chain = hp = mkrhost( host ); /* make1 */
  333.         else {            /* add new host */
  334.             hp = mkrhost( host );    /* create node */
  335.         end_host_chain->rh_next = hp; /* add after last */
  336.         end_host_chain = hp;    /* point end to me */
  337.         } /* add new host */
  338.  
  339.         hp->rh_user = hp->rh_endu = mkruser( user ); /* create usr chain */
  340.     } /* search failed */
  341.     else {                /* search won */
  342.         register RUSER *up;
  343.  
  344.         up = mkruser( user );    /* create user node */
  345.         hp->rh_endu->ru_next = up;    /* add after last for this host */
  346.         hp->rh_endu = up;        /* point end of list to us */
  347.     } /* add to existing chain (search won) */
  348.     } /* non-empty host */
  349.  
  350. # ifdef DEBUG
  351.     if( *user == EOS )
  352.         printf(" @%s\n", host);
  353.     else
  354.         printf("%s@%s\n", user, host);
  355. # endif /* DEBUG defined */
  356. } /* add 4n */
  357.  
  358. GLOBAL BOOL islocalhost( s )
  359.     char *s;
  360. {
  361.     struct hostent *h;
  362.     char **a;
  363.  
  364.     if( strcmp( s, officialhostname ) == 0 || strcmp( s, localhost ) == 0 )
  365.     return( TRUE );
  366.  
  367.     a = aliases;
  368.     while( *a != NULL )
  369.     if( strcmp( s, *a++ ) == 0 )
  370.         return( TRUE );
  371.  
  372.     if( (h = gethostbyname( s )) == NULL )
  373.     return( FALSE );
  374.  
  375.     if( strcmp( h->h_name, officialhostname ) == 0 )
  376.     return( TRUE );
  377.  
  378.     return( FALSE );
  379. } /* islocalhost */
  380.  
  381. /*
  382.  * Local variables:
  383.  * comment-column: 40
  384.  * End:
  385.  */
  386.