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  * Service routines
304520Snw141292  */
314520Snw141292 
324520Snw141292 #include "idmapd.h"
334520Snw141292 #include "idmap_priv.h"
345731Sbaban #include "nldaputils.h"
354520Snw141292 #include <signal.h>
364520Snw141292 #include <thread.h>
374520Snw141292 #include <string.h>
384520Snw141292 #include <strings.h>
394520Snw141292 #include <errno.h>
404520Snw141292 #include <assert.h>
414520Snw141292 #include <sys/types.h>
424520Snw141292 #include <sys/stat.h>
434520Snw141292 #include <ucred.h>
444520Snw141292 #include <pwd.h>
454520Snw141292 #include <auth_attr.h>
464520Snw141292 #include <secdb.h>
47*5968Snw141292 #include <sys/u8_textprep.h>
484520Snw141292 
494520Snw141292 #define	_VALIDATE_LIST_CB_DATA(col, val, siz)\
504520Snw141292 	retcode = validate_list_cb_data(cb_data, argc, argv, col,\
514520Snw141292 			(uchar_t **)val, siz);\
524520Snw141292 	if (retcode == IDMAP_NEXT) {\
534520Snw141292 		result->retcode = IDMAP_NEXT;\
544520Snw141292 		return (0);\
554520Snw141292 	} else if (retcode < 0) {\
564520Snw141292 		result->retcode = retcode;\
574520Snw141292 		return (1);\
584520Snw141292 	}
594520Snw141292 
604520Snw141292 #define	PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\
614520Snw141292 	rcode = process_list_svc_sql(db, sql, limit, cb, res);\
624520Snw141292 	if (rcode == IDMAP_ERR_BUSY)\
634520Snw141292 		res->retcode = IDMAP_ERR_BUSY;\
644520Snw141292 	else if (rcode == IDMAP_SUCCESS && len == 0)\
654520Snw141292 		res->retcode = IDMAP_ERR_NOTFOUND;
664520Snw141292 
674520Snw141292 
685064Sdm199847 #define	STRDUP_OR_FAIL(to, from) \
695064Sdm199847 	if ((from) == NULL) \
705064Sdm199847 		to = NULL; \
715064Sdm199847 	else { \
725064Sdm199847 		if ((to = strdup(from)) == NULL) \
735064Sdm199847 			return (1); \
745064Sdm199847 	}
755064Sdm199847 
764520Snw141292 /* ARGSUSED */
774520Snw141292 bool_t
785696Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp)
795696Snw141292 {
804520Snw141292 	return (TRUE);
814520Snw141292 }
824520Snw141292 
835474Sbaban /*
845474Sbaban  * RPC layer allocates empty strings to replace NULL char *.
855474Sbaban  * This utility function frees these empty strings.
865474Sbaban  */
875731Sbaban static
885731Sbaban void
895474Sbaban sanitize_mapping_request(idmap_mapping *req)
905474Sbaban {
915474Sbaban 	free(req->id1name);
925474Sbaban 	req->id1name = NULL;
935474Sbaban 	free(req->id1domain);
945474Sbaban 	req->id1domain = NULL;
955474Sbaban 	free(req->id2name);
965474Sbaban 	req->id2name = NULL;
975474Sbaban 	free(req->id2domain);
985474Sbaban 	req->id2domain = NULL;
995731Sbaban 	req->direction = _IDMAP_F_DONE;
1005474Sbaban }
1015474Sbaban 
102*5968Snw141292 static
103*5968Snw141292 int
104*5968Snw141292 validate_mapped_id_by_name_req(idmap_mapping *req)
105*5968Snw141292 {
106*5968Snw141292 	int e;
107*5968Snw141292 
108*5968Snw141292 	if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req))
109*5968Snw141292 		return (IDMAP_SUCCESS);
110*5968Snw141292 
111*5968Snw141292 	if (IS_REQUEST_SID(*req, 1)) {
112*5968Snw141292 		if (!EMPTY_STRING(req->id1name) &&
113*5968Snw141292 		    u8_validate(req->id1name, strlen(req->id1name),
114*5968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
115*5968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
116*5968Snw141292 		if (!EMPTY_STRING(req->id1domain) &&
117*5968Snw141292 		    u8_validate(req->id1domain, strlen(req->id1domain),
118*5968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
119*5968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
120*5968Snw141292 	}
121*5968Snw141292 
122*5968Snw141292 	return (IDMAP_SUCCESS);
123*5968Snw141292 }
124*5968Snw141292 
125*5968Snw141292 static
126*5968Snw141292 int
127*5968Snw141292 validate_rule(idmap_namerule *rule)
128*5968Snw141292 {
129*5968Snw141292 	int e;
130*5968Snw141292 
131*5968Snw141292 	if (!EMPTY_STRING(rule->winname) &&
132*5968Snw141292 	    u8_validate(rule->winname, strlen(rule->winname),
133*5968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
134*5968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
135*5968Snw141292 
136*5968Snw141292 	if (!EMPTY_STRING(rule->windomain) &&
137*5968Snw141292 	    u8_validate(rule->windomain, strlen(rule->windomain),
138*5968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
139*5968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
140*5968Snw141292 
141*5968Snw141292 	return (IDMAP_SUCCESS);
142*5968Snw141292 
143*5968Snw141292 }
144*5968Snw141292 
145*5968Snw141292 static
146*5968Snw141292 bool_t
147*5968Snw141292 validate_rules(idmap_update_batch *batch)
148*5968Snw141292 {
149*5968Snw141292 	idmap_update_op	*up;
150*5968Snw141292 	int i;
151*5968Snw141292 
152*5968Snw141292 	for (i = 0; i < batch->idmap_update_batch_len; i++) {
153*5968Snw141292 		up = &(batch->idmap_update_batch_val[i]);
154*5968Snw141292 		if (validate_rule(&(up->idmap_update_op_u.rule))
155*5968Snw141292 		    != IDMAP_SUCCESS)
156*5968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
157*5968Snw141292 	}
158*5968Snw141292 
159*5968Snw141292 	return (IDMAP_SUCCESS);
160*5968Snw141292 }
161*5968Snw141292 
1624520Snw141292 /* ARGSUSED */
1634520Snw141292 bool_t
1644520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
1655696Snw141292 		idmap_ids_res *result, struct svc_req *rqstp)
1665696Snw141292 {
1674520Snw141292 	sqlite		*cache = NULL, *db = NULL;
1684520Snw141292 	lookup_state_t	state;
1695731Sbaban 	idmap_retcode	retcode;
1704864Sbaban 	uint_t		i;
1714520Snw141292 
1724520Snw141292 	/* Init */
1734520Snw141292 	(void) memset(result, 0, sizeof (*result));
1744520Snw141292 	(void) memset(&state, 0, sizeof (state));
1754520Snw141292 
1764520Snw141292 	/* Return success if nothing was requested */
1774520Snw141292 	if (batch.idmap_mapping_batch_len < 1)
1784520Snw141292 		goto out;
1794520Snw141292 
1804520Snw141292 	/* Get cache handle */
1814520Snw141292 	result->retcode = get_cache_handle(&cache);
1824520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1834520Snw141292 		goto out;
1844520Snw141292 
1854520Snw141292 	/* Get db handle */
1864520Snw141292 	result->retcode = get_db_handle(&db);
1874520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1884520Snw141292 		goto out;
1894520Snw141292 
1904520Snw141292 	/* Allocate result array */
1914520Snw141292 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
1925696Snw141292 	    sizeof (idmap_id_res));
1934520Snw141292 	if (result->ids.ids_val == NULL) {
1944520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
1954520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
1964520Snw141292 		goto out;
1974520Snw141292 	}
1984520Snw141292 	result->ids.ids_len = batch.idmap_mapping_batch_len;
1994520Snw141292 
2004864Sbaban 	/* Allocate hash table to check for duplicate sids */
2014864Sbaban 	state.sid_history = calloc(batch.idmap_mapping_batch_len,
2025696Snw141292 	    sizeof (*state.sid_history));
2034864Sbaban 	if (state.sid_history == NULL) {
2044864Sbaban 		idmapdlog(LOG_ERR, "Out of memory");
2054864Sbaban 		result->retcode = IDMAP_ERR_MEMORY;
2064864Sbaban 		goto out;
2074864Sbaban 	}
2084864Sbaban 	state.sid_history_size = batch.idmap_mapping_batch_len;
2094864Sbaban 	for (i = 0; i < state.sid_history_size; i++) {
2104864Sbaban 		state.sid_history[i].key = state.sid_history_size;
2114864Sbaban 		state.sid_history[i].next = state.sid_history_size;
2124864Sbaban 	}
2134864Sbaban 	state.batch = &batch;
2144864Sbaban 	state.result = result;
2154864Sbaban 
2165731Sbaban 	/* Get directory-based name mapping info */
2175731Sbaban 	result->retcode = get_ds_namemap_type(&state);
2185731Sbaban 	if (result->retcode != IDMAP_SUCCESS)
2195731Sbaban 		goto out;
2205731Sbaban 
2214520Snw141292 	/* Init our 'done' flags */
2224520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
2234520Snw141292 
2244520Snw141292 	/* First stage */
2254520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
2264864Sbaban 		state.curpos = i;
2275474Sbaban 		(void) sanitize_mapping_request(
2285474Sbaban 		    &batch.idmap_mapping_batch_val[i]);
2294520Snw141292 		if (IS_BATCH_SID(batch, i)) {
2304520Snw141292 			retcode = sid2pid_first_pass(
2315696Snw141292 			    &state,
2325696Snw141292 			    cache,
2335696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2345696Snw141292 			    &result->ids.ids_val[i]);
2354520Snw141292 		} else if (IS_BATCH_UID(batch, i)) {
2364520Snw141292 			retcode = pid2sid_first_pass(
2375696Snw141292 			    &state,
2385696Snw141292 			    cache,
2395696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2405696Snw141292 			    &result->ids.ids_val[i], 1, 0);
2414520Snw141292 		} else if (IS_BATCH_GID(batch, i)) {
2424520Snw141292 			retcode = pid2sid_first_pass(
2435696Snw141292 			    &state,
2445696Snw141292 			    cache,
2455696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2465696Snw141292 			    &result->ids.ids_val[i], 0, 0);
2474520Snw141292 		} else {
2484520Snw141292 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
2494520Snw141292 			continue;
2504520Snw141292 		}
2514520Snw141292 		if (IDMAP_FATAL_ERROR(retcode)) {
2524520Snw141292 			result->retcode = retcode;
2534520Snw141292 			goto out;
2544520Snw141292 		}
2554520Snw141292 	}
2564520Snw141292 
2574520Snw141292 	/* Check if we are done */
2584520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
2594520Snw141292 		goto out;
2604520Snw141292 
2615731Sbaban 	/*
2625731Sbaban 	 * native LDAP lookups:
2635731Sbaban 	 * If nldap or mixed mode is enabled then pid2sid mapping requests
2645731Sbaban 	 * need to lookup native LDAP directory service by uid/gid to get
2655731Sbaban 	 * winname and unixname.
2665731Sbaban 	 */
2675731Sbaban 	if (state.nldap_nqueries) {
2685731Sbaban 		retcode = nldap_lookup_batch(&state, &batch, result);
2695731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
2705731Sbaban 			result->retcode = retcode;
2714520Snw141292 			goto out;
2724520Snw141292 		}
2735731Sbaban 	}
2744520Snw141292 
2755731Sbaban 	/*
2765731Sbaban 	 * AD lookups:
2775731Sbaban 	 * 1. The pid2sid requests in the preceding step which successfully
2785731Sbaban 	 *    retrieved winname from native LDAP objects will now need to
2795731Sbaban 	 *    lookup AD by winname to get sid.
2805731Sbaban 	 * 2. The sid2pid requests will need to lookup AD by sid to get
2815731Sbaban 	 *    winname and unixname (AD or mixed mode).
2825731Sbaban 	 * 3. If AD-based name mapping is enabled then pid2sid mapping
2835731Sbaban 	 *    requests need to lookup AD by unixname to get winname and sid.
2845731Sbaban 	 */
2855731Sbaban 	if (state.ad_nqueries) {
2865731Sbaban 		retcode = ad_lookup_batch(&state, &batch, result);
2875731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
2885731Sbaban 			result->retcode = retcode;
2895731Sbaban 			goto out;
2905731Sbaban 		}
2915731Sbaban 	}
2925731Sbaban 
2935731Sbaban 	/*
2945731Sbaban 	 * native LDAP lookups:
2955731Sbaban 	 * If nldap mode is enabled then sid2pid mapping requests
2965731Sbaban 	 * which successfully retrieved winname from AD objects in the
2975731Sbaban 	 * preceding step, will now need to lookup native LDAP directory
2985731Sbaban 	 * service by winname to get unixname and pid.
2995731Sbaban 	 */
3005731Sbaban 	if (state.nldap_nqueries) {
3015731Sbaban 		retcode = nldap_lookup_batch(&state, &batch, result);
3025731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
3035731Sbaban 			result->retcode = retcode;
3045731Sbaban 			goto out;
3055731Sbaban 		}
3065731Sbaban 	}
3075731Sbaban 
3085731Sbaban 	/* Reset 'done' flags */
3095731Sbaban 	state.sid2pid_done = state.pid2sid_done = TRUE;
3104520Snw141292 
3114520Snw141292 	/* Second stage */
3124520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
3134864Sbaban 		state.curpos = i;
3144520Snw141292 		if (IS_BATCH_SID(batch, i)) {
3154520Snw141292 			retcode = sid2pid_second_pass(
3165696Snw141292 			    &state,
3175696Snw141292 			    cache,
3185696Snw141292 			    db,
3195696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3205696Snw141292 			    &result->ids.ids_val[i]);
3215731Sbaban 		} else if (IS_BATCH_UID(batch, i)) {
3225731Sbaban 			retcode = pid2sid_second_pass(
3235731Sbaban 			    &state,
3245731Sbaban 			    cache,
3255731Sbaban 			    db,
3265731Sbaban 			    &batch.idmap_mapping_batch_val[i],
3275731Sbaban 			    &result->ids.ids_val[i], 1);
3285731Sbaban 		} else if (IS_BATCH_GID(batch, i)) {
3295731Sbaban 			retcode = pid2sid_second_pass(
3305731Sbaban 			    &state,
3315731Sbaban 			    cache,
3325731Sbaban 			    db,
3335731Sbaban 			    &batch.idmap_mapping_batch_val[i],
3345731Sbaban 			    &result->ids.ids_val[i], 0);
3355731Sbaban 		} else {
3365731Sbaban 			/* First stage has already set the error */
3375731Sbaban 			continue;
3385731Sbaban 		}
3395731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
3405731Sbaban 			result->retcode = retcode;
3415731Sbaban 			goto out;
3424520Snw141292 		}
3434520Snw141292 	}
3444520Snw141292 
3454520Snw141292 	/* Check if we are done */
3464520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
3474520Snw141292 		goto out;
3484520Snw141292 
3494520Snw141292 	/* Reset our 'done' flags */
3504520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
3514520Snw141292 
3524520Snw141292 	/* Update cache in a single transaction */
3534520Snw141292 	if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS)
3544520Snw141292 		goto out;
3554520Snw141292 
3564520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
3574864Sbaban 		state.curpos = i;
3584520Snw141292 		if (IS_BATCH_SID(batch, i)) {
3594520Snw141292 			(void) update_cache_sid2pid(
3605696Snw141292 			    &state,
3615696Snw141292 			    cache,
3625696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3635696Snw141292 			    &result->ids.ids_val[i]);
3644520Snw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
3655696Snw141292 		    (IS_BATCH_GID(batch, i))) {
3664520Snw141292 			(void) update_cache_pid2sid(
3675696Snw141292 			    &state,
3685696Snw141292 			    cache,
3695696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3705696Snw141292 			    &result->ids.ids_val[i]);
3714520Snw141292 		}
3724520Snw141292 	}
3734520Snw141292 
3745331Samw 	/* Commit if we have at least one successful update */
3754520Snw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
3764520Snw141292 		(void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;");
3774520Snw141292 	else
3784520Snw141292 		(void) sql_exec_no_cb(cache, "END TRANSACTION;");
3794520Snw141292 
3804520Snw141292 out:
3815731Sbaban 	cleanup_lookup_state(&state);
3824520Snw141292 	if (IDMAP_ERROR(result->retcode)) {
3834520Snw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
3844520Snw141292 		result->ids.ids_len = 0;
3854520Snw141292 		result->ids.ids_val = NULL;
3864520Snw141292 	}
3874520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
3884520Snw141292 	return (TRUE);
3894520Snw141292 }
3904520Snw141292 
3914520Snw141292 
3924520Snw141292 /* ARGSUSED */
3935696Snw141292 static
3945696Snw141292 int
3955696Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames)
3965696Snw141292 {
3974520Snw141292 	list_cb_data_t		*cb_data;
3984520Snw141292 	char			*str;
3994520Snw141292 	idmap_mappings_res	*result;
4004520Snw141292 	idmap_retcode		retcode;
4014520Snw141292 	int			w2u, u2w;
4024520Snw141292 	char			*end;
4035696Snw141292 	static int		validated_column_names = 0;
4045696Snw141292 
4055696Snw141292 	if (!validated_column_names) {
4065696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
4075696Snw141292 		assert(strcmp(colnames[1], "sidprefix") == 0);
4085696Snw141292 		assert(strcmp(colnames[2], "rid") == 0);
4095696Snw141292 		assert(strcmp(colnames[3], "pid") == 0);
4105696Snw141292 		assert(strcmp(colnames[4], "w2u") == 0);
4115696Snw141292 		assert(strcmp(colnames[5], "u2w") == 0);
4125696Snw141292 		assert(strcmp(colnames[6], "windomain") == 0);
4135696Snw141292 		assert(strcmp(colnames[7], "canon_winname") == 0);
4145696Snw141292 		assert(strcmp(colnames[8], "unixname") == 0);
4155696Snw141292 		assert(strcmp(colnames[9], "is_user") == 0);
4165696Snw141292 		assert(strcmp(colnames[10], "is_wuser") == 0);
4175696Snw141292 		validated_column_names = 1;
4185696Snw141292 	}
4195696Snw141292 
4204520Snw141292 
4214520Snw141292 	cb_data = (list_cb_data_t *)parg;
4224520Snw141292 	result = (idmap_mappings_res *)cb_data->result;
4234520Snw141292 
4245696Snw141292 	_VALIDATE_LIST_CB_DATA(11, &result->mappings.mappings_val,
4255696Snw141292 	    sizeof (idmap_mapping));
4264520Snw141292 
4274520Snw141292 	result->mappings.mappings_len++;
4284520Snw141292 
4294520Snw141292 	if ((str = strdup(argv[1])) == NULL)
4304520Snw141292 		return (1);
4314520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
4325696Snw141292 	    str;
4334520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
4345696Snw141292 	    strtoul(argv[2], &end, 10);
4355696Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype =
4365696Snw141292 	    strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID;
4374520Snw141292 
4384520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
4395696Snw141292 	    strtoul(argv[3], &end, 10);
4405696Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype =
4415696Snw141292 	    strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID;
4424520Snw141292 
4435696Snw141292 	w2u = argv[4] ? strtol(argv[4], &end, 10) : 0;
4445696Snw141292 	u2w = argv[5] ? strtol(argv[5], &end, 10) : 0;
4454520Snw141292 
4464520Snw141292 	if (w2u > 0 && u2w == 0)
4474644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4484644Sbaban 		    IDMAP_DIRECTION_W2U;
4494520Snw141292 	else if (w2u == 0 && u2w > 0)
4504644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4514644Sbaban 		    IDMAP_DIRECTION_U2W;
4524520Snw141292 	else
4534644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4544644Sbaban 		    IDMAP_DIRECTION_BI;
4554520Snw141292 
4565064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain,
4575064Sdm199847 	    argv[6]);
4584520Snw141292 
4595064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name,
4605064Sdm199847 	    argv[7]);
4614520Snw141292 
4625064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name,
4635064Sdm199847 	    argv[8]);
4645064Sdm199847 
4654520Snw141292 
4664520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
4674520Snw141292 	cb_data->next++;
4684520Snw141292 	result->retcode = IDMAP_SUCCESS;
4694520Snw141292 	return (0);
4704520Snw141292 }
4714520Snw141292 
4724520Snw141292 
4734520Snw141292 /* ARGSUSED */
4744520Snw141292 bool_t
4755696Snw141292 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit,
4765696Snw141292     idmap_mappings_res *result, struct svc_req *rqstp)
4775696Snw141292 {
4784520Snw141292 	sqlite		*cache = NULL;
4794520Snw141292 	char		lbuf[30], rbuf[30];
4804520Snw141292 	uint64_t	maxlimit;
4814520Snw141292 	idmap_retcode	retcode;
4824520Snw141292 	char		*sql = NULL;
4834520Snw141292 
4844520Snw141292 	(void) memset(result, 0, sizeof (*result));
4854520Snw141292 	lbuf[0] = rbuf[0] = 0;
4864520Snw141292 
4874520Snw141292 	RDLOCK_CONFIG();
4884520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
4894520Snw141292 	UNLOCK_CONFIG();
4904520Snw141292 
4914520Snw141292 	/* Get cache handle */
4924520Snw141292 	result->retcode = get_cache_handle(&cache);
4934520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
4944520Snw141292 		goto out;
4954520Snw141292 
4964520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
4974520Snw141292 
4984520Snw141292 	/* Create LIMIT expression. */
4994520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
5004520Snw141292 		limit = maxlimit;
5014520Snw141292 	if (limit > 0)
5024520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
5035696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
5044520Snw141292 
5054520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
5064520Snw141292 
5074520Snw141292 	/*
5084520Snw141292 	 * Combine all the above into a giant SELECT statement that
5094520Snw141292 	 * will return the requested mappings
5104520Snw141292 	 */
5115696Snw141292 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w, "
5125696Snw141292 	    "windomain, canon_winname, unixname, is_user, is_wuser "
5135696Snw141292 	    " FROM idmap_cache WHERE "
5145696Snw141292 	    " %s %s;",
5155696Snw141292 	    rbuf, lbuf);
5164520Snw141292 	if (sql == NULL) {
5174520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
5184520Snw141292 		goto out;
5194520Snw141292 	}
5204520Snw141292 
5214520Snw141292 	/* Execute the SQL statement and update the return buffer */
5224520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb,
5235696Snw141292 	    result, result->mappings.mappings_len);
5244520Snw141292 
5254520Snw141292 out:
5264520Snw141292 	if (sql)
5274520Snw141292 		sqlite_freemem(sql);
5284520Snw141292 	if (IDMAP_ERROR(result->retcode))
5294520Snw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
5304520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
5314520Snw141292 	return (TRUE);
5324520Snw141292 }
5334520Snw141292 
5344520Snw141292 
5354520Snw141292 /* ARGSUSED */
5365696Snw141292 static
5375696Snw141292 int
5385696Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames)
5395696Snw141292 {
5404520Snw141292 	list_cb_data_t		*cb_data;
5414520Snw141292 	idmap_namerules_res	*result;
5424520Snw141292 	idmap_retcode		retcode;
5434520Snw141292 	int			w2u_order, u2w_order;
5444520Snw141292 	char			*end;
5455696Snw141292 	static int		validated_column_names = 0;
5465696Snw141292 
5475696Snw141292 	if (!validated_column_names) {
5485696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
5495696Snw141292 		assert(strcmp(colnames[1], "is_user") == 0);
5505696Snw141292 		assert(strcmp(colnames[2], "is_wuser") == 0);
5515696Snw141292 		assert(strcmp(colnames[3], "windomain") == 0);
5525696Snw141292 		assert(strcmp(colnames[4], "winname_display") == 0);
5535696Snw141292 		assert(strcmp(colnames[5], "is_nt4") == 0);
5545696Snw141292 		assert(strcmp(colnames[6], "unixname") == 0);
5555696Snw141292 		assert(strcmp(colnames[7], "w2u_order") == 0);
5565696Snw141292 		assert(strcmp(colnames[8], "u2w_order") == 0);
5575696Snw141292 		validated_column_names = 1;
5585696Snw141292 	}
5594520Snw141292 
5604520Snw141292 	cb_data = (list_cb_data_t *)parg;
5614520Snw141292 	result = (idmap_namerules_res *)cb_data->result;
5624520Snw141292 
5635696Snw141292 	_VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val,
5645696Snw141292 	    sizeof (idmap_namerule));
5654520Snw141292 
5664520Snw141292 	result->rules.rules_len++;
5674520Snw141292 
5684520Snw141292 	result->rules.rules_val[cb_data->next].is_user =
5695696Snw141292 	    strtol(argv[1], &end, 10);
5705696Snw141292 
5715696Snw141292 	result->rules.rules_val[cb_data->next].is_wuser =
5725696Snw141292 	    strtol(argv[2], &end, 10);
5734520Snw141292 
5745064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain,
5755064Sdm199847 	    argv[3]);
5764520Snw141292 
5775696Snw141292 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname,
5785696Snw141292 	    argv[4]);
5795696Snw141292 
5804520Snw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
5815696Snw141292 	    strtol(argv[5], &end, 10);
5824520Snw141292 
5835064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname,
5845696Snw141292 	    argv[6]);
5854520Snw141292 
5865696Snw141292 	w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0;
5875696Snw141292 	u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0;
5884520Snw141292 
5894520Snw141292 	if (w2u_order > 0 && u2w_order == 0)
5904644Sbaban 		result->rules.rules_val[cb_data->next].direction =
5914644Sbaban 		    IDMAP_DIRECTION_W2U;
5924520Snw141292 	else if (w2u_order == 0 && u2w_order > 0)
5934644Sbaban 		result->rules.rules_val[cb_data->next].direction =
5944644Sbaban 		    IDMAP_DIRECTION_U2W;
5954520Snw141292 	else
5964644Sbaban 		result->rules.rules_val[cb_data->next].direction =
5974644Sbaban 		    IDMAP_DIRECTION_BI;
5984520Snw141292 
5994520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
6004520Snw141292 	cb_data->next++;
6014520Snw141292 	result->retcode = IDMAP_SUCCESS;
6024520Snw141292 	return (0);
6034520Snw141292 }
6044520Snw141292 
6054520Snw141292 
6064520Snw141292 /* ARGSUSED */
6074520Snw141292 bool_t
6084520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
6094520Snw141292 		uint64_t limit, idmap_namerules_res *result,
6105696Snw141292 		struct svc_req *rqstp)
6115696Snw141292 {
6124520Snw141292 
6134520Snw141292 	sqlite		*db = NULL;
6144520Snw141292 	char		w2ubuf[15], u2wbuf[15];
6154520Snw141292 	char		lbuf[30], rbuf[30];
6164520Snw141292 	char		*sql = NULL;
6175696Snw141292 	char		*expr = NULL;
6184520Snw141292 	uint64_t	maxlimit;
6194520Snw141292 	idmap_retcode	retcode;
6204520Snw141292 
6214520Snw141292 	(void) memset(result, 0, sizeof (*result));
6224520Snw141292 	lbuf[0] = rbuf[0] = 0;
6234520Snw141292 
624*5968Snw141292 	result->retcode = validate_rule(&rule);
625*5968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
626*5968Snw141292 		goto out;
627*5968Snw141292 
6284520Snw141292 	RDLOCK_CONFIG();
6294520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
6304520Snw141292 	UNLOCK_CONFIG();
6314520Snw141292 
6324520Snw141292 	/* Get db handle */
6334520Snw141292 	result->retcode = get_db_handle(&db);
6344520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
6354520Snw141292 		goto out;
6364520Snw141292 
6374520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
6384520Snw141292 
6395696Snw141292 	w2ubuf[0] = u2wbuf[0] = 0;
6405696Snw141292 	if (rule.direction == IDMAP_DIRECTION_BI) {
6414520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
6424520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
6434644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
6444520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
6454520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
6465696Snw141292 		    "AND (u2w_order = 0 OR u2w_order ISNULL)");
6474644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
6484520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
6495696Snw141292 		    "AND (w2u_order = 0 OR w2u_order ISNULL)");
6504520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
6514520Snw141292 	}
6524520Snw141292 
653*5968Snw141292 	result->retcode = gen_sql_expr_from_rule(&rule, &expr);
654*5968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
6555696Snw141292 		goto out;
6564520Snw141292 
6574520Snw141292 	/* Create LIMIT expression. */
6584520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
6594520Snw141292 		limit = maxlimit;
6604520Snw141292 	if (limit > 0)
6614520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
6625696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
6634520Snw141292 
6644520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
6654520Snw141292 
6664520Snw141292 	/*
6674520Snw141292 	 * Combine all the above into a giant SELECT statement that
6684520Snw141292 	 * will return the requested rules
6694520Snw141292 	 */
6705696Snw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, "
6715696Snw141292 	    "winname_display, is_nt4, unixname, w2u_order, u2w_order "
6725696Snw141292 	    "FROM namerules WHERE "
6735696Snw141292 	    " %s %s %s %s %s;",
6745696Snw141292 	    rbuf, expr, w2ubuf, u2wbuf, lbuf);
6755696Snw141292 
6764520Snw141292 	if (sql == NULL) {
6774520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
6784520Snw141292 		goto out;
6794520Snw141292 	}
6804520Snw141292 
6814520Snw141292 	/* Execute the SQL statement and update the return buffer */
6824520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb,
6835696Snw141292 	    result, result->rules.rules_len);
6844520Snw141292 
6854520Snw141292 out:
6865696Snw141292 	if (expr)
6875696Snw141292 		sqlite_freemem(expr);
6884520Snw141292 	if (sql)
6894520Snw141292 		sqlite_freemem(sql);
6904520Snw141292 	if (IDMAP_ERROR(result->retcode))
6914520Snw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
6924520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
6934520Snw141292 	return (TRUE);
6944520Snw141292 }
6954520Snw141292 
6964520Snw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
6974520Snw141292 static int
6985696Snw141292 verify_rules_auth(struct svc_req *rqstp)
6995696Snw141292 {
7004520Snw141292 	ucred_t		*uc = NULL;
7014520Snw141292 	uid_t		uid;
7024520Snw141292 	char		buf[1024];
7034520Snw141292 	struct passwd	pwd;
7044520Snw141292 	const char	*me = "verify_rules_auth";
7054520Snw141292 
7064520Snw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
7074520Snw141292 		idmapdlog(LOG_ERR,
7085696Snw141292 		    "%s: svc_getcallerucred failed (errno=%d)",
7095696Snw141292 		    me, errno);
7104520Snw141292 		return (-1);
7114520Snw141292 	}
7124520Snw141292 
7134520Snw141292 	uid = ucred_geteuid(uc);
7144520Snw141292 	if (uid == (uid_t)-1) {
7154520Snw141292 		idmapdlog(LOG_ERR,
7165696Snw141292 		    "%s: ucred_geteuid failed (errno=%d)",
7175696Snw141292 		    me, errno);
7184520Snw141292 		ucred_free(uc);
7194520Snw141292 		return (-1);
7204520Snw141292 	}
7214520Snw141292 
7224520Snw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
7234520Snw141292 		idmapdlog(LOG_ERR,
7245696Snw141292 		    "%s: getpwuid_r(%u) failed (errno=%d)",
7255696Snw141292 		    me, uid, errno);
7264520Snw141292 		ucred_free(uc);
7274520Snw141292 		return (-1);
7284520Snw141292 	}
7294520Snw141292 
7304520Snw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
7314520Snw141292 		idmapdlog(LOG_INFO,
7325696Snw141292 		    "%s: %s does not have authorization.",
7335696Snw141292 		    me, pwd.pw_name);
7344520Snw141292 		ucred_free(uc);
7354520Snw141292 		return (-1);
7364520Snw141292 	}
7374520Snw141292 
7384520Snw141292 	ucred_free(uc);
7394520Snw141292 	return (1);
7404520Snw141292 }
7414520Snw141292 
7425064Sdm199847 /*
7435064Sdm199847  * Meaning of the return values is the following: For retcode ==
7445064Sdm199847  * IDMAP_SUCCESS, everything went OK and error_index is
7455064Sdm199847  * undefined. Otherwise, error_index >=0 shows the failed batch
7465064Sdm199847  * element. errro_index == -1 indicates failure at the beginning,
7475064Sdm199847  * error_index == -2 at the end.
7485064Sdm199847  */
7495064Sdm199847 
7504520Snw141292 /* ARGSUSED */
7514520Snw141292 bool_t
7525064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res,
7535696Snw141292 		struct svc_req *rqstp)
7545696Snw141292 {
7554520Snw141292 	sqlite		*db = NULL;
7564520Snw141292 	idmap_update_op	*up;
7574520Snw141292 	int		i;
7584884Sjp151216 	int		trans = FALSE;
7594520Snw141292 
7605064Sdm199847 	res->error_index = -1;
7615064Sdm199847 	(void) memset(&res->error_rule, 0, sizeof (res->error_rule));
7625064Sdm199847 	(void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule));
7635064Sdm199847 
7644520Snw141292 	if (verify_rules_auth(rqstp) < 0) {
7655064Sdm199847 		res->retcode = IDMAP_ERR_PERMISSION_DENIED;
7664520Snw141292 		goto out;
7674520Snw141292 	}
7684520Snw141292 
7694520Snw141292 	if (batch.idmap_update_batch_len == 0 ||
7705696Snw141292 	    batch.idmap_update_batch_val == NULL) {
7715064Sdm199847 		res->retcode = IDMAP_SUCCESS;
7724520Snw141292 		goto out;
7734520Snw141292 	}
7744520Snw141292 
775*5968Snw141292 	res->retcode = validate_rules(&batch);
776*5968Snw141292 	if (res->retcode != IDMAP_SUCCESS)
777*5968Snw141292 		goto out;
778*5968Snw141292 
7794520Snw141292 	/* Get db handle */
7805064Sdm199847 	res->retcode = get_db_handle(&db);
7815064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
7824520Snw141292 		goto out;
7834520Snw141292 
7845064Sdm199847 	res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;");
7855064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
7864520Snw141292 		goto out;
7874884Sjp151216 	trans = TRUE;
7884520Snw141292 
7894520Snw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
7904520Snw141292 		up = &batch.idmap_update_batch_val[i];
7914520Snw141292 		switch (up->opnum) {
7924520Snw141292 		case OP_NONE:
7935064Sdm199847 			res->retcode = IDMAP_SUCCESS;
7944520Snw141292 			break;
7954520Snw141292 		case OP_ADD_NAMERULE:
7965064Sdm199847 			res->retcode = add_namerule(db,
7975696Snw141292 			    &up->idmap_update_op_u.rule);
7984520Snw141292 			break;
7994520Snw141292 		case OP_RM_NAMERULE:
8005064Sdm199847 			res->retcode = rm_namerule(db,
8015696Snw141292 			    &up->idmap_update_op_u.rule);
8024520Snw141292 			break;
8034520Snw141292 		case OP_FLUSH_NAMERULES:
8045696Snw141292 			res->retcode = flush_namerules(db);
8054520Snw141292 			break;
8064520Snw141292 		default:
8075064Sdm199847 			res->retcode = IDMAP_ERR_NOTSUPPORTED;
8085064Sdm199847 			break;
8094520Snw141292 		};
8104520Snw141292 
8115064Sdm199847 		if (res->retcode != IDMAP_SUCCESS) {
8125064Sdm199847 			res->error_index = i;
8135064Sdm199847 			if (up->opnum == OP_ADD_NAMERULE ||
8145064Sdm199847 			    up->opnum == OP_RM_NAMERULE) {
8155064Sdm199847 				idmap_stat r2 =
8165064Sdm199847 				    idmap_namerule_cpy(&res->error_rule,
8175696Snw141292 				    &up->idmap_update_op_u.rule);
8185064Sdm199847 				if (r2 != IDMAP_SUCCESS)
8195064Sdm199847 					res->retcode = r2;
8205064Sdm199847 			}
8214520Snw141292 			goto out;
8225064Sdm199847 		}
8234520Snw141292 	}
8244520Snw141292 
8254520Snw141292 out:
8264884Sjp151216 	if (trans) {
8275064Sdm199847 		if (res->retcode == IDMAP_SUCCESS) {
8285064Sdm199847 			res->retcode =
8295064Sdm199847 			    sql_exec_no_cb(db, "COMMIT TRANSACTION;");
8305064Sdm199847 			if (res->retcode !=  IDMAP_SUCCESS)
8315064Sdm199847 				res->error_index = -2;
8325064Sdm199847 		}
8334884Sjp151216 		else
8344884Sjp151216 			(void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;");
8354520Snw141292 	}
8365064Sdm199847 
8375064Sdm199847 	res->retcode = idmap_stat4prot(res->retcode);
8385064Sdm199847 
8394520Snw141292 	return (TRUE);
8404520Snw141292 }
8414520Snw141292 
8424520Snw141292 
8434520Snw141292 /* ARGSUSED */
8444520Snw141292 bool_t
8454520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
8465696Snw141292 		idmap_mappings_res *result, struct svc_req *rqstp)
8475696Snw141292 {
8484520Snw141292 	sqlite		*cache = NULL, *db = NULL;
8494520Snw141292 
8504520Snw141292 	/* Init */
8514520Snw141292 	(void) memset(result, 0, sizeof (*result));
8524520Snw141292 
853*5968Snw141292 	result->retcode = validate_mapped_id_by_name_req(&request);
854*5968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
855*5968Snw141292 		goto out;
856*5968Snw141292 
8574520Snw141292 	/* Get cache handle */
8584520Snw141292 	result->retcode = get_cache_handle(&cache);
8594520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
8604520Snw141292 		goto out;
8614520Snw141292 
8624520Snw141292 	/* Get db handle */
8634520Snw141292 	result->retcode = get_db_handle(&db);
8644520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
8654520Snw141292 		goto out;
8664520Snw141292 
8674520Snw141292 	/* Allocate result */
8684520Snw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
8694520Snw141292 	if (result->mappings.mappings_val == NULL) {
8704520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
8714520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
8724520Snw141292 		goto out;
8734520Snw141292 	}
8744520Snw141292 	result->mappings.mappings_len = 1;
8754520Snw141292 
8765696Snw141292 
8775696Snw141292 	if (IS_REQUEST_SID(request, 1)) {
8784520Snw141292 		result->retcode = get_w2u_mapping(
8795696Snw141292 		    cache,
8805696Snw141292 		    db,
8815696Snw141292 		    &request,
8825696Snw141292 		    result->mappings.mappings_val);
8834520Snw141292 	} else if (IS_REQUEST_UID(request)) {
8844520Snw141292 		result->retcode = get_u2w_mapping(
8855696Snw141292 		    cache,
8865696Snw141292 		    db,
8875696Snw141292 		    &request,
8885696Snw141292 		    result->mappings.mappings_val,
8895696Snw141292 		    1);
8904520Snw141292 	} else if (IS_REQUEST_GID(request)) {
8914520Snw141292 		result->retcode = get_u2w_mapping(
8925696Snw141292 		    cache,
8935696Snw141292 		    db,
8945696Snw141292 		    &request,
8955696Snw141292 		    result->mappings.mappings_val,
8965696Snw141292 		    0);
8974520Snw141292 	} else {
8984520Snw141292 		result->retcode = IDMAP_ERR_IDTYPE;
8994520Snw141292 	}
9004520Snw141292 
9014520Snw141292 out:
9024520Snw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
9034520Snw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
9044520Snw141292 		result->mappings.mappings_len = 0;
9054520Snw141292 		result->mappings.mappings_val = NULL;
9064520Snw141292 	}
9074520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
9084520Snw141292 	return (TRUE);
9094520Snw141292 }
9104520Snw141292 
9114520Snw141292 
9124520Snw141292 /* ARGSUSED */
9134520Snw141292 int
9144520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
9155696Snw141292 		caddr_t result)
9165696Snw141292 {
9174520Snw141292 	(void) xdr_free(xdr_result, result);
9184520Snw141292 	return (TRUE);
9194520Snw141292 }
920