1 |
/* (C) Copyright 1993,1994 by Carnegie Mellon University |
2 |
* All Rights Reserved. |
3 |
* |
4 |
* Permission to use, copy, modify, distribute, and sell this software |
5 |
* and its documentation for any purpose is hereby granted without |
6 |
* fee, provided that the above copyright notice appear in all copies |
7 |
* and that both that copyright notice and this permission notice |
8 |
* appear in supporting documentation, and that the name of Carnegie |
9 |
* Mellon University not be used in advertising or publicity |
10 |
* pertaining to distribution of the software without specific, |
11 |
* written prior permission. Carnegie Mellon University makes no |
12 |
* representations about the suitability of this software for any |
13 |
* purpose. It is provided "as is" without express or implied |
14 |
* warranty. |
15 |
* |
16 |
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO |
17 |
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
18 |
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE |
19 |
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
20 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
21 |
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
22 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
23 |
* SOFTWARE. |
24 |
*/ |
25 |
#include <stdio.h> |
26 |
#include <stdlib.h> |
27 |
#include <string.h> |
28 |
#include <errno.h> |
29 |
#ifdef HAVE_GETOPT_H |
30 |
#include <getopt.h> |
31 |
#endif |
32 |
#include <sys/types.h> |
33 |
#include <sys/wait.h> |
34 |
#include <unistd.h> |
35 |
#include "common.h" |
36 |
#include "version.h" |
37 |
#include "xmalloc.h" |
38 |
|
39 |
#define MAXADDRESS 100 |
40 |
|
41 |
extern int attachment; |
42 |
|
43 |
void usage(void); |
44 |
void sendmail(FILE *infile, char **addr, int start); |
45 |
void inews(FILE *infile); |
46 |
void os_perror(char *str); |
47 |
int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile, |
48 |
char *subject, char *headers, long int maxsize, |
49 |
char *typeoverride, char *outfname); |
50 |
|
51 |
int main(int argc, char **argv) |
52 |
{ |
53 |
int opt; |
54 |
char *fname = 0; |
55 |
char *subject = 0; |
56 |
char *descfname = 0; |
57 |
long maxsize = 0; |
58 |
char *outfname = 0; |
59 |
char *newsgroups = 0; |
60 |
char *ctype = 0; |
61 |
char *headers = 0; |
62 |
int i; |
63 |
char *p; |
64 |
char sbuf[1024]; |
65 |
char fnamebuf[4096]; |
66 |
int part; |
67 |
FILE *infile; |
68 |
FILE *descfile = 0; |
69 |
|
70 |
if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') { |
71 |
maxsize = atoi(p); |
72 |
} |
73 |
|
74 |
while ((opt = getopt(argc, argv, "as:d:m:c:o:n:")) != EOF) { |
75 |
switch (opt) { |
76 |
case 's': |
77 |
subject = optarg; |
78 |
break; |
79 |
|
80 |
case 'd': |
81 |
descfname = optarg; |
82 |
break; |
83 |
|
84 |
case 'm': |
85 |
maxsize = atoi(optarg); |
86 |
break; |
87 |
|
88 |
case 'c': |
89 |
ctype = optarg; |
90 |
break; |
91 |
|
92 |
case 'o': |
93 |
outfname = optarg; |
94 |
break; |
95 |
|
96 |
case 'n': |
97 |
newsgroups = optarg; |
98 |
break; |
99 |
|
100 |
case 'a': |
101 |
attachment = 1; |
102 |
break; |
103 |
|
104 |
default: |
105 |
usage(); |
106 |
|
107 |
} |
108 |
} |
109 |
|
110 |
if (ctype) { |
111 |
if (!strncasecmp(ctype, "text/", 5)) { |
112 |
fprintf(stderr, "This program is not appropriate for encoding textual data\n"); |
113 |
exit(1); |
114 |
} |
115 |
if (strncasecmp(ctype, "application/", 12) && strncasecmp(ctype, "audio/", 6) && |
116 |
strncasecmp(ctype, "image/", 6) && strncasecmp(ctype, "video/", 6)) { |
117 |
fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n"); |
118 |
exit(1); |
119 |
} |
120 |
} |
121 |
|
122 |
if (optind == argc) { |
123 |
fprintf(stderr, "An input file must be specified\n"); |
124 |
usage(); |
125 |
} |
126 |
fname = argv[optind++]; |
127 |
|
128 |
/* Must have exactly one of -o, -n, or destination addrs */ |
129 |
if (optind == argc) { |
130 |
if (outfname && newsgroups) { |
131 |
fprintf(stderr, "The -o and -n switches are mutually exclusive.\n"); |
132 |
usage(); |
133 |
} |
134 |
if (!outfname && !newsgroups) { |
135 |
fprintf(stderr, "Either an address or one of the -o or -n switches is required\n"); |
136 |
usage(); |
137 |
} |
138 |
if (newsgroups) { |
139 |
headers = xmalloc(strlen(newsgroups) + 25); |
140 |
sprintf(headers, "Newsgroups: %s\n", newsgroups); |
141 |
} |
142 |
} |
143 |
else { |
144 |
if (outfname) { |
145 |
fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n"); |
146 |
usage(); |
147 |
} |
148 |
if (newsgroups) { |
149 |
fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n"); |
150 |
usage(); |
151 |
} |
152 |
headers = xmalloc(strlen(argv[optind]) + 25); |
153 |
sprintf(headers, "To: %s", argv[optind]); |
154 |
for (i = optind+1; i < argc; i++) { |
155 |
headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25); |
156 |
strcat(headers, ",\n\t"); |
157 |
strcat(headers, argv[i]); |
158 |
} |
159 |
strcat(headers, "\n"); |
160 |
} |
161 |
|
162 |
if (!subject) { |
163 |
fputs("Subject: ", stdout); |
164 |
fflush(stdout); |
165 |
if (!fgets(sbuf, sizeof(sbuf), stdin)) { |
166 |
fprintf(stderr, "A subject is required\n"); |
167 |
usage(); |
168 |
} |
169 |
if ((p = strchr(sbuf, '\n'))) *p = '\0'; |
170 |
subject = sbuf; |
171 |
} |
172 |
|
173 |
if (!outfname) { |
174 |
if (getenv("TMPDIR")) { |
175 |
strcpy(fnamebuf, getenv("TMPDIR")); |
176 |
} |
177 |
else { |
178 |
strcpy(fnamebuf, "/var/tmp"); |
179 |
} |
180 |
strcat(fnamebuf, "/mpackXXXXXX"); |
181 |
mktemp(fnamebuf); |
182 |
outfname = strsave(fnamebuf); |
183 |
} |
184 |
|
185 |
infile = fopen(fname, "r"); |
186 |
if (!infile) { |
187 |
os_perror(fname); |
188 |
exit(1); |
189 |
} |
190 |
|
191 |
if (descfname) { |
192 |
descfile = fopen(descfname, "r"); |
193 |
if (!descfile) { |
194 |
os_perror(descfname); |
195 |
exit(1); |
196 |
} |
197 |
} |
198 |
|
199 |
if (encode(infile, (FILE *)0, fname, descfile, subject, headers, |
200 |
maxsize, ctype, outfname)) exit(1); |
201 |
|
202 |
if (optind < argc || newsgroups) { |
203 |
for (part = 0;;part++) { |
204 |
sprintf(fnamebuf, "%s.%02d", outfname, part); |
205 |
infile = fopen(part ? fnamebuf : outfname, "r"); |
206 |
if (!infile) { |
207 |
if (part) break; |
208 |
continue; |
209 |
} |
210 |
if (newsgroups) { |
211 |
inews(infile); |
212 |
} |
213 |
else { |
214 |
sendmail(infile, argv, optind); |
215 |
} |
216 |
fclose(infile); |
217 |
remove(part ? fnamebuf : outfname); |
218 |
} |
219 |
} |
220 |
|
221 |
exit(0); |
222 |
} |
223 |
|
224 |
void usage(void) |
225 |
{ |
226 |
fprintf(stderr, "mpack version %s\n", MPACK_VERSION); |
227 |
fprintf(stderr, |
228 |
"usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n"); |
229 |
fprintf(stderr, |
230 |
" mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n"); |
231 |
fprintf(stderr, |
232 |
" mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n"); |
233 |
exit(1); |
234 |
} |
235 |
|
236 |
void sendmail(FILE *infile, char **addr, int start) |
237 |
{ |
238 |
int status; |
239 |
int pid; |
240 |
|
241 |
if (start < 2) abort(); |
242 |
|
243 |
#ifdef SCO |
244 |
addr[--start] = "execmail"; |
245 |
#else |
246 |
addr[--start] = "-oi"; |
247 |
addr[--start] = "sendmail"; |
248 |
#endif |
249 |
|
250 |
do { |
251 |
pid = fork(); |
252 |
} while (pid == -1 && errno == EAGAIN); |
253 |
|
254 |
if (pid == -1) { |
255 |
perror("fork"); |
256 |
return; |
257 |
} |
258 |
if (pid != 0) { |
259 |
while (pid != wait(&status)); |
260 |
return; |
261 |
} |
262 |
|
263 |
dup2(fileno(infile), 0); |
264 |
fclose(infile); |
265 |
#ifdef SCO |
266 |
execv("/usr/lib/mail/execmail", addr+start); |
267 |
#else |
268 |
execv("/usr/lib/sendmail", addr+start); |
269 |
execv("/usr/sbin/sendmail", addr+start); |
270 |
#endif |
271 |
perror("execv"); |
272 |
_exit(1); |
273 |
} |
274 |
|
275 |
void inews(FILE *infile) |
276 |
{ |
277 |
int status; |
278 |
int pid; |
279 |
|
280 |
do { |
281 |
pid = fork(); |
282 |
} while (pid == -1 && errno == EAGAIN); |
283 |
|
284 |
if (pid == -1) { |
285 |
perror("fork"); |
286 |
return; |
287 |
} |
288 |
if (pid != 0) { |
289 |
while (pid != wait(&status)); |
290 |
return; |
291 |
} |
292 |
|
293 |
dup2(fileno(infile), 0); |
294 |
fclose(infile); |
295 |
execlp("inews", "inews", "-h", "-S", (char *)0); |
296 |
execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0); |
297 |
execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0); |
298 |
execl("/etc/inews", "inews", "-h", "-S", (char *)0); |
299 |
execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0); |
300 |
execl("/usr/news/inews", "inews", "-h", "-S", (char *)0); |
301 |
execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0); |
302 |
perror("execl"); |
303 |
_exit(1); |
304 |
} |
305 |
|
306 |
void warn(void) |
307 |
{ |
308 |
abort(); |
309 |
} |