1c5c4113dSnw141292 /* 2c5c4113dSnw141292 * CDDL HEADER START 3c5c4113dSnw141292 * 4c5c4113dSnw141292 * The contents of this file are subject to the terms of the 5c5c4113dSnw141292 * Common Development and Distribution License (the "License"). 6c5c4113dSnw141292 * You may not use this file except in compliance with the License. 7c5c4113dSnw141292 * 8c5c4113dSnw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9c5c4113dSnw141292 * or http://www.opensolaris.org/os/licensing. 10c5c4113dSnw141292 * See the License for the specific language governing permissions 11c5c4113dSnw141292 * and limitations under the License. 12c5c4113dSnw141292 * 13c5c4113dSnw141292 * When distributing Covered Code, include this CDDL HEADER in each 14c5c4113dSnw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15c5c4113dSnw141292 * If applicable, add the following below this CDDL HEADER, with the 16c5c4113dSnw141292 * fields enclosed by brackets "[]" replaced with your own identifying 17c5c4113dSnw141292 * information: Portions Copyright [yyyy] [name of copyright owner] 18c5c4113dSnw141292 * 19c5c4113dSnw141292 * CDDL HEADER END 20c5c4113dSnw141292 */ 21c5c4113dSnw141292 /* 227a8a68f5SJulian Pullen * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23c5c4113dSnw141292 * Use is subject to license terms. 24c5c4113dSnw141292 */ 25c5c4113dSnw141292 26c5c4113dSnw141292 /* 27c5c4113dSnw141292 * Database related utility routines 28c5c4113dSnw141292 */ 29c5c4113dSnw141292 30c5c4113dSnw141292 #include <stdio.h> 31c5c4113dSnw141292 #include <stdlib.h> 32c5c4113dSnw141292 #include <string.h> 33c5c4113dSnw141292 #include <errno.h> 34c5c4113dSnw141292 #include <sys/types.h> 35c5c4113dSnw141292 #include <sys/stat.h> 36c5c4113dSnw141292 #include <rpc/rpc.h> 37c5c4113dSnw141292 #include <sys/sid.h> 38c5c4113dSnw141292 #include <time.h> 39c5c4113dSnw141292 #include <pwd.h> 40c5c4113dSnw141292 #include <grp.h> 4184decf41Sjp151216 #include <pthread.h> 4284decf41Sjp151216 #include <assert.h> 43cd37da74Snw141292 #include <sys/u8_textprep.h> 448c155366SJordan Brown #include <alloca.h> 45c5c4113dSnw141292 46c5c4113dSnw141292 #include "idmapd.h" 47c5c4113dSnw141292 #include "adutils.h" 48c5c4113dSnw141292 #include "string.h" 49c5c4113dSnw141292 #include "idmap_priv.h" 50cd37da74Snw141292 #include "schema.h" 51e8c27ec8Sbaban #include "nldaputils.h" 52*fe1c642dSBill Krier #include "miscutils.h" 53c5c4113dSnw141292 5484decf41Sjp151216 55c5c4113dSnw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *, 56c5c4113dSnw141292 sqlite_vm **, int *, int, const char ***); 57e8c27ec8Sbaban static idmap_retcode lookup_localsid2pid(idmap_mapping *, idmap_id_res *); 58e8c27ec8Sbaban static idmap_retcode lookup_cache_name2sid(sqlite *, const char *, 59e8c27ec8Sbaban const char *, char **, char **, idmap_rid_t *, int *); 60e8c27ec8Sbaban 6108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States #define NELEM(a) (sizeof (a) / sizeof ((a)[0])) 62c5c4113dSnw141292 63c5c4113dSnw141292 #define EMPTY_NAME(name) (*name == 0 || strcmp(name, "\"\"") == 0) 64c5c4113dSnw141292 65c5c4113dSnw141292 #define DO_NOT_ALLOC_NEW_ID_MAPPING(req)\ 66c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC) 67c5c4113dSnw141292 68c5c4113dSnw141292 #define AVOID_NAMESERVICE(req)\ 69c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE) 70c5c4113dSnw141292 712b4a7802SBaban Kenkre #define ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)\ 722b4a7802SBaban Kenkre (req->flag & IDMAP_REQ_FLG_WK_OR_LOCAL_SIDS_ONLY) 732b4a7802SBaban Kenkre 74e8c27ec8Sbaban #define IS_EPHEMERAL(pid) (pid > INT32_MAX && pid != SENTINEL_PID) 75c5c4113dSnw141292 76c5c4113dSnw141292 77c5c4113dSnw141292 typedef enum init_db_option { 78c5c4113dSnw141292 FAIL_IF_CORRUPT = 0, 79c5c4113dSnw141292 REMOVE_IF_CORRUPT = 1 80c5c4113dSnw141292 } init_db_option_t; 81c5c4113dSnw141292 8284decf41Sjp151216 /* 8308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Thread specific data to hold the database handles so that the 8408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * databases are not opened and closed for every request. It also 8584decf41Sjp151216 * contains the sqlite busy handler structure. 8684decf41Sjp151216 */ 8784decf41Sjp151216 8884decf41Sjp151216 struct idmap_busy { 8984decf41Sjp151216 const char *name; 9084decf41Sjp151216 const int *delays; 9184decf41Sjp151216 int delay_size; 9284decf41Sjp151216 int total; 9384decf41Sjp151216 int sec; 9484decf41Sjp151216 }; 9584decf41Sjp151216 9684decf41Sjp151216 9784decf41Sjp151216 typedef struct idmap_tsd { 9884decf41Sjp151216 sqlite *db_db; 9984decf41Sjp151216 sqlite *cache_db; 10084decf41Sjp151216 struct idmap_busy cache_busy; 10184decf41Sjp151216 struct idmap_busy db_busy; 10284decf41Sjp151216 } idmap_tsd_t; 10384decf41Sjp151216 104e3f2c991SKeyur Desai /* 105e3f2c991SKeyur Desai * Flags to indicate how local the directory we're consulting is. 106e3f2c991SKeyur Desai * If neither is set, it means the directory belongs to a remote forest. 107e3f2c991SKeyur Desai */ 108e3f2c991SKeyur Desai #define DOMAIN_IS_LOCAL 0x01 109e3f2c991SKeyur Desai #define FOREST_IS_LOCAL 0x02 11084decf41Sjp151216 11184decf41Sjp151216 static const int cache_delay_table[] = 11284decf41Sjp151216 { 1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 11384decf41Sjp151216 50, 50, 60, 70, 80, 90, 100}; 11484decf41Sjp151216 11584decf41Sjp151216 static const int db_delay_table[] = 11684decf41Sjp151216 { 5, 10, 15, 20, 30, 40, 55, 70, 100}; 11784decf41Sjp151216 11884decf41Sjp151216 11984decf41Sjp151216 static pthread_key_t idmap_tsd_key; 12084decf41Sjp151216 12184decf41Sjp151216 void 12284decf41Sjp151216 idmap_tsd_destroy(void *key) 12384decf41Sjp151216 { 12484decf41Sjp151216 12584decf41Sjp151216 idmap_tsd_t *tsd = (idmap_tsd_t *)key; 12684decf41Sjp151216 if (tsd) { 12784decf41Sjp151216 if (tsd->db_db) 12884decf41Sjp151216 (void) sqlite_close(tsd->db_db); 12984decf41Sjp151216 if (tsd->cache_db) 13084decf41Sjp151216 (void) sqlite_close(tsd->cache_db); 13184decf41Sjp151216 free(tsd); 13284decf41Sjp151216 } 13384decf41Sjp151216 } 13484decf41Sjp151216 13584decf41Sjp151216 int 136cd37da74Snw141292 idmap_init_tsd_key(void) 137cd37da74Snw141292 { 13884decf41Sjp151216 return (pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy)); 13984decf41Sjp151216 } 14084decf41Sjp151216 14184decf41Sjp151216 14284decf41Sjp151216 14384decf41Sjp151216 idmap_tsd_t * 14484decf41Sjp151216 idmap_get_tsd(void) 14584decf41Sjp151216 { 14684decf41Sjp151216 idmap_tsd_t *tsd; 14784decf41Sjp151216 14884decf41Sjp151216 if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) { 14984decf41Sjp151216 /* No thread specific data so create it */ 15084decf41Sjp151216 if ((tsd = malloc(sizeof (*tsd))) != NULL) { 15184decf41Sjp151216 /* Initialize thread specific data */ 15284decf41Sjp151216 (void) memset(tsd, 0, sizeof (*tsd)); 15384decf41Sjp151216 /* save the trhread specific data */ 15484decf41Sjp151216 if (pthread_setspecific(idmap_tsd_key, tsd) != 0) { 15584decf41Sjp151216 /* Can't store key */ 15684decf41Sjp151216 free(tsd); 15784decf41Sjp151216 tsd = NULL; 15884decf41Sjp151216 } 15984decf41Sjp151216 } else { 16084decf41Sjp151216 tsd = NULL; 16184decf41Sjp151216 } 16284decf41Sjp151216 } 16384decf41Sjp151216 16484decf41Sjp151216 return (tsd); 16584decf41Sjp151216 } 16684decf41Sjp151216 167cd37da74Snw141292 /* 168cd37da74Snw141292 * A simple wrapper around u8_textprep_str() that returns the Unicode 169cd37da74Snw141292 * lower-case version of some string. The result must be freed. 170cd37da74Snw141292 */ 171cd37da74Snw141292 char * 172cd37da74Snw141292 tolower_u8(const char *s) 173cd37da74Snw141292 { 174cd37da74Snw141292 char *res = NULL; 175cd37da74Snw141292 char *outs; 176cd37da74Snw141292 size_t inlen, outlen, inbytesleft, outbytesleft; 177cd37da74Snw141292 int rc, err; 178cd37da74Snw141292 179cd37da74Snw141292 /* 180cd37da74Snw141292 * u8_textprep_str() does not allocate memory. The input and 181cd37da74Snw141292 * output buffers may differ in size (though that would be more 182cd37da74Snw141292 * likely when normalization is done). We have to loop over it... 183cd37da74Snw141292 * 184cd37da74Snw141292 * To improve the chances that we can avoid looping we add 10 185cd37da74Snw141292 * bytes of output buffer room the first go around. 186cd37da74Snw141292 */ 187cd37da74Snw141292 inlen = inbytesleft = strlen(s); 188cd37da74Snw141292 outlen = outbytesleft = inlen + 10; 189cd37da74Snw141292 if ((res = malloc(outlen)) == NULL) 190cd37da74Snw141292 return (NULL); 191cd37da74Snw141292 outs = res; 192cd37da74Snw141292 193cd37da74Snw141292 while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs, 194cd37da74Snw141292 &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 && 195cd37da74Snw141292 err == E2BIG) { 196cd37da74Snw141292 if ((res = realloc(res, outlen + inbytesleft)) == NULL) 197cd37da74Snw141292 return (NULL); 198cd37da74Snw141292 /* adjust input/output buffer pointers */ 199cd37da74Snw141292 s += (inlen - inbytesleft); 200cd37da74Snw141292 outs = res + outlen - outbytesleft; 201cd37da74Snw141292 /* adjust outbytesleft and outlen */ 202cd37da74Snw141292 outlen += inbytesleft; 203cd37da74Snw141292 outbytesleft += inbytesleft; 204cd37da74Snw141292 } 205cd37da74Snw141292 206cd37da74Snw141292 if (rc < 0) { 207cd37da74Snw141292 free(res); 208cd37da74Snw141292 res = NULL; 209cd37da74Snw141292 return (NULL); 210cd37da74Snw141292 } 211cd37da74Snw141292 212cd37da74Snw141292 res[outlen - outbytesleft] = '\0'; 213cd37da74Snw141292 214cd37da74Snw141292 return (res); 215cd37da74Snw141292 } 216cd37da74Snw141292 217cd37da74Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 218cd37da74Snw141292 const char *while_doing); 219cd37da74Snw141292 220c5c4113dSnw141292 221c5c4113dSnw141292 /* 222c5c4113dSnw141292 * Initialize 'dbname' using 'sql' 223c5c4113dSnw141292 */ 224cd37da74Snw141292 static 225cd37da74Snw141292 int 226cd37da74Snw141292 init_db_instance(const char *dbname, int version, 227cd37da74Snw141292 const char *detect_version_sql, char * const *sql, 228cd37da74Snw141292 init_db_option_t opt, int *created, int *upgraded) 229c5c4113dSnw141292 { 230cd37da74Snw141292 int rc, curr_version; 231cd37da74Snw141292 int tries = 1; 232cd37da74Snw141292 int prio = LOG_NOTICE; 233c5c4113dSnw141292 sqlite *db = NULL; 234cd37da74Snw141292 char *errmsg = NULL; 235c5c4113dSnw141292 236cd37da74Snw141292 *created = 0; 237cd37da74Snw141292 *upgraded = 0; 238c5c4113dSnw141292 239cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 240cd37da74Snw141292 tries = 3; 241cd37da74Snw141292 242cd37da74Snw141292 rinse_repeat: 243cd37da74Snw141292 if (tries == 0) { 244cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname); 245c5c4113dSnw141292 return (-1); 246cd37da74Snw141292 } 247cd37da74Snw141292 if (tries-- == 1) 248cd37da74Snw141292 /* Last try, log errors */ 249cd37da74Snw141292 prio = LOG_ERR; 250c5c4113dSnw141292 251cd37da74Snw141292 db = sqlite_open(dbname, 0600, &errmsg); 252cd37da74Snw141292 if (db == NULL) { 253cd37da74Snw141292 idmapdlog(prio, "Error creating database %s (%s)", 254cd37da74Snw141292 dbname, CHECK_NULL(errmsg)); 255cd37da74Snw141292 sqlite_freemem(errmsg); 256cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 257c5c4113dSnw141292 (void) unlink(dbname); 258cd37da74Snw141292 goto rinse_repeat; 259c5c4113dSnw141292 } 260c5c4113dSnw141292 261c5c4113dSnw141292 sqlite_busy_timeout(db, 3000); 262cd37da74Snw141292 263cd37da74Snw141292 /* Detect current version of schema in the db, if any */ 264cd37da74Snw141292 curr_version = 0; 265cd37da74Snw141292 if (detect_version_sql != NULL) { 266cd37da74Snw141292 char *end, **results; 267cd37da74Snw141292 int nrow; 268cd37da74Snw141292 269cd37da74Snw141292 #ifdef IDMAPD_DEBUG 270cd37da74Snw141292 (void) fprintf(stderr, "Schema version detection SQL: %s\n", 271cd37da74Snw141292 detect_version_sql); 272cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 273cd37da74Snw141292 rc = sqlite_get_table(db, detect_version_sql, &results, 274cd37da74Snw141292 &nrow, NULL, &errmsg); 275cd37da74Snw141292 if (rc != SQLITE_OK) { 276cd37da74Snw141292 idmapdlog(prio, 277cd37da74Snw141292 "Error detecting schema version of db %s (%s)", 278cd37da74Snw141292 dbname, errmsg); 279cd37da74Snw141292 sqlite_freemem(errmsg); 280cd37da74Snw141292 sqlite_free_table(results); 281c5c4113dSnw141292 sqlite_close(db); 282cd37da74Snw141292 return (-1); 283cd37da74Snw141292 } 284cd37da74Snw141292 if (nrow != 1) { 285cd37da74Snw141292 idmapdlog(prio, 286cd37da74Snw141292 "Error detecting schema version of db %s", dbname); 287cd37da74Snw141292 sqlite_close(db); 288cd37da74Snw141292 sqlite_free_table(results); 289cd37da74Snw141292 return (-1); 290cd37da74Snw141292 } 291cd37da74Snw141292 curr_version = strtol(results[1], &end, 10); 292cd37da74Snw141292 sqlite_free_table(results); 293c5c4113dSnw141292 } 294c5c4113dSnw141292 295cd37da74Snw141292 if (curr_version < 0) { 296cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 297cd37da74Snw141292 (void) unlink(dbname); 298cd37da74Snw141292 goto rinse_repeat; 299c5c4113dSnw141292 } 300c5c4113dSnw141292 301cd37da74Snw141292 if (curr_version == version) 302cd37da74Snw141292 goto done; 303cd37da74Snw141292 304cd37da74Snw141292 /* Install or upgrade schema */ 305cd37da74Snw141292 #ifdef IDMAPD_DEBUG 306cd37da74Snw141292 (void) fprintf(stderr, "Schema init/upgrade SQL: %s\n", 307cd37da74Snw141292 sql[curr_version]); 308cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 309cd37da74Snw141292 rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname, 310cd37da74Snw141292 (curr_version == 0) ? "installing schema" : "upgrading schema"); 311cd37da74Snw141292 if (rc != 0) { 312cd37da74Snw141292 idmapdlog(prio, "Error %s schema for db %s", dbname, 313cd37da74Snw141292 (curr_version == 0) ? "installing schema" : 314cd37da74Snw141292 "upgrading schema"); 315cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 316cd37da74Snw141292 (void) unlink(dbname); 317cd37da74Snw141292 goto rinse_repeat; 318c5c4113dSnw141292 } 319c5c4113dSnw141292 320cd37da74Snw141292 *upgraded = (curr_version > 0); 321cd37da74Snw141292 *created = (curr_version == 0); 322cd37da74Snw141292 323cd37da74Snw141292 done: 324c5c4113dSnw141292 (void) sqlite_close(db); 325cd37da74Snw141292 return (0); 326c5c4113dSnw141292 } 327c5c4113dSnw141292 32884decf41Sjp151216 32984decf41Sjp151216 /* 33084decf41Sjp151216 * This is the SQLite database busy handler that retries the SQL 33184decf41Sjp151216 * operation until it is successful. 33284decf41Sjp151216 */ 33384decf41Sjp151216 int 33484decf41Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */ 33584decf41Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count) 33684decf41Sjp151216 { 33784decf41Sjp151216 struct idmap_busy *busy = arg; 33884decf41Sjp151216 int delay; 33984decf41Sjp151216 struct timespec rqtp; 34084decf41Sjp151216 34184decf41Sjp151216 if (count == 1) { 34284decf41Sjp151216 busy->total = 0; 34384decf41Sjp151216 busy->sec = 2; 34484decf41Sjp151216 } 34584decf41Sjp151216 if (busy->total > 1000 * busy->sec) { 3462b3ecdebSjp151216 idmapdlog(LOG_DEBUG, 34784decf41Sjp151216 "Thread %d waited %d sec for the %s database", 34884decf41Sjp151216 pthread_self(), busy->sec, busy->name); 34984decf41Sjp151216 busy->sec++; 35084decf41Sjp151216 } 35184decf41Sjp151216 35284decf41Sjp151216 if (count <= busy->delay_size) { 35384decf41Sjp151216 delay = busy->delays[count-1]; 35484decf41Sjp151216 } else { 35584decf41Sjp151216 delay = busy->delays[busy->delay_size - 1]; 35684decf41Sjp151216 } 35784decf41Sjp151216 busy->total += delay; 35884decf41Sjp151216 rqtp.tv_sec = 0; 35984decf41Sjp151216 rqtp.tv_nsec = delay * (NANOSEC / MILLISEC); 36084decf41Sjp151216 (void) nanosleep(&rqtp, NULL); 36184decf41Sjp151216 return (1); 36284decf41Sjp151216 } 36384decf41Sjp151216 36484decf41Sjp151216 365c5c4113dSnw141292 /* 366c5c4113dSnw141292 * Get the database handle 367c5c4113dSnw141292 */ 368c5c4113dSnw141292 idmap_retcode 369cd37da74Snw141292 get_db_handle(sqlite **db) 370cd37da74Snw141292 { 371c5c4113dSnw141292 char *errmsg; 37284decf41Sjp151216 idmap_tsd_t *tsd; 373c5c4113dSnw141292 374c5c4113dSnw141292 /* 37584decf41Sjp151216 * Retrieve the db handle from thread-specific storage 376c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 377c5c4113dSnw141292 */ 37884decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 37984decf41Sjp151216 idmapdlog(LOG_ERR, 380cd37da74Snw141292 "Error getting thread specific data for %s", IDMAP_DBNAME); 38184decf41Sjp151216 return (IDMAP_ERR_MEMORY); 38284decf41Sjp151216 } 383c5c4113dSnw141292 38484decf41Sjp151216 if (tsd->db_db == NULL) { 38584decf41Sjp151216 tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg); 38684decf41Sjp151216 if (tsd->db_db == NULL) { 387cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 388c5c4113dSnw141292 IDMAP_DBNAME, CHECK_NULL(errmsg)); 389c5c4113dSnw141292 sqlite_freemem(errmsg); 390cd37da74Snw141292 return (IDMAP_ERR_DB); 391c5c4113dSnw141292 } 392cd37da74Snw141292 39384decf41Sjp151216 tsd->db_busy.name = IDMAP_DBNAME; 39484decf41Sjp151216 tsd->db_busy.delays = db_delay_table; 39584decf41Sjp151216 tsd->db_busy.delay_size = sizeof (db_delay_table) / 39684decf41Sjp151216 sizeof (int); 39784decf41Sjp151216 sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler, 39884decf41Sjp151216 &tsd->db_busy); 39984decf41Sjp151216 } 40084decf41Sjp151216 *db = tsd->db_db; 401c5c4113dSnw141292 return (IDMAP_SUCCESS); 402c5c4113dSnw141292 } 403c5c4113dSnw141292 404c5c4113dSnw141292 /* 405c5c4113dSnw141292 * Get the cache handle 406c5c4113dSnw141292 */ 407c5c4113dSnw141292 idmap_retcode 408cd37da74Snw141292 get_cache_handle(sqlite **cache) 409cd37da74Snw141292 { 410c5c4113dSnw141292 char *errmsg; 41184decf41Sjp151216 idmap_tsd_t *tsd; 412c5c4113dSnw141292 413c5c4113dSnw141292 /* 41484decf41Sjp151216 * Retrieve the db handle from thread-specific storage 415c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 416c5c4113dSnw141292 */ 41784decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 418cd37da74Snw141292 idmapdlog(LOG_ERR, "Error getting thread specific data for %s", 41984decf41Sjp151216 IDMAP_DBNAME); 42084decf41Sjp151216 return (IDMAP_ERR_MEMORY); 42184decf41Sjp151216 } 422c5c4113dSnw141292 42384decf41Sjp151216 if (tsd->cache_db == NULL) { 42484decf41Sjp151216 tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg); 42584decf41Sjp151216 if (tsd->cache_db == NULL) { 426cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 427c5c4113dSnw141292 IDMAP_CACHENAME, CHECK_NULL(errmsg)); 428c5c4113dSnw141292 sqlite_freemem(errmsg); 429cd37da74Snw141292 return (IDMAP_ERR_DB); 430c5c4113dSnw141292 } 431cd37da74Snw141292 43284decf41Sjp151216 tsd->cache_busy.name = IDMAP_CACHENAME; 43384decf41Sjp151216 tsd->cache_busy.delays = cache_delay_table; 43484decf41Sjp151216 tsd->cache_busy.delay_size = sizeof (cache_delay_table) / 43584decf41Sjp151216 sizeof (int); 43684decf41Sjp151216 sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler, 43784decf41Sjp151216 &tsd->cache_busy); 43884decf41Sjp151216 } 43984decf41Sjp151216 *cache = tsd->cache_db; 440c5c4113dSnw141292 return (IDMAP_SUCCESS); 441c5c4113dSnw141292 } 442c5c4113dSnw141292 443c5c4113dSnw141292 /* 444c5c4113dSnw141292 * Initialize cache and db 445c5c4113dSnw141292 */ 446c5c4113dSnw141292 int 447cd37da74Snw141292 init_dbs() 448cd37da74Snw141292 { 44948258c6bSjp151216 char *sql[4]; 450cd37da74Snw141292 int created, upgraded; 451cd37da74Snw141292 452c5c4113dSnw141292 /* name-based mappings; probably OK to blow away in a pinch(?) */ 453cd37da74Snw141292 sql[0] = DB_INSTALL_SQL; 454cd37da74Snw141292 sql[1] = DB_UPGRADE_FROM_v1_SQL; 45548258c6bSjp151216 sql[2] = NULL; 456cd37da74Snw141292 457cd37da74Snw141292 if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql, 458cd37da74Snw141292 FAIL_IF_CORRUPT, &created, &upgraded) < 0) 459c5c4113dSnw141292 return (-1); 460c5c4113dSnw141292 461c5c4113dSnw141292 /* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */ 462cd37da74Snw141292 sql[0] = CACHE_INSTALL_SQL; 463cd37da74Snw141292 sql[1] = CACHE_UPGRADE_FROM_v1_SQL; 46448258c6bSjp151216 sql[2] = CACHE_UPGRADE_FROM_v2_SQL; 46548258c6bSjp151216 sql[3] = NULL; 46648258c6bSjp151216 467cd37da74Snw141292 if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL, 468cd37da74Snw141292 sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0) 469c5c4113dSnw141292 return (-1); 470c5c4113dSnw141292 471cd37da74Snw141292 _idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0; 472cd37da74Snw141292 473c5c4113dSnw141292 return (0); 474c5c4113dSnw141292 } 475c5c4113dSnw141292 476c5c4113dSnw141292 /* 477c5c4113dSnw141292 * Finalize databases 478c5c4113dSnw141292 */ 479c5c4113dSnw141292 void 480cd37da74Snw141292 fini_dbs() 481cd37da74Snw141292 { 482c5c4113dSnw141292 } 483c5c4113dSnw141292 484c5c4113dSnw141292 /* 485e8c27ec8Sbaban * This table is a listing of status codes that will be returned to the 486c5c4113dSnw141292 * client when a SQL command fails with the corresponding error message. 487c5c4113dSnw141292 */ 488c5c4113dSnw141292 static msg_table_t sqlmsgtable[] = { 48962c60062Sbaban {IDMAP_ERR_U2W_NAMERULE_CONFLICT, 490c5c4113dSnw141292 "columns unixname, is_user, u2w_order are not unique"}, 49162c60062Sbaban {IDMAP_ERR_W2U_NAMERULE_CONFLICT, 492cd37da74Snw141292 "columns winname, windomain, is_user, is_wuser, w2u_order are not" 493cd37da74Snw141292 " unique"}, 494cd37da74Snw141292 {IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"}, 495c5c4113dSnw141292 {-1, NULL} 496c5c4113dSnw141292 }; 497c5c4113dSnw141292 498c5c4113dSnw141292 /* 499c5c4113dSnw141292 * idmapd's version of string2stat to map SQLite messages to 500c5c4113dSnw141292 * status codes 501c5c4113dSnw141292 */ 502c5c4113dSnw141292 idmap_retcode 503cd37da74Snw141292 idmapd_string2stat(const char *msg) 504cd37da74Snw141292 { 505c5c4113dSnw141292 int i; 506c5c4113dSnw141292 for (i = 0; sqlmsgtable[i].msg; i++) { 507c5c4113dSnw141292 if (strcasecmp(sqlmsgtable[i].msg, msg) == 0) 508c5c4113dSnw141292 return (sqlmsgtable[i].retcode); 509c5c4113dSnw141292 } 510c5c4113dSnw141292 return (IDMAP_ERR_OTHER); 511c5c4113dSnw141292 } 512c5c4113dSnw141292 513c5c4113dSnw141292 /* 514cd37da74Snw141292 * Executes some SQL in a transaction. 515cd37da74Snw141292 * 516cd37da74Snw141292 * Returns 0 on success, -1 if it failed but the rollback succeeded, -2 517cd37da74Snw141292 * if the rollback failed. 518cd37da74Snw141292 */ 519cd37da74Snw141292 static 520cd37da74Snw141292 int 521cd37da74Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 522cd37da74Snw141292 const char *while_doing) 523cd37da74Snw141292 { 524cd37da74Snw141292 char *errmsg = NULL; 525cd37da74Snw141292 int rc; 526cd37da74Snw141292 527cd37da74Snw141292 rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg); 528cd37da74Snw141292 if (rc != SQLITE_OK) { 529cd37da74Snw141292 idmapdlog(LOG_ERR, "Begin transaction failed (%s) " 530cd37da74Snw141292 "while %s (%s)", errmsg, while_doing, dbname); 531cd37da74Snw141292 sqlite_freemem(errmsg); 532cd37da74Snw141292 return (-1); 533cd37da74Snw141292 } 534cd37da74Snw141292 535cd37da74Snw141292 rc = sqlite_exec(db, sql, NULL, NULL, &errmsg); 536cd37da74Snw141292 if (rc != SQLITE_OK) { 537cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg, 538cd37da74Snw141292 while_doing, dbname); 539cd37da74Snw141292 sqlite_freemem(errmsg); 540cd37da74Snw141292 errmsg = NULL; 541cd37da74Snw141292 goto rollback; 542cd37da74Snw141292 } 543cd37da74Snw141292 544cd37da74Snw141292 rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg); 545cd37da74Snw141292 if (rc == SQLITE_OK) { 546cd37da74Snw141292 sqlite_freemem(errmsg); 547cd37da74Snw141292 return (0); 548cd37da74Snw141292 } 549cd37da74Snw141292 550cd37da74Snw141292 idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)", 551cd37da74Snw141292 errmsg, while_doing, dbname); 552cd37da74Snw141292 sqlite_freemem(errmsg); 553cd37da74Snw141292 errmsg = NULL; 554cd37da74Snw141292 555cd37da74Snw141292 rollback: 556cd37da74Snw141292 rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg); 557cd37da74Snw141292 if (rc != SQLITE_OK) { 558cd37da74Snw141292 idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)", 559cd37da74Snw141292 errmsg, while_doing, dbname); 560cd37da74Snw141292 sqlite_freemem(errmsg); 561cd37da74Snw141292 return (-2); 562cd37da74Snw141292 } 563cd37da74Snw141292 sqlite_freemem(errmsg); 564cd37da74Snw141292 565cd37da74Snw141292 return (-1); 566cd37da74Snw141292 } 567cd37da74Snw141292 568cd37da74Snw141292 /* 569c5c4113dSnw141292 * Execute the given SQL statment without using any callbacks 570c5c4113dSnw141292 */ 571c5c4113dSnw141292 idmap_retcode 57271590c90Snw141292 sql_exec_no_cb(sqlite *db, const char *dbname, char *sql) 573cd37da74Snw141292 { 574c5c4113dSnw141292 char *errmsg = NULL; 57584decf41Sjp151216 int r; 576c5c4113dSnw141292 idmap_retcode retcode; 577c5c4113dSnw141292 578c5c4113dSnw141292 r = sqlite_exec(db, sql, NULL, NULL, &errmsg); 57984decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 580c5c4113dSnw141292 581c5c4113dSnw141292 if (r != SQLITE_OK) { 58271590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing %s " 58371590c90Snw141292 "(%s)", dbname, sql, CHECK_NULL(errmsg)); 584c5c4113dSnw141292 retcode = idmapd_string2stat(errmsg); 58562c60062Sbaban if (errmsg != NULL) 586c5c4113dSnw141292 sqlite_freemem(errmsg); 587c5c4113dSnw141292 return (retcode); 588c5c4113dSnw141292 } 589c5c4113dSnw141292 590c5c4113dSnw141292 return (IDMAP_SUCCESS); 591c5c4113dSnw141292 } 592c5c4113dSnw141292 593c5c4113dSnw141292 /* 594c5c4113dSnw141292 * Generate expression that can be used in WHERE statements. 595c5c4113dSnw141292 * Examples: 596c5c4113dSnw141292 * <prefix> <col> <op> <value> <suffix> 597c5c4113dSnw141292 * "" "unixuser" "=" "foo" "AND" 598c5c4113dSnw141292 */ 599c5c4113dSnw141292 idmap_retcode 600cd37da74Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out) 601cd37da74Snw141292 { 602cd37da74Snw141292 char *s_windomain = NULL, *s_winname = NULL; 603cd37da74Snw141292 char *s_unixname = NULL; 604bbf6f00cSJordan Brown char *dir; 605cd37da74Snw141292 char *lower_winname; 606cd37da74Snw141292 int retcode = IDMAP_SUCCESS; 607cd37da74Snw141292 608c5c4113dSnw141292 if (out == NULL) 609c5c4113dSnw141292 return (IDMAP_ERR_ARG); 610c5c4113dSnw141292 611c5c4113dSnw141292 612cd37da74Snw141292 if (!EMPTY_STRING(rule->windomain)) { 613cd37da74Snw141292 s_windomain = sqlite_mprintf("AND windomain = %Q ", 614cd37da74Snw141292 rule->windomain); 615cd37da74Snw141292 if (s_windomain == NULL) { 616cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 617cd37da74Snw141292 goto out; 618c5c4113dSnw141292 } 619cd37da74Snw141292 } 620cd37da74Snw141292 621cd37da74Snw141292 if (!EMPTY_STRING(rule->winname)) { 622cd37da74Snw141292 if ((lower_winname = tolower_u8(rule->winname)) == NULL) 623cd37da74Snw141292 lower_winname = rule->winname; 624cd37da74Snw141292 s_winname = sqlite_mprintf( 625cd37da74Snw141292 "AND winname = %Q AND is_wuser = %d ", 626cd37da74Snw141292 lower_winname, rule->is_wuser ? 1 : 0); 627cd37da74Snw141292 if (lower_winname != rule->winname) 628cd37da74Snw141292 free(lower_winname); 629cd37da74Snw141292 if (s_winname == NULL) { 630cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 631cd37da74Snw141292 goto out; 632cd37da74Snw141292 } 633cd37da74Snw141292 } 634cd37da74Snw141292 635cd37da74Snw141292 if (!EMPTY_STRING(rule->unixname)) { 636cd37da74Snw141292 s_unixname = sqlite_mprintf( 637cd37da74Snw141292 "AND unixname = %Q AND is_user = %d ", 638cd37da74Snw141292 rule->unixname, rule->is_user ? 1 : 0); 639cd37da74Snw141292 if (s_unixname == NULL) { 640cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 641cd37da74Snw141292 goto out; 642cd37da74Snw141292 } 643cd37da74Snw141292 } 644cd37da74Snw141292 645bbf6f00cSJordan Brown switch (rule->direction) { 646bbf6f00cSJordan Brown case IDMAP_DIRECTION_BI: 647bbf6f00cSJordan Brown dir = "AND w2u_order > 0 AND u2w_order > 0"; 648bbf6f00cSJordan Brown break; 649bbf6f00cSJordan Brown case IDMAP_DIRECTION_W2U: 650bbf6f00cSJordan Brown dir = "AND w2u_order > 0" 651bbf6f00cSJordan Brown " AND (u2w_order = 0 OR u2w_order ISNULL)"; 652bbf6f00cSJordan Brown break; 653bbf6f00cSJordan Brown case IDMAP_DIRECTION_U2W: 654bbf6f00cSJordan Brown dir = "AND u2w_order > 0" 655bbf6f00cSJordan Brown " AND (w2u_order = 0 OR w2u_order ISNULL)"; 656bbf6f00cSJordan Brown break; 657bbf6f00cSJordan Brown default: 658bbf6f00cSJordan Brown dir = ""; 659bbf6f00cSJordan Brown break; 660bbf6f00cSJordan Brown } 661bbf6f00cSJordan Brown 662bbf6f00cSJordan Brown *out = sqlite_mprintf("%s %s %s %s", 663cd37da74Snw141292 s_windomain ? s_windomain : "", 664cd37da74Snw141292 s_winname ? s_winname : "", 665bbf6f00cSJordan Brown s_unixname ? s_unixname : "", 666bbf6f00cSJordan Brown dir); 667cd37da74Snw141292 668cd37da74Snw141292 if (*out == NULL) { 669cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 670cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 671cd37da74Snw141292 goto out; 672cd37da74Snw141292 } 673cd37da74Snw141292 674cd37da74Snw141292 out: 675cd37da74Snw141292 if (s_windomain != NULL) 676cd37da74Snw141292 sqlite_freemem(s_windomain); 677cd37da74Snw141292 if (s_winname != NULL) 678cd37da74Snw141292 sqlite_freemem(s_winname); 679cd37da74Snw141292 if (s_unixname != NULL) 680cd37da74Snw141292 sqlite_freemem(s_unixname); 681cd37da74Snw141292 682cd37da74Snw141292 return (retcode); 683cd37da74Snw141292 } 684cd37da74Snw141292 685cd37da74Snw141292 686c5c4113dSnw141292 687c5c4113dSnw141292 /* 688c5c4113dSnw141292 * Generate and execute SQL statement for LIST RPC calls 689c5c4113dSnw141292 */ 690c5c4113dSnw141292 idmap_retcode 69171590c90Snw141292 process_list_svc_sql(sqlite *db, const char *dbname, char *sql, uint64_t limit, 69248258c6bSjp151216 int flag, list_svc_cb cb, void *result) 693cd37da74Snw141292 { 694c5c4113dSnw141292 list_cb_data_t cb_data; 695c5c4113dSnw141292 char *errmsg = NULL; 69684decf41Sjp151216 int r; 697c5c4113dSnw141292 idmap_retcode retcode = IDMAP_ERR_INTERNAL; 698c5c4113dSnw141292 699c5c4113dSnw141292 (void) memset(&cb_data, 0, sizeof (cb_data)); 700c5c4113dSnw141292 cb_data.result = result; 701c5c4113dSnw141292 cb_data.limit = limit; 70248258c6bSjp151216 cb_data.flag = flag; 703c5c4113dSnw141292 70484decf41Sjp151216 705c5c4113dSnw141292 r = sqlite_exec(db, sql, cb, &cb_data, &errmsg); 70684decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 707c5c4113dSnw141292 switch (r) { 708c5c4113dSnw141292 case SQLITE_OK: 709c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 71084decf41Sjp151216 break; 71184decf41Sjp151216 712c5c4113dSnw141292 default: 713c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 71471590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing " 71571590c90Snw141292 "%s (%s)", dbname, sql, CHECK_NULL(errmsg)); 71684decf41Sjp151216 break; 717c5c4113dSnw141292 } 71862c60062Sbaban if (errmsg != NULL) 719c5c4113dSnw141292 sqlite_freemem(errmsg); 720c5c4113dSnw141292 return (retcode); 721c5c4113dSnw141292 } 722c5c4113dSnw141292 723c5c4113dSnw141292 /* 724c5c4113dSnw141292 * This routine is called by callbacks that process the results of 725c5c4113dSnw141292 * LIST RPC calls to validate data and to allocate memory for 726c5c4113dSnw141292 * the result array. 727c5c4113dSnw141292 */ 728c5c4113dSnw141292 idmap_retcode 729c5c4113dSnw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv, 730cd37da74Snw141292 int ncol, uchar_t **list, size_t valsize) 731cd37da74Snw141292 { 732c5c4113dSnw141292 size_t nsize; 733c5c4113dSnw141292 void *tmplist; 734c5c4113dSnw141292 735c5c4113dSnw141292 if (cb_data->limit > 0 && cb_data->next == cb_data->limit) 736c5c4113dSnw141292 return (IDMAP_NEXT); 737c5c4113dSnw141292 738c5c4113dSnw141292 if (argc < ncol || argv == NULL) { 739c5c4113dSnw141292 idmapdlog(LOG_ERR, "Invalid data"); 740c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 741c5c4113dSnw141292 } 742c5c4113dSnw141292 743c5c4113dSnw141292 /* alloc in bulk to reduce number of reallocs */ 744c5c4113dSnw141292 if (cb_data->next >= cb_data->len) { 745c5c4113dSnw141292 nsize = (cb_data->len + SIZE_INCR) * valsize; 746c5c4113dSnw141292 tmplist = realloc(*list, nsize); 747c5c4113dSnw141292 if (tmplist == NULL) { 748c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 749c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 750c5c4113dSnw141292 } 751c5c4113dSnw141292 *list = tmplist; 752c5c4113dSnw141292 (void) memset(*list + (cb_data->len * valsize), 0, 753c5c4113dSnw141292 SIZE_INCR * valsize); 754c5c4113dSnw141292 cb_data->len += SIZE_INCR; 755c5c4113dSnw141292 } 756c5c4113dSnw141292 return (IDMAP_SUCCESS); 757c5c4113dSnw141292 } 758c5c4113dSnw141292 759cd37da74Snw141292 static 760cd37da74Snw141292 idmap_retcode 761c5c4113dSnw141292 get_namerule_order(char *winname, char *windomain, char *unixname, 762cd37da74Snw141292 int direction, int is_diagonal, int *w2u_order, int *u2w_order) 763cd37da74Snw141292 { 764c5c4113dSnw141292 *w2u_order = 0; 765c5c4113dSnw141292 *u2w_order = 0; 766c5c4113dSnw141292 767c5c4113dSnw141292 /* 768c5c4113dSnw141292 * Windows to UNIX lookup order: 769c5c4113dSnw141292 * 1. winname@domain (or winname) to "" 770c5c4113dSnw141292 * 2. winname@domain (or winname) to unixname 771c5c4113dSnw141292 * 3. winname@* to "" 772c5c4113dSnw141292 * 4. winname@* to unixname 773c5c4113dSnw141292 * 5. *@domain (or *) to * 774c5c4113dSnw141292 * 6. *@domain (or *) to "" 775c5c4113dSnw141292 * 7. *@domain (or *) to unixname 776c5c4113dSnw141292 * 8. *@* to * 777c5c4113dSnw141292 * 9. *@* to "" 778c5c4113dSnw141292 * 10. *@* to unixname 779c5c4113dSnw141292 * 780c5c4113dSnw141292 * winname is a special case of winname@domain when domain is the 781c5c4113dSnw141292 * default domain. Similarly * is a special case of *@domain when 782c5c4113dSnw141292 * domain is the default domain. 783c5c4113dSnw141292 * 784c5c4113dSnw141292 * Note that "" has priority over specific names because "" inhibits 785c5c4113dSnw141292 * mappings and traditionally deny rules always had higher priority. 786c5c4113dSnw141292 */ 787651c0131Sbaban if (direction != IDMAP_DIRECTION_U2W) { 788651c0131Sbaban /* bi-directional or from windows to unix */ 789c5c4113dSnw141292 if (winname == NULL) 790c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 791c5c4113dSnw141292 else if (unixname == NULL) 792c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 793c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 794c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 795c5c4113dSnw141292 else if (*winname == '*' && windomain && *windomain == '*') { 796c5c4113dSnw141292 if (*unixname == '*') 797c5c4113dSnw141292 *w2u_order = 8; 798c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 799c5c4113dSnw141292 *w2u_order = 9; 800c5c4113dSnw141292 else /* unixname == name */ 801c5c4113dSnw141292 *w2u_order = 10; 802c5c4113dSnw141292 } else if (*winname == '*') { 803c5c4113dSnw141292 if (*unixname == '*') 804c5c4113dSnw141292 *w2u_order = 5; 805c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 806c5c4113dSnw141292 *w2u_order = 6; 807c5c4113dSnw141292 else /* name */ 808c5c4113dSnw141292 *w2u_order = 7; 80962c60062Sbaban } else if (windomain != NULL && *windomain == '*') { 810c5c4113dSnw141292 /* winname == name */ 811c5c4113dSnw141292 if (*unixname == '*') 812c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 813c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 814c5c4113dSnw141292 *w2u_order = 3; 815c5c4113dSnw141292 else /* name */ 816c5c4113dSnw141292 *w2u_order = 4; 817c5c4113dSnw141292 } else { 818c5c4113dSnw141292 /* winname == name && windomain == null or name */ 819c5c4113dSnw141292 if (*unixname == '*') 820c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 821c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 822c5c4113dSnw141292 *w2u_order = 1; 823c5c4113dSnw141292 else /* name */ 824c5c4113dSnw141292 *w2u_order = 2; 825c5c4113dSnw141292 } 826cd37da74Snw141292 827c5c4113dSnw141292 } 828c5c4113dSnw141292 829c5c4113dSnw141292 /* 830cd37da74Snw141292 * 1. unixname to "", non-diagonal 831cd37da74Snw141292 * 2. unixname to winname@domain (or winname), non-diagonal 832cd37da74Snw141292 * 3. unixname to "", diagonal 833cd37da74Snw141292 * 4. unixname to winname@domain (or winname), diagonal 834cd37da74Snw141292 * 5. * to *@domain (or *), non-diagonal 835cd37da74Snw141292 * 5. * to *@domain (or *), diagonal 836cd37da74Snw141292 * 7. * to "" 837cd37da74Snw141292 * 8. * to winname@domain (or winname) 838cd37da74Snw141292 * 9. * to "", non-diagonal 839cd37da74Snw141292 * 10. * to winname@domain (or winname), diagonal 840c5c4113dSnw141292 */ 841651c0131Sbaban if (direction != IDMAP_DIRECTION_W2U) { 842cd37da74Snw141292 int diagonal = is_diagonal ? 1 : 0; 843cd37da74Snw141292 844651c0131Sbaban /* bi-directional or from unix to windows */ 845c5c4113dSnw141292 if (unixname == NULL || EMPTY_NAME(unixname)) 846c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 847c5c4113dSnw141292 else if (winname == NULL) 848c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 84962c60062Sbaban else if (windomain != NULL && *windomain == '*') 850651c0131Sbaban return (IDMAP_ERR_U2W_NAMERULE); 851c5c4113dSnw141292 else if (*unixname == '*') { 852c5c4113dSnw141292 if (*winname == '*') 853cd37da74Snw141292 *u2w_order = 5 + diagonal; 854c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 855cd37da74Snw141292 *u2w_order = 7 + 2 * diagonal; 856c5c4113dSnw141292 else 857cd37da74Snw141292 *u2w_order = 8 + 2 * diagonal; 858c5c4113dSnw141292 } else { 859c5c4113dSnw141292 if (*winname == '*') 860c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 861c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 862cd37da74Snw141292 *u2w_order = 1 + 2 * diagonal; 863c5c4113dSnw141292 else 864cd37da74Snw141292 *u2w_order = 2 + 2 * diagonal; 865c5c4113dSnw141292 } 866c5c4113dSnw141292 } 867c5c4113dSnw141292 return (IDMAP_SUCCESS); 868c5c4113dSnw141292 } 869c5c4113dSnw141292 870c5c4113dSnw141292 /* 871c5c4113dSnw141292 * Generate and execute SQL statement to add name-based mapping rule 872c5c4113dSnw141292 */ 873c5c4113dSnw141292 idmap_retcode 874cd37da74Snw141292 add_namerule(sqlite *db, idmap_namerule *rule) 875cd37da74Snw141292 { 876c5c4113dSnw141292 char *sql = NULL; 877c5c4113dSnw141292 idmap_stat retcode; 8788e228215Sdm199847 char *dom = NULL; 87908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char *name; 880c5c4113dSnw141292 int w2u_order, u2w_order; 881c5c4113dSnw141292 char w2ubuf[11], u2wbuf[11]; 88208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char *canonname = NULL; 88308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char *canondomain = NULL; 884c5c4113dSnw141292 8858e228215Sdm199847 retcode = get_namerule_order(rule->winname, rule->windomain, 886cd37da74Snw141292 rule->unixname, rule->direction, 887cd37da74Snw141292 rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order); 888c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 889c5c4113dSnw141292 goto out; 890c5c4113dSnw141292 891c5c4113dSnw141292 if (w2u_order) 892c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order); 893c5c4113dSnw141292 if (u2w_order) 894c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order); 895c5c4113dSnw141292 89662c60062Sbaban /* 89762c60062Sbaban * For the triggers on namerules table to work correctly: 89862c60062Sbaban * 1) Use NULL instead of 0 for w2u_order and u2w_order 89962c60062Sbaban * 2) Use "" instead of NULL for "no domain" 90062c60062Sbaban */ 90162c60062Sbaban 90208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States name = rule->winname; 9038e228215Sdm199847 dom = rule->windomain; 904c5c4113dSnw141292 905c5c4113dSnw141292 RDLOCK_CONFIG(); 90608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (lookup_wksids_name2sid(name, dom, 90708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States &canonname, &canondomain, 90808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States NULL, NULL, NULL) == IDMAP_SUCCESS) { 90908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States name = canonname; 91008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States dom = canondomain; 91108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else if (EMPTY_STRING(dom)) { 912c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain) 913c8e26105Sjp151216 dom = _idmapdstate.cfg->pgcfg.default_domain; 914c5c4113dSnw141292 else 915c5c4113dSnw141292 dom = ""; 91662c60062Sbaban } 91784decf41Sjp151216 sql = sqlite_mprintf("INSERT into namerules " 918cd37da74Snw141292 "(is_user, is_wuser, windomain, winname_display, is_nt4, " 919c5c4113dSnw141292 "unixname, w2u_order, u2w_order) " 920cd37da74Snw141292 "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);", 921cd37da74Snw141292 rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom, 92208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States name, rule->is_nt4 ? 1 : 0, rule->unixname, 923cd37da74Snw141292 w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL); 924c5c4113dSnw141292 UNLOCK_CONFIG(); 925c5c4113dSnw141292 926c5c4113dSnw141292 if (sql == NULL) { 927c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 928c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 929c5c4113dSnw141292 goto out; 930c5c4113dSnw141292 } 931c5c4113dSnw141292 93271590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 933c5c4113dSnw141292 934c5c4113dSnw141292 if (retcode == IDMAP_ERR_OTHER) 935c5c4113dSnw141292 retcode = IDMAP_ERR_CFG; 936c5c4113dSnw141292 937c5c4113dSnw141292 out: 93808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(canonname); 93908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(canondomain); 94062c60062Sbaban if (sql != NULL) 941c5c4113dSnw141292 sqlite_freemem(sql); 942c5c4113dSnw141292 return (retcode); 943c5c4113dSnw141292 } 944c5c4113dSnw141292 945c5c4113dSnw141292 /* 946c5c4113dSnw141292 * Flush name-based mapping rules 947c5c4113dSnw141292 */ 948c5c4113dSnw141292 idmap_retcode 949cd37da74Snw141292 flush_namerules(sqlite *db) 950cd37da74Snw141292 { 951c5c4113dSnw141292 idmap_stat retcode; 952c5c4113dSnw141292 95371590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "DELETE FROM namerules;"); 954c5c4113dSnw141292 955c5c4113dSnw141292 return (retcode); 956c5c4113dSnw141292 } 957c5c4113dSnw141292 958c5c4113dSnw141292 /* 959c5c4113dSnw141292 * Generate and execute SQL statement to remove a name-based mapping rule 960c5c4113dSnw141292 */ 961c5c4113dSnw141292 idmap_retcode 962cd37da74Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule) 963cd37da74Snw141292 { 964c5c4113dSnw141292 char *sql = NULL; 965c5c4113dSnw141292 idmap_stat retcode; 966cd37da74Snw141292 char *expr = NULL; 967c5c4113dSnw141292 9688e228215Sdm199847 if (rule->direction < 0 && EMPTY_STRING(rule->windomain) && 9698e228215Sdm199847 EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname)) 970c5c4113dSnw141292 return (IDMAP_SUCCESS); 971c5c4113dSnw141292 972cd37da74Snw141292 retcode = gen_sql_expr_from_rule(rule, &expr); 973cd37da74Snw141292 if (retcode != IDMAP_SUCCESS) 974c5c4113dSnw141292 goto out; 975c5c4113dSnw141292 976bbf6f00cSJordan Brown sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s;", expr); 977c5c4113dSnw141292 978c5c4113dSnw141292 if (sql == NULL) { 979c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 980c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 981c5c4113dSnw141292 goto out; 982c5c4113dSnw141292 } 983c5c4113dSnw141292 984cd37da74Snw141292 98571590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 986c5c4113dSnw141292 987c5c4113dSnw141292 out: 988cd37da74Snw141292 if (expr != NULL) 989cd37da74Snw141292 sqlite_freemem(expr); 99062c60062Sbaban if (sql != NULL) 991c5c4113dSnw141292 sqlite_freemem(sql); 992c5c4113dSnw141292 return (retcode); 993c5c4113dSnw141292 } 994c5c4113dSnw141292 995c5c4113dSnw141292 /* 996c5c4113dSnw141292 * Compile the given SQL query and step just once. 997c5c4113dSnw141292 * 998c5c4113dSnw141292 * Input: 999c5c4113dSnw141292 * db - db handle 1000c5c4113dSnw141292 * sql - SQL statement 1001c5c4113dSnw141292 * 1002c5c4113dSnw141292 * Output: 1003c5c4113dSnw141292 * vm - virtual SQL machine 1004c5c4113dSnw141292 * ncol - number of columns in the result 1005c5c4113dSnw141292 * values - column values 1006c5c4113dSnw141292 * 1007c5c4113dSnw141292 * Return values: 1008c5c4113dSnw141292 * IDMAP_SUCCESS 1009c5c4113dSnw141292 * IDMAP_ERR_NOTFOUND 1010c5c4113dSnw141292 * IDMAP_ERR_INTERNAL 1011c5c4113dSnw141292 */ 1012c5c4113dSnw141292 1013cd37da74Snw141292 static 1014cd37da74Snw141292 idmap_retcode 1015c5c4113dSnw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol, 1016cd37da74Snw141292 int reqcol, const char ***values) 1017cd37da74Snw141292 { 1018c5c4113dSnw141292 char *errmsg = NULL; 101984decf41Sjp151216 int r; 1020c5c4113dSnw141292 102184decf41Sjp151216 if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) { 1022cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1023cd37da74Snw141292 CHECK_NULL(errmsg)); 1024c5c4113dSnw141292 sqlite_freemem(errmsg); 1025c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1026c5c4113dSnw141292 } 1027c5c4113dSnw141292 1028c5c4113dSnw141292 r = sqlite_step(*vm, ncol, values, NULL); 102984decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 1030c5c4113dSnw141292 103184decf41Sjp151216 if (r == SQLITE_ROW) { 103262c60062Sbaban if (ncol != NULL && *ncol < reqcol) { 1033c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1034c5c4113dSnw141292 *vm = NULL; 1035c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1036c5c4113dSnw141292 } 1037c5c4113dSnw141292 /* Caller will call finalize after using the results */ 1038c5c4113dSnw141292 return (IDMAP_SUCCESS); 1039c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 1040c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1041c5c4113dSnw141292 *vm = NULL; 1042c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1043c5c4113dSnw141292 } 1044c5c4113dSnw141292 1045c5c4113dSnw141292 (void) sqlite_finalize(*vm, &errmsg); 1046c5c4113dSnw141292 *vm = NULL; 1047cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1048cd37da74Snw141292 CHECK_NULL(errmsg)); 1049c5c4113dSnw141292 sqlite_freemem(errmsg); 1050c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1051c5c4113dSnw141292 } 1052c5c4113dSnw141292 105362c60062Sbaban /* 1054479ac375Sdm199847 * Load config in the state. 1055e8c27ec8Sbaban * 1056479ac375Sdm199847 * nm_siduid and nm_sidgid fields: 1057e8c27ec8Sbaban * state->nm_siduid represents mode used by sid2uid and uid2sid 1058e8c27ec8Sbaban * requests for directory-based name mappings. Similarly, 1059e8c27ec8Sbaban * state->nm_sidgid represents mode used by sid2gid and gid2sid 1060e8c27ec8Sbaban * requests. 1061e8c27ec8Sbaban * 1062e8c27ec8Sbaban * sid2uid/uid2sid: 1063e3f2c991SKeyur Desai * none -> directory_based_mapping != DIRECTORY_MAPPING_NAME 1064e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixuser_attr 1065e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr 1066e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixuser_attr 1067e8c27ec8Sbaban * 1068e8c27ec8Sbaban * sid2gid/gid2sid: 1069e3f2c991SKeyur Desai * none -> directory_based_mapping != DIRECTORY_MAPPING_NAME 1070e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixgroup_attr 1071e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr 1072e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr 1073e8c27ec8Sbaban */ 1074e8c27ec8Sbaban idmap_retcode 1075479ac375Sdm199847 load_cfg_in_state(lookup_state_t *state) 1076e8c27ec8Sbaban { 1077e8c27ec8Sbaban state->nm_siduid = IDMAP_NM_NONE; 1078e8c27ec8Sbaban state->nm_sidgid = IDMAP_NM_NONE; 1079e8c27ec8Sbaban RDLOCK_CONFIG(); 1080479ac375Sdm199847 10814aa0a5e7Snw141292 state->eph_map_unres_sids = 0; 10824aa0a5e7Snw141292 if (_idmapdstate.cfg->pgcfg.eph_map_unres_sids) 10834aa0a5e7Snw141292 state->eph_map_unres_sids = 1; 10844aa0a5e7Snw141292 1085e3f2c991SKeyur Desai state->directory_based_mapping = 1086e3f2c991SKeyur Desai _idmapdstate.cfg->pgcfg.directory_based_mapping; 1087e3f2c991SKeyur Desai 1088479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 1089479ac375Sdm199847 state->defdom = 1090479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.default_domain); 1091479ac375Sdm199847 if (state->defdom == NULL) { 1092479ac375Sdm199847 UNLOCK_CONFIG(); 1093479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1094479ac375Sdm199847 } 1095479ac375Sdm199847 } else { 1096479ac375Sdm199847 UNLOCK_CONFIG(); 1097dc03a638Sdm199847 return (IDMAP_SUCCESS); 1098479ac375Sdm199847 } 1099e3f2c991SKeyur Desai 1100e3f2c991SKeyur Desai if (_idmapdstate.cfg->pgcfg.directory_based_mapping != 1101e3f2c991SKeyur Desai DIRECTORY_MAPPING_NAME) { 1102e8c27ec8Sbaban UNLOCK_CONFIG(); 1103e8c27ec8Sbaban return (IDMAP_SUCCESS); 1104e8c27ec8Sbaban } 1105e3f2c991SKeyur Desai 1106e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1107e8c27ec8Sbaban state->nm_siduid = 1108e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1109e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1110e8c27ec8Sbaban state->nm_sidgid = 1111e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1112e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1113e8c27ec8Sbaban } else { 1114e8c27ec8Sbaban state->nm_siduid = 1115e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1116e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1117e8c27ec8Sbaban state->nm_sidgid = 1118e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1119e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1120e8c27ec8Sbaban } 1121e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) { 1122e8c27ec8Sbaban state->ad_unixuser_attr = 1123e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr); 1124e8c27ec8Sbaban if (state->ad_unixuser_attr == NULL) { 1125e8c27ec8Sbaban UNLOCK_CONFIG(); 1126e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1127e8c27ec8Sbaban } 1128e8c27ec8Sbaban } 1129e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) { 1130e8c27ec8Sbaban state->ad_unixgroup_attr = 1131e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr); 1132e8c27ec8Sbaban if (state->ad_unixgroup_attr == NULL) { 1133e8c27ec8Sbaban UNLOCK_CONFIG(); 1134e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1135e8c27ec8Sbaban } 1136e8c27ec8Sbaban } 1137479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1138479ac375Sdm199847 state->nldap_winname_attr = 1139479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr); 1140479ac375Sdm199847 if (state->nldap_winname_attr == NULL) { 1141479ac375Sdm199847 UNLOCK_CONFIG(); 1142479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1143479ac375Sdm199847 } 1144479ac375Sdm199847 } 1145e8c27ec8Sbaban UNLOCK_CONFIG(); 1146e8c27ec8Sbaban return (IDMAP_SUCCESS); 1147e8c27ec8Sbaban } 1148e8c27ec8Sbaban 1149e8c27ec8Sbaban /* 115008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Set the rule with specified values. 115148258c6bSjp151216 * All the strings are copied. 115248258c6bSjp151216 */ 115348258c6bSjp151216 static void 115448258c6bSjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain, 115548258c6bSjp151216 const char *winname, const char *unixname, boolean_t is_user, 115648258c6bSjp151216 boolean_t is_wuser, boolean_t is_nt4, int direction) 115748258c6bSjp151216 { 115848258c6bSjp151216 /* 115948258c6bSjp151216 * Only update if they differ because we have to free 116048258c6bSjp151216 * and duplicate the strings 116148258c6bSjp151216 */ 116248258c6bSjp151216 if (rule->windomain == NULL || windomain == NULL || 116348258c6bSjp151216 strcmp(rule->windomain, windomain) != 0) { 116448258c6bSjp151216 if (rule->windomain != NULL) { 116548258c6bSjp151216 free(rule->windomain); 116648258c6bSjp151216 rule->windomain = NULL; 116748258c6bSjp151216 } 116848258c6bSjp151216 if (windomain != NULL) 116948258c6bSjp151216 rule->windomain = strdup(windomain); 117048258c6bSjp151216 } 117148258c6bSjp151216 117248258c6bSjp151216 if (rule->winname == NULL || winname == NULL || 117348258c6bSjp151216 strcmp(rule->winname, winname) != 0) { 117448258c6bSjp151216 if (rule->winname != NULL) { 117548258c6bSjp151216 free(rule->winname); 117648258c6bSjp151216 rule->winname = NULL; 117748258c6bSjp151216 } 117848258c6bSjp151216 if (winname != NULL) 117948258c6bSjp151216 rule->winname = strdup(winname); 118048258c6bSjp151216 } 118148258c6bSjp151216 118248258c6bSjp151216 if (rule->unixname == NULL || unixname == NULL || 118348258c6bSjp151216 strcmp(rule->unixname, unixname) != 0) { 118448258c6bSjp151216 if (rule->unixname != NULL) { 118548258c6bSjp151216 free(rule->unixname); 118648258c6bSjp151216 rule->unixname = NULL; 118748258c6bSjp151216 } 118848258c6bSjp151216 if (unixname != NULL) 118948258c6bSjp151216 rule->unixname = strdup(unixname); 119048258c6bSjp151216 } 119148258c6bSjp151216 119248258c6bSjp151216 rule->is_user = is_user; 119348258c6bSjp151216 rule->is_wuser = is_wuser; 119448258c6bSjp151216 rule->is_nt4 = is_nt4; 119548258c6bSjp151216 rule->direction = direction; 119648258c6bSjp151216 } 119748258c6bSjp151216 1198e8c27ec8Sbaban /* 1199e8c27ec8Sbaban * Lookup well-known SIDs table either by winname or by SID. 120008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 120108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If the given winname or SID is a well-known SID then we set is_wksid 1202e8c27ec8Sbaban * variable and then proceed to see if the SID has a hard mapping to 1203e8c27ec8Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to 120408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * fixed ephemeral ids). The direction flag indicates whether we have 120508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * a mapping; UNDEF indicates that we do not. 120608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 120708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If we find a mapping then we return success, except for the 120808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * special case of SENTINEL_PID which indicates an inhibited mapping. 120908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 121008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If we find a matching entry, but no mapping, we supply SID, name, and type 121108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * information and return "not found". Higher layers will probably 121208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * do ephemeral mapping. 121308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 121408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If we do not find a match, we return "not found" and leave the question 121508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * to higher layers. 1216e8c27ec8Sbaban */ 1217cd37da74Snw141292 static 1218cd37da74Snw141292 idmap_retcode 121908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *is_wksid) 1220cd37da74Snw141292 { 122108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const wksids_table_t *wksid; 122262c60062Sbaban 122308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *is_wksid = 0; 1224e8c27ec8Sbaban 122508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States assert(req->id1.idmap_id_u.sid.prefix != NULL || 122608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1name != NULL); 122708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1228e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 122908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States wksid = find_wksid_by_sid(req->id1.idmap_id_u.sid.prefix, 123008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1.idmap_id_u.sid.rid, res->id.idtype); 123108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 123208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States wksid = find_wksid_by_name(req->id1name, req->id1domain, 123308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idtype); 123408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 123508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid == NULL) 123608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_ERR_NOTFOUND); 123708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 123808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Found matching entry. */ 123908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 124008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Fill in name if it was not already there. */ 1241e8c27ec8Sbaban if (req->id1name == NULL) { 124208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1name = strdup(wksid->winname); 1243e8c27ec8Sbaban if (req->id1name == NULL) 1244e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1245e8c27ec8Sbaban } 124608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 124708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Fill in SID if it was not already there */ 124808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (req->id1.idmap_id_u.sid.prefix == NULL) { 124908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->sidprefix != NULL) { 1250e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = 125108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States strdup(wksid->sidprefix); 125208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 125308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States RDLOCK_CONFIG(); 125408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1.idmap_id_u.sid.prefix = 125508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States strdup(_idmapdstate.cfg->pgcfg.machine_sid); 125608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States UNLOCK_CONFIG(); 125708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 1258e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) 1259e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 126008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1.idmap_id_u.sid.rid = wksid->rid; 1261e8c27ec8Sbaban } 1262e8c27ec8Sbaban 126308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Fill in the canonical domain if not already there */ 126408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (req->id1domain == NULL) { 126508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const char *dom; 126608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 126708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States RDLOCK_CONFIG(); 1268*fe1c642dSBill Krier if (wksid->domain != NULL) 126908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States dom = wksid->domain; 1270*fe1c642dSBill Krier else 127108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States dom = _idmapdstate.hostname; 127208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1domain = strdup(dom); 127308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States UNLOCK_CONFIG(); 127408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (req->id1domain == NULL) 127508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_ERR_MEMORY); 127608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 127708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 127808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *is_wksid = 1; 1279e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1280e8c27ec8Sbaban 128108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID; 1282e8c27ec8Sbaban 1283479ac375Sdm199847 if (res->id.idtype == IDMAP_POSIXID) { 128408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idtype = wksid->is_wuser ? IDMAP_UID : IDMAP_GID; 1285479ac375Sdm199847 } 128608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 128708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->direction == IDMAP_DIRECTION_UNDEF) { 128808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 128908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * We don't have a mapping 129008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * (But note that we may have supplied SID, name, or type 129108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * information.) 129208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 1293e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 129408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 129508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 129608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 129708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * We have an explicit mapping. 129808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 129908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->pid == SENTINEL_PID) { 130008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 130108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * ... which is that mapping is inhibited. 130208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 130308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_ERR_NOMAPPING); 130408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 130562c60062Sbaban 1306e8c27ec8Sbaban switch (res->id.idtype) { 1307c5c4113dSnw141292 case IDMAP_UID: 130808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idmap_id_u.uid = wksid->pid; 130908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States break; 1310c5c4113dSnw141292 case IDMAP_GID: 131108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idmap_id_u.gid = wksid->pid; 131208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States break; 1313c5c4113dSnw141292 default: 131408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* IDMAP_POSIXID is eliminated above */ 1315c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 1316c5c4113dSnw141292 } 131708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 131808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->direction = wksid->direction; 131908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID; 132008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->info.src = IDMAP_MAP_SRC_HARD_CODED; 132108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_SUCCESS); 1322c5c4113dSnw141292 } 1323c5c4113dSnw141292 1324cd37da74Snw141292 132508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 132608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Look for an entry mapping a PID to a SID. 132708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 132808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Note that direction=UNDEF entries do not specify a mapping, 132908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * and that SENTINEL_PID entries represent either an inhibited 133008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * mapping or an ephemeral mapping. We don't handle either here; 133108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * they are filtered out by find_wksid_by_pid. 133208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 1333cd37da74Snw141292 static 1334cd37da74Snw141292 idmap_retcode 1335cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user) 1336cd37da74Snw141292 { 133708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const wksids_table_t *wksid; 133808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 133908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States wksid = find_wksid_by_pid(req->id1.idmap_id_u.uid, is_user); 134008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid == NULL) 1341e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 134208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1343e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) { 134408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idtype = wksid->is_wuser ? IDMAP_USID : IDMAP_GSID; 1345e8c27ec8Sbaban } 134608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idmap_id_u.sid.rid = wksid->rid; 134708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 134808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->sidprefix != NULL) { 1349c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 135008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States strdup(wksid->sidprefix); 135108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 135208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States RDLOCK_CONFIG(); 135308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->id.idmap_id_u.sid.prefix = 135408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States strdup(_idmapdstate.cfg->pgcfg.machine_sid); 135508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States UNLOCK_CONFIG(); 135608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 135708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1358c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 1359c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1360c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 1361c5c4113dSnw141292 } 136208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1363*fe1c642dSBill Krier /* Fill in name if it was not already there. */ 1364*fe1c642dSBill Krier if (req->id2name == NULL) { 1365*fe1c642dSBill Krier req->id2name = strdup(wksid->winname); 1366*fe1c642dSBill Krier if (req->id2name == NULL) 1367*fe1c642dSBill Krier return (IDMAP_ERR_MEMORY); 1368*fe1c642dSBill Krier } 1369*fe1c642dSBill Krier 1370*fe1c642dSBill Krier /* Fill in the canonical domain if not already there */ 1371*fe1c642dSBill Krier if (req->id2domain == NULL) { 1372*fe1c642dSBill Krier const char *dom; 1373*fe1c642dSBill Krier 1374*fe1c642dSBill Krier RDLOCK_CONFIG(); 1375*fe1c642dSBill Krier if (wksid->domain != NULL) 1376*fe1c642dSBill Krier dom = wksid->domain; 1377*fe1c642dSBill Krier else 1378*fe1c642dSBill Krier dom = _idmapdstate.hostname; 1379*fe1c642dSBill Krier req->id2domain = strdup(dom); 1380*fe1c642dSBill Krier UNLOCK_CONFIG(); 1381*fe1c642dSBill Krier if (req->id2domain == NULL) 1382*fe1c642dSBill Krier return (IDMAP_ERR_MEMORY); 1383*fe1c642dSBill Krier } 1384*fe1c642dSBill Krier 138508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States res->direction = wksid->direction; 1386fc724630SAlan Wright res->info.how.map_type = IDMAP_MAP_TYPE_KNOWN_SID; 138748258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 1388c5c4113dSnw141292 return (IDMAP_SUCCESS); 1389c5c4113dSnw141292 } 139062c60062Sbaban 139108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 139208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Look up a name in the wksids list, matching name and, if supplied, domain, 139308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * and extract data. 139408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 139508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Given: 139608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * name Windows user name 139708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * domain Windows domain name (or NULL) 139808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 139908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Return: Error code 140008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 140108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *canonname canonical name (if canonname non-NULL) [1] 140208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *canondomain canonical domain (if canondomain non-NULL) [1] 140308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *sidprefix SID prefix (if sidprefix non-NULL) [1] 140408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *rid RID (if rid non-NULL) [2] 140508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *type Type (if type non-NULL) [2] 140608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 140708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * [1] malloc'ed, NULL on error 140808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * [2] Undefined on error 140908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 1410cd37da74Snw141292 idmap_retcode 141108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_wksids_name2sid( 141208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const char *name, 141308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const char *domain, 141408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **canonname, 141508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **canondomain, 141608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **sidprefix, 141708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States idmap_rid_t *rid, 141808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States int *type) 1419cd37da74Snw141292 { 142008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const wksids_table_t *wksid; 1421479ac375Sdm199847 142208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (sidprefix != NULL) 142308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = NULL; 142408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) 142508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = NULL; 142608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL) 142708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = NULL; 1428479ac375Sdm199847 142908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States wksid = find_wksid_by_name(name, domain, IDMAP_POSIXID); 143008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid == NULL) 143108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_ERR_NOTFOUND); 143208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 143308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (sidprefix != NULL) { 143408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->sidprefix != NULL) { 143508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = strdup(wksid->sidprefix); 143608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 143708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States RDLOCK_CONFIG(); 143808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = strdup( 143908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States _idmapdstate.cfg->pgcfg.machine_sid); 144008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States UNLOCK_CONFIG(); 144162c60062Sbaban } 144208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*sidprefix == NULL) 144308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States goto nomem; 144408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 144508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 144608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (rid != NULL) 144708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *rid = wksid->rid; 144808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 144908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) { 145008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = strdup(wksid->winname); 145108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*canonname == NULL) 145208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States goto nomem; 145308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 145408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 145508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL) { 145608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (wksid->domain != NULL) { 145708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = strdup(wksid->domain); 145808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 145908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States RDLOCK_CONFIG(); 146008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = strdup(_idmapdstate.hostname); 146108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States UNLOCK_CONFIG(); 146208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 146308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*canondomain == NULL) 146408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States goto nomem; 146508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 146608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 146708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (type != NULL) 146808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *type = (wksid->is_wuser) ? 146908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States _IDMAP_T_USER : _IDMAP_T_GROUP; 147008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 147108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States return (IDMAP_SUCCESS); 147208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 147308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States nomem: 1474cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 147508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1476e8c27ec8Sbaban if (sidprefix != NULL) { 1477e8c27ec8Sbaban free(*sidprefix); 1478e8c27ec8Sbaban *sidprefix = NULL; 1479e8c27ec8Sbaban } 148008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 148108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) { 148208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*canonname); 148308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = NULL; 148408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 148508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 148608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL) { 148708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*canondomain); 148808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = NULL; 148908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 149008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1491cd37da74Snw141292 return (IDMAP_ERR_MEMORY); 1492cd37da74Snw141292 } 1493c5c4113dSnw141292 1494cd37da74Snw141292 static 1495cd37da74Snw141292 idmap_retcode 1496cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1497cd37da74Snw141292 { 1498c5c4113dSnw141292 char *end; 1499c5c4113dSnw141292 char *sql = NULL; 1500c5c4113dSnw141292 const char **values; 1501c5c4113dSnw141292 sqlite_vm *vm = NULL; 1502c5c4113dSnw141292 int ncol, is_user; 1503c5c4113dSnw141292 uid_t pid; 1504c5c4113dSnw141292 time_t curtime, exp; 1505c5c4113dSnw141292 idmap_retcode retcode; 1506042addd6Sbaban char *is_user_string, *lower_name; 1507c5c4113dSnw141292 1508c5c4113dSnw141292 /* Current time */ 1509c5c4113dSnw141292 errno = 0; 1510c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1511cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1512c5c4113dSnw141292 strerror(errno)); 1513c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1514c5c4113dSnw141292 goto out; 1515c5c4113dSnw141292 } 1516c5c4113dSnw141292 1517e8c27ec8Sbaban switch (res->id.idtype) { 1518cd37da74Snw141292 case IDMAP_UID: 1519cd37da74Snw141292 is_user_string = "1"; 1520cd37da74Snw141292 break; 1521cd37da74Snw141292 case IDMAP_GID: 1522cd37da74Snw141292 is_user_string = "0"; 1523cd37da74Snw141292 break; 1524cd37da74Snw141292 case IDMAP_POSIXID: 1525cd37da74Snw141292 /* the non-diagonal mapping */ 1526cd37da74Snw141292 is_user_string = "is_wuser"; 1527cd37da74Snw141292 break; 1528cd37da74Snw141292 default: 1529cd37da74Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 1530cd37da74Snw141292 goto out; 1531cd37da74Snw141292 } 1532cd37da74Snw141292 1533c5c4113dSnw141292 /* SQL to lookup the cache */ 153448258c6bSjp151216 1535e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1536e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 153748258c6bSjp151216 "unixname, u2w, is_wuser, " 153848258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 153948258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1540cd37da74Snw141292 "FROM idmap_cache WHERE is_user = %s AND " 1541c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 1542c5c4113dSnw141292 "(pid >= 2147483648 OR " 1543c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1544c5c4113dSnw141292 "expiration > %d));", 1545e8c27ec8Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix, 1546e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, curtime); 1547e8c27ec8Sbaban } else if (req->id1name != NULL) { 1548042addd6Sbaban if ((lower_name = tolower_u8(req->id1name)) == NULL) 1549042addd6Sbaban lower_name = req->id1name; 1550e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 155148258c6bSjp151216 "unixname, u2w, is_wuser, " 155248258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 155348258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1554e8c27ec8Sbaban "FROM idmap_cache WHERE is_user = %s AND " 1555e8c27ec8Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND " 1556e8c27ec8Sbaban "(pid >= 2147483648 OR " 1557e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 1558e8c27ec8Sbaban "expiration > %d));", 155948258c6bSjp151216 is_user_string, lower_name, req->id1domain, 156048258c6bSjp151216 curtime); 1561042addd6Sbaban if (lower_name != req->id1name) 1562042addd6Sbaban free(lower_name); 1563e8c27ec8Sbaban } else { 1564e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 1565e8c27ec8Sbaban goto out; 1566e8c27ec8Sbaban } 1567c5c4113dSnw141292 if (sql == NULL) { 1568c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1569c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1570c5c4113dSnw141292 goto out; 1571c5c4113dSnw141292 } 157248258c6bSjp151216 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 157348258c6bSjp151216 14, &values); 1574c5c4113dSnw141292 sqlite_freemem(sql); 1575c5c4113dSnw141292 1576c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 1577c5c4113dSnw141292 goto out; 1578c5c4113dSnw141292 } else if (retcode == IDMAP_SUCCESS) { 1579c5c4113dSnw141292 /* sanity checks */ 1580c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 1581c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1582c5c4113dSnw141292 goto out; 1583c5c4113dSnw141292 } 1584c5c4113dSnw141292 1585c5c4113dSnw141292 pid = strtoul(values[0], &end, 10); 1586c5c4113dSnw141292 is_user = strncmp(values[1], "0", 2) ? 1 : 0; 1587c5c4113dSnw141292 1588cd37da74Snw141292 if (is_user) { 1589cd37da74Snw141292 res->id.idtype = IDMAP_UID; 1590cd37da74Snw141292 res->id.idmap_id_u.uid = pid; 1591cd37da74Snw141292 } else { 1592cd37da74Snw141292 res->id.idtype = IDMAP_GID; 1593cd37da74Snw141292 res->id.idmap_id_u.gid = pid; 1594cd37da74Snw141292 } 1595cd37da74Snw141292 1596c5c4113dSnw141292 /* 1597c5c4113dSnw141292 * We may have an expired ephemeral mapping. Consider 1598c5c4113dSnw141292 * the expired entry as valid if we are not going to 1599c5c4113dSnw141292 * perform name-based mapping. But do not renew the 1600c5c4113dSnw141292 * expiration. 1601c5c4113dSnw141292 * If we will be doing name-based mapping then store the 1602c5c4113dSnw141292 * ephemeral pid in the result so that we can use it 1603c5c4113dSnw141292 * if we end up doing dynamic mapping again. 1604c5c4113dSnw141292 */ 1605c5c4113dSnw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) && 1606cd37da74Snw141292 !AVOID_NAMESERVICE(req) && 1607cd37da74Snw141292 IS_EPHEMERAL(pid) && values[2] != NULL) { 1608c5c4113dSnw141292 exp = strtoll(values[2], &end, 10); 1609c5c4113dSnw141292 if (exp && exp <= curtime) { 1610c5c4113dSnw141292 /* Store the ephemeral pid */ 1611651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 1612cd37da74Snw141292 req->direction |= is_user 1613cd37da74Snw141292 ? _IDMAP_F_EXP_EPH_UID 1614cd37da74Snw141292 : _IDMAP_F_EXP_EPH_GID; 1615c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 1616c5c4113dSnw141292 } 1617c5c4113dSnw141292 } 1618c5c4113dSnw141292 } 1619c5c4113dSnw141292 1620c5c4113dSnw141292 out: 1621c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 162262c60062Sbaban if (values[4] != NULL) 1623c5c4113dSnw141292 res->direction = 1624651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 1625651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 1626c5c4113dSnw141292 else 1627651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 1628c5c4113dSnw141292 162962c60062Sbaban if (values[3] != NULL) { 1630e8c27ec8Sbaban if (req->id2name != NULL) 1631e8c27ec8Sbaban free(req->id2name); 16328e228215Sdm199847 req->id2name = strdup(values[3]); 16338e228215Sdm199847 if (req->id2name == NULL) { 1634c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1635c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1636c5c4113dSnw141292 } 1637c5c4113dSnw141292 } 1638e8c27ec8Sbaban 1639e8c27ec8Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ? 1640e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 164148258c6bSjp151216 164248258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 164348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 164448258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 164548258c6bSjp151216 switch (res->info.how.map_type) { 164648258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 164748258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 164848258c6bSjp151216 strdup(values[7]); 164948258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 165048258c6bSjp151216 strdup(values[8]); 165148258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 165248258c6bSjp151216 strdup(values[9]); 165348258c6bSjp151216 break; 165448258c6bSjp151216 165548258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 165648258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 165748258c6bSjp151216 strdup(values[7]); 165848258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 165948258c6bSjp151216 strdup(values[8]); 166048258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 166148258c6bSjp151216 strdup(values[9]); 166248258c6bSjp151216 break; 166348258c6bSjp151216 166448258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 166548258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 166648258c6bSjp151216 strdup(values[10]); 166748258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 166848258c6bSjp151216 strdup(values[11]); 166948258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 167048258c6bSjp151216 strdup(values[12]); 167148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 167248258c6bSjp151216 strtoul(values[13], &end, 1); 167348258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 167448258c6bSjp151216 is_user; 167548258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 167648258c6bSjp151216 strtoul(values[5], &end, 1); 167748258c6bSjp151216 break; 167848258c6bSjp151216 167948258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 168048258c6bSjp151216 break; 168148258c6bSjp151216 168248258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 168348258c6bSjp151216 break; 168448258c6bSjp151216 168548258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 168648258c6bSjp151216 break; 168748258c6bSjp151216 1688e3f2c991SKeyur Desai case IDMAP_MAP_TYPE_IDMU: 1689e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.dn = 1690e3f2c991SKeyur Desai strdup(values[7]); 1691e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.attr = 1692e3f2c991SKeyur Desai strdup(values[8]); 1693e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.value = 1694e3f2c991SKeyur Desai strdup(values[9]); 1695e3f2c991SKeyur Desai break; 1696e3f2c991SKeyur Desai 169748258c6bSjp151216 default: 1698e3f2c991SKeyur Desai /* Unknown mapping type */ 169948258c6bSjp151216 assert(FALSE); 170048258c6bSjp151216 } 170148258c6bSjp151216 } 1702c5c4113dSnw141292 } 170362c60062Sbaban if (vm != NULL) 1704c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1705c5c4113dSnw141292 return (retcode); 1706c5c4113dSnw141292 } 1707c5c4113dSnw141292 1708cd37da74Snw141292 static 1709cd37da74Snw141292 idmap_retcode 171062c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid, 171108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **canonname, char **canondomain, int *type) 1712cd37da74Snw141292 { 1713c5c4113dSnw141292 char *end; 1714c5c4113dSnw141292 char *sql = NULL; 1715c5c4113dSnw141292 const char **values; 1716c5c4113dSnw141292 sqlite_vm *vm = NULL; 1717c5c4113dSnw141292 int ncol; 1718c5c4113dSnw141292 time_t curtime; 1719c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 1720c5c4113dSnw141292 1721c5c4113dSnw141292 /* Get current time */ 1722c5c4113dSnw141292 errno = 0; 1723c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1724cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1725c5c4113dSnw141292 strerror(errno)); 1726c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1727c5c4113dSnw141292 goto out; 1728c5c4113dSnw141292 } 1729c5c4113dSnw141292 1730c5c4113dSnw141292 /* SQL to lookup the cache */ 1731cd37da74Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type " 1732cd37da74Snw141292 "FROM name_cache WHERE " 1733c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND " 1734c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1735c5c4113dSnw141292 "expiration > %d);", 1736c5c4113dSnw141292 sidprefix, rid, curtime); 1737c5c4113dSnw141292 if (sql == NULL) { 1738c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1739c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1740c5c4113dSnw141292 goto out; 1741c5c4113dSnw141292 } 1742c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values); 1743c5c4113dSnw141292 sqlite_freemem(sql); 1744c5c4113dSnw141292 1745c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 174662c60062Sbaban if (type != NULL) { 1747c5c4113dSnw141292 if (values[2] == NULL) { 1748c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1749c5c4113dSnw141292 goto out; 1750c5c4113dSnw141292 } 1751c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 1752c5c4113dSnw141292 } 1753c5c4113dSnw141292 175408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL && values[0] != NULL) { 175508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if ((*canonname = strdup(values[0])) == NULL) { 1756c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1757c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1758c5c4113dSnw141292 goto out; 1759c5c4113dSnw141292 } 1760c5c4113dSnw141292 } 1761c5c4113dSnw141292 176208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL && values[1] != NULL) { 176308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if ((*canondomain = strdup(values[1])) == NULL) { 176408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) { 176508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*canonname); 176608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = NULL; 1767c5c4113dSnw141292 } 1768c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1769c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1770c5c4113dSnw141292 goto out; 1771c5c4113dSnw141292 } 1772c5c4113dSnw141292 } 1773c5c4113dSnw141292 } 1774c5c4113dSnw141292 1775c5c4113dSnw141292 out: 177662c60062Sbaban if (vm != NULL) 1777c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1778c5c4113dSnw141292 return (retcode); 1779c5c4113dSnw141292 } 1780c5c4113dSnw141292 1781c5c4113dSnw141292 /* 1782e8c27ec8Sbaban * Given SID, find winname using name_cache OR 1783e8c27ec8Sbaban * Given winname, find SID using name_cache. 1784e8c27ec8Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and 1785e8c27ec8Sbaban * req->id2 is unix id 1786c5c4113dSnw141292 */ 1787cd37da74Snw141292 static 1788cd37da74Snw141292 idmap_retcode 1789e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1790cd37da74Snw141292 { 1791c5c4113dSnw141292 int type = -1; 1792c5c4113dSnw141292 idmap_retcode retcode; 1793e8c27ec8Sbaban char *sidprefix = NULL; 1794c5c4113dSnw141292 idmap_rid_t rid; 1795c5c4113dSnw141292 char *name = NULL, *domain = NULL; 1796c5c4113dSnw141292 1797e8c27ec8Sbaban /* Done if we've both sid and winname */ 1798e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) 1799e8c27ec8Sbaban return (IDMAP_SUCCESS); 1800c5c4113dSnw141292 1801e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 180208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Lookup sid to winname */ 1803e8c27ec8Sbaban retcode = lookup_cache_sid2name(cache, 1804e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix, 1805e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type); 180608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } else { 180708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Lookup winame to sid */ 180808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States retcode = lookup_cache_name2sid(cache, req->id1name, 180908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1domain, &name, &sidprefix, &rid, &type); 1810e8c27ec8Sbaban } 181162c60062Sbaban 1812e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1813c5c4113dSnw141292 free(name); 1814c5c4113dSnw141292 free(domain); 1815e8c27ec8Sbaban free(sidprefix); 1816e8c27ec8Sbaban return (retcode); 1817e8c27ec8Sbaban } 1818e8c27ec8Sbaban 1819e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) { 1820e8c27ec8Sbaban res->id.idtype = (type == _IDMAP_T_USER) ? 1821e8c27ec8Sbaban IDMAP_UID : IDMAP_GID; 1822e8c27ec8Sbaban } 1823e8c27ec8Sbaban req->id1.idtype = (type == _IDMAP_T_USER) ? 1824e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1825e8c27ec8Sbaban 1826e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 182708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 182808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 182908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If we found canonical names or domain, use them instead of 183008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * the existing values. 183108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 1832e8c27ec8Sbaban if (name != NULL) { 183308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(req->id1name); 183408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id1name = name; 1835e8c27ec8Sbaban } 183608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (domain != NULL) { 183708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(req->id1domain); 1838e8c27ec8Sbaban req->id1domain = domain; 183908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 184008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 1841e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) { 1842e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix; 1843e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = rid; 1844c5c4113dSnw141292 } 1845c5c4113dSnw141292 return (retcode); 1846c5c4113dSnw141292 } 1847c5c4113dSnw141292 18484d61c878SJulian Pullen 18494d61c878SJulian Pullen 18504d61c878SJulian Pullen static int 18514d61c878SJulian Pullen ad_lookup_batch_int(lookup_state_t *state, idmap_mapping_batch *batch, 1852e3f2c991SKeyur Desai idmap_ids_res *result, adutils_ad_t *dir, int how_local, 1853e3f2c991SKeyur Desai int *num_processed) 1854cd37da74Snw141292 { 1855c5c4113dSnw141292 idmap_retcode retcode; 1856e3f2c991SKeyur Desai int i, num_queued, is_wuser, is_user; 18574d61c878SJulian Pullen int next_request; 1858e8c27ec8Sbaban int retries = 0, eunixtype; 1859e8c27ec8Sbaban char **unixname; 1860c5c4113dSnw141292 idmap_mapping *req; 1861c5c4113dSnw141292 idmap_id_res *res; 1862e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 186348258c6bSjp151216 idmap_how *how; 1864479ac375Sdm199847 char **dn, **attr, **value; 1865e8c27ec8Sbaban 18664d61c878SJulian Pullen *num_processed = 0; 18674d61c878SJulian Pullen 1868e8c27ec8Sbaban /* 1869e8c27ec8Sbaban * Since req->id2.idtype is unused, we will use it here 1870e8c27ec8Sbaban * to retrieve the value of sid_type. But it needs to be 1871e8c27ec8Sbaban * reset to IDMAP_NONE before we return to prevent xdr 1872e8c27ec8Sbaban * from mis-interpreting req->id2 when it tries to free 1873e8c27ec8Sbaban * the input argument. Other option is to allocate an 1874e8c27ec8Sbaban * array of integers and use it instead for the batched 1875e8c27ec8Sbaban * call. But why un-necessarily allocate memory. That may 1876e8c27ec8Sbaban * be an option if req->id2.idtype cannot be re-used in 1877e8c27ec8Sbaban * future. 1878e3f2c991SKeyur Desai * 1879e3f2c991SKeyur Desai * Similarly, we use req->id2.idmap_id_u.uid to return uidNumber 1880e3f2c991SKeyur Desai * or gidNumber supplied by IDMU, and reset it back to SENTINEL_PID 1881e3f2c991SKeyur Desai * when we're done. Note that the query always puts the result in 1882e3f2c991SKeyur Desai * req->id2.idmap_id_u.uid, not .gid. 1883e8c27ec8Sbaban */ 1884c5c4113dSnw141292 retry: 1885e3f2c991SKeyur Desai retcode = idmap_lookup_batch_start(dir, state->ad_nqueries, 1886e3f2c991SKeyur Desai state->directory_based_mapping, 1887e3f2c991SKeyur Desai state->defdom, 1888e3f2c991SKeyur Desai &qs); 1889e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 18902b4a7802SBaban Kenkre if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && 18912b4a7802SBaban Kenkre retries++ < ADUTILS_DEF_NUM_RETRIES) 18920dcc7149Snw141292 goto retry; 1893349d5d8fSnw141292 degrade_svc(1, "failed to create batch for AD lookup"); 1894e8c27ec8Sbaban goto out; 1895c5c4113dSnw141292 } 18964d61c878SJulian Pullen num_queued = 0; 1897c5c4113dSnw141292 1898c8e26105Sjp151216 restore_svc(); 1899c8e26105Sjp151216 1900e3f2c991SKeyur Desai if (how_local & FOREST_IS_LOCAL) { 19014d61c878SJulian Pullen /* 19024d61c878SJulian Pullen * Directory based name mapping is only performed within the 1903e3f2c991SKeyur Desai * joined forest. We don't trust other "trusted" 19044d61c878SJulian Pullen * forests to provide DS-based name mapping information because 19054d61c878SJulian Pullen * AD's definition of "cross-forest trust" does not encompass 19064d61c878SJulian Pullen * this sort of behavior. 19074d61c878SJulian Pullen */ 19084d61c878SJulian Pullen idmap_lookup_batch_set_unixattr(qs, 19094d61c878SJulian Pullen state->ad_unixuser_attr, state->ad_unixgroup_attr); 19104d61c878SJulian Pullen } 1911e8c27ec8Sbaban 19124d61c878SJulian Pullen for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 1913c5c4113dSnw141292 req = &batch->idmap_mapping_batch_val[i]; 1914c5c4113dSnw141292 res = &result->ids.ids_val[i]; 191548258c6bSjp151216 how = &res->info.how; 191648258c6bSjp151216 1917e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 1918e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1919e3f2c991SKeyur Desai req->id2.idmap_id_u.uid = SENTINEL_PID; 1920c5c4113dSnw141292 1921e3f2c991SKeyur Desai /* Skip if no AD lookup required */ 1922e3f2c991SKeyur Desai if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1923e8c27ec8Sbaban continue; 1924e8c27ec8Sbaban 1925e3f2c991SKeyur Desai /* Skip if we've already tried and gotten a "not found" */ 1926e3f2c991SKeyur Desai if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD) 1927e3f2c991SKeyur Desai continue; 1928e3f2c991SKeyur Desai 1929e3f2c991SKeyur Desai /* Skip if we've already either succeeded or failed */ 193096c3a9a0Sbaban if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR) 1931c5c4113dSnw141292 continue; 1932e8c27ec8Sbaban 1933e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1934e8c27ec8Sbaban 1935479ac375Sdm199847 /* win2unix request: */ 1936e8c27ec8Sbaban 1937e3f2c991SKeyur Desai posix_id_t *pid = NULL; 1938479ac375Sdm199847 unixname = dn = attr = value = NULL; 1939e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 1940e3f2c991SKeyur Desai if (state->directory_based_mapping == 1941e3f2c991SKeyur Desai DIRECTORY_MAPPING_NAME && 1942e3f2c991SKeyur Desai req->id2name == NULL) { 1943e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID && 1944e8c27ec8Sbaban AD_OR_MIXED(state->nm_siduid)) { 1945e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 1946e8c27ec8Sbaban unixname = &req->id2name; 1947e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GID && 1948e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1949e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 1950e8c27ec8Sbaban unixname = &req->id2name; 1951e8c27ec8Sbaban } else if (AD_OR_MIXED(state->nm_siduid) || 1952e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1953e8c27ec8Sbaban unixname = &req->id2name; 1954e8c27ec8Sbaban } 19554d61c878SJulian Pullen 1956479ac375Sdm199847 if (unixname != NULL) { 1957479ac375Sdm199847 /* 1958479ac375Sdm199847 * Get how info for DS-based name 1959479ac375Sdm199847 * mapping only if AD or MIXED 1960479ac375Sdm199847 * mode is enabled. 1961479ac375Sdm199847 */ 1962479ac375Sdm199847 idmap_info_free(&res->info); 196348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 196448258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1965479ac375Sdm199847 dn = &how->idmap_how_u.ad.dn; 1966479ac375Sdm199847 attr = &how->idmap_how_u.ad.attr; 1967479ac375Sdm199847 value = &how->idmap_how_u.ad.value; 1968479ac375Sdm199847 } 1969e3f2c991SKeyur Desai } else if (state->directory_based_mapping == 1970e3f2c991SKeyur Desai DIRECTORY_MAPPING_IDMU && 1971e3f2c991SKeyur Desai (how_local & DOMAIN_IS_LOCAL)) { 1972e3f2c991SKeyur Desai /* 1973e3f2c991SKeyur Desai * Ensure that we only do IDMU processing 1974e3f2c991SKeyur Desai * when querying the domain we've joined. 1975e3f2c991SKeyur Desai */ 1976e3f2c991SKeyur Desai pid = &req->id2.idmap_id_u.uid; 1977e3f2c991SKeyur Desai /* 1978e3f2c991SKeyur Desai * Get how info for IDMU based mapping. 1979e3f2c991SKeyur Desai */ 1980e3f2c991SKeyur Desai idmap_info_free(&res->info); 1981e3f2c991SKeyur Desai res->info.src = IDMAP_MAP_SRC_NEW; 1982e3f2c991SKeyur Desai how->map_type = IDMAP_MAP_TYPE_IDMU; 1983e3f2c991SKeyur Desai dn = &how->idmap_how_u.idmu.dn; 1984e3f2c991SKeyur Desai attr = &how->idmap_how_u.idmu.attr; 1985e3f2c991SKeyur Desai value = &how->idmap_how_u.idmu.value; 1986e3f2c991SKeyur Desai } 1987e3f2c991SKeyur Desai 1988479ac375Sdm199847 if (req->id1.idmap_id_u.sid.prefix != NULL) { 1989479ac375Sdm199847 /* Lookup AD by SID */ 1990c5c4113dSnw141292 retcode = idmap_sid2name_batch_add1( 1991e8c27ec8Sbaban qs, req->id1.idmap_id_u.sid.prefix, 1992e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, eunixtype, 1993479ac375Sdm199847 dn, attr, value, 1994479ac375Sdm199847 (req->id1name == NULL) ? 1995479ac375Sdm199847 &req->id1name : NULL, 1996479ac375Sdm199847 (req->id1domain == NULL) ? 1997479ac375Sdm199847 &req->id1domain : NULL, 1998479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1999e3f2c991SKeyur Desai pid, 2000479ac375Sdm199847 &res->retcode); 20014d61c878SJulian Pullen if (retcode == IDMAP_SUCCESS) 20024d61c878SJulian Pullen num_queued++; 2003479ac375Sdm199847 } else { 2004479ac375Sdm199847 /* Lookup AD by winname */ 2005479ac375Sdm199847 assert(req->id1name != NULL); 2006479ac375Sdm199847 retcode = idmap_name2sid_batch_add1( 2007479ac375Sdm199847 qs, req->id1name, req->id1domain, 2008479ac375Sdm199847 eunixtype, 2009479ac375Sdm199847 dn, attr, value, 2010479ac375Sdm199847 &req->id1name, 2011479ac375Sdm199847 &req->id1.idmap_id_u.sid.prefix, 2012479ac375Sdm199847 &req->id1.idmap_id_u.sid.rid, 2013479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 2014e3f2c991SKeyur Desai pid, 2015479ac375Sdm199847 &res->retcode); 20164d61c878SJulian Pullen if (retcode == IDMAP_SUCCESS) 20174d61c878SJulian Pullen num_queued++; 2018479ac375Sdm199847 } 2019c5c4113dSnw141292 2020e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 2021479ac375Sdm199847 2022479ac375Sdm199847 /* unix2win request: */ 2023e8c27ec8Sbaban 2024e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL && 2025e8c27ec8Sbaban req->id2name != NULL) { 20264d61c878SJulian Pullen /* Already have SID and winname. done */ 2027e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 2028e8c27ec8Sbaban continue; 2029c5c4113dSnw141292 } 2030c5c4113dSnw141292 2031e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) { 2032cd37da74Snw141292 /* 2033e8c27ec8Sbaban * SID but no winname -- lookup AD by 2034e8c27ec8Sbaban * SID to get winname. 2035479ac375Sdm199847 * how info is not needed here because 2036479ac375Sdm199847 * we are not retrieving unixname from 2037479ac375Sdm199847 * AD. 2038cd37da74Snw141292 */ 20394d61c878SJulian Pullen 2040e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1( 2041e8c27ec8Sbaban qs, res->id.idmap_id_u.sid.prefix, 2042e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 204348258c6bSjp151216 _IDMAP_T_UNDEF, 2044479ac375Sdm199847 NULL, NULL, NULL, 204548258c6bSjp151216 &req->id2name, 2046e8c27ec8Sbaban &req->id2domain, (int *)&req->id2.idtype, 2047e3f2c991SKeyur Desai NULL, NULL, &res->retcode); 20484d61c878SJulian Pullen if (retcode == IDMAP_SUCCESS) 20494d61c878SJulian Pullen num_queued++; 2050e8c27ec8Sbaban } else if (req->id2name != NULL) { 2051e8c27ec8Sbaban /* 2052e8c27ec8Sbaban * winname but no SID -- lookup AD by 2053e8c27ec8Sbaban * winname to get SID. 2054479ac375Sdm199847 * how info is not needed here because 2055479ac375Sdm199847 * we are not retrieving unixname from 2056479ac375Sdm199847 * AD. 2057e8c27ec8Sbaban */ 2058e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1( 2059e8c27ec8Sbaban qs, req->id2name, req->id2domain, 206048258c6bSjp151216 _IDMAP_T_UNDEF, 2061479ac375Sdm199847 NULL, NULL, NULL, NULL, 2062e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 2063e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 2064e8c27ec8Sbaban (int *)&req->id2.idtype, NULL, 2065e3f2c991SKeyur Desai NULL, 2066e8c27ec8Sbaban &res->retcode); 20674d61c878SJulian Pullen if (retcode == IDMAP_SUCCESS) 20684d61c878SJulian Pullen num_queued++; 2069e3f2c991SKeyur Desai } else if (state->directory_based_mapping == 2070e3f2c991SKeyur Desai DIRECTORY_MAPPING_IDMU && 2071e3f2c991SKeyur Desai (how_local & DOMAIN_IS_LOCAL)) { 2072e3f2c991SKeyur Desai assert(req->id1.idmap_id_u.uid != SENTINEL_PID); 2073e3f2c991SKeyur Desai is_user = IS_REQUEST_UID(*req); 2074e3f2c991SKeyur Desai if (res->id.idtype == IDMAP_USID) 2075e3f2c991SKeyur Desai is_wuser = 1; 2076e3f2c991SKeyur Desai else if (res->id.idtype == IDMAP_GSID) 2077e3f2c991SKeyur Desai is_wuser = 0; 2078e3f2c991SKeyur Desai else 2079e3f2c991SKeyur Desai is_wuser = is_user; 2080e3f2c991SKeyur Desai 2081e3f2c991SKeyur Desai /* IDMU can't do diagonal mappings */ 2082e3f2c991SKeyur Desai if (is_user != is_wuser) 2083e3f2c991SKeyur Desai continue; 2084e3f2c991SKeyur Desai 2085e3f2c991SKeyur Desai idmap_info_free(&res->info); 2086e3f2c991SKeyur Desai res->info.src = IDMAP_MAP_SRC_NEW; 2087e3f2c991SKeyur Desai how->map_type = IDMAP_MAP_TYPE_IDMU; 2088e3f2c991SKeyur Desai retcode = idmap_pid2sid_batch_add1( 2089e3f2c991SKeyur Desai qs, req->id1.idmap_id_u.uid, is_user, 2090e3f2c991SKeyur Desai &how->idmap_how_u.ad.dn, 2091e3f2c991SKeyur Desai &how->idmap_how_u.ad.attr, 2092e3f2c991SKeyur Desai &how->idmap_how_u.ad.value, 2093e3f2c991SKeyur Desai &res->id.idmap_id_u.sid.prefix, 2094e3f2c991SKeyur Desai &res->id.idmap_id_u.sid.rid, 2095e3f2c991SKeyur Desai &req->id2name, &req->id2domain, 2096e3f2c991SKeyur Desai (int *)&req->id2.idtype, &res->retcode); 2097e3f2c991SKeyur Desai if (retcode == IDMAP_SUCCESS) 2098e3f2c991SKeyur Desai num_queued++; 2099e8c27ec8Sbaban } else if (req->id1name != NULL) { 2100e8c27ec8Sbaban /* 21014d61c878SJulian Pullen * No SID and no winname but we've unixname. 21024d61c878SJulian Pullen * Lookup AD by unixname to get SID. 2103e8c27ec8Sbaban */ 2104e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 2105e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 2106e8c27ec8Sbaban is_wuser = 1; 2107e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 2108e8c27ec8Sbaban is_wuser = 0; 2109e8c27ec8Sbaban else 2110e8c27ec8Sbaban is_wuser = is_user; 21114d61c878SJulian Pullen 2112479ac375Sdm199847 idmap_info_free(&res->info); 211348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 211448258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 2115e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1( 2116e8c27ec8Sbaban qs, req->id1name, is_user, is_wuser, 211748258c6bSjp151216 &how->idmap_how_u.ad.dn, 211848258c6bSjp151216 &how->idmap_how_u.ad.attr, 211948258c6bSjp151216 &how->idmap_how_u.ad.value, 2120e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 2121e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 2122e8c27ec8Sbaban &req->id2name, &req->id2domain, 2123e8c27ec8Sbaban (int *)&req->id2.idtype, &res->retcode); 21244d61c878SJulian Pullen if (retcode == IDMAP_SUCCESS) 21254d61c878SJulian Pullen num_queued++; 2126e8c27ec8Sbaban } 2127cd37da74Snw141292 } 2128cd37da74Snw141292 21294d61c878SJulian Pullen if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) { 21304d61c878SJulian Pullen req->direction |= _IDMAP_F_LOOKUP_OTHER_AD; 21314d61c878SJulian Pullen retcode = IDMAP_SUCCESS; 21324d61c878SJulian Pullen } else if (retcode != IDMAP_SUCCESS) { 21334d61c878SJulian Pullen break; 21344d61c878SJulian Pullen } 21354d61c878SJulian Pullen } /* End of for loop */ 21364d61c878SJulian Pullen 213796c3a9a0Sbaban if (retcode == IDMAP_SUCCESS) { 213896c3a9a0Sbaban /* add keeps track if we added an entry to the batch */ 21394d61c878SJulian Pullen if (num_queued > 0) 21400dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 214196c3a9a0Sbaban else 214296c3a9a0Sbaban idmap_lookup_release_batch(&qs); 2143e3f2c991SKeyur Desai } else { 2144e3f2c991SKeyur Desai idmap_lookup_release_batch(&qs); 2145e3f2c991SKeyur Desai num_queued = 0; 2146e3f2c991SKeyur Desai next_request = i + 1; 214796c3a9a0Sbaban } 2148cd37da74Snw141292 21492b4a7802SBaban Kenkre if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && 21502b4a7802SBaban Kenkre retries++ < ADUTILS_DEF_NUM_RETRIES) 2151c5c4113dSnw141292 goto retry; 2152c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 2153349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 2154c5c4113dSnw141292 21554d61c878SJulian Pullen if (retcode != IDMAP_SUCCESS) { 21564d61c878SJulian Pullen /* Mark any unproccessed requests for an other AD */ 21574d61c878SJulian Pullen for (i = next_request; i < batch->idmap_mapping_batch_len; 21584d61c878SJulian Pullen i++) { 21594d61c878SJulian Pullen req = &batch->idmap_mapping_batch_val[i]; 21604d61c878SJulian Pullen req->direction |= _IDMAP_F_LOOKUP_OTHER_AD; 21614d61c878SJulian Pullen 21624d61c878SJulian Pullen } 21634d61c878SJulian Pullen } 21644d61c878SJulian Pullen 2165e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2166e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests"); 2167c5c4113dSnw141292 2168c5c4113dSnw141292 out: 2169e8c27ec8Sbaban /* 2170e8c27ec8Sbaban * This loop does the following: 2171479ac375Sdm199847 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request. 2172479ac375Sdm199847 * 2. Reset req->id2.idtype to IDMAP_NONE 2173479ac375Sdm199847 * 3. If batch_start or batch_add failed then set the status 2174479ac375Sdm199847 * of each request marked for AD lookup to that error. 21754d61c878SJulian Pullen * 4. Evaluate the type of the AD object (i.e. user or group) 21764d61c878SJulian Pullen * and update the idtype in request. 2177e8c27ec8Sbaban */ 2178e8c27ec8Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 2179e3f2c991SKeyur Desai int type; 2180e3f2c991SKeyur Desai uid_t posix_id; 2181e3f2c991SKeyur Desai 2182e8c27ec8Sbaban req = &batch->idmap_mapping_batch_val[i]; 2183e8c27ec8Sbaban type = req->id2.idtype; 2184e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 2185e3f2c991SKeyur Desai posix_id = req->id2.idmap_id_u.uid; 2186e3f2c991SKeyur Desai req->id2.idmap_id_u.uid = SENTINEL_PID; 21875e0794bcSbaban res = &result->ids.ids_val[i]; 2188e3f2c991SKeyur Desai 2189e3f2c991SKeyur Desai /* 2190e3f2c991SKeyur Desai * If it didn't need AD lookup, ignore it. 2191e3f2c991SKeyur Desai */ 2192e3f2c991SKeyur Desai if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 2193e8c27ec8Sbaban continue; 2194e8c27ec8Sbaban 2195e3f2c991SKeyur Desai /* 2196e3f2c991SKeyur Desai * If we deferred it this time, reset for the next 2197e3f2c991SKeyur Desai * AD server. 2198e3f2c991SKeyur Desai */ 2199e3f2c991SKeyur Desai if (req->direction & _IDMAP_F_LOOKUP_OTHER_AD) { 2200e3f2c991SKeyur Desai req->direction &= ~_IDMAP_F_LOOKUP_OTHER_AD; 2201e3f2c991SKeyur Desai continue; 2202e3f2c991SKeyur Desai } 2203e3f2c991SKeyur Desai 22044d61c878SJulian Pullen /* Count number processed */ 22054d61c878SJulian Pullen (*num_processed)++; 22064d61c878SJulian Pullen 2207479ac375Sdm199847 /* Reset AD lookup flag */ 2208479ac375Sdm199847 req->direction &= ~(_IDMAP_F_LOOKUP_AD); 2209479ac375Sdm199847 2210479ac375Sdm199847 /* 22114d61c878SJulian Pullen * If batch_start or batch_add failed then set the 22124d61c878SJulian Pullen * status of each request marked for AD lookup to 22134d61c878SJulian Pullen * that error. 2214479ac375Sdm199847 */ 2215e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2216e8c27ec8Sbaban res->retcode = retcode; 2217e8c27ec8Sbaban continue; 2218e8c27ec8Sbaban } 2219e8c27ec8Sbaban 222048258c6bSjp151216 if (res->retcode == IDMAP_ERR_NOTFOUND) { 222148258c6bSjp151216 /* Nothing found - remove the preset info */ 2222479ac375Sdm199847 idmap_info_free(&res->info); 222348258c6bSjp151216 } 222448258c6bSjp151216 2225e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 2226e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) 2227e8c27ec8Sbaban continue; 2228479ac375Sdm199847 /* Evaluate result type */ 2229e8c27ec8Sbaban switch (type) { 2230e8c27ec8Sbaban case _IDMAP_T_USER: 2231e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2232e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 2233e3f2c991SKeyur Desai /* 2234e3f2c991SKeyur Desai * We found a user. If we got information 2235e3f2c991SKeyur Desai * from IDMU and we were expecting a user, 2236e3f2c991SKeyur Desai * copy the id. 2237e3f2c991SKeyur Desai */ 2238e3f2c991SKeyur Desai if (posix_id != SENTINEL_PID && 2239e3f2c991SKeyur Desai res->id.idtype == IDMAP_UID) { 2240e3f2c991SKeyur Desai res->id.idmap_id_u.uid = posix_id; 2241e3f2c991SKeyur Desai res->direction = IDMAP_DIRECTION_BI; 2242e3f2c991SKeyur Desai res->info.how.map_type = 2243e3f2c991SKeyur Desai IDMAP_MAP_TYPE_IDMU; 2244e3f2c991SKeyur Desai res->info.src = IDMAP_MAP_SRC_NEW; 2245e3f2c991SKeyur Desai } 2246e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 2247e8c27ec8Sbaban break; 22484d61c878SJulian Pullen 2249e8c27ec8Sbaban case _IDMAP_T_GROUP: 2250e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2251e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 2252e3f2c991SKeyur Desai /* 2253e3f2c991SKeyur Desai * We found a group. If we got information 2254e3f2c991SKeyur Desai * from IDMU and we were expecting a group, 2255e3f2c991SKeyur Desai * copy the id. 2256e3f2c991SKeyur Desai */ 2257e3f2c991SKeyur Desai if (posix_id != SENTINEL_PID && 2258e3f2c991SKeyur Desai res->id.idtype == IDMAP_GID) { 2259e3f2c991SKeyur Desai res->id.idmap_id_u.gid = posix_id; 2260e3f2c991SKeyur Desai res->direction = IDMAP_DIRECTION_BI; 2261e3f2c991SKeyur Desai res->info.how.map_type = 2262e3f2c991SKeyur Desai IDMAP_MAP_TYPE_IDMU; 2263e3f2c991SKeyur Desai res->info.src = IDMAP_MAP_SRC_NEW; 2264e3f2c991SKeyur Desai } 2265e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 2266e8c27ec8Sbaban break; 22674d61c878SJulian Pullen 2268e8c27ec8Sbaban default: 2269e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2270e8c27ec8Sbaban break; 2271e8c27ec8Sbaban } 2272479ac375Sdm199847 if (res->retcode == IDMAP_SUCCESS && 2273479ac375Sdm199847 req->id1name != NULL && 2274479ac375Sdm199847 (req->id2name == NULL || 2275479ac375Sdm199847 res->id.idmap_id_u.uid == SENTINEL_PID) && 2276479ac375Sdm199847 NLDAP_MODE(res->id.idtype, state)) { 2277479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2278479ac375Sdm199847 state->nldap_nqueries++; 2279479ac375Sdm199847 } 2280e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 2281e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2282e8c27ec8Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) && 2283e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix == NULL && 2284e3f2c991SKeyur Desai req->id2name == NULL) /* no winname */ 2285e8c27ec8Sbaban /* 2286e3f2c991SKeyur Desai * If AD lookup by unixname or pid 22874d61c878SJulian Pullen * failed with non fatal error 22884d61c878SJulian Pullen * then clear the error (ie set 22894d61c878SJulian Pullen * res->retcode to success). 22904d61c878SJulian Pullen * This allows the next pass to 22914d61c878SJulian Pullen * process other mapping 2292479ac375Sdm199847 * mechanisms for this request. 2293e8c27ec8Sbaban */ 2294e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 2295e8c27ec8Sbaban continue; 2296e8c27ec8Sbaban } 2297479ac375Sdm199847 /* Evaluate result type */ 2298e8c27ec8Sbaban switch (type) { 2299e8c27ec8Sbaban case _IDMAP_T_USER: 2300e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2301e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 2302e8c27ec8Sbaban break; 23034d61c878SJulian Pullen 2304e8c27ec8Sbaban case _IDMAP_T_GROUP: 2305e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2306e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 2307e8c27ec8Sbaban break; 23084d61c878SJulian Pullen 2309e8c27ec8Sbaban default: 2310e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2311e8c27ec8Sbaban break; 2312e8c27ec8Sbaban } 2313e8c27ec8Sbaban } 2314e8c27ec8Sbaban } 2315e8c27ec8Sbaban 23164d61c878SJulian Pullen return (retcode); 23174d61c878SJulian Pullen } 23184d61c878SJulian Pullen 23194d61c878SJulian Pullen 23204d61c878SJulian Pullen 23214d61c878SJulian Pullen /* 23224d61c878SJulian Pullen * Batch AD lookups 23234d61c878SJulian Pullen */ 23244d61c878SJulian Pullen idmap_retcode 23254d61c878SJulian Pullen ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch, 23264d61c878SJulian Pullen idmap_ids_res *result) 23274d61c878SJulian Pullen { 23284d61c878SJulian Pullen idmap_retcode retcode; 23294d61c878SJulian Pullen int i, j; 23304d61c878SJulian Pullen idmap_mapping *req; 23314d61c878SJulian Pullen idmap_id_res *res; 23324d61c878SJulian Pullen int num_queries; 23334d61c878SJulian Pullen int num_processed; 23344d61c878SJulian Pullen 23354d61c878SJulian Pullen if (state->ad_nqueries == 0) 23364d61c878SJulian Pullen return (IDMAP_SUCCESS); 23374d61c878SJulian Pullen 23384d61c878SJulian Pullen for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 23394d61c878SJulian Pullen req = &batch->idmap_mapping_batch_val[i]; 23404d61c878SJulian Pullen res = &result->ids.ids_val[i]; 23414d61c878SJulian Pullen 23424d61c878SJulian Pullen /* Skip if not marked for AD lookup or already in error. */ 23434d61c878SJulian Pullen if (!(req->direction & _IDMAP_F_LOOKUP_AD) || 23444d61c878SJulian Pullen res->retcode != IDMAP_SUCCESS) 23454d61c878SJulian Pullen continue; 23464d61c878SJulian Pullen 23474d61c878SJulian Pullen /* Init status */ 23484d61c878SJulian Pullen res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR; 23494d61c878SJulian Pullen } 23504d61c878SJulian Pullen 23514d61c878SJulian Pullen RDLOCK_CONFIG(); 23524d61c878SJulian Pullen num_queries = state->ad_nqueries; 23534d61c878SJulian Pullen 2354e3f2c991SKeyur Desai if (_idmapdstate.num_gcs == 0 && _idmapdstate.num_dcs == 0) { 23554d61c878SJulian Pullen /* Case of no ADs */ 23564d61c878SJulian Pullen retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY; 23574d61c878SJulian Pullen for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 23584d61c878SJulian Pullen req = &batch->idmap_mapping_batch_val[i]; 23594d61c878SJulian Pullen res = &result->ids.ids_val[i]; 23604d61c878SJulian Pullen if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 23614d61c878SJulian Pullen continue; 23624d61c878SJulian Pullen req->direction &= ~(_IDMAP_F_LOOKUP_AD); 23634d61c878SJulian Pullen res->retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY; 23644d61c878SJulian Pullen } 2365e3f2c991SKeyur Desai goto out; 23664d61c878SJulian Pullen } 2367e3f2c991SKeyur Desai 2368e3f2c991SKeyur Desai if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) { 2369e3f2c991SKeyur Desai for (i = 0; i < _idmapdstate.num_dcs && num_queries > 0; i++) { 2370e3f2c991SKeyur Desai 2371e3f2c991SKeyur Desai retcode = ad_lookup_batch_int(state, batch, 2372e3f2c991SKeyur Desai result, _idmapdstate.dcs[i], 2373e3f2c991SKeyur Desai i == 0 ? DOMAIN_IS_LOCAL|FOREST_IS_LOCAL : 0, 2374e3f2c991SKeyur Desai &num_processed); 2375e3f2c991SKeyur Desai num_queries -= num_processed; 2376e3f2c991SKeyur Desai 2377e3f2c991SKeyur Desai } 2378e3f2c991SKeyur Desai } 2379e3f2c991SKeyur Desai 2380e3f2c991SKeyur Desai for (i = 0; i < _idmapdstate.num_gcs && num_queries > 0; i++) { 2381e3f2c991SKeyur Desai 2382e3f2c991SKeyur Desai retcode = ad_lookup_batch_int(state, batch, result, 2383e3f2c991SKeyur Desai _idmapdstate.gcs[i], 2384e3f2c991SKeyur Desai i == 0 ? FOREST_IS_LOCAL : 0, 2385e3f2c991SKeyur Desai &num_processed); 2386e3f2c991SKeyur Desai num_queries -= num_processed; 2387e3f2c991SKeyur Desai 2388e3f2c991SKeyur Desai } 2389e3f2c991SKeyur Desai 2390e3f2c991SKeyur Desai /* 2391e3f2c991SKeyur Desai * There are no more ADs to try. Return errors for any 2392e3f2c991SKeyur Desai * remaining requests. 2393e3f2c991SKeyur Desai */ 2394e3f2c991SKeyur Desai if (num_queries > 0) { 2395e3f2c991SKeyur Desai for (j = 0; j < batch->idmap_mapping_batch_len; j++) { 2396e3f2c991SKeyur Desai req = &batch->idmap_mapping_batch_val[j]; 2397e3f2c991SKeyur Desai res = &result->ids.ids_val[j]; 2398e3f2c991SKeyur Desai if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 2399e3f2c991SKeyur Desai continue; 2400e3f2c991SKeyur Desai req->direction &= ~(_IDMAP_F_LOOKUP_AD); 2401e3f2c991SKeyur Desai res->retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 2402e3f2c991SKeyur Desai } 2403e3f2c991SKeyur Desai } 2404e3f2c991SKeyur Desai 2405e3f2c991SKeyur Desai out: 24064d61c878SJulian Pullen UNLOCK_CONFIG(); 24074d61c878SJulian Pullen 2408e8c27ec8Sbaban /* AD lookups done. Reset state->ad_nqueries and return */ 2409e8c27ec8Sbaban state->ad_nqueries = 0; 2410c5c4113dSnw141292 return (retcode); 2411c5c4113dSnw141292 } 2412c5c4113dSnw141292 2413cd37da74Snw141292 /* 2414cd37da74Snw141292 * Convention when processing win2unix requests: 2415cd37da74Snw141292 * 2416cd37da74Snw141292 * Windows identity: 2417cd37da74Snw141292 * req->id1name = 2418cd37da74Snw141292 * winname if given otherwise winname found will be placed 2419cd37da74Snw141292 * here. 2420cd37da74Snw141292 * req->id1domain = 2421cd37da74Snw141292 * windomain if given otherwise windomain found will be 2422cd37da74Snw141292 * placed here. 2423cd37da74Snw141292 * req->id1.idtype = 2424cd37da74Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll 2425cd37da74Snw141292 * be set to IDMAP_USID/GSID depending upon whether the 2426cd37da74Snw141292 * given SID is user or group respectively. The user/group-ness 2427cd37da74Snw141292 * is determined either when looking up well-known SIDs table OR 2428*fe1c642dSBill Krier * if the SID is found in namecache OR by ad_lookup_batch(). 2429cd37da74Snw141292 * req->id1..sid.[prefix, rid] = 2430cd37da74Snw141292 * SID if given otherwise SID found will be placed here. 2431cd37da74Snw141292 * 2432cd37da74Snw141292 * Unix identity: 2433cd37da74Snw141292 * req->id2name = 2434cd37da74Snw141292 * unixname found will be placed here. 2435cd37da74Snw141292 * req->id2domain = 2436cd37da74Snw141292 * NOT USED 2437cd37da74Snw141292 * res->id.idtype = 2438cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 2439cd37da74Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found 2440cd37da74Snw141292 * will be placed here. 2441cd37da74Snw141292 * res->id..[uid or gid] = 2442cd37da74Snw141292 * UID/GID found will be placed here. 2443cd37da74Snw141292 * 2444cd37da74Snw141292 * Others: 2445cd37da74Snw141292 * res->retcode = 2446cd37da74Snw141292 * Return status for this request will be placed here. 2447cd37da74Snw141292 * res->direction = 2448cd37da74Snw141292 * Direction found will be placed here. Direction 2449cd37da74Snw141292 * meaning whether the resultant mapping is valid 2450cd37da74Snw141292 * only from win2unix or bi-directional. 2451cd37da74Snw141292 * req->direction = 2452cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 2453cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 2454cd37da74Snw141292 * of the request. 2455cd37da74Snw141292 * req->id2.idtype = 2456cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 2457cd37da74Snw141292 * type and is used to initialize res->id.idtype. 2458cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 2459cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 2460cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 2461cd37da74Snw141292 * mis-interpreting the contents of req->id2. 2462e3f2c991SKeyur Desai * req->id2.idmap_id_u.uid = 2463e3f2c991SKeyur Desai * INTERNAL USE. If the AD lookup finds IDMU data 2464e3f2c991SKeyur Desai * (uidNumber or gidNumber, depending on the type of 2465e3f2c991SKeyur Desai * the entry), it's left here. 2466cd37da74Snw141292 */ 2467cd37da74Snw141292 2468cd37da74Snw141292 /* 2469cd37da74Snw141292 * This function does the following: 2470cd37da74Snw141292 * 1. Lookup well-known SIDs table. 2471cd37da74Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it. 2472cd37da74Snw141292 * 3. Lookup cache. 2473cd37da74Snw141292 * 4. Check if the client does not want new mapping to be allocated 2474cd37da74Snw141292 * in which case this pass is the final pass. 2475cd37da74Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs 2476cd37da74Snw141292 * to do AD lookup. 2477cd37da74Snw141292 */ 2478c5c4113dSnw141292 idmap_retcode 2479479ac375Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req, 2480cd37da74Snw141292 idmap_id_res *res) 2481cd37da74Snw141292 { 2482c5c4113dSnw141292 idmap_retcode retcode; 2483e8c27ec8Sbaban int wksid; 2484c5c4113dSnw141292 2485e8c27ec8Sbaban /* Initialize result */ 2486e8c27ec8Sbaban res->id.idtype = req->id2.idtype; 2487e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2488e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 2489e8c27ec8Sbaban wksid = 0; 2490c5c4113dSnw141292 2491cf5b5989Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) { 2492*fe1c642dSBill Krier /* They have to give us *something* to work with! */ 2493e8c27ec8Sbaban if (req->id1name == NULL) { 2494e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 2495c5c4113dSnw141292 goto out; 2496c5c4113dSnw141292 } 2497*fe1c642dSBill Krier 2498e8c27ec8Sbaban /* sanitize sidprefix */ 2499e8c27ec8Sbaban free(req->id1.idmap_id_u.sid.prefix); 2500e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = NULL; 2501*fe1c642dSBill Krier 2502*fe1c642dSBill Krier /* Allow for a fully-qualified name in the "name" parameter */ 2503*fe1c642dSBill Krier if (req->id1domain == NULL) { 2504*fe1c642dSBill Krier char *p; 2505*fe1c642dSBill Krier p = strchr(req->id1name, '@'); 2506*fe1c642dSBill Krier if (p != NULL) { 2507*fe1c642dSBill Krier char *q; 2508*fe1c642dSBill Krier q = req->id1name; 2509*fe1c642dSBill Krier req->id1name = strndup(q, p - req->id1name); 2510*fe1c642dSBill Krier req->id1domain = strdup(p+1); 2511*fe1c642dSBill Krier free(q); 2512*fe1c642dSBill Krier if (req->id1name == NULL || 2513*fe1c642dSBill Krier req->id1domain == NULL) { 2514*fe1c642dSBill Krier retcode = IDMAP_ERR_MEMORY; 2515*fe1c642dSBill Krier goto out; 2516*fe1c642dSBill Krier } 2517*fe1c642dSBill Krier } 2518*fe1c642dSBill Krier } 2519e8c27ec8Sbaban } 2520c5c4113dSnw141292 2521e8c27ec8Sbaban /* Lookup well-known SIDs table */ 2522e8c27ec8Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid); 2523*fe1c642dSBill Krier /* 2524*fe1c642dSBill Krier * Note that IDMAP_SUCCESS means that we found a hardwired mapping. 2525*fe1c642dSBill Krier * If we found a well-known identity but no mapping, wksid==true and 2526*fe1c642dSBill Krier * retcode==IDMAP_ERR_NOTFOUND. 2527*fe1c642dSBill Krier */ 2528c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2529c5c4113dSnw141292 goto out; 2530c5c4113dSnw141292 2531e8c27ec8Sbaban if (!wksid) { 25322b4a7802SBaban Kenkre /* Check if this is a localsid */ 2533e8c27ec8Sbaban retcode = lookup_localsid2pid(req, res); 2534e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2535e8c27ec8Sbaban goto out; 25362b4a7802SBaban Kenkre 25372b4a7802SBaban Kenkre if (ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)) { 25384d61c878SJulian Pullen retcode = IDMAP_ERR_NONE_GENERATED; 25392b4a7802SBaban Kenkre goto out; 25402b4a7802SBaban Kenkre } 2541e8c27ec8Sbaban } 2542e8c27ec8Sbaban 2543*fe1c642dSBill Krier /* 2544*fe1c642dSBill Krier * If this is a name-based request and we don't have a domain, 2545*fe1c642dSBill Krier * use the default domain. Note that the well-known identity 2546*fe1c642dSBill Krier * cases will have supplied a SID prefix already, and that we 2547*fe1c642dSBill Krier * don't (yet?) support looking up a local user through a Windows 2548*fe1c642dSBill Krier * style name. 2549*fe1c642dSBill Krier */ 2550*fe1c642dSBill Krier if (req->id1.idmap_id_u.sid.prefix == NULL && 2551*fe1c642dSBill Krier req->id1name != NULL && req->id1domain == NULL) { 2552*fe1c642dSBill Krier if (state->defdom == NULL) { 2553*fe1c642dSBill Krier retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 2554*fe1c642dSBill Krier goto out; 2555*fe1c642dSBill Krier } 2556*fe1c642dSBill Krier req->id1domain = strdup(state->defdom); 2557*fe1c642dSBill Krier if (req->id1domain == NULL) { 2558*fe1c642dSBill Krier retcode = IDMAP_ERR_MEMORY; 2559*fe1c642dSBill Krier goto out; 2560*fe1c642dSBill Krier } 2561*fe1c642dSBill Krier } 2562*fe1c642dSBill Krier 2563e8c27ec8Sbaban /* Lookup cache */ 2564479ac375Sdm199847 retcode = lookup_cache_sid2pid(state->cache, req, res); 2565c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2566c5c4113dSnw141292 goto out; 2567c5c4113dSnw141292 2568c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 25694d61c878SJulian Pullen retcode = IDMAP_ERR_NONE_GENERATED; 2570c5c4113dSnw141292 goto out; 2571c5c4113dSnw141292 } 2572c5c4113dSnw141292 2573c5c4113dSnw141292 /* 2574e8c27ec8Sbaban * Failed to find non-expired entry in cache. Next step is 2575e8c27ec8Sbaban * to determine if this request needs to be batched for AD lookup. 2576e8c27ec8Sbaban * 2577e8c27ec8Sbaban * At this point we have either sid or winname or both. If we don't 2578e8c27ec8Sbaban * have both then lookup name_cache for the sid or winname 2579e8c27ec8Sbaban * whichever is missing. If not found then this request will be 2580e8c27ec8Sbaban * batched for AD lookup. 2581e8c27ec8Sbaban */ 2582479ac375Sdm199847 retcode = lookup_name_cache(state->cache, req, res); 2583e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS && retcode != IDMAP_ERR_NOTFOUND) 2584e8c27ec8Sbaban goto out; 2585e8c27ec8Sbaban 2586e8c27ec8Sbaban /* 2587e8c27ec8Sbaban * Set the flag to indicate that we are not done yet so that 2588e8c27ec8Sbaban * subsequent passes considers this request for name-based 2589e8c27ec8Sbaban * mapping and ephemeral mapping. 2590c5c4113dSnw141292 */ 2591c5c4113dSnw141292 state->sid2pid_done = FALSE; 2592e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 2593c5c4113dSnw141292 2594c5c4113dSnw141292 /* 2595e8c27ec8Sbaban * Even if we have both sid and winname, we still may need to batch 2596e8c27ec8Sbaban * this request for AD lookup if we don't have unixname and 2597e8c27ec8Sbaban * directory-based name mapping (AD or mixed) is enabled. 2598e8c27ec8Sbaban * We avoid AD lookup for well-known SIDs because they don't have 2599e8c27ec8Sbaban * regular AD objects. 2600c5c4113dSnw141292 */ 2601e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS || 2602e8c27ec8Sbaban (!wksid && req->id2name == NULL && 2603e3f2c991SKeyur Desai AD_OR_MIXED_MODE(res->id.idtype, state)) || 2604e3f2c991SKeyur Desai (!wksid && res->id.idmap_id_u.uid == SENTINEL_PID && 2605e3f2c991SKeyur Desai state->directory_based_mapping == DIRECTORY_MAPPING_IDMU)) { 2606c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 2607e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 2608c5c4113dSnw141292 state->ad_nqueries++; 2609479ac375Sdm199847 } else if (NLDAP_MODE(res->id.idtype, state)) { 2610479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2611479ac375Sdm199847 state->nldap_nqueries++; 2612c5c4113dSnw141292 } 2613c5c4113dSnw141292 2614c5c4113dSnw141292 2615c5c4113dSnw141292 out: 2616c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2617e8c27ec8Sbaban /* 2618e8c27ec8Sbaban * If we are done and there was an error then set fallback pid 2619e8c27ec8Sbaban * in the result. 2620e8c27ec8Sbaban */ 2621e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 2622e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2623c5c4113dSnw141292 return (retcode); 2624c5c4113dSnw141292 } 2625c5c4113dSnw141292 2626c5c4113dSnw141292 /* 2627c5c4113dSnw141292 * Generate SID using the following convention 2628c5c4113dSnw141292 * <machine-sid-prefix>-<1000 + uid> 2629c5c4113dSnw141292 * <machine-sid-prefix>-<2^31 + gid> 2630c5c4113dSnw141292 */ 2631cd37da74Snw141292 static 2632cd37da74Snw141292 idmap_retcode 263348258c6bSjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user, 263448258c6bSjp151216 int fallback) 2635cd37da74Snw141292 { 2636e8c27ec8Sbaban free(res->id.idmap_id_u.sid.prefix); 2637e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix = NULL; 2638e8c27ec8Sbaban 2639e8c27ec8Sbaban /* 2640e8c27ec8Sbaban * Diagonal mapping for localSIDs not supported because of the 2641e8c27ec8Sbaban * way we generate localSIDs. 2642e8c27ec8Sbaban */ 2643e8c27ec8Sbaban if (is_user && res->id.idtype == IDMAP_GSID) 2644a0aa776eSAlan Wright return (IDMAP_ERR_NOTGROUP); 2645e8c27ec8Sbaban if (!is_user && res->id.idtype == IDMAP_USID) 2646a0aa776eSAlan Wright return (IDMAP_ERR_NOTUSER); 2647e8c27ec8Sbaban 2648c5c4113dSnw141292 /* Skip 1000 UIDs */ 26491fcced4cSJordan Brown if (is_user && 26501fcced4cSJordan Brown req->id1.idmap_id_u.uid + LOCALRID_UID_MIN > LOCALRID_UID_MAX) 265162c60062Sbaban return (IDMAP_ERR_NOMAPPING); 2652c5c4113dSnw141292 2653c5c4113dSnw141292 RDLOCK_CONFIG(); 2654e8c27ec8Sbaban /* 2655e8c27ec8Sbaban * machine_sid is never NULL because if it is we won't be here. 2656e8c27ec8Sbaban * No need to assert because stdrup(NULL) will core anyways. 2657e8c27ec8Sbaban */ 2658c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 2659c5c4113dSnw141292 strdup(_idmapdstate.cfg->pgcfg.machine_sid); 2660c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 2661c5c4113dSnw141292 UNLOCK_CONFIG(); 2662c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2663c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 2664c5c4113dSnw141292 } 2665c5c4113dSnw141292 UNLOCK_CONFIG(); 2666c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 26671fcced4cSJordan Brown (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_UID_MIN : 26681fcced4cSJordan Brown req->id1.idmap_id_u.gid + LOCALRID_GID_MIN; 2669651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 2670e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2671e8c27ec8Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 2672c5c4113dSnw141292 2673fc724630SAlan Wright if (!fallback) { 267448258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 267548258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 267648258c6bSjp151216 } 267748258c6bSjp151216 2678c5c4113dSnw141292 /* 2679c5c4113dSnw141292 * Don't update name_cache because local sids don't have 2680c5c4113dSnw141292 * valid windows names. 2681c5c4113dSnw141292 */ 2682e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 2683947c7bc0Sbaban return (IDMAP_SUCCESS); 2684c5c4113dSnw141292 } 2685c5c4113dSnw141292 2686cd37da74Snw141292 static 2687cd37da74Snw141292 idmap_retcode 2688cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res) 2689cd37da74Snw141292 { 2690c5c4113dSnw141292 char *sidprefix; 2691c5c4113dSnw141292 uint32_t rid; 2692c5c4113dSnw141292 int s; 2693c5c4113dSnw141292 2694c5c4113dSnw141292 /* 2695c5c4113dSnw141292 * If the sidprefix == localsid then UID = last RID - 1000 or 2696c5c4113dSnw141292 * GID = last RID - 2^31. 2697c5c4113dSnw141292 */ 2698e8c27ec8Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL) 2699e8c27ec8Sbaban /* This means we are looking up by winname */ 2700e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2701c5c4113dSnw141292 rid = req->id1.idmap_id_u.sid.rid; 2702c5c4113dSnw141292 2703c5c4113dSnw141292 RDLOCK_CONFIG(); 2704c5c4113dSnw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ? 2705cd37da74Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1; 2706c5c4113dSnw141292 UNLOCK_CONFIG(); 2707c5c4113dSnw141292 2708e8c27ec8Sbaban /* 2709e8c27ec8Sbaban * If the given sidprefix does not match machine_sid then this is 2710e8c27ec8Sbaban * not a local SID. 2711e8c27ec8Sbaban */ 2712e8c27ec8Sbaban if (s != 0) 2713e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2714e8c27ec8Sbaban 2715e8c27ec8Sbaban switch (res->id.idtype) { 2716c5c4113dSnw141292 case IDMAP_UID: 27171fcced4cSJordan Brown if (rid < LOCALRID_UID_MIN || rid > LOCALRID_UID_MAX) 2718e8c27ec8Sbaban return (IDMAP_ERR_ARG); 27191fcced4cSJordan Brown res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN; 2720c5c4113dSnw141292 break; 2721c5c4113dSnw141292 case IDMAP_GID: 27221fcced4cSJordan Brown if (rid < LOCALRID_GID_MIN) 2723e8c27ec8Sbaban return (IDMAP_ERR_ARG); 27241fcced4cSJordan Brown res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN; 2725c5c4113dSnw141292 break; 2726c5c4113dSnw141292 case IDMAP_POSIXID: 27271fcced4cSJordan Brown if (rid >= LOCALRID_GID_MIN) { 27281fcced4cSJordan Brown res->id.idmap_id_u.gid = rid - LOCALRID_GID_MIN; 2729c5c4113dSnw141292 res->id.idtype = IDMAP_GID; 27301fcced4cSJordan Brown } else if (rid >= LOCALRID_UID_MIN) { 27311fcced4cSJordan Brown res->id.idmap_id_u.uid = rid - LOCALRID_UID_MIN; 2732c5c4113dSnw141292 res->id.idtype = IDMAP_UID; 27331fcced4cSJordan Brown } else { 27341fcced4cSJordan Brown return (IDMAP_ERR_ARG); 2735c5c4113dSnw141292 } 2736c5c4113dSnw141292 break; 2737c5c4113dSnw141292 default: 2738c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 2739c5c4113dSnw141292 } 274048258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 274148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 2742c5c4113dSnw141292 return (IDMAP_SUCCESS); 2743c5c4113dSnw141292 } 2744c5c4113dSnw141292 2745e8c27ec8Sbaban /* 2746e8c27ec8Sbaban * Name service lookup by unixname to get pid 2747e8c27ec8Sbaban */ 2748cd37da74Snw141292 static 2749cd37da74Snw141292 idmap_retcode 2750e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id) 2751cd37da74Snw141292 { 2752cd37da74Snw141292 struct passwd pwd, *pwdp; 2753cd37da74Snw141292 struct group grp, *grpp; 27548c155366SJordan Brown char *buf; 27558c155366SJordan Brown static size_t pwdbufsiz = 0; 27568c155366SJordan Brown static size_t grpbufsiz = 0; 2757c5c4113dSnw141292 2758e8c27ec8Sbaban switch (id->idtype) { 2759e8c27ec8Sbaban case IDMAP_UID: 27608c155366SJordan Brown if (pwdbufsiz == 0) 27618c155366SJordan Brown pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX); 27628c155366SJordan Brown buf = alloca(pwdbufsiz); 27638c155366SJordan Brown pwdp = getpwnam_r(name, &pwd, buf, pwdbufsiz); 2764e8c27ec8Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL && 2765cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 27668c155366SJordan Brown pwdp = getpwnam_r(lower_name, &pwd, buf, pwdbufsiz); 2767cd37da74Snw141292 if (pwdp == NULL) { 2768bbf6f00cSJordan Brown if (errno == 0) 2769c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2770c5c4113dSnw141292 else 2771c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2772c5c4113dSnw141292 } 2773e8c27ec8Sbaban id->idmap_id_u.uid = pwd.pw_uid; 2774e8c27ec8Sbaban break; 2775e8c27ec8Sbaban case IDMAP_GID: 27768c155366SJordan Brown if (grpbufsiz == 0) 27778c155366SJordan Brown grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX); 27788c155366SJordan Brown buf = alloca(grpbufsiz); 27798c155366SJordan Brown grpp = getgrnam_r(name, &grp, buf, grpbufsiz); 2780e8c27ec8Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL && 2781cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 27828c155366SJordan Brown grpp = getgrnam_r(lower_name, &grp, buf, grpbufsiz); 2783cd37da74Snw141292 if (grpp == NULL) { 2784bbf6f00cSJordan Brown if (errno == 0) 2785c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2786c5c4113dSnw141292 else 2787c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2788c5c4113dSnw141292 } 2789e8c27ec8Sbaban id->idmap_id_u.gid = grp.gr_gid; 2790e8c27ec8Sbaban break; 2791e8c27ec8Sbaban default: 2792e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2793c5c4113dSnw141292 } 2794c5c4113dSnw141292 return (IDMAP_SUCCESS); 2795c5c4113dSnw141292 } 2796c5c4113dSnw141292 2797e8c27ec8Sbaban 2798e8c27ec8Sbaban /* 2799e8c27ec8Sbaban * Name service lookup by pid to get unixname 2800e8c27ec8Sbaban */ 2801e8c27ec8Sbaban static 2802e8c27ec8Sbaban idmap_retcode 2803e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname) 2804e8c27ec8Sbaban { 2805e8c27ec8Sbaban struct passwd pwd; 2806e8c27ec8Sbaban struct group grp; 28078c155366SJordan Brown char *buf; 28088c155366SJordan Brown static size_t pwdbufsiz = 0; 28098c155366SJordan Brown static size_t grpbufsiz = 0; 2810e8c27ec8Sbaban 2811e8c27ec8Sbaban if (is_user) { 28128c155366SJordan Brown if (pwdbufsiz == 0) 28138c155366SJordan Brown pwdbufsiz = sysconf(_SC_GETPW_R_SIZE_MAX); 28148c155366SJordan Brown buf = alloca(pwdbufsiz); 2815e8c27ec8Sbaban errno = 0; 28168c155366SJordan Brown if (getpwuid_r(pid, &pwd, buf, pwdbufsiz) == NULL) { 2817bbf6f00cSJordan Brown if (errno == 0) 2818e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2819e8c27ec8Sbaban else 2820e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2821e8c27ec8Sbaban } 2822e8c27ec8Sbaban *unixname = strdup(pwd.pw_name); 2823e8c27ec8Sbaban } else { 28248c155366SJordan Brown if (grpbufsiz == 0) 28258c155366SJordan Brown grpbufsiz = sysconf(_SC_GETGR_R_SIZE_MAX); 28268c155366SJordan Brown buf = alloca(grpbufsiz); 2827e8c27ec8Sbaban errno = 0; 28288c155366SJordan Brown if (getgrgid_r(pid, &grp, buf, grpbufsiz) == NULL) { 2829bbf6f00cSJordan Brown if (errno == 0) 2830e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2831e8c27ec8Sbaban else 2832e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2833e8c27ec8Sbaban } 2834e8c27ec8Sbaban *unixname = strdup(grp.gr_name); 2835e8c27ec8Sbaban } 2836e8c27ec8Sbaban if (*unixname == NULL) 2837e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 2838e8c27ec8Sbaban return (IDMAP_SUCCESS); 2839e8c27ec8Sbaban } 2840e8c27ec8Sbaban 2841c5c4113dSnw141292 /* 2842c5c4113dSnw141292 * Name-based mapping 2843c5c4113dSnw141292 * 2844c5c4113dSnw141292 * Case 1: If no rule matches do ephemeral 2845c5c4113dSnw141292 * 2846c5c4113dSnw141292 * Case 2: If rule matches and unixname is "" then return no mapping. 2847c5c4113dSnw141292 * 2848c5c4113dSnw141292 * Case 3: If rule matches and unixname is specified then lookup name 2849c5c4113dSnw141292 * service using the unixname. If unixname not found then return no mapping. 2850c5c4113dSnw141292 * 2851c5c4113dSnw141292 * Case 4: If rule matches and unixname is * then lookup name service 2852c5c4113dSnw141292 * using winname as the unixname. If unixname not found then process 2853c5c4113dSnw141292 * other rules using the lookup order. If no other rule matches then do 2854c5c4113dSnw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4. 2855c5c4113dSnw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping 2856c5c4113dSnw141292 * instead of the default behaviour of doing ephemeral mapping. 2857c5c4113dSnw141292 * 2858c5c4113dSnw141292 * Example 1: 2859c5c4113dSnw141292 * *@sfbay == * 2860c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2861c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to an ephemeral id. 2862c5c4113dSnw141292 * 2863c5c4113dSnw141292 * Example 2: 2864c5c4113dSnw141292 * *@sfbay == * 2865c5c4113dSnw141292 * *@sfbay => guest 2866c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2867c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to guest. 2868c5c4113dSnw141292 * 2869c5c4113dSnw141292 * Example 3: 2870c5c4113dSnw141292 * *@sfbay == * 2871c5c4113dSnw141292 * *@sfbay => "" 2872c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2873c5c4113dSnw141292 * the name service then we will return no mapping for foo@sfbay. 2874c5c4113dSnw141292 * 2875c5c4113dSnw141292 */ 2876cd37da74Snw141292 static 2877cd37da74Snw141292 idmap_retcode 2878479ac375Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state, 2879479ac375Sdm199847 idmap_mapping *req, idmap_id_res *res) 2880cd37da74Snw141292 { 2881cd37da74Snw141292 const char *unixname, *windomain; 2882cd37da74Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL; 2883c5c4113dSnw141292 idmap_retcode retcode; 2884cd37da74Snw141292 char *end, *lower_unixname, *winname; 2885c5c4113dSnw141292 const char **values; 2886c5c4113dSnw141292 sqlite_vm *vm = NULL; 288708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States int ncol, r, is_user, is_wuser; 288848258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 288948258c6bSjp151216 int direction; 2890c5c4113dSnw141292 const char *me = "name_based_mapping_sid2pid"; 2891c5c4113dSnw141292 2892e8c27ec8Sbaban assert(req->id1name != NULL); /* We have winname */ 2893e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have unixname */ 2894e8c27ec8Sbaban 28958e228215Sdm199847 winname = req->id1name; 28968e228215Sdm199847 windomain = req->id1domain; 2897cd37da74Snw141292 2898cd37da74Snw141292 switch (req->id1.idtype) { 2899cd37da74Snw141292 case IDMAP_USID: 2900cd37da74Snw141292 is_wuser = 1; 2901cd37da74Snw141292 break; 2902cd37da74Snw141292 case IDMAP_GSID: 2903cd37da74Snw141292 is_wuser = 0; 2904cd37da74Snw141292 break; 2905cd37da74Snw141292 default: 2906e8c27ec8Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the " 2907e8c27ec8Sbaban "given Windows id is user or group.", me); 2908cd37da74Snw141292 return (IDMAP_ERR_INTERNAL); 2909cd37da74Snw141292 } 2910cd37da74Snw141292 2911e8c27ec8Sbaban switch (res->id.idtype) { 2912cd37da74Snw141292 case IDMAP_UID: 2913cd37da74Snw141292 is_user = 1; 2914cd37da74Snw141292 break; 2915cd37da74Snw141292 case IDMAP_GID: 2916cd37da74Snw141292 is_user = 0; 2917cd37da74Snw141292 break; 2918cd37da74Snw141292 case IDMAP_POSIXID: 2919cd37da74Snw141292 is_user = is_wuser; 2920cd37da74Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID; 2921cd37da74Snw141292 break; 2922cd37da74Snw141292 } 2923c5c4113dSnw141292 2924479ac375Sdm199847 if (windomain == NULL) 292562c60062Sbaban windomain = ""; 2926c5c4113dSnw141292 2927cd37da74Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL) 2928cd37da74Snw141292 lower_winname = winname; /* hope for the best */ 2929c5c4113dSnw141292 sql = sqlite_mprintf( 293048258c6bSjp151216 "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 " 293148258c6bSjp151216 "FROM namerules WHERE " 2932cd37da74Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND " 2933c5c4113dSnw141292 "(winname = %Q OR winname = '*') AND " 293408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States "(windomain = %Q OR windomain = '*') " 2935c5c4113dSnw141292 "ORDER BY w2u_order ASC;", 293608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States is_user, is_wuser, lower_winname, windomain); 2937c5c4113dSnw141292 if (sql == NULL) { 2938c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2939c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 2940c5c4113dSnw141292 goto out; 2941c5c4113dSnw141292 } 2942c5c4113dSnw141292 2943479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 2944c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2945cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2946cd37da74Snw141292 CHECK_NULL(errmsg)); 2947c5c4113dSnw141292 sqlite_freemem(errmsg); 2948c5c4113dSnw141292 goto out; 2949c5c4113dSnw141292 } 2950c5c4113dSnw141292 295184decf41Sjp151216 for (;;) { 2952c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 295384decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 2954c5c4113dSnw141292 295584decf41Sjp151216 if (r == SQLITE_ROW) { 295648258c6bSjp151216 if (ncol < 5) { 2957c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2958c5c4113dSnw141292 goto out; 2959c5c4113dSnw141292 } 2960c5c4113dSnw141292 if (values[0] == NULL) { 2961c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2962c5c4113dSnw141292 goto out; 2963c5c4113dSnw141292 } 2964c5c4113dSnw141292 296548258c6bSjp151216 if (values[1] != NULL) 296648258c6bSjp151216 direction = 296748258c6bSjp151216 (strtol(values[1], &end, 10) == 0)? 296848258c6bSjp151216 IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 296948258c6bSjp151216 else 297048258c6bSjp151216 direction = IDMAP_DIRECTION_W2U; 297148258c6bSjp151216 2972c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 297348258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 297448258c6bSjp151216 values[0], is_wuser, is_user, 297548258c6bSjp151216 strtol(values[4], &end, 10), 297648258c6bSjp151216 direction); 2977c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2978c5c4113dSnw141292 goto out; 2979c5c4113dSnw141292 } 298048258c6bSjp151216 298148258c6bSjp151216 if (values[0][0] == '*') { 298248258c6bSjp151216 unixname = winname; 298348258c6bSjp151216 lower_unixname = lower_winname; 298448258c6bSjp151216 } else { 298548258c6bSjp151216 unixname = values[0]; 298648258c6bSjp151216 lower_unixname = NULL; 298748258c6bSjp151216 } 298848258c6bSjp151216 2989e8c27ec8Sbaban retcode = ns_lookup_byname(unixname, lower_unixname, 2990e8c27ec8Sbaban &res->id); 2991c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 299248258c6bSjp151216 if (values[0][0] == '*') 2993c5c4113dSnw141292 /* Case 4 */ 2994c5c4113dSnw141292 continue; 299548258c6bSjp151216 else { 2996c5c4113dSnw141292 /* Case 3 */ 299748258c6bSjp151216 idmap_namerule_set(rule, values[3], 299848258c6bSjp151216 values[2], values[0], is_wuser, 299948258c6bSjp151216 is_user, 300048258c6bSjp151216 strtol(values[4], &end, 10), 300148258c6bSjp151216 direction); 3002c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 3003c5c4113dSnw141292 } 300448258c6bSjp151216 } 3005c5c4113dSnw141292 goto out; 3006c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 3007c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 3008c5c4113dSnw141292 goto out; 3009c5c4113dSnw141292 } else { 3010c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 3011c5c4113dSnw141292 vm = NULL; 3012cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3013cd37da74Snw141292 CHECK_NULL(errmsg)); 3014c5c4113dSnw141292 sqlite_freemem(errmsg); 3015c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3016c5c4113dSnw141292 goto out; 3017c5c4113dSnw141292 } 3018c5c4113dSnw141292 } 3019c5c4113dSnw141292 3020c5c4113dSnw141292 out: 302148258c6bSjp151216 if (sql != NULL) 3022c5c4113dSnw141292 sqlite_freemem(sql); 3023c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 302462c60062Sbaban if (values[1] != NULL) 3025c5c4113dSnw141292 res->direction = 3026651c0131Sbaban (strtol(values[1], &end, 10) == 0)? 3027651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 3028c5c4113dSnw141292 else 3029651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 303048258c6bSjp151216 30318e228215Sdm199847 req->id2name = strdup(unixname); 3032479ac375Sdm199847 if (req->id2name == NULL) { 3033479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 3034479ac375Sdm199847 } 3035479ac375Sdm199847 } 3036479ac375Sdm199847 3037479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 303848258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 303948258c6bSjp151216 values[0], is_wuser, is_user, strtol(values[4], &end, 10), 304048258c6bSjp151216 res->direction); 3041fc724630SAlan Wright } 3042fc724630SAlan Wright 3043fc724630SAlan Wright if (retcode != IDMAP_ERR_NOTFOUND) { 3044fc724630SAlan Wright res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 304548258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 3046c5c4113dSnw141292 } 3047479ac375Sdm199847 3048cd37da74Snw141292 if (lower_winname != NULL && lower_winname != winname) 3049cd37da74Snw141292 free(lower_winname); 305062c60062Sbaban if (vm != NULL) 3051c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3052c5c4113dSnw141292 return (retcode); 3053c5c4113dSnw141292 } 3054c5c4113dSnw141292 3055c5c4113dSnw141292 static 3056c5c4113dSnw141292 int 3057c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid) 3058c5c4113dSnw141292 { 3059c5c4113dSnw141292 uid_t uid; 3060c5c4113dSnw141292 gid_t gid; 3061c5c4113dSnw141292 int err; 3062c5c4113dSnw141292 3063c5c4113dSnw141292 *next_uid = (uid_t)-1; 3064c5c4113dSnw141292 uid = _idmapdstate.next_uid++; 3065c5c4113dSnw141292 if (uid >= _idmapdstate.limit_uid) { 3066c5c4113dSnw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0) 3067c5c4113dSnw141292 return (err); 3068c5c4113dSnw141292 3069c5c4113dSnw141292 _idmapdstate.limit_uid = uid + 8192; 3070c5c4113dSnw141292 _idmapdstate.next_uid = uid; 3071c5c4113dSnw141292 } 3072c5c4113dSnw141292 *next_uid = uid; 3073c5c4113dSnw141292 3074c5c4113dSnw141292 return (0); 3075c5c4113dSnw141292 } 3076c5c4113dSnw141292 3077c5c4113dSnw141292 static 3078c5c4113dSnw141292 int 3079c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid) 3080c5c4113dSnw141292 { 3081c5c4113dSnw141292 uid_t uid; 3082c5c4113dSnw141292 gid_t gid; 3083c5c4113dSnw141292 int err; 3084c5c4113dSnw141292 3085c5c4113dSnw141292 *next_gid = (uid_t)-1; 3086c5c4113dSnw141292 gid = _idmapdstate.next_gid++; 3087c5c4113dSnw141292 if (gid >= _idmapdstate.limit_gid) { 3088c5c4113dSnw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0) 3089c5c4113dSnw141292 return (err); 3090c5c4113dSnw141292 3091c5c4113dSnw141292 _idmapdstate.limit_gid = gid + 8192; 3092c5c4113dSnw141292 _idmapdstate.next_gid = gid; 3093c5c4113dSnw141292 } 3094c5c4113dSnw141292 *next_gid = gid; 3095c5c4113dSnw141292 3096c5c4113dSnw141292 return (0); 3097c5c4113dSnw141292 } 3098c5c4113dSnw141292 309962c60062Sbaban static 310062c60062Sbaban int 3101cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize) 3102cd37da74Snw141292 { 310362c60062Sbaban uint_t hval, i, len; 310462c60062Sbaban 310562c60062Sbaban if (str == NULL) 310662c60062Sbaban return (0); 310762c60062Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) { 310862c60062Sbaban hval += str[i]; 310962c60062Sbaban hval += (hval << 10); 311062c60062Sbaban hval ^= (hval >> 6); 311162c60062Sbaban } 311262c60062Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) { 311362c60062Sbaban hval += str[i]; 311462c60062Sbaban hval += (hval << 10); 311562c60062Sbaban hval ^= (hval >> 6); 311662c60062Sbaban } 311762c60062Sbaban hval += (hval << 3); 311862c60062Sbaban hval ^= (hval >> 11); 311962c60062Sbaban hval += (hval << 15); 312062c60062Sbaban return (hval % htsize); 312162c60062Sbaban } 312262c60062Sbaban 312362c60062Sbaban static 312462c60062Sbaban int 312562c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid, 3126cd37da74Snw141292 uid_t *pid) 3127cd37da74Snw141292 { 312862c60062Sbaban uint_t next, key; 312962c60062Sbaban uint_t htsize = state->sid_history_size; 313062c60062Sbaban idmap_sid *sid; 313162c60062Sbaban 313262c60062Sbaban next = gethash(prefix, rid, htsize); 313362c60062Sbaban while (next != htsize) { 313462c60062Sbaban key = state->sid_history[next].key; 313562c60062Sbaban if (key == htsize) 313662c60062Sbaban return (0); 313762c60062Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1. 313862c60062Sbaban idmap_id_u.sid; 313962c60062Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) { 314062c60062Sbaban *pid = state->result->ids.ids_val[key].id. 314162c60062Sbaban idmap_id_u.uid; 314262c60062Sbaban return (1); 314362c60062Sbaban } 314462c60062Sbaban next = state->sid_history[next].next; 314562c60062Sbaban } 314662c60062Sbaban return (0); 314762c60062Sbaban } 314862c60062Sbaban 314962c60062Sbaban static 315062c60062Sbaban void 3151cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid) 3152cd37da74Snw141292 { 315362c60062Sbaban uint_t hash, next; 315462c60062Sbaban uint_t htsize = state->sid_history_size; 315562c60062Sbaban 315662c60062Sbaban hash = next = gethash(prefix, rid, htsize); 315762c60062Sbaban while (state->sid_history[next].key != htsize) { 315862c60062Sbaban next++; 315962c60062Sbaban next %= htsize; 316062c60062Sbaban } 316162c60062Sbaban state->sid_history[next].key = state->curpos; 316262c60062Sbaban if (hash == next) 316362c60062Sbaban return; 316462c60062Sbaban state->sid_history[next].next = state->sid_history[hash].next; 316562c60062Sbaban state->sid_history[hash].next = next; 316662c60062Sbaban } 3167c5c4113dSnw141292 3168e8c27ec8Sbaban void 3169e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state) 3170e8c27ec8Sbaban { 3171e8c27ec8Sbaban free(state->sid_history); 3172e8c27ec8Sbaban free(state->ad_unixuser_attr); 3173e8c27ec8Sbaban free(state->ad_unixgroup_attr); 3174479ac375Sdm199847 free(state->nldap_winname_attr); 3175479ac375Sdm199847 free(state->defdom); 3176e8c27ec8Sbaban } 3177e8c27ec8Sbaban 3178c5c4113dSnw141292 /* ARGSUSED */ 3179c5c4113dSnw141292 static 3180c5c4113dSnw141292 idmap_retcode 3181479ac375Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state, 3182cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3183cd37da74Snw141292 { 3184c5c4113dSnw141292 3185c5c4113dSnw141292 uid_t next_pid; 3186c5c4113dSnw141292 3187651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 318862c60062Sbaban 318948258c6bSjp151216 if (IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 319048258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 319148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 319262c60062Sbaban return (IDMAP_SUCCESS); 319348258c6bSjp151216 } 319462c60062Sbaban 319562c60062Sbaban if (state->sid_history != NULL && 319662c60062Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix, 319762c60062Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) { 319862c60062Sbaban res->id.idmap_id_u.uid = next_pid; 319948258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 320048258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 320162c60062Sbaban return (IDMAP_SUCCESS); 320262c60062Sbaban } 320362c60062Sbaban 320462c60062Sbaban if (res->id.idtype == IDMAP_UID) { 3205c5c4113dSnw141292 if (get_next_eph_uid(&next_pid) != 0) 3206c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 3207c5c4113dSnw141292 res->id.idmap_id_u.uid = next_pid; 3208c5c4113dSnw141292 } else { 3209c5c4113dSnw141292 if (get_next_eph_gid(&next_pid) != 0) 3210c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 3211c5c4113dSnw141292 res->id.idmap_id_u.gid = next_pid; 3212c5c4113dSnw141292 } 3213c5c4113dSnw141292 321448258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 321548258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 321662c60062Sbaban if (state->sid_history != NULL) 321762c60062Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix, 321862c60062Sbaban req->id1.idmap_id_u.sid.rid); 321962c60062Sbaban 3220c5c4113dSnw141292 return (IDMAP_SUCCESS); 3221c5c4113dSnw141292 } 3222c5c4113dSnw141292 3223c5c4113dSnw141292 idmap_retcode 3224479ac375Sdm199847 sid2pid_second_pass(lookup_state_t *state, 3225cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3226cd37da74Snw141292 { 3227c5c4113dSnw141292 idmap_retcode retcode; 3228c5c4113dSnw141292 3229c5c4113dSnw141292 /* Check if second pass is needed */ 3230e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3231c5c4113dSnw141292 return (res->retcode); 3232c5c4113dSnw141292 3233c5c4113dSnw141292 /* Get status from previous pass */ 3234e8c27ec8Sbaban retcode = res->retcode; 32354aa0a5e7Snw141292 if (retcode != IDMAP_SUCCESS && state->eph_map_unres_sids && 32364aa0a5e7Snw141292 !EMPTY_STRING(req->id1.idmap_id_u.sid.prefix) && 32374aa0a5e7Snw141292 EMPTY_STRING(req->id1name)) { 32384aa0a5e7Snw141292 /* 32394aa0a5e7Snw141292 * We are asked to map an unresolvable SID to a UID or 32404aa0a5e7Snw141292 * GID, but, which? We'll treat all unresolvable SIDs 32414aa0a5e7Snw141292 * as users unless the caller specified which of a UID 32424aa0a5e7Snw141292 * or GID they want. 32434aa0a5e7Snw141292 */ 3244a7c8bd9fSNicolas Williams if (req->id1.idtype == IDMAP_SID) 3245a7c8bd9fSNicolas Williams req->id1.idtype = IDMAP_USID; 32464aa0a5e7Snw141292 if (res->id.idtype == IDMAP_POSIXID) 32474aa0a5e7Snw141292 res->id.idtype = IDMAP_UID; 32484aa0a5e7Snw141292 goto do_eph; 32494aa0a5e7Snw141292 } 3250e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3251e8c27ec8Sbaban goto out; 3252c5c4113dSnw141292 3253e8c27ec8Sbaban /* 3254e3f2c991SKeyur Desai * There are two ways we might get here with a Posix ID: 3255e3f2c991SKeyur Desai * - It could be from an expired ephemeral cache entry. 3256e3f2c991SKeyur Desai * - It could be from IDMU. 3257e3f2c991SKeyur Desai * If it's from IDMU, we need to look up the name, for name-based 3258e3f2c991SKeyur Desai * requests and the cache. 3259e3f2c991SKeyur Desai */ 3260e3f2c991SKeyur Desai if (!IS_EPHEMERAL(res->id.idmap_id_u.uid) && 3261e3f2c991SKeyur Desai res->id.idmap_id_u.uid != SENTINEL_PID) { 3262e3f2c991SKeyur Desai if (req->id2name == NULL) { 3263e3f2c991SKeyur Desai /* 3264e3f2c991SKeyur Desai * If the lookup fails, go ahead anyway. 3265e3f2c991SKeyur Desai * The general UNIX rule is that it's OK to 3266e3f2c991SKeyur Desai * have a UID or GID that isn't in the 3267e3f2c991SKeyur Desai * name service. 3268e3f2c991SKeyur Desai */ 3269e3f2c991SKeyur Desai (void) ns_lookup_bypid(res->id.idmap_id_u.uid, 3270e3f2c991SKeyur Desai res->id.idtype == IDMAP_UID, &req->id2name); 3271e3f2c991SKeyur Desai } 3272e3f2c991SKeyur Desai goto out; 3273e3f2c991SKeyur Desai } 3274e3f2c991SKeyur Desai 3275e3f2c991SKeyur Desai /* 3276e8c27ec8Sbaban * If directory-based name mapping is enabled then the unixname 3277e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode or 3278e8c27ec8Sbaban * mixed-mode) or from native LDAP object (nldap-mode) -- done. 3279e8c27ec8Sbaban */ 3280e8c27ec8Sbaban if (req->id2name != NULL) { 3281e8c27ec8Sbaban assert(res->id.idtype != IDMAP_POSIXID); 3282e8c27ec8Sbaban if (AD_MODE(res->id.idtype, state)) 3283e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 3284e8c27ec8Sbaban else if (NLDAP_MODE(res->id.idtype, state)) 3285e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 3286e8c27ec8Sbaban else if (MIXED_MODE(res->id.idtype, state)) 3287e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 3288c5c4113dSnw141292 3289e8c27ec8Sbaban /* 3290e8c27ec8Sbaban * Special case: (1) If the ad_unixuser_attr and 3291e8c27ec8Sbaban * ad_unixgroup_attr uses the same attribute 3292e8c27ec8Sbaban * name and (2) if this is a diagonal mapping 3293e8c27ec8Sbaban * request and (3) the unixname has been retrieved 3294e8c27ec8Sbaban * from the AD object -- then we ignore it and fallback 3295e8c27ec8Sbaban * to name-based mapping rules and ephemeral mapping 3296e8c27ec8Sbaban * 3297e8c27ec8Sbaban * Example: 3298e8c27ec8Sbaban * Properties: 3299e8c27ec8Sbaban * config/ad_unixuser_attr = "unixname" 3300e8c27ec8Sbaban * config/ad_unixgroup_attr = "unixname" 3301e8c27ec8Sbaban * AD user object: 3302e8c27ec8Sbaban * dn: cn=bob ... 3303e8c27ec8Sbaban * objectclass: user 3304e8c27ec8Sbaban * sam: bob 3305e8c27ec8Sbaban * unixname: bob1234 3306e8c27ec8Sbaban * AD group object: 3307e8c27ec8Sbaban * dn: cn=winadmins ... 3308e8c27ec8Sbaban * objectclass: group 3309e8c27ec8Sbaban * sam: winadmins 3310e8c27ec8Sbaban * unixname: unixadmins 3311e8c27ec8Sbaban * 3312e8c27ec8Sbaban * In this example whether "unixname" refers to a unixuser 3313e8c27ec8Sbaban * or unixgroup depends upon the AD object. 3314e8c27ec8Sbaban * 3315e8c27ec8Sbaban * $idmap show -c winname:bob gid 3316e8c27ec8Sbaban * AD lookup by "samAccountName=bob" for 3317e8c27ec8Sbaban * "ad_unixgroup_attr (i.e unixname)" for directory-based 3318e8c27ec8Sbaban * mapping would get "bob1234" which is not what we want. 3319e8c27ec8Sbaban * Now why not getgrnam_r("bob1234") and use it if it 3320e8c27ec8Sbaban * is indeed a unixgroup? That's because Unix can have 3321e8c27ec8Sbaban * users and groups with the same name and we clearly 3322e8c27ec8Sbaban * don't know the intention of the admin here. 3323e8c27ec8Sbaban * Therefore we ignore this and fallback to name-based 3324e8c27ec8Sbaban * mapping rules or ephemeral mapping. 3325e8c27ec8Sbaban */ 3326e8c27ec8Sbaban if ((AD_MODE(res->id.idtype, state) || 3327e8c27ec8Sbaban MIXED_MODE(res->id.idtype, state)) && 3328e8c27ec8Sbaban state->ad_unixuser_attr != NULL && 3329e8c27ec8Sbaban state->ad_unixgroup_attr != NULL && 3330e8c27ec8Sbaban strcasecmp(state->ad_unixuser_attr, 3331e8c27ec8Sbaban state->ad_unixgroup_attr) == 0 && 3332e8c27ec8Sbaban ((req->id1.idtype == IDMAP_USID && 3333e8c27ec8Sbaban res->id.idtype == IDMAP_GID) || 3334e8c27ec8Sbaban (req->id1.idtype == IDMAP_GSID && 3335e8c27ec8Sbaban res->id.idtype == IDMAP_UID))) { 3336e8c27ec8Sbaban free(req->id2name); 3337e8c27ec8Sbaban req->id2name = NULL; 3338e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 3339e8c27ec8Sbaban /* fallback */ 3340e8c27ec8Sbaban } else { 3341e8c27ec8Sbaban if (res->id.idmap_id_u.uid == SENTINEL_PID) 3342e8c27ec8Sbaban retcode = ns_lookup_byname(req->id2name, 3343e8c27ec8Sbaban NULL, &res->id); 3344e8c27ec8Sbaban /* 3345479ac375Sdm199847 * If ns_lookup_byname() fails that means the 3346479ac375Sdm199847 * unixname (req->id2name), which was obtained 3347479ac375Sdm199847 * from the AD object by directory-based mapping, 3348479ac375Sdm199847 * is not a valid Unix user/group and therefore 3349479ac375Sdm199847 * we return the error to the client instead of 3350479ac375Sdm199847 * doing rule-based mapping or ephemeral mapping. 3351479ac375Sdm199847 * This way the client can detect the issue. 3352e8c27ec8Sbaban */ 3353c5c4113dSnw141292 goto out; 3354c5c4113dSnw141292 } 3355e8c27ec8Sbaban } 3356c5c4113dSnw141292 335748258c6bSjp151216 /* Free any mapping info from Directory based mapping */ 335848258c6bSjp151216 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 335948258c6bSjp151216 idmap_info_free(&res->info); 336048258c6bSjp151216 3361e8c27ec8Sbaban /* 3362e8c27ec8Sbaban * If we don't have unixname then evaluate local name-based 3363e8c27ec8Sbaban * mapping rules. 3364e8c27ec8Sbaban */ 3365479ac375Sdm199847 retcode = name_based_mapping_sid2pid(state, req, res); 3366e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 3367e8c27ec8Sbaban goto out; 3368e8c27ec8Sbaban 33694aa0a5e7Snw141292 do_eph: 3370c5c4113dSnw141292 /* If not found, do ephemeral mapping */ 3371479ac375Sdm199847 retcode = dynamic_ephemeral_mapping(state, req, res); 3372c5c4113dSnw141292 3373c5c4113dSnw141292 out: 3374c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 3375e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 3376e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 3377e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 3378e8c27ec8Sbaban } 3379e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 3380e8c27ec8Sbaban state->sid2pid_done = FALSE; 3381c5c4113dSnw141292 return (retcode); 3382c5c4113dSnw141292 } 3383c5c4113dSnw141292 3384c5c4113dSnw141292 idmap_retcode 3385479ac375Sdm199847 update_cache_pid2sid(lookup_state_t *state, 3386cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3387cd37da74Snw141292 { 3388c5c4113dSnw141292 char *sql = NULL; 3389c5c4113dSnw141292 idmap_retcode retcode; 339048258c6bSjp151216 char *map_dn = NULL; 339148258c6bSjp151216 char *map_attr = NULL; 339248258c6bSjp151216 char *map_value = NULL; 339348258c6bSjp151216 char *map_windomain = NULL; 339448258c6bSjp151216 char *map_winname = NULL; 339548258c6bSjp151216 char *map_unixname = NULL; 339648258c6bSjp151216 int map_is_nt4 = FALSE; 3397c5c4113dSnw141292 3398c5c4113dSnw141292 /* Check if we need to cache anything */ 3399e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3400c5c4113dSnw141292 return (IDMAP_SUCCESS); 3401c5c4113dSnw141292 3402c5c4113dSnw141292 /* We don't cache negative entries */ 3403c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3404c5c4113dSnw141292 return (IDMAP_SUCCESS); 3405c5c4113dSnw141292 3406e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 340748258c6bSjp151216 assert(req->id1.idmap_id_u.uid != SENTINEL_PID); 340848258c6bSjp151216 assert(res->id.idtype != IDMAP_SID); 340948258c6bSjp151216 3410e3f2c991SKeyur Desai /* 3411e3f2c991SKeyur Desai * If we've gotten to this point and we *still* don't know the 3412e3f2c991SKeyur Desai * unixname, well, we'd like to have it now for the cache. 3413e3f2c991SKeyur Desai * 3414e3f2c991SKeyur Desai * If we truly always need it for the cache, we should probably 3415e3f2c991SKeyur Desai * look it up once at the beginning, rather than "at need" in 3416e3f2c991SKeyur Desai * several places as is now done. However, it's not really clear 3417e3f2c991SKeyur Desai * that we *do* need it in the cache; there's a decent argument 3418e3f2c991SKeyur Desai * that the cache should contain only SIDs and PIDs, so we'll 3419e3f2c991SKeyur Desai * leave our options open by doing it "at need" here too. 3420e3f2c991SKeyur Desai * 3421e3f2c991SKeyur Desai * If we can't find it... c'est la vie. 3422e3f2c991SKeyur Desai */ 3423e3f2c991SKeyur Desai if (req->id1name == NULL) { 3424e3f2c991SKeyur Desai (void) ns_lookup_bypid(req->id1.idmap_id_u.uid, 3425e3f2c991SKeyur Desai req->id1.idtype == IDMAP_UID, &req->id1name); 3426e3f2c991SKeyur Desai } 3427e3f2c991SKeyur Desai 342848258c6bSjp151216 assert(res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN); 342948258c6bSjp151216 switch (res->info.how.map_type) { 343048258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 343148258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 343248258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 343348258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 343448258c6bSjp151216 break; 343548258c6bSjp151216 343648258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 343748258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 343848258c6bSjp151216 map_attr = res->info.how.idmap_how_u.nldap.attr; 343948258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 344048258c6bSjp151216 break; 344148258c6bSjp151216 344248258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 344348258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 344448258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 344548258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 344648258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 344748258c6bSjp151216 break; 344848258c6bSjp151216 344948258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 345048258c6bSjp151216 break; 345148258c6bSjp151216 345248258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 345348258c6bSjp151216 break; 345448258c6bSjp151216 3455e3f2c991SKeyur Desai case IDMAP_MAP_TYPE_IDMU: 3456e3f2c991SKeyur Desai map_dn = res->info.how.idmap_how_u.idmu.dn; 3457e3f2c991SKeyur Desai map_attr = res->info.how.idmap_how_u.idmu.attr; 3458e3f2c991SKeyur Desai map_value = res->info.how.idmap_how_u.idmu.value; 3459e3f2c991SKeyur Desai break; 3460e3f2c991SKeyur Desai 346148258c6bSjp151216 default: 346248258c6bSjp151216 /* Dont cache other mapping types */ 346348258c6bSjp151216 assert(FALSE); 346448258c6bSjp151216 } 3465e8c27ec8Sbaban 3466c5c4113dSnw141292 /* 3467c5c4113dSnw141292 * Using NULL for u2w instead of 0 so that our trigger allows 3468c5c4113dSnw141292 * the same pid to be the destination in multiple entries 3469c5c4113dSnw141292 */ 3470c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3471cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 347248258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 347348258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 347448258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3475cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 347648258c6bSjp151216 "strftime('%%s','now') + 600, %q, 1, " 347748258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ", 3478cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3479cd37da74Snw141292 req->id2domain, req->id2name, req->id1.idmap_id_u.uid, 3480cd37da74Snw141292 req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0, 3481e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? 1 : 0, 348248258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 348348258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 348448258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3485c5c4113dSnw141292 3486c5c4113dSnw141292 if (sql == NULL) { 3487c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3488c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3489c5c4113dSnw141292 goto out; 3490c5c4113dSnw141292 } 3491c5c4113dSnw141292 3492479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3493c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3494c5c4113dSnw141292 goto out; 3495c5c4113dSnw141292 3496c5c4113dSnw141292 state->pid2sid_done = FALSE; 3497c5c4113dSnw141292 sqlite_freemem(sql); 3498c5c4113dSnw141292 sql = NULL; 3499c5c4113dSnw141292 3500e8c27ec8Sbaban /* Check if we need to update namecache */ 3501e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3502c5c4113dSnw141292 goto out; 3503c5c4113dSnw141292 35048e228215Sdm199847 if (req->id2name == NULL) 3505c5c4113dSnw141292 goto out; 3506c5c4113dSnw141292 3507c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3508cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3509c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3510cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3511cd37da74Snw141292 req->id2name, req->id2domain, 3512e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3513c5c4113dSnw141292 3514c5c4113dSnw141292 if (sql == NULL) { 3515c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3516c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3517c5c4113dSnw141292 goto out; 3518c5c4113dSnw141292 } 3519c5c4113dSnw141292 3520479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3521c5c4113dSnw141292 3522c5c4113dSnw141292 out: 352348258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 352448258c6bSjp151216 idmap_info_free(&res->info); 352562c60062Sbaban if (sql != NULL) 3526c5c4113dSnw141292 sqlite_freemem(sql); 3527c5c4113dSnw141292 return (retcode); 3528c5c4113dSnw141292 } 3529c5c4113dSnw141292 3530c5c4113dSnw141292 idmap_retcode 3531479ac375Sdm199847 update_cache_sid2pid(lookup_state_t *state, 3532cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3533cd37da74Snw141292 { 3534c5c4113dSnw141292 char *sql = NULL; 3535c5c4113dSnw141292 idmap_retcode retcode; 3536c5c4113dSnw141292 int is_eph_user; 353748258c6bSjp151216 char *map_dn = NULL; 353848258c6bSjp151216 char *map_attr = NULL; 353948258c6bSjp151216 char *map_value = NULL; 354048258c6bSjp151216 char *map_windomain = NULL; 354148258c6bSjp151216 char *map_winname = NULL; 354248258c6bSjp151216 char *map_unixname = NULL; 354348258c6bSjp151216 int map_is_nt4 = FALSE; 3544c5c4113dSnw141292 3545c5c4113dSnw141292 /* Check if we need to cache anything */ 3546e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3547c5c4113dSnw141292 return (IDMAP_SUCCESS); 3548c5c4113dSnw141292 3549c5c4113dSnw141292 /* We don't cache negative entries */ 3550c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3551c5c4113dSnw141292 return (IDMAP_SUCCESS); 3552c5c4113dSnw141292 3553c5c4113dSnw141292 if (req->direction & _IDMAP_F_EXP_EPH_UID) 3554c5c4113dSnw141292 is_eph_user = 1; 3555c5c4113dSnw141292 else if (req->direction & _IDMAP_F_EXP_EPH_GID) 3556c5c4113dSnw141292 is_eph_user = 0; 3557c5c4113dSnw141292 else 3558c5c4113dSnw141292 is_eph_user = -1; 3559c5c4113dSnw141292 3560c5c4113dSnw141292 if (is_eph_user >= 0 && !IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 3561c5c4113dSnw141292 sql = sqlite_mprintf("UPDATE idmap_cache " 3562c5c4113dSnw141292 "SET w2u = 0 WHERE " 3563c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 3564c5c4113dSnw141292 "pid >= 2147483648 AND is_user = %d;", 3565c5c4113dSnw141292 req->id1.idmap_id_u.sid.prefix, 3566c5c4113dSnw141292 req->id1.idmap_id_u.sid.rid, 3567c5c4113dSnw141292 is_eph_user); 3568c5c4113dSnw141292 if (sql == NULL) { 3569c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3570c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3571c5c4113dSnw141292 goto out; 3572c5c4113dSnw141292 } 3573c5c4113dSnw141292 3574479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3575c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3576c5c4113dSnw141292 goto out; 3577c5c4113dSnw141292 3578c5c4113dSnw141292 sqlite_freemem(sql); 3579c5c4113dSnw141292 sql = NULL; 3580c5c4113dSnw141292 } 3581c5c4113dSnw141292 3582e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 358348258c6bSjp151216 assert(res->id.idmap_id_u.uid != SENTINEL_PID); 358448258c6bSjp151216 358548258c6bSjp151216 switch (res->info.how.map_type) { 358648258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 358748258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 358848258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 358948258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 359048258c6bSjp151216 break; 359148258c6bSjp151216 359248258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 359348258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 359448258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 359548258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 359648258c6bSjp151216 break; 359748258c6bSjp151216 359848258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 359948258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 360048258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 360148258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 360248258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 360348258c6bSjp151216 break; 360448258c6bSjp151216 360548258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 360648258c6bSjp151216 break; 360748258c6bSjp151216 3608e3f2c991SKeyur Desai case IDMAP_MAP_TYPE_IDMU: 3609e3f2c991SKeyur Desai map_dn = res->info.how.idmap_how_u.idmu.dn; 3610e3f2c991SKeyur Desai map_attr = res->info.how.idmap_how_u.idmu.attr; 3611e3f2c991SKeyur Desai map_value = res->info.how.idmap_how_u.idmu.value; 3612e3f2c991SKeyur Desai break; 3613e3f2c991SKeyur Desai 361448258c6bSjp151216 default: 361548258c6bSjp151216 /* Dont cache other mapping types */ 361648258c6bSjp151216 assert(FALSE); 361748258c6bSjp151216 } 3618cd37da74Snw141292 3619c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3620cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 362148258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 362248258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 362348258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3624cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 362548258c6bSjp151216 "strftime('%%s','now') + 600, 1, %q, " 362648258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);", 3627cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3628e8c27ec8Sbaban (req->id1domain != NULL) ? req->id1domain : "", req->id1name, 3629e8c27ec8Sbaban res->id.idmap_id_u.uid, req->id2name, 3630e8c27ec8Sbaban (res->id.idtype == IDMAP_UID) ? 1 : 0, 3631cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? 1 : 0, 363248258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 363348258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 363448258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3635c5c4113dSnw141292 3636c5c4113dSnw141292 if (sql == NULL) { 3637c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3638c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3639c5c4113dSnw141292 goto out; 3640c5c4113dSnw141292 } 3641c5c4113dSnw141292 3642479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3643c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3644c5c4113dSnw141292 goto out; 3645c5c4113dSnw141292 3646c5c4113dSnw141292 state->sid2pid_done = FALSE; 3647c5c4113dSnw141292 sqlite_freemem(sql); 3648c5c4113dSnw141292 sql = NULL; 3649c5c4113dSnw141292 3650e8c27ec8Sbaban /* Check if we need to update namecache */ 3651e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3652c5c4113dSnw141292 goto out; 3653c5c4113dSnw141292 3654cf5b5989Sdm199847 if (EMPTY_STRING(req->id1name)) 3655c5c4113dSnw141292 goto out; 3656c5c4113dSnw141292 3657c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3658cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3659c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3660cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3661cd37da74Snw141292 req->id1name, req->id1domain, 3662cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3663c5c4113dSnw141292 3664c5c4113dSnw141292 if (sql == NULL) { 3665c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3666c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3667c5c4113dSnw141292 goto out; 3668c5c4113dSnw141292 } 3669c5c4113dSnw141292 3670479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3671c5c4113dSnw141292 3672c5c4113dSnw141292 out: 367348258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 367448258c6bSjp151216 idmap_info_free(&res->info); 367548258c6bSjp151216 367662c60062Sbaban if (sql != NULL) 3677c5c4113dSnw141292 sqlite_freemem(sql); 3678c5c4113dSnw141292 return (retcode); 3679c5c4113dSnw141292 } 3680c5c4113dSnw141292 3681cd37da74Snw141292 static 3682cd37da74Snw141292 idmap_retcode 3683c5c4113dSnw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res, 3684*fe1c642dSBill Krier int is_user) 3685cd37da74Snw141292 { 3686c5c4113dSnw141292 char *end; 3687c5c4113dSnw141292 char *sql = NULL; 3688c5c4113dSnw141292 const char **values; 3689c5c4113dSnw141292 sqlite_vm *vm = NULL; 3690c5c4113dSnw141292 int ncol; 3691c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3692c5c4113dSnw141292 time_t curtime; 3693e8c27ec8Sbaban idmap_id_type idtype; 3694c5c4113dSnw141292 3695c5c4113dSnw141292 /* Current time */ 3696c5c4113dSnw141292 errno = 0; 3697c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3698cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3699c5c4113dSnw141292 strerror(errno)); 3700c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3701c5c4113dSnw141292 goto out; 3702c5c4113dSnw141292 } 3703c5c4113dSnw141292 3704e8c27ec8Sbaban /* SQL to lookup the cache by pid or by unixname */ 3705e8c27ec8Sbaban if (req->id1.idmap_id_u.uid != SENTINEL_PID) { 370648258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 370748258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 370848258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 370948258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3710c5c4113dSnw141292 "FROM idmap_cache WHERE " 3711c5c4113dSnw141292 "pid = %u AND u2w = 1 AND is_user = %d AND " 3712c5c4113dSnw141292 "(pid >= 2147483648 OR " 3713c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3714c5c4113dSnw141292 "expiration > %d));", 3715c5c4113dSnw141292 req->id1.idmap_id_u.uid, is_user, curtime); 3716e8c27ec8Sbaban } else if (req->id1name != NULL) { 371748258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 371848258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 371948258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 372048258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3721e8c27ec8Sbaban "FROM idmap_cache WHERE " 3722e8c27ec8Sbaban "unixname = %Q AND u2w = 1 AND is_user = %d AND " 3723e8c27ec8Sbaban "(pid >= 2147483648 OR " 3724e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 3725e8c27ec8Sbaban "expiration > %d));", 3726e8c27ec8Sbaban req->id1name, is_user, curtime); 372748258c6bSjp151216 } else { 372848258c6bSjp151216 retcode = IDMAP_ERR_ARG; 372948258c6bSjp151216 goto out; 3730e8c27ec8Sbaban } 3731e8c27ec8Sbaban 3732c5c4113dSnw141292 if (sql == NULL) { 3733c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3734c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3735c5c4113dSnw141292 goto out; 3736c5c4113dSnw141292 } 373748258c6bSjp151216 retcode = sql_compile_n_step_once( 373848258c6bSjp151216 cache, sql, &vm, &ncol, 14, &values); 3739c5c4113dSnw141292 sqlite_freemem(sql); 3740c5c4113dSnw141292 3741c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) 3742c5c4113dSnw141292 goto out; 3743c5c4113dSnw141292 else if (retcode == IDMAP_SUCCESS) { 3744c5c4113dSnw141292 /* sanity checks */ 3745c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 3746c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3747c5c4113dSnw141292 goto out; 3748c5c4113dSnw141292 } 3749c5c4113dSnw141292 3750e8c27ec8Sbaban switch (res->id.idtype) { 3751c5c4113dSnw141292 case IDMAP_SID: 3752cd37da74Snw141292 case IDMAP_USID: 3753cd37da74Snw141292 case IDMAP_GSID: 3754e8c27ec8Sbaban idtype = strtol(values[5], &end, 10) == 1 3755cd37da74Snw141292 ? IDMAP_USID : IDMAP_GSID; 3756cd37da74Snw141292 3757e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID && 3758e8c27ec8Sbaban idtype != IDMAP_USID) { 3759cd37da74Snw141292 retcode = IDMAP_ERR_NOTUSER; 3760cd37da74Snw141292 goto out; 3761e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GSID && 3762e8c27ec8Sbaban idtype != IDMAP_GSID) { 3763cd37da74Snw141292 retcode = IDMAP_ERR_NOTGROUP; 3764cd37da74Snw141292 goto out; 3765cd37da74Snw141292 } 3766e8c27ec8Sbaban res->id.idtype = idtype; 3767cd37da74Snw141292 3768c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 3769c5c4113dSnw141292 strtoul(values[1], &end, 10); 3770c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = strdup(values[0]); 3771c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 3772c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3773c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3774c5c4113dSnw141292 goto out; 3775c5c4113dSnw141292 } 3776c5c4113dSnw141292 377762c60062Sbaban if (values[4] != NULL) 3778c5c4113dSnw141292 res->direction = 3779651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 3780651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3781c5c4113dSnw141292 else 3782651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 3783c5c4113dSnw141292 3784*fe1c642dSBill Krier if (values[2] == NULL) 3785c5c4113dSnw141292 break; 37868e228215Sdm199847 req->id2name = strdup(values[2]); 37878e228215Sdm199847 if (req->id2name == NULL) { 3788c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3789c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3790c5c4113dSnw141292 goto out; 3791c5c4113dSnw141292 } 3792c5c4113dSnw141292 3793c5c4113dSnw141292 if (values[3] == NULL) 3794c5c4113dSnw141292 break; 37958e228215Sdm199847 req->id2domain = strdup(values[3]); 37968e228215Sdm199847 if (req->id2domain == NULL) { 3797c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3798c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3799c5c4113dSnw141292 goto out; 3800c5c4113dSnw141292 } 3801cd37da74Snw141292 3802c5c4113dSnw141292 break; 3803c5c4113dSnw141292 default: 3804c5c4113dSnw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 3805c5c4113dSnw141292 break; 3806c5c4113dSnw141292 } 380748258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 380848258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 380948258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 381048258c6bSjp151216 switch (res->info.how.map_type) { 381148258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 381248258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 381348258c6bSjp151216 strdup(values[7]); 381448258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 381548258c6bSjp151216 strdup(values[8]); 381648258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 381748258c6bSjp151216 strdup(values[9]); 381848258c6bSjp151216 break; 381948258c6bSjp151216 382048258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 382148258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 382248258c6bSjp151216 strdup(values[7]); 382348258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 382448258c6bSjp151216 strdup(values[8]); 382548258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 382648258c6bSjp151216 strdup(values[9]); 382748258c6bSjp151216 break; 382848258c6bSjp151216 382948258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 383048258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 383148258c6bSjp151216 strdup(values[10]); 383248258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 383348258c6bSjp151216 strdup(values[11]); 383448258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 383548258c6bSjp151216 strdup(values[12]); 383648258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 383748258c6bSjp151216 strtoul(values[13], &end, 10); 383848258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 383948258c6bSjp151216 is_user; 384048258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 384148258c6bSjp151216 strtol(values[5], &end, 10); 384248258c6bSjp151216 break; 384348258c6bSjp151216 384448258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 384548258c6bSjp151216 break; 384648258c6bSjp151216 384748258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 384848258c6bSjp151216 break; 384948258c6bSjp151216 385048258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 385148258c6bSjp151216 break; 385248258c6bSjp151216 3853e3f2c991SKeyur Desai case IDMAP_MAP_TYPE_IDMU: 3854e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.dn = 3855e3f2c991SKeyur Desai strdup(values[7]); 3856e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.attr = 3857e3f2c991SKeyur Desai strdup(values[8]); 3858e3f2c991SKeyur Desai res->info.how.idmap_how_u.idmu.value = 3859e3f2c991SKeyur Desai strdup(values[9]); 3860e3f2c991SKeyur Desai break; 3861e3f2c991SKeyur Desai 386248258c6bSjp151216 default: 3863e3f2c991SKeyur Desai /* Unknown mapping type */ 386448258c6bSjp151216 assert(FALSE); 386548258c6bSjp151216 } 386648258c6bSjp151216 } 3867c5c4113dSnw141292 } 3868c5c4113dSnw141292 3869c5c4113dSnw141292 out: 387062c60062Sbaban if (vm != NULL) 3871c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3872c5c4113dSnw141292 return (retcode); 3873c5c4113dSnw141292 } 3874c5c4113dSnw141292 387508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 387608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Given: 387708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * cache sqlite handle 387808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * name Windows user name 387908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * domain Windows domain name 388008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 388108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Return: Error code 388208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 388308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *canonname Canonical name (if canonname is non-NULL) [1] 388408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *sidprefix SID prefix [1] 388508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *rid RID 388608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *type Type of name 388708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 388808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * [1] malloc'ed, NULL on error 388908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 3890cd37da74Snw141292 static 3891cd37da74Snw141292 idmap_retcode 3892c5c4113dSnw141292 lookup_cache_name2sid(sqlite *cache, const char *name, const char *domain, 3893cd37da74Snw141292 char **canonname, char **sidprefix, idmap_rid_t *rid, int *type) 3894cd37da74Snw141292 { 3895cd37da74Snw141292 char *end, *lower_name; 389608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char *sql; 3897c5c4113dSnw141292 const char **values; 3898c5c4113dSnw141292 sqlite_vm *vm = NULL; 3899c5c4113dSnw141292 int ncol; 3900c5c4113dSnw141292 time_t curtime; 390108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States idmap_retcode retcode; 390208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 390308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = NULL; 390408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) 390508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = NULL; 3906c5c4113dSnw141292 3907c5c4113dSnw141292 /* Get current time */ 3908c5c4113dSnw141292 errno = 0; 3909c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3910cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3911c5c4113dSnw141292 strerror(errno)); 3912c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3913c5c4113dSnw141292 goto out; 3914c5c4113dSnw141292 } 3915c5c4113dSnw141292 3916c5c4113dSnw141292 /* SQL to lookup the cache */ 3917cd37da74Snw141292 if ((lower_name = tolower_u8(name)) == NULL) 3918cd37da74Snw141292 lower_name = (char *)name; 3919cd37da74Snw141292 sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name " 3920cd37da74Snw141292 "FROM name_cache WHERE name = %Q AND domain = %Q AND " 3921c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3922cd37da74Snw141292 "expiration > %d);", lower_name, domain, curtime); 3923cd37da74Snw141292 if (lower_name != name) 3924cd37da74Snw141292 free(lower_name); 3925c5c4113dSnw141292 if (sql == NULL) { 3926c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3927c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3928c5c4113dSnw141292 goto out; 3929c5c4113dSnw141292 } 3930cd37da74Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values); 393108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 3932c5c4113dSnw141292 sqlite_freemem(sql); 3933c5c4113dSnw141292 393408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (retcode != IDMAP_SUCCESS) 393508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States goto out; 393608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 393762c60062Sbaban if (type != NULL) { 3938c5c4113dSnw141292 if (values[2] == NULL) { 3939c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3940c5c4113dSnw141292 goto out; 3941c5c4113dSnw141292 } 3942c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 3943c5c4113dSnw141292 } 3944c5c4113dSnw141292 3945e8c27ec8Sbaban if (values[0] == NULL || values[1] == NULL) { 3946e8c27ec8Sbaban retcode = IDMAP_ERR_CACHE; 3947e8c27ec8Sbaban goto out; 3948e8c27ec8Sbaban } 3949e8c27ec8Sbaban 3950cd37da74Snw141292 if (canonname != NULL) { 3951cd37da74Snw141292 assert(values[3] != NULL); 395208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = strdup(values[3]); 395308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*canonname == NULL) { 3954cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3955cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 3956cd37da74Snw141292 goto out; 3957cd37da74Snw141292 } 3958cd37da74Snw141292 } 3959cd37da74Snw141292 396008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = strdup(values[0]); 396108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*sidprefix == NULL) { 3962c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3963c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3964c5c4113dSnw141292 goto out; 3965c5c4113dSnw141292 } 3966c5c4113dSnw141292 *rid = strtoul(values[1], &end, 10); 396708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 396808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States retcode = IDMAP_SUCCESS; 3969c5c4113dSnw141292 3970c5c4113dSnw141292 out: 397162c60062Sbaban if (vm != NULL) 3972c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 397308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States 397408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (retcode != IDMAP_SUCCESS) { 397508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*sidprefix); 397608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *sidprefix = NULL; 397708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canonname != NULL) { 397808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*canonname); 397908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canonname = NULL; 398008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 398108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 3982c5c4113dSnw141292 return (retcode); 3983c5c4113dSnw141292 } 3984c5c4113dSnw141292 3985cd37da74Snw141292 static 3986cd37da74Snw141292 idmap_retcode 3987e8c27ec8Sbaban ad_lookup_by_winname(lookup_state_t *state, 3988e8c27ec8Sbaban const char *name, const char *domain, int eunixtype, 398948258c6bSjp151216 char **dn, char **attr, char **value, char **canonname, 399048258c6bSjp151216 char **sidprefix, idmap_rid_t *rid, int *wintype, 399148258c6bSjp151216 char **unixname) 3992cd37da74Snw141292 { 39934d61c878SJulian Pullen int retries; 3994c5c4113dSnw141292 idmap_query_state_t *qs = NULL; 3995c5c4113dSnw141292 idmap_retcode rc, retcode; 39964d61c878SJulian Pullen int i; 39974d61c878SJulian Pullen int found_ad = 0; 3998c5c4113dSnw141292 39992b4a7802SBaban Kenkre RDLOCK_CONFIG(); 4000e3f2c991SKeyur Desai if (_idmapdstate.num_gcs > 0) { 4001e3f2c991SKeyur Desai for (i = 0; i < _idmapdstate.num_gcs && !found_ad; i++) { 40024d61c878SJulian Pullen retries = 0; 40034d61c878SJulian Pullen retry: 4004e3f2c991SKeyur Desai retcode = idmap_lookup_batch_start( 4005e3f2c991SKeyur Desai _idmapdstate.gcs[i], 4006e3f2c991SKeyur Desai 1, 4007e3f2c991SKeyur Desai _idmapdstate.cfg->pgcfg.directory_based_mapping, 4008e3f2c991SKeyur Desai _idmapdstate.cfg->pgcfg.default_domain, 4009e3f2c991SKeyur Desai &qs); 4010e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 40112b4a7802SBaban Kenkre if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && 40122b4a7802SBaban Kenkre retries++ < ADUTILS_DEF_NUM_RETRIES) 40130dcc7149Snw141292 goto retry; 40144d61c878SJulian Pullen degrade_svc(1, "failed to create request for " 40154d61c878SJulian Pullen "AD lookup by winname"); 4016e8c27ec8Sbaban return (retcode); 4017c5c4113dSnw141292 } 4018c5c4113dSnw141292 4019c8e26105Sjp151216 restore_svc(); 4020c8e26105Sjp151216 40214d61c878SJulian Pullen if (state != NULL && i == 0) { 40224d61c878SJulian Pullen /* 40234d61c878SJulian Pullen * Directory based name mapping is only 40244d61c878SJulian Pullen * performed within the joined forest (i == 0). 40254d61c878SJulian Pullen * We don't trust other "trusted" forests to 40264d61c878SJulian Pullen * provide DS-based name mapping information 40274d61c878SJulian Pullen * because AD's definition of "cross-forest 40284d61c878SJulian Pullen * trust" does not encompass this sort of 40294d61c878SJulian Pullen * behavior. 40304d61c878SJulian Pullen */ 40314d61c878SJulian Pullen idmap_lookup_batch_set_unixattr(qs, 40324d61c878SJulian Pullen state->ad_unixuser_attr, 4033e8c27ec8Sbaban state->ad_unixgroup_attr); 40344d61c878SJulian Pullen } 4035c5c4113dSnw141292 40364d61c878SJulian Pullen retcode = idmap_name2sid_batch_add1(qs, name, domain, 40374d61c878SJulian Pullen eunixtype, dn, attr, value, canonname, sidprefix, 4038e3f2c991SKeyur Desai rid, wintype, unixname, NULL, &rc); 40394d61c878SJulian Pullen if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) { 40404d61c878SJulian Pullen idmap_lookup_release_batch(&qs); 40414d61c878SJulian Pullen continue; 40424d61c878SJulian Pullen } 40434d61c878SJulian Pullen found_ad = 1; 4044e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 404584decf41Sjp151216 idmap_lookup_release_batch(&qs); 4046c5c4113dSnw141292 else 40470dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 4048c5c4113dSnw141292 40492b4a7802SBaban Kenkre if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && 40502b4a7802SBaban Kenkre retries++ < ADUTILS_DEF_NUM_RETRIES) 4051c5c4113dSnw141292 goto retry; 4052c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 40534d61c878SJulian Pullen degrade_svc(1, 40544d61c878SJulian Pullen "some AD lookups timed out repeatedly"); 40554d61c878SJulian Pullen } 40564d61c878SJulian Pullen } else { 40574d61c878SJulian Pullen /* No AD case */ 40584d61c878SJulian Pullen retcode = IDMAP_ERR_NO_ACTIVEDIRECTORY; 40594d61c878SJulian Pullen } 40604d61c878SJulian Pullen UNLOCK_CONFIG(); 4061c5c4113dSnw141292 4062c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) { 4063e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by winname failed"); 4064c5c4113dSnw141292 return (retcode); 4065e8c27ec8Sbaban } 4066c5c4113dSnw141292 return (rc); 4067c5c4113dSnw141292 } 4068c5c4113dSnw141292 406908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 407008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Given: 407108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * cache sqlite handle to cache 407208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * name Windows user name 407308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * domain Windows domain name 407408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * local_only if true, don't try AD lookups 407508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 407608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * Returns: Error code 407708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 407808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *canonname Canonical name (if non-NULL) [1] 407908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *canondomain Canonical domain (if non-NULL) [1] 408008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *sidprefix SID prefix [1] 408108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *rid RID 408208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * *req Request (direction is updated) 408308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * 408408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * [1] malloc'ed, NULL on error 408508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 4086cd37da74Snw141292 idmap_retcode 408708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States lookup_name2sid( 408808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States sqlite *cache, 408908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const char *name, 409008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States const char *domain, 409108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States int *is_wuser, 409208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **canonname, 409308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **canondomain, 409408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char **sidprefix, 409508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States idmap_rid_t *rid, 409608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States idmap_mapping *req, 409708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States int local_only) 4098cd37da74Snw141292 { 4099c5c4113dSnw141292 int type; 4100c5c4113dSnw141292 idmap_retcode retcode; 4101c5c4113dSnw141292 4102cd37da74Snw141292 *sidprefix = NULL; 4103e8c27ec8Sbaban if (canonname != NULL) 4104cd37da74Snw141292 *canonname = NULL; 410508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL) 410608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = NULL; 4107cd37da74Snw141292 4108e8c27ec8Sbaban /* Lookup well-known SIDs table */ 410908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States retcode = lookup_wksids_name2sid(name, domain, canonname, canondomain, 411008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States sidprefix, rid, &type); 411162c60062Sbaban if (retcode == IDMAP_SUCCESS) { 4112e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 411362c60062Sbaban goto out; 411462c60062Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 411562c60062Sbaban return (retcode); 411662c60062Sbaban } 411762c60062Sbaban 4118e8c27ec8Sbaban /* Lookup cache */ 4119cd37da74Snw141292 retcode = lookup_cache_name2sid(cache, name, domain, canonname, 4120cd37da74Snw141292 sidprefix, rid, &type); 4121e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 4122e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 4123e8c27ec8Sbaban goto out; 4124e8c27ec8Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 4125e8c27ec8Sbaban return (retcode); 4126e8c27ec8Sbaban } 4127e8c27ec8Sbaban 4128479ac375Sdm199847 /* 4129479ac375Sdm199847 * The caller may be using this function to determine if this 4130479ac375Sdm199847 * request needs to be marked for AD lookup or not 4131479ac375Sdm199847 * (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this 4132479ac375Sdm199847 * function to AD lookup now. 4133479ac375Sdm199847 */ 4134479ac375Sdm199847 if (local_only) 4135479ac375Sdm199847 return (retcode); 4136479ac375Sdm199847 4137e8c27ec8Sbaban /* Lookup AD */ 4138e8c27ec8Sbaban retcode = ad_lookup_by_winname(NULL, name, domain, _IDMAP_T_UNDEF, 413948258c6bSjp151216 NULL, NULL, NULL, canonname, sidprefix, rid, &type, NULL); 4140c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 4141c5c4113dSnw141292 return (retcode); 4142c5c4113dSnw141292 414362c60062Sbaban out: 4144c5c4113dSnw141292 /* 4145c5c4113dSnw141292 * Entry found (cache or Windows lookup) 4146cd37da74Snw141292 * is_wuser is both input as well as output parameter 4147c5c4113dSnw141292 */ 4148e8c27ec8Sbaban if (*is_wuser == 1 && type != _IDMAP_T_USER) 4149e8c27ec8Sbaban retcode = IDMAP_ERR_NOTUSER; 4150e8c27ec8Sbaban else if (*is_wuser == 0 && type != _IDMAP_T_GROUP) 4151e8c27ec8Sbaban retcode = IDMAP_ERR_NOTGROUP; 4152e8c27ec8Sbaban else if (*is_wuser == -1) { 4153c5c4113dSnw141292 /* Caller wants to know if its user or group */ 4154c5c4113dSnw141292 if (type == _IDMAP_T_USER) 4155cd37da74Snw141292 *is_wuser = 1; 4156c5c4113dSnw141292 else if (type == _IDMAP_T_GROUP) 4157cd37da74Snw141292 *is_wuser = 0; 4158e8c27ec8Sbaban else 4159e8c27ec8Sbaban retcode = IDMAP_ERR_SID; 4160e8c27ec8Sbaban } 4161e8c27ec8Sbaban 416208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (retcode == IDMAP_SUCCESS) { 416308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* 416408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * If we were asked for a canonical domain and none 416508f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * of the searches have provided one, assume it's the 416608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States * supplied domain. 416708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States */ 416808f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL && *canondomain == NULL) { 416908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = strdup(domain); 417008f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (*canondomain == NULL) 417108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States retcode = IDMAP_ERR_MEMORY; 417208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 417308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 4174e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4175cd37da74Snw141292 free(*sidprefix); 4176cd37da74Snw141292 *sidprefix = NULL; 4177e8c27ec8Sbaban if (canonname != NULL) { 4178cd37da74Snw141292 free(*canonname); 4179cd37da74Snw141292 *canonname = NULL; 4180cd37da74Snw141292 } 418108f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States if (canondomain != NULL) { 418208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States free(*canondomain); 418308f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States *canondomain = NULL; 418408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States } 4185c5c4113dSnw141292 } 4186c5c4113dSnw141292 return (retcode); 4187c5c4113dSnw141292 } 4188c5c4113dSnw141292 4189cd37da74Snw141292 static 4190cd37da74Snw141292 idmap_retcode 4191479ac375Sdm199847 name_based_mapping_pid2sid(lookup_state_t *state, const char *unixname, 4192cd37da74Snw141292 int is_user, idmap_mapping *req, idmap_id_res *res) 4193cd37da74Snw141292 { 4194c5c4113dSnw141292 const char *winname, *windomain; 4195cd37da74Snw141292 char *canonname; 419608f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States char *canondomain; 4197c5c4113dSnw141292 char *sql = NULL, *errmsg = NULL; 4198c5c4113dSnw141292 idmap_retcode retcode; 4199c5c4113dSnw141292 char *end; 4200c5c4113dSnw141292 const char **values; 4201c5c4113dSnw141292 sqlite_vm *vm = NULL; 420248258c6bSjp151216 int ncol, r; 4203cd37da74Snw141292 int is_wuser; 4204e8c27ec8Sbaban const char *me = "name_based_mapping_pid2sid"; 420548258c6bSjp151216 int non_wild_match = FALSE; 420648258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 420748258c6bSjp151216 int direction; 4208e8c27ec8Sbaban 4209e8c27ec8Sbaban assert(unixname != NULL); /* We have unixname */ 4210e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have winname */ 4211e8c27ec8Sbaban assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */ 4212c5c4113dSnw141292 4213c5c4113dSnw141292 sql = sqlite_mprintf( 421448258c6bSjp151216 "SELECT winname_display, windomain, w2u_order, " 421548258c6bSjp151216 "is_wuser, unixname, is_nt4 " 421648258c6bSjp151216 "FROM namerules WHERE " 4217c5c4113dSnw141292 "u2w_order > 0 AND is_user = %d AND " 4218c5c4113dSnw141292 "(unixname = %Q OR unixname = '*') " 4219cd37da74Snw141292 "ORDER BY u2w_order ASC;", is_user, unixname); 4220c5c4113dSnw141292 if (sql == NULL) { 4221c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 4222c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 4223c5c4113dSnw141292 goto out; 4224c5c4113dSnw141292 } 4225c5c4113dSnw141292 4226479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 4227c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 4228cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 4229cd37da74Snw141292 CHECK_NULL(errmsg)); 4230c5c4113dSnw141292 sqlite_freemem(errmsg); 4231c5c4113dSnw141292 goto out; 4232c5c4113dSnw141292 } 4233c5c4113dSnw141292 423448258c6bSjp151216 for (;;) { 4235c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 423684decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 423784decf41Sjp151216 if (r == SQLITE_ROW) { 423848258c6bSjp151216 if (ncol < 6) { 4239c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 4240c5c4113dSnw141292 goto out; 4241c5c4113dSnw141292 } 4242c5c4113dSnw141292 if (values[0] == NULL) { 4243c5c4113dSnw141292 /* values [1] and [2] can be null */ 4244c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 4245c5c4113dSnw141292 goto out; 4246c5c4113dSnw141292 } 424748258c6bSjp151216 424848258c6bSjp151216 if (values[2] != NULL) 424948258c6bSjp151216 direction = 425048258c6bSjp151216 (strtol(values[2], &end, 10) == 0)? 425148258c6bSjp151216 IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 425248258c6bSjp151216 else 425348258c6bSjp151216 direction = IDMAP_DIRECTION_U2W; 425448258c6bSjp151216 4255c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 425648258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], 425748258c6bSjp151216 values[4], is_user, 425848258c6bSjp151216 strtol(values[3], &end, 10), 425948258c6bSjp151216 strtol(values[5], &end, 10), 426048258c6bSjp151216 direction); 4261c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 4262c5c4113dSnw141292 goto out; 4263c5c4113dSnw141292 } 4264cd37da74Snw141292 4265cd37da74Snw141292 if (values[0][0] == '*') { 426648258c6bSjp151216 winname = unixname; 426748258c6bSjp151216 if (non_wild_match) { 4268cd37da74Snw141292 /* 426948258c6bSjp151216 * There were non-wildcard rules 427048258c6bSjp151216 * where the Windows identity doesn't 427148258c6bSjp151216 * exist. Return no mapping. 4272cd37da74Snw141292 */ 4273cd37da74Snw141292 retcode = IDMAP_ERR_NOMAPPING; 4274cd37da74Snw141292 goto out; 4275cd37da74Snw141292 } 4276cd37da74Snw141292 } else { 427748258c6bSjp151216 /* Save first non-wild match rule */ 427848258c6bSjp151216 if (!non_wild_match) { 427948258c6bSjp151216 idmap_namerule_set(rule, values[1], 428048258c6bSjp151216 values[0], values[4], 428148258c6bSjp151216 is_user, 428248258c6bSjp151216 strtol(values[3], &end, 10), 428348258c6bSjp151216 strtol(values[5], &end, 10), 428448258c6bSjp151216 direction); 428548258c6bSjp151216 non_wild_match = TRUE; 428648258c6bSjp151216 } 4287cd37da74Snw141292 winname = values[0]; 4288cd37da74Snw141292 } 428948258c6bSjp151216 is_wuser = res->id.idtype == IDMAP_USID ? 1 429048258c6bSjp151216 : res->id.idtype == IDMAP_GSID ? 0 429148258c6bSjp151216 : -1; 429262c60062Sbaban if (values[1] != NULL) 4293c5c4113dSnw141292 windomain = values[1]; 4294479ac375Sdm199847 else if (state->defdom != NULL) 4295479ac375Sdm199847 windomain = state->defdom; 4296c5c4113dSnw141292 else { 4297cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: no domain", me); 4298c5c4113dSnw141292 retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 4299c5c4113dSnw141292 goto out; 4300c5c4113dSnw141292 } 4301cd37da74Snw141292 4302479ac375Sdm199847 retcode = lookup_name2sid(state->cache, 4303479ac375Sdm199847 winname, windomain, 430408f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States &is_wuser, &canonname, &canondomain, 4305cd37da74Snw141292 &res->id.idmap_id_u.sid.prefix, 4306479ac375Sdm199847 &res->id.idmap_id_u.sid.rid, req, 0); 4307e8c27ec8Sbaban 4308c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 4309c5c4113dSnw141292 continue; 4310c5c4113dSnw141292 } 4311c5c4113dSnw141292 goto out; 431248258c6bSjp151216 4313c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 431448258c6bSjp151216 /* 431548258c6bSjp151216 * If there were non-wildcard rules where 431648258c6bSjp151216 * Windows identity doesn't exist 431748258c6bSjp151216 * return no mapping. 431848258c6bSjp151216 */ 431948258c6bSjp151216 if (non_wild_match) 432048258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 432148258c6bSjp151216 else 4322c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 4323c5c4113dSnw141292 goto out; 4324c5c4113dSnw141292 } else { 4325c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 4326c5c4113dSnw141292 vm = NULL; 4327cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 4328cd37da74Snw141292 CHECK_NULL(errmsg)); 4329c5c4113dSnw141292 sqlite_freemem(errmsg); 4330c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 4331c5c4113dSnw141292 goto out; 4332c5c4113dSnw141292 } 4333c5c4113dSnw141292 } 4334c5c4113dSnw141292 4335c5c4113dSnw141292 out: 433662c60062Sbaban if (sql != NULL) 4337c5c4113dSnw141292 sqlite_freemem(sql); 4338c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 4339cd37da74Snw141292 res->id.idtype = is_wuser ? IDMAP_USID : IDMAP_GSID; 4340cd37da74Snw141292 434162c60062Sbaban if (values[2] != NULL) 4342c5c4113dSnw141292 res->direction = 4343651c0131Sbaban (strtol(values[2], &end, 10) == 0)? 4344651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 4345c5c4113dSnw141292 else 4346651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 43478e228215Sdm199847 4348cd37da74Snw141292 req->id2name = canonname; 434908f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States req->id2domain = canondomain; 4350c5c4113dSnw141292 } 4351479ac375Sdm199847 4352479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 435348258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], values[4], 435448258c6bSjp151216 is_user, strtol(values[3], &end, 10), 435548258c6bSjp151216 strtol(values[5], &end, 10), 435648258c6bSjp151216 rule->direction); 4357fc724630SAlan Wright } 4358fc724630SAlan Wright 4359fc724630SAlan Wright if (retcode != IDMAP_ERR_NOTFOUND) { 4360fc724630SAlan Wright res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 436148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 4362c5c4113dSnw141292 } 4363fc724630SAlan Wright 436462c60062Sbaban if (vm != NULL) 4365c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 4366c5c4113dSnw141292 return (retcode); 4367c5c4113dSnw141292 } 4368c5c4113dSnw141292 4369cd37da74Snw141292 /* 4370cd37da74Snw141292 * Convention when processing unix2win requests: 4371cd37da74Snw141292 * 4372cd37da74Snw141292 * Unix identity: 4373cd37da74Snw141292 * req->id1name = 4374cd37da74Snw141292 * unixname if given otherwise unixname found will be placed 4375cd37da74Snw141292 * here. 4376cd37da74Snw141292 * req->id1domain = 4377cd37da74Snw141292 * NOT USED 4378cd37da74Snw141292 * req->id1.idtype = 4379cd37da74Snw141292 * Given type (IDMAP_UID or IDMAP_GID) 4380cd37da74Snw141292 * req->id1..[uid or gid] = 4381cd37da74Snw141292 * UID/GID if given otherwise UID/GID found will be placed here. 4382cd37da74Snw141292 * 4383cd37da74Snw141292 * Windows identity: 4384cd37da74Snw141292 * req->id2name = 4385cd37da74Snw141292 * winname found will be placed here. 4386cd37da74Snw141292 * req->id2domain = 4387cd37da74Snw141292 * windomain found will be placed here. 4388cd37da74Snw141292 * res->id.idtype = 4389cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 4390cd37da74Snw141292 * it is IDMAP_SID then actual type (IDMAP_USID/GSID) found 4391cd37da74Snw141292 * will be placed here. 4392cd37da74Snw141292 * req->id..sid.[prefix, rid] = 4393cd37da74Snw141292 * SID found will be placed here. 4394cd37da74Snw141292 * 4395cd37da74Snw141292 * Others: 4396cd37da74Snw141292 * res->retcode = 4397cd37da74Snw141292 * Return status for this request will be placed here. 4398cd37da74Snw141292 * res->direction = 4399cd37da74Snw141292 * Direction found will be placed here. Direction 4400cd37da74Snw141292 * meaning whether the resultant mapping is valid 4401cd37da74Snw141292 * only from unix2win or bi-directional. 4402cd37da74Snw141292 * req->direction = 4403cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 4404cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 4405cd37da74Snw141292 * of the request. 4406cd37da74Snw141292 * req->id2.idtype = 4407cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 4408cd37da74Snw141292 * type and is used to initialize res->id.idtype. 4409cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 4410cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 4411cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 4412cd37da74Snw141292 * mis-interpreting the contents of req->id2. 4413cd37da74Snw141292 * req->id2..[uid or gid or sid] = 4414cd37da74Snw141292 * NOT USED 4415cd37da74Snw141292 */ 4416cd37da74Snw141292 4417cd37da74Snw141292 /* 4418cd37da74Snw141292 * This function does the following: 4419cd37da74Snw141292 * 1. Lookup well-known SIDs table. 4420cd37da74Snw141292 * 2. Lookup cache. 4421cd37da74Snw141292 * 3. Check if the client does not want new mapping to be allocated 4422cd37da74Snw141292 * in which case this pass is the final pass. 4423e8c27ec8Sbaban * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs 4424e8c27ec8Sbaban * to do AD/NLDAP lookup. 4425cd37da74Snw141292 */ 4426c5c4113dSnw141292 idmap_retcode 4427479ac375Sdm199847 pid2sid_first_pass(lookup_state_t *state, idmap_mapping *req, 4428*fe1c642dSBill Krier idmap_id_res *res, int is_user) 4429cd37da74Snw141292 { 4430e8c27ec8Sbaban idmap_retcode retcode; 4431e8c27ec8Sbaban bool_t gen_localsid_on_err = FALSE; 4432c5c4113dSnw141292 4433e8c27ec8Sbaban /* Initialize result */ 4434c5c4113dSnw141292 res->id.idtype = req->id2.idtype; 4435e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 4436c5c4113dSnw141292 4437e8c27ec8Sbaban if (req->id2.idmap_id_u.sid.prefix != NULL) { 4438e8c27ec8Sbaban /* sanitize sidprefix */ 4439e8c27ec8Sbaban free(req->id2.idmap_id_u.sid.prefix); 4440e8c27ec8Sbaban req->id2.idmap_id_u.sid.prefix = NULL; 4441e8c27ec8Sbaban } 4442e8c27ec8Sbaban 444348258c6bSjp151216 /* Find pid */ 444448258c6bSjp151216 if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 4445*fe1c642dSBill Krier if (req->id1name == NULL) { 4446*fe1c642dSBill Krier retcode = IDMAP_ERR_ARG; 4447*fe1c642dSBill Krier goto out; 4448*fe1c642dSBill Krier } 4449*fe1c642dSBill Krier 445048258c6bSjp151216 if (ns_lookup_byname(req->id1name, NULL, &req->id1) 445148258c6bSjp151216 != IDMAP_SUCCESS) { 445248258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 445348258c6bSjp151216 goto out; 445448258c6bSjp151216 } 445548258c6bSjp151216 } 445648258c6bSjp151216 445708f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Lookup in well-known SIDs table */ 4458c5c4113dSnw141292 retcode = lookup_wksids_pid2sid(req, res, is_user); 4459c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 4460c5c4113dSnw141292 goto out; 4461c5c4113dSnw141292 446208f0d8daSafshin salek ardakani - Sun Microsystems - Irvine United States /* Lookup in cache */ 4463*fe1c642dSBill Krier retcode = lookup_cache_pid2sid(state->cache, req, res, is_user); 4464c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 4465c5c4113dSnw141292 goto out; 4466c5c4113dSnw141292 4467c5c4113dSnw141292 /* Ephemeral ids cannot be allocated during pid2sid */ 4468c5c4113dSnw141292 if (IS_EPHEMERAL(req->id1.idmap_id_u.uid)) { 446962c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 4470c5c4113dSnw141292 goto out; 4471c5c4113dSnw141292 } 4472c5c4113dSnw141292 447348258c6bSjp151216 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) { 44744d61c878SJulian Pullen retcode = IDMAP_ERR_NONE_GENERATED; 447548258c6bSjp151216 goto out; 447648258c6bSjp151216 } 447748258c6bSjp151216 447848258c6bSjp151216 if (AVOID_NAMESERVICE(req)) { 4479e8c27ec8Sbaban gen_localsid_on_err = TRUE; 448062c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 4481c5c4113dSnw141292 goto out; 4482c5c4113dSnw141292 } 4483c5c4113dSnw141292 4484e8c27ec8Sbaban /* Set flags for the next stage */ 4485e3f2c991SKeyur Desai if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) { 4486e3f2c991SKeyur Desai req->direction |= _IDMAP_F_LOOKUP_AD; 4487e3f2c991SKeyur Desai state->ad_nqueries++; 4488e3f2c991SKeyur Desai } else if (AD_MODE(req->id1.idtype, state)) { 4489e8c27ec8Sbaban /* 4490e8c27ec8Sbaban * If AD-based name mapping is enabled then the next stage 4491e8c27ec8Sbaban * will need to lookup AD using unixname to get the 4492e8c27ec8Sbaban * corresponding winname. 4493e8c27ec8Sbaban */ 4494e8c27ec8Sbaban if (req->id1name == NULL) { 4495e8c27ec8Sbaban /* Get unixname if only pid is given. */ 4496e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, 4497e8c27ec8Sbaban is_user, &req->id1name); 4498479ac375Sdm199847 if (retcode != IDMAP_SUCCESS) { 4499479ac375Sdm199847 gen_localsid_on_err = TRUE; 4500e8c27ec8Sbaban goto out; 4501c5c4113dSnw141292 } 4502479ac375Sdm199847 } 4503e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 4504e8c27ec8Sbaban state->ad_nqueries++; 4505e8c27ec8Sbaban } else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) { 4506e8c27ec8Sbaban /* 4507e8c27ec8Sbaban * If native LDAP or mixed mode is enabled for name mapping 4508e8c27ec8Sbaban * then the next stage will need to lookup native LDAP using 4509e8c27ec8Sbaban * unixname/pid to get the corresponding winname. 4510e8c27ec8Sbaban */ 4511e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_NLDAP; 4512e8c27ec8Sbaban state->nldap_nqueries++; 4513c5c4113dSnw141292 } 4514c5c4113dSnw141292 4515e8c27ec8Sbaban /* 4516e8c27ec8Sbaban * Failed to find non-expired entry in cache. Set the flag to 4517e8c27ec8Sbaban * indicate that we are not done yet. 4518e8c27ec8Sbaban */ 4519e8c27ec8Sbaban state->pid2sid_done = FALSE; 4520e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 4521e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 4522e8c27ec8Sbaban 4523e8c27ec8Sbaban out: 4524e8c27ec8Sbaban res->retcode = idmap_stat4prot(retcode); 4525e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 4526e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 452748258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 4528e8c27ec8Sbaban return (retcode); 4529e8c27ec8Sbaban } 4530e8c27ec8Sbaban 4531e8c27ec8Sbaban idmap_retcode 4532479ac375Sdm199847 pid2sid_second_pass(lookup_state_t *state, idmap_mapping *req, 4533479ac375Sdm199847 idmap_id_res *res, int is_user) 4534e8c27ec8Sbaban { 4535e8c27ec8Sbaban bool_t gen_localsid_on_err = TRUE; 4536e8c27ec8Sbaban idmap_retcode retcode = IDMAP_SUCCESS; 4537e8c27ec8Sbaban 4538e8c27ec8Sbaban /* Check if second pass is needed */ 4539e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 4540e8c27ec8Sbaban return (res->retcode); 4541e8c27ec8Sbaban 4542e8c27ec8Sbaban /* Get status from previous pass */ 4543e8c27ec8Sbaban retcode = res->retcode; 4544e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4545e8c27ec8Sbaban goto out; 4546e8c27ec8Sbaban 4547e8c27ec8Sbaban /* 4548e8c27ec8Sbaban * If directory-based name mapping is enabled then the winname 4549e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode) 4550479ac375Sdm199847 * or from native LDAP object (nldap-mode or mixed-mode). 4551479ac375Sdm199847 * Note that if we have winname but no SID then it's an error 4552479ac375Sdm199847 * because this implies that the Native LDAP entry contains 4553479ac375Sdm199847 * winname which does not exist and it's better that we return 4554479ac375Sdm199847 * an error instead of doing rule-based mapping so that the user 4555479ac375Sdm199847 * can detect the issue and take appropriate action. 4556e8c27ec8Sbaban */ 4557479ac375Sdm199847 if (req->id2name != NULL) { 4558479ac375Sdm199847 /* Return notfound if we've winname but no SID. */ 4559479ac375Sdm199847 if (res->id.idmap_id_u.sid.prefix == NULL) { 4560479ac375Sdm199847 retcode = IDMAP_ERR_NOTFOUND; 4561479ac375Sdm199847 goto out; 4562479ac375Sdm199847 } 4563e3f2c991SKeyur Desai if (state->directory_based_mapping == DIRECTORY_MAPPING_IDMU) 4564e3f2c991SKeyur Desai res->direction = IDMAP_DIRECTION_BI; 4565e3f2c991SKeyur Desai else if (AD_MODE(req->id1.idtype, state)) 4566e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4567e8c27ec8Sbaban else if (NLDAP_MODE(req->id1.idtype, state)) 4568e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4569e8c27ec8Sbaban else if (MIXED_MODE(req->id1.idtype, state)) 4570e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 4571e8c27ec8Sbaban goto out; 4572479ac375Sdm199847 } else if (res->id.idmap_id_u.sid.prefix != NULL) { 4573479ac375Sdm199847 /* 4574479ac375Sdm199847 * We've SID but no winname. This is fine because 4575479ac375Sdm199847 * the caller may have only requested SID. 4576479ac375Sdm199847 */ 4577479ac375Sdm199847 goto out; 4578e8c27ec8Sbaban } 4579e8c27ec8Sbaban 4580479ac375Sdm199847 /* Free any mapping info from Directory based mapping */ 4581479ac375Sdm199847 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 4582479ac375Sdm199847 idmap_info_free(&res->info); 4583479ac375Sdm199847 4584e8c27ec8Sbaban if (req->id1name == NULL) { 4585e8c27ec8Sbaban /* Get unixname from name service */ 4586e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user, 4587e8c27ec8Sbaban &req->id1name); 4588e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4589e8c27ec8Sbaban goto out; 4590e8c27ec8Sbaban } else if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 4591e8c27ec8Sbaban /* Get pid from name service */ 4592e8c27ec8Sbaban retcode = ns_lookup_byname(req->id1name, NULL, &req->id1); 4593e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4594e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4595e8c27ec8Sbaban goto out; 4596e8c27ec8Sbaban } 4597e8c27ec8Sbaban } 4598e8c27ec8Sbaban 4599e8c27ec8Sbaban /* Use unixname to evaluate local name-based mapping rules */ 4600479ac375Sdm199847 retcode = name_based_mapping_pid2sid(state, req->id1name, is_user, 4601c5c4113dSnw141292 req, res); 4602c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 460348258c6bSjp151216 retcode = generate_localsid(req, res, is_user, FALSE); 4604e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4605e8c27ec8Sbaban } 4606c5c4113dSnw141292 4607c5c4113dSnw141292 out: 4608c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 4609e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 4610e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 4611479ac375Sdm199847 free(req->id2name); 4612479ac375Sdm199847 req->id2name = NULL; 4613479ac375Sdm199847 free(req->id2domain); 4614479ac375Sdm199847 req->id2domain = NULL; 4615e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 461648258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 4617479ac375Sdm199847 else 4618479ac375Sdm199847 res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 4619e8c27ec8Sbaban } 4620e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 4621e8c27ec8Sbaban state->pid2sid_done = FALSE; 4622c5c4113dSnw141292 return (retcode); 4623c5c4113dSnw141292 } 4624