forked from trusteddomainproject/OpenDMARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopendmarc_util.c
489 lines (461 loc) · 8.53 KB
/
opendmarc_util.c
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*************************************************************************
** Copyright (c) 2012, 2014, 2016, The Trusted Domain Project.
** All rights reserved.
**************************************************************************/
#include "opendmarc_internal.h"
/* libbsd if found */
#ifdef USE_BSD_H
# include <bsd/string.h>
#endif /* USE_BSD_H */
/* libstrl if needed */
#ifdef USE_STRL_H
# include <strl.h>
#endif /* USE_STRL_H */
/* opendmarc_strl if needed */
#ifdef USE_DMARCSTRL_H
# include <opendmarc_strl.h>
#endif /* USE_DMARCSTRL_H */
/*****************************************************
** OPENDMARC_UTIL_CLEARARGV -- Free the argv array
**
** Parameters:
** ary -- Pointer to array to free
** Returns:
** NULL always
** Side Effects:
** Allocates and reallocates memory.
*/
u_char **
opendmarc_util_clearargv(u_char **ary)
{
if (ary != NULL)
{
u_char **arp;
for (arp = ary; *arp != NULL; ++arp)
{
(void) free(*arp);
*arp = NULL;
}
(void) free(ary);
ary = NULL;
}
return ary;
}
/*****************************************************
** OPENDMARC_UTIL_PUSHARGV -- Add to and array of strings.
**
** Parameters:
** str -- The string to add.
** ary -- The array to extend.
** cnt -- Points to number of elements.
** Returns:
** ary on success.
** NULL on error and sets errno.
** Side Effects:
** Allocates and reallocates memory.
*/
u_char **
opendmarc_util_pushargv(u_char *str, u_char **ary, int *cnt)
{
int i;
u_char **tmp;
if (str == NULL)
return ary;
if (ary == NULL)
{
ary = malloc(sizeof(char **) * 2);
if (ary == NULL)
{
return NULL;
}
ary[0] = strdup(str);
ary[1] = NULL;
if (ary[0] == NULL)
{
(void) free(ary);
return NULL;
}
if (cnt != NULL)
*cnt = 1;
return ary;
}
if (cnt == NULL)
{
for (i = 0; ;i++)
{
if (ary[i] == NULL)
break;
}
}
else
i = *cnt;
tmp = realloc((void *)ary, sizeof(char **) * (i+2));
if (tmp == NULL)
{
ary = opendmarc_util_clearargv(ary);
return NULL;
}
ary = tmp;
ary[i] = strdup(str);
if (ary[i] == NULL)
{
ary = opendmarc_util_clearargv(ary);
return NULL;
}
ary[i+1] = NULL;
if (cnt != NULL)
*cnt = i + 1;
return ary;
}
/*****************************************************
** OPENDMARC_UTIL_DUPE_ARGV -- Duplicate an argv
**
** Parameters:
** ary -- Pointer to array to dupe
** Returns:
** u_char ** -- On success
** NULL -- on error
** Side Effects:
** Allocates and reallocates memory.
*/
u_char **
opendmarc_util_dupe_argv(u_char **ary)
{
u_char **new = NULL;
int new_cnt = 0;
if (ary != NULL)
{
u_char **arp;
for (arp = ary; *arp != NULL; ++arp)
new = opendmarc_util_pushargv(*arp, new, &new_cnt);
}
return new;
}
/*****************************************************
** OPENDMARC_UTIL_CLEANUP -- Remove whitespace
**
** Parameters:
** str -- The string cleanup
** buf -- Where to place result
** buflen -- Length of buf
** Returns:
** buf on success.
** NULL on error and sets errno.
** Side Effects:
*/
u_char *
opendmarc_util_cleanup(u_char *str, u_char *buf, size_t buflen)
{
char *sp, *ep;
if (str == NULL || buf == NULL || strlen((char *)str) > buflen)
{
errno = EINVAL;
return NULL;
}
(void) memset(buf, '\0', buflen);
for (sp = str, ep = buf; *sp != '\0'; sp++)
{
if (!isascii(*sp) || !isspace(*sp))
*ep++ = *sp;
}
return buf;
}
/************************************************************
** OPENDMARC_UTIL_FINDDOMAIN --Focus on the domain
**
** Parameters:
** raw -- The address containing domain
** buf -- Where to place result
** buflen -- Length of buf
** Returns:
** buf on success.
** NULL on error and sets errno.
** Side Effects:
** e.g. (foo) [email protected] (bar) ---> a.com
** "foo" <[email protected]> "foo" --> a.com
*/
u_char *
opendmarc_util_finddomain(u_char *raw, u_char *buf, size_t buflen)
{
u_char *a = NULL;
u_char *b = NULL;
u_char *ep = NULL;
u_char copy[BUFSIZ];
u_char *cp = NULL;
int inparen = 0;
#define OPENDMARC_MAX_QUOTES (256)
int quotes[OPENDMARC_MAX_QUOTES + 1];
int numquotes = 0;
size_t len;
if (raw == NULL)
return NULL;
(void) memset(copy, '\0', sizeof copy);
len = strlen((char *)raw);
if (len > BUFSIZ)
len = BUFSIZ - 1;
(void) strncpy(copy, raw, len);
/*
* Quoted commas do not delimit addresses.
* Un-quoted ones do.
*/
for (cp = copy; *cp != '\0'; ++cp)
{
/*
* <> has a higher precedence than quotes.
* Prevents "From: Davide D'Marco <[email protected]>" from breaking.
*/
if (*cp == '<')
break;
if (numquotes == 0 && *cp == ',')
{
*cp = '\0';
break;
}
if (numquotes > 0 && *cp == ')')
{
if (quotes[numquotes-1] == ')')
{
--numquotes;
*cp = ' ';
continue;
}
}
if (*cp == '"' || *cp == '\'' || *cp == '(')
{
if (*cp == '(')
*cp = ')';
if (numquotes == 0)
{
quotes[numquotes] = *cp;
++numquotes;
*cp = ' ';
continue;
}
if (*cp == quotes[numquotes -1])
{
--numquotes;
*cp = ' ';
continue;
}
quotes[numquotes] = *cp;
if (numquotes >= OPENDMARC_MAX_QUOTES)
break;
++numquotes;
*cp = ' ';
continue;
}
if (numquotes > 0)
*cp = ' ';
}
ep = copy + strlen((char *)copy);
for (b = ep-1; b > copy; --b)
{
if (*b == '<')
break;
}
if (*b == '<')
{
for (a = b; a < ep; ++a)
{
if (*a == '>')
break;
}
if (*a == '>')
{
*a = '\0';
cp = ++b;
goto strip_local_part;
}
}
for (a = copy; a < ep; a++)
{
if (isspace((int)*a))
continue;
if (*a == '(')
{
inparen = 1;
continue;
}
if (inparen == 1 && *a != ')')
continue;
if (inparen == 1 && *a == ')')
{
inparen = 0;
continue;
}
break;
}
for (b = ep -1; b > a; --b)
{
if (isspace((int)*b))
continue;
if (*b == ')')
{
inparen = 1;
continue;
}
if (inparen == 1 && *b != '(')
continue;
if (inparen == 1 && *b == '(')
{
inparen = 0;
continue;
}
break;
}
*(b+1) ='\0';
cp = a;
strip_local_part:
if (cp == NULL)
cp = copy;
ep = strchr(cp, '@');
if (ep != NULL)
cp = ep + 1;
len = strlen((char *)cp);
if (len > buflen)
cp[buflen -1] = '\0';
len = strlen((char *)cp);
if (len > 0)
{
/*
* If the domain name ends in a dot, drop that dot.
*/
ep = cp + len -1;
if (*ep == '.')
*ep = '\0';
}
(void) strlcpy(buf, cp, buflen);
return buf;
}
char **
opendmarc_util_freenargv(char **ary, int *num)
{
if (ary != NULL)
{
char **ccp;
for (ccp = ary; *ccp != NULL; ++ccp)
{
(void) free(*ccp);
*ccp = NULL;
}
(void) free(ary);
ary = NULL;
}
if (num != NULL)
*num = 0;
return NULL;
}
char **
opendmarc_util_pushnargv(char *str, char **ary, int *num)
{
int i;
char **tmp;
if (str != NULL)
{
if (ary == NULL)
{
ary = calloc(sizeof(char **), 2);
if (ary == NULL)
{
if (num != NULL)
*num = 0;
return NULL;
}
*ary = strdup(str);
*(ary+1) = NULL;
if (*ary == NULL)
{
(void) free(ary);
ary = NULL;
if (num != NULL)
*num = 0;
return NULL;
}
if (num != NULL)
*num = 1;
return ary;
}
i = 0;
if (num == NULL)
{
for (i = 0; ;i++)
{
if (ary[i] == NULL)
break;
}
}
else
i = *num;
tmp = realloc((void *)ary, sizeof(char **) * (i+2));
if (tmp == NULL)
{
ary = opendmarc_util_freenargv(ary, num);
return NULL;
}
ary = tmp;
ary[i] = strdup(str);
if (ary[i] == NULL)
{
ary = opendmarc_util_freenargv(ary, num);
return NULL;
}
++i;
ary[i] = NULL;
if (num != NULL)
*num = i;
}
return ary;
}
/*
** Convert a decimal unsigned long interger into a string.
** Returns a pointer to the passed buffer.
*/
char *
opendmarc_util_ultoa(unsigned long val, char *buffer, size_t bufferlen)
{
register char *b = buffer;
register size_t l = bufferlen;
register unsigned long v = val;
register long mod, d, digit;
#define MAXDIGITS (32)
int digits[MAXDIGITS];
if (b == NULL || l < 2)
return NULL;
if (v == 0)
{
*b++ = '0';
*b = '\0';
return buffer;
}
digit = 0;
do
{
mod = v % 10;
v = v / 10;
digits[digit] = mod;
++digit;
if (digit >= MAXDIGITS)
break;
} while(v != 0);
for (d = digit-1; d >= 0; --d)
{
switch (digits[d])
{
case 0: *b++ = '0'; --l; break;
case 1: *b++ = '1'; --l; break;
case 2: *b++ = '2'; --l; break;
case 3: *b++ = '3'; --l; break;
case 4: *b++ = '4'; --l; break;
case 5: *b++ = '5'; --l; break;
case 6: *b++ = '6'; --l; break;
case 7: *b++ = '7'; --l; break;
case 8: *b++ = '8'; --l; break;
case 9: *b++ = '9'; --l; break;
}
if (l == 1)
break;
}
*b = '\0';
return buffer;
}