xref: /titanic_44/usr/src/cmd/idmap/idmapd/server.c (revision 84decf41e1c0970e397cc8710dfcf81db5b8c6da)
1c5c4113dSnw141292 /*
2c5c4113dSnw141292  * CDDL HEADER START
3c5c4113dSnw141292  *
4c5c4113dSnw141292  * The contents of this file are subject to the terms of the
5c5c4113dSnw141292  * Common Development and Distribution License (the "License").
6c5c4113dSnw141292  * You may not use this file except in compliance with the License.
7c5c4113dSnw141292  *
8c5c4113dSnw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c5c4113dSnw141292  * or http://www.opensolaris.org/os/licensing.
10c5c4113dSnw141292  * See the License for the specific language governing permissions
11c5c4113dSnw141292  * and limitations under the License.
12c5c4113dSnw141292  *
13c5c4113dSnw141292  * When distributing Covered Code, include this CDDL HEADER in each
14c5c4113dSnw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c5c4113dSnw141292  * If applicable, add the following below this CDDL HEADER, with the
16c5c4113dSnw141292  * fields enclosed by brackets "[]" replaced with your own identifying
17c5c4113dSnw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
18c5c4113dSnw141292  *
19c5c4113dSnw141292  * CDDL HEADER END
20c5c4113dSnw141292  */
21c5c4113dSnw141292 /*
22c5c4113dSnw141292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23c5c4113dSnw141292  * Use is subject to license terms.
24c5c4113dSnw141292  */
25c5c4113dSnw141292 
26c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c5c4113dSnw141292 
28c5c4113dSnw141292 /*
29c5c4113dSnw141292  * Service routines
30c5c4113dSnw141292  */
31c5c4113dSnw141292 
32c5c4113dSnw141292 #include "idmapd.h"
33c5c4113dSnw141292 #include "idmap_priv.h"
34c5c4113dSnw141292 #include <signal.h>
35c5c4113dSnw141292 #include <thread.h>
36c5c4113dSnw141292 #include <string.h>
37c5c4113dSnw141292 #include <strings.h>
38c5c4113dSnw141292 #include <errno.h>
39c5c4113dSnw141292 #include <assert.h>
40c5c4113dSnw141292 #include <sys/types.h>
41c5c4113dSnw141292 #include <sys/stat.h>
42c5c4113dSnw141292 #include <ucred.h>
43c5c4113dSnw141292 #include <pwd.h>
44c5c4113dSnw141292 #include <auth_attr.h>
45c5c4113dSnw141292 #include <secdb.h>
46c5c4113dSnw141292 
47c5c4113dSnw141292 #define	_VALIDATE_LIST_CB_DATA(col, val, siz)\
48c5c4113dSnw141292 	retcode = validate_list_cb_data(cb_data, argc, argv, col,\
49c5c4113dSnw141292 			(uchar_t **)val, siz);\
50c5c4113dSnw141292 	if (retcode == IDMAP_NEXT) {\
51c5c4113dSnw141292 		result->retcode = IDMAP_NEXT;\
52c5c4113dSnw141292 		return (0);\
53c5c4113dSnw141292 	} else if (retcode < 0) {\
54c5c4113dSnw141292 		result->retcode = retcode;\
55c5c4113dSnw141292 		return (1);\
56c5c4113dSnw141292 	}
57c5c4113dSnw141292 
58c5c4113dSnw141292 #define	PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\
59c5c4113dSnw141292 	rcode = process_list_svc_sql(db, sql, limit, cb, res);\
60c5c4113dSnw141292 	if (rcode == IDMAP_ERR_BUSY)\
61c5c4113dSnw141292 		res->retcode = IDMAP_ERR_BUSY;\
62c5c4113dSnw141292 	else if (rcode == IDMAP_SUCCESS && len == 0)\
63c5c4113dSnw141292 		res->retcode = IDMAP_ERR_NOTFOUND;
64c5c4113dSnw141292 
65c5c4113dSnw141292 
66c5c4113dSnw141292 /* ARGSUSED */
67c5c4113dSnw141292 bool_t
68c5c4113dSnw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) {
69c5c4113dSnw141292 	return (TRUE);
70c5c4113dSnw141292 }
71c5c4113dSnw141292 
72c5c4113dSnw141292 #define	IS_BATCH_SID(batch, i)\
73c5c4113dSnw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID
74c5c4113dSnw141292 
75c5c4113dSnw141292 #define	IS_BATCH_UID(batch, i)\
76c5c4113dSnw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID
77c5c4113dSnw141292 
78c5c4113dSnw141292 #define	IS_BATCH_GID(batch, i)\
79c5c4113dSnw141292 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID
80c5c4113dSnw141292 
81c5c4113dSnw141292 #define	IS_REQUEST_SID(request)\
82c5c4113dSnw141292 	request.id1.idtype == IDMAP_SID
83c5c4113dSnw141292 
84c5c4113dSnw141292 #define	IS_REQUEST_UID(request)\
85c5c4113dSnw141292 	request.id1.idtype == IDMAP_UID
86c5c4113dSnw141292 
87c5c4113dSnw141292 #define	IS_REQUEST_GID(request)\
88c5c4113dSnw141292 	request.id1.idtype == IDMAP_GID
89c5c4113dSnw141292 
90c5c4113dSnw141292 /* ARGSUSED */
91c5c4113dSnw141292 bool_t
92c5c4113dSnw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
93c5c4113dSnw141292 		idmap_ids_res *result, struct svc_req *rqstp) {
94c5c4113dSnw141292 	sqlite		*cache = NULL, *db = NULL;
95c5c4113dSnw141292 	lookup_state_t	state;
96c5c4113dSnw141292 	idmap_retcode	retcode, winrc;
9762c60062Sbaban 	uint_t		i;
98c5c4113dSnw141292 
99c5c4113dSnw141292 	/* Init */
100c5c4113dSnw141292 	(void) memset(result, 0, sizeof (*result));
101c5c4113dSnw141292 	(void) memset(&state, 0, sizeof (state));
102c5c4113dSnw141292 
103c5c4113dSnw141292 	/* Return success if nothing was requested */
104c5c4113dSnw141292 	if (batch.idmap_mapping_batch_len < 1)
105c5c4113dSnw141292 		goto out;
106c5c4113dSnw141292 
107c5c4113dSnw141292 	/* Get cache handle */
108c5c4113dSnw141292 	result->retcode = get_cache_handle(&cache);
109c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
110c5c4113dSnw141292 		goto out;
111c5c4113dSnw141292 
112c5c4113dSnw141292 	/* Get db handle */
113c5c4113dSnw141292 	result->retcode = get_db_handle(&db);
114c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
115c5c4113dSnw141292 		goto out;
116c5c4113dSnw141292 
117c5c4113dSnw141292 	/* Allocate result array */
118c5c4113dSnw141292 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
119c5c4113dSnw141292 			sizeof (idmap_id_res));
120c5c4113dSnw141292 	if (result->ids.ids_val == NULL) {
121c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
122c5c4113dSnw141292 		result->retcode = IDMAP_ERR_MEMORY;
123c5c4113dSnw141292 		goto out;
124c5c4113dSnw141292 	}
125c5c4113dSnw141292 	result->ids.ids_len = batch.idmap_mapping_batch_len;
126c5c4113dSnw141292 
12762c60062Sbaban 	/* Allocate hash table to check for duplicate sids */
12862c60062Sbaban 	state.sid_history = calloc(batch.idmap_mapping_batch_len,
12962c60062Sbaban 			sizeof (*state.sid_history));
13062c60062Sbaban 	if (state.sid_history == NULL) {
13162c60062Sbaban 		idmapdlog(LOG_ERR, "Out of memory");
13262c60062Sbaban 		result->retcode = IDMAP_ERR_MEMORY;
13362c60062Sbaban 		goto out;
13462c60062Sbaban 	}
13562c60062Sbaban 	state.sid_history_size = batch.idmap_mapping_batch_len;
13662c60062Sbaban 	for (i = 0; i < state.sid_history_size; i++) {
13762c60062Sbaban 		state.sid_history[i].key = state.sid_history_size;
13862c60062Sbaban 		state.sid_history[i].next = state.sid_history_size;
13962c60062Sbaban 	}
14062c60062Sbaban 	state.batch = &batch;
14162c60062Sbaban 	state.result = result;
14262c60062Sbaban 
143c5c4113dSnw141292 	/* Init our 'done' flags */
144c5c4113dSnw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
145c5c4113dSnw141292 
146c5c4113dSnw141292 	/* First stage */
147c5c4113dSnw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
14862c60062Sbaban 		state.curpos = i;
149c5c4113dSnw141292 		if (IS_BATCH_SID(batch, i)) {
150c5c4113dSnw141292 			retcode = sid2pid_first_pass(
151c5c4113dSnw141292 				&state,
152c5c4113dSnw141292 				cache,
153c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
154c5c4113dSnw141292 				&result->ids.ids_val[i]);
155c5c4113dSnw141292 		} else if (IS_BATCH_UID(batch, i)) {
156c5c4113dSnw141292 			retcode = pid2sid_first_pass(
157c5c4113dSnw141292 				&state,
158c5c4113dSnw141292 				cache,
159c5c4113dSnw141292 				db,
160c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
161c5c4113dSnw141292 				&result->ids.ids_val[i], 1, 0);
162c5c4113dSnw141292 		} else if (IS_BATCH_GID(batch, i)) {
163c5c4113dSnw141292 			retcode = pid2sid_first_pass(
164c5c4113dSnw141292 				&state,
165c5c4113dSnw141292 				cache,
166c5c4113dSnw141292 				db,
167c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
168c5c4113dSnw141292 				&result->ids.ids_val[i], 0, 0);
169c5c4113dSnw141292 		} else {
170c5c4113dSnw141292 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
171c5c4113dSnw141292 			continue;
172c5c4113dSnw141292 		}
173c5c4113dSnw141292 		if (IDMAP_FATAL_ERROR(retcode)) {
174c5c4113dSnw141292 			result->retcode = retcode;
175c5c4113dSnw141292 			goto out;
176c5c4113dSnw141292 		}
177c5c4113dSnw141292 	}
178c5c4113dSnw141292 
179c5c4113dSnw141292 	/* Check if we are done */
180c5c4113dSnw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
181c5c4113dSnw141292 		goto out;
182c5c4113dSnw141292 
183c5c4113dSnw141292 	/* Process Windows server lookups for sid2name */
184c5c4113dSnw141292 	if (state.ad_nqueries) {
185c5c4113dSnw141292 		winrc = lookup_win_batch_sid2name(&state, &batch,
186c5c4113dSnw141292 				result);
187c5c4113dSnw141292 		if (IDMAP_FATAL_ERROR(winrc)) {
188c5c4113dSnw141292 			result->retcode = winrc;
189c5c4113dSnw141292 			goto out;
190c5c4113dSnw141292 		}
191c5c4113dSnw141292 	} else
192c5c4113dSnw141292 		winrc = IDMAP_SUCCESS;
193c5c4113dSnw141292 
194c5c4113dSnw141292 	/* Reset sid2pid 'done' flag */
195c5c4113dSnw141292 	state.sid2pid_done = TRUE;
196c5c4113dSnw141292 
197c5c4113dSnw141292 	/* Second stage */
198c5c4113dSnw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
19962c60062Sbaban 		state.curpos = i;
200c5c4113dSnw141292 		/* Process sid to pid ONLY */
201c5c4113dSnw141292 		if (IS_BATCH_SID(batch, i)) {
202c5c4113dSnw141292 			if (IDMAP_ERROR(winrc))
203c5c4113dSnw141292 				result->ids.ids_val[i].retcode = winrc;
204c5c4113dSnw141292 			retcode = sid2pid_second_pass(
205c5c4113dSnw141292 				&state,
206c5c4113dSnw141292 				cache,
207c5c4113dSnw141292 				db,
208c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
209c5c4113dSnw141292 				&result->ids.ids_val[i]);
210c5c4113dSnw141292 			if (IDMAP_FATAL_ERROR(retcode)) {
211c5c4113dSnw141292 				result->retcode = retcode;
212c5c4113dSnw141292 				goto out;
213c5c4113dSnw141292 			}
214c5c4113dSnw141292 		}
215c5c4113dSnw141292 	}
216c5c4113dSnw141292 
217c5c4113dSnw141292 	/* Check if we are done */
218c5c4113dSnw141292 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
219c5c4113dSnw141292 		goto out;
220c5c4113dSnw141292 
221c5c4113dSnw141292 	/* Reset our 'done' flags */
222c5c4113dSnw141292 	state.sid2pid_done = state.pid2sid_done = TRUE;
223c5c4113dSnw141292 
224c5c4113dSnw141292 	/* Update cache in a single transaction */
225c5c4113dSnw141292 	if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS)
226c5c4113dSnw141292 		goto out;
227c5c4113dSnw141292 
228c5c4113dSnw141292 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
22962c60062Sbaban 		state.curpos = i;
230c5c4113dSnw141292 		if (IS_BATCH_SID(batch, i)) {
231c5c4113dSnw141292 			(void) update_cache_sid2pid(
232c5c4113dSnw141292 				&state,
233c5c4113dSnw141292 				cache,
234c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
235c5c4113dSnw141292 				&result->ids.ids_val[i]);
236c5c4113dSnw141292 		} else if ((IS_BATCH_UID(batch, i)) ||
237c5c4113dSnw141292 				(IS_BATCH_GID(batch, i))) {
238c5c4113dSnw141292 			(void) update_cache_pid2sid(
239c5c4113dSnw141292 				&state,
240c5c4113dSnw141292 				cache,
241c5c4113dSnw141292 				&batch.idmap_mapping_batch_val[i],
242c5c4113dSnw141292 				&result->ids.ids_val[i]);
243c5c4113dSnw141292 		}
244c5c4113dSnw141292 	}
245c5c4113dSnw141292 
246c5c4113dSnw141292 	/* Commit if we have atleast one successful update */
247c5c4113dSnw141292 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
248c5c4113dSnw141292 		(void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;");
249c5c4113dSnw141292 	else
250c5c4113dSnw141292 		(void) sql_exec_no_cb(cache, "END TRANSACTION;");
251c5c4113dSnw141292 
252c5c4113dSnw141292 out:
25362c60062Sbaban 	if (state.sid_history)
25462c60062Sbaban 		free(state.sid_history);
255c5c4113dSnw141292 	if (IDMAP_ERROR(result->retcode)) {
256c5c4113dSnw141292 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
257c5c4113dSnw141292 		result->ids.ids_len = 0;
258c5c4113dSnw141292 		result->ids.ids_val = NULL;
259c5c4113dSnw141292 	}
260c5c4113dSnw141292 	result->retcode = idmap_stat4prot(result->retcode);
261c5c4113dSnw141292 	return (TRUE);
262c5c4113dSnw141292 }
263c5c4113dSnw141292 
264c5c4113dSnw141292 
265c5c4113dSnw141292 /* ARGSUSED */
266c5c4113dSnw141292 static int
267c5c4113dSnw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) {
268c5c4113dSnw141292 	list_cb_data_t		*cb_data;
269c5c4113dSnw141292 	char			*str;
270c5c4113dSnw141292 	idmap_mappings_res	*result;
271c5c4113dSnw141292 	idmap_utf8str		*ptr;
272c5c4113dSnw141292 	idmap_retcode		retcode;
273c5c4113dSnw141292 	int			w2u, u2w;
274c5c4113dSnw141292 	char			*end;
275c5c4113dSnw141292 
276c5c4113dSnw141292 	cb_data = (list_cb_data_t *)parg;
277c5c4113dSnw141292 	result = (idmap_mappings_res *)cb_data->result;
278c5c4113dSnw141292 
279c5c4113dSnw141292 	_VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val,
280c5c4113dSnw141292 		sizeof (idmap_mapping));
281c5c4113dSnw141292 
282c5c4113dSnw141292 	result->mappings.mappings_len++;
283c5c4113dSnw141292 
284c5c4113dSnw141292 	if ((str = strdup(argv[1])) == NULL)
285c5c4113dSnw141292 		return (1);
286c5c4113dSnw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
287c5c4113dSnw141292 		str;
288c5c4113dSnw141292 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
289c5c4113dSnw141292 		strtoul(argv[2], &end, 10);
290c5c4113dSnw141292 	result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID;
291c5c4113dSnw141292 
292c5c4113dSnw141292 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
293c5c4113dSnw141292 		strtoul(argv[3], &end, 10);
294c5c4113dSnw141292 	result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID;
295c5c4113dSnw141292 
296c5c4113dSnw141292 	w2u = argv[4]?strtol(argv[4], &end, 10):0;
297c5c4113dSnw141292 	u2w = argv[5]?strtol(argv[5], &end, 10):0;
298c5c4113dSnw141292 
299c5c4113dSnw141292 	if (w2u > 0 && u2w == 0)
300651c0131Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
301651c0131Sbaban 		    IDMAP_DIRECTION_W2U;
302c5c4113dSnw141292 	else if (w2u == 0 && u2w > 0)
303651c0131Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
304651c0131Sbaban 		    IDMAP_DIRECTION_U2W;
305c5c4113dSnw141292 	else
306651c0131Sbaban 		result->mappings.mappings_val[cb_data->next].direction =
307651c0131Sbaban 		    IDMAP_DIRECTION_BI;
308c5c4113dSnw141292 
309c5c4113dSnw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id1domain;
310c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[6], 0) != IDMAP_SUCCESS)
311c5c4113dSnw141292 		return (1);
312c5c4113dSnw141292 
313c5c4113dSnw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id1name;
314c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[7], 0) != IDMAP_SUCCESS)
315c5c4113dSnw141292 		return (1);
316c5c4113dSnw141292 
317c5c4113dSnw141292 	ptr = &result->mappings.mappings_val[cb_data->next].id2name;
318c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[8], 0) != IDMAP_SUCCESS)
319c5c4113dSnw141292 		return (1);
320c5c4113dSnw141292 
321c5c4113dSnw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
322c5c4113dSnw141292 	cb_data->next++;
323c5c4113dSnw141292 	result->retcode = IDMAP_SUCCESS;
324c5c4113dSnw141292 	return (0);
325c5c4113dSnw141292 }
326c5c4113dSnw141292 
327c5c4113dSnw141292 
328c5c4113dSnw141292 /* ARGSUSED */
329c5c4113dSnw141292 bool_t
330c5c4113dSnw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid,
331c5c4113dSnw141292 		uint64_t limit, idmap_mappings_res *result,
332c5c4113dSnw141292 		struct svc_req *rqstp) {
333c5c4113dSnw141292 	sqlite		*cache = NULL;
334c5c4113dSnw141292 	char		lbuf[30], rbuf[30];
335c5c4113dSnw141292 	uint64_t	maxlimit;
336c5c4113dSnw141292 	idmap_retcode	retcode;
337c5c4113dSnw141292 	char		*sql = NULL;
338c5c4113dSnw141292 
339c5c4113dSnw141292 	(void) memset(result, 0, sizeof (*result));
340c5c4113dSnw141292 	lbuf[0] = rbuf[0] = 0;
341c5c4113dSnw141292 
342c5c4113dSnw141292 	RDLOCK_CONFIG();
343c5c4113dSnw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
344c5c4113dSnw141292 	UNLOCK_CONFIG();
345c5c4113dSnw141292 
346c5c4113dSnw141292 	/* Get cache handle */
347c5c4113dSnw141292 	result->retcode = get_cache_handle(&cache);
348c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
349c5c4113dSnw141292 		goto out;
350c5c4113dSnw141292 
351c5c4113dSnw141292 	result->retcode = IDMAP_ERR_INTERNAL;
352c5c4113dSnw141292 
353c5c4113dSnw141292 	/* Create LIMIT expression. */
354c5c4113dSnw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
355c5c4113dSnw141292 		limit = maxlimit;
356c5c4113dSnw141292 	if (limit > 0)
357c5c4113dSnw141292 		(void) snprintf(lbuf, sizeof (lbuf),
358c5c4113dSnw141292 			"LIMIT %" PRIu64, limit + 1ULL);
359c5c4113dSnw141292 
360c5c4113dSnw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
361c5c4113dSnw141292 
362c5c4113dSnw141292 	/*
363c5c4113dSnw141292 	 * Combine all the above into a giant SELECT statement that
364c5c4113dSnw141292 	 * will return the requested mappings
365c5c4113dSnw141292 	 */
366c5c4113dSnw141292 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w,"
367c5c4113dSnw141292 			" windomain, winname, unixname"
368c5c4113dSnw141292 			" FROM idmap_cache WHERE "
369c5c4113dSnw141292 			" %s AND is_user = %d %s;",
370c5c4113dSnw141292 			rbuf, is_user?1:0, lbuf);
371c5c4113dSnw141292 	if (sql == NULL) {
372c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
373c5c4113dSnw141292 		goto out;
374c5c4113dSnw141292 	}
375c5c4113dSnw141292 
376c5c4113dSnw141292 	/* Execute the SQL statement and update the return buffer */
377c5c4113dSnw141292 	PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb,
378c5c4113dSnw141292 		result, result->mappings.mappings_len);
379c5c4113dSnw141292 
380c5c4113dSnw141292 out:
381c5c4113dSnw141292 	if (sql)
382c5c4113dSnw141292 		sqlite_freemem(sql);
383c5c4113dSnw141292 	if (IDMAP_ERROR(result->retcode))
384c5c4113dSnw141292 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
385c5c4113dSnw141292 	result->retcode = idmap_stat4prot(result->retcode);
386c5c4113dSnw141292 	return (TRUE);
387c5c4113dSnw141292 }
388c5c4113dSnw141292 
389c5c4113dSnw141292 
390c5c4113dSnw141292 /* ARGSUSED */
391c5c4113dSnw141292 static int
392c5c4113dSnw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) {
393c5c4113dSnw141292 	list_cb_data_t		*cb_data;
394c5c4113dSnw141292 	idmap_namerules_res	*result;
395c5c4113dSnw141292 	idmap_retcode		retcode;
396c5c4113dSnw141292 	idmap_utf8str		*ptr;
397c5c4113dSnw141292 	int			w2u_order, u2w_order;
398c5c4113dSnw141292 	char			*end;
399c5c4113dSnw141292 
400c5c4113dSnw141292 	cb_data = (list_cb_data_t *)parg;
401c5c4113dSnw141292 	result = (idmap_namerules_res *)cb_data->result;
402c5c4113dSnw141292 
403c5c4113dSnw141292 	_VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val,
404c5c4113dSnw141292 		sizeof (idmap_namerule));
405c5c4113dSnw141292 
406c5c4113dSnw141292 	result->rules.rules_len++;
407c5c4113dSnw141292 
408c5c4113dSnw141292 	result->rules.rules_val[cb_data->next].is_user =
409c5c4113dSnw141292 		strtol(argv[1], &end, 10);
410c5c4113dSnw141292 
411c5c4113dSnw141292 	ptr = &result->rules.rules_val[cb_data->next].windomain;
412c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[2], 0) != IDMAP_SUCCESS)
413c5c4113dSnw141292 		return (1);
414c5c4113dSnw141292 
415c5c4113dSnw141292 	ptr = &result->rules.rules_val[cb_data->next].winname;
416c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[3], 0) != IDMAP_SUCCESS)
417c5c4113dSnw141292 		return (1);
418c5c4113dSnw141292 
419c5c4113dSnw141292 	result->rules.rules_val[cb_data->next].is_nt4 =
420c5c4113dSnw141292 		strtol(argv[4], &end, 10);
421c5c4113dSnw141292 
422c5c4113dSnw141292 	ptr = &result->rules.rules_val[cb_data->next].unixname;
423c5c4113dSnw141292 	if (idmap_str2utf8(&ptr, argv[5], 0) != IDMAP_SUCCESS)
424c5c4113dSnw141292 		return (1);
425c5c4113dSnw141292 
426c5c4113dSnw141292 	w2u_order = argv[6]?strtol(argv[6], &end, 10):0;
427c5c4113dSnw141292 	u2w_order = argv[7]?strtol(argv[7], &end, 10):0;
428c5c4113dSnw141292 
429c5c4113dSnw141292 	if (w2u_order > 0 && u2w_order == 0)
430651c0131Sbaban 		result->rules.rules_val[cb_data->next].direction =
431651c0131Sbaban 		    IDMAP_DIRECTION_W2U;
432c5c4113dSnw141292 	else if (w2u_order == 0 && u2w_order > 0)
433651c0131Sbaban 		result->rules.rules_val[cb_data->next].direction =
434651c0131Sbaban 		    IDMAP_DIRECTION_U2W;
435c5c4113dSnw141292 	else
436651c0131Sbaban 		result->rules.rules_val[cb_data->next].direction =
437651c0131Sbaban 		    IDMAP_DIRECTION_BI;
438c5c4113dSnw141292 
439c5c4113dSnw141292 	result->lastrowid = strtoll(argv[0], &end, 10);
440c5c4113dSnw141292 	cb_data->next++;
441c5c4113dSnw141292 	result->retcode = IDMAP_SUCCESS;
442c5c4113dSnw141292 	return (0);
443c5c4113dSnw141292 }
444c5c4113dSnw141292 
445c5c4113dSnw141292 
446c5c4113dSnw141292 /* ARGSUSED */
447c5c4113dSnw141292 bool_t
448c5c4113dSnw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
449c5c4113dSnw141292 		uint64_t limit, idmap_namerules_res *result,
450c5c4113dSnw141292 		struct svc_req *rqstp) {
451c5c4113dSnw141292 
452c5c4113dSnw141292 	sqlite		*db = NULL;
453c5c4113dSnw141292 	char		w2ubuf[15], u2wbuf[15];
454c5c4113dSnw141292 	char		lbuf[30], rbuf[30];
455c5c4113dSnw141292 	char		*sql = NULL;
456c5c4113dSnw141292 	char		*s_windomain = NULL, *s_winname = NULL;
457c5c4113dSnw141292 	char		*s_unixname = NULL;
458c5c4113dSnw141292 	uint64_t	maxlimit;
459c5c4113dSnw141292 	idmap_retcode	retcode;
460c5c4113dSnw141292 
461c5c4113dSnw141292 	(void) memset(result, 0, sizeof (*result));
462c5c4113dSnw141292 	lbuf[0] = rbuf[0] = 0;
463c5c4113dSnw141292 
464c5c4113dSnw141292 	RDLOCK_CONFIG();
465c5c4113dSnw141292 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
466c5c4113dSnw141292 	UNLOCK_CONFIG();
467c5c4113dSnw141292 
468c5c4113dSnw141292 	/* Get db handle */
469c5c4113dSnw141292 	result->retcode = get_db_handle(&db);
470c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
471c5c4113dSnw141292 		goto out;
472c5c4113dSnw141292 
473c5c4113dSnw141292 	result->retcode = IDMAP_ERR_INTERNAL;
474c5c4113dSnw141292 
475c5c4113dSnw141292 	if (rule.direction < 0) {
476c5c4113dSnw141292 		w2ubuf[0] = u2wbuf[0] = 0;
477651c0131Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_BI) {
478c5c4113dSnw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
479c5c4113dSnw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
480651c0131Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
481c5c4113dSnw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
482c5c4113dSnw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
483c5c4113dSnw141292 				"AND (u2w_order = 0 OR u2w_order ISNULL)");
484651c0131Sbaban 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
485c5c4113dSnw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
486c5c4113dSnw141292 				"AND (w2u_order = 0 OR w2u_order ISNULL)");
487c5c4113dSnw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
488c5c4113dSnw141292 	}
489c5c4113dSnw141292 
490c5c4113dSnw141292 	/* Create where statement for windomain */
491c5c4113dSnw141292 	if (rule.windomain.idmap_utf8str_len > 0) {
492c5c4113dSnw141292 		if (gen_sql_expr_from_utf8str("AND", "windomain", "=",
493c5c4113dSnw141292 				&rule.windomain,
494c5c4113dSnw141292 				"", &s_windomain) != IDMAP_SUCCESS)
495c5c4113dSnw141292 			goto out;
496c5c4113dSnw141292 	}
497c5c4113dSnw141292 
498c5c4113dSnw141292 	/* Create where statement for winname */
499c5c4113dSnw141292 	if (rule.winname.idmap_utf8str_len > 0) {
500c5c4113dSnw141292 		if (gen_sql_expr_from_utf8str("AND", "winname", "=",
501c5c4113dSnw141292 				&rule.winname,
502c5c4113dSnw141292 				"", &s_winname) != IDMAP_SUCCESS)
503c5c4113dSnw141292 			goto out;
504c5c4113dSnw141292 	}
505c5c4113dSnw141292 
506c5c4113dSnw141292 	/* Create where statement for unixname */
507c5c4113dSnw141292 	if (rule.unixname.idmap_utf8str_len > 0) {
508c5c4113dSnw141292 		if (gen_sql_expr_from_utf8str("AND", "unixname", "=",
509c5c4113dSnw141292 				&rule.unixname,
510c5c4113dSnw141292 				"", &s_unixname) != IDMAP_SUCCESS)
511c5c4113dSnw141292 			goto out;
512c5c4113dSnw141292 	}
513c5c4113dSnw141292 
514c5c4113dSnw141292 	/* Create LIMIT expression. */
515c5c4113dSnw141292 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
516c5c4113dSnw141292 		limit = maxlimit;
517c5c4113dSnw141292 	if (limit > 0)
518c5c4113dSnw141292 		(void) snprintf(lbuf, sizeof (lbuf),
519c5c4113dSnw141292 			"LIMIT %" PRIu64, limit + 1ULL);
520c5c4113dSnw141292 
521c5c4113dSnw141292 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
522c5c4113dSnw141292 
523c5c4113dSnw141292 	/*
524c5c4113dSnw141292 	 * Combine all the above into a giant SELECT statement that
525c5c4113dSnw141292 	 * will return the requested rules
526c5c4113dSnw141292 	 */
527c5c4113dSnw141292 	sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, "
528c5c4113dSnw141292 			"is_nt4, unixname, w2u_order, u2w_order "
529c5c4113dSnw141292 			"FROM namerules WHERE "
530c5c4113dSnw141292 			" %s AND is_user = %d %s %s %s %s %s %s;",
531c5c4113dSnw141292 			rbuf, rule.is_user?1:0,
532c5c4113dSnw141292 			s_windomain?s_windomain:"",
533c5c4113dSnw141292 			s_winname?s_winname:"",
534c5c4113dSnw141292 			s_unixname?s_unixname:"",
535c5c4113dSnw141292 			w2ubuf, u2wbuf, lbuf);
536c5c4113dSnw141292 	if (sql == NULL) {
537c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
538c5c4113dSnw141292 		goto out;
539c5c4113dSnw141292 	}
540c5c4113dSnw141292 
541c5c4113dSnw141292 	/* Execute the SQL statement and update the return buffer */
542c5c4113dSnw141292 	PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb,
543c5c4113dSnw141292 		result, result->rules.rules_len);
544c5c4113dSnw141292 
545c5c4113dSnw141292 out:
546c5c4113dSnw141292 	if (s_windomain)
547c5c4113dSnw141292 		sqlite_freemem(s_windomain);
548c5c4113dSnw141292 	if (s_winname)
549c5c4113dSnw141292 		sqlite_freemem(s_winname);
550c5c4113dSnw141292 	if (s_unixname)
551c5c4113dSnw141292 		sqlite_freemem(s_unixname);
552c5c4113dSnw141292 	if (sql)
553c5c4113dSnw141292 		sqlite_freemem(sql);
554c5c4113dSnw141292 	if (IDMAP_ERROR(result->retcode))
555c5c4113dSnw141292 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
556c5c4113dSnw141292 	result->retcode = idmap_stat4prot(result->retcode);
557c5c4113dSnw141292 	return (TRUE);
558c5c4113dSnw141292 }
559c5c4113dSnw141292 
560c5c4113dSnw141292 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
561c5c4113dSnw141292 static int
562c5c4113dSnw141292 verify_rules_auth(struct svc_req *rqstp) {
563c5c4113dSnw141292 	ucred_t		*uc = NULL;
564c5c4113dSnw141292 	uid_t		uid;
565c5c4113dSnw141292 	char		buf[1024];
566c5c4113dSnw141292 	struct passwd	pwd;
567c5c4113dSnw141292 	const char	*me = "verify_rules_auth";
568c5c4113dSnw141292 
569c5c4113dSnw141292 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
570c5c4113dSnw141292 		idmapdlog(LOG_ERR,
571c5c4113dSnw141292 			"%s: svc_getcallerucred failed (errno=%d)",
572c5c4113dSnw141292 			me, errno);
573c5c4113dSnw141292 		return (-1);
574c5c4113dSnw141292 	}
575c5c4113dSnw141292 
576c5c4113dSnw141292 	uid = ucred_geteuid(uc);
577c5c4113dSnw141292 	if (uid == (uid_t)-1) {
578c5c4113dSnw141292 		idmapdlog(LOG_ERR,
579c5c4113dSnw141292 			"%s: ucred_geteuid failed (errno=%d)",
580c5c4113dSnw141292 			me, errno);
581c5c4113dSnw141292 		ucred_free(uc);
582c5c4113dSnw141292 		return (-1);
583c5c4113dSnw141292 	}
584c5c4113dSnw141292 
585c5c4113dSnw141292 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
586c5c4113dSnw141292 		idmapdlog(LOG_ERR,
587c5c4113dSnw141292 			"%s: getpwuid_r(%u) failed (errno=%d)",
588c5c4113dSnw141292 			me, uid, errno);
589c5c4113dSnw141292 		ucred_free(uc);
590c5c4113dSnw141292 		return (-1);
591c5c4113dSnw141292 	}
592c5c4113dSnw141292 
593c5c4113dSnw141292 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
594c5c4113dSnw141292 		idmapdlog(LOG_INFO,
595c5c4113dSnw141292 			"%s: %s does not have authorization.",
596c5c4113dSnw141292 			me, pwd.pw_name);
597c5c4113dSnw141292 		ucred_free(uc);
598c5c4113dSnw141292 		return (-1);
599c5c4113dSnw141292 	}
600c5c4113dSnw141292 
601c5c4113dSnw141292 	ucred_free(uc);
602c5c4113dSnw141292 	return (1);
603c5c4113dSnw141292 }
604c5c4113dSnw141292 
605c5c4113dSnw141292 /* ARGSUSED */
606c5c4113dSnw141292 bool_t
607c5c4113dSnw141292 idmap_update_1_svc(idmap_update_batch batch, idmap_retcode *result,
608c5c4113dSnw141292 		struct svc_req *rqstp) {
609c5c4113dSnw141292 	sqlite		*db = NULL;
610c5c4113dSnw141292 	idmap_update_op	*up;
611c5c4113dSnw141292 	int		i;
612*84decf41Sjp151216 	int		trans = FALSE;
613c5c4113dSnw141292 
614c5c4113dSnw141292 	if (verify_rules_auth(rqstp) < 0) {
615c5c4113dSnw141292 		*result = IDMAP_ERR_PERMISSION_DENIED;
616c5c4113dSnw141292 		goto out;
617c5c4113dSnw141292 	}
618c5c4113dSnw141292 
619c5c4113dSnw141292 	if (batch.idmap_update_batch_len == 0 ||
620c5c4113dSnw141292 			batch.idmap_update_batch_val == NULL) {
621c5c4113dSnw141292 		*result = IDMAP_SUCCESS;
622c5c4113dSnw141292 		goto out;
623c5c4113dSnw141292 	}
624c5c4113dSnw141292 
625c5c4113dSnw141292 	/* Get db handle */
626c5c4113dSnw141292 	*result = get_db_handle(&db);
627c5c4113dSnw141292 	if (*result != IDMAP_SUCCESS)
628c5c4113dSnw141292 		goto out;
629c5c4113dSnw141292 
630c5c4113dSnw141292 	*result = sql_exec_no_cb(db, "BEGIN TRANSACTION;");
631c5c4113dSnw141292 	if (*result != IDMAP_SUCCESS)
632c5c4113dSnw141292 		goto out;
633*84decf41Sjp151216 	trans = TRUE;
634c5c4113dSnw141292 
635c5c4113dSnw141292 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
636c5c4113dSnw141292 		up = &batch.idmap_update_batch_val[i];
637c5c4113dSnw141292 		switch (up->opnum) {
638c5c4113dSnw141292 		case OP_NONE:
639c5c4113dSnw141292 			*result = IDMAP_SUCCESS;
640c5c4113dSnw141292 			break;
641c5c4113dSnw141292 		case OP_ADD_NAMERULE:
642c5c4113dSnw141292 			*result = add_namerule(db,
643c5c4113dSnw141292 				&up->idmap_update_op_u.rule);
644c5c4113dSnw141292 			break;
645c5c4113dSnw141292 		case OP_RM_NAMERULE:
646c5c4113dSnw141292 			*result = rm_namerule(db,
647c5c4113dSnw141292 				&up->idmap_update_op_u.rule);
648c5c4113dSnw141292 			break;
649c5c4113dSnw141292 		case OP_FLUSH_NAMERULES:
650c5c4113dSnw141292 			*result = flush_namerules(db,
651c5c4113dSnw141292 				up->idmap_update_op_u.is_user);
652c5c4113dSnw141292 			break;
653c5c4113dSnw141292 		default:
654c5c4113dSnw141292 			*result = IDMAP_ERR_NOTSUPPORTED;
655c5c4113dSnw141292 			goto out;
656c5c4113dSnw141292 		};
657c5c4113dSnw141292 
658c5c4113dSnw141292 		if (*result != IDMAP_SUCCESS)
659c5c4113dSnw141292 			goto out;
660c5c4113dSnw141292 	}
661c5c4113dSnw141292 
662c5c4113dSnw141292 out:
663*84decf41Sjp151216 	if (trans) {
664*84decf41Sjp151216 		if (*result == IDMAP_SUCCESS)
665c5c4113dSnw141292 			*result = sql_exec_no_cb(db, "COMMIT TRANSACTION;");
666*84decf41Sjp151216 		else
667*84decf41Sjp151216 			(void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;");
668c5c4113dSnw141292 	}
669c5c4113dSnw141292 	*result = idmap_stat4prot(*result);
670c5c4113dSnw141292 	return (TRUE);
671c5c4113dSnw141292 }
672c5c4113dSnw141292 
673c5c4113dSnw141292 
674c5c4113dSnw141292 /* ARGSUSED */
675c5c4113dSnw141292 bool_t
676c5c4113dSnw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
677c5c4113dSnw141292 		idmap_mappings_res *result, struct svc_req *rqstp) {
678c5c4113dSnw141292 	sqlite		*cache = NULL, *db = NULL;
679c5c4113dSnw141292 
680c5c4113dSnw141292 	/* Init */
681c5c4113dSnw141292 	(void) memset(result, 0, sizeof (*result));
682c5c4113dSnw141292 
683c5c4113dSnw141292 	/* Get cache handle */
684c5c4113dSnw141292 	result->retcode = get_cache_handle(&cache);
685c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
686c5c4113dSnw141292 		goto out;
687c5c4113dSnw141292 
688c5c4113dSnw141292 	/* Get db handle */
689c5c4113dSnw141292 	result->retcode = get_db_handle(&db);
690c5c4113dSnw141292 	if (result->retcode != IDMAP_SUCCESS)
691c5c4113dSnw141292 		goto out;
692c5c4113dSnw141292 
693c5c4113dSnw141292 	/* Allocate result */
694c5c4113dSnw141292 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
695c5c4113dSnw141292 	if (result->mappings.mappings_val == NULL) {
696c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
697c5c4113dSnw141292 		result->retcode = IDMAP_ERR_MEMORY;
698c5c4113dSnw141292 		goto out;
699c5c4113dSnw141292 	}
700c5c4113dSnw141292 	result->mappings.mappings_len = 1;
701c5c4113dSnw141292 
702c5c4113dSnw141292 	if (IS_REQUEST_SID(request)) {
703c5c4113dSnw141292 		result->retcode = get_w2u_mapping(
704c5c4113dSnw141292 			cache,
705c5c4113dSnw141292 			db,
706c5c4113dSnw141292 			&request,
707c5c4113dSnw141292 			result->mappings.mappings_val);
708c5c4113dSnw141292 	} else if (IS_REQUEST_UID(request)) {
709c5c4113dSnw141292 		result->retcode = get_u2w_mapping(
710c5c4113dSnw141292 			cache,
711c5c4113dSnw141292 			db,
712c5c4113dSnw141292 			&request,
713c5c4113dSnw141292 			result->mappings.mappings_val,
714c5c4113dSnw141292 			1);
715c5c4113dSnw141292 	} else if (IS_REQUEST_GID(request)) {
716c5c4113dSnw141292 		result->retcode = get_u2w_mapping(
717c5c4113dSnw141292 			cache,
718c5c4113dSnw141292 			db,
719c5c4113dSnw141292 			&request,
720c5c4113dSnw141292 			result->mappings.mappings_val,
721c5c4113dSnw141292 			0);
722c5c4113dSnw141292 	} else {
723c5c4113dSnw141292 		result->retcode = IDMAP_ERR_IDTYPE;
724c5c4113dSnw141292 	}
725c5c4113dSnw141292 
726c5c4113dSnw141292 out:
727c5c4113dSnw141292 	if (IDMAP_FATAL_ERROR(result->retcode)) {
728c5c4113dSnw141292 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
729c5c4113dSnw141292 		result->mappings.mappings_len = 0;
730c5c4113dSnw141292 		result->mappings.mappings_val = NULL;
731c5c4113dSnw141292 	}
732c5c4113dSnw141292 	result->retcode = idmap_stat4prot(result->retcode);
733c5c4113dSnw141292 	return (TRUE);
734c5c4113dSnw141292 }
735c5c4113dSnw141292 
736c5c4113dSnw141292 
737c5c4113dSnw141292 /* ARGSUSED */
738c5c4113dSnw141292 int
739c5c4113dSnw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
740c5c4113dSnw141292 		caddr_t result) {
741c5c4113dSnw141292 	(void) xdr_free(xdr_result, result);
742c5c4113dSnw141292 	return (TRUE);
743c5c4113dSnw141292 }
744