xref: /titanic_50/usr/src/cmd/idmap/idmapd/dbutils.c (revision 19449258028e6813f0b7a606b554b2fa37a390ec)
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 /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2348cd229bSGordon Ross  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24c5c4113dSnw141292  */
25c5c4113dSnw141292 
26c5c4113dSnw141292 /*
27c5c4113dSnw141292  * Database related utility routines
28c5c4113dSnw141292  */
29c5c4113dSnw141292 
30c5c4113dSnw141292 #include <stdio.h>
31c5c4113dSnw141292 #include <stdlib.h>
32c5c4113dSnw141292 #include <string.h>
33c5c4113dSnw141292 #include <errno.h>
34c5c4113dSnw141292 #include <sys/types.h>
35c5c4113dSnw141292 #include <sys/stat.h>
36c5c4113dSnw141292 #include <rpc/rpc.h>
37c5c4113dSnw141292 #include <sys/sid.h>
38c5c4113dSnw141292 #include <time.h>
39c5c4113dSnw141292 #include <pwd.h>
40c5c4113dSnw141292 #include <grp.h>
4184decf41Sjp151216 #include <pthread.h>
4284decf41Sjp151216 #include <assert.h>
43cd37da74Snw141292 #include <sys/u8_textprep.h>
448c155366SJordan Brown #include <alloca.h>
45cb174861Sjoyce mcintosh #include <libuutil.h>
46148c5f43SAlan Wright #include <note.h>
47c5c4113dSnw141292 
48c5c4113dSnw141292 #include "idmapd.h"
49c5c4113dSnw141292 #include "adutils.h"
50c5c4113dSnw141292 #include "string.h"
51c5c4113dSnw141292 #include "idmap_priv.h"
52cd37da74Snw141292 #include "schema.h"
53e8c27ec8Sbaban #include "nldaputils.h"
54148c5f43SAlan Wright #include "idmap_lsa.h"
55c5c4113dSnw141292 
5684decf41Sjp151216 
57c5c4113dSnw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *,
58c5c4113dSnw141292 		sqlite_vm **, int *, int, const char ***);
59e8c27ec8Sbaban static idmap_retcode lookup_localsid2pid(idmap_mapping *, idmap_id_res *);
60e8c27ec8Sbaban static idmap_retcode lookup_cache_name2sid(sqlite *, const char *,
61148c5f43SAlan Wright 	    const char *, char **, char **, idmap_rid_t *, idmap_id_type *);
62e8c27ec8Sbaban 
63c5c4113dSnw141292 #define	EMPTY_NAME(name)	(*name == 0 || strcmp(name, "\"\"") == 0)
64c5c4113dSnw141292 
65c5c4113dSnw141292 #define	DO_NOT_ALLOC_NEW_ID_MAPPING(req)\
66c5c4113dSnw141292 		(req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC)
67c5c4113dSnw141292 
68c5c4113dSnw141292 #define	AVOID_NAMESERVICE(req)\
69c5c4113dSnw141292 		(req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE)
70c5c4113dSnw141292 
712b4a7802SBaban Kenkre #define	ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)\
722b4a7802SBaban Kenkre 		(req->flag & IDMAP_REQ_FLG_WK_OR_LOCAL_SIDS_ONLY)
732b4a7802SBaban Kenkre 
74c5c4113dSnw141292 typedef enum init_db_option {
75c5c4113dSnw141292 	FAIL_IF_CORRUPT = 0,
76c5c4113dSnw141292 	REMOVE_IF_CORRUPT = 1
77c5c4113dSnw141292 } init_db_option_t;
78c5c4113dSnw141292 
7984decf41Sjp151216 /*
8008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Thread specific data to hold the database handles so that the
8108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * databases are not opened and closed for every request. It also
8284decf41Sjp151216  * contains the sqlite busy handler structure.
8384decf41Sjp151216  */
8484decf41Sjp151216 
8584decf41Sjp151216 struct idmap_busy {
8684decf41Sjp151216 	const char *name;
8784decf41Sjp151216 	const int *delays;
8884decf41Sjp151216 	int delay_size;
8984decf41Sjp151216 	int total;
9084decf41Sjp151216 	int sec;
9184decf41Sjp151216 };
9284decf41Sjp151216 
9384decf41Sjp151216 
9484decf41Sjp151216 typedef struct idmap_tsd {
9584decf41Sjp151216 	sqlite *db_db;
9684decf41Sjp151216 	sqlite *cache_db;
9784decf41Sjp151216 	struct idmap_busy cache_busy;
9884decf41Sjp151216 	struct idmap_busy db_busy;
9984decf41Sjp151216 } idmap_tsd_t;
10084decf41Sjp151216 
101e3f2c991SKeyur Desai /*
102e3f2c991SKeyur Desai  * Flags to indicate how local the directory we're consulting is.
103e3f2c991SKeyur Desai  * If neither is set, it means the directory belongs to a remote forest.
104e3f2c991SKeyur Desai  */
105e3f2c991SKeyur Desai #define	DOMAIN_IS_LOCAL	0x01
106e3f2c991SKeyur Desai #define	FOREST_IS_LOCAL	0x02
10784decf41Sjp151216 
10884decf41Sjp151216 static const int cache_delay_table[] =
10984decf41Sjp151216 		{ 1, 2, 5, 10, 15, 20, 25, 30,  35,  40,
11084decf41Sjp151216 		50,  50, 60, 70, 80, 90, 100};
11184decf41Sjp151216 
11284decf41Sjp151216 static const int db_delay_table[] =
11384decf41Sjp151216 		{ 5, 10, 15, 20, 30,  40,  55,  70, 100};
11484decf41Sjp151216 
11584decf41Sjp151216 
11684decf41Sjp151216 static pthread_key_t	idmap_tsd_key;
11784decf41Sjp151216 
11884decf41Sjp151216 void
idmap_tsd_destroy(void * key)11984decf41Sjp151216 idmap_tsd_destroy(void *key)
12084decf41Sjp151216 {
12184decf41Sjp151216 
12284decf41Sjp151216 	idmap_tsd_t	*tsd = (idmap_tsd_t *)key;
12384decf41Sjp151216 	if (tsd) {
12484decf41Sjp151216 		if (tsd->db_db)
12584decf41Sjp151216 			(void) sqlite_close(tsd->db_db);
12684decf41Sjp151216 		if (tsd->cache_db)
12784decf41Sjp151216 			(void) sqlite_close(tsd->cache_db);
12884decf41Sjp151216 		free(tsd);
12984decf41Sjp151216 	}
13084decf41Sjp151216 }
13184decf41Sjp151216 
132148c5f43SAlan Wright void
idmap_init_tsd_key(void)133cd37da74Snw141292 idmap_init_tsd_key(void)
134cd37da74Snw141292 {
135148c5f43SAlan Wright 	int rc;
136148c5f43SAlan Wright 
137148c5f43SAlan Wright 	rc = pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy);
138148c5f43SAlan Wright 	assert(rc == 0);
13984decf41Sjp151216 }
14084decf41Sjp151216 
14184decf41Sjp151216 
14284decf41Sjp151216 
14384decf41Sjp151216 idmap_tsd_t *
idmap_get_tsd(void)14484decf41Sjp151216 idmap_get_tsd(void)
14584decf41Sjp151216 {
14684decf41Sjp151216 	idmap_tsd_t	*tsd;
14784decf41Sjp151216 
14884decf41Sjp151216 	if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) {
14984decf41Sjp151216 		/* No thread specific data so create it */
15084decf41Sjp151216 		if ((tsd = malloc(sizeof (*tsd))) != NULL) {
15184decf41Sjp151216 			/* Initialize thread specific data */
15284decf41Sjp151216 			(void) memset(tsd, 0, sizeof (*tsd));
15384decf41Sjp151216 			/* save the trhread specific data */
15484decf41Sjp151216 			if (pthread_setspecific(idmap_tsd_key, tsd) != 0) {
15584decf41Sjp151216 				/* Can't store key */
15684decf41Sjp151216 				free(tsd);
15784decf41Sjp151216 				tsd = NULL;
15884decf41Sjp151216 			}
15984decf41Sjp151216 		} else {
16084decf41Sjp151216 			tsd = NULL;
16184decf41Sjp151216 		}
16284decf41Sjp151216 	}
16384decf41Sjp151216 
16484decf41Sjp151216 	return (tsd);
16584decf41Sjp151216 }
16684decf41Sjp151216 
167cd37da74Snw141292 /*
168cd37da74Snw141292  * A simple wrapper around u8_textprep_str() that returns the Unicode
169cd37da74Snw141292  * lower-case version of some string.  The result must be freed.
170cd37da74Snw141292  */
171cd37da74Snw141292 char *
tolower_u8(const char * s)172cd37da74Snw141292 tolower_u8(const char *s)
173cd37da74Snw141292 {
174cd37da74Snw141292 	char *res = NULL;
175cd37da74Snw141292 	char *outs;
176cd37da74Snw141292 	size_t inlen, outlen, inbytesleft, outbytesleft;
177cd37da74Snw141292 	int rc, err;
178cd37da74Snw141292 
179cd37da74Snw141292 	/*
180cd37da74Snw141292 	 * u8_textprep_str() does not allocate memory.  The input and
181cd37da74Snw141292 	 * output buffers may differ in size (though that would be more
182cd37da74Snw141292 	 * likely when normalization is done).  We have to loop over it...
183cd37da74Snw141292 	 *
184cd37da74Snw141292 	 * To improve the chances that we can avoid looping we add 10
185cd37da74Snw141292 	 * bytes of output buffer room the first go around.
186cd37da74Snw141292 	 */
187cd37da74Snw141292 	inlen = inbytesleft = strlen(s);
188cd37da74Snw141292 	outlen = outbytesleft = inlen + 10;
189cd37da74Snw141292 	if ((res = malloc(outlen)) == NULL)
190cd37da74Snw141292 		return (NULL);
191cd37da74Snw141292 	outs = res;
192cd37da74Snw141292 
193cd37da74Snw141292 	while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs,
194cd37da74Snw141292 	    &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 &&
195cd37da74Snw141292 	    err == E2BIG) {
196cd37da74Snw141292 		if ((res = realloc(res, outlen + inbytesleft)) == NULL)
197cd37da74Snw141292 			return (NULL);
198cd37da74Snw141292 		/* adjust input/output buffer pointers */
199cd37da74Snw141292 		s += (inlen - inbytesleft);
200cd37da74Snw141292 		outs = res + outlen - outbytesleft;
201cd37da74Snw141292 		/* adjust outbytesleft and outlen */
202cd37da74Snw141292 		outlen += inbytesleft;
203cd37da74Snw141292 		outbytesleft += inbytesleft;
204cd37da74Snw141292 	}
205cd37da74Snw141292 
206cd37da74Snw141292 	if (rc < 0) {
207cd37da74Snw141292 		free(res);
208cd37da74Snw141292 		res = NULL;
209cd37da74Snw141292 		return (NULL);
210cd37da74Snw141292 	}
211cd37da74Snw141292 
212cd37da74Snw141292 	res[outlen - outbytesleft] = '\0';
213cd37da74Snw141292 
214cd37da74Snw141292 	return (res);
215cd37da74Snw141292 }
216cd37da74Snw141292 
217cd37da74Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname,
218cd37da74Snw141292 	const char *while_doing);
219cd37da74Snw141292 
220c5c4113dSnw141292 
221c5c4113dSnw141292 /*
222c5c4113dSnw141292  * Initialize 'dbname' using 'sql'
223c5c4113dSnw141292  */
224cd37da74Snw141292 static
225cd37da74Snw141292 int
init_db_instance(const char * dbname,int version,const char * detect_version_sql,char * const * sql,init_db_option_t opt,int * created,int * upgraded)226cd37da74Snw141292 init_db_instance(const char *dbname, int version,
227cd37da74Snw141292 	const char *detect_version_sql, char * const *sql,
228cd37da74Snw141292 	init_db_option_t opt, int *created, int *upgraded)
229c5c4113dSnw141292 {
230cd37da74Snw141292 	int rc, curr_version;
231cd37da74Snw141292 	int tries = 1;
232cd37da74Snw141292 	int prio = LOG_NOTICE;
233c5c4113dSnw141292 	sqlite *db = NULL;
234cd37da74Snw141292 	char *errmsg = NULL;
235c5c4113dSnw141292 
236cd37da74Snw141292 	*created = 0;
237cd37da74Snw141292 	*upgraded = 0;
238c5c4113dSnw141292 
239cd37da74Snw141292 	if (opt == REMOVE_IF_CORRUPT)
240cd37da74Snw141292 		tries = 3;
241cd37da74Snw141292 
242cd37da74Snw141292 rinse_repeat:
243cd37da74Snw141292 	if (tries == 0) {
244cd37da74Snw141292 		idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname);
245c5c4113dSnw141292 		return (-1);
246cd37da74Snw141292 	}
247cd37da74Snw141292 	if (tries-- == 1)
248cd37da74Snw141292 		/* Last try, log errors */
249cd37da74Snw141292 		prio = LOG_ERR;
250c5c4113dSnw141292 
251cd37da74Snw141292 	db = sqlite_open(dbname, 0600, &errmsg);
252cd37da74Snw141292 	if (db == NULL) {
253cd37da74Snw141292 		idmapdlog(prio, "Error creating database %s (%s)",
254cd37da74Snw141292 		    dbname, CHECK_NULL(errmsg));
255cd37da74Snw141292 		sqlite_freemem(errmsg);
256cd37da74Snw141292 		if (opt == REMOVE_IF_CORRUPT)
257c5c4113dSnw141292 			(void) unlink(dbname);
258cd37da74Snw141292 		goto rinse_repeat;
259c5c4113dSnw141292 	}
260c5c4113dSnw141292 
261c5c4113dSnw141292 	sqlite_busy_timeout(db, 3000);
262cd37da74Snw141292 
263cd37da74Snw141292 	/* Detect current version of schema in the db, if any */
264cd37da74Snw141292 	curr_version = 0;
265cd37da74Snw141292 	if (detect_version_sql != NULL) {
266cd37da74Snw141292 		char *end, **results;
267cd37da74Snw141292 		int nrow;
268cd37da74Snw141292 
269cd37da74Snw141292 #ifdef	IDMAPD_DEBUG
270cd37da74Snw141292 		(void) fprintf(stderr, "Schema version detection SQL: %s\n",
271cd37da74Snw141292 		    detect_version_sql);
272cd37da74Snw141292 #endif	/* IDMAPD_DEBUG */
273cd37da74Snw141292 		rc = sqlite_get_table(db, detect_version_sql, &results,
274cd37da74Snw141292 		    &nrow, NULL, &errmsg);
275cd37da74Snw141292 		if (rc != SQLITE_OK) {
276cd37da74Snw141292 			idmapdlog(prio,
277cd37da74Snw141292 			    "Error detecting schema version of db %s (%s)",
278cd37da74Snw141292 			    dbname, errmsg);
279cd37da74Snw141292 			sqlite_freemem(errmsg);
280cd37da74Snw141292 			sqlite_free_table(results);
281c5c4113dSnw141292 			sqlite_close(db);
282cd37da74Snw141292 			return (-1);
283cd37da74Snw141292 		}
284cd37da74Snw141292 		if (nrow != 1) {
285cd37da74Snw141292 			idmapdlog(prio,
286cd37da74Snw141292 			    "Error detecting schema version of db %s", dbname);
287cd37da74Snw141292 			sqlite_close(db);
288cd37da74Snw141292 			sqlite_free_table(results);
289cd37da74Snw141292 			return (-1);
290cd37da74Snw141292 		}
291cd37da74Snw141292 		curr_version = strtol(results[1], &end, 10);
292cd37da74Snw141292 		sqlite_free_table(results);
293c5c4113dSnw141292 	}
294c5c4113dSnw141292 
295cd37da74Snw141292 	if (curr_version < 0) {
296cd37da74Snw141292 		if (opt == REMOVE_IF_CORRUPT)
297cd37da74Snw141292 			(void) unlink(dbname);
298cd37da74Snw141292 		goto rinse_repeat;
299c5c4113dSnw141292 	}
300c5c4113dSnw141292 
301cd37da74Snw141292 	if (curr_version == version)
302cd37da74Snw141292 		goto done;
303cd37da74Snw141292 
304cd37da74Snw141292 	/* Install or upgrade schema */
305cd37da74Snw141292 #ifdef	IDMAPD_DEBUG
306cd37da74Snw141292 	(void) fprintf(stderr, "Schema init/upgrade SQL: %s\n",
307cd37da74Snw141292 	    sql[curr_version]);
308cd37da74Snw141292 #endif	/* IDMAPD_DEBUG */
309cd37da74Snw141292 	rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname,
310cd37da74Snw141292 	    (curr_version == 0) ? "installing schema" : "upgrading schema");
311cd37da74Snw141292 	if (rc != 0) {
312cd37da74Snw141292 		idmapdlog(prio, "Error %s schema for db %s", dbname,
313cd37da74Snw141292 		    (curr_version == 0) ? "installing schema" :
314cd37da74Snw141292 		    "upgrading schema");
315cd37da74Snw141292 		if (opt == REMOVE_IF_CORRUPT)
316cd37da74Snw141292 			(void) unlink(dbname);
317cd37da74Snw141292 		goto rinse_repeat;
318c5c4113dSnw141292 	}
319c5c4113dSnw141292 
320cd37da74Snw141292 	*upgraded = (curr_version > 0);
321cd37da74Snw141292 	*created = (curr_version == 0);
322cd37da74Snw141292 
323cd37da74Snw141292 done:
324c5c4113dSnw141292 	(void) sqlite_close(db);
325cd37da74Snw141292 	return (0);
326c5c4113dSnw141292 }
327c5c4113dSnw141292 
32884decf41Sjp151216 
32984decf41Sjp151216 /*
33084decf41Sjp151216  * This is the SQLite database busy handler that retries the SQL
33184decf41Sjp151216  * operation until it is successful.
33284decf41Sjp151216  */
33384decf41Sjp151216 int
33484decf41Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */
idmap_sqlite_busy_handler(void * arg,const char * table_name,int count)33584decf41Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count)
33684decf41Sjp151216 {
33784decf41Sjp151216 	struct idmap_busy	*busy = arg;
33884decf41Sjp151216 	int			delay;
33984decf41Sjp151216 	struct timespec		rqtp;
34084decf41Sjp151216 
34184decf41Sjp151216 	if (count == 1)  {
34284decf41Sjp151216 		busy->total = 0;
34384decf41Sjp151216 		busy->sec = 2;
34484decf41Sjp151216 	}
34584decf41Sjp151216 	if (busy->total > 1000 * busy->sec) {
3462b3ecdebSjp151216 		idmapdlog(LOG_DEBUG,
34784decf41Sjp151216 		    "Thread %d waited %d sec for the %s database",
34884decf41Sjp151216 		    pthread_self(), busy->sec, busy->name);
34984decf41Sjp151216 		busy->sec++;
35084decf41Sjp151216 	}
35184decf41Sjp151216 
35284decf41Sjp151216 	if (count <= busy->delay_size) {
35384decf41Sjp151216 		delay = busy->delays[count-1];
35484decf41Sjp151216 	} else {
35584decf41Sjp151216 		delay = busy->delays[busy->delay_size - 1];
35684decf41Sjp151216 	}
35784decf41Sjp151216 	busy->total += delay;
35884decf41Sjp151216 	rqtp.tv_sec = 0;
359*19449258SJosef 'Jeff' Sipek 	rqtp.tv_nsec = MSEC2NSEC(delay);
36084decf41Sjp151216 	(void) nanosleep(&rqtp, NULL);
36184decf41Sjp151216 	return (1);
36284decf41Sjp151216 }
36384decf41Sjp151216 
36484decf41Sjp151216 
365c5c4113dSnw141292 /*
366c5c4113dSnw141292  * Get the database handle
367c5c4113dSnw141292  */
368c5c4113dSnw141292 idmap_retcode
get_db_handle(sqlite ** db)369cd37da74Snw141292 get_db_handle(sqlite **db)
370cd37da74Snw141292 {
371c5c4113dSnw141292 	char		*errmsg;
37284decf41Sjp151216 	idmap_tsd_t	*tsd;
373c5c4113dSnw141292 
374c5c4113dSnw141292 	/*
37584decf41Sjp151216 	 * Retrieve the db handle from thread-specific storage
376c5c4113dSnw141292 	 * If none exists, open and store in thread-specific storage.
377c5c4113dSnw141292 	 */
37884decf41Sjp151216 	if ((tsd = idmap_get_tsd()) == NULL) {
37984decf41Sjp151216 		idmapdlog(LOG_ERR,
380cd37da74Snw141292 		    "Error getting thread specific data for %s", IDMAP_DBNAME);
38184decf41Sjp151216 		return (IDMAP_ERR_MEMORY);
38284decf41Sjp151216 	}
383c5c4113dSnw141292 
38484decf41Sjp151216 	if (tsd->db_db == NULL) {
38584decf41Sjp151216 		tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg);
38684decf41Sjp151216 		if (tsd->db_db == NULL) {
387cd37da74Snw141292 			idmapdlog(LOG_ERR, "Error opening database %s (%s)",
388c5c4113dSnw141292 			    IDMAP_DBNAME, CHECK_NULL(errmsg));
389c5c4113dSnw141292 			sqlite_freemem(errmsg);
390cd37da74Snw141292 			return (IDMAP_ERR_DB);
391c5c4113dSnw141292 		}
392cd37da74Snw141292 
39384decf41Sjp151216 		tsd->db_busy.name = IDMAP_DBNAME;
39484decf41Sjp151216 		tsd->db_busy.delays = db_delay_table;
39584decf41Sjp151216 		tsd->db_busy.delay_size = sizeof (db_delay_table) /
39684decf41Sjp151216 		    sizeof (int);
39784decf41Sjp151216 		sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler,
39884decf41Sjp151216 		    &tsd->db_busy);
39984decf41Sjp151216 	}
40084decf41Sjp151216 	*db = tsd->db_db;
401c5c4113dSnw141292 	return (IDMAP_SUCCESS);
402c5c4113dSnw141292 }
403c5c4113dSnw141292 
404c5c4113dSnw141292 /*
405c5c4113dSnw141292  * Get the cache handle
406c5c4113dSnw141292  */
407c5c4113dSnw141292 idmap_retcode
get_cache_handle(sqlite ** cache)408cd37da74Snw141292 get_cache_handle(sqlite **cache)
409cd37da74Snw141292 {
410c5c4113dSnw141292 	char		*errmsg;
41184decf41Sjp151216 	idmap_tsd_t	*tsd;
412c5c4113dSnw141292 
413c5c4113dSnw141292 	/*
41484decf41Sjp151216 	 * Retrieve the db handle from thread-specific storage
415c5c4113dSnw141292 	 * If none exists, open and store in thread-specific storage.
416c5c4113dSnw141292 	 */
41784decf41Sjp151216 	if ((tsd = idmap_get_tsd()) == NULL) {
418cd37da74Snw141292 		idmapdlog(LOG_ERR, "Error getting thread specific data for %s",
41984decf41Sjp151216 		    IDMAP_DBNAME);
42084decf41Sjp151216 		return (IDMAP_ERR_MEMORY);
42184decf41Sjp151216 	}
422c5c4113dSnw141292 
42384decf41Sjp151216 	if (tsd->cache_db == NULL) {
42484decf41Sjp151216 		tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg);
42584decf41Sjp151216 		if (tsd->cache_db == NULL) {
426cd37da74Snw141292 			idmapdlog(LOG_ERR, "Error opening database %s (%s)",
427c5c4113dSnw141292 			    IDMAP_CACHENAME, CHECK_NULL(errmsg));
428c5c4113dSnw141292 			sqlite_freemem(errmsg);
429cd37da74Snw141292 			return (IDMAP_ERR_DB);
430c5c4113dSnw141292 		}
431cd37da74Snw141292 
43284decf41Sjp151216 		tsd->cache_busy.name = IDMAP_CACHENAME;
43384decf41Sjp151216 		tsd->cache_busy.delays = cache_delay_table;
43484decf41Sjp151216 		tsd->cache_busy.delay_size = sizeof (cache_delay_table) /
43584decf41Sjp151216 		    sizeof (int);
43684decf41Sjp151216 		sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler,
43784decf41Sjp151216 		    &tsd->cache_busy);
43884decf41Sjp151216 	}
43984decf41Sjp151216 	*cache = tsd->cache_db;
440c5c4113dSnw141292 	return (IDMAP_SUCCESS);
441c5c4113dSnw141292 }
442c5c4113dSnw141292 
443c5c4113dSnw141292 /*
444c5c4113dSnw141292  * Initialize cache and db
445c5c4113dSnw141292  */
446c5c4113dSnw141292 int
init_dbs()447cd37da74Snw141292 init_dbs()
448cd37da74Snw141292 {
44948258c6bSjp151216 	char *sql[4];
450cd37da74Snw141292 	int created, upgraded;
451cd37da74Snw141292 
452c5c4113dSnw141292 	/* name-based mappings; probably OK to blow away in a pinch(?) */
453cd37da74Snw141292 	sql[0] = DB_INSTALL_SQL;
454cd37da74Snw141292 	sql[1] = DB_UPGRADE_FROM_v1_SQL;
45548258c6bSjp151216 	sql[2] = NULL;
456cd37da74Snw141292 
457cd37da74Snw141292 	if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql,
458cd37da74Snw141292 	    FAIL_IF_CORRUPT, &created, &upgraded) < 0)
459c5c4113dSnw141292 		return (-1);
460c5c4113dSnw141292 
461c5c4113dSnw141292 	/* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */
462cd37da74Snw141292 	sql[0] = CACHE_INSTALL_SQL;
463cd37da74Snw141292 	sql[1] = CACHE_UPGRADE_FROM_v1_SQL;
46448258c6bSjp151216 	sql[2] = CACHE_UPGRADE_FROM_v2_SQL;
46548258c6bSjp151216 	sql[3] = NULL;
46648258c6bSjp151216 
467cd37da74Snw141292 	if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL,
468cd37da74Snw141292 	    sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0)
469c5c4113dSnw141292 		return (-1);
470c5c4113dSnw141292 
471cd37da74Snw141292 	_idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0;
472cd37da74Snw141292 
473c5c4113dSnw141292 	return (0);
474c5c4113dSnw141292 }
475c5c4113dSnw141292 
476c5c4113dSnw141292 /*
477c5c4113dSnw141292  * Finalize databases
478c5c4113dSnw141292  */
479c5c4113dSnw141292 void
fini_dbs()480cd37da74Snw141292 fini_dbs()
481cd37da74Snw141292 {
482c5c4113dSnw141292 }
483c5c4113dSnw141292 
484c5c4113dSnw141292 /*
485e8c27ec8Sbaban  * This table is a listing of status codes that will be returned to the
486c5c4113dSnw141292  * client when a SQL command fails with the corresponding error message.
487c5c4113dSnw141292  */
488c5c4113dSnw141292 static msg_table_t sqlmsgtable[] = {
48962c60062Sbaban 	{IDMAP_ERR_U2W_NAMERULE_CONFLICT,
490c5c4113dSnw141292 	"columns unixname, is_user, u2w_order are not unique"},
49162c60062Sbaban 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT,
492cd37da74Snw141292 	"columns winname, windomain, is_user, is_wuser, w2u_order are not"
493cd37da74Snw141292 	" unique"},
494cd37da74Snw141292 	{IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"},
495c5c4113dSnw141292 	{-1, NULL}
496c5c4113dSnw141292 };
497c5c4113dSnw141292 
498c5c4113dSnw141292 /*
499c5c4113dSnw141292  * idmapd's version of string2stat to map SQLite messages to
500c5c4113dSnw141292  * status codes
501c5c4113dSnw141292  */
502c5c4113dSnw141292 idmap_retcode
idmapd_string2stat(const char * msg)503cd37da74Snw141292 idmapd_string2stat(const char *msg)
504cd37da74Snw141292 {
505c5c4113dSnw141292 	int i;
506c5c4113dSnw141292 	for (i = 0; sqlmsgtable[i].msg; i++) {
507c5c4113dSnw141292 		if (strcasecmp(sqlmsgtable[i].msg, msg) == 0)
508c5c4113dSnw141292 			return (sqlmsgtable[i].retcode);
509c5c4113dSnw141292 	}
510c5c4113dSnw141292 	return (IDMAP_ERR_OTHER);
511c5c4113dSnw141292 }
512c5c4113dSnw141292 
513c5c4113dSnw141292 /*
514cd37da74Snw141292  * Executes some SQL in a transaction.
515cd37da74Snw141292  *
516cd37da74Snw141292  * Returns 0 on success, -1 if it failed but the rollback succeeded, -2
517cd37da74Snw141292  * if the rollback failed.
518cd37da74Snw141292  */
519cd37da74Snw141292 static
520cd37da74Snw141292 int
sql_exec_tran_no_cb(sqlite * db,char * sql,const char * dbname,const char * while_doing)521cd37da74Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname,
522cd37da74Snw141292 	const char *while_doing)
523cd37da74Snw141292 {
524cd37da74Snw141292 	char		*errmsg = NULL;
525cd37da74Snw141292 	int		rc;
526cd37da74Snw141292 
527cd37da74Snw141292 	rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg);
528cd37da74Snw141292 	if (rc != SQLITE_OK) {
529cd37da74Snw141292 		idmapdlog(LOG_ERR, "Begin transaction failed (%s) "
530cd37da74Snw141292 		    "while %s (%s)", errmsg, while_doing, dbname);
531cd37da74Snw141292 		sqlite_freemem(errmsg);
532cd37da74Snw141292 		return (-1);
533cd37da74Snw141292 	}
534cd37da74Snw141292 
535cd37da74Snw141292 	rc = sqlite_exec(db, sql, NULL, NULL, &errmsg);
536cd37da74Snw141292 	if (rc != SQLITE_OK) {
537cd37da74Snw141292 		idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg,
538cd37da74Snw141292 		    while_doing, dbname);
539cd37da74Snw141292 		sqlite_freemem(errmsg);
540cd37da74Snw141292 		errmsg = NULL;
541cd37da74Snw141292 		goto rollback;
542cd37da74Snw141292 	}
543cd37da74Snw141292 
544cd37da74Snw141292 	rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg);
545cd37da74Snw141292 	if (rc == SQLITE_OK) {
546cd37da74Snw141292 		sqlite_freemem(errmsg);
547cd37da74Snw141292 		return (0);
548cd37da74Snw141292 	}
549cd37da74Snw141292 
550cd37da74Snw141292 	idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)",
551cd37da74Snw141292 	    errmsg, while_doing, dbname);
552cd37da74Snw141292 	sqlite_freemem(errmsg);
553cd37da74Snw141292 	errmsg = NULL;
554cd37da74Snw141292 
555cd37da74Snw141292 rollback:
556cd37da74Snw141292 	rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg);
557cd37da74Snw141292 	if (rc != SQLITE_OK) {
558cd37da74Snw141292 		idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)",
559cd37da74Snw141292 		    errmsg, while_doing, dbname);
560cd37da74Snw141292 		sqlite_freemem(errmsg);
561cd37da74Snw141292 		return (-2);
562cd37da74Snw141292 	}
563cd37da74Snw141292 	sqlite_freemem(errmsg);
564cd37da74Snw141292 
565cd37da74Snw141292 	return (-1);
566cd37da74Snw141292 }
567cd37da74Snw141292 
568cd37da74Snw141292 /*
569c5c4113dSnw141292  * Execute the given SQL statment without using any callbacks
570c5c4113dSnw141292  */
571c5c4113dSnw141292 idmap_retcode
sql_exec_no_cb(sqlite * db,const char * dbname,char * sql)57271590c90Snw141292 sql_exec_no_cb(sqlite *db, const char *dbname, char *sql)
573cd37da74Snw141292 {
574c5c4113dSnw141292 	char		*errmsg = NULL;
57584decf41Sjp151216 	int		r;
576c5c4113dSnw141292 	idmap_retcode	retcode;
577c5c4113dSnw141292 
578c5c4113dSnw141292 	r = sqlite_exec(db, sql, NULL, NULL, &errmsg);
57984decf41Sjp151216 	assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
580c5c4113dSnw141292 
581c5c4113dSnw141292 	if (r != SQLITE_OK) {
58271590c90Snw141292 		idmapdlog(LOG_ERR, "Database error on %s while executing %s "
58371590c90Snw141292 		    "(%s)", dbname, sql, CHECK_NULL(errmsg));
584c5c4113dSnw141292 		retcode = idmapd_string2stat(errmsg);
58562c60062Sbaban 		if (errmsg != NULL)
586c5c4113dSnw141292 			sqlite_freemem(errmsg);
587c5c4113dSnw141292 		return (retcode);
588c5c4113dSnw141292 	}
589c5c4113dSnw141292 
590c5c4113dSnw141292 	return (IDMAP_SUCCESS);
591c5c4113dSnw141292 }
592c5c4113dSnw141292 
593c5c4113dSnw141292 /*
594c5c4113dSnw141292  * Generate expression that can be used in WHERE statements.
595c5c4113dSnw141292  * Examples:
596c5c4113dSnw141292  * <prefix> <col>      <op> <value>   <suffix>
597c5c4113dSnw141292  * ""       "unixuser" "="  "foo" "AND"
598c5c4113dSnw141292  */
599c5c4113dSnw141292 idmap_retcode
gen_sql_expr_from_rule(idmap_namerule * rule,char ** out)600cd37da74Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out)
601cd37da74Snw141292 {
602cd37da74Snw141292 	char	*s_windomain = NULL, *s_winname = NULL;
603cd37da74Snw141292 	char	*s_unixname = NULL;
604bbf6f00cSJordan Brown 	char	*dir;
605cd37da74Snw141292 	char	*lower_winname;
606cd37da74Snw141292 	int	retcode = IDMAP_SUCCESS;
607cd37da74Snw141292 
608c5c4113dSnw141292 	if (out == NULL)
609c5c4113dSnw141292 		return (IDMAP_ERR_ARG);
610c5c4113dSnw141292 
611c5c4113dSnw141292 
612cd37da74Snw141292 	if (!EMPTY_STRING(rule->windomain)) {
613cd37da74Snw141292 		s_windomain =  sqlite_mprintf("AND windomain = %Q ",
614cd37da74Snw141292 		    rule->windomain);
615cd37da74Snw141292 		if (s_windomain == NULL) {
616cd37da74Snw141292 			retcode = IDMAP_ERR_MEMORY;
617cd37da74Snw141292 			goto out;
618c5c4113dSnw141292 		}
619cd37da74Snw141292 	}
620cd37da74Snw141292 
621cd37da74Snw141292 	if (!EMPTY_STRING(rule->winname)) {
622cd37da74Snw141292 		if ((lower_winname = tolower_u8(rule->winname)) == NULL)
623cd37da74Snw141292 			lower_winname = rule->winname;
624cd37da74Snw141292 		s_winname = sqlite_mprintf(
625cd37da74Snw141292 		    "AND winname = %Q AND is_wuser = %d ",
626cd37da74Snw141292 		    lower_winname, rule->is_wuser ? 1 : 0);
627cd37da74Snw141292 		if (lower_winname != rule->winname)
628cd37da74Snw141292 			free(lower_winname);
629cd37da74Snw141292 		if (s_winname == NULL) {
630cd37da74Snw141292 			retcode = IDMAP_ERR_MEMORY;
631cd37da74Snw141292 			goto out;
632cd37da74Snw141292 		}
633cd37da74Snw141292 	}
634cd37da74Snw141292 
635cd37da74Snw141292 	if (!EMPTY_STRING(rule->unixname)) {
636cd37da74Snw141292 		s_unixname = sqlite_mprintf(
637cd37da74Snw141292 		    "AND unixname = %Q AND is_user = %d ",
638cd37da74Snw141292 		    rule->unixname, rule->is_user ? 1 : 0);
639cd37da74Snw141292 		if (s_unixname == NULL) {
640cd37da74Snw141292 			retcode = IDMAP_ERR_MEMORY;
641cd37da74Snw141292 			goto out;
642cd37da74Snw141292 		}
643cd37da74Snw141292 	}
644cd37da74Snw141292 
645bbf6f00cSJordan Brown 	switch (rule->direction) {
646bbf6f00cSJordan Brown 	case IDMAP_DIRECTION_BI:
647bbf6f00cSJordan Brown 		dir = "AND w2u_order > 0 AND u2w_order > 0";
648bbf6f00cSJordan Brown 		break;
649bbf6f00cSJordan Brown 	case IDMAP_DIRECTION_W2U:
650bbf6f00cSJordan Brown 		dir = "AND w2u_order > 0"
651bbf6f00cSJordan Brown 		    " AND (u2w_order = 0 OR u2w_order ISNULL)";
652bbf6f00cSJordan Brown 		break;
653bbf6f00cSJordan Brown 	case IDMAP_DIRECTION_U2W:
654bbf6f00cSJordan Brown 		dir = "AND u2w_order > 0"
655bbf6f00cSJordan Brown 		    " AND (w2u_order = 0 OR w2u_order ISNULL)";
656bbf6f00cSJordan Brown 		break;
657bbf6f00cSJordan Brown 	default:
658bbf6f00cSJordan Brown 		dir = "";
659bbf6f00cSJordan Brown 		break;
660bbf6f00cSJordan Brown 	}
661bbf6f00cSJordan Brown 
662bbf6f00cSJordan Brown 	*out = sqlite_mprintf("%s %s %s %s",
663cd37da74Snw141292 	    s_windomain ? s_windomain : "",
664cd37da74Snw141292 	    s_winname ? s_winname : "",
665bbf6f00cSJordan Brown 	    s_unixname ? s_unixname : "",
666bbf6f00cSJordan Brown 	    dir);
667cd37da74Snw141292 
668cd37da74Snw141292 	if (*out == NULL) {
669cd37da74Snw141292 		retcode = IDMAP_ERR_MEMORY;
670cd37da74Snw141292 		idmapdlog(LOG_ERR, "Out of memory");
671cd37da74Snw141292 		goto out;
672cd37da74Snw141292 	}
673cd37da74Snw141292 
674cd37da74Snw141292 out:
675cd37da74Snw141292 	if (s_windomain != NULL)
676cd37da74Snw141292 		sqlite_freemem(s_windomain);
677cd37da74Snw141292 	if (s_winname != NULL)
678cd37da74Snw141292 		sqlite_freemem(s_winname);
679cd37da74Snw141292 	if (s_unixname != NULL)
680cd37da74Snw141292 		sqlite_freemem(s_unixname);
681cd37da74Snw141292 
682cd37da74Snw141292 	return (retcode);
683cd37da74Snw141292 }
684cd37da74Snw141292 
685cd37da74Snw141292 
686c5c4113dSnw141292 
687c5c4113dSnw141292 /*
688c5c4113dSnw141292  * Generate and execute SQL statement for LIST RPC calls
689c5c4113dSnw141292  */
690c5c4113dSnw141292 idmap_retcode
process_list_svc_sql(sqlite * db,const char * dbname,char * sql,uint64_t limit,int flag,list_svc_cb cb,void * result)69171590c90Snw141292 process_list_svc_sql(sqlite *db, const char *dbname, char *sql, uint64_t limit,
69248258c6bSjp151216 		int flag, list_svc_cb cb, void *result)
693cd37da74Snw141292 {
694c5c4113dSnw141292 	list_cb_data_t	cb_data;
695c5c4113dSnw141292 	char		*errmsg = NULL;
69684decf41Sjp151216 	int		r;
697c5c4113dSnw141292 	idmap_retcode	retcode = IDMAP_ERR_INTERNAL;
698c5c4113dSnw141292 
699c5c4113dSnw141292 	(void) memset(&cb_data, 0, sizeof (cb_data));
700c5c4113dSnw141292 	cb_data.result = result;
701c5c4113dSnw141292 	cb_data.limit = limit;
70248258c6bSjp151216 	cb_data.flag = flag;
703c5c4113dSnw141292 
70484decf41Sjp151216 
705c5c4113dSnw141292 	r = sqlite_exec(db, sql, cb, &cb_data, &errmsg);
70684decf41Sjp151216 	assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
707c5c4113dSnw141292 	switch (r) {
708c5c4113dSnw141292 	case SQLITE_OK:
709c5c4113dSnw141292 		retcode = IDMAP_SUCCESS;
71084decf41Sjp151216 		break;
71184decf41Sjp151216 
712c5c4113dSnw141292 	default:
713c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
71471590c90Snw141292 		idmapdlog(LOG_ERR, "Database error on %s while executing "
71571590c90Snw141292 		    "%s (%s)", dbname, sql, CHECK_NULL(errmsg));
71684decf41Sjp151216 		break;
717c5c4113dSnw141292 	}
71862c60062Sbaban 	if (errmsg != NULL)
719c5c4113dSnw141292 		sqlite_freemem(errmsg);
720c5c4113dSnw141292 	return (retcode);
721c5c4113dSnw141292 }
722c5c4113dSnw141292 
723c5c4113dSnw141292 /*
724c5c4113dSnw141292  * This routine is called by callbacks that process the results of
725c5c4113dSnw141292  * LIST RPC calls to validate data and to allocate memory for
726c5c4113dSnw141292  * the result array.
727c5c4113dSnw141292  */
728c5c4113dSnw141292 idmap_retcode
validate_list_cb_data(list_cb_data_t * cb_data,int argc,char ** argv,int ncol,uchar_t ** list,size_t valsize)729c5c4113dSnw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv,
730cd37da74Snw141292 		int ncol, uchar_t **list, size_t valsize)
731cd37da74Snw141292 {
732c5c4113dSnw141292 	size_t	nsize;
733c5c4113dSnw141292 	void	*tmplist;
734c5c4113dSnw141292 
735c5c4113dSnw141292 	if (cb_data->limit > 0 && cb_data->next == cb_data->limit)
736c5c4113dSnw141292 		return (IDMAP_NEXT);
737c5c4113dSnw141292 
738c5c4113dSnw141292 	if (argc < ncol || argv == NULL) {
739c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Invalid data");
740c5c4113dSnw141292 		return (IDMAP_ERR_INTERNAL);
741c5c4113dSnw141292 	}
742c5c4113dSnw141292 
743c5c4113dSnw141292 	/* alloc in bulk to reduce number of reallocs */
744c5c4113dSnw141292 	if (cb_data->next >= cb_data->len) {
745c5c4113dSnw141292 		nsize = (cb_data->len + SIZE_INCR) * valsize;
746c5c4113dSnw141292 		tmplist = realloc(*list, nsize);
747c5c4113dSnw141292 		if (tmplist == NULL) {
748c5c4113dSnw141292 			idmapdlog(LOG_ERR, "Out of memory");
749c5c4113dSnw141292 			return (IDMAP_ERR_MEMORY);
750c5c4113dSnw141292 		}
751c5c4113dSnw141292 		*list = tmplist;
752c5c4113dSnw141292 		(void) memset(*list + (cb_data->len * valsize), 0,
753c5c4113dSnw141292 		    SIZE_INCR * valsize);
754c5c4113dSnw141292 		cb_data->len += SIZE_INCR;
755c5c4113dSnw141292 	}
756c5c4113dSnw141292 	return (IDMAP_SUCCESS);
757c5c4113dSnw141292 }
758c5c4113dSnw141292 
759cd37da74Snw141292 static
760cd37da74Snw141292 idmap_retcode
get_namerule_order(char * winname,char * windomain,char * unixname,int direction,int is_diagonal,int * w2u_order,int * u2w_order)761c5c4113dSnw141292 get_namerule_order(char *winname, char *windomain, char *unixname,
762cd37da74Snw141292 	int direction, int is_diagonal, int *w2u_order, int *u2w_order)
763cd37da74Snw141292 {
764c5c4113dSnw141292 	*w2u_order = 0;
765c5c4113dSnw141292 	*u2w_order = 0;
766c5c4113dSnw141292 
767c5c4113dSnw141292 	/*
768c5c4113dSnw141292 	 * Windows to UNIX lookup order:
769c5c4113dSnw141292 	 *  1. winname@domain (or winname) to ""
770c5c4113dSnw141292 	 *  2. winname@domain (or winname) to unixname
771c5c4113dSnw141292 	 *  3. winname@* to ""
772c5c4113dSnw141292 	 *  4. winname@* to unixname
773c5c4113dSnw141292 	 *  5. *@domain (or *) to *
774c5c4113dSnw141292 	 *  6. *@domain (or *) to ""
775c5c4113dSnw141292 	 *  7. *@domain (or *) to unixname
776c5c4113dSnw141292 	 *  8. *@* to *
777c5c4113dSnw141292 	 *  9. *@* to ""
778c5c4113dSnw141292 	 * 10. *@* to unixname
779c5c4113dSnw141292 	 *
780c5c4113dSnw141292 	 * winname is a special case of winname@domain when domain is the
781c5c4113dSnw141292 	 * default domain. Similarly * is a special case of *@domain when
782c5c4113dSnw141292 	 * domain is the default domain.
783c5c4113dSnw141292 	 *
784c5c4113dSnw141292 	 * Note that "" has priority over specific names because "" inhibits
785c5c4113dSnw141292 	 * mappings and traditionally deny rules always had higher priority.
786c5c4113dSnw141292 	 */
787651c0131Sbaban 	if (direction != IDMAP_DIRECTION_U2W) {
788651c0131Sbaban 		/* bi-directional or from windows to unix */
789c5c4113dSnw141292 		if (winname == NULL)
790c5c4113dSnw141292 			return (IDMAP_ERR_W2U_NAMERULE);
791c5c4113dSnw141292 		else if (unixname == NULL)
792c5c4113dSnw141292 			return (IDMAP_ERR_W2U_NAMERULE);
793c5c4113dSnw141292 		else if (EMPTY_NAME(winname))
794c5c4113dSnw141292 			return (IDMAP_ERR_W2U_NAMERULE);
795c5c4113dSnw141292 		else if (*winname == '*' && windomain && *windomain == '*') {
796c5c4113dSnw141292 			if (*unixname == '*')
797c5c4113dSnw141292 				*w2u_order = 8;
798c5c4113dSnw141292 			else if (EMPTY_NAME(unixname))
799c5c4113dSnw141292 				*w2u_order = 9;
800c5c4113dSnw141292 			else /* unixname == name */
801c5c4113dSnw141292 				*w2u_order = 10;
802c5c4113dSnw141292 		} else if (*winname == '*') {
803c5c4113dSnw141292 			if (*unixname == '*')
804c5c4113dSnw141292 				*w2u_order = 5;
805c5c4113dSnw141292 			else if (EMPTY_NAME(unixname))
806c5c4113dSnw141292 				*w2u_order = 6;
807c5c4113dSnw141292 			else /* name */
808c5c4113dSnw141292 				*w2u_order = 7;
80962c60062Sbaban 		} else if (windomain != NULL && *windomain == '*') {
810c5c4113dSnw141292 			/* winname == name */
811c5c4113dSnw141292 			if (*unixname == '*')
812c5c4113dSnw141292 				return (IDMAP_ERR_W2U_NAMERULE);
813c5c4113dSnw141292 			else if (EMPTY_NAME(unixname))
814c5c4113dSnw141292 				*w2u_order = 3;
815c5c4113dSnw141292 			else /* name */
816c5c4113dSnw141292 				*w2u_order = 4;
817c5c4113dSnw141292 		} else  {
818c5c4113dSnw141292 			/* winname == name && windomain == null or name */
819c5c4113dSnw141292 			if (*unixname == '*')
820c5c4113dSnw141292 				return (IDMAP_ERR_W2U_NAMERULE);
821c5c4113dSnw141292 			else if (EMPTY_NAME(unixname))
822c5c4113dSnw141292 				*w2u_order = 1;
823c5c4113dSnw141292 			else /* name */
824c5c4113dSnw141292 				*w2u_order = 2;
825c5c4113dSnw141292 		}
826cd37da74Snw141292 
827c5c4113dSnw141292 	}
828c5c4113dSnw141292 
829c5c4113dSnw141292 	/*
830cd37da74Snw141292 	 * 1. unixname to "", non-diagonal
831cd37da74Snw141292 	 * 2. unixname to winname@domain (or winname), non-diagonal
832cd37da74Snw141292 	 * 3. unixname to "", diagonal
833cd37da74Snw141292 	 * 4. unixname to winname@domain (or winname), diagonal
834cd37da74Snw141292 	 * 5. * to *@domain (or *), non-diagonal
835cd37da74Snw141292 	 * 5. * to *@domain (or *), diagonal
836cd37da74Snw141292 	 * 7. * to ""
837cd37da74Snw141292 	 * 8. * to winname@domain (or winname)
838cd37da74Snw141292 	 * 9. * to "", non-diagonal
839cd37da74Snw141292 	 * 10. * to winname@domain (or winname), diagonal
840c5c4113dSnw141292 	 */
841651c0131Sbaban 	if (direction != IDMAP_DIRECTION_W2U) {
842cd37da74Snw141292 		int diagonal = is_diagonal ? 1 : 0;
843cd37da74Snw141292 
844651c0131Sbaban 		/* bi-directional or from unix to windows */
845c5c4113dSnw141292 		if (unixname == NULL || EMPTY_NAME(unixname))
846c5c4113dSnw141292 			return (IDMAP_ERR_U2W_NAMERULE);
847c5c4113dSnw141292 		else if (winname == NULL)
848c5c4113dSnw141292 			return (IDMAP_ERR_U2W_NAMERULE);
84962c60062Sbaban 		else if (windomain != NULL && *windomain == '*')
850651c0131Sbaban 			return (IDMAP_ERR_U2W_NAMERULE);
851c5c4113dSnw141292 		else if (*unixname == '*') {
852c5c4113dSnw141292 			if (*winname == '*')
853cd37da74Snw141292 				*u2w_order = 5 + diagonal;
854c5c4113dSnw141292 			else if (EMPTY_NAME(winname))
855cd37da74Snw141292 				*u2w_order = 7 + 2 * diagonal;
856c5c4113dSnw141292 			else
857cd37da74Snw141292 				*u2w_order = 8 + 2 * diagonal;
858c5c4113dSnw141292 		} else {
859c5c4113dSnw141292 			if (*winname == '*')
860c5c4113dSnw141292 				return (IDMAP_ERR_U2W_NAMERULE);
861c5c4113dSnw141292 			else if (EMPTY_NAME(winname))
862cd37da74Snw141292 				*u2w_order = 1 + 2 * diagonal;
863c5c4113dSnw141292 			else
864cd37da74Snw141292 				*u2w_order = 2 + 2 * diagonal;
865c5c4113dSnw141292 		}
866c5c4113dSnw141292 	}
867c5c4113dSnw141292 	return (IDMAP_SUCCESS);
868c5c4113dSnw141292 }
869c5c4113dSnw141292 
870c5c4113dSnw141292 /*
871c5c4113dSnw141292  * Generate and execute SQL statement to add name-based mapping rule
872c5c4113dSnw141292  */
873c5c4113dSnw141292 idmap_retcode
add_namerule(sqlite * db,idmap_namerule * rule)874cd37da74Snw141292 add_namerule(sqlite *db, idmap_namerule *rule)
875cd37da74Snw141292 {
876c5c4113dSnw141292 	char		*sql = NULL;
877c5c4113dSnw141292 	idmap_stat	retcode;
8788e228215Sdm199847 	char		*dom = NULL;
87908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*name;
880c5c4113dSnw141292 	int		w2u_order, u2w_order;
881c5c4113dSnw141292 	char		w2ubuf[11], u2wbuf[11];
88208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*canonname = NULL;
88308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*canondomain = NULL;
884c5c4113dSnw141292 
8858e228215Sdm199847 	retcode = get_namerule_order(rule->winname, rule->windomain,
886cd37da74Snw141292 	    rule->unixname, rule->direction,
887cd37da74Snw141292 	    rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order);
888c5c4113dSnw141292 	if (retcode != IDMAP_SUCCESS)
889c5c4113dSnw141292 		goto out;
890c5c4113dSnw141292 
891c5c4113dSnw141292 	if (w2u_order)
892c5c4113dSnw141292 		(void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order);
893c5c4113dSnw141292 	if (u2w_order)
894c5c4113dSnw141292 		(void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order);
895c5c4113dSnw141292 
89662c60062Sbaban 	/*
89762c60062Sbaban 	 * For the triggers on namerules table to work correctly:
89862c60062Sbaban 	 * 1) Use NULL instead of 0 for w2u_order and u2w_order
89962c60062Sbaban 	 * 2) Use "" instead of NULL for "no domain"
90062c60062Sbaban 	 */
90162c60062Sbaban 
90208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	name = rule->winname;
9038e228215Sdm199847 	dom = rule->windomain;
904c5c4113dSnw141292 
905c5c4113dSnw141292 	RDLOCK_CONFIG();
90608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (lookup_wksids_name2sid(name, dom,
90708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    &canonname, &canondomain,
90808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    NULL, NULL, NULL) == IDMAP_SUCCESS) {
90908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		name = canonname;
91008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		dom = canondomain;
91108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else if (EMPTY_STRING(dom)) {
912c8e26105Sjp151216 		if (_idmapdstate.cfg->pgcfg.default_domain)
913c8e26105Sjp151216 			dom = _idmapdstate.cfg->pgcfg.default_domain;
914c5c4113dSnw141292 		else
915c5c4113dSnw141292 			dom = "";
91662c60062Sbaban 	}
91784decf41Sjp151216 	sql = sqlite_mprintf("INSERT into namerules "
918cd37da74Snw141292 	    "(is_user, is_wuser, windomain, winname_display, is_nt4, "
919c5c4113dSnw141292 	    "unixname, w2u_order, u2w_order) "
920cd37da74Snw141292 	    "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);",
921cd37da74Snw141292 	    rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom,
92208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    name, rule->is_nt4 ? 1 : 0, rule->unixname,
923cd37da74Snw141292 	    w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL);
924c5c4113dSnw141292 	UNLOCK_CONFIG();
925c5c4113dSnw141292 
926c5c4113dSnw141292 	if (sql == NULL) {
927c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
928c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
929c5c4113dSnw141292 		goto out;
930c5c4113dSnw141292 	}
931c5c4113dSnw141292 
93271590c90Snw141292 	retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql);
933c5c4113dSnw141292 
934c5c4113dSnw141292 	if (retcode == IDMAP_ERR_OTHER)
935c5c4113dSnw141292 		retcode = IDMAP_ERR_CFG;
936c5c4113dSnw141292 
937c5c4113dSnw141292 out:
93808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	free(canonname);
93908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	free(canondomain);
94062c60062Sbaban 	if (sql != NULL)
941c5c4113dSnw141292 		sqlite_freemem(sql);
942c5c4113dSnw141292 	return (retcode);
943c5c4113dSnw141292 }
944c5c4113dSnw141292 
945c5c4113dSnw141292 /*
946c5c4113dSnw141292  * Flush name-based mapping rules
947c5c4113dSnw141292  */
948c5c4113dSnw141292 idmap_retcode
flush_namerules(sqlite * db)949cd37da74Snw141292 flush_namerules(sqlite *db)
950cd37da74Snw141292 {
951c5c4113dSnw141292 	idmap_stat	retcode;
952c5c4113dSnw141292 
95371590c90Snw141292 	retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "DELETE FROM namerules;");
954c5c4113dSnw141292 
955c5c4113dSnw141292 	return (retcode);
956c5c4113dSnw141292 }
957c5c4113dSnw141292 
958c5c4113dSnw141292 /*
959c5c4113dSnw141292  * Generate and execute SQL statement to remove a name-based mapping rule
960c5c4113dSnw141292  */
961c5c4113dSnw141292 idmap_retcode
rm_namerule(sqlite * db,idmap_namerule * rule)962cd37da74Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule)
963cd37da74Snw141292 {
964c5c4113dSnw141292 	char		*sql = NULL;
965c5c4113dSnw141292 	idmap_stat	retcode;
966cd37da74Snw141292 	char		*expr = NULL;
967c5c4113dSnw141292 
9688e228215Sdm199847 	if (rule->direction < 0 && EMPTY_STRING(rule->windomain) &&
9698e228215Sdm199847 	    EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
970c5c4113dSnw141292 		return (IDMAP_SUCCESS);
971c5c4113dSnw141292 
972cd37da74Snw141292 	retcode = gen_sql_expr_from_rule(rule, &expr);
973cd37da74Snw141292 	if (retcode != IDMAP_SUCCESS)
974c5c4113dSnw141292 		goto out;
975c5c4113dSnw141292 
976bbf6f00cSJordan Brown 	sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s;", expr);
977c5c4113dSnw141292 
978c5c4113dSnw141292 	if (sql == NULL) {
979c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
980c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
981c5c4113dSnw141292 		goto out;
982c5c4113dSnw141292 	}
983c5c4113dSnw141292 
984cd37da74Snw141292 
98571590c90Snw141292 	retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql);
986c5c4113dSnw141292 
987c5c4113dSnw141292 out:
988cd37da74Snw141292 	if (expr != NULL)
989cd37da74Snw141292 		sqlite_freemem(expr);
99062c60062Sbaban 	if (sql != NULL)
991c5c4113dSnw141292 		sqlite_freemem(sql);
992c5c4113dSnw141292 	return (retcode);
993c5c4113dSnw141292 }
994c5c4113dSnw141292 
995c5c4113dSnw141292 /*
996c5c4113dSnw141292  * Compile the given SQL query and step just once.
997c5c4113dSnw141292  *
998c5c4113dSnw141292  * Input:
999c5c4113dSnw141292  * db  - db handle
1000c5c4113dSnw141292  * sql - SQL statement
1001c5c4113dSnw141292  *
1002c5c4113dSnw141292  * Output:
1003c5c4113dSnw141292  * vm     -  virtual SQL machine
1004c5c4113dSnw141292  * ncol   - number of columns in the result
1005c5c4113dSnw141292  * values - column values
1006c5c4113dSnw141292  *
1007c5c4113dSnw141292  * Return values:
1008c5c4113dSnw141292  * IDMAP_SUCCESS
1009c5c4113dSnw141292  * IDMAP_ERR_NOTFOUND
1010c5c4113dSnw141292  * IDMAP_ERR_INTERNAL
1011c5c4113dSnw141292  */
1012c5c4113dSnw141292 
1013cd37da74Snw141292 static
1014cd37da74Snw141292 idmap_retcode
sql_compile_n_step_once(sqlite * db,char * sql,sqlite_vm ** vm,int * ncol,int reqcol,const char *** values)1015c5c4113dSnw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol,
1016cd37da74Snw141292 		int reqcol, const char ***values)
1017cd37da74Snw141292 {
1018c5c4113dSnw141292 	char		*errmsg = NULL;
101984decf41Sjp151216 	int		r;
1020c5c4113dSnw141292 
102184decf41Sjp151216 	if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) {
1022cd37da74Snw141292 		idmapdlog(LOG_ERR, "Database error during %s (%s)", sql,
1023cd37da74Snw141292 		    CHECK_NULL(errmsg));
1024c5c4113dSnw141292 		sqlite_freemem(errmsg);
1025c5c4113dSnw141292 		return (IDMAP_ERR_INTERNAL);
1026c5c4113dSnw141292 	}
1027c5c4113dSnw141292 
1028c5c4113dSnw141292 	r = sqlite_step(*vm, ncol, values, NULL);
102984decf41Sjp151216 	assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
1030c5c4113dSnw141292 
103184decf41Sjp151216 	if (r == SQLITE_ROW) {
103262c60062Sbaban 		if (ncol != NULL && *ncol < reqcol) {
1033c5c4113dSnw141292 			(void) sqlite_finalize(*vm, NULL);
1034c5c4113dSnw141292 			*vm = NULL;
1035c5c4113dSnw141292 			return (IDMAP_ERR_INTERNAL);
1036c5c4113dSnw141292 		}
1037c5c4113dSnw141292 		/* Caller will call finalize after using the results */
1038c5c4113dSnw141292 		return (IDMAP_SUCCESS);
1039c5c4113dSnw141292 	} else if (r == SQLITE_DONE) {
1040c5c4113dSnw141292 		(void) sqlite_finalize(*vm, NULL);
1041c5c4113dSnw141292 		*vm = NULL;
1042c5c4113dSnw141292 		return (IDMAP_ERR_NOTFOUND);
1043c5c4113dSnw141292 	}
1044c5c4113dSnw141292 
1045c5c4113dSnw141292 	(void) sqlite_finalize(*vm, &errmsg);
1046c5c4113dSnw141292 	*vm = NULL;
1047cd37da74Snw141292 	idmapdlog(LOG_ERR, "Database error during %s (%s)", sql,
1048cd37da74Snw141292 	    CHECK_NULL(errmsg));
1049c5c4113dSnw141292 	sqlite_freemem(errmsg);
1050c5c4113dSnw141292 	return (IDMAP_ERR_INTERNAL);
1051c5c4113dSnw141292 }
1052c5c4113dSnw141292 
105362c60062Sbaban /*
1054479ac375Sdm199847  * Load config in the state.
1055e8c27ec8Sbaban  *
1056479ac375Sdm199847  * nm_siduid and nm_sidgid fields:
1057e8c27ec8Sbaban  * state->nm_siduid represents mode used by sid2uid and uid2sid
1058e8c27ec8Sbaban  * requests for directory-based name mappings. Similarly,
1059e8c27ec8Sbaban  * state->nm_sidgid represents mode used by sid2gid and gid2sid
1060e8c27ec8Sbaban  * requests.
1061e8c27ec8Sbaban  *
1062e8c27ec8Sbaban  * sid2uid/uid2sid:
1063e3f2c991SKeyur Desai  * none       -> directory_based_mapping != DIRECTORY_MAPPING_NAME
1064e8c27ec8Sbaban  * AD-mode    -> !nldap_winname_attr && ad_unixuser_attr
1065e8c27ec8Sbaban  * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr
1066e8c27ec8Sbaban  * mixed-mode -> nldap_winname_attr && ad_unixuser_attr
1067e8c27ec8Sbaban  *
1068e8c27ec8Sbaban  * sid2gid/gid2sid:
1069e3f2c991SKeyur Desai  * none       -> directory_based_mapping != DIRECTORY_MAPPING_NAME
1070e8c27ec8Sbaban  * AD-mode    -> !nldap_winname_attr && ad_unixgroup_attr
1071e8c27ec8Sbaban  * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr
1072e8c27ec8Sbaban  * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr
1073e8c27ec8Sbaban  */
1074e8c27ec8Sbaban idmap_retcode
load_cfg_in_state(lookup_state_t * state)1075479ac375Sdm199847 load_cfg_in_state(lookup_state_t *state)
1076e8c27ec8Sbaban {
1077e8c27ec8Sbaban 	state->nm_siduid = IDMAP_NM_NONE;
1078e8c27ec8Sbaban 	state->nm_sidgid = IDMAP_NM_NONE;
1079e8c27ec8Sbaban 	RDLOCK_CONFIG();
1080479ac375Sdm199847 
10814aa0a5e7Snw141292 	state->eph_map_unres_sids = 0;
10824aa0a5e7Snw141292 	if (_idmapdstate.cfg->pgcfg.eph_map_unres_sids)
10834aa0a5e7Snw141292 		state->eph_map_unres_sids = 1;
10844aa0a5e7Snw141292 
108548cd229bSGordon Ross 	state->id_cache_timeout =
108648cd229bSGordon Ross 	    _idmapdstate.cfg->pgcfg.id_cache_timeout;
108748cd229bSGordon Ross 	state->name_cache_timeout =
108848cd229bSGordon Ross 	    _idmapdstate.cfg->pgcfg.name_cache_timeout;
108948cd229bSGordon Ross 
1090e3f2c991SKeyur Desai 	state->directory_based_mapping =
1091e3f2c991SKeyur Desai 	    _idmapdstate.cfg->pgcfg.directory_based_mapping;
1092e3f2c991SKeyur Desai 
1093479ac375Sdm199847 	if (_idmapdstate.cfg->pgcfg.default_domain != NULL) {
1094479ac375Sdm199847 		state->defdom =
1095479ac375Sdm199847 		    strdup(_idmapdstate.cfg->pgcfg.default_domain);
1096479ac375Sdm199847 		if (state->defdom == NULL) {
1097479ac375Sdm199847 			UNLOCK_CONFIG();
1098479ac375Sdm199847 			return (IDMAP_ERR_MEMORY);
1099479ac375Sdm199847 		}
1100479ac375Sdm199847 	} else {
1101479ac375Sdm199847 		UNLOCK_CONFIG();
1102dc03a638Sdm199847 		return (IDMAP_SUCCESS);
1103479ac375Sdm199847 	}
1104e3f2c991SKeyur Desai 
1105e3f2c991SKeyur Desai 	if (_idmapdstate.cfg->pgcfg.directory_based_mapping !=
1106e3f2c991SKeyur Desai 	    DIRECTORY_MAPPING_NAME) {
1107e8c27ec8Sbaban 		UNLOCK_CONFIG();
1108e8c27ec8Sbaban 		return (IDMAP_SUCCESS);
1109e8c27ec8Sbaban 	}
1110e3f2c991SKeyur Desai 
1111e8c27ec8Sbaban 	if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) {
1112e8c27ec8Sbaban 		state->nm_siduid =
1113e8c27ec8Sbaban 		    (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL)
1114e8c27ec8Sbaban 		    ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP;
1115e8c27ec8Sbaban 		state->nm_sidgid =
1116e8c27ec8Sbaban 		    (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL)
1117e8c27ec8Sbaban 		    ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP;
1118e8c27ec8Sbaban 	} else {
1119e8c27ec8Sbaban 		state->nm_siduid =
1120e8c27ec8Sbaban 		    (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL)
1121e8c27ec8Sbaban 		    ? IDMAP_NM_AD : IDMAP_NM_NONE;
1122e8c27ec8Sbaban 		state->nm_sidgid =
1123e8c27ec8Sbaban 		    (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL)
1124e8c27ec8Sbaban 		    ? IDMAP_NM_AD : IDMAP_NM_NONE;
1125e8c27ec8Sbaban 	}
1126e8c27ec8Sbaban 	if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) {
1127e8c27ec8Sbaban 		state->ad_unixuser_attr =
1128e8c27ec8Sbaban 		    strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr);
1129e8c27ec8Sbaban 		if (state->ad_unixuser_attr == NULL) {
1130e8c27ec8Sbaban 			UNLOCK_CONFIG();
1131e8c27ec8Sbaban 			return (IDMAP_ERR_MEMORY);
1132e8c27ec8Sbaban 		}
1133e8c27ec8Sbaban 	}
1134e8c27ec8Sbaban 	if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) {
1135e8c27ec8Sbaban 		state->ad_unixgroup_attr =
1136e8c27ec8Sbaban 		    strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr);
1137e8c27ec8Sbaban 		if (state->ad_unixgroup_attr == NULL) {
1138e8c27ec8Sbaban 			UNLOCK_CONFIG();
1139e8c27ec8Sbaban 			return (IDMAP_ERR_MEMORY);
1140e8c27ec8Sbaban 		}
1141e8c27ec8Sbaban 	}
1142479ac375Sdm199847 	if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) {
1143479ac375Sdm199847 		state->nldap_winname_attr =
1144479ac375Sdm199847 		    strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr);
1145479ac375Sdm199847 		if (state->nldap_winname_attr == NULL) {
1146479ac375Sdm199847 			UNLOCK_CONFIG();
1147479ac375Sdm199847 			return (IDMAP_ERR_MEMORY);
1148479ac375Sdm199847 		}
1149479ac375Sdm199847 	}
1150e8c27ec8Sbaban 	UNLOCK_CONFIG();
1151e8c27ec8Sbaban 	return (IDMAP_SUCCESS);
1152e8c27ec8Sbaban }
1153e8c27ec8Sbaban 
1154e8c27ec8Sbaban /*
115508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Set the rule with specified values.
115648258c6bSjp151216  * All the strings are copied.
115748258c6bSjp151216  */
115848258c6bSjp151216 static void
idmap_namerule_set(idmap_namerule * rule,const char * windomain,const char * winname,const char * unixname,boolean_t is_user,boolean_t is_wuser,boolean_t is_nt4,int direction)115948258c6bSjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain,
116048258c6bSjp151216 		const char *winname, const char *unixname, boolean_t is_user,
116148258c6bSjp151216 		boolean_t is_wuser, boolean_t is_nt4, int direction)
116248258c6bSjp151216 {
116348258c6bSjp151216 	/*
116448258c6bSjp151216 	 * Only update if they differ because we have to free
116548258c6bSjp151216 	 * and duplicate the strings
116648258c6bSjp151216 	 */
116748258c6bSjp151216 	if (rule->windomain == NULL || windomain == NULL ||
116848258c6bSjp151216 	    strcmp(rule->windomain, windomain) != 0) {
116948258c6bSjp151216 		if (rule->windomain != NULL) {
117048258c6bSjp151216 			free(rule->windomain);
117148258c6bSjp151216 			rule->windomain = NULL;
117248258c6bSjp151216 		}
117348258c6bSjp151216 		if (windomain != NULL)
117448258c6bSjp151216 			rule->windomain = strdup(windomain);
117548258c6bSjp151216 	}
117648258c6bSjp151216 
117748258c6bSjp151216 	if (rule->winname == NULL || winname == NULL ||
117848258c6bSjp151216 	    strcmp(rule->winname, winname) != 0) {
117948258c6bSjp151216 		if (rule->winname != NULL) {
118048258c6bSjp151216 			free(rule->winname);
118148258c6bSjp151216 			rule->winname = NULL;
118248258c6bSjp151216 		}
118348258c6bSjp151216 		if (winname != NULL)
118448258c6bSjp151216 			rule->winname = strdup(winname);
118548258c6bSjp151216 	}
118648258c6bSjp151216 
118748258c6bSjp151216 	if (rule->unixname == NULL || unixname == NULL ||
118848258c6bSjp151216 	    strcmp(rule->unixname, unixname) != 0) {
118948258c6bSjp151216 		if (rule->unixname != NULL) {
119048258c6bSjp151216 			free(rule->unixname);
119148258c6bSjp151216 			rule->unixname = NULL;
119248258c6bSjp151216 		}
119348258c6bSjp151216 		if (unixname != NULL)
119448258c6bSjp151216 			rule->unixname = strdup(unixname);
119548258c6bSjp151216 	}
119648258c6bSjp151216 
119748258c6bSjp151216 	rule->is_user = is_user;
119848258c6bSjp151216 	rule->is_wuser = is_wuser;
119948258c6bSjp151216 	rule->is_nt4 = is_nt4;
120048258c6bSjp151216 	rule->direction = direction;
120148258c6bSjp151216 }
120248258c6bSjp151216 
1203e8c27ec8Sbaban /*
1204e8c27ec8Sbaban  * Lookup well-known SIDs table either by winname or by SID.
120508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
120608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * If the given winname or SID is a well-known SID then we set is_wksid
1207e8c27ec8Sbaban  * variable and then proceed to see if the SID has a hard mapping to
1208e8c27ec8Sbaban  * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to
120908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * fixed ephemeral ids). The direction flag indicates whether we have
121008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * a mapping; UNDEF indicates that we do not.
121108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
121208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * If we find a mapping then we return success, except for the
12139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * special case of IDMAP_SENTINEL_PID which indicates an inhibited mapping.
121408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
121508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * If we find a matching entry, but no mapping, we supply SID, name, and type
121608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * information and return "not found".  Higher layers will probably
121708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * do ephemeral mapping.
121808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
121908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * If we do not find a match, we return "not found" and leave the question
122008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * to higher layers.
1221e8c27ec8Sbaban  */
1222cd37da74Snw141292 static
1223cd37da74Snw141292 idmap_retcode
lookup_wksids_sid2pid(idmap_mapping * req,idmap_id_res * res,int * is_wksid)122408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *is_wksid)
1225cd37da74Snw141292 {
122608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	const wksids_table_t *wksid;
122762c60062Sbaban 
122808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	*is_wksid = 0;
1229e8c27ec8Sbaban 
123008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	assert(req->id1.idmap_id_u.sid.prefix != NULL ||
123108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    req->id1name != NULL);
123208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1233e8c27ec8Sbaban 	if (req->id1.idmap_id_u.sid.prefix != NULL) {
123408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		wksid = find_wksid_by_sid(req->id1.idmap_id_u.sid.prefix,
123508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    req->id1.idmap_id_u.sid.rid, res->id.idtype);
123608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
123708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		wksid = find_wksid_by_name(req->id1name, req->id1domain,
123808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    res->id.idtype);
123908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
124008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid == NULL)
124108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		return (IDMAP_ERR_NOTFOUND);
124208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
124308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Found matching entry. */
124408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
124508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Fill in name if it was not already there. */
1246e8c27ec8Sbaban 	if (req->id1name == NULL) {
124708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		req->id1name = strdup(wksid->winname);
1248e8c27ec8Sbaban 		if (req->id1name == NULL)
1249e8c27ec8Sbaban 			return (IDMAP_ERR_MEMORY);
1250e8c27ec8Sbaban 	}
125108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
125208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Fill in SID if it was not already there */
125308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (req->id1.idmap_id_u.sid.prefix == NULL) {
125408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (wksid->sidprefix != NULL) {
1255e8c27ec8Sbaban 			req->id1.idmap_id_u.sid.prefix =
125608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			    strdup(wksid->sidprefix);
125708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		} else {
125808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			RDLOCK_CONFIG();
125908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			req->id1.idmap_id_u.sid.prefix =
126008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			    strdup(_idmapdstate.cfg->pgcfg.machine_sid);
126108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			UNLOCK_CONFIG();
126208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		}
1263e8c27ec8Sbaban 		if (req->id1.idmap_id_u.sid.prefix == NULL)
1264e8c27ec8Sbaban 			return (IDMAP_ERR_MEMORY);
126508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		req->id1.idmap_id_u.sid.rid = wksid->rid;
1266e8c27ec8Sbaban 	}
1267e8c27ec8Sbaban 
126808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Fill in the canonical domain if not already there */
126908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (req->id1domain == NULL) {
127008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		const char *dom;
127108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
127208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		RDLOCK_CONFIG();
1273fe1c642dSBill Krier 		if (wksid->domain != NULL)
127408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			dom = wksid->domain;
1275fe1c642dSBill Krier 		else
127608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			dom = _idmapdstate.hostname;
127708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		req->id1domain = strdup(dom);
127808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		UNLOCK_CONFIG();
127908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (req->id1domain == NULL)
128008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			return (IDMAP_ERR_MEMORY);
128108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
128208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
128308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	*is_wksid = 1;
1284e8c27ec8Sbaban 	req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
1285e8c27ec8Sbaban 
128608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	req->id1.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID;
1287e8c27ec8Sbaban 
1288479ac375Sdm199847 	if (res->id.idtype == IDMAP_POSIXID) {
128908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		res->id.idtype = wksid->is_wuser ? IDMAP_UID : IDMAP_GID;
1290479ac375Sdm199847 	}
129108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
129208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid->direction == IDMAP_DIRECTION_UNDEF) {
129308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/*
129408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * We don't have a mapping
129508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * (But note that we may have supplied SID, name, or type
129608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * information.)
129708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 */
1298e8c27ec8Sbaban 		return (IDMAP_ERR_NOTFOUND);
129908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
130008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
130108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/*
130208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * We have an explicit mapping.
130308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	 */
13049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid->pid == IDMAP_SENTINEL_PID) {
130508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/*
130608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * ... which is that mapping is inhibited.
130708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 */
130808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		return (IDMAP_ERR_NOMAPPING);
130908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
131062c60062Sbaban 
1311e8c27ec8Sbaban 	switch (res->id.idtype) {
1312c5c4113dSnw141292 	case IDMAP_UID:
131308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		res->id.idmap_id_u.uid = wksid->pid;
131408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		break;
1315c5c4113dSnw141292 	case IDMAP_GID:
131608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		res->id.idmap_id_u.gid = wksid->pid;
131708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		break;
1318c5c4113dSnw141292 	default:
131908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/* IDMAP_POSIXID is eliminated above */
1320c5c4113dSnw141292 		return (IDMAP_ERR_NOTSUPPORTED);
1321c5c4113dSnw141292 	}
132208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
132308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	res->direction = wksid->direction;
132408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID;
132508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	res->info.src = IDMAP_MAP_SRC_HARD_CODED;
132608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	return (IDMAP_SUCCESS);
1327c5c4113dSnw141292 }
1328c5c4113dSnw141292 
1329cd37da74Snw141292 
133008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /*
133108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Look for an entry mapping a PID to a SID.
133208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
133308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Note that direction=UNDEF entries do not specify a mapping,
13349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * and that IDMAP_SENTINEL_PID entries represent either an inhibited
133508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * mapping or an ephemeral mapping.  We don't handle either here;
133608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * they are filtered out by find_wksid_by_pid.
133708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  */
1338cd37da74Snw141292 static
1339cd37da74Snw141292 idmap_retcode
lookup_wksids_pid2sid(idmap_mapping * req,idmap_id_res * res,int is_user)1340cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user)
1341cd37da74Snw141292 {
134208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	const wksids_table_t *wksid;
134308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
134408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	wksid = find_wksid_by_pid(req->id1.idmap_id_u.uid, is_user);
134508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid == NULL)
1346e8c27ec8Sbaban 		return (IDMAP_ERR_NOTFOUND);
134708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1348e8c27ec8Sbaban 	if (res->id.idtype == IDMAP_SID) {
134908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		res->id.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID;
1350e8c27ec8Sbaban 	}
135108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	res->id.idmap_id_u.sid.rid = wksid->rid;
135208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
135308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid->sidprefix != NULL) {
1354c5c4113dSnw141292 		res->id.idmap_id_u.sid.prefix =
135508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    strdup(wksid->sidprefix);
135608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
135708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		RDLOCK_CONFIG();
135808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		res->id.idmap_id_u.sid.prefix =
135908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    strdup(_idmapdstate.cfg->pgcfg.machine_sid);
136008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		UNLOCK_CONFIG();
136108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
136208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1363c5c4113dSnw141292 	if (res->id.idmap_id_u.sid.prefix == NULL) {
1364c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
1365c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
1366c5c4113dSnw141292 	}
136708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1368fe1c642dSBill Krier 	/* Fill in name if it was not already there. */
1369fe1c642dSBill Krier 	if (req->id2name == NULL) {
1370fe1c642dSBill Krier 		req->id2name = strdup(wksid->winname);
1371fe1c642dSBill Krier 		if (req->id2name == NULL)
1372fe1c642dSBill Krier 			return (IDMAP_ERR_MEMORY);
1373fe1c642dSBill Krier 	}
1374fe1c642dSBill Krier 
1375fe1c642dSBill Krier 	/* Fill in the canonical domain if not already there */
1376fe1c642dSBill Krier 	if (req->id2domain == NULL) {
1377fe1c642dSBill Krier 		const char *dom;
1378fe1c642dSBill Krier 
1379fe1c642dSBill Krier 		RDLOCK_CONFIG();
1380fe1c642dSBill Krier 		if (wksid->domain != NULL)
1381fe1c642dSBill Krier 			dom = wksid->domain;
1382fe1c642dSBill Krier 		else
1383fe1c642dSBill Krier 			dom = _idmapdstate.hostname;
1384fe1c642dSBill Krier 		req->id2domain = strdup(dom);
1385fe1c642dSBill Krier 		UNLOCK_CONFIG();
1386fe1c642dSBill Krier 		if (req->id2domain == NULL)
1387fe1c642dSBill Krier 			return (IDMAP_ERR_MEMORY);
1388fe1c642dSBill Krier 	}
1389fe1c642dSBill Krier 
139008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	res->direction = wksid->direction;
1391fc724630SAlan Wright 	res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID;
139248258c6bSjp151216 	res->info.src = IDMAP_MAP_SRC_HARD_CODED;
1393c5c4113dSnw141292 	return (IDMAP_SUCCESS);
1394c5c4113dSnw141292 }
139562c60062Sbaban 
139608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /*
139708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Look up a name in the wksids list, matching name and, if supplied, domain,
139808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * and extract data.
139908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
140008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Given:
140108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * name		Windows user name
140208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * domain	Windows domain name (or NULL)
140308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
140408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Return:  Error code
140508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
140608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *canonname	canonical name (if canonname non-NULL) [1]
140708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *canondomain	canonical domain (if canondomain non-NULL) [1]
140808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *sidprefix	SID prefix (if sidprefix non-NULL) [1]
140908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *rid		RID (if rid non-NULL) [2]
141008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *type	Type (if type non-NULL) [2]
141108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
141208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * [1] malloc'ed, NULL on error
141308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * [2] Undefined on error
141408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  */
1415cd37da74Snw141292 idmap_retcode
lookup_wksids_name2sid(const char * name,const char * domain,char ** canonname,char ** canondomain,char ** sidprefix,idmap_rid_t * rid,idmap_id_type * type)141608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_wksids_name2sid(
141708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     const char *name,
141808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     const char *domain,
141908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **canonname,
142008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **canondomain,
142108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **sidprefix,
142208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     idmap_rid_t *rid,
1423148c5f43SAlan Wright     idmap_id_type *type)
1424cd37da74Snw141292 {
142508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	const wksids_table_t *wksid;
1426479ac375Sdm199847 
142708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (sidprefix != NULL)
142808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*sidprefix = NULL;
142908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canonname != NULL)
143008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canonname = NULL;
143108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canondomain != NULL)
143208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canondomain = NULL;
1433479ac375Sdm199847 
143408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	wksid = find_wksid_by_name(name, domain, IDMAP_POSIXID);
143508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (wksid == NULL)
143608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		return (IDMAP_ERR_NOTFOUND);
143708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
143808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (sidprefix != NULL) {
143908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (wksid->sidprefix != NULL) {
144008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*sidprefix = strdup(wksid->sidprefix);
144108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		} else {
144208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			RDLOCK_CONFIG();
144308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*sidprefix = strdup(
144408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			    _idmapdstate.cfg->pgcfg.machine_sid);
144508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			UNLOCK_CONFIG();
144662c60062Sbaban 		}
144708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (*sidprefix == NULL)
144808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			goto nomem;
144908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
145008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
145108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (rid != NULL)
145208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*rid = wksid->rid;
145308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
145408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canonname != NULL) {
145508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canonname = strdup(wksid->winname);
145608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (*canonname == NULL)
145708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			goto nomem;
145808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
145908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
146008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canondomain != NULL) {
146108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (wksid->domain != NULL) {
146208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*canondomain = strdup(wksid->domain);
146308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		} else {
146408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			RDLOCK_CONFIG();
146508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*canondomain = strdup(_idmapdstate.hostname);
146608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			UNLOCK_CONFIG();
146708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		}
146808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (*canondomain == NULL)
146908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			goto nomem;
147008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
147108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
147208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (type != NULL)
147308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*type = (wksid->is_wuser) ?
1474148c5f43SAlan Wright 		    IDMAP_USID : IDMAP_GSID;
147508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
147608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	return (IDMAP_SUCCESS);
147708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
147808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States nomem:
1479cd37da74Snw141292 	idmapdlog(LOG_ERR, "Out of memory");
148008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1481e8c27ec8Sbaban 	if (sidprefix != NULL) {
1482e8c27ec8Sbaban 		free(*sidprefix);
1483e8c27ec8Sbaban 		*sidprefix = NULL;
1484e8c27ec8Sbaban 	}
148508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
148608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canonname != NULL) {
148708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		free(*canonname);
148808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canonname = NULL;
148908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
149008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
149108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canondomain != NULL) {
149208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		free(*canondomain);
149308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canondomain = NULL;
149408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
149508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1496cd37da74Snw141292 	return (IDMAP_ERR_MEMORY);
1497cd37da74Snw141292 }
1498c5c4113dSnw141292 
1499cd37da74Snw141292 static
1500cd37da74Snw141292 idmap_retcode
lookup_cache_sid2pid(sqlite * cache,idmap_mapping * req,idmap_id_res * res)1501cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res)
1502cd37da74Snw141292 {
1503c5c4113dSnw141292 	char		*end;
1504c5c4113dSnw141292 	char		*sql = NULL;
1505c5c4113dSnw141292 	const char	**values;
1506c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
1507c5c4113dSnw141292 	int		ncol, is_user;
1508c5c4113dSnw141292 	uid_t		pid;
1509c5c4113dSnw141292 	time_t		curtime, exp;
1510c5c4113dSnw141292 	idmap_retcode	retcode;
1511042addd6Sbaban 	char		*is_user_string, *lower_name;
1512c5c4113dSnw141292 
1513c5c4113dSnw141292 	/* Current time */
1514c5c4113dSnw141292 	errno = 0;
1515c5c4113dSnw141292 	if ((curtime = time(NULL)) == (time_t)-1) {
1516cd37da74Snw141292 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
1517c5c4113dSnw141292 		    strerror(errno));
1518c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
1519c5c4113dSnw141292 		goto out;
1520c5c4113dSnw141292 	}
1521c5c4113dSnw141292 
1522e8c27ec8Sbaban 	switch (res->id.idtype) {
1523cd37da74Snw141292 	case IDMAP_UID:
1524cd37da74Snw141292 		is_user_string = "1";
1525cd37da74Snw141292 		break;
1526cd37da74Snw141292 	case IDMAP_GID:
1527cd37da74Snw141292 		is_user_string = "0";
1528cd37da74Snw141292 		break;
1529cd37da74Snw141292 	case IDMAP_POSIXID:
1530cd37da74Snw141292 		/* the non-diagonal mapping */
1531cd37da74Snw141292 		is_user_string = "is_wuser";
1532cd37da74Snw141292 		break;
1533cd37da74Snw141292 	default:
1534cd37da74Snw141292 		retcode = IDMAP_ERR_NOTSUPPORTED;
1535cd37da74Snw141292 		goto out;
1536cd37da74Snw141292 	}
1537cd37da74Snw141292 
1538c5c4113dSnw141292 	/* SQL to lookup the cache */
153948258c6bSjp151216 
1540e8c27ec8Sbaban 	if (req->id1.idmap_id_u.sid.prefix != NULL) {
1541e8c27ec8Sbaban 		sql = sqlite_mprintf("SELECT pid, is_user, expiration, "
154248258c6bSjp151216 		    "unixname, u2w, is_wuser, "
154348258c6bSjp151216 		    "map_type, map_dn, map_attr, map_value, "
154448258c6bSjp151216 		    "map_windomain, map_winname, map_unixname, map_is_nt4 "
1545cd37da74Snw141292 		    "FROM idmap_cache WHERE is_user = %s AND "
1546c5c4113dSnw141292 		    "sidprefix = %Q AND rid = %u AND w2u = 1 AND "
1547c5c4113dSnw141292 		    "(pid >= 2147483648 OR "
1548c5c4113dSnw141292 		    "(expiration = 0 OR expiration ISNULL OR "
1549c5c4113dSnw141292 		    "expiration > %d));",
1550e8c27ec8Sbaban 		    is_user_string, req->id1.idmap_id_u.sid.prefix,
1551e8c27ec8Sbaban 		    req->id1.idmap_id_u.sid.rid, curtime);
1552e8c27ec8Sbaban 	} else if (req->id1name != NULL) {
1553042addd6Sbaban 		if ((lower_name = tolower_u8(req->id1name)) == NULL)
1554042addd6Sbaban 			lower_name = req->id1name;
1555e8c27ec8Sbaban 		sql = sqlite_mprintf("SELECT pid, is_user, expiration, "
155648258c6bSjp151216 		    "unixname, u2w, is_wuser, "
155748258c6bSjp151216 		    "map_type, map_dn, map_attr, map_value, "
155848258c6bSjp151216 		    "map_windomain, map_winname, map_unixname, map_is_nt4 "
1559e8c27ec8Sbaban 		    "FROM idmap_cache WHERE is_user = %s AND "
1560e8c27ec8Sbaban 		    "winname = %Q AND windomain = %Q AND w2u = 1 AND "
1561e8c27ec8Sbaban 		    "(pid >= 2147483648 OR "
1562e8c27ec8Sbaban 		    "(expiration = 0 OR expiration ISNULL OR "
1563e8c27ec8Sbaban 		    "expiration > %d));",
156448258c6bSjp151216 		    is_user_string, lower_name, req->id1domain,
156548258c6bSjp151216 		    curtime);
1566042addd6Sbaban 		if (lower_name != req->id1name)
1567042addd6Sbaban 			free(lower_name);
1568e8c27ec8Sbaban 	} else {
1569e8c27ec8Sbaban 		retcode = IDMAP_ERR_ARG;
1570e8c27ec8Sbaban 		goto out;
1571e8c27ec8Sbaban 	}
1572c5c4113dSnw141292 	if (sql == NULL) {
1573c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
1574c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
1575c5c4113dSnw141292 		goto out;
1576c5c4113dSnw141292 	}
157748258c6bSjp151216 	retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol,
157848258c6bSjp151216 	    14, &values);
1579c5c4113dSnw141292 	sqlite_freemem(sql);
1580c5c4113dSnw141292 
1581c5c4113dSnw141292 	if (retcode == IDMAP_ERR_NOTFOUND) {
1582c5c4113dSnw141292 		goto out;
1583c5c4113dSnw141292 	} else if (retcode == IDMAP_SUCCESS) {
1584c5c4113dSnw141292 		/* sanity checks */
1585c5c4113dSnw141292 		if (values[0] == NULL || values[1] == NULL) {
1586c5c4113dSnw141292 			retcode = IDMAP_ERR_CACHE;
1587c5c4113dSnw141292 			goto out;
1588c5c4113dSnw141292 		}
1589c5c4113dSnw141292 
1590c5c4113dSnw141292 		pid = strtoul(values[0], &end, 10);
1591c5c4113dSnw141292 		is_user = strncmp(values[1], "0", 2) ? 1 : 0;
1592c5c4113dSnw141292 
1593cd37da74Snw141292 		if (is_user) {
1594cd37da74Snw141292 			res->id.idtype = IDMAP_UID;
1595cd37da74Snw141292 			res->id.idmap_id_u.uid = pid;
1596cd37da74Snw141292 		} else {
1597cd37da74Snw141292 			res->id.idtype = IDMAP_GID;
1598cd37da74Snw141292 			res->id.idmap_id_u.gid = pid;
1599cd37da74Snw141292 		}
1600cd37da74Snw141292 
1601c5c4113dSnw141292 		/*
1602c5c4113dSnw141292 		 * We may have an expired ephemeral mapping. Consider
1603c5c4113dSnw141292 		 * the expired entry as valid if we are not going to
1604c5c4113dSnw141292 		 * perform name-based mapping. But do not renew the
1605c5c4113dSnw141292 		 * expiration.
1606c5c4113dSnw141292 		 * If we will be doing name-based mapping then store the
1607c5c4113dSnw141292 		 * ephemeral pid in the result so that we can use it
1608c5c4113dSnw141292 		 * if we end up doing dynamic mapping again.
1609c5c4113dSnw141292 		 */
1610c5c4113dSnw141292 		if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) &&
1611cd37da74Snw141292 		    !AVOID_NAMESERVICE(req) &&
16129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    IDMAP_ID_IS_EPHEMERAL(pid) && values[2] != NULL) {
1613c5c4113dSnw141292 			exp = strtoll(values[2], &end, 10);
1614c5c4113dSnw141292 			if (exp && exp <= curtime) {
1615c5c4113dSnw141292 				/* Store the ephemeral pid */
1616651c0131Sbaban 				res->direction = IDMAP_DIRECTION_BI;
1617cd37da74Snw141292 				req->direction |= is_user
1618cd37da74Snw141292 				    ? _IDMAP_F_EXP_EPH_UID
1619cd37da74Snw141292 				    : _IDMAP_F_EXP_EPH_GID;
1620c5c4113dSnw141292 				retcode = IDMAP_ERR_NOTFOUND;
1621c5c4113dSnw141292 			}
1622c5c4113dSnw141292 		}
1623c5c4113dSnw141292 	}
1624c5c4113dSnw141292 
1625c5c4113dSnw141292 out:
1626c5c4113dSnw141292 	if (retcode == IDMAP_SUCCESS) {
162762c60062Sbaban 		if (values[4] != NULL)
1628c5c4113dSnw141292 			res->direction =
1629651c0131Sbaban 			    (strtol(values[4], &end, 10) == 0)?
1630651c0131Sbaban 			    IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
1631c5c4113dSnw141292 		else
1632651c0131Sbaban 			res->direction = IDMAP_DIRECTION_W2U;
1633c5c4113dSnw141292 
163462c60062Sbaban 		if (values[3] != NULL) {
1635e8c27ec8Sbaban 			if (req->id2name != NULL)
1636e8c27ec8Sbaban 				free(req->id2name);
16378e228215Sdm199847 			req->id2name = strdup(values[3]);
16388e228215Sdm199847 			if (req->id2name == NULL) {
1639c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
1640c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
1641c5c4113dSnw141292 			}
1642c5c4113dSnw141292 		}
1643e8c27ec8Sbaban 
1644e8c27ec8Sbaban 		req->id1.idtype = strncmp(values[5], "0", 2) ?
1645e8c27ec8Sbaban 		    IDMAP_USID : IDMAP_GSID;
164648258c6bSjp151216 
164748258c6bSjp151216 		if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
164848258c6bSjp151216 			res->info.src = IDMAP_MAP_SRC_CACHE;
164948258c6bSjp151216 			res->info.how.map_type = strtoul(values[6], &end, 10);
165048258c6bSjp151216 			switch (res->info.how.map_type) {
165148258c6bSjp151216 			case IDMAP_MAP_TYPE_DS_AD:
165248258c6bSjp151216 				res->info.how.idmap_how_u.ad.dn =
165348258c6bSjp151216 				    strdup(values[7]);
165448258c6bSjp151216 				res->info.how.idmap_how_u.ad.attr =
165548258c6bSjp151216 				    strdup(values[8]);
165648258c6bSjp151216 				res->info.how.idmap_how_u.ad.value =
165748258c6bSjp151216 				    strdup(values[9]);
165848258c6bSjp151216 				break;
165948258c6bSjp151216 
166048258c6bSjp151216 			case IDMAP_MAP_TYPE_DS_NLDAP:
166148258c6bSjp151216 				res->info.how.idmap_how_u.nldap.dn =
166248258c6bSjp151216 				    strdup(values[7]);
166348258c6bSjp151216 				res->info.how.idmap_how_u.nldap.attr =
166448258c6bSjp151216 				    strdup(values[8]);
166548258c6bSjp151216 				res->info.how.idmap_how_u.nldap.value =
166648258c6bSjp151216 				    strdup(values[9]);
166748258c6bSjp151216 				break;
166848258c6bSjp151216 
166948258c6bSjp151216 			case IDMAP_MAP_TYPE_RULE_BASED:
167048258c6bSjp151216 				res->info.how.idmap_how_u.rule.windomain =
167148258c6bSjp151216 				    strdup(values[10]);
167248258c6bSjp151216 				res->info.how.idmap_how_u.rule.winname =
167348258c6bSjp151216 				    strdup(values[11]);
167448258c6bSjp151216 				res->info.how.idmap_how_u.rule.unixname =
167548258c6bSjp151216 				    strdup(values[12]);
167648258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_nt4 =
167748258c6bSjp151216 				    strtoul(values[13], &end, 1);
167848258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_user =
167948258c6bSjp151216 				    is_user;
168048258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_wuser =
168148258c6bSjp151216 				    strtoul(values[5], &end, 1);
168248258c6bSjp151216 				break;
168348258c6bSjp151216 
168448258c6bSjp151216 			case IDMAP_MAP_TYPE_EPHEMERAL:
168548258c6bSjp151216 				break;
168648258c6bSjp151216 
168748258c6bSjp151216 			case IDMAP_MAP_TYPE_LOCAL_SID:
168848258c6bSjp151216 				break;
168948258c6bSjp151216 
169048258c6bSjp151216 			case IDMAP_MAP_TYPE_KNOWN_SID:
169148258c6bSjp151216 				break;
169248258c6bSjp151216 
1693e3f2c991SKeyur Desai 			case IDMAP_MAP_TYPE_IDMU:
1694e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.dn =
1695e3f2c991SKeyur Desai 				    strdup(values[7]);
1696e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.attr =
1697e3f2c991SKeyur Desai 				    strdup(values[8]);
1698e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.value =
1699e3f2c991SKeyur Desai 				    strdup(values[9]);
1700e3f2c991SKeyur Desai 				break;
1701e3f2c991SKeyur Desai 
170248258c6bSjp151216 			default:
1703e3f2c991SKeyur Desai 				/* Unknown mapping type */
170448258c6bSjp151216 				assert(FALSE);
170548258c6bSjp151216 			}
170648258c6bSjp151216 		}
1707c5c4113dSnw141292 	}
170862c60062Sbaban 	if (vm != NULL)
1709c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
1710c5c4113dSnw141292 	return (retcode);
1711c5c4113dSnw141292 }
1712c5c4113dSnw141292 
1713148c5f43SAlan Wright /*
1714148c5f43SAlan Wright  * Previous versions used two enumerations for representing types.
1715148c5f43SAlan Wright  * One of those has largely been eliminated, but was used in the
1716148c5f43SAlan Wright  * name cache table and so during an upgrade might still be visible.
1717148c5f43SAlan Wright  * In addition, the test suite prepopulates the cache with these values.
1718148c5f43SAlan Wright  *
1719148c5f43SAlan Wright  * This function translates those old values into the new values.
1720148c5f43SAlan Wright  *
1721148c5f43SAlan Wright  * This code deliberately does not use symbolic values for the legacy
1722148c5f43SAlan Wright  * values.  This is the *only* place where they should be used.
1723148c5f43SAlan Wright  */
1724148c5f43SAlan Wright static
1725148c5f43SAlan Wright idmap_id_type
xlate_legacy_type(int type)1726148c5f43SAlan Wright xlate_legacy_type(int type)
1727148c5f43SAlan Wright {
1728148c5f43SAlan Wright 	switch (type) {
1729148c5f43SAlan Wright 	case -1004:	/* _IDMAP_T_USER */
1730148c5f43SAlan Wright 		return (IDMAP_USID);
1731148c5f43SAlan Wright 	case -1005:	/* _IDMAP_T_GROUP */
1732148c5f43SAlan Wright 		return (IDMAP_GSID);
1733148c5f43SAlan Wright 	default:
1734148c5f43SAlan Wright 		return (type);
1735148c5f43SAlan Wright 	}
1736148c5f43SAlan Wright 	NOTE(NOTREACHED)
1737148c5f43SAlan Wright }
1738148c5f43SAlan Wright 
1739cd37da74Snw141292 static
1740cd37da74Snw141292 idmap_retcode
lookup_cache_sid2name(sqlite * cache,const char * sidprefix,idmap_rid_t rid,char ** canonname,char ** canondomain,idmap_id_type * type)174162c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid,
1742148c5f43SAlan Wright 		char **canonname, char **canondomain, idmap_id_type *type)
1743cd37da74Snw141292 {
1744c5c4113dSnw141292 	char		*end;
1745c5c4113dSnw141292 	char		*sql = NULL;
1746c5c4113dSnw141292 	const char	**values;
1747c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
1748c5c4113dSnw141292 	int		ncol;
1749c5c4113dSnw141292 	time_t		curtime;
1750c5c4113dSnw141292 	idmap_retcode	retcode = IDMAP_SUCCESS;
1751c5c4113dSnw141292 
1752c5c4113dSnw141292 	/* Get current time */
1753c5c4113dSnw141292 	errno = 0;
1754c5c4113dSnw141292 	if ((curtime = time(NULL)) == (time_t)-1) {
1755cd37da74Snw141292 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
1756c5c4113dSnw141292 		    strerror(errno));
1757c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
1758c5c4113dSnw141292 		goto out;
1759c5c4113dSnw141292 	}
1760c5c4113dSnw141292 
1761c5c4113dSnw141292 	/* SQL to lookup the cache */
1762cd37da74Snw141292 	sql = sqlite_mprintf("SELECT canon_name, domain, type "
1763cd37da74Snw141292 	    "FROM name_cache WHERE "
1764c5c4113dSnw141292 	    "sidprefix = %Q AND rid = %u AND "
1765c5c4113dSnw141292 	    "(expiration = 0 OR expiration ISNULL OR "
1766c5c4113dSnw141292 	    "expiration > %d);",
1767c5c4113dSnw141292 	    sidprefix, rid, curtime);
1768c5c4113dSnw141292 	if (sql == NULL) {
1769c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
1770c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
1771c5c4113dSnw141292 		goto out;
1772c5c4113dSnw141292 	}
1773c5c4113dSnw141292 	retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values);
1774c5c4113dSnw141292 	sqlite_freemem(sql);
1775c5c4113dSnw141292 
1776c5c4113dSnw141292 	if (retcode == IDMAP_SUCCESS) {
177762c60062Sbaban 		if (type != NULL) {
1778c5c4113dSnw141292 			if (values[2] == NULL) {
1779c5c4113dSnw141292 				retcode = IDMAP_ERR_CACHE;
1780c5c4113dSnw141292 				goto out;
1781c5c4113dSnw141292 			}
1782148c5f43SAlan Wright 			*type = xlate_legacy_type(strtol(values[2], &end, 10));
1783c5c4113dSnw141292 		}
1784c5c4113dSnw141292 
178508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (canonname != NULL && values[0] != NULL) {
178608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			if ((*canonname = strdup(values[0])) == NULL) {
1787c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
1788c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
1789c5c4113dSnw141292 				goto out;
1790c5c4113dSnw141292 			}
1791c5c4113dSnw141292 		}
1792c5c4113dSnw141292 
179308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (canondomain != NULL && values[1] != NULL) {
179408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			if ((*canondomain = strdup(values[1])) == NULL) {
179508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 				if (canonname != NULL) {
179608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 					free(*canonname);
179708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 					*canonname = NULL;
1798c5c4113dSnw141292 				}
1799c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
1800c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
1801c5c4113dSnw141292 				goto out;
1802c5c4113dSnw141292 			}
1803c5c4113dSnw141292 		}
1804c5c4113dSnw141292 	}
1805c5c4113dSnw141292 
1806c5c4113dSnw141292 out:
180762c60062Sbaban 	if (vm != NULL)
1808c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
1809c5c4113dSnw141292 	return (retcode);
1810c5c4113dSnw141292 }
1811c5c4113dSnw141292 
1812c5c4113dSnw141292 /*
1813e8c27ec8Sbaban  * Given SID, find winname using name_cache OR
1814e8c27ec8Sbaban  * Given winname, find SID using name_cache.
1815e8c27ec8Sbaban  * Used when mapping win to unix i.e. req->id1 is windows id and
1816e8c27ec8Sbaban  * req->id2 is unix id
1817c5c4113dSnw141292  */
1818cd37da74Snw141292 static
1819cd37da74Snw141292 idmap_retcode
lookup_name_cache(sqlite * cache,idmap_mapping * req,idmap_id_res * res)1820e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res)
1821cd37da74Snw141292 {
1822148c5f43SAlan Wright 	idmap_id_type	type = -1;
1823c5c4113dSnw141292 	idmap_retcode	retcode;
1824e8c27ec8Sbaban 	char		*sidprefix = NULL;
1825c5c4113dSnw141292 	idmap_rid_t	rid;
1826c5c4113dSnw141292 	char		*name = NULL, *domain = NULL;
1827c5c4113dSnw141292 
1828e8c27ec8Sbaban 	/* Done if we've both sid and winname */
1829148c5f43SAlan Wright 	if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) {
1830148c5f43SAlan Wright 		/* Don't bother TRACE()ing, too boring */
1831e8c27ec8Sbaban 		return (IDMAP_SUCCESS);
1832148c5f43SAlan Wright 	}
1833c5c4113dSnw141292 
1834e8c27ec8Sbaban 	if (req->id1.idmap_id_u.sid.prefix != NULL) {
183508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/* Lookup sid to winname */
1836e8c27ec8Sbaban 		retcode = lookup_cache_sid2name(cache,
1837e8c27ec8Sbaban 		    req->id1.idmap_id_u.sid.prefix,
1838e8c27ec8Sbaban 		    req->id1.idmap_id_u.sid.rid, &name, &domain, &type);
183908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
184008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/* Lookup winame to sid */
184108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		retcode = lookup_cache_name2sid(cache, req->id1name,
184208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		    req->id1domain, &name, &sidprefix, &rid, &type);
1843e8c27ec8Sbaban 	}
184462c60062Sbaban 
1845e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS) {
1846148c5f43SAlan Wright 		if (retcode == IDMAP_ERR_NOTFOUND) {
1847148c5f43SAlan Wright 			TRACE(req, res, "Not found in name cache");
1848148c5f43SAlan Wright 		} else {
1849148c5f43SAlan Wright 			TRACE(req, res, "Name cache lookup error=%d", retcode);
1850148c5f43SAlan Wright 		}
1851c5c4113dSnw141292 		free(name);
1852c5c4113dSnw141292 		free(domain);
1853e8c27ec8Sbaban 		free(sidprefix);
1854e8c27ec8Sbaban 		return (retcode);
1855e8c27ec8Sbaban 	}
1856e8c27ec8Sbaban 
1857148c5f43SAlan Wright 	req->id1.idtype = type;
1858e8c27ec8Sbaban 
1859e8c27ec8Sbaban 	req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
186008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
186108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/*
186208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * If we found canonical names or domain, use them instead of
186308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * the existing values.
186408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	 */
1865e8c27ec8Sbaban 	if (name != NULL) {
186608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		free(req->id1name);
186708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		req->id1name = name;
1868e8c27ec8Sbaban 	}
186908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (domain != NULL) {
187008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		free(req->id1domain);
1871e8c27ec8Sbaban 		req->id1domain = domain;
187208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
187308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
1874e8c27ec8Sbaban 	if (req->id1.idmap_id_u.sid.prefix == NULL) {
1875e8c27ec8Sbaban 		req->id1.idmap_id_u.sid.prefix = sidprefix;
1876e8c27ec8Sbaban 		req->id1.idmap_id_u.sid.rid = rid;
1877c5c4113dSnw141292 	}
1878148c5f43SAlan Wright 
1879148c5f43SAlan Wright 	TRACE(req, res, "Found in name cache");
1880c5c4113dSnw141292 	return (retcode);
1881c5c4113dSnw141292 }
1882c5c4113dSnw141292 
18834d61c878SJulian Pullen 
18844d61c878SJulian Pullen 
18854d61c878SJulian Pullen static int
ad_lookup_batch_int(lookup_state_t * state,idmap_mapping_batch * batch,idmap_ids_res * result,adutils_ad_t * dir,int how_local,int * num_processed)18864d61c878SJulian Pullen ad_lookup_batch_int(lookup_state_t *state, idmap_mapping_batch *batch,
1887e3f2c991SKeyur Desai 		idmap_ids_res *result, adutils_ad_t *dir, int how_local,
1888e3f2c991SKeyur Desai 		int *num_processed)
1889cd37da74Snw141292 {
1890c5c4113dSnw141292 	idmap_retcode	retcode;
1891e3f2c991SKeyur Desai 	int		i,  num_queued, is_wuser, is_user;
18924d61c878SJulian Pullen 	int		next_request;
1893148c5f43SAlan Wright 	int		retries = 0, esidtype;
1894e8c27ec8Sbaban 	char		**unixname;
1895c5c4113dSnw141292 	idmap_mapping	*req;
1896c5c4113dSnw141292 	idmap_id_res	*res;
1897e8c27ec8Sbaban 	idmap_query_state_t	*qs = NULL;
189848258c6bSjp151216 	idmap_how	*how;
1899479ac375Sdm199847 	char		**dn, **attr, **value;
1900e8c27ec8Sbaban 
19014d61c878SJulian Pullen 	*num_processed = 0;
19024d61c878SJulian Pullen 
1903e8c27ec8Sbaban 	/*
1904e8c27ec8Sbaban 	 * Since req->id2.idtype is unused, we will use it here
1905e8c27ec8Sbaban 	 * to retrieve the value of sid_type. But it needs to be
1906e8c27ec8Sbaban 	 * reset to IDMAP_NONE before we return to prevent xdr
1907e8c27ec8Sbaban 	 * from mis-interpreting req->id2 when it tries to free
1908e8c27ec8Sbaban 	 * the input argument. Other option is to allocate an
1909e8c27ec8Sbaban 	 * array of integers and use it instead for the batched
1910e8c27ec8Sbaban 	 * call. But why un-necessarily allocate memory. That may
1911e8c27ec8Sbaban 	 * be an option if req->id2.idtype cannot be re-used in
1912e8c27ec8Sbaban 	 * future.
1913e3f2c991SKeyur Desai 	 *
19149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * Similarly, we use req->id2.idmap_id_u.uid to return
19159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * uidNumber or gidNumber supplied by IDMU, and reset it
19169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * back to IDMAP_SENTINEL_PID when we're done.  Note that
19179fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * the query always puts the result in req->id2.idmap_id_u.uid,
19189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * not .gid.
1919e8c27ec8Sbaban 	 */
1920c5c4113dSnw141292 retry:
1921e3f2c991SKeyur Desai 	retcode = idmap_lookup_batch_start(dir, state->ad_nqueries,
1922e3f2c991SKeyur Desai 	    state->directory_based_mapping,
1923e3f2c991SKeyur Desai 	    state->defdom,
1924e3f2c991SKeyur Desai 	    &qs);
1925e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS) {
19262b4a7802SBaban Kenkre 		if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
19272b4a7802SBaban Kenkre 		    retries++ < ADUTILS_DEF_NUM_RETRIES)
19280dcc7149Snw141292 			goto retry;
1929349d5d8fSnw141292 		degrade_svc(1, "failed to create batch for AD lookup");
1930e8c27ec8Sbaban 			goto out;
1931c5c4113dSnw141292 	}
19324d61c878SJulian Pullen 	num_queued = 0;
1933c5c4113dSnw141292 
1934c8e26105Sjp151216 	restore_svc();
1935c8e26105Sjp151216 
1936e3f2c991SKeyur Desai 	if (how_local & FOREST_IS_LOCAL) {
19374d61c878SJulian Pullen 		/*
19384d61c878SJulian Pullen 		 * Directory based name mapping is only performed within the
1939e3f2c991SKeyur Desai 		 * joined forest.  We don't trust other "trusted"
19404d61c878SJulian Pullen 		 * forests to provide DS-based name mapping information because
19414d61c878SJulian Pullen 		 * AD's definition of "cross-forest trust" does not encompass
19424d61c878SJulian Pullen 		 * this sort of behavior.
19434d61c878SJulian Pullen 		 */
19444d61c878SJulian Pullen 		idmap_lookup_batch_set_unixattr(qs,
19454d61c878SJulian Pullen 		    state->ad_unixuser_attr, state->ad_unixgroup_attr);
19464d61c878SJulian Pullen 	}
1947e8c27ec8Sbaban 
19484d61c878SJulian Pullen 	for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
1949c5c4113dSnw141292 		req = &batch->idmap_mapping_batch_val[i];
1950c5c4113dSnw141292 		res = &result->ids.ids_val[i];
195148258c6bSjp151216 		how = &res->info.how;
195248258c6bSjp151216 
1953e8c27ec8Sbaban 		retcode = IDMAP_SUCCESS;
1954e8c27ec8Sbaban 		req->id2.idtype = IDMAP_NONE;
19559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		req->id2.idmap_id_u.uid = IDMAP_SENTINEL_PID;
1956c5c4113dSnw141292 
1957e3f2c991SKeyur Desai 		/* Skip if no AD lookup required */
1958e3f2c991SKeyur Desai 		if (!(req->direction & _IDMAP_F_LOOKUP_AD))
1959e8c27ec8Sbaban 			continue;
1960e8c27ec8Sbaban 
1961e3f2c991SKeyur Desai 		/* Skip if we've already tried and gotten a "not found" */
1962e3f2c991SKeyur Desai 		if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD)
1963e3f2c991SKeyur Desai 			continue;
1964e3f2c991SKeyur Desai 
1965e3f2c991SKeyur Desai 		/* Skip if we've already either succeeded or failed */
196696c3a9a0Sbaban 		if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR)
1967c5c4113dSnw141292 			continue;
1968e8c27ec8Sbaban 
1969148c5f43SAlan Wright 		if (IS_ID_SID(req->id1)) {
1970e8c27ec8Sbaban 
1971479ac375Sdm199847 			/* win2unix request: */
1972e8c27ec8Sbaban 
1973e3f2c991SKeyur Desai 			posix_id_t *pid = NULL;
1974479ac375Sdm199847 			unixname = dn = attr = value = NULL;
1975148c5f43SAlan Wright 			esidtype = IDMAP_SID;
1976e3f2c991SKeyur Desai 			if (state->directory_based_mapping ==
1977e3f2c991SKeyur Desai 			    DIRECTORY_MAPPING_NAME &&
1978e3f2c991SKeyur Desai 			    req->id2name == NULL) {
1979e8c27ec8Sbaban 				if (res->id.idtype == IDMAP_UID &&
1980e8c27ec8Sbaban 				    AD_OR_MIXED(state->nm_siduid)) {
1981148c5f43SAlan Wright 					esidtype = IDMAP_USID;
1982e8c27ec8Sbaban 					unixname = &req->id2name;
1983e8c27ec8Sbaban 				} else if (res->id.idtype == IDMAP_GID &&
1984e8c27ec8Sbaban 				    AD_OR_MIXED(state->nm_sidgid)) {
1985148c5f43SAlan Wright 					esidtype = IDMAP_GSID;
1986e8c27ec8Sbaban 					unixname = &req->id2name;
1987e8c27ec8Sbaban 				} else if (AD_OR_MIXED(state->nm_siduid) ||
1988e8c27ec8Sbaban 				    AD_OR_MIXED(state->nm_sidgid)) {
1989e8c27ec8Sbaban 					unixname = &req->id2name;
1990e8c27ec8Sbaban 				}
19914d61c878SJulian Pullen 
1992479ac375Sdm199847 				if (unixname != NULL) {
1993479ac375Sdm199847 					/*
1994479ac375Sdm199847 					 * Get how info for DS-based name
1995479ac375Sdm199847 					 * mapping only if AD or MIXED
1996479ac375Sdm199847 					 * mode is enabled.
1997479ac375Sdm199847 					 */
1998148c5f43SAlan Wright 					idmap_how_clear(&res->info.how);
199948258c6bSjp151216 					res->info.src = IDMAP_MAP_SRC_NEW;
200048258c6bSjp151216 					how->map_type = IDMAP_MAP_TYPE_DS_AD;
2001479ac375Sdm199847 					dn = &how->idmap_how_u.ad.dn;
2002479ac375Sdm199847 					attr = &how->idmap_how_u.ad.attr;
2003479ac375Sdm199847 					value = &how->idmap_how_u.ad.value;
2004479ac375Sdm199847 				}
2005e3f2c991SKeyur Desai 			} else if (state->directory_based_mapping ==
2006e3f2c991SKeyur Desai 			    DIRECTORY_MAPPING_IDMU &&
2007e3f2c991SKeyur Desai 			    (how_local & DOMAIN_IS_LOCAL)) {
2008e3f2c991SKeyur Desai 				/*
2009e3f2c991SKeyur Desai 				 * Ensure that we only do IDMU processing
2010e3f2c991SKeyur Desai 				 * when querying the domain we've joined.
2011e3f2c991SKeyur Desai 				 */
2012e3f2c991SKeyur Desai 				pid = &req->id2.idmap_id_u.uid;
2013e3f2c991SKeyur Desai 				/*
2014e3f2c991SKeyur Desai 				 * Get how info for IDMU based mapping.
2015e3f2c991SKeyur Desai 				 */
2016148c5f43SAlan Wright 				idmap_how_clear(&res->info.how);
2017e3f2c991SKeyur Desai 				res->info.src = IDMAP_MAP_SRC_NEW;
2018e3f2c991SKeyur Desai 				how->map_type = IDMAP_MAP_TYPE_IDMU;
2019e3f2c991SKeyur Desai 				dn = &how->idmap_how_u.idmu.dn;
2020e3f2c991SKeyur Desai 				attr = &how->idmap_how_u.idmu.attr;
2021e3f2c991SKeyur Desai 				value = &how->idmap_how_u.idmu.value;
2022e3f2c991SKeyur Desai 			}
2023e3f2c991SKeyur Desai 
2024479ac375Sdm199847 			if (req->id1.idmap_id_u.sid.prefix != NULL) {
2025479ac375Sdm199847 				/* Lookup AD by SID */
2026c5c4113dSnw141292 				retcode = idmap_sid2name_batch_add1(
2027e8c27ec8Sbaban 				    qs, req->id1.idmap_id_u.sid.prefix,
2028148c5f43SAlan Wright 				    &req->id1.idmap_id_u.sid.rid, esidtype,
2029479ac375Sdm199847 				    dn, attr, value,
2030479ac375Sdm199847 				    (req->id1name == NULL) ?
2031479ac375Sdm199847 				    &req->id1name : NULL,
2032479ac375Sdm199847 				    (req->id1domain == NULL) ?
2033479ac375Sdm199847 				    &req->id1domain : NULL,
2034148c5f43SAlan Wright 				    &req->id2.idtype, unixname,
2035e3f2c991SKeyur Desai 				    pid,
2036479ac375Sdm199847 				    &res->retcode);
20374d61c878SJulian Pullen 				if (retcode == IDMAP_SUCCESS)
20384d61c878SJulian Pullen 					num_queued++;
2039479ac375Sdm199847 			} else {
2040479ac375Sdm199847 				/* Lookup AD by winname */
2041479ac375Sdm199847 				assert(req->id1name != NULL);
2042479ac375Sdm199847 				retcode = idmap_name2sid_batch_add1(
2043479ac375Sdm199847 				    qs, req->id1name, req->id1domain,
2044148c5f43SAlan Wright 				    esidtype,
2045479ac375Sdm199847 				    dn, attr, value,
2046479ac375Sdm199847 				    &req->id1name,
2047479ac375Sdm199847 				    &req->id1.idmap_id_u.sid.prefix,
2048479ac375Sdm199847 				    &req->id1.idmap_id_u.sid.rid,
2049148c5f43SAlan Wright 				    &req->id2.idtype, unixname,
2050e3f2c991SKeyur Desai 				    pid,
2051479ac375Sdm199847 				    &res->retcode);
20524d61c878SJulian Pullen 				if (retcode == IDMAP_SUCCESS)
20534d61c878SJulian Pullen 					num_queued++;
2054479ac375Sdm199847 			}
2055c5c4113dSnw141292 
2056148c5f43SAlan Wright 		} else if (IS_ID_UID(req->id1) || IS_ID_GID(req->id1)) {
2057479ac375Sdm199847 
2058479ac375Sdm199847 			/* unix2win request: */
2059e8c27ec8Sbaban 
2060e8c27ec8Sbaban 			if (res->id.idmap_id_u.sid.prefix != NULL &&
2061e8c27ec8Sbaban 			    req->id2name != NULL) {
20624d61c878SJulian Pullen 				/* Already have SID and winname. done */
2063e8c27ec8Sbaban 				res->retcode = IDMAP_SUCCESS;
2064e8c27ec8Sbaban 				continue;
2065c5c4113dSnw141292 			}
2066c5c4113dSnw141292 
2067e8c27ec8Sbaban 			if (res->id.idmap_id_u.sid.prefix != NULL) {
2068cd37da74Snw141292 				/*
2069e8c27ec8Sbaban 				 * SID but no winname -- lookup AD by
2070e8c27ec8Sbaban 				 * SID to get winname.
2071479ac375Sdm199847 				 * how info is not needed here because
2072479ac375Sdm199847 				 * we are not retrieving unixname from
2073479ac375Sdm199847 				 * AD.
2074cd37da74Snw141292 				 */
20754d61c878SJulian Pullen 
2076e8c27ec8Sbaban 				retcode = idmap_sid2name_batch_add1(
2077e8c27ec8Sbaban 				    qs, res->id.idmap_id_u.sid.prefix,
2078e8c27ec8Sbaban 				    &res->id.idmap_id_u.sid.rid,
2079148c5f43SAlan Wright 				    IDMAP_POSIXID,
2080479ac375Sdm199847 				    NULL, NULL, NULL,
208148258c6bSjp151216 				    &req->id2name,
2082148c5f43SAlan Wright 				    &req->id2domain, &req->id2.idtype,
2083e3f2c991SKeyur Desai 				    NULL, NULL, &res->retcode);
20844d61c878SJulian Pullen 				if (retcode == IDMAP_SUCCESS)
20854d61c878SJulian Pullen 					num_queued++;
2086e8c27ec8Sbaban 			} else if (req->id2name != NULL) {
2087e8c27ec8Sbaban 				/*
2088e8c27ec8Sbaban 				 * winname but no SID -- lookup AD by
2089e8c27ec8Sbaban 				 * winname to get SID.
2090479ac375Sdm199847 				 * how info is not needed here because
2091479ac375Sdm199847 				 * we are not retrieving unixname from
2092479ac375Sdm199847 				 * AD.
2093e8c27ec8Sbaban 				 */
2094e8c27ec8Sbaban 				retcode = idmap_name2sid_batch_add1(
2095e8c27ec8Sbaban 				    qs, req->id2name, req->id2domain,
2096148c5f43SAlan Wright 				    IDMAP_POSIXID,
2097479ac375Sdm199847 				    NULL, NULL, NULL, NULL,
2098e8c27ec8Sbaban 				    &res->id.idmap_id_u.sid.prefix,
2099e8c27ec8Sbaban 				    &res->id.idmap_id_u.sid.rid,
2100148c5f43SAlan Wright 				    &req->id2.idtype, NULL,
2101e3f2c991SKeyur Desai 				    NULL,
2102e8c27ec8Sbaban 				    &res->retcode);
21034d61c878SJulian Pullen 				if (retcode == IDMAP_SUCCESS)
21044d61c878SJulian Pullen 					num_queued++;
2105e3f2c991SKeyur Desai 			} else if (state->directory_based_mapping ==
2106e3f2c991SKeyur Desai 			    DIRECTORY_MAPPING_IDMU &&
2107e3f2c991SKeyur Desai 			    (how_local & DOMAIN_IS_LOCAL)) {
21089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 				assert(req->id1.idmap_id_u.uid !=
21099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 				    IDMAP_SENTINEL_PID);
2110148c5f43SAlan Wright 				is_user = IS_ID_UID(req->id1);
2111e3f2c991SKeyur Desai 				if (res->id.idtype == IDMAP_USID)
2112e3f2c991SKeyur Desai 					is_wuser = 1;
2113e3f2c991SKeyur Desai 				else if (res->id.idtype == IDMAP_GSID)
2114e3f2c991SKeyur Desai 					is_wuser = 0;
2115e3f2c991SKeyur Desai 				else
2116e3f2c991SKeyur Desai 					is_wuser = is_user;
2117e3f2c991SKeyur Desai 
2118e3f2c991SKeyur Desai 				/* IDMU can't do diagonal mappings */
2119e3f2c991SKeyur Desai 				if (is_user != is_wuser)
2120e3f2c991SKeyur Desai 					continue;
2121e3f2c991SKeyur Desai 
2122148c5f43SAlan Wright 				idmap_how_clear(&res->info.how);
2123e3f2c991SKeyur Desai 				res->info.src = IDMAP_MAP_SRC_NEW;
2124e3f2c991SKeyur Desai 				how->map_type = IDMAP_MAP_TYPE_IDMU;
2125e3f2c991SKeyur Desai 				retcode = idmap_pid2sid_batch_add1(
2126e3f2c991SKeyur Desai 				    qs, req->id1.idmap_id_u.uid, is_user,
2127e3f2c991SKeyur Desai 				    &how->idmap_how_u.ad.dn,
2128e3f2c991SKeyur Desai 				    &how->idmap_how_u.ad.attr,
2129e3f2c991SKeyur Desai 				    &how->idmap_how_u.ad.value,
2130e3f2c991SKeyur Desai 				    &res->id.idmap_id_u.sid.prefix,
2131e3f2c991SKeyur Desai 				    &res->id.idmap_id_u.sid.rid,
2132e3f2c991SKeyur Desai 				    &req->id2name, &req->id2domain,
2133148c5f43SAlan Wright 				    &req->id2.idtype, &res->retcode);
2134e3f2c991SKeyur Desai 				if (retcode == IDMAP_SUCCESS)
2135e3f2c991SKeyur Desai 					num_queued++;
2136e8c27ec8Sbaban 			} else if (req->id1name != NULL) {
2137e8c27ec8Sbaban 				/*
21384d61c878SJulian Pullen 				 * No SID and no winname but we've unixname.
21394d61c878SJulian Pullen 				 * Lookup AD by unixname to get SID.
2140e8c27ec8Sbaban 				 */
2141148c5f43SAlan Wright 				is_user = (IS_ID_UID(req->id1)) ? 1 : 0;
2142e8c27ec8Sbaban 				if (res->id.idtype == IDMAP_USID)
2143e8c27ec8Sbaban 					is_wuser = 1;
2144e8c27ec8Sbaban 				else if (res->id.idtype == IDMAP_GSID)
2145e8c27ec8Sbaban 					is_wuser = 0;
2146e8c27ec8Sbaban 				else
2147e8c27ec8Sbaban 					is_wuser = is_user;
21484d61c878SJulian Pullen 
2149148c5f43SAlan Wright 				idmap_how_clear(&res->info.how);
215048258c6bSjp151216 				res->info.src = IDMAP_MAP_SRC_NEW;
215148258c6bSjp151216 				how->map_type = IDMAP_MAP_TYPE_DS_AD;
2152e8c27ec8Sbaban 				retcode = idmap_unixname2sid_batch_add1(
2153e8c27ec8Sbaban 				    qs, req->id1name, is_user, is_wuser,
215448258c6bSjp151216 				    &how->idmap_how_u.ad.dn,
215548258c6bSjp151216 				    &how->idmap_how_u.ad.attr,
215648258c6bSjp151216 				    &how->idmap_how_u.ad.value,
2157e8c27ec8Sbaban 				    &res->id.idmap_id_u.sid.prefix,
2158e8c27ec8Sbaban 				    &res->id.idmap_id_u.sid.rid,
2159e8c27ec8Sbaban 				    &req->id2name, &req->id2domain,
2160148c5f43SAlan Wright 				    &req->id2.idtype, &res->retcode);
21614d61c878SJulian Pullen 				if (retcode == IDMAP_SUCCESS)
21624d61c878SJulian Pullen 					num_queued++;
2163e8c27ec8Sbaban 			}
2164cd37da74Snw141292 		}
2165cd37da74Snw141292 
21664d61c878SJulian Pullen 		if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
21674d61c878SJulian Pullen 			req->direction |= _IDMAP_F_LOOKUP_OTHER_AD;
21684d61c878SJulian Pullen 			retcode = IDMAP_SUCCESS;
21694d61c878SJulian Pullen 		} else if (retcode != IDMAP_SUCCESS) {
21704d61c878SJulian Pullen 			break;
21714d61c878SJulian Pullen 		}
21724d61c878SJulian Pullen 	} /* End of for loop */
21734d61c878SJulian Pullen 
217496c3a9a0Sbaban 	if (retcode == IDMAP_SUCCESS) {
217596c3a9a0Sbaban 		/* add keeps track if we added an entry to the batch */
21764d61c878SJulian Pullen 		if (num_queued > 0)
21770dcc7149Snw141292 			retcode = idmap_lookup_batch_end(&qs);
217896c3a9a0Sbaban 		else
217996c3a9a0Sbaban 			idmap_lookup_release_batch(&qs);
2180e3f2c991SKeyur Desai 	} else {
2181e3f2c991SKeyur Desai 		idmap_lookup_release_batch(&qs);
2182e3f2c991SKeyur Desai 		num_queued = 0;
2183e3f2c991SKeyur Desai 		next_request = i + 1;
218496c3a9a0Sbaban 	}
2185cd37da74Snw141292 
21862b4a7802SBaban Kenkre 	if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
21872b4a7802SBaban Kenkre 	    retries++ < ADUTILS_DEF_NUM_RETRIES)
2188c5c4113dSnw141292 		goto retry;
2189c8e26105Sjp151216 	else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
2190349d5d8fSnw141292 		degrade_svc(1, "some AD lookups timed out repeatedly");
2191c5c4113dSnw141292 
21924d61c878SJulian Pullen 	if (retcode != IDMAP_SUCCESS) {
21934d61c878SJulian Pullen 		/* Mark any unproccessed requests for an other AD */
21944d61c878SJulian Pullen 		for (i = next_request; i < batch->idmap_mapping_batch_len;
21954d61c878SJulian Pullen 		    i++) {
21964d61c878SJulian Pullen 			req = &batch->idmap_mapping_batch_val[i];
21974d61c878SJulian Pullen 			req->direction |= _IDMAP_F_LOOKUP_OTHER_AD;
21984d61c878SJulian Pullen 
21994d61c878SJulian Pullen 		}
22004d61c878SJulian Pullen 	}
22014d61c878SJulian Pullen 
2202e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS)
2203e8c27ec8Sbaban 		idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests");
2204c5c4113dSnw141292 
2205c5c4113dSnw141292 out:
2206e8c27ec8Sbaban 	/*
2207e8c27ec8Sbaban 	 * This loop does the following:
2208479ac375Sdm199847 	 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request.
2209479ac375Sdm199847 	 * 2. Reset req->id2.idtype to IDMAP_NONE
2210479ac375Sdm199847 	 * 3. If batch_start or batch_add failed then set the status
2211479ac375Sdm199847 	 *    of each request marked for AD lookup to that error.
22124d61c878SJulian Pullen 	 * 4. Evaluate the type of the AD object (i.e. user or group)
22134d61c878SJulian Pullen 	 *    and update the idtype in request.
2214e8c27ec8Sbaban 	 */
2215e8c27ec8Sbaban 	for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
2216148c5f43SAlan Wright 		idmap_id_type type;
2217e3f2c991SKeyur Desai 		uid_t posix_id;
2218e3f2c991SKeyur Desai 
2219e8c27ec8Sbaban 		req = &batch->idmap_mapping_batch_val[i];
2220e8c27ec8Sbaban 		type = req->id2.idtype;
2221e8c27ec8Sbaban 		req->id2.idtype = IDMAP_NONE;
2222e3f2c991SKeyur Desai 		posix_id = req->id2.idmap_id_u.uid;
22239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		req->id2.idmap_id_u.uid = IDMAP_SENTINEL_PID;
22245e0794bcSbaban 		res = &result->ids.ids_val[i];
2225e3f2c991SKeyur Desai 
2226e3f2c991SKeyur Desai 		/*
2227e3f2c991SKeyur Desai 		 * If it didn't need AD lookup, ignore it.
2228e3f2c991SKeyur Desai 		 */
2229e3f2c991SKeyur Desai 		if (!(req->direction & _IDMAP_F_LOOKUP_AD))
2230e8c27ec8Sbaban 			continue;
2231e8c27ec8Sbaban 
2232e3f2c991SKeyur Desai 		/*
2233e3f2c991SKeyur Desai 		 * If we deferred it this time, reset for the next
2234e3f2c991SKeyur Desai 		 * AD server.
2235e3f2c991SKeyur Desai 		 */
2236e3f2c991SKeyur Desai 		if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD) {
2237e3f2c991SKeyur Desai 			req->direction &= ~_IDMAP_F_LOOKUP_OTHER_AD;
2238e3f2c991SKeyur Desai 			continue;
2239e3f2c991SKeyur Desai 		}
2240e3f2c991SKeyur Desai 
22414d61c878SJulian Pullen 		/* Count number processed */
22424d61c878SJulian Pullen 		(*num_processed)++;
22434d61c878SJulian Pullen 
2244479ac375Sdm199847 		/* Reset AD lookup flag */
2245479ac375Sdm199847 		req->direction &= ~(_IDMAP_F_LOOKUP_AD);
2246479ac375Sdm199847 
2247479ac375Sdm199847 		/*
22484d61c878SJulian Pullen 		 * If batch_start or batch_add failed then set the
22494d61c878SJulian Pullen 		 * status of each request marked for AD lookup to
22504d61c878SJulian Pullen 		 * that error.
2251479ac375Sdm199847 		 */
2252e8c27ec8Sbaban 		if (retcode != IDMAP_SUCCESS) {
2253e8c27ec8Sbaban 			res->retcode = retcode;
2254e8c27ec8Sbaban 			continue;
2255e8c27ec8Sbaban 		}
2256e8c27ec8Sbaban 
225748258c6bSjp151216 		if (res->retcode == IDMAP_ERR_NOTFOUND) {
225848258c6bSjp151216 			/* Nothing found - remove the preset info */
2259148c5f43SAlan Wright 			idmap_how_clear(&res->info.how);
226048258c6bSjp151216 		}
226148258c6bSjp151216 
2262148c5f43SAlan Wright 		if (IS_ID_SID(req->id1)) {
2263cb174861Sjoyce mcintosh 			if (res->retcode == IDMAP_ERR_NOTFOUND) {
2264cb174861Sjoyce mcintosh 				TRACE(req, res, "Not found in AD");
2265e8c27ec8Sbaban 				continue;
2266cb174861Sjoyce mcintosh 			}
2267cb174861Sjoyce mcintosh 			if (res->retcode != IDMAP_SUCCESS) {
2268cb174861Sjoyce mcintosh 				TRACE(req, res, "AD lookup error=%d",
2269cb174861Sjoyce mcintosh 				    res->retcode);
2270cb174861Sjoyce mcintosh 				continue;
2271cb174861Sjoyce mcintosh 			}
2272479ac375Sdm199847 			/* Evaluate result type */
2273e8c27ec8Sbaban 			switch (type) {
2274148c5f43SAlan Wright 			case IDMAP_USID:
2275e8c27ec8Sbaban 				if (res->id.idtype == IDMAP_POSIXID)
2276e8c27ec8Sbaban 					res->id.idtype = IDMAP_UID;
2277e3f2c991SKeyur Desai 				/*
2278e3f2c991SKeyur Desai 				 * We found a user.  If we got information
2279e3f2c991SKeyur Desai 				 * from IDMU and we were expecting a user,
2280e3f2c991SKeyur Desai 				 * copy the id.
2281e3f2c991SKeyur Desai 				 */
22829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 				if (posix_id != IDMAP_SENTINEL_PID &&
2283e3f2c991SKeyur Desai 				    res->id.idtype == IDMAP_UID) {
2284e3f2c991SKeyur Desai 					res->id.idmap_id_u.uid = posix_id;
2285e3f2c991SKeyur Desai 					res->direction = IDMAP_DIRECTION_BI;
2286e3f2c991SKeyur Desai 					res->info.how.map_type =
2287e3f2c991SKeyur Desai 					    IDMAP_MAP_TYPE_IDMU;
2288e3f2c991SKeyur Desai 					res->info.src = IDMAP_MAP_SRC_NEW;
2289e3f2c991SKeyur Desai 				}
2290e8c27ec8Sbaban 				req->id1.idtype = IDMAP_USID;
2291e8c27ec8Sbaban 				break;
22924d61c878SJulian Pullen 
2293148c5f43SAlan Wright 			case IDMAP_GSID:
2294e8c27ec8Sbaban 				if (res->id.idtype == IDMAP_POSIXID)
2295e8c27ec8Sbaban 					res->id.idtype = IDMAP_GID;
2296e3f2c991SKeyur Desai 				/*
2297e3f2c991SKeyur Desai 				 * We found a group.  If we got information
2298e3f2c991SKeyur Desai 				 * from IDMU and we were expecting a group,
2299e3f2c991SKeyur Desai 				 * copy the id.
2300e3f2c991SKeyur Desai 				 */
23019fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 				if (posix_id != IDMAP_SENTINEL_PID &&
2302e3f2c991SKeyur Desai 				    res->id.idtype == IDMAP_GID) {
2303e3f2c991SKeyur Desai 					res->id.idmap_id_u.gid = posix_id;
2304e3f2c991SKeyur Desai 					res->direction = IDMAP_DIRECTION_BI;
2305e3f2c991SKeyur Desai 					res->info.how.map_type =
2306e3f2c991SKeyur Desai 					    IDMAP_MAP_TYPE_IDMU;
2307e3f2c991SKeyur Desai 					res->info.src = IDMAP_MAP_SRC_NEW;
2308e3f2c991SKeyur Desai 				}
2309e8c27ec8Sbaban 				req->id1.idtype = IDMAP_GSID;
2310e8c27ec8Sbaban 				break;
23114d61c878SJulian Pullen 
2312e8c27ec8Sbaban 			default:
2313e8c27ec8Sbaban 				res->retcode = IDMAP_ERR_SID;
2314e8c27ec8Sbaban 				break;
2315e8c27ec8Sbaban 			}
2316cb174861Sjoyce mcintosh 			TRACE(req, res, "Found in AD");
2317479ac375Sdm199847 			if (res->retcode == IDMAP_SUCCESS &&
2318479ac375Sdm199847 			    req->id1name != NULL &&
2319479ac375Sdm199847 			    (req->id2name == NULL ||
23209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			    res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID) &&
2321479ac375Sdm199847 			    NLDAP_MODE(res->id.idtype, state)) {
2322479ac375Sdm199847 				req->direction |= _IDMAP_F_LOOKUP_NLDAP;
2323479ac375Sdm199847 				state->nldap_nqueries++;
2324479ac375Sdm199847 			}
2325148c5f43SAlan Wright 		} else if (IS_ID_UID(req->id1) || IS_ID_GID(req->id1)) {
2326e8c27ec8Sbaban 			if (res->retcode != IDMAP_SUCCESS) {
2327e8c27ec8Sbaban 				if ((!(IDMAP_FATAL_ERROR(res->retcode))) &&
2328e8c27ec8Sbaban 				    res->id.idmap_id_u.sid.prefix == NULL &&
2329cb174861Sjoyce mcintosh 				    req->id2name == NULL) {
2330e8c27ec8Sbaban 					/*
2331e3f2c991SKeyur Desai 					 * If AD lookup by unixname or pid
23324d61c878SJulian Pullen 					 * failed with non fatal error
23334d61c878SJulian Pullen 					 * then clear the error (ie set
23344d61c878SJulian Pullen 					 * res->retcode to success).
23354d61c878SJulian Pullen 					 * This allows the next pass to
23364d61c878SJulian Pullen 					 * process other mapping
2337479ac375Sdm199847 					 * mechanisms for this request.
2338e8c27ec8Sbaban 					 */
2339cb174861Sjoyce mcintosh 					if (res->retcode ==
2340cb174861Sjoyce mcintosh 					    IDMAP_ERR_NOTFOUND) {
2341cb174861Sjoyce mcintosh 						/* This is not an error */
2342e8c27ec8Sbaban 						res->retcode = IDMAP_SUCCESS;
2343cb174861Sjoyce mcintosh 						TRACE(req, res,
2344cb174861Sjoyce mcintosh 						    "Not found in AD");
2345cb174861Sjoyce mcintosh 					} else {
2346cb174861Sjoyce mcintosh 						TRACE(req, res,
2347cb174861Sjoyce mcintosh 						"AD lookup error (ignored)");
2348cb174861Sjoyce mcintosh 						res->retcode = IDMAP_SUCCESS;
2349cb174861Sjoyce mcintosh 					}
2350cb174861Sjoyce mcintosh 				} else {
2351cb174861Sjoyce mcintosh 					TRACE(req, res, "AD lookup error");
2352cb174861Sjoyce mcintosh 				}
2353e8c27ec8Sbaban 				continue;
2354e8c27ec8Sbaban 			}
2355479ac375Sdm199847 			/* Evaluate result type */
2356e8c27ec8Sbaban 			switch (type) {
2357148c5f43SAlan Wright 			case IDMAP_USID:
2358148c5f43SAlan Wright 			case IDMAP_GSID:
2359e8c27ec8Sbaban 				if (res->id.idtype == IDMAP_SID)
2360148c5f43SAlan Wright 					res->id.idtype = type;
2361e8c27ec8Sbaban 				break;
23624d61c878SJulian Pullen 
2363e8c27ec8Sbaban 			default:
2364e8c27ec8Sbaban 				res->retcode = IDMAP_ERR_SID;
2365e8c27ec8Sbaban 				break;
2366e8c27ec8Sbaban 			}
2367cb174861Sjoyce mcintosh 			TRACE(req, res, "Found in AD");
2368e8c27ec8Sbaban 		}
2369e8c27ec8Sbaban 	}
2370e8c27ec8Sbaban 
23714d61c878SJulian Pullen 	return (retcode);
23724d61c878SJulian Pullen }
23734d61c878SJulian Pullen 
23744d61c878SJulian Pullen 
23754d61c878SJulian Pullen 
23764d61c878SJulian Pullen /*
23774d61c878SJulian Pullen  * Batch AD lookups
23784d61c878SJulian Pullen  */
23794d61c878SJulian Pullen idmap_retcode
ad_lookup_batch(lookup_state_t * state,idmap_mapping_batch * batch,idmap_ids_res * result)23804d61c878SJulian Pullen ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch,
23814d61c878SJulian Pullen 		idmap_ids_res *result)
23824d61c878SJulian Pullen {
23834d61c878SJulian Pullen 	idmap_retcode	retcode;
23844d61c878SJulian Pullen 	int		i, j;
23854d61c878SJulian Pullen 	idmap_mapping	*req;
23864d61c878SJulian Pullen 	idmap_id_res	*res;
23874d61c878SJulian Pullen 	int		num_queries;
23884d61c878SJulian Pullen 	int		num_processed;
23894d61c878SJulian Pullen 
23904d61c878SJulian Pullen 	if (state->ad_nqueries == 0)
23914d61c878SJulian Pullen 		return (IDMAP_SUCCESS);
23924d61c878SJulian Pullen 
23934d61c878SJulian Pullen 	for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
23944d61c878SJulian Pullen 		req = &batch->idmap_mapping_batch_val[i];
23954d61c878SJulian Pullen 		res = &result->ids.ids_val[i];
23964d61c878SJulian Pullen 
23974d61c878SJulian Pullen 		/* Skip if not marked for AD lookup or already in error. */
23984d61c878SJulian Pullen 		if (!(req->direction & _IDMAP_F_LOOKUP_AD) ||
23994d61c878SJulian Pullen 		    res->retcode != IDMAP_SUCCESS)
24004d61c878SJulian Pullen 			continue;
24014d61c878SJulian Pullen 
24024d61c878SJulian Pullen 		/* Init status */
24034d61c878SJulian Pullen 		res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
24044d61c878SJulian Pullen 	}
24054d61c878SJulian Pullen 
24064d61c878SJulian Pullen 	RDLOCK_CONFIG();
24074d61c878SJulian Pullen 	num_queries = state->ad_nqueries;
24084d61c878SJulian Pullen 
2409e3f2c991SKeyur Desai 	if (_idmapdstate.num_gcs == 0 && _idmapdstate.num_dcs == 0) {
24104d61c878SJulian Pullen 		/* Case of no ADs */
24114d61c878SJulian Pullen 		retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
24124d61c878SJulian Pullen 		for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
24134d61c878SJulian Pullen 			req = &batch->idmap_mapping_batch_val[i];
24144d61c878SJulian Pullen 			res = &result->ids.ids_val[i];
24154d61c878SJulian Pullen 			if (!(req->direction & _IDMAP_F_LOOKUP_AD))
24164d61c878SJulian Pullen 				continue;
24174d61c878SJulian Pullen 			req->direction &= ~(_IDMAP_F_LOOKUP_AD);
24184d61c878SJulian Pullen 			res->retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
24194d61c878SJulian Pullen 		}
2420e3f2c991SKeyur Desai 		goto out;
24214d61c878SJulian Pullen 	}
2422e3f2c991SKeyur Desai 
2423e3f2c991SKeyur Desai 	if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) {
2424e3f2c991SKeyur Desai 		for (i = 0; i < _idmapdstate.num_dcs && num_queries > 0; i++) {
2425e3f2c991SKeyur Desai 
2426e3f2c991SKeyur Desai 			retcode = ad_lookup_batch_int(state, batch,
2427e3f2c991SKeyur Desai 			    result, _idmapdstate.dcs[i],
2428e3f2c991SKeyur Desai 			    i == 0 ? DOMAIN_IS_LOCAL|FOREST_IS_LOCAL : 0,
2429e3f2c991SKeyur Desai 			    &num_processed);
2430e3f2c991SKeyur Desai 			num_queries -= num_processed;
2431e3f2c991SKeyur Desai 
2432e3f2c991SKeyur Desai 		}
2433e3f2c991SKeyur Desai 	}
2434e3f2c991SKeyur Desai 
2435e3f2c991SKeyur Desai 	for (i = 0; i < _idmapdstate.num_gcs && num_queries > 0; i++) {
2436e3f2c991SKeyur Desai 
2437e3f2c991SKeyur Desai 		retcode = ad_lookup_batch_int(state, batch, result,
2438e3f2c991SKeyur Desai 		    _idmapdstate.gcs[i],
2439e3f2c991SKeyur Desai 		    i == 0 ? FOREST_IS_LOCAL : 0,
2440e3f2c991SKeyur Desai 		    &num_processed);
2441e3f2c991SKeyur Desai 		num_queries -= num_processed;
2442e3f2c991SKeyur Desai 
2443e3f2c991SKeyur Desai 	}
2444e3f2c991SKeyur Desai 
2445e3f2c991SKeyur Desai 	/*
2446e3f2c991SKeyur Desai 	 * There are no more ADs to try.  Return errors for any
2447e3f2c991SKeyur Desai 	 * remaining requests.
2448e3f2c991SKeyur Desai 	 */
2449e3f2c991SKeyur Desai 	if (num_queries > 0) {
2450e3f2c991SKeyur Desai 		for (j = 0; j < batch->idmap_mapping_batch_len; j++) {
2451e3f2c991SKeyur Desai 			req = &batch->idmap_mapping_batch_val[j];
2452e3f2c991SKeyur Desai 			res = &result->ids.ids_val[j];
2453e3f2c991SKeyur Desai 			if (!(req->direction & _IDMAP_F_LOOKUP_AD))
2454e3f2c991SKeyur Desai 				continue;
2455e3f2c991SKeyur Desai 			req->direction &= ~(_IDMAP_F_LOOKUP_AD);
2456e3f2c991SKeyur Desai 			res->retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
2457e3f2c991SKeyur Desai 		}
2458e3f2c991SKeyur Desai 	}
2459e3f2c991SKeyur Desai 
2460e3f2c991SKeyur Desai out:
24614d61c878SJulian Pullen 	UNLOCK_CONFIG();
24624d61c878SJulian Pullen 
2463e8c27ec8Sbaban 	/* AD lookups done. Reset state->ad_nqueries and return */
2464e8c27ec8Sbaban 	state->ad_nqueries = 0;
2465c5c4113dSnw141292 	return (retcode);
2466c5c4113dSnw141292 }
2467c5c4113dSnw141292 
2468cd37da74Snw141292 /*
2469cd37da74Snw141292  * Convention when processing win2unix requests:
2470cd37da74Snw141292  *
2471cd37da74Snw141292  * Windows identity:
2472cd37da74Snw141292  * req->id1name =
2473cd37da74Snw141292  *              winname if given otherwise winname found will be placed
2474cd37da74Snw141292  *              here.
2475cd37da74Snw141292  * req->id1domain =
2476cd37da74Snw141292  *              windomain if given otherwise windomain found will be
2477cd37da74Snw141292  *              placed here.
2478cd37da74Snw141292  * req->id1.idtype =
2479cd37da74Snw141292  *              Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll
2480cd37da74Snw141292  *              be set to IDMAP_USID/GSID depending upon whether the
2481cd37da74Snw141292  *              given SID is user or group respectively. The user/group-ness
2482cd37da74Snw141292  *              is determined either when looking up well-known SIDs table OR
2483fe1c642dSBill Krier  *              if the SID is found in namecache OR by ad_lookup_batch().
2484cd37da74Snw141292  * req->id1..sid.[prefix, rid] =
2485cd37da74Snw141292  *              SID if given otherwise SID found will be placed here.
2486cd37da74Snw141292  *
2487cd37da74Snw141292  * Unix identity:
2488cd37da74Snw141292  * req->id2name =
2489cd37da74Snw141292  *              unixname found will be placed here.
2490cd37da74Snw141292  * req->id2domain =
2491cd37da74Snw141292  *              NOT USED
2492cd37da74Snw141292  * res->id.idtype =
2493cd37da74Snw141292  *              Target type initialized from req->id2.idtype. If
2494cd37da74Snw141292  *              it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found
2495cd37da74Snw141292  *              will be placed here.
2496cd37da74Snw141292  * res->id..[uid or gid] =
2497cd37da74Snw141292  *              UID/GID found will be placed here.
2498cd37da74Snw141292  *
2499cd37da74Snw141292  * Others:
2500cd37da74Snw141292  * res->retcode =
2501cd37da74Snw141292  *              Return status for this request will be placed here.
2502cd37da74Snw141292  * res->direction =
2503cd37da74Snw141292  *              Direction found will be placed here. Direction
2504cd37da74Snw141292  *              meaning whether the resultant mapping is valid
2505cd37da74Snw141292  *              only from win2unix or bi-directional.
2506cd37da74Snw141292  * req->direction =
2507cd37da74Snw141292  *              INTERNAL USE. Used by idmapd to set various
2508cd37da74Snw141292  *              flags (_IDMAP_F_xxxx) to aid in processing
2509cd37da74Snw141292  *              of the request.
2510cd37da74Snw141292  * req->id2.idtype =
2511cd37da74Snw141292  *              INTERNAL USE. Initially this is the requested target
2512cd37da74Snw141292  *              type and is used to initialize res->id.idtype.
2513cd37da74Snw141292  *              ad_lookup_batch() uses this field temporarily to store
2514cd37da74Snw141292  *              sid_type obtained by the batched AD lookups and after
2515cd37da74Snw141292  *              use resets it to IDMAP_NONE to prevent xdr from
2516cd37da74Snw141292  *              mis-interpreting the contents of req->id2.
2517e3f2c991SKeyur Desai  * req->id2.idmap_id_u.uid =
2518e3f2c991SKeyur Desai  *              INTERNAL USE.  If the AD lookup finds IDMU data
2519e3f2c991SKeyur Desai  *		(uidNumber or gidNumber, depending on the type of
2520e3f2c991SKeyur Desai  *		the entry), it's left here.
2521cd37da74Snw141292  */
2522cd37da74Snw141292 
2523cd37da74Snw141292 /*
2524cd37da74Snw141292  * This function does the following:
2525cd37da74Snw141292  * 1. Lookup well-known SIDs table.
2526cd37da74Snw141292  * 2. Check if the given SID is a local-SID and if so extract UID/GID from it.
2527cd37da74Snw141292  * 3. Lookup cache.
2528cd37da74Snw141292  * 4. Check if the client does not want new mapping to be allocated
2529cd37da74Snw141292  *    in which case this pass is the final pass.
2530cd37da74Snw141292  * 5. Set AD lookup flag if it determines that the next stage needs
2531cd37da74Snw141292  *    to do AD lookup.
2532cd37da74Snw141292  */
2533c5c4113dSnw141292 idmap_retcode
sid2pid_first_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)2534479ac375Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req,
2535cd37da74Snw141292 		idmap_id_res *res)
2536cd37da74Snw141292 {
2537c5c4113dSnw141292 	idmap_retcode	retcode;
2538e8c27ec8Sbaban 	int		wksid;
2539c5c4113dSnw141292 
2540e8c27ec8Sbaban 	/* Initialize result */
2541e8c27ec8Sbaban 	res->id.idtype = req->id2.idtype;
25429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	res->id.idmap_id_u.uid = IDMAP_SENTINEL_PID;
2543e8c27ec8Sbaban 	res->direction = IDMAP_DIRECTION_UNDEF;
2544e8c27ec8Sbaban 	wksid = 0;
2545c5c4113dSnw141292 
2546cf5b5989Sdm199847 	if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) {
2547fe1c642dSBill Krier 		/* They have to give us *something* to work with! */
2548e8c27ec8Sbaban 		if (req->id1name == NULL) {
2549e8c27ec8Sbaban 			retcode = IDMAP_ERR_ARG;
2550c5c4113dSnw141292 			goto out;
2551c5c4113dSnw141292 		}
2552fe1c642dSBill Krier 
2553e8c27ec8Sbaban 		/* sanitize sidprefix */
2554e8c27ec8Sbaban 		free(req->id1.idmap_id_u.sid.prefix);
2555e8c27ec8Sbaban 		req->id1.idmap_id_u.sid.prefix = NULL;
2556fe1c642dSBill Krier 
2557fe1c642dSBill Krier 		/* Allow for a fully-qualified name in the "name" parameter */
2558fe1c642dSBill Krier 		if (req->id1domain == NULL) {
2559fe1c642dSBill Krier 			char *p;
2560fe1c642dSBill Krier 			p = strchr(req->id1name, '@');
2561fe1c642dSBill Krier 			if (p != NULL) {
2562fe1c642dSBill Krier 				char *q;
2563fe1c642dSBill Krier 				q = req->id1name;
2564cb174861Sjoyce mcintosh 				req->id1name = uu_strndup(q, p - req->id1name);
2565fe1c642dSBill Krier 				req->id1domain = strdup(p+1);
2566fe1c642dSBill Krier 				free(q);
2567fe1c642dSBill Krier 				if (req->id1name == NULL ||
2568fe1c642dSBill Krier 				    req->id1domain == NULL) {
2569fe1c642dSBill Krier 					retcode = IDMAP_ERR_MEMORY;
2570fe1c642dSBill Krier 					goto out;
2571fe1c642dSBill Krier 				}
2572fe1c642dSBill Krier 			}
2573fe1c642dSBill Krier 		}
2574e8c27ec8Sbaban 	}
2575c5c4113dSnw141292 
2576e8c27ec8Sbaban 	/* Lookup well-known SIDs table */
2577e8c27ec8Sbaban 	retcode = lookup_wksids_sid2pid(req, res, &wksid);
2578148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
2579148c5f43SAlan Wright 		/* Found a well-known account with a hardwired mapping */
2580148c5f43SAlan Wright 		TRACE(req, res, "Hardwired mapping");
2581c5c4113dSnw141292 		goto out;
2582148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
2583148c5f43SAlan Wright 		TRACE(req, res,
2584148c5f43SAlan Wright 		    "Well-known account lookup failed, code %d", retcode);
2585148c5f43SAlan Wright 		goto out;
2586148c5f43SAlan Wright 	}
2587c5c4113dSnw141292 
2588148c5f43SAlan Wright 	if (wksid) {
2589148c5f43SAlan Wright 		/* Found a well-known account, but no mapping */
2590148c5f43SAlan Wright 		TRACE(req, res, "Well-known account");
2591148c5f43SAlan Wright 	} else {
2592148c5f43SAlan Wright 		TRACE(req, res, "Not a well-known account");
2593148c5f43SAlan Wright 
25942b4a7802SBaban Kenkre 		/* Check if this is a localsid */
2595e8c27ec8Sbaban 		retcode = lookup_localsid2pid(req, res);
2596148c5f43SAlan Wright 		if (retcode == IDMAP_SUCCESS) {
2597148c5f43SAlan Wright 			TRACE(req, res, "Local SID");
2598e8c27ec8Sbaban 			goto out;
2599148c5f43SAlan Wright 		} else if (retcode != IDMAP_ERR_NOTFOUND) {
2600148c5f43SAlan Wright 			TRACE(req, res,
2601148c5f43SAlan Wright 			    "Local SID lookup error=%d", retcode);
2602148c5f43SAlan Wright 			goto out;
2603148c5f43SAlan Wright 		}
2604148c5f43SAlan Wright 		TRACE(req, res, "Not a local SID");
26052b4a7802SBaban Kenkre 
26062b4a7802SBaban Kenkre 		if (ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)) {
26074d61c878SJulian Pullen 			retcode = IDMAP_ERR_NONE_GENERATED;
26082b4a7802SBaban Kenkre 			goto out;
26092b4a7802SBaban Kenkre 		}
2610e8c27ec8Sbaban 	}
2611e8c27ec8Sbaban 
2612fe1c642dSBill Krier 	/*
2613fe1c642dSBill Krier 	 * If this is a name-based request and we don't have a domain,
2614fe1c642dSBill Krier 	 * use the default domain.  Note that the well-known identity
2615fe1c642dSBill Krier 	 * cases will have supplied a SID prefix already, and that we
2616fe1c642dSBill Krier 	 * don't (yet?) support looking up a local user through a Windows
2617fe1c642dSBill Krier 	 * style name.
2618fe1c642dSBill Krier 	 */
2619fe1c642dSBill Krier 	if (req->id1.idmap_id_u.sid.prefix == NULL &&
2620fe1c642dSBill Krier 	    req->id1name != NULL && req->id1domain == NULL) {
2621fe1c642dSBill Krier 		if (state->defdom == NULL) {
2622fe1c642dSBill Krier 			retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
2623fe1c642dSBill Krier 			goto out;
2624fe1c642dSBill Krier 		}
2625fe1c642dSBill Krier 		req->id1domain = strdup(state->defdom);
2626fe1c642dSBill Krier 		if (req->id1domain == NULL) {
2627fe1c642dSBill Krier 			retcode = IDMAP_ERR_MEMORY;
2628fe1c642dSBill Krier 			goto out;
2629fe1c642dSBill Krier 		}
2630148c5f43SAlan Wright 		TRACE(req, res, "Added default domain");
2631fe1c642dSBill Krier 	}
2632fe1c642dSBill Krier 
2633e8c27ec8Sbaban 	/* Lookup cache */
2634479ac375Sdm199847 	retcode = lookup_cache_sid2pid(state->cache, req, res);
2635148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
2636148c5f43SAlan Wright 		TRACE(req, res, "Found in mapping cache");
2637c5c4113dSnw141292 		goto out;
2638148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
2639148c5f43SAlan Wright 		TRACE(req, res, "Mapping cache lookup error=%d", retcode);
2640148c5f43SAlan Wright 		goto out;
2641148c5f43SAlan Wright 	}
2642148c5f43SAlan Wright 	TRACE(req, res, "Not found in mapping cache");
2643c5c4113dSnw141292 
2644c5c4113dSnw141292 	if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) {
26454d61c878SJulian Pullen 		retcode = IDMAP_ERR_NONE_GENERATED;
2646c5c4113dSnw141292 		goto out;
2647c5c4113dSnw141292 	}
2648c5c4113dSnw141292 
2649c5c4113dSnw141292 	/*
2650e8c27ec8Sbaban 	 * Failed to find non-expired entry in cache. Next step is
2651e8c27ec8Sbaban 	 * to determine if this request needs to be batched for AD lookup.
2652e8c27ec8Sbaban 	 *
2653e8c27ec8Sbaban 	 * At this point we have either sid or winname or both. If we don't
2654e8c27ec8Sbaban 	 * have both then lookup name_cache for the sid or winname
2655e8c27ec8Sbaban 	 * whichever is missing. If not found then this request will be
2656e8c27ec8Sbaban 	 * batched for AD lookup.
2657e8c27ec8Sbaban 	 */
2658479ac375Sdm199847 	retcode = lookup_name_cache(state->cache, req, res);
2659148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
2660148c5f43SAlan Wright 		if (res->id.idtype == IDMAP_POSIXID) {
2661148c5f43SAlan Wright 			if (req->id1.idtype == IDMAP_USID)
2662148c5f43SAlan Wright 				res->id.idtype = IDMAP_UID;
2663148c5f43SAlan Wright 			else
2664148c5f43SAlan Wright 				res->id.idtype = IDMAP_GID;
2665148c5f43SAlan Wright 		}
2666148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND)
2667e8c27ec8Sbaban 		goto out;
2668e8c27ec8Sbaban 
2669148c5f43SAlan Wright 	if (_idmapdstate.cfg->pgcfg.use_lsa &&
2670148c5f43SAlan Wright 	    _idmapdstate.cfg->pgcfg.domain_name != NULL) {
2671148c5f43SAlan Wright 		/*
2672148c5f43SAlan Wright 		 * If we don't have both name and SID, try looking up the
2673148c5f43SAlan Wright 		 * entry with LSA.
2674148c5f43SAlan Wright 		 */
2675148c5f43SAlan Wright 		if (req->id1.idmap_id_u.sid.prefix != NULL &&
2676148c5f43SAlan Wright 		    req->id1name == NULL) {
2677148c5f43SAlan Wright 
2678148c5f43SAlan Wright 			retcode = lookup_lsa_by_sid(
2679148c5f43SAlan Wright 			    req->id1.idmap_id_u.sid.prefix,
2680148c5f43SAlan Wright 			    req->id1.idmap_id_u.sid.rid,
2681148c5f43SAlan Wright 			    &req->id1name, &req->id1domain, &req->id1.idtype);
2682148c5f43SAlan Wright 			if (retcode == IDMAP_SUCCESS) {
2683148c5f43SAlan Wright 				TRACE(req, res, "Found with LSA");
2684148c5f43SAlan Wright 			} else if (retcode == IDMAP_ERR_NOTFOUND) {
2685148c5f43SAlan Wright 				TRACE(req, res, "Not found with LSA");
2686148c5f43SAlan Wright 			} else {
2687148c5f43SAlan Wright 				TRACE(req, res, "LSA error %d", retcode);
2688148c5f43SAlan Wright 				goto out;
2689148c5f43SAlan Wright 			}
2690148c5f43SAlan Wright 
2691148c5f43SAlan Wright 		} else  if (req->id1name != NULL &&
2692148c5f43SAlan Wright 		    req->id1.idmap_id_u.sid.prefix == NULL) {
2693148c5f43SAlan Wright 			char *canonname;
2694148c5f43SAlan Wright 			char *canondomain;
2695148c5f43SAlan Wright 
2696148c5f43SAlan Wright 			retcode = lookup_lsa_by_name(
2697148c5f43SAlan Wright 			    req->id1name, req->id1domain,
2698148c5f43SAlan Wright 			    &req->id1.idmap_id_u.sid.prefix,
2699148c5f43SAlan Wright 			    &req->id1.idmap_id_u.sid.rid,
2700148c5f43SAlan Wright 			    &canonname, &canondomain,
2701148c5f43SAlan Wright 			    &req->id1.idtype);
2702148c5f43SAlan Wright 			if (retcode == IDMAP_SUCCESS) {
2703148c5f43SAlan Wright 				free(req->id1name);
2704148c5f43SAlan Wright 				req->id1name = canonname;
2705148c5f43SAlan Wright 				free(req->id1domain);
2706148c5f43SAlan Wright 				req->id1domain = canondomain;
2707148c5f43SAlan Wright 				TRACE(req, res, "Found with LSA");
2708148c5f43SAlan Wright 			} else if (retcode == IDMAP_ERR_NOTFOUND) {
2709148c5f43SAlan Wright 				TRACE(req, res, "Not found with LSA");
2710148c5f43SAlan Wright 			} else {
2711148c5f43SAlan Wright 				TRACE(req, res, "LSA error %d", retcode);
2712148c5f43SAlan Wright 				goto out;
2713148c5f43SAlan Wright 			}
2714148c5f43SAlan Wright 		}
2715148c5f43SAlan Wright 	}
2716148c5f43SAlan Wright 
2717e8c27ec8Sbaban 	/*
2718e8c27ec8Sbaban 	 * Set the flag to indicate that we are not done yet so that
2719e8c27ec8Sbaban 	 * subsequent passes considers this request for name-based
2720e8c27ec8Sbaban 	 * mapping and ephemeral mapping.
2721c5c4113dSnw141292 	 */
2722c5c4113dSnw141292 	state->sid2pid_done = FALSE;
2723e8c27ec8Sbaban 	req->direction |= _IDMAP_F_NOTDONE;
2724c5c4113dSnw141292 
2725c5c4113dSnw141292 	/*
2726e8c27ec8Sbaban 	 * Even if we have both sid and winname, we still may need to batch
2727e8c27ec8Sbaban 	 * this request for AD lookup if we don't have unixname and
2728e8c27ec8Sbaban 	 * directory-based name mapping (AD or mixed) is enabled.
2729e8c27ec8Sbaban 	 * We avoid AD lookup for well-known SIDs because they don't have
2730e8c27ec8Sbaban 	 * regular AD objects.
2731c5c4113dSnw141292 	 */
2732e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS ||
2733e8c27ec8Sbaban 	    (!wksid && req->id2name == NULL &&
2734e3f2c991SKeyur Desai 	    AD_OR_MIXED_MODE(res->id.idtype, state)) ||
27359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    (!wksid && res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID &&
2736e3f2c991SKeyur Desai 	    state->directory_based_mapping == DIRECTORY_MAPPING_IDMU)) {
2737c5c4113dSnw141292 		retcode = IDMAP_SUCCESS;
2738e8c27ec8Sbaban 		req->direction |= _IDMAP_F_LOOKUP_AD;
2739c5c4113dSnw141292 		state->ad_nqueries++;
2740479ac375Sdm199847 	} else if (NLDAP_MODE(res->id.idtype, state)) {
2741479ac375Sdm199847 		req->direction |= _IDMAP_F_LOOKUP_NLDAP;
2742479ac375Sdm199847 		state->nldap_nqueries++;
2743c5c4113dSnw141292 	}
2744c5c4113dSnw141292 
2745c5c4113dSnw141292 
2746c5c4113dSnw141292 out:
2747c5c4113dSnw141292 	res->retcode = idmap_stat4prot(retcode);
2748e8c27ec8Sbaban 	/*
2749e8c27ec8Sbaban 	 * If we are done and there was an error then set fallback pid
2750e8c27ec8Sbaban 	 * in the result.
2751e8c27ec8Sbaban 	 */
2752e8c27ec8Sbaban 	if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS)
2753e8c27ec8Sbaban 		res->id.idmap_id_u.uid = UID_NOBODY;
2754c5c4113dSnw141292 	return (retcode);
2755c5c4113dSnw141292 }
2756c5c4113dSnw141292 
2757c5c4113dSnw141292 /*
2758c5c4113dSnw141292  * Generate SID using the following convention
2759c5c4113dSnw141292  * 	<machine-sid-prefix>-<1000 + uid>
2760c5c4113dSnw141292  * 	<machine-sid-prefix>-<2^31 + gid>
2761c5c4113dSnw141292  */
2762cd37da74Snw141292 static
2763cd37da74Snw141292 idmap_retcode
generate_localsid(idmap_mapping * req,idmap_id_res * res,int is_user,int fallback)276448258c6bSjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user,
276548258c6bSjp151216 		int fallback)
2766cd37da74Snw141292 {
2767e8c27ec8Sbaban 	free(res->id.idmap_id_u.sid.prefix);
2768e8c27ec8Sbaban 	res->id.idmap_id_u.sid.prefix = NULL;
2769e8c27ec8Sbaban 
2770e8c27ec8Sbaban 	/*
2771e8c27ec8Sbaban 	 * Diagonal mapping for localSIDs not supported because of the
2772e8c27ec8Sbaban 	 * way we generate localSIDs.
2773e8c27ec8Sbaban 	 */
2774e8c27ec8Sbaban 	if (is_user && res->id.idtype == IDMAP_GSID)
2775a0aa776eSAlan Wright 		return (IDMAP_ERR_NOTGROUP);
2776e8c27ec8Sbaban 	if (!is_user && res->id.idtype == IDMAP_USID)
2777a0aa776eSAlan Wright 		return (IDMAP_ERR_NOTUSER);
2778e8c27ec8Sbaban 
2779c5c4113dSnw141292 	/* Skip 1000 UIDs */
27801fcced4cSJordan Brown 	if (is_user &&
27811fcced4cSJordan Brown 	    req->id1.idmap_id_u.uid + LOCALRID_UID_MIN > LOCALRID_UID_MAX)
278262c60062Sbaban 		return (IDMAP_ERR_NOMAPPING);
2783c5c4113dSnw141292 
2784c5c4113dSnw141292 	RDLOCK_CONFIG();
2785e8c27ec8Sbaban 	/*
2786e8c27ec8Sbaban 	 * machine_sid is never NULL because if it is we won't be here.
2787148c5f43SAlan Wright 	 * No need to assert because strdup(NULL) will core anyways.
2788e8c27ec8Sbaban 	 */
2789c5c4113dSnw141292 	res->id.idmap_id_u.sid.prefix =
2790c5c4113dSnw141292 	    strdup(_idmapdstate.cfg->pgcfg.machine_sid);
2791c5c4113dSnw141292 	if (res->id.idmap_id_u.sid.prefix == NULL) {
2792c5c4113dSnw141292 		UNLOCK_CONFIG();
2793c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
2794c5c4113dSnw141292 		return (IDMAP_ERR_MEMORY);
2795c5c4113dSnw141292 	}
2796c5c4113dSnw141292 	UNLOCK_CONFIG();
2797c5c4113dSnw141292 	res->id.idmap_id_u.sid.rid =
27981fcced4cSJordan Brown 	    (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_UID_MIN :
27991fcced4cSJordan Brown 	    req->id1.idmap_id_u.gid + LOCALRID_GID_MIN;
2800651c0131Sbaban 	res->direction = IDMAP_DIRECTION_BI;
2801e8c27ec8Sbaban 	if (res->id.idtype == IDMAP_SID)
2802e8c27ec8Sbaban 		res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID;
2803c5c4113dSnw141292 
2804fc724630SAlan Wright 	if (!fallback) {
280548258c6bSjp151216 		res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID;
280648258c6bSjp151216 		res->info.src = IDMAP_MAP_SRC_ALGORITHMIC;
280748258c6bSjp151216 	}
280848258c6bSjp151216 
2809c5c4113dSnw141292 	/*
2810c5c4113dSnw141292 	 * Don't update name_cache because local sids don't have
2811c5c4113dSnw141292 	 * valid windows names.
2812c5c4113dSnw141292 	 */
2813e8c27ec8Sbaban 	req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
2814947c7bc0Sbaban 	return (IDMAP_SUCCESS);
2815c5c4113dSnw141292 }
2816c5c4113dSnw141292 
2817cd37da74Snw141292 static
2818cd37da74Snw141292 idmap_retcode
lookup_localsid2pid(idmap_mapping * req,idmap_id_res * res)2819cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res)
2820cd37da74Snw141292 {
2821c5c4113dSnw141292 	char		*sidprefix;
2822c5c4113dSnw141292 	uint32_t	rid;
2823c5c4113dSnw141292 	int		s;
2824c5c4113dSnw141292 
2825c5c4113dSnw141292 	/*
2826c5c4113dSnw141292 	 * If the sidprefix == localsid then UID = last RID - 1000 or
2827c5c4113dSnw141292 	 * GID = last RID - 2^31.
2828c5c4113dSnw141292 	 */
2829e8c27ec8Sbaban 	if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL)
2830e8c27ec8Sbaban 		/* This means we are looking up by winname */
2831e8c27ec8Sbaban 		return (IDMAP_ERR_NOTFOUND);
2832c5c4113dSnw141292 	rid = req->id1.idmap_id_u.sid.rid;
2833c5c4113dSnw141292 
2834c5c4113dSnw141292 	RDLOCK_CONFIG();
2835c5c4113dSnw141292 	s = (_idmapdstate.cfg->pgcfg.machine_sid) ?
2836cd37da74Snw141292 	    strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1;
2837c5c4113dSnw141292 	UNLOCK_CONFIG();
2838c5c4113dSnw141292 
2839e8c27ec8Sbaban 	/*
2840e8c27ec8Sbaban 	 * If the given sidprefix does not match machine_sid then this is
2841e8c27ec8Sbaban 	 * not a local SID.
2842e8c27ec8Sbaban 	 */
2843e8c27ec8Sbaban 	if (s != 0)
2844e8c27ec8Sbaban 		return (IDMAP_ERR_NOTFOUND);
2845e8c27ec8Sbaban 
2846e8c27ec8Sbaban 	switch (res->id.idtype) {
2847c5c4113dSnw141292 	case IDMAP_UID:
28481fcced4cSJordan Brown 		if (rid < LOCALRID_UID_MIN || rid > LOCALRID_UID_MAX)
2849e8c27ec8Sbaban 			return (IDMAP_ERR_ARG);
28501fcced4cSJordan Brown 		res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN;
2851c5c4113dSnw141292 		break;
2852c5c4113dSnw141292 	case IDMAP_GID:
28531fcced4cSJordan Brown 		if (rid < LOCALRID_GID_MIN)
2854e8c27ec8Sbaban 			return (IDMAP_ERR_ARG);
28551fcced4cSJordan Brown 		res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN;
2856c5c4113dSnw141292 		break;
2857c5c4113dSnw141292 	case IDMAP_POSIXID:
28581fcced4cSJordan Brown 		if (rid >= LOCALRID_GID_MIN) {
28591fcced4cSJordan Brown 			res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN;
2860c5c4113dSnw141292 			res->id.idtype = IDMAP_GID;
28611fcced4cSJordan Brown 		} else if (rid >= LOCALRID_UID_MIN) {
28621fcced4cSJordan Brown 			res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN;
2863c5c4113dSnw141292 			res->id.idtype = IDMAP_UID;
28641fcced4cSJordan Brown 		} else {
28651fcced4cSJordan Brown 			return (IDMAP_ERR_ARG);
2866c5c4113dSnw141292 		}
2867c5c4113dSnw141292 		break;
2868c5c4113dSnw141292 	default:
2869c5c4113dSnw141292 		return (IDMAP_ERR_NOTSUPPORTED);
2870c5c4113dSnw141292 	}
287148258c6bSjp151216 	res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID;
287248258c6bSjp151216 	res->info.src = IDMAP_MAP_SRC_ALGORITHMIC;
2873c5c4113dSnw141292 	return (IDMAP_SUCCESS);
2874c5c4113dSnw141292 }
2875c5c4113dSnw141292 
2876e8c27ec8Sbaban /*
2877e8c27ec8Sbaban  * Name service lookup by unixname to get pid
2878e8c27ec8Sbaban  */
2879cd37da74Snw141292 static
2880cd37da74Snw141292 idmap_retcode
ns_lookup_byname(const char * name,const char * lower_name,idmap_id * id)2881e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id)
2882cd37da74Snw141292 {
2883cd37da74Snw141292 	struct passwd	pwd, *pwdp;
2884cd37da74Snw141292 	struct group	grp, *grpp;
28858c155366SJordan Brown 	char		*buf;
28868c155366SJordan Brown 	static size_t	pwdbufsiz = 0;
28878c155366SJordan Brown 	static size_t	grpbufsiz = 0;
2888c5c4113dSnw141292 
2889e8c27ec8Sbaban 	switch (id->idtype) {
2890e8c27ec8Sbaban 	case IDMAP_UID:
28918c155366SJordan Brown 		if (pwdbufsiz == 0)
28928c155366SJordan Brown 			pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
28938c155366SJordan Brown 		buf = alloca(pwdbufsiz);
28948c155366SJordan Brown 		pwdp = getpwnam_r(name, &pwd, buf, pwdbufsiz);
2895e8c27ec8Sbaban 		if (pwdp == NULL && errno == 0 && lower_name != NULL &&
2896cd37da74Snw141292 		    name != lower_name && strcmp(name, lower_name) != 0)
28978c155366SJordan Brown 			pwdp = getpwnam_r(lower_name, &pwd, buf, pwdbufsiz);
2898cd37da74Snw141292 		if (pwdp == NULL) {
2899bbf6f00cSJordan Brown 			if (errno == 0)
2900c5c4113dSnw141292 				return (IDMAP_ERR_NOTFOUND);
2901c5c4113dSnw141292 			else
2902c5c4113dSnw141292 				return (IDMAP_ERR_INTERNAL);
2903c5c4113dSnw141292 		}
2904e8c27ec8Sbaban 		id->idmap_id_u.uid = pwd.pw_uid;
2905e8c27ec8Sbaban 		break;
2906e8c27ec8Sbaban 	case IDMAP_GID:
29078c155366SJordan Brown 		if (grpbufsiz == 0)
29088c155366SJordan Brown 			grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX);
29098c155366SJordan Brown 		buf = alloca(grpbufsiz);
29108c155366SJordan Brown 		grpp = getgrnam_r(name, &grp, buf, grpbufsiz);
2911e8c27ec8Sbaban 		if (grpp == NULL && errno == 0 && lower_name != NULL &&
2912cd37da74Snw141292 		    name != lower_name && strcmp(name, lower_name) != 0)
29138c155366SJordan Brown 			grpp = getgrnam_r(lower_name, &grp, buf, grpbufsiz);
2914cd37da74Snw141292 		if (grpp == NULL) {
2915bbf6f00cSJordan Brown 			if (errno == 0)
2916c5c4113dSnw141292 				return (IDMAP_ERR_NOTFOUND);
2917c5c4113dSnw141292 			else
2918c5c4113dSnw141292 				return (IDMAP_ERR_INTERNAL);
2919c5c4113dSnw141292 		}
2920e8c27ec8Sbaban 		id->idmap_id_u.gid = grp.gr_gid;
2921e8c27ec8Sbaban 		break;
2922e8c27ec8Sbaban 	default:
2923e8c27ec8Sbaban 		return (IDMAP_ERR_ARG);
2924c5c4113dSnw141292 	}
2925c5c4113dSnw141292 	return (IDMAP_SUCCESS);
2926c5c4113dSnw141292 }
2927c5c4113dSnw141292 
2928e8c27ec8Sbaban 
2929e8c27ec8Sbaban /*
2930e8c27ec8Sbaban  * Name service lookup by pid to get unixname
2931e8c27ec8Sbaban  */
2932e8c27ec8Sbaban static
2933e8c27ec8Sbaban idmap_retcode
ns_lookup_bypid(uid_t pid,int is_user,char ** unixname)2934e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname)
2935e8c27ec8Sbaban {
2936e8c27ec8Sbaban 	struct passwd	pwd;
2937e8c27ec8Sbaban 	struct group	grp;
29388c155366SJordan Brown 	char		*buf;
29398c155366SJordan Brown 	static size_t	pwdbufsiz = 0;
29408c155366SJordan Brown 	static size_t	grpbufsiz = 0;
2941e8c27ec8Sbaban 
2942e8c27ec8Sbaban 	if (is_user) {
29438c155366SJordan Brown 		if (pwdbufsiz == 0)
29448c155366SJordan Brown 			pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
29458c155366SJordan Brown 		buf = alloca(pwdbufsiz);
2946e8c27ec8Sbaban 		errno = 0;
29478c155366SJordan Brown 		if (getpwuid_r(pid, &pwd, buf, pwdbufsiz) == NULL) {
2948bbf6f00cSJordan Brown 			if (errno == 0)
2949e8c27ec8Sbaban 				return (IDMAP_ERR_NOTFOUND);
2950e8c27ec8Sbaban 			else
2951e8c27ec8Sbaban 				return (IDMAP_ERR_INTERNAL);
2952e8c27ec8Sbaban 		}
2953e8c27ec8Sbaban 		*unixname = strdup(pwd.pw_name);
2954e8c27ec8Sbaban 	} else {
29558c155366SJordan Brown 		if (grpbufsiz == 0)
29568c155366SJordan Brown 			grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX);
29578c155366SJordan Brown 		buf = alloca(grpbufsiz);
2958e8c27ec8Sbaban 		errno = 0;
29598c155366SJordan Brown 		if (getgrgid_r(pid, &grp, buf, grpbufsiz) == NULL) {
2960bbf6f00cSJordan Brown 			if (errno == 0)
2961e8c27ec8Sbaban 				return (IDMAP_ERR_NOTFOUND);
2962e8c27ec8Sbaban 			else
2963e8c27ec8Sbaban 				return (IDMAP_ERR_INTERNAL);
2964e8c27ec8Sbaban 		}
2965e8c27ec8Sbaban 		*unixname = strdup(grp.gr_name);
2966e8c27ec8Sbaban 	}
2967e8c27ec8Sbaban 	if (*unixname == NULL)
2968e8c27ec8Sbaban 		return (IDMAP_ERR_MEMORY);
2969e8c27ec8Sbaban 	return (IDMAP_SUCCESS);
2970e8c27ec8Sbaban }
2971e8c27ec8Sbaban 
2972c5c4113dSnw141292 /*
2973c5c4113dSnw141292  * Name-based mapping
2974c5c4113dSnw141292  *
2975c5c4113dSnw141292  * Case 1: If no rule matches do ephemeral
2976c5c4113dSnw141292  *
2977c5c4113dSnw141292  * Case 2: If rule matches and unixname is "" then return no mapping.
2978c5c4113dSnw141292  *
2979c5c4113dSnw141292  * Case 3: If rule matches and unixname is specified then lookup name
2980c5c4113dSnw141292  *  service using the unixname. If unixname not found then return no mapping.
2981c5c4113dSnw141292  *
2982c5c4113dSnw141292  * Case 4: If rule matches and unixname is * then lookup name service
2983c5c4113dSnw141292  *  using winname as the unixname. If unixname not found then process
2984c5c4113dSnw141292  *  other rules using the lookup order. If no other rule matches then do
2985c5c4113dSnw141292  *  ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4.
2986c5c4113dSnw141292  *  This allows us to specify a fallback unixname per _domain_ or no mapping
2987c5c4113dSnw141292  *  instead of the default behaviour of doing ephemeral mapping.
2988c5c4113dSnw141292  *
2989c5c4113dSnw141292  * Example 1:
2990c5c4113dSnw141292  * *@sfbay == *
2991c5c4113dSnw141292  * If looking up windows users foo@sfbay and foo does not exists in
2992c5c4113dSnw141292  * the name service then foo@sfbay will be mapped to an ephemeral id.
2993c5c4113dSnw141292  *
2994c5c4113dSnw141292  * Example 2:
2995c5c4113dSnw141292  * *@sfbay == *
2996c5c4113dSnw141292  * *@sfbay => guest
2997c5c4113dSnw141292  * If looking up windows users foo@sfbay and foo does not exists in
2998c5c4113dSnw141292  * the name service then foo@sfbay will be mapped to guest.
2999c5c4113dSnw141292  *
3000c5c4113dSnw141292  * Example 3:
3001c5c4113dSnw141292  * *@sfbay == *
3002c5c4113dSnw141292  * *@sfbay => ""
3003c5c4113dSnw141292  * If looking up windows users foo@sfbay and foo does not exists in
3004c5c4113dSnw141292  * the name service then we will return no mapping for foo@sfbay.
3005c5c4113dSnw141292  *
3006c5c4113dSnw141292  */
3007cd37da74Snw141292 static
3008cd37da74Snw141292 idmap_retcode
name_based_mapping_sid2pid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)3009479ac375Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state,
3010479ac375Sdm199847 		idmap_mapping *req, idmap_id_res *res)
3011cd37da74Snw141292 {
3012cd37da74Snw141292 	const char	*unixname, *windomain;
3013cd37da74Snw141292 	char		*sql = NULL, *errmsg = NULL, *lower_winname = NULL;
3014c5c4113dSnw141292 	idmap_retcode	retcode;
3015cd37da74Snw141292 	char		*end, *lower_unixname, *winname;
3016c5c4113dSnw141292 	const char	**values;
3017c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
301808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	int		ncol, r, is_user, is_wuser;
301948258c6bSjp151216 	idmap_namerule	*rule = &res->info.how.idmap_how_u.rule;
302048258c6bSjp151216 	int		direction;
3021c5c4113dSnw141292 	const char	*me = "name_based_mapping_sid2pid";
3022c5c4113dSnw141292 
3023e8c27ec8Sbaban 	assert(req->id1name != NULL); /* We have winname */
3024e8c27ec8Sbaban 	assert(req->id2name == NULL); /* We don't have unixname */
3025e8c27ec8Sbaban 
30268e228215Sdm199847 	winname = req->id1name;
30278e228215Sdm199847 	windomain = req->id1domain;
3028cd37da74Snw141292 
3029cd37da74Snw141292 	switch (req->id1.idtype) {
3030cd37da74Snw141292 	case IDMAP_USID:
3031cd37da74Snw141292 		is_wuser = 1;
3032cd37da74Snw141292 		break;
3033cd37da74Snw141292 	case IDMAP_GSID:
3034cd37da74Snw141292 		is_wuser = 0;
3035cd37da74Snw141292 		break;
3036cd37da74Snw141292 	default:
3037e8c27ec8Sbaban 		idmapdlog(LOG_ERR, "%s: Unable to determine if the "
3038e8c27ec8Sbaban 		    "given Windows id is user or group.", me);
3039cd37da74Snw141292 		return (IDMAP_ERR_INTERNAL);
3040cd37da74Snw141292 	}
3041cd37da74Snw141292 
3042e8c27ec8Sbaban 	switch (res->id.idtype) {
3043cd37da74Snw141292 	case IDMAP_UID:
3044cd37da74Snw141292 		is_user = 1;
3045cd37da74Snw141292 		break;
3046cd37da74Snw141292 	case IDMAP_GID:
3047cd37da74Snw141292 		is_user = 0;
3048cd37da74Snw141292 		break;
3049cd37da74Snw141292 	case IDMAP_POSIXID:
3050cd37da74Snw141292 		is_user = is_wuser;
3051cd37da74Snw141292 		res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID;
3052cd37da74Snw141292 		break;
3053cd37da74Snw141292 	}
3054c5c4113dSnw141292 
3055479ac375Sdm199847 	if (windomain == NULL)
305662c60062Sbaban 		windomain = "";
3057c5c4113dSnw141292 
3058cd37da74Snw141292 	if ((lower_winname = tolower_u8(winname)) == NULL)
3059cd37da74Snw141292 		lower_winname = winname;    /* hope for the best */
3060c5c4113dSnw141292 	sql = sqlite_mprintf(
306148258c6bSjp151216 	    "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 "
306248258c6bSjp151216 	    "FROM namerules WHERE "
3063cd37da74Snw141292 	    "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND "
3064c5c4113dSnw141292 	    "(winname = %Q OR winname = '*') AND "
306508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    "(windomain = %Q OR windomain = '*') "
3066c5c4113dSnw141292 	    "ORDER BY w2u_order ASC;",
306708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	    is_user, is_wuser, lower_winname, windomain);
3068c5c4113dSnw141292 	if (sql == NULL) {
3069c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3070c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
3071c5c4113dSnw141292 		goto out;
3072c5c4113dSnw141292 	}
3073c5c4113dSnw141292 
3074479ac375Sdm199847 	if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) {
3075c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3076cd37da74Snw141292 		idmapdlog(LOG_ERR, "%s: database error (%s)", me,
3077cd37da74Snw141292 		    CHECK_NULL(errmsg));
3078c5c4113dSnw141292 		sqlite_freemem(errmsg);
3079c5c4113dSnw141292 		goto out;
3080c5c4113dSnw141292 	}
3081c5c4113dSnw141292 
308284decf41Sjp151216 	for (;;) {
3083c5c4113dSnw141292 		r = sqlite_step(vm, &ncol, &values, NULL);
308484decf41Sjp151216 		assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
3085c5c4113dSnw141292 
308684decf41Sjp151216 		if (r == SQLITE_ROW) {
308748258c6bSjp151216 			if (ncol < 5) {
3088c5c4113dSnw141292 				retcode = IDMAP_ERR_INTERNAL;
3089c5c4113dSnw141292 				goto out;
3090c5c4113dSnw141292 			}
3091148c5f43SAlan Wright 
3092148c5f43SAlan Wright 			TRACE(req, res, "Matching rule: %s@%s -> %s",
3093148c5f43SAlan Wright 			    values[2] == NULL ? "(null)" : values[2],
3094148c5f43SAlan Wright 			    values[3] == NULL ? "(null)" : values[3],
3095148c5f43SAlan Wright 			    values[0] == NULL ? "(null)" : values[0]);
3096148c5f43SAlan Wright 
3097c5c4113dSnw141292 			if (values[0] == NULL) {
3098c5c4113dSnw141292 				retcode = IDMAP_ERR_INTERNAL;
3099c5c4113dSnw141292 				goto out;
3100c5c4113dSnw141292 			}
3101c5c4113dSnw141292 
310248258c6bSjp151216 			if (values[1] != NULL)
310348258c6bSjp151216 				direction =
310448258c6bSjp151216 				    (strtol(values[1], &end, 10) == 0)?
310548258c6bSjp151216 				    IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
310648258c6bSjp151216 			else
310748258c6bSjp151216 				direction = IDMAP_DIRECTION_W2U;
310848258c6bSjp151216 
3109c5c4113dSnw141292 			if (EMPTY_NAME(values[0])) {
3110148c5f43SAlan Wright 				TRACE(req, res, "Mapping inhibited");
311148258c6bSjp151216 				idmap_namerule_set(rule, values[3], values[2],
31129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 				    values[0], is_user, is_wuser,
311348258c6bSjp151216 				    strtol(values[4], &end, 10),
311448258c6bSjp151216 				    direction);
3115c5c4113dSnw141292 				retcode = IDMAP_ERR_NOMAPPING;
3116c5c4113dSnw141292 				goto out;
3117c5c4113dSnw141292 			}
311848258c6bSjp151216 
311948258c6bSjp151216 			if (values[0][0] == '*') {
312048258c6bSjp151216 				unixname = winname;
312148258c6bSjp151216 				lower_unixname = lower_winname;
312248258c6bSjp151216 			} else {
312348258c6bSjp151216 				unixname = values[0];
312448258c6bSjp151216 				lower_unixname = NULL;
312548258c6bSjp151216 			}
312648258c6bSjp151216 
3127e8c27ec8Sbaban 			retcode = ns_lookup_byname(unixname, lower_unixname,
3128e8c27ec8Sbaban 			    &res->id);
3129148c5f43SAlan Wright 			if (retcode == IDMAP_SUCCESS) {
3130148c5f43SAlan Wright 				break;
3131148c5f43SAlan Wright 			} else if (retcode == IDMAP_ERR_NOTFOUND) {
3132148c5f43SAlan Wright 				if (values[0][0] == '*') {
3133148c5f43SAlan Wright 					TRACE(req, res,
3134148c5f43SAlan Wright 					    "%s not found, continuing",
3135148c5f43SAlan Wright 					    unixname);
3136c5c4113dSnw141292 					/* Case 4 */
3137c5c4113dSnw141292 					continue;
3138148c5f43SAlan Wright 				} else {
3139148c5f43SAlan Wright 					TRACE(req, res,
3140148c5f43SAlan Wright 					    "%s not found, error", unixname);
3141c5c4113dSnw141292 					/* Case 3 */
314248258c6bSjp151216 					idmap_namerule_set(rule, values[3],
31439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 					    values[2], values[0], is_user,
31449fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 					    is_wuser,
314548258c6bSjp151216 					    strtol(values[4], &end, 10),
314648258c6bSjp151216 					    direction);
3147c5c4113dSnw141292 					retcode = IDMAP_ERR_NOMAPPING;
3148c5c4113dSnw141292 				}
3149148c5f43SAlan Wright 			} else {
3150148c5f43SAlan Wright 				TRACE(req, res, "Looking up %s error=%d",
3151148c5f43SAlan Wright 				    unixname, retcode);
315248258c6bSjp151216 			}
3153c5c4113dSnw141292 			goto out;
3154c5c4113dSnw141292 		} else if (r == SQLITE_DONE) {
3155cb174861Sjoyce mcintosh 			TRACE(req, res, "No matching rule");
3156c5c4113dSnw141292 			retcode = IDMAP_ERR_NOTFOUND;
3157c5c4113dSnw141292 			goto out;
3158c5c4113dSnw141292 		} else {
3159c5c4113dSnw141292 			(void) sqlite_finalize(vm, &errmsg);
3160c5c4113dSnw141292 			vm = NULL;
3161cd37da74Snw141292 			idmapdlog(LOG_ERR, "%s: database error (%s)", me,
3162cd37da74Snw141292 			    CHECK_NULL(errmsg));
3163c5c4113dSnw141292 			sqlite_freemem(errmsg);
3164c5c4113dSnw141292 			retcode = IDMAP_ERR_INTERNAL;
3165c5c4113dSnw141292 			goto out;
3166c5c4113dSnw141292 		}
3167c5c4113dSnw141292 	}
3168c5c4113dSnw141292 
3169148c5f43SAlan Wright 	/* Found */
3170148c5f43SAlan Wright 
317162c60062Sbaban 	if (values[1] != NULL)
3172c5c4113dSnw141292 		res->direction =
3173651c0131Sbaban 		    (strtol(values[1], &end, 10) == 0)?
3174651c0131Sbaban 		    IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
3175c5c4113dSnw141292 	else
3176651c0131Sbaban 		res->direction = IDMAP_DIRECTION_W2U;
317748258c6bSjp151216 
31788e228215Sdm199847 	req->id2name = strdup(unixname);
3179479ac375Sdm199847 	if (req->id2name == NULL) {
3180479ac375Sdm199847 		retcode = IDMAP_ERR_MEMORY;
3181148c5f43SAlan Wright 		goto out;
3182479ac375Sdm199847 	}
3183148c5f43SAlan Wright 	TRACE(req, res, "UNIX name found");
3184479ac375Sdm199847 
318548258c6bSjp151216 	idmap_namerule_set(rule, values[3], values[2],
31869fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    values[0], is_user, is_wuser, strtol(values[4], &end, 10),
318748258c6bSjp151216 	    res->direction);
3188148c5f43SAlan Wright 
3189148c5f43SAlan Wright out:
3190148c5f43SAlan Wright 	if (retcode != IDMAP_SUCCESS &&
3191148c5f43SAlan Wright 	    retcode != IDMAP_ERR_NOTFOUND &&
3192148c5f43SAlan Wright 	    retcode != IDMAP_ERR_NOMAPPING) {
3193148c5f43SAlan Wright 		TRACE(req, res, "Rule processing error, code=%d", retcode);
3194fc724630SAlan Wright 	}
3195fc724630SAlan Wright 
3196148c5f43SAlan Wright 	if (sql != NULL)
3197148c5f43SAlan Wright 		sqlite_freemem(sql);
3198148c5f43SAlan Wright 
3199fc724630SAlan Wright 	if (retcode != IDMAP_ERR_NOTFOUND) {
3200fc724630SAlan Wright 		res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED;
320148258c6bSjp151216 		res->info.src = IDMAP_MAP_SRC_NEW;
3202c5c4113dSnw141292 	}
3203479ac375Sdm199847 
3204cd37da74Snw141292 	if (lower_winname != NULL && lower_winname != winname)
3205cd37da74Snw141292 		free(lower_winname);
320662c60062Sbaban 	if (vm != NULL)
3207c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
3208c5c4113dSnw141292 	return (retcode);
3209c5c4113dSnw141292 }
3210c5c4113dSnw141292 
3211c5c4113dSnw141292 static
3212c5c4113dSnw141292 int
get_next_eph_uid(uid_t * next_uid)3213c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid)
3214c5c4113dSnw141292 {
3215c5c4113dSnw141292 	uid_t uid;
3216c5c4113dSnw141292 	gid_t gid;
3217c5c4113dSnw141292 	int err;
3218c5c4113dSnw141292 
3219c5c4113dSnw141292 	*next_uid = (uid_t)-1;
3220c5c4113dSnw141292 	uid = _idmapdstate.next_uid++;
3221c5c4113dSnw141292 	if (uid >= _idmapdstate.limit_uid) {
3222c5c4113dSnw141292 		if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0)
3223c5c4113dSnw141292 			return (err);
3224c5c4113dSnw141292 
3225c5c4113dSnw141292 		_idmapdstate.limit_uid = uid + 8192;
3226c5c4113dSnw141292 		_idmapdstate.next_uid = uid;
3227c5c4113dSnw141292 	}
3228c5c4113dSnw141292 	*next_uid = uid;
3229c5c4113dSnw141292 
3230c5c4113dSnw141292 	return (0);
3231c5c4113dSnw141292 }
3232c5c4113dSnw141292 
3233c5c4113dSnw141292 static
3234c5c4113dSnw141292 int
get_next_eph_gid(gid_t * next_gid)3235c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid)
3236c5c4113dSnw141292 {
3237c5c4113dSnw141292 	uid_t uid;
3238c5c4113dSnw141292 	gid_t gid;
3239c5c4113dSnw141292 	int err;
3240c5c4113dSnw141292 
3241c5c4113dSnw141292 	*next_gid = (uid_t)-1;
3242c5c4113dSnw141292 	gid = _idmapdstate.next_gid++;
3243c5c4113dSnw141292 	if (gid >= _idmapdstate.limit_gid) {
3244c5c4113dSnw141292 		if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0)
3245c5c4113dSnw141292 			return (err);
3246c5c4113dSnw141292 
3247c5c4113dSnw141292 		_idmapdstate.limit_gid = gid + 8192;
3248c5c4113dSnw141292 		_idmapdstate.next_gid = gid;
3249c5c4113dSnw141292 	}
3250c5c4113dSnw141292 	*next_gid = gid;
3251c5c4113dSnw141292 
3252c5c4113dSnw141292 	return (0);
3253c5c4113dSnw141292 }
3254c5c4113dSnw141292 
325562c60062Sbaban static
325662c60062Sbaban int
gethash(const char * str,uint32_t num,uint_t htsize)3257cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize)
3258cd37da74Snw141292 {
325962c60062Sbaban 	uint_t  hval, i, len;
326062c60062Sbaban 
326162c60062Sbaban 	if (str == NULL)
326262c60062Sbaban 		return (0);
326362c60062Sbaban 	for (len = strlen(str), hval = 0, i = 0; i < len; i++) {
326462c60062Sbaban 		hval += str[i];
326562c60062Sbaban 		hval += (hval << 10);
326662c60062Sbaban 		hval ^= (hval >> 6);
326762c60062Sbaban 	}
326862c60062Sbaban 	for (str = (const char *)&num, i = 0; i < sizeof (num); i++) {
326962c60062Sbaban 		hval += str[i];
327062c60062Sbaban 		hval += (hval << 10);
327162c60062Sbaban 		hval ^= (hval >> 6);
327262c60062Sbaban 	}
327362c60062Sbaban 	hval += (hval << 3);
327462c60062Sbaban 	hval ^= (hval >> 11);
327562c60062Sbaban 	hval += (hval << 15);
327662c60062Sbaban 	return (hval % htsize);
327762c60062Sbaban }
327862c60062Sbaban 
327962c60062Sbaban static
328062c60062Sbaban int
get_from_sid_history(lookup_state_t * state,const char * prefix,uint32_t rid,uid_t * pid)328162c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid,
3282cd37da74Snw141292 		uid_t *pid)
3283cd37da74Snw141292 {
328462c60062Sbaban 	uint_t		next, key;
328562c60062Sbaban 	uint_t		htsize = state->sid_history_size;
328662c60062Sbaban 	idmap_sid	*sid;
328762c60062Sbaban 
328862c60062Sbaban 	next = gethash(prefix, rid, htsize);
328962c60062Sbaban 	while (next != htsize) {
329062c60062Sbaban 		key = state->sid_history[next].key;
329162c60062Sbaban 		if (key == htsize)
329262c60062Sbaban 			return (0);
329362c60062Sbaban 		sid = &state->batch->idmap_mapping_batch_val[key].id1.
329462c60062Sbaban 		    idmap_id_u.sid;
329562c60062Sbaban 		if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) {
329662c60062Sbaban 			*pid = state->result->ids.ids_val[key].id.
329762c60062Sbaban 			    idmap_id_u.uid;
329862c60062Sbaban 			return (1);
329962c60062Sbaban 		}
330062c60062Sbaban 		next = state->sid_history[next].next;
330162c60062Sbaban 	}
330262c60062Sbaban 	return (0);
330362c60062Sbaban }
330462c60062Sbaban 
330562c60062Sbaban static
330662c60062Sbaban void
add_to_sid_history(lookup_state_t * state,const char * prefix,uint32_t rid)3307cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid)
3308cd37da74Snw141292 {
330962c60062Sbaban 	uint_t		hash, next;
331062c60062Sbaban 	uint_t		htsize = state->sid_history_size;
331162c60062Sbaban 
331262c60062Sbaban 	hash = next = gethash(prefix, rid, htsize);
331362c60062Sbaban 	while (state->sid_history[next].key != htsize) {
331462c60062Sbaban 		next++;
331562c60062Sbaban 		next %= htsize;
331662c60062Sbaban 	}
331762c60062Sbaban 	state->sid_history[next].key = state->curpos;
331862c60062Sbaban 	if (hash == next)
331962c60062Sbaban 		return;
332062c60062Sbaban 	state->sid_history[next].next = state->sid_history[hash].next;
332162c60062Sbaban 	state->sid_history[hash].next = next;
332262c60062Sbaban }
3323c5c4113dSnw141292 
3324e8c27ec8Sbaban void
cleanup_lookup_state(lookup_state_t * state)3325e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state)
3326e8c27ec8Sbaban {
3327e8c27ec8Sbaban 	free(state->sid_history);
3328e8c27ec8Sbaban 	free(state->ad_unixuser_attr);
3329e8c27ec8Sbaban 	free(state->ad_unixgroup_attr);
3330479ac375Sdm199847 	free(state->nldap_winname_attr);
3331479ac375Sdm199847 	free(state->defdom);
3332e8c27ec8Sbaban }
3333e8c27ec8Sbaban 
3334c5c4113dSnw141292 /* ARGSUSED */
3335c5c4113dSnw141292 static
3336c5c4113dSnw141292 idmap_retcode
dynamic_ephemeral_mapping(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)3337479ac375Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state,
3338cd37da74Snw141292 		idmap_mapping *req, idmap_id_res *res)
3339cd37da74Snw141292 {
3340c5c4113dSnw141292 
3341c5c4113dSnw141292 	uid_t		next_pid;
3342c5c4113dSnw141292 
3343651c0131Sbaban 	res->direction = IDMAP_DIRECTION_BI;
334462c60062Sbaban 
33459fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid)) {
334648258c6bSjp151216 		res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
334748258c6bSjp151216 		res->info.src = IDMAP_MAP_SRC_CACHE;
334862c60062Sbaban 		return (IDMAP_SUCCESS);
334948258c6bSjp151216 	}
335062c60062Sbaban 
335162c60062Sbaban 	if (state->sid_history != NULL &&
335262c60062Sbaban 	    get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix,
335362c60062Sbaban 	    req->id1.idmap_id_u.sid.rid, &next_pid)) {
335462c60062Sbaban 		res->id.idmap_id_u.uid = next_pid;
335548258c6bSjp151216 		res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
335648258c6bSjp151216 		res->info.src = IDMAP_MAP_SRC_NEW;
335762c60062Sbaban 		return (IDMAP_SUCCESS);
335862c60062Sbaban 	}
335962c60062Sbaban 
336062c60062Sbaban 	if (res->id.idtype == IDMAP_UID) {
3361c5c4113dSnw141292 		if (get_next_eph_uid(&next_pid) != 0)
3362c5c4113dSnw141292 			return (IDMAP_ERR_INTERNAL);
3363c5c4113dSnw141292 		res->id.idmap_id_u.uid = next_pid;
3364c5c4113dSnw141292 	} else {
3365c5c4113dSnw141292 		if (get_next_eph_gid(&next_pid) != 0)
3366c5c4113dSnw141292 			return (IDMAP_ERR_INTERNAL);
3367c5c4113dSnw141292 		res->id.idmap_id_u.gid = next_pid;
3368c5c4113dSnw141292 	}
3369c5c4113dSnw141292 
337048258c6bSjp151216 	res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
337148258c6bSjp151216 	res->info.src = IDMAP_MAP_SRC_NEW;
337262c60062Sbaban 	if (state->sid_history != NULL)
337362c60062Sbaban 		add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix,
337462c60062Sbaban 		    req->id1.idmap_id_u.sid.rid);
337562c60062Sbaban 
3376c5c4113dSnw141292 	return (IDMAP_SUCCESS);
3377c5c4113dSnw141292 }
3378c5c4113dSnw141292 
3379c5c4113dSnw141292 idmap_retcode
sid2pid_second_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)3380479ac375Sdm199847 sid2pid_second_pass(lookup_state_t *state,
3381cd37da74Snw141292 		idmap_mapping *req, idmap_id_res *res)
3382cd37da74Snw141292 {
3383c5c4113dSnw141292 	idmap_retcode	retcode;
3384148c5f43SAlan Wright 	idmap_retcode	retcode2;
3385c5c4113dSnw141292 
3386c5c4113dSnw141292 	/* Check if second pass is needed */
3387e8c27ec8Sbaban 	if (ARE_WE_DONE(req->direction))
3388c5c4113dSnw141292 		return (res->retcode);
3389c5c4113dSnw141292 
3390c5c4113dSnw141292 	/* Get status from previous pass */
3391e8c27ec8Sbaban 	retcode = res->retcode;
33924aa0a5e7Snw141292 	if (retcode != IDMAP_SUCCESS && state->eph_map_unres_sids &&
33934aa0a5e7Snw141292 	    !EMPTY_STRING(req->id1.idmap_id_u.sid.prefix) &&
33944aa0a5e7Snw141292 	    EMPTY_STRING(req->id1name)) {
33954aa0a5e7Snw141292 		/*
33964aa0a5e7Snw141292 		 * We are asked to map an unresolvable SID to a UID or
33974aa0a5e7Snw141292 		 * GID, but, which?  We'll treat all unresolvable SIDs
33984aa0a5e7Snw141292 		 * as users unless the caller specified which of a UID
33994aa0a5e7Snw141292 		 * or GID they want.
34004aa0a5e7Snw141292 		 */
3401a7c8bd9fSNicolas Williams 		if (req->id1.idtype == IDMAP_SID)
3402a7c8bd9fSNicolas Williams 			req->id1.idtype = IDMAP_USID;
3403148c5f43SAlan Wright 		if (res->id.idtype == IDMAP_POSIXID) {
34044aa0a5e7Snw141292 			res->id.idtype = IDMAP_UID;
3405148c5f43SAlan Wright 			TRACE(req, res, "Assume unresolvable SID is user");
3406148c5f43SAlan Wright 		} else if (res->id.idtype == IDMAP_UID) {
3407148c5f43SAlan Wright 			TRACE(req, res, "Must map unresolvable SID to user");
3408148c5f43SAlan Wright 		} else if (res->id.idtype == IDMAP_GID) {
3409148c5f43SAlan Wright 			TRACE(req, res, "Must map unresolvable SID to group");
3410148c5f43SAlan Wright 		}
34114aa0a5e7Snw141292 		goto do_eph;
34124aa0a5e7Snw141292 	}
3413e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS)
3414e8c27ec8Sbaban 		goto out;
3415c5c4113dSnw141292 
3416e8c27ec8Sbaban 	/*
3417e3f2c991SKeyur Desai 	 * There are two ways we might get here with a Posix ID:
3418e3f2c991SKeyur Desai 	 * - It could be from an expired ephemeral cache entry.
3419e3f2c991SKeyur Desai 	 * - It could be from IDMU.
3420e3f2c991SKeyur Desai 	 * If it's from IDMU, we need to look up the name, for name-based
3421e3f2c991SKeyur Desai 	 * requests and the cache.
3422e3f2c991SKeyur Desai 	 */
34239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (!IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid) &&
34249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    res->id.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
3425e3f2c991SKeyur Desai 		if (req->id2name == NULL) {
3426e3f2c991SKeyur Desai 			/*
3427e3f2c991SKeyur Desai 			 * If the lookup fails, go ahead anyway.
3428e3f2c991SKeyur Desai 			 * The general UNIX rule is that it's OK to
3429e3f2c991SKeyur Desai 			 * have a UID or GID that isn't in the
3430e3f2c991SKeyur Desai 			 * name service.
3431e3f2c991SKeyur Desai 			 */
3432148c5f43SAlan Wright 			retcode2 = ns_lookup_bypid(res->id.idmap_id_u.uid,
3433e3f2c991SKeyur Desai 			    res->id.idtype == IDMAP_UID, &req->id2name);
3434148c5f43SAlan Wright 			if (IDMAP_ERROR(retcode2)) {
3435148c5f43SAlan Wright 				TRACE(req, res,
3436148c5f43SAlan Wright 				    "Getting UNIX name, error=%d (ignored)",
3437148c5f43SAlan Wright 				    retcode2);
3438148c5f43SAlan Wright 			} else {
3439148c5f43SAlan Wright 				TRACE(req, res, "Found UNIX name");
3440148c5f43SAlan Wright 			}
3441e3f2c991SKeyur Desai 		}
3442e3f2c991SKeyur Desai 		goto out;
3443e3f2c991SKeyur Desai 	}
3444e3f2c991SKeyur Desai 
3445e3f2c991SKeyur Desai 	/*
3446e8c27ec8Sbaban 	 * If directory-based name mapping is enabled then the unixname
3447e8c27ec8Sbaban 	 * may already have been retrieved from the AD object (AD-mode or
3448e8c27ec8Sbaban 	 * mixed-mode) or from native LDAP object (nldap-mode) -- done.
3449e8c27ec8Sbaban 	 */
3450e8c27ec8Sbaban 	if (req->id2name != NULL) {
3451e8c27ec8Sbaban 		assert(res->id.idtype != IDMAP_POSIXID);
3452e8c27ec8Sbaban 		if (AD_MODE(res->id.idtype, state))
3453e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_BI;
3454e8c27ec8Sbaban 		else if (NLDAP_MODE(res->id.idtype, state))
3455e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_BI;
3456e8c27ec8Sbaban 		else if (MIXED_MODE(res->id.idtype, state))
3457e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_W2U;
3458c5c4113dSnw141292 
3459e8c27ec8Sbaban 		/*
3460e8c27ec8Sbaban 		 * Special case: (1) If the ad_unixuser_attr and
3461e8c27ec8Sbaban 		 * ad_unixgroup_attr uses the same attribute
3462e8c27ec8Sbaban 		 * name and (2) if this is a diagonal mapping
3463e8c27ec8Sbaban 		 * request and (3) the unixname has been retrieved
3464e8c27ec8Sbaban 		 * from the AD object -- then we ignore it and fallback
3465e8c27ec8Sbaban 		 * to name-based mapping rules and ephemeral mapping
3466e8c27ec8Sbaban 		 *
3467e8c27ec8Sbaban 		 * Example:
3468e8c27ec8Sbaban 		 *  Properties:
3469e8c27ec8Sbaban 		 *    config/ad_unixuser_attr = "unixname"
3470e8c27ec8Sbaban 		 *    config/ad_unixgroup_attr = "unixname"
3471e8c27ec8Sbaban 		 *  AD user object:
3472e8c27ec8Sbaban 		 *    dn: cn=bob ...
3473e8c27ec8Sbaban 		 *    objectclass: user
3474e8c27ec8Sbaban 		 *    sam: bob
3475e8c27ec8Sbaban 		 *    unixname: bob1234
3476e8c27ec8Sbaban 		 *  AD group object:
3477e8c27ec8Sbaban 		 *    dn: cn=winadmins ...
3478e8c27ec8Sbaban 		 *    objectclass: group
3479e8c27ec8Sbaban 		 *    sam: winadmins
3480e8c27ec8Sbaban 		 *    unixname: unixadmins
3481e8c27ec8Sbaban 		 *
3482e8c27ec8Sbaban 		 *  In this example whether "unixname" refers to a unixuser
3483e8c27ec8Sbaban 		 *  or unixgroup depends upon the AD object.
3484e8c27ec8Sbaban 		 *
3485e8c27ec8Sbaban 		 * $idmap show -c winname:bob gid
3486e8c27ec8Sbaban 		 *    AD lookup by "samAccountName=bob" for
3487e8c27ec8Sbaban 		 *    "ad_unixgroup_attr (i.e unixname)" for directory-based
3488e8c27ec8Sbaban 		 *    mapping would get "bob1234" which is not what we want.
3489e8c27ec8Sbaban 		 *    Now why not getgrnam_r("bob1234") and use it if it
3490e8c27ec8Sbaban 		 *    is indeed a unixgroup? That's because Unix can have
3491e8c27ec8Sbaban 		 *    users and groups with the same name and we clearly
3492e8c27ec8Sbaban 		 *    don't know the intention of the admin here.
3493e8c27ec8Sbaban 		 *    Therefore we ignore this and fallback to name-based
3494e8c27ec8Sbaban 		 *    mapping rules or ephemeral mapping.
3495e8c27ec8Sbaban 		 */
3496e8c27ec8Sbaban 		if ((AD_MODE(res->id.idtype, state) ||
3497e8c27ec8Sbaban 		    MIXED_MODE(res->id.idtype, state)) &&
3498e8c27ec8Sbaban 		    state->ad_unixuser_attr != NULL &&
3499e8c27ec8Sbaban 		    state->ad_unixgroup_attr != NULL &&
3500e8c27ec8Sbaban 		    strcasecmp(state->ad_unixuser_attr,
3501e8c27ec8Sbaban 		    state->ad_unixgroup_attr) == 0 &&
3502e8c27ec8Sbaban 		    ((req->id1.idtype == IDMAP_USID &&
3503e8c27ec8Sbaban 		    res->id.idtype == IDMAP_GID) ||
3504e8c27ec8Sbaban 		    (req->id1.idtype == IDMAP_GSID &&
3505e8c27ec8Sbaban 		    res->id.idtype == IDMAP_UID))) {
3506148c5f43SAlan Wright 			TRACE(req, res, "Ignoring UNIX name found in AD");
3507e8c27ec8Sbaban 			free(req->id2name);
3508e8c27ec8Sbaban 			req->id2name = NULL;
35099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			res->id.idmap_id_u.uid = IDMAP_SENTINEL_PID;
3510e8c27ec8Sbaban 			/* fallback */
3511e8c27ec8Sbaban 		} else {
3512148c5f43SAlan Wright 			if (res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
3513e8c27ec8Sbaban 				retcode = ns_lookup_byname(req->id2name,
3514e8c27ec8Sbaban 				    NULL, &res->id);
3515148c5f43SAlan Wright 				if (retcode != IDMAP_SUCCESS) {
3516e8c27ec8Sbaban 					/*
3517148c5f43SAlan Wright 					 * If ns_lookup_byname() fails that
3518148c5f43SAlan Wright 					 * means the unixname (req->id2name),
3519148c5f43SAlan Wright 					 * which was obtained from the AD
3520148c5f43SAlan Wright 					 * object by directory-based mapping,
3521148c5f43SAlan Wright 					 * is not a valid Unix user/group and
3522148c5f43SAlan Wright 					 * therefore we return the error to the
3523148c5f43SAlan Wright 					 * client instead of doing rule-based
3524148c5f43SAlan Wright 					 * mapping or ephemeral mapping. This
3525148c5f43SAlan Wright 					 * way the client can detect the issue.
3526e8c27ec8Sbaban 					 */
3527148c5f43SAlan Wright 					TRACE(req, res,
3528148c5f43SAlan Wright 					    "UNIX lookup error=%d", retcode);
3529148c5f43SAlan Wright 					goto out;
3530148c5f43SAlan Wright 				}
3531148c5f43SAlan Wright 				TRACE(req, res, "UNIX lookup");
3532148c5f43SAlan Wright 			}
3533c5c4113dSnw141292 			goto out;
3534c5c4113dSnw141292 		}
3535e8c27ec8Sbaban 	}
3536c5c4113dSnw141292 
353748258c6bSjp151216 	/* Free any mapping info from Directory based mapping */
353848258c6bSjp151216 	if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN)
3539148c5f43SAlan Wright 		idmap_how_clear(&res->info.how);
354048258c6bSjp151216 
3541e8c27ec8Sbaban 	/*
3542e8c27ec8Sbaban 	 * If we don't have unixname then evaluate local name-based
3543e8c27ec8Sbaban 	 * mapping rules.
3544e8c27ec8Sbaban 	 */
3545479ac375Sdm199847 	retcode = name_based_mapping_sid2pid(state, req, res);
3546148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
3547148c5f43SAlan Wright 		TRACE(req, res, "Rule-based mapping");
3548e8c27ec8Sbaban 		goto out;
3549148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
3550148c5f43SAlan Wright 		TRACE(req, res, "Rule-based mapping error=%d", retcode);
3551148c5f43SAlan Wright 		goto out;
3552148c5f43SAlan Wright 	}
3553e8c27ec8Sbaban 
35544aa0a5e7Snw141292 do_eph:
3555c5c4113dSnw141292 	/* If not found, do ephemeral mapping */
3556479ac375Sdm199847 	retcode = dynamic_ephemeral_mapping(state, req, res);
3557148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
3558148c5f43SAlan Wright 		TRACE(req, res, "Ephemeral mapping");
3559148c5f43SAlan Wright 		goto out;
3560148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
3561148c5f43SAlan Wright 		TRACE(req, res, "Ephemeral mapping error=%d", retcode);
3562148c5f43SAlan Wright 		goto out;
3563148c5f43SAlan Wright 	}
3564c5c4113dSnw141292 
3565c5c4113dSnw141292 out:
3566c5c4113dSnw141292 	res->retcode = idmap_stat4prot(retcode);
3567e8c27ec8Sbaban 	if (res->retcode != IDMAP_SUCCESS) {
3568e8c27ec8Sbaban 		req->direction = _IDMAP_F_DONE;
3569e8c27ec8Sbaban 		res->id.idmap_id_u.uid = UID_NOBODY;
3570e8c27ec8Sbaban 	}
3571e8c27ec8Sbaban 	if (!ARE_WE_DONE(req->direction))
3572e8c27ec8Sbaban 		state->sid2pid_done = FALSE;
3573c5c4113dSnw141292 	return (retcode);
3574c5c4113dSnw141292 }
3575c5c4113dSnw141292 
3576c5c4113dSnw141292 idmap_retcode
update_cache_pid2sid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)3577479ac375Sdm199847 update_cache_pid2sid(lookup_state_t *state,
3578cd37da74Snw141292 		idmap_mapping *req, idmap_id_res *res)
3579cd37da74Snw141292 {
3580c5c4113dSnw141292 	char		*sql = NULL;
3581c5c4113dSnw141292 	idmap_retcode	retcode;
3582148c5f43SAlan Wright 	idmap_retcode	retcode2;
358348258c6bSjp151216 	char		*map_dn = NULL;
358448258c6bSjp151216 	char		*map_attr = NULL;
358548258c6bSjp151216 	char		*map_value = NULL;
358648258c6bSjp151216 	char 		*map_windomain = NULL;
358748258c6bSjp151216 	char		*map_winname = NULL;
358848258c6bSjp151216 	char		*map_unixname = NULL;
358948258c6bSjp151216 	int		map_is_nt4 = FALSE;
3590c5c4113dSnw141292 
3591c5c4113dSnw141292 	/* Check if we need to cache anything */
3592e8c27ec8Sbaban 	if (ARE_WE_DONE(req->direction))
3593c5c4113dSnw141292 		return (IDMAP_SUCCESS);
3594c5c4113dSnw141292 
3595c5c4113dSnw141292 	/* We don't cache negative entries */
3596c5c4113dSnw141292 	if (res->retcode != IDMAP_SUCCESS)
3597c5c4113dSnw141292 		return (IDMAP_SUCCESS);
3598c5c4113dSnw141292 
3599e8c27ec8Sbaban 	assert(res->direction != IDMAP_DIRECTION_UNDEF);
36009fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	assert(req->id1.idmap_id_u.uid != IDMAP_SENTINEL_PID);
360148258c6bSjp151216 	assert(res->id.idtype != IDMAP_SID);
360248258c6bSjp151216 
3603e3f2c991SKeyur Desai 	/*
3604e3f2c991SKeyur Desai 	 * If we've gotten to this point and we *still* don't know the
3605e3f2c991SKeyur Desai 	 * unixname, well, we'd like to have it now for the cache.
3606e3f2c991SKeyur Desai 	 *
3607e3f2c991SKeyur Desai 	 * If we truly always need it for the cache, we should probably
3608e3f2c991SKeyur Desai 	 * look it up once at the beginning, rather than "at need" in
3609e3f2c991SKeyur Desai 	 * several places as is now done.  However, it's not really clear
3610e3f2c991SKeyur Desai 	 * that we *do* need it in the cache; there's a decent argument
3611e3f2c991SKeyur Desai 	 * that the cache should contain only SIDs and PIDs, so we'll
3612e3f2c991SKeyur Desai 	 * leave our options open by doing it "at need" here too.
3613e3f2c991SKeyur Desai 	 *
3614e3f2c991SKeyur Desai 	 * If we can't find it... c'est la vie.
3615e3f2c991SKeyur Desai 	 */
3616e3f2c991SKeyur Desai 	if (req->id1name == NULL) {
3617148c5f43SAlan Wright 		retcode2 = ns_lookup_bypid(req->id1.idmap_id_u.uid,
3618e3f2c991SKeyur Desai 		    req->id1.idtype == IDMAP_UID, &req->id1name);
3619148c5f43SAlan Wright 		if (retcode2 == IDMAP_SUCCESS)
3620148c5f43SAlan Wright 			TRACE(req, res, "Found UNIX name");
3621148c5f43SAlan Wright 		else
3622148c5f43SAlan Wright 			TRACE(req, res, "Getting UNIX name error=%d", retcode2);
3623e3f2c991SKeyur Desai 	}
3624e3f2c991SKeyur Desai 
362548258c6bSjp151216 	assert(res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN);
362648258c6bSjp151216 	switch (res->info.how.map_type) {
362748258c6bSjp151216 	case IDMAP_MAP_TYPE_DS_AD:
362848258c6bSjp151216 		map_dn = res->info.how.idmap_how_u.ad.dn;
362948258c6bSjp151216 		map_attr = res->info.how.idmap_how_u.ad.attr;
363048258c6bSjp151216 		map_value = res->info.how.idmap_how_u.ad.value;
363148258c6bSjp151216 		break;
363248258c6bSjp151216 
363348258c6bSjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
363448258c6bSjp151216 		map_dn = res->info.how.idmap_how_u.nldap.dn;
363548258c6bSjp151216 		map_attr = res->info.how.idmap_how_u.nldap.attr;
363648258c6bSjp151216 		map_value = res->info.how.idmap_how_u.nldap.value;
363748258c6bSjp151216 		break;
363848258c6bSjp151216 
363948258c6bSjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
364048258c6bSjp151216 		map_windomain = res->info.how.idmap_how_u.rule.windomain;
364148258c6bSjp151216 		map_winname = res->info.how.idmap_how_u.rule.winname;
364248258c6bSjp151216 		map_unixname = res->info.how.idmap_how_u.rule.unixname;
364348258c6bSjp151216 		map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4;
364448258c6bSjp151216 		break;
364548258c6bSjp151216 
364648258c6bSjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
364748258c6bSjp151216 		break;
364848258c6bSjp151216 
364948258c6bSjp151216 	case IDMAP_MAP_TYPE_LOCAL_SID:
365048258c6bSjp151216 		break;
365148258c6bSjp151216 
3652e3f2c991SKeyur Desai 	case IDMAP_MAP_TYPE_IDMU:
3653e3f2c991SKeyur Desai 		map_dn = res->info.how.idmap_how_u.idmu.dn;
3654e3f2c991SKeyur Desai 		map_attr = res->info.how.idmap_how_u.idmu.attr;
3655e3f2c991SKeyur Desai 		map_value = res->info.how.idmap_how_u.idmu.value;
3656e3f2c991SKeyur Desai 		break;
3657e3f2c991SKeyur Desai 
365848258c6bSjp151216 	default:
3659148c5f43SAlan Wright 		/* Don't cache other mapping types */
366048258c6bSjp151216 		assert(FALSE);
366148258c6bSjp151216 	}
3662e8c27ec8Sbaban 
3663c5c4113dSnw141292 	/*
3664c5c4113dSnw141292 	 * Using NULL for u2w instead of 0 so that our trigger allows
3665c5c4113dSnw141292 	 * the same pid to be the destination in multiple entries
3666c5c4113dSnw141292 	 */
3667c5c4113dSnw141292 	sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache "
3668cd37da74Snw141292 	    "(sidprefix, rid, windomain, canon_winname, pid, unixname, "
366948258c6bSjp151216 	    "is_user, is_wuser, expiration, w2u, u2w, "
367048258c6bSjp151216 	    "map_type, map_dn, map_attr, map_value, map_windomain, "
367148258c6bSjp151216 	    "map_winname, map_unixname, map_is_nt4) "
3672cd37da74Snw141292 	    "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
367348cd229bSGordon Ross 	    "strftime('%%s','now') + %u, %q, 1, "
367448258c6bSjp151216 	    "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ",
3675cd37da74Snw141292 	    res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid,
3676cd37da74Snw141292 	    req->id2domain, req->id2name, req->id1.idmap_id_u.uid,
3677cd37da74Snw141292 	    req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0,
3678e8c27ec8Sbaban 	    (res->id.idtype == IDMAP_USID) ? 1 : 0,
367948cd229bSGordon Ross 	    state->id_cache_timeout,
368048258c6bSjp151216 	    (res->direction == 0) ? "1" : NULL,
368148258c6bSjp151216 	    res->info.how.map_type, map_dn, map_attr, map_value,
368248258c6bSjp151216 	    map_windomain, map_winname, map_unixname, map_is_nt4);
3683c5c4113dSnw141292 
3684c5c4113dSnw141292 	if (sql == NULL) {
3685c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3686c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3687c5c4113dSnw141292 		goto out;
3688c5c4113dSnw141292 	}
3689c5c4113dSnw141292 
3690479ac375Sdm199847 	retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
3691c5c4113dSnw141292 	if (retcode != IDMAP_SUCCESS)
3692c5c4113dSnw141292 		goto out;
3693c5c4113dSnw141292 
3694c5c4113dSnw141292 	state->pid2sid_done = FALSE;
3695c5c4113dSnw141292 	sqlite_freemem(sql);
3696c5c4113dSnw141292 	sql = NULL;
3697c5c4113dSnw141292 
3698e8c27ec8Sbaban 	/* Check if we need to update namecache */
3699e8c27ec8Sbaban 	if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE)
3700c5c4113dSnw141292 		goto out;
3701c5c4113dSnw141292 
37028e228215Sdm199847 	if (req->id2name == NULL)
3703c5c4113dSnw141292 		goto out;
3704c5c4113dSnw141292 
3705c5c4113dSnw141292 	sql = sqlite_mprintf("INSERT OR REPLACE into name_cache "
3706cd37da74Snw141292 	    "(sidprefix, rid, canon_name, domain, type, expiration) "
370748cd229bSGordon Ross 	    "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + %u); ",
3708cd37da74Snw141292 	    res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid,
3709cd37da74Snw141292 	    req->id2name, req->id2domain,
371048cd229bSGordon Ross 	    res->id.idtype, state->name_cache_timeout);
3711c5c4113dSnw141292 
3712c5c4113dSnw141292 	if (sql == NULL) {
3713c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3714c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3715c5c4113dSnw141292 		goto out;
3716c5c4113dSnw141292 	}
3717c5c4113dSnw141292 
3718479ac375Sdm199847 	retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
3719c5c4113dSnw141292 
3720c5c4113dSnw141292 out:
372162c60062Sbaban 	if (sql != NULL)
3722c5c4113dSnw141292 		sqlite_freemem(sql);
3723c5c4113dSnw141292 	return (retcode);
3724c5c4113dSnw141292 }
3725c5c4113dSnw141292 
3726c5c4113dSnw141292 idmap_retcode
update_cache_sid2pid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)3727479ac375Sdm199847 update_cache_sid2pid(lookup_state_t *state,
3728cd37da74Snw141292 		idmap_mapping *req, idmap_id_res *res)
3729cd37da74Snw141292 {
3730c5c4113dSnw141292 	char		*sql = NULL;
3731c5c4113dSnw141292 	idmap_retcode	retcode;
3732c5c4113dSnw141292 	int		is_eph_user;
373348258c6bSjp151216 	char		*map_dn = NULL;
373448258c6bSjp151216 	char		*map_attr = NULL;
373548258c6bSjp151216 	char		*map_value = NULL;
373648258c6bSjp151216 	char 		*map_windomain = NULL;
373748258c6bSjp151216 	char		*map_winname = NULL;
373848258c6bSjp151216 	char		*map_unixname = NULL;
373948258c6bSjp151216 	int		map_is_nt4 = FALSE;
3740c5c4113dSnw141292 
3741c5c4113dSnw141292 	/* Check if we need to cache anything */
3742e8c27ec8Sbaban 	if (ARE_WE_DONE(req->direction))
3743c5c4113dSnw141292 		return (IDMAP_SUCCESS);
3744c5c4113dSnw141292 
3745c5c4113dSnw141292 	/* We don't cache negative entries */
3746c5c4113dSnw141292 	if (res->retcode != IDMAP_SUCCESS)
3747c5c4113dSnw141292 		return (IDMAP_SUCCESS);
3748c5c4113dSnw141292 
3749c5c4113dSnw141292 	if (req->direction & _IDMAP_F_EXP_EPH_UID)
3750c5c4113dSnw141292 		is_eph_user = 1;
3751c5c4113dSnw141292 	else if (req->direction & _IDMAP_F_EXP_EPH_GID)
3752c5c4113dSnw141292 		is_eph_user = 0;
3753c5c4113dSnw141292 	else
3754c5c4113dSnw141292 		is_eph_user = -1;
3755c5c4113dSnw141292 
37569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (is_eph_user >= 0 &&
37579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    !IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid)) {
3758c5c4113dSnw141292 		sql = sqlite_mprintf("UPDATE idmap_cache "
3759c5c4113dSnw141292 		    "SET w2u = 0 WHERE "
3760c5c4113dSnw141292 		    "sidprefix = %Q AND rid = %u AND w2u = 1 AND "
3761c5c4113dSnw141292 		    "pid >= 2147483648 AND is_user = %d;",
3762c5c4113dSnw141292 		    req->id1.idmap_id_u.sid.prefix,
3763c5c4113dSnw141292 		    req->id1.idmap_id_u.sid.rid,
3764c5c4113dSnw141292 		    is_eph_user);
3765c5c4113dSnw141292 		if (sql == NULL) {
3766c5c4113dSnw141292 			retcode = IDMAP_ERR_INTERNAL;
3767c5c4113dSnw141292 			idmapdlog(LOG_ERR, "Out of memory");
3768c5c4113dSnw141292 			goto out;
3769c5c4113dSnw141292 		}
3770c5c4113dSnw141292 
3771479ac375Sdm199847 		retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
3772c5c4113dSnw141292 		if (retcode != IDMAP_SUCCESS)
3773c5c4113dSnw141292 			goto out;
3774c5c4113dSnw141292 
3775c5c4113dSnw141292 		sqlite_freemem(sql);
3776c5c4113dSnw141292 		sql = NULL;
3777c5c4113dSnw141292 	}
3778c5c4113dSnw141292 
3779e8c27ec8Sbaban 	assert(res->direction != IDMAP_DIRECTION_UNDEF);
37809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	assert(res->id.idmap_id_u.uid != IDMAP_SENTINEL_PID);
378148258c6bSjp151216 
378248258c6bSjp151216 	switch (res->info.how.map_type) {
378348258c6bSjp151216 	case IDMAP_MAP_TYPE_DS_AD:
378448258c6bSjp151216 		map_dn = res->info.how.idmap_how_u.ad.dn;
378548258c6bSjp151216 		map_attr = res->info.how.idmap_how_u.ad.attr;
378648258c6bSjp151216 		map_value = res->info.how.idmap_how_u.ad.value;
378748258c6bSjp151216 		break;
378848258c6bSjp151216 
378948258c6bSjp151216 	case IDMAP_MAP_TYPE_DS_NLDAP:
379048258c6bSjp151216 		map_dn = res->info.how.idmap_how_u.nldap.dn;
379148258c6bSjp151216 		map_attr = res->info.how.idmap_how_u.ad.attr;
379248258c6bSjp151216 		map_value = res->info.how.idmap_how_u.nldap.value;
379348258c6bSjp151216 		break;
379448258c6bSjp151216 
379548258c6bSjp151216 	case IDMAP_MAP_TYPE_RULE_BASED:
379648258c6bSjp151216 		map_windomain = res->info.how.idmap_how_u.rule.windomain;
379748258c6bSjp151216 		map_winname = res->info.how.idmap_how_u.rule.winname;
379848258c6bSjp151216 		map_unixname = res->info.how.idmap_how_u.rule.unixname;
379948258c6bSjp151216 		map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4;
380048258c6bSjp151216 		break;
380148258c6bSjp151216 
380248258c6bSjp151216 	case IDMAP_MAP_TYPE_EPHEMERAL:
380348258c6bSjp151216 		break;
380448258c6bSjp151216 
3805e3f2c991SKeyur Desai 	case IDMAP_MAP_TYPE_IDMU:
3806e3f2c991SKeyur Desai 		map_dn = res->info.how.idmap_how_u.idmu.dn;
3807e3f2c991SKeyur Desai 		map_attr = res->info.how.idmap_how_u.idmu.attr;
3808e3f2c991SKeyur Desai 		map_value = res->info.how.idmap_how_u.idmu.value;
3809e3f2c991SKeyur Desai 		break;
3810e3f2c991SKeyur Desai 
381148258c6bSjp151216 	default:
3812148c5f43SAlan Wright 		/* Don't cache other mapping types */
381348258c6bSjp151216 		assert(FALSE);
381448258c6bSjp151216 	}
3815cd37da74Snw141292 
3816c5c4113dSnw141292 	sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache "
3817cd37da74Snw141292 	    "(sidprefix, rid, windomain, canon_winname, pid, unixname, "
381848258c6bSjp151216 	    "is_user, is_wuser, expiration, w2u, u2w, "
381948258c6bSjp151216 	    "map_type, map_dn, map_attr, map_value, map_windomain, "
382048258c6bSjp151216 	    "map_winname, map_unixname, map_is_nt4) "
3821cd37da74Snw141292 	    "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
382248cd229bSGordon Ross 	    "strftime('%%s','now') + %u, 1, %q, "
382348258c6bSjp151216 	    "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
3824cd37da74Snw141292 	    req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid,
3825e8c27ec8Sbaban 	    (req->id1domain != NULL) ? req->id1domain : "", req->id1name,
3826e8c27ec8Sbaban 	    res->id.idmap_id_u.uid, req->id2name,
3827e8c27ec8Sbaban 	    (res->id.idtype == IDMAP_UID) ? 1 : 0,
3828cd37da74Snw141292 	    (req->id1.idtype == IDMAP_USID) ? 1 : 0,
382948cd229bSGordon Ross 	    state->id_cache_timeout,
383048258c6bSjp151216 	    (res->direction == 0) ? "1" : NULL,
383148258c6bSjp151216 	    res->info.how.map_type, map_dn, map_attr, map_value,
383248258c6bSjp151216 	    map_windomain, map_winname, map_unixname, map_is_nt4);
3833c5c4113dSnw141292 
3834c5c4113dSnw141292 	if (sql == NULL) {
3835c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3836c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3837c5c4113dSnw141292 		goto out;
3838c5c4113dSnw141292 	}
3839c5c4113dSnw141292 
3840479ac375Sdm199847 	retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
3841c5c4113dSnw141292 	if (retcode != IDMAP_SUCCESS)
3842c5c4113dSnw141292 		goto out;
3843c5c4113dSnw141292 
3844c5c4113dSnw141292 	state->sid2pid_done = FALSE;
3845c5c4113dSnw141292 	sqlite_freemem(sql);
3846c5c4113dSnw141292 	sql = NULL;
3847c5c4113dSnw141292 
3848e8c27ec8Sbaban 	/* Check if we need to update namecache */
3849e8c27ec8Sbaban 	if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE)
3850c5c4113dSnw141292 		goto out;
3851c5c4113dSnw141292 
3852cf5b5989Sdm199847 	if (EMPTY_STRING(req->id1name))
3853c5c4113dSnw141292 		goto out;
3854c5c4113dSnw141292 
3855c5c4113dSnw141292 	sql = sqlite_mprintf("INSERT OR REPLACE into name_cache "
3856cd37da74Snw141292 	    "(sidprefix, rid, canon_name, domain, type, expiration) "
385748cd229bSGordon Ross 	    "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + %u); ",
3858cd37da74Snw141292 	    req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid,
3859cd37da74Snw141292 	    req->id1name, req->id1domain,
386048cd229bSGordon Ross 	    req->id1.idtype, state->name_cache_timeout);
3861c5c4113dSnw141292 
3862c5c4113dSnw141292 	if (sql == NULL) {
3863c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3864c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3865c5c4113dSnw141292 		goto out;
3866c5c4113dSnw141292 	}
3867c5c4113dSnw141292 
3868479ac375Sdm199847 	retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
3869c5c4113dSnw141292 
3870c5c4113dSnw141292 out:
387162c60062Sbaban 	if (sql != NULL)
3872c5c4113dSnw141292 		sqlite_freemem(sql);
3873c5c4113dSnw141292 	return (retcode);
3874c5c4113dSnw141292 }
3875c5c4113dSnw141292 
3876cd37da74Snw141292 static
3877cd37da74Snw141292 idmap_retcode
lookup_cache_pid2sid(sqlite * cache,idmap_mapping * req,idmap_id_res * res,int is_user)3878c5c4113dSnw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res,
3879fe1c642dSBill Krier 		int is_user)
3880cd37da74Snw141292 {
3881c5c4113dSnw141292 	char		*end;
3882c5c4113dSnw141292 	char		*sql = NULL;
3883c5c4113dSnw141292 	const char	**values;
3884c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
3885c5c4113dSnw141292 	int		ncol;
3886c5c4113dSnw141292 	idmap_retcode	retcode = IDMAP_SUCCESS;
3887c5c4113dSnw141292 	time_t		curtime;
3888e8c27ec8Sbaban 	idmap_id_type	idtype;
3889c5c4113dSnw141292 
3890c5c4113dSnw141292 	/* Current time */
3891c5c4113dSnw141292 	errno = 0;
3892c5c4113dSnw141292 	if ((curtime = time(NULL)) == (time_t)-1) {
3893cd37da74Snw141292 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
3894c5c4113dSnw141292 		    strerror(errno));
3895c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
3896c5c4113dSnw141292 		goto out;
3897c5c4113dSnw141292 	}
3898c5c4113dSnw141292 
3899e8c27ec8Sbaban 	/* SQL to lookup the cache by pid or by unixname */
39009fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (req->id1.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
390148258c6bSjp151216 		sql = sqlite_mprintf("SELECT sidprefix, rid, "
390248258c6bSjp151216 		    "canon_winname, windomain, w2u, is_wuser, "
390348258c6bSjp151216 		    "map_type, map_dn, map_attr, map_value, map_windomain, "
390448258c6bSjp151216 		    "map_winname, map_unixname, map_is_nt4 "
3905c5c4113dSnw141292 		    "FROM idmap_cache WHERE "
3906c5c4113dSnw141292 		    "pid = %u AND u2w = 1 AND is_user = %d AND "
3907c5c4113dSnw141292 		    "(pid >= 2147483648 OR "
3908c5c4113dSnw141292 		    "(expiration = 0 OR expiration ISNULL OR "
3909c5c4113dSnw141292 		    "expiration > %d));",
3910c5c4113dSnw141292 		    req->id1.idmap_id_u.uid, is_user, curtime);
3911e8c27ec8Sbaban 	} else if (req->id1name != NULL) {
391248258c6bSjp151216 		sql = sqlite_mprintf("SELECT sidprefix, rid, "
391348258c6bSjp151216 		    "canon_winname, windomain, w2u, is_wuser, "
391448258c6bSjp151216 		    "map_type, map_dn, map_attr, map_value, map_windomain, "
391548258c6bSjp151216 		    "map_winname, map_unixname, map_is_nt4 "
3916e8c27ec8Sbaban 		    "FROM idmap_cache WHERE "
3917e8c27ec8Sbaban 		    "unixname = %Q AND u2w = 1 AND is_user = %d AND "
3918e8c27ec8Sbaban 		    "(pid >= 2147483648 OR "
3919e8c27ec8Sbaban 		    "(expiration = 0 OR expiration ISNULL OR "
3920e8c27ec8Sbaban 		    "expiration > %d));",
3921e8c27ec8Sbaban 		    req->id1name, is_user, curtime);
392248258c6bSjp151216 	} else {
392348258c6bSjp151216 		retcode = IDMAP_ERR_ARG;
392448258c6bSjp151216 		goto out;
3925e8c27ec8Sbaban 	}
3926e8c27ec8Sbaban 
3927c5c4113dSnw141292 	if (sql == NULL) {
3928c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
3929c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
3930c5c4113dSnw141292 		goto out;
3931c5c4113dSnw141292 	}
393248258c6bSjp151216 	retcode = sql_compile_n_step_once(
393348258c6bSjp151216 	    cache, sql, &vm, &ncol, 14, &values);
3934c5c4113dSnw141292 	sqlite_freemem(sql);
3935c5c4113dSnw141292 
3936c5c4113dSnw141292 	if (retcode == IDMAP_ERR_NOTFOUND)
3937c5c4113dSnw141292 		goto out;
3938c5c4113dSnw141292 	else if (retcode == IDMAP_SUCCESS) {
3939c5c4113dSnw141292 		/* sanity checks */
3940c5c4113dSnw141292 		if (values[0] == NULL || values[1] == NULL) {
3941c5c4113dSnw141292 			retcode = IDMAP_ERR_CACHE;
3942c5c4113dSnw141292 			goto out;
3943c5c4113dSnw141292 		}
3944c5c4113dSnw141292 
3945e8c27ec8Sbaban 		switch (res->id.idtype) {
3946c5c4113dSnw141292 		case IDMAP_SID:
3947cd37da74Snw141292 		case IDMAP_USID:
3948cd37da74Snw141292 		case IDMAP_GSID:
3949e8c27ec8Sbaban 			idtype = strtol(values[5], &end, 10) == 1
3950cd37da74Snw141292 			    ? IDMAP_USID : IDMAP_GSID;
3951cd37da74Snw141292 
3952e8c27ec8Sbaban 			if (res->id.idtype == IDMAP_USID &&
3953e8c27ec8Sbaban 			    idtype != IDMAP_USID) {
3954cd37da74Snw141292 				retcode = IDMAP_ERR_NOTUSER;
3955cd37da74Snw141292 				goto out;
3956e8c27ec8Sbaban 			} else if (res->id.idtype == IDMAP_GSID &&
3957e8c27ec8Sbaban 			    idtype != IDMAP_GSID) {
3958cd37da74Snw141292 				retcode = IDMAP_ERR_NOTGROUP;
3959cd37da74Snw141292 				goto out;
3960cd37da74Snw141292 			}
3961e8c27ec8Sbaban 			res->id.idtype = idtype;
3962cd37da74Snw141292 
3963c5c4113dSnw141292 			res->id.idmap_id_u.sid.rid =
3964c5c4113dSnw141292 			    strtoul(values[1], &end, 10);
3965c5c4113dSnw141292 			res->id.idmap_id_u.sid.prefix = strdup(values[0]);
3966c5c4113dSnw141292 			if (res->id.idmap_id_u.sid.prefix == NULL) {
3967c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
3968c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
3969c5c4113dSnw141292 				goto out;
3970c5c4113dSnw141292 			}
3971c5c4113dSnw141292 
397262c60062Sbaban 			if (values[4] != NULL)
3973c5c4113dSnw141292 				res->direction =
3974651c0131Sbaban 				    (strtol(values[4], &end, 10) == 0)?
3975651c0131Sbaban 				    IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
3976c5c4113dSnw141292 			else
3977651c0131Sbaban 				res->direction = IDMAP_DIRECTION_U2W;
3978c5c4113dSnw141292 
3979fe1c642dSBill Krier 			if (values[2] == NULL)
3980c5c4113dSnw141292 				break;
39818e228215Sdm199847 			req->id2name = strdup(values[2]);
39828e228215Sdm199847 			if (req->id2name == NULL) {
3983c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
3984c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
3985c5c4113dSnw141292 				goto out;
3986c5c4113dSnw141292 			}
3987c5c4113dSnw141292 
3988c5c4113dSnw141292 			if (values[3] == NULL)
3989c5c4113dSnw141292 				break;
39908e228215Sdm199847 			req->id2domain = strdup(values[3]);
39918e228215Sdm199847 			if (req->id2domain == NULL) {
3992c5c4113dSnw141292 				idmapdlog(LOG_ERR, "Out of memory");
3993c5c4113dSnw141292 				retcode = IDMAP_ERR_MEMORY;
3994c5c4113dSnw141292 				goto out;
3995c5c4113dSnw141292 			}
3996cd37da74Snw141292 
3997c5c4113dSnw141292 			break;
3998c5c4113dSnw141292 		default:
3999c5c4113dSnw141292 			retcode = IDMAP_ERR_NOTSUPPORTED;
4000c5c4113dSnw141292 			break;
4001c5c4113dSnw141292 		}
400248258c6bSjp151216 		if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
400348258c6bSjp151216 			res->info.src = IDMAP_MAP_SRC_CACHE;
400448258c6bSjp151216 			res->info.how.map_type = strtoul(values[6], &end, 10);
400548258c6bSjp151216 			switch (res->info.how.map_type) {
400648258c6bSjp151216 			case IDMAP_MAP_TYPE_DS_AD:
400748258c6bSjp151216 				res->info.how.idmap_how_u.ad.dn =
400848258c6bSjp151216 				    strdup(values[7]);
400948258c6bSjp151216 				res->info.how.idmap_how_u.ad.attr =
401048258c6bSjp151216 				    strdup(values[8]);
401148258c6bSjp151216 				res->info.how.idmap_how_u.ad.value =
401248258c6bSjp151216 				    strdup(values[9]);
401348258c6bSjp151216 				break;
401448258c6bSjp151216 
401548258c6bSjp151216 			case IDMAP_MAP_TYPE_DS_NLDAP:
401648258c6bSjp151216 				res->info.how.idmap_how_u.nldap.dn =
401748258c6bSjp151216 				    strdup(values[7]);
401848258c6bSjp151216 				res->info.how.idmap_how_u.nldap.attr =
401948258c6bSjp151216 				    strdup(values[8]);
402048258c6bSjp151216 				res->info.how.idmap_how_u.nldap.value =
402148258c6bSjp151216 				    strdup(values[9]);
402248258c6bSjp151216 				break;
402348258c6bSjp151216 
402448258c6bSjp151216 			case IDMAP_MAP_TYPE_RULE_BASED:
402548258c6bSjp151216 				res->info.how.idmap_how_u.rule.windomain =
402648258c6bSjp151216 				    strdup(values[10]);
402748258c6bSjp151216 				res->info.how.idmap_how_u.rule.winname =
402848258c6bSjp151216 				    strdup(values[11]);
402948258c6bSjp151216 				res->info.how.idmap_how_u.rule.unixname =
403048258c6bSjp151216 				    strdup(values[12]);
403148258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_nt4 =
403248258c6bSjp151216 				    strtoul(values[13], &end, 10);
403348258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_user =
403448258c6bSjp151216 				    is_user;
403548258c6bSjp151216 				res->info.how.idmap_how_u.rule.is_wuser =
403648258c6bSjp151216 				    strtol(values[5], &end, 10);
403748258c6bSjp151216 				break;
403848258c6bSjp151216 
403948258c6bSjp151216 			case IDMAP_MAP_TYPE_EPHEMERAL:
404048258c6bSjp151216 				break;
404148258c6bSjp151216 
404248258c6bSjp151216 			case IDMAP_MAP_TYPE_LOCAL_SID:
404348258c6bSjp151216 				break;
404448258c6bSjp151216 
404548258c6bSjp151216 			case IDMAP_MAP_TYPE_KNOWN_SID:
404648258c6bSjp151216 				break;
404748258c6bSjp151216 
4048e3f2c991SKeyur Desai 			case IDMAP_MAP_TYPE_IDMU:
4049e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.dn =
4050e3f2c991SKeyur Desai 				    strdup(values[7]);
4051e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.attr =
4052e3f2c991SKeyur Desai 				    strdup(values[8]);
4053e3f2c991SKeyur Desai 				res->info.how.idmap_how_u.idmu.value =
4054e3f2c991SKeyur Desai 				    strdup(values[9]);
4055e3f2c991SKeyur Desai 				break;
4056e3f2c991SKeyur Desai 
405748258c6bSjp151216 			default:
4058e3f2c991SKeyur Desai 				/* Unknown mapping type */
405948258c6bSjp151216 				assert(FALSE);
406048258c6bSjp151216 			}
406148258c6bSjp151216 		}
4062c5c4113dSnw141292 	}
4063c5c4113dSnw141292 
4064c5c4113dSnw141292 out:
406562c60062Sbaban 	if (vm != NULL)
4066c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
4067c5c4113dSnw141292 	return (retcode);
4068c5c4113dSnw141292 }
4069c5c4113dSnw141292 
407008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /*
407108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Given:
407208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * cache	sqlite handle
407308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * name		Windows user name
407408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * domain	Windows domain name
407508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
407608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Return:  Error code
407708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
407808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *canonname	Canonical name (if canonname is non-NULL) [1]
407908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *sidprefix	SID prefix [1]
408008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *rid		RID
408108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *type	Type of name
408208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
408308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * [1] malloc'ed, NULL on error
408408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  */
4085cd37da74Snw141292 static
4086cd37da74Snw141292 idmap_retcode
lookup_cache_name2sid(sqlite * cache,const char * name,const char * domain,char ** canonname,char ** sidprefix,idmap_rid_t * rid,idmap_id_type * type)4087148c5f43SAlan Wright lookup_cache_name2sid(
4088148c5f43SAlan Wright     sqlite *cache,
4089148c5f43SAlan Wright     const char *name,
4090148c5f43SAlan Wright     const char *domain,
4091148c5f43SAlan Wright     char **canonname,
4092148c5f43SAlan Wright     char **sidprefix,
4093148c5f43SAlan Wright     idmap_rid_t *rid,
4094148c5f43SAlan Wright     idmap_id_type *type)
4095cd37da74Snw141292 {
4096cd37da74Snw141292 	char		*end, *lower_name;
409708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*sql;
4098c5c4113dSnw141292 	const char	**values;
4099c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
4100c5c4113dSnw141292 	int		ncol;
4101c5c4113dSnw141292 	time_t		curtime;
410208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	idmap_retcode	retcode;
410308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
410408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	*sidprefix = NULL;
410508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canonname != NULL)
410608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canonname = NULL;
4107c5c4113dSnw141292 
4108c5c4113dSnw141292 	/* Get current time */
4109c5c4113dSnw141292 	errno = 0;
4110c5c4113dSnw141292 	if ((curtime = time(NULL)) == (time_t)-1) {
4111cd37da74Snw141292 		idmapdlog(LOG_ERR, "Failed to get current time (%s)",
4112c5c4113dSnw141292 		    strerror(errno));
4113c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
4114c5c4113dSnw141292 		goto out;
4115c5c4113dSnw141292 	}
4116c5c4113dSnw141292 
4117c5c4113dSnw141292 	/* SQL to lookup the cache */
4118cd37da74Snw141292 	if ((lower_name = tolower_u8(name)) == NULL)
4119cd37da74Snw141292 		lower_name = (char *)name;
4120cd37da74Snw141292 	sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name "
4121cd37da74Snw141292 	    "FROM name_cache WHERE name = %Q AND domain = %Q AND "
4122c5c4113dSnw141292 	    "(expiration = 0 OR expiration ISNULL OR "
4123cd37da74Snw141292 	    "expiration > %d);", lower_name, domain, curtime);
4124cd37da74Snw141292 	if (lower_name != name)
4125cd37da74Snw141292 		free(lower_name);
4126c5c4113dSnw141292 	if (sql == NULL) {
4127c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
4128c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
4129c5c4113dSnw141292 		goto out;
4130c5c4113dSnw141292 	}
4131cd37da74Snw141292 	retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values);
413208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
4133c5c4113dSnw141292 	sqlite_freemem(sql);
4134c5c4113dSnw141292 
413508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (retcode != IDMAP_SUCCESS)
413608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		goto out;
413708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
413862c60062Sbaban 	if (type != NULL) {
4139c5c4113dSnw141292 		if (values[2] == NULL) {
4140c5c4113dSnw141292 			retcode = IDMAP_ERR_CACHE;
4141c5c4113dSnw141292 			goto out;
4142c5c4113dSnw141292 		}
4143148c5f43SAlan Wright 		*type = xlate_legacy_type(strtol(values[2], &end, 10));
4144c5c4113dSnw141292 	}
4145c5c4113dSnw141292 
4146e8c27ec8Sbaban 	if (values[0] == NULL || values[1] == NULL) {
4147e8c27ec8Sbaban 		retcode = IDMAP_ERR_CACHE;
4148e8c27ec8Sbaban 		goto out;
4149e8c27ec8Sbaban 	}
4150e8c27ec8Sbaban 
4151cd37da74Snw141292 	if (canonname != NULL) {
4152cd37da74Snw141292 		assert(values[3] != NULL);
415308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canonname = strdup(values[3]);
415408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (*canonname == NULL) {
4155cd37da74Snw141292 			idmapdlog(LOG_ERR, "Out of memory");
4156cd37da74Snw141292 			retcode = IDMAP_ERR_MEMORY;
4157cd37da74Snw141292 			goto out;
4158cd37da74Snw141292 		}
4159cd37da74Snw141292 	}
4160cd37da74Snw141292 
416108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	*sidprefix = strdup(values[0]);
416208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (*sidprefix == NULL) {
4163c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
4164c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
4165c5c4113dSnw141292 		goto out;
4166c5c4113dSnw141292 	}
4167c5c4113dSnw141292 	*rid = strtoul(values[1], &end, 10);
416808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
416908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	retcode = IDMAP_SUCCESS;
4170c5c4113dSnw141292 
4171c5c4113dSnw141292 out:
417262c60062Sbaban 	if (vm != NULL)
4173c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
417408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 
417508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (retcode != IDMAP_SUCCESS) {
417608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		free(*sidprefix);
417708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*sidprefix = NULL;
417808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (canonname != NULL) {
417908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			free(*canonname);
418008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*canonname = NULL;
418108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		}
418208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
4183c5c4113dSnw141292 	return (retcode);
4184c5c4113dSnw141292 }
4185c5c4113dSnw141292 
4186cd37da74Snw141292 static
4187cd37da74Snw141292 idmap_retcode
ad_lookup_by_winname(lookup_state_t * state,const char * name,const char * domain,int esidtype,char ** dn,char ** attr,char ** value,char ** canonname,char ** sidprefix,idmap_rid_t * rid,idmap_id_type * wintype,char ** unixname)4188e8c27ec8Sbaban ad_lookup_by_winname(lookup_state_t *state,
4189148c5f43SAlan Wright 		const char *name, const char *domain, int esidtype,
419048258c6bSjp151216 		char **dn, char **attr, char **value, char **canonname,
4191148c5f43SAlan Wright 		char **sidprefix, idmap_rid_t *rid, idmap_id_type *wintype,
419248258c6bSjp151216 		char **unixname)
4193cd37da74Snw141292 {
41944d61c878SJulian Pullen 	int			retries;
4195c5c4113dSnw141292 	idmap_query_state_t	*qs = NULL;
4196c5c4113dSnw141292 	idmap_retcode		rc, retcode;
41974d61c878SJulian Pullen 	int			i;
41984d61c878SJulian Pullen 	int			found_ad = 0;
4199c5c4113dSnw141292 
42002b4a7802SBaban Kenkre 	RDLOCK_CONFIG();
4201e3f2c991SKeyur Desai 	if (_idmapdstate.num_gcs > 0) {
4202e3f2c991SKeyur Desai 		for (i = 0; i < _idmapdstate.num_gcs && !found_ad; i++) {
42034d61c878SJulian Pullen 			retries = 0;
42044d61c878SJulian Pullen retry:
4205e3f2c991SKeyur Desai 			retcode = idmap_lookup_batch_start(
4206e3f2c991SKeyur Desai 			    _idmapdstate.gcs[i],
4207e3f2c991SKeyur Desai 			    1,
4208e3f2c991SKeyur Desai 			    _idmapdstate.cfg->pgcfg.directory_based_mapping,
4209e3f2c991SKeyur Desai 			    _idmapdstate.cfg->pgcfg.default_domain,
4210e3f2c991SKeyur Desai 			    &qs);
4211e8c27ec8Sbaban 			if (retcode != IDMAP_SUCCESS) {
42122b4a7802SBaban Kenkre 				if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
42132b4a7802SBaban Kenkre 				    retries++ < ADUTILS_DEF_NUM_RETRIES)
42140dcc7149Snw141292 					goto retry;
42154d61c878SJulian Pullen 				degrade_svc(1, "failed to create request for "
42164d61c878SJulian Pullen 				    "AD lookup by winname");
4217e8c27ec8Sbaban 				return (retcode);
4218c5c4113dSnw141292 			}
4219c5c4113dSnw141292 
4220c8e26105Sjp151216 			restore_svc();
4221c8e26105Sjp151216 
42224d61c878SJulian Pullen 			if (state != NULL && i == 0) {
42234d61c878SJulian Pullen 				/*
42244d61c878SJulian Pullen 				 * Directory based name mapping is only
42254d61c878SJulian Pullen 				 * performed within the joined forest (i == 0).
42264d61c878SJulian Pullen 				 * We don't trust other "trusted" forests to
42274d61c878SJulian Pullen 				 * provide DS-based name mapping information
42284d61c878SJulian Pullen 				 * because AD's definition of "cross-forest
42294d61c878SJulian Pullen 				 * trust" does not encompass this sort of
42304d61c878SJulian Pullen 				 * behavior.
42314d61c878SJulian Pullen 				 */
42324d61c878SJulian Pullen 				idmap_lookup_batch_set_unixattr(qs,
42334d61c878SJulian Pullen 				    state->ad_unixuser_attr,
4234e8c27ec8Sbaban 				    state->ad_unixgroup_attr);
42354d61c878SJulian Pullen 			}
4236c5c4113dSnw141292 
42374d61c878SJulian Pullen 			retcode = idmap_name2sid_batch_add1(qs, name, domain,
4238148c5f43SAlan Wright 			    esidtype, dn, attr, value, canonname, sidprefix,
4239e3f2c991SKeyur Desai 			    rid, wintype, unixname, NULL, &rc);
42404d61c878SJulian Pullen 			if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
42414d61c878SJulian Pullen 				idmap_lookup_release_batch(&qs);
42424d61c878SJulian Pullen 				continue;
42434d61c878SJulian Pullen 			}
42444d61c878SJulian Pullen 			found_ad = 1;
4245e8c27ec8Sbaban 			if (retcode != IDMAP_SUCCESS)
424684decf41Sjp151216 				idmap_lookup_release_batch(&qs);
4247c5c4113dSnw141292 			else
42480dcc7149Snw141292 				retcode = idmap_lookup_batch_end(&qs);
4249c5c4113dSnw141292 
42502b4a7802SBaban Kenkre 			if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
42512b4a7802SBaban Kenkre 			    retries++ < ADUTILS_DEF_NUM_RETRIES)
4252c5c4113dSnw141292 				goto retry;
4253c8e26105Sjp151216 			else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
42544d61c878SJulian Pullen 				degrade_svc(1,
42554d61c878SJulian Pullen 				    "some AD lookups timed out repeatedly");
42564d61c878SJulian Pullen 		}
42574d61c878SJulian Pullen 	} else {
42584d61c878SJulian Pullen 		/* No AD case */
42594d61c878SJulian Pullen 		retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
42604d61c878SJulian Pullen 	}
42614d61c878SJulian Pullen 	UNLOCK_CONFIG();
4262c5c4113dSnw141292 
4263c5c4113dSnw141292 	if (retcode != IDMAP_SUCCESS) {
42649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		idmapdlog(LOG_NOTICE,
42659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    "AD lookup of winname %s@%s failed, error code %d",
42669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    name == NULL ? "(null)" : name,
42679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    domain == NULL ? "(null)" : domain,
42689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    retcode);
4269c5c4113dSnw141292 		return (retcode);
4270e8c27ec8Sbaban 	}
4271c5c4113dSnw141292 	return (rc);
4272c5c4113dSnw141292 }
4273c5c4113dSnw141292 
427408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /*
427508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Given:
427608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * cache	sqlite handle to cache
427708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * name		Windows user name
427808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * domain	Windows domain name
427908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * local_only	if true, don't try AD lookups
428008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
428108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * Returns: Error code
428208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
428308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *canonname	Canonical name (if non-NULL) [1]
428408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *canondomain	Canonical domain (if non-NULL) [1]
428508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *sidprefix	SID prefix [1]
428608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *rid		RID
428708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * *req		Request (direction is updated)
428808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  *
428908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  * [1] malloc'ed, NULL on error
429008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States  */
4291cd37da74Snw141292 idmap_retcode
lookup_name2sid(sqlite * cache,const char * name,const char * domain,int want_wuser,char ** canonname,char ** canondomain,char ** sidprefix,idmap_rid_t * rid,idmap_id_type * type,idmap_mapping * req,int local_only)429208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_name2sid(
429308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     sqlite *cache,
429408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     const char *name,
429508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     const char *domain,
4296148c5f43SAlan Wright     int want_wuser,
429708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **canonname,
429808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **canondomain,
429908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     char **sidprefix,
430008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     idmap_rid_t *rid,
4301148c5f43SAlan Wright     idmap_id_type *type,
430208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     idmap_mapping *req,
430308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States     int local_only)
4304cd37da74Snw141292 {
4305c5c4113dSnw141292 	idmap_retcode	retcode;
4306c5c4113dSnw141292 
4307cd37da74Snw141292 	*sidprefix = NULL;
4308e8c27ec8Sbaban 	if (canonname != NULL)
4309cd37da74Snw141292 		*canonname = NULL;
431008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (canondomain != NULL)
431108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		*canondomain = NULL;
4312cd37da74Snw141292 
4313e8c27ec8Sbaban 	/* Lookup well-known SIDs table */
431408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	retcode = lookup_wksids_name2sid(name, domain, canonname, canondomain,
4315148c5f43SAlan Wright 	    sidprefix, rid, type);
431662c60062Sbaban 	if (retcode == IDMAP_SUCCESS) {
4317e8c27ec8Sbaban 		req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
431862c60062Sbaban 		goto out;
431962c60062Sbaban 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
432062c60062Sbaban 		return (retcode);
432162c60062Sbaban 	}
432262c60062Sbaban 
4323e8c27ec8Sbaban 	/* Lookup cache */
4324cd37da74Snw141292 	retcode = lookup_cache_name2sid(cache, name, domain, canonname,
4325148c5f43SAlan Wright 	    sidprefix, rid, type);
4326e8c27ec8Sbaban 	if (retcode == IDMAP_SUCCESS) {
4327e8c27ec8Sbaban 		req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
4328e8c27ec8Sbaban 		goto out;
4329e8c27ec8Sbaban 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
4330e8c27ec8Sbaban 		return (retcode);
4331e8c27ec8Sbaban 	}
4332e8c27ec8Sbaban 
4333479ac375Sdm199847 	/*
4334479ac375Sdm199847 	 * The caller may be using this function to determine if this
4335479ac375Sdm199847 	 * request needs to be marked for AD lookup or not
4336479ac375Sdm199847 	 * (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this
4337479ac375Sdm199847 	 * function to AD lookup now.
4338479ac375Sdm199847 	 */
4339479ac375Sdm199847 	if (local_only)
4340479ac375Sdm199847 		return (retcode);
4341479ac375Sdm199847 
4342148c5f43SAlan Wright 	if (_idmapdstate.cfg->pgcfg.use_lsa &&
4343148c5f43SAlan Wright 	    _idmapdstate.cfg->pgcfg.domain_name != NULL &&
4344148c5f43SAlan Wright 	    name != NULL && *sidprefix == NULL) {
4345148c5f43SAlan Wright 		retcode = lookup_lsa_by_name(name, domain,
4346148c5f43SAlan Wright 		    sidprefix, rid,
4347148c5f43SAlan Wright 		    canonname, canondomain,
4348148c5f43SAlan Wright 		    type);
4349148c5f43SAlan Wright 		if (retcode == IDMAP_SUCCESS)
4350148c5f43SAlan Wright 			goto out;
4351148c5f43SAlan Wright 		else if (retcode != IDMAP_ERR_NOTFOUND)
4352148c5f43SAlan Wright 			return (retcode);
4353148c5f43SAlan Wright 	}
4354148c5f43SAlan Wright 
4355e8c27ec8Sbaban 	/* Lookup AD */
4356148c5f43SAlan Wright 	retcode = ad_lookup_by_winname(NULL, name, domain, IDMAP_POSIXID,
4357148c5f43SAlan Wright 	    NULL, NULL, NULL, canonname, sidprefix, rid, type, NULL);
4358c5c4113dSnw141292 	if (retcode != IDMAP_SUCCESS)
4359c5c4113dSnw141292 		return (retcode);
4360c5c4113dSnw141292 
436162c60062Sbaban out:
4362c5c4113dSnw141292 	/*
4363c5c4113dSnw141292 	 * Entry found (cache or Windows lookup)
4364c5c4113dSnw141292 	 */
4365148c5f43SAlan Wright 	if (want_wuser == 1 && *type != IDMAP_USID)
4366e8c27ec8Sbaban 		retcode = IDMAP_ERR_NOTUSER;
4367148c5f43SAlan Wright 	else if (want_wuser == 0 && *type != IDMAP_GSID)
4368e8c27ec8Sbaban 		retcode = IDMAP_ERR_NOTGROUP;
4369148c5f43SAlan Wright 	else if (want_wuser == -1) {
4370148c5f43SAlan Wright 		/*
4371148c5f43SAlan Wright 		 * Caller wants to know if its user or group
4372148c5f43SAlan Wright 		 * Verify that it's one or the other.
4373148c5f43SAlan Wright 		 */
4374148c5f43SAlan Wright 		if (*type != IDMAP_USID && *type != IDMAP_GSID)
4375e8c27ec8Sbaban 			retcode = IDMAP_ERR_SID;
4376e8c27ec8Sbaban 	}
4377e8c27ec8Sbaban 
437808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (retcode == IDMAP_SUCCESS) {
437908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		/*
438008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * If we were asked for a canonical domain and none
438108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * of the searches have provided one, assume it's the
438208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 * supplied domain.
438308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		 */
438408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (canondomain != NULL && *canondomain == NULL) {
438508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*canondomain = strdup(domain);
438608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			if (*canondomain == NULL)
438708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 				retcode = IDMAP_ERR_MEMORY;
438808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		}
438908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
4390e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS) {
4391cd37da74Snw141292 		free(*sidprefix);
4392cd37da74Snw141292 		*sidprefix = NULL;
4393e8c27ec8Sbaban 		if (canonname != NULL) {
4394cd37da74Snw141292 			free(*canonname);
4395cd37da74Snw141292 			*canonname = NULL;
4396cd37da74Snw141292 		}
439708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (canondomain != NULL) {
439808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			free(*canondomain);
439908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 			*canondomain = NULL;
440008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 		}
4401c5c4113dSnw141292 	}
4402c5c4113dSnw141292 	return (retcode);
4403c5c4113dSnw141292 }
4404c5c4113dSnw141292 
4405cd37da74Snw141292 static
4406cd37da74Snw141292 idmap_retcode
name_based_mapping_pid2sid(lookup_state_t * state,const char * unixname,int is_user,idmap_mapping * req,idmap_id_res * res)4407479ac375Sdm199847 name_based_mapping_pid2sid(lookup_state_t *state, const char *unixname,
4408cd37da74Snw141292 		int is_user, idmap_mapping *req, idmap_id_res *res)
4409cd37da74Snw141292 {
4410c5c4113dSnw141292 	const char	*winname, *windomain;
4411cd37da74Snw141292 	char		*canonname;
441208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*canondomain;
4413c5c4113dSnw141292 	char		*sql = NULL, *errmsg = NULL;
4414c5c4113dSnw141292 	idmap_retcode	retcode;
4415c5c4113dSnw141292 	char		*end;
4416c5c4113dSnw141292 	const char	**values;
4417c5c4113dSnw141292 	sqlite_vm	*vm = NULL;
441848258c6bSjp151216 	int		ncol, r;
4419148c5f43SAlan Wright 	int		want_wuser;
4420e8c27ec8Sbaban 	const char	*me = "name_based_mapping_pid2sid";
442148258c6bSjp151216 	idmap_namerule	*rule = &res->info.how.idmap_how_u.rule;
442248258c6bSjp151216 	int direction;
4423e8c27ec8Sbaban 
4424e8c27ec8Sbaban 	assert(unixname != NULL); /* We have unixname */
4425e8c27ec8Sbaban 	assert(req->id2name == NULL); /* We don't have winname */
4426e8c27ec8Sbaban 	assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */
4427c5c4113dSnw141292 
4428c5c4113dSnw141292 	sql = sqlite_mprintf(
442948258c6bSjp151216 	    "SELECT winname_display, windomain, w2u_order, "
443048258c6bSjp151216 	    "is_wuser, unixname, is_nt4 "
443148258c6bSjp151216 	    "FROM namerules WHERE "
4432c5c4113dSnw141292 	    "u2w_order > 0 AND is_user = %d AND "
4433c5c4113dSnw141292 	    "(unixname = %Q OR unixname = '*') "
4434cd37da74Snw141292 	    "ORDER BY u2w_order ASC;", is_user, unixname);
4435c5c4113dSnw141292 	if (sql == NULL) {
4436c5c4113dSnw141292 		idmapdlog(LOG_ERR, "Out of memory");
4437c5c4113dSnw141292 		retcode = IDMAP_ERR_MEMORY;
4438c5c4113dSnw141292 		goto out;
4439c5c4113dSnw141292 	}
4440c5c4113dSnw141292 
4441479ac375Sdm199847 	if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) {
4442c5c4113dSnw141292 		retcode = IDMAP_ERR_INTERNAL;
4443cd37da74Snw141292 		idmapdlog(LOG_ERR, "%s: database error (%s)", me,
4444cd37da74Snw141292 		    CHECK_NULL(errmsg));
4445c5c4113dSnw141292 		sqlite_freemem(errmsg);
4446c5c4113dSnw141292 		goto out;
4447c5c4113dSnw141292 	}
4448c5c4113dSnw141292 
444948258c6bSjp151216 	for (;;) {
4450c5c4113dSnw141292 		r = sqlite_step(vm, &ncol, &values, NULL);
445184decf41Sjp151216 		assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
445284decf41Sjp151216 		if (r == SQLITE_ROW) {
445348258c6bSjp151216 			if (ncol < 6) {
4454c5c4113dSnw141292 				retcode = IDMAP_ERR_INTERNAL;
4455c5c4113dSnw141292 				goto out;
4456c5c4113dSnw141292 			}
4457148c5f43SAlan Wright 
4458148c5f43SAlan Wright 			TRACE(req, res, "Matching rule: %s -> %s@%s",
4459148c5f43SAlan Wright 			    values[4] == NULL ? "(null)" : values[4],
4460148c5f43SAlan Wright 			    values[0] == NULL ? "(null)" : values[0],
4461148c5f43SAlan Wright 			    values[1] == NULL ? "(null)" : values[1]);
4462148c5f43SAlan Wright 
4463c5c4113dSnw141292 			if (values[0] == NULL) {
4464c5c4113dSnw141292 				/* values [1] and [2] can be null */
4465c5c4113dSnw141292 				retcode = IDMAP_ERR_INTERNAL;
4466c5c4113dSnw141292 				goto out;
4467c5c4113dSnw141292 			}
446848258c6bSjp151216 
446948258c6bSjp151216 			if (values[2] != NULL)
447048258c6bSjp151216 				direction =
447148258c6bSjp151216 				    (strtol(values[2], &end, 10) == 0)?
447248258c6bSjp151216 				    IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
447348258c6bSjp151216 			else
447448258c6bSjp151216 				direction = IDMAP_DIRECTION_U2W;
447548258c6bSjp151216 
4476c5c4113dSnw141292 			if (EMPTY_NAME(values[0])) {
447748258c6bSjp151216 				idmap_namerule_set(rule, values[1], values[0],
447848258c6bSjp151216 				    values[4], is_user,
447948258c6bSjp151216 				    strtol(values[3], &end, 10),
448048258c6bSjp151216 				    strtol(values[5], &end, 10),
448148258c6bSjp151216 				    direction);
4482148c5f43SAlan Wright 				TRACE(req, res, "Mapping inhibited");
4483c5c4113dSnw141292 				retcode = IDMAP_ERR_NOMAPPING;
4484c5c4113dSnw141292 				goto out;
4485c5c4113dSnw141292 			}
4486cd37da74Snw141292 
4487cd37da74Snw141292 			if (values[0][0] == '*') {
448848258c6bSjp151216 				winname = unixname;
4489cd37da74Snw141292 			} else {
4490cd37da74Snw141292 				winname = values[0];
4491cd37da74Snw141292 			}
4492cb174861Sjoyce mcintosh 
4493148c5f43SAlan Wright 			want_wuser = res->id.idtype == IDMAP_USID ? 1
449448258c6bSjp151216 			    : res->id.idtype == IDMAP_GSID ? 0
449548258c6bSjp151216 			    : -1;
449662c60062Sbaban 			if (values[1] != NULL)
4497c5c4113dSnw141292 				windomain = values[1];
4498148c5f43SAlan Wright 			else if (state->defdom != NULL) {
4499479ac375Sdm199847 				windomain = state->defdom;
4500148c5f43SAlan Wright 				TRACE(req, res,
4501148c5f43SAlan Wright 				    "Added default domain %s to rule",
4502148c5f43SAlan Wright 				    windomain);
4503148c5f43SAlan Wright 			} else {
4504cd37da74Snw141292 				idmapdlog(LOG_ERR, "%s: no domain", me);
4505148c5f43SAlan Wright 				TRACE(req, res,
4506148c5f43SAlan Wright 				    "No domain in rule, and no default domain");
4507c5c4113dSnw141292 				retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
4508c5c4113dSnw141292 				goto out;
4509c5c4113dSnw141292 			}
4510cd37da74Snw141292 
4511479ac375Sdm199847 			retcode = lookup_name2sid(state->cache,
4512479ac375Sdm199847 			    winname, windomain,
4513148c5f43SAlan Wright 			    want_wuser, &canonname, &canondomain,
4514cd37da74Snw141292 			    &res->id.idmap_id_u.sid.prefix,
4515148c5f43SAlan Wright 			    &res->id.idmap_id_u.sid.rid,
4516148c5f43SAlan Wright 			    &res->id.idtype, req, 0);
4517e8c27ec8Sbaban 
4518148c5f43SAlan Wright 			if (retcode == IDMAP_SUCCESS) {
4519148c5f43SAlan Wright 				break;
4520148c5f43SAlan Wright 			} else if (retcode == IDMAP_ERR_NOTFOUND) {
4521cb174861Sjoyce mcintosh 				if (values[0][0] == '*') {
4522148c5f43SAlan Wright 					TRACE(req, res,
4523148c5f43SAlan Wright 					    "%s@%s not found, continuing",
4524148c5f43SAlan Wright 					    winname, windomain);
4525c5c4113dSnw141292 					continue;
4526148c5f43SAlan Wright 				} else {
4527148c5f43SAlan Wright 					TRACE(req, res,
4528cb174861Sjoyce mcintosh 					    "%s@%s not found",
4529cb174861Sjoyce mcintosh 					    winname, windomain);
4530cb174861Sjoyce mcintosh 					retcode = IDMAP_ERR_NOMAPPING;
4531cb174861Sjoyce mcintosh 				}
4532cb174861Sjoyce mcintosh 			} else {
4533cb174861Sjoyce mcintosh 				TRACE(req, res,
4534148c5f43SAlan Wright 				    "Looking up %s@%s error=%d",
4535148c5f43SAlan Wright 				    winname, windomain, retcode);
4536c5c4113dSnw141292 			}
4537148c5f43SAlan Wright 
4538cb174861Sjoyce mcintosh 			idmap_namerule_set(rule, values[1],
4539cb174861Sjoyce mcintosh 			    values[0], values[4], is_user,
4540cb174861Sjoyce mcintosh 			    strtol(values[3], &end, 10),
4541cb174861Sjoyce mcintosh 			    strtol(values[5], &end, 10),
4542cb174861Sjoyce mcintosh 			    direction);
4543cb174861Sjoyce mcintosh 
4544c5c4113dSnw141292 			goto out;
454548258c6bSjp151216 
4546c5c4113dSnw141292 		} else if (r == SQLITE_DONE) {
4547cb174861Sjoyce mcintosh 			TRACE(req, res, "No matching rule");
4548c5c4113dSnw141292 			retcode = IDMAP_ERR_NOTFOUND;
4549c5c4113dSnw141292 			goto out;
4550c5c4113dSnw141292 		} else {
4551c5c4113dSnw141292 			(void) sqlite_finalize(vm, &errmsg);
4552c5c4113dSnw141292 			vm = NULL;
4553cd37da74Snw141292 			idmapdlog(LOG_ERR, "%s: database error (%s)", me,
4554cd37da74Snw141292 			    CHECK_NULL(errmsg));
4555c5c4113dSnw141292 			sqlite_freemem(errmsg);
4556c5c4113dSnw141292 			retcode = IDMAP_ERR_INTERNAL;
4557c5c4113dSnw141292 			goto out;
4558c5c4113dSnw141292 		}
4559c5c4113dSnw141292 	}
4560c5c4113dSnw141292 
456162c60062Sbaban 	if (values[2] != NULL)
4562c5c4113dSnw141292 		res->direction =
4563651c0131Sbaban 		    (strtol(values[2], &end, 10) == 0)?
4564651c0131Sbaban 		    IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
4565c5c4113dSnw141292 	else
4566651c0131Sbaban 		res->direction = IDMAP_DIRECTION_U2W;
45678e228215Sdm199847 
4568cd37da74Snw141292 	req->id2name = canonname;
456908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	req->id2domain = canondomain;
4570479ac375Sdm199847 
457148258c6bSjp151216 	idmap_namerule_set(rule, values[1], values[0], values[4],
457248258c6bSjp151216 	    is_user, strtol(values[3], &end, 10),
457348258c6bSjp151216 	    strtol(values[5], &end, 10),
457448258c6bSjp151216 	    rule->direction);
4575148c5f43SAlan Wright 	TRACE(req, res, "Windows name found");
4576148c5f43SAlan Wright 
4577148c5f43SAlan Wright out:
4578148c5f43SAlan Wright 	if (sql != NULL)
4579148c5f43SAlan Wright 		sqlite_freemem(sql);
4580fc724630SAlan Wright 
4581fc724630SAlan Wright 	if (retcode != IDMAP_ERR_NOTFOUND) {
4582fc724630SAlan Wright 		res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED;
458348258c6bSjp151216 		res->info.src = IDMAP_MAP_SRC_NEW;
4584c5c4113dSnw141292 	}
4585fc724630SAlan Wright 
458662c60062Sbaban 	if (vm != NULL)
4587c5c4113dSnw141292 		(void) sqlite_finalize(vm, NULL);
4588c5c4113dSnw141292 	return (retcode);
4589c5c4113dSnw141292 }
4590c5c4113dSnw141292 
4591cd37da74Snw141292 /*
4592cd37da74Snw141292  * Convention when processing unix2win requests:
4593cd37da74Snw141292  *
4594cd37da74Snw141292  * Unix identity:
4595cd37da74Snw141292  * req->id1name =
4596cd37da74Snw141292  *              unixname if given otherwise unixname found will be placed
4597cd37da74Snw141292  *              here.
4598cd37da74Snw141292  * req->id1domain =
4599cd37da74Snw141292  *              NOT USED
4600cd37da74Snw141292  * req->id1.idtype =
4601cd37da74Snw141292  *              Given type (IDMAP_UID or IDMAP_GID)
4602cd37da74Snw141292  * req->id1..[uid or gid] =
4603cd37da74Snw141292  *              UID/GID if given otherwise UID/GID found will be placed here.
4604cd37da74Snw141292  *
4605cd37da74Snw141292  * Windows identity:
4606cd37da74Snw141292  * req->id2name =
4607cd37da74Snw141292  *              winname found will be placed here.
4608cd37da74Snw141292  * req->id2domain =
4609cd37da74Snw141292  *              windomain found will be placed here.
4610cd37da74Snw141292  * res->id.idtype =
4611cd37da74Snw141292  *              Target type initialized from req->id2.idtype. If
4612cd37da74Snw141292  *              it is IDMAP_SID then actual type (IDMAP_USID/GSID) found
4613cd37da74Snw141292  *              will be placed here.
4614cd37da74Snw141292  * req->id..sid.[prefix, rid] =
4615cd37da74Snw141292  *              SID found will be placed here.
4616cd37da74Snw141292  *
4617cd37da74Snw141292  * Others:
4618cd37da74Snw141292  * res->retcode =
4619cd37da74Snw141292  *              Return status for this request will be placed here.
4620cd37da74Snw141292  * res->direction =
4621cd37da74Snw141292  *              Direction found will be placed here. Direction
4622cd37da74Snw141292  *              meaning whether the resultant mapping is valid
4623cd37da74Snw141292  *              only from unix2win or bi-directional.
4624cd37da74Snw141292  * req->direction =
4625cd37da74Snw141292  *              INTERNAL USE. Used by idmapd to set various
4626cd37da74Snw141292  *              flags (_IDMAP_F_xxxx) to aid in processing
4627cd37da74Snw141292  *              of the request.
4628cd37da74Snw141292  * req->id2.idtype =
4629cd37da74Snw141292  *              INTERNAL USE. Initially this is the requested target
4630cd37da74Snw141292  *              type and is used to initialize res->id.idtype.
4631cd37da74Snw141292  *              ad_lookup_batch() uses this field temporarily to store
4632cd37da74Snw141292  *              sid_type obtained by the batched AD lookups and after
4633cd37da74Snw141292  *              use resets it to IDMAP_NONE to prevent xdr from
4634cd37da74Snw141292  *              mis-interpreting the contents of req->id2.
4635cd37da74Snw141292  * req->id2..[uid or gid or sid] =
4636cd37da74Snw141292  *              NOT USED
4637cd37da74Snw141292  */
4638cd37da74Snw141292 
4639cd37da74Snw141292 /*
4640cd37da74Snw141292  * This function does the following:
4641cd37da74Snw141292  * 1. Lookup well-known SIDs table.
4642cd37da74Snw141292  * 2. Lookup cache.
4643cd37da74Snw141292  * 3. Check if the client does not want new mapping to be allocated
4644cd37da74Snw141292  *    in which case this pass is the final pass.
4645e8c27ec8Sbaban  * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs
4646e8c27ec8Sbaban  *    to do AD/NLDAP lookup.
4647cd37da74Snw141292  */
4648c5c4113dSnw141292 idmap_retcode
pid2sid_first_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res,int is_user)4649479ac375Sdm199847 pid2sid_first_pass(lookup_state_t *state, idmap_mapping *req,
4650fe1c642dSBill Krier 		idmap_id_res *res, int is_user)
4651cd37da74Snw141292 {
4652e8c27ec8Sbaban 	idmap_retcode	retcode;
4653148c5f43SAlan Wright 	idmap_retcode	retcode2;
4654e8c27ec8Sbaban 	bool_t		gen_localsid_on_err = FALSE;
4655c5c4113dSnw141292 
4656e8c27ec8Sbaban 	/* Initialize result */
4657c5c4113dSnw141292 	res->id.idtype = req->id2.idtype;
4658e8c27ec8Sbaban 	res->direction = IDMAP_DIRECTION_UNDEF;
4659c5c4113dSnw141292 
4660e8c27ec8Sbaban 	if (req->id2.idmap_id_u.sid.prefix != NULL) {
4661e8c27ec8Sbaban 		/* sanitize sidprefix */
4662e8c27ec8Sbaban 		free(req->id2.idmap_id_u.sid.prefix);
4663e8c27ec8Sbaban 		req->id2.idmap_id_u.sid.prefix = NULL;
4664e8c27ec8Sbaban 	}
4665e8c27ec8Sbaban 
466648258c6bSjp151216 	/* Find pid */
46679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (req->id1.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
4668fe1c642dSBill Krier 		if (req->id1name == NULL) {
4669fe1c642dSBill Krier 			retcode = IDMAP_ERR_ARG;
4670fe1c642dSBill Krier 			goto out;
4671fe1c642dSBill Krier 		}
4672fe1c642dSBill Krier 
4673148c5f43SAlan Wright 		retcode = ns_lookup_byname(req->id1name, NULL, &req->id1);
4674148c5f43SAlan Wright 		if (retcode != IDMAP_SUCCESS) {
4675148c5f43SAlan Wright 			TRACE(req, res, "Getting UNIX ID error=%d", retcode);
467648258c6bSjp151216 			retcode = IDMAP_ERR_NOMAPPING;
467748258c6bSjp151216 			goto out;
467848258c6bSjp151216 		}
4679148c5f43SAlan Wright 		TRACE(req, res, "Found UNIX ID");
468048258c6bSjp151216 	}
468148258c6bSjp151216 
468208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Lookup in well-known SIDs table */
4683c5c4113dSnw141292 	retcode = lookup_wksids_pid2sid(req, res, is_user);
4684148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
4685148c5f43SAlan Wright 		TRACE(req, res, "Hardwired mapping");
4686c5c4113dSnw141292 		goto out;
4687148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
4688148c5f43SAlan Wright 		TRACE(req, res,
4689148c5f43SAlan Wright 		    "Well-known account lookup error=%d", retcode);
4690148c5f43SAlan Wright 		goto out;
4691148c5f43SAlan Wright 	}
4692c5c4113dSnw141292 
469308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 	/* Lookup in cache */
4694fe1c642dSBill Krier 	retcode = lookup_cache_pid2sid(state->cache, req, res, is_user);
4695148c5f43SAlan Wright 	if (retcode == IDMAP_SUCCESS) {
4696148c5f43SAlan Wright 		TRACE(req, res, "Found in mapping cache");
4697c5c4113dSnw141292 		goto out;
4698148c5f43SAlan Wright 	} else if (retcode != IDMAP_ERR_NOTFOUND) {
4699148c5f43SAlan Wright 		TRACE(req, res,
4700148c5f43SAlan Wright 		    "Mapping cache lookup error=%d", retcode);
4701148c5f43SAlan Wright 		goto out;
4702148c5f43SAlan Wright 	}
4703148c5f43SAlan Wright 	TRACE(req, res, "Not found in mapping cache");
4704c5c4113dSnw141292 
4705c5c4113dSnw141292 	/* Ephemeral ids cannot be allocated during pid2sid */
47069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (IDMAP_ID_IS_EPHEMERAL(req->id1.idmap_id_u.uid)) {
470762c60062Sbaban 		retcode = IDMAP_ERR_NOMAPPING;
4708148c5f43SAlan Wright 		TRACE(req, res, "Shouldn't have an ephemeral ID here");
4709c5c4113dSnw141292 		goto out;
4710c5c4113dSnw141292 	}
4711c5c4113dSnw141292 
471248258c6bSjp151216 	if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) {
47134d61c878SJulian Pullen 		retcode = IDMAP_ERR_NONE_GENERATED;
471448258c6bSjp151216 		goto out;
471548258c6bSjp151216 	}
471648258c6bSjp151216 
471748258c6bSjp151216 	if (AVOID_NAMESERVICE(req)) {
4718e8c27ec8Sbaban 		gen_localsid_on_err = TRUE;
471962c60062Sbaban 		retcode = IDMAP_ERR_NOMAPPING;
4720c5c4113dSnw141292 		goto out;
4721c5c4113dSnw141292 	}
4722c5c4113dSnw141292 
4723e8c27ec8Sbaban 	/* Set flags for the next stage */
4724e3f2c991SKeyur Desai 	if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) {
4725e3f2c991SKeyur Desai 		req->direction |= _IDMAP_F_LOOKUP_AD;
4726e3f2c991SKeyur Desai 		state->ad_nqueries++;
4727e3f2c991SKeyur Desai 	} else if (AD_MODE(req->id1.idtype, state)) {
4728e8c27ec8Sbaban 		/*
4729e8c27ec8Sbaban 		 * If AD-based name mapping is enabled then the next stage
4730e8c27ec8Sbaban 		 * will need to lookup AD using unixname to get the
4731e8c27ec8Sbaban 		 * corresponding winname.
4732e8c27ec8Sbaban 		 */
4733e8c27ec8Sbaban 		if (req->id1name == NULL) {
4734e8c27ec8Sbaban 			/* Get unixname if only pid is given. */
4735e8c27ec8Sbaban 			retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid,
4736e8c27ec8Sbaban 			    is_user, &req->id1name);
4737479ac375Sdm199847 			if (retcode != IDMAP_SUCCESS) {
4738148c5f43SAlan Wright 				TRACE(req, res,
4739148c5f43SAlan Wright 				    "Getting UNIX name error=%d", retcode);
4740479ac375Sdm199847 				gen_localsid_on_err = TRUE;
4741e8c27ec8Sbaban 				goto out;
4742c5c4113dSnw141292 			}
4743148c5f43SAlan Wright 			TRACE(req, res, "Found UNIX name");
4744479ac375Sdm199847 		}
4745e8c27ec8Sbaban 		req->direction |= _IDMAP_F_LOOKUP_AD;
4746e8c27ec8Sbaban 		state->ad_nqueries++;
4747e8c27ec8Sbaban 	} else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) {
4748e8c27ec8Sbaban 		/*
4749e8c27ec8Sbaban 		 * If native LDAP or mixed mode is enabled for name mapping
4750e8c27ec8Sbaban 		 * then the next stage will need to lookup native LDAP using
4751e8c27ec8Sbaban 		 * unixname/pid to get the corresponding winname.
4752e8c27ec8Sbaban 		 */
4753e8c27ec8Sbaban 		req->direction |= _IDMAP_F_LOOKUP_NLDAP;
4754e8c27ec8Sbaban 		state->nldap_nqueries++;
4755c5c4113dSnw141292 	}
4756c5c4113dSnw141292 
4757e8c27ec8Sbaban 	/*
4758e8c27ec8Sbaban 	 * Failed to find non-expired entry in cache. Set the flag to
4759e8c27ec8Sbaban 	 * indicate that we are not done yet.
4760e8c27ec8Sbaban 	 */
4761e8c27ec8Sbaban 	state->pid2sid_done = FALSE;
4762e8c27ec8Sbaban 	req->direction |= _IDMAP_F_NOTDONE;
4763e8c27ec8Sbaban 	retcode = IDMAP_SUCCESS;
4764e8c27ec8Sbaban 
4765e8c27ec8Sbaban out:
4766e8c27ec8Sbaban 	res->retcode = idmap_stat4prot(retcode);
4767148c5f43SAlan Wright 	if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) {
4768148c5f43SAlan Wright 		if (gen_localsid_on_err == TRUE) {
4769148c5f43SAlan Wright 			retcode2 = generate_localsid(req, res, is_user, TRUE);
4770148c5f43SAlan Wright 			if (retcode2 == IDMAP_SUCCESS)
4771148c5f43SAlan Wright 				TRACE(req, res, "Generate local SID");
4772148c5f43SAlan Wright 			else
4773148c5f43SAlan Wright 				TRACE(req, res,
4774148c5f43SAlan Wright 				    "Generate local SID error=%d", retcode2);
4775148c5f43SAlan Wright 		}
4776148c5f43SAlan Wright 	}
4777e8c27ec8Sbaban 	return (retcode);
4778e8c27ec8Sbaban }
4779e8c27ec8Sbaban 
4780e8c27ec8Sbaban idmap_retcode
pid2sid_second_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res,int is_user)4781479ac375Sdm199847 pid2sid_second_pass(lookup_state_t *state, idmap_mapping *req,
4782479ac375Sdm199847 	idmap_id_res *res, int is_user)
4783e8c27ec8Sbaban {
4784e8c27ec8Sbaban 	bool_t		gen_localsid_on_err = TRUE;
4785e8c27ec8Sbaban 	idmap_retcode	retcode = IDMAP_SUCCESS;
4786148c5f43SAlan Wright 	idmap_retcode	retcode2;
4787e8c27ec8Sbaban 
4788e8c27ec8Sbaban 	/* Check if second pass is needed */
4789e8c27ec8Sbaban 	if (ARE_WE_DONE(req->direction))
4790e8c27ec8Sbaban 		return (res->retcode);
4791e8c27ec8Sbaban 
4792e8c27ec8Sbaban 	/* Get status from previous pass */
4793e8c27ec8Sbaban 	retcode = res->retcode;
4794e8c27ec8Sbaban 	if (retcode != IDMAP_SUCCESS)
4795e8c27ec8Sbaban 		goto out;
4796e8c27ec8Sbaban 
4797e8c27ec8Sbaban 	/*
4798e8c27ec8Sbaban 	 * If directory-based name mapping is enabled then the winname
4799e8c27ec8Sbaban 	 * may already have been retrieved from the AD object (AD-mode)
4800479ac375Sdm199847 	 * or from native LDAP object (nldap-mode or mixed-mode).
4801479ac375Sdm199847 	 * Note that if we have winname but no SID then it's an error
4802479ac375Sdm199847 	 * because this implies that the Native LDAP entry contains
4803479ac375Sdm199847 	 * winname which does not exist and it's better that we return
4804479ac375Sdm199847 	 * an error instead of doing rule-based mapping so that the user
4805479ac375Sdm199847 	 * can detect the issue and take appropriate action.
4806e8c27ec8Sbaban 	 */
4807479ac375Sdm199847 	if (req->id2name != NULL) {
4808479ac375Sdm199847 		/* Return notfound if we've winname but no SID. */
4809479ac375Sdm199847 		if (res->id.idmap_id_u.sid.prefix == NULL) {
4810148c5f43SAlan Wright 			TRACE(req, res, "Windows name but no SID");
4811479ac375Sdm199847 			retcode = IDMAP_ERR_NOTFOUND;
4812479ac375Sdm199847 			goto out;
4813479ac375Sdm199847 		}
4814e3f2c991SKeyur Desai 		if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU)
4815e3f2c991SKeyur Desai 			res->direction = IDMAP_DIRECTION_BI;
4816e3f2c991SKeyur Desai 		else if (AD_MODE(req->id1.idtype, state))
4817e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_BI;
4818e8c27ec8Sbaban 		else if (NLDAP_MODE(req->id1.idtype, state))
4819e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_BI;
4820e8c27ec8Sbaban 		else if (MIXED_MODE(req->id1.idtype, state))
4821e8c27ec8Sbaban 			res->direction = IDMAP_DIRECTION_W2U;
4822e8c27ec8Sbaban 		goto out;
4823479ac375Sdm199847 	} else if (res->id.idmap_id_u.sid.prefix != NULL) {
4824479ac375Sdm199847 		/*
4825479ac375Sdm199847 		 * We've SID but no winname. This is fine because
4826479ac375Sdm199847 		 * the caller may have only requested SID.
4827479ac375Sdm199847 		 */
4828479ac375Sdm199847 		goto out;
4829e8c27ec8Sbaban 	}
4830e8c27ec8Sbaban 
4831479ac375Sdm199847 	/* Free any mapping info from Directory based mapping */
4832479ac375Sdm199847 	if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN)
4833148c5f43SAlan Wright 		idmap_how_clear(&res->info.how);
4834479ac375Sdm199847 
4835e8c27ec8Sbaban 	if (req->id1name == NULL) {
4836e8c27ec8Sbaban 		/* Get unixname from name service */
4837e8c27ec8Sbaban 		retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user,
4838e8c27ec8Sbaban 		    &req->id1name);
4839148c5f43SAlan Wright 		if (retcode != IDMAP_SUCCESS) {
4840148c5f43SAlan Wright 			TRACE(req, res,
4841148c5f43SAlan Wright 			    "Getting UNIX name error=%d", retcode);
4842e8c27ec8Sbaban 			goto out;
4843148c5f43SAlan Wright 		}
4844148c5f43SAlan Wright 		TRACE(req, res, "Found UNIX name");
48459fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	} else if (req->id1.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
4846e8c27ec8Sbaban 		/* Get pid from name service */
4847e8c27ec8Sbaban 		retcode = ns_lookup_byname(req->id1name, NULL, &req->id1);
4848e8c27ec8Sbaban 		if (retcode != IDMAP_SUCCESS) {
4849148c5f43SAlan Wright 			TRACE(req, res,
4850148c5f43SAlan Wright 			    "Getting UNIX ID error=%d", retcode);
4851e8c27ec8Sbaban 			gen_localsid_on_err = FALSE;
4852e8c27ec8Sbaban 			goto out;
4853e8c27ec8Sbaban 		}
4854148c5f43SAlan Wright 		TRACE(req, res, "Found UNIX ID");
4855e8c27ec8Sbaban 	}
4856e8c27ec8Sbaban 
4857e8c27ec8Sbaban 	/* Use unixname to evaluate local name-based mapping rules */
4858479ac375Sdm199847 	retcode = name_based_mapping_pid2sid(state, req->id1name, is_user,
4859c5c4113dSnw141292 	    req, res);
4860c5c4113dSnw141292 	if (retcode == IDMAP_ERR_NOTFOUND) {
486148258c6bSjp151216 		retcode = generate_localsid(req, res, is_user, FALSE);
4862148c5f43SAlan Wright 		if (retcode == IDMAP_SUCCESS) {
4863148c5f43SAlan Wright 			TRACE(req, res, "Generated local SID");
4864148c5f43SAlan Wright 		} else {
4865148c5f43SAlan Wright 			TRACE(req, res,
4866148c5f43SAlan Wright 			    "Generating local SID error=%d", retcode);
4867148c5f43SAlan Wright 		}
4868e8c27ec8Sbaban 		gen_localsid_on_err = FALSE;
4869e8c27ec8Sbaban 	}
4870c5c4113dSnw141292 
4871c5c4113dSnw141292 out:
4872c5c4113dSnw141292 	res->retcode = idmap_stat4prot(retcode);
4873e8c27ec8Sbaban 	if (res->retcode != IDMAP_SUCCESS) {
4874e8c27ec8Sbaban 		req->direction = _IDMAP_F_DONE;
4875479ac375Sdm199847 		free(req->id2name);
4876479ac375Sdm199847 		req->id2name = NULL;
4877479ac375Sdm199847 		free(req->id2domain);
4878479ac375Sdm199847 		req->id2domain = NULL;
4879148c5f43SAlan Wright 		if (gen_localsid_on_err == TRUE) {
4880148c5f43SAlan Wright 			retcode2 = generate_localsid(req, res, is_user, TRUE);
4881148c5f43SAlan Wright 			if (retcode2 == IDMAP_SUCCESS)
4882148c5f43SAlan Wright 				TRACE(req, res, "Generate local SID");
4883479ac375Sdm199847 			else
4884148c5f43SAlan Wright 				TRACE(req, res,
4885148c5f43SAlan Wright 				    "Generate local SID error=%d", retcode2);
4886148c5f43SAlan Wright 		} else {
4887479ac375Sdm199847 			res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID;
4888e8c27ec8Sbaban 		}
4889148c5f43SAlan Wright 	}
4890e8c27ec8Sbaban 	if (!ARE_WE_DONE(req->direction))
4891e8c27ec8Sbaban 		state->pid2sid_done = FALSE;
4892c5c4113dSnw141292 	return (retcode);
4893c5c4113dSnw141292 }
48949fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
48959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States idmap_retcode
idmap_cache_flush(idmap_flush_op op)48969fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States idmap_cache_flush(idmap_flush_op op)
48979fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
48989fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	idmap_retcode	rc;
48999fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	sqlite *cache = NULL;
49009fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	char *sql1;
49019fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	char *sql2;
49029fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49039fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	switch (op) {
49049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	case IDMAP_FLUSH_EXPIRE:
49059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		sql1 =
49069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    "UPDATE idmap_cache SET expiration=1 WHERE expiration>0;";
49079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		sql2 =
49089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    "UPDATE name_cache SET expiration=1 WHERE expiration>0;";
49099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		break;
49109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	case IDMAP_FLUSH_DELETE:
49129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		sql1 = "DELETE FROM idmap_cache;";
49139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		sql2 = "DELETE FROM name_cache;";
49149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		break;
49159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	default:
49179fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (IDMAP_ERR_INTERNAL);
49189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
49199fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	rc = get_cache_handle(&cache);
49219fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (rc != IDMAP_SUCCESS)
49229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (rc);
49239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	/*
49259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * Note that we flush the idmapd cache first, before the kernel
49269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * cache.  If we did it the other way 'round, a request could come
49279fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * in after the kernel cache flush and pull a soon-to-be-flushed
49289fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * idmapd cache entry back into the kernel cache.  This way the
49299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * worst that will happen is that a new entry will be added to
49309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 * the kernel cache and then immediately flushed.
49319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	 */
49329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	rc = sql_exec_no_cb(cache, IDMAP_CACHENAME, sql1);
49349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (rc != IDMAP_SUCCESS)
49359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (rc);
49369fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49379fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	rc = sql_exec_no_cb(cache, IDMAP_CACHENAME, sql2);
49389fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
49399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	(void) __idmap_flush_kcache();
49409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	return (rc);
49419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
4942