2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
6 * Copyright (C) Paul Ashton 1997-1998.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* this module apparently provides an implementation of DCE/RPC over a
24 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
25 * documentation are available (in on-line form) from the X-Open group.
27 * this module should provide a level of abstraction between SMB
28 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
29 * data copies, and network traffic.
31 * in this version, which takes a "let's learn what's going on and
32 * get something running" approach, there is additional network
33 * traffic generated, but the code should be easier to understand...
35 * ... if you read the docs. or stare at packets for weeks on end.
42 * A list of the rids of well known BUILTIN and Domain users
46 rid_name builtin_alias_rids[] =
48 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" },
49 { BUILTIN_ALIAS_RID_USERS , "Users" },
50 { BUILTIN_ALIAS_RID_GUESTS , "Guests" },
51 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" },
53 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" },
54 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" },
55 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" },
56 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" },
57 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" },
61 /* array lookup of well-known Domain RID users. */
62 rid_name domain_user_rids[] =
64 { DOMAIN_USER_RID_ADMIN , "Administrator" },
65 { DOMAIN_USER_RID_GUEST , "Guest" },
69 /* array lookup of well-known Domain RID groups. */
70 rid_name domain_group_rids[] =
72 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" },
73 { DOMAIN_GROUP_RID_USERS , "Domain Users" },
74 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" },
78 /*******************************************************************
79 gets a domain user's groups
80 ********************************************************************/
81 NTSTATUS get_alias_user_groups(TALLOC_CTX *ctx, DOM_SID *sid, int *numgroups, uint32 **prids, DOM_SID *q_sid)
83 SAM_ACCOUNT *sam_pass=NULL;
84 struct sys_grent *glist;
85 struct sys_grent *grp;
86 int i, num, cur_rid=0;
91 fstring str_domsid, str_qsid;
93 uint32 *rids=NULL, *new_rids=NULL;
94 gid_t winbind_gid_low, winbind_gid_high;
98 * this code is far from perfect.
99 * first it enumerates the full /etc/group and that can be slow.
100 * second, it works only with users' SIDs
101 * whereas the day we support nested groups, it will have to
102 * support both users's SIDs and domain groups' SIDs
104 * having our own ldap backend would be so much faster !
105 * we're far from that, but hope one day ;-) JFM.
111 lp_winbind_gid(&winbind_gid_low, &winbind_gid_high);
114 DEBUG(10,("get_alias_user_groups: looking if SID %s is a member of groups in the SID domain %s\n",
115 sid_to_string(str_qsid, q_sid), sid_to_string(str_domsid, sid)));
117 sid_peek_rid(q_sid, &rid);
119 pdb_init_sam(&sam_pass);
121 ret = pdb_getsampwrid(sam_pass, rid);
124 pdb_free_sam(&sam_pass);
125 return NT_STATUS_NO_SUCH_USER;
128 fstrcpy(user_name, pdb_get_username(sam_pass));
129 grid=pdb_get_group_rid(sam_pass);
130 gid=pdb_get_gid(sam_pass);
132 grp = glist = getgrent_list();
134 pdb_free_sam(&sam_pass);
135 return NT_STATUS_NO_MEMORY;
138 for (; grp != NULL; grp = grp->next) {
139 if(!get_group_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)) {
140 DEBUG(10,("get_alias_user_groups: gid %d. not found\n", (int)grp->gr_gid));
144 /* if it's not an alias, continue */
145 if (map.sid_name_use!=SID_NAME_ALIAS) {
146 DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
150 sid_copy(&tmp_sid, &map.sid);
151 sid_split_rid(&tmp_sid, &rid);
153 /* if the sid is not in the correct domain, continue */
154 if (!sid_equal(&tmp_sid, sid)) {
155 DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
159 /* Don't return winbind groups as they are not local! */
160 if ((grp->gr_gid >= winbind_gid_low) && (grp->gr_gid <= winbind_gid_high)) {
161 DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name));
165 /* Don't return user private groups... */
166 if (Get_Pwnam(map.nt_name) != 0) {
167 DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name));
171 /* the group is fine, we can check if there is the user we're looking for */
172 DEBUG(10,("get_alias_user_groups: checking if the user is a member of %s.\n", map.nt_name));
174 for(num=0; grp->gr_mem[num]!=NULL; num++) {
175 if(strcmp(grp->gr_mem[num], user_name)==0) {
176 /* we found the user, add the group to the list */
178 new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
179 if (new_rids==NULL) {
180 DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
181 pdb_free_sam(&sam_pass);
182 return NT_STATUS_NO_MEMORY;
186 sid_peek_rid(&map.sid, &(rids[cur_rid]));
187 DEBUG(10,("get_alias_user_groups: user found in group %s\n", map.nt_name));
196 /* now check for the user's gid (the primary group rid) */
197 for (i=0; i<cur_rid && grid!=rids[i]; i++)
200 /* the user's gid is already there */
202 DEBUG(10,("get_alias_user_groups: user is already in the list. good.\n"));
206 DEBUG(10,("get_alias_user_groups: looking for gid %d of user %s\n", (int)gid, user_name));
208 if(!get_group_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
209 DEBUG(0,("get_alias_user_groups: gid of user %s doesn't exist. Check your /etc/passwd and /etc/group files\n", user_name));
213 /* the primary group isn't an alias */
214 if (map.sid_name_use!=SID_NAME_ALIAS) {
215 DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
219 sid_copy(&tmp_sid, &map.sid);
220 sid_split_rid(&tmp_sid, &rid);
222 /* if the sid is not in the correct domain, continue */
223 if (!sid_equal(&tmp_sid, sid)) {
224 DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
228 /* Don't return winbind groups as they are not local! */
229 if ((gid >= winbind_gid_low) && (gid <= winbind_gid_high)) {
230 DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name ));
234 /* Don't return user private groups... */
235 if (Get_Pwnam(map.nt_name) != 0) {
236 DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name ));
240 new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
241 if (new_rids==NULL) {
242 DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
243 pdb_free_sam(&sam_pass);
244 return NT_STATUS_NO_MEMORY;
248 sid_peek_rid(&map.sid, &(rids[cur_rid]));
254 pdb_free_sam(&sam_pass);
260 /*******************************************************************
261 gets a domain user's groups
262 ********************************************************************/
263 BOOL get_domain_user_groups(TALLOC_CTX *ctx, int *numgroups, DOM_GID **pgids, SAM_ACCOUNT *sam_pass)
266 int i, num, num_entries, cur_gid=0;
275 fstrcpy(user_name, pdb_get_username(sam_pass));
276 grid=pdb_get_group_rid(sam_pass);
278 DEBUG(10,("get_domain_user_groups: searching domain groups [%s] is a member of\n", user_name));
280 /* first get the list of the domain groups */
281 if (!enum_group_mapping(SID_NAME_DOM_GRP, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
283 DEBUG(10,("get_domain_user_groups: there are %d mapped groups\n", num_entries));
286 * alloc memory. In the worse case, we alloc memory for nothing.
287 * but I prefer to alloc for nothing
288 * than reallocing everytime.
290 gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) * num_entries);
292 /* for each group, check if the user is a member of*/
293 for(i=0; i<num_entries; i++) {
294 if ((grp=getgrgid(map[i].gid)) == NULL) {
296 DEBUG(5,("get_domain_user_groups: gid %d doesn't exist anymore !\n", (int)map[i].gid));
300 for(num=0; grp->gr_mem[num]!=NULL; num++) {
301 if(strcmp(grp->gr_mem[num], user_name)==0) {
302 /* we found the user, add the group to the list */
303 sid_peek_rid(&map[i].sid, &(gids[cur_gid].g_rid));
304 gids[cur_gid].attr=7;
305 DEBUG(10,("get_domain_user_groups: user found in group %s\n", map[i].nt_name));
312 /* we have checked the groups */
313 /* we must now check the gid of the user or the primary group rid, that's the same */
314 for (i=0; i<cur_gid && grid!=gids[i].g_rid; i++)
317 /* the user's gid is already there */
320 * the primary group of the user but be the first one in the list
323 gids[i].g_rid=gids[0].g_rid;
328 for(i=0; i<num_entries; i++) {
329 sid_peek_rid(&map[i].sid, &tmp_rid);
332 * the primary group of the user but be the first one in the list
335 gids[cur_gid].g_rid=gids[0].g_rid;
336 gids[0].g_rid=tmp_rid;
337 gids[cur_gid].attr=7;
338 DEBUG(10,("get_domain_user_groups: primary gid of user found in group %s\n", map[i].nt_name));
340 goto done; /* leave the loop early */
344 DEBUG(0,("get_domain_user_groups: primary gid of user [%s] is not a Domain group !\n", user_name));
345 DEBUGADD(0,("get_domain_user_groups: You should fix it, NT doesn't like that\n"));
355 /*******************************************************************
356 Look up a local (domain) rid and return a name and type.
357 ********************************************************************/
358 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
361 (*type) = SID_NAME_DOM_GRP;
363 DEBUG(5,("lookup_group_name: rid: %d", rid));
365 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
370 if (domain_group_rids[i].rid != 0)
372 fstrcpy(group_name, domain_group_rids[i].name);
373 DEBUG(5,(" = %s\n", group_name));
377 DEBUG(5,(" none mapped\n"));
378 return NT_STATUS_NONE_MAPPED;
381 /*******************************************************************
382 Look up a local alias rid and return a name and type.
383 ********************************************************************/
384 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
387 (*type) = SID_NAME_WKN_GRP;
389 DEBUG(5,("lookup_alias_name: rid: %d", rid));
391 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
396 if (builtin_alias_rids[i].rid != 0)
398 fstrcpy(alias_name, builtin_alias_rids[i].name);
399 DEBUG(5,(" = %s\n", alias_name));
403 DEBUG(5,(" none mapped\n"));
404 return NT_STATUS_NONE_MAPPED;
407 /*******************************************************************
408 Look up a local user rid and return a name and type.
409 ********************************************************************/
410 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
412 SAM_ACCOUNT *sampwd=NULL;
416 (*type) = SID_NAME_USER;
418 DEBUG(5,("lookup_user_name: rid: %d", rid));
420 /* look up the well-known domain user rids first */
421 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
426 if (domain_user_rids[i].rid != 0) {
427 fstrcpy(user_name, domain_user_rids[i].name);
428 DEBUG(5,(" = %s\n", user_name));
432 pdb_init_sam(&sampwd);
434 /* ok, it's a user. find the user account */
436 ret = pdb_getsampwrid(sampwd, rid);
440 fstrcpy(user_name, pdb_get_username(sampwd) );
441 DEBUG(5,(" = %s\n", user_name));
442 pdb_free_sam(&sampwd);
446 DEBUG(5,(" none mapped\n"));
447 pdb_free_sam(&sampwd);
448 return NT_STATUS_NONE_MAPPED;
451 /*******************************************************************
452 Look up a local (domain) group name and return a rid
453 ********************************************************************/
454 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
457 int i = -1; /* start do loop at -1 */
459 do /* find, if it exists, a group rid for the group name*/
462 (*rid) = domain_group_rids[i].rid;
463 grp_name = domain_group_rids[i].name;
465 } while (grp_name != NULL && !strequal(grp_name, group_name));
467 return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
470 /*******************************************************************
471 Look up a local (BUILTIN) alias name and return a rid
472 ********************************************************************/
473 NTSTATUS local_lookup_alias_rid(char *alias_name, uint32 *rid)
476 int i = -1; /* start do loop at -1 */
478 do /* find, if it exists, a alias rid for the alias name*/
481 (*rid) = builtin_alias_rids[i].rid;
482 als_name = builtin_alias_rids[i].name;
484 } while (als_name != NULL && !strequal(als_name, alias_name));
486 return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
489 /*******************************************************************
490 Look up a local user name and return a rid
491 ********************************************************************/
492 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
494 SAM_ACCOUNT *sampass=NULL;
499 pdb_init_sam(&sampass);
501 /* find the user account */
503 ret = pdb_getsampwnam(sampass, user_name);
507 (*rid) = pdb_get_user_rid(sampass);
508 pdb_free_sam(&sampass);
512 pdb_free_sam(&sampass);
513 return NT_STATUS_NONE_MAPPED;