~ubuntu-branches/debian/sid/openchange/sid

« back to all changes in this revision

Viewing changes to mapiproxy/libmapistore/mapistore_processing.c

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-04-12 20:07:57 UTC
  • mfrom: (11 sid)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20120412200757-k933d9trljmxj1l4
Tags: 1:1.0-4
* openchangeserver: Add dependency on openchangeproxy.
* Rebuild against newer version of Samba 4.
* Use dpkg-buildflags.
* Migrate to Git, update Vcs-Git header.
* Switch to debhelper 9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
   OpenChange Project
5
5
 
6
 
   Copyright (C) Julien Kerihuel 2009-2011
 
6
   Copyright (C) Julien Kerihuel 2009-2012
7
7
 
8
8
   This program is free software; you can redistribute it and/or modify
9
9
   it under the terms of the GNU General Public License as published by
19
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 */
21
21
 
22
 
 
23
22
#include <sys/types.h>
24
 
 
25
 
#define __STDC_FORMAT_MACROS    1
26
 
#include <inttypes.h>
27
 
 
28
23
#include <dirent.h>
29
24
#include <fcntl.h>
30
25
#include <string.h>
31
26
#include <errno.h>
32
27
 
 
28
#include "mapistore.h"
33
29
#include "mapistore_errors.h"
34
 
#include "mapistore.h"
35
 
#include "mapistore_common.h"
36
30
#include "mapistore_private.h"
37
31
#include <dlinklist.h>
 
32
#include "libmapi/libmapi_private.h"
38
33
 
39
34
#include <tdb.h>
40
35
 
41
 
char    *mapping_path = NULL;
42
 
char    *mapistore_dbpath = NULL;
43
 
char    *mapistore_nprops_dbpath = NULL;
44
 
char    *mapistore_firstorgdn = NULL;
 
36
char *mapping_path = NULL;
45
37
 
46
38
/**
47
39
   \details Set the mapping path
55
47
 
56
48
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
57
49
 */
58
 
_PUBLIC_ enum MAPISTORE_ERROR mapistore_set_mapping_path(const char *path)
 
