File: unixpk.c

package info (click to toggle)
mpack 1.6-8.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,144 kB
  • ctags: 1,184
  • sloc: ansic: 8,785; sh: 330; makefile: 75; perl: 25
file content (307 lines) | stat: -rw-r--r-- 7,408 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* (C) Copyright 1993,1994 by Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Carnegie
 * Mellon University not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  Carnegie Mellon University makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#include "version.h"
#include "xmalloc.h"

#define MAXADDRESS 100

extern int attachment;

void usage(void);
void sendmail(FILE *infile, char **addr, int start);
void inews(FILE *infile);
void os_perror(char *str);
int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile,
	   char *subject, char *headers, long int maxsize,
	   char *typeoverride, char *outfname);

int main(int argc, char **argv)
{
    int opt;
    char *fname = 0;
    char *subject = 0;
    char *descfname = 0;
    long maxsize = 0;
    char *outfname = 0;
    char *newsgroups = 0;
    char *ctype = 0;
    char *headers = 0;
    int i;
    char *p;
    char sbuf[1024];
    char fnamebuf[4096];
    int part;
    FILE *infile;
    FILE *descfile = 0;

    if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
	maxsize = atoi(p);
    }

    while ((opt = getopt(argc, argv, "as:d:m:c:o:n:")) != EOF) {
	switch (opt) {
	case 's':
	    subject = optarg;
	    break;

	case 'd':
	    descfname = optarg;
	    break;

	case 'm':
	    maxsize = atoi(optarg);
	    break;

	case 'c':
	    ctype = optarg;
	    break;

	case 'o':
	    outfname = optarg;
	    break;

	case 'n':
	    newsgroups = optarg;
	    break;

	case 'a':
	    attachment = 1;
	    break;

	default:
	    usage();

	}
    }

    if (ctype) {
	if (!strncasecmp(ctype, "text/", 5)) {
	    fprintf(stderr, "This program is not appropriate for encoding textual data\n");
	    exit(1);
	}
	if (strncasecmp(ctype, "application/", 12) && strncasecmp(ctype, "audio/", 6) &&
	    strncasecmp(ctype, "image/", 6) && strncasecmp(ctype, "video/", 6)) {
	    fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
	    exit(1);
	}
    }

    if (optind == argc) {
	fprintf(stderr, "An input file must be specified\n");
	usage();
    }
    fname = argv[optind++];

    /* Must have exactly one of -o, -n, or destination addrs */
    if (optind == argc) {
	if (outfname && newsgroups) {
	    fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
	    usage();
	}
	if (!outfname && !newsgroups) {
	    fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
	    usage();
	}
	if (newsgroups) {
	    headers = xmalloc(strlen(newsgroups) + 25);
	    sprintf(headers, "Newsgroups: %s\n", newsgroups);
	}
    }
    else {
	if (outfname) {
	    fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
	    usage();
	}
	if (newsgroups) {
	    fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
	    usage();
	}
	headers = xmalloc(strlen(argv[optind]) + 25);
	sprintf(headers, "To: %s", argv[optind]);
	for (i = optind+1; i < argc; i++) {
	    headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
	    strcat(headers, ",\n\t");
	    strcat(headers, argv[i]);
	}
	strcat(headers, "\n");
    }

    if (!subject) {
	fputs("Subject: ", stdout);
	fflush(stdout);
	if (!fgets(sbuf, sizeof(sbuf), stdin)) {
	    fprintf(stderr, "A subject is required\n");
	    usage();
	}
	if ((p = strchr(sbuf, '\n'))) *p = '\0';
	subject = sbuf;
    }	

    if (!outfname) {
	if (getenv("TMPDIR")) {
	    strcpy(fnamebuf, getenv("TMPDIR"));
	}
	else {
	    strcpy(fnamebuf, "/var/tmp");
	}
	strcat(fnamebuf, "/mpackXXXXXX");
	mktemp(fnamebuf);
	outfname = strsave(fnamebuf);
    }

    infile = fopen(fname, "r");
    if (!infile) {
	os_perror(fname);
	exit(1);
    }

    if (descfname) {
	descfile = fopen(descfname, "r");
	if (!descfile) {
	    os_perror(descfname);
	    exit(1);
	}
    }

    if (encode(infile, (FILE *)0, fname, descfile, subject, headers,
	       maxsize, ctype, outfname)) exit(1);

    if (optind < argc || newsgroups) {
	for (part = 0;;part++) {
	    sprintf(fnamebuf, "%s.%02d", outfname, part);
	    infile = fopen(part ? fnamebuf : outfname, "r");
	    if (!infile) {
		if (part) break;
		continue;
	    }
	    if (newsgroups) {
		inews(infile);
	    }
	    else {
		sendmail(infile, argv, optind);
	    }
	    fclose(infile);
	    remove(part ? fnamebuf : outfname);
	}
    }

    exit(0);
}

void usage(void)
{
    fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
    fprintf(stderr, 
"usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n");
    fprintf(stderr, 
"       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
    fprintf(stderr, 
"       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n");
    exit(1);
}

void sendmail(FILE *infile, char **addr, int start)
{
    int status;
    int pid;

    if (start < 2) abort();

#ifdef SCO
    addr[--start] = "execmail";
#else
    addr[--start] = "-oi";
    addr[--start] = "sendmail";
#endif

    do {
	pid = fork();
    } while (pid == -1 && errno == EAGAIN);
    
    if (pid == -1) {
	perror("fork");
	return;
    }
    if (pid != 0) {
	while (pid != wait(&status));
	return;
    }

    dup2(fileno(infile), 0);
    fclose(infile);
#ifdef SCO
    execv("/usr/lib/mail/execmail", addr+start);
#else
    execv("/usr/lib/sendmail", addr+start);
    execv("/usr/sbin/sendmail", addr+start);
#endif
    perror("execv");
    _exit(1);
}

void inews(FILE *infile)
{
    int status;
    int pid;

    do {
	pid = fork();
    } while (pid == -1 && errno == EAGAIN);
    
    if (pid == -1) {
	perror("fork");
	return;
    }
    if (pid != 0) {
	while (pid != wait(&status));
	return;
    }

    dup2(fileno(infile), 0);
    fclose(infile);
    execlp("inews", "inews", "-h", "-S", (char *)0);
    execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0);
    execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0);
    execl("/etc/inews", "inews", "-h", "-S", (char *)0);
    execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0);
    execl("/usr/news/inews", "inews", "-h", "-S", (char *)0);
    execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0);
    perror("execl");
    _exit(1);
}

void warn(void)
{
    abort();
}