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
|
/*
Copyright (C) 2004 IC & S dbmail@ic-s.nl
Copyright (c) 2005-2006 NFG Net Facilities Group BV support@nfg.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dbmail.h"
#define NR_ACL_FLAGS 9
#define THIS_MODULE "acl"
static const char *acl_right_strings[] = {
"lookup_flag",
"read_flag",
"seen_flag",
"write_flag",
"insert_flag",
"post_flag",
"create_flag",
"delete_flag",
"administer_flag"
};
static const char acl_right_chars[] = "lrswipcda";
//{'l','r','s','w','i','p','c','d','a'};
/* local functions */
static ACLRight_t acl_get_right_from_char(char right_char);
static int acl_change_rights(u64_t userid, u64_t mboxid,
const char *rightsstring, int set);
static int acl_replace_rights(u64_t userid, u64_t mboxid,
const char *rightsstring);
static int acl_set_one_right(u64_t userid, u64_t mboxid,
ACLRight_t right, int set);
static int acl_get_rightsstring_identifier(char *identifier, u64_t mboxid,
/*@out@*/ char *rightsstring);
static int acl_get_rightsstring(u64_t userid, u64_t mboxid,
/*@out@*/ char *rightsstring);
int acl_has_right(mailbox_t *mailbox, u64_t userid, ACLRight_t right)
{
u64_t anyone_userid;
int test;
switch(right) {
case ACL_RIGHT_SEEN:
case ACL_RIGHT_WRITE:
case ACL_RIGHT_INSERT:
case ACL_RIGHT_POST:
case ACL_RIGHT_CREATE:
case ACL_RIGHT_DELETE:
case ACL_RIGHT_ADMINISTER:
if (mailbox_is_writable(mailbox->uid))
return FALSE;
break;
case ACL_RIGHT_LOOKUP:
case ACL_RIGHT_READ:
case ACL_RIGHT_NONE:
/* Write access not required;
* check these by flags. */
break;
}
const char *right_flag = acl_right_strings[right];
/* Check if the user has the right; this will also
* return true if the user is the mailbox owner. */
if ((test = db_acl_has_right(mailbox, userid, right_flag)))
return TRUE;
/* else check the 'anyone' user */
test = auth_user_exists(DBMAIL_ACL_ANYONE_USER, &anyone_userid);
if (test == DM_EQUERY)
return DM_EQUERY;
if (test)
return db_acl_has_right(mailbox, anyone_userid, right_flag);
return FALSE;
}
int acl_set_rights(u64_t userid, u64_t mboxid, const char *rightsstring)
{
if (rightsstring[0] == '-')
return acl_change_rights(userid, mboxid, rightsstring, 0);
if (rightsstring[0] == '+')
return acl_change_rights(userid, mboxid, rightsstring, 1);
return acl_replace_rights(userid, mboxid, rightsstring);
}
ACLRight_t acl_get_right_from_char(char right_char)
{
switch (right_char) {
case 'l':
return ACL_RIGHT_LOOKUP;
case 'r':
return ACL_RIGHT_READ;
case 's':
return ACL_RIGHT_SEEN;
case 'w':
return ACL_RIGHT_WRITE;
case 'i':
return ACL_RIGHT_INSERT;
case 'p':
return ACL_RIGHT_POST;
case 'c':
return ACL_RIGHT_CREATE;
case 'd':
return ACL_RIGHT_DELETE;
case 'a':
return ACL_RIGHT_ADMINISTER;
default:
TRACE(TRACE_ERROR, "error wrong acl character. This error should have been caught earlier!");
return ACL_RIGHT_NONE;
}
}
int
acl_change_rights(u64_t userid, u64_t mboxid, const char *rightsstring,
int set)
{
size_t i;
char rightchar;
for (i = 1; i < strlen(rightsstring); i++) {
rightchar = rightsstring[i];
if (acl_set_one_right(userid, mboxid,
acl_get_right_from_char(rightchar),
set) < 0)
return -1;
}
return 1;
}
int
acl_replace_rights(u64_t userid, u64_t mboxid, const char *rightsstring)
{
unsigned i;
int set;
TRACE(TRACE_DEBUG, "replacing rights for user [%" U64_T_FORMAT "], mailbox [%" U64_T_FORMAT "] to %s", userid, mboxid, rightsstring);
for (i = ACL_RIGHT_LOOKUP; i < ACL_RIGHT_NONE; i++) {
if (strchr(rightsstring, (int) acl_right_chars[i]))
set = 1;
else
set = 0;
if (db_acl_set_right
(userid, mboxid, acl_right_strings[i], set) < 0) {
TRACE(TRACE_ERROR, "error replacing ACL");
return -1;
}
}
return 1;
}
int
acl_set_one_right(u64_t userid, u64_t mboxid, ACLRight_t right, int set)
{
return db_acl_set_right(userid, mboxid, acl_right_strings[right],
set);
}
/*
int acl_delete_acl(u64_t userid, u64_t mboxid)
{
return db_acl_delete_acl(userid, mboxid);
}
*/
char *acl_get_acl(u64_t mboxid)
{
u64_t userid;
char *username;
size_t acl_string_size = 0;
size_t acl_strlen;
char *acl_string; /* return string */
char *identifier; /* one identifier */
char *identifier_astring; /* identifier as IMAP astring */
char rightsstring[NR_ACL_FLAGS + 1];
int result;
struct dm_list identifier_list;
struct element *identifier_elm;
unsigned nr_identifiers = 0;
result = db_acl_get_identifier(mboxid, &identifier_list);
if (result < 0) {
TRACE(TRACE_ERROR, "error when getting identifier list for mailbox [%" U64_T_FORMAT "].", mboxid);
dm_list_free(&identifier_list.start);
return NULL;
}
/* add the current user to the list if this user is the owner
* of the mailbox
*/
if (db_get_mailbox_owner(mboxid, &userid) < 0) {
TRACE(TRACE_ERROR, "error querying ownership of mailbox");
dm_list_free(&identifier_list.start);
return NULL;
}
if ((username = auth_get_userid(userid)) == NULL) {
TRACE(TRACE_ERROR, "error getting username for user [%" U64_T_FORMAT "]", userid);
dm_list_free(&identifier_list.start);
return NULL;
}
if (dm_list_nodeadd(&identifier_list, username, strlen(username) + 1) == NULL) {
TRACE(TRACE_ERROR, "error adding username to list");
dm_list_free(&identifier_list.start);
g_free(username);
return NULL;
}
g_free(username);
TRACE(TRACE_DEBUG, "before looping identifiers!");
identifier_elm = dm_list_getstart(&identifier_list);
while (identifier_elm) {
nr_identifiers++;
identifier_astring = dbmail_imap_astring_as_string(identifier_elm->data);
acl_string_size += strlen(identifier_astring) + NR_ACL_FLAGS + 2;
g_free(identifier_astring);
identifier_elm = identifier_elm->nextnode;
}
TRACE(TRACE_DEBUG, "acl_string size = %zd", acl_string_size);
if (! (acl_string = g_new0(char, acl_string_size + 1))) {
dm_list_free(&identifier_list.start);
TRACE(TRACE_FATAL, "error allocating memory");
return NULL;
}
identifier_elm = dm_list_getstart(&identifier_list);
while (identifier_elm) {
identifier = (char *) identifier_elm->data;
if (acl_get_rightsstring_identifier(identifier, mboxid, rightsstring) < 0) {
dm_list_free(&identifier_list.start);
g_free(acl_string);
return NULL;
}
TRACE(TRACE_DEBUG, "%s", rightsstring);
if (strlen(rightsstring) > 0) {
acl_strlen = strlen(acl_string);
identifier_astring = dbmail_imap_astring_as_string(identifier);
(void) snprintf(&acl_string[acl_strlen], acl_string_size - acl_strlen, "%s %s ", identifier_astring, rightsstring);
g_free(identifier_astring);
}
identifier_elm = identifier_elm->nextnode;
}
dm_list_free(&identifier_list.start);
return g_strstrip(acl_string);
}
char *acl_listrights(u64_t userid, u64_t mboxid)
{
int result;
if ((result = db_user_is_mailbox_owner(userid, mboxid)) < 0) {
TRACE(TRACE_ERROR, "error checking if user is owner of a mailbox");
return NULL;
}
if (result == 0) {
/* user is not owner. User will never be granted any right
by default, but may be granted any right by setting the
right ACL */
return g_strdup("\"\" l r s w i p c d a");
}
/* user is owner, User will always be granted all rights */
return g_strdup(acl_right_chars);
}
char *acl_myrights(u64_t userid, u64_t mboxid)
{
char *rightsstring;
if (! (rightsstring = g_new0(char, NR_ACL_FLAGS + 1))) {
TRACE(TRACE_ERROR, "error allocating memory for rightsstring");
return NULL;
}
if (acl_get_rightsstring(userid, mboxid, rightsstring) < 0) {
TRACE(TRACE_ERROR, "error getting rightsstring.");
g_free(rightsstring);
return NULL;
}
return rightsstring;
}
int acl_get_rightsstring_identifier(char *identifier, u64_t mboxid, char *rightsstring)
{
u64_t userid;
assert(rightsstring);
memset(rightsstring, '\0', NR_ACL_FLAGS + 1);
if (auth_user_exists(identifier, &userid) < 0) {
TRACE(TRACE_ERROR, "error finding user id for user with name [%s]", identifier);
return -1;
}
return acl_get_rightsstring(userid, mboxid, rightsstring);
}
int acl_get_rightsstring(u64_t userid, u64_t mboxid, char *rightsstring)
{
int result;
u64_t owner_idnr;
mailbox_t mailbox;
struct ACLMap map;
assert(rightsstring);
memset(rightsstring, '\0', NR_ACL_FLAGS + 1);
if ((result = db_get_mailbox_owner(mboxid, &owner_idnr)) <= 0)
return result;
if (owner_idnr == userid) {
TRACE(TRACE_DEBUG, "mailbox [%" U64_T_FORMAT "] is owned by user [%" U64_T_FORMAT "], giving all rights", mboxid, userid);
g_strlcat(rightsstring, acl_right_chars, NR_ACL_FLAGS+1);
return 1;
}
memset(&mailbox, '\0', sizeof(mailbox_t));
memset(&map, '\0', sizeof(struct ACLMap));
mailbox.uid = mboxid;
mailbox.owner_idnr = owner_idnr;
if ((result = db_acl_get_acl_map(&mailbox, userid, &map)) == DM_EQUERY)
return result;
if (map.lookup_flag)
g_strlcat(rightsstring,"l", NR_ACL_FLAGS+1);
if (map.read_flag)
g_strlcat(rightsstring,"r", NR_ACL_FLAGS+1);
if (map.seen_flag)
g_strlcat(rightsstring,"s", NR_ACL_FLAGS+1);
if (map.write_flag)
g_strlcat(rightsstring,"w", NR_ACL_FLAGS+1);
if (map.insert_flag)
g_strlcat(rightsstring,"i", NR_ACL_FLAGS+1);
if (map.post_flag)
g_strlcat(rightsstring,"p", NR_ACL_FLAGS+1);
if (map.create_flag)
g_strlcat(rightsstring,"c", NR_ACL_FLAGS+1);
if (map.delete_flag)
g_strlcat(rightsstring,"d", NR_ACL_FLAGS+1);
if (map.administer_flag)
g_strlcat(rightsstring,"a", NR_ACL_FLAGS+1);
return 1;
}
|