Initial import
[samba] / 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 #if 0   /* NOT USED */
26
27 /**********************************************************************
28  Get the free RID base if idmap is configured, otherwise return 0
29 **********************************************************************/
30
31 uint32 idmap_get_free_rid_base(void)
32 {
33         uint32 low, high;
34         if (idmap_get_free_rid_range(&low, &high)) {
35                 return low;
36         }
37         return 0;
38 }
39
40 /**********************************************************************
41 **********************************************************************/
42
43 BOOL idmap_check_ugid_is_in_free_range(uint32 id)
44 {
45         uint32 low, high;
46
47         if (!idmap_get_free_ugid_range(&low, &high)) {
48                 return False;
49         }
50         if (id < low || id > high) {
51                 return False;
52         }
53         return True;
54 }
55
56 /**********************************************************************
57 **********************************************************************/
58
59 BOOL idmap_check_rid_is_in_free_range(uint32 rid)
60 {
61         uint32 low, high;
62
63         if (!idmap_get_free_rid_range(&low, &high)) {
64                 return False;
65         }
66         if (rid < algorithmic_rid_base()) {
67                 return True;
68         }
69
70         if (rid < low || rid > high) {
71                 return False;
72         }
73
74         return True;
75 }
76
77 /**********************************************************************
78  if it is a foreign SID or if the SID is in the free range, return true
79 **********************************************************************/
80
81 BOOL idmap_check_sid_is_in_free_range(const DOM_SID *sid)
82 {
83         if (sid_compare_domain(get_global_sam_sid(), sid) == 0) {
84         
85                 uint32 rid;
86
87                 if (sid_peek_rid(sid, &rid)) {
88                         return idmap_check_rid_is_in_free_range(rid);
89                 }
90
91                 return False;
92         }
93
94         return True;
95 }
96
97 #endif  /* NOT USED */
98
99 /*****************************************************************
100  Returns SID pointer.
101 *****************************************************************/  
102
103 NTSTATUS idmap_uid_to_sid(DOM_SID *sid, uid_t uid, int flags)
104 {
105         unid_t id;
106
107         DEBUG(10,("idmap_uid_to_sid: uid = [%lu]\n", (unsigned long)uid));
108
109         flags |= ID_USERID;
110         id.uid = uid;
111         
112         return idmap_get_sid_from_id(sid, id, flags);
113 }
114
115 /*****************************************************************
116  Group mapping is used for gids that maps to Wellknown SIDs
117  Returns SID pointer.
118 *****************************************************************/  
119
120 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid, int flags)
121 {
122         unid_t id;
123
124         DEBUG(10,("idmap_gid_to_sid: gid = [%lu]\n", (unsigned long)gid));
125
126         flags |= ID_GROUPID;
127         id.gid = gid;
128
129         return idmap_get_sid_from_id(sid, id, flags);
130 }
131
132 /*****************************************************************
133  if it is a foreign sid or it is in idmap rid range check idmap,
134  otherwise falls back to the legacy algorithmic mapping.
135  Returns True if this name is a user sid and the conversion
136  was done correctly, False if not.
137 *****************************************************************/  
138
139 NTSTATUS idmap_sid_to_uid(const DOM_SID *sid, uid_t *uid, uint32 flags)
140 {
141         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
142         unid_t id;
143
144         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_static(sid)));
145
146         flags |= ID_USERID;
147
148         ret = idmap_get_id_from_sid(&id, (int *)&flags, sid);
149         
150         if ( NT_STATUS_IS_OK(ret) ) {
151                 DEBUG(10,("idmap_sid_to_uid: uid = [%lu]\n", (unsigned long)id.uid));
152                 *uid = id.uid;
153         } 
154
155         return ret;
156
157 }
158
159 /*****************************************************************
160  *THE CANONICAL* convert SID to gid function.
161  if it is a foreign sid or it is in idmap rid range check idmap,
162  otherwise falls back to the legacy algorithmic mapping.
163  Group mapping is used for gids that maps to Wellknown SIDs
164  Returns True if this name is a user sid and the conversion
165  was done correctly, False if not.
166 *****************************************************************/  
167
168 NTSTATUS idmap_sid_to_gid(const DOM_SID *sid, gid_t *gid, uint32 flags)
169 {
170         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
171         unid_t id;
172
173         DEBUG(10,("sid_to_gid: sid = [%s]\n", sid_string_static(sid)));
174
175         flags |= ID_GROUPID;
176
177         ret = idmap_get_id_from_sid(&id, (int *)&flags, sid);
178         
179         if ( NT_STATUS_IS_OK(ret) ) 
180         {
181                 DEBUG(10,("idmap_sid_to_gid: gid = [%lu]\n", (unsigned long)id.gid));
182                 *gid = id.gid;
183         }
184
185         return ret;
186 }
187
188 /* placeholder for checking lp_winbind_nss_info() */
189 BOOL use_nss_info(const char *info)
190 {
191         int i;
192         const char **list = lp_winbind_nss_info();
193
194         for (i=0; list[i]; i++) {
195                 if (strequal(list[i], info))
196                         return True;
197         }
198
199         return False;
200 }