This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.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
97         /*
98          * this code is far from perfect.
99          * first it enumerates the full /etc/group and that can be slow.
100          * second, it works only with users' SIDs
101          * whereas the day we support nested groups, it will have to
102          * support both users's SIDs and domain groups' SIDs
103          *
104          * having our own ldap backend would be so much faster !
105          * we're far from that, but hope one day ;-) JFM.
106          */
107
108         *prids=NULL;
109         *numgroups=0;
110
111         lp_winbind_gid(&winbind_gid_low, &winbind_gid_high);
112
113
114         DEBUG(10,("get_alias_user_groups: looking if SID %s is a member of groups in the SID domain %s\n", 
115                   sid_to_string(str_qsid, q_sid), sid_to_string(str_domsid, sid)));
116
117         sid_peek_rid(q_sid, &rid);
118
119         pdb_init_sam(&sam_pass);
120         become_root();
121         ret = pdb_getsampwrid(sam_pass, rid);
122         unbecome_root();
123         if (ret == False) {
124                 pdb_free_sam(&sam_pass);
125                 return NT_STATUS_NO_SUCH_USER;
126         }
127
128         fstrcpy(user_name, pdb_get_username(sam_pass));
129         grid=pdb_get_group_rid(sam_pass);
130         gid=pdb_get_gid(sam_pass);
131         
132         grp = glist = getgrent_list();
133         if (grp == NULL) {
134                 pdb_free_sam(&sam_pass);
135                 return NT_STATUS_NO_MEMORY;
136         }
137         
138         for (; grp != NULL; grp = grp->next) {
139                 if(!get_group_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)) {
140                         DEBUG(10,("get_alias_user_groups: gid %d. not found\n", (int)grp->gr_gid));
141                         continue;
142                 }
143                 
144                 /* if it's not an alias, continue */
145                 if (map.sid_name_use!=SID_NAME_ALIAS) {
146                         DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
147                         continue;
148                 }
149
150                 sid_copy(&tmp_sid, &map.sid);
151                 sid_split_rid(&tmp_sid, &rid);
152                 
153                 /* if the sid is not in the correct domain, continue */
154                 if (!sid_equal(&tmp_sid, sid)) {
155                         DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
156                         continue;
157                 }
158
159                 /* Don't return winbind groups as they are not local! */
160                 if ((grp->gr_gid >= winbind_gid_low) && (grp->gr_gid <= winbind_gid_high)) {
161                         DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name));
162                         continue;
163                 }
164
165                 /* Don't return user private groups... */
166                 if (Get_Pwnam(map.nt_name) != 0) {
167                         DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name));
168                         continue;                       
169                 }
170                 
171                 /* the group is fine, we can check if there is the user we're looking for */
172                 DEBUG(10,("get_alias_user_groups: checking if the user is a member of %s.\n", map.nt_name));
173                 
174                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
175                         if(strcmp(grp->gr_mem[num], user_name)==0) {
176                                 /* we found the user, add the group to the list */
177                                 
178                                 new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
179                                 if (new_rids==NULL) {
180                                         DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
181                                         pdb_free_sam(&sam_pass);
182                                         return NT_STATUS_NO_MEMORY;
183                                 }
184                                 rids=new_rids;
185                                 
186                                 sid_peek_rid(&map.sid, &(rids[cur_rid]));
187                                 DEBUG(10,("get_alias_user_groups: user found in group %s\n", map.nt_name));
188                                 cur_rid++;
189                                 break;
190                         }
191                 }
192         }
193
194         grent_free(glist);
195
196         /* now check for the user's gid (the primary group rid) */
197         for (i=0; i<cur_rid && grid!=rids[i]; i++)
198                 ;
199
200         /* the user's gid is already there */
201         if (i!=cur_rid) {
202                 DEBUG(10,("get_alias_user_groups: user is already in the list. good.\n"));
203                 goto done;
204         }
205
206         DEBUG(10,("get_alias_user_groups: looking for gid %d of user %s\n", (int)gid, user_name));
207
208         if(!get_group_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
209                 DEBUG(0,("get_alias_user_groups: gid of user %s doesn't exist. Check your /etc/passwd and /etc/group files\n", user_name));
210                 goto done;
211         }       
212
213         /* the primary group isn't an alias */
214         if (map.sid_name_use!=SID_NAME_ALIAS) {
215                 DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
216                 goto done;
217         }
218
219         sid_copy(&tmp_sid, &map.sid);
220         sid_split_rid(&tmp_sid, &rid);
221
222         /* if the sid is not in the correct domain, continue */
223         if (!sid_equal(&tmp_sid, sid)) {
224                 DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
225                 goto done;
226         }
227
228         /* Don't return winbind groups as they are not local! */
229         if ((gid >= winbind_gid_low) && (gid <= winbind_gid_high)) {
230                 DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name ));
231                 goto done;
232         }
233
234         /* Don't return user private groups... */
235         if (Get_Pwnam(map.nt_name) != 0) {
236                 DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name ));
237                 goto done;                      
238         }
239
240         new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
241         if (new_rids==NULL) {
242                 DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
243                 pdb_free_sam(&sam_pass);
244                 return NT_STATUS_NO_MEMORY;
245         }
246         rids=new_rids;
247
248         sid_peek_rid(&map.sid, &(rids[cur_rid]));
249         cur_rid++;
250
251 done:
252         *prids=rids;
253         *numgroups=cur_rid;
254         pdb_free_sam(&sam_pass);
255
256         return NT_STATUS_OK;
257 }
258
259
260 /*******************************************************************
261  gets a domain user's groups
262  ********************************************************************/
263 BOOL get_domain_user_groups(TALLOC_CTX *ctx, int *numgroups, DOM_GID **pgids, SAM_ACCOUNT *sam_pass)
264 {
265         GROUP_MAP *map=NULL;
266         int i, num, num_entries, cur_gid=0;
267         struct group *grp;
268         DOM_GID *gids;
269         fstring user_name;
270         uint32 grid;
271         uint32 tmp_rid;
272
273         *numgroups= 0;
274
275         fstrcpy(user_name, pdb_get_username(sam_pass));
276         grid=pdb_get_group_rid(sam_pass);
277
278         DEBUG(10,("get_domain_user_groups: searching domain groups [%s] is a member of\n", user_name));
279
280         /* first get the list of the domain groups */
281         if (!enum_group_mapping(SID_NAME_DOM_GRP, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
282                 return False;
283         DEBUG(10,("get_domain_user_groups: there are %d mapped groups\n", num_entries));
284
285         /* 
286          * alloc memory. In the worse case, we alloc memory for nothing.
287          * but I prefer to alloc for nothing
288          * than reallocing everytime.
289          */
290         gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) *  num_entries);  
291
292         /* for each group, check if the user is a member of*/
293         for(i=0; i<num_entries; i++) {
294                 if ((grp=getgrgid(map[i].gid)) == NULL) {
295                         /* very weird !!! */
296                         DEBUG(5,("get_domain_user_groups: gid %d doesn't exist anymore !\n", (int)map[i].gid));
297                         continue;
298                 }
299
300                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
301                         if(strcmp(grp->gr_mem[num], user_name)==0) {
302                                 /* we found the user, add the group to the list */
303                                 sid_peek_rid(&map[i].sid, &(gids[cur_gid].g_rid));
304                                 gids[cur_gid].attr=7;
305                                 DEBUG(10,("get_domain_user_groups: user found in group %s\n", map[i].nt_name));
306                                 cur_gid++;
307                                 break;
308                         }
309                 }
310         }
311
312         /* we have checked the groups */
313         /* we must now check the gid of the user or the primary group rid, that's the same */
314         for (i=0; i<cur_gid && grid!=gids[i].g_rid; i++)
315                 ;
316         
317         /* the user's gid is already there */
318         if (i!=cur_gid) {
319                 /* 
320                  * the primary group of the user but be the first one in the list
321                  * don't ask ! JFM.
322                  */
323                 gids[i].g_rid=gids[0].g_rid;
324                 gids[0].g_rid=grid;
325                 goto done;
326         }
327
328         for(i=0; i<num_entries; i++) {
329                 sid_peek_rid(&map[i].sid, &tmp_rid);
330                 if (tmp_rid==grid) {
331                         /* 
332                          * the primary group of the user but be the first one in the list
333                          * don't ask ! JFM.
334                          */
335                         gids[cur_gid].g_rid=gids[0].g_rid;
336                         gids[0].g_rid=tmp_rid;
337                         gids[cur_gid].attr=7;
338                         DEBUG(10,("get_domain_user_groups: primary gid of user found in group %s\n", map[i].nt_name));
339                         cur_gid++;
340                         goto done; /* leave the loop early */
341                 }
342         }
343
344         DEBUG(0,("get_domain_user_groups: primary gid of user [%s] is not a Domain group !\n", user_name));
345         DEBUGADD(0,("get_domain_user_groups: You should fix it, NT doesn't like that\n"));
346
347  done:
348         *pgids=gids;
349         *numgroups=cur_gid;
350         safe_free(map);
351
352         return True;
353 }
354
355 /*******************************************************************
356  Look up a local (domain) rid and return a name and type.
357  ********************************************************************/
358 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
359 {
360         int i = 0; 
361         (*type) = SID_NAME_DOM_GRP;
362
363         DEBUG(5,("lookup_group_name: rid: %d", rid));
364
365         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
366         {
367                 i++;
368         }
369
370         if (domain_group_rids[i].rid != 0)
371         {
372                 fstrcpy(group_name, domain_group_rids[i].name);
373                 DEBUG(5,(" = %s\n", group_name));
374                 return NT_STATUS_OK;
375         }
376
377         DEBUG(5,(" none mapped\n"));
378         return NT_STATUS_NONE_MAPPED;
379 }
380
381 /*******************************************************************
382  Look up a local alias rid and return a name and type.
383  ********************************************************************/
384 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
385 {
386         int i = 0; 
387         (*type) = SID_NAME_WKN_GRP;
388
389         DEBUG(5,("lookup_alias_name: rid: %d", rid));
390
391         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
392         {
393                 i++;
394         }
395
396         if (builtin_alias_rids[i].rid != 0)
397         {
398                 fstrcpy(alias_name, builtin_alias_rids[i].name);
399                 DEBUG(5,(" = %s\n", alias_name));
400                 return NT_STATUS_OK;
401         }
402
403         DEBUG(5,(" none mapped\n"));
404         return NT_STATUS_NONE_MAPPED;
405 }
406
407 /*******************************************************************
408  Look up a local user rid and return a name and type.
409  ********************************************************************/
410 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
411 {
412         SAM_ACCOUNT *sampwd=NULL;
413         int i = 0;
414         BOOL ret;
415         
416         (*type) = SID_NAME_USER;
417
418         DEBUG(5,("lookup_user_name: rid: %d", rid));
419
420         /* look up the well-known domain user rids first */
421         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
422         {
423                 i++;
424         }
425
426         if (domain_user_rids[i].rid != 0) {
427                 fstrcpy(user_name, domain_user_rids[i].name);
428                 DEBUG(5,(" = %s\n", user_name));
429                 return NT_STATUS_OK;
430         }
431
432         pdb_init_sam(&sampwd);
433
434         /* ok, it's a user.  find the user account */
435         become_root();
436         ret = pdb_getsampwrid(sampwd, rid);
437         unbecome_root();
438
439         if (ret == True) {
440                 fstrcpy(user_name, pdb_get_username(sampwd) );
441                 DEBUG(5,(" = %s\n", user_name));
442                 pdb_free_sam(&sampwd);
443                 return NT_STATUS_OK;
444         }
445
446         DEBUG(5,(" none mapped\n"));
447         pdb_free_sam(&sampwd);
448         return NT_STATUS_NONE_MAPPED;
449 }
450
451 /*******************************************************************
452  Look up a local (domain) group name and return a rid
453  ********************************************************************/
454 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
455 {
456         char *grp_name;
457         int i = -1; /* start do loop at -1 */
458
459         do /* find, if it exists, a group rid for the group name*/
460         {
461                 i++;
462                 (*rid) = domain_group_rids[i].rid;
463                 grp_name = domain_group_rids[i].name;
464
465         } while (grp_name != NULL && !strequal(grp_name, group_name));
466
467         return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
468 }
469
470 /*******************************************************************
471  Look up a local (BUILTIN) alias name and return a rid
472  ********************************************************************/
473 NTSTATUS local_lookup_alias_rid(char *alias_name, uint32 *rid)
474 {
475         char *als_name;
476         int i = -1; /* start do loop at -1 */
477
478         do /* find, if it exists, a alias rid for the alias name*/
479         {
480                 i++;
481                 (*rid) = builtin_alias_rids[i].rid;
482                 als_name = builtin_alias_rids[i].name;
483
484         } while (als_name != NULL && !strequal(als_name, alias_name));
485
486         return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
487 }
488
489 /*******************************************************************
490  Look up a local user name and return a rid
491  ********************************************************************/
492 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
493 {
494         SAM_ACCOUNT *sampass=NULL;
495         BOOL ret;
496
497         (*rid) = 0;
498
499         pdb_init_sam(&sampass);
500
501         /* find the user account */
502         become_root();
503         ret = pdb_getsampwnam(sampass, user_name);
504         unbecome_root();
505
506         if (ret == True) {
507                 (*rid) = pdb_get_user_rid(sampass);
508                 pdb_free_sam(&sampass);
509                 return NT_STATUS_OK;
510         }
511
512         pdb_free_sam(&sampass);
513         return NT_STATUS_NONE_MAPPED;
514 }