comparison src/fail_msg.c @ 10:26e34ae9a3e3

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