xref: /original-bsd/usr.bin/xsend/xsend/xsend.c (revision 0f30d223)
1 #ifndef lint
2 static char sccsid[] = "@(#)xsend.c	4.5 02/14/84";
3 #endif
4 
5 #include "xmail.h"
6 #include <sys/types.h>
7 #include <pwd.h>
8 #include <sys/stat.h>
9 #include <sys/dir.h>
10 extern int errno;
11 struct stat stbuf;
12 int uid, destuid;
13 char *myname, *dest, *keyfile[128], line[128];
14 struct direct *dbuf;
15 char *maildir = "/usr/spool/secretmail/";
16 FILE *kf, *mf;
17 DIR *df;
18 MINT *a[42], *cd[6][128];
19 MINT *msg;
20 char buf[256], eof;
21 int dbg;
22 extern char *malloc(), *getlogin();
23 
24 main(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");
49 	df = opendir(maildir);
50 	if(df == NULL)
51 	{	perror(maildir);
52 		exit(1);
53 	}
54 	strcpy(line, dest);
55 	strcat(line, ".%d");
56 	nmax = -1;
57 	while ((dbuf=readdir(df))!=NULL)
58 	{	if(sscanf(dbuf->d_name, line, &i) != 1)
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();
76 	{
77 		char	hostname[32];
78 		FILE	*nf, *popen();
79 		struct	passwd	*passp;
80 
81 		sprintf(buf, "/bin/mail %s", dest);
82 		if ((nf = popen(buf, "w")) == NULL)
83 			xfatal("cannot pipe to /bin/mail");
84 		passp = getpwuid(getuid());
85 		if (passp == 0){
86 			pclose(nf);
87 			xfatal("Who are you?");
88 		}
89 		gethostname(hostname, sizeof(hostname));
90 		fprintf(nf, "Subject: %s@%s sent you secret mail\n",
91 			passp->pw_name, hostname);
92 		fprintf(nf,
93 		 "Your secret mail can be read on host %s using ``xget''.\n",
94 			hostname);
95 		pclose(nf);
96 	}
97 	exit(0);
98 }
99 mkcd()
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 }
109 encipher(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 }
115 init()
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 }
125 run()
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 }
147