BSD 3 development
[unix-history] / usr / src / cmd / uucp / cntrl.c
index dd2c9f4..fdb245a 100644 (file)
@@ -1,7 +1,10 @@
+       /*  cntrl  2.7  5/24/79  21:37:36  */
 #include "uucp.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include "uucp.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 
+static char SiD[] = "@(#)cntrl 2.7";
+
 
 struct Proto {
        char P_id;
 
 struct Proto {
        char P_id;
@@ -32,8 +35,27 @@ int (*Turnon)(), (*Turnoff)();
 
 #define YES "Y"
 #define NO "N"
 
 #define YES "Y"
 #define NO "N"
-#define Y 'Y'
-#define N 'N'
+
+/*  failure messages  */
+#define EM_MAX         6
+#define EM_LOCACC      "N1"    /* local access to file denied */
+#define EM_RMTACC      "N2"    /* remote access to file/path denied */
+#define EM_BADUUCP     "N3"    /* a bad uucp command was generated */
+#define EM_NOTMP       "N4"    /* remote error - can't create temp */
+#define EM_RMTCP       "N5"    /* can't copy to remote directory - file in public */
+#define EM_LOCCP       "N6"    /* can't copy on local system */
+
+char *Em_msg[] = {
+       "COPY FAILED (reason not given by remote)",
+       "local access to file denied",
+       "remote access to path/file denied",
+       "system error - bad uucp command generated",
+       "remote system can't create temp file",
+       "can't copy to file/directory - file left in PUBDIR/user/file",
+       "can't copy to file/directory - file left in PUBDIR/user/file"
+};
+
+/*       */
 
 
 #define XUUCP 'X'      /* execute uucp (string) */
 
 
 #define XUUCP 'X'      /* execute uucp (string) */
@@ -53,9 +75,9 @@ int (*Turnon)(), (*Turnoff)();
 #define W_DFILE                wrkvec[5]
 #define W_MODE         wrkvec[6]
 
 #define W_DFILE                wrkvec[5]
 #define W_MODE         wrkvec[6]
 
-#define RMESG(m, s) if (rmesg(m, s) != 0) return(FAIL);
-#define RAMESG(s) if (rmesg('\0', s) != 0) return(FAIL)
-#define WMESG(m, s) if(wmesg(m, s) != 0) return(FAIL)
+#define RMESG(m, s) if (rmesg(m, s) != 0) {(*Turnoff)(); return(FAIL);}
+#define RAMESG(s) if (rmesg('\0', s) != 0) {(*Turnoff)(); return(FAIL);}
+#define WMESG(m, s) if(wmesg(m, s) != 0) {(*Turnoff)(); return(FAIL);}
 
 char Wfile[MAXFULLNAME] = {'\0'};
 char Dfile[MAXFULLNAME];
 
 char Wfile[MAXFULLNAME] = {'\0'};
 char Dfile[MAXFULLNAME];
@@ -86,6 +108,7 @@ char *wkpre;
        extern (*Rdmsg)(), (*Wrmsg)();
        extern char *index(), *lastpart();
        int status = 1, i;
        extern (*Rdmsg)(), (*Wrmsg)();
        extern char *index(), *lastpart();
        int status = 1, i;
+       int mailopt;
        int ret;
        static int pnum, tmpnum = 0;
 
        int ret;
        static int pnum, tmpnum = 0;
 
@@ -100,6 +123,7 @@ top:
                        goto process;
                }
                wrktype = W_TYPE[0];
                        goto process;
                }
                wrktype = W_TYPE[0];
