forked from trusteddomainproject/OpenDMARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopendmarc_tld.c
311 lines (282 loc) · 7.11 KB
/
opendmarc_tld.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
/*************************************************************************
** Copyright (c) 2012, 2014, 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 */
static OPENDMARC_HASH_CTX *TLD_hctx = NULL;
static OPENDMARC_HASH_CTX *TLDbak_hctx = NULL;
# if HAVE_PTHREAD_H || HAVE_PTHREAD
static pthread_mutex_t TLD_hctx_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static char TLDfile[MAXPATHLEN];
int
opendmarc_reverse_domain(u_char *domain, u_char *buf, size_t buflen)
{
u_char *dp, *ep, *cp;
u_char copy[MAXDNSHOSTNAME];
if (buf == NULL || buflen == 0 || domain == NULL)
{
return EINVAL;
}
(void) memset((char *)buf, '\0', buflen);
/*
* Strip all but one leading dot.
*/
for (cp = domain; *cp != '\0'; ++cp)
{
if (*cp != '.')
break;
}
if (strlen(cp) == 0)
{
return EINVAL;
}
if (cp > domain)
--cp;
(void) memset((char *)copy, '\0', sizeof copy);
(void) strlcpy((char *)copy, cp, sizeof copy);
ep = copy + strlen((char *)copy);
/*
* Strip all trailing dots.
*/
for (cp = ep-1; cp > copy; --cp)
{
if (*cp == '.')
*cp = '\0';
else
break;
}
ep = cp+1;
do
{
for (dp = ep; dp > copy; --dp)
if (*dp == '.')
break;
ep = dp;
if (*dp == '.')
++dp;
strlcat((char *)buf, (char *)dp, buflen);
if (*ep == '.')
{
(void) strlcat((char *)buf, ".", buflen);
*ep = '\0';
--ep;
}
} while (dp > copy && ep > copy);
return 0;
}
/**************************************************************************************
** OPENDMARC_TLD_READ_FILE -- Read in the file of TLDs and prepare to select against it.
** Arguments:
** path_fname -- The path and file name to read and process
** commentstring -- The leading characters that comment out a line
** drop -- Drop these leading characters but bind a dot to this TLD
** except -- Prefix that marks at TLD as a stand-alone TLD with domain.
**
** Returns:
** 0 -- On success
** != 0 -- On error and set's errno
** Side Effect:
** Opens and read a file (read-only)
** Allocates memory to store the result.
** Be certain to call opendmarc_tld_shutdown() to free allocated memory.
**************************************************************************************/
int
opendmarc_tld_read_file(char *path_fname, char *commentstring, char *drop, char *except)
{
FILE * fp;
u_char buf[BUFSIZ];
char * cp;
void * vp;
int nlines;
int ret;
u_char revbuf[MAXDNSHOSTNAME];
int adddot;
int preflen;
OPENDMARC_HASH_CTX *hashp;
if (path_fname == NULL)
{
if (*TLDfile == '\0')
return errno = EINVAL;
path_fname = TLDfile;
}
else
(void) strlcpy(TLDfile, path_fname, sizeof TLDfile);
if (commentstring == NULL)
commentstring = "//";
hashp = opendmarc_hash_init(4096 * 2);
if (hashp == NULL)
return (errno == 0) ? ENOMEM : errno;
fp = fopen(path_fname, "r");
if (fp == NULL)
return errno;
errno = 0;
while (fgets((char *)buf, sizeof buf, fp) != NULL)
{
cp = strchr((char *)buf, '\n');
if (cp != NULL)
*cp = '\0';
cp = strchr((char *)buf, '\r');
if (cp != NULL)
*cp = '\0';
if (strncmp(commentstring, (char *)buf, strlen(commentstring)) == 0 || *buf == '\0')
{
if ((cp = strstr((char *)buf, "xn-")) != NULL)
{
char *ep;
for (ep = cp; *ep != '\0'; ++ep)
{
if (isspace((int)*ep))
break;
}
*ep = '\0';
ret = opendmarc_reverse_domain((u_char *)cp, revbuf, sizeof revbuf);
adddot = TRUE;
goto got_xn;
}
continue;
}
adddot = TRUE;
preflen = 0;
if (drop != NULL && strncasecmp(drop, (char *)buf, strlen(drop)) == 0)
{
preflen = strlen(drop);
adddot = TRUE;
}
if (except != NULL && strncasecmp(except, (char *)buf, strlen(except)) == 0)
{
preflen = strlen(except);
adddot = FALSE;
}
ret = opendmarc_reverse_domain(buf+preflen, revbuf, sizeof revbuf);
got_xn:
if (ret != 0)
continue;
if (adddot == TRUE)
(void) strlcat((char *)revbuf, ".", sizeof revbuf);
vp = opendmarc_hash_lookup(hashp, revbuf, (void *)revbuf, strlen(revbuf));
nlines++;
}
(void) fclose(fp);
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_lock(&TLD_hctx_mutex);
# endif
if (TLDbak_hctx != NULL)
TLDbak_hctx = opendmarc_hash_shutdown(TLDbak_hctx);
TLDbak_hctx = TLD_hctx;
TLD_hctx = hashp;
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_unlock(&TLD_hctx_mutex);
# endif
return 0;
}
/**************************************************************************************
** OPENDMARC_TLD_SHUTDOWN -- Free the tld hash and return
** Arguments:
** none --- void agrguments
** Returns:
** 0 -- Always
** Side Effect:
** Frees memory
** Locks and sets the hash to NULL
**************************************************************************************/
void
opendmarc_tld_shutdown()
{
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_lock(&TLD_hctx_mutex);
# endif
if (TLDbak_hctx != NULL)
TLDbak_hctx = opendmarc_hash_shutdown(TLDbak_hctx);
if (TLD_hctx != NULL)
TLD_hctx = opendmarc_hash_shutdown(TLD_hctx);
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_unlock(&TLD_hctx_mutex);
# endif
return;
}
int
opendmarc_get_tld(u_char *domain, u_char *tld, size_t tld_len)
{
int ret;
u_char revbuf[MAXDNSHOSTNAME];
u_char *rp;
u_char save;
void * vp;
if (domain == NULL || tld == NULL || tld_len == 0)
return errno = EINVAL;
ret = opendmarc_reverse_domain(domain, revbuf, sizeof revbuf);
if (ret != 0)
return (errno == 0) ? EINVAL : errno;
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_lock(&TLD_hctx_mutex);
# endif
vp = TLD_hctx;
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_unlock(&TLD_hctx_mutex);
# endif
if (vp == NULL)
{
/*
** No tld list was supplied so copy the domain and return.
*/
(void) strlcpy(tld, domain, tld_len);
return 0;
}
for (rp = revbuf + strlen(revbuf) -1; rp > revbuf; --rp)
{
if (rp == revbuf)
{
/* no match found in the hash table. */
(void) strlcpy(tld, domain, tld_len);
break;
}
if (*rp == '.')
{
save = *(rp+1);
*(rp+1) = '\0';
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_lock(&TLD_hctx_mutex);
# endif
vp = opendmarc_hash_lookup(TLD_hctx, revbuf, NULL, 0);
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_unlock(&TLD_hctx_mutex);
# endif
if (vp != NULL)
{
*(rp+1) = save;
(void) opendmarc_reverse_domain(revbuf, tld, tld_len);
return 0;
}
*(rp+1) = save;
*rp = '\0';
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_lock(&TLD_hctx_mutex);
# endif
vp = opendmarc_hash_lookup(TLD_hctx, revbuf, NULL, 0);
# if HAVE_PTHREAD_H || HAVE_PTHREAD
(void) pthread_mutex_unlock(&TLD_hctx_mutex);
# endif
if (vp != NULL)
{
char * cp = strchr(revbuf, '.');
if (cp == NULL)
*rp = '.';
(void) opendmarc_reverse_domain(revbuf, tld, tld_len);
return 0;
}
}
}
return 0;
}