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