home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elm.lzh / ELM / UTILS / NEWALIAS.C < prev    next >
C/C++ Source or Header  |  1991-01-11  |  16KB  |  643 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: newalias.c,v 4.1.1.2 90/08/02 21:57:58 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.2 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    newalias.c,v $
  17.  * Revision 4.1.1.2  90/08/02  21:57:58  syd
  18.  * The newly introduced function 'stricmp' has a name conflict with a libc
  19.  * function under SunOS 4.1.  Changed name to istrcmp.
  20.  * From: scs@lokkur.dexter.mi.us (Steve Simmons)
  21.  * 
  22.  * Revision 4.1.1.1  90/06/05  21:11:20  syd
  23.  * alias command in ELM2.3 fails because of the wrong sized aliases.hash
  24.  * newalias did not truncate existing file (aliases.hash)
  25.  * From: Toshinori Maeno <tmaeno@cc.titech.ac.jp>
  26.  * 
  27.  * Revision 4.1  90/04/28  22:44:46  syd
  28.  * checkin of Elm 2.3 as of Release PL0
  29.  * 
  30.  *
  31.  ******************************************************************************/
  32.  
  33. /** Install a new set of aliases for the 'Elm' mailer. 
  34.  
  35.     If invoked with a specific filename, it assumes that
  36.   it is working with an individual users alias tables, and
  37.   generates the .alias.hash and .alias.data files in their
  38.   home directory.
  39.     If, however, it is invoked with no arguments, then
  40.   it assumes that the user is updating the system alias
  41.   file and uses the defaults for everything.
  42.  
  43.   The format for the input file is;
  44.     alias1, alias2, ... = username = address
  45. or  alias1, alias2, ... = groupname= member, member, member, ...
  46.                                      member, member, member, ...
  47.  
  48. **/
  49.  
  50. #include <stdio.h>
  51. #include "defs.h"
  52. #include "sysdefs.h"        /* ELM system definitions */
  53. #include <ctype.h>
  54.  
  55. #ifdef BSD
  56. #  include <sys/file.h>
  57. #  undef tolower
  58. #  undef toupper
  59. #else
  60. #  include <fcntl.h>
  61. #endif
  62.  
  63. static char ident[] = { WHAT_STRING };
  64.  
  65. #define group(string)        (strpbrk(string,", ") != NULL)
  66.  
  67. struct alias_rec
  68. shash_table[MAX_SALIASES];    /* the actual hash table     */
  69.  
  70. struct alias_rec
  71. uhash_table[MAX_UALIASES];    /* the actual hash table     */
  72.  
  73. int  hash_table_loaded=0;    /* is system table actually loaded? */
  74.  
  75. int  buff_loaded;        /* for file input overlap... */
  76. int  error= 0;            /* if errors, don't save!    */
  77. int  is_system=0;        /* system file updating?     */
  78. int  count=0;            /* how many aliases so far?  */
  79. long offset = 0L;        /* data file line offset!    */
  80. char home[SLEN];        /* the users home directory  */
  81.  
  82. main(argc, argv)
  83. int argc;
  84. char *argv[];
  85. {
  86.     FILE *in, *data;
  87.     char inputname[SLEN], hashname[SLEN], dataname[SLEN];
  88.     char buffer[LONG_STRING];
  89.     int  a, hash, count = 0, owner;
  90.  
  91.     for (a = 1; a < argc; ++a) {
  92.       if (strcmp(argv[a], "-g") == 0)
  93.         is_system = 1;
  94.       else {
  95.         printf("Usage: %s [-g]\n", argv[0]);
  96.         exit(1);
  97.       }
  98.     }
  99.  
  100.     if (is_system) {   /* update system aliases */
  101.       printf("Updating the system alias file...\n");
  102.  
  103.       strcpy(inputname, system_text_file);
  104.       strcpy(hashname,  system_hash_file);
  105.       strcpy(dataname,  system_data_file);
  106.       init_table(shash_table, MAX_SALIASES);
  107.     }
  108.     else
  109.       printf("Updating your personal alias file...\n");
  110.     
  111.     if (! is_system) {
  112.       if (strcpy(home, getenv("HOME")) == NULL) {
  113.         printf("I'm confused - no HOME variable in environment!\n");
  114.         exit(1);
  115.       }
  116.  
  117.       sprintf(inputname, "%s/%s", home, ALIAS_TEXT);
  118.       sprintf(hashname,  "%s/%s", home, ALIAS_HASH); 
  119.       sprintf(dataname,  "%s/%s", home, ALIAS_DATA); 
  120.  
  121.       init_table(uhash_table, MAX_UALIASES); 
  122.  
  123.       read_in_system(shash_table, sizeof shash_table);
  124.     }
  125.  
  126.     if ((in = fopen(inputname,"r")) == NULL) {
  127.       /** let's see if they have the files in the old place... **/
  128.       sprintf(buffer, "%s/.alias_text", home);
  129.       if (access(buffer, ACCESS_EXISTS) != -1) {
  130.         update_alias_file_locations();
  131.         in = fopen(inputname, "r");
  132.       }
  133.       else {
  134.         printf("Couldn't open %s for input!\n", inputname);
  135.         exit(1);
  136.       }
  137.     }
  138.  
  139.     if ((hash = open(hashname, O_WRONLY)) == -1)
  140.     {
  141.         if ((hash = creat(hashname, O_WRONLY)) == -1)        
  142.           {
  143.             printf("Couldn't open %s for output!\n", hashname);
  144.               exit(1);
  145.           }
  146.     }
  147.  
  148.     if ((data = fopen(dataname,"w")) == NULL) {
  149.       printf("Couldn't open %s for output!\n", dataname);
  150.       exit(1);
  151.     }
  152.  
  153.     buff_loaded = 0;     /* file buffer empty right now! */
  154.  
  155.     while (get_alias(in, buffer) != -1) {
  156.       if (is_system)
  157.         put_alias(data, buffer, shash_table, MAX_SALIASES);    
  158.       else
  159.         put_alias(data, buffer, uhash_table, MAX_UALIASES);    
  160.       count++;
  161.     }
  162.  
  163.     if (error) {
  164.       printf("\n** Not saving tables!  Please fix and re-run %s!\n",
  165.          argv[0]);
  166.       exit(1);
  167.     }
  168.     else {
  169.       if (is_system)
  170.         write(hash, shash_table, sizeof shash_table);
  171.       else
  172.         write(hash, uhash_table, sizeof uhash_table);
  173.  
  174.       close(hash);
  175.       fclose(data);
  176.       fclose(in);
  177.     
  178.       printf("Processed %d aliases\n", count);
  179.       exit(0);
  180.     }
  181. }
  182.  
  183. int
  184. get_alias(file, buffer)
  185. FILE *file;
  186. char *buffer;
  187. {
  188.     /* load buffer with the next complete alias from the file.
  189.        (this can include reading in multiple lines and appending
  190.        them all together!)  Returns EOF after last entry in file.
  191.     
  192.     Lines that start with '#' are assumed to be comments and are
  193.      ignored.  White space as the first field of a line is taken
  194.     to indicate that this line is a continuation of the previous. */
  195.  
  196.     static char mybuffer[SLEN];
  197.     int    done = 0, first_read = 1;
  198.  
  199.     /** get the first line of the entry... **/
  200.  
  201.     buffer[0] = '\0';            /* zero out line */
  202.  
  203.     do {
  204.       if (get_line(file, mybuffer, first_read) == -1) 
  205.         return(-1);
  206.       first_read = 0;
  207.       if (mybuffer[0] != '#')
  208.         strcpy(buffer, mybuffer);
  209.     } while (strlen(buffer) == 0);    
  210.  
  211.     /** now read in the rest (if there is any!) **/
  212.  
  213.     do {
  214.       if (get_line(file, mybuffer, first_read) == -1) {
  215.         buff_loaded = 0;    /* force a read next pass! */
  216.         return(0);    /* okay. let's just hand 'buffer' back! */
  217.       }
  218.       done = (! whitespace(mybuffer[0]));
  219.       if (! done)
  220.         strcat(buffer, mybuffer);
  221.       done = (done && mybuffer[0] != '#');
  222.     } while (! done);
  223.     
  224.     return(0);    /* no sweat! */
  225. }
  226.  
  227. put_alias(data, buffer, table, size)
  228. FILE *data;
  229. char *buffer;
  230. struct alias_rec table[];
  231. int  size;
  232. {
  233.     /** break buffer down into three pieces: aliases, comment, and address.
  234.         Make the appropriate entries in the table (size) 
  235.     **/
  236.  
  237.     char aliases[LONG_STRING], address[LONG_STRING];
  238.     char comment[LONG_STRING], c;
  239.     int  first, last, i = 0, j = 0;
  240.  
  241.     remove_all(' ', TAB, buffer);
  242.  
  243.     for (i=0; buffer[i] != '=' && i < LONG_STRING; i++)
  244.       aliases[i] = buffer[i];
  245.     aliases[i] = '\0';
  246.  
  247.     for (i=strlen(buffer)-1; buffer[i] != '=' && i > 0; i--)
  248.       address[j++] = buffer[i];
  249.     address[j] = '\0';
  250.  
  251.     comment[0] = '\0';    /* default to nothing at all... */
  252.  
  253.     if ((first=strlen(aliases)+1) < (last=(strlen(buffer) - j))) {
  254.       extract_comment(comment, buffer, first, last); 
  255.     }
  256.  
  257.     reverse(address);
  258.  
  259.     add_to_table(data, aliases, comment, address, table, size);
  260. }
  261.  
  262. int
  263. get_line(file, buffer, first_line)
  264. FILE *file;
  265. char *buffer;
  266. int  first_line;
  267. {
  268.     /** read line from file.  If first_line and buff_loaded, 
  269.         then just return! **/
  270.  
  271.     int stat, len;
  272.  
  273.     if (first_line && buff_loaded) {
  274.       buff_loaded = 1;
  275.       return(0);
  276.     }
  277.  
  278.     buff_loaded = 1;    /* we're going to get SOMETHING in the buffer */
  279.  
  280.     stat = fgets(buffer, SLEN, file) == NULL ? -1 : 0;
  281.  
  282.     if (stat != -1) {
  283.       len = strlen(buffer);
  284.       if (len > 0) {
  285.         if (buffer[len - 1] != '\n') {
  286.           printf("Line too long, split using continuation line format (starting line\nwith whitespace):\n%s\n\n", buffer);
  287.           exit(1);
  288.         }
  289.       }
  290.       no_ret(buffer);
  291.     }
  292.  
  293.     return(stat);
  294. }
  295.  
  296. reverse(string)
  297. char *string;
  298. {
  299.     /** reverse the order of the characters in string... 
  300.         uses a bubble-sort type of algorithm!                 **/
  301.     
  302.     register int f, l;
  303.     char     c;
  304.     
  305.     f = 0;
  306.     l = strlen(string) - 1;
  307.     
  308.     while (f < l) {
  309.       c = string[f];
  310.        string[f] = string[l];
  311.       string[l] = c;
  312.       f++;
  313.       l--;
  314.     }
  315. }
  316.  
  317. add_to_table(data, aliases, comment, address, table, size)
  318. FILE *data;
  319. char *aliases, *comment, *address;
  320. struct alias_rec table[];
  321. int  size;
  322. {
  323.     /** add address + comment to datafile, incrementing offset count 
  324.         (bytes), then for each alias in the aliases string, add to the
  325.         hash table, with the associated pointer value! **/
  326.  
  327.     static char buf[SLEN], *word, *s;
  328.     long additive = 1L;
  329.  
  330.     word = buf;    /* use the allocated space! */
  331.  
  332.     for ( s = aliases ; *s != '\0' && (ok_alias_char(*s)||*s==',') ; ++s ) ;
  333.     if ( *s != '\0' ) {
  334.       printf("Error - character '%c' in alias '%s' is not supported.\n",
  335.         *s, aliases);
  336.       error++;
  337.       return;
  338.     }
  339.  
  340.     if (group(address)) {
  341.       check_group(address, aliases);
  342.       if (error) return;    /* don't do work if we aren't to save it! */
  343.       fprintf(data, "!%s\n", address);
  344.       additive = 2L;
  345.     }
  346.     else {
  347.       if (error) return;    /* don't do work if we aren't to save it! */
  348.       if (strlen(comment) > 0) {
  349.         fprintf(data, "%s (%s)\n", address, comment);
  350.         additive = (long) (strlen(comment) + 4);
  351.       }
  352.       else
  353.         fprintf(data, "%s\n", address, comment);
  354.     }
  355.  
  356.     while ((word = (char *) strtok(aliases,", ")) != NULL) {
  357.       add_to_hash_table(word, offset, table, size);
  358.       aliases = NULL;    /* let's get ALL entries via 'strtok' */
  359.       count++;
  360.     }
  361.  
  362.     if ( is_system ? count > MAX_SALIASES-35 : count > MAX_UALIASES-21) {
  363.       printf("** Too many aliases in file! **\n");
  364.       error++;
  365.     }
  366.  
  367.     offset = (offset + (long) strlen(address) + additive);
  368. }    
  369.  
  370. remove_all(c1, c2, string)
  371. char c1, c2, *string;
  372. {
  373.     /* Remove all occurances of character 'c1' or 'c2' from the string.
  374.        Hacked (literally) to NOT remove ANY characters from within the
  375.        equals fields.  This will only be used if the line contains TWO
  376.        equalss (and comments with equalss in them are the kiss of death!)
  377.      */
  378.  
  379.     char buffer[LONG_STRING];
  380.     register int i = 0, j = 0, first_equals = -1, last_equals = -1;
  381.     
  382.     for (i = 0; string[i] != '\0' && i < LONG_STRING; i++) {
  383.       if (string[i] != c1 && string[i] != c2)
  384.         buffer[j++] = string[i];
  385.  
  386.       if (first_equals == -1 && string[i] == '=') {
  387.         first_equals = i;
  388.         for (last_equals=strlen(string);string[last_equals] != '='; 
  389.         last_equals--) ;
  390.       }
  391.       else if (i > first_equals && i < last_equals)
  392.        if (string[i] == c1 || string[i] == c2)
  393.          buffer[j++] = string[i];
  394.     }
  395.     
  396.     buffer[j] = '\0';
  397.     strcpy(string, buffer);
  398. }
  399.  
  400. add_to_hash_table(word, offset, table, size)
  401. char *word;
  402. long  offset;
  403. struct alias_rec table[];
  404. int   size;
  405. {
  406.     /** add word and offset to current hash table. **/
  407.     register int loc;
  408.     
  409.     if (strlen(word) > 20) {
  410.       printf("Bad alias name: %s.  Too long.\n", word);
  411.       exit(1);
  412.     }
  413.  
  414.     loc = hash_it(word, size);
  415.  
  416.     while (table[loc].name[0] != '\0' && istrcmp(table[loc].name,word) != 0)
  417.       loc = (loc + 1) % size; 
  418.  
  419.     if (table[loc].name[0] == '\0') {
  420.       strcpy(table[loc].name, word);
  421.       table[loc].byte = htonl(offset);
  422.     }
  423.     else 
  424.       printf("** Duplicate alias '%s' in file.  Multiples ignored.\n",
  425.              word);
  426. }
  427.  
  428. int
  429. istrcmp(s1,s2)
  430. register char *s1, *s2;
  431. {
  432.     /* case insensitive comparison */
  433.     register int d;
  434.     for (;;) {
  435.       d = ( isupper(*s1) ? tolower(*s1) : *s1 )
  436.           - ( isupper(*s2) ? tolower(*s2) : *s2 ) ;
  437.       if ( d != 0 || *s1 == '\0' || *s2 == '\0' )
  438.         return d;
  439.       ++s1;
  440.       ++s2;
  441.     }
  442.     /*NOTREACHED*/
  443. }
  444.  
  445. int
  446. hash_it(string, table_size)
  447. register char *string;
  448. int   table_size;
  449. {
  450.     /** compute the hash function of the string, returning
  451.         it (mod table_size) **/
  452.  
  453.     register int sum = 0;
  454.     for ( ; *string != '\0' ; ++string )
  455.       sum += (int) ( isupper(*string) ? tolower(*string) : *string );
  456.     return(sum % table_size);
  457. }
  458.  
  459.  
  460. init_table(table, size)
  461. struct alias_rec table[];
  462. int size;
  463. {
  464.     /** initialize hash table! **/
  465.  
  466.     register int i;
  467.  
  468.     for (i=0; i < size; i++)
  469.       table[i].name[0] = '\0';
  470. }
  471.  
  472. read_in_system(table, size)
  473. struct alias_rec table[];
  474. int size;
  475. {
  476.     /** read in the system hash table...to check for group aliases
  477.         from the user alias file (to ensure that there are no names
  478.         in the user group files that are not purely contained within
  479.         either alias table) **/
  480.     
  481.     int  fd;
  482.     char fname[SLEN];
  483.  
  484.     sprintf(fname, "%s/%s", mailhome, ALIAS_HASH);
  485.  
  486.     if ((fd = open(fname, O_RDONLY)) == -1)
  487.       return;    /* no sweat: flag 'hash_table_loaded' not set! */
  488.  
  489.     (void) read(fd, table, size);
  490.     close(fd);
  491.     hash_table_loaded++;
  492. }
  493.     
  494. check_group(names, groupname)
  495. char *names, *groupname;
  496. {
  497.     /** one by one make sure each name in the group is defined
  498.         in either the system alias file or the user alias file.
  499.         This search is linearly dependent, so all group aliases
  500.         in the source file should appear LAST, after all the user
  501.         aliases! **/
  502.  
  503.     char *word, *bufptr, buffer[LONG_STRING];
  504.     int aliased;
  505.  
  506.     strcpy(buffer, names);
  507.     bufptr = (char *) buffer;
  508.     names[0] = '\0';
  509.  
  510.     while ((word = (char *) strtok(bufptr,", ")) != NULL) {
  511.       if (! (aliased = can_find(word))) 
  512.         if (! valid_name(word)) {
  513.           error++;
  514.           printf("** Alias %s in group %s is bad!\n", word, groupname);
  515.         }
  516.       bufptr = NULL;
  517.       if (names[0])
  518.         strcat(names, ", ");
  519.       strcat(names, word);
  520.     }
  521. }
  522.  
  523. int
  524. can_find(name)
  525. char *name;
  526. {    
  527.     /** find name in either hash table...use 'is_system' variable to
  528.         determine if we should look in both or just system....    **/
  529.  
  530.     register int loc;
  531.     
  532.     if (strlen(name) > 20) {
  533.       error++;
  534.       printf("** Bad alias name: %s.  Too long.\n", name);
  535.       return(1);    /* fake out: don't want 2 error messages! */
  536.     }
  537.  
  538.     /** system alias table... **/
  539.     if (hash_table_loaded || is_system) {
  540.       loc = hash_it(name, MAX_SALIASES);
  541.  
  542.       while (istrcmp(name, shash_table[loc].name) != 0 && 
  543.                  shash_table[loc].name[0] != '\0')
  544.         loc = (loc + 1) % MAX_SALIASES; 
  545.   
  546.       if (istrcmp(name, shash_table[loc].name) == 0)
  547.         return(1);    /* found it! */
  548.     }
  549.  
  550.     if (! is_system) {    /* okay! Let's check the user alias file! */
  551.       loc = hash_it(name, MAX_UALIASES);
  552.  
  553.       while (istrcmp(name, uhash_table[loc].name) != 0 && 
  554.                  uhash_table[loc].name[0] != '\0')
  555.         loc = (loc + 1) % MAX_UALIASES; 
  556.  
  557.       if (istrcmp(name, uhash_table[loc].name) == 0)
  558.         return(1);    /* found it! */
  559.     }
  560.  
  561.     return(0);
  562. }
  563.  
  564. extract_comment(comment, buffer, first, last)
  565. char *comment, *buffer;
  566. int first, last;
  567. {
  568.     /** Buffer contains a comment, located between the first and last
  569.         values.  Copy that into 'comment', but remove leading and
  570.         trailing white space.  Note also that it doesn't copy past
  571.         a comma, so `unpublishable' comments can be of the form;
  572.         dave: Dave Taylor, HP Labs : taylor@hplabs
  573.         and the output will be "taylor@hplabs (Dave Taylor)".
  574.     **/
  575.  
  576.     register int loc = 0; 
  577.  
  578.     /** first off, skip the LEADING white space... **/
  579.  
  580.     while (whitespace(buffer[first])) first++;
  581.     
  582.     /** now let's backup the 'last' value until we hit a non-whitespace **/
  583.  
  584.     last -= 2;    /* starts at ch AFTER equals.. */
  585.     while (whitespace(buffer[last])) last--;
  586.  
  587.     /** now a final check to make sure we're still talking about a 
  588.         reasonable string (rather than a "joe :: joe@dec" type string) **/
  589.  
  590.     if (first < last) {
  591.       /* one more check - let's find the comma, if present... */
  592.       for (loc=first; loc < last; loc++)
  593.         if (buffer[loc] == ',') {
  594.           last = loc-1;
  595.           break;
  596.       }
  597.       loc = 0;
  598.       while (first <= last)
  599.         comment[loc++] = buffer[first++];
  600.       comment[loc] = '\0';
  601.     }
  602. }
  603.  
  604. update_alias_file_locations()
  605. {
  606.     /** a short-term routine to ensure that the data files are
  607.         moved into the correct directory... **/
  608.  
  609.     char source[SLEN], dest[SLEN];
  610.  
  611.     /** first let's create the directory if it ain't there... **/
  612.  
  613.     sprintf(source, "%s/.elm", home);
  614.  
  615.     /** Some systems don't have a mkdir call - how inconvienient! **/
  616. #ifdef MKDIR
  617.     (void) mkdir(source, 0700);
  618. #else
  619.     system("makdir $HOME/.elm");
  620. /*    system("chmod 700 $HOME/.elm");    */
  621. #endif /* MKDIR */
  622.  
  623.     /** now *link* the files... **/
  624.  
  625.     sprintf(source, "%s/.alias_text", home);
  626.     sprintf(dest,   "%s/%s",          home, ALIAS_TEXT);
  627.     copy(source, dest,1);
  628.     unlink(source);
  629.  
  630.     sprintf(source, "%s/.alias_hash", home);
  631.     sprintf(dest,   "%s/%s",          home, ALIAS_HASH);
  632.     copy(source, dest,1);
  633.     unlink(source);
  634.  
  635.     sprintf(source, "%s/.alias_data", home);
  636.     sprintf(dest,   "%s/%s",          home, ALIAS_DATA);
  637.     copy(source, dest,1);
  638.     unlink(source);
  639.  
  640.     printf("\n*** Moved all data files into %s/.elm directory ***\n\n",
  641.         home);
  642. }
  643.