1 /* $NetBSD: bounce_templates.c,v 1.3 2022/10/08 16:12:45 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* bounce_templates 3 6 /* SUMMARY 7 /* bounce template group support 8 /* SYNOPSIS 9 /* #include <bounce_template.h> 10 /* 11 /* typedef struct { 12 /* .in +4 13 /* BOUNCE_TEMPLATE *failure; 14 /* BOUNCE_TEMPLATE *delay; 15 /* BOUNCE_TEMPLATE *success; 16 /* BOUNCE_TEMPLATE *verify; 17 /* .in -4 18 /* } BOUNCE_TEMPLATES; 19 /* 20 /* BOUNCE_TEMPLATES *bounce_templates_create(void) 21 /* 22 /* void bounce_templates_free(templates) 23 /* BOUNCE_TEMPLATES *templates; 24 /* 25 /* void bounce_templates_load(stream, templates) 26 /* VSTREAM *stream; 27 /* BOUNCE_TEMPLATES *templates; 28 /* 29 /* void bounce_templates_expand(stream, templates) 30 /* VSTREAM *stream; 31 /* BOUNCE_TEMPLATES *templates; 32 /* 33 /* void bounce_templates_dump(stream, templates) 34 /* VSTREAM *stream; 35 /* BOUNCE_TEMPLATES *templates; 36 /* DESCRIPTION 37 /* This module implements support for bounce template groups 38 /* (i.e. groups that contain one template of each type). 39 /* 40 /* bounce_templates_create() creates a bounce template group, 41 /* with default settings. 42 /* 43 /* bounce_templates_free() destroys a bounce template group. 44 /* 45 /* bounce_templates_load() reads zero or more bounce templates 46 /* from the specified file to override built-in templates. 47 /* 48 /* bounce_templates_expand() expands $name macros and writes 49 /* the text portions of the specified bounce template group 50 /* to the specified stream. 51 /* 52 /* bounce_templates_dump() writes the complete content of the 53 /* specified bounce template group to the specified stream. 54 /* The format is compatible with bounce_templates_load(). 55 /* DIAGNOSTICS 56 /* Fatal error: out of memory, undefined macro name in template. 57 /* SEE ALSO 58 /* bounce_template(3) bounce template support 59 /* LICENSE 60 /* .ad 61 /* .fi 62 /* The Secure Mailer license must be distributed with this software. 63 /* AUTHOR(S) 64 /* Wietse Venema 65 /* IBM T.J. Watson Research 66 /* P.O. Box 704 67 /* Yorktown Heights, NY 10598, USA 68 /* 69 /* Wietse Venema 70 /* Google, Inc. 71 /* 111 8th Avenue 72 /* New York, NY 10011, USA 73 /*--*/ 74 75 /* System library. */ 76 77 #include <sys_defs.h> 78 #include <ctype.h> 79 #include <string.h> 80 81 /* Utility library. */ 82 83 #include <msg.h> 84 #include <mymalloc.h> 85 #include <stringops.h> 86 #include <vstring.h> 87 #include <vstream.h> 88 #include <vstring_vstream.h> 89 90 /* Global library. */ 91 92 #include <mail_addr.h> 93 #include <mail_proto.h> 94 95 /* Application-specific. */ 96 97 #include <bounce_template.h> 98 99 /* 100 * The fail template is for permanent failure. 101 */ 102 static const char *def_bounce_failure_body[] = { 103 "This is the mail system at host $myhostname.", 104 "", 105 "I'm sorry to have to inform you that your message could not", 106 "be delivered to one or more recipients. It's attached below.", 107 "", 108 "For further assistance, please send mail to " MAIL_ADDR_POSTMASTER ".", 109 "", 110 "If you do so, please include this problem report. You can", 111 "delete your own text from the attached returned message.", 112 "", 113 " The mail system", 114 0, 115 }; 116 117 static const BOUNCE_TEMPLATE def_bounce_failure_template = { 118 0, 119 BOUNCE_TMPL_CLASS_FAILURE, 120 "[built-in]", 121 "us-ascii", 122 MAIL_ATTR_ENC_7BIT, 123 MAIL_ADDR_MAIL_DAEMON " (Mail Delivery System)", 124 "Mail Delivery System <" MAIL_ADDR_MAIL_DAEMON ">", 125 "Undelivered Mail Returned to Sender", 126 "Postmaster Copy: Undelivered Mail", 127 def_bounce_failure_body, 128 &def_bounce_failure_template, 129 }; 130 131 /* 132 * The delay template is for delayed mail notifications. 133 */ 134 static const char *def_bounce_delay_body[] = { 135 "This is the mail system at host $myhostname.", 136 "", 137 "####################################################################", 138 "# THIS IS A WARNING ONLY. YOU DO NOT NEED TO RESEND YOUR MESSAGE. #", 139 "####################################################################", 140 "", 141 "Your message could not be delivered for more than $delay_warning_time_hours hour(s).", 142 "It will be retried until it is $maximal_queue_lifetime_days day(s) old.", 143 "", 144 "For further assistance, please send mail to " MAIL_ADDR_POSTMASTER ".", 145 "", 146 "If you do so, please include this problem report. You can", 147 "delete your own text from the attached returned message.", 148 "", 149 " The mail system", 150 0, 151 }; 152 153 static const BOUNCE_TEMPLATE def_bounce_delay_template = { 154 0, 155 BOUNCE_TMPL_CLASS_DELAY, 156 "[built-in]", 157 "us-ascii", 158 MAIL_ATTR_ENC_7BIT, 159 MAIL_ADDR_MAIL_DAEMON " (Mail Delivery System)", 160 "Mail Delivery System <" MAIL_ADDR_MAIL_DAEMON ">", 161 "Delayed Mail (still being retried)", 162 "Postmaster Warning: Delayed Mail", 163 def_bounce_delay_body, 164 &def_bounce_delay_template 165 }; 166 167 /* 168 * The success template is for "delivered", "expanded" and "relayed" success 169 * notifications. 170 */ 171 static const char *def_bounce_success_body[] = { 172 "This is the mail system at host $myhostname.", 173 "", 174 "Your message was successfully delivered to the destination(s)", 175 "listed below. If the message was delivered to mailbox you will", 176 "receive no further notifications. Otherwise you may still receive", 177 "notifications of mail delivery errors from other systems.", 178 "", 179 " The mail system", 180 0, 181 }; 182 183 static const BOUNCE_TEMPLATE def_bounce_success_template = { 184 0, 185 BOUNCE_TMPL_CLASS_SUCCESS, 186 "[built-in]", 187 "us-ascii", 188 MAIL_ATTR_ENC_7BIT, 189 MAIL_ADDR_MAIL_DAEMON " (Mail Delivery System)", 190 "Mail Delivery System <" MAIL_ADDR_MAIL_DAEMON ">", 191 "Successful Mail Delivery Report", 192 0, 193 def_bounce_success_body, 194 &def_bounce_success_template, 195 }; 196 197 /* 198 * The "verify" template is for verbose delivery (sendmail -v) and for 199 * address verification (sendmail -bv). 200 */ 201 static const char *def_bounce_verify_body[] = { 202 "This is the mail system at host $myhostname.", 203 "", 204 "Enclosed is the mail delivery report that you requested.", 205 "", 206 " The mail system", 207 0, 208 }; 209 210 static const BOUNCE_TEMPLATE def_bounce_verify_template = { 211 0, 212 BOUNCE_TMPL_CLASS_VERIFY, 213 "[built-in]", 214 "us-ascii", 215 MAIL_ATTR_ENC_7BIT, 216 MAIL_ADDR_MAIL_DAEMON " (Mail Delivery System)", 217 "Mail Delivery System <" MAIL_ADDR_MAIL_DAEMON ">", 218 "Mail Delivery Status Report", 219 0, 220 def_bounce_verify_body, 221 &def_bounce_verify_template, 222 }; 223 224 /* 225 * SLMs. 226 */ 227 #define STR(x) vstring_str(x) 228 229 /* bounce_templates_create - create template group */ 230 231 BOUNCE_TEMPLATES *bounce_templates_create(void) 232 { 233 BOUNCE_TEMPLATES *bs; 234 235 bs = (BOUNCE_TEMPLATES *) mymalloc(sizeof(*bs)); 236 bs->failure = bounce_template_create(&def_bounce_failure_template); 237 bs->delay = bounce_template_create(&def_bounce_delay_template); 238 bs->success = bounce_template_create(&def_bounce_success_template); 239 bs->verify = bounce_template_create(&def_bounce_verify_template); 240 return (bs); 241 } 242 243 /* bounce_templates_free - destroy template group */ 244 245 void bounce_templates_free(BOUNCE_TEMPLATES *bs) 246 { 247 bounce_template_free(bs->failure); 248 bounce_template_free(bs->delay); 249 bounce_template_free(bs->success); 250 bounce_template_free(bs->verify); 251 myfree((void *) bs); 252 } 253 254 /* bounce_templates_load - load template or group from stream */ 255 256 void bounce_templates_load(VSTREAM *fp, BOUNCE_TEMPLATES *ts) 257 { 258 VSTRING *line_buf; 259 char *member_name; 260 VSTRING *multi_line_buf = 0; 261 VSTRING *saved_member_name = 0; 262 VSTRING *saved_end_marker = 0; 263 char *value; 264 int lineno; 265 const char *err; 266 char *cp; 267 int len; /* Grr... */ 268 269 /* 270 * XXX That's a lot of non-reusable code to parse a configuration file. 271 * Unfortunately, much of the "name = value" infrastructure is married to 272 * the dict(3) class which doesn't really help here. 273 */ 274 line_buf = vstring_alloc(100); 275 lineno = 1; 276 while (vstring_get_nonl(line_buf, fp) > 0) { 277 lineno++; 278 cp = STR(line_buf) + strspn(STR(line_buf), " \t\n\v\f\r"); 279 if (*cp == 0 || *cp == '#') 280 continue; 281 if ((err = split_nameval(STR(line_buf), &member_name, &value)) != 0) 282 msg_fatal("%s, line %d: %s: \"%s\"", 283 VSTREAM_PATH(fp), lineno, err, STR(line_buf)); 284 if (value[0] == '<' && value[1] == '<') { 285 value += 2; 286 while (ISSPACE(*value)) 287 value++; 288 if (*value == 0) 289 msg_fatal("%s, line %d: missing end marker after <<", 290 VSTREAM_PATH(fp), lineno); 291 if (!ISALNUM(*value)) 292 msg_fatal("%s, line %d: malformed end marker after <<", 293 VSTREAM_PATH(fp), lineno); 294 if (multi_line_buf == 0) { 295 saved_member_name = vstring_alloc(100); 296 saved_end_marker = vstring_alloc(100); 297 multi_line_buf = vstring_alloc(100); 298 } else 299 VSTRING_RESET(multi_line_buf); 300 vstring_strcpy(saved_member_name, member_name); 301 vstring_strcpy(saved_end_marker, value); 302 while (vstring_get_nonl(line_buf, fp) > 0) { 303 lineno++; 304 if (strcmp(STR(line_buf), STR(saved_end_marker)) == 0) 305 break; 306 if (VSTRING_LEN(multi_line_buf) > 0) 307 vstring_strcat(multi_line_buf, "\n"); 308 vstring_strcat(multi_line_buf, STR(line_buf)); 309 } 310 if (vstream_feof(fp)) 311 msg_warn("%s, line %d: missing \"%s\" end marker", 312 VSTREAM_PATH(fp), lineno, value); 313 member_name = STR(saved_member_name); 314 value = STR(multi_line_buf); 315 } 316 #define MATCH_TMPL_NAME(tname, tname_len, mname) \ 317 (strncmp(tname, mname, tname_len = strlen(tname)) == 0 \ 318 && strcmp(mname + tname_len, "_template") == 0) 319 320 if (MATCH_TMPL_NAME(ts->failure->class, len, member_name)) 321 bounce_template_load(ts->failure, VSTREAM_PATH(fp), value); 322 else if (MATCH_TMPL_NAME(ts->delay->class, len, member_name)) 323 bounce_template_load(ts->delay, VSTREAM_PATH(fp), value); 324 else if (MATCH_TMPL_NAME(ts->success->class, len, member_name)) 325 bounce_template_load(ts->success, VSTREAM_PATH(fp), value); 326 else if (MATCH_TMPL_NAME(ts->verify->class, len, member_name)) 327 bounce_template_load(ts->verify, VSTREAM_PATH(fp), value); 328 else 329 msg_warn("%s, line %d: unknown template name: %s " 330 "-- ignoring this template", 331 VSTREAM_PATH(fp), lineno, member_name); 332 } 333 vstring_free(line_buf); 334 if (multi_line_buf) { 335 vstring_free(saved_member_name); 336 vstring_free(saved_end_marker); 337 vstring_free(multi_line_buf); 338 } 339 } 340 341 /* bounce_plain_out - output line as plain text */ 342 343 static int bounce_plain_out(VSTREAM *fp, const char *text) 344 { 345 vstream_fprintf(fp, "%s\n", text); 346 return (0); 347 } 348 349 /* bounce_templates_expand - dump expanded template group text to stream */ 350 351 void bounce_templates_expand(VSTREAM *fp, BOUNCE_TEMPLATES *ts) 352 { 353 BOUNCE_TEMPLATE *tp; 354 355 tp = ts->failure; 356 vstream_fprintf(fp, "expanded_%s_text = <<EOF\n", tp->class); 357 bounce_template_expand(bounce_plain_out, fp, tp); 358 vstream_fprintf(fp, "EOF\n\n"); 359 360 tp = ts->delay; 361 vstream_fprintf(fp, "expanded_%s_text = <<EOF\n", tp->class); 362 bounce_template_expand(bounce_plain_out, fp, tp); 363 vstream_fprintf(fp, "EOF\n\n"); 364 365 tp = ts->success; 366 vstream_fprintf(fp, "expanded_%s_text = <<EOF\n", tp->class); 367 bounce_template_expand(bounce_plain_out, fp, tp); 368 vstream_fprintf(fp, "EOF\n\n"); 369 370 tp = ts->verify; 371 vstream_fprintf(fp, "expanded_%s_text = <<EOF\n", tp->class); 372 bounce_template_expand(bounce_plain_out, fp, tp); 373 vstream_fprintf(fp, "EOF\n"); 374 } 375 376 /* bounce_templates_dump - dump bounce template group to stream */ 377 378 void bounce_templates_dump(VSTREAM *fp, BOUNCE_TEMPLATES *ts) 379 { 380 BOUNCE_TEMPLATE *tp; 381 382 tp = ts->failure; 383 vstream_fprintf(fp, "%s_template = <<EOF\n", tp->class); 384 bounce_template_dump(fp, tp); 385 vstream_fprintf(fp, "EOF\n\n"); 386 387 tp = ts->delay; 388 vstream_fprintf(fp, "%s_template = <<EOF\n", tp->class); 389 bounce_template_dump(fp, tp); 390 vstream_fprintf(fp, "EOF\n\n"); 391 392 tp = ts->success; 393 vstream_fprintf(fp, "%s_template = <<EOF\n", tp->class); 394 bounce_template_dump(fp, tp); 395 vstream_fprintf(fp, "EOF\n\n"); 396 397 tp = ts->verify; 398 vstream_fprintf(fp, "%s_template = <<EOF\n", tp->class); 399 bounce_template_dump(fp, tp); 400 vstream_fprintf(fp, "EOF\n"); 401 } 402