home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / sendmail / aux / newaliases.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-19  |  5.6 KB  |  241 lines

  1. /*
  2.  * Copyright (c) 1983 Eric P. Allman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #ifndef lint
  36. char copyright[] =
  37. "@(#) Copyright (c) 1988 Regents of the University of California.\n\
  38.  All rights reserved.\n";
  39. #endif /* not lint */
  40.  
  41. #ifndef lint
  42. static char sccsid[] = "@(#)newaliases.c    5.4 (Berkeley) 6/1/90";
  43. #endif /* not lint */
  44.  
  45. # include <stdio.h>
  46. # include <ctype.h>
  47. # include "sendmail.h"
  48. # include "pathnames.h"
  49.  
  50. static    char SccsId[] = "@(#)newaliases.c    5.4    6/1/90";
  51.  
  52. typedef struct { char *dptr; int dsize; } datum;
  53. char *aliases = ALIASFILE;
  54. char dirbuf[100];
  55. char pagbuf[100];
  56. int LineNo;
  57. char *To;
  58. int ExitStat;
  59. int Errors;
  60. HDR *Header;
  61. struct mailer *Mailer[MAXMAILERS+1];
  62. int NextMailer = 0;
  63. # ifdef DEBUG
  64. int Debug;
  65. # endif DEBUG
  66.  
  67. main(argc, argv)
  68.     int argc;
  69.     char *argv[];
  70. {
  71.     int f;
  72.     char line[BUFSIZ];
  73.     register char *p;
  74.     char *p2;
  75.     char *rhs;
  76.     int naliases, bytes, longest;
  77.     datum key, content;
  78.     bool skipping;
  79.     ADDRESS al, bl;
  80.     extern char *prescan();
  81.     extern ADDRESS *parse();
  82.     bool contin;
  83.     char *cffile = _PATH_SENDMAILCF;
  84.  
  85. # ifdef DEBUG
  86.     if (argc > 1 && strcmp(argv[1], "-T") == 0)
  87.     {
  88.         Debug = 100;
  89.         argc--;
  90.         argv++;
  91.     }
  92. # endif DEBUG
  93.     if (argc > 1)
  94.         aliases = argv[1];
  95.     if (argc > 2)
  96.         cffile = argv[2];
  97.     readcf(cffile);
  98.  
  99.     (void) strcpy(dirbuf, aliases);
  100.     (void) strcat(dirbuf, ".dir");
  101.     (void) strcpy(pagbuf, aliases);
  102.     (void) strcat(pagbuf, ".pag");
  103.     f = creat(dirbuf, 0666);
  104.     if (f < 0) {
  105.         perror(dirbuf);
  106.         exit(1);
  107.     }
  108.     (void) close(f);
  109.     f = creat(pagbuf, 0666);
  110.     if (f < 0) {
  111.         perror(pagbuf);
  112.         exit(1);
  113.     }
  114.     (void) close(f);
  115.     if (dbminit(aliases) < 0)
  116.         exit(1);
  117.     if (freopen(aliases, "r", stdin) == 0) {
  118.         perror(aliases);
  119.         exit(1);
  120.     }
  121.  
  122.     /* read and interpret lines */
  123.     LineNo = 0;
  124.     naliases = 0;
  125.     bytes = 0;
  126.     longest = 0;
  127.     skipping = FALSE;
  128.     while (fgets(line, sizeof (line), stdin) != NULL)
  129.     {
  130.         LineNo++;
  131.         switch (line[0])
  132.         {
  133.           case '#':
  134.           case '\n':
  135.           case '\0':
  136.             skipping = FALSE;
  137.             continue;
  138.  
  139.           case ' ':
  140.           case '\t':
  141.             if (!skipping)
  142.                 usrerr("Non-continuation line starts with space");
  143.             skipping = TRUE;
  144.             continue;
  145.         }
  146.         skipping = FALSE;
  147.  
  148.         /* process the LHS */
  149.         for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
  150.             continue;
  151.         if (*p == '\0' || *p == '\n')
  152.         {
  153.          syntaxerr:
  154.             usrerr("missing colon");
  155.             continue;
  156.         }
  157.         *p++ = '\0';
  158.         if (parse(line, &al, 1) == NULL)
  159.         {
  160.             *--p = ':';
  161.             goto syntaxerr;
  162.         }
  163.         rhs = p;
  164.         contin = FALSE;
  165.         for (;;)
  166.         {
  167.             register char c;
  168.  
  169.             /* do parsing & compression of addresses */
  170.             c = *p;
  171.             while (c != '\0')
  172.             {
  173.                 p2 = p;
  174.                 while (*p != '\n' && *p != ',' && *p != '\0')
  175.                     p++;
  176.                 c = *p;
  177.                 *p++ = '\0';
  178.                 if (*p2 == '\0')
  179.                 {
  180.                     p[-1] = c;
  181.                     continue;
  182.                 }
  183.                 parse(p2, &bl, -1);
  184.                 contin = (c == ',');
  185.                 p[-1] = c;
  186.                 while (isspace(*p))
  187.                     p++;
  188.             }
  189.  
  190.             /* see if there should be a continuation line */
  191.             if (!contin)
  192.                 break;
  193.  
  194.             /* read continuation line */
  195.             p--;
  196.             if (fgets(p, sizeof line - (p - line), stdin) == NULL)
  197.                 break;
  198.             LineNo++;
  199.  
  200.             if (!isspace(*p))
  201.                 usrerr("continuation line missing");
  202.         }
  203.         if (al.q_mailer != MN_LOCAL)
  204.         {
  205.             usrerr("cannot alias non-local names");
  206.             continue;
  207.         }
  208.         naliases++;
  209.         key.dsize = strlen(al.q_user) + 1;
  210.         key.dptr = al.q_user;
  211.         content.dsize = strlen(rhs) + 1;
  212.         if (content.dsize > longest)
  213.             longest = content.dsize;
  214.         content.dptr = rhs;
  215.         bytes += key.dsize + content.dsize;
  216.         if (store(key, content), 0)
  217.         /* if (f = store(key, content)) */
  218.             usrerr("Dbm internal error return %d from store\n", f);
  219.     }
  220.     fprintf(stderr, "%d aliases, %d bytes, longest %d bytes, %d errors\n",
  221.         naliases, bytes, longest, Errors);
  222.  
  223.     exit(ExitStat);
  224. }
  225.  
  226. usrerr(fmt, a, b, c, d, e)
  227.     char *fmt;
  228. {
  229.     Errors++;
  230.     fprintf(stderr, "line %d: ", LineNo);
  231.     fprintf(stderr, fmt, a, b, c, d, e);
  232.     fprintf(stderr, "\n");
  233.     return (-1);
  234. }
  235.  
  236. syserr(fmt, a, b, c, d, e)
  237.     char *fmt;
  238. {
  239.     return (usrerr(fmt, a, b, c, d, e));
  240. }
  241.