~ubuntu-branches/debian/experimental/openchange/experimental

« back to all changes in this revision

Viewing changes to mapiproxy/libmapistore/mapistore_namedprops.c

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij, Mathieu Parent, Jelmer Vernooij
  • Date: 2010-06-19 00:14:54 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100619001454-c9qbwzlbv3rgqrir
Tags: 1:0.9+svn1970-1
[ Mathieu Parent ]
* Add Vcs-Browser
* libmapi-dev depends on libndr-standard-dev.
  Fix "Missing dependency on samba4-dev" (Closes: #549012)

[ Jelmer Vernooij ]
* Use source format version 3.
* Bump standards version to 3.8.4.
* New upstream snapshot.
* Add build dependency on zlib1g-dev. Closes: #577330
* Migrate packaging to Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   OpenChange Storage Abstraction Layer library
 
3
 
 
4
   OpenChange Project
 
5
 
 
6
   Copyright (C) Julien Kerihuel 2010
 
7
 
 
8
   This program is free software; you can redistribute it and/or modify
 
9
   it under the terms of the GNU General Public License as published by
 
10
   the Free Software Foundation; either version 3 of the License, or
 
11
   (at your option) any later version.
 
12
   
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
   
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "mapistore.h"
 
23
#include "mapistore_errors.h"
 
24
#include "mapistore_private.h"
 
25
#include <libmapi/defs_private.h>
 
26
#include <ldb.h>
 
27
 
 
28
 
 
29
static const char *mapistore_namedprops_get_ldif_path(void)
 
30
{
 
31
        return MAPISTORE_LDIF;
 
32
}
 
33
 
 
34
/**
 
35
   \details Initialize the named properties database or return pointer
 
36
   to the existing one if already initialized/opened.
 
37
 
 
38
   \param mem_ctx pointer to the memory context
 
39
   \param ldb_ctx pointer on pointer to the ldb context the function
 
40
   returns
 
41
 
 
42
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
 
43
 */
 
44
int mapistore_namedprops_init(TALLOC_CTX *mem_ctx, void **_ldb_ctx)
 
45
{
 
46
        int                     ret;
 
47
        struct stat             sb;
 
48
        struct ldb_context      *ldb_ctx = NULL;
 
49
        struct ldb_ldif         *ldif;
 
50
        char                    *filename;
 
51
        FILE                    *f;
 
52
        struct tevent_context   *ev;
 
53
        char                    *database;
 
54
 
 
55
        /* Sanity checks */
 
56
        MAPISTORE_RETVAL_IF(!mem_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
 
57
        MAPISTORE_RETVAL_IF(!_ldb_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
 
58
 
 
59
        ev = tevent_context_init(mem_ctx);
 
60
        MAPISTORE_RETVAL_IF(!ev, MAPISTORE_ERR_NO_MEMORY, NULL);
 
61
 
 
62
        database = talloc_asprintf(mem_ctx, "%s/%s", mapistore_get_mapping_path(), MAPISTORE_DB_NAMED);
 
63
        DEBUG(0, ("database = %s\n", database));
 
64
 
 
65
        /* Step 1. Stat the database and populate it if it doesn't exist */
 
66
        if (stat(database, &sb) == -1) {
 
67
                ldb_ctx = mapistore_ldb_wrap_connect(ldb_ctx, ev, database, 0);
 
68
                talloc_free(database);
 
69
                MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_DATABASE_INIT, NULL);
 
70
 
 
71
                filename = talloc_asprintf(mem_ctx, "%s/mapistore_namedprops.ldif", 
 
72
                                           mapistore_namedprops_get_ldif_path());
 
73
                f = fopen(filename, "r");
 
74
                talloc_free(filename);
 
75
                MAPISTORE_RETVAL_IF(!f, MAPISTORE_ERROR, NULL);
 
76
 
 
77
                while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) {
 
78
                        ldif->msg = ldb_msg_canonicalize(ldb_ctx, ldif->msg);
 
79
                        ret = ldb_add(ldb_ctx, ldif->msg);
 
80
                        if (ret != LDB_SUCCESS) {
 
81
                                fclose(f);
 
82
                                MAPISTORE_RETVAL_IF(ret, MAPISTORE_ERR_DATABASE_INIT, NULL);
 
83
                        }
 
84
                        ldb_ldif_read_free(ldb_ctx, ldif);
 
85
                }
 
86
                fclose(f);
 
87
 
 
88
        } else {
 
89
                ldb_ctx = mapistore_ldb_wrap_connect(ldb_ctx, ev, database, 0);
 
90
                talloc_free(database);
 
91
                MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_DATABASE_INIT, NULL);
 
92
        }
 
93
 
 
94
        *_ldb_ctx = ldb_ctx;
 
95
 
 
96
        return MAPISTORE_SUCCESS;
 
97
}
 
98
 
 
99
 
 
100
/**
 
101
   \details return the mapped property ID matching the nameid
 
102
   structure passed in parameter.
 
103
 
 
104
   \param ldb_ctx pointer to the namedprops ldb context
 
105
   \param nameid the MAPINAMEID structure to lookup
 
106
   \param propID pointer to the property ID the function returns
 
107
 
 
108
   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE_ERROR
 
109
 */
 
110
_PUBLIC_ int mapistore_namedprops_get_mapped_id(void *ldb_ctx, 
 
111
                                                struct MAPINAMEID nameid, 
 
112
                                                uint16_t *propID)
 
113
{
 
114
        TALLOC_CTX              *mem_ctx;
 
115
        struct ldb_result       *res = NULL;
 
116
        const char * const      attrs[] = { "*", NULL };
 
117
        int                     ret;
 
118
        char                    *filter = NULL;
 
119
        char                    *guid;
 
120
 
 
121
        /* Sanity checks */
 
122
        MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERROR, NULL);
 
123
        MAPISTORE_RETVAL_IF(!propID, MAPISTORE_ERROR, NULL);
 
124
 
 
125
        *propID = 0;
 
126
        mem_ctx = talloc_named(NULL, 0, "mapistore_namedprops_get_mapped_propID");
 
127
        guid = GUID_string(mem_ctx, (const struct GUID *)&nameid.lpguid);
 
128
 
 
129
        switch (nameid.ulKind) {
 
130
        case MNID_ID:
 
131
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=MNID_ID)(oleguid=%s)(cn=0x%.4x))",
 
132
                                         guid, nameid.kind.lid);
 
133
                break;
 
134
        case MNID_STRING:
 
135
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=MNID_STRING)(oleguid=%s)(cn=%s))",
 
136
                                         guid, nameid.kind.lpwstr.Name);
 
137
                break;
 
138
        }
 
139
        talloc_free(guid);
 
140
 
 
141
        ret = ldb_search(ldb_ctx, mem_ctx, &res, ldb_get_default_basedn(ldb_ctx),
 
142
                         LDB_SCOPE_SUBTREE, attrs, filter);
 
143
        MAPISTORE_RETVAL_IF(ret != LDB_SUCCESS || !res->count, MAPISTORE_ERROR, mem_ctx);
 
144
 
 
145
        *propID = ldb_msg_find_attr_as_uint(res->msgs[0], "mapped_id", 0);
 
146
        MAPISTORE_RETVAL_IF(!*propID, MAPISTORE_ERROR, mem_ctx);
 
147
 
 
148
        talloc_free(filter);
 
149
        talloc_free(mem_ctx);
 
150
 
 
151
        return MAPISTORE_SUCCESS;
 
152
}