*id_to_*id call reshape to return NTSTATUS errors
[ira/wip.git] / source3 / 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 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_RPC_SRV
43
44 /*
45  * A list of the rids of well known BUILTIN and Domain users
46  * and groups.
47  */
48
49 rid_name builtin_alias_rids[] =
50 {  
51     { BUILTIN_ALIAS_RID_ADMINS       , "Administrators" },
52     { BUILTIN_ALIAS_RID_USERS        , "Users" },
53     { BUILTIN_ALIAS_RID_GUESTS       , "Guests" },
54     { BUILTIN_ALIAS_RID_POWER_USERS  , "Power Users" },
55    
56     { BUILTIN_ALIAS_RID_ACCOUNT_OPS  , "Account Operators" },
57     { BUILTIN_ALIAS_RID_SYSTEM_OPS   , "System Operators" },
58     { BUILTIN_ALIAS_RID_PRINT_OPS    , "Print Operators" },
59     { BUILTIN_ALIAS_RID_BACKUP_OPS   , "Backup Operators" },
60     { BUILTIN_ALIAS_RID_REPLICATOR   , "Replicator" },
61     { 0                             , NULL }
62 };
63
64 /* array lookup of well-known Domain RID users. */
65 rid_name domain_user_rids[] =
66 {  
67     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
68     { DOMAIN_USER_RID_GUEST         , "Guest" },
69     { 0                             , NULL }
70 };
71
72 /* array lookup of well-known Domain RID groups. */
73 rid_name domain_group_rids[] =
74 {  
75     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
76     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
77     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
78     { 0                             , NULL }
79 };
80
81 /*******************************************************************
82  gets a domain user's groups
83  ********************************************************************/
84 NTSTATUS get_alias_user_groups(TALLOC_CTX *ctx, DOM_SID *sid, int *numgroups, uint32 **prids, DOM_SID *q_sid)
85 {
86         SAM_ACCOUNT *sam_pass=NULL;
87         int i, cur_rid=0;
88         gid_t gid;
89         gid_t *groups = NULL;
90         int num_groups;
91         GROUP_MAP map;
92         DOM_SID tmp_sid;
93         fstring user_name;
94         fstring str_domsid, str_qsid;
95         uint32 rid,grid;
96         uint32 *rids=NULL, *new_rids=NULL;
97         gid_t winbind_gid_low, winbind_gid_high;
98         BOOL ret;
99         BOOL winbind_groups_exist;
100
101         /*
102          * this code is far from perfect.
103          * first it enumerates the full /etc/group and that can be slow.
104          * second, it works only with users' SIDs
105          * whereas the day we support nested groups, it will have to
106          * support both users's SIDs and domain groups' SIDs
107          *
108          * having our own ldap backend would be so much faster !
109          * we're far from that, but hope one day ;-) JFM.
110          */
111
112         *prids=NULL;
113         *numgroups=0;
114
115         winbind_groups_exist = lp_idmap_gid(&winbind_gid_low, &winbind_gid_high);
116
117
118         DEBUG(10,("get_alias_user_groups: looking if SID %s is a member of groups in the SID domain %s\n", 
119                   sid_to_string(str_qsid, q_sid), sid_to_string(str_domsid, sid)));
120
121         pdb_init_sam(&sam_pass);
122         become_root();
123         ret = pdb_getsampwsid(sam_pass, q_sid);
124         unbecome_root();
125         if (ret == False) {
126                 pdb_free_sam(&sam_pass);
127                 return NT_STATUS_NO_SUCH_USER;
128         }
129
130         fstrcpy(user_name, pdb_get_username(sam_pass));
131         grid=pdb_get_group_rid(sam_pass);
132         if (NT_STATUS_IS_ERR(sid_to_gid(pdb_get_group_sid(sam_pass), &gid))) {
133                 /* this should never happen */
134                 DEBUG(2,("get_alias_user_groups: sid_to_gid failed!\n"));
135                 pdb_free_sam(&sam_pass);
136                 return NT_STATUS_UNSUCCESSFUL;
137         }
138
139         become_root();
140         /* on some systems this must run as root */
141         num_groups = getgroups_user(user_name, &groups);        
142         unbecome_root();
143         if (num_groups == -1) {
144                 /* this should never happen */
145                 DEBUG(2,("get_alias_user_groups: getgroups_user failed\n"));
146                 pdb_free_sam(&sam_pass);
147                 return NT_STATUS_UNSUCCESSFUL;
148         }
149
150         for (i=0;i<num_groups;i++) {
151                 if(!get_group_from_gid(groups[i], &map, MAPPING_WITHOUT_PRIV)) {
152                         DEBUG(10,("get_alias_user_groups: gid %d. not found\n", (int)groups[i]));
153                         continue;
154                 }
155                 
156                 /* if it's not an alias, continue */
157                 if (map.sid_name_use!=SID_NAME_ALIAS) {
158                         DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
159                         continue;
160                 }
161
162                 sid_copy(&tmp_sid, &map.sid);
163                 sid_split_rid(&tmp_sid, &rid);
164                 
165                 /* if the sid is not in the correct domain, continue */
166                 if (!sid_equal(&tmp_sid, sid)) {
167                         DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
168                         continue;
169                 }
170
171                 /* Don't return winbind groups as they are not local! */
172                 if (winbind_groups_exist && (groups[i] >= winbind_gid_low) && (groups[i] <= winbind_gid_high)) {
173                         DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name));
174                         continue;
175                 }
176
177                 /* Don't return user private groups... */
178                 if (Get_Pwnam(map.nt_name) != 0) {
179                         DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name));
180                         continue;                       
181                 }
182                 
183                 new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
184                 if (new_rids==NULL) {
185                         DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
186                         pdb_free_sam(&sam_pass);
187                         free(groups);
188                         return NT_STATUS_NO_MEMORY;
189                 }
190                 rids=new_rids;
191                 
192                 sid_peek_rid(&map.sid, &(rids[cur_rid]));
193                 cur_rid++;
194                 break;
195         }
196
197         free(groups);
198
199         /* now check for the user's gid (the primary group rid) */
200         for (i=0; i<cur_rid && grid!=rids[i]; i++)
201                 ;
202
203         /* the user's gid is already there */
204         if (i!=cur_rid) {
205                 DEBUG(10,("get_alias_user_groups: user is already in the list. good.\n"));
206                 goto done;
207         }
208
209         DEBUG(10,("get_alias_user_groups: looking for gid %d of user %s\n", (int)gid, user_name));
210
211         if(!get_group_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
212                 DEBUG(0,("get_alias_user_groups: gid of user %s doesn't exist. Check your /etc/passwd and /etc/group files\n", user_name));
213                 goto done;
214         }       
215
216         /* the primary group isn't an alias */
217         if (map.sid_name_use!=SID_NAME_ALIAS) {
218                 DEBUG(10,("get_alias_user_groups: not returing %s, not an ALIAS group.\n", map.nt_name));
219                 goto done;
220         }
221
222         sid_copy(&tmp_sid, &map.sid);
223         sid_split_rid(&tmp_sid, &rid);
224
225         /* if the sid is not in the correct domain, continue */
226         if (!sid_equal(&tmp_sid, sid)) {
227                 DEBUG(10,("get_alias_user_groups: not returing %s, not in the domain SID.\n", map.nt_name));
228                 goto done;
229         }
230
231         /* Don't return winbind groups as they are not local! */
232         if (winbind_groups_exist && (gid >= winbind_gid_low) && (gid <= winbind_gid_high)) {
233                 DEBUG(10,("get_alias_user_groups: not returing %s, not local.\n", map.nt_name ));
234                 goto done;
235         }
236
237         /* Don't return user private groups... */
238         if (Get_Pwnam(map.nt_name) != 0) {
239                 DEBUG(10,("get_alias_user_groups: not returing %s, clashes with user.\n", map.nt_name ));
240                 goto done;                      
241         }
242
243         new_rids=(uint32 *)Realloc(rids, sizeof(uint32)*(cur_rid+1));
244         if (new_rids==NULL) {
245                 DEBUG(10,("get_alias_user_groups: could not realloc memory\n"));
246                 pdb_free_sam(&sam_pass);
247                 return NT_STATUS_NO_MEMORY;
248         }
249         rids=new_rids;
250
251         sid_peek_rid(&map.sid, &(rids[cur_rid]));
252         cur_rid++;
253
254 done:
255         *prids=rids;
256         *numgroups=cur_rid;
257         pdb_free_sam(&sam_pass);
258
259         return NT_STATUS_OK;
260 }
261
262
263 /*******************************************************************
264  gets a domain user's groups
265  ********************************************************************/
266 BOOL get_domain_user_groups(TALLOC_CTX *ctx, int *numgroups, DOM_GID **pgids, SAM_ACCOUNT *sam_pass)
267 {
268         GROUP_MAP *map=NULL;
269         int i, num, num_entries, cur_gid=0;
270         struct group *grp;
271         DOM_GID *gids;
272         fstring user_name;
273         uint32 grid;
274         uint32 tmp_rid;
275
276         *numgroups= 0;
277
278         fstrcpy(user_name, pdb_get_username(sam_pass));
279         grid=pdb_get_group_rid(sam_pass);
280
281         DEBUG(10,("get_domain_user_groups: searching domain groups [%s] is a member of\n", user_name));
282
283         /* first get the list of the domain groups */
284         if (!pdb_enum_group_mapping(SID_NAME_DOM_GRP, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
285                 return False;
286         DEBUG(10,("get_domain_user_groups: there are %d mapped groups\n", num_entries));
287
288         /* 
289          * alloc memory. In the worse case, we alloc memory for nothing.
290          * but I prefer to alloc for nothing
291          * than reallocing everytime.
292          */
293         gids = (DOM_GID *)talloc(ctx, sizeof(DOM_GID) *  num_entries);  
294
295         /* for each group, check if the user is a member of*/
296         for(i=0; i<num_entries; i++) {
297                 if ((grp=getgrgid(map[i].gid)) == NULL) {
298                         /* very weird !!! */
299                         DEBUG(5,("get_domain_user_groups: gid %d doesn't exist anymore !\n", (int)map[i].gid));
300                         continue;
301                 }
302
303                 for(num=0; grp->gr_mem[num]!=NULL; num++) {
304                         if(strcmp(grp->gr_mem[num], user_name)==0) {
305                                 /* we found the user, add the group to the list */
306                                 sid_peek_rid(&map[i].sid, &(gids[cur_gid].g_rid));
307                                 gids[cur_gid].attr=7;
308                                 DEBUG(10,("get_domain_user_groups: user found in group %s\n", map[i].nt_name));
309                                 cur_gid++;
310                                 break;
311                         }
312                 }
313         }
314
315         /* we have checked the groups */
316         /* we must now check the gid of the user or the primary group rid, that's the same */
317         for (i=0; i<cur_gid && grid!=gids[i].g_rid; i++)
318                 ;
319         
320         /* the user's gid is already there */
321         if (i!=cur_gid) {
322                 /* 
323                  * the primary group of the user but be the first one in the list
324                  * don't ask ! JFM.
325                  */
326                 gids[i].g_rid=gids[0].g_rid;
327                 gids[0].g_rid=grid;
328                 goto done;
329         }
330
331         for(i=0; i<num_entries; i++) {
332                 sid_peek_rid(&map[i].sid, &tmp_rid);
333                 if (tmp_rid==grid) {
334                         /* 
335                          * the primary group of the user but be the first one in the list
336                          * don't ask ! JFM.
337                          */
338                         gids[cur_gid].g_rid=gids[0].g_rid;
339                         gids[0].g_rid=tmp_rid;
340                         gids[cur_gid].attr=7;
341                         DEBUG(10,("get_domain_user_groups: primary gid of user found in group %s\n", map[i].nt_name));
342                         cur_gid++;
343                         goto done; /* leave the loop early */
344                 }
345         }
346
347         DEBUG(0,("get_domain_user_groups: primary gid of user [%s] is not a Domain group !\n", user_name));
348         DEBUGADD(0,("get_domain_user_groups: You should fix it, NT doesn't like that\n"));
349
350
351  done:
352         *pgids=gids;
353         *numgroups=cur_gid;
354         safe_free(map);
355
356         return True;
357 }
358
359 /*******************************************************************
360  gets a domain user's groups from their already-calculated NT_USER_TOKEN
361  ********************************************************************/
362 NTSTATUS nt_token_to_group_list(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid, 
363                                 const NT_USER_TOKEN *nt_token,
364                                 int *numgroups, DOM_GID **pgids) 
365 {
366         DOM_GID *gids;
367         int i;
368
369         gids = (DOM_GID *)talloc(mem_ctx, sizeof(*gids) * nt_token->num_sids);
370
371         if (!gids) {
372                 return NT_STATUS_NO_MEMORY;
373         }
374
375         *numgroups=0;
376
377         for (i=PRIMARY_GROUP_SID_INDEX; i < nt_token->num_sids; i++) {
378                 if (sid_compare_domain(domain_sid, &nt_token->user_sids[i])==0) {
379                         sid_peek_rid(&nt_token->user_sids[i], &(gids[*numgroups].g_rid));
380                         gids[*numgroups].attr=7;
381                         (*numgroups)++;
382                 }
383         }
384         *pgids = gids; 
385         return NT_STATUS_OK;
386 }
387
388 /*******************************************************************
389  Look up a local (domain) rid and return a name and type.
390  ********************************************************************/
391 NTSTATUS local_lookup_group_name(uint32 rid, char *group_name, uint32 *type)
392 {
393         int i = 0; 
394         (*type) = SID_NAME_DOM_GRP;
395
396         DEBUG(5,("lookup_group_name: rid: %d", rid));
397
398         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
399         {
400                 i++;
401         }
402
403         if (domain_group_rids[i].rid != 0)
404         {
405                 fstrcpy(group_name, domain_group_rids[i].name);
406                 DEBUG(5,(" = %s\n", group_name));
407                 return NT_STATUS_OK;
408         }
409
410         DEBUG(5,(" none mapped\n"));
411         return NT_STATUS_NONE_MAPPED;
412 }
413
414 /*******************************************************************
415  Look up a local alias rid and return a name and type.
416  ********************************************************************/
417 NTSTATUS local_lookup_alias_name(uint32 rid, char *alias_name, uint32 *type)
418 {
419         int i = 0; 
420         (*type) = SID_NAME_WKN_GRP;
421
422         DEBUG(5,("lookup_alias_name: rid: %d", rid));
423
424         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
425         {
426                 i++;
427         }
428
429         if (builtin_alias_rids[i].rid != 0)
430         {
431                 fstrcpy(alias_name, builtin_alias_rids[i].name);
432                 DEBUG(5,(" = %s\n", alias_name));
433                 return NT_STATUS_OK;
434         }
435
436         DEBUG(5,(" none mapped\n"));
437         return NT_STATUS_NONE_MAPPED;
438 }
439
440
441 #if 0 /*Nobody uses this function just now*/
442 /*******************************************************************
443  Look up a local user rid and return a name and type.
444  ********************************************************************/
445 NTSTATUS local_lookup_user_name(uint32 rid, char *user_name, uint32 *type)
446 {
447         SAM_ACCOUNT *sampwd=NULL;
448         int i = 0;
449         BOOL ret;
450         
451         (*type) = SID_NAME_USER;
452
453         DEBUG(5,("lookup_user_name: rid: %d", rid));
454
455         /* look up the well-known domain user rids first */
456         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
457         {
458                 i++;
459         }
460
461         if (domain_user_rids[i].rid != 0) {
462                 fstrcpy(user_name, domain_user_rids[i].name);
463                 DEBUG(5,(" = %s\n", user_name));
464                 return NT_STATUS_OK;
465         }
466
467         pdb_init_sam(&sampwd);
468
469         /* ok, it's a user.  find the user account */
470         become_root();
471         ret = pdb_getsampwrid(sampwd, rid);
472         unbecome_root();
473
474         if (ret == True) {
475                 fstrcpy(user_name, pdb_get_username(sampwd) );
476                 DEBUG(5,(" = %s\n", user_name));
477                 pdb_free_sam(&sampwd);
478                 return NT_STATUS_OK;
479         }
480
481         DEBUG(5,(" none mapped\n"));
482         pdb_free_sam(&sampwd);
483         return NT_STATUS_NONE_MAPPED;
484 }
485
486 #endif
487
488 /*******************************************************************
489  Look up a local (domain) group name and return a rid
490  ********************************************************************/
491 NTSTATUS local_lookup_group_rid(char *group_name, uint32 *rid)
492 {
493         const char *grp_name;
494         int i = -1; /* start do loop at -1 */
495
496         do /* find, if it exists, a group rid for the group name*/
497         {
498                 i++;
499                 (*rid) = domain_group_rids[i].rid;
500                 grp_name = domain_group_rids[i].name;
501
502         } while (grp_name != NULL && !strequal(grp_name, group_name));
503
504         return (grp_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
505 }
506
507 /*******************************************************************
508  Look up a local (BUILTIN) alias name and return a rid
509  ********************************************************************/
510 NTSTATUS local_lookup_alias_rid(const char *alias_name, uint32 *rid)
511 {
512         const char *als_name;
513         int i = -1; /* start do loop at -1 */
514
515         do /* find, if it exists, a alias rid for the alias name*/
516         {
517                 i++;
518                 (*rid) = builtin_alias_rids[i].rid;
519                 als_name = builtin_alias_rids[i].name;
520
521         } while (als_name != NULL && !strequal(als_name, alias_name));
522
523         return (als_name != NULL) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
524 }
525
526 /*******************************************************************
527  Look up a local user name and return a rid
528  ********************************************************************/
529 NTSTATUS local_lookup_user_rid(char *user_name, uint32 *rid)
530 {
531         SAM_ACCOUNT *sampass=NULL;
532         BOOL ret;
533
534         (*rid) = 0;
535
536         pdb_init_sam(&sampass);
537
538         /* find the user account */
539         become_root();
540         ret = pdb_getsampwnam(sampass, user_name);
541         unbecome_root();
542
543         if (ret == True) {
544                 (*rid) = pdb_get_user_rid(sampass);
545                 pdb_free_sam(&sampass);
546                 return NT_STATUS_OK;
547         }
548
549         pdb_free_sam(&sampass);
550         return NT_STATUS_NONE_MAPPED;
551 }