Mercurial > dovecot > core-2.2
annotate src/lib/var-expand.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | 3bae5694a7a5 |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21389
diff
changeset
|
1 /* Copyright (c) 2003-2017 Dovecot authors, see the included COPYING file */ |
1034 | 2 |
3 #include "lib.h" | |
5485 | 4 #include "array.h" |
4162
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
5 #include "md5.h" |
3566 | 6 #include "hash.h" |
4162
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
7 #include "hex-binary.h" |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
8 #include "base64.h" |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
9 #include "hostpid.h" |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
10 #include "hmac.h" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
11 #include "pkcs5.h" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
12 #include "hash-method.h" |
1034 | 13 #include "str.h" |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
14 #include "strescape.h" |
1034 | 15 #include "var-expand.h" |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
16 #include "var-expand-private.h" |
1034 | 17 |
13833
8a63f621bd2e
var_expand(): Added %{uid} and %{gid} variables.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
18 #include <unistd.h> |
7523 | 19 #include <ctype.h> |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
20 |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
21 #define TABLE_LAST(t) \ |
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
22 ((t)->key == '\0' && (t)->long_key == NULL) |
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
23 |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
24 struct var_expand_modifier { |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
25 char key; |
3566 | 26 const char *(*func)(const char *, struct var_expand_context *); |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
27 }; |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
28 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
29 static ARRAY(struct var_expand_extension_func_table) var_expand_extensions; |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
30 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
31 static const char * |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5485
diff
changeset
|
32 m_str_lcase(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
3566 | 33 { |
34 return t_str_lcase(str); | |
35 } | |
36 | |
37 static const char * | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5485
diff
changeset
|
38 m_str_ucase(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
3566 | 39 { |
4160
86fa00c163dc
%U modifier was actually lowercasing the string. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
40 return t_str_ucase(str); |
3566 | 41 } |
42 | |
43 static const char * | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5485
diff
changeset
|
44 m_str_escape(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
3566 | 45 { |
46 return str_escape(str); | |
47 } | |
48 | |
49 static const char * | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5485
diff
changeset
|
50 m_str_hex(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
51 { |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
52 unsigned long long l; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
53 |
19038
f8ab4f979e92
Removed all invocations of strtoll() and friends.
Stephan Bosch <stephan@rename-it.nl>
parents:
19030
diff
changeset
|
54 if (str_to_ullong(str, &l) < 0) |
f8ab4f979e92
Removed all invocations of strtoll() and friends.
Stephan Bosch <stephan@rename-it.nl>
parents:
19030
diff
changeset
|
55 l = 0; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
56 return t_strdup_printf("%llx", l); |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
57 } |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
58 |
3573 | 59 static const char * |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5485
diff
changeset
|
60 m_str_reverse(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
3573 | 61 { |
62 size_t len = strlen(str); | |
63 char *p, *rev; | |
64 | |
65 rev = t_malloc(len + 1); | |
66 rev[len] = '\0'; | |
67 | |
68 for (p = rev + len - 1; *str != '\0'; str++) | |
69 *p-- = *str; | |
70 return rev; | |
71 } | |
72 | |
3566 | 73 static const char *m_str_hash(const char *str, struct var_expand_context *ctx) |
74 { | |
75 unsigned int value = str_hash(str); | |
76 string_t *hash = t_str_new(20); | |
77 | |
78 if (ctx->width != 0) { | |
79 value %= ctx->width; | |
80 ctx->width = 0; | |
81 } | |
3572 | 82 |
3566 | 83 str_printfa(hash, "%x", value); |
4558
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
84 while ((int)str_len(hash) < ctx->offset) |
3566 | 85 str_insert(hash, 0, "0"); |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
86 ctx->offset = 0; |
3566 | 87 |
88 return str_c(hash); | |
89 } | |
90 | |
7523 | 91 static const char * |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
92 m_str_newhash(const char *str, struct var_expand_context *ctx) |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
93 { |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
94 string_t *hash = t_str_new(20); |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
95 unsigned char result[MD5_RESULTLEN]; |
16473
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
96 unsigned int i; |
16475
e5bd0a7c6a1e
var_expand(): Fixed initializing variable to %N
Timo Sirainen <tss@iki.fi>
parents:
16473
diff
changeset
|
97 uint64_t value = 0; |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
98 |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
99 md5_get_digest(str, strlen(str), result); |
16473
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
100 for (i = 0; i < sizeof(value); i++) { |
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
101 value <<= 8; |
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
102 value |= result[i]; |
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
103 } |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
104 |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
105 if (ctx->width != 0) { |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
106 value %= ctx->width; |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
107 ctx->width = 0; |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
108 } |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
109 |
16473
956324a5fc86
var_expand*(): Fixed %N to work the same with little and big endian CPUs.
Timo Sirainen <tss@iki.fi>
parents:
16418
diff
changeset
|
110 str_printfa(hash, "%x", (unsigned int)value); |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
111 while ((int)str_len(hash) < ctx->offset) |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
112 str_insert(hash, 0, "0"); |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
113 ctx->offset = 0; |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
114 |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
115 return str_c(hash); |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
116 } |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
117 |
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
118 static const char * |
7523 | 119 m_str_md5(const char *str, struct var_expand_context *ctx ATTR_UNUSED) |
4162
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
120 { |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
121 unsigned char digest[16]; |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
122 |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
123 md5_get_digest(str, strlen(str), digest); |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
124 |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
125 return binary_to_hex(digest, sizeof(digest)); |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
126 } |
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
127 |
7523 | 128 static const char * |
129 m_str_ldap_dn(const char *str, struct var_expand_context *ctx ATTR_UNUSED) | |
4693
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
130 { |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
131 string_t *ret = t_str_new(256); |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
132 |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
133 while (*str) { |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
134 if (*str == '.') |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
135 str_append(ret, ",dc="); |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
136 else |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
137 str_append_c(ret, *str); |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
138 str++; |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
139 } |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
140 |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
141 return str_free_without_data(&ret); |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
142 } |
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
143 |
7523 | 144 static const char * |
145 m_str_trim(const char *str, struct var_expand_context *ctx ATTR_UNUSED) | |
146 { | |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21062
diff
changeset
|
147 size_t len; |
7523 | 148 |
149 len = strlen(str); | |
150 while (len > 0 && i_isspace(str[len-1])) | |
151 len--; | |
152 return t_strndup(str, len); | |
153 } | |
154 | |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
155 #define MAX_MODIFIER_COUNT 10 |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
156 static const struct var_expand_modifier modifiers[] = { |
3566 | 157 { 'L', m_str_lcase }, |
158 { 'U', m_str_ucase }, | |
159 { 'E', m_str_escape }, | |
160 { 'X', m_str_hex }, | |
3573 | 161 { 'R', m_str_reverse }, |
3566 | 162 { 'H', m_str_hash }, |
16418
4d711ec26847
var_expand(): Added %N, which is the same as %H except based on MD5.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
163 { 'N', m_str_newhash }, |
4162
baa2f72e4010
%M modifier returns string's MD5 sum. Patch by Ben Winslow
Timo Sirainen <tss@iki.fi>
parents:
4160
diff
changeset
|
164 { 'M', m_str_md5 }, |
4693
c28892e80d6c
Added %D modified to return "sub.domain.org" as "sub,dc=domain,dc=org" (for
Timo Sirainen <tss@iki.fi>
parents:
4558
diff
changeset
|
165 { 'D', m_str_ldap_dn }, |
7523 | 166 { 'T', m_str_trim }, |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
167 { '\0', NULL } |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
168 }; |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
169 |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
170 static const char * |
21061
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
171 var_expand_short(const struct var_expand_table *table, char key) |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
172 { |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
173 const struct var_expand_table *t; |
21061
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
174 |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
175 if (table != NULL) { |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
176 for (t = table; !TABLE_LAST(t); t++) { |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
177 if (t->key == key) |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
178 return t->value != NULL ? t->value : ""; |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
179 } |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
180 } |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
181 |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
182 /* not found */ |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
183 if (key == '%') |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
184 return "%"; |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
185 return NULL; |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
186 } |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
187 |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
188 static int |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
189 var_expand_hash(struct var_expand_context *ctx, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
190 const char *key, const char *field, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
191 const char **result_r, const char **error_r) |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
192 { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
193 enum { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
194 FORMAT_HEX, |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
195 FORMAT_HEX_UC, |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
196 FORMAT_BASE64 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
197 } format = FORMAT_HEX; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
198 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
199 const char *p = strchr(key, ';'); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
200 const char *const *args = NULL; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
201 const char *algo = key; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
202 const char *value; |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
203 int ret; |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
204 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
205 if (p != NULL) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
206 algo = t_strcut(key, ';'); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
207 args = t_strsplit(p+1, ","); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
208 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
209 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
210 const struct hash_method *method; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
211 if (strcmp(algo, "pkcs5") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
212 method = hash_method_lookup("sha256"); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
213 } else if ((method = hash_method_lookup(algo)) == NULL) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
214 return 0; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
215 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
216 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
217 string_t *field_value = t_str_new(64); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
218 string_t *salt = t_str_new(64); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
219 string_t *tmp = t_str_new(method->digest_size); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
220 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
221 if ((ret = var_expand_long(ctx, field, strlen(field), |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
222 &value, error_r)) < 1) { |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
223 return ret; |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
224 } |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
225 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
226 str_append(field_value, value); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
227 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
228 /* default values */ |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
229 unsigned int rounds = 1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
230 unsigned int truncbits = 0; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
231 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
232 if (strcmp(algo, "pkcs5") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
233 rounds = 2048; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
234 str_append(salt, field); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
235 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
236 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
237 while(args != NULL && *args != NULL) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
238 const char *k = t_strcut(*args, '='); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
239 const char *value = strchr(*args, '='); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
240 if (value == NULL) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
241 args++; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
242 continue; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
243 } else { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
244 value++; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
245 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
246 if (strcmp(k, "rounds") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
247 if (str_to_uint(value, &rounds)<0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
248 *error_r = t_strdup_printf( |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
249 "Cannot parse hash arguments:" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
250 "'%s' is not number for rounds", |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
251 value); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
252 return -1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
253 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
254 if (rounds < 1) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
255 *error_r = t_strdup_printf( |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
256 "Cannot parse hash arguments:" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
257 "rounds must be at least 1"); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
258 return -1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
259 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
260 } else if (strcmp(k, "truncate") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
261 if (str_to_uint(value, &truncbits)<0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
262 *error_r = t_strdup_printf( |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
263 "Cannot parse hash arguments:" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
264 "'%s' is not number for truncbits", |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
265 value); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
266 return -1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
267 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
268 truncbits = I_MIN(truncbits, method->digest_size*8); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
269 } else if (strcmp(k, "salt") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
270 str_truncate(salt, 0); |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
271 var_expand_with_funcs(salt, value, ctx->table, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
272 ctx->func_table, ctx->context); |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
273 break; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
274 } else if (strcmp(k, "format") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
275 if (strcmp(value, "hex") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
276 format = FORMAT_HEX; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
277 } else if (strcmp(value, "hexuc") == 0){ |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
278 format = FORMAT_HEX_UC; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
279 } else if (strcmp(value, "base64") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
280 format = FORMAT_BASE64; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
281 } else { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
282 *error_r = t_strdup_printf( |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
283 "Cannot parse hash arguments:" |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
284 "'%s' is not supported format", |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
285 value); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
286 return -1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
287 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
288 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
289 args++; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
290 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
291 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
292 str_truncate(tmp, 0); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
293 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
294 if (strcmp(algo, "pkcs5") == 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
295 if (pkcs5_pbkdf(PKCS5_PBKDF2, method, |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
296 field_value->data, field_value->used, |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
297 salt->data, salt->used, |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
298 rounds, HMAC_MAX_CONTEXT_SIZE, tmp) != 0) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
299 *error_r = "Cannot hash: PKCS5_PBKDF2 failed"; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
300 return -1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
301 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
302 } else { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
303 void *context = t_malloc(method->context_size); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
304 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
305 str_append_str(tmp, field_value); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
306 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
307 for(;rounds>0;rounds--) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
308 method->init(context); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
309 if (salt->used > 0) |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
310 method->loop(context, salt->data, salt->used); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
311 method->loop(context, tmp->data, tmp->used); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
312 unsigned char *digest = |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
313 buffer_get_modifiable_data(tmp, NULL); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
314 method->result(context, digest); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
315 if (tmp->used != method->digest_size) |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
316 buffer_set_used_size(tmp, method->digest_size); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
317 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
318 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
319 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
320 if (truncbits > 0) |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
321 buffer_truncate_rshift_bits(tmp, truncbits); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
322 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
323 switch(format) { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
324 case FORMAT_HEX: |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
325 *result_r = binary_to_hex(tmp->data, tmp->used); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
326 return 1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
327 case FORMAT_HEX_UC: |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
328 *result_r = binary_to_hex(tmp->data, tmp->used); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
329 return 1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
330 case FORMAT_BASE64: { |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
331 string_t *dest = t_str_new(64); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
332 base64_encode(tmp->data, tmp->used, dest); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
333 *result_r = str_c(dest); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
334 return 1; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
335 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
336 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
337 |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
338 i_unreached(); |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
339 } |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
340 |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
341 static int |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
342 var_expand_func(const struct var_expand_func_table *func_table, |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
343 const char *key, const char *data, void *context, |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
344 const char **var_r, const char **error_r) |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
345 { |
19030
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
346 const char *value; |
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
347 |
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
348 if (strcmp(key, "env") == 0) { |
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
349 value = getenv(data); |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
350 *var_r = value != NULL ? value : ""; |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
351 return 1; |
19030
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
352 } |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
353 if (func_table != NULL) { |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
354 for (; func_table->key != NULL; func_table++) { |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
355 if (strcmp(func_table->key, key) == 0) { |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
356 value = func_table->func(data, context); |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
357 *var_r = value != NULL ? value : ""; |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
358 return 1; |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
359 } |
19030
061c21b5c5e4
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as ""
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
360 } |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
361 } |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
362 *error_r = t_strdup_printf("Unknown variable '%%%s'", key); |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
363 *var_r = t_strdup_printf("UNSUPPORTED_VARIABLE_%s", key); |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
364 return 0; |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
365 } |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
366 |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
367 static int |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
368 var_expand_try_extension(struct var_expand_context *ctx, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
369 const char *key, const char *data, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
370 const char **var_r, const char **error_r) |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
371 { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
372 int ret; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
373 const char *sep = strchr(key, ';'); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
374 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
375 if (sep == NULL) sep = key + strlen(key); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
376 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
377 /* try with extensions */ |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
378 const struct var_expand_extension_func_table *f; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
379 array_foreach(&var_expand_extensions, f) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
380 /* ensure we won't match abbreviations */ |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
381 size_t len = sep-key; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
382 if (strncasecmp(key, f->key, len) == 0 && f->key[len] == '\0') |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
383 return f->func(ctx, key, data, var_r, error_r); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
384 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
385 if ((ret = var_expand_func(ctx->func_table, key, data, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
386 ctx->context, var_r, error_r)) == 0) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
387 *error_r = t_strdup_printf("Unknown variable '%%%s'", key); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
388 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
389 return ret; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
390 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
391 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
392 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
393 int |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
394 var_expand_long(struct var_expand_context *ctx, |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
395 const void *key_start, size_t key_len, |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
396 const char **var_r, const char **error_r) |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
397 { |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
398 const struct var_expand_table *t; |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
399 const char *key, *value = NULL; |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
400 int ret = 1; |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
401 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
402 if (ctx->table != NULL) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
403 for (t = ctx->table; !TABLE_LAST(t); t++) { |
14055
56c3a6c1d187
var_expand*(): Allow table to be NULL (when using only func_table).
Timo Sirainen <tss@iki.fi>
parents:
14053
diff
changeset
|
404 if (t->long_key != NULL && |
56c3a6c1d187
var_expand*(): Allow table to be NULL (when using only func_table).
Timo Sirainen <tss@iki.fi>
parents:
14053
diff
changeset
|
405 strncmp(t->long_key, key_start, key_len) == 0 && |
56c3a6c1d187
var_expand*(): Allow table to be NULL (when using only func_table).
Timo Sirainen <tss@iki.fi>
parents:
14053
diff
changeset
|
406 t->long_key[key_len] == '\0') { |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
407 *var_r = t->value != NULL ? t->value : ""; |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
408 return 1; |
14055
56c3a6c1d187
var_expand*(): Allow table to be NULL (when using only func_table).
Timo Sirainen <tss@iki.fi>
parents:
14053
diff
changeset
|
409 } |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
410 } |
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
411 } |
15509
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
412 key = t_strndup(key_start, key_len); |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
413 |
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
414 /* built-in variables: */ |
15509
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
415 switch (key_len) { |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
416 case 3: |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
417 if (strcmp(key, "pid") == 0) |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
418 value = my_pid; |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
419 else if (strcmp(key, "uid") == 0) |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
420 value = dec2str(geteuid()); |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
421 else if (strcmp(key, "gid") == 0) |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
422 value = dec2str(getegid()); |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
423 break; |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
424 case 8: |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
425 if (strcmp(key, "hostname") == 0) |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
426 value = my_hostname; |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
427 break; |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
428 } |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
429 |
15509
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
430 if (value == NULL) { |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
431 const char *data = strchr(key, ':'); |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
432 |
15509
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
433 if (data != NULL) |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
434 key = t_strdup_until(key, data++); |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
435 else |
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
436 data = ""; |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
437 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
438 ret = var_expand_try_extension(ctx, key, data, &value, error_r); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
439 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
440 if (ret <= 0 && value == NULL) { |
21062
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
441 value = ""; |
f8318f1b0749
lib: Add generic hashing support to var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21061
diff
changeset
|
442 } |
15509
3a33e686fc38
var_expand*(): Don't use a data stack frame when expanding long %{variables}
Timo Sirainen <tss@iki.fi>
parents:
14339
diff
changeset
|
443 } |
20053
2a83fcc09a68
lib: var_expand() now expands %{nonexistent} to UNSUPPORTED_VARIABLE_nonexistent
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
444 if (value == NULL) |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
445 value = t_strdup_printf("UNSUPPORTED_VARIABLE_%s", key); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
446 *var_r = value; |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
447 return ret; |
10526
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
448 } |
e1b725d02c30
var_expand(): Added support for built-in host, pid and env:* variables.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
449 |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
450 void var_expand_with_funcs(string_t *dest, const char *str, |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
451 const struct var_expand_table *table, |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
452 const struct var_expand_func_table *func_table, |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
453 void *context) |
1034 | 454 { |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
455 const struct var_expand_modifier *m; |
1034 | 456 const char *var; |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
457 struct var_expand_context ctx; |
3566 | 458 const char *(*modifier[MAX_MODIFIER_COUNT]) |
459 (const char *, struct var_expand_context *); | |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
460 const char *end; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21062
diff
changeset
|
461 unsigned int i, modifier_count; |
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21062
diff
changeset
|
462 size_t len; |
1034 | 463 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21322
diff
changeset
|
464 i_zero(&ctx); |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
465 ctx.table = table; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
466 ctx.func_table = func_table; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
467 ctx.context = context; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
468 |
1034 | 469 for (; *str != '\0'; str++) { |
470 if (*str != '%') | |
471 str_append_c(dest, *str); | |
472 else { | |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
473 int sign = 1; |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
474 |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
475 str++; |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
476 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
477 /* reset per-field modifiers */ |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
478 ctx.offset = 0; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
479 ctx.width = 0; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
480 ctx.zero_padding = FALSE; |
2368
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
481 |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
482 /* [<offset>.]<width>[<modifiers>]<variable> */ |
4558
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
483 if (*str == '-') { |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
484 sign = -1; |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
485 str++; |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
486 } |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
487 if (*str == '0') { |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
488 ctx.zero_padding = TRUE; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
489 str++; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
490 } |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
491 while (*str >= '0' && *str <= '9') { |
3566 | 492 ctx.width = ctx.width*10 + (*str - '0'); |
1034 | 493 str++; |
494 } | |
495 | |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
496 if (*str == '.') { |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
497 ctx.offset = sign * ctx.width; |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
498 sign = 1; |
3566 | 499 ctx.width = 0; |
2368
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
500 str++; |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
501 |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
502 /* if offset was prefixed with zero (or it was |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
503 plain zero), just ignore that. zero padding |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
504 is done with the width. */ |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
505 ctx.zero_padding = FALSE; |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
506 if (*str == '0') { |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
507 ctx.zero_padding = TRUE; |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
508 str++; |
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
509 } |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
510 if (*str == '-') { |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
511 sign = -1; |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
512 str++; |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
513 } |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
514 |
2368
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
515 while (*str >= '0' && *str <= '9') { |
3566 | 516 ctx.width = ctx.width*10 + (*str - '0'); |
2368
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
517 str++; |
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
518 } |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
519 ctx.width = sign * ctx.width; |
2368
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
520 } |
1ce02ffc7ec7
Added support for %offset.width format in variables.
Timo Sirainen <tss@iki.fi>
parents:
2057
diff
changeset
|
521 |
21807
949ab17b9294
lib: Fix whitespace in var-expand.c
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
522 modifier_count = 0; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
523 while (modifier_count < MAX_MODIFIER_COUNT) { |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
524 modifier[modifier_count] = NULL; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
525 for (m = modifiers; m->key != '\0'; m++) { |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
526 if (m->key == *str) { |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
527 /* @UNSAFE */ |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
528 modifier[modifier_count] = |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
529 m->func; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
530 str++; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
531 break; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
532 } |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
533 } |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
534 if (modifier[modifier_count] == NULL) |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
535 break; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
536 modifier_count++; |
1034 | 537 } |
538 | |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
539 if (*str == '\0') |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
540 break; |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
541 |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
542 var = NULL; |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
543 if (*str == '{' && (end = strchr(str, '}')) != NULL) { |
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
544 /* %{long_key} */ |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
545 const char *error; |
21058
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
546 unsigned int ctr = 1; |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
547 end = str; |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
548 while(*++end != '\0' && ctr > 0) { |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
549 if (*end == '{') ctr++; |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
550 if (*end == '}') ctr--; |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
551 } |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
552 if (ctr == 0) |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
553 /* it needs to come back a bit */ |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
554 end--; |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
555 /* if there is no } it will consume rest of the |
16a74bf21d94
lib: Do not consume {} inside %{}
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20551
diff
changeset
|
556 string */ |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
557 len = end - (str + 1); |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
558 if (var_expand_long(&ctx, str+1, len, &var, &error) < 0) |
21858
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
559 i_error("var_expand_long(%s) failed: %s", |
c2ba1f66c449
lib: Use error_r in var_expand_long and friends
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21807
diff
changeset
|
560 str+1, error); |
20053
2a83fcc09a68
lib: var_expand() now expands %{nonexistent} to UNSUPPORTED_VARIABLE_nonexistent
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
561 i_assert(var != NULL); |
2a83fcc09a68
lib: var_expand() now expands %{nonexistent} to UNSUPPORTED_VARIABLE_nonexistent
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
562 str = end; |
21061
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
563 } else { |
3494fd39f647
lib: var_expand() code cleanup - Moved short %v expansion to its own function.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21058
diff
changeset
|
564 var = var_expand_short(table, *str); |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1034
diff
changeset
|
565 } |
1034 | 566 |
567 if (var != NULL) { | |
3566 | 568 for (i = 0; i < modifier_count; i++) |
569 var = modifier[i](var, &ctx); | |
4558
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
570 |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
571 if (ctx.offset < 0) { |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
572 /* if offset is < 0 then we want to |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
573 start at the end */ |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
574 size_t len = strlen(var); |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
575 |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
576 if (len > (size_t)-ctx.offset) |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
577 var += len + ctx.offset; |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
578 } else { |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
579 while (*var != '\0' && ctx.offset > 0) { |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
580 ctx.offset--; |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
581 var++; |
fc58084df7b5
Negative offsets count from the end of the string. Patch by Johannes Berg.
Timo Sirainen <tss@iki.fi>
parents:
4162
diff
changeset
|
582 } |
3566 | 583 } |
584 if (ctx.width == 0) | |
1034 | 585 str_append(dest, var); |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
586 else if (!ctx.zero_padding) { |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
587 if (ctx.width < 0) |
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
588 ctx.width = strlen(var) - (-ctx.width); |
3566 | 589 str_append_n(dest, var, ctx.width); |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
590 } else { |
4788
385b77e37f1f
Fixed zero padding handling and documented it. %0.1n shouldn't enable it,
Timo Sirainen <tss@iki.fi>
parents:
4693
diff
changeset
|
591 /* %05d -like padding. no truncation. */ |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21062
diff
changeset
|
592 ssize_t len = strlen(var); |
3566 | 593 while (len < ctx.width) { |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
594 str_append_c(dest, '0'); |
3566 | 595 ctx.width--; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
596 } |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
597 str_append(dest, var); |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
598 } |
1034 | 599 } |
600 } | |
601 } | |
602 } | |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
603 |
14053
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
604 void var_expand(string_t *dest, const char *str, |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
605 const struct var_expand_table *table) |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
606 { |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
607 var_expand_with_funcs(dest, str, table, NULL, NULL); |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
608 } |
ba86a60e3059
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
Timo Sirainen <tss@iki.fi>
parents:
13833
diff
changeset
|
609 |
20550
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
610 static bool |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
611 var_get_key_range_full(const char *str, unsigned int *idx_r, |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
612 unsigned int *size_r) |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
613 { |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
614 const struct var_expand_modifier *m; |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
615 unsigned int i = 0; |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
616 |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
617 /* [<offset>.]<width>[<modifiers>]<variable> */ |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
618 while ((str[i] >= '0' && str[i] <= '9') || str[i] == '-') |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
619 i++; |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
620 |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
621 if (str[i] == '.') { |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
622 i++; |
17159
97d31cbbfe01
liblib: Added negative %variable lengths to count backwards from the end of the value.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
623 while ((str[i] >= '0' && str[i] <= '9') || str[i] == '-') |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
624 i++; |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
625 } |
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
626 |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
627 do { |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
628 for (m = modifiers; m->key != '\0'; m++) { |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
629 if (m->key == str[i]) { |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
630 i++; |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
631 break; |
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
632 } |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
633 } |
2977
4a6788997812
Added support for multiple modifiers. Added %X = hex modifier. Prefixing
Timo Sirainen <tss@iki.fi>
parents:
2975
diff
changeset
|
634 } while (m->key != '\0'); |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
635 |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
636 if (str[i] != '{') { |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
637 /* short key */ |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
638 *idx_r = i; |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
639 *size_r = str[i] == '\0' ? 0 : 1; |
20550
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
640 return FALSE; |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
641 } else { |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
642 /* long key */ |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
643 *idx_r = ++i; |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
644 for (; str[i] != '\0'; i++) { |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
645 if (str[i] == '}') |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
646 break; |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
647 } |
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
648 *size_r = i - *idx_r; |
20550
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
649 return TRUE; |
14339
13d4c4f91622
liblib: Added var_get_key_range()
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
650 } |
2975
c59fd95ad3ee
Auth cache key was built wrong if any modifiers were used for %vars.
Timo Sirainen <tss@iki.fi>
parents:
2368
diff
changeset
|
651 } |
5485 | 652 |
20550
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
653 char var_get_key(const char *str) |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
654 { |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
655 unsigned int idx, size; |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
656 |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
657 if (var_get_key_range_full(str, &idx, &size)) |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
658 return '{'; |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
659 return str[idx]; |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
660 } |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
661 |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
662 void var_get_key_range(const char *str, unsigned int *idx_r, |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
663 unsigned int *size_r) |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
664 { |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
665 (void)var_get_key_range_full(str, idx_r, size_r); |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
666 } |
ba37ec73c6aa
lib: var_get_key() didn't handle %{long_variables} correctly
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20053
diff
changeset
|
667 |
11073
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
668 static bool var_has_long_key(const char **str, const char *long_key) |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
669 { |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
670 const char *start, *end; |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
671 |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
672 start = strchr(*str, '{'); |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
673 i_assert(start != NULL); |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
674 |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
675 end = strchr(++start, '}'); |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
676 if (end == NULL) |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
677 return FALSE; |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
678 |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
679 if (strncmp(start, long_key, end-start) == 0 && |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
680 long_key[end-start] == '\0') |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
681 return TRUE; |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
682 |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
683 *str = end; |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
684 return FALSE; |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
685 } |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
686 |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
687 bool var_has_key(const char *str, char key, const char *long_key) |
8324 | 688 { |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
689 char c; |
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
690 |
8324 | 691 for (; *str != '\0'; str++) { |
692 if (*str == '%' && str[1] != '\0') { | |
693 str++; | |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
694 c = var_get_key(str); |
20551
64f547f5bd86
lib: var_has_key() properly ignores key=='\0' now.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20550
diff
changeset
|
695 if (c == key && key != '\0') |
8324 | 696 return TRUE; |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
697 |
11073
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
698 if (c == '{' && long_key != NULL) { |
462376c65e73
var_has_key(): Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
699 if (var_has_long_key(&str, long_key)) |
8544
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
700 return TRUE; |
983d38de06c9
var_expand(): Added support for long %{variable} names.
Timo Sirainen <tss@iki.fi>
parents:
8324
diff
changeset
|
701 } |
8324 | 702 } |
703 } | |
704 return FALSE; | |
705 } | |
706 | |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
707 void var_expand_extensions_deinit(void) |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
708 { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
709 array_free(&var_expand_extensions); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
710 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
711 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
712 void var_expand_extensions_init(void) |
5485 | 713 { |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
714 i_array_init(&var_expand_extensions, 32); |
5485 | 715 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
716 /* put all hash methods there */ |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
717 for(const struct hash_method **meth = hash_methods; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
718 *meth != NULL; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
719 meth++) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
720 struct var_expand_extension_func_table *func = |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
721 array_append_space(&var_expand_extensions); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
722 func->key = (*meth)->name; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
723 func->func = var_expand_hash; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
724 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
725 |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
726 /* pkcs5 */ |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
727 struct var_expand_extension_func_table *func = |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
728 array_append_space(&var_expand_extensions); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
729 func->key = "pkcs5"; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
730 func->func = var_expand_hash; |
22539
3bae5694a7a5
var-expand: Add support for conditionals
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21873
diff
changeset
|
731 |
3bae5694a7a5
var-expand: Add support for conditionals
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21873
diff
changeset
|
732 /* if */ |
3bae5694a7a5
var-expand: Add support for conditionals
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21873
diff
changeset
|
733 func = array_append_space(&var_expand_extensions); |
3bae5694a7a5
var-expand: Add support for conditionals
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21873
diff
changeset
|
734 func->key = "if"; |
3bae5694a7a5
var-expand: Add support for conditionals
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21873
diff
changeset
|
735 func->func = var_expand_if; |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
736 } |
5485 | 737 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
738 void |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
739 var_expand_register_func_array(const struct var_expand_extension_func_table *funcs) |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
740 { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
741 for(const struct var_expand_extension_func_table *ptr = funcs; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
742 ptr->key != NULL; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
743 ptr++) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
744 i_assert(*ptr->key != '\0'); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
745 array_insert(&var_expand_extensions, 0, ptr, 1); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
746 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
747 } |
5485 | 748 |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
749 void |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
750 var_expand_unregister_func_array(const struct var_expand_extension_func_table *funcs) |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
751 { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
752 for(const struct var_expand_extension_func_table *ptr = funcs; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
753 ptr->key != NULL; |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
754 ptr++) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
755 i_assert(ptr->func != NULL); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
756 for(unsigned int i = 0; i < array_count(&var_expand_extensions); i++) { |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
757 const struct var_expand_extension_func_table *func = |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
758 array_idx(&var_expand_extensions, i); |
21873
ed955a3b0766
lib: Fix strcasecmp mistake in var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21859
diff
changeset
|
759 if (strcasecmp(func->key, ptr->key) == 0) { |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
760 array_delete(&var_expand_extensions, i, 1); |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
761 } |
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21858
diff
changeset
|
762 } |
5485 | 763 } |
764 } |