home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCP_Blars.lzh / uuxqt.c < prev   
C/C++ Source or Header  |  1991-10-11  |  7KB  |  323 lines

  1. /*
  2.  * uuxqt.c
  3.  *
  4.  * Unix to Unix Execute
  5.  *
  6.  * This program searches for work files in the spool
  7.  * directory and executes them.  Work files have a X. prefix.
  8.  *
  9.  * It is executed by 'uucico', 'cron', or 'at'
  10.  *
  11.  * Based on a program by Richard H. Lamb.
  12.  * Ported to OS-9/68000:  Wolfgang Ocker    January 1988
  13.  *
  14.  */
  15.  
  16. #include "uucp.h"
  17.  
  18. #include <modes.h>
  19. #include <dir.h>        /* defines MAXNAMLEN and directory stuff */
  20. #include <strings.h>
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include <errno.h>
  24. #include <pwd.h>
  25.  
  26. extern int  errno;
  27. extern char **environ;
  28.  
  29. #define MAXLINE   256
  30. #define ERROR     -1
  31. #define MAX_CMDS  50
  32.  
  33. #define TRUE      (-1)
  34. #define FALSE     (0)
  35.  
  36. char command[MAXLINE], input[MAXLINE], output[MAXLINE], line[MAXLINE];
  37. char Cfilename[MAXNAMLEN+1], filename[MAXNAMLEN+1], ltime[32];
  38. char systemname[200], username[200];
  39.  
  40. char *mfgets();
  41. void printmsg();
  42.  
  43. char *allcmds[MAX_CMDS];
  44. int  ncmds;     /* # of commands */
  45.  
  46. char **args;
  47.  
  48. extern int os9forkc();
  49.  
  50. FILE *log;      /* Log file */
  51.  
  52. void main(argc, argv)
  53.   int  argc;
  54.   char *argv[];
  55. {
  56.   register struct direct  *dir;
  57.   register char *p;
  58.   FILE          *fdC;
  59.   DIR           *dd;
  60.   int           inull, onull;
  61.   int           i;
  62.   long          tstamp;
  63.   int           lock;           /* Lock file */
  64.   int           cmds;           /* allowed commands */
  65.   char          tmp[MAXLINE];   /* temporary */
  66.   char          modtmp[200], *cp, *cp2;
  67.   char          s_spooldir[100], s_libdir[100], s_commands[120];
  68.   int           tmpfd0, tmpfd1, tmpfd2;
  69.   int           pid, status;
  70.   struct passwd *pw;
  71.  
  72.     if((pw = getpwname("uucp")) != NULL) {
  73.         if (getuid() != pw->pw_giduid) {
  74.           if (setuid(pw->pw_giduid) < 0) {
  75.             fprintf(stderr, "Must run as uucp\n");
  76.         exit(1);
  77.         }
  78.     }
  79.     }
  80.  
  81.     strcpy(s_spooldir, SPOOLDIR);
  82.     strcpy(s_libdir, LIBDIR);
  83.  
  84.   chdir(s_spooldir);
  85.  
  86.   log = fopen(XQTLOG, "a");
  87.  
  88.   if ((dd = opendir(".")) == (DIR *) NULL) {
  89.     exit(1);
  90.   }
  91.  
  92.   sprintf(s_commands, "%s/commands", s_libdir);
  93.   if ((cmds = open(s_commands, S_IREAD)) < 0) {
  94.     closedir(dd);
  95.     fprintf(stderr, "Could not open %s\n", s_commands);
  96.     exit(1);
  97.   }
  98.  
  99.   ncmds = 0;
  100.   while ((i = readln(cmds, tmp, sizeof tmp)) > 0) {
  101.     if (i == 1 || tmp[0]=='#')
  102.       continue;
  103.  
  104.     tmp[--i] = '\0';
  105.     if (p = index(tmp, ' '))
  106.       *p = '\0';
  107.  
  108.     if ((allcmds[ncmds] = (char *) malloc(strlen(tmp))) == NULL) {
  109.       close(cmds);
  110.       fprintf(stderr, "malloc failure\n");
  111.       exit(1);
  112.     }
  113.  
  114.     strcpy(allcmds[ncmds], tmp);
  115.     if (++ncmds >= MAX_CMDS)
  116.       break;
  117.   }
  118.  
  119.   close(cmds);
  120.  
  121.   while ((dir = readdir(dd)) != (struct direct *) NULL) {
  122.     if (strncmp(dir->d_name, "X.", 2) != 0)
  123.       continue;
  124.  
  125. fprintf(stderr, "Processing %s\n", dir->d_name);
  126.     strncpy(Cfilename, dir->d_name, MAXNAMLEN);
  127.  
  128.     if ((fdC = fopen(Cfilename, "r")) == (FILE *) ERROR)
  129.       continue;
  130.  
  131.     input[0] = output[0] = '\0';
  132.     inull = onull = FALSE;
  133.  
  134.     while (mfgets(line, MAXLINE, fdC) != NULL) {
  135.       switch (line[0]) {
  136.         case 'C':
  137.           strcpy(command, &line[2]);
  138.           break;
  139.  
  140.         case 'F':
  141.           strcpy(filename, &line[2]);
  142.       if ((p = index(filename, ' ')) != NULL) *p = '\0';
  143.  
  144.           if (access(filename, S_IREAD) < 0) {
  145.             fclose(fdC);
  146.             goto not_ready;
  147.           }
  148.           break;
  149.  
  150.         case 'I':
  151.           strcpy(input, &line[2]);
  152.           break;
  153.  
  154.         case 'O':
  155.           strcpy(output, &line[2]);
  156.           break;
  157.  
  158.         case 'U':
  159.           strcpy(username, &line[2]);
  160.       if ((p = index(username, ' ')) != NULL) {
  161.           *p++ = '\0';
  162.           strcpy(systemname, p);
  163.       }
  164.  
  165.           if (username[0] == '\"') {
  166.             strcpy(username, username+1);
  167.             username[strlen(username)-1] = '\0';
  168.           }
  169.           break;
  170.  
  171.         default:
  172.           break;
  173.       }
  174.     }
  175.  
  176.     fclose(fdC);
  177.  
  178.     if (strlen(input) == 0) {
  179.       inull = TRUE;
  180.       strcpy(input, "/nil");
  181.     }
  182.  
  183.     if (strlen(output) == 0) {
  184.       onull = TRUE;
  185.       strcpy(output, "/nil");
  186.     }
  187.  
  188. fprintf(stderr, "input='%s' output='%s' command='%s'\n", input, output, command);
  189.     if (getargs(command, &args) < 0) {
  190.       printmsg("Fatal: getargs() returns error\n");
  191.       goto not_ready;
  192.     }
  193. for(i=0; args[i] != NULL; i++) {
  194. fprintf(stderr, "arg[%d]='%s'\n", i, args[i]);
  195. }
  196.    
  197.     if (legalcommand(args[0]) < 0) {
  198.       printmsg("System Security check: can't execute \'%s\'", command);
  199.       goto not_ready;
  200.     }
  201.  
  202.     printmsg("");
  203.  
  204.     tmpfd0 = dup(0);
  205.     close(0);
  206.     if (open(input, S_IREAD) != 0) {
  207.         fprintf(stderr, "Could not open '%s'\n", input);
  208.     close(0);
  209.     dup(tmpfd0);
  210.     close(tmpfd0);
  211.     goto not_ready;
  212.     }
  213.     tmpfd1 = dup(1);
  214.     close(1);
  215.     if (creat(output, S_IWRITE) != 1) {
  216.         fprintf(stderr, "Could not create '%s'\n", output);
  217.     close(0);
  218.     dup(tmpfd0);
  219.     close(1);
  220.     dup(tmpfd1);
  221.     goto not_ready;
  222.     }
  223.     tmpfd2 = dup(2);
  224.     close(2);
  225.     if (open("/nil", S_IWRITE) != 2) {
  226.         fprintf(stderr, "could not open /nil for stderr\n");
  227.     close(0);
  228.     dup(tmpfd0);
  229.     close(1);
  230.     dup(tmpfd1);
  231.     close(2);
  232.     dup(tmpfd2);
  233.     goto not_ready;
  234.     }
  235.  
  236.     pid = os9exec(os9forkc, args[0], args, environ, 0, 0, 3);
  237.  
  238.     close(0);
  239.     dup(tmpfd0);
  240.     close(1);
  241.     dup(tmpfd1);
  242.     close(2);
  243.     dup(tmpfd2);
  244.     close(tmpfd0);
  245.     close(tmpfd1);
  246.     close(tmpfd2);
  247.     free(args);
  248.  
  249.     if (pid == -1)
  250.       printmsg("Error %d on line '%s'", errno, line);
  251.     else {
  252.       while (wait(&status) != pid) ;
  253.       if ((status & 0x00ffff) != 0)
  254.         printmsg("Error %d on line '%s'", status & 0x0ffff, line);
  255.       else {
  256.         unlink(Cfilename);
  257.  
  258.         if (!inull)
  259.           while (unlink(input) < 0)
  260.             if (errno == E_SHARE)
  261.               sleep(2);
  262.             else
  263.               break;
  264.  
  265.         if (!onull)
  266.           while (unlink(output) < 0)
  267.             if (errno == E_SHARE)
  268.               sleep(2);
  269.             else
  270.               break;
  271.       }
  272.     }
  273.  
  274. not_ready:
  275.     ;
  276.  
  277.   }
  278.  
  279.   if (log != NULL)
  280.     fclose(log);
  281.  
  282.   closedir(dd);
  283. }
  284.  
  285.  
  286. /*
  287.  * l e g a l c o m m a n d
  288.  */
  289. int legalcommand(command)
  290.   char *command;
  291.  
  292. {
  293.   int  i;
  294.  
  295.   for (i = 0; i < ncmds; i++)
  296.     if (!strcmp(command, allcmds[i]))
  297.       return(0);
  298.  
  299.   return(-1);
  300. }
  301.  
  302. /*
  303.  * p r i n t m s g
  304.  */
  305. void printmsg(msg, a1, a2, a3)
  306.   char *msg;
  307. {
  308.   int  tstamp;
  309.   char ltime[40];
  310.   char buf[100];
  311.  
  312.   if (log == NULL)
  313.     return;
  314.  
  315.   sprintf(buf, msg, a1, a2, a3);
  316.  
  317.   time(&tstamp);
  318.   strcpy(ltime, ctime(&tstamp));
  319.   ltime[strlen(ltime) - 1] = '\0';
  320.  
  321.   fprintf(log, "%s %s %s!%s %s\n", ltime, buf, systemname, username, line);
  322. }
  323.