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