50
_PUBLIC_ enum mapistore_error mapistore_set_mapping_path(const char *path)
59
51
{
60
52
        TALLOC_CTX      *mem_ctx;
61
53
        DIR             *dir;
78
70
 
79
71
        /* Step 1. Check if path is valid path */
80
72
        dir = opendir(path);
81
 
        if (!dir) {
82
 
                return MAPISTORE_ERR_NO_DIRECTORY;
83
 
        }
 
73
        MAPISTORE_RETVAL_IF(!dir, MAPISTORE_ERR_NO_DIRECTORY, NULL);
84
74
 
85
75
        /* Step 2. TODO: Check for write permissions */
86
 
 
87
 
        if (closedir(dir) == -1) {
88
 
                /* FIXME: Should have a better error name here */
89
 
                return MAPISTORE_ERR_NO_DIRECTORY;
90
 
        }
 
76
        /* FIXME: Should have a better error name here */
 
77
        MAPISTORE_RETVAL_IF(closedir(dir) == -1, MAPISTORE_ERR_NO_DIRECTORY, NULL);
91
78
        
92
79
        mem_ctx = talloc_autofree_context();
93
80
        mapping_path = talloc_strdup(mem_ctx, path);
101
88
 */
102
89
const char *mapistore_get_mapping_path(void)
103
90
{
104
 
        return (!mapping_path) ? MAPISTORE_MAPPING_PATH : (const char *)mapping_path;
105
 
}
106
 
 
107
 
 
108
 
/**
109
 
   \details Set the mapistore.ldb mapping path
110
 
 
111
 
   \param dbname string pointer to the mapistore database path
112
 
 
113
 
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
114
 
 */
115
 
enum MAPISTORE_ERROR mapistore_set_database_path(const char *dbname)
116
 
{
117
 
        TALLOC_CTX      *mem_ctx;
118
 
 
119
 
        if (!dbname) {
120
 
                if (mapistore_dbpath) {
121
 
                        talloc_free(mapistore_dbpath);
122
 
                }
123
 
                mapistore_dbpath = NULL;
124
 
                return MAPISTORE_SUCCESS;
125
 
        }
126
 
 
127
 
        if (mapistore_dbpath) {
128
 
                talloc_free(mapistore_dbpath);
129
 
                mapistore_dbpath = NULL;
130
 
        }
131
 
 
132
 
        mem_ctx = talloc_autofree_context();
133
 
        mapistore_dbpath = talloc_strdup(mem_ctx, dbname);
134
 
 
135
 
        return MAPISTORE_SUCCESS;
136
 
}
137
 
 
138
 
 
139
 
/**
140
 
   \details Return the current path to mapistore.ldb database
141
 
 
142
 
   \return pointer to the mapistore database path
143
 
 */
144
 
const char *mapistore_get_database_path(void)
145
 
{
146
 
        return (!mapistore_dbpath) ? MAPISTORE_DBPATH : (const char *) mapistore_dbpath;
147
 
}
148
 
 
149
 
/**
150
 
   \details Return the current path to mapistore named properties LDIF
151
 
   files
152
 
 
153
 
   \return pointer to the mapistore named properties LDIF path
154
 
 */
155
 
const char *mapistore_get_named_properties_ldif_path(void)
156
 
{
157
 
        return MAPISTORE_LDIF;
158
 
}
159
 
 
160
 
/**
161
 
   \details Set the mapistore_named_properties.ldb mapping path
162
 
 
163
 
   \param dbname string pointer to the mapistore named properties database path
164
 
 
165
 
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
166
 
 */
167
 
enum MAPISTORE_ERROR mapistore_set_named_properties_database_path(const char *dbname)
168
 
{
169
 
        TALLOC_CTX      *mem_ctx;
170
 
 
171
 
        if (!dbname) {
172
 
                if (mapistore_nprops_dbpath) {
173
 
                        talloc_free(mapistore_nprops_dbpath);
174
 
                }
175
 
                mapistore_nprops_dbpath = NULL;
176
 
                return MAPISTORE_SUCCESS;
177
 
        }
178
 
 
179
 
        if (mapistore_nprops_dbpath) {
180
 
                talloc_free(mapistore_nprops_dbpath);
181
 
                mapistore_nprops_dbpath = NULL;
182
 
        }
183
 
 
184
 
        mem_ctx = talloc_autofree_context();
185
 
        mapistore_nprops_dbpath = talloc_strdup(mem_ctx, dbname);
186
 
 
187
 
        return MAPISTORE_SUCCESS;
188
 
}
189
 
 
190
 
/**
191
 
   \details Return the current path to mapistore_named_properties.ldb
192
 
   database
193
 
 
194
 
   \return pointer to the mapistore named properties database path on
195
 
   success, otherwise NULL
196
 
 */
197
 
const char *mapistore_get_named_properties_database_path(void)
198
 
{
199
 
        return (!mapistore_nprops_dbpath) ? MAPISTORE_DB_NAMEDPROPS_PATH : mapistore_nprops_dbpath;
200
 
}
201
 
 
202
 
 
203
 
enum MAPISTORE_ERROR mapistore_set_firstorgdn(const char *firstou, const char *firstorg, const char *serverdn)
204
 
{
205
 
        TALLOC_CTX      *mem_ctx;
206
 
 
207
 
        if (mapistore_firstorgdn) {
208
 
                talloc_free(mapistore_firstorgdn);
209
 
        }
210
 
 
211
 
        mem_ctx = talloc_autofree_context();
212
 
        mapistore_firstorgdn = talloc_asprintf(mem_ctx, TMPL_MDB_FIRSTORGDN, firstou, firstorg, serverdn);
213
 
        if (!mapistore_firstorgdn) {
214
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_CRITICAL, "Unable to allocate memory to set %s\n", "firstorgdn");
215
 
                return MAPISTORE_ERR_NO_MEMORY;
216
 
        }
217
 
 
218
 
        return MAPISTORE_SUCCESS;
219
 
}
220
 
 
221
 
