PageRenderTime 73ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/src/exim.c

https://gitlab.com/Exim/exim
C | 5670 lines | 5194 code | 164 blank | 312 comment | 133 complexity | fc7ec81c449b6f2bf8adf30630933d1f MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*************************************************
  2. * Exim - an Internet mail transport agent *
  3. *************************************************/
  4. /* Copyright (c) University of Cambridge 1995 - 2014 */
  5. /* See the file NOTICE for conditions of use and distribution. */
  6. /* The main function: entry point, initialization, and high-level control.
  7. Also a few functions that don't naturally fit elsewhere. */
  8. #include "exim.h"
  9. #ifdef USE_GNUTLS
  10. # include <gnutls/gnutls.h>
  11. # if GNUTLS_VERSION_NUMBER < 0x030103 && !defined(DISABLE_OCSP)
  12. # define DISABLE_OCSP
  13. # endif
  14. #endif
  15. extern void init_lookup_list(void);
  16. /*************************************************
  17. * Function interface to store functions *
  18. *************************************************/
  19. /* We need some real functions to pass to the PCRE regular expression library
  20. for store allocation via Exim's store manager. The normal calls are actually
  21. macros that pass over location information to make tracing easier. These
  22. functions just interface to the standard macro calls. A good compiler will
  23. optimize out the tail recursion and so not make them too expensive. There
  24. are two sets of functions; one for use when we want to retain the compiled
  25. regular expression for a long time; the other for short-term use. */
  26. static void *
  27. function_store_get(size_t size)
  28. {
  29. return store_get((int)size);
  30. }
  31. static void
  32. function_dummy_free(void *block) { block = block; }
  33. static void *
  34. function_store_malloc(size_t size)
  35. {
  36. return store_malloc((int)size);
  37. }
  38. static void
  39. function_store_free(void *block)
  40. {
  41. store_free(block);
  42. }
  43. /*************************************************
  44. * Enums for cmdline interface *
  45. *************************************************/
  46. enum commandline_info { CMDINFO_NONE=0,
  47. CMDINFO_HELP, CMDINFO_SIEVE, CMDINFO_DSCP };
  48. /*************************************************
  49. * Compile regular expression and panic on fail *
  50. *************************************************/
  51. /* This function is called when failure to compile a regular expression leads
  52. to a panic exit. In other cases, pcre_compile() is called directly. In many
  53. cases where this function is used, the results of the compilation are to be
  54. placed in long-lived store, so we temporarily reset the store management
  55. functions that PCRE uses if the use_malloc flag is set.
  56. Argument:
  57. pattern the pattern to compile
  58. caseless TRUE if caseless matching is required
  59. use_malloc TRUE if compile into malloc store
  60. Returns: pointer to the compiled pattern
  61. */
  62. const pcre *
  63. regex_must_compile(const uschar *pattern, BOOL caseless, BOOL use_malloc)
  64. {
  65. int offset;
  66. int options = PCRE_COPT;
  67. const pcre *yield;
  68. const uschar *error;
  69. if (use_malloc)
  70. {
  71. pcre_malloc = function_store_malloc;
  72. pcre_free = function_store_free;
  73. }
  74. if (caseless) options |= PCRE_CASELESS;
  75. yield = pcre_compile(CCS pattern, options, (const char **)&error, &offset, NULL);
  76. pcre_malloc = function_store_get;
  77. pcre_free = function_dummy_free;
  78. if (yield == NULL)
  79. log_write(0, LOG_MAIN|LOG_PANIC_DIE, "regular expression error: "
  80. "%s at offset %d while compiling %s", error, offset, pattern);
  81. return yield;
  82. }
  83. /*************************************************
  84. * Execute regular expression and set strings *
  85. *************************************************/
  86. /* This function runs a regular expression match, and sets up the pointers to
  87. the matched substrings.
  88. Arguments:
  89. re the compiled expression
  90. subject the subject string
  91. options additional PCRE options
  92. setup if < 0 do full setup
  93. if >= 0 setup from setup+1 onwards,
  94. excluding the full matched string
  95. Returns: TRUE or FALSE
  96. */
  97. BOOL
  98. regex_match_and_setup(const pcre *re, const uschar *subject, int options, int setup)
  99. {
  100. int ovector[3*(EXPAND_MAXN+1)];
  101. uschar * s = string_copy(subject); /* de-constifying */
  102. int n = pcre_exec(re, NULL, CS s, Ustrlen(s), 0,
  103. PCRE_EOPT | options, ovector, sizeof(ovector)/sizeof(int));
  104. BOOL yield = n >= 0;
  105. if (n == 0) n = EXPAND_MAXN + 1;
  106. if (yield)
  107. {
  108. int nn;
  109. expand_nmax = (setup < 0)? 0 : setup + 1;
  110. for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2)
  111. {
  112. expand_nstring[expand_nmax] = s + ovector[nn];
  113. expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
  114. }
  115. expand_nmax--;
  116. }
  117. return yield;
  118. }
  119. /*************************************************
  120. * Set up processing details *
  121. *************************************************/
  122. /* Save a text string for dumping when SIGUSR1 is received.
  123. Do checks for overruns.
  124. Arguments: format and arguments, as for printf()
  125. Returns: nothing
  126. */
  127. void
  128. set_process_info(const char *format, ...)
  129. {
  130. int len;
  131. va_list ap;
  132. sprintf(CS process_info, "%5d ", (int)getpid());
  133. len = Ustrlen(process_info);
  134. va_start(ap, format);
  135. if (!string_vformat(process_info + len, PROCESS_INFO_SIZE - len - 2, format, ap))
  136. Ustrcpy(process_info + len, "**** string overflowed buffer ****");
  137. len = Ustrlen(process_info);
  138. process_info[len+0] = '\n';
  139. process_info[len+1] = '\0';
  140. process_info_len = len + 1;
  141. DEBUG(D_process_info) debug_printf("set_process_info: %s", process_info);
  142. va_end(ap);
  143. }
  144. /*************************************************
  145. * Handler for SIGUSR1 *
  146. *************************************************/
  147. /* SIGUSR1 causes any exim process to write to the process log details of
  148. what it is currently doing. It will only be used if the OS is capable of
  149. setting up a handler that causes automatic restarting of any system call
  150. that is in progress at the time.
  151. This function takes care to be signal-safe.
  152. Argument: the signal number (SIGUSR1)
  153. Returns: nothing
  154. */
  155. static void
  156. usr1_handler(int sig)
  157. {
  158. int fd;
  159. os_restarting_signal(sig, usr1_handler);
  160. fd = Uopen(process_log_path, O_APPEND|O_WRONLY, LOG_MODE);
  161. if (fd < 0)
  162. {
  163. /* If we are already running as the Exim user, try to create it in the
  164. current process (assuming spool_directory exists). Otherwise, if we are
  165. root, do the creation in an exim:exim subprocess. */
  166. int euid = geteuid();
  167. if (euid == exim_uid)
  168. fd = Uopen(process_log_path, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE);
  169. else if (euid == root_uid)
  170. fd = log_create_as_exim(process_log_path);
  171. }
  172. /* If we are neither exim nor root, or if we failed to create the log file,
  173. give up. There is not much useful we can do with errors, since we don't want
  174. to disrupt whatever is going on outside the signal handler. */
  175. if (fd < 0) return;
  176. {int dummy = write(fd, process_info, process_info_len); dummy = dummy; }
  177. (void)close(fd);
  178. }
  179. /*************************************************
  180. * Timeout handler *
  181. *************************************************/
  182. /* This handler is enabled most of the time that Exim is running. The handler
  183. doesn't actually get used unless alarm() has been called to set a timer, to
  184. place a time limit on a system call of some kind. When the handler is run, it
  185. re-enables itself.
  186. There are some other SIGALRM handlers that are used in special cases when more
  187. than just a flag setting is required; for example, when reading a message's
  188. input. These are normally set up in the code module that uses them, and the
  189. SIGALRM handler is reset to this one afterwards.
  190. Argument: the signal value (SIGALRM)
  191. Returns: nothing
  192. */
  193. void
  194. sigalrm_handler(int sig)
  195. {
  196. sig = sig; /* Keep picky compilers happy */
  197. sigalrm_seen = TRUE;
  198. os_non_restarting_signal(SIGALRM, sigalrm_handler);
  199. }
  200. /*************************************************
  201. * Sleep for a fractional time interval *
  202. *************************************************/
  203. /* This function is called by millisleep() and exim_wait_tick() to wait for a
  204. period of time that may include a fraction of a second. The coding is somewhat
  205. tedious. We do not expect setitimer() ever to fail, but if it does, the process
  206. will wait for ever, so we panic in this instance. (There was a case of this
  207. when a bug in a function that calls milliwait() caused it to pass invalid data.
  208. That's when I added the check. :-)
  209. We assume it to be not worth sleeping for under 100us; this value will
  210. require revisiting as hardware advances. This avoids the issue of
  211. a zero-valued timer setting meaning "never fire".
  212. Argument: an itimerval structure containing the interval
  213. Returns: nothing
  214. */
  215. static void
  216. milliwait(struct itimerval *itval)
  217. {
  218. sigset_t sigmask;
  219. sigset_t old_sigmask;
  220. if (itval->it_value.tv_usec < 100 && itval->it_value.tv_sec == 0)
  221. return;
  222. (void)sigemptyset(&sigmask); /* Empty mask */
  223. (void)sigaddset(&sigmask, SIGALRM); /* Add SIGALRM */
  224. (void)sigprocmask(SIG_BLOCK, &sigmask, &old_sigmask); /* Block SIGALRM */
  225. if (setitimer(ITIMER_REAL, itval, NULL) < 0) /* Start timer */
  226. log_write(0, LOG_MAIN|LOG_PANIC_DIE,
  227. "setitimer() failed: %s", strerror(errno));
  228. (void)sigfillset(&sigmask); /* All signals */
  229. (void)sigdelset(&sigmask, SIGALRM); /* Remove SIGALRM */
  230. (void)sigsuspend(&sigmask); /* Until SIGALRM */
  231. (void)sigprocmask(SIG_SETMASK, &old_sigmask, NULL); /* Restore mask */
  232. }
  233. /*************************************************
  234. * Millisecond sleep function *
  235. *************************************************/
  236. /* The basic sleep() function has a granularity of 1 second, which is too rough
  237. in some cases - for example, when using an increasing delay to slow down
  238. spammers.
  239. Argument: number of millseconds
  240. Returns: nothing
  241. */
  242. void
  243. millisleep(int msec)
  244. {
  245. struct itimerval itval;
  246. itval.it_interval.tv_sec = 0;
  247. itval.it_interval.tv_usec = 0;
  248. itval.it_value.tv_sec = msec/1000;
  249. itval.it_value.tv_usec = (msec % 1000) * 1000;
  250. milliwait(&itval);
  251. }
  252. /*************************************************
  253. * Compare microsecond times *
  254. *************************************************/
  255. /*
  256. Arguments:
  257. tv1 the first time
  258. tv2 the second time
  259. Returns: -1, 0, or +1
  260. */
  261. int
  262. exim_tvcmp(struct timeval *t1, struct timeval *t2)
  263. {
  264. if (t1->tv_sec > t2->tv_sec) return +1;
  265. if (t1->tv_sec < t2->tv_sec) return -1;
  266. if (t1->tv_usec > t2->tv_usec) return +1;
  267. if (t1->tv_usec < t2->tv_usec) return -1;
  268. return 0;
  269. }
  270. /*************************************************
  271. * Clock tick wait function *
  272. *************************************************/
  273. /* Exim uses a time + a pid to generate a unique identifier in two places: its
  274. message IDs, and in file names for maildir deliveries. Because some OS now
  275. re-use pids within the same second, sub-second times are now being used.
  276. However, for absolute certaintly, we must ensure the clock has ticked before
  277. allowing the relevant process to complete. At the time of implementation of
  278. this code (February 2003), the speed of processors is such that the clock will
  279. invariably have ticked already by the time a process has done its job. This
  280. function prepares for the time when things are faster - and it also copes with
  281. clocks that go backwards.
  282. Arguments:
  283. then_tv A timeval which was used to create uniqueness; its usec field
  284. has been rounded down to the value of the resolution.
  285. We want to be sure the current time is greater than this.
  286. resolution The resolution that was used to divide the microseconds
  287. (1 for maildir, larger for message ids)
  288. Returns: nothing
  289. */
  290. void
  291. exim_wait_tick(struct timeval *then_tv, int resolution)
  292. {
  293. struct timeval now_tv;
  294. long int now_true_usec;
  295. (void)gettimeofday(&now_tv, NULL);
  296. now_true_usec = now_tv.tv_usec;
  297. now_tv.tv_usec = (now_true_usec/resolution) * resolution;
  298. if (exim_tvcmp(&now_tv, then_tv) <= 0)
  299. {
  300. struct itimerval itval;
  301. itval.it_interval.tv_sec = 0;
  302. itval.it_interval.tv_usec = 0;
  303. itval.it_value.tv_sec = then_tv->tv_sec - now_tv.tv_sec;
  304. itval.it_value.tv_usec = then_tv->tv_usec + resolution - now_true_usec;
  305. /* We know that, overall, "now" is less than or equal to "then". Therefore, a
  306. negative value for the microseconds is possible only in the case when "now"
  307. is more than a second less than "then". That means that itval.it_value.tv_sec
  308. is greater than zero. The following correction is therefore safe. */
  309. if (itval.it_value.tv_usec < 0)
  310. {
  311. itval.it_value.tv_usec += 1000000;
  312. itval.it_value.tv_sec -= 1;
  313. }
  314. DEBUG(D_transport|D_receive)
  315. {
  316. if (!running_in_test_harness)
  317. {
  318. debug_printf("tick check: " TIME_T_FMT ".%06lu " TIME_T_FMT ".%06lu\n",
  319. then_tv->tv_sec, (long) then_tv->tv_usec,
  320. now_tv.tv_sec, (long) now_tv.tv_usec);
  321. debug_printf("waiting " TIME_T_FMT ".%06lu\n",
  322. itval.it_value.tv_sec, (long) itval.it_value.tv_usec);
  323. }
  324. }
  325. milliwait(&itval);
  326. }
  327. }
  328. /*************************************************
  329. * Call fopen() with umask 777 and adjust mode *
  330. *************************************************/
  331. /* Exim runs with umask(0) so that files created with open() have the mode that
  332. is specified in the open() call. However, there are some files, typically in
  333. the spool directory, that are created with fopen(). They end up world-writeable
  334. if no precautions are taken. Although the spool directory is not accessible to
  335. the world, this is an untidiness. So this is a wrapper function for fopen()
  336. that sorts out the mode of the created file.
  337. Arguments:
  338. filename the file name
  339. options the fopen() options
  340. mode the required mode
  341. Returns: the fopened FILE or NULL
  342. */
  343. FILE *
  344. modefopen(const uschar *filename, const char *options, mode_t mode)
  345. {
  346. mode_t saved_umask = umask(0777);
  347. FILE *f = Ufopen(filename, options);
  348. (void)umask(saved_umask);
  349. if (f != NULL) (void)fchmod(fileno(f), mode);
  350. return f;
  351. }
  352. /*************************************************
  353. * Ensure stdin, stdout, and stderr exist *
  354. *************************************************/
  355. /* Some operating systems grumble if an exec() happens without a standard
  356. input, output, and error (fds 0, 1, 2) being defined. The worry is that some
  357. file will be opened and will use these fd values, and then some other bit of
  358. code will assume, for example, that it can write error messages to stderr.
  359. This function ensures that fds 0, 1, and 2 are open if they do not already
  360. exist, by connecting them to /dev/null.
  361. This function is also used to ensure that std{in,out,err} exist at all times,
  362. so that if any library that Exim calls tries to use them, it doesn't crash.
  363. Arguments: None
  364. Returns: Nothing
  365. */
  366. void
  367. exim_nullstd(void)
  368. {
  369. int i;
  370. int devnull = -1;
  371. struct stat statbuf;
  372. for (i = 0; i <= 2; i++)
  373. {
  374. if (fstat(i, &statbuf) < 0 && errno == EBADF)
  375. {
  376. if (devnull < 0) devnull = open("/dev/null", O_RDWR);
  377. if (devnull < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s",
  378. string_open_failed(errno, "/dev/null"));
  379. if (devnull != i) (void)dup2(devnull, i);
  380. }
  381. }
  382. if (devnull > 2) (void)close(devnull);
  383. }
  384. /*************************************************
  385. * Close unwanted file descriptors for delivery *
  386. *************************************************/
  387. /* This function is called from a new process that has been forked to deliver
  388. an incoming message, either directly, or using exec.
  389. We want any smtp input streams to be closed in this new process. However, it
  390. has been observed that using fclose() here causes trouble. When reading in -bS
  391. input, duplicate copies of messages have been seen. The files will be sharing a
  392. file pointer with the parent process, and it seems that fclose() (at least on
  393. some systems - I saw this on Solaris 2.5.1) messes with that file pointer, at
  394. least sometimes. Hence we go for closing the underlying file descriptors.
  395. If TLS is active, we want to shut down the TLS library, but without molesting
  396. the parent's SSL connection.
  397. For delivery of a non-SMTP message, we want to close stdin and stdout (and
  398. stderr unless debugging) because the calling process might have set them up as
  399. pipes and be waiting for them to close before it waits for the submission
  400. process to terminate. If they aren't closed, they hold up the calling process
  401. until the initial delivery process finishes, which is not what we want.
  402. Exception: We do want it for synchronous delivery!
  403. And notwithstanding all the above, if D_resolver is set, implying resolver
  404. debugging, leave stdout open, because that's where the resolver writes its
  405. debugging output.
  406. When we close stderr (which implies we've also closed stdout), we also get rid
  407. of any controlling terminal.
  408. Arguments: None
  409. Returns: Nothing
  410. */
  411. static void
  412. close_unwanted(void)
  413. {
  414. if (smtp_input)
  415. {
  416. #ifdef SUPPORT_TLS
  417. tls_close(TRUE, FALSE); /* Shut down the TLS library */
  418. #endif
  419. (void)close(fileno(smtp_in));
  420. (void)close(fileno(smtp_out));
  421. smtp_in = NULL;
  422. }
  423. else
  424. {
  425. (void)close(0); /* stdin */
  426. if ((debug_selector & D_resolver) == 0) (void)close(1); /* stdout */
  427. if (debug_selector == 0) /* stderr */
  428. {
  429. if (!synchronous_delivery)
  430. {
  431. (void)close(2);
  432. log_stderr = NULL;
  433. }
  434. (void)setsid();
  435. }
  436. }
  437. }
  438. /*************************************************
  439. * Set uid and gid *
  440. *************************************************/
  441. /* This function sets a new uid and gid permanently, optionally calling
  442. initgroups() to set auxiliary groups. There are some special cases when running
  443. Exim in unprivileged modes. In these situations the effective uid will not be
  444. root; if we already have the right effective uid/gid, and don't need to
  445. initialize any groups, leave things as they are.
  446. Arguments:
  447. uid the uid
  448. gid the gid
  449. igflag TRUE if initgroups() wanted
  450. msg text to use in debugging output and failure log
  451. Returns: nothing; bombs out on failure
  452. */
  453. void
  454. exim_setugid(uid_t uid, gid_t gid, BOOL igflag, uschar *msg)
  455. {
  456. uid_t euid = geteuid();
  457. gid_t egid = getegid();
  458. if (euid == root_uid || euid != uid || egid != gid || igflag)
  459. {
  460. /* At least one OS returns +1 for initgroups failure, so just check for
  461. non-zero. */
  462. if (igflag)
  463. {
  464. struct passwd *pw = getpwuid(uid);
  465. if (pw != NULL)
  466. {
  467. if (initgroups(pw->pw_name, gid) != 0)
  468. log_write(0,LOG_MAIN|LOG_PANIC_DIE,"initgroups failed for uid=%ld: %s",
  469. (long int)uid, strerror(errno));
  470. }
  471. else log_write(0, LOG_MAIN|LOG_PANIC_DIE, "cannot run initgroups(): "
  472. "no passwd entry for uid=%ld", (long int)uid);
  473. }
  474. if (setgid(gid) < 0 || setuid(uid) < 0)
  475. {
  476. log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to set gid=%ld or uid=%ld "
  477. "(euid=%ld): %s", (long int)gid, (long int)uid, (long int)euid, msg);
  478. }
  479. }
  480. /* Debugging output included uid/gid and all groups */
  481. DEBUG(D_uid)
  482. {
  483. int group_count, save_errno;
  484. gid_t group_list[NGROUPS_MAX];
  485. debug_printf("changed uid/gid: %s\n uid=%ld gid=%ld pid=%ld\n", msg,
  486. (long int)geteuid(), (long int)getegid(), (long int)getpid());
  487. group_count = getgroups(NGROUPS_MAX, group_list);
  488. save_errno = errno;
  489. debug_printf(" auxiliary group list:");
  490. if (group_count > 0)
  491. {
  492. int i;
  493. for (i = 0; i < group_count; i++) debug_printf(" %d", (int)group_list[i]);
  494. }
  495. else if (group_count < 0)
  496. debug_printf(" <error: %s>", strerror(save_errno));
  497. else debug_printf(" <none>");
  498. debug_printf("\n");
  499. }
  500. }
  501. /*************************************************
  502. * Exit point *
  503. *************************************************/
  504. /* Exim exits via this function so that it always clears up any open
  505. databases.
  506. Arguments:
  507. rc return code
  508. Returns: does not return
  509. */
  510. void
  511. exim_exit(int rc)
  512. {
  513. search_tidyup();
  514. DEBUG(D_any)
  515. debug_printf(">>>>>>>>>>>>>>>> Exim pid=%d terminating with rc=%d "
  516. ">>>>>>>>>>>>>>>>\n", (int)getpid(), rc);
  517. exit(rc);
  518. }
  519. /*************************************************
  520. * Extract port from host address *
  521. *************************************************/
  522. /* Called to extract the port from the values given to -oMa and -oMi.
  523. It also checks the syntax of the address, and terminates it before the
  524. port data when a port is extracted.
  525. Argument:
  526. address the address, with possible port on the end
  527. Returns: the port, or zero if there isn't one
  528. bombs out on a syntax error
  529. */
  530. static int
  531. check_port(uschar *address)
  532. {
  533. int port = host_address_extract_port(address);
  534. if (string_is_ip_address(address, NULL) == 0)
  535. {
  536. fprintf(stderr, "exim abandoned: \"%s\" is not an IP address\n", address);
  537. exit(EXIT_FAILURE);
  538. }
  539. return port;
  540. }
  541. /*************************************************
  542. * Test/verify an address *
  543. *************************************************/
  544. /* This function is called by the -bv and -bt code. It extracts a working
  545. address from a full RFC 822 address. This isn't really necessary per se, but it
  546. has the effect of collapsing source routes.
  547. Arguments:
  548. s the address string
  549. flags flag bits for verify_address()
  550. exit_value to be set for failures
  551. Returns: nothing
  552. */
  553. static void
  554. test_address(uschar *s, int flags, int *exit_value)
  555. {
  556. int start, end, domain;
  557. uschar *parse_error = NULL;
  558. uschar *address = parse_extract_address(s, &parse_error, &start, &end, &domain,
  559. FALSE);
  560. if (address == NULL)
  561. {
  562. fprintf(stdout, "syntax error: %s\n", parse_error);
  563. *exit_value = 2;
  564. }
  565. else
  566. {
  567. int rc = verify_address(deliver_make_addr(address,TRUE), stdout, flags, -1,
  568. -1, -1, NULL, NULL, NULL);
  569. if (rc == FAIL) *exit_value = 2;
  570. else if (rc == DEFER && *exit_value == 0) *exit_value = 1;
  571. }
  572. }
  573. /*************************************************
  574. * Show supported features *
  575. *************************************************/
  576. /* This function is called for -bV/--version and for -d to output the optional
  577. features of the current Exim binary.
  578. Arguments: a FILE for printing
  579. Returns: nothing
  580. */
  581. static void
  582. show_whats_supported(FILE *f)
  583. {
  584. auth_info *authi;
  585. #ifdef DB_VERSION_STRING
  586. fprintf(f, "Berkeley DB: %s\n", DB_VERSION_STRING);
  587. #elif defined(BTREEVERSION) && defined(HASHVERSION)
  588. #ifdef USE_DB
  589. fprintf(f, "Probably Berkeley DB version 1.8x (native mode)\n");
  590. #else
  591. fprintf(f, "Probably Berkeley DB version 1.8x (compatibility mode)\n");
  592. #endif
  593. #elif defined(_DBM_RDONLY) || defined(dbm_dirfno)
  594. fprintf(f, "Probably ndbm\n");
  595. #elif defined(USE_TDB)
  596. fprintf(f, "Using tdb\n");
  597. #else
  598. #ifdef USE_GDBM
  599. fprintf(f, "Probably GDBM (native mode)\n");
  600. #else
  601. fprintf(f, "Probably GDBM (compatibility mode)\n");
  602. #endif
  603. #endif
  604. fprintf(f, "Support for:");
  605. #ifdef SUPPORT_CRYPTEQ
  606. fprintf(f, " crypteq");
  607. #endif
  608. #if HAVE_ICONV
  609. fprintf(f, " iconv()");
  610. #endif
  611. #if HAVE_IPV6
  612. fprintf(f, " IPv6");
  613. #endif
  614. #ifdef HAVE_SETCLASSRESOURCES
  615. fprintf(f, " use_setclassresources");
  616. #endif
  617. #ifdef SUPPORT_PAM
  618. fprintf(f, " PAM");
  619. #endif
  620. #ifdef EXIM_PERL
  621. fprintf(f, " Perl");
  622. #endif
  623. #ifdef EXPAND_DLFUNC
  624. fprintf(f, " Expand_dlfunc");
  625. #endif
  626. #ifdef USE_TCP_WRAPPERS
  627. fprintf(f, " TCPwrappers");
  628. #endif
  629. #ifdef SUPPORT_TLS
  630. #ifdef USE_GNUTLS
  631. fprintf(f, " GnuTLS");
  632. #else
  633. fprintf(f, " OpenSSL");
  634. #endif
  635. #endif
  636. #ifdef SUPPORT_TRANSLATE_IP_ADDRESS
  637. fprintf(f, " translate_ip_address");
  638. #endif
  639. #ifdef SUPPORT_MOVE_FROZEN_MESSAGES
  640. fprintf(f, " move_frozen_messages");
  641. #endif
  642. #ifdef WITH_CONTENT_SCAN
  643. fprintf(f, " Content_Scanning");
  644. #endif
  645. #ifndef DISABLE_DKIM
  646. fprintf(f, " DKIM");
  647. #endif
  648. #ifdef WITH_OLD_DEMIME
  649. fprintf(f, " Old_Demime");
  650. #endif
  651. #ifndef DISABLE_DNSSEC
  652. fprintf(f, " DNSSEC");
  653. #endif
  654. #ifndef DISABLE_PRDR
  655. fprintf(f, " PRDR");
  656. #endif
  657. #ifndef DISABLE_OCSP
  658. fprintf(f, " OCSP");
  659. #endif
  660. #ifdef EXPERIMENTAL_SPF
  661. fprintf(f, " Experimental_SPF");
  662. #endif
  663. #ifdef EXPERIMENTAL_SRS
  664. fprintf(f, " Experimental_SRS");
  665. #endif
  666. #ifdef EXPERIMENTAL_BRIGHTMAIL
  667. fprintf(f, " Experimental_Brightmail");
  668. #endif
  669. #ifdef EXPERIMENTAL_DANE
  670. fprintf(f, " Experimental_DANE");
  671. #endif
  672. #ifdef EXPERIMENTAL_DCC
  673. fprintf(f, " Experimental_DCC");
  674. #endif
  675. #ifdef EXPERIMENTAL_DMARC
  676. fprintf(f, " Experimental_DMARC");
  677. #endif
  678. #ifdef EXPERIMENTAL_PROXY
  679. fprintf(f, " Experimental_Proxy");
  680. #endif
  681. #ifdef EXPERIMENTAL_EVENT
  682. fprintf(f, " Experimental_Event");
  683. #endif
  684. #ifdef EXPERIMENTAL_REDIS
  685. fprintf(f, " Experimental_Redis");
  686. #endif
  687. #ifdef EXPERIMENTAL_SOCKS
  688. fprintf(f, " Experimental_SOCKS");
  689. #endif
  690. #ifdef EXPERIMENTAL_INTERNATIONAL
  691. fprintf(f, " Experimental_International");
  692. #endif
  693. fprintf(f, "\n");
  694. fprintf(f, "Lookups (built-in):");
  695. #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
  696. fprintf(f, " lsearch wildlsearch nwildlsearch iplsearch");
  697. #endif
  698. #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
  699. fprintf(f, " cdb");
  700. #endif
  701. #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
  702. fprintf(f, " dbm dbmjz dbmnz");
  703. #endif
  704. #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
  705. fprintf(f, " dnsdb");
  706. #endif
  707. #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
  708. fprintf(f, " dsearch");
  709. #endif
  710. #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
  711. fprintf(f, " ibase");
  712. #endif
  713. #if defined(LOOKUP_LDAP) && LOOKUP_LDAP!=2
  714. fprintf(f, " ldap ldapdn ldapm");
  715. #endif
  716. #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
  717. fprintf(f, " mysql");
  718. #endif
  719. #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
  720. fprintf(f, " nis nis0");
  721. #endif
  722. #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
  723. fprintf(f, " nisplus");
  724. #endif
  725. #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
  726. fprintf(f, " oracle");
  727. #endif
  728. #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
  729. fprintf(f, " passwd");
  730. #endif
  731. #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
  732. fprintf(f, " pgsql");
  733. #endif
  734. #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
  735. fprintf(f, " sqlite");
  736. #endif
  737. #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
  738. fprintf(f, " testdb");
  739. #endif
  740. #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
  741. fprintf(f, " whoson");
  742. #endif
  743. fprintf(f, "\n");
  744. fprintf(f, "Authenticators:");
  745. #ifdef AUTH_CRAM_MD5
  746. fprintf(f, " cram_md5");
  747. #endif
  748. #ifdef AUTH_CYRUS_SASL
  749. fprintf(f, " cyrus_sasl");
  750. #endif
  751. #ifdef AUTH_DOVECOT
  752. fprintf(f, " dovecot");
  753. #endif
  754. #ifdef AUTH_GSASL
  755. fprintf(f, " gsasl");
  756. #endif
  757. #ifdef AUTH_HEIMDAL_GSSAPI
  758. fprintf(f, " heimdal_gssapi");
  759. #endif
  760. #ifdef AUTH_PLAINTEXT
  761. fprintf(f, " plaintext");
  762. #endif
  763. #ifdef AUTH_SPA
  764. fprintf(f, " spa");
  765. #endif
  766. fprintf(f, "\n");
  767. fprintf(f, "Routers:");
  768. #ifdef ROUTER_ACCEPT
  769. fprintf(f, " accept");
  770. #endif
  771. #ifdef ROUTER_DNSLOOKUP
  772. fprintf(f, " dnslookup");
  773. #endif
  774. #ifdef ROUTER_IPLITERAL
  775. fprintf(f, " ipliteral");
  776. #endif
  777. #ifdef ROUTER_IPLOOKUP
  778. fprintf(f, " iplookup");
  779. #endif
  780. #ifdef ROUTER_MANUALROUTE
  781. fprintf(f, " manualroute");
  782. #endif
  783. #ifdef ROUTER_QUERYPROGRAM
  784. fprintf(f, " queryprogram");
  785. #endif
  786. #ifdef ROUTER_REDIRECT
  787. fprintf(f, " redirect");
  788. #endif
  789. fprintf(f, "\n");
  790. fprintf(f, "Transports:");
  791. #ifdef TRANSPORT_APPENDFILE
  792. fprintf(f, " appendfile");
  793. #ifdef SUPPORT_MAILDIR
  794. fprintf(f, "/maildir");
  795. #endif
  796. #ifdef SUPPORT_MAILSTORE
  797. fprintf(f, "/mailstore");
  798. #endif
  799. #ifdef SUPPORT_MBX
  800. fprintf(f, "/mbx");
  801. #endif
  802. #endif
  803. #ifdef TRANSPORT_AUTOREPLY
  804. fprintf(f, " autoreply");
  805. #endif
  806. #ifdef TRANSPORT_LMTP
  807. fprintf(f, " lmtp");
  808. #endif
  809. #ifdef TRANSPORT_PIPE
  810. fprintf(f, " pipe");
  811. #endif
  812. #ifdef TRANSPORT_SMTP
  813. fprintf(f, " smtp");
  814. #endif
  815. fprintf(f, "\n");
  816. if (fixed_never_users[0] > 0)
  817. {
  818. int i;
  819. fprintf(f, "Fixed never_users: ");
  820. for (i = 1; i <= (int)fixed_never_users[0] - 1; i++)
  821. fprintf(f, "%d:", (unsigned int)fixed_never_users[i]);
  822. fprintf(f, "%d\n", (unsigned int)fixed_never_users[i]);
  823. }
  824. fprintf(f, "Size of off_t: " SIZE_T_FMT "\n", sizeof(off_t));
  825. /* Everything else is details which are only worth reporting when debugging.
  826. Perhaps the tls_version_report should move into this too. */
  827. DEBUG(D_any) do {
  828. int i;
  829. /* clang defines __GNUC__ (at least, for me) so test for it first */
  830. #if defined(__clang__)
  831. fprintf(f, "Compiler: CLang [%s]\n", __clang_version__);
  832. #elif defined(__GNUC__)
  833. fprintf(f, "Compiler: GCC [%s]\n",
  834. # ifdef __VERSION__
  835. __VERSION__
  836. # else
  837. "? unknown version ?"
  838. # endif
  839. );
  840. #else
  841. fprintf(f, "Compiler: <unknown>\n");
  842. #endif
  843. #ifdef SUPPORT_TLS
  844. tls_version_report(f);
  845. #endif
  846. #ifdef EXPERIMENTAL_INTERNATIONAL
  847. utf8_version_report(f);
  848. #endif
  849. for (authi = auths_available; *authi->driver_name != '\0'; ++authi)
  850. if (authi->version_report)
  851. (*authi->version_report)(f);
  852. /* PCRE_PRERELEASE is either defined and empty or a bare sequence of
  853. characters; unless it's an ancient version of PCRE in which case it
  854. is not defined. */
  855. #ifndef PCRE_PRERELEASE
  856. #define PCRE_PRERELEASE
  857. #endif
  858. #define QUOTE(X) #X
  859. #define EXPAND_AND_QUOTE(X) QUOTE(X)
  860. fprintf(f, "Library version: PCRE: Compile: %d.%d%s\n"
  861. " Runtime: %s\n",
  862. PCRE_MAJOR, PCRE_MINOR,
  863. EXPAND_AND_QUOTE(PCRE_PRERELEASE) "",
  864. pcre_version());
  865. #undef QUOTE
  866. #undef EXPAND_AND_QUOTE
  867. init_lookup_list();
  868. for (i = 0; i < lookup_list_count; i++)
  869. if (lookup_list[i]->version_report)
  870. lookup_list[i]->version_report(f);
  871. #ifdef WHITELIST_D_MACROS
  872. fprintf(f, "WHITELIST_D_MACROS: \"%s\"\n", WHITELIST_D_MACROS);
  873. #else
  874. fprintf(f, "WHITELIST_D_MACROS unset\n");
  875. #endif
  876. #ifdef TRUSTED_CONFIG_LIST
  877. fprintf(f, "TRUSTED_CONFIG_LIST: \"%s\"\n", TRUSTED_CONFIG_LIST);
  878. #else
  879. fprintf(f, "TRUSTED_CONFIG_LIST unset\n");
  880. #endif
  881. } while (0);
  882. }
  883. /*************************************************
  884. * Show auxiliary information about Exim *
  885. *************************************************/
  886. static void
  887. show_exim_information(enum commandline_info request, FILE *stream)
  888. {
  889. const uschar **pp;
  890. switch(request)
  891. {
  892. case CMDINFO_NONE:
  893. fprintf(stream, "Oops, something went wrong.\n");
  894. return;
  895. case CMDINFO_HELP:
  896. fprintf(stream,
  897. "The -bI: flag takes a string indicating which information to provide.\n"
  898. "If the string is not recognised, you'll get this help (on stderr).\n"
  899. "\n"
  900. " exim -bI:help this information\n"
  901. " exim -bI:dscp dscp value keywords known\n"
  902. " exim -bI:sieve list of supported sieve extensions, one per line.\n"
  903. );
  904. return;
  905. case CMDINFO_SIEVE:
  906. for (pp = exim_sieve_extension_list; *pp; ++pp)
  907. fprintf(stream, "%s\n", *pp);
  908. return;
  909. case CMDINFO_DSCP:
  910. dscp_list_to_stream(stream);
  911. return;
  912. }
  913. }
  914. /*************************************************
  915. * Quote a local part *
  916. *************************************************/
  917. /* This function is used when a sender address or a From: or Sender: header
  918. line is being created from the caller's login, or from an authenticated_id. It
  919. applies appropriate quoting rules for a local part.
  920. Argument: the local part
  921. Returns: the local part, quoted if necessary
  922. */
  923. uschar *
  924. local_part_quote(uschar *lpart)
  925. {
  926. BOOL needs_quote = FALSE;
  927. int size, ptr;
  928. uschar *yield;
  929. uschar *t;
  930. for (t = lpart; !needs_quote && *t != 0; t++)
  931. {
  932. needs_quote = !isalnum(*t) && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL &&
  933. (*t != '.' || t == lpart || t[1] == 0);
  934. }
  935. if (!needs_quote) return lpart;
  936. size = ptr = 0;
  937. yield = string_cat(NULL, &size, &ptr, US"\"", 1);
  938. for (;;)
  939. {
  940. uschar *nq = US Ustrpbrk(lpart, "\\\"");
  941. if (nq == NULL)
  942. {
  943. yield = string_cat(yield, &size, &ptr, lpart, Ustrlen(lpart));
  944. break;
  945. }
  946. yield = string_cat(yield, &size, &ptr, lpart, nq - lpart);
  947. yield = string_cat(yield, &size, &ptr, US"\\", 1);
  948. yield = string_cat(yield, &size, &ptr, nq, 1);
  949. lpart = nq + 1;
  950. }
  951. yield = string_cat(yield, &size, &ptr, US"\"", 1);
  952. yield[ptr] = 0;
  953. return yield;
  954. }
  955. #ifdef USE_READLINE
  956. /*************************************************
  957. * Load readline() functions *
  958. *************************************************/
  959. /* This function is called from testing executions that read data from stdin,
  960. but only when running as the calling user. Currently, only -be does this. The
  961. function loads the readline() function library and passes back the functions.
  962. On some systems, it needs the curses library, so load that too, but try without
  963. it if loading fails. All this functionality has to be requested at build time.
  964. Arguments:
  965. fn_readline_ptr pointer to where to put the readline pointer
  966. fn_addhist_ptr pointer to where to put the addhistory function
  967. Returns: the dlopen handle or NULL on failure
  968. */
  969. static void *
  970. set_readline(char * (**fn_readline_ptr)(const char *),
  971. void (**fn_addhist_ptr)(const char *))
  972. {
  973. void *dlhandle;
  974. void *dlhandle_curses = dlopen("libcurses." DYNLIB_FN_EXT, RTLD_GLOBAL|RTLD_LAZY);
  975. dlhandle = dlopen("libreadline." DYNLIB_FN_EXT, RTLD_GLOBAL|RTLD_NOW);
  976. if (dlhandle_curses != NULL) dlclose(dlhandle_curses);
  977. if (dlhandle != NULL)
  978. {
  979. /* Checked manual pages; at least in GNU Readline 6.1, the prototypes are:
  980. * char * readline (const char *prompt);
  981. * void add_history (const char *string);
  982. */
  983. *fn_readline_ptr = (char *(*)(const char*))dlsym(dlhandle, "readline");
  984. *fn_addhist_ptr = (void(*)(const char*))dlsym(dlhandle, "add_history");
  985. }
  986. else
  987. {
  988. DEBUG(D_any) debug_printf("failed to load readline: %s\n", dlerror());
  989. }
  990. return dlhandle;
  991. }
  992. #endif
  993. /*************************************************
  994. * Get a line from stdin for testing things *
  995. *************************************************/
  996. /* This function is called when running tests that can take a number of lines
  997. of input (for example, -be and -bt). It handles continuations and trailing
  998. spaces. And prompting and a blank line output on eof. If readline() is in use,
  999. the arguments are non-NULL and provide the relevant functions.
  1000. Arguments:
  1001. fn_readline readline function or NULL
  1002. fn_addhist addhist function or NULL
  1003. Returns: pointer to dynamic memory, or NULL at end of file
  1004. */
  1005. static uschar *
  1006. get_stdinput(char *(*fn_readline)(const char *), void(*fn_addhist)(const char *))
  1007. {
  1008. int i;
  1009. int size = 0;
  1010. int ptr = 0;
  1011. uschar *yield = NULL;
  1012. if (fn_readline == NULL) { printf("> "); fflush(stdout); }
  1013. for (i = 0;; i++)
  1014. {
  1015. uschar buffer[1024];
  1016. uschar *p, *ss;
  1017. #ifdef USE_READLINE
  1018. char *readline_line = NULL;
  1019. if (fn_readline != NULL)
  1020. {
  1021. if ((readline_line = fn_readline((i > 0)? "":"> ")) == NULL) break;
  1022. if (*readline_line != 0 && fn_addhist != NULL) fn_addhist(readline_line);
  1023. p = US readline_line;
  1024. }
  1025. else
  1026. #endif
  1027. /* readline() not in use */
  1028. {
  1029. if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break;
  1030. p = buffer;
  1031. }
  1032. /* Handle the line */
  1033. ss = p + (int)Ustrlen(p);
  1034. while (ss > p && isspace(ss[-1])) ss--;
  1035. if (i > 0)
  1036. {
  1037. while (p < ss && isspace(*p)) p++; /* leading space after cont */
  1038. }
  1039. yield = string_cat(yield, &size, &ptr, p, ss - p);
  1040. #ifdef USE_READLINE
  1041. if (fn_readline != NULL) free(readline_line);
  1042. #endif
  1043. if (ss == p || yield[ptr-1] != '\\')
  1044. {
  1045. yield[ptr] = 0;
  1046. break;
  1047. }
  1048. yield[--ptr] = 0;
  1049. }
  1050. if (yield == NULL) printf("\n");
  1051. return yield;
  1052. }
  1053. /*************************************************
  1054. * Output usage information for the program *
  1055. *************************************************/
  1056. /* This function is called when there are no recipients
  1057. or a specific --help argument was added.
  1058. Arguments:
  1059. progname information on what name we were called by
  1060. Returns: DOES NOT RETURN
  1061. */
  1062. static void
  1063. exim_usage(uschar *progname)
  1064. {
  1065. /* Handle specific program invocation varients */
  1066. if (Ustrcmp(progname, US"-mailq") == 0)
  1067. {
  1068. fprintf(stderr,
  1069. "mailq - list the contents of the mail queue\n\n"
  1070. "For a list of options, see the Exim documentation.\n");
  1071. exit(EXIT_FAILURE);
  1072. }
  1073. /* Generic usage - we output this whatever happens */
  1074. fprintf(stderr,
  1075. "Exim is a Mail Transfer Agent. It is normally called by Mail User Agents,\n"
  1076. "not directly from a shell command line. Options and/or arguments control\n"
  1077. "what it does when called. For a list of options, see the Exim documentation.\n");
  1078. exit(EXIT_FAILURE);
  1079. }
  1080. /*************************************************
  1081. * Validate that the macros given are okay *
  1082. *************************************************/
  1083. /* Typically, Exim will drop privileges if macros are supplied. In some
  1084. cases, we want to not do so.
  1085. Arguments: none (macros is a global)
  1086. Returns: true if trusted, false otherwise
  1087. */
  1088. static BOOL
  1089. macros_trusted(void)
  1090. {
  1091. #ifdef WHITELIST_D_MACROS
  1092. macro_item *m;
  1093. uschar *whitelisted, *end, *p, **whites, **w;
  1094. int white_count, i, n;
  1095. size_t len;
  1096. BOOL prev_char_item, found;
  1097. #endif
  1098. if (macros == NULL)
  1099. return TRUE;
  1100. #ifndef WHITELIST_D_MACROS
  1101. return FALSE;
  1102. #else
  1103. /* We only trust -D overrides for some invoking users:
  1104. root, the exim run-time user, the optional config owner user.
  1105. I don't know why config-owner would be needed, but since they can own the
  1106. config files anyway, there's no security risk to letting them override -D. */
  1107. if ( ! ((real_uid == root_uid)
  1108. || (real_uid == exim_uid)
  1109. #ifdef CONFIGURE_OWNER
  1110. || (real_uid == config_uid)
  1111. #endif
  1112. ))
  1113. {
  1114. debug_printf("macros_trusted rejecting macros for uid %d\n", (int) real_uid);
  1115. return FALSE;
  1116. }
  1117. /* Get a list of macros which are whitelisted */
  1118. whitelisted = string_copy_malloc(US WHITELIST_D_MACROS);
  1119. prev_char_item = FALSE;
  1120. white_count = 0;
  1121. for (p = whitelisted; *p != '\0'; ++p)
  1122. {
  1123. if (*p == ':' || isspace(*p))
  1124. {
  1125. *p = '\0';
  1126. if (prev_char_item)
  1127. ++white_count;
  1128. prev_char_item = FALSE;
  1129. continue;
  1130. }
  1131. if (!prev_char_item)
  1132. prev_char_item = TRUE;
  1133. }
  1134. end = p;
  1135. if (prev_char_item)
  1136. ++white_count;
  1137. if (!white_count)
  1138. return FALSE;
  1139. whites = store_malloc(sizeof(uschar *) * (white_count+1));
  1140. for (p = whitelisted, i = 0; (p != end) && (i < white_count); ++p)
  1141. {
  1142. if (*p != '\0')
  1143. {
  1144. whites[i++] = p;
  1145. if (i == white_count)
  1146. break;
  1147. while (*p != '\0' && p < end)
  1148. ++p;
  1149. }
  1150. }
  1151. whites[i] = NULL;
  1152. /* The list of macros should be very short. Accept the N*M complexity. */
  1153. for (m = macros; m != NULL; m = m->next)
  1154. {
  1155. found = FALSE;
  1156. for (w = whites; *w; ++w)
  1157. if (Ustrcmp(*w, m->name) == 0)
  1158. {
  1159. found = TRUE;
  1160. break;
  1161. }
  1162. if (!found)
  1163. return FALSE;
  1164. if (m->replacement == NULL)
  1165. continue;
  1166. len = Ustrlen(m->replacement);
  1167. if (len == 0)
  1168. continue;
  1169. n = pcre_exec(regex_whitelisted_macro, NULL, CS m->replacement, len,
  1170. 0, PCRE_EOPT, NULL, 0);
  1171. if (n < 0)
  1172. {
  1173. if (n != PCRE_ERROR_NOMATCH)
  1174. debug_printf("macros_trusted checking %s returned %d\n", m->name, n);
  1175. return FALSE;
  1176. }
  1177. }
  1178. DEBUG(D_any) debug_printf("macros_trusted overridden to true by whitelisting\n");
  1179. return TRUE;
  1180. #endif
  1181. }
  1182. /*************************************************
  1183. * Entry point and high-level code *
  1184. *************************************************/
  1185. /* Entry point for the Exim mailer. Analyse the arguments and arrange to take
  1186. the appropriate action. All the necessary functions are present in the one
  1187. binary. I originally thought one should split it up, but it turns out that so
  1188. much of the apparatus is needed in each chunk that one might as well just have
  1189. it all available all the time, which then makes the coding easier as well.
  1190. Arguments:
  1191. argc count of entries in argv
  1192. argv argument strings, with argv[0] being the program name
  1193. Returns: EXIT_SUCCESS if terminated successfully
  1194. EXIT_FAILURE otherwise, except when a message has been sent
  1195. to the sender, and -oee was given
  1196. */
  1197. int
  1198. main(int argc, char **cargv)
  1199. {
  1200. uschar **argv = USS cargv;
  1201. int arg_receive_timeout = -1;
  1202. int arg_smtp_receive_timeout = -1;
  1203. int arg_error_handling = error_handling;
  1204. int filter_sfd = -1;
  1205. int filter_ufd = -1;
  1206. int group_count;
  1207. int i, rv;
  1208. int list_queue_option = 0;
  1209. int msg_action = 0;
  1210. int msg_action_arg = -1;
  1211. int namelen = (argv[0] == NULL)? 0 : Ustrlen(argv[0]);
  1212. int queue_only_reason = 0;
  1213. #ifdef EXIM_PERL
  1214. int perl_start_option = 0;
  1215. #endif
  1216. int recipients_arg = argc;
  1217. int sender_address_domain = 0;
  1218. int test_retry_arg = -1;
  1219. int test_rewrite_arg = -1;
  1220. BOOL arg_queue_only = FALSE;
  1221. BOOL bi_option = FALSE;
  1222. BOOL checking = FALSE;
  1223. BOOL count_queue = FALSE;
  1224. BOOL expansion_test = FALSE;
  1225. BOOL extract_recipients = FALSE;
  1226. BOOL flag_G = FALSE;
  1227. BOOL flag_n = FALSE;
  1228. BOOL forced_delivery = FALSE;
  1229. BOOL f_end_dot = FALSE;
  1230. BOOL deliver_give_up = FALSE;
  1231. BOOL list_queue = FALSE;
  1232. BOOL list_options = FALSE;
  1233. BOOL local_queue_only;
  1234. BOOL more = TRUE;
  1235. BOOL one_msg_action = FALSE;
  1236. BOOL queue_only_set = FALSE;
  1237. BOOL receiving_message = TRUE;
  1238. BOOL sender_ident_set = FALSE;
  1239. BOOL session_local_queue_only;
  1240. BOOL unprivileged;
  1241. BOOL removed_privilege = FALSE;
  1242. BOOL usage_wanted = FALSE;
  1243. BOOL verify_address_mode = FALSE;
  1244. BOOL verify_as_sender = FALSE;
  1245. BOOL version_printed = FALSE;
  1246. uschar *alias_arg = NULL;
  1247. uschar *called_as = US"";
  1248. uschar *cmdline_syslog_name = NULL;
  1249. uschar *start_queue_run_id = NULL;
  1250. uschar *stop_queue_run_id = NULL;
  1251. uschar *expansion_test_message = NULL;
  1252. uschar *ftest_domain = NULL;
  1253. uschar *ftest_localpart = NULL;
  1254. uschar *ftest_prefix = NULL;
  1255. uschar *ftest_suffix = NULL;
  1256. uschar *log_oneline = NULL;
  1257. uschar *malware_test_file = NULL;
  1258. uschar *real_sender_address;
  1259. uschar *originator_home = US"/";
  1260. size_t sz;
  1261. void *reset_point;
  1262. struct passwd *pw;
  1263. struct stat statbuf;
  1264. pid_t passed_qr_pid = (pid_t)0;
  1265. int passed_qr_pipe = -1;
  1266. gid_t group_list[NGROUPS_MAX];
  1267. /* For the -bI: flag */
  1268. enum commandline_info info_flag = CMDINFO_NONE;
  1269. BOOL info_stdout = FALSE;
  1270. /* Possible options for -R and -S */
  1271. static uschar *rsopts[] = { US"f", US"ff", US"r", US"rf", US"rff" };
  1272. /* Need to define this in case we need to change the environment in order
  1273. to get rid of a bogus time zone. We have to make it char rather than uschar
  1274. because some OS define it in /usr/include/unistd.h. */
  1275. extern char **environ;
  1276. /* If the Exim user and/or group and/or the configuration file owner/group were
  1277. defined by ref:name at build time, we must now find the actual uid/gid values.
  1278. This is a feature to make the lives of binary distributors easier. */
  1279. #ifdef EXIM_USERNAME
  1280. if (route_finduser(US EXIM_USERNAME, &pw, &exim_uid))
  1281. {
  1282. if (exim_uid == 0)
  1283. {
  1284. fprintf(stderr, "exim: refusing to run with uid 0 for \"%s\"\n",
  1285. EXIM_USERNAME);
  1286. exit(EXIT_FAILURE);
  1287. }
  1288. /* If ref:name uses a number as the name, route_finduser() returns
  1289. TRUE with exim_uid set and pw coerced to NULL. */
  1290. if (pw)
  1291. exim_gid = pw->pw_gid;
  1292. #ifndef EXIM_GROUPNAME
  1293. else
  1294. {
  1295. fprintf(stderr,
  1296. "exim: ref:name should specify a usercode, not a group.\n"
  1297. "exim: can't let you get away with it unless you also specify a group.\n");
  1298. exit(EXIT_FAILURE);
  1299. }
  1300. #endif
  1301. }
  1302. else
  1303. {
  1304. fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n",
  1305. EXIM_USERNAME);
  1306. exit(EXIT_FAILURE);
  1307. }
  1308. #endif
  1309. #ifdef EXIM_GROUPNAME
  1310. if (!route_findgroup(US EXIM_GROUPNAME, &exim_gid))
  1311. {
  1312. fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n",
  1313. EXIM_GROUPNAME);
  1314. exit(EXIT_FAILURE);
  1315. }
  1316. #endif
  1317. #ifdef CONFIGURE_OWNERNAME
  1318. if (!route_finduser(US CONFIGURE_OWNERNAME, NULL, &config_uid))
  1319. {
  1320. fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n",
  1321. CONFIGURE_OWNERNAME);
  1322. exit(EXIT_FAILURE);
  1323. }
  1324. #endif
  1325. /* We default the system_filter_user to be the Exim run-time user, as a
  1326. sane non-root value. */
  1327. system_filter_uid = exim_uid;
  1328. #ifdef CONFIGURE_GROUPNAME
  1329. if (!route_findgroup(US CONFIGURE_GROUPNAME, &config_gid))
  1330. {
  1331. fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n",
  1332. CONFIGURE_GROUPNAME);
  1333. exit(EXIT_FAILURE);
  1334. }
  1335. #endif
  1336. /* In the Cygwin environment, some initialization needs doing. It is fudged
  1337. in by means of this macro. */
  1338. #ifdef OS_INIT
  1339. OS_INIT
  1340. #endif
  1341. /* Check a field which is patched when we are running Exim within its
  1342. testing harness; do a fast initial check, and then the whole thing. */
  1343. running_in_test_harness =
  1344. *running_status == '<' && Ustrcmp(running_status, "<<<testing>>>") == 0;
  1345. /* The C standard says that the equivalent of setlocale(LC_ALL, "C") is obeyed
  1346. at the start of a program; however, it seems that some environments do not
  1347. follow this. A "strange" locale can affect the formatting of timestamps, so we
  1348. make quite sure. */
  1349. setlocale(LC_ALL, "C");
  1350. /* Set up the default handler for timing using alarm(). */
  1351. os_non_restarting_signal(SIGALRM, sigalrm_handler);
  1352. /* Ensure we have a buffer for constructing log entries. Use malloc directly,
  1353. because store_malloc writes a log entry on failure. */
  1354. log_buffer = (uschar *)malloc(LOG_BUFFER_SIZE);
  1355. if (log_buffer == NULL)
  1356. {
  1357. fprintf(stderr, "exim: failed to get store for log buffer\n");
  1358. exit(EXIT_FAILURE);
  1359. }
  1360. /* Set log_stderr to stderr, provided that stderr exists. This gets reset to
  1361. NULL when the daemon is run and the file is closed. We have to use this
  1362. indirection, because some systems don't allow writing to the variable "stderr".
  1363. */
  1364. if (fstat(fileno(stderr), &statbuf) >= 0) log_stderr = stderr;
  1365. /* Arrange for the PCRE regex library to use our store functions. Note that
  1366. the normal calls are actually macros that add additional arguments for
  1367. debugging purposes so we have to assign specially constructed functions here.
  1368. The default is to use store in the stacking pool, but this is overridden in the
  1369. regex_must_compile() function. */
  1370. pcre_malloc = function_store_get;
  1371. pcre_free = function_dummy_free;
  1372. /* Ensure there is a big buffer for temporary use in several places. It is put
  1373. in malloc store so that it can be freed for enlargement if necessary. */
  1374. big_buffer = store_malloc(big_buffer_size);
  1375. /* Set up the handler for the data request signal, and set the initial
  1376. descriptive text. */
  1377. set_process_info("initializing");
  1378. os_restarting_signal(SIGUSR1, usr1_handler);
  1379. /* SIGHUP is used to get the daemon to reconfigure. It gets set as appropriate
  1380. in the daemon code. For the rest of Exim's uses, we ignore it. */
  1381. signal(SIGHUP, SIG_IGN);
  1382. /* We don't want to die on pipe errors as the code is written to handle
  1383. the write error instead. */
  1384. signal(SIGPIPE, SIG_IGN);
  1385. /* Under some circumstance on some OS, Exim can get called with SIGCHLD
  1386. set to SIG_IGN. This causes subprocesses that complete before the parent
  1387. process waits for them not to hang around, so when Exim calls wait(), nothing
  1388. is there. The wait() code has been made robust against this, but let's ensure
  1389. that SIGCHLD is set to SIG_DFL, because it's tidier to wait and get a process
  1390. ending status. We use sigaction rather than plain signal() on those OS where
  1391. SA_NOCLDWAIT exists, because we want to be sure it is turned off. (There was a
  1392. problem on AIX with this.) */
  1393. #ifdef SA_NOCLDWAIT
  1394. {
  1395. struct sigaction act;
  1396. act.sa_handler = SIG_DFL;
  1397. sigemptyset(&(act.sa_mask));
  1398. act.sa_flags = 0;
  1399. sigaction(SIGCHLD, &act, NULL);
  1400. }
  1401. #else
  1402. signal(SIGCHLD, SIG_DFL);
  1403. #endif
  1404. /* Save the arguments for use if we re-exec exim as a daemon after receiving
  1405. SIGHUP. */
  1406. sighup_argv = argv;
  1407. /* Set up the version number. Set up the leading 'E' for the external form of
  1408. message ids, set the pointer to the internal form, and initialize it to
  1409. indicate no message being processed. */
  1410. version_init();
  1411. message_id_option[0] = '-';
  1412. message_id_external = message_id_option + 1;
  1413. message_id_external[0] = 'E';
  1414. message_id = message_id_external + 1;
  1415. message_id[0] = 0;
  1416. /* Set the umask to zero so that any files Exim creates using open() are
  1417. created with the modes that it specifies. NOTE: Files created with fopen() have
  1418. a problem, which was not recognized till rather late (February 2006). With this
  1419. umask, such files will be world writeable. (They are all content scanning files
  1420. in the spool directory, which isn't world-accessible, so this is not a
  1421. disaster, but it's untidy.) I don't want to change this overall setting,
  1422. however, because it will interact badly with the open() calls. Instead, there's
  1423. now a function called modefopen() that fiddles with the umask while calling
  1424. fopen(). */
  1425. (void)umask(0);
  1426. /* Precompile the regular expression for matching a message id. Keep this in
  1427. step with the code that generates ids in the accept.c module. We need to do
  1428. this here, because the -M options check their arguments for syntactic validity
  1429. using mac_ismsgid, which uses this. */
  1430. regex_ismsgid =
  1431. regex_must_compile(US"^(?:[^\\W_]{6}-){2}[^\\W_]{2}$", FALSE, TRUE);
  1432. /* Precompile the regular expression that is used for matching an SMTP error
  1433. code, possibly extended, at the start of an error message. Note that the
  1434. terminating whitespace character is included. */
  1435. regex_smtp_code =
  1436. regex_must_compile(US"^\\d\\d\\d\\s(?:\\d\\.\\d\\d?\\d?\\.\\d\\d?\\d?\\s)?",
  1437. FALSE, TRUE);
  1438. #ifdef WHITELIST_D_MACROS
  1439. /* Precompile the regular expression used to filter the content of macros
  1440. given to -D for permissibility. */
  1441. regex_whitelisted_macro =
  1442. regex_must_compile(US"^[A-Za-z0-9_/.-]*$", FALSE, TRUE);
  1443. #endif
  1444. /* If the program is called as "mailq" treat it as equivalent to "exim -bp";
  1445. this seems to be a generally accepted convention, since one finds symbolic
  1446. links called "mailq" in standard OS configurations. */
  1447. if ((namelen == 5 && Ustrcmp(argv[0], "mailq") == 0) ||
  1448. (namelen > 5 && Ustrncmp(argv[0] + namelen - 6, "/mailq", 6) == 0))
  1449. {
  1450. list_queue = TRUE;
  1451. receiving_message = FALSE;
  1452. called_as = US"-mailq";
  1453. }
  1454. /* If the program is called as "rmail" treat it as equivalent to
  1455. "exim -i -oee", thus allowing UUCP messages to be input using non-SMTP mode,
  1456. i.e. preventing a single dot on a line from terminating the message, and
  1457. returning with zero return code, even in cases of error (provided an error
  1458. message has been sent). */
  1459. if ((namelen == 5 && Ustrcmp(argv[0], "rmail") == 0) ||
  1460. (namelen > 5 && Ustrncmp(argv[0] + namelen - 6, "/rmail", 6) == 0))
  1461. {
  1462. dot_ends = FALSE;
  1463. called_as = US"-rmail";
  1464. errors_sender_rc = EXIT_SUCCESS;
  1465. }
  1466. /* If the program is called as "rsmtp" treat it as equivalent to "exim -bS";
  1467. this is a smail convention. */
  1468. if ((namelen == 5 && Ustrcmp(argv[0], "rsmtp") == 0) ||
  1469. (namelen >

Large files files are truncated, but you can click here to view the full file