meillo@0: /* MasqMail meillo@0: Copyright (C) 2000-2001 Oliver Kurth meillo@0: * meillo@0: * This program is free software; you can redistribute it and/or modify meillo@0: * it under the terms of the GNU General Public License as published by meillo@0: * the Free Software Foundation; either version 2 of the License, or meillo@0: * (at your option) any later version. meillo@10: * meillo@0: * This program is distributed in the hope that it will be useful, meillo@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: * GNU General Public License for more details. meillo@0: * meillo@0: * You should have received a copy of the GNU General Public License meillo@0: * along with this program; if not, write to the Free Software meillo@0: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: meillo@0: #include "masqmail.h" meillo@0: #include "peopen.h" meillo@0: #include "readsock.h" meillo@0: meillo@10: gboolean meillo@10: fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args) meillo@0: { meillo@10: gboolean ok = FALSE; meillo@10: address *ret_path = NULL; meillo@0: meillo@10: /* do not bounce bounces, send to postmaster instead */ meillo@10: if (msg->return_path->local_part[0] == 0) { meillo@10: GList *node; meillo@0: meillo@10: ret_path = create_address_qualified("postmaster", TRUE, conf.host_name); meillo@10: foreach(failed_rcpts, node) { meillo@10: address *addr = (address *) (node->data); meillo@10: if (addr_isequal_parent(addr, ret_path)) { meillo@10: logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n", msg->uid, addr_string(ret_path)); meillo@10: return FALSE; meillo@10: } meillo@10: } meillo@10: } else meillo@10: ret_path = copy_address(msg->return_path); meillo@0: meillo@10: DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path)); meillo@0: meillo@10: if (template) { meillo@10: FILE *file; meillo@10: GList *var_table = var_table_conf(var_table_msg(NULL, msg)); meillo@10: gchar *err_msg = g_strdup_vprintf(err_fmt, args); meillo@0: meillo@10: var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg)); meillo@10: g_free(err_msg); meillo@0: meillo@10: if ((file = fopen(template, "r"))) { meillo@10: FILE *out; meillo@10: gchar *cmd; meillo@10: pid_t pid; meillo@0: meillo@10: // cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s", meillo@10: // ret_path->local_part, ret_path->domain); meillo@10: cmd = g_strdup_printf(SBINDIR "/masqmail -oi -f <> %s@%s", ret_path->local_part, ret_path->domain); meillo@10: if ((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))) { meillo@10: gchar fmt[256], line[256]; meillo@10: int status, ret; meillo@0: meillo@10: while ((ret = read_sockline(file, fmt, 256, 0, 0)) > 0) { meillo@10: if (fmt[0] == '@') { meillo@10: GList *node; meillo@10: if (strncmp(fmt, "@failed_rcpts", 13) == 0) { meillo@10: foreach(failed_rcpts, node) { meillo@10: address *rcpt = (address *) (node->data); meillo@10: fprintf(out, "\t%s\n", addr_string(rcpt)); meillo@10: } meillo@10: } else if (strncmp(fmt, "@msg_headers", 12) == 0) { meillo@10: foreach(msg->hdr_list, node) { meillo@10: header *hdr = (header *) (node->data); meillo@10: fputs(hdr->header, out); meillo@10: } meillo@10: } else if (strncmp(fmt, "@msg_body", 9) == 0) { meillo@10: /* we may have to read the data at this point meillo@10: and remember if we did */ meillo@10: gboolean flag = (msg->data_list == NULL); meillo@10: if (flag) { meillo@10: if (!spool_read_data(msg)) { meillo@10: logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid); meillo@10: } meillo@10: } meillo@10: foreach(msg->data_list, node) { meillo@10: gchar *line = (gchar *) (node->data); meillo@10: fputs(line, out); meillo@10: } meillo@10: if (flag) meillo@10: msg_free_data(msg); meillo@10: } meillo@10: } else { meillo@10: expand(var_table, fmt, line, 256); meillo@10: fputs(line, out); meillo@10: } meillo@10: } meillo@10: meillo@10: fclose(out); meillo@10: waitpid(pid, &status, 0); meillo@10: if ((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)) { meillo@10: if (WEXITSTATUS(status) != EXIT_SUCCESS) meillo@10: logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status)); meillo@10: if (WIFSIGNALED(status)) meillo@10: logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status)); meillo@10: } else meillo@10: ok = TRUE; meillo@10: } else { meillo@10: logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno)); meillo@10: } meillo@10: g_free(cmd); meillo@10: fclose(file); meillo@10: } else meillo@10: logwrite(LOG_ALERT, "could not open failure message template %s: %s\n", conf.errmsg_file, strerror(errno)); meillo@10: meillo@10: destroy_table(var_table); meillo@0: } meillo@0: meillo@10: destroy_address(ret_path); meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@0: /* meillo@0: ival : |--|--|----|--------|--------| meillo@0: warned: |-------W-------------W------ meillo@0: result: |nnnyyyynnnnyyyyyyyyyynnnnnnn meillo@0: */ meillo@0: meillo@10: static gboolean meillo@10: warn_msg_is_due(message * msg) meillo@0: { meillo@10: time_t now = time(NULL); meillo@10: gint dummy; meillo@10: meillo@10: GList *node; meillo@10: for (node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)) { meillo@10: gchar *str_ival = (gchar *) (node->data); meillo@10: gint ival = time_interval(str_ival, &dummy); meillo@10: if (ival >= 0) { meillo@10: DEBUG(5) debugf("ival = %d\n", ival); meillo@10: DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time); meillo@10: if ((now - msg->received_time) > ival) { meillo@10: if (msg->warned_time != 0) { meillo@10: if ((msg->warned_time - msg->received_time) < ival) meillo@10: return TRUE; meillo@10: } else meillo@10: return TRUE; meillo@10: } meillo@10: } else meillo@10: logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival); meillo@10: } meillo@10: return FALSE; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: warn_msg(message * msg, gchar * template, GList * defered_rcpts, gchar * err_fmt, va_list args) meillo@0: { meillo@10: time_t now = time(NULL); meillo@0: meillo@10: if (warn_msg_is_due(msg)) { meillo@10: if (fail_msg(msg, template, defered_rcpts, err_fmt, args)) { meillo@10: msg->warned_time = now; meillo@10: return TRUE; meillo@10: } else meillo@10: return FALSE; meillo@10: } meillo@10: return TRUE; meillo@0: }