This (hopefully) fixes a bug reported by Kai Krueger <kai@kruegernetz.de>
[kai/samba.git] / source / rpc_server / srv_util.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1998
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
6  *  Copyright (C) Paul Ashton                  1997-1998.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /*  this module apparently provides an implementation of DCE/RPC over a
24  *  named pipe (IPC$ connection using SMBtrans).  details of DCE/RPC
25  *  documentation are available (in on-line form) from the X-Open group.
26  *
27  *  this module should provide a level of abstraction between SMB
28  *  and DCE/RPC, while minimising the amount of mallocs, unnecessary
29  *  data copies, and network traffic.
30  *
31  *  in this version, which takes a "let's learn what's going on and
32  *  get something running" approach, there is additional network
33  *  traffic generated, but the code should be easier to understand...
34  *
35  *  ... if you read the docs.  or stare at packets for weeks on end.
36  *
37  */
38
39 #include "includes.h"
40
41 /*
42  * A list of the rids of well known BUILTIN and Domain users
43  * and groups.
44  */
45
46 rid_name builtin_alias_rids[] =
47 {  
48     { BUILTIN_ALIAS_RID_ADMINS       , "Administrators" },
49     { BUILTIN_ALIAS_RID_USERS        , "Users" },
50     { BUILTIN_ALIAS_RID_GUESTS       , "Guests" },
51     { BUILTIN_ALIAS_RID_POWER_USERS  , "Power Users" },
52    
53     { BUILTIN_ALIAS_RID_ACCOUNT_OPS  , "Account Operators" },
54     { BUILTIN_ALIAS_RID_SYSTEM_OPS   , "System Operators" },
55     { BUILTIN_ALIAS_RID_PRINT_OPS    , "Print Operators" },
56     { BUILTIN_ALIAS_RID_BACKUP_OPS   , "Backup Operators" },
57     { BUILTIN_ALIAS_RID_REPLICATOR   , "Replicator" },
58     { 0                             , NULL }
59 };
60
61 /* array lookup of well-known Domain RID users. */
62 rid_name domain_user_rids[] =
63 {  
64     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
65     { DOMAIN_USER_RID_GUEST         , "Guest" },
66     { 0                             , NULL }
67 };
68
69 /* array lookup of well-known Domain RID groups. */
70 rid_name domain_group_rids[] =
71 {  
72     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
73     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
74     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
75     { 0                             , NULL }
76 };
77
78 /*******************************************************************
79  gets a domain user's groups
80  ********************************************************************/
81 NTSTATUS get_alias_user_groups(TALLOC_CTX *ctx, DOM_SID *sid, int *numgroups, uint32 **prids, DOM_SID *q_sid)
82 {
83         SAM_ACCOUNT *sam_pass=NULL;
84         struct sys_grent *glist;
85         struct sys_grent *grp;
86         int i, num, cur_rid=0;
87         gid_t gid;
88         GROUP_MAP map;
89         DOM_SID tmp_sid;
90         fstring user_name;
91         fstring str_domsid, str_qsid;
92         uint32 rid,grid;
93         uint32 *rids=NULL, *new_rids=NULL;
94         gid_t winbind_gid_low, winbind_gid_high;
95         BOOL ret;
96         BOOL winbind_groups_exist;
97
98         /*
99          * this code is far from perfect.
100          * first it enumerates the full /etc/group and that can be slow.
101          * second, it works only with users' SIDs
102          * whereas the day we support nested groups, it will have to
103          * support both users's SIDs and domain groups' SIDs
104          *
105          * having our own ldap backend would be so much faster !
106          * we're far from that, but hope one day ;-) JFM.
107          */
108
109         *prids=NULL;
110         *numgroups=0;
111
112         winbind_groups_exist = lp_winbind_gid(&winbind_gid_low, &winbind_gid_high);
113
114
115         DEBUG(10,("get_alias_user_groups: looking if SID %s is a member of groups in the SID domain %s\n", 
116                   sid_to_string(str_qsid, q_sid), sid_to_string(str_domsid, sid)));
117
118         pdb_init_sam(&sam_pass);
119         become_root();
120         ret = pdb_getsampwsid(sam_pass, q_sid);
121         unbecome_root();
122         if (ret == False) {
123                 pdb_free_sam(&sam_pass);
124                 return NT_STATUS_NO_SUCH_USER;
125         }
126
127         fstrcpy(user_name, pdb_get_username(sam_pass));
128         grid=pdb_get_group_rid(sam_pass);
129         gid=pdb_get_gid(sam_pass);
130         
131         grp = glist = getgrent_list();
132         if (grp == NULL) {
133                 pdb_free_sam(&sam_pass);
134                 return NT_STATUS_NO_MEMORY;
135         }
136         
137         for (; grp != NULL; grp = grp->next) {
138                 if(!get_group_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)) {
139                         DEBUG(10,("get_alias_user_groups: gid %d. not found\n", (int)grp->gr_gid));
140                         continue;
141                 }
142                 
143                 /* if it's not an alias, continue */
144                 if (map.sid_name_use!=SID_NAME_ALIAS) {
145                         DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
146                         continue;
147                 }
148
149                 sid_copy(&tmp_sid, &map.sid);
150                 sid_split_rid(&tmp_sid, &rid);
151                 
152                 /* if the sid is not in the correct domain, continue */
153                 if (!sid_equal(&tmp_sid, sid)) {
154                         DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
155                         continue;
156                 }
157
158                 /* Don't return winbind groups as they are not local! */
159                 if (winbind_groups_exist && (grp->gr_gid >= winbind_gid_low) && (grp->gr_gid <= winbind_gid_high)) {
160                         DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name));
161                         continue;
162                 }
163
164                 /* Don't return user private groups... */
165                 if (Get_Pwnam(map.nt_name) != 0) {
166                         DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name));
167                         continue;                       
168                 }
169                 
170                 /* the group is fine, we can check if there is the user we're looking for */
171                 DEBUG(10,("get_alias_user_groups: checking if the user is a member of %s.\n", map.nt_name));
172                 
173                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
174                         if(strcmp(grp->gr_mem[num], user_name)==0) {
175                                 /* we found the user, add the group to the list */
176                                 
177                                 new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
178                                 if (new_rids==NULL) {
179                                         DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
180                                         pdb_free_sam(&sam_pass);
181                                         return NT_STATUS_NO_MEMORY;
182                                 }
183                                 rids=new_rids;
184                                 
185                                 sid_peek_rid(&map.sid, &(rids[cur_rid]));
186                                 DEBUG(10,("get_alias_user_groups: user found in group %s\n", map.nt_name));
187                                 cur_rid++;
188                                 break;
189                         }
190                 }
191         }
192
193         grent_free(glist);
194
195         /* now check for the user's gid (the primary group rid) */
196         for (i=0; i<cur_rid && grid!=rids[i]; i++)
197                 ;
198
199         /* the user's gid is already there */
200         if (i!=cur_rid) {
201                 DEBUG(10,("get_alias_user_groups: user is already in the list. good.\n"));
202                 goto done;
203         }
204
205         DEBUG(10,("get_alias_user_groups: looking for gid %d of user %s\n", (int)gid, user_name));
206
207         if(!get_group_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
208                 DEBUG(0,("get_alias_user_groups: gid of user %s doesn't exist. Check your /etc/passwd and /etc/group files\n", user_name));
209                 goto done;
210         }       
211
212         /* the primary group isn't an alias */
213         if (map.sid_name_use!=SID_NAME_ALIAS) {
214                 DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
215                 goto done;
216         }
217
218         sid_copy(&tmp_sid, &map.sid);
219         sid_split_rid(&tmp_sid, &rid);
220
221         /* if the sid is not in the correct domain, continue */
222         if (!sid_equal(&tmp_sid, sid)) {
223                 DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
224                 goto done;
225         }
226
227         /* Don't return winbind groups as they are not local! */
228         if (winbind_groups_exist && (gid >= winbind_gid_low) && (gid <= winbind_gid_high)) {
229                 DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name ));
230                 goto done;
231         }
232
233         /* Don't return user private groups... */
234         if (Get_Pwnam(map.nt_name) != 0) {
235                 DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name ));
236                 goto done;                      
237         }
238
239         new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
240         if (new_rids==NULL) {
241                 DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
242                 pdb_free_sam(&sam_pass);
243                 return NT_STATUS_NO_MEMORY;
244         }
245         rids=new_rids;
246
247         sid_peek_rid(&map.sid, &(rids[cur_rid]));
248         cur_rid++;
249
250 done:
251         *prids=rids;
252         *numgroups=cur_rid;
253         pdb_free_sam(&sam_pass);
254
255         return NT_STATUS_OK;
256 }
257
258
259 /*******************************************************************
260  gets a domain user's groups
261  ********************************************************************/
262 BOOL get_domain_user_groups(TALLOC_CTX *ctx, int *numgroups, DOM_GID **pgids, SAM_ACCOUNT *sam_pass)
263 {
264         GROUP_MAP *map=NULL;
265         int i, num, num_entries, cur_gid=0;
266         struct group *grp;
267         DOM_GID *gids;
268         fstring user_name;
269         uint32 grid;
270         uint32 tmp_rid;
271
272         *numgroups= 0;
273
274         fstrcpy(user_name, pdb_get_username(sam_pass));
275         grid=pdb_get_group_rid(sam_pass);
276
277         DEBUG(10,("get_domain_user_groups: searching domain groups [%s] is a member of\n", user_name));
278
279         /* first get the list of the domain groups */
280         if (!enum_group_mapping(SID_NAME_DOM_GRP, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
281                 return False;
282         DEBUG(10,("get_domain_user_groups: there are %d mapped groups\n", num_entries));
283
284         /* 
285          * alloc memory. In the worse case, we alloc memory for nothing.
286          * but I prefer to alloc for nothing
287          * than reallocing everytime.
288          */
289         gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) *  num_entries);  
290
291         /* for each group, check if the user is a member of*/
292         for(i=0; i<num_entries; i++) {
293                 if ((grp=getgrgid(map[i].gid)) == NULL) {
294                         /* very weird !!! */
295                         DEBUG(5,("get_domain_user_groups: gid %d doesn't exist anymore !\n", (int)map[i].gid));
296                         continue;
297                 }
298
299                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
300                         if(strcmp(grp->gr_mem[num], user_name)==0) {
301                                 /* we found the user, add the group to the list */
302                                 sid_peek_rid(&map[i].sid, &(gids[cur_gid].g_rid));
303                                 gids[cur_gid].attr=7;
304                                 DEBUG(10,("get_domain_user_groups: user found in group %s\n", map[i].nt_name));
305                                 cur_gid++;
306                                 break;
307                         }
308                 }
309         }
310
311         /* we have checked the groups */
312         /* we must now check the gid of the user or the primary group rid, that's the same */
313         for (i=0; i<cur_gid && grid!=gids[i].g_rid; i++)
314                 ;
315         
316         /* the user's gid is already there */
317         if (i!=cur_gid) {
318                 /* 
319                  * the primary group of the user but be the first one in the list
320                  * don't ask ! JFM.
321                  */
322                 gids[i].g_rid=gids[0].g_rid;
323                 gids[0].g_rid=grid;
324                 goto done;
325         }
326
327         for(i=0; i<num_entries; i++) {
328                 sid_peek_rid(&map[i].sid, &tmp_rid);
329                 if (tmp_rid==grid) {
330                         /* 
331                          * the primary group of the user but be the first one in the list
332                          * don't ask ! JFM.
333                          */
334                         gids[cur_gid].g_rid=gids[0].g_rid;
335                         gids[0].g_rid=tmp_rid;
336                         gids[cur_gid].attr=7;
337                         DEBUG(10,("get_domain_user_groups: primary gid of user found in group %s\n", map[i].nt_name));
338                         cur_gid++;
339                         goto done; /* leave the loop early */
340                 }
341         }
342
343         DEBUG(0,("get_domain_user_groups: primary gid of user [%s] is not a Domain group !\n", user_name));
344         DEBUGADD(0,("get_domain_user_groups: You should fix it, NT doesn't like that\n"));
345
346  done:
347         *pgids=gids;
348         *numgroups=cur_gid;
349         safe_free(map);
350
351         return True;
352 }
353
354 /*******************************************************************
355  Look up a local (domain) rid and return a name and type.
356  ********************************************************************/
357 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
358 {
359         int i = 0; 
360         (*type) = SID_NAME_DOM_GRP;
361
362         DEBUG(5,("lookup_group_name: rid: %d", rid));
363
364         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
365         {
366                 i++;
367         }
368
369         if (domain_group_rids[i].rid != 0)
370         {
371                 fstrcpy(group_name, domain_group_rids[i].name);
372                 DEBUG(5,(" = %s\n", group_name));
373                 return NT_STATUS_OK;
374         }
375
376         DEBUG(5,(" none mapped\n"));
377         return NT_STATUS_NONE_MAPPED;
378 }
379
380 /*******************************************************************
381  Look up a local alias rid and return a name and type.
382  ********************************************************************/
383 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
384 {
385         int i = 0; 
386         (*type) = SID_NAME_WKN_GRP;
387
388         DEBUG(5,("lookup_alias_name: rid: %d", rid));
389
390         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
391         {
392                 i++;
393         }
394
395         if (builtin_alias_rids[i].rid != 0)
396         {
397                 fstrcpy(alias_name, builtin_alias_rids[i].name);
398                 DEBUG(5,(" = %s\n", alias_name));
399                 return NT_STATUS_OK;
400         }
401
402         DEBUG(5,(" none mapped\n"));
403         return NT_STATUS_NONE_MAPPED;
404 }
405
406
407 #if 0 /*Nobody uses this function just now*/
408 /*******************************************************************
409  Look up a local user rid and return a name and type.
410  ********************************************************************/
411 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
412 {
413         SAM_ACCOUNT *sampwd=NULL;
414         int i = 0;
415         BOOL ret;
416         
417         (*type) = SID_NAME_USER;
418
419         DEBUG(5,("lookup_user_name: rid: %d", rid));
420
421         /* look up the well-known domain user rids first */
422         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
423         {
424                 i++;
425         }
426
427         if (domain_user_rids[i].rid != 0) {
428                 fstrcpy(user_name, domain_user_rids[i].name);
429                 DEBUG(5,(" = %s\n", user_name));
430                 return NT_STATUS_OK;
431         }
432
433         pdb_init_sam(&sampwd);
434
435         /* ok, it's a user.  find the user account */
436         become_root();
437         ret = pdb_getsampwrid(sampwd, rid);
438         unbecome_root();
439
440         if (ret == True) {
441                 fstrcpy(user_name, pdb_get_username(sampwd) );
442                 DEBUG(5,(" = %s\n", user_name));
443                 pdb_free_sam(&sampwd);
444                 return NT_STATUS_OK;
445         }
446
447         DEBUG(5,(" none mapped\n"));
448         pdb_free_sam(&sampwd);
449         return NT_STATUS_NONE_MAPPED;
450 }
451
452 #endif
453
454 /*******************************************************************
455  Look up a local (domain) group name and return a rid
456  ********************************************************************/
457 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
458 {
459         char *grp_name;
460         int i = -1; /* start do loop at -1 */
461
462         do /* find, if it exists, a group rid for the group name*/
463         {
464                 i++;
465                 (*rid) = domain_group_rids[i].rid;
466                 grp_name = domain_group_rids[i].name;
467
468         } while (grp_name != NULL && !strequal(grp_name, group_name));
469
470         return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
471 }
472
473 /*******************************************************************
474  Look up a local (BUILTIN) alias name and return a rid
475  ********************************************************************/
476 NTSTATUS local_lookup_alias_rid(char *alias_name, uint32 *rid)
477 {
478         char *als_name;
479         int i = -1; /* start do loop at -1 */
480
481         do /* find, if it exists, a alias rid for the alias name*/
482         {
483                 i++;
484                 (*rid) = builtin_alias_rids[i].rid;
485                 als_name = builtin_alias_rids[i].name;
486
487         } while (als_name != NULL && !strequal(als_name, alias_name));
488
489         return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
490 }
491
492 /*******************************************************************
493  Look up a local user name and return a rid
494  ********************************************************************/
495 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
496 {
497         SAM_ACCOUNT *sampass=NULL;
498         BOOL ret;
499
500         (*rid) = 0;
501
502         pdb_init_sam(&sampass);
503
504         /* find the user account */
505         become_root();
506         ret = pdb_getsampwnam(sampass, user_name);
507         unbecome_root();
508
509         if (ret == True) {
510                 (*rid) = pdb_get_user_rid(sampass);
511                 pdb_free_sam(&sampass);
512                 return NT_STATUS_OK;
513         }
514
515         pdb_free_sam(&sampass);
516         return NT_STATUS_NONE_MAPPED;
517 }