111788Ssam #ifndef lint 2*12565Sralph static char sccsid[] = "@(#)xget.c 4.3 05/19/83"; 311788Ssam #endif 411788Ssam 511788Ssam #include "xmail.h" 611788Ssam #include "sys/types.h" 712387Smckusick #include "dir.h" 811788Ssam #include "ctype.h" 911788Ssam #include "pwd.h" 1011788Ssam #include "sys/stat.h" 1111788Ssam char *myname; 1211788Ssam int uid; 1312387Smckusick struct direct *dbuf; 1411788Ssam char *maildir = "/usr/spool/secretmail/"; 1512387Smckusick FILE *kf, *mf; 1612387Smckusick 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; 2612387Smckusick 2711788Ssam uid = getuid(); 2812387Smckusick 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': 73*12565Sralph fclose(mf); 7411788Ssam unlink(line); 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"); 85*12565Sralph for(p = buf+1; isspace(*p); p++); 8611788Ssam p[strlen(p)-1] = 0; 8711788Ssam kf = fopen(p, "a"); 8811788Ssam if(kf == NULL) 8911788Ssam { perror(p); 90*12565Sralph goto cmnd; 9111788Ssam } 9211788Ssam decipher(mf, kf); 9311788Ssam fclose(mf); 9411788Ssam fclose(kf); 9511788Ssam unlink(line); 9611788Ssam break; 97*12565Sralph default: 98*12565Sralph printf("Commands are:\n"); 99*12565Sralph printf("q quit, leaving unread messages\n"); 100*12565Sralph printf("n delete current message and goto next\n"); 101*12565Sralph printf("d same as above\n"); 102*12565Sralph printf("\\n same as above\n"); 103*12565Sralph printf("! execute shell command\n"); 104*12565Sralph printf("s save message in the named file or mbox\n"); 105*12565Sralph printf("w same as above\n"); 106*12565Sralph printf("? prints this list\n"); 107*12565Sralph goto cmnd; 10811788Ssam } 10911788Ssam } 11011788Ssam exit(0); 11111788Ssam } 11211788Ssam icmp(a, b) int *a, *b; 11311788Ssam { 11411788Ssam return(*a - *b); 11511788Ssam } 11611788Ssam files() 11711788Ssam { int i; 11812387Smckusick if((df = opendir(maildir)) == NULL) 11911788Ssam { perror(maildir); 12011788Ssam exit(1); 12111788Ssam } 12211788Ssam strcpy(line, myname); 12311788Ssam strcat(line, ".%d"); 12412387Smckusick while ((dbuf = readdir(df)) != NULL) 125*12565Sralph { 126*12565Sralph if(sscanf(dbuf->d_name, line, &i) != 1) 12711788Ssam continue; 12811788Ssam if(fcnt >= MXF) 12911788Ssam break; 13011788Ssam fnum[fcnt++] = i; 13111788Ssam } 13212387Smckusick closedir(df); 13311788Ssam if(fcnt == 0) 13411788Ssam { printf("no secret mail\n"); 13511788Ssam exit(0); 13611788Ssam } 13711788Ssam qsort(fnum, fcnt, sizeof(int), icmp); 13811788Ssam } 13911788Ssam decipher(u, w) FILE *u, *w; 14011788Ssam { int i; 14111788Ssam short a; 14211788Ssam for(;;) 14311788Ssam { nin(mbuf, u); 14411788Ssam if(feof(u)) break; 14511788Ssam mult(mbuf, x, mbuf); 14611788Ssam mdiv(mbuf, b, q, mbuf); 14711788Ssam for(i=1; i<=3; i++) 14811788Ssam { a = mbuf->val[i]; 14911788Ssam putc(a&0177, w); 15011788Ssam a >>= 8; 15111788Ssam putc(a&0177, w); 15211788Ssam } 15311788Ssam } 15411788Ssam } 155