1 /* $NetBSD: cleanup_addr.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* cleanup_addr 3 6 /* SUMMARY 7 /* process envelope addresses 8 /* SYNOPSIS 9 /* #include <cleanup.h> 10 /* 11 /* void cleanup_addr_sender(state, addr) 12 /* CLEANUP_STATE *state; 13 /* const char *addr; 14 /* 15 /* void cleanup_addr_recipient(state, addr) 16 /* CLEANUP_STATE *state; 17 /* const char *addr; 18 /* 19 /* void cleanup_addr_bcc(state, addr) 20 /* CLEANUP_STATE *state; 21 /* const char *addr; 22 /* DESCRIPTION 23 /* This module processes envelope address records and writes the result 24 /* to the queue file. Processing includes address rewriting and 25 /* sender/recipient auto bcc address generation. 26 /* 27 /* cleanup_addr_sender() processes sender envelope information and updates 28 /* state->sender. 29 /* 30 /* cleanup_addr_recipient() processes recipient envelope information 31 /* and updates state->recip. 32 /* 33 /* cleanup_addr_bcc() processes recipient envelope information. This 34 /* is a separate function to avoid invoking cleanup_addr_recipient() 35 /* recursively. 36 /* 37 /* Arguments: 38 /* .IP state 39 /* Queue file and message processing state. This state is updated 40 /* as records are processed and as errors happen. 41 /* .IP buf 42 /* Record content. 43 /* LICENSE 44 /* .ad 45 /* .fi 46 /* The Secure Mailer license must be distributed with this software. 47 /* AUTHOR(S) 48 /* Wietse Venema 49 /* IBM T.J. Watson Research 50 /* P.O. Box 704 51 /* Yorktown Heights, NY 10598, USA 52 /*--*/ 53 54 /* System library. */ 55 56 #include <sys_defs.h> 57 #include <string.h> 58 #include <stdlib.h> 59 60 #ifdef STRCASECMP_IN_STRINGS_H 61 #include <strings.h> 62 #endif 63 64 /* Utility library. */ 65 66 #include <msg.h> 67 #include <vstring.h> 68 #include <vstream.h> 69 #include <mymalloc.h> 70 #include <stringops.h> 71 72 /* Global library. */ 73 74 #include <rec_type.h> 75 #include <cleanup_user.h> 76 #include <mail_params.h> 77 #include <ext_prop.h> 78 #include <mail_addr.h> 79 #include <canon_addr.h> 80 #include <mail_addr_find.h> 81 #include <mail_proto.h> 82 #include <dsn_mask.h> 83 84 /* Application-specific. */ 85 86 #include "cleanup.h" 87 88 #define STR vstring_str 89 #define IGNORE_EXTENSION (char **) 0 90 91 /* cleanup_addr_sender - process envelope sender record */ 92 93 void cleanup_addr_sender(CLEANUP_STATE *state, const char *buf) 94 { 95 VSTRING *clean_addr = vstring_alloc(100); 96 const char *bcc; 97 98 /* 99 * Note: an unqualified envelope address is for all practical purposes 100 * equivalent to a fully qualified local address, both for delivery and 101 * for replying. Having to support both forms is error prone, therefore 102 * an incomplete envelope address is rewritten to fully qualified form in 103 * the local domain context. 104 * 105 * 20000520: Replace mailer-daemon@$myorigin by the null address, to handle 106 * bounced mail traffic more robustly. 107 */ 108 cleanup_rewrite_internal(MAIL_ATTR_RWR_LOCAL, clean_addr, buf); 109 if (strncasecmp(STR(clean_addr), MAIL_ADDR_MAIL_DAEMON "@", 110 sizeof(MAIL_ADDR_MAIL_DAEMON)) == 0) { 111 canon_addr_internal(state->temp1, MAIL_ADDR_MAIL_DAEMON); 112 if (strcasecmp(STR(clean_addr), STR(state->temp1)) == 0) 113 vstring_strcpy(clean_addr, ""); 114 } 115 if (state->flags & CLEANUP_FLAG_MAP_OK) { 116 if (cleanup_send_canon_maps 117 && (cleanup_send_canon_flags & CLEANUP_CANON_FLAG_ENV_FROM)) 118 cleanup_map11_internal(state, clean_addr, cleanup_send_canon_maps, 119 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 120 if (cleanup_comm_canon_maps 121 && (cleanup_comm_canon_flags & CLEANUP_CANON_FLAG_ENV_FROM)) 122 cleanup_map11_internal(state, clean_addr, cleanup_comm_canon_maps, 123 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 124 if (cleanup_masq_domains 125 && (cleanup_masq_flags & CLEANUP_MASQ_FLAG_ENV_FROM)) 126 cleanup_masquerade_internal(clean_addr, cleanup_masq_domains); 127 } 128 CLEANUP_OUT_BUF(state, REC_TYPE_FROM, clean_addr); 129 if (state->sender) /* XXX Can't happen */ 130 myfree(state->sender); 131 state->sender = mystrdup(STR(clean_addr)); /* Used by Milter client */ 132 if ((state->flags & CLEANUP_FLAG_BCC_OK) 133 && *STR(clean_addr) 134 && cleanup_send_bcc_maps 135 && (bcc = mail_addr_find(cleanup_send_bcc_maps, STR(clean_addr), 136 IGNORE_EXTENSION)) != 0) 137 cleanup_addr_bcc(state, bcc); 138 vstring_free(clean_addr); 139 } 140 141 /* cleanup_addr_recipient - process envelope recipient */ 142 143 void cleanup_addr_recipient(CLEANUP_STATE *state, const char *buf) 144 { 145 VSTRING *clean_addr = vstring_alloc(100); 146 const char *bcc; 147 148 /* 149 * Note: an unqualified envelope address is for all practical purposes 150 * equivalent to a fully qualified local address, both for delivery and 151 * for replying. Having to support both forms is error prone, therefore 152 * an incomplete envelope address is rewritten to fully qualified form in 153 * the local domain context. 154 */ 155 cleanup_rewrite_internal(MAIL_ATTR_RWR_LOCAL, 156 clean_addr, *buf ? buf : var_empty_addr); 157 if (state->flags & CLEANUP_FLAG_MAP_OK) { 158 if (cleanup_rcpt_canon_maps 159 && (cleanup_rcpt_canon_flags & CLEANUP_CANON_FLAG_ENV_RCPT)) 160 cleanup_map11_internal(state, clean_addr, cleanup_rcpt_canon_maps, 161 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 162 if (cleanup_comm_canon_maps 163 && (cleanup_comm_canon_flags & CLEANUP_CANON_FLAG_ENV_RCPT)) 164 cleanup_map11_internal(state, clean_addr, cleanup_comm_canon_maps, 165 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 166 if (cleanup_masq_domains 167 && (cleanup_masq_flags & CLEANUP_MASQ_FLAG_ENV_RCPT)) 168 cleanup_masquerade_internal(clean_addr, cleanup_masq_domains); 169 } 170 cleanup_out_recipient(state, state->dsn_orcpt, state->dsn_notify, 171 state->orig_rcpt, STR(clean_addr)); 172 if (state->recip) /* This can happen */ 173 myfree(state->recip); 174 state->recip = mystrdup(STR(clean_addr)); /* Used by Milter client */ 175 if ((state->flags & CLEANUP_FLAG_BCC_OK) 176 && *STR(clean_addr) 177 && cleanup_rcpt_bcc_maps 178 && (bcc = mail_addr_find(cleanup_rcpt_bcc_maps, STR(clean_addr), 179 IGNORE_EXTENSION)) != 0) 180 cleanup_addr_bcc(state, bcc); 181 vstring_free(clean_addr); 182 } 183 184 /* cleanup_addr_bcc - process automatic BCC recipient */ 185 186 void cleanup_addr_bcc(CLEANUP_STATE *state, const char *bcc) 187 { 188 VSTRING *clean_addr = vstring_alloc(100); 189 190 /* 191 * Note: BCC addresses are supplied locally, and must be rewritten in the 192 * local address rewriting context. 193 */ 194 #define NO_DSN_ORCPT ((char *) 0) 195 196 cleanup_rewrite_internal(MAIL_ATTR_RWR_LOCAL, clean_addr, bcc); 197 if (state->flags & CLEANUP_FLAG_MAP_OK) { 198 if (cleanup_rcpt_canon_maps 199 && (cleanup_rcpt_canon_flags & CLEANUP_CANON_FLAG_ENV_RCPT)) 200 cleanup_map11_internal(state, clean_addr, cleanup_rcpt_canon_maps, 201 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 202 if (cleanup_comm_canon_maps 203 && (cleanup_comm_canon_flags & CLEANUP_CANON_FLAG_ENV_RCPT)) 204 cleanup_map11_internal(state, clean_addr, cleanup_comm_canon_maps, 205 cleanup_ext_prop_mask & EXT_PROP_CANONICAL); 206 if (cleanup_masq_domains 207 && (cleanup_masq_flags & CLEANUP_MASQ_FLAG_ENV_RCPT)) 208 cleanup_masquerade_internal(clean_addr, cleanup_masq_domains); 209 } 210 cleanup_out_recipient(state, NO_DSN_ORCPT, DSN_NOTIFY_NEVER, 211 STR(clean_addr), STR(clean_addr)); 212 vstring_free(clean_addr); 213 } 214