summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/nssldb/ldb-nss.c
blob: e256f41a4db7b93eb77718de65bbbb12fc4cd9e5 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* 
   LDB nsswitch module

   Copyright (C) Simo Sorce 2006
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ldb-nss.h"

struct _ldb_nss_context *_ldb_nss_ctx = NULL;

NSS_STATUS _ldb_nss_init(void)
{
	int ret;

	pid_t mypid = getpid();

	if (_ldb_nss_ctx != NULL) {
		if (_ldb_nss_ctx->pid == mypid) {
			/* already initialized */
			return NSS_STATUS_SUCCESS;
		} else {
			/* we are in a forked child now, reinitialize */
			talloc_free(_ldb_nss_ctx);
			_ldb_nss_ctx = NULL;
		}
	}
		
	_ldb_nss_ctx = talloc_named(NULL, 0, "_ldb_nss_ctx(%u)", mypid);
	if (_ldb_nss_ctx == NULL) {
		return NSS_STATUS_UNAVAIL;
	}

	_ldb_nss_ctx->pid = mypid;

	_ldb_nss_ctx->ldb = ldb_init(_ldb_nss_ctx);
	if (_ldb_nss_ctx->ldb == NULL) {
		goto failed;
	}

	ret = ldb_connect(_ldb_nss_ctx->ldb, _LDB_NSS_URL, LDB_FLG_RDONLY, NULL);
	if (ret != LDB_SUCCESS) {
		goto failed;
	}

	_ldb_nss_ctx->base = ldb_dn_new(_ldb_nss_ctx, _ldb_nss_ctx->ldb, _LDB_NSS_BASEDN);
	if ( ! ldb_dn_validate(_ldb_nss_ctx->base)) {
		goto failed;
	}

	_ldb_nss_ctx->pw_cur = 0;
	_ldb_nss_ctx->pw_res = NULL;
	_ldb_nss_ctx->gr_cur = 0;
	_ldb_nss_ctx->gr_res = NULL;

	return NSS_STATUS_SUCCESS;

failed:
	/* talloc_free(_ldb_nss_ctx); */
	_ldb_nss_ctx = NULL;
	return NSS_STATUS_UNAVAIL;
}

NSS_STATUS _ldb_nss_fill_passwd(struct passwd *result,
				char *buffer,
				int buflen,
				int *errnop,
				struct ldb_message *msg)
{
	int len;
	int bufpos;
	const char *tmp;

	bufpos = 0;