+               mailopt = index(W_OPTNS, 'm') != NULL;
 
                DEBUG(4, "wrktype %c, ", wrktype);
                if (wrktype == XUUCP) {
 
                DEBUG(4, "wrktype %c, ", wrktype);
                if (wrktype == XUUCP) {
@@ -127,21 +151,24 @@ top:
                if (wrktype == SNDFILE ) {
                        strcpy(filename, W_FILE1);
                        expfile(filename);
                if (wrktype == SNDFILE ) {
                        strcpy(filename, W_FILE1);
                        expfile(filename);
-                       if (chkpth(User, "", filename)) {
+                       if (chkpth(User, "", filename) || anyread(filename)) {
                                /*  access denied  */
                                logent("DENIED", "ACCESS");
                                /*  access denied  */
                                logent("DENIED", "ACCESS");
-                               unlink(W_DFILE);
-                               unlink(Wfile);
+                               unlinkdf(W_DFILE);
+                               lnotify(User, filename, "access denied");
                                goto top;
                        }
 
                        strcpy(Dfile, W_DFILE);
                                goto top;
                        }
 
                        strcpy(Dfile, W_DFILE);
-                       if ((fp = fopen(Dfile, "r")) == NULL
-                         && (fp = fopen(filename, "r")) == NULL) {
+                       fp = NULL;
+                       if (index(W_OPTNS, 'c') == NULL)
+                               fp = fopen(Dfile, "r");
+                       if (fp == NULL &&
+                          (fp = fopen(filename, "r")) == NULL) {
                                /*  can not read data file  */
                                logent("CAN'T READ DATA", "FAILED");
                                /*  can not read data file  */
                                logent("CAN'T READ DATA", "FAILED");
-                               unlink(Wfile);
-                               unlink(Dfile);
+                               unlinkdf(Dfile);
+                               lnotify(User, filename, "can't access");
                                goto top;
                        }
                }
                                goto top;
                        }
                }
@@ -153,15 +180,14 @@ top:
                         || chkperm(filename, User, index(W_OPTNS, 'd'))) {
                                /*  access denied  */
                                logent("DENIED", "ACCESS");
                         || chkperm(filename, User, index(W_OPTNS, 'd'))) {
                                /*  access denied  */
                                logent("DENIED", "ACCESS");
-                               unlink(Wfile);
+                               lnotify(User, filename, "access denied");
                                goto top;
                        }
                        sprintf(Dfile, "%s/TM.%05d.%03d", Spool, pnum, tmpnum++);
                        if ((fp = fopen(Dfile, "w")) == NULL) {
                                /*  can not create temp  */
                                logent("CAN'T CREATE TM", "FAILED");
                                goto top;
                        }
                        sprintf(Dfile, "%s/TM.%05d.%03d", Spool, pnum, tmpnum++);
                        if ((fp = fopen(Dfile, "w")) == NULL) {
                                /*  can not create temp  */
                                logent("CAN'T CREATE TM", "FAILED");
-                               unlink(Wfile);
-                               unlink(Dfile);
+                               unlinkdf(Dfile);
                                goto top;
                        }
                        chmod(Dfile, 0666);
                                goto top;
                        }
                        chmod(Dfile, 0666);
@@ -179,21 +205,21 @@ sendmsg:
        goto process;
 
 process:
        goto process;
 
 process:
