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;
56 * A list of the rids of well known BUILTIN and Domain users
60 rid_name builtin_alias_rids[] =
62 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" },
63 { BUILTIN_ALIAS_RID_USERS , "Users" },
64 { BUILTIN_ALIAS_RID_GUESTS , "Guests" },
65 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" },
67 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" },
68 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" },
69 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" },
70 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" },
71 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" },
75 /* array lookup of well-known Domain RID users. */
76 rid_name domain_user_rids[] =
78 { DOMAIN_USER_RID_ADMIN , "Administrator" },
79 { DOMAIN_USER_RID_GUEST , "Guest" },
83 /* array lookup of well-known Domain RID groups. */
84 rid_name domain_group_rids[] =
86 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" },
87 { DOMAIN_GROUP_RID_USERS , "Domain Users" },
88 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" },
93 int make_dom_gids(DOMAIN_GRP *mem, int num_members, DOM_GID **ppgids)
101 DEBUG(4,("make_dom_gids: %d\n", num_members));
103 if (mem == NULL || num_members == 0)
108 for (i = 0, count = 0; i < num_members && count < LSA_MAX_GROUPS; i++)
115 uint8 attr = mem[count].attr;
116 char *name = mem[count].name;
119 status = lookup_grp_rid(name, &rid, &type);
124 gids = (DOM_GID *)Realloc( gids, sizeof(DOM_GID) * (count+1) );
128 DEBUG(0,("make_dom_gids: Realloc fail !\n"));
132 gids[count].g_rid = rid;
133 gids[count].attr = attr;
135 DEBUG(5,("group name: %s rid: %d attr: %d\n",
141 DEBUG(1,("make_dom_gids: unknown group name %s\n", name));
149 /*******************************************************************
150 gets a domain user's groups
151 ********************************************************************/
152 int get_domain_user_groups(DOMAIN_GRP_MEMBER **grp_members, uint32 group_rid)
157 if (grp_members == NULL) return 0;
159 grp = getgrouprid(group_rid, grp_members, &num_mem);
170 /*******************************************************************
172 ********************************************************************/
173 uint32 lookup_builtin_names(DOM_SID *sid, char *name, uint8 *type)
175 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
177 status = (status != 0x0) ? lookup_wk_user_name (sid, name, type) : status;
178 status = (status != 0x0) ? lookup_wk_group_name(sid, name, type) : status;
179 status = (status != 0x0) ? lookup_wk_alias_name(sid, name, type) : status;
185 /*******************************************************************
186 lookup_added_name - names that have been added to the SAM database by admins.
187 ********************************************************************/
188 uint32 lookup_added_name(DOM_SID *sid, char *name, uint8 *type)
190 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
192 status = (status != 0x0) ? lookup_user_name (sid, name, type) : status;
193 status = (status != 0x0) ? lookup_group_name(sid, name, type) : status;
194 status = (status != 0x0) ? lookup_alias_name(sid, name, type) : status;
200 /*******************************************************************
202 ********************************************************************/
203 uint32 lookup_name(DOM_SID *sid, char *name, uint8 *type)
205 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
207 status = (status != 0x0) ? lookup_builtin_names(sid, name, type) : status;
208 status = (status != 0x0) ? lookup_added_name (sid, name, type) : status;
214 /*******************************************************************
216 ********************************************************************/
217 uint32 lookup_wk_group_name(DOM_SID *sid, char *group_name, uint8 *type)
223 (*type) = SID_NAME_WKN_GRP;
226 sid_split_rid(&tmp, &rid);
228 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
230 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
233 DEBUG(5,("lookup_wk_group_name: rid: %d", rid));
235 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
240 if (domain_group_rids[i].rid != 0)
242 fstrcpy(group_name, domain_group_rids[i].name);
243 DEBUG(5,(" = %s\n", group_name));
247 DEBUG(5,(" none mapped\n"));
248 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
251 /*******************************************************************
253 ********************************************************************/
254 uint32 lookup_group_name(DOM_SID *sid, char *group_name, uint8 *type)
256 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
260 (*type) = SID_NAME_DOM_GRP;
263 sid_split_rid(&tmp, &rid);
265 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
270 DEBUG(5,("lookup_group_name: rid: 0x%x", rid));
272 if (map_group_sid_to_name(sid, group_name, NULL))
279 DEBUG(5,(" = %s\n", group_name));
283 DEBUG(5,(" none mapped\n"));
289 /*******************************************************************
291 ********************************************************************/
292 uint32 lookup_wk_alias_name(DOM_SID *sid, char *alias_name, uint8 *type)
298 (*type) = SID_NAME_ALIAS;
301 sid_split_rid(&tmp, &rid);
303 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
305 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
308 DEBUG(5,("lookup_wk_alias_name: rid: %d", rid));
310 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
315 if (builtin_alias_rids[i].rid != 0)
317 fstrcpy(alias_name, builtin_alias_rids[i].name);
318 DEBUG(5,(" = %s\n", alias_name));
322 DEBUG(5,(" none mapped\n"));
323 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
326 /*******************************************************************
328 ********************************************************************/
329 uint32 lookup_alias_name(DOM_SID *sid, char *alias_name, uint8 *type)
332 (*type) = SID_NAME_ALIAS;
334 sid_to_string(sid_str, sid);
336 DEBUG(2,("lookup_alias_name: sid: %d\n", sid_str));
337 DEBUG(2,(" NOT IMPLEMENTED\n"));
339 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
342 /*******************************************************************
343 lookup well-known user name
344 ********************************************************************/
345 uint32 lookup_wk_user_name(DOM_SID *sid, char *user_name, uint8 *type)
351 (*type) = SID_NAME_USER;
354 sid_split_rid(&tmp, &rid);
356 if (!sid_equal(&global_sid_S_1_5_20, &tmp))
358 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
361 DEBUG(5,("lookup_wk_user_name: rid: %d", rid));
363 /* look up the well-known domain user rids first */
364 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
369 if (domain_user_rids[i].rid != 0)
371 fstrcpy(user_name, domain_user_rids[i].name);
372 DEBUG(5,(" = %s\n", user_name));
376 DEBUG(5,(" none mapped\n"));
377 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
380 /*******************************************************************
382 ********************************************************************/
383 uint32 lookup_user_name(DOM_SID *sid, char *user_name, uint8 *type)
385 struct sam_disp_info *disp_info;
389 (*type) = SID_NAME_USER;
392 sid_split_rid(&tmp, &rid);
394 if (sid_equal(&global_sam_sid, &tmp))
396 DEBUG(5,("lookup_user_name in SAM %s: rid: %d",
397 global_sam_name, rid));
399 /* find the user account */
401 disp_info = getsamdisprid(rid);
404 if (disp_info != NULL)
406 fstrcpy(user_name, disp_info->smb_name);
407 DEBUG(5,(" = %s\n", user_name));
411 DEBUG(5,(" none mapped\n"));
414 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
417 /*******************************************************************
419 ********************************************************************/
420 uint32 lookup_group_rid(char *group_name, uint32 *rid, uint8 *type)
425 (*type) = SID_NAME_DOM_GRP;
427 DEBUG(5,("lookup_group_rid: name: %s", group_name));
429 if (map_group_name_to_sid(group_name, &sid) &&
430 sid_split_rid(&sid, rid) &&
431 sid_equal(&sid, &global_sam_sid))
433 DEBUG(5,(" = 0x%x\n", (*rid)));
437 DEBUG(5,(" none mapped\n"));
438 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
441 /*******************************************************************
443 ********************************************************************/
444 uint32 lookup_wk_group_rid(char *group_name, uint32 *rid, uint8 *type)
447 int i = -1; /* start do loop at -1 */
449 (*type) = SID_NAME_WKN_GRP;
451 do /* find, if it exists, a group rid for the group name */
454 (*rid) = domain_group_rids[i].rid;
455 grp_name = domain_group_rids[i].name;
457 } while (grp_name != NULL && !strequal(grp_name, group_name));
459 return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
462 /*******************************************************************
464 ********************************************************************/
465 uint32 lookup_alias_sid(char *alias_name, DOM_SID *sid, uint8 *type)
467 (*type) = SID_NAME_ALIAS;
469 DEBUG(5,("lookup_alias_rid: name: %s", alias_name));
471 if (map_alias_name_to_sid(alias_name, sid))
474 sid_to_string(sid_str, sid);
475 DEBUG(5,(" = %s\n", sid_str));
479 DEBUG(5,(" none mapped\n"));
480 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
483 /*******************************************************************
485 ********************************************************************/
486 uint32 lookup_alias_rid(char *alias_name, uint32 *rid, uint8 *type)
491 (*type) = SID_NAME_ALIAS;
493 DEBUG(5,("lookup_alias_rid: name: %s", alias_name));
495 if (map_alias_name_to_sid(alias_name, &sid) &&
496 sid_split_rid(&sid, rid) &&
497 sid_equal(&sid, &global_sam_sid))
499 DEBUG(5,(" = 0x%x\n", (*rid)));
503 DEBUG(5,(" none mapped\n"));
504 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
507 /*******************************************************************
509 ********************************************************************/
510 uint32 lookup_wk_alias_sid(char *alias_name, DOM_SID *sid, uint8 *type)
515 (*type) = SID_NAME_ALIAS;
517 do /* find, if it exists, a alias rid for the alias name*/
519 rid = builtin_alias_rids[i].rid;
520 als_name = builtin_alias_rids[i].name;
524 if (strequal(als_name, alias_name))
526 sid_copy(sid, &global_sid_S_1_5_20);
527 sid_append_rid(sid, rid);
532 } while (als_name != NULL);
534 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
537 /*******************************************************************
539 ********************************************************************/
540 uint32 lookup_wk_alias_rid(char *alias_name, uint32 *rid, uint8 *type)
543 int i = -1; /* start do loop at -1 */
545 (*type) = SID_NAME_ALIAS;
547 do /* find, if it exists, a alias rid for the alias name*/
550 (*rid) = builtin_alias_rids[i].rid;
551 als_name = builtin_alias_rids[i].name;
553 } while (als_name != NULL && !strequal(als_name, alias_name));
555 return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
558 /*******************************************************************
560 ********************************************************************/
561 uint32 lookup_sid(char *name, DOM_SID *sid, uint8 *type)
563 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
567 split_domain_name(name, domain, user);
569 if (!strequal(domain, global_sam_name))
571 DEBUG(0,("lookup_sid: remote domain %s not supported\n", domain));
575 status = (status != 0x0) ? lookup_wk_alias_sid(user, sid, type) : status;
576 status = (status != 0x0) ? lookup_alias_sid (user, sid, type) : status;
578 status = (status != 0x0) ? lookup_domain_sid (user, sid, type) : status;
584 /*******************************************************************
585 lookup_added_user_rid
586 ********************************************************************/
587 uint32 lookup_added_user_rids(char *user_name,
588 uint32 *usr_rid, uint32 *grp_rid)
590 struct sam_passwd *sam_pass;
594 /* find the user account */
596 sam_pass = getsam21pwnam(user_name);
599 if (sam_pass != NULL)
601 (*usr_rid) = sam_pass->user_rid ;
602 (*grp_rid) = sam_pass->group_rid;
606 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
609 /*******************************************************************
610 lookup_added_user_rid
611 ********************************************************************/
612 uint32 lookup_added_user_rid(char *user_name, uint32 *rid, uint8 *type)
614 struct sam_passwd *sam_pass;
616 (*type) = SID_NAME_USER;
618 /* find the user account */
620 sam_pass = getsam21pwnam(user_name);
623 if (sam_pass != NULL)
625 (*rid) = sam_pass->user_rid;
629 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
632 /*******************************************************************
634 ********************************************************************/
635 uint32 lookup_wk_user_rid(char *user_name, uint32 *rid, uint8 *type)
638 int i = -1; /* start do loop at -1 */
640 (*type) = SID_NAME_USER;
642 do /* find, if it exists, a alias rid for the alias name*/
645 (*rid) = domain_user_rids[i].rid;
646 usr_name = domain_user_rids[i].name;
648 } while (usr_name != NULL && !strequal(usr_name, user_name));
650 return (usr_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
653 /*******************************************************************
655 ********************************************************************/
656 uint32 lookup_added_grp_rid(char *name, uint32 *rid, uint8 *type)
658 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
660 status = (status != 0x0) ? lookup_group_rid(name, rid, type) : status;
661 status = (status != 0x0) ? lookup_alias_rid(name, rid, type) : status;
666 /*******************************************************************
667 lookup_builtin_grp_rid
668 ********************************************************************/
669 uint32 lookup_builtin_grp_rid(char *name, uint32 *rid, uint8 *type)
671 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
673 status = (status != 0x0) ? lookup_wk_group_rid(name, rid, type) : status;
674 status = (status != 0x0) ? lookup_wk_alias_rid(name, rid, type) : status;
679 /*******************************************************************
681 ********************************************************************/
682 uint32 lookup_grp_rid(char *name, uint32 *rid, uint8 *type)
684 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
686 status = (status != 0x0) ? lookup_builtin_grp_rid(name, rid, type) : status;
687 status = (status != 0x0) ? lookup_added_grp_rid (name, rid, type) : status;
692 /*******************************************************************
694 ********************************************************************/
695 uint32 lookup_user_rid(char *name, uint32 *rid, uint8 *type)
697 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
699 status = (status != 0x0) ? lookup_wk_user_rid (name, rid, type) : status;
700 status = (status != 0x0) ? lookup_added_user_rid(name, rid, type) : status;
705 /*******************************************************************
707 ********************************************************************/
708 uint32 lookup_rid(char *name, uint32 *rid, uint8 *type)
710 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
712 status = (status != 0x0) ? lookup_user_rid(name, rid, type) : status;
713 status = (status != 0x0) ? lookup_grp_rid (name, rid, type) : status;
718 /*******************************************************************
720 ********************************************************************/
721 uint32 lookup_user_rids(char *name, uint32 *usr_rid, uint32 *grp_rid)
723 uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
727 * try an ordinary user lookup
730 status = lookup_added_user_rids(name, usr_rid, grp_rid);
737 * hm. must be a well-known user, in a well-known group.
740 status = lookup_wk_user_rid(name, usr_rid, &type);
741 if (status != 0 || type != SID_NAME_USER)
743 return status; /* ok, maybe not! */
745 if (type != SID_NAME_USER)
747 return 0xC0000000 | NT_STATUS_NONE_MAPPED; /* users only... */
751 * ok, got the user rid: now try the group rid
754 status = lookup_builtin_grp_rid(name, grp_rid, &type);
755 if (type == SID_NAME_DOM_GRP ||
756 type == SID_NAME_ALIAS ||
757 type == SID_NAME_WKN_GRP)
759 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;