14520Snw141292 /*
24520Snw141292 * CDDL HEADER START
34520Snw141292 *
44520Snw141292 * The contents of this file are subject to the terms of the
54520Snw141292 * Common Development and Distribution License (the "License").
64520Snw141292 * You may not use this file except in compliance with the License.
74520Snw141292 *
84520Snw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292 * or http://www.opensolaris.org/os/licensing.
104520Snw141292 * See the License for the specific language governing permissions
114520Snw141292 * and limitations under the License.
124520Snw141292 *
134520Snw141292 * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292 * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292 * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292 * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292 *
194520Snw141292 * CDDL HEADER END
204520Snw141292 */
214520Snw141292 /*
2212508Samw@Sun.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
234520Snw141292 */
244520Snw141292
254520Snw141292 /*
264520Snw141292 * Database related utility routines
274520Snw141292 */
284520Snw141292
294520Snw141292 #include <stdio.h>
304520Snw141292 #include <stdlib.h>
314520Snw141292 #include <string.h>
324520Snw141292 #include <errno.h>
334520Snw141292 #include <sys/types.h>
344520Snw141292 #include <sys/stat.h>
354520Snw141292 #include <rpc/rpc.h>
364520Snw141292 #include <sys/sid.h>
374520Snw141292 #include <time.h>
384520Snw141292 #include <pwd.h>
394520Snw141292 #include <grp.h>
404884Sjp151216 #include <pthread.h>
414884Sjp151216 #include <assert.h>
425696Snw141292 #include <sys/u8_textprep.h>
439132SJordan.Brown@Sun.COM #include <alloca.h>
44*12890SJoyce.McIntosh@Sun.COM #include <libuutil.h>
4512508Samw@Sun.COM #include <note.h>
464520Snw141292
474520Snw141292 #include "idmapd.h"
484520Snw141292 #include "adutils.h"
494520Snw141292 #include "string.h"
504520Snw141292 #include "idmap_priv.h"
515696Snw141292 #include "schema.h"
525731Sbaban #include "nldaputils.h"
5312508Samw@Sun.COM #include "idmap_lsa.h"
544520Snw141292
554884Sjp151216
564520Snw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *,
574520Snw141292 sqlite_vm **, int *, int, const char ***);
585731Sbaban static idmap_retcode lookup_localsid2pid(idmap_mapping *, idmap_id_res *);
595731Sbaban static idmap_retcode lookup_cache_name2sid(sqlite *, const char *,
6012508Samw@Sun.COM const char *, char **, char **, idmap_rid_t *, idmap_id_type *);
615731Sbaban
624520Snw141292 #define EMPTY_NAME(name) (*name == 0 || strcmp(name, "\"\"") == 0)
634520Snw141292
644520Snw141292 #define DO_NOT_ALLOC_NEW_ID_MAPPING(req)\
654520Snw141292 (req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC)
664520Snw141292
674520Snw141292 #define AVOID_NAMESERVICE(req)\
684520Snw141292 (req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE)
694520Snw141292
708040SBaban.Kenkre@Sun.COM #define ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)\
718040SBaban.Kenkre@Sun.COM (req->flag & IDMAP_REQ_FLG_WK_OR_LOCAL_SIDS_ONLY)
728040SBaban.Kenkre@Sun.COM
734520Snw141292 typedef enum init_db_option {
744520Snw141292 FAIL_IF_CORRUPT = 0,
754520Snw141292 REMOVE_IF_CORRUPT = 1
764520Snw141292 } init_db_option_t;
774520Snw141292
784884Sjp151216 /*
799422SAfshin.Ardakani@Sun.COM * Thread specific data to hold the database handles so that the
809422SAfshin.Ardakani@Sun.COM * databases are not opened and closed for every request. It also
814884Sjp151216 * contains the sqlite busy handler structure.
824884Sjp151216 */
834884Sjp151216
844884Sjp151216 struct idmap_busy {
854884Sjp151216 const char *name;
864884Sjp151216 const int *delays;
874884Sjp151216 int delay_size;
884884Sjp151216 int total;
894884Sjp151216 int sec;
904884Sjp151216 };
914884Sjp151216
924884Sjp151216
934884Sjp151216 typedef struct idmap_tsd {
944884Sjp151216 sqlite *db_db;
954884Sjp151216 sqlite *cache_db;
964884Sjp151216 struct idmap_busy cache_busy;
974884Sjp151216 struct idmap_busy db_busy;
984884Sjp151216 } idmap_tsd_t;
994884Sjp151216
10010504SKeyur.Desai@Sun.COM /*
10110504SKeyur.Desai@Sun.COM * Flags to indicate how local the directory we're consulting is.
10210504SKeyur.Desai@Sun.COM * If neither is set, it means the directory belongs to a remote forest.
10310504SKeyur.Desai@Sun.COM */
10410504SKeyur.Desai@Sun.COM #define DOMAIN_IS_LOCAL 0x01
10510504SKeyur.Desai@Sun.COM #define FOREST_IS_LOCAL 0x02
1064884Sjp151216
1074884Sjp151216 static const int cache_delay_table[] =
1084884Sjp151216 { 1, 2, 5, 10, 15, 20, 25, 30, 35, 40,
1094884Sjp151216 50, 50, 60, 70, 80, 90, 100};
1104884Sjp151216
1114884Sjp151216 static const int db_delay_table[] =
1124884Sjp151216 { 5, 10, 15, 20, 30, 40, 55, 70, 100};
1134884Sjp151216
1144884Sjp151216
1154884Sjp151216 static pthread_key_t idmap_tsd_key;
1164884Sjp151216
1174884Sjp151216 void
idmap_tsd_destroy(void * key)1184884Sjp151216 idmap_tsd_destroy(void *key)
1194884Sjp151216 {
1204884Sjp151216
1214884Sjp151216 idmap_tsd_t *tsd = (idmap_tsd_t *)key;
1224884Sjp151216 if (tsd) {
1234884Sjp151216 if (tsd->db_db)
1244884Sjp151216 (void) sqlite_close(tsd->db_db);
1254884Sjp151216 if (tsd->cache_db)
1264884Sjp151216 (void) sqlite_close(tsd->cache_db);
1274884Sjp151216 free(tsd);
1284884Sjp151216 }
1294884Sjp151216 }
1304884Sjp151216
13112508Samw@Sun.COM void
idmap_init_tsd_key(void)1325696Snw141292 idmap_init_tsd_key(void)
1335696Snw141292 {
13412508Samw@Sun.COM int rc;
13512508Samw@Sun.COM
13612508Samw@Sun.COM rc = pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy);
13712508Samw@Sun.COM assert(rc == 0);
1384884Sjp151216 }
1394884Sjp151216
1404884Sjp151216
1414884Sjp151216
1424884Sjp151216 idmap_tsd_t *
idmap_get_tsd(void)1434884Sjp151216 idmap_get_tsd(void)
1444884Sjp151216 {
1454884Sjp151216 idmap_tsd_t *tsd;
1464884Sjp151216
1474884Sjp151216 if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) {
1484884Sjp151216 /* No thread specific data so create it */
1494884Sjp151216 if ((tsd = malloc(sizeof (*tsd))) != NULL) {
1504884Sjp151216 /* Initialize thread specific data */
1514884Sjp151216 (void) memset(tsd, 0, sizeof (*tsd));
1524884Sjp151216 /* save the trhread specific data */
1534884Sjp151216 if (pthread_setspecific(idmap_tsd_key, tsd) != 0) {
1544884Sjp151216 /* Can't store key */
1554884Sjp151216 free(tsd);
1564884Sjp151216 tsd = NULL;
1574884Sjp151216 }
1584884Sjp151216 } else {
1594884Sjp151216 tsd = NULL;
1604884Sjp151216 }
1614884Sjp151216 }
1624884Sjp151216
1634884Sjp151216 return (tsd);
1644884Sjp151216 }
1654884Sjp151216
1665696Snw141292 /*
1675696Snw141292 * A simple wrapper around u8_textprep_str() that returns the Unicode
1685696Snw141292 * lower-case version of some string. The result must be freed.
1695696Snw141292 */
1705696Snw141292 char *
tolower_u8(const char * s)1715696Snw141292 tolower_u8(const char *s)
1725696Snw141292 {
1735696Snw141292 char *res = NULL;
1745696Snw141292 char *outs;
1755696Snw141292 size_t inlen, outlen, inbytesleft, outbytesleft;
1765696Snw141292 int rc, err;
1775696Snw141292
1785696Snw141292 /*
1795696Snw141292 * u8_textprep_str() does not allocate memory. The input and
1805696Snw141292 * output buffers may differ in size (though that would be more
1815696Snw141292 * likely when normalization is done). We have to loop over it...
1825696Snw141292 *
1835696Snw141292 * To improve the chances that we can avoid looping we add 10
1845696Snw141292 * bytes of output buffer room the first go around.
1855696Snw141292 */
1865696Snw141292 inlen = inbytesleft = strlen(s);
1875696Snw141292 outlen = outbytesleft = inlen + 10;
1885696Snw141292 if ((res = malloc(outlen)) == NULL)
1895696Snw141292 return (NULL);
1905696Snw141292 outs = res;
1915696Snw141292
1925696Snw141292 while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs,
1935696Snw141292 &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 &&
1945696Snw141292 err == E2BIG) {
1955696Snw141292 if ((res = realloc(res, outlen + inbytesleft)) == NULL)
1965696Snw141292 return (NULL);
1975696Snw141292 /* adjust input/output buffer pointers */
1985696Snw141292 s += (inlen - inbytesleft);
1995696Snw141292 outs = res + outlen - outbytesleft;
2005696Snw141292 /* adjust outbytesleft and outlen */
2015696Snw141292 outlen += inbytesleft;
2025696Snw141292 outbytesleft += inbytesleft;
2035696Snw141292 }
2045696Snw141292
2055696Snw141292 if (rc < 0) {
2065696Snw141292 free(res);
2075696Snw141292 res = NULL;
2085696Snw141292 return (NULL);
2095696Snw141292 }
2105696Snw141292
2115696Snw141292 res[outlen - outbytesleft] = '\0';
2125696Snw141292
2135696Snw141292 return (res);
2145696Snw141292 }
2155696Snw141292
2165696Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname,
2175696Snw141292 const char *while_doing);
2185696Snw141292
2194520Snw141292
2204520Snw141292 /*
2214520Snw141292 * Initialize 'dbname' using 'sql'
2224520Snw141292 */
2235696Snw141292 static
2245696Snw141292 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)2255696Snw141292 init_db_instance(const char *dbname, int version,
2265696Snw141292 const char *detect_version_sql, char * const *sql,
2275696Snw141292 init_db_option_t opt, int *created, int *upgraded)
2284520Snw141292 {
2295696Snw141292 int rc, curr_version;
2305696Snw141292 int tries = 1;
2315696Snw141292 int prio = LOG_NOTICE;
2324520Snw141292 sqlite *db = NULL;
2335696Snw141292 char *errmsg = NULL;
2345696Snw141292
2355696Snw141292 *created = 0;
2365696Snw141292 *upgraded = 0;
2375696Snw141292
2385696Snw141292 if (opt == REMOVE_IF_CORRUPT)
2395696Snw141292 tries = 3;
2405696Snw141292
2415696Snw141292 rinse_repeat:
2425696Snw141292 if (tries == 0) {
2435696Snw141292 idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname);
2445696Snw141292 return (-1);
2455696Snw141292 }
2465696Snw141292 if (tries-- == 1)
2475696Snw141292 /* Last try, log errors */
2485696Snw141292 prio = LOG_ERR;
2495696Snw141292
2505696Snw141292 db = sqlite_open(dbname, 0600, &errmsg);
2515696Snw141292 if (db == NULL) {
2525696Snw141292 idmapdlog(prio, "Error creating database %s (%s)",
2535696Snw141292 dbname, CHECK_NULL(errmsg));
2545696Snw141292 sqlite_freemem(errmsg);
2555696Snw141292 if (opt == REMOVE_IF_CORRUPT)
2565696Snw141292 (void) unlink(dbname);
2575696Snw141292 goto rinse_repeat;
2584520Snw141292 }
2594520Snw141292
2604520Snw141292 sqlite_busy_timeout(db, 3000);
2615696Snw141292
2625696Snw141292 /* Detect current version of schema in the db, if any */
2635696Snw141292 curr_version = 0;
2645696Snw141292 if (detect_version_sql != NULL) {
2655696Snw141292 char *end, **results;
2665696Snw141292 int nrow;
2675696Snw141292
2685696Snw141292 #ifdef IDMAPD_DEBUG
2695696Snw141292 (void) fprintf(stderr, "Schema version detection SQL: %s\n",
2705696Snw141292 detect_version_sql);
2715696Snw141292 #endif /* IDMAPD_DEBUG */
2725696Snw141292 rc = sqlite_get_table(db, detect_version_sql, &results,
2735696Snw141292 &nrow, NULL, &errmsg);
2745696Snw141292 if (rc != SQLITE_OK) {
2755696Snw141292 idmapdlog(prio,
2765696Snw141292 "Error detecting schema version of db %s (%s)",
2775696Snw141292 dbname, errmsg);
2785696Snw141292 sqlite_freemem(errmsg);
2795696Snw141292 sqlite_free_table(results);
2805696Snw141292 sqlite_close(db);
2815696Snw141292 return (-1);
2825696Snw141292 }
2835696Snw141292 if (nrow != 1) {
2845696Snw141292 idmapdlog(prio,
2855696Snw141292 "Error detecting schema version of db %s", dbname);
2865696Snw141292 sqlite_close(db);
2875696Snw141292 sqlite_free_table(results);
2885696Snw141292 return (-1);
2895696Snw141292 }
2905696Snw141292 curr_version = strtol(results[1], &end, 10);
2915696Snw141292 sqlite_free_table(results);
2924520Snw141292 }
2934520Snw141292
2945696Snw141292 if (curr_version < 0) {
2955696Snw141292 if (opt == REMOVE_IF_CORRUPT)
2965696Snw141292 (void) unlink(dbname);
2975696Snw141292 goto rinse_repeat;
2984520Snw141292 }
2994520Snw141292
3005696Snw141292 if (curr_version == version)
3015696Snw141292 goto done;
3025696Snw141292
3035696Snw141292 /* Install or upgrade schema */
3045696Snw141292 #ifdef IDMAPD_DEBUG
3055696Snw141292 (void) fprintf(stderr, "Schema init/upgrade SQL: %s\n",
3065696Snw141292 sql[curr_version]);
3075696Snw141292 #endif /* IDMAPD_DEBUG */
3085696Snw141292 rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname,
3095696Snw141292 (curr_version == 0) ? "installing schema" : "upgrading schema");
3105696Snw141292 if (rc != 0) {
3115696Snw141292 idmapdlog(prio, "Error %s schema for db %s", dbname,
3125696Snw141292 (curr_version == 0) ? "installing schema" :
3135696Snw141292 "upgrading schema");
3145696Snw141292 if (opt == REMOVE_IF_CORRUPT)
3155696Snw141292 (void) unlink(dbname);
3165696Snw141292 goto rinse_repeat;
3174520Snw141292 }
3184520Snw141292
3195696Snw141292 *upgraded = (curr_version > 0);
3205696Snw141292 *created = (curr_version == 0);
3215696Snw141292
3225696Snw141292 done:
3234520Snw141292 (void) sqlite_close(db);
3245696Snw141292 return (0);
3254520Snw141292 }
3264520Snw141292
3274884Sjp151216
3284884Sjp151216 /*
3294884Sjp151216 * This is the SQLite database busy handler that retries the SQL
3304884Sjp151216 * operation until it is successful.
3314884Sjp151216 */
3324884Sjp151216 int
3334884Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */
idmap_sqlite_busy_handler(void * arg,const char * table_name,int count)3344884Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count)
3354884Sjp151216 {
3364884Sjp151216 struct idmap_busy *busy = arg;
3374884Sjp151216 int delay;
3384884Sjp151216 struct timespec rqtp;
3394884Sjp151216
3404884Sjp151216 if (count == 1) {
3414884Sjp151216 busy->total = 0;
3424884Sjp151216 busy->sec = 2;
3434884Sjp151216 }
3444884Sjp151216 if (busy->total > 1000 * busy->sec) {
3456414Sjp151216 idmapdlog(LOG_DEBUG,
3464884Sjp151216 "Thread %d waited %d sec for the %s database",
3474884Sjp151216 pthread_self(), busy->sec, busy->name);
3484884Sjp151216 busy->sec++;
3494884Sjp151216 }
3504884Sjp151216
3514884Sjp151216 if (count <= busy->delay_size) {
3524884Sjp151216 delay = busy->delays[count-1];
3534884Sjp151216 } else {
3544884Sjp151216 delay = busy->delays[busy->delay_size - 1];
3554884Sjp151216 }
3564884Sjp151216 busy->total += delay;
3574884Sjp151216 rqtp.tv_sec = 0;
3584884Sjp151216 rqtp.tv_nsec = delay * (NANOSEC / MILLISEC);
3594884Sjp151216 (void) nanosleep(&rqtp, NULL);
3604884Sjp151216 return (1);
3614884Sjp151216 }
3624884Sjp151216
3634884Sjp151216
3644520Snw141292 /*
3654520Snw141292 * Get the database handle
3664520Snw141292 */
3674520Snw141292 idmap_retcode
get_db_handle(sqlite ** db)3685696Snw141292 get_db_handle(sqlite **db)
3695696Snw141292 {
3705696Snw141292 char *errmsg;
3715696Snw141292 idmap_tsd_t *tsd;
3724520Snw141292
3734520Snw141292 /*
3744884Sjp151216 * Retrieve the db handle from thread-specific storage
3754520Snw141292 * If none exists, open and store in thread-specific storage.
3764520Snw141292 */
3774884Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) {
3784520Snw141292 idmapdlog(LOG_ERR,
3795696Snw141292 "Error getting thread specific data for %s", IDMAP_DBNAME);
3804884Sjp151216 return (IDMAP_ERR_MEMORY);
3814520Snw141292 }
3824884Sjp151216
3834884Sjp151216 if (tsd->db_db == NULL) {
3844884Sjp151216 tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg);
3854884Sjp151216 if (tsd->db_db == NULL) {
3865696Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)",
3875696Snw141292 IDMAP_DBNAME, CHECK_NULL(errmsg));
3884884Sjp151216 sqlite_freemem(errmsg);
3895696Snw141292 return (IDMAP_ERR_DB);
3904884Sjp151216 }
3915696Snw141292
3924884Sjp151216 tsd->db_busy.name = IDMAP_DBNAME;
3934884Sjp151216 tsd->db_busy.delays = db_delay_table;
3944884Sjp151216 tsd->db_busy.delay_size = sizeof (db_delay_table) /
3954884Sjp151216 sizeof (int);
3964884Sjp151216 sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler,
3974884Sjp151216 &tsd->db_busy);
3984884Sjp151216 }
3994884Sjp151216 *db = tsd->db_db;
4004520Snw141292 return (IDMAP_SUCCESS);
4014520Snw141292 }
4024520Snw141292
4034520Snw141292 /*
4044520Snw141292 * Get the cache handle
4054520Snw141292 */
4064520Snw141292 idmap_retcode
get_cache_handle(sqlite ** cache)4075696Snw141292 get_cache_handle(sqlite **cache)
4085696Snw141292 {
4095696Snw141292 char *errmsg;
4105696Snw141292 idmap_tsd_t *tsd;
4114520Snw141292
4124520Snw141292 /*
4134884Sjp151216 * Retrieve the db handle from thread-specific storage
4144520Snw141292 * If none exists, open and store in thread-specific storage.
4154520Snw141292 */
4164884Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) {
4175696Snw141292 idmapdlog(LOG_ERR, "Error getting thread specific data for %s",
4185696Snw141292 IDMAP_DBNAME);
4194884Sjp151216 return (IDMAP_ERR_MEMORY);
4204520Snw141292 }
4214884Sjp151216
4224884Sjp151216 if (tsd->cache_db == NULL) {
4234884Sjp151216 tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg);
4244884Sjp151216 if (tsd->cache_db == NULL) {
4255696Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)",
4265696Snw141292 IDMAP_CACHENAME, CHECK_NULL(errmsg));
4274884Sjp151216 sqlite_freemem(errmsg);
4285696Snw141292 return (IDMAP_ERR_DB);
4294884Sjp151216 }
4305696Snw141292
4314884Sjp151216 tsd->cache_busy.name = IDMAP_CACHENAME;
4324884Sjp151216 tsd->cache_busy.delays = cache_delay_table;
4334884Sjp151216 tsd->cache_busy.delay_size = sizeof (cache_delay_table) /
4344884Sjp151216 sizeof (int);
4354884Sjp151216 sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler,
4364884Sjp151216 &tsd->cache_busy);
4374884Sjp151216 }
4384884Sjp151216 *cache = tsd->cache_db;
4394520Snw141292 return (IDMAP_SUCCESS);
4404520Snw141292 }
4414520Snw141292
4424520Snw141292 /*
4434520Snw141292 * Initialize cache and db
4444520Snw141292 */
4454520Snw141292 int
init_dbs()4465696Snw141292 init_dbs()
4475696Snw141292 {
4486386Sjp151216 char *sql[4];
4495696Snw141292 int created, upgraded;
4505696Snw141292
4514520Snw141292 /* name-based mappings; probably OK to blow away in a pinch(?) */
4525696Snw141292 sql[0] = DB_INSTALL_SQL;
4535696Snw141292 sql[1] = DB_UPGRADE_FROM_v1_SQL;
4546386Sjp151216 sql[2] = NULL;
4555696Snw141292
4565696Snw141292 if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql,
4575696Snw141292 FAIL_IF_CORRUPT, &created, &upgraded) < 0)
4584520Snw141292 return (-1);
4594520Snw141292
4604520Snw141292 /* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */
4615696Snw141292 sql[0] = CACHE_INSTALL_SQL;
4625696Snw141292 sql[1] = CACHE_UPGRADE_FROM_v1_SQL;
4636386Sjp151216 sql[2] = CACHE_UPGRADE_FROM_v2_SQL;
4646386Sjp151216 sql[3] = NULL;
4656386Sjp151216
4665696Snw141292 if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL,
4675696Snw141292 sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0)
4684520Snw141292 return (-1);
4694520Snw141292
4705696Snw141292 _idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0;
4715696Snw141292
4724520Snw141292 return (0);
4734520Snw141292 }
4744520Snw141292
4754520Snw141292 /*
4764520Snw141292 * Finalize databases
4774520Snw141292 */
4784520Snw141292 void
fini_dbs()4795696Snw141292 fini_dbs()
4805696Snw141292 {
4814520Snw141292 }
4824520Snw141292
4834520Snw141292 /*
4845731Sbaban * This table is a listing of status codes that will be returned to the
4854520Snw141292 * client when a SQL command fails with the corresponding error message.
4864520Snw141292 */
4874520Snw141292 static msg_table_t sqlmsgtable[] = {
4884864Sbaban {IDMAP_ERR_U2W_NAMERULE_CONFLICT,
4894520Snw141292 "columns unixname, is_user, u2w_order are not unique"},
4904864Sbaban {IDMAP_ERR_W2U_NAMERULE_CONFLICT,
4915696Snw141292 "columns winname, windomain, is_user, is_wuser, w2u_order are not"
4925696Snw141292 " unique"},
4935696Snw141292 {IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"},
4944520Snw141292 {-1, NULL}
4954520Snw141292 };
4964520Snw141292
4974520Snw141292 /*
4984520Snw141292 * idmapd's version of string2stat to map SQLite messages to
4994520Snw141292 * status codes
5004520Snw141292 */
5014520Snw141292 idmap_retcode
idmapd_string2stat(const char * msg)5025696Snw141292 idmapd_string2stat(const char *msg)
5035696Snw141292 {
5044520Snw141292 int i;
5054520Snw141292 for (i = 0; sqlmsgtable[i].msg; i++) {
5064520Snw141292 if (strcasecmp(sqlmsgtable[i].msg, msg) == 0)
5074520Snw141292 return (sqlmsgtable[i].retcode);
5084520Snw141292 }
5094520Snw141292 return (IDMAP_ERR_OTHER);
5104520Snw141292 }
5114520Snw141292
5124520Snw141292 /*
5135696Snw141292 * Executes some SQL in a transaction.
5145696Snw141292 *
5155696Snw141292 * Returns 0 on success, -1 if it failed but the rollback succeeded, -2
5165696Snw141292 * if the rollback failed.
5175696Snw141292 */
5185696Snw141292 static
5195696Snw141292 int
sql_exec_tran_no_cb(sqlite * db,char * sql,const char * dbname,const char * while_doing)5205696Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname,
5215696Snw141292 const char *while_doing)
5225696Snw141292 {
5235696Snw141292 char *errmsg = NULL;
5245696Snw141292 int rc;
5255696Snw141292
5265696Snw141292 rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg);
5275696Snw141292 if (rc != SQLITE_OK) {
5285696Snw141292 idmapdlog(LOG_ERR, "Begin transaction failed (%s) "
5295696Snw141292 "while %s (%s)", errmsg, while_doing, dbname);
5305696Snw141292 sqlite_freemem(errmsg);
5315696Snw141292 return (-1);
5325696Snw141292 }
5335696Snw141292
5345696Snw141292 rc = sqlite_exec(db, sql, NULL, NULL, &errmsg);
5355696Snw141292 if (rc != SQLITE_OK) {
5365696Snw141292 idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg,
5375696Snw141292 while_doing, dbname);
5385696Snw141292 sqlite_freemem(errmsg);
5395696Snw141292 errmsg = NULL;
5405696Snw141292 goto rollback;
5415696Snw141292 }
5425696Snw141292
5435696Snw141292 rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg);
5445696Snw141292 if (rc == SQLITE_OK) {
5455696Snw141292 sqlite_freemem(errmsg);
5465696Snw141292 return (0);
5475696Snw141292 }
5485696Snw141292
5495696Snw141292 idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)",
5505696Snw141292 errmsg, while_doing, dbname);
5515696Snw141292 sqlite_freemem(errmsg);
5525696Snw141292 errmsg = NULL;
5535696Snw141292
5545696Snw141292 rollback:
5555696Snw141292 rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg);
5565696Snw141292 if (rc != SQLITE_OK) {
5575696Snw141292 idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)",
5585696Snw141292 errmsg, while_doing, dbname);
5595696Snw141292 sqlite_freemem(errmsg);
5605696Snw141292 return (-2);
5615696Snw141292 }
5625696Snw141292 sqlite_freemem(errmsg);
5635696Snw141292
5645696Snw141292 return (-1);
5655696Snw141292 }
5665696Snw141292
5675696Snw141292 /*
5684520Snw141292 * Execute the given SQL statment without using any callbacks
5694520Snw141292 */
5704520Snw141292 idmap_retcode
sql_exec_no_cb(sqlite * db,const char * dbname,char * sql)5716017Snw141292 sql_exec_no_cb(sqlite *db, const char *dbname, char *sql)
5725696Snw141292 {
5734520Snw141292 char *errmsg = NULL;
5744884Sjp151216 int r;
5754520Snw141292 idmap_retcode retcode;
5764520Snw141292
5774884Sjp151216 r = sqlite_exec(db, sql, NULL, NULL, &errmsg);
5784884Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
5794520Snw141292
5804520Snw141292 if (r != SQLITE_OK) {
5816017Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing %s "
5826017Snw141292 "(%s)", dbname, sql, CHECK_NULL(errmsg));
5834884Sjp151216 retcode = idmapd_string2stat(errmsg);
5844864Sbaban if (errmsg != NULL)
5854520Snw141292 sqlite_freemem(errmsg);
5864520Snw141292 return (retcode);
5874520Snw141292 }
5884520Snw141292
5894520Snw141292 return (IDMAP_SUCCESS);
5904520Snw141292 }
5914520Snw141292
5924520Snw141292 /*
5934520Snw141292 * Generate expression that can be used in WHERE statements.
5944520Snw141292 * Examples:
5954520Snw141292 * <prefix> <col> <op> <value> <suffix>
5964520Snw141292 * "" "unixuser" "=" "foo" "AND"
5974520Snw141292 */
5984520Snw141292 idmap_retcode
gen_sql_expr_from_rule(idmap_namerule * rule,char ** out)5995696Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out)
6005696Snw141292 {
6015696Snw141292 char *s_windomain = NULL, *s_winname = NULL;
6025696Snw141292 char *s_unixname = NULL;
60310966SJordan.Brown@Sun.COM char *dir;
6045696Snw141292 char *lower_winname;
6055696Snw141292 int retcode = IDMAP_SUCCESS;
6065696Snw141292
6074520Snw141292 if (out == NULL)
6084520Snw141292 return (IDMAP_ERR_ARG);
6094520Snw141292
6105696Snw141292
6115696Snw141292 if (!EMPTY_STRING(rule->windomain)) {
6125696Snw141292 s_windomain = sqlite_mprintf("AND windomain = %Q ",
6135696Snw141292 rule->windomain);
6145696Snw141292 if (s_windomain == NULL) {
6155696Snw141292 retcode = IDMAP_ERR_MEMORY;
6165696Snw141292 goto out;
6175696Snw141292 }
6185696Snw141292 }
6195696Snw141292
6205696Snw141292 if (!EMPTY_STRING(rule->winname)) {
6215696Snw141292 if ((lower_winname = tolower_u8(rule->winname)) == NULL)
6225696Snw141292 lower_winname = rule->winname;
6235696Snw141292 s_winname = sqlite_mprintf(
6245696Snw141292 "AND winname = %Q AND is_wuser = %d ",
6255696Snw141292 lower_winname, rule->is_wuser ? 1 : 0);
6265696Snw141292 if (lower_winname != rule->winname)
6275696Snw141292 free(lower_winname);
6285696Snw141292 if (s_winname == NULL) {
6295696Snw141292 retcode = IDMAP_ERR_MEMORY;
6305696Snw141292 goto out;
6315696Snw141292 }
6325696Snw141292 }
6335696Snw141292
6345696Snw141292 if (!EMPTY_STRING(rule->unixname)) {
6355696Snw141292 s_unixname = sqlite_mprintf(
6365696Snw141292 "AND unixname = %Q AND is_user = %d ",
6375696Snw141292 rule->unixname, rule->is_user ? 1 : 0);
6385696Snw141292 if (s_unixname == NULL) {
6395696Snw141292 retcode = IDMAP_ERR_MEMORY;
6405696Snw141292 goto out;
6415696Snw141292 }
6425696Snw141292 }
6435696Snw141292
64410966SJordan.Brown@Sun.COM switch (rule->direction) {
64510966SJordan.Brown@Sun.COM case IDMAP_DIRECTION_BI:
64610966SJordan.Brown@Sun.COM dir = "AND w2u_order > 0 AND u2w_order > 0";
64710966SJordan.Brown@Sun.COM break;
64810966SJordan.Brown@Sun.COM case IDMAP_DIRECTION_W2U:
64910966SJordan.Brown@Sun.COM dir = "AND w2u_order > 0"
65010966SJordan.Brown@Sun.COM " AND (u2w_order = 0 OR u2w_order ISNULL)";
65110966SJordan.Brown@Sun.COM break;
65210966SJordan.Brown@Sun.COM case IDMAP_DIRECTION_U2W:
65310966SJordan.Brown@Sun.COM dir = "AND u2w_order > 0"
65410966SJordan.Brown@Sun.COM " AND (w2u_order = 0 OR w2u_order ISNULL)";
65510966SJordan.Brown@Sun.COM break;
65610966SJordan.Brown@Sun.COM default:
65710966SJordan.Brown@Sun.COM dir = "";
65810966SJordan.Brown@Sun.COM break;
65910966SJordan.Brown@Sun.COM }
66010966SJordan.Brown@Sun.COM
66110966SJordan.Brown@Sun.COM *out = sqlite_mprintf("%s %s %s %s",
6625696Snw141292 s_windomain ? s_windomain : "",
6635696Snw141292 s_winname ? s_winname : "",
66410966SJordan.Brown@Sun.COM s_unixname ? s_unixname : "",
66510966SJordan.Brown@Sun.COM dir);
6665696Snw141292
6675696Snw141292 if (*out == NULL) {
6685696Snw141292 retcode = IDMAP_ERR_MEMORY;
6695696Snw141292 idmapdlog(LOG_ERR, "Out of memory");
6705696Snw141292 goto out;
6715696Snw141292 }
6725696Snw141292
6735696Snw141292 out:
6745696Snw141292 if (s_windomain != NULL)
6755696Snw141292 sqlite_freemem(s_windomain);
6765696Snw141292 if (s_winname != NULL)
6775696Snw141292 sqlite_freemem(s_winname);
6785696Snw141292 if (s_unixname != NULL)
6795696Snw141292 sqlite_freemem(s_unixname);
6805696Snw141292
6815696Snw141292 return (retcode);
6824520Snw141292 }
6834520Snw141292
6845696Snw141292
6855696Snw141292
6864520Snw141292 /*
6874520Snw141292 * Generate and execute SQL statement for LIST RPC calls
6884520Snw141292 */
6894520Snw141292 idmap_retcode
process_list_svc_sql(sqlite * db,const char * dbname,char * sql,uint64_t limit,int flag,list_svc_cb cb,void * result)6906017Snw141292 process_list_svc_sql(sqlite *db, const char *dbname, char *sql, uint64_t limit,
6916386Sjp151216 int flag, list_svc_cb cb, void *result)
6925696Snw141292 {
6934520Snw141292 list_cb_data_t cb_data;
6944520Snw141292 char *errmsg = NULL;
6954884Sjp151216 int r;
6964520Snw141292 idmap_retcode retcode = IDMAP_ERR_INTERNAL;
6974520Snw141292
6984520Snw141292 (void) memset(&cb_data, 0, sizeof (cb_data));
6994520Snw141292 cb_data.result = result;
7004520Snw141292 cb_data.limit = limit;
7016386Sjp151216 cb_data.flag = flag;
7024520Snw141292
7034884Sjp151216
7044884Sjp151216 r = sqlite_exec(db, sql, cb, &cb_data, &errmsg);
7054884Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
7064884Sjp151216 switch (r) {
7074884Sjp151216 case SQLITE_OK:
7084884Sjp151216 retcode = IDMAP_SUCCESS;
7094884Sjp151216 break;
7104884Sjp151216
7114884Sjp151216 default:
7124884Sjp151216 retcode = IDMAP_ERR_INTERNAL;
7136017Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing "
7146017Snw141292 "%s (%s)", dbname, sql, CHECK_NULL(errmsg));
7154884Sjp151216 break;
7164520Snw141292 }
7174864Sbaban if (errmsg != NULL)
7184520Snw141292 sqlite_freemem(errmsg);
7194520Snw141292 return (retcode);
7204520Snw141292 }
7214520Snw141292
7224520Snw141292 /*
7234520Snw141292 * This routine is called by callbacks that process the results of
7244520Snw141292 * LIST RPC calls to validate data and to allocate memory for
7254520Snw141292 * the result array.
7264520Snw141292 */
7274520Snw141292 idmap_retcode
validate_list_cb_data(list_cb_data_t * cb_data,int argc,char ** argv,int ncol,uchar_t ** list,size_t valsize)7284520Snw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv,
7295696Snw141292 int ncol, uchar_t **list, size_t valsize)
7305696Snw141292 {
7314520Snw141292 size_t nsize;
7324520Snw141292 void *tmplist;
7334520Snw141292
7344520Snw141292 if (cb_data->limit > 0 && cb_data->next == cb_data->limit)
7354520Snw141292 return (IDMAP_NEXT);
7364520Snw141292
7374520Snw141292 if (argc < ncol || argv == NULL) {
7384520Snw141292 idmapdlog(LOG_ERR, "Invalid data");
7394520Snw141292 return (IDMAP_ERR_INTERNAL);
7404520Snw141292 }
7414520Snw141292
7424520Snw141292 /* alloc in bulk to reduce number of reallocs */
7434520Snw141292 if (cb_data->next >= cb_data->len) {
7444520Snw141292 nsize = (cb_data->len + SIZE_INCR) * valsize;
7454520Snw141292 tmplist = realloc(*list, nsize);
7464520Snw141292 if (tmplist == NULL) {
7474520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
7484520Snw141292 return (IDMAP_ERR_MEMORY);
7494520Snw141292 }
7504520Snw141292 *list = tmplist;
7514520Snw141292 (void) memset(*list + (cb_data->len * valsize), 0,
7525696Snw141292 SIZE_INCR * valsize);
7534520Snw141292 cb_data->len += SIZE_INCR;
7544520Snw141292 }
7554520Snw141292 return (IDMAP_SUCCESS);
7564520Snw141292 }
7574520Snw141292
7585696Snw141292 static
7595696Snw141292 idmap_retcode
get_namerule_order(char * winname,char * windomain,char * unixname,int direction,int is_diagonal,int * w2u_order,int * u2w_order)7604520Snw141292 get_namerule_order(char *winname, char *windomain, char *unixname,
7615696Snw141292 int direction, int is_diagonal, int *w2u_order, int *u2w_order)
7625696Snw141292 {
7634520Snw141292 *w2u_order = 0;
7644520Snw141292 *u2w_order = 0;
7654520Snw141292
7664520Snw141292 /*
7674520Snw141292 * Windows to UNIX lookup order:
7684520Snw141292 * 1. winname@domain (or winname) to ""
7694520Snw141292 * 2. winname@domain (or winname) to unixname
7704520Snw141292 * 3. winname@* to ""
7714520Snw141292 * 4. winname@* to unixname
7724520Snw141292 * 5. *@domain (or *) to *
7734520Snw141292 * 6. *@domain (or *) to ""
7744520Snw141292 * 7. *@domain (or *) to unixname
7754520Snw141292 * 8. *@* to *
7764520Snw141292 * 9. *@* to ""
7774520Snw141292 * 10. *@* to unixname
7784520Snw141292 *
7794520Snw141292 * winname is a special case of winname@domain when domain is the
7804520Snw141292 * default domain. Similarly * is a special case of *@domain when
7814520Snw141292 * domain is the default domain.
7824520Snw141292 *
7834520Snw141292 * Note that "" has priority over specific names because "" inhibits
7844520Snw141292 * mappings and traditionally deny rules always had higher priority.
7854520Snw141292 */
7864644Sbaban if (direction != IDMAP_DIRECTION_U2W) {
7874644Sbaban /* bi-directional or from windows to unix */
7884520Snw141292 if (winname == NULL)
7894520Snw141292 return (IDMAP_ERR_W2U_NAMERULE);
7904520Snw141292 else if (unixname == NULL)
7914520Snw141292 return (IDMAP_ERR_W2U_NAMERULE);
7924520Snw141292 else if (EMPTY_NAME(winname))
7934520Snw141292 return (IDMAP_ERR_W2U_NAMERULE);
7944520Snw141292 else if (*winname == '*' && windomain && *windomain == '*') {
7954520Snw141292 if (*unixname == '*')
7964520Snw141292 *w2u_order = 8;
7974520Snw141292 else if (EMPTY_NAME(unixname))
7984520Snw141292 *w2u_order = 9;
7994520Snw141292 else /* unixname == name */
8004520Snw141292 *w2u_order = 10;
8014520Snw141292 } else if (*winname == '*') {
8024520Snw141292 if (*unixname == '*')
8034520Snw141292 *w2u_order = 5;
8044520Snw141292 else if (EMPTY_NAME(unixname))
8054520Snw141292 *w2u_order = 6;
8064520Snw141292 else /* name */
8074520Snw141292 *w2u_order = 7;
8084864Sbaban } else if (windomain != NULL && *windomain == '*') {
8094520Snw141292 /* winname == name */
8104520Snw141292 if (*unixname == '*')
8114520Snw141292 return (IDMAP_ERR_W2U_NAMERULE);
8124520Snw141292 else if (EMPTY_NAME(unixname))
8134520Snw141292 *w2u_order = 3;
8144520Snw141292 else /* name */
8154520Snw141292 *w2u_order = 4;
8164520Snw141292 } else {
8174520Snw141292 /* winname == name && windomain == null or name */
8184520Snw141292 if (*unixname == '*')
8194520Snw141292 return (IDMAP_ERR_W2U_NAMERULE);
8204520Snw141292 else if (EMPTY_NAME(unixname))
8214520Snw141292 *w2u_order = 1;
8224520Snw141292 else /* name */
8234520Snw141292 *w2u_order = 2;
8244520Snw141292 }
8255696Snw141292
8264520Snw141292 }
8274520Snw141292
8284520Snw141292 /*
8295696Snw141292 * 1. unixname to "", non-diagonal
8305696Snw141292 * 2. unixname to winname@domain (or winname), non-diagonal
8315696Snw141292 * 3. unixname to "", diagonal
8325696Snw141292 * 4. unixname to winname@domain (or winname), diagonal
8335696Snw141292 * 5. * to *@domain (or *), non-diagonal
8345696Snw141292 * 5. * to *@domain (or *), diagonal
8355696Snw141292 * 7. * to ""
8365696Snw141292 * 8. * to winname@domain (or winname)
8375696Snw141292 * 9. * to "", non-diagonal
8385696Snw141292 * 10. * to winname@domain (or winname), diagonal
8394520Snw141292 */
8404644Sbaban if (direction != IDMAP_DIRECTION_W2U) {
8415696Snw141292 int diagonal = is_diagonal ? 1 : 0;
8425696Snw141292
8434644Sbaban /* bi-directional or from unix to windows */
8444520Snw141292 if (unixname == NULL || EMPTY_NAME(unixname))
8454520Snw141292 return (IDMAP_ERR_U2W_NAMERULE);
8464520Snw141292 else if (winname == NULL)
8474520Snw141292 return (IDMAP_ERR_U2W_NAMERULE);
8484864Sbaban else if (windomain != NULL && *windomain == '*')
8494644Sbaban return (IDMAP_ERR_U2W_NAMERULE);
8504520Snw141292 else if (*unixname == '*') {
8514520Snw141292 if (*winname == '*')
8525696Snw141292 *u2w_order = 5 + diagonal;
8534520Snw141292 else if (EMPTY_NAME(winname))
8545696Snw141292 *u2w_order = 7 + 2 * diagonal;
8554520Snw141292 else
8565696Snw141292 *u2w_order = 8 + 2 * diagonal;
8574520Snw141292 } else {
8584520Snw141292 if (*winname == '*')
8594520Snw141292 return (IDMAP_ERR_U2W_NAMERULE);
8604520Snw141292 else if (EMPTY_NAME(winname))
8615696Snw141292 *u2w_order = 1 + 2 * diagonal;
8624520Snw141292 else
8635696Snw141292 *u2w_order = 2 + 2 * diagonal;
8644520Snw141292 }
8654520Snw141292 }
8664520Snw141292 return (IDMAP_SUCCESS);
8674520Snw141292 }
8684520Snw141292
8694520Snw141292 /*
8704520Snw141292 * Generate and execute SQL statement to add name-based mapping rule
8714520Snw141292 */
8724520Snw141292 idmap_retcode
add_namerule(sqlite * db,idmap_namerule * rule)8735696Snw141292 add_namerule(sqlite *db, idmap_namerule *rule)
8745696Snw141292 {
8754520Snw141292 char *sql = NULL;
8764520Snw141292 idmap_stat retcode;
8775064Sdm199847 char *dom = NULL;
8789422SAfshin.Ardakani@Sun.COM char *name;
8794520Snw141292 int w2u_order, u2w_order;
8804520Snw141292 char w2ubuf[11], u2wbuf[11];
8819422SAfshin.Ardakani@Sun.COM char *canonname = NULL;
8829422SAfshin.Ardakani@Sun.COM char *canondomain = NULL;
8834520Snw141292
8845064Sdm199847 retcode = get_namerule_order(rule->winname, rule->windomain,
8855696Snw141292 rule->unixname, rule->direction,
8865696Snw141292 rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order);
8874520Snw141292 if (retcode != IDMAP_SUCCESS)
8884520Snw141292 goto out;
8894520Snw141292
8904520Snw141292 if (w2u_order)
8914520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order);
8924520Snw141292 if (u2w_order)
8934520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order);
8944520Snw141292
8954864Sbaban /*
8964864Sbaban * For the triggers on namerules table to work correctly:
8974864Sbaban * 1) Use NULL instead of 0 for w2u_order and u2w_order
8984864Sbaban * 2) Use "" instead of NULL for "no domain"
8994864Sbaban */
9004864Sbaban
9019422SAfshin.Ardakani@Sun.COM name = rule->winname;
9029422SAfshin.Ardakani@Sun.COM dom = rule->windomain;
9034520Snw141292
9044520Snw141292 RDLOCK_CONFIG();
9059422SAfshin.Ardakani@Sun.COM if (lookup_wksids_name2sid(name, dom,
9069422SAfshin.Ardakani@Sun.COM &canonname, &canondomain,
9079422SAfshin.Ardakani@Sun.COM NULL, NULL, NULL) == IDMAP_SUCCESS) {
9089422SAfshin.Ardakani@Sun.COM name = canonname;
9099422SAfshin.Ardakani@Sun.COM dom = canondomain;
9109422SAfshin.Ardakani@Sun.COM } else if (EMPTY_STRING(dom)) {
9115317Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain)
9125317Sjp151216 dom = _idmapdstate.cfg->pgcfg.default_domain;
9134864Sbaban else
9144864Sbaban dom = "";
9154864Sbaban }
9164884Sjp151216 sql = sqlite_mprintf("INSERT into namerules "
9175696Snw141292 "(is_user, is_wuser, windomain, winname_display, is_nt4, "
9185696Snw141292 "unixname, w2u_order, u2w_order) "
9195696Snw141292 "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);",
9205696Snw141292 rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom,
9219422SAfshin.Ardakani@Sun.COM name, rule->is_nt4 ? 1 : 0, rule->unixname,
9225696Snw141292 w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL);
9234520Snw141292 UNLOCK_CONFIG();
9244520Snw141292
9254520Snw141292 if (sql == NULL) {
9264520Snw141292 retcode = IDMAP_ERR_INTERNAL;
9274520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
9284520Snw141292 goto out;
9294520Snw141292 }
9304520Snw141292
9316017Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql);
9324520Snw141292
9334520Snw141292 if (retcode == IDMAP_ERR_OTHER)
9344520Snw141292 retcode = IDMAP_ERR_CFG;
9354520Snw141292
9364520Snw141292 out:
9379422SAfshin.Ardakani@Sun.COM free(canonname);
9389422SAfshin.Ardakani@Sun.COM free(canondomain);
9394864Sbaban if (sql != NULL)
9404520Snw141292 sqlite_freemem(sql);
9414520Snw141292 return (retcode);
9424520Snw141292 }
9434520Snw141292
9444520Snw141292 /*
9454520Snw141292 * Flush name-based mapping rules
9464520Snw141292 */
9474520Snw141292 idmap_retcode
flush_namerules(sqlite * db)9485696Snw141292 flush_namerules(sqlite *db)
9495696Snw141292 {
9504520Snw141292 idmap_stat retcode;
9514520Snw141292
9526017Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "DELETE FROM namerules;");
9535696Snw141292
9544520Snw141292 return (retcode);
9554520Snw141292 }
9564520Snw141292
9574520Snw141292 /*
9584520Snw141292 * Generate and execute SQL statement to remove a name-based mapping rule
9594520Snw141292 */
9604520Snw141292 idmap_retcode
rm_namerule(sqlite * db,idmap_namerule * rule)9615696Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule)
9625696Snw141292 {
9634520Snw141292 char *sql = NULL;
9644520Snw141292 idmap_stat retcode;
9655696Snw141292 char *expr = NULL;
9664520Snw141292
9675064Sdm199847 if (rule->direction < 0 && EMPTY_STRING(rule->windomain) &&
9685064Sdm199847 EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname))
9694520Snw141292 return (IDMAP_SUCCESS);
9704520Snw141292
9715696Snw141292 retcode = gen_sql_expr_from_rule(rule, &expr);
9725696Snw141292 if (retcode != IDMAP_SUCCESS)
9735696Snw141292 goto out;
9745696Snw141292
97510966SJordan.Brown@Sun.COM sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s;", expr);
9764520Snw141292
9774520Snw141292 if (sql == NULL) {
9784520Snw141292 retcode = IDMAP_ERR_INTERNAL;
9794520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
9804520Snw141292 goto out;
9814520Snw141292 }
9824520Snw141292
9835696Snw141292
9846017Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql);
9854520Snw141292
9864520Snw141292 out:
9875696Snw141292 if (expr != NULL)
9885696Snw141292 sqlite_freemem(expr);
9894864Sbaban if (sql != NULL)
9904520Snw141292 sqlite_freemem(sql);
9914520Snw141292 return (retcode);
9924520Snw141292 }
9934520Snw141292
9944520Snw141292 /*
9954520Snw141292 * Compile the given SQL query and step just once.
9964520Snw141292 *
9974520Snw141292 * Input:
9984520Snw141292 * db - db handle
9994520Snw141292 * sql - SQL statement
10004520Snw141292 *
10014520Snw141292 * Output:
10024520Snw141292 * vm - virtual SQL machine
10034520Snw141292 * ncol - number of columns in the result
10044520Snw141292 * values - column values
10054520Snw141292 *
10064520Snw141292 * Return values:
10074520Snw141292 * IDMAP_SUCCESS
10084520Snw141292 * IDMAP_ERR_NOTFOUND
10094520Snw141292 * IDMAP_ERR_INTERNAL
10104520Snw141292 */
10114520Snw141292
10125696Snw141292 static
10135696Snw141292 idmap_retcode
sql_compile_n_step_once(sqlite * db,char * sql,sqlite_vm ** vm,int * ncol,int reqcol,const char *** values)10144520Snw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol,
10155696Snw141292 int reqcol, const char ***values)
10165696Snw141292 {
10174520Snw141292 char *errmsg = NULL;
10184884Sjp151216 int r;
10194884Sjp151216
10204884Sjp151216 if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) {
10215696Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql,
10225696Snw141292 CHECK_NULL(errmsg));
10234520Snw141292 sqlite_freemem(errmsg);
10244520Snw141292 return (IDMAP_ERR_INTERNAL);
10254520Snw141292 }
10264520Snw141292
10274884Sjp151216 r = sqlite_step(*vm, ncol, values, NULL);
10284884Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
10294884Sjp151216
10304884Sjp151216 if (r == SQLITE_ROW) {
10314864Sbaban if (ncol != NULL && *ncol < reqcol) {
10324520Snw141292 (void) sqlite_finalize(*vm, NULL);
10334520Snw141292 *vm = NULL;
10344520Snw141292 return (IDMAP_ERR_INTERNAL);
10354520Snw141292 }
10364520Snw141292 /* Caller will call finalize after using the results */
10374520Snw141292 return (IDMAP_SUCCESS);
10384520Snw141292 } else if (r == SQLITE_DONE) {
10394520Snw141292 (void) sqlite_finalize(*vm, NULL);
10404520Snw141292 *vm = NULL;
10414520Snw141292 return (IDMAP_ERR_NOTFOUND);
10424520Snw141292 }
10434520Snw141292
10444520Snw141292 (void) sqlite_finalize(*vm, &errmsg);
10454520Snw141292 *vm = NULL;
10465696Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql,
10475696Snw141292 CHECK_NULL(errmsg));
10484520Snw141292 sqlite_freemem(errmsg);
10494520Snw141292 return (IDMAP_ERR_INTERNAL);
10504520Snw141292 }
10514520Snw141292
10524864Sbaban /*
10536616Sdm199847 * Load config in the state.
10545731Sbaban *
10556616Sdm199847 * nm_siduid and nm_sidgid fields:
10565731Sbaban * state->nm_siduid represents mode used by sid2uid and uid2sid
10575731Sbaban * requests for directory-based name mappings. Similarly,
10585731Sbaban * state->nm_sidgid represents mode used by sid2gid and gid2sid
10595731Sbaban * requests.
10605731Sbaban *
10615731Sbaban * sid2uid/uid2sid:
106210504SKeyur.Desai@Sun.COM * none -> directory_based_mapping != DIRECTORY_MAPPING_NAME
10635731Sbaban * AD-mode -> !nldap_winname_attr && ad_unixuser_attr
10645731Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr
10655731Sbaban * mixed-mode -> nldap_winname_attr && ad_unixuser_attr
10665731Sbaban *
10675731Sbaban * sid2gid/gid2sid:
106810504SKeyur.Desai@Sun.COM * none -> directory_based_mapping != DIRECTORY_MAPPING_NAME
10695731Sbaban * AD-mode -> !nldap_winname_attr && ad_unixgroup_attr
10705731Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr
10715731Sbaban * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr
10725731Sbaban */
10735731Sbaban idmap_retcode
load_cfg_in_state(lookup_state_t * state)10746616Sdm199847 load_cfg_in_state(lookup_state_t *state)
10755731Sbaban {
10765731Sbaban state->nm_siduid = IDMAP_NM_NONE;
10775731Sbaban state->nm_sidgid = IDMAP_NM_NONE;
10785731Sbaban RDLOCK_CONFIG();
10796616Sdm199847
10807031Snw141292 state->eph_map_unres_sids = 0;
10817031Snw141292 if (_idmapdstate.cfg->pgcfg.eph_map_unres_sids)
10827031Snw141292 state->eph_map_unres_sids = 1;
10837031Snw141292
108410504SKeyur.Desai@Sun.COM state->directory_based_mapping =
108510504SKeyur.Desai@Sun.COM _idmapdstate.cfg->pgcfg.directory_based_mapping;
108610504SKeyur.Desai@Sun.COM
10876616Sdm199847 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) {
10886616Sdm199847 state->defdom =
10896616Sdm199847 strdup(_idmapdstate.cfg->pgcfg.default_domain);
10906616Sdm199847 if (state->defdom == NULL) {
10916616Sdm199847 UNLOCK_CONFIG();
10926616Sdm199847 return (IDMAP_ERR_MEMORY);
10936616Sdm199847 }
10946616Sdm199847 } else {
10956616Sdm199847 UNLOCK_CONFIG();
10966813Sdm199847 return (IDMAP_SUCCESS);
10976616Sdm199847 }
109810504SKeyur.Desai@Sun.COM
109910504SKeyur.Desai@Sun.COM if (_idmapdstate.cfg->pgcfg.directory_based_mapping !=
110010504SKeyur.Desai@Sun.COM DIRECTORY_MAPPING_NAME) {
11015731Sbaban UNLOCK_CONFIG();
11025731Sbaban return (IDMAP_SUCCESS);
11035731Sbaban }
110410504SKeyur.Desai@Sun.COM
11055731Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) {
11065731Sbaban state->nm_siduid =
11075731Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL)
11085731Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP;
11095731Sbaban state->nm_sidgid =
11105731Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL)
11115731Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP;
11125731Sbaban } else {
11135731Sbaban state->nm_siduid =
11145731Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL)
11155731Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE;
11165731Sbaban state->nm_sidgid =
11175731Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL)
11185731Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE;
11195731Sbaban }
11205731Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) {
11215731Sbaban state->ad_unixuser_attr =
11225731Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr);
11235731Sbaban if (state->ad_unixuser_attr == NULL) {
11245731Sbaban UNLOCK_CONFIG();
11255731Sbaban return (IDMAP_ERR_MEMORY);
11265731Sbaban }
11275731Sbaban }
11285731Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) {
11295731Sbaban state->ad_unixgroup_attr =
11305731Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr);
11315731Sbaban if (state->ad_unixgroup_attr == NULL) {
11325731Sbaban UNLOCK_CONFIG();
11335731Sbaban return (IDMAP_ERR_MEMORY);
11345731Sbaban }
11355731Sbaban }
11366616Sdm199847 if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) {
11376616Sdm199847 state->nldap_winname_attr =
11386616Sdm199847 strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr);
11396616Sdm199847 if (state->nldap_winname_attr == NULL) {
11406616Sdm199847 UNLOCK_CONFIG();
11416616Sdm199847 return (IDMAP_ERR_MEMORY);
11426616Sdm199847 }
11436616Sdm199847 }
11445731Sbaban UNLOCK_CONFIG();
11455731Sbaban return (IDMAP_SUCCESS);
11465731Sbaban }
11475731Sbaban
11485731Sbaban /*
11499422SAfshin.Ardakani@Sun.COM * Set the rule with specified values.
11506386Sjp151216 * All the strings are copied.
11516386Sjp151216 */
11526386Sjp151216 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)11536386Sjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain,
11546386Sjp151216 const char *winname, const char *unixname, boolean_t is_user,
11556386Sjp151216 boolean_t is_wuser, boolean_t is_nt4, int direction)
11566386Sjp151216 {
11576386Sjp151216 /*
11586386Sjp151216 * Only update if they differ because we have to free
11596386Sjp151216 * and duplicate the strings
11606386Sjp151216 */
11616386Sjp151216 if (rule->windomain == NULL || windomain == NULL ||
11626386Sjp151216 strcmp(rule->windomain, windomain) != 0) {
11636386Sjp151216 if (rule->windomain != NULL) {
11646386Sjp151216 free(rule->windomain);
11656386Sjp151216 rule->windomain = NULL;
11666386Sjp151216 }
11676386Sjp151216 if (windomain != NULL)
11686386Sjp151216 rule->windomain = strdup(windomain);
11696386Sjp151216 }
11706386Sjp151216
11716386Sjp151216 if (rule->winname == NULL || winname == NULL ||
11726386Sjp151216 strcmp(rule->winname, winname) != 0) {
11736386Sjp151216 if (rule->winname != NULL) {
11746386Sjp151216 free(rule->winname);
11756386Sjp151216 rule->winname = NULL;
11766386Sjp151216 }
11776386Sjp151216 if (winname != NULL)
11786386Sjp151216 rule->winname = strdup(winname);
11796386Sjp151216 }
11806386Sjp151216
11816386Sjp151216 if (rule->unixname == NULL || unixname == NULL ||
11826386Sjp151216 strcmp(rule->unixname, unixname) != 0) {
11836386Sjp151216 if (rule->unixname != NULL) {
11846386Sjp151216 free(rule->unixname);
11856386Sjp151216 rule->unixname = NULL;
11866386Sjp151216 }
11876386Sjp151216 if (unixname != NULL)
11886386Sjp151216 rule->unixname = strdup(unixname);
11896386Sjp151216 }
11906386Sjp151216
11916386Sjp151216 rule->is_user = is_user;
11926386Sjp151216 rule->is_wuser = is_wuser;
11936386Sjp151216 rule->is_nt4 = is_nt4;
11946386Sjp151216 rule->direction = direction;
11956386Sjp151216 }
11966386Sjp151216
11975731Sbaban /*
11985731Sbaban * Lookup well-known SIDs table either by winname or by SID.
11999422SAfshin.Ardakani@Sun.COM *
12009422SAfshin.Ardakani@Sun.COM * If the given winname or SID is a well-known SID then we set is_wksid
12015731Sbaban * variable and then proceed to see if the SID has a hard mapping to
12025731Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to
12039422SAfshin.Ardakani@Sun.COM * fixed ephemeral ids). The direction flag indicates whether we have
12049422SAfshin.Ardakani@Sun.COM * a mapping; UNDEF indicates that we do not.
12059422SAfshin.Ardakani@Sun.COM *
12069422SAfshin.Ardakani@Sun.COM * If we find a mapping then we return success, except for the
120711963SAfshin.Ardakani@Sun.COM * special case of IDMAP_SENTINEL_PID which indicates an inhibited mapping.
12089422SAfshin.Ardakani@Sun.COM *
12099422SAfshin.Ardakani@Sun.COM * If we find a matching entry, but no mapping, we supply SID, name, and type
12109422SAfshin.Ardakani@Sun.COM * information and return "not found". Higher layers will probably
12119422SAfshin.Ardakani@Sun.COM * do ephemeral mapping.
12129422SAfshin.Ardakani@Sun.COM *
12139422SAfshin.Ardakani@Sun.COM * If we do not find a match, we return "not found" and leave the question
12149422SAfshin.Ardakani@Sun.COM * to higher layers.
12155731Sbaban */
12165696Snw141292 static
12175696Snw141292 idmap_retcode
lookup_wksids_sid2pid(idmap_mapping * req,idmap_id_res * res,int * is_wksid)12189422SAfshin.Ardakani@Sun.COM lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *is_wksid)
12195696Snw141292 {
12209422SAfshin.Ardakani@Sun.COM const wksids_table_t *wksid;
12219422SAfshin.Ardakani@Sun.COM
12229422SAfshin.Ardakani@Sun.COM *is_wksid = 0;
12239422SAfshin.Ardakani@Sun.COM
12249422SAfshin.Ardakani@Sun.COM assert(req->id1.idmap_id_u.sid.prefix != NULL ||
12259422SAfshin.Ardakani@Sun.COM req->id1name != NULL);
12269422SAfshin.Ardakani@Sun.COM
12279422SAfshin.Ardakani@Sun.COM if (req->id1.idmap_id_u.sid.prefix != NULL) {
12289422SAfshin.Ardakani@Sun.COM wksid = find_wksid_by_sid(req->id1.idmap_id_u.sid.prefix,
12299422SAfshin.Ardakani@Sun.COM req->id1.idmap_id_u.sid.rid, res->id.idtype);
12309422SAfshin.Ardakani@Sun.COM } else {
12319422SAfshin.Ardakani@Sun.COM wksid = find_wksid_by_name(req->id1name, req->id1domain,
12329422SAfshin.Ardakani@Sun.COM res->id.idtype);
12339422SAfshin.Ardakani@Sun.COM }
12349422SAfshin.Ardakani@Sun.COM if (wksid == NULL)
12359422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_NOTFOUND);
12369422SAfshin.Ardakani@Sun.COM
12379422SAfshin.Ardakani@Sun.COM /* Found matching entry. */
12389422SAfshin.Ardakani@Sun.COM
12399422SAfshin.Ardakani@Sun.COM /* Fill in name if it was not already there. */
12409422SAfshin.Ardakani@Sun.COM if (req->id1name == NULL) {
12419422SAfshin.Ardakani@Sun.COM req->id1name = strdup(wksid->winname);
12429422SAfshin.Ardakani@Sun.COM if (req->id1name == NULL)
12439422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_MEMORY);
12449422SAfshin.Ardakani@Sun.COM }
12459422SAfshin.Ardakani@Sun.COM
12469422SAfshin.Ardakani@Sun.COM /* Fill in SID if it was not already there */
12479422SAfshin.Ardakani@Sun.COM if (req->id1.idmap_id_u.sid.prefix == NULL) {
12489422SAfshin.Ardakani@Sun.COM if (wksid->sidprefix != NULL) {
12495731Sbaban req->id1.idmap_id_u.sid.prefix =
12509422SAfshin.Ardakani@Sun.COM strdup(wksid->sidprefix);
12519422SAfshin.Ardakani@Sun.COM } else {
12529422SAfshin.Ardakani@Sun.COM RDLOCK_CONFIG();
12539422SAfshin.Ardakani@Sun.COM req->id1.idmap_id_u.sid.prefix =
12549422SAfshin.Ardakani@Sun.COM strdup(_idmapdstate.cfg->pgcfg.machine_sid);
12559422SAfshin.Ardakani@Sun.COM UNLOCK_CONFIG();
12569422SAfshin.Ardakani@Sun.COM }
12579422SAfshin.Ardakani@Sun.COM if (req->id1.idmap_id_u.sid.prefix == NULL)
12589422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_MEMORY);
12599422SAfshin.Ardakani@Sun.COM req->id1.idmap_id_u.sid.rid = wksid->rid;
12609422SAfshin.Ardakani@Sun.COM }
12619422SAfshin.Ardakani@Sun.COM
12629422SAfshin.Ardakani@Sun.COM /* Fill in the canonical domain if not already there */
12639422SAfshin.Ardakani@Sun.COM if (req->id1domain == NULL) {
12649422SAfshin.Ardakani@Sun.COM const char *dom;
12659422SAfshin.Ardakani@Sun.COM
12669422SAfshin.Ardakani@Sun.COM RDLOCK_CONFIG();
126711337SWilliam.Krier@Sun.COM if (wksid->domain != NULL)
12689422SAfshin.Ardakani@Sun.COM dom = wksid->domain;
126911337SWilliam.Krier@Sun.COM else
12709422SAfshin.Ardakani@Sun.COM dom = _idmapdstate.hostname;
12719422SAfshin.Ardakani@Sun.COM req->id1domain = strdup(dom);
12729422SAfshin.Ardakani@Sun.COM UNLOCK_CONFIG();
12739422SAfshin.Ardakani@Sun.COM if (req->id1domain == NULL)
12749422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_MEMORY);
12759422SAfshin.Ardakani@Sun.COM }
12769422SAfshin.Ardakani@Sun.COM
12779422SAfshin.Ardakani@Sun.COM *is_wksid = 1;
12789422SAfshin.Ardakani@Sun.COM req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
12799422SAfshin.Ardakani@Sun.COM
12809422SAfshin.Ardakani@Sun.COM req->id1.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID;
12819422SAfshin.Ardakani@Sun.COM
12829422SAfshin.Ardakani@Sun.COM if (res->id.idtype == IDMAP_POSIXID) {
12839422SAfshin.Ardakani@Sun.COM res->id.idtype = wksid->is_wuser ? IDMAP_UID : IDMAP_GID;
12849422SAfshin.Ardakani@Sun.COM }
12859422SAfshin.Ardakani@Sun.COM
12869422SAfshin.Ardakani@Sun.COM if (wksid->direction == IDMAP_DIRECTION_UNDEF) {
12879422SAfshin.Ardakani@Sun.COM /*
12889422SAfshin.Ardakani@Sun.COM * We don't have a mapping
12899422SAfshin.Ardakani@Sun.COM * (But note that we may have supplied SID, name, or type
12909422SAfshin.Ardakani@Sun.COM * information.)
12919422SAfshin.Ardakani@Sun.COM */
12929422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_NOTFOUND);
12934520Snw141292 }
12949422SAfshin.Ardakani@Sun.COM
12959422SAfshin.Ardakani@Sun.COM /*
12969422SAfshin.Ardakani@Sun.COM * We have an explicit mapping.
12979422SAfshin.Ardakani@Sun.COM */
129811963SAfshin.Ardakani@Sun.COM if (wksid->pid == IDMAP_SENTINEL_PID) {
12999422SAfshin.Ardakani@Sun.COM /*
13009422SAfshin.Ardakani@Sun.COM * ... which is that mapping is inhibited.
13019422SAfshin.Ardakani@Sun.COM */
13029422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_NOMAPPING);
13039422SAfshin.Ardakani@Sun.COM }
13049422SAfshin.Ardakani@Sun.COM
13059422SAfshin.Ardakani@Sun.COM switch (res->id.idtype) {
13069422SAfshin.Ardakani@Sun.COM case IDMAP_UID:
13079422SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.uid = wksid->pid;
13089422SAfshin.Ardakani@Sun.COM break;
13099422SAfshin.Ardakani@Sun.COM case IDMAP_GID:
13109422SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.gid = wksid->pid;
13119422SAfshin.Ardakani@Sun.COM break;
13129422SAfshin.Ardakani@Sun.COM default:
13139422SAfshin.Ardakani@Sun.COM /* IDMAP_POSIXID is eliminated above */
13149422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_NOTSUPPORTED);
13159422SAfshin.Ardakani@Sun.COM }
13169422SAfshin.Ardakani@Sun.COM
13179422SAfshin.Ardakani@Sun.COM res->direction = wksid->direction;
13189422SAfshin.Ardakani@Sun.COM res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID;
13199422SAfshin.Ardakani@Sun.COM res->info.src = IDMAP_MAP_SRC_HARD_CODED;
13209422SAfshin.Ardakani@Sun.COM return (IDMAP_SUCCESS);
13214520Snw141292 }
13224520Snw141292
13235696Snw141292
13249422SAfshin.Ardakani@Sun.COM /*
13259422SAfshin.Ardakani@Sun.COM * Look for an entry mapping a PID to a SID.
13269422SAfshin.Ardakani@Sun.COM *
13279422SAfshin.Ardakani@Sun.COM * Note that direction=UNDEF entries do not specify a mapping,
132811963SAfshin.Ardakani@Sun.COM * and that IDMAP_SENTINEL_PID entries represent either an inhibited
13299422SAfshin.Ardakani@Sun.COM * mapping or an ephemeral mapping. We don't handle either here;
13309422SAfshin.Ardakani@Sun.COM * they are filtered out by find_wksid_by_pid.
13319422SAfshin.Ardakani@Sun.COM */
13325696Snw141292 static
13335696Snw141292 idmap_retcode
lookup_wksids_pid2sid(idmap_mapping * req,idmap_id_res * res,int is_user)13345696Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user)
13355696Snw141292 {
13369422SAfshin.Ardakani@Sun.COM const wksids_table_t *wksid;
13379422SAfshin.Ardakani@Sun.COM
13389422SAfshin.Ardakani@Sun.COM wksid = find_wksid_by_pid(req->id1.idmap_id_u.uid, is_user);
13399422SAfshin.Ardakani@Sun.COM if (wksid == NULL)
13405731Sbaban return (IDMAP_ERR_NOTFOUND);
13419422SAfshin.Ardakani@Sun.COM
13429422SAfshin.Ardakani@Sun.COM if (res->id.idtype == IDMAP_SID) {
13439422SAfshin.Ardakani@Sun.COM res->id.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID;
13449422SAfshin.Ardakani@Sun.COM }
13459422SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.sid.rid = wksid->rid;
13469422SAfshin.Ardakani@Sun.COM
13479422SAfshin.Ardakani@Sun.COM if (wksid->sidprefix != NULL) {
13489422SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.sid.prefix =
13499422SAfshin.Ardakani@Sun.COM strdup(wksid->sidprefix);
13509422SAfshin.Ardakani@Sun.COM } else {
13519422SAfshin.Ardakani@Sun.COM RDLOCK_CONFIG();
13529422SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.sid.prefix =
13539422SAfshin.Ardakani@Sun.COM strdup(_idmapdstate.cfg->pgcfg.machine_sid);
13549422SAfshin.Ardakani@Sun.COM UNLOCK_CONFIG();
13559422SAfshin.Ardakani@Sun.COM }
13569422SAfshin.Ardakani@Sun.COM
13579422SAfshin.Ardakani@Sun.COM if (res->id.idmap_id_u.sid.prefix == NULL) {
13589422SAfshin.Ardakani@Sun.COM idmapdlog(LOG_ERR, "Out of memory");
13599422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_MEMORY);
13604864Sbaban }
13619422SAfshin.Ardakani@Sun.COM
136211337SWilliam.Krier@Sun.COM /* Fill in name if it was not already there. */
136311337SWilliam.Krier@Sun.COM if (req->id2name == NULL) {
136411337SWilliam.Krier@Sun.COM req->id2name = strdup(wksid->winname);
136511337SWilliam.Krier@Sun.COM if (req->id2name == NULL)
136611337SWilliam.Krier@Sun.COM return (IDMAP_ERR_MEMORY);
136711337SWilliam.Krier@Sun.COM }
136811337SWilliam.Krier@Sun.COM
136911337SWilliam.Krier@Sun.COM /* Fill in the canonical domain if not already there */
137011337SWilliam.Krier@Sun.COM if (req->id2domain == NULL) {
137111337SWilliam.Krier@Sun.COM const char *dom;
137211337SWilliam.Krier@Sun.COM
137311337SWilliam.Krier@Sun.COM RDLOCK_CONFIG();
137411337SWilliam.Krier@Sun.COM if (wksid->domain != NULL)
137511337SWilliam.Krier@Sun.COM dom = wksid->domain;
137611337SWilliam.Krier@Sun.COM else
137711337SWilliam.Krier@Sun.COM dom = _idmapdstate.hostname;
137811337SWilliam.Krier@Sun.COM req->id2domain = strdup(dom);
137911337SWilliam.Krier@Sun.COM UNLOCK_CONFIG();
138011337SWilliam.Krier@Sun.COM if (req->id2domain == NULL)
138111337SWilliam.Krier@Sun.COM return (IDMAP_ERR_MEMORY);
138211337SWilliam.Krier@Sun.COM }
138311337SWilliam.Krier@Sun.COM
13849422SAfshin.Ardakani@Sun.COM res->direction = wksid->direction;
13859422SAfshin.Ardakani@Sun.COM res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID;
13869422SAfshin.Ardakani@Sun.COM res->info.src = IDMAP_MAP_SRC_HARD_CODED;
13879422SAfshin.Ardakani@Sun.COM return (IDMAP_SUCCESS);
13884864Sbaban }
13894864Sbaban
13909422SAfshin.Ardakani@Sun.COM /*
13919422SAfshin.Ardakani@Sun.COM * Look up a name in the wksids list, matching name and, if supplied, domain,
13929422SAfshin.Ardakani@Sun.COM * and extract data.
13939422SAfshin.Ardakani@Sun.COM *
13949422SAfshin.Ardakani@Sun.COM * Given:
13959422SAfshin.Ardakani@Sun.COM * name Windows user name
13969422SAfshin.Ardakani@Sun.COM * domain Windows domain name (or NULL)
13979422SAfshin.Ardakani@Sun.COM *
13989422SAfshin.Ardakani@Sun.COM * Return: Error code
13999422SAfshin.Ardakani@Sun.COM *
14009422SAfshin.Ardakani@Sun.COM * *canonname canonical name (if canonname non-NULL) [1]
14019422SAfshin.Ardakani@Sun.COM * *canondomain canonical domain (if canondomain non-NULL) [1]
14029422SAfshin.Ardakani@Sun.COM * *sidprefix SID prefix (if sidprefix non-NULL) [1]
14039422SAfshin.Ardakani@Sun.COM * *rid RID (if rid non-NULL) [2]
14049422SAfshin.Ardakani@Sun.COM * *type Type (if type non-NULL) [2]
14059422SAfshin.Ardakani@Sun.COM *
14069422SAfshin.Ardakani@Sun.COM * [1] malloc'ed, NULL on error
14079422SAfshin.Ardakani@Sun.COM * [2] Undefined on error
14089422SAfshin.Ardakani@Sun.COM */
14095696Snw141292 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)14109422SAfshin.Ardakani@Sun.COM lookup_wksids_name2sid(
14119422SAfshin.Ardakani@Sun.COM const char *name,
14129422SAfshin.Ardakani@Sun.COM const char *domain,
14139422SAfshin.Ardakani@Sun.COM char **canonname,
14149422SAfshin.Ardakani@Sun.COM char **canondomain,
14159422SAfshin.Ardakani@Sun.COM char **sidprefix,
14169422SAfshin.Ardakani@Sun.COM idmap_rid_t *rid,
141712508Samw@Sun.COM idmap_id_type *type)
14185696Snw141292 {
14199422SAfshin.Ardakani@Sun.COM const wksids_table_t *wksid;
14209422SAfshin.Ardakani@Sun.COM
14219422SAfshin.Ardakani@Sun.COM if (sidprefix != NULL)
14229422SAfshin.Ardakani@Sun.COM *sidprefix = NULL;
14239422SAfshin.Ardakani@Sun.COM if (canonname != NULL)
14249422SAfshin.Ardakani@Sun.COM *canonname = NULL;
14259422SAfshin.Ardakani@Sun.COM if (canondomain != NULL)
14269422SAfshin.Ardakani@Sun.COM *canondomain = NULL;
14279422SAfshin.Ardakani@Sun.COM
14289422SAfshin.Ardakani@Sun.COM wksid = find_wksid_by_name(name, domain, IDMAP_POSIXID);
14299422SAfshin.Ardakani@Sun.COM if (wksid == NULL)
14309422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_NOTFOUND);
14319422SAfshin.Ardakani@Sun.COM
14329422SAfshin.Ardakani@Sun.COM if (sidprefix != NULL) {
14339422SAfshin.Ardakani@Sun.COM if (wksid->sidprefix != NULL) {
14349422SAfshin.Ardakani@Sun.COM *sidprefix = strdup(wksid->sidprefix);
14359422SAfshin.Ardakani@Sun.COM } else {
14369422SAfshin.Ardakani@Sun.COM RDLOCK_CONFIG();
14379422SAfshin.Ardakani@Sun.COM *sidprefix = strdup(
14389422SAfshin.Ardakani@Sun.COM _idmapdstate.cfg->pgcfg.machine_sid);
14399422SAfshin.Ardakani@Sun.COM UNLOCK_CONFIG();
14405731Sbaban }
14419422SAfshin.Ardakani@Sun.COM if (*sidprefix == NULL)
14429422SAfshin.Ardakani@Sun.COM goto nomem;
14439422SAfshin.Ardakani@Sun.COM }
14449422SAfshin.Ardakani@Sun.COM
14459422SAfshin.Ardakani@Sun.COM if (rid != NULL)
14469422SAfshin.Ardakani@Sun.COM *rid = wksid->rid;
14479422SAfshin.Ardakani@Sun.COM
14489422SAfshin.Ardakani@Sun.COM if (canonname != NULL) {
14499422SAfshin.Ardakani@Sun.COM *canonname = strdup(wksid->winname);
14509422SAfshin.Ardakani@Sun.COM if (*canonname == NULL)
14519422SAfshin.Ardakani@Sun.COM goto nomem;
14529422SAfshin.Ardakani@Sun.COM }
14539422SAfshin.Ardakani@Sun.COM
14549422SAfshin.Ardakani@Sun.COM if (canondomain != NULL) {
14559422SAfshin.Ardakani@Sun.COM if (wksid->domain != NULL) {
14569422SAfshin.Ardakani@Sun.COM *canondomain = strdup(wksid->domain);
14579422SAfshin.Ardakani@Sun.COM } else {
14589422SAfshin.Ardakani@Sun.COM RDLOCK_CONFIG();
14599422SAfshin.Ardakani@Sun.COM *canondomain = strdup(_idmapdstate.hostname);
14609422SAfshin.Ardakani@Sun.COM UNLOCK_CONFIG();
14614520Snw141292 }
14629422SAfshin.Ardakani@Sun.COM if (*canondomain == NULL)
14639422SAfshin.Ardakani@Sun.COM goto nomem;
14644520Snw141292 }
14659422SAfshin.Ardakani@Sun.COM
14669422SAfshin.Ardakani@Sun.COM if (type != NULL)
14679422SAfshin.Ardakani@Sun.COM *type = (wksid->is_wuser) ?
146812508Samw@Sun.COM IDMAP_USID : IDMAP_GSID;
14699422SAfshin.Ardakani@Sun.COM
14709422SAfshin.Ardakani@Sun.COM return (IDMAP_SUCCESS);
14719422SAfshin.Ardakani@Sun.COM
14729422SAfshin.Ardakani@Sun.COM nomem:
14739422SAfshin.Ardakani@Sun.COM idmapdlog(LOG_ERR, "Out of memory");
14749422SAfshin.Ardakani@Sun.COM
14759422SAfshin.Ardakani@Sun.COM if (sidprefix != NULL) {
14769422SAfshin.Ardakani@Sun.COM free(*sidprefix);
14779422SAfshin.Ardakani@Sun.COM *sidprefix = NULL;
14789422SAfshin.Ardakani@Sun.COM }
14799422SAfshin.Ardakani@Sun.COM
14809422SAfshin.Ardakani@Sun.COM if (canonname != NULL) {
14819422SAfshin.Ardakani@Sun.COM free(*canonname);
14829422SAfshin.Ardakani@Sun.COM *canonname = NULL;
14839422SAfshin.Ardakani@Sun.COM }
14849422SAfshin.Ardakani@Sun.COM
14859422SAfshin.Ardakani@Sun.COM if (canondomain != NULL) {
14869422SAfshin.Ardakani@Sun.COM free(*canondomain);
14879422SAfshin.Ardakani@Sun.COM *canondomain = NULL;
14889422SAfshin.Ardakani@Sun.COM }
14899422SAfshin.Ardakani@Sun.COM
14909422SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_MEMORY);
14914520Snw141292 }
14924520Snw141292
14935696Snw141292 static
14945696Snw141292 idmap_retcode
lookup_cache_sid2pid(sqlite * cache,idmap_mapping * req,idmap_id_res * res)14955696Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res)
14965696Snw141292 {
14974520Snw141292 char *end;
14984520Snw141292 char *sql = NULL;
14994520Snw141292 const char **values;
15004520Snw141292 sqlite_vm *vm = NULL;
15014520Snw141292 int ncol, is_user;
15024520Snw141292 uid_t pid;
15034520Snw141292 time_t curtime, exp;
15044520Snw141292 idmap_retcode retcode;
15055932Sbaban char *is_user_string, *lower_name;
15064520Snw141292
15074520Snw141292 /* Current time */
15084520Snw141292 errno = 0;
15094520Snw141292 if ((curtime = time(NULL)) == (time_t)-1) {
15105696Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)",
15115696Snw141292 strerror(errno));
15124520Snw141292 retcode = IDMAP_ERR_INTERNAL;
15134520Snw141292 goto out;
15144520Snw141292 }
15154520Snw141292
15165731Sbaban switch (res->id.idtype) {
15175696Snw141292 case IDMAP_UID:
15185696Snw141292 is_user_string = "1";
15195696Snw141292 break;
15205696Snw141292 case IDMAP_GID:
15215696Snw141292 is_user_string = "0";
15225696Snw141292 break;
15235696Snw141292 case IDMAP_POSIXID:
15245696Snw141292 /* the non-diagonal mapping */
15255696Snw141292 is_user_string = "is_wuser";
15265696Snw141292 break;
15275696Snw141292 default:
15285696Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED;
15295696Snw141292 goto out;
15305696Snw141292 }
15315696Snw141292
15324520Snw141292 /* SQL to lookup the cache */
15336386Sjp151216
15345731Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) {
15355731Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, "
15366386Sjp151216 "unixname, u2w, is_wuser, "
15376386Sjp151216 "map_type, map_dn, map_attr, map_value, "
15386386Sjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 "
15395731Sbaban "FROM idmap_cache WHERE is_user = %s AND "
15405731Sbaban "sidprefix = %Q AND rid = %u AND w2u = 1 AND "
15415731Sbaban "(pid >= 2147483648 OR "
15425731Sbaban "(expiration = 0 OR expiration ISNULL OR "
15435731Sbaban "expiration > %d));",
15445731Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix,
15455731Sbaban req->id1.idmap_id_u.sid.rid, curtime);
15465731Sbaban } else if (req->id1name != NULL) {
15475932Sbaban if ((lower_name = tolower_u8(req->id1name)) == NULL)
15485932Sbaban lower_name = req->id1name;
15495731Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, "
15506386Sjp151216 "unixname, u2w, is_wuser, "
15516386Sjp151216 "map_type, map_dn, map_attr, map_value, "
15526386Sjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 "
15535731Sbaban "FROM idmap_cache WHERE is_user = %s AND "
15545731Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND "
15555731Sbaban "(pid >= 2147483648 OR "
15565731Sbaban "(expiration = 0 OR expiration ISNULL OR "
15575731Sbaban "expiration > %d));",
15586386Sjp151216 is_user_string, lower_name, req->id1domain,
15596386Sjp151216 curtime);
15605932Sbaban if (lower_name != req->id1name)
15615932Sbaban free(lower_name);
15625731Sbaban } else {
15635731Sbaban retcode = IDMAP_ERR_ARG;
15645731Sbaban goto out;
15655731Sbaban }
15664520Snw141292 if (sql == NULL) {
15674520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
15684520Snw141292 retcode = IDMAP_ERR_MEMORY;
15694520Snw141292 goto out;
15704520Snw141292 }
15716386Sjp151216 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol,
15726386Sjp151216 14, &values);
15734520Snw141292 sqlite_freemem(sql);
15744520Snw141292
15754520Snw141292 if (retcode == IDMAP_ERR_NOTFOUND) {
15764520Snw141292 goto out;
15774520Snw141292 } else if (retcode == IDMAP_SUCCESS) {
15784520Snw141292 /* sanity checks */
15794520Snw141292 if (values[0] == NULL || values[1] == NULL) {
15804520Snw141292 retcode = IDMAP_ERR_CACHE;
15814520Snw141292 goto out;
15824520Snw141292 }
15834520Snw141292
15844520Snw141292 pid = strtoul(values[0], &end, 10);
15855731Sbaban is_user = strncmp(values[1], "0", 2) ? 1 : 0;
15864520Snw141292
15875696Snw141292 if (is_user) {
15885696Snw141292 res->id.idtype = IDMAP_UID;
15895696Snw141292 res->id.idmap_id_u.uid = pid;
15905696Snw141292 } else {
15915696Snw141292 res->id.idtype = IDMAP_GID;
15925696Snw141292 res->id.idmap_id_u.gid = pid;
15935696Snw141292 }
15945696Snw141292
15954520Snw141292 /*
15964520Snw141292 * We may have an expired ephemeral mapping. Consider
15974520Snw141292 * the expired entry as valid if we are not going to
15984520Snw141292 * perform name-based mapping. But do not renew the
15994520Snw141292 * expiration.
16004520Snw141292 * If we will be doing name-based mapping then store the
16014520Snw141292 * ephemeral pid in the result so that we can use it
16024520Snw141292 * if we end up doing dynamic mapping again.
16034520Snw141292 */
16044520Snw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) &&
16055696Snw141292 !AVOID_NAMESERVICE(req) &&
160611963SAfshin.Ardakani@Sun.COM IDMAP_ID_IS_EPHEMERAL(pid) && values[2] != NULL) {
16075696Snw141292 exp = strtoll(values[2], &end, 10);
16085696Snw141292 if (exp && exp <= curtime) {
16095696Snw141292 /* Store the ephemeral pid */
16105696Snw141292 res->direction = IDMAP_DIRECTION_BI;
16115696Snw141292 req->direction |= is_user
16125696Snw141292 ? _IDMAP_F_EXP_EPH_UID
16135696Snw141292 : _IDMAP_F_EXP_EPH_GID;
16145696Snw141292 retcode = IDMAP_ERR_NOTFOUND;
16154520Snw141292 }
16164520Snw141292 }
16174520Snw141292 }
16184520Snw141292
16194520Snw141292 out:
16204520Snw141292 if (retcode == IDMAP_SUCCESS) {
16214864Sbaban if (values[4] != NULL)
16224520Snw141292 res->direction =
16234644Sbaban (strtol(values[4], &end, 10) == 0)?
16244644Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
16254520Snw141292 else
16264644Sbaban res->direction = IDMAP_DIRECTION_W2U;
16274520Snw141292
16284864Sbaban if (values[3] != NULL) {
16295731Sbaban if (req->id2name != NULL)
16305731Sbaban free(req->id2name);
16315064Sdm199847 req->id2name = strdup(values[3]);
16325064Sdm199847 if (req->id2name == NULL) {
16334520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
16344520Snw141292 retcode = IDMAP_ERR_MEMORY;
16354520Snw141292 }
16364520Snw141292 }
16375731Sbaban
16385731Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ?
16395731Sbaban IDMAP_USID : IDMAP_GSID;
16406386Sjp151216
16416386Sjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
16426386Sjp151216 res->info.src = IDMAP_MAP_SRC_CACHE;
16436386Sjp151216 res->info.how.map_type = strtoul(values[6], &end, 10);
16446386Sjp151216 switch (res->info.how.map_type) {
16456386Sjp151216 case IDMAP_MAP_TYPE_DS_AD:
16466386Sjp151216 res->info.how.idmap_how_u.ad.dn =
16476386Sjp151216 strdup(values[7]);
16486386Sjp151216 res->info.how.idmap_how_u.ad.attr =
16496386Sjp151216 strdup(values[8]);
16506386Sjp151216 res->info.how.idmap_how_u.ad.value =
16516386Sjp151216 strdup(values[9]);
16526386Sjp151216 break;
16536386Sjp151216
16546386Sjp151216 case IDMAP_MAP_TYPE_DS_NLDAP:
16556386Sjp151216 res->info.how.idmap_how_u.nldap.dn =
16566386Sjp151216 strdup(values[7]);
16576386Sjp151216 res->info.how.idmap_how_u.nldap.attr =
16586386Sjp151216 strdup(values[8]);
16596386Sjp151216 res->info.how.idmap_how_u.nldap.value =
16606386Sjp151216 strdup(values[9]);
16616386Sjp151216 break;
16626386Sjp151216
16636386Sjp151216 case IDMAP_MAP_TYPE_RULE_BASED:
16646386Sjp151216 res->info.how.idmap_how_u.rule.windomain =
16656386Sjp151216 strdup(values[10]);
16666386Sjp151216 res->info.how.idmap_how_u.rule.winname =
16676386Sjp151216 strdup(values[11]);
16686386Sjp151216 res->info.how.idmap_how_u.rule.unixname =
16696386Sjp151216 strdup(values[12]);
16706386Sjp151216 res->info.how.idmap_how_u.rule.is_nt4 =
16716386Sjp151216 strtoul(values[13], &end, 1);
16726386Sjp151216 res->info.how.idmap_how_u.rule.is_user =
16736386Sjp151216 is_user;
16746386Sjp151216 res->info.how.idmap_how_u.rule.is_wuser =
16756386Sjp151216 strtoul(values[5], &end, 1);
16766386Sjp151216 break;
16776386Sjp151216
16786386Sjp151216 case IDMAP_MAP_TYPE_EPHEMERAL:
16796386Sjp151216 break;
16806386Sjp151216
16816386Sjp151216 case IDMAP_MAP_TYPE_LOCAL_SID:
16826386Sjp151216 break;
16836386Sjp151216
16846386Sjp151216 case IDMAP_MAP_TYPE_KNOWN_SID:
16856386Sjp151216 break;
16866386Sjp151216
168710504SKeyur.Desai@Sun.COM case IDMAP_MAP_TYPE_IDMU:
168810504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.dn =
168910504SKeyur.Desai@Sun.COM strdup(values[7]);
169010504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.attr =
169110504SKeyur.Desai@Sun.COM strdup(values[8]);
169210504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.value =
169310504SKeyur.Desai@Sun.COM strdup(values[9]);
169410504SKeyur.Desai@Sun.COM break;
169510504SKeyur.Desai@Sun.COM
16966386Sjp151216 default:
169710504SKeyur.Desai@Sun.COM /* Unknown mapping type */
16986386Sjp151216 assert(FALSE);
16996386Sjp151216 }
17006386Sjp151216 }
17014520Snw141292 }
17024864Sbaban if (vm != NULL)
17034520Snw141292 (void) sqlite_finalize(vm, NULL);
17044520Snw141292 return (retcode);
17054520Snw141292 }
17064520Snw141292
170712508Samw@Sun.COM /*
170812508Samw@Sun.COM * Previous versions used two enumerations for representing types.
170912508Samw@Sun.COM * One of those has largely been eliminated, but was used in the
171012508Samw@Sun.COM * name cache table and so during an upgrade might still be visible.
171112508Samw@Sun.COM * In addition, the test suite prepopulates the cache with these values.
171212508Samw@Sun.COM *
171312508Samw@Sun.COM * This function translates those old values into the new values.
171412508Samw@Sun.COM *
171512508Samw@Sun.COM * This code deliberately does not use symbolic values for the legacy
171612508Samw@Sun.COM * values. This is the *only* place where they should be used.
171712508Samw@Sun.COM */
171812508Samw@Sun.COM static
171912508Samw@Sun.COM idmap_id_type
xlate_legacy_type(int type)172012508Samw@Sun.COM xlate_legacy_type(int type)
172112508Samw@Sun.COM {
172212508Samw@Sun.COM switch (type) {
172312508Samw@Sun.COM case -1004: /* _IDMAP_T_USER */
172412508Samw@Sun.COM return (IDMAP_USID);
172512508Samw@Sun.COM case -1005: /* _IDMAP_T_GROUP */
172612508Samw@Sun.COM return (IDMAP_GSID);
172712508Samw@Sun.COM default:
172812508Samw@Sun.COM return (type);
172912508Samw@Sun.COM }
173012508Samw@Sun.COM NOTE(NOTREACHED)
173112508Samw@Sun.COM }
173212508Samw@Sun.COM
17335696Snw141292 static
17345696Snw141292 idmap_retcode
lookup_cache_sid2name(sqlite * cache,const char * sidprefix,idmap_rid_t rid,char ** canonname,char ** canondomain,idmap_id_type * type)17354864Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid,
173612508Samw@Sun.COM char **canonname, char **canondomain, idmap_id_type *type)
17375696Snw141292 {
17384520Snw141292 char *end;
17394520Snw141292 char *sql = NULL;
17404520Snw141292 const char **values;
17414520Snw141292 sqlite_vm *vm = NULL;
17424520Snw141292 int ncol;
17434520Snw141292 time_t curtime;
17444520Snw141292 idmap_retcode retcode = IDMAP_SUCCESS;
17454520Snw141292
17464520Snw141292 /* Get current time */
17474520Snw141292 errno = 0;
17484520Snw141292 if ((curtime = time(NULL)) == (time_t)-1) {
17495696Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)",
17505696Snw141292 strerror(errno));
17514520Snw141292 retcode = IDMAP_ERR_INTERNAL;
17524520Snw141292 goto out;
17534520Snw141292 }
17544520Snw141292
17554520Snw141292 /* SQL to lookup the cache */
17565696Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type "
17575696Snw141292 "FROM name_cache WHERE "
17585696Snw141292 "sidprefix = %Q AND rid = %u AND "
17595696Snw141292 "(expiration = 0 OR expiration ISNULL OR "
17605696Snw141292 "expiration > %d);",
17615696Snw141292 sidprefix, rid, curtime);
17624520Snw141292 if (sql == NULL) {
17634520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
17644520Snw141292 retcode = IDMAP_ERR_MEMORY;
17654520Snw141292 goto out;
17664520Snw141292 }
17674520Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values);
17684520Snw141292 sqlite_freemem(sql);
17694520Snw141292
17704520Snw141292 if (retcode == IDMAP_SUCCESS) {
17714864Sbaban if (type != NULL) {
17724520Snw141292 if (values[2] == NULL) {
17734520Snw141292 retcode = IDMAP_ERR_CACHE;
17744520Snw141292 goto out;
17754520Snw141292 }
177612508Samw@Sun.COM *type = xlate_legacy_type(strtol(values[2], &end, 10));
17774520Snw141292 }
17784520Snw141292
17799422SAfshin.Ardakani@Sun.COM if (canonname != NULL && values[0] != NULL) {
17809422SAfshin.Ardakani@Sun.COM if ((*canonname = strdup(values[0])) == NULL) {
17814520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
17824520Snw141292 retcode = IDMAP_ERR_MEMORY;
17834520Snw141292 goto out;
17844520Snw141292 }
17854520Snw141292 }
17864520Snw141292
17879422SAfshin.Ardakani@Sun.COM if (canondomain != NULL && values[1] != NULL) {
17889422SAfshin.Ardakani@Sun.COM if ((*canondomain = strdup(values[1])) == NULL) {
17899422SAfshin.Ardakani@Sun.COM if (canonname != NULL) {
17909422SAfshin.Ardakani@Sun.COM free(*canonname);
17919422SAfshin.Ardakani@Sun.COM *canonname = NULL;
17924520Snw141292 }
17934520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
17944520Snw141292 retcode = IDMAP_ERR_MEMORY;
17954520Snw141292 goto out;
17964520Snw141292 }
17974520Snw141292 }
17984520Snw141292 }
17994520Snw141292
18004520Snw141292 out:
18014864Sbaban if (vm != NULL)
18024520Snw141292 (void) sqlite_finalize(vm, NULL);
18034520Snw141292 return (retcode);
18044520Snw141292 }
18054520Snw141292
18064520Snw141292 /*
18075731Sbaban * Given SID, find winname using name_cache OR
18085731Sbaban * Given winname, find SID using name_cache.
18095731Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and
18105731Sbaban * req->id2 is unix id
18114520Snw141292 */
18125696Snw141292 static
18135696Snw141292 idmap_retcode
lookup_name_cache(sqlite * cache,idmap_mapping * req,idmap_id_res * res)18145731Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res)
18155696Snw141292 {
181612508Samw@Sun.COM idmap_id_type type = -1;
18174520Snw141292 idmap_retcode retcode;
18185731Sbaban char *sidprefix = NULL;
18194520Snw141292 idmap_rid_t rid;
18204520Snw141292 char *name = NULL, *domain = NULL;
18214520Snw141292
18225731Sbaban /* Done if we've both sid and winname */
182312508Samw@Sun.COM if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) {
182412508Samw@Sun.COM /* Don't bother TRACE()ing, too boring */
18255731Sbaban return (IDMAP_SUCCESS);
182612508Samw@Sun.COM }
18275731Sbaban
18285731Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) {
18299422SAfshin.Ardakani@Sun.COM /* Lookup sid to winname */
18305731Sbaban retcode = lookup_cache_sid2name(cache,
18315731Sbaban req->id1.idmap_id_u.sid.prefix,
18325731Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type);
18339422SAfshin.Ardakani@Sun.COM } else {
18349422SAfshin.Ardakani@Sun.COM /* Lookup winame to sid */
18359422SAfshin.Ardakani@Sun.COM retcode = lookup_cache_name2sid(cache, req->id1name,
18369422SAfshin.Ardakani@Sun.COM req->id1domain, &name, &sidprefix, &rid, &type);
18375731Sbaban }
18385731Sbaban
18395731Sbaban if (retcode != IDMAP_SUCCESS) {
184012508Samw@Sun.COM if (retcode == IDMAP_ERR_NOTFOUND) {
184112508Samw@Sun.COM TRACE(req, res, "Not found in name cache");
184212508Samw@Sun.COM } else {
184312508Samw@Sun.COM TRACE(req, res, "Name cache lookup error=%d", retcode);
184412508Samw@Sun.COM }
18455731Sbaban free(name);
18465731Sbaban free(domain);
18475731Sbaban free(sidprefix);
18485731Sbaban return (retcode);
18495731Sbaban }
18505731Sbaban
185112508Samw@Sun.COM req->id1.idtype = type;
18525731Sbaban
18535731Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
18549422SAfshin.Ardakani@Sun.COM
18559422SAfshin.Ardakani@Sun.COM /*
18569422SAfshin.Ardakani@Sun.COM * If we found canonical names or domain, use them instead of
18579422SAfshin.Ardakani@Sun.COM * the existing values.
18589422SAfshin.Ardakani@Sun.COM */
18595731Sbaban if (name != NULL) {
18609422SAfshin.Ardakani@Sun.COM free(req->id1name);
18619422SAfshin.Ardakani@Sun.COM req->id1name = name;
18625731Sbaban }
18639422SAfshin.Ardakani@Sun.COM if (domain != NULL) {
18649422SAfshin.Ardakani@Sun.COM free(req->id1domain);
18655731Sbaban req->id1domain = domain;
18669422SAfshin.Ardakani@Sun.COM }
18679422SAfshin.Ardakani@Sun.COM
18685731Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) {
18695731Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix;
18705731Sbaban req->id1.idmap_id_u.sid.rid = rid;
18714520Snw141292 }
187212508Samw@Sun.COM
187312508Samw@Sun.COM TRACE(req, res, "Found in name cache");
18744520Snw141292 return (retcode);
18754520Snw141292 }
18764520Snw141292
18778361SJulian.Pullen@Sun.COM
18788361SJulian.Pullen@Sun.COM
18798361SJulian.Pullen@Sun.COM 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)18808361SJulian.Pullen@Sun.COM ad_lookup_batch_int(lookup_state_t *state, idmap_mapping_batch *batch,
188110504SKeyur.Desai@Sun.COM idmap_ids_res *result, adutils_ad_t *dir, int how_local,
188210504SKeyur.Desai@Sun.COM int *num_processed)
18835696Snw141292 {
18844520Snw141292 idmap_retcode retcode;
188510504SKeyur.Desai@Sun.COM int i, num_queued, is_wuser, is_user;
18868361SJulian.Pullen@Sun.COM int next_request;
188712508Samw@Sun.COM int retries = 0, esidtype;
18885731Sbaban char **unixname;
18894520Snw141292 idmap_mapping *req;
18904520Snw141292 idmap_id_res *res;
18915731Sbaban idmap_query_state_t *qs = NULL;
18926386Sjp151216 idmap_how *how;
18936616Sdm199847 char **dn, **attr, **value;
18945731Sbaban
18958361SJulian.Pullen@Sun.COM *num_processed = 0;
18968361SJulian.Pullen@Sun.COM
18975731Sbaban /*
18985731Sbaban * Since req->id2.idtype is unused, we will use it here
18995731Sbaban * to retrieve the value of sid_type. But it needs to be
19005731Sbaban * reset to IDMAP_NONE before we return to prevent xdr
19015731Sbaban * from mis-interpreting req->id2 when it tries to free
19025731Sbaban * the input argument. Other option is to allocate an
19035731Sbaban * array of integers and use it instead for the batched
19045731Sbaban * call. But why un-necessarily allocate memory. That may
19055731Sbaban * be an option if req->id2.idtype cannot be re-used in
19065731Sbaban * future.
190710504SKeyur.Desai@Sun.COM *
190811963SAfshin.Ardakani@Sun.COM * Similarly, we use req->id2.idmap_id_u.uid to return
190911963SAfshin.Ardakani@Sun.COM * uidNumber or gidNumber supplied by IDMU, and reset it
191011963SAfshin.Ardakani@Sun.COM * back to IDMAP_SENTINEL_PID when we're done. Note that
191111963SAfshin.Ardakani@Sun.COM * the query always puts the result in req->id2.idmap_id_u.uid,
191211963SAfshin.Ardakani@Sun.COM * not .gid.
19135731Sbaban */
19144520Snw141292 retry:
191510504SKeyur.Desai@Sun.COM retcode = idmap_lookup_batch_start(dir, state->ad_nqueries,
191610504SKeyur.Desai@Sun.COM state->directory_based_mapping,
191710504SKeyur.Desai@Sun.COM state->defdom,
191810504SKeyur.Desai@Sun.COM &qs);
19195731Sbaban if (retcode != IDMAP_SUCCESS) {
19208040SBaban.Kenkre@Sun.COM if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
19218040SBaban.Kenkre@Sun.COM retries++ < ADUTILS_DEF_NUM_RETRIES)
19225968Snw141292 goto retry;
19236097Snw141292 degrade_svc(1, "failed to create batch for AD lookup");
19248361SJulian.Pullen@Sun.COM goto out;
19254520Snw141292 }
19268361SJulian.Pullen@Sun.COM num_queued = 0;
19274520Snw141292
19285317Sjp151216 restore_svc();
19295317Sjp151216
193010504SKeyur.Desai@Sun.COM if (how_local & FOREST_IS_LOCAL) {
19318361SJulian.Pullen@Sun.COM /*
19328361SJulian.Pullen@Sun.COM * Directory based name mapping is only performed within the
193310504SKeyur.Desai@Sun.COM * joined forest. We don't trust other "trusted"
19348361SJulian.Pullen@Sun.COM * forests to provide DS-based name mapping information because
19358361SJulian.Pullen@Sun.COM * AD's definition of "cross-forest trust" does not encompass
19368361SJulian.Pullen@Sun.COM * this sort of behavior.
19378361SJulian.Pullen@Sun.COM */
19388361SJulian.Pullen@Sun.COM idmap_lookup_batch_set_unixattr(qs,
19398361SJulian.Pullen@Sun.COM state->ad_unixuser_attr, state->ad_unixgroup_attr);
19408361SJulian.Pullen@Sun.COM }
19418361SJulian.Pullen@Sun.COM
19428361SJulian.Pullen@Sun.COM for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
19434520Snw141292 req = &batch->idmap_mapping_batch_val[i];
19444520Snw141292 res = &result->ids.ids_val[i];
19456386Sjp151216 how = &res->info.how;
19466386Sjp151216
19475731Sbaban retcode = IDMAP_SUCCESS;
19485731Sbaban req->id2.idtype = IDMAP_NONE;
194911963SAfshin.Ardakani@Sun.COM req->id2.idmap_id_u.uid = IDMAP_SENTINEL_PID;
195010504SKeyur.Desai@Sun.COM
195110504SKeyur.Desai@Sun.COM /* Skip if no AD lookup required */
195210504SKeyur.Desai@Sun.COM if (!(req->direction & _IDMAP_F_LOOKUP_AD))
19535731Sbaban continue;
19545731Sbaban
195510504SKeyur.Desai@Sun.COM /* Skip if we've already tried and gotten a "not found" */
195610504SKeyur.Desai@Sun.COM if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD)
195710504SKeyur.Desai@Sun.COM continue;
195810504SKeyur.Desai@Sun.COM
195910504SKeyur.Desai@Sun.COM /* Skip if we've already either succeeded or failed */
19606963Sbaban if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR)
19615731Sbaban continue;
19625731Sbaban
196312508Samw@Sun.COM if (IS_ID_SID(req->id1)) {
19646616Sdm199847
19656616Sdm199847 /* win2unix request: */
19666616Sdm199847
196710504SKeyur.Desai@Sun.COM posix_id_t *pid = NULL;
19686616Sdm199847 unixname = dn = attr = value = NULL;
196912508Samw@Sun.COM esidtype = IDMAP_SID;
197010504SKeyur.Desai@Sun.COM if (state->directory_based_mapping ==
197110504SKeyur.Desai@Sun.COM DIRECTORY_MAPPING_NAME &&
197210504SKeyur.Desai@Sun.COM req->id2name == NULL) {
19735731Sbaban if (res->id.idtype == IDMAP_UID &&
19745731Sbaban AD_OR_MIXED(state->nm_siduid)) {
197512508Samw@Sun.COM esidtype = IDMAP_USID;
19765731Sbaban unixname = &req->id2name;
19775731Sbaban } else if (res->id.idtype == IDMAP_GID &&
19785731Sbaban AD_OR_MIXED(state->nm_sidgid)) {
197912508Samw@Sun.COM esidtype = IDMAP_GSID;
19805731Sbaban unixname = &req->id2name;
19815731Sbaban } else if (AD_OR_MIXED(state->nm_siduid) ||
19825731Sbaban AD_OR_MIXED(state->nm_sidgid)) {
19835731Sbaban unixname = &req->id2name;
19845731Sbaban }
198510504SKeyur.Desai@Sun.COM
198610504SKeyur.Desai@Sun.COM if (unixname != NULL) {
198710504SKeyur.Desai@Sun.COM /*
198810504SKeyur.Desai@Sun.COM * Get how info for DS-based name
198910504SKeyur.Desai@Sun.COM * mapping only if AD or MIXED
199010504SKeyur.Desai@Sun.COM * mode is enabled.
199110504SKeyur.Desai@Sun.COM */
199212508Samw@Sun.COM idmap_how_clear(&res->info.how);
199310504SKeyur.Desai@Sun.COM res->info.src = IDMAP_MAP_SRC_NEW;
199410504SKeyur.Desai@Sun.COM how->map_type = IDMAP_MAP_TYPE_DS_AD;
199510504SKeyur.Desai@Sun.COM dn = &how->idmap_how_u.ad.dn;
199610504SKeyur.Desai@Sun.COM attr = &how->idmap_how_u.ad.attr;
199710504SKeyur.Desai@Sun.COM value = &how->idmap_how_u.ad.value;
199810504SKeyur.Desai@Sun.COM }
199910504SKeyur.Desai@Sun.COM } else if (state->directory_based_mapping ==
200010504SKeyur.Desai@Sun.COM DIRECTORY_MAPPING_IDMU &&
200110504SKeyur.Desai@Sun.COM (how_local & DOMAIN_IS_LOCAL)) {
20026616Sdm199847 /*
200310504SKeyur.Desai@Sun.COM * Ensure that we only do IDMU processing
200410504SKeyur.Desai@Sun.COM * when querying the domain we've joined.
200510504SKeyur.Desai@Sun.COM */
200610504SKeyur.Desai@Sun.COM pid = &req->id2.idmap_id_u.uid;
200710504SKeyur.Desai@Sun.COM /*
200810504SKeyur.Desai@Sun.COM * Get how info for IDMU based mapping.
20096616Sdm199847 */
201012508Samw@Sun.COM idmap_how_clear(&res->info.how);
20116616Sdm199847 res->info.src = IDMAP_MAP_SRC_NEW;
201210504SKeyur.Desai@Sun.COM how->map_type = IDMAP_MAP_TYPE_IDMU;
201310504SKeyur.Desai@Sun.COM dn = &how->idmap_how_u.idmu.dn;
201410504SKeyur.Desai@Sun.COM attr = &how->idmap_how_u.idmu.attr;
201510504SKeyur.Desai@Sun.COM value = &how->idmap_how_u.idmu.value;
20166616Sdm199847 }
201710504SKeyur.Desai@Sun.COM
20186616Sdm199847 if (req->id1.idmap_id_u.sid.prefix != NULL) {
20196616Sdm199847 /* Lookup AD by SID */
20206616Sdm199847 retcode = idmap_sid2name_batch_add1(
20216616Sdm199847 qs, req->id1.idmap_id_u.sid.prefix,
202212508Samw@Sun.COM &req->id1.idmap_id_u.sid.rid, esidtype,
20236616Sdm199847 dn, attr, value,
20246616Sdm199847 (req->id1name == NULL) ?
20256616Sdm199847 &req->id1name : NULL,
20266616Sdm199847 (req->id1domain == NULL) ?
20276616Sdm199847 &req->id1domain : NULL,
202812508Samw@Sun.COM &req->id2.idtype, unixname,
202910504SKeyur.Desai@Sun.COM pid,
20306616Sdm199847 &res->retcode);
20318361SJulian.Pullen@Sun.COM if (retcode == IDMAP_SUCCESS)
20328361SJulian.Pullen@Sun.COM num_queued++;
20336616Sdm199847 } else {
20346616Sdm199847 /* Lookup AD by winname */
20356616Sdm199847 assert(req->id1name != NULL);
20366616Sdm199847 retcode = idmap_name2sid_batch_add1(
20376616Sdm199847 qs, req->id1name, req->id1domain,
203812508Samw@Sun.COM esidtype,
20396616Sdm199847 dn, attr, value,
20406616Sdm199847 &req->id1name,
20416616Sdm199847 &req->id1.idmap_id_u.sid.prefix,
20426616Sdm199847 &req->id1.idmap_id_u.sid.rid,
204312508Samw@Sun.COM &req->id2.idtype, unixname,
204410504SKeyur.Desai@Sun.COM pid,
20456616Sdm199847 &res->retcode);
20468361SJulian.Pullen@Sun.COM if (retcode == IDMAP_SUCCESS)
20478361SJulian.Pullen@Sun.COM num_queued++;
20486616Sdm199847 }
20495731Sbaban
205012508Samw@Sun.COM } else if (IS_ID_UID(req->id1) || IS_ID_GID(req->id1)) {
20516616Sdm199847
20526616Sdm199847 /* unix2win request: */
20535731Sbaban
20545731Sbaban if (res->id.idmap_id_u.sid.prefix != NULL &&
20555731Sbaban req->id2name != NULL) {
20568361SJulian.Pullen@Sun.COM /* Already have SID and winname. done */
20575731Sbaban res->retcode = IDMAP_SUCCESS;
20585731Sbaban continue;
20595731Sbaban }
20605731Sbaban
20615731Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) {
20625731Sbaban /*
20635731Sbaban * SID but no winname -- lookup AD by
20645731Sbaban * SID to get winname.
20656616Sdm199847 * how info is not needed here because
20666616Sdm199847 * we are not retrieving unixname from
20676616Sdm199847 * AD.
20685731Sbaban */
20698361SJulian.Pullen@Sun.COM
20705731Sbaban retcode = idmap_sid2name_batch_add1(
20715731Sbaban qs, res->id.idmap_id_u.sid.prefix,
20725731Sbaban &res->id.idmap_id_u.sid.rid,
207312508Samw@Sun.COM IDMAP_POSIXID,
20746616Sdm199847 NULL, NULL, NULL,
20756386Sjp151216 &req->id2name,
207612508Samw@Sun.COM &req->id2domain, &req->id2.idtype,
207710504SKeyur.Desai@Sun.COM NULL, NULL, &res->retcode);
20788361SJulian.Pullen@Sun.COM if (retcode == IDMAP_SUCCESS)
20798361SJulian.Pullen@Sun.COM num_queued++;
20805731Sbaban } else if (req->id2name != NULL) {
20815731Sbaban /*
20825731Sbaban * winname but no SID -- lookup AD by
20835731Sbaban * winname to get SID.
20846616Sdm199847 * how info is not needed here because
20856616Sdm199847 * we are not retrieving unixname from
20866616Sdm199847 * AD.
20875731Sbaban */
20885731Sbaban retcode = idmap_name2sid_batch_add1(
20895731Sbaban qs, req->id2name, req->id2domain,
209012508Samw@Sun.COM IDMAP_POSIXID,
20916616Sdm199847 NULL, NULL, NULL, NULL,
20925731Sbaban &res->id.idmap_id_u.sid.prefix,
20935731Sbaban &res->id.idmap_id_u.sid.rid,
209412508Samw@Sun.COM &req->id2.idtype, NULL,
209510504SKeyur.Desai@Sun.COM NULL,
20965731Sbaban &res->retcode);
20978361SJulian.Pullen@Sun.COM if (retcode == IDMAP_SUCCESS)
20988361SJulian.Pullen@Sun.COM num_queued++;
209910504SKeyur.Desai@Sun.COM } else if (state->directory_based_mapping ==
210010504SKeyur.Desai@Sun.COM DIRECTORY_MAPPING_IDMU &&
210110504SKeyur.Desai@Sun.COM (how_local & DOMAIN_IS_LOCAL)) {
210211963SAfshin.Ardakani@Sun.COM assert(req->id1.idmap_id_u.uid !=
210311963SAfshin.Ardakani@Sun.COM IDMAP_SENTINEL_PID);
210412508Samw@Sun.COM is_user = IS_ID_UID(req->id1);
210510504SKeyur.Desai@Sun.COM if (res->id.idtype == IDMAP_USID)
210610504SKeyur.Desai@Sun.COM is_wuser = 1;
210710504SKeyur.Desai@Sun.COM else if (res->id.idtype == IDMAP_GSID)
210810504SKeyur.Desai@Sun.COM is_wuser = 0;
210910504SKeyur.Desai@Sun.COM else
211010504SKeyur.Desai@Sun.COM is_wuser = is_user;
211110504SKeyur.Desai@Sun.COM
211210504SKeyur.Desai@Sun.COM /* IDMU can't do diagonal mappings */
211310504SKeyur.Desai@Sun.COM if (is_user != is_wuser)
211410504SKeyur.Desai@Sun.COM continue;
211510504SKeyur.Desai@Sun.COM
211612508Samw@Sun.COM idmap_how_clear(&res->info.how);
211710504SKeyur.Desai@Sun.COM res->info.src = IDMAP_MAP_SRC_NEW;
211810504SKeyur.Desai@Sun.COM how->map_type = IDMAP_MAP_TYPE_IDMU;
211910504SKeyur.Desai@Sun.COM retcode = idmap_pid2sid_batch_add1(
212010504SKeyur.Desai@Sun.COM qs, req->id1.idmap_id_u.uid, is_user,
212110504SKeyur.Desai@Sun.COM &how->idmap_how_u.ad.dn,
212210504SKeyur.Desai@Sun.COM &how->idmap_how_u.ad.attr,
212310504SKeyur.Desai@Sun.COM &how->idmap_how_u.ad.value,
212410504SKeyur.Desai@Sun.COM &res->id.idmap_id_u.sid.prefix,
212510504SKeyur.Desai@Sun.COM &res->id.idmap_id_u.sid.rid,
212610504SKeyur.Desai@Sun.COM &req->id2name, &req->id2domain,
212712508Samw@Sun.COM &req->id2.idtype, &res->retcode);
212810504SKeyur.Desai@Sun.COM if (retcode == IDMAP_SUCCESS)
212910504SKeyur.Desai@Sun.COM num_queued++;
21305731Sbaban } else if (req->id1name != NULL) {
21315731Sbaban /*
21328361SJulian.Pullen@Sun.COM * No SID and no winname but we've unixname.
21338361SJulian.Pullen@Sun.COM * Lookup AD by unixname to get SID.
21345731Sbaban */
213512508Samw@Sun.COM is_user = (IS_ID_UID(req->id1)) ? 1 : 0;
21365731Sbaban if (res->id.idtype == IDMAP_USID)
21375731Sbaban is_wuser = 1;
21385731Sbaban else if (res->id.idtype == IDMAP_GSID)
21395731Sbaban is_wuser = 0;
21405731Sbaban else
21415731Sbaban is_wuser = is_user;
21428361SJulian.Pullen@Sun.COM
214312508Samw@Sun.COM idmap_how_clear(&res->info.how);
21446386Sjp151216 res->info.src = IDMAP_MAP_SRC_NEW;
21456386Sjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD;
21465731Sbaban retcode = idmap_unixname2sid_batch_add1(
21475731Sbaban qs, req->id1name, is_user, is_wuser,
21486386Sjp151216 &how->idmap_how_u.ad.dn,
21496386Sjp151216 &how->idmap_how_u.ad.attr,
21506386Sjp151216 &how->idmap_how_u.ad.value,
21515731Sbaban &res->id.idmap_id_u.sid.prefix,
21525731Sbaban &res->id.idmap_id_u.sid.rid,
21535731Sbaban &req->id2name, &req->id2domain,
215412508Samw@Sun.COM &req->id2.idtype, &res->retcode);
21558361SJulian.Pullen@Sun.COM if (retcode == IDMAP_SUCCESS)
21568361SJulian.Pullen@Sun.COM num_queued++;
21575731Sbaban }
21585731Sbaban }
21598361SJulian.Pullen@Sun.COM
21608361SJulian.Pullen@Sun.COM if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
21618361SJulian.Pullen@Sun.COM req->direction |= _IDMAP_F_LOOKUP_OTHER_AD;
21628361SJulian.Pullen@Sun.COM retcode = IDMAP_SUCCESS;
21638361SJulian.Pullen@Sun.COM } else if (retcode != IDMAP_SUCCESS) {
21645731Sbaban break;
21654520Snw141292 }
21668361SJulian.Pullen@Sun.COM } /* End of for loop */
21674520Snw141292
21686963Sbaban if (retcode == IDMAP_SUCCESS) {
21696963Sbaban /* add keeps track if we added an entry to the batch */
21708361SJulian.Pullen@Sun.COM if (num_queued > 0)
21716963Sbaban retcode = idmap_lookup_batch_end(&qs);
21726963Sbaban else
21736963Sbaban idmap_lookup_release_batch(&qs);
217410504SKeyur.Desai@Sun.COM } else {
217510504SKeyur.Desai@Sun.COM idmap_lookup_release_batch(&qs);
217610504SKeyur.Desai@Sun.COM num_queued = 0;
217710504SKeyur.Desai@Sun.COM next_request = i + 1;
21786963Sbaban }
21795696Snw141292
21808040SBaban.Kenkre@Sun.COM if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
21818040SBaban.Kenkre@Sun.COM retries++ < ADUTILS_DEF_NUM_RETRIES)
21824520Snw141292 goto retry;
21835317Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
21846097Snw141292 degrade_svc(1, "some AD lookups timed out repeatedly");
21854520Snw141292
21868361SJulian.Pullen@Sun.COM if (retcode != IDMAP_SUCCESS) {
21878361SJulian.Pullen@Sun.COM /* Mark any unproccessed requests for an other AD */
21888361SJulian.Pullen@Sun.COM for (i = next_request; i < batch->idmap_mapping_batch_len;
21898361SJulian.Pullen@Sun.COM i++) {
21908361SJulian.Pullen@Sun.COM req = &batch->idmap_mapping_batch_val[i];
21918361SJulian.Pullen@Sun.COM req->direction |= _IDMAP_F_LOOKUP_OTHER_AD;
21928361SJulian.Pullen@Sun.COM
21938361SJulian.Pullen@Sun.COM }
21948361SJulian.Pullen@Sun.COM }
21958361SJulian.Pullen@Sun.COM
21965731Sbaban if (retcode != IDMAP_SUCCESS)
21975731Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests");
21984520Snw141292
21994520Snw141292 out:
22005731Sbaban /*
22015731Sbaban * This loop does the following:
22026616Sdm199847 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request.
22036616Sdm199847 * 2. Reset req->id2.idtype to IDMAP_NONE
22046616Sdm199847 * 3. If batch_start or batch_add failed then set the status
22056616Sdm199847 * of each request marked for AD lookup to that error.
22068361SJulian.Pullen@Sun.COM * 4. Evaluate the type of the AD object (i.e. user or group)
22078361SJulian.Pullen@Sun.COM * and update the idtype in request.
22085731Sbaban */
22095731Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
221012508Samw@Sun.COM idmap_id_type type;
221110504SKeyur.Desai@Sun.COM uid_t posix_id;
221210504SKeyur.Desai@Sun.COM
22135731Sbaban req = &batch->idmap_mapping_batch_val[i];
22145731Sbaban type = req->id2.idtype;
22155731Sbaban req->id2.idtype = IDMAP_NONE;
221610504SKeyur.Desai@Sun.COM posix_id = req->id2.idmap_id_u.uid;
221711963SAfshin.Ardakani@Sun.COM req->id2.idmap_id_u.uid = IDMAP_SENTINEL_PID;
22185746Sbaban res = &result->ids.ids_val[i];
221910504SKeyur.Desai@Sun.COM
222010504SKeyur.Desai@Sun.COM /*
222110504SKeyur.Desai@Sun.COM * If it didn't need AD lookup, ignore it.
222210504SKeyur.Desai@Sun.COM */
222310504SKeyur.Desai@Sun.COM if (!(req->direction & _IDMAP_F_LOOKUP_AD))
22245731Sbaban continue;
22255731Sbaban
222610504SKeyur.Desai@Sun.COM /*
222710504SKeyur.Desai@Sun.COM * If we deferred it this time, reset for the next
222810504SKeyur.Desai@Sun.COM * AD server.
222910504SKeyur.Desai@Sun.COM */
223010504SKeyur.Desai@Sun.COM if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD) {
223110504SKeyur.Desai@Sun.COM req->direction &= ~_IDMAP_F_LOOKUP_OTHER_AD;
223210504SKeyur.Desai@Sun.COM continue;
223310504SKeyur.Desai@Sun.COM }
223410504SKeyur.Desai@Sun.COM
22358361SJulian.Pullen@Sun.COM /* Count number processed */
22368361SJulian.Pullen@Sun.COM (*num_processed)++;
22378361SJulian.Pullen@Sun.COM
22386616Sdm199847 /* Reset AD lookup flag */
22396616Sdm199847 req->direction &= ~(_IDMAP_F_LOOKUP_AD);
22406616Sdm199847
22416616Sdm199847 /*
22428361SJulian.Pullen@Sun.COM * If batch_start or batch_add failed then set the
22438361SJulian.Pullen@Sun.COM * status of each request marked for AD lookup to
22448361SJulian.Pullen@Sun.COM * that error.
22456616Sdm199847 */
22465731Sbaban if (retcode != IDMAP_SUCCESS) {
22475731Sbaban res->retcode = retcode;
22485731Sbaban continue;
22495731Sbaban }
22505731Sbaban
22516386Sjp151216 if (res->retcode == IDMAP_ERR_NOTFOUND) {
22526386Sjp151216 /* Nothing found - remove the preset info */
225312508Samw@Sun.COM idmap_how_clear(&res->info.how);
22546386Sjp151216 }
22556386Sjp151216
225612508Samw@Sun.COM if (IS_ID_SID(req->id1)) {
2257*12890SJoyce.McIntosh@Sun.COM if (res->retcode == IDMAP_ERR_NOTFOUND) {
2258*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "Not found in AD");
22595731Sbaban continue;
2260*12890SJoyce.McIntosh@Sun.COM }
2261*12890SJoyce.McIntosh@Sun.COM if (res->retcode != IDMAP_SUCCESS) {
2262*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "AD lookup error=%d",
2263*12890SJoyce.McIntosh@Sun.COM res->retcode);
2264*12890SJoyce.McIntosh@Sun.COM continue;
2265*12890SJoyce.McIntosh@Sun.COM }
22666616Sdm199847 /* Evaluate result type */
22675731Sbaban switch (type) {
226812508Samw@Sun.COM case IDMAP_USID:
22695731Sbaban if (res->id.idtype == IDMAP_POSIXID)
22705731Sbaban res->id.idtype = IDMAP_UID;
227110504SKeyur.Desai@Sun.COM /*
227210504SKeyur.Desai@Sun.COM * We found a user. If we got information
227310504SKeyur.Desai@Sun.COM * from IDMU and we were expecting a user,
227410504SKeyur.Desai@Sun.COM * copy the id.
227510504SKeyur.Desai@Sun.COM */
227611963SAfshin.Ardakani@Sun.COM if (posix_id != IDMAP_SENTINEL_PID &&
227710504SKeyur.Desai@Sun.COM res->id.idtype == IDMAP_UID) {
227810504SKeyur.Desai@Sun.COM res->id.idmap_id_u.uid = posix_id;
227910504SKeyur.Desai@Sun.COM res->direction = IDMAP_DIRECTION_BI;
228010504SKeyur.Desai@Sun.COM res->info.how.map_type =
228110504SKeyur.Desai@Sun.COM IDMAP_MAP_TYPE_IDMU;
228210504SKeyur.Desai@Sun.COM res->info.src = IDMAP_MAP_SRC_NEW;
228310504SKeyur.Desai@Sun.COM }
22845731Sbaban req->id1.idtype = IDMAP_USID;
22855731Sbaban break;
22868361SJulian.Pullen@Sun.COM
228712508Samw@Sun.COM case IDMAP_GSID:
22885731Sbaban if (res->id.idtype == IDMAP_POSIXID)
22895731Sbaban res->id.idtype = IDMAP_GID;
229010504SKeyur.Desai@Sun.COM /*
229110504SKeyur.Desai@Sun.COM * We found a group. If we got information
229210504SKeyur.Desai@Sun.COM * from IDMU and we were expecting a group,
229310504SKeyur.Desai@Sun.COM * copy the id.
229410504SKeyur.Desai@Sun.COM */
229511963SAfshin.Ardakani@Sun.COM if (posix_id != IDMAP_SENTINEL_PID &&
229610504SKeyur.Desai@Sun.COM res->id.idtype == IDMAP_GID) {
229710504SKeyur.Desai@Sun.COM res->id.idmap_id_u.gid = posix_id;
229810504SKeyur.Desai@Sun.COM res->direction = IDMAP_DIRECTION_BI;
229910504SKeyur.Desai@Sun.COM res->info.how.map_type =
230010504SKeyur.Desai@Sun.COM IDMAP_MAP_TYPE_IDMU;
230110504SKeyur.Desai@Sun.COM res->info.src = IDMAP_MAP_SRC_NEW;
230210504SKeyur.Desai@Sun.COM }
23035731Sbaban req->id1.idtype = IDMAP_GSID;
23045731Sbaban break;
23058361SJulian.Pullen@Sun.COM
23065731Sbaban default:
23075731Sbaban res->retcode = IDMAP_ERR_SID;
23085731Sbaban break;
23095731Sbaban }
2310*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "Found in AD");
23116616Sdm199847 if (res->retcode == IDMAP_SUCCESS &&
23126616Sdm199847 req->id1name != NULL &&
23136616Sdm199847 (req->id2name == NULL ||
231411963SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID) &&
23156616Sdm199847 NLDAP_MODE(res->id.idtype, state)) {
23166616Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP;
23176616Sdm199847 state->nldap_nqueries++;
23186616Sdm199847 }
231912508Samw@Sun.COM } else if (IS_ID_UID(req->id1) || IS_ID_GID(req->id1)) {
23205731Sbaban if (res->retcode != IDMAP_SUCCESS) {
23215731Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) &&
23225731Sbaban res->id.idmap_id_u.sid.prefix == NULL &&
2323*12890SJoyce.McIntosh@Sun.COM req->id2name == NULL) {
23245731Sbaban /*
232510504SKeyur.Desai@Sun.COM * If AD lookup by unixname or pid
23268361SJulian.Pullen@Sun.COM * failed with non fatal error
23278361SJulian.Pullen@Sun.COM * then clear the error (ie set
23288361SJulian.Pullen@Sun.COM * res->retcode to success).
23298361SJulian.Pullen@Sun.COM * This allows the next pass to
23308361SJulian.Pullen@Sun.COM * process other mapping
23316616Sdm199847 * mechanisms for this request.
23325731Sbaban */
2333*12890SJoyce.McIntosh@Sun.COM if (res->retcode ==
2334*12890SJoyce.McIntosh@Sun.COM IDMAP_ERR_NOTFOUND) {
2335*12890SJoyce.McIntosh@Sun.COM /* This is not an error */
2336*12890SJoyce.McIntosh@Sun.COM res->retcode = IDMAP_SUCCESS;
2337*12890SJoyce.McIntosh@Sun.COM TRACE(req, res,
2338*12890SJoyce.McIntosh@Sun.COM "Not found in AD");
2339*12890SJoyce.McIntosh@Sun.COM } else {
2340*12890SJoyce.McIntosh@Sun.COM TRACE(req, res,
2341*12890SJoyce.McIntosh@Sun.COM "AD lookup error (ignored)");
2342*12890SJoyce.McIntosh@Sun.COM res->retcode = IDMAP_SUCCESS;
2343*12890SJoyce.McIntosh@Sun.COM }
2344*12890SJoyce.McIntosh@Sun.COM } else {
2345*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "AD lookup error");
2346*12890SJoyce.McIntosh@Sun.COM }
23475731Sbaban continue;
23485731Sbaban }
23496616Sdm199847 /* Evaluate result type */
23505731Sbaban switch (type) {
235112508Samw@Sun.COM case IDMAP_USID:
235212508Samw@Sun.COM case IDMAP_GSID:
23535731Sbaban if (res->id.idtype == IDMAP_SID)
235412508Samw@Sun.COM res->id.idtype = type;
23555731Sbaban break;
23568361SJulian.Pullen@Sun.COM
23575731Sbaban default:
23585731Sbaban res->retcode = IDMAP_ERR_SID;
23595731Sbaban break;
23605731Sbaban }
2361*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "Found in AD");
23625731Sbaban }
23635731Sbaban }
23645731Sbaban
23658361SJulian.Pullen@Sun.COM return (retcode);
23668361SJulian.Pullen@Sun.COM }
23678361SJulian.Pullen@Sun.COM
23688361SJulian.Pullen@Sun.COM
23698361SJulian.Pullen@Sun.COM
23708361SJulian.Pullen@Sun.COM /*
23718361SJulian.Pullen@Sun.COM * Batch AD lookups
23728361SJulian.Pullen@Sun.COM */
23738361SJulian.Pullen@Sun.COM idmap_retcode
ad_lookup_batch(lookup_state_t * state,idmap_mapping_batch * batch,idmap_ids_res * result)23748361SJulian.Pullen@Sun.COM ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch,
23758361SJulian.Pullen@Sun.COM idmap_ids_res *result)
23768361SJulian.Pullen@Sun.COM {
23778361SJulian.Pullen@Sun.COM idmap_retcode retcode;
23788361SJulian.Pullen@Sun.COM int i, j;
23798361SJulian.Pullen@Sun.COM idmap_mapping *req;
23808361SJulian.Pullen@Sun.COM idmap_id_res *res;
23818361SJulian.Pullen@Sun.COM int num_queries;
23828361SJulian.Pullen@Sun.COM int num_processed;
23838361SJulian.Pullen@Sun.COM
23848361SJulian.Pullen@Sun.COM if (state->ad_nqueries == 0)
23858361SJulian.Pullen@Sun.COM return (IDMAP_SUCCESS);
23868361SJulian.Pullen@Sun.COM
23878361SJulian.Pullen@Sun.COM for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
23888361SJulian.Pullen@Sun.COM req = &batch->idmap_mapping_batch_val[i];
23898361SJulian.Pullen@Sun.COM res = &result->ids.ids_val[i];
23908361SJulian.Pullen@Sun.COM
23918361SJulian.Pullen@Sun.COM /* Skip if not marked for AD lookup or already in error. */
23928361SJulian.Pullen@Sun.COM if (!(req->direction & _IDMAP_F_LOOKUP_AD) ||
23938361SJulian.Pullen@Sun.COM res->retcode != IDMAP_SUCCESS)
23948361SJulian.Pullen@Sun.COM continue;
23958361SJulian.Pullen@Sun.COM
23968361SJulian.Pullen@Sun.COM /* Init status */
23978361SJulian.Pullen@Sun.COM res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR;
23988361SJulian.Pullen@Sun.COM }
23998361SJulian.Pullen@Sun.COM
24008361SJulian.Pullen@Sun.COM RDLOCK_CONFIG();
24018361SJulian.Pullen@Sun.COM num_queries = state->ad_nqueries;
240210504SKeyur.Desai@Sun.COM
240310504SKeyur.Desai@Sun.COM if (_idmapdstate.num_gcs == 0 && _idmapdstate.num_dcs == 0) {
24048361SJulian.Pullen@Sun.COM /* Case of no ADs */
24058361SJulian.Pullen@Sun.COM retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
24068361SJulian.Pullen@Sun.COM for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
24078361SJulian.Pullen@Sun.COM req = &batch->idmap_mapping_batch_val[i];
24088361SJulian.Pullen@Sun.COM res = &result->ids.ids_val[i];
24098361SJulian.Pullen@Sun.COM if (!(req->direction & _IDMAP_F_LOOKUP_AD))
24108361SJulian.Pullen@Sun.COM continue;
24118361SJulian.Pullen@Sun.COM req->direction &= ~(_IDMAP_F_LOOKUP_AD);
24128361SJulian.Pullen@Sun.COM res->retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
24138361SJulian.Pullen@Sun.COM }
241410504SKeyur.Desai@Sun.COM goto out;
24158361SJulian.Pullen@Sun.COM }
241610504SKeyur.Desai@Sun.COM
241710504SKeyur.Desai@Sun.COM if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) {
241810504SKeyur.Desai@Sun.COM for (i = 0; i < _idmapdstate.num_dcs && num_queries > 0; i++) {
241910504SKeyur.Desai@Sun.COM
242010504SKeyur.Desai@Sun.COM retcode = ad_lookup_batch_int(state, batch,
242110504SKeyur.Desai@Sun.COM result, _idmapdstate.dcs[i],
242210504SKeyur.Desai@Sun.COM i == 0 ? DOMAIN_IS_LOCAL|FOREST_IS_LOCAL : 0,
242310504SKeyur.Desai@Sun.COM &num_processed);
242410504SKeyur.Desai@Sun.COM num_queries -= num_processed;
242510504SKeyur.Desai@Sun.COM
242610504SKeyur.Desai@Sun.COM }
242710504SKeyur.Desai@Sun.COM }
242810504SKeyur.Desai@Sun.COM
242910504SKeyur.Desai@Sun.COM for (i = 0; i < _idmapdstate.num_gcs && num_queries > 0; i++) {
243010504SKeyur.Desai@Sun.COM
243110504SKeyur.Desai@Sun.COM retcode = ad_lookup_batch_int(state, batch, result,
243210504SKeyur.Desai@Sun.COM _idmapdstate.gcs[i],
243310504SKeyur.Desai@Sun.COM i == 0 ? FOREST_IS_LOCAL : 0,
243410504SKeyur.Desai@Sun.COM &num_processed);
243510504SKeyur.Desai@Sun.COM num_queries -= num_processed;
243610504SKeyur.Desai@Sun.COM
243710504SKeyur.Desai@Sun.COM }
243810504SKeyur.Desai@Sun.COM
243910504SKeyur.Desai@Sun.COM /*
244010504SKeyur.Desai@Sun.COM * There are no more ADs to try. Return errors for any
244110504SKeyur.Desai@Sun.COM * remaining requests.
244210504SKeyur.Desai@Sun.COM */
244310504SKeyur.Desai@Sun.COM if (num_queries > 0) {
244410504SKeyur.Desai@Sun.COM for (j = 0; j < batch->idmap_mapping_batch_len; j++) {
244510504SKeyur.Desai@Sun.COM req = &batch->idmap_mapping_batch_val[j];
244610504SKeyur.Desai@Sun.COM res = &result->ids.ids_val[j];
244710504SKeyur.Desai@Sun.COM if (!(req->direction & _IDMAP_F_LOOKUP_AD))
244810504SKeyur.Desai@Sun.COM continue;
244910504SKeyur.Desai@Sun.COM req->direction &= ~(_IDMAP_F_LOOKUP_AD);
245010504SKeyur.Desai@Sun.COM res->retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
245110504SKeyur.Desai@Sun.COM }
245210504SKeyur.Desai@Sun.COM }
245310504SKeyur.Desai@Sun.COM
245410504SKeyur.Desai@Sun.COM out:
24558361SJulian.Pullen@Sun.COM UNLOCK_CONFIG();
24568361SJulian.Pullen@Sun.COM
24575731Sbaban /* AD lookups done. Reset state->ad_nqueries and return */
24585731Sbaban state->ad_nqueries = 0;
24594520Snw141292 return (retcode);
24604520Snw141292 }
24614520Snw141292
24625696Snw141292 /*
24635696Snw141292 * Convention when processing win2unix requests:
24645696Snw141292 *
24655696Snw141292 * Windows identity:
24665696Snw141292 * req->id1name =
24675696Snw141292 * winname if given otherwise winname found will be placed
24685696Snw141292 * here.
24695696Snw141292 * req->id1domain =
24705696Snw141292 * windomain if given otherwise windomain found will be
24715696Snw141292 * placed here.
24725696Snw141292 * req->id1.idtype =
24735696Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll
24745696Snw141292 * be set to IDMAP_USID/GSID depending upon whether the
24755696Snw141292 * given SID is user or group respectively. The user/group-ness
24765696Snw141292 * is determined either when looking up well-known SIDs table OR
247711337SWilliam.Krier@Sun.COM * if the SID is found in namecache OR by ad_lookup_batch().
24785696Snw141292 * req->id1..sid.[prefix, rid] =
24795696Snw141292 * SID if given otherwise SID found will be placed here.
24805696Snw141292 *
24815696Snw141292 * Unix identity:
24825696Snw141292 * req->id2name =
24835696Snw141292 * unixname found will be placed here.
24845696Snw141292 * req->id2domain =
24855696Snw141292 * NOT USED
24865696Snw141292 * res->id.idtype =
24875696Snw141292 * Target type initialized from req->id2.idtype. If
24885696Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found
24895696Snw141292 * will be placed here.
24905696Snw141292 * res->id..[uid or gid] =
24915696Snw141292 * UID/GID found will be placed here.
24925696Snw141292 *
24935696Snw141292 * Others:
24945696Snw141292 * res->retcode =
24955696Snw141292 * Return status for this request will be placed here.
24965696Snw141292 * res->direction =
24975696Snw141292 * Direction found will be placed here. Direction
24985696Snw141292 * meaning whether the resultant mapping is valid
24995696Snw141292 * only from win2unix or bi-directional.
25005696Snw141292 * req->direction =
25015696Snw141292 * INTERNAL USE. Used by idmapd to set various
25025696Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing
25035696Snw141292 * of the request.
25045696Snw141292 * req->id2.idtype =
25055696Snw141292 * INTERNAL USE. Initially this is the requested target
25065696Snw141292 * type and is used to initialize res->id.idtype.
25075696Snw141292 * ad_lookup_batch() uses this field temporarily to store
25085696Snw141292 * sid_type obtained by the batched AD lookups and after
25095696Snw141292 * use resets it to IDMAP_NONE to prevent xdr from
25105696Snw141292 * mis-interpreting the contents of req->id2.
251110504SKeyur.Desai@Sun.COM * req->id2.idmap_id_u.uid =
251210504SKeyur.Desai@Sun.COM * INTERNAL USE. If the AD lookup finds IDMU data
251310504SKeyur.Desai@Sun.COM * (uidNumber or gidNumber, depending on the type of
251410504SKeyur.Desai@Sun.COM * the entry), it's left here.
25155696Snw141292 */
25165696Snw141292
25175696Snw141292 /*
25185696Snw141292 * This function does the following:
25195696Snw141292 * 1. Lookup well-known SIDs table.
25205696Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it.
25215696Snw141292 * 3. Lookup cache.
25225696Snw141292 * 4. Check if the client does not want new mapping to be allocated
25235696Snw141292 * in which case this pass is the final pass.
25245696Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs
25255696Snw141292 * to do AD lookup.
25265696Snw141292 */
25274520Snw141292 idmap_retcode
sid2pid_first_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)25286616Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req,
25295696Snw141292 idmap_id_res *res)
25305696Snw141292 {
25314520Snw141292 idmap_retcode retcode;
25325731Sbaban int wksid;
25335731Sbaban
25345731Sbaban /* Initialize result */
25355731Sbaban res->id.idtype = req->id2.idtype;
253611963SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.uid = IDMAP_SENTINEL_PID;
25375731Sbaban res->direction = IDMAP_DIRECTION_UNDEF;
25385731Sbaban wksid = 0;
25394520Snw141292
25405127Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) {
254111337SWilliam.Krier@Sun.COM /* They have to give us *something* to work with! */
25425731Sbaban if (req->id1name == NULL) {
25435731Sbaban retcode = IDMAP_ERR_ARG;
25445731Sbaban goto out;
25455731Sbaban }
254611337SWilliam.Krier@Sun.COM
25475731Sbaban /* sanitize sidprefix */
25485731Sbaban free(req->id1.idmap_id_u.sid.prefix);
25495731Sbaban req->id1.idmap_id_u.sid.prefix = NULL;
255011337SWilliam.Krier@Sun.COM
255111337SWilliam.Krier@Sun.COM /* Allow for a fully-qualified name in the "name" parameter */
255211337SWilliam.Krier@Sun.COM if (req->id1domain == NULL) {
255311337SWilliam.Krier@Sun.COM char *p;
255411337SWilliam.Krier@Sun.COM p = strchr(req->id1name, '@');
255511337SWilliam.Krier@Sun.COM if (p != NULL) {
255611337SWilliam.Krier@Sun.COM char *q;
255711337SWilliam.Krier@Sun.COM q = req->id1name;
2558*12890SJoyce.McIntosh@Sun.COM req->id1name = uu_strndup(q, p - req->id1name);
255911337SWilliam.Krier@Sun.COM req->id1domain = strdup(p+1);
256011337SWilliam.Krier@Sun.COM free(q);
256111337SWilliam.Krier@Sun.COM if (req->id1name == NULL ||
256211337SWilliam.Krier@Sun.COM req->id1domain == NULL) {
256311337SWilliam.Krier@Sun.COM retcode = IDMAP_ERR_MEMORY;
256411337SWilliam.Krier@Sun.COM goto out;
256511337SWilliam.Krier@Sun.COM }
256611337SWilliam.Krier@Sun.COM }
256711337SWilliam.Krier@Sun.COM }
25684520Snw141292 }
25695731Sbaban
25705731Sbaban /* Lookup well-known SIDs table */
25715731Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid);
257212508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
257312508Samw@Sun.COM /* Found a well-known account with a hardwired mapping */
257412508Samw@Sun.COM TRACE(req, res, "Hardwired mapping");
257512508Samw@Sun.COM goto out;
257612508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
257712508Samw@Sun.COM TRACE(req, res,
257812508Samw@Sun.COM "Well-known account lookup failed, code %d", retcode);
25794520Snw141292 goto out;
258012508Samw@Sun.COM }
258112508Samw@Sun.COM
258212508Samw@Sun.COM if (wksid) {
258312508Samw@Sun.COM /* Found a well-known account, but no mapping */
258412508Samw@Sun.COM TRACE(req, res, "Well-known account");
258512508Samw@Sun.COM } else {
258612508Samw@Sun.COM TRACE(req, res, "Not a well-known account");
258712508Samw@Sun.COM
25888040SBaban.Kenkre@Sun.COM /* Check if this is a localsid */
25895731Sbaban retcode = lookup_localsid2pid(req, res);
259012508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
259112508Samw@Sun.COM TRACE(req, res, "Local SID");
25925731Sbaban goto out;
259312508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
259412508Samw@Sun.COM TRACE(req, res,
259512508Samw@Sun.COM "Local SID lookup error=%d", retcode);
259612508Samw@Sun.COM goto out;
259712508Samw@Sun.COM }
259812508Samw@Sun.COM TRACE(req, res, "Not a local SID");
25998040SBaban.Kenkre@Sun.COM
26008040SBaban.Kenkre@Sun.COM if (ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)) {
26018361SJulian.Pullen@Sun.COM retcode = IDMAP_ERR_NONE_GENERATED;
26028040SBaban.Kenkre@Sun.COM goto out;
26038040SBaban.Kenkre@Sun.COM }
26045731Sbaban }
26055731Sbaban
260611337SWilliam.Krier@Sun.COM /*
260711337SWilliam.Krier@Sun.COM * If this is a name-based request and we don't have a domain,
260811337SWilliam.Krier@Sun.COM * use the default domain. Note that the well-known identity
260911337SWilliam.Krier@Sun.COM * cases will have supplied a SID prefix already, and that we
261011337SWilliam.Krier@Sun.COM * don't (yet?) support looking up a local user through a Windows
261111337SWilliam.Krier@Sun.COM * style name.
261211337SWilliam.Krier@Sun.COM */
261311337SWilliam.Krier@Sun.COM if (req->id1.idmap_id_u.sid.prefix == NULL &&
261411337SWilliam.Krier@Sun.COM req->id1name != NULL && req->id1domain == NULL) {
261511337SWilliam.Krier@Sun.COM if (state->defdom == NULL) {
261611337SWilliam.Krier@Sun.COM retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
261711337SWilliam.Krier@Sun.COM goto out;
261811337SWilliam.Krier@Sun.COM }
261911337SWilliam.Krier@Sun.COM req->id1domain = strdup(state->defdom);
262011337SWilliam.Krier@Sun.COM if (req->id1domain == NULL) {
262111337SWilliam.Krier@Sun.COM retcode = IDMAP_ERR_MEMORY;
262211337SWilliam.Krier@Sun.COM goto out;
262311337SWilliam.Krier@Sun.COM }
262412508Samw@Sun.COM TRACE(req, res, "Added default domain");
262511337SWilliam.Krier@Sun.COM }
262611337SWilliam.Krier@Sun.COM
26275731Sbaban /* Lookup cache */
26286616Sdm199847 retcode = lookup_cache_sid2pid(state->cache, req, res);
262912508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
263012508Samw@Sun.COM TRACE(req, res, "Found in mapping cache");
26314520Snw141292 goto out;
263212508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
263312508Samw@Sun.COM TRACE(req, res, "Mapping cache lookup error=%d", retcode);
263412508Samw@Sun.COM goto out;
263512508Samw@Sun.COM }
263612508Samw@Sun.COM TRACE(req, res, "Not found in mapping cache");
26374520Snw141292
26384520Snw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) {
26398361SJulian.Pullen@Sun.COM retcode = IDMAP_ERR_NONE_GENERATED;
26404520Snw141292 goto out;
26414520Snw141292 }
26424520Snw141292
26434520Snw141292 /*
26445731Sbaban * Failed to find non-expired entry in cache. Next step is
26455731Sbaban * to determine if this request needs to be batched for AD lookup.
26465731Sbaban *
26475731Sbaban * At this point we have either sid or winname or both. If we don't
26485731Sbaban * have both then lookup name_cache for the sid or winname
26495731Sbaban * whichever is missing. If not found then this request will be
26505731Sbaban * batched for AD lookup.
26514520Snw141292 */
26526616Sdm199847 retcode = lookup_name_cache(state->cache, req, res);
265312508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
265412508Samw@Sun.COM if (res->id.idtype == IDMAP_POSIXID) {
265512508Samw@Sun.COM if (req->id1.idtype == IDMAP_USID)
265612508Samw@Sun.COM res->id.idtype = IDMAP_UID;
265712508Samw@Sun.COM else
265812508Samw@Sun.COM res->id.idtype = IDMAP_GID;
265912508Samw@Sun.COM }
266012508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND)
26615731Sbaban goto out;
26624520Snw141292
266312508Samw@Sun.COM if (_idmapdstate.cfg->pgcfg.use_lsa &&
266412508Samw@Sun.COM _idmapdstate.cfg->pgcfg.domain_name != NULL) {
266512508Samw@Sun.COM /*
266612508Samw@Sun.COM * If we don't have both name and SID, try looking up the
266712508Samw@Sun.COM * entry with LSA.
266812508Samw@Sun.COM */
266912508Samw@Sun.COM if (req->id1.idmap_id_u.sid.prefix != NULL &&
267012508Samw@Sun.COM req->id1name == NULL) {
267112508Samw@Sun.COM
267212508Samw@Sun.COM retcode = lookup_lsa_by_sid(
267312508Samw@Sun.COM req->id1.idmap_id_u.sid.prefix,
267412508Samw@Sun.COM req->id1.idmap_id_u.sid.rid,
267512508Samw@Sun.COM &req->id1name, &req->id1domain, &req->id1.idtype);
267612508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
267712508Samw@Sun.COM TRACE(req, res, "Found with LSA");
267812508Samw@Sun.COM } else if (retcode == IDMAP_ERR_NOTFOUND) {
267912508Samw@Sun.COM TRACE(req, res, "Not found with LSA");
268012508Samw@Sun.COM } else {
268112508Samw@Sun.COM TRACE(req, res, "LSA error %d", retcode);
268212508Samw@Sun.COM goto out;
268312508Samw@Sun.COM }
268412508Samw@Sun.COM
268512508Samw@Sun.COM } else if (req->id1name != NULL &&
268612508Samw@Sun.COM req->id1.idmap_id_u.sid.prefix == NULL) {
268712508Samw@Sun.COM char *canonname;
268812508Samw@Sun.COM char *canondomain;
268912508Samw@Sun.COM
269012508Samw@Sun.COM retcode = lookup_lsa_by_name(
269112508Samw@Sun.COM req->id1name, req->id1domain,
269212508Samw@Sun.COM &req->id1.idmap_id_u.sid.prefix,
269312508Samw@Sun.COM &req->id1.idmap_id_u.sid.rid,
269412508Samw@Sun.COM &canonname, &canondomain,
269512508Samw@Sun.COM &req->id1.idtype);
269612508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
269712508Samw@Sun.COM free(req->id1name);
269812508Samw@Sun.COM req->id1name = canonname;
269912508Samw@Sun.COM free(req->id1domain);
270012508Samw@Sun.COM req->id1domain = canondomain;
270112508Samw@Sun.COM TRACE(req, res, "Found with LSA");
270212508Samw@Sun.COM } else if (retcode == IDMAP_ERR_NOTFOUND) {
270312508Samw@Sun.COM TRACE(req, res, "Not found with LSA");
270412508Samw@Sun.COM } else {
270512508Samw@Sun.COM TRACE(req, res, "LSA error %d", retcode);
270612508Samw@Sun.COM goto out;
270712508Samw@Sun.COM }
270812508Samw@Sun.COM }
270912508Samw@Sun.COM }
271012508Samw@Sun.COM
27114520Snw141292 /*
27125731Sbaban * Set the flag to indicate that we are not done yet so that
27135731Sbaban * subsequent passes considers this request for name-based
27145731Sbaban * mapping and ephemeral mapping.
27154520Snw141292 */
27165731Sbaban state->sid2pid_done = FALSE;
27175731Sbaban req->direction |= _IDMAP_F_NOTDONE;
27185731Sbaban
27195731Sbaban /*
27205731Sbaban * Even if we have both sid and winname, we still may need to batch
27215731Sbaban * this request for AD lookup if we don't have unixname and
27225731Sbaban * directory-based name mapping (AD or mixed) is enabled.
27235731Sbaban * We avoid AD lookup for well-known SIDs because they don't have
27245731Sbaban * regular AD objects.
27255731Sbaban */
27265731Sbaban if (retcode != IDMAP_SUCCESS ||
27275731Sbaban (!wksid && req->id2name == NULL &&
272810504SKeyur.Desai@Sun.COM AD_OR_MIXED_MODE(res->id.idtype, state)) ||
272911963SAfshin.Ardakani@Sun.COM (!wksid && res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID &&
273010504SKeyur.Desai@Sun.COM state->directory_based_mapping == DIRECTORY_MAPPING_IDMU)) {
27314520Snw141292 retcode = IDMAP_SUCCESS;
27325731Sbaban req->direction |= _IDMAP_F_LOOKUP_AD;
27334520Snw141292 state->ad_nqueries++;
27346616Sdm199847 } else if (NLDAP_MODE(res->id.idtype, state)) {
27356616Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP;
27366616Sdm199847 state->nldap_nqueries++;
27374520Snw141292 }
27384520Snw141292
27394520Snw141292
27404520Snw141292 out:
27414520Snw141292 res->retcode = idmap_stat4prot(retcode);
27425731Sbaban /*
27435731Sbaban * If we are done and there was an error then set fallback pid
27445731Sbaban * in the result.
27455731Sbaban */
27465731Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS)
27475731Sbaban res->id.idmap_id_u.uid = UID_NOBODY;
27484520Snw141292 return (retcode);
27494520Snw141292 }
27504520Snw141292
27514520Snw141292 /*
27524520Snw141292 * Generate SID using the following convention
27534520Snw141292 * <machine-sid-prefix>-<1000 + uid>
27544520Snw141292 * <machine-sid-prefix>-<2^31 + gid>
27554520Snw141292 */
27565696Snw141292 static
27575696Snw141292 idmap_retcode
generate_localsid(idmap_mapping * req,idmap_id_res * res,int is_user,int fallback)27586386Sjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user,
27596386Sjp151216 int fallback)
27605696Snw141292 {
27615731Sbaban free(res->id.idmap_id_u.sid.prefix);
27625731Sbaban res->id.idmap_id_u.sid.prefix = NULL;
27635731Sbaban
27645731Sbaban /*
27655731Sbaban * Diagonal mapping for localSIDs not supported because of the
27665731Sbaban * way we generate localSIDs.
27675731Sbaban */
27685731Sbaban if (is_user && res->id.idtype == IDMAP_GSID)
276910717Samw@Sun.COM return (IDMAP_ERR_NOTGROUP);
27705731Sbaban if (!is_user && res->id.idtype == IDMAP_USID)
277110717Samw@Sun.COM return (IDMAP_ERR_NOTUSER);
27725731Sbaban
27735731Sbaban /* Skip 1000 UIDs */
277410122SJordan.Brown@Sun.COM if (is_user &&
277510122SJordan.Brown@Sun.COM req->id1.idmap_id_u.uid + LOCALRID_UID_MIN > LOCALRID_UID_MAX)
27765731Sbaban return (IDMAP_ERR_NOMAPPING);
27775731Sbaban
27785731Sbaban RDLOCK_CONFIG();
27795731Sbaban /*
27805731Sbaban * machine_sid is never NULL because if it is we won't be here.
278112508Samw@Sun.COM * No need to assert because strdup(NULL) will core anyways.
27825731Sbaban */
27835731Sbaban res->id.idmap_id_u.sid.prefix =
27845731Sbaban strdup(_idmapdstate.cfg->pgcfg.machine_sid);
27855731Sbaban if (res->id.idmap_id_u.sid.prefix == NULL) {
27864520Snw141292 UNLOCK_CONFIG();
27875731Sbaban idmapdlog(LOG_ERR, "Out of memory");
27885731Sbaban return (IDMAP_ERR_MEMORY);
27894520Snw141292 }
27905731Sbaban UNLOCK_CONFIG();
27915731Sbaban res->id.idmap_id_u.sid.rid =
279210122SJordan.Brown@Sun.COM (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_UID_MIN :
279310122SJordan.Brown@Sun.COM req->id1.idmap_id_u.gid + LOCALRID_GID_MIN;
27945731Sbaban res->direction = IDMAP_DIRECTION_BI;
27955731Sbaban if (res->id.idtype == IDMAP_SID)
27965731Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID;
27975731Sbaban
27989021Samw@Sun.COM if (!fallback) {
27996386Sjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID;
28006386Sjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC;
28016386Sjp151216 }
28026386Sjp151216
28035731Sbaban /*
28045731Sbaban * Don't update name_cache because local sids don't have
28055731Sbaban * valid windows names.
28065731Sbaban */
28075731Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
28085731Sbaban return (IDMAP_SUCCESS);
28094520Snw141292 }
28104520Snw141292
28115696Snw141292 static
28125696Snw141292 idmap_retcode
lookup_localsid2pid(idmap_mapping * req,idmap_id_res * res)28135696Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res)
28145696Snw141292 {
28154520Snw141292 char *sidprefix;
28164520Snw141292 uint32_t rid;
28174520Snw141292 int s;
28184520Snw141292
28194520Snw141292 /*
28204520Snw141292 * If the sidprefix == localsid then UID = last RID - 1000 or
28214520Snw141292 * GID = last RID - 2^31.
28224520Snw141292 */
28235731Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL)
28245731Sbaban /* This means we are looking up by winname */
28255731Sbaban return (IDMAP_ERR_NOTFOUND);
28264520Snw141292 rid = req->id1.idmap_id_u.sid.rid;
28274520Snw141292
28284520Snw141292 RDLOCK_CONFIG();
28295696Snw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ?
28305696Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1;
28314520Snw141292 UNLOCK_CONFIG();
28324520Snw141292
28335731Sbaban /*
28345731Sbaban * If the given sidprefix does not match machine_sid then this is
28355731Sbaban * not a local SID.
28365731Sbaban */
28375731Sbaban if (s != 0)
28385731Sbaban return (IDMAP_ERR_NOTFOUND);
28395731Sbaban
28405731Sbaban switch (res->id.idtype) {
28415731Sbaban case IDMAP_UID:
284210122SJordan.Brown@Sun.COM if (rid < LOCALRID_UID_MIN || rid > LOCALRID_UID_MAX)
28435731Sbaban return (IDMAP_ERR_ARG);
284410122SJordan.Brown@Sun.COM res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN;
28455731Sbaban break;
28465731Sbaban case IDMAP_GID:
284710122SJordan.Brown@Sun.COM if (rid < LOCALRID_GID_MIN)
28485731Sbaban return (IDMAP_ERR_ARG);
284910122SJordan.Brown@Sun.COM res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN;
28505731Sbaban break;
28515731Sbaban case IDMAP_POSIXID:
285210122SJordan.Brown@Sun.COM if (rid >= LOCALRID_GID_MIN) {
285310122SJordan.Brown@Sun.COM res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN;
28544520Snw141292 res->id.idtype = IDMAP_GID;
285510122SJordan.Brown@Sun.COM } else if (rid >= LOCALRID_UID_MIN) {
285610122SJordan.Brown@Sun.COM res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN;
285710122SJordan.Brown@Sun.COM res->id.idtype = IDMAP_UID;
28585731Sbaban } else {
285910122SJordan.Brown@Sun.COM return (IDMAP_ERR_ARG);
28604520Snw141292 }
28615731Sbaban break;
28625731Sbaban default:
28635731Sbaban return (IDMAP_ERR_NOTSUPPORTED);
28644520Snw141292 }
28659021Samw@Sun.COM res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID;
28669021Samw@Sun.COM res->info.src = IDMAP_MAP_SRC_ALGORITHMIC;
28675731Sbaban return (IDMAP_SUCCESS);
28684520Snw141292 }
28694520Snw141292
28705731Sbaban /*
28715731Sbaban * Name service lookup by unixname to get pid
28725731Sbaban */
28735696Snw141292 static
28745696Snw141292 idmap_retcode
ns_lookup_byname(const char * name,const char * lower_name,idmap_id * id)28755731Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id)
28765696Snw141292 {
28775696Snw141292 struct passwd pwd, *pwdp;
28785696Snw141292 struct group grp, *grpp;
28799132SJordan.Brown@Sun.COM char *buf;
28809132SJordan.Brown@Sun.COM static size_t pwdbufsiz = 0;
28819132SJordan.Brown@Sun.COM static size_t grpbufsiz = 0;
28824520Snw141292
28835731Sbaban switch (id->idtype) {
28845731Sbaban case IDMAP_UID:
28859132SJordan.Brown@Sun.COM if (pwdbufsiz == 0)
28869132SJordan.Brown@Sun.COM pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
28879132SJordan.Brown@Sun.COM buf = alloca(pwdbufsiz);
28889132SJordan.Brown@Sun.COM pwdp = getpwnam_r(name, &pwd, buf, pwdbufsiz);
28895731Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL &&
28905696Snw141292 name != lower_name && strcmp(name, lower_name) != 0)
28919132SJordan.Brown@Sun.COM pwdp = getpwnam_r(lower_name, &pwd, buf, pwdbufsiz);
28925696Snw141292 if (pwdp == NULL) {
289310966SJordan.Brown@Sun.COM if (errno == 0)
28944520Snw141292 return (IDMAP_ERR_NOTFOUND);
28954520Snw141292 else
28964520Snw141292 return (IDMAP_ERR_INTERNAL);
28974520Snw141292 }
28985731Sbaban id->idmap_id_u.uid = pwd.pw_uid;
28995731Sbaban break;
29005731Sbaban case IDMAP_GID:
29019132SJordan.Brown@Sun.COM if (grpbufsiz == 0)
29029132SJordan.Brown@Sun.COM grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX);
29039132SJordan.Brown@Sun.COM buf = alloca(grpbufsiz);
29049132SJordan.Brown@Sun.COM grpp = getgrnam_r(name, &grp, buf, grpbufsiz);
29055731Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL &&
29065696Snw141292 name != lower_name && strcmp(name, lower_name) != 0)
29079132SJordan.Brown@Sun.COM grpp = getgrnam_r(lower_name, &grp, buf, grpbufsiz);
29085696Snw141292 if (grpp == NULL) {
290910966SJordan.Brown@Sun.COM if (errno == 0)
29104520Snw141292 return (IDMAP_ERR_NOTFOUND);
29114520Snw141292 else
29124520Snw141292 return (IDMAP_ERR_INTERNAL);
29134520Snw141292 }
29145731Sbaban id->idmap_id_u.gid = grp.gr_gid;
29155731Sbaban break;
29165731Sbaban default:
29175731Sbaban return (IDMAP_ERR_ARG);
29184520Snw141292 }
29194520Snw141292 return (IDMAP_SUCCESS);
29204520Snw141292 }
29214520Snw141292
29225731Sbaban
29235731Sbaban /*
29245731Sbaban * Name service lookup by pid to get unixname
29255731Sbaban */
29265731Sbaban static
29275731Sbaban idmap_retcode
ns_lookup_bypid(uid_t pid,int is_user,char ** unixname)29285731Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname)
29295731Sbaban {
29305731Sbaban struct passwd pwd;
29315731Sbaban struct group grp;
29329132SJordan.Brown@Sun.COM char *buf;
29339132SJordan.Brown@Sun.COM static size_t pwdbufsiz = 0;
29349132SJordan.Brown@Sun.COM static size_t grpbufsiz = 0;
29355731Sbaban
29365731Sbaban if (is_user) {
29379132SJordan.Brown@Sun.COM if (pwdbufsiz == 0)
29389132SJordan.Brown@Sun.COM pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX);
29399132SJordan.Brown@Sun.COM buf = alloca(pwdbufsiz);
29405731Sbaban errno = 0;
29419132SJordan.Brown@Sun.COM if (getpwuid_r(pid, &pwd, buf, pwdbufsiz) == NULL) {
294210966SJordan.Brown@Sun.COM if (errno == 0)
29435731Sbaban return (IDMAP_ERR_NOTFOUND);
29445731Sbaban else
29455731Sbaban return (IDMAP_ERR_INTERNAL);
29465731Sbaban }
29475731Sbaban *unixname = strdup(pwd.pw_name);
29485731Sbaban } else {
29499132SJordan.Brown@Sun.COM if (grpbufsiz == 0)
29509132SJordan.Brown@Sun.COM grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX);
29519132SJordan.Brown@Sun.COM buf = alloca(grpbufsiz);
29525731Sbaban errno = 0;
29539132SJordan.Brown@Sun.COM if (getgrgid_r(pid, &grp, buf, grpbufsiz) == NULL) {
295410966SJordan.Brown@Sun.COM if (errno == 0)
29555731Sbaban return (IDMAP_ERR_NOTFOUND);
29565731Sbaban else
29575731Sbaban return (IDMAP_ERR_INTERNAL);
29585731Sbaban }
29595731Sbaban *unixname = strdup(grp.gr_name);
29605731Sbaban }
29615731Sbaban if (*unixname == NULL)
29625731Sbaban return (IDMAP_ERR_MEMORY);
29635731Sbaban return (IDMAP_SUCCESS);
29645731Sbaban }
29655731Sbaban
29664520Snw141292 /*
29674520Snw141292 * Name-based mapping
29684520Snw141292 *
29694520Snw141292 * Case 1: If no rule matches do ephemeral
29704520Snw141292 *
29714520Snw141292 * Case 2: If rule matches and unixname is "" then return no mapping.
29724520Snw141292 *
29734520Snw141292 * Case 3: If rule matches and unixname is specified then lookup name
29744520Snw141292 * service using the unixname. If unixname not found then return no mapping.
29754520Snw141292 *
29764520Snw141292 * Case 4: If rule matches and unixname is * then lookup name service
29774520Snw141292 * using winname as the unixname. If unixname not found then process
29784520Snw141292 * other rules using the lookup order. If no other rule matches then do
29794520Snw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4.
29804520Snw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping
29814520Snw141292 * instead of the default behaviour of doing ephemeral mapping.
29824520Snw141292 *
29834520Snw141292 * Example 1:
29844520Snw141292 * *@sfbay == *
29854520Snw141292 * If looking up windows users foo@sfbay and foo does not exists in
29864520Snw141292 * the name service then foo@sfbay will be mapped to an ephemeral id.
29874520Snw141292 *
29884520Snw141292 * Example 2:
29894520Snw141292 * *@sfbay == *
29904520Snw141292 * *@sfbay => guest
29914520Snw141292 * If looking up windows users foo@sfbay and foo does not exists in
29924520Snw141292 * the name service then foo@sfbay will be mapped to guest.
29934520Snw141292 *
29944520Snw141292 * Example 3:
29954520Snw141292 * *@sfbay == *
29964520Snw141292 * *@sfbay => ""
29974520Snw141292 * If looking up windows users foo@sfbay and foo does not exists in
29984520Snw141292 * the name service then we will return no mapping for foo@sfbay.
29994520Snw141292 *
30004520Snw141292 */
30015696Snw141292 static
30025696Snw141292 idmap_retcode
name_based_mapping_sid2pid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)30036616Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state,
30046616Sdm199847 idmap_mapping *req, idmap_id_res *res)
30055696Snw141292 {
30065696Snw141292 const char *unixname, *windomain;
30075696Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL;
30084520Snw141292 idmap_retcode retcode;
30095696Snw141292 char *end, *lower_unixname, *winname;
30104520Snw141292 const char **values;
30114520Snw141292 sqlite_vm *vm = NULL;
30129422SAfshin.Ardakani@Sun.COM int ncol, r, is_user, is_wuser;
30136386Sjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule;
30146386Sjp151216 int direction;
30154520Snw141292 const char *me = "name_based_mapping_sid2pid";
30164520Snw141292
30175731Sbaban assert(req->id1name != NULL); /* We have winname */
30185731Sbaban assert(req->id2name == NULL); /* We don't have unixname */
30195731Sbaban
30205064Sdm199847 winname = req->id1name;
30215064Sdm199847 windomain = req->id1domain;
30225696Snw141292
30235696Snw141292 switch (req->id1.idtype) {
30245696Snw141292 case IDMAP_USID:
30255696Snw141292 is_wuser = 1;
30265696Snw141292 break;
30275696Snw141292 case IDMAP_GSID:
30285696Snw141292 is_wuser = 0;
30295696Snw141292 break;
30305696Snw141292 default:
30315731Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the "
30325731Sbaban "given Windows id is user or group.", me);
30335696Snw141292 return (IDMAP_ERR_INTERNAL);
30345696Snw141292 }
30355696Snw141292
30365731Sbaban switch (res->id.idtype) {
30375696Snw141292 case IDMAP_UID:
30385696Snw141292 is_user = 1;
30395696Snw141292 break;
30405696Snw141292 case IDMAP_GID:
30415696Snw141292 is_user = 0;
30425696Snw141292 break;
30435696Snw141292 case IDMAP_POSIXID:
30445696Snw141292 is_user = is_wuser;
30455696Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID;
30465696Snw141292 break;
30475696Snw141292 }
30484520Snw141292
30496616Sdm199847 if (windomain == NULL)
30504864Sbaban windomain = "";
30514520Snw141292
30525696Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL)
30535696Snw141292 lower_winname = winname; /* hope for the best */
30544520Snw141292 sql = sqlite_mprintf(
30556386Sjp151216 "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 "
30566386Sjp151216 "FROM namerules WHERE "
30575696Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND "
30585696Snw141292 "(winname = %Q OR winname = '*') AND "
30599422SAfshin.Ardakani@Sun.COM "(windomain = %Q OR windomain = '*') "
30605696Snw141292 "ORDER BY w2u_order ASC;",
30619422SAfshin.Ardakani@Sun.COM is_user, is_wuser, lower_winname, windomain);
30624520Snw141292 if (sql == NULL) {
30634520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
30644520Snw141292 retcode = IDMAP_ERR_MEMORY;
30654520Snw141292 goto out;
30664520Snw141292 }
30674520Snw141292
30686616Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) {
30694520Snw141292 retcode = IDMAP_ERR_INTERNAL;
30705696Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me,
30715696Snw141292 CHECK_NULL(errmsg));
30724520Snw141292 sqlite_freemem(errmsg);
30734520Snw141292 goto out;
30744520Snw141292 }
30754520Snw141292
30766386Sjp151216 for (;;) {
30774520Snw141292 r = sqlite_step(vm, &ncol, &values, NULL);
30784884Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
30794884Sjp151216
30804884Sjp151216 if (r == SQLITE_ROW) {
30816386Sjp151216 if (ncol < 5) {
30824520Snw141292 retcode = IDMAP_ERR_INTERNAL;
30834520Snw141292 goto out;
30844520Snw141292 }
308512508Samw@Sun.COM
308612508Samw@Sun.COM TRACE(req, res, "Matching rule: %s@%s -> %s",
308712508Samw@Sun.COM values[2] == NULL ? "(null)" : values[2],
308812508Samw@Sun.COM values[3] == NULL ? "(null)" : values[3],
308912508Samw@Sun.COM values[0] == NULL ? "(null)" : values[0]);
309012508Samw@Sun.COM
30914520Snw141292 if (values[0] == NULL) {
30924520Snw141292 retcode = IDMAP_ERR_INTERNAL;
30934520Snw141292 goto out;
30944520Snw141292 }
30954520Snw141292
30966386Sjp151216 if (values[1] != NULL)
30976386Sjp151216 direction =
30986386Sjp151216 (strtol(values[1], &end, 10) == 0)?
30996386Sjp151216 IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
31006386Sjp151216 else
31016386Sjp151216 direction = IDMAP_DIRECTION_W2U;
31026386Sjp151216
31034520Snw141292 if (EMPTY_NAME(values[0])) {
310412508Samw@Sun.COM TRACE(req, res, "Mapping inhibited");
31056386Sjp151216 idmap_namerule_set(rule, values[3], values[2],
310611963SAfshin.Ardakani@Sun.COM values[0], is_user, is_wuser,
31076386Sjp151216 strtol(values[4], &end, 10),
31086386Sjp151216 direction);
31094520Snw141292 retcode = IDMAP_ERR_NOMAPPING;
31104520Snw141292 goto out;
31114520Snw141292 }
31126386Sjp151216
31136386Sjp151216 if (values[0][0] == '*') {
31146386Sjp151216 unixname = winname;
31156386Sjp151216 lower_unixname = lower_winname;
31166386Sjp151216 } else {
31176386Sjp151216 unixname = values[0];
31186386Sjp151216 lower_unixname = NULL;
31196386Sjp151216 }
31206386Sjp151216
31215731Sbaban retcode = ns_lookup_byname(unixname, lower_unixname,
31225731Sbaban &res->id);
312312508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
312412508Samw@Sun.COM break;
312512508Samw@Sun.COM } else if (retcode == IDMAP_ERR_NOTFOUND) {
312612508Samw@Sun.COM if (values[0][0] == '*') {
312712508Samw@Sun.COM TRACE(req, res,
312812508Samw@Sun.COM "%s not found, continuing",
312912508Samw@Sun.COM unixname);
31304520Snw141292 /* Case 4 */
31314520Snw141292 continue;
313212508Samw@Sun.COM } else {
313312508Samw@Sun.COM TRACE(req, res,
313412508Samw@Sun.COM "%s not found, error", unixname);
31354520Snw141292 /* Case 3 */
31366386Sjp151216 idmap_namerule_set(rule, values[3],
313711963SAfshin.Ardakani@Sun.COM values[2], values[0], is_user,
313811963SAfshin.Ardakani@Sun.COM is_wuser,
31396386Sjp151216 strtol(values[4], &end, 10),
31406386Sjp151216 direction);
31414520Snw141292 retcode = IDMAP_ERR_NOMAPPING;
31426386Sjp151216 }
314312508Samw@Sun.COM } else {
314412508Samw@Sun.COM TRACE(req, res, "Looking up %s error=%d",
314512508Samw@Sun.COM unixname, retcode);
31464520Snw141292 }
31474520Snw141292 goto out;
31484520Snw141292 } else if (r == SQLITE_DONE) {
3149*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "No matching rule");
31504520Snw141292 retcode = IDMAP_ERR_NOTFOUND;
31514520Snw141292 goto out;
31524520Snw141292 } else {
31534520Snw141292 (void) sqlite_finalize(vm, &errmsg);
31544520Snw141292 vm = NULL;
31555696Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me,
31565696Snw141292 CHECK_NULL(errmsg));
31574520Snw141292 sqlite_freemem(errmsg);
31584520Snw141292 retcode = IDMAP_ERR_INTERNAL;
31594520Snw141292 goto out;
31604520Snw141292 }
31614520Snw141292 }
31624520Snw141292
316312508Samw@Sun.COM /* Found */
316412508Samw@Sun.COM
316512508Samw@Sun.COM if (values[1] != NULL)
316612508Samw@Sun.COM res->direction =
316712508Samw@Sun.COM (strtol(values[1], &end, 10) == 0)?
316812508Samw@Sun.COM IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI;
316912508Samw@Sun.COM else
317012508Samw@Sun.COM res->direction = IDMAP_DIRECTION_W2U;
317112508Samw@Sun.COM
317212508Samw@Sun.COM req->id2name = strdup(unixname);
317312508Samw@Sun.COM if (req->id2name == NULL) {
317412508Samw@Sun.COM retcode = IDMAP_ERR_MEMORY;
317512508Samw@Sun.COM goto out;
317612508Samw@Sun.COM }
317712508Samw@Sun.COM TRACE(req, res, "UNIX name found");
317812508Samw@Sun.COM
317912508Samw@Sun.COM idmap_namerule_set(rule, values[3], values[2],
318012508Samw@Sun.COM values[0], is_user, is_wuser, strtol(values[4], &end, 10),
318112508Samw@Sun.COM res->direction);
318212508Samw@Sun.COM
31834520Snw141292 out:
318412508Samw@Sun.COM if (retcode != IDMAP_SUCCESS &&
318512508Samw@Sun.COM retcode != IDMAP_ERR_NOTFOUND &&
318612508Samw@Sun.COM retcode != IDMAP_ERR_NOMAPPING) {
318712508Samw@Sun.COM TRACE(req, res, "Rule processing error, code=%d", retcode);
318812508Samw@Sun.COM }
318912508Samw@Sun.COM
31906386Sjp151216 if (sql != NULL)
31916386Sjp151216 sqlite_freemem(sql);
31929021Samw@Sun.COM
31939021Samw@Sun.COM if (retcode != IDMAP_ERR_NOTFOUND) {
31949021Samw@Sun.COM res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED;
31956386Sjp151216 res->info.src = IDMAP_MAP_SRC_NEW;
31964520Snw141292 }
31976616Sdm199847
31985696Snw141292 if (lower_winname != NULL && lower_winname != winname)
31995696Snw141292 free(lower_winname);
32004864Sbaban if (vm != NULL)
32014520Snw141292 (void) sqlite_finalize(vm, NULL);
32024520Snw141292 return (retcode);
32034520Snw141292 }
32044520Snw141292
32054520Snw141292 static
32064520Snw141292 int
get_next_eph_uid(uid_t * next_uid)32074520Snw141292 get_next_eph_uid(uid_t *next_uid)
32084520Snw141292 {
32094520Snw141292 uid_t uid;
32104520Snw141292 gid_t gid;
32114520Snw141292 int err;
32124520Snw141292
32134520Snw141292 *next_uid = (uid_t)-1;
32144520Snw141292 uid = _idmapdstate.next_uid++;
32154520Snw141292 if (uid >= _idmapdstate.limit_uid) {
32164520Snw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0)
32174520Snw141292 return (err);
32184520Snw141292
32194520Snw141292 _idmapdstate.limit_uid = uid + 8192;
32204520Snw141292 _idmapdstate.next_uid = uid;
32214520Snw141292 }
32224520Snw141292 *next_uid = uid;
32234520Snw141292
32244520Snw141292 return (0);
32254520Snw141292 }
32264520Snw141292
32274520Snw141292 static
32284520Snw141292 int
get_next_eph_gid(gid_t * next_gid)32294520Snw141292 get_next_eph_gid(gid_t *next_gid)
32304520Snw141292 {
32314520Snw141292 uid_t uid;
32324520Snw141292 gid_t gid;
32334520Snw141292 int err;
32344520Snw141292
32354520Snw141292 *next_gid = (uid_t)-1;
32364520Snw141292 gid = _idmapdstate.next_gid++;
32374520Snw141292 if (gid >= _idmapdstate.limit_gid) {
32384520Snw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0)
32394520Snw141292 return (err);
32404520Snw141292
32414520Snw141292 _idmapdstate.limit_gid = gid + 8192;
32424520Snw141292 _idmapdstate.next_gid = gid;
32434520Snw141292 }
32444520Snw141292 *next_gid = gid;
32454520Snw141292
32464520Snw141292 return (0);
32474520Snw141292 }
32484520Snw141292
32494864Sbaban static
32504864Sbaban int
gethash(const char * str,uint32_t num,uint_t htsize)32515696Snw141292 gethash(const char *str, uint32_t num, uint_t htsize)
32525696Snw141292 {
32534864Sbaban uint_t hval, i, len;
32544864Sbaban
32554864Sbaban if (str == NULL)
32564864Sbaban return (0);
32574864Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) {
32584864Sbaban hval += str[i];
32594864Sbaban hval += (hval << 10);
32604864Sbaban hval ^= (hval >> 6);
32614864Sbaban }
32624864Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) {
32634864Sbaban hval += str[i];
32644864Sbaban hval += (hval << 10);
32654864Sbaban hval ^= (hval >> 6);
32664864Sbaban }
32674864Sbaban hval += (hval << 3);
32684864Sbaban hval ^= (hval >> 11);
32694864Sbaban hval += (hval << 15);
32704864Sbaban return (hval % htsize);
32714864Sbaban }
32724864Sbaban
32734864Sbaban static
32744864Sbaban int
get_from_sid_history(lookup_state_t * state,const char * prefix,uint32_t rid,uid_t * pid)32754864Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid,
32765696Snw141292 uid_t *pid)
32775696Snw141292 {
32784864Sbaban uint_t next, key;
32794864Sbaban uint_t htsize = state->sid_history_size;
32804864Sbaban idmap_sid *sid;
32814864Sbaban
32824864Sbaban next = gethash(prefix, rid, htsize);
32834864Sbaban while (next != htsize) {
32844864Sbaban key = state->sid_history[next].key;
32854864Sbaban if (key == htsize)
32864864Sbaban return (0);
32874864Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1.
32884864Sbaban idmap_id_u.sid;
32894864Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) {
32904864Sbaban *pid = state->result->ids.ids_val[key].id.
32914864Sbaban idmap_id_u.uid;
32924864Sbaban return (1);
32934864Sbaban }
32944864Sbaban next = state->sid_history[next].next;
32954864Sbaban }
32964864Sbaban return (0);
32974864Sbaban }
32984864Sbaban
32994864Sbaban static
33004864Sbaban void
add_to_sid_history(lookup_state_t * state,const char * prefix,uint32_t rid)33015696Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid)
33025696Snw141292 {
33034864Sbaban uint_t hash, next;
33044864Sbaban uint_t htsize = state->sid_history_size;
33054864Sbaban
33064864Sbaban hash = next = gethash(prefix, rid, htsize);
33074864Sbaban while (state->sid_history[next].key != htsize) {
33084864Sbaban next++;
33094864Sbaban next %= htsize;
33104864Sbaban }
33114864Sbaban state->sid_history[next].key = state->curpos;
33124864Sbaban if (hash == next)
33134864Sbaban return;
33144864Sbaban state->sid_history[next].next = state->sid_history[hash].next;
33154864Sbaban state->sid_history[hash].next = next;
33164864Sbaban }
33174520Snw141292
33185731Sbaban void
cleanup_lookup_state(lookup_state_t * state)33195731Sbaban cleanup_lookup_state(lookup_state_t *state)
33205731Sbaban {
33215731Sbaban free(state->sid_history);
33225731Sbaban free(state->ad_unixuser_attr);
33235731Sbaban free(state->ad_unixgroup_attr);
33246616Sdm199847 free(state->nldap_winname_attr);
33256616Sdm199847 free(state->defdom);
33265731Sbaban }
33275731Sbaban
33284520Snw141292 /* ARGSUSED */
33294520Snw141292 static
33304520Snw141292 idmap_retcode
dynamic_ephemeral_mapping(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)33316616Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state,
33325696Snw141292 idmap_mapping *req, idmap_id_res *res)
33335696Snw141292 {
33344520Snw141292
33354520Snw141292 uid_t next_pid;
33364520Snw141292
33374864Sbaban res->direction = IDMAP_DIRECTION_BI;
33384864Sbaban
333911963SAfshin.Ardakani@Sun.COM if (IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid)) {
33406386Sjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
33416386Sjp151216 res->info.src = IDMAP_MAP_SRC_CACHE;
33424864Sbaban return (IDMAP_SUCCESS);
33436386Sjp151216 }
33444864Sbaban
33454864Sbaban if (state->sid_history != NULL &&
33464864Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix,
33474864Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) {
33484864Sbaban res->id.idmap_id_u.uid = next_pid;
33496386Sjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
33506386Sjp151216 res->info.src = IDMAP_MAP_SRC_NEW;
33514864Sbaban return (IDMAP_SUCCESS);
33524864Sbaban }
33534864Sbaban
33544864Sbaban if (res->id.idtype == IDMAP_UID) {
33554520Snw141292 if (get_next_eph_uid(&next_pid) != 0)
33564520Snw141292 return (IDMAP_ERR_INTERNAL);
33574520Snw141292 res->id.idmap_id_u.uid = next_pid;
33584520Snw141292 } else {
33594520Snw141292 if (get_next_eph_gid(&next_pid) != 0)
33604520Snw141292 return (IDMAP_ERR_INTERNAL);
33614520Snw141292 res->id.idmap_id_u.gid = next_pid;
33624520Snw141292 }
33634520Snw141292
33646386Sjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL;
33656386Sjp151216 res->info.src = IDMAP_MAP_SRC_NEW;
33664864Sbaban if (state->sid_history != NULL)
33674864Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix,
33684864Sbaban req->id1.idmap_id_u.sid.rid);
33694864Sbaban
33704520Snw141292 return (IDMAP_SUCCESS);
33714520Snw141292 }
33724520Snw141292
33734520Snw141292 idmap_retcode
sid2pid_second_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)33746616Sdm199847 sid2pid_second_pass(lookup_state_t *state,
33755696Snw141292 idmap_mapping *req, idmap_id_res *res)
33765696Snw141292 {
33774520Snw141292 idmap_retcode retcode;
337812508Samw@Sun.COM idmap_retcode retcode2;
33794520Snw141292
33804520Snw141292 /* Check if second pass is needed */
33815731Sbaban if (ARE_WE_DONE(req->direction))
33824520Snw141292 return (res->retcode);
33834520Snw141292
33844520Snw141292 /* Get status from previous pass */
33855731Sbaban retcode = res->retcode;
33867031Snw141292 if (retcode != IDMAP_SUCCESS && state->eph_map_unres_sids &&
33877031Snw141292 !EMPTY_STRING(req->id1.idmap_id_u.sid.prefix) &&
33887031Snw141292 EMPTY_STRING(req->id1name)) {
33897031Snw141292 /*
33907031Snw141292 * We are asked to map an unresolvable SID to a UID or
33917031Snw141292 * GID, but, which? We'll treat all unresolvable SIDs
33927031Snw141292 * as users unless the caller specified which of a UID
33937031Snw141292 * or GID they want.
33947031Snw141292 */
33957818SNicolas.Williams@Sun.COM if (req->id1.idtype == IDMAP_SID)
33967818SNicolas.Williams@Sun.COM req->id1.idtype = IDMAP_USID;
339712508Samw@Sun.COM if (res->id.idtype == IDMAP_POSIXID) {
33987031Snw141292 res->id.idtype = IDMAP_UID;
339912508Samw@Sun.COM TRACE(req, res, "Assume unresolvable SID is user");
340012508Samw@Sun.COM } else if (res->id.idtype == IDMAP_UID) {
340112508Samw@Sun.COM TRACE(req, res, "Must map unresolvable SID to user");
340212508Samw@Sun.COM } else if (res->id.idtype == IDMAP_GID) {
340312508Samw@Sun.COM TRACE(req, res, "Must map unresolvable SID to group");
340412508Samw@Sun.COM }
34057031Snw141292 goto do_eph;
34067031Snw141292 }
34075731Sbaban if (retcode != IDMAP_SUCCESS)
34085731Sbaban goto out;
34095731Sbaban
34105731Sbaban /*
341110504SKeyur.Desai@Sun.COM * There are two ways we might get here with a Posix ID:
341210504SKeyur.Desai@Sun.COM * - It could be from an expired ephemeral cache entry.
341310504SKeyur.Desai@Sun.COM * - It could be from IDMU.
341410504SKeyur.Desai@Sun.COM * If it's from IDMU, we need to look up the name, for name-based
341510504SKeyur.Desai@Sun.COM * requests and the cache.
341610504SKeyur.Desai@Sun.COM */
341711963SAfshin.Ardakani@Sun.COM if (!IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid) &&
341811963SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
341910504SKeyur.Desai@Sun.COM if (req->id2name == NULL) {
342010504SKeyur.Desai@Sun.COM /*
342110504SKeyur.Desai@Sun.COM * If the lookup fails, go ahead anyway.
342210504SKeyur.Desai@Sun.COM * The general UNIX rule is that it's OK to
342310504SKeyur.Desai@Sun.COM * have a UID or GID that isn't in the
342410504SKeyur.Desai@Sun.COM * name service.
342510504SKeyur.Desai@Sun.COM */
342612508Samw@Sun.COM retcode2 = ns_lookup_bypid(res->id.idmap_id_u.uid,
342710504SKeyur.Desai@Sun.COM res->id.idtype == IDMAP_UID, &req->id2name);
342812508Samw@Sun.COM if (IDMAP_ERROR(retcode2)) {
342912508Samw@Sun.COM TRACE(req, res,
343012508Samw@Sun.COM "Getting UNIX name, error=%d (ignored)",
343112508Samw@Sun.COM retcode2);
343212508Samw@Sun.COM } else {
343312508Samw@Sun.COM TRACE(req, res, "Found UNIX name");
343412508Samw@Sun.COM }
343510504SKeyur.Desai@Sun.COM }
343610504SKeyur.Desai@Sun.COM goto out;
343710504SKeyur.Desai@Sun.COM }
343810504SKeyur.Desai@Sun.COM
343910504SKeyur.Desai@Sun.COM /*
34405731Sbaban * If directory-based name mapping is enabled then the unixname
34415731Sbaban * may already have been retrieved from the AD object (AD-mode or
34425731Sbaban * mixed-mode) or from native LDAP object (nldap-mode) -- done.
34435731Sbaban */
34445731Sbaban if (req->id2name != NULL) {
34455731Sbaban assert(res->id.idtype != IDMAP_POSIXID);
34465731Sbaban if (AD_MODE(res->id.idtype, state))
34475731Sbaban res->direction = IDMAP_DIRECTION_BI;
34485731Sbaban else if (NLDAP_MODE(res->id.idtype, state))
34495731Sbaban res->direction = IDMAP_DIRECTION_BI;
34505731Sbaban else if (MIXED_MODE(res->id.idtype, state))
34515731Sbaban res->direction = IDMAP_DIRECTION_W2U;
34525731Sbaban
34535731Sbaban /*
34545731Sbaban * Special case: (1) If the ad_unixuser_attr and
34555731Sbaban * ad_unixgroup_attr uses the same attribute
34565731Sbaban * name and (2) if this is a diagonal mapping
34575731Sbaban * request and (3) the unixname has been retrieved
34585731Sbaban * from the AD object -- then we ignore it and fallback
34595731Sbaban * to name-based mapping rules and ephemeral mapping
34605731Sbaban *
34615731Sbaban * Example:
34625731Sbaban * Properties:
34635731Sbaban * config/ad_unixuser_attr = "unixname"
34645731Sbaban * config/ad_unixgroup_attr = "unixname"
34655731Sbaban * AD user object:
34665731Sbaban * dn: cn=bob ...
34675731Sbaban * objectclass: user
34685731Sbaban * sam: bob
34695731Sbaban * unixname: bob1234
34705731Sbaban * AD group object:
34715731Sbaban * dn: cn=winadmins ...
34725731Sbaban * objectclass: group
34735731Sbaban * sam: winadmins
34745731Sbaban * unixname: unixadmins
34755731Sbaban *
34765731Sbaban * In this example whether "unixname" refers to a unixuser
34775731Sbaban * or unixgroup depends upon the AD object.
34785731Sbaban *
34795731Sbaban * $idmap show -c winname:bob gid
34805731Sbaban * AD lookup by "samAccountName=bob" for
34815731Sbaban * "ad_unixgroup_attr (i.e unixname)" for directory-based
34825731Sbaban * mapping would get "bob1234" which is not what we want.
34835731Sbaban * Now why not getgrnam_r("bob1234") and use it if it
34845731Sbaban * is indeed a unixgroup? That's because Unix can have
34855731Sbaban * users and groups with the same name and we clearly
34865731Sbaban * don't know the intention of the admin here.
34875731Sbaban * Therefore we ignore this and fallback to name-based
34885731Sbaban * mapping rules or ephemeral mapping.
34895731Sbaban */
34905731Sbaban if ((AD_MODE(res->id.idtype, state) ||
34915731Sbaban MIXED_MODE(res->id.idtype, state)) &&
34925731Sbaban state->ad_unixuser_attr != NULL &&
34935731Sbaban state->ad_unixgroup_attr != NULL &&
34945731Sbaban strcasecmp(state->ad_unixuser_attr,
34955731Sbaban state->ad_unixgroup_attr) == 0 &&
34965731Sbaban ((req->id1.idtype == IDMAP_USID &&
34975731Sbaban res->id.idtype == IDMAP_GID) ||
34985731Sbaban (req->id1.idtype == IDMAP_GSID &&
34995731Sbaban res->id.idtype == IDMAP_UID))) {
350012508Samw@Sun.COM TRACE(req, res, "Ignoring UNIX name found in AD");
35015731Sbaban free(req->id2name);
35025731Sbaban req->id2name = NULL;
350311963SAfshin.Ardakani@Sun.COM res->id.idmap_id_u.uid = IDMAP_SENTINEL_PID;
35045731Sbaban /* fallback */
35055731Sbaban } else {
350612508Samw@Sun.COM if (res->id.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
35075731Sbaban retcode = ns_lookup_byname(req->id2name,
35085731Sbaban NULL, &res->id);
350912508Samw@Sun.COM if (retcode != IDMAP_SUCCESS) {
351012508Samw@Sun.COM /*
351112508Samw@Sun.COM * If ns_lookup_byname() fails that
351212508Samw@Sun.COM * means the unixname (req->id2name),
351312508Samw@Sun.COM * which was obtained from the AD
351412508Samw@Sun.COM * object by directory-based mapping,
351512508Samw@Sun.COM * is not a valid Unix user/group and
351612508Samw@Sun.COM * therefore we return the error to the
351712508Samw@Sun.COM * client instead of doing rule-based
351812508Samw@Sun.COM * mapping or ephemeral mapping. This
351912508Samw@Sun.COM * way the client can detect the issue.
352012508Samw@Sun.COM */
352112508Samw@Sun.COM TRACE(req, res,
352212508Samw@Sun.COM "UNIX lookup error=%d", retcode);
352312508Samw@Sun.COM goto out;
352412508Samw@Sun.COM }
352512508Samw@Sun.COM TRACE(req, res, "UNIX lookup");
352612508Samw@Sun.COM }
35275731Sbaban goto out;
35284520Snw141292 }
35294520Snw141292 }
35304520Snw141292
35316386Sjp151216 /* Free any mapping info from Directory based mapping */
35326386Sjp151216 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN)
353312508Samw@Sun.COM idmap_how_clear(&res->info.how);
35346386Sjp151216
35355731Sbaban /*
35365731Sbaban * If we don't have unixname then evaluate local name-based
35375731Sbaban * mapping rules.
35385731Sbaban */
35396616Sdm199847 retcode = name_based_mapping_sid2pid(state, req, res);
354012508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
354112508Samw@Sun.COM TRACE(req, res, "Rule-based mapping");
35424520Snw141292 goto out;
354312508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
354412508Samw@Sun.COM TRACE(req, res, "Rule-based mapping error=%d", retcode);
354512508Samw@Sun.COM goto out;
354612508Samw@Sun.COM }
35474520Snw141292
35487031Snw141292 do_eph:
35495731Sbaban /* If not found, do ephemeral mapping */
35506616Sdm199847 retcode = dynamic_ephemeral_mapping(state, req, res);
355112508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
355212508Samw@Sun.COM TRACE(req, res, "Ephemeral mapping");
355312508Samw@Sun.COM goto out;
355412508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
355512508Samw@Sun.COM TRACE(req, res, "Ephemeral mapping error=%d", retcode);
355612508Samw@Sun.COM goto out;
355712508Samw@Sun.COM }
35584520Snw141292
35594520Snw141292 out:
35604520Snw141292 res->retcode = idmap_stat4prot(retcode);
35615731Sbaban if (res->retcode != IDMAP_SUCCESS) {
35625731Sbaban req->direction = _IDMAP_F_DONE;
35635731Sbaban res->id.idmap_id_u.uid = UID_NOBODY;
35645731Sbaban }
35655731Sbaban if (!ARE_WE_DONE(req->direction))
35665731Sbaban state->sid2pid_done = FALSE;
35674520Snw141292 return (retcode);
35684520Snw141292 }
35694520Snw141292
35704520Snw141292 idmap_retcode
update_cache_pid2sid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)35716616Sdm199847 update_cache_pid2sid(lookup_state_t *state,
35725696Snw141292 idmap_mapping *req, idmap_id_res *res)
35735696Snw141292 {
35744520Snw141292 char *sql = NULL;
35754520Snw141292 idmap_retcode retcode;
357612508Samw@Sun.COM idmap_retcode retcode2;
35776386Sjp151216 char *map_dn = NULL;
35786386Sjp151216 char *map_attr = NULL;
35796386Sjp151216 char *map_value = NULL;
35806386Sjp151216 char *map_windomain = NULL;
35816386Sjp151216 char *map_winname = NULL;
35826386Sjp151216 char *map_unixname = NULL;
35836386Sjp151216 int map_is_nt4 = FALSE;
35844520Snw141292
35854520Snw141292 /* Check if we need to cache anything */
35865731Sbaban if (ARE_WE_DONE(req->direction))
35874520Snw141292 return (IDMAP_SUCCESS);
35884520Snw141292
35894520Snw141292 /* We don't cache negative entries */
35904520Snw141292 if (res->retcode != IDMAP_SUCCESS)
35914520Snw141292 return (IDMAP_SUCCESS);
35924520Snw141292
35935731Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF);
359411963SAfshin.Ardakani@Sun.COM assert(req->id1.idmap_id_u.uid != IDMAP_SENTINEL_PID);
35956386Sjp151216 assert(res->id.idtype != IDMAP_SID);
35966386Sjp151216
359710504SKeyur.Desai@Sun.COM /*
359810504SKeyur.Desai@Sun.COM * If we've gotten to this point and we *still* don't know the
359910504SKeyur.Desai@Sun.COM * unixname, well, we'd like to have it now for the cache.
360010504SKeyur.Desai@Sun.COM *
360110504SKeyur.Desai@Sun.COM * If we truly always need it for the cache, we should probably
360210504SKeyur.Desai@Sun.COM * look it up once at the beginning, rather than "at need" in
360310504SKeyur.Desai@Sun.COM * several places as is now done. However, it's not really clear
360410504SKeyur.Desai@Sun.COM * that we *do* need it in the cache; there's a decent argument
360510504SKeyur.Desai@Sun.COM * that the cache should contain only SIDs and PIDs, so we'll
360610504SKeyur.Desai@Sun.COM * leave our options open by doing it "at need" here too.
360710504SKeyur.Desai@Sun.COM *
360810504SKeyur.Desai@Sun.COM * If we can't find it... c'est la vie.
360910504SKeyur.Desai@Sun.COM */
361010504SKeyur.Desai@Sun.COM if (req->id1name == NULL) {
361112508Samw@Sun.COM retcode2 = ns_lookup_bypid(req->id1.idmap_id_u.uid,
361210504SKeyur.Desai@Sun.COM req->id1.idtype == IDMAP_UID, &req->id1name);
361312508Samw@Sun.COM if (retcode2 == IDMAP_SUCCESS)
361412508Samw@Sun.COM TRACE(req, res, "Found UNIX name");
361512508Samw@Sun.COM else
361612508Samw@Sun.COM TRACE(req, res, "Getting UNIX name error=%d", retcode2);
361710504SKeyur.Desai@Sun.COM }
361810504SKeyur.Desai@Sun.COM
36196386Sjp151216 assert(res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN);
36206386Sjp151216 switch (res->info.how.map_type) {
36216386Sjp151216 case IDMAP_MAP_TYPE_DS_AD:
36226386Sjp151216 map_dn = res->info.how.idmap_how_u.ad.dn;
36236386Sjp151216 map_attr = res->info.how.idmap_how_u.ad.attr;
36246386Sjp151216 map_value = res->info.how.idmap_how_u.ad.value;
36256386Sjp151216 break;
36266386Sjp151216
36276386Sjp151216 case IDMAP_MAP_TYPE_DS_NLDAP:
36286386Sjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn;
36296386Sjp151216 map_attr = res->info.how.idmap_how_u.nldap.attr;
36306386Sjp151216 map_value = res->info.how.idmap_how_u.nldap.value;
36316386Sjp151216 break;
36326386Sjp151216
36336386Sjp151216 case IDMAP_MAP_TYPE_RULE_BASED:
36346386Sjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain;
36356386Sjp151216 map_winname = res->info.how.idmap_how_u.rule.winname;
36366386Sjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname;
36376386Sjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4;
36386386Sjp151216 break;
36396386Sjp151216
36406386Sjp151216 case IDMAP_MAP_TYPE_EPHEMERAL:
36416386Sjp151216 break;
36426386Sjp151216
36436386Sjp151216 case IDMAP_MAP_TYPE_LOCAL_SID:
36446386Sjp151216 break;
36456386Sjp151216
364610504SKeyur.Desai@Sun.COM case IDMAP_MAP_TYPE_IDMU:
364710504SKeyur.Desai@Sun.COM map_dn = res->info.how.idmap_how_u.idmu.dn;
364810504SKeyur.Desai@Sun.COM map_attr = res->info.how.idmap_how_u.idmu.attr;
364910504SKeyur.Desai@Sun.COM map_value = res->info.how.idmap_how_u.idmu.value;
365010504SKeyur.Desai@Sun.COM break;
365110504SKeyur.Desai@Sun.COM
36526386Sjp151216 default:
365312508Samw@Sun.COM /* Don't cache other mapping types */
36546386Sjp151216 assert(FALSE);
36556386Sjp151216 }
36565731Sbaban
36574520Snw141292 /*
36584520Snw141292 * Using NULL for u2w instead of 0 so that our trigger allows
36594520Snw141292 * the same pid to be the destination in multiple entries
36604520Snw141292 */
36614520Snw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache "
36625696Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, "
36636386Sjp151216 "is_user, is_wuser, expiration, w2u, u2w, "
36646386Sjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, "
36656386Sjp151216 "map_winname, map_unixname, map_is_nt4) "
36665696Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
36676386Sjp151216 "strftime('%%s','now') + 600, %q, 1, "
36686386Sjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ",
36695696Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid,
36705696Snw141292 req->id2domain, req->id2name, req->id1.idmap_id_u.uid,
36715696Snw141292 req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0,
36725731Sbaban (res->id.idtype == IDMAP_USID) ? 1 : 0,
36736386Sjp151216 (res->direction == 0) ? "1" : NULL,
36746386Sjp151216 res->info.how.map_type, map_dn, map_attr, map_value,
36756386Sjp151216 map_windomain, map_winname, map_unixname, map_is_nt4);
36764520Snw141292
36774520Snw141292 if (sql == NULL) {
36784520Snw141292 retcode = IDMAP_ERR_INTERNAL;
36794520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
36804520Snw141292 goto out;
36814520Snw141292 }
36824520Snw141292
36836616Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
36844520Snw141292 if (retcode != IDMAP_SUCCESS)
36854520Snw141292 goto out;
36864520Snw141292
36874520Snw141292 state->pid2sid_done = FALSE;
36884520Snw141292 sqlite_freemem(sql);
36894520Snw141292 sql = NULL;
36904520Snw141292
36915731Sbaban /* Check if we need to update namecache */
36925731Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE)
36934520Snw141292 goto out;
36944520Snw141292
36955064Sdm199847 if (req->id2name == NULL)
36964520Snw141292 goto out;
36974520Snw141292
36984520Snw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache "
36995696Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) "
37005696Snw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ",
37015696Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid,
37025696Snw141292 req->id2name, req->id2domain,
370312508Samw@Sun.COM res->id.idtype);
37044520Snw141292
37054520Snw141292 if (sql == NULL) {
37064520Snw141292 retcode = IDMAP_ERR_INTERNAL;
37074520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
37084520Snw141292 goto out;
37094520Snw141292 }
37104520Snw141292
37116616Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
37124520Snw141292
37134520Snw141292 out:
37144864Sbaban if (sql != NULL)
37154520Snw141292 sqlite_freemem(sql);
37164520Snw141292 return (retcode);
37174520Snw141292 }
37184520Snw141292
37194520Snw141292 idmap_retcode
update_cache_sid2pid(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res)37206616Sdm199847 update_cache_sid2pid(lookup_state_t *state,
37215696Snw141292 idmap_mapping *req, idmap_id_res *res)
37225696Snw141292 {
37234520Snw141292 char *sql = NULL;
37244520Snw141292 idmap_retcode retcode;
37254520Snw141292 int is_eph_user;
37266386Sjp151216 char *map_dn = NULL;
37276386Sjp151216 char *map_attr = NULL;
37286386Sjp151216 char *map_value = NULL;
37296386Sjp151216 char *map_windomain = NULL;
37306386Sjp151216 char *map_winname = NULL;
37316386Sjp151216 char *map_unixname = NULL;
37326386Sjp151216 int map_is_nt4 = FALSE;
37334520Snw141292
37344520Snw141292 /* Check if we need to cache anything */
37355731Sbaban if (ARE_WE_DONE(req->direction))
37364520Snw141292 return (IDMAP_SUCCESS);
37374520Snw141292
37384520Snw141292 /* We don't cache negative entries */
37394520Snw141292 if (res->retcode != IDMAP_SUCCESS)
37404520Snw141292 return (IDMAP_SUCCESS);
37414520Snw141292
37424520Snw141292 if (req->direction & _IDMAP_F_EXP_EPH_UID)
37434520Snw141292 is_eph_user = 1;
37444520Snw141292 else if (req->direction & _IDMAP_F_EXP_EPH_GID)
37454520Snw141292 is_eph_user = 0;
37464520Snw141292 else
37474520Snw141292 is_eph_user = -1;
37484520Snw141292
374911963SAfshin.Ardakani@Sun.COM if (is_eph_user >= 0 &&
375011963SAfshin.Ardakani@Sun.COM !IDMAP_ID_IS_EPHEMERAL(res->id.idmap_id_u.uid)) {
37514520Snw141292 sql = sqlite_mprintf("UPDATE idmap_cache "
37525696Snw141292 "SET w2u = 0 WHERE "
37535696Snw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND "
37545696Snw141292 "pid >= 2147483648 AND is_user = %d;",
37555696Snw141292 req->id1.idmap_id_u.sid.prefix,
37565696Snw141292 req->id1.idmap_id_u.sid.rid,
37575696Snw141292 is_eph_user);
37584520Snw141292 if (sql == NULL) {
37594520Snw141292 retcode = IDMAP_ERR_INTERNAL;
37604520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
37614520Snw141292 goto out;
37624520Snw141292 }
37634520Snw141292
37646616Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
37654520Snw141292 if (retcode != IDMAP_SUCCESS)
37664520Snw141292 goto out;
37674520Snw141292
37684520Snw141292 sqlite_freemem(sql);
37694520Snw141292 sql = NULL;
37704520Snw141292 }
37714520Snw141292
37725731Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF);
377311963SAfshin.Ardakani@Sun.COM assert(res->id.idmap_id_u.uid != IDMAP_SENTINEL_PID);
37746386Sjp151216
37756386Sjp151216 switch (res->info.how.map_type) {
37766386Sjp151216 case IDMAP_MAP_TYPE_DS_AD:
37776386Sjp151216 map_dn = res->info.how.idmap_how_u.ad.dn;
37786386Sjp151216 map_attr = res->info.how.idmap_how_u.ad.attr;
37796386Sjp151216 map_value = res->info.how.idmap_how_u.ad.value;
37806386Sjp151216 break;
37816386Sjp151216
37826386Sjp151216 case IDMAP_MAP_TYPE_DS_NLDAP:
37836386Sjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn;
37846386Sjp151216 map_attr = res->info.how.idmap_how_u.ad.attr;
37856386Sjp151216 map_value = res->info.how.idmap_how_u.nldap.value;
37866386Sjp151216 break;
37876386Sjp151216
37886386Sjp151216 case IDMAP_MAP_TYPE_RULE_BASED:
37896386Sjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain;
37906386Sjp151216 map_winname = res->info.how.idmap_how_u.rule.winname;
37916386Sjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname;
37926386Sjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4;
37936386Sjp151216 break;
37946386Sjp151216
37956386Sjp151216 case IDMAP_MAP_TYPE_EPHEMERAL:
37966386Sjp151216 break;
37976386Sjp151216
379810504SKeyur.Desai@Sun.COM case IDMAP_MAP_TYPE_IDMU:
379910504SKeyur.Desai@Sun.COM map_dn = res->info.how.idmap_how_u.idmu.dn;
380010504SKeyur.Desai@Sun.COM map_attr = res->info.how.idmap_how_u.idmu.attr;
380110504SKeyur.Desai@Sun.COM map_value = res->info.how.idmap_how_u.idmu.value;
380210504SKeyur.Desai@Sun.COM break;
380310504SKeyur.Desai@Sun.COM
38046386Sjp151216 default:
380512508Samw@Sun.COM /* Don't cache other mapping types */
38066386Sjp151216 assert(FALSE);
38076386Sjp151216 }
38085696Snw141292
38094520Snw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache "
38105696Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, "
38116386Sjp151216 "is_user, is_wuser, expiration, w2u, u2w, "
38126386Sjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, "
38136386Sjp151216 "map_winname, map_unixname, map_is_nt4) "
38145696Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
38156386Sjp151216 "strftime('%%s','now') + 600, 1, %q, "
38166386Sjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
38175696Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid,
38185731Sbaban (req->id1domain != NULL) ? req->id1domain : "", req->id1name,
38195731Sbaban res->id.idmap_id_u.uid, req->id2name,
38205731Sbaban (res->id.idtype == IDMAP_UID) ? 1 : 0,
38215696Snw141292 (req->id1.idtype == IDMAP_USID) ? 1 : 0,
38226386Sjp151216 (res->direction == 0) ? "1" : NULL,
38236386Sjp151216 res->info.how.map_type, map_dn, map_attr, map_value,
38246386Sjp151216 map_windomain, map_winname, map_unixname, map_is_nt4);
38254520Snw141292
38264520Snw141292 if (sql == NULL) {
38274520Snw141292 retcode = IDMAP_ERR_INTERNAL;
38284520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
38294520Snw141292 goto out;
38304520Snw141292 }
38314520Snw141292
38326616Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
38334520Snw141292 if (retcode != IDMAP_SUCCESS)
38344520Snw141292 goto out;
38354520Snw141292
38364520Snw141292 state->sid2pid_done = FALSE;
38374520Snw141292 sqlite_freemem(sql);
38384520Snw141292 sql = NULL;
38394520Snw141292
38405731Sbaban /* Check if we need to update namecache */
38415731Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE)
38424520Snw141292 goto out;
38434520Snw141292
38445127Sdm199847 if (EMPTY_STRING(req->id1name))
38454520Snw141292 goto out;
38464520Snw141292
38474520Snw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache "
38485696Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) "
38495696Snw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ",
38505696Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid,
38515696Snw141292 req->id1name, req->id1domain,
385212508Samw@Sun.COM req->id1.idtype);
38534520Snw141292
38544520Snw141292 if (sql == NULL) {
38554520Snw141292 retcode = IDMAP_ERR_INTERNAL;
38564520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
38574520Snw141292 goto out;
38584520Snw141292 }
38594520Snw141292
38606616Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql);
38614520Snw141292
38624520Snw141292 out:
38634864Sbaban if (sql != NULL)
38644520Snw141292 sqlite_freemem(sql);
38654520Snw141292 return (retcode);
38664520Snw141292 }
38674520Snw141292
38685696Snw141292 static
38695696Snw141292 idmap_retcode
lookup_cache_pid2sid(sqlite * cache,idmap_mapping * req,idmap_id_res * res,int is_user)38704520Snw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res,
387111337SWilliam.Krier@Sun.COM int is_user)
38725696Snw141292 {
38734520Snw141292 char *end;
38744520Snw141292 char *sql = NULL;
38754520Snw141292 const char **values;
38764520Snw141292 sqlite_vm *vm = NULL;
38774520Snw141292 int ncol;
38784520Snw141292 idmap_retcode retcode = IDMAP_SUCCESS;
38794520Snw141292 time_t curtime;
38805731Sbaban idmap_id_type idtype;
38814520Snw141292
38824520Snw141292 /* Current time */
38834520Snw141292 errno = 0;
38844520Snw141292 if ((curtime = time(NULL)) == (time_t)-1) {
38855696Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)",
38865696Snw141292 strerror(errno));
38874520Snw141292 retcode = IDMAP_ERR_INTERNAL;
38884520Snw141292 goto out;
38894520Snw141292 }
38904520Snw141292
38915731Sbaban /* SQL to lookup the cache by pid or by unixname */
389211963SAfshin.Ardakani@Sun.COM if (req->id1.idmap_id_u.uid != IDMAP_SENTINEL_PID) {
38936386Sjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, "
38946386Sjp151216 "canon_winname, windomain, w2u, is_wuser, "
38956386Sjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, "
38966386Sjp151216 "map_winname, map_unixname, map_is_nt4 "
38975731Sbaban "FROM idmap_cache WHERE "
38985731Sbaban "pid = %u AND u2w = 1 AND is_user = %d AND "
38995731Sbaban "(pid >= 2147483648 OR "
39005731Sbaban "(expiration = 0 OR expiration ISNULL OR "
39015731Sbaban "expiration > %d));",
39025731Sbaban req->id1.idmap_id_u.uid, is_user, curtime);
39035731Sbaban } else if (req->id1name != NULL) {
39046386Sjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, "
39056386Sjp151216 "canon_winname, windomain, w2u, is_wuser, "
39066386Sjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, "
39076386Sjp151216 "map_winname, map_unixname, map_is_nt4 "
39085731Sbaban "FROM idmap_cache WHERE "
39095731Sbaban "unixname = %Q AND u2w = 1 AND is_user = %d AND "
39105731Sbaban "(pid >= 2147483648 OR "
39115731Sbaban "(expiration = 0 OR expiration ISNULL OR "
39125731Sbaban "expiration > %d));",
39135731Sbaban req->id1name, is_user, curtime);
39146386Sjp151216 } else {
39156386Sjp151216 retcode = IDMAP_ERR_ARG;
39166386Sjp151216 goto out;
39175731Sbaban }
39185731Sbaban
39194520Snw141292 if (sql == NULL) {
39204520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
39214520Snw141292 retcode = IDMAP_ERR_MEMORY;
39224520Snw141292 goto out;
39234520Snw141292 }
39246386Sjp151216 retcode = sql_compile_n_step_once(
39256386Sjp151216 cache, sql, &vm, &ncol, 14, &values);
39264520Snw141292 sqlite_freemem(sql);
39274520Snw141292
39284520Snw141292 if (retcode == IDMAP_ERR_NOTFOUND)
39294520Snw141292 goto out;
39304520Snw141292 else if (retcode == IDMAP_SUCCESS) {
39314520Snw141292 /* sanity checks */
39324520Snw141292 if (values[0] == NULL || values[1] == NULL) {
39334520Snw141292 retcode = IDMAP_ERR_CACHE;
39344520Snw141292 goto out;
39354520Snw141292 }
39364520Snw141292
39375731Sbaban switch (res->id.idtype) {
39384520Snw141292 case IDMAP_SID:
39395696Snw141292 case IDMAP_USID:
39405696Snw141292 case IDMAP_GSID:
39415731Sbaban idtype = strtol(values[5], &end, 10) == 1
39425696Snw141292 ? IDMAP_USID : IDMAP_GSID;
39435696Snw141292
39445731Sbaban if (res->id.idtype == IDMAP_USID &&
39455731Sbaban idtype != IDMAP_USID) {
39465696Snw141292 retcode = IDMAP_ERR_NOTUSER;
39475696Snw141292 goto out;
39485731Sbaban } else if (res->id.idtype == IDMAP_GSID &&
39495731Sbaban idtype != IDMAP_GSID) {
39505696Snw141292 retcode = IDMAP_ERR_NOTGROUP;
39515696Snw141292 goto out;
39525696Snw141292 }
39535731Sbaban res->id.idtype = idtype;
39545696Snw141292
39554520Snw141292 res->id.idmap_id_u.sid.rid =
39565696Snw141292 strtoul(values[1], &end, 10);
39574520Snw141292 res->id.idmap_id_u.sid.prefix = strdup(values[0]);
39584520Snw141292 if (res->id.idmap_id_u.sid.prefix == NULL) {
39594520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
39604520Snw141292 retcode = IDMAP_ERR_MEMORY;
39614520Snw141292 goto out;
39624520Snw141292 }
39634520Snw141292
39644864Sbaban if (values[4] != NULL)
39654520Snw141292 res->direction =
39664644Sbaban (strtol(values[4], &end, 10) == 0)?
39674644Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
39684520Snw141292 else
39694644Sbaban res->direction = IDMAP_DIRECTION_U2W;
39704520Snw141292
397111337SWilliam.Krier@Sun.COM if (values[2] == NULL)
39724520Snw141292 break;
39735064Sdm199847 req->id2name = strdup(values[2]);
39745064Sdm199847 if (req->id2name == NULL) {
39754520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
39764520Snw141292 retcode = IDMAP_ERR_MEMORY;
39774520Snw141292 goto out;
39784520Snw141292 }
39794520Snw141292
39804520Snw141292 if (values[3] == NULL)
39814520Snw141292 break;
39825064Sdm199847 req->id2domain = strdup(values[3]);
39835064Sdm199847 if (req->id2domain == NULL) {
39844520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
39854520Snw141292 retcode = IDMAP_ERR_MEMORY;
39864520Snw141292 goto out;
39874520Snw141292 }
39885696Snw141292
39894520Snw141292 break;
39904520Snw141292 default:
39914520Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED;
39924520Snw141292 break;
39934520Snw141292 }
39946386Sjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) {
39956386Sjp151216 res->info.src = IDMAP_MAP_SRC_CACHE;
39966386Sjp151216 res->info.how.map_type = strtoul(values[6], &end, 10);
39976386Sjp151216 switch (res->info.how.map_type) {
39986386Sjp151216 case IDMAP_MAP_TYPE_DS_AD:
39996386Sjp151216 res->info.how.idmap_how_u.ad.dn =
40006386Sjp151216 strdup(values[7]);
40016386Sjp151216 res->info.how.idmap_how_u.ad.attr =
40026386Sjp151216 strdup(values[8]);
40036386Sjp151216 res->info.how.idmap_how_u.ad.value =
40046386Sjp151216 strdup(values[9]);
40056386Sjp151216 break;
40066386Sjp151216
40076386Sjp151216 case IDMAP_MAP_TYPE_DS_NLDAP:
40086386Sjp151216 res->info.how.idmap_how_u.nldap.dn =
40096386Sjp151216 strdup(values[7]);
40106386Sjp151216 res->info.how.idmap_how_u.nldap.attr =
40116386Sjp151216 strdup(values[8]);
40126386Sjp151216 res->info.how.idmap_how_u.nldap.value =
40136386Sjp151216 strdup(values[9]);
40146386Sjp151216 break;
40156386Sjp151216
40166386Sjp151216 case IDMAP_MAP_TYPE_RULE_BASED:
40176386Sjp151216 res->info.how.idmap_how_u.rule.windomain =
40186386Sjp151216 strdup(values[10]);
40196386Sjp151216 res->info.how.idmap_how_u.rule.winname =
40206386Sjp151216 strdup(values[11]);
40216386Sjp151216 res->info.how.idmap_how_u.rule.unixname =
40226386Sjp151216 strdup(values[12]);
40236386Sjp151216 res->info.how.idmap_how_u.rule.is_nt4 =
40246386Sjp151216 strtoul(values[13], &end, 10);
40256386Sjp151216 res->info.how.idmap_how_u.rule.is_user =
40266386Sjp151216 is_user;
40276386Sjp151216 res->info.how.idmap_how_u.rule.is_wuser =
40286386Sjp151216 strtol(values[5], &end, 10);
40296386Sjp151216 break;
40306386Sjp151216
40316386Sjp151216 case IDMAP_MAP_TYPE_EPHEMERAL:
40326386Sjp151216 break;
40336386Sjp151216
40346386Sjp151216 case IDMAP_MAP_TYPE_LOCAL_SID:
40356386Sjp151216 break;
40366386Sjp151216
40376386Sjp151216 case IDMAP_MAP_TYPE_KNOWN_SID:
40386386Sjp151216 break;
40396386Sjp151216
404010504SKeyur.Desai@Sun.COM case IDMAP_MAP_TYPE_IDMU:
404110504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.dn =
404210504SKeyur.Desai@Sun.COM strdup(values[7]);
404310504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.attr =
404410504SKeyur.Desai@Sun.COM strdup(values[8]);
404510504SKeyur.Desai@Sun.COM res->info.how.idmap_how_u.idmu.value =
404610504SKeyur.Desai@Sun.COM strdup(values[9]);
404710504SKeyur.Desai@Sun.COM break;
404810504SKeyur.Desai@Sun.COM
40496386Sjp151216 default:
405010504SKeyur.Desai@Sun.COM /* Unknown mapping type */
40516386Sjp151216 assert(FALSE);
40526386Sjp151216 }
40536386Sjp151216 }
40544520Snw141292 }
40554520Snw141292
40564520Snw141292 out:
40574864Sbaban if (vm != NULL)
40584520Snw141292 (void) sqlite_finalize(vm, NULL);
40594520Snw141292 return (retcode);
40604520Snw141292 }
40614520Snw141292
40629422SAfshin.Ardakani@Sun.COM /*
40639422SAfshin.Ardakani@Sun.COM * Given:
40649422SAfshin.Ardakani@Sun.COM * cache sqlite handle
40659422SAfshin.Ardakani@Sun.COM * name Windows user name
40669422SAfshin.Ardakani@Sun.COM * domain Windows domain name
40679422SAfshin.Ardakani@Sun.COM *
40689422SAfshin.Ardakani@Sun.COM * Return: Error code
40699422SAfshin.Ardakani@Sun.COM *
40709422SAfshin.Ardakani@Sun.COM * *canonname Canonical name (if canonname is non-NULL) [1]
40719422SAfshin.Ardakani@Sun.COM * *sidprefix SID prefix [1]
40729422SAfshin.Ardakani@Sun.COM * *rid RID
40739422SAfshin.Ardakani@Sun.COM * *type Type of name
40749422SAfshin.Ardakani@Sun.COM *
40759422SAfshin.Ardakani@Sun.COM * [1] malloc'ed, NULL on error
40769422SAfshin.Ardakani@Sun.COM */
40775696Snw141292 static
40785696Snw141292 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)407912508Samw@Sun.COM lookup_cache_name2sid(
408012508Samw@Sun.COM sqlite *cache,
408112508Samw@Sun.COM const char *name,
408212508Samw@Sun.COM const char *domain,
408312508Samw@Sun.COM char **canonname,
408412508Samw@Sun.COM char **sidprefix,
408512508Samw@Sun.COM idmap_rid_t *rid,
408612508Samw@Sun.COM idmap_id_type *type)
40875696Snw141292 {
40885696Snw141292 char *end, *lower_name;
40899422SAfshin.Ardakani@Sun.COM char *sql;
40904520Snw141292 const char **values;
40914520Snw141292 sqlite_vm *vm = NULL;
40924520Snw141292 int ncol;
40934520Snw141292 time_t curtime;
40949422SAfshin.Ardakani@Sun.COM idmap_retcode retcode;
40959422SAfshin.Ardakani@Sun.COM
40969422SAfshin.Ardakani@Sun.COM *sidprefix = NULL;
40979422SAfshin.Ardakani@Sun.COM if (canonname != NULL)
40989422SAfshin.Ardakani@Sun.COM *canonname = NULL;
40994520Snw141292
41004520Snw141292 /* Get current time */
41014520Snw141292 errno = 0;
41024520Snw141292 if ((curtime = time(NULL)) == (time_t)-1) {
41035696Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)",
41045696Snw141292 strerror(errno));
41054520Snw141292 retcode = IDMAP_ERR_INTERNAL;
41064520Snw141292 goto out;
41074520Snw141292 }
41084520Snw141292
41094520Snw141292 /* SQL to lookup the cache */
41105696Snw141292 if ((lower_name = tolower_u8(name)) == NULL)
41115696Snw141292 lower_name = (char *)name;
41125696Snw141292 sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name "
41135696Snw141292 "FROM name_cache WHERE name = %Q AND domain = %Q AND "
41145696Snw141292 "(expiration = 0 OR expiration ISNULL OR "
41155696Snw141292 "expiration > %d);", lower_name, domain, curtime);
41165696Snw141292 if (lower_name != name)
41175696Snw141292 free(lower_name);
41184520Snw141292 if (sql == NULL) {
41194520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
41204520Snw141292 retcode = IDMAP_ERR_MEMORY;
41214520Snw141292 goto out;
41224520Snw141292 }
41235696Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values);
41249422SAfshin.Ardakani@Sun.COM
41254520Snw141292 sqlite_freemem(sql);
41264520Snw141292
41279422SAfshin.Ardakani@Sun.COM if (retcode != IDMAP_SUCCESS)
41289422SAfshin.Ardakani@Sun.COM goto out;
41299422SAfshin.Ardakani@Sun.COM
41309422SAfshin.Ardakani@Sun.COM if (type != NULL) {
41319422SAfshin.Ardakani@Sun.COM if (values[2] == NULL) {
41325731Sbaban retcode = IDMAP_ERR_CACHE;
41335731Sbaban goto out;
41345731Sbaban }
413512508Samw@Sun.COM *type = xlate_legacy_type(strtol(values[2], &end, 10));
41369422SAfshin.Ardakani@Sun.COM }
41379422SAfshin.Ardakani@Sun.COM
41389422SAfshin.Ardakani@Sun.COM if (values[0] == NULL || values[1] == NULL) {
41399422SAfshin.Ardakani@Sun.COM retcode = IDMAP_ERR_CACHE;
41409422SAfshin.Ardakani@Sun.COM goto out;
41419422SAfshin.Ardakani@Sun.COM }
41429422SAfshin.Ardakani@Sun.COM
41439422SAfshin.Ardakani@Sun.COM if (canonname != NULL) {
41449422SAfshin.Ardakani@Sun.COM assert(values[3] != NULL);
41459422SAfshin.Ardakani@Sun.COM *canonname = strdup(values[3]);
41469422SAfshin.Ardakani@Sun.COM if (*canonname == NULL) {
41474520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
41484520Snw141292 retcode = IDMAP_ERR_MEMORY;
41494520Snw141292 goto out;
41504520Snw141292 }
41514520Snw141292 }
41524520Snw141292
41539422SAfshin.Ardakani@Sun.COM *sidprefix = strdup(values[0]);
41549422SAfshin.Ardakani@Sun.COM if (*sidprefix == NULL) {
41559422SAfshin.Ardakani@Sun.COM idmapdlog(LOG_ERR, "Out of memory");
41569422SAfshin.Ardakani@Sun.COM retcode = IDMAP_ERR_MEMORY;
41579422SAfshin.Ardakani@Sun.COM goto out;
41589422SAfshin.Ardakani@Sun.COM }
41599422SAfshin.Ardakani@Sun.COM *rid = strtoul(values[1], &end, 10);
41609422SAfshin.Ardakani@Sun.COM
41619422SAfshin.Ardakani@Sun.COM retcode = IDMAP_SUCCESS;
41629422SAfshin.Ardakani@Sun.COM
41634520Snw141292 out:
41644864Sbaban if (vm != NULL)
41654520Snw141292 (void) sqlite_finalize(vm, NULL);
41669422SAfshin.Ardakani@Sun.COM
41679422SAfshin.Ardakani@Sun.COM if (retcode != IDMAP_SUCCESS) {
41689422SAfshin.Ardakani@Sun.COM free(*sidprefix);
41699422SAfshin.Ardakani@Sun.COM *sidprefix = NULL;
41709422SAfshin.Ardakani@Sun.COM if (canonname != NULL) {
41719422SAfshin.Ardakani@Sun.COM free(*canonname);
41729422SAfshin.Ardakani@Sun.COM *canonname = NULL;
41739422SAfshin.Ardakani@Sun.COM }
41749422SAfshin.Ardakani@Sun.COM }
41754520Snw141292 return (retcode);
41764520Snw141292 }
41774520Snw141292
41785696Snw141292 static
41795696Snw141292 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)41805731Sbaban ad_lookup_by_winname(lookup_state_t *state,
418112508Samw@Sun.COM const char *name, const char *domain, int esidtype,
41826386Sjp151216 char **dn, char **attr, char **value, char **canonname,
418312508Samw@Sun.COM char **sidprefix, idmap_rid_t *rid, idmap_id_type *wintype,
41846386Sjp151216 char **unixname)
41855696Snw141292 {
41868361SJulian.Pullen@Sun.COM int retries;
41874520Snw141292 idmap_query_state_t *qs = NULL;
41884520Snw141292 idmap_retcode rc, retcode;
41898361SJulian.Pullen@Sun.COM int i;
41908361SJulian.Pullen@Sun.COM int found_ad = 0;
41918361SJulian.Pullen@Sun.COM
41928040SBaban.Kenkre@Sun.COM RDLOCK_CONFIG();
419310504SKeyur.Desai@Sun.COM if (_idmapdstate.num_gcs > 0) {
419410504SKeyur.Desai@Sun.COM for (i = 0; i < _idmapdstate.num_gcs && !found_ad; i++) {
41958361SJulian.Pullen@Sun.COM retries = 0;
41968361SJulian.Pullen@Sun.COM retry:
419710504SKeyur.Desai@Sun.COM retcode = idmap_lookup_batch_start(
419810504SKeyur.Desai@Sun.COM _idmapdstate.gcs[i],
419910504SKeyur.Desai@Sun.COM 1,
420010504SKeyur.Desai@Sun.COM _idmapdstate.cfg->pgcfg.directory_based_mapping,
420110504SKeyur.Desai@Sun.COM _idmapdstate.cfg->pgcfg.default_domain,
420210504SKeyur.Desai@Sun.COM &qs);
42038361SJulian.Pullen@Sun.COM if (retcode != IDMAP_SUCCESS) {
42048361SJulian.Pullen@Sun.COM if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
42058361SJulian.Pullen@Sun.COM retries++ < ADUTILS_DEF_NUM_RETRIES)
42068361SJulian.Pullen@Sun.COM goto retry;
42078361SJulian.Pullen@Sun.COM degrade_svc(1, "failed to create request for "
42088361SJulian.Pullen@Sun.COM "AD lookup by winname");
42098361SJulian.Pullen@Sun.COM return (retcode);
42108361SJulian.Pullen@Sun.COM }
42118361SJulian.Pullen@Sun.COM
42128361SJulian.Pullen@Sun.COM restore_svc();
42138361SJulian.Pullen@Sun.COM
42148361SJulian.Pullen@Sun.COM if (state != NULL && i == 0) {
42158361SJulian.Pullen@Sun.COM /*
42168361SJulian.Pullen@Sun.COM * Directory based name mapping is only
42178361SJulian.Pullen@Sun.COM * performed within the joined forest (i == 0).
42188361SJulian.Pullen@Sun.COM * We don't trust other "trusted" forests to
42198361SJulian.Pullen@Sun.COM * provide DS-based name mapping information
42208361SJulian.Pullen@Sun.COM * because AD's definition of "cross-forest
42218361SJulian.Pullen@Sun.COM * trust" does not encompass this sort of
42228361SJulian.Pullen@Sun.COM * behavior.
42238361SJulian.Pullen@Sun.COM */
42248361SJulian.Pullen@Sun.COM idmap_lookup_batch_set_unixattr(qs,
42258361SJulian.Pullen@Sun.COM state->ad_unixuser_attr,
42268361SJulian.Pullen@Sun.COM state->ad_unixgroup_attr);
42278361SJulian.Pullen@Sun.COM }
42288361SJulian.Pullen@Sun.COM
42298361SJulian.Pullen@Sun.COM retcode = idmap_name2sid_batch_add1(qs, name, domain,
423012508Samw@Sun.COM esidtype, dn, attr, value, canonname, sidprefix,
423110504SKeyur.Desai@Sun.COM rid, wintype, unixname, NULL, &rc);
42328361SJulian.Pullen@Sun.COM if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
42338361SJulian.Pullen@Sun.COM idmap_lookup_release_batch(&qs);
42348361SJulian.Pullen@Sun.COM continue;
42358361SJulian.Pullen@Sun.COM }
42368361SJulian.Pullen@Sun.COM found_ad = 1;
42378361SJulian.Pullen@Sun.COM if (retcode != IDMAP_SUCCESS)
42388361SJulian.Pullen@Sun.COM idmap_lookup_release_batch(&qs);
42398361SJulian.Pullen@Sun.COM else
42408361SJulian.Pullen@Sun.COM retcode = idmap_lookup_batch_end(&qs);
42418361SJulian.Pullen@Sun.COM
42428361SJulian.Pullen@Sun.COM if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
42438361SJulian.Pullen@Sun.COM retries++ < ADUTILS_DEF_NUM_RETRIES)
42448361SJulian.Pullen@Sun.COM goto retry;
42458361SJulian.Pullen@Sun.COM else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
42468361SJulian.Pullen@Sun.COM degrade_svc(1,
42478361SJulian.Pullen@Sun.COM "some AD lookups timed out repeatedly");
42488361SJulian.Pullen@Sun.COM }
42498361SJulian.Pullen@Sun.COM } else {
42508361SJulian.Pullen@Sun.COM /* No AD case */
42518361SJulian.Pullen@Sun.COM retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY;
42524520Snw141292 }
42538361SJulian.Pullen@Sun.COM UNLOCK_CONFIG();
42544520Snw141292
42554520Snw141292 if (retcode != IDMAP_SUCCESS) {
425611963SAfshin.Ardakani@Sun.COM idmapdlog(LOG_NOTICE,
425711963SAfshin.Ardakani@Sun.COM "AD lookup of winname %s@%s failed, error code %d",
425811963SAfshin.Ardakani@Sun.COM name == NULL ? "(null)" : name,
425911963SAfshin.Ardakani@Sun.COM domain == NULL ? "(null)" : domain,
426011963SAfshin.Ardakani@Sun.COM retcode);
42614520Snw141292 return (retcode);
42625731Sbaban }
42635731Sbaban return (rc);
42644520Snw141292 }
42654520Snw141292
42669422SAfshin.Ardakani@Sun.COM /*
42679422SAfshin.Ardakani@Sun.COM * Given:
42689422SAfshin.Ardakani@Sun.COM * cache sqlite handle to cache
42699422SAfshin.Ardakani@Sun.COM * name Windows user name
42709422SAfshin.Ardakani@Sun.COM * domain Windows domain name
42719422SAfshin.Ardakani@Sun.COM * local_only if true, don't try AD lookups
42729422SAfshin.Ardakani@Sun.COM *
42739422SAfshin.Ardakani@Sun.COM * Returns: Error code
42749422SAfshin.Ardakani@Sun.COM *
42759422SAfshin.Ardakani@Sun.COM * *canonname Canonical name (if non-NULL) [1]
42769422SAfshin.Ardakani@Sun.COM * *canondomain Canonical domain (if non-NULL) [1]
42779422SAfshin.Ardakani@Sun.COM * *sidprefix SID prefix [1]
42789422SAfshin.Ardakani@Sun.COM * *rid RID
42799422SAfshin.Ardakani@Sun.COM * *req Request (direction is updated)
42809422SAfshin.Ardakani@Sun.COM *
42819422SAfshin.Ardakani@Sun.COM * [1] malloc'ed, NULL on error
42829422SAfshin.Ardakani@Sun.COM */
42835696Snw141292 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)42849422SAfshin.Ardakani@Sun.COM lookup_name2sid(
42859422SAfshin.Ardakani@Sun.COM sqlite *cache,
42869422SAfshin.Ardakani@Sun.COM const char *name,
42879422SAfshin.Ardakani@Sun.COM const char *domain,
428812508Samw@Sun.COM int want_wuser,
42899422SAfshin.Ardakani@Sun.COM char **canonname,
42909422SAfshin.Ardakani@Sun.COM char **canondomain,
42919422SAfshin.Ardakani@Sun.COM char **sidprefix,
42929422SAfshin.Ardakani@Sun.COM idmap_rid_t *rid,
429312508Samw@Sun.COM idmap_id_type *type,
42949422SAfshin.Ardakani@Sun.COM idmap_mapping *req,
42959422SAfshin.Ardakani@Sun.COM int local_only)
42965696Snw141292 {
42974520Snw141292 idmap_retcode retcode;
42984520Snw141292
42995696Snw141292 *sidprefix = NULL;
43005731Sbaban if (canonname != NULL)
43015731Sbaban *canonname = NULL;
43029422SAfshin.Ardakani@Sun.COM if (canondomain != NULL)
43039422SAfshin.Ardakani@Sun.COM *canondomain = NULL;
43045731Sbaban
43055731Sbaban /* Lookup well-known SIDs table */
43069422SAfshin.Ardakani@Sun.COM retcode = lookup_wksids_name2sid(name, domain, canonname, canondomain,
430712508Samw@Sun.COM sidprefix, rid, type);
43084864Sbaban if (retcode == IDMAP_SUCCESS) {
43095731Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
43104864Sbaban goto out;
43114864Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) {
43124864Sbaban return (retcode);
43134864Sbaban }
43144864Sbaban
43155731Sbaban /* Lookup cache */
43165696Snw141292 retcode = lookup_cache_name2sid(cache, name, domain, canonname,
431712508Samw@Sun.COM sidprefix, rid, type);
43185731Sbaban if (retcode == IDMAP_SUCCESS) {
43195731Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE;
43205731Sbaban goto out;
43215731Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) {
43224520Snw141292 return (retcode);
43234520Snw141292 }
43244520Snw141292
43256616Sdm199847 /*
43266616Sdm199847 * The caller may be using this function to determine if this
43276616Sdm199847 * request needs to be marked for AD lookup or not
43286616Sdm199847 * (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this
43296616Sdm199847 * function to AD lookup now.
43306616Sdm199847 */
43316616Sdm199847 if (local_only)
43326616Sdm199847 return (retcode);
43336616Sdm199847
433412508Samw@Sun.COM if (_idmapdstate.cfg->pgcfg.use_lsa &&
433512508Samw@Sun.COM _idmapdstate.cfg->pgcfg.domain_name != NULL &&
433612508Samw@Sun.COM name != NULL && *sidprefix == NULL) {
433712508Samw@Sun.COM retcode = lookup_lsa_by_name(name, domain,
433812508Samw@Sun.COM sidprefix, rid,
433912508Samw@Sun.COM canonname, canondomain,
434012508Samw@Sun.COM type);
434112508Samw@Sun.COM if (retcode == IDMAP_SUCCESS)
434212508Samw@Sun.COM goto out;
434312508Samw@Sun.COM else if (retcode != IDMAP_ERR_NOTFOUND)
434412508Samw@Sun.COM return (retcode);
434512508Samw@Sun.COM }
434612508Samw@Sun.COM
43475731Sbaban /* Lookup AD */
434812508Samw@Sun.COM retcode = ad_lookup_by_winname(NULL, name, domain, IDMAP_POSIXID,
434912508Samw@Sun.COM NULL, NULL, NULL, canonname, sidprefix, rid, type, NULL);
43505731Sbaban if (retcode != IDMAP_SUCCESS)
43515731Sbaban return (retcode);
43525731Sbaban
43534864Sbaban out:
43544520Snw141292 /*
43554520Snw141292 * Entry found (cache or Windows lookup)
43564520Snw141292 */
435712508Samw@Sun.COM if (want_wuser == 1 && *type != IDMAP_USID)
43585731Sbaban retcode = IDMAP_ERR_NOTUSER;
435912508Samw@Sun.COM else if (want_wuser == 0 && *type != IDMAP_GSID)
43605731Sbaban retcode = IDMAP_ERR_NOTGROUP;
436112508Samw@Sun.COM else if (want_wuser == -1) {
436212508Samw@Sun.COM /*
436312508Samw@Sun.COM * Caller wants to know if its user or group
436412508Samw@Sun.COM * Verify that it's one or the other.
436512508Samw@Sun.COM */
436612508Samw@Sun.COM if (*type != IDMAP_USID && *type != IDMAP_GSID)
43675731Sbaban retcode = IDMAP_ERR_SID;
43685731Sbaban }
43695731Sbaban
43709422SAfshin.Ardakani@Sun.COM if (retcode == IDMAP_SUCCESS) {
43719422SAfshin.Ardakani@Sun.COM /*
43729422SAfshin.Ardakani@Sun.COM * If we were asked for a canonical domain and none
43739422SAfshin.Ardakani@Sun.COM * of the searches have provided one, assume it's the
43749422SAfshin.Ardakani@Sun.COM * supplied domain.
43759422SAfshin.Ardakani@Sun.COM */
43769422SAfshin.Ardakani@Sun.COM if (canondomain != NULL && *canondomain == NULL) {
43779422SAfshin.Ardakani@Sun.COM *canondomain = strdup(domain);
43789422SAfshin.Ardakani@Sun.COM if (*canondomain == NULL)
43799422SAfshin.Ardakani@Sun.COM retcode = IDMAP_ERR_MEMORY;
43809422SAfshin.Ardakani@Sun.COM }
43819422SAfshin.Ardakani@Sun.COM }
43825731Sbaban if (retcode != IDMAP_SUCCESS) {
43835731Sbaban free(*sidprefix);
43845731Sbaban *sidprefix = NULL;
43855731Sbaban if (canonname != NULL) {
43865731Sbaban free(*canonname);
43875731Sbaban *canonname = NULL;
43885696Snw141292 }
43899422SAfshin.Ardakani@Sun.COM if (canondomain != NULL) {
43909422SAfshin.Ardakani@Sun.COM free(*canondomain);
43919422SAfshin.Ardakani@Sun.COM *canondomain = NULL;
43929422SAfshin.Ardakani@Sun.COM }
43934520Snw141292 }
43944520Snw141292 return (retcode);
43954520Snw141292 }
43964520Snw141292
43975696Snw141292 static
43985696Snw141292 idmap_retcode
name_based_mapping_pid2sid(lookup_state_t * state,const char * unixname,int is_user,idmap_mapping * req,idmap_id_res * res)43996616Sdm199847 name_based_mapping_pid2sid(lookup_state_t *state, const char *unixname,
44005696Snw141292 int is_user, idmap_mapping *req, idmap_id_res *res)
44015696Snw141292 {
44024520Snw141292 const char *winname, *windomain;
44035696Snw141292 char *canonname;
44049422SAfshin.Ardakani@Sun.COM char *canondomain;
44054520Snw141292 char *sql = NULL, *errmsg = NULL;
44064520Snw141292 idmap_retcode retcode;
44074520Snw141292 char *end;
44084520Snw141292 const char **values;
44094520Snw141292 sqlite_vm *vm = NULL;
44106386Sjp151216 int ncol, r;
441112508Samw@Sun.COM int want_wuser;
44124520Snw141292 const char *me = "name_based_mapping_pid2sid";
44136386Sjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule;
44146386Sjp151216 int direction;
44155731Sbaban
44165731Sbaban assert(unixname != NULL); /* We have unixname */
44175731Sbaban assert(req->id2name == NULL); /* We don't have winname */
44185731Sbaban assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */
44194520Snw141292
44204520Snw141292 sql = sqlite_mprintf(
44216386Sjp151216 "SELECT winname_display, windomain, w2u_order, "
44226386Sjp151216 "is_wuser, unixname, is_nt4 "
44236386Sjp151216 "FROM namerules WHERE "
44245696Snw141292 "u2w_order > 0 AND is_user = %d AND "
44255696Snw141292 "(unixname = %Q OR unixname = '*') "
44265696Snw141292 "ORDER BY u2w_order ASC;", is_user, unixname);
44274520Snw141292 if (sql == NULL) {
44284520Snw141292 idmapdlog(LOG_ERR, "Out of memory");
44294520Snw141292 retcode = IDMAP_ERR_MEMORY;
44304520Snw141292 goto out;
44314520Snw141292 }
44324520Snw141292
44336616Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) {
44344520Snw141292 retcode = IDMAP_ERR_INTERNAL;
44355696Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me,
44365696Snw141292 CHECK_NULL(errmsg));
44374520Snw141292 sqlite_freemem(errmsg);
44384520Snw141292 goto out;
44394520Snw141292 }
44404520Snw141292
44416386Sjp151216 for (;;) {
44424520Snw141292 r = sqlite_step(vm, &ncol, &values, NULL);
44434884Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY);
44444884Sjp151216 if (r == SQLITE_ROW) {
44456386Sjp151216 if (ncol < 6) {
44464520Snw141292 retcode = IDMAP_ERR_INTERNAL;
44474520Snw141292 goto out;
44484520Snw141292 }
444912508Samw@Sun.COM
445012508Samw@Sun.COM TRACE(req, res, "Matching rule: %s -> %s@%s",
445112508Samw@Sun.COM values[4] == NULL ? "(null)" : values[4],
445212508Samw@Sun.COM values[0] == NULL ? "(null)" : values[0],
445312508Samw@Sun.COM values[1] == NULL ? "(null)" : values[1]);
445412508Samw@Sun.COM
44554520Snw141292 if (values[0] == NULL) {
44564520Snw141292 /* values [1] and [2] can be null */
44574520Snw141292 retcode = IDMAP_ERR_INTERNAL;
44584520Snw141292 goto out;
44594520Snw141292 }
44606386Sjp151216
44616386Sjp151216 if (values[2] != NULL)
44626386Sjp151216 direction =
44636386Sjp151216 (strtol(values[2], &end, 10) == 0)?
44646386Sjp151216 IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
44656386Sjp151216 else
44666386Sjp151216 direction = IDMAP_DIRECTION_U2W;
44676386Sjp151216
44684520Snw141292 if (EMPTY_NAME(values[0])) {
44696386Sjp151216 idmap_namerule_set(rule, values[1], values[0],
44706386Sjp151216 values[4], is_user,
44716386Sjp151216 strtol(values[3], &end, 10),
44726386Sjp151216 strtol(values[5], &end, 10),
44736386Sjp151216 direction);
447412508Samw@Sun.COM TRACE(req, res, "Mapping inhibited");
44754520Snw141292 retcode = IDMAP_ERR_NOMAPPING;
44764520Snw141292 goto out;
44774520Snw141292 }
44785696Snw141292
44795696Snw141292 if (values[0][0] == '*') {
44806386Sjp151216 winname = unixname;
44815696Snw141292 } else {
44825696Snw141292 winname = values[0];
44835696Snw141292 }
4484*12890SJoyce.McIntosh@Sun.COM
448512508Samw@Sun.COM want_wuser = res->id.idtype == IDMAP_USID ? 1
44866386Sjp151216 : res->id.idtype == IDMAP_GSID ? 0
44876386Sjp151216 : -1;
44884864Sbaban if (values[1] != NULL)
44894520Snw141292 windomain = values[1];
449012508Samw@Sun.COM else if (state->defdom != NULL) {
44916616Sdm199847 windomain = state->defdom;
449212508Samw@Sun.COM TRACE(req, res,
449312508Samw@Sun.COM "Added default domain %s to rule",
449412508Samw@Sun.COM windomain);
449512508Samw@Sun.COM } else {
44965696Snw141292 idmapdlog(LOG_ERR, "%s: no domain", me);
449712508Samw@Sun.COM TRACE(req, res,
449812508Samw@Sun.COM "No domain in rule, and no default domain");
44994520Snw141292 retcode = IDMAP_ERR_DOMAIN_NOTFOUND;
45004520Snw141292 goto out;
45014520Snw141292 }
45025696Snw141292
45036616Sdm199847 retcode = lookup_name2sid(state->cache,
45046616Sdm199847 winname, windomain,
450512508Samw@Sun.COM want_wuser, &canonname, &canondomain,
45065696Snw141292 &res->id.idmap_id_u.sid.prefix,
450712508Samw@Sun.COM &res->id.idmap_id_u.sid.rid,
450812508Samw@Sun.COM &res->id.idtype, req, 0);
450912508Samw@Sun.COM
451012508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
451112508Samw@Sun.COM break;
451212508Samw@Sun.COM } else if (retcode == IDMAP_ERR_NOTFOUND) {
4513*12890SJoyce.McIntosh@Sun.COM if (values[0][0] == '*') {
4514*12890SJoyce.McIntosh@Sun.COM TRACE(req, res,
4515*12890SJoyce.McIntosh@Sun.COM "%s@%s not found, continuing",
4516*12890SJoyce.McIntosh@Sun.COM winname, windomain);
4517*12890SJoyce.McIntosh@Sun.COM continue;
4518*12890SJoyce.McIntosh@Sun.COM } else {
4519*12890SJoyce.McIntosh@Sun.COM TRACE(req, res,
4520*12890SJoyce.McIntosh@Sun.COM "%s@%s not found",
4521*12890SJoyce.McIntosh@Sun.COM winname, windomain);
4522*12890SJoyce.McIntosh@Sun.COM retcode = IDMAP_ERR_NOMAPPING;
4523*12890SJoyce.McIntosh@Sun.COM }
452412508Samw@Sun.COM } else {
452512508Samw@Sun.COM TRACE(req, res,
452612508Samw@Sun.COM "Looking up %s@%s error=%d",
452712508Samw@Sun.COM winname, windomain, retcode);
45284520Snw141292 }
452912508Samw@Sun.COM
4530*12890SJoyce.McIntosh@Sun.COM idmap_namerule_set(rule, values[1],
4531*12890SJoyce.McIntosh@Sun.COM values[0], values[4], is_user,
4532*12890SJoyce.McIntosh@Sun.COM strtol(values[3], &end, 10),
4533*12890SJoyce.McIntosh@Sun.COM strtol(values[5], &end, 10),
4534*12890SJoyce.McIntosh@Sun.COM direction);
4535*12890SJoyce.McIntosh@Sun.COM
45364520Snw141292 goto out;
45376386Sjp151216
45384520Snw141292 } else if (r == SQLITE_DONE) {
4539*12890SJoyce.McIntosh@Sun.COM TRACE(req, res, "No matching rule");
4540*12890SJoyce.McIntosh@Sun.COM retcode = IDMAP_ERR_NOTFOUND;
45414520Snw141292 goto out;
45424520Snw141292 } else {
45434520Snw141292 (void) sqlite_finalize(vm, &errmsg);
45444520Snw141292 vm = NULL;
45455696Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me,
45465696Snw141292 CHECK_NULL(errmsg));
45474520Snw141292 sqlite_freemem(errmsg);
45484520Snw141292 retcode = IDMAP_ERR_INTERNAL;
45494520Snw141292 goto out;
45504520Snw141292 }
45514520Snw141292 }
45524520Snw141292
455312508Samw@Sun.COM if (values[2] != NULL)
455412508Samw@Sun.COM res->direction =
455512508Samw@Sun.COM (strtol(values[2], &end, 10) == 0)?
455612508Samw@Sun.COM IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI;
455712508Samw@Sun.COM else
455812508Samw@Sun.COM res->direction = IDMAP_DIRECTION_U2W;
455912508Samw@Sun.COM
456012508Samw@Sun.COM req->id2name = canonname;
456112508Samw@Sun.COM req->id2domain = canondomain;
456212508Samw@Sun.COM
456312508Samw@Sun.COM idmap_namerule_set(rule, values[1], values[0], values[4],
456412508Samw@Sun.COM is_user, strtol(values[3], &end, 10),
456512508Samw@Sun.COM strtol(values[5], &end, 10),
456612508Samw@Sun.COM rule->direction);
456712508Samw@Sun.COM TRACE(req, res, "Windows name found");
456812508Samw@Sun.COM
45694520Snw141292 out:
45704864Sbaban if (sql != NULL)
45714520Snw141292 sqlite_freemem(sql);
45729021Samw@Sun.COM
45739021Samw@Sun.COM if (retcode != IDMAP_ERR_NOTFOUND) {
45749021Samw@Sun.COM res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED;
45756386Sjp151216 res->info.src = IDMAP_MAP_SRC_NEW;
45764520Snw141292 }
45779021Samw@Sun.COM
45784864Sbaban if (vm != NULL)
45794520Snw141292 (void) sqlite_finalize(vm, NULL);
45804520Snw141292 return (retcode);
45814520Snw141292 }
45824520Snw141292
45835696Snw141292 /*
45845696Snw141292 * Convention when processing unix2win requests:
45855696Snw141292 *
45865696Snw141292 * Unix identity:
45875696Snw141292 * req->id1name =
45885696Snw141292 * unixname if given otherwise unixname found will be placed
45895696Snw141292 * here.
45905696Snw141292 * req->id1domain =
45915696Snw141292 * NOT USED
45925696Snw141292 * req->id1.idtype =
45935696Snw141292 * Given type (IDMAP_UID or IDMAP_GID)
45945696Snw141292 * req->id1..[uid or gid] =
45955696Snw141292 * UID/GID if given otherwise UID/GID found will be placed here.
45965696Snw141292 *
45975696Snw141292 * Windows identity:
45985696Snw141292 * req->id2name =
45995696Snw141292 * winname found will be placed here.
46005696Snw141292 * req->id2domain =
46015696Snw141292 * windomain found will be placed here.
46025696Snw141292 * res->id.idtype =
46035696Snw141292 * Target type initialized from req->id2.idtype. If
46045696Snw141292 * it is IDMAP_SID then actual type (IDMAP_USID/GSID) found
46055696Snw141292 * will be placed here.
46065696Snw141292 * req->id..sid.[prefix, rid] =
46075696Snw141292 * SID found will be placed here.
46085696Snw141292 *
46095696Snw141292 * Others:
46105696Snw141292 * res->retcode =
46115696Snw141292 * Return status for this request will be placed here.
46125696Snw141292 * res->direction =
46135696Snw141292 * Direction found will be placed here. Direction
46145696Snw141292 * meaning whether the resultant mapping is valid
46155696Snw141292 * only from unix2win or bi-directional.
46165696Snw141292 * req->direction =
46175696Snw141292 * INTERNAL USE. Used by idmapd to set various
46185696Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing
46195696Snw141292 * of the request.
46205696Snw141292 * req->id2.idtype =
46215696Snw141292 * INTERNAL USE. Initially this is the requested target
46225696Snw141292 * type and is used to initialize res->id.idtype.
46235696Snw141292 * ad_lookup_batch() uses this field temporarily to store
46245696Snw141292 * sid_type obtained by the batched AD lookups and after
46255696Snw141292 * use resets it to IDMAP_NONE to prevent xdr from
46265696Snw141292 * mis-interpreting the contents of req->id2.
46275696Snw141292 * req->id2..[uid or gid or sid] =
46285696Snw141292 * NOT USED
46295696Snw141292 */
46305696Snw141292
46315696Snw141292 /*
46325696Snw141292 * This function does the following:
46335696Snw141292 * 1. Lookup well-known SIDs table.
46345696Snw141292 * 2. Lookup cache.
46355696Snw141292 * 3. Check if the client does not want new mapping to be allocated
46365696Snw141292 * in which case this pass is the final pass.
46375731Sbaban * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs
46385731Sbaban * to do AD/NLDAP lookup.
46395696Snw141292 */
46404520Snw141292 idmap_retcode
pid2sid_first_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res,int is_user)46416616Sdm199847 pid2sid_first_pass(lookup_state_t *state, idmap_mapping *req,
464211337SWilliam.Krier@Sun.COM idmap_id_res *res, int is_user)
46435696Snw141292 {
46445731Sbaban idmap_retcode retcode;
464512508Samw@Sun.COM idmap_retcode retcode2;
46465731Sbaban bool_t gen_localsid_on_err = FALSE;
46475731Sbaban
46485731Sbaban /* Initialize result */
46494520Snw141292 res->id.idtype = req->id2.idtype;
46505731Sbaban res->direction = IDMAP_DIRECTION_UNDEF;
46515731Sbaban
46525731Sbaban if (req->id2.idmap_id_u.sid.prefix != NULL) {
46535731Sbaban /* sanitize sidprefix */
46545731Sbaban free(req->id2.idmap_id_u.sid.prefix);
46555731Sbaban req->id2.idmap_id_u.sid.prefix = NULL;
46565731Sbaban }
46575731Sbaban
46586386Sjp151216 /* Find pid */
465911963SAfshin.Ardakani@Sun.COM if (req->id1.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
466011337SWilliam.Krier@Sun.COM if (req->id1name == NULL) {
466111337SWilliam.Krier@Sun.COM retcode = IDMAP_ERR_ARG;
466211337SWilliam.Krier@Sun.COM goto out;
466311337SWilliam.Krier@Sun.COM }
466411337SWilliam.Krier@Sun.COM
466512508Samw@Sun.COM retcode = ns_lookup_byname(req->id1name, NULL, &req->id1);
466612508Samw@Sun.COM if (retcode != IDMAP_SUCCESS) {
466712508Samw@Sun.COM TRACE(req, res, "Getting UNIX ID error=%d", retcode);
46686386Sjp151216 retcode = IDMAP_ERR_NOMAPPING;
46696386Sjp151216 goto out;
46706386Sjp151216 }
467112508Samw@Sun.COM TRACE(req, res, "Found UNIX ID");
46726386Sjp151216 }
46736386Sjp151216
46749422SAfshin.Ardakani@Sun.COM /* Lookup in well-known SIDs table */
46754520Snw141292 retcode = lookup_wksids_pid2sid(req, res, is_user);
467612508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
467712508Samw@Sun.COM TRACE(req, res, "Hardwired mapping");
46784520Snw141292 goto out;
467912508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
468012508Samw@Sun.COM TRACE(req, res,
468112508Samw@Sun.COM "Well-known account lookup error=%d", retcode);
468212508Samw@Sun.COM goto out;
468312508Samw@Sun.COM }
46844520Snw141292
46859422SAfshin.Ardakani@Sun.COM /* Lookup in cache */
468611337SWilliam.Krier@Sun.COM retcode = lookup_cache_pid2sid(state->cache, req, res, is_user);
468712508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
468812508Samw@Sun.COM TRACE(req, res, "Found in mapping cache");
46894520Snw141292 goto out;
469012508Samw@Sun.COM } else if (retcode != IDMAP_ERR_NOTFOUND) {
469112508Samw@Sun.COM TRACE(req, res,
469212508Samw@Sun.COM "Mapping cache lookup error=%d", retcode);
469312508Samw@Sun.COM goto out;
469412508Samw@Sun.COM }
469512508Samw@Sun.COM TRACE(req, res, "Not found in mapping cache");
46964520Snw141292
46974520Snw141292 /* Ephemeral ids cannot be allocated during pid2sid */
469811963SAfshin.Ardakani@Sun.COM if (IDMAP_ID_IS_EPHEMERAL(req->id1.idmap_id_u.uid)) {
46994864Sbaban retcode = IDMAP_ERR_NOMAPPING;
470012508Samw@Sun.COM TRACE(req, res, "Shouldn't have an ephemeral ID here");
47014520Snw141292 goto out;
47024520Snw141292 }
47034520Snw141292
47046386Sjp151216 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) {
47058361SJulian.Pullen@Sun.COM retcode = IDMAP_ERR_NONE_GENERATED;
47066386Sjp151216 goto out;
47076386Sjp151216 }
47086386Sjp151216
47096386Sjp151216 if (AVOID_NAMESERVICE(req)) {
47105731Sbaban gen_localsid_on_err = TRUE;
47114864Sbaban retcode = IDMAP_ERR_NOMAPPING;
47124520Snw141292 goto out;
47134520Snw141292 }
47144520Snw141292
47155731Sbaban /* Set flags for the next stage */
471610504SKeyur.Desai@Sun.COM if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) {
471710504SKeyur.Desai@Sun.COM req->direction |= _IDMAP_F_LOOKUP_AD;
471810504SKeyur.Desai@Sun.COM state->ad_nqueries++;
471910504SKeyur.Desai@Sun.COM } else if (AD_MODE(req->id1.idtype, state)) {
47205731Sbaban /*
47215731Sbaban * If AD-based name mapping is enabled then the next stage
47225731Sbaban * will need to lookup AD using unixname to get the
47235731Sbaban * corresponding winname.
47245731Sbaban */
47255731Sbaban if (req->id1name == NULL) {
47265731Sbaban /* Get unixname if only pid is given. */
47275731Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid,
47285731Sbaban is_user, &req->id1name);
47296616Sdm199847 if (retcode != IDMAP_SUCCESS) {
473012508Samw@Sun.COM TRACE(req, res,
473112508Samw@Sun.COM "Getting UNIX name error=%d", retcode);
47326616Sdm199847 gen_localsid_on_err = TRUE;
47335731Sbaban goto out;
47346616Sdm199847 }
473512508Samw@Sun.COM TRACE(req, res, "Found UNIX name");
47364520Snw141292 }
47375731Sbaban req->direction |= _IDMAP_F_LOOKUP_AD;
47385731Sbaban state->ad_nqueries++;
47395731Sbaban } else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) {
47405731Sbaban /*
47415731Sbaban * If native LDAP or mixed mode is enabled for name mapping
47425731Sbaban * then the next stage will need to lookup native LDAP using
47435731Sbaban * unixname/pid to get the corresponding winname.
47445731Sbaban */
47455731Sbaban req->direction |= _IDMAP_F_LOOKUP_NLDAP;
47465731Sbaban state->nldap_nqueries++;
47475731Sbaban }
47485731Sbaban
47495731Sbaban /*
47505731Sbaban * Failed to find non-expired entry in cache. Set the flag to
47515731Sbaban * indicate that we are not done yet.
47525731Sbaban */
47535731Sbaban state->pid2sid_done = FALSE;
47545731Sbaban req->direction |= _IDMAP_F_NOTDONE;
47555731Sbaban retcode = IDMAP_SUCCESS;
47565731Sbaban
47575731Sbaban out:
47585731Sbaban res->retcode = idmap_stat4prot(retcode);
475912508Samw@Sun.COM if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) {
476012508Samw@Sun.COM if (gen_localsid_on_err == TRUE) {
476112508Samw@Sun.COM retcode2 = generate_localsid(req, res, is_user, TRUE);
476212508Samw@Sun.COM if (retcode2 == IDMAP_SUCCESS)
476312508Samw@Sun.COM TRACE(req, res, "Generate local SID");
476412508Samw@Sun.COM else
476512508Samw@Sun.COM TRACE(req, res,
476612508Samw@Sun.COM "Generate local SID error=%d", retcode2);
476712508Samw@Sun.COM }
476812508Samw@Sun.COM }
47695731Sbaban return (retcode);
47705731Sbaban }
47715731Sbaban
47725731Sbaban idmap_retcode
pid2sid_second_pass(lookup_state_t * state,idmap_mapping * req,idmap_id_res * res,int is_user)47736616Sdm199847 pid2sid_second_pass(lookup_state_t *state, idmap_mapping *req,
47746616Sdm199847 idmap_id_res *res, int is_user)
47755731Sbaban {
47765731Sbaban bool_t gen_localsid_on_err = TRUE;
47775731Sbaban idmap_retcode retcode = IDMAP_SUCCESS;
477812508Samw@Sun.COM idmap_retcode retcode2;
47795731Sbaban
47805731Sbaban /* Check if second pass is needed */
47815731Sbaban if (ARE_WE_DONE(req->direction))
47825731Sbaban return (res->retcode);
47835731Sbaban
47845731Sbaban /* Get status from previous pass */
47855731Sbaban retcode = res->retcode;
47865731Sbaban if (retcode != IDMAP_SUCCESS)
47875731Sbaban goto out;
47885731Sbaban
47895731Sbaban /*
47905731Sbaban * If directory-based name mapping is enabled then the winname
47915731Sbaban * may already have been retrieved from the AD object (AD-mode)
47926616Sdm199847 * or from native LDAP object (nldap-mode or mixed-mode).
47936616Sdm199847 * Note that if we have winname but no SID then it's an error
47946616Sdm199847 * because this implies that the Native LDAP entry contains
47956616Sdm199847 * winname which does not exist and it's better that we return
47966616Sdm199847 * an error instead of doing rule-based mapping so that the user
47976616Sdm199847 * can detect the issue and take appropriate action.
47985731Sbaban */
47996616Sdm199847 if (req->id2name != NULL) {
48006616Sdm199847 /* Return notfound if we've winname but no SID. */
48016616Sdm199847 if (res->id.idmap_id_u.sid.prefix == NULL) {
480212508Samw@Sun.COM TRACE(req, res, "Windows name but no SID");
48036616Sdm199847 retcode = IDMAP_ERR_NOTFOUND;
48046616Sdm199847 goto out;
48056616Sdm199847 }
480610504SKeyur.Desai@Sun.COM if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU)
480710504SKeyur.Desai@Sun.COM res->direction = IDMAP_DIRECTION_BI;
480810504SKeyur.Desai@Sun.COM else if (AD_MODE(req->id1.idtype, state))
48095731Sbaban res->direction = IDMAP_DIRECTION_BI;
48105731Sbaban else if (NLDAP_MODE(req->id1.idtype, state))
48115731Sbaban res->direction = IDMAP_DIRECTION_BI;
48125731Sbaban else if (MIXED_MODE(req->id1.idtype, state))
48135731Sbaban res->direction = IDMAP_DIRECTION_W2U;
48145731Sbaban goto out;
48156616Sdm199847 } else if (res->id.idmap_id_u.sid.prefix != NULL) {
48166616Sdm199847 /*
48176616Sdm199847 * We've SID but no winname. This is fine because
48186616Sdm199847 * the caller may have only requested SID.
48196616Sdm199847 */
48206616Sdm199847 goto out;
48215731Sbaban }
48225731Sbaban
48236616Sdm199847 /* Free any mapping info from Directory based mapping */
48246616Sdm199847 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN)
482512508Samw@Sun.COM idmap_how_clear(&res->info.how);
48266616Sdm199847
48275731Sbaban if (req->id1name == NULL) {
48285731Sbaban /* Get unixname from name service */
48295731Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user,
48305731Sbaban &req->id1name);
483112508Samw@Sun.COM if (retcode != IDMAP_SUCCESS) {
483212508Samw@Sun.COM TRACE(req, res,
483312508Samw@Sun.COM "Getting UNIX name error=%d", retcode);
48345731Sbaban goto out;
483512508Samw@Sun.COM }
483612508Samw@Sun.COM TRACE(req, res, "Found UNIX name");
483711963SAfshin.Ardakani@Sun.COM } else if (req->id1.idmap_id_u.uid == IDMAP_SENTINEL_PID) {
48385731Sbaban /* Get pid from name service */
48395731Sbaban retcode = ns_lookup_byname(req->id1name, NULL, &req->id1);
48405731Sbaban if (retcode != IDMAP_SUCCESS) {
484112508Samw@Sun.COM TRACE(req, res,
484212508Samw@Sun.COM "Getting UNIX ID error=%d", retcode);
48435731Sbaban gen_localsid_on_err = FALSE;
48445731Sbaban goto out;
48454520Snw141292 }
484612508Samw@Sun.COM TRACE(req, res, "Found UNIX ID");
48474520Snw141292 }
48484520Snw141292
48495731Sbaban /* Use unixname to evaluate local name-based mapping rules */
48506616Sdm199847 retcode = name_based_mapping_pid2sid(state, req->id1name, is_user,
48515696Snw141292 req, res);
48524520Snw141292 if (retcode == IDMAP_ERR_NOTFOUND) {
48536386Sjp151216 retcode = generate_localsid(req, res, is_user, FALSE);
485412508Samw@Sun.COM if (retcode == IDMAP_SUCCESS) {
485512508Samw@Sun.COM TRACE(req, res, "Generated local SID");
485612508Samw@Sun.COM } else {
485712508Samw@Sun.COM TRACE(req, res,
485812508Samw@Sun.COM "Generating local SID error=%d", retcode);
485912508Samw@Sun.COM }
48605731Sbaban gen_localsid_on_err = FALSE;
48615731Sbaban }
48624520Snw141292
48634520Snw141292 out:
48645731Sbaban res->retcode = idmap_stat4prot(retcode);
48655731Sbaban if (res->retcode != IDMAP_SUCCESS) {
48665731Sbaban req->direction = _IDMAP_F_DONE;
48676616Sdm199847 free(req->id2name);
48686616Sdm199847 req->id2name = NULL;
48696616Sdm199847 free(req->id2domain);
48706616Sdm199847 req->id2domain = NULL;
487112508Samw@Sun.COM if (gen_localsid_on_err == TRUE) {
487212508Samw@Sun.COM retcode2 = generate_localsid(req, res, is_user, TRUE);
487312508Samw@Sun.COM if (retcode2 == IDMAP_SUCCESS)
487412508Samw@Sun.COM TRACE(req, res, "Generate local SID");
487512508Samw@Sun.COM else
487612508Samw@Sun.COM TRACE(req, res,
487712508Samw@Sun.COM "Generate local SID error=%d", retcode2);
487812508Samw@Sun.COM } else {
48796616Sdm199847 res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID;
488012508Samw@Sun.COM }
48814520Snw141292 }
48825731Sbaban if (!ARE_WE_DONE(req->direction))
48834520Snw141292 state->pid2sid_done = FALSE;
48844520Snw141292 return (retcode);
48854520Snw141292 }
488611963SAfshin.Ardakani@Sun.COM
488711963SAfshin.Ardakani@Sun.COM idmap_retcode
idmap_cache_flush(idmap_flush_op op)488811963SAfshin.Ardakani@Sun.COM idmap_cache_flush(idmap_flush_op op)
488911963SAfshin.Ardakani@Sun.COM {
489011963SAfshin.Ardakani@Sun.COM idmap_retcode rc;
489111963SAfshin.Ardakani@Sun.COM sqlite *cache = NULL;
489211963SAfshin.Ardakani@Sun.COM char *sql1;
489311963SAfshin.Ardakani@Sun.COM char *sql2;
489411963SAfshin.Ardakani@Sun.COM
489511963SAfshin.Ardakani@Sun.COM switch (op) {
489611963SAfshin.Ardakani@Sun.COM case IDMAP_FLUSH_EXPIRE:
489711963SAfshin.Ardakani@Sun.COM sql1 =
489811963SAfshin.Ardakani@Sun.COM "UPDATE idmap_cache SET expiration=1 WHERE expiration>0;";
489911963SAfshin.Ardakani@Sun.COM sql2 =
490011963SAfshin.Ardakani@Sun.COM "UPDATE name_cache SET expiration=1 WHERE expiration>0;";
490111963SAfshin.Ardakani@Sun.COM break;
490211963SAfshin.Ardakani@Sun.COM
490311963SAfshin.Ardakani@Sun.COM case IDMAP_FLUSH_DELETE:
490411963SAfshin.Ardakani@Sun.COM sql1 = "DELETE FROM idmap_cache;";
490511963SAfshin.Ardakani@Sun.COM sql2 = "DELETE FROM name_cache;";
490611963SAfshin.Ardakani@Sun.COM break;
490711963SAfshin.Ardakani@Sun.COM
490811963SAfshin.Ardakani@Sun.COM default:
490911963SAfshin.Ardakani@Sun.COM return (IDMAP_ERR_INTERNAL);
491011963SAfshin.Ardakani@Sun.COM }
491111963SAfshin.Ardakani@Sun.COM
491211963SAfshin.Ardakani@Sun.COM rc = get_cache_handle(&cache);
491311963SAfshin.Ardakani@Sun.COM if (rc != IDMAP_SUCCESS)
491411963SAfshin.Ardakani@Sun.COM return (rc);
491511963SAfshin.Ardakani@Sun.COM
491611963SAfshin.Ardakani@Sun.COM /*
491711963SAfshin.Ardakani@Sun.COM * Note that we flush the idmapd cache first, before the kernel
491811963SAfshin.Ardakani@Sun.COM * cache. If we did it the other way 'round, a request could come
491911963SAfshin.Ardakani@Sun.COM * in after the kernel cache flush and pull a soon-to-be-flushed
492011963SAfshin.Ardakani@Sun.COM * idmapd cache entry back into the kernel cache. This way the
492111963SAfshin.Ardakani@Sun.COM * worst that will happen is that a new entry will be added to
492211963SAfshin.Ardakani@Sun.COM * the kernel cache and then immediately flushed.
492311963SAfshin.Ardakani@Sun.COM */
492411963SAfshin.Ardakani@Sun.COM
492511963SAfshin.Ardakani@Sun.COM rc = sql_exec_no_cb(cache, IDMAP_CACHENAME, sql1);
492611963SAfshin.Ardakani@Sun.COM if (rc != IDMAP_SUCCESS)
492711963SAfshin.Ardakani@Sun.COM return (rc);
492811963SAfshin.Ardakani@Sun.COM
492911963SAfshin.Ardakani@Sun.COM rc = sql_exec_no_cb(cache, IDMAP_CACHENAME, sql2);
493011963SAfshin.Ardakani@Sun.COM
493111963SAfshin.Ardakani@Sun.COM (void) __idmap_flush_kcache();
493211963SAfshin.Ardakani@Sun.COM return (rc);
493311963SAfshin.Ardakani@Sun.COM }
4934