-
Notifications
You must be signed in to change notification settings - Fork 29
/
ext-editheader-common.c
200 lines (157 loc) · 4.75 KB
/
ext-editheader-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "mempool.h"
#include "array.h"
#include "rfc2822.h"
#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-settings.h"
#include "sieve-extensions.h"
#include "ext-editheader-limits.h"
#include "ext-editheader-common.h"
/*
* Extension configuration
*/
struct ext_editheader_header {
const char *name;
bool forbid_add:1;
bool forbid_delete:1;
};
struct ext_editheader_config {
pool_t pool;
ARRAY(struct ext_editheader_header) headers;
size_t max_header_size;
};
static struct ext_editheader_header *ext_editheader_config_header_find
(struct ext_editheader_config *ext_config, const char *hname)
{
struct ext_editheader_header *headers;
unsigned int count, i;
headers = array_get_modifiable(&ext_config->headers, &count);
for ( i = 0; i < count; i++ ) {
if ( strcasecmp(hname, headers[i].name) == 0 )
return &headers[i];
}
return NULL;
}
static void ext_editheader_config_headers
(struct sieve_instance *svinst,
struct ext_editheader_config *ext_config,
const char *setting, bool forbid_add, bool forbid_delete)
{
const char *setval;
setval = sieve_setting_get(svinst, setting);
if ( setval != NULL ) {
const char **headers = t_strsplit_spaces(setval, " \t");
while ( *headers != NULL ) {
struct ext_editheader_header *header;
if ( !rfc2822_header_field_name_verify
(*headers, strlen(*headers)) ) {
sieve_sys_warning(svinst, "editheader: "
"setting %s contains invalid header field name "
"`%s' (ignored)", setting, *headers);
continue;
}
header=ext_editheader_config_header_find(ext_config, *headers);
if ( header == NULL ) {
header = array_append_space(&ext_config->headers);
header->name = p_strdup(ext_config->pool, *headers);
}
if (forbid_add)
header->forbid_add = TRUE;
if (forbid_delete)
header->forbid_delete = TRUE;
headers++;
}
}
}
bool ext_editheader_load
(const struct sieve_extension *ext, void **context)
{
struct ext_editheader_config *ext_config;
struct sieve_instance *svinst = ext->svinst;
size_t max_header_size;
pool_t pool;
if ( *context != NULL ) {
ext_editheader_unload(ext);
*context = NULL;
}
T_BEGIN {
pool = pool_alloconly_create("editheader_config", 1024);
ext_config = p_new(pool, struct ext_editheader_config, 1);
ext_config->pool = pool;
ext_config->max_header_size = EXT_EDITHEADER_DEFAULT_MAX_HEADER_SIZE;
p_array_init(&ext_config->headers, pool, 16);
ext_editheader_config_headers(svinst, ext_config,
"sieve_editheader_protected", TRUE, TRUE);
ext_editheader_config_headers(svinst, ext_config,
"sieve_editheader_forbid_add", TRUE, FALSE);
ext_editheader_config_headers(svinst, ext_config,
"sieve_editheader_forbid_delete", FALSE, TRUE);
if ( sieve_setting_get_size_value
(svinst, "sieve_editheader_max_header_size", &max_header_size) ) {
if ( max_header_size < EXT_EDITHEADER_MINIMUM_MAX_HEADER_SIZE ) {
sieve_sys_warning(svinst,
"editheader: value of sieve_editheader_max_header_size setting "
"(=%"PRIuSIZE_T") is less than the minimum (=%"PRIuSIZE_T") "
"(ignored)", max_header_size,
(size_t) EXT_EDITHEADER_MINIMUM_MAX_HEADER_SIZE);
} else {
ext_config->max_header_size = max_header_size;
}
}
} T_END;
*context = (void *) ext_config;
return TRUE;
}
void ext_editheader_unload(const struct sieve_extension *ext)
{
struct ext_editheader_config *ext_config =
(struct ext_editheader_config *) ext->context;
if ( ext_config != NULL ) {
pool_unref(&ext_config->pool);
}
}
/*
* Protected headers
*/
bool ext_editheader_header_allow_add
(const struct sieve_extension *ext, const char *hname)
{
struct ext_editheader_config *ext_config =
(struct ext_editheader_config *) ext->context;
const struct ext_editheader_header *header;
if ( strcasecmp(hname, "subject") == 0 )
return TRUE;
if ( (header=ext_editheader_config_header_find
(ext_config, hname)) == NULL )
return TRUE;
return !header->forbid_add;
}
bool ext_editheader_header_allow_delete
(const struct sieve_extension *ext, const char *hname)
{
struct ext_editheader_config *ext_config =
(struct ext_editheader_config *) ext->context;
const struct ext_editheader_header *header;
if ( strcasecmp(hname, "received") == 0
|| strcasecmp(hname, "auto-submitted") == 0 )
return FALSE;
if ( strcasecmp(hname, "subject") == 0 )
return TRUE;
if ( (header=ext_editheader_config_header_find
(ext_config, hname)) == NULL )
return TRUE;
return !header->forbid_delete;
}
/*
* Limits
*/
bool ext_editheader_header_too_large
(const struct sieve_extension *ext, size_t size)
{
struct ext_editheader_config *ext_config =
(struct ext_editheader_config *) ext->context;
return size > ext_config->max_header_size;
}