summaryrefslogtreecommitdiff
path: root/source3/sam/idmap_smbldap.c
blob: b1aae2b86f7875426600764b26182ff5f2555752 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
   Unix SMB/CIFS implementation.

   idmap LDAP backend

   Copyright (C) Tim Potter 		2000
   Copyright (C) Jim McDonough <jmcd@us.ibm.com>	2003
   Copyright (C) Simo Sorce 		2003
   Copyright (C) Gerald Carter 		2003

   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_IDMAP

struct ldap_connection *ldap_conn = NULL;

/* number tries while allocating new id */
#define LDAP_MAX_ALLOC_ID 128


/***********************************************************************
 This function cannot be called to modify a mapping, only set a new one
***********************************************************************/

static NTSTATUS ldap_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
{
	NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
	pstring id_str;
	const char *type;
	fstring sid_string;
	struct ldap_message *msg;
	struct ldap_message *mod_res = NULL;
	char *mod;

	type = (id_type & ID_USERID) ? "uidNumber" : "gidNumber";

	sid_to_string( sid_string, sid );

	pstr_sprintf(id_str, "%lu",
		     ((id_type & ID_USERID) ?
		      (unsigned long)id.uid : (unsigned long)id.gid));

	asprintf(&mod,
		 "dn: sambaSID=%s,%s\n"
		 "changetype: add\n"
		 "objectClass: sambaIdmapEntry\n"
		 "objectClass: sambaSidEntry\n"
		 "sambaSID: %s\n"
		 "%s: %lu\n",
		 sid_string, lp_ldap_idmap_suffix(), sid_string, type,
		 ((id_type & ID_USERID) ?
		  (unsigned long)id.uid : (unsigned long)id.gid));

	msg = ldap_ldif2msg(mod);

	SAFE_FREE(mod);

	if (msg == NULL)
		return NT_STATUS_NO_MEMORY;

	mod_res = ldap_transaction(ldap_conn, msg);

	if ((mod_res == NULL) || (mod_res->r.ModifyResponse.resultcode != 0))
		goto out;

	ret = NT_STATUS_OK;
 out:
	destroy_ldap_message(msg);
	destroy_ldap_message(mod_res);
	return ret;
}

/*****************************************************************************
 Allocate a new RID
*****************************************************************************/

static NTSTATUS ldap_allocate_rid(uint32 *rid, int rid_type)
{
	return NT_STATUS_UNSUCCESSFUL;
}

/*****************************************************************************
 Allocate a new uid or gid
*****************************************************************************/

static NTSTATUS ldap_allocate_id(unid_t *id, int id_type)
{
	NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
	uid_t	luid, huid;
	gid_t	lgid, hgid;
	const char *attrs[] = { "uidNumber", "gidNumber" };
	struct ldap_message *idpool_s = NULL;
	struct ldap_message *idpool = NULL;
	struct ldap_message *mod_msg = NULL;
	struct ldap_message *mod_res = NULL;
	int value;
	const char *id_attrib;
	char *mod;

	id_attrib = (id_type & ID_USERID) ? "uidNumber" : "gidNumber";

	idpool_s = new_ldap_search_message(lp_ldap_suffix(),
					   LDAP_SEARCH_SCOPE_SUB,
					   "(objectclass=sambaUnixIdPool)",
					   2, attrs);

	if (idpool_s == NULL)
		return NT_STATUS_NO_MEMORY;

	idpool = ldap_searchone(ldap_conn, idpool_s, NULL);

	if (idpool == NULL)
		goto out;

	if (!ldap_find_single_int(idpool, id_attrib, &value))
		goto out;

	/* this must succeed or else we wouldn't have initialized */
		
	lp_idmap_uid( &luid, &huid);
	lp_idmap_gid( &lgid, &hgid);
	
	/* make sure we still have room to grow */
	
	if (id_type & ID_USERID) {
		id->uid = value;
		if (id->uid > huid ) {
			DEBUG(0,("ldap_allocate_id: Cannot allocate uid "
				 "above %lu!\n",  (unsigned long)huid));
			goto out;
		}
	}
	else { 
		id->gid = value;
		if (id->gid > hgid ) {
			DEBUG(0,("ldap_allocate_id: Cannot allocate gid "
				 "above %lu!\n", (unsigned long)hgid));
			goto out;
		}
	}
	
	asprintf(&mod,
		 "dn: %s\n"
		 "changetype: modify\n"
		 "delete: %s\n"
		 "%s: %d\n"
		 "-\n"
		 "add: %s\n"
		 "%s: %d\n",
		 idpool->r.SearchResultEntry.dn, id_attrib, id_attrib, value,
		 id_attrib, id_attrib, value+1);

	mod_msg = ldap_ldif2msg(mod);

	SAFE_FREE(mod);

	if (mod_msg == NULL)
		goto out;

	mod_res = ldap_transaction(ldap_conn, mod_msg);

	if ((mod_res == NULL) || (mod_res->r.ModifyResponse.resultcode != 0))
		goto out;

	ret = NT_STATUS_OK;
out:
	destroy_ldap_message(idpool_s);
	destroy_ldap_message(idpool);
	destroy_ldap_message(mod_msg);
	destroy_ldap_message(mod_res);

	return ret;
}

