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