1: #ifndef lint 2: static char sccsid[] = "@(#)uucp.c 5.1 (Berkeley) 7/2/83"; 3: #endif 4: 5: #include "uucp.h" 6: #include <sys/types.h> 7: #include <sys/stat.h> 8: 9: /* 10: * uucp 11: */ 12: 13: int Uid; 14: char *Ropt = " "; 15: char Path[100], Optns[10], Ename[8]; 16: char Grade = 'n'; 17: int Copy = 1; 18: char Nuser[32]; 19: 20: /* variables used to check if talking to more than one system. */ 21: int xsflag = -1; 22: char xsys[8] = 0; 23: 24: #define MAXCOUNT 20 /* maximun number of commands per C. file */ 25: 26: 27: main(argc, argv) 28: char *argv[]; 29: { 30: int ret; 31: register char *sysfile1, *sysfl2, *cp; 32: char file1[MAXFULLNAME], file2[MAXFULLNAME]; 33: int orig_uid = getuid(); 34: 35: strcpy(Progname, "uucp"); 36: uucpname(Myname); 37: umask(WFMASK); 38: Optns[0] = '-'; 39: Optns[1] = 'd'; 40: Optns[2] = 'C'; 41: Ename[0] = Nuser[0] = Optns[3] = '\0'; 42: while(argc>1 && argv[1][0] == '-'){ 43: switch(argv[1][1]){ 44: case 'C': 45: Copy = 1; 46: Optns[2] = 'C'; 47: break; 48: case 'c': 49: Copy = 0; 50: Optns[2] = 'c'; 51: break; 52: case 'd': 53: break; 54: case 'f': 55: Optns[1] = 'f'; 56: break; 57: case 'e': 58: sprintf(Ename, "%.7s", &argv[1][2]); 59: break; 60: case 'g': 61: Grade = argv[1][2]; break; 62: case 'm': 63: strcat(Optns, "m"); 64: break; 65: case 'n': 66: sprintf(Nuser, "%.31s", &argv[1][2]); 67: break; 68: case 'r': 69: Ropt = argv[1]; 70: break; 71: case 's': 72: Spool = &argv[1][2]; break; 73: case 'x': 74: chkdebug(orig_uid); 75: Debug = atoi(&argv[1][2]); 76: if (Debug <= 0) 77: Debug = 1; 78: break; 79: default: 80: printf("unknown flag %s\n", argv[1]); break; 81: } 82: --argc; argv++; 83: } 84: DEBUG(4, "\n\n** %s **\n", "START"); 85: gwd(Wrkdir); 86: subchdir(Spool); 87: 88: Uid = getuid(); 89: ret = guinfo(Uid, User, Path); 90: ASSERT(ret == 0, "CAN NOT FIND UID", "", Uid); 91: DEBUG(4, "UID %d, ", Uid); 92: DEBUG(4, "User %s,", User); 93: DEBUG(4, "Ename (%s) ", Ename); 94: DEBUG(4, "PATH %s\n", Path); 95: if (argc < 3) { 96: fprintf(stderr, "usage uucp from ... to\n"); 97: cleanup(0); 98: } 99: 100: 101: /* set up "to" system and file names */ 102: if ((cp = index(argv[argc - 1], '!')) != NULL) { 103: sysfl2 = argv[argc - 1]; 104: *cp = '\0'; 105: if (*sysfl2 == '\0') 106: sysfl2 = Myname; 107: else 108: sprintf(Rmtname, "%.7s", sysfl2); 109: if (versys(sysfl2) != 0) { 110: fprintf(stderr, "bad system name: %s\n", sysfl2); 111: cleanup(0); 112: } 113: strcpy(file2, cp + 1); 114: } 115: else { 116: sysfl2 = Myname; 117: strcpy(file2, argv[argc - 1]); 118: } 119: if (strlen(sysfl2) > 7) 120: *(sysfl2 + 7) = '\0'; 121: 122: 123: /* do each from argument */ 124: while (argc > 2) { 125: if ((cp = index(argv[1], '!')) != NULL) { 126: sysfile1 = argv[1]; 127: *cp = '\0'; 128: if (strlen(sysfile1) > 7) 129: *(sysfile1 + 7) = '\0'; 130: if (*sysfile1 == '\0') 131: sysfile1 = Myname; 132: else 133: sprintf(Rmtname, "%.7s", sysfile1); 134: if (versys(sysfile1) != 0) { 135: fprintf(stderr, "bad system name: %s\n", sysfile1); 136: cleanup(0); 137: } 138: strcpy(file1, cp + 1); 139: } 140: else { 141: sysfile1 = Myname; 142: strcpy(file1, argv[1]); 143: } 144: DEBUG(4, "file1 - %s\n", file1); 145: copy(sysfile1, file1, sysfl2, file2); 146: --argc; 147: argv++; 148: } 149: 150: clscfile(); 151: if (*Ropt != '-' && xsflag >= 0) 152: xuucico(xsys); 153: cleanup(0); 154: } 155: 156: cleanup(code) 157: int code; 158: { 159: logcls(); 160: rmlock(CNULL); 161: if (code) 162: fprintf(stderr, "uucp failed. code %d\n", code); 163: exit(code); 164: } 165: 166: 167: /*** 168: * copy(s1, f1, s2, f2) generate copy files 169: * char *s1, *f1, *s2, *f2; 170: * 171: * return codes 0 | FAIL 172: */ 173: 174: copy(s1, f1, s2, f2) 175: register char *s1, *f1, *s2, *f2; 176: { 177: int type, statret; 178: struct stat stbuf, stbuf1; 179: char dfile[NAMESIZE]; 180: char file1[MAXFULLNAME], file2[MAXFULLNAME]; 181: FILE *cfp, *gtcfile(); 182: char opts[100]; 183: 184: type = 0; 185: opts[0] = '\0'; 186: strcpy(file1, f1); 187: strcpy(file2, f2); 188: if (strcmp(s1, Myname) != SAME) 189: type = 1; 190: if (strcmp(s2, Myname) != SAME) 191: type += 2; 192: if (type & 01) 193: if ((index(f1, '*') != NULL 194: || index(f1, '?') != NULL 195: || index(f1, '[') != NULL)) 196: type = 4; 197: 198: switch (type) { 199: case 0: 200: /* all work here */ 201: DEBUG(4, "all work here %d\n", type); 202: if (ckexpf(file1)) 203: return(FAIL); 204: if (ckexpf(file2)) 205: return(FAIL); 206: if (stat(subfile(file1), &stbuf) != 0) { 207: fprintf(stderr, "can't get file status %s \n copy failed\n", 208: file1); 209: return(0); 210: } 211: statret = stat(subfile(file2), &stbuf1); 212: if (statret == 0 213: && stbuf.st_ino == stbuf1.st_ino 214: && stbuf.st_dev == stbuf1.st_dev) { 215: fprintf(stderr, "%s %s - same file; can't copy\n", file1, file2); 216: return(0); 217: } 218: if (chkpth(User, "", file1) != 0 219: || chkperm(file2, index(Optns, 'd')) 220: || chkpth(User, "", file2) != 0) { 221: fprintf(stderr, "permission denied\n"); 222: cleanup(1); 223: } 224: if ((stbuf.st_mode & ANYREAD) == 0) { 225: fprintf(stderr, "can't read file (%s) mode (%o)\n", 226: file1, stbuf.st_mode); 227: return(FAIL); 228: } 229: if (statret == 0 && (stbuf1.st_mode & ANYWRITE) == 0) { 230: fprintf(stderr, "can't write file (%s) mode (%o)\n", 231: file2, stbuf.st_mode); 232: return(FAIL); 233: } 234: xcp(file1, file2); 235: logent("WORK HERE", "DONE"); 236: return(0); 237: case 1: 238: /* receive file */ 239: DEBUG(4, "receive file - %d\n", type); 240: chsys(s1); 241: if (file1[0] != '~') 242: if (ckexpf(file1)) 243: return(FAIL); 244: if (ckexpf(file2)) 245: return(FAIL); 246: if (chkpth(User, "", file2) != 0) { 247: fprintf(stderr, "permission denied\n"); 248: return(FAIL); 249: } 250: if (Ename[0] != '\0') { 251: /* execute uux - remote uucp */ 252: xuux(Ename, s1, file1, s2, file2, opts); 253: return(0); 254: } 255: 256: cfp = gtcfile(s1); 257: fprintf(cfp, "R %s %s %s %s\n", file1, file2, User, Optns); 258: break; 259: case 2: 260: /* send file */ 261: if (ckexpf(file1)) 262: return(FAIL); 263: if (file2[0] != '~') 264: if (ckexpf(file2)) 265: return(FAIL); 266: DEBUG(4, "send file - %d\n", type); 267: chsys(s2); 268: 269: if (chkpth(User, "", file1) != 0) { 270: fprintf(stderr, "permission denied %s\n", file1); 271: return(FAIL); 272: } 273: if (stat(subfile(file1), &stbuf) != 0) { 274: fprintf(stderr, "can't get status for file %s\n", file1); 275: return(FAIL); 276: } 277: if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 278: fprintf(stderr, "directory name illegal - %s\n", 279: file1); 280: return(FAIL); 281: } 282: if ((stbuf.st_mode & ANYREAD) == 0) { 283: fprintf(stderr, "can't read file (%s) mode (%o)\n", 284: file1, stbuf.st_mode); 285: return(FAIL); 286: } 287: if ((Nuser[0] != '\0') && (index(Optns, 'n') == NULL)) 288: strcat(Optns, "n"); 289: if (Ename[0] != '\0') { 290: /* execute uux - remote uucp */ 291: if (Nuser[0] != '\0') 292: sprintf(opts, "-n%s", Nuser); 293: xuux(Ename, s1, file1, s2, file2, opts); 294: return(0); 295: } 296: if (Copy) { 297: gename(DATAPRE, s2, Grade, dfile); 298: if (xcp(file1, dfile) != 0) { 299: fprintf(stderr, "can't copy %s\n", file1); 300: return(FAIL); 301: } 302: } 303: else { 304: /* make a dummy D. name */ 305: /* cntrl.c knows names < 6 chars are dummy D. files */ 306: strcpy(dfile, "D.0"); 307: } 308: cfp = gtcfile(s2); 309: fprintf(cfp, "S %s %s %s %s %s %o %s\n", file1, file2, 310: User, Optns, dfile, stbuf.st_mode & 0777, Nuser); 311: break; 312: case 3: 313: case 4: 314: /* send uucp command for execution on s1 */ 315: DEBUG(4, "send uucp command - %d\n", type); 316: chsys(s1); 317: if (strcmp(s2, Myname) == SAME) { 318: if (ckexpf(file2)) 319: return(FAIL); 320: if (chkpth(User, "", file2) != 0) { 321: fprintf(stderr, "permission denied\n"); 322: return(FAIL); 323: } 324: } 325: if (Ename[0] != '\0') { 326: /* execute uux - remote uucp */ 327: xuux(Ename, s1, file1, s2, file2, opts); 328: return(0); 329: } 330: cfp = gtcfile(s1); 331: fprintf(cfp, "X %s %s!%s %s %s\n", file1, s2, file2, User, Optns); 332: break; 333: } 334: return(0); 335: } 336: 337: /*** 338: * xuux(ename, s1, s2, f1, f2, opts) execute uux for remote uucp 339: * 340: * return code - none 341: */ 342: 343: xuux(ename, s1, f1, s2, f2, opts) 344: char *ename, *s1, *s2, *f1, *f2, *opts; 345: { 346: char cmd[200]; 347: 348: DEBUG(4, "Ropt(%s) ", Ropt); 349: DEBUG(4, "ename(%s) ", ename); 350: DEBUG(4, "s1(%s) ", s1); 351: DEBUG(4, "f1(%s) ", f1); 352: DEBUG(4, "s2(%s) ", s2); 353: DEBUG(4, "f2(%s)\n", f2); 354: sprintf(cmd, "uux %s %s!uucp %s %s!%s \\(%s!%s\\)", 355: Ropt, ename, opts, s1, f1, s2, f2); 356: DEBUG(4, "cmd (%s)\n", cmd); 357: system(cmd); 358: return; 359: } 360: 361: FILE *Cfp = NULL; 362: char Cfile[NAMESIZE]; 363: 364: /*** 365: * gtcfile(sys) - get a Cfile descriptor 366: * 367: * return an open file descriptor 368: */ 369: 370: FILE * 371: gtcfile(sys) 372: register char *sys; 373: { 374: static char presys[8] = ""; 375: static int cmdcount = 0; 376: register int savemask; 377: 378: if (strcmp(presys, sys) != SAME /* this is !SAME on first call */ 379: || ++cmdcount > MAXCOUNT) { 380: 381: cmdcount = 1; 382: if (presys[0] != '\0') { 383: clscfile(); 384: } 385: gename(CMDPRE, sys, Grade, Cfile); 386: savemask = umask(~0200); 387: Cfp = fopen(subfile(Cfile), "w"); 388: umask(savemask); 389: ASSERT(Cfp != NULL, "CAN'T OPEN", Cfile, 0); 390: strcpy(presys, sys); 391: } 392: return(Cfp); 393: } 394: 395: /*** 396: * clscfile() - close cfile 397: * 398: * return code - none 399: */ 400: 401: clscfile() 402: { 403: if (Cfp == NULL) 404: return; 405: fclose(Cfp); 406: chmod(subfile(Cfile), ~WFMASK & 0777); 407: logent(Cfile, "QUE'D"); 408: Cfp = NULL; 409: return; 410: } 411: 412: /**** 413: * 414: * chsys(s1) compile a list of all systems we are referencing 415: * char *s1 416: * 417: * no return code -- sets up the xsys array. 418: * Author: mcnc!swd, Stephen Daniel 419: */ 420: 421: chsys(s1) 422: register char *s1; 423: { 424: if (xsflag < 0) 425: xsflag = 0; 426: else if (xsflag > 0) 427: return; 428: 429: if (xsys[0] == '\0') { 430: strncpy(xsys, s1, 7); 431: return; 432: } 433: 434: if (strcmp(xsys, s1) == SAME) 435: return; 436: 437: xsflag++; 438: xsys[0] = '\0'; 439: }