14520Snw141292 /*
24520Snw141292  * CDDL HEADER START
34520Snw141292  *
44520Snw141292  * The contents of this file are subject to the terms of the
54520Snw141292  * Common Development and Distribution License (the "License").
64520Snw141292  * You may not use this file except in compliance with the License.
74520Snw141292  *
84520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292  * or http://www.opensolaris.org/os/licensing.
104520Snw141292  * See the License for the specific language governing permissions
114520Snw141292  * and limitations under the License.
124520Snw141292  *
134520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292  *
194520Snw141292  * CDDL HEADER END
204520Snw141292  */
214520Snw141292 /*
225968Snw141292  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234520Snw141292  * Use is subject to license terms.
244520Snw141292  */
254520Snw141292 
264520Snw141292 #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>
475968Snw141292 #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 
60*6386Sjp151216 #define	PROCESS_LIST_SVC_SQL(rcode, db, dbname, sql, limit, flag, cb, res, len)\
61*6386Sjp151216 	rcode = process_list_svc_sql(db, dbname, sql, limit, flag, 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 
1025968Snw141292 static
1035968Snw141292 int
1045968Snw141292 validate_mapped_id_by_name_req(idmap_mapping *req)
1055968Snw141292 {
1065968Snw141292 	int e;
1075968Snw141292 
1085968Snw141292 	if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req))
1095968Snw141292 		return (IDMAP_SUCCESS);
1105968Snw141292 
1115968Snw141292 	if (IS_REQUEST_SID(*req, 1)) {
1125968Snw141292 		if (!EMPTY_STRING(req->id1name) &&
1135968Snw141292 		    u8_validate(req->id1name, strlen(req->id1name),
1145968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1155968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1165968Snw141292 		if (!EMPTY_STRING(req->id1domain) &&
1175968Snw141292 		    u8_validate(req->id1domain, strlen(req->id1domain),
1185968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1195968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1205968Snw141292 	}
1215968Snw141292 
1225968Snw141292 	return (IDMAP_SUCCESS);
1235968Snw141292 }
1245968Snw141292 
1255968Snw141292 static
1265968Snw141292 int
1275968Snw141292 validate_rule(idmap_namerule *rule)
1285968Snw141292 {
1295968Snw141292 	int e;
1305968Snw141292 
1315968Snw141292 	if (!EMPTY_STRING(rule->winname) &&
1325968Snw141292 	    u8_validate(rule->winname, strlen(rule->winname),
1335968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1345968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
1355968Snw141292 
1365968Snw141292 	if (!EMPTY_STRING(rule->windomain) &&
1375968Snw141292 	    u8_validate(rule->windomain, strlen(rule->windomain),
1385968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1395968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
1405968Snw141292 
1415968Snw141292 	return (IDMAP_SUCCESS);
1425968Snw141292 
1435968Snw141292 }
1445968Snw141292 
1455968Snw141292 static
1465968Snw141292 bool_t
1475968Snw141292 validate_rules(idmap_update_batch *batch)
1485968Snw141292 {
1495968Snw141292 	idmap_update_op	*up;
1505968Snw141292 	int i;
1515968Snw141292 
1525968Snw141292 	for (i = 0; i < batch->idmap_update_batch_len; i++) {
1535968Snw141292 		up = &(batch->idmap_update_batch_val[i]);
1545968Snw141292 		if (validate_rule(&(up->idmap_update_op_u.rule))
1555968Snw141292 		    != IDMAP_SUCCESS)
1565968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1575968Snw141292 	}
1585968Snw141292 
1595968Snw141292 	return (IDMAP_SUCCESS);
1605968Snw141292 }
1615968Snw141292 
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 */
3536017Snw141292 	if (sql_exec_no_cb(cache, IDMAP_CACHENAME, "BEGIN TRANSACTION;")
3546017Snw141292 	    != IDMAP_SUCCESS)
3554520Snw141292 		goto out;
3564520Snw141292 
3574520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
3584864Sbaban 		state.curpos = i;
3594520Snw141292 		if (IS_BATCH_SID(batch, i)) {
3604520Snw141292 			(void) update_cache_sid2pid(
3615696Snw141292 			    &state,
3625696Snw141292 			    cache,
3635696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3645696Snw141292 			    &result->ids.ids_val[i]);
3654520Snw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
3665696Snw141292 		    (IS_BATCH_GID(batch, i))) {
3674520Snw141292 			(void) update_cache_pid2sid(
3685696Snw141292 			    &state,
3695696Snw141292 			    cache,
3705696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3715696Snw141292 			    &result->ids.ids_val[i]);
3724520Snw141292 		}
3734520Snw141292 	}
3744520Snw141292 
3755331Samw 	/* Commit if we have at least one successful update */
3764520Snw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
3776017Snw141292 		(void) sql_exec_no_cb(cache, IDMAP_CACHENAME,
3786017Snw141292 		    "COMMIT TRANSACTION;");
3794520Snw141292 	else
3806017Snw141292 		(void) sql_exec_no_cb(cache, IDMAP_CACHENAME,
3816017Snw141292 		    "END TRANSACTION;");
3824520Snw141292 
3834520Snw141292 out:
3845731Sbaban 	cleanup_lookup_state(&state);
3854520Snw141292 	if (IDMAP_ERROR(result->retcode)) {
3864520Snw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
3874520Snw141292 		result->ids.ids_len = 0;
3884520Snw141292 		result->ids.ids_val = NULL;
3894520Snw141292 	}
3904520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
3914520Snw141292 	return (TRUE);
3924520Snw141292 }
3934520Snw141292 
3944520Snw141292 
3954520Snw141292 /* ARGSUSED */
3965696Snw141292 static
3975696Snw141292 int
3985696Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames)
3995696Snw141292 {
4004520Snw141292 	list_cb_data_t		*cb_data;
4014520Snw141292 	char			*str;
4024520Snw141292 	idmap_mappings_res	*result;
4034520Snw141292 	idmap_retcode		retcode;
4044520Snw141292 	int			w2u, u2w;
4054520Snw141292 	char			*end;
4065696Snw141292 	static int		validated_column_names = 0;
407*6386Sjp151216 	idmap_how		*how;
408*6386Sjp151216 
409*6386Sjp151216 	cb_data = (list_cb_data_t *)parg;
4105696Snw141292 
4115696Snw141292 	if (!validated_column_names) {
4125696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
4135696Snw141292 		assert(strcmp(colnames[1], "sidprefix") == 0);
4145696Snw141292 		assert(strcmp(colnames[2], "rid") == 0);
4155696Snw141292 		assert(strcmp(colnames[3], "pid") == 0);
4165696Snw141292 		assert(strcmp(colnames[4], "w2u") == 0);
4175696Snw141292 		assert(strcmp(colnames[5], "u2w") == 0);
4185696Snw141292 		assert(strcmp(colnames[6], "windomain") == 0);
4195696Snw141292 		assert(strcmp(colnames[7], "canon_winname") == 0);
4205696Snw141292 		assert(strcmp(colnames[8], "unixname") == 0);
4215696Snw141292 		assert(strcmp(colnames[9], "is_user") == 0);
4225696Snw141292 		assert(strcmp(colnames[10], "is_wuser") == 0);
423*6386Sjp151216 		assert(strcmp(colnames[11], "map_type") == 0);
424*6386Sjp151216 		assert(strcmp(colnames[12], "map_dn") == 0);
425*6386Sjp151216 		assert(strcmp(colnames[13], "map_attr") == 0);
426*6386Sjp151216 		assert(strcmp(colnames[14], "map_value") == 0);
427*6386Sjp151216 		assert(strcmp(colnames[15], "map_windomain") == 0);
428*6386Sjp151216 		assert(strcmp(colnames[16], "map_winname") == 0);
429*6386Sjp151216 		assert(strcmp(colnames[17], "map_unixname") == 0);
430*6386Sjp151216 		assert(strcmp(colnames[18], "map_is_nt4") == 0);
4315696Snw141292 		validated_column_names = 1;
4325696Snw141292 	}
4335696Snw141292 
4344520Snw141292 	result = (idmap_mappings_res *)cb_data->result;
4354520Snw141292 
436*6386Sjp151216 	_VALIDATE_LIST_CB_DATA(19, &result->mappings.mappings_val,
4375696Snw141292 	    sizeof (idmap_mapping));
4384520Snw141292 
4394520Snw141292 	result->mappings.mappings_len++;
4404520Snw141292 
4414520Snw141292 	if ((str = strdup(argv[1])) == NULL)
4424520Snw141292 		return (1);
4434520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
4445696Snw141292 	    str;
4454520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
4465696Snw141292 	    strtoul(argv[2], &end, 10);
4475696Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype =
4485696Snw141292 	    strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID;
4494520Snw141292 
4504520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
4515696Snw141292 	    strtoul(argv[3], &end, 10);
4525696Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype =
4535696Snw141292 	    strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID;
4544520Snw141292 
4555696Snw141292 	w2u = argv[4] ? strtol(argv[4], &end, 10) : 0;
4565696Snw141292 	u2w = argv[5] ? strtol(argv[5], &end, 10) : 0;
4574520Snw141292 
4584520Snw141292 	if (w2u > 0 && u2w == 0)
4594644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4604644Sbaban 		    IDMAP_DIRECTION_W2U;
4614520Snw141292 	else if (w2u == 0 && u2w > 0)
4624644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4634644Sbaban 		    IDMAP_DIRECTION_U2W;
4644520Snw141292 	else
4654644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4664644Sbaban 		    IDMAP_DIRECTION_BI;
4674520Snw141292 
4685064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain,
4695064Sdm199847 	    argv[6]);
4704520Snw141292 
4715064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name,
4725064Sdm199847 	    argv[7]);
4734520Snw141292 
4745064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name,
4755064Sdm199847 	    argv[8]);
4765064Sdm199847 
477*6386Sjp151216 	if (cb_data->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
478*6386Sjp151216 		how = &result->mappings.mappings_val[cb_data->next].info.how;
479*6386Sjp151216 		how->map_type = strtoul(argv[11], &end, 10);
480*6386Sjp151216 		switch (how->map_type) {
481*6386Sjp151216 		case IDMAP_MAP_TYPE_DS_AD:
482*6386Sjp151216 			how->idmap_how_u.ad.dn =
483*6386Sjp151216 			    strdup(argv[12]);
484*6386Sjp151216 			how->idmap_how_u.ad.attr =
485*6386Sjp151216 			    strdup(argv[13]);
486*6386Sjp151216 			how->idmap_how_u.ad.value =
487*6386Sjp151216 			    strdup(argv[14]);
488*6386Sjp151216 			break;
489*6386Sjp151216 
490*6386Sjp151216 		case IDMAP_MAP_TYPE_DS_NLDAP:
491*6386Sjp151216 			how->idmap_how_u.nldap.dn =
492*6386Sjp151216 			    strdup(argv[12]);
493*6386Sjp151216 			how->idmap_how_u.nldap.attr =
494*6386Sjp151216 			    strdup(argv[13]);
495*6386Sjp151216 			how->idmap_how_u.nldap.value =
496*6386Sjp151216 			    strdup(argv[14]);
497*6386Sjp151216 			break;
498*6386Sjp151216 
499*6386Sjp151216 		case IDMAP_MAP_TYPE_RULE_BASED:
500*6386Sjp151216 			how->idmap_how_u.rule.windomain =
501*6386Sjp151216 			    strdup(argv[15]);
502*6386Sjp151216 			how->idmap_how_u.rule.winname =
503*6386Sjp151216 			    strdup(argv[16]);
504*6386Sjp151216 			how->idmap_how_u.rule.unixname =
505*6386Sjp151216 			    strdup(argv[17]);
506*6386Sjp151216 			how->idmap_how_u.rule.is_nt4 =
507*6386Sjp151216 			    strtoul(argv[18], &end, 10);
508*6386Sjp151216 			how->idmap_how_u.rule.is_user =
509*6386Sjp151216 			    strtol(argv[9], &end, 10);
510*6386Sjp151216 			how->idmap_how_u.rule.is_wuser =
511*6386Sjp151216 			    strtol(argv[10], &end, 10);
512*6386Sjp151216 			break;
513*6386Sjp151216 
514*6386Sjp151216 		case IDMAP_MAP_TYPE_EPHEMERAL:
515*6386Sjp151216 			break;
516*6386Sjp151216 
517*6386Sjp151216 		case IDMAP_MAP_TYPE_LOCAL_SID:
518*6386Sjp151216 			break;
519*6386Sjp151216 
520*6386Sjp151216 		default:
521*6386Sjp151216 			/* Unknow mapping type */
522*6386Sjp151216 			assert(FALSE);
523*6386Sjp151216 		}
524*6386Sjp151216 
525*6386Sjp151216 	}
5264520Snw141292 
5274520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
5284520Snw141292 	cb_data->next++;
5294520Snw141292 	result->retcode = IDMAP_SUCCESS;
5304520Snw141292 	return (0);
5314520Snw141292 }
5324520Snw141292 
5334520Snw141292 
5344520Snw141292 /* ARGSUSED */
5354520Snw141292 bool_t
536*6386Sjp151216 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit, int32_t flag,
5375696Snw141292     idmap_mappings_res *result, struct svc_req *rqstp)
5385696Snw141292 {
5394520Snw141292 	sqlite		*cache = NULL;
5404520Snw141292 	char		lbuf[30], rbuf[30];
5414520Snw141292 	uint64_t	maxlimit;
5424520Snw141292 	idmap_retcode	retcode;
5434520Snw141292 	char		*sql = NULL;
544*6386Sjp151216 	time_t		curtime;
5454520Snw141292 
5464520Snw141292 	(void) memset(result, 0, sizeof (*result));
5474520Snw141292 	lbuf[0] = rbuf[0] = 0;
5484520Snw141292 
549*6386Sjp151216 	/* Current time */
550*6386Sjp151216 	errno = 0;
551*6386Sjp151216 	if ((curtime = time(NULL)) == (time_t)-1) {
552*6386Sjp151216 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
553*6386Sjp151216 		    strerror(errno));
554*6386Sjp151216 		retcode = IDMAP_ERR_INTERNAL;
555*6386Sjp151216 		goto out;
556*6386Sjp151216 	}
557*6386Sjp151216 
5584520Snw141292 	RDLOCK_CONFIG();
5594520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
5604520Snw141292 	UNLOCK_CONFIG();
5614520Snw141292 
5624520Snw141292 	/* Get cache handle */
5634520Snw141292 	result->retcode = get_cache_handle(&cache);
5644520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
5654520Snw141292 		goto out;
5664520Snw141292 
5674520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
5684520Snw141292 
5694520Snw141292 	/* Create LIMIT expression. */
5704520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
5714520Snw141292 		limit = maxlimit;
5724520Snw141292 	if (limit > 0)
5734520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
5745696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
5754520Snw141292 
5764520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
5774520Snw141292 
5784520Snw141292 	/*
5794520Snw141292 	 * Combine all the above into a giant SELECT statement that
5804520Snw141292 	 * will return the requested mappings
5814520Snw141292 	 */
582*6386Sjp151216 
583*6386Sjp151216 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, "
584*6386Sjp151216 	    "u2w, windomain, canon_winname, unixname, is_user, is_wuser, "
585*6386Sjp151216 	    "map_type, map_dn, map_attr, map_value, map_windomain, "
586*6386Sjp151216 	    "map_winname, map_unixname, map_is_nt4 "
587*6386Sjp151216 	    "FROM idmap_cache WHERE %s AND "
588*6386Sjp151216 	    "(pid >= 2147483648 OR (expiration = 0 OR "
589*6386Sjp151216 	    "expiration ISNULL  OR expiration > %d)) "
590*6386Sjp151216 	    "%s;",
591*6386Sjp151216 	    rbuf, curtime, lbuf);
5924520Snw141292 	if (sql == NULL) {
5934520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
5944520Snw141292 		goto out;
5954520Snw141292 	}
5964520Snw141292 
5974520Snw141292 	/* Execute the SQL statement and update the return buffer */
5986017Snw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, IDMAP_CACHENAME, sql, limit,
599*6386Sjp151216 	    flag, list_mappings_cb, result, result->mappings.mappings_len);
6004520Snw141292 
6014520Snw141292 out:
6024520Snw141292 	if (sql)
6034520Snw141292 		sqlite_freemem(sql);
6044520Snw141292 	if (IDMAP_ERROR(result->retcode))
6054520Snw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
6064520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
6074520Snw141292 	return (TRUE);
6084520Snw141292 }
6094520Snw141292 
6104520Snw141292 
6114520Snw141292 /* ARGSUSED */
6125696Snw141292 static
6135696Snw141292 int
6145696Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames)
6155696Snw141292 {
6164520Snw141292 	list_cb_data_t		*cb_data;
6174520Snw141292 	idmap_namerules_res	*result;
6184520Snw141292 	idmap_retcode		retcode;
6194520Snw141292 	int			w2u_order, u2w_order;
6204520Snw141292 	char			*end;
6215696Snw141292 	static int		validated_column_names = 0;
6225696Snw141292 
6235696Snw141292 	if (!validated_column_names) {
6245696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
6255696Snw141292 		assert(strcmp(colnames[1], "is_user") == 0);
6265696Snw141292 		assert(strcmp(colnames[2], "is_wuser") == 0);
6275696Snw141292 		assert(strcmp(colnames[3], "windomain") == 0);
6285696Snw141292 		assert(strcmp(colnames[4], "winname_display") == 0);
6295696Snw141292 		assert(strcmp(colnames[5], "is_nt4") == 0);
6305696Snw141292 		assert(strcmp(colnames[6], "unixname") == 0);
6315696Snw141292 		assert(strcmp(colnames[7], "w2u_order") == 0);
6325696Snw141292 		assert(strcmp(colnames[8], "u2w_order") == 0);
6335696Snw141292 		validated_column_names = 1;
6345696Snw141292 	}
6354520Snw141292 
6364520Snw141292 	cb_data = (list_cb_data_t *)parg;
6374520Snw141292 	result = (idmap_namerules_res *)cb_data->result;
6384520Snw141292 
6395696Snw141292 	_VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val,
6405696Snw141292 	    sizeof (idmap_namerule));
6414520Snw141292 
6424520Snw141292 	result->rules.rules_len++;
6434520Snw141292 
6444520Snw141292 	result->rules.rules_val[cb_data->next].is_user =
6455696Snw141292 	    strtol(argv[1], &end, 10);
6465696Snw141292 
6475696Snw141292 	result->rules.rules_val[cb_data->next].is_wuser =
6485696Snw141292 	    strtol(argv[2], &end, 10);
6494520Snw141292 
6505064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain,
6515064Sdm199847 	    argv[3]);
6524520Snw141292 
6535696Snw141292 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname,
6545696Snw141292 	    argv[4]);
6555696Snw141292 
6564520Snw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
6575696Snw141292 	    strtol(argv[5], &end, 10);
6584520Snw141292 
6595064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname,
6605696Snw141292 	    argv[6]);
6614520Snw141292 
6625696Snw141292 	w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0;
6635696Snw141292 	u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0;
6644520Snw141292 
6654520Snw141292 	if (w2u_order > 0 && u2w_order == 0)
6664644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6674644Sbaban 		    IDMAP_DIRECTION_W2U;
6684520Snw141292 	else if (w2u_order == 0 && u2w_order > 0)
6694644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6704644Sbaban 		    IDMAP_DIRECTION_U2W;
6714520Snw141292 	else
6724644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6734644Sbaban 		    IDMAP_DIRECTION_BI;
6744520Snw141292 
6754520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
6764520Snw141292 	cb_data->next++;
6774520Snw141292 	result->retcode = IDMAP_SUCCESS;
6784520Snw141292 	return (0);
6794520Snw141292 }
6804520Snw141292 
6814520Snw141292 
6824520Snw141292 /* ARGSUSED */
6834520Snw141292 bool_t
6844520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
6854520Snw141292 		uint64_t limit, idmap_namerules_res *result,
6865696Snw141292 		struct svc_req *rqstp)
6875696Snw141292 {
6884520Snw141292 
6894520Snw141292 	sqlite		*db = NULL;
6904520Snw141292 	char		w2ubuf[15], u2wbuf[15];
6914520Snw141292 	char		lbuf[30], rbuf[30];
6924520Snw141292 	char		*sql = NULL;
6935696Snw141292 	char		*expr = NULL;
6944520Snw141292 	uint64_t	maxlimit;
6954520Snw141292 	idmap_retcode	retcode;
6964520Snw141292 
6974520Snw141292 	(void) memset(result, 0, sizeof (*result));
6984520Snw141292 	lbuf[0] = rbuf[0] = 0;
6994520Snw141292 
7005968Snw141292 	result->retcode = validate_rule(&rule);
7015968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7025968Snw141292 		goto out;
7035968Snw141292 
7044520Snw141292 	RDLOCK_CONFIG();
7054520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
7064520Snw141292 	UNLOCK_CONFIG();
7074520Snw141292 
7084520Snw141292 	/* Get db handle */
7094520Snw141292 	result->retcode = get_db_handle(&db);
7104520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7114520Snw141292 		goto out;
7124520Snw141292 
7134520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
7144520Snw141292 
7155696Snw141292 	w2ubuf[0] = u2wbuf[0] = 0;
7165696Snw141292 	if (rule.direction == IDMAP_DIRECTION_BI) {
7174520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
7184520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
7194644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
7204520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
7214520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
7225696Snw141292 		    "AND (u2w_order = 0 OR u2w_order ISNULL)");
7234644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
7244520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
7255696Snw141292 		    "AND (w2u_order = 0 OR w2u_order ISNULL)");
7264520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
7274520Snw141292 	}
7284520Snw141292 
7295968Snw141292 	result->retcode = gen_sql_expr_from_rule(&rule, &expr);
7305968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7315696Snw141292 		goto out;
7324520Snw141292 
7334520Snw141292 	/* Create LIMIT expression. */
7344520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
7354520Snw141292 		limit = maxlimit;
7364520Snw141292 	if (limit > 0)
7374520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
7385696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
7394520Snw141292 
7404520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
7414520Snw141292 
7424520Snw141292 	/*
7434520Snw141292 	 * Combine all the above into a giant SELECT statement that
7444520Snw141292 	 * will return the requested rules
7454520Snw141292 	 */
7465696Snw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, "
7475696Snw141292 	    "winname_display, is_nt4, unixname, w2u_order, u2w_order "
7485696Snw141292 	    "FROM namerules WHERE "
7495696Snw141292 	    " %s %s %s %s %s;",
7505696Snw141292 	    rbuf, expr, w2ubuf, u2wbuf, lbuf);
7515696Snw141292 
7524520Snw141292 	if (sql == NULL) {
7534520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
7544520Snw141292 		goto out;
7554520Snw141292 	}
7564520Snw141292 
7574520Snw141292 	/* Execute the SQL statement and update the return buffer */
7586017Snw141292 	PROCESS_LIST_SVC_SQL(retcode, db, IDMAP_DBNAME, sql, limit,
759*6386Sjp151216 	    0, list_namerules_cb, result, result->rules.rules_len);
7604520Snw141292 
7614520Snw141292 out:
7625696Snw141292 	if (expr)
7635696Snw141292 		sqlite_freemem(expr);
7644520Snw141292 	if (sql)
7654520Snw141292 		sqlite_freemem(sql);
7664520Snw141292 	if (IDMAP_ERROR(result->retcode))
7674520Snw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
7684520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
7694520Snw141292 	return (TRUE);
7704520Snw141292 }
7714520Snw141292 
7724520Snw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
7734520Snw141292 static int
7745696Snw141292 verify_rules_auth(struct svc_req *rqstp)
7755696Snw141292 {
7764520Snw141292 	ucred_t		*uc = NULL;
7774520Snw141292 	uid_t		uid;
7784520Snw141292 	char		buf[1024];
7794520Snw141292 	struct passwd	pwd;
7804520Snw141292 
7814520Snw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
7826017Snw141292 		idmapdlog(LOG_ERR, "svc_getcallerucred failed during "
7836017Snw141292 		    "authorization (%s)", strerror(errno));
7844520Snw141292 		return (-1);
7854520Snw141292 	}
7864520Snw141292 
7874520Snw141292 	uid = ucred_geteuid(uc);
7884520Snw141292 	if (uid == (uid_t)-1) {
7896017Snw141292 		idmapdlog(LOG_ERR, "ucred_geteuid failed during "
7906017Snw141292 		    "authorization (%s)", strerror(errno));
7914520Snw141292 		ucred_free(uc);
7924520Snw141292 		return (-1);
7934520Snw141292 	}
7944520Snw141292 
7954520Snw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
7966017Snw141292 		idmapdlog(LOG_ERR, "getpwuid_r(%u) failed during "
7976017Snw141292 		    "authorization (%s)", uid, strerror(errno));
7984520Snw141292 		ucred_free(uc);
7994520Snw141292 		return (-1);
8004520Snw141292 	}
8014520Snw141292 
8024520Snw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
8036017Snw141292 		idmapdlog(LOG_INFO, "%s is not authorized (%s)",
8046017Snw141292 		    pwd.pw_name, IDMAP_RULES_AUTH);
8054520Snw141292 		ucred_free(uc);
8064520Snw141292 		return (-1);
8074520Snw141292 	}
8084520Snw141292 
8094520Snw141292 	ucred_free(uc);
8104520Snw141292 	return (1);
8114520Snw141292 }
8124520Snw141292 
8135064Sdm199847 /*
8145064Sdm199847  * Meaning of the return values is the following: For retcode ==
8155064Sdm199847  * IDMAP_SUCCESS, everything went OK and error_index is
8165064Sdm199847  * undefined. Otherwise, error_index >=0 shows the failed batch
8175064Sdm199847  * element. errro_index == -1 indicates failure at the beginning,
8185064Sdm199847  * error_index == -2 at the end.
8195064Sdm199847  */
8205064Sdm199847 
8214520Snw141292 /* ARGSUSED */
8224520Snw141292 bool_t
8235064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res,
8245696Snw141292 		struct svc_req *rqstp)
8255696Snw141292 {
8264520Snw141292 	sqlite		*db = NULL;
8274520Snw141292 	idmap_update_op	*up;
8284520Snw141292 	int		i;
8294884Sjp151216 	int		trans = FALSE;
8304520Snw141292 
8315064Sdm199847 	res->error_index = -1;
8325064Sdm199847 	(void) memset(&res->error_rule, 0, sizeof (res->error_rule));
8335064Sdm199847 	(void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule));
8345064Sdm199847 
8354520Snw141292 	if (verify_rules_auth(rqstp) < 0) {
8365064Sdm199847 		res->retcode = IDMAP_ERR_PERMISSION_DENIED;
8374520Snw141292 		goto out;
8384520Snw141292 	}
8394520Snw141292 
8404520Snw141292 	if (batch.idmap_update_batch_len == 0 ||
8415696Snw141292 	    batch.idmap_update_batch_val == NULL) {
8425064Sdm199847 		res->retcode = IDMAP_SUCCESS;
8434520Snw141292 		goto out;
8444520Snw141292 	}
8454520Snw141292 
8465968Snw141292 	res->retcode = validate_rules(&batch);
8475968Snw141292 	if (res->retcode != IDMAP_SUCCESS)
8485968Snw141292 		goto out;
8495968Snw141292 
8504520Snw141292 	/* Get db handle */
8515064Sdm199847 	res->retcode = get_db_handle(&db);
8525064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
8534520Snw141292 		goto out;
8544520Snw141292 
8556017Snw141292 	res->retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "BEGIN TRANSACTION;");
8565064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
8574520Snw141292 		goto out;
8584884Sjp151216 	trans = TRUE;
8594520Snw141292 
8604520Snw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
8614520Snw141292 		up = &batch.idmap_update_batch_val[i];
8624520Snw141292 		switch (up->opnum) {
8634520Snw141292 		case OP_NONE:
8645064Sdm199847 			res->retcode = IDMAP_SUCCESS;
8654520Snw141292 			break;
8664520Snw141292 		case OP_ADD_NAMERULE:
8675064Sdm199847 			res->retcode = add_namerule(db,
8685696Snw141292 			    &up->idmap_update_op_u.rule);
8694520Snw141292 			break;
8704520Snw141292 		case OP_RM_NAMERULE:
8715064Sdm199847 			res->retcode = rm_namerule(db,
8725696Snw141292 			    &up->idmap_update_op_u.rule);
8734520Snw141292 			break;
8744520Snw141292 		case OP_FLUSH_NAMERULES:
8755696Snw141292 			res->retcode = flush_namerules(db);
8764520Snw141292 			break;
8774520Snw141292 		default:
8785064Sdm199847 			res->retcode = IDMAP_ERR_NOTSUPPORTED;
8795064Sdm199847 			break;
8804520Snw141292 		};
8814520Snw141292 
8825064Sdm199847 		if (res->retcode != IDMAP_SUCCESS) {
8835064Sdm199847 			res->error_index = i;
8845064Sdm199847 			if (up->opnum == OP_ADD_NAMERULE ||
8855064Sdm199847 			    up->opnum == OP_RM_NAMERULE) {
8865064Sdm199847 				idmap_stat r2 =
8875064Sdm199847 				    idmap_namerule_cpy(&res->error_rule,
8885696Snw141292 				    &up->idmap_update_op_u.rule);
8895064Sdm199847 				if (r2 != IDMAP_SUCCESS)
8905064Sdm199847 					res->retcode = r2;
8915064Sdm199847 			}
8924520Snw141292 			goto out;
8935064Sdm199847 		}
8944520Snw141292 	}
8954520Snw141292 
8964520Snw141292 out:
8974884Sjp151216 	if (trans) {
8985064Sdm199847 		if (res->retcode == IDMAP_SUCCESS) {
8995064Sdm199847 			res->retcode =
9006017Snw141292 			    sql_exec_no_cb(db, IDMAP_DBNAME,
9016017Snw141292 			    "COMMIT TRANSACTION;");
9025064Sdm199847 			if (res->retcode !=  IDMAP_SUCCESS)
9035064Sdm199847 				res->error_index = -2;
9045064Sdm199847 		}
9054884Sjp151216 		else
9066017Snw141292 			(void) sql_exec_no_cb(db, IDMAP_DBNAME,
9076017Snw141292 			    "ROLLBACK TRANSACTION;");
9084520Snw141292 	}
9095064Sdm199847 
9105064Sdm199847 	res->retcode = idmap_stat4prot(res->retcode);
9115064Sdm199847 
9124520Snw141292 	return (TRUE);
9134520Snw141292 }
9144520Snw141292 
9154520Snw141292 
9164520Snw141292 /* ARGSUSED */
9174520Snw141292 bool_t
9184520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
9195696Snw141292 		idmap_mappings_res *result, struct svc_req *rqstp)
9205696Snw141292 {
9214520Snw141292 	sqlite		*cache = NULL, *db = NULL;
9224520Snw141292 
9234520Snw141292 	/* Init */
9244520Snw141292 	(void) memset(result, 0, sizeof (*result));
9254520Snw141292 
9265968Snw141292 	result->retcode = validate_mapped_id_by_name_req(&request);
9275968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9285968Snw141292 		goto out;
9295968Snw141292 
9304520Snw141292 	/* Get cache handle */
9314520Snw141292 	result->retcode = get_cache_handle(&cache);
9324520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9334520Snw141292 		goto out;
9344520Snw141292 
9354520Snw141292 	/* Get db handle */
9364520Snw141292 	result->retcode = get_db_handle(&db);
9374520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9384520Snw141292 		goto out;
9394520Snw141292 
9404520Snw141292 	/* Allocate result */
9414520Snw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
9424520Snw141292 	if (result->mappings.mappings_val == NULL) {
9434520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
9444520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
9454520Snw141292 		goto out;
9464520Snw141292 	}
9474520Snw141292 	result->mappings.mappings_len = 1;
9484520Snw141292 
9495696Snw141292 
9505696Snw141292 	if (IS_REQUEST_SID(request, 1)) {
9514520Snw141292 		result->retcode = get_w2u_mapping(
9525696Snw141292 		    cache,
9535696Snw141292 		    db,
9545696Snw141292 		    &request,
9555696Snw141292 		    result->mappings.mappings_val);
9564520Snw141292 	} else if (IS_REQUEST_UID(request)) {
9574520Snw141292 		result->retcode = get_u2w_mapping(
9585696Snw141292 		    cache,
9595696Snw141292 		    db,
9605696Snw141292 		    &request,
9615696Snw141292 		    result->mappings.mappings_val,
9625696Snw141292 		    1);
9634520Snw141292 	} else if (IS_REQUEST_GID(request)) {
9644520Snw141292 		result->retcode = get_u2w_mapping(
9655696Snw141292 		    cache,
9665696Snw141292 		    db,
9675696Snw141292 		    &request,
9685696Snw141292 		    result->mappings.mappings_val,
9695696Snw141292 		    0);
9704520Snw141292 	} else {
9714520Snw141292 		result->retcode = IDMAP_ERR_IDTYPE;
9724520Snw141292 	}
9734520Snw141292 
9744520Snw141292 out:
9754520Snw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
9764520Snw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
9774520Snw141292 		result->mappings.mappings_len = 0;
9784520Snw141292 		result->mappings.mappings_val = NULL;
9794520Snw141292 	}
9804520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
9814520Snw141292 	return (TRUE);
9824520Snw141292 }
9834520Snw141292 
9844520Snw141292 
9854520Snw141292 /* ARGSUSED */
9864520Snw141292 int
9874520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
9885696Snw141292 		caddr_t result)
9895696Snw141292 {
9904520Snw141292 	(void) xdr_free(xdr_result, result);
9914520Snw141292 	return (TRUE);
9924520Snw141292 }
993