1: #include "xmail.h" 2: #include "sys/types.h" 3: #include "pwd.h" 4: #include "sys/stat.h" 5: #include "sys/dir.h" 6: extern int errno; 7: struct stat stbuf; 8: int uid, destuid; 9: char *myname, *dest, *keyfile[128], line[128]; 10: struct direct dbuf; 11: char *maildir = "/usr/spool/secretmail/"; 12: FILE *kf, *mf, *df; 13: MINT *a[42], *cd[6][128]; 14: MINT *msg; 15: char buf[256], eof; 16: int dbg; 17: main(argc, argv) char **argv; 18: { int i, nmax, len; 19: char *p; 20: long now; 21: if(argc != 2) 22: xfatal("mail to exactly one person"); 23: uid = getuid(); 24: p =getlogin(); 25: if(p == NULL) 26: p = getpwuid(uid)->pw_name; 27: myname = malloc(strlen(p)+1); 28: strcpy(myname, p); 29: dest = argv[1]; 30: strcpy(keyfile, maildir); 31: strcat(keyfile, dest); 32: strcat(keyfile, ".key"); 33: if(stat(keyfile, &stbuf) <0) 34: xfatal("addressee not enrolled"); 35: destuid = getpwnam(dest)->pw_uid; 36: if(destuid != stbuf.st_uid) 37: fprintf(stderr, "warning: addressee's key file may be subverted\n"); 38: errno = 0; 39: kf = fopen(keyfile, "r"); 40: if(kf == NULL) 41: xfatal("addressee's key weird"); 42: df = fopen(maildir, "r"); 43: if(df == NULL) 44: { perror(maildir); 45: exit(1); 46: } 47: strcpy(line, dest); 48: strcat(line, ".%d"); 49: nmax = -1; 50: for(; !feof(df);) 51: { fread(&dbuf, sizeof(dbuf), 1, df); 52: if(dbuf.d_ino == 0) continue; 53: if(sscanf(dbuf.d_name, line, &i) != 1) 54: continue; 55: if(i>nmax) nmax = i; 56: } 57: nmax ++; 58: for(i=0; i<10; i++) 59: { sprintf(line, "%s%s.%d", maildir, dest, nmax+i); 60: if(creat(line, 0666) >= 0) break; 61: } 62: if(i==10) xfatal("cannot create mail file"); 63: mf = fopen(line, "w"); 64: init(); 65: time(&now); 66: sprintf(buf, "From %s %s", myname, ctime(&now) ); 67: #ifdef DBG 68: dbg = 1; 69: #endif 70: run(); 71: sprintf(buf, "mail %s <%snotice", dest, maildir); 72: system(buf); 73: exit(0); 74: } 75: mkcd() 76: { int i, j, k, n; 77: for(i=0; i<42; i++) 78: nin(a[i], kf); 79: fclose(kf); 80: for(i=0; i<6; i++) 81: for(j=0; j<128; j++) 82: for(k=j, n=0; k>0 && n<7; n++, k>>=1) 83: if(k&01) madd(cd[i][j], a[7*i+n], cd[i][j]); 84: } 85: encipher(s) char s[6]; 86: { int i; 87: msub(msg, msg, msg); 88: for(i=0; i<6; i++) 89: madd(msg, cd[i][s[i]&0177], msg); 90: } 91: init() 92: { int i, j; 93: msg = itom(0); 94: for(i=0; i<42; i++) 95: a[i] = itom(0); 96: for(i=0; i<6; i++) 97: for(j=0; j<128; j++) 98: cd[i][j] = itom(0); 99: mkcd(); 100: } 101: run() 102: { char *p; 103: int i, len, eof = 0; 104: for(;;) 105: { len = strlen(buf); 106: for(i=0; i<len/6; i++) 107: { 108: encipher(buf+6*i); 109: nout(msg, mf); 110: } 111: p = buf; 112: for(i *= 6; i<len; i++) 113: *p++ = buf[i]; 114: if(eof) return; 115: fgets(p, sizeof(buf)-6, stdin); 116: if(strcmp(p, ".\n") == 0 || feof(stdin)) 117: { for(i=0; i<6; i++) *p++ = ' '; 118: *p = 0; 119: eof = 1; 120: } 121: } 122: }