2 Unix SMB/CIFS implementation.
4 Copyright (C) Simo Sorce 2003
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.
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.
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.*/
23 #define DBGC_CLASS DBGC_IDMAP
27 /**********************************************************************
28 Get the free RID base if idmap is configured, otherwise return 0
29 **********************************************************************/
31 uint32
idmap_get_free_rid_base(void)
34 if (idmap_get_free_rid_range(&low
, &high
)) {
40 /**********************************************************************
41 **********************************************************************/
43 BOOL
idmap_check_ugid_is_in_free_range(uint32 id
)
47 if (!idmap_get_free_ugid_range(&low
, &high
)) {
50 if (id
< low
|| id
> high
) {
56 /**********************************************************************
57 **********************************************************************/
59 BOOL
idmap_check_rid_is_in_free_range(uint32 rid
)
63 if (!idmap_get_free_rid_range(&low
, &high
)) {
66 if (rid
< algorithmic_rid_base()) {
70 if (rid
< low
|| rid
> high
) {
77 /**********************************************************************
78 if it is a foreign SID or if the SID is in the free range, return true
79 **********************************************************************/
81 BOOL
idmap_check_sid_is_in_free_range(const DOM_SID
*sid
)
83 if (sid_compare_domain(get_global_sam_sid(), sid
) == 0) {
87 if (sid_peek_rid(sid
, &rid
)) {
88 return idmap_check_rid_is_in_free_range(rid
);
99 /*****************************************************************
101 *****************************************************************/
103 NTSTATUS
idmap_uid_to_sid(DOM_SID
*sid
, uid_t uid
)
108 DEBUG(10,("idmap_uid_to_sid: uid = [%lu]\n", (unsigned long)uid
));
113 return idmap_get_sid_from_id(sid
, id
, flags
);
116 /*****************************************************************
117 Group mapping is used for gids that maps to Wellknown SIDs
119 *****************************************************************/
121 NTSTATUS
idmap_gid_to_sid(DOM_SID
*sid
, gid_t gid
)
126 DEBUG(10,("idmap_gid_to_sid: gid = [%lu]\n", (unsigned long)gid
));
130 if (!idmap_check_ugid_is_in_free_range(gid
)) {
131 flags
|= ID_QUERY_ONLY
;
135 return idmap_get_sid_from_id(sid
, id
, flags
);
138 /*****************************************************************
139 if it is a foreign sid or it is in idmap rid range check idmap,
140 otherwise falls back to the legacy algorithmic mapping.
141 Returns True if this name is a user sid and the conversion
142 was done correctly, False if not.
143 *****************************************************************/
145 NTSTATUS
idmap_sid_to_uid(const DOM_SID
*sid
, uid_t
*uid
, uint32 flags
)
147 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
150 DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_static(sid
)));
154 ret
= idmap_get_id_from_sid(&id
, (int *)&flags
, sid
);
156 if ( NT_STATUS_IS_OK(ret
) ) {
157 DEBUG(10,("idmap_sid_to_uid: uid = [%lu]\n", (unsigned long)id
.uid
));
165 /*****************************************************************
166 *THE CANONICAL* convert SID to gid function.
167 if it is a foreign sid or it is in idmap rid range check idmap,
168 otherwise falls back to the legacy algorithmic mapping.
169 Group mapping is used for gids that maps to Wellknown SIDs
170 Returns True if this name is a user sid and the conversion
171 was done correctly, False if not.
172 *****************************************************************/
174 NTSTATUS
idmap_sid_to_gid(const DOM_SID
*sid
, gid_t
*gid
, uint32 flags
)
176 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
179 DEBUG(10,("sid_to_gid: sid = [%s]\n", sid_string_static(sid
)));
183 ret
= idmap_get_id_from_sid(&id
, (int *)&flags
, sid
);
185 if ( NT_STATUS_IS_OK(ret
) )
187 DEBUG(10,("idmap_sid_to_gid: gid = [%lu]\n", (unsigned long)id
.gid
));
195 /***************************************************************************
196 Check first, call set_mapping if it doesn't already exist.
197 ***************************************************************************/
199 static NTSTATUS
wellknown_id_init(DOM_SID
*sid
, unid_t id
, int flags
)
202 int qflags
= flags
| ID_QUERY_ONLY
;
204 if (!NT_STATUS_IS_OK(idmap_get_id_from_sid(&storedid
, &qflags
, sid
))) {
205 return idmap_set_mapping(sid
, id
, flags
);
207 if (flags
== ID_USERID
&& id
.uid
!= storedid
.uid
) {
208 DEBUG(0,("wellknown_id_init: WARNING ! Stored uid %u for SID %s is not the same as the requested uid %u\n",
209 (unsigned int)storedid
.uid
, sid_string_static(sid
), (unsigned int)id
.uid
));
210 DEBUG(0,("wellknown_id_init: Attempting to overwrite old mapping with new.\n"));
211 return idmap_set_mapping(sid
, id
, flags
);
212 } else if (flags
== ID_GROUPID
&& id
.gid
!= storedid
.gid
) {
213 DEBUG(0,("wellknown_id_init: WARNING ! Stored gid %u for SID %s is not the same as the requested gid %u\n",
214 (unsigned int)storedid
.gid
, sid_string_static(sid
), (unsigned int)id
.gid
));
215 DEBUG(0,("wellknown_id_init: Attempting to overwrite old mapping with new.\n"));
216 return idmap_set_mapping(sid
, id
, flags
);
222 /***************************************************************************
223 Initialize idmap withWellknown SIDs like Guest, that are necessary
224 to make samba run properly.
225 ***************************************************************************/
227 BOOL
idmap_init_wellknown_sids(void)
229 const char *guest_account
= lp_guestaccount();
237 if (!(guest_account
&& *guest_account
)) {
238 DEBUG(1, ("NULL guest account!?!?\n"));
242 pass
= getpwnam_alloc(guest_account
);
247 /* Fill in the SID for the guest account. */
248 id
.uid
= pass
->pw_uid
;
249 sid_copy(&sid
, get_global_sam_sid());
250 sid_append_rid(&sid
, DOMAIN_USER_RID_GUEST
);
252 if (!NT_STATUS_IS_OK(wellknown_id_init(&sid
, id
, ID_USERID
))) {
253 DEBUG(0, ("Failed to setup UID mapping for GUEST (%s) to (%u)\n",
254 sid_to_string(sid_string
, &sid
), (unsigned int)id
.uid
));
259 /* check if DOMAIN_GROUP_RID_GUESTS SID is set, if not store the
260 * guest account gid as mapping */
261 id
.gid
= pass
->pw_gid
;
262 sid_copy(&sid
, get_global_sam_sid());
263 sid_append_rid(&sid
, DOMAIN_GROUP_RID_GUESTS
);
264 if (!NT_STATUS_IS_OK(wellknown_id_init(&sid
, id
, ID_GROUPID
))) {
265 DEBUG(0, ("Failed to setup GID mapping for Group DOMAIN GUESTS (%s) to (%u)\n",
266 sid_to_string(sid_string
, &sid
), (unsigned int)id
.gid
));
272 /* now fill in group mappings */
273 if(pdb_enum_group_mapping(SID_NAME_UNKNOWN
, &map
, &num_entries
, ENUM_ONLY_MAPPED
)) {
276 for (i
= 0; i
< num_entries
; i
++) {
278 wellknown_id_init(&map
[i
].sid
, id
, ID_GROUPID
);
283 /* Fill in the SID for the administrator account. */
285 sid_copy(&sid
, get_global_sam_sid());
286 sid_append_rid(&sid
, DOMAIN_USER_RID_ADMIN
);
288 if (!NT_STATUS_IS_OK(wellknown_id_init(&sid
, id
, ID_USERID
))) {
289 DEBUG(0, ("Failed to setup UID mapping for ADMINISTRATOR (%s) to (%u)\n",
290 sid_to_string(sid_string
, &sid
), (unsigned int)id
.uid
));