first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source / rpc_server / srv_util.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
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.
9  *  
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.
14  *  
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.
19  *  
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.
23  */
24
25 /*  this module apparently provides an implementation of DCE/RPC over a
26  *  named pipe (IPC$ connection using SMBtrans).  details of DCE/RPC
27  *  documentation are available (in on-line form) from the X-Open group.
28  *
29  *  this module should provide a level of abstraction between SMB
30  *  and DCE/RPC, while minimising the amount of mallocs, unnecessary
31  *  data copies, and network traffic.
32  *
33  *  in this version, which takes a "let's learn what's going on and
34  *  get something running" approach, there is additional network
35  *  traffic generated, but the code should be easier to understand...
36  *
37  *  ... if you read the docs.  or stare at packets for weeks on end.
38  *
39  */
40
41 #include "includes.h"
42 #include "nterr.h"
43
44 extern int DEBUGLEVEL;
45
46 /*
47  * A list of the rids of well known BUILTIN and Domain users
48  * and groups.
49  */
50
51 rid_name builtin_alias_rids[] =
52 {  
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" },
57    
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 }
64 };
65
66 /* array lookup of well-known Domain RID users. */
67 rid_name domain_user_rids[] =
68 {  
69     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
70     { DOMAIN_USER_RID_GUEST         , "Guest" },
71     { 0                             , NULL }
72 };
73
74 /* array lookup of well-known Domain RID groups. */
75 rid_name domain_group_rids[] =
76 {  
77     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
78     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
79     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
80     { 0                             , NULL }
81 };
82
83 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
84 {
85   char *ptr;
86   pstring s2;
87   int count;
88   DOM_GID *gids;
89
90   *ppgids = NULL;
91
92   DEBUG(4,("make_dom_gids: %s\n", gids_str));
93
94   if (gids_str == NULL || *gids_str == 0)
95     return 0;
96
97   for (count = 0, ptr = gids_str; 
98        next_token(&ptr, s2, NULL, sizeof(s2)); 
99        count++)
100     ;
101
102   gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
103   if(!gids)
104   {
105     DEBUG(0,("make_dom_gids: malloc fail !\n"));
106     return 0;
107   }
108
109   for (count = 0, ptr = gids_str; 
110        next_token(&ptr, s2, NULL, sizeof(s2)) && 
111                count < LSA_MAX_GROUPS; 
112        count++) 
113   {
114     /* the entries are of the form GID/ATTR, ATTR being optional.*/
115     char *attr;
116     uint32 rid = 0;
117     int i;
118
119     attr = strchr(s2,'/');
120     if (attr)
121       *attr++ = 0;
122
123     if (!attr || !*attr)
124       attr = "7"; /* default value for attribute is 7 */
125
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++)
128     {
129       if (strequal(builtin_alias_rids[i].name, s2))
130       {
131         rid = builtin_alias_rids[i].rid;
132         break;
133       }
134     }
135
136     if (rid == 0)
137       rid = atoi(s2);
138
139     if (rid == 0)
140     {
141       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
142       count--;
143     }
144     else
145     {
146       gids[count].g_rid = rid;
147       gids[count].attr  = atoi(attr);
148
149       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
150     }
151   }
152
153   *ppgids = gids;
154   return count;
155 }
156
157
158 /*******************************************************************
159  gets a domain user's groups
160  ********************************************************************/
161 void get_domain_user_groups(char *domain_groups, char *user)
162 {
163         pstring tmp;
164
165         if (domain_groups == NULL || user == NULL) return;
166
167         /* any additional groups this user is in.  e.g power users */
168         pstrcpy(domain_groups, lp_domain_groups());
169
170         /* can only be a user or a guest.  cannot be guest _and_ admin */
171         if (user_in_list(user, lp_domain_guest_group()))
172         {
173                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
174                 pstrcat(domain_groups, tmp);
175
176                 DEBUG(3,("domain guest group access %s granted\n", tmp));
177         }
178         else
179         {
180                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
181                 pstrcat(domain_groups, tmp);
182
183                 DEBUG(3,("domain group access %s granted\n", tmp));
184
185                 if (user_in_list(user, lp_domain_admin_group()))
186                 {
187                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
188                         pstrcat(domain_groups, tmp);
189
190                         DEBUG(3,("domain admin group access %s granted\n", tmp));
191                 }
192         }
193 }
194
195
196 /*******************************************************************
197  lookup_group_name
198  ********************************************************************/
199 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
200 {
201         int i = 0; 
202         (*type) = SID_NAME_DOM_GRP;
203
204         DEBUG(5,("lookup_group_name: rid: %d", rid));
205
206         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
207         {
208                 i++;
209         }
210
211         if (domain_group_rids[i].rid != 0)
212         {
213                 fstrcpy(group_name, domain_group_rids[i].name);
214                 DEBUG(5,(" = %s\n", group_name));
215                 return 0x0;
216         }
217
218         DEBUG(5,(" none mapped\n"));
219         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
220 }
221
222 /*******************************************************************
223  lookup_alias_name
224  ********************************************************************/
225 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
226 {
227         int i = 0; 
228         (*type) = SID_NAME_WKN_GRP;
229
230         DEBUG(5,("lookup_alias_name: rid: %d", rid));
231
232         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
233         {
234                 i++;
235         }
236
237         if (builtin_alias_rids[i].rid != 0)
238         {
239                 fstrcpy(alias_name, builtin_alias_rids[i].name);
240                 DEBUG(5,(" = %s\n", alias_name));
241                 return 0x0;
242         }
243
244         DEBUG(5,(" none mapped\n"));
245         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
246 }
247
248 /*******************************************************************
249  lookup_user_name
250  ********************************************************************/
251 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
252 {
253         struct sam_disp_info *disp_info;
254         int i = 0;
255         (*type) = SID_NAME_USER;
256
257         DEBUG(5,("lookup_user_name: rid: %d", rid));
258
259         /* look up the well-known domain user rids first */
260         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
261         {
262                 i++;
263         }
264
265         if (domain_user_rids[i].rid != 0)
266         {
267                 fstrcpy(user_name, domain_user_rids[i].name);
268                 DEBUG(5,(" = %s\n", user_name));
269                 return 0x0;
270         }
271
272         /* ok, it's a user.  find the user account */
273         become_root(True);
274         disp_info = getsamdisprid(rid);
275         unbecome_root(True);
276
277         if (disp_info != NULL)
278         {
279                 fstrcpy(user_name, disp_info->smb_name);
280                 DEBUG(5,(" = %s\n", user_name));
281                 return 0x0;
282         }
283
284         DEBUG(5,(" none mapped\n"));
285         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
286 }
287
288 /*******************************************************************
289  lookup_group_rid
290  ********************************************************************/
291 uint32 lookup_group_rid(char *group_name, uint32 *rid)
292 {
293         char *grp_name;
294         int i = -1; /* start do loop at -1 */
295
296         do /* find, if it exists, a group rid for the group name*/
297         {
298                 i++;
299                 (*rid) = domain_group_rids[i].rid;
300                 grp_name = domain_group_rids[i].name;
301
302         } while (grp_name != NULL && !strequal(grp_name, group_name));
303
304         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
305 }
306
307 /*******************************************************************
308  lookup_alias_rid
309  ********************************************************************/
310 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
311 {
312         char *als_name;
313         int i = -1; /* start do loop at -1 */
314
315         do /* find, if it exists, a alias rid for the alias name*/
316         {
317                 i++;
318                 (*rid) = builtin_alias_rids[i].rid;
319                 als_name = builtin_alias_rids[i].name;
320
321         } while (als_name != NULL && !strequal(als_name, alias_name));
322
323         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
324 }
325
326 /*******************************************************************
327  lookup_user_rid
328  ********************************************************************/
329 uint32 lookup_user_rid(char *user_name, uint32 *rid)
330 {
331         struct sam_passwd *sam_pass;
332         (*rid) = 0;
333
334         /* find the user account */
335         become_root(True);
336         sam_pass = getsam21pwnam(user_name);
337         unbecome_root(True);
338
339         if (sam_pass != NULL)
340         {
341                 (*rid) = sam_pass->user_rid;
342                 return 0x0;
343         }
344
345         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
346 }