+       ultouch();      /*  touch all lock files  */
        DEBUG(4, " PROCESS: msg - %s\n", msg);
        switch (msg[0]) {
 
        case RQSTCMPT:
                DEBUG(4, "%s\n", "RQSTCMPT:");
        DEBUG(4, " PROCESS: msg - %s\n", msg);
        switch (msg[0]) {
 
        case RQSTCMPT:
                DEBUG(4, "%s\n", "RQSTCMPT:");
-               logent((msg[1] == 'N') ? "FAILED" : "SUCCEEDED", "REQUEST");
+               logent(msg, "REQUESTED");
                if (role == MASTER) {
                if (role == MASTER) {
-                       notify(W_OPTNS, W_USER, W_FILE1, Rmtname,
-                         (msg[1] == N) ? "failed" : "succeeded");
+                       notify(mailopt, W_USER, W_FILE1, Rmtname, &msg[1]);
                }
                goto top;
 
        case HUP:
                DEBUG(4, "%s\n", "HUP:");
                }
                goto top;
 
        case HUP:
                DEBUG(4, "%s\n", "HUP:");
-               if (msg[1] == Y) {
+               if (msg[1] == 'Y') {
                        WMESG(HUP, YES);
                        (*Turnoff)();
                        Rdmsg = imsg;
                        WMESG(HUP, YES);
                        (*Turnoff)();
                        Rdmsg = imsg;
@@ -201,7 +227,7 @@ process:
                        return(0);
                }
 
                        return(0);
                }
 
-               if (msg[1] == N) {
+               if (msg[1] == 'N') {
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        role = SLAVE;
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        role = SLAVE;
@@ -221,7 +247,6 @@ process:
 
        case XUUCP:
                if (role == MASTER) {
 
        case XUUCP:
                if (role == MASTER) {
-                       unlink(Wfile);
                        goto top;
                }
 
                        goto top;
                }
 
@@ -249,25 +274,30 @@ process:
                /*  MASTER section of SNDFILE  */
 
                DEBUG(4, "%s\n", "SNDFILE:");
                /*  MASTER section of SNDFILE  */
 
                DEBUG(4, "%s\n", "SNDFILE:");
-               if (msg[1] == N) {
-                       logent("DENIED", "REQUEST");
+               if (msg[1] == 'N') {
+                       i = atoi(&msg[2]);
+                       if (i < 0 || i > EM_MAX)
+                               i = 0;
+                       logent(Em_msg[i], "REQUEST");
+                       notify(mailopt, W_USER, W_FILE1, Rmtname, &msg[1]);
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        fclose(fp);
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        fclose(fp);
-                       unlink(W_DFILE);
-                       unlink(Wfile);
+                       unlinkdf(W_DFILE);
                        goto top;
                }
 
                        goto top;
                }
 
-               if (msg[1] == Y) {
+               if (msg[1] == 'Y') {
                        /* send file */
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        ret = (*Wrdata)(fp, Ofn);
                        fclose(fp);
                        /* send file */
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        ret = (*Wrdata)(fp, Ofn);
                        fclose(fp);
-                       if (ret != 0)
+                       if (ret != 0) {
+                               (*Turnoff)();
                                return(FAIL);
                                return(FAIL);
-                       unlink(W_DFILE);
+                       }
+                       unlinkdf(W_DFILE);
                        RMESG(RQSTCMPT, msg);
                        goto process;
                }
                        RMESG(RQSTCMPT, msg);
                        goto process;
                }
@@ -289,7 +319,7 @@ process:
                expfile(filename);
                if (chkpth("", Rmtname, filename)
                 || chkperm(filename, Loginuser, index(W_OPTNS, 'd'))) {
                expfile(filename);
                if (chkpth("", Rmtname, filename)
                 || chkperm(filename, Loginuser, index(W_OPTNS, 'd'))) {
-                       WMESG(SNDFILE, NO);
+                       WMESG(SNDFILE, EM_RMTACC);
                        logent("DENIED", "PERMISSION");
                        goto top;
                }
                        logent("DENIED", "PERMISSION");
                        goto top;
                }
@@ -303,9 +333,9 @@ process:
                DEBUG(4, "chkpth ok Rmtname - %s\n", Rmtname);
                sprintf(Dfile, "%s/TM.%05d.%03d", Spool, pnum, tmpnum++);
                if((fp = fopen(Dfile, "w")) == NULL) {
                DEBUG(4, "chkpth ok Rmtname - %s\n", Rmtname);
                sprintf(Dfile, "%s/TM.%05d.%03d", Spool, pnum, tmpnum++);
                if((fp = fopen(Dfile, "w")) == NULL) {
-                       WMESG(SNDFILE, NO);
+                       WMESG(SNDFILE, EM_NOTMP);
                        logent("CAN'T OPEN", "DENIED");
                        logent("CAN'T OPEN", "DENIED");
-                       unlink(Dfile);
+                       unlinkdf(Dfile);
                        goto top;
                }
                chmod(Dfile, 0666);
                        goto top;
                }
                chmod(Dfile, 0666);
@@ -313,62 +343,73 @@ process:
                WMESG(SNDFILE, YES);
                ret = (*Rddata)(Ifn, fp);
                fclose(fp);
                WMESG(SNDFILE, YES);
                ret = (*Rddata)(Ifn, fp);
                fclose(fp);
-               if (ret != 0)
+               if (ret != 0) {
+                       (*Turnoff)();
                        return(FAIL);
                        return(FAIL);
+               }
                /* copy to user directory */
                status = xmv(Dfile, filename);
                /* copy to user directory */
                status = xmv(Dfile, filename);
-               WMESG(RQSTCMPT, status ? NO : YES);
+               WMESG(RQSTCMPT, status ? EM_RMTCP : YES);
                logent(status ? "FAILED" : "SUCCEEDED", "COPY");
                logent(status ? "FAILED" : "SUCCEEDED", "COPY");
-               sscanf(W_MODE, "%o", &filemode);
-               DEBUG(4, "mode - %o\n", filemode);
-               if (filemode <= 0)
-                       filemode = 0666;
                if (status == 0) {
                if (status == 0) {
-                       filemode |= 0666;
+                       sscanf(W_MODE, "%o", &filemode);
+                       DEBUG(4, "mode - %o\n", filemode);
+                       if (filemode <= 0)
+                               filemode = 0666;
                        chmod(filename, filemode | 0666);
                }
                        chmod(filename, filemode | 0666);
                }
+               else {
+                       putinpub(filename, Dfile, W_USER);
+               }
+
                goto top;
 
        case RCVFILE:
                /*  MASTER section of RCVFILE  */
 
                DEBUG(4, "%s\n", "RCVFILE:");
                goto top;
 
        case RCVFILE:
                /*  MASTER section of RCVFILE  */
 
                DEBUG(4, "%s\n", "RCVFILE:");
-               if (msg[1] == N) {
-                       logent("REQUEST", "DENIED");
+               if (msg[1] == 'N') {
+                       i = atoi(&msg[2]);
+                       if (i < 0 || i > EM_MAX)
+                               i = 0;
+                       logent(Em_msg[i], "REQUEST");
+                       notify(mailopt, W_USER, W_FILE1, Rmtname, &msg[1]);
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        ASSERT(role == MASTER,
                                "role - %d", role);
-                       unlink(Wfile);
                        fclose(fp);
                        goto top;
                }
 
                        fclose(fp);
                        goto top;
                }
 
-               if (msg[1] == Y) {
+               if (msg[1] == 'Y') {
                        /* receive file */
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        ret = (*Rddata)(Ifn, fp);
                        fclose(fp);
                        /* receive file */
                        ASSERT(role == MASTER,
                                "role - %d", role);
                        ret = (*Rddata)(Ifn, fp);
                        fclose(fp);
-                       if (ret != 0)
+                       if (ret != 0) {
+                               (*Turnoff)();
                                return(FAIL);
                                return(FAIL);
+                       }
                        /* copy to user directory */
                        if (isdir(filename)) {
                                strcat(filename, "/");
                                strcat(filename, lastpart(W_FILE1));
                        }
                        status = xmv(Dfile, filename);
                        /* copy to user directory */
                        if (isdir(filename)) {
                                strcat(filename, "/");
                                strcat(filename, lastpart(W_FILE1));
                        }
                        status = xmv(Dfile, filename);
-                       WMESG(RQSTCMPT, status ? NO : YES);
+                       WMESG(RQSTCMPT, status ? EM_RMTCP : YES);
                        logent(status ? "FAILED" : "SUCCEEDED", "COPY");
                        logent(status ? "FAILED" : "SUCCEEDED", "COPY");
-                       notify(W_OPTNS, W_USER, filename, Rmtname,
-                         status ? "failed" : "succeeded");
-                       sscanf(&msg[2], "%o", &filemode);
-                       DEBUG(4, "mode - %o\n", filemode);
-                       if (filemode <= 0)
-                               filemode = 0666;
+                       notify(mailopt, W_USER, filename, Rmtname,
+                         status ? EM_LOCCP : YES);
                        if (status == 0) {
                        if (status == 0) {
-                               unlink(Dfile);
-                               filemode |= 0666;
+                               sscanf(&msg[2], "%o", &filemode);
+                               DEBUG(4, "mode - %o\n", filemode);
+                               if (filemode <= 0)
+                                       filemode = 0666;
                                chmod(filename, filemode | 0666);
                        }
                                chmod(filename, filemode | 0666);
                        }
+                       else {
+                               putinpub(filename, Dfile, W_USER);
+                       }
                        goto top;
                }
 
                        goto top;
                }
 
@@ -393,15 +434,15 @@ process:
                }
                strcpy(User, W_USER);
                ASSERT(strlen(User) <= 10, "User - %s\n", User);
                }
                strcpy(User, W_USER);
                ASSERT(strlen(User) <= 10, "User - %s\n", User);
-               if (chkpth("", Rmtname, filename) != 0) {
-                       WMESG(RCVFILE, NO);
+               if (chkpth("", Rmtname, filename) || anyread(filename)) {
+                       WMESG(RCVFILE, EM_RMTACC);
                        logent("DENIED", "PERMISSION");
                        goto top;
                }
                DEBUG(4, "chkpth ok Rmtname - %s\n", Rmtname);
 
                if ((fp = fopen(filename, "r")) == NULL) {
                        logent("DENIED", "PERMISSION");
                        goto top;
                }
                DEBUG(4, "chkpth ok Rmtname - %s\n", Rmtname);
 
                if ((fp = fopen(filename, "r")) == NULL) {
-                       WMESG(RCVFILE, NO);
+                       WMESG(RCVFILE, EM_RMTACC);
                        logent("CAN'T OPEN", "DENIED");
                        goto top;
                }
                        logent("CAN'T OPEN", "DENIED");
                        goto top;
                }
@@ -413,11 +454,14 @@ process:
                WMESG(RCVFILE, msg);
                ret = (*Wrdata)(fp, Ofn);
                fclose(fp);
                WMESG(RCVFILE, msg);
                ret = (*Wrdata)(fp, Ofn);
                fclose(fp);
-               if (ret != 0)
+               if (ret != 0) {
+                       (*Turnoff)();
                        return(FAIL);
                        return(FAIL);
+               }
                RMESG(RQSTCMPT, msg);
                goto process;
        }
                RMESG(RQSTCMPT, msg);
                goto process;
        }
