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 
1278040SBaban.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 
1668040SBaban.Kenkre@Sun.COM 		if (sendsz <= MIN_STACK_NEEDS) {
1675232Snw141292 			sendsz = 0;	/* RPC call may fail */
1688040SBaban.Kenkre@Sun.COM 		} else {
1698040SBaban.Kenkre@Sun.COM 			/* Leave 64Kb (just a guess) for our needs */
1708040SBaban.Kenkre@Sun.COM 			sendsz -= MIN_STACK_NEEDS;
1718040SBaban.Kenkre@Sun.COM 
1728040SBaban.Kenkre@Sun.COM 			/* Divide the stack space left by two */
1738040SBaban.Kenkre@Sun.COM 			sendsz = RNDUP(sendsz / 2);
1748040SBaban.Kenkre@Sun.COM 
1758040SBaban.Kenkre@Sun.COM 			/* Limit sendsz to 256KB */
1768040SBaban.Kenkre@Sun.COM 			if (sendsz > IDMAP_MAX_DOOR_RPC)
1778040SBaban.Kenkre@Sun.COM 				sendsz = IDMAP_MAX_DOOR_RPC;
1788040SBaban.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 
227*8361SJulian.Pullen@Sun.COM static 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 
248*8361SJulian.Pullen@Sun.COM 
2496616Sdm199847 idmap_stat
2506616Sdm199847 idmap_get_prop_ds(idmap_handle_t *handle, idmap_prop_type pr,
2516616Sdm199847     idmap_ad_disc_ds_t *dc)
2526616Sdm199847 {
2536616Sdm199847 	idmap_prop_res res;
2546616Sdm199847 	idmap_stat rc = IDMAP_SUCCESS;
2556616Sdm199847 
2566616Sdm199847 	rc = idmap_get_prop(handle, pr, &res);
2576616Sdm199847 	if (rc < 0)
2586616Sdm199847 		return (rc);
2596616Sdm199847 
2606616Sdm199847 	dc->port = res.value.idmap_prop_val_u.dsval.port;
2616616Sdm199847 	(void) strlcpy(dc->host, res.value.idmap_prop_val_u.dsval.host,
2626616Sdm199847 	    AD_DISC_MAXHOSTNAME);
2636616Sdm199847 
2646616Sdm199847 	/* xdr doesn't guarantee 0-termination of char[]: */
2656616Sdm199847 	dc->host[AD_DISC_MAXHOSTNAME - 1] = '\0';
2666616Sdm199847 
2676616Sdm199847 	return (rc);
2686616Sdm199847 }
2696616Sdm199847 
2706616Sdm199847 
2716616Sdm199847 /*
2726616Sdm199847  * Sometimes the property is not set. In that case, str is set to NULL but
2736616Sdm199847  * otherwise IDMAP_SUCCESS is returned.
2746616Sdm199847  */
2756616Sdm199847 idmap_stat
2766616Sdm199847 idmap_get_prop_str(idmap_handle_t *handle, idmap_prop_type pr, char **str)
2776616Sdm199847 {
2786616Sdm199847 	idmap_prop_res res;
2796616Sdm199847 	idmap_stat rc = IDMAP_SUCCESS;
2806616Sdm199847 
2816616Sdm199847 	rc = idmap_get_prop(handle, pr, &res);
2826616Sdm199847 	if (rc < 0)
2836616Sdm199847 		return (rc);
2846616Sdm199847 
2856616Sdm199847 	rc = idmap_strdupnull(str, res.value.idmap_prop_val_u.utf8val);
2866616Sdm199847 	return (rc);
2876616Sdm199847 }
2884520Snw141292 
2894520Snw141292 /*
2904520Snw141292  * Create/Initialize handle for updates
2914520Snw141292  *
2924520Snw141292  * Output:
2934520Snw141292  * udthandle - update handle
2944520Snw141292  */
2954520Snw141292 idmap_stat
2965696Snw141292 idmap_udt_create(idmap_handle_t *handle, idmap_udt_handle_t **udthandle)
2975696Snw141292 {
2984520Snw141292 	idmap_udt_handle_t	*tmp;
2994520Snw141292 
3004520Snw141292 	if (handle == NULL || udthandle == NULL) {
3014520Snw141292 		errno = EINVAL;
3024520Snw141292 		return (IDMAP_ERR_ARG);
3034520Snw141292 	}
3044520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
3054520Snw141292 		errno = ENOMEM;
3064520Snw141292 		return (IDMAP_ERR_MEMORY);
3074520Snw141292 	}
3084520Snw141292 
3094520Snw141292 	tmp->ih = handle;
3104520Snw141292 	*udthandle = tmp;
3114520Snw141292 	return (IDMAP_SUCCESS);
3124520Snw141292 }
3134520Snw141292 
3144520Snw141292 
3154520Snw141292 /*
3164520Snw141292  * All the updates specified by the update handle are committed
3174520Snw141292  * in a single transaction. i.e either all succeed or none.
3184520Snw141292  *
3194520Snw141292  * Input:
3204520Snw141292  * udthandle - update handle with the update requests
3214520Snw141292  *
3224520Snw141292  * Return value:
3234520Snw141292  * Status of the commit
3244520Snw141292  */
3254520Snw141292 idmap_stat
3265696Snw141292 idmap_udt_commit(idmap_udt_handle_t *udthandle)
3275696Snw141292 {
3284520Snw141292 	CLIENT			*clnt;
3294520Snw141292 	enum clnt_stat		clntstat;
3305064Sdm199847 	idmap_update_res	res;
3315064Sdm199847 	idmap_stat		retcode;
3324520Snw141292 
3334520Snw141292 	if (udthandle == NULL) {
3344520Snw141292 		errno = EINVAL;
3354520Snw141292 		return (IDMAP_ERR_ARG);
3364520Snw141292 	}
3375064Sdm199847 
3385064Sdm199847 	(void) memset(&res, 0, sizeof (res));
3395064Sdm199847 
3404520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(udthandle->ih, clnt);
3414520Snw141292 	clntstat = clnt_call(clnt, IDMAP_UPDATE,
3425696Snw141292 	    (xdrproc_t)xdr_idmap_update_batch, (caddr_t)&udthandle->batch,
3435696Snw141292 	    (xdrproc_t)xdr_idmap_update_res, (caddr_t)&res,
3445696Snw141292 	    TIMEOUT);
3454644Sbaban 
3465064Sdm199847 	if (clntstat != RPC_SUCCESS) {
3475064Sdm199847 		retcode = _idmap_rpc2stat(clnt);
3485064Sdm199847 		goto out;
3495064Sdm199847 	}
3505064Sdm199847 
3515064Sdm199847 	retcode = udthandle->commit_stat = res.retcode;
3525064Sdm199847 	udthandle->error_index = res.error_index;
3535064Sdm199847 
3545064Sdm199847 	if (retcode != IDMAP_SUCCESS) {
3555064Sdm199847 
3565064Sdm199847 		if (udthandle->error_index < 0)
3575064Sdm199847 			goto out;
3585064Sdm199847 
3595064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->error_rule,
3605064Sdm199847 		    &res.error_rule);
3615064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
3625064Sdm199847 			udthandle->error_index = -2;
3635064Sdm199847 			goto out;
3645064Sdm199847 		}
3655064Sdm199847 
3665064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->conflict_rule,
3675064Sdm199847 		    &res.conflict_rule);
3685064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
3695064Sdm199847 			udthandle->error_index = -2;
3705064Sdm199847 			goto out;
3715064Sdm199847 		}
3725064Sdm199847 	}
3735064Sdm199847 
3745064Sdm199847 	retcode = res.retcode;
3755064Sdm199847 
3765064Sdm199847 
3775064Sdm199847 out:
3784644Sbaban 	/* reset handle so that it can be used again */
3795064Sdm199847 	if (retcode == IDMAP_SUCCESS) {
3805064Sdm199847 		_IDMAP_RESET_UDT_HANDLE(udthandle);
3815064Sdm199847 	}
3825064Sdm199847 
3835064Sdm199847 	(void) xdr_free(xdr_idmap_update_res, (caddr_t)&res);
3845064Sdm199847 	errno = idmap_stat2errno(retcode);
3855064Sdm199847 	return (retcode);
3865064Sdm199847 }
3875064Sdm199847 
3885064Sdm199847 
3895064Sdm199847 static void
3905064Sdm199847 idmap_namerule_parts_clear(char **windomain, char **winname,
3915696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
3925696Snw141292     boolean_t *is_nt4, int *direction)
3935696Snw141292 {
3945064Sdm199847 	if (windomain)
3955064Sdm199847 		*windomain = NULL;
3965064Sdm199847 	if (winname)
3975064Sdm199847 		*winname = NULL;
3985064Sdm199847 	if (unixname)
3995064Sdm199847 		*unixname = NULL;
4005064Sdm199847 
4015064Sdm199847 	if (is_nt4)
4025064Sdm199847 		*is_nt4 = 0;
4035064Sdm199847 	if (is_user)
4045064Sdm199847 		*is_user = -1;
4055696Snw141292 	if (is_wuser)
4065696Snw141292 		*is_wuser = -1;
4075064Sdm199847 	if (direction)
4085064Sdm199847 		*direction = IDMAP_DIRECTION_UNDEF;
4095064Sdm199847 }
4105064Sdm199847 
4115064Sdm199847 static idmap_stat
4125696Snw141292 idmap_namerule2parts(idmap_namerule *rule,
4135064Sdm199847     char **windomain, char **winname,
4145696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4155696Snw141292     boolean_t *is_nt4, int *direction)
4165696Snw141292 {
4175064Sdm199847 	idmap_stat retcode;
4185064Sdm199847 
4195064Sdm199847 	if (EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
4205064Sdm199847 		return (IDMAP_ERR_NORESULT);
4215064Sdm199847 
4225064Sdm199847 
4235064Sdm199847 	retcode = idmap_strdupnull(windomain, rule->windomain);
4245064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4255064Sdm199847 		goto errout;
4265064Sdm199847 
4275064Sdm199847 	retcode = idmap_strdupnull(winname, rule->winname);
4285064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4295064Sdm199847 		goto errout;
4305064Sdm199847 
4315064Sdm199847 	retcode = idmap_strdupnull(unixname, rule->unixname);
4325064Sdm199847 	if (retcode != IDMAP_SUCCESS)
4335064Sdm199847 		goto errout;
4345064Sdm199847 
4355064Sdm199847 
4365064Sdm199847 	if (is_user)
4375064Sdm199847 		*is_user = rule->is_user;
4385696Snw141292 	if (is_wuser)
4395696Snw141292 		*is_wuser = rule->is_wuser;
4405064Sdm199847 	if (is_nt4)
4415064Sdm199847 		*is_nt4 = rule->is_nt4;
4425064Sdm199847 	if (direction)
4435064Sdm199847 		*direction = rule->direction;
4445064Sdm199847 
4455064Sdm199847 
4465064Sdm199847 	return (IDMAP_SUCCESS);
4474644Sbaban 
4485064Sdm199847 errout:
4495064Sdm199847 	if (windomain && *windomain)
4505064Sdm199847 		free(*windomain);
4515064Sdm199847 	if (winname && *winname)
4525064Sdm199847 		free(*winname);
4535064Sdm199847 	if (unixname && *unixname)
4545064Sdm199847 		free(*unixname);
4555064Sdm199847 
4565064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
4575696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
4585064Sdm199847 
4594520Snw141292 	return (retcode);
4605064Sdm199847 
4615064Sdm199847 }
4625064Sdm199847 
4635064Sdm199847 /*
4645064Sdm199847  * Retrieve the index of the failed batch element. error_index == -1
4655064Sdm199847  * indicates failure at the beginning, -2 at the end.
4665064Sdm199847  *
4675064Sdm199847  * If idmap_udt_commit didn't return error, the returned value is undefined.
4685064Sdm199847  *
4695064Sdm199847  * Return value:
4705064Sdm199847  * IDMAP_SUCCESS
4715064Sdm199847  */
4725064Sdm199847 
4735064Sdm199847 idmap_stat
4745064Sdm199847 idmap_udt_get_error_index(idmap_udt_handle_t *udthandle,
4755696Snw141292     int64_t *error_index)
4765696Snw141292 {
4775064Sdm199847 	if (error_index)
4785064Sdm199847 		*error_index = udthandle->error_index;
4795064Sdm199847 
4805064Sdm199847 	return (IDMAP_SUCCESS);
4815064Sdm199847 }
4825064Sdm199847 
4835064Sdm199847 
4845064Sdm199847 /*
4855064Sdm199847  * Retrieve the rule which caused the batch to fail. If
4865064Sdm199847  * idmap_udt_commit didn't return error or if error_index is < 0, the
4875064Sdm199847  * retrieved rule is undefined.
4885064Sdm199847  *
4895064Sdm199847  * Return value:
4905064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
4915064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
4925064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
4935064Sdm199847  */
4945064Sdm199847 
4955064Sdm199847 idmap_stat
4965064Sdm199847 idmap_udt_get_error_rule(idmap_udt_handle_t *udthandle,
4975064Sdm199847     char **windomain, char **winname,
4985696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4995696Snw141292     boolean_t *is_nt4, int *direction)
5005696Snw141292 {
5015064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
5025696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
5035064Sdm199847 
5045064Sdm199847 	if (udthandle->commit_stat == IDMAP_SUCCESS ||
5055064Sdm199847 	    udthandle->error_index < 0)
5065064Sdm199847 		return (IDMAP_ERR_NORESULT);
5075064Sdm199847 
5085064Sdm199847 	return (idmap_namerule2parts(
5095696Snw141292 	    &udthandle->error_rule,
5105696Snw141292 	    windomain,
5115696Snw141292 	    winname,
5125696Snw141292 	    unixname,
5135696Snw141292 	    is_user,
5145696Snw141292 	    is_wuser,
5155696Snw141292 	    is_nt4,
5165696Snw141292 	    direction));
5175064Sdm199847 }
5185064Sdm199847 
5195064Sdm199847 /*
5205064Sdm199847  * Retrieve the rule with which there was a conflict. TODO: retrieve
5215064Sdm199847  * the value.
5225064Sdm199847  *
5235064Sdm199847  * Return value:
5245064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
5255064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
5265064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
5275064Sdm199847  */
5285064Sdm199847 
5295064Sdm199847 idmap_stat
5305064Sdm199847 idmap_udt_get_conflict_rule(idmap_udt_handle_t *udthandle,
5315064Sdm199847     char **windomain, char **winname,
5325696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
5335696Snw141292     boolean_t *is_nt4, int *direction)
5345696Snw141292 {
5355064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
5365696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
5375064Sdm199847 
5385064Sdm199847 	if (udthandle->commit_stat != IDMAP_ERR_W2U_NAMERULE_CONFLICT &&
5395064Sdm199847 	    udthandle->commit_stat != IDMAP_ERR_U2W_NAMERULE_CONFLICT) {
5405696Snw141292 		return (IDMAP_ERR_NORESULT);
5415064Sdm199847 	}
5425064Sdm199847 
5435064Sdm199847 	return (idmap_namerule2parts(
5445696Snw141292 	    &udthandle->conflict_rule,
5455696Snw141292 	    windomain,
5465696Snw141292 	    winname,
5475696Snw141292 	    unixname,
5485696Snw141292 	    is_user,
5495696Snw141292 	    is_wuser,
5505696Snw141292 	    is_nt4,
5515696Snw141292 	    direction));
5524520Snw141292 }
5534520Snw141292 
5544520Snw141292 
5554520Snw141292 /*
5564520Snw141292  * Destroy the update handle
5574520Snw141292  */
5584520Snw141292 void
5595696Snw141292 idmap_udt_destroy(idmap_udt_handle_t *udthandle)
5605696Snw141292 {
5614520Snw141292 	if (udthandle == NULL)
5624520Snw141292 		return;
5634520Snw141292 	(void) xdr_free(xdr_idmap_update_batch, (caddr_t)&udthandle->batch);
5645064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->error_rule);
5655064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->conflict_rule);
5664520Snw141292 	free(udthandle);
5674520Snw141292 }
5684520Snw141292 
5694520Snw141292 
5704520Snw141292 idmap_stat
5714520Snw141292 idmap_udt_add_namerule(idmap_udt_handle_t *udthandle, const char *windomain,
5725696Snw141292     boolean_t is_user, boolean_t is_wuser, const char *winname,
5735696Snw141292     const char *unixname, boolean_t is_nt4, int direction)
5745696Snw141292 {
5754520Snw141292 	idmap_retcode	retcode;
5764644Sbaban 	idmap_namerule	*rule = NULL;
5774520Snw141292 
5784644Sbaban 	retcode = _udt_extend_batch(udthandle);
5794520Snw141292 	if (retcode != IDMAP_SUCCESS)
5804520Snw141292 		goto errout;
5814520Snw141292 
5824520Snw141292 	rule = &udthandle->batch.
5835696Snw141292 	    idmap_update_batch_val[udthandle->next].
5845696Snw141292 	    idmap_update_op_u.rule;
5854520Snw141292 	rule->is_user = is_user;
5865696Snw141292 	rule->is_wuser = is_wuser;
5874520Snw141292 	rule->direction = direction;
5884520Snw141292 	rule->is_nt4 = is_nt4;
5895064Sdm199847 
5905064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
5915064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5925064Sdm199847 		goto errout;
5935064Sdm199847 
5945064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
5955064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5965064Sdm199847 		goto errout;
5975064Sdm199847 
5985064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
5995064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6005064Sdm199847 		goto errout;
6014644Sbaban 
6024644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6034644Sbaban 	    OP_ADD_NAMERULE;
6044520Snw141292 	udthandle->next++;
6054520Snw141292 	return (IDMAP_SUCCESS);
6064520Snw141292 
6074520Snw141292 errout:
6084644Sbaban 	/* The batch should still be usable */
6094644Sbaban 	if (rule)
6104644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
6114520Snw141292 	errno = idmap_stat2errno(retcode);
6124520Snw141292 	return (retcode);
6134520Snw141292 }
6144520Snw141292 
6154520Snw141292 
6164520Snw141292 /* ARGSUSED */
6174520Snw141292 idmap_stat
6184520Snw141292 idmap_udt_rm_namerule(idmap_udt_handle_t *udthandle, boolean_t is_user,
6195696Snw141292     boolean_t is_wuser,	const char *windomain, const char *winname,
6205696Snw141292     const char *unixname, int direction)
6215696Snw141292 {
6224520Snw141292 	idmap_retcode	retcode;
6234644Sbaban 	idmap_namerule	*rule = NULL;
6244520Snw141292 
6254644Sbaban 	retcode = _udt_extend_batch(udthandle);
6264520Snw141292 	if (retcode != IDMAP_SUCCESS)
6274520Snw141292 		goto errout;
6284520Snw141292 
6294520Snw141292 	rule = &udthandle->batch.
6305696Snw141292 	    idmap_update_batch_val[udthandle->next].
6315696Snw141292 	    idmap_update_op_u.rule;
6324520Snw141292 	rule->is_user = is_user;
6335696Snw141292 	rule->is_wuser = is_wuser;
6344520Snw141292 	rule->direction = direction;
6355064Sdm199847 
6365064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
6375064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6385064Sdm199847 		goto errout;
6395064Sdm199847 
6405064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
6415064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6425064Sdm199847 		goto errout;
6435064Sdm199847 
6445064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
6455064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6465064Sdm199847 		goto errout;
6475064Sdm199847 
6484644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6494644Sbaban 	    OP_RM_NAMERULE;
6504520Snw141292 	udthandle->next++;
6514520Snw141292 	return (IDMAP_SUCCESS);
6524520Snw141292 
6534520Snw141292 errout:
6544644Sbaban 	if (rule)
6554644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
6564520Snw141292 	errno = idmap_stat2errno(retcode);
6574520Snw141292 	return (retcode);
6584520Snw141292 }
6594520Snw141292 
6604520Snw141292 
6614520Snw141292 /* ARGSUSED */
6624520Snw141292 idmap_stat
6635696Snw141292 idmap_udt_flush_namerules(idmap_udt_handle_t *udthandle)
6645696Snw141292 {
6654520Snw141292 	idmap_retcode	retcode;
6664520Snw141292 
6674644Sbaban 	retcode = _udt_extend_batch(udthandle);
6684520Snw141292 	if (retcode != IDMAP_SUCCESS)
6694520Snw141292 		goto errout;
6704520Snw141292 
6714644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
6724644Sbaban 	    OP_FLUSH_NAMERULES;
6734520Snw141292 	udthandle->next++;
6744520Snw141292 	return (IDMAP_SUCCESS);
6754520Snw141292 
6764520Snw141292 errout:
6774520Snw141292 	errno = idmap_stat2errno(retcode);
6784520Snw141292 	return (retcode);
6794520Snw141292 }
6804520Snw141292 
6814520Snw141292 
6824520Snw141292 /*
6834520Snw141292  * Set the number of entries requested per batch by the iterator
6844520Snw141292  *
6854520Snw141292  * Input:
6864520Snw141292  * iter  - iterator
6874520Snw141292  * limit - number of entries requested per batch
6884520Snw141292  */
6894520Snw141292 idmap_stat
6905696Snw141292 idmap_iter_set_limit(idmap_iter_t *iter, uint64_t limit)
6915696Snw141292 {
6924520Snw141292 	if (iter == NULL) {
6934520Snw141292 		errno = EINVAL;
6944520Snw141292 		return (IDMAP_ERR_ARG);
6954520Snw141292 	}
6964520Snw141292 	iter->limit = limit;
6974520Snw141292 	return (IDMAP_SUCCESS);
6984520Snw141292 }
6994520Snw141292 
7004520Snw141292 
7014520Snw141292 /*
7024520Snw141292  * Create iterator to get name-based mapping rules
7034520Snw141292  *
7044520Snw141292  * Input:
7054520Snw141292  * windomain - Windows domain
7064520Snw141292  * is_user   - user or group rules
7074520Snw141292  * winname   - Windows user or group name
7084520Snw141292  * unixname  - Unix user or group name
7094520Snw141292  *
7104520Snw141292  * Output:
7114520Snw141292  * iter - iterator
7124520Snw141292  */
7134520Snw141292 idmap_stat
7144520Snw141292 idmap_iter_namerules(idmap_handle_t *handle, const char *windomain,
7155696Snw141292 		boolean_t is_user, boolean_t is_wuser, const char *winname,
7165696Snw141292 		const char *unixname, idmap_iter_t **iter)
7175696Snw141292 {
7184520Snw141292 
7194520Snw141292 	idmap_iter_t			*tmpiter;
7204520Snw141292 	idmap_list_namerules_1_argument	*arg = NULL;
7214520Snw141292 	idmap_namerule			*rule;
7224520Snw141292 	idmap_retcode			retcode;
7234520Snw141292 
7244520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_NAMERULES);
7254520Snw141292 
7264520Snw141292 	rule = &arg->rule;
7274520Snw141292 	rule->is_user = is_user;
7285696Snw141292 	rule->is_wuser = is_wuser;
7294644Sbaban 	rule->direction = IDMAP_DIRECTION_UNDEF;
7305064Sdm199847 
7315064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
7325064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7335064Sdm199847 		goto errout;
7345064Sdm199847 
7355064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
7365064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7375064Sdm199847 		goto errout;
7385064Sdm199847 
7395064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
7405064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7415064Sdm199847 		goto errout;
7424520Snw141292 
7434520Snw141292 	*iter = tmpiter;
7444520Snw141292 	return (IDMAP_SUCCESS);
7454520Snw141292 
7464520Snw141292 errout:
7474520Snw141292 	__ITER_ERR_RETURN(tmpiter, arg,
7485696Snw141292 	    xdr_idmap_list_namerules_1_argument, retcode);
7494520Snw141292 }
7504520Snw141292 
7514520Snw141292 
7524520Snw141292 /*
7534520Snw141292  * Iterate through the name-based mapping rules
7544520Snw141292  *
7554520Snw141292  * Input:
7564520Snw141292  * iter - iterator
7574520Snw141292  *
7584520Snw141292  * Output:
7594520Snw141292  * windomain - Windows domain
7604520Snw141292  * winname   - Windows user or group name
7614520Snw141292  * unixname  - Unix user or group name
7624520Snw141292  * is_nt4    - NT4 or AD
7634520Snw141292  * direction - bi(0), win2unix(1), unix2win(2)
7644520Snw141292  *
7654520Snw141292  * Return value:
7664520Snw141292  * 0   - done
7674520Snw141292  * 1   - more results available
7684520Snw141292  * < 0 - error
7694520Snw141292  */
7704520Snw141292 idmap_stat
7714520Snw141292 idmap_iter_next_namerule(idmap_iter_t *iter, char **windomain,
7725696Snw141292     char **winname, char **unixname,  boolean_t *is_user,
7735696Snw141292     boolean_t *is_wuser, boolean_t *is_nt4, int *direction)
7745696Snw141292 {
7754520Snw141292 	idmap_namerules_res		*namerules;
7764520Snw141292 	idmap_list_namerules_1_argument	*arg;
7774520Snw141292 	idmap_retcode			retcode;
7784520Snw141292 
7795696Snw141292 	idmap_namerule_parts_clear(windomain, winname,
7805696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
7815696Snw141292 
7824520Snw141292 
7834520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_NAMERULES);
7844520Snw141292 
7854520Snw141292 	namerules = (idmap_namerules_res *)iter->retlist;
7864520Snw141292 	if (iter->retcode == IDMAP_NEXT && (namerules == NULL ||
7875696Snw141292 	    iter->next >= namerules->rules.rules_len)) {
7884520Snw141292 
7894520Snw141292 		if ((arg = iter->arg) == NULL) {
7904520Snw141292 			errno = EINVAL;
7914520Snw141292 			return (IDMAP_ERR_ARG);
7924520Snw141292 		}
7934520Snw141292 		arg->limit = iter->limit;
7944520Snw141292 
7954520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_NAMERULES,
7965696Snw141292 		    iter, arg,
7975696Snw141292 		    (uchar_t **)&namerules, sizeof (*namerules),
7985696Snw141292 		    (xdrproc_t)xdr_idmap_list_namerules_1_argument,
7995696Snw141292 		    (xdrproc_t)xdr_idmap_namerules_res);
8004520Snw141292 		if (retcode != IDMAP_SUCCESS)
8014520Snw141292 			return (retcode);
8024520Snw141292 
8034520Snw141292 		if (IDMAP_ERROR(namerules->retcode)) {
8044520Snw141292 			retcode  = namerules->retcode;
8054520Snw141292 			xdr_free(xdr_idmap_namerules_res, (caddr_t)namerules);
8064520Snw141292 			free(namerules);
8074520Snw141292 			iter->retlist = NULL;
8084520Snw141292 			return (retcode);
8094520Snw141292 		}
8104520Snw141292 		iter->retcode = namerules->retcode;
8114520Snw141292 		arg->lastrowid = namerules->lastrowid;
8124520Snw141292 	}
8134520Snw141292 
8144520Snw141292 	if (namerules == NULL || namerules->rules.rules_len == 0)
8154520Snw141292 		return (IDMAP_SUCCESS);
8164520Snw141292 
8174520Snw141292 	if (iter->next >= namerules->rules.rules_len) {
8184520Snw141292 		return (IDMAP_ERR_ARG);
8194520Snw141292 	}
8204520Snw141292 
8215064Sdm199847 	retcode = idmap_strdupnull(windomain,
8225064Sdm199847 	    namerules->rules.rules_val[iter->next].windomain);
8235064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8245064Sdm199847 		goto errout;
8255064Sdm199847 
8265064Sdm199847 	retcode = idmap_strdupnull(winname,
8275064Sdm199847 	    namerules->rules.rules_val[iter->next].winname);
8285064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8295064Sdm199847 		goto errout;
8305064Sdm199847 
8315064Sdm199847 	retcode = idmap_strdupnull(unixname,
8325064Sdm199847 	    namerules->rules.rules_val[iter->next].unixname);
8335064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8345064Sdm199847 		goto errout;
8355064Sdm199847 
8364520Snw141292 	if (is_nt4)
8374520Snw141292 		*is_nt4 = namerules->rules.rules_val[iter->next].is_nt4;
8385696Snw141292 	if (is_user)
8395696Snw141292 		*is_user = namerules->rules.rules_val[iter->next].is_user;
8405696Snw141292 	if (is_wuser)
8415696Snw141292 		*is_wuser = namerules->rules.rules_val[iter->next].is_wuser;
8424520Snw141292 	if (direction)
8434520Snw141292 		*direction = namerules->rules.rules_val[iter->next].direction;
8444520Snw141292 	iter->next++;
8454520Snw141292 
8464520Snw141292 	if (iter->next == namerules->rules.rules_len)
8474520Snw141292 		return (iter->retcode);
8484520Snw141292 	else
8494520Snw141292 		return (IDMAP_NEXT);
8504520Snw141292 
8514520Snw141292 errout:
8524520Snw141292 	if (windomain && *windomain)
8534520Snw141292 		free(*windomain);
8544520Snw141292 	if (winname && *winname)
8554520Snw141292 		free(*winname);
8564520Snw141292 	if (unixname && *unixname)
8574520Snw141292 		free(*unixname);
8584520Snw141292 	return (retcode);
8594520Snw141292 }
8604520Snw141292 
8614520Snw141292 
8624520Snw141292 /*
8634520Snw141292  * Create iterator to get SID to UID/GID mappings
8644520Snw141292  *
8654520Snw141292  * Output:
8664520Snw141292  * iter - iterator
8674520Snw141292  */
8684520Snw141292 idmap_stat
8696386Sjp151216 idmap_iter_mappings(idmap_handle_t *handle, idmap_iter_t **iter, int flag)
8705696Snw141292 {
8714520Snw141292 	idmap_iter_t			*tmpiter;
8724520Snw141292 	idmap_list_mappings_1_argument	*arg = NULL;
8734520Snw141292 
8744520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_MAPPINGS);
8754520Snw141292 
8766386Sjp151216 	arg->flag = flag;
8774520Snw141292 	*iter = tmpiter;
8784520Snw141292 	return (IDMAP_SUCCESS);
8794520Snw141292 }
8804520Snw141292 
8814520Snw141292 
8824520Snw141292 /*
8834520Snw141292  * Iterate through the SID to UID/GID mappings
8844520Snw141292  *
8854520Snw141292  * Input:
8864520Snw141292  * iter - iterator
8874520Snw141292  *
8884520Snw141292  * Output:
8894520Snw141292  * sid - SID in canonical form
8904520Snw141292  * pid - UID or GID
8914520Snw141292  *
8924520Snw141292  * Return value:
8934520Snw141292  * 0   - done
8944520Snw141292  * 1   - more results available
8954520Snw141292  * < 0 - error
8964520Snw141292  */
8974520Snw141292 idmap_stat
8984520Snw141292 idmap_iter_next_mapping(idmap_iter_t *iter, char **sidprefix,
8995696Snw141292     idmap_rid_t *rid, uid_t *pid, char **winname,
9005696Snw141292     char **windomain, char **unixname, boolean_t *is_user,
9016386Sjp151216     boolean_t *is_wuser, int *direction, idmap_info *info)
9025696Snw141292 {
9034520Snw141292 	idmap_mappings_res		*mappings;
9044520Snw141292 	idmap_list_mappings_1_argument	*arg;
9054520Snw141292 	idmap_retcode			retcode;
9064520Snw141292 	char				*str;
9074520Snw141292 
9084520Snw141292 	if (sidprefix)
9094520Snw141292 		*sidprefix = NULL;
9104520Snw141292 	if (rid)
9114520Snw141292 		*rid = UINT32_MAX;
9124520Snw141292 	if (winname)
9134520Snw141292 		*winname = NULL;
9144520Snw141292 	if (windomain)
9154520Snw141292 		*windomain = NULL;
9164520Snw141292 	if (unixname)
9174520Snw141292 		*unixname = NULL;
9184520Snw141292 	if (pid)
9194520Snw141292 		*pid = UINT32_MAX;
9205696Snw141292 	if (is_user)
9215696Snw141292 		*is_user = -1;
9225696Snw141292 	if (is_wuser)
9235696Snw141292 		*is_wuser = -1;
9244520Snw141292 	if (direction)
9254644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
9264520Snw141292 
9274520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_MAPPINGS);
9284520Snw141292 
9294520Snw141292 	mappings = (idmap_mappings_res *)iter->retlist;
9304520Snw141292 	if (iter->retcode == IDMAP_NEXT && (mappings == NULL ||
9315696Snw141292 	    iter->next >= mappings->mappings.mappings_len)) {
9324520Snw141292 
9334520Snw141292 		if ((arg = iter->arg) == NULL) {
9344520Snw141292 			errno = EINVAL;
9354520Snw141292 			return (IDMAP_ERR_ARG);
9364520Snw141292 		}
9374520Snw141292 		arg->limit = iter->limit;
9384520Snw141292 
9394520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_MAPPINGS,
9405696Snw141292 		    iter, arg,
9415696Snw141292 		    (uchar_t **)&mappings, sizeof (*mappings),
9425696Snw141292 		    (xdrproc_t)xdr_idmap_list_mappings_1_argument,
9435696Snw141292 		    (xdrproc_t)xdr_idmap_mappings_res);
9444520Snw141292 		if (retcode != IDMAP_SUCCESS)
9454520Snw141292 			return (retcode);
9464520Snw141292 
9474520Snw141292 		if (IDMAP_ERROR(mappings->retcode)) {
9484520Snw141292 			retcode  = mappings->retcode;
9494520Snw141292 			xdr_free(xdr_idmap_mappings_res, (caddr_t)mappings);
9504520Snw141292 			free(mappings);
9514520Snw141292 			iter->retlist = NULL;
9524520Snw141292 			return (retcode);
9534520Snw141292 		}
9544520Snw141292 		iter->retcode = mappings->retcode;
9554520Snw141292 		arg->lastrowid = mappings->lastrowid;
9564520Snw141292 	}
9574520Snw141292 
9584520Snw141292 	if (mappings == NULL || mappings->mappings.mappings_len == 0)
9594520Snw141292 		return (IDMAP_SUCCESS);
9604520Snw141292 
9614520Snw141292 	if (iter->next >= mappings->mappings.mappings_len) {
9624520Snw141292 		return (IDMAP_ERR_ARG);
9634520Snw141292 	}
9644520Snw141292 
9654520Snw141292 	if (sidprefix) {
9664520Snw141292 		str = mappings->mappings.mappings_val[iter->next].id1.
9675696Snw141292 		    idmap_id_u.sid.prefix;
9684695Sbaban 		if (str && *str != '\0') {
9694520Snw141292 			*sidprefix = strdup(str);
9704526Sbaban 			if (*sidprefix == NULL) {
9714526Sbaban 				retcode = IDMAP_ERR_MEMORY;
9724526Sbaban 				goto errout;
9734526Sbaban 			}
9744520Snw141292 		}
9754520Snw141292 	}
9764520Snw141292 	if (rid)
9774520Snw141292 		*rid = mappings->mappings.mappings_val[iter->next].id1.
9785696Snw141292 		    idmap_id_u.sid.rid;
9795064Sdm199847 
9805064Sdm199847 	retcode = idmap_strdupnull(windomain,
9815064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1domain);
9825064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9835064Sdm199847 		goto errout;
9845064Sdm199847 
9855064Sdm199847 	retcode = idmap_strdupnull(winname,
9865064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1name);
9875064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9885064Sdm199847 		goto errout;
9895064Sdm199847 
9905064Sdm199847 	retcode = idmap_strdupnull(unixname,
9915064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id2name);
9925064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9935064Sdm199847 		goto errout;
9945064Sdm199847 
9955064Sdm199847 
9964520Snw141292 	if (pid)
9974520Snw141292 		*pid = mappings->mappings.mappings_val[iter->next].id2.
9985696Snw141292 		    idmap_id_u.uid;
9994520Snw141292 	if (direction)
10004520Snw141292 		*direction = mappings->mappings.mappings_val[iter->next].
10015696Snw141292 		    direction;
10025696Snw141292 	if (is_user)
10035696Snw141292 		*is_user = (mappings->mappings.mappings_val[iter->next].id2
10045696Snw141292 		    .idtype == IDMAP_UID)?1:0;
10055696Snw141292 	if (is_wuser)
10065696Snw141292 		*is_wuser = (mappings->mappings.mappings_val[iter->next].id1
10075696Snw141292 		    .idtype == IDMAP_USID)?1:0;
10085696Snw141292 
10096386Sjp151216 	if (info) {
10106386Sjp151216 		retcode = idmap_info_cpy(info,
10116386Sjp151216 		    &mappings->mappings.mappings_val[iter->next].info);
10126386Sjp151216 		if (retcode != IDMAP_SUCCESS)
10136386Sjp151216 			goto errout;
10146386Sjp151216 	}
10154520Snw141292 	iter->next++;
10164520Snw141292 
10174520Snw141292 	if (iter->next == mappings->mappings.mappings_len)
10184520Snw141292 		return (iter->retcode);
10194520Snw141292 	else
10204520Snw141292 		return (IDMAP_NEXT);
10214520Snw141292 
10224520Snw141292 errout:
10234520Snw141292 	if (sidprefix && *sidprefix)
10244520Snw141292 		free(*sidprefix);
10254520Snw141292 	if (winname && *winname)
10264520Snw141292 		free(*winname);
10274520Snw141292 	if (windomain && *windomain)
10284520Snw141292 		free(*windomain);
10294520Snw141292 	if (unixname && *unixname)
10304520Snw141292 		free(*unixname);
10314520Snw141292 	return (retcode);
10324520Snw141292 }
10334520Snw141292 
10344520Snw141292 
10354520Snw141292 /*
10364520Snw141292  * Destroy the iterator
10374520Snw141292  */
10384520Snw141292 void
10395696Snw141292 idmap_iter_destroy(idmap_iter_t *iter)
10405696Snw141292 {
10414520Snw141292 	xdrproc_t _xdr_argument, _xdr_result;
10424520Snw141292 
10434520Snw141292 	if (iter == NULL)
10444520Snw141292 		return;
10454520Snw141292 
10464520Snw141292 	switch (iter->type) {
10474520Snw141292 	case IDMAP_LIST_NAMERULES:
10484520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_namerules_1_argument;
10494520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_namerules_res;
10504520Snw141292 		break;
10514520Snw141292 	case IDMAP_LIST_MAPPINGS:
10524520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_mappings_1_argument;
10534520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_mappings_res;
10544520Snw141292 		break;
10554520Snw141292 	default:
10564520Snw141292 		free(iter);
10574520Snw141292 		return;
10584520Snw141292 	};
10594520Snw141292 
10604520Snw141292 	if (iter->arg) {
10614520Snw141292 		xdr_free(_xdr_argument, (caddr_t)iter->arg);
10624520Snw141292 		free(iter->arg);
10634520Snw141292 	}
10644520Snw141292 	if (iter->retlist) {
10654520Snw141292 		xdr_free(_xdr_result, (caddr_t)iter->retlist);
10664520Snw141292 		free(iter->retlist);
10674520Snw141292 	}
10684520Snw141292 	free(iter);
10694520Snw141292 }
10704520Snw141292 
10714520Snw141292 
10724520Snw141292 /*
10734520Snw141292  * Create handle to get SID to UID/GID mapping entries
10744520Snw141292  *
10754520Snw141292  * Input:
10764520Snw141292  * gh - "get mapping" handle
10774520Snw141292  */
10784520Snw141292 idmap_stat
10795696Snw141292 idmap_get_create(idmap_handle_t *handle, idmap_get_handle_t **gh)
10805696Snw141292 {
10814520Snw141292 	idmap_get_handle_t	*tmp;
10824520Snw141292 
10834520Snw141292 	/* sanity checks */
10844520Snw141292 	if (handle == NULL || gh == NULL) {
10854520Snw141292 		errno = EINVAL;
10864520Snw141292 		return (IDMAP_ERR_ARG);
10874520Snw141292 	}
10884520Snw141292 
10894520Snw141292 	/* allocate the handle */
10904520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
10914520Snw141292 		errno = ENOMEM;
10924520Snw141292 		return (IDMAP_ERR_MEMORY);
10934520Snw141292 	}
10944520Snw141292 
10954520Snw141292 	tmp->ih = handle;
10964520Snw141292 	*gh = tmp;
10974520Snw141292 	return (IDMAP_SUCCESS);
10984520Snw141292 }
10994520Snw141292 
11004520Snw141292 
11014520Snw141292 /*
11024520Snw141292  * Given SID, get UID
11034520Snw141292  *
11044520Snw141292  * Input:
11054520Snw141292  * sidprefix  - SID prefix
11064520Snw141292  * rid        - RID
11074520Snw141292  * flag       - flag
11084520Snw141292  *
11094520Snw141292  * Output:
11104520Snw141292  * stat - status of the get request
11114520Snw141292  * uid  - POSIX UID if stat = 0
11124520Snw141292  *
11134520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
11144520Snw141292  */
11154520Snw141292 idmap_stat
11164520Snw141292 idmap_get_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
11175696Snw141292 		int flag, uid_t *uid, idmap_stat *stat)
11185696Snw141292 {
11196386Sjp151216 	return (idmap_getext_uidbysid(gh, sidprefix, rid, flag, uid,
11206386Sjp151216 	    NULL, stat));
11216386Sjp151216 }
11224520Snw141292 
11236386Sjp151216 /*
11246386Sjp151216  * Given SID, get UID
11256386Sjp151216  *
11266386Sjp151216  * Input:
11276386Sjp151216  * sidprefix  - SID prefix
11286386Sjp151216  * rid        - RID
11296386Sjp151216  * flag       - flag
11306386Sjp151216  *
11316386Sjp151216  * Output:
11326386Sjp151216  * stat - status of the get request
11336386Sjp151216  * uid  - POSIX UID if stat = 0
11346386Sjp151216  * how  - mapping type if stat = 0
11356386Sjp151216  *
11366386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
11376386Sjp151216  */
11386386Sjp151216 
11396386Sjp151216 idmap_stat
11406386Sjp151216 idmap_getext_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
11416386Sjp151216 		int flag, uid_t *uid, idmap_info *info, idmap_stat *stat)
11426386Sjp151216 {
11434520Snw141292 	idmap_retcode	retcode;
11444644Sbaban 	idmap_mapping	*mapping = NULL;
11454520Snw141292 
11464520Snw141292 	/* sanity checks */
11474520Snw141292 	if (gh == NULL)
11484520Snw141292 		return (IDMAP_ERR_ARG);
11494520Snw141292 	if (uid == NULL || sidprefix == NULL)
11504520Snw141292 		return (IDMAP_ERR_ARG);
11514520Snw141292 
11527369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
11537369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
11547369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_uidbysid(sidprefix, rid, uid);
11557369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
11567369SJulian.Pullen@Sun.COM 			*stat = retcode;
11577369SJulian.Pullen@Sun.COM 			return (retcode);
11587369SJulian.Pullen@Sun.COM 		}
11597369SJulian.Pullen@Sun.COM 	}
11607369SJulian.Pullen@Sun.COM 
11614520Snw141292 	/* Extend the request array and the return list */
11624520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
11634520Snw141292 		goto errout;
11644520Snw141292 
11654520Snw141292 	/* Setup the request */
11664520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
11674520Snw141292 	mapping->flag = flag;
11684520Snw141292 	mapping->id1.idtype = IDMAP_SID;
11694520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
11704520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
11714520Snw141292 		retcode = IDMAP_ERR_MEMORY;
11724520Snw141292 		goto errout;
11734520Snw141292 	}
11744520Snw141292 	mapping->id2.idtype = IDMAP_UID;
11754520Snw141292 
11764520Snw141292 	/* Setup pointers for the result */
11774520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_UID;
11784520Snw141292 	gh->retlist[gh->next].uid = uid;
11794520Snw141292 	gh->retlist[gh->next].stat = stat;
11806386Sjp151216 	gh->retlist[gh->next].info = info;
11817369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
11824520Snw141292 
11834520Snw141292 	gh->next++;
11844520Snw141292 	return (IDMAP_SUCCESS);
11854520Snw141292 
11864520Snw141292 errout:
11874644Sbaban 	/* Batch created so far should still be usable */
11884644Sbaban 	if (mapping)
11894644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
11904520Snw141292 	errno = idmap_stat2errno(retcode);
11914520Snw141292 	return (retcode);
11924520Snw141292 }
11934520Snw141292 
11944520Snw141292 
11954520Snw141292 /*
11964520Snw141292  * Given SID, get GID
11974520Snw141292  *
11984520Snw141292  * Input:
11994520Snw141292  * sidprefix  - SID prefix
12004520Snw141292  * rid        - rid
12014520Snw141292  * flag       - flag
12024520Snw141292  *
12034520Snw141292  * Output:
12044520Snw141292  * stat - status of the get request
12054520Snw141292  * gid  - POSIX GID if stat = 0
12064520Snw141292  *
12074520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
12084520Snw141292  */
12094520Snw141292 idmap_stat
12104520Snw141292 idmap_get_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
12115696Snw141292 		int flag, gid_t *gid, idmap_stat *stat)
12125696Snw141292 {
12136386Sjp151216 	return (idmap_getext_gidbysid(gh, sidprefix, rid, flag, gid,
12146386Sjp151216 	    NULL, stat));
12156386Sjp151216 }
12166386Sjp151216 
12176386Sjp151216 
12186386Sjp151216 /*
12196386Sjp151216  * Given SID, get GID
12206386Sjp151216  *
12216386Sjp151216  * Input:
12226386Sjp151216  * sidprefix  - SID prefix
12236386Sjp151216  * rid        - rid
12246386Sjp151216  * flag       - flag
12256386Sjp151216  *
12266386Sjp151216  * Output:
12276386Sjp151216  * stat - status of the get request
12286386Sjp151216  * gid  - POSIX GID if stat = 0
12296386Sjp151216  * how  - mapping type if stat = 0
12306386Sjp151216  *
12316386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
12326386Sjp151216  */
12336386Sjp151216 idmap_stat
12346386Sjp151216 idmap_getext_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
12356386Sjp151216 		int flag, gid_t *gid, idmap_info *info, idmap_stat *stat)
12366386Sjp151216 {
12374520Snw141292 
12384520Snw141292 	idmap_retcode	retcode;
12394644Sbaban 	idmap_mapping	*mapping = NULL;
12404520Snw141292 
12414520Snw141292 	/* sanity checks */
12424520Snw141292 	if (gh == NULL)
12434520Snw141292 		return (IDMAP_ERR_ARG);
12444520Snw141292 	if (gid == NULL || sidprefix == NULL)
12454520Snw141292 		return (IDMAP_ERR_ARG);
12464520Snw141292 
12477369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
12487369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
12497369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_gidbysid(sidprefix, rid, gid);
12507369SJulian.Pullen@Sun.COM 		if (retcode == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
12517369SJulian.Pullen@Sun.COM 			*stat = retcode;
12527369SJulian.Pullen@Sun.COM 			return (retcode);
12537369SJulian.Pullen@Sun.COM 		}
12547369SJulian.Pullen@Sun.COM 	}
12557369SJulian.Pullen@Sun.COM 
12564520Snw141292 	/* Extend the request array and the return list */
12574520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
12584520Snw141292 		goto errout;
12594520Snw141292 
12604520Snw141292 	/* Setup the request */
12614520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
12624520Snw141292 	mapping->flag = flag;
12634520Snw141292 	mapping->id1.idtype = IDMAP_SID;
12644520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
12654520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
12664520Snw141292 		retcode = IDMAP_ERR_MEMORY;
12674520Snw141292 		goto errout;
12684520Snw141292 	}
12694520Snw141292 	mapping->id2.idtype = IDMAP_GID;
12704520Snw141292 
12714520Snw141292 	/* Setup pointers for the result */
12724520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_GID;
12734520Snw141292 	gh->retlist[gh->next].gid = gid;
12744520Snw141292 	gh->retlist[gh->next].stat = stat;
12756386Sjp151216 	gh->retlist[gh->next].info = info;
12767369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
12774520Snw141292 
12784520Snw141292 	gh->next++;
12794520Snw141292 	return (IDMAP_SUCCESS);
12804520Snw141292 
12814520Snw141292 errout:
12824644Sbaban 	if (mapping)
12834644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
12844520Snw141292 	errno = idmap_stat2errno(retcode);
12854520Snw141292 	return (retcode);
12864520Snw141292 }
12874520Snw141292 
12884520Snw141292 
12896386Sjp151216 
12904520Snw141292 /*
12914520Snw141292  * Given SID, get POSIX ID i.e. UID/GID
12924520Snw141292  *
12934520Snw141292  * Input:
12944520Snw141292  * sidprefix  - SID prefix
12954520Snw141292  * rid        - rid
12964520Snw141292  * flag       - flag
12974520Snw141292  *
12984520Snw141292  * Output:
12994520Snw141292  * stat    - status of the get request
13004520Snw141292  * is_user - user or group
13014520Snw141292  * pid     - POSIX UID if stat = 0 and is_user = 1
13024520Snw141292  *           POSIX GID if stat = 0 and is_user = 0
13034520Snw141292  *
13044520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
13054520Snw141292  */
13064520Snw141292 idmap_stat
13074520Snw141292 idmap_get_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
13085696Snw141292 		int flag, uid_t *pid, int *is_user, idmap_stat *stat)
13095696Snw141292 {
13106386Sjp151216 	return (idmap_getext_pidbysid(gh, sidprefix, rid, flag, pid, is_user,
13116386Sjp151216 	    NULL, stat));
13126386Sjp151216 }
13136386Sjp151216 
13146386Sjp151216 
13156386Sjp151216 
13166386Sjp151216 /*
13176386Sjp151216  * Given SID, get POSIX ID i.e. UID/GID
13186386Sjp151216  *
13196386Sjp151216  * Input:
13206386Sjp151216  * sidprefix  - SID prefix
13216386Sjp151216  * rid        - rid
13226386Sjp151216  * flag       - flag
13236386Sjp151216  *
13246386Sjp151216  * Output:
13256386Sjp151216  * stat    - status of the get request
13266386Sjp151216  * is_user - user or group
13276386Sjp151216  * pid     - POSIX UID if stat = 0 and is_user = 1
13286386Sjp151216  *           POSIX GID if stat = 0 and is_user = 0
13296386Sjp151216  * how     - mapping type if stat = 0
13306386Sjp151216  *
13316386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
13326386Sjp151216  */
13336386Sjp151216 idmap_stat
13346386Sjp151216 idmap_getext_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
13356386Sjp151216 	int flag, uid_t *pid, int *is_user, idmap_info *info, idmap_stat *stat)
13366386Sjp151216 {
13374520Snw141292 	idmap_retcode	retcode;
13384644Sbaban 	idmap_mapping	*mapping = NULL;
13394520Snw141292 
13404520Snw141292 	/* sanity checks */
13414520Snw141292 	if (gh == NULL)
13424520Snw141292 		return (IDMAP_ERR_ARG);
13434520Snw141292 	if (pid == NULL || sidprefix == NULL || is_user == NULL)
13444520Snw141292 		return (IDMAP_ERR_ARG);
13454520Snw141292 
13467369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
13477369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
13487369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_pidbysid(sidprefix, rid, pid,
13497369SJulian.Pullen@Sun.COM 		    is_user);
13507369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
13517369SJulian.Pullen@Sun.COM 			*stat = retcode;
13527369SJulian.Pullen@Sun.COM 			return (retcode);
13537369SJulian.Pullen@Sun.COM 		}
13547369SJulian.Pullen@Sun.COM 	}
13557369SJulian.Pullen@Sun.COM 
13564520Snw141292 	/* Extend the request array and the return list */
13574520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
13584520Snw141292 		goto errout;
13594520Snw141292 
13604520Snw141292 	/* Setup the request */
13614520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
13624520Snw141292 	mapping->flag = flag;
13634520Snw141292 	mapping->id1.idtype = IDMAP_SID;
13644520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
13654520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
13664520Snw141292 		retcode = IDMAP_ERR_MEMORY;
13674520Snw141292 		goto errout;
13684520Snw141292 	}
13694520Snw141292 	mapping->id2.idtype = IDMAP_POSIXID;
13704520Snw141292 
13714520Snw141292 	/* Setup pointers for the result */
13724520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_POSIXID;
13734520Snw141292 	gh->retlist[gh->next].uid = pid;
13744520Snw141292 	gh->retlist[gh->next].gid = pid;
13754520Snw141292 	gh->retlist[gh->next].is_user = is_user;
13764520Snw141292 	gh->retlist[gh->next].stat = stat;
13776386Sjp151216 	gh->retlist[gh->next].info = info;
13787369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
13794520Snw141292 
13804520Snw141292 	gh->next++;
13814520Snw141292 	return (IDMAP_SUCCESS);
13824520Snw141292 
13834520Snw141292 errout:
13844644Sbaban 	if (mapping)
13854644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
13864520Snw141292 	errno = idmap_stat2errno(retcode);
13874520Snw141292 	return (retcode);
13884520Snw141292 }
13894520Snw141292 
13904520Snw141292 
13914520Snw141292 /*
13924520Snw141292  * Given UID, get SID
13934520Snw141292  *
13944520Snw141292  * Input:
13954520Snw141292  * uid  - POSIX UID
13964520Snw141292  * flag - flag
13974520Snw141292  *
13984520Snw141292  * Output:
13994520Snw141292  * stat - status of the get request
14004520Snw141292  * sid  - SID prefix (if stat == 0)
14014520Snw141292  * rid  - rid
14024520Snw141292  *
14034520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
14044520Snw141292  */
14054520Snw141292 idmap_stat
14064520Snw141292 idmap_get_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
14075696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
14085696Snw141292 {
14096386Sjp151216 	return (idmap_getext_sidbyuid(gh, uid, flag, sidprefix, rid,
14106386Sjp151216 	    NULL, stat));
14116386Sjp151216 }
14126386Sjp151216 
14136386Sjp151216 
14146386Sjp151216 /*
14156386Sjp151216  * Given UID, get SID
14166386Sjp151216  *
14176386Sjp151216  * Input:
14186386Sjp151216  * uid  - POSIX UID
14196386Sjp151216  * flag - flag
14206386Sjp151216  *
14216386Sjp151216  * Output:
14226386Sjp151216  * stat - status of the get request
14236386Sjp151216  * sid  - SID prefix (if stat == 0)
14246386Sjp151216  * rid  - rid
14256386Sjp151216  * how  - mapping type if stat = 0
14266386Sjp151216  *
14276386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
14286386Sjp151216  */
14296386Sjp151216 idmap_stat
14306386Sjp151216 idmap_getext_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
14316386Sjp151216 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
14326386Sjp151216 {
14334520Snw141292 
14344520Snw141292 	idmap_retcode	retcode;
14354644Sbaban 	idmap_mapping	*mapping = NULL;
14364520Snw141292 
14374520Snw141292 	/* sanity checks */
14384520Snw141292 	if (gh == NULL)
14394520Snw141292 		return (IDMAP_ERR_ARG);
14404520Snw141292 	if (sidprefix == NULL)
14414520Snw141292 		return (IDMAP_ERR_ARG);
14424520Snw141292 
14437369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
14447369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
14457369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_sidbyuid(sidprefix, rid, uid);
14467369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
14477369SJulian.Pullen@Sun.COM 			*stat = retcode;
14487369SJulian.Pullen@Sun.COM 			return (retcode);
14497369SJulian.Pullen@Sun.COM 		}
14507369SJulian.Pullen@Sun.COM 	}
14517369SJulian.Pullen@Sun.COM 
14524520Snw141292 	/* Extend the request array and the return list */
14534520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
14544520Snw141292 		goto errout;
14554520Snw141292 
14564520Snw141292 	/* Setup the request */
14574520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
14584520Snw141292 	mapping->flag = flag;
14594520Snw141292 	mapping->id1.idtype = IDMAP_UID;
14604520Snw141292 	mapping->id1.idmap_id_u.uid = uid;
14614520Snw141292 	mapping->id2.idtype = IDMAP_SID;
14624520Snw141292 
14634520Snw141292 	/* Setup pointers for the result */
14644520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
14654520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
14664520Snw141292 	gh->retlist[gh->next].rid = rid;
14674520Snw141292 	gh->retlist[gh->next].stat = stat;
14686386Sjp151216 	gh->retlist[gh->next].info = info;
14697369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
14704520Snw141292 
14714520Snw141292 	gh->next++;
14724520Snw141292 	return (IDMAP_SUCCESS);
14734520Snw141292 
14744520Snw141292 errout:
14754644Sbaban 	if (mapping)
14764644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
14774520Snw141292 	errno = idmap_stat2errno(retcode);
14784520Snw141292 	return (retcode);
14794520Snw141292 }
14804520Snw141292 
14814520Snw141292 
14824520Snw141292 /*
14834520Snw141292  * Given GID, get SID
14844520Snw141292  *
14854520Snw141292  * Input:
14864520Snw141292  * gid  - POSIX GID
14874520Snw141292  * flag - flag
14884520Snw141292  *
14894520Snw141292  * Output:
14904520Snw141292  * stat       - status of the get request
14914520Snw141292  * sidprefix  - SID prefix (if stat == 0)
14924520Snw141292  * rid        - rid
14934520Snw141292  *
14944520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
14954520Snw141292  */
14964520Snw141292 idmap_stat
14974520Snw141292 idmap_get_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
14985696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
14995696Snw141292 {
15006386Sjp151216 	return (idmap_getext_sidbygid(gh, gid, flag, sidprefix, rid,
15016386Sjp151216 	    NULL, stat));
15026386Sjp151216 }
15036386Sjp151216 
15046386Sjp151216 
15056386Sjp151216 /*
15066386Sjp151216  * Given GID, get SID
15076386Sjp151216  *
15086386Sjp151216  * Input:
15096386Sjp151216  * gid  - POSIX GID
15106386Sjp151216  * flag - flag
15116386Sjp151216  *
15126386Sjp151216  * Output:
15136386Sjp151216  * stat       - status of the get request
15146386Sjp151216  * sidprefix  - SID prefix (if stat == 0)
15156386Sjp151216  * rid        - rid
15166386Sjp151216  * how        - mapping type if stat = 0
15176386Sjp151216  *
15186386Sjp151216  * Note: The output parameters will be set by idmap_get_mappings()
15196386Sjp151216  */
15206386Sjp151216 idmap_stat
15216386Sjp151216 idmap_getext_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
15226386Sjp151216 	char **sidprefix, idmap_rid_t *rid, idmap_info *info, idmap_stat *stat)
15236386Sjp151216 {
15244520Snw141292 
15254520Snw141292 	idmap_retcode	retcode;
15264644Sbaban 	idmap_mapping	*mapping = NULL;
15274520Snw141292 
15284520Snw141292 	/* sanity checks */
15294520Snw141292 	if (gh == NULL)
15304520Snw141292 		return (IDMAP_ERR_ARG);
15314520Snw141292 	if (sidprefix == NULL)
15324520Snw141292 		return (IDMAP_ERR_ARG);
15334520Snw141292 
15347369SJulian.Pullen@Sun.COM 	if ((flag & IDMAP_REQ_FLG_USE_CACHE) &&
15357369SJulian.Pullen@Sun.COM 	    !(flag & IDMAP_REQ_FLG_MAPPING_INFO)) {
15367369SJulian.Pullen@Sun.COM 		retcode = idmap_cache_lookup_sidbygid(sidprefix, rid, gid);
15377369SJulian.Pullen@Sun.COM 		if (retcode  == IDMAP_SUCCESS || retcode == IDMAP_ERR_MEMORY) {
15387369SJulian.Pullen@Sun.COM 			*stat = retcode;
15397369SJulian.Pullen@Sun.COM 			return (retcode);
15407369SJulian.Pullen@Sun.COM 		}
15417369SJulian.Pullen@Sun.COM 	}
15427369SJulian.Pullen@Sun.COM 
15434520Snw141292 	/* Extend the request array and the return list */
15444520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
15454520Snw141292 		goto errout;
15464520Snw141292 
15474520Snw141292 	/* Setup the request */
15484520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
15494520Snw141292 	mapping->flag = flag;
15504520Snw141292 	mapping->id1.idtype = IDMAP_GID;
15514520Snw141292 	mapping->id1.idmap_id_u.gid = gid;
15524520Snw141292 	mapping->id2.idtype = IDMAP_SID;
15534520Snw141292 
15544520Snw141292 	/* Setup pointers for the result */
15554520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
15564520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
15574520Snw141292 	gh->retlist[gh->next].rid = rid;
15584520Snw141292 	gh->retlist[gh->next].stat = stat;
15596386Sjp151216 	gh->retlist[gh->next].info = info;
15607369SJulian.Pullen@Sun.COM 	gh->retlist[gh->next].cache_res = flag & IDMAP_REQ_FLG_USE_CACHE;
15614520Snw141292 
15624520Snw141292 	gh->next++;
15634520Snw141292 	return (IDMAP_SUCCESS);
15644520Snw141292 
15654520Snw141292 errout:
15664644Sbaban 	if (mapping)
15674644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
15684520Snw141292 	errno = idmap_stat2errno(retcode);
15694520Snw141292 	return (retcode);
15704520Snw141292 }
15714520Snw141292 
15724520Snw141292 
15734520Snw141292 /*
15744520Snw141292  * Process the batched "get mapping" requests. The results (i.e.
15754520Snw141292  * status and identity) will be available in the data areas
15764520Snw141292  * provided by individual requests.
15774520Snw141292  */
15784520Snw141292 idmap_stat
15795696Snw141292 idmap_get_mappings(idmap_get_handle_t *gh)
15805696Snw141292 {
15814520Snw141292 	CLIENT		*clnt;
15824520Snw141292 	enum clnt_stat	clntstat;
15834520Snw141292 	idmap_retcode	retcode;
15844520Snw141292 	idmap_ids_res	res;
15857369SJulian.Pullen@Sun.COM 	idmap_id	*res_id;
15864520Snw141292 	int		i;
15877369SJulian.Pullen@Sun.COM 	idmap_id	*req_id;
15887369SJulian.Pullen@Sun.COM 	int		direction;
15894520Snw141292 
15904520Snw141292 	if (gh == NULL) {
15914520Snw141292 		errno = EINVAL;
15924520Snw141292 		return (IDMAP_ERR_ARG);
15934520Snw141292 	}
15944520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(gh->ih, clnt);
15954520Snw141292 
15964520Snw141292 	(void) memset(&res, 0, sizeof (idmap_ids_res));
15974520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_IDS,
15985696Snw141292 	    (xdrproc_t)xdr_idmap_mapping_batch,
15995696Snw141292 	    (caddr_t)&gh->batch,
16005696Snw141292 	    (xdrproc_t)xdr_idmap_ids_res,
16015696Snw141292 	    (caddr_t)&res,
16025696Snw141292 	    TIMEOUT);
16034520Snw141292 	if (clntstat != RPC_SUCCESS) {
16044644Sbaban 		retcode = _idmap_rpc2stat(clnt);
16054520Snw141292 		goto out;
16064520Snw141292 	}
16074520Snw141292 	if (res.retcode != IDMAP_SUCCESS) {
16084520Snw141292 		retcode = res.retcode;
16094520Snw141292 		goto out;
16104520Snw141292 	}
16114520Snw141292 	for (i = 0; i < gh->next; i++) {
16124520Snw141292 		if (i >= res.ids.ids_len) {
16134520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
16144520Snw141292 			continue;
16154520Snw141292 		}
16164520Snw141292 		*gh->retlist[i].stat = res.ids.ids_val[i].retcode;
16177369SJulian.Pullen@Sun.COM 		res_id = &res.ids.ids_val[i].id;
16187369SJulian.Pullen@Sun.COM 		direction = res.ids.ids_val[i].direction;
16197369SJulian.Pullen@Sun.COM 		req_id = &gh->batch.idmap_mapping_batch_val[i].id1;
16207369SJulian.Pullen@Sun.COM 		switch (res_id->idtype) {
16214520Snw141292 		case IDMAP_UID:
16224520Snw141292 			if (gh->retlist[i].uid)
16237369SJulian.Pullen@Sun.COM 				*gh->retlist[i].uid = res_id->idmap_id_u.uid;
16244520Snw141292 			if (gh->retlist[i].is_user)
16254520Snw141292 				*gh->retlist[i].is_user = 1;
16267369SJulian.Pullen@Sun.COM 
16277369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
16287369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
16297369SJulian.Pullen@Sun.COM 				if (gh->retlist[i].is_user != NULL)
16307369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2pid(
16317369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16327369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16337369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.uid, 1,
16347369SJulian.Pullen@Sun.COM 					    direction);
16357369SJulian.Pullen@Sun.COM 				else
16367369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2uid(
16377369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16387369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16397369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.uid,
16407369SJulian.Pullen@Sun.COM 					    direction);
16417369SJulian.Pullen@Sun.COM 			}
16424520Snw141292 			break;
16437369SJulian.Pullen@Sun.COM 
16444520Snw141292 		case IDMAP_GID:
16454520Snw141292 			if (gh->retlist[i].gid)
16467369SJulian.Pullen@Sun.COM 				*gh->retlist[i].gid = res_id->idmap_id_u.gid;
16474520Snw141292 			if (gh->retlist[i].is_user)
16484520Snw141292 				*gh->retlist[i].is_user = 0;
16497369SJulian.Pullen@Sun.COM 
16507369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
16517369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
16527369SJulian.Pullen@Sun.COM 				if (gh->retlist[i].is_user != NULL)
16537369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2pid(
16547369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16557369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16567369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.gid, 0,
16577369SJulian.Pullen@Sun.COM 					    direction);
16587369SJulian.Pullen@Sun.COM 				else
16597369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2gid(
16607369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.prefix,
16617369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.sid.rid,
16627369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.gid,
16637369SJulian.Pullen@Sun.COM 					    direction);
16647369SJulian.Pullen@Sun.COM 			}
16654520Snw141292 			break;
16667369SJulian.Pullen@Sun.COM 
16674864Sbaban 		case IDMAP_POSIXID:
16684864Sbaban 			if (gh->retlist[i].uid)
16694864Sbaban 				*gh->retlist[i].uid = 60001;
16704864Sbaban 			if (gh->retlist[i].is_user)
16714864Sbaban 				*gh->retlist[i].is_user = -1;
16724864Sbaban 			break;
16737369SJulian.Pullen@Sun.COM 
16744520Snw141292 		case IDMAP_SID:
16755696Snw141292 		case IDMAP_USID:
16765696Snw141292 		case IDMAP_GSID:
16774520Snw141292 			if (gh->retlist[i].rid)
16787369SJulian.Pullen@Sun.COM 				*gh->retlist[i].rid =
16797369SJulian.Pullen@Sun.COM 				    res_id->idmap_id_u.sid.rid;
16804520Snw141292 			if (gh->retlist[i].sidprefix) {
16817369SJulian.Pullen@Sun.COM 				if (res_id->idmap_id_u.sid.prefix == NULL ||
16827369SJulian.Pullen@Sun.COM 				    *res_id->idmap_id_u.sid.prefix == '\0') {
16834520Snw141292 					*gh->retlist[i].sidprefix = NULL;
16844520Snw141292 					break;
16854520Snw141292 				}
16864520Snw141292 				*gh->retlist[i].sidprefix =
16877369SJulian.Pullen@Sun.COM 				    strdup(res_id->idmap_id_u.sid.prefix);
16884520Snw141292 				if (*gh->retlist[i].sidprefix == NULL)
16894520Snw141292 					*gh->retlist[i].stat =
16905696Snw141292 					    IDMAP_ERR_MEMORY;
16914520Snw141292 			}
16927369SJulian.Pullen@Sun.COM 			if (res.ids.ids_val[i].retcode == IDMAP_SUCCESS &&
16937369SJulian.Pullen@Sun.COM 			    gh->retlist[i].cache_res) {
16947369SJulian.Pullen@Sun.COM 				if (req_id->idtype == IDMAP_UID)
16957369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2uid(
16967369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.prefix,
16977369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.rid,
16987369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.uid,
16997369SJulian.Pullen@Sun.COM 					    direction);
17007369SJulian.Pullen@Sun.COM 				else /* req_id->idtype == IDMAP_GID */
17017369SJulian.Pullen@Sun.COM 					idmap_cache_add_sid2gid(
17027369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.prefix,
17037369SJulian.Pullen@Sun.COM 					    res_id->idmap_id_u.sid.rid,
17047369SJulian.Pullen@Sun.COM 					    req_id->idmap_id_u.gid,
17057369SJulian.Pullen@Sun.COM 					    direction);
17067369SJulian.Pullen@Sun.COM 			}
17074520Snw141292 			break;
17087369SJulian.Pullen@Sun.COM 
17094520Snw141292 		case IDMAP_NONE:
17104520Snw141292 			break;
17117369SJulian.Pullen@Sun.COM 
17124520Snw141292 		default:
17134520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
17144520Snw141292 			break;
17154520Snw141292 		}
17166386Sjp151216 		if (gh->retlist[i].info != NULL)
17176386Sjp151216 			(void) idmap_info_cpy(gh->retlist[i].info,
17186386Sjp151216 			    &res.ids.ids_val[i].info);
17194520Snw141292 	}
17204520Snw141292 	retcode = IDMAP_SUCCESS;
17214520Snw141292 
17224520Snw141292 out:
17234644Sbaban 	_IDMAP_RESET_GET_HANDLE(gh);
17244520Snw141292 	(void) xdr_free(xdr_idmap_ids_res, (caddr_t)&res);
17254520Snw141292 	errno = idmap_stat2errno(retcode);
17264520Snw141292 	return (retcode);
17274520Snw141292 }
17284520Snw141292 
17294520Snw141292 
17304520Snw141292 /*
17314520Snw141292  * Destroy the "get mapping" handle
17324520Snw141292  */
17334520Snw141292 void
17345696Snw141292 idmap_get_destroy(idmap_get_handle_t *gh)
17355696Snw141292 {
17364520Snw141292 	if (gh == NULL)
17374520Snw141292 		return;
17384520Snw141292 	(void) xdr_free(xdr_idmap_mapping_batch, (caddr_t)&gh->batch);
17394520Snw141292 	if (gh->retlist)
17404520Snw141292 		free(gh->retlist);
17414520Snw141292 	free(gh);
17424520Snw141292 }
17434520Snw141292 
17444520Snw141292 
17454520Snw141292 /*
17464520Snw141292  * Get windows to unix mapping
17474520Snw141292  */
17484520Snw141292 idmap_stat
17494520Snw141292 idmap_get_w2u_mapping(idmap_handle_t *handle,
17504520Snw141292 		const char *sidprefix, idmap_rid_t *rid,
17514520Snw141292 		const char *winname, const char *windomain,
17525696Snw141292 		int flag, int *is_user, int *is_wuser,
17536386Sjp151216 		uid_t *pid, char **unixname, int *direction, idmap_info *info)
17545696Snw141292 {
17554520Snw141292 	CLIENT			*clnt;
17564520Snw141292 	enum clnt_stat		clntstat;
17574520Snw141292 	idmap_mapping		request, *mapping;
17584520Snw141292 	idmap_mappings_res	result;
17594520Snw141292 	idmap_retcode		retcode, rc;
17604520Snw141292 
17614520Snw141292 	if (handle == NULL) {
17624520Snw141292 		errno = EINVAL;
17634520Snw141292 		return (IDMAP_ERR_ARG);
17644520Snw141292 	}
17654520Snw141292 
17664520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
17674520Snw141292 
17684520Snw141292 	(void) memset(&request, 0, sizeof (request));
17694520Snw141292 	(void) memset(&result, 0, sizeof (result));
17704520Snw141292 
17714520Snw141292 	if (pid)
17724520Snw141292 		*pid = UINT32_MAX;
17734520Snw141292 	if (unixname)
17744520Snw141292 		*unixname = NULL;
17754520Snw141292 	if (direction)
17764644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
17774520Snw141292 
17784520Snw141292 	request.flag = flag;
17794520Snw141292 	request.id1.idtype = IDMAP_SID;
17804520Snw141292 	if (sidprefix && rid) {
17814520Snw141292 		request.id1.idmap_id_u.sid.prefix = (char *)sidprefix;
17824520Snw141292 		request.id1.idmap_id_u.sid.rid = *rid;
17834520Snw141292 	} else if (winname) {
17845064Sdm199847 		retcode = idmap_strdupnull(&request.id1name, winname);
17855247Sbaban 		if (retcode != IDMAP_SUCCESS)
17864520Snw141292 			goto out;
17875064Sdm199847 
17885064Sdm199847 		retcode = idmap_strdupnull(&request.id1domain, windomain);
17895247Sbaban 		if (retcode != IDMAP_SUCCESS)
17905064Sdm199847 			goto out;
17915064Sdm199847 
17924520Snw141292 		request.id1.idmap_id_u.sid.prefix = NULL;
17934520Snw141292 	} else {
17944520Snw141292 		errno = EINVAL;
17954520Snw141292 		return (IDMAP_ERR_ARG);
17964520Snw141292 	}
17974520Snw141292 
17985696Snw141292 	if (*is_user == 1)
17994520Snw141292 		request.id2.idtype = IDMAP_UID;
18004520Snw141292 	else if (*is_user == 0)
18014520Snw141292 		request.id2.idtype = IDMAP_GID;
18024520Snw141292 	else
18034520Snw141292 		request.id2.idtype = IDMAP_POSIXID;
18044520Snw141292 
18055696Snw141292 	if (*is_wuser == 1)
18065696Snw141292 		request.id1.idtype = IDMAP_USID;
18075696Snw141292 	else if (*is_wuser == 0)
18085696Snw141292 		request.id1.idtype = IDMAP_GSID;
18095696Snw141292 	else
18105696Snw141292 		request.id1.idtype = IDMAP_SID;
18115696Snw141292 
18124520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
18135696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
18145696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
18155696Snw141292 	    TIMEOUT);
18164520Snw141292 
18174644Sbaban 	if (clntstat != RPC_SUCCESS)
18184644Sbaban 		return (_idmap_rpc2stat(clnt));
18194520Snw141292 
18204520Snw141292 	retcode = result.retcode;
18214520Snw141292 
18224520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
18234520Snw141292 		if (retcode == IDMAP_SUCCESS)
18244520Snw141292 			retcode = IDMAP_ERR_NORESULT;
18254520Snw141292 		goto out;
18264520Snw141292 	}
18274520Snw141292 
18284864Sbaban 	if (mapping->id2.idtype == IDMAP_UID) {
18295696Snw141292 		*is_user = 1;
18304864Sbaban 	} else if (mapping->id2.idtype == IDMAP_GID) {
18315696Snw141292 		*is_user = 0;
18324864Sbaban 	} else {
18334864Sbaban 		goto out;
18344864Sbaban 	}
18355696Snw141292 
18365696Snw141292 	if (mapping->id1.idtype == IDMAP_USID) {
18375696Snw141292 		*is_wuser = 1;
18385696Snw141292 	} else if (mapping->id1.idtype == IDMAP_GSID) {
18395696Snw141292 		*is_wuser = 0;
18405696Snw141292 	} else {
18415696Snw141292 		goto out;
18425696Snw141292 	}
18435696Snw141292 
18444520Snw141292 	if (direction)
18454520Snw141292 		*direction = mapping->direction;
18464520Snw141292 	if (pid)
18474520Snw141292 		*pid = mapping->id2.idmap_id_u.uid;
18485064Sdm199847 
18495064Sdm199847 	rc = idmap_strdupnull(unixname, mapping->id2name);
18505064Sdm199847 	if (rc != IDMAP_SUCCESS)
18515064Sdm199847 		retcode = rc;
18524520Snw141292 
18536386Sjp151216 	rc = idmap_info_cpy(info, &mapping->info);
18546386Sjp151216 	if (rc != IDMAP_SUCCESS)
18556386Sjp151216 		retcode = rc;
18566386Sjp151216 
18574520Snw141292 out:
18586966Sjp151216 	if (request.id1name != NULL)
18596966Sjp151216 		free(request.id1name);
18606966Sjp151216 	if (request.id1domain != NULL)
18616966Sjp151216 		free(request.id1domain);
18624520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
18634520Snw141292 	if (retcode != IDMAP_SUCCESS)
18644520Snw141292 		errno = idmap_stat2errno(retcode);
18654520Snw141292 	return (retcode);
18664520Snw141292 }
18674520Snw141292 
18684520Snw141292 
18694520Snw141292 /*
18704520Snw141292  * Get unix to windows mapping
18714520Snw141292  */
18724520Snw141292 idmap_stat
18734520Snw141292 idmap_get_u2w_mapping(idmap_handle_t *handle,
18744520Snw141292 		uid_t *pid, const char *unixname,
18755696Snw141292 		int flag, int is_user, int *is_wuser,
18764520Snw141292 		char **sidprefix, idmap_rid_t *rid,
18774520Snw141292 		char **winname, char **windomain,
18786386Sjp151216 		int *direction, idmap_info *info)
18795696Snw141292 {
18804520Snw141292 	CLIENT			*clnt;
18814520Snw141292 	enum clnt_stat		clntstat;
18824520Snw141292 	idmap_mapping		request, *mapping;
18834520Snw141292 	idmap_mappings_res	result;
18844520Snw141292 	idmap_retcode		retcode, rc;
18854520Snw141292 
18864520Snw141292 	if (handle == NULL) {
18874520Snw141292 		errno = EINVAL;
18884520Snw141292 		return (IDMAP_ERR_ARG);
18894520Snw141292 	}
18904520Snw141292 
18914520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
18924520Snw141292 
18934520Snw141292 	if (sidprefix)
18944520Snw141292 		*sidprefix = NULL;
18954520Snw141292 	if (winname)
18964520Snw141292 		*winname = NULL;
18974520Snw141292 	if (windomain)
18984520Snw141292 		*windomain = NULL;
18994520Snw141292 	if (rid)
19004520Snw141292 		*rid = UINT32_MAX;
19014520Snw141292 	if (direction)
19024644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
19034520Snw141292 
19044520Snw141292 	(void) memset(&request, 0, sizeof (request));
19054520Snw141292 	(void) memset(&result, 0, sizeof (result));
19064520Snw141292 
19074520Snw141292 	request.flag = flag;
19084520Snw141292 	request.id1.idtype = is_user?IDMAP_UID:IDMAP_GID;
19094520Snw141292 
19104520Snw141292 	if (pid && *pid != UINT32_MAX) {
19114520Snw141292 		request.id1.idmap_id_u.uid = *pid;
19124520Snw141292 	} else if (unixname) {
19135064Sdm199847 		request.id1name = (char *)unixname;
19144520Snw141292 		request.id1.idmap_id_u.uid = UINT32_MAX;
19154520Snw141292 	} else {
19164520Snw141292 		errno = EINVAL;
19174520Snw141292 		return (IDMAP_ERR_ARG);
19184520Snw141292 	}
19194520Snw141292 
19205696Snw141292 	if (is_wuser == NULL)
19215696Snw141292 		request.id2.idtype = IDMAP_SID;
19225696Snw141292 	else if (*is_wuser == -1)
19235696Snw141292 		request.id2.idtype = IDMAP_SID;
19245696Snw141292 	else if (*is_wuser == 0)
19255696Snw141292 		request.id2.idtype = IDMAP_GSID;
19265696Snw141292 	else if (*is_wuser == 1)
19275696Snw141292 		request.id2.idtype = IDMAP_USID;
19284520Snw141292 
19294520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
19305696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
19315696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
19325696Snw141292 	    TIMEOUT);
19334520Snw141292 
19344644Sbaban 	if (clntstat != RPC_SUCCESS)
19354644Sbaban 		return (_idmap_rpc2stat(clnt));
19364520Snw141292 
19374520Snw141292 	retcode = result.retcode;
19384520Snw141292 
19394520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
19404520Snw141292 		if (retcode == IDMAP_SUCCESS)
19414520Snw141292 			retcode = IDMAP_ERR_NORESULT;
19424520Snw141292 		goto out;
19434520Snw141292 	}
19444520Snw141292 
19455696Snw141292 	if (direction != NULL)
19464520Snw141292 		*direction = mapping->direction;
19475696Snw141292 
19486386Sjp151216 	if (is_wuser != NULL) {
19496386Sjp151216 		if (mapping->id2.idtype == IDMAP_USID)
19506386Sjp151216 			*is_wuser = 1;
19516386Sjp151216 		else if (mapping->id2.idtype == IDMAP_GSID)
19526386Sjp151216 			*is_wuser = 0;
19536386Sjp151216 		else
19546386Sjp151216 			*is_wuser = -1;
19556386Sjp151216 	}
19565696Snw141292 
19574695Sbaban 	if (sidprefix && mapping->id2.idmap_id_u.sid.prefix &&
19584695Sbaban 	    *mapping->id2.idmap_id_u.sid.prefix != '\0') {
19594520Snw141292 		*sidprefix = strdup(mapping->id2.idmap_id_u.sid.prefix);
19604520Snw141292 		if (*sidprefix == NULL) {
19614520Snw141292 			retcode = IDMAP_ERR_MEMORY;
19624520Snw141292 			goto errout;
19634520Snw141292 		}
19644520Snw141292 	}
19654520Snw141292 	if (rid)
19664520Snw141292 		*rid = mapping->id2.idmap_id_u.sid.rid;
19675064Sdm199847 
19685064Sdm199847 	rc = idmap_strdupnull(winname, mapping->id2name);
19695064Sdm199847 	if (rc != IDMAP_SUCCESS)
19705064Sdm199847 		retcode = rc;
19715064Sdm199847 
19725064Sdm199847 	rc = idmap_strdupnull(windomain, mapping->id2domain);
19735064Sdm199847 	if (rc != IDMAP_SUCCESS)
19745064Sdm199847 		retcode = rc;
19754520Snw141292 
19766386Sjp151216 	rc = idmap_info_cpy(info, &mapping->info);
19776386Sjp151216 	if (rc != IDMAP_SUCCESS)
19786386Sjp151216 		retcode = rc;
19796386Sjp151216 
19804520Snw141292 	goto out;
19814520Snw141292 
19824520Snw141292 errout:
19834520Snw141292 	if (sidprefix && *sidprefix) {
19844520Snw141292 		free(*sidprefix);
19854520Snw141292 		*sidprefix = NULL;
19864520Snw141292 	}
19874520Snw141292 	if (winname && *winname) {
19884520Snw141292 		free(*winname);
19894520Snw141292 		*winname = NULL;
19904520Snw141292 	}
19914520Snw141292 	if (windomain && *windomain) {
19924520Snw141292 		free(*windomain);
19934520Snw141292 		*windomain = NULL;
19944520Snw141292 	}
19954520Snw141292 
19964520Snw141292 out:
19974520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
19984520Snw141292 	if (retcode != IDMAP_SUCCESS)
19994520Snw141292 		errno = idmap_stat2errno(retcode);
20004520Snw141292 	return (retcode);
20014520Snw141292 }
20024520Snw141292 
20034520Snw141292 
20044520Snw141292 
20054520Snw141292 #define	gettext(s)	s
20064520Snw141292 static stat_table_t stattable[] = {
20074520Snw141292 	{IDMAP_SUCCESS, gettext("Success"), 0},
20084520Snw141292 	{IDMAP_NEXT, gettext("More results available"), 0},
20094520Snw141292 	{IDMAP_ERR_OTHER, gettext("Undefined error"), EINVAL},
20104520Snw141292 	{IDMAP_ERR_INTERNAL, gettext("Internal error"), EINVAL},
20114520Snw141292 	{IDMAP_ERR_MEMORY, gettext("Out of memory"), ENOMEM},
20124520Snw141292 	{IDMAP_ERR_NORESULT, gettext("No results available"), EINVAL},
20134520Snw141292 	{IDMAP_ERR_NOTUSER, gettext("Not a user"), EINVAL},
20144520Snw141292 	{IDMAP_ERR_NOTGROUP, gettext("Not a group"), EINVAL},
20154644Sbaban 	{IDMAP_ERR_NOTSUPPORTED, gettext("Operation not supported"), ENOTSUP},
20164520Snw141292 	{IDMAP_ERR_W2U_NAMERULE,
20174520Snw141292 		gettext("Invalid Windows to UNIX name-based rule"), EINVAL},
20184520Snw141292 	{IDMAP_ERR_U2W_NAMERULE,
20194520Snw141292 		gettext("Invalid UNIX to Windows name-based rule"), EINVAL},
20204520Snw141292 	{IDMAP_ERR_CACHE, gettext("Invalid cache"), EINVAL},
20214520Snw141292 	{IDMAP_ERR_DB, gettext("Invalid database"), EINVAL},
20224520Snw141292 	{IDMAP_ERR_ARG, gettext("Invalid argument"), EINVAL},
20234520Snw141292 	{IDMAP_ERR_SID, gettext("Invalid SID"), EINVAL},
20244520Snw141292 	{IDMAP_ERR_IDTYPE, gettext("Invalid identity type"), EINVAL},
20254644Sbaban 	{IDMAP_ERR_RPC_HANDLE, gettext("Bad RPC handle"), EBADF},
20264520Snw141292 	{IDMAP_ERR_RPC, gettext("RPC error"), EINVAL},
20274520Snw141292 	{IDMAP_ERR_CLIENT_HANDLE, gettext("Bad client handle"), EINVAL},
20284644Sbaban 	{IDMAP_ERR_BUSY, gettext("Server is busy"), EBUSY},
20294695Sbaban 	{IDMAP_ERR_PERMISSION_DENIED, gettext("Permission denied"), EACCES},
20304520Snw141292 	{IDMAP_ERR_NOMAPPING,
20314520Snw141292 		gettext("Mapping not found or inhibited"), EINVAL},
20324520Snw141292 	{IDMAP_ERR_NEW_ID_ALLOC_REQD,
20334520Snw141292 		gettext("New mapping needs to be created"), EINVAL},
20344520Snw141292 	{IDMAP_ERR_DOMAIN, gettext("Invalid domain"), EINVAL},
20354520Snw141292 	{IDMAP_ERR_SECURITY, gettext("Security issue"), EINVAL},
20364520Snw141292 	{IDMAP_ERR_NOTFOUND, gettext("Not found"), EINVAL},
20374520Snw141292 	{IDMAP_ERR_DOMAIN_NOTFOUND, gettext("Domain not found"), EINVAL},
20384520Snw141292 	{IDMAP_ERR_UPDATE_NOTALLOWED, gettext("Update not allowed"), EINVAL},
20394520Snw141292 	{IDMAP_ERR_CFG, gettext("Configuration error"), EINVAL},
20404520Snw141292 	{IDMAP_ERR_CFG_CHANGE, gettext("Invalid configuration change"), EINVAL},
20414520Snw141292 	{IDMAP_ERR_NOTMAPPED_WELLKNOWN,
20424520Snw141292 		gettext("No mapping for well-known SID"), EINVAL},
20434520Snw141292 	{IDMAP_ERR_RETRIABLE_NET_ERR,
20444864Sbaban 		gettext("Windows lookup failed"), EINVAL},
20454864Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
20464864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
20474864Sbaban 		"Windows to UNIX name-based rule"), EINVAL},
20484864Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
20494864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
20504864Sbaban 		"Unix to Windows name-based rule"), EINVAL},
20515968Snw141292 	{IDMAP_ERR_BAD_UTF8,
20525968Snw141292 		gettext("Invalid or illegal UTF-8 sequence found in "
20535968Snw141292 		"a given Windows entity name or domain name"), EINVAL},
2054*8361SJulian.Pullen@Sun.COM 	{IDMAP_ERR_NONE_GENERATED,
20556386Sjp151216 		gettext("Mapping not found and none created (see -c option)"),
20566386Sjp151216 		EINVAL},
20576616Sdm199847 	{IDMAP_ERR_PROP_UNKNOWN,
20586616Sdm199847 		gettext("Undefined property"),
20596616Sdm199847 		EINVAL},
20606616Sdm199847 	{IDMAP_ERR_NS_LDAP_CFG,
20616616Sdm199847 		gettext("Native LDAP configuration error"), EINVAL},
20626616Sdm199847 	{IDMAP_ERR_NS_LDAP_PARTIAL,
20636616Sdm199847 		gettext("Partial result from Native LDAP"), EINVAL},
20646616Sdm199847 	{IDMAP_ERR_NS_LDAP_OP_FAILED,
20656616Sdm199847 		gettext("Native LDAP operation failed"), EINVAL},
20666616Sdm199847 	{IDMAP_ERR_NS_LDAP_BAD_WINNAME,
20676616Sdm199847 		gettext("Improper winname form found in Native LDAP"), EINVAL},
2068*8361SJulian.Pullen@Sun.COM 	{IDMAP_ERR_NO_ACTIVEDIRECTORY,
2069*8361SJulian.Pullen@Sun.COM 		gettext("No AD servers"),
2070*8361SJulian.Pullen@Sun.COM 		EINVAL},
20714520Snw141292 	{-1, NULL, 0}
20724520Snw141292 };
20734520Snw141292 #undef	gettext
20744520Snw141292 
20754520Snw141292 
20764520Snw141292 /*
20774520Snw141292  * Get description of status code
20784520Snw141292  *
20794520Snw141292  * Input:
20804520Snw141292  * status - Status code returned by libidmap API call
20814520Snw141292  *
20824520Snw141292  * Return Value:
20834520Snw141292  * human-readable localized description of idmap_stat
20844520Snw141292  */
20854520Snw141292 /* ARGSUSED */
20864520Snw141292 const char *
20875696Snw141292 idmap_stat2string(idmap_handle_t *handle, idmap_stat status)
20885696Snw141292 {
20894520Snw141292 	int i;
20904520Snw141292 
20914520Snw141292 	for (i = 0; stattable[i].msg; i++) {
20924520Snw141292 		if (stattable[i].retcode == status)
20934526Sbaban 			return (gettext(stattable[i].msg));
20944520Snw141292 	}
20954520Snw141292 	return (gettext("Unknown error"));
20964520Snw141292 }
20974520Snw141292 
20984520Snw141292 
20994520Snw141292 static int
21005696Snw141292 idmap_stat2errno(idmap_stat stat)
21015696Snw141292 {
21024520Snw141292 	int i;
21034520Snw141292 	for (i = 0; stattable[i].msg; i++) {
21044520Snw141292 		if (stattable[i].retcode == stat)
21054520Snw141292 			return (stattable[i].errnum);
21064520Snw141292 	}
21074520Snw141292 	return (EINVAL);
21084520Snw141292 }
21094520Snw141292 
21104520Snw141292 
21114520Snw141292 /*
21124520Snw141292  * Get status code from string
21134520Snw141292  */
21144520Snw141292 idmap_stat
21155696Snw141292 idmap_string2stat(const char *str)
21165696Snw141292 {
21174520Snw141292 	if (str == NULL)
21184520Snw141292 		return (IDMAP_ERR_INTERNAL);
21194520Snw141292 
21204520Snw141292 #define	return_cmp(a) \
21214520Snw141292 	if (0 == strcmp(str, "IDMAP_ERR_" #a)) \
21224520Snw141292 		return (IDMAP_ERR_ ## a);
21234520Snw141292 
21244520Snw141292 	return_cmp(OTHER);
21254520Snw141292 	return_cmp(INTERNAL);
21264520Snw141292 	return_cmp(MEMORY);
21274520Snw141292 	return_cmp(NORESULT);
21284520Snw141292 	return_cmp(NOTUSER);
21294520Snw141292 	return_cmp(NOTGROUP);
21304520Snw141292 	return_cmp(NOTSUPPORTED);
21314520Snw141292 	return_cmp(W2U_NAMERULE);
21324520Snw141292 	return_cmp(U2W_NAMERULE);
21334520Snw141292 	return_cmp(CACHE);
21344520Snw141292 	return_cmp(DB);
21354520Snw141292 	return_cmp(ARG);
21364520Snw141292 	return_cmp(SID);
21374520Snw141292 	return_cmp(IDTYPE);
21384520Snw141292 	return_cmp(RPC_HANDLE);
21394520Snw141292 	return_cmp(RPC);
21404520Snw141292 	return_cmp(CLIENT_HANDLE);
21414520Snw141292 	return_cmp(BUSY);
21424520Snw141292 	return_cmp(PERMISSION_DENIED);
21434520Snw141292 	return_cmp(NOMAPPING);
21444520Snw141292 	return_cmp(NEW_ID_ALLOC_REQD);
21454520Snw141292 	return_cmp(DOMAIN);
21464520Snw141292 	return_cmp(SECURITY);
21474520Snw141292 	return_cmp(NOTFOUND);
21484520Snw141292 	return_cmp(DOMAIN_NOTFOUND);
21494520Snw141292 	return_cmp(MEMORY);
21504520Snw141292 	return_cmp(UPDATE_NOTALLOWED);
21514520Snw141292 	return_cmp(CFG);
21524520Snw141292 	return_cmp(CFG_CHANGE);
21534520Snw141292 	return_cmp(NOTMAPPED_WELLKNOWN);
21544520Snw141292 	return_cmp(RETRIABLE_NET_ERR);
21554864Sbaban 	return_cmp(W2U_NAMERULE_CONFLICT);
21564864Sbaban 	return_cmp(U2W_NAMERULE_CONFLICT);
21576616Sdm199847 	return_cmp(BAD_UTF8);
2158*8361SJulian.Pullen@Sun.COM 	return_cmp(NONE_GENERATED);
21596616Sdm199847 	return_cmp(PROP_UNKNOWN);
21606616Sdm199847 	return_cmp(NS_LDAP_CFG);
21616616Sdm199847 	return_cmp(NS_LDAP_PARTIAL);
21626616Sdm199847 	return_cmp(NS_LDAP_OP_FAILED);
21636616Sdm199847 	return_cmp(NS_LDAP_BAD_WINNAME);
2164*8361SJulian.Pullen@Sun.COM 	return_cmp(NO_ACTIVEDIRECTORY);
21654520Snw141292 #undef return_cmp
21664520Snw141292 
21674520Snw141292 	return (IDMAP_ERR_OTHER);
21684520Snw141292 }
21694520Snw141292 
21704520Snw141292 
21714520Snw141292 /*
21724520Snw141292  * Map the given status to one that can be returned by the protocol
21734520Snw141292  */
21744520Snw141292 idmap_stat
21755696Snw141292 idmap_stat4prot(idmap_stat status)
21765696Snw141292 {
21774520Snw141292 	switch (status) {
21784520Snw141292 	case IDMAP_ERR_MEMORY:
21794520Snw141292 	case IDMAP_ERR_CACHE:
21804520Snw141292 		return (IDMAP_ERR_INTERNAL);
21814520Snw141292 	}
21824520Snw141292 	return (status);
21834520Snw141292 }
21845043Sbaban 
21855043Sbaban 
21865043Sbaban /*
21875247Sbaban  * This is a convenience routine which duplicates a string after
21885247Sbaban  * checking for NULL pointers. This function will return success if
21895247Sbaban  * either the 'to' OR 'from' pointers are NULL.
21905064Sdm199847  */
21915064Sdm199847 static idmap_stat
21925696Snw141292 idmap_strdupnull(char **to, const char *from)
21935696Snw141292 {
21945247Sbaban 	if (to == NULL)
21955247Sbaban 		return (IDMAP_SUCCESS);
21965247Sbaban 
21975064Sdm199847 	if (from == NULL || *from == '\0') {
21985064Sdm199847 		*to = NULL;
21995064Sdm199847 		return (IDMAP_SUCCESS);
22005064Sdm199847 	}
22015064Sdm199847 
22025064Sdm199847 	*to = strdup(from);
22035064Sdm199847 	if (*to == NULL)
22045064Sdm199847 		return (IDMAP_ERR_MEMORY);
22055064Sdm199847 	return (IDMAP_SUCCESS);
22065064Sdm199847 }
22075064Sdm199847 
22086386Sjp151216 
22095064Sdm199847 idmap_stat
22105696Snw141292 idmap_namerule_cpy(idmap_namerule *to, idmap_namerule *from)
22115696Snw141292 {
22125064Sdm199847 	idmap_stat retval;
22135064Sdm199847 
22146386Sjp151216 	if (to == NULL)
22156386Sjp151216 		return (IDMAP_SUCCESS);
22166386Sjp151216 
22175064Sdm199847 	(void) memcpy(to, from, sizeof (idmap_namerule));
22186386Sjp151216 	to->windomain = NULL;
22196386Sjp151216 	to->winname = NULL;
22206386Sjp151216 	to->unixname = NULL;
22215064Sdm199847 
22225064Sdm199847 	retval = idmap_strdupnull(&to->windomain, from->windomain);
22235064Sdm199847 	if (retval != IDMAP_SUCCESS)
22245064Sdm199847 		return (retval);
22255064Sdm199847 
22265064Sdm199847 	retval = idmap_strdupnull(&to->winname, from->winname);
22276386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22286386Sjp151216 		free(to->windomain);
22296386Sjp151216 		to->windomain = NULL;
22306386Sjp151216 		return (retval);
22316386Sjp151216 	}
22326386Sjp151216 
22336386Sjp151216 	retval = idmap_strdupnull(&to->unixname, from->unixname);
22346386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22356386Sjp151216 		free(to->windomain);
22366386Sjp151216 		to->windomain = NULL;
22376386Sjp151216 		free(to->winname);
22386386Sjp151216 		to->winname = NULL;
22396386Sjp151216 		return (retval);
22406386Sjp151216 	}
22416386Sjp151216 
22426386Sjp151216 	return (retval);
22436386Sjp151216 }
22446386Sjp151216 
22456386Sjp151216 
22466386Sjp151216 static
22476386Sjp151216 idmap_stat
22486386Sjp151216 idmap_how_ds_based_cpy(idmap_how_ds_based *to, idmap_how_ds_based *from)
22496386Sjp151216 {
22506386Sjp151216 	idmap_stat retval;
22516386Sjp151216 
22526386Sjp151216 	if (to == NULL)
22536386Sjp151216 		return (IDMAP_SUCCESS);
22546386Sjp151216 
22556386Sjp151216 	retval = idmap_strdupnull(&to->dn, from->dn);
22565064Sdm199847 	if (retval != IDMAP_SUCCESS)
22575064Sdm199847 		return (retval);
22585064Sdm199847 
22596386Sjp151216 	retval = idmap_strdupnull(&to->attr, from->attr);
22606386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22616386Sjp151216 		free(to->dn);
22626386Sjp151216 		to->dn = NULL;
22636386Sjp151216 		return (retval);
22646386Sjp151216 	}
22656386Sjp151216 
22666386Sjp151216 	retval = idmap_strdupnull(&to->value, from->value);
22676386Sjp151216 	if (retval != IDMAP_SUCCESS) {
22686386Sjp151216 		free(to->dn);
22696386Sjp151216 		to->dn = NULL;
22706386Sjp151216 		free(to->attr);
22716386Sjp151216 		to->attr = NULL;
22726386Sjp151216 		return (retval);
22736386Sjp151216 	}
22745064Sdm199847 
22755064Sdm199847 	return (retval);
22765064Sdm199847 }
22775064Sdm199847 
22785064Sdm199847 
22796386Sjp151216 idmap_stat
22806386Sjp151216 idmap_info_cpy(idmap_info *to, idmap_info *from)
22816386Sjp151216 {
22826386Sjp151216 	idmap_stat retval = IDMAP_SUCCESS;
22836386Sjp151216 
22846386Sjp151216 	if (to == NULL)
22856386Sjp151216 		return (IDMAP_SUCCESS);
22866386Sjp151216 
22876386Sjp151216 	(void) memset(to, 0, sizeof (idmap_info));
22886386Sjp151216 
22896386Sjp151216 	to->src = from->src;
22906386Sjp151216 	to->how.map_type = from->how.map_type;
22916386Sjp151216 	switch (to->how.map_type) {
22926386Sjp151216 	case IDMAP_MAP_TYPE_DS_AD:
22936386Sjp151216 		retval = idmap_how_ds_based_cpy(&to->how.idmap_how_u.ad,
22946386Sjp151216 		    &from->how.idmap_how_u.ad);
22956386Sjp151216 		break;
22966386Sjp151216 
22976386Sjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
22986386Sjp151216 		retval = idmap_how_ds_based_cpy(&to->how.idmap_how_u.nldap,
22996386Sjp151216 		    &from->how.idmap_how_u.nldap);
23006386Sjp151216 		break;
23016386Sjp151216 
23026386Sjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
23036386Sjp151216 		retval = idmap_namerule_cpy(&to->how.idmap_how_u.rule,
23046386Sjp151216 		    &from->how.idmap_how_u.rule);
23056386Sjp151216 		break;
23066386Sjp151216 
23076386Sjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
23086386Sjp151216 		break;
23096386Sjp151216 
23106386Sjp151216 	case IDMAP_MAP_TYPE_LOCAL_SID:
23116386Sjp151216 		break;
23126386Sjp151216 
23136386Sjp151216 	case IDMAP_MAP_TYPE_KNOWN_SID:
23146386Sjp151216 		break;
23156386Sjp151216 	}
23166386Sjp151216 	return (retval);
23176386Sjp151216 }
23186386Sjp151216 
23196386Sjp151216 
23206386Sjp151216 /*
23216386Sjp151216  * This routine is similar to idmap_info_cpy, but the strings
23226386Sjp151216  * are moved from the "from" info to the "to" info.
23236386Sjp151216  * This routine is equivelent of:
23246386Sjp151216  *
23256386Sjp151216  *	idmap_info_cpy(to,from);
23266386Sjp151216  *	idmap_info_free(from);
23276386Sjp151216  */
23286386Sjp151216 idmap_stat
23296386Sjp151216 idmap_info_mov(idmap_info *to, idmap_info *from)
23306386Sjp151216 {
23316386Sjp151216 	idmap_stat retval = IDMAP_SUCCESS;
23326386Sjp151216 
23336386Sjp151216 	if (to == NULL) {
23346386Sjp151216 		idmap_info_free(from);
23356386Sjp151216 		return (IDMAP_SUCCESS);
23366386Sjp151216 	}
23376386Sjp151216 	(void) memcpy(to, from, sizeof (idmap_info));
23386386Sjp151216 
23396386Sjp151216 	(void) memset(from, 0, sizeof (idmap_info));
23406386Sjp151216 
23416386Sjp151216 	return (retval);
23426386Sjp151216 }
23436386Sjp151216 
23446386Sjp151216 
23456386Sjp151216 void
23466386Sjp151216 idmap_info_free(idmap_info *info)
23476386Sjp151216 {
23486386Sjp151216 	idmap_how *how;
23496386Sjp151216 
23506386Sjp151216 	if (info == NULL)
23516386Sjp151216 		return;
23526386Sjp151216 
23536386Sjp151216 	how = &info->how;
23546386Sjp151216 	switch (how->map_type) {
23556386Sjp151216 	case IDMAP_MAP_TYPE_DS_AD:
23566386Sjp151216 		free(how->idmap_how_u.ad.dn);
23576386Sjp151216 		how->idmap_how_u.ad.dn = NULL;
23586386Sjp151216 		free(how->idmap_how_u.ad.attr);
23596386Sjp151216 		how->idmap_how_u.ad.attr = NULL;
23606386Sjp151216 		free(how->idmap_how_u.ad.value);
23616386Sjp151216 		how->idmap_how_u.ad.value = NULL;
23626386Sjp151216 		break;
23636386Sjp151216 
23646386Sjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
23656386Sjp151216 		free(how->idmap_how_u.nldap.dn);
23666386Sjp151216 		how->idmap_how_u.nldap.dn = NULL;
23676386Sjp151216 		free(how->idmap_how_u.nldap.attr);
23686386Sjp151216 		how->idmap_how_u.nldap.attr = NULL;
23696386Sjp151216 		free(how->idmap_how_u.nldap.value);
23706386Sjp151216 		how->idmap_how_u.nldap.value = NULL;
23716386Sjp151216 		break;
23726386Sjp151216 
23736386Sjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
23746386Sjp151216 		free(how->idmap_how_u.rule.windomain);
23756386Sjp151216 		how->idmap_how_u.rule.windomain = NULL;
23766386Sjp151216 		free(how->idmap_how_u.rule.winname);
23776386Sjp151216 		how->idmap_how_u.rule.winname = NULL;
23786386Sjp151216 		free(how->idmap_how_u.rule.unixname);
23796386Sjp151216 		how->idmap_how_u.rule.unixname = NULL;
23806386Sjp151216 		break;
23816386Sjp151216 
23826386Sjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
23836386Sjp151216 		break;
23846386Sjp151216 
23856386Sjp151216 	case IDMAP_MAP_TYPE_LOCAL_SID:
23866386Sjp151216 		break;
23876386Sjp151216 	}
23886386Sjp151216 	how->map_type = IDMAP_MAP_TYPE_UNKNOWN;
23896386Sjp151216 	info->src = IDMAP_MAP_SRC_UNKNOWN;
23906386Sjp151216 }
23916386Sjp151216 
23926386Sjp151216 
23935064Sdm199847 /*
23945043Sbaban  * Get uid given Windows name
23955043Sbaban  */
23965043Sbaban idmap_stat
23977369SJulian.Pullen@Sun.COM idmap_getuidbywinname(const char *name, const char *domain, int flag,
23987369SJulian.Pullen@Sun.COM 	uid_t *uid)
23995696Snw141292 {
24005043Sbaban 	idmap_handle_t	*ih;
24015043Sbaban 	idmap_retcode	rc;
24025696Snw141292 	int		is_user = 1;
24035696Snw141292 	int		is_wuser = -1;
24047369SJulian.Pullen@Sun.COM 	int 		direction;
24055043Sbaban 
24065043Sbaban 	if (uid == NULL)
24075043Sbaban 		return (IDMAP_ERR_ARG);
24085043Sbaban 
24097369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
24107369SJulian.Pullen@Sun.COM 		rc = idmap_cache_lookup_uidbywinname(name, domain, uid);
24117369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
24127369SJulian.Pullen@Sun.COM 			return (rc);
24137369SJulian.Pullen@Sun.COM 	}
24145043Sbaban 	/* Get mapping */
24155043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
24165043Sbaban 		return (rc);
24177369SJulian.Pullen@Sun.COM 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, flag,
24187369SJulian.Pullen@Sun.COM 	    &is_user, &is_wuser, uid, NULL, &direction, NULL);
24195043Sbaban 	(void) idmap_fini(ih);
24205043Sbaban 
24217369SJulian.Pullen@Sun.COM 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
24227369SJulian.Pullen@Sun.COM 		/* If we have not got the domain don't store UID to winname */
24237369SJulian.Pullen@Sun.COM 		if (domain == NULL)
24247369SJulian.Pullen@Sun.COM 			direction = IDMAP_DIRECTION_W2U;
24257369SJulian.Pullen@Sun.COM 		idmap_cache_add_winname2uid(name, domain, *uid, direction);
24267369SJulian.Pullen@Sun.COM 	}
24277369SJulian.Pullen@Sun.COM 
24285043Sbaban 	return (rc);
24295043Sbaban }
24305043Sbaban 
24315043Sbaban 
24325043Sbaban /*
24335043Sbaban  * Get gid given Windows name
24345043Sbaban  */
24355043Sbaban idmap_stat
24367369SJulian.Pullen@Sun.COM idmap_getgidbywinname(const char *name, const char *domain, int flag,
24377369SJulian.Pullen@Sun.COM 	gid_t *gid)
24385696Snw141292 {
24395043Sbaban 	idmap_handle_t	*ih;
24405043Sbaban 	idmap_retcode	rc;
24415696Snw141292 	int		is_user = 0;
24425696Snw141292 	int		is_wuser = -1;
24437369SJulian.Pullen@Sun.COM 	int		direction;
24445043Sbaban 
24455043Sbaban 	if (gid == NULL)
24465043Sbaban 		return (IDMAP_ERR_ARG);
24475043Sbaban 
24487369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
24497369SJulian.Pullen@Sun.COM 		rc = idmap_cache_lookup_gidbywinname(name, domain, gid);
24507369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS || rc == IDMAP_ERR_MEMORY)
24517369SJulian.Pullen@Sun.COM 			return (rc);
24527369SJulian.Pullen@Sun.COM 	}
24537369SJulian.Pullen@Sun.COM 
24545043Sbaban 	/* Get mapping */
24555043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
24565043Sbaban 		return (rc);
24577369SJulian.Pullen@Sun.COM 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, flag,
24587369SJulian.Pullen@Sun.COM 	    &is_user, &is_wuser, gid, NULL, &direction, NULL);
24595043Sbaban 	(void) idmap_fini(ih);
24605043Sbaban 
24617369SJulian.Pullen@Sun.COM 	if (rc == IDMAP_SUCCESS && (flag & IDMAP_REQ_FLG_USE_CACHE)) {
24627369SJulian.Pullen@Sun.COM 		/* If we have not got the domain don't store GID to winname */
24637369SJulian.Pullen@Sun.COM 		if (domain == NULL)
24647369SJulian.Pullen@Sun.COM 			direction = IDMAP_DIRECTION_W2U;
24657369SJulian.Pullen@Sun.COM 		idmap_cache_add_winname2gid(name, domain, *gid, direction);
24667369SJulian.Pullen@Sun.COM 	}
24677369SJulian.Pullen@Sun.COM 
24685043Sbaban 	return (rc);
24695043Sbaban }
24705043Sbaban 
24715043Sbaban 
24725043Sbaban /*
24735043Sbaban  * Get winname given pid
24745043Sbaban  */
24755043Sbaban static idmap_retcode
24767369SJulian.Pullen@Sun.COM idmap_getwinnamebypid(uid_t pid, int is_user, int flag, char **name,
24777369SJulian.Pullen@Sun.COM 	char **domain)
24785696Snw141292 {
24795043Sbaban 	idmap_handle_t	*ih;
24805043Sbaban 	idmap_retcode	rc;
24815043Sbaban 	int		len;
24825043Sbaban 	char		*winname, *windomain;
24837369SJulian.Pullen@Sun.COM 	int		direction;
24845043Sbaban 
24855043Sbaban 	if (name == NULL)
24865043Sbaban 		return (IDMAP_ERR_ARG);
24875043Sbaban 
24887369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
24897369SJulian.Pullen@Sun.COM 		if (is_user)
24907369SJulian.Pullen@Sun.COM 			rc = idmap_cache_lookup_winnamebyuid(&winname,
24917369SJulian.Pullen@Sun.COM 			    &windomain, pid);
24927369SJulian.Pullen@Sun.COM 		else
24937369SJulian.Pullen@Sun.COM 			rc = idmap_cache_lookup_winnamebygid(&winname,
24947369SJulian.Pullen@Sun.COM 			    &windomain, pid);
24957369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_SUCCESS)
24967369SJulian.Pullen@Sun.COM 			goto out;
24977369SJulian.Pullen@Sun.COM 		if (rc == IDMAP_ERR_MEMORY)
24987369SJulian.Pullen@Sun.COM 			return (rc);
24997369SJulian.Pullen@Sun.COM 	}
25007369SJulian.Pullen@Sun.COM 
25015043Sbaban 	/* Get mapping */
25025043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
25035043Sbaban 		return (rc);
25047369SJulian.Pullen@Sun.COM 	rc = idmap_get_u2w_mapping(ih, &pid, NULL, flag, is_user, NULL,
25057369SJulian.Pullen@Sun.COM 	    NULL, NULL, &winname, &windomain, &direction, NULL);
25065043Sbaban 	(void) idmap_fini(ih);
25075043Sbaban 
25085043Sbaban 	/* Return on error */
25095043Sbaban 	if (rc != IDMAP_SUCCESS)
25105043Sbaban 		return (rc);
25115043Sbaban 
25125043Sbaban 	/*
25135043Sbaban 	 * The given PID may have been mapped to a locally
25145043Sbaban 	 * generated SID in which case there isn't any
25155043Sbaban 	 * Windows name
25165043Sbaban 	 */
25175043Sbaban 	if (winname == NULL || windomain == NULL) {
25185043Sbaban 		idmap_free(winname);
25195043Sbaban 		idmap_free(windomain);
25205043Sbaban 		return (IDMAP_ERR_NORESULT);
25215043Sbaban 	}
25225043Sbaban 
25237369SJulian.Pullen@Sun.COM 	if (flag & IDMAP_REQ_FLG_USE_CACHE) {
25247369SJulian.Pullen@Sun.COM 		if (is_user)
25257369SJulian.Pullen@Sun.COM 			idmap_cache_add_winname2uid(winname, windomain,
25267369SJulian.Pullen@Sun.COM 			    pid, direction);
25277369SJulian.Pullen@Sun.COM 		else
25287369SJulian.Pullen@Sun.COM 			idmap_cache_add_winname2gid(winname, windomain,
25297369SJulian.Pullen@Sun.COM 			    pid, direction);
25307369SJulian.Pullen@Sun.COM 	}
25317369SJulian.Pullen@Sun.COM 
25327369SJulian.Pullen@Sun.COM out:
25335043Sbaban 	if (domain != NULL) {
25345043Sbaban 		*name = winname;
25355043Sbaban 		*domain = windomain;
25365043Sbaban 	} else {
25375043Sbaban 		len = strlen(winname) + strlen(windomain) + 2;
25385043Sbaban 		if ((*name = malloc(len)) != NULL)
25395043Sbaban 			(void) snprintf(*name, len, "%s@%s", winname,
25405043Sbaban 			    windomain);
25415043Sbaban 		else
25425043Sbaban 			rc = IDMAP_ERR_MEMORY;
25435043Sbaban 		idmap_free(winname);
25445043Sbaban 		idmap_free(windomain);
25455043Sbaban 	}
25467369SJulian.Pullen@Sun.COM 
25475043Sbaban 	return (rc);
25485043Sbaban }
25495043Sbaban 
25505043Sbaban 
25515043Sbaban /*
25525043Sbaban  * Get winname given uid
25535043Sbaban  */
25545043Sbaban idmap_stat
25557369SJulian.Pullen@Sun.COM idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
25565696Snw141292 {
25577369SJulian.Pullen@Sun.COM 	return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
25585043Sbaban }
25595043Sbaban 
25605043Sbaban 
25615043Sbaban /*
25625043Sbaban  * Get winname given gid
25635043Sbaban  */
25645043Sbaban idmap_stat
25657369SJulian.Pullen@Sun.COM idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
25665696Snw141292 {
25677369SJulian.Pullen@Sun.COM 	return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
25685043Sbaban }
25696616Sdm199847 
25706616Sdm199847 
25716616Sdm199847 /* printflike */
25726616Sdm199847 void
25736616Sdm199847 idmapdlog(int pri, const char *format, ...) {
25746616Sdm199847 	va_list args;
25756616Sdm199847 
25766616Sdm199847 	va_start(args, format);
25776616Sdm199847 	if (pri <= logstate.max_pri) {
25786616Sdm199847 		(void) vfprintf(stderr, format, args);
25796616Sdm199847 		(void) fprintf(stderr, "\n");
25806616Sdm199847 	}
25816616Sdm199847 
25826616Sdm199847 	/*
25836616Sdm199847 	 * We don't want to fill up the logs with useless messages when
25846616Sdm199847 	 * we're degraded, but we still want to log.
25856616Sdm199847 	 */
25866616Sdm199847 	if (logstate.degraded)
25876616Sdm199847 		pri = LOG_DEBUG;
25886616Sdm199847 
25896616Sdm199847 	if (logstate.write_syslog)
25906616Sdm199847 		(void) vsyslog(pri, format, args);
25916616Sdm199847 	va_end(args);
25926616Sdm199847 }
25936616Sdm199847 
25946616Sdm199847 void
25956616Sdm199847 idmap_log_stderr(int pri)
25966616Sdm199847 {
25976616Sdm199847 	logstate.max_pri = pri;
25986616Sdm199847 }
25996616Sdm199847 
26006616Sdm199847 void
26016616Sdm199847 idmap_log_syslog(bool_t what)
26026616Sdm199847 {
26036616Sdm199847 	logstate.write_syslog = what;
26046616Sdm199847 }
26056616Sdm199847 
26066616Sdm199847 void
26076616Sdm199847 idmap_log_degraded(bool_t what)
26086616Sdm199847 {
26096616Sdm199847 	logstate.degraded = what;
26106616Sdm199847 }
2611