111788Ssam #ifndef lint 2*12387Smckusick static char sccsid[] = "@(#)xget.c 4.2 05/11/83"; 311788Ssam #endif 411788Ssam 511788Ssam #include "xmail.h" 611788Ssam #include "sys/types.h" 7*12387Smckusick #include "dir.h" 811788Ssam #include "ctype.h" 911788Ssam #include "pwd.h" 1011788Ssam #include "sys/stat.h" 1111788Ssam char *myname; 1211788Ssam int uid; 13*12387Smckusick struct direct *dbuf; 1411788Ssam char *maildir = "/usr/spool/secretmail/"; 15*12387Smckusick FILE *kf, *mf; 16*12387Smckusick DIR *df; 1711788Ssam MINT *x, *b, *one, *t45, *z, *q, *r; 1811788Ssam MINT *two, *t15, *mbuf; 1911788Ssam char buf[256], line[128]; 2011788Ssam #define MXF 100 2111788Ssam int fnum[MXF], fcnt; 2211788Ssam struct stat stbuf; 2311788Ssam main() 2411788Ssam { int i; 2511788Ssam char *p; 26*12387Smckusick 2711788Ssam uid = getuid(); 28*12387Smckusick myname = (char *)getlogin(); 2911788Ssam if(myname == NULL) 3011788Ssam myname = getpwuid(uid)->pw_name; 3111788Ssam comminit(); 3211788Ssam mbuf = itom(0); 3311788Ssam files(); 3411788Ssam setup(getpass("Key: ")); 3511788Ssam mkb(); 3611788Ssam mkx(); 3711788Ssam #ifndef debug 3811788Ssam invert(x, b, x); 3911788Ssam #else 4011788Ssam invert(x, b, z); 4111788Ssam mult(x, z, z); 4211788Ssam mdiv(z, b, q, z); 4311788Ssam omout(z); 4411788Ssam invert(x, b, x); 4511788Ssam #endif 4611788Ssam for(i=0; i<fcnt; i++) 4711788Ssam { sprintf(line, "%s%s.%d", maildir, myname, fnum[i]); 4811788Ssam if(stat(line, &stbuf)<0) 4911788Ssam { perror(line); 5011788Ssam continue; 5111788Ssam } 5211788Ssam if(stbuf.st_size == 0) 5311788Ssam { printf("zero length mail file\n"); 5411788Ssam unlink(line); 5511788Ssam continue; 5611788Ssam } 5711788Ssam if((mf = fopen(line, "r"))==NULL) 5811788Ssam { perror(line); 5911788Ssam continue; 6011788Ssam } 6111788Ssam decipher(mf, stdout); 6211788Ssam cmnd: 6311788Ssam printf("? "); 6411788Ssam fgets(buf, sizeof(buf), stdin); 6511788Ssam if(feof(stdin)) exit(0); 6611788Ssam switch(buf[0]) 6711788Ssam { 6811788Ssam case 'q': 6911788Ssam exit(0); 7011788Ssam case 'n': 7111788Ssam case 'd': 7211788Ssam case '\n': 7311788Ssam unlink(line); 7411788Ssam fclose(mf); 7511788Ssam break; 7611788Ssam case '!': 7711788Ssam system(buf+1); 7811788Ssam printf("!\n"); 7911788Ssam goto cmnd; 8011788Ssam case 's': 8111788Ssam case 'w': 8211788Ssam rewind(mf); 8311788Ssam if(buf[1] == '\n' || buf[1] == '\0') 8411788Ssam strcpy(buf, "s mbox\n"); 8511788Ssam for(p=buf; !isspace(*p); p++); 8611788Ssam for(; isspace(*p); p++); 8711788Ssam p[strlen(p)-1] = 0; 8811788Ssam kf = fopen(p, "a"); 8911788Ssam if(kf == NULL) 9011788Ssam { perror(p); 9111788Ssam break; 9211788Ssam } 9311788Ssam decipher(mf, kf); 9411788Ssam fclose(mf); 9511788Ssam fclose(kf); 9611788Ssam unlink(line); 9711788Ssam break; 9811788Ssam } 9911788Ssam } 10011788Ssam exit(0); 10111788Ssam } 10211788Ssam icmp(a, b) int *a, *b; 10311788Ssam { 10411788Ssam return(*a - *b); 10511788Ssam } 10611788Ssam files() 10711788Ssam { int i; 108*12387Smckusick if((df = opendir(maildir)) == NULL) 10911788Ssam { perror(maildir); 11011788Ssam exit(1); 11111788Ssam } 11211788Ssam strcpy(line, myname); 11311788Ssam strcat(line, ".%d"); 114*12387Smckusick while ((dbuf = readdir(df)) != NULL) 115*12387Smckusick { if(sscanf(dbuf->d_name, line, &i) != 1) 11611788Ssam continue; 11711788Ssam if(fcnt >= MXF) 11811788Ssam break; 11911788Ssam fnum[fcnt++] = i; 12011788Ssam } 121*12387Smckusick closedir(df); 12211788Ssam if(fcnt == 0) 12311788Ssam { printf("no secret mail\n"); 12411788Ssam exit(0); 12511788Ssam } 12611788Ssam qsort(fnum, fcnt, sizeof(int), icmp); 12711788Ssam } 12811788Ssam decipher(u, w) FILE *u, *w; 12911788Ssam { int i; 13011788Ssam short a; 13111788Ssam for(;;) 13211788Ssam { nin(mbuf, u); 13311788Ssam if(feof(u)) break; 13411788Ssam mult(mbuf, x, mbuf); 13511788Ssam mdiv(mbuf, b, q, mbuf); 13611788Ssam for(i=1; i<=3; i++) 13711788Ssam { a = mbuf->val[i]; 13811788Ssam putc(a&0177, w); 13911788Ssam a >>= 8; 14011788Ssam putc(a&0177, w); 14111788Ssam } 14211788Ssam } 14311788Ssam } 144