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
|
/*
SSSD
System Database
Copyright (C) Simo Sorce <ssorce@redhat.com> 2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util/util.h"
#include "db/sysdb_private.h"
#include "confdb/confdb.h"
#include <time.h>
/************************************************
* Initialiazation stuff
*/
static int sysdb_read_var(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *name,
const char *def_value,
char **target)
{
int ret;
char **values;
ret = confdb_get_param(cdb, mem_ctx,
SYSDB_CONF_SECTION,
name, &values);
if (ret != EOK)
return ret;
if (values[0])
*target = values[0];
else
*target = talloc_strdup(mem_ctx, def_value);
return EOK;
}
static int sysdb_get_db_path(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
char **db_path)
{
TALLOC_CTX *tmp_ctx;
char *default_ldb_path;
char *path;
int ret;
tmp_ctx = talloc_new(mem_ctx);
if (!tmp_ctx)
return ENOMEM;
default_ldb_path = talloc_asprintf(tmp_ctx, "%s/%s", DB_PATH, SYSDB_FILE);
if (default_ldb_path == NULL) {
ret = ENOMEM;
goto done;
}
sysdb_read_var(tmp_ctx, cdb, "ldbFile",
default_ldb_path, &path);
*db_path = talloc_steal(mem_ctx, path);
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
static int sysdb_check_init(struct sysdb_ctx *ctx)
{
TALLOC_CTX *tmp_ctx;
const char *base_ldif;
struct ldb_ldif *ldif;
struct ldb_message_element *el;
struct ldb_result *res;
struct ldb_dn *verdn;
char *version = NULL;
int ret;
tmp_ctx = talloc_new(ctx);
if (!tmp_ctx)
return ENOMEM;
verdn = ldb_dn_new(tmp_ctx, ctx->ldb, "cn=sysdb");
if (!verdn) {
ret = EIO;
goto done;
}
ret = ldb_search(ctx->ldb, tmp_ctx, &res,
verdn, LDB_SCOPE_BASE,
NULL, NULL);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
if (res->count > 1) {
ret = EIO;
goto done;
}
if (res->count == 1) {
el = ldb_msg_find_element(res->msgs[0], "version");
if (el) {
if (el->num_values != 1) {
ret = EINVAL;
goto done;
}
version = talloc_strndup(tmp_ctx,
(char *)(el->values[0].data),
el->values[0].length);
if (!version) {
ret = ENOMEM;
goto done;
}
if (strcmp(version, SYSDB_VERSION) == 0) {
/* all fine, return */
ret = EOK;
goto done;
}
}
DEBUG(0,("Unknown DB version [%s], expected [%s], aborting!\n",
version?version:"not found", SYSDB_VERSION));
ret = EINVAL;
goto done;
}
/* cn=sysdb does not exists, means db is empty, populate */
base_ldif = SYSDB_BASE_LDIF;
while ((ldif = ldb_ldif_read_string(ctx->ldb, &base_ldif))) {
ret = ldb_add(ctx->ldb, ldif->msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to inizialiaze DB (%d,[%s]), aborting!\n",
ret, ldb_errstring(ctx->ldb)));
ret = EIO;
goto done;
}
ldb_ldif_read_free(ctx->ldb, ldif);
}
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
int sysdb_init(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct confdb_ctx *cdb,
const char *alt_db_path,
struct sysdb_ctx **_ctx)
{
struct sysdb_ctx *ctx;
int ret;
if (!ev) return EINVAL;
ctx = talloc_zero(mem_ctx, struct sysdb_ctx);
if (!ctx) {
return ENOMEM;
}
ctx->ev = ev;
if (!alt_db_path) {
ret = sysdb_get_db_path(ctx, cdb, &ctx->ldb_file);
if (ret != EOK) {
return ret;
}
} else {
ctx->ldb_file = talloc_strdup(ctx, alt_db_path);
}
if (ctx->ldb_file == NULL) {
return ENOMEM;
}
DEBUG(3, ("DB Path is: %s\n", ctx->ldb_file));
ctx->ldb = ldb_init(ctx, ev);
if (!ctx->ldb) {
talloc_free(ctx);
return EIO;
}
ret = ldb_connect(ctx->ldb, ctx->ldb_file, 0, NULL);
if (ret != LDB_SUCCESS) {
talloc_free(ctx);
return EIO;
}
ret = sysdb_check_init(ctx);
if (ret != EOK) {
talloc_free(ctx);
return ret;
}
*_ctx = ctx;
return EOK;
}
|