/*****************************************************************************
 get a sid from an id
*****************************************************************************/

static NTSTATUS ldap_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
{
	pstring filter;
	const char *type;
	NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
	const char *attr_list[] = { "sambaSID" };
	struct ldap_message *msg;
	struct ldap_message *entry = NULL;
	char *sid_str;

	type = (id_type & ID_USERID) ? "uidNumber" : "gidNumber";

	pstr_sprintf(filter, "(&(objectClass=%s)(%s=%lu))", "sambaIdmapEntry",
		     type,
		     ((id_type & ID_USERID) ?
		      (unsigned long)id.uid : (unsigned long)id.gid));

	msg = new_ldap_search_message(lp_ldap_idmap_suffix(),
				      LDAP_SEARCH_SCOPE_SUB,
				      filter, 1, attr_list);

	if (msg == NULL)
		return NT_STATUS_NO_MEMORY;

	entry = ldap_searchone(ldap_conn, msg, NULL);

	if (entry == NULL)
		goto out;

	if (!ldap_find_single_string(entry, "sambaSID", entry->mem_ctx,
				     &sid_str))
		goto out;

	if (!string_to_sid(sid, sid_str))
		goto out;

	ret = NT_STATUS_OK;
out:
	destroy_ldap_message(msg);
	destroy_ldap_message(entry);

	return ret;
}

/***********************************************************************
 Get an id from a sid 
***********************************************************************/

static NTSTATUS ldap_get_id_from_sid(unid_t *id, int *id_type,
				     const DOM_SID *sid)
{
	pstring filter;
	const char *type;
	NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
	struct ldap_message *msg;
	struct ldap_message *entry = NULL;
	int i;

	DEBUG(8,("ldap_get_id_from_sid: %s (%s)\n", sid_string_static(sid),
		(*id_type & ID_GROUPID ? "group" : "user") ));

	type = ((*id_type) & ID_USERID) ? "uidNumber" : "gidNumber";

	pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))", 
		     "sambaIdmapEntry", "sambaSID", sid_string_static(sid));

	msg = new_ldap_search_message(lp_ldap_idmap_suffix(),
				      LDAP_SEARCH_SCOPE_SUB,
				      filter, 1, &type);

	if (msg == NULL)
		return NT_STATUS_NO_MEMORY;

	entry = ldap_searchone(ldap_conn, msg, NULL);

	if (entry != NULL) {
		int value;

		if (!ldap_find_single_int(entry, type, &value))
			goto out;

		if ((*id_type) & ID_USERID)
			id->uid = value;
		else
			id->gid = value;

		ret = NT_STATUS_OK;
		goto out;
	}

	if ((*id_type) & ID_QUERY_ONLY)
		goto out;

	/* Allocate a new RID */

	for (i = 0; i < LDAP_MAX_ALLOC_ID; i++) {
		ret = ldap_allocate_id(id, *id_type);
		if ( NT_STATUS_IS_OK(ret) )
			break;
	}
		
	if ( !NT_STATUS_IS_OK(ret) ) {
		DEBUG(0,("Could not allocate id\n"));
		goto out;
	}

	DEBUG(10,("ldap_get_id_from_sid: Allocated new %cid [%ul]\n",
		  (*id_type & ID_GROUPID ? 'g' : 'u'), (uint32)id->uid ));

	ret = ldap_set_mapping(sid, *id, *id_type);

