1 /* $NetBSD: forward.c,v 1.3 2020/03/18 19:05:16 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* forward 3 6 /* SUMMARY 7 /* message forwarding 8 /* SYNOPSIS 9 /* #include "local.h" 10 /* 11 /* int forward_init() 12 /* 13 /* int forward_append(attr) 14 /* DELIVER_ATTR attr; 15 /* 16 /* int forward_finish(request, attr, cancel) 17 /* DELIVER_REQUEST *request; 18 /* DELIVER_ATTR attr; 19 /* int cancel; 20 /* DESCRIPTION 21 /* This module implements the client interface for message 22 /* forwarding. 23 /* 24 /* forward_init() initializes internal data structures. 25 /* 26 /* forward_append() appends a recipient to the list of recipients 27 /* that will receive a message with the specified message sender 28 /* and delivered-to addresses. 29 /* 30 /* forward_finish() forwards the actual message contents and 31 /* releases the memory allocated by forward_init() and by 32 /* forward_append(). When the \fIcancel\fR argument is true, no 33 /* messages will be forwarded. The \fIattr\fR argument specifies 34 /* the original message delivery attributes as they were before 35 /* alias or forward expansions. 36 /* DIAGNOSTICS 37 /* A non-zero result means that the requested operation should 38 /* be tried again. 39 /* Warnings: problems connecting to the forwarding service, 40 /* corrupt message file. A corrupt message is saved to the 41 /* "corrupt" queue for further inspection. 42 /* Fatal: out of memory. 43 /* Panic: missing forward_init() or forward_finish() call. 44 /* LICENSE 45 /* .ad 46 /* .fi 47 /* The Secure Mailer license must be distributed with this software. 48 /* AUTHOR(S) 49 /* Wietse Venema 50 /* IBM T.J. Watson Research 51 /* P.O. Box 704 52 /* Yorktown Heights, NY 10598, USA 53 /* 54 /* Wietse Venema 55 /* Google, Inc. 56 /* 111 8th Avenue 57 /* New York, NY 10011, USA 58 /*--*/ 59 60 /* System library. */ 61 62 #include <sys_defs.h> 63 #include <sys/time.h> 64 #include <unistd.h> 65 66 /* Utility library. */ 67 68 #include <msg.h> 69 #include <mymalloc.h> 70 #include <htable.h> 71 #include <argv.h> 72 #include <vstring.h> 73 #include <vstream.h> 74 #include <vstring_vstream.h> 75 #include <iostuff.h> 76 #include <stringops.h> 77 78 /* Global library. */ 79 80 #include <mail_proto.h> 81 #include <cleanup_user.h> 82 #include <sent.h> 83 #include <record.h> 84 #include <rec_type.h> 85 #include <mark_corrupt.h> 86 #include <mail_date.h> 87 #include <mail_params.h> 88 #include <dsn_mask.h> 89 #include <smtputf8.h> 90 91 /* Application-specific. */ 92 93 #include "local.h" 94 95 /* 96 * Use one cleanup service connection for each (delivered to, sender) pair. 97 */ 98 static HTABLE *forward_dt; 99 100 typedef struct FORWARD_INFO { 101 VSTREAM *cleanup; /* clean up service handle */ 102 char *queue_id; /* forwarded message queue id */ 103 struct timeval posting_time; /* posting time */ 104 } FORWARD_INFO; 105 106 /* forward_init - prepare for forwarding */ 107 108 int forward_init(void) 109 { 110 111 /* 112 * Sanity checks. 113 */ 114 if (forward_dt != 0) 115 msg_panic("forward_init: missing forward_finish call"); 116 117 forward_dt = htable_create(0); 118 return (0); 119 } 120 121 /* forward_open - open connection to cleanup service */ 122 123 static FORWARD_INFO *forward_open(DELIVER_REQUEST *request, const char *sender) 124 { 125 VSTRING *buffer = vstring_alloc(100); 126 FORWARD_INFO *info; 127 VSTREAM *cleanup; 128 129 #define FORWARD_OPEN_RETURN(res) do { \ 130 vstring_free(buffer); \ 131 return (res); \ 132 } while (0) 133 134 /* 135 * Contact the cleanup service and save the new mail queue id. Request 136 * that the cleanup service bounces bad messages to the sender so that we 137 * can avoid the trouble of bounce management. 138 * 139 * In case you wonder what kind of bounces, examples are "too many hops", 140 * "message too large", perhaps some others. The reason not to bounce 141 * ourselves is that we don't really know who the recipients are. 142 */ 143 cleanup = mail_connect(MAIL_CLASS_PUBLIC, var_cleanup_service, BLOCKING); 144 if (cleanup == 0) { 145 msg_warn("connect to %s/%s: %m", 146 MAIL_CLASS_PUBLIC, var_cleanup_service); 147 FORWARD_OPEN_RETURN(0); 148 } 149 close_on_exec(vstream_fileno(cleanup), CLOSE_ON_EXEC); 150 if (attr_scan(cleanup, ATTR_FLAG_STRICT, 151 RECV_ATTR_STR(MAIL_ATTR_QUEUEID, buffer), 152 ATTR_TYPE_END) != 1) { 153 vstream_fclose(cleanup); 154 FORWARD_OPEN_RETURN(0); 155 } 156 info = (FORWARD_INFO *) mymalloc(sizeof(FORWARD_INFO)); 157 info->cleanup = cleanup; 158 info->queue_id = mystrdup(STR(buffer)); 159 GETTIMEOFDAY(&info->posting_time); 160 161 #define FORWARD_CLEANUP_FLAGS \ 162 (CLEANUP_FLAG_BOUNCE | CLEANUP_FLAG_MASK_INTERNAL \ 163 | smtputf8_autodetect(MAIL_SRC_MASK_FORWARD) \ 164 | ((request->smtputf8 & SMTPUTF8_FLAG_REQUESTED) ? \ 165 CLEANUP_FLAG_SMTPUTF8 : 0)) 166 167 attr_print(cleanup, ATTR_FLAG_NONE, 168 SEND_ATTR_INT(MAIL_ATTR_FLAGS, FORWARD_CLEANUP_FLAGS), 169 ATTR_TYPE_END); 170 171 /* 172 * Send initial message envelope information. For bounces, set the 173 * designated sender: mailing list owner, posting user, whatever. 174 */ 175 rec_fprintf(cleanup, REC_TYPE_TIME, REC_TYPE_TIME_FORMAT, 176 REC_TYPE_TIME_ARG(info->posting_time)); 177 rec_fputs(cleanup, REC_TYPE_FROM, sender); 178 179 /* 180 * Don't send the original envelope ID or full/headers return mask if it 181 * was reset due to mailing list expansion. 182 */ 183 if (request->dsn_ret) 184 rec_fprintf(cleanup, REC_TYPE_ATTR, "%s=%d", 185 MAIL_ATTR_DSN_RET, request->dsn_ret); 186 if (request->dsn_envid && *(request->dsn_envid)) 187 rec_fprintf(cleanup, REC_TYPE_ATTR, "%s=%s", 188 MAIL_ATTR_DSN_ENVID, request->dsn_envid); 189 190 /* 191 * Zero-length attribute values are place holders for unavailable 192 * attribute values. See qmgr_message.c. They are not meant to be 193 * propagated to queue files. 194 */ 195 #define PASS_ATTR(fp, name, value) do { \ 196 if ((value) && *(value)) \ 197 rec_fprintf((fp), REC_TYPE_ATTR, "%s=%s", (name), (value)); \ 198 } while (0) 199 200 /* 201 * XXX encapsulate these as one object. 202 */ 203 PASS_ATTR(cleanup, MAIL_ATTR_LOG_CLIENT_NAME, request->client_name); 204 PASS_ATTR(cleanup, MAIL_ATTR_LOG_CLIENT_ADDR, request->client_addr); 205 PASS_ATTR(cleanup, MAIL_ATTR_LOG_PROTO_NAME, request->client_proto); 206 PASS_ATTR(cleanup, MAIL_ATTR_LOG_HELO_NAME, request->client_helo); 207 PASS_ATTR(cleanup, MAIL_ATTR_SASL_METHOD, request->sasl_method); 208 PASS_ATTR(cleanup, MAIL_ATTR_SASL_USERNAME, request->sasl_username); 209 PASS_ATTR(cleanup, MAIL_ATTR_SASL_SENDER, request->sasl_sender); 210 PASS_ATTR(cleanup, MAIL_ATTR_LOG_IDENT, request->log_ident); 211 PASS_ATTR(cleanup, MAIL_ATTR_RWR_CONTEXT, request->rewrite_context); 212 213 FORWARD_OPEN_RETURN(info); 214 } 215 216 /* forward_append - append recipient to message envelope */ 217 218 int forward_append(DELIVER_ATTR attr) 219 { 220 FORWARD_INFO *info; 221 HTABLE *table_snd; 222 223 /* 224 * Sanity checks. 225 */ 226 if (msg_verbose) 227 msg_info("forward delivered=%s sender=%s recip=%s", 228 attr.delivered, attr.sender, attr.rcpt.address); 229 if (forward_dt == 0) 230 msg_panic("forward_append: missing forward_init call"); 231 232 /* 233 * In order to find the recipient list, first index a table by 234 * delivered-to header address, then by envelope sender address. 235 */ 236 if ((table_snd = (HTABLE *) htable_find(forward_dt, attr.delivered)) == 0) { 237 table_snd = htable_create(0); 238 htable_enter(forward_dt, attr.delivered, (void *) table_snd); 239 } 240 if ((info = (FORWARD_INFO *) htable_find(table_snd, attr.sender)) == 0) { 241 if ((info = forward_open(attr.request, attr.sender)) == 0) 242 return (-1); 243 htable_enter(table_snd, attr.sender, (void *) info); 244 } 245 246 /* 247 * Append the recipient to the message envelope. Don't send the original 248 * recipient or notification mask if it was reset due to mailing list 249 * expansion. 250 */ 251 if (*attr.rcpt.dsn_orcpt) 252 rec_fprintf(info->cleanup, REC_TYPE_ATTR, "%s=%s", 253 MAIL_ATTR_DSN_ORCPT, attr.rcpt.dsn_orcpt); 254 if (attr.rcpt.dsn_notify) 255 rec_fprintf(info->cleanup, REC_TYPE_ATTR, "%s=%d", 256 MAIL_ATTR_DSN_NOTIFY, attr.rcpt.dsn_notify); 257 if (*attr.rcpt.orig_addr) 258 rec_fputs(info->cleanup, REC_TYPE_ORCP, attr.rcpt.orig_addr); 259 rec_fputs(info->cleanup, REC_TYPE_RCPT, attr.rcpt.address); 260 261 return (vstream_ferror(info->cleanup)); 262 } 263 264 /* forward_send - send forwarded message */ 265 266 static int forward_send(FORWARD_INFO *info, DELIVER_REQUEST *request, 267 DELIVER_ATTR attr, char *delivered) 268 { 269 const char *myname = "forward_send"; 270 VSTRING *buffer = vstring_alloc(100); 271 VSTRING *folded; 272 int status; 273 int rec_type = 0; 274 275 /* 276 * Start the message content segment. Prepend our Delivered-To: header to 277 * the message data. Stop at the first error. XXX Rely on the front-end 278 * services to enforce record size limits. 279 */ 280 rec_fputs(info->cleanup, REC_TYPE_MESG, ""); 281 vstring_strcpy(buffer, delivered); 282 rec_fprintf(info->cleanup, REC_TYPE_NORM, "Received: by %s (%s)", 283 var_myhostname, var_mail_name); 284 rec_fprintf(info->cleanup, REC_TYPE_NORM, "\tid %s; %s", 285 info->queue_id, mail_date(info->posting_time.tv_sec)); 286 if (local_deliver_hdr_mask & DELIVER_HDR_FWD) { 287 folded = vstring_alloc(100); 288 rec_fprintf(info->cleanup, REC_TYPE_NORM, "Delivered-To: %s", 289 casefold(folded, (STR(buffer)))); 290 vstring_free(folded); 291 } 292 if ((status = vstream_ferror(info->cleanup)) == 0) 293 if (vstream_fseek(attr.fp, attr.offset, SEEK_SET) < 0) 294 msg_fatal("%s: seek queue file %s: %m:", 295 myname, VSTREAM_PATH(attr.fp)); 296 while (status == 0 && (rec_type = rec_get(attr.fp, buffer, 0)) > 0) { 297 if (rec_type != REC_TYPE_CONT && rec_type != REC_TYPE_NORM) 298 break; 299 status = (REC_PUT_BUF(info->cleanup, rec_type, buffer) != rec_type); 300 } 301 if (status == 0 && rec_type != REC_TYPE_XTRA) { 302 msg_warn("%s: bad record type: %d in message content", 303 info->queue_id, rec_type); 304 status |= mark_corrupt(attr.fp); 305 } 306 307 /* 308 * Send the end-of-data marker only when there were no errors. 309 */ 310 if (status == 0) { 311 rec_fputs(info->cleanup, REC_TYPE_XTRA, ""); 312 rec_fputs(info->cleanup, REC_TYPE_END, ""); 313 } 314 315 /* 316 * Retrieve the cleanup service completion status only if there are no 317 * problems. 318 */ 319 if (status == 0) 320 if (vstream_fflush(info->cleanup) 321 || attr_scan(info->cleanup, ATTR_FLAG_MISSING, 322 RECV_ATTR_INT(MAIL_ATTR_STATUS, &status), 323 ATTR_TYPE_END) != 1) 324 status = 1; 325 326 /* 327 * Log successful forwarding. 328 * 329 * XXX DSN alias and .forward expansion already report SUCCESS, so don't do 330 * it again here. 331 */ 332 if (status == 0) { 333 attr.rcpt.dsn_notify = 334 (attr.rcpt.dsn_notify == DSN_NOTIFY_SUCCESS ? 335 DSN_NOTIFY_NEVER : attr.rcpt.dsn_notify & ~DSN_NOTIFY_SUCCESS); 336 dsb_update(attr.why, "2.0.0", "relayed", DSB_SKIP_RMTA, DSB_SKIP_REPLY, 337 "forwarded as %s", info->queue_id); 338 status = sent(BOUNCE_FLAGS(request), SENT_ATTR(attr)); 339 } 340 341 /* 342 * Cleanup. 343 */ 344 vstring_free(buffer); 345 return (status); 346 } 347 348 /* forward_finish - complete message forwarding requests and clean up */ 349 350 int forward_finish(DELIVER_REQUEST *request, DELIVER_ATTR attr, int cancel) 351 { 352 HTABLE_INFO **dt_list; 353 HTABLE_INFO **dt; 354 HTABLE_INFO **sn_list; 355 HTABLE_INFO **sn; 356 HTABLE *table_snd; 357 char *delivered; 358 char *sender; 359 FORWARD_INFO *info; 360 int status = cancel; 361 362 /* 363 * Sanity checks. 364 */ 365 if (forward_dt == 0) 366 msg_panic("forward_finish: missing forward_init call"); 367 368 /* 369 * Walk over all delivered-to header addresses and over each envelope 370 * sender address. 371 */ 372 for (dt = dt_list = htable_list(forward_dt); *dt; dt++) { 373 delivered = dt[0]->key; 374 table_snd = (HTABLE *) dt[0]->value; 375 for (sn = sn_list = htable_list(table_snd); *sn; sn++) { 376 sender = sn[0]->key; 377 info = (FORWARD_INFO *) sn[0]->value; 378 if (status == 0) 379 status |= forward_send(info, request, attr, delivered); 380 if (msg_verbose) 381 msg_info("forward_finish: delivered %s sender %s status %d", 382 delivered, sender, status); 383 (void) vstream_fclose(info->cleanup); 384 myfree(info->queue_id); 385 myfree((void *) info); 386 } 387 myfree((void *) sn_list); 388 htable_free(table_snd, (void (*) (void *)) 0); 389 } 390 myfree((void *) dt_list); 391 htable_free(forward_dt, (void (*) (void *)) 0); 392 forward_dt = 0; 393 return (status); 394 } 395