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 /*
22*5968Snw141292  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234520Snw141292  * Use is subject to license terms.
244520Snw141292  */
254520Snw141292 
264520Snw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
274520Snw141292 
284520Snw141292 /*
294520Snw141292  * libidmap API
304520Snw141292  */
314520Snw141292 
324520Snw141292 #include <stdlib.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"
444520Snw141292 
454520Snw141292 static struct timeval TIMEOUT = { 25, 0 };
464520Snw141292 
474520Snw141292 static int idmap_stat2errno(idmap_stat);
485064Sdm199847 static idmap_stat idmap_strdupnull(char **, const char *);
494520Snw141292 
504520Snw141292 #define	__ITER_CREATE(itera, argu, handl, ityp)\
514520Snw141292 	if (handl == NULL) {\
524520Snw141292 		errno = EINVAL;\
534520Snw141292 		return (IDMAP_ERR_ARG);\
544520Snw141292 	}\
554520Snw141292 	itera = calloc(1, sizeof (*itera));\
564520Snw141292 	if (itera == NULL) {\
574520Snw141292 		errno = ENOMEM;\
584520Snw141292 		return (IDMAP_ERR_MEMORY);\
594520Snw141292 	}\
604520Snw141292 	argu = calloc(1, sizeof (*argu));\
614520Snw141292 	if (argu == NULL) {\
624520Snw141292 		free(itera);\
634520Snw141292 		errno = ENOMEM;\
644520Snw141292 		return (IDMAP_ERR_MEMORY);\
654520Snw141292 	}\
664520Snw141292 	itera->ih = handl;\
674520Snw141292 	itera->type = ityp;\
684520Snw141292 	itera->retcode = IDMAP_NEXT;\
694520Snw141292 	itera->limit = 1024;\
704520Snw141292 	itera->arg = argu;
714520Snw141292 
724520Snw141292 
734520Snw141292 #define	__ITER_ERR_RETURN(itera, argu, xdr_argu, iretcod)\
744520Snw141292 	if (argu) {\
754520Snw141292 		xdr_free(xdr_argu, (caddr_t)argu);\
764520Snw141292 		free(argu);\
774520Snw141292 	}\
784520Snw141292 	if (itera)\
794520Snw141292 		free(itera);\
804520Snw141292 	return (iretcod);
814520Snw141292 
824520Snw141292 
834520Snw141292 #define	__ITER_CHECK(itera, ityp)\
844520Snw141292 	if (itera == NULL) {\
854520Snw141292 		errno = EINVAL;\
864520Snw141292 		return (IDMAP_ERR_ARG);\
874520Snw141292 	}\
884520Snw141292 	if (itera->type != ityp) {\
894520Snw141292 		errno = EINVAL;\
904520Snw141292 		return (IDMAP_ERR_ARG);\
914520Snw141292 	}
924520Snw141292 
935064Sdm199847 #define	EMPTY_STRING(str)	(str == NULL || *str == '\0')
944520Snw141292 
954520Snw141292 /*
964520Snw141292  * Free memory allocated by libidmap API
974520Snw141292  *
984520Snw141292  * Input:
994520Snw141292  * ptr - memory to be freed
1004520Snw141292  */
1014520Snw141292 void
1025696Snw141292 idmap_free(void *ptr)
1035696Snw141292 {
1044520Snw141292 	free(ptr);
1054520Snw141292 }
1064520Snw141292 
1074520Snw141292 
1085232Snw141292 #define	MIN_STACK_NEEDS	16384
1095232Snw141292 
1104520Snw141292 /*
1114520Snw141292  * Create and Initialize idmap client handle for rpc/doors
1124520Snw141292  *
1134520Snw141292  * Output:
1144520Snw141292  * handle - idmap handle
1154520Snw141292  */
1164520Snw141292 idmap_stat
1175696Snw141292 idmap_init(idmap_handle_t **handle)
1185696Snw141292 {
1194520Snw141292 	CLIENT			*clnt = NULL;
1204520Snw141292 	struct idmap_handle	*hptr;
1215232Snw141292 	uint_t			sendsz = 0;
1225232Snw141292 	stack_t			st;
1234520Snw141292 
1244520Snw141292 	*handle = NULL;
1254520Snw141292 	hptr = (struct idmap_handle *)calloc(1, sizeof (*hptr));
1264520Snw141292 	if (hptr == NULL)
1274520Snw141292 		return (IDMAP_ERR_MEMORY);
1284520Snw141292 
1295232Snw141292 	/*
1305232Snw141292 	 * clnt_door_call() alloca()s sendsz bytes (twice too, once for
1315232Snw141292 	 * the call args buffer and once for the call result buffer), so
1325232Snw141292 	 * we want to pick a sendsz that will be large enough, but not
1335232Snw141292 	 * too large.
1345232Snw141292 	 */
1355232Snw141292 	if (stack_getbounds(&st) == 0) {
1365232Snw141292 		/*
1375232Snw141292 		 * Estimate how much stack space is left;
1385232Snw141292 		 * st.ss_sp is the top of stack.
1395232Snw141292 		 */
1405232Snw141292 		if ((char *)&sendsz < (char *)st.ss_sp)
1415232Snw141292 			/* stack grows up */
1425232Snw141292 			sendsz = ((char *)st.ss_sp - (char *)&sendsz);
1435232Snw141292 		else
1445232Snw141292 			/* stack grows down */
1455232Snw141292 			sendsz = ((char *)&sendsz - (char *)st.ss_sp);
1465232Snw141292 
1475232Snw141292 		/*
1485232Snw141292 		 * Take much of the stack space left, divided by two,
1495232Snw141292 		 * but leave enough for our needs (just a guess!), and
1505232Snw141292 		 * if we can't, then roll the dice.
1515232Snw141292 		 */
1525232Snw141292 		sendsz = RNDUP(sendsz / 2);
1535232Snw141292 		if (sendsz < MIN_STACK_NEEDS)
1545232Snw141292 			sendsz = 0;	/* RPC call may fail */
1555232Snw141292 		else if (sendsz > IDMAP_MAX_DOOR_RPC)
1565232Snw141292 			sendsz = IDMAP_MAX_DOOR_RPC;
1575232Snw141292 	}
1585232Snw141292 
1595232Snw141292 	clnt = clnt_door_create(IDMAP_PROG, IDMAP_V1, sendsz);
1604520Snw141292 	if (clnt == NULL) {
1614520Snw141292 		free(hptr);
1624520Snw141292 		return (IDMAP_ERR_RPC);
1634520Snw141292 	}
1644520Snw141292 	hptr->type = _IDMAP_HANDLE_RPC_DOORS;
1654520Snw141292 	hptr->privhandle = clnt;
1664520Snw141292 	*handle = hptr;
1674520Snw141292 	return (IDMAP_SUCCESS);
1684520Snw141292 }
1694520Snw141292 
1704520Snw141292 
1714520Snw141292 /*
1724520Snw141292  * Finalize idmap handle
1734520Snw141292  *
1744520Snw141292  * Input:
1754520Snw141292  * handle - idmap handle
1764520Snw141292  */
1774520Snw141292 idmap_stat
1785696Snw141292 idmap_fini(idmap_handle_t *handle)
1795696Snw141292 {
1804520Snw141292 	CLIENT			*clnt;
1814520Snw141292 	struct idmap_handle	*hptr;
1824520Snw141292 
1834520Snw141292 	if (handle == NULL)
1844520Snw141292 		return (IDMAP_SUCCESS);
1854520Snw141292 
1864520Snw141292 	hptr = (struct idmap_handle *)handle;
1874520Snw141292 
1884520Snw141292 	switch (hptr->type) {
1894520Snw141292 	case _IDMAP_HANDLE_RPC_DOORS:
1904520Snw141292 		clnt = (CLIENT *)hptr->privhandle;
1914520Snw141292 		if (clnt) {
1924520Snw141292 			if (clnt->cl_auth)
1934520Snw141292 				auth_destroy(clnt->cl_auth);
1944520Snw141292 			clnt_destroy(clnt);
1954520Snw141292 		}
1964520Snw141292 		break;
1974520Snw141292 	default:
1984520Snw141292 		break;
1994520Snw141292 	}
2004520Snw141292 	free(hptr);
2014520Snw141292 	return (IDMAP_SUCCESS);
2024520Snw141292 }
2034520Snw141292 
2044520Snw141292 
2054520Snw141292 
2064520Snw141292 /*
2074520Snw141292  * Create/Initialize handle for updates
2084520Snw141292  *
2094520Snw141292  * Output:
2104520Snw141292  * udthandle - update handle
2114520Snw141292  */
2124520Snw141292 idmap_stat
2135696Snw141292 idmap_udt_create(idmap_handle_t *handle, idmap_udt_handle_t **udthandle)
2145696Snw141292 {
2154520Snw141292 	idmap_udt_handle_t	*tmp;
2164520Snw141292 
2174520Snw141292 	if (handle == NULL || udthandle == NULL) {
2184520Snw141292 		errno = EINVAL;
2194520Snw141292 		return (IDMAP_ERR_ARG);
2204520Snw141292 	}
2214520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
2224520Snw141292 		errno = ENOMEM;
2234520Snw141292 		return (IDMAP_ERR_MEMORY);
2244520Snw141292 	}
2254520Snw141292 
2264520Snw141292 	tmp->ih = handle;
2274520Snw141292 	*udthandle = tmp;
2284520Snw141292 	return (IDMAP_SUCCESS);
2294520Snw141292 }
2304520Snw141292 
2314520Snw141292 
2324520Snw141292 /*
2334520Snw141292  * All the updates specified by the update handle are committed
2344520Snw141292  * in a single transaction. i.e either all succeed or none.
2354520Snw141292  *
2364520Snw141292  * Input:
2374520Snw141292  * udthandle - update handle with the update requests
2384520Snw141292  *
2394520Snw141292  * Return value:
2404520Snw141292  * Status of the commit
2414520Snw141292  */
2424520Snw141292 idmap_stat
2435696Snw141292 idmap_udt_commit(idmap_udt_handle_t *udthandle)
2445696Snw141292 {
2454520Snw141292 	CLIENT			*clnt;
2464520Snw141292 	enum clnt_stat		clntstat;
2475064Sdm199847 	idmap_update_res	res;
2485064Sdm199847 	idmap_stat		retcode;
2494520Snw141292 
2504520Snw141292 	if (udthandle == NULL) {
2514520Snw141292 		errno = EINVAL;
2524520Snw141292 		return (IDMAP_ERR_ARG);
2534520Snw141292 	}
2545064Sdm199847 
2555064Sdm199847 	(void) memset(&res, 0, sizeof (res));
2565064Sdm199847 
2574520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(udthandle->ih, clnt);
2584520Snw141292 	clntstat = clnt_call(clnt, IDMAP_UPDATE,
2595696Snw141292 	    (xdrproc_t)xdr_idmap_update_batch, (caddr_t)&udthandle->batch,
2605696Snw141292 	    (xdrproc_t)xdr_idmap_update_res, (caddr_t)&res,
2615696Snw141292 	    TIMEOUT);
2624644Sbaban 
2635064Sdm199847 	if (clntstat != RPC_SUCCESS) {
2645064Sdm199847 		retcode = _idmap_rpc2stat(clnt);
2655064Sdm199847 		goto out;
2665064Sdm199847 	}
2675064Sdm199847 
2685064Sdm199847 	retcode = udthandle->commit_stat = res.retcode;
2695064Sdm199847 	udthandle->error_index = res.error_index;
2705064Sdm199847 
2715064Sdm199847 	if (retcode != IDMAP_SUCCESS) {
2725064Sdm199847 
2735064Sdm199847 		if (udthandle->error_index < 0)
2745064Sdm199847 			goto out;
2755064Sdm199847 
2765064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->error_rule,
2775064Sdm199847 		    &res.error_rule);
2785064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
2795064Sdm199847 			udthandle->error_index = -2;
2805064Sdm199847 			goto out;
2815064Sdm199847 		}
2825064Sdm199847 
2835064Sdm199847 		retcode = idmap_namerule_cpy(&udthandle->conflict_rule,
2845064Sdm199847 		    &res.conflict_rule);
2855064Sdm199847 		if (retcode != IDMAP_SUCCESS) {
2865064Sdm199847 			udthandle->error_index = -2;
2875064Sdm199847 			goto out;
2885064Sdm199847 		}
2895064Sdm199847 	}
2905064Sdm199847 
2915064Sdm199847 	retcode = res.retcode;
2925064Sdm199847 
2935064Sdm199847 
2945064Sdm199847 out:
2954644Sbaban 	/* reset handle so that it can be used again */
2965064Sdm199847 	if (retcode == IDMAP_SUCCESS) {
2975064Sdm199847 		_IDMAP_RESET_UDT_HANDLE(udthandle);
2985064Sdm199847 	}
2995064Sdm199847 
3005064Sdm199847 	(void) xdr_free(xdr_idmap_update_res, (caddr_t)&res);
3015064Sdm199847 	errno = idmap_stat2errno(retcode);
3025064Sdm199847 	return (retcode);
3035064Sdm199847 }
3045064Sdm199847 
3055064Sdm199847 
3065064Sdm199847 static void
3075064Sdm199847 idmap_namerule_parts_clear(char **windomain, char **winname,
3085696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
3095696Snw141292     boolean_t *is_nt4, int *direction)
3105696Snw141292 {
3115064Sdm199847 	if (windomain)
3125064Sdm199847 		*windomain = NULL;
3135064Sdm199847 	if (winname)
3145064Sdm199847 		*winname = NULL;
3155064Sdm199847 	if (unixname)
3165064Sdm199847 		*unixname = NULL;
3175064Sdm199847 
3185064Sdm199847 	if (is_nt4)
3195064Sdm199847 		*is_nt4 = 0;
3205064Sdm199847 	if (is_user)
3215064Sdm199847 		*is_user = -1;
3225696Snw141292 	if (is_wuser)
3235696Snw141292 		*is_wuser = -1;
3245064Sdm199847 	if (direction)
3255064Sdm199847 		*direction = IDMAP_DIRECTION_UNDEF;
3265064Sdm199847 }
3275064Sdm199847 
3285064Sdm199847 static idmap_stat
3295696Snw141292 idmap_namerule2parts(idmap_namerule *rule,
3305064Sdm199847     char **windomain, char **winname,
3315696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
3325696Snw141292     boolean_t *is_nt4, int *direction)
3335696Snw141292 {
3345064Sdm199847 	idmap_stat retcode;
3355064Sdm199847 
3365064Sdm199847 	if (EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
3375064Sdm199847 		return (IDMAP_ERR_NORESULT);
3385064Sdm199847 
3395064Sdm199847 
3405064Sdm199847 	retcode = idmap_strdupnull(windomain, rule->windomain);
3415064Sdm199847 	if (retcode != IDMAP_SUCCESS)
3425064Sdm199847 		goto errout;
3435064Sdm199847 
3445064Sdm199847 	retcode = idmap_strdupnull(winname, rule->winname);
3455064Sdm199847 	if (retcode != IDMAP_SUCCESS)
3465064Sdm199847 		goto errout;
3475064Sdm199847 
3485064Sdm199847 	retcode = idmap_strdupnull(unixname, rule->unixname);
3495064Sdm199847 	if (retcode != IDMAP_SUCCESS)
3505064Sdm199847 		goto errout;
3515064Sdm199847 
3525064Sdm199847 
3535064Sdm199847 	if (is_user)
3545064Sdm199847 		*is_user = rule->is_user;
3555696Snw141292 	if (is_wuser)
3565696Snw141292 		*is_wuser = rule->is_wuser;
3575064Sdm199847 	if (is_nt4)
3585064Sdm199847 		*is_nt4 = rule->is_nt4;
3595064Sdm199847 	if (direction)
3605064Sdm199847 		*direction = rule->direction;
3615064Sdm199847 
3625064Sdm199847 
3635064Sdm199847 	return (IDMAP_SUCCESS);
3644644Sbaban 
3655064Sdm199847 errout:
3665064Sdm199847 	if (windomain && *windomain)
3675064Sdm199847 		free(*windomain);
3685064Sdm199847 	if (winname && *winname)
3695064Sdm199847 		free(*winname);
3705064Sdm199847 	if (unixname && *unixname)
3715064Sdm199847 		free(*unixname);
3725064Sdm199847 
3735064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
3745696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
3755064Sdm199847 
3764520Snw141292 	return (retcode);
3775064Sdm199847 
3785064Sdm199847 }
3795064Sdm199847 
3805064Sdm199847 /*
3815064Sdm199847  * Retrieve the index of the failed batch element. error_index == -1
3825064Sdm199847  * indicates failure at the beginning, -2 at the end.
3835064Sdm199847  *
3845064Sdm199847  * If idmap_udt_commit didn't return error, the returned value is undefined.
3855064Sdm199847  *
3865064Sdm199847  * Return value:
3875064Sdm199847  * IDMAP_SUCCESS
3885064Sdm199847  */
3895064Sdm199847 
3905064Sdm199847 idmap_stat
3915064Sdm199847 idmap_udt_get_error_index(idmap_udt_handle_t *udthandle,
3925696Snw141292     int64_t *error_index)
3935696Snw141292 {
3945064Sdm199847 	if (error_index)
3955064Sdm199847 		*error_index = udthandle->error_index;
3965064Sdm199847 
3975064Sdm199847 	return (IDMAP_SUCCESS);
3985064Sdm199847 }
3995064Sdm199847 
4005064Sdm199847 
4015064Sdm199847 /*
4025064Sdm199847  * Retrieve the rule which caused the batch to fail. If
4035064Sdm199847  * idmap_udt_commit didn't return error or if error_index is < 0, the
4045064Sdm199847  * retrieved rule is undefined.
4055064Sdm199847  *
4065064Sdm199847  * Return value:
4075064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
4085064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
4095064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
4105064Sdm199847  */
4115064Sdm199847 
4125064Sdm199847 idmap_stat
4135064Sdm199847 idmap_udt_get_error_rule(idmap_udt_handle_t *udthandle,
4145064Sdm199847     char **windomain, char **winname,
4155696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4165696Snw141292     boolean_t *is_nt4, int *direction)
4175696Snw141292 {
4185064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
4195696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
4205064Sdm199847 
4215064Sdm199847 	if (udthandle->commit_stat == IDMAP_SUCCESS ||
4225064Sdm199847 	    udthandle->error_index < 0)
4235064Sdm199847 		return (IDMAP_ERR_NORESULT);
4245064Sdm199847 
4255064Sdm199847 	return (idmap_namerule2parts(
4265696Snw141292 	    &udthandle->error_rule,
4275696Snw141292 	    windomain,
4285696Snw141292 	    winname,
4295696Snw141292 	    unixname,
4305696Snw141292 	    is_user,
4315696Snw141292 	    is_wuser,
4325696Snw141292 	    is_nt4,
4335696Snw141292 	    direction));
4345064Sdm199847 }
4355064Sdm199847 
4365064Sdm199847 /*
4375064Sdm199847  * Retrieve the rule with which there was a conflict. TODO: retrieve
4385064Sdm199847  * the value.
4395064Sdm199847  *
4405064Sdm199847  * Return value:
4415064Sdm199847  * IDMAP_ERR_NORESULT if there is no error rule.
4425064Sdm199847  * IDMAP_SUCCESS if the rule was obtained OK.
4435064Sdm199847  * other error code (IDMAP_ERR_NOMEMORY etc)
4445064Sdm199847  */
4455064Sdm199847 
4465064Sdm199847 idmap_stat
4475064Sdm199847 idmap_udt_get_conflict_rule(idmap_udt_handle_t *udthandle,
4485064Sdm199847     char **windomain, char **winname,
4495696Snw141292     char **unixname, boolean_t *is_user, boolean_t *is_wuser,
4505696Snw141292     boolean_t *is_nt4, int *direction)
4515696Snw141292 {
4525064Sdm199847 	idmap_namerule_parts_clear(windomain, winname,
4535696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
4545064Sdm199847 
4555064Sdm199847 	if (udthandle->commit_stat != IDMAP_ERR_W2U_NAMERULE_CONFLICT &&
4565064Sdm199847 	    udthandle->commit_stat != IDMAP_ERR_U2W_NAMERULE_CONFLICT) {
4575696Snw141292 		return (IDMAP_ERR_NORESULT);
4585064Sdm199847 	}
4595064Sdm199847 
4605064Sdm199847 	return (idmap_namerule2parts(
4615696Snw141292 	    &udthandle->conflict_rule,
4625696Snw141292 	    windomain,
4635696Snw141292 	    winname,
4645696Snw141292 	    unixname,
4655696Snw141292 	    is_user,
4665696Snw141292 	    is_wuser,
4675696Snw141292 	    is_nt4,
4685696Snw141292 	    direction));
4694520Snw141292 }
4704520Snw141292 
4714520Snw141292 
4724520Snw141292 /*
4734520Snw141292  * Destroy the update handle
4744520Snw141292  */
4754520Snw141292 void
4765696Snw141292 idmap_udt_destroy(idmap_udt_handle_t *udthandle)
4775696Snw141292 {
4784520Snw141292 	if (udthandle == NULL)
4794520Snw141292 		return;
4804520Snw141292 	(void) xdr_free(xdr_idmap_update_batch, (caddr_t)&udthandle->batch);
4815064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->error_rule);
4825064Sdm199847 	(void) xdr_free(xdr_idmap_namerule, (caddr_t)&udthandle->conflict_rule);
4834520Snw141292 	free(udthandle);
4844520Snw141292 }
4854520Snw141292 
4864520Snw141292 
4874520Snw141292 idmap_stat
4884520Snw141292 idmap_udt_add_namerule(idmap_udt_handle_t *udthandle, const char *windomain,
4895696Snw141292     boolean_t is_user, boolean_t is_wuser, const char *winname,
4905696Snw141292     const char *unixname, boolean_t is_nt4, int direction)
4915696Snw141292 {
4924520Snw141292 	idmap_retcode	retcode;
4934644Sbaban 	idmap_namerule	*rule = NULL;
4944520Snw141292 
4954644Sbaban 	retcode = _udt_extend_batch(udthandle);
4964520Snw141292 	if (retcode != IDMAP_SUCCESS)
4974520Snw141292 		goto errout;
4984520Snw141292 
4994520Snw141292 	rule = &udthandle->batch.
5005696Snw141292 	    idmap_update_batch_val[udthandle->next].
5015696Snw141292 	    idmap_update_op_u.rule;
5024520Snw141292 	rule->is_user = is_user;
5035696Snw141292 	rule->is_wuser = is_wuser;
5044520Snw141292 	rule->direction = direction;
5054520Snw141292 	rule->is_nt4 = is_nt4;
5065064Sdm199847 
5075064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
5085064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5095064Sdm199847 		goto errout;
5105064Sdm199847 
5115064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
5125064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5135064Sdm199847 		goto errout;
5145064Sdm199847 
5155064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
5165064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5175064Sdm199847 		goto errout;
5184644Sbaban 
5194644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
5204644Sbaban 	    OP_ADD_NAMERULE;
5214520Snw141292 	udthandle->next++;
5224520Snw141292 	return (IDMAP_SUCCESS);
5234520Snw141292 
5244520Snw141292 errout:
5254644Sbaban 	/* The batch should still be usable */
5264644Sbaban 	if (rule)
5274644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
5284520Snw141292 	errno = idmap_stat2errno(retcode);
5294520Snw141292 	return (retcode);
5304520Snw141292 }
5314520Snw141292 
5324520Snw141292 
5334520Snw141292 /* ARGSUSED */
5344520Snw141292 idmap_stat
5354520Snw141292 idmap_udt_rm_namerule(idmap_udt_handle_t *udthandle, boolean_t is_user,
5365696Snw141292     boolean_t is_wuser,	const char *windomain, const char *winname,
5375696Snw141292     const char *unixname, int direction)
5385696Snw141292 {
5394520Snw141292 	idmap_retcode	retcode;
5404644Sbaban 	idmap_namerule	*rule = NULL;
5414520Snw141292 
5424644Sbaban 	retcode = _udt_extend_batch(udthandle);
5434520Snw141292 	if (retcode != IDMAP_SUCCESS)
5444520Snw141292 		goto errout;
5454520Snw141292 
5464520Snw141292 	rule = &udthandle->batch.
5475696Snw141292 	    idmap_update_batch_val[udthandle->next].
5485696Snw141292 	    idmap_update_op_u.rule;
5494520Snw141292 	rule->is_user = is_user;
5505696Snw141292 	rule->is_wuser = is_wuser;
5514520Snw141292 	rule->direction = direction;
5525064Sdm199847 
5535064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
5545064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5555064Sdm199847 		goto errout;
5565064Sdm199847 
5575064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
5585064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5595064Sdm199847 		goto errout;
5605064Sdm199847 
5615064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
5625064Sdm199847 	if (retcode != IDMAP_SUCCESS)
5635064Sdm199847 		goto errout;
5645064Sdm199847 
5654644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
5664644Sbaban 	    OP_RM_NAMERULE;
5674520Snw141292 	udthandle->next++;
5684520Snw141292 	return (IDMAP_SUCCESS);
5694520Snw141292 
5704520Snw141292 errout:
5714644Sbaban 	if (rule)
5724644Sbaban 		(void) xdr_free(xdr_idmap_namerule, (caddr_t)rule);
5734520Snw141292 	errno = idmap_stat2errno(retcode);
5744520Snw141292 	return (retcode);
5754520Snw141292 }
5764520Snw141292 
5774520Snw141292 
5784520Snw141292 /* ARGSUSED */
5794520Snw141292 idmap_stat
5805696Snw141292 idmap_udt_flush_namerules(idmap_udt_handle_t *udthandle)
5815696Snw141292 {
5824520Snw141292 	idmap_retcode	retcode;
5834520Snw141292 
5844644Sbaban 	retcode = _udt_extend_batch(udthandle);
5854520Snw141292 	if (retcode != IDMAP_SUCCESS)
5864520Snw141292 		goto errout;
5874520Snw141292 
5884644Sbaban 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
5894644Sbaban 	    OP_FLUSH_NAMERULES;
5904520Snw141292 	udthandle->next++;
5914520Snw141292 	return (IDMAP_SUCCESS);
5924520Snw141292 
5934520Snw141292 errout:
5944520Snw141292 	errno = idmap_stat2errno(retcode);
5954520Snw141292 	return (retcode);
5964520Snw141292 }
5974520Snw141292 
5984520Snw141292 
5994520Snw141292 /*
6004520Snw141292  * Set the number of entries requested per batch by the iterator
6014520Snw141292  *
6024520Snw141292  * Input:
6034520Snw141292  * iter  - iterator
6044520Snw141292  * limit - number of entries requested per batch
6054520Snw141292  */
6064520Snw141292 idmap_stat
6075696Snw141292 idmap_iter_set_limit(idmap_iter_t *iter, uint64_t limit)
6085696Snw141292 {
6094520Snw141292 	if (iter == NULL) {
6104520Snw141292 		errno = EINVAL;
6114520Snw141292 		return (IDMAP_ERR_ARG);
6124520Snw141292 	}
6134520Snw141292 	iter->limit = limit;
6144520Snw141292 	return (IDMAP_SUCCESS);
6154520Snw141292 }
6164520Snw141292 
6174520Snw141292 
6184520Snw141292 /*
6194520Snw141292  * Create iterator to get name-based mapping rules
6204520Snw141292  *
6214520Snw141292  * Input:
6224520Snw141292  * windomain - Windows domain
6234520Snw141292  * is_user   - user or group rules
6244520Snw141292  * winname   - Windows user or group name
6254520Snw141292  * unixname  - Unix user or group name
6264520Snw141292  *
6274520Snw141292  * Output:
6284520Snw141292  * iter - iterator
6294520Snw141292  */
6304520Snw141292 idmap_stat
6314520Snw141292 idmap_iter_namerules(idmap_handle_t *handle, const char *windomain,
6325696Snw141292 		boolean_t is_user, boolean_t is_wuser, const char *winname,
6335696Snw141292 		const char *unixname, idmap_iter_t **iter)
6345696Snw141292 {
6354520Snw141292 
6364520Snw141292 	idmap_iter_t			*tmpiter;
6374520Snw141292 	idmap_list_namerules_1_argument	*arg = NULL;
6384520Snw141292 	idmap_namerule			*rule;
6394520Snw141292 	idmap_retcode			retcode;
6404520Snw141292 
6414520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_NAMERULES);
6424520Snw141292 
6434520Snw141292 	rule = &arg->rule;
6444520Snw141292 	rule->is_user = is_user;
6455696Snw141292 	rule->is_wuser = is_wuser;
6464644Sbaban 	rule->direction = IDMAP_DIRECTION_UNDEF;
6475064Sdm199847 
6485064Sdm199847 	retcode = idmap_strdupnull(&rule->windomain, windomain);
6495064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6505064Sdm199847 		goto errout;
6515064Sdm199847 
6525064Sdm199847 	retcode = idmap_strdupnull(&rule->winname, winname);
6535064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6545064Sdm199847 		goto errout;
6555064Sdm199847 
6565064Sdm199847 	retcode = idmap_strdupnull(&rule->unixname, unixname);
6575064Sdm199847 	if (retcode != IDMAP_SUCCESS)
6585064Sdm199847 		goto errout;
6594520Snw141292 
6604520Snw141292 	*iter = tmpiter;
6614520Snw141292 	return (IDMAP_SUCCESS);
6624520Snw141292 
6634520Snw141292 errout:
6644520Snw141292 	__ITER_ERR_RETURN(tmpiter, arg,
6655696Snw141292 	    xdr_idmap_list_namerules_1_argument, retcode);
6664520Snw141292 }
6674520Snw141292 
6684520Snw141292 
6694520Snw141292 /*
6704520Snw141292  * Iterate through the name-based mapping rules
6714520Snw141292  *
6724520Snw141292  * Input:
6734520Snw141292  * iter - iterator
6744520Snw141292  *
6754520Snw141292  * Output:
6764520Snw141292  * windomain - Windows domain
6774520Snw141292  * winname   - Windows user or group name
6784520Snw141292  * unixname  - Unix user or group name
6794520Snw141292  * is_nt4    - NT4 or AD
6804520Snw141292  * direction - bi(0), win2unix(1), unix2win(2)
6814520Snw141292  *
6824520Snw141292  * Return value:
6834520Snw141292  * 0   - done
6844520Snw141292  * 1   - more results available
6854520Snw141292  * < 0 - error
6864520Snw141292  */
6874520Snw141292 idmap_stat
6884520Snw141292 idmap_iter_next_namerule(idmap_iter_t *iter, char **windomain,
6895696Snw141292     char **winname, char **unixname,  boolean_t *is_user,
6905696Snw141292     boolean_t *is_wuser, boolean_t *is_nt4, int *direction)
6915696Snw141292 {
6924520Snw141292 	idmap_namerules_res		*namerules;
6934520Snw141292 	idmap_list_namerules_1_argument	*arg;
6944520Snw141292 	idmap_retcode			retcode;
6954520Snw141292 
6965696Snw141292 	idmap_namerule_parts_clear(windomain, winname,
6975696Snw141292 	    unixname, is_user, is_wuser, is_nt4, direction);
6985696Snw141292 
6994520Snw141292 
7004520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_NAMERULES);
7014520Snw141292 
7024520Snw141292 	namerules = (idmap_namerules_res *)iter->retlist;
7034520Snw141292 	if (iter->retcode == IDMAP_NEXT && (namerules == NULL ||
7045696Snw141292 	    iter->next >= namerules->rules.rules_len)) {
7054520Snw141292 
7064520Snw141292 		if ((arg = iter->arg) == NULL) {
7074520Snw141292 			errno = EINVAL;
7084520Snw141292 			return (IDMAP_ERR_ARG);
7094520Snw141292 		}
7104520Snw141292 		arg->limit = iter->limit;
7114520Snw141292 
7124520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_NAMERULES,
7135696Snw141292 		    iter, arg,
7145696Snw141292 		    (uchar_t **)&namerules, sizeof (*namerules),
7155696Snw141292 		    (xdrproc_t)xdr_idmap_list_namerules_1_argument,
7165696Snw141292 		    (xdrproc_t)xdr_idmap_namerules_res);
7174520Snw141292 		if (retcode != IDMAP_SUCCESS)
7184520Snw141292 			return (retcode);
7194520Snw141292 
7204520Snw141292 		if (IDMAP_ERROR(namerules->retcode)) {
7214520Snw141292 			retcode  = namerules->retcode;
7224520Snw141292 			xdr_free(xdr_idmap_namerules_res, (caddr_t)namerules);
7234520Snw141292 			free(namerules);
7244520Snw141292 			iter->retlist = NULL;
7254520Snw141292 			return (retcode);
7264520Snw141292 		}
7274520Snw141292 		iter->retcode = namerules->retcode;
7284520Snw141292 		arg->lastrowid = namerules->lastrowid;
7294520Snw141292 	}
7304520Snw141292 
7314520Snw141292 	if (namerules == NULL || namerules->rules.rules_len == 0)
7324520Snw141292 		return (IDMAP_SUCCESS);
7334520Snw141292 
7344520Snw141292 	if (iter->next >= namerules->rules.rules_len) {
7354520Snw141292 		return (IDMAP_ERR_ARG);
7364520Snw141292 	}
7374520Snw141292 
7385064Sdm199847 	retcode = idmap_strdupnull(windomain,
7395064Sdm199847 	    namerules->rules.rules_val[iter->next].windomain);
7405064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7415064Sdm199847 		goto errout;
7425064Sdm199847 
7435064Sdm199847 	retcode = idmap_strdupnull(winname,
7445064Sdm199847 	    namerules->rules.rules_val[iter->next].winname);
7455064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7465064Sdm199847 		goto errout;
7475064Sdm199847 
7485064Sdm199847 	retcode = idmap_strdupnull(unixname,
7495064Sdm199847 	    namerules->rules.rules_val[iter->next].unixname);
7505064Sdm199847 	if (retcode != IDMAP_SUCCESS)
7515064Sdm199847 		goto errout;
7525064Sdm199847 
7534520Snw141292 	if (is_nt4)
7544520Snw141292 		*is_nt4 = namerules->rules.rules_val[iter->next].is_nt4;
7555696Snw141292 	if (is_user)
7565696Snw141292 		*is_user = namerules->rules.rules_val[iter->next].is_user;
7575696Snw141292 	if (is_wuser)
7585696Snw141292 		*is_wuser = namerules->rules.rules_val[iter->next].is_wuser;
7594520Snw141292 	if (direction)
7604520Snw141292 		*direction = namerules->rules.rules_val[iter->next].direction;
7614520Snw141292 	iter->next++;
7624520Snw141292 
7634520Snw141292 	if (iter->next == namerules->rules.rules_len)
7644520Snw141292 		return (iter->retcode);
7654520Snw141292 	else
7664520Snw141292 		return (IDMAP_NEXT);
7674520Snw141292 
7684520Snw141292 errout:
7694520Snw141292 	if (windomain && *windomain)
7704520Snw141292 		free(*windomain);
7714520Snw141292 	if (winname && *winname)
7724520Snw141292 		free(*winname);
7734520Snw141292 	if (unixname && *unixname)
7744520Snw141292 		free(*unixname);
7754520Snw141292 	return (retcode);
7764520Snw141292 }
7774520Snw141292 
7784520Snw141292 
7794520Snw141292 /*
7804520Snw141292  * Create iterator to get SID to UID/GID mappings
7814520Snw141292  *
7824520Snw141292  * Output:
7834520Snw141292  * iter - iterator
7844520Snw141292  */
7854520Snw141292 idmap_stat
7865696Snw141292 idmap_iter_mappings(idmap_handle_t *handle, idmap_iter_t **iter)
7875696Snw141292 {
7884520Snw141292 	idmap_iter_t			*tmpiter;
7894520Snw141292 	idmap_list_mappings_1_argument	*arg = NULL;
7904520Snw141292 
7914520Snw141292 	__ITER_CREATE(tmpiter, arg, handle, IDMAP_LIST_MAPPINGS);
7924520Snw141292 
7934520Snw141292 	*iter = tmpiter;
7944520Snw141292 	return (IDMAP_SUCCESS);
7954520Snw141292 }
7964520Snw141292 
7974520Snw141292 
7984520Snw141292 /*
7994520Snw141292  * Iterate through the SID to UID/GID mappings
8004520Snw141292  *
8014520Snw141292  * Input:
8024520Snw141292  * iter - iterator
8034520Snw141292  *
8044520Snw141292  * Output:
8054520Snw141292  * sid - SID in canonical form
8064520Snw141292  * pid - UID or GID
8074520Snw141292  *
8084520Snw141292  * Return value:
8094520Snw141292  * 0   - done
8104520Snw141292  * 1   - more results available
8114520Snw141292  * < 0 - error
8124520Snw141292  */
8134520Snw141292 idmap_stat
8144520Snw141292 idmap_iter_next_mapping(idmap_iter_t *iter, char **sidprefix,
8155696Snw141292     idmap_rid_t *rid, uid_t *pid, char **winname,
8165696Snw141292     char **windomain, char **unixname, boolean_t *is_user,
8175696Snw141292     boolean_t *is_wuser, int *direction)
8185696Snw141292 {
8194520Snw141292 	idmap_mappings_res		*mappings;
8204520Snw141292 	idmap_list_mappings_1_argument	*arg;
8214520Snw141292 	idmap_retcode			retcode;
8224520Snw141292 	char				*str;
8234520Snw141292 
8244520Snw141292 	if (sidprefix)
8254520Snw141292 		*sidprefix = NULL;
8264520Snw141292 	if (rid)
8274520Snw141292 		*rid = UINT32_MAX;
8284520Snw141292 	if (winname)
8294520Snw141292 		*winname = NULL;
8304520Snw141292 	if (windomain)
8314520Snw141292 		*windomain = NULL;
8324520Snw141292 	if (unixname)
8334520Snw141292 		*unixname = NULL;
8344520Snw141292 	if (pid)
8354520Snw141292 		*pid = UINT32_MAX;
8365696Snw141292 	if (is_user)
8375696Snw141292 		*is_user = -1;
8385696Snw141292 	if (is_wuser)
8395696Snw141292 		*is_wuser = -1;
8404520Snw141292 	if (direction)
8414644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
8424520Snw141292 
8434520Snw141292 	__ITER_CHECK(iter, IDMAP_LIST_MAPPINGS);
8444520Snw141292 
8454520Snw141292 	mappings = (idmap_mappings_res *)iter->retlist;
8464520Snw141292 	if (iter->retcode == IDMAP_NEXT && (mappings == NULL ||
8475696Snw141292 	    iter->next >= mappings->mappings.mappings_len)) {
8484520Snw141292 
8494520Snw141292 		if ((arg = iter->arg) == NULL) {
8504520Snw141292 			errno = EINVAL;
8514520Snw141292 			return (IDMAP_ERR_ARG);
8524520Snw141292 		}
8534520Snw141292 		arg->limit = iter->limit;
8544520Snw141292 
8554520Snw141292 		retcode = _iter_get_next_list(IDMAP_LIST_MAPPINGS,
8565696Snw141292 		    iter, arg,
8575696Snw141292 		    (uchar_t **)&mappings, sizeof (*mappings),
8585696Snw141292 		    (xdrproc_t)xdr_idmap_list_mappings_1_argument,
8595696Snw141292 		    (xdrproc_t)xdr_idmap_mappings_res);
8604520Snw141292 		if (retcode != IDMAP_SUCCESS)
8614520Snw141292 			return (retcode);
8624520Snw141292 
8634520Snw141292 		if (IDMAP_ERROR(mappings->retcode)) {
8644520Snw141292 			retcode  = mappings->retcode;
8654520Snw141292 			xdr_free(xdr_idmap_mappings_res, (caddr_t)mappings);
8664520Snw141292 			free(mappings);
8674520Snw141292 			iter->retlist = NULL;
8684520Snw141292 			return (retcode);
8694520Snw141292 		}
8704520Snw141292 		iter->retcode = mappings->retcode;
8714520Snw141292 		arg->lastrowid = mappings->lastrowid;
8724520Snw141292 	}
8734520Snw141292 
8744520Snw141292 	if (mappings == NULL || mappings->mappings.mappings_len == 0)
8754520Snw141292 		return (IDMAP_SUCCESS);
8764520Snw141292 
8774520Snw141292 	if (iter->next >= mappings->mappings.mappings_len) {
8784520Snw141292 		return (IDMAP_ERR_ARG);
8794520Snw141292 	}
8804520Snw141292 
8814520Snw141292 	if (sidprefix) {
8824520Snw141292 		str = mappings->mappings.mappings_val[iter->next].id1.
8835696Snw141292 		    idmap_id_u.sid.prefix;
8844695Sbaban 		if (str && *str != '\0') {
8854520Snw141292 			*sidprefix = strdup(str);
8864526Sbaban 			if (*sidprefix == NULL) {
8874526Sbaban 				retcode = IDMAP_ERR_MEMORY;
8884526Sbaban 				goto errout;
8894526Sbaban 			}
8904520Snw141292 		}
8914520Snw141292 	}
8924520Snw141292 	if (rid)
8934520Snw141292 		*rid = mappings->mappings.mappings_val[iter->next].id1.
8945696Snw141292 		    idmap_id_u.sid.rid;
8955064Sdm199847 
8965064Sdm199847 	retcode = idmap_strdupnull(windomain,
8975064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1domain);
8985064Sdm199847 	if (retcode != IDMAP_SUCCESS)
8995064Sdm199847 		goto errout;
9005064Sdm199847 
9015064Sdm199847 	retcode = idmap_strdupnull(winname,
9025064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id1name);
9035064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9045064Sdm199847 		goto errout;
9055064Sdm199847 
9065064Sdm199847 	retcode = idmap_strdupnull(unixname,
9075064Sdm199847 	    mappings->mappings.mappings_val[iter->next].id2name);
9085064Sdm199847 	if (retcode != IDMAP_SUCCESS)
9095064Sdm199847 		goto errout;
9105064Sdm199847 
9115064Sdm199847 
9124520Snw141292 	if (pid)
9134520Snw141292 		*pid = mappings->mappings.mappings_val[iter->next].id2.
9145696Snw141292 		    idmap_id_u.uid;
9154520Snw141292 	if (direction)
9164520Snw141292 		*direction = mappings->mappings.mappings_val[iter->next].
9175696Snw141292 		    direction;
9185696Snw141292 	if (is_user)
9195696Snw141292 		*is_user = (mappings->mappings.mappings_val[iter->next].id2
9205696Snw141292 		    .idtype == IDMAP_UID)?1:0;
9215696Snw141292 	if (is_wuser)
9225696Snw141292 		*is_wuser = (mappings->mappings.mappings_val[iter->next].id1
9235696Snw141292 		    .idtype == IDMAP_USID)?1:0;
9245696Snw141292 
9254520Snw141292 	iter->next++;
9264520Snw141292 
9274520Snw141292 	if (iter->next == mappings->mappings.mappings_len)
9284520Snw141292 		return (iter->retcode);
9294520Snw141292 	else
9304520Snw141292 		return (IDMAP_NEXT);
9314520Snw141292 
9324520Snw141292 errout:
9334520Snw141292 	if (sidprefix && *sidprefix)
9344520Snw141292 		free(*sidprefix);
9354520Snw141292 	if (winname && *winname)
9364520Snw141292 		free(*winname);
9374520Snw141292 	if (windomain && *windomain)
9384520Snw141292 		free(*windomain);
9394520Snw141292 	if (unixname && *unixname)
9404520Snw141292 		free(*unixname);
9414520Snw141292 	return (retcode);
9424520Snw141292 }
9434520Snw141292 
9444520Snw141292 
9454520Snw141292 /*
9464520Snw141292  * Destroy the iterator
9474520Snw141292  */
9484520Snw141292 void
9495696Snw141292 idmap_iter_destroy(idmap_iter_t *iter)
9505696Snw141292 {
9514520Snw141292 	xdrproc_t _xdr_argument, _xdr_result;
9524520Snw141292 
9534520Snw141292 	if (iter == NULL)
9544520Snw141292 		return;
9554520Snw141292 
9564520Snw141292 	switch (iter->type) {
9574520Snw141292 	case IDMAP_LIST_NAMERULES:
9584520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_namerules_1_argument;
9594520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_namerules_res;
9604520Snw141292 		break;
9614520Snw141292 	case IDMAP_LIST_MAPPINGS:
9624520Snw141292 		_xdr_argument = (xdrproc_t)xdr_idmap_list_mappings_1_argument;
9634520Snw141292 		_xdr_result = (xdrproc_t)xdr_idmap_mappings_res;
9644520Snw141292 		break;
9654520Snw141292 	default:
9664520Snw141292 		free(iter);
9674520Snw141292 		return;
9684520Snw141292 	};
9694520Snw141292 
9704520Snw141292 	if (iter->arg) {
9714520Snw141292 		xdr_free(_xdr_argument, (caddr_t)iter->arg);
9724520Snw141292 		free(iter->arg);
9734520Snw141292 	}
9744520Snw141292 	if (iter->retlist) {
9754520Snw141292 		xdr_free(_xdr_result, (caddr_t)iter->retlist);
9764520Snw141292 		free(iter->retlist);
9774520Snw141292 	}
9784520Snw141292 	free(iter);
9794520Snw141292 }
9804520Snw141292 
9814520Snw141292 
9824520Snw141292 /*
9834520Snw141292  * Create handle to get SID to UID/GID mapping entries
9844520Snw141292  *
9854520Snw141292  * Input:
9864520Snw141292  * gh - "get mapping" handle
9874520Snw141292  */
9884520Snw141292 idmap_stat
9895696Snw141292 idmap_get_create(idmap_handle_t *handle, idmap_get_handle_t **gh)
9905696Snw141292 {
9914520Snw141292 	idmap_get_handle_t	*tmp;
9924520Snw141292 
9934520Snw141292 	/* sanity checks */
9944520Snw141292 	if (handle == NULL || gh == NULL) {
9954520Snw141292 		errno = EINVAL;
9964520Snw141292 		return (IDMAP_ERR_ARG);
9974520Snw141292 	}
9984520Snw141292 
9994520Snw141292 	/* allocate the handle */
10004520Snw141292 	if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
10014520Snw141292 		errno = ENOMEM;
10024520Snw141292 		return (IDMAP_ERR_MEMORY);
10034520Snw141292 	}
10044520Snw141292 
10054520Snw141292 	tmp->ih = handle;
10064520Snw141292 	*gh = tmp;
10074520Snw141292 	return (IDMAP_SUCCESS);
10084520Snw141292 }
10094520Snw141292 
10104520Snw141292 
10114520Snw141292 /*
10124520Snw141292  * Given SID, get UID
10134520Snw141292  *
10144520Snw141292  * Input:
10154520Snw141292  * sidprefix  - SID prefix
10164520Snw141292  * rid        - RID
10174520Snw141292  * flag       - flag
10184520Snw141292  *
10194520Snw141292  * Output:
10204520Snw141292  * stat - status of the get request
10214520Snw141292  * uid  - POSIX UID if stat = 0
10224520Snw141292  *
10234520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
10244520Snw141292  */
10254520Snw141292 idmap_stat
10264520Snw141292 idmap_get_uidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
10275696Snw141292 		int flag, uid_t *uid, idmap_stat *stat)
10285696Snw141292 {
10294520Snw141292 
10304520Snw141292 	idmap_retcode	retcode;
10314644Sbaban 	idmap_mapping	*mapping = NULL;
10324520Snw141292 
10334520Snw141292 	/* sanity checks */
10344520Snw141292 	if (gh == NULL)
10354520Snw141292 		return (IDMAP_ERR_ARG);
10364520Snw141292 	if (uid == NULL || sidprefix == NULL)
10374520Snw141292 		return (IDMAP_ERR_ARG);
10384520Snw141292 
10394520Snw141292 	/* Extend the request array and the return list */
10404520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
10414520Snw141292 		goto errout;
10424520Snw141292 
10434520Snw141292 	/* Setup the request */
10444520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
10454520Snw141292 	mapping->flag = flag;
10464520Snw141292 	mapping->id1.idtype = IDMAP_SID;
10474520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
10484520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
10494520Snw141292 		retcode = IDMAP_ERR_MEMORY;
10504520Snw141292 		goto errout;
10514520Snw141292 	}
10524520Snw141292 	mapping->id2.idtype = IDMAP_UID;
10534520Snw141292 
10544520Snw141292 	/* Setup pointers for the result */
10554520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_UID;
10564520Snw141292 	gh->retlist[gh->next].uid = uid;
10574520Snw141292 	gh->retlist[gh->next].stat = stat;
10584520Snw141292 
10594520Snw141292 	gh->next++;
10604520Snw141292 	return (IDMAP_SUCCESS);
10614520Snw141292 
10624520Snw141292 errout:
10634644Sbaban 	/* Batch created so far should still be usable */
10644644Sbaban 	if (mapping)
10654644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
10664520Snw141292 	errno = idmap_stat2errno(retcode);
10674520Snw141292 	return (retcode);
10684520Snw141292 }
10694520Snw141292 
10704520Snw141292 
10714520Snw141292 /*
10724520Snw141292  * Given SID, get GID
10734520Snw141292  *
10744520Snw141292  * Input:
10754520Snw141292  * sidprefix  - SID prefix
10764520Snw141292  * rid        - rid
10774520Snw141292  * flag       - flag
10784520Snw141292  *
10794520Snw141292  * Output:
10804520Snw141292  * stat - status of the get request
10814520Snw141292  * gid  - POSIX GID if stat = 0
10824520Snw141292  *
10834520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
10844520Snw141292  */
10854520Snw141292 idmap_stat
10864520Snw141292 idmap_get_gidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
10875696Snw141292 		int flag, gid_t *gid, idmap_stat *stat)
10885696Snw141292 {
10894520Snw141292 
10904520Snw141292 	idmap_retcode	retcode;
10914644Sbaban 	idmap_mapping	*mapping = NULL;
10924520Snw141292 
10934520Snw141292 	/* sanity checks */
10944520Snw141292 	if (gh == NULL)
10954520Snw141292 		return (IDMAP_ERR_ARG);
10964520Snw141292 	if (gid == NULL || sidprefix == NULL)
10974520Snw141292 		return (IDMAP_ERR_ARG);
10984520Snw141292 
10994520Snw141292 	/* Extend the request array and the return list */
11004520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
11014520Snw141292 		goto errout;
11024520Snw141292 
11034520Snw141292 	/* Setup the request */
11044520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
11054520Snw141292 	mapping->flag = flag;
11064520Snw141292 	mapping->id1.idtype = IDMAP_SID;
11074520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
11084520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
11094520Snw141292 		retcode = IDMAP_ERR_MEMORY;
11104520Snw141292 		goto errout;
11114520Snw141292 	}
11124520Snw141292 	mapping->id2.idtype = IDMAP_GID;
11134520Snw141292 
11144520Snw141292 	/* Setup pointers for the result */
11154520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_GID;
11164520Snw141292 	gh->retlist[gh->next].gid = gid;
11174520Snw141292 	gh->retlist[gh->next].stat = stat;
11184520Snw141292 
11194520Snw141292 	gh->next++;
11204520Snw141292 	return (IDMAP_SUCCESS);
11214520Snw141292 
11224520Snw141292 errout:
11234644Sbaban 	if (mapping)
11244644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
11254520Snw141292 	errno = idmap_stat2errno(retcode);
11264520Snw141292 	return (retcode);
11274520Snw141292 }
11284520Snw141292 
11294520Snw141292 
11304520Snw141292 /*
11314520Snw141292  * Given SID, get POSIX ID i.e. UID/GID
11324520Snw141292  *
11334520Snw141292  * Input:
11344520Snw141292  * sidprefix  - SID prefix
11354520Snw141292  * rid        - rid
11364520Snw141292  * flag       - flag
11374520Snw141292  *
11384520Snw141292  * Output:
11394520Snw141292  * stat    - status of the get request
11404520Snw141292  * is_user - user or group
11414520Snw141292  * pid     - POSIX UID if stat = 0 and is_user = 1
11424520Snw141292  *           POSIX GID if stat = 0 and is_user = 0
11434520Snw141292  *
11444520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
11454520Snw141292  */
11464520Snw141292 idmap_stat
11474520Snw141292 idmap_get_pidbysid(idmap_get_handle_t *gh, char *sidprefix, idmap_rid_t rid,
11485696Snw141292 		int flag, uid_t *pid, int *is_user, idmap_stat *stat)
11495696Snw141292 {
11504520Snw141292 	idmap_retcode	retcode;
11514644Sbaban 	idmap_mapping	*mapping = NULL;
11524520Snw141292 
11534520Snw141292 	/* sanity checks */
11544520Snw141292 	if (gh == NULL)
11554520Snw141292 		return (IDMAP_ERR_ARG);
11564520Snw141292 	if (pid == NULL || sidprefix == NULL || is_user == NULL)
11574520Snw141292 		return (IDMAP_ERR_ARG);
11584520Snw141292 
11594520Snw141292 	/* Extend the request array and the return list */
11604520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
11614520Snw141292 		goto errout;
11624520Snw141292 
11634520Snw141292 	/* Setup the request */
11644520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
11654520Snw141292 	mapping->flag = flag;
11664520Snw141292 	mapping->id1.idtype = IDMAP_SID;
11674520Snw141292 	mapping->id1.idmap_id_u.sid.rid = rid;
11684520Snw141292 	if ((mapping->id1.idmap_id_u.sid.prefix = strdup(sidprefix)) == NULL) {
11694520Snw141292 		retcode = IDMAP_ERR_MEMORY;
11704520Snw141292 		goto errout;
11714520Snw141292 	}
11724520Snw141292 	mapping->id2.idtype = IDMAP_POSIXID;
11734520Snw141292 
11744520Snw141292 	/* Setup pointers for the result */
11754520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_POSIXID;
11764520Snw141292 	gh->retlist[gh->next].uid = pid;
11774520Snw141292 	gh->retlist[gh->next].gid = pid;
11784520Snw141292 	gh->retlist[gh->next].is_user = is_user;
11794520Snw141292 	gh->retlist[gh->next].stat = stat;
11804520Snw141292 
11814520Snw141292 	gh->next++;
11824520Snw141292 	return (IDMAP_SUCCESS);
11834520Snw141292 
11844520Snw141292 errout:
11854644Sbaban 	if (mapping)
11864644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
11874520Snw141292 	errno = idmap_stat2errno(retcode);
11884520Snw141292 	return (retcode);
11894520Snw141292 }
11904520Snw141292 
11914520Snw141292 
11924520Snw141292 /*
11934520Snw141292  * Given UID, get SID
11944520Snw141292  *
11954520Snw141292  * Input:
11964520Snw141292  * uid  - POSIX UID
11974520Snw141292  * flag - flag
11984520Snw141292  *
11994520Snw141292  * Output:
12004520Snw141292  * stat - status of the get request
12014520Snw141292  * sid  - SID prefix (if stat == 0)
12024520Snw141292  * rid  - rid
12034520Snw141292  *
12044520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
12054520Snw141292  */
12064520Snw141292 idmap_stat
12074520Snw141292 idmap_get_sidbyuid(idmap_get_handle_t *gh, uid_t uid, int flag,
12085696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
12095696Snw141292 {
12104520Snw141292 
12114520Snw141292 	idmap_retcode	retcode;
12124644Sbaban 	idmap_mapping	*mapping = NULL;
12134520Snw141292 
12144520Snw141292 	/* sanity checks */
12154520Snw141292 	if (gh == NULL)
12164520Snw141292 		return (IDMAP_ERR_ARG);
12174520Snw141292 	if (sidprefix == NULL)
12184520Snw141292 		return (IDMAP_ERR_ARG);
12194520Snw141292 
12204520Snw141292 	/* Extend the request array and the return list */
12214520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
12224520Snw141292 		goto errout;
12234520Snw141292 
12244520Snw141292 	/* Setup the request */
12254520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
12264520Snw141292 	mapping->flag = flag;
12274520Snw141292 	mapping->id1.idtype = IDMAP_UID;
12284520Snw141292 	mapping->id1.idmap_id_u.uid = uid;
12294520Snw141292 	mapping->id2.idtype = IDMAP_SID;
12304520Snw141292 
12314520Snw141292 	/* Setup pointers for the result */
12324520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
12334520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
12344520Snw141292 	gh->retlist[gh->next].rid = rid;
12354520Snw141292 	gh->retlist[gh->next].stat = stat;
12364520Snw141292 
12374520Snw141292 	gh->next++;
12384520Snw141292 	return (IDMAP_SUCCESS);
12394520Snw141292 
12404520Snw141292 errout:
12414644Sbaban 	if (mapping)
12424644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
12434520Snw141292 	errno = idmap_stat2errno(retcode);
12444520Snw141292 	return (retcode);
12454520Snw141292 }
12464520Snw141292 
12474520Snw141292 
12484520Snw141292 /*
12494520Snw141292  * Given GID, get SID
12504520Snw141292  *
12514520Snw141292  * Input:
12524520Snw141292  * gid  - POSIX GID
12534520Snw141292  * flag - flag
12544520Snw141292  *
12554520Snw141292  * Output:
12564520Snw141292  * stat       - status of the get request
12574520Snw141292  * sidprefix  - SID prefix (if stat == 0)
12584520Snw141292  * rid        - rid
12594520Snw141292  *
12604520Snw141292  * Note: The output parameters will be set by idmap_get_mappings()
12614520Snw141292  */
12624520Snw141292 idmap_stat
12634520Snw141292 idmap_get_sidbygid(idmap_get_handle_t *gh, gid_t gid, int flag,
12645696Snw141292 		char **sidprefix, idmap_rid_t *rid, idmap_stat *stat)
12655696Snw141292 {
12664520Snw141292 
12674520Snw141292 	idmap_retcode	retcode;
12684644Sbaban 	idmap_mapping	*mapping = NULL;
12694520Snw141292 
12704520Snw141292 	/* sanity checks */
12714520Snw141292 	if (gh == NULL)
12724520Snw141292 		return (IDMAP_ERR_ARG);
12734520Snw141292 	if (sidprefix == NULL)
12744520Snw141292 		return (IDMAP_ERR_ARG);
12754520Snw141292 
12764520Snw141292 	/* Extend the request array and the return list */
12774520Snw141292 	if ((retcode = _get_ids_extend_batch(gh)) != IDMAP_SUCCESS)
12784520Snw141292 		goto errout;
12794520Snw141292 
12804520Snw141292 	/* Setup the request */
12814520Snw141292 	mapping = &gh->batch.idmap_mapping_batch_val[gh->next];
12824520Snw141292 	mapping->flag = flag;
12834520Snw141292 	mapping->id1.idtype = IDMAP_GID;
12844520Snw141292 	mapping->id1.idmap_id_u.gid = gid;
12854520Snw141292 	mapping->id2.idtype = IDMAP_SID;
12864520Snw141292 
12874520Snw141292 	/* Setup pointers for the result */
12884520Snw141292 	gh->retlist[gh->next].idtype = IDMAP_SID;
12894520Snw141292 	gh->retlist[gh->next].sidprefix = sidprefix;
12904520Snw141292 	gh->retlist[gh->next].rid = rid;
12914520Snw141292 	gh->retlist[gh->next].stat = stat;
12924520Snw141292 
12934520Snw141292 	gh->next++;
12944520Snw141292 	return (IDMAP_SUCCESS);
12954520Snw141292 
12964520Snw141292 errout:
12974644Sbaban 	if (mapping)
12984644Sbaban 		(void) memset(mapping, 0, sizeof (*mapping));
12994520Snw141292 	errno = idmap_stat2errno(retcode);
13004520Snw141292 	return (retcode);
13014520Snw141292 }
13024520Snw141292 
13034520Snw141292 
13044520Snw141292 /*
13054520Snw141292  * Process the batched "get mapping" requests. The results (i.e.
13064520Snw141292  * status and identity) will be available in the data areas
13074520Snw141292  * provided by individual requests.
13084520Snw141292  */
13094520Snw141292 idmap_stat
13105696Snw141292 idmap_get_mappings(idmap_get_handle_t *gh)
13115696Snw141292 {
13124520Snw141292 	CLIENT		*clnt;
13134520Snw141292 	enum clnt_stat	clntstat;
13144520Snw141292 	idmap_retcode	retcode;
13154520Snw141292 	idmap_ids_res	res;
13164520Snw141292 	idmap_id	*id;
13174520Snw141292 	int		i;
13184520Snw141292 
13194520Snw141292 	if (gh == NULL) {
13204520Snw141292 		errno = EINVAL;
13214520Snw141292 		return (IDMAP_ERR_ARG);
13224520Snw141292 	}
13234520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(gh->ih, clnt);
13244520Snw141292 
13254520Snw141292 	(void) memset(&res, 0, sizeof (idmap_ids_res));
13264520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_IDS,
13275696Snw141292 	    (xdrproc_t)xdr_idmap_mapping_batch,
13285696Snw141292 	    (caddr_t)&gh->batch,
13295696Snw141292 	    (xdrproc_t)xdr_idmap_ids_res,
13305696Snw141292 	    (caddr_t)&res,
13315696Snw141292 	    TIMEOUT);
13324520Snw141292 	if (clntstat != RPC_SUCCESS) {
13334644Sbaban 		retcode = _idmap_rpc2stat(clnt);
13344520Snw141292 		goto out;
13354520Snw141292 	}
13364520Snw141292 	if (res.retcode != IDMAP_SUCCESS) {
13374520Snw141292 		retcode = res.retcode;
13384520Snw141292 		goto out;
13394520Snw141292 	}
13404520Snw141292 	for (i = 0; i < gh->next; i++) {
13414520Snw141292 		if (i >= res.ids.ids_len) {
13424520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
13434520Snw141292 			continue;
13444520Snw141292 		}
13454520Snw141292 		*gh->retlist[i].stat = res.ids.ids_val[i].retcode;
13464520Snw141292 		id = &res.ids.ids_val[i].id;
13474520Snw141292 		switch (id->idtype) {
13484520Snw141292 		case IDMAP_UID:
13494520Snw141292 			if (gh->retlist[i].uid)
13504520Snw141292 				*gh->retlist[i].uid = id->idmap_id_u.uid;
13514520Snw141292 			if (gh->retlist[i].is_user)
13524520Snw141292 				*gh->retlist[i].is_user = 1;
13534520Snw141292 			break;
13544520Snw141292 		case IDMAP_GID:
13554520Snw141292 			if (gh->retlist[i].gid)
13564520Snw141292 				*gh->retlist[i].gid = id->idmap_id_u.gid;
13574520Snw141292 			if (gh->retlist[i].is_user)
13584520Snw141292 				*gh->retlist[i].is_user = 0;
13594520Snw141292 			break;
13604864Sbaban 		case IDMAP_POSIXID:
13614864Sbaban 			if (gh->retlist[i].uid)
13624864Sbaban 				*gh->retlist[i].uid = 60001;
13634864Sbaban 			if (gh->retlist[i].is_user)
13644864Sbaban 				*gh->retlist[i].is_user = -1;
13654864Sbaban 			break;
13664520Snw141292 		case IDMAP_SID:
13675696Snw141292 		case IDMAP_USID:
13685696Snw141292 		case IDMAP_GSID:
13694520Snw141292 			if (gh->retlist[i].rid)
13704520Snw141292 				*gh->retlist[i].rid = id->idmap_id_u.sid.rid;
13714520Snw141292 			if (gh->retlist[i].sidprefix) {
13724695Sbaban 				if (id->idmap_id_u.sid.prefix == NULL ||
13734695Sbaban 				    *id->idmap_id_u.sid.prefix == '\0') {
13744520Snw141292 					*gh->retlist[i].sidprefix = NULL;
13754520Snw141292 					break;
13764520Snw141292 				}
13774520Snw141292 				*gh->retlist[i].sidprefix =
13785696Snw141292 				    strdup(id->idmap_id_u.sid.prefix);
13794520Snw141292 				if (*gh->retlist[i].sidprefix == NULL)
13804520Snw141292 					*gh->retlist[i].stat =
13815696Snw141292 					    IDMAP_ERR_MEMORY;
13824520Snw141292 			}
13834520Snw141292 			break;
13844520Snw141292 		case IDMAP_NONE:
13854520Snw141292 			break;
13864520Snw141292 		default:
13874520Snw141292 			*gh->retlist[i].stat = IDMAP_ERR_NORESULT;
13884520Snw141292 			break;
13894520Snw141292 		}
13904520Snw141292 	}
13914520Snw141292 	retcode = IDMAP_SUCCESS;
13924520Snw141292 
13934520Snw141292 out:
13944644Sbaban 	_IDMAP_RESET_GET_HANDLE(gh);
13954520Snw141292 	(void) xdr_free(xdr_idmap_ids_res, (caddr_t)&res);
13964520Snw141292 	errno = idmap_stat2errno(retcode);
13974520Snw141292 	return (retcode);
13984520Snw141292 }
13994520Snw141292 
14004520Snw141292 
14014520Snw141292 /*
14024520Snw141292  * Destroy the "get mapping" handle
14034520Snw141292  */
14044520Snw141292 void
14055696Snw141292 idmap_get_destroy(idmap_get_handle_t *gh)
14065696Snw141292 {
14074520Snw141292 	if (gh == NULL)
14084520Snw141292 		return;
14094520Snw141292 	(void) xdr_free(xdr_idmap_mapping_batch, (caddr_t)&gh->batch);
14104520Snw141292 	if (gh->retlist)
14114520Snw141292 		free(gh->retlist);
14124520Snw141292 	free(gh);
14134520Snw141292 }
14144520Snw141292 
14154520Snw141292 
14164520Snw141292 /*
14174520Snw141292  * Get windows to unix mapping
14184520Snw141292  */
14194520Snw141292 idmap_stat
14204520Snw141292 idmap_get_w2u_mapping(idmap_handle_t *handle,
14214520Snw141292 		const char *sidprefix, idmap_rid_t *rid,
14224520Snw141292 		const char *winname, const char *windomain,
14235696Snw141292 		int flag, int *is_user, int *is_wuser,
14245696Snw141292 		uid_t *pid, char **unixname, int *direction)
14255696Snw141292 {
14264520Snw141292 	CLIENT			*clnt;
14274520Snw141292 	enum clnt_stat		clntstat;
14284520Snw141292 	idmap_mapping		request, *mapping;
14294520Snw141292 	idmap_mappings_res	result;
14304520Snw141292 	idmap_retcode		retcode, rc;
14314520Snw141292 
14324520Snw141292 	if (handle == NULL) {
14334520Snw141292 		errno = EINVAL;
14344520Snw141292 		return (IDMAP_ERR_ARG);
14354520Snw141292 	}
14364520Snw141292 
14374520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
14384520Snw141292 
14394520Snw141292 	(void) memset(&request, 0, sizeof (request));
14404520Snw141292 	(void) memset(&result, 0, sizeof (result));
14414520Snw141292 
14424520Snw141292 	if (pid)
14434520Snw141292 		*pid = UINT32_MAX;
14444520Snw141292 	if (unixname)
14454520Snw141292 		*unixname = NULL;
14464520Snw141292 	if (direction)
14474644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
14484520Snw141292 
14494520Snw141292 	request.flag = flag;
14504520Snw141292 	request.id1.idtype = IDMAP_SID;
14514520Snw141292 	if (sidprefix && rid) {
14524520Snw141292 		request.id1.idmap_id_u.sid.prefix = (char *)sidprefix;
14534520Snw141292 		request.id1.idmap_id_u.sid.rid = *rid;
14544520Snw141292 	} else if (winname) {
14555064Sdm199847 		retcode = idmap_strdupnull(&request.id1name, winname);
14565247Sbaban 		if (retcode != IDMAP_SUCCESS)
14574520Snw141292 			goto out;
14585064Sdm199847 
14595064Sdm199847 		retcode = idmap_strdupnull(&request.id1domain, windomain);
14605247Sbaban 		if (retcode != IDMAP_SUCCESS)
14615064Sdm199847 			goto out;
14625064Sdm199847 
14634520Snw141292 		request.id1.idmap_id_u.sid.prefix = NULL;
14644520Snw141292 	} else {
14654520Snw141292 		errno = EINVAL;
14664520Snw141292 		return (IDMAP_ERR_ARG);
14674520Snw141292 	}
14684520Snw141292 
14695696Snw141292 	if (*is_user == 1)
14704520Snw141292 		request.id2.idtype = IDMAP_UID;
14714520Snw141292 	else if (*is_user == 0)
14724520Snw141292 		request.id2.idtype = IDMAP_GID;
14734520Snw141292 	else
14744520Snw141292 		request.id2.idtype = IDMAP_POSIXID;
14754520Snw141292 
14765696Snw141292 	if (*is_wuser == 1)
14775696Snw141292 		request.id1.idtype = IDMAP_USID;
14785696Snw141292 	else if (*is_wuser == 0)
14795696Snw141292 		request.id1.idtype = IDMAP_GSID;
14805696Snw141292 	else
14815696Snw141292 		request.id1.idtype = IDMAP_SID;
14825696Snw141292 
14834520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
14845696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
14855696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
14865696Snw141292 	    TIMEOUT);
14874520Snw141292 
14884644Sbaban 	if (clntstat != RPC_SUCCESS)
14894644Sbaban 		return (_idmap_rpc2stat(clnt));
14904520Snw141292 
14914520Snw141292 	retcode = result.retcode;
14924520Snw141292 
14934520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
14944520Snw141292 		if (retcode == IDMAP_SUCCESS)
14954520Snw141292 			retcode = IDMAP_ERR_NORESULT;
14964520Snw141292 		goto out;
14974520Snw141292 	}
14984520Snw141292 
14994864Sbaban 	if (mapping->id2.idtype == IDMAP_UID) {
15005696Snw141292 		*is_user = 1;
15014864Sbaban 	} else if (mapping->id2.idtype == IDMAP_GID) {
15025696Snw141292 		*is_user = 0;
15034864Sbaban 	} else {
15044864Sbaban 		goto out;
15054864Sbaban 	}
15065696Snw141292 
15075696Snw141292 	if (mapping->id1.idtype == IDMAP_USID) {
15085696Snw141292 		*is_wuser = 1;
15095696Snw141292 	} else if (mapping->id1.idtype == IDMAP_GSID) {
15105696Snw141292 		*is_wuser = 0;
15115696Snw141292 	} else {
15125696Snw141292 		goto out;
15135696Snw141292 	}
15145696Snw141292 
15154520Snw141292 	if (direction)
15164520Snw141292 		*direction = mapping->direction;
15174520Snw141292 	if (pid)
15184520Snw141292 		*pid = mapping->id2.idmap_id_u.uid;
15195064Sdm199847 
15205064Sdm199847 	rc = idmap_strdupnull(unixname, mapping->id2name);
15215064Sdm199847 	if (rc != IDMAP_SUCCESS)
15225064Sdm199847 		retcode = rc;
15234520Snw141292 
15244520Snw141292 out:
15254520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
15264520Snw141292 	if (retcode != IDMAP_SUCCESS)
15274520Snw141292 		errno = idmap_stat2errno(retcode);
15284520Snw141292 	return (retcode);
15294520Snw141292 }
15304520Snw141292 
15314520Snw141292 
15324520Snw141292 /*
15334520Snw141292  * Get unix to windows mapping
15344520Snw141292  */
15354520Snw141292 idmap_stat
15364520Snw141292 idmap_get_u2w_mapping(idmap_handle_t *handle,
15374520Snw141292 		uid_t *pid, const char *unixname,
15385696Snw141292 		int flag, int is_user, int *is_wuser,
15394520Snw141292 		char **sidprefix, idmap_rid_t *rid,
15404520Snw141292 		char **winname, char **windomain,
15415696Snw141292 		int *direction)
15425696Snw141292 {
15434520Snw141292 	CLIENT			*clnt;
15444520Snw141292 	enum clnt_stat		clntstat;
15454520Snw141292 	idmap_mapping		request, *mapping;
15464520Snw141292 	idmap_mappings_res	result;
15474520Snw141292 	idmap_retcode		retcode, rc;
15484520Snw141292 
15494520Snw141292 	if (handle == NULL) {
15504520Snw141292 		errno = EINVAL;
15514520Snw141292 		return (IDMAP_ERR_ARG);
15524520Snw141292 	}
15534520Snw141292 
15544520Snw141292 	_IDMAP_GET_CLIENT_HANDLE(handle, clnt);
15554520Snw141292 
15564520Snw141292 	if (sidprefix)
15574520Snw141292 		*sidprefix = NULL;
15584520Snw141292 	if (winname)
15594520Snw141292 		*winname = NULL;
15604520Snw141292 	if (windomain)
15614520Snw141292 		*windomain = NULL;
15624520Snw141292 	if (rid)
15634520Snw141292 		*rid = UINT32_MAX;
15644520Snw141292 	if (direction)
15654644Sbaban 		*direction = IDMAP_DIRECTION_UNDEF;
15664520Snw141292 
15674520Snw141292 	(void) memset(&request, 0, sizeof (request));
15684520Snw141292 	(void) memset(&result, 0, sizeof (result));
15694520Snw141292 
15704520Snw141292 	request.flag = flag;
15714520Snw141292 	request.id1.idtype = is_user?IDMAP_UID:IDMAP_GID;
15724520Snw141292 
15734520Snw141292 	if (pid && *pid != UINT32_MAX) {
15744520Snw141292 		request.id1.idmap_id_u.uid = *pid;
15754520Snw141292 	} else if (unixname) {
15765064Sdm199847 		request.id1name = (char *)unixname;
15774520Snw141292 		request.id1.idmap_id_u.uid = UINT32_MAX;
15784520Snw141292 	} else {
15794520Snw141292 		errno = EINVAL;
15804520Snw141292 		return (IDMAP_ERR_ARG);
15814520Snw141292 	}
15824520Snw141292 
15835696Snw141292 	if (is_wuser == NULL)
15845696Snw141292 		request.id2.idtype = IDMAP_SID;
15855696Snw141292 	else if (*is_wuser == -1)
15865696Snw141292 		request.id2.idtype = IDMAP_SID;
15875696Snw141292 	else if (*is_wuser == 0)
15885696Snw141292 		request.id2.idtype = IDMAP_GSID;
15895696Snw141292 	else if (*is_wuser == 1)
15905696Snw141292 		request.id2.idtype = IDMAP_USID;
15914520Snw141292 
15924520Snw141292 	clntstat = clnt_call(clnt, IDMAP_GET_MAPPED_ID_BY_NAME,
15935696Snw141292 	    (xdrproc_t)xdr_idmap_mapping, (caddr_t)&request,
15945696Snw141292 	    (xdrproc_t)xdr_idmap_mappings_res, (caddr_t)&result,
15955696Snw141292 	    TIMEOUT);
15964520Snw141292 
15974644Sbaban 	if (clntstat != RPC_SUCCESS)
15984644Sbaban 		return (_idmap_rpc2stat(clnt));
15994520Snw141292 
16004520Snw141292 	retcode = result.retcode;
16014520Snw141292 
16024520Snw141292 	if ((mapping = result.mappings.mappings_val) == NULL) {
16034520Snw141292 		if (retcode == IDMAP_SUCCESS)
16044520Snw141292 			retcode = IDMAP_ERR_NORESULT;
16054520Snw141292 		goto out;
16064520Snw141292 	}
16074520Snw141292 
16085696Snw141292 	if (direction != NULL)
16094520Snw141292 		*direction = mapping->direction;
16105696Snw141292 
16115696Snw141292 	if (is_wuser != NULL)
16125696Snw141292 		*is_wuser = mapping->id2.idtype == IDMAP_USID ? 1 : 0;
16135696Snw141292 
16144695Sbaban 	if (sidprefix && mapping->id2.idmap_id_u.sid.prefix &&
16154695Sbaban 	    *mapping->id2.idmap_id_u.sid.prefix != '\0') {
16164520Snw141292 		*sidprefix = strdup(mapping->id2.idmap_id_u.sid.prefix);
16174520Snw141292 		if (*sidprefix == NULL) {
16184520Snw141292 			retcode = IDMAP_ERR_MEMORY;
16194520Snw141292 			goto errout;
16204520Snw141292 		}
16214520Snw141292 	}
16224520Snw141292 	if (rid)
16234520Snw141292 		*rid = mapping->id2.idmap_id_u.sid.rid;
16245064Sdm199847 
16255064Sdm199847 	rc = idmap_strdupnull(winname, mapping->id2name);
16265064Sdm199847 	if (rc != IDMAP_SUCCESS)
16275064Sdm199847 		retcode = rc;
16285064Sdm199847 
16295064Sdm199847 	rc = idmap_strdupnull(windomain, mapping->id2domain);
16305064Sdm199847 	if (rc != IDMAP_SUCCESS)
16315064Sdm199847 		retcode = rc;
16324520Snw141292 
16334520Snw141292 	goto out;
16344520Snw141292 
16354520Snw141292 errout:
16364520Snw141292 	if (sidprefix && *sidprefix) {
16374520Snw141292 		free(*sidprefix);
16384520Snw141292 		*sidprefix = NULL;
16394520Snw141292 	}
16404520Snw141292 	if (winname && *winname) {
16414520Snw141292 		free(*winname);
16424520Snw141292 		*winname = NULL;
16434520Snw141292 	}
16444520Snw141292 	if (windomain && *windomain) {
16454520Snw141292 		free(*windomain);
16464520Snw141292 		*windomain = NULL;
16474520Snw141292 	}
16484520Snw141292 
16494520Snw141292 out:
16504520Snw141292 	xdr_free(xdr_idmap_mappings_res, (caddr_t)&result);
16514520Snw141292 	if (retcode != IDMAP_SUCCESS)
16524520Snw141292 		errno = idmap_stat2errno(retcode);
16534520Snw141292 	return (retcode);
16544520Snw141292 }
16554520Snw141292 
16564520Snw141292 
16574520Snw141292 
16584520Snw141292 #define	gettext(s)	s
16594520Snw141292 static stat_table_t stattable[] = {
16604520Snw141292 	{IDMAP_SUCCESS, gettext("Success"), 0},
16614520Snw141292 	{IDMAP_NEXT, gettext("More results available"), 0},
16624520Snw141292 	{IDMAP_ERR_OTHER, gettext("Undefined error"), EINVAL},
16634520Snw141292 	{IDMAP_ERR_INTERNAL, gettext("Internal error"), EINVAL},
16644520Snw141292 	{IDMAP_ERR_MEMORY, gettext("Out of memory"), ENOMEM},
16654520Snw141292 	{IDMAP_ERR_NORESULT, gettext("No results available"), EINVAL},
16664520Snw141292 	{IDMAP_ERR_NOTUSER, gettext("Not a user"), EINVAL},
16674520Snw141292 	{IDMAP_ERR_NOTGROUP, gettext("Not a group"), EINVAL},
16684644Sbaban 	{IDMAP_ERR_NOTSUPPORTED, gettext("Operation not supported"), ENOTSUP},
16694520Snw141292 	{IDMAP_ERR_W2U_NAMERULE,
16704520Snw141292 		gettext("Invalid Windows to UNIX name-based rule"), EINVAL},
16714520Snw141292 	{IDMAP_ERR_U2W_NAMERULE,
16724520Snw141292 		gettext("Invalid UNIX to Windows name-based rule"), EINVAL},
16734520Snw141292 	{IDMAP_ERR_CACHE, gettext("Invalid cache"), EINVAL},
16744520Snw141292 	{IDMAP_ERR_DB, gettext("Invalid database"), EINVAL},
16754520Snw141292 	{IDMAP_ERR_ARG, gettext("Invalid argument"), EINVAL},
16764520Snw141292 	{IDMAP_ERR_SID, gettext("Invalid SID"), EINVAL},
16774520Snw141292 	{IDMAP_ERR_IDTYPE, gettext("Invalid identity type"), EINVAL},
16784644Sbaban 	{IDMAP_ERR_RPC_HANDLE, gettext("Bad RPC handle"), EBADF},
16794520Snw141292 	{IDMAP_ERR_RPC, gettext("RPC error"), EINVAL},
16804520Snw141292 	{IDMAP_ERR_CLIENT_HANDLE, gettext("Bad client handle"), EINVAL},
16814644Sbaban 	{IDMAP_ERR_BUSY, gettext("Server is busy"), EBUSY},
16824695Sbaban 	{IDMAP_ERR_PERMISSION_DENIED, gettext("Permission denied"), EACCES},
16834520Snw141292 	{IDMAP_ERR_NOMAPPING,
16844520Snw141292 		gettext("Mapping not found or inhibited"), EINVAL},
16854520Snw141292 	{IDMAP_ERR_NEW_ID_ALLOC_REQD,
16864520Snw141292 		gettext("New mapping needs to be created"), EINVAL},
16874520Snw141292 	{IDMAP_ERR_DOMAIN, gettext("Invalid domain"), EINVAL},
16884520Snw141292 	{IDMAP_ERR_SECURITY, gettext("Security issue"), EINVAL},
16894520Snw141292 	{IDMAP_ERR_NOTFOUND, gettext("Not found"), EINVAL},
16904520Snw141292 	{IDMAP_ERR_DOMAIN_NOTFOUND, gettext("Domain not found"), EINVAL},
16914520Snw141292 	{IDMAP_ERR_UPDATE_NOTALLOWED, gettext("Update not allowed"), EINVAL},
16924520Snw141292 	{IDMAP_ERR_CFG, gettext("Configuration error"), EINVAL},
16934520Snw141292 	{IDMAP_ERR_CFG_CHANGE, gettext("Invalid configuration change"), EINVAL},
16944520Snw141292 	{IDMAP_ERR_NOTMAPPED_WELLKNOWN,
16954520Snw141292 		gettext("No mapping for well-known SID"), EINVAL},
16964520Snw141292 	{IDMAP_ERR_RETRIABLE_NET_ERR,
16974864Sbaban 		gettext("Windows lookup failed"), EINVAL},
16984864Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
16994864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
17004864Sbaban 		"Windows to UNIX name-based rule"), EINVAL},
17014864Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
17024864Sbaban 		gettext("Duplicate rule or conflicts with an existing "
17034864Sbaban 		"Unix to Windows name-based rule"), EINVAL},
1704*5968Snw141292 	{IDMAP_ERR_BAD_UTF8,
1705*5968Snw141292 		gettext("Invalid or illegal UTF-8 sequence found in "
1706*5968Snw141292 		"a given Windows entity name or domain name"), EINVAL},
17074520Snw141292 	{-1, NULL, 0}
17084520Snw141292 };
17094520Snw141292 #undef	gettext
17104520Snw141292 
17114520Snw141292 
17124520Snw141292 /*
17134520Snw141292  * Get description of status code
17144520Snw141292  *
17154520Snw141292  * Input:
17164520Snw141292  * status - Status code returned by libidmap API call
17174520Snw141292  *
17184520Snw141292  * Return Value:
17194520Snw141292  * human-readable localized description of idmap_stat
17204520Snw141292  */
17214520Snw141292 /* ARGSUSED */
17224520Snw141292 const char *
17235696Snw141292 idmap_stat2string(idmap_handle_t *handle, idmap_stat status)
17245696Snw141292 {
17254520Snw141292 	int i;
17264520Snw141292 
17274520Snw141292 	for (i = 0; stattable[i].msg; i++) {
17284520Snw141292 		if (stattable[i].retcode == status)
17294526Sbaban 			return (gettext(stattable[i].msg));
17304520Snw141292 	}
17314520Snw141292 	return (gettext("Unknown error"));
17324520Snw141292 }
17334520Snw141292 
17344520Snw141292 
17354520Snw141292 static int
17365696Snw141292 idmap_stat2errno(idmap_stat stat)
17375696Snw141292 {
17384520Snw141292 	int i;
17394520Snw141292 	for (i = 0; stattable[i].msg; i++) {
17404520Snw141292 		if (stattable[i].retcode == stat)
17414520Snw141292 			return (stattable[i].errnum);
17424520Snw141292 	}
17434520Snw141292 	return (EINVAL);
17444520Snw141292 }
17454520Snw141292 
17464520Snw141292 
17474520Snw141292 /*
17484520Snw141292  * Get status code from string
17494520Snw141292  */
17504520Snw141292 idmap_stat
17515696Snw141292 idmap_string2stat(const char *str)
17525696Snw141292 {
17534520Snw141292 	if (str == NULL)
17544520Snw141292 		return (IDMAP_ERR_INTERNAL);
17554520Snw141292 
17564520Snw141292 #define	return_cmp(a) \
17574520Snw141292 	if (0 == strcmp(str, "IDMAP_ERR_" #a)) \
17584520Snw141292 		return (IDMAP_ERR_ ## a);
17594520Snw141292 
17604520Snw141292 	return_cmp(OTHER);
17614520Snw141292 	return_cmp(INTERNAL);
17624520Snw141292 	return_cmp(MEMORY);
17634520Snw141292 	return_cmp(NORESULT);
17644520Snw141292 	return_cmp(NOTUSER);
17654520Snw141292 	return_cmp(NOTGROUP);
17664520Snw141292 	return_cmp(NOTSUPPORTED);
17674520Snw141292 	return_cmp(W2U_NAMERULE);
17684520Snw141292 	return_cmp(U2W_NAMERULE);
17694520Snw141292 	return_cmp(CACHE);
17704520Snw141292 	return_cmp(DB);
17714520Snw141292 	return_cmp(ARG);
17724520Snw141292 	return_cmp(SID);
17734520Snw141292 	return_cmp(IDTYPE);
17744520Snw141292 	return_cmp(RPC_HANDLE);
17754520Snw141292 	return_cmp(RPC);
17764520Snw141292 	return_cmp(CLIENT_HANDLE);
17774520Snw141292 	return_cmp(BUSY);
17784520Snw141292 	return_cmp(PERMISSION_DENIED);
17794520Snw141292 	return_cmp(NOMAPPING);
17804520Snw141292 	return_cmp(NEW_ID_ALLOC_REQD);
17814520Snw141292 	return_cmp(DOMAIN);
17824520Snw141292 	return_cmp(SECURITY);
17834520Snw141292 	return_cmp(NOTFOUND);
17844520Snw141292 	return_cmp(DOMAIN_NOTFOUND);
17854520Snw141292 	return_cmp(MEMORY);
17864520Snw141292 	return_cmp(UPDATE_NOTALLOWED);
17874520Snw141292 	return_cmp(CFG);
17884520Snw141292 	return_cmp(CFG_CHANGE);
17894520Snw141292 	return_cmp(NOTMAPPED_WELLKNOWN);
17904520Snw141292 	return_cmp(RETRIABLE_NET_ERR);
17914864Sbaban 	return_cmp(W2U_NAMERULE_CONFLICT);
17924864Sbaban 	return_cmp(U2W_NAMERULE_CONFLICT);
17934520Snw141292 #undef return_cmp
17944520Snw141292 
17954520Snw141292 	return (IDMAP_ERR_OTHER);
17964520Snw141292 }
17974520Snw141292 
17984520Snw141292 
17994520Snw141292 /*
18004520Snw141292  * Map the given status to one that can be returned by the protocol
18014520Snw141292  */
18024520Snw141292 idmap_stat
18035696Snw141292 idmap_stat4prot(idmap_stat status)
18045696Snw141292 {
18054520Snw141292 	switch (status) {
18064520Snw141292 	case IDMAP_ERR_MEMORY:
18074520Snw141292 	case IDMAP_ERR_CACHE:
18084520Snw141292 		return (IDMAP_ERR_INTERNAL);
18094520Snw141292 	}
18104520Snw141292 	return (status);
18114520Snw141292 }
18125043Sbaban 
18135043Sbaban 
18145043Sbaban /*
18155247Sbaban  * This is a convenience routine which duplicates a string after
18165247Sbaban  * checking for NULL pointers. This function will return success if
18175247Sbaban  * either the 'to' OR 'from' pointers are NULL.
18185064Sdm199847  */
18195064Sdm199847 static idmap_stat
18205696Snw141292 idmap_strdupnull(char **to, const char *from)
18215696Snw141292 {
18225247Sbaban 	if (to == NULL)
18235247Sbaban 		return (IDMAP_SUCCESS);
18245247Sbaban 
18255064Sdm199847 	if (from == NULL || *from == '\0') {
18265064Sdm199847 		*to = NULL;
18275064Sdm199847 		return (IDMAP_SUCCESS);
18285064Sdm199847 	}
18295064Sdm199847 
18305064Sdm199847 	*to = strdup(from);
18315064Sdm199847 	if (*to == NULL)
18325064Sdm199847 		return (IDMAP_ERR_MEMORY);
18335064Sdm199847 	return (IDMAP_SUCCESS);
18345064Sdm199847 }
18355064Sdm199847 
18365064Sdm199847 idmap_stat
18375696Snw141292 idmap_namerule_cpy(idmap_namerule *to, idmap_namerule *from)
18385696Snw141292 {
18395064Sdm199847 	idmap_stat retval;
18405064Sdm199847 
18415064Sdm199847 	(void) memcpy(to, from, sizeof (idmap_namerule));
18425064Sdm199847 
18435064Sdm199847 	retval = idmap_strdupnull(&to->windomain, from->windomain);
18445064Sdm199847 	if (retval != IDMAP_SUCCESS)
18455064Sdm199847 		return (retval);
18465064Sdm199847 
18475064Sdm199847 	retval = idmap_strdupnull(&to->winname, from->winname);
18485064Sdm199847 	if (retval != IDMAP_SUCCESS)
18495064Sdm199847 		return (retval);
18505064Sdm199847 
18515064Sdm199847 	retval = idmap_strdupnull(&to->unixname, from->unixname);
18525064Sdm199847 
18535064Sdm199847 	return (retval);
18545064Sdm199847 }
18555064Sdm199847 
18565064Sdm199847 
18575064Sdm199847 /*
18585043Sbaban  * Get uid given Windows name
18595043Sbaban  */
18605043Sbaban idmap_stat
18615696Snw141292 idmap_getuidbywinname(const char *name, const char *domain, uid_t *uid)
18625696Snw141292 {
18635043Sbaban 	idmap_handle_t	*ih;
18645043Sbaban 	idmap_retcode	rc;
18655696Snw141292 	int		is_user = 1;
18665696Snw141292 	int		is_wuser = -1;
18675043Sbaban 
18685043Sbaban 	if (uid == NULL)
18695043Sbaban 		return (IDMAP_ERR_ARG);
18705043Sbaban 
18715043Sbaban 	/* Get mapping */
18725043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
18735043Sbaban 		return (rc);
18745043Sbaban 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, 0,
18755696Snw141292 	    &is_user, &is_wuser, uid, NULL, NULL);
18765043Sbaban 	(void) idmap_fini(ih);
18775043Sbaban 
18785043Sbaban 	/*
18795043Sbaban 	 * XXX Until we have diagonal mapping support, check if
18805043Sbaban 	 * the given name belongs to a user
18815043Sbaban 	 */
18825043Sbaban 	if (rc == IDMAP_SUCCESS && !is_user)
18835043Sbaban 		return (IDMAP_ERR_NOTUSER);
18845043Sbaban 	return (rc);
18855043Sbaban }
18865043Sbaban 
18875043Sbaban 
18885043Sbaban /*
18895043Sbaban  * Get gid given Windows name
18905043Sbaban  */
18915043Sbaban idmap_stat
18925696Snw141292 idmap_getgidbywinname(const char *name, const char *domain, gid_t *gid)
18935696Snw141292 {
18945043Sbaban 	idmap_handle_t	*ih;
18955043Sbaban 	idmap_retcode	rc;
18965696Snw141292 	int		is_user = 0;
18975696Snw141292 	int		is_wuser = -1;
18985043Sbaban 
18995043Sbaban 	if (gid == NULL)
19005043Sbaban 		return (IDMAP_ERR_ARG);
19015043Sbaban 
19025043Sbaban 	/* Get mapping */
19035043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
19045043Sbaban 		return (rc);
19055043Sbaban 	rc = idmap_get_w2u_mapping(ih, NULL, NULL, name, domain, 0,
19065696Snw141292 	    &is_user, &is_wuser, gid, NULL, NULL);
19075043Sbaban 	(void) idmap_fini(ih);
19085043Sbaban 
19095043Sbaban 	/*
19105043Sbaban 	 * XXX Until we have diagonal mapping support, check if
19115043Sbaban 	 * the given name belongs to a group
19125043Sbaban 	 */
19135043Sbaban 	if (rc == IDMAP_SUCCESS && is_user)
19145043Sbaban 		return (IDMAP_ERR_NOTGROUP);
19155043Sbaban 	return (rc);
19165043Sbaban }
19175043Sbaban 
19185043Sbaban 
19195043Sbaban /*
19205043Sbaban  * Get winname given pid
19215043Sbaban  */
19225043Sbaban static idmap_retcode
19235696Snw141292 idmap_getwinnamebypid(uid_t pid, int is_user, char **name, char **domain)
19245696Snw141292 {
19255043Sbaban 	idmap_handle_t	*ih;
19265043Sbaban 	idmap_retcode	rc;
19275043Sbaban 	int		len;
19285043Sbaban 	char		*winname, *windomain;
19295043Sbaban 
19305043Sbaban 	if (name == NULL)
19315043Sbaban 		return (IDMAP_ERR_ARG);
19325043Sbaban 
19335043Sbaban 	/* Get mapping */
19345043Sbaban 	if ((rc = idmap_init(&ih)) != IDMAP_SUCCESS)
19355043Sbaban 		return (rc);
19365696Snw141292 	rc = idmap_get_u2w_mapping(ih, &pid, NULL, 0, is_user, NULL, NULL,
19375043Sbaban 	    NULL, &winname, &windomain, NULL);
19385043Sbaban 	(void) idmap_fini(ih);
19395043Sbaban 
19405043Sbaban 	/* Return on error */
19415043Sbaban 	if (rc != IDMAP_SUCCESS)
19425043Sbaban 		return (rc);
19435043Sbaban 
19445043Sbaban 	/*
19455043Sbaban 	 * The given PID may have been mapped to a locally
19465043Sbaban 	 * generated SID in which case there isn't any
19475043Sbaban 	 * Windows name
19485043Sbaban 	 */
19495043Sbaban 	if (winname == NULL || windomain == NULL) {
19505043Sbaban 		idmap_free(winname);
19515043Sbaban 		idmap_free(windomain);
19525043Sbaban 		return (IDMAP_ERR_NORESULT);
19535043Sbaban 	}
19545043Sbaban 
19555043Sbaban 	if (domain != NULL) {
19565043Sbaban 		*name = winname;
19575043Sbaban 		*domain = windomain;
19585043Sbaban 	} else {
19595043Sbaban 		len = strlen(winname) + strlen(windomain) + 2;
19605043Sbaban 		if ((*name = malloc(len)) != NULL)
19615043Sbaban 			(void) snprintf(*name, len, "%s@%s", winname,
19625043Sbaban 			    windomain);
19635043Sbaban 		else
19645043Sbaban 			rc = IDMAP_ERR_MEMORY;
19655043Sbaban 		idmap_free(winname);
19665043Sbaban 		idmap_free(windomain);
19675043Sbaban 	}
19685043Sbaban 	return (rc);
19695043Sbaban }
19705043Sbaban 
19715043Sbaban 
19725043Sbaban /*
19735043Sbaban  * Get winname given uid
19745043Sbaban  */
19755043Sbaban idmap_stat
19765696Snw141292 idmap_getwinnamebyuid(uid_t uid, char **name, char **domain)
19775696Snw141292 {
19785043Sbaban 	return (idmap_getwinnamebypid(uid, 1, name, domain));
19795043Sbaban }
19805043Sbaban 
19815043Sbaban 
19825043Sbaban /*
19835043Sbaban  * Get winname given gid
19845043Sbaban  */
19855043Sbaban idmap_stat
19865696Snw141292 idmap_getwinnamebygid(gid_t gid, char **name, char **domain)
19875696Snw141292 {
19885043Sbaban 	return (idmap_getwinnamebypid(gid, 0, name, domain));
19895043Sbaban }
1990