masqmail-0.2

annotate src/fail_msg.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
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@0 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@0 25 gboolean fail_msg(message *msg, gchar *template,
meillo@0 26 GList *failed_rcpts, gchar *err_fmt, va_list args)
meillo@0 27 {
meillo@0 28 gboolean ok = FALSE;
meillo@0 29 address *ret_path = NULL;
meillo@0 30
meillo@0 31 /* do not bounce bounces, send to postmaster instead */
meillo@0 32 if(msg->return_path->local_part[0] == 0){
meillo@0 33 GList *node;
meillo@0 34
meillo@0 35 ret_path = create_address_qualified("postmaster", TRUE, conf.host_name);
meillo@0 36 foreach(failed_rcpts, node){
meillo@0 37 address *addr = (address *)(node->data);
meillo@0 38 if(addr_isequal_parent(addr, ret_path)){
meillo@0 39 logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n",
meillo@0 40 msg->uid, addr_string(ret_path));
meillo@0 41 return FALSE;
meillo@0 42 }
meillo@0 43 }
meillo@0 44 }else
meillo@0 45 ret_path = copy_address(msg->return_path);
meillo@0 46
meillo@0 47 DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path));
meillo@0 48
meillo@0 49 if(template){
meillo@0 50 FILE *file;
meillo@0 51 GList *var_table = var_table_conf(var_table_msg(NULL, msg));
meillo@0 52 gchar *err_msg = g_strdup_vprintf(err_fmt, args);
meillo@0 53
meillo@0 54 var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg));
meillo@0 55 g_free(err_msg);
meillo@0 56
meillo@0 57 if((file = fopen(template, "r"))){
meillo@0 58 FILE *out;
meillo@0 59 gchar *cmd;
meillo@0 60 pid_t pid;
meillo@0 61
meillo@0 62 // cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s",
meillo@0 63 // ret_path->local_part, ret_path->domain);
meillo@0 64 cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f <> %s@%s",
meillo@0 65 ret_path->local_part, ret_path->domain);
meillo@0 66 if((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))){
meillo@0 67 gchar fmt[256], line[256];
meillo@0 68 int status, ret;
meillo@0 69
meillo@0 70 while((ret = read_sockline(file, fmt, 256, 0, 0)) > 0){
meillo@0 71 if(fmt[0] == '@'){
meillo@0 72 GList *node;
meillo@0 73 if(strncmp(fmt, "@failed_rcpts", 13) == 0){
meillo@0 74 foreach(failed_rcpts, node){
meillo@0 75 address *rcpt = (address *)(node->data);
meillo@0 76 fprintf(out, "\t%s\n", addr_string(rcpt));
meillo@0 77 }
meillo@0 78 }else if(strncmp(fmt, "@msg_headers", 12) == 0){
meillo@0 79 foreach(msg->hdr_list, node){
meillo@0 80 header *hdr = (header *)(node->data);
meillo@0 81 fputs(hdr->header, out);
meillo@0 82 }
meillo@0 83 }else if(strncmp(fmt, "@msg_body", 9) == 0){
meillo@0 84 /* we may have to read the data at this point
meillo@0 85 and remember if we did */
meillo@0 86 gboolean flag = (msg->data_list == NULL);
meillo@0 87 if(flag){
meillo@0 88 if(!spool_read_data(msg)){
meillo@0 89 logwrite(LOG_ALERT, "could not open data spool file %s\n",
meillo@0 90 msg->uid);
meillo@0 91 }
meillo@0 92 }
meillo@0 93 foreach(msg->data_list, node){
meillo@0 94 gchar *line = (gchar *)(node->data);
meillo@0 95 fputs(line, out);
meillo@0 96 }
meillo@0 97 if(flag) msg_free_data(msg);
meillo@0 98 }
meillo@0 99 }else{
meillo@0 100 expand(var_table, fmt, line, 256);
meillo@0 101 fputs(line, out);
meillo@0 102 }
meillo@0 103 }
meillo@0 104
meillo@0 105 fclose(out);
meillo@0 106 waitpid(pid, &status, 0);
meillo@0 107 if((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)){
meillo@0 108 if(WEXITSTATUS(status) != EXIT_SUCCESS)
meillo@0 109 logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status));
meillo@0 110 if(WIFSIGNALED(status))
meillo@0 111 logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status));
meillo@0 112 }else ok = TRUE;
meillo@0 113 }else{
meillo@0 114 logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno));
meillo@0 115 }
meillo@0 116 g_free(cmd);
meillo@0 117 fclose(file);
meillo@0 118 }else
meillo@0 119 logwrite(LOG_ALERT, "could not open failure message template %s: %s\n",
meillo@0 120 conf.errmsg_file, strerror(errno));
meillo@0 121
meillo@0 122 destroy_table(var_table);
meillo@0 123 }
meillo@0 124
meillo@0 125 destroy_address(ret_path);
meillo@0 126
meillo@0 127 return ok;
meillo@0 128 }
meillo@0 129
meillo@0 130 /*
meillo@0 131 ival : |--|--|----|--------|--------|
meillo@0 132 warned: |-------W-------------W------
meillo@0 133 result: |nnnyyyynnnnyyyyyyyyyynnnnnnn
meillo@0 134 */
meillo@0 135
meillo@0 136 static
meillo@0 137 gboolean warn_msg_is_due(message *msg)
meillo@0 138 {
meillo@0 139 time_t now = time(NULL);
meillo@0 140 gint dummy;
meillo@0 141
meillo@0 142 GList *node;
meillo@0 143 for(node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)){
meillo@0 144 gchar *str_ival = (gchar *)(node->data);
meillo@0 145 gint ival = time_interval(str_ival, &dummy);
meillo@0 146 if(ival >= 0){
meillo@0 147 DEBUG(5) debugf("ival = %d\n", ival);
meillo@0 148 DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time);
meillo@0 149 if((now - msg->received_time) > ival){
meillo@0 150 if(msg->warned_time != 0){
meillo@0 151 if((msg->warned_time - msg->received_time) < ival)
meillo@0 152 return TRUE;
meillo@0 153 }else
meillo@0 154 return TRUE;
meillo@0 155 }
meillo@0 156 }else
meillo@0 157 logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival);
meillo@0 158 }
meillo@0 159 return FALSE;
meillo@0 160 }
meillo@0 161
meillo@0 162 gboolean warn_msg(message *msg, gchar *template,
meillo@0 163 GList *defered_rcpts, gchar *err_fmt, va_list args)
meillo@0 164 {
meillo@0 165 time_t now = time(NULL);
meillo@0 166
meillo@0 167 if(warn_msg_is_due(msg)){
meillo@0 168 if(fail_msg(msg, template, defered_rcpts, err_fmt, args)){
meillo@0 169 msg->warned_time = now;
meillo@0 170 return TRUE;
meillo@0 171 }else
meillo@0 172 return FALSE;
meillo@0 173 }
meillo@0 174 return TRUE;
meillo@0 175 }
meillo@0 176
meillo@0 177