const char *mapistore_get_firstorgdn(void)
222
 
{
223
 
        return mapistore_firstorgdn;
 
91
        return (const char *)mapping_path;
224
92
}
225
93
 
226
94
 
232
100
 
233
101
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
234
102
 */
235
 
enum MAPISTORE_ERROR mapistore_init_mapping_context(struct processing_context *pctx)
 
103
enum mapistore_error mapistore_init_mapping_context(struct processing_context *pctx)
236
104
{
237
105
        TDB_DATA        key;
238
106
        TDB_DATA        dbuf;
242
110
        char            *tmp_buf;
243
111
        int             ret;
244
112
 
245
 
        /* mapistore_v2 */
246
 
        const char      *db_path;
247
 
 
248
 
        /* Sanity Checks */
 
113
        /* Sanity checks */
249
114
        MAPISTORE_RETVAL_IF(!pctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
250
115
        MAPISTORE_RETVAL_IF(pctx->mapping_ctx, MAPISTORE_ERR_ALREADY_INITIALIZED, NULL);
251
116
 
252
117
        pctx->mapping_ctx = talloc_zero(pctx, struct id_mapping_context);
253
118
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx, MAPISTORE_ERR_NO_MEMORY, NULL);
254
119
 
255
 
        mem_ctx = talloc_named(NULL, 0, __FUNCTION__);
256
 
 
257
 
        /* Step 1. Retrieve the mapistore database path */
258
 
        db_path = mapistore_get_database_path();
259
 
        if (!db_path) {
260
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_CRITICAL, "Unable to retrieve the mapistore database %s\n", "path");
261
 
                talloc_free(mem_ctx);
262
 
                talloc_free(pctx->mapping_ctx);
263
 
                return MAPISTORE_ERR_DATABASE_INIT;
264
 
        }
265
 
 
266
 
        /* Step 2. Initialize tevent structure */
267
 
        pctx->mapping_ctx->ev = tevent_context_init(pctx);
268
 
 
269
 
        /* Step 3. Open a wrapped connection to mapistore.ldb */
270
 
        pctx->mapping_ctx->ldb_ctx = mapistore_ldb_wrap_connect(pctx, pctx->mapping_ctx->ev, db_path, 0);
271
 
        if (!pctx->mapping_ctx->ldb_ctx) {
272
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_CRITICAL, "Failed to open wrapped connection over %s\n", "mapistore.ldb");
273
 
                talloc_free(pctx->mapping_ctx);
274
 
                return MAPISTORE_ERR_DATABASE_INIT;
275
 
        }
 
120
        mem_ctx = talloc_named(NULL, 0, "mapistore_init_mapping_context");
276
121
 
277
122
        /* Open/Create the used ID database */
