/usr.bin/mail/util.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 598 lines · 448 code · 43 blank · 107 comment · 124 complexity · d6857ccfb8c24f6d402cd583cf0340bd MD5 · raw file

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