14520Snw141292 /*
24520Snw141292  * CDDL HEADER START
34520Snw141292  *
44520Snw141292  * The contents of this file are subject to the terms of the
54520Snw141292  * Common Development and Distribution License (the "License").
64520Snw141292  * You may not use this file except in compliance with the License.
74520Snw141292  *
84520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292  * or http://www.opensolaris.org/os/licensing.
104520Snw141292  * See the License for the specific language governing permissions
114520Snw141292  * and limitations under the License.
124520Snw141292  *
134520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292  *
194520Snw141292  * CDDL HEADER END
204520Snw141292  */
214520Snw141292 /*
225968Snw141292  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234520Snw141292  * Use is subject to license terms.
244520Snw141292  */
254520Snw141292 
264520Snw141292 
274520Snw141292 /*
284520Snw141292  * Service routines
294520Snw141292  */
304520Snw141292 
314520Snw141292 #include "idmapd.h"
324520Snw141292 #include "idmap_priv.h"
335731Sbaban #include "nldaputils.h"
344520Snw141292 #include <signal.h>
354520Snw141292 #include <thread.h>
364520Snw141292 #include <string.h>
374520Snw141292 #include <strings.h>
384520Snw141292 #include <errno.h>
394520Snw141292 #include <assert.h>
404520Snw141292 #include <sys/types.h>
414520Snw141292 #include <sys/stat.h>
424520Snw141292 #include <ucred.h>
434520Snw141292 #include <pwd.h>
444520Snw141292 #include <auth_attr.h>
454520Snw141292 #include <secdb.h>
465968Snw141292 #include <sys/u8_textprep.h>
474520Snw141292 
484520Snw141292 #define	_VALIDATE_LIST_CB_DATA(col, val, siz)\
494520Snw141292 	retcode = validate_list_cb_data(cb_data, argc, argv, col,\
504520Snw141292 			(uchar_t **)val, siz);\
514520Snw141292 	if (retcode == IDMAP_NEXT) {\
524520Snw141292 		result->retcode = IDMAP_NEXT;\
534520Snw141292 		return (0);\
544520Snw141292 	} else if (retcode < 0) {\
554520Snw141292 		result->retcode = retcode;\
564520Snw141292 		return (1);\
574520Snw141292 	}
584520Snw141292 
596386Sjp151216 #define	PROCESS_LIST_SVC_SQL(rcode, db, dbname, sql, limit, flag, cb, res, len)\
606386Sjp151216 	rcode = process_list_svc_sql(db, dbname, sql, limit, flag, cb, res);\
614520Snw141292 	if (rcode == IDMAP_ERR_BUSY)\
624520Snw141292 		res->retcode = IDMAP_ERR_BUSY;\
634520Snw141292 	else if (rcode == IDMAP_SUCCESS && len == 0)\
644520Snw141292 		res->retcode = IDMAP_ERR_NOTFOUND;
654520Snw141292 
664520Snw141292 
675064Sdm199847 #define	STRDUP_OR_FAIL(to, from) \
685064Sdm199847 	if ((from) == NULL) \
695064Sdm199847 		to = NULL; \
705064Sdm199847 	else { \
715064Sdm199847 		if ((to = strdup(from)) == NULL) \
725064Sdm199847 			return (1); \
735064Sdm199847 	}
745064Sdm199847 
756616Sdm199847 #define	STRDUP_CHECK(to, from) \
766616Sdm199847 	if ((from) != NULL) { \
776616Sdm199847 		to = strdup(from); \
786616Sdm199847 		if (to == NULL) { \
796616Sdm199847 			result->retcode = IDMAP_ERR_MEMORY; \
806616Sdm199847 			goto out; \
816616Sdm199847 		} \
826616Sdm199847 	}
836616Sdm199847 
844520Snw141292 /* ARGSUSED */
854520Snw141292 bool_t
865696Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp)
875696Snw141292 {
884520Snw141292 	return (TRUE);
894520Snw141292 }
904520Snw141292 
915474Sbaban /*
925474Sbaban  * RPC layer allocates empty strings to replace NULL char *.
935474Sbaban  * This utility function frees these empty strings.
945474Sbaban  */
955731Sbaban static
965731Sbaban void
975474Sbaban sanitize_mapping_request(idmap_mapping *req)
985474Sbaban {
995474Sbaban 	free(req->id1name);
1005474Sbaban 	req->id1name = NULL;
1015474Sbaban 	free(req->id1domain);
1025474Sbaban 	req->id1domain = NULL;
1035474Sbaban 	free(req->id2name);
1045474Sbaban 	req->id2name = NULL;
1055474Sbaban 	free(req->id2domain);
1065474Sbaban 	req->id2domain = NULL;
1075731Sbaban 	req->direction = _IDMAP_F_DONE;
1085474Sbaban }
1095474Sbaban 
1105968Snw141292 static
1115968Snw141292 int
1125968Snw141292 validate_mapped_id_by_name_req(idmap_mapping *req)
1135968Snw141292 {
1145968Snw141292 	int e;
1155968Snw141292 
1165968Snw141292 	if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req))
1175968Snw141292 		return (IDMAP_SUCCESS);
1185968Snw141292 
1195968Snw141292 	if (IS_REQUEST_SID(*req, 1)) {
1205968Snw141292 		if (!EMPTY_STRING(req->id1name) &&
1215968Snw141292 		    u8_validate(req->id1name, strlen(req->id1name),
1225968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1235968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1245968Snw141292 		if (!EMPTY_STRING(req->id1domain) &&
1255968Snw141292 		    u8_validate(req->id1domain, strlen(req->id1domain),
1265968Snw141292 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1275968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1285968Snw141292 	}
1295968Snw141292 
1305968Snw141292 	return (IDMAP_SUCCESS);
1315968Snw141292 }
1325968Snw141292 
1335968Snw141292 static
1345968Snw141292 int
1355968Snw141292 validate_rule(idmap_namerule *rule)
1365968Snw141292 {
1375968Snw141292 	int e;
1385968Snw141292 
1395968Snw141292 	if (!EMPTY_STRING(rule->winname) &&
1405968Snw141292 	    u8_validate(rule->winname, strlen(rule->winname),
1415968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1425968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
1435968Snw141292 
1445968Snw141292 	if (!EMPTY_STRING(rule->windomain) &&
1455968Snw141292 	    u8_validate(rule->windomain, strlen(rule->windomain),
1465968Snw141292 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
1475968Snw141292 		return (IDMAP_ERR_BAD_UTF8);
1485968Snw141292 
1495968Snw141292 	return (IDMAP_SUCCESS);
1505968Snw141292 
1515968Snw141292 }
1525968Snw141292 
1535968Snw141292 static
1545968Snw141292 bool_t
1555968Snw141292 validate_rules(idmap_update_batch *batch)
1565968Snw141292 {
1575968Snw141292 	idmap_update_op	*up;
1585968Snw141292 	int i;
1595968Snw141292 
1605968Snw141292 	for (i = 0; i < batch->idmap_update_batch_len; i++) {
1615968Snw141292 		up = &(batch->idmap_update_batch_val[i]);
1625968Snw141292 		if (validate_rule(&(up->idmap_update_op_u.rule))
1635968Snw141292 		    != IDMAP_SUCCESS)
1645968Snw141292 			return (IDMAP_ERR_BAD_UTF8);
1655968Snw141292 	}
1665968Snw141292 
1675968Snw141292 	return (IDMAP_SUCCESS);
1685968Snw141292 }
1695968Snw141292 
1704520Snw141292 /* ARGSUSED */
1714520Snw141292 bool_t
1724520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
1735696Snw141292 		idmap_ids_res *result, struct svc_req *rqstp)
1745696Snw141292 {
1754520Snw141292 	sqlite		*cache = NULL, *db = NULL;
1764520Snw141292 	lookup_state_t	state;
1775731Sbaban 	idmap_retcode	retcode;
1784864Sbaban 	uint_t		i;
1794520Snw141292 
1804520Snw141292 	/* Init */
1814520Snw141292 	(void) memset(result, 0, sizeof (*result));
1824520Snw141292 	(void) memset(&state, 0, sizeof (state));
1834520Snw141292 
1844520Snw141292 	/* Return success if nothing was requested */
1854520Snw141292 	if (batch.idmap_mapping_batch_len < 1)
1864520Snw141292 		goto out;
1874520Snw141292 
1884520Snw141292 	/* Get cache handle */
1894520Snw141292 	result->retcode = get_cache_handle(&cache);
1904520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1914520Snw141292 		goto out;
1926616Sdm199847 	state.cache = cache;
1934520Snw141292 
1944520Snw141292 	/* Get db handle */
1954520Snw141292 	result->retcode = get_db_handle(&db);
1964520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1974520Snw141292 		goto out;
1986616Sdm199847 	state.db = db;
1994520Snw141292 
2004520Snw141292 	/* Allocate result array */
2014520Snw141292 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
2025696Snw141292 	    sizeof (idmap_id_res));
2034520Snw141292 	if (result->ids.ids_val == NULL) {
2044520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
2054520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
2064520Snw141292 		goto out;
2074520Snw141292 	}
2084520Snw141292 	result->ids.ids_len = batch.idmap_mapping_batch_len;
2094520Snw141292 
2104864Sbaban 	/* Allocate hash table to check for duplicate sids */
2114864Sbaban 	state.sid_history = calloc(batch.idmap_mapping_batch_len,
2125696Snw141292 	    sizeof (*state.sid_history));
2134864Sbaban 	if (state.sid_history == NULL) {
2144864Sbaban 		idmapdlog(LOG_ERR, "Out of memory");
2154864Sbaban 		result->retcode = IDMAP_ERR_MEMORY;
2164864Sbaban 		goto out;
2174864Sbaban 	}
2184864Sbaban 	state.sid_history_size = batch.idmap_mapping_batch_len;
2194864Sbaban 	for (i = 0; i < state.sid_history_size; i++) {
2204864Sbaban 		state.sid_history[i].key = state.sid_history_size;
2214864Sbaban 		state.sid_history[i].next = state.sid_history_size;
2224864Sbaban 	}
2234864Sbaban 	state.batch = &batch;
2244864Sbaban 	state.result = result;
2254864Sbaban 
2265731Sbaban 	/* Get directory-based name mapping info */
2276616Sdm199847 	result->retcode = load_cfg_in_state(&state);
2285731Sbaban 	if (result->retcode != IDMAP_SUCCESS)
2295731Sbaban 		goto out;
2305731Sbaban 
2314520Snw141292 	/* Init our 'done' flags */
2324520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
2334520Snw141292 
2344520Snw141292 	/* First stage */
2354520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
2364864Sbaban 		state.curpos = i;
2375474Sbaban 		(void) sanitize_mapping_request(
2385474Sbaban 		    &batch.idmap_mapping_batch_val[i]);
2394520Snw141292 		if (IS_BATCH_SID(batch, i)) {
2404520Snw141292 			retcode = sid2pid_first_pass(
2415696Snw141292 			    &state,
2425696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2435696Snw141292 			    &result->ids.ids_val[i]);
2444520Snw141292 		} else if (IS_BATCH_UID(batch, i)) {
2454520Snw141292 			retcode = pid2sid_first_pass(
2465696Snw141292 			    &state,
2475696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2485696Snw141292 			    &result->ids.ids_val[i], 1, 0);
2494520Snw141292 		} else if (IS_BATCH_GID(batch, i)) {
2504520Snw141292 			retcode = pid2sid_first_pass(
2515696Snw141292 			    &state,
2525696Snw141292 			    &batch.idmap_mapping_batch_val[i],
2535696Snw141292 			    &result->ids.ids_val[i], 0, 0);
2544520Snw141292 		} else {
2554520Snw141292 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
2564520Snw141292 			continue;
2574520Snw141292 		}
2584520Snw141292 		if (IDMAP_FATAL_ERROR(retcode)) {
2594520Snw141292 			result->retcode = retcode;
2604520Snw141292 			goto out;
2614520Snw141292 		}
2624520Snw141292 	}
2634520Snw141292 
2644520Snw141292 	/* Check if we are done */
2654520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
2664520Snw141292 		goto out;
2674520Snw141292 
2685731Sbaban 	/*
2695731Sbaban 	 * native LDAP lookups:
2706616Sdm199847 	 *  pid2sid:
2716616Sdm199847 	 *	- nldap or mixed mode. Lookup nldap by pid or unixname to get
2726616Sdm199847 	 *	  winname.
2736616Sdm199847 	 *  sid2pid:
2746616Sdm199847 	 *	- nldap mode. Got winname and sid (either given or found in
2756616Sdm199847 	 *	  name_cache). Lookup nldap by winname to get pid and
2766616Sdm199847 	 *	  unixname.
2775731Sbaban 	 */
2785731Sbaban 	if (state.nldap_nqueries) {
2795731Sbaban 		retcode = nldap_lookup_batch(&state, &batch, result);
2805731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
2815731Sbaban 			result->retcode = retcode;
2824520Snw141292 			goto out;
2834520Snw141292 		}
2845731Sbaban 	}
2854520Snw141292 
2865731Sbaban 	/*
2875731Sbaban 	 * AD lookups:
2886616Sdm199847 	 *  pid2sid:
2896616Sdm199847 	 *	- nldap or mixed mode. Got winname from nldap lookup.
2906616Sdm199847 	 *	  winname2sid could not be resolved locally. Lookup AD
2916616Sdm199847 	 *	  by winname to get sid.
2926616Sdm199847 	 *	- ad mode. Got unixname. Lookup AD by unixname to get
2936616Sdm199847 	 *	  winname and sid.
2946616Sdm199847 	 *  sid2pid:
2956616Sdm199847 	 *	- ad or mixed mode. Lookup AD by sid or winname to get
2966616Sdm199847 	 *	  winname, sid and unixname.
2976616Sdm199847 	 *	- any mode. Got either sid or winname but not both. Lookup
2986616Sdm199847 	 *	  AD by sid or winname to get winname, sid.
2995731Sbaban 	 */
3005731Sbaban 	if (state.ad_nqueries) {
3015731Sbaban 		retcode = ad_lookup_batch(&state, &batch, result);
3025731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
3035731Sbaban 			result->retcode = retcode;
3045731Sbaban 			goto out;
3055731Sbaban 		}
3065731Sbaban 	}
3075731Sbaban 
3085731Sbaban 	/*
3095731Sbaban 	 * native LDAP lookups:
3106616Sdm199847 	 *  sid2pid:
3116616Sdm199847 	 *	- nldap mode. Got winname and sid from AD lookup. Lookup nldap
3126616Sdm199847 	 *	  by winname to get pid and unixname.
3135731Sbaban 	 */
3145731Sbaban 	if (state.nldap_nqueries) {
3155731Sbaban 		retcode = nldap_lookup_batch(&state, &batch, result);
3165731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
3175731Sbaban 			result->retcode = retcode;
3185731Sbaban 			goto out;
3195731Sbaban 		}
3205731Sbaban 	}
3215731Sbaban 
3225731Sbaban 	/* Reset 'done' flags */
3235731Sbaban 	state.sid2pid_done = state.pid2sid_done = TRUE;
3244520Snw141292 
3254520Snw141292 	/* Second stage */
3264520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
3274864Sbaban 		state.curpos = i;
3284520Snw141292 		if (IS_BATCH_SID(batch, i)) {
3294520Snw141292 			retcode = sid2pid_second_pass(
3305696Snw141292 			    &state,
3315696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3325696Snw141292 			    &result->ids.ids_val[i]);
3335731Sbaban 		} else if (IS_BATCH_UID(batch, i)) {
3345731Sbaban 			retcode = pid2sid_second_pass(
3355731Sbaban 			    &state,
3365731Sbaban 			    &batch.idmap_mapping_batch_val[i],
3375731Sbaban 			    &result->ids.ids_val[i], 1);
3385731Sbaban 		} else if (IS_BATCH_GID(batch, i)) {
3395731Sbaban 			retcode = pid2sid_second_pass(
3405731Sbaban 			    &state,
3415731Sbaban 			    &batch.idmap_mapping_batch_val[i],
3425731Sbaban 			    &result->ids.ids_val[i], 0);
3435731Sbaban 		} else {
3445731Sbaban 			/* First stage has already set the error */
3455731Sbaban 			continue;
3465731Sbaban 		}
3475731Sbaban 		if (IDMAP_FATAL_ERROR(retcode)) {
3485731Sbaban 			result->retcode = retcode;
3495731Sbaban 			goto out;
3504520Snw141292 		}
3514520Snw141292 	}
3524520Snw141292 
3534520Snw141292 	/* Check if we are done */
3544520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
3554520Snw141292 		goto out;
3564520Snw141292 
3574520Snw141292 	/* Reset our 'done' flags */
3584520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
3594520Snw141292 
3604520Snw141292 	/* Update cache in a single transaction */
3616017Snw141292 	if (sql_exec_no_cb(cache, IDMAP_CACHENAME, "BEGIN TRANSACTION;")
3626017Snw141292 	    != IDMAP_SUCCESS)
3634520Snw141292 		goto out;
3644520Snw141292 
3654520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
3664864Sbaban 		state.curpos = i;
3674520Snw141292 		if (IS_BATCH_SID(batch, i)) {
3684520Snw141292 			(void) update_cache_sid2pid(
3695696Snw141292 			    &state,
3705696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3715696Snw141292 			    &result->ids.ids_val[i]);
3724520Snw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
3735696Snw141292 		    (IS_BATCH_GID(batch, i))) {
3744520Snw141292 			(void) update_cache_pid2sid(
3755696Snw141292 			    &state,
3765696Snw141292 			    &batch.idmap_mapping_batch_val[i],
3775696Snw141292 			    &result->ids.ids_val[i]);
3784520Snw141292 		}
3794520Snw141292 	}
3804520Snw141292 
3815331Samw 	/* Commit if we have at least one successful update */
3824520Snw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
3836017Snw141292 		(void) sql_exec_no_cb(cache, IDMAP_CACHENAME,
3846017Snw141292 		    "COMMIT TRANSACTION;");
3854520Snw141292 	else
3866017Snw141292 		(void) sql_exec_no_cb(cache, IDMAP_CACHENAME,
3876017Snw141292 		    "END TRANSACTION;");
3884520Snw141292 
3894520Snw141292 out:
3905731Sbaban 	cleanup_lookup_state(&state);
3914520Snw141292 	if (IDMAP_ERROR(result->retcode)) {
3924520Snw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
3934520Snw141292 		result->ids.ids_len = 0;
3944520Snw141292 		result->ids.ids_val = NULL;
3954520Snw141292 	}
3964520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
3974520Snw141292 	return (TRUE);
3984520Snw141292 }
3994520Snw141292 
4004520Snw141292 
4014520Snw141292 /* ARGSUSED */
4025696Snw141292 static
4035696Snw141292 int
4045696Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames)
4055696Snw141292 {
4064520Snw141292 	list_cb_data_t		*cb_data;
4074520Snw141292 	char			*str;
4084520Snw141292 	idmap_mappings_res	*result;
4094520Snw141292 	idmap_retcode		retcode;
4104520Snw141292 	int			w2u, u2w;
4114520Snw141292 	char			*end;
4125696Snw141292 	static int		validated_column_names = 0;
4136386Sjp151216 	idmap_how		*how;
4146386Sjp151216 
4156386Sjp151216 	cb_data = (list_cb_data_t *)parg;
4165696Snw141292 
4175696Snw141292 	if (!validated_column_names) {
4185696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
4195696Snw141292 		assert(strcmp(colnames[1], "sidprefix") == 0);
4205696Snw141292 		assert(strcmp(colnames[2], "rid") == 0);
4215696Snw141292 		assert(strcmp(colnames[3], "pid") == 0);
4225696Snw141292 		assert(strcmp(colnames[4], "w2u") == 0);
4235696Snw141292 		assert(strcmp(colnames[5], "u2w") == 0);
4245696Snw141292 		assert(strcmp(colnames[6], "windomain") == 0);
4255696Snw141292 		assert(strcmp(colnames[7], "canon_winname") == 0);
4265696Snw141292 		assert(strcmp(colnames[8], "unixname") == 0);
4275696Snw141292 		assert(strcmp(colnames[9], "is_user") == 0);
4285696Snw141292 		assert(strcmp(colnames[10], "is_wuser") == 0);
4296386Sjp151216 		assert(strcmp(colnames[11], "map_type") == 0);
4306386Sjp151216 		assert(strcmp(colnames[12], "map_dn") == 0);
4316386Sjp151216 		assert(strcmp(colnames[13], "map_attr") == 0);
4326386Sjp151216 		assert(strcmp(colnames[14], "map_value") == 0);
4336386Sjp151216 		assert(strcmp(colnames[15], "map_windomain") == 0);
4346386Sjp151216 		assert(strcmp(colnames[16], "map_winname") == 0);
4356386Sjp151216 		assert(strcmp(colnames[17], "map_unixname") == 0);
4366386Sjp151216 		assert(strcmp(colnames[18], "map_is_nt4") == 0);
4375696Snw141292 		validated_column_names = 1;
4385696Snw141292 	}
4395696Snw141292 
4404520Snw141292 	result = (idmap_mappings_res *)cb_data->result;
4414520Snw141292 
4426386Sjp151216 	_VALIDATE_LIST_CB_DATA(19, &result->mappings.mappings_val,
4435696Snw141292 	    sizeof (idmap_mapping));
4444520Snw141292 
4454520Snw141292 	result->mappings.mappings_len++;
4464520Snw141292 
4474520Snw141292 	if ((str = strdup(argv[1])) == NULL)
4484520Snw141292 		return (1);
4494520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
4505696Snw141292 	    str;
4514520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
4525696Snw141292 	    strtoul(argv[2], &end, 10);
4535696Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype =
4545696Snw141292 	    strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID;
4554520Snw141292 
4564520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
4575696Snw141292 	    strtoul(argv[3], &end, 10);
4585696Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype =
4595696Snw141292 	    strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID;
4604520Snw141292 
4615696Snw141292 	w2u = argv[4] ? strtol(argv[4], &end, 10) : 0;
4625696Snw141292 	u2w = argv[5] ? strtol(argv[5], &end, 10) : 0;
4634520Snw141292 
4644520Snw141292 	if (w2u > 0 && u2w == 0)
4654644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4664644Sbaban 		    IDMAP_DIRECTION_W2U;
4674520Snw141292 	else if (w2u == 0 && u2w > 0)
4684644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4694644Sbaban 		    IDMAP_DIRECTION_U2W;
4704520Snw141292 	else
4714644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
4724644Sbaban 		    IDMAP_DIRECTION_BI;
4734520Snw141292 
4745064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain,
4755064Sdm199847 	    argv[6]);
4764520Snw141292 
4775064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name,
4785064Sdm199847 	    argv[7]);
4794520Snw141292 
4805064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name,
4815064Sdm199847 	    argv[8]);
4825064Sdm199847 
4836386Sjp151216 	if (cb_data->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
4846386Sjp151216 		how = &result->mappings.mappings_val[cb_data->next].info.how;
4856386Sjp151216 		how->map_type = strtoul(argv[11], &end, 10);
4866386Sjp151216 		switch (how->map_type) {
4876386Sjp151216 		case IDMAP_MAP_TYPE_DS_AD:
4886386Sjp151216 			how->idmap_how_u.ad.dn =
4896386Sjp151216 			    strdup(argv[12]);
4906386Sjp151216 			how->idmap_how_u.ad.attr =
4916386Sjp151216 			    strdup(argv[13]);
4926386Sjp151216 			how->idmap_how_u.ad.value =
4936386Sjp151216 			    strdup(argv[14]);
4946386Sjp151216 			break;
4956386Sjp151216 
4966386Sjp151216 		case IDMAP_MAP_TYPE_DS_NLDAP:
4976386Sjp151216 			how->idmap_how_u.nldap.dn =
4986386Sjp151216 			    strdup(argv[12]);
4996386Sjp151216 			how->idmap_how_u.nldap.attr =
5006386Sjp151216 			    strdup(argv[13]);
5016386Sjp151216 			how->idmap_how_u.nldap.value =
5026386Sjp151216 			    strdup(argv[14]);
5036386Sjp151216 			break;
5046386Sjp151216 
5056386Sjp151216 		case IDMAP_MAP_TYPE_RULE_BASED:
5066386Sjp151216 			how->idmap_how_u.rule.windomain =
5076386Sjp151216 			    strdup(argv[15]);
5086386Sjp151216 			how->idmap_how_u.rule.winname =
5096386Sjp151216 			    strdup(argv[16]);
5106386Sjp151216 			how->idmap_how_u.rule.unixname =
5116386Sjp151216 			    strdup(argv[17]);
5126386Sjp151216 			how->idmap_how_u.rule.is_nt4 =
5136386Sjp151216 			    strtoul(argv[18], &end, 10);
5146386Sjp151216 			how->idmap_how_u.rule.is_user =
5156386Sjp151216 			    strtol(argv[9], &end, 10);
5166386Sjp151216 			how->idmap_how_u.rule.is_wuser =
5176386Sjp151216 			    strtol(argv[10], &end, 10);
5186386Sjp151216 			break;
5196386Sjp151216 
5206386Sjp151216 		case IDMAP_MAP_TYPE_EPHEMERAL:
5216386Sjp151216 			break;
5226386Sjp151216 
5236386Sjp151216 		case IDMAP_MAP_TYPE_LOCAL_SID:
5246386Sjp151216 			break;
5256386Sjp151216 
5266386Sjp151216 		default:
5276386Sjp151216 			/* Unknow mapping type */
5286386Sjp151216 			assert(FALSE);
5296386Sjp151216 		}
5306386Sjp151216 
5316386Sjp151216 	}
5324520Snw141292 
5334520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
5344520Snw141292 	cb_data->next++;
5354520Snw141292 	result->retcode = IDMAP_SUCCESS;
5364520Snw141292 	return (0);
5374520Snw141292 }
5384520Snw141292 
5394520Snw141292 
5404520Snw141292 /* ARGSUSED */
5414520Snw141292 bool_t
5426386Sjp151216 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit, int32_t flag,
5435696Snw141292     idmap_mappings_res *result, struct svc_req *rqstp)
5445696Snw141292 {
5454520Snw141292 	sqlite		*cache = NULL;
5464520Snw141292 	char		lbuf[30], rbuf[30];
5474520Snw141292 	uint64_t	maxlimit;
5484520Snw141292 	idmap_retcode	retcode;
5494520Snw141292 	char		*sql = NULL;
5506386Sjp151216 	time_t		curtime;
5514520Snw141292 
5524520Snw141292 	(void) memset(result, 0, sizeof (*result));
5534520Snw141292 	lbuf[0] = rbuf[0] = 0;
5544520Snw141292 
5556386Sjp151216 	/* Current time */
5566386Sjp151216 	errno = 0;
5576386Sjp151216 	if ((curtime = time(NULL)) == (time_t)-1) {
5586386Sjp151216 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
5596386Sjp151216 		    strerror(errno));
5606386Sjp151216 		retcode = IDMAP_ERR_INTERNAL;
5616386Sjp151216 		goto out;
5626386Sjp151216 	}
5636386Sjp151216 
5644520Snw141292 	RDLOCK_CONFIG();
5654520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
5664520Snw141292 	UNLOCK_CONFIG();
5674520Snw141292 
5684520Snw141292 	/* Get cache handle */
5694520Snw141292 	result->retcode = get_cache_handle(&cache);
5704520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
5714520Snw141292 		goto out;
5724520Snw141292 
5734520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
5744520Snw141292 
5754520Snw141292 	/* Create LIMIT expression. */
5764520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
5774520Snw141292 		limit = maxlimit;
5784520Snw141292 	if (limit > 0)
5794520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
5805696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
5814520Snw141292 
5824520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
5834520Snw141292 
5844520Snw141292 	/*
5854520Snw141292 	 * Combine all the above into a giant SELECT statement that
5864520Snw141292 	 * will return the requested mappings
5874520Snw141292 	 */
5886386Sjp151216 
5896386Sjp151216 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, "
5906386Sjp151216 	    "u2w, windomain, canon_winname, unixname, is_user, is_wuser, "
5916386Sjp151216 	    "map_type, map_dn, map_attr, map_value, map_windomain, "
5926386Sjp151216 	    "map_winname, map_unixname, map_is_nt4 "
5936386Sjp151216 	    "FROM idmap_cache WHERE %s AND "
5946386Sjp151216 	    "(pid >= 2147483648 OR (expiration = 0 OR "
5956386Sjp151216 	    "expiration ISNULL  OR expiration > %d)) "
5966386Sjp151216 	    "%s;",
5976386Sjp151216 	    rbuf, curtime, lbuf);
5984520Snw141292 	if (sql == NULL) {
5996616Sdm199847 		result->retcode = IDMAP_ERR_MEMORY;
6004520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
6014520Snw141292 		goto out;
6024520Snw141292 	}
6034520Snw141292 
6044520Snw141292 	/* Execute the SQL statement and update the return buffer */
6056017Snw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, IDMAP_CACHENAME, sql, limit,
6066386Sjp151216 	    flag, list_mappings_cb, result, result->mappings.mappings_len);
6074520Snw141292 
6084520Snw141292 out:
6094520Snw141292 	if (sql)
6104520Snw141292 		sqlite_freemem(sql);
6114520Snw141292 	if (IDMAP_ERROR(result->retcode))
6124520Snw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
6134520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
6144520Snw141292 	return (TRUE);
6154520Snw141292 }
6164520Snw141292 
6174520Snw141292 
6184520Snw141292 /* ARGSUSED */
6195696Snw141292 static
6205696Snw141292 int
6215696Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames)
6225696Snw141292 {
6234520Snw141292 	list_cb_data_t		*cb_data;
6244520Snw141292 	idmap_namerules_res	*result;
6254520Snw141292 	idmap_retcode		retcode;
6264520Snw141292 	int			w2u_order, u2w_order;
6274520Snw141292 	char			*end;
6285696Snw141292 	static int		validated_column_names = 0;
6295696Snw141292 
6305696Snw141292 	if (!validated_column_names) {
6315696Snw141292 		assert(strcmp(colnames[0], "rowid") == 0);
6325696Snw141292 		assert(strcmp(colnames[1], "is_user") == 0);
6335696Snw141292 		assert(strcmp(colnames[2], "is_wuser") == 0);
6345696Snw141292 		assert(strcmp(colnames[3], "windomain") == 0);
6355696Snw141292 		assert(strcmp(colnames[4], "winname_display") == 0);
6365696Snw141292 		assert(strcmp(colnames[5], "is_nt4") == 0);
6375696Snw141292 		assert(strcmp(colnames[6], "unixname") == 0);
6385696Snw141292 		assert(strcmp(colnames[7], "w2u_order") == 0);
6395696Snw141292 		assert(strcmp(colnames[8], "u2w_order") == 0);
6405696Snw141292 		validated_column_names = 1;
6415696Snw141292 	}
6424520Snw141292 
6434520Snw141292 	cb_data = (list_cb_data_t *)parg;
6444520Snw141292 	result = (idmap_namerules_res *)cb_data->result;
6454520Snw141292 
6465696Snw141292 	_VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val,
6475696Snw141292 	    sizeof (idmap_namerule));
6484520Snw141292 
6494520Snw141292 	result->rules.rules_len++;
6504520Snw141292 
6514520Snw141292 	result->rules.rules_val[cb_data->next].is_user =
6525696Snw141292 	    strtol(argv[1], &end, 10);
6535696Snw141292 
6545696Snw141292 	result->rules.rules_val[cb_data->next].is_wuser =
6555696Snw141292 	    strtol(argv[2], &end, 10);
6564520Snw141292 
6575064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain,
6585064Sdm199847 	    argv[3]);
6594520Snw141292 
6605696Snw141292 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname,
6615696Snw141292 	    argv[4]);
6625696Snw141292 
6634520Snw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
6645696Snw141292 	    strtol(argv[5], &end, 10);
6654520Snw141292 
6665064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname,
6675696Snw141292 	    argv[6]);
6684520Snw141292 
6695696Snw141292 	w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0;
6705696Snw141292 	u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0;
6714520Snw141292 
6724520Snw141292 	if (w2u_order > 0 && u2w_order == 0)
6734644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6744644Sbaban 		    IDMAP_DIRECTION_W2U;
6754520Snw141292 	else if (w2u_order == 0 && u2w_order > 0)
6764644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6774644Sbaban 		    IDMAP_DIRECTION_U2W;
6784520Snw141292 	else
6794644Sbaban 		result->rules.rules_val[cb_data->next].direction =
6804644Sbaban 		    IDMAP_DIRECTION_BI;
6814520Snw141292 
6824520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
6834520Snw141292 	cb_data->next++;
6844520Snw141292 	result->retcode = IDMAP_SUCCESS;
6854520Snw141292 	return (0);
6864520Snw141292 }
6874520Snw141292 
6884520Snw141292 
6894520Snw141292 /* ARGSUSED */
6904520Snw141292 bool_t
6914520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
6924520Snw141292 		uint64_t limit, idmap_namerules_res *result,
6935696Snw141292 		struct svc_req *rqstp)
6945696Snw141292 {
6954520Snw141292 
6964520Snw141292 	sqlite		*db = NULL;
6974520Snw141292 	char		w2ubuf[15], u2wbuf[15];
6984520Snw141292 	char		lbuf[30], rbuf[30];
6994520Snw141292 	char		*sql = NULL;
7005696Snw141292 	char		*expr = NULL;
7014520Snw141292 	uint64_t	maxlimit;
7024520Snw141292 	idmap_retcode	retcode;
7034520Snw141292 
7044520Snw141292 	(void) memset(result, 0, sizeof (*result));
7054520Snw141292 	lbuf[0] = rbuf[0] = 0;
7064520Snw141292 
7075968Snw141292 	result->retcode = validate_rule(&rule);
7085968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7095968Snw141292 		goto out;
7105968Snw141292 
7114520Snw141292 	RDLOCK_CONFIG();
7124520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
7134520Snw141292 	UNLOCK_CONFIG();
7144520Snw141292 
7154520Snw141292 	/* Get db handle */
7164520Snw141292 	result->retcode = get_db_handle(&db);
7174520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7184520Snw141292 		goto out;
7194520Snw141292 
7204520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
7214520Snw141292 
7225696Snw141292 	w2ubuf[0] = u2wbuf[0] = 0;
7235696Snw141292 	if (rule.direction == IDMAP_DIRECTION_BI) {
7244520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
7254520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
7264644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
7274520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
7284520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
7295696Snw141292 		    "AND (u2w_order = 0 OR u2w_order ISNULL)");
7304644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
7314520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
7325696Snw141292 		    "AND (w2u_order = 0 OR w2u_order ISNULL)");
7334520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
7344520Snw141292 	}
7354520Snw141292 
7365968Snw141292 	result->retcode = gen_sql_expr_from_rule(&rule, &expr);
7375968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7385696Snw141292 		goto out;
7394520Snw141292 
7404520Snw141292 	/* Create LIMIT expression. */
7414520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
7424520Snw141292 		limit = maxlimit;
7434520Snw141292 	if (limit > 0)
7444520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
7455696Snw141292 		    "LIMIT %" PRIu64, limit + 1ULL);
7464520Snw141292 
7474520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
7484520Snw141292 
7494520Snw141292 	/*
7504520Snw141292 	 * Combine all the above into a giant SELECT statement that
7514520Snw141292 	 * will return the requested rules
7524520Snw141292 	 */
7535696Snw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, "
7545696Snw141292 	    "winname_display, is_nt4, unixname, w2u_order, u2w_order "
7555696Snw141292 	    "FROM namerules WHERE "
7565696Snw141292 	    " %s %s %s %s %s;",
7575696Snw141292 	    rbuf, expr, w2ubuf, u2wbuf, lbuf);
7585696Snw141292 
7594520Snw141292 	if (sql == NULL) {
7606616Sdm199847 		result->retcode = IDMAP_ERR_MEMORY;
7614520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
7624520Snw141292 		goto out;
7634520Snw141292 	}
7644520Snw141292 
7654520Snw141292 	/* Execute the SQL statement and update the return buffer */
7666017Snw141292 	PROCESS_LIST_SVC_SQL(retcode, db, IDMAP_DBNAME, sql, limit,
7676386Sjp151216 	    0, list_namerules_cb, result, result->rules.rules_len);
7684520Snw141292 
7694520Snw141292 out:
7705696Snw141292 	if (expr)
7715696Snw141292 		sqlite_freemem(expr);
7724520Snw141292 	if (sql)
7734520Snw141292 		sqlite_freemem(sql);
7744520Snw141292 	if (IDMAP_ERROR(result->retcode))
7754520Snw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
7764520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
7774520Snw141292 	return (TRUE);
7784520Snw141292 }
7794520Snw141292 
7804520Snw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
7814520Snw141292 static int
7825696Snw141292 verify_rules_auth(struct svc_req *rqstp)
7835696Snw141292 {
7844520Snw141292 	ucred_t		*uc = NULL;
7854520Snw141292 	uid_t		uid;
7864520Snw141292 	char		buf[1024];
7874520Snw141292 	struct passwd	pwd;
7884520Snw141292 
7894520Snw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
7906017Snw141292 		idmapdlog(LOG_ERR, "svc_getcallerucred failed during "
7916017Snw141292 		    "authorization (%s)", strerror(errno));
7924520Snw141292 		return (-1);
7934520Snw141292 	}
7944520Snw141292 
7954520Snw141292 	uid = ucred_geteuid(uc);
7964520Snw141292 	if (uid == (uid_t)-1) {
7976017Snw141292 		idmapdlog(LOG_ERR, "ucred_geteuid failed during "
7986017Snw141292 		    "authorization (%s)", strerror(errno));
7994520Snw141292 		ucred_free(uc);
8004520Snw141292 		return (-1);
8014520Snw141292 	}
8024520Snw141292 
8034520Snw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
8046017Snw141292 		idmapdlog(LOG_ERR, "getpwuid_r(%u) failed during "
8056017Snw141292 		    "authorization (%s)", uid, strerror(errno));
8064520Snw141292 		ucred_free(uc);
8074520Snw141292 		return (-1);
8084520Snw141292 	}
8094520Snw141292 
8104520Snw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
8116017Snw141292 		idmapdlog(LOG_INFO, "%s is not authorized (%s)",
8126017Snw141292 		    pwd.pw_name, IDMAP_RULES_AUTH);
8134520Snw141292 		ucred_free(uc);
8144520Snw141292 		return (-1);
8154520Snw141292 	}
8164520Snw141292 
8174520Snw141292 	ucred_free(uc);
8184520Snw141292 	return (1);
8194520Snw141292 }
8204520Snw141292 
8215064Sdm199847 /*
8225064Sdm199847  * Meaning of the return values is the following: For retcode ==
8235064Sdm199847  * IDMAP_SUCCESS, everything went OK and error_index is
8245064Sdm199847  * undefined. Otherwise, error_index >=0 shows the failed batch
8255064Sdm199847  * element. errro_index == -1 indicates failure at the beginning,
8265064Sdm199847  * error_index == -2 at the end.
8275064Sdm199847  */
8285064Sdm199847 
8294520Snw141292 /* ARGSUSED */
8304520Snw141292 bool_t
8315064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res,
8325696Snw141292 		struct svc_req *rqstp)
8335696Snw141292 {
8344520Snw141292 	sqlite		*db = NULL;
8354520Snw141292 	idmap_update_op	*up;
8364520Snw141292 	int		i;
8374884Sjp151216 	int		trans = FALSE;
8384520Snw141292 
8395064Sdm199847 	res->error_index = -1;
8405064Sdm199847 	(void) memset(&res->error_rule, 0, sizeof (res->error_rule));
8415064Sdm199847 	(void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule));
8425064Sdm199847 
8434520Snw141292 	if (verify_rules_auth(rqstp) < 0) {
8445064Sdm199847 		res->retcode = IDMAP_ERR_PERMISSION_DENIED;
8454520Snw141292 		goto out;
8464520Snw141292 	}
8474520Snw141292 
8484520Snw141292 	if (batch.idmap_update_batch_len == 0 ||
8495696Snw141292 	    batch.idmap_update_batch_val == NULL) {
8505064Sdm199847 		res->retcode = IDMAP_SUCCESS;
8514520Snw141292 		goto out;
8524520Snw141292 	}
8534520Snw141292 
8545968Snw141292 	res->retcode = validate_rules(&batch);
8555968Snw141292 	if (res->retcode != IDMAP_SUCCESS)
8565968Snw141292 		goto out;
8575968Snw141292 
8584520Snw141292 	/* Get db handle */
8595064Sdm199847 	res->retcode = get_db_handle(&db);
8605064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
8614520Snw141292 		goto out;
8624520Snw141292 
8636017Snw141292 	res->retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "BEGIN TRANSACTION;");
8645064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
8654520Snw141292 		goto out;
8664884Sjp151216 	trans = TRUE;
8674520Snw141292 
8684520Snw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
8694520Snw141292 		up = &batch.idmap_update_batch_val[i];
8704520Snw141292 		switch (up->opnum) {
8714520Snw141292 		case OP_NONE:
8725064Sdm199847 			res->retcode = IDMAP_SUCCESS;
8734520Snw141292 			break;
8744520Snw141292 		case OP_ADD_NAMERULE:
8755064Sdm199847 			res->retcode = add_namerule(db,
8765696Snw141292 			    &up->idmap_update_op_u.rule);
8774520Snw141292 			break;
8784520Snw141292 		case OP_RM_NAMERULE:
8795064Sdm199847 			res->retcode = rm_namerule(db,
8805696Snw141292 			    &up->idmap_update_op_u.rule);
8814520Snw141292 			break;
8824520Snw141292 		case OP_FLUSH_NAMERULES:
8835696Snw141292 			res->retcode = flush_namerules(db);
8844520Snw141292 			break;
8854520Snw141292 		default:
8865064Sdm199847 			res->retcode = IDMAP_ERR_NOTSUPPORTED;
8875064Sdm199847 			break;
8884520Snw141292 		};
8894520Snw141292 
8905064Sdm199847 		if (res->retcode != IDMAP_SUCCESS) {
8915064Sdm199847 			res->error_index = i;
8925064Sdm199847 			if (up->opnum == OP_ADD_NAMERULE ||
8935064Sdm199847 			    up->opnum == OP_RM_NAMERULE) {
8945064Sdm199847 				idmap_stat r2 =
8955064Sdm199847 				    idmap_namerule_cpy(&res->error_rule,
8965696Snw141292 				    &up->idmap_update_op_u.rule);
8975064Sdm199847 				if (r2 != IDMAP_SUCCESS)
8985064Sdm199847 					res->retcode = r2;
8995064Sdm199847 			}
9004520Snw141292 			goto out;
9015064Sdm199847 		}
9024520Snw141292 	}
9034520Snw141292 
9044520Snw141292 out:
9054884Sjp151216 	if (trans) {
9065064Sdm199847 		if (res->retcode == IDMAP_SUCCESS) {
9075064Sdm199847 			res->retcode =
9086017Snw141292 			    sql_exec_no_cb(db, IDMAP_DBNAME,
9096017Snw141292 			    "COMMIT TRANSACTION;");
9105064Sdm199847 			if (res->retcode !=  IDMAP_SUCCESS)
9115064Sdm199847 				res->error_index = -2;
9125064Sdm199847 		}
9134884Sjp151216 		else
9146017Snw141292 			(void) sql_exec_no_cb(db, IDMAP_DBNAME,
9156017Snw141292 			    "ROLLBACK TRANSACTION;");
9164520Snw141292 	}
9175064Sdm199847 
9185064Sdm199847 	res->retcode = idmap_stat4prot(res->retcode);
9195064Sdm199847 
9204520Snw141292 	return (TRUE);
9214520Snw141292 }
9224520Snw141292 
9234520Snw141292 
9244520Snw141292 /* ARGSUSED */
9254520Snw141292 bool_t
9264520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
9275696Snw141292 		idmap_mappings_res *result, struct svc_req *rqstp)
9285696Snw141292 {
9294520Snw141292 	sqlite		*cache = NULL, *db = NULL;
9304520Snw141292 
9314520Snw141292 	/* Init */
9324520Snw141292 	(void) memset(result, 0, sizeof (*result));
9334520Snw141292 
9345968Snw141292 	result->retcode = validate_mapped_id_by_name_req(&request);
9355968Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9365968Snw141292 		goto out;
9375968Snw141292 
9384520Snw141292 	/* Get cache handle */
9394520Snw141292 	result->retcode = get_cache_handle(&cache);
9404520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9414520Snw141292 		goto out;
9424520Snw141292 
9434520Snw141292 	/* Get db handle */
9444520Snw141292 	result->retcode = get_db_handle(&db);
9454520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
9464520Snw141292 		goto out;
9474520Snw141292 
9484520Snw141292 	/* Allocate result */
9494520Snw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
9504520Snw141292 	if (result->mappings.mappings_val == NULL) {
9514520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
9524520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
9534520Snw141292 		goto out;
9544520Snw141292 	}
9554520Snw141292 	result->mappings.mappings_len = 1;
9564520Snw141292 
9575696Snw141292 
9585696Snw141292 	if (IS_REQUEST_SID(request, 1)) {
9594520Snw141292 		result->retcode = get_w2u_mapping(
9605696Snw141292 		    cache,
9615696Snw141292 		    db,
9625696Snw141292 		    &request,
9635696Snw141292 		    result->mappings.mappings_val);
9644520Snw141292 	} else if (IS_REQUEST_UID(request)) {
9654520Snw141292 		result->retcode = get_u2w_mapping(
9665696Snw141292 		    cache,
9675696Snw141292 		    db,
9685696Snw141292 		    &request,
9695696Snw141292 		    result->mappings.mappings_val,
9705696Snw141292 		    1);
9714520Snw141292 	} else if (IS_REQUEST_GID(request)) {
9724520Snw141292 		result->retcode = get_u2w_mapping(
9735696Snw141292 		    cache,
9745696Snw141292 		    db,
9755696Snw141292 		    &request,
9765696Snw141292 		    result->mappings.mappings_val,
9775696Snw141292 		    0);
9784520Snw141292 	} else {
9794520Snw141292 		result->retcode = IDMAP_ERR_IDTYPE;
9804520Snw141292 	}
9814520Snw141292 
9824520Snw141292 out:
9834520Snw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
9844520Snw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
9854520Snw141292 		result->mappings.mappings_len = 0;
9864520Snw141292 		result->mappings.mappings_val = NULL;
9874520Snw141292 	}
9884520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
9894520Snw141292 	return (TRUE);
9904520Snw141292 }
9914520Snw141292 
9926616Sdm199847 /* ARGSUSED */
9936616Sdm199847 bool_t
9946616Sdm199847 idmap_get_prop_1_svc(idmap_prop_type request,
9956616Sdm199847 		idmap_prop_res *result, struct svc_req *rqstp)
9966616Sdm199847 {
9976616Sdm199847 	idmap_pg_config_t *pgcfg;
9986616Sdm199847 
9996616Sdm199847 	/* Init */
10006616Sdm199847 	(void) memset(result, 0, sizeof (*result));
10016616Sdm199847 	result->retcode = IDMAP_SUCCESS;
10026616Sdm199847 	result->value.prop = request;
10036616Sdm199847 
10046616Sdm199847 	RDLOCK_CONFIG();
10056616Sdm199847 
10066616Sdm199847 	/* Just shortcuts: */
10076616Sdm199847 	pgcfg = &_idmapdstate.cfg->pgcfg;
1008*8361SJulian.Pullen@Sun.COM 
10096616Sdm199847 
10106616Sdm199847 	switch (request) {
10116616Sdm199847 	case PROP_LIST_SIZE_LIMIT:
10126616Sdm199847 		result->value.idmap_prop_val_u.intval = pgcfg->list_size_limit;
10136616Sdm199847 		result->auto_discovered = FALSE;
10146616Sdm199847 		break;
10156616Sdm199847 	case PROP_DEFAULT_DOMAIN:
10166616Sdm199847 		result->auto_discovered = FALSE;
10176616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10186616Sdm199847 		    pgcfg->default_domain);
10196616Sdm199847 		break;
10206616Sdm199847 	case PROP_DOMAIN_NAME:
10216616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10226616Sdm199847 		    pgcfg->domain_name);
10236616Sdm199847 		result->auto_discovered =
1024*8361SJulian.Pullen@Sun.COM 		    pgcfg->domain_name_auto_disc;
10256616Sdm199847 		break;
10266616Sdm199847 	case PROP_MACHINE_SID:
10276616Sdm199847 		result->auto_discovered = FALSE;
10286616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10296616Sdm199847 		    pgcfg->machine_sid);
10306616Sdm199847 		break;
10316616Sdm199847 	case PROP_DOMAIN_CONTROLLER:
10326616Sdm199847 		if (pgcfg->domain_controller != NULL) {
10336616Sdm199847 			(void) memcpy(&result->value.idmap_prop_val_u.dsval,
10346616Sdm199847 			    pgcfg->domain_controller,
10356616Sdm199847 			    sizeof (idmap_ad_disc_ds_t));
10366616Sdm199847 		}
1037*8361SJulian.Pullen@Sun.COM 		result->auto_discovered = pgcfg->domain_controller_auto_disc;
10386616Sdm199847 		break;
10396616Sdm199847 	case PROP_FOREST_NAME:
10406616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10416616Sdm199847 		    pgcfg->forest_name);
1042*8361SJulian.Pullen@Sun.COM 		result->auto_discovered = pgcfg->forest_name_auto_disc;
10436616Sdm199847 		break;
10446616Sdm199847 	case PROP_SITE_NAME:
10456616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10466616Sdm199847 		    pgcfg->site_name);
1047*8361SJulian.Pullen@Sun.COM 		result->auto_discovered = pgcfg->site_name_auto_disc;
10486616Sdm199847 		break;
10496616Sdm199847 	case PROP_GLOBAL_CATALOG:
10506616Sdm199847 		if (pgcfg->global_catalog != NULL) {
10516616Sdm199847 			(void) memcpy(&result->value.idmap_prop_val_u.dsval,
10526616Sdm199847 			    pgcfg->global_catalog, sizeof (idmap_ad_disc_ds_t));
10536616Sdm199847 		}
1054*8361SJulian.Pullen@Sun.COM 		result->auto_discovered = pgcfg->global_catalog_auto_disc;
10556616Sdm199847 		break;
10566616Sdm199847 	case PROP_AD_UNIXUSER_ATTR:
10576616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10586616Sdm199847 		    pgcfg->ad_unixuser_attr);
10596616Sdm199847 		result->auto_discovered = FALSE;
10606616Sdm199847 		break;
10616616Sdm199847 	case PROP_AD_UNIXGROUP_ATTR:
10626616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10636616Sdm199847 		    pgcfg->ad_unixgroup_attr);
10646616Sdm199847 		result->auto_discovered = FALSE;
10656616Sdm199847 		break;
10666616Sdm199847 	case PROP_NLDAP_WINNAME_ATTR:
10676616Sdm199847 		STRDUP_CHECK(result->value.idmap_prop_val_u.utf8val,
10686616Sdm199847 		    pgcfg->nldap_winname_attr);
10696616Sdm199847 		result->auto_discovered = FALSE;
10706616Sdm199847 		break;
10716616Sdm199847 	case PROP_DS_NAME_MAPPING_ENABLED:
10726616Sdm199847 		result->value.idmap_prop_val_u.boolval =
10736616Sdm199847 		    pgcfg->ds_name_mapping_enabled;
10746616Sdm199847 		result->auto_discovered = FALSE;
10756616Sdm199847 		break;
10766616Sdm199847 	default:
10776616Sdm199847 		result->retcode = IDMAP_ERR_PROP_UNKNOWN;
10786616Sdm199847 		break;
10796616Sdm199847 	}
10806616Sdm199847 
10816616Sdm199847 out:
10826616Sdm199847 	UNLOCK_CONFIG();
10836616Sdm199847 	if (IDMAP_FATAL_ERROR(result->retcode)) {
10846616Sdm199847 		xdr_free(xdr_idmap_prop_res, (caddr_t)result);
10856616Sdm199847 		result->value.prop = PROP_UNKNOWN;
10866616Sdm199847 	}
10876616Sdm199847 	result->retcode = idmap_stat4prot(result->retcode);
10886616Sdm199847 	return (TRUE);
10896616Sdm199847 }
10906616Sdm199847 
10914520Snw141292 
10924520Snw141292 /* ARGSUSED */
10934520Snw141292 int
10944520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
10955696Snw141292 		caddr_t result)
10965696Snw141292 {
10974520Snw141292 	(void) xdr_free(xdr_result, result);
10984520Snw141292 	return (TRUE);
10994520Snw141292 }
1100