summaryrefslogtreecommitdiff
blob: 87fa920fc08fe4360cbe4dde6e6caa498e6d3b22 (plain)
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
Provides a new version of annotation support for the cyrus imapd server [Version: 2.3.9]

diff -r 321cda1d6136 imap/annotate.c
--- a/imap/annotate.c	Tue Apr 22 10:44:56 2008 +0200
+++ b/imap/annotate.c	Tue Apr 22 10:47:04 2008 +0200
@@ -90,6 +90,8 @@ int (*proxy_fetch_func)(const char *serv
 			struct strlist *attribute_pat) = NULL;
 int (*proxy_store_func)(const char *server, const char *mbox_pat,
 			struct entryattlist *entryatts) = NULL;
+
+void init_annotation_definitions();
 
 /* String List Management */
 /*
@@ -237,6 +239,8 @@ void annotatemore_init(int myflags,
     if (store_func) {
 	proxy_store_func = store_func;
     }
+    
+    init_annotation_definitions();
 }
 
 void annotatemore_open(char *fname)
@@ -1832,6 +1836,224 @@ const struct annotate_st_entry mailbox_r
     { NULL, 0, ANNOTATION_PROXY_T_INVALID, 0, 0, NULL, NULL }
 };
 
+struct annotate_st_entry_list *server_entries_list = NULL;
+struct annotate_st_entry_list *mailbox_rw_entries_list = NULL;
+
+enum {
+  ANNOTATION_SCOPE_SERVER = 1,
+  ANNOTATION_SCOPE_MAILBOX = 2
+};
+
+const struct annotate_attrib annotation_scope_names[] =
+{
+    { "server", ANNOTATION_SCOPE_SERVER },
+    { "mailbox", ANNOTATION_SCOPE_MAILBOX },
+    { NULL, 0 }
+};
+
+const struct annotate_attrib annotation_proxy_type_names[] =
+{
+    { "proxy", PROXY_ONLY },
+    { "backend", BACKEND_ONLY },
+    { "proxy_and_backend", PROXY_AND_BACKEND },
+    { NULL, 0 }
+};
+
+const struct annotate_attrib attribute_type_names[] = 
+{
+    { "content-type", ATTRIB_TYPE_CONTENTTYPE },
+    { "string", ATTRIB_TYPE_STRING },
+    { "boolean", ATTRIB_TYPE_BOOLEAN },
+    { "uint", ATTRIB_TYPE_UINT },
+    { "int", ATTRIB_TYPE_INT },
+    { NULL, 0 }
+};
+
+#define ANNOT_DEF_MAXLINELEN 1024
+
+int table_lookup(const struct annotate_attrib *table,
+                                  char* name,
+                                  size_t namelen,
+                                  char* errmsg) 
+/* search in table for the value given by name and namelen (name is null-terminated,
+   but possibly more than just the key). errmsg is used to hint the user where we failed */
+{
+  char errbuf[ANNOT_DEF_MAXLINELEN*2];
+  int entry;
+  
+  for (entry = 0; table[entry].name &&
+                  (strncasecmp(table[entry].name, name, namelen)
+                   || table[entry].name[namelen] != '\0'); entry++);
+
+  if (! table[entry].name) {
+    sprintf(errbuf, "invalid %s at '%s'", errmsg, name);
+    fatal(errbuf, EC_CONFIG);
+  }
+  return table[entry].entry;
+}
+
+char *consume_comma(char* p)
+  /* advance beyond the next ',', skipping whitespace, fail if next non-space is no comma */
+{
+  char errbuf[ANNOT_DEF_MAXLINELEN*2];
+
+  for (; *p && isspace(*p); p++);  
+  if (*p != ',') {
+    sprintf(errbuf, "',' expected, '%s' found parsing annotation definition",
+            p);
+    fatal(errbuf, EC_CONFIG);
+  }
+  p++;
+  for (; *p && isspace(*p); p++);  
+  
+  return p;
+}
+
+int parse_table_lookup_bitmask(const struct annotate_attrib *table,
+                               char** s,
+                               char* errmsg) 
+  /* parses strings of the form value1 [ value2 [ ... ]]
+     value1 is mapped via table to ints and the result ored
+     whitespace is allowed between value names and punctuation
+     the field must end in '\0' or ','
+     s is advanced to '\0' or ','
+     on error errmsg is used to identify item to be parsed
+   */
+{
+  char errbuf[ANNOT_DEF_MAXLINELEN*2];
+  int result = 0;
+  char *p, *p2;
+
+  p = *s;
+  do {
+    p2 = p;
+    for (; *p && (isalnum(*p) || *p=='.' || *p=='-' || *p=='_' || *p=='/');p++);
+    result |= table_lookup(table, p2, p-p2, errmsg);
+    for (; *p && isspace(*p); p++);
+  } while (*p && *p != ',');
+
+  *s = p;
+  return result;
+}
+
+void init_annotation_definitions()
+{
+  char *p, *p2, *tmp;
+  const char *filename;
+  char aline[ANNOT_DEF_MAXLINELEN];
+  char errbuf[ANNOT_DEF_MAXLINELEN*2];
+  struct annotate_st_entry_list *se, *me;
+  struct annotate_st_entry *ae;
+  int i;
+  FILE* f;
+
+  /* NOTE: we assume # static entries > 0 */
+  server_entries_list = xmalloc(sizeof(struct annotate_st_entry_list));
+  mailbox_rw_entries_list = xmalloc(sizeof(struct annotate_st_entry_list));
+  se = server_entries_list;
+  me = mailbox_rw_entries_list;
+  /* copy static entries into list */
+  for (i = 0; server_entries[i].name;i++) {
+    se->entry = &server_entries[i];
+    if (server_entries[i+1].name) {
+      se->next = xmalloc(sizeof(struct annotate_st_entry_list));
+      se = se->next;
+    }
+  }
+  /* copy static entries into list */
+  for (i = 0; mailbox_rw_entries[i].name;i++) {
+    me->entry = &mailbox_rw_entries[i];
+    if (mailbox_rw_entries[i+1].name) {
+      me->next = xmalloc(sizeof(struct annotate_st_entry_list));
+      me = me->next;
+    }
+  }
+
+  /* parse config file */
+  filename = config_getstring(IMAPOPT_ANNOTATION_DEFINITIONS);
+
+  if (! filename) {
+    se->next = NULL;
+    me->next = NULL;
+    return;
+  }
+  
+  f = fopen(filename,"r");
+  if (! f) {
+    sprintf(errbuf, "could not open annotation definiton %s", filename);
+    fatal(errbuf, EC_CONFIG);
+  }
+  
+  while (fgets(aline, sizeof(aline), f)) {
+    // remove leading space, skip blank lines and comments
+    for (p = aline; *p && isspace(*p); p++);
+    if (!*p || *p == '#') continue;
+
+    // note, we only do the most basic validity checking and may
+    // be more restrictive than neccessary
+
+    ae = xmalloc(sizeof(struct annotate_st_entry));
+
+    p2 = p;
+    for (; *p && (isalnum(*p) || *p=='.' || *p=='-' || *p=='_' || *p=='/');p++);
+    // TV-TODO: should test for empty
+    ae->name = xstrndup(p2, p-p2);
+
+    p = consume_comma(p);
+  
+    p2 = p;
+    for (; *p && (isalnum(*p) || *p=='.' || *p=='-' || *p=='_' || *p=='/');p++);
+
+    if (table_lookup(annotation_scope_names, p2, p-p2,
+                     "annotation scope")==ANNOTATION_SCOPE_SERVER) {
+      se->next = xmalloc(sizeof(struct annotate_st_entry_list));
+      se = se->next;
+      se->entry = ae;
+    }
+    else {
+      me->next = xmalloc(sizeof(struct annotate_st_entry_list));
+      me = me->next;      
+      me->entry = ae;
+    }
+    
+    p = consume_comma(p);
+    p2 = p;
+    for (; *p && (isalnum(*p) || *p=='.' || *p=='-' || *p=='_' || *p=='/');p++);
+    ae->type = table_lookup(attribute_type_names, p2, p-p2,
+                                  "attribute type");
+  
+    p = consume_comma(p);
+    ae->proxytype = parse_table_lookup_bitmask(annotation_proxy_type_names,
+                                               &p,
+                                               "annotation proxy type");
+
+    p = consume_comma(p);
+    ae->attribs = parse_table_lookup_bitmask(annotation_attributes,
+                                             &p,
+                                             "annotation attributes");
+    
+    p = consume_comma(p);
+    p2 = p;
+    for (; *p && (isalnum(*p) || *p=='.' || *p=='-' || *p=='_' || *p=='/');p++);
+    tmp = xstrndup(p2, p-p2);
+    ae->acl = cyrus_acl_strtomask(tmp);
+    free(tmp);
+
+    for (; *p && isspace(*p); p++);
+    if (*p) {
+      sprintf(errbuf, "junk at end of line: '%s'", p);
+      fatal(errbuf, EC_CONFIG);
+    }
+    
+    ae->set = annotation_set_todb;
+    ae->rock = NULL;
+  }
+
+  fclose(f);
+  se->next = NULL;
+  me->next = NULL;
+}
+
 int annotatemore_store(char *mailbox,
 		       struct entryattlist *l,
 		       struct namespace *namespace,
@@ -1843,7 +2065,7 @@ int annotatemore_store(char *mailbox,
     struct entryattlist *e = l;
     struct attvaluelist *av;
     struct storedata sdata;
-    const struct annotate_st_entry *entries;
+    const struct annotate_st_entry_list *entries, *currententry;
     time_t now = time(0);
 
     memset(&sdata, 0, sizeof(struct storedata));
@@ -1854,45 +2076,45 @@ int annotatemore_store(char *mailbox,
 
     if (!mailbox[0]) {
 	/* server annotations */
-	entries = server_entries;
+	entries = server_entries_list;
     }
     else {
 	/* mailbox annotation(s) */
-	entries = mailbox_rw_entries;
+	entries = mailbox_rw_entries_list;
     }
 
     /* Build a list of callbacks for storing the annotations */
     while (e) {
-	int entrycount, attribs;
+	int attribs;
 	struct annotate_st_entry_list *nentry = NULL;
 
 	/* See if we support this entry */
-	for (entrycount = 0;
-	     entries[entrycount].name;
-	     entrycount++) {
-	    if (!strcmp(e->entry, entries[entrycount].name)) {
+	for (currententry = entries;
+	     currententry;
+	     currententry = currententry->next) {
+	    if (!strcmp(e->entry, currententry->entry->name)) {
 		break;
 	    }
 	}
-	if (!entries[entrycount].name) {
+	if (!currententry) {
 	    /* unknown annotation */
 	    return IMAP_PERMISSION_DENIED;
 	}
 
 	/* Add this entry to our list only if it
 	   applies to our particular server type */
-	if ((entries[entrycount].proxytype != PROXY_ONLY)
+	if ((currententry->entry->proxytype != PROXY_ONLY)
 	    || proxy_store_func) {
 	    nentry = xzmalloc(sizeof(struct annotate_st_entry_list));
 	    nentry->next = sdata.entry_list;
-	    nentry->entry = &(entries[entrycount]);
+	    nentry->entry = currententry->entry;
 	    nentry->shared.modifiedsince = now;
 	    nentry->priv.modifiedsince = now;
 	    sdata.entry_list = nentry;
 	}
 
 	/* See if we are allowed to set the given attributes. */
-	attribs = entries[entrycount].attribs;
+	attribs = currententry->entry->attribs;
 	av = e->attvalues;
 	while (av) {
 	    const char *value;
@@ -1902,7 +2124,7 @@ int annotatemore_store(char *mailbox,
 		    goto cleanup;
 		}
 		value = annotate_canon_value(av->value,
-					     entries[entrycount].type);
+					     currententry->entry->type);
 		if (!value) {
 		    r = IMAP_ANNOTATION_BADVALUE;
 		    goto cleanup;
@@ -1928,7 +2150,7 @@ int annotatemore_store(char *mailbox,
 		    goto cleanup;
 		}
 		value = annotate_canon_value(av->value,
-					     entries[entrycount].type);
+					     currententry->entry->type);
 		if (!value) {
 		    r = IMAP_ANNOTATION_BADVALUE;
 		    goto cleanup;
@@ -2110,3 +2332,10 @@ int annotatemore_delete(const char *mbox
 
     return annotatemore_rename(mboxname, NULL, NULL, NULL);
 }
+
+/* This file contains code Copyright (c) 2006 by Thomas Viehmann.
+ * You may distribute source code or binaries under the conditions
+ * conditions given in the CMU license, provided this note stays intact
+ * in the distributed source. If you want to distribute my code without
+ * this notice, do contact me at <tv@beamnet.de>.
+ */
diff -r 321cda1d6136 lib/imapoptions
--- a/lib/imapoptions	Tue Apr 22 10:44:56 2008 +0200
+++ b/lib/imapoptions	Tue Apr 22 10:47:04 2008 +0200
@@ -172,6 +172,9 @@ are listed with ``<none>''.
 /* Should non-admin users be allowed to set ACLs for the 'anyone'
    user on their mailboxes?  In a large organization this can cause
    support problems, but it's enabled by default. */
+
+{ "annotation_definitions", NULL, STRING }
+/* File containing annotation definitions. */
 
 { "auth_mech", "unix", STRINGLIST("unix", "pts", "krb", "krb5")}
 /* The authorization mechanism to use. */