masqmail

annotate src/fail_msg.c @ 19:7354c2e0eb31

added myself to AUTHORS and the source files I improved
author meillo@marmaro.de
date Thu, 06 Nov 2008 10:36:56 +0100
parents 26e34ae9a3e3
children dcb315792513
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 2000-2001 Oliver Kurth
meillo@0 3 *
meillo@0 4 * This program is free software; you can redistribute it and/or modify
meillo@0 5 * it under the terms of the GNU General Public License as published by
meillo@0 6 * the Free Software Foundation; either version 2 of the License, or
meillo@0 7 * (at your option) any later version.
meillo@10 8 *
meillo@0 9 * This program is distributed in the hope that it will be useful,
meillo@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 * GNU General Public License for more details.
meillo@0 13 *
meillo@0 14 * You should have received a copy of the GNU General Public License
meillo@0 15 * along with this program; if not, write to the Free Software
meillo@0 16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include <sys/wait.h>
meillo@0 20
meillo@0 21 #include "masqmail.h"
meillo@0 22 #include "peopen.h"
meillo@0 23 #include "readsock.h"
meillo@0 24
meillo@10 25 gboolean
meillo@10 26 fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args)
meillo@0 27 {
meillo@10 28 gboolean ok = FALSE;
meillo@10 29 address *ret_path = NULL;
meillo@0 30
meillo@10 31 /* do not bounce bounces, send to postmaster instead */
meillo@15 32 if (msg->return_path->local_part[0] == '\0') {
meillo@10 33 GList *node;
meillo@0 34
meillo@10 35 ret_path = create_address_qualified("postmaster", TRUE, conf.host_name);
meillo@10 36 foreach(failed_rcpts, node) {
meillo@10 37 address *addr = (address *) (node->data);
meillo@10 38 if (addr_isequal_parent(addr, ret_path)) {
meillo@10 39 logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n", msg->uid, addr_string(ret_path));
meillo@10 40 return FALSE;
meillo@10 41 }
meillo@10 42 }
meillo@10 43 } else
meillo@10 44 ret_path = copy_address(msg->return_path);
meillo@0 45
meillo@10 46 DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path));
meillo@0 47
meillo@10 48 if (template) {
meillo@10 49 FILE *file;
meillo@10 50 GList *var_table = var_table_conf(var_table_msg(NULL, msg));
meillo@10 51 gchar *err_msg = g_strdup_vprintf(err_fmt, args);
meillo@0 52
meillo@10 53 var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg));
meillo@10 54 g_free(err_msg);
meillo@0 55
meillo@10 56 if ((file = fopen(template, "r"))) {
meillo@10 57 FILE *out;
meillo@10 58 gchar *cmd;
meillo@10 59 pid_t pid;
meillo@0 60
meillo@10 61 // cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s",
meillo@10 62 // ret_path->local_part, ret_path->domain);
meillo@10 63 cmd = g_strdup_printf(SBINDIR "/masqmail -oi -f <> %s@%s", ret_path->local_part, ret_path->domain);
meillo@10 64 if ((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))) {
meillo@10 65 gchar fmt[256], line[256];
meillo@10 66 int status, ret;
meillo@0 67
meillo@10 68 while ((ret = read_sockline(file, fmt, 256, 0, 0)) > 0) {
meillo@10 69 if (fmt[0] == '@') {
meillo@10 70 GList *node;
meillo@10 71 if (strncmp(fmt, "@failed_rcpts", 13) == 0) {
meillo@10 72 foreach(failed_rcpts, node) {
meillo@10 73 address *rcpt = (address *) (node->data);
meillo@10 74 fprintf(out, "\t%s\n", addr_string(rcpt));
meillo@10 75 }
meillo@10 76 } else if (strncmp(fmt, "@msg_headers", 12) == 0) {
meillo@10 77 foreach(msg->hdr_list, node) {
meillo@10 78 header *hdr = (header *) (node->data);
meillo@10 79 fputs(hdr->header, out);
meillo@10 80 }
meillo@10 81 } else if (strncmp(fmt, "@msg_body", 9) == 0) {
meillo@15 82 /* we may have to read the data at this point and remember if we did */
meillo@10 83 gboolean flag = (msg->data_list == NULL);
meillo@10 84 if (flag) {
meillo@10 85 if (!spool_read_data(msg)) {
meillo@10 86 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
meillo@10 87 }
meillo@10 88 }
meillo@10 89 foreach(msg->data_list, node) {
meillo@10 90 gchar *line = (gchar *) (node->data);
meillo@10 91 fputs(line, out);
meillo@10 92 }
meillo@10 93 if (flag)
meillo@10 94 msg_free_data(msg);
meillo@10 95 }
meillo@10 96 } else {
meillo@10 97 expand(var_table, fmt, line, 256);
meillo@10 98 fputs(line, out);
meillo@10 99 }
meillo@10 100 }
meillo@10 101
meillo@10 102 fclose(out);
meillo@10 103 waitpid(pid, &status, 0);
meillo@10 104 if ((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)) {
meillo@10 105 if (WEXITSTATUS(status) != EXIT_SUCCESS)
meillo@10 106 logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status));
meillo@10 107 if (WIFSIGNALED(status))
meillo@10 108 logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status));
meillo@10 109 } else
meillo@10 110 ok = TRUE;
meillo@10 111 } else {
meillo@10 112 logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno));
meillo@10 113 }
meillo@10 114 g_free(cmd);
meillo@10 115 fclose(file);
meillo@10 116 } else
meillo@10 117 logwrite(LOG_ALERT, "could not open failure message template %s: %s\n", conf.errmsg_file, strerror(errno));
meillo@10 118
meillo@10 119 destroy_table(var_table);
meillo@0 120 }
meillo@0 121
meillo@10 122 destroy_address(ret_path);
meillo@0 123
meillo@10 124 return ok;
meillo@0 125 }
meillo@0 126
meillo@0 127 /*
meillo@0 128 ival : |--|--|----|--------|--------|
meillo@0 129 warned: |-------W-------------W------
meillo@0 130 result: |nnnyyyynnnnyyyyyyyyyynnnnnnn
meillo@0 131 */
meillo@0 132
meillo@10 133 static gboolean
meillo@10 134 warn_msg_is_due(message * msg)
meillo@0 135 {
meillo@10 136 time_t now = time(NULL);
meillo@10 137 gint dummy;
meillo@10 138
meillo@10 139 GList *node;
meillo@10 140 for (node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)) {
meillo@10 141 gchar *str_ival = (gchar *) (node->data);
meillo@10 142 gint ival = time_interval(str_ival, &dummy);
meillo@10 143 if (ival >= 0) {
meillo@10 144 DEBUG(5) debugf("ival = %d\n", ival);
meillo@10 145 DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time);
meillo@10 146 if ((now - msg->received_time) > ival) {
meillo@10 147 if (msg->warned_time != 0) {
meillo@10 148 if ((msg->warned_time - msg->received_time) < ival)
meillo@10 149 return TRUE;
meillo@10 150 } else
meillo@10 151 return TRUE;
meillo@10 152 }
meillo@10 153 } else
meillo@10 154 logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival);
meillo@10 155 }
meillo@10 156 return FALSE;
meillo@0 157 }
meillo@0 158
meillo@10 159 gboolean
meillo@10 160 warn_msg(message * msg, gchar * template, GList * defered_rcpts, gchar * err_fmt, va_list args)
meillo@0 161 {
meillo@10 162 time_t now = time(NULL);
meillo@0 163
meillo@10 164 if (warn_msg_is_due(msg)) {
meillo@10 165 if (fail_msg(msg, template, defered_rcpts, err_fmt, args)) {
meillo@10 166 msg->warned_time = now;
meillo@10 167 return TRUE;
meillo@10 168 } else
meillo@10 169 return FALSE;
meillo@10 170 }
meillo@10 171 return TRUE;
meillo@0 172 }