File: uucp.c

package info (click to toggle)
deliver 2.1.14-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 492 kB
  • ctags: 914
  • sloc: ansic: 6,049; yacc: 405; makefile: 130; sh: 34
file content (338 lines) | stat: -rw-r--r-- 7,190 bytes parent folder | download | duplicates (3)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* $Id: uucp.c,v 1.3 1992/01/20 20:36:05 chip Exp $
 *
 * Handle mail destined for other hosts via UUCP.
 * Deliver is intended as a very low-level program, so we don't
 * do anything fancy here.  We just hand the message to uux.
 *
 * $Log: uucp.c,v $
 * Revision 1.3  1992/01/20 20:36:05  chip
 * Allow for UUX_OPTS to be a list.
 * Support UUX_DASH_A, so UUCP failure messages are mailed to original sender.
 *
 * Revision 1.2  1991/10/23  20:09:26  chip
 * Use tdup() to duplicate temp file fds.
 *
 * Revision 1.1  1991/05/13  18:36:55  chip
 * Initial revision
 *
 */

#include "deliver.h"

/*
 * Local functions.
 */

static char *find_uux();
static int uucp_copy();

/*
 * Local data.
 */

#ifdef UUX_OPTS
static char *uux_opts[] = { UUX_OPTS };
#define UUX_OPTCOUNT (sizeof(uux_opts) / sizeof(uux_opts[0]))
#else
#define UUX_OPTCOUNT 0
#endif

/*----------------------------------------------------------------------
 * Send mail to UUCP addresses (if any).
 * Return count of UUCP addresses for which delivery was attempted.
 */

int
uucp_deliver()
{
    DEST *d;
    char *uav[UUX_ARGCOUNT + UUX_OPTCOUNT + 8];/* arguments for execv() */
    char **av;			/* remote addresses in uav[] */
    DEST *dv[UUX_ARGCOUNT];	/* destinations in av[] */
    char rmail[UUCP_NAMESIZE + 8];	/* "sysname!rmail" */
    char *uux;
    int uucpcount;

    uux = find_uux();

    av = uav;
    *av++ = "uux";

#ifdef UUX_OPTS
    {
	int i;

	for (i = 0; i < UUX_OPTCOUNT; ++i)
	    *av++ = uux_opts[i];
    }
#endif

#ifdef UUX_DASH_A
    {
	char *s, *p;

	/* Send failure notices to original sender, not local sender. */
	s = orig_sender;

	/* Strip smail-generated "hostname!" unless address contains "@". */
	if (strchr(s, '@') == NULL)
	{
	    size_t hostlen;

	    hostlen = strlen(hostname);
	    while (strncmp(s, hostname, hostlen) == 0 && s[hostlen] == '!')
		s += hostlen + 1;
	}

	/* Generate "-asender" option. */
	p = zalloc(sizeof("-a") + strlen(s));
	strcpy(p, "-a");
	strcat(p, s);
	*av++ = p;
    }
#endif

    *av++ = "-";
    *av++ = rmail;

    /*
     * Look for a UUCP address that is "working".  If we find one,
     * then we scan the rest of the list for other UUCP addresses
     * that begin with the same first hop.  If we find any, then
     * we handle them too.  Note that as we continue scanning,
     * we'll find those same addresses again; that's okay, though,
     * because their status fields will report that they're
     * already done.  Cool, eh?
     */

    uucpcount = 0;

    for (d = first_dest(); d; d = next_dest(d))
    {
	FILE *uux_fp;
	DEST *ud;
	DERROR e;
	char *bang;
	unsigned namesize;
	int argcount, argsize, problem, a;

	if (d->d_class != CL_UUCP || d->d_state != ST_WORKING)
	    continue;

	++uucpcount;

	if (printaddrs)
	    (void) printf("%s\n", d->d_name);

	if (dryrun)
	{
	    d->d_state = ST_DONE;
	    continue;
	}

	/*
	 * This is the first destination with the given system
	 * as the first hop.  Generate the rmail command.
	 */

	if ((bang = strchr(d->d_name, '!')) == NULL
	    || (namesize = bang - d->d_name) > UUCP_NAMESIZE)
	{
	    dest_err(d, E_NSHOST);
	    continue;
	}

	(void) strncpy(rmail, d->d_name, namesize);
	(void) strcpy(rmail + namesize, "!rmail");

	/*
	 * Now keep looking for addresses until a limit is reached,
	 * either max arguments or max argument size.
	 * We'll find them again, but their statuses will prevent us
	 * from trying to mail to them twice.
	 */

	argcount = 0;
	argsize = 0;
	for (ud = d; ud; ud = next_dest(ud))
	{
	    char *rest, *arg;

	    if (ud->d_class != CL_UUCP
		|| ud->d_state != ST_WORKING)
		continue;

	    if (strncmp(ud->d_name, d->d_name, namesize + 1) != 0)
		continue;

	    /*
	     * We have a match!  (Or, it could be the first one.)
	     * Be sure we don't exceed our configured maxima,
	     * except for the first address, which always goes.
	     */

	    rest = ud->d_name + namesize + 1;

	    if (argcount > 0)
	    {
		if (argcount + 1 > UUX_ARGCOUNT
		    || argsize + strlen(rest) + 2 > UUX_ARGSIZE)
		    break;
	    }

	    /*
	     * Generate a uux argument and save the destination.
	     */

	    arg = zalloc((unsigned) 3 + strlen(rest));
	    (void) sprintf(arg, "(%s)", rest);

	    av[argcount] = arg;
	    dv[argcount] = ud;

	    /*
	     * Keep track of arg count and total size.
	     */

	    ++argcount;
	    argsize += strlen(arg);
	}

	av[argcount] = NULL;

	/*
	 * Do the dirty deed.
	 * We have to remember the error code as a variable,
	 * since it may apply to multiple destinations.
	 */

	problem = 0;
	e = E_PIPE;		/* default error */

	if ((uux_fp = ct_fopenv(real_ct, uux, uav, "w")) == NULL)
	    problem = 1;
	else
	{
	    if (uucp_copy(uux_fp) < 0)
		problem = 1;

	    if (ct_fclose(uux_fp))
	    {
		/* "No such host" overrides piping problems. */
		e = E_NSHOST;
		problem = 1;
	    }
	}

	/*
	 * We're done.  Update each destination's status.
	 */

	for (a = 0; a < argcount; ++a)
	{
	    free(av[a]);

	    if (problem)
		dest_err(dv[a], e);
	    else
		dv[a]->d_state = ST_DONE;
	}

	/* Track the correct count of UUCP addresses found. */

	uucpcount += argcount - 1;
    }

    return uucpcount;
}

