r10392: Merges from SAMBA_3_0 for 3.0.20a
[Samba.git] / source / sam / idmap_rid.c
blob0ba97946965ce65d01554d22cfba56f7449f08bc
1 /*
2 * idmap_rid: static map between Active Directory/NT RIDs and RFC 2307 accounts
3 * Copyright (C) Guenther Deschner, 2004
4 * Copyright (C) Sumit Bose, 2004
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
27 NTSTATUS init_module(void);
29 struct dom_entry {
30 fstring name;
31 fstring sid;
32 uint32 min_id;
33 uint32 max_id;
36 typedef struct trust_dom_array {
37 int number;
38 struct dom_entry *dom;
39 } trust_dom_array;
41 static trust_dom_array trust;
43 static NTSTATUS rid_idmap_parse(const char *init_param,
44 uint32 num_domains,
45 fstring *domain_names,
46 DOM_SID *domain_sids,
47 uid_t u_low,
48 uid_t u_high)
50 const char *p;
51 int i;
52 fstring sid_str;
53 BOOL known_domain = False;
54 fstring tok;
56 p = init_param;
57 trust.number = 0;
59 /* falling back to automatic mapping when there were no options given */
60 if (!*init_param) {
62 DEBUG(3,("rid_idmap_parse: no domain list given or trusted domain-support deactivated, falling back to automatic mapping for own domain:\n"));
64 sid_to_string(sid_str, &domain_sids[0]);
66 fstrcpy(trust.dom[0].name, domain_names[0]);
67 fstrcpy(trust.dom[0].sid, sid_str);
68 trust.dom[0].min_id = u_low;
69 trust.dom[0].max_id = u_high;
70 trust.number = 1;
72 DEBUGADD(3,("rid_idmap_parse:\tdomain: [%s], sid: [%s], range=[%d-%d]\n",
73 trust.dom[0].name, trust.dom[0].sid, trust.dom[0].min_id, trust.dom[0].max_id));
74 return NT_STATUS_OK;
77 /* scan through the init_param-list */
78 while (next_token(&init_param, tok, LIST_SEP, sizeof(tok))) {
80 p = tok;
81 DEBUG(3,("rid_idmap_parse: parsing entry: %d\n", trust.number));
83 /* reinit sizes */
84 trust.dom = SMB_REALLOC_ARRAY(trust.dom, struct dom_entry,
85 trust.number+1);
87 if ( trust.dom == NULL ) {
88 return NT_STATUS_NO_MEMORY;
91 if (!next_token(&p, tok, "=", sizeof(tok))) {
92 DEBUG(0, ("rid_idmap_parse: no '=' sign found in domain list [%s]\n", init_param));
93 return NT_STATUS_UNSUCCESSFUL;
96 /* add the name */
97 fstrcpy(trust.dom[trust.number].name, tok);
98 DEBUGADD(3,("rid_idmap_parse:\tentry %d has name: [%s]\n", trust.number, trust.dom[trust.number].name));
100 /* add the domain-sid */
101 for (i=0; i<num_domains; i++) {
103 known_domain = False;
105 if (strequal(domain_names[i], trust.dom[trust.number].name)) {
107 sid_to_string(sid_str, &domain_sids[i]);
108 fstrcpy(trust.dom[trust.number].sid, sid_str);
110 DEBUGADD(3,("rid_idmap_parse:\tentry %d has sid: [%s]\n", trust.number, trust.dom[trust.number].sid));
111 known_domain = True;
112 break;
116 if (!known_domain) {
117 DEBUG(0,("rid_idmap_parse: your DC does not know anything about domain: [%s]\n", trust.dom[trust.number].name));
118 return NT_STATUS_INVALID_PARAMETER;
121 if (!next_token(&p, tok, "-", sizeof(tok))) {
122 DEBUG(0,("rid_idmap_parse: no mapping-range defined\n"));
123 return NT_STATUS_INVALID_PARAMETER;
126 /* add min_id */
127 trust.dom[trust.number].min_id = atoi(tok);
128 DEBUGADD(3,("rid_idmap_parse:\tentry %d has min_id: [%d]\n", trust.number, trust.dom[trust.number].min_id));
130 /* add max_id */
131 trust.dom[trust.number].max_id = atoi(p);
132 DEBUGADD(3,("rid_idmap_parse:\tentry %d has max_id: [%d]\n", trust.number, trust.dom[trust.number].max_id));
134 trust.number++;
137 return NT_STATUS_OK;
141 static NTSTATUS rid_idmap_get_domains(uint32 *num_domains, fstring **domain_names, DOM_SID **domain_sids)
143 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
144 struct cli_state *cli;
145 TALLOC_CTX *mem_ctx;
146 POLICY_HND pol;
147 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
148 fstring dc_name;
149 struct in_addr dc_ip;
150 const char *password = NULL;
151 const char *username = NULL;
152 const char *domain = NULL;
153 uint32 info_class = 5;
154 char *domain_name = NULL;
155 DOM_SID *domain_sid, sid;
156 fstring sid_str;
157 int i;
158 uint32 trusted_num_domains = 0;
159 char **trusted_domain_names;
160 DOM_SID *trusted_domain_sids;
161 uint32 enum_ctx = 0;
162 int own_domains = 2;
164 /* put the results together */
165 *num_domains = 2;
166 *domain_names = SMB_MALLOC_ARRAY(fstring, *num_domains);
167 *domain_sids = SMB_MALLOC_ARRAY(DOM_SID, *num_domains);
169 /* avoid calling a DC when trusted domains are not allowed anyway */
170 if (!lp_allow_trusted_domains()) {
172 fstrcpy((*domain_names)[0], lp_workgroup());
173 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
174 DEBUG(0,("rid_idmap_get_domains: failed to retrieve domain sid\n"));
175 return status;
177 sid_copy(&(*domain_sids)[0], &sid);
179 /* add BUILTIN */
180 fstrcpy((*domain_names)[1], "BUILTIN");
181 sid_copy(&(*domain_sids)[1], &global_sid_Builtin);
183 return NT_STATUS_OK;
186 /* create mem_ctx */
187 if (!(mem_ctx = talloc_init("rid_idmap_get_trusted_domains"))) {
188 DEBUG(0, ("rid_idmap_get_domains: talloc_init() failed\n"));
189 return NT_STATUS_NO_MEMORY;
192 if (!get_dc_name(lp_workgroup(), 0, dc_name, &dc_ip)) {
193 DEBUG(1, ("rid_idmap_get_domains: could not get dc-name\n"));
194 return NT_STATUS_UNSUCCESSFUL;
197 /* open a connection to the dc */
198 username = secrets_fetch(SECRETS_AUTH_USER, NULL);
199 password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
200 domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
202 if (username) {
204 if (!domain)
205 domain = smb_xstrdup(lp_workgroup());
207 if (!password)
208 password = smb_xstrdup("");
210 DEBUG(3, ("rid_idmap_get_domains: IPC$ connections done by user %s\\%s\n", domain, username));
212 } else {
214 DEBUG(3, ("rid_idmap_get_domains: IPC$ connections done anonymously\n"));
215 username = "";
216 domain = "";
217 password = "";
220 DEBUG(10, ("rid_idmap_get_domains: opening connection to [%s]\n", dc_name));
222 status = cli_full_connection(&cli, global_myname(), dc_name,
223 NULL, 0,
224 "IPC$", "IPC",
225 username,
226 lp_workgroup(),
227 password,
228 CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK, True, NULL);
230 if (!NT_STATUS_IS_OK(status)) {
231 DEBUG(1, ("rid_idmap_get_domains: could not setup connection to dc\n"));
232 return status;
235 /* query the lsa-pipe */
236 if (!cli_nt_session_open (cli, PI_LSARPC)) {
237 DEBUG(1, ("rid_idmap_get_domains: could not setup connection to dc\n"));
238 goto out;
241 /* query policies */
242 status = cli_lsa_open_policy(cli, mem_ctx, False, des_access, &pol);
243 if (!NT_STATUS_IS_OK(status)) {
244 goto out;
247 status = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, &domain_name, &domain_sid);
248 if (!NT_STATUS_IS_OK(status)) {
249 DEBUG(1, ("rid_idmap_get_domains: cannot retrieve domain-info\n"));
250 goto out;
253 sid_to_string(sid_str, domain_sid);
254 DEBUG(10,("rid_idmap_get_domains: my domain: [%s], sid: [%s]\n", domain_name, sid_str));
256 /* scan trusted domains */
257 DEBUG(10, ("rid_idmap_get_domains: enumerating trusted domains\n"));
258 status = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
259 &trusted_num_domains,
260 &trusted_domain_names,
261 &trusted_domain_sids);
263 if (!NT_STATUS_IS_OK(status) &&
264 !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) &&
265 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
266 DEBUG(1, ("rid_idmap_get_domains: could not enumerate trusted domains\n"));
267 goto out;
270 /* show trusted domains */
271 DEBUG(10,("rid_idmap_get_domains: scan for trusted domains gave %d results:\n", trusted_num_domains));
272 for (i=0; i<trusted_num_domains; i++) {
273 sid_to_string(sid_str, &trusted_domain_sids[i]);
274 DEBUGADD(10,("rid_idmap_get_domains:\t#%d\tDOMAIN: [%s], SID: [%s]\n",
275 i, trusted_domain_names[i], sid_str));
278 if (!sid_equal(domain_sid, get_global_sam_sid()))
279 ++own_domains;
281 /* put the results together */
282 *num_domains = trusted_num_domains + own_domains;
283 *domain_names = SMB_REALLOC_ARRAY(*domain_names, fstring,
284 *num_domains);
285 *domain_sids = SMB_REALLOC_ARRAY(*domain_sids, DOM_SID, *num_domains);
287 /* first add mydomain */
288 fstrcpy((*domain_names)[0], domain_name);
289 sid_copy(&(*domain_sids)[0], domain_sid);
291 /* then add BUILTIN */
292 fstrcpy((*domain_names)[1], "BUILTIN");
293 sid_copy(&(*domain_sids)[1], &global_sid_Builtin);
295 /* then add my local sid */
296 if (!sid_equal(domain_sid, get_global_sam_sid())) {
297 fstrcpy((*domain_names)[2], global_myname());
298 sid_copy(&(*domain_sids)[2], get_global_sam_sid());
301 /* add trusted domains */
302 for (i=0; i<trusted_num_domains; i++) {
303 fstrcpy((*domain_names)[i+own_domains], trusted_domain_names[i]);
304 sid_copy(&((*domain_sids)[i+own_domains]), &(trusted_domain_sids[i]));
307 /* show complete domain list */
308 DEBUG(5,("rid_idmap_get_domains: complete domain-list has %d entries:\n", *num_domains));
309 for (i=0; i<*num_domains; i++) {
310 sid_to_string(sid_str, &((*domain_sids)[i]));
311 DEBUGADD(5,("rid_idmap_get_domains:\t#%d\tdomain: [%s], sid: [%s]\n",
312 i, (*domain_names)[i], sid_str ));
315 status = NT_STATUS_OK;
317 out:
318 cli_lsa_close(cli, mem_ctx, &pol);
319 cli_nt_session_close(cli);
320 talloc_destroy(mem_ctx);
321 cli_shutdown(cli);
323 return status;
326 static NTSTATUS rid_idmap_init(char *init_param)
328 int i, j;
329 uid_t u_low, u_high;
330 gid_t g_low, g_high;
331 uint32 num_domains = 0;
332 fstring *domain_names;
333 DOM_SID *domain_sids;
334 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
335 trust.dom = NULL;
337 /* basic sanity checks */
338 if (!lp_idmap_uid(&u_low, &u_high) || !lp_idmap_gid(&g_low, &g_high)) {
339 DEBUG(0, ("rid_idmap_init: cannot get required global idmap-ranges.\n"));
340 return nt_status;
343 if (u_low != g_low || u_high != g_high) {
344 DEBUG(0, ("rid_idmap_init: range defined in \"idmap uid\" must match range of \"idmap gid\".\n"));
345 return nt_status;
348 if (lp_allow_trusted_domains()) {
349 #if IDMAP_RID_SUPPORT_TRUSTED_DOMAINS
350 DEBUG(3,("rid_idmap_init: enabling trusted-domain-mapping\n"));
351 #else
352 DEBUG(0,("rid_idmap_init: idmap_rid does not work with trusted domains\n"));
353 DEBUGADD(0,("rid_idmap_init: please set \"allow trusted domains\" to \"no\" when using idmap_rid\n"));
354 return nt_status;
355 #endif
358 /* init sizes */
359 trust.dom = SMB_MALLOC_P(struct dom_entry);
360 if (trust.dom == NULL) {
361 return NT_STATUS_NO_MEMORY;
364 /* retrieve full domain list */
365 nt_status = rid_idmap_get_domains(&num_domains, &domain_names, &domain_sids);
366 if (!NT_STATUS_IS_OK(nt_status) &&
367 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES) &&
368 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES)) {
369 DEBUG(0, ("rid_idmap_init: cannot fetch sids for domain and/or trusted-domains from domain-controller.\n"));
370 return nt_status;
373 /* parse the init string */
374 nt_status = rid_idmap_parse(init_param, num_domains, domain_names, domain_sids, u_low, u_high);
375 if (!NT_STATUS_IS_OK(nt_status)) {
376 DEBUG(0, ("rid_idmap_init: cannot parse module-configuration\n"));
377 goto out;
380 nt_status = NT_STATUS_INVALID_PARAMETER;
382 /* some basic sanity checks */
383 for (i=0; i<trust.number; i++) {
385 if (trust.dom[i].min_id > trust.dom[i].max_id) {
386 DEBUG(0, ("rid_idmap_init: min_id (%d) has to be smaller than max_id (%d) for domain [%s]\n",
387 trust.dom[i].min_id, trust.dom[i].max_id, trust.dom[i].name));
388 goto out;
391 if (trust.dom[i].min_id < u_low || trust.dom[i].max_id > u_high) {
392 DEBUG(0, ("rid_idmap_init: mapping of domain [%s] (%d-%d) has to fit into global idmap range (%d-%d).\n",
393 trust.dom[i].name, trust.dom[i].min_id, trust.dom[i].max_id, u_low, u_high));
394 goto out;
398 /* check for overlaps */
399 for (i=0; i<trust.number-1; i++) {
400 for (j=i+1; j<trust.number; j++) {
401 if (trust.dom[i].min_id <= trust.dom[j].max_id && trust.dom[j].min_id <= trust.dom[i].max_id) {
402 DEBUG(0, ("rid_idmap_init: the ranges of domain [%s] and [%s] overlap\n",
403 trust.dom[i+1].name, trust.dom[i].name));
404 goto out;
409 DEBUG(3, ("rid_idmap_init: using %d mappings:\n", trust.number));
410 for (i=0; i<trust.number; i++) {
411 DEBUGADD(3, ("rid_idmap_init:\tdomain: [%s], sid: [%s], min_id: [%d], max_id: [%d]\n",
412 trust.dom[i].name, trust.dom[i].sid, trust.dom[i].min_id, trust.dom[i].max_id));
415 nt_status = NT_STATUS_OK;
417 out:
418 SAFE_FREE(domain_names);
419 SAFE_FREE(domain_sids);
421 return nt_status;
424 static NTSTATUS rid_idmap_get_sid_from_id(DOM_SID *sid, unid_t unid, int id_type)
426 fstring sid_string;
427 int i;
428 DOM_SID sidstr;
430 /* find range */
431 for (i=0; i<trust.number; i++) {
432 if (trust.dom[i].min_id <= unid.uid && trust.dom[i].max_id >= unid.uid )
433 break;
436 if (i == trust.number) {
437 DEBUG(0,("rid_idmap_get_sid_from_id: no suitable range available for id: %d\n", unid.uid));
438 return NT_STATUS_INVALID_PARAMETER;
441 /* use lower-end of idmap-range as offset for users and groups*/
442 unid.uid -= trust.dom[i].min_id;
444 if (!trust.dom[i].sid)
445 return NT_STATUS_INVALID_PARAMETER;
447 string_to_sid(&sidstr, trust.dom[i].sid);
448 sid_copy(sid, &sidstr);
449 if (!sid_append_rid( sid, (unsigned long)unid.uid )) {
450 DEBUG(0,("rid_idmap_get_sid_from_id: could not append rid to domain sid\n"));
451 return NT_STATUS_NO_MEMORY;
454 DEBUG(3, ("rid_idmap_get_sid_from_id: mapped POSIX %s %d to SID [%s]\n",
455 (id_type == ID_GROUPID) ? "GID" : "UID", unid.uid,
456 sid_to_string(sid_string, sid)));
458 return NT_STATUS_OK;
461 static NTSTATUS rid_idmap_get_id_from_sid(unid_t *unid, int *id_type, const DOM_SID *sid)
463 fstring sid_string;
464 int i;
465 uint32 rid;
466 DOM_SID sidstr;
468 /* check if we have a mapping for the sid */
469 for (i=0; i<trust.number; i++) {
470 if (!trust.dom[i].sid) {
471 return NT_STATUS_INVALID_PARAMETER;
473 string_to_sid(&sidstr, trust.dom[i].sid);
474 if ( sid_compare_domain(sid, &sidstr) == 0 )
475 break;
478 if (i == trust.number) {
479 DEBUG(0,("rid_idmap_get_id_from_sid: no suitable range available for sid: %s\n",
480 sid_string_static(sid)));
481 return NT_STATUS_INVALID_PARAMETER;
484 if (!sid_peek_rid(sid, &rid)) {
485 DEBUG(0,("rid_idmap_get_id_from_sid: could not peek rid\n"));
486 return NT_STATUS_INVALID_PARAMETER;
489 /* use lower-end of idmap-range as offset for users and groups */
490 unid->uid = rid + trust.dom[i].min_id;
492 if (unid->uid > trust.dom[i].max_id) {
493 DEBUG(0,("rid_idmap_get_id_from_sid: rid: %d (%s: %d) too high for mapping of domain: %s (%d-%d)\n",
494 rid, (*id_type == ID_GROUPID) ? "GID" : "UID", unid->uid, trust.dom[i].name,
495 trust.dom[i].min_id, trust.dom[i].max_id));
496 return NT_STATUS_INVALID_PARAMETER;
498 if (unid->uid < trust.dom[i].min_id) {
499 DEBUG(0,("rid_idmap_get_id_from_sid: rid: %d (%s: %d) too low for mapping of domain: %s (%d-%d)\n",
500 rid, (*id_type == ID_GROUPID) ? "GID" : "UID", unid->uid,
501 trust.dom[i].name, trust.dom[i].min_id, trust.dom[i].max_id));
502 return NT_STATUS_INVALID_PARAMETER;
505 DEBUG(3,("rid_idmap_get_id_from_sid: mapped SID [%s] to POSIX %s %d\n",
506 sid_to_string(sid_string, sid),
507 (*id_type == ID_GROUPID) ? "GID" : "UID", unid->uid));
509 return NT_STATUS_OK;
513 static NTSTATUS rid_idmap_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
515 return NT_STATUS_NOT_IMPLEMENTED;
518 static NTSTATUS rid_idmap_close(void)
520 SAFE_FREE(trust.dom);
522 return NT_STATUS_OK;
525 static NTSTATUS rid_idmap_allocate_rid(uint32 *rid, int rid_type)
527 return NT_STATUS_NOT_IMPLEMENTED;
530 static NTSTATUS rid_idmap_allocate_id(unid_t *id, int id_type)
532 return NT_STATUS_NOT_IMPLEMENTED;
535 static void rid_idmap_status(void)
537 DEBUG(0, ("RID IDMAP Status not available\n"));
540 static struct idmap_methods rid_methods = {
541 rid_idmap_init,
542 rid_idmap_allocate_rid,
543 rid_idmap_allocate_id,
544 rid_idmap_get_sid_from_id,
545 rid_idmap_get_id_from_sid,
546 rid_idmap_set_mapping,
547 rid_idmap_close,
548 rid_idmap_status
551 NTSTATUS init_module(void)
553 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "idmap_rid", &rid_methods);
554 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);