update copyright notice since it we are now almost 4 months into 2003
[Samba.git] / source / rpc_server / srv_util.c
blob44c35d3d588c289b6a1d94273c559f8a0fb1cfd4
1 /*
2 * Unix SMB/Netbios implementation.
3 * Version 1.9.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1998
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7 * Copyright (C) Paul Ashton 1997-1998.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* this module apparently provides an implementation of DCE/RPC over a
25 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
26 * documentation are available (in on-line form) from the X-Open group.
28 * this module should provide a level of abstraction between SMB
29 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
30 * data copies, and network traffic.
32 * in this version, which takes a "let's learn what's going on and
33 * get something running" approach, there is additional network
34 * traffic generated, but the code should be easier to understand...
36 * ... if you read the docs. or stare at packets for weeks on end.
40 #include "includes.h"
43 * A list of the rids of well known BUILTIN and Domain users
44 * and groups.
47 rid_name builtin_alias_rids[] =
49 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" },
50 { BUILTIN_ALIAS_RID_USERS , "Users" },
51 { BUILTIN_ALIAS_RID_GUESTS , "Guests" },
52 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" },
54 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" },
55 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" },
56 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" },
57 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" },
58 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" },
59 { 0 , NULL }
62 /* array lookup of well-known Domain RID users. */
63 rid_name domain_user_rids[] =
65 { DOMAIN_USER_RID_ADMIN , "Administrator" },
66 { DOMAIN_USER_RID_GUEST , "Guest" },
67 { 0 , NULL }
70 /* array lookup of well-known Domain RID groups. */
71 rid_name domain_group_rids[] =
73 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" },
74 { DOMAIN_GROUP_RID_USERS , "Domain Users" },
75 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" },
76 { 0 , NULL }
79 #define LSA_MAX_GROUPS 96
81 int make_dom_gids(TALLOC_CTX *ctx, char *gids_str, DOM_GID **ppgids)
83 const char *ptr;
84 pstring s2;
85 int count;
86 DOM_GID *gids;
88 *ppgids = NULL;
90 DEBUG(4,("make_dom_gids: %s\n", gids_str));
92 if (gids_str == NULL || *gids_str == 0)
93 return 0;
95 for (count = 0, ptr = gids_str;
96 next_token(&ptr, s2, NULL, sizeof(s2));
97 count++)
100 gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) * count );
101 if(!gids)
103 DEBUG(0,("make_dom_gids: talloc fail !\n"));
104 return 0;
107 for (count = 0, ptr = gids_str;
108 next_token(&ptr, s2, NULL, sizeof(s2)) &&
109 count < LSA_MAX_GROUPS;
110 count++)
112 /* the entries are of the form GID/ATTR, ATTR being optional.*/
113 const char *attr = NULL;
114 char *pattr = NULL;
115 uint32 rid = 0;
116 int i;
118 pattr = strchr(s2,'/');
119 if (pattr)
120 *pattr++ = 0;
122 attr = pattr;
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 #if 0 /* removed by --jerry */
168 /* any additional groups this user is in. e.g power users */
169 pstrcpy(domain_groups, lp_domain_groups());
170 #else
171 *domain_groups = '\0';
172 #endif
174 /* can only be a user or a guest. cannot be guest _and_ admin */
175 if (user_in_list(user, lp_domain_guest_group()))
177 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
178 pstrcat(domain_groups, tmp);
180 DEBUG(3,("domain guest group access %s granted\n", tmp));
182 else
184 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
185 pstrcat(domain_groups, tmp);
187 DEBUG(3,("domain group access %s granted\n", tmp));
189 if (user_in_list(user, lp_domain_admin_group()))
191 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
192 pstrcat(domain_groups, tmp);
194 DEBUG(3,("domain admin group access %s granted\n", tmp));
199 /*******************************************************************
200 Look up a local (domain) rid and return a name and type.
201 ********************************************************************/
202 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
204 int i = 0;
205 (*type) = SID_NAME_DOM_GRP;
207 DEBUG(5,("lookup_group_name: rid: %d", rid));
209 while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
211 i++;
214 if (domain_group_rids[i].rid != 0)
216 fstrcpy(group_name, domain_group_rids[i].name);
217 DEBUG(5,(" = %s\n", group_name));
218 return NT_STATUS_OK;
221 DEBUG(5,(" none mapped\n"));
222 return NT_STATUS_NONE_MAPPED;
225 /*******************************************************************
226 Look up a local alias rid and return a name and type.
227 ********************************************************************/
228 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
230 int i = 0;
231 (*type) = SID_NAME_WKN_GRP;
233 DEBUG(5,("lookup_alias_name: rid: %d", rid));
235 while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
237 i++;
240 if (builtin_alias_rids[i].rid != 0)
242 fstrcpy(alias_name, builtin_alias_rids[i].name);
243 DEBUG(5,(" = %s\n", alias_name));
244 return NT_STATUS_OK;
247 DEBUG(5,(" none mapped\n"));
248 return NT_STATUS_NONE_MAPPED;
251 /*******************************************************************
252 Look up a local user rid and return a name and type.
253 ********************************************************************/
254 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
256 SAM_ACCOUNT *sampwd=NULL;
257 int i = 0;
258 BOOL ret;
260 (*type) = SID_NAME_USER;
262 DEBUG(5,("lookup_user_name: rid: %d", rid));
264 /* look up the well-known domain user rids first */
265 while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
267 i++;
270 if (domain_user_rids[i].rid != 0) {
271 fstrcpy(user_name, domain_user_rids[i].name);
272 DEBUG(5,(" = %s\n", user_name));
273 return NT_STATUS_OK;
276 pdb_init_sam(&sampwd);
278 /* ok, it's a user. find the user account */
279 become_root();
280 ret = pdb_getsampwrid(sampwd, rid);
281 unbecome_root();
283 if (ret == True) {
284 fstrcpy(user_name, pdb_get_username(sampwd) );
285 DEBUG(5,(" = %s\n", user_name));
286 pdb_free_sam(sampwd);
287 return NT_STATUS_OK;
290 DEBUG(5,(" none mapped\n"));
291 pdb_free_sam(sampwd);
292 return NT_STATUS_NONE_MAPPED;
295 /*******************************************************************
296 Look up a local (domain) group name and return a rid
297 ********************************************************************/
298 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
300 const char *grp_name;
301 int i = -1; /* start do loop at -1 */
303 do /* find, if it exists, a group rid for the group name*/
305 i++;
306 (*rid) = domain_group_rids[i].rid;
307 grp_name = domain_group_rids[i].name;
309 } while (grp_name != NULL && !strequal(grp_name, group_name));
311 return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
314 /*******************************************************************
315 Look up a local (BUILTIN) alias name and return a rid
316 ********************************************************************/
317 NTSTATUS local_lookup_alias_rid(char *alias_name, uint32 *rid)
319 const char *als_name;
320 int i = -1; /* start do loop at -1 */
322 do /* find, if it exists, a alias rid for the alias name*/
324 i++;
325 (*rid) = builtin_alias_rids[i].rid;
326 als_name = builtin_alias_rids[i].name;
328 } while (als_name != NULL && !strequal(als_name, alias_name));
330 return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
333 /*******************************************************************
334 Look up a local user name and return a rid
335 ********************************************************************/
336 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
338 SAM_ACCOUNT *sampass=NULL;
339 BOOL ret;
341 (*rid) = 0;
343 pdb_init_sam(&sampass);
345 /* find the user account */
346 become_root();
347 ret = pdb_getsampwnam(sampass, user_name);
348 unbecome_root();
350 if (ret == True) {
351 (*rid) = pdb_get_user_rid(sampass);
352 pdb_free_sam(sampass);
353 return NT_STATUS_OK;
356 pdb_free_sam(sampass);
357 return NT_STATUS_NONE_MAPPED;