+       (*Turnoff)();
        return(FAIL);
 }
 
        return(FAIL);
 }
 
@@ -469,23 +513,49 @@ char *s, m;
 
 
 /***
 
 
 /***
- *     notify(options, user, file, sys, stwork)        mail results of copy
- *     char *options, *user, *file, *sys, *stword);
+ *     notify          mail results of command
  *
  *     return codes:  none
  */
 
  *
  *     return codes:  none
  */
 
-notify(options, user, file, sys, stword)
-char *options, *user, *file, *sys, *stword;
+notify(mailopt, user, file, sys, msgcode)
+char *user, *file, *sys, *msgcode;
 {
        char str[200];
 {
        char str[200];
-       if (index(options, 'm') == NULL)
+       int i;
+       char *msg;
+
+       if (!mailopt && *msgcode == 'Y')
                return;
                return;
-       sprintf(str, "file %s, system %s, copy %s\n", file, sys, stword);
+       if (*msgcode == 'Y')
+               msg = "copy succeeded";
+       else {
+               i = atoi(msgcode + 1);
+               if (i < 1 || i > EM_MAX)
+                       i = 0;
+               msg = Em_msg[i];
+       }
+       sprintf(str, "file %s, system %s\n%s\n",
+               file, sys, msg);
        mailst(user, str);
        return;
 }
 
        mailst(user, str);
        return;
 }
 
