summaryrefslogtreecommitdiffstats
path: root/src/global/strip_addr.c
blob: c4c39b013af4b67c5fc7cfb155847ff444258121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*++
/* NAME
/*	strip_addr 3
/* SUMMARY
/*	strip extension from full or localpart-only address
/* SYNOPSIS
/*	#include <strip_addr.h>
/*
/*	char	*strip_addr_internal(address, extension, delimiter_set)
/*	const char *address;
/*	char	**extension;
/*	const char *delimiter_set;
/* LEGACY SUPPORT
/*	char	*strip_addr(address, extension, delimiter_set)
/*	const char *address;
/*	char	**extension;
/*	const char *delimiter_set;
/* DESCRIPTION
/*	strip_addr*() takes an address and either returns a null
/*	pointer when the address contains no address extension,
/*	or returns a copy of the address without address extension.
/*	The caller is expected to pass the copy to myfree().
/*
/*	With strip_addr_internal(), the input and result are in
/*	internal form.
/*
/*	strip_addr() is a backwards-compatible form for legacy code.
/*
/*	Arguments:
/* .IP address
/*	Address localpart or user@domain form.
/* .IP extension
/*	A null pointer, or the address of a pointer that is set to
/*	the address of a dynamic memory copy of the address extension
/*	that had to be chopped off.
/*	The copy includes the recipient address delimiter.
/*	The caller is expected to pass the copy to myfree().
/* .IP delimiter_set
/*	Set of recipient address delimiter characters.
/* SEE ALSO
/*	split_addr(3) strip extension from localpart
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <string.h>

/* Utility library. */

#include <mymalloc.h>

/* Global library. */

#include <split_addr.h>
#include <strip_addr.h>

/* strip_addr - strip extension from address */

char   *strip_addr_internal(const char *full, char **extension,
			            const char *delimiter_set)
{
    char   *ratsign;
    char   *extent;
    char   *saved_ext;
    char   *stripped;

    /*
     * A quick test to eliminate inputs without delimiter anywhere.
     */
    if (*delimiter_set == 0 || full[strcspn(full, delimiter_set)] == 0) {
	stripped = saved_ext = 0;
    } else {
	stripped = mystrdup(full);
	if ((ratsign = strrchr(stripped, '@')) != 0)
	    *ratsign = 0;
	if ((extent = split_addr(stripped, delimiter_set)) != 0) {
	    extent -= 1;
	    if (extension) {
		*extent = full[strlen(stripped)];
		saved_ext = mystrdup(extent);
		*extent = 0;
	    } else
		saved_ext = 0;
	    if (ratsign != 0) {
		*ratsign = '@';
		memmove(extent, ratsign, strlen(ratsign) + 1);
	    }
	} else {
	    myfree(stripped);
	    stripped = saved_ext = 0;
	}
    }
    if (extension)
	*extension = saved_ext;
    return (stripped);
}

#ifdef TEST

#include <msg.h>
#include <mail_params.h>

int     main(int unused_argc, char **unused_argv)
{
    char   *extension;
    char   *stripped;
    char   *delim = "+-";

#define NO_DELIM	""

    /*
     * No static initializer, because this is owned by a library.
     */
    var_double_bounce_sender = DEF_DOUBLE_BOUNCE;

    /*
     * Incredible. This function takes only three arguments, and the tests
     * already take more lines of code than the code being tested.
     */
    stripped = strip_addr_internal("foo", (char **) 0, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 1");

    stripped = strip_addr_internal("foo", &extension, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 2");
    if (extension != 0)
	msg_panic("strip_addr botch 3");

    stripped = strip_addr_internal("foo", (char **) 0, delim);
    if (stripped != 0)
	msg_panic("strip_addr botch 4");

    stripped = strip_addr_internal("foo", &extension, delim);
    if (stripped != 0)
	msg_panic("strip_addr botch 5");
    if (extension != 0)
	msg_panic("strip_addr botch 6");

    stripped = strip_addr_internal("foo@bar", (char **) 0, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 7");

    stripped = strip_addr_internal("foo@bar", &extension, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 8");
    if (extension != 0)
	msg_panic("strip_addr botch 9");

    stripped = strip_addr_internal("foo@bar", (char **) 0, delim);
    if (stripped != 0)
	msg_panic("strip_addr botch 10");

    stripped = strip_addr_internal("foo@bar", &extension, delim);
    if (stripped != 0)
	msg_panic("strip_addr botch 11");
    if (extension != 0)
	msg_panic("strip_addr botch 12");

    stripped = strip_addr_internal("foo-ext", (char **) 0, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 13");

    stripped = strip_addr_internal("foo-ext", &extension, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 14");
    if (extension != 0)
	msg_panic("strip_addr botch 15");

    stripped = strip_addr_internal("foo-ext", (char **) 0, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 16");
    msg_info("wanted:    foo-ext -> %s", "foo");
    msg_info("strip_addr foo-ext -> %s", stripped);
    myfree(stripped);

    stripped = strip_addr_internal("foo-ext", &extension, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 17");
    if (extension == 0)
	msg_panic("strip_addr botch 18");
    msg_info("wanted:    foo-ext -> %s %s", "foo", "-ext");
    msg_info("strip_addr foo-ext -> %s %s", stripped, extension);
    myfree(stripped);
    myfree(extension);

    stripped = strip_addr_internal("foo-ext@bar", (char **) 0, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 19");

    stripped = strip_addr_internal("foo-ext@bar", &extension, NO_DELIM);
    if (stripped != 0)
	msg_panic("strip_addr botch 20");
    if (extension != 0)
	msg_panic("strip_addr botch 21");

    stripped = strip_addr_internal("foo-ext@bar", (char **) 0, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 22");
    msg_info("wanted:    foo-ext@bar -> %s", "foo@bar");
    msg_info("strip_addr foo-ext@bar -> %s", stripped);
    myfree(stripped);

    stripped = strip_addr_internal("foo-ext@bar", &extension, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 23");
    if (extension == 0)
	msg_panic("strip_addr botch 24");
    msg_info("wanted:    foo-ext@bar -> %s %s", "foo@bar", "-ext");
    msg_info("strip_addr foo-ext@bar -> %s %s", stripped, extension);
    myfree(stripped);
    myfree(extension);

    stripped = strip_addr_internal("foo+ext@bar", &extension, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 25");
    if (extension == 0)
	msg_panic("strip_addr botch 26");
    msg_info("wanted:    foo+ext@bar -> %s %s", "foo@bar", "+ext");
    msg_info("strip_addr foo+ext@bar -> %s %s", stripped, extension);
    myfree(stripped);
    myfree(extension);

    stripped = strip_addr_internal("foo bar+ext", &extension, delim);
    if (stripped == 0)
	msg_panic("strip_addr botch 27");
    if (extension == 0)
	msg_panic("strip_addr botch 28");
    msg_info("wanted:    foo bar+ext -> %s %s", "foo bar", "+ext");
    msg_info("strip_addr foo bar+ext -> %s %s", stripped, extension);
    myfree(stripped);
    myfree(extension);

    return (0);
}

#endif