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 /*
224520Snw141292  * Copyright 2007 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"
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>
464520Snw141292 
474520Snw141292 #define	_VALIDATE_LIST_CB_DATA(col, val, siz)\
484520Snw141292 	retcode = validate_list_cb_data(cb_data, argc, argv, col,\
494520Snw141292 			(uchar_t **)val, siz);\
504520Snw141292 	if (retcode == IDMAP_NEXT) {\
514520Snw141292 		result->retcode = IDMAP_NEXT;\
524520Snw141292 		return (0);\
534520Snw141292 	} else if (retcode < 0) {\
544520Snw141292 		result->retcode = retcode;\
554520Snw141292 		return (1);\
564520Snw141292 	}
574520Snw141292 
584520Snw141292 #define	PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\
594520Snw141292 	rcode = process_list_svc_sql(db, sql, limit, cb, res);\
604520Snw141292 	if (rcode == IDMAP_ERR_BUSY)\
614520Snw141292 		res->retcode = IDMAP_ERR_BUSY;\
624520Snw141292 	else if (rcode == IDMAP_SUCCESS && len == 0)\
634520Snw141292 		res->retcode = IDMAP_ERR_NOTFOUND;
644520Snw141292 
654520Snw141292 
665064Sdm199847 #define	STRDUP_OR_FAIL(to, from) \
675064Sdm199847 	if ((from) == NULL) \
685064Sdm199847 		to = NULL; \
695064Sdm199847 	else { \
705064Sdm199847 		if ((to = strdup(from)) == NULL) \
715064Sdm199847 			return (1); \
725064Sdm199847 	}
735064Sdm199847 
744520Snw141292 /* ARGSUSED */
754520Snw141292 bool_t
764520Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) {
774520Snw141292 	return (TRUE);
784520Snw141292 }
794520Snw141292 
804520Snw141292 #define	IS_BATCH_SID(batch, i)\
814520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID
824520Snw141292 
834520Snw141292 #define	IS_BATCH_UID(batch, i)\
844520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID
854520Snw141292 
864520Snw141292 #define	IS_BATCH_GID(batch, i)\
874520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID
884520Snw141292 
894520Snw141292 #define	IS_REQUEST_SID(request)\
904520Snw141292 	request.id1.idtype == IDMAP_SID
914520Snw141292 
924520Snw141292 #define	IS_REQUEST_UID(request)\
934520Snw141292 	request.id1.idtype == IDMAP_UID
944520Snw141292 
954520Snw141292 #define	IS_REQUEST_GID(request)\
964520Snw141292 	request.id1.idtype == IDMAP_GID
974520Snw141292 
98*5474Sbaban /*
99*5474Sbaban  * RPC layer allocates empty strings to replace NULL char *.
100*5474Sbaban  * This utility function frees these empty strings.
101*5474Sbaban  */
102*5474Sbaban static void
103*5474Sbaban sanitize_mapping_request(idmap_mapping *req)
104*5474Sbaban {
105*5474Sbaban 	free(req->id1name);
106*5474Sbaban 	req->id1name = NULL;
107*5474Sbaban 	free(req->id1domain);
108*5474Sbaban 	req->id1domain = NULL;
109*5474Sbaban 	free(req->id2name);
110*5474Sbaban 	req->id2name = NULL;
111*5474Sbaban 	free(req->id2domain);
112*5474Sbaban 	req->id2domain = NULL;
113*5474Sbaban }
114*5474Sbaban 
1154520Snw141292 /* ARGSUSED */
1164520Snw141292 bool_t
1174520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
1184520Snw141292 		idmap_ids_res *result, struct svc_req *rqstp) {
1194520Snw141292 	sqlite		*cache = NULL, *db = NULL;
1204520Snw141292 	lookup_state_t	state;
1214520Snw141292 	idmap_retcode	retcode, winrc;
1224864Sbaban 	uint_t		i;
1234520Snw141292 
1244520Snw141292 	/* Init */
1254520Snw141292 	(void) memset(result, 0, sizeof (*result));
1264520Snw141292 	(void) memset(&state, 0, sizeof (state));
1274520Snw141292 
1284520Snw141292 	/* Return success if nothing was requested */
1294520Snw141292 	if (batch.idmap_mapping_batch_len < 1)
1304520Snw141292 		goto out;
1314520Snw141292 
1324520Snw141292 	/* Get cache handle */
1334520Snw141292 	result->retcode = get_cache_handle(&cache);
1344520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1354520Snw141292 		goto out;
1364520Snw141292 
1374520Snw141292 	/* Get db handle */
1384520Snw141292 	result->retcode = get_db_handle(&db);
1394520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1404520Snw141292 		goto out;
1414520Snw141292 
1424520Snw141292 	/* Allocate result array */
1434520Snw141292 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
1444520Snw141292 			sizeof (idmap_id_res));
1454520Snw141292 	if (result->ids.ids_val == NULL) {
1464520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
1474520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
1484520Snw141292 		goto out;
1494520Snw141292 	}
1504520Snw141292 	result->ids.ids_len = batch.idmap_mapping_batch_len;
1514520Snw141292 
1524864Sbaban 	/* Allocate hash table to check for duplicate sids */
1534864Sbaban 	state.sid_history = calloc(batch.idmap_mapping_batch_len,
1544864Sbaban 			sizeof (*state.sid_history));
1554864Sbaban 	if (state.sid_history == NULL) {
1564864Sbaban 		idmapdlog(LOG_ERR, "Out of memory");
1574864Sbaban 		result->retcode = IDMAP_ERR_MEMORY;
1584864Sbaban 		goto out;
1594864Sbaban 	}
1604864Sbaban 	state.sid_history_size = batch.idmap_mapping_batch_len;
1614864Sbaban 	for (i = 0; i < state.sid_history_size; i++) {
1624864Sbaban 		state.sid_history[i].key = state.sid_history_size;
1634864Sbaban 		state.sid_history[i].next = state.sid_history_size;
1644864Sbaban 	}
1654864Sbaban 	state.batch = &batch;
1664864Sbaban 	state.result = result;
1674864Sbaban 
1684520Snw141292 	/* Init our 'done' flags */
1694520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
1704520Snw141292 
1714520Snw141292 	/* First stage */
1724520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
1734864Sbaban 		state.curpos = i;
174*5474Sbaban 		(void) sanitize_mapping_request(
175*5474Sbaban 		    &batch.idmap_mapping_batch_val[i]);
1764520Snw141292 		if (IS_BATCH_SID(batch, i)) {
1774520Snw141292 			retcode = sid2pid_first_pass(
1784520Snw141292 				&state,
1794520Snw141292 				cache,
1804520Snw141292 				&batch.idmap_mapping_batch_val[i],
1814520Snw141292 				&result->ids.ids_val[i]);
1824520Snw141292 		} else if (IS_BATCH_UID(batch, i)) {
1834520Snw141292 			retcode = pid2sid_first_pass(
1844520Snw141292 				&state,
1854520Snw141292 				cache,
1864520Snw141292 				db,
1874520Snw141292 				&batch.idmap_mapping_batch_val[i],
1884520Snw141292 				&result->ids.ids_val[i], 1, 0);
1894520Snw141292 		} else if (IS_BATCH_GID(batch, i)) {
1904520Snw141292 			retcode = pid2sid_first_pass(
1914520Snw141292 				&state,
1924520Snw141292 				cache,
1934520Snw141292 				db,
1944520Snw141292 				&batch.idmap_mapping_batch_val[i],
1954520Snw141292 				&result->ids.ids_val[i], 0, 0);
1964520Snw141292 		} else {
1974520Snw141292 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
1984520Snw141292 			continue;
1994520Snw141292 		}
2004520Snw141292 		if (IDMAP_FATAL_ERROR(retcode)) {
2014520Snw141292 			result->retcode = retcode;
2024520Snw141292 			goto out;
2034520Snw141292 		}
2044520Snw141292 	}
2054520Snw141292 
2064520Snw141292 	/* Check if we are done */
2074520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
2084520Snw141292 		goto out;
2094520Snw141292 
2104520Snw141292 	/* Process Windows server lookups for sid2name */
2114520Snw141292 	if (state.ad_nqueries) {
2124520Snw141292 		winrc = lookup_win_batch_sid2name(&state, &batch,
2134520Snw141292 				result);
2144520Snw141292 		if (IDMAP_FATAL_ERROR(winrc)) {
2154520Snw141292 			result->retcode = winrc;
2164520Snw141292 			goto out;
2174520Snw141292 		}
2184520Snw141292 	} else
2194520Snw141292 		winrc = IDMAP_SUCCESS;
2204520Snw141292 
2214520Snw141292 	/* Reset sid2pid 'done' flag */
2224520Snw141292 	state.sid2pid_done = TRUE;
2234520Snw141292 
2244520Snw141292 	/* Second stage */
2254520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
2264864Sbaban 		state.curpos = i;
2274520Snw141292 		/* Process sid to pid ONLY */
2284520Snw141292 		if (IS_BATCH_SID(batch, i)) {
2294520Snw141292 			if (IDMAP_ERROR(winrc))
2304520Snw141292 				result->ids.ids_val[i].retcode = winrc;
2314520Snw141292 			retcode = sid2pid_second_pass(
2324520Snw141292 				&state,
2334520Snw141292 				cache,
2344520Snw141292 				db,
2354520Snw141292 				&batch.idmap_mapping_batch_val[i],
2364520Snw141292 				&result->ids.ids_val[i]);
2374520Snw141292 			if (IDMAP_FATAL_ERROR(retcode)) {
2384520Snw141292 				result->retcode = retcode;
2394520Snw141292 				goto out;
2404520Snw141292 			}
2414520Snw141292 		}
2424520Snw141292 	}
2434520Snw141292 
2444520Snw141292 	/* Check if we are done */
2454520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
2464520Snw141292 		goto out;
2474520Snw141292 
2484520Snw141292 	/* Reset our 'done' flags */
2494520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
2504520Snw141292 
2514520Snw141292 	/* Update cache in a single transaction */
2524520Snw141292 	if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS)
2534520Snw141292 		goto out;
2544520Snw141292 
2554520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
2564864Sbaban 		state.curpos = i;
2574520Snw141292 		if (IS_BATCH_SID(batch, i)) {
2584520Snw141292 			(void) update_cache_sid2pid(
2594520Snw141292 				&state,
2604520Snw141292 				cache,
2614520Snw141292 				&batch.idmap_mapping_batch_val[i],
2624520Snw141292 				&result->ids.ids_val[i]);
2634520Snw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
2644520Snw141292 				(IS_BATCH_GID(batch, i))) {
2654520Snw141292 			(void) update_cache_pid2sid(
2664520Snw141292 				&state,
2674520Snw141292 				cache,
2684520Snw141292 				&batch.idmap_mapping_batch_val[i],
2694520Snw141292 				&result->ids.ids_val[i]);
2704520Snw141292 		}
2714520Snw141292 	}
2724520Snw141292 
2735331Samw 	/* Commit if we have at least one successful update */
2744520Snw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
2754520Snw141292 		(void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;");
2764520Snw141292 	else
2774520Snw141292 		(void) sql_exec_no_cb(cache, "END TRANSACTION;");
2784520Snw141292 
2794520Snw141292 out:
2804864Sbaban 	if (state.sid_history)
2814864Sbaban 		free(state.sid_history);
2824520Snw141292 	if (IDMAP_ERROR(result->retcode)) {
2834520Snw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
2844520Snw141292 		result->ids.ids_len = 0;
2854520Snw141292 		result->ids.ids_val = NULL;
2864520Snw141292 	}
2874520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
2884520Snw141292 	return (TRUE);
2894520Snw141292 }
2904520Snw141292 
2914520Snw141292 
2924520Snw141292 /* ARGSUSED */
2934520Snw141292 static int
2944520Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) {
2954520Snw141292 	list_cb_data_t		*cb_data;
2964520Snw141292 	char			*str;
2974520Snw141292 	idmap_mappings_res	*result;
2984520Snw141292 	idmap_retcode		retcode;
2994520Snw141292 	int			w2u, u2w;
3004520Snw141292 	char			*end;
3014520Snw141292 
3024520Snw141292 	cb_data = (list_cb_data_t *)parg;
3034520Snw141292 	result = (idmap_mappings_res *)cb_data->result;
3044520Snw141292 
3054520Snw141292 	_VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val,
3064520Snw141292 		sizeof (idmap_mapping));
3074520Snw141292 
3084520Snw141292 	result->mappings.mappings_len++;
3094520Snw141292 
3104520Snw141292 	if ((str = strdup(argv[1])) == NULL)
3114520Snw141292 		return (1);
3124520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
3134520Snw141292 		str;
3144520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
3154520Snw141292 		strtoul(argv[2], &end, 10);
3164520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID;
3174520Snw141292 
3184520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
3194520Snw141292 		strtoul(argv[3], &end, 10);
3204520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID;
3214520Snw141292 
3224520Snw141292 	w2u = argv[4]?strtol(argv[4], &end, 10):0;
3234520Snw141292 	u2w = argv[5]?strtol(argv[5], &end, 10):0;
3244520Snw141292 
3254520Snw141292 	if (w2u > 0 && u2w == 0)
3264644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
3274644Sbaban 		    IDMAP_DIRECTION_W2U;
3284520Snw141292 	else if (w2u == 0 && u2w > 0)
3294644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
3304644Sbaban 		    IDMAP_DIRECTION_U2W;
3314520Snw141292 	else
3324644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
3334644Sbaban 		    IDMAP_DIRECTION_BI;
3344520Snw141292 
3355064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain,
3365064Sdm199847 	    argv[6]);
3374520Snw141292 
3385064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name,
3395064Sdm199847 	    argv[7]);
3404520Snw141292 
3415064Sdm199847 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name,
3425064Sdm199847 	    argv[8]);
3435064Sdm199847 
3444520Snw141292 
3454520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
3464520Snw141292 	cb_data->next++;
3474520Snw141292 	result->retcode = IDMAP_SUCCESS;
3484520Snw141292 	return (0);
3494520Snw141292 }
3504520Snw141292 
3514520Snw141292 
3524520Snw141292 /* ARGSUSED */
3534520Snw141292 bool_t
3544520Snw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid,
3554520Snw141292 		uint64_t limit, idmap_mappings_res *result,
3564520Snw141292 		struct svc_req *rqstp) {
3574520Snw141292 	sqlite		*cache = NULL;
3584520Snw141292 	char		lbuf[30], rbuf[30];
3594520Snw141292 	uint64_t	maxlimit;
3604520Snw141292 	idmap_retcode	retcode;
3614520Snw141292 	char		*sql = NULL;
3624520Snw141292 
3634520Snw141292 	(void) memset(result, 0, sizeof (*result));
3644520Snw141292 	lbuf[0] = rbuf[0] = 0;
3654520Snw141292 
3664520Snw141292 	RDLOCK_CONFIG();
3674520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
3684520Snw141292 	UNLOCK_CONFIG();
3694520Snw141292 
3704520Snw141292 	/* Get cache handle */
3714520Snw141292 	result->retcode = get_cache_handle(&cache);
3724520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
3734520Snw141292 		goto out;
3744520Snw141292 
3754520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
3764520Snw141292 
3774520Snw141292 	/* Create LIMIT expression. */
3784520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
3794520Snw141292 		limit = maxlimit;
3804520Snw141292 	if (limit > 0)
3814520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
3824520Snw141292 			"LIMIT %" PRIu64, limit + 1ULL);
3834520Snw141292 
3844520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
3854520Snw141292 
3864520Snw141292 	/*
3874520Snw141292 	 * Combine all the above into a giant SELECT statement that
3884520Snw141292 	 * will return the requested mappings
3894520Snw141292 	 */
3904520Snw141292 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w,"
3914520Snw141292 			" windomain, winname, unixname"
3924520Snw141292 			" FROM idmap_cache WHERE "
3934520Snw141292 			" %s AND is_user = %d %s;",
3944520Snw141292 			rbuf, is_user?1:0, lbuf);
3954520Snw141292 	if (sql == NULL) {
3964520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
3974520Snw141292 		goto out;
3984520Snw141292 	}
3994520Snw141292 
4004520Snw141292 	/* Execute the SQL statement and update the return buffer */
4014520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb,
4024520Snw141292 		result, result->mappings.mappings_len);
4034520Snw141292 
4044520Snw141292 out:
4054520Snw141292 	if (sql)
4064520Snw141292 		sqlite_freemem(sql);
4074520Snw141292 	if (IDMAP_ERROR(result->retcode))
4084520Snw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
4094520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
4104520Snw141292 	return (TRUE);
4114520Snw141292 }
4124520Snw141292 
4134520Snw141292 
4144520Snw141292 /* ARGSUSED */
4154520Snw141292 static int
4164520Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) {
4174520Snw141292 	list_cb_data_t		*cb_data;
4184520Snw141292 	idmap_namerules_res	*result;
4194520Snw141292 	idmap_retcode		retcode;
4204520Snw141292 	int			w2u_order, u2w_order;
4214520Snw141292 	char			*end;
4224520Snw141292 
4234520Snw141292 	cb_data = (list_cb_data_t *)parg;
4244520Snw141292 	result = (idmap_namerules_res *)cb_data->result;
4254520Snw141292 
4264520Snw141292 	_VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val,
4274520Snw141292 		sizeof (idmap_namerule));
4284520Snw141292 
4294520Snw141292 	result->rules.rules_len++;
4304520Snw141292 
4314520Snw141292 	result->rules.rules_val[cb_data->next].is_user =
4324520Snw141292 		strtol(argv[1], &end, 10);
4334520Snw141292 
4345064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain,
4355064Sdm199847 	    argv[2]);
4364520Snw141292 
4375064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname,
4385064Sdm199847 	    argv[3]);
4394520Snw141292 
4404520Snw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
4414520Snw141292 		strtol(argv[4], &end, 10);
4424520Snw141292 
4435064Sdm199847 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname,
4445064Sdm199847 	    argv[5]);
4454520Snw141292 
4464520Snw141292 	w2u_order = argv[6]?strtol(argv[6], &end, 10):0;
4474520Snw141292 	u2w_order = argv[7]?strtol(argv[7], &end, 10):0;
4484520Snw141292 
4494520Snw141292 	if (w2u_order > 0 && u2w_order == 0)
4504644Sbaban 		result->rules.rules_val[cb_data->next].direction =
4514644Sbaban 		    IDMAP_DIRECTION_W2U;
4524520Snw141292 	else if (w2u_order == 0 && u2w_order > 0)
4534644Sbaban 		result->rules.rules_val[cb_data->next].direction =
4544644Sbaban 		    IDMAP_DIRECTION_U2W;
4554520Snw141292 	else
4564644Sbaban 		result->rules.rules_val[cb_data->next].direction =
4574644Sbaban 		    IDMAP_DIRECTION_BI;
4584520Snw141292 
4594520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
4604520Snw141292 	cb_data->next++;
4614520Snw141292 	result->retcode = IDMAP_SUCCESS;
4624520Snw141292 	return (0);
4634520Snw141292 }
4644520Snw141292 
4654520Snw141292 
4664520Snw141292 /* ARGSUSED */
4674520Snw141292 bool_t
4684520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
4694520Snw141292 		uint64_t limit, idmap_namerules_res *result,
4704520Snw141292 		struct svc_req *rqstp) {
4714520Snw141292 
4724520Snw141292 	sqlite		*db = NULL;
4734520Snw141292 	char		w2ubuf[15], u2wbuf[15];
4744520Snw141292 	char		lbuf[30], rbuf[30];
4754520Snw141292 	char		*sql = NULL;
4764520Snw141292 	char		*s_windomain = NULL, *s_winname = NULL;
4774520Snw141292 	char		*s_unixname = NULL;
4784520Snw141292 	uint64_t	maxlimit;
4794520Snw141292 	idmap_retcode	retcode;
4804520Snw141292 
4814520Snw141292 	(void) memset(result, 0, sizeof (*result));
4824520Snw141292 	lbuf[0] = rbuf[0] = 0;
4834520Snw141292 
4844520Snw141292 	RDLOCK_CONFIG();
4854520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
4864520Snw141292 	UNLOCK_CONFIG();
4874520Snw141292 
4884520Snw141292 	/* Get db handle */
4894520Snw141292 	result->retcode = get_db_handle(&db);
4904520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
4914520Snw141292 		goto out;
4924520Snw141292 
4934520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
4944520Snw141292 
4954520Snw141292 	if (rule.direction < 0) {
4964520Snw141292 		w2ubuf[0] = u2wbuf[0] = 0;
4974644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_BI) {
4984520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
4994520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
5004644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
5014520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
5024520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
5034520Snw141292 				"AND (u2w_order = 0 OR u2w_order ISNULL)");
5044644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
5054520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
5064520Snw141292 				"AND (w2u_order = 0 OR w2u_order ISNULL)");
5074520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
5084520Snw141292 	}
5094520Snw141292 
5104520Snw141292 	/* Create where statement for windomain */
5115064Sdm199847 	if (!EMPTY_STRING(rule.windomain)) {
5124520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "windomain", "=",
5135064Sdm199847 				rule.windomain,
5144520Snw141292 				"", &s_windomain) != IDMAP_SUCCESS)
5154520Snw141292 			goto out;
5164520Snw141292 	}
5174520Snw141292 
5184520Snw141292 	/* Create where statement for winname */
5195064Sdm199847 	if (!EMPTY_STRING(rule.winname)) {
5204520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "winname", "=",
5215064Sdm199847 				rule.winname,
5224520Snw141292 				"", &s_winname) != IDMAP_SUCCESS)
5234520Snw141292 			goto out;
5244520Snw141292 	}
5254520Snw141292 
5264520Snw141292 	/* Create where statement for unixname */
5275064Sdm199847 	if (!EMPTY_STRING(rule.unixname)) {
5284520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "unixname", "=",
5295064Sdm199847 				rule.unixname,
5304520Snw141292 				"", &s_unixname) != IDMAP_SUCCESS)
5314520Snw141292 			goto out;
5324520Snw141292 	}
5334520Snw141292 
5344520Snw141292 	/* Create LIMIT expression. */
5354520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
5364520Snw141292 		limit = maxlimit;
5374520Snw141292 	if (limit > 0)
5384520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
5394520Snw141292 			"LIMIT %" PRIu64, limit + 1ULL);
5404520Snw141292 
5414520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
5424520Snw141292 
5434520Snw141292 	/*
5444520Snw141292 	 * Combine all the above into a giant SELECT statement that
5454520Snw141292 	 * will return the requested rules
5464520Snw141292 	 */
5474520Snw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, "
5484520Snw141292 			"is_nt4, unixname, w2u_order, u2w_order "
5494520Snw141292 			"FROM namerules WHERE "
5504520Snw141292 			" %s AND is_user = %d %s %s %s %s %s %s;",
5514520Snw141292 			rbuf, rule.is_user?1:0,
5524520Snw141292 			s_windomain?s_windomain:"",
5534520Snw141292 			s_winname?s_winname:"",
5544520Snw141292 			s_unixname?s_unixname:"",
5554520Snw141292 			w2ubuf, u2wbuf, lbuf);
5564520Snw141292 	if (sql == NULL) {
5574520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
5584520Snw141292 		goto out;
5594520Snw141292 	}
5604520Snw141292 
5614520Snw141292 	/* Execute the SQL statement and update the return buffer */
5624520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb,
5634520Snw141292 		result, result->rules.rules_len);
5644520Snw141292 
5654520Snw141292 out:
5664520Snw141292 	if (s_windomain)
5674520Snw141292 		sqlite_freemem(s_windomain);
5684520Snw141292 	if (s_winname)
5694520Snw141292 		sqlite_freemem(s_winname);
5704520Snw141292 	if (s_unixname)
5714520Snw141292 		sqlite_freemem(s_unixname);
5724520Snw141292 	if (sql)
5734520Snw141292 		sqlite_freemem(sql);
5744520Snw141292 	if (IDMAP_ERROR(result->retcode))
5754520Snw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
5764520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
5774520Snw141292 	return (TRUE);
5784520Snw141292 }
5794520Snw141292 
5804520Snw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
5814520Snw141292 static int
5824520Snw141292 verify_rules_auth(struct svc_req *rqstp) {
5834520Snw141292 	ucred_t		*uc = NULL;
5844520Snw141292 	uid_t		uid;
5854520Snw141292 	char		buf[1024];
5864520Snw141292 	struct passwd	pwd;
5874520Snw141292 	const char	*me = "verify_rules_auth";
5884520Snw141292 
5894520Snw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
5904520Snw141292 		idmapdlog(LOG_ERR,
5914520Snw141292 			"%s: svc_getcallerucred failed (errno=%d)",
5924520Snw141292 			me, errno);
5934520Snw141292 		return (-1);
5944520Snw141292 	}
5954520Snw141292 
5964520Snw141292 	uid = ucred_geteuid(uc);
5974520Snw141292 	if (uid == (uid_t)-1) {
5984520Snw141292 		idmapdlog(LOG_ERR,
5994520Snw141292 			"%s: ucred_geteuid failed (errno=%d)",
6004520Snw141292 			me, errno);
6014520Snw141292 		ucred_free(uc);
6024520Snw141292 		return (-1);
6034520Snw141292 	}
6044520Snw141292 
6054520Snw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
6064520Snw141292 		idmapdlog(LOG_ERR,
6074520Snw141292 			"%s: getpwuid_r(%u) failed (errno=%d)",
6084520Snw141292 			me, uid, errno);
6094520Snw141292 		ucred_free(uc);
6104520Snw141292 		return (-1);
6114520Snw141292 	}
6124520Snw141292 
6134520Snw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
6144520Snw141292 		idmapdlog(LOG_INFO,
6154520Snw141292 			"%s: %s does not have authorization.",
6164520Snw141292 			me, pwd.pw_name);
6174520Snw141292 		ucred_free(uc);
6184520Snw141292 		return (-1);
6194520Snw141292 	}
6204520Snw141292 
6214520Snw141292 	ucred_free(uc);
6224520Snw141292 	return (1);
6234520Snw141292 }
6244520Snw141292 
6255064Sdm199847 /*
6265064Sdm199847  * Meaning of the return values is the following: For retcode ==
6275064Sdm199847  * IDMAP_SUCCESS, everything went OK and error_index is
6285064Sdm199847  * undefined. Otherwise, error_index >=0 shows the failed batch
6295064Sdm199847  * element. errro_index == -1 indicates failure at the beginning,
6305064Sdm199847  * error_index == -2 at the end.
6315064Sdm199847  */
6325064Sdm199847 
6334520Snw141292 /* ARGSUSED */
6344520Snw141292 bool_t
6355064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res,
6364520Snw141292 		struct svc_req *rqstp) {
6374520Snw141292 	sqlite		*db = NULL;
6384520Snw141292 	idmap_update_op	*up;
6394520Snw141292 	int		i;
6404884Sjp151216 	int		trans = FALSE;
6414520Snw141292 
6425064Sdm199847 	res->error_index = -1;
6435064Sdm199847 	(void) memset(&res->error_rule, 0, sizeof (res->error_rule));
6445064Sdm199847 	(void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule));
6455064Sdm199847 
6464520Snw141292 	if (verify_rules_auth(rqstp) < 0) {
6475064Sdm199847 		res->retcode = IDMAP_ERR_PERMISSION_DENIED;
6484520Snw141292 		goto out;
6494520Snw141292 	}
6504520Snw141292 
6514520Snw141292 	if (batch.idmap_update_batch_len == 0 ||
6524520Snw141292 			batch.idmap_update_batch_val == NULL) {
6535064Sdm199847 		res->retcode = IDMAP_SUCCESS;
6544520Snw141292 		goto out;
6554520Snw141292 	}
6564520Snw141292 
6574520Snw141292 	/* Get db handle */
6585064Sdm199847 	res->retcode = get_db_handle(&db);
6595064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
6604520Snw141292 		goto out;
6614520Snw141292 
6625064Sdm199847 	res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;");
6635064Sdm199847 	if (res->retcode != IDMAP_SUCCESS)
6644520Snw141292 		goto out;
6654884Sjp151216 	trans = TRUE;
6664520Snw141292 
6674520Snw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
6684520Snw141292 		up = &batch.idmap_update_batch_val[i];
6694520Snw141292 		switch (up->opnum) {
6704520Snw141292 		case OP_NONE:
6715064Sdm199847 			res->retcode = IDMAP_SUCCESS;
6724520Snw141292 			break;
6734520Snw141292 		case OP_ADD_NAMERULE:
6745064Sdm199847 			res->retcode = add_namerule(db,
6754520Snw141292 				&up->idmap_update_op_u.rule);
6764520Snw141292 			break;
6774520Snw141292 		case OP_RM_NAMERULE:
6785064Sdm199847 			res->retcode = rm_namerule(db,
6794520Snw141292 				&up->idmap_update_op_u.rule);
6804520Snw141292 			break;
6814520Snw141292 		case OP_FLUSH_NAMERULES:
6825064Sdm199847 			res->retcode = flush_namerules(db,
6834520Snw141292 				up->idmap_update_op_u.is_user);
6844520Snw141292 			break;
6854520Snw141292 		default:
6865064Sdm199847 			res->retcode = IDMAP_ERR_NOTSUPPORTED;
6875064Sdm199847 			break;
6884520Snw141292 		};
6894520Snw141292 
6905064Sdm199847 		if (res->retcode != IDMAP_SUCCESS) {
6915064Sdm199847 			res->error_index = i;
6925064Sdm199847 			if (up->opnum == OP_ADD_NAMERULE ||
6935064Sdm199847 			    up->opnum == OP_RM_NAMERULE) {
6945064Sdm199847 				idmap_stat r2 =
6955064Sdm199847 				    idmap_namerule_cpy(&res->error_rule,
6965064Sdm199847 					&up->idmap_update_op_u.rule);
6975064Sdm199847 				if (r2 != IDMAP_SUCCESS)
6985064Sdm199847 					res->retcode = r2;
6995064Sdm199847 			}
7004520Snw141292 			goto out;
7015064Sdm199847 		}
7024520Snw141292 	}
7034520Snw141292 
7044520Snw141292 out:
7054884Sjp151216 	if (trans) {
7065064Sdm199847 		if (res->retcode == IDMAP_SUCCESS) {
7075064Sdm199847 			res->retcode =
7085064Sdm199847 			    sql_exec_no_cb(db, "COMMIT TRANSACTION;");
7095064Sdm199847 			if (res->retcode !=  IDMAP_SUCCESS)
7105064Sdm199847 				res->error_index = -2;
7115064Sdm199847 		}
7124884Sjp151216 		else
7134884Sjp151216 			(void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;");
7144520Snw141292 	}
7155064Sdm199847 
7165064Sdm199847 	res->retcode = idmap_stat4prot(res->retcode);
7175064Sdm199847 
7184520Snw141292 	return (TRUE);
7194520Snw141292 }
7204520Snw141292 
7214520Snw141292 
7224520Snw141292 /* ARGSUSED */
7234520Snw141292 bool_t
7244520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
7254520Snw141292 		idmap_mappings_res *result, struct svc_req *rqstp) {
7264520Snw141292 	sqlite		*cache = NULL, *db = NULL;
7274520Snw141292 
7284520Snw141292 	/* Init */
7294520Snw141292 	(void) memset(result, 0, sizeof (*result));
7304520Snw141292 
7314520Snw141292 	/* Get cache handle */
7324520Snw141292 	result->retcode = get_cache_handle(&cache);
7334520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7344520Snw141292 		goto out;
7354520Snw141292 
7364520Snw141292 	/* Get db handle */
7374520Snw141292 	result->retcode = get_db_handle(&db);
7384520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
7394520Snw141292 		goto out;
7404520Snw141292 
7414520Snw141292 	/* Allocate result */
7424520Snw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
7434520Snw141292 	if (result->mappings.mappings_val == NULL) {
7444520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
7454520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
7464520Snw141292 		goto out;
7474520Snw141292 	}
7484520Snw141292 	result->mappings.mappings_len = 1;
7494520Snw141292 
7504520Snw141292 	if (IS_REQUEST_SID(request)) {
7514520Snw141292 		result->retcode = get_w2u_mapping(
7524520Snw141292 			cache,
7534520Snw141292 			db,
7544520Snw141292 			&request,
7554520Snw141292 			result->mappings.mappings_val);
7564520Snw141292 	} else if (IS_REQUEST_UID(request)) {
7574520Snw141292 		result->retcode = get_u2w_mapping(
7584520Snw141292 			cache,
7594520Snw141292 			db,
7604520Snw141292 			&request,
7614520Snw141292 			result->mappings.mappings_val,
7624520Snw141292 			1);
7634520Snw141292 	} else if (IS_REQUEST_GID(request)) {
7644520Snw141292 		result->retcode = get_u2w_mapping(
7654520Snw141292 			cache,
7664520Snw141292 			db,
7674520Snw141292 			&request,
7684520Snw141292 			result->mappings.mappings_val,
7694520Snw141292 			0);
7704520Snw141292 	} else {
7714520Snw141292 		result->retcode = IDMAP_ERR_IDTYPE;
7724520Snw141292 	}
7734520Snw141292 
7744520Snw141292 out:
7754520Snw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
7764520Snw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
7774520Snw141292 		result->mappings.mappings_len = 0;
7784520Snw141292 		result->mappings.mappings_val = NULL;
7794520Snw141292 	}
7804520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
7814520Snw141292 	return (TRUE);
7824520Snw141292 }
7834520Snw141292 
7844520Snw141292 
7854520Snw141292 /* ARGSUSED */
7864520Snw141292 int
7874520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
7884520Snw141292 		caddr_t result) {
7894520Snw141292 	(void) xdr_free(xdr_result, result);
7904520Snw141292 	return (TRUE);
7914520Snw141292 }
792