PageRenderTime 70ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/sendmail/src/recipient.c

https://github.com/okuoku/freebsd-head
C | 2072 lines | 1474 code | 176 blank | 422 comment | 496 complexity | 8e1e529f3b765e93aa9807c140df8f6d MD5 | raw file
  1. /*
  2. * Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
  3. * All rights reserved.
  4. * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
  5. * Copyright (c) 1988, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * By using this file, you agree to the terms and conditions set
  9. * forth in the LICENSE file which can be found at the top level of
  10. * the sendmail distribution.
  11. *
  12. */
  13. #include <sendmail.h>
  14. SM_RCSID("@(#)$Id: recipient.c,v 8.349 2007/07/10 17:01:22 ca Exp $")
  15. static void includetimeout __P((int));
  16. static ADDRESS *self_reference __P((ADDRESS *));
  17. static int sortexpensive __P((ADDRESS *, ADDRESS *));
  18. static int sortbysignature __P((ADDRESS *, ADDRESS *));
  19. static int sorthost __P((ADDRESS *, ADDRESS *));
  20. typedef int sortfn_t __P((ADDRESS *, ADDRESS *));
  21. /*
  22. ** SORTHOST -- strcmp()-like func for host portion of an ADDRESS
  23. **
  24. ** Parameters:
  25. ** xx -- first ADDRESS
  26. ** yy -- second ADDRESS
  27. **
  28. ** Returns:
  29. ** <0 when xx->q_host is less than yy->q_host
  30. ** >0 when xx->q_host is greater than yy->q_host
  31. ** 0 when equal
  32. */
  33. static int
  34. sorthost(xx, yy)
  35. register ADDRESS *xx;
  36. register ADDRESS *yy;
  37. {
  38. #if _FFR_HOST_SORT_REVERSE
  39. /* XXX maybe compare hostnames from the end? */
  40. return sm_strrevcasecmp(xx->q_host, yy->q_host);
  41. #else /* _FFR_HOST_SORT_REVERSE */
  42. return sm_strcasecmp(xx->q_host, yy->q_host);
  43. #endif /* _FFR_HOST_SORT_REVERSE */
  44. }
  45. /*
  46. ** SORTEXPENSIVE -- strcmp()-like func for expensive mailers
  47. **
  48. ** The mailer has been noted already as "expensive" for 'xx'. This
  49. ** will give a result relative to 'yy'. Expensive mailers get rated
  50. ** "greater than" non-expensive mailers because during the delivery phase
  51. ** it will get queued -- no use it getting in the way of less expensive
  52. ** recipients. We avoid an MX RR lookup when both 'xx' and 'yy' are
  53. ** expensive since an MX RR lookup happens when extracted from the queue
  54. ** later.
  55. **
  56. ** Parameters:
  57. ** xx -- first ADDRESS
  58. ** yy -- second ADDRESS
  59. **
  60. ** Returns:
  61. ** <0 when xx->q_host is less than yy->q_host and both are
  62. ** expensive
  63. ** >0 when xx->q_host is greater than yy->q_host, or when
  64. ** 'yy' is non-expensive
  65. ** 0 when equal (by expense and q_host)
  66. */
  67. static int
  68. sortexpensive(xx, yy)
  69. ADDRESS *xx;
  70. ADDRESS *yy;
  71. {
  72. if (!bitnset(M_EXPENSIVE, yy->q_mailer->m_flags))
  73. return 1; /* xx should go later */
  74. #if _FFR_HOST_SORT_REVERSE
  75. /* XXX maybe compare hostnames from the end? */
  76. return sm_strrevcasecmp(xx->q_host, yy->q_host);
  77. #else /* _FFR_HOST_SORT_REVERSE */
  78. return sm_strcasecmp(xx->q_host, yy->q_host);
  79. #endif /* _FFR_HOST_SORT_REVERSE */
  80. }
  81. /*
  82. ** SORTBYSIGNATURE -- a strcmp()-like func for q_mailer and q_host in ADDRESS
  83. **
  84. ** Parameters:
  85. ** xx -- first ADDRESS
  86. ** yy -- second ADDRESS
  87. **
  88. ** Returns:
  89. ** 0 when the "signature"'s are same
  90. ** <0 when xx->q_signature is less than yy->q_signature
  91. ** >0 when xx->q_signature is greater than yy->q_signature
  92. **
  93. ** Side Effect:
  94. ** May set ADDRESS pointer for q_signature if not already set.
  95. */
  96. static int
  97. sortbysignature(xx, yy)
  98. ADDRESS *xx;
  99. ADDRESS *yy;
  100. {
  101. register int ret;
  102. /* Let's avoid redoing the signature over and over again */
  103. if (xx->q_signature == NULL)
  104. xx->q_signature = hostsignature(xx->q_mailer, xx->q_host);
  105. if (yy->q_signature == NULL)
  106. yy->q_signature = hostsignature(yy->q_mailer, yy->q_host);
  107. ret = strcmp(xx->q_signature, yy->q_signature);
  108. /*
  109. ** If the two signatures are the same then we will return a sort
  110. ** value based on 'q_user'. But note that we have reversed xx and yy
  111. ** on purpose. This additional compare helps reduce the number of
  112. ** sameaddr() calls and loops in recipient() for the case when
  113. ** the rcpt list has been provided already in-order.
  114. */
  115. if (ret == 0)
  116. return strcmp(yy->q_user, xx->q_user);
  117. else
  118. return ret;
  119. }
  120. /*
  121. ** SENDTOLIST -- Designate a send list.
  122. **
  123. ** The parameter is a comma-separated list of people to send to.
  124. ** This routine arranges to send to all of them.
  125. **
  126. ** Parameters:
  127. ** list -- the send list.
  128. ** ctladdr -- the address template for the person to
  129. ** send to -- effective uid/gid are important.
  130. ** This is typically the alias that caused this
  131. ** expansion.
  132. ** sendq -- a pointer to the head of a queue to put
  133. ** these people into.
  134. ** aliaslevel -- the current alias nesting depth -- to
  135. ** diagnose loops.
  136. ** e -- the envelope in which to add these recipients.
  137. **
  138. ** Returns:
  139. ** The number of addresses actually on the list.
  140. */
  141. /* q_flags bits inherited from ctladdr */
  142. #define QINHERITEDBITS (QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY|QHASNOTIFY)
  143. int
  144. sendtolist(list, ctladdr, sendq, aliaslevel, e)
  145. char *list;
  146. ADDRESS *ctladdr;
  147. ADDRESS **sendq;
  148. int aliaslevel;
  149. register ENVELOPE *e;
  150. {
  151. register char *p;
  152. register ADDRESS *SM_NONVOLATILE al; /* list of addresses to send to */
  153. SM_NONVOLATILE char delimiter; /* the address delimiter */
  154. SM_NONVOLATILE int naddrs;
  155. SM_NONVOLATILE int i;
  156. char *endp;
  157. char *oldto = e->e_to;
  158. char *SM_NONVOLATILE bufp;
  159. char buf[MAXNAME + 1];
  160. if (list == NULL)
  161. {
  162. syserr("sendtolist: null list");
  163. return 0;
  164. }
  165. if (tTd(25, 1))
  166. {
  167. sm_dprintf("sendto: %s\n ctladdr=", list);
  168. printaddr(sm_debug_file(), ctladdr, false);
  169. }
  170. /* heuristic to determine old versus new style addresses */
  171. if (ctladdr == NULL &&
  172. (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
  173. strchr(list, '<') != NULL || strchr(list, '(') != NULL))
  174. e->e_flags &= ~EF_OLDSTYLE;
  175. delimiter = ' ';
  176. if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
  177. delimiter = ',';
  178. al = NULL;
  179. naddrs = 0;
  180. /* make sure we have enough space to copy the string */
  181. i = strlen(list) + 1;
  182. if (i <= sizeof(buf))
  183. {
  184. bufp = buf;
  185. i = sizeof(buf);
  186. }
  187. else
  188. bufp = sm_malloc_x(i);
  189. endp = bufp + i;
  190. SM_TRY
  191. {
  192. (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
  193. macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
  194. for (p = bufp; *p != '\0'; )
  195. {
  196. auto char *delimptr;
  197. register ADDRESS *a;
  198. SM_ASSERT(p < endp);
  199. /* parse the address */
  200. while ((isascii(*p) && isspace(*p)) || *p == ',')
  201. p++;
  202. SM_ASSERT(p < endp);
  203. a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter,
  204. &delimptr, e, true);
  205. p = delimptr;
  206. SM_ASSERT(p < endp);
  207. if (a == NULL)
  208. continue;
  209. a->q_next = al;
  210. a->q_alias = ctladdr;
  211. /* arrange to inherit attributes from parent */
  212. if (ctladdr != NULL)
  213. {
  214. ADDRESS *b;
  215. /* self reference test */
  216. if (sameaddr(ctladdr, a))
  217. {
  218. if (tTd(27, 5))
  219. {
  220. sm_dprintf("sendtolist: QSELFREF ");
  221. printaddr(sm_debug_file(), ctladdr, false);
  222. }
  223. ctladdr->q_flags |= QSELFREF;
  224. }
  225. /* check for address loops */
  226. b = self_reference(a);
  227. if (b != NULL)
  228. {
  229. b->q_flags |= QSELFREF;
  230. if (tTd(27, 5))
  231. {
  232. sm_dprintf("sendtolist: QSELFREF ");
  233. printaddr(sm_debug_file(), b, false);
  234. }
  235. if (a != b)
  236. {
  237. if (tTd(27, 5))
  238. {
  239. sm_dprintf("sendtolist: QS_DONTSEND ");
  240. printaddr(sm_debug_file(), a, false);
  241. }
  242. a->q_state = QS_DONTSEND;
  243. b->q_flags |= a->q_flags & QNOTREMOTE;
  244. continue;
  245. }
  246. }
  247. /* full name */
  248. if (a->q_fullname == NULL)
  249. a->q_fullname = ctladdr->q_fullname;
  250. /* various flag bits */
  251. a->q_flags &= ~QINHERITEDBITS;
  252. a->q_flags |= ctladdr->q_flags & QINHERITEDBITS;
  253. /* DSN recipient information */
  254. a->q_finalrcpt = ctladdr->q_finalrcpt;
  255. a->q_orcpt = ctladdr->q_orcpt;
  256. }
  257. al = a;
  258. }
  259. /* arrange to send to everyone on the local send list */
  260. while (al != NULL)
  261. {
  262. register ADDRESS *a = al;
  263. al = a->q_next;
  264. a = recipient(a, sendq, aliaslevel, e);
  265. naddrs++;
  266. }
  267. }
  268. SM_FINALLY
  269. {
  270. e->e_to = oldto;
  271. if (bufp != buf)
  272. sm_free(bufp);
  273. macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
  274. }
  275. SM_END_TRY
  276. return naddrs;
  277. }
  278. #if MILTER
  279. /*
  280. ** REMOVEFROMLIST -- Remove addresses from a send list.
  281. **
  282. ** The parameter is a comma-separated list of recipients to remove.
  283. ** Note that it only deletes matching addresses. If those addresses
  284. ** have been expanded already in the sendq, it won't mark the
  285. ** expanded recipients as QS_REMOVED.
  286. **
  287. ** Parameters:
  288. ** list -- the list to remove.
  289. ** sendq -- a pointer to the head of a queue to remove
  290. ** these addresses from.
  291. ** e -- the envelope in which to remove these recipients.
  292. **
  293. ** Returns:
  294. ** The number of addresses removed from the list.
  295. **
  296. */
  297. int
  298. removefromlist(list, sendq, e)
  299. char *list;
  300. ADDRESS **sendq;
  301. ENVELOPE *e;
  302. {
  303. SM_NONVOLATILE char delimiter; /* the address delimiter */
  304. SM_NONVOLATILE int naddrs;
  305. SM_NONVOLATILE int i;
  306. char *p;
  307. char *oldto = e->e_to;
  308. char *SM_NONVOLATILE bufp;
  309. char buf[MAXNAME + 1];
  310. if (list == NULL)
  311. {
  312. syserr("removefromlist: null list");
  313. return 0;
  314. }
  315. if (tTd(25, 1))
  316. sm_dprintf("removefromlist: %s\n", list);
  317. /* heuristic to determine old versus new style addresses */
  318. if (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
  319. strchr(list, '<') != NULL || strchr(list, '(') != NULL)
  320. e->e_flags &= ~EF_OLDSTYLE;
  321. delimiter = ' ';
  322. if (!bitset(EF_OLDSTYLE, e->e_flags))
  323. delimiter = ',';
  324. naddrs = 0;
  325. /* make sure we have enough space to copy the string */
  326. i = strlen(list) + 1;
  327. if (i <= sizeof(buf))
  328. {
  329. bufp = buf;
  330. i = sizeof(buf);
  331. }
  332. else
  333. bufp = sm_malloc_x(i);
  334. SM_TRY
  335. {
  336. (void) sm_strlcpy(bufp, denlstring(list, false, true), i);
  337. #if _FFR_ADDR_TYPE_MODES
  338. if (AddrTypeModes)
  339. macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
  340. "e r d");
  341. else
  342. #endif /* _FFR_ADDR_TYPE_MODES */
  343. macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
  344. for (p = bufp; *p != '\0'; )
  345. {
  346. ADDRESS a; /* parsed address to be removed */
  347. ADDRESS *q;
  348. ADDRESS **pq;
  349. char *delimptr;
  350. /* parse the address */
  351. while ((isascii(*p) && isspace(*p)) || *p == ',')
  352. p++;
  353. if (parseaddr(p, &a, RF_COPYALL|RF_RM_ADDR,
  354. delimiter, &delimptr, e, true) == NULL)
  355. {
  356. p = delimptr;
  357. continue;
  358. }
  359. p = delimptr;
  360. for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
  361. {
  362. if (!QS_IS_DEAD(q->q_state) &&
  363. (sameaddr(q, &a) ||
  364. strcmp(q->q_paddr, a.q_paddr) == 0))
  365. {
  366. if (tTd(25, 5))
  367. {
  368. sm_dprintf("removefromlist: QS_REMOVED ");
  369. printaddr(sm_debug_file(), &a, false);
  370. }
  371. q->q_state = QS_REMOVED;
  372. naddrs++;
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. SM_FINALLY
  379. {
  380. e->e_to = oldto;
  381. if (bufp != buf)
  382. sm_free(bufp);
  383. macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
  384. }
  385. SM_END_TRY
  386. return naddrs;
  387. }
  388. #endif /* MILTER */
  389. /*
  390. ** RECIPIENT -- Designate a message recipient
  391. ** Saves the named person for future mailing (after some checks).
  392. **
  393. ** Parameters:
  394. ** new -- the (preparsed) address header for the recipient.
  395. ** sendq -- a pointer to the head of a queue to put the
  396. ** recipient in. Duplicate suppression is done
  397. ** in this queue.
  398. ** aliaslevel -- the current alias nesting depth.
  399. ** e -- the current envelope.
  400. **
  401. ** Returns:
  402. ** The actual address in the queue. This will be "a" if
  403. ** the address is not a duplicate, else the original address.
  404. **
  405. */
  406. ADDRESS *
  407. recipient(new, sendq, aliaslevel, e)
  408. register ADDRESS *new;
  409. register ADDRESS **sendq;
  410. int aliaslevel;
  411. register ENVELOPE *e;
  412. {
  413. register ADDRESS *q;
  414. ADDRESS **pq;
  415. ADDRESS **prev;
  416. register struct mailer *m;
  417. register char *p;
  418. int i, buflen;
  419. bool quoted; /* set if the addr has a quote bit */
  420. bool insert;
  421. int findusercount;
  422. bool initialdontsend;
  423. char *buf;
  424. char buf0[MAXNAME + 1]; /* unquoted image of the user name */
  425. sortfn_t *sortfn;
  426. p = NULL;
  427. quoted = false;
  428. insert = false;
  429. findusercount = 0;
  430. initialdontsend = QS_IS_DEAD(new->q_state);
  431. e->e_to = new->q_paddr;
  432. m = new->q_mailer;
  433. errno = 0;
  434. if (aliaslevel == 0)
  435. new->q_flags |= QPRIMARY;
  436. if (tTd(26, 1))
  437. {
  438. sm_dprintf("\nrecipient (%d): ", aliaslevel);
  439. printaddr(sm_debug_file(), new, false);
  440. }
  441. /* if this is primary, use it as original recipient */
  442. if (new->q_alias == NULL)
  443. {
  444. if (e->e_origrcpt == NULL)
  445. e->e_origrcpt = new->q_paddr;
  446. else if (e->e_origrcpt != new->q_paddr)
  447. e->e_origrcpt = "";
  448. }
  449. /* find parent recipient for finalrcpt and orcpt */
  450. for (q = new; q->q_alias != NULL; q = q->q_alias)
  451. continue;
  452. /* find final recipient DSN address */
  453. if (new->q_finalrcpt == NULL &&
  454. e->e_from.q_mailer != NULL)
  455. {
  456. char frbuf[MAXLINE];
  457. p = e->e_from.q_mailer->m_addrtype;
  458. if (p == NULL)
  459. p = "rfc822";
  460. if (sm_strcasecmp(p, "rfc822") != 0)
  461. {
  462. (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
  463. q->q_mailer->m_addrtype,
  464. q->q_user);
  465. }
  466. else if (strchr(q->q_user, '@') != NULL)
  467. {
  468. (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
  469. p, q->q_user);
  470. }
  471. else if (strchr(q->q_paddr, '@') != NULL)
  472. {
  473. char *qp;
  474. bool b;
  475. qp = q->q_paddr;
  476. /* strip brackets from address */
  477. b = false;
  478. if (*qp == '<')
  479. {
  480. b = qp[strlen(qp) - 1] == '>';
  481. if (b)
  482. qp[strlen(qp) - 1] = '\0';
  483. qp++;
  484. }
  485. (void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
  486. p, qp);
  487. /* undo damage */
  488. if (b)
  489. qp[strlen(qp)] = '>';
  490. }
  491. else
  492. {
  493. (void) sm_snprintf(frbuf, sizeof(frbuf),
  494. "%s; %.700s@%.100s",
  495. p, q->q_user, MyHostName);
  496. }
  497. new->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool, frbuf);
  498. }
  499. #if _FFR_GEN_ORCPT
  500. /* set ORCPT DSN arg if not already set */
  501. if (new->q_orcpt == NULL)
  502. {
  503. /* check for an existing ORCPT */
  504. if (q->q_orcpt != NULL)
  505. new->q_orcpt = q->q_orcpt;
  506. else
  507. {
  508. /* make our own */
  509. bool b = false;
  510. char *qp;
  511. char obuf[MAXLINE];
  512. if (e->e_from.q_mailer != NULL)
  513. p = e->e_from.q_mailer->m_addrtype;
  514. if (p == NULL)
  515. p = "rfc822";
  516. (void) sm_strlcpyn(obuf, sizeof(obuf), 2, p, ";");
  517. qp = q->q_paddr;
  518. /* FFR: Needs to strip comments from stdin addrs */
  519. /* strip brackets from address */
  520. if (*qp == '<')
  521. {
  522. b = qp[strlen(qp) - 1] == '>';
  523. if (b)
  524. qp[strlen(qp) - 1] = '\0';
  525. qp++;
  526. }
  527. p = xtextify(denlstring(qp, true, false), "=");
  528. if (sm_strlcat(obuf, p, sizeof(obuf)) >= sizeof(obuf))
  529. {
  530. /* if too big, don't use it */
  531. obuf[0] = '\0';
  532. }
  533. /* undo damage */
  534. if (b)
  535. qp[strlen(qp)] = '>';
  536. if (obuf[0] != '\0')
  537. new->q_orcpt =
  538. sm_rpool_strdup_x(e->e_rpool, obuf);
  539. }
  540. }
  541. #endif /* _FFR_GEN_ORCPT */
  542. /* break aliasing loops */
  543. if (aliaslevel > MaxAliasRecursion)
  544. {
  545. new->q_state = QS_BADADDR;
  546. new->q_status = "5.4.6";
  547. if (new->q_alias != NULL)
  548. {
  549. new->q_alias->q_state = QS_BADADDR;
  550. new->q_alias->q_status = "5.4.6";
  551. }
  552. if ((SuprErrs || !LogUsrErrs) && LogLevel > 0)
  553. {
  554. sm_syslog(LOG_ERR, e->e_id,
  555. "aliasing/forwarding loop broken: %s (%d aliases deep; %d max)",
  556. FileName != NULL ? FileName : "", aliaslevel,
  557. MaxAliasRecursion);
  558. }
  559. usrerrenh(new->q_status,
  560. "554 aliasing/forwarding loop broken (%d aliases deep; %d max)",
  561. aliaslevel, MaxAliasRecursion);
  562. return new;
  563. }
  564. /*
  565. ** Finish setting up address structure.
  566. */
  567. /* get unquoted user for file, program or user.name check */
  568. i = strlen(new->q_user);
  569. if (i >= sizeof(buf0))
  570. {
  571. buflen = i + 1;
  572. buf = xalloc(buflen);
  573. }
  574. else
  575. {
  576. buf = buf0;
  577. buflen = sizeof(buf0);
  578. }
  579. (void) sm_strlcpy(buf, new->q_user, buflen);
  580. for (p = buf; *p != '\0' && !quoted; p++)
  581. {
  582. if (*p == '\\')
  583. quoted = true;
  584. }
  585. stripquotes(buf);
  586. /* check for direct mailing to restricted mailers */
  587. if (m == ProgMailer)
  588. {
  589. if (new->q_alias == NULL || UseMSP ||
  590. bitset(EF_UNSAFE, e->e_flags))
  591. {
  592. new->q_state = QS_BADADDR;
  593. new->q_status = "5.7.1";
  594. usrerrenh(new->q_status,
  595. "550 Cannot mail directly to programs");
  596. }
  597. else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
  598. {
  599. new->q_state = QS_BADADDR;
  600. new->q_status = "5.7.1";
  601. if (new->q_alias->q_ruser == NULL)
  602. usrerrenh(new->q_status,
  603. "550 UID %d is an unknown user: cannot mail to programs",
  604. new->q_alias->q_uid);
  605. else
  606. usrerrenh(new->q_status,
  607. "550 User %s@%s doesn't have a valid shell for mailing to programs",
  608. new->q_alias->q_ruser, MyHostName);
  609. }
  610. else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
  611. {
  612. new->q_state = QS_BADADDR;
  613. new->q_status = "5.7.1";
  614. new->q_rstatus = "550 Unsafe for mailing to programs";
  615. usrerrenh(new->q_status,
  616. "550 Address %s is unsafe for mailing to programs",
  617. new->q_alias->q_paddr);
  618. }
  619. }
  620. /*
  621. ** Look up this person in the recipient list.
  622. ** If they are there already, return, otherwise continue.
  623. ** If the list is empty, just add it. Notice the cute
  624. ** hack to make from addresses suppress things correctly:
  625. ** the QS_DUPLICATE state will be set in the send list.
  626. ** [Please note: the emphasis is on "hack."]
  627. */
  628. prev = NULL;
  629. /*
  630. ** If this message is going to the queue or FastSplit is set
  631. ** and it is the first try and the envelope hasn't split, then we
  632. ** avoid doing an MX RR lookup now because one will be done when the
  633. ** message is extracted from the queue later. It can go to the queue
  634. ** because all messages are going to the queue or this mailer of
  635. ** the current recipient is marked expensive.
  636. */
  637. if (UseMSP || WILL_BE_QUEUED(e->e_sendmode) ||
  638. (!bitset(EF_SPLIT, e->e_flags) && e->e_ntries == 0 &&
  639. FastSplit > 0))
  640. sortfn = sorthost;
  641. else if (NoConnect && bitnset(M_EXPENSIVE, new->q_mailer->m_flags))
  642. sortfn = sortexpensive;
  643. else
  644. sortfn = sortbysignature;
  645. for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
  646. {
  647. /*
  648. ** If address is "less than" it should be inserted now.
  649. ** If address is "greater than" current comparison it'll
  650. ** insert later in the list; so loop again (if possible).
  651. ** If address is "equal" (different equal than sameaddr()
  652. ** call) then check if sameaddr() will be true.
  653. ** Because this list is now sorted, it'll mean fewer
  654. ** comparisons and fewer loops which is important for more
  655. ** recipients.
  656. */
  657. i = (*sortfn)(new, q);
  658. if (i == 0) /* equal */
  659. {
  660. /*
  661. ** Sortbysignature() has said that the two have
  662. ** equal MX RR's and the same user. Calling sameaddr()
  663. ** now checks if the two hosts are as identical as the
  664. ** MX RR's are (which might not be the case)
  665. ** before saying these are the identical addresses.
  666. */
  667. if (sameaddr(q, new) &&
  668. (bitset(QRCPTOK, q->q_flags) ||
  669. !bitset(QPRIMARY, q->q_flags)))
  670. {
  671. if (tTd(26, 1))
  672. {
  673. sm_dprintf("%s in sendq: ",
  674. new->q_paddr);
  675. printaddr(sm_debug_file(), q, false);
  676. }
  677. if (!bitset(QPRIMARY, q->q_flags))
  678. {
  679. if (!QS_IS_DEAD(new->q_state))
  680. message("duplicate suppressed");
  681. else
  682. q->q_state = QS_DUPLICATE;
  683. q->q_flags |= new->q_flags;
  684. }
  685. else if (bitset(QSELFREF, q->q_flags)
  686. || q->q_state == QS_REMOVED)
  687. {
  688. /*
  689. ** If an earlier milter removed the
  690. ** address, a later one can still add
  691. ** it back.
  692. */
  693. q->q_state = new->q_state;
  694. q->q_flags |= new->q_flags;
  695. }
  696. new = q;
  697. goto done;
  698. }
  699. }
  700. else if (i < 0) /* less than */
  701. {
  702. insert = true;
  703. break;
  704. }
  705. prev = pq;
  706. }
  707. /* pq should point to an address, never NULL */
  708. SM_ASSERT(pq != NULL);
  709. /* add address on list */
  710. if (insert)
  711. {
  712. /*
  713. ** insert before 'pq'. Only possible when at least 1
  714. ** ADDRESS is in the list already.
  715. */
  716. new->q_next = *pq;
  717. if (prev == NULL)
  718. *sendq = new; /* To be the first ADDRESS */
  719. else
  720. (*prev)->q_next = new;
  721. }
  722. else
  723. {
  724. /*
  725. ** Place in list at current 'pq' position. Possible
  726. ** when there are 0 or more ADDRESS's in the list.
  727. */
  728. new->q_next = NULL;
  729. *pq = new;
  730. }
  731. /* added a new address: clear split flag */
  732. e->e_flags &= ~EF_SPLIT;
  733. /*
  734. ** Alias the name and handle special mailer types.
  735. */
  736. trylocaluser:
  737. if (tTd(29, 7))
  738. {
  739. sm_dprintf("at trylocaluser: ");
  740. printaddr(sm_debug_file(), new, false);
  741. }
  742. if (!QS_IS_OK(new->q_state))
  743. {
  744. if (QS_IS_UNDELIVERED(new->q_state))
  745. e->e_nrcpts++;
  746. goto testselfdestruct;
  747. }
  748. if (m == InclMailer)
  749. {
  750. new->q_state = QS_INCLUDED;
  751. if (new->q_alias == NULL || UseMSP ||
  752. bitset(EF_UNSAFE, e->e_flags))
  753. {
  754. new->q_state = QS_BADADDR;
  755. new->q_status = "5.7.1";
  756. usrerrenh(new->q_status,
  757. "550 Cannot mail directly to :include:s");
  758. }
  759. else
  760. {
  761. int ret;
  762. message("including file %s", new->q_user);
  763. ret = include(new->q_user, false, new,
  764. sendq, aliaslevel, e);
  765. if (transienterror(ret))
  766. {
  767. if (LogLevel > 2)
  768. sm_syslog(LOG_ERR, e->e_id,
  769. "include %s: transient error: %s",
  770. shortenstring(new->q_user,
  771. MAXSHORTSTR),
  772. sm_errstring(ret));
  773. new->q_state = QS_QUEUEUP;
  774. usrerr("451 4.2.4 Cannot open %s: %s",
  775. shortenstring(new->q_user,
  776. MAXSHORTSTR),
  777. sm_errstring(ret));
  778. }
  779. else if (ret != 0)
  780. {
  781. new->q_state = QS_BADADDR;
  782. new->q_status = "5.2.4";
  783. usrerrenh(new->q_status,
  784. "550 Cannot open %s: %s",
  785. shortenstring(new->q_user,
  786. MAXSHORTSTR),
  787. sm_errstring(ret));
  788. }
  789. }
  790. }
  791. else if (m == FileMailer)
  792. {
  793. /* check if allowed */
  794. if (new->q_alias == NULL || UseMSP ||
  795. bitset(EF_UNSAFE, e->e_flags))
  796. {
  797. new->q_state = QS_BADADDR;
  798. new->q_status = "5.7.1";
  799. usrerrenh(new->q_status,
  800. "550 Cannot mail directly to files");
  801. }
  802. else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
  803. {
  804. new->q_state = QS_BADADDR;
  805. new->q_status = "5.7.1";
  806. if (new->q_alias->q_ruser == NULL)
  807. usrerrenh(new->q_status,
  808. "550 UID %d is an unknown user: cannot mail to files",
  809. new->q_alias->q_uid);
  810. else
  811. usrerrenh(new->q_status,
  812. "550 User %s@%s doesn't have a valid shell for mailing to files",
  813. new->q_alias->q_ruser, MyHostName);
  814. }
  815. else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
  816. {
  817. new->q_state = QS_BADADDR;
  818. new->q_status = "5.7.1";
  819. new->q_rstatus = "550 Unsafe for mailing to files";
  820. usrerrenh(new->q_status,
  821. "550 Address %s is unsafe for mailing to files",
  822. new->q_alias->q_paddr);
  823. }
  824. }
  825. /* try aliasing */
  826. if (!quoted && QS_IS_OK(new->q_state) &&
  827. bitnset(M_ALIASABLE, m->m_flags))
  828. alias(new, sendq, aliaslevel, e);
  829. #if USERDB
  830. /* if not aliased, look it up in the user database */
  831. if (!bitset(QNOTREMOTE, new->q_flags) &&
  832. QS_IS_SENDABLE(new->q_state) &&
  833. bitnset(M_CHECKUDB, m->m_flags))
  834. {
  835. if (udbexpand(new, sendq, aliaslevel, e) == EX_TEMPFAIL)
  836. {
  837. new->q_state = QS_QUEUEUP;
  838. if (e->e_message == NULL)
  839. e->e_message = sm_rpool_strdup_x(e->e_rpool,
  840. "Deferred: user database error");
  841. if (new->q_message == NULL)
  842. new->q_message = "Deferred: user database error";
  843. if (LogLevel > 8)
  844. sm_syslog(LOG_INFO, e->e_id,
  845. "deferred: udbexpand: %s",
  846. sm_errstring(errno));
  847. message("queued (user database error): %s",
  848. sm_errstring(errno));
  849. e->e_nrcpts++;
  850. goto testselfdestruct;
  851. }
  852. }
  853. #endif /* USERDB */
  854. /*
  855. ** If we have a level two config file, then pass the name through
  856. ** Ruleset 5 before sending it off. Ruleset 5 has the right
  857. ** to rewrite it to another mailer. This gives us a hook
  858. ** after local aliasing has been done.
  859. */
  860. if (tTd(29, 5))
  861. {
  862. sm_dprintf("recipient: testing local? cl=%d, rr5=%p\n\t",
  863. ConfigLevel, RewriteRules[5]);
  864. printaddr(sm_debug_file(), new, false);
  865. }
  866. if (ConfigLevel >= 2 && RewriteRules[5] != NULL &&
  867. bitnset(M_TRYRULESET5, m->m_flags) &&
  868. !bitset(QNOTREMOTE, new->q_flags) &&
  869. QS_IS_OK(new->q_state))
  870. {
  871. maplocaluser(new, sendq, aliaslevel + 1, e);
  872. }
  873. /*
  874. ** If it didn't get rewritten to another mailer, go ahead
  875. ** and deliver it.
  876. */
  877. if (QS_IS_OK(new->q_state) &&
  878. bitnset(M_HASPWENT, m->m_flags))
  879. {
  880. auto bool fuzzy;
  881. SM_MBDB_T user;
  882. int status;
  883. /* warning -- finduser may trash buf */
  884. status = finduser(buf, &fuzzy, &user);
  885. switch (status)
  886. {
  887. case EX_TEMPFAIL:
  888. new->q_state = QS_QUEUEUP;
  889. new->q_status = "4.5.2";
  890. giveresponse(EX_TEMPFAIL, new->q_status, m, NULL,
  891. new->q_alias, (time_t) 0, e, new);
  892. break;
  893. default:
  894. new->q_state = QS_BADADDR;
  895. new->q_status = "5.1.1";
  896. new->q_rstatus = "550 5.1.1 User unknown";
  897. giveresponse(EX_NOUSER, new->q_status, m, NULL,
  898. new->q_alias, (time_t) 0, e, new);
  899. break;
  900. case EX_OK:
  901. if (fuzzy)
  902. {
  903. /* name was a fuzzy match */
  904. new->q_user = sm_rpool_strdup_x(e->e_rpool,
  905. user.mbdb_name);
  906. if (findusercount++ > 3)
  907. {
  908. new->q_state = QS_BADADDR;
  909. new->q_status = "5.4.6";
  910. usrerrenh(new->q_status,
  911. "554 aliasing/forwarding loop for %s broken",
  912. user.mbdb_name);
  913. goto done;
  914. }
  915. /* see if it aliases */
  916. (void) sm_strlcpy(buf, user.mbdb_name, buflen);
  917. goto trylocaluser;
  918. }
  919. if (*user.mbdb_homedir == '\0')
  920. new->q_home = NULL;
  921. else if (strcmp(user.mbdb_homedir, "/") == 0)
  922. new->q_home = "";
  923. else
  924. new->q_home = sm_rpool_strdup_x(e->e_rpool,
  925. user.mbdb_homedir);
  926. if (user.mbdb_uid != SM_NO_UID)
  927. {
  928. new->q_uid = user.mbdb_uid;
  929. new->q_gid = user.mbdb_gid;
  930. new->q_flags |= QGOODUID;
  931. }
  932. new->q_ruser = sm_rpool_strdup_x(e->e_rpool,
  933. user.mbdb_name);
  934. if (user.mbdb_fullname[0] != '\0')
  935. new->q_fullname = sm_rpool_strdup_x(e->e_rpool,
  936. user.mbdb_fullname);
  937. if (!usershellok(user.mbdb_name, user.mbdb_shell))
  938. {
  939. new->q_flags |= QBOGUSSHELL;
  940. }
  941. if (bitset(EF_VRFYONLY, e->e_flags))
  942. {
  943. /* don't do any more now */
  944. new->q_state = QS_VERIFIED;
  945. }
  946. else if (!quoted)
  947. forward(new, sendq, aliaslevel, e);
  948. }
  949. }
  950. if (!QS_IS_DEAD(new->q_state))
  951. e->e_nrcpts++;
  952. testselfdestruct:
  953. new->q_flags |= QTHISPASS;
  954. if (tTd(26, 8))
  955. {
  956. sm_dprintf("testselfdestruct: ");
  957. printaddr(sm_debug_file(), new, false);
  958. if (tTd(26, 10))
  959. {
  960. sm_dprintf("SENDQ:\n");
  961. printaddr(sm_debug_file(), *sendq, true);
  962. sm_dprintf("----\n");
  963. }
  964. }
  965. if (new->q_alias == NULL && new != &e->e_from &&
  966. QS_IS_DEAD(new->q_state))
  967. {
  968. for (q = *sendq; q != NULL; q = q->q_next)
  969. {
  970. if (!QS_IS_DEAD(q->q_state))
  971. break;
  972. }
  973. if (q == NULL)
  974. {
  975. new->q_state = QS_BADADDR;
  976. new->q_status = "5.4.6";
  977. usrerrenh(new->q_status,
  978. "554 aliasing/forwarding loop broken");
  979. }
  980. }
  981. done:
  982. new->q_flags |= QTHISPASS;
  983. if (buf != buf0)
  984. sm_free(buf); /* XXX leak if above code raises exception */
  985. /*
  986. ** If we are at the top level, check to see if this has
  987. ** expanded to exactly one address. If so, it can inherit
  988. ** the primaryness of the address.
  989. **
  990. ** While we're at it, clear the QTHISPASS bits.
  991. */
  992. if (aliaslevel == 0)
  993. {
  994. int nrcpts = 0;
  995. ADDRESS *only = NULL;
  996. for (q = *sendq; q != NULL; q = q->q_next)
  997. {
  998. if (bitset(QTHISPASS, q->q_flags) &&
  999. QS_IS_SENDABLE(q->q_state))
  1000. {
  1001. nrcpts++;
  1002. only = q;
  1003. }
  1004. q->q_flags &= ~QTHISPASS;
  1005. }
  1006. if (nrcpts == 1)
  1007. {
  1008. /* check to see if this actually got a new owner */
  1009. q = only;
  1010. while ((q = q->q_alias) != NULL)
  1011. {
  1012. if (q->q_owner != NULL)
  1013. break;
  1014. }
  1015. if (q == NULL)
  1016. only->q_flags |= QPRIMARY;
  1017. }
  1018. else if (!initialdontsend && nrcpts > 0)
  1019. {
  1020. /* arrange for return receipt */
  1021. e->e_flags |= EF_SENDRECEIPT;
  1022. new->q_flags |= QEXPANDED;
  1023. if (e->e_xfp != NULL &&
  1024. bitset(QPINGONSUCCESS, new->q_flags))
  1025. (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
  1026. "%s... expanded to multiple addresses\n",
  1027. new->q_paddr);
  1028. }
  1029. }
  1030. new->q_flags |= QRCPTOK;
  1031. (void) sm_snprintf(buf0, sizeof(buf0), "%d", e->e_nrcpts);
  1032. macdefine(&e->e_macro, A_TEMP, macid("{nrcpts}"), buf0);
  1033. return new;
  1034. }
  1035. /*
  1036. ** FINDUSER -- find the password entry for a user.
  1037. **
  1038. ** This looks a lot like getpwnam, except that it may want to
  1039. ** do some fancier pattern matching in /etc/passwd.
  1040. **
  1041. ** This routine contains most of the time of many sendmail runs.
  1042. ** It deserves to be optimized.
  1043. **
  1044. ** Parameters:
  1045. ** name -- the name to match against.
  1046. ** fuzzyp -- an outarg that is set to true if this entry
  1047. ** was found using the fuzzy matching algorithm;
  1048. ** set to false otherwise.
  1049. ** user -- structure to fill in if user is found
  1050. **
  1051. ** Returns:
  1052. ** On success, fill in *user, set *fuzzyp and return EX_OK.
  1053. ** If the user was not found, return EX_NOUSER.
  1054. ** On error, return EX_TEMPFAIL or EX_OSERR.
  1055. **
  1056. ** Side Effects:
  1057. ** may modify name.
  1058. */
  1059. int
  1060. finduser(name, fuzzyp, user)
  1061. char *name;
  1062. bool *fuzzyp;
  1063. SM_MBDB_T *user;
  1064. {
  1065. #if MATCHGECOS
  1066. register struct passwd *pw;
  1067. #endif /* MATCHGECOS */
  1068. register char *p;
  1069. bool tryagain;
  1070. int status;
  1071. if (tTd(29, 4))
  1072. sm_dprintf("finduser(%s): ", name);
  1073. *fuzzyp = false;
  1074. #if HESIOD
  1075. /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
  1076. for (p = name; *p != '\0'; p++)
  1077. if (!isascii(*p) || !isdigit(*p))
  1078. break;
  1079. if (*p == '\0')
  1080. {
  1081. if (tTd(29, 4))
  1082. sm_dprintf("failed (numeric input)\n");
  1083. return EX_NOUSER;
  1084. }
  1085. #endif /* HESIOD */
  1086. /* look up this login name using fast path */
  1087. status = sm_mbdb_lookup(name, user);
  1088. if (status != EX_NOUSER)
  1089. {
  1090. if (tTd(29, 4))
  1091. sm_dprintf("%s (non-fuzzy)\n", sm_strexit(status));
  1092. return status;
  1093. }
  1094. /* try mapping it to lower case */
  1095. tryagain = false;
  1096. for (p = name; *p != '\0'; p++)
  1097. {
  1098. if (isascii(*p) && isupper(*p))
  1099. {
  1100. *p = tolower(*p);
  1101. tryagain = true;
  1102. }
  1103. }
  1104. if (tryagain && (status = sm_mbdb_lookup(name, user)) != EX_NOUSER)
  1105. {
  1106. if (tTd(29, 4))
  1107. sm_dprintf("%s (lower case)\n", sm_strexit(status));
  1108. *fuzzyp = true;
  1109. return status;
  1110. }
  1111. #if MATCHGECOS
  1112. /* see if fuzzy matching allowed */
  1113. if (!MatchGecos)
  1114. {
  1115. if (tTd(29, 4))
  1116. sm_dprintf("not found (fuzzy disabled)\n");
  1117. return EX_NOUSER;
  1118. }
  1119. /* search for a matching full name instead */
  1120. for (p = name; *p != '\0'; p++)
  1121. {
  1122. if (*p == (SpaceSub & 0177) || *p == '_')
  1123. *p = ' ';
  1124. }
  1125. (void) setpwent();
  1126. while ((pw = getpwent()) != NULL)
  1127. {
  1128. char buf[MAXNAME + 1];
  1129. # if 0
  1130. if (sm_strcasecmp(pw->pw_name, name) == 0)
  1131. {
  1132. if (tTd(29, 4))
  1133. sm_dprintf("found (case wrapped)\n");
  1134. break;
  1135. }
  1136. # endif /* 0 */
  1137. sm_pwfullname(pw->pw_gecos, pw->pw_name, buf, sizeof(buf));
  1138. if (strchr(buf, ' ') != NULL && sm_strcasecmp(buf, name) == 0)
  1139. {
  1140. if (tTd(29, 4))
  1141. sm_dprintf("fuzzy matches %s\n", pw->pw_name);
  1142. message("sending to login name %s", pw->pw_name);
  1143. break;
  1144. }
  1145. }
  1146. if (pw != NULL)
  1147. *fuzzyp = true;
  1148. else if (tTd(29, 4))
  1149. sm_dprintf("no fuzzy match found\n");
  1150. # if DEC_OSF_BROKEN_GETPWENT /* DEC OSF/1 3.2 or earlier */
  1151. endpwent();
  1152. # endif /* DEC_OSF_BROKEN_GETPWENT */
  1153. if (pw == NULL)
  1154. return EX_NOUSER;
  1155. sm_mbdb_frompw(user, pw);
  1156. return EX_OK;
  1157. #else /* MATCHGECOS */
  1158. if (tTd(29, 4))
  1159. sm_dprintf("not found (fuzzy disabled)\n");
  1160. return EX_NOUSER;
  1161. #endif /* MATCHGECOS */
  1162. }
  1163. /*
  1164. ** WRITABLE -- predicate returning if the file is writable.
  1165. **
  1166. ** This routine must duplicate the algorithm in sys/fio.c.
  1167. ** Unfortunately, we cannot use the access call since we
  1168. ** won't necessarily be the real uid when we try to
  1169. ** actually open the file.
  1170. **
  1171. ** Notice that ANY file with ANY execute bit is automatically
  1172. ** not writable. This is also enforced by mailfile.
  1173. **
  1174. ** Parameters:
  1175. ** filename -- the file name to check.
  1176. ** ctladdr -- the controlling address for this file.
  1177. ** flags -- SFF_* flags to control the function.
  1178. **
  1179. ** Returns:
  1180. ** true -- if we will be able to write this file.
  1181. ** false -- if we cannot write this file.
  1182. **
  1183. ** Side Effects:
  1184. ** none.
  1185. */
  1186. bool
  1187. writable(filename, ctladdr, flags)
  1188. char *filename;
  1189. ADDRESS *ctladdr;
  1190. long flags;
  1191. {
  1192. uid_t euid = 0;
  1193. gid_t egid = 0;
  1194. char *user = NULL;
  1195. if (tTd(44, 5))
  1196. sm_dprintf("writable(%s, 0x%lx)\n", filename, flags);
  1197. /*
  1198. ** File does exist -- check that it is writable.
  1199. */
  1200. if (geteuid() != 0)
  1201. {
  1202. euid = geteuid();
  1203. egid = getegid();
  1204. user = NULL;
  1205. }
  1206. else if (ctladdr != NULL)
  1207. {
  1208. euid = ctladdr->q_uid;
  1209. egid = ctladdr->q_gid;
  1210. user = ctladdr->q_user;
  1211. }
  1212. else if (bitset(SFF_RUNASREALUID, flags))
  1213. {
  1214. euid = RealUid;
  1215. egid = RealGid;
  1216. user = RealUserName;
  1217. }
  1218. else if (FileMailer != NULL && !bitset(SFF_ROOTOK, flags))
  1219. {
  1220. if (FileMailer->m_uid == NO_UID)
  1221. {
  1222. euid = DefUid;
  1223. user = DefUser;
  1224. }
  1225. else
  1226. {
  1227. euid = FileMailer->m_uid;
  1228. user = NULL;
  1229. }
  1230. if (FileMailer->m_gid == NO_GID)
  1231. egid = DefGid;
  1232. else
  1233. egid = FileMailer->m_gid;
  1234. }
  1235. else
  1236. {
  1237. euid = egid = 0;
  1238. user = NULL;
  1239. }
  1240. if (!bitset(SFF_ROOTOK, flags))
  1241. {
  1242. if (euid == 0)
  1243. {
  1244. euid = DefUid;
  1245. user = DefUser;
  1246. }
  1247. if (egid == 0)
  1248. egid = DefGid;
  1249. }
  1250. if (geteuid() == 0 &&
  1251. (ctladdr == NULL || !bitset(QGOODUID, ctladdr->q_flags)))
  1252. flags |= SFF_SETUIDOK;
  1253. if (!bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
  1254. flags |= SFF_NOSLINK;
  1255. if (!bitnset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail))
  1256. flags |= SFF_NOHLINK;
  1257. errno = safefile(filename, euid, egid, user, flags, S_IWRITE, NULL);
  1258. return errno == 0;
  1259. }
  1260. /*
  1261. ** INCLUDE -- handle :include: specification.
  1262. **
  1263. ** Parameters:
  1264. ** fname -- filename to include.
  1265. ** forwarding -- if true, we are reading a .forward file.
  1266. ** if false, it's a :include: file.
  1267. ** ctladdr -- address template to use to fill in these
  1268. ** addresses -- effective user/group id are
  1269. ** the important things.
  1270. ** sendq -- a pointer to the head of the send queue
  1271. ** to put these addresses in.
  1272. ** aliaslevel -- the alias nesting depth.
  1273. ** e -- the current envelope.
  1274. **
  1275. ** Returns:
  1276. ** open error status
  1277. **
  1278. ** Side Effects:
  1279. ** reads the :include: file and sends to everyone
  1280. ** listed in that file.
  1281. **
  1282. ** Security Note:
  1283. ** If you have restricted chown (that is, you can't
  1284. ** give a file away), it is reasonable to allow programs
  1285. ** and files called from this :include: file to be to be
  1286. ** run as the owner of the :include: file. This is bogus
  1287. ** if there is any chance of someone giving away a file.
  1288. ** We assume that pre-POSIX systems can give away files.
  1289. **
  1290. ** There is an additional restriction that if you
  1291. ** forward to a :include: file, it will not take on
  1292. ** the ownership of the :include: file. This may not
  1293. ** be necessary, but shouldn't hurt.
  1294. */
  1295. static jmp_buf CtxIncludeTimeout;
  1296. int
  1297. include(fname, forwarding, ctladdr, sendq, aliaslevel, e)
  1298. char *fname;
  1299. bool forwarding;
  1300. ADDRESS *ctladdr;
  1301. ADDRESS **sendq;
  1302. int aliaslevel;
  1303. ENVELOPE *e;
  1304. {
  1305. SM_FILE_T *volatile fp = NULL;
  1306. char *oldto = e->e_to;
  1307. char *oldfilename = FileName;
  1308. int oldlinenumber = LineNumber;
  1309. register SM_EVENT *ev = NULL;
  1310. int nincludes;
  1311. int mode;
  1312. volatile bool maxreached = false;
  1313. register ADDRESS *ca;
  1314. volatile uid_t saveduid;
  1315. volatile gid_t savedgid;
  1316. volatile uid_t uid;
  1317. volatile gid_t gid;
  1318. char *volatile user;
  1319. int rval = 0;
  1320. volatile long sfflags = SFF_REGONLY;
  1321. register char *p;
  1322. bool safechown = false;
  1323. volatile bool safedir = false;
  1324. struct stat st;
  1325. char buf[MAXLINE];
  1326. if (tTd(27, 2))
  1327. sm_dprintf("include(%s)\n", fname);
  1328. if (tTd(27, 4))
  1329. sm_dprintf(" ruid=%d euid=%d\n",
  1330. (int) getuid(), (int) geteuid());
  1331. if (tTd(27, 14))
  1332. {
  1333. sm_dprintf("ctladdr ");
  1334. printaddr(sm_debug_file(), ctladdr, false);
  1335. }
  1336. if (tTd(27, 9))
  1337. sm_dprintf("include: old uid = %d/%d\n",
  1338. (int) getuid(), (int) geteuid());
  1339. if (forwarding)
  1340. {
  1341. sfflags |= SFF_MUSTOWN|SFF_ROOTOK;
  1342. if (!bitnset(DBS_GROUPWRITABLEFORWARDFILE, DontBlameSendmail))
  1343. sfflags |= SFF_NOGWFILES;
  1344. if (!bitnset(DBS_WORLDWRITABLEFORWARDFILE, DontBlameSendmail))
  1345. sfflags |= SFF_NOWWFILES;
  1346. }
  1347. else
  1348. {
  1349. if (!bitnset(DBS_GROUPWRITABLEINCLUDEFILE, DontBlameSendmail))
  1350. sfflags |= SFF_NOGWFILES;
  1351. if (!bitnset(DBS_WORLDWRITABLEINCLUDEFILE, DontBlameSendmail))
  1352. sfflags |= SFF_NOWWFILES;
  1353. }
  1354. /*
  1355. ** If RunAsUser set, won't be able to run programs as user
  1356. ** so mark them as unsafe unless the administrator knows better.
  1357. */
  1358. if ((geteuid() != 0 || RunAsUid != 0) &&
  1359. !bitnset(DBS_NONROOTSAFEADDR, DontBlameSendmail))
  1360. {
  1361. if (tTd(27, 4))
  1362. sm_dprintf("include: not safe (euid=%d, RunAsUid=%d)\n",
  1363. (int) geteuid(), (int) RunAsUid);
  1364. ctladdr->q_flags |= QUNSAFEADDR;
  1365. }
  1366. ca = getctladdr(ctladdr);
  1367. if (ca == NULL ||
  1368. (ca->q_uid == DefUid && ca->q_gid == 0))
  1369. {
  1370. uid = DefUid;
  1371. gid = DefGid;
  1372. user = DefUser;
  1373. }
  1374. else
  1375. {
  1376. uid = ca->q_uid;
  1377. gid = ca->q_gid;
  1378. user = ca->q_user;
  1379. }
  1380. #if MAILER_SETUID_METHOD != USE_SETUID
  1381. saveduid = geteuid();
  1382. savedgid = getegid();
  1383. if (saveduid == 0)
  1384. {
  1385. if (!DontInitGroups)
  1386. {
  1387. if (initgroups(user, gid) == -1)
  1388. {
  1389. rval = EAGAIN;
  1390. syserr("include: initgroups(%s, %d) failed",
  1391. user, gid);
  1392. goto resetuid;
  1393. }
  1394. }
  1395. else
  1396. {
  1397. GIDSET_T gidset[1];
  1398. gidset[0] = gid;
  1399. if (setgroups(1, gidset) == -1)
  1400. {
  1401. rval = EAGAIN;
  1402. syserr("include: setgroups() failed");
  1403. goto resetuid;
  1404. }
  1405. }
  1406. if (gid != 0 && setgid(gid) < -1)
  1407. {
  1408. rval = EAGAIN;
  1409. syserr("setgid(%d) failure", gid);
  1410. goto resetuid;
  1411. }
  1412. if (uid != 0)
  1413. {
  1414. # if MAILER_SETUID_METHOD == USE_SETEUID
  1415. if (seteuid(uid) < 0)
  1416. {
  1417. rval = EAGAIN;
  1418. syserr("seteuid(%d) failure (real=%d, eff=%d)",
  1419. uid, (int) getuid(), (int) geteuid());
  1420. goto resetuid;
  1421. }
  1422. # endif /* MAILER_SETUID_METHOD == USE_SETEUID */
  1423. # if MAILER_SETUID_METHOD == USE_SETREUID
  1424. if (setreuid(0, uid) < 0)
  1425. {
  1426. rval = EAGAIN;
  1427. syserr("setreuid(0, %d) failure (real=%d, eff=%d)",
  1428. uid, (int) getuid(), (int) geteuid());
  1429. goto resetuid;
  1430. }
  1431. # endif /* MAILER_SETUID_METHOD == USE_SETREUID */
  1432. }
  1433. }
  1434. #endif /* MAILER_SETUID_METHOD != USE_SETUID */
  1435. if (tTd(27, 9))
  1436. sm_dprintf("include: new uid = %d/%d\n",
  1437. (int) getuid(), (int) geteuid());
  1438. /*
  1439. ** If home directory is remote mounted but server is down,
  1440. ** this can hang or give errors; use a timeout to avoid this
  1441. */
  1442. if (setjmp(CtxIncludeTimeout) != 0)
  1443. {
  1444. ctladdr->q_state = QS_QUEUEUP;
  1445. errno = 0;
  1446. /* return pseudo-error code */
  1447. rval = E_SM_OPENTIMEOUT;
  1448. goto resetuid;
  1449. }
  1450. if (TimeOuts.to_fileopen > 0)
  1451. ev = sm_setevent(TimeOuts.to_fileopen, includetimeout, 0);
  1452. else
  1453. ev = NULL;
  1454. /* check for writable parent directory */
  1455. p = strrchr(fname, '/');
  1456. if (p != NULL)
  1457. {
  1458. int ret;
  1459. *p = '\0';
  1460. ret = safedirpath(fname, uid, gid, user,
  1461. sfflags|SFF_SAFEDIRPATH, 0, 0);
  1462. if (ret == 0)
  1463. {
  1464. /* in safe directory: relax chown & link rules */
  1465. safedir = true;
  1466. sfflags |= SFF_NOPATHCHECK;
  1467. }
  1468. else
  1469. {
  1470. if (bitnset((forwarding ?
  1471. DBS_FORWARDFILEINUNSAFEDIRPATH :
  1472. DBS_INCLUDEFILEINUNSAFEDIRPATH),
  1473. DontBlameSendmail))
  1474. sfflags |= SFF_NOPATHCHECK;
  1475. else if (bitnset((forwarding ?
  1476. DBS_FORWARDFILEINGROUPWRITABLEDIRPATH :
  1477. DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH),
  1478. DontBlameSendmail) &&
  1479. ret == E_SM_GWDIR)
  1480. {
  1481. setbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
  1482. DontBlameSendmail);
  1483. ret = safedirpath(fname, uid, gid, user,
  1484. sfflags|SFF_SAFEDIRPATH,
  1485. 0, 0);
  1486. clrbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
  1487. DontBlameSendmail);
  1488. if (ret == 0)
  1489. sfflags |= SFF_NOPATHCHECK;
  1490. else
  1491. sfflags |= SFF_SAFEDIRPATH;
  1492. }
  1493. else
  1494. sfflags |= SFF_SAFEDIRPATH;
  1495. if (ret > E_PSEUDOBASE &&
  1496. !bitnset((forwarding ?
  1497. DBS_FORWARDFILEINUNSAFEDIRPATHSAFE :
  1498. DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE),
  1499. DontBlameSendmail))
  1500. {
  1501. if (LogLevel > 11)
  1502. sm_syslog(LOG_INFO, e->e_id,
  1503. "%s: unsafe directory path, marked unsafe",
  1504. shortenstring(fname, MAXSHORTSTR));
  1505. ctladdr->q_flags |= QUNSAFEADDR;
  1506. }
  1507. }
  1508. *p = '/';
  1509. }
  1510. /* allow links only in unwritable directories */
  1511. if (!safedir &&
  1512. !bitnset((forwarding ?
  1513. DBS_LINKEDFORWARDFILEINWRITABLEDIR :
  1514. DBS_LINKEDINCLUDEFILEINWRITABLEDIR),
  1515. DontBlameSendmail))
  1516. sfflags |= SFF_NOLINK;
  1517. rval = safefile(fname, uid, gid, user, sfflags, S_IREAD, &st);
  1518. if (rval != 0)
  1519. {
  1520. /* don't use this :include: file */
  1521. if (tTd(27, 4))
  1522. sm_dprintf("include: not safe (uid=%d): %s\n",
  1523. (int) uid, sm_errstring(rval));
  1524. }
  1525. else if ((fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fname,
  1526. SM_IO_RDONLY, NULL)) == NULL)
  1527. {
  1528. rval = errno;
  1529. if (tTd(27, 4))
  1530. sm_dprintf("include: open: %s\n", sm_errstring(rval));
  1531. }
  1532. else if (filechanged(fname, sm_io_getinfo(fp,SM_IO_WHAT_FD, NULL), &st))
  1533. {
  1534. rval = E_SM_FILECHANGE;
  1535. if (tTd(27, 4))
  1536. sm_dprintf("include: file changed after open\n");
  1537. }
  1538. if (ev != NULL)
  1539. sm_clrevent(ev);
  1540. resetuid:
  1541. #if HASSETREUID || USESETEUID
  1542. if (saveduid == 0)
  1543. {
  1544. if (uid != 0)
  1545. {
  1546. # if USESETEUID
  1547. if (seteuid(0) < 0)
  1548. syserr("!seteuid(0) failure (real=%d, eff=%d)",
  1549. (int) getuid(), (int) geteuid());
  1550. # else /* USESETEUID */
  1551. if (setreuid(-1, 0) < 0)
  1552. syserr("!setreuid(-1, 0) failure (real=%d, eff=%d)",
  1553. (int) getuid(), (int) geteuid());
  1554. if (setreuid(RealUid, 0) < 0)
  1555. syserr("!setreuid(%d, 0) failure (real=%d, eff=%d)",
  1556. (int) RealUid, (int) getuid(),
  1557. (int) geteuid());
  1558. # endif /* USESETEUID */
  1559. }
  1560. if (setgid(savedgid) < 0)
  1561. syserr("!setgid(%d) failure (real=%d eff=%d)",
  1562. (int) savedgid, (int) getgid(),
  1563. (int) getegid());
  1564. }
  1565. #endif /* HASSETREUID || USESETEUID */
  1566. if (tTd(27, 9))
  1567. sm_dprintf("include: reset uid = %d/%d\n",
  1568. (int) getuid(), (int) geteuid());
  1569. if (rval == E_SM_OPENTIMEOUT)
  1570. usrerr("451 4.4.1 open timeout on %s", fname);
  1571. if (fp == NULL)
  1572. return rval;
  1573. if (fstat(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), &st) < 0)
  1574. {
  1575. rval = errno;
  1576. syserr("Cannot fstat %s!", fname);
  1577. (void) sm_io_close(fp, SM_TIME_DEFAULT);
  1578. return rval;
  1579. }
  1580. /* if path was writable, check to avoid file giveaway tricks */
  1581. safechown = chownsafe(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), safedir);
  1582. if (tTd(27, 6))
  1583. sm_dprintf("include: parent of %s is %s, chown is %ssafe\n",
  1584. fname, safedir ? "safe" : "dangerous",
  1585. safechown ? "" : "un");
  1586. /* if no controlling user or coming from an alias delivery */
  1587. if (safechown &&
  1588. (ca == NULL ||
  1589. (ca->q_uid == DefUid && ca->q_gid == 0)))
  1590. {
  1591. ctladdr->q_uid = st.st_uid;
  1592. ctladdr->q_gid = st.st_gid;
  1593. ctladdr->q_flags |= QGOODUID;
  1594. }
  1595. if (ca != NULL && ca->q_uid == st.st_uid)
  1596. {
  1597. /* optimization -- avoid getpwuid if we already have info */
  1598. ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
  1599. ctladdr->q_ruser = ca->q_ruser;
  1600. }
  1601. else if (!forwarding)
  1602. {
  1603. register struct passwd *pw;
  1604. pw = sm_getpwuid(st.st_uid);
  1605. if (pw == NULL)
  1606. {
  1607. ctladdr->q_uid = st.st_uid;
  1608. ctladdr->q_flags |= QBOGUSSHELL;
  1609. }
  1610. else
  1611. {
  1612. char *sh;
  1613. ctladdr->q_ruser = sm_rpool_strdup_x(e->e_rpool,
  1614. pw->pw_name);
  1615. if (safechown)
  1616. sh = pw->pw_shell;
  1617. else
  1618. sh = "/SENDMAIL/ANY/SHELL/";
  1619. if (!usershellok(pw->pw_name, sh))
  1620. {
  1621. if (LogLevel > 11)
  1622. sm_syslog(LOG_INFO, e->e_id,
  1623. "%s: user %s has bad shell %s, marked %s",
  1624. shortenstring(fname,
  1625. MAXSHORTSTR),
  1626. pw->pw_name, sh,
  1627. safechown ? "bogus" : "unsafe");
  1628. if (safechown)
  1629. ctladdr->q_flags |= QBOGUSSHELL;
  1630. else
  1631. ctladdr->q_flags |= QUNSAFEADDR;
  1632. }
  1633. }
  1634. }
  1635. if (bitset(EF_VRFYONLY, e->e_flags))
  1636. {
  1637. /* don't do any more now */
  1638. ctladdr->q_state = QS_VERIFIED;
  1639. e->e_nrcpts++;
  1640. (void) sm_io_close(fp, SM_TIME_DEFAULT);
  1641. return rval;
  1642. }
  1643. /*
  1644. ** Check to see if some bad guy can write this file
  1645. **
  1646. ** Group write checking could be more clever, e.g.,
  1647. ** guessing as to which groups are actually safe ("sys"
  1648. ** may be; "user" probably is not).
  1649. */
  1650. mode = S_IWOTH;
  1651. if (!bitnset((forwarding ?
  1652. DBS_GROUPWRITABLEFORWARDFILESAFE :
  1653. DBS_GROUPWRITABLEINCLUDEFILESAFE),
  1654. DontBlameSendmail))
  1655. mode |= S_IWGRP;
  1656. if (bitset(mode, st.st_mode))
  1657. {
  1658. if (tTd(27, 6))
  1659. sm_dprintf("include: %s is %s writable, marked unsafe\n",
  1660. shortenstring(fname, MAXSHORTSTR),
  1661. bitset(S_IWOTH, st.st_mode) ? "world"
  1662. : "group");
  1663. if (LogLevel > 11)
  1664. sm_syslog(LOG_INFO, e->e_id,
  1665. "%s: %s writable %s file, marked unsafe",
  1666. shortenstring(fname, MAXSHORTSTR),
  1667. bitset(S_IWOTH, st.st_mode) ? "world" : "group",
  1668. forwarding ? "forward" : ":include:");
  1669. ctladdr->q_flags |= QUNSAFEADDR;
  1670. }
  1671. /* read the file -- each line is a comma-separated list. */
  1672. FileName = fname;
  1673. LineNumber = 0;
  1674. ctladdr->q_flags &= ~QSELFREF;
  1675. nincludes = 0;
  1676. while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL &&
  1677. !maxreached)
  1678. {
  1679. fixcrlf(buf, true);
  1680. LineNumber++;
  1681. if (buf[0] == '#' || buf[0] == '\0')
  1682. continue;
  1683. /* <sp>#@# introduces a comment anywhere */
  1684. /* for Japanese character sets */
  1685. for (p = buf; (p = strchr(++p, '#')) != NULL; )
  1686. {
  1687. if (p[1] == '@' && p[2] == '#' &&
  1688. isascii(p[-1]) && isspace(p[-1]) &&
  1689. (p[3] == '\0' || (isascii(p[3]) && isspace(p[3]))))
  1690. {
  1691. --p;
  1692. while (p > buf && isascii(p[-1]) &&
  1693. isspace(p[-1]))
  1694. --p;
  1695. p[0] = '\0';
  1696. break;
  1697. }
  1698. }
  1699. if (buf[0] == '\0')
  1700. continue;
  1701. e->e_to = NULL;
  1702. message("%s to %s",
  1703. forwarding ? "forwarding" : "sending", buf);
  1704. if (forwarding && LogLevel > 10)
  1705. sm_syslog(LOG_INFO, e->e_id,
  1706. "forward %.200s => %s",
  1707. oldto, shortenstring(buf, MAXSHORTSTR));
  1708. nincludes += sendtolist(buf, ctladdr, sendq, aliaslevel + 1, e);
  1709. if (forwarding &&
  1710. MaxForwardEntries > 0 &&
  1711. nincludes >= MaxForwardEntries)
  1712. {
  1713. /* just stop reading and processing further entries */
  1714. #if 0
  1715. /* additional: (?) */
  1716. ctladdr->q_state = QS_DONTSEND;
  1717. #endif /* 0 */
  1718. syserr("Attempt to forward to more than %d addresses (in %s)!",
  1719. MaxForwardEntries, fname);
  1720. maxreached = true;
  1721. }
  1722. }
  1723. if (sm_io_error(fp) && tTd(27, 3))
  1724. sm_dprintf("include: read error: %s\n", sm_errstring(errno));
  1725. if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
  1726. {
  1727. if (aliaslevel <= MaxAliasRecursion ||
  1728. ctladdr->q_state != QS_BADADDR)
  1729. {
  1730. ctladdr->q_state = QS_DONTSEND;
  1731. if (tTd(27, 5))
  1732. {
  1733. sm_dprintf("include: QS_DONTSEND ");
  1734. printaddr(sm_debug_file(), ctladdr, false);
  1735. }
  1736. }
  1737. }
  1738. (void) sm_io_close(fp, SM_TIME_DEFAULT);
  1739. FileName = oldfilename;
  1740. LineNumber = oldlinenumber;
  1741. e->e_to = oldto;
  1742. return rval;
  1743. }
  1744. static void
  1745. includetimeout(ignore)
  1746. int ignore;
  1747. {
  1748. /*
  1749. ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
  1750. ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
  1751. ** DOING.
  1752. */
  1753. errno = ETIMEDOUT;
  1754. longjmp(CtxIncludeTimeout, 1);
  1755. }
  1756. /*
  1757. ** SENDTOARGV -- send to an argument vector.
  1758. **
  1759. ** Parameters:
  1760. ** argv -- argument vector to send to.
  1761. ** e -- the current envelope.
  1762. **
  1763. ** Returns:
  1764. ** none.
  1765. **
  1766. ** Side Effects:
  1767. ** puts all addresses on the argument vector onto the
  1768. ** send queue.
  1769. */
  1770. void
  1771. sendtoargv(argv, e)
  1772. register char **argv;
  1773. register ENVELOPE *e;
  1774. {
  1775. register char *p;
  1776. while ((p = *argv++) != NULL)
  1777. (void) sendtolist(p, NULLADDR, &e->e_sendqueue, 0, e);
  1778. }
  1779. /*
  1780. ** GETCTLADDR -- get controlling address from an address header.
  1781. **
  1782. ** If none, get one corresponding to the effective userid.
  1783. **
  1784. ** Parameters:
  1785. ** a -- the address to find the controller of.
  1786. **
  1787. ** Returns:
  1788. ** the controlling address.
  1789. */
  1790. ADDRESS *
  1791. getctladdr(a)
  1792. register ADDRESS *a;
  1793. {
  1794. while (a != NULL && !bitset(QGOODUID, a->q_flags))
  1795. a = a->q_alias;
  1796. return a;
  1797. }
  1798. /*
  1799. ** SELF_REFERENCE -- check to see if an address references itself
  1800. **
  1801. ** The check is done through a chain of aliases. If it is part of
  1802. ** a loop, break the loop at the "best" address, that is, the one
  1803. ** that exists as a real user.
  1804. **
  1805. ** This is to handle the case of:
  1806. ** awc: Andrew.Chang
  1807. ** Andrew.Chang: awc@mail.server
  1808. ** which is a problem only on mail.server.
  1809. **
  1810. ** Parameters:
  1811. ** a -- the address to check.
  1812. **
  1813. ** Returns:
  1814. ** The address that should be retained.
  1815. */
  1816. static ADDRESS *
  1817. self_reference(a)
  1818. ADDRESS *a;
  1819. {
  1820. ADDRESS *b; /* top entry in self ref loop */
  1821. ADDRESS *c; /* entry that point to a real mail box */
  1822. if (tTd(27, 1))
  1823. sm_dprintf("self_reference(%s)\n", a->q_paddr);
  1824. for (b = a->q_alias; b != NULL; b = b->q_alias)
  1825. {
  1826. if (sameaddr(a, b))
  1827. break;
  1828. }
  1829. if (b == NULL)
  1830. {
  1831. if (tTd(27, 1))
  1832. sm_dprintf("\t... no self ref\n");
  1833. return NULL;
  1834. }
  1835. /*
  1836. ** Pick the first address that resolved to a real mail box
  1837. ** i.e has a mbdb entry. The returned value will be marked
  1838. ** QSELFREF in recipient(), which in turn will disable alias()
  1839. ** from marking it as QS_IS_DEAD(), which mean it will be used
  1840. ** as a deliverable address.
  1841. **
  1842. ** The 2 key thing to note here are:
  1843. ** 1) we are in a recursive call sequence:
  1844. ** alias->sendtolist->recipient->alias
  1845. ** 2) normally, when we return back to alias(), the address
  1846. ** will be marked QS_EXPANDED, since alias() assumes the
  1847. ** expanded form will be used instead of the current address.
  1848. ** This behaviour is turned off if the address is marked
  1849. ** QSELFREF. We set QSELFREF when we return to recipient().
  1850. */
  1851. c = a;
  1852. while (c != NULL)
  1853. {
  1854. if (tTd(27, 10))
  1855. sm_dprintf(" %s", c->q_user);
  1856. if (bitnset(M_HASPWENT, c->q_mailer->m_flags))
  1857. {
  1858. SM_MBDB_T user;
  1859. if (tTd(27, 2))
  1860. sm_dprintf("\t... getpwnam(%s)... ", c->q_user);
  1861. if (sm_mbdb_lookup(c->q_user, &user) == EX_OK)
  1862. {
  1863. if (tTd(27, 2))
  1864. sm_dprintf("found\n");
  1865. /* ought to cache results here */
  1866. if (sameaddr(b, c))
  1867. return b;
  1868. else
  1869. return c;
  1870. }
  1871. if (tTd(27, 2))
  1872. sm_dprintf("failed\n");
  1873. }
  1874. else
  1875. {
  1876. /* if local delivery, compare usernames */
  1877. if (bitnset(M_LOCALMAILER, c->q_mailer->m_flags) &&
  1878. b->q_mailer == c->q_mailer)
  1879. {
  1880. if (tTd(27, 2))
  1881. sm_dprintf("\t... local match (%s)\n",
  1882. c->q_user);
  1883. if (sameaddr(b, c))
  1884. return b;
  1885. else
  1886. return c;
  1887. }
  1888. }
  1889. if (tTd(27, 10))
  1890. sm_dprintf("\n");
  1891. c = c->q_alias;
  1892. }
  1893. if (tTd(27, 1))
  1894. sm_dprintf("\t... cannot break loop for \"%s\"\n", a->q_paddr);
  1895. return NULL;
  1896. }