1 /* $NetBSD: strip_addr.c,v 1.1.1.1 2009/06/23 10:08:48 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* strip_addr 3 6 /* SUMMARY 7 /* strip extension from full or localpart-only address 8 /* SYNOPSIS 9 /* #include <strip_addr.h> 10 /* 11 /* char *strip_addr(address, extension, delimiter) 12 /* const char *address; 13 /* char **extension; 14 /* int delimiter; 15 /* DESCRIPTION 16 /* strip_addr() takes an address and either returns a null 17 /* pointer when the address contains no address extension, 18 /* or returns a copy of the address without address extension. 19 /* The caller is expected to pass the copy to myfree(). 20 /* 21 /* Arguments: 22 /* .IP address 23 /* Address localpart or user@domain form. 24 /* .IP extension 25 /* A null pointer, or the address of a pointer that is set to 26 /* the address of a dynamic memory copy of the address extension 27 /* that had to be chopped off. 28 /* The copy includes the recipient address delimiter. 29 /* The caller is expected to pass the copy to myfree(). 30 /* .IP delimiter 31 /* Recipient address delimiter. 32 /* SEE ALSO 33 /* split_addr(3) strip extension from localpart 34 /* LICENSE 35 /* .ad 36 /* .fi 37 /* The Secure Mailer license must be distributed with this software. 38 /* AUTHOR(S) 39 /* Wietse Venema 40 /* IBM T.J. Watson Research 41 /* P.O. Box 704 42 /* Yorktown Heights, NY 10598, USA 43 /*--*/ 44 45 /* System library. */ 46 47 #include <sys_defs.h> 48 #include <string.h> 49 50 /* Utility library. */ 51 52 #include <mymalloc.h> 53 54 /* Global library. */ 55 56 #include <split_addr.h> 57 #include <strip_addr.h> 58 59 /* strip_addr - strip extension from address */ 60 61 char *strip_addr(const char *full, char **extension, int delimiter) 62 { 63 char *ratsign; 64 char *extent; 65 char *saved_ext; 66 char *stripped; 67 68 /* 69 * A quick test to eliminate inputs without delimiter anywhere. 70 */ 71 if (delimiter == 0 || strchr(full, delimiter) == 0) { 72 stripped = saved_ext = 0; 73 } else { 74 stripped = mystrdup(full); 75 if ((ratsign = strrchr(stripped, '@')) != 0) 76 *ratsign = 0; 77 if ((extent = split_addr(stripped, delimiter)) != 0) { 78 extent -= 1; 79 if (extension) { 80 *extent = delimiter; 81 saved_ext = mystrdup(extent); 82 *extent = 0; 83 } else 84 saved_ext = 0; 85 if (ratsign != 0) { 86 *ratsign = '@'; 87 memmove(extent, ratsign, strlen(ratsign) + 1); 88 } 89 } else { 90 myfree(stripped); 91 stripped = saved_ext = 0; 92 } 93 } 94 if (extension) 95 *extension = saved_ext; 96 return (stripped); 97 } 98 99 #ifdef TEST 100 101 #include <msg.h> 102 #include <mail_params.h> 103 104 char *var_double_bounce_sender = DEF_DOUBLE_BOUNCE; 105 106 int main(int unused_argc, char **unused_argv) 107 { 108 char *extension; 109 char *stripped; 110 int delim = '-'; 111 112 /* 113 * Incredible. This function takes only three arguments, and the tests 114 * already take more lines of code than the code being tested. 115 */ 116 stripped = strip_addr("foo", (char **) 0, 0); 117 if (stripped != 0) 118 msg_panic("strip_addr botch 1"); 119 120 stripped = strip_addr("foo", &extension, 0); 121 if (stripped != 0) 122 msg_panic("strip_addr botch 2"); 123 if (extension != 0) 124 msg_panic("strip_addr botch 3"); 125 126 stripped = strip_addr("foo", (char **) 0, delim); 127 if (stripped != 0) 128 msg_panic("strip_addr botch 4"); 129 130 stripped = strip_addr("foo", &extension, delim); 131 if (stripped != 0) 132 msg_panic("strip_addr botch 5"); 133 if (extension != 0) 134 msg_panic("strip_addr botch 6"); 135 136 stripped = strip_addr("foo@bar", (char **) 0, 0); 137 if (stripped != 0) 138 msg_panic("strip_addr botch 7"); 139 140 stripped = strip_addr("foo@bar", &extension, 0); 141 if (stripped != 0) 142 msg_panic("strip_addr botch 8"); 143 if (extension != 0) 144 msg_panic("strip_addr botch 9"); 145 146 stripped = strip_addr("foo@bar", (char **) 0, delim); 147 if (stripped != 0) 148 msg_panic("strip_addr botch 10"); 149 150 stripped = strip_addr("foo@bar", &extension, delim); 151 if (stripped != 0) 152 msg_panic("strip_addr botch 11"); 153 if (extension != 0) 154 msg_panic("strip_addr botch 12"); 155 156 stripped = strip_addr("foo-ext", (char **) 0, 0); 157 if (stripped != 0) 158 msg_panic("strip_addr botch 13"); 159 160 stripped = strip_addr("foo-ext", &extension, 0); 161 if (stripped != 0) 162 msg_panic("strip_addr botch 14"); 163 if (extension != 0) 164 msg_panic("strip_addr botch 15"); 165 166 stripped = strip_addr("foo-ext", (char **) 0, delim); 167 if (stripped == 0) 168 msg_panic("strip_addr botch 16"); 169 msg_info("wanted: foo-ext -> %s", "foo"); 170 msg_info("strip_addr foo-ext -> %s", stripped); 171 myfree(stripped); 172 173 stripped = strip_addr("foo-ext", &extension, delim); 174 if (stripped == 0) 175 msg_panic("strip_addr botch 17"); 176 if (extension == 0) 177 msg_panic("strip_addr botch 18"); 178 msg_info("wanted: foo-ext -> %s %s", "foo", "-ext"); 179 msg_info("strip_addr foo-ext -> %s %s", stripped, extension); 180 myfree(stripped); 181 myfree(extension); 182 183 stripped = strip_addr("foo-ext@bar", (char **) 0, 0); 184 if (stripped != 0) 185 msg_panic("strip_addr botch 19"); 186 187 stripped = strip_addr("foo-ext@bar", &extension, 0); 188 if (stripped != 0) 189 msg_panic("strip_addr botch 20"); 190 if (extension != 0) 191 msg_panic("strip_addr botch 21"); 192 193 stripped = strip_addr("foo-ext@bar", (char **) 0, delim); 194 if (stripped == 0) 195 msg_panic("strip_addr botch 22"); 196 msg_info("wanted: foo-ext@bar -> %s", "foo@bar"); 197 msg_info("strip_addr foo-ext@bar -> %s", stripped); 198 myfree(stripped); 199 200 stripped = strip_addr("foo-ext@bar", &extension, delim); 201 if (stripped == 0) 202 msg_panic("strip_addr botch 23"); 203 if (extension == 0) 204 msg_panic("strip_addr botch 24"); 205 msg_info("wanted: foo-ext@bar -> %s %s", "foo@bar", "-ext"); 206 msg_info("strip_addr foo-ext@bar -> %s %s", stripped, extension); 207 myfree(stripped); 208 myfree(extension); 209 210 return (0); 211 } 212 213 #endif 214