masqmail-0.2

view src/fail_msg.c @ 3:8c55886cacd8

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