1 /* $NetBSD: smtp_map11.c,v 1.2 2017/02/14 01:16:48 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* smtp_map11 3 6 /* SUMMARY 7 /* one-to-one address mapping 8 /* SYNOPSIS 9 /* #include <smtp.h> 10 /* 11 /* int smtp_map11_external(addr, maps, propagate) 12 /* VSTRING *addr; 13 /* MAPS *maps; 14 /* int propagate; 15 /* 16 /* int smtp_map11_internal(addr, maps, propagate) 17 /* VSTRING *addr; 18 /* MAPS *maps; 19 /* int propagate; 20 /* 21 /* int smtp_map11_tree(tree, maps, propagate) 22 /* TOK822 *tree; 23 /* MAPS *maps; 24 /* int propagate; 25 /* DESCRIPTION 26 /* This module performs non-recursive one-to-one address mapping. 27 /* An unmatched address extension is propagated when 28 /* \fIpropagate\fR is non-zero. 29 /* 30 /* smtp_map11_external() looks up the RFC 822 external (quoted) string 31 /* form of an address in the maps specified via the \fImaps\fR argument. 32 /* 33 /* smtp_map11_internal() is a wrapper around the 34 /* smtp_map11_external() routine that transforms from 35 /* internal (quoted) string form to external form and back. 36 /* 37 /* smtp_map11_tree() is a wrapper around the 38 /* smtp_map11_external() routine that transforms from 39 /* internal parse tree form to external form and back. 40 /* DIAGNOSTICS 41 /* Table lookup errors are fatal. 42 /* SEE ALSO 43 /* mail_addr_map(3) address mappings 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 55 /* System library. */ 56 57 #include <sys_defs.h> 58 #include <string.h> 59 60 /* Utility library. */ 61 62 #include <msg.h> 63 #include <vstring.h> 64 #include <dict.h> 65 #include <argv.h> 66 #include <tok822.h> 67 68 /* Global library. */ 69 70 #include <mail_addr_map.h> 71 #include <quote_822_local.h> 72 73 /* Application-specific. */ 74 75 #include <smtp.h> 76 77 /* smtp_map11_external - one-to-one table lookups */ 78 79 int smtp_map11_external(VSTRING *addr, MAPS *maps, int propagate) 80 { 81 const char *myname = "smtp_map11_external"; 82 ARGV *new_addr; 83 const char *result; 84 85 if ((new_addr = mail_addr_map(maps, STR(addr), propagate)) != 0) { 86 if (new_addr->argc > 1) 87 msg_warn("multi-valued %s result for %s", maps->title, STR(addr)); 88 result = new_addr->argv[0]; 89 if (msg_verbose) 90 msg_info("%s: %s -> %s", myname, STR(addr), result); 91 vstring_strcpy(addr, result); 92 argv_free(new_addr); 93 return (1); 94 } else { 95 if (maps->error != 0) 96 msg_fatal("%s map lookup problem for %s", maps->title, STR(addr)); 97 if (msg_verbose) 98 msg_info("%s: %s not found", myname, STR(addr)); 99 return (0); 100 } 101 } 102 103 /* smtp_map11_tree - rewrite address node */ 104 105 int smtp_map11_tree(TOK822 *tree, MAPS *maps, int propagate) 106 { 107 VSTRING *temp = vstring_alloc(100); 108 int ret; 109 110 tok822_externalize(temp, tree->head, TOK822_STR_DEFL); 111 ret = smtp_map11_external(temp, maps, propagate); 112 tok822_free_tree(tree->head); 113 tree->head = tok822_scan(STR(temp), &tree->tail); 114 vstring_free(temp); 115 return (ret); 116 } 117 118 /* smtp_map11_internal - rewrite address internal form */ 119 120 int smtp_map11_internal(VSTRING *addr, MAPS *maps, int propagate) 121 { 122 VSTRING *temp = vstring_alloc(100); 123 int ret; 124 125 quote_822_local(temp, STR(addr)); 126 ret = smtp_map11_external(temp, maps, propagate); 127 unquote_822_local(addr, STR(temp)); 128 vstring_free(temp); 129 return (ret); 130 } 131 132 #ifdef TEST 133 134 #include <msg_vstream.h> 135 #include <stringops.h> 136 #include <mail_params.h> 137 138 int main(int argc, char **argv) 139 { 140 VSTRING *buf = vstring_alloc(100); 141 MAPS *maps; 142 143 msg_vstream_init(basename(argv[0]), VSTREAM_ERR); 144 if (argc < 3) 145 msg_fatal("usage: %s maptype:mapname address...", argv[0]); 146 147 util_utf8_enable = 1; 148 maps = maps_create(argv[1], argv[1], DICT_FLAG_FOLD_FIX 149 | DICT_FLAG_UTF8_REQUEST); 150 mail_params_init(); 151 if (chdir(var_queue_dir) < 0) 152 msg_fatal("chdir(%s): %m", var_queue_dir); 153 argv += 1; 154 155 msg_verbose = 1; 156 while (--argc && *++argv) { 157 msg_info("-- start %s --", *argv); 158 smtp_map11_external(vstring_strcpy(buf, *argv), maps, 1); 159 msg_info("-- end %s --", *argv); 160 } 161 vstring_free(buf); 162 maps_free(maps); 163 return (0); 164 } 165 166 #endif 167