aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2020-07-09 19:15:20 +0300
committerSergey Poznyakoff <gray@gnu.org>2020-07-09 19:15:20 +0300
commit61e02a9c7b80e537a33f3b4718ab90aa4c2c8934 (patch)
treebbf839051b762bf635274a85d6e416ef59611927 /src
parentba541da248eb75ae85f9dc4535a8b097d7aaaf59 (diff)
downloadmailfromd-61e02a9c7b80e537a33f3b4718ab90aa4c2c8934.tar.gz
mailfromd-61e02a9c7b80e537a33f3b4718ab90aa4c2c8934.tar.bz2
DKIM: Minor change
* src/dkim.c (dkim_hash): Use dkim_header_list_first/ dkim_header_list_first/dkim_header_list_end instead of mu_wordsplit to iterate over the list of headers.
Diffstat (limited to 'src')
-rw-r--r--src/dkim.c54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/dkim.c b/src/dkim.c
index 65df649b..28f17a30 100644
--- a/src/dkim.c
+++ b/src/dkim.c
@@ -301,11 +301,18 @@ pubkey_from_base64(struct rsa_public_key *pub, const char *str)
return result;
}
+/* State of iteratiom over a colon-separated list of headers. */
struct h_list_buf {
- char const *ptr;
- char *base;
- size_t size;
+ char const *ptr; /* Current position in the header list. */
+ char *base; /* Return memory pointer. */
+ size_t size; /* Size of memory allocated for base. */
};
+
+/*
+ * dkim_header_list_next(SAVE)
+ * ---------------------------
+ * Free the state buffer allocated by dkim_header_list_first.
+ */
void
dkim_header_list_end(void *save)
{
@@ -314,7 +321,11 @@ dkim_header_list_end(void *save)
free(hbuf);
}
-char *
+/*
+ * Return next header from the header list, or NULL if the list is exhausted.
+ * SAVE is the iteration state pointer returned by dkim_header_list_first.
+ */
+static char *
dkim_header_list_next(void *save)
{
struct h_list_buf *hbuf = save;
@@ -338,6 +349,13 @@ dkim_header_list_next(void *save)
return hbuf->base;
}
+/*
+ * Start iteration over a list of header names (H_LIST), delimited by
+ * colons with optional whitespace around them. Return first header
+ * name and save the state in the memory location pointed to by SAVE.
+ * No matter the return value, dkim_header_list_end must be called to
+ * reclaim the allocated memory.
+ */
char *
dkim_header_list_first(char const *h_list, void *save)
{
@@ -1044,7 +1062,8 @@ dkim_hash(mu_message_t msg, struct dkim_signature *sig, char const *sigstr,
struct sha256_ctx *ctx)
{
mu_stream_t canon_stream = NULL;
- struct mu_wordsplit ws;
+ char *hp;
+ void *hstate;
struct header_map h_all = HEADER_MAP_INITIALIZER(h_all);
struct header_map h_sel = HEADER_MAP_INITIALIZER(h_sel);
struct header_map *hmap;
@@ -1053,7 +1072,7 @@ dkim_hash(mu_message_t msg, struct dkim_signature *sig, char const *sigstr,
char c;
int rc;
int result = DKIM_HASH_ERR;
- size_t count, i;
+ size_t count;
enum { H_INIT, H_HEADER, H_CR1, H_CR2, H_NL } state = H_INIT;
mu_stream_t sigcanon, str;
char *sig_str_buf;
@@ -1062,16 +1081,6 @@ dkim_hash(mu_message_t msg, struct dkim_signature *sig, char const *sigstr,
if (canonicalize(msg, sig->canon, &canon_stream))
goto err;
- /* Create header map */
- ws.ws_delim = ":";
- if (mu_wordsplit(sig->h, &ws,
- MU_WRDSF_DELIM | WRDSF_WS |
- MU_WRDSF_NOVAR | MU_WRDSF_NOCMD)) {
- mu_error(_("can't parse %s: %s"),
- sig->h, mu_wordsplit_strerror(&ws));
- goto err;
- }
-
/*
* Scan the header part of the canonicalized stream and record
* the headers in the h_all list.
@@ -1151,11 +1160,11 @@ end:
* Select headers to hash according to the h= tag. Selected headers
* are removed from the h_all and added to the h_sel list.
*/
- for (i = 0; i < ws.ws_wordc; i++) {
- if (is_rev_header(ws.ws_wordv[i])) {
+ for (hp = dkim_header_list_first(sig->h, &hstate); hp;
+ hp = dkim_header_list_next(hstate)) {
+ if (is_rev_header(hp)) {
HEADER_MAP_FOREACH_REV(hmap, &h_all) {
- if (mu_c_strcasecmp(hmap->header,
- ws.ws_wordv[i]) == 0) {
+ if (mu_c_strcasecmp(hmap->header, hp) == 0) {
header_map_remove(hmap);
header_map_append(&h_sel, hmap);
break;
@@ -1163,8 +1172,7 @@ end:
}
} else {
HEADER_MAP_FOREACH(hmap, &h_all) {
- if (mu_c_strcasecmp(hmap->header,
- ws.ws_wordv[i]) == 0) {
+ if (mu_c_strcasecmp(hmap->header, hp) == 0) {
header_map_remove(hmap);
header_map_append(&h_sel, hmap);
break;
@@ -1172,6 +1180,7 @@ end:
}
}
}
+ dkim_header_list_end(hstate);
/* Hash the body */
if (dkim_body_hash(canon_stream, bh))
@@ -1246,7 +1255,6 @@ err:
mu_opool_destroy(&op);
header_map_free(&h_all);
header_map_free(&h_sel);
- mu_wordsplit_free(&ws);
mu_stream_destroy(&canon_stream);
return result;
}

Return to:

Send suggestions and report system problems to the System administrator.