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 
664520Snw141292 /* ARGSUSED */
674520Snw141292 bool_t
684520Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) {
694520Snw141292 	return (TRUE);
704520Snw141292 }
714520Snw141292 
724520Snw141292 #define	IS_BATCH_SID(batch, i)\
734520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID
744520Snw141292 
754520Snw141292 #define	IS_BATCH_UID(batch, i)\
764520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID
774520Snw141292 
784520Snw141292 #define	IS_BATCH_GID(batch, i)\
794520Snw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID
804520Snw141292 
814520Snw141292 #define	IS_REQUEST_SID(request)\
824520Snw141292 	request.id1.idtype == IDMAP_SID
834520Snw141292 
844520Snw141292 #define	IS_REQUEST_UID(request)\
854520Snw141292 	request.id1.idtype == IDMAP_UID
864520Snw141292 
874520Snw141292 #define	IS_REQUEST_GID(request)\
884520Snw141292 	request.id1.idtype == IDMAP_GID
894520Snw141292 
904520Snw141292 /* ARGSUSED */
914520Snw141292 bool_t
924520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
934520Snw141292 		idmap_ids_res *result, struct svc_req *rqstp) {
944520Snw141292 	sqlite		*cache = NULL, *db = NULL;
954520Snw141292 	lookup_state_t	state;
964520Snw141292 	idmap_retcode	retcode, winrc;
974520Snw141292 	int		i;
984520Snw141292 
994520Snw141292 	/* Init */
1004520Snw141292 	(void) memset(result, 0, sizeof (*result));
1014520Snw141292 	(void) memset(&state, 0, sizeof (state));
1024520Snw141292 
1034520Snw141292 	/* Return success if nothing was requested */
1044520Snw141292 	if (batch.idmap_mapping_batch_len < 1)
1054520Snw141292 		goto out;
1064520Snw141292 
1074520Snw141292 	/* Get cache handle */
1084520Snw141292 	result->retcode = get_cache_handle(&cache);
1094520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1104520Snw141292 		goto out;
1114520Snw141292 
1124520Snw141292 	/* Get db handle */
1134520Snw141292 	result->retcode = get_db_handle(&db);
1144520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
1154520Snw141292 		goto out;
1164520Snw141292 
1174520Snw141292 	/* Allocate result array */
1184520Snw141292 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
1194520Snw141292 			sizeof (idmap_id_res));
1204520Snw141292 	if (result->ids.ids_val == NULL) {
1214520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
1224520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
1234520Snw141292 		goto out;
1244520Snw141292 	}
1254520Snw141292 	result->ids.ids_len = batch.idmap_mapping_batch_len;
1264520Snw141292 
1274520Snw141292 	/* Init our 'done' flags */
1284520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
1294520Snw141292 
1304520Snw141292 	/* First stage */
1314520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
1324520Snw141292 		if (IS_BATCH_SID(batch, i)) {
1334520Snw141292 			retcode = sid2pid_first_pass(
1344520Snw141292 				&state,
1354520Snw141292 				cache,
1364520Snw141292 				&batch.idmap_mapping_batch_val[i],
1374520Snw141292 				&result->ids.ids_val[i]);
1384520Snw141292 		} else if (IS_BATCH_UID(batch, i)) {
1394520Snw141292 			retcode = pid2sid_first_pass(
1404520Snw141292 				&state,
1414520Snw141292 				cache,
1424520Snw141292 				db,
1434520Snw141292 				&batch.idmap_mapping_batch_val[i],
1444520Snw141292 				&result->ids.ids_val[i], 1, 0);
1454520Snw141292 		} else if (IS_BATCH_GID(batch, i)) {
1464520Snw141292 			retcode = pid2sid_first_pass(
1474520Snw141292 				&state,
1484520Snw141292 				cache,
1494520Snw141292 				db,
1504520Snw141292 				&batch.idmap_mapping_batch_val[i],
1514520Snw141292 				&result->ids.ids_val[i], 0, 0);
1524520Snw141292 		} else {
1534520Snw141292 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
1544520Snw141292 			continue;
1554520Snw141292 		}
1564520Snw141292 		if (IDMAP_FATAL_ERROR(retcode)) {
1574520Snw141292 			result->retcode = retcode;
1584520Snw141292 			goto out;
1594520Snw141292 		}
1604520Snw141292 	}
1614520Snw141292 
1624520Snw141292 	/* Check if we are done */
1634520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
1644520Snw141292 		goto out;
1654520Snw141292 
1664520Snw141292 	/* Process Windows server lookups for sid2name */
1674520Snw141292 	if (state.ad_nqueries) {
1684520Snw141292 		winrc = lookup_win_batch_sid2name(&state, &batch,
1694520Snw141292 				result);
1704520Snw141292 		if (IDMAP_FATAL_ERROR(winrc)) {
1714520Snw141292 			result->retcode = winrc;
1724520Snw141292 			goto out;
1734520Snw141292 		}
1744520Snw141292 	} else
1754520Snw141292 		winrc = IDMAP_SUCCESS;
1764520Snw141292 
1774520Snw141292 	/* Reset sid2pid 'done' flag */
1784520Snw141292 	state.sid2pid_done = TRUE;
1794520Snw141292 
1804520Snw141292 	/* Second stage */
1814520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
1824520Snw141292 		/* Process sid to pid ONLY */
1834520Snw141292 		if (IS_BATCH_SID(batch, i)) {
1844520Snw141292 			if (IDMAP_ERROR(winrc))
1854520Snw141292 				result->ids.ids_val[i].retcode = winrc;
1864520Snw141292 			retcode = sid2pid_second_pass(
1874520Snw141292 				&state,
1884520Snw141292 				cache,
1894520Snw141292 				db,
1904520Snw141292 				&batch.idmap_mapping_batch_val[i],
1914520Snw141292 				&result->ids.ids_val[i]);
1924520Snw141292 			if (IDMAP_FATAL_ERROR(retcode)) {
1934520Snw141292 				result->retcode = retcode;
1944520Snw141292 				goto out;
1954520Snw141292 			}
1964520Snw141292 		}
1974520Snw141292 	}
1984520Snw141292 
1994520Snw141292 	/* Check if we are done */
2004520Snw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
2014520Snw141292 		goto out;
2024520Snw141292 
2034520Snw141292 	/* Reset our 'done' flags */
2044520Snw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
2054520Snw141292 
2064520Snw141292 	/* Update cache in a single transaction */
2074520Snw141292 	if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS)
2084520Snw141292 		goto out;
2094520Snw141292 
2104520Snw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
2114520Snw141292 		if (IS_BATCH_SID(batch, i)) {
2124520Snw141292 			(void) update_cache_sid2pid(
2134520Snw141292 				&state,
2144520Snw141292 				cache,
2154520Snw141292 				&batch.idmap_mapping_batch_val[i],
2164520Snw141292 				&result->ids.ids_val[i]);
2174520Snw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
2184520Snw141292 				(IS_BATCH_GID(batch, i))) {
2194520Snw141292 			(void) update_cache_pid2sid(
2204520Snw141292 				&state,
2214520Snw141292 				cache,
2224520Snw141292 				&batch.idmap_mapping_batch_val[i],
2234520Snw141292 				&result->ids.ids_val[i]);
2244520Snw141292 		}
2254520Snw141292 	}
2264520Snw141292 
2274520Snw141292 	/* Commit if we have atleast one successful update */
2284520Snw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
2294520Snw141292 		(void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;");
2304520Snw141292 	else
2314520Snw141292 		(void) sql_exec_no_cb(cache, "END TRANSACTION;");
2324520Snw141292 
2334520Snw141292 out:
2344520Snw141292 	if (IDMAP_ERROR(result->retcode)) {
2354520Snw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
2364520Snw141292 		result->ids.ids_len = 0;
2374520Snw141292 		result->ids.ids_val = NULL;
2384520Snw141292 	}
2394520Snw141292 	if (cache)
2404520Snw141292 		(void) sqlite_close(cache);
2414520Snw141292 	if (db)
2424520Snw141292 		(void) sqlite_close(db);
2434520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
2444520Snw141292 	return (TRUE);
2454520Snw141292 }
2464520Snw141292 
2474520Snw141292 
2484520Snw141292 /* ARGSUSED */
2494520Snw141292 static int
2504520Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) {
2514520Snw141292 	list_cb_data_t		*cb_data;
2524520Snw141292 	char			*str;
2534520Snw141292 	idmap_mappings_res	*result;
2544520Snw141292 	idmap_utf8str		*ptr;
2554520Snw141292 	idmap_retcode		retcode;
2564520Snw141292 	int			w2u, u2w;
2574520Snw141292 	char			*end;
2584520Snw141292 
2594520Snw141292 	cb_data = (list_cb_data_t *)parg;
2604520Snw141292 	result = (idmap_mappings_res *)cb_data->result;
2614520Snw141292 
2624520Snw141292 	_VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val,
2634520Snw141292 		sizeof (idmap_mapping));
2644520Snw141292 
2654520Snw141292 	result->mappings.mappings_len++;
2664520Snw141292 
2674520Snw141292 	if ((str = strdup(argv[1])) == NULL)
2684520Snw141292 		return (1);
2694520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
2704520Snw141292 		str;
2714520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
2724520Snw141292 		strtoul(argv[2], &end, 10);
2734520Snw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID;
2744520Snw141292 
2754520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
2764520Snw141292 		strtoul(argv[3], &end, 10);
2774520Snw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID;
2784520Snw141292 
2794520Snw141292 	w2u = argv[4]?strtol(argv[4], &end, 10):0;
2804520Snw141292 	u2w = argv[5]?strtol(argv[5], &end, 10):0;
2814520Snw141292 
2824520Snw141292 	if (w2u > 0 && u2w == 0)
283*4644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
284*4644Sbaban 		    IDMAP_DIRECTION_W2U;
2854520Snw141292 	else if (w2u == 0 && u2w > 0)
286*4644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
287*4644Sbaban 		    IDMAP_DIRECTION_U2W;
2884520Snw141292 	else
289*4644Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
290*4644Sbaban 		    IDMAP_DIRECTION_BI;
2914520Snw141292 
2924520Snw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id1domain;
2934520Snw141292 	if (idmap_str2utf8(&ptr, argv[6], 0) != IDMAP_SUCCESS)
2944520Snw141292 		return (1);
2954520Snw141292 
2964520Snw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id1name;
2974520Snw141292 	if (idmap_str2utf8(&ptr, argv[7], 0) != IDMAP_SUCCESS)
2984520Snw141292 		return (1);
2994520Snw141292 
3004520Snw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id2name;
3014520Snw141292 	if (idmap_str2utf8(&ptr, argv[8], 0) != IDMAP_SUCCESS)
3024520Snw141292 		return (1);
3034520Snw141292 
3044520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
3054520Snw141292 	cb_data->next++;
3064520Snw141292 	result->retcode = IDMAP_SUCCESS;
3074520Snw141292 	return (0);
3084520Snw141292 }
3094520Snw141292 
3104520Snw141292 
3114520Snw141292 /* ARGSUSED */
3124520Snw141292 bool_t
3134520Snw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid,
3144520Snw141292 		uint64_t limit, idmap_mappings_res *result,
3154520Snw141292 		struct svc_req *rqstp) {
3164520Snw141292 	sqlite		*cache = NULL;
3174520Snw141292 	char		lbuf[30], rbuf[30];
3184520Snw141292 	uint64_t	maxlimit;
3194520Snw141292 	idmap_retcode	retcode;
3204520Snw141292 	char		*sql = NULL;
3214520Snw141292 
3224520Snw141292 	(void) memset(result, 0, sizeof (*result));
3234520Snw141292 	lbuf[0] = rbuf[0] = 0;
3244520Snw141292 
3254520Snw141292 	RDLOCK_CONFIG();
3264520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
3274520Snw141292 	UNLOCK_CONFIG();
3284520Snw141292 
3294520Snw141292 	/* Get cache handle */
3304520Snw141292 	result->retcode = get_cache_handle(&cache);
3314520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
3324520Snw141292 		goto out;
3334520Snw141292 
3344520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
3354520Snw141292 
3364520Snw141292 	/* Create LIMIT expression. */
3374520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
3384520Snw141292 		limit = maxlimit;
3394520Snw141292 	if (limit > 0)
3404520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
3414520Snw141292 			"LIMIT %" PRIu64, limit + 1ULL);
3424520Snw141292 
3434520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
3444520Snw141292 
3454520Snw141292 	/*
3464520Snw141292 	 * Combine all the above into a giant SELECT statement that
3474520Snw141292 	 * will return the requested mappings
3484520Snw141292 	 */
3494520Snw141292 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w,"
3504520Snw141292 			" windomain, winname, unixname"
3514520Snw141292 			" FROM idmap_cache WHERE "
3524520Snw141292 			" %s AND is_user = %d %s;",
3534520Snw141292 			rbuf, is_user?1:0, lbuf);
3544520Snw141292 	if (sql == NULL) {
3554520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
3564520Snw141292 		goto out;
3574520Snw141292 	}
3584520Snw141292 
3594520Snw141292 	/* Execute the SQL statement and update the return buffer */
3604520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb,
3614520Snw141292 		result, result->mappings.mappings_len);
3624520Snw141292 
3634520Snw141292 out:
3644520Snw141292 	if (sql)
3654520Snw141292 		sqlite_freemem(sql);
3664520Snw141292 	if (IDMAP_ERROR(result->retcode))
3674520Snw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
3684520Snw141292 	if (cache)
3694520Snw141292 		(void) sqlite_close(cache);
3704520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
3714520Snw141292 	return (TRUE);
3724520Snw141292 }
3734520Snw141292 
3744520Snw141292 
3754520Snw141292 /* ARGSUSED */
3764520Snw141292 static int
3774520Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) {
3784520Snw141292 	list_cb_data_t		*cb_data;
3794520Snw141292 	idmap_namerules_res	*result;
3804520Snw141292 	idmap_retcode		retcode;
3814520Snw141292 	idmap_utf8str		*ptr;
3824520Snw141292 	int			w2u_order, u2w_order;
3834520Snw141292 	char			*end;
3844520Snw141292 
3854520Snw141292 	cb_data = (list_cb_data_t *)parg;
3864520Snw141292 	result = (idmap_namerules_res *)cb_data->result;
3874520Snw141292 
3884520Snw141292 	_VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val,
3894520Snw141292 		sizeof (idmap_namerule));
3904520Snw141292 
3914520Snw141292 	result->rules.rules_len++;
3924520Snw141292 
3934520Snw141292 	result->rules.rules_val[cb_data->next].is_user =
3944520Snw141292 		strtol(argv[1], &end, 10);
3954520Snw141292 
3964520Snw141292 	ptr = &result->rules.rules_val[cb_data->next].windomain;
3974520Snw141292 	if (idmap_str2utf8(&ptr, argv[2], 0) != IDMAP_SUCCESS)
3984520Snw141292 		return (1);
3994520Snw141292 
4004520Snw141292 	ptr = &result->rules.rules_val[cb_data->next].winname;
4014520Snw141292 	if (idmap_str2utf8(&ptr, argv[3], 0) != IDMAP_SUCCESS)
4024520Snw141292 		return (1);
4034520Snw141292 
4044520Snw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
4054520Snw141292 		strtol(argv[4], &end, 10);
4064520Snw141292 
4074520Snw141292 	ptr = &result->rules.rules_val[cb_data->next].unixname;
4084520Snw141292 	if (idmap_str2utf8(&ptr, argv[5], 0) != IDMAP_SUCCESS)
4094520Snw141292 		return (1);
4104520Snw141292 
4114520Snw141292 	w2u_order = argv[6]?strtol(argv[6], &end, 10):0;
4124520Snw141292 	u2w_order = argv[7]?strtol(argv[7], &end, 10):0;
4134520Snw141292 
4144520Snw141292 	if (w2u_order > 0 && u2w_order == 0)
415*4644Sbaban 		result->rules.rules_val[cb_data->next].direction =
416*4644Sbaban 		    IDMAP_DIRECTION_W2U;
4174520Snw141292 	else if (w2u_order == 0 && u2w_order > 0)
418*4644Sbaban 		result->rules.rules_val[cb_data->next].direction =
419*4644Sbaban 		    IDMAP_DIRECTION_U2W;
4204520Snw141292 	else
421*4644Sbaban 		result->rules.rules_val[cb_data->next].direction =
422*4644Sbaban 		    IDMAP_DIRECTION_BI;
4234520Snw141292 
4244520Snw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
4254520Snw141292 	cb_data->next++;
4264520Snw141292 	result->retcode = IDMAP_SUCCESS;
4274520Snw141292 	return (0);
4284520Snw141292 }
4294520Snw141292 
4304520Snw141292 
4314520Snw141292 /* ARGSUSED */
4324520Snw141292 bool_t
4334520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
4344520Snw141292 		uint64_t limit, idmap_namerules_res *result,
4354520Snw141292 		struct svc_req *rqstp) {
4364520Snw141292 
4374520Snw141292 	sqlite		*db = NULL;
4384520Snw141292 	char		w2ubuf[15], u2wbuf[15];
4394520Snw141292 	char		lbuf[30], rbuf[30];
4404520Snw141292 	char		*sql = NULL;
4414520Snw141292 	char		*s_windomain = NULL, *s_winname = NULL;
4424520Snw141292 	char		*s_unixname = NULL;
4434520Snw141292 	uint64_t	maxlimit;
4444520Snw141292 	idmap_retcode	retcode;
4454520Snw141292 
4464520Snw141292 	(void) memset(result, 0, sizeof (*result));
4474520Snw141292 	lbuf[0] = rbuf[0] = 0;
4484520Snw141292 
4494520Snw141292 	RDLOCK_CONFIG();
4504520Snw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
4514520Snw141292 	UNLOCK_CONFIG();
4524520Snw141292 
4534520Snw141292 	/* Get db handle */
4544520Snw141292 	result->retcode = get_db_handle(&db);
4554520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
4564520Snw141292 		goto out;
4574520Snw141292 
4584520Snw141292 	result->retcode = IDMAP_ERR_INTERNAL;
4594520Snw141292 
4604520Snw141292 	if (rule.direction < 0) {
4614520Snw141292 		w2ubuf[0] = u2wbuf[0] = 0;
462*4644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_BI) {
4634520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
4644520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
465*4644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
4664520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
4674520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
4684520Snw141292 				"AND (u2w_order = 0 OR u2w_order ISNULL)");
469*4644Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
4704520Snw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
4714520Snw141292 				"AND (w2u_order = 0 OR w2u_order ISNULL)");
4724520Snw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
4734520Snw141292 	}
4744520Snw141292 
4754520Snw141292 	/* Create where statement for windomain */
4764520Snw141292 	if (rule.windomain.idmap_utf8str_len > 0) {
4774520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "windomain", "=",
4784520Snw141292 				&rule.windomain,
4794520Snw141292 				"", &s_windomain) != IDMAP_SUCCESS)
4804520Snw141292 			goto out;
4814520Snw141292 	}
4824520Snw141292 
4834520Snw141292 	/* Create where statement for winname */
4844520Snw141292 	if (rule.winname.idmap_utf8str_len > 0) {
4854520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "winname", "=",
4864520Snw141292 				&rule.winname,
4874520Snw141292 				"", &s_winname) != IDMAP_SUCCESS)
4884520Snw141292 			goto out;
4894520Snw141292 	}
4904520Snw141292 
4914520Snw141292 	/* Create where statement for unixname */
4924520Snw141292 	if (rule.unixname.idmap_utf8str_len > 0) {
4934520Snw141292 		if (gen_sql_expr_from_utf8str("AND", "unixname", "=",
4944520Snw141292 				&rule.unixname,
4954520Snw141292 				"", &s_unixname) != IDMAP_SUCCESS)
4964520Snw141292 			goto out;
4974520Snw141292 	}
4984520Snw141292 
4994520Snw141292 	/* Create LIMIT expression. */
5004520Snw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
5014520Snw141292 		limit = maxlimit;
5024520Snw141292 	if (limit > 0)
5034520Snw141292 		(void) snprintf(lbuf, sizeof (lbuf),
5044520Snw141292 			"LIMIT %" PRIu64, limit + 1ULL);
5054520Snw141292 
5064520Snw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
5074520Snw141292 
5084520Snw141292 	/*
5094520Snw141292 	 * Combine all the above into a giant SELECT statement that
5104520Snw141292 	 * will return the requested rules
5114520Snw141292 	 */
5124520Snw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, "
5134520Snw141292 			"is_nt4, unixname, w2u_order, u2w_order "
5144520Snw141292 			"FROM namerules WHERE "
5154520Snw141292 			" %s AND is_user = %d %s %s %s %s %s %s;",
5164520Snw141292 			rbuf, rule.is_user?1:0,
5174520Snw141292 			s_windomain?s_windomain:"",
5184520Snw141292 			s_winname?s_winname:"",
5194520Snw141292 			s_unixname?s_unixname:"",
5204520Snw141292 			w2ubuf, u2wbuf, lbuf);
5214520Snw141292 	if (sql == NULL) {
5224520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
5234520Snw141292 		goto out;
5244520Snw141292 	}
5254520Snw141292 
5264520Snw141292 	/* Execute the SQL statement and update the return buffer */
5274520Snw141292 	PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb,
5284520Snw141292 		result, result->rules.rules_len);
5294520Snw141292 
5304520Snw141292 out:
5314520Snw141292 	if (s_windomain)
5324520Snw141292 		sqlite_freemem(s_windomain);
5334520Snw141292 	if (s_winname)
5344520Snw141292 		sqlite_freemem(s_winname);
5354520Snw141292 	if (s_unixname)
5364520Snw141292 		sqlite_freemem(s_unixname);
5374520Snw141292 	if (sql)
5384520Snw141292 		sqlite_freemem(sql);
5394520Snw141292 	if (IDMAP_ERROR(result->retcode))
5404520Snw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
5414520Snw141292 	if (db)
5424520Snw141292 		(void) sqlite_close(db);
5434520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
5444520Snw141292 	return (TRUE);
5454520Snw141292 }
5464520Snw141292 
5474520Snw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
5484520Snw141292 static int
5494520Snw141292 verify_rules_auth(struct svc_req *rqstp) {
5504520Snw141292 	ucred_t		*uc = NULL;
5514520Snw141292 	uid_t		uid;
5524520Snw141292 	char		buf[1024];
5534520Snw141292 	struct passwd	pwd;
5544520Snw141292 	const char	*me = "verify_rules_auth";
5554520Snw141292 
5564520Snw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
5574520Snw141292 		idmapdlog(LOG_ERR,
5584520Snw141292 			"%s: svc_getcallerucred failed (errno=%d)",
5594520Snw141292 			me, errno);
5604520Snw141292 		return (-1);
5614520Snw141292 	}
5624520Snw141292 
5634520Snw141292 	uid = ucred_geteuid(uc);
5644520Snw141292 	if (uid == (uid_t)-1) {
5654520Snw141292 		idmapdlog(LOG_ERR,
5664520Snw141292 			"%s: ucred_geteuid failed (errno=%d)",
5674520Snw141292 			me, errno);
5684520Snw141292 		ucred_free(uc);
5694520Snw141292 		return (-1);
5704520Snw141292 	}
5714520Snw141292 
5724520Snw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
5734520Snw141292 		idmapdlog(LOG_ERR,
5744520Snw141292 			"%s: getpwuid_r(%u) failed (errno=%d)",
5754520Snw141292 			me, uid, errno);
5764520Snw141292 		ucred_free(uc);
5774520Snw141292 		return (-1);
5784520Snw141292 	}
5794520Snw141292 
5804520Snw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
5814520Snw141292 		idmapdlog(LOG_INFO,
5824520Snw141292 			"%s: %s does not have authorization.",
5834520Snw141292 			me, pwd.pw_name);
5844520Snw141292 		ucred_free(uc);
5854520Snw141292 		return (-1);
5864520Snw141292 	}
5874520Snw141292 
5884520Snw141292 	ucred_free(uc);
5894520Snw141292 	return (1);
5904520Snw141292 }
5914520Snw141292 
5924520Snw141292 /* ARGSUSED */
5934520Snw141292 bool_t
5944520Snw141292 idmap_update_1_svc(idmap_update_batch batch, idmap_retcode *result,
5954520Snw141292 		struct svc_req *rqstp) {
5964520Snw141292 	sqlite		*db = NULL;
5974520Snw141292 	idmap_update_op	*up;
5984520Snw141292 	int		i;
5994520Snw141292 
6004520Snw141292 	if (verify_rules_auth(rqstp) < 0) {
6014520Snw141292 		*result = IDMAP_ERR_PERMISSION_DENIED;
6024520Snw141292 		goto out;
6034520Snw141292 	}
6044520Snw141292 
6054520Snw141292 	if (batch.idmap_update_batch_len == 0 ||
6064520Snw141292 			batch.idmap_update_batch_val == NULL) {
6074520Snw141292 		*result = IDMAP_SUCCESS;
6084520Snw141292 		goto out;
6094520Snw141292 	}
6104520Snw141292 
6114520Snw141292 	/* Get db handle */
6124520Snw141292 	*result = get_db_handle(&db);
6134520Snw141292 	if (*result != IDMAP_SUCCESS)
6144520Snw141292 		goto out;
6154520Snw141292 
6164520Snw141292 	*result = sql_exec_no_cb(db, "BEGIN TRANSACTION;");
6174520Snw141292 	if (*result != IDMAP_SUCCESS)
6184520Snw141292 		goto out;
6194520Snw141292 
6204520Snw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
6214520Snw141292 		up = &batch.idmap_update_batch_val[i];
6224520Snw141292 		switch (up->opnum) {
6234520Snw141292 		case OP_NONE:
6244520Snw141292 			*result = IDMAP_SUCCESS;
6254520Snw141292 			break;
6264520Snw141292 		case OP_ADD_NAMERULE:
6274520Snw141292 			*result = add_namerule(db,
6284520Snw141292 				&up->idmap_update_op_u.rule);
6294520Snw141292 			break;
6304520Snw141292 		case OP_RM_NAMERULE:
6314520Snw141292 			*result = rm_namerule(db,
6324520Snw141292 				&up->idmap_update_op_u.rule);
6334520Snw141292 			break;
6344520Snw141292 		case OP_FLUSH_NAMERULES:
6354520Snw141292 			*result = flush_namerules(db,
6364520Snw141292 				up->idmap_update_op_u.is_user);
6374520Snw141292 			break;
6384520Snw141292 		default:
6394520Snw141292 			*result = IDMAP_ERR_NOTSUPPORTED;
6404520Snw141292 			goto out;
6414520Snw141292 		};
6424520Snw141292 
6434520Snw141292 		if (*result != IDMAP_SUCCESS)
6444520Snw141292 			goto out;
6454520Snw141292 	}
6464520Snw141292 
6474520Snw141292 out:
6484520Snw141292 	if (*result == IDMAP_SUCCESS && db) {
6494520Snw141292 		*result = sql_exec_no_cb(db, "COMMIT TRANSACTION;");
6504520Snw141292 	}
6514520Snw141292 
6524520Snw141292 	if (db)
6534520Snw141292 		(void) sqlite_close(db);
6544520Snw141292 	*result = idmap_stat4prot(*result);
6554520Snw141292 	return (TRUE);
6564520Snw141292 }
6574520Snw141292 
6584520Snw141292 
6594520Snw141292 /* ARGSUSED */
6604520Snw141292 bool_t
6614520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
6624520Snw141292 		idmap_mappings_res *result, struct svc_req *rqstp) {
6634520Snw141292 	sqlite		*cache = NULL, *db = NULL;
6644520Snw141292 
6654520Snw141292 	/* Init */
6664520Snw141292 	(void) memset(result, 0, sizeof (*result));
6674520Snw141292 
6684520Snw141292 	/* Get cache handle */
6694520Snw141292 	result->retcode = get_cache_handle(&cache);
6704520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
6714520Snw141292 		goto out;
6724520Snw141292 
6734520Snw141292 	/* Get db handle */
6744520Snw141292 	result->retcode = get_db_handle(&db);
6754520Snw141292 	if (result->retcode != IDMAP_SUCCESS)
6764520Snw141292 		goto out;
6774520Snw141292 
6784520Snw141292 	/* Allocate result */
6794520Snw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
6804520Snw141292 	if (result->mappings.mappings_val == NULL) {
6814520Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
6824520Snw141292 		result->retcode = IDMAP_ERR_MEMORY;
6834520Snw141292 		goto out;
6844520Snw141292 	}
6854520Snw141292 	result->mappings.mappings_len = 1;
6864520Snw141292 
6874520Snw141292 	if (IS_REQUEST_SID(request)) {
6884520Snw141292 		result->retcode = get_w2u_mapping(
6894520Snw141292 			cache,
6904520Snw141292 			db,
6914520Snw141292 			&request,
6924520Snw141292 			result->mappings.mappings_val);
6934520Snw141292 	} else if (IS_REQUEST_UID(request)) {
6944520Snw141292 		result->retcode = get_u2w_mapping(
6954520Snw141292 			cache,
6964520Snw141292 			db,
6974520Snw141292 			&request,
6984520Snw141292 			result->mappings.mappings_val,
6994520Snw141292 			1);
7004520Snw141292 	} else if (IS_REQUEST_GID(request)) {
7014520Snw141292 		result->retcode = get_u2w_mapping(
7024520Snw141292 			cache,
7034520Snw141292 			db,
7044520Snw141292 			&request,
7054520Snw141292 			result->mappings.mappings_val,
7064520Snw141292 			0);
7074520Snw141292 	} else {
7084520Snw141292 		result->retcode = IDMAP_ERR_IDTYPE;
7094520Snw141292 	}
7104520Snw141292 
7114520Snw141292 out:
7124520Snw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
7134520Snw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
7144520Snw141292 		result->mappings.mappings_len = 0;
7154520Snw141292 		result->mappings.mappings_val = NULL;
7164520Snw141292 	}
7174520Snw141292 	if (cache)
7184520Snw141292 		(void) sqlite_close(cache);
7194520Snw141292 	if (db)
7204520Snw141292 		(void) sqlite_close(db);
7214520Snw141292 	result->retcode = idmap_stat4prot(result->retcode);
7224520Snw141292 	return (TRUE);
7234520Snw141292 }
7244520Snw141292 
7254520Snw141292 
7264520Snw141292 /* ARGSUSED */
7274520Snw141292 int
7284520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
7294520Snw141292 		caddr_t result) {
7304520Snw141292 	(void) xdr_free(xdr_result, result);
7314520Snw141292 	return (TRUE);
7324520Snw141292 }
733