2
OpenChange Storage Abstraction Layer library
6
Copyright (C) Julien Kerihuel 2010
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.
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.
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/>.
22
#include "mapistore.h"
23
#include "mapistore_errors.h"
24
#include "mapistore_private.h"
25
#include <libmapi/defs_private.h>
29
static const char *mapistore_namedprops_get_ldif_path(void)
31
return MAPISTORE_LDIF;
35
\details Initialize the named properties database or return pointer
36
to the existing one if already initialized/opened.
38
\param mem_ctx pointer to the memory context
39
\param ldb_ctx pointer on pointer to the ldb context the function
42
\return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
44
int mapistore_namedprops_init(TALLOC_CTX *mem_ctx, void **_ldb_ctx)
48
struct ldb_context *ldb_ctx = NULL;
49
struct ldb_ldif *ldif;
52
struct tevent_context *ev;
56
MAPISTORE_RETVAL_IF(!mem_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
57
MAPISTORE_RETVAL_IF(!_ldb_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
59
ev = tevent_context_init(mem_ctx);
60
MAPISTORE_RETVAL_IF(!ev, MAPISTORE_ERR_NO_MEMORY, NULL);
62
database = talloc_asprintf(mem_ctx, "%s/%s", mapistore_get_mapping_path(), MAPISTORE_DB_NAMED);
63
DEBUG(0, ("database = %s\n", database));
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);
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);
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) {
82
MAPISTORE_RETVAL_IF(ret, MAPISTORE_ERR_DATABASE_INIT, NULL);
84
ldb_ldif_read_free(ldb_ctx, ldif);
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);
96
return MAPISTORE_SUCCESS;
101
\details return the mapped property ID matching the nameid
102
structure passed in parameter.
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
108
\return MAPISTORE_SUCCESS on success, otherwise MAPISTORE_ERROR
110
_PUBLIC_ int mapistore_namedprops_get_mapped_id(void *ldb_ctx,
111
struct MAPINAMEID nameid,
115
struct ldb_result *res = NULL;
116
const char * const attrs[] = { "*", NULL };
122
MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERROR, NULL);
123
MAPISTORE_RETVAL_IF(!propID, MAPISTORE_ERROR, NULL);
126
mem_ctx = talloc_named(NULL, 0, "mapistore_namedprops_get_mapped_propID");
127
guid = GUID_string(mem_ctx, (const struct GUID *)&nameid.lpguid);
129
switch (nameid.ulKind) {
131
filter = talloc_asprintf(mem_ctx, "(&(objectClass=MNID_ID)(oleguid=%s)(cn=0x%.4x))",
132
guid, nameid.kind.lid);
135
filter = talloc_asprintf(mem_ctx, "(&(objectClass=MNID_STRING)(oleguid=%s)(cn=%s))",
136
guid, nameid.kind.lpwstr.Name);
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);
145
*propID = ldb_msg_find_attr_as_uint(res->msgs[0], "mapped_id", 0);
146
MAPISTORE_RETVAL_IF(!*propID, MAPISTORE_ERROR, mem_ctx);
149
talloc_free(mem_ctx);
151
return MAPISTORE_SUCCESS;