14520Snw141292 /*
24520Snw141292  * CDDL HEADER START
34520Snw141292  *
44520Snw141292  * The contents of this file are subject to the terms of the
54520Snw141292  * Common Development and Distribution License (the "License").
64520Snw141292  * You may not use this file except in compliance with the License.
74520Snw141292  *
84520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292  * or http://www.opensolaris.org/os/licensing.
104520Snw141292  * See the License for the specific language governing permissions
114520Snw141292  * and limitations under the License.
124520Snw141292  *
134520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292  *
194520Snw141292  * CDDL HEADER END
204520Snw141292  */
214520Snw141292 /*
225968Snw141292  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234520Snw141292  * Use is subject to license terms.
244520Snw141292  */
254520Snw141292 
264520Snw141292 
274520Snw141292 /*
284520Snw141292  * libidmap API
294520Snw141292  */
304520Snw141292 
314520Snw141292 #include <stdlib.h>
326616Sdm199847 #include <sys/varargs.h>
334520Snw141292 #include <inttypes.h>
344520Snw141292 #include <errno.h>
354520Snw141292 #include <strings.h>
364520Snw141292 #include <ctype.h>
374520Snw141292 #include <sys/param.h>
384520Snw141292 #include <sys/types.h>
394520Snw141292 #include <sys/stat.h>
404520Snw141292 #include <dlfcn.h>
414520Snw141292 #include <libintl.h>
425232Snw141292 #include <ucontext.h>
434520Snw141292 #include "idmap_impl.h"
447369SJulian.Pullen@Sun.COM #include "idmap_cache.h"
454520Snw141292 
466616Sdm199847 /*LINTLIBRARY*/
476616Sdm199847 
486616Sdm199847 /*
496616Sdm199847  * The following structure determines where the log messages from idmapdlog()
506616Sdm199847  * go to. It can be stderr (idmap, idmapd -d) and/or syslog (idmapd).
516616Sdm199847  *
526616Sdm199847  * logstate.max_pri is integer cutoff necessary to silence low-priority
536616Sdm199847  * messages to stderr. Syslog has its own means so there a boolean
546616Sdm199847  * logstate.write_syslog is enough.
556616Sdm199847  *
566616Sdm199847  * logstate.degraded is a mode used by idmapd in its degraded state.
576616Sdm199847  */
586616Sdm199847 
596616Sdm199847 static struct {
606616Sdm199847 	bool_t write_syslog;
616616Sdm199847 	int max_pri; /* Max priority written to stderr */
626616Sdm199847 	bool_t degraded;
636616Sdm199847 } logstate = {FALSE, LOG_DEBUG, FALSE};
646616Sdm199847 
656616Sdm199847 
664520Snw141292 static struct timeval TIMEOUT = { 25, 0 };
674520Snw141292 
684520Snw141292 static int idmap_stat2errno(idmap_stat);
696616Sdm199847 static idmap_stat	idmap_strdupnull(char **, const char *);
704520Snw141292 
714520Snw141292 #define	__ITER_CREATE(itera, argu, handl, ityp)\
724520Snw141292 	if (handl == NULL) {\
734520Snw141292 		errno = EINVAL;\
744520Snw141292 		return (IDMAP_ERR_ARG);\
754520Snw141292 	}\
764520Snw141292 	itera = calloc(1, sizeof (*itera));\
774520Snw141292 	if (itera == NULL) {\
784520Snw141292 		errno = ENOMEM;\
794520Snw141292 		return (IDMAP_ERR_MEMORY);\
804520Snw141292 	}\
814520Snw141292 	argu = calloc(1, sizeof (*argu));\
824520Snw141292 	if (argu == NULL) {\
834520Snw141292 		free(itera);\
844520Snw141292 		errno = ENOMEM;\
854520Snw141292 		return (IDMAP_ERR_MEMORY);\
864520Snw141292 	}\
874520Snw141292 	itera->ih = handl;\
884520Snw141292 	itera->type = ityp;\
894520Snw141292 	itera->retcode = IDMAP_NEXT;\
904520Snw141292 	itera->limit = 1024;\
914520Snw141292 	itera->arg = argu;
924520Snw141292 
934520Snw141292 
944520Snw141292 #define	__ITER_ERR_RETURN(itera, argu, xdr_argu, iretcod)\
954520Snw141292 	if (argu) {\
964520Snw141292 		xdr_free(xdr_argu, (caddr_t)argu);\
974520Snw141292 		free(argu);\
984520Snw141292 	}\
994520Snw141292 	if (itera)\
1004520Snw141292 		free(itera);\
1014520Snw141292 	return (iretcod);
1024520Snw141292 
1034520Snw141292 
1044520Snw141292 #define	__ITER_CHECK(itera, ityp)\
1054520Snw141292 	if (itera == NULL) {\
1064520Snw141292 		errno = EINVAL;\
1074520Snw141292 		return (IDMAP_ERR_ARG);\
1084520Snw141292 	}\
1094520Snw141292 	if (itera->type != ityp) {\
1104520Snw141292 		errno = EINVAL;\
1114520Snw141292 		return (IDMAP_ERR_ARG);\
1124520Snw141292 	}
1134520Snw141292 
1144520Snw141292 /*
1154520Snw141292  * Free memory allocated by libidmap API
1164520Snw141292  *
1174520Snw141292  * Input:
1184520Snw141292  * ptr - memory to be freed
1194520Snw141292  */
1204520Snw141292 void
1215696Snw141292 idmap_free(void *ptr)
1225696Snw141292 {
1234520Snw141292 	free(ptr);
1244520Snw141292 }
1254520Snw141292 
1264520Snw141292 
127*8040SBaban.Kenkre@Sun.COM #define	MIN_STACK_NEEDS	65536
1285232Snw141292 
1294520Snw141292 /*
1304520Snw141292  * Create and Initialize idmap client handle for rpc/doors
1314520Snw141292  *
1324520Snw141292  * Output:
1334520Snw141292  * handle - idmap handle
1344520Snw141292  */
1354520Snw141292 idmap_stat
1365696Snw141292 idmap_init(idmap_handle_t **handle)
1375696Snw141292 {
1384520Snw141292 	CLIENT			*clnt = NULL;
1394520Snw141292 	struct idmap_handle	*hptr;
1405232Snw141292 	uint_t			sendsz = 0;
1415232Snw141292 	stack_t			st;
1424520Snw141292 
1434520Snw141292 	*handle = NULL;
1444520Snw141292 	hptr = (struct idmap_handle *)calloc(1, sizeof (*hptr));
1454520Snw141292 	if (hptr == NULL)
1464520Snw141292 		return (IDMAP_ERR_MEMORY);
1474520Snw141292 
1485232Snw141292 	/*
1495232Snw141292 	 * clnt_door_call() alloca()s sendsz bytes (twice too, once for
1505232Snw141292 	 * the call args buffer and once for the call result buffer), so
1515232Snw141292 	 * we want to pick a sendsz that will be large enough, but not
1525232Snw141292 	 * too large.
1535232Snw141292 	 */
1545232Snw141292 	if (stack_getbounds(&st) == 0) {
1555232Snw141292 		/*
1565232Snw141292 		 * Estimate how much stack space is left;
1575232Snw141292 		 * st.ss_sp is the top of stack.
1585232Snw141292 		 */
1595232Snw141292 		if ((char *)&sendsz < (char *)st.ss_sp)
1605232Snw141292 			/* stack grows up */
1615232Snw141292 			sendsz = ((char *)st.ss_sp - (char *)&sendsz);
1625232Snw141292 		else
1635232Snw141292 			/* stack grows down */
1645232Snw141292 			sendsz = ((char *)&sendsz - (char *)st.ss_sp);
1655232Snw141292 
166*8040SBaban.Kenkre@Sun.COM 		if (sendsz <= MIN_STACK_NEEDS) {
1675232Snw141292 			sendsz = 0;	/* RPC call may fail */
168*8040SBaban.Kenkre@Sun.COM 		} else {
169*8040SBaban.Kenkre@Sun.COM 			/* Leave 64Kb (just a guess) for our needs */
170*8040SBaban.Kenkre@Sun.COM 			sendsz -= MIN_STACK_NEEDS;
171*8040SBaban.Kenkre@Sun.COM 
172*8040SBaban.Kenkre@Sun.COM 			/* Divide the stack space left by two */
173*8040SBaban.Kenkre@Sun.COM 			sendsz = RNDUP(sendsz / 2);
174*8040SBaban.Kenkre@Sun.COM 
175*8040SBaban.Kenkre@Sun.COM 			/* Limit sendsz to 256KB */
176*8040SBaban.Kenkre@Sun.COM 			if (sendsz > IDMAP_MAX_DOOR_RPC)
177*8040SBaban.Kenkre@Sun.COM 				sendsz = IDMAP_MAX_DOOR_RPC;
178*8040SBaban.Kenkre@Sun.COM 		}
1795232Snw141292 	}
1805232Snw141292 
1815232Snw141292 	clnt = clnt_door_create(IDMAP_PROG, IDMAP_V1, sendsz);
1824520Snw141292 	if (clnt == NULL) {
1834520Snw141292 		free(hptr);
1844520Snw141292 		return (IDMAP_ERR_RPC);
1854520Snw141292 	}
1864520Snw141292 	hptr->type = _IDMAP_HANDLE_RPC_DOORS;
1874520Snw141292 	hptr->privhandle = clnt;
1884520Snw141292 	*handle = hptr;
1894520Snw141292 	return (IDMAP_SUCCESS);
1904520Snw141292 }
1914520Snw141292 
1924520Snw141292 
1934520Snw141292 /*
1944520Snw141292  * Finalize idmap handle
1954520Snw141292  *
1964520Snw141292  * Input:
1974520Snw141292  * handle - idmap handle
1984520Snw141292  */
1994520Snw141292 idmap_stat
2005696Snw141292 idmap_fini(idmap_handle_t *handle)
2015696Snw141292 {
2024520Snw141292 	CLIENT			*clnt;
2034520Snw141292 	struct idmap_handle	*hptr;
2044520Snw141292 
2054520Snw141292 	if (handle == NULL)
2064520Snw141292 		return (IDMAP_SUCCESS);
2074520Snw141292 
2084520Snw141292 	hptr = (struct idmap_handle *)handle;
2094520Snw141292 
2104520Snw141292 	switch (hptr->type) {
2114520Snw141292 	case _IDMAP_HANDLE_RPC_DOORS:
2124520Snw141292 		clnt = (CLIENT *)hptr->privhandle;
2134520Snw141292 		if (clnt) {
2144520Snw141292 			if (clnt->cl_auth)
2154520Snw141292 				auth_destroy(clnt->cl_auth);
2164520Snw141292 			clnt_destroy(clnt);
2174520Snw141292 		}
2184520Snw141292 		break;
2194520Snw141292 	default:
2204520Snw141292 		break;
2214520Snw141292 	}
2224520Snw141292 	free(hptr);
2234520Snw141292 	return (IDMAP_SUCCESS);
2244520Snw141292 }
2254520Snw141292 
2264520Snw141292 
2276616Sdm199847 idmap_stat
2286616Sdm199847 idmap_get_prop(idmap_handle_t *handle, idmap_prop_type pr, idmap_prop_res *res)
2296616Sdm199847 {
2306616Sdm199847 	CLIENT			*clnt;
2316616Sdm199847 	enum clnt_stat		clntstat;
2326616Sdm199847 
2336616Sdm199847 
2346616Sdm199847 	(void) memset(res, 0, sizeof (*res));
2356616Sdm199847 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
2366616Sdm199847 
2376616Sdm199847 	clntstat = clnt_call(clnt, IDMAP_GET_PROP,
2386616Sdm199847 	    (xdrproc_t)xdr_idmap_prop_type, (caddr_t)&pr,
2396616Sdm199847 	    (xdrproc_t)xdr_idmap_prop_res, (caddr_t)res, TIMEOUT);
2406616Sdm199847 
2416616Sdm199847 	if (clntstat != RPC_SUCCESS) {
2426616Sdm199847 		return (_idmap_rpc2stat(clnt));
2436616Sdm199847 	}
2446616Sdm199847 
2456616Sdm199847 	return (res->retcode); /* This might not be IDMAP_SUCCESS! */
2466616Sdm199847 
2476616Sdm199847 #if 0
2486616Sdm199847 	(void) memset(&res, 0, sizeof (res));
2496616Sdm199847 	pr = PROP_DOMAIN_CONTROLLER;
2506616Sdm199847 
2516616Sdm199847 	clntstat = clnt_call(clnt, IDMAP_GET_PROP,
2526616Sdm199847 	    (xdrproc_t)xdr_idmap_prop_type, (caddr_t)&pr,
2536616Sdm199847 	    (xdrproc_t)xdr_idmap_prop_res, (caddr_t)&res, TIMEOUT);
2546616Sdm199847 
2556616Sdm199847 	if (clntstat != RPC_SUCCESS) {
2566616Sdm199847 		fprintf(stderr, "clntstat != RPC_SUCCESS\n");
2576616Sdm199847 		rc = _idmap_rpc2stat(clnt);
2586616Sdm199847 		goto cleanup;
2596616Sdm199847 	}
2606616Sdm199847 #endif
2616616Sdm199847 
2626616Sdm199847 }
2636616Sdm199847 
2646616Sdm199847 idmap_stat
2656616Sdm199847 idmap_get_prop_ds(idmap_handle_t *handle, idmap_prop_type pr,
2666616Sdm199847     idmap_ad_disc_ds_t *dc)
2676616Sdm199847 {
2686616Sdm199847 	idmap_prop_res res;
2696616Sdm199847 	idmap_stat rc = IDMAP_SUCCESS;
2706616Sdm199847 
2716616Sdm199847 	rc = idmap_get_prop(handle, pr, &res);
2726616Sdm199847 	if (rc < 0)
2736616Sdm199847 		return (rc);
2746616Sdm199847 
2756616Sdm199847 	dc->port = res.value.idmap_prop_val_u.dsval.port;
2766616Sdm199847 	(void) strlcpy(dc->host, res.value.idmap_prop_val_u.dsval.host,
2776616Sdm199847 	    AD_DISC_MAXHOSTNAME);
2786616Sdm199847 
2796616Sdm199847 	/* xdr doesn't guarantee 0-termination of char[]: */
2806616Sdm199847 	dc->host[AD_DISC_MAXHOSTNAME - 1] = '\0';
2816616Sdm199847 
2826616Sdm199847 	return (rc);
2836616Sdm199847 }
2846616Sdm199847 
2856616Sdm199847 
2866616Sdm199847 /*
2876616Sdm199847  * Sometimes the property is not set. In that case, str is set to NULL but
2886616Sdm199847  * otherwise IDMAP_SUCCESS is returned.
2896616Sdm199847  */
2906616Sdm199847 idmap_stat
2916616Sdm199847 idmap_get_prop_str(idmap_handle_t *handle, idmap_prop_type pr, char **str)
2926616Sdm199847 {
2936616Sdm199847 	idmap_prop_res res;
2946616Sdm199847 	idmap_stat rc = IDMAP_SUCCESS;
2956616Sdm199847 
2966616Sdm199847 	rc = idmap_get_prop(handle, pr, &res);
2976616Sdm199847 	if (rc < 0)
2986616Sdm199847 		return (rc);
2996616Sdm199847 
3006616Sdm199847 	rc = idmap_strdupnull(str, res.value.idmap_prop_val_u.utf8val);
3016616Sdm199847 	return (rc);
3026616Sdm199847 }
3034520Snw141292 
3044520Snw141292 /*
3054520Snw141292  * Create/Initialize handle for updates
3064520Snw141292  *
3074520Snw141292  * Output:
3084520Snw141292  * udthandle - update handle
3094520Snw141292  */
3104520Snw141292 idmap_stat
3115696Snw141292 idmap_udt_create(idmap_handle_t *handle, idmap_udt_handle_t **udthandle)
3125696Snw141292 {
3134520Snw141292 	idmap_udt_handle_t	*tmp;
3144520Snw141292 
3154520Snw141292 	if (handle == NULL || udthandle == NULL) {
3164520Snw141292 		errno = EINVAL;
3174520Snw141292 		return (IDMAP_ERR_ARG);
3184520Snw141292 	}
3194520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
3204520Snw141292 		errno = ENOMEM;
3214520Snw141292 		return (IDMAP_ERR_MEMORY);
3224520Snw141292 	}
3234520Snw141292 
3244520Snw141292 	tmp->ih = handle;
3254520Snw141292 	*udthandle = tmp;
3264520Snw141292 	return (IDMAP_SUCCESS);
3274520Snw141292 }
3284520Snw141292 
3294520Snw141292 
3304520Snw141292 /*
3314520Snw141292  * All the updates specified by the update handle are committed
3324520Snw141292  * in a single transaction. i.e either all succeed or none.
3334520Snw141292  *
3344520Snw141292  * Input:
3354520Snw141292  * udthandle - update handle with the update requests
3364520Snw141292  *
3374520Snw141292  * Return value:
3384520Snw141292  * Status of the commit
3394520Snw141292  */
3404520Snw141292 idmap_stat
3415696Snw141292 idmap_udt_commit(idmap_udt_handle_t *udthandle)
3425696Snw141292 {
3434520Snw141292 	CLIENT			*clnt;
3444520Snw141292 	enum clnt_stat		clntstat;
3455064Sdm199847 	idmap_update_res	res;
3465064Sdm199847 	idmap_stat		retcode;
3474520Snw141292 
3484520Snw141292 	if (udthandle == NULL) {
3494520Snw141292 		errno = EINVAL;
3504520Snw141292 		return (IDMAP_ERR_ARG);
3514520Snw141292 	}
3525064Sdm199847 
3535064Sdm199847 	(void) memset(&res, 0, sizeof (res));
3545064Sdm199847 
3554520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(udthandle->ih, clnt);
3564520Snw141292 	clntstat = clnt_call(clnt, IDMAP_UPDATE,
3575696Snw141292 	    (xdrproc_t)xdr_idmap_update_batch, (caddr_t)&udthandle->batch,
3585696Snw141292 	    (xdrproc_t)xdr_idmap_update_res, (caddr_t)&res,
3595696Snw141292 	    TIMEOUT);
3604644Sbaban 
3615064Sdm199847 	if (clntstat != RPC_SUCCESS) {
3625064Sdm199847 		retcode = _idmap_rpc2stat(clnt);
3635064Sdm199847 		goto out;
3645064Sdm199847 	}
3655064Sdm199847 
3665064Sdm199847 	retcode = udthandle->commit_stat = res.retcode;
3675064Sdm199847 	udthandle->error_index = res.error_index;
3685064Sdm199847 
3695064Sdm199847 	if (retcode != IDMAP_SUCCESS) {
3705064Sdm199847 
3715064Sdm199847 		if (udthandle->error_index < 0)
3725064Sdm199847 			goto out;
3735064Sdm199847 
3745064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->error_rule,
3755064Sdm199847 		    &res.error_rule);
3765064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
3775064Sdm199847 			udthandle->error_index = -2;
3785064Sdm199847 			goto out;
3795064Sdm199847 		}
3805064Sdm199847 
3815064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->conflict_rule,
3825064Sdm199847 		    &res.conflict_rule);
3835064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
3845064Sdm199847 			udthandle->error_index = -2;
3855064Sdm199847 			goto out;
3865064Sdm199847 		}
3875064Sdm199847 	}
3885064Sdm199847 
3895064Sdm199847 	retcode = res.retcode;
3905064Sdm199847 
3915064Sdm199847 
3925064Sdm199847 out:
3934644Sbaban 	/* reset handle so that it can be used again */
3945064Sdm199847 	if (retcode == IDMAP_SUCCESS) {
3955064Sdm199847 		_IDMAP_RESET_UDT_HANDLE(udthandle);
3965064Sdm199847 	}
3975064Sdm199847 
3985064Sdm199847 	(void) xdr_free(xdr_idmap_update_res, (caddr_t)&res);
3995064Sdm199847 	errno = idmap_stat2errno(retcode);
4005064Sdm199847 	return (retcode);
4015064Sdm199847 }
4025064Sdm199847 
4035064Sdm199847 
4045064Sdm199847 static void
4055064Sdm199847 idmap_namerule_parts_clear(char **windomain, char **winname,
4065696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4075696Snw141292     boolean_t *is_nt4, int *direction)
4085696Snw141292 {
4095064Sdm199847 	if (windomain)
4105064Sdm199847 		*windomain = NULL;
4115064Sdm199847 	if (winname)
4125064Sdm199847 		*winname = NULL;
4135064Sdm199847 	if (unixname)
4145064Sdm199847 		*unixname = NULL;
4155064Sdm199847 
4165064Sdm199847 	if (is_nt4)
4175064Sdm199847 		*is_nt4 = 0;
4185064Sdm199847 	if (is_user)
4195064Sdm199847 		*is_user = -1;
4205696Snw141292 	if (is_wuser)
4215696Snw141292 		*is_wuser = -1;
4225064Sdm199847 	if (direction)
4235064Sdm199847 		*direction = IDMAP_DIRECTION_UNDEF;
4245064Sdm199847 }
4255064Sdm199847 
4265064Sdm199847 static idmap_stat
4275696Snw141292 idmap_namerule2parts(idmap_namerule *rule,
4285064Sdm199847     char **windomain, char **winname,
4295696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4305696Snw141292     boolean_t *is_nt4, int *direction)
4315696Snw141292 {
4325064Sdm199847 	idmap_stat retcode;
4335064Sdm199847 
4345064Sdm199847 	if (EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
4355064Sdm199847 		return (IDMAP_ERR_NORESULT);
4365064Sdm199847 
4375064Sdm199847 
4385064Sdm199847 	retcode = idmap_strdupnull(windomain, rule->windomain);
4395064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4405064Sdm199847 		goto errout;
4415064Sdm199847 
4425064Sdm199847 	retcode = idmap_strdupnull(winname, rule->winname);
4435064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4445064Sdm199847 		goto errout;
4455064Sdm199847 
4465064Sdm199847 	retcode = idmap_strdupnull(unixname, rule->unixname);
4475064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4485064Sdm199847 		goto errout;
4495064Sdm199847 
4505064Sdm199847 
4515064Sdm199847 	if (is_user)
4525064Sdm199847 		*is_user = rule->is_user;
4535696Snw141292 	if (is_wuser)
4545696Snw141292 		*is_wuser = rule->is_wuser;
4555064Sdm199847 	if (is_nt4)
4565064Sdm199847 		*is_nt4 = rule->is_nt4;
4575064Sdm199847 	if (direction)
4585064Sdm199847 		*direction = rule->direction;
4595064Sdm199847 
4605064Sdm199847 
4615064Sdm199847 	return (IDMAP_SUCCESS);
4624644Sbaban 
4635064Sdm199847 errout:
4645064Sdm199847 	if (windomain && *windomain)
4655064Sdm199847 		free(*windomain);
4665064Sdm199847 	if (winname && *winname)
4675064Sdm199847 		free(*winname);
4685064Sdm199847 	if (unixname && *unixname)
4695064Sdm199847 		free(*unixname);
4705064Sdm199847 
4715064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
4725696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
4735064Sdm199847 
4744520Snw141292 	return (retcode);
4755064Sdm199847 
4765064Sdm199847 }
4775064Sdm199847 
4785064Sdm199847 /*
4795064Sdm199847  * Retrieve the index of the failed batch element. error_index == -1
4805064Sdm199847  * indicates failure at the beginning, -2 at the end.
4815064Sdm199847  *
4825064Sdm199847  * If idmap_udt_commit didn't return error, the returned value is undefined.
4835064Sdm199847  *
4845064Sdm199847  * Return value:
4855064Sdm199847  * IDMAP_SUCCESS
4865064Sdm199847  */
4875064Sdm199847 
4885064Sdm199847 idmap_stat
4895064Sdm199847 idmap_udt_get_error_index(idmap_udt_handle_t *udthandle,
4905696Snw141292     int64_t *error_index)
4915696Snw141292 {
4925064Sdm199847 	if (error_index)
4935064Sdm199847 		*error_index = udthandle->error_index;
4945064Sdm199847 
4955064Sdm199847 	return (IDMAP_SUCCESS);
4965064Sdm199847 }
4975064Sdm199847 
4985064Sdm199847 
4995064Sdm199847 /*
5005064Sdm199847  * Retrieve the rule which caused the batch to fail. If
5015064Sdm199847  * idmap_udt_commit didn't return error or if error_index is < 0, the
5025064Sdm199847  * retrieved rule is undefined.
5035064Sdm199847  *
5045064Sdm199847  * Return value:
5055064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
5065064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
5075064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
5085064Sdm199847  */
5095064Sdm199847 
5105064Sdm199847 idmap_stat
5115064Sdm199847 idmap_udt_get_error_rule(idmap_udt_handle_t *udthandle,
5125064Sdm199847     char **windomain, char **winname,
5135696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
5145696Snw141292     boolean_t *is_nt4, int *direction)
5155696Snw141292 {
5165064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
5175696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
5185064Sdm199847 
5195064Sdm199847 	if (udthandle->commit_stat == IDMAP_SUCCESS ||
5205064Sdm199847 	    udthandle->error_index < 0)
5215064Sdm199847 		return (IDMAP_ERR_NORESULT);
5225064Sdm199847 
5235064Sdm199847 	return (idmap_namerule2parts(
5245696Snw141292 	    &udthandle->error_rule,
5255696Snw141292 	    windomain,
5265696Snw141292 	    winname,
5275696Snw141292 	    unixname,
5285696Snw141292 	    is_user,
5295696Snw141292 	    is_wuser,
5305696Snw141292 	    is_nt4,
5315696Snw141292 	    direction));
5325064Sdm199847 }
5335064Sdm199847 
5345064Sdm199847 /*
5355064Sdm199847  * Retrieve the rule with which there was a conflict. TODO: retrieve
5365064Sdm199847  * the value.
5375064Sdm199847  *
5385064Sdm199847  * Return value:
5395064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
5405064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
5415064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
5425064Sdm199847  */
5435064Sdm199847 
5445064Sdm199847 idmap_stat
5455064Sdm199847 idmap_udt_get_conflict_rule(idmap_udt_handle_t *udthandle,
5465064Sdm199847     char **windomain, char **winname,
5475696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
5485696Snw141292     boolean_t *is_nt4, int *direction)
5495696Snw141292 {
5505064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
5515696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
5525064Sdm199847 
5535064Sdm199847 	if (udthandle->commit_stat != IDMAP_ERR_W2U_NAMERULE_CONFLICT &&
5545064Sdm199847 	    udthandle->commit_stat != IDMAP_ERR_U2W_NAMERULE_CONFLICT) {
5555696Snw141292 		return (IDMAP_ERR_NORESULT);
5565064Sdm199847 	}
5575064Sdm199847 
5585064Sdm199847 	return (idmap_namerule2parts(
5595696Snw141292 	    &udthandle->conflict_rule,
5605696Snw141292 	    windomain,
5615696Snw141292 	    winname,
5625696Snw141292 	    unixname,
5635696Snw141292 	    is_user,
5645696Snw141292 	    is_wuser,
5655696Snw141292 	    is_nt4,
5665696Snw141292 	    direction));
5674520Snw141292 }
5684520Snw141292 
5694520Snw141292 
5704520Snw141292 /*
5714520Snw141292  * Destroy the update handle
5724520Snw141292  */
5734520Snw141292 void
5745696Snw141292 idmap_udt_destroy(idmap_udt_handle_t *udthandle)
5755696Snw141292 {
5764520Snw141292 	if (udthandle == NULL)
5774520Snw141292 		return;
5784520Snw141292 	(void) xdr_free(xdr_idmap_update_batch, (caddr_t)&udthandle->batch);
5795064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->error_rule);
5805064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->conflict_rule);
5814520Snw141292 	free(udthandle);
5824520Snw141292 }
5834520Snw141292 
5844520Snw141292 
5854520Snw141292 idmap_stat
5864520Snw141292 idmap_udt_add_namerule(idmap_udt_handle_t *udthandle, const char *windomain,
5875696Snw141292     boolean_t is_user, boolean_t is_wuser, const char *winname,
5885696Snw141292     const char *unixname, boolean_t is_nt4, int direction)
5895696Snw141292 {
5904520Snw141292 	idmap_retcode	retcode;
5914644Sbaban 	idmap_namerule	*rule = NULL;
5924520Snw141292 
5934644Sbaban 	retcode = _udt_extend_batch(udthandle);
5944520Snw141292 	if (retcode != IDMAP_SUCCESS)
5954520Snw141292 		goto errout;
5964520Snw141292 
5974520Snw141292 	rule = &udthandle->batch.
5985696Snw141292 	    idmap_update_batch_val[udthandle->next].
5995696Snw141292 	    idmap_update_op_u.rule;
6004520Snw141292 	rule->is_user = is_user;
6015696Snw141292 	rule->is_wuser = is_wuser;
6024520Snw141292 	rule->direction = direction;
6034520Snw141292 	rule->is_nt4 = is_nt4;
6045064Sdm199847 
6055064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
6065064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6075064Sdm199847 		goto errout;
6085064Sdm199847 
6095064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
6105064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6115064Sdm199847 		goto errout;
6125064Sdm199847 
6135064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
6145064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6155064Sdm199847 		goto errout;
6164644Sbaban 
6174644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6184644Sbaban 	    OP_ADD_NAMERULE;
6194520Snw141292 	udthandle->next++;
6204520Snw141292 	return (IDMAP_SUCCESS);
6214520Snw141292 
6224520Snw141292 errout:
6234644Sbaban 	/* The batch should still be usable */
6244644Sbaban 	if (rule)
6254644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
6264520Snw141292 	errno = idmap_stat2errno(retcode);
6274520Snw141292 	return (retcode);
6284520Snw141292 }
6294520Snw141292 
6304520Snw141292 
6314520Snw141292 /* ARGSUSED */
6324520Snw141292 idmap_stat
6334520Snw141292 idmap_udt_rm_namerule(idmap_udt_handle_t *udthandle, boolean_t is_user,
6345696Snw141292     boolean_t is_wuser,	const char *windomain, const char *winname,
6355696Snw141292     const char *unixname, int direction)
6365696Snw141292 {
6374520Snw141292 	idmap_retcode	retcode;
6384644Sbaban 	idmap_namerule	*rule = NULL;
6394520Snw141292 
6404644Sbaban 	retcode = _udt_extend_batch(udthandle);
6414520Snw141292 	if (retcode != IDMAP_SUCCESS)
6424520Snw141292 		goto errout;
6434520Snw141292 
6444520Snw141292 	rule = &udthandle->batch.
6455696Snw141292 	    idmap_update_batch_val[udthandle->next].
6465696Snw141292 	    idmap_update_op_u.rule;
6474520Snw141292 	rule->is_user = is_user;
6485696Snw141292 	rule->is_wuser = is_wuser;
6494520Snw141292 	rule->direction = direction;
6505064Sdm199847 
6515064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
6525064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6535064Sdm199847 		goto errout;
6545064Sdm199847 
6555064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
6565064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6575064Sdm199847 		goto errout;
6585064Sdm199847 
6595064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
6605064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6615064Sdm199847 		goto errout;
6625064Sdm199847 
6634644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6644644Sbaban 	    OP_RM_NAMERULE;
6654520Snw141292 	udthandle->next++;
6664520Snw141292 	return (IDMAP_SUCCESS);
6674520Snw141292 
6684520Snw141292 errout:
6694644Sbaban 	if (rule)
6704644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
6714520Snw141292 	errno = idmap_stat2errno(retcode);
6724520Snw141292 	return (retcode);
6734520Snw141292 }
6744520Snw141292 
6754520Snw141292 
6764520Snw141292 /* ARGSUSED */
6774520Snw141292 idmap_stat
6785696Snw141292 idmap_udt_flush_namerules(idmap_udt_handle_t *udthandle)
6795696Snw141292 {
6804520Snw141292 	idmap_retcode	retcode;
6814520Snw141292 
6824644Sbaban 	retcode = _udt_extend_batch(udthandle);
6834520Snw141292 	if (retcode != IDMAP_SUCCESS)
6844520Snw141292 		goto errout;
6854520Snw141292 
6864644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6874644Sbaban 	    OP_FLUSH_NAMERULES;
6884520Snw141292 	udthandle->next++;
6894520Snw141292 	return (IDMAP_SUCCESS);
6904520Snw141292 
6914520Snw141292 errout:
6924520Snw141292 	errno = idmap_stat2errno(retcode);
6934520Snw141292 	return (retcode);
6944520Snw141292 }
6954520Snw141292 
6964520Snw141292 
6974520Snw141292 /*
6984520Snw141292  * Set the number of entries requested per batch by the iterator
6994520Snw141292  *
7004520Snw141292  * Input:
7014520Snw141292  * iter  - iterator
7024520Snw141292  * limit - number of entries requested per batch
7034520Snw141292  */
7044520Snw141292 idmap_stat
7055696Snw141292 idmap_iter_set_limit(idmap_iter_t *iter, uint64_t limit)
7065696Snw141292 {
7074520Snw141292 	if (iter == NULL) {
7084520Snw141292 		errno = EINVAL;
7094520Snw141292 		return (IDMAP_ERR_ARG);
7104520Snw141292 	}
7114520Snw141292 	iter->limit = limit;
7124520Snw141292 	return (IDMAP_SUCCESS);
7134520Snw141292 }
7144520Snw141292 
7154520Snw141292 
7164520Snw141292 /*
7174520Snw141292  * Create iterator to get name-based mapping rules
7184520Snw141292  *
7194520Snw141292  * Input:
7204520Snw141292  * windomain - Windows domain
7214520Snw141292  * is_user   - user or group rules
7224520Snw141292  * winname   - Windows user or group name
7234520Snw141292  * unixname  - Unix user or group name
7244520Snw141292  *
7254520Snw141292  * Output:
7264520Snw141292  * iter - iterator
7274520Snw141292  */
7284520Snw141292 idmap_stat
7294520Snw141292 idmap_iter_namerules(idmap_handle_t *handle, const char *windomain,
7305696Snw141292 		boolean_t is_user, boolean_t is_wuser, const char *winname,
7315696Snw141292 		const char *unixname, idmap_iter_t **iter)
7325696Snw141292 {
7334520Snw141292 
7344520Snw141292 	idmap_iter_t			*tmpiter;
7354520Snw141292 	idmap_list_namerules_1_argument	*arg = NULL;
7364520Snw141292 	idmap_namerule			*rule;
7374520Snw141292 	idmap_retcode			retcode;
7384520Snw141292 
7394520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_NAMERULES);
7404520Snw141292 
7414520Snw141292 	rule = &arg->rule;
7424520Snw141292 	rule->is_user = is_user;
7435696Snw141292 	rule->is_wuser = is_wuser;
7444644Sbaban 	rule->direction = IDMAP_DIRECTION_UNDEF;
7455064Sdm199847 
7465064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
7475064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7485064Sdm199847 		goto errout;
7495064Sdm199847 
7505064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
7515064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7525064Sdm199847 		goto errout;
7535064Sdm199847 
7545064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
7555064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7565064Sdm199847 		goto errout;
7574520Snw141292 
7584520Snw141292 	*iter = tmpiter;
7594520Snw141292 	return (IDMAP_SUCCESS);
7604520Snw141292 
7614520Snw141292 errout:
7624520Snw141292 	__ITER_ERR_RETURN(tmpiter, arg,
7635696Snw141292 	    xdr_idmap_list_namerules_1_argument, retcode);
7644520Snw141292 }
7654520Snw141292 
7664520Snw141292 
7674520Snw141292 /*
7684520Snw141292  * Iterate through the name-based mapping rules
7694520Snw141292  *
7704520Snw141292  * Input:
7714520Snw141292  * iter - iterator
7724520Snw141292  *
7734520Snw141292  * Output:
7744520Snw141292  * windomain - Windows domain
7754520Snw141292  * winname   - Windows user or group name
7764520Snw141292  * unixname  - Unix user or group name
7774520Snw141292  * is_nt4    - NT4 or AD
7784520Snw141292  * direction - bi(0), win2unix(1), unix2win(2)
7794520Snw141292  *
7804520Snw141292  * Return value:
7814520Snw141292  * 0   - done
7824520Snw141292  * 1   - more results available
7834520Snw141292  * < 0 - error
7844520Snw141292  */
7854520Snw141292 idmap_stat
7864520Snw141292 idmap_iter_next_namerule(idmap_iter_t *iter, char **windomain,
7875696Snw141292     char **winname, char **unixname,  boolean_t *is_user,
7885696Snw141292     boolean_t *is_wuser, boolean_t *is_nt4, int *direction)
7895696Snw141292 {
7904520Snw141292 	idmap_namerules_res		*namerules;
7914520Snw141292 	idmap_list_namerules_1_argument	*arg;
7924520Snw141292 	idmap_retcode			retcode;
7934520Snw141292 
7945696Snw141292 	idmap_namerule_parts_clear(windomain, winname,
7955696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
7965696Snw141292 
7974520Snw141292 
7984520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_NAMERULES);
7994520Snw141292 
8004520Snw141292 	namerules = (idmap_namerules_res *)iter->retlist;
8014520Snw141292 	if (iter->retcode == IDMAP_NEXT && (namerules == NULL ||
8025696Snw141292 	    iter->next >= namerules->rules.rules_len)) {
8034520Snw141292 
8044520Snw141292 		if ((arg = iter->arg) == NULL) {
8054520Snw141292 			errno = EINVAL;
8064520Snw141292 			return (IDMAP_ERR_ARG);
8074520Snw141292 		}
8084520Snw141292 		arg->limit = iter->limit;
8094520Snw141292 
8104520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_NAMERULES,
8115696Snw141292 		    iter, arg,
8125696Snw141292 		    (uchar_t **)&namerules, sizeof (*namerules),
8135696Snw141292 		    (xdrproc_t)xdr_idmap_list_namerules_1_argument,
8145696Snw141292 		    (xdrproc_t)xdr_idmap_namerules_res);
8154520Snw141292 		if (retcode != IDMAP_SUCCESS)
8164520Snw141292 			return (retcode);
8174520Snw141292 
8184520Snw141292 		if (IDMAP_ERROR(namerules->retcode)) {
8194520Snw141292 			retcode  = namerules->retcode;
8204520Snw141292 			xdr_free(xdr_idmap_namerules_res, (caddr_t)namerules);
8214520Snw141292 			free(namerules);
8224520Snw141292 			iter->retlist = NULL;
8234520Snw141292 			return (retcode);
8244520Snw141292 		}
8254520Snw141292 		iter->retcode = namerules->retcode;
8264520Snw141292 		arg->lastrowid = namerules->lastrowid;
8274520Snw141292 	}
8284520Snw141292 
8294520Snw141292 	if (namerules == NULL || namerules->rules.rules_len == 0)
8304520Snw141292 		return (IDMAP_SUCCESS);
8314520Snw141292 
8324520Snw141292 	if (iter->next >= namerules->rules.rules_len) {
8334520Snw141292 		return (IDMAP_ERR_ARG);
8344520Snw141292 	}
8354520Snw141292 
8365064Sdm199847 	retcode = idmap_strdupnull(windomain,
8375064Sdm199847 	    namerules->rules.rules_val[iter->next].windomain);
8385064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8395064Sdm199847 		goto errout;
8405064Sdm199847 
8415064Sdm199847 	retcode = idmap_strdupnull(winname,
8425064Sdm199847 	    namerules->rules.rules_val[iter->next].winname);
8435064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8445064Sdm199847 		goto errout;
8455064Sdm199847 
8465064Sdm199847 	retcode = idmap_strdupnull(unixname,
8475064Sdm199847 	    namerules->rules.rules_val[iter->next].unixname);
8485064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8495064Sdm199847 		goto errout;
8505064Sdm199847 
8514520Snw141292 	if (is_nt4)
8524520Snw141292 		*is_nt4 = namerules->rules.rules_val[iter->next].is_nt4;
8535696Snw141292 	if (is_user)
8545696Snw141292 		*is_user = namerules->rules.rules_val[iter->next].is_user;
8555696Snw141292 	if (is_wuser)
8565696Snw141292 		*is_wuser = namerules->rules.rules_val[iter->next].is_wuser;
8574520Snw141292 	if (direction)
8584520Snw141292 		*direction = namerules->rules.rules_val[iter->next].direction;
8594520Snw141292 	iter->next++;
8604520Snw141292 
8614520Snw141292 	if (iter->next == namerules->rules.rules_len)
8624520Snw141292 		return (iter->retcode);
8634520Snw141292 	else
8644520Snw141292 		return (IDMAP_NEXT);
8654520Snw141292 
8664520Snw141292 errout:
8674520Snw141292 	if (windomain && *windomain)
8684520Snw141292 		free(*windomain);
8694520Snw141292 	if (winname && *winname)
8704520Snw141292 		free(*winname);
8714520Snw141292 	if (unixname && *unixname)
8724520Snw141292 		free(*unixname);
8734520Snw141292 	return (retcode);
8744520Snw141292 }
8754520Snw141292 
8764520Snw141292 
8774520Snw141292 /*
8784520Snw141292  * Create iterator to get SID to UID/GID mappings
8794520Snw141292  *
8804520Snw141292  * Output:
8814520Snw141292  * iter - iterator
8824520Snw141292  */
8834520Snw141292 idmap_stat
8846386Sjp151216 idmap_iter_mappings(idmap_handle_t *handle, idmap_iter_t **iter, int flag)
8855696Snw141292 {
8864520Snw141292 	idmap_iter_t			*tmpiter;
8874520Snw141292 	idmap_list_mappings_1_argument	*arg = NULL;
8884520Snw141292 
8894520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_MAPPINGS);
8904520Snw141292 
8916386Sjp151216 	arg->flag = flag;
8924520Snw141292 	*iter = tmpiter;
8934520Snw141292 	return (IDMAP_SUCCESS);
8944520Snw141292 }
8954520Snw141292 
8964520Snw141292 
8974520Snw141292 /*
8984520Snw141292  * Iterate through the SID to UID/GID mappings
8994520Snw141292  *
9004520Snw141292  * Input:
9014520Snw141292  * iter - iterator
9024520Snw141292  *
9034520Snw141292  * Output:
9044520Snw141292  * sid - SID in canonical form
9054520Snw141292  * pid - UID or GID
9064520Snw141292  *
9074520Snw141292  * Return value:
9084520Snw141292  * 0   - done
9094520Snw141292  * 1   - more results available
9104520Snw141292  * < 0 - error
9114520Snw141292  */
9124520Snw141292 idmap_stat
9134520Snw141292 idmap_iter_next_mapping(idmap_iter_t *iter, char **sidprefix,
9145696Snw141292     idmap_rid_t *rid, uid_t *pid, char **winname,
9155696Snw141292     char **windomain, char **unixname, boolean_t *is_user,
9166386Sjp151216     boolean_t *is_wuser, int *direction, idmap_info *info)
9175696Snw141292 {
9184520Snw141292 	idmap_mappings_res		*mappings;
9194520Snw141292 	idmap_list_mappings_1_argument	*arg;
9204520Snw141292 	idmap_retcode			retcode;
9214520Snw141292 	char				*str;
9224520Snw141292 
9234520Snw141292 	if (sidprefix)
9244520Snw141292 		*sidprefix = NULL;
9254520Snw141292 	if (rid)
9264520Snw141292 		*rid = UINT32_MAX;
9274520Snw141292 	if (winname)
9284520Snw141292 		*winname = NULL;
9294520Snw141292 	if (windomain)
9304520Snw141292 		*windomain = NULL;
9314520Snw141292 	if (unixname)
9324520Snw141292 		*unixname = NULL;
9334520Snw141292 	if (pid)
9344520Snw141292 		*pid = UINT32_MAX;
9355696Snw141292 	if (is_user)
9365696Snw141292 		*is_user = -1;
9375696Snw141292 	if (is_wuser)
9385696Snw141292 		*is_wuser = -1;
9394520Snw141292 	if (direction)
9404644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
9414520Snw141292 
9424520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_MAPPINGS);
9434520Snw141292 
9444520Snw141292 	mappings = (idmap_mappings_res *)iter->retlist;
9454520Snw141292 	if (iter->retcode == IDMAP_NEXT && (mappings == NULL ||
9465696Snw141292 	    iter->next >= mappings->mappings.mappings_len)) {
9474520Snw141292 
9484520Snw141292 		if ((arg = iter->arg) == NULL) {
9494520Snw141292 			errno = EINVAL;
9504520Snw141292 			return (IDMAP_ERR_ARG);
9514520Snw141292 		}
9524520Snw141292 		arg->limit = iter->limit;
9534520Snw141292 
9544520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_MAPPINGS,
9555696Snw141292 		    iter, arg,
9565696Snw141292 		    (uchar_t **)&mappings, sizeof (*mappings),
9575696Snw141292 		    (xdrproc_t)xdr_idmap_list_mappings_1_argument,
9585696Snw141292 		    (xdrproc_t)xdr_idmap_mappings_res);
9594520Snw141292 		if (retcode != IDMAP_SUCCESS)
9604520Snw141292 			return (retcode);
9614520Snw141292 
9624520Snw141292 		if (IDMAP_ERROR(mappings->retcode)) {
9634520Snw141292 			retcode  = mappings->retcode;
9644520Snw141292 			xdr_free(xdr_idmap_mappings_res, (caddr_t)mappings);
9654520Snw141292 			free(mappings);
9664520Snw141292 			iter->retlist = NULL;
9674520Snw141292 			return (retcode);
9684520Snw141292 		}
9694520Snw141292 		iter->retcode = mappings->retcode;
9704520Snw141292 		arg->lastrowid = mappings->lastrowid;
9714520Snw141292 	}
9724520Snw141292 
9734520Snw141292 	if (mappings == NULL || mappings->mappings.mappings_len == 0)
9744520Snw141292 		return (IDMAP_SUCCESS);
9754520Snw141292 
9764520Snw141292 	if (iter->next >= mappings->mappings.mappings_len) {
9774520Snw141292 		return (IDMAP_ERR_ARG);
9784520Snw141292 	}
9794520Snw141292 
9804520Snw141292 	if (sidprefix) {
9814520Snw141292 		str = mappings->mappings.mappings_val[iter->next].id1.
9825696Snw141292 		    idmap_id_u.sid.prefix;
9834695Sbaban 		if (str && *str != '\0') {
9844520Snw141292 			*sidprefix = strdup(str);
9854526Sbaban 			if (*sidprefix == NULL) {
9864526Sbaban 				retcode = IDMAP_ERR_MEMORY;
9874526Sbaban 				goto errout;
9884526Sbaban 			}
9894520Snw141292 		}
9904520Snw141292 	}
9914520Snw141292 	if (rid)
9924520Snw141292 		*rid = mappings->mappings.mappings_val[iter->next].id1.
9935696Snw141292 		    idmap_id_u.sid.rid;
9945064Sdm199847 
9955064Sdm199847 	retcode = idmap_strdupnull(windomain,
9965064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1domain);
9975064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9985064Sdm199847 		goto errout;
9995064Sdm199847 
10005064Sdm199847 	retcode = idmap_strdupnull(winname,
10015064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1name);
10025064Sdm199847 	if (retcode != IDMAP_SUCCESS)
10035064Sdm199847 		goto errout;
10045064Sdm199847 
10055064Sdm199847 	retcode = idmap_strdupnull(unixname,
10065064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id2name);
10075064Sdm199847 	if (retcode != IDMAP_SUCCESS)
10085064Sdm199847 		goto errout;
10095064Sdm199847 
10105064Sdm199847 
10114520Snw141292 	if (pid)
10124520Snw141292 		*pid = mappings->mappings.mappings_val[iter->next].id2.
10135696Snw141292 		    idmap_id_u.uid;
10144520Snw141292 	if (direction)
10154520Snw141292 		*direction = mappings->mappings.mappings_val[iter->next].
10165696Snw141292 		    direction;
10175696Snw141292 	if (is_user)
10185696Snw141292 		*is_user = (mappings->mappings.mappings_val[iter->next].id2
10195696Snw141292 		    .idtype == IDMAP_UID)?1:0;
10205696Snw141292 	if (is_wuser)
10215696Snw141292 		*is_wuser = (mappings->mappings.mappings_val[iter->next].id1
10225696Snw141292 		    .idtype == IDMAP_USID)?1:0;
10235696Snw141292 
10246386Sjp151216 	if (info) {
10256386Sjp151216 		retcode = idmap_info_cpy(info,
10266386Sjp151216 		    &mappings->mappings.mappings_val[iter->next].info);
10276386Sjp151216 		if (retcode != IDMAP_SUCCESS)
10286386Sjp151216 			goto errout;
10296386Sjp151216 	}
10304520Snw141292 	iter->next++;
10314520Snw141292 
10324520Snw141292 	if (iter->next == mappings->mappings.mappings_len)
10334520Snw141292 		return (iter->retcode);
10344520Snw141292 	else
10354520Snw141292 		return (IDMAP_NEXT);
10364520Snw141292 
10374520Snw141292 errout:
10384520Snw141292 	if (sidprefix && *sidprefix)
10394520Snw141292 		free(*sidprefix);
10404520Snw141292 	if (winname && *winname)
10414520Snw141292 		free(*winname);
10424520Snw141292 	if (windomain && *windomain)
10434520Snw141292 		free(*windomain);
10444520Snw141292 	if (unixname && *unixname)
10454520Snw141292 		free(*unixname);
10464520Snw141292 	return (retcode);
10474520Snw141292 }
10484520Snw141292 
10494520Snw141292 
10504520Snw141292 /*
10514520Snw141292  * Destroy the iterator
10524520Snw141292  */
10534520Snw141292 void
10545696Snw141292 idmap_iter_destroy(idmap_iter_t *iter)
10555696Snw141292 {
10564520Snw141292 	xdrproc_t _xdr_argument, _xdr_result;
10574520Snw141292 
10584520Snw141292 	if (iter == NULL)
10594520Snw141292 		return;
10604520Snw141292 
10614520Snw141292 	switch (iter->type) {
10624520Snw141292 	case IDMAP_LIST_NAMERULES:
10634520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_namerules_1_argument;
10644520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_namerules_res;
10654520Snw141292 		break;
10664520Snw141292 	case IDMAP_LIST_MAPPINGS:
10674520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_mappings_1_argument;
10684520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_mappings_res;
10694520Snw141292 		break;
10704520Snw141292 	default:
10714520Snw141292 		free(iter);
10724520Snw141292 		return;
10734520Snw141292 	};
10744520Snw141292 
10754520Snw141292 	if (iter->arg) {
10764520Snw141292 		xdr_free(_xdr_argument, (caddr_t)iter->arg);
10774520Snw141292 		free(iter->arg);
10784520Snw141292 	}
10794520Snw141292 	if (iter->retlist) {
10804520Snw141292 		xdr_free(_xdr_result, (caddr_t)iter->retlist);
10814520Snw141292 		free(iter->retlist);
10824520Snw141292 	}
10834520Snw141292 	free(iter);
10844520Snw141292 }
10854520Snw141292 
10864520Snw141292 
10874520Snw141292 /*
10884520Snw141292  * Create handle to get SID to UID/GID mapping entries
10894520Snw141292  *
10904520Snw141292  * Input:
10914520Snw141292  * gh - "get mapping" handle
10924520Snw141292  */
10934520Snw141292 idmap_stat
10945696Snw141292 idmap_get_create(idmap_handle_t *handle, idmap_get_handle_t **gh)
10955696Snw141292 {
10964520Snw141292 	idmap_get_handle_t	*tmp;
10974520Snw141292 
10984520Snw141292 	/* sanity checks */
10994520Snw141292 	if (handle == NULL || gh == NULL) {
11004520Snw141292 		errno = EINVAL;
11014520Snw141292 		return (IDMAP_ERR_ARG);
11024520Snw141292 	}
11034520Snw141292 
11044520Snw141292 	/* allocate the handle */
11054520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
11064520Snw141292 		errno = ENOMEM;
11074520Snw141292 		return (IDMAP_ERR_MEMORY);
11084520Snw141292 	}
11094520Snw141292 
11104520Snw141292 	tmp->ih = handle;
11114520Snw141292 	*gh = tmp;
11124520Snw141292 	return (IDMAP_SUCCESS);
11134520Snw141292 }
11144520Snw141292 
11154520Snw141292 
11164520Snw141292 /*
11174520Snw141292  * Given SID, get UID
11184520Snw141292  *
11194520Snw141292  * Input:
11204520Snw141292  * sidprefix  - SID prefix
11214520Snw141292  * rid        - RID
11224520Snw141292  * flag       - flag
11234520Snw141292  *
11244520Snw141292  * Output:
11254520Snw141292  * stat - status of the get request
11264520Snw141292  * uid  - POSIX UID if stat = 0
11274520Snw141292  *
11284520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
11294520Snw141292  */
11304520Snw141292 idmap_stat
11314520Snw141292 idmap_get_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
11325696Snw141292 		int flag, uid_t *uid, idmap_stat *stat)
11335696Snw141292 {
11346386Sjp151216 	return (idmap_getext_uidbysid(gh, sidprefix, rid, flag, uid,
11356386Sjp151216 	    NULL, stat));
11366386Sjp151216 }
11374520Snw141292 
11386386Sjp151216 /*
11396386Sjp151216  * Given SID, get UID
11406386Sjp151216  *
11416386Sjp151216  * Input:
11426386Sjp151216  * sidprefix  - SID prefix
11436386Sjp151216  * rid        - RID
11446386Sjp151216  * flag       - flag
11456386Sjp151216  *
11466386Sjp151216  * Output:
11476386Sjp151216  * stat - status of the get request
11486386Sjp151216  * uid  - POSIX UID if stat = 0
11496386Sjp151216  * how  - mapping type if stat = 0
11506386Sjp151216  *
11516386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
11526386Sjp151216  */
11536386Sjp151216 
11546386Sjp151216 idmap_stat
11556386Sjp151216 idmap_getext_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
11566386Sjp151216 		int flag, uid_t *uid, idmap_info *info, idmap_stat *stat)
11576386Sjp151216 {
11584520Snw141292 	idmap_retcode	retcode;
11594644Sbaban 	idmap_mapping	*mapping = NULL;
11604520Snw141292 
11614520Snw141292 	/* sanity checks */
11624520Snw141292 	if (gh == NULL)
11634520Snw141292 		return (IDMAP_ERR_ARG);
11644520Snw141292 	if (uid == NULL || sidprefix == NULL)
11654520Snw141292 		return (IDMAP_ERR_ARG);
11664520Snw141292 
11677369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
11687369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
11697369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_uidbysid(sidprefix, rid, uid);
11707369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
11717369SJulian.Pullen@Sun.COM 			*stat = retcode;
11727369SJulian.Pullen@Sun.COM 			return (retcode);
11737369SJulian.Pullen@Sun.COM 		}
11747369SJulian.Pullen@Sun.COM 	}
11757369SJulian.Pullen@Sun.COM 
11764520Snw141292 	/* Extend the request array and the return list */
11774520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
11784520Snw141292 		goto errout;
11794520Snw141292 
11804520Snw141292 	/* Setup the request */
11814520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
11824520Snw141292 	mapping->flag = flag;
11834520Snw141292 	mapping->id1.idtype = IDMAP_SID;
11844520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
11854520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
11864520Snw141292 		retcode = IDMAP_ERR_MEMORY;
11874520Snw141292 		goto errout;
11884520Snw141292 	}
11894520Snw141292 	mapping->id2.idtype = IDMAP_UID;
11904520Snw141292 
11914520Snw141292 	/* Setup pointers for the result */
11924520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_UID;
11934520Snw141292 	gh->retlist[gh->next].uid = uid;
11944520Snw141292 	gh->retlist[gh->next].stat = stat;
11956386Sjp151216 	gh->retlist[gh->next].info = info;
11967369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
11974520Snw141292 
11984520Snw141292 	gh->next++;
11994520Snw141292 	return (IDMAP_SUCCESS);
12004520Snw141292 
12014520Snw141292 errout:
12024644Sbaban 	/* Batch created so far should still be usable */
12034644Sbaban 	if (mapping)
12044644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
12054520Snw141292 	errno = idmap_stat2errno(retcode);
12064520Snw141292 	return (retcode);
12074520Snw141292 }
12084520Snw141292 
12094520Snw141292 
12104520Snw141292 /*
12114520Snw141292  * Given SID, get GID
12124520Snw141292  *
12134520Snw141292  * Input:
12144520Snw141292  * sidprefix  - SID prefix
12154520Snw141292  * rid        - rid
12164520Snw141292  * flag       - flag
12174520Snw141292  *
12184520Snw141292  * Output:
12194520Snw141292  * stat - status of the get request
12204520Snw141292  * gid  - POSIX GID if stat = 0
12214520Snw141292  *
12224520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
12234520Snw141292  */
12244520Snw141292 idmap_stat
12254520Snw141292 idmap_get_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
12265696Snw141292 		int flag, gid_t *gid, idmap_stat *stat)
12275696Snw141292 {
12286386Sjp151216 	return (idmap_getext_gidbysid(gh, sidprefix, rid, flag, gid,
12296386Sjp151216 	    NULL, stat));
12306386Sjp151216 }
12316386Sjp151216 
12326386Sjp151216 
12336386Sjp151216 /*
12346386Sjp151216  * Given SID, get GID
12356386Sjp151216  *
12366386Sjp151216  * Input:
12376386Sjp151216  * sidprefix  - SID prefix
12386386Sjp151216  * rid        - rid
12396386Sjp151216  * flag       - flag
12406386Sjp151216  *
12416386Sjp151216  * Output:
12426386Sjp151216  * stat - status of the get request
12436386Sjp151216  * gid  - POSIX GID if stat = 0
12446386Sjp151216  * how  - mapping type if stat = 0
12456386Sjp151216  *
12466386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
12476386Sjp151216  */
12486386Sjp151216 idmap_stat
12496386Sjp151216 idmap_getext_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
12506386Sjp151216 		int flag, gid_t *gid, idmap_info *info, idmap_stat *stat)
12516386Sjp151216 {
12524520Snw141292 
12534520Snw141292 	idmap_retcode	retcode;
12544644Sbaban 	idmap_mapping	*mapping = NULL;
12554520Snw141292 
12564520Snw141292 	/* sanity checks */
12574520Snw141292 	if (gh == NULL)
12584520Snw141292 		return (IDMAP_ERR_ARG);
12594520Snw141292 	if (gid == NULL || sidprefix == NULL)
12604520Snw141292 		return (IDMAP_ERR_ARG);
12614520Snw141292 
12627369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
12637369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
12647369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_gidbysid(sidprefix, rid, gid);
12657369SJulian.Pullen@Sun.COM 		if (retcode == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12667369SJulian.Pullen@Sun.COM 			*stat = retcode;
12677369SJulian.Pullen@Sun.COM 			return (retcode);
12687369SJulian.Pullen@Sun.COM 		}
12697369SJulian.Pullen@Sun.COM 	}
12707369SJulian.Pullen@Sun.COM 
12714520Snw141292 	/* Extend the request array and the return list */
12724520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
12734520Snw141292 		goto errout;
12744520Snw141292 
12754520Snw141292 	/* Setup the request */
12764520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
12774520Snw141292 	mapping->flag = flag;
12784520Snw141292 	mapping->id1.idtype = IDMAP_SID;
12794520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
12804520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
12814520Snw141292 		retcode = IDMAP_ERR_MEMORY;
12824520Snw141292 		goto errout;
12834520Snw141292 	}
12844520Snw141292 	mapping->id2.idtype = IDMAP_GID;
12854520Snw141292 
12864520Snw141292 	/* Setup pointers for the result */
12874520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_GID;
12884520Snw141292 	gh->retlist[gh->next].gid = gid;
12894520Snw141292 	gh->retlist[gh->next].stat = stat;
12906386Sjp151216 	gh->retlist[gh->next].info = info;
12917369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
12924520Snw141292 
12934520Snw141292 	gh->next++;
12944520Snw141292 	return (IDMAP_SUCCESS);
12954520Snw141292 
12964520Snw141292 errout:
12974644Sbaban 	if (mapping)
12984644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
12994520Snw141292 	errno = idmap_stat2errno(retcode);
13004520Snw141292 	return (retcode);
13014520Snw141292 }
13024520Snw141292 
13034520Snw141292 
13046386Sjp151216 
13054520Snw141292 /*
13064520Snw141292  * Given SID, get POSIX ID i.e. UID/GID
13074520Snw141292  *
13084520Snw141292  * Input:
13094520Snw141292  * sidprefix  - SID prefix
13104520Snw141292  * rid        - rid
13114520Snw141292  * flag       - flag
13124520Snw141292  *
13134520Snw141292  * Output:
13144520Snw141292  * stat    - status of the get request
13154520Snw141292  * is_user - user or group
13164520Snw141292  * pid     - POSIX UID if stat = 0 and is_user = 1
13174520Snw141292  *           POSIX GID if stat = 0 and is_user = 0
13184520Snw141292  *
13194520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
13204520Snw141292  */
13214520Snw141292 idmap_stat
13224520Snw141292 idmap_get_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
13235696Snw141292 		int flag, uid_t *pid, int *is_user, idmap_stat *stat)
13245696Snw141292 {
13256386Sjp151216 	return (idmap_getext_pidbysid(gh, sidprefix, rid, flag, pid, is_user,
13266386Sjp151216 	    NULL, stat));
13276386Sjp151216 }
13286386Sjp151216 
13296386Sjp151216 
13306386Sjp151216 
13316386Sjp151216 /*
13326386Sjp151216  * Given SID, get POSIX ID i.e. UID/GID
13336386Sjp151216  *
13346386Sjp151216  * Input:
13356386Sjp151216  * sidprefix  - SID prefix
13366386Sjp151216  * rid        - rid
13376386Sjp151216  * flag       - flag
13386386Sjp151216  *
13396386Sjp151216  * Output:
13406386Sjp151216  * stat    - status of the get request
13416386Sjp151216  * is_user - user or group
13426386Sjp151216  * pid     - POSIX UID if stat = 0 and is_user = 1
13436386Sjp151216  *           POSIX GID if stat = 0 and is_user = 0
13446386Sjp151216  * how     - mapping type if stat = 0
13456386Sjp151216  *
13466386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
13476386Sjp151216  */
13486386Sjp151216 idmap_stat
13496386Sjp151216 idmap_getext_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
13506386Sjp151216 	int flag, uid_t *pid, int *is_user, idmap_info *info, idmap_stat *stat)
13516386Sjp151216 {
13524520Snw141292 	idmap_retcode	retcode;
13534644Sbaban 	idmap_mapping	*mapping = NULL;
13544520Snw141292 
13554520Snw141292 	/* sanity checks */
13564520Snw141292 	if (gh == NULL)
13574520Snw141292 		return (IDMAP_ERR_ARG);
13584520Snw141292 	if (pid == NULL || sidprefix == NULL || is_user == NULL)
13594520Snw141292 		return (IDMAP_ERR_ARG);
13604520Snw141292 
13617369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
13627369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
13637369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_pidbysid(sidprefix, rid, pid,
13647369SJulian.Pullen@Sun.COM 		    is_user);
13657369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
13667369SJulian.Pullen@Sun.COM 			*stat = retcode;
13677369SJulian.Pullen@Sun.COM 			return (retcode);
13687369SJulian.Pullen@Sun.COM 		}
13697369SJulian.Pullen@Sun.COM 	}
13707369SJulian.Pullen@Sun.COM 
13714520Snw141292 	/* Extend the request array and the return list */
13724520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
13734520Snw141292 		goto errout;
13744520Snw141292 
13754520Snw141292 	/* Setup the request */
13764520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
13774520Snw141292 	mapping->flag = flag;
13784520Snw141292 	mapping->id1.idtype = IDMAP_SID;
13794520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
13804520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
13814520Snw141292 		retcode = IDMAP_ERR_MEMORY;
13824520Snw141292 		goto errout;
13834520Snw141292 	}
13844520Snw141292 	mapping->id2.idtype = IDMAP_POSIXID;
13854520Snw141292 
13864520Snw141292 	/* Setup pointers for the result */
13874520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_POSIXID;
13884520Snw141292 	gh->retlist[gh->next].uid = pid;
13894520Snw141292 	gh->retlist[gh->next].gid = pid;
13904520Snw141292 	gh->retlist[gh->next].is_user = is_user;
13914520Snw141292 	gh->retlist[gh->next].stat = stat;
13926386Sjp151216 	gh->retlist[gh->next].info = info;
13937369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
13944520Snw141292 
13954520Snw141292 	gh->next++;
13964520Snw141292 	return (IDMAP_SUCCESS);
13974520Snw141292 
13984520Snw141292 errout:
13994644Sbaban 	if (mapping)
14004644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
14014520Snw141292 	errno = idmap_stat2errno(retcode);
14024520Snw141292 	return (retcode);
14034520Snw141292 }
14044520Snw141292 
14054520Snw141292 
14064520Snw141292 /*
14074520Snw141292  * Given UID, get SID
14084520Snw141292  *
14094520Snw141292  * Input:
14104520Snw141292  * uid  - POSIX UID
14114520Snw141292  * flag - flag
14124520Snw141292  *
14134520Snw141292  * Output:
14144520Snw141292  * stat - status of the get request
14154520Snw141292  * sid  - SID prefix (if stat == 0)
14164520Snw141292  * rid  - rid
14174520Snw141292  *
14184520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
14194520Snw141292  */
14204520Snw141292 idmap_stat
14214520Snw141292 idmap_get_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
14225696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
14235696Snw141292 {
14246386Sjp151216 	return (idmap_getext_sidbyuid(gh, uid, flag, sidprefix, rid,
14256386Sjp151216 	    NULL, stat));
14266386Sjp151216 }
14276386Sjp151216 
14286386Sjp151216 
14296386Sjp151216 /*
14306386Sjp151216  * Given UID, get SID
14316386Sjp151216  *
14326386Sjp151216  * Input:
14336386Sjp151216  * uid  - POSIX UID
14346386Sjp151216  * flag - flag
14356386Sjp151216  *
14366386Sjp151216  * Output:
14376386Sjp151216  * stat - status of the get request
14386386Sjp151216  * sid  - SID prefix (if stat == 0)
14396386Sjp151216  * rid  - rid
14406386Sjp151216  * how  - mapping type if stat = 0
14416386Sjp151216  *
14426386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
14436386Sjp151216  */
14446386Sjp151216 idmap_stat
14456386Sjp151216 idmap_getext_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
14466386Sjp151216 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
14476386Sjp151216 {
14484520Snw141292 
14494520Snw141292 	idmap_retcode	retcode;
14504644Sbaban 	idmap_mapping	*mapping = NULL;
14514520Snw141292 
14524520Snw141292 	/* sanity checks */
14534520Snw141292 	if (gh == NULL)
14544520Snw141292 		return (IDMAP_ERR_ARG);
14554520Snw141292 	if (sidprefix == NULL)
14564520Snw141292 		return (IDMAP_ERR_ARG);
14574520Snw141292 
14587369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
14597369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
14607369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_sidbyuid(sidprefix, rid, uid);
14617369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
14627369SJulian.Pullen@Sun.COM 			*stat = retcode;
14637369SJulian.Pullen@Sun.COM 			return (retcode);
14647369SJulian.Pullen@Sun.COM 		}
14657369SJulian.Pullen@Sun.COM 	}
14667369SJulian.Pullen@Sun.COM 
14674520Snw141292 	/* Extend the request array and the return list */
14684520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
14694520Snw141292 		goto errout;
14704520Snw141292 
14714520Snw141292 	/* Setup the request */
14724520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
14734520Snw141292 	mapping->flag = flag;
14744520Snw141292 	mapping->id1.idtype = IDMAP_UID;
14754520Snw141292 	mapping->id1.idmap_id_u.uid = uid;
14764520Snw141292 	mapping->id2.idtype = IDMAP_SID;
14774520Snw141292 
14784520Snw141292 	/* Setup pointers for the result */
14794520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
14804520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
14814520Snw141292 	gh->retlist[gh->next].rid = rid;
14824520Snw141292 	gh->retlist[gh->next].stat = stat;
14836386Sjp151216 	gh->retlist[gh->next].info = info;
14847369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
14854520Snw141292 
14864520Snw141292 	gh->next++;
14874520Snw141292 	return (IDMAP_SUCCESS);
14884520Snw141292 
14894520Snw141292 errout:
14904644Sbaban 	if (mapping)
14914644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
14924520Snw141292 	errno = idmap_stat2errno(retcode);
14934520Snw141292 	return (retcode);
14944520Snw141292 }
14954520Snw141292 
14964520Snw141292 
14974520Snw141292 /*
14984520Snw141292  * Given GID, get SID
14994520Snw141292  *
15004520Snw141292  * Input:
15014520Snw141292  * gid  - POSIX GID
15024520Snw141292  * flag - flag
15034520Snw141292  *
15044520Snw141292  * Output:
15054520Snw141292  * stat       - status of the get request
15064520Snw141292  * sidprefix  - SID prefix (if stat == 0)
15074520Snw141292  * rid        - rid
15084520Snw141292  *
15094520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
15104520Snw141292  */
15114520Snw141292 idmap_stat
15124520Snw141292 idmap_get_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
15135696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
15145696Snw141292 {
15156386Sjp151216 	return (idmap_getext_sidbygid(gh, gid, flag, sidprefix, rid,
15166386Sjp151216 	    NULL, stat));
15176386Sjp151216 }
15186386Sjp151216 
15196386Sjp151216 
15206386Sjp151216 /*
15216386Sjp151216  * Given GID, get SID
15226386Sjp151216  *
15236386Sjp151216  * Input:
15246386Sjp151216  * gid  - POSIX GID
15256386Sjp151216  * flag - flag
15266386Sjp151216  *
15276386Sjp151216  * Output:
15286386Sjp151216  * stat       - status of the get request
15296386Sjp151216  * sidprefix  - SID prefix (if stat == 0)
15306386Sjp151216  * rid        - rid
15316386Sjp151216  * how        - mapping type if stat = 0
15326386Sjp151216  *
15336386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
15346386Sjp151216  */
15356386Sjp151216 idmap_stat
15366386Sjp151216 idmap_getext_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
15376386Sjp151216 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
15386386Sjp151216 {
15394520Snw141292 
15404520Snw141292 	idmap_retcode	retcode;
15414644Sbaban 	idmap_mapping	*mapping = NULL;
15424520Snw141292 
15434520Snw141292 	/* sanity checks */
15444520Snw141292 	if (gh == NULL)
15454520Snw141292 		return (IDMAP_ERR_ARG);
15464520Snw141292 	if (sidprefix == NULL)
15474520Snw141292 		return (IDMAP_ERR_ARG);
15484520Snw141292 
15497369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
15507369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
15517369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_sidbygid(sidprefix, rid, gid);
15527369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
15537369SJulian.Pullen@Sun.COM 			*stat = retcode;
15547369SJulian.Pullen@Sun.COM 			return (retcode);
15557369SJulian.Pullen@Sun.COM 		}
15567369SJulian.Pullen@Sun.COM 	}
15577369SJulian.Pullen@Sun.COM 
15584520Snw141292 	/* Extend the request array and the return list */
15594520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
15604520Snw141292 		goto errout;
15614520Snw141292 
15624520Snw141292 	/* Setup the request */
15634520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
15644520Snw141292 	mapping->flag = flag;
15654520Snw141292 	mapping->id1.idtype = IDMAP_GID;
15664520Snw141292 	mapping->id1.idmap_id_u.gid = gid;
15674520Snw141292 	mapping->id2.idtype = IDMAP_SID;
15684520Snw141292 
15694520Snw141292 	/* Setup pointers for the result */
15704520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
15714520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
15724520Snw141292 	gh->retlist[gh->next].rid = rid;
15734520Snw141292 	gh->retlist[gh->next].stat = stat;
15746386Sjp151216 	gh->retlist[gh->next].info = info;
15757369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
15764520Snw141292 
15774520Snw141292 	gh->next++;
15784520Snw141292 	return (IDMAP_SUCCESS);
15794520Snw141292 
15804520Snw141292 errout:
15814644Sbaban 	if (mapping)
15824644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
15834520Snw141292 	errno = idmap_stat2errno(retcode);
15844520Snw141292 	return (retcode);
15854520Snw141292 }
15864520Snw141292 
15874520Snw141292 
15884520Snw141292 /*
15894520Snw141292  * Process the batched "get mapping" requests. The results (i.e.
15904520Snw141292  * status and identity) will be available in the data areas
15914520Snw141292  * provided by individual requests.
15924520Snw141292  */
15934520Snw141292 idmap_stat
15945696Snw141292 idmap_get_mappings(idmap_get_handle_t *gh)
15955696Snw141292 {
15964520Snw141292 	CLIENT		*clnt;
15974520Snw141292 	enum clnt_stat	clntstat;
15984520Snw141292 	idmap_retcode	retcode;
15994520Snw141292 	idmap_ids_res	res;
16007369SJulian.Pullen@Sun.COM 	idmap_id	*res_id;
16014520Snw141292 	int		i;
16027369SJulian.Pullen@Sun.COM 	idmap_id	*req_id;
16037369SJulian.Pullen@Sun.COM 	int		direction;
16044520Snw141292 
16054520Snw141292 	if (gh == NULL) {
16064520Snw141292 		errno = EINVAL;
16074520Snw141292 		return (IDMAP_ERR_ARG);
16084520Snw141292 	}
16094520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(gh->ih, clnt);
16104520Snw141292 
16114520Snw141292 	(void) memset(&res, 0, sizeof (idmap_ids_res));
16124520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_IDS,
16135696Snw141292 	    (xdrproc_t)xdr_idmap_mapping_batch,
16145696Snw141292 	    (caddr_t)&gh->batch,
16155696Snw141292 	    (xdrproc_t)xdr_idmap_ids_res,
16165696Snw141292 	    (caddr_t)&res,
16175696Snw141292 	    TIMEOUT);
16184520Snw141292 	if (clntstat != RPC_SUCCESS) {
16194644Sbaban 		retcode = _idmap_rpc2stat(clnt);
16204520Snw141292 		goto out;
16214520Snw141292 	}
16224520Snw141292 	if (res.retcode != IDMAP_SUCCESS) {
16234520Snw141292 		retcode = res.retcode;
16244520Snw141292 		goto out;
16254520Snw141292 	}
16264520Snw141292 	for (i = 0; i < gh->next; i++) {
16274520Snw141292 		if (i >= res.ids.ids_len) {
16284520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
16294520Snw141292 			continue;
16304520Snw141292 		}
16314520Snw141292 		*gh->retlist[i].stat = res.ids.ids_val[i].retcode;
16327369SJulian.Pullen@Sun.COM 		res_id = &res.ids.ids_val[i].id;
16337369SJulian.Pullen@Sun.COM 		direction = res.ids.ids_val[i].direction;
16347369SJulian.Pullen@Sun.COM 		req_id = &gh->batch.idmap_mapping_batch_val[i].id1;
16357369SJulian.Pullen@Sun.COM 		switch (res_id->idtype) {
16364520Snw141292 		case IDMAP_UID:
16374520Snw141292 			if (gh->retlist[i].uid)
16387369SJulian.Pullen@Sun.COM 				*gh->retlist[i].uid = res_id->idmap_id_u.uid;
16394520Snw141292 			if (gh->retlist[i].is_user)
16404520Snw141292 				*gh->retlist[i].is_user = 1;
16417369SJulian.Pullen@Sun.COM 
16427369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
16437369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
16447369SJulian.Pullen@Sun.COM 				if (gh->retlist[i].is_user != NULL)
16457369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2pid(
16467369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16477369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16487369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.uid, 1,
16497369SJulian.Pullen@Sun.COM 					    direction);
16507369SJulian.Pullen@Sun.COM 				else
16517369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2uid(
16527369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16537369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16547369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.uid,
16557369SJulian.Pullen@Sun.COM 					    direction);
16567369SJulian.Pullen@Sun.COM 			}
16574520Snw141292 			break;
16587369SJulian.Pullen@Sun.COM 
16594520Snw141292 		case IDMAP_GID:
16604520Snw141292 			if (gh->retlist[i].gid)
16617369SJulian.Pullen@Sun.COM 				*gh->retlist[i].gid = res_id->idmap_id_u.gid;
16624520Snw141292 			if (gh->retlist[i].is_user)
16634520Snw141292 				*gh->retlist[i].is_user = 0;
16647369SJulian.Pullen@Sun.COM 
16657369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
16667369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
16677369SJulian.Pullen@Sun.COM 				if (gh->retlist[i].is_user != NULL)
16687369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2pid(
16697369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16707369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16717369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.gid, 0,
16727369SJulian.Pullen@Sun.COM 					    direction);
16737369SJulian.Pullen@Sun.COM 				else
16747369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2gid(
16757369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16767369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16777369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.gid,
16787369SJulian.Pullen@Sun.COM 					    direction);
16797369SJulian.Pullen@Sun.COM 			}
16804520Snw141292 			break;
16817369SJulian.Pullen@Sun.COM 
16824864Sbaban 		case IDMAP_POSIXID:
16834864Sbaban 			if (gh->retlist[i].uid)
16844864Sbaban 				*gh->retlist[i].uid = 60001;
16854864Sbaban 			if (gh->retlist[i].is_user)
16864864Sbaban 				*gh->retlist[i].is_user = -1;
16874864Sbaban 			break;
16887369SJulian.Pullen@Sun.COM 
16894520Snw141292 		case IDMAP_SID:
16905696Snw141292 		case IDMAP_USID:
16915696Snw141292 		case IDMAP_GSID:
16924520Snw141292 			if (gh->retlist[i].rid)
16937369SJulian.Pullen@Sun.COM 				*gh->retlist[i].rid =
16947369SJulian.Pullen@Sun.COM 				    res_id->idmap_id_u.sid.rid;
16954520Snw141292 			if (gh->retlist[i].sidprefix) {
16967369SJulian.Pullen@Sun.COM 				if (res_id->idmap_id_u.sid.prefix == NULL ||
16977369SJulian.Pullen@Sun.COM 				    *res_id->idmap_id_u.sid.prefix == '\0') {
16984520Snw141292 					*gh->retlist[i].sidprefix = NULL;
16994520Snw141292 					break;
17004520Snw141292 				}
17014520Snw141292 				*gh->retlist[i].sidprefix =
17027369SJulian.Pullen@Sun.COM 				    strdup(res_id->idmap_id_u.sid.prefix);
17034520Snw141292 				if (*gh->retlist[i].sidprefix == NULL)
17044520Snw141292 					*gh->retlist[i].stat =
17055696Snw141292 					    IDMAP_ERR_MEMORY;
17064520Snw141292 			}
17077369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
17087369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
17097369SJulian.Pullen@Sun.COM 				if (req_id->idtype == IDMAP_UID)
17107369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2uid(
17117369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.prefix,
17127369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.rid,
17137369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.uid,
17147369SJulian.Pullen@Sun.COM 					    direction);
17157369SJulian.Pullen@Sun.COM 				else /* req_id->idtype == IDMAP_GID */
17167369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2gid(
17177369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.prefix,
17187369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.rid,
17197369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.gid,
17207369SJulian.Pullen@Sun.COM 					    direction);
17217369SJulian.Pullen@Sun.COM 			}
17224520Snw141292 			break;
17237369SJulian.Pullen@Sun.COM 
17244520Snw141292 		case IDMAP_NONE:
17254520Snw141292 			break;
17267369SJulian.Pullen@Sun.COM 
17274520Snw141292 		default:
17284520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
17294520Snw141292 			break;
17304520Snw141292 		}
17316386Sjp151216 		if (gh->retlist[i].info != NULL)
17326386Sjp151216 			(void) idmap_info_cpy(gh->retlist[i].info,
17336386Sjp151216 			    &res.ids.ids_val[i].info);
17344520Snw141292 	}
17354520Snw141292 	retcode = IDMAP_SUCCESS;
17364520Snw141292 
17374520Snw141292 out:
17384644Sbaban 	_IDMAP_RESET_GET_HANDLE(gh);
17394520Snw141292 	(void) xdr_free(xdr_idmap_ids_res, (caddr_t)&res);
17404520Snw141292 	errno = idmap_stat2errno(retcode);
17414520Snw141292 	return (retcode);
17424520Snw141292 }
17434520Snw141292 
17444520Snw141292 
17454520Snw141292 /*
17464520Snw141292  * Destroy the "get mapping" handle
17474520Snw141292  */
17484520Snw141292 void
17495696Snw141292 idmap_get_destroy(idmap_get_handle_t *gh)
17505696Snw141292 {
17514520Snw141292 	if (gh == NULL)
17524520Snw141292 		return;
17534520Snw141292 	(void) xdr_free(xdr_idmap_mapping_batch, (caddr_t)&gh->batch);
17544520Snw141292 	if (gh->retlist)
17554520Snw141292 		free(gh->retlist);
17564520Snw141292 	free(gh);
17574520Snw141292 }
17584520Snw141292 
17594520Snw141292 
17604520Snw141292 /*
17614520Snw141292  * Get windows to unix mapping
17624520Snw141292  */
17634520Snw141292 idmap_stat
17644520Snw141292 idmap_get_w2u_mapping(idmap_handle_t *handle,
17654520Snw141292 		const char *sidprefix, idmap_rid_t *rid,
17664520Snw141292 		const char *winname, const char *windomain,
17675696Snw141292 		int flag, int *is_user, int *is_wuser,
17686386Sjp151216 		uid_t *pid, char **unixname, int *direction, idmap_info *info)
17695696Snw141292 {
17704520Snw141292 	CLIENT			*clnt;
17714520Snw141292 	enum clnt_stat		clntstat;
17724520Snw141292 	idmap_mapping		request, *mapping;
17734520Snw141292 	idmap_mappings_res	result;
17744520Snw141292 	idmap_retcode		retcode, rc;
17754520Snw141292 
17764520Snw141292 	if (handle == NULL) {
17774520Snw141292 		errno = EINVAL;
17784520Snw141292 		return (IDMAP_ERR_ARG);
17794520Snw141292 	}
17804520Snw141292 
17814520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
17824520Snw141292 
17834520Snw141292 	(void) memset(&request, 0, sizeof (request));
17844520Snw141292 	(void) memset(&result, 0, sizeof (result));
17854520Snw141292 
17864520Snw141292 	if (pid)
17874520Snw141292 		*pid = UINT32_MAX;
17884520Snw141292 	if (unixname)
17894520Snw141292 		*unixname = NULL;
17904520Snw141292 	if (direction)
17914644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
17924520Snw141292 
17934520Snw141292 	request.flag = flag;
17944520Snw141292 	request.id1.idtype = IDMAP_SID;
17954520Snw141292 	if (sidprefix && rid) {
17964520Snw141292 		request.id1.idmap_id_u.sid.prefix = (char *)sidprefix;
17974520Snw141292 		request.id1.idmap_id_u.sid.rid = *rid;
17984520Snw141292 	} else if (winname) {
17995064Sdm199847 		retcode = idmap_strdupnull(&request.id1name, winname);
18005247Sbaban 		if (retcode != IDMAP_SUCCESS)
18014520Snw141292 			goto out;
18025064Sdm199847 
18035064Sdm199847 		retcode = idmap_strdupnull(&request.id1domain, windomain);
18045247Sbaban 		if (retcode != IDMAP_SUCCESS)
18055064Sdm199847 			goto out;
18065064Sdm199847 
18074520Snw141292 		request.id1.idmap_id_u.sid.prefix = NULL;
18084520Snw141292 	} else {
18094520Snw141292 		errno = EINVAL;
18104520Snw141292 		return (IDMAP_ERR_ARG);
18114520Snw141292 	}
18124520Snw141292 
18135696Snw141292 	if (*is_user == 1)
18144520Snw141292 		request.id2.idtype = IDMAP_UID;
18154520Snw141292 	else if (*is_user == 0)
18164520Snw141292 		request.id2.idtype = IDMAP_GID;
18174520Snw141292 	else
18184520Snw141292 		request.id2.idtype = IDMAP_POSIXID;
18194520Snw141292 
18205696Snw141292 	if (*is_wuser == 1)
18215696Snw141292 		request.id1.idtype = IDMAP_USID;
18225696Snw141292 	else if (*is_wuser == 0)
18235696Snw141292 		request.id1.idtype = IDMAP_GSID;
18245696Snw141292 	else
18255696Snw141292 		request.id1.idtype = IDMAP_SID;
18265696Snw141292 
18274520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
18285696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
18295696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
18305696Snw141292 	    TIMEOUT);
18314520Snw141292 
18324644Sbaban 	if (clntstat != RPC_SUCCESS)
18334644Sbaban 		return (_idmap_rpc2stat(clnt));
18344520Snw141292 
18354520Snw141292 	retcode = result.retcode;
18364520Snw141292 
18374520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
18384520Snw141292 		if (retcode == IDMAP_SUCCESS)
18394520Snw141292 			retcode = IDMAP_ERR_NORESULT;
18404520Snw141292 		goto out;
18414520Snw141292 	}
18424520Snw141292 
18434864Sbaban 	if (mapping->id2.idtype == IDMAP_UID) {
18445696Snw141292 		*is_user = 1;
18454864Sbaban 	} else if (mapping->id2.idtype == IDMAP_GID) {
18465696Snw141292 		*is_user = 0;
18474864Sbaban 	} else {
18484864Sbaban 		goto out;
18494864Sbaban 	}
18505696Snw141292 
18515696Snw141292 	if (mapping->id1.idtype == IDMAP_USID) {
18525696Snw141292 		*is_wuser = 1;
18535696Snw141292 	} else if (mapping->id1.idtype == IDMAP_GSID) {
18545696Snw141292 		*is_wuser = 0;
18555696Snw141292 	} else {
18565696Snw141292 		goto out;
18575696Snw141292 	}
18585696Snw141292 
18594520Snw141292 	if (direction)
18604520Snw141292 		*direction = mapping->direction;
18614520Snw141292 	if (pid)
18624520Snw141292 		*pid = mapping->id2.idmap_id_u.uid;
18635064Sdm199847 
18645064Sdm199847 	rc = idmap_strdupnull(unixname, mapping->id2name);
18655064Sdm199847 	if (rc != IDMAP_SUCCESS)
18665064Sdm199847 		retcode = rc;
18674520Snw141292 
18686386Sjp151216 	rc = idmap_info_cpy(info, &mapping->info);
18696386Sjp151216 	if (rc != IDMAP_SUCCESS)
18706386Sjp151216 		retcode = rc;
18716386Sjp151216 
18724520Snw141292 out:
18736966Sjp151216 	if (request.id1name != NULL)
18746966Sjp151216 		free(request.id1name);
18756966Sjp151216 	if (request.id1domain != NULL)
18766966Sjp151216 		free(request.id1domain);
18774520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
18784520Snw141292 	if (retcode != IDMAP_SUCCESS)
18794520Snw141292 		errno = idmap_stat2errno(retcode);
18804520Snw141292 	return (retcode);
18814520Snw141292 }
18824520Snw141292 
18834520Snw141292 
18844520Snw141292 /*
18854520Snw141292  * Get unix to windows mapping
18864520Snw141292  */
18874520Snw141292 idmap_stat
18884520Snw141292 idmap_get_u2w_mapping(idmap_handle_t *handle,
18894520Snw141292 		uid_t *pid, const char *unixname,
18905696Snw141292 		int flag, int is_user, int *is_wuser,
18914520Snw141292 		char **sidprefix, idmap_rid_t *rid,
18924520Snw141292 		char **winname, char **windomain,
18936386Sjp151216 		int *direction, idmap_info *info)
18945696Snw141292 {
18954520Snw141292 	CLIENT			*clnt;
18964520Snw141292 	enum clnt_stat		clntstat;
18974520Snw141292 	idmap_mapping		request, *mapping;
18984520Snw141292 	idmap_mappings_res	result;
18994520Snw141292 	idmap_retcode		retcode, rc;
19004520Snw141292 
19014520Snw141292 	if (handle == NULL) {
19024520Snw141292 		errno = EINVAL;
19034520Snw141292 		return (IDMAP_ERR_ARG);
19044520Snw141292 	}
19054520Snw141292 
19064520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
19074520Snw141292 
19084520Snw141292 	if (sidprefix)
19094520Snw141292 		*sidprefix = NULL;
19104520Snw141292 	if (winname)
19114520Snw141292 		*winname = NULL;
19124520Snw141292 	if (windomain)
19134520Snw141292 		*windomain = NULL;
19144520Snw141292 	if (rid)
19154520Snw141292 		*rid = UINT32_MAX;
19164520Snw141292 	if (direction)
19174644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
19184520Snw141292 
19194520Snw141292 	(void) memset(&request, 0, sizeof (request));
19204520Snw141292 	(void) memset(&result, 0, sizeof (result));
19214520Snw141292 
19224520Snw141292 	request.flag = flag;
19234520Snw141292 	request.id1.idtype = is_user?IDMAP_UID:IDMAP_GID;
19244520Snw141292 
19254520Snw141292 	if (pid && *pid != UINT32_MAX) {
19264520Snw141292 		request.id1.idmap_id_u.uid = *pid;
19274520Snw141292 	} else if (unixname) {
19285064Sdm199847 		request.id1name = (char *)unixname;
19294520Snw141292 		request.id1.idmap_id_u.uid = UINT32_MAX;
19304520Snw141292 	} else {
19314520Snw141292 		errno = EINVAL;
19324520Snw141292 		return (IDMAP_ERR_ARG);
19334520Snw141292 	}
19344520Snw141292 
19355696Snw141292 	if (is_wuser == NULL)
19365696Snw141292 		request.id2.idtype = IDMAP_SID;
19375696Snw141292 	else if (*is_wuser == -1)
19385696Snw141292 		request.id2.idtype = IDMAP_SID;
19395696Snw141292 	else if (*is_wuser == 0)
19405696Snw141292 		request.id2.idtype = IDMAP_GSID;
19415696Snw141292 	else if (*is_wuser == 1)
19425696Snw141292 		request.id2.idtype = IDMAP_USID;
19434520Snw141292 
19444520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
19455696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
19465696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
19475696Snw141292 	    TIMEOUT);
19484520Snw141292 
19494644Sbaban 	if (clntstat != RPC_SUCCESS)
19504644Sbaban 		return (_idmap_rpc2stat(clnt));
19514520Snw141292 
19524520Snw141292 	retcode = result.retcode;
19534520Snw141292 
19544520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
19554520Snw141292 		if (retcode == IDMAP_SUCCESS)
19564520Snw141292 			retcode = IDMAP_ERR_NORESULT;
19574520Snw141292 		goto out;
19584520Snw141292 	}
19594520Snw141292 
19605696Snw141292 	if (direction != NULL)
19614520Snw141292 		*direction = mapping->direction;
19625696Snw141292 
19636386Sjp151216 	if (is_wuser != NULL) {
19646386Sjp151216 		if (mapping->id2.idtype == IDMAP_USID)
19656386Sjp151216 			*is_wuser = 1;
19666386Sjp151216 		else if (mapping->id2.idtype == IDMAP_GSID)
19676386Sjp151216 			*is_wuser = 0;
19686386Sjp151216 		else
19696386Sjp151216 			*is_wuser = -1;
19706386Sjp151216 	}
19715696Snw141292 
19724695Sbaban 	if (sidprefix && mapping->id2.idmap_id_u.sid.prefix &&
19734695Sbaban 	    *mapping->id2.idmap_id_u.sid.prefix != '\0') {
19744520Snw141292 		*sidprefix = strdup(mapping->id2.idmap_id_u.sid.prefix);
19754520Snw141292 		if (*sidprefix == NULL) {
19764520Snw141292 			retcode = IDMAP_ERR_MEMORY;
19774520Snw141292 			goto errout;
19784520Snw141292 		}
19794520Snw141292 	}
19804520Snw141292 	if (rid)
19814520Snw141292 		*rid = mapping->id2.idmap_id_u.sid.rid;
19825064Sdm199847 
19835064Sdm199847 	rc = idmap_strdupnull(winname, mapping->id2name);
19845064Sdm199847 	if (rc != IDMAP_SUCCESS)
19855064Sdm199847 		retcode = rc;
19865064Sdm199847 
19875064Sdm199847 	rc = idmap_strdupnull(windomain, mapping->id2domain);
19885064Sdm199847 	if (rc != IDMAP_SUCCESS)
19895064Sdm199847 		retcode = rc;
19904520Snw141292 
19916386Sjp151216 	rc = idmap_info_cpy(info, &mapping->info);
19926386Sjp151216 	if (rc != IDMAP_SUCCESS)
19936386Sjp151216 		retcode = rc;
19946386Sjp151216 
19954520Snw141292 	goto out;
19964520Snw141292 
19974520Snw141292 errout:
19984520Snw141292 	if (sidprefix && *sidprefix) {
19994520Snw141292 		free(*sidprefix);
20004520Snw141292 		*sidprefix = NULL;
20014520Snw141292 	}
20024520Snw141292 	if (winname && *winname) {
20034520Snw141292 		free(*winname);
20044520Snw141292 		*winname = NULL;
20054520Snw141292 	}
20064520Snw141292 	if (windomain && *windomain) {
20074520Snw141292 		free(*windomain);
20084520Snw141292 		*windomain = NULL;
20094520Snw141292 	}
20104520Snw141292 
20114520Snw141292 out:
20124520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
20134520Snw141292 	if (retcode != IDMAP_SUCCESS)
20144520Snw141292 		errno = idmap_stat2errno(retcode);
20154520Snw141292 	return (retcode);
20164520Snw141292 }
20174520Snw141292 
20184520Snw141292 
20194520Snw141292 
20204520Snw141292 #define	gettext(s)	s
20214520Snw141292 static stat_table_t stattable[] = {
20224520Snw141292 	{IDMAP_SUCCESS, gettext("Success"), 0},
20234520Snw141292 	{IDMAP_NEXT, gettext("More results available"), 0},
20244520Snw141292 	{IDMAP_ERR_OTHER, gettext("Undefined error"), EINVAL},
20254520Snw141292 	{IDMAP_ERR_INTERNAL, gettext("Internal error"), EINVAL},
20264520Snw141292 	{IDMAP_ERR_MEMORY, gettext("Out of memory"), ENOMEM},
20274520Snw141292 	{IDMAP_ERR_NORESULT, gettext("No results available"), EINVAL},
20284520Snw141292 	{IDMAP_ERR_NOTUSER, gettext("Not a user"), EINVAL},
20294520Snw141292 	{IDMAP_ERR_NOTGROUP, gettext("Not a group"), EINVAL},
20304644Sbaban 	{IDMAP_ERR_NOTSUPPORTED, gettext("Operation not supported"), ENOTSUP},
20314520Snw141292 	{IDMAP_ERR_W2U_NAMERULE,
20324520Snw141292 		gettext("Invalid Windows to UNIX name-based rule"), EINVAL},
20334520Snw141292 	{IDMAP_ERR_U2W_NAMERULE,
20344520Snw141292 		gettext("Invalid UNIX to Windows name-based rule"), EINVAL},
20354520Snw141292 	{IDMAP_ERR_CACHE, gettext("Invalid cache"), EINVAL},
20364520Snw141292 	{IDMAP_ERR_DB, gettext("Invalid database"), EINVAL},
20374520Snw141292 	{IDMAP_ERR_ARG, gettext("Invalid argument"), EINVAL},
20384520Snw141292 	{IDMAP_ERR_SID, gettext("Invalid SID"), EINVAL},
20394520Snw141292 	{IDMAP_ERR_IDTYPE, gettext("Invalid identity type"), EINVAL},
20404644Sbaban 	{IDMAP_ERR_RPC_HANDLE, gettext("Bad RPC handle"), EBADF},
20414520Snw141292 	{IDMAP_ERR_RPC, gettext("RPC error"), EINVAL},
20424520Snw141292 	{IDMAP_ERR_CLIENT_HANDLE, gettext("Bad client handle"), EINVAL},
20434644Sbaban 	{IDMAP_ERR_BUSY, gettext("Server is busy"), EBUSY},
20444695Sbaban 	{IDMAP_ERR_PERMISSION_DENIED, gettext("Permission denied"), EACCES},
20454520Snw141292 	{IDMAP_ERR_NOMAPPING,
20464520Snw141292 		gettext("Mapping not found or inhibited"), EINVAL},
20474520Snw141292 	{IDMAP_ERR_NEW_ID_ALLOC_REQD,
20484520Snw141292 		gettext("New mapping needs to be created"), EINVAL},
20494520Snw141292 	{IDMAP_ERR_DOMAIN, gettext("Invalid domain"), EINVAL},
20504520Snw141292 	{IDMAP_ERR_SECURITY, gettext("Security issue"), EINVAL},
20514520Snw141292 	{IDMAP_ERR_NOTFOUND, gettext("Not found"), EINVAL},
20524520Snw141292 	{IDMAP_ERR_DOMAIN_NOTFOUND, gettext("Domain not found"), EINVAL},
20534520Snw141292 	{IDMAP_ERR_UPDATE_NOTALLOWED, gettext("Update not allowed"), EINVAL},
20544520Snw141292 	{IDMAP_ERR_CFG, gettext("Configuration error"), EINVAL},
20554520Snw141292 	{IDMAP_ERR_CFG_CHANGE, gettext("Invalid configuration change"), EINVAL},
20564520Snw141292 	{IDMAP_ERR_NOTMAPPED_WELLKNOWN,
20574520Snw141292 		gettext("No mapping for well-known SID"), EINVAL},
20584520Snw141292 	{IDMAP_ERR_RETRIABLE_NET_ERR,
20594864Sbaban 		gettext("Windows lookup failed"), EINVAL},
20604864Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
20614864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
20624864Sbaban 		"Windows to UNIX name-based rule"), EINVAL},
20634864Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
20644864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
20654864Sbaban 		"Unix to Windows name-based rule"), EINVAL},
20665968Snw141292 	{IDMAP_ERR_BAD_UTF8,
20675968Snw141292 		gettext("Invalid or illegal UTF-8 sequence found in "
20685968Snw141292 		"a given Windows entity name or domain name"), EINVAL},
20696386Sjp151216 	{IDMAP_ERR_NONEGENERATED,
20706386Sjp151216 		gettext("Mapping not found and none created (see -c option)"),
20716386Sjp151216 		EINVAL},
20726616Sdm199847 	{IDMAP_ERR_PROP_UNKNOWN,
20736616Sdm199847 		gettext("Undefined property"),
20746616Sdm199847 		EINVAL},
20756616Sdm199847 	{IDMAP_ERR_NS_LDAP_CFG,
20766616Sdm199847 		gettext("Native LDAP configuration error"), EINVAL},
20776616Sdm199847 	{IDMAP_ERR_NS_LDAP_PARTIAL,
20786616Sdm199847 		gettext("Partial result from Native LDAP"), EINVAL},
20796616Sdm199847 	{IDMAP_ERR_NS_LDAP_OP_FAILED,
20806616Sdm199847 		gettext("Native LDAP operation failed"), EINVAL},
20816616Sdm199847 	{IDMAP_ERR_NS_LDAP_BAD_WINNAME,
20826616Sdm199847 		gettext("Improper winname form found in Native LDAP"), EINVAL},
20834520Snw141292 	{-1, NULL, 0}
20844520Snw141292 };
20854520Snw141292 #undef	gettext
20864520Snw141292 
20874520Snw141292 
20884520Snw141292 /*
20894520Snw141292  * Get description of status code
20904520Snw141292  *
20914520Snw141292  * Input:
20924520Snw141292  * status - Status code returned by libidmap API call
20934520Snw141292  *
20944520Snw141292  * Return Value:
20954520Snw141292  * human-readable localized description of idmap_stat
20964520Snw141292  */
20974520Snw141292 /* ARGSUSED */
20984520Snw141292 const char *
20995696Snw141292 idmap_stat2string(idmap_handle_t *handle, idmap_stat status)
21005696Snw141292 {
21014520Snw141292 	int i;
21024520Snw141292 
21034520Snw141292 	for (i = 0; stattable[i].msg; i++) {
21044520Snw141292 		if (stattable[i].retcode == status)
21054526Sbaban 			return (gettext(stattable[i].msg));
21064520Snw141292 	}
21074520Snw141292 	return (gettext("Unknown error"));
21084520Snw141292 }
21094520Snw141292 
21104520Snw141292 
21114520Snw141292 static int
21125696Snw141292 idmap_stat2errno(idmap_stat stat)
21135696Snw141292 {
21144520Snw141292 	int i;
21154520Snw141292 	for (i = 0; stattable[i].msg; i++) {
21164520Snw141292 		if (stattable[i].retcode == stat)
21174520Snw141292 			return (stattable[i].errnum);
21184520Snw141292 	}
21194520Snw141292 	return (EINVAL);
21204520Snw141292 }
21214520Snw141292 
21224520Snw141292 
21234520Snw141292 /*
21244520Snw141292  * Get status code from string
21254520Snw141292  */
21264520Snw141292 idmap_stat
21275696Snw141292 idmap_string2stat(const char *str)
21285696Snw141292 {
21294520Snw141292 	if (str == NULL)
21304520Snw141292 		return (IDMAP_ERR_INTERNAL);
21314520Snw141292 
21324520Snw141292 #define	return_cmp(a) \
21334520Snw141292 	if (0 == strcmp(str, "IDMAP_ERR_" #a)) \
21344520Snw141292 		return (IDMAP_ERR_ ## a);
21354520Snw141292 
21364520Snw141292 	return_cmp(OTHER);
21374520Snw141292 	return_cmp(INTERNAL);
21384520Snw141292 	return_cmp(MEMORY);
21394520Snw141292 	return_cmp(NORESULT);
21404520Snw141292 	return_cmp(NOTUSER);
21414520Snw141292 	return_cmp(NOTGROUP);
21424520Snw141292 	return_cmp(NOTSUPPORTED);
21434520Snw141292 	return_cmp(W2U_NAMERULE);
21444520Snw141292 	return_cmp(U2W_NAMERULE);
21454520Snw141292 	return_cmp(CACHE);
21464520Snw141292 	return_cmp(DB);
21474520Snw141292 	return_cmp(ARG);
21484520Snw141292 	return_cmp(SID);
21494520Snw141292 	return_cmp(IDTYPE);
21504520Snw141292 	return_cmp(RPC_HANDLE);
21514520Snw141292 	return_cmp(RPC);
21524520Snw141292 	return_cmp(CLIENT_HANDLE);
21534520Snw141292 	return_cmp(BUSY);
21544520Snw141292 	return_cmp(PERMISSION_DENIED);
21554520Snw141292 	return_cmp(NOMAPPING);
21564520Snw141292 	return_cmp(NEW_ID_ALLOC_REQD);
21574520Snw141292 	return_cmp(DOMAIN);
21584520Snw141292 	return_cmp(SECURITY);
21594520Snw141292 	return_cmp(NOTFOUND);
21604520Snw141292 	return_cmp(DOMAIN_NOTFOUND);
21614520Snw141292 	return_cmp(MEMORY);
21624520Snw141292 	return_cmp(UPDATE_NOTALLOWED);
21634520Snw141292 	return_cmp(CFG);
21644520Snw141292 	return_cmp(CFG_CHANGE);
21654520Snw141292 	return_cmp(NOTMAPPED_WELLKNOWN);
21664520Snw141292 	return_cmp(RETRIABLE_NET_ERR);
21674864Sbaban 	return_cmp(W2U_NAMERULE_CONFLICT);
21684864Sbaban 	return_cmp(U2W_NAMERULE_CONFLICT);
21696616Sdm199847 	return_cmp(BAD_UTF8);
21706616Sdm199847 	return_cmp(NONEGENERATED);
21716616Sdm199847 	return_cmp(PROP_UNKNOWN);
21726616Sdm199847 	return_cmp(NS_LDAP_CFG);
21736616Sdm199847 	return_cmp(NS_LDAP_PARTIAL);
21746616Sdm199847 	return_cmp(NS_LDAP_OP_FAILED);
21756616Sdm199847 	return_cmp(NS_LDAP_BAD_WINNAME);
21764520Snw141292 #undef return_cmp
21774520Snw141292 
21784520Snw141292 	return (IDMAP_ERR_OTHER);
21794520Snw141292 }
21804520Snw141292 
21814520Snw141292 
21824520Snw141292 /*
21834520Snw141292  * Map the given status to one that can be returned by the protocol
21844520Snw141292  */
21854520Snw141292 idmap_stat
21865696Snw141292 idmap_stat4prot(idmap_stat status)
21875696Snw141292 {
21884520Snw141292 	switch (status) {
21894520Snw141292 	case IDMAP_ERR_MEMORY:
21904520Snw141292 	case IDMAP_ERR_CACHE:
21914520Snw141292 		return (IDMAP_ERR_INTERNAL);
21924520Snw141292 	}
21934520Snw141292 	return (status);
21944520Snw141292 }
21955043Sbaban 
21965043Sbaban 
21975043Sbaban /*
21985247Sbaban  * This is a convenience routine which duplicates a string after
21995247Sbaban  * checking for NULL pointers. This function will return success if
22005247Sbaban  * either the 'to' OR 'from' pointers are NULL.
22015064Sdm199847  */
22025064Sdm199847 static idmap_stat
22035696Snw141292 idmap_strdupnull(char **to, const char *from)
22045696Snw141292 {
22055247Sbaban 	if (to == NULL)
22065247Sbaban 		return (IDMAP_SUCCESS);
22075247Sbaban 
22085064Sdm199847 	if (from == NULL || *from == '\0') {
22095064Sdm199847 		*to = NULL;
22105064Sdm199847 		return (IDMAP_SUCCESS);
22115064Sdm199847 	}
22125064Sdm199847 
22135064Sdm199847 	*to = strdup(from);
22145064Sdm199847 	if (*to == NULL)
22155064Sdm199847 		return (IDMAP_ERR_MEMORY);
22165064Sdm199847 	return (IDMAP_SUCCESS);
22175064Sdm199847 }
22185064Sdm199847 
22196386Sjp151216 
22205064Sdm199847 idmap_stat
22215696Snw141292 idmap_namerule_cpy(idmap_namerule *to, idmap_namerule *from)
22225696Snw141292 {
22235064Sdm199847 	idmap_stat retval;
22245064Sdm199847 
22256386Sjp151216 	if (to == NULL)
22266386Sjp151216 		return (IDMAP_SUCCESS);
22276386Sjp151216 
22285064Sdm199847 	(void) memcpy(to, from, sizeof (idmap_namerule));
22296386Sjp151216 	to->windomain = NULL;
22306386Sjp151216 	to->winname = NULL;
22316386Sjp151216 	to->unixname = NULL;
22325064Sdm199847 
22335064Sdm199847 	retval = idmap_strdupnull(&to->windomain, from->windomain);
22345064Sdm199847 	if (retval != IDMAP_SUCCESS)
22355064Sdm199847 		return (retval);
22365064Sdm199847 
22375064Sdm199847 	retval = idmap_strdupnull(&to->winname, from->winname);
22386386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22396386Sjp151216 		free(to->windomain);
22406386Sjp151216 		to->windomain = NULL;
22416386Sjp151216 		return (retval);
22426386Sjp151216 	}
22436386Sjp151216 
22446386Sjp151216 	retval = idmap_strdupnull(&to->unixname, from->unixname);
22456386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22466386Sjp151216 		free(to->windomain);
22476386Sjp151216 		to->windomain = NULL;
22486386Sjp151216 		free(to->winname);
22496386Sjp151216 		to->winname = NULL;
22506386Sjp151216 		return (retval);
22516386Sjp151216 	}
22526386Sjp151216 
22536386Sjp151216 	return (retval);
22546386Sjp151216 }
22556386Sjp151216 
22566386Sjp151216 
22576386Sjp151216 static
22586386Sjp151216 idmap_stat
22596386Sjp151216 idmap_how_ds_based_cpy(idmap_how_ds_based *to, idmap_how_ds_based *from)
22606386Sjp151216 {
22616386Sjp151216 	idmap_stat retval;
22626386Sjp151216 
22636386Sjp151216 	if (to == NULL)
22646386Sjp151216 		return (IDMAP_SUCCESS);
22656386Sjp151216 
22666386Sjp151216 	retval = idmap_strdupnull(&to->dn, from->dn);
22675064Sdm199847 	if (retval != IDMAP_SUCCESS)
22685064Sdm199847 		return (retval);
22695064Sdm199847 
22706386Sjp151216 	retval = idmap_strdupnull(&to->attr, from->attr);
22716386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22726386Sjp151216 		free(to->dn);
22736386Sjp151216 		to->dn = NULL;
22746386Sjp151216 		return (retval);
22756386Sjp151216 	}
22766386Sjp151216 
22776386Sjp151216 	retval = idmap_strdupnull(&to->value, from->value);
22786386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22796386Sjp151216 		free(to->dn);
22806386Sjp151216 		to->dn = NULL;
22816386Sjp151216 		free(to->attr);
22826386Sjp151216 		to->attr = NULL;
22836386Sjp151216 		return (retval);
22846386Sjp151216 	}
22855064Sdm199847 
22865064Sdm199847 	return (retval);
22875064Sdm199847 }
22885064Sdm199847 
22895064Sdm199847 
22906386Sjp151216 idmap_stat
22916386Sjp151216 idmap_info_cpy(idmap_info *to, idmap_info *from)
22926386Sjp151216 {
22936386Sjp151216 	idmap_stat retval = IDMAP_SUCCESS;
22946386Sjp151216 
22956386Sjp151216 	if (to == NULL)
22966386Sjp151216 		return (IDMAP_SUCCESS);
22976386Sjp151216 
22986386Sjp151216 	(void) memset(to, 0, sizeof (idmap_info));
22996386Sjp151216 
23006386Sjp151216 	to->src = from->src;
23016386Sjp151216 	to->how.map_type = from->how.map_type;
23026386Sjp151216 	switch (to->how.map_type) {
23036386Sjp151216 	case IDMAP_MAP_TYPE_DS_AD:
23046386Sjp151216 		retval = idmap_how_ds_based_cpy(&to->how.idmap_how_u.ad,
23056386Sjp151216 		    &from->how.idmap_how_u.ad);
23066386Sjp151216 		break;
23076386Sjp151216 
23086386Sjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
23096386Sjp151216 		retval = idmap_how_ds_based_cpy(&to->how.idmap_how_u.nldap,
23106386Sjp151216 		    &from->how.idmap_how_u.nldap);
23116386Sjp151216 		break;
23126386Sjp151216 
23136386Sjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
23146386Sjp151216 		retval = idmap_namerule_cpy(&to->how.idmap_how_u.rule,
23156386Sjp151216 		    &from->how.idmap_how_u.rule);
23166386Sjp151216 		break;
23176386Sjp151216 
23186386Sjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
23196386Sjp151216 		break;
23206386Sjp151216 
23216386Sjp151216 	case IDMAP_MAP_TYPE_LOCAL_SID:
23226386Sjp151216 		break;
23236386Sjp151216 
23246386Sjp151216 	case IDMAP_MAP_TYPE_KNOWN_SID:
23256386Sjp151216 		break;
23266386Sjp151216 	}
23276386Sjp151216 	return (retval);
23286386Sjp151216 }
23296386Sjp151216 
23306386Sjp151216 
23316386Sjp151216 /*
23326386Sjp151216  * This routine is similar to idmap_info_cpy, but the strings
23336386Sjp151216  * are moved from the "from" info to the "to" info.
23346386Sjp151216  * This routine is equivelent of:
23356386Sjp151216  *
23366386Sjp151216  *	idmap_info_cpy(to,from);
23376386Sjp151216  *	idmap_info_free(from);
23386386Sjp151216  */
23396386Sjp151216 idmap_stat
23406386Sjp151216 idmap_info_mov(idmap_info *to, idmap_info *from)
23416386Sjp151216 {
23426386Sjp151216 	idmap_stat retval = IDMAP_SUCCESS;
23436386Sjp151216 
23446386Sjp151216 	if (to == NULL) {
23456386Sjp151216 		idmap_info_free(from);
23466386Sjp151216 		return (IDMAP_SUCCESS);
23476386Sjp151216 	}
23486386Sjp151216 	(void) memcpy(to, from, sizeof (idmap_info));
23496386Sjp151216 
23506386Sjp151216 	(void) memset(from, 0, sizeof (idmap_info));
23516386Sjp151216 
23526386Sjp151216 	return (retval);
23536386Sjp151216 }
23546386Sjp151216 
23556386Sjp151216 
23566386Sjp151216 void
23576386Sjp151216 idmap_info_free(idmap_info *info)
23586386Sjp151216 {
23596386Sjp151216 	idmap_how *how;
23606386Sjp151216 
23616386Sjp151216 	if (info == NULL)
23626386Sjp151216 		return;
23636386Sjp151216 
23646386Sjp151216 	how = &info->how;
23656386Sjp151216 	switch (how->map_type) {
23666386Sjp151216 	case IDMAP_MAP_TYPE_DS_AD:
23676386Sjp151216 		free(how->idmap_how_u.ad.dn);
23686386Sjp151216 		how->idmap_how_u.ad.dn = NULL;
23696386Sjp151216 		free(how->idmap_how_u.ad.attr);
23706386Sjp151216 		how->idmap_how_u.ad.attr = NULL;
23716386Sjp151216 		free(how->idmap_how_u.ad.value);
23726386Sjp151216 		how->idmap_how_u.ad.value = NULL;
23736386Sjp151216 		break;
23746386Sjp151216 
23756386Sjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
23766386Sjp151216 		free(how->idmap_how_u.nldap.dn);
23776386Sjp151216 		how->idmap_how_u.nldap.dn = NULL;
23786386Sjp151216 		free(how->idmap_how_u.nldap.attr);
23796386Sjp151216 		how->idmap_how_u.nldap.attr = NULL;
23806386Sjp151216 		free(how->idmap_how_u.nldap.value);
23816386Sjp151216 		how->idmap_how_u.nldap.value = NULL;
23826386Sjp151216 		break;
23836386Sjp151216 
23846386Sjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
23856386Sjp151216 		free(how->idmap_how_u.rule.windomain);
23866386Sjp151216 		how->idmap_how_u.rule.windomain = NULL;
23876386Sjp151216 		free(how->idmap_how_u.rule.winname);
23886386Sjp151216 		how->idmap_how_u.rule.winname = NULL;
23896386Sjp151216 		free(how->idmap_how_u.rule.unixname);
23906386Sjp151216 		how->idmap_how_u.rule.unixname = NULL;
23916386Sjp151216 		break;
23926386Sjp151216 
23936386Sjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
23946386Sjp151216 		break;
23956386Sjp151216 
23966386Sjp151216 	case IDMAP_MAP_TYPE_LOCAL_SID:
23976386Sjp151216 		break;
23986386Sjp151216 	}
23996386Sjp151216 	how->map_type = IDMAP_MAP_TYPE_UNKNOWN;
24006386Sjp151216 	info->src = IDMAP_MAP_SRC_UNKNOWN;
24016386Sjp151216 }
24026386Sjp151216 
24036386Sjp151216 
24045064Sdm199847 /*
24055043Sbaban  * Get uid given Windows name
24065043Sbaban  */
24075043Sbaban idmap_stat
24087369SJulian.Pullen@Sun.COM idmap_getuidbywinname(const char *name, const char *domain, int flag,
24097369SJulian.Pullen@Sun.COM 	uid_t *uid)
24105696Snw141292 {
24115043Sbaban 	idmap_handle_t	*ih;
24125043Sbaban 	idmap_retcode	rc;
24135696Snw141292 	int		is_user = 1;
24145696Snw141292 	int		is_wuser = -1;
24157369SJulian.Pullen@Sun.COM 	int 		direction;
24165043Sbaban 
24175043Sbaban 	if (uid == NULL)
24185043Sbaban 		return (IDMAP_ERR_ARG);
24195043Sbaban 
24207369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
24217369SJulian.Pullen@Sun.COM 		rc = idmap_cache_lookup_uidbywinname(name, domain, uid);
24227369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
24237369SJulian.Pullen@Sun.COM 			return (rc);
24247369SJulian.Pullen@Sun.COM 	}
24255043Sbaban 	/* Get mapping */
24265043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
24275043Sbaban 		return (rc);
24287369SJulian.Pullen@Sun.COM 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, flag,
24297369SJulian.Pullen@Sun.COM 	    &is_user, &is_wuser, uid, NULL, &direction, NULL);
24305043Sbaban 	(void) idmap_fini(ih);
24315043Sbaban 
24327369SJulian.Pullen@Sun.COM 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
24337369SJulian.Pullen@Sun.COM 		/* If we have not got the domain don't store UID to winname */
24347369SJulian.Pullen@Sun.COM 		if (domain == NULL)
24357369SJulian.Pullen@Sun.COM 			direction = IDMAP_DIRECTION_W2U;
24367369SJulian.Pullen@Sun.COM 		idmap_cache_add_winname2uid(name, domain, *uid, direction);
24377369SJulian.Pullen@Sun.COM 	}
24387369SJulian.Pullen@Sun.COM 
24395043Sbaban 	return (rc);
24405043Sbaban }
24415043Sbaban 
24425043Sbaban 
24435043Sbaban /*
24445043Sbaban  * Get gid given Windows name
24455043Sbaban  */
24465043Sbaban idmap_stat
24477369SJulian.Pullen@Sun.COM idmap_getgidbywinname(const char *name, const char *domain, int flag,
24487369SJulian.Pullen@Sun.COM 	gid_t *gid)
24495696Snw141292 {
24505043Sbaban 	idmap_handle_t	*ih;
24515043Sbaban 	idmap_retcode	rc;
24525696Snw141292 	int		is_user = 0;
24535696Snw141292 	int		is_wuser = -1;
24547369SJulian.Pullen@Sun.COM 	int		direction;
24555043Sbaban 
24565043Sbaban 	if (gid == NULL)
24575043Sbaban 		return (IDMAP_ERR_ARG);
24585043Sbaban 
24597369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
24607369SJulian.Pullen@Sun.COM 		rc = idmap_cache_lookup_gidbywinname(name, domain, gid);
24617369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
24627369SJulian.Pullen@Sun.COM 			return (rc);
24637369SJulian.Pullen@Sun.COM 	}
24647369SJulian.Pullen@Sun.COM 
24655043Sbaban 	/* Get mapping */
24665043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
24675043Sbaban 		return (rc);
24687369SJulian.Pullen@Sun.COM 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, flag,
24697369SJulian.Pullen@Sun.COM 	    &is_user, &is_wuser, gid, NULL, &direction, NULL);
24705043Sbaban 	(void) idmap_fini(ih);
24715043Sbaban 
24727369SJulian.Pullen@Sun.COM 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
24737369SJulian.Pullen@Sun.COM 		/* If we have not got the domain don't store GID to winname */
24747369SJulian.Pullen@Sun.COM 		if (domain == NULL)
24757369SJulian.Pullen@Sun.COM 			direction = IDMAP_DIRECTION_W2U;
24767369SJulian.Pullen@Sun.COM 		idmap_cache_add_winname2gid(name, domain, *gid, direction);
24777369SJulian.Pullen@Sun.COM 	}
24787369SJulian.Pullen@Sun.COM 
24795043Sbaban 	return (rc);
24805043Sbaban }
24815043Sbaban 
24825043Sbaban 
24835043Sbaban /*
24845043Sbaban  * Get winname given pid
24855043Sbaban  */
24865043Sbaban static idmap_retcode
24877369SJulian.Pullen@Sun.COM idmap_getwinnamebypid(uid_t pid, int is_user, int flag, char **name,
24887369SJulian.Pullen@Sun.COM 	char **domain)
24895696Snw141292 {
24905043Sbaban 	idmap_handle_t	*ih;
24915043Sbaban 	idmap_retcode	rc;
24925043Sbaban 	int		len;
24935043Sbaban 	char		*winname, *windomain;
24947369SJulian.Pullen@Sun.COM 	int		direction;
24955043Sbaban 
24965043Sbaban 	if (name == NULL)
24975043Sbaban 		return (IDMAP_ERR_ARG);
24985043Sbaban 
24997369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
25007369SJulian.Pullen@Sun.COM 		if (is_user)
25017369SJulian.Pullen@Sun.COM 			rc = idmap_cache_lookup_winnamebyuid(&winname,
25027369SJulian.Pullen@Sun.COM 			    &windomain, pid);
25037369SJulian.Pullen@Sun.COM 		else
25047369SJulian.Pullen@Sun.COM 			rc = idmap_cache_lookup_winnamebygid(&winname,
25057369SJulian.Pullen@Sun.COM 			    &windomain, pid);
25067369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS)
25077369SJulian.Pullen@Sun.COM 			goto out;
25087369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_ERR_MEMORY)
25097369SJulian.Pullen@Sun.COM 			return (rc);
25107369SJulian.Pullen@Sun.COM 	}
25117369SJulian.Pullen@Sun.COM 
25125043Sbaban 	/* Get mapping */
25135043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
25145043Sbaban 		return (rc);
25157369SJulian.Pullen@Sun.COM 	rc = idmap_get_u2w_mapping(ih, &pid, NULL, flag, is_user, NULL,
25167369SJulian.Pullen@Sun.COM 	    NULL, NULL, &winname, &windomain, &direction, NULL);
25175043Sbaban 	(void) idmap_fini(ih);
25185043Sbaban 
25195043Sbaban 	/* Return on error */
25205043Sbaban 	if (rc != IDMAP_SUCCESS)
25215043Sbaban 		return (rc);
25225043Sbaban 
25235043Sbaban 	/*
25245043Sbaban 	 * The given PID may have been mapped to a locally
25255043Sbaban 	 * generated SID in which case there isn't any
25265043Sbaban 	 * Windows name
25275043Sbaban 	 */
25285043Sbaban 	if (winname == NULL || windomain == NULL) {
25295043Sbaban 		idmap_free(winname);
25305043Sbaban 		idmap_free(windomain);
25315043Sbaban 		return (IDMAP_ERR_NORESULT);
25325043Sbaban 	}
25335043Sbaban 
25347369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
25357369SJulian.Pullen@Sun.COM 		if (is_user)
25367369SJulian.Pullen@Sun.COM 			idmap_cache_add_winname2uid(winname, windomain,
25377369SJulian.Pullen@Sun.COM 			    pid, direction);
25387369SJulian.Pullen@Sun.COM 		else
25397369SJulian.Pullen@Sun.COM 			idmap_cache_add_winname2gid(winname, windomain,
25407369SJulian.Pullen@Sun.COM 			    pid, direction);
25417369SJulian.Pullen@Sun.COM 	}
25427369SJulian.Pullen@Sun.COM 
25437369SJulian.Pullen@Sun.COM out:
25445043Sbaban 	if (domain != NULL) {
25455043Sbaban 		*name = winname;
25465043Sbaban 		*domain = windomain;
25475043Sbaban 	} else {
25485043Sbaban 		len = strlen(winname) + strlen(windomain) + 2;
25495043Sbaban 		if ((*name = malloc(len)) != NULL)
25505043Sbaban 			(void) snprintf(*name, len, "%s@%s", winname,
25515043Sbaban 			    windomain);
25525043Sbaban 		else
25535043Sbaban 			rc = IDMAP_ERR_MEMORY;
25545043Sbaban 		idmap_free(winname);
25555043Sbaban 		idmap_free(windomain);
25565043Sbaban 	}
25577369SJulian.Pullen@Sun.COM 
25585043Sbaban 	return (rc);
25595043Sbaban }
25605043Sbaban 
25615043Sbaban 
25625043Sbaban /*
25635043Sbaban  * Get winname given uid
25645043Sbaban  */
25655043Sbaban idmap_stat
25667369SJulian.Pullen@Sun.COM idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
25675696Snw141292 {
25687369SJulian.Pullen@Sun.COM 	return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
25695043Sbaban }
25705043Sbaban 
25715043Sbaban 
25725043Sbaban /*
25735043Sbaban  * Get winname given gid
25745043Sbaban  */
25755043Sbaban idmap_stat
25767369SJulian.Pullen@Sun.COM idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
25775696Snw141292 {
25787369SJulian.Pullen@Sun.COM 	return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
25795043Sbaban }
25806616Sdm199847 
25816616Sdm199847 
25826616Sdm199847 /* printflike */
25836616Sdm199847 void
25846616Sdm199847 idmapdlog(int pri, const char *format, ...) {
25856616Sdm199847 	va_list args;
25866616Sdm199847 
25876616Sdm199847 	va_start(args, format);
25886616Sdm199847 	if (pri <= logstate.max_pri) {
25896616Sdm199847 		(void) vfprintf(stderr, format, args);
25906616Sdm199847 		(void) fprintf(stderr, "\n");
25916616Sdm199847 	}
25926616Sdm199847 
25936616Sdm199847 	/*
25946616Sdm199847 	 * We don't want to fill up the logs with useless messages when
25956616Sdm199847 	 * we're degraded, but we still want to log.
25966616Sdm199847 	 */
25976616Sdm199847 	if (logstate.degraded)
25986616Sdm199847 		pri = LOG_DEBUG;
25996616Sdm199847 
26006616Sdm199847 	if (logstate.write_syslog)
26016616Sdm199847 		(void) vsyslog(pri, format, args);
26026616Sdm199847 	va_end(args);
26036616Sdm199847 }
26046616Sdm199847 
26056616Sdm199847 void
26066616Sdm199847 idmap_log_stderr(int pri)
26076616Sdm199847 {
26086616Sdm199847 	logstate.max_pri = pri;
26096616Sdm199847 }
26106616Sdm199847 
26116616Sdm199847 void
26126616Sdm199847 idmap_log_syslog(bool_t what)
26136616Sdm199847 {
26146616Sdm199847 	logstate.write_syslog = what;
26156616Sdm199847 }
26166616Sdm199847 
26176616Sdm199847 void
26186616Sdm199847 idmap_log_degraded(bool_t what)
26196616Sdm199847 {
26206616Sdm199847 	logstate.degraded = what;
26216616Sdm199847 }
2622