added the following message to all dce/rpc client/server code, except
[samba.git] / source3 / rpc_server / srv_util.c
1
2 /*
3  * THIS CODE IS OUT-OF-DATE BY TWO YEARS, IS LEGACY DESIGN AND VERY, VERY,
4  * INCOMPLETE.  PLEASE DO NOT MAKE ANY FURTHER ENHANCEMENTS TO THIS CODE
5  * UNLESS THEY ARE ALSO CARRIED OUT IN THE SAMBA_TNG BRANCH.
6  *
7  * PLEASE DO NOT TREAT THIS CODE AS AUTHORITATIVE IN *ANY* WAY.
8  *
9  * REPEAT, PLEASE DO NOT MAKE ANY MODIFICATIONS TO THIS CODE WITHOUT
10  * FIRST CHECKING THE EQUIVALENT MODULE IN SAMBA_TNG, UPDATING THAT
11  * FIRST, *THEN* CONSIDER MAKING THE SAME MODIFICATION IN THIS BRANCH
12  *
13  * YOU WILL, ALMOST GUARANTEED, FIND THAT THE BUG-FIX OR ENHANCEMENT THAT
14  * YOU THINK IS NECESSARY, HAS ALREADY BEEN IMPLEMENTED IN SAMBA_TNG.
15  * IF IT HAS NOT, YOUR BUG-FIX OR ENHANCEMENT *MUST* GO INTO SAMBA_TNG
16  * AS THE SAMBA_TNG CODE WILL REPLACE THIS MODULE WITHOUT REFERENCE TO
17  * ANYTHING IN IT, WITH THE POSSIBLE RISK THAT THE BUG-FIX OR ENHANCEMENT
18  * MAY BE LOST.
19  *
20  * PLEASE OBSERVE AND RESPECT THIS SIMPLE REQUEST.
21  *
22  * THANK YOU.
23  *
24  * lkcl@samba.org
25  */
26
27 /* 
28  *  Unix SMB/Netbios implementation.
29  *  Version 1.9.
30  *  RPC Pipe client / server routines
31  *  Copyright (C) Andrew Tridgell              1992-1998
32  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
33  *  Copyright (C) Paul Ashton                  1997-1998.
34  *  
35  *  This program is free software; you can redistribute it and/or modify
36  *  it under the terms of the GNU General Public License as published by
37  *  the Free Software Foundation; either version 2 of the License, or
38  *  (at your option) any later version.
39  *  
40  *  This program is distributed in the hope that it will be useful,
41  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
42  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43  *  GNU General Public License for more details.
44  *  
45  *  You should have received a copy of the GNU General Public License
46  *  along with this program; if not, write to the Free Software
47  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48  */
49
50 /*  this module apparently provides an implementation of DCE/RPC over a
51  *  named pipe (IPC$ connection using SMBtrans).  details of DCE/RPC
52  *  documentation are available (in on-line form) from the X-Open group.
53  *
54  *  this module should provide a level of abstraction between SMB
55  *  and DCE/RPC, while minimising the amount of mallocs, unnecessary
56  *  data copies, and network traffic.
57  *
58  *  in this version, which takes a "let's learn what's going on and
59  *  get something running" approach, there is additional network
60  *  traffic generated, but the code should be easier to understand...
61  *
62  *  ... if you read the docs.  or stare at packets for weeks on end.
63  *
64  */
65
66 #include "includes.h"
67 #include "nterr.h"
68
69 extern int DEBUGLEVEL;
70
71 /*
72  * A list of the rids of well known BUILTIN and Domain users
73  * and groups.
74  */
75
76 rid_name builtin_alias_rids[] =
77 {  
78     { BUILTIN_ALIAS_RID_ADMINS       , "Administrators" },
79     { BUILTIN_ALIAS_RID_USERS        , "Users" },
80     { BUILTIN_ALIAS_RID_GUESTS       , "Guests" },
81     { BUILTIN_ALIAS_RID_POWER_USERS  , "Power Users" },
82    
83     { BUILTIN_ALIAS_RID_ACCOUNT_OPS  , "Account Operators" },
84     { BUILTIN_ALIAS_RID_SYSTEM_OPS   , "System Operators" },
85     { BUILTIN_ALIAS_RID_PRINT_OPS    , "Print Operators" },
86     { BUILTIN_ALIAS_RID_BACKUP_OPS   , "Backup Operators" },
87     { BUILTIN_ALIAS_RID_REPLICATOR   , "Replicator" },
88     { 0                             , NULL }
89 };
90
91 /* array lookup of well-known Domain RID users. */
92 rid_name domain_user_rids[] =
93 {  
94     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
95     { DOMAIN_USER_RID_GUEST         , "Guest" },
96     { 0                             , NULL }
97 };
98
99 /* array lookup of well-known Domain RID groups. */
100 rid_name domain_group_rids[] =
101 {  
102     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
103     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
104     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
105     { 0                             , NULL }
106 };
107
108 int make_dom_gids(char *gids_str, DOM_GID **ppgids)
109 {
110   char *ptr;
111   pstring s2;
112   int count;
113   DOM_GID *gids;
114
115   *ppgids = NULL;
116
117   DEBUG(4,("make_dom_gids: %s\n", gids_str));
118
119   if (gids_str == NULL || *gids_str == 0)
120     return 0;
121
122   for (count = 0, ptr = gids_str; 
123        next_token(&ptr, s2, NULL, sizeof(s2)); 
124        count++)
125     ;
126
127   gids = (DOM_GID *)malloc( sizeof(DOM_GID) * count );
128   if(!gids)
129   {
130     DEBUG(0,("make_dom_gids: malloc fail !\n"));
131     return 0;
132   }
133
134   for (count = 0, ptr = gids_str; 
135        next_token(&ptr, s2, NULL, sizeof(s2)) && 
136                count < LSA_MAX_GROUPS; 
137        count++) 
138   {
139     /* the entries are of the form GID/ATTR, ATTR being optional.*/
140     char *attr;
141     uint32 rid = 0;
142     int i;
143
144     attr = strchr(s2,'/');
145     if (attr)
146       *attr++ = 0;
147
148     if (!attr || !*attr)
149       attr = "7"; /* default value for attribute is 7 */
150
151     /* look up the RID string and see if we can turn it into a rid number */
152     for (i = 0; builtin_alias_rids[i].name != NULL; i++)
153     {
154       if (strequal(builtin_alias_rids[i].name, s2))
155       {
156         rid = builtin_alias_rids[i].rid;
157         break;
158       }
159     }
160
161     if (rid == 0)
162       rid = atoi(s2);
163
164     if (rid == 0)
165     {
166       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
167       count--;
168     }
169     else
170     {
171       gids[count].g_rid = rid;
172       gids[count].attr  = atoi(attr);
173
174       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
175     }
176   }
177
178   *ppgids = gids;
179   return count;
180 }
181
182
183 /*******************************************************************
184  gets a domain user's groups
185  ********************************************************************/
186 void get_domain_user_groups(char *domain_groups, char *user)
187 {
188         pstring tmp;
189
190         if (domain_groups == NULL || user == NULL) return;
191
192         /* any additional groups this user is in.  e.g power users */
193         pstrcpy(domain_groups, lp_domain_groups());
194
195         /* can only be a user or a guest.  cannot be guest _and_ admin */
196         if (user_in_list(user, lp_domain_guest_group()))
197         {
198                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
199                 pstrcat(domain_groups, tmp);
200
201                 DEBUG(3,("domain guest group access %s granted\n", tmp));
202         }
203         else
204         {
205                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
206                 pstrcat(domain_groups, tmp);
207
208                 DEBUG(3,("domain group access %s granted\n", tmp));
209
210                 if (user_in_list(user, lp_domain_admin_group()))
211                 {
212                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
213                         pstrcat(domain_groups, tmp);
214
215                         DEBUG(3,("domain admin group access %s granted\n", tmp));
216                 }
217         }
218 }
219
220
221 /*******************************************************************
222  lookup_group_name
223  ********************************************************************/
224 uint32 lookup_group_name(uint32 rid, char *group_name, uint32 *type)
225 {
226         int i = 0; 
227         (*type) = SID_NAME_DOM_GRP;
228
229         DEBUG(5,("lookup_group_name: rid: %d", rid));
230
231         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
232         {
233                 i++;
234         }
235
236         if (domain_group_rids[i].rid != 0)
237         {
238                 fstrcpy(group_name, domain_group_rids[i].name);
239                 DEBUG(5,(" = %s\n", group_name));
240                 return 0x0;
241         }
242
243         DEBUG(5,(" none mapped\n"));
244         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
245 }
246
247 /*******************************************************************
248  lookup_alias_name
249  ********************************************************************/
250 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
251 {
252         int i = 0; 
253         (*type) = SID_NAME_WKN_GRP;
254
255         DEBUG(5,("lookup_alias_name: rid: %d", rid));
256
257         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
258         {
259                 i++;
260         }
261
262         if (builtin_alias_rids[i].rid != 0)
263         {
264                 fstrcpy(alias_name, builtin_alias_rids[i].name);
265                 DEBUG(5,(" = %s\n", alias_name));
266                 return 0x0;
267         }
268
269         DEBUG(5,(" none mapped\n"));
270         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
271 }
272
273 /*******************************************************************
274  lookup_user_name
275  ********************************************************************/
276 uint32 lookup_user_name(uint32 rid, char *user_name, uint32 *type)
277 {
278         struct sam_disp_info *disp_info;
279         int i = 0;
280         (*type) = SID_NAME_USER;
281
282         DEBUG(5,("lookup_user_name: rid: %d", rid));
283
284         /* look up the well-known domain user rids first */
285         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
286         {
287                 i++;
288         }
289
290         if (domain_user_rids[i].rid != 0)
291         {
292                 fstrcpy(user_name, domain_user_rids[i].name);
293                 DEBUG(5,(" = %s\n", user_name));
294                 return 0x0;
295         }
296
297         /* ok, it's a user.  find the user account */
298         become_root(True);
299         disp_info = getsamdisprid(rid);
300         unbecome_root(True);
301
302         if (disp_info != NULL)
303         {
304                 fstrcpy(user_name, disp_info->smb_name);
305                 DEBUG(5,(" = %s\n", user_name));
306                 return 0x0;
307         }
308
309         DEBUG(5,(" none mapped\n"));
310         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
311 }
312
313 /*******************************************************************
314  lookup_group_rid
315  ********************************************************************/
316 uint32 lookup_group_rid(char *group_name, uint32 *rid)
317 {
318         char *grp_name;
319         int i = -1; /* start do loop at -1 */
320
321         do /* find, if it exists, a group rid for the group name*/
322         {
323                 i++;
324                 (*rid) = domain_group_rids[i].rid;
325                 grp_name = domain_group_rids[i].name;
326
327         } while (grp_name != NULL && !strequal(grp_name, group_name));
328
329         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
330 }
331
332 /*******************************************************************
333  lookup_alias_rid
334  ********************************************************************/
335 uint32 lookup_alias_rid(char *alias_name, uint32 *rid)
336 {
337         char *als_name;
338         int i = -1; /* start do loop at -1 */
339
340         do /* find, if it exists, a alias rid for the alias name*/
341         {
342                 i++;
343                 (*rid) = builtin_alias_rids[i].rid;
344                 als_name = builtin_alias_rids[i].name;
345
346         } while (als_name != NULL && !strequal(als_name, alias_name));
347
348         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
349 }
350
351 /*******************************************************************
352  lookup_user_rid
353  ********************************************************************/
354 uint32 lookup_user_rid(char *user_name, uint32 *rid)
355 {
356         struct sam_passwd *sam_pass;
357         (*rid) = 0;
358
359         /* find the user account */
360         become_root(True);
361         sam_pass = getsam21pwnam(user_name);
362         unbecome_root(True);
363
364         if (sam_pass != NULL)
365         {
366                 (*rid) = sam_pass->user_rid;
367                 return 0x0;
368         }
369
370         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
371 }