278
123
        if (!pctx->mapping_ctx->used_ctx) {
279
124
                dbpath = talloc_asprintf(mem_ctx, "%s/%s", mapistore_get_mapping_path(), MAPISTORE_DB_NAME_USED_ID);
280
 
                pctx->mapping_ctx->used_ctx = tdb_wrap_open(pctx, dbpath, 0, 0, O_RDWR|O_CREAT, 0600);
 
125
                pctx->mapping_ctx->used_ctx = mapistore_tdb_wrap_open(pctx, dbpath, 0, 0, O_RDWR|O_CREAT, 0600);
281
126
                talloc_free(dbpath);
282
127
                if (!pctx->mapping_ctx->used_ctx) {
283
 
                        MSTORE_DEBUG_ERROR(MSTORE_LEVEL_CRITICAL, "Unable to create/open used ID database: %s\n", strerror(errno));
 
128
                        DEBUG(3, ("[%s:%d]: %s\n", __FUNCTION__, __LINE__, strerror(errno)));
284
129
                        talloc_free(mem_ctx);
285
130
                        talloc_free(pctx->mapping_ctx);
286
131
                        return MAPISTORE_ERR_DATABASE_INIT;
302
147
                ret = tdb_store(pctx->mapping_ctx->used_ctx->tdb, key, dbuf, TDB_INSERT);
303
148
                talloc_free(dbuf.dptr);
304
149
                if (ret == -1) {
305
 
                        MSTORE_DEBUG_ERROR(MSTORE_LEVEL_CRITICAL, "Unable to create %s record: %s\n",
306
 
                                           MAPISTORE_DB_LAST_ID_KEY, tdb_errorstr(pctx->mapping_ctx->used_ctx->tdb));
 
150
                        DEBUG(3, ("[%s:%d]: Unable to create %s record: %s\n", __FUNCTION__, __LINE__,
 
151
                                  MAPISTORE_DB_LAST_ID_KEY, tdb_errorstr(pctx->mapping_ctx->used_ctx->tdb)));
307
152
                        talloc_free(mem_ctx);
308
153
                        talloc_free(pctx->mapping_ctx);
309
154
 
324
169
        return MAPISTORE_SUCCESS;
325
170
}
326
171
 
327
 
enum MAPISTORE_ERROR mapistore_ldb_write_ldif_string_to_store(struct ldb_context *ldb_ctx, const char *ldif_string)
328
 
{
329
 
        int                     ret;
330
 
        struct ldb_ldif         *ldif;
331
 
 
332
 
        /* Sanity checks */
333
 
        MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
334
 
        MAPISTORE_RETVAL_IF(!ldif_string, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
335
 
 
336
 
        while ((ldif = ldb_ldif_read_string(ldb_ctx, (const char **)&ldif_string))) {
337
 
                ret = ldb_msg_normalize(ldb_ctx, ldif, ldif->msg, &ldif->msg);
338
 
                if (ret != LDB_SUCCESS) {
339
 
                        ldb_ldif_read_free(ldb_ctx, ldif);
340
 
                        MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to normalize %s\n", "ldif");
341
 
                        return MAPISTORE_ERROR;
342
 
                }
343
 
                ret = ldb_add(ldb_ctx, ldif->msg);
344
 
                if (ret != LDB_SUCCESS) {
345
 
                        ldb_ldif_read_free(ldb_ctx, ldif);
346
 
                        MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to add ldb %s\n", "msg");
347
 
                        return MAPISTORE_ERROR;
348
 
                }
349
 
                ldb_ldif_read_free(ldb_ctx, ldif);
350
 
        }
351
 
 
352
 
        return MAPISTORE_SUCCESS;
353
 
}
354
 
 
355
 
 
356
 
enum MAPISTORE_ERROR mapistore_write_ldif_string_to_store(struct processing_context *pctx, const char *ldif_string)
357
 
{
358
 
        struct ldb_context      *ldb_ctx;
359
 
 
360
 
        /* Sanity checks */
361
 
        MAPISTORE_RETVAL_IF(!pctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
362
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
363
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx->ldb_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
364
 
        MAPISTORE_RETVAL_IF(!ldif_string, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
365
 
 
366
 
        ldb_ctx = pctx->mapping_ctx->ldb_ctx;
367
 
 
368
 
        return mapistore_ldb_write_ldif_string_to_store(ldb_ctx, ldif_string);
369
 
}
370
 
 
371
 
 
372
 
enum MAPISTORE_ERROR mapistore_ldb_write_ldif_file_to_store(struct ldb_context *ldb_ctx, const char *ldif_path)
373
 
{
374
 
        int                     ret;
375
 
        FILE                    *f;
376
 
        struct ldb_ldif         *ldif;
377
 
        TALLOC_CTX              *mem_ctx;
378
 
 
379
 
        /* Sanity checks */
380
 
        MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
381
 
        MAPISTORE_RETVAL_IF(!ldif_path, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
382
 
 
383
 
        f = fopen(ldif_path, "r");
384
 
        MAPISTORE_RETVAL_IF(!f, MAPISTORE_ERR_DATABASE_INIT, NULL);
385
 
 
386
 
        mem_ctx = talloc_named(NULL, 0, __FUNCTION__);
387
 
 
388
 
        while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) {
389
 
                struct ldb_message      *normalized_msg;
390
 
 
391
 
                ret = ldb_msg_normalize(ldb_ctx, mem_ctx, ldif->msg, &normalized_msg);
392
 
                MAPISTORE_RETVAL_IF(ret != LDB_SUCCESS, MAPISTORE_ERR_DATABASE_OPS, mem_ctx);
393
 
                ret = ldb_add(ldb_ctx, normalized_msg);
394
 
                if (ret != LDB_SUCCESS) {
395
 
                        fclose(f);
396
 
                        MAPISTORE_RETVAL_IF(ret != LDB_SUCCESS, MAPISTORE_ERR_DATABASE_OPS, mem_ctx);
397
 
                }
398
 
                ldb_ldif_read_free(ldb_ctx, ldif);
399
 
                talloc_free(normalized_msg);
400
 
        }
401
 
 
402
 
        fclose(f);
403
 
        talloc_free(mem_ctx);
404
 
 
405
 
        return MAPISTORE_SUCCESS;
406
 
}
407
 
 
408
 
 
409
 
/**
410
 
   \details Retrieve the next available folder or message identifier
411
 
 
412
 
   \param pctx pointer to the mapistore processing context
413
 
   \param username the username's mailbox to retrieve GlobalCount from
414
 
   \param fmid pointer to the first available fmid within the range
415
 
 
416
 
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
417
 
 */
418
 
enum MAPISTORE_ERROR mapistore_get_new_fmid(struct processing_context *pctx,
419
 
                                            const char *username,
420
 
                                            uint64_t *fmid)
421
 
{
422
 
        int                     ret;
423
 
        TALLOC_CTX              *mem_ctx;
424
 
        struct ldb_context      *ldb_ctx;
425
 
        struct ldb_result       *res = NULL;
426
 
        struct ldb_message      *msg;
427
 
        const char * const      attrs[] = { "*", NULL };
428
 
        uint64_t                ReplicaID;
429
 
        uint64_t                GlobalCount;
430
 
        uint64_t                new_GlobalCount;
431
 
 
432
 
        /* Sanity checks */
433
 
        MAPISTORE_RETVAL_IF(!pctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
434
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
435
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx->ldb_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
436
 
        MAPISTORE_RETVAL_IF(!username, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
437
 
        MAPISTORE_RETVAL_IF(!fmid, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
438
 
 
439
 
        mem_ctx = talloc_named(NULL, 0, __FUNCTION__);
440
 
 
441
 
        /* Step 1. Retrieve the server object */
442
 
        ldb_ctx = pctx->mapping_ctx->ldb_ctx;
443
 
 
444
 
        /* TODO: In the future, we may want to deal with different servers */
445
 
        /* TODO: Use ldb_transaction to lock between get/set operations */
446
 
        ret = ldb_search(ldb_ctx, mem_ctx, &res, ldb_get_root_basedn(ldb_ctx),
447
 
                         LDB_SCOPE_SUBTREE, attrs, "(&(cn=%s)(objectClass=store))", username);
448
 
        if (ret != LDB_SUCCESS || res->count != 1) {
449
 
                talloc_free(mem_ctx);
450
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to find the user store %s\n", "object");
451
 
                return MAPISTORE_ERR_NOT_FOUND;
452
 
        }
453
 
 
454
 
        /* Step 2. Get the [48 GlobalCount][16 ReplicaID] */
455
 
        GlobalCount = ldb_msg_find_attr_as_uint64(res->msgs[0], "GlobalCount", 0);
456
 
        new_GlobalCount = GlobalCount + 1;
457
 
 
458
 
        ReplicaID = ldb_msg_find_attr_as_uint64(res->msgs[0], "ReplicaID", 0);
459
 
 
460
 
        /* Step 3. Update the GlobalCount */
461
 
        msg = ldb_msg_new(mem_ctx);
462
 
        msg->dn = ldb_dn_copy(msg, ldb_msg_find_attr_as_dn(ldb_ctx, mem_ctx, res->msgs[0], "distinguishedName"));
463
 
        ldb_msg_add_fmt(msg, "GlobalCount", "0x%"PRIx64, new_GlobalCount);
464
 
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
465
 
        ret = ldb_modify(ldb_ctx, msg);
466
 
        if (ret != LDB_SUCCESS) {
467
 
                talloc_free(mem_ctx);
468
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to update GlobalCount for server %s\n", "object");
469
 
                return MAPISTORE_ERROR;
470
 
        }
471
 
 
472
 
        /* Step 4. Set the fmid value */
473
 
 
474
 
        talloc_free(mem_ctx);
475
 
        *fmid = (GlobalCount << 16) + ReplicaID;
476
 
 
477
 
        return MAPISTORE_SUCCESS;
478
 
}
479
 
 
480
 
 
481
 
/**
482
 
   \details Retrieve an allocation range
483
 
 
484
 
   \param pctx pointer to the mapistore processing context
485
 
   \param username the user store from which to retrieve the new allocation range
486
 
   \param range the range to allocate
487
 
   \param range_start pointer to the first allocation range fmid to return
488
 
   \param range_end pointer to the last allocation range fmid to return
489
 
 
490
 
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
491
 
 */
492
 
enum MAPISTORE_ERROR mapistore_get_new_allocation_range(struct processing_context *pctx,
493
 
                                                        const char *username,
494
 
                                                        uint64_t range,
495
 
                                                        uint64_t *range_start,
496
 
                                                        uint64_t *range_end)
497
 
{
498
 
        int                     ret;
499
 
        TALLOC_CTX              *mem_ctx;
500
 
        struct ldb_context      *ldb_ctx;
501
 
        struct ldb_result       *res = NULL;
502
 
        struct ldb_message      *msg;
503
 
        const char * const      attrs[] = { "*", NULL };
504
 
        uint64_t                ReplicaID;
505
 
        uint64_t                GlobalCount;
506
 
        uint64_t                new_GlobalCount;
507
 
 
508
 
        /* Sanity checks */
509
 
        MAPISTORE_RETVAL_IF(!pctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
510
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
511
 
        MAPISTORE_RETVAL_IF(!pctx->mapping_ctx->ldb_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
512
 
        MAPISTORE_RETVAL_IF(!range_start || !range_end, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
513
 
 
514
 
        mem_ctx = talloc_named(NULL, 0, __FUNCTION__);
515
 
 
516
 
        /* Step 1. Retrieve the user store object */
517
 
        ldb_ctx = pctx->mapping_ctx->ldb_ctx;
518
 
        ret = ldb_search(ldb_ctx, mem_ctx, &res, ldb_get_root_basedn(ldb_ctx),
519
 
                         LDB_SCOPE_SUBTREE, attrs, "(&(objectClass=store)(cn=%s))", username);
520
 
        if (ret != LDB_SUCCESS || res->count != 1) {
521
 
                talloc_free(mem_ctx);
522
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to find the user store %s\n", "object");
523
 
                return MAPISTORE_ERR_NOT_FOUND;
524
 
        }
525
 
 
526
 
        /* Step 2. Get the current GlobalCount and set the new one */
527
 
        GlobalCount = ldb_msg_find_attr_as_uint64(res->msgs[0], "GlobalCount", 0);
528
 
        ReplicaID = ldb_msg_find_attr_as_uint64(res->msgs[0], "ReplicaID", 0);
529
 
 
530
 
        *range_start = GlobalCount;
531
 
 
532
 
        new_GlobalCount = GlobalCount + range -1;
533
 
        *range_end = new_GlobalCount;
534
 
 
535
 
        new_GlobalCount += 1;
536
 
 
537
 
        /* Step 3. Update the GlobalCount */
538
 
        msg = ldb_msg_new(mem_ctx);
539
 
        msg->dn = ldb_dn_copy(msg, ldb_msg_find_attr_as_dn(ldb_ctx, mem_ctx, res->msgs[0], "distinguishedName"));
540
 
        ldb_msg_add_fmt(msg, "GlobalCount", "0x%"PRIx64, new_GlobalCount);
541
 
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
542
 
        ret = ldb_modify(ldb_ctx, msg);
543
 
        if (ret != LDB_SUCCESS) {
544
 
                talloc_free(mem_ctx);
545
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to update GlobalCount for server %s\n", "object");
546
 
                return MAPISTORE_ERROR;
547
 
        }
548
 
 
549
 
        /* Step 4. Set the fmid value ranges */
550
 
        talloc_free(mem_ctx);
551
 
        *range_start = (*range_start << 16) + ReplicaID;
552
 
        *range_end = (*range_end << 16) + ReplicaID;
553
 
 
554
 
        return MAPISTORE_SUCCESS;
555
 
}
556
 
 
557
 
 
558
 
/**
559
 
   \details Retrieve the mapistore URI for a given user mailbox
560
 
 
561
 
   \param pctx pointer to the processing context
562
 
   \param username pointer to the username to lookup
563
 
   \param mapistore_uri pointer on pointer to the mapistore URI
564
 
 
565
 
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
566
 
 */
567
 
enum MAPISTORE_ERROR mapistore_get_mailbox_uri(struct processing_context *pctx,
568
 
                                               const char *username,
569
 
                                               char **mapistore_uri)
570
 
{
571
 
        int                     ret;
572
 
        TALLOC_CTX              *mem_ctx;
573
 
        const char              *uri;
574
 
        const char * const      attrs[] = { "*", NULL };
575
 
        struct ldb_context      *ldb_ctx;
576
 
        struct ldb_result       *res;
577
 
        struct ldb_dn           *dn;
578
 
 
579
 
        /* Sanity checks */
580
 
        MAPISTORE_RETVAL_IF(!pctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
581
 
        MAPISTORE_RETVAL_IF(!username, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
582
 
        MAPISTORE_RETVAL_IF(!mapistore_uri, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
583
 
 
584
 
        mem_ctx = talloc_new(pctx);
585
 
        ldb_ctx = pctx->mapping_ctx->ldb_ctx;
586
 
 
587
 
        dn = ldb_dn_new_fmt(mem_ctx, ldb_ctx, TMPL_MDB_USERSTORE, username, mapistore_get_firstorgdn());
588
 
        ret = ldb_search(ldb_ctx, mem_ctx, &res, dn, LDB_SCOPE_SUBTREE, attrs, "(&(objectClass=mailbox)(objectClass=container))");
589
 
        if (ret != LDB_SUCCESS || res->count != 1) {
590
 
                talloc_free(mem_ctx);
591
 
                MSTORE_DEBUG_ERROR(MSTORE_LEVEL_INFO, "Unable to find the user mailbox root %s\n", "container");
592
 
                return MAPISTORE_ERR_NOT_FOUND;
593
 
        }
594
 
 
595
 
        uri = ldb_msg_find_attr_as_string(res->msgs[0], "mapistore_uri", NULL);
596
 
        MAPISTORE_RETVAL_IF(!uri, MAPISTORE_ERR_NOT_FOUND, mem_ctx);
597
 
 
598
 
        *mapistore_uri = talloc_strdup(pctx, (char *)uri);
599
 
 
600
 
        talloc_free(mem_ctx);
601
 
        return MAPISTORE_SUCCESS;
602
 
}
603
 
 
604
172
 
605
173
/**
606
174
   \details Return an unused or new context identifier
611
179
 
612
180
   \return a non zero context identifier on success, otherwise 0.
613
181
 */
614
 
enum MAPISTORE_ERROR mapistore_get_context_id(struct processing_context *pctx, uint32_t *context_id)
 
182
enum mapistore_error mapistore_get_context_id(struct processing_context *pctx, uint32_t *context_id)
615
183
{
616
184
        struct context_id_list  *el;
617
185
 
645
213
 
646
214
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
647
215
 */
648
 
enum MAPISTORE_ERROR mapistore_free_context_id(struct processing_context *pctx, uint32_t context_id)
 
216
enum mapistore_error mapistore_free_context_id(struct processing_context *pctx, uint32_t context_id)
649
217
{
650
218
        struct context_id_list  *el;
651
219