1 /* mboxlist.h -- Mailbox list manipulation routines
2  *
3  * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  */
42 
43 #ifndef INCLUDED_MBOXLIST_H
44 #define INCLUDED_MBOXLIST_H
45 
46 #include "config.h"
47 #include "cyrusdb.h"
48 #include "dlist.h"
49 #include "mailbox.h"
50 #include "auth.h"
51 #include "mboxevent.h"
52 #include "mboxname.h"
53 
54 /*
55  * Maximum length of partition name. [config.c has a limit of 70]
56  */
57 #define MAX_PARTITION_LEN 64
58 
59 /* flags for types of mailboxes */
60 #define MBTYPE_REMOTE (1<<0) /* Not on this server (part is remote host) */
61 #define MBTYPE_RESERVE (1<<1) /* Reserved [mupdate/imapd] /
62                                Rename Target [imapd] (part is normal, but
63                                you are not allowed to create this mailbox,
64                                even though it doesn't actually exist */
65 #define MBTYPE_NETNEWS (1<<2) /* Netnews Mailbox - NO LONGER USED */
66 #define MBTYPE_MOVING (1<<3) /* Mailbox in mid-transfer (part is remotehost!localpart) */
67 #define MBTYPE_DELETED (1<<4) /* Mailbox has been deleted, but not yet cleaned up */
68 #define MBTYPE_CALENDAR (1<<5) /* CalDAV Calendar Mailbox */
69 #define MBTYPE_ADDRESSBOOK (1<<6) /* CardDAV Addressbook Mailbox */
70 #define MBTYPE_COLLECTION (1<<7) /* WebDAV Collection Mailbox */
71 
72 #define MBTYPES_DAV (MBTYPE_CALENDAR|MBTYPE_ADDRESSBOOK|MBTYPE_COLLECTION)
73 #define MBTYPES_NONIMAP (MBTYPE_NETNEWS|MBTYPES_DAV)
74 
75 /* master name of the mailboxes file */
76 #define FNAME_MBOXLIST "/mailboxes.db"
77 
78 #define HOSTNAME_SIZE 512
79 
80 /* each mailbox has the following data */
81 struct mboxlist_entry {
82     char *name;
83     char *ext_name;
84     time_t mtime;
85     uint32_t uidvalidity;
86     modseq_t foldermodseq;
87     int mbtype;
88     char *partition;
89     char *server; /* holds remote machine for REMOTE mailboxes */
90     char *acl;
91     /* extra fields */
92     char *uniqueid;
93     /* legacy upgrade support */
94     char *legacy_specialuse;
95 };
96 
97 typedef struct mboxlist_entry mbentry_t;
98 
99 mbentry_t *mboxlist_entry_create();
100 
101 char *mbentry_metapath(const struct mboxlist_entry *mbentry, int metatype, int isnew);
102 char *mbentry_datapath(const struct mboxlist_entry *mbentry, uint32_t);
103 
104 int mboxlist_parse_entry(mbentry_t **mbentryptr,
105                          const char *name, size_t namelen,
106                          const char *data, size_t datalen);
107 
108 mbentry_t *mboxlist_entry_copy(const mbentry_t *src);
109 
110 void mboxlist_entry_free(mbentry_t **mbentryptr);
111 
112 const char *mboxlist_mbtype_to_string(uint32_t mbtype);
113 uint32_t mboxlist_string_to_mbtype(const char *string);
114 
115 int mboxlist_delete(const char *name);
116 /* Lookup 'name' in the mailbox list. */
117 int mboxlist_lookup(const char *name, mbentry_t **mbentryptr,
118                     struct txn **tid);
119 int mboxlist_lookup_allow_all(const char *name,
120                                    mbentry_t **mbentryptr,
121                                    struct txn **tid);
122 
123 char *mboxlist_find_specialuse(const char *use, const char *userid);
124 char *mboxlist_find_uniqueid(const char *uniqueid, const char *userid);
125 
126 
127 /* insert/delete stub entries */
128 int mboxlist_insertremote(mbentry_t *mbentry, struct txn **rettid);
129 int mboxlist_deleteremote(const char *name, struct txn **in_tid);
130 
131 /* Update a mailbox's entry */
132 int mboxlist_update(mbentry_t *mbentry, int localonly);
133 
134 /* check user's ability to create mailbox */
135 int mboxlist_createmailboxcheck(const char *name, int mbtype,
136                                 const char *partition,
137                                 int isadmin, const char *userid,
138                                 const struct auth_state *auth_state,
139                                 char **newacl, char **newpartition,
140                                 int forceuser);
141 
142 /* create mailbox */
143 /* localonly creates the local mailbox without touching mupdate */
144 /* forceuser allows the creation of user.x.<name> without a user.x */
145 /* dbonly skips filesystem operations (e.g. reconstruct) */
146 /* notify sends a MailboxCreate event notification */
147 /* if given a mailbox pointer, return the still-locked mailbox
148  * for further manipulation */
149 int mboxlist_createmailbox(const char *name, int mbtype,
150                            const char *partition,
151                            int isadmin, const char *userid,
152                            const struct auth_state *auth_state,
153                            int localonly, int forceuser, int dbonly,
154                            int notify, struct mailbox **mailboxptr);
155 
156 /* create mailbox from sync */
157 int mboxlist_createsync(const char *name, int mbtype, const char *partition,
158                         const char *userid, const struct auth_state *auth_state,
159                         int options, unsigned uidvalidity,
160                         modseq_t highestmodseq, const char *acl,
161                         const char *uniqueid, int local_only,
162                         struct mailbox **mboxptr);
163 
164 /* delated delete */
165 /* Translate delete into rename */
166 /* prepare MailboxDelete notification if mboxevent is not NULL */
167 int
168 mboxlist_delayed_deletemailbox(const char *name, int isadmin, const char *userid,
169                                const struct auth_state *auth_state,
170                                struct mboxevent *mboxevent,
171                                int checkacl,
172                                int localonly,
173                                int force);
174 /* Delete a mailbox. */
175 /* setting local_only disables any communication with the mupdate server
176  * and deletes the mailbox from the filesystem regardless of if it is
177  * MBTYPE_REMOTE or not */
178 /* force ignores errors and just tries to wipe the mailbox off the face of
179  * the planet */
180 /* prepare MailboxDelete notification if mboxevent is not NULL */
181 int mboxlist_deletemailbox(const char *name, int isadmin, const char *userid,
182                            const struct auth_state *auth_state,
183                            struct mboxevent *mboxevent,
184                            int checkacl,
185                            int local_only, int force);
186 
187 /* Rename/move a mailbox (hierarchical) */
188 /* prepare MailboxRename notification if mboxevent is not NULL */
189 int mboxlist_renamemailbox(const char *oldname, const char *newname,
190                            const char *partition, unsigned uidvalidity,
191                            int isadmin, const char *userid,
192                            const struct auth_state *auth_state,
193                            struct mboxevent *mboxevent,
194                            int local_only, int forceuser, int ignorequota);
195 
196 /* change ACL */
197 int mboxlist_setacl(const struct namespace *namespace, const char *name,
198                     const char *identifier, const char *rights, int isadmin,
199                     const char *userid, const struct auth_state *auth_state);
200 
201 /* Change all ACLs on mailbox */
202 int mboxlist_sync_setacls(const char *name, const char *acl);
203 
204 int mboxlist_set_racls(int enabled);
205 
206 modseq_t mboxlist_foldermodseq_dirty(struct mailbox *mailbox);
207 
208 struct findall_data {
209     const char *extname;
210     int mb_category;
211     const mbentry_t *mbentry;
212     const mbname_t *mbname;
213 };
214 
215 typedef int findall_cb(struct findall_data *data, void *rock);
216 
217 /* Find all mailboxes that match 'pattern'. */
218 int mboxlist_findall(struct namespace *namespace,
219                      const char *pattern, int isadmin,
220                      const char *userid, const struct auth_state *auth_state,
221                      findall_cb *proc, void *rock);
222 int mboxlist_findallmulti(struct namespace *namespace,
223                           const strarray_t *patterns, int isadmin,
224                           const char *userid, const struct auth_state *auth_state,
225                           findall_cb *proc, void *rock);
226 int mboxlist_findone(struct namespace *namespace,
227                      const char *intname, int isadmin,
228                      const char *userid, const struct auth_state *auth_state,
229                      findall_cb *proc, void *rock);
230 
231 /* Find a mailbox's parent (if any) */
232 int mboxlist_findparent(const char *mboxname,
233                         mbentry_t **mbentryp);
234 
235 /* direct access to subs DB */
236 typedef int user_cb(const char *userid, void *rock);
237 int mboxlist_alluser(user_cb *proc, void *rock);
238 
239 typedef int mboxlist_cb(const mbentry_t *mbentry, void *rock);
240 
241 int mboxlist_visible(const char *userid, const struct auth_state *auth_state,
242                      mboxlist_cb *proc, void *rock, int incdel);
243 int mboxlist_allmbox(const char *prefix, mboxlist_cb *proc, void *rock, int incdel);
244 #define MBOXTREE_TOMBSTONES (1<<0)
245 #define MBOXTREE_DELETED (1<<1)
246 #define MBOXTREE_SKIP_ROOT (1<<2)
247 #define MBOXTREE_SKIP_CHILDREN (1<<3)
248 #define MBOXTREE_SKIP_PERSONAL (1<<4)
249 #define MBOXTREE_PLUS_RACL (1<<5)
250 int mboxlist_mboxtree(const char *mboxname, mboxlist_cb *proc, void *rock, int flags);
251 int mboxlist_usermboxtree(const char *userid, mboxlist_cb *proc, void *rock, int flags);
252 int mboxlist_usersubs(const char *userid, mboxlist_cb *proc, void *rock, int flags);
253 
254 strarray_t *mboxlist_sublist(const char *userid);
255 
256 /* Find subscribed mailboxes that match 'pattern'. */
257 int mboxlist_findsub(struct namespace *namespace,
258                      const char *pattern, int isadmin,
259                      const char *userid, const struct auth_state *auth_state,
260                      findall_cb *proc, void *rock,
261                      int force);
262 int mboxlist_findsubmulti(struct namespace *namespace,
263                           const strarray_t *patterns, int isadmin,
264                           const char *userid, const struct auth_state *auth_state,
265                           findall_cb *proc, void *rock,
266                           int force);
267 
268 /* given a mailbox 'name', where should we stage messages for it?
269    'stagedir' should be MAX_MAILBOX_PATH. */
270 int mboxlist_findstage(const char *name, char *stagedir, size_t sd_len);
271 
272 /* Check 'user's subscription status for mailbox 'name' */
273 int mboxlist_checksub(const char *name, const char *userid);
274 
275 /* Change 'user's subscription status for mailbox 'name'. */
276 int mboxlist_changesub(const char *name, const char *userid,
277                        const struct auth_state *auth_state,
278                        int add, int force, int notify);
279 
280 /* set or create quota root */
281 int mboxlist_setquotas(const char *root,
282                        quota_t newquotas[QUOTA_NUMRESOURCES], int force);
283 int mboxlist_unsetquota(const char *root);
284 
285 /* open the mailboxes db */
286 void mboxlist_open(const char *name);
287 
288 /* close the database */
289 void mboxlist_close(void);
290 
291 /* initialize database structures */
292 #define MBOXLIST_SYNC 0x02
293 void mboxlist_init(int flags);
294 
295 /* done with database stuff */
296 void mboxlist_done(void);
297 
298 /* for transactions */
299 int mboxlist_commit(struct txn *tid);
300 int mboxlist_abort(struct txn *tid);
301 
302 int mboxlist_delayed_delete_isenabled(void);
303 
304 #endif
305