Snuck in some whitespace cleanup while I was visiting these files. (-:
[ira/wip.git] / source3 / rpc_server / srv_util.c
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.
13  *  
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.
18  *  
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.
22  */
23
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.
27  *
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.
31  *
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...
35  *
36  *  ... if you read the docs.  or stare at packets for weeks on end.
37  *
38  */
39
40 #include "includes.h"
41
42 extern int DEBUGLEVEL;
43
44 /*
45  * A list of the rids of well known BUILTIN and Domain users
46  * and groups.
47  */
48
49 rid_name builtin_alias_rids[] =
50 {  
51     { BUILTIN_ALIAS_RID_ADMINS       , "Administrators" },
52     { BUILTIN_ALIAS_RID_USERS        , "Users" },
53     { BUILTIN_ALIAS_RID_GUESTS       , "Guests" },
54     { BUILTIN_ALIAS_RID_POWER_USERS  , "Power Users" },
55    
56     { BUILTIN_ALIAS_RID_ACCOUNT_OPS  , "Account Operators" },
57     { BUILTIN_ALIAS_RID_SYSTEM_OPS   , "System Operators" },
58     { BUILTIN_ALIAS_RID_PRINT_OPS    , "Print Operators" },
59     { BUILTIN_ALIAS_RID_BACKUP_OPS   , "Backup Operators" },
60     { BUILTIN_ALIAS_RID_REPLICATOR   , "Replicator" },
61     { 0                             , NULL }
62 };
63
64 /* array lookup of well-known Domain RID users. */
65 rid_name domain_user_rids[] =
66 {  
67     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
68     { DOMAIN_USER_RID_GUEST         , "Guest" },
69     { 0                             , NULL }
70 };
71
72 /* array lookup of well-known Domain RID groups. */
73 rid_name domain_group_rids[] =
74 {  
75     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
76     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
77     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
78     { 0                             , NULL }
79 };
80
81 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
82 {
83   char *ptr;
84   pstring s2;
85   int count;
86   DOM_GID *gids;
87
88   *ppgids = NULL;
89
90   DEBUG(4,("make_dom_gids: %s\n", gids_str));
91
92   if (gids_str == NULL || *gids_str == 0)
93     return 0;
94
95   for (count = 0, ptr = gids_str; 
96        next_token(&ptr, s2, NULL, sizeof(s2)); 
97        count++)
98     ;
99
100   gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
101   if(!gids)
102   {
103     DEBUG(0,("make_dom_gids: malloc fail !\n"));
104     return 0;
105   }
106
107   for (count = 0, ptr = gids_str; 
108        next_token(&ptr, s2, NULL, sizeof(s2)) && 
109                count < LSA_MAX_GROUPS; 
110        count++) 
111   {
112     /* the entries are of the form GID/ATTR, ATTR being optional.*/
113     char *attr;
114     uint32 rid = 0;
115     int i;
116
117     attr = strchr(s2,'/');
118     if (attr)
119       *attr++ = 0;
120
121     if (!attr || !*attr)
122       attr = "7"; /* default value for attribute is 7 */
123
124     /* look up the RID string and see if we can turn it into a rid number */
125     for (i = 0; builtin_alias_rids[i].name != NULL; i++)
126     {
127       if (strequal(builtin_alias_rids[i].name, s2))
128       {
129         rid = builtin_alias_rids[i].rid;
130         break;
131       }
132     }
133
134     if (rid == 0)
135       rid = atoi(s2);
136
137     if (rid == 0)
138     {
139       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
140       count--;
141     }
142     else
143     {
144       gids[count].g_rid = rid;
145       gids[count].attr  = atoi(attr);
146
147       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
148     }
149   }
150
151   *ppgids = gids;
152   return count;
153 }
154
155
156 /*******************************************************************
157  gets a domain user's groups
158  ********************************************************************/
159 void get_domain_user_groups(char *domain_groups, char *user)
160 {
161         pstring tmp;
162
163         if (domain_groups == NULL || user == NULL) return;
164
165         /* any additional groups this user is in.  e.g power users */
166         pstrcpy(domain_groups, lp_domain_groups());
167
168         /* can only be a user or a guest.  cannot be guest _and_ admin */
169         if (user_in_list(user, lp_domain_guest_group()))
170         {
171                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
172                 pstrcat(domain_groups, tmp);
173
174                 DEBUG(3,("domain guest group access %s granted\n", tmp));
175         }
176         else
177         {
178                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
179                 pstrcat(domain_groups, tmp);
180
181                 DEBUG(3,("domain group access %s granted\n", tmp));
182
183                 if (user_in_list(user, lp_domain_admin_group()))
184                 {
185                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
186                         pstrcat(domain_groups, tmp);
187
188                         DEBUG(3,("domain admin group access %s granted\n", tmp));
189                 }
190         }
191 }
192
193
194 /*******************************************************************
195  lookup_group_name
196  ********************************************************************/
197 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
198 {
199         int i = 0; 
200         (*type) = SID_NAME_DOM_GRP;
201
202         DEBUG(5,("lookup_group_name: rid: %d", rid));
203
204         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
205         {
206                 i++;
207         }
208
209         if (domain_group_rids[i].rid != 0)
210         {
211                 fstrcpy(group_name, domain_group_rids[i].name);
212                 DEBUG(5,(" = %s\n", group_name));
213                 return 0x0;
214         }
215
216         DEBUG(5,(" none mapped\n"));
217         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
218 }
219
220 /*******************************************************************
221  lookup_alias_name
222  ********************************************************************/
223 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
224 {
225         int i = 0; 
226         (*type) = SID_NAME_WKN_GRP;
227
228         DEBUG(5,("lookup_alias_name: rid: %d", rid));
229
230         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
231         {
232                 i++;
233         }
234
235         if (builtin_alias_rids[i].rid != 0)
236         {
237                 fstrcpy(alias_name, builtin_alias_rids[i].name);
238                 DEBUG(5,(" = %s\n", alias_name));
239                 return 0x0;
240         }
241
242         DEBUG(5,(" none mapped\n"));
243         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
244 }
245
246 /*******************************************************************
247  lookup_user_name
248  ********************************************************************/
249 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
250 {
251         struct sam_disp_info *disp_info;
252         int i = 0;
253         (*type) = SID_NAME_USER;
254
255         DEBUG(5,("lookup_user_name: rid: %d", rid));
256
257         /* look up the well-known domain user rids first */
258         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
259         {
260                 i++;
261         }
262
263         if (domain_user_rids[i].rid != 0)
264         {
265                 fstrcpy(user_name, domain_user_rids[i].name);
266                 DEBUG(5,(" = %s\n", user_name));
267                 return 0x0;
268         }
269
270         /* ok, it's a user.  find the user account */
271         become_root(True);
272         disp_info = getsamdisprid(rid);
273         unbecome_root(True);
274
275         if (disp_info != NULL)
276         {
277                 fstrcpy(user_name, disp_info->smb_name);
278                 DEBUG(5,(" = %s\n", user_name));
279                 return 0x0;
280         }
281
282         DEBUG(5,(" none mapped\n"));
283         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
284 }
285
286 /*******************************************************************
287  lookup_group_rid
288  ********************************************************************/
289 uint32 lookup_group_rid(char *group_name, uint32 *rid)
290 {
291         char *grp_name;
292         int i = -1; /* start do loop at -1 */
293
294         do /* find, if it exists, a group rid for the group name*/
295         {
296                 i++;
297                 (*rid) = domain_group_rids[i].rid;
298                 grp_name = domain_group_rids[i].name;
299
300         } while (grp_name != NULL && !strequal(grp_name, group_name));
301
302         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
303 }
304
305 /*******************************************************************
306  lookup_alias_rid
307  ********************************************************************/
308 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
309 {
310         char *als_name;
311         int i = -1; /* start do loop at -1 */
312
313         do /* find, if it exists, a alias rid for the alias name*/
314         {
315                 i++;
316                 (*rid) = builtin_alias_rids[i].rid;
317                 als_name = builtin_alias_rids[i].name;
318
319         } while (als_name != NULL && !strequal(als_name, alias_name));
320
321         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
322 }
323
324 /*******************************************************************
325  lookup_user_rid
326  ********************************************************************/
327 uint32 lookup_user_rid(char *user_name, uint32 *rid)
328 {
329         struct sam_passwd *sam_pass;
330         (*rid) = 0;
331
332         /* find the user account */
333         become_root(True);
334         sam_pass = getsam21pwnam(user_name);
335         unbecome_root(True);
336
337         if (sam_pass != NULL)
338         {
339                 (*rid) = sam_pass->user_rid;
340                 return 0x0;
341         }
342
343         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
344 }