	/* get username */
	tmp = ldb_msg_find_attr_as_string(msg, "uid", NULL);
	if (tmp == NULL) {
		/* this is a fatal error */
		*errnop = errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->pw_name = &buffer[bufpos];
	bufpos += len;

	/* get userPassword */
	tmp = ldb_msg_find_attr_as_string(msg, "userPassword", NULL);
	if (tmp == NULL) {
		tmp = "LDB";
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->pw_passwd = &buffer[bufpos];
	bufpos += len;

	/* this backend never serves an uid 0 user */
	result->pw_uid = ldb_msg_find_attr_as_int(msg, "uidNumber", 0);
	if (result->pw_uid == 0) {
		/* this is a fatal error */
		*errnop = errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}

	result->pw_gid = ldb_msg_find_attr_as_int(msg, "gidNumber", 0);
	if (result->pw_gid == 0) {
		/* this is a fatal error */
		*errnop = errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}

	/* get gecos */
	tmp = ldb_msg_find_attr_as_string(msg, "gecos", NULL);
	if (tmp == NULL) {
		tmp = "";
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->pw_gecos = &buffer[bufpos];
	bufpos += len;

	/* get homeDirectory */
	tmp = ldb_msg_find_attr_as_string(msg, "homeDirectory", NULL);
	if (tmp == NULL) {
		tmp = "";
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->pw_dir = &buffer[bufpos];
	bufpos += len;

	/* get shell */
	tmp = ldb_msg_find_attr_as_string(msg, "loginShell", NULL);
	if (tmp == NULL) {
		tmp = "";
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->pw_shell = &buffer[bufpos];
	bufpos += len;

	return NSS_STATUS_SUCCESS;
}

NSS_STATUS _ldb_nss_fill_group(struct group *result,
				char *buffer,
				int buflen,
				int *errnop,
				struct ldb_message *group,
				struct ldb_result *members)
{
	const char *tmp;
	size_t len;
	size_t bufpos;
	size_t lsize;
	int i;

	bufpos = 0;

	/* get group name */
	tmp = ldb_msg_find_attr_as_string(group, "cn", NULL);
	if (tmp == NULL) {
		/* this is a fatal error */
		*errnop = errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->gr_name = &buffer[bufpos];
	bufpos += len;

	/* get userPassword */
	tmp = ldb_msg_find_attr_as_string(group, "userPassword", NULL);
	if (tmp == NULL) {
		tmp = "LDB";
	}
	len = strlen(tmp)+1;
	if (bufpos + len > buflen) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	}
	memcpy(&buffer[bufpos], tmp, len);
	result->gr_passwd = &buffer[bufpos];
	bufpos += len;

	result->gr_gid = ldb_msg_find_attr_as_int(group, "gidNumber", 0);
	if (result->gr_gid == 0) {
		/* this is a fatal error */
		*errnop = errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}

	/* check if there is enough memory for the list of pointers */
	lsize = (members->count + 1) * sizeof(char *);

	/* align buffer on pointer boundary */
	bufpos += (sizeof(char*) - ((unsigned long)(buffer) % sizeof(char*)));
	if ((buflen - bufpos) < lsize) {
		/* buffer too small */
		*errnop = errno = EAGAIN;
		return NSS_STATUS_TRYAGAIN;
	} 

	result->gr_mem = (char **)&buffer[bufpos];
	bufpos += lsize;

	for (i = 0; i < members->count; i++) {
		tmp = ldb_msg_find_attr_as_string(members->msgs[i], "uid", NULL);
		if (tmp == NULL) {
			/* this is a fatal error */
			*errnop = errno = ENOENT;
			return NSS_STATUS_UNAVAIL;
		}
		len = strlen(tmp)+1;
		if (bufpos + len > buflen) {
			/* buffer too small */
			*errnop = errno = EAGAIN;
			return NSS_STATUS_TRYAGAIN;
		}
		memcpy(&buffer[bufpos], tmp, len);
		result->gr_mem[i] = &buffer[bufpos];
		bufpos += len;
	}

	result->gr_mem[i] = NULL;

	return NSS_STATUS_SUCCESS;
}

NSS_STATUS _ldb_nss_fill_initgr(gid_t group,
				long int limit,
				long int *start,
				long int *size,
				gid_t **groups,
				int *errnop,
				struct ldb_result *grlist)
{
	NSS_STATUS ret;
	int i;

	for (i = 0; i < grlist->count; i++) {

		if (limit && (*start > limit)) {
			/* TODO: warn no all groups were reported */
			*errnop = 0;
			ret = NSS_STATUS_SUCCESS;
			goto done;
		}

		if (*start == *size) {
			/* buffer full, enlarge it */
			long int gs;
			gid_t *gm;

			gs = (*size) + 32;
			if (limit && (gs > limit)) {
				gs = limit;
			}

			gm = (gid_t *)realloc((*groups), gs * sizeof(gid_t));
			if ( ! gm) {
				*errnop = ENOMEM;
				ret = NSS_STATUS_UNAVAIL;
				goto done;
			}

			*groups = gm;
			*size = gs;
		}

		(*groups)[*start] = ldb_msg_find_attr_as_int(grlist->msgs[i], "gidNumber", 0);
		if ((*groups)[*start] == 0 || (*groups)[*start] == group) {
			/* skip root group or primary group */
			continue;
		}
		(*start)++;

	}

	*errnop = 0;
	ret = NSS_STATUS_SUCCESS;
done:
	return ret;
}

#define _LDB_NSS_ALLOC_CHECK(mem) do { if (!mem) { errno = ENOMEM; return NSS_STATUS_UNAVAIL; } } while(0)

NSS_STATUS _ldb_nss_group_request(struct ldb_result **_res,
					struct ldb_dn *group_dn,
					const char * const *attrs,
					const char *mattr)
{
	struct ldb_control **ctrls;
	struct ldb_control *ctrl;
	struct ldb_asq_control *asqc;
	struct ldb_request *req;
	int ret;
	struct ldb_result *res = *_res;

	ctrls = talloc_array(res, struct ldb_control *, 2);
	_LDB_NSS_ALLOC_CHECK(ctrls);

	ctrl = talloc(ctrls, struct ldb_control);
	_LDB_NSS_ALLOC_CHECK(ctrl);

	asqc = talloc(ctrl, struct ldb_asq_control);
	_LDB_NSS_ALLOC_CHECK(asqc);

	asqc->source_attribute = talloc_strdup(asqc, mattr);
	_LDB_NSS_ALLOC_CHECK(asqc->source_attribute);

	asqc->request = 1;
	asqc->src_attr_len = strlen(asqc->source_attribute);
	ctrl->oid = LDB_CONTROL_ASQ_OID;
	ctrl->critical = 1;
	ctrl->data = asqc;
	ctrls[0] = ctrl;
	ctrls[1] = NULL;

	ret = ldb_build_search_req(
				&req,
				_ldb_nss_ctx->ldb,
				res,
				group_dn,
				LDB_SCOPE_BASE,
				"(objectClass=*)",
				attrs,
				ctrls,
				res,
				ldb_search_default_callback);
	
	if (ret != LDB_SUCCESS) {
		errno = ENOENT;
		return NSS_STATUS_UNAVAIL;
	}

	ldb_set_timeout(_ldb_nss_ctx->ldb, req, 0);

	ret = ldb_request(_ldb_nss_ctx->ldb, req);

	if (ret == LDB_SUCCESS) {
		ret = ldb_wait(req->handle, LDB_WAIT_ALL);
	} else {
		talloc_free(req);
		return NSS_STATUS_UNAVAIL;
	}

	talloc_free(req);
	return NSS_STATUS_SUCCESS;
}