Skip to content

Commit b3790a2

Browse files
committed
Merge pull request trusteddomainproject#207 form futatuki/libopendkim-expose-nametables
2 parents 180bb8a + e421a4d commit b3790a2

23 files changed

+1047
-139
lines changed

RELEASE_NOTES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ release, and a summary of the changes in that release.
4949
oversized input lines. Reported by Mars Peng.
5050
LIBOPENDKIM: Fix parsing bug in dkim_mail_parse_multi(), where quotes
5151
were not being properly handled.
52+
LIBOPENDKIM: Expose conversion table between internal code already
53+
provided as DKIM_ macros and their literal name in C string.
5254
TOOLS: Feature requrest #187: Add option to match subdomains when
5355
generating zone files. Patch from Andreas Schulze.
5456

libopendkim/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ endif
88
LDADD = ./libopendkim.la
99

1010
lib_LTLIBRARIES = libopendkim.la
11-
libopendkim_la_SOURCES = base32.c base64.c dkim-atps.c dkim-cache.c dkim-canon.c dkim-dns.c dkim-keys.c dkim-mailparse.c dkim-report.c dkim-tables.c dkim-test.c dkim-util.c dkim.c util.c base64.h dkim-cache.h dkim-canon.h dkim-dns.h dkim-internal.h dkim-keys.h dkim-mailparse.h dkim-report.h dkim-tables.h dkim-test.h dkim-types.h dkim-util.h dkim.h util.h
11+
libopendkim_la_SOURCES = base32.c base64.c dkim-atps.c dkim-cache.c dkim-canon.c dkim-dns.c dkim-keys.c dkim-mailparse.c dkim-report.c dkim-tables.c dkim-test.c dkim-util.c dkim.c util.c base64.h dkim-cache.h dkim-canon.h dkim-dns.h dkim-internal.h dkim-keys.h dkim-mailparse.h dkim-report.h dkim-test.h dkim-types.h dkim-util.h dkim.h util.h
1212
libopendkim_la_CPPFLAGS = $(LIBCRYPTO_CPPFLAGS)
1313
libopendkim_la_CFLAGS = $(LIBCRYPTO_INCDIRS) $(LIBOPENDKIM_INC) $(COV_CFLAGS)
1414
libopendkim_la_LDFLAGS = -no-undefined $(LIBCRYPTO_LIBDIRS) $(COV_LDFLAGS) -version-info $(LIBOPENDKIM_VERSION_INFO)

libopendkim/dkim-atps.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "dkim.h"
2222
#include "dkim-internal.h"
2323
#include "dkim-types.h"
24-
#include "dkim-tables.h"
2524
#include "util.h"
2625

