fixed a return value
[kai/samba.git] / source / 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 /*
43  * A list of the rids of well known BUILTIN and Domain users
44  * and groups.
45  */
46
47 rid_name builtin_alias_rids[] =
48 {  
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" },
53    
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 }
60 };
61
62 /* array lookup of well-known Domain RID users. */
63 rid_name domain_user_rids[] =
64 {  
65     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
66     { DOMAIN_USER_RID_GUEST         , "Guest" },
67     { 0                             , NULL }
68 };
69
70 /* array lookup of well-known Domain RID groups. */
71 rid_name domain_group_rids[] =
72 {  
73     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
74     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
75     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
76     { 0                             , NULL }
77 };
78
79 int make_dom_gids(TALLOC_CTX *ctx, char *gids_str, DOM_GID **ppgids)
80 {
81   char *ptr;
82   pstring s2;
83   int count;
84   DOM_GID *gids;
85
86   *ppgids = NULL;
87
88   DEBUG(4,("make_dom_gids: %s\n", gids_str));
89
90   if (gids_str == NULL || *gids_str == 0)
91     return 0;
92
93   for (count = 0, ptr = gids_str; 
94        next_token(&ptr, s2, NULL, sizeof(s2)); 
95        count++)
96     ;
97
98   gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) * count );
99   if(!gids)
100   {
101     DEBUG(0,("make_dom_gids: talloc fail !\n"));
102     return 0;
103   }
104
105   for (count = 0, ptr = gids_str; 
106        next_token(&ptr, s2, NULL, sizeof(s2)) && 
107                count < LSA_MAX_GROUPS; 
108        count++) 
109   {
110     /* the entries are of the form GID/ATTR, ATTR being optional.*/
111     char *attr;
112     uint32 rid = 0;
113     int i;
114
115     attr = strchr_m(s2,'/');
116     if (attr)
117       *attr++ = 0;
118
119     if (!attr || !*attr)
120       attr = "7"; /* default value for attribute is 7 */
121
122     /* look up the RID string and see if we can turn it into a rid number */
123     for (i = 0; builtin_alias_rids[i].name != NULL; i++)
124     {
125       if (strequal(builtin_alias_rids[i].name, s2))
126       {
127         rid = builtin_alias_rids[i].rid;
128         break;
129       }
130     }
131
132     if (rid == 0)
133       rid = atoi(s2);
134
135     if (rid == 0)
136     {
137       DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n", s2, attr));
138       count--;
139     }
140     else
141     {
142       gids[count].g_rid = rid;
143       gids[count].attr  = atoi(attr);
144
145       DEBUG(5,("group id: %d attr: %d\n", gids[count].g_rid, gids[count].attr));
146     }
147   }
148
149   *ppgids = gids;
150   return count;
151 }
152
153 /*******************************************************************
154  gets a domain user's groups
155  ********************************************************************/
156 BOOL new_get_domain_user_groups(TALLOC_CTX *ctx, int *numgroups, DOM_GID **pgids, SAM_ACCOUNT *sam_pass)
157 {
158         GROUP_MAP *map=NULL;
159         int i, num, num_entries, cur_gid=0;
160         struct group *grp;
161         DOM_GID *gids;
162         fstring user_name;
163         uint32 grid;
164         uint32 tmp_rid;
165
166         fstrcpy(user_name, pdb_get_username(sam_pass));
167         grid=pdb_get_group_rid(sam_pass);
168
169         DEBUG(10,("new_get_domain_user_groups: searching domain groups [%s] is a member of\n", user_name));
170
171         /* first get the list of the domain groups */
172         if (!enum_group_mapping(SID_NAME_DOM_GRP, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
173                 return False;
174         DEBUG(10,("new_get_domain_user_groups: there are %d mapped groups\n", num_entries));
175
176
177         /* 
178          * alloc memory. In the worse case, we alloc memory for nothing.
179          * but I prefer to alloc for nothing
180          * than reallocing everytime.
181          */
182         gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) *  num_entries);  
183
184         /* for each group, check if the user is a member of*/
185         for(i=0; i<num_entries; i++) {
186                 if ((grp=getgrgid(map[i].gid)) == NULL) {
187                         /* very weird !!! */
188                         DEBUG(5,("new_get_domain_user_groups: gid %d doesn't exist anymore !\n", (int)map[i].gid));
189                         continue;
190                 }
191
192                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
193                         if(strcmp(grp->gr_mem[num], user_name)==0) {
194                                 /* we found the user, add the group to the list */
195                                 sid_peek_rid(&map[i].sid, &(gids[cur_gid].g_rid));
196                                 gids[cur_gid].attr=map[i].sid_name_use;
197                                 DEBUG(10,("new_get_domain_user_groups: user found in group %s\n", map[i].nt_name));
198                                 cur_gid++;
199                                 break;
200                         }
201                 }
202         }
203
204         /* we have checked the groups */
205         /* we must now check the gid of the user or the primary group rid, that's the same */
206         for (i=0; i<cur_gid && grid!=gids[i].g_rid; i++)
207                 ;
208         
209         /* the user's gid is already there */
210         if (i!=cur_gid) {
211                 goto done;
212         }
213
214         for(i=0; i<num_entries; i++) {
215                 sid_peek_rid(&map[i].sid, &tmp_rid);
216                 if (tmp_rid==grid) {
217                         gids[cur_gid].g_rid=tmp_rid;
218                         gids[cur_gid].attr=map[i].sid_name_use;
219                         DEBUG(10,("new_get_domain_user_groups: primary gid of user found in group %s\n", map[i].nt_name));
220                         cur_gid++;
221                         goto done; /* leave the loop early */
222                 }
223         }
224
225  done:
226         *pgids=gids;
227         *numgroups=cur_gid;
228         safe_free(map);
229         return True;
230 }
231
232 /*******************************************************************
233  gets a domain user's groups
234  ********************************************************************/
235 void get_domain_user_groups(char *domain_groups, const char *user)
236 {
237         pstring tmp;
238
239         if (domain_groups == NULL || user == NULL) return;
240
241         /* can only be a user or a guest.  cannot be guest _and_ admin */
242         if (user_in_list(user, lp_domain_guest_group()))
243         {
244                 slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_GUESTS);
245                 pstrcat(domain_groups, tmp);
246
247                 DEBUG(3,("domain guest group access %s granted\n", tmp));
248         }
249         else
250         {
251                 slprintf(tmp, sizeof(tmp) -1, " %ld/7 ", DOMAIN_GROUP_RID_USERS);
252                 pstrcat(domain_groups, tmp);
253
254                 DEBUG(3,("domain group access %s granted\n", tmp));
255
256                 if (user_in_list(user, lp_domain_admin_group()))
257                 {
258                         slprintf(tmp, sizeof(tmp) - 1, " %ld/7 ", DOMAIN_GROUP_RID_ADMINS);
259                         pstrcat(domain_groups, tmp);
260
261                         DEBUG(3,("domain admin group access %s granted\n", tmp));
262                 }
263         }
264 }
265
266 /*******************************************************************
267  Look up a local (domain) rid and return a name and type.
268  ********************************************************************/
269 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
270 {
271         int i = 0; 
272         (*type) = SID_NAME_DOM_GRP;
273
274         DEBUG(5,("lookup_group_name: rid: %d", rid));
275
276         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
277         {
278                 i++;
279         }
280
281         if (domain_group_rids[i].rid != 0)
282         {
283                 fstrcpy(group_name, domain_group_rids[i].name);
284                 DEBUG(5,(" = %s\n", group_name));
285                 return NT_STATUS_OK;
286         }
287
288         DEBUG(5,(" none mapped\n"));
289         return NT_STATUS_NONE_MAPPED;
290 }
291
292 /*******************************************************************
293  Look up a local alias rid and return a name and type.
294  ********************************************************************/
295 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
296 {
297         int i = 0; 
298         (*type) = SID_NAME_WKN_GRP;
299
300         DEBUG(5,("lookup_alias_name: rid: %d", rid));
301
302         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
303         {
304                 i++;
305         }
306
307         if (builtin_alias_rids[i].rid != 0)
308         {
309                 fstrcpy(alias_name, builtin_alias_rids[i].name);
310                 DEBUG(5,(" = %s\n", alias_name));
311                 return NT_STATUS_OK;
312         }
313
314         DEBUG(5,(" none mapped\n"));
315         return NT_STATUS_NONE_MAPPED;
316 }
317
318 /*******************************************************************
319  Look up a local user rid and return a name and type.
320  ********************************************************************/
321 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
322 {
323         SAM_ACCOUNT *sampwd=NULL;
324         int i = 0;
325         BOOL ret;
326         
327         (*type) = SID_NAME_USER;
328
329         DEBUG(5,("lookup_user_name: rid: %d", rid));
330
331         /* look up the well-known domain user rids first */
332         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
333         {
334                 i++;
335         }
336
337         if (domain_user_rids[i].rid != 0) {
338                 fstrcpy(user_name, domain_user_rids[i].name);
339                 DEBUG(5,(" = %s\n", user_name));
340                 return NT_STATUS_OK;
341         }
342
343         pdb_init_sam(&sampwd);
344
345         /* ok, it's a user.  find the user account */
346         become_root();
347         ret = pdb_getsampwrid(sampwd, rid);
348         unbecome_root();
349
350         if (ret == True) {
351                 fstrcpy(user_name, pdb_get_username(sampwd) );
352                 DEBUG(5,(" = %s\n", user_name));
353                 pdb_free_sam(&sampwd);
354                 return NT_STATUS_OK;
355         }
356
357         DEBUG(5,(" none mapped\n"));
358         pdb_free_sam(&sampwd);
359         return NT_STATUS_NONE_MAPPED;
360 }
361
362 /*******************************************************************
363  Look up a local (domain) group name and return a rid
364  ********************************************************************/
365 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
366 {
367         char *grp_name;
368         int i = -1; /* start do loop at -1 */
369
370         do /* find, if it exists, a group rid for the group name*/
371         {
372                 i++;
373                 (*rid) = domain_group_rids[i].rid;
374                 grp_name = domain_group_rids[i].name;
375
376         } while (grp_name != NULL && !strequal(grp_name, group_name));
377
378         return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
379 }
380
381 /*******************************************************************
382  Look up a local (BUILTIN) alias name and return a rid
383  ********************************************************************/
384 NTSTATUS local_lookup_alias_rid(char *alias_name, uint32 *rid)
385 {
386         char *als_name;
387         int i = -1; /* start do loop at -1 */
388
389         do /* find, if it exists, a alias rid for the alias name*/
390         {
391                 i++;
392                 (*rid) = builtin_alias_rids[i].rid;
393                 als_name = builtin_alias_rids[i].name;
394
395         } while (als_name != NULL && !strequal(als_name, alias_name));
396
397         return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
398 }
399
400 /*******************************************************************
401  Look up a local user name and return a rid
402  ********************************************************************/
403 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
404 {
405         SAM_ACCOUNT *sampass=NULL;
406         BOOL ret;
407
408         (*rid) = 0;
409
410         pdb_init_sam(&sampass);
411
412         /* find the user account */
413         become_root();
414         ret = pdb_getsampwnam(sampass, user_name);
415         unbecome_root();
416
417         if (ret == True) {
418                 (*rid) = pdb_get_user_rid(sampass);
419                 pdb_free_sam(&sampass);
420                 return NT_STATUS_OK;
421         }
422
423         pdb_free_sam(&sampass);
424         return NT_STATUS_NONE_MAPPED;
425 }