Let the carnage begin....
[metze/old/v3-2-winbind-ndr.git] / source / sam / idmap_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ID Mapping
4    Copyright (C) Simo Sorce 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_IDMAP
24
25 /*****************************************************************
26  Returns SID pointer.
27 *****************************************************************/  
28
29 NTSTATUS idmap_uid_to_sid(DOM_SID *sid, uid_t uid, int flags)
30 {
31         unid_t id;
32
33         DEBUG(10,("idmap_uid_to_sid: uid = [%lu]\n", (unsigned long)uid));
34
35         flags |= ID_USERID;
36         id.uid = uid;
37         
38         return idmap_get_sid_from_id(sid, id, flags);
39 }
40
41 /*****************************************************************
42  Group mapping is used for gids that maps to Wellknown SIDs
43  Returns SID pointer.
44 *****************************************************************/  
45
46 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid, int flags)
47 {
48         unid_t id;
49
50         DEBUG(10,("idmap_gid_to_sid: gid = [%lu]\n", (unsigned long)gid));
51
52         flags |= ID_GROUPID;
53         id.gid = gid;
54
55         return idmap_get_sid_from_id(sid, id, flags);
56 }
57
58 /*****************************************************************
59  if it is a foreign sid or it is in idmap rid range check idmap,
60  otherwise falls back to the legacy algorithmic mapping.
61  Returns True if this name is a user sid and the conversion
62  was done correctly, False if not.
63 *****************************************************************/  
64
65 NTSTATUS idmap_sid_to_uid(const DOM_SID *sid, uid_t *uid, uint32 flags)
66 {
67         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
68         unid_t id;
69
70         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_static(sid)));
71
72         flags |= ID_USERID;
73
74         ret = idmap_get_id_from_sid(&id, (int *)&flags, sid);
75         
76         if ( NT_STATUS_IS_OK(ret) ) {
77                 DEBUG(10,("idmap_sid_to_uid: uid = [%lu]\n", (unsigned long)id.uid));
78                 *uid = id.uid;
79         } 
80
81         return ret;
82
83 }
84
85 /*****************************************************************
86  *THE CANONICAL* convert SID to gid function.
87  if it is a foreign sid or it is in idmap rid range check idmap,
88  otherwise falls back to the legacy algorithmic mapping.
89  Group mapping is used for gids that maps to Wellknown SIDs
90  Returns True if this name is a user sid and the conversion
91  was done correctly, False if not.
92 *****************************************************************/  
93
94 NTSTATUS idmap_sid_to_gid(const DOM_SID *sid, gid_t *gid, uint32 flags)
95 {
96         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
97         unid_t id;
98
99         DEBUG(10,("sid_to_gid: sid = [%s]\n", sid_string_static(sid)));
100
101         flags |= ID_GROUPID;
102
103         ret = idmap_get_id_from_sid(&id, (int *)&flags, sid);
104         
105         if ( NT_STATUS_IS_OK(ret) ) 
106         {
107                 DEBUG(10,("idmap_sid_to_gid: gid = [%lu]\n", (unsigned long)id.gid));
108                 *gid = id.gid;
109         }
110
111         return ret;
112 }
113
114 /* placeholder for checking lp_winbind_nss_info() */
115 BOOL use_nss_info(const char *info)
116 {
117         int i;
118         const char **list = lp_winbind_nss_info();
119
120         for (i=0; list[i]; i++) {
121                 if (strequal(list[i], info))
122                         return True;
123         }
124
125         return False;
126 }