Removed save directory argument to become_root() calls. Probably most of
[samba.git] / source3 / rpc_server / srv_lookup.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 /*
25  *
26  *
27
28  this module provides nt user / nt rid lookup functions.
29  users, local groups, domain groups.
30
31  no unix / samba functions should be called in this module:
32  it should purely provide a gateway to the password database API,
33  the local group database API or the domain group database API,
34  but first checking built-in rids.
35
36  did i say rids?  oops, what about "S-1-1" the "Everyone" group
37  and other such well-known sids...
38
39  speed is not of the essence: no particular optimisation is in place.
40
41  *
42  *
43  */
44
45 #include "includes.h"
46
47 extern int DEBUGLEVEL;
48
49 extern fstring global_sam_name;
50 extern DOM_SID global_sam_sid;
51 extern DOM_SID global_sid_S_1_5_20;
52
53 /*
54  * A list of the rids of well known BUILTIN and Domain users
55  * and groups.
56  */
57
58 rid_name builtin_alias_rids[] =
59 {  
60     { BUILTIN_ALIAS_RID_ADMINS       , "Administrators" },
61     { BUILTIN_ALIAS_RID_USERS        , "Users" },
62     { BUILTIN_ALIAS_RID_GUESTS       , "Guests" },
63     { BUILTIN_ALIAS_RID_POWER_USERS  , "Power Users" },
64    
65     { BUILTIN_ALIAS_RID_ACCOUNT_OPS  , "Account Operators" },
66     { BUILTIN_ALIAS_RID_SYSTEM_OPS   , "System Operators" },
67     { BUILTIN_ALIAS_RID_PRINT_OPS    , "Print Operators" },
68     { BUILTIN_ALIAS_RID_BACKUP_OPS   , "Backup Operators" },
69     { BUILTIN_ALIAS_RID_REPLICATOR   , "Replicator" },
70     { 0                             , NULL }
71 };
72
73 /* array lookup of well-known Domain RID users. */
74 rid_name domain_user_rids[] =
75 {  
76     { DOMAIN_USER_RID_ADMIN         , "Administrator" },
77     { DOMAIN_USER_RID_GUEST         , "Guest" },
78     { 0                             , NULL }
79 };
80
81 /* array lookup of well-known Domain RID groups. */
82 rid_name domain_group_rids[] =
83 {  
84     { DOMAIN_GROUP_RID_ADMINS       , "Domain Admins" },
85     { DOMAIN_GROUP_RID_USERS        , "Domain Users" },
86     { DOMAIN_GROUP_RID_GUESTS       , "Domain Guests" },
87     { 0                             , NULL }
88 };
89
90
91 int make_dom_gids(DOMAIN_GRP *mem, int num_members, DOM_GID **ppgids)
92 {
93         int count;
94         int i;
95         DOM_GID *gids = NULL;
96
97         *ppgids = NULL;
98
99         DEBUG(4,("make_dom_gids: %d\n", num_members));
100
101         if (mem == NULL || num_members == 0)
102         {
103                 return 0;
104         }
105
106         for (i = 0, count = 0; i < num_members && count < LSA_MAX_GROUPS; i++) 
107         {
108                 uint32 status;
109
110                 uint32 rid;
111                 uint8  type;
112
113                 uint8  attr  = mem[count].attr;
114                 char   *name = mem[count].name;
115
116                 become_root();
117                 status = lookup_grp_rid(name, &rid, &type);
118                 unbecome_root();
119
120                 if (status == 0x0)
121                 {
122                         gids = (DOM_GID *)Realloc( gids, sizeof(DOM_GID) * (count+1) );
123
124                         if (gids == NULL)
125                         {
126                                 DEBUG(0,("make_dom_gids: Realloc fail !\n"));
127                                 return 0;
128                         }
129
130                         gids[count].g_rid = rid;
131                         gids[count].attr  = attr;
132
133                         DEBUG(5,("group name: %s rid: %d attr: %d\n",
134                                   name, rid, attr));
135                         count++;
136                 }
137                 else
138                 {
139                         DEBUG(1,("make_dom_gids: unknown group name %s\n", name));
140                 }
141         }
142
143         *ppgids = gids;
144         return count;
145 }
146
147 /*******************************************************************
148  gets a domain user's groups
149  ********************************************************************/
150 int get_domain_user_groups(DOMAIN_GRP_MEMBER **grp_members, uint32 group_rid)
151 {
152         DOMAIN_GRP *grp;
153         int num_mem;
154
155         if (grp_members == NULL) return 0;
156
157         grp = getgrouprid(group_rid, grp_members, &num_mem);
158
159         if (grp == NULL)
160         {
161                 return 0;
162         }
163
164         return num_mem;
165 }
166
167
168 /*******************************************************************
169  lookup_builtin_names
170  ********************************************************************/
171 uint32 lookup_builtin_names(uint32 rid, char *name, uint8 *type)
172 {
173         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
174
175         status = (status != 0x0) ? lookup_wk_user_name (rid, name, type) : status;
176         status = (status != 0x0) ? lookup_wk_group_name(rid, name, type) : status;
177         status = (status != 0x0) ? lookup_wk_alias_name(rid, name, type) : status;
178
179         return status;
180 }
181
182
183 /*******************************************************************
184  lookup_added_name - names that have been added to the SAM database by admins.
185  ********************************************************************/
186 uint32 lookup_added_name(uint32 rid, char *name, uint8 *type)
187 {
188         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
189
190         status = (status != 0x0) ? lookup_user_name (rid, name, type) : status;
191         status = (status != 0x0) ? lookup_group_name(rid, name, type) : status;
192         status = (status != 0x0) ? lookup_alias_name(rid, name, type) : status;
193
194         return status;
195 }
196
197
198 /*******************************************************************
199  lookup_name
200  ********************************************************************/
201 uint32 lookup_name(uint32 rid, char *name, uint8 *type)
202 {
203         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
204
205         status = (status != 0x0) ? lookup_builtin_names(rid, name, type) : status;
206         status = (status != 0x0) ? lookup_added_name   (rid, name, type) : status;
207
208         return status;
209 }
210
211
212 /*******************************************************************
213  lookup_wk_group_name
214  ********************************************************************/
215 uint32 lookup_wk_group_name(uint32 rid, char *group_name, uint8 *type)
216 {
217         int i = 0; 
218         (*type) = SID_NAME_WKN_GRP;
219
220         DEBUG(5,("lookup_wk_group_name: rid: %d", rid));
221
222         while (domain_group_rids[i].rid != rid && domain_group_rids[i].rid != 0)
223         {
224                 i++;
225         }
226
227         if (domain_group_rids[i].rid != 0)
228         {
229                 fstrcpy(group_name, domain_group_rids[i].name);
230                 DEBUG(5,(" = %s\n", group_name));
231                 return 0x0;
232         }
233
234         DEBUG(5,(" none mapped\n"));
235         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
236 }
237
238 /*******************************************************************
239  lookup_group_name
240  ********************************************************************/
241 uint32 lookup_group_name(uint32 rid, char *group_name, uint8 *type)
242 {
243         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
244         DOM_SID sid;
245
246         DEBUG(5,("lookup_group_name: rid: 0x%x", rid));
247
248         sid_copy      (&sid, &global_sam_sid);
249         sid_append_rid(&sid, rid);
250
251         (*type) = SID_NAME_DOM_GRP;
252
253         if (map_group_sid_to_name(&sid, group_name, NULL))
254         {
255                 status = 0x0;
256         }
257
258         if (status == 0x0)
259         {
260                 DEBUG(5,(" = %s\n", group_name));
261         }
262         else
263         {
264                 DEBUG(5,(" none mapped\n"));
265         }
266
267         return status;
268 }
269
270 /*******************************************************************
271  lookup_wk_alias_name
272  ********************************************************************/
273 uint32 lookup_wk_alias_name(uint32 rid, char *alias_name, uint8 *type)
274 {
275         int i = 0; 
276         (*type) = SID_NAME_ALIAS;
277
278         DEBUG(5,("lookup_wk_alias_name: rid: %d", rid));
279
280         while (builtin_alias_rids[i].rid != rid && builtin_alias_rids[i].rid != 0)
281         {
282                 i++;
283         }
284
285         if (builtin_alias_rids[i].rid != 0)
286         {
287                 fstrcpy(alias_name, builtin_alias_rids[i].name);
288                 DEBUG(5,(" = %s\n", alias_name));
289                 return 0x0;
290         }
291
292         DEBUG(5,(" none mapped\n"));
293         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
294 }
295
296 /*******************************************************************
297  lookup_alias_name
298  ********************************************************************/
299 uint32 lookup_alias_name(uint32 rid, char *alias_name, uint8 *type)
300 {
301         (*type) = SID_NAME_ALIAS;
302
303         DEBUG(2,("lookup_alias_name: rid: %d\n", rid));
304         DEBUG(2,(" NOT IMPLEMENTED\n"));
305
306         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
307 }
308
309 /*******************************************************************
310  lookup well-known user name
311  ********************************************************************/
312 uint32 lookup_wk_user_name(uint32 rid, char *user_name, uint8 *type)
313 {
314         int i = 0;
315         (*type) = SID_NAME_USER;
316
317         DEBUG(5,("lookup_wk_user_name: rid: %d", rid));
318
319         /* look up the well-known domain user rids first */
320         while (domain_user_rids[i].rid != rid && domain_user_rids[i].rid != 0)
321         {
322                 i++;
323         }
324
325         if (domain_user_rids[i].rid != 0)
326         {
327                 fstrcpy(user_name, domain_user_rids[i].name);
328                 DEBUG(5,(" = %s\n", user_name));
329                 return 0x0;
330         }
331
332         DEBUG(5,(" none mapped\n"));
333         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
334 }
335
336 /*******************************************************************
337  lookup user name
338  ********************************************************************/
339 uint32 lookup_user_name(uint32 rid, char *user_name, uint8 *type)
340 {
341         struct sam_disp_info *disp_info;
342         (*type) = SID_NAME_USER;
343
344         DEBUG(5,("lookup_user_name: rid: %d", rid));
345
346         /* find the user account */
347         become_root();
348         disp_info = getsamdisprid(rid);
349         unbecome_root();
350
351         if (disp_info != NULL)
352         {
353                 fstrcpy(user_name, disp_info->smb_name);
354                 DEBUG(5,(" = %s\n", user_name));
355                 return 0x0;
356         }
357
358         DEBUG(5,(" none mapped\n"));
359         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
360 }
361
362 /*******************************************************************
363  lookup_group_rid
364  ********************************************************************/
365 uint32 lookup_group_rid(char *group_name, uint32 *rid, uint8 *type)
366 {
367         DOM_SID sid;
368
369         (*rid) = 0;
370         (*type) = SID_NAME_DOM_GRP;
371
372         DEBUG(5,("lookup_group_rid: name: %s", group_name));
373
374         if (map_group_name_to_sid(group_name, &sid) &&
375             sid_split_rid(&sid, rid) &&
376             sid_equal(&sid, &global_sam_sid))
377         {
378                 DEBUG(5,(" = 0x%x\n", (*rid)));
379                 return 0x0;
380         }
381
382         DEBUG(5,(" none mapped\n"));
383         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
384 }
385
386 /*******************************************************************
387  lookup_wk_group_rid
388  ********************************************************************/
389 uint32 lookup_wk_group_rid(char *group_name, uint32 *rid, uint8 *type)
390 {
391         char *grp_name;
392         int i = -1; /* start do loop at -1 */
393         (*rid) = 0;
394         (*type) = SID_NAME_WKN_GRP;
395
396         do /* find, if it exists, a group rid for the group name */
397         {
398                 i++;
399                 (*rid) = domain_group_rids[i].rid;
400                 grp_name = domain_group_rids[i].name;
401
402         } while (grp_name != NULL && !strequal(grp_name, group_name));
403
404         return (grp_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
405 }
406
407 /*******************************************************************
408  lookup_alias_sid
409  ********************************************************************/
410 uint32 lookup_alias_sid(char *alias_name, DOM_SID *sid, uint8 *type)
411 {
412         (*type) = SID_NAME_ALIAS;
413
414         DEBUG(5,("lookup_alias_rid: name: %s", alias_name));
415
416         if (map_alias_name_to_sid(alias_name, sid))
417         {
418                 fstring sid_str;
419                 sid_to_string(sid_str, sid);
420                 DEBUG(5,(" = %s\n", sid_str));
421                 return 0x0;
422         }
423
424         DEBUG(5,(" none mapped\n"));
425         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
426 }
427
428 /*******************************************************************
429  lookup_alias_rid
430  ********************************************************************/
431 uint32 lookup_alias_rid(char *alias_name, uint32 *rid, uint8 *type)
432 {
433         DOM_SID sid;
434
435         (*rid) = 0;
436         (*type) = SID_NAME_ALIAS;
437
438         DEBUG(5,("lookup_alias_rid: name: %s", alias_name));
439
440         if (map_alias_name_to_sid(alias_name, &sid) &&
441             sid_split_rid(&sid, rid) &&
442             sid_equal(&sid, &global_sam_sid))
443         {
444                 DEBUG(5,(" = 0x%x\n", (*rid)));
445                 return 0x0;
446         }
447
448         DEBUG(5,(" none mapped\n"));
449         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
450 }
451
452 /*******************************************************************
453  lookup_wk_alias_sid
454  ********************************************************************/
455 uint32 lookup_wk_alias_sid(char *alias_name, DOM_SID *sid, uint8 *type)
456 {
457         char *als_name;
458         int i = 0;
459         uint32 rid;
460         (*type) = SID_NAME_ALIAS;
461
462         do /* find, if it exists, a alias rid for the alias name*/
463         {
464                 rid      = builtin_alias_rids[i].rid;
465                 als_name = builtin_alias_rids[i].name;
466
467                 i++;
468
469                 if (strequal(als_name, alias_name))
470                 {
471                         sid_copy(sid, &global_sid_S_1_5_20);
472                         sid_append_rid(sid, rid);
473
474                         return 0x0;
475                 }
476                         
477         } while (als_name != NULL);
478
479         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
480 }
481
482 /*******************************************************************
483  lookup_wk_alias_rid
484  ********************************************************************/
485 uint32 lookup_wk_alias_rid(char *alias_name, uint32 *rid, uint8 *type)
486 {
487         char *als_name;
488         int i = -1; /* start do loop at -1 */
489         (*rid) = 0;
490         (*type) = SID_NAME_ALIAS;
491
492         do /* find, if it exists, a alias rid for the alias name*/
493         {
494                 i++;
495                 (*rid) = builtin_alias_rids[i].rid;
496                 als_name = builtin_alias_rids[i].name;
497
498         } while (als_name != NULL && !strequal(als_name, alias_name));
499
500         return (als_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
501 }
502
503 /*******************************************************************
504  lookup_sid
505  ********************************************************************/
506 uint32 lookup_sid(char *name, DOM_SID *sid, uint8 *type)
507 {
508         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
509         fstring domain;
510         fstring user;
511         
512         split_domain_name(name, domain, user);
513
514         if (!strequal(domain, global_sam_name))
515         {
516                 DEBUG(0,("lookup_sid: remote domain %s not supported\n", domain));
517                 return status;
518         }
519
520         status = (status != 0x0) ? lookup_wk_alias_sid(user, sid, type) : status;
521         status = (status != 0x0) ? lookup_alias_sid   (user, sid, type) : status;
522 #if 0
523         status = (status != 0x0) ? lookup_domain_sid  (user, sid, type) : status;
524 #endif
525
526         return status;
527 }
528
529 /*******************************************************************
530  lookup_added_user_rid
531  ********************************************************************/
532 uint32 lookup_added_user_rids(char *user_name,
533                 uint32 *usr_rid, uint32 *grp_rid)
534 {
535         struct sam_passwd *sam_pass;
536         (*usr_rid) = 0;
537         (*grp_rid) = 0;
538
539         /* find the user account */
540         become_root();
541         sam_pass = getsam21pwnam(user_name);
542         unbecome_root();
543
544         if (sam_pass != NULL)
545         {
546                 (*usr_rid) = sam_pass->user_rid ;
547                 (*grp_rid) = sam_pass->group_rid;
548                 return 0x0;
549         }
550
551         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
552 }
553
554 /*******************************************************************
555  lookup_added_user_rid
556  ********************************************************************/
557 uint32 lookup_added_user_rid(char *user_name, uint32 *rid, uint8 *type)
558 {
559         struct sam_passwd *sam_pass;
560         (*rid) = 0;
561         (*type) = SID_NAME_USER;
562
563         /* find the user account */
564         become_root();
565         sam_pass = getsam21pwnam(user_name);
566         unbecome_root();
567
568         if (sam_pass != NULL)
569         {
570                 (*rid) = sam_pass->user_rid;
571                 return 0x0;
572         }
573
574         return 0xC0000000 | NT_STATUS_NONE_MAPPED;
575 }
576
577 /*******************************************************************
578  lookup_wk_user_rid
579  ********************************************************************/
580 uint32 lookup_wk_user_rid(char *user_name, uint32 *rid, uint8 *type)
581 {
582         char *usr_name;
583         int i = -1; /* start do loop at -1 */
584         (*rid) = 0;
585         (*type) = SID_NAME_USER;
586
587         do /* find, if it exists, a alias rid for the alias name*/
588         {
589                 i++;
590                 (*rid) = domain_user_rids[i].rid;
591                 usr_name = domain_user_rids[i].name;
592
593         } while (usr_name != NULL && !strequal(usr_name, user_name));
594
595         return (usr_name != NULL) ? 0 : 0xC0000000 | NT_STATUS_NONE_MAPPED;
596 }
597
598 /*******************************************************************
599  lookup_added_grp_rid
600  ********************************************************************/
601 uint32 lookup_added_grp_rid(char *name, uint32 *rid, uint8 *type)
602 {
603         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
604
605         status = (status != 0x0) ? lookup_group_rid(name, rid, type) : status;
606         status = (status != 0x0) ? lookup_alias_rid(name, rid, type) : status;
607
608         return status;
609 }
610
611 /*******************************************************************
612  lookup_builtin_grp_rid
613  ********************************************************************/
614 uint32 lookup_builtin_grp_rid(char *name, uint32 *rid, uint8 *type)
615 {
616         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
617
618         status = (status != 0x0) ? lookup_wk_group_rid(name, rid, type) : status;
619         status = (status != 0x0) ? lookup_wk_alias_rid(name, rid, type) : status;
620
621         return status;
622 }
623
624 /*******************************************************************
625  lookup_grp_rid
626  ********************************************************************/
627 uint32 lookup_grp_rid(char *name, uint32 *rid, uint8 *type)
628 {
629         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
630
631         status = (status != 0x0) ? lookup_builtin_grp_rid(name, rid, type) : status;
632         status = (status != 0x0) ? lookup_added_grp_rid  (name, rid, type) : status;
633
634         return status;
635 }
636
637 /*******************************************************************
638  lookup_user_rid
639  ********************************************************************/
640 uint32 lookup_user_rid(char *name, uint32 *rid, uint8 *type)
641 {
642         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
643
644         status = (status != 0x0) ? lookup_wk_user_rid   (name, rid, type) : status;
645         status = (status != 0x0) ? lookup_added_user_rid(name, rid, type) : status;
646
647         return status;
648 }
649
650 /*******************************************************************
651  lookup_rid
652  ********************************************************************/
653 uint32 lookup_rid(char *name, uint32 *rid, uint8 *type)
654 {
655         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
656
657         status = (status != 0x0) ? lookup_user_rid(name, rid, type) : status;
658         status = (status != 0x0) ? lookup_grp_rid (name, rid, type) : status;
659
660         return status;
661 }
662
663 /*******************************************************************
664  lookup_user_rids
665  ********************************************************************/
666 uint32 lookup_user_rids(char *name, uint32 *usr_rid, uint32 *grp_rid)
667 {
668         uint32 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
669         uint8 type;
670
671         /*
672          * try an ordinary user lookup
673          */
674
675         status = lookup_added_user_rids(name, usr_rid, grp_rid);
676         if (status == 0)
677         {
678                 return status;
679         }
680
681         /*
682          * hm.  must be a well-known user, in a well-known group.
683          */
684
685         status = lookup_wk_user_rid(name, usr_rid, &type);
686         if (status != 0 || type != SID_NAME_USER)
687         {
688                 return status; /* ok, maybe not! */
689         }
690         if (type != SID_NAME_USER)
691         {
692                 return 0xC0000000 | NT_STATUS_NONE_MAPPED; /* users only... */
693         }
694
695         /*
696          * ok, got the user rid: now try the group rid
697          */
698
699         status = lookup_builtin_grp_rid(name, grp_rid, &type);
700         if (type == SID_NAME_DOM_GRP ||
701             type == SID_NAME_ALIAS ||
702             type == SID_NAME_WKN_GRP)
703         {
704                 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
705         }
706
707         return status;
708 }