1 /* $NetBSD: smtp_map11.c,v 1.1.1.2 2013/01/02 18:59:07 tron 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 #include <valid_hostname.h> 68 69 /* Global library. */ 70 71 #include <mail_addr_map.h> 72 #include <quote_822_local.h> 73 74 /* Application-specific. */ 75 76 #include <smtp.h> 77 78 /* smtp_map11_external - one-to-one table lookups */ 79 80 int smtp_map11_external(VSTRING *addr, MAPS *maps, int propagate) 81 { 82 const char *myname = "smtp_map11_external"; 83 ARGV *new_addr; 84 const char *result; 85 86 if ((new_addr = mail_addr_map(maps, STR(addr), propagate)) != 0) { 87 if (new_addr->argc > 1) 88 msg_warn("multi-valued %s result for %s", maps->title, STR(addr)); 89 result = new_addr->argv[0]; 90 if (msg_verbose) 91 msg_info("%s: %s -> %s", myname, STR(addr), result); 92 vstring_strcpy(addr, result); 93 argv_free(new_addr); 94 return (1); 95 } else { 96 if (maps->error != 0) 97 msg_fatal("%s map lookup problem for %s", maps->title, STR(addr)); 98 if (msg_verbose) 99 msg_info("%s: %s not found", myname, STR(addr)); 100 return (0); 101 } 102 } 103 104 /* smtp_map11_tree - rewrite address node */ 105 106 int smtp_map11_tree(TOK822 *tree, MAPS *maps, int propagate) 107 { 108 VSTRING *temp = vstring_alloc(100); 109 int ret; 110 111 tok822_externalize(temp, tree->head, TOK822_STR_DEFL); 112 ret = smtp_map11_external(temp, maps, propagate); 113 tok822_free_tree(tree->head); 114 tree->head = tok822_scan(STR(temp), &tree->tail); 115 vstring_free(temp); 116 return (ret); 117 } 118 119 /* smtp_map11_internal - rewrite address internal form */ 120 121 int smtp_map11_internal(VSTRING *addr, MAPS *maps, int propagate) 122 { 123 VSTRING *temp = vstring_alloc(100); 124 int ret; 125 126 quote_822_local(temp, STR(addr)); 127 ret = smtp_map11_external(temp, maps, propagate); 128 unquote_822_local(addr, STR(temp)); 129 vstring_free(temp); 130 return (ret); 131 } 132 133 #ifdef TEST 134 135 #include <msg_vstream.h> 136 #include <stringops.h> 137 #include <mail_params.h> 138 139 int main(int argc, char **argv) 140 { 141 VSTRING *buf = vstring_alloc(100); 142 MAPS *maps; 143 144 msg_vstream_init(basename(argv[0]), VSTREAM_ERR); 145 if (argc < 3) 146 msg_fatal("usage: %s maptype:mapname address...", argv[0]); 147 148 maps = maps_create(argv[1], argv[1], DICT_FLAG_FOLD_FIX); 149 mail_params_init(); 150 if (chdir(var_queue_dir) < 0) 151 msg_fatal("chdir(%s): %m", var_queue_dir); 152 argv += 1; 153 154 msg_verbose = 1; 155 while (--argc && *++argv) { 156 msg_info("-- start %s --", *argv); 157 smtp_map11_external(vstring_strcpy(buf, *argv), maps, 1); 158 msg_info("-- end %s --", *argv); 159 } 160 vstring_free(buf); 161 maps_free(maps); 162 return (0); 163 } 164 165 #endif 166