3 * Unix SMB/Netbios implementation.
5 * RPC Pipe client / server routines
6 * Copyright (C) Andrew Tridgell 1992-1998
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8 * Copyright (C) Paul Ashton 1997-1998.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 this module provides nt user / nt rid lookup functions.
30 users, local groups, domain groups.
32 no unix / samba functions should be called in this module:
33 it should purely provide a gateway to the password database API,
34 the local group database API or the domain group database API,
35 but first checking built-in rids.
37 did i say rids? oops, what about "S-1-1" the "Everyone" group
38 and other such well-known sids...
40 speed is not of the essence: no particular optimisation is in place.
49 extern int DEBUGLEVEL;
51 extern fstring global_sam_name;
52 extern DOM_SID global_sam_sid;
53 extern DOM_SID global_sid_S_1_5_20;
55 extern rid_name builtin_alias_rids[];
56 extern rid_name domain_user_rids[];
57 extern rid_name domain_group_rids[];
59 int make_dom_gids(DOMAIN_GRP *mem, int num_members, DOM_GID **ppgids)
67 DEBUG(4,("make_dom_gids: %d\n", num_members));
69 if (mem == NULL || num_members == 0)
74 for (i = 0, count = 0; i < num_members && count < LSA_MAX_GROUPS; i++)
82 uint8 attr = mem[count].attr;
83 char *name = mem[count].name;
86 status = lookup_name(name, &sid, &type);
89 if (status == 0x0 && !sid_front_equal(&global_sam_sid, &sid))
92 sid_to_string(sid_str, &sid);
93 DEBUG(1,("make_dom_gids: unknown sid %s for groupname %s\n",
96 else if (status == 0x0)
98 sid_split_rid(&sid, &rid);
100 gids = (DOM_GID *)Realloc( gids, sizeof(DOM_GID) * (count+1) );
104 DEBUG(0,("make_dom_gids: Realloc fail !\n"));
108 gids[count].g_rid = rid;
109 gids[count].attr = attr;
111 DEBUG(5,("group name: %s rid: %d attr: %d\n",
117 DEBUG(1,("make_dom_gids: unknown groupname %s\n", name));
125 /*******************************************************************
126 gets a domain user's groups
127 ********************************************************************/
128 int get_domain_user_groups(DOMAIN_GRP_MEMBER **grp_members, uint32 group_rid)
133 if (grp_members == NULL) return 0;
135 grp = getgrouprid(group_rid, grp_members, &num_mem);
146 /*******************************************************************
148 ********************************************************************/
149 uint32 lookup_builtin_sid(DOM_SID *sid, char *name, uint8 *type)
151 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
153 status = (status != 0x0) ? lookup_wk_user_sid (sid, name, type) : status;
154 status = (status != 0x0) ? lookup_wk_group_sid(sid, name, type) : status;
155 status = (status != 0x0) ? lookup_wk_alias_sid(sid, name, type) : status;
161 /*******************************************************************
162 lookup_added_sid - names that have been added to the SAM database by admins.
163 ********************************************************************/
164 uint32 lookup_added_sid(DOM_SID *sid, char *name, uint8 *type)
166 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
168 status = (status != 0x0) ? lookup_user_sid (sid, name, type) : status;
169 status = (status != 0x0) ? lookup_group_sid(sid, name, type) : status;
170 status = (status != 0x0) ? lookup_alias_sid(sid, name, type) : status;
176 /*******************************************************************
178 ********************************************************************/
179 uint32 lookup_sid(DOM_SID *sid, char *name, uint8 *type)
181 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
183 status = (status != 0x0) ? lookup_builtin_sid(sid, name, type) : status;
184 status = (status != 0x0) ? lookup_added_sid (sid, name, type) : status;
190 /*******************************************************************
192 ********************************************************************/
193 uint32 lookup_wk_group_sid(DOM_SID *sid, char *group_name, uint8 *type)
199 (*type) = SID_NAME_WKN_GRP;
202 sid_split_rid(&tmp, &rid);
204 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
206 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
209 DEBUG(5,("lookup_wk_group_sid: rid: %d", rid));
211 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
216 if (domain_group_rids[i].rid != 0)
218 fstrcpy(group_name, domain_group_rids[i].name);
219 DEBUG(5,(" = %s\n", group_name));
223 DEBUG(5,(" none mapped\n"));
224 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
227 /*******************************************************************
229 ********************************************************************/
230 uint32 lookup_group_sid(DOM_SID *sid, char *group_name, uint8 *type)
235 DOMAIN_GRP *grp = NULL;
236 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
238 (*type) = SID_NAME_DOM_GRP;
240 sid_to_string(sid_str, sid);
241 DEBUG(5,("lookup_group_sid: sid: %s", sid_str));
244 sid_split_rid(&tmp, &rid);
246 if (!sid_equal(&global_sam_sid, &tmp))
248 DEBUG(5,("not our SID\n"));
249 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
252 grp = getgrouprid(rid, NULL, NULL);
256 fstrcpy(group_name, grp->name);
257 DEBUG(5,(" = %s\n", group_name));
261 DEBUG(5,(" none mapped\n"));
265 /*******************************************************************
267 ********************************************************************/
268 uint32 lookup_wk_alias_sid(DOM_SID *sid, char *alias_name, uint8 *type)
274 (*type) = SID_NAME_ALIAS;
277 sid_split_rid(&tmp, &rid);
279 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
281 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
284 DEBUG(5,("lookup_wk_alias_sid: rid: %d", rid));
286 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
291 if (builtin_alias_rids[i].rid != 0)
293 fstrcpy(alias_name, builtin_alias_rids[i].name);
294 DEBUG(5,(" = %s\n", alias_name));
298 DEBUG(5,(" none mapped\n"));
299 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
302 /*******************************************************************
304 ********************************************************************/
305 uint32 lookup_alias_sid(DOM_SID *sid, char *alias_name, uint8 *type)
310 LOCAL_GRP *als = NULL;
311 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
313 (*type) = SID_NAME_ALIAS;
315 sid_to_string(sid_str, sid);
316 DEBUG(5,("lookup_alias_sid: sid: %s", sid_str));
319 sid_split_rid(&tmp, &rid);
321 if (!sid_equal(&global_sam_sid, &tmp))
323 DEBUG(5,("not our SID\n"));
324 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
327 als = getaliasrid(rid, NULL, NULL);
331 fstrcpy(alias_name, als->name);
332 DEBUG(5,(" = %s\n", alias_name));
336 DEBUG(5,(" none mapped\n"));
340 /*******************************************************************
341 lookup well-known user name
342 ********************************************************************/
343 uint32 lookup_wk_user_sid(DOM_SID *sid, char *user_name, uint8 *type)
349 (*type) = SID_NAME_USER;
352 sid_split_rid(&tmp, &rid);
354 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
356 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
359 DEBUG(5,("lookup_wk_user_sid: rid: %d", rid));
361 /* look up the well-known domain user rids first */
362 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
367 if (domain_user_rids[i].rid != 0)
369 fstrcpy(user_name, domain_user_rids[i].name);
370 DEBUG(5,(" = %s\n", user_name));
374 DEBUG(5,(" none mapped\n"));
375 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
378 /*******************************************************************
380 ********************************************************************/
381 uint32 lookup_user_sid(DOM_SID *sid, char *user_name, uint8 *type)
383 struct sam_disp_info *disp_info;
387 (*type) = SID_NAME_USER;
390 sid_split_rid(&tmp, &rid);
392 if (sid_equal(&global_sam_sid, &tmp))
394 DEBUG(5,("lookup_user_sid in SAM %s: rid: %d",
395 global_sam_name, rid));
397 /* find the user account */
399 disp_info = getsamdisprid(rid);
402 if (disp_info != NULL)
404 fstrcpy(user_name, disp_info->nt_name);
405 DEBUG(5,(" = %s\n", user_name));
409 DEBUG(5,(" none mapped\n"));
412 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
415 /*******************************************************************
417 ********************************************************************/
418 uint32 lookup_added_group_name(const char *grp_name, const char *domain,
419 DOM_SID *sid, uint8 *type)
421 DOMAIN_GRP *grp = NULL;
422 (*type) = SID_NAME_DOM_GRP;
424 DEBUG(5,("lookup_added_group_name: name: %s", grp_name));
426 if (!strequal(domain, global_sam_name))
428 DEBUG(5,(" not our domain\n"));
429 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
432 grp = getgroupntnam(grp_name, NULL, NULL);
436 sid_copy(sid, &global_sam_sid);
437 sid_append_rid(sid, grp->rid);
439 DEBUG(5,(" = 0x%x\n", grp->rid));
443 DEBUG(5,(" none mapped\n"));
444 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
447 /*******************************************************************
448 lookup_added_alias_name
449 ********************************************************************/
450 uint32 lookup_added_alias_name(const char *als_name, const char *domain,
451 DOM_SID *sid, uint8 *type)
453 LOCAL_GRP *als = NULL;
454 (*type) = SID_NAME_ALIAS;
456 DEBUG(5,("lookup_added_alias_name: name: %s\\%s", domain, als_name));
458 if (!strequal(domain, global_sam_name))
460 DEBUG(5,(" not our domain\n"));
461 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
464 als = getaliasntnam(als_name, NULL, NULL);
468 sid_copy(sid, &global_sam_sid);
469 sid_append_rid(sid, als->rid);
471 DEBUG(5,(" = 0x%x\n", als->rid));
475 DEBUG(5,(" none mapped\n"));
476 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
479 /*******************************************************************
480 lookup_added_user_rid
481 ********************************************************************/
482 uint32 lookup_added_user_rids(char *nt_name,
483 uint32 *usr_rid, uint32 *grp_rid)
485 struct sam_passwd *sam_pass;
489 /* find the user account */
491 sam_pass = getsam21pwntnam(nt_name);
494 if (sam_pass != NULL)
496 (*usr_rid) = sam_pass->user_rid ;
497 (*grp_rid) = sam_pass->group_rid;
501 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
504 /*******************************************************************
505 lookup_added_user_name
506 ********************************************************************/
507 static uint32 lookup_added_user_name(const char *nt_name, const char *domain,
508 DOM_SID *sid, uint8 *type)
510 struct sam_passwd *sam_pass;
511 (*type) = SID_NAME_USER;
513 if (!strequal(domain, global_sam_name))
515 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
518 /* find the user account */
520 sam_pass = getsam21pwntnam(nt_name);
523 if (sam_pass != NULL)
525 sid_copy(sid, &global_sam_sid);
526 sid_append_rid(sid, sam_pass->user_rid);
531 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
534 /*******************************************************************
536 ********************************************************************/
537 static uint32 lookup_grp_name(const char *name, const char *domain,
538 DOM_SID *sid, uint8 *type)
540 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
542 status = (status != 0x0) ? lookup_wk_group_name (name, domain, sid, type) : status;
543 status = (status != 0x0) ? lookup_builtin_alias_name(name, domain, sid, type) : status;
544 status = (status != 0x0) ? lookup_added_group_name (name, domain, sid, type) : status;
545 status = (status != 0x0) ? lookup_added_alias_name (name, domain, sid, type) : status;
550 /*******************************************************************
552 ********************************************************************/
553 static uint32 lookup_user_name(const char *name, const char *domain,
554 DOM_SID *sid, uint8 *type)
556 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
558 status = (status != 0x0) ? lookup_wk_user_name (name, domain, sid, type) : status;
559 status = (status != 0x0) ? lookup_added_user_name(name, domain, sid, type) : status;
564 /*******************************************************************
566 ********************************************************************/
567 uint32 lookup_name(char *name, DOM_SID *sid, uint8 *type)
569 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
573 split_domain_name(name, domain, user);
575 if (!strequal(domain, global_sam_name))
577 DEBUG(0,("lookup_name: remote domain %s not supported\n", domain));
581 status = (status != 0x0) ? lookup_user_name (name, domain, sid, type) : status;
582 status = (status != 0x0) ? lookup_grp_name (name, domain, sid, type) : status;
584 status = (status != 0x0) ? lookup_domain_name (domain, sid, type) : status;