home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / batch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  11.9 KB  |  502 lines

  1. #ifndef lint
  2. static    char    *rcsid = "@(#)$Header: batch.c,v 1.15 91/01/13 03:27:26 sob Exp $";
  3. #endif
  4. /*
  5.  * Batch subroutine for Cnews.
  6.  * Cooperates with C news input subsystem.
  7.  *    newsboot must be told to run partial batches left at a crash.
  8.  *
  9. ***************************************************************************
  10. This work in its current form is Copyright 1989 Stan Barber
  11. and is based on the work of Henry Spencer and Geoff Collyer at the University
  12. of Toronto. This software may be distributed freely as long as no profit is
  13. made from such distribution and this notice is reproducted in whole.
  14. ***************************************************************************
  15. This software is provided on an "as is" basis with no guarantee of 
  16. usefulness or correctness of operation for any purpose, intended or
  17. otherwise. The author is in no way liable for this software's performance
  18. or any damage it may cause to any data of any kind anywhere.
  19. ***************************************************************************
  20. */
  21. #include "common.h"
  22. #include <signal.h>
  23. #ifdef sparc
  24. #include <vfork.h>
  25. #endif
  26. #ifdef BATCHED_INPUT
  27. #define YES 1
  28. #define NO 0
  29.  
  30. /* imports */
  31. extern time_t time();
  32. extern char *malloc(), *mktemp(), *index(), *rindex();
  33. /* forwards */
  34. static char *strsave();
  35. #ifdef XFER_TIMEOUT
  36. static int xfer_timeout();
  37. #endif
  38. static int cpstdin();
  39. static int appbatch();
  40. static int enqueue();
  41.  
  42. /* private data */
  43. static char tempfile[256];
  44. static int xfer_lines, old_xfer_lines;
  45.  
  46. static char art[COPYSIZE];        /* entire article, if it fits */
  47. static char *endart = art;        /* points just past end of article */
  48. static int incore = YES;
  49.  
  50. #ifdef NONEWSRUN
  51. static int uniq = 0;                    /* unique counter for this process */
  52. static int in_batchdir = NO;
  53.  
  54. #endif /* NONEWSRUN */
  55. static struct batch_file {
  56.     char *name;
  57.     FILE *file;
  58.     char isopen;
  59.     time_t start;            /* time of creation */
  60.     off_t size;            /* current size */
  61.     int arts;            /* number of articles */
  62. } btch = { NULL, NULL, NO, 0, 0 };
  63.  
  64. /*
  65.  * stash stdin (up to ".") on the end of the batch input file.
  66.  * kick newsrun if the batch is non-empty and too big or too old.
  67.  *
  68.  * Parameters:
  69.  *    "cont_code" is the response code to transmit on successful startup.
  70.  *    "err_code" is the response code to transmit when something goes wrong.
  71.  *
  72.  * Returns: -1 on non-zero return from child, 0 on error before fork/exec, 1 else.
  73.  * Side effects: Creates and removes temporary file; accepts input from client.
  74.  *        Can time out if XFER_TIMEOUT is defined.
  75.  */
  76. int
  77. batch_input_article(cont_code, err_code, errbuf, msg_id)
  78. int cont_code, err_code;
  79. char *errbuf;
  80. char *msg_id;
  81. {
  82.     int status = 1;            /* okay status */
  83.  
  84.     /* protect locking */
  85.     signal(SIGINT, SIG_IGN);
  86.     signal(SIGQUIT, SIG_IGN);
  87.     signal(SIGHUP, SIG_IGN);
  88.  
  89. #ifdef NONEWSRUN
  90.     /* This really should be done in main.c so that we don't have to
  91.        check each time we get a new article - sigh */
  92.  
  93.     if (!in_batchdir)
  94.         if (chdir(INDIR) < 0)
  95.             syslog(LOG_ERR, "chdir(%s) failed", INDIR);
  96.         else
  97.             in_batchdir = YES;
  98.  
  99. #endif /* NONEWSRUN */
  100.     if (btch.name == NULL) {
  101.         /* BATCH_FILE may trigger unprivileged() */
  102.         btch.name = mktemp(strsave(BATCH_FILE));
  103.     }
  104.     if (btch.name == NULL)
  105.         return 0;
  106.     tempfile[0] = '\0';
  107. #ifdef UMASK
  108.     (void) umask(UMASK);
  109. #endif
  110.     /* may create tempfile */
  111.     if (!cpstdin(cont_code, err_code, errbuf, msg_id))
  112.         return 0;
  113. #ifdef POSTER
  114.     if (tempfile[0])
  115.         (void) chown(tempfile, uid_poster, gid_poster);
  116. #endif
  117.     status = appbatch();
  118.     btch.arts++;
  119.     if (tempfile[0] != '\0')
  120.         (void) unlink(tempfile);
  121.     if (status == 1 && oktorunbatch())
  122.         status = enqueue(cont_code, err_code, errbuf);
  123.     return status;
  124. }
  125.  
  126. int                        /* boolean */
  127. oktorunbatch()
  128. {
  129.     struct stat stbuf;
  130.  
  131.     if (!btch.isopen || fstat(fileno(btch.file), &stbuf) < 0)
  132.         return NO;
  133.     btch.size = stbuf.st_size;
  134.     return(btch.arts >= TOOMANY || btch.size > TOOBIG ||
  135.         btch.size > 0 && time((time_t *)NULL) - btch.start > TOOOLD);
  136. }
  137.  
  138. /*
  139.  * Copy standard input (up to a "." line) to art, if it fits,
  140.  * else to a temporary file.
  141.  */
  142. /* ARGSUSED errbuf */
  143. static int                    /* boolean: got article ok? */
  144. cpstdin(cont_code, err_code, errbuf, msg_id)
  145. int cont_code, err_code;
  146. char *errbuf, *msg_id;
  147. {
  148.     register FILE *tfp = NULL;
  149.     register char *cp, *realline;
  150.     char line[NNTP_STRLEN];
  151.     int toobig = NO;
  152.  
  153.     /* TODO: is this right?  used to open here, with errors here */
  154.     printf("%d Ok\r\n", cont_code);
  155.     (void) fflush(stdout);
  156.  
  157.     xfer_lines = old_xfer_lines = 0;
  158.     incore = YES;
  159.     art[0] = '\0';
  160.     endart = art;
  161. #ifdef XFER_TIMEOUT
  162.     signal(SIGALRM, xfer_timeout);
  163.     (void) alarm(XFER_TIMEOUT);
  164. #endif
  165.     while (fgets(line, sizeof line, stdin) != NULL) {
  166.         xfer_lines++;
  167.         if ((cp = rindex(line, '\r')) != NULL ||
  168.             (cp = rindex(line, '\n')) != NULL)
  169.             *cp = '\0';            /* nuke CRLF */
  170.         if (line[0] == '.' && line[1] == '\0')
  171.             break;                /* article end: exit */
  172.         /* remove hidden dot if present */
  173.         realline = (line[0] == '.'? line+1: line);
  174.         if (toobig) {                /* straight to disk */
  175.             (void) fputs(realline, tfp);
  176.             (void) putc('\n', tfp);
  177.         } else {
  178.             int len = strlen(realline);
  179.  
  180.             /*
  181.              * Does art have room to append realline + \n\0?
  182.              * If not, open temp file and dump art & realline there.
  183.              */
  184.             if (sizeof art - (endart - art) < len + 1 + 1) {
  185.                 (void) strcpy(tempfile, "/tmp/rpostXXXXXX");
  186.                 (void) mktemp(tempfile);
  187.                 tfp = fopen(tempfile, "w");
  188.                 if (tfp == NULL) {
  189.                     printf("%d Cannot create temporary file.\r\n",
  190.                         err_code);
  191.                     (void) fflush(stdout);
  192.                     return 0;
  193.                 }
  194. #ifdef OK_IN_MIDDLE_OKAY
  195.                 else {
  196.                     printf("%d Ok\r\n", cont_code);
  197.                     (void) fflush(stdout);
  198.                 }
  199. #endif
  200.                 (void) fwrite(art, 1, endart - art, tfp);
  201.                 toobig = YES;
  202.                 incore = NO;
  203.                 art[0] = '\0';
  204.                 endart = art;
  205.                 (void) fputs(realline, tfp);
  206.                 (void) putc('\n', tfp);
  207.             } else {
  208.                 /* fits: append realline\n to art at endart */
  209.                 (void) strcpy(endart, realline);
  210.                 endart += len;
  211.                 *endart++ = '\n';
  212.                 *endart = '\0';
  213.             }
  214.         }
  215.     }
  216.     if (tfp != NULL)
  217.         (void) fclose(tfp);
  218. #ifdef XFER_TIMEOUT
  219.     (void) alarm(0);
  220.     (void) signal(SIGALRM, SIG_DFL);
  221. #endif
  222.  
  223.     /* See if the connection got closed somehow... */
  224.     if (line[0] != '.' && line[1] != '\0') {
  225.         if (tempfile[0] != '\0')
  226.             (void) unlink(tempfile);
  227. #ifdef SYSLOG
  228. #ifdef LOG
  229.         syslog(LOG_ERR,
  230.             "%s cpstdin: EOF before period on line by itself %s",
  231.             hostname, msg_id);
  232. #else
  233.         syslog(LOG_ERR,
  234.             "cpstdin: EOF before period on line by itself %s", msg_id);
  235. #endif
  236. #endif
  237.         return 0;
  238.     }
  239.     return 1;
  240. }
  241.  
  242. static int
  243. xfer_timeout()
  244. {
  245. #ifdef XFER_TIMEOUT
  246.     if (old_xfer_lines < xfer_lines) {
  247.         old_xfer_lines = xfer_lines;
  248.         (void) alarm(XFER_TIMEOUT);
  249.         return;
  250.     }
  251.     /* Timed out. */
  252.     printf("%d timeout after %d seconds, closing connection.\r\n",
  253.         ERR_FAULT, XFER_TIMEOUT);
  254.     fflush(stdout);
  255. #ifdef SYSLOG
  256. #ifdef LOG
  257.     syslog(LOG_ERR, "%s transfer_timeout", hostname);
  258. #endif LOG
  259. #endif
  260.     (void) unlink(tempfile);
  261.     exit(1);
  262. #endif XFER_TIMEOUT
  263. }
  264.  
  265. /*
  266.  * Append "#! rnews count" and art (or tempfile) to batch file, locking
  267.  * assumed. If batch file is too big or too old (but not empty), feed it
  268.  * to newsrun.
  269.  */
  270. static int                /* same as batch_input_article */
  271. appbatch()
  272. {
  273.     register FILE *tfp = NULL;
  274.     register int bytes = 0;
  275.     int status = 1;                /* okay status */
  276.     long size = 0;
  277.     char artbuf[COPYSIZE];
  278.     struct stat stbuf;
  279.  
  280.     if (btch.file == NULL) {
  281.         btch.file = fopen(btch.name, "a");
  282.         if (btch.file == NULL) {
  283. #ifdef SYSLOG
  284.             syslog(LOG_ERR,"appbatch: fopen: %s: %m", btch.name);
  285. #endif
  286.             return 0;
  287.         }
  288.         btch.isopen = YES;
  289.         btch.start = time(&btch.start);
  290.         btch.size = 0;
  291.         btch.arts = 0;
  292.     }
  293.  
  294.     /* find article size and write the article */
  295.     if (incore)
  296.         size = endart - art;
  297.     else {
  298.         tfp = fopen(tempfile, "r");
  299.         if (tfp == NULL)
  300.             return 0;
  301.         if (fstat(fileno(tfp), &stbuf) >= 0)
  302.             size = stbuf.st_size;
  303.     }
  304.     (void) fprintf(btch.file, "#! rnews %ld %s\n", size, hostname);
  305.  
  306.     /* copy the article to the batch file */
  307.     if (incore)
  308.         (void) fwrite(art, 1, endart - art, btch.file);
  309.     else {
  310.         while ((bytes = fread(artbuf, 1, sizeof artbuf, tfp)) > 0)
  311.             if (fwrite(artbuf, 1, bytes, btch.file) != bytes) {
  312. #ifdef SYSLOG
  313.                 syslog(LOG_ERR,
  314.                     "enqueue: fwrite can't write %s",
  315.                     btch.name);
  316. #endif
  317.                 status = 0;    /* hmm, #! count is off */
  318.                 break;
  319.             }
  320.         (void) fclose(tfp);
  321.     }
  322.     if (fflush(btch.file) == EOF) {
  323. #ifdef SYSLOG
  324.         syslog(LOG_ERR, "enqueue: fflush: %s", btch.name);
  325. #endif
  326.         status = 0;
  327.     }
  328.     return status;
  329. }
  330.  
  331. /*
  332.  * Enqueue any partial batch.  Called before exit.
  333.  */
  334. enqpartbatch(cont_code, err_code, errbuf)
  335. int cont_code, err_code;
  336. char *errbuf;
  337. {
  338.     struct stat stbuf;
  339.  
  340.     if (btch.isopen && fstat(fileno(btch.file), &stbuf) >= 0) {
  341.         if (btch.size > 0)
  342.             enqueue(cont_code, err_code, errbuf);
  343.         else {
  344.             (void) fclose(btch.file);
  345.             btch.file = NULL;
  346.             btch.isopen = NO;
  347.             (void) unlink(btch.name); /* remove empty batch */
  348.         }
  349.     }
  350. }
  351.  
  352. /* 
  353.  * insert the batch file into the input subsystem queue by renaming
  354.  * it to an all-numeric name, then kick newsrun to process it.
  355.  * locks btch.name as appropriate.
  356.  */
  357. static int                /* same as batch_input_article */
  358. enqueue(cont_code, err_code, errbuf)
  359. int cont_code, err_code;
  360. char *errbuf;
  361. {
  362.     time_t now;
  363.     int pid, wpid, status, fd, exitstat;
  364.     char permname[MAXDIGITS], *number = permname, *newsrun;
  365.     struct stat stbuf;
  366. #ifdef POSTER
  367.     char *envp[4], user[sizeof(POSTER) + 5], logname[sizeof(POSTER) + 8];
  368.     char *home;
  369. #else
  370.     char *envp[1];
  371. #endif
  372.  
  373.     if (fclose(btch.file) != 0) {
  374. #ifdef SYSLOG
  375.         syslog(LOG_ERR, "enqueue: fclose: %m");
  376. #endif
  377.     }
  378.     btch.file = NULL;
  379.     btch.isopen = NO;
  380.     btch.start = 0;
  381.     btch.size = 0;
  382.     btch.arts = 0;
  383.  
  384.     (void) fflush(stdout);
  385.     (void) fflush(stderr);
  386. #ifndef NONEWSRUN
  387.     pid = fork();
  388.     if (pid == -1) {
  389. #ifdef SYSLOG
  390.         syslog(LOG_ERR, "enqueue: fork: %m");
  391. #endif
  392.         return 0;
  393.     } else if (pid != 0) {            /* parent */
  394.         while ((wpid = wait(&status)) != -1 && wpid != pid)
  395.             ;
  396.         exitstat = (status>>8)&0377;
  397. #ifdef SYSLOG
  398.         if (exitstat != 0) {
  399.             syslog(LOG_ERR, " enqueue returned exit status 0%o",
  400.                 exitstat);
  401.         }
  402. #endif
  403.         return exitstat != 0? -1 :1;
  404.     }
  405. #endif /* NONEWSRUN */
  406. #ifdef POSTER
  407. #ifndef USG
  408.         if (getuid() == 0) initgroups(POSTER,gid_poster);
  409. #endif
  410.         (void) setgid(gid_poster);
  411.         (void) setuid(uid_poster);
  412. #endif
  413. #ifndef NONEWSRUN
  414.     /* child: must exit */
  415. #ifdef SYSLOG
  416.     /* Close in such a way that syslog() will know to reopen */
  417.     closelog();
  418. #endif
  419.     for (fd = 3; fd < 20; fd++)
  420.         (void) close(fd);
  421.     if (chdir(INDIR) < 0) {
  422. #ifdef SYSLOG
  423.         syslog(LOG_ERR, "enqueue: chdir(%s): %m", INDIR);
  424. #else
  425.         ;
  426. #endif
  427.     }
  428.  
  429.     /* rename btch.name to a number so newsrun will see it */
  430.     sprintf(number, "%ld", (long)time(&now));
  431. #else
  432.     sprintf(number, "%d.%d", getpid(), uniq++);
  433. #endif /* NONEWSRUN */
  434.     while (link(btch.name, permname) < 0) {
  435.         if (stat(btch.name, &stbuf) < 0)
  436.             break;
  437.         sleep(2);
  438. #ifndef NONEWSRUN
  439.         sprintf(number, "%ld", (long)time(&now));
  440. #else
  441.         sprintf(number, "%d.%d", getpid(), uniq++);
  442. #endif /* NONEWSRUN */
  443.     }
  444.     if (unlink(btch.name) < 0) {
  445. #ifdef SYSLOG
  446.         syslog(LOG_ERR, "enqueue: cannot unlink %s: %m", btch.name);
  447. #endif
  448.     }
  449. #ifndef NONEWSRUN
  450.  
  451.     signal(SIGINT, SIG_IGN);
  452.     signal(SIGQUIT, SIG_IGN);
  453.     signal(SIGHUP, SIG_IGN);
  454.     (void) fflush(stdout);
  455.     (void) fflush(stderr);
  456.     newsrun = strsave(NEWSRUN);
  457.     if (newsrun == NULL)
  458.         newsrun = "/usr/lib/newsbin/input/newsrun";
  459.  
  460.     /* Empty environment keeps cnews inews from telling lies */
  461. #ifdef POSTER
  462.     sprintf(user, "USER=%s", POSTER);
  463.     sprintf(logname, "LOGNAME=%s", POSTER);
  464.     if ((home = (char *)malloc(strlen(home_poster)+5)) != NULL)
  465.         sprintf(home, "HOME=%s", home_poster);
  466.     envp[0] = user;
  467.     envp[1] = logname;
  468.     envp[2] = home;
  469.     envp[3] = 0;
  470. #else
  471.     envp[0] = 0;
  472. #endif
  473. #ifdef USG
  474.      /* execle() fails because newsrun is a shell procedure */
  475.      execle("/bin/sh", "sh", newsrun, (char *)NULL, envp);
  476. #else
  477.     execle(newsrun, newsrun, (char *)NULL, envp);
  478. #endif
  479. #ifdef SYSLOG
  480.     syslog(LOG_ERR, "enqueue: execle(%s): %m", newsrun);
  481. #endif
  482.     exit(1);
  483.     /* NOTREACHED */
  484. #else
  485.     return(1);
  486. #endif /* NONEWSRUN */
  487. }
  488.  
  489. static char *
  490. strsave(s)
  491. register char *s;
  492. {
  493.     register char *news = malloc((unsigned)(strlen(s) + 1));
  494.  
  495.     if (news != NULL)
  496.         strcpy(news, s);
  497.     return news;
  498. }
  499. #endif
  500.  
  501.  
  502.