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