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