1 #ifndef lint 2 static char sccsid[] = "@(#)xsend.c 4.6 05/11/89"; 3 #endif 4 5 #include <sys/param.h> 6 #include <sys/stat.h> 7 #include <sys/dir.h> 8 #include <pwd.h> 9 #include "xmail.h" 10 #include "pathnames.h" 11 12 extern int errno; 13 struct stat stbuf; 14 int uid, destuid; 15 char *myname, *dest, *keyfile[128], line[128]; 16 struct direct *dbuf; 17 char *maildir = _PATH_SECRETMAIL; 18 FILE *kf, *mf; 19 DIR *df; 20 MINT *a[42], *cd[6][128]; 21 MINT *msg; 22 char buf[256], eof; 23 int dbg; 24 extern char *malloc(), *getlogin(); 25 26 main(argc, argv) char **argv; 27 { int i, nmax, len; 28 char *p; 29 long now; 30 if(argc != 2) 31 xfatal("mail to exactly one person"); 32 uid = getuid(); 33 p =getlogin(); 34 if(p == NULL) 35 p = getpwuid(uid)->pw_name; 36 myname = malloc(strlen(p)+1); 37 strcpy(myname, p); 38 dest = argv[1]; 39 strcpy(keyfile, maildir); 40 strcat(keyfile, dest); 41 strcat(keyfile, ".key"); 42 if(stat(keyfile, &stbuf) <0) 43 xfatal("addressee not enrolled"); 44 destuid = getpwnam(dest)->pw_uid; 45 if(destuid != stbuf.st_uid) 46 fprintf(stderr, "warning: addressee's key file may be subverted\n"); 47 errno = 0; 48 kf = fopen(keyfile, "r"); 49 if(kf == NULL) 50 xfatal("addressee's key weird"); 51 df = opendir(maildir); 52 if(df == NULL) 53 { perror(maildir); 54 exit(1); 55 } 56 strcpy(line, dest); 57 strcat(line, ".%d"); 58 nmax = -1; 59 while ((dbuf=readdir(df))!=NULL) 60 { if(sscanf(dbuf->d_name, line, &i) != 1) 61 continue; 62 if(i>nmax) nmax = i; 63 } 64 nmax ++; 65 for(i=0; i<10; i++) 66 { sprintf(line, "%s%s.%d", maildir, dest, nmax+i); 67 if(creat(line, 0666) >= 0) break; 68 } 69 if(i==10) xfatal("cannot create mail file"); 70 mf = fopen(line, "w"); 71 init(); 72 time(&now); 73 sprintf(buf, "From %s %s", myname, ctime(&now) ); 74 #ifdef DBG 75 dbg = 1; 76 #endif 77 run(); 78 { 79 char hostname[MAXHOSTNAMELEN]; 80 FILE *nf, *popen(); 81 struct passwd *passp; 82 83 sprintf(buf, "%s %s", _PATH_MAIL, dest); 84 if ((nf = popen(buf, "w")) == NULL) 85 xfatal("cannot pipe to %s", _PATH_MAIL); 86 passp = getpwuid(getuid()); 87 if (passp == 0){ 88 pclose(nf); 89 xfatal("Who are you?"); 90 } 91 gethostname(hostname, sizeof(hostname)); 92 fprintf(nf, "Subject: %s@%s sent you secret mail\n", 93 passp->pw_name, hostname); 94 fprintf(nf, 95 "Your secret mail can be read on host %s using ``xget''.\n", 96 hostname); 97 pclose(nf); 98 } 99 exit(0); 100 } 101 mkcd() 102 { int i, j, k, n; 103 for(i=0; i<42; i++) 104 nin(a[i], kf); 105 fclose(kf); 106 for(i=0; i<6; i++) 107 for(j=0; j<128; j++) 108 for(k=j, n=0; k>0 && n<7; n++, k>>=1) 109 if(k&01) madd(cd[i][j], a[7*i+n], cd[i][j]); 110 } 111 encipher(s) char s[6]; 112 { int i; 113 msub(msg, msg, msg); 114 for(i=0; i<6; i++) 115 madd(msg, cd[i][s[i]&0177], msg); 116 } 117 init() 118 { int i, j; 119 msg = itom(0); 120 for(i=0; i<42; i++) 121 a[i] = itom(0); 122 for(i=0; i<6; i++) 123 for(j=0; j<128; j++) 124 cd[i][j] = itom(0); 125 mkcd(); 126 } 127 run() 128 { char *p; 129 int i, len, eof = 0; 130 for(;;) 131 { len = strlen(buf); 132 for(i=0; i<len/6; i++) 133 { 134 encipher(buf+6*i); 135 nout(msg, mf); 136 } 137 p = buf; 138 for(i *= 6; i<len; i++) 139 *p++ = buf[i]; 140 if(eof) return; 141 fgets(p, sizeof(buf)-6, stdin); 142 if(strcmp(p, ".\n") == 0 || feof(stdin)) 143 { for(i=0; i<6; i++) *p++ = ' '; 144 *p = 0; 145 eof = 1; 146 } 147 } 148 } 149