static void *threadwrap(void *data) { struct thread_pvt *thread = data; void *ret = NULL; int cnt; objref(thread); for(cnt = 0;!testflag(thread, TL_THREAD_RUN) && (cnt < 100); cnt++) { usleep(1000); } if (cnt == 100) { return NULL; } pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); if (!testflag(thread, TL_THREAD_CAN_CANCEL)) { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); } if (!testflag(thread, TL_THREAD_JOINABLE)) { pthread_detach(thread->thr); } pthread_cleanup_push(thread_cleanup, thread); ret = thread->func(thread->data); pthread_cleanup_pop(1); return (ret); }
/* * loop through all threads till they stoped * setting stop will flag threads to stop */ static void *managethread(void *data) { struct thread_pvt *thread; int last = 0; if (!(thread = get_thread_from_id())) { return NULL; } for(;;) { /*if im the last one leave this is done locked to make sure no items are added/removed*/ objlock(threads); if (!(bucket_list_cnt(threads->list) - last)) { if (threads->manager) { objunref(threads->manager); threads->manager = NULL; } objunlock(threads); objunref(thread); break; } objunlock(threads); /* Ive been joined so i can leave when im alone*/ if (testflag(thread, TL_THREAD_JOIN)) { clearflag(thread, TL_THREAD_JOIN); last = 1; } /*Cancel all running threads*/ if (testflag(thread, TL_THREAD_STOP)) { clearflag(thread, TL_THREAD_STOP); /* Stop any more threads*/ objlock(threads); if (threads->manager) { objunref(threads->manager); threads->manager = NULL; } objunlock(threads); /* cancel all threads now that they stoped*/ bucketlist_callback(threads->list, stop_threads, thread); last = 1; } #ifdef __WIN32__ Sleep(1000); #else sleep(1); #endif } return NULL; }
static void stop_threads(void *data, void *data2) { struct thread_pvt *thread = data; struct thread_pvt *man = data2; /*Dont footbullet*/ if (!pthread_equal(man->thr, thread->thr)) { if (thread->sighandler) { pthread_kill(thread->thr, SIGTERM); } if (testflag(thread, TL_THREAD_CAN_CANCEL) && testflag(thread, TL_THREAD_RUN)) { pthread_cancel(thread->thr); } clearflag(thread, TL_THREAD_RUN); } }
/** @brief create a thread result must be unreferenced * * @note If the manager thread has not yet started this will start the manager thread. * @warning @ref THREAD_OPTION_RETURN flag controls the return of this function. * @warning Threads should periodically check the result of framework_threadok() and cleanup or use @ref THREAD_OPTION_CANCEL * @param func Function to run thread on. * @param cleanup Cleanup function to run. * @param sig_handler Thread signal handler. * @param data Data to pass to callbacks. * @param flags Options of @ref thread_option_flags passed * @returns a thread structure that must be un referencend OR NULL depending on flags.*/ extern struct thread_pvt *framework_mkthread(threadfunc func, threadcleanup cleanup, threadsighandler sig_handler, void *data, int flags) { struct thread_pvt *thread; struct threadcontainer *tc = NULL; /*Grab a reference for threads in this scope start up if we can*/ if (!(tc = (objref(threads)) ? threads : NULL)) { if (!thread_can_start) { return NULL; } else if (!startthreads()) { return NULL; } if (!(tc = (objref(threads)) ? threads : NULL)) { return NULL; } } objlock(tc); /* dont allow threads if no manager or it not started*/ if ((!tc->manager || !func) && (func != managethread)) { /*im shuting down*/ objunlock(tc); objunref(tc); return NULL; } else if (!(thread = objalloc(sizeof(*thread), free_thread))) { /* could not create*/ objunlock(tc); objunref(tc); return NULL; } thread->data = (objref(data)) ? data : NULL; thread->flags = flags << 16; thread->cleanup = cleanup; thread->sighandler = sig_handler; thread->func = func; objunlock(tc); /* start thread and check it*/ if (pthread_create(&thread->thr, NULL, threadwrap, thread) || pthread_kill(thread->thr, 0)) { objunref(thread); objunref(tc); return NULL; } /*Activate the thread it needs to be flaged to run or it will die*/ objlock(tc); addtobucket(tc->list, thread); setflag(thread, TL_THREAD_RUN); objunlock(tc); objunref(tc); if (testflag(thread, TL_THREAD_RETURN)) { return thread; } else { objunref(thread); return NULL; } }
/** @brief let threads check there status. * * @return 0 if the thread should terminate.*/ extern int framework_threadok() { struct thread_pvt *thr; int ret; thr = get_thread_from_id(); ret =(thr) ? testflag(thr, TL_THREAD_RUN) : 0; objunref(thr); return ret; }
int dnslookup_router_entry( router_instance *rblock, /* data for this instantiation */ address_item *addr, /* address we are working on */ struct passwd *pw, /* passwd entry after check_local_user */ int verify, /* v_none/v_recipient/v_sender/v_expn */ address_item **addr_local, /* add it to this if it's local */ address_item **addr_remote, /* add it to this if it's remote */ address_item **addr_new, /* put new addresses on here */ address_item **addr_succeed) /* put old address here on success */ { host_item h; int rc; int widen_sep = 0; int whichrrs = HOST_FIND_BY_MX | HOST_FIND_BY_A; dnslookup_router_options_block *ob = (dnslookup_router_options_block *)(rblock->options_block); uschar *srv_service = NULL; uschar *widen = NULL; uschar *pre_widen = addr->domain; uschar *post_widen = NULL; uschar *fully_qualified_name; uschar *listptr; uschar widen_buffer[256]; addr_new = addr_new; /* Keep picky compilers happy */ addr_succeed = addr_succeed; DEBUG(D_route) debug_printf("%s router called for %s\n domain = %s\n", rblock->name, addr->address, addr->domain); /* If an SRV check is required, expand the service name */ if (ob->check_srv != NULL) { srv_service = expand_string(ob->check_srv); if (srv_service == NULL && !expand_string_forcedfail) { addr->message = string_sprintf("%s router: failed to expand \"%s\": %s", rblock->name, ob->check_srv, expand_string_message); return DEFER; } else whichrrs |= HOST_FIND_BY_SRV; } /* Set up the first of any widening domains. The code further down copes with either pre- or post-widening, but at present there is no way to turn on pre-widening, as actually doing so seems like a rather bad idea, and nobody has requested it. Pre-widening would cause local abbreviated names to take precedence over global names. For example, if the domain is "xxx.ch" it might be something in the "ch" toplevel domain, but it also might be xxx.ch.xyz.com. The choice of pre- or post-widening affects which takes precedence. If ever somebody comes up with some kind of requirement for pre-widening, presumably with some conditions under which it is done, it can be selected here. The rewrite_headers option works only when routing an address at transport time, because the alterations to the headers are not persistent so must be worked out immediately before they are used. Sender addresses are routed for verification purposes, but never at transport time, so any header changes that you might expect as a result of sender domain widening do not occur. Therefore we do not perform widening when verifying sender addresses; however, widening sender addresses is OK if we do not have to rewrite the headers. A corollary of this is that if the current address is not the original address, then it does not appear in the message header so it is also OK to widen. The suppression of widening for sender addresses is silent because it is the normal desirable behaviour. */ if (ob->widen_domains != NULL && (verify != v_sender || !ob->rewrite_headers || addr->parent != NULL)) { listptr = ob->widen_domains; widen = string_nextinlist(&listptr, &widen_sep, widen_buffer, sizeof(widen_buffer)); /**** if (some condition requiring pre-widening) { post_widen = pre_widen; pre_widen = NULL; } ****/ } /* Loop to cope with explicit widening of domains as configured. This code copes with widening that may happen before or after the original name. The decision as to which is taken above. */ for (;;) { int flags = whichrrs; BOOL removed = FALSE; if (pre_widen != NULL) { h.name = pre_widen; pre_widen = NULL; } else if (widen != NULL) { h.name = string_sprintf("%s.%s", addr->domain, widen); widen = string_nextinlist(&listptr, &widen_sep, widen_buffer, sizeof(widen_buffer)); DEBUG(D_route) debug_printf("%s router widened %s to %s\n", rblock->name, addr->domain, h.name); } else if (post_widen != NULL) { h.name = post_widen; post_widen = NULL; DEBUG(D_route) debug_printf("%s router trying %s after widening failed\n", rblock->name, h.name); } else return DECLINE; /* Set up the rest of the initial host item. Others may get chained on if there is more than one IP address. We set it up here instead of outside the loop so as to re-initialize if a previous try succeeded but was rejected because of not having an MX record. */ h.next = NULL; h.address = NULL; h.port = PORT_NONE; h.mx = MX_NONE; h.status = hstatus_unknown; h.why = hwhy_unknown; h.last_try = 0; /* Unfortunately, we cannot set the mx_only option in advance, because the DNS lookup may extend an unqualified name. Therefore, we must do the test subsequently. We use the same logic as that for widen_domains above to avoid requesting a header rewrite that cannot work. */ if (verify != v_sender || !ob->rewrite_headers || addr->parent != NULL) { if (ob->qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE; if (ob->search_parents) flags |= HOST_FIND_SEARCH_PARENTS; } rc = host_find_bydns(&h, rblock->ignore_target_hosts, flags, srv_service, ob->srv_fail_domains, ob->mx_fail_domains, &fully_qualified_name, &removed); if (removed) setflag(addr, af_local_host_removed); /* If host found with only address records, test for the domain's being in the mx_domains list. Note that this applies also to SRV records; the name of the option is historical. */ if ((rc == HOST_FOUND || rc == HOST_FOUND_LOCAL) && h.mx < 0 && ob->mx_domains != NULL) { switch(match_isinlist(fully_qualified_name, &(ob->mx_domains), 0, &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL)) { case DEFER: addr->message = US"lookup defer for mx_domains"; return DEFER; case OK: DEBUG(D_route) debug_printf("%s router rejected %s: no MX record(s)\n", rblock->name, fully_qualified_name); continue; } } /* Deferral returns forthwith, and anything other than failure breaks the loop. */ if (rc == HOST_FIND_AGAIN) { if (rblock->pass_on_timeout) { DEBUG(D_route) debug_printf("%s router timed out, and pass_on_timeout is set\n", rblock->name); return PASS; } addr->message = US"host lookup did not complete"; return DEFER; } if (rc != HOST_FIND_FAILED) break; /* Check to see if the failure is the result of MX records pointing to non-existent domains, and if so, set an appropriate error message; the case of an MX or SRV record pointing to "." is another special case that we can detect. Otherwise "unknown mail domain" is used, which is confusing. Also, in this case don't do the widening. We need check only the first host to see if its MX has been filled in, but there is no address, because if there were any usable addresses returned, we would not have had HOST_FIND_FAILED. As a common cause of this problem is MX records with IP addresses on the RHS, give a special message in this case. */ if (h.mx >= 0 && h.address == NULL) { setflag(addr, af_pass_message); /* This is not a security risk */ if (h.name[0] == 0) addr->message = US"an MX or SRV record indicated no SMTP service"; else { addr->message = US"all relevant MX records point to non-existent hosts"; if (!allow_mx_to_ip && string_is_ip_address(h.name, NULL) != 0) { addr->user_message = string_sprintf("It appears that the DNS operator for %s\n" "has installed an invalid MX record with an IP address\n" "instead of a domain name on the right hand side.", addr->domain); addr->message = string_sprintf("%s or (invalidly) to IP addresses", addr->message); } } return DECLINE; } /* If there's a syntax error, do not continue with any widening, and note the error. */ if (host_find_failed_syntax) { addr->message = string_sprintf("mail domain \"%s\" is syntactically " "invalid", h.name); return DECLINE; } } /* If the original domain name has been changed as a result of the host lookup, set up a child address for rerouting and request header rewrites if so configured. Then yield REROUTED. However, if the only change is a change of case in the domain name, which some resolvers yield (others don't), just change the domain name in the original address so that the official version is used in RCPT commands. */ if (Ustrcmp(addr->domain, fully_qualified_name) != 0) { if (strcmpic(addr->domain, fully_qualified_name) == 0) { uschar *at = Ustrrchr(addr->address, '@'); memcpy(at+1, fully_qualified_name, Ustrlen(at+1)); } else { rf_change_domain(addr, fully_qualified_name, ob->rewrite_headers, addr_new); return REROUTED; } } /* If the yield is HOST_FOUND_LOCAL, the remote domain name either found MX records with the lowest numbered one pointing to a host with an IP address that is set on one of the interfaces of this machine, or found A records or got addresses from gethostbyname() that contain one for this machine. This can happen quite legitimately if the original name was a shortened form of a domain, but we will have picked that up already via the name change test above. Otherwise, the action to be taken can be configured by the self option, the handling of which is in a separate function, as it is also required for other routers. */ if (rc == HOST_FOUND_LOCAL) { rc = rf_self_action(addr, &h, rblock->self_code, rblock->self_rewrite, rblock->self, addr_new); if (rc != OK) return rc; } /* Otherwise, insist on being a secondary MX if so configured */ else if (ob->check_secondary_mx && !testflag(addr, af_local_host_removed)) { DEBUG(D_route) debug_printf("check_secondary_mx set and local host not secondary\n"); return DECLINE; } /* Set up the errors address, if any. */ rc = rf_get_errors_address(addr, rblock, verify, &(addr->p.errors_address)); if (rc != OK) return rc; /* Set up the additional and removeable headers for this address. */ rc = rf_get_munge_headers(addr, rblock, &(addr->p.extra_headers), &(addr->p.remove_headers)); if (rc != OK) return rc; /* Get store in which to preserve the original host item, chained on to the address. */ addr->host_list = store_get(sizeof(host_item)); addr->host_list[0] = h; /* Fill in the transport and queue the address for delivery. */ if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr, rblock->name, NULL)) return DEFER; addr->transport = rblock->transport; return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)? OK : DEFER; }
static void add_generated(router_instance *rblock, address_item **addr_new, address_item *addr, address_item *generated, address_item_propagated *addr_prop, ugid_block *ugidptr, struct passwd *pw) { redirect_router_options_block *ob = (redirect_router_options_block *)(rblock->options_block); while (generated != NULL) { address_item *parent; address_item *next = generated; uschar *errors_address = next->prop.errors_address; generated = next->next; next->parent = addr; orflag(next, addr, af_ignore_error); next->start_router = rblock->redirect_router; if (addr->child_count == SHRT_MAX) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d " "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address); addr->child_count++; next->next = *addr_new; *addr_new = next; /* Don't do the "one_time" thing for the first pass of a 2-stage queue run. */ if (ob->one_time && !queue_2stage) { for (parent = addr; parent->parent != NULL; parent = parent->parent); next->onetime_parent = parent->address; } if (ob->hide_child_in_errmsg) setflag(next, af_hide_child); /* If check_ancestor is set, we want to know if any ancestor of this address is the address we are about to generate. The check must be done caselessly unless the ancestor was routed by a case-sensitive router. */ if (ob->check_ancestor) { for (parent = addr; parent != NULL; parent = parent->parent) { if (((parent->router != NULL && parent->router->caseful_local_part)? Ustrcmp(next->address, parent->address) : strcmpic(next->address, parent->address) ) == 0) { DEBUG(D_route) debug_printf("generated parent replaced by child\n"); next->address = string_copy(addr->address); break; } } } /* A user filter may, under some circumstances, set up an errors address. If so, we must take care to re-instate it when we copy in the propagated data so that it overrides any errors_to setting on the router. */ next->prop = *addr_prop; if (errors_address != NULL) next->prop.errors_address = errors_address; /* For pipes, files, and autoreplies, record this router as handling them, because they don't go through the routing process again. Then set up uid, gid, home and current directories for transporting. */ if (testflag(next, af_pfr)) { next->router = rblock; rf_set_ugid(next, ugidptr); /* Will contain pw values if not overridden */ /* When getting the home directory out of the password information, wrap it in \N...\N to avoid expansion later. In Cygwin, home directories can contain $ characters. */ if (rblock->home_directory != NULL) next->home_dir = rblock->home_directory; else if (rblock->check_local_user) next->home_dir = string_sprintf("\\N%s\\N", pw->pw_dir); else if (rblock->router_home_directory != NULL && testflag(addr, af_home_expanded)) { next->home_dir = deliver_home; setflag(next, af_home_expanded); } next->current_dir = rblock->current_directory; /* Permission options */ if (!ob->forbid_pipe) setflag(next, af_allow_pipe); if (!ob->forbid_file) setflag(next, af_allow_file); if (!ob->forbid_filter_reply) setflag(next, af_allow_reply); /* If the transport setting fails, the error gets picked up at the outer level from the setting of basic_errno in the address. */ if (next->address[0] == '|') { address_pipe = next->address; if (rf_get_transport(ob->pipe_transport_name, &(ob->pipe_transport), next, rblock->name, US"pipe_transport")) next->transport = ob->pipe_transport; address_pipe = NULL; } else if (next->address[0] == '>') { if (rf_get_transport(ob->reply_transport_name, &(ob->reply_transport), next, rblock->name, US"reply_transport")) next->transport = ob->reply_transport; } else /* must be file or directory */ { int len = Ustrlen(next->address); address_file = next->address; if (next->address[len-1] == '/') { if (rf_get_transport(ob->directory_transport_name, &(ob->directory_transport), next, rblock->name, US"directory_transport")) next->transport = ob->directory_transport; } else { if (rf_get_transport(ob->file_transport_name, &(ob->file_transport), next, rblock->name, US"file_transport")) next->transport = ob->file_transport; } address_file = NULL; } } #ifdef SUPPORT_I18N next->prop.utf8_msg = string_is_utf8(next->address) || (sender_address && string_is_utf8(sender_address)); #endif DEBUG(D_route) { debug_printf("%s router generated %s\n %serrors_to=%s transport=%s\n", rblock->name, next->address, testflag(next, af_pfr)? "pipe, file, or autoreply\n " : "", next->prop.errors_address, (next->transport == NULL)? US"NULL" : next->transport->name); if (testflag(next, af_uid_set)) debug_printf(" uid=%ld ", (long int)(next->uid)); else debug_printf(" uid=unset "); if (testflag(next, af_gid_set)) debug_printf("gid=%ld ", (long int)(next->gid)); else debug_printf("gid=unset "); #ifdef SUPPORT_I18N if (next->prop.utf8_msg) debug_printf("utf8 "); #endif debug_printf("home=%s\n", next->home_dir); } } }
BOOL pipe_transport_entry( transport_instance *tblock, /* data for this instantiation */ address_item *addr) /* address(es) we are working on */ { pid_t pid, outpid; int fd_in, fd_out, rc; int envcount = 0; int envsep = 0; int expand_fail; pipe_transport_options_block *ob = (pipe_transport_options_block *)(tblock->options_block); int timeout = ob->timeout; BOOL written_ok = FALSE; BOOL expand_arguments; const uschar **argv; uschar *envp[50]; const uschar *envlist = ob->environment; uschar *cmd, *ss; uschar *eol = (ob->use_crlf)? US"\r\n" : US"\n"; DEBUG(D_transport) debug_printf("%s transport entered\n", tblock->name); /* Set up for the good case */ addr->transport_return = OK; addr->basic_errno = 0; /* Pipes are not accepted as general addresses, but they can be generated from .forward files or alias files. In those cases, the pfr flag is set, and the command to be obeyed is pointed to by addr->local_part; it starts with the pipe symbol. In other cases, the command is supplied as one of the pipe transport's options. */ if (testflag(addr, af_pfr) && addr->local_part[0] == '|') { if (ob->force_command) { /* Enables expansion of $address_pipe into seperate arguments */ setflag(addr, af_force_command); cmd = ob->cmd; expand_arguments = TRUE; expand_fail = PANIC; } else { cmd = addr->local_part + 1; while (isspace(*cmd)) cmd++; expand_arguments = testflag(addr, af_expand_pipe); expand_fail = FAIL; } } else { cmd = ob->cmd; expand_arguments = TRUE; expand_fail = PANIC; } /* If no command has been supplied, we are in trouble. * We also check for an empty string since it may be * coming from addr->local_part[0] == '|' */ if (cmd == NULL || *cmd == '\0') { addr->transport_return = DEFER; addr->message = string_sprintf("no command specified for %s transport", tblock->name); return FALSE; } /* When a pipe is set up by a filter file, there may be values for $thisaddress and numerical the variables in existence. These are passed in addr->pipe_expandn for use here. */ if (expand_arguments && addr->pipe_expandn != NULL) { uschar **ss = addr->pipe_expandn; expand_nmax = -1; if (*ss != NULL) filter_thisaddress = *ss++; while (*ss != NULL) { expand_nstring[++expand_nmax] = *ss; expand_nlength[expand_nmax] = Ustrlen(*ss++); } } /* The default way of processing the command is to split it up into arguments here, and run it directly. This offers some security advantages. However, there are installations that want by default to run commands under /bin/sh always, so there is an option to do that. */ if (ob->use_shell) { if (!set_up_shell_command(&argv, cmd, expand_arguments, expand_fail, addr, tblock->name)) return FALSE; } else if (!set_up_direct_command(&argv, cmd, expand_arguments, expand_fail, addr, tblock->name, ob)) return FALSE; expand_nmax = -1; /* Reset */ filter_thisaddress = NULL; /* Set up the environment for the command. */ envp[envcount++] = string_sprintf("LOCAL_PART=%s", deliver_localpart); envp[envcount++] = string_sprintf("LOGNAME=%s", deliver_localpart); envp[envcount++] = string_sprintf("USER=%s", deliver_localpart); envp[envcount++] = string_sprintf("LOCAL_PART_PREFIX=%#s", deliver_localpart_prefix); envp[envcount++] = string_sprintf("LOCAL_PART_SUFFIX=%#s", deliver_localpart_suffix); envp[envcount++] = string_sprintf("DOMAIN=%s", deliver_domain); envp[envcount++] = string_sprintf("HOME=%#s", deliver_home); envp[envcount++] = string_sprintf("MESSAGE_ID=%s", message_id); envp[envcount++] = string_sprintf("PATH=%s", ob->path); envp[envcount++] = string_sprintf("RECIPIENT=%#s%#s%#s@%#s", deliver_localpart_prefix, deliver_localpart, deliver_localpart_suffix, deliver_domain); envp[envcount++] = string_sprintf("QUALIFY_DOMAIN=%s", qualify_domain_sender); envp[envcount++] = string_sprintf("SENDER=%s", sender_address); envp[envcount++] = US"SHELL=/bin/sh"; if (addr->host_list != NULL) envp[envcount++] = string_sprintf("HOST=%s", addr->host_list->name); if (timestamps_utc) envp[envcount++] = US"TZ=UTC"; else if (timezone_string != NULL && timezone_string[0] != 0) envp[envcount++] = string_sprintf("TZ=%s", timezone_string); /* Add any requested items */ if (envlist) { envlist = expand_cstring(envlist); if (envlist == NULL) { addr->transport_return = DEFER; addr->message = string_sprintf("failed to expand string \"%s\" " "for %s transport: %s", ob->environment, tblock->name, expand_string_message); return FALSE; } } while ((ss = string_nextinlist(&envlist, &envsep, big_buffer, big_buffer_size)) != NULL) { if (envcount > sizeof(envp)/sizeof(uschar *) - 2) { addr->transport_return = DEFER; addr->message = string_sprintf("too many environment settings for " "%s transport", tblock->name); return FALSE; } envp[envcount++] = string_copy(ss); } envp[envcount] = NULL; /* If the -N option is set, can't do any more. */ if (dont_deliver) { DEBUG(D_transport) debug_printf("*** delivery by %s transport bypassed by -N option", tblock->name); return FALSE; } /* Handling the output from the pipe is tricky. If a file for catching this output is provided, we could in theory just hand that fd over to the process, but this isn't very safe because it might loop and carry on writing for ever (which is exactly what happened in early versions of Exim). Therefore we use the standard child_open() function, which creates pipes. We can then read our end of the output pipe and count the number of bytes that come through, chopping the sub-process if it exceeds some limit. However, this means we want to run a sub-process with both its input and output attached to pipes. We can't handle that easily from a single parent process using straightforward code such as the transport_write_message() function because the subprocess might not be reading its input because it is trying to write to a full output pipe. The complication of redesigning the world to handle this is too great - simpler just to run another process to do the reading of the output pipe. */ /* As this is a local transport, we are already running with the required uid/gid and current directory. Request that the new process be a process group leader, so we can kill it and all its children on a timeout. */ if ((pid = child_open(USS argv, envp, ob->umask, &fd_in, &fd_out, TRUE)) < 0) { addr->transport_return = DEFER; addr->message = string_sprintf( "Failed to create child process for %s transport: %s", tblock->name, strerror(errno)); return FALSE; } /* Now fork a process to handle the output that comes down the pipe. */ if ((outpid = fork()) < 0) { addr->basic_errno = errno; addr->transport_return = DEFER; addr->message = string_sprintf( "Failed to create process for handling output in %s transport", tblock->name); (void)close(fd_in); (void)close(fd_out); return FALSE; } /* This is the code for the output-handling subprocess. Read from the pipe in chunks, and write to the return file if one is provided. Keep track of the number of bytes handled. If the limit is exceeded, try to kill the subprocess group, and in any case close the pipe and exit, which should cause the subprocess to fail. */ if (outpid == 0) { int count = 0; (void)close(fd_in); set_process_info("reading output from |%s", cmd); while ((rc = read(fd_out, big_buffer, big_buffer_size)) > 0) { if (addr->return_file >= 0) if(write(addr->return_file, big_buffer, rc) != rc) DEBUG(D_transport) debug_printf("Problem writing to return_file\n"); count += rc; if (count > ob->max_output) { DEBUG(D_transport) debug_printf("Too much output from pipe - killed\n"); if (addr->return_file >= 0) { uschar *message = US"\n\n*** Too much output - remainder discarded ***\n"; rc = Ustrlen(message); if(write(addr->return_file, message, rc) != rc) DEBUG(D_transport) debug_printf("Problem writing to return_file\n"); } killpg(pid, SIGKILL); break; } } (void)close(fd_out); _exit(0); } (void)close(fd_out); /* Not used in this process */ /* Carrying on now with the main parent process. Attempt to write the message to it down the pipe. It is a fallacy to think that you can detect write errors when the sub-process fails to read the pipe. The parent process may complete writing and close the pipe before the sub-process completes. We could sleep a bit here to let the sub-process get going, but it may still not complete. So we ignore all writing errors. (When in the test harness, we do do a short sleep so any debugging output is likely to be in the same order.) */ if (running_in_test_harness) millisleep(500); DEBUG(D_transport) debug_printf("Writing message to pipe\n"); /* Arrange to time out writes if there is a timeout set. */ if (timeout > 0) { sigalrm_seen = FALSE; transport_write_timeout = timeout; } /* Reset the counter of bytes written */ transport_count = 0; /* First write any configured prefix information */ if (ob->message_prefix != NULL) { uschar *prefix = expand_string(ob->message_prefix); if (prefix == NULL) { addr->transport_return = search_find_defer? DEFER : PANIC; addr->message = string_sprintf("Expansion of \"%s\" (prefix for %s " "transport) failed: %s", ob->message_prefix, tblock->name, expand_string_message); return FALSE; } if (!transport_write_block(fd_in, prefix, Ustrlen(prefix))) goto END_WRITE; } /* If the use_bsmtp option is set, we need to write SMTP prefix information. The various different values for batching are handled outside; if there is more than one address available here, all must be included. Force SMTP dot-handling. */ if (ob->use_bsmtp) { address_item *a; if (!transport_write_string(fd_in, "MAIL FROM:<%s>%s", return_path, eol)) goto END_WRITE; for (a = addr; a != NULL; a = a->next) { if (!transport_write_string(fd_in, "RCPT TO:<%s>%s", transport_rcpt_address(a, tblock->rcpt_include_affixes), eol)) goto END_WRITE; } if (!transport_write_string(fd_in, "DATA%s", eol)) goto END_WRITE; } /* Now the actual message - the options were set at initialization time */ if (!transport_write_message(addr, fd_in, ob->options, 0, tblock->add_headers, tblock->remove_headers, ob->check_string, ob->escape_string, tblock->rewrite_rules, tblock->rewrite_existflags)) goto END_WRITE; /* Now any configured suffix */ if (ob->message_suffix != NULL) { uschar *suffix = expand_string(ob->message_suffix); if (suffix == NULL) { addr->transport_return = search_find_defer? DEFER : PANIC; addr->message = string_sprintf("Expansion of \"%s\" (suffix for %s " "transport) failed: %s", ob->message_suffix, tblock->name, expand_string_message); return FALSE; } if (!transport_write_block(fd_in, suffix, Ustrlen(suffix))) goto END_WRITE; } /* If local_smtp, write the terminating dot. */ if (ob->use_bsmtp && !transport_write_string(fd_in, ".%s", eol)) goto END_WRITE; /* Flag all writing completed successfully. */ written_ok = TRUE; /* Come here if there are errors during writing. */ END_WRITE: /* OK, the writing is now all done. Close the pipe. */ (void) close(fd_in); /* Handle errors during writing. For timeouts, set the timeout for waiting for the child process to 1 second. If the process at the far end of the pipe died without reading all of it, we expect an EPIPE error, which should be ignored. We used also to ignore WRITEINCOMPLETE but the writing function is now cleverer at handling OS where the death of a pipe doesn't give EPIPE immediately. See comments therein. */ if (!written_ok) { if (errno == ETIMEDOUT) { addr->message = string_sprintf("%stimeout while writing to pipe", transport_filter_timed_out? "transport filter " : ""); addr->transport_return = ob->timeout_defer? DEFER : FAIL; timeout = 1; } else if (errno == EPIPE) { debug_printf("transport error EPIPE ignored\n"); } else { addr->transport_return = PANIC; addr->basic_errno = errno; if (errno == ERRNO_CHHEADER_FAIL) addr->message = string_sprintf("Failed to expand headers_add or headers_remove: %s", expand_string_message); else if (errno == ERRNO_FILTER_FAIL) addr->message = string_sprintf("Transport filter process failed (%d)%s", addr->more_errno, (addr->more_errno == EX_EXECFAILED)? ": unable to execute command" : ""); else if (errno == ERRNO_WRITEINCOMPLETE) addr->message = string_sprintf("Failed repeatedly to write data"); else addr->message = string_sprintf("Error %d", errno); return FALSE; } } /* Wait for the child process to complete and take action if the returned status is nonzero. The timeout will be just 1 second if any of the writes above timed out. */ if ((rc = child_close(pid, timeout)) != 0) { uschar *tmsg = (addr->message == NULL)? US"" : string_sprintf(" (preceded by %s)", addr->message); /* The process did not complete in time; kill its process group and fail the delivery. It appears to be necessary to kill the output process too, as otherwise it hangs on for some time if the actual pipe process is sleeping. (At least, that's what I observed on Solaris 2.5.1.) Since we are failing the delivery, that shouldn't cause any problem. */ if (rc == -256) { killpg(pid, SIGKILL); kill(outpid, SIGKILL); addr->transport_return = ob->timeout_defer? DEFER : FAIL; addr->message = string_sprintf("pipe delivery process timed out%s", tmsg); } /* Wait() failed. */ else if (rc == -257) { addr->transport_return = PANIC; addr->message = string_sprintf("Wait() failed for child process of %s " "transport: %s%s", tblock->name, strerror(errno), tmsg); } /* Since the transport_filter timed out we assume it has sent the child process a malformed or incomplete data stream. Kill off the child process and prevent checking its exit status as it will has probably exited in error. This prevents the transport_filter timeout message from getting overwritten by the exit error which is not the cause of the problem. */ else if (transport_filter_timed_out) { killpg(pid, SIGKILL); kill(outpid, SIGKILL); } /* Either the process completed, but yielded a non-zero (necessarily positive) status, or the process was terminated by a signal (rc will contain the negation of the signal number). Treat killing by signal as failure unless status is being ignored. By default, the message is bounced back, unless freeze_signal is set, in which case it is frozen instead. */ else if (rc < 0) { if (ob->freeze_signal) { addr->transport_return = DEFER; addr->special_action = SPECIAL_FREEZE; addr->message = string_sprintf("Child process of %s transport (running " "command \"%s\") was terminated by signal %d (%s)%s", tblock->name, cmd, -rc, os_strsignal(-rc), tmsg); } else if (!ob->ignore_status) { addr->transport_return = FAIL; addr->message = string_sprintf("Child process of %s transport (running " "command \"%s\") was terminated by signal %d (%s)%s", tblock->name, cmd, -rc, os_strsignal(-rc), tmsg); } } /* For positive values (process terminated with non-zero status), we need a status code to request deferral. A number of systems contain the following line in sysexits.h: #define EX_TEMPFAIL 75 with the description EX_TEMPFAIL -- temporary failure, indicating something that is not really an error. In sendmail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later. Based on this, we use exit code EX_TEMPFAIL as a default to mean "defer" when not ignoring the returned status. However, there is now an option that contains a list of temporary codes, with TEMPFAIL and CANTCREAT as defaults. Another case that needs special treatment is if execve() failed (typically the command that was given is a non-existent path). By default this is treated as just another failure, but if freeze_exec_fail is set, the reaction is to freeze the message rather than bounce the address. Exim used to signal this failure with EX_UNAVAILABLE, which is definined in many systems as #define EX_UNAVAILABLE 69 with the description EX_UNAVAILABLE -- A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a catchall message when something you wanted to do doesn't work, but you don't know why. However, this can be confused with a command that actually returns 69 because something *it* wanted is unavailable. At release 4.21, Exim was changed to use return code 127 instead, because this is what the shell returns when it is unable to exec a command. We define it as EX_EXECFAILED, and use it in child.c to signal execve() failure and other unexpected failures such as setuid() not working - though that won't be the case here because we aren't changing uid. */ else { /* Always handle execve() failure specially if requested to */ if (ob->freeze_exec_fail && (rc == EX_EXECFAILED)) { addr->transport_return = DEFER; addr->special_action = SPECIAL_FREEZE; addr->message = string_sprintf("pipe process failed to exec \"%s\"%s", cmd, tmsg); } /* Otherwise take action only if not ignoring status */ else if (!ob->ignore_status) { uschar *ss; int size, ptr, i; /* If temp_errors is "*" all codes are temporary. Initializion checks that it's either "*" or a list of numbers. If not "*", scan the list of temporary failure codes; if any match, the result is DEFER. */ if (ob->temp_errors[0] == '*') addr->transport_return = DEFER; else { const uschar *s = ob->temp_errors; uschar *p; uschar buffer[64]; int sep = 0; addr->transport_return = FAIL; while ((p = string_nextinlist(&s,&sep,buffer,sizeof(buffer)))) if (rc == Uatoi(p)) { addr->transport_return = DEFER; break; } } /* Ensure the message contains the expanded command and arguments. This doesn't have to be brilliantly efficient - it is an error situation. */ addr->message = string_sprintf("Child process of %s transport returned " "%d", tblock->name, rc); ptr = Ustrlen(addr->message); size = ptr + 1; /* If the return code is > 128, it often means that a shell command was terminated by a signal. */ ss = (rc > 128)? string_sprintf("(could mean shell command ended by signal %d (%s))", rc-128, os_strsignal(rc-128)) : US os_strexit(rc); if (*ss != 0) { addr->message = string_cat(addr->message, &size, &ptr, US" ", 1); addr->message = string_cat(addr->message, &size, &ptr, ss, Ustrlen(ss)); } /* Now add the command and arguments */ addr->message = string_cat(addr->message, &size, &ptr, US" from command:", 14); for (i = 0; i < sizeof(argv)/sizeof(int *) && argv[i] != NULL; i++) { BOOL quote = FALSE; addr->message = string_cat(addr->message, &size, &ptr, US" ", 1); if (Ustrpbrk(argv[i], " \t") != NULL) { quote = TRUE; addr->message = string_cat(addr->message, &size, &ptr, US"\"", 1); } addr->message = string_cat(addr->message, &size, &ptr, argv[i], Ustrlen(argv[i])); if (quote) addr->message = string_cat(addr->message, &size, &ptr, US"\"", 1); } /* Add previous filter timeout message, if present. */ if (*tmsg != 0) addr->message = string_cat(addr->message, &size, &ptr, tmsg, Ustrlen(tmsg)); addr->message[ptr] = 0; /* Ensure concatenated string terminated */ } } } /* Ensure all subprocesses (in particular, the output handling process) are complete before we pass this point. */ while (wait(&rc) >= 0); DEBUG(D_transport) debug_printf("%s transport yielded %d\n", tblock->name, addr->transport_return); /* If there has been a problem, the message in addr->message contains details of the pipe command. We don't want to expose these to the world, so we set up something bland to return to the sender. */ if (addr->transport_return != OK) addr->user_message = US"local delivery failed"; return FALSE; }
BOOL rf_queue_add(address_item *addr, address_item **paddr_local, address_item **paddr_remote, router_instance *rblock, struct passwd *pw) { addr->prop.domain_data = deliver_domain_data; /* Save these values for */ addr->prop.localpart_data = deliver_localpart_data; /* use in the transport */ /* Handle a local transport */ if (addr->transport != NULL && addr->transport->info->local) { ugid_block ugid; /* Default uid/gid and transport-time home directory are from the passwd file when check_local_user is set, but can be overridden by explicit settings. When getting the home directory out of the password information, set the flag that prevents expansion later. */ if (pw != NULL) { addr->uid = pw->pw_uid; addr->gid = pw->pw_gid; setflag(addr, af_uid_set|af_gid_set|af_home_expanded); addr->home_dir = string_copy(US pw->pw_dir); } if (!rf_get_ugid(rblock, addr, &ugid)) return FALSE; rf_set_ugid(addr, &ugid); /* transport_home_directory (in rblock->home_directory) takes priority; otherwise use the expanded value of router_home_directory. The flag also tells the transport not to re-expand it. */ if (rblock->home_directory != NULL) { addr->home_dir = rblock->home_directory; clearflag(addr, af_home_expanded); } else if (addr->home_dir == NULL && testflag(addr, af_home_expanded)) addr->home_dir = deliver_home; addr->current_dir = rblock->current_directory; addr->next = *paddr_local; *paddr_local = addr; } /* For a remote transport, set up the fallback host list, and keep a count of the total number of addresses routed to remote transports. */ else { addr->fallback_hosts = rblock->fallback_hostlist; addr->next = *paddr_remote; *paddr_remote = addr; remote_delivery_count++; } DEBUG(D_route) { debug_printf("queued for %s transport: local_part = %s\ndomain = %s\n" " errors_to=%s\n", (addr->transport == NULL)? US"<unset>" : addr->transport->name, addr->local_part, addr->domain, addr->prop.errors_address); debug_printf(" domain_data=%s localpart_data=%s\n", addr->prop.domain_data, addr->prop.localpart_data); } return TRUE; }