masqmail

annotate src/fail_msg.c @ 414:309935f59820

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