|
|
1.1 root 1: /*
2: * smtpqer -- put mail (on stdin) in an smtpq subdirectory, with a shell script
3: * to send it via smtp
4: */
5:
6: #define USAGE "usage: smtpqer [-n] [-H helohost] [-d domain] [-K] [-a toaddr] [-u] from tohost to...\n"
7: #define DIRDOMLEVEL 2
8: #define SPOOLNAMSIZ 12 /* keep spool dir names no longer (12 allows L.name on v9) */
9: #include <stdio.h>
10: #include <signal.h>
11: #include "string.h"
12: #include "mail.h"
13: #include <ctype.h>
14: #include <sys/stat.h>
15:
16: /* globals */
17: char datafile[1024];
18: int debug = 0;
19:
20: void bomb();
21:
22: /* imports (other than in .h's) */
23: extern spoolsubdir[];
24: extern void exit(), cleanlocks();
25: extern FILE* lockopen();
26: extern char *sysname_read();
27:
28: /* interrupt handling */
29: SIGRETURN
30: catchhup(s)
31: int s;
32: {
33: bomb("interrupted: HUP\n");
34: }
35:
36: /* interrupt handling */
37: SIGRETURN
38: catchint(s)
39: int s;
40: {
41: bomb("interrupted: INT\n");
42: }
43:
44: main(argc, argv)
45: int argc;
46: char *argv[];
47: {
48: register int c;
49: int errflg=0;
50: extern int optind;
51: extern char *optarg;
52: char *p;
53: char *prefix;
54: char *domain=0;
55: char *sys=0;
56: int unixformat=0;
57: int gateway=0;
58: int helo=0;
59: int norun=0;
60: char *helohost=0;
61: char *gatehost=0;
62: char *toaddr=0;
63: char *from=0;
64: char *host=0;
65: char *spooldir;
66: string *to=s_new();
67:
68: umask(2);
69:
70: signal(SIGHUP, SIG_IGN);
71: signal(SIGINT, catchint);
72: while ((c=getopt(argc, argv, "H:d:ua:nD")) != EOF)
73: switch(c) {
74: case 'H': helohost=optarg; break;
75: case 'd': domain=optarg; break;
76: case 'u': unixformat++; break;
77: case 'a': toaddr=optarg; break;
78: case 'n': norun++; break;
79: case 'D': debug++; break;
80: case '?':
81: default:
82: errflg++; break;
83: }
84: if (errflg || (argc - optind) < 3)
85: bomb(USAGE);
86: if(*argv[optind]==0 || *argv[optind+1]==0 || *argv[optind+2]==0)
87: bomb(USAGE);
88: if (helohost==NULL)
89: helohost=s_to_c(s_copy(sysname_read()));
90:
91: from=argv[optind++];
92: host=argv[optind++];
93: for (; optind < argc; optind++){
94: s_append(to, argv[optind]);
95: s_append(to, " ");
96: }
97:
98: /*
99: * make spool files:
100: * C.xxxxxxxxxxxx - the control file
101: * D.xxxxxxxxxxxx - the data file
102: */
103: if(gotodir(host)<0)
104: bomb("going to spool directory %s\n", spoolsubdir);
105: makedata(from, domain, s_to_c(to));
106: makectl(unixformat, helohost, domain, from, toaddr, host, s_to_c(to));
107:
108: /*
109: * run the queue for the receiver
110: */
111: if(!norun)
112: smtpsched("Qsmtpsched", spoolsubdir);
113:
114: exit(0);
115: }
116:
117: /*
118: * the data file is pre-converted to rfc822
119: */
120: makedata(from, domain, to)
121: char *from;
122: char *domain;
123: char *to;
124: {
125: int fd;
126: FILE *fp;
127:
128: /*
129: * create data file
130: */
131: strcpy(datafile, "D.xxxxxxxxxxxx");
132: fd = mkdatafile(datafile);
133: if(fd<0)
134: bomb("creating spool file\n");
135: fp = fdopen(fd, "w");
136:
137: /*
138: * copy data
139: */
140: clearerr(fp);
141: clearerr(stdin);
142: copymsg(stdin, fp);
143: fflush(fp);
144:
145: /*
146: * make sure it worked
147: */
148: if(ferror(fp) || ferror(stdin)){
149: unlink(datafile);
150: bomb("writing data file");
151: }
152: fclose(fp);
153: }
154:
155: /*
156: * just copy input to output
157: */
158: copymsg(in, out)
159: FILE *in;
160: FILE *out;
161: {
162: char buf[4096];
163: int n;
164:
165: while(n=fread(buf, 1, sizeof(buf), in))
166: if(fwrite(buf, 1, n, out)!=n)
167: bomb("writing data file");
168: }
169:
170: /*
171: * Make a control file. The two lines contain:
172: * <reply-addr> <dest>
173: * -H <hello host> -d <domain> <reply_addr> <dest> <recipients>
174: */
175: makectl(unixformat, helo, domain, from, daddr, dest, to)
176: char *from, *dest, *to;
177: {
178: string *msg = s_new();
179:
180: s_append(msg, from);
181: s_append(msg, " ");
182: s_append(msg, dest);
183: s_append(msg, "\n");
184: if(unixformat)
185: s_append(msg, "-u ");
186: if(domain){
187: s_append(msg, "-d ");
188: s_append(msg, domain);
189: s_append(msg, " ");
190: }
191: if(daddr){
192: s_append(msg, "-a ");
193: s_append(msg, daddr);
194: s_append(msg, " ");
195: }
196: s_append(msg, "-H ");
197: s_append(msg, helo);
198: s_append(msg, " ");
199: s_append(msg, from);
200: s_append(msg, " ");
201: s_append(msg, dest);
202: s_append(msg, " ");
203: s_append(msg, to);
204: s_append(msg, "\n");
205: if(mkctlfile('C', datafile, s_to_c(msg))<0)
206: bomb("creating control file\n");
207: }
208:
209: void
210: bomb(msg, a1, a2, a3, a4)
211: char *msg;
212: {
213: fprintf(stderr, "smtpqer: ");
214: fprintf(stderr, msg, a1, a2, a3, a4);
215: if (datafile[0]!=0)
216: unlink(datafile);
217: exit(1);
218: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.