xref: /titanic_50/usr/src/cmd/idmap/idmapd/server.c (revision d4517e84e3ffaa7bc4207a17f694c309904e7c48)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Service routines
30  */
31 
32 #include "idmapd.h"
33 #include "idmap_priv.h"
34 #include "nldaputils.h"
35 #include <signal.h>
36 #include <thread.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <errno.h>
40 #include <assert.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <ucred.h>
44 #include <pwd.h>
45 #include <auth_attr.h>
46 #include <secdb.h>
47 #include <sys/u8_textprep.h>
48 
49 #define	_VALIDATE_LIST_CB_DATA(col, val, siz)\
50 	retcode = validate_list_cb_data(cb_data, argc, argv, col,\
51 			(uchar_t **)val, siz);\
52 	if (retcode == IDMAP_NEXT) {\
53 		result->retcode = IDMAP_NEXT;\
54 		return (0);\
55 	} else if (retcode < 0) {\
56 		result->retcode = retcode;\
57 		return (1);\
58 	}
59 
60 #define	PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\
61 	rcode = process_list_svc_sql(db, sql, limit, cb, res);\
62 	if (rcode == IDMAP_ERR_BUSY)\
63 		res->retcode = IDMAP_ERR_BUSY;\
64 	else if (rcode == IDMAP_SUCCESS && len == 0)\
65 		res->retcode = IDMAP_ERR_NOTFOUND;
66 
67 
68 #define	STRDUP_OR_FAIL(to, from) \
69 	if ((from) == NULL) \
70 		to = NULL; \
71 	else { \
72 		if ((to = strdup(from)) == NULL) \
73 			return (1); \
74 	}
75 
76 /* ARGSUSED */
77 bool_t
78 idmap_null_1_svc(void *result, struct svc_req *rqstp)
79 {
80 	return (TRUE);
81 }
82 
83 /*
84  * RPC layer allocates empty strings to replace NULL char *.
85  * This utility function frees these empty strings.
86  */
87 static
88 void
89 sanitize_mapping_request(idmap_mapping *req)
90 {
91 	free(req->id1name);
92 	req->id1name = NULL;
93 	free(req->id1domain);
94 	req->id1domain = NULL;
95 	free(req->id2name);
96 	req->id2name = NULL;
97 	free(req->id2domain);
98 	req->id2domain = NULL;
99 	req->direction = _IDMAP_F_DONE;
100 }
101 
102 static
103 int
104 validate_mapped_id_by_name_req(idmap_mapping *req)
105 {
106 	int e;
107 
108 	if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req))
109 		return (IDMAP_SUCCESS);
110 
111 	if (IS_REQUEST_SID(*req, 1)) {
112 		if (!EMPTY_STRING(req->id1name) &&
113 		    u8_validate(req->id1name, strlen(req->id1name),
114 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
115 			return (IDMAP_ERR_BAD_UTF8);
116 		if (!EMPTY_STRING(req->id1domain) &&
117 		    u8_validate(req->id1domain, strlen(req->id1domain),
118 		    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
119 			return (IDMAP_ERR_BAD_UTF8);
120 	}
121 
122 	return (IDMAP_SUCCESS);
123 }
124 
125 static
126 int
127 validate_rule(idmap_namerule *rule)
128 {
129 	int e;
130 
131 	if (!EMPTY_STRING(rule->winname) &&
132 	    u8_validate(rule->winname, strlen(rule->winname),
133 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
134 		return (IDMAP_ERR_BAD_UTF8);
135 
136 	if (!EMPTY_STRING(rule->windomain) &&
137 	    u8_validate(rule->windomain, strlen(rule->windomain),
138 	    NULL, U8_VALIDATE_ENTIRE, &e) < 0)
139 		return (IDMAP_ERR_BAD_UTF8);
140 
141 	return (IDMAP_SUCCESS);
142 
143 }
144 
145 static
146 bool_t
147 validate_rules(idmap_update_batch *batch)
148 {
149 	idmap_update_op	*up;
150 	int i;
151 
152 	for (i = 0; i < batch->idmap_update_batch_len; i++) {
153 		up = &(batch->idmap_update_batch_val[i]);
154 		if (validate_rule(&(up->idmap_update_op_u.rule))
155 		    != IDMAP_SUCCESS)
156 			return (IDMAP_ERR_BAD_UTF8);
157 	}
158 
159 	return (IDMAP_SUCCESS);
160 }
161 
162 /* ARGSUSED */
163 bool_t
164 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch,
165 		idmap_ids_res *result, struct svc_req *rqstp)
166 {
167 	sqlite		*cache = NULL, *db = NULL;
168 	lookup_state_t	state;
169 	idmap_retcode	retcode;
170 	uint_t		i;
171 
172 	/* Init */
173 	(void) memset(result, 0, sizeof (*result));
174 	(void) memset(&state, 0, sizeof (state));
175 
176 	/* Return success if nothing was requested */
177 	if (batch.idmap_mapping_batch_len < 1)
178 		goto out;
179 
180 	/* Get cache handle */
181 	result->retcode = get_cache_handle(&cache);
182 	if (result->retcode != IDMAP_SUCCESS)
183 		goto out;
184 
185 	/* Get db handle */
186 	result->retcode = get_db_handle(&db);
187 	if (result->retcode != IDMAP_SUCCESS)
188 		goto out;
189 
190 	/* Allocate result array */
191 	result->ids.ids_val = calloc(batch.idmap_mapping_batch_len,
192 	    sizeof (idmap_id_res));
193 	if (result->ids.ids_val == NULL) {
194 		idmapdlog(LOG_ERR, "Out of memory");
195 		result->retcode = IDMAP_ERR_MEMORY;
196 		goto out;
197 	}
198 	result->ids.ids_len = batch.idmap_mapping_batch_len;
199 
200 	/* Allocate hash table to check for duplicate sids */
201 	state.sid_history = calloc(batch.idmap_mapping_batch_len,
202 	    sizeof (*state.sid_history));
203 	if (state.sid_history == NULL) {
204 		idmapdlog(LOG_ERR, "Out of memory");
205 		result->retcode = IDMAP_ERR_MEMORY;
206 		goto out;
207 	}
208 	state.sid_history_size = batch.idmap_mapping_batch_len;
209 	for (i = 0; i < state.sid_history_size; i++) {
210 		state.sid_history[i].key = state.sid_history_size;
211 		state.sid_history[i].next = state.sid_history_size;
212 	}
213 	state.batch = &batch;
214 	state.result = result;
215 
216 	/* Get directory-based name mapping info */
217 	result->retcode = get_ds_namemap_type(&state);
218 	if (result->retcode != IDMAP_SUCCESS)
219 		goto out;
220 
221 	/* Init our 'done' flags */
222 	state.sid2pid_done = state.pid2sid_done = TRUE;
223 
224 	/* First stage */
225 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
226 		state.curpos = i;
227 		(void) sanitize_mapping_request(
228 		    &batch.idmap_mapping_batch_val[i]);
229 		if (IS_BATCH_SID(batch, i)) {
230 			retcode = sid2pid_first_pass(
231 			    &state,
232 			    cache,
233 			    &batch.idmap_mapping_batch_val[i],
234 			    &result->ids.ids_val[i]);
235 		} else if (IS_BATCH_UID(batch, i)) {
236 			retcode = pid2sid_first_pass(
237 			    &state,
238 			    cache,
239 			    &batch.idmap_mapping_batch_val[i],
240 			    &result->ids.ids_val[i], 1, 0);
241 		} else if (IS_BATCH_GID(batch, i)) {
242 			retcode = pid2sid_first_pass(
243 			    &state,
244 			    cache,
245 			    &batch.idmap_mapping_batch_val[i],
246 			    &result->ids.ids_val[i], 0, 0);
247 		} else {
248 			result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE;
249 			continue;
250 		}
251 		if (IDMAP_FATAL_ERROR(retcode)) {
252 			result->retcode = retcode;
253 			goto out;
254 		}
255 	}
256 
257 	/* Check if we are done */
258 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
259 		goto out;
260 
261 	/*
262 	 * native LDAP lookups:
263 	 * If nldap or mixed mode is enabled then pid2sid mapping requests
264 	 * need to lookup native LDAP directory service by uid/gid to get
265 	 * winname and unixname.
266 	 */
267 	if (state.nldap_nqueries) {
268 		retcode = nldap_lookup_batch(&state, &batch, result);
269 		if (IDMAP_FATAL_ERROR(retcode)) {
270 			result->retcode = retcode;
271 			goto out;
272 		}
273 	}
274 
275 	/*
276 	 * AD lookups:
277 	 * 1. The pid2sid requests in the preceding step which successfully
278 	 *    retrieved winname from native LDAP objects will now need to
279 	 *    lookup AD by winname to get sid.
280 	 * 2. The sid2pid requests will need to lookup AD by sid to get
281 	 *    winname and unixname (AD or mixed mode).
282 	 * 3. If AD-based name mapping is enabled then pid2sid mapping
283 	 *    requests need to lookup AD by unixname to get winname and sid.
284 	 */
285 	if (state.ad_nqueries) {
286 		retcode = ad_lookup_batch(&state, &batch, result);
287 		if (IDMAP_FATAL_ERROR(retcode)) {
288 			result->retcode = retcode;
289 			goto out;
290 		}
291 	}
292 
293 	/*
294 	 * native LDAP lookups:
295 	 * If nldap mode is enabled then sid2pid mapping requests
296 	 * which successfully retrieved winname from AD objects in the
297 	 * preceding step, will now need to lookup native LDAP directory
298 	 * service by winname to get unixname and pid.
299 	 */
300 	if (state.nldap_nqueries) {
301 		retcode = nldap_lookup_batch(&state, &batch, result);
302 		if (IDMAP_FATAL_ERROR(retcode)) {
303 			result->retcode = retcode;
304 			goto out;
305 		}
306 	}
307 
308 	/* Reset 'done' flags */
309 	state.sid2pid_done = state.pid2sid_done = TRUE;
310 
311 	/* Second stage */
312 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
313 		state.curpos = i;
314 		if (IS_BATCH_SID(batch, i)) {
315 			retcode = sid2pid_second_pass(
316 			    &state,
317 			    cache,
318 			    db,
319 			    &batch.idmap_mapping_batch_val[i],
320 			    &result->ids.ids_val[i]);
321 		} else if (IS_BATCH_UID(batch, i)) {
322 			retcode = pid2sid_second_pass(
323 			    &state,
324 			    cache,
325 			    db,
326 			    &batch.idmap_mapping_batch_val[i],
327 			    &result->ids.ids_val[i], 1);
328 		} else if (IS_BATCH_GID(batch, i)) {
329 			retcode = pid2sid_second_pass(
330 			    &state,
331 			    cache,
332 			    db,
333 			    &batch.idmap_mapping_batch_val[i],
334 			    &result->ids.ids_val[i], 0);
335 		} else {
336 			/* First stage has already set the error */
337 			continue;
338 		}
339 		if (IDMAP_FATAL_ERROR(retcode)) {
340 			result->retcode = retcode;
341 			goto out;
342 		}
343 	}
344 
345 	/* Check if we are done */
346 	if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE)
347 		goto out;
348 
349 	/* Reset our 'done' flags */
350 	state.sid2pid_done = state.pid2sid_done = TRUE;
351 
352 	/* Update cache in a single transaction */
353 	if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS)
354 		goto out;
355 
356 	for (i = 0; i < batch.idmap_mapping_batch_len; i++) {
357 		state.curpos = i;
358 		if (IS_BATCH_SID(batch, i)) {
359 			(void) update_cache_sid2pid(
360 			    &state,
361 			    cache,
362 			    &batch.idmap_mapping_batch_val[i],
363 			    &result->ids.ids_val[i]);
364 		} else if ((IS_BATCH_UID(batch, i)) ||
365 		    (IS_BATCH_GID(batch, i))) {
366 			(void) update_cache_pid2sid(
367 			    &state,
368 			    cache,
369 			    &batch.idmap_mapping_batch_val[i],
370 			    &result->ids.ids_val[i]);
371 		}
372 	}
373 
374 	/* Commit if we have at least one successful update */
375 	if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE)
376 		(void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;");
377 	else
378 		(void) sql_exec_no_cb(cache, "END TRANSACTION;");
379 
380 out:
381 	cleanup_lookup_state(&state);
382 	if (IDMAP_ERROR(result->retcode)) {
383 		xdr_free(xdr_idmap_ids_res, (caddr_t)result);
384 		result->ids.ids_len = 0;
385 		result->ids.ids_val = NULL;
386 	}
387 	result->retcode = idmap_stat4prot(result->retcode);
388 	return (TRUE);
389 }
390 
391 
392 /* ARGSUSED */
393 static
394 int
395 list_mappings_cb(void *parg, int argc, char **argv, char **colnames)
396 {
397 	list_cb_data_t		*cb_data;
398 	char			*str;
399 	idmap_mappings_res	*result;
400 	idmap_retcode		retcode;
401 	int			w2u, u2w;
402 	char			*end;
403 	static int		validated_column_names = 0;
404 
405 	if (!validated_column_names) {
406 		assert(strcmp(colnames[0], "rowid") == 0);
407 		assert(strcmp(colnames[1], "sidprefix") == 0);
408 		assert(strcmp(colnames[2], "rid") == 0);
409 		assert(strcmp(colnames[3], "pid") == 0);
410 		assert(strcmp(colnames[4], "w2u") == 0);
411 		assert(strcmp(colnames[5], "u2w") == 0);
412 		assert(strcmp(colnames[6], "windomain") == 0);
413 		assert(strcmp(colnames[7], "canon_winname") == 0);
414 		assert(strcmp(colnames[8], "unixname") == 0);
415 		assert(strcmp(colnames[9], "is_user") == 0);
416 		assert(strcmp(colnames[10], "is_wuser") == 0);
417 		validated_column_names = 1;
418 	}
419 
420 
421 	cb_data = (list_cb_data_t *)parg;
422 	result = (idmap_mappings_res *)cb_data->result;
423 
424 	_VALIDATE_LIST_CB_DATA(11, &result->mappings.mappings_val,
425 	    sizeof (idmap_mapping));
426 
427 	result->mappings.mappings_len++;
428 
429 	if ((str = strdup(argv[1])) == NULL)
430 		return (1);
431 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix =
432 	    str;
433 	result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid =
434 	    strtoul(argv[2], &end, 10);
435 	result->mappings.mappings_val[cb_data->next].id1.idtype =
436 	    strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID;
437 
438 	result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid =
439 	    strtoul(argv[3], &end, 10);
440 	result->mappings.mappings_val[cb_data->next].id2.idtype =
441 	    strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID;
442 
443 	w2u = argv[4] ? strtol(argv[4], &end, 10) : 0;
444 	u2w = argv[5] ? strtol(argv[5], &end, 10) : 0;
445 
446 	if (w2u > 0 && u2w == 0)
447 		result->mappings.mappings_val[cb_data->next].direction =
448 		    IDMAP_DIRECTION_W2U;
449 	else if (w2u == 0 && u2w > 0)
450 		result->mappings.mappings_val[cb_data->next].direction =
451 		    IDMAP_DIRECTION_U2W;
452 	else
453 		result->mappings.mappings_val[cb_data->next].direction =
454 		    IDMAP_DIRECTION_BI;
455 
456 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain,
457 	    argv[6]);
458 
459 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name,
460 	    argv[7]);
461 
462 	STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name,
463 	    argv[8]);
464 
465 
466 	result->lastrowid = strtoll(argv[0], &end, 10);
467 	cb_data->next++;
468 	result->retcode = IDMAP_SUCCESS;
469 	return (0);
470 }
471 
472 
473 /* ARGSUSED */
474 bool_t
475 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit,
476     idmap_mappings_res *result, struct svc_req *rqstp)
477 {
478 	sqlite		*cache = NULL;
479 	char		lbuf[30], rbuf[30];
480 	uint64_t	maxlimit;
481 	idmap_retcode	retcode;
482 	char		*sql = NULL;
483 
484 	(void) memset(result, 0, sizeof (*result));
485 	lbuf[0] = rbuf[0] = 0;
486 
487 	RDLOCK_CONFIG();
488 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
489 	UNLOCK_CONFIG();
490 
491 	/* Get cache handle */
492 	result->retcode = get_cache_handle(&cache);
493 	if (result->retcode != IDMAP_SUCCESS)
494 		goto out;
495 
496 	result->retcode = IDMAP_ERR_INTERNAL;
497 
498 	/* Create LIMIT expression. */
499 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
500 		limit = maxlimit;
501 	if (limit > 0)
502 		(void) snprintf(lbuf, sizeof (lbuf),
503 		    "LIMIT %" PRIu64, limit + 1ULL);
504 
505 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
506 
507 	/*
508 	 * Combine all the above into a giant SELECT statement that
509 	 * will return the requested mappings
510 	 */
511 	sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w, "
512 	    "windomain, canon_winname, unixname, is_user, is_wuser "
513 	    " FROM idmap_cache WHERE "
514 	    " %s %s;",
515 	    rbuf, lbuf);
516 	if (sql == NULL) {
517 		idmapdlog(LOG_ERR, "Out of memory");
518 		goto out;
519 	}
520 
521 	/* Execute the SQL statement and update the return buffer */
522 	PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb,
523 	    result, result->mappings.mappings_len);
524 
525 out:
526 	if (sql)
527 		sqlite_freemem(sql);
528 	if (IDMAP_ERROR(result->retcode))
529 		(void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
530 	result->retcode = idmap_stat4prot(result->retcode);
531 	return (TRUE);
532 }
533 
534 
535 /* ARGSUSED */
536 static
537 int
538 list_namerules_cb(void *parg, int argc, char **argv, char **colnames)
539 {
540 	list_cb_data_t		*cb_data;
541 	idmap_namerules_res	*result;
542 	idmap_retcode		retcode;
543 	int			w2u_order, u2w_order;
544 	char			*end;
545 	static int		validated_column_names = 0;
546 
547 	if (!validated_column_names) {
548 		assert(strcmp(colnames[0], "rowid") == 0);
549 		assert(strcmp(colnames[1], "is_user") == 0);
550 		assert(strcmp(colnames[2], "is_wuser") == 0);
551 		assert(strcmp(colnames[3], "windomain") == 0);
552 		assert(strcmp(colnames[4], "winname_display") == 0);
553 		assert(strcmp(colnames[5], "is_nt4") == 0);
554 		assert(strcmp(colnames[6], "unixname") == 0);
555 		assert(strcmp(colnames[7], "w2u_order") == 0);
556 		assert(strcmp(colnames[8], "u2w_order") == 0);
557 		validated_column_names = 1;
558 	}
559 
560 	cb_data = (list_cb_data_t *)parg;
561 	result = (idmap_namerules_res *)cb_data->result;
562 
563 	_VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val,
564 	    sizeof (idmap_namerule));
565 
566 	result->rules.rules_len++;
567 
568 	result->rules.rules_val[cb_data->next].is_user =
569 	    strtol(argv[1], &end, 10);
570 
571 	result->rules.rules_val[cb_data->next].is_wuser =
572 	    strtol(argv[2], &end, 10);
573 
574 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain,
575 	    argv[3]);
576 
577 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname,
578 	    argv[4]);
579 
580 	result->rules.rules_val[cb_data->next].is_nt4 =
581 	    strtol(argv[5], &end, 10);
582 
583 	STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname,
584 	    argv[6]);
585 
586 	w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0;
587 	u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0;
588 
589 	if (w2u_order > 0 && u2w_order == 0)
590 		result->rules.rules_val[cb_data->next].direction =
591 		    IDMAP_DIRECTION_W2U;
592 	else if (w2u_order == 0 && u2w_order > 0)
593 		result->rules.rules_val[cb_data->next].direction =
594 		    IDMAP_DIRECTION_U2W;
595 	else
596 		result->rules.rules_val[cb_data->next].direction =
597 		    IDMAP_DIRECTION_BI;
598 
599 	result->lastrowid = strtoll(argv[0], &end, 10);
600 	cb_data->next++;
601 	result->retcode = IDMAP_SUCCESS;
602 	return (0);
603 }
604 
605 
606 /* ARGSUSED */
607 bool_t
608 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid,
609 		uint64_t limit, idmap_namerules_res *result,
610 		struct svc_req *rqstp)
611 {
612 
613 	sqlite		*db = NULL;
614 	char		w2ubuf[15], u2wbuf[15];
615 	char		lbuf[30], rbuf[30];
616 	char		*sql = NULL;
617 	char		*expr = NULL;
618 	uint64_t	maxlimit;
619 	idmap_retcode	retcode;
620 
621 	(void) memset(result, 0, sizeof (*result));
622 	lbuf[0] = rbuf[0] = 0;
623 
624 	result->retcode = validate_rule(&rule);
625 	if (result->retcode != IDMAP_SUCCESS)
626 		goto out;
627 
628 	RDLOCK_CONFIG();
629 	maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit;
630 	UNLOCK_CONFIG();
631 
632 	/* Get db handle */
633 	result->retcode = get_db_handle(&db);
634 	if (result->retcode != IDMAP_SUCCESS)
635 		goto out;
636 
637 	result->retcode = IDMAP_ERR_INTERNAL;
638 
639 	w2ubuf[0] = u2wbuf[0] = 0;
640 	if (rule.direction == IDMAP_DIRECTION_BI) {
641 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
642 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
643 	} else if (rule.direction == IDMAP_DIRECTION_W2U) {
644 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0");
645 		(void) snprintf(u2wbuf, sizeof (u2wbuf),
646 		    "AND (u2w_order = 0 OR u2w_order ISNULL)");
647 	} else if (rule.direction == IDMAP_DIRECTION_U2W) {
648 		(void) snprintf(w2ubuf, sizeof (w2ubuf),
649 		    "AND (w2u_order = 0 OR w2u_order ISNULL)");
650 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0");
651 	}
652 
653 	result->retcode = gen_sql_expr_from_rule(&rule, &expr);
654 	if (result->retcode != IDMAP_SUCCESS)
655 		goto out;
656 
657 	/* Create LIMIT expression. */
658 	if (limit == 0 || (maxlimit > 0 && maxlimit < limit))
659 		limit = maxlimit;
660 	if (limit > 0)
661 		(void) snprintf(lbuf, sizeof (lbuf),
662 		    "LIMIT %" PRIu64, limit + 1ULL);
663 
664 	(void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid);
665 
666 	/*
667 	 * Combine all the above into a giant SELECT statement that
668 	 * will return the requested rules
669 	 */
670 	sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, "
671 	    "winname_display, is_nt4, unixname, w2u_order, u2w_order "
672 	    "FROM namerules WHERE "
673 	    " %s %s %s %s %s;",
674 	    rbuf, expr, w2ubuf, u2wbuf, lbuf);
675 
676 	if (sql == NULL) {
677 		idmapdlog(LOG_ERR, "Out of memory");
678 		goto out;
679 	}
680 
681 	/* Execute the SQL statement and update the return buffer */
682 	PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb,
683 	    result, result->rules.rules_len);
684 
685 out:
686 	if (expr)
687 		sqlite_freemem(expr);
688 	if (sql)
689 		sqlite_freemem(sql);
690 	if (IDMAP_ERROR(result->retcode))
691 		(void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result);
692 	result->retcode = idmap_stat4prot(result->retcode);
693 	return (TRUE);
694 }
695 
696 #define	IDMAP_RULES_AUTH	"solaris.admin.idmap.rules"
697 static int
698 verify_rules_auth(struct svc_req *rqstp)
699 {
700 	ucred_t		*uc = NULL;
701 	uid_t		uid;
702 	char		buf[1024];
703 	struct passwd	pwd;
704 	const char	*me = "verify_rules_auth";
705 
706 	if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
707 		idmapdlog(LOG_ERR,
708 		    "%s: svc_getcallerucred failed (errno=%d)",
709 		    me, errno);
710 		return (-1);
711 	}
712 
713 	uid = ucred_geteuid(uc);
714 	if (uid == (uid_t)-1) {
715 		idmapdlog(LOG_ERR,
716 		    "%s: ucred_geteuid failed (errno=%d)",
717 		    me, errno);
718 		ucred_free(uc);
719 		return (-1);
720 	}
721 
722 	if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) {
723 		idmapdlog(LOG_ERR,
724 		    "%s: getpwuid_r(%u) failed (errno=%d)",
725 		    me, uid, errno);
726 		ucred_free(uc);
727 		return (-1);
728 	}
729 
730 	if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) {
731 		idmapdlog(LOG_INFO,
732 		    "%s: %s does not have authorization.",
733 		    me, pwd.pw_name);
734 		ucred_free(uc);
735 		return (-1);
736 	}
737 
738 	ucred_free(uc);
739 	return (1);
740 }
741 
742 /*
743  * Meaning of the return values is the following: For retcode ==
744  * IDMAP_SUCCESS, everything went OK and error_index is
745  * undefined. Otherwise, error_index >=0 shows the failed batch
746  * element. errro_index == -1 indicates failure at the beginning,
747  * error_index == -2 at the end.
748  */
749 
750 /* ARGSUSED */
751 bool_t
752 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res,
753 		struct svc_req *rqstp)
754 {
755 	sqlite		*db = NULL;
756 	idmap_update_op	*up;
757 	int		i;
758 	int		trans = FALSE;
759 
760 	res->error_index = -1;
761 	(void) memset(&res->error_rule, 0, sizeof (res->error_rule));
762 	(void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule));
763 
764 	if (verify_rules_auth(rqstp) < 0) {
765 		res->retcode = IDMAP_ERR_PERMISSION_DENIED;
766 		goto out;
767 	}
768 
769 	if (batch.idmap_update_batch_len == 0 ||
770 	    batch.idmap_update_batch_val == NULL) {
771 		res->retcode = IDMAP_SUCCESS;
772 		goto out;
773 	}
774 
775 	res->retcode = validate_rules(&batch);
776 	if (res->retcode != IDMAP_SUCCESS)
777 		goto out;
778 
779 	/* Get db handle */
780 	res->retcode = get_db_handle(&db);
781 	if (res->retcode != IDMAP_SUCCESS)
782 		goto out;
783 
784 	res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;");
785 	if (res->retcode != IDMAP_SUCCESS)
786 		goto out;
787 	trans = TRUE;
788 
789 	for (i = 0; i < batch.idmap_update_batch_len; i++) {
790 		up = &batch.idmap_update_batch_val[i];
791 		switch (up->opnum) {
792 		case OP_NONE:
793 			res->retcode = IDMAP_SUCCESS;
794 			break;
795 		case OP_ADD_NAMERULE:
796 			res->retcode = add_namerule(db,
797 			    &up->idmap_update_op_u.rule);
798 			break;
799 		case OP_RM_NAMERULE:
800 			res->retcode = rm_namerule(db,
801 			    &up->idmap_update_op_u.rule);
802 			break;
803 		case OP_FLUSH_NAMERULES:
804 			res->retcode = flush_namerules(db);
805 			break;
806 		default:
807 			res->retcode = IDMAP_ERR_NOTSUPPORTED;
808 			break;
809 		};
810 
811 		if (res->retcode != IDMAP_SUCCESS) {
812 			res->error_index = i;
813 			if (up->opnum == OP_ADD_NAMERULE ||
814 			    up->opnum == OP_RM_NAMERULE) {
815 				idmap_stat r2 =
816 				    idmap_namerule_cpy(&res->error_rule,
817 				    &up->idmap_update_op_u.rule);
818 				if (r2 != IDMAP_SUCCESS)
819 					res->retcode = r2;
820 			}
821 			goto out;
822 		}
823 	}
824 
825 out:
826 	if (trans) {
827 		if (res->retcode == IDMAP_SUCCESS) {
828 			res->retcode =
829 			    sql_exec_no_cb(db, "COMMIT TRANSACTION;");
830 			if (res->retcode !=  IDMAP_SUCCESS)
831 				res->error_index = -2;
832 		}
833 		else
834 			(void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;");
835 	}
836 
837 	res->retcode = idmap_stat4prot(res->retcode);
838 
839 	return (TRUE);
840 }
841 
842 
843 /* ARGSUSED */
844 bool_t
845 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request,
846 		idmap_mappings_res *result, struct svc_req *rqstp)
847 {
848 	sqlite		*cache = NULL, *db = NULL;
849 
850 	/* Init */
851 	(void) memset(result, 0, sizeof (*result));
852 
853 	result->retcode = validate_mapped_id_by_name_req(&request);
854 	if (result->retcode != IDMAP_SUCCESS)
855 		goto out;
856 
857 	/* Get cache handle */
858 	result->retcode = get_cache_handle(&cache);
859 	if (result->retcode != IDMAP_SUCCESS)
860 		goto out;
861 
862 	/* Get db handle */
863 	result->retcode = get_db_handle(&db);
864 	if (result->retcode != IDMAP_SUCCESS)
865 		goto out;
866 
867 	/* Allocate result */
868 	result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping));
869 	if (result->mappings.mappings_val == NULL) {
870 		idmapdlog(LOG_ERR, "Out of memory");
871 		result->retcode = IDMAP_ERR_MEMORY;
872 		goto out;
873 	}
874 	result->mappings.mappings_len = 1;
875 
876 
877 	if (IS_REQUEST_SID(request, 1)) {
878 		result->retcode = get_w2u_mapping(
879 		    cache,
880 		    db,
881 		    &request,
882 		    result->mappings.mappings_val);
883 	} else if (IS_REQUEST_UID(request)) {
884 		result->retcode = get_u2w_mapping(
885 		    cache,
886 		    db,
887 		    &request,
888 		    result->mappings.mappings_val,
889 		    1);
890 	} else if (IS_REQUEST_GID(request)) {
891 		result->retcode = get_u2w_mapping(
892 		    cache,
893 		    db,
894 		    &request,
895 		    result->mappings.mappings_val,
896 		    0);
897 	} else {
898 		result->retcode = IDMAP_ERR_IDTYPE;
899 	}
900 
901 out:
902 	if (IDMAP_FATAL_ERROR(result->retcode)) {
903 		xdr_free(xdr_idmap_mappings_res, (caddr_t)result);
904 		result->mappings.mappings_len = 0;
905 		result->mappings.mappings_val = NULL;
906 	}
907 	result->retcode = idmap_stat4prot(result->retcode);
908 	return (TRUE);
909 }
910 
911 
912 /* ARGSUSED */
913 int
914 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result,
915 		caddr_t result)
916 {
917 	(void) xdr_free(xdr_result, result);
918 	return (TRUE);
919 }
920