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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/*
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>
struct ldb_dn *sysdb_user_dn(struct sysdb_ctx *ctx, void *memctx,
const char *domain, const char *name)
{
return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_USER, name, domain);
}
struct ldb_dn *sysdb_group_dn(struct sysdb_ctx *ctx, void *memctx,
const char *domain, const char *name)
{
return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_GROUP, name, domain);
}
struct ldb_context *sysdb_ctx_get_ldb(struct sysdb_ctx *ctx)
{
return ctx->ldb;
}
struct sysdb_attrs *sysdb_new_attrs(TALLOC_CTX *memctx)
{
return talloc_zero(memctx, struct sysdb_attrs);
}
static int sysdb_attrs_get_el(struct sysdb_attrs *attrs, const char *name,
struct ldb_message_element **el)
{
struct ldb_message_element *e = NULL;
int i;
for (i = 0; i < attrs->num; i++) {
if (strcasecmp(name, attrs->a[i].name) == 0)
e = &(attrs->a[i]);
}
if (!e) {
e = talloc_realloc(attrs, attrs->a,
struct ldb_message_element, attrs->num+1);
if (!e) return ENOMEM;
attrs->a = e;
e[attrs->num].name = talloc_strdup(e, name);
if (!e[attrs->num].name) return ENOMEM;
e[attrs->num].num_values = 0;
e[attrs->num].values = NULL;
e[attrs->num].flags = 0;
e = &(attrs->a[attrs->num]);
attrs->num++;
}
*el = e;
return EOK;
}
int sysdb_attrs_add_val(struct sysdb_attrs *attrs,
const char *name, const struct ldb_val *val)
{
struct ldb_message_element *el;
struct ldb_val *vals;
int ret;
ret = sysdb_attrs_get_el(attrs, name, &el);
vals = talloc_realloc(attrs->a, el->values,
struct ldb_val, el->num_values+1);
if (!vals) return ENOMEM;
vals[el->num_values] = ldb_val_dup(vals, val);
if (vals[el->num_values].data == NULL &&
vals[el->num_values].length != 0) {
return ENOMEM;
}
el->values = vals;
el->num_values++;
return EOK;
}
int sysdb_attrs_add_string(struct sysdb_attrs *attrs,
const char *name, const char *str)
{
struct ldb_val v;
v.data = (uint8_t *)str;
v.length = strlen(str);
return sysdb_attrs_add_val(attrs, name, &v);
}
/************************************************
* 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;
}
|