/*----------------------------------------------------------------------
 * Where is uux?
 * This function is allowed to be wrong; if a user without a
 * uux binary is trying to send UUCP mail, he's got problems.
 */

static char *
find_uux()
{
    static char uux1[] = "/bin/uux";
    static char uux2[] = "/usr/bin/uux";

    return exists(uux1) ? uux1 : uux2;
}

/*----------------------------------------------------------------------
 * Write the message for UUCP transmission to the given file.
 */

static int
uucp_copy(ofp)
FILE *ofp;
{
    FILE *ifp;
    char *p;
    register int c;
    int fd;
    char buf[BUFSIZ];

    if ((fd = tdup(tfd[T_HDR], ttype[T_HDR])) == -1)
	return -1;
    (void) lseek(fd, 0L, 0);
    if ((ifp = fdopen(fd, "r")) == NULL)
    {
	error("can't fdopen %s fd", ttype[T_HDR]);
	(void) close(fd);
	return -1;
    }

    /*
     * Copy the header, but tack "remote from" onto the end of the
     * From_ line.  (If it weren't for dealing with the From_ line,
     * I'd skip stream I/O altogether and use read/write.  Maybe
     * I should save the length of the From_ line when I copy it...)
     */

    (void) fgets(buf, GETSIZE(buf), ifp);
    if ((p = strchr(buf, '\n')) != NULL)
	*p = 0;

    if ((p = skipfrom(buf)) == NULL)
	(void) fputs(buf, ofp);	/* should never happen */
    else
    {
	unsigned hlen;

	hlen = strlen(hostname);
	if (strncmp(p, hostname, hlen) == 0 && *(p + hlen) == '!')
	    p += hlen + 1;
	(void) fputs("From ", ofp);
	(void) fputs(p, ofp);
    }

    (void) fprintf(ofp, " remote from %s\n", hostname);

    while ((c = getc(ifp)) != EOF)
	(void) putc(c, ofp);

    (void) fclose(ifp);

    /*
     * Copy the body
     */

    if ((fd = tdup(tfd[T_BODY], ttype[T_BODY])) == -1)
	return -1;
    (void) lseek(fd, 0L, 0);
    if ((ifp = fdopen(fd, "r")) == NULL)
    {
	error("can't fdopen %s fd", ttype[T_BODY]);
	(void) close(fd);
	return -1;
    }

    while ((c = getc(ifp)) != EOF)
	(void) putc(c, ofp);

    (void) fclose(ifp);
    return 0;
}