1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "md5.h"
#include "hash.h"
#include "hex-binary.h"
#include "base64.h"
#include "hostpid.h"
#include "hmac.h"
#include "pkcs5.h"
#include "hash-method.h"
#include "str.h"
#include "strescape.h"
#include "var-expand.h"
#include "var-expand-private.h"
#include <unistd.h>
#include <ctype.h>
#define TABLE_LAST(t) \
((t)->key == '\0' && (t)->long_key == NULL)
struct var_expand_modifier {
char key;
const char *(*func)(const char *, struct var_expand_context *);
};
static ARRAY(struct var_expand_extension_func_table) var_expand_extensions;
static const char *
m_str_lcase(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
return t_str_lcase(str);
}
static const char *
m_str_ucase(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
return t_str_ucase(str);
}
static const char *
m_str_escape(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
return str_escape(str);
}
static const char *
m_str_hex(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
unsigned long long l;
if (str_to_ullong(str, &l) < 0)
l = 0;
return t_strdup_printf("%llx", l);
}
static const char *
m_str_reverse(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
size_t len = strlen(str);
char *p, *rev;
rev = t_malloc_no0(len + 1);
rev[len] = '\0';
for (p = rev + len - 1; *str != '\0'; str++)
*p-- = *str;
return rev;
}
static const char *m_str_hash(const char *str, struct var_expand_context *ctx)
{
unsigned int value = str_hash(str);
string_t *hash = t_str_new(20);
if (ctx->width != 0) {
value %= ctx->width;
ctx->width = 0;
}
str_printfa(hash, "%x", value);
while ((int)str_len(hash) < ctx->offset)
str_insert(hash, 0, "0");
ctx->offset = 0;
return str_c(hash);
}
static const char *
m_str_newhash(const char *str, struct var_expand_context *ctx)
{
string_t *hash = t_str_new(20);
unsigned char result[MD5_RESULTLEN];
unsigned int i;
uint64_t value = 0;
md5_get_digest(str, strlen(str), result);
for (i = 0; i < sizeof(value); i++) {
value <<= 8;
value |= result[i];
}
if (ctx->width != 0) {
value %= ctx->width;
ctx->width = 0;
}
str_printfa(hash, "%x", (unsigned int)value);
while ((int)str_len(hash) < ctx->offset)
str_insert(hash, 0, "0");
ctx->offset = 0;
return str_c(hash);
}
static const char *
m_str_md5(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
unsigned char digest[16];
md5_get_digest(str, strlen(str), digest);
return binary_to_hex(digest, sizeof(digest));
}
static const char *
m_str_ldap_dn(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
string_t *ret = t_str_new(256);
while (*str != '\0') {
if (*str == '.')
str_append(ret, ",dc=");
else
str_append_c(ret, *str);
str++;
}
return str_free_without_data(&ret);
}
static const char *
m_str_trim(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
{
size_t len;
len = strlen(str);
while (len > 0 && i_isspace(str[len-1]))
len--;
return t_strndup(str, len);
}
#define MAX_MODIFIER_COUNT 10
static const struct var_expand_modifier modifiers[] = {
{ 'L', m_str_lcase },
{ 'U', m_str_ucase },
{ 'E', m_str_escape },
{ 'X', m_str_hex },
{ 'R', m_str_reverse },
{ 'H', m_str_hash },
{ 'N', m_str_newhash },
{ 'M', m_str_md5 },
{ 'D', m_str_ldap_dn },
{ 'T', m_str_trim },
{ '\0', NULL }
};
static int
var_expand_short(const struct var_expand_table *table, char key,
const char **var_r, const char **error_r)
{
const struct var_expand_table *t;
if (table != NULL) {
for (t = table; !TABLE_LAST(t); t++) {
if (t->key == key) {
*var_r = t->value != NULL ? t->value : "";
return 1;
}
}
}
/* not found */
if (key == '%') {
*var_r = "%";
return 1;
}
if (*error_r == NULL)
*error_r = t_strdup_printf("Unknown variable '%%%c'", key);
*var_r = t_strdup_printf("UNSUPPORTED_VARIABLE_%c", key);
return 0;
}
static int
var_expand_hash(struct var_expand_context *ctx,
const char *key, const char *field,
const char **result_r, const char **error_r)
{
enum {
FORMAT_HEX,
FORMAT_HEX_UC,
FORMAT_BASE64
} format = FORMAT_HEX;
const char *p = strchr(key, ';');
const char *const *args = NULL;
const char *algo = key;
const char *value;
int ret;
if (p != NULL) {
algo = t_strcut(key, ';');
args = t_strsplit(p+1, ",");
}
const struct hash_method *method;
if (strcmp(algo, "pkcs5") == 0) {
method = hash_method_lookup("sha256");
} else if ((method = hash_method_lookup(algo)) == NULL) {
return 0;
}
string_t *field_value = t_str_new(64);
string_t *salt = t_str_new(64);
string_t *tmp = t_str_new(method->digest_size);
if ((ret = var_expand_long(ctx, field, strlen(field),
&value, error_r)) < 1) {
return ret;
}
str_append(field_value, value);
/* default values */
unsigned int rounds = 1;
unsigned int truncbits = 0;
if (strcmp(algo, "pkcs5") == 0) {
rounds = 2048;
str_append(salt, field);
}
while(args != NULL && *args != NULL) {
const char *k = t_strcut(*args, '=');
const char *value = strchr(*args, '=');
if (value == NULL) {
args++;
continue;
} else {
value++;
}
if (strcmp(k, "rounds") == 0) {
if (str_to_uint(value, &rounds)<0) {
*error_r = t_strdup_printf(
"Cannot parse hash arguments:"
"'%s' is not number for rounds",
value);
return -1;
}
if (rounds < 1) {
*error_r = t_strdup_printf(
"Cannot parse hash arguments:"
"rounds must be at least 1");
return -1;
}
} else if (strcmp(k, "truncate") == 0) {
if (str_to_uint(value, &truncbits)<0) {
*error_r = t_strdup_printf(
"Cannot parse hash arguments:"
"'%s' is not number for truncbits",
value);
return -1;
}
truncbits = I_MIN(truncbits, method->digest_size*8);
} else if (strcmp(k, "salt") == 0) {
str_truncate(salt, 0);
if (var_expand_with_funcs(salt, value, ctx->table,
ctx->func_table, ctx->context,
error_r) < 0) {
return -1;
}
break;
} else if (strcmp(k, "format") == 0) {
if (strcmp(value, "hex") == 0) {
format = FORMAT_HEX;
} else if (strcmp(value, "hexuc") == 0){
format = FORMAT_HEX_UC;
} else if (strcmp(value, "base64") == 0) {
format = FORMAT_BASE64;
} else {
*error_r = t_strdup_printf(
"Cannot parse hash arguments:"
"'%s' is not supported format",
value);
return -1;
}
}
args++;
}
str_truncate(tmp, 0);
if (strcmp(algo, "pkcs5") == 0) {
if (pkcs5_pbkdf(PKCS5_PBKDF2, method,
field_value->data, field_value->used,
salt->data, salt->used,
rounds, HMAC_MAX_CONTEXT_SIZE, tmp) != 0) {
*error_r = "Cannot hash: PKCS5_PBKDF2 failed";
return -1;
}
} else {
void *context = t_malloc_no0(method->context_size);
str_append_str(tmp, field_value);
for(;rounds>0;rounds--) {
method->init(context);
if (salt->used > 0)
method->loop(context, salt->data, salt->used);
method->loop(context, tmp->data, tmp->used);
unsigned char *digest =
buffer_get_modifiable_data(tmp, NULL);
method->result(context, digest);
if (tmp->used != method->digest_size)
buffer_set_used_size(tmp, method->digest_size);
}
}
if (truncbits > 0)
buffer_truncate_rshift_bits(tmp, truncbits);
switch(format) {
case FORMAT_HEX:
*result_r = binary_to_hex(tmp->data, tmp->used);
return 1;
case FORMAT_HEX_UC:
*result_r = binary_to_hex(tmp->data, tmp->used);
return 1;
case FORMAT_BASE64: {
string_t *dest = t_str_new(64);
base64_encode(tmp->data, tmp->used, dest);
*result_r = str_c(dest);
return 1;
}
}
i_unreached();
}
static int
var_expand_func(const struct var_expand_func_table *func_table,
const char *key, const char *data, void *context,
const char **var_r, const char **error_r)
{
const char *value = NULL;
int ret;
if (strcmp(key, "env") == 0) {
value = getenv(data);
*var_r = value != NULL ? value : "";
return 1;
}
if (func_table != NULL) {
for (; func_table->key != NULL; func_table++) {
if (strcmp(func_table->key, key) == 0) {
ret = func_table->func(data, context, &value, error_r);
*var_r = value != NULL ? value : "";
return ret;
}
}
}
if (*error_r == NULL)
*error_r = t_strdup_printf("Unknown variable '%%%s'", key);
*var_r = t_strdup_printf("UNSUPPORTED_VARIABLE_%s", key);
return 0;
}
static int
var_expand_try_extension(struct var_expand_context *ctx,
const char *key, const char *data,
const char **var_r, const char **error_r)
{
int ret;
const char *sep = strchr(key, ';');
if (sep == NULL) sep = key + strlen(key);
/* try with extensions */
const struct var_expand_extension_func_table *f;
array_foreach(&var_expand_extensions, f) {
/* ensure we won't match abbreviations */
size_t len = sep-key;
if (strncasecmp(key, f->key, len) == 0 && f->key[len] == '\0')
return f->func(ctx, key, data, var_r, error_r);
}
if ((ret = var_expand_func(ctx->func_table, key, data,
ctx->context, var_r, error_r)) == 0) {
*error_r = t_strdup_printf("Unknown variable '%%%s'", key);
}
return ret;
}
int
var_expand_long(struct var_expand_context *ctx,
const void *key_start, size_t key_len,
const char **var_r, const char **error_r)
{
const struct var_expand_table *t;
const char *key, *value = NULL;
int ret = 1;
if (ctx->table != NULL) {
for (t = ctx->table; !TABLE_LAST(t); t++) {
if (t->long_key != NULL &&
strncmp(t->long_key, key_start, key_len) == 0 &&
t->long_key[key_len] == '\0') {
*var_r = t->value != NULL ? t->value : "";
return 1;
}
}
}
key = t_strndup(key_start, key_len);
/* built-in variables: */
switch (key_len) {
case 3:
if (strcmp(key, "pid") == 0)
value = my_pid;
else if (strcmp(key, "uid") == 0)
value = dec2str(geteuid());
else if (strcmp(key, "gid") == 0)
value = dec2str(getegid());
break;
case 8:
if (strcmp(key, "hostname") == 0)
value = my_hostname;
break;
}
if (value == NULL) {
const char *data = strchr(key, ':');
if (data != NULL)
key = t_strdup_until(key, data++);
else
data = "";
ret = var_expand_try_extension(ctx, key, data, &value, error_r);
if (ret <= 0 && value == NULL) {
value = "";
}
}
*var_r = value;
return ret;
}
int var_expand_with_funcs(string_t *dest, const char *str,
const struct var_expand_table *table,
const struct var_expand_func_table *func_table,
void *context, const char **error_r)
{
const struct var_expand_modifier *m;
const char *var;
struct var_expand_context ctx;
const char *(*modifier[MAX_MODIFIER_COUNT])
(const char *, struct var_expand_context *);
const char *end;
unsigned int i, modifier_count;
size_t len;
int ret, final_ret = 1;
*error_r = NULL;
i_zero(&ctx);
ctx.table = table;
ctx.func_table = func_table;
ctx.context = context;
for (; *str != '\0'; str++) {
if (*str != '%')
str_append_c(dest, *str);
else {
int sign = 1;
str++;
/* reset per-field modifiers */
ctx.offset = 0;
ctx.width = 0;
ctx.zero_padding = FALSE;
/* [<offset>.]<width>[<modifiers>]<variable> */
if (*str == '-') {
sign = -1;
str++;
}
if (*str == '0') {
ctx.zero_padding = TRUE;
str++;
}
while (*str >= '0' && *str <= '9') {
ctx.width = ctx.width*10 + (*str - '0');
str++;
}
if (*str == '.') {
ctx.offset = sign * ctx.width;
sign = 1;
ctx.width = 0;
str++;
/* if offset was prefixed with zero (or it was
plain zero), just ignore that. zero padding
is done with the width. */
ctx.zero_padding = FALSE;
if (*str == '0') {
ctx.zero_padding = TRUE;
str++;
}
if (*str == '-') {
sign = -1;
str++;
}
while (*str >= '0' && *str <= '9') {
ctx.width = ctx.width*10 + (*str - '0');
str++;
}
ctx.width = sign * ctx.width;
}
modifier_count = 0;
while (modifier_count < MAX_MODIFIER_COUNT) {
modifier[modifier_count] = NULL;
for (m = modifiers; m->key != '\0'; m++) {
if (m->key == *str) {
/* @UNSAFE */
modifier[modifier_count] =
m->func;
str++;
break;
}
}
if (modifier[modifier_count] == NULL)
break;
modifier_count++;
}
if (*str == '\0')
break;
var = NULL;
if (*str == '{' && strchr(str, '}') != NULL) {
/* %{long_key} */
unsigned int ctr = 1;
bool escape = FALSE;
end = str;
while(*++end != '\0' && ctr > 0) {
if (!escape && *end == '\\') {
escape = TRUE;
continue;
}
if (escape) {
escape = FALSE;
continue;
}
if (*end == '{') ctr++;
if (*end == '}') ctr--;
}
if (ctr == 0)
/* it needs to come back a bit */
end--;
/* if there is no } it will consume rest of the
string */
len = end - (str + 1);
ret = var_expand_long(&ctx, str+1, len,
&var, error_r);
str = end;
} else {
ret = var_expand_short(ctx.table, *str,
&var, error_r);
}
i_assert(var != NULL);
if (final_ret > ret)
final_ret = ret;
if (ret <= 0)
str_append(dest, var);
else {
for (i = 0; i < modifier_count; i++)
var = modifier[i](var, &ctx);
if (ctx.offset < 0) {
/* if offset is < 0 then we want to
start at the end */
size_t len = strlen(var);
size_t offset_from_end = -ctx.offset;
if (len > offset_from_end)
var += len - offset_from_end;
} else {
while (*var != '\0' && ctx.offset > 0) {
ctx.offset--;
var++;
}
}
if (ctx.width == 0)
str_append(dest, var);
else if (!ctx.zero_padding) {
if (ctx.width < 0)
ctx.width = strlen(var) - (-ctx.width);
str_append_max(dest, var, ctx.width);
} else {
/* %05d -like padding. no truncation. */
ssize_t len = strlen(var);
while (len < ctx.width) {
str_append_c(dest, '0');
ctx.width--;
}
str_append(dest, var);
}
}
}
}
return final_ret;
}
int var_expand(string_t *dest, const char *str,
const struct var_expand_table *table, const char **error_r)
{
return var_expand_with_funcs(dest, str, table, NULL, NULL, error_r);
}
static bool
var_get_key_range_full(const char *str, unsigned int *idx_r,
unsigned int *size_r)
{
const struct var_expand_modifier *m;
unsigned int i = 0;
/* [<offset>.]<width>[<modifiers>]<variable> */
while ((str[i] >= '0' && str[i] <= '9') || str[i] == '-')
i++;
if (str[i] == '.') {
i++;
while ((str[i] >= '0' && str[i] <= '9') || str[i] == '-')
i++;
}
do {
for (m = modifiers; m->key != '\0'; m++) {
if (m->key == str[i]) {
i++;
break;
}
}
} while (m->key != '\0');
if (str[i] != '{') {
/* short key */
*idx_r = i;
*size_r = str[i] == '\0' ? 0 : 1;
return FALSE;
} else {
unsigned int depth = 1;
bool escape = FALSE;
/* long key */
*idx_r = ++i;
for (; str[i] != '\0'; i++) {
if (!escape && str[i] == '\\') {
escape = TRUE;
continue;
}
if (escape) {
escape = FALSE;
continue;
}
if (str[i] == '{')
depth++;
if (str[i] == '}') {
if (--depth==0)
break;
}
}
*size_r = i - *idx_r;
return TRUE;
}
}
char var_get_key(const char *str)
{
unsigned int idx, size;
if (var_get_key_range_full(str, &idx, &size))
return '{';
return str[idx];
}
void var_get_key_range(const char *str, unsigned int *idx_r,
unsigned int *size_r)
{
(void)var_get_key_range_full(str, idx_r, size_r);
}
static bool var_has_long_key(const char **str, const char *long_key)
{
const char *start, *end;
start = strchr(*str, '{');
i_assert(start != NULL);
end = strchr(++start, '}');
if (end == NULL)
return FALSE;
if (strncmp(start, long_key, end-start) == 0 &&
long_key[end-start] == '\0')
return TRUE;
*str = end;
return FALSE;
}
bool var_has_key(const char *str, char key, const char *long_key)
{
char c;
for (; *str != '\0'; str++) {
if (*str == '%' && str[1] != '\0') {
str++;
c = var_get_key(str);
if (c == key && key != '\0')
return TRUE;
if (c == '{' && long_key != NULL) {
if (var_has_long_key(&str, long_key))
return TRUE;
}
}
}
return FALSE;
}
void var_expand_extensions_deinit(void)
{
array_free(&var_expand_extensions);
}
void var_expand_extensions_init(void)
{
i_array_init(&var_expand_extensions, 32);
/* put all hash methods there */
for(const struct hash_method **meth = hash_methods;
*meth != NULL;
meth++) {
struct var_expand_extension_func_table *func =
array_append_space(&var_expand_extensions);
func->key = (*meth)->name;
func->func = var_expand_hash;
}
/* pkcs5 */
struct var_expand_extension_func_table *func =
array_append_space(&var_expand_extensions);
func->key = "pkcs5";
func->func = var_expand_hash;
/* if */
func = array_append_space(&var_expand_extensions);
func->key = "if";
func->func = var_expand_if;
}
void
var_expand_register_func_array(const struct var_expand_extension_func_table *funcs)
{
for(const struct var_expand_extension_func_table *ptr = funcs;
ptr->key != NULL;
ptr++) {
i_assert(*ptr->key != '\0');
array_push_front(&var_expand_extensions, ptr);
}
}
void
var_expand_unregister_func_array(const struct var_expand_extension_func_table *funcs)
{
for(const struct var_expand_extension_func_table *ptr = funcs;
ptr->key != NULL;
ptr++) {
i_assert(ptr->func != NULL);
for(unsigned int i = 0; i < array_count(&var_expand_extensions); i++) {
const struct var_expand_extension_func_table *func =
array_idx(&var_expand_extensions, i);
if (strcasecmp(func->key, ptr->key) == 0) {
array_delete(&var_expand_extensions, i, 1);
}
}
}
}
struct var_expand_table *
var_expand_merge_tables(pool_t pool, const struct var_expand_table *a,
const struct var_expand_table *b)
{
ARRAY(struct var_expand_table) table;
size_t a_size = var_expand_table_size(a);
size_t b_size = var_expand_table_size(b);
p_array_init(&table, pool, a_size + b_size + 1);
for(size_t i=0; i<a_size; i++) {
struct var_expand_table *entry =
array_append_space(&table);
entry->key = a[i].key;
entry->value = p_strdup(pool, a[i].value);
entry->long_key = p_strdup(pool, a[i].long_key);
}
for(size_t i=0; i<b_size; i++) {
struct var_expand_table *entry =
array_append_space(&table);
entry->key = b[i].key;
entry->value = p_strdup(pool, b[i].value);
entry->long_key = p_strdup(pool, b[i].long_key);
}
array_append_zero(&table);
return array_front_modifiable(&table);
}
|