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