Large commit which restructures the local password storage API.
[Samba.git] / source / rpc_server / srv_util.c
blob8349b7add69af5d44abee8d7284b32b31c81c0f4
1 #define OLD_NTDOMAIN 1
3 /*
4 * Unix SMB/Netbios implementation.
5 * Version 1.9.
6 * RPC Pipe client / server routines
7 * Copyright (C) Andrew Tridgell 1992-1998
8 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
9 * Copyright (C) Paul Ashton 1997-1998.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* this module apparently provides an implementation of DCE/RPC over a
27 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
28 * documentation are available (in on-line form) from the X-Open group.
30 * this module should provide a level of abstraction between SMB
31 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
32 * data copies, and network traffic.
34 * in this version, which takes a "let's learn what's going on and
35 * get something running" approach, there is additional network
36 * traffic generated, but the code should be easier to understand...
38 * ... if you read the docs. or stare at packets for weeks on end.
42 #include "includes.h"
44 extern int DEBUGLEVEL;
47 * A list of the rids of well known BUILTIN and Domain users
48 * and groups.
51 rid_name builtin_alias_rids[] =
53 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" },
54 { BUILTIN_ALIAS_RID_USERS , "Users" },
55 { BUILTIN_ALIAS_RID_GUESTS , "Guests" },
56 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" },
58 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" },
59 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" },
60 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" },
61 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" },
62 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" },
63 { 0 , NULL }
66 /* array lookup of well-known Domain RID users. */
67 rid_name domain_user_rids[] =
69 { DOMAIN_USER_RID_ADMIN , "Administrator" },
70 { DOMAIN_USER_RID_GUEST , "Guest" },
71 { 0 , NULL }
74 /* array lookup of well-known Domain RID groups. */
75 rid_name domain_group_rids[] =
77 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" },
78 { DOMAIN_GROUP_RID_USERS , "Domain Users" },
79 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" },
80 { 0 , NULL }
83 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
85 char *ptr;
86 pstring s2;
87 int count;
88 DOM_GID *gids;
90 *ppgids = NULL;
92 DEBUG(4,("make_dom_gids: %s\n", gids_str));
94 if (gids_str == NULL || *gids_str == 0)
95 return 0;
97 for (count = 0, ptr = gids_str;
98 next_token(&ptr, s2, NULL, sizeof(s2));
99 count++)
102 gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
103 if(!gids)
105 DEBUG(0,("make_dom_gids: malloc fail !\n"));
106 return 0;
109 for (count = 0, ptr = gids_str;
110 next_token(&ptr, s2, NULL, sizeof(s2)) &&
111 count < LSA_MAX_GROUPS;
112 count++)
114 /* the entries are of the form GID/ATTR, ATTR being optional.*/
115 char *attr;
116 uint32 rid = 0;
117 int i;
119 attr = strchr(s2,'/');
120 if (attr)
121 *attr++ = 0;
123 if (!attr || !*attr)
124 attr = "7"; /* default value for attribute is 7 */
126 /* look up the RID string and see if we can turn it into a rid number */
127 for (i = 0; builtin_alias_rids[i].name != NULL; i++)
129 if (strequal(builtin_alias_rids[i].name, s2))
131 rid = builtin_alias_rids[i].rid;
132 break;
136 if (rid == 0)
137 rid = atoi(s2);
139 if (rid == 0)
141 DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
142 count--;
144 else
146 gids[count].g_rid = rid;
147 gids[count].attr = atoi(attr);
149 DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
153 *ppgids = gids;
154 return count;
158 /*******************************************************************
159 gets a domain user's groups
160 ********************************************************************/
161 void get_domain_user_groups(char *domain_groups, char *user)
163 pstring tmp;
165 if (domain_groups == NULL || user == NULL) return;
167 /* any additional groups this user is in. e.g power users */
168 pstrcpy(domain_groups, lp_domain_groups());
170 /* can only be a user or a guest. cannot be guest _and_ admin */
171 if (user_in_list(user, lp_domain_guest_group()))
173 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
174 pstrcat(domain_groups, tmp);
176 DEBUG(3,("domain guest group access %s granted\n", tmp));
178 else
180 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
181 pstrcat(domain_groups, tmp);
183 DEBUG(3,("domain group access %s granted\n", tmp));
185 if (user_in_list(user, lp_domain_admin_group()))
187 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
188 pstrcat(domain_groups, tmp);
190 DEBUG(3,("domain admin group access %s granted\n", tmp));
196 /*******************************************************************
197 lookup_group_name
198 ********************************************************************/
199 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
201 int i = 0;
202 (*type) = SID_NAME_DOM_GRP;
204 DEBUG(5,("lookup_group_name: rid: %d", rid));
206 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
208 i++;
211 if (domain_group_rids[i].rid != 0)
213 fstrcpy(group_name, domain_group_rids[i].name);
214 DEBUG(5,(" = %s\n", group_name));
215 return 0x0;
218 DEBUG(5,(" none mapped\n"));
219 return NT_STATUS_NONE_MAPPED;
222 /*******************************************************************
223 lookup_alias_name
224 ********************************************************************/
225 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
227 int i = 0;
228 (*type) = SID_NAME_WKN_GRP;
230 DEBUG(5,("lookup_alias_name: rid: %d", rid));
232 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
234 i++;
237 if (builtin_alias_rids[i].rid != 0)
239 fstrcpy(alias_name, builtin_alias_rids[i].name);
240 DEBUG(5,(" = %s\n", alias_name));
241 return 0x0;
244 DEBUG(5,(" none mapped\n"));
245 return NT_STATUS_NONE_MAPPED;
248 /*******************************************************************
249 lookup_user_name
250 ********************************************************************/
251 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
253 struct sam_disp_info *disp_info;
254 int i = 0;
255 (*type) = SID_NAME_USER;
257 DEBUG(5,("lookup_user_name: rid: %d", rid));
259 /* look up the well-known domain user rids first */
260 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
262 i++;
265 if (domain_user_rids[i].rid != 0)
267 fstrcpy(user_name, domain_user_rids[i].name);
268 DEBUG(5,(" = %s\n", user_name));
269 return 0x0;
272 /* ok, it's a user. find the user account */
273 become_root();
274 disp_info = pdb_sam_to_dispinfo(pdb_getsampwrid(rid));
275 unbecome_root();
277 if (disp_info != NULL)
279 fstrcpy(user_name, disp_info->smb_name);
280 DEBUG(5,(" = %s\n", user_name));
281 return 0x0;
284 DEBUG(5,(" none mapped\n"));
285 return NT_STATUS_NONE_MAPPED;
288 /*******************************************************************
289 lookup_group_rid
290 ********************************************************************/
291 uint32 lookup_group_rid(char *group_name, uint32 *rid)
293 char *grp_name;
294 int i = -1; /* start do loop at -1 */
296 do /* find, if it exists, a group rid for the group name*/
298 i++;
299 (*rid) = domain_group_rids[i].rid;
300 grp_name = domain_group_rids[i].name;
302 } while (grp_name != NULL && !strequal(grp_name, group_name));
304 return (grp_name != NULL) ? 0 : NT_STATUS_NONE_MAPPED;
307 /*******************************************************************
308 lookup_alias_rid
309 ********************************************************************/
310 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
312 char *als_name;
313 int i = -1; /* start do loop at -1 */
315 do /* find, if it exists, a alias rid for the alias name*/
317 i++;
318 (*rid) = builtin_alias_rids[i].rid;
319 als_name = builtin_alias_rids[i].name;
321 } while (als_name != NULL && !strequal(als_name, alias_name));
323 return (als_name != NULL) ? 0 : NT_STATUS_NONE_MAPPED;
326 /*******************************************************************
327 lookup_user_rid
328 ********************************************************************/
329 uint32 lookup_user_rid(char *user_name, uint32 *rid)
331 SAM_ACCOUNT *sam_pass;
332 (*rid) = 0;
334 /* find the user account */
335 become_root();
336 sam_pass = pdb_getsampwnam(user_name);
337 unbecome_root();
339 if (sam_pass != NULL)
341 *rid = pdb_get_user_rid(sam_pass);
342 return 0x0;
345 return NT_STATUS_NONE_MAPPED;
348 #undef OLD_NTDOMAIN