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