6779186 need domain controller hot failover
[unleashed.git] / usr / src / lib / smbsrv / libsmb / common / smb_idmap.c
blob8201db46aac2e9464f2a8d90cef7b9bb4d033677
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <syslog.h>
26 #include <strings.h>
27 #include <smbsrv/libsmb.h>
29 static int smb_idmap_batch_binsid(smb_idmap_batch_t *sib);
32 * Report an idmap error.
34 void
35 smb_idmap_check(const char *s, idmap_stat stat)
37 if (stat != IDMAP_SUCCESS) {
38 if (s == NULL)
39 s = "smb_idmap_check";
41 syslog(LOG_ERR, "%s: %s", s, idmap_stat2string(stat));
46 * smb_idmap_getsid
48 * Tries to get a mapping for the given uid/gid
50 idmap_stat
51 smb_idmap_getsid(uid_t id, int idtype, smb_sid_t **sid)
53 smb_idmap_batch_t sib;
54 idmap_stat stat;
56 stat = smb_idmap_batch_create(&sib, 1, SMB_IDMAP_ID2SID);
57 if (stat != IDMAP_SUCCESS)
58 return (stat);
60 stat = smb_idmap_batch_getsid(sib.sib_idmaph, &sib.sib_maps[0],
61 id, idtype);
63 if (stat != IDMAP_SUCCESS) {
64 smb_idmap_batch_destroy(&sib);
65 return (stat);
68 stat = smb_idmap_batch_getmappings(&sib);
70 if (stat != IDMAP_SUCCESS) {
71 smb_idmap_batch_destroy(&sib);
72 return (stat);
75 *sid = smb_sid_dup(sib.sib_maps[0].sim_sid);
77 smb_idmap_batch_destroy(&sib);
79 return (IDMAP_SUCCESS);
83 * smb_idmap_getid
85 * Tries to get a mapping for the given SID
87 idmap_stat
88 smb_idmap_getid(smb_sid_t *sid, uid_t *id, int *id_type)
90 smb_idmap_batch_t sib;
91 smb_idmap_t *sim;
92 idmap_stat stat;
94 stat = smb_idmap_batch_create(&sib, 1, SMB_IDMAP_SID2ID);
95 if (stat != IDMAP_SUCCESS)
96 return (stat);
98 sim = &sib.sib_maps[0];
99 sim->sim_id = id;
100 stat = smb_idmap_batch_getid(sib.sib_idmaph, sim, sid, *id_type);
101 if (stat != IDMAP_SUCCESS) {
102 smb_idmap_batch_destroy(&sib);
103 return (stat);
106 stat = smb_idmap_batch_getmappings(&sib);
108 if (stat != IDMAP_SUCCESS) {
109 smb_idmap_batch_destroy(&sib);
110 return (stat);
113 *id_type = sim->sim_idtype;
114 smb_idmap_batch_destroy(&sib);
116 return (IDMAP_SUCCESS);
120 * smb_idmap_batch_create
122 * Creates and initializes the context for batch ID mapping.
124 idmap_stat
125 smb_idmap_batch_create(smb_idmap_batch_t *sib, uint16_t nmap, int flags)
127 idmap_stat stat;
129 if (!sib)
130 return (IDMAP_ERR_ARG);
132 bzero(sib, sizeof (smb_idmap_batch_t));
133 stat = idmap_get_create(&sib->sib_idmaph);
135 if (stat != IDMAP_SUCCESS) {
136 smb_idmap_check("idmap_get_create", stat);
137 return (stat);
140 sib->sib_flags = flags;
141 sib->sib_nmap = nmap;
142 sib->sib_size = nmap * sizeof (smb_idmap_t);
143 sib->sib_maps = malloc(sib->sib_size);
144 if (!sib->sib_maps)
145 return (IDMAP_ERR_MEMORY);
147 bzero(sib->sib_maps, sib->sib_size);
148 return (IDMAP_SUCCESS);
152 * smb_idmap_batch_destroy
154 * Frees the batch ID mapping context.
156 void
157 smb_idmap_batch_destroy(smb_idmap_batch_t *sib)
159 int i;
161 if (sib == NULL)
162 return;
164 if (sib->sib_idmaph) {
165 idmap_get_destroy(sib->sib_idmaph);
166 sib->sib_idmaph = NULL;
169 if (sib->sib_maps == NULL)
170 return;
172 if (sib->sib_flags & SMB_IDMAP_ID2SID) {
174 * SIDs are allocated only when mapping
175 * UID/GID to SIDs
177 for (i = 0; i < sib->sib_nmap; i++)
178 smb_sid_free(sib->sib_maps[i].sim_sid);
181 if (sib->sib_size && sib->sib_maps) {
182 free(sib->sib_maps);
183 sib->sib_maps = NULL;
188 * smb_idmap_batch_getid
190 * Queue a request to map the given SID to a UID or GID.
192 * sim->sim_id should point to variable that's supposed to
193 * hold the returned UID/GID. This needs to be setup by caller
194 * of this function.
195 * If requested ID type is known, it's passed as 'idtype',
196 * if it's unknown it'll be returned in sim->sim_idtype.
198 idmap_stat
199 smb_idmap_batch_getid(idmap_get_handle_t *idmaph, smb_idmap_t *sim,
200 smb_sid_t *sid, int idtype)
202 char sidstr[SMB_SID_STRSZ];
203 smb_sid_t *tmpsid;
204 idmap_stat stat;
205 int flag = 0;
207 if (idmaph == NULL || sim == NULL || sid == NULL)
208 return (IDMAP_ERR_ARG);
210 if ((tmpsid = smb_sid_split(sid, &sim->sim_rid)) == NULL)
211 return (IDMAP_ERR_MEMORY);
213 smb_sid_tostr(tmpsid, sidstr);
214 sim->sim_domsid = sidstr;
215 sim->sim_idtype = idtype;
216 smb_sid_free(tmpsid);
218 switch (idtype) {
219 case SMB_IDMAP_USER:
220 stat = idmap_get_uidbysid(idmaph, sim->sim_domsid,
221 sim->sim_rid, flag, sim->sim_id, &sim->sim_stat);
222 smb_idmap_check("idmap_get_uidbysid", stat);
223 break;
225 case SMB_IDMAP_GROUP:
226 stat = idmap_get_gidbysid(idmaph, sim->sim_domsid,
227 sim->sim_rid, flag, sim->sim_id, &sim->sim_stat);
228 smb_idmap_check("idmap_get_gidbysid", stat);
229 break;
231 case SMB_IDMAP_UNKNOWN:
232 stat = idmap_get_pidbysid(idmaph, sim->sim_domsid,
233 sim->sim_rid, flag, sim->sim_id, &sim->sim_idtype,
234 &sim->sim_stat);
235 smb_idmap_check("idmap_get_pidbysid", stat);
236 break;
238 default:
239 return (IDMAP_ERR_ARG);
242 return (stat);
246 * smb_idmap_batch_getsid
248 * Queue a request to map the given UID/GID to a SID.
250 * sim->sim_domsid and sim->sim_rid will contain the mapping
251 * result upon successful process of the batched request.
253 idmap_stat
254 smb_idmap_batch_getsid(idmap_get_handle_t *idmaph, smb_idmap_t *sim,
255 uid_t id, int idtype)
257 idmap_stat stat;
258 int flag = 0;
260 if (!idmaph || !sim)
261 return (IDMAP_ERR_ARG);
263 switch (idtype) {
264 case SMB_IDMAP_USER:
265 stat = idmap_get_sidbyuid(idmaph, id, flag,
266 &sim->sim_domsid, &sim->sim_rid, &sim->sim_stat);
267 smb_idmap_check("idmap_get_sidbyuid", stat);
268 break;
270 case SMB_IDMAP_GROUP:
271 stat = idmap_get_sidbygid(idmaph, id, flag,
272 &sim->sim_domsid, &sim->sim_rid, &sim->sim_stat);
273 smb_idmap_check("idmap_get_sidbygid", stat);
274 break;
276 case SMB_IDMAP_OWNERAT:
277 /* Current Owner S-1-5-32-766 */
278 sim->sim_domsid = strdup(NT_BUILTIN_DOMAIN_SIDSTR);
279 sim->sim_rid = SECURITY_CURRENT_OWNER_RID;
280 sim->sim_stat = IDMAP_SUCCESS;
281 stat = IDMAP_SUCCESS;
282 break;
284 case SMB_IDMAP_GROUPAT:
285 /* Current Group S-1-5-32-767 */
286 sim->sim_domsid = strdup(NT_BUILTIN_DOMAIN_SIDSTR);
287 sim->sim_rid = SECURITY_CURRENT_GROUP_RID;
288 sim->sim_stat = IDMAP_SUCCESS;
289 stat = IDMAP_SUCCESS;
290 break;
292 case SMB_IDMAP_EVERYONE:
293 /* Everyone S-1-1-0 */
294 sim->sim_domsid = strdup(NT_WORLD_AUTH_SIDSTR);
295 sim->sim_rid = 0;
296 sim->sim_stat = IDMAP_SUCCESS;
297 stat = IDMAP_SUCCESS;
298 break;
300 default:
301 return (IDMAP_ERR_ARG);
304 return (stat);
308 * smb_idmap_batch_getmappings
310 * trigger ID mapping service to get the mappings for queued
311 * requests.
313 * Checks the result of all the queued requests.
315 idmap_stat
316 smb_idmap_batch_getmappings(smb_idmap_batch_t *sib)
318 idmap_stat stat = IDMAP_SUCCESS;
319 smb_idmap_t *sim;
320 int i;
322 if ((stat = idmap_get_mappings(sib->sib_idmaph)) != IDMAP_SUCCESS) {
323 smb_idmap_check("idmap_get_mappings", stat);
324 return (stat);
328 * Check the status for all the queued requests
330 for (i = 0, sim = sib->sib_maps; i < sib->sib_nmap; i++, sim++) {
331 if (sim->sim_stat != IDMAP_SUCCESS) {
332 if (sib->sib_flags == SMB_IDMAP_SID2ID) {
333 smb_tracef("[%d] %d (%d)", sim->sim_idtype,
334 sim->sim_rid, sim->sim_stat);
336 return (sim->sim_stat);
340 if (smb_idmap_batch_binsid(sib) != 0)
341 stat = IDMAP_ERR_OTHER;
343 return (stat);
347 * smb_idmap_batch_binsid
349 * Convert sidrids to binary sids
351 * Returns 0 if successful and non-zero upon failure.
353 static int
354 smb_idmap_batch_binsid(smb_idmap_batch_t *sib)
356 smb_sid_t *sid;
357 smb_idmap_t *sim;
358 int i;
360 if (sib->sib_flags & SMB_IDMAP_SID2ID)
361 /* This operation is not required */
362 return (0);
364 sim = sib->sib_maps;
365 for (i = 0; i < sib->sib_nmap; sim++, i++) {
366 if (sim->sim_domsid == NULL)
367 return (-1);
369 sid = smb_sid_fromstr(sim->sim_domsid);
370 free(sim->sim_domsid);
371 if (sid == NULL)
372 return (-1);
374 sim->sim_sid = smb_sid_splice(sid, sim->sim_rid);
375 free(sid);
378 return (0);