xref: /illumos-gate/usr/src/lib/libidmap/common/idmap_api.c (revision ad8ef92ae01ac09e533731f5a517162c634308b4)
1c5c4113dSnw /*
2c5c4113dSnw  * CDDL HEADER START
3c5c4113dSnw  *
4c5c4113dSnw  * The contents of this file are subject to the terms of the
5c5c4113dSnw  * Common Development and Distribution License (the "License").
6c5c4113dSnw  * You may not use this file except in compliance with the License.
7c5c4113dSnw  *
8c5c4113dSnw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c5c4113dSnw  * or http://www.opensolaris.org/os/licensing.
10c5c4113dSnw  * See the License for the specific language governing permissions
11c5c4113dSnw  * and limitations under the License.
12c5c4113dSnw  *
13c5c4113dSnw  * When distributing Covered Code, include this CDDL HEADER in each
14c5c4113dSnw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c5c4113dSnw  * If applicable, add the following below this CDDL HEADER, with the
16c5c4113dSnw  * fields enclosed by brackets "[]" replaced with your own identifying
17c5c4113dSnw  * information: Portions Copyright [yyyy] [name of copyright owner]
18c5c4113dSnw  *
19c5c4113dSnw  * CDDL HEADER END
20c5c4113dSnw  */
21c5c4113dSnw /*
22c5866007SKeyur Desai  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23*ad8ef92aSMilan Jurik  * Copyright Milan Jurik 2012. All rights reserved.
24c5c4113dSnw  */
25c5c4113dSnw 
26c5c4113dSnw 
27c5c4113dSnw /*
28c5c4113dSnw  * libidmap API
29c5c4113dSnw  */
30c5c4113dSnw 
31c5c4113dSnw #include <stdlib.h>
32479ac375Sdm #include <sys/varargs.h>
33c5c4113dSnw #include <inttypes.h>
34c5c4113dSnw #include <errno.h>
35c5c4113dSnw #include <strings.h>
36c5c4113dSnw #include <ctype.h>
37c5c4113dSnw #include <sys/param.h>
38c5c4113dSnw #include <sys/types.h>
39c5c4113dSnw #include <sys/stat.h>
40c5c4113dSnw #include <dlfcn.h>
41c5c4113dSnw #include <libintl.h>
42c5866007SKeyur Desai #include <syslog.h>
43148c5f43SAlan Wright #include <assert.h>
44c5c4113dSnw #include "idmap_impl.h"
453ee87bcaSJulian Pullen #include "idmap_cache.h"
46c5c4113dSnw 
47c5c4113dSnw static struct timeval TIMEOUT = { 25, 0 };
48c5c4113dSnw 
49c5c4113dSnw static int idmap_stat2errno(idmap_stat);
50479ac375Sdm static idmap_stat	idmap_strdupnull(char **, const char *);
51c5c4113dSnw 
521fdeec65Sjoyce mcintosh #define	__ITER_CREATE(itera, argu, ityp)\
53c5c4113dSnw 	itera = calloc(1, sizeof (*itera));\
54c5c4113dSnw 	if (itera == NULL) {\
55c5c4113dSnw 		errno = ENOMEM;\
56c5c4113dSnw 		return (IDMAP_ERR_MEMORY);\
57c5c4113dSnw 	}\
58c5c4113dSnw 	argu = calloc(1, sizeof (*argu));\
59c5c4113dSnw 	if (argu == NULL) {\
60c5c4113dSnw 		free(itera);\
61c5c4113dSnw 		errno = ENOMEM;\
62c5c4113dSnw 		return (IDMAP_ERR_MEMORY);\
63c5c4113dSnw 	}\
64c5c4113dSnw 	itera->type = ityp;\
65c5c4113dSnw 	itera->retcode = IDMAP_NEXT;\
66c5c4113dSnw 	itera->limit = 1024;\
67c5c4113dSnw 	itera->arg = argu;
68c5c4113dSnw 
69c5c4113dSnw #define	__ITER_CHECK(itera, ityp)\
70c5c4113dSnw 	if (itera == NULL) {\
71c5c4113dSnw 		errno = EINVAL;\
72c5c4113dSnw 		return (IDMAP_ERR_ARG);\
73c5c4113dSnw 	}\
74c5c4113dSnw 	if (itera->type != ityp) {\
75c5c4113dSnw 		errno = EINVAL;\
76c5c4113dSnw 		return (IDMAP_ERR_ARG);\
77c5c4113dSnw 	}
78c5c4113dSnw 
79c5c4113dSnw /*
80c5c4113dSnw  * Free memory allocated by libidmap API
81c5c4113dSnw  *
82c5c4113dSnw  * Input:
83c5c4113dSnw  * ptr - memory to be freed
84c5c4113dSnw  */
85c5c4113dSnw void
86cd37da74Snw idmap_free(void *ptr)
87cd37da74Snw {
88c5c4113dSnw 	free(ptr);
89c5c4113dSnw }
90c5c4113dSnw 
91c5c4113dSnw 
924d61c878SJulian Pullen static idmap_stat
931fdeec65Sjoyce mcintosh idmap_get_prop(idmap_prop_type pr, idmap_prop_res *res)
94479ac375Sdm {
951fdeec65Sjoyce mcintosh 	idmap_stat retcode;
96479ac375Sdm 
97479ac375Sdm 	(void) memset(res, 0, sizeof (*res));
98479ac375Sdm 
991fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_PROP,
100479ac375Sdm 	    (xdrproc_t)xdr_idmap_prop_type, (caddr_t)&pr,
101479ac375Sdm 	    (xdrproc_t)xdr_idmap_prop_res, (caddr_t)res, TIMEOUT);
1021fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
1031fdeec65Sjoyce mcintosh 		return (retcode);
104479ac375Sdm 
105479ac375Sdm 	return (res->retcode); /* This might not be IDMAP_SUCCESS! */
106479ac375Sdm }
107479ac375Sdm 
1084d61c878SJulian Pullen 
109479ac375Sdm idmap_stat
1101fdeec65Sjoyce mcintosh idmap_get_prop_ds(idmap_prop_type pr, idmap_ad_disc_ds_t *dc)
111479ac375Sdm {
112479ac375Sdm 	idmap_prop_res res;
113479ac375Sdm 	idmap_stat rc = IDMAP_SUCCESS;
114479ac375Sdm 
1151fdeec65Sjoyce mcintosh 	rc = idmap_get_prop(pr, &res);
116479ac375Sdm 	if (rc < 0)
117479ac375Sdm 		return (rc);
118479ac375Sdm 
119479ac375Sdm 	dc->port = res.value.idmap_prop_val_u.dsval.port;
120479ac375Sdm 	(void) strlcpy(dc->host, res.value.idmap_prop_val_u.dsval.host,
121479ac375Sdm 	    AD_DISC_MAXHOSTNAME);
122479ac375Sdm 
123479ac375Sdm 	/* xdr doesn't guarantee 0-termination of char[]: */
124479ac375Sdm 	dc->host[AD_DISC_MAXHOSTNAME - 1] = '\0';
125479ac375Sdm 
126479ac375Sdm 	return (rc);
127479ac375Sdm }
128479ac375Sdm 
129479ac375Sdm 
130479ac375Sdm /*
131479ac375Sdm  * Sometimes the property is not set. In that case, str is set to NULL but
132479ac375Sdm  * otherwise IDMAP_SUCCESS is returned.
133479ac375Sdm  */
134479ac375Sdm idmap_stat
1351fdeec65Sjoyce mcintosh idmap_get_prop_str(idmap_prop_type pr, char **str)
136479ac375Sdm {
137479ac375Sdm 	idmap_prop_res res;
138479ac375Sdm 	idmap_stat rc = IDMAP_SUCCESS;
139479ac375Sdm 
1401fdeec65Sjoyce mcintosh 	rc = idmap_get_prop(pr, &res);
141479ac375Sdm 	if (rc < 0)
142479ac375Sdm 		return (rc);
143479ac375Sdm 
144479ac375Sdm 	rc = idmap_strdupnull(str, res.value.idmap_prop_val_u.utf8val);
145479ac375Sdm 	return (rc);
146479ac375Sdm }
147c5c4113dSnw 
148c5c4113dSnw /*
149c5c4113dSnw  * Create/Initialize handle for updates
150c5c4113dSnw  *
151c5c4113dSnw  * Output:
152c5c4113dSnw  * udthandle - update handle
153c5c4113dSnw  */
154c5c4113dSnw idmap_stat
1551fdeec65Sjoyce mcintosh idmap_udt_create(idmap_udt_handle_t **udthandle)
156cd37da74Snw {
157c5c4113dSnw 	idmap_udt_handle_t	*tmp;
158c5c4113dSnw 
1591fdeec65Sjoyce mcintosh 	if (udthandle == NULL) {
160c5c4113dSnw 		errno = EINVAL;
161c5c4113dSnw 		return (IDMAP_ERR_ARG);
162c5c4113dSnw 	}
163c5c4113dSnw 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
164c5c4113dSnw 		errno = ENOMEM;
165c5c4113dSnw 		return (IDMAP_ERR_MEMORY);
166c5c4113dSnw 	}
167c5c4113dSnw 
168c5c4113dSnw 	*udthandle = tmp;
169c5c4113dSnw 	return (IDMAP_SUCCESS);
170c5c4113dSnw }
171c5c4113dSnw 
172c5c4113dSnw 
173c5c4113dSnw /*
174c5c4113dSnw  * All the updates specified by the update handle are committed
175c5c4113dSnw  * in a single transaction. i.e either all succeed or none.
176c5c4113dSnw  *
177c5c4113dSnw  * Input:
178c5c4113dSnw  * udthandle - update handle with the update requests
179c5c4113dSnw  *
180c5c4113dSnw  * Return value:
181c5c4113dSnw  * Status of the commit
182c5c4113dSnw  */
183c5c4113dSnw idmap_stat
184cd37da74Snw idmap_udt_commit(idmap_udt_handle_t *udthandle)
185cd37da74Snw {
1868e228215Sdm 	idmap_update_res	res;
1878e228215Sdm 	idmap_stat		retcode;
188c5c4113dSnw 
189c5c4113dSnw 	if (udthandle == NULL) {
190c5c4113dSnw 		errno = EINVAL;
191c5c4113dSnw 		return (IDMAP_ERR_ARG);
192c5c4113dSnw 	}
1938e228215Sdm 
1948e228215Sdm 	(void) memset(&res, 0, sizeof (res));
1958e228215Sdm 
1961fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_UPDATE,
197cd37da74Snw 	    (xdrproc_t)xdr_idmap_update_batch, (caddr_t)&udthandle->batch,
198cd37da74Snw 	    (xdrproc_t)xdr_idmap_update_res, (caddr_t)&res,
199cd37da74Snw 	    TIMEOUT);
2001fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
2018e228215Sdm 		goto out;
2028e228215Sdm 
2038e228215Sdm 	retcode = udthandle->commit_stat = res.retcode;
2048e228215Sdm 	udthandle->error_index = res.error_index;
2058e228215Sdm 
2068e228215Sdm 	if (retcode != IDMAP_SUCCESS) {
2078e228215Sdm 
2088e228215Sdm 		if (udthandle->error_index < 0)
2098e228215Sdm 			goto out;
2108e228215Sdm 
2118e228215Sdm 		retcode = idmap_namerule_cpy(&udthandle->error_rule,
2128e228215Sdm 		    &res.error_rule);
2138e228215Sdm 		if (retcode != IDMAP_SUCCESS) {
2148e228215Sdm 			udthandle->error_index = -2;
2158e228215Sdm 			goto out;
2168e228215Sdm 		}
2178e228215Sdm 
2188e228215Sdm 		retcode = idmap_namerule_cpy(&udthandle->conflict_rule,
2198e228215Sdm 		    &res.conflict_rule);
2208e228215Sdm 		if (retcode != IDMAP_SUCCESS) {
2218e228215Sdm 			udthandle->error_index = -2;
2228e228215Sdm 			goto out;
2238e228215Sdm 		}
2248e228215Sdm 	}
2258e228215Sdm 
2268e228215Sdm 	retcode = res.retcode;
2278e228215Sdm 
2288e228215Sdm 
2298e228215Sdm out:
230651c0131Sbaban 	/* reset handle so that it can be used again */
2318e228215Sdm 	if (retcode == IDMAP_SUCCESS) {
2328e228215Sdm 		_IDMAP_RESET_UDT_HANDLE(udthandle);
2338e228215Sdm 	}
234651c0131Sbaban 
2358e228215Sdm 	(void) xdr_free(xdr_idmap_update_res, (caddr_t)&res);
2368e228215Sdm 	errno = idmap_stat2errno(retcode);
2378e228215Sdm 	return (retcode);
2388e228215Sdm }
2398e228215Sdm 
2408e228215Sdm 
2418e228215Sdm static void
2428e228215Sdm idmap_namerule_parts_clear(char **windomain, char **winname,
243cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
244cd37da74Snw     boolean_t *is_nt4, int *direction)
245cd37da74Snw {
2468e228215Sdm 	if (windomain)
2478e228215Sdm 		*windomain = NULL;
2488e228215Sdm 	if (winname)
2498e228215Sdm 		*winname = NULL;
2508e228215Sdm 	if (unixname)
2518e228215Sdm 		*unixname = NULL;
2528e228215Sdm 
2538e228215Sdm 	if (is_nt4)
2548e228215Sdm 		*is_nt4 = 0;
2558e228215Sdm 	if (is_user)
2568e228215Sdm 		*is_user = -1;
257cd37da74Snw 	if (is_wuser)
258cd37da74Snw 		*is_wuser = -1;
2598e228215Sdm 	if (direction)
2608e228215Sdm 		*direction = IDMAP_DIRECTION_UNDEF;
2618e228215Sdm }
2628e228215Sdm 
2638e228215Sdm static idmap_stat
264cd37da74Snw idmap_namerule2parts(idmap_namerule *rule,
2658e228215Sdm     char **windomain, char **winname,
266cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
267cd37da74Snw     boolean_t *is_nt4, int *direction)
268cd37da74Snw {
2698e228215Sdm 	idmap_stat retcode;
2708e228215Sdm 
2718e228215Sdm 	if (EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
2728e228215Sdm 		return (IDMAP_ERR_NORESULT);
2738e228215Sdm 
2748e228215Sdm 
2758e228215Sdm 	retcode = idmap_strdupnull(windomain, rule->windomain);
276c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
2778e228215Sdm 		goto errout;
2788e228215Sdm 
2798e228215Sdm 	retcode = idmap_strdupnull(winname, rule->winname);
2808e228215Sdm 	if (retcode != IDMAP_SUCCESS)
2818e228215Sdm 		goto errout;
2828e228215Sdm 
2838e228215Sdm 	retcode = idmap_strdupnull(unixname, rule->unixname);
2848e228215Sdm 	if (retcode != IDMAP_SUCCESS)
2858e228215Sdm 		goto errout;
2868e228215Sdm 
2878e228215Sdm 
2888e228215Sdm 	if (is_user)
2898e228215Sdm 		*is_user = rule->is_user;
290cd37da74Snw 	if (is_wuser)
291cd37da74Snw 		*is_wuser = rule->is_wuser;
2928e228215Sdm 	if (is_nt4)
2938e228215Sdm 		*is_nt4 = rule->is_nt4;
2948e228215Sdm 	if (direction)
2958e228215Sdm 		*direction = rule->direction;
2968e228215Sdm 
2978e228215Sdm 
2988e228215Sdm 	return (IDMAP_SUCCESS);
2998e228215Sdm 
3008e228215Sdm errout:
3018e228215Sdm 	if (windomain && *windomain)
3028e228215Sdm 		free(*windomain);
3038e228215Sdm 	if (winname && *winname)
3048e228215Sdm 		free(*winname);
3058e228215Sdm 	if (unixname && *unixname)
3068e228215Sdm 		free(*unixname);
3078e228215Sdm 
3088e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
309cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3108e228215Sdm 
311c5c4113dSnw 	return (retcode);
3128e228215Sdm 
3138e228215Sdm }
3148e228215Sdm 
3158e228215Sdm /*
3168e228215Sdm  * Retrieve the index of the failed batch element. error_index == -1
3178e228215Sdm  * indicates failure at the beginning, -2 at the end.
3188e228215Sdm  *
3198e228215Sdm  * If idmap_udt_commit didn't return error, the returned value is undefined.
3208e228215Sdm  *
3218e228215Sdm  * Return value:
3228e228215Sdm  * IDMAP_SUCCESS
3238e228215Sdm  */
3248e228215Sdm 
3258e228215Sdm idmap_stat
3268e228215Sdm idmap_udt_get_error_index(idmap_udt_handle_t *udthandle,
327cd37da74Snw     int64_t *error_index)
328cd37da74Snw {
3298e228215Sdm 	if (error_index)
3308e228215Sdm 		*error_index = udthandle->error_index;
3318e228215Sdm 
3328e228215Sdm 	return (IDMAP_SUCCESS);
3338e228215Sdm }
3348e228215Sdm 
3358e228215Sdm 
3368e228215Sdm /*
3378e228215Sdm  * Retrieve the rule which caused the batch to fail. If
3388e228215Sdm  * idmap_udt_commit didn't return error or if error_index is < 0, the
3398e228215Sdm  * retrieved rule is undefined.
3408e228215Sdm  *
3418e228215Sdm  * Return value:
3428e228215Sdm  * IDMAP_ERR_NORESULT if there is no error rule.
3438e228215Sdm  * IDMAP_SUCCESS if the rule was obtained OK.
3448e228215Sdm  * other error code (IDMAP_ERR_NOMEMORY etc)
3458e228215Sdm  */
3468e228215Sdm 
3478e228215Sdm idmap_stat
3488e228215Sdm idmap_udt_get_error_rule(idmap_udt_handle_t *udthandle,
3498e228215Sdm     char **windomain, char **winname,
350cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
351cd37da74Snw     boolean_t *is_nt4, int *direction)
352cd37da74Snw {
3538e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
354cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3558e228215Sdm 
3568e228215Sdm 	if (udthandle->commit_stat == IDMAP_SUCCESS ||
3578e228215Sdm 	    udthandle->error_index < 0)
3588e228215Sdm 		return (IDMAP_ERR_NORESULT);
3598e228215Sdm 
3608e228215Sdm 	return (idmap_namerule2parts(
361cd37da74Snw 	    &udthandle->error_rule,
362cd37da74Snw 	    windomain,
363cd37da74Snw 	    winname,
364cd37da74Snw 	    unixname,
365cd37da74Snw 	    is_user,
366cd37da74Snw 	    is_wuser,
367cd37da74Snw 	    is_nt4,
368cd37da74Snw 	    direction));
3698e228215Sdm }
3708e228215Sdm 
3718e228215Sdm /*
3728e228215Sdm  * Retrieve the rule with which there was a conflict. TODO: retrieve
3738e228215Sdm  * the value.
3748e228215Sdm  *
3758e228215Sdm  * Return value:
3768e228215Sdm  * IDMAP_ERR_NORESULT if there is no error rule.
3778e228215Sdm  * IDMAP_SUCCESS if the rule was obtained OK.
3788e228215Sdm  * other error code (IDMAP_ERR_NOMEMORY etc)
3798e228215Sdm  */
3808e228215Sdm 
3818e228215Sdm idmap_stat
3828e228215Sdm idmap_udt_get_conflict_rule(idmap_udt_handle_t *udthandle,
3838e228215Sdm     char **windomain, char **winname,
384cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
385cd37da74Snw     boolean_t *is_nt4, int *direction)
386cd37da74Snw {
3878e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
388cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3898e228215Sdm 
3908e228215Sdm 	if (udthandle->commit_stat != IDMAP_ERR_W2U_NAMERULE_CONFLICT &&
3918e228215Sdm 	    udthandle->commit_stat != IDMAP_ERR_U2W_NAMERULE_CONFLICT) {
392cd37da74Snw 		return (IDMAP_ERR_NORESULT);
3938e228215Sdm 	}
3948e228215Sdm 
3958e228215Sdm 	return (idmap_namerule2parts(
396cd37da74Snw 	    &udthandle->conflict_rule,
397cd37da74Snw 	    windomain,
398cd37da74Snw 	    winname,
399cd37da74Snw 	    unixname,
400cd37da74Snw 	    is_user,
401cd37da74Snw 	    is_wuser,
402cd37da74Snw 	    is_nt4,
403cd37da74Snw 	    direction));
404c5c4113dSnw }
405c5c4113dSnw 
406c5c4113dSnw 
407c5c4113dSnw /*
408c5c4113dSnw  * Destroy the update handle
409c5c4113dSnw  */
410c5c4113dSnw void
411cd37da74Snw idmap_udt_destroy(idmap_udt_handle_t *udthandle)
412cd37da74Snw {
413c5c4113dSnw 	if (udthandle == NULL)
414c5c4113dSnw 		return;
415c5c4113dSnw 	(void) xdr_free(xdr_idmap_update_batch, (caddr_t)&udthandle->batch);
4168e228215Sdm 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->error_rule);
4178e228215Sdm 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->conflict_rule);
418c5c4113dSnw 	free(udthandle);
419c5c4113dSnw }
420c5c4113dSnw 
421c5c4113dSnw 
422c5c4113dSnw idmap_stat
423c5c4113dSnw idmap_udt_add_namerule(idmap_udt_handle_t *udthandle, const char *windomain,
424cd37da74Snw     boolean_t is_user, boolean_t is_wuser, const char *winname,
425cd37da74Snw     const char *unixname, boolean_t is_nt4, int direction)
426cd37da74Snw {
427c5c4113dSnw 	idmap_retcode	retcode;
428651c0131Sbaban 	idmap_namerule	*rule = NULL;
429c5c4113dSnw 
430651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
431c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
432c5c4113dSnw 		goto errout;
433c5c4113dSnw 
434c5c4113dSnw 	rule = &udthandle->batch.
435cd37da74Snw 	    idmap_update_batch_val[udthandle->next].
436cd37da74Snw 	    idmap_update_op_u.rule;
437c5c4113dSnw 	rule->is_user = is_user;
438cd37da74Snw 	rule->is_wuser = is_wuser;
439c5c4113dSnw 	rule->direction = direction;
440c5c4113dSnw 	rule->is_nt4 = is_nt4;
4418e228215Sdm 
4428e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
4438e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4448e228215Sdm 		goto errout;
4458e228215Sdm 
4468e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
4478e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4488e228215Sdm 		goto errout;
4498e228215Sdm 
4508e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
4518e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4528e228215Sdm 		goto errout;
453651c0131Sbaban 
454651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
455651c0131Sbaban 	    OP_ADD_NAMERULE;
456c5c4113dSnw 	udthandle->next++;
457c5c4113dSnw 	return (IDMAP_SUCCESS);
458c5c4113dSnw 
459c5c4113dSnw errout:
460651c0131Sbaban 	/* The batch should still be usable */
461651c0131Sbaban 	if (rule)
462651c0131Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
463c5c4113dSnw 	errno = idmap_stat2errno(retcode);
464c5c4113dSnw 	return (retcode);
465c5c4113dSnw }
466c5c4113dSnw 
467c5c4113dSnw 
468c5c4113dSnw /* ARGSUSED */
469c5c4113dSnw idmap_stat
470c5c4113dSnw idmap_udt_rm_namerule(idmap_udt_handle_t *udthandle, boolean_t is_user,
471cd37da74Snw     boolean_t is_wuser,	const char *windomain, const char *winname,
472cd37da74Snw     const char *unixname, int direction)
473cd37da74Snw {
474c5c4113dSnw 	idmap_retcode	retcode;
475651c0131Sbaban 	idmap_namerule	*rule = NULL;
476c5c4113dSnw 
477651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
478c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
479c5c4113dSnw 		goto errout;
480c5c4113dSnw 
481c5c4113dSnw 	rule = &udthandle->batch.
482cd37da74Snw 	    idmap_update_batch_val[udthandle->next].
483cd37da74Snw 	    idmap_update_op_u.rule;
484c5c4113dSnw 	rule->is_user = is_user;
485cd37da74Snw 	rule->is_wuser = is_wuser;
486c5c4113dSnw 	rule->direction = direction;
4878e228215Sdm 
4888e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
4898e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4908e228215Sdm 		goto errout;
4918e228215Sdm 
4928e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
4938e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4948e228215Sdm 		goto errout;
4958e228215Sdm 
4968e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
4978e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4988e228215Sdm 		goto errout;
4998e228215Sdm 
500651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
501651c0131Sbaban 	    OP_RM_NAMERULE;
502c5c4113dSnw 	udthandle->next++;
503c5c4113dSnw 	return (IDMAP_SUCCESS);
504c5c4113dSnw 
505c5c4113dSnw errout:
506651c0131Sbaban 	if (rule)
507651c0131Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
508c5c4113dSnw 	errno = idmap_stat2errno(retcode);
509c5c4113dSnw 	return (retcode);
510c5c4113dSnw }
511c5c4113dSnw 
512c5c4113dSnw 
513c5c4113dSnw /* ARGSUSED */
514c5c4113dSnw idmap_stat
515cd37da74Snw idmap_udt_flush_namerules(idmap_udt_handle_t *udthandle)
516cd37da74Snw {
517c5c4113dSnw 	idmap_retcode	retcode;
518c5c4113dSnw 
519651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
520c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
521c5c4113dSnw 		goto errout;
522c5c4113dSnw 
523651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
524651c0131Sbaban 	    OP_FLUSH_NAMERULES;
525c5c4113dSnw 	udthandle->next++;
526c5c4113dSnw 	return (IDMAP_SUCCESS);
527c5c4113dSnw 
528c5c4113dSnw errout:
529c5c4113dSnw 	errno = idmap_stat2errno(retcode);
530c5c4113dSnw 	return (retcode);
531c5c4113dSnw }
532c5c4113dSnw 
533c5c4113dSnw 
534c5c4113dSnw /*
535c5c4113dSnw  * Set the number of entries requested per batch by the iterator
536c5c4113dSnw  *
537c5c4113dSnw  * Input:
538c5c4113dSnw  * iter  - iterator
539c5c4113dSnw  * limit - number of entries requested per batch
540c5c4113dSnw  */
541c5c4113dSnw idmap_stat
542cd37da74Snw idmap_iter_set_limit(idmap_iter_t *iter, uint64_t limit)
543cd37da74Snw {
544c5c4113dSnw 	if (iter == NULL) {
545c5c4113dSnw 		errno = EINVAL;
546c5c4113dSnw 		return (IDMAP_ERR_ARG);
547c5c4113dSnw 	}
548c5c4113dSnw 	iter->limit = limit;
549c5c4113dSnw 	return (IDMAP_SUCCESS);
550c5c4113dSnw }
551c5c4113dSnw 
552c5c4113dSnw 
553c5c4113dSnw /*
554c5c4113dSnw  * Create iterator to get name-based mapping rules
555c5c4113dSnw  *
556c5c4113dSnw  * Input:
557c5c4113dSnw  * windomain - Windows domain
558c5c4113dSnw  * is_user   - user or group rules
559c5c4113dSnw  * winname   - Windows user or group name
560c5c4113dSnw  * unixname  - Unix user or group name
561c5c4113dSnw  *
562c5c4113dSnw  * Output:
563c5c4113dSnw  * iter - iterator
564c5c4113dSnw  */
565c5c4113dSnw idmap_stat
5661fdeec65Sjoyce mcintosh idmap_iter_namerules(const char *windomain,
567cd37da74Snw 		boolean_t is_user, boolean_t is_wuser, const char *winname,
568cd37da74Snw 		const char *unixname, idmap_iter_t **iter)
569cd37da74Snw {
570c5c4113dSnw 
571c5c4113dSnw 	idmap_iter_t			*tmpiter;
572c5c4113dSnw 	idmap_list_namerules_1_argument	*arg = NULL;
573c5c4113dSnw 	idmap_namerule			*rule;
574c5c4113dSnw 	idmap_retcode			retcode;
575c5c4113dSnw 
5761fdeec65Sjoyce mcintosh 	__ITER_CREATE(tmpiter, arg, IDMAP_LIST_NAMERULES);
577c5c4113dSnw 
578c5c4113dSnw 	rule = &arg->rule;
579c5c4113dSnw 	rule->is_user = is_user;
580cd37da74Snw 	rule->is_wuser = is_wuser;
581651c0131Sbaban 	rule->direction = IDMAP_DIRECTION_UNDEF;
5828e228215Sdm 
5838e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
5848e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5858e228215Sdm 		goto errout;
5868e228215Sdm 
5878e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
5888e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5898e228215Sdm 		goto errout;
5908e228215Sdm 
5918e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
5928e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5938e228215Sdm 		goto errout;
594c5c4113dSnw 
595c5c4113dSnw 	*iter = tmpiter;
596c5c4113dSnw 	return (IDMAP_SUCCESS);
597c5c4113dSnw 
598c5c4113dSnw errout:
599*ad8ef92aSMilan Jurik 	if (arg) {
600*ad8ef92aSMilan Jurik 		xdr_free(xdr_idmap_list_namerules_1_argument, (char *)arg);
601*ad8ef92aSMilan Jurik 		free(arg);
602*ad8ef92aSMilan Jurik 	}
603*ad8ef92aSMilan Jurik 	if (tmpiter)
604*ad8ef92aSMilan Jurik 		free(tmpiter);
605*ad8ef92aSMilan Jurik 
606*ad8ef92aSMilan Jurik 	return (retcode);
607c5c4113dSnw }
608c5c4113dSnw 
609c5c4113dSnw 
610c5c4113dSnw /*
611c5c4113dSnw  * Iterate through the name-based mapping rules
612c5c4113dSnw  *
613c5c4113dSnw  * Input:
614c5c4113dSnw  * iter - iterator
615c5c4113dSnw  *
616c5c4113dSnw  * Output:
617c5c4113dSnw  * windomain - Windows domain
618c5c4113dSnw  * winname   - Windows user or group name
619c5c4113dSnw  * unixname  - Unix user or group name
620c5c4113dSnw  * is_nt4    - NT4 or AD
621c5c4113dSnw  * direction - bi(0), win2unix(1), unix2win(2)
622c5c4113dSnw  *
623c5c4113dSnw  * Return value:
624c5c4113dSnw  * 0   - done
625c5c4113dSnw  * 1   - more results available
626c5c4113dSnw  * < 0 - error
627c5c4113dSnw  */
628c5c4113dSnw idmap_stat
629c5c4113dSnw idmap_iter_next_namerule(idmap_iter_t *iter, char **windomain,
630cd37da74Snw     char **winname, char **unixname,  boolean_t *is_user,
631cd37da74Snw     boolean_t *is_wuser, boolean_t *is_nt4, int *direction)
632cd37da74Snw {
633c5c4113dSnw 	idmap_namerules_res		*namerules;
634c5c4113dSnw 	idmap_list_namerules_1_argument	*arg;
635c5c4113dSnw 	idmap_retcode			retcode;
636c5c4113dSnw 
637cd37da74Snw 	idmap_namerule_parts_clear(windomain, winname,
638cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
639cd37da74Snw 
640c5c4113dSnw 
641c5c4113dSnw 	__ITER_CHECK(iter, IDMAP_LIST_NAMERULES);
642c5c4113dSnw 
643c5c4113dSnw 	namerules = (idmap_namerules_res *)iter->retlist;
644c5c4113dSnw 	if (iter->retcode == IDMAP_NEXT && (namerules == NULL ||
645cd37da74Snw 	    iter->next >= namerules->rules.rules_len)) {
646c5c4113dSnw 
647c5c4113dSnw 		if ((arg = iter->arg) == NULL) {
648c5c4113dSnw 			errno = EINVAL;
649c5c4113dSnw 			return (IDMAP_ERR_ARG);
650c5c4113dSnw 		}
651c5c4113dSnw 		arg->limit = iter->limit;
652c5c4113dSnw 
653c5c4113dSnw 		retcode = _iter_get_next_list(IDMAP_LIST_NAMERULES,
654cd37da74Snw 		    iter, arg,
655cd37da74Snw 		    (uchar_t **)&namerules, sizeof (*namerules),
656cd37da74Snw 		    (xdrproc_t)xdr_idmap_list_namerules_1_argument,
657cd37da74Snw 		    (xdrproc_t)xdr_idmap_namerules_res);
658c5c4113dSnw 		if (retcode != IDMAP_SUCCESS)
659c5c4113dSnw 			return (retcode);
660c5c4113dSnw 
661c5c4113dSnw 		if (IDMAP_ERROR(namerules->retcode)) {
662c5c4113dSnw 			retcode  = namerules->retcode;
663c5c4113dSnw 			xdr_free(xdr_idmap_namerules_res, (caddr_t)namerules);
664c5c4113dSnw 			free(namerules);
665c5c4113dSnw 			iter->retlist = NULL;
666c5c4113dSnw 			return (retcode);
667c5c4113dSnw 		}
668c5c4113dSnw 		iter->retcode = namerules->retcode;
669c5c4113dSnw 		arg->lastrowid = namerules->lastrowid;
670c5c4113dSnw 	}
671c5c4113dSnw 
672c5c4113dSnw 	if (namerules == NULL || namerules->rules.rules_len == 0)
673c5c4113dSnw 		return (IDMAP_SUCCESS);
674c5c4113dSnw 
675c5c4113dSnw 	if (iter->next >= namerules->rules.rules_len) {
676c5c4113dSnw 		return (IDMAP_ERR_ARG);
677c5c4113dSnw 	}
678c5c4113dSnw 
6798e228215Sdm 	retcode = idmap_strdupnull(windomain,
6808e228215Sdm 	    namerules->rules.rules_val[iter->next].windomain);
6818e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6828e228215Sdm 		goto errout;
6838e228215Sdm 
6848e228215Sdm 	retcode = idmap_strdupnull(winname,
6858e228215Sdm 	    namerules->rules.rules_val[iter->next].winname);
6868e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6878e228215Sdm 		goto errout;
6888e228215Sdm 
6898e228215Sdm 	retcode = idmap_strdupnull(unixname,
6908e228215Sdm 	    namerules->rules.rules_val[iter->next].unixname);
6918e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6928e228215Sdm 		goto errout;
6938e228215Sdm 
694c5c4113dSnw 	if (is_nt4)
695c5c4113dSnw 		*is_nt4 = namerules->rules.rules_val[iter->next].is_nt4;
696cd37da74Snw 	if (is_user)
697cd37da74Snw 		*is_user = namerules->rules.rules_val[iter->next].is_user;
698cd37da74Snw 	if (is_wuser)
699cd37da74Snw 		*is_wuser = namerules->rules.rules_val[iter->next].is_wuser;
700c5c4113dSnw 	if (direction)
701c5c4113dSnw 		*direction = namerules->rules.rules_val[iter->next].direction;
702c5c4113dSnw 	iter->next++;
703c5c4113dSnw 
704c5c4113dSnw 	if (iter->next == namerules->rules.rules_len)
705c5c4113dSnw 		return (iter->retcode);
706c5c4113dSnw 	else
707c5c4113dSnw 		return (IDMAP_NEXT);
708c5c4113dSnw 
709c5c4113dSnw errout:
710c5c4113dSnw 	if (windomain && *windomain)
711c5c4113dSnw 		free(*windomain);
712c5c4113dSnw 	if (winname && *winname)
713c5c4113dSnw 		free(*winname);
714c5c4113dSnw 	if (unixname && *unixname)
715c5c4113dSnw 		free(*unixname);
716c5c4113dSnw 	return (retcode);
717c5c4113dSnw }
718c5c4113dSnw 
719c5c4113dSnw 
720c5c4113dSnw /*
721c5c4113dSnw  * Create iterator to get SID to UID/GID mappings
722c5c4113dSnw  *
723c5c4113dSnw  * Output:
724c5c4113dSnw  * iter - iterator
725c5c4113dSnw  */
726c5c4113dSnw idmap_stat
7271fdeec65Sjoyce mcintosh idmap_iter_mappings(idmap_iter_t **iter, int flag)
728cd37da74Snw {
729c5c4113dSnw 	idmap_iter_t			*tmpiter;
730c5c4113dSnw 	idmap_list_mappings_1_argument	*arg = NULL;
731c5c4113dSnw 
7321fdeec65Sjoyce mcintosh 	__ITER_CREATE(tmpiter, arg, IDMAP_LIST_MAPPINGS);
733c5c4113dSnw 
73448258c6bSjp 	arg->flag = flag;
735c5c4113dSnw 	*iter = tmpiter;
736c5c4113dSnw 	return (IDMAP_SUCCESS);
737c5c4113dSnw }
738c5c4113dSnw 
739c5c4113dSnw 
740c5c4113dSnw /*
741c5c4113dSnw  * Iterate through the SID to UID/GID mappings
742c5c4113dSnw  *
743c5c4113dSnw  * Input:
744c5c4113dSnw  * iter - iterator
745c5c4113dSnw  *
746c5c4113dSnw  * Output:
747c5c4113dSnw  * sid - SID in canonical form
748c5c4113dSnw  * pid - UID or GID
749c5c4113dSnw  *
750c5c4113dSnw  * Return value:
751c5c4113dSnw  * 0   - done
752c5c4113dSnw  * 1   - more results available
753c5c4113dSnw  * < 0 - error
754c5c4113dSnw  */
755c5c4113dSnw idmap_stat
756c5c4113dSnw idmap_iter_next_mapping(idmap_iter_t *iter, char **sidprefix,
757cd37da74Snw     idmap_rid_t *rid, uid_t *pid, char **winname,
758cd37da74Snw     char **windomain, char **unixname, boolean_t *is_user,
75948258c6bSjp     boolean_t *is_wuser, int *direction, idmap_info *info)
760cd37da74Snw {
761c5c4113dSnw 	idmap_mappings_res		*mappings;
762c5c4113dSnw 	idmap_list_mappings_1_argument	*arg;
763c5c4113dSnw 	idmap_retcode			retcode;
764c5c4113dSnw 	char				*str;
765c5c4113dSnw 
766c5c4113dSnw 	if (sidprefix)
767c5c4113dSnw 		*sidprefix = NULL;
768c5c4113dSnw 	if (rid)
769c5c4113dSnw 		*rid = UINT32_MAX;
770c5c4113dSnw 	if (winname)
771c5c4113dSnw 		*winname = NULL;
772c5c4113dSnw 	if (windomain)
773c5c4113dSnw 		*windomain = NULL;
774c5c4113dSnw 	if (unixname)
775c5c4113dSnw 		*unixname = NULL;
776c5c4113dSnw 	if (pid)
777c5c4113dSnw 		*pid = UINT32_MAX;
778cd37da74Snw 	if (is_user)
779cd37da74Snw 		*is_user = -1;
780cd37da74Snw 	if (is_wuser)
781cd37da74Snw 		*is_wuser = -1;
782c5c4113dSnw 	if (direction)
783651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
784c5c4113dSnw 
785c5c4113dSnw 	__ITER_CHECK(iter, IDMAP_LIST_MAPPINGS);
786c5c4113dSnw 
787c5c4113dSnw 	mappings = (idmap_mappings_res *)iter->retlist;
788c5c4113dSnw 	if (iter->retcode == IDMAP_NEXT && (mappings == NULL ||
789cd37da74Snw 	    iter->next >= mappings->mappings.mappings_len)) {
790c5c4113dSnw 
791c5c4113dSnw 		if ((arg = iter->arg) == NULL) {
792c5c4113dSnw 			errno = EINVAL;
793c5c4113dSnw 			return (IDMAP_ERR_ARG);
794c5c4113dSnw 		}
795c5c4113dSnw 		arg->limit = iter->limit;
796c5c4113dSnw 
797c5c4113dSnw 		retcode = _iter_get_next_list(IDMAP_LIST_MAPPINGS,
798cd37da74Snw 		    iter, arg,
799cd37da74Snw 		    (uchar_t **)&mappings, sizeof (*mappings),
800cd37da74Snw 		    (xdrproc_t)xdr_idmap_list_mappings_1_argument,
801cd37da74Snw 		    (xdrproc_t)xdr_idmap_mappings_res);
802c5c4113dSnw 		if (retcode != IDMAP_SUCCESS)
803c5c4113dSnw 			return (retcode);
804c5c4113dSnw 
805c5c4113dSnw 		if (IDMAP_ERROR(mappings->retcode)) {
806c5c4113dSnw 			retcode  = mappings->retcode;
807c5c4113dSnw 			xdr_free(xdr_idmap_mappings_res, (caddr_t)mappings);
808c5c4113dSnw 			free(mappings);
809c5c4113dSnw 			iter->retlist = NULL;
810c5c4113dSnw 			return (retcode);
811c5c4113dSnw 		}
812c5c4113dSnw 		iter->retcode = mappings->retcode;
813c5c4113dSnw 		arg->lastrowid = mappings->lastrowid;
814c5c4113dSnw 	}
815c5c4113dSnw 
816c5c4113dSnw 	if (mappings == NULL || mappings->mappings.mappings_len == 0)
817c5c4113dSnw 		return (IDMAP_SUCCESS);
818c5c4113dSnw 
819c5c4113dSnw 	if (iter->next >= mappings->mappings.mappings_len) {
820c5c4113dSnw 		return (IDMAP_ERR_ARG);
821c5c4113dSnw 	}
822c5c4113dSnw 
823c5c4113dSnw 	if (sidprefix) {
824c5c4113dSnw 		str = mappings->mappings.mappings_val[iter->next].id1.
825cd37da74Snw 		    idmap_id_u.sid.prefix;
8268edda628Sbaban 		if (str && *str != '\0') {
827c5c4113dSnw 			*sidprefix = strdup(str);
8289581d9f4Sbaban 			if (*sidprefix == NULL) {
8299581d9f4Sbaban 				retcode = IDMAP_ERR_MEMORY;
8309581d9f4Sbaban 				goto errout;
8319581d9f4Sbaban 			}
832c5c4113dSnw 		}
833c5c4113dSnw 	}
834c5c4113dSnw 	if (rid)
835c5c4113dSnw 		*rid = mappings->mappings.mappings_val[iter->next].id1.
836cd37da74Snw 		    idmap_id_u.sid.rid;
8378e228215Sdm 
8388e228215Sdm 	retcode = idmap_strdupnull(windomain,
8398e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id1domain);
8408e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8418e228215Sdm 		goto errout;
8428e228215Sdm 
8438e228215Sdm 	retcode = idmap_strdupnull(winname,
8448e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id1name);
8458e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8468e228215Sdm 		goto errout;
8478e228215Sdm 
8488e228215Sdm 	retcode = idmap_strdupnull(unixname,
8498e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id2name);
8508e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8518e228215Sdm 		goto errout;
8528e228215Sdm 
8538e228215Sdm 
854c5c4113dSnw 	if (pid)
855c5c4113dSnw 		*pid = mappings->mappings.mappings_val[iter->next].id2.
856cd37da74Snw 		    idmap_id_u.uid;
857c5c4113dSnw 	if (direction)
858c5c4113dSnw 		*direction = mappings->mappings.mappings_val[iter->next].
859cd37da74Snw 		    direction;
860cd37da74Snw 	if (is_user)
861cd37da74Snw 		*is_user = (mappings->mappings.mappings_val[iter->next].id2
862cd37da74Snw 		    .idtype == IDMAP_UID)?1:0;
863cd37da74Snw 	if (is_wuser)
864cd37da74Snw 		*is_wuser = (mappings->mappings.mappings_val[iter->next].id1
865cd37da74Snw 		    .idtype == IDMAP_USID)?1:0;
866cd37da74Snw 
86748258c6bSjp 	if (info) {
868148c5f43SAlan Wright 		idmap_info_mov(info,
86948258c6bSjp 		    &mappings->mappings.mappings_val[iter->next].info);
87048258c6bSjp 	}
871c5c4113dSnw 	iter->next++;
872c5c4113dSnw 
873c5c4113dSnw 	if (iter->next == mappings->mappings.mappings_len)
874c5c4113dSnw 		return (iter->retcode);
875c5c4113dSnw 	else
876c5c4113dSnw 		return (IDMAP_NEXT);
877c5c4113dSnw 
878c5c4113dSnw errout:
879c5c4113dSnw 	if (sidprefix && *sidprefix)
880c5c4113dSnw 		free(*sidprefix);
881c5c4113dSnw 	if (winname && *winname)
882c5c4113dSnw 		free(*winname);
883c5c4113dSnw 	if (windomain && *windomain)
884c5c4113dSnw 		free(*windomain);
885c5c4113dSnw 	if (unixname && *unixname)
886c5c4113dSnw 		free(*unixname);
887c5c4113dSnw 	return (retcode);
888c5c4113dSnw }
889c5c4113dSnw 
890c5c4113dSnw 
891c5c4113dSnw /*
892c5c4113dSnw  * Destroy the iterator
893c5c4113dSnw  */
894c5c4113dSnw void
895cd37da74Snw idmap_iter_destroy(idmap_iter_t *iter)
896cd37da74Snw {
897c5c4113dSnw 	xdrproc_t _xdr_argument, _xdr_result;
898c5c4113dSnw 
899c5c4113dSnw 	if (iter == NULL)
900c5c4113dSnw 		return;
901c5c4113dSnw 
902c5c4113dSnw 	switch (iter->type) {
903c5c4113dSnw 	case IDMAP_LIST_NAMERULES:
904c5c4113dSnw 		_xdr_argument = (xdrproc_t)xdr_idmap_list_namerules_1_argument;
905c5c4113dSnw 		_xdr_result = (xdrproc_t)xdr_idmap_namerules_res;
906c5c4113dSnw 		break;
907c5c4113dSnw 	case IDMAP_LIST_MAPPINGS:
908c5c4113dSnw 		_xdr_argument = (xdrproc_t)xdr_idmap_list_mappings_1_argument;
909c5c4113dSnw 		_xdr_result = (xdrproc_t)xdr_idmap_mappings_res;
910c5c4113dSnw 		break;
911c5c4113dSnw 	default:
912c5c4113dSnw 		free(iter);
913c5c4113dSnw 		return;
914c5c4113dSnw 	};
915c5c4113dSnw 
916c5c4113dSnw 	if (iter->arg) {
917c5c4113dSnw 		xdr_free(_xdr_argument, (caddr_t)iter->arg);
918c5c4113dSnw 		free(iter->arg);
919c5c4113dSnw 	}
920c5c4113dSnw 	if (iter->retlist) {
921c5c4113dSnw 		xdr_free(_xdr_result, (caddr_t)iter->retlist);
922c5c4113dSnw 		free(iter->retlist);
923c5c4113dSnw 	}
924c5c4113dSnw 	free(iter);
925c5c4113dSnw }
926c5c4113dSnw 
927c5c4113dSnw 
928c5c4113dSnw /*
929c5c4113dSnw  * Create handle to get SID to UID/GID mapping entries
930c5c4113dSnw  *
931c5c4113dSnw  * Input:
932c5c4113dSnw  * gh - "get mapping" handle
933c5c4113dSnw  */
934c5c4113dSnw idmap_stat
9351fdeec65Sjoyce mcintosh idmap_get_create(idmap_get_handle_t **gh)
936cd37da74Snw {
937c5c4113dSnw 	idmap_get_handle_t	*tmp;
938c5c4113dSnw 
939c5c4113dSnw 	/* allocate the handle */
940c5c4113dSnw 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
941c5c4113dSnw 		errno = ENOMEM;
942c5c4113dSnw 		return (IDMAP_ERR_MEMORY);
943c5c4113dSnw 	}
944c5c4113dSnw 
945c5c4113dSnw 	*gh = tmp;
946c5c4113dSnw 	return (IDMAP_SUCCESS);
947c5c4113dSnw }
948c5c4113dSnw 
949c5c4113dSnw 
950c5c4113dSnw /*
951c5c4113dSnw  * Given SID, get UID
952c5c4113dSnw  *
953c5c4113dSnw  * Input:
954c5c4113dSnw  * sidprefix  - SID prefix
955c5c4113dSnw  * rid        - RID
956c5c4113dSnw  * flag       - flag
957c5c4113dSnw  *
958c5c4113dSnw  * Output:
959c5c4113dSnw  * stat - status of the get request
960c5c4113dSnw  * uid  - POSIX UID if stat = 0
961c5c4113dSnw  *
962c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
963c5c4113dSnw  */
964c5c4113dSnw idmap_stat
965c5c4113dSnw idmap_get_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
966cd37da74Snw 		int flag, uid_t *uid, idmap_stat *stat)
967cd37da74Snw {
96848258c6bSjp 	return (idmap_getext_uidbysid(gh, sidprefix, rid, flag, uid,
96948258c6bSjp 	    NULL, stat));
97048258c6bSjp }
97148258c6bSjp 
97248258c6bSjp /*
97348258c6bSjp  * Given SID, get UID
97448258c6bSjp  *
97548258c6bSjp  * Input:
97648258c6bSjp  * sidprefix  - SID prefix
97748258c6bSjp  * rid        - RID
97848258c6bSjp  * flag       - flag
97948258c6bSjp  *
98048258c6bSjp  * Output:
98148258c6bSjp  * stat - status of the get request
98248258c6bSjp  * uid  - POSIX UID if stat = 0
98348258c6bSjp  * how  - mapping type if stat = 0
98448258c6bSjp  *
98548258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
98648258c6bSjp  */
987c5c4113dSnw 
98848258c6bSjp idmap_stat
98948258c6bSjp idmap_getext_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
99048258c6bSjp 		int flag, uid_t *uid, idmap_info *info, idmap_stat *stat)
99148258c6bSjp {
992c5c4113dSnw 	idmap_retcode	retcode;
993651c0131Sbaban 	idmap_mapping	*mapping = NULL;
994c5c4113dSnw 
995c5c4113dSnw 	/* sanity checks */
996c5c4113dSnw 	if (gh == NULL)
997c5c4113dSnw 		return (IDMAP_ERR_ARG);
998c5c4113dSnw 	if (uid == NULL || sidprefix == NULL)
999c5c4113dSnw 		return (IDMAP_ERR_ARG);
1000c5c4113dSnw 
10013ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
10023ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
10033ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_uidbysid(sidprefix, rid, uid);
10043ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
10053ee87bcaSJulian Pullen 			*stat = retcode;
10063ee87bcaSJulian Pullen 			return (retcode);
10073ee87bcaSJulian Pullen 		}
10083ee87bcaSJulian Pullen 	}
10093ee87bcaSJulian Pullen 
1010c5c4113dSnw 	/* Extend the request array and the return list */
1011c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1012c5c4113dSnw 		goto errout;
1013c5c4113dSnw 
1014c5c4113dSnw 	/* Setup the request */
1015c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1016c5c4113dSnw 	mapping->flag = flag;
1017c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1018c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1019c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1020c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1021c5c4113dSnw 		goto errout;
1022c5c4113dSnw 	}
1023c5c4113dSnw 	mapping->id2.idtype = IDMAP_UID;
1024c5c4113dSnw 
1025c5c4113dSnw 	/* Setup pointers for the result */
1026c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_UID;
1027c5c4113dSnw 	gh->retlist[gh->next].uid = uid;
1028c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
102948258c6bSjp 	gh->retlist[gh->next].info = info;
10303ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1031c5c4113dSnw 
1032c5c4113dSnw 	gh->next++;
1033c5c4113dSnw 	return (IDMAP_SUCCESS);
1034c5c4113dSnw 
1035c5c4113dSnw errout:
1036651c0131Sbaban 	/* Batch created so far should still be usable */
1037651c0131Sbaban 	if (mapping)
1038651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1039c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1040c5c4113dSnw 	return (retcode);
1041c5c4113dSnw }
1042c5c4113dSnw 
1043c5c4113dSnw 
1044c5c4113dSnw /*
1045c5c4113dSnw  * Given SID, get GID
1046c5c4113dSnw  *
1047c5c4113dSnw  * Input:
1048c5c4113dSnw  * sidprefix  - SID prefix
1049c5c4113dSnw  * rid        - rid
1050c5c4113dSnw  * flag       - flag
1051c5c4113dSnw  *
1052c5c4113dSnw  * Output:
1053c5c4113dSnw  * stat - status of the get request
1054c5c4113dSnw  * gid  - POSIX GID if stat = 0
1055c5c4113dSnw  *
1056c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1057c5c4113dSnw  */
1058c5c4113dSnw idmap_stat
1059c5c4113dSnw idmap_get_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1060cd37da74Snw 		int flag, gid_t *gid, idmap_stat *stat)
1061cd37da74Snw {
106248258c6bSjp 	return (idmap_getext_gidbysid(gh, sidprefix, rid, flag, gid,
106348258c6bSjp 	    NULL, stat));
106448258c6bSjp }
106548258c6bSjp 
106648258c6bSjp 
106748258c6bSjp /*
106848258c6bSjp  * Given SID, get GID
106948258c6bSjp  *
107048258c6bSjp  * Input:
107148258c6bSjp  * sidprefix  - SID prefix
107248258c6bSjp  * rid        - rid
107348258c6bSjp  * flag       - flag
107448258c6bSjp  *
107548258c6bSjp  * Output:
107648258c6bSjp  * stat - status of the get request
107748258c6bSjp  * gid  - POSIX GID if stat = 0
107848258c6bSjp  * how  - mapping type if stat = 0
107948258c6bSjp  *
108048258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
108148258c6bSjp  */
108248258c6bSjp idmap_stat
108348258c6bSjp idmap_getext_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
108448258c6bSjp 		int flag, gid_t *gid, idmap_info *info, idmap_stat *stat)
108548258c6bSjp {
1086c5c4113dSnw 
1087c5c4113dSnw 	idmap_retcode	retcode;
1088651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1089c5c4113dSnw 
1090c5c4113dSnw 	/* sanity checks */
1091c5c4113dSnw 	if (gh == NULL)
1092c5c4113dSnw 		return (IDMAP_ERR_ARG);
1093c5c4113dSnw 	if (gid == NULL || sidprefix == NULL)
1094c5c4113dSnw 		return (IDMAP_ERR_ARG);
1095c5c4113dSnw 
10963ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
10973ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
10983ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_gidbysid(sidprefix, rid, gid);
10993ee87bcaSJulian Pullen 		if (retcode == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
11003ee87bcaSJulian Pullen 			*stat = retcode;
11013ee87bcaSJulian Pullen 			return (retcode);
11023ee87bcaSJulian Pullen 		}
11033ee87bcaSJulian Pullen 	}
11043ee87bcaSJulian Pullen 
1105c5c4113dSnw 	/* Extend the request array and the return list */
1106c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1107c5c4113dSnw 		goto errout;
1108c5c4113dSnw 
1109c5c4113dSnw 	/* Setup the request */
1110c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1111c5c4113dSnw 	mapping->flag = flag;
1112c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1113c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1114c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1115c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1116c5c4113dSnw 		goto errout;
1117c5c4113dSnw 	}
1118c5c4113dSnw 	mapping->id2.idtype = IDMAP_GID;
1119c5c4113dSnw 
1120c5c4113dSnw 	/* Setup pointers for the result */
1121c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_GID;
1122c5c4113dSnw 	gh->retlist[gh->next].gid = gid;
1123c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
112448258c6bSjp 	gh->retlist[gh->next].info = info;
11253ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1126c5c4113dSnw 
1127c5c4113dSnw 	gh->next++;
1128c5c4113dSnw 	return (IDMAP_SUCCESS);
1129c5c4113dSnw 
1130c5c4113dSnw errout:
1131651c0131Sbaban 	if (mapping)
1132651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1133c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1134c5c4113dSnw 	return (retcode);
1135c5c4113dSnw }
1136c5c4113dSnw 
1137c5c4113dSnw 
113848258c6bSjp 
1139c5c4113dSnw /*
1140c5c4113dSnw  * Given SID, get POSIX ID i.e. UID/GID
1141c5c4113dSnw  *
1142c5c4113dSnw  * Input:
1143c5c4113dSnw  * sidprefix  - SID prefix
1144c5c4113dSnw  * rid        - rid
1145c5c4113dSnw  * flag       - flag
1146c5c4113dSnw  *
1147c5c4113dSnw  * Output:
1148c5c4113dSnw  * stat    - status of the get request
1149c5c4113dSnw  * is_user - user or group
1150c5c4113dSnw  * pid     - POSIX UID if stat = 0 and is_user = 1
1151c5c4113dSnw  *           POSIX GID if stat = 0 and is_user = 0
1152c5c4113dSnw  *
1153c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1154c5c4113dSnw  */
1155c5c4113dSnw idmap_stat
1156c5c4113dSnw idmap_get_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1157cd37da74Snw 		int flag, uid_t *pid, int *is_user, idmap_stat *stat)
115848258c6bSjp {
115948258c6bSjp 	return (idmap_getext_pidbysid(gh, sidprefix, rid, flag, pid, is_user,
116048258c6bSjp 	    NULL, stat));
116148258c6bSjp }
116248258c6bSjp 
116348258c6bSjp 
116448258c6bSjp 
116548258c6bSjp /*
116648258c6bSjp  * Given SID, get POSIX ID i.e. UID/GID
116748258c6bSjp  *
116848258c6bSjp  * Input:
116948258c6bSjp  * sidprefix  - SID prefix
117048258c6bSjp  * rid        - rid
117148258c6bSjp  * flag       - flag
117248258c6bSjp  *
117348258c6bSjp  * Output:
117448258c6bSjp  * stat    - status of the get request
117548258c6bSjp  * is_user - user or group
117648258c6bSjp  * pid     - POSIX UID if stat = 0 and is_user = 1
117748258c6bSjp  *           POSIX GID if stat = 0 and is_user = 0
117848258c6bSjp  * how     - mapping type if stat = 0
117948258c6bSjp  *
118048258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
118148258c6bSjp  */
118248258c6bSjp idmap_stat
118348258c6bSjp idmap_getext_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
118448258c6bSjp 	int flag, uid_t *pid, int *is_user, idmap_info *info, idmap_stat *stat)
1185cd37da74Snw {
1186c5c4113dSnw 	idmap_retcode	retcode;
1187651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1188c5c4113dSnw 
1189c5c4113dSnw 	/* sanity checks */
1190c5c4113dSnw 	if (gh == NULL)
1191c5c4113dSnw 		return (IDMAP_ERR_ARG);
1192c5c4113dSnw 	if (pid == NULL || sidprefix == NULL || is_user == NULL)
1193c5c4113dSnw 		return (IDMAP_ERR_ARG);
1194c5c4113dSnw 
11953ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
11963ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
11973ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_pidbysid(sidprefix, rid, pid,
11983ee87bcaSJulian Pullen 		    is_user);
11993ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12003ee87bcaSJulian Pullen 			*stat = retcode;
12013ee87bcaSJulian Pullen 			return (retcode);
12023ee87bcaSJulian Pullen 		}
12033ee87bcaSJulian Pullen 	}
12043ee87bcaSJulian Pullen 
1205c5c4113dSnw 	/* Extend the request array and the return list */
1206c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1207c5c4113dSnw 		goto errout;
1208c5c4113dSnw 
1209c5c4113dSnw 	/* Setup the request */
1210c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1211c5c4113dSnw 	mapping->flag = flag;
1212c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1213c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1214c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1215c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1216c5c4113dSnw 		goto errout;
1217c5c4113dSnw 	}
1218c5c4113dSnw 	mapping->id2.idtype = IDMAP_POSIXID;
1219c5c4113dSnw 
1220c5c4113dSnw 	/* Setup pointers for the result */
1221c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_POSIXID;
1222c5c4113dSnw 	gh->retlist[gh->next].uid = pid;
1223c5c4113dSnw 	gh->retlist[gh->next].gid = pid;
1224c5c4113dSnw 	gh->retlist[gh->next].is_user = is_user;
1225c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
122648258c6bSjp 	gh->retlist[gh->next].info = info;
12273ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1228c5c4113dSnw 
1229c5c4113dSnw 	gh->next++;
1230c5c4113dSnw 	return (IDMAP_SUCCESS);
1231c5c4113dSnw 
1232c5c4113dSnw errout:
1233651c0131Sbaban 	if (mapping)
1234651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1235c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1236c5c4113dSnw 	return (retcode);
1237c5c4113dSnw }
1238c5c4113dSnw 
1239c5c4113dSnw 
1240c5c4113dSnw /*
1241c5c4113dSnw  * Given UID, get SID
1242c5c4113dSnw  *
1243c5c4113dSnw  * Input:
1244c5c4113dSnw  * uid  - POSIX UID
1245c5c4113dSnw  * flag - flag
1246c5c4113dSnw  *
1247c5c4113dSnw  * Output:
1248c5c4113dSnw  * stat - status of the get request
1249c5c4113dSnw  * sid  - SID prefix (if stat == 0)
1250c5c4113dSnw  * rid  - rid
1251c5c4113dSnw  *
1252c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1253c5c4113dSnw  */
1254c5c4113dSnw idmap_stat
1255c5c4113dSnw idmap_get_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
1256cd37da74Snw 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
1257cd37da74Snw {
125848258c6bSjp 	return (idmap_getext_sidbyuid(gh, uid, flag, sidprefix, rid,
125948258c6bSjp 	    NULL, stat));
126048258c6bSjp }
126148258c6bSjp 
126248258c6bSjp 
126348258c6bSjp /*
126448258c6bSjp  * Given UID, get SID
126548258c6bSjp  *
126648258c6bSjp  * Input:
126748258c6bSjp  * uid  - POSIX UID
126848258c6bSjp  * flag - flag
126948258c6bSjp  *
127048258c6bSjp  * Output:
127148258c6bSjp  * stat - status of the get request
127248258c6bSjp  * sid  - SID prefix (if stat == 0)
127348258c6bSjp  * rid  - rid
127448258c6bSjp  * how  - mapping type if stat = 0
127548258c6bSjp  *
127648258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
127748258c6bSjp  */
127848258c6bSjp idmap_stat
127948258c6bSjp idmap_getext_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
128048258c6bSjp 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
128148258c6bSjp {
1282c5c4113dSnw 
1283c5c4113dSnw 	idmap_retcode	retcode;
1284651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1285c5c4113dSnw 
1286c5c4113dSnw 	/* sanity checks */
1287c5c4113dSnw 	if (gh == NULL)
1288c5c4113dSnw 		return (IDMAP_ERR_ARG);
1289c5c4113dSnw 	if (sidprefix == NULL)
1290c5c4113dSnw 		return (IDMAP_ERR_ARG);
1291c5c4113dSnw 
12923ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
12933ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
12943ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_sidbyuid(sidprefix, rid, uid);
12953ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12963ee87bcaSJulian Pullen 			*stat = retcode;
12973ee87bcaSJulian Pullen 			return (retcode);
12983ee87bcaSJulian Pullen 		}
12993ee87bcaSJulian Pullen 	}
13003ee87bcaSJulian Pullen 
1301c5c4113dSnw 	/* Extend the request array and the return list */
1302c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1303c5c4113dSnw 		goto errout;
1304c5c4113dSnw 
1305c5c4113dSnw 	/* Setup the request */
1306c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1307c5c4113dSnw 	mapping->flag = flag;
1308c5c4113dSnw 	mapping->id1.idtype = IDMAP_UID;
1309c5c4113dSnw 	mapping->id1.idmap_id_u.uid = uid;
1310c5c4113dSnw 	mapping->id2.idtype = IDMAP_SID;
1311c5c4113dSnw 
1312c5c4113dSnw 	/* Setup pointers for the result */
1313c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_SID;
1314c5c4113dSnw 	gh->retlist[gh->next].sidprefix = sidprefix;
1315c5c4113dSnw 	gh->retlist[gh->next].rid = rid;
1316c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
131748258c6bSjp 	gh->retlist[gh->next].info = info;
13183ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1319c5c4113dSnw 
1320c5c4113dSnw 	gh->next++;
1321c5c4113dSnw 	return (IDMAP_SUCCESS);
1322c5c4113dSnw 
1323c5c4113dSnw errout:
1324651c0131Sbaban 	if (mapping)
1325651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1326c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1327c5c4113dSnw 	return (retcode);
1328c5c4113dSnw }
1329c5c4113dSnw 
1330c5c4113dSnw 
1331c5c4113dSnw /*
1332c5c4113dSnw  * Given GID, get SID
1333c5c4113dSnw  *
1334c5c4113dSnw  * Input:
1335c5c4113dSnw  * gid  - POSIX GID
1336c5c4113dSnw  * flag - flag
1337c5c4113dSnw  *
1338c5c4113dSnw  * Output:
1339c5c4113dSnw  * stat       - status of the get request
1340c5c4113dSnw  * sidprefix  - SID prefix (if stat == 0)
1341c5c4113dSnw  * rid        - rid
1342c5c4113dSnw  *
1343c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1344c5c4113dSnw  */
1345c5c4113dSnw idmap_stat
1346c5c4113dSnw idmap_get_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
1347cd37da74Snw 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
1348cd37da74Snw {
134948258c6bSjp 	return (idmap_getext_sidbygid(gh, gid, flag, sidprefix, rid,
135048258c6bSjp 	    NULL, stat));
135148258c6bSjp }
135248258c6bSjp 
135348258c6bSjp 
135448258c6bSjp /*
135548258c6bSjp  * Given GID, get SID
135648258c6bSjp  *
135748258c6bSjp  * Input:
135848258c6bSjp  * gid  - POSIX GID
135948258c6bSjp  * flag - flag
136048258c6bSjp  *
136148258c6bSjp  * Output:
136248258c6bSjp  * stat       - status of the get request
136348258c6bSjp  * sidprefix  - SID prefix (if stat == 0)
136448258c6bSjp  * rid        - rid
136548258c6bSjp  * how        - mapping type if stat = 0
136648258c6bSjp  *
136748258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
136848258c6bSjp  */
136948258c6bSjp idmap_stat
137048258c6bSjp idmap_getext_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
137148258c6bSjp 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
137248258c6bSjp {
1373c5c4113dSnw 
1374c5c4113dSnw 	idmap_retcode	retcode;
1375651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1376c5c4113dSnw 
1377c5c4113dSnw 	/* sanity checks */
1378c5c4113dSnw 	if (gh == NULL)
1379c5c4113dSnw 		return (IDMAP_ERR_ARG);
1380c5c4113dSnw 	if (sidprefix == NULL)
1381c5c4113dSnw 		return (IDMAP_ERR_ARG);
1382c5c4113dSnw 
13833ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
13843ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
13853ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_sidbygid(sidprefix, rid, gid);
13863ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
13873ee87bcaSJulian Pullen 			*stat = retcode;
13883ee87bcaSJulian Pullen 			return (retcode);
13893ee87bcaSJulian Pullen 		}
13903ee87bcaSJulian Pullen 	}
13913ee87bcaSJulian Pullen 
1392c5c4113dSnw 	/* Extend the request array and the return list */
1393c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1394c5c4113dSnw 		goto errout;
1395c5c4113dSnw 
1396c5c4113dSnw 	/* Setup the request */
1397c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1398c5c4113dSnw 	mapping->flag = flag;
1399c5c4113dSnw 	mapping->id1.idtype = IDMAP_GID;
1400c5c4113dSnw 	mapping->id1.idmap_id_u.gid = gid;
1401c5c4113dSnw 	mapping->id2.idtype = IDMAP_SID;
1402c5c4113dSnw 
1403c5c4113dSnw 	/* Setup pointers for the result */
1404c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_SID;
1405c5c4113dSnw 	gh->retlist[gh->next].sidprefix = sidprefix;
1406c5c4113dSnw 	gh->retlist[gh->next].rid = rid;
1407c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
140848258c6bSjp 	gh->retlist[gh->next].info = info;
14093ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1410c5c4113dSnw 
1411c5c4113dSnw 	gh->next++;
1412c5c4113dSnw 	return (IDMAP_SUCCESS);
1413c5c4113dSnw 
1414c5c4113dSnw errout:
1415651c0131Sbaban 	if (mapping)
1416651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1417c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1418c5c4113dSnw 	return (retcode);
1419c5c4113dSnw }
1420c5c4113dSnw 
1421c5c4113dSnw 
1422c5c4113dSnw /*
1423c5c4113dSnw  * Process the batched "get mapping" requests. The results (i.e.
1424c5c4113dSnw  * status and identity) will be available in the data areas
1425c5c4113dSnw  * provided by individual requests.
1426c5c4113dSnw  */
1427c5c4113dSnw idmap_stat
1428cd37da74Snw idmap_get_mappings(idmap_get_handle_t *gh)
1429cd37da74Snw {
1430c5c4113dSnw 	idmap_retcode	retcode;
1431c5c4113dSnw 	idmap_ids_res	res;
14323ee87bcaSJulian Pullen 	idmap_id	*res_id;
1433c5c4113dSnw 	int		i;
14343ee87bcaSJulian Pullen 	idmap_id	*req_id;
14353ee87bcaSJulian Pullen 	int		direction;
1436c5c4113dSnw 
1437c5c4113dSnw 	if (gh == NULL) {
1438c5c4113dSnw 		errno = EINVAL;
1439c5c4113dSnw 		return (IDMAP_ERR_ARG);
1440c5c4113dSnw 	}
1441c5c4113dSnw 
1442c5c4113dSnw 	(void) memset(&res, 0, sizeof (idmap_ids_res));
14431fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_IDS,
1444cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping_batch,
1445cd37da74Snw 	    (caddr_t)&gh->batch,
1446cd37da74Snw 	    (xdrproc_t)xdr_idmap_ids_res,
1447cd37da74Snw 	    (caddr_t)&res,
1448cd37da74Snw 	    TIMEOUT);
14491fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS) {
1450c5c4113dSnw 		goto out;
1451c5c4113dSnw 	}
1452c5c4113dSnw 	if (res.retcode != IDMAP_SUCCESS) {
1453c5c4113dSnw 		retcode = res.retcode;
1454c5c4113dSnw 		goto out;
1455c5c4113dSnw 	}
1456c5c4113dSnw 	for (i = 0; i < gh->next; i++) {
1457c5c4113dSnw 		if (i >= res.ids.ids_len) {
1458c5c4113dSnw 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
1459c5c4113dSnw 			continue;
1460c5c4113dSnw 		}
1461c5c4113dSnw 		*gh->retlist[i].stat = res.ids.ids_val[i].retcode;
14623ee87bcaSJulian Pullen 		res_id = &res.ids.ids_val[i].id;
14633ee87bcaSJulian Pullen 		direction = res.ids.ids_val[i].direction;
14643ee87bcaSJulian Pullen 		req_id = &gh->batch.idmap_mapping_batch_val[i].id1;
14653ee87bcaSJulian Pullen 		switch (res_id->idtype) {
1466c5c4113dSnw 		case IDMAP_UID:
1467c5c4113dSnw 			if (gh->retlist[i].uid)
14683ee87bcaSJulian Pullen 				*gh->retlist[i].uid = res_id->idmap_id_u.uid;
1469c5c4113dSnw 			if (gh->retlist[i].is_user)
1470c5c4113dSnw 				*gh->retlist[i].is_user = 1;
14713ee87bcaSJulian Pullen 
14723ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
14733ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
14743ee87bcaSJulian Pullen 				if (gh->retlist[i].is_user != NULL)
14753ee87bcaSJulian Pullen 					idmap_cache_add_sid2pid(
14763ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
14773ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
14783ee87bcaSJulian Pullen 					    res_id->idmap_id_u.uid, 1,
14793ee87bcaSJulian Pullen 					    direction);
14803ee87bcaSJulian Pullen 				else
14813ee87bcaSJulian Pullen 					idmap_cache_add_sid2uid(
14823ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
14833ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
14843ee87bcaSJulian Pullen 					    res_id->idmap_id_u.uid,
14853ee87bcaSJulian Pullen 					    direction);
14863ee87bcaSJulian Pullen 			}
1487c5c4113dSnw 			break;
14883ee87bcaSJulian Pullen 
1489c5c4113dSnw 		case IDMAP_GID:
1490c5c4113dSnw 			if (gh->retlist[i].gid)
14913ee87bcaSJulian Pullen 				*gh->retlist[i].gid = res_id->idmap_id_u.gid;
1492c5c4113dSnw 			if (gh->retlist[i].is_user)
1493c5c4113dSnw 				*gh->retlist[i].is_user = 0;
14943ee87bcaSJulian Pullen 
14953ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
14963ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
14973ee87bcaSJulian Pullen 				if (gh->retlist[i].is_user != NULL)
14983ee87bcaSJulian Pullen 					idmap_cache_add_sid2pid(
14993ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
15003ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
15013ee87bcaSJulian Pullen 					    res_id->idmap_id_u.gid, 0,
15023ee87bcaSJulian Pullen 					    direction);
15033ee87bcaSJulian Pullen 				else
15043ee87bcaSJulian Pullen 					idmap_cache_add_sid2gid(
15053ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
15063ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
15073ee87bcaSJulian Pullen 					    res_id->idmap_id_u.gid,
15083ee87bcaSJulian Pullen 					    direction);
15093ee87bcaSJulian Pullen 			}
1510c5c4113dSnw 			break;
15113ee87bcaSJulian Pullen 
151262c60062Sbaban 		case IDMAP_POSIXID:
151362c60062Sbaban 			if (gh->retlist[i].uid)
151462c60062Sbaban 				*gh->retlist[i].uid = 60001;
151562c60062Sbaban 			if (gh->retlist[i].is_user)
151662c60062Sbaban 				*gh->retlist[i].is_user = -1;
151762c60062Sbaban 			break;
15183ee87bcaSJulian Pullen 
1519c5c4113dSnw 		case IDMAP_SID:
1520cd37da74Snw 		case IDMAP_USID:
1521cd37da74Snw 		case IDMAP_GSID:
1522c5c4113dSnw 			if (gh->retlist[i].rid)
15233ee87bcaSJulian Pullen 				*gh->retlist[i].rid =
15243ee87bcaSJulian Pullen 				    res_id->idmap_id_u.sid.rid;
1525c5c4113dSnw 			if (gh->retlist[i].sidprefix) {
15263ee87bcaSJulian Pullen 				if (res_id->idmap_id_u.sid.prefix == NULL ||
15273ee87bcaSJulian Pullen 				    *res_id->idmap_id_u.sid.prefix == '\0') {
1528c5c4113dSnw 					*gh->retlist[i].sidprefix = NULL;
1529c5c4113dSnw 					break;
1530c5c4113dSnw 				}
1531c5c4113dSnw 				*gh->retlist[i].sidprefix =
15323ee87bcaSJulian Pullen 				    strdup(res_id->idmap_id_u.sid.prefix);
1533c5c4113dSnw 				if (*gh->retlist[i].sidprefix == NULL)
1534c5c4113dSnw 					*gh->retlist[i].stat =
1535cd37da74Snw 					    IDMAP_ERR_MEMORY;
1536c5c4113dSnw 			}
15373ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
15383ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
15393ee87bcaSJulian Pullen 				if (req_id->idtype == IDMAP_UID)
15403ee87bcaSJulian Pullen 					idmap_cache_add_sid2uid(
15413ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.prefix,
15423ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.rid,
15433ee87bcaSJulian Pullen 					    req_id->idmap_id_u.uid,
15443ee87bcaSJulian Pullen 					    direction);
15453ee87bcaSJulian Pullen 				else /* req_id->idtype == IDMAP_GID */
15463ee87bcaSJulian Pullen 					idmap_cache_add_sid2gid(
15473ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.prefix,
15483ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.rid,
15493ee87bcaSJulian Pullen 					    req_id->idmap_id_u.gid,
15503ee87bcaSJulian Pullen 					    direction);
15513ee87bcaSJulian Pullen 			}
1552c5c4113dSnw 			break;
15533ee87bcaSJulian Pullen 
1554c5c4113dSnw 		case IDMAP_NONE:
1555c5c4113dSnw 			break;
15563ee87bcaSJulian Pullen 
1557c5c4113dSnw 		default:
1558c5c4113dSnw 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
1559c5c4113dSnw 			break;
1560c5c4113dSnw 		}
1561148c5f43SAlan Wright 		if (gh->retlist[i].info != NULL) {
1562148c5f43SAlan Wright 			idmap_info_mov(gh->retlist[i].info,
156348258c6bSjp 			    &res.ids.ids_val[i].info);
1564148c5f43SAlan Wright 		}
1565c5c4113dSnw 	}
1566c5c4113dSnw 	retcode = IDMAP_SUCCESS;
1567c5c4113dSnw 
1568c5c4113dSnw out:
1569651c0131Sbaban 	_IDMAP_RESET_GET_HANDLE(gh);
1570c5c4113dSnw 	(void) xdr_free(xdr_idmap_ids_res, (caddr_t)&res);
1571c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1572c5c4113dSnw 	return (retcode);
1573c5c4113dSnw }
1574c5c4113dSnw 
1575c5c4113dSnw 
1576c5c4113dSnw /*
1577c5c4113dSnw  * Destroy the "get mapping" handle
1578c5c4113dSnw  */
1579c5c4113dSnw void
1580cd37da74Snw idmap_get_destroy(idmap_get_handle_t *gh)
1581cd37da74Snw {
1582c5c4113dSnw 	if (gh == NULL)
1583c5c4113dSnw 		return;
1584c5c4113dSnw 	(void) xdr_free(xdr_idmap_mapping_batch, (caddr_t)&gh->batch);
1585c5c4113dSnw 	if (gh->retlist)
1586c5c4113dSnw 		free(gh->retlist);
1587c5c4113dSnw 	free(gh);
1588c5c4113dSnw }
1589c5c4113dSnw 
1590c5c4113dSnw 
1591c5c4113dSnw /*
1592c5c4113dSnw  * Get windows to unix mapping
1593c5c4113dSnw  */
1594c5c4113dSnw idmap_stat
15951fdeec65Sjoyce mcintosh idmap_get_w2u_mapping(
1596c5c4113dSnw 		const char *sidprefix, idmap_rid_t *rid,
1597c5c4113dSnw 		const char *winname, const char *windomain,
1598cd37da74Snw 		int flag, int *is_user, int *is_wuser,
159948258c6bSjp 		uid_t *pid, char **unixname, int *direction, idmap_info *info)
1600cd37da74Snw {
1601c5c4113dSnw 	idmap_mapping		request, *mapping;
1602c5c4113dSnw 	idmap_mappings_res	result;
1603c5c4113dSnw 	idmap_retcode		retcode, rc;
1604c5c4113dSnw 
1605c5c4113dSnw 	(void) memset(&request, 0, sizeof (request));
1606c5c4113dSnw 	(void) memset(&result, 0, sizeof (result));
1607c5c4113dSnw 
1608c5c4113dSnw 	if (pid)
1609c5c4113dSnw 		*pid = UINT32_MAX;
1610c5c4113dSnw 	if (unixname)
1611c5c4113dSnw 		*unixname = NULL;
1612c5c4113dSnw 	if (direction)
1613651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
1614c5c4113dSnw 
1615c5c4113dSnw 	request.flag = flag;
1616c5c4113dSnw 	request.id1.idtype = IDMAP_SID;
1617c5c4113dSnw 	if (sidprefix && rid) {
1618c5c4113dSnw 		request.id1.idmap_id_u.sid.prefix = (char *)sidprefix;
1619c5c4113dSnw 		request.id1.idmap_id_u.sid.rid = *rid;
1620c5c4113dSnw 	} else if (winname) {
16218e228215Sdm 		retcode = idmap_strdupnull(&request.id1name, winname);
1622c5a946baSbaban 		if (retcode != IDMAP_SUCCESS)
1623c5c4113dSnw 			goto out;
16248e228215Sdm 
16258e228215Sdm 		retcode = idmap_strdupnull(&request.id1domain, windomain);
1626c5a946baSbaban 		if (retcode != IDMAP_SUCCESS)
16278e228215Sdm 			goto out;
16288e228215Sdm 
1629c5c4113dSnw 		request.id1.idmap_id_u.sid.prefix = NULL;
1630c5c4113dSnw 	} else {
1631c5c4113dSnw 		errno = EINVAL;
1632c5c4113dSnw 		return (IDMAP_ERR_ARG);
1633c5c4113dSnw 	}
1634c5c4113dSnw 
1635cd37da74Snw 	if (*is_user == 1)
1636c5c4113dSnw 		request.id2.idtype = IDMAP_UID;
1637c5c4113dSnw 	else if (*is_user == 0)
1638c5c4113dSnw 		request.id2.idtype = IDMAP_GID;
1639c5c4113dSnw 	else
1640c5c4113dSnw 		request.id2.idtype = IDMAP_POSIXID;
1641c5c4113dSnw 
1642cd37da74Snw 	if (*is_wuser == 1)
1643cd37da74Snw 		request.id1.idtype = IDMAP_USID;
1644cd37da74Snw 	else if (*is_wuser == 0)
1645cd37da74Snw 		request.id1.idtype = IDMAP_GSID;
1646cd37da74Snw 	else
1647cd37da74Snw 		request.id1.idtype = IDMAP_SID;
1648cd37da74Snw 
16491fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_ID_BY_NAME,
1650cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
1651cd37da74Snw 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
1652cd37da74Snw 	    TIMEOUT);
1653c5c4113dSnw 
16541fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
16551fdeec65Sjoyce mcintosh 		return (retcode);
1656c5c4113dSnw 
1657c5c4113dSnw 	retcode = result.retcode;
1658c5c4113dSnw 
1659c5c4113dSnw 	if ((mapping = result.mappings.mappings_val) == NULL) {
1660c5c4113dSnw 		if (retcode == IDMAP_SUCCESS)
1661c5c4113dSnw 			retcode = IDMAP_ERR_NORESULT;
1662c5c4113dSnw 		goto out;
1663c5c4113dSnw 	}
1664c5c4113dSnw 
1665148c5f43SAlan Wright 	if (info != NULL)
1666148c5f43SAlan Wright 		idmap_info_mov(info, &mapping->info);
1667148c5f43SAlan Wright 
166862c60062Sbaban 	if (mapping->id2.idtype == IDMAP_UID) {
1669cd37da74Snw 		*is_user = 1;
167062c60062Sbaban 	} else if (mapping->id2.idtype == IDMAP_GID) {
1671cd37da74Snw 		*is_user = 0;
167262c60062Sbaban 	} else {
167362c60062Sbaban 		goto out;
167462c60062Sbaban 	}
1675cd37da74Snw 
1676cd37da74Snw 	if (mapping->id1.idtype == IDMAP_USID) {
1677cd37da74Snw 		*is_wuser = 1;
1678cd37da74Snw 	} else if (mapping->id1.idtype == IDMAP_GSID) {
1679cd37da74Snw 		*is_wuser = 0;
1680cd37da74Snw 	} else {
1681cd37da74Snw 		goto out;
1682cd37da74Snw 	}
1683cd37da74Snw 
1684c5c4113dSnw 	if (direction)
1685c5c4113dSnw 		*direction = mapping->direction;
1686c5c4113dSnw 	if (pid)
1687c5c4113dSnw 		*pid = mapping->id2.idmap_id_u.uid;
16888e228215Sdm 
16898e228215Sdm 	rc = idmap_strdupnull(unixname, mapping->id2name);
16908e228215Sdm 	if (rc != IDMAP_SUCCESS)
16918e228215Sdm 		retcode = rc;
1692c5c4113dSnw 
1693c5c4113dSnw out:
1694f7b4b2feSjp 	if (request.id1name != NULL)
1695f7b4b2feSjp 		free(request.id1name);
1696f7b4b2feSjp 	if (request.id1domain != NULL)
1697f7b4b2feSjp 		free(request.id1domain);
1698c5c4113dSnw 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
1699c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
1700c5c4113dSnw 		errno = idmap_stat2errno(retcode);
1701c5c4113dSnw 	return (retcode);
1702c5c4113dSnw }
1703c5c4113dSnw 
1704c5c4113dSnw 
1705c5c4113dSnw /*
1706c5c4113dSnw  * Get unix to windows mapping
1707c5c4113dSnw  */
1708c5c4113dSnw idmap_stat
17091fdeec65Sjoyce mcintosh idmap_get_u2w_mapping(
1710c5c4113dSnw 		uid_t *pid, const char *unixname,
1711cd37da74Snw 		int flag, int is_user, int *is_wuser,
1712c5c4113dSnw 		char **sidprefix, idmap_rid_t *rid,
1713c5c4113dSnw 		char **winname, char **windomain,
171448258c6bSjp 		int *direction, idmap_info *info)
1715cd37da74Snw {
1716c5c4113dSnw 	idmap_mapping		request, *mapping;
1717c5c4113dSnw 	idmap_mappings_res	result;
1718c5c4113dSnw 	idmap_retcode		retcode, rc;
1719c5c4113dSnw 
1720c5c4113dSnw 	if (sidprefix)
1721c5c4113dSnw 		*sidprefix = NULL;
1722c5c4113dSnw 	if (winname)
1723c5c4113dSnw 		*winname = NULL;
1724c5c4113dSnw 	if (windomain)
1725c5c4113dSnw 		*windomain = NULL;
1726c5c4113dSnw 	if (rid)
1727c5c4113dSnw 		*rid = UINT32_MAX;
1728c5c4113dSnw 	if (direction)
1729651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
1730c5c4113dSnw 
1731c5c4113dSnw 	(void) memset(&request, 0, sizeof (request));
1732c5c4113dSnw 	(void) memset(&result, 0, sizeof (result));
1733c5c4113dSnw 
1734c5c4113dSnw 	request.flag = flag;
1735c5c4113dSnw 	request.id1.idtype = is_user?IDMAP_UID:IDMAP_GID;
1736c5c4113dSnw 
1737c5c4113dSnw 	if (pid && *pid != UINT32_MAX) {
1738c5c4113dSnw 		request.id1.idmap_id_u.uid = *pid;
1739c5c4113dSnw 	} else if (unixname) {
17408e228215Sdm 		request.id1name = (char *)unixname;
1741c5c4113dSnw 		request.id1.idmap_id_u.uid = UINT32_MAX;
1742c5c4113dSnw 	} else {
1743c5c4113dSnw 		errno = EINVAL;
1744c5c4113dSnw 		return (IDMAP_ERR_ARG);
1745c5c4113dSnw 	}
1746c5c4113dSnw 
1747cd37da74Snw 	if (is_wuser == NULL)
1748cd37da74Snw 		request.id2.idtype = IDMAP_SID;
1749cd37da74Snw 	else if (*is_wuser == -1)
1750cd37da74Snw 		request.id2.idtype = IDMAP_SID;
1751cd37da74Snw 	else if (*is_wuser == 0)
1752cd37da74Snw 		request.id2.idtype = IDMAP_GSID;
1753cd37da74Snw 	else if (*is_wuser == 1)
1754cd37da74Snw 		request.id2.idtype = IDMAP_USID;
1755c5c4113dSnw 
17561fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_ID_BY_NAME,
1757cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
1758cd37da74Snw 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
1759cd37da74Snw 	    TIMEOUT);
1760c5c4113dSnw 
17611fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
17621fdeec65Sjoyce mcintosh 		return (retcode);
1763c5c4113dSnw 
1764c5c4113dSnw 	retcode = result.retcode;
1765c5c4113dSnw 
1766c5c4113dSnw 	if ((mapping = result.mappings.mappings_val) == NULL) {
1767c5c4113dSnw 		if (retcode == IDMAP_SUCCESS)
1768c5c4113dSnw 			retcode = IDMAP_ERR_NORESULT;
1769c5c4113dSnw 		goto out;
1770c5c4113dSnw 	}
1771c5c4113dSnw 
1772148c5f43SAlan Wright 	if (info != NULL)
1773148c5f43SAlan Wright 		idmap_info_mov(info, &mapping->info);
1774148c5f43SAlan Wright 
1775cd37da74Snw 	if (direction != NULL)
1776c5c4113dSnw 		*direction = mapping->direction;
1777cd37da74Snw 
177848258c6bSjp 	if (is_wuser != NULL) {
177948258c6bSjp 		if (mapping->id2.idtype == IDMAP_USID)
178048258c6bSjp 			*is_wuser = 1;
178148258c6bSjp 		else if (mapping->id2.idtype == IDMAP_GSID)
178248258c6bSjp 			*is_wuser = 0;
178348258c6bSjp 		else
178448258c6bSjp 			*is_wuser = -1;
178548258c6bSjp 	}
1786cd37da74Snw 
17878edda628Sbaban 	if (sidprefix && mapping->id2.idmap_id_u.sid.prefix &&
17888edda628Sbaban 	    *mapping->id2.idmap_id_u.sid.prefix != '\0') {
1789c5c4113dSnw 		*sidprefix = strdup(mapping->id2.idmap_id_u.sid.prefix);
1790c5c4113dSnw 		if (*sidprefix == NULL) {
1791c5c4113dSnw 			retcode = IDMAP_ERR_MEMORY;
1792c5c4113dSnw 			goto errout;
1793c5c4113dSnw 		}
1794c5c4113dSnw 	}
1795c5c4113dSnw 	if (rid)
1796c5c4113dSnw 		*rid = mapping->id2.idmap_id_u.sid.rid;
17978e228215Sdm 
17988e228215Sdm 	rc = idmap_strdupnull(winname, mapping->id2name);
17998e228215Sdm 	if (rc != IDMAP_SUCCESS)
18008e228215Sdm 		retcode = rc;
18018e228215Sdm 
18028e228215Sdm 	rc = idmap_strdupnull(windomain, mapping->id2domain);
18038e228215Sdm 	if (rc != IDMAP_SUCCESS)
18048e228215Sdm 		retcode = rc;
1805c5c4113dSnw 
1806c5c4113dSnw 	goto out;
1807c5c4113dSnw 
1808c5c4113dSnw errout:
1809c5c4113dSnw 	if (sidprefix && *sidprefix) {
1810c5c4113dSnw 		free(*sidprefix);
1811c5c4113dSnw 		*sidprefix = NULL;
1812c5c4113dSnw 	}
1813c5c4113dSnw 	if (winname && *winname) {
1814c5c4113dSnw 		free(*winname);
1815c5c4113dSnw 		*winname = NULL;
1816c5c4113dSnw 	}
1817c5c4113dSnw 	if (windomain && *windomain) {
1818c5c4113dSnw 		free(*windomain);
1819c5c4113dSnw 		*windomain = NULL;
1820c5c4113dSnw 	}
1821c5c4113dSnw 
1822c5c4113dSnw out:
1823c5c4113dSnw 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
1824c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
1825c5c4113dSnw 		errno = idmap_stat2errno(retcode);
1826c5c4113dSnw 	return (retcode);
1827c5c4113dSnw }
1828c5c4113dSnw 
1829c5c4113dSnw 
1830c5c4113dSnw 
1831c5c4113dSnw #define	gettext(s)	s
1832c5c4113dSnw static stat_table_t stattable[] = {
1833c5c4113dSnw 	{IDMAP_SUCCESS, gettext("Success"), 0},
1834c5c4113dSnw 	{IDMAP_NEXT, gettext("More results available"), 0},
1835c5c4113dSnw 	{IDMAP_ERR_OTHER, gettext("Undefined error"), EINVAL},
1836c5c4113dSnw 	{IDMAP_ERR_INTERNAL, gettext("Internal error"), EINVAL},
1837c5c4113dSnw 	{IDMAP_ERR_MEMORY, gettext("Out of memory"), ENOMEM},
1838c5c4113dSnw 	{IDMAP_ERR_NORESULT, gettext("No results available"), EINVAL},
1839c5c4113dSnw 	{IDMAP_ERR_NOTUSER, gettext("Not a user"), EINVAL},
1840c5c4113dSnw 	{IDMAP_ERR_NOTGROUP, gettext("Not a group"), EINVAL},
1841651c0131Sbaban 	{IDMAP_ERR_NOTSUPPORTED, gettext("Operation not supported"), ENOTSUP},
1842c5c4113dSnw 	{IDMAP_ERR_W2U_NAMERULE,
1843c5c4113dSnw 		gettext("Invalid Windows to UNIX name-based rule"), EINVAL},
1844c5c4113dSnw 	{IDMAP_ERR_U2W_NAMERULE,
1845c5c4113dSnw 		gettext("Invalid UNIX to Windows name-based rule"), EINVAL},
1846c5c4113dSnw 	{IDMAP_ERR_CACHE, gettext("Invalid cache"), EINVAL},
1847c5c4113dSnw 	{IDMAP_ERR_DB, gettext("Invalid database"), EINVAL},
1848c5c4113dSnw 	{IDMAP_ERR_ARG, gettext("Invalid argument"), EINVAL},
1849c5c4113dSnw 	{IDMAP_ERR_SID, gettext("Invalid SID"), EINVAL},
1850c5c4113dSnw 	{IDMAP_ERR_IDTYPE, gettext("Invalid identity type"), EINVAL},
1851651c0131Sbaban 	{IDMAP_ERR_RPC_HANDLE, gettext("Bad RPC handle"), EBADF},
1852c5c4113dSnw 	{IDMAP_ERR_RPC, gettext("RPC error"), EINVAL},
1853c5c4113dSnw 	{IDMAP_ERR_CLIENT_HANDLE, gettext("Bad client handle"), EINVAL},
1854651c0131Sbaban 	{IDMAP_ERR_BUSY, gettext("Server is busy"), EBUSY},
18558edda628Sbaban 	{IDMAP_ERR_PERMISSION_DENIED, gettext("Permission denied"), EACCES},
1856c5c4113dSnw 	{IDMAP_ERR_NOMAPPING,
1857c5c4113dSnw 		gettext("Mapping not found or inhibited"), EINVAL},
1858c5c4113dSnw 	{IDMAP_ERR_NEW_ID_ALLOC_REQD,
1859c5c4113dSnw 		gettext("New mapping needs to be created"), EINVAL},
1860c5c4113dSnw 	{IDMAP_ERR_DOMAIN, gettext("Invalid domain"), EINVAL},
1861c5c4113dSnw 	{IDMAP_ERR_SECURITY, gettext("Security issue"), EINVAL},
1862c5c4113dSnw 	{IDMAP_ERR_NOTFOUND, gettext("Not found"), EINVAL},
1863c5c4113dSnw 	{IDMAP_ERR_DOMAIN_NOTFOUND, gettext("Domain not found"), EINVAL},
1864c5c4113dSnw 	{IDMAP_ERR_UPDATE_NOTALLOWED, gettext("Update not allowed"), EINVAL},
1865c5c4113dSnw 	{IDMAP_ERR_CFG, gettext("Configuration error"), EINVAL},
1866c5c4113dSnw 	{IDMAP_ERR_CFG_CHANGE, gettext("Invalid configuration change"), EINVAL},
1867c5c4113dSnw 	{IDMAP_ERR_NOTMAPPED_WELLKNOWN,
1868c5c4113dSnw 		gettext("No mapping for well-known SID"), EINVAL},
1869c5c4113dSnw 	{IDMAP_ERR_RETRIABLE_NET_ERR,
187062c60062Sbaban 		gettext("Windows lookup failed"), EINVAL},
187162c60062Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
187262c60062Sbaban 		gettext("Duplicate rule or conflicts with an existing "
187362c60062Sbaban 		"Windows to UNIX name-based rule"), EINVAL},
187462c60062Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
187562c60062Sbaban 		gettext("Duplicate rule or conflicts with an existing "
187662c60062Sbaban 		"Unix to Windows name-based rule"), EINVAL},
18770dcc7149Snw 	{IDMAP_ERR_BAD_UTF8,
18780dcc7149Snw 		gettext("Invalid or illegal UTF-8 sequence found in "
18790dcc7149Snw 		"a given Windows entity name or domain name"), EINVAL},
18804d61c878SJulian Pullen 	{IDMAP_ERR_NONE_GENERATED,
188148258c6bSjp 		gettext("Mapping not found and none created (see -c option)"),
188248258c6bSjp 		EINVAL},
1883479ac375Sdm 	{IDMAP_ERR_PROP_UNKNOWN,
1884479ac375Sdm 		gettext("Undefined property"),
1885479ac375Sdm 		EINVAL},
1886479ac375Sdm 	{IDMAP_ERR_NS_LDAP_CFG,
1887479ac375Sdm 		gettext("Native LDAP configuration error"), EINVAL},
1888479ac375Sdm 	{IDMAP_ERR_NS_LDAP_PARTIAL,
1889479ac375Sdm 		gettext("Partial result from Native LDAP"), EINVAL},
1890479ac375Sdm 	{IDMAP_ERR_NS_LDAP_OP_FAILED,
1891479ac375Sdm 		gettext("Native LDAP operation failed"), EINVAL},
1892479ac375Sdm 	{IDMAP_ERR_NS_LDAP_BAD_WINNAME,
1893479ac375Sdm 		gettext("Improper winname form found in Native LDAP"), EINVAL},
18944d61c878SJulian Pullen 	{IDMAP_ERR_NO_ACTIVEDIRECTORY,
18954d61c878SJulian Pullen 		gettext("No AD servers"),
18964d61c878SJulian Pullen 		EINVAL},
1897c5c4113dSnw 	{-1, NULL, 0}
1898c5c4113dSnw };
1899c5c4113dSnw #undef	gettext
1900c5c4113dSnw 
1901c5c4113dSnw 
1902c5c4113dSnw /*
1903c5c4113dSnw  * Get description of status code
1904c5c4113dSnw  *
1905c5c4113dSnw  * Input:
1906c5c4113dSnw  * status - Status code returned by libidmap API call
1907c5c4113dSnw  *
1908c5c4113dSnw  * Return Value:
1909c5c4113dSnw  * human-readable localized description of idmap_stat
1910c5c4113dSnw  */
1911c5c4113dSnw const char *
19121fdeec65Sjoyce mcintosh idmap_stat2string(idmap_stat status)
1913cd37da74Snw {
1914c5c4113dSnw 	int i;
1915c5c4113dSnw 
1916c5c4113dSnw 	for (i = 0; stattable[i].msg; i++) {
1917c5c4113dSnw 		if (stattable[i].retcode == status)
19181fcced4cSJordan Brown 			return (dgettext(TEXT_DOMAIN, stattable[i].msg));
1919c5c4113dSnw 	}
19201fcced4cSJordan Brown 	return (dgettext(TEXT_DOMAIN, "Unknown error"));
1921c5c4113dSnw }
1922c5c4113dSnw 
1923c5c4113dSnw 
1924c5c4113dSnw static int
1925cd37da74Snw idmap_stat2errno(idmap_stat stat)
1926cd37da74Snw {
1927c5c4113dSnw 	int i;
1928c5c4113dSnw 	for (i = 0; stattable[i].msg; i++) {
1929c5c4113dSnw 		if (stattable[i].retcode == stat)
1930c5c4113dSnw 			return (stattable[i].errnum);
1931c5c4113dSnw 	}
1932c5c4113dSnw 	return (EINVAL);
1933c5c4113dSnw }
1934c5c4113dSnw 
1935c5c4113dSnw 
1936c5c4113dSnw /*
1937c5c4113dSnw  * Get status code from string
1938c5c4113dSnw  */
1939c5c4113dSnw idmap_stat
1940cd37da74Snw idmap_string2stat(const char *str)
1941cd37da74Snw {
1942c5c4113dSnw 	if (str == NULL)
1943c5c4113dSnw 		return (IDMAP_ERR_INTERNAL);
1944c5c4113dSnw 
1945c5c4113dSnw #define	return_cmp(a) \
1946c5c4113dSnw 	if (0 == strcmp(str, "IDMAP_ERR_" #a)) \
1947c5c4113dSnw 		return (IDMAP_ERR_ ## a);
1948c5c4113dSnw 
1949c5c4113dSnw 	return_cmp(OTHER);
1950c5c4113dSnw 	return_cmp(INTERNAL);
1951c5c4113dSnw 	return_cmp(MEMORY);
1952c5c4113dSnw 	return_cmp(NORESULT);
1953c5c4113dSnw 	return_cmp(NOTUSER);
1954c5c4113dSnw 	return_cmp(NOTGROUP);
1955c5c4113dSnw 	return_cmp(NOTSUPPORTED);
1956c5c4113dSnw 	return_cmp(W2U_NAMERULE);
1957c5c4113dSnw 	return_cmp(U2W_NAMERULE);
1958c5c4113dSnw 	return_cmp(CACHE);
1959c5c4113dSnw 	return_cmp(DB);
1960c5c4113dSnw 	return_cmp(ARG);
1961c5c4113dSnw 	return_cmp(SID);
1962c5c4113dSnw 	return_cmp(IDTYPE);
1963c5c4113dSnw 	return_cmp(RPC_HANDLE);
1964c5c4113dSnw 	return_cmp(RPC);
1965c5c4113dSnw 	return_cmp(CLIENT_HANDLE);
1966c5c4113dSnw 	return_cmp(BUSY);
1967c5c4113dSnw 	return_cmp(PERMISSION_DENIED);
1968c5c4113dSnw 	return_cmp(NOMAPPING);
1969c5c4113dSnw 	return_cmp(NEW_ID_ALLOC_REQD);
1970c5c4113dSnw 	return_cmp(DOMAIN);
1971c5c4113dSnw 	return_cmp(SECURITY);
1972c5c4113dSnw 	return_cmp(NOTFOUND);
1973c5c4113dSnw 	return_cmp(DOMAIN_NOTFOUND);
1974c5c4113dSnw 	return_cmp(MEMORY);
1975c5c4113dSnw 	return_cmp(UPDATE_NOTALLOWED);
1976c5c4113dSnw 	return_cmp(CFG);
1977c5c4113dSnw 	return_cmp(CFG_CHANGE);
1978c5c4113dSnw 	return_cmp(NOTMAPPED_WELLKNOWN);
1979c5c4113dSnw 	return_cmp(RETRIABLE_NET_ERR);
198062c60062Sbaban 	return_cmp(W2U_NAMERULE_CONFLICT);
198162c60062Sbaban 	return_cmp(U2W_NAMERULE_CONFLICT);
1982479ac375Sdm 	return_cmp(BAD_UTF8);
19834d61c878SJulian Pullen 	return_cmp(NONE_GENERATED);
1984479ac375Sdm 	return_cmp(PROP_UNKNOWN);
1985479ac375Sdm 	return_cmp(NS_LDAP_CFG);
1986479ac375Sdm 	return_cmp(NS_LDAP_PARTIAL);
1987479ac375Sdm 	return_cmp(NS_LDAP_OP_FAILED);
1988479ac375Sdm 	return_cmp(NS_LDAP_BAD_WINNAME);
19894d61c878SJulian Pullen 	return_cmp(NO_ACTIVEDIRECTORY);
1990c5c4113dSnw #undef return_cmp
1991c5c4113dSnw 
1992c5c4113dSnw 	return (IDMAP_ERR_OTHER);
1993c5c4113dSnw }
1994c5c4113dSnw 
1995c5c4113dSnw 
1996c5c4113dSnw /*
1997c5c4113dSnw  * Map the given status to one that can be returned by the protocol
1998c5c4113dSnw  */
1999c5c4113dSnw idmap_stat
2000cd37da74Snw idmap_stat4prot(idmap_stat status)
2001cd37da74Snw {
2002c5c4113dSnw 	switch (status) {
2003c5c4113dSnw 	case IDMAP_ERR_MEMORY:
2004c5c4113dSnw 	case IDMAP_ERR_CACHE:
2005c5c4113dSnw 		return (IDMAP_ERR_INTERNAL);
2006c5c4113dSnw 	}
2007c5c4113dSnw 	return (status);
2008c5c4113dSnw }
2009dd5829d1Sbaban 
2010dd5829d1Sbaban 
20118e228215Sdm /*
2012c5a946baSbaban  * This is a convenience routine which duplicates a string after
2013c5a946baSbaban  * checking for NULL pointers. This function will return success if
2014c5a946baSbaban  * either the 'to' OR 'from' pointers are NULL.
20158e228215Sdm  */
20168e228215Sdm static idmap_stat
2017cd37da74Snw idmap_strdupnull(char **to, const char *from)
2018cd37da74Snw {
2019c5a946baSbaban 	if (to == NULL)
2020c5a946baSbaban 		return (IDMAP_SUCCESS);
2021c5a946baSbaban 
20228e228215Sdm 	if (from == NULL || *from == '\0') {
20238e228215Sdm 		*to = NULL;
20248e228215Sdm 		return (IDMAP_SUCCESS);
20258e228215Sdm 	}
20268e228215Sdm 
20278e228215Sdm 	*to = strdup(from);
20288e228215Sdm 	if (*to == NULL)
20298e228215Sdm 		return (IDMAP_ERR_MEMORY);
20308e228215Sdm 	return (IDMAP_SUCCESS);
20318e228215Sdm }
20328e228215Sdm 
203348258c6bSjp 
20348e228215Sdm idmap_stat
2035cd37da74Snw idmap_namerule_cpy(idmap_namerule *to, idmap_namerule *from)
2036cd37da74Snw {
20378e228215Sdm 	idmap_stat retval;
20388e228215Sdm 
203948258c6bSjp 	if (to == NULL)
204048258c6bSjp 		return (IDMAP_SUCCESS);
204148258c6bSjp 
20428e228215Sdm 	(void) memcpy(to, from, sizeof (idmap_namerule));
204348258c6bSjp 	to->windomain = NULL;
204448258c6bSjp 	to->winname = NULL;
204548258c6bSjp 	to->unixname = NULL;
20468e228215Sdm 
20478e228215Sdm 	retval = idmap_strdupnull(&to->windomain, from->windomain);
20488e228215Sdm 	if (retval != IDMAP_SUCCESS)
20498e228215Sdm 		return (retval);
20508e228215Sdm 
20518e228215Sdm 	retval = idmap_strdupnull(&to->winname, from->winname);
205248258c6bSjp 	if (retval != IDMAP_SUCCESS) {
205348258c6bSjp 		free(to->windomain);
205448258c6bSjp 		to->windomain = NULL;
20558e228215Sdm 		return (retval);
205648258c6bSjp 	}
20578e228215Sdm 
20588e228215Sdm 	retval = idmap_strdupnull(&to->unixname, from->unixname);
205948258c6bSjp 	if (retval != IDMAP_SUCCESS) {
206048258c6bSjp 		free(to->windomain);
206148258c6bSjp 		to->windomain = NULL;
206248258c6bSjp 		free(to->winname);
206348258c6bSjp 		to->winname = NULL;
206448258c6bSjp 		return (retval);
206548258c6bSjp 	}
206648258c6bSjp 
206748258c6bSjp 	return (retval);
206848258c6bSjp }
206948258c6bSjp 
207048258c6bSjp 
207148258c6bSjp /*
2072148c5f43SAlan Wright  * Move the contents of the "info" structure from "from" to "to".
207348258c6bSjp  */
2074148c5f43SAlan Wright void
207548258c6bSjp idmap_info_mov(idmap_info *to, idmap_info *from)
207648258c6bSjp {
207748258c6bSjp 	(void) memcpy(to, from, sizeof (idmap_info));
207848258c6bSjp 	(void) memset(from, 0, sizeof (idmap_info));
20798e228215Sdm }
20808e228215Sdm 
20818e228215Sdm 
208248258c6bSjp void
208348258c6bSjp idmap_info_free(idmap_info *info)
208448258c6bSjp {
208548258c6bSjp 	if (info == NULL)
208648258c6bSjp 		return;
208748258c6bSjp 
2088148c5f43SAlan Wright 	xdr_free(xdr_idmap_info, (caddr_t)info);
2089148c5f43SAlan Wright 	(void) memset(info, 0, sizeof (idmap_info));
2090148c5f43SAlan Wright }
209148258c6bSjp 
209248258c6bSjp 
2093148c5f43SAlan Wright void
2094148c5f43SAlan Wright idmap_how_clear(idmap_how *how)
2095148c5f43SAlan Wright {
2096148c5f43SAlan Wright 	xdr_free(xdr_idmap_how, (caddr_t)how);
2097148c5f43SAlan Wright 	(void) memset(how, 0, sizeof (*how));
209848258c6bSjp }
209948258c6bSjp 
210048258c6bSjp 
2101dd5829d1Sbaban /*
2102dd5829d1Sbaban  * Get uid given Windows name
2103dd5829d1Sbaban  */
2104dd5829d1Sbaban idmap_stat
21053ee87bcaSJulian Pullen idmap_getuidbywinname(const char *name, const char *domain, int flag,
21063ee87bcaSJulian Pullen 	uid_t *uid)
2107cd37da74Snw {
2108dd5829d1Sbaban 	idmap_retcode	rc;
2109cd37da74Snw 	int		is_user = 1;
2110cd37da74Snw 	int		is_wuser = -1;
21113ee87bcaSJulian Pullen 	int 		direction;
2112dd5829d1Sbaban 
2113dd5829d1Sbaban 	if (uid == NULL)
2114dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2115dd5829d1Sbaban 
21163ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21173ee87bcaSJulian Pullen 		rc = idmap_cache_lookup_uidbywinname(name, domain, uid);
21183ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
21193ee87bcaSJulian Pullen 			return (rc);
21203ee87bcaSJulian Pullen 	}
2121dd5829d1Sbaban 	/* Get mapping */
21221fdeec65Sjoyce mcintosh 	rc = idmap_get_w2u_mapping(NULL, NULL, name, domain, flag,
21233ee87bcaSJulian Pullen 	    &is_user, &is_wuser, uid, NULL, &direction, NULL);
2124dd5829d1Sbaban 
21253ee87bcaSJulian Pullen 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
21263ee87bcaSJulian Pullen 		/* If we have not got the domain don't store UID to winname */
21273ee87bcaSJulian Pullen 		if (domain == NULL)
21283ee87bcaSJulian Pullen 			direction = IDMAP_DIRECTION_W2U;
21293ee87bcaSJulian Pullen 		idmap_cache_add_winname2uid(name, domain, *uid, direction);
21303ee87bcaSJulian Pullen 	}
21313ee87bcaSJulian Pullen 
2132dd5829d1Sbaban 	return (rc);
2133dd5829d1Sbaban }
2134dd5829d1Sbaban 
2135dd5829d1Sbaban 
2136dd5829d1Sbaban /*
2137dd5829d1Sbaban  * Get gid given Windows name
2138dd5829d1Sbaban  */
2139dd5829d1Sbaban idmap_stat
21403ee87bcaSJulian Pullen idmap_getgidbywinname(const char *name, const char *domain, int flag,
21413ee87bcaSJulian Pullen 	gid_t *gid)
2142cd37da74Snw {
2143dd5829d1Sbaban 	idmap_retcode	rc;
2144cd37da74Snw 	int		is_user = 0;
2145cd37da74Snw 	int		is_wuser = -1;
21463ee87bcaSJulian Pullen 	int		direction;
2147dd5829d1Sbaban 
2148dd5829d1Sbaban 	if (gid == NULL)
2149dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2150dd5829d1Sbaban 
21513ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21523ee87bcaSJulian Pullen 		rc = idmap_cache_lookup_gidbywinname(name, domain, gid);
21533ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
21543ee87bcaSJulian Pullen 			return (rc);
21553ee87bcaSJulian Pullen 	}
21563ee87bcaSJulian Pullen 
2157dd5829d1Sbaban 	/* Get mapping */
21581fdeec65Sjoyce mcintosh 	rc = idmap_get_w2u_mapping(NULL, NULL, name, domain, flag,
21593ee87bcaSJulian Pullen 	    &is_user, &is_wuser, gid, NULL, &direction, NULL);
2160dd5829d1Sbaban 
21613ee87bcaSJulian Pullen 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
21623ee87bcaSJulian Pullen 		/* If we have not got the domain don't store GID to winname */
21633ee87bcaSJulian Pullen 		if (domain == NULL)
21643ee87bcaSJulian Pullen 			direction = IDMAP_DIRECTION_W2U;
21653ee87bcaSJulian Pullen 		idmap_cache_add_winname2gid(name, domain, *gid, direction);
21663ee87bcaSJulian Pullen 	}
21673ee87bcaSJulian Pullen 
2168dd5829d1Sbaban 	return (rc);
2169dd5829d1Sbaban }
2170dd5829d1Sbaban 
2171dd5829d1Sbaban 
2172dd5829d1Sbaban /*
2173dd5829d1Sbaban  * Get winname given pid
2174dd5829d1Sbaban  */
2175dd5829d1Sbaban static idmap_retcode
21763ee87bcaSJulian Pullen idmap_getwinnamebypid(uid_t pid, int is_user, int flag, char **name,
21773ee87bcaSJulian Pullen 	char **domain)
2178cd37da74Snw {
2179dd5829d1Sbaban 	idmap_retcode	rc;
2180dd5829d1Sbaban 	int		len;
2181dd5829d1Sbaban 	char		*winname, *windomain;
21823ee87bcaSJulian Pullen 	int		direction;
2183dd5829d1Sbaban 
2184dd5829d1Sbaban 	if (name == NULL)
2185dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2186dd5829d1Sbaban 
21873ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21883ee87bcaSJulian Pullen 		if (is_user)
21893ee87bcaSJulian Pullen 			rc = idmap_cache_lookup_winnamebyuid(&winname,
21903ee87bcaSJulian Pullen 			    &windomain, pid);
21913ee87bcaSJulian Pullen 		else
21923ee87bcaSJulian Pullen 			rc = idmap_cache_lookup_winnamebygid(&winname,
21933ee87bcaSJulian Pullen 			    &windomain, pid);
21943ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS)
21953ee87bcaSJulian Pullen 			goto out;
21963ee87bcaSJulian Pullen 		if (rc == IDMAP_ERR_MEMORY)
21973ee87bcaSJulian Pullen 			return (rc);
21983ee87bcaSJulian Pullen 	}
21993ee87bcaSJulian Pullen 
2200dd5829d1Sbaban 	/* Get mapping */
22011fdeec65Sjoyce mcintosh 	rc = idmap_get_u2w_mapping(&pid, NULL, flag, is_user, NULL,
22023ee87bcaSJulian Pullen 	    NULL, NULL, &winname, &windomain, &direction, NULL);
2203dd5829d1Sbaban 
2204dd5829d1Sbaban 	/* Return on error */
2205dd5829d1Sbaban 	if (rc != IDMAP_SUCCESS)
2206dd5829d1Sbaban 		return (rc);
2207dd5829d1Sbaban 
2208dd5829d1Sbaban 	/*
2209dd5829d1Sbaban 	 * The given PID may have been mapped to a locally
2210dd5829d1Sbaban 	 * generated SID in which case there isn't any
2211dd5829d1Sbaban 	 * Windows name
2212dd5829d1Sbaban 	 */
2213dd5829d1Sbaban 	if (winname == NULL || windomain == NULL) {
2214dd5829d1Sbaban 		idmap_free(winname);
2215dd5829d1Sbaban 		idmap_free(windomain);
2216dd5829d1Sbaban 		return (IDMAP_ERR_NORESULT);
2217dd5829d1Sbaban 	}
2218dd5829d1Sbaban 
22193ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
22203ee87bcaSJulian Pullen 		if (is_user)
22213ee87bcaSJulian Pullen 			idmap_cache_add_winname2uid(winname, windomain,
22223ee87bcaSJulian Pullen 			    pid, direction);
22233ee87bcaSJulian Pullen 		else
22243ee87bcaSJulian Pullen 			idmap_cache_add_winname2gid(winname, windomain,
22253ee87bcaSJulian Pullen 			    pid, direction);
22263ee87bcaSJulian Pullen 	}
22273ee87bcaSJulian Pullen 
22283ee87bcaSJulian Pullen out:
2229dd5829d1Sbaban 	if (domain != NULL) {
2230dd5829d1Sbaban 		*name = winname;
2231dd5829d1Sbaban 		*domain = windomain;
2232dd5829d1Sbaban 	} else {
2233dd5829d1Sbaban 		len = strlen(winname) + strlen(windomain) + 2;
2234dd5829d1Sbaban 		if ((*name = malloc(len)) != NULL)
2235dd5829d1Sbaban 			(void) snprintf(*name, len, "%s@%s", winname,
2236dd5829d1Sbaban 			    windomain);
2237dd5829d1Sbaban 		else
2238dd5829d1Sbaban 			rc = IDMAP_ERR_MEMORY;
2239dd5829d1Sbaban 		idmap_free(winname);
2240dd5829d1Sbaban 		idmap_free(windomain);
2241dd5829d1Sbaban 	}
22423ee87bcaSJulian Pullen 
2243dd5829d1Sbaban 	return (rc);
2244dd5829d1Sbaban }
2245dd5829d1Sbaban 
2246dd5829d1Sbaban 
2247dd5829d1Sbaban /*
2248dd5829d1Sbaban  * Get winname given uid
2249dd5829d1Sbaban  */
2250dd5829d1Sbaban idmap_stat
22513ee87bcaSJulian Pullen idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
2252cd37da74Snw {
22533ee87bcaSJulian Pullen 	return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
2254dd5829d1Sbaban }
2255dd5829d1Sbaban 
2256dd5829d1Sbaban 
2257dd5829d1Sbaban /*
2258dd5829d1Sbaban  * Get winname given gid
2259dd5829d1Sbaban  */
2260dd5829d1Sbaban idmap_stat
22613ee87bcaSJulian Pullen idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
2262cd37da74Snw {
22633ee87bcaSJulian Pullen 	return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
2264dd5829d1Sbaban }
22659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States idmap_stat
22671fdeec65Sjoyce mcintosh idmap_flush(idmap_flush_op op)
22689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
22691fdeec65Sjoyce mcintosh 	idmap_retcode		rc1, rc2;
22709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22711fdeec65Sjoyce mcintosh 	rc1 = _idmap_clnt_call(IDMAP_FLUSH,
22729fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    (xdrproc_t)xdr_idmap_flush_op, (caddr_t)&op,
22731fdeec65Sjoyce mcintosh 	    (xdrproc_t)xdr_idmap_retcode, (caddr_t)&rc2, TIMEOUT);
22749fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22751fdeec65Sjoyce mcintosh 	if (rc1 != IDMAP_SUCCESS)
22761fdeec65Sjoyce mcintosh 		return (rc1);
22771fdeec65Sjoyce mcintosh 	return (rc2);
22789fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
2279c5866007SKeyur Desai 
2280c5866007SKeyur Desai 
2281c5866007SKeyur Desai /*
2282c5866007SKeyur Desai  * syslog is the default logger.
2283c5866007SKeyur Desai  * It can be overwritten by supplying a logger
2284c5866007SKeyur Desai  * with  idmap_set_logger()
2285c5866007SKeyur Desai  */
2286c5866007SKeyur Desai idmap_logger_t logger = syslog;
2287c5866007SKeyur Desai 
2288c5866007SKeyur Desai 
2289c5866007SKeyur Desai void
2290c5866007SKeyur Desai idmap_set_logger(idmap_logger_t funct)
2291c5866007SKeyur Desai {
2292c5866007SKeyur Desai 	logger = funct;
2293c5866007SKeyur Desai }
2294148c5f43SAlan Wright 
2295148c5f43SAlan Wright /*
2296148c5f43SAlan Wright  * Helper functions that concatenate two parts of a name and then
2297148c5f43SAlan Wright  * look up a value, so that the same set of functions can be used to
2298148c5f43SAlan Wright  * process both "in" and "out" parameters.
2299148c5f43SAlan Wright  */
2300148c5f43SAlan Wright static
2301148c5f43SAlan Wright boolean_t
2302148c5f43SAlan Wright idmap_trace_get_str(nvlist_t *entry, char *n1, char *n2, char **ret)
2303148c5f43SAlan Wright {
2304148c5f43SAlan Wright 	char name[IDMAP_TRACE_NAME_MAX+1];	/* Max used is about 11 */
2305148c5f43SAlan Wright 	int err;
2306148c5f43SAlan Wright 
2307148c5f43SAlan Wright 	(void) strlcpy(name, n1, sizeof (name));
2308148c5f43SAlan Wright 	if (n2 != NULL)
2309148c5f43SAlan Wright 		(void) strlcat(name, n2, sizeof (name));
2310148c5f43SAlan Wright 
2311148c5f43SAlan Wright 	err = nvlist_lookup_string(entry, name, ret);
2312148c5f43SAlan Wright 	return (err == 0);
2313148c5f43SAlan Wright }
2314148c5f43SAlan Wright 
2315148c5f43SAlan Wright static
2316148c5f43SAlan Wright boolean_t
2317148c5f43SAlan Wright idmap_trace_get_int(nvlist_t *entry, char *n1, char *n2, int64_t *ret)
2318148c5f43SAlan Wright {
2319148c5f43SAlan Wright 	char name[IDMAP_TRACE_NAME_MAX+1];	/* Max used is about 11 */
2320148c5f43SAlan Wright 	int err;
2321148c5f43SAlan Wright 
2322148c5f43SAlan Wright 	(void) strlcpy(name, n1, sizeof (name));
2323148c5f43SAlan Wright 	if (n2 != NULL)
2324148c5f43SAlan Wright 		(void) strlcat(name, n2, sizeof (name));
2325148c5f43SAlan Wright 
2326148c5f43SAlan Wright 	err = nvlist_lookup_int64(entry, name, ret);
2327148c5f43SAlan Wright 	return (err == 0);
2328148c5f43SAlan Wright }
2329148c5f43SAlan Wright 
2330148c5f43SAlan Wright static
2331148c5f43SAlan Wright void
2332148c5f43SAlan Wright idmap_trace_print_id(FILE *out, nvlist_t *entry, char *fromto)
2333148c5f43SAlan Wright {
2334148c5f43SAlan Wright 	char *s;
2335148c5f43SAlan Wright 	int64_t i64;
2336148c5f43SAlan Wright 
2337148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, fromto, IDMAP_TRACE_TYPE, &i64)) {
2338148c5f43SAlan Wright 		switch (i64) {
2339148c5f43SAlan Wright 		case IDMAP_POSIXID:
2340148c5f43SAlan Wright 			(void) fprintf(out, "unixname ");
2341148c5f43SAlan Wright 			break;
2342148c5f43SAlan Wright 		case IDMAP_UID:
2343148c5f43SAlan Wright 			(void) fprintf(out, "unixuser ");
2344148c5f43SAlan Wright 			break;
2345148c5f43SAlan Wright 		case IDMAP_GID:
2346148c5f43SAlan Wright 			(void) fprintf(out, "unixgroup ");
2347148c5f43SAlan Wright 			break;
2348148c5f43SAlan Wright 		case IDMAP_SID:
2349148c5f43SAlan Wright 			(void) fprintf(out, "winname ");
2350148c5f43SAlan Wright 			break;
2351148c5f43SAlan Wright 		case IDMAP_USID:
2352148c5f43SAlan Wright 			(void) fprintf(out, "winuser ");
2353148c5f43SAlan Wright 			break;
2354148c5f43SAlan Wright 		case IDMAP_GSID:
2355148c5f43SAlan Wright 			(void) fprintf(out, "wingroup ");
2356148c5f43SAlan Wright 			break;
2357148c5f43SAlan Wright 		case IDMAP_NONE:
2358148c5f43SAlan Wright 			(void) fprintf(out, gettext("unknown "));
2359148c5f43SAlan Wright 			break;
2360148c5f43SAlan Wright 		default:
2361148c5f43SAlan Wright 			(void) fprintf(out, gettext("bad %d "), (int)i64);
2362148c5f43SAlan Wright 			break;
2363148c5f43SAlan Wright 		}
2364148c5f43SAlan Wright 	}
2365148c5f43SAlan Wright 
2366148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, fromto, IDMAP_TRACE_NAME, &s))
2367148c5f43SAlan Wright 		(void) fprintf(out, "%s ", s);
2368148c5f43SAlan Wright 
2369148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, fromto, IDMAP_TRACE_SID, &s))
2370148c5f43SAlan Wright 		(void) fprintf(out, "%s ", s);
2371148c5f43SAlan Wright 
2372148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, fromto, IDMAP_TRACE_UNIXID, &i64))
2373148c5f43SAlan Wright 		(void) fprintf(out, "%u ", (uid_t)i64);
2374148c5f43SAlan Wright }
2375148c5f43SAlan Wright 
2376148c5f43SAlan Wright void
2377148c5f43SAlan Wright idmap_trace_print_1(FILE *out, char *prefix, nvlist_t *entry)
2378148c5f43SAlan Wright {
2379148c5f43SAlan Wright 	char *s;
2380148c5f43SAlan Wright 	int64_t i64;
2381148c5f43SAlan Wright 
2382148c5f43SAlan Wright 	(void) fprintf(out, "%s", prefix);
2383148c5f43SAlan Wright 	idmap_trace_print_id(out, entry, "from");
2384148c5f43SAlan Wright 	(void) fprintf(out, "-> ");
2385148c5f43SAlan Wright 	idmap_trace_print_id(out, entry, "to");
2386148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, IDMAP_TRACE_ERROR, NULL, &i64))
2387148c5f43SAlan Wright 		(void) fprintf(out, gettext("Error %d "), (int)i64);
2388148c5f43SAlan Wright 	(void) fprintf(out, "-");
2389148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, IDMAP_TRACE_MESSAGE, NULL, &s))
2390148c5f43SAlan Wright 		(void) fprintf(out, " %s", s);
2391148c5f43SAlan Wright 	(void) fprintf(out, "\n");
2392148c5f43SAlan Wright }
2393148c5f43SAlan Wright 
2394148c5f43SAlan Wright void
2395148c5f43SAlan Wright idmap_trace_print(FILE *out, char *prefix, nvlist_t *trace)
2396148c5f43SAlan Wright {
2397148c5f43SAlan Wright 	nvpair_t *nvp;
2398148c5f43SAlan Wright 
2399148c5f43SAlan Wright 	for (nvp = nvlist_next_nvpair(trace, NULL);
2400148c5f43SAlan Wright 	    nvp != NULL;
2401148c5f43SAlan Wright 	    nvp = nvlist_next_nvpair(trace, nvp)) {
2402148c5f43SAlan Wright 		nvlist_t *entry;
2403148c5f43SAlan Wright 		int err;
2404148c5f43SAlan Wright 
2405148c5f43SAlan Wright 		err = nvpair_value_nvlist(nvp, &entry);
2406148c5f43SAlan Wright 		assert(err == 0);
2407148c5f43SAlan Wright 
2408148c5f43SAlan Wright 		idmap_trace_print_1(out, prefix, entry);
2409148c5f43SAlan Wright 	}
2410148c5f43SAlan Wright }
2411