+/***
+ *     lnotify(user, file, mesg)       - local notify
+ *
+ *     return code - none
+ */
+
+lnotify(user, file, mesg)
+char *user, *file, *mesg;
+{
+       char mbuf[200];
+       sprintf(mbuf, "file %s on %s\n%s\n", file, Myname, mesg);
+       mailst(user, mbuf);
+       return;
+}
+
 
 /***
  *     startup(role)
 
 /***
  *     startup(role)
@@ -527,7 +597,7 @@ int role;
        else {
                WMESG(SLTPTCL, blptcl(str));
                RMESG(USEPTCL, msg);
        else {
                WMESG(SLTPTCL, blptcl(str));
                RMESG(USEPTCL, msg);
-               if (msg[1] == N) {
+               if (msg[1] == 'N') {
                        return(FAIL);
                }
 
                        return(FAIL);
                }
 
@@ -630,3 +700,39 @@ char *c;
        DEBUG(4, "Proto start-fail %c\n", *c);
        return(FAIL);
 }
        DEBUG(4, "Proto start-fail %c\n", *c);
        return(FAIL);
 }
+
+/***
+ *     putinpub        put file in public place
+ *
+ *     return code  0 | FAIL
+ */
+
+putinpub(file, tmp, user)
+char *file, *user, *tmp;
+{
+       char fullname[MAXFULLNAME];
+       char *lastpart();
+
+       sprintf(fullname, "%s/%s/", PUBDIR, user);
+       if (mkdirs(fullname) != 0) {
+               /* can not make directories */
+               return(FAIL);
+       }
+       strcat(fullname, lastpart(file));
+       xmv(tmp, fullname);
+       return(0);
+}
+
+/***
+ *     unlinkdf(file)  - unlink D. file
+ *
+ *     return code - none
+ */
+
+unlinkdf(file)
+char *file;
+{
+       if (strlen(file) > 6)
+               unlink(file);
+       return;
+}