2726
#ifdef USE_GNUTLS
@@ -142,7 +141,7 @@ dkim_atps_check(DKIM *dkim, DKIM_SIGINFO *sig, struct timeval *timeout,
142141
/* confirm it requested a hash we know how to do */
143142
if (strcasecmp(ahash, "none") != 0)
144143
{
145-
hash = dkim_name_to_code(hashes, ahash);
144+
hash = dkim_name_to_code(dkim_table_hashes, ahash);
146145
if (hash == -1)
147146
return DKIM_STAT_INVALID;
148147
}

libopendkim/dkim-internal.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ typedef int dkim_key_t;
9797
#define DKIM_KEY_SERVICE 5 /* s */
9898
#define DKIM_KEY_FLAGS 6 /* t */
9999

100+
extern DKIM_NAMETABLE *dkim_table_keyparams;
101+
100102
/*
101103
** DKIM_SETTYPE -- types of sets
102104
*/
@@ -108,6 +110,8 @@ typedef int dkim_set_t;
108110
#define DKIM_SETTYPE_KEY 1
109111
#define DKIM_SETTYPE_SIGREPORT 2
110112

113+
extern DKIM_NAMETABLE *dkim_table_settypes;
114+
111115
/*
112116
** DKIM_HASHTYPE -- types of hashes
113117
*/
@@ -116,6 +120,8 @@ typedef int dkim_set_t;
116120
#define DKIM_HASHTYPE_SHA1 0
117121
#define DKIM_HASHTYPE_SHA256 1
118122

123+
extern DKIM_NAMETABLE *dkim_table_hashes;
124+
119125
/*
120126
** DKIM_KEYTYPE -- types of keys
121127
*/
@@ -124,6 +130,8 @@ typedef int dkim_set_t;
124130
#define DKIM_KEYTYPE_RSA 0
125131
#define DKIM_KEYTYPE_ED25519 1
126132

133+
extern DKIM_NAMETABLE *dkim_table_keytypes;
134+
127135
/*
128136
** DKIM_SET -- a set of parameters and values
129137
*/
@@ -152,6 +160,15 @@ typedef struct dkim_key DKIM_KEY;
152160
struct dkim_canon;
153161
typedef struct dkim_canon DKIM_CANON;
154162

163+
164+
#ifdef _FFR_CONDITIONAL
165+
166+
/*
167+
** mandatory DKIM tags
168+
*/
169+
extern DKIM_NAMETABLE *dkim_table_mandatory;
170+
#endif /* _FFR_CONDITIONAL */
171+
155172
/* prototypes */
156173
extern DKIM_STAT dkim_process_set __P((DKIM *, dkim_set_t, u_char *, size_t,
157174
void *, _Bool, const char *));

libopendkim/dkim-report.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "dkim-report.h"
2323
#include "dkim-internal.h"
2424
#include "dkim-types.h"
25-
#include "dkim-tables.h"
2625
#include "util.h"
2726

2827
/* prototypes */

libopendkim/dkim-tables.c

Lines changed: 131 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@
1010

1111
/* system includes */
1212
#include <sys/types.h>
13+
#include <stdlib.h>
1314
#include <string.h>
1415
#include <assert.h>
1516

1617
/* libopendkim includes */
17-
#include "dkim-tables.h"
1818
#include "dkim-internal.h"
1919

20+
/* structures */
21+
struct dkim_nametable
22+
{
23+
const char * tbl_name; /* name */
24+
const int tbl_code; /* code */
25+
};
26+
27+
struct dkim_iter_ctx
28+
{
29+
DKIM_NAMETABLE *current; /* current table entry */
30+
_Bool is_eot; /* It is last entry or not */
31+
};
32+
2033
/* lookup tables */
21-
static struct nametable prv_keyparams[] = /* key parameters */
34+
static struct dkim_nametable prv_keyparams[] = /* key parameters */
2235
{
2336
{ "a", DKIM_KEY_ALGORITHM },
2437
{ "n", DKIM_KEY_NOTES },
@@ -28,17 +41,17 @@ static struct nametable prv_keyparams[] = /* key parameters */
2841
{ "v", DKIM_KEY_VERSION },
2942
{ NULL, -1 }
3043
};
31-
struct nametable *keyparams = prv_keyparams;
44+
DKIM_NAMETABLE *dkim_table_keyparams = prv_keyparams;
3245

33-
static struct nametable prv_keyflags[] = /* key flags */
46+
static struct dkim_nametable prv_keyflags[] = /* key flags */
3447
{
3548
{ "y", DKIM_SIGFLAG_TESTKEY },
3649
{ "s", DKIM_SIGFLAG_NOSUBDOMAIN },
3750
{ NULL, -1 }
3851
};
39-
struct nametable *keyflags = prv_keyflags;
52+
DKIM_NAMETABLE *dkim_table_keyflags = prv_keyflags;
4053

41-
static struct nametable prv_sigparams[] = /* signature parameters */
54+
static struct dkim_nametable prv_sigparams[] = /* signature parameters */
4255
{
4356
{ "a", DKIM_PARAM_SIGNALG },
4457
{ "b", DKIM_PARAM_SIGNATURE },
@@ -56,49 +69,49 @@ static struct nametable prv_sigparams[] = /* signature parameters */
5669
{ "z", DKIM_PARAM_COPIEDHDRS },
5770
{ NULL, -1 }
5871
};
59-
struct nametable *sigparams = prv_sigparams;
72+
DKIM_NAMETABLE *dkim_table_sigparams = prv_sigparams;
6073

61-
static struct nametable prv_algorithms[] = /* signing algorithms */
74+
static struct dkim_nametable prv_algorithms[] = /* signing algorithms */
6275
{
6376
{ "rsa-sha1", DKIM_SIGN_RSASHA1 },
6477
{ "rsa-sha256", DKIM_SIGN_RSASHA256 },
6578
{ "ed25519-sha256", DKIM_SIGN_ED25519SHA256 },
6679
{ NULL, -1 },
6780
};
68-
struct nametable *algorithms = prv_algorithms;
81+
DKIM_NAMETABLE *dkim_table_algorithms = prv_algorithms;
6982

70-
static struct nametable prv_canonicalizations[] = /* canonicalizations */
83+
static struct dkim_nametable prv_canonicalizations[] = /* canonicalizations */
7184
{
7285
{ "simple", DKIM_CANON_SIMPLE },
7386
{ "relaxed", DKIM_CANON_RELAXED },
7487
{ NULL, -1 },
7588
};
76-
struct nametable *canonicalizations = prv_canonicalizations;
89+
DKIM_NAMETABLE *dkim_table_canonicalizations = prv_canonicalizations;
7790

78-
static struct nametable prv_hashes[] = /* hashes */
91+
static struct dkim_nametable prv_hashes[] = /* hashes */
7992
{
8093
{ "sha1", DKIM_HASHTYPE_SHA1 },
8194
{ "sha256", DKIM_HASHTYPE_SHA256 },
8295
{ NULL, -1 },
8396
};
84-
struct nametable *hashes = prv_hashes;
97+
DKIM_NAMETABLE *dkim_table_hashes = prv_hashes;
8598

86-
static struct nametable prv_keytypes[] = /* key types */
99+
static struct dkim_nametable prv_keytypes[] = /* key types */
87100
{
88101
{ "rsa", DKIM_KEYTYPE_RSA },
89102
{ "ed25519", DKIM_KEYTYPE_ED25519 },
90103
{ NULL, -1 },
91104
};
92-
struct nametable *keytypes = prv_keytypes;
105+
DKIM_NAMETABLE *dkim_table_keytypes = prv_keytypes;
93106

94-
static struct nametable prv_querytypes[] = /* query types */
107+
static struct dkim_nametable prv_querytypes[] = /* query types */
95108
{
96109
{ "dns", DKIM_QUERY_DNS },
97110
{ NULL, -1 },
98111
};
99-
struct nametable *querytypes = prv_querytypes;
112+
DKIM_NAMETABLE *dkim_table_querytypes = prv_querytypes;
100113

101-
static struct nametable prv_results[] = /* result codes */
114+
static struct dkim_nametable prv_results[] = /* result codes */
102115
{
103116
{ "Success", DKIM_STAT_OK },
104117
{ "Bad signature", DKIM_STAT_BADSIG },
@@ -116,20 +129,21 @@ static struct nametable prv_results[] = /* result codes */
116129
{ "Invalid result", DKIM_STAT_CBINVALID },
117130
{ "Try again later", DKIM_STAT_CBTRYAGAIN },
118131
{ "Multiple DNS replies", DKIM_STAT_MULTIDNSREPLY },
132+
{ "End of the table", DKIM_STAT_ITER_EOT },
119133
{ NULL, -1 },
120134
};
121-
struct nametable *results = prv_results;
135+
DKIM_NAMETABLE *dkim_table_results = prv_results;
122136

123-
static struct nametable prv_settypes[] = /* set types */
137+
static struct dkim_nametable prv_settypes[] = /* set types */
124138
{
125139
{ "key", DKIM_SETTYPE_KEY },
126140
{ "signature", DKIM_SETTYPE_SIGNATURE },
127141
{ "signature reporting", DKIM_SETTYPE_SIGREPORT },
128142
{ NULL, -1 },
129143
};
130-
struct nametable *settypes = prv_settypes;
144+
DKIM_NAMETABLE *dkim_table_settypes = prv_settypes;
131145

132-
static struct nametable prv_sigerrors[] = /* signature parsing errors */
146+
static struct dkim_nametable prv_sigerrors[] = /* signature parsing errors */
133147
{
134148
{ "no signature error", DKIM_SIGERROR_OK },
135149
{ "unsupported signature version", DKIM_SIGERROR_VERSION },
@@ -182,15 +196,15 @@ static struct nametable prv_sigerrors[] = /* signature parsing errors */
182196
#endif /* _FFR_CONDITIONAL */
183197
{ NULL, -1 },
184198
};
185-
struct nametable *sigerrors = prv_sigerrors;
199+
DKIM_NAMETABLE *dkim_table_sigerrors = prv_sigerrors;
186200

187201
#ifdef _FFR_CONDITIONAL
188-
static struct nametable prv_mandatory[] = /* mandatory DKIM tags */
202+
static struct dkim_nametable prv_mandatory[] = /* mandatory DKIM tags */
189203
{
190204
{ "!cd", 0 },
191205
{ NULL, -1 },
192206
};
193-
struct nametable *mandatory = prv_mandatory;
207+
DKIM_NAMETABLE *dkim_table_mandatory = prv_mandatory;
194208
#endif /* _FFR_CONDITIONAL */
195209

196210
/* ===================================================================== */
@@ -207,7 +221,7 @@ struct nametable *mandatory = prv_mandatory;
207221
*/
208222

209223
const char *
210-
dkim_code_to_name(struct nametable *tbl, const int code)
224+
dkim_code_to_name(DKIM_NAMETABLE *tbl, const int code)
211225
{
212226
int c;
213227

@@ -235,7 +249,7 @@ dkim_code_to_name(struct nametable *tbl, const int code)
235249
*/
236250

237251
const int
238-
dkim_name_to_code(struct nametable *tbl, const char *name)
252+
dkim_name_to_code(DKIM_NAMETABLE *tbl, const char *name)
239253
{
240254
int c;
241255

@@ -250,3 +264,93 @@ dkim_name_to_code(struct nametable *tbl, const char *name)
250264
return tbl[c].tbl_code;
251265
}
252266
}
267+
268+
/*
269+
** DKIM_NAMETABLE_FIRST -- get the first entry of the table and start iteration
270+
**
271+
** Parameters:
272+
** tbl -- name table
273+
** ctx -- iteration context (returned)
274+
** name -- name in the first item in the table (returned)
275+
** code -- code in the first item in the table (returned)
276+
**
277+
** Return value:
278+
** A DKIM_STAT_OK -- retrieve the first item successfully
279+
** A DKIM_STAT_ITER_EOT -- the table has no item.
280+
** A DKIM_STAT_NORESOURCE -- cannot allocate memory for the
281+
** iteration context
282+
**
283+
*/
284+
DKIM_STAT
285+
dkim_nametable_first(DKIM_NAMETABLE *tbl, DKIM_ITER_CTX **ctx,
286+
const char **name, int *code)
287+
{
288+
*ctx = (DKIM_ITER_CTX *)
289+
malloc(sizeof(DKIM_ITER_CTX));
290+
if (*ctx == NULL)
291+
{
292+
return DKIM_STAT_NORESOURCE;
293+
}
294+
if (tbl->tbl_name == NULL)
295+
{
296+
(*ctx)->current = NULL;
297+
(*ctx)->is_eot = TRUE;
298+
return DKIM_STAT_ITER_EOT;
299+
}
300+
*name = tbl->tbl_name;
301+
*code = tbl->tbl_code;
302+
(*ctx)->current = tbl;
303+
(*ctx)->is_eot = (((*ctx)->current)[1].tbl_name == NULL)? TRUE : FALSE;
304+
return DKIM_STAT_OK;
305+
}
306+
307+
/*
308+
** DKIM_NAMETABLE_NEXT -- get the next entry on the iteration the table
309+
**
310+
** Parameters:
311+
** ctx -- iteration context (updated)
312+
** name -- name in the first item in the table (returned)
313+
** code -- code in the first item in the table (returned)
314+
**
315+
** Return value:
316+
** A DKIM_STAT_OK -- retrieve the first item successfully
317+
** A DKIM_STAT_ITER_EOT -- the table has no item.
318+
**
319+
*/
320+
DKIM_STAT
321+
dkim_nametable_next(DKIM_ITER_CTX *ctx, const char **name, int *code)
322+
{
323+
if (ctx->is_eot)
324+
{
325+
return DKIM_STAT_ITER_EOT;
326+
}
327+
ctx->current++;
328+
*name = ctx->current->tbl_name;
329+
*code = ctx->current->tbl_code;
330+
ctx->is_eot = ((ctx->current)[1].tbl_name == NULL)? TRUE : FALSE;
331+
return DKIM_STAT_OK;
332+
}
333+
334+
/*
335+
** DKIM_ITER_CTX_FREE -- release resources associated with
336+
** a nametable iteration context
337+
**
338+
** Parameters:
339+
** ctx -- iteration context
340+
**
341+
** Return value:
342+
** DKIM_STAT_OK -- operation was successful
343+
**
344+
** Note: This function is a placeholder to add some operation associated
345+
** with future changes of the structure of the tables.
346+
**
347+
*/
348+
DKIM_STAT
349+
dkim_iter_ctx_free(DKIM_ITER_CTX *ctx)
350+
{
351+
if (ctx != NULL)
352+
{
353+
free((void *)ctx);
354+
}
355+
return DKIM_STAT_OK;
356+
}

0 commit comments

Comments
 (0)