1 /* $NetBSD: defer.c,v 1.2 2017/02/14 01:16:45 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* defer 3 6 /* SUMMARY 7 /* defer service client interface 8 /* SYNOPSIS 9 /* #include <defer.h> 10 /* 11 /* int defer_append(flags, id, stats, rcpt, relay, dsn) 12 /* int flags; 13 /* const char *id; 14 /* MSG_STATS *stats; 15 /* RECIPIENT *rcpt; 16 /* const char *relay; 17 /* DSN *dsn; 18 /* 19 /* int defer_flush(flags, queue, id, encoding, smtputf8, sender, 20 /* dsn_envid, dsn_ret) 21 /* int flags; 22 /* const char *queue; 23 /* const char *id; 24 /* const char *encoding; 25 /* int smtputf8; 26 /* const char *sender; 27 /* const char *dsn_envid; 28 /* int dsn_ret; 29 /* 30 /* int defer_warn(flags, queue, id, encoding, smtputf8, sender, 31 dsn_envid, dsn_ret) 32 /* int flags; 33 /* const char *queue; 34 /* const char *id; 35 /* const char *encoding; 36 /* int smtputf8; 37 /* const char *sender; 38 /* const char *dsn_envid; 39 /* int dsn_ret; 40 /* 41 /* int defer_one(flags, queue, id, encoding, smtputf8, sender, 42 /* dsn_envid, ret, stats, recipient, relay, dsn) 43 /* int flags; 44 /* const char *queue; 45 /* const char *id; 46 /* const char *encoding; 47 /* int smtputf8; 48 /* const char *sender; 49 /* const char *dsn_envid; 50 /* int dsn_ret; 51 /* MSG_STATS *stats; 52 /* RECIPIENT *rcpt; 53 /* const char *relay; 54 /* DSN *dsn; 55 /* INTERNAL API 56 /* int defer_append_intern(flags, id, stats, rcpt, relay, dsn) 57 /* int flags; 58 /* const char *id; 59 /* MSG_STATS *stats; 60 /* RECIPIENT *rcpt; 61 /* const char *relay; 62 /* DESCRIPTION 63 /* This module implements a client interface to the defer service, 64 /* which maintains a per-message logfile with status records for 65 /* each recipient whose delivery is deferred, and the dsn_text why. 66 /* 67 /* defer_append() appends a record to the per-message defer log, 68 /* with the dsn_text for delayed delivery to the named rcpt, 69 /* updates the address verification service, or updates a message 70 /* delivery record on request by the sender. The flags argument 71 /* determines the action. 72 /* The result is a convenient non-zero value. 73 /* When the fast flush cache is enabled, the fast flush server is 74 /* notified of deferred mail. 75 /* 76 /* defer_flush() bounces the specified message to the specified 77 /* sender, including the defer log that was built with defer_append(). 78 /* defer_flush() requests that the deferred recipients are deleted 79 /* from the original queue file; the defer logfile is deleted after 80 /* successful completion. 81 /* The result is zero in case of success, non-zero otherwise. 82 /* 83 /* defer_warn() sends a warning message that the mail in 84 /* question has been deferred. The defer log is not deleted, 85 /* and no recipients are deleted from the original queue file. 86 /* 87 /* defer_one() implements dsn_filter(3) compatibility for the 88 /* bounce_one() routine. 89 /* 90 /* defer_append_intern() is for use after the DSN filter. 91 /* 92 /* Arguments: 93 /* .IP flags 94 /* The bit-wise OR of zero or more of the following (specify 95 /* BOUNCE_FLAG_NONE to explicitly request not special processing): 96 /* .RS 97 /* .IP BOUNCE_FLAG_CLEAN 98 /* Delete the defer log in case of an error (as in: pretend 99 /* that we never even tried to defer this message). 100 /* .IP BOUNCE_FLAG_DELRCPT 101 /* When specified with a flush request, request that 102 /* recipients be deleted from the queue file. 103 /* 104 /* Note: the bounce daemon ignores this request when the 105 /* recipient queue file offset is <= 0. 106 /* .IP DEL_REQ_FLAG_MTA_VRFY 107 /* The message is an MTA-requested address verification probe. 108 /* Update the address verification database instead of deferring 109 /* mail. 110 /* .IP DEL_REQ_FLAG_USR_VRFY 111 /* The message is a user-requested address expansion probe. 112 /* Update the message delivery record instead of deferring 113 /* mail. 114 /* .IP DEL_REQ_FLAG_RECORD 115 /* This is a normal message with logged delivery. Update the 116 /* message delivery record and defer mail delivery. 117 /* .RE 118 /* .IP queue 119 /* The message queue name of the original message file. 120 /* .IP id 121 /* The queue id of the original message file. 122 /* .IP stats 123 /* Time stamps from different message delivery stages 124 /* and session reuse count. 125 /* .IP rcpt 126 /* Recipient information. See recipient_list(3). 127 /* .IP relay 128 /* Host we could not talk to. 129 /* .IP dsn 130 /* Delivery status. See dsn(3). The specified action is ignored. 131 /* .IP encoding 132 /* The body content encoding: MAIL_ATTR_ENC_{7BIT,8BIT,NONE}. 133 /* .IP smtputf8 134 /* The level of SMTPUTF8 support (to be defined). 135 /* .IP sender 136 /* The sender envelope address. 137 /* .IP dsn_envid 138 /* Optional DSN envelope ID. 139 /* .IP dsn_ret 140 /* Optional DSN return full/headers option. 141 /* .PP 142 /* For convenience, these functions always return a non-zero result. 143 /* DIAGNOSTICS 144 /* Warnings: problems connecting to the defer service. 145 /* Fatal: out of memory. 146 /* BUGS 147 /* Should be replaced by routines with an attribute-value based 148 /* interface instead of an interface that uses a rigid argument list. 149 /* LICENSE 150 /* .ad 151 /* .fi 152 /* The Secure Mailer license must be distributed with this software. 153 /* AUTHOR(S) 154 /* Wietse Venema 155 /* IBM T.J. Watson Research 156 /* P.O. Box 704 157 /* Yorktown Heights, NY 10598, USA 158 /*--*/ 159 160 /* System library. */ 161 162 #include <sys_defs.h> 163 #include <string.h> 164 165 /* Utility library. */ 166 167 #include <msg.h> 168 #include <vstring.h> 169 170 /* Global library. */ 171 172 #define DSN_INTERN 173 #include <mail_params.h> 174 #include <mail_queue.h> 175 #include <mail_proto.h> 176 #include <flush_clnt.h> 177 #include <verify.h> 178 #include <dsn_util.h> 179 #include <rcpt_print.h> 180 #include <dsn_print.h> 181 #include <log_adhoc.h> 182 #include <trace.h> 183 #include <defer.h> 184 185 #define STR(x) vstring_str(x) 186 187 /* defer_append - defer message delivery */ 188 189 int defer_append(int flags, const char *id, MSG_STATS *stats, 190 RECIPIENT *rcpt, const char *relay, 191 DSN *dsn) 192 { 193 DSN my_dsn = *dsn; 194 DSN *dsn_res; 195 196 /* 197 * Sanity check. 198 */ 199 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) { 200 msg_warn("defer_append: ignoring dsn code \"%s\"", my_dsn.status); 201 my_dsn.status = "4.0.0"; 202 } 203 204 /* 205 * DSN filter (Postfix 3.0). 206 */ 207 if (delivery_status_filter != 0 208 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) { 209 if (dsn_res->status[0] == '5') 210 return (bounce_append_intern(flags, id, stats, rcpt, relay, dsn_res)); 211 my_dsn = *dsn_res; 212 } 213 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn)); 214 } 215 216 /* defer_append_intern - defer message delivery */ 217 218 int defer_append_intern(int flags, const char *id, MSG_STATS *stats, 219 RECIPIENT *rcpt, const char *relay, 220 DSN *dsn) 221 { 222 const char *rcpt_domain; 223 DSN my_dsn = *dsn; 224 int status; 225 226 /* 227 * MTA-requested address verification information is stored in the verify 228 * service database. 229 */ 230 if (flags & DEL_REQ_FLAG_MTA_VRFY) { 231 my_dsn.action = "undeliverable"; 232 status = verify_append(id, stats, rcpt, relay, &my_dsn, 233 DEL_RCPT_STAT_DEFER); 234 return (status); 235 } 236 237 /* 238 * User-requested address verification information is logged and mailed 239 * to the requesting user. 240 */ 241 if (flags & DEL_REQ_FLAG_USR_VRFY) { 242 my_dsn.action = "undeliverable"; 243 status = trace_append(flags, id, stats, rcpt, relay, &my_dsn); 244 return (status); 245 } 246 247 /* 248 * Normal mail delivery. May also send a delivery record to the user. 249 * 250 * XXX DSN We write all deferred recipients to the defer logfile regardless 251 * of DSN NOTIFY options, because those options don't apply to mailq(1) 252 * reports or to postmaster notifications. 253 */ 254 else { 255 256 /* 257 * Supply default action. 258 */ 259 my_dsn.action = "delayed"; 260 261 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 262 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_APPEND), 263 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 264 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 265 SEND_ATTR_FUNC(rcpt_print, (void *) rcpt), 266 SEND_ATTR_FUNC(dsn_print, (void *) &my_dsn), 267 ATTR_TYPE_END) != 0) 268 msg_warn("%s: %s service failure", id, var_defer_service); 269 log_adhoc(id, stats, rcpt, relay, &my_dsn, "deferred"); 270 271 /* 272 * Traced delivery. 273 */ 274 if (flags & DEL_REQ_FLAG_RECORD) 275 if (trace_append(flags, id, stats, rcpt, relay, &my_dsn) != 0) 276 msg_warn("%s: %s service failure", id, var_trace_service); 277 278 /* 279 * Notify the fast flush service. XXX Should not this belong in the 280 * bounce/defer daemon? Well, doing it here is more robust. 281 */ 282 if ((rcpt_domain = strrchr(rcpt->address, '@')) != 0 283 && *++rcpt_domain != 0) 284 switch (flush_add(rcpt_domain, id)) { 285 case FLUSH_STAT_OK: 286 case FLUSH_STAT_DENY: 287 break; 288 default: 289 msg_warn("%s: %s service failure", id, var_flush_service); 290 break; 291 } 292 return (-1); 293 } 294 } 295 296 /* defer_flush - flush the defer log and deliver to the sender */ 297 298 int defer_flush(int flags, const char *queue, const char *id, 299 const char *encoding, int smtputf8, 300 const char *sender, const char *dsn_envid, 301 int dsn_ret) 302 { 303 flags |= BOUNCE_FLAG_DELRCPT; 304 305 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 306 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_FLUSH), 307 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 308 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue), 309 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 310 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding), 311 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8), 312 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender), 313 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, dsn_envid), 314 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret), 315 ATTR_TYPE_END) == 0) { 316 return (0); 317 } else { 318 return (-1); 319 } 320 } 321 322 /* defer_warn - send a copy of the defer log to the sender as a warning bounce 323 * do not flush the log */ 324 325 int defer_warn(int flags, const char *queue, const char *id, 326 const char *encoding, int smtputf8, 327 const char *sender, const char *envid, int dsn_ret) 328 { 329 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 330 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_WARN), 331 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 332 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue), 333 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 334 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding), 335 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8), 336 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender), 337 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, envid), 338 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret), 339 ATTR_TYPE_END) == 0) { 340 return (0); 341 } else { 342 return (-1); 343 } 344 } 345 346 /* defer_one - defer mail for one recipient */ 347 348 int defer_one(int flags, const char *queue, const char *id, 349 const char *encoding, int smtputf8, 350 const char *sender, const char *dsn_envid, 351 int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt, 352 const char *relay, DSN *dsn) 353 { 354 DSN my_dsn = *dsn; 355 DSN *dsn_res; 356 357 /* 358 * Sanity check. 359 */ 360 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) { 361 msg_warn("defer_one: ignoring dsn code \"%s\"", my_dsn.status); 362 my_dsn.status = "4.0.0"; 363 } 364 365 /* 366 * DSN filter (Postfix 3.0). 367 */ 368 if (delivery_status_filter != 0 369 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) { 370 if (dsn_res->status[0] == '5') 371 return (bounce_one_intern(flags, queue, id, encoding, smtputf8, 372 sender, dsn_envid, dsn_ret, stats, 373 rcpt, relay, dsn_res)); 374 my_dsn = *dsn_res; 375 } 376 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn)); 377 } 378