out:
	destroy_ldap_message(msg);
	destroy_ldap_message(entry);

	return ret;
}

/**********************************************************************
 Verify the sambaUnixIdPool entry in the directory.  
**********************************************************************/
static NTSTATUS verify_idpool(void)
{
	const char *attr_list[3] = { "uidnumber", "gidnumber", "objectclass" };
	BOOL result;
	char *mod;
	struct ldap_message *msg, *entry, *res;

	uid_t	luid, huid;
	gid_t	lgid, hgid;

	msg = new_ldap_search_message(lp_ldap_suffix(),
				      LDAP_SEARCH_SCOPE_SUB,
				      "(objectClass=sambaUnixIdPool)",
				      3, attr_list);

	if (msg == NULL)
		return NT_STATUS_NO_MEMORY;

	entry = ldap_searchone(ldap_conn, msg, NULL);

	result = (entry != NULL);

	destroy_ldap_message(msg);
	destroy_ldap_message(entry);

	if (result)
		return NT_STATUS_OK;

	if ( !lp_idmap_uid(&luid, &huid) || !lp_idmap_gid( &lgid, &hgid ) ) {
		DEBUG(3,("ldap_idmap_init: idmap uid/gid parameters not "
			 "specified\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	asprintf(&mod,
		 "dn: %s\n"
		 "changetype: modify\n"
		 "add: objectClass\n"
		 "objectClass: sambaUnixIdPool\n"
		 "-\n"
		 "add: uidNumber\n"
		 "uidNumber: %lu\n"
		 "-\n"
		 "add: gidNumber\n"
		 "gidNumber: %lu\n",
		 lp_ldap_idmap_suffix(),
		 (unsigned long)luid, (unsigned long)lgid);
		 
	msg = ldap_ldif2msg(mod);

	SAFE_FREE(mod);

	if (msg == NULL)
		return NT_STATUS_NO_MEMORY;

	res = ldap_transaction(ldap_conn, msg);

	if ((res == NULL) || (res->r.ModifyResponse.resultcode != 0)) {
		destroy_ldap_message(msg);
		destroy_ldap_message(res);
		DEBUG(5, ("Could not add sambaUnixIdPool\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	destroy_ldap_message(msg);
	destroy_ldap_message(res);
	return NT_STATUS_OK;
}

/*****************************************************************************
 Initialise idmap database. 
*****************************************************************************/

static NTSTATUS ldap_idmap_init( char *params )
{
	NTSTATUS nt_status;
	char *dn, *pw;

	ldap_conn = new_ldap_connection();

	if (!fetch_ldap_pw(&dn, &pw))
		return NT_STATUS_UNSUCCESSFUL;

	ldap_conn->auth_dn = talloc_strdup(ldap_conn->mem_ctx, dn);
	ldap_conn->simple_pw = talloc_strdup(ldap_conn->mem_ctx, pw);

	SAFE_FREE(dn);
	SAFE_FREE(pw);

	if (!ldap_setup_connection(ldap_conn, params, NULL, NULL))
		return NT_STATUS_UNSUCCESSFUL;

	/* see if the idmap suffix and sub entries exists */
	
	nt_status = verify_idpool();	
	if ( !NT_STATUS_IS_OK(nt_status) )
		return nt_status;
		
	return NT_STATUS_OK;
}

/*****************************************************************************
 End the LDAP session
*****************************************************************************/

static NTSTATUS ldap_idmap_close(void)
{

	DEBUG(5,("The connection to the LDAP server was closed\n"));
	/* maybe free the results here --metze */
	
	return NT_STATUS_OK;
}


/* This function doesn't make as much sense in an LDAP world since the calling
   node doesn't really control the ID ranges */
static void ldap_idmap_status(void)
{
	DEBUG(0, ("LDAP IDMAP Status not available\n"));
}

static struct idmap_methods ldap_methods = {
	ldap_idmap_init,
	ldap_allocate_rid,
	ldap_allocate_id,
	ldap_get_sid_from_id,
	ldap_get_id_from_sid,
	ldap_set_mapping,
	ldap_idmap_close,
	ldap_idmap_status

};

NTSTATUS idmap_smbldap_init(void)
{
	return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "smbldap", &ldap_methods);
}