xref: /freebsd/usr.bin/mail/util.c (revision 55620f43deef5c0eb5b4b0f675de18b30c8d1c2d)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)aux.c	8.1 (Berkeley) 6/6/93";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/time.h>
39 
40 #include <fcntl.h>
41 
42 #include "rcv.h"
43 #include "extern.h"
44 
45 /*
46  * Mail -- a mail program
47  *
48  * Auxiliary functions.
49  */
50 
51 static char *save2str(char *, char *);
52 
53 /*
54  * Return a pointer to a dynamic copy of the argument.
55  */
56 char *
57 savestr(char *str)
58 {
59 	char *new;
60 	int size = strlen(str) + 1;
61 
62 	if ((new = salloc(size)) != NULL)
63 		bcopy(str, new, size);
64 	return (new);
65 }
66 
67 /*
68  * Make a copy of new argument incorporating old one.
69  */
70 static char *
71 save2str(char *str, char *old)
72 {
73 	char *new;
74 	int newsize = strlen(str) + 1;
75 	int oldsize = old ? strlen(old) + 1 : 0;
76 
77 	if ((new = salloc(newsize + oldsize)) != NULL) {
78 		if (oldsize) {
79 			bcopy(old, new, oldsize);
80 			new[oldsize - 1] = ' ';
81 		}
82 		bcopy(str, new + oldsize, newsize);
83 	}
84 	return (new);
85 }
86 
87 /*
88  * Touch the named message by setting its MTOUCH flag.
89  * Touched messages have the effect of not being sent
90  * back to the system mailbox on exit.
91  */
92 void
93 touch(struct message *mp)
94 {
95 
96 	mp->m_flag |= MTOUCH;
97 	if ((mp->m_flag & MREAD) == 0)
98 		mp->m_flag |= MREAD|MSTATUS;
99 }
100 
101 /*
102  * Test to see if the passed file name is a directory.
103  * Return true if it is.
104  */
105 int
106 isdir(char name[])
107 {
108 	struct stat sbuf;
109 
110 	if (stat(name, &sbuf) < 0)
111 		return (0);
112 	return (S_ISDIR(sbuf.st_mode));
113 }
114 
115 /*
116  * Count the number of arguments in the given string raw list.
117  */
118 int
119 argcount(char **argv)
120 {
121 	char **ap;
122 
123 	for (ap = argv; *ap++ != NULL;)
124 		;
125 	return (ap - argv - 1);
126 }
127 
128 /*
129  * Return the desired header line from the passed message
130  * pointer (or NULL if the desired header field is not available).
131  */
132 char *
133 hfield(const char *field, struct message *mp)
134 {
135 	FILE *ibuf;
136 	char linebuf[LINESIZE];
137 	int lc;
138 	char *hfield;
139 	char *colon, *oldhfield = NULL;
140 
141 	ibuf = setinput(mp);
142 	if ((lc = mp->m_lines - 1) < 0)
143 		return (NULL);
144 	if (readline(ibuf, linebuf, LINESIZE) < 0)
145 		return (NULL);
146 	while (lc > 0) {
147 		if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
148 			return (oldhfield);
149 		if ((hfield = ishfield(linebuf, colon, field)) != NULL)
150 			oldhfield = save2str(hfield, oldhfield);
151 	}
152 	return (oldhfield);
153 }
154 
155 /*
156  * Return the next header field found in the given message.
157  * Return >= 0 if something found, < 0 elsewise.
158  * "colon" is set to point to the colon in the header.
159  * Must deal with \ continuations & other such fraud.
160  */
161 int
162 gethfield(FILE *f, char linebuf[], int rem, char **colon)
163 {
164 	char line2[LINESIZE];
165 	char *cp, *cp2;
166 	int c;
167 
168 	for (;;) {
169 		if (--rem < 0)
170 			return (-1);
171 		if ((c = readline(f, linebuf, LINESIZE)) <= 0)
172 			return (-1);
173 		for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
174 		    cp++)
175 			;
176 		if (*cp != ':' || cp == linebuf)
177 			continue;
178 		/*
179 		 * I guess we got a headline.
180 		 * Handle wraparounding
181 		 */
182 		*colon = cp;
183 		cp = linebuf + c;
184 		for (;;) {
185 			while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
186 				;
187 			cp++;
188 			if (rem <= 0)
189 				break;
190 			ungetc(c = getc(f), f);
191 			if (c != ' ' && c != '\t')
192 				break;
193 			if ((c = readline(f, line2, LINESIZE)) < 0)
194 				break;
195 			rem--;
196 			for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
197 				;
198 			c -= cp2 - line2;
199 			if (cp + c >= linebuf + LINESIZE - 2)
200 				break;
201 			*cp++ = ' ';
202 			bcopy(cp2, cp, c);
203 			cp += c;
204 		}
205 		*cp = 0;
206 		return (rem);
207 	}
208 	/* NOTREACHED */
209 }
210 
211 /*
212  * Check whether the passed line is a header line of
213  * the desired breed.  Return the field body, or 0.
214  */
215 
216 char*
217 ishfield(char linebuf[], char *colon, const char *field)
218 {
219 	char *cp = colon;
220 
221 	*cp = 0;
222 	if (strcasecmp(linebuf, field) != 0) {
223 		*cp = ':';
224 		return (0);
225 	}
226 	*cp = ':';
227 	for (cp++; *cp == ' ' || *cp == '\t'; cp++)
228 		;
229 	return (cp);
230 }
231 
232 /*
233  * Copy a string and lowercase the result.
234  * dsize: space left in buffer (including space for NULL)
235  */
236 void
237 istrncpy(char *dest, const char *src, size_t dsize)
238 {
239 
240 	strlcpy(dest, src, dsize);
241 	for (; *dest; dest++)
242 		*dest = tolower((unsigned char)*dest);
243 }
244 
245 /*
246  * The following code deals with input stacking to do source
247  * commands.  All but the current file pointer are saved on
248  * the stack.
249  */
250 
251 static	int	ssp;			/* Top of file stack */
252 struct sstack {
253 	FILE	*s_file;		/* File we were in. */
254 	int	s_cond;			/* Saved state of conditionals */
255 	int	s_loading;		/* Loading .mailrc, etc. */
256 };
257 #define	SSTACK_SIZE	64		/* XXX was NOFILE. */
258 static struct sstack sstack[SSTACK_SIZE];
259 
260 /*
261  * Pushdown current input file and switch to a new one.
262  * Set the global flag "sourcing" so that others will realize
263  * that they are no longer reading from a tty (in all probability).
264  */
265 int
266 source(char **arglist)
267 {
268 	FILE *fi;
269 	char *cp;
270 
271 	if ((cp = expand(*arglist)) == NULL)
272 		return (1);
273 	if ((fi = Fopen(cp, "r")) == NULL) {
274 		warn("%s", cp);
275 		return (1);
276 	}
277 	if (ssp >= SSTACK_SIZE - 1) {
278 		printf("Too much \"sourcing\" going on.\n");
279 		(void)Fclose(fi);
280 		return (1);
281 	}
282 	sstack[ssp].s_file = input;
283 	sstack[ssp].s_cond = cond;
284 	sstack[ssp].s_loading = loading;
285 	ssp++;
286 	loading = 0;
287 	cond = CANY;
288 	input = fi;
289 	sourcing++;
290 	return (0);
291 }
292 
293 /*
294  * Pop the current input back to the previous level.
295  * Update the "sourcing" flag as appropriate.
296  */
297 int
298 unstack(void)
299 {
300 	if (ssp <= 0) {
301 		printf("\"Source\" stack over-pop.\n");
302 		sourcing = 0;
303 		return (1);
304 	}
305 	(void)Fclose(input);
306 	if (cond != CANY)
307 		printf("Unmatched \"if\"\n");
308 	ssp--;
309 	cond = sstack[ssp].s_cond;
310 	loading = sstack[ssp].s_loading;
311 	input = sstack[ssp].s_file;
312 	if (ssp == 0)
313 		sourcing = loading;
314 	return (0);
315 }
316 
317 /*
318  * Touch the indicated file.
319  * This is nifty for the shell.
320  */
321 void
322 alter(char *name)
323 {
324 	struct timespec ts[2];
325 
326 	(void)clock_gettime(CLOCK_REALTIME, &ts[0]);
327 	ts[0].tv_sec++;
328 	ts[1].tv_sec = 0;
329 	ts[1].tv_nsec = UTIME_OMIT;
330 	(void)utimensat(AT_FDCWD, name, ts, 0);
331 }
332 
333 /*
334  * Get sender's name from this message.  If the message has
335  * a bunch of arpanet stuff in it, we may have to skin the name
336  * before returning it.
337  */
338 char *
339 nameof(struct message *mp, int reptype)
340 {
341 	char *cp, *cp2;
342 
343 	cp = skin(name1(mp, reptype));
344 	if (reptype != 0 || charcount(cp, '!') < 2)
345 		return (cp);
346 	cp2 = strrchr(cp, '!');
347 	cp2--;
348 	while (cp2 > cp && *cp2 != '!')
349 		cp2--;
350 	if (*cp2 == '!')
351 		return (cp2 + 1);
352 	return (cp);
353 }
354 
355 /*
356  * Start of a "comment".
357  * Ignore it.
358  */
359 char *
360 skip_comment(char *cp)
361 {
362 	int nesting = 1;
363 
364 	for (; nesting > 0 && *cp; cp++) {
365 		switch (*cp) {
366 		case '\\':
367 			if (cp[1])
368 				cp++;
369 			break;
370 		case '(':
371 			nesting++;
372 			break;
373 		case ')':
374 			nesting--;
375 			break;
376 		}
377 	}
378 	return (cp);
379 }
380 
381 /*
382  * Skin an arpa net address according to the RFC 822 interpretation
383  * of "host-phrase."
384  */
385 char *
386 skin(char *name)
387 {
388 	char *nbuf, *bufend, *cp, *cp2;
389 	int c, gotlt, lastsp;
390 
391 	if (name == NULL)
392 		return (NULL);
393 	if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
394 	    && strchr(name, ' ') == NULL)
395 		return (name);
396 
397 	/* We assume that length(input) <= length(output) */
398 	if ((nbuf = malloc(strlen(name) + 1)) == NULL)
399 		err(1, "Out of memory");
400 	gotlt = 0;
401 	lastsp = 0;
402 	bufend = nbuf;
403 	for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
404 		switch (c) {
405 		case '(':
406 			cp = skip_comment(cp);
407 			lastsp = 0;
408 			break;
409 
410 		case '"':
411 			/*
412 			 * Start of a "quoted-string".
413 			 * Copy it in its entirety.
414 			 */
415 			while ((c = *cp) != '\0') {
416 				cp++;
417 				if (c == '"')
418 					break;
419 				if (c != '\\')
420 					*cp2++ = c;
421 				else if ((c = *cp) != '\0') {
422 					*cp2++ = c;
423 					cp++;
424 				}
425 			}
426 			lastsp = 0;
427 			break;
428 
429 		case ' ':
430 			if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
431 				cp += 3, *cp2++ = '@';
432 			else
433 			if (cp[0] == '@' && cp[1] == ' ')
434 				cp += 2, *cp2++ = '@';
435 			else
436 				lastsp = 1;
437 			break;
438 
439 		case '<':
440 			cp2 = bufend;
441 			gotlt++;
442 			lastsp = 0;
443 			break;
444 
445 		case '>':
446 			if (gotlt) {
447 				gotlt = 0;
448 				while ((c = *cp) != '\0' && c != ',') {
449 					cp++;
450 					if (c == '(')
451 						cp = skip_comment(cp);
452 					else if (c == '"')
453 						while ((c = *cp) != '\0') {
454 							cp++;
455 							if (c == '"')
456 								break;
457 							if (c == '\\' && *cp != '\0')
458 								cp++;
459 						}
460 				}
461 				lastsp = 0;
462 				break;
463 			}
464 			/* FALLTHROUGH */
465 
466 		default:
467 			if (lastsp) {
468 				lastsp = 0;
469 				*cp2++ = ' ';
470 			}
471 			*cp2++ = c;
472 			if (c == ',' && !gotlt &&
473 			    (*cp == ' ' || *cp == '"' || *cp == '<')) {
474 				*cp2++ = ' ';
475 				while (*cp == ' ')
476 					cp++;
477 				lastsp = 0;
478 				bufend = cp2;
479 			}
480 		}
481 	}
482 	*cp2 = '\0';
483 
484 	if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
485 		nbuf = cp;
486 	return (nbuf);
487 }
488 
489 /*
490  * Fetch the sender's name from the passed message.
491  * Reptype can be
492  *	0 -- get sender's name for display purposes
493  *	1 -- get sender's name for reply
494  *	2 -- get sender's name for Reply
495  */
496 char *
497 name1(struct message *mp, int reptype)
498 {
499 	char namebuf[LINESIZE];
500 	char linebuf[LINESIZE];
501 	char *cp, *cp2;
502 	FILE *ibuf;
503 	int first = 1;
504 
505 	if ((cp = hfield("from", mp)) != NULL)
506 		return (cp);
507 	if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
508 		return (cp);
509 	ibuf = setinput(mp);
510 	namebuf[0] = '\0';
511 	if (readline(ibuf, linebuf, LINESIZE) < 0)
512 		return (savestr(namebuf));
513 newname:
514 	for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
515 		;
516 	for (; *cp == ' ' || *cp == '\t'; cp++)
517 		;
518 	for (cp2 = &namebuf[strlen(namebuf)];
519 	    *cp != '\0' && *cp != ' ' && *cp != '\t' &&
520 	    cp2 < namebuf + LINESIZE - 1;)
521 		*cp2++ = *cp++;
522 	*cp2 = '\0';
523 	if (readline(ibuf, linebuf, LINESIZE) < 0)
524 		return (savestr(namebuf));
525 	if ((cp = strchr(linebuf, 'F')) == NULL)
526 		return (savestr(namebuf));
527 	if (strncmp(cp, "From", 4) != 0)
528 		return (savestr(namebuf));
529 	while ((cp = strchr(cp, 'r')) != NULL) {
530 		if (strncmp(cp, "remote", 6) == 0) {
531 			if ((cp = strchr(cp, 'f')) == NULL)
532 				break;
533 			if (strncmp(cp, "from", 4) != 0)
534 				break;
535 			if ((cp = strchr(cp, ' ')) == NULL)
536 				break;
537 			cp++;
538 			if (first) {
539 				cp2 = namebuf;
540 				first = 0;
541 			} else
542 				cp2 = strrchr(namebuf, '!') + 1;
543 			strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
544 			strcat(namebuf, "!");
545 			goto newname;
546 		}
547 		cp++;
548 	}
549 	return (savestr(namebuf));
550 }
551 
552 /*
553  * Count the occurrences of c in str
554  */
555 int
556 charcount(char *str, int c)
557 {
558 	char *cp;
559 	int i;
560 
561 	for (i = 0, cp = str; *cp != '\0'; cp++)
562 		if (*cp == c)
563 			i++;
564 	return (i);
565 }
566 
567 /*
568  * See if the given header field is supposed to be ignored.
569  */
570 int
571 isign(const char *field, struct ignoretab ignore[2])
572 {
573 	char realfld[LINESIZE];
574 
575 	if (ignore == ignoreall)
576 		return (1);
577 	/*
578 	 * Lower-case the string, so that "Status" and "status"
579 	 * will hash to the same place.
580 	 */
581 	istrncpy(realfld, field, sizeof(realfld));
582 	if (ignore[1].i_count > 0)
583 		return (!member(realfld, ignore + 1));
584 	else
585 		return (member(realfld, ignore));
586 }
587 
588 int
589 member(char *realfield, struct ignoretab *table)
590 {
591 	struct ignore *igp;
592 
593 	for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
594 		if (*igp->i_field == *realfield &&
595 		    equal(igp->i_field, realfield))
596 			return (1);
597 	return (0);
598 }
599