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.
23ad8ef92aSMilan Jurik  * Copyright Milan Jurik 2012. All rights reserved.
248ce3a038SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
2529d55245SJerry Jelinek  * Copyright 2015 Joyent, Inc.
26c5c4113dSnw  */
27c5c4113dSnw 
28c5c4113dSnw 
29c5c4113dSnw /*
30c5c4113dSnw  * libidmap API
31c5c4113dSnw  */
32c5c4113dSnw 
33c5c4113dSnw #include <stdlib.h>
34479ac375Sdm #include <sys/varargs.h>
35c5c4113dSnw #include <inttypes.h>
36c5c4113dSnw #include <errno.h>
37c5c4113dSnw #include <strings.h>
38c5c4113dSnw #include <ctype.h>
39c5c4113dSnw #include <sys/param.h>
40c5c4113dSnw #include <sys/types.h>
41c5c4113dSnw #include <sys/stat.h>
42c5c4113dSnw #include <dlfcn.h>
43c5c4113dSnw #include <libintl.h>
44c5866007SKeyur Desai #include <syslog.h>
45148c5f43SAlan Wright #include <assert.h>
46c5c4113dSnw #include "idmap_impl.h"
473ee87bcaSJulian Pullen #include "idmap_cache.h"
48c5c4113dSnw 
49c5c4113dSnw static struct timeval TIMEOUT = { 25, 0 };
50c5c4113dSnw 
51c5c4113dSnw static int idmap_stat2errno(idmap_stat);
52479ac375Sdm static idmap_stat	idmap_strdupnull(char **, const char *);
53c5c4113dSnw 
541fdeec65Sjoyce mcintosh #define	__ITER_CREATE(itera, argu, ityp)\
55c5c4113dSnw 	itera = calloc(1, sizeof (*itera));\
56c5c4113dSnw 	if (itera == NULL) {\
57c5c4113dSnw 		errno = ENOMEM;\
58c5c4113dSnw 		return (IDMAP_ERR_MEMORY);\
59c5c4113dSnw 	}\
60c5c4113dSnw 	argu = calloc(1, sizeof (*argu));\
61c5c4113dSnw 	if (argu == NULL) {\
62c5c4113dSnw 		free(itera);\
63c5c4113dSnw 		errno = ENOMEM;\
64c5c4113dSnw 		return (IDMAP_ERR_MEMORY);\
65c5c4113dSnw 	}\
66c5c4113dSnw 	itera->type = ityp;\
67c5c4113dSnw 	itera->retcode = IDMAP_NEXT;\
68c5c4113dSnw 	itera->limit = 1024;\
69c5c4113dSnw 	itera->arg = argu;
70c5c4113dSnw 
71c5c4113dSnw #define	__ITER_CHECK(itera, ityp)\
72c5c4113dSnw 	if (itera == NULL) {\
73c5c4113dSnw 		errno = EINVAL;\
74c5c4113dSnw 		return (IDMAP_ERR_ARG);\
75c5c4113dSnw 	}\
76c5c4113dSnw 	if (itera->type != ityp) {\
77c5c4113dSnw 		errno = EINVAL;\
78c5c4113dSnw 		return (IDMAP_ERR_ARG);\
79c5c4113dSnw 	}
80c5c4113dSnw 
81c5c4113dSnw /*
82c5c4113dSnw  * Free memory allocated by libidmap API
83c5c4113dSnw  *
84c5c4113dSnw  * Input:
85c5c4113dSnw  * ptr - memory to be freed
86c5c4113dSnw  */
87c5c4113dSnw void
idmap_free(void * ptr)88cd37da74Snw idmap_free(void *ptr)
89cd37da74Snw {
90c5c4113dSnw 	free(ptr);
91c5c4113dSnw }
92c5c4113dSnw 
93c5c4113dSnw 
944d61c878SJulian Pullen static idmap_stat
idmap_get_prop(idmap_prop_type pr,idmap_prop_res * res)951fdeec65Sjoyce mcintosh idmap_get_prop(idmap_prop_type pr, idmap_prop_res *res)
96479ac375Sdm {
971fdeec65Sjoyce mcintosh 	idmap_stat retcode;
98479ac375Sdm 
99479ac375Sdm 	(void) memset(res, 0, sizeof (*res));
100479ac375Sdm 
1011fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_PROP,
102479ac375Sdm 	    (xdrproc_t)xdr_idmap_prop_type, (caddr_t)&pr,
103479ac375Sdm 	    (xdrproc_t)xdr_idmap_prop_res, (caddr_t)res, TIMEOUT);
1041fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
1051fdeec65Sjoyce mcintosh 		return (retcode);
106479ac375Sdm 
107479ac375Sdm 	return (res->retcode); /* This might not be IDMAP_SUCCESS! */
108479ac375Sdm }
109479ac375Sdm 
1104d61c878SJulian Pullen 
111479ac375Sdm idmap_stat
idmap_get_prop_ds(idmap_prop_type pr,idmap_ad_disc_ds_t * dc)1121fdeec65Sjoyce mcintosh idmap_get_prop_ds(idmap_prop_type pr, idmap_ad_disc_ds_t *dc)
113479ac375Sdm {
114479ac375Sdm 	idmap_prop_res res;
115479ac375Sdm 	idmap_stat rc = IDMAP_SUCCESS;
116479ac375Sdm 
1171fdeec65Sjoyce mcintosh 	rc = idmap_get_prop(pr, &res);
118479ac375Sdm 	if (rc < 0)
119479ac375Sdm 		return (rc);
120479ac375Sdm 
121479ac375Sdm 	dc->port = res.value.idmap_prop_val_u.dsval.port;
122479ac375Sdm 	(void) strlcpy(dc->host, res.value.idmap_prop_val_u.dsval.host,
123479ac375Sdm 	    AD_DISC_MAXHOSTNAME);
124479ac375Sdm 
125479ac375Sdm 	/* xdr doesn't guarantee 0-termination of char[]: */
126479ac375Sdm 	dc->host[AD_DISC_MAXHOSTNAME - 1] = '\0';
127479ac375Sdm 
128479ac375Sdm 	return (rc);
129479ac375Sdm }
130479ac375Sdm 
131479ac375Sdm 
132479ac375Sdm /*
133479ac375Sdm  * Sometimes the property is not set. In that case, str is set to NULL but
134479ac375Sdm  * otherwise IDMAP_SUCCESS is returned.
135479ac375Sdm  */
136479ac375Sdm idmap_stat
idmap_get_prop_str(idmap_prop_type pr,char ** str)1371fdeec65Sjoyce mcintosh idmap_get_prop_str(idmap_prop_type pr, char **str)
138479ac375Sdm {
139479ac375Sdm 	idmap_prop_res res;
140479ac375Sdm 	idmap_stat rc = IDMAP_SUCCESS;
141479ac375Sdm 
1421fdeec65Sjoyce mcintosh 	rc = idmap_get_prop(pr, &res);
143479ac375Sdm 	if (rc < 0)
144479ac375Sdm 		return (rc);
145479ac375Sdm 
146479ac375Sdm 	rc = idmap_strdupnull(str, res.value.idmap_prop_val_u.utf8val);
147479ac375Sdm 	return (rc);
148479ac375Sdm }
149c5c4113dSnw 
150c5c4113dSnw /*
151c5c4113dSnw  * Create/Initialize handle for updates
152c5c4113dSnw  *
153c5c4113dSnw  * Output:
154c5c4113dSnw  * udthandle - update handle
155c5c4113dSnw  */
156c5c4113dSnw idmap_stat
idmap_udt_create(idmap_udt_handle_t ** udthandle)1571fdeec65Sjoyce mcintosh idmap_udt_create(idmap_udt_handle_t **udthandle)
158cd37da74Snw {
159c5c4113dSnw 	idmap_udt_handle_t	*tmp;
160c5c4113dSnw 
1611fdeec65Sjoyce mcintosh 	if (udthandle == NULL) {
162c5c4113dSnw 		errno = EINVAL;
163c5c4113dSnw 		return (IDMAP_ERR_ARG);
164c5c4113dSnw 	}
165c5c4113dSnw 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
166c5c4113dSnw 		errno = ENOMEM;
167c5c4113dSnw 		return (IDMAP_ERR_MEMORY);
168c5c4113dSnw 	}
169c5c4113dSnw 
170c5c4113dSnw 	*udthandle = tmp;
171c5c4113dSnw 	return (IDMAP_SUCCESS);
172c5c4113dSnw }
173c5c4113dSnw 
174c5c4113dSnw 
175c5c4113dSnw /*
176c5c4113dSnw  * All the updates specified by the update handle are committed
177c5c4113dSnw  * in a single transaction. i.e either all succeed or none.
178c5c4113dSnw  *
179c5c4113dSnw  * Input:
180c5c4113dSnw  * udthandle - update handle with the update requests
181c5c4113dSnw  *
182c5c4113dSnw  * Return value:
183c5c4113dSnw  * Status of the commit
184c5c4113dSnw  */
185c5c4113dSnw idmap_stat
idmap_udt_commit(idmap_udt_handle_t * udthandle)186cd37da74Snw idmap_udt_commit(idmap_udt_handle_t *udthandle)
187cd37da74Snw {
1888e228215Sdm 	idmap_update_res	res;
1898e228215Sdm 	idmap_stat		retcode;
190c5c4113dSnw 
191c5c4113dSnw 	if (udthandle == NULL) {
192c5c4113dSnw 		errno = EINVAL;
193c5c4113dSnw 		return (IDMAP_ERR_ARG);
194c5c4113dSnw 	}
1958e228215Sdm 
1968e228215Sdm 	(void) memset(&res, 0, sizeof (res));
1978e228215Sdm 
1981fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_UPDATE,
199cd37da74Snw 	    (xdrproc_t)xdr_idmap_update_batch, (caddr_t)&udthandle->batch,
200cd37da74Snw 	    (xdrproc_t)xdr_idmap_update_res, (caddr_t)&res,
201cd37da74Snw 	    TIMEOUT);
2021fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
2038e228215Sdm 		goto out;
2048e228215Sdm 
2058e228215Sdm 	retcode = udthandle->commit_stat = res.retcode;
2068e228215Sdm 	udthandle->error_index = res.error_index;
2078e228215Sdm 
2088e228215Sdm 	if (retcode != IDMAP_SUCCESS) {
2098e228215Sdm 
2108e228215Sdm 		if (udthandle->error_index < 0)
2118e228215Sdm 			goto out;
2128e228215Sdm 
2138e228215Sdm 		retcode = idmap_namerule_cpy(&udthandle->error_rule,
2148e228215Sdm 		    &res.error_rule);
2158e228215Sdm 		if (retcode != IDMAP_SUCCESS) {
2168e228215Sdm 			udthandle->error_index = -2;
2178e228215Sdm 			goto out;
2188e228215Sdm 		}
2198e228215Sdm 
2208e228215Sdm 		retcode = idmap_namerule_cpy(&udthandle->conflict_rule,
2218e228215Sdm 		    &res.conflict_rule);
2228e228215Sdm 		if (retcode != IDMAP_SUCCESS) {
2238e228215Sdm 			udthandle->error_index = -2;
2248e228215Sdm 			goto out;
2258e228215Sdm 		}
2268e228215Sdm 	}
2278e228215Sdm 
2288e228215Sdm 	retcode = res.retcode;
2298e228215Sdm 
2308e228215Sdm 
2318e228215Sdm out:
232651c0131Sbaban 	/* reset handle so that it can be used again */
2338e228215Sdm 	if (retcode == IDMAP_SUCCESS) {
2348e228215Sdm 		_IDMAP_RESET_UDT_HANDLE(udthandle);
2358e228215Sdm 	}
236651c0131Sbaban 
237*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_update_res, (caddr_t)&res);
2388e228215Sdm 	errno = idmap_stat2errno(retcode);
2398e228215Sdm 	return (retcode);
2408e228215Sdm }
2418e228215Sdm 
2428e228215Sdm 
2438e228215Sdm static void
idmap_namerule_parts_clear(char ** windomain,char ** winname,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,boolean_t * is_nt4,int * direction)2448e228215Sdm idmap_namerule_parts_clear(char **windomain, char **winname,
245cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
246cd37da74Snw     boolean_t *is_nt4, int *direction)
247cd37da74Snw {
2488e228215Sdm 	if (windomain)
2498e228215Sdm 		*windomain = NULL;
2508e228215Sdm 	if (winname)
2518e228215Sdm 		*winname = NULL;
2528e228215Sdm 	if (unixname)
2538e228215Sdm 		*unixname = NULL;
2548e228215Sdm 
2558e228215Sdm 	if (is_nt4)
2568e228215Sdm 		*is_nt4 = 0;
2578e228215Sdm 	if (is_user)
2588e228215Sdm 		*is_user = -1;
259cd37da74Snw 	if (is_wuser)
260cd37da74Snw 		*is_wuser = -1;
2618e228215Sdm 	if (direction)
2628e228215Sdm 		*direction = IDMAP_DIRECTION_UNDEF;
2638e228215Sdm }
2648e228215Sdm 
2658e228215Sdm static idmap_stat
idmap_namerule2parts(idmap_namerule * rule,char ** windomain,char ** winname,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,boolean_t * is_nt4,int * direction)266cd37da74Snw idmap_namerule2parts(idmap_namerule *rule,
2678e228215Sdm     char **windomain, char **winname,
268cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
269cd37da74Snw     boolean_t *is_nt4, int *direction)
270cd37da74Snw {
2718e228215Sdm 	idmap_stat retcode;
2728e228215Sdm 
2738e228215Sdm 	if (EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
2748e228215Sdm 		return (IDMAP_ERR_NORESULT);
2758e228215Sdm 
2768e228215Sdm 
2778e228215Sdm 	retcode = idmap_strdupnull(windomain, rule->windomain);
278c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
2798e228215Sdm 		goto errout;
2808e228215Sdm 
2818e228215Sdm 	retcode = idmap_strdupnull(winname, rule->winname);
2828e228215Sdm 	if (retcode != IDMAP_SUCCESS)
2838e228215Sdm 		goto errout;
2848e228215Sdm 
2858e228215Sdm 	retcode = idmap_strdupnull(unixname, rule->unixname);
2868e228215Sdm 	if (retcode != IDMAP_SUCCESS)
2878e228215Sdm 		goto errout;
2888e228215Sdm 
2898e228215Sdm 
2908e228215Sdm 	if (is_user)
2918e228215Sdm 		*is_user = rule->is_user;
292cd37da74Snw 	if (is_wuser)
293cd37da74Snw 		*is_wuser = rule->is_wuser;
2948e228215Sdm 	if (is_nt4)
2958e228215Sdm 		*is_nt4 = rule->is_nt4;
2968e228215Sdm 	if (direction)
2978e228215Sdm 		*direction = rule->direction;
2988e228215Sdm 
2998e228215Sdm 
3008e228215Sdm 	return (IDMAP_SUCCESS);
3018e228215Sdm 
3028e228215Sdm errout:
3038e228215Sdm 	if (windomain && *windomain)
3048e228215Sdm 		free(*windomain);
3058e228215Sdm 	if (winname && *winname)
3068e228215Sdm 		free(*winname);
3078e228215Sdm 	if (unixname && *unixname)
3088e228215Sdm 		free(*unixname);
3098e228215Sdm 
3108e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
311cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3128e228215Sdm 
313c5c4113dSnw 	return (retcode);
3148e228215Sdm 
3158e228215Sdm }
3168e228215Sdm 
3178e228215Sdm /*
3188e228215Sdm  * Retrieve the index of the failed batch element. error_index == -1
3198e228215Sdm  * indicates failure at the beginning, -2 at the end.
3208e228215Sdm  *
3218e228215Sdm  * If idmap_udt_commit didn't return error, the returned value is undefined.
3228e228215Sdm  *
3238e228215Sdm  * Return value:
3248e228215Sdm  * IDMAP_SUCCESS
3258e228215Sdm  */
3268e228215Sdm 
3278e228215Sdm idmap_stat
idmap_udt_get_error_index(idmap_udt_handle_t * udthandle,int64_t * error_index)3288e228215Sdm idmap_udt_get_error_index(idmap_udt_handle_t *udthandle,
329cd37da74Snw     int64_t *error_index)
330cd37da74Snw {
3318e228215Sdm 	if (error_index)
3328e228215Sdm 		*error_index = udthandle->error_index;
3338e228215Sdm 
3348e228215Sdm 	return (IDMAP_SUCCESS);
3358e228215Sdm }
3368e228215Sdm 
3378e228215Sdm 
3388e228215Sdm /*
3398e228215Sdm  * Retrieve the rule which caused the batch to fail. If
3408e228215Sdm  * idmap_udt_commit didn't return error or if error_index is < 0, the
3418e228215Sdm  * retrieved rule is undefined.
3428e228215Sdm  *
3438e228215Sdm  * Return value:
3448e228215Sdm  * IDMAP_ERR_NORESULT if there is no error rule.
3458e228215Sdm  * IDMAP_SUCCESS if the rule was obtained OK.
3468e228215Sdm  * other error code (IDMAP_ERR_NOMEMORY etc)
3478e228215Sdm  */
3488e228215Sdm 
3498e228215Sdm idmap_stat
idmap_udt_get_error_rule(idmap_udt_handle_t * udthandle,char ** windomain,char ** winname,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,boolean_t * is_nt4,int * direction)3508e228215Sdm idmap_udt_get_error_rule(idmap_udt_handle_t *udthandle,
3518e228215Sdm     char **windomain, char **winname,
352cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
353cd37da74Snw     boolean_t *is_nt4, int *direction)
354cd37da74Snw {
3558e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
356cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3578e228215Sdm 
3588e228215Sdm 	if (udthandle->commit_stat == IDMAP_SUCCESS ||
3598e228215Sdm 	    udthandle->error_index < 0)
3608e228215Sdm 		return (IDMAP_ERR_NORESULT);
3618e228215Sdm 
3628e228215Sdm 	return (idmap_namerule2parts(
363cd37da74Snw 	    &udthandle->error_rule,
364cd37da74Snw 	    windomain,
365cd37da74Snw 	    winname,
366cd37da74Snw 	    unixname,
367cd37da74Snw 	    is_user,
368cd37da74Snw 	    is_wuser,
369cd37da74Snw 	    is_nt4,
370cd37da74Snw 	    direction));
3718e228215Sdm }
3728e228215Sdm 
3738e228215Sdm /*
3748e228215Sdm  * Retrieve the rule with which there was a conflict. TODO: retrieve
3758e228215Sdm  * the value.
3768e228215Sdm  *
3778e228215Sdm  * Return value:
3788e228215Sdm  * IDMAP_ERR_NORESULT if there is no error rule.
3798e228215Sdm  * IDMAP_SUCCESS if the rule was obtained OK.
3808e228215Sdm  * other error code (IDMAP_ERR_NOMEMORY etc)
3818e228215Sdm  */
3828e228215Sdm 
3838e228215Sdm idmap_stat
idmap_udt_get_conflict_rule(idmap_udt_handle_t * udthandle,char ** windomain,char ** winname,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,boolean_t * is_nt4,int * direction)3848e228215Sdm idmap_udt_get_conflict_rule(idmap_udt_handle_t *udthandle,
3858e228215Sdm     char **windomain, char **winname,
386cd37da74Snw     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
387cd37da74Snw     boolean_t *is_nt4, int *direction)
388cd37da74Snw {
3898e228215Sdm 	idmap_namerule_parts_clear(windomain, winname,
390cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
3918e228215Sdm 
3928e228215Sdm 	if (udthandle->commit_stat != IDMAP_ERR_W2U_NAMERULE_CONFLICT &&
3938e228215Sdm 	    udthandle->commit_stat != IDMAP_ERR_U2W_NAMERULE_CONFLICT) {
394cd37da74Snw 		return (IDMAP_ERR_NORESULT);
3958e228215Sdm 	}
3968e228215Sdm 
3978e228215Sdm 	return (idmap_namerule2parts(
398cd37da74Snw 	    &udthandle->conflict_rule,
399cd37da74Snw 	    windomain,
400cd37da74Snw 	    winname,
401cd37da74Snw 	    unixname,
402cd37da74Snw 	    is_user,
403cd37da74Snw 	    is_wuser,
404cd37da74Snw 	    is_nt4,
405cd37da74Snw 	    direction));
406c5c4113dSnw }
407c5c4113dSnw 
408c5c4113dSnw 
409c5c4113dSnw /*
410c5c4113dSnw  * Destroy the update handle
411c5c4113dSnw  */
412c5c4113dSnw void
idmap_udt_destroy(idmap_udt_handle_t * udthandle)413cd37da74Snw idmap_udt_destroy(idmap_udt_handle_t *udthandle)
414cd37da74Snw {
415c5c4113dSnw 	if (udthandle == NULL)
416c5c4113dSnw 		return;
417*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_update_batch, (caddr_t)&udthandle->batch);
418*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->error_rule);
419*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->conflict_rule);
420c5c4113dSnw 	free(udthandle);
421c5c4113dSnw }
422c5c4113dSnw 
423c5c4113dSnw 
424c5c4113dSnw idmap_stat
idmap_udt_add_namerule(idmap_udt_handle_t * udthandle,const char * windomain,boolean_t is_user,boolean_t is_wuser,const char * winname,const char * unixname,boolean_t is_nt4,int direction)425c5c4113dSnw idmap_udt_add_namerule(idmap_udt_handle_t *udthandle, const char *windomain,
426cd37da74Snw     boolean_t is_user, boolean_t is_wuser, const char *winname,
427cd37da74Snw     const char *unixname, boolean_t is_nt4, int direction)
428cd37da74Snw {
429c5c4113dSnw 	idmap_retcode	retcode;
430651c0131Sbaban 	idmap_namerule	*rule = NULL;
431c5c4113dSnw 
432651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
433c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
434c5c4113dSnw 		goto errout;
435c5c4113dSnw 
436c5c4113dSnw 	rule = &udthandle->batch.
437cd37da74Snw 	    idmap_update_batch_val[udthandle->next].
438cd37da74Snw 	    idmap_update_op_u.rule;
439c5c4113dSnw 	rule->is_user = is_user;
440cd37da74Snw 	rule->is_wuser = is_wuser;
441c5c4113dSnw 	rule->direction = direction;
442c5c4113dSnw 	rule->is_nt4 = is_nt4;
4438e228215Sdm 
4448e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
4458e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4468e228215Sdm 		goto errout;
4478e228215Sdm 
4488e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
4498e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4508e228215Sdm 		goto errout;
4518e228215Sdm 
4528e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
4538e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4548e228215Sdm 		goto errout;
455651c0131Sbaban 
456651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
457651c0131Sbaban 	    OP_ADD_NAMERULE;
458c5c4113dSnw 	udthandle->next++;
459c5c4113dSnw 	return (IDMAP_SUCCESS);
460c5c4113dSnw 
461c5c4113dSnw errout:
462651c0131Sbaban 	/* The batch should still be usable */
463651c0131Sbaban 	if (rule)
464*a17ce845SMarcel Telka 		xdr_free(xdr_idmap_namerule, (caddr_t)rule);
465c5c4113dSnw 	errno = idmap_stat2errno(retcode);
466c5c4113dSnw 	return (retcode);
467c5c4113dSnw }
468c5c4113dSnw 
469c5c4113dSnw 
470c5c4113dSnw /* ARGSUSED */
471c5c4113dSnw idmap_stat
idmap_udt_rm_namerule(idmap_udt_handle_t * udthandle,boolean_t is_user,boolean_t is_wuser,const char * windomain,const char * winname,const char * unixname,int direction)472c5c4113dSnw idmap_udt_rm_namerule(idmap_udt_handle_t *udthandle, boolean_t is_user,
473cd37da74Snw     boolean_t is_wuser,	const char *windomain, const char *winname,
474cd37da74Snw     const char *unixname, int direction)
475cd37da74Snw {
476c5c4113dSnw 	idmap_retcode	retcode;
477651c0131Sbaban 	idmap_namerule	*rule = NULL;
478c5c4113dSnw 
479651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
480c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
481c5c4113dSnw 		goto errout;
482c5c4113dSnw 
483c5c4113dSnw 	rule = &udthandle->batch.
484cd37da74Snw 	    idmap_update_batch_val[udthandle->next].
485cd37da74Snw 	    idmap_update_op_u.rule;
486c5c4113dSnw 	rule->is_user = is_user;
487cd37da74Snw 	rule->is_wuser = is_wuser;
488c5c4113dSnw 	rule->direction = direction;
4898e228215Sdm 
4908e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
4918e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4928e228215Sdm 		goto errout;
4938e228215Sdm 
4948e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
4958e228215Sdm 	if (retcode != IDMAP_SUCCESS)
4968e228215Sdm 		goto errout;
4978e228215Sdm 
4988e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
4998e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5008e228215Sdm 		goto errout;
5018e228215Sdm 
502651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
503651c0131Sbaban 	    OP_RM_NAMERULE;
504c5c4113dSnw 	udthandle->next++;
505c5c4113dSnw 	return (IDMAP_SUCCESS);
506c5c4113dSnw 
507c5c4113dSnw errout:
508651c0131Sbaban 	if (rule)
509*a17ce845SMarcel Telka 		xdr_free(xdr_idmap_namerule, (caddr_t)rule);
510c5c4113dSnw 	errno = idmap_stat2errno(retcode);
511c5c4113dSnw 	return (retcode);
512c5c4113dSnw }
513c5c4113dSnw 
514c5c4113dSnw 
515c5c4113dSnw /* ARGSUSED */
516c5c4113dSnw idmap_stat
idmap_udt_flush_namerules(idmap_udt_handle_t * udthandle)517cd37da74Snw idmap_udt_flush_namerules(idmap_udt_handle_t *udthandle)
518cd37da74Snw {
519c5c4113dSnw 	idmap_retcode	retcode;
520c5c4113dSnw 
521651c0131Sbaban 	retcode = _udt_extend_batch(udthandle);
522c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
523c5c4113dSnw 		goto errout;
524c5c4113dSnw 
525651c0131Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
526651c0131Sbaban 	    OP_FLUSH_NAMERULES;
527c5c4113dSnw 	udthandle->next++;
528c5c4113dSnw 	return (IDMAP_SUCCESS);
529c5c4113dSnw 
530c5c4113dSnw errout:
531c5c4113dSnw 	errno = idmap_stat2errno(retcode);
532c5c4113dSnw 	return (retcode);
533c5c4113dSnw }
534c5c4113dSnw 
535c5c4113dSnw 
536c5c4113dSnw /*
537c5c4113dSnw  * Set the number of entries requested per batch by the iterator
538c5c4113dSnw  *
539c5c4113dSnw  * Input:
540c5c4113dSnw  * iter  - iterator
541c5c4113dSnw  * limit - number of entries requested per batch
542c5c4113dSnw  */
543c5c4113dSnw idmap_stat
idmap_iter_set_limit(idmap_iter_t * iter,uint64_t limit)544cd37da74Snw idmap_iter_set_limit(idmap_iter_t *iter, uint64_t limit)
545cd37da74Snw {
546c5c4113dSnw 	if (iter == NULL) {
547c5c4113dSnw 		errno = EINVAL;
548c5c4113dSnw 		return (IDMAP_ERR_ARG);
549c5c4113dSnw 	}
550c5c4113dSnw 	iter->limit = limit;
551c5c4113dSnw 	return (IDMAP_SUCCESS);
552c5c4113dSnw }
553c5c4113dSnw 
554c5c4113dSnw 
555c5c4113dSnw /*
556c5c4113dSnw  * Create iterator to get name-based mapping rules
557c5c4113dSnw  *
558c5c4113dSnw  * Input:
559c5c4113dSnw  * windomain - Windows domain
560c5c4113dSnw  * is_user   - user or group rules
561c5c4113dSnw  * winname   - Windows user or group name
562c5c4113dSnw  * unixname  - Unix user or group name
563c5c4113dSnw  *
564c5c4113dSnw  * Output:
565c5c4113dSnw  * iter - iterator
566c5c4113dSnw  */
567c5c4113dSnw idmap_stat
idmap_iter_namerules(const char * windomain,boolean_t is_user,boolean_t is_wuser,const char * winname,const char * unixname,idmap_iter_t ** iter)568*a17ce845SMarcel Telka idmap_iter_namerules(const char *windomain, boolean_t is_user,
569*a17ce845SMarcel Telka     boolean_t is_wuser, const char *winname, const char *unixname,
570*a17ce845SMarcel Telka     idmap_iter_t **iter)
571cd37da74Snw {
572c5c4113dSnw 
573c5c4113dSnw 	idmap_iter_t			*tmpiter;
574c5c4113dSnw 	idmap_list_namerules_1_argument	*arg = NULL;
575c5c4113dSnw 	idmap_namerule			*rule;
576c5c4113dSnw 	idmap_retcode			retcode;
577c5c4113dSnw 
5781fdeec65Sjoyce mcintosh 	__ITER_CREATE(tmpiter, arg, IDMAP_LIST_NAMERULES);
579c5c4113dSnw 
580c5c4113dSnw 	rule = &arg->rule;
581c5c4113dSnw 	rule->is_user = is_user;
582cd37da74Snw 	rule->is_wuser = is_wuser;
583651c0131Sbaban 	rule->direction = IDMAP_DIRECTION_UNDEF;
5848e228215Sdm 
5858e228215Sdm 	retcode = idmap_strdupnull(&rule->windomain, windomain);
5868e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5878e228215Sdm 		goto errout;
5888e228215Sdm 
5898e228215Sdm 	retcode = idmap_strdupnull(&rule->winname, winname);
5908e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5918e228215Sdm 		goto errout;
5928e228215Sdm 
5938e228215Sdm 	retcode = idmap_strdupnull(&rule->unixname, unixname);
5948e228215Sdm 	if (retcode != IDMAP_SUCCESS)
5958e228215Sdm 		goto errout;
596c5c4113dSnw 
597c5c4113dSnw 	*iter = tmpiter;
598c5c4113dSnw 	return (IDMAP_SUCCESS);
599c5c4113dSnw 
600c5c4113dSnw errout:
601ad8ef92aSMilan Jurik 	if (arg) {
602ad8ef92aSMilan Jurik 		xdr_free(xdr_idmap_list_namerules_1_argument, (char *)arg);
603ad8ef92aSMilan Jurik 		free(arg);
604ad8ef92aSMilan Jurik 	}
605ad8ef92aSMilan Jurik 	if (tmpiter)
606ad8ef92aSMilan Jurik 		free(tmpiter);
607ad8ef92aSMilan Jurik 
608ad8ef92aSMilan Jurik 	return (retcode);
609c5c4113dSnw }
610c5c4113dSnw 
611c5c4113dSnw 
612c5c4113dSnw /*
613c5c4113dSnw  * Iterate through the name-based mapping rules
614c5c4113dSnw  *
615c5c4113dSnw  * Input:
616c5c4113dSnw  * iter - iterator
617c5c4113dSnw  *
618c5c4113dSnw  * Output:
619c5c4113dSnw  * windomain - Windows domain
620c5c4113dSnw  * winname   - Windows user or group name
621c5c4113dSnw  * unixname  - Unix user or group name
622c5c4113dSnw  * is_nt4    - NT4 or AD
623c5c4113dSnw  * direction - bi(0), win2unix(1), unix2win(2)
624c5c4113dSnw  *
625c5c4113dSnw  * Return value:
626c5c4113dSnw  * 0   - done
627c5c4113dSnw  * 1   - more results available
628c5c4113dSnw  * < 0 - error
629c5c4113dSnw  */
630c5c4113dSnw idmap_stat
idmap_iter_next_namerule(idmap_iter_t * iter,char ** windomain,char ** winname,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,boolean_t * is_nt4,int * direction)631c5c4113dSnw idmap_iter_next_namerule(idmap_iter_t *iter, char **windomain,
632cd37da74Snw     char **winname, char **unixname,  boolean_t *is_user,
633cd37da74Snw     boolean_t *is_wuser, boolean_t *is_nt4, int *direction)
634cd37da74Snw {
635c5c4113dSnw 	idmap_namerules_res		*namerules;
636c5c4113dSnw 	idmap_list_namerules_1_argument	*arg;
637c5c4113dSnw 	idmap_retcode			retcode;
638c5c4113dSnw 
639cd37da74Snw 	idmap_namerule_parts_clear(windomain, winname,
640cd37da74Snw 	    unixname, is_user, is_wuser, is_nt4, direction);
641cd37da74Snw 
642c5c4113dSnw 
643c5c4113dSnw 	__ITER_CHECK(iter, IDMAP_LIST_NAMERULES);
644c5c4113dSnw 
645c5c4113dSnw 	namerules = (idmap_namerules_res *)iter->retlist;
646c5c4113dSnw 	if (iter->retcode == IDMAP_NEXT && (namerules == NULL ||
647cd37da74Snw 	    iter->next >= namerules->rules.rules_len)) {
648c5c4113dSnw 
649c5c4113dSnw 		if ((arg = iter->arg) == NULL) {
650c5c4113dSnw 			errno = EINVAL;
651c5c4113dSnw 			return (IDMAP_ERR_ARG);
652c5c4113dSnw 		}
653c5c4113dSnw 		arg->limit = iter->limit;
654c5c4113dSnw 
655c5c4113dSnw 		retcode = _iter_get_next_list(IDMAP_LIST_NAMERULES,
656cd37da74Snw 		    iter, arg,
657cd37da74Snw 		    (uchar_t **)&namerules, sizeof (*namerules),
658cd37da74Snw 		    (xdrproc_t)xdr_idmap_list_namerules_1_argument,
659cd37da74Snw 		    (xdrproc_t)xdr_idmap_namerules_res);
660c5c4113dSnw 		if (retcode != IDMAP_SUCCESS)
661c5c4113dSnw 			return (retcode);
662c5c4113dSnw 
663c5c4113dSnw 		if (IDMAP_ERROR(namerules->retcode)) {
664c5c4113dSnw 			retcode  = namerules->retcode;
665c5c4113dSnw 			xdr_free(xdr_idmap_namerules_res, (caddr_t)namerules);
666c5c4113dSnw 			free(namerules);
667c5c4113dSnw 			iter->retlist = NULL;
668c5c4113dSnw 			return (retcode);
669c5c4113dSnw 		}
670c5c4113dSnw 		iter->retcode = namerules->retcode;
671c5c4113dSnw 		arg->lastrowid = namerules->lastrowid;
672c5c4113dSnw 	}
673c5c4113dSnw 
674c5c4113dSnw 	if (namerules == NULL || namerules->rules.rules_len == 0)
675c5c4113dSnw 		return (IDMAP_SUCCESS);
676c5c4113dSnw 
677c5c4113dSnw 	if (iter->next >= namerules->rules.rules_len) {
678c5c4113dSnw 		return (IDMAP_ERR_ARG);
679c5c4113dSnw 	}
680c5c4113dSnw 
6818e228215Sdm 	retcode = idmap_strdupnull(windomain,
6828e228215Sdm 	    namerules->rules.rules_val[iter->next].windomain);
6838e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6848e228215Sdm 		goto errout;
6858e228215Sdm 
6868e228215Sdm 	retcode = idmap_strdupnull(winname,
6878e228215Sdm 	    namerules->rules.rules_val[iter->next].winname);
6888e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6898e228215Sdm 		goto errout;
6908e228215Sdm 
6918e228215Sdm 	retcode = idmap_strdupnull(unixname,
6928e228215Sdm 	    namerules->rules.rules_val[iter->next].unixname);
6938e228215Sdm 	if (retcode != IDMAP_SUCCESS)
6948e228215Sdm 		goto errout;
6958e228215Sdm 
696c5c4113dSnw 	if (is_nt4)
697c5c4113dSnw 		*is_nt4 = namerules->rules.rules_val[iter->next].is_nt4;
698cd37da74Snw 	if (is_user)
699cd37da74Snw 		*is_user = namerules->rules.rules_val[iter->next].is_user;
700cd37da74Snw 	if (is_wuser)
701cd37da74Snw 		*is_wuser = namerules->rules.rules_val[iter->next].is_wuser;
702c5c4113dSnw 	if (direction)
703c5c4113dSnw 		*direction = namerules->rules.rules_val[iter->next].direction;
704c5c4113dSnw 	iter->next++;
705c5c4113dSnw 
706c5c4113dSnw 	if (iter->next == namerules->rules.rules_len)
707c5c4113dSnw 		return (iter->retcode);
708c5c4113dSnw 	else
709c5c4113dSnw 		return (IDMAP_NEXT);
710c5c4113dSnw 
711c5c4113dSnw errout:
712c5c4113dSnw 	if (windomain && *windomain)
713c5c4113dSnw 		free(*windomain);
714c5c4113dSnw 	if (winname && *winname)
715c5c4113dSnw 		free(*winname);
716c5c4113dSnw 	if (unixname && *unixname)
717c5c4113dSnw 		free(*unixname);
718c5c4113dSnw 	return (retcode);
719c5c4113dSnw }
720c5c4113dSnw 
721c5c4113dSnw 
722c5c4113dSnw /*
723c5c4113dSnw  * Create iterator to get SID to UID/GID mappings
724c5c4113dSnw  *
725c5c4113dSnw  * Output:
726c5c4113dSnw  * iter - iterator
727c5c4113dSnw  */
728c5c4113dSnw idmap_stat
idmap_iter_mappings(idmap_iter_t ** iter,int flag)7291fdeec65Sjoyce mcintosh idmap_iter_mappings(idmap_iter_t **iter, int flag)
730cd37da74Snw {
731c5c4113dSnw 	idmap_iter_t			*tmpiter;
732c5c4113dSnw 	idmap_list_mappings_1_argument	*arg = NULL;
733c5c4113dSnw 
7341fdeec65Sjoyce mcintosh 	__ITER_CREATE(tmpiter, arg, IDMAP_LIST_MAPPINGS);
735c5c4113dSnw 
73648258c6bSjp 	arg->flag = flag;
737c5c4113dSnw 	*iter = tmpiter;
738c5c4113dSnw 	return (IDMAP_SUCCESS);
739c5c4113dSnw }
740c5c4113dSnw 
741c5c4113dSnw 
742c5c4113dSnw /*
743c5c4113dSnw  * Iterate through the SID to UID/GID mappings
744c5c4113dSnw  *
745c5c4113dSnw  * Input:
746c5c4113dSnw  * iter - iterator
747c5c4113dSnw  *
748c5c4113dSnw  * Output:
749c5c4113dSnw  * sid - SID in canonical form
750c5c4113dSnw  * pid - UID or GID
751c5c4113dSnw  *
752c5c4113dSnw  * Return value:
753c5c4113dSnw  * 0   - done
754c5c4113dSnw  * 1   - more results available
755c5c4113dSnw  * < 0 - error
756c5c4113dSnw  */
757c5c4113dSnw idmap_stat
idmap_iter_next_mapping(idmap_iter_t * iter,char ** sidprefix,idmap_rid_t * rid,uid_t * pid,char ** winname,char ** windomain,char ** unixname,boolean_t * is_user,boolean_t * is_wuser,int * direction,idmap_info * info)758c5c4113dSnw idmap_iter_next_mapping(idmap_iter_t *iter, char **sidprefix,
759cd37da74Snw     idmap_rid_t *rid, uid_t *pid, char **winname,
760cd37da74Snw     char **windomain, char **unixname, boolean_t *is_user,
76148258c6bSjp     boolean_t *is_wuser, int *direction, idmap_info *info)
762cd37da74Snw {
763c5c4113dSnw 	idmap_mappings_res		*mappings;
764c5c4113dSnw 	idmap_list_mappings_1_argument	*arg;
765c5c4113dSnw 	idmap_retcode			retcode;
766c5c4113dSnw 	char				*str;
767c5c4113dSnw 
768c5c4113dSnw 	if (sidprefix)
769c5c4113dSnw 		*sidprefix = NULL;
770c5c4113dSnw 	if (rid)
771c5c4113dSnw 		*rid = UINT32_MAX;
772c5c4113dSnw 	if (winname)
773c5c4113dSnw 		*winname = NULL;
774c5c4113dSnw 	if (windomain)
775c5c4113dSnw 		*windomain = NULL;
776c5c4113dSnw 	if (unixname)
777c5c4113dSnw 		*unixname = NULL;
778c5c4113dSnw 	if (pid)
779c5c4113dSnw 		*pid = UINT32_MAX;
780cd37da74Snw 	if (is_user)
781cd37da74Snw 		*is_user = -1;
782cd37da74Snw 	if (is_wuser)
783cd37da74Snw 		*is_wuser = -1;
784c5c4113dSnw 	if (direction)
785651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
786c5c4113dSnw 
787c5c4113dSnw 	__ITER_CHECK(iter, IDMAP_LIST_MAPPINGS);
788c5c4113dSnw 
789c5c4113dSnw 	mappings = (idmap_mappings_res *)iter->retlist;
790c5c4113dSnw 	if (iter->retcode == IDMAP_NEXT && (mappings == NULL ||
791cd37da74Snw 	    iter->next >= mappings->mappings.mappings_len)) {
792c5c4113dSnw 
793c5c4113dSnw 		if ((arg = iter->arg) == NULL) {
794c5c4113dSnw 			errno = EINVAL;
795c5c4113dSnw 			return (IDMAP_ERR_ARG);
796c5c4113dSnw 		}
797c5c4113dSnw 		arg->limit = iter->limit;
798c5c4113dSnw 
799c5c4113dSnw 		retcode = _iter_get_next_list(IDMAP_LIST_MAPPINGS,
800cd37da74Snw 		    iter, arg,
801cd37da74Snw 		    (uchar_t **)&mappings, sizeof (*mappings),
802cd37da74Snw 		    (xdrproc_t)xdr_idmap_list_mappings_1_argument,
803cd37da74Snw 		    (xdrproc_t)xdr_idmap_mappings_res);
804c5c4113dSnw 		if (retcode != IDMAP_SUCCESS)
805c5c4113dSnw 			return (retcode);
806c5c4113dSnw 
807c5c4113dSnw 		if (IDMAP_ERROR(mappings->retcode)) {
808c5c4113dSnw 			retcode  = mappings->retcode;
809c5c4113dSnw 			xdr_free(xdr_idmap_mappings_res, (caddr_t)mappings);
810c5c4113dSnw 			free(mappings);
811c5c4113dSnw 			iter->retlist = NULL;
812c5c4113dSnw 			return (retcode);
813c5c4113dSnw 		}
814c5c4113dSnw 		iter->retcode = mappings->retcode;
815c5c4113dSnw 		arg->lastrowid = mappings->lastrowid;
816c5c4113dSnw 	}
817c5c4113dSnw 
818c5c4113dSnw 	if (mappings == NULL || mappings->mappings.mappings_len == 0)
819c5c4113dSnw 		return (IDMAP_SUCCESS);
820c5c4113dSnw 
821c5c4113dSnw 	if (iter->next >= mappings->mappings.mappings_len) {
822c5c4113dSnw 		return (IDMAP_ERR_ARG);
823c5c4113dSnw 	}
824c5c4113dSnw 
825c5c4113dSnw 	if (sidprefix) {
826c5c4113dSnw 		str = mappings->mappings.mappings_val[iter->next].id1.
827cd37da74Snw 		    idmap_id_u.sid.prefix;
8288edda628Sbaban 		if (str && *str != '\0') {
829c5c4113dSnw 			*sidprefix = strdup(str);
8309581d9f4Sbaban 			if (*sidprefix == NULL) {
8319581d9f4Sbaban 				retcode = IDMAP_ERR_MEMORY;
8329581d9f4Sbaban 				goto errout;
8339581d9f4Sbaban 			}
834c5c4113dSnw 		}
835c5c4113dSnw 	}
836c5c4113dSnw 	if (rid)
837c5c4113dSnw 		*rid = mappings->mappings.mappings_val[iter->next].id1.
838cd37da74Snw 		    idmap_id_u.sid.rid;
8398e228215Sdm 
8408e228215Sdm 	retcode = idmap_strdupnull(windomain,
8418e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id1domain);
8428e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8438e228215Sdm 		goto errout;
8448e228215Sdm 
8458e228215Sdm 	retcode = idmap_strdupnull(winname,
8468e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id1name);
8478e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8488e228215Sdm 		goto errout;
8498e228215Sdm 
8508e228215Sdm 	retcode = idmap_strdupnull(unixname,
8518e228215Sdm 	    mappings->mappings.mappings_val[iter->next].id2name);
8528e228215Sdm 	if (retcode != IDMAP_SUCCESS)
8538e228215Sdm 		goto errout;
8548e228215Sdm 
8558e228215Sdm 
856c5c4113dSnw 	if (pid)
857c5c4113dSnw 		*pid = mappings->mappings.mappings_val[iter->next].id2.
858cd37da74Snw 		    idmap_id_u.uid;
859c5c4113dSnw 	if (direction)
860c5c4113dSnw 		*direction = mappings->mappings.mappings_val[iter->next].
861cd37da74Snw 		    direction;
862cd37da74Snw 	if (is_user)
863cd37da74Snw 		*is_user = (mappings->mappings.mappings_val[iter->next].id2
864cd37da74Snw 		    .idtype == IDMAP_UID)?1:0;
865cd37da74Snw 	if (is_wuser)
866cd37da74Snw 		*is_wuser = (mappings->mappings.mappings_val[iter->next].id1
867cd37da74Snw 		    .idtype == IDMAP_USID)?1:0;
868cd37da74Snw 
86948258c6bSjp 	if (info) {
870148c5f43SAlan Wright 		idmap_info_mov(info,
87148258c6bSjp 		    &mappings->mappings.mappings_val[iter->next].info);
87248258c6bSjp 	}
873c5c4113dSnw 	iter->next++;
874c5c4113dSnw 
875c5c4113dSnw 	if (iter->next == mappings->mappings.mappings_len)
876c5c4113dSnw 		return (iter->retcode);
877c5c4113dSnw 	else
878c5c4113dSnw 		return (IDMAP_NEXT);
879c5c4113dSnw 
880c5c4113dSnw errout:
881c5c4113dSnw 	if (sidprefix && *sidprefix)
882c5c4113dSnw 		free(*sidprefix);
883c5c4113dSnw 	if (winname && *winname)
884c5c4113dSnw 		free(*winname);
885c5c4113dSnw 	if (windomain && *windomain)
886c5c4113dSnw 		free(*windomain);
887c5c4113dSnw 	if (unixname && *unixname)
888c5c4113dSnw 		free(*unixname);
889c5c4113dSnw 	return (retcode);
890c5c4113dSnw }
891c5c4113dSnw 
892c5c4113dSnw 
893c5c4113dSnw /*
894c5c4113dSnw  * Destroy the iterator
895c5c4113dSnw  */
896c5c4113dSnw void
idmap_iter_destroy(idmap_iter_t * iter)897cd37da74Snw idmap_iter_destroy(idmap_iter_t *iter)
898cd37da74Snw {
899c5c4113dSnw 	xdrproc_t _xdr_argument, _xdr_result;
900c5c4113dSnw 
901c5c4113dSnw 	if (iter == NULL)
902c5c4113dSnw 		return;
903c5c4113dSnw 
904c5c4113dSnw 	switch (iter->type) {
905c5c4113dSnw 	case IDMAP_LIST_NAMERULES:
906c5c4113dSnw 		_xdr_argument = (xdrproc_t)xdr_idmap_list_namerules_1_argument;
907c5c4113dSnw 		_xdr_result = (xdrproc_t)xdr_idmap_namerules_res;
908c5c4113dSnw 		break;
909c5c4113dSnw 	case IDMAP_LIST_MAPPINGS:
910c5c4113dSnw 		_xdr_argument = (xdrproc_t)xdr_idmap_list_mappings_1_argument;
911c5c4113dSnw 		_xdr_result = (xdrproc_t)xdr_idmap_mappings_res;
912c5c4113dSnw 		break;
913c5c4113dSnw 	default:
914c5c4113dSnw 		free(iter);
915c5c4113dSnw 		return;
916c5c4113dSnw 	};
917c5c4113dSnw 
918c5c4113dSnw 	if (iter->arg) {
919c5c4113dSnw 		xdr_free(_xdr_argument, (caddr_t)iter->arg);
920c5c4113dSnw 		free(iter->arg);
921c5c4113dSnw 	}
922c5c4113dSnw 	if (iter->retlist) {
923c5c4113dSnw 		xdr_free(_xdr_result, (caddr_t)iter->retlist);
924c5c4113dSnw 		free(iter->retlist);
925c5c4113dSnw 	}
926c5c4113dSnw 	free(iter);
927c5c4113dSnw }
928c5c4113dSnw 
929c5c4113dSnw 
930c5c4113dSnw /*
931c5c4113dSnw  * Create handle to get SID to UID/GID mapping entries
932c5c4113dSnw  *
933c5c4113dSnw  * Input:
934c5c4113dSnw  * gh - "get mapping" handle
935c5c4113dSnw  */
936c5c4113dSnw idmap_stat
idmap_get_create(idmap_get_handle_t ** gh)9371fdeec65Sjoyce mcintosh idmap_get_create(idmap_get_handle_t **gh)
938cd37da74Snw {
939c5c4113dSnw 	idmap_get_handle_t	*tmp;
940c5c4113dSnw 
941c5c4113dSnw 	/* allocate the handle */
942c5c4113dSnw 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
943c5c4113dSnw 		errno = ENOMEM;
944c5c4113dSnw 		return (IDMAP_ERR_MEMORY);
945c5c4113dSnw 	}
946c5c4113dSnw 
947c5c4113dSnw 	*gh = tmp;
948c5c4113dSnw 	return (IDMAP_SUCCESS);
949c5c4113dSnw }
950c5c4113dSnw 
951c5c4113dSnw 
952c5c4113dSnw /*
953c5c4113dSnw  * Given SID, get UID
954c5c4113dSnw  *
955c5c4113dSnw  * Input:
956c5c4113dSnw  * sidprefix  - SID prefix
957c5c4113dSnw  * rid        - RID
958c5c4113dSnw  * flag       - flag
959c5c4113dSnw  *
960c5c4113dSnw  * Output:
961c5c4113dSnw  * stat - status of the get request
962c5c4113dSnw  * uid  - POSIX UID if stat = 0
963c5c4113dSnw  *
964c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
965c5c4113dSnw  */
966c5c4113dSnw idmap_stat
idmap_get_uidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,uid_t * uid,idmap_stat * stat)967c5c4113dSnw idmap_get_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
968*a17ce845SMarcel Telka     int flag, uid_t *uid, idmap_stat *stat)
969cd37da74Snw {
97048258c6bSjp 	return (idmap_getext_uidbysid(gh, sidprefix, rid, flag, uid,
97148258c6bSjp 	    NULL, stat));
97248258c6bSjp }
97348258c6bSjp 
97448258c6bSjp /*
97548258c6bSjp  * Given SID, get UID
97648258c6bSjp  *
97748258c6bSjp  * Input:
97848258c6bSjp  * sidprefix  - SID prefix
97948258c6bSjp  * rid        - RID
98048258c6bSjp  * flag       - flag
98148258c6bSjp  *
98248258c6bSjp  * Output:
98348258c6bSjp  * stat - status of the get request
98448258c6bSjp  * uid  - POSIX UID if stat = 0
98548258c6bSjp  * how  - mapping type if stat = 0
98648258c6bSjp  *
98748258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
98848258c6bSjp  */
989c5c4113dSnw 
99048258c6bSjp idmap_stat
idmap_getext_uidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,uid_t * uid,idmap_info * info,idmap_stat * stat)99148258c6bSjp idmap_getext_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
992*a17ce845SMarcel Telka     int flag, uid_t *uid, idmap_info *info, idmap_stat *stat)
99348258c6bSjp {
994c5c4113dSnw 	idmap_retcode	retcode;
995651c0131Sbaban 	idmap_mapping	*mapping = NULL;
996c5c4113dSnw 
997c5c4113dSnw 	/* sanity checks */
998c5c4113dSnw 	if (gh == NULL)
999c5c4113dSnw 		return (IDMAP_ERR_ARG);
1000c5c4113dSnw 	if (uid == NULL || sidprefix == NULL)
1001c5c4113dSnw 		return (IDMAP_ERR_ARG);
1002c5c4113dSnw 
10033ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
10043ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
10053ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_uidbysid(sidprefix, rid, uid);
10063ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
10073ee87bcaSJulian Pullen 			*stat = retcode;
10083ee87bcaSJulian Pullen 			return (retcode);
10093ee87bcaSJulian Pullen 		}
10103ee87bcaSJulian Pullen 	}
10113ee87bcaSJulian Pullen 
1012c5c4113dSnw 	/* Extend the request array and the return list */
1013c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1014c5c4113dSnw 		goto errout;
1015c5c4113dSnw 
1016c5c4113dSnw 	/* Setup the request */
1017c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1018c5c4113dSnw 	mapping->flag = flag;
1019c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1020c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1021c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1022c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1023c5c4113dSnw 		goto errout;
1024c5c4113dSnw 	}
1025c5c4113dSnw 	mapping->id2.idtype = IDMAP_UID;
1026c5c4113dSnw 
1027c5c4113dSnw 	/* Setup pointers for the result */
1028c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_UID;
1029c5c4113dSnw 	gh->retlist[gh->next].uid = uid;
1030c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
103148258c6bSjp 	gh->retlist[gh->next].info = info;
10323ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1033c5c4113dSnw 
1034c5c4113dSnw 	gh->next++;
1035c5c4113dSnw 	return (IDMAP_SUCCESS);
1036c5c4113dSnw 
1037c5c4113dSnw errout:
1038651c0131Sbaban 	/* Batch created so far should still be usable */
1039651c0131Sbaban 	if (mapping)
1040651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1041c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1042c5c4113dSnw 	return (retcode);
1043c5c4113dSnw }
1044c5c4113dSnw 
1045c5c4113dSnw 
1046c5c4113dSnw /*
1047c5c4113dSnw  * Given SID, get GID
1048c5c4113dSnw  *
1049c5c4113dSnw  * Input:
1050c5c4113dSnw  * sidprefix  - SID prefix
1051c5c4113dSnw  * rid        - rid
1052c5c4113dSnw  * flag       - flag
1053c5c4113dSnw  *
1054c5c4113dSnw  * Output:
1055c5c4113dSnw  * stat - status of the get request
1056c5c4113dSnw  * gid  - POSIX GID if stat = 0
1057c5c4113dSnw  *
1058c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1059c5c4113dSnw  */
1060c5c4113dSnw idmap_stat
idmap_get_gidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,gid_t * gid,idmap_stat * stat)1061c5c4113dSnw idmap_get_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1062*a17ce845SMarcel Telka     int flag, gid_t *gid, idmap_stat *stat)
1063cd37da74Snw {
106448258c6bSjp 	return (idmap_getext_gidbysid(gh, sidprefix, rid, flag, gid,
106548258c6bSjp 	    NULL, stat));
106648258c6bSjp }
106748258c6bSjp 
106848258c6bSjp 
106948258c6bSjp /*
107048258c6bSjp  * Given SID, get GID
107148258c6bSjp  *
107248258c6bSjp  * Input:
107348258c6bSjp  * sidprefix  - SID prefix
107448258c6bSjp  * rid        - rid
107548258c6bSjp  * flag       - flag
107648258c6bSjp  *
107748258c6bSjp  * Output:
107848258c6bSjp  * stat - status of the get request
107948258c6bSjp  * gid  - POSIX GID if stat = 0
108048258c6bSjp  * how  - mapping type if stat = 0
108148258c6bSjp  *
108248258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
108348258c6bSjp  */
108448258c6bSjp idmap_stat
idmap_getext_gidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,gid_t * gid,idmap_info * info,idmap_stat * stat)108548258c6bSjp idmap_getext_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1086*a17ce845SMarcel Telka     int flag, gid_t *gid, idmap_info *info, idmap_stat *stat)
108748258c6bSjp {
1088c5c4113dSnw 
1089c5c4113dSnw 	idmap_retcode	retcode;
1090651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1091c5c4113dSnw 
1092c5c4113dSnw 	/* sanity checks */
1093c5c4113dSnw 	if (gh == NULL)
1094c5c4113dSnw 		return (IDMAP_ERR_ARG);
1095c5c4113dSnw 	if (gid == NULL || sidprefix == NULL)
1096c5c4113dSnw 		return (IDMAP_ERR_ARG);
1097c5c4113dSnw 
10983ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
10993ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
11003ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_gidbysid(sidprefix, rid, gid);
11013ee87bcaSJulian Pullen 		if (retcode == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
11023ee87bcaSJulian Pullen 			*stat = retcode;
11033ee87bcaSJulian Pullen 			return (retcode);
11043ee87bcaSJulian Pullen 		}
11053ee87bcaSJulian Pullen 	}
11063ee87bcaSJulian Pullen 
1107c5c4113dSnw 	/* Extend the request array and the return list */
1108c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1109c5c4113dSnw 		goto errout;
1110c5c4113dSnw 
1111c5c4113dSnw 	/* Setup the request */
1112c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1113c5c4113dSnw 	mapping->flag = flag;
1114c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1115c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1116c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1117c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1118c5c4113dSnw 		goto errout;
1119c5c4113dSnw 	}
1120c5c4113dSnw 	mapping->id2.idtype = IDMAP_GID;
1121c5c4113dSnw 
1122c5c4113dSnw 	/* Setup pointers for the result */
1123c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_GID;
1124c5c4113dSnw 	gh->retlist[gh->next].gid = gid;
1125c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
112648258c6bSjp 	gh->retlist[gh->next].info = info;
11273ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1128c5c4113dSnw 
1129c5c4113dSnw 	gh->next++;
1130c5c4113dSnw 	return (IDMAP_SUCCESS);
1131c5c4113dSnw 
1132c5c4113dSnw errout:
1133651c0131Sbaban 	if (mapping)
1134651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1135c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1136c5c4113dSnw 	return (retcode);
1137c5c4113dSnw }
1138c5c4113dSnw 
1139c5c4113dSnw 
114048258c6bSjp 
1141c5c4113dSnw /*
1142c5c4113dSnw  * Given SID, get POSIX ID i.e. UID/GID
1143c5c4113dSnw  *
1144c5c4113dSnw  * Input:
1145c5c4113dSnw  * sidprefix  - SID prefix
1146c5c4113dSnw  * rid        - rid
1147c5c4113dSnw  * flag       - flag
1148c5c4113dSnw  *
1149c5c4113dSnw  * Output:
1150c5c4113dSnw  * stat    - status of the get request
1151c5c4113dSnw  * is_user - user or group
1152c5c4113dSnw  * pid     - POSIX UID if stat = 0 and is_user = 1
1153c5c4113dSnw  *           POSIX GID if stat = 0 and is_user = 0
1154c5c4113dSnw  *
1155c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1156c5c4113dSnw  */
1157c5c4113dSnw idmap_stat
idmap_get_pidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,uid_t * pid,int * is_user,idmap_stat * stat)1158c5c4113dSnw idmap_get_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1159*a17ce845SMarcel Telka     int flag, uid_t *pid, int *is_user, idmap_stat *stat)
116048258c6bSjp {
116148258c6bSjp 	return (idmap_getext_pidbysid(gh, sidprefix, rid, flag, pid, is_user,
116248258c6bSjp 	    NULL, stat));
116348258c6bSjp }
116448258c6bSjp 
116548258c6bSjp 
116648258c6bSjp 
116748258c6bSjp /*
116848258c6bSjp  * Given SID, get POSIX ID i.e. UID/GID
116948258c6bSjp  *
117048258c6bSjp  * Input:
117148258c6bSjp  * sidprefix  - SID prefix
117248258c6bSjp  * rid        - rid
117348258c6bSjp  * flag       - flag
117448258c6bSjp  *
117548258c6bSjp  * Output:
117648258c6bSjp  * stat    - status of the get request
117748258c6bSjp  * is_user - user or group
117848258c6bSjp  * pid     - POSIX UID if stat = 0 and is_user = 1
117948258c6bSjp  *           POSIX GID if stat = 0 and is_user = 0
118048258c6bSjp  * how     - mapping type if stat = 0
118148258c6bSjp  *
118248258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
118348258c6bSjp  */
118448258c6bSjp idmap_stat
idmap_getext_pidbysid(idmap_get_handle_t * gh,char * sidprefix,idmap_rid_t rid,int flag,uid_t * pid,int * is_user,idmap_info * info,idmap_stat * stat)118548258c6bSjp idmap_getext_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
1186*a17ce845SMarcel Telka     int flag, uid_t *pid, int *is_user, idmap_info *info, idmap_stat *stat)
1187cd37da74Snw {
1188c5c4113dSnw 	idmap_retcode	retcode;
1189651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1190c5c4113dSnw 
1191c5c4113dSnw 	/* sanity checks */
1192c5c4113dSnw 	if (gh == NULL)
1193c5c4113dSnw 		return (IDMAP_ERR_ARG);
1194c5c4113dSnw 	if (pid == NULL || sidprefix == NULL || is_user == NULL)
1195c5c4113dSnw 		return (IDMAP_ERR_ARG);
1196c5c4113dSnw 
11973ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
11983ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
11993ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_pidbysid(sidprefix, rid, pid,
12003ee87bcaSJulian Pullen 		    is_user);
12013ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12023ee87bcaSJulian Pullen 			*stat = retcode;
12033ee87bcaSJulian Pullen 			return (retcode);
12043ee87bcaSJulian Pullen 		}
12053ee87bcaSJulian Pullen 	}
12063ee87bcaSJulian Pullen 
1207c5c4113dSnw 	/* Extend the request array and the return list */
1208c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1209c5c4113dSnw 		goto errout;
1210c5c4113dSnw 
1211c5c4113dSnw 	/* Setup the request */
1212c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1213c5c4113dSnw 	mapping->flag = flag;
1214c5c4113dSnw 	mapping->id1.idtype = IDMAP_SID;
1215c5c4113dSnw 	mapping->id1.idmap_id_u.sid.rid = rid;
1216c5c4113dSnw 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
1217c5c4113dSnw 		retcode = IDMAP_ERR_MEMORY;
1218c5c4113dSnw 		goto errout;
1219c5c4113dSnw 	}
1220c5c4113dSnw 	mapping->id2.idtype = IDMAP_POSIXID;
1221c5c4113dSnw 
1222c5c4113dSnw 	/* Setup pointers for the result */
1223c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_POSIXID;
1224c5c4113dSnw 	gh->retlist[gh->next].uid = pid;
1225c5c4113dSnw 	gh->retlist[gh->next].gid = pid;
1226c5c4113dSnw 	gh->retlist[gh->next].is_user = is_user;
1227c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
122848258c6bSjp 	gh->retlist[gh->next].info = info;
12293ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1230c5c4113dSnw 
1231c5c4113dSnw 	gh->next++;
1232c5c4113dSnw 	return (IDMAP_SUCCESS);
1233c5c4113dSnw 
1234c5c4113dSnw errout:
1235651c0131Sbaban 	if (mapping)
1236651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1237c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1238c5c4113dSnw 	return (retcode);
1239c5c4113dSnw }
1240c5c4113dSnw 
1241c5c4113dSnw 
1242c5c4113dSnw /*
1243c5c4113dSnw  * Given UID, get SID
1244c5c4113dSnw  *
1245c5c4113dSnw  * Input:
1246c5c4113dSnw  * uid  - POSIX UID
1247c5c4113dSnw  * flag - flag
1248c5c4113dSnw  *
1249c5c4113dSnw  * Output:
1250c5c4113dSnw  * stat - status of the get request
1251c5c4113dSnw  * sid  - SID prefix (if stat == 0)
1252c5c4113dSnw  * rid  - rid
1253c5c4113dSnw  *
1254c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1255c5c4113dSnw  */
1256c5c4113dSnw idmap_stat
idmap_get_sidbyuid(idmap_get_handle_t * gh,uid_t uid,int flag,char ** sidprefix,idmap_rid_t * rid,idmap_stat * stat)1257c5c4113dSnw idmap_get_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
1258*a17ce845SMarcel Telka     char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
1259cd37da74Snw {
126048258c6bSjp 	return (idmap_getext_sidbyuid(gh, uid, flag, sidprefix, rid,
126148258c6bSjp 	    NULL, stat));
126248258c6bSjp }
126348258c6bSjp 
126448258c6bSjp 
126548258c6bSjp /*
126648258c6bSjp  * Given UID, get SID
126748258c6bSjp  *
126848258c6bSjp  * Input:
126948258c6bSjp  * uid  - POSIX UID
127048258c6bSjp  * flag - flag
127148258c6bSjp  *
127248258c6bSjp  * Output:
127348258c6bSjp  * stat - status of the get request
127448258c6bSjp  * sid  - SID prefix (if stat == 0)
127548258c6bSjp  * rid  - rid
127648258c6bSjp  * how  - mapping type if stat = 0
127748258c6bSjp  *
127848258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
127948258c6bSjp  */
128048258c6bSjp idmap_stat
idmap_getext_sidbyuid(idmap_get_handle_t * gh,uid_t uid,int flag,char ** sidprefix,idmap_rid_t * rid,idmap_info * info,idmap_stat * stat)128148258c6bSjp idmap_getext_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
1282*a17ce845SMarcel Telka     char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
128348258c6bSjp {
1284c5c4113dSnw 
1285c5c4113dSnw 	idmap_retcode	retcode;
1286651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1287c5c4113dSnw 
1288c5c4113dSnw 	/* sanity checks */
1289c5c4113dSnw 	if (gh == NULL)
1290c5c4113dSnw 		return (IDMAP_ERR_ARG);
1291c5c4113dSnw 	if (sidprefix == NULL)
1292c5c4113dSnw 		return (IDMAP_ERR_ARG);
1293c5c4113dSnw 
12943ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
12953ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
12963ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_sidbyuid(sidprefix, rid, uid);
12973ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12983ee87bcaSJulian Pullen 			*stat = retcode;
12993ee87bcaSJulian Pullen 			return (retcode);
13003ee87bcaSJulian Pullen 		}
13013ee87bcaSJulian Pullen 	}
13023ee87bcaSJulian Pullen 
1303c5c4113dSnw 	/* Extend the request array and the return list */
1304c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1305c5c4113dSnw 		goto errout;
1306c5c4113dSnw 
1307c5c4113dSnw 	/* Setup the request */
1308c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1309c5c4113dSnw 	mapping->flag = flag;
1310c5c4113dSnw 	mapping->id1.idtype = IDMAP_UID;
1311c5c4113dSnw 	mapping->id1.idmap_id_u.uid = uid;
1312c5c4113dSnw 	mapping->id2.idtype = IDMAP_SID;
1313c5c4113dSnw 
1314c5c4113dSnw 	/* Setup pointers for the result */
1315c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_SID;
1316c5c4113dSnw 	gh->retlist[gh->next].sidprefix = sidprefix;
1317c5c4113dSnw 	gh->retlist[gh->next].rid = rid;
1318c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
131948258c6bSjp 	gh->retlist[gh->next].info = info;
13203ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1321c5c4113dSnw 
1322c5c4113dSnw 	gh->next++;
1323c5c4113dSnw 	return (IDMAP_SUCCESS);
1324c5c4113dSnw 
1325c5c4113dSnw errout:
1326651c0131Sbaban 	if (mapping)
1327651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1328c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1329c5c4113dSnw 	return (retcode);
1330c5c4113dSnw }
1331c5c4113dSnw 
1332c5c4113dSnw 
1333c5c4113dSnw /*
1334c5c4113dSnw  * Given GID, get SID
1335c5c4113dSnw  *
1336c5c4113dSnw  * Input:
1337c5c4113dSnw  * gid  - POSIX GID
1338c5c4113dSnw  * flag - flag
1339c5c4113dSnw  *
1340c5c4113dSnw  * Output:
1341c5c4113dSnw  * stat       - status of the get request
1342c5c4113dSnw  * sidprefix  - SID prefix (if stat == 0)
1343c5c4113dSnw  * rid        - rid
1344c5c4113dSnw  *
1345c5c4113dSnw  * Note: The output parameters will be set by idmap_get_mappings()
1346c5c4113dSnw  */
1347c5c4113dSnw idmap_stat
idmap_get_sidbygid(idmap_get_handle_t * gh,gid_t gid,int flag,char ** sidprefix,idmap_rid_t * rid,idmap_stat * stat)1348c5c4113dSnw idmap_get_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
1349*a17ce845SMarcel Telka     char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
1350cd37da74Snw {
135148258c6bSjp 	return (idmap_getext_sidbygid(gh, gid, flag, sidprefix, rid,
135248258c6bSjp 	    NULL, stat));
135348258c6bSjp }
135448258c6bSjp 
135548258c6bSjp 
135648258c6bSjp /*
135748258c6bSjp  * Given GID, get SID
135848258c6bSjp  *
135948258c6bSjp  * Input:
136048258c6bSjp  * gid  - POSIX GID
136148258c6bSjp  * flag - flag
136248258c6bSjp  *
136348258c6bSjp  * Output:
136448258c6bSjp  * stat       - status of the get request
136548258c6bSjp  * sidprefix  - SID prefix (if stat == 0)
136648258c6bSjp  * rid        - rid
136748258c6bSjp  * how        - mapping type if stat = 0
136848258c6bSjp  *
136948258c6bSjp  * Note: The output parameters will be set by idmap_get_mappings()
137048258c6bSjp  */
137148258c6bSjp idmap_stat
idmap_getext_sidbygid(idmap_get_handle_t * gh,gid_t gid,int flag,char ** sidprefix,idmap_rid_t * rid,idmap_info * info,idmap_stat * stat)137248258c6bSjp idmap_getext_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
1373*a17ce845SMarcel Telka     char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
137448258c6bSjp {
1375c5c4113dSnw 
1376c5c4113dSnw 	idmap_retcode	retcode;
1377651c0131Sbaban 	idmap_mapping	*mapping = NULL;
1378c5c4113dSnw 
1379c5c4113dSnw 	/* sanity checks */
1380c5c4113dSnw 	if (gh == NULL)
1381c5c4113dSnw 		return (IDMAP_ERR_ARG);
1382c5c4113dSnw 	if (sidprefix == NULL)
1383c5c4113dSnw 		return (IDMAP_ERR_ARG);
1384c5c4113dSnw 
13853ee87bcaSJulian Pullen 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
13863ee87bcaSJulian Pullen 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
13873ee87bcaSJulian Pullen 		retcode = idmap_cache_lookup_sidbygid(sidprefix, rid, gid);
13883ee87bcaSJulian Pullen 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
13893ee87bcaSJulian Pullen 			*stat = retcode;
13903ee87bcaSJulian Pullen 			return (retcode);
13913ee87bcaSJulian Pullen 		}
13923ee87bcaSJulian Pullen 	}
13933ee87bcaSJulian Pullen 
1394c5c4113dSnw 	/* Extend the request array and the return list */
1395c5c4113dSnw 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
1396c5c4113dSnw 		goto errout;
1397c5c4113dSnw 
1398c5c4113dSnw 	/* Setup the request */
1399c5c4113dSnw 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
1400c5c4113dSnw 	mapping->flag = flag;
1401c5c4113dSnw 	mapping->id1.idtype = IDMAP_GID;
1402c5c4113dSnw 	mapping->id1.idmap_id_u.gid = gid;
1403c5c4113dSnw 	mapping->id2.idtype = IDMAP_SID;
1404c5c4113dSnw 
1405c5c4113dSnw 	/* Setup pointers for the result */
1406c5c4113dSnw 	gh->retlist[gh->next].idtype = IDMAP_SID;
1407c5c4113dSnw 	gh->retlist[gh->next].sidprefix = sidprefix;
1408c5c4113dSnw 	gh->retlist[gh->next].rid = rid;
1409c5c4113dSnw 	gh->retlist[gh->next].stat = stat;
141048258c6bSjp 	gh->retlist[gh->next].info = info;
14113ee87bcaSJulian Pullen 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
1412c5c4113dSnw 
1413c5c4113dSnw 	gh->next++;
1414c5c4113dSnw 	return (IDMAP_SUCCESS);
1415c5c4113dSnw 
1416c5c4113dSnw errout:
1417651c0131Sbaban 	if (mapping)
1418651c0131Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
1419c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1420c5c4113dSnw 	return (retcode);
1421c5c4113dSnw }
1422c5c4113dSnw 
1423c5c4113dSnw 
1424c5c4113dSnw /*
1425c5c4113dSnw  * Process the batched "get mapping" requests. The results (i.e.
1426c5c4113dSnw  * status and identity) will be available in the data areas
1427c5c4113dSnw  * provided by individual requests.
1428c5c4113dSnw  */
1429c5c4113dSnw idmap_stat
idmap_get_mappings(idmap_get_handle_t * gh)1430cd37da74Snw idmap_get_mappings(idmap_get_handle_t *gh)
1431cd37da74Snw {
1432c5c4113dSnw 	idmap_retcode	retcode;
1433c5c4113dSnw 	idmap_ids_res	res;
14343ee87bcaSJulian Pullen 	idmap_id	*res_id;
1435c5c4113dSnw 	int		i;
14363ee87bcaSJulian Pullen 	idmap_id	*req_id;
14373ee87bcaSJulian Pullen 	int		direction;
1438c5c4113dSnw 
1439c5c4113dSnw 	if (gh == NULL) {
1440c5c4113dSnw 		errno = EINVAL;
1441c5c4113dSnw 		return (IDMAP_ERR_ARG);
1442c5c4113dSnw 	}
1443c5c4113dSnw 
1444c5c4113dSnw 	(void) memset(&res, 0, sizeof (idmap_ids_res));
14451fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_IDS,
1446cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping_batch,
1447cd37da74Snw 	    (caddr_t)&gh->batch,
1448cd37da74Snw 	    (xdrproc_t)xdr_idmap_ids_res,
1449cd37da74Snw 	    (caddr_t)&res,
1450cd37da74Snw 	    TIMEOUT);
14511fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS) {
1452c5c4113dSnw 		goto out;
1453c5c4113dSnw 	}
1454c5c4113dSnw 	if (res.retcode != IDMAP_SUCCESS) {
1455c5c4113dSnw 		retcode = res.retcode;
1456c5c4113dSnw 		goto out;
1457c5c4113dSnw 	}
1458c5c4113dSnw 	for (i = 0; i < gh->next; i++) {
1459c5c4113dSnw 		if (i >= res.ids.ids_len) {
1460c5c4113dSnw 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
1461c5c4113dSnw 			continue;
1462c5c4113dSnw 		}
1463c5c4113dSnw 		*gh->retlist[i].stat = res.ids.ids_val[i].retcode;
14643ee87bcaSJulian Pullen 		res_id = &res.ids.ids_val[i].id;
14653ee87bcaSJulian Pullen 		direction = res.ids.ids_val[i].direction;
14663ee87bcaSJulian Pullen 		req_id = &gh->batch.idmap_mapping_batch_val[i].id1;
14673ee87bcaSJulian Pullen 		switch (res_id->idtype) {
1468c5c4113dSnw 		case IDMAP_UID:
1469c5c4113dSnw 			if (gh->retlist[i].uid)
14703ee87bcaSJulian Pullen 				*gh->retlist[i].uid = res_id->idmap_id_u.uid;
1471c5c4113dSnw 			if (gh->retlist[i].is_user)
1472c5c4113dSnw 				*gh->retlist[i].is_user = 1;
14733ee87bcaSJulian Pullen 
14743ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
14753ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
14763ee87bcaSJulian Pullen 				if (gh->retlist[i].is_user != NULL)
14773ee87bcaSJulian Pullen 					idmap_cache_add_sid2pid(
14783ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
14793ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
14803ee87bcaSJulian Pullen 					    res_id->idmap_id_u.uid, 1,
14813ee87bcaSJulian Pullen 					    direction);
14823ee87bcaSJulian Pullen 				else
14833ee87bcaSJulian Pullen 					idmap_cache_add_sid2uid(
14843ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
14853ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
14863ee87bcaSJulian Pullen 					    res_id->idmap_id_u.uid,
14873ee87bcaSJulian Pullen 					    direction);
14883ee87bcaSJulian Pullen 			}
1489c5c4113dSnw 			break;
14903ee87bcaSJulian Pullen 
1491c5c4113dSnw 		case IDMAP_GID:
1492c5c4113dSnw 			if (gh->retlist[i].gid)
14933ee87bcaSJulian Pullen 				*gh->retlist[i].gid = res_id->idmap_id_u.gid;
1494c5c4113dSnw 			if (gh->retlist[i].is_user)
1495c5c4113dSnw 				*gh->retlist[i].is_user = 0;
14963ee87bcaSJulian Pullen 
14973ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
14983ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
14993ee87bcaSJulian Pullen 				if (gh->retlist[i].is_user != NULL)
15003ee87bcaSJulian Pullen 					idmap_cache_add_sid2pid(
15013ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
15023ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
15033ee87bcaSJulian Pullen 					    res_id->idmap_id_u.gid, 0,
15043ee87bcaSJulian Pullen 					    direction);
15053ee87bcaSJulian Pullen 				else
15063ee87bcaSJulian Pullen 					idmap_cache_add_sid2gid(
15073ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.prefix,
15083ee87bcaSJulian Pullen 					    req_id->idmap_id_u.sid.rid,
15093ee87bcaSJulian Pullen 					    res_id->idmap_id_u.gid,
15103ee87bcaSJulian Pullen 					    direction);
15113ee87bcaSJulian Pullen 			}
1512c5c4113dSnw 			break;
15133ee87bcaSJulian Pullen 
151462c60062Sbaban 		case IDMAP_POSIXID:
151562c60062Sbaban 			if (gh->retlist[i].uid)
151662c60062Sbaban 				*gh->retlist[i].uid = 60001;
151762c60062Sbaban 			if (gh->retlist[i].is_user)
151862c60062Sbaban 				*gh->retlist[i].is_user = -1;
151962c60062Sbaban 			break;
15203ee87bcaSJulian Pullen 
1521c5c4113dSnw 		case IDMAP_SID:
1522cd37da74Snw 		case IDMAP_USID:
1523cd37da74Snw 		case IDMAP_GSID:
1524c5c4113dSnw 			if (gh->retlist[i].rid)
15253ee87bcaSJulian Pullen 				*gh->retlist[i].rid =
15263ee87bcaSJulian Pullen 				    res_id->idmap_id_u.sid.rid;
1527c5c4113dSnw 			if (gh->retlist[i].sidprefix) {
15283ee87bcaSJulian Pullen 				if (res_id->idmap_id_u.sid.prefix == NULL ||
15293ee87bcaSJulian Pullen 				    *res_id->idmap_id_u.sid.prefix == '\0') {
1530c5c4113dSnw 					*gh->retlist[i].sidprefix = NULL;
1531c5c4113dSnw 					break;
1532c5c4113dSnw 				}
1533c5c4113dSnw 				*gh->retlist[i].sidprefix =
15343ee87bcaSJulian Pullen 				    strdup(res_id->idmap_id_u.sid.prefix);
1535c5c4113dSnw 				if (*gh->retlist[i].sidprefix == NULL)
1536c5c4113dSnw 					*gh->retlist[i].stat =
1537cd37da74Snw 					    IDMAP_ERR_MEMORY;
1538c5c4113dSnw 			}
15393ee87bcaSJulian Pullen 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
15403ee87bcaSJulian Pullen 			    gh->retlist[i].cache_res) {
15413ee87bcaSJulian Pullen 				if (req_id->idtype == IDMAP_UID)
15423ee87bcaSJulian Pullen 					idmap_cache_add_sid2uid(
15433ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.prefix,
15443ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.rid,
15453ee87bcaSJulian Pullen 					    req_id->idmap_id_u.uid,
15463ee87bcaSJulian Pullen 					    direction);
15473ee87bcaSJulian Pullen 				else /* req_id->idtype == IDMAP_GID */
15483ee87bcaSJulian Pullen 					idmap_cache_add_sid2gid(
15493ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.prefix,
15503ee87bcaSJulian Pullen 					    res_id->idmap_id_u.sid.rid,
15513ee87bcaSJulian Pullen 					    req_id->idmap_id_u.gid,
15523ee87bcaSJulian Pullen 					    direction);
15533ee87bcaSJulian Pullen 			}
1554c5c4113dSnw 			break;
15553ee87bcaSJulian Pullen 
1556c5c4113dSnw 		case IDMAP_NONE:
1557c5c4113dSnw 			break;
15583ee87bcaSJulian Pullen 
1559c5c4113dSnw 		default:
1560c5c4113dSnw 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
1561c5c4113dSnw 			break;
1562c5c4113dSnw 		}
1563148c5f43SAlan Wright 		if (gh->retlist[i].info != NULL) {
1564148c5f43SAlan Wright 			idmap_info_mov(gh->retlist[i].info,
156548258c6bSjp 			    &res.ids.ids_val[i].info);
1566148c5f43SAlan Wright 		}
1567c5c4113dSnw 	}
1568c5c4113dSnw 	retcode = IDMAP_SUCCESS;
1569c5c4113dSnw 
1570c5c4113dSnw out:
1571651c0131Sbaban 	_IDMAP_RESET_GET_HANDLE(gh);
1572*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_ids_res, (caddr_t)&res);
1573c5c4113dSnw 	errno = idmap_stat2errno(retcode);
1574c5c4113dSnw 	return (retcode);
1575c5c4113dSnw }
1576c5c4113dSnw 
1577c5c4113dSnw 
1578c5c4113dSnw /*
1579c5c4113dSnw  * Destroy the "get mapping" handle
1580c5c4113dSnw  */
1581c5c4113dSnw void
idmap_get_destroy(idmap_get_handle_t * gh)1582cd37da74Snw idmap_get_destroy(idmap_get_handle_t *gh)
1583cd37da74Snw {
1584c5c4113dSnw 	if (gh == NULL)
1585c5c4113dSnw 		return;
1586*a17ce845SMarcel Telka 	xdr_free(xdr_idmap_mapping_batch, (caddr_t)&gh->batch);
1587c5c4113dSnw 	if (gh->retlist)
1588c5c4113dSnw 		free(gh->retlist);
1589c5c4113dSnw 	free(gh);
1590c5c4113dSnw }
1591c5c4113dSnw 
1592c5c4113dSnw 
1593c5c4113dSnw /*
1594c5c4113dSnw  * Get windows to unix mapping
1595c5c4113dSnw  */
1596c5c4113dSnw idmap_stat
idmap_get_w2u_mapping(const char * sidprefix,idmap_rid_t * rid,const char * winname,const char * windomain,int flag,int * is_user,int * is_wuser,uid_t * pid,char ** unixname,int * direction,idmap_info * info)15971fdeec65Sjoyce mcintosh idmap_get_w2u_mapping(
1598c5c4113dSnw 		const char *sidprefix, idmap_rid_t *rid,
1599c5c4113dSnw 		const char *winname, const char *windomain,
1600cd37da74Snw 		int flag, int *is_user, int *is_wuser,
160148258c6bSjp 		uid_t *pid, char **unixname, int *direction, idmap_info *info)
1602cd37da74Snw {
1603c5c4113dSnw 	idmap_mapping		request, *mapping;
1604c5c4113dSnw 	idmap_mappings_res	result;
1605c5c4113dSnw 	idmap_retcode		retcode, rc;
1606c5c4113dSnw 
1607c5c4113dSnw 	(void) memset(&request, 0, sizeof (request));
1608c5c4113dSnw 	(void) memset(&result, 0, sizeof (result));
1609c5c4113dSnw 
1610c5c4113dSnw 	if (pid)
1611c5c4113dSnw 		*pid = UINT32_MAX;
1612c5c4113dSnw 	if (unixname)
1613c5c4113dSnw 		*unixname = NULL;
1614c5c4113dSnw 	if (direction)
1615651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
1616c5c4113dSnw 
1617c5c4113dSnw 	request.flag = flag;
1618c5c4113dSnw 	request.id1.idtype = IDMAP_SID;
1619c5c4113dSnw 	if (sidprefix && rid) {
1620c5c4113dSnw 		request.id1.idmap_id_u.sid.prefix = (char *)sidprefix;
1621c5c4113dSnw 		request.id1.idmap_id_u.sid.rid = *rid;
1622c5c4113dSnw 	} else if (winname) {
16238e228215Sdm 		retcode = idmap_strdupnull(&request.id1name, winname);
1624c5a946baSbaban 		if (retcode != IDMAP_SUCCESS)
1625c5c4113dSnw 			goto out;
16268e228215Sdm 
16278e228215Sdm 		retcode = idmap_strdupnull(&request.id1domain, windomain);
1628c5a946baSbaban 		if (retcode != IDMAP_SUCCESS)
16298e228215Sdm 			goto out;
16308e228215Sdm 
1631c5c4113dSnw 		request.id1.idmap_id_u.sid.prefix = NULL;
1632c5c4113dSnw 	} else {
1633c5c4113dSnw 		errno = EINVAL;
1634c5c4113dSnw 		return (IDMAP_ERR_ARG);
1635c5c4113dSnw 	}
1636c5c4113dSnw 
1637cd37da74Snw 	if (*is_user == 1)
1638c5c4113dSnw 		request.id2.idtype = IDMAP_UID;
1639c5c4113dSnw 	else if (*is_user == 0)
1640c5c4113dSnw 		request.id2.idtype = IDMAP_GID;
1641c5c4113dSnw 	else
1642c5c4113dSnw 		request.id2.idtype = IDMAP_POSIXID;
1643c5c4113dSnw 
1644cd37da74Snw 	if (*is_wuser == 1)
1645cd37da74Snw 		request.id1.idtype = IDMAP_USID;
1646cd37da74Snw 	else if (*is_wuser == 0)
1647cd37da74Snw 		request.id1.idtype = IDMAP_GSID;
1648cd37da74Snw 	else
1649cd37da74Snw 		request.id1.idtype = IDMAP_SID;
1650cd37da74Snw 
16511fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_ID_BY_NAME,
1652cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
1653cd37da74Snw 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
1654cd37da74Snw 	    TIMEOUT);
1655c5c4113dSnw 
16561fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
165729d55245SJerry Jelinek 		goto out;
1658c5c4113dSnw 
1659c5c4113dSnw 	retcode = result.retcode;
1660c5c4113dSnw 
1661c5c4113dSnw 	if ((mapping = result.mappings.mappings_val) == NULL) {
1662c5c4113dSnw 		if (retcode == IDMAP_SUCCESS)
1663c5c4113dSnw 			retcode = IDMAP_ERR_NORESULT;
1664c5c4113dSnw 		goto out;
1665c5c4113dSnw 	}
1666c5c4113dSnw 
1667148c5f43SAlan Wright 	if (info != NULL)
1668148c5f43SAlan Wright 		idmap_info_mov(info, &mapping->info);
1669148c5f43SAlan Wright 
167062c60062Sbaban 	if (mapping->id2.idtype == IDMAP_UID) {
1671cd37da74Snw 		*is_user = 1;
167262c60062Sbaban 	} else if (mapping->id2.idtype == IDMAP_GID) {
1673cd37da74Snw 		*is_user = 0;
167462c60062Sbaban 	} else {
167562c60062Sbaban 		goto out;
167662c60062Sbaban 	}
1677cd37da74Snw 
1678cd37da74Snw 	if (mapping->id1.idtype == IDMAP_USID) {
1679cd37da74Snw 		*is_wuser = 1;
1680cd37da74Snw 	} else if (mapping->id1.idtype == IDMAP_GSID) {
1681cd37da74Snw 		*is_wuser = 0;
1682cd37da74Snw 	} else {
1683cd37da74Snw 		goto out;
1684cd37da74Snw 	}
1685cd37da74Snw 
1686c5c4113dSnw 	if (direction)
1687c5c4113dSnw 		*direction = mapping->direction;
1688c5c4113dSnw 	if (pid)
1689c5c4113dSnw 		*pid = mapping->id2.idmap_id_u.uid;
16908e228215Sdm 
16918e228215Sdm 	rc = idmap_strdupnull(unixname, mapping->id2name);
16928e228215Sdm 	if (rc != IDMAP_SUCCESS)
16938e228215Sdm 		retcode = rc;
1694c5c4113dSnw 
1695c5c4113dSnw out:
1696f7b4b2feSjp 	if (request.id1name != NULL)
1697f7b4b2feSjp 		free(request.id1name);
1698f7b4b2feSjp 	if (request.id1domain != NULL)
1699f7b4b2feSjp 		free(request.id1domain);
1700c5c4113dSnw 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
1701c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
1702c5c4113dSnw 		errno = idmap_stat2errno(retcode);
1703c5c4113dSnw 	return (retcode);
1704c5c4113dSnw }
1705c5c4113dSnw 
1706c5c4113dSnw 
1707c5c4113dSnw /*
1708c5c4113dSnw  * Get unix to windows mapping
1709c5c4113dSnw  */
1710c5c4113dSnw idmap_stat
idmap_get_u2w_mapping(uid_t * pid,const char * unixname,int flag,int is_user,int * is_wuser,char ** sidprefix,idmap_rid_t * rid,char ** winname,char ** windomain,int * direction,idmap_info * info)17111fdeec65Sjoyce mcintosh idmap_get_u2w_mapping(
1712c5c4113dSnw 		uid_t *pid, const char *unixname,
1713cd37da74Snw 		int flag, int is_user, int *is_wuser,
1714c5c4113dSnw 		char **sidprefix, idmap_rid_t *rid,
1715c5c4113dSnw 		char **winname, char **windomain,
171648258c6bSjp 		int *direction, idmap_info *info)
1717cd37da74Snw {
1718c5c4113dSnw 	idmap_mapping		request, *mapping;
1719c5c4113dSnw 	idmap_mappings_res	result;
1720c5c4113dSnw 	idmap_retcode		retcode, rc;
1721c5c4113dSnw 
1722c5c4113dSnw 	if (sidprefix)
1723c5c4113dSnw 		*sidprefix = NULL;
1724c5c4113dSnw 	if (winname)
1725c5c4113dSnw 		*winname = NULL;
1726c5c4113dSnw 	if (windomain)
1727c5c4113dSnw 		*windomain = NULL;
1728c5c4113dSnw 	if (rid)
1729c5c4113dSnw 		*rid = UINT32_MAX;
1730c5c4113dSnw 	if (direction)
1731651c0131Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
1732c5c4113dSnw 
1733c5c4113dSnw 	(void) memset(&request, 0, sizeof (request));
1734c5c4113dSnw 	(void) memset(&result, 0, sizeof (result));
1735c5c4113dSnw 
1736c5c4113dSnw 	request.flag = flag;
1737c5c4113dSnw 	request.id1.idtype = is_user?IDMAP_UID:IDMAP_GID;
1738c5c4113dSnw 
1739c5c4113dSnw 	if (pid && *pid != UINT32_MAX) {
1740c5c4113dSnw 		request.id1.idmap_id_u.uid = *pid;
1741c5c4113dSnw 	} else if (unixname) {
17428e228215Sdm 		request.id1name = (char *)unixname;
1743c5c4113dSnw 		request.id1.idmap_id_u.uid = UINT32_MAX;
1744c5c4113dSnw 	} else {
1745c5c4113dSnw 		errno = EINVAL;
1746c5c4113dSnw 		return (IDMAP_ERR_ARG);
1747c5c4113dSnw 	}
1748c5c4113dSnw 
1749cd37da74Snw 	if (is_wuser == NULL)
1750cd37da74Snw 		request.id2.idtype = IDMAP_SID;
1751cd37da74Snw 	else if (*is_wuser == -1)
1752cd37da74Snw 		request.id2.idtype = IDMAP_SID;
1753cd37da74Snw 	else if (*is_wuser == 0)
1754cd37da74Snw 		request.id2.idtype = IDMAP_GSID;
1755cd37da74Snw 	else if (*is_wuser == 1)
1756cd37da74Snw 		request.id2.idtype = IDMAP_USID;
1757c5c4113dSnw 
17581fdeec65Sjoyce mcintosh 	retcode = _idmap_clnt_call(IDMAP_GET_MAPPED_ID_BY_NAME,
1759cd37da74Snw 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
1760cd37da74Snw 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
1761cd37da74Snw 	    TIMEOUT);
1762c5c4113dSnw 
17631fdeec65Sjoyce mcintosh 	if (retcode != IDMAP_SUCCESS)
17641fdeec65Sjoyce mcintosh 		return (retcode);
1765c5c4113dSnw 
1766c5c4113dSnw 	retcode = result.retcode;
1767c5c4113dSnw 
1768c5c4113dSnw 	if ((mapping = result.mappings.mappings_val) == NULL) {
1769c5c4113dSnw 		if (retcode == IDMAP_SUCCESS)
1770c5c4113dSnw 			retcode = IDMAP_ERR_NORESULT;
1771c5c4113dSnw 		goto out;
1772c5c4113dSnw 	}
1773c5c4113dSnw 
1774148c5f43SAlan Wright 	if (info != NULL)
1775148c5f43SAlan Wright 		idmap_info_mov(info, &mapping->info);
1776148c5f43SAlan Wright 
1777cd37da74Snw 	if (direction != NULL)
1778c5c4113dSnw 		*direction = mapping->direction;
1779cd37da74Snw 
178048258c6bSjp 	if (is_wuser != NULL) {
178148258c6bSjp 		if (mapping->id2.idtype == IDMAP_USID)
178248258c6bSjp 			*is_wuser = 1;
178348258c6bSjp 		else if (mapping->id2.idtype == IDMAP_GSID)
178448258c6bSjp 			*is_wuser = 0;
178548258c6bSjp 		else
178648258c6bSjp 			*is_wuser = -1;
178748258c6bSjp 	}
1788cd37da74Snw 
17898edda628Sbaban 	if (sidprefix && mapping->id2.idmap_id_u.sid.prefix &&
17908edda628Sbaban 	    *mapping->id2.idmap_id_u.sid.prefix != '\0') {
1791c5c4113dSnw 		*sidprefix = strdup(mapping->id2.idmap_id_u.sid.prefix);
1792c5c4113dSnw 		if (*sidprefix == NULL) {
1793c5c4113dSnw 			retcode = IDMAP_ERR_MEMORY;
1794c5c4113dSnw 			goto errout;
1795c5c4113dSnw 		}
1796c5c4113dSnw 	}
1797c5c4113dSnw 	if (rid)
1798c5c4113dSnw 		*rid = mapping->id2.idmap_id_u.sid.rid;
17998e228215Sdm 
18008e228215Sdm 	rc = idmap_strdupnull(winname, mapping->id2name);
18018e228215Sdm 	if (rc != IDMAP_SUCCESS)
18028e228215Sdm 		retcode = rc;
18038e228215Sdm 
18048e228215Sdm 	rc = idmap_strdupnull(windomain, mapping->id2domain);
18058e228215Sdm 	if (rc != IDMAP_SUCCESS)
18068e228215Sdm 		retcode = rc;
1807c5c4113dSnw 
1808c5c4113dSnw 	goto out;
1809c5c4113dSnw 
1810c5c4113dSnw errout:
1811c5c4113dSnw 	if (sidprefix && *sidprefix) {
1812c5c4113dSnw 		free(*sidprefix);
1813c5c4113dSnw 		*sidprefix = NULL;
1814c5c4113dSnw 	}
1815c5c4113dSnw 	if (winname && *winname) {
1816c5c4113dSnw 		free(*winname);
1817c5c4113dSnw 		*winname = NULL;
1818c5c4113dSnw 	}
1819c5c4113dSnw 	if (windomain && *windomain) {
1820c5c4113dSnw 		free(*windomain);
1821c5c4113dSnw 		*windomain = NULL;
1822c5c4113dSnw 	}
1823c5c4113dSnw 
1824c5c4113dSnw out:
1825c5c4113dSnw 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
1826c5c4113dSnw 	if (retcode != IDMAP_SUCCESS)
1827c5c4113dSnw 		errno = idmap_stat2errno(retcode);
1828c5c4113dSnw 	return (retcode);
1829c5c4113dSnw }
1830c5c4113dSnw 
1831c5c4113dSnw 
1832c5c4113dSnw 
1833c5c4113dSnw #define	gettext(s)	s
1834c5c4113dSnw static stat_table_t stattable[] = {
1835c5c4113dSnw 	{IDMAP_SUCCESS, gettext("Success"), 0},
1836c5c4113dSnw 	{IDMAP_NEXT, gettext("More results available"), 0},
1837c5c4113dSnw 	{IDMAP_ERR_OTHER, gettext("Undefined error"), EINVAL},
1838c5c4113dSnw 	{IDMAP_ERR_INTERNAL, gettext("Internal error"), EINVAL},
1839c5c4113dSnw 	{IDMAP_ERR_MEMORY, gettext("Out of memory"), ENOMEM},
1840c5c4113dSnw 	{IDMAP_ERR_NORESULT, gettext("No results available"), EINVAL},
1841c5c4113dSnw 	{IDMAP_ERR_NOTUSER, gettext("Not a user"), EINVAL},
1842c5c4113dSnw 	{IDMAP_ERR_NOTGROUP, gettext("Not a group"), EINVAL},
1843651c0131Sbaban 	{IDMAP_ERR_NOTSUPPORTED, gettext("Operation not supported"), ENOTSUP},
1844c5c4113dSnw 	{IDMAP_ERR_W2U_NAMERULE,
1845c5c4113dSnw 		gettext("Invalid Windows to UNIX name-based rule"), EINVAL},
1846c5c4113dSnw 	{IDMAP_ERR_U2W_NAMERULE,
1847c5c4113dSnw 		gettext("Invalid UNIX to Windows name-based rule"), EINVAL},
1848c5c4113dSnw 	{IDMAP_ERR_CACHE, gettext("Invalid cache"), EINVAL},
1849c5c4113dSnw 	{IDMAP_ERR_DB, gettext("Invalid database"), EINVAL},
1850c5c4113dSnw 	{IDMAP_ERR_ARG, gettext("Invalid argument"), EINVAL},
1851c5c4113dSnw 	{IDMAP_ERR_SID, gettext("Invalid SID"), EINVAL},
1852c5c4113dSnw 	{IDMAP_ERR_IDTYPE, gettext("Invalid identity type"), EINVAL},
1853651c0131Sbaban 	{IDMAP_ERR_RPC_HANDLE, gettext("Bad RPC handle"), EBADF},
1854c5c4113dSnw 	{IDMAP_ERR_RPC, gettext("RPC error"), EINVAL},
1855c5c4113dSnw 	{IDMAP_ERR_CLIENT_HANDLE, gettext("Bad client handle"), EINVAL},
1856651c0131Sbaban 	{IDMAP_ERR_BUSY, gettext("Server is busy"), EBUSY},
18578edda628Sbaban 	{IDMAP_ERR_PERMISSION_DENIED, gettext("Permission denied"), EACCES},
1858c5c4113dSnw 	{IDMAP_ERR_NOMAPPING,
1859c5c4113dSnw 		gettext("Mapping not found or inhibited"), EINVAL},
1860c5c4113dSnw 	{IDMAP_ERR_NEW_ID_ALLOC_REQD,
1861c5c4113dSnw 		gettext("New mapping needs to be created"), EINVAL},
1862c5c4113dSnw 	{IDMAP_ERR_DOMAIN, gettext("Invalid domain"), EINVAL},
1863c5c4113dSnw 	{IDMAP_ERR_SECURITY, gettext("Security issue"), EINVAL},
1864c5c4113dSnw 	{IDMAP_ERR_NOTFOUND, gettext("Not found"), EINVAL},
1865c5c4113dSnw 	{IDMAP_ERR_DOMAIN_NOTFOUND, gettext("Domain not found"), EINVAL},
1866c5c4113dSnw 	{IDMAP_ERR_UPDATE_NOTALLOWED, gettext("Update not allowed"), EINVAL},
1867c5c4113dSnw 	{IDMAP_ERR_CFG, gettext("Configuration error"), EINVAL},
1868c5c4113dSnw 	{IDMAP_ERR_CFG_CHANGE, gettext("Invalid configuration change"), EINVAL},
1869c5c4113dSnw 	{IDMAP_ERR_NOTMAPPED_WELLKNOWN,
1870c5c4113dSnw 		gettext("No mapping for well-known SID"), EINVAL},
1871c5c4113dSnw 	{IDMAP_ERR_RETRIABLE_NET_ERR,
187262c60062Sbaban 		gettext("Windows lookup failed"), EINVAL},
187362c60062Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
187462c60062Sbaban 		gettext("Duplicate rule or conflicts with an existing "
187562c60062Sbaban 		"Windows to UNIX name-based rule"), EINVAL},
187662c60062Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
187762c60062Sbaban 		gettext("Duplicate rule or conflicts with an existing "
187862c60062Sbaban 		"Unix to Windows name-based rule"), EINVAL},
18790dcc7149Snw 	{IDMAP_ERR_BAD_UTF8,
18800dcc7149Snw 		gettext("Invalid or illegal UTF-8 sequence found in "
18810dcc7149Snw 		"a given Windows entity name or domain name"), EINVAL},
18824d61c878SJulian Pullen 	{IDMAP_ERR_NONE_GENERATED,
188348258c6bSjp 		gettext("Mapping not found and none created (see -c option)"),
188448258c6bSjp 		EINVAL},
1885479ac375Sdm 	{IDMAP_ERR_PROP_UNKNOWN,
1886479ac375Sdm 		gettext("Undefined property"),
1887479ac375Sdm 		EINVAL},
1888479ac375Sdm 	{IDMAP_ERR_NS_LDAP_CFG,
1889479ac375Sdm 		gettext("Native LDAP configuration error"), EINVAL},
1890479ac375Sdm 	{IDMAP_ERR_NS_LDAP_PARTIAL,
1891479ac375Sdm 		gettext("Partial result from Native LDAP"), EINVAL},
1892479ac375Sdm 	{IDMAP_ERR_NS_LDAP_OP_FAILED,
1893479ac375Sdm 		gettext("Native LDAP operation failed"), EINVAL},
1894479ac375Sdm 	{IDMAP_ERR_NS_LDAP_BAD_WINNAME,
1895479ac375Sdm 		gettext("Improper winname form found in Native LDAP"), EINVAL},
18964d61c878SJulian Pullen 	{IDMAP_ERR_NO_ACTIVEDIRECTORY,
18974d61c878SJulian Pullen 		gettext("No AD servers"),
18984d61c878SJulian Pullen 		EINVAL},
1899c5c4113dSnw 	{-1, NULL, 0}
1900c5c4113dSnw };
1901c5c4113dSnw #undef	gettext
1902c5c4113dSnw 
1903c5c4113dSnw 
1904c5c4113dSnw /*
1905c5c4113dSnw  * Get description of status code
1906c5c4113dSnw  *
1907c5c4113dSnw  * Input:
1908c5c4113dSnw  * status - Status code returned by libidmap API call
1909c5c4113dSnw  *
1910c5c4113dSnw  * Return Value:
1911c5c4113dSnw  * human-readable localized description of idmap_stat
1912c5c4113dSnw  */
1913c5c4113dSnw const char *
idmap_stat2string(idmap_stat status)19141fdeec65Sjoyce mcintosh idmap_stat2string(idmap_stat status)
1915cd37da74Snw {
1916c5c4113dSnw 	int i;
1917c5c4113dSnw 
1918c5c4113dSnw 	for (i = 0; stattable[i].msg; i++) {
1919c5c4113dSnw 		if (stattable[i].retcode == status)
19201fcced4cSJordan Brown 			return (dgettext(TEXT_DOMAIN, stattable[i].msg));
1921c5c4113dSnw 	}
19221fcced4cSJordan Brown 	return (dgettext(TEXT_DOMAIN, "Unknown error"));
1923c5c4113dSnw }
1924c5c4113dSnw 
1925c5c4113dSnw 
1926c5c4113dSnw static int
idmap_stat2errno(idmap_stat stat)1927cd37da74Snw idmap_stat2errno(idmap_stat stat)
1928cd37da74Snw {
1929c5c4113dSnw 	int i;
1930c5c4113dSnw 	for (i = 0; stattable[i].msg; i++) {
1931c5c4113dSnw 		if (stattable[i].retcode == stat)
1932c5c4113dSnw 			return (stattable[i].errnum);
1933c5c4113dSnw 	}
1934c5c4113dSnw 	return (EINVAL);
1935c5c4113dSnw }
1936c5c4113dSnw 
1937c5c4113dSnw 
1938c5c4113dSnw /*
1939c5c4113dSnw  * Get status code from string
1940c5c4113dSnw  */
1941c5c4113dSnw idmap_stat
idmap_string2stat(const char * str)1942cd37da74Snw idmap_string2stat(const char *str)
1943cd37da74Snw {
1944c5c4113dSnw 	if (str == NULL)
1945c5c4113dSnw 		return (IDMAP_ERR_INTERNAL);
1946c5c4113dSnw 
1947c5c4113dSnw #define	return_cmp(a) \
1948c5c4113dSnw 	if (0 == strcmp(str, "IDMAP_ERR_" #a)) \
1949c5c4113dSnw 		return (IDMAP_ERR_ ## a);
1950c5c4113dSnw 
1951c5c4113dSnw 	return_cmp(OTHER);
1952c5c4113dSnw 	return_cmp(INTERNAL);
1953c5c4113dSnw 	return_cmp(MEMORY);
1954c5c4113dSnw 	return_cmp(NORESULT);
1955c5c4113dSnw 	return_cmp(NOTUSER);
1956c5c4113dSnw 	return_cmp(NOTGROUP);
1957c5c4113dSnw 	return_cmp(NOTSUPPORTED);
1958c5c4113dSnw 	return_cmp(W2U_NAMERULE);
1959c5c4113dSnw 	return_cmp(U2W_NAMERULE);
1960c5c4113dSnw 	return_cmp(CACHE);
1961c5c4113dSnw 	return_cmp(DB);
1962c5c4113dSnw 	return_cmp(ARG);
1963c5c4113dSnw 	return_cmp(SID);
1964c5c4113dSnw 	return_cmp(IDTYPE);
1965c5c4113dSnw 	return_cmp(RPC_HANDLE);
1966c5c4113dSnw 	return_cmp(RPC);
1967c5c4113dSnw 	return_cmp(CLIENT_HANDLE);
1968c5c4113dSnw 	return_cmp(BUSY);
1969c5c4113dSnw 	return_cmp(PERMISSION_DENIED);
1970c5c4113dSnw 	return_cmp(NOMAPPING);
1971c5c4113dSnw 	return_cmp(NEW_ID_ALLOC_REQD);
1972c5c4113dSnw 	return_cmp(DOMAIN);
1973c5c4113dSnw 	return_cmp(SECURITY);
1974c5c4113dSnw 	return_cmp(NOTFOUND);
1975c5c4113dSnw 	return_cmp(DOMAIN_NOTFOUND);
1976c5c4113dSnw 	return_cmp(MEMORY);
1977c5c4113dSnw 	return_cmp(UPDATE_NOTALLOWED);
1978c5c4113dSnw 	return_cmp(CFG);
1979c5c4113dSnw 	return_cmp(CFG_CHANGE);
1980c5c4113dSnw 	return_cmp(NOTMAPPED_WELLKNOWN);
1981c5c4113dSnw 	return_cmp(RETRIABLE_NET_ERR);
198262c60062Sbaban 	return_cmp(W2U_NAMERULE_CONFLICT);
198362c60062Sbaban 	return_cmp(U2W_NAMERULE_CONFLICT);
1984479ac375Sdm 	return_cmp(BAD_UTF8);
19854d61c878SJulian Pullen 	return_cmp(NONE_GENERATED);
1986479ac375Sdm 	return_cmp(PROP_UNKNOWN);
1987479ac375Sdm 	return_cmp(NS_LDAP_CFG);
1988479ac375Sdm 	return_cmp(NS_LDAP_PARTIAL);
1989479ac375Sdm 	return_cmp(NS_LDAP_OP_FAILED);
1990479ac375Sdm 	return_cmp(NS_LDAP_BAD_WINNAME);
19914d61c878SJulian Pullen 	return_cmp(NO_ACTIVEDIRECTORY);
1992c5c4113dSnw #undef return_cmp
1993c5c4113dSnw 
1994c5c4113dSnw 	return (IDMAP_ERR_OTHER);
1995c5c4113dSnw }
1996c5c4113dSnw 
1997c5c4113dSnw 
1998c5c4113dSnw /*
1999c5c4113dSnw  * Map the given status to one that can be returned by the protocol
2000c5c4113dSnw  */
2001c5c4113dSnw idmap_stat
idmap_stat4prot(idmap_stat status)2002cd37da74Snw idmap_stat4prot(idmap_stat status)
2003cd37da74Snw {
2004c5c4113dSnw 	switch (status) {
2005c5c4113dSnw 	case IDMAP_ERR_MEMORY:
2006c5c4113dSnw 	case IDMAP_ERR_CACHE:
2007c5c4113dSnw 		return (IDMAP_ERR_INTERNAL);
2008c5c4113dSnw 	}
2009c5c4113dSnw 	return (status);
2010c5c4113dSnw }
2011dd5829d1Sbaban 
2012dd5829d1Sbaban 
20138e228215Sdm /*
2014c5a946baSbaban  * This is a convenience routine which duplicates a string after
2015c5a946baSbaban  * checking for NULL pointers. This function will return success if
2016c5a946baSbaban  * either the 'to' OR 'from' pointers are NULL.
20178e228215Sdm  */
20188e228215Sdm static idmap_stat
idmap_strdupnull(char ** to,const char * from)2019cd37da74Snw idmap_strdupnull(char **to, const char *from)
2020cd37da74Snw {
2021c5a946baSbaban 	if (to == NULL)
2022c5a946baSbaban 		return (IDMAP_SUCCESS);
2023c5a946baSbaban 
20248e228215Sdm 	if (from == NULL || *from == '\0') {
20258e228215Sdm 		*to = NULL;
20268e228215Sdm 		return (IDMAP_SUCCESS);
20278e228215Sdm 	}
20288e228215Sdm 
20298e228215Sdm 	*to = strdup(from);
20308e228215Sdm 	if (*to == NULL)
20318e228215Sdm 		return (IDMAP_ERR_MEMORY);
20328e228215Sdm 	return (IDMAP_SUCCESS);
20338e228215Sdm }
20348e228215Sdm 
203548258c6bSjp 
20368e228215Sdm idmap_stat
idmap_namerule_cpy(idmap_namerule * to,idmap_namerule * from)2037cd37da74Snw idmap_namerule_cpy(idmap_namerule *to, idmap_namerule *from)
2038cd37da74Snw {
20398e228215Sdm 	idmap_stat retval;
20408e228215Sdm 
204148258c6bSjp 	if (to == NULL)
204248258c6bSjp 		return (IDMAP_SUCCESS);
204348258c6bSjp 
20448e228215Sdm 	(void) memcpy(to, from, sizeof (idmap_namerule));
204548258c6bSjp 	to->windomain = NULL;
204648258c6bSjp 	to->winname = NULL;
204748258c6bSjp 	to->unixname = NULL;
20488e228215Sdm 
20498e228215Sdm 	retval = idmap_strdupnull(&to->windomain, from->windomain);
20508e228215Sdm 	if (retval != IDMAP_SUCCESS)
20518e228215Sdm 		return (retval);
20528e228215Sdm 
20538e228215Sdm 	retval = idmap_strdupnull(&to->winname, from->winname);
205448258c6bSjp 	if (retval != IDMAP_SUCCESS) {
205548258c6bSjp 		free(to->windomain);
205648258c6bSjp 		to->windomain = NULL;
20578e228215Sdm 		return (retval);
205848258c6bSjp 	}
20598e228215Sdm 
20608e228215Sdm 	retval = idmap_strdupnull(&to->unixname, from->unixname);
206148258c6bSjp 	if (retval != IDMAP_SUCCESS) {
206248258c6bSjp 		free(to->windomain);
206348258c6bSjp 		to->windomain = NULL;
206448258c6bSjp 		free(to->winname);
206548258c6bSjp 		to->winname = NULL;
206648258c6bSjp 		return (retval);
206748258c6bSjp 	}
206848258c6bSjp 
206948258c6bSjp 	return (retval);
207048258c6bSjp }
207148258c6bSjp 
207248258c6bSjp 
207348258c6bSjp /*
2074148c5f43SAlan Wright  * Move the contents of the "info" structure from "from" to "to".
207548258c6bSjp  */
2076148c5f43SAlan Wright void
idmap_info_mov(idmap_info * to,idmap_info * from)207748258c6bSjp idmap_info_mov(idmap_info *to, idmap_info *from)
207848258c6bSjp {
207948258c6bSjp 	(void) memcpy(to, from, sizeof (idmap_info));
208048258c6bSjp 	(void) memset(from, 0, sizeof (idmap_info));
20818e228215Sdm }
20828e228215Sdm 
20838e228215Sdm 
208448258c6bSjp void
idmap_info_free(idmap_info * info)208548258c6bSjp idmap_info_free(idmap_info *info)
208648258c6bSjp {
208748258c6bSjp 	if (info == NULL)
208848258c6bSjp 		return;
208948258c6bSjp 
2090148c5f43SAlan Wright 	xdr_free(xdr_idmap_info, (caddr_t)info);
2091148c5f43SAlan Wright 	(void) memset(info, 0, sizeof (idmap_info));
2092148c5f43SAlan Wright }
209348258c6bSjp 
209448258c6bSjp 
2095148c5f43SAlan Wright void
idmap_how_clear(idmap_how * how)2096148c5f43SAlan Wright idmap_how_clear(idmap_how *how)
2097148c5f43SAlan Wright {
2098148c5f43SAlan Wright 	xdr_free(xdr_idmap_how, (caddr_t)how);
2099148c5f43SAlan Wright 	(void) memset(how, 0, sizeof (*how));
210048258c6bSjp }
210148258c6bSjp 
210248258c6bSjp 
2103dd5829d1Sbaban /*
2104dd5829d1Sbaban  * Get uid given Windows name
2105dd5829d1Sbaban  */
2106dd5829d1Sbaban idmap_stat
idmap_getuidbywinname(const char * name,const char * domain,int flag,uid_t * uid)21073ee87bcaSJulian Pullen idmap_getuidbywinname(const char *name, const char *domain, int flag,
2108*a17ce845SMarcel Telka     uid_t *uid)
2109cd37da74Snw {
2110dd5829d1Sbaban 	idmap_retcode	rc;
2111cd37da74Snw 	int		is_user = 1;
2112cd37da74Snw 	int		is_wuser = -1;
21133ee87bcaSJulian Pullen 	int 		direction;
2114dd5829d1Sbaban 
2115dd5829d1Sbaban 	if (uid == NULL)
2116dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2117dd5829d1Sbaban 
21183ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21193ee87bcaSJulian Pullen 		rc = idmap_cache_lookup_uidbywinname(name, domain, uid);
21203ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
21213ee87bcaSJulian Pullen 			return (rc);
21223ee87bcaSJulian Pullen 	}
2123dd5829d1Sbaban 	/* Get mapping */
21241fdeec65Sjoyce mcintosh 	rc = idmap_get_w2u_mapping(NULL, NULL, name, domain, flag,
21253ee87bcaSJulian Pullen 	    &is_user, &is_wuser, uid, NULL, &direction, NULL);
2126dd5829d1Sbaban 
21273ee87bcaSJulian Pullen 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
21283ee87bcaSJulian Pullen 		/* If we have not got the domain don't store UID to winname */
21293ee87bcaSJulian Pullen 		if (domain == NULL)
21303ee87bcaSJulian Pullen 			direction = IDMAP_DIRECTION_W2U;
21313ee87bcaSJulian Pullen 		idmap_cache_add_winname2uid(name, domain, *uid, direction);
21323ee87bcaSJulian Pullen 	}
21333ee87bcaSJulian Pullen 
2134dd5829d1Sbaban 	return (rc);
2135dd5829d1Sbaban }
2136dd5829d1Sbaban 
2137dd5829d1Sbaban 
2138dd5829d1Sbaban /*
2139dd5829d1Sbaban  * Get gid given Windows name
2140dd5829d1Sbaban  */
2141dd5829d1Sbaban idmap_stat
idmap_getgidbywinname(const char * name,const char * domain,int flag,gid_t * gid)21423ee87bcaSJulian Pullen idmap_getgidbywinname(const char *name, const char *domain, int flag,
2143*a17ce845SMarcel Telka     gid_t *gid)
2144cd37da74Snw {
2145dd5829d1Sbaban 	idmap_retcode	rc;
2146cd37da74Snw 	int		is_user = 0;
2147cd37da74Snw 	int		is_wuser = -1;
21483ee87bcaSJulian Pullen 	int		direction;
2149dd5829d1Sbaban 
2150dd5829d1Sbaban 	if (gid == NULL)
2151dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2152dd5829d1Sbaban 
21533ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21543ee87bcaSJulian Pullen 		rc = idmap_cache_lookup_gidbywinname(name, domain, gid);
21553ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
21563ee87bcaSJulian Pullen 			return (rc);
21573ee87bcaSJulian Pullen 	}
21583ee87bcaSJulian Pullen 
2159dd5829d1Sbaban 	/* Get mapping */
21601fdeec65Sjoyce mcintosh 	rc = idmap_get_w2u_mapping(NULL, NULL, name, domain, flag,
21613ee87bcaSJulian Pullen 	    &is_user, &is_wuser, gid, NULL, &direction, NULL);
2162dd5829d1Sbaban 
21633ee87bcaSJulian Pullen 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
21643ee87bcaSJulian Pullen 		/* If we have not got the domain don't store GID to winname */
21653ee87bcaSJulian Pullen 		if (domain == NULL)
21663ee87bcaSJulian Pullen 			direction = IDMAP_DIRECTION_W2U;
21673ee87bcaSJulian Pullen 		idmap_cache_add_winname2gid(name, domain, *gid, direction);
21683ee87bcaSJulian Pullen 	}
21693ee87bcaSJulian Pullen 
2170dd5829d1Sbaban 	return (rc);
2171dd5829d1Sbaban }
2172dd5829d1Sbaban 
2173dd5829d1Sbaban 
2174dd5829d1Sbaban /*
2175dd5829d1Sbaban  * Get winname given pid
2176dd5829d1Sbaban  */
21771ed6b69aSGordon Ross idmap_stat
idmap_getwinnamebypid(uid_t pid,int is_user,int flag,char ** name,char ** domain)21783ee87bcaSJulian Pullen idmap_getwinnamebypid(uid_t pid, int is_user, int flag, char **name,
2179*a17ce845SMarcel Telka     char **domain)
2180cd37da74Snw {
2181dd5829d1Sbaban 	idmap_retcode	rc;
2182dd5829d1Sbaban 	int		len;
2183dd5829d1Sbaban 	char		*winname, *windomain;
21843ee87bcaSJulian Pullen 	int		direction;
2185dd5829d1Sbaban 
2186dd5829d1Sbaban 	if (name == NULL)
2187dd5829d1Sbaban 		return (IDMAP_ERR_ARG);
2188dd5829d1Sbaban 
21893ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
21903ee87bcaSJulian Pullen 		if (is_user)
21913ee87bcaSJulian Pullen 			rc = idmap_cache_lookup_winnamebyuid(&winname,
21923ee87bcaSJulian Pullen 			    &windomain, pid);
21933ee87bcaSJulian Pullen 		else
21943ee87bcaSJulian Pullen 			rc = idmap_cache_lookup_winnamebygid(&winname,
21953ee87bcaSJulian Pullen 			    &windomain, pid);
21963ee87bcaSJulian Pullen 		if (rc == IDMAP_SUCCESS)
21973ee87bcaSJulian Pullen 			goto out;
21983ee87bcaSJulian Pullen 		if (rc == IDMAP_ERR_MEMORY)
21993ee87bcaSJulian Pullen 			return (rc);
22003ee87bcaSJulian Pullen 	}
22013ee87bcaSJulian Pullen 
2202dd5829d1Sbaban 	/* Get mapping */
22031fdeec65Sjoyce mcintosh 	rc = idmap_get_u2w_mapping(&pid, NULL, flag, is_user, NULL,
22043ee87bcaSJulian Pullen 	    NULL, NULL, &winname, &windomain, &direction, NULL);
2205dd5829d1Sbaban 
2206dd5829d1Sbaban 	/* Return on error */
2207dd5829d1Sbaban 	if (rc != IDMAP_SUCCESS)
2208dd5829d1Sbaban 		return (rc);
2209dd5829d1Sbaban 
2210dd5829d1Sbaban 	/*
2211dd5829d1Sbaban 	 * The given PID may have been mapped to a locally
2212dd5829d1Sbaban 	 * generated SID in which case there isn't any
2213dd5829d1Sbaban 	 * Windows name
2214dd5829d1Sbaban 	 */
22158ce3a038SMarcel Telka 	if (winname == NULL) {
2216dd5829d1Sbaban 		idmap_free(windomain);
2217dd5829d1Sbaban 		return (IDMAP_ERR_NORESULT);
2218dd5829d1Sbaban 	}
2219dd5829d1Sbaban 
22203ee87bcaSJulian Pullen 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
22213ee87bcaSJulian Pullen 		if (is_user)
22223ee87bcaSJulian Pullen 			idmap_cache_add_winname2uid(winname, windomain,
22233ee87bcaSJulian Pullen 			    pid, direction);
22243ee87bcaSJulian Pullen 		else
22253ee87bcaSJulian Pullen 			idmap_cache_add_winname2gid(winname, windomain,
22263ee87bcaSJulian Pullen 			    pid, direction);
22273ee87bcaSJulian Pullen 	}
22283ee87bcaSJulian Pullen 
22293ee87bcaSJulian Pullen out:
2230dd5829d1Sbaban 	if (domain != NULL) {
2231dd5829d1Sbaban 		*name = winname;
2232dd5829d1Sbaban 		*domain = windomain;
2233dd5829d1Sbaban 	} else {
22348ce3a038SMarcel Telka 		char *wd = windomain != NULL ? windomain : "";
22358ce3a038SMarcel Telka 		len = snprintf(NULL, 0, "%s@%s", winname, wd) + 1;
2236dd5829d1Sbaban 		if ((*name = malloc(len)) != NULL)
22378ce3a038SMarcel Telka 			(void) snprintf(*name, len, "%s@%s", winname, wd);
2238dd5829d1Sbaban 		else
2239dd5829d1Sbaban 			rc = IDMAP_ERR_MEMORY;
2240dd5829d1Sbaban 		idmap_free(winname);
2241dd5829d1Sbaban 		idmap_free(windomain);
2242dd5829d1Sbaban 	}
22433ee87bcaSJulian Pullen 
2244dd5829d1Sbaban 	return (rc);
2245dd5829d1Sbaban }
2246dd5829d1Sbaban 
2247dd5829d1Sbaban 
2248dd5829d1Sbaban /*
2249dd5829d1Sbaban  * Get winname given uid
2250dd5829d1Sbaban  */
2251dd5829d1Sbaban idmap_stat
idmap_getwinnamebyuid(uid_t uid,int flag,char ** name,char ** domain)22523ee87bcaSJulian Pullen idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
2253cd37da74Snw {
22543ee87bcaSJulian Pullen 	return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
2255dd5829d1Sbaban }
2256dd5829d1Sbaban 
2257dd5829d1Sbaban 
2258dd5829d1Sbaban /*
2259dd5829d1Sbaban  * Get winname given gid
2260dd5829d1Sbaban  */
2261dd5829d1Sbaban idmap_stat
idmap_getwinnamebygid(gid_t gid,int flag,char ** name,char ** domain)22623ee87bcaSJulian Pullen idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
2263cd37da74Snw {
22643ee87bcaSJulian Pullen 	return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
2265dd5829d1Sbaban }
22669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States idmap_stat
idmap_flush(idmap_flush_op op)22681fdeec65Sjoyce mcintosh idmap_flush(idmap_flush_op op)
22699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
22701fdeec65Sjoyce mcintosh 	idmap_retcode		rc1, rc2;
22719fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22721fdeec65Sjoyce mcintosh 	rc1 = _idmap_clnt_call(IDMAP_FLUSH,
22739fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    (xdrproc_t)xdr_idmap_flush_op, (caddr_t)&op,
22741fdeec65Sjoyce mcintosh 	    (xdrproc_t)xdr_idmap_retcode, (caddr_t)&rc2, TIMEOUT);
22759fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
22761fdeec65Sjoyce mcintosh 	if (rc1 != IDMAP_SUCCESS)
22771fdeec65Sjoyce mcintosh 		return (rc1);
22781fdeec65Sjoyce mcintosh 	return (rc2);
22799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
2280c5866007SKeyur Desai 
2281c5866007SKeyur Desai 
2282c5866007SKeyur Desai /*
2283c5866007SKeyur Desai  * syslog is the default logger.
2284c5866007SKeyur Desai  * It can be overwritten by supplying a logger
2285c5866007SKeyur Desai  * with  idmap_set_logger()
2286c5866007SKeyur Desai  */
2287c5866007SKeyur Desai idmap_logger_t logger = syslog;
2288c5866007SKeyur Desai 
2289c5866007SKeyur Desai 
2290c5866007SKeyur Desai void
idmap_set_logger(idmap_logger_t funct)2291c5866007SKeyur Desai idmap_set_logger(idmap_logger_t funct)
2292c5866007SKeyur Desai {
2293c5866007SKeyur Desai 	logger = funct;
2294c5866007SKeyur Desai }
2295148c5f43SAlan Wright 
2296148c5f43SAlan Wright /*
2297148c5f43SAlan Wright  * Helper functions that concatenate two parts of a name and then
2298148c5f43SAlan Wright  * look up a value, so that the same set of functions can be used to
2299148c5f43SAlan Wright  * process both "in" and "out" parameters.
2300148c5f43SAlan Wright  */
2301148c5f43SAlan Wright static
2302148c5f43SAlan Wright boolean_t
idmap_trace_get_str(nvlist_t * entry,char * n1,char * n2,char ** ret)2303148c5f43SAlan Wright idmap_trace_get_str(nvlist_t *entry, char *n1, char *n2, char **ret)
2304148c5f43SAlan Wright {
2305148c5f43SAlan Wright 	char name[IDMAP_TRACE_NAME_MAX+1];	/* Max used is about 11 */
2306148c5f43SAlan Wright 	int err;
2307148c5f43SAlan Wright 
2308148c5f43SAlan Wright 	(void) strlcpy(name, n1, sizeof (name));
2309148c5f43SAlan Wright 	if (n2 != NULL)
2310148c5f43SAlan Wright 		(void) strlcat(name, n2, sizeof (name));
2311148c5f43SAlan Wright 
2312148c5f43SAlan Wright 	err = nvlist_lookup_string(entry, name, ret);
2313148c5f43SAlan Wright 	return (err == 0);
2314148c5f43SAlan Wright }
2315148c5f43SAlan Wright 
2316148c5f43SAlan Wright static
2317148c5f43SAlan Wright boolean_t
idmap_trace_get_int(nvlist_t * entry,char * n1,char * n2,int64_t * ret)2318148c5f43SAlan Wright idmap_trace_get_int(nvlist_t *entry, char *n1, char *n2, int64_t *ret)
2319148c5f43SAlan Wright {
2320148c5f43SAlan Wright 	char name[IDMAP_TRACE_NAME_MAX+1];	/* Max used is about 11 */
2321148c5f43SAlan Wright 	int err;
2322148c5f43SAlan Wright 
2323148c5f43SAlan Wright 	(void) strlcpy(name, n1, sizeof (name));
2324148c5f43SAlan Wright 	if (n2 != NULL)
2325148c5f43SAlan Wright 		(void) strlcat(name, n2, sizeof (name));
2326148c5f43SAlan Wright 
2327148c5f43SAlan Wright 	err = nvlist_lookup_int64(entry, name, ret);
2328148c5f43SAlan Wright 	return (err == 0);
2329148c5f43SAlan Wright }
2330148c5f43SAlan Wright 
2331148c5f43SAlan Wright static
2332148c5f43SAlan Wright void
idmap_trace_print_id(FILE * out,nvlist_t * entry,char * fromto)2333148c5f43SAlan Wright idmap_trace_print_id(FILE *out, nvlist_t *entry, char *fromto)
2334148c5f43SAlan Wright {
2335148c5f43SAlan Wright 	char *s;
2336148c5f43SAlan Wright 	int64_t i64;
2337148c5f43SAlan Wright 
2338148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, fromto, IDMAP_TRACE_TYPE, &i64)) {
2339148c5f43SAlan Wright 		switch (i64) {
2340148c5f43SAlan Wright 		case IDMAP_POSIXID:
2341148c5f43SAlan Wright 			(void) fprintf(out, "unixname ");
2342148c5f43SAlan Wright 			break;
2343148c5f43SAlan Wright 		case IDMAP_UID:
2344148c5f43SAlan Wright 			(void) fprintf(out, "unixuser ");
2345148c5f43SAlan Wright 			break;
2346148c5f43SAlan Wright 		case IDMAP_GID:
2347148c5f43SAlan Wright 			(void) fprintf(out, "unixgroup ");
2348148c5f43SAlan Wright 			break;
2349148c5f43SAlan Wright 		case IDMAP_SID:
2350148c5f43SAlan Wright 			(void) fprintf(out, "winname ");
2351148c5f43SAlan Wright 			break;
2352148c5f43SAlan Wright 		case IDMAP_USID:
2353148c5f43SAlan Wright 			(void) fprintf(out, "winuser ");
2354148c5f43SAlan Wright 			break;
2355148c5f43SAlan Wright 		case IDMAP_GSID:
2356148c5f43SAlan Wright 			(void) fprintf(out, "wingroup ");
2357148c5f43SAlan Wright 			break;
2358148c5f43SAlan Wright 		case IDMAP_NONE:
2359148c5f43SAlan Wright 			(void) fprintf(out, gettext("unknown "));
2360148c5f43SAlan Wright 			break;
2361148c5f43SAlan Wright 		default:
2362148c5f43SAlan Wright 			(void) fprintf(out, gettext("bad %d "), (int)i64);
2363148c5f43SAlan Wright 			break;
2364148c5f43SAlan Wright 		}
2365148c5f43SAlan Wright 	}
2366148c5f43SAlan Wright 
2367148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, fromto, IDMAP_TRACE_NAME, &s))
2368148c5f43SAlan Wright 		(void) fprintf(out, "%s ", s);
2369148c5f43SAlan Wright 
2370148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, fromto, IDMAP_TRACE_SID, &s))
2371148c5f43SAlan Wright 		(void) fprintf(out, "%s ", s);
2372148c5f43SAlan Wright 
2373148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, fromto, IDMAP_TRACE_UNIXID, &i64))
2374148c5f43SAlan Wright 		(void) fprintf(out, "%u ", (uid_t)i64);
2375148c5f43SAlan Wright }
2376148c5f43SAlan Wright 
2377148c5f43SAlan Wright void
idmap_trace_print_1(FILE * out,char * prefix,nvlist_t * entry)2378148c5f43SAlan Wright idmap_trace_print_1(FILE *out, char *prefix, nvlist_t *entry)
2379148c5f43SAlan Wright {
2380148c5f43SAlan Wright 	char *s;
2381148c5f43SAlan Wright 	int64_t i64;
2382148c5f43SAlan Wright 
2383148c5f43SAlan Wright 	(void) fprintf(out, "%s", prefix);
2384148c5f43SAlan Wright 	idmap_trace_print_id(out, entry, "from");
2385148c5f43SAlan Wright 	(void) fprintf(out, "-> ");
2386148c5f43SAlan Wright 	idmap_trace_print_id(out, entry, "to");
2387148c5f43SAlan Wright 	if (idmap_trace_get_int(entry, IDMAP_TRACE_ERROR, NULL, &i64))
2388148c5f43SAlan Wright 		(void) fprintf(out, gettext("Error %d "), (int)i64);
2389148c5f43SAlan Wright 	(void) fprintf(out, "-");
2390148c5f43SAlan Wright 	if (idmap_trace_get_str(entry, IDMAP_TRACE_MESSAGE, NULL, &s))
2391148c5f43SAlan Wright 		(void) fprintf(out, " %s", s);
2392148c5f43SAlan Wright 	(void) fprintf(out, "\n");
2393148c5f43SAlan Wright }
2394148c5f43SAlan Wright 
2395148c5f43SAlan Wright void
idmap_trace_print(FILE * out,char * prefix,nvlist_t * trace)2396148c5f43SAlan Wright idmap_trace_print(FILE *out, char *prefix, nvlist_t *trace)
2397148c5f43SAlan Wright {
2398148c5f43SAlan Wright 	nvpair_t *nvp;
2399148c5f43SAlan Wright 
2400148c5f43SAlan Wright 	for (nvp = nvlist_next_nvpair(trace, NULL);
2401148c5f43SAlan Wright 	    nvp != NULL;
2402148c5f43SAlan Wright 	    nvp = nvlist_next_nvpair(trace, nvp)) {
2403148c5f43SAlan Wright 		nvlist_t *entry;
2404148c5f43SAlan Wright 		int err;
2405148c5f43SAlan Wright 
2406148c5f43SAlan Wright 		err = nvpair_value_nvlist(nvp, &entry);
2407148c5f43SAlan Wright 		assert(err == 0);
2408148c5f43SAlan Wright 
2409148c5f43SAlan Wright 		idmap_trace_print_1(out, prefix, entry);
2410148c5f43SAlan Wright 	}
2411148c5f43SAlan Wright }
2412