1 /* $NetBSD: defer.c,v 1.3 2022/10/08 16:12: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 /* Wietse Venema 160 /* Google, Inc. 161 /* 111 8th Avenue 162 /* New York, NY 10011, USA 163 /*--*/ 164 165 /* System library. */ 166 167 #include <sys_defs.h> 168 #include <string.h> 169 170 /* Utility library. */ 171 172 #include <msg.h> 173 #include <vstring.h> 174 175 /* Global library. */ 176 177 #define DSN_INTERN 178 #include <mail_params.h> 179 #include <mail_queue.h> 180 #include <mail_proto.h> 181 #include <flush_clnt.h> 182 #include <verify.h> 183 #include <dsn_util.h> 184 #include <rcpt_print.h> 185 #include <dsn_print.h> 186 #include <log_adhoc.h> 187 #include <trace.h> 188 #include <defer.h> 189 190 #define STR(x) vstring_str(x) 191 192 /* defer_append - defer message delivery */ 193 194 int defer_append(int flags, const char *id, MSG_STATS *stats, 195 RECIPIENT *rcpt, const char *relay, 196 DSN *dsn) 197 { 198 DSN my_dsn = *dsn; 199 DSN *dsn_res; 200 201 /* 202 * Sanity check. 203 */ 204 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) { 205 msg_warn("defer_append: ignoring dsn code \"%s\"", my_dsn.status); 206 my_dsn.status = "4.0.0"; 207 } 208 209 /* 210 * DSN filter (Postfix 3.0). 211 */ 212 if (delivery_status_filter != 0 213 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) { 214 if (dsn_res->status[0] == '5') 215 return (bounce_append_intern(flags, id, stats, rcpt, relay, dsn_res)); 216 my_dsn = *dsn_res; 217 } 218 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn)); 219 } 220 221 /* defer_append_intern - defer message delivery */ 222 223 int defer_append_intern(int flags, const char *id, MSG_STATS *stats, 224 RECIPIENT *rcpt, const char *relay, 225 DSN *dsn) 226 { 227 const char *rcpt_domain; 228 DSN my_dsn = *dsn; 229 int status; 230 231 /* 232 * MTA-requested address verification information is stored in the verify 233 * service database. 234 */ 235 if (flags & DEL_REQ_FLAG_MTA_VRFY) { 236 my_dsn.action = "undeliverable"; 237 status = verify_append(id, stats, rcpt, relay, &my_dsn, 238 DEL_RCPT_STAT_DEFER); 239 return (status); 240 } 241 242 /* 243 * User-requested address verification information is logged and mailed 244 * to the requesting user. 245 */ 246 if (flags & DEL_REQ_FLAG_USR_VRFY) { 247 my_dsn.action = "undeliverable"; 248 status = trace_append(flags, id, stats, rcpt, relay, &my_dsn); 249 return (status); 250 } 251 252 /* 253 * Normal mail delivery. May also send a delivery record to the user. 254 * 255 * XXX DSN We write all deferred recipients to the defer logfile regardless 256 * of DSN NOTIFY options, because those options don't apply to mailq(1) 257 * reports or to postmaster notifications. 258 */ 259 else { 260 261 /* 262 * Supply default action. 263 */ 264 my_dsn.action = "delayed"; 265 266 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 267 MAIL_ATTR_PROTO_BOUNCE, 268 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_APPEND), 269 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 270 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 271 SEND_ATTR_FUNC(rcpt_print, (const void *) rcpt), 272 SEND_ATTR_FUNC(dsn_print, (const void *) &my_dsn), 273 ATTR_TYPE_END) != 0) 274 msg_warn("%s: %s service failure", id, var_defer_service); 275 log_adhoc(id, stats, rcpt, relay, &my_dsn, "deferred"); 276 277 /* 278 * Traced delivery. 279 */ 280 if (flags & DEL_REQ_FLAG_RECORD) 281 if (trace_append(flags, id, stats, rcpt, relay, &my_dsn) != 0) 282 msg_warn("%s: %s service failure", id, var_trace_service); 283 284 /* 285 * Notify the fast flush service. XXX Should not this belong in the 286 * bounce/defer daemon? Well, doing it here is more robust. 287 */ 288 if ((rcpt_domain = strrchr(rcpt->address, '@')) != 0 289 && *++rcpt_domain != 0) 290 switch (flush_add(rcpt_domain, id)) { 291 case FLUSH_STAT_OK: 292 case FLUSH_STAT_DENY: 293 break; 294 default: 295 msg_warn("%s: %s service failure", id, var_flush_service); 296 break; 297 } 298 return (-1); 299 } 300 } 301 302 /* defer_flush - flush the defer log and deliver to the sender */ 303 304 int defer_flush(int flags, const char *queue, const char *id, 305 const char *encoding, int smtputf8, 306 const char *sender, const char *dsn_envid, 307 int dsn_ret) 308 { 309 flags |= BOUNCE_FLAG_DELRCPT; 310 311 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 312 MAIL_ATTR_PROTO_BOUNCE, 313 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_FLUSH), 314 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 315 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue), 316 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 317 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding), 318 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8), 319 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender), 320 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, dsn_envid), 321 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret), 322 ATTR_TYPE_END) == 0) { 323 return (0); 324 } else { 325 return (-1); 326 } 327 } 328 329 /* defer_warn - send a copy of the defer log to the sender as a warning bounce 330 * do not flush the log */ 331 332 int defer_warn(int flags, const char *queue, const char *id, 333 const char *encoding, int smtputf8, 334 const char *sender, const char *envid, int dsn_ret) 335 { 336 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service, 337 MAIL_ATTR_PROTO_BOUNCE, 338 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_WARN), 339 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags), 340 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue), 341 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id), 342 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding), 343 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8), 344 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender), 345 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, envid), 346 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret), 347 ATTR_TYPE_END) == 0) { 348 return (0); 349 } else { 350 return (-1); 351 } 352 } 353 354 /* defer_one - defer mail for one recipient */ 355 356 int defer_one(int flags, const char *queue, const char *id, 357 const char *encoding, int smtputf8, 358 const char *sender, const char *dsn_envid, 359 int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt, 360 const char *relay, DSN *dsn) 361 { 362 DSN my_dsn = *dsn; 363 DSN *dsn_res; 364 365 /* 366 * Sanity check. 367 */ 368 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) { 369 msg_warn("defer_one: ignoring dsn code \"%s\"", my_dsn.status); 370 my_dsn.status = "4.0.0"; 371 } 372 373 /* 374 * DSN filter (Postfix 3.0). 375 */ 376 if (delivery_status_filter != 0 377 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) { 378 if (dsn_res->status[0] == '5') 379 return (bounce_one_intern(flags, queue, id, encoding, smtputf8, 380 sender, dsn_envid, dsn_ret, stats, 381 rcpt, relay, dsn_res)); 382 my_dsn = *dsn_res; 383 } 384 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn)); 385 } 386