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 /* 22042addd6Sbaban * Copyright 2008 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> 44c5c4113dSnw141292 45c5c4113dSnw141292 #include "idmapd.h" 46c5c4113dSnw141292 #include "adutils.h" 47c5c4113dSnw141292 #include "string.h" 48c5c4113dSnw141292 #include "idmap_priv.h" 49cd37da74Snw141292 #include "schema.h" 50e8c27ec8Sbaban #include "nldaputils.h" 51c5c4113dSnw141292 5284decf41Sjp151216 53c5c4113dSnw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *, 54c5c4113dSnw141292 sqlite_vm **, int *, int, const char ***); 55479ac375Sdm199847 static idmap_retcode ad_lookup_one(lookup_state_t *, idmap_mapping *, 56479ac375Sdm199847 idmap_id_res *); 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 61c5c4113dSnw141292 62c5c4113dSnw141292 #define EMPTY_NAME(name) (*name == 0 || strcmp(name, "\"\"") == 0) 63c5c4113dSnw141292 64c5c4113dSnw141292 #define DO_NOT_ALLOC_NEW_ID_MAPPING(req)\ 65c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC) 66c5c4113dSnw141292 67c5c4113dSnw141292 #define AVOID_NAMESERVICE(req)\ 68c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE) 69c5c4113dSnw141292 70e8c27ec8Sbaban #define IS_EPHEMERAL(pid) (pid > INT32_MAX && pid != SENTINEL_PID) 71c5c4113dSnw141292 72c5c4113dSnw141292 #define LOCALRID_MIN 1000 73c5c4113dSnw141292 74c5c4113dSnw141292 75c5c4113dSnw141292 typedef enum init_db_option { 76c5c4113dSnw141292 FAIL_IF_CORRUPT = 0, 77c5c4113dSnw141292 REMOVE_IF_CORRUPT = 1 78c5c4113dSnw141292 } init_db_option_t; 79c5c4113dSnw141292 8084decf41Sjp151216 /* 81e8c27ec8Sbaban * Data structure to store well-known SIDs and 82e8c27ec8Sbaban * associated mappings (if any) 83e8c27ec8Sbaban */ 84e8c27ec8Sbaban typedef struct wksids_table { 85e8c27ec8Sbaban const char *sidprefix; 86e8c27ec8Sbaban uint32_t rid; 87e8c27ec8Sbaban const char *winname; 88e8c27ec8Sbaban int is_wuser; 89e8c27ec8Sbaban uid_t pid; 90e8c27ec8Sbaban int is_user; 91e8c27ec8Sbaban int direction; 92e8c27ec8Sbaban } wksids_table_t; 93e8c27ec8Sbaban 94e8c27ec8Sbaban /* 9584decf41Sjp151216 * Thread specfic data to hold the database handles so that the 9684decf41Sjp151216 * databaes are not opened and closed for every request. It also 9784decf41Sjp151216 * contains the sqlite busy handler structure. 9884decf41Sjp151216 */ 9984decf41Sjp151216 10084decf41Sjp151216 struct idmap_busy { 10184decf41Sjp151216 const char *name; 10284decf41Sjp151216 const int *delays; 10384decf41Sjp151216 int delay_size; 10484decf41Sjp151216 int total; 10584decf41Sjp151216 int sec; 10684decf41Sjp151216 }; 10784decf41Sjp151216 10884decf41Sjp151216 10984decf41Sjp151216 typedef struct idmap_tsd { 11084decf41Sjp151216 sqlite *db_db; 11184decf41Sjp151216 sqlite *cache_db; 11284decf41Sjp151216 struct idmap_busy cache_busy; 11384decf41Sjp151216 struct idmap_busy db_busy; 11484decf41Sjp151216 } idmap_tsd_t; 11584decf41Sjp151216 11684decf41Sjp151216 11784decf41Sjp151216 11884decf41Sjp151216 static const int cache_delay_table[] = 11984decf41Sjp151216 { 1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 12084decf41Sjp151216 50, 50, 60, 70, 80, 90, 100}; 12184decf41Sjp151216 12284decf41Sjp151216 static const int db_delay_table[] = 12384decf41Sjp151216 { 5, 10, 15, 20, 30, 40, 55, 70, 100}; 12484decf41Sjp151216 12584decf41Sjp151216 12684decf41Sjp151216 static pthread_key_t idmap_tsd_key; 12784decf41Sjp151216 12884decf41Sjp151216 void 12984decf41Sjp151216 idmap_tsd_destroy(void *key) 13084decf41Sjp151216 { 13184decf41Sjp151216 13284decf41Sjp151216 idmap_tsd_t *tsd = (idmap_tsd_t *)key; 13384decf41Sjp151216 if (tsd) { 13484decf41Sjp151216 if (tsd->db_db) 13584decf41Sjp151216 (void) sqlite_close(tsd->db_db); 13684decf41Sjp151216 if (tsd->cache_db) 13784decf41Sjp151216 (void) sqlite_close(tsd->cache_db); 13884decf41Sjp151216 free(tsd); 13984decf41Sjp151216 } 14084decf41Sjp151216 } 14184decf41Sjp151216 14284decf41Sjp151216 int 143cd37da74Snw141292 idmap_init_tsd_key(void) 144cd37da74Snw141292 { 14584decf41Sjp151216 return (pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy)); 14684decf41Sjp151216 } 14784decf41Sjp151216 14884decf41Sjp151216 14984decf41Sjp151216 15084decf41Sjp151216 idmap_tsd_t * 15184decf41Sjp151216 idmap_get_tsd(void) 15284decf41Sjp151216 { 15384decf41Sjp151216 idmap_tsd_t *tsd; 15484decf41Sjp151216 15584decf41Sjp151216 if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) { 15684decf41Sjp151216 /* No thread specific data so create it */ 15784decf41Sjp151216 if ((tsd = malloc(sizeof (*tsd))) != NULL) { 15884decf41Sjp151216 /* Initialize thread specific data */ 15984decf41Sjp151216 (void) memset(tsd, 0, sizeof (*tsd)); 16084decf41Sjp151216 /* save the trhread specific data */ 16184decf41Sjp151216 if (pthread_setspecific(idmap_tsd_key, tsd) != 0) { 16284decf41Sjp151216 /* Can't store key */ 16384decf41Sjp151216 free(tsd); 16484decf41Sjp151216 tsd = NULL; 16584decf41Sjp151216 } 16684decf41Sjp151216 } else { 16784decf41Sjp151216 tsd = NULL; 16884decf41Sjp151216 } 16984decf41Sjp151216 } 17084decf41Sjp151216 17184decf41Sjp151216 return (tsd); 17284decf41Sjp151216 } 17384decf41Sjp151216 174cd37da74Snw141292 /* 175cd37da74Snw141292 * A simple wrapper around u8_textprep_str() that returns the Unicode 176cd37da74Snw141292 * lower-case version of some string. The result must be freed. 177cd37da74Snw141292 */ 178cd37da74Snw141292 char * 179cd37da74Snw141292 tolower_u8(const char *s) 180cd37da74Snw141292 { 181cd37da74Snw141292 char *res = NULL; 182cd37da74Snw141292 char *outs; 183cd37da74Snw141292 size_t inlen, outlen, inbytesleft, outbytesleft; 184cd37da74Snw141292 int rc, err; 185cd37da74Snw141292 186cd37da74Snw141292 /* 187cd37da74Snw141292 * u8_textprep_str() does not allocate memory. The input and 188cd37da74Snw141292 * output buffers may differ in size (though that would be more 189cd37da74Snw141292 * likely when normalization is done). We have to loop over it... 190cd37da74Snw141292 * 191cd37da74Snw141292 * To improve the chances that we can avoid looping we add 10 192cd37da74Snw141292 * bytes of output buffer room the first go around. 193cd37da74Snw141292 */ 194cd37da74Snw141292 inlen = inbytesleft = strlen(s); 195cd37da74Snw141292 outlen = outbytesleft = inlen + 10; 196cd37da74Snw141292 if ((res = malloc(outlen)) == NULL) 197cd37da74Snw141292 return (NULL); 198cd37da74Snw141292 outs = res; 199cd37da74Snw141292 200cd37da74Snw141292 while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs, 201cd37da74Snw141292 &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 && 202cd37da74Snw141292 err == E2BIG) { 203cd37da74Snw141292 if ((res = realloc(res, outlen + inbytesleft)) == NULL) 204cd37da74Snw141292 return (NULL); 205cd37da74Snw141292 /* adjust input/output buffer pointers */ 206cd37da74Snw141292 s += (inlen - inbytesleft); 207cd37da74Snw141292 outs = res + outlen - outbytesleft; 208cd37da74Snw141292 /* adjust outbytesleft and outlen */ 209cd37da74Snw141292 outlen += inbytesleft; 210cd37da74Snw141292 outbytesleft += inbytesleft; 211cd37da74Snw141292 } 212cd37da74Snw141292 213cd37da74Snw141292 if (rc < 0) { 214cd37da74Snw141292 free(res); 215cd37da74Snw141292 res = NULL; 216cd37da74Snw141292 return (NULL); 217cd37da74Snw141292 } 218cd37da74Snw141292 219cd37da74Snw141292 res[outlen - outbytesleft] = '\0'; 220cd37da74Snw141292 221cd37da74Snw141292 return (res); 222cd37da74Snw141292 } 223cd37da74Snw141292 224cd37da74Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 225cd37da74Snw141292 const char *while_doing); 226cd37da74Snw141292 227c5c4113dSnw141292 228c5c4113dSnw141292 /* 229c5c4113dSnw141292 * Initialize 'dbname' using 'sql' 230c5c4113dSnw141292 */ 231cd37da74Snw141292 static 232cd37da74Snw141292 int 233cd37da74Snw141292 init_db_instance(const char *dbname, int version, 234cd37da74Snw141292 const char *detect_version_sql, char * const *sql, 235cd37da74Snw141292 init_db_option_t opt, int *created, int *upgraded) 236c5c4113dSnw141292 { 237cd37da74Snw141292 int rc, curr_version; 238cd37da74Snw141292 int tries = 1; 239cd37da74Snw141292 int prio = LOG_NOTICE; 240c5c4113dSnw141292 sqlite *db = NULL; 241cd37da74Snw141292 char *errmsg = NULL; 242c5c4113dSnw141292 243cd37da74Snw141292 *created = 0; 244cd37da74Snw141292 *upgraded = 0; 245c5c4113dSnw141292 246cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 247cd37da74Snw141292 tries = 3; 248cd37da74Snw141292 249cd37da74Snw141292 rinse_repeat: 250cd37da74Snw141292 if (tries == 0) { 251cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname); 252c5c4113dSnw141292 return (-1); 253cd37da74Snw141292 } 254cd37da74Snw141292 if (tries-- == 1) 255cd37da74Snw141292 /* Last try, log errors */ 256cd37da74Snw141292 prio = LOG_ERR; 257c5c4113dSnw141292 258cd37da74Snw141292 db = sqlite_open(dbname, 0600, &errmsg); 259cd37da74Snw141292 if (db == NULL) { 260cd37da74Snw141292 idmapdlog(prio, "Error creating database %s (%s)", 261cd37da74Snw141292 dbname, CHECK_NULL(errmsg)); 262cd37da74Snw141292 sqlite_freemem(errmsg); 263cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 264c5c4113dSnw141292 (void) unlink(dbname); 265cd37da74Snw141292 goto rinse_repeat; 266c5c4113dSnw141292 } 267c5c4113dSnw141292 268c5c4113dSnw141292 sqlite_busy_timeout(db, 3000); 269cd37da74Snw141292 270cd37da74Snw141292 /* Detect current version of schema in the db, if any */ 271cd37da74Snw141292 curr_version = 0; 272cd37da74Snw141292 if (detect_version_sql != NULL) { 273cd37da74Snw141292 char *end, **results; 274cd37da74Snw141292 int nrow; 275cd37da74Snw141292 276cd37da74Snw141292 #ifdef IDMAPD_DEBUG 277cd37da74Snw141292 (void) fprintf(stderr, "Schema version detection SQL: %s\n", 278cd37da74Snw141292 detect_version_sql); 279cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 280cd37da74Snw141292 rc = sqlite_get_table(db, detect_version_sql, &results, 281cd37da74Snw141292 &nrow, NULL, &errmsg); 282cd37da74Snw141292 if (rc != SQLITE_OK) { 283cd37da74Snw141292 idmapdlog(prio, 284cd37da74Snw141292 "Error detecting schema version of db %s (%s)", 285cd37da74Snw141292 dbname, errmsg); 286cd37da74Snw141292 sqlite_freemem(errmsg); 287cd37da74Snw141292 sqlite_free_table(results); 288c5c4113dSnw141292 sqlite_close(db); 289cd37da74Snw141292 return (-1); 290cd37da74Snw141292 } 291cd37da74Snw141292 if (nrow != 1) { 292cd37da74Snw141292 idmapdlog(prio, 293cd37da74Snw141292 "Error detecting schema version of db %s", dbname); 294cd37da74Snw141292 sqlite_close(db); 295cd37da74Snw141292 sqlite_free_table(results); 296cd37da74Snw141292 return (-1); 297cd37da74Snw141292 } 298cd37da74Snw141292 curr_version = strtol(results[1], &end, 10); 299cd37da74Snw141292 sqlite_free_table(results); 300c5c4113dSnw141292 } 301c5c4113dSnw141292 302cd37da74Snw141292 if (curr_version < 0) { 303cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 304cd37da74Snw141292 (void) unlink(dbname); 305cd37da74Snw141292 goto rinse_repeat; 306c5c4113dSnw141292 } 307c5c4113dSnw141292 308cd37da74Snw141292 if (curr_version == version) 309cd37da74Snw141292 goto done; 310cd37da74Snw141292 311cd37da74Snw141292 /* Install or upgrade schema */ 312cd37da74Snw141292 #ifdef IDMAPD_DEBUG 313cd37da74Snw141292 (void) fprintf(stderr, "Schema init/upgrade SQL: %s\n", 314cd37da74Snw141292 sql[curr_version]); 315cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 316cd37da74Snw141292 rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname, 317cd37da74Snw141292 (curr_version == 0) ? "installing schema" : "upgrading schema"); 318cd37da74Snw141292 if (rc != 0) { 319cd37da74Snw141292 idmapdlog(prio, "Error %s schema for db %s", dbname, 320cd37da74Snw141292 (curr_version == 0) ? "installing schema" : 321cd37da74Snw141292 "upgrading schema"); 322cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 323cd37da74Snw141292 (void) unlink(dbname); 324cd37da74Snw141292 goto rinse_repeat; 325c5c4113dSnw141292 } 326c5c4113dSnw141292 327cd37da74Snw141292 *upgraded = (curr_version > 0); 328cd37da74Snw141292 *created = (curr_version == 0); 329cd37da74Snw141292 330cd37da74Snw141292 done: 331c5c4113dSnw141292 (void) sqlite_close(db); 332cd37da74Snw141292 return (0); 333c5c4113dSnw141292 } 334c5c4113dSnw141292 33584decf41Sjp151216 33684decf41Sjp151216 /* 33784decf41Sjp151216 * This is the SQLite database busy handler that retries the SQL 33884decf41Sjp151216 * operation until it is successful. 33984decf41Sjp151216 */ 34084decf41Sjp151216 int 34184decf41Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */ 34284decf41Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count) 34384decf41Sjp151216 { 34484decf41Sjp151216 struct idmap_busy *busy = arg; 34584decf41Sjp151216 int delay; 34684decf41Sjp151216 struct timespec rqtp; 34784decf41Sjp151216 34884decf41Sjp151216 if (count == 1) { 34984decf41Sjp151216 busy->total = 0; 35084decf41Sjp151216 busy->sec = 2; 35184decf41Sjp151216 } 35284decf41Sjp151216 if (busy->total > 1000 * busy->sec) { 3532b3ecdebSjp151216 idmapdlog(LOG_DEBUG, 35484decf41Sjp151216 "Thread %d waited %d sec for the %s database", 35584decf41Sjp151216 pthread_self(), busy->sec, busy->name); 35684decf41Sjp151216 busy->sec++; 35784decf41Sjp151216 } 35884decf41Sjp151216 35984decf41Sjp151216 if (count <= busy->delay_size) { 36084decf41Sjp151216 delay = busy->delays[count-1]; 36184decf41Sjp151216 } else { 36284decf41Sjp151216 delay = busy->delays[busy->delay_size - 1]; 36384decf41Sjp151216 } 36484decf41Sjp151216 busy->total += delay; 36584decf41Sjp151216 rqtp.tv_sec = 0; 36684decf41Sjp151216 rqtp.tv_nsec = delay * (NANOSEC / MILLISEC); 36784decf41Sjp151216 (void) nanosleep(&rqtp, NULL); 36884decf41Sjp151216 return (1); 36984decf41Sjp151216 } 37084decf41Sjp151216 37184decf41Sjp151216 372c5c4113dSnw141292 /* 373c5c4113dSnw141292 * Get the database handle 374c5c4113dSnw141292 */ 375c5c4113dSnw141292 idmap_retcode 376cd37da74Snw141292 get_db_handle(sqlite **db) 377cd37da74Snw141292 { 378c5c4113dSnw141292 char *errmsg; 37984decf41Sjp151216 idmap_tsd_t *tsd; 380c5c4113dSnw141292 381c5c4113dSnw141292 /* 38284decf41Sjp151216 * Retrieve the db handle from thread-specific storage 383c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 384c5c4113dSnw141292 */ 38584decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 38684decf41Sjp151216 idmapdlog(LOG_ERR, 387cd37da74Snw141292 "Error getting thread specific data for %s", IDMAP_DBNAME); 38884decf41Sjp151216 return (IDMAP_ERR_MEMORY); 38984decf41Sjp151216 } 390c5c4113dSnw141292 39184decf41Sjp151216 if (tsd->db_db == NULL) { 39284decf41Sjp151216 tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg); 39384decf41Sjp151216 if (tsd->db_db == NULL) { 394cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 395c5c4113dSnw141292 IDMAP_DBNAME, CHECK_NULL(errmsg)); 396c5c4113dSnw141292 sqlite_freemem(errmsg); 397cd37da74Snw141292 return (IDMAP_ERR_DB); 398c5c4113dSnw141292 } 399cd37da74Snw141292 40084decf41Sjp151216 tsd->db_busy.name = IDMAP_DBNAME; 40184decf41Sjp151216 tsd->db_busy.delays = db_delay_table; 40284decf41Sjp151216 tsd->db_busy.delay_size = sizeof (db_delay_table) / 40384decf41Sjp151216 sizeof (int); 40484decf41Sjp151216 sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler, 40584decf41Sjp151216 &tsd->db_busy); 40684decf41Sjp151216 } 40784decf41Sjp151216 *db = tsd->db_db; 408c5c4113dSnw141292 return (IDMAP_SUCCESS); 409c5c4113dSnw141292 } 410c5c4113dSnw141292 411c5c4113dSnw141292 /* 412c5c4113dSnw141292 * Get the cache handle 413c5c4113dSnw141292 */ 414c5c4113dSnw141292 idmap_retcode 415cd37da74Snw141292 get_cache_handle(sqlite **cache) 416cd37da74Snw141292 { 417c5c4113dSnw141292 char *errmsg; 41884decf41Sjp151216 idmap_tsd_t *tsd; 419c5c4113dSnw141292 420c5c4113dSnw141292 /* 42184decf41Sjp151216 * Retrieve the db handle from thread-specific storage 422c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 423c5c4113dSnw141292 */ 42484decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 425cd37da74Snw141292 idmapdlog(LOG_ERR, "Error getting thread specific data for %s", 42684decf41Sjp151216 IDMAP_DBNAME); 42784decf41Sjp151216 return (IDMAP_ERR_MEMORY); 42884decf41Sjp151216 } 429c5c4113dSnw141292 43084decf41Sjp151216 if (tsd->cache_db == NULL) { 43184decf41Sjp151216 tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg); 43284decf41Sjp151216 if (tsd->cache_db == NULL) { 433cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 434c5c4113dSnw141292 IDMAP_CACHENAME, CHECK_NULL(errmsg)); 435c5c4113dSnw141292 sqlite_freemem(errmsg); 436cd37da74Snw141292 return (IDMAP_ERR_DB); 437c5c4113dSnw141292 } 438cd37da74Snw141292 43984decf41Sjp151216 tsd->cache_busy.name = IDMAP_CACHENAME; 44084decf41Sjp151216 tsd->cache_busy.delays = cache_delay_table; 44184decf41Sjp151216 tsd->cache_busy.delay_size = sizeof (cache_delay_table) / 44284decf41Sjp151216 sizeof (int); 44384decf41Sjp151216 sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler, 44484decf41Sjp151216 &tsd->cache_busy); 44584decf41Sjp151216 } 44684decf41Sjp151216 *cache = tsd->cache_db; 447c5c4113dSnw141292 return (IDMAP_SUCCESS); 448c5c4113dSnw141292 } 449c5c4113dSnw141292 450c5c4113dSnw141292 /* 451c5c4113dSnw141292 * Initialize cache and db 452c5c4113dSnw141292 */ 453c5c4113dSnw141292 int 454cd37da74Snw141292 init_dbs() 455cd37da74Snw141292 { 45648258c6bSjp151216 char *sql[4]; 457cd37da74Snw141292 int created, upgraded; 458cd37da74Snw141292 459c5c4113dSnw141292 /* name-based mappings; probably OK to blow away in a pinch(?) */ 460cd37da74Snw141292 sql[0] = DB_INSTALL_SQL; 461cd37da74Snw141292 sql[1] = DB_UPGRADE_FROM_v1_SQL; 46248258c6bSjp151216 sql[2] = NULL; 463cd37da74Snw141292 464cd37da74Snw141292 if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql, 465cd37da74Snw141292 FAIL_IF_CORRUPT, &created, &upgraded) < 0) 466c5c4113dSnw141292 return (-1); 467c5c4113dSnw141292 468c5c4113dSnw141292 /* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */ 469cd37da74Snw141292 sql[0] = CACHE_INSTALL_SQL; 470cd37da74Snw141292 sql[1] = CACHE_UPGRADE_FROM_v1_SQL; 47148258c6bSjp151216 sql[2] = CACHE_UPGRADE_FROM_v2_SQL; 47248258c6bSjp151216 sql[3] = NULL; 47348258c6bSjp151216 474cd37da74Snw141292 if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL, 475cd37da74Snw141292 sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0) 476c5c4113dSnw141292 return (-1); 477c5c4113dSnw141292 478cd37da74Snw141292 _idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0; 479cd37da74Snw141292 480c5c4113dSnw141292 return (0); 481c5c4113dSnw141292 } 482c5c4113dSnw141292 483c5c4113dSnw141292 /* 484c5c4113dSnw141292 * Finalize databases 485c5c4113dSnw141292 */ 486c5c4113dSnw141292 void 487cd37da74Snw141292 fini_dbs() 488cd37da74Snw141292 { 489c5c4113dSnw141292 } 490c5c4113dSnw141292 491c5c4113dSnw141292 /* 492e8c27ec8Sbaban * This table is a listing of status codes that will be returned to the 493c5c4113dSnw141292 * client when a SQL command fails with the corresponding error message. 494c5c4113dSnw141292 */ 495c5c4113dSnw141292 static msg_table_t sqlmsgtable[] = { 49662c60062Sbaban {IDMAP_ERR_U2W_NAMERULE_CONFLICT, 497c5c4113dSnw141292 "columns unixname, is_user, u2w_order are not unique"}, 49862c60062Sbaban {IDMAP_ERR_W2U_NAMERULE_CONFLICT, 499cd37da74Snw141292 "columns winname, windomain, is_user, is_wuser, w2u_order are not" 500cd37da74Snw141292 " unique"}, 501cd37da74Snw141292 {IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"}, 502c5c4113dSnw141292 {-1, NULL} 503c5c4113dSnw141292 }; 504c5c4113dSnw141292 505c5c4113dSnw141292 /* 506c5c4113dSnw141292 * idmapd's version of string2stat to map SQLite messages to 507c5c4113dSnw141292 * status codes 508c5c4113dSnw141292 */ 509c5c4113dSnw141292 idmap_retcode 510cd37da74Snw141292 idmapd_string2stat(const char *msg) 511cd37da74Snw141292 { 512c5c4113dSnw141292 int i; 513c5c4113dSnw141292 for (i = 0; sqlmsgtable[i].msg; i++) { 514c5c4113dSnw141292 if (strcasecmp(sqlmsgtable[i].msg, msg) == 0) 515c5c4113dSnw141292 return (sqlmsgtable[i].retcode); 516c5c4113dSnw141292 } 517c5c4113dSnw141292 return (IDMAP_ERR_OTHER); 518c5c4113dSnw141292 } 519c5c4113dSnw141292 520c5c4113dSnw141292 /* 521cd37da74Snw141292 * Executes some SQL in a transaction. 522cd37da74Snw141292 * 523cd37da74Snw141292 * Returns 0 on success, -1 if it failed but the rollback succeeded, -2 524cd37da74Snw141292 * if the rollback failed. 525cd37da74Snw141292 */ 526cd37da74Snw141292 static 527cd37da74Snw141292 int 528cd37da74Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 529cd37da74Snw141292 const char *while_doing) 530cd37da74Snw141292 { 531cd37da74Snw141292 char *errmsg = NULL; 532cd37da74Snw141292 int rc; 533cd37da74Snw141292 534cd37da74Snw141292 rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg); 535cd37da74Snw141292 if (rc != SQLITE_OK) { 536cd37da74Snw141292 idmapdlog(LOG_ERR, "Begin transaction failed (%s) " 537cd37da74Snw141292 "while %s (%s)", errmsg, while_doing, dbname); 538cd37da74Snw141292 sqlite_freemem(errmsg); 539cd37da74Snw141292 return (-1); 540cd37da74Snw141292 } 541cd37da74Snw141292 542cd37da74Snw141292 rc = sqlite_exec(db, sql, NULL, NULL, &errmsg); 543cd37da74Snw141292 if (rc != SQLITE_OK) { 544cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg, 545cd37da74Snw141292 while_doing, dbname); 546cd37da74Snw141292 sqlite_freemem(errmsg); 547cd37da74Snw141292 errmsg = NULL; 548cd37da74Snw141292 goto rollback; 549cd37da74Snw141292 } 550cd37da74Snw141292 551cd37da74Snw141292 rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg); 552cd37da74Snw141292 if (rc == SQLITE_OK) { 553cd37da74Snw141292 sqlite_freemem(errmsg); 554cd37da74Snw141292 return (0); 555cd37da74Snw141292 } 556cd37da74Snw141292 557cd37da74Snw141292 idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)", 558cd37da74Snw141292 errmsg, while_doing, dbname); 559cd37da74Snw141292 sqlite_freemem(errmsg); 560cd37da74Snw141292 errmsg = NULL; 561cd37da74Snw141292 562cd37da74Snw141292 rollback: 563cd37da74Snw141292 rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg); 564cd37da74Snw141292 if (rc != SQLITE_OK) { 565cd37da74Snw141292 idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)", 566cd37da74Snw141292 errmsg, while_doing, dbname); 567cd37da74Snw141292 sqlite_freemem(errmsg); 568cd37da74Snw141292 return (-2); 569cd37da74Snw141292 } 570cd37da74Snw141292 sqlite_freemem(errmsg); 571cd37da74Snw141292 572cd37da74Snw141292 return (-1); 573cd37da74Snw141292 } 574cd37da74Snw141292 575cd37da74Snw141292 /* 576c5c4113dSnw141292 * Execute the given SQL statment without using any callbacks 577c5c4113dSnw141292 */ 578c5c4113dSnw141292 idmap_retcode 57971590c90Snw141292 sql_exec_no_cb(sqlite *db, const char *dbname, char *sql) 580cd37da74Snw141292 { 581c5c4113dSnw141292 char *errmsg = NULL; 58284decf41Sjp151216 int r; 583c5c4113dSnw141292 idmap_retcode retcode; 584c5c4113dSnw141292 585c5c4113dSnw141292 r = sqlite_exec(db, sql, NULL, NULL, &errmsg); 58684decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 587c5c4113dSnw141292 588c5c4113dSnw141292 if (r != SQLITE_OK) { 58971590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing %s " 59071590c90Snw141292 "(%s)", dbname, sql, CHECK_NULL(errmsg)); 591c5c4113dSnw141292 retcode = idmapd_string2stat(errmsg); 59262c60062Sbaban if (errmsg != NULL) 593c5c4113dSnw141292 sqlite_freemem(errmsg); 594c5c4113dSnw141292 return (retcode); 595c5c4113dSnw141292 } 596c5c4113dSnw141292 597c5c4113dSnw141292 return (IDMAP_SUCCESS); 598c5c4113dSnw141292 } 599c5c4113dSnw141292 600c5c4113dSnw141292 /* 601c5c4113dSnw141292 * Generate expression that can be used in WHERE statements. 602c5c4113dSnw141292 * Examples: 603c5c4113dSnw141292 * <prefix> <col> <op> <value> <suffix> 604c5c4113dSnw141292 * "" "unixuser" "=" "foo" "AND" 605c5c4113dSnw141292 */ 606c5c4113dSnw141292 idmap_retcode 607cd37da74Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out) 608cd37da74Snw141292 { 609cd37da74Snw141292 char *s_windomain = NULL, *s_winname = NULL; 610cd37da74Snw141292 char *s_unixname = NULL; 611cd37da74Snw141292 char *lower_winname; 612cd37da74Snw141292 int retcode = IDMAP_SUCCESS; 613cd37da74Snw141292 614c5c4113dSnw141292 if (out == NULL) 615c5c4113dSnw141292 return (IDMAP_ERR_ARG); 616c5c4113dSnw141292 617c5c4113dSnw141292 618cd37da74Snw141292 if (!EMPTY_STRING(rule->windomain)) { 619cd37da74Snw141292 s_windomain = sqlite_mprintf("AND windomain = %Q ", 620cd37da74Snw141292 rule->windomain); 621cd37da74Snw141292 if (s_windomain == NULL) { 622cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 623cd37da74Snw141292 goto out; 624c5c4113dSnw141292 } 625cd37da74Snw141292 } 626cd37da74Snw141292 627cd37da74Snw141292 if (!EMPTY_STRING(rule->winname)) { 628cd37da74Snw141292 if ((lower_winname = tolower_u8(rule->winname)) == NULL) 629cd37da74Snw141292 lower_winname = rule->winname; 630cd37da74Snw141292 s_winname = sqlite_mprintf( 631cd37da74Snw141292 "AND winname = %Q AND is_wuser = %d ", 632cd37da74Snw141292 lower_winname, rule->is_wuser ? 1 : 0); 633cd37da74Snw141292 if (lower_winname != rule->winname) 634cd37da74Snw141292 free(lower_winname); 635cd37da74Snw141292 if (s_winname == NULL) { 636cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 637cd37da74Snw141292 goto out; 638cd37da74Snw141292 } 639cd37da74Snw141292 } 640cd37da74Snw141292 641cd37da74Snw141292 if (!EMPTY_STRING(rule->unixname)) { 642cd37da74Snw141292 s_unixname = sqlite_mprintf( 643cd37da74Snw141292 "AND unixname = %Q AND is_user = %d ", 644cd37da74Snw141292 rule->unixname, rule->is_user ? 1 : 0); 645cd37da74Snw141292 if (s_unixname == NULL) { 646cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 647cd37da74Snw141292 goto out; 648cd37da74Snw141292 } 649cd37da74Snw141292 } 650cd37da74Snw141292 651cd37da74Snw141292 *out = sqlite_mprintf("%s %s %s", 652cd37da74Snw141292 s_windomain ? s_windomain : "", 653cd37da74Snw141292 s_winname ? s_winname : "", 654cd37da74Snw141292 s_unixname ? s_unixname : ""); 655cd37da74Snw141292 656cd37da74Snw141292 if (*out == NULL) { 657cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 658cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 659cd37da74Snw141292 goto out; 660cd37da74Snw141292 } 661cd37da74Snw141292 662cd37da74Snw141292 out: 663cd37da74Snw141292 if (s_windomain != NULL) 664cd37da74Snw141292 sqlite_freemem(s_windomain); 665cd37da74Snw141292 if (s_winname != NULL) 666cd37da74Snw141292 sqlite_freemem(s_winname); 667cd37da74Snw141292 if (s_unixname != NULL) 668cd37da74Snw141292 sqlite_freemem(s_unixname); 669cd37da74Snw141292 670cd37da74Snw141292 return (retcode); 671cd37da74Snw141292 } 672cd37da74Snw141292 673cd37da74Snw141292 674c5c4113dSnw141292 675c5c4113dSnw141292 /* 676c5c4113dSnw141292 * Generate and execute SQL statement for LIST RPC calls 677c5c4113dSnw141292 */ 678c5c4113dSnw141292 idmap_retcode 67971590c90Snw141292 process_list_svc_sql(sqlite *db, const char *dbname, char *sql, uint64_t limit, 68048258c6bSjp151216 int flag, list_svc_cb cb, void *result) 681cd37da74Snw141292 { 682c5c4113dSnw141292 list_cb_data_t cb_data; 683c5c4113dSnw141292 char *errmsg = NULL; 68484decf41Sjp151216 int r; 685c5c4113dSnw141292 idmap_retcode retcode = IDMAP_ERR_INTERNAL; 686c5c4113dSnw141292 687c5c4113dSnw141292 (void) memset(&cb_data, 0, sizeof (cb_data)); 688c5c4113dSnw141292 cb_data.result = result; 689c5c4113dSnw141292 cb_data.limit = limit; 69048258c6bSjp151216 cb_data.flag = flag; 691c5c4113dSnw141292 69284decf41Sjp151216 693c5c4113dSnw141292 r = sqlite_exec(db, sql, cb, &cb_data, &errmsg); 69484decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 695c5c4113dSnw141292 switch (r) { 696c5c4113dSnw141292 case SQLITE_OK: 697c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 69884decf41Sjp151216 break; 69984decf41Sjp151216 700c5c4113dSnw141292 default: 701c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 70271590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing " 70371590c90Snw141292 "%s (%s)", dbname, sql, CHECK_NULL(errmsg)); 70484decf41Sjp151216 break; 705c5c4113dSnw141292 } 70662c60062Sbaban if (errmsg != NULL) 707c5c4113dSnw141292 sqlite_freemem(errmsg); 708c5c4113dSnw141292 return (retcode); 709c5c4113dSnw141292 } 710c5c4113dSnw141292 711c5c4113dSnw141292 /* 712c5c4113dSnw141292 * This routine is called by callbacks that process the results of 713c5c4113dSnw141292 * LIST RPC calls to validate data and to allocate memory for 714c5c4113dSnw141292 * the result array. 715c5c4113dSnw141292 */ 716c5c4113dSnw141292 idmap_retcode 717c5c4113dSnw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv, 718cd37da74Snw141292 int ncol, uchar_t **list, size_t valsize) 719cd37da74Snw141292 { 720c5c4113dSnw141292 size_t nsize; 721c5c4113dSnw141292 void *tmplist; 722c5c4113dSnw141292 723c5c4113dSnw141292 if (cb_data->limit > 0 && cb_data->next == cb_data->limit) 724c5c4113dSnw141292 return (IDMAP_NEXT); 725c5c4113dSnw141292 726c5c4113dSnw141292 if (argc < ncol || argv == NULL) { 727c5c4113dSnw141292 idmapdlog(LOG_ERR, "Invalid data"); 728c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 729c5c4113dSnw141292 } 730c5c4113dSnw141292 731c5c4113dSnw141292 /* alloc in bulk to reduce number of reallocs */ 732c5c4113dSnw141292 if (cb_data->next >= cb_data->len) { 733c5c4113dSnw141292 nsize = (cb_data->len + SIZE_INCR) * valsize; 734c5c4113dSnw141292 tmplist = realloc(*list, nsize); 735c5c4113dSnw141292 if (tmplist == NULL) { 736c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 737c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 738c5c4113dSnw141292 } 739c5c4113dSnw141292 *list = tmplist; 740c5c4113dSnw141292 (void) memset(*list + (cb_data->len * valsize), 0, 741c5c4113dSnw141292 SIZE_INCR * valsize); 742c5c4113dSnw141292 cb_data->len += SIZE_INCR; 743c5c4113dSnw141292 } 744c5c4113dSnw141292 return (IDMAP_SUCCESS); 745c5c4113dSnw141292 } 746c5c4113dSnw141292 747cd37da74Snw141292 static 748cd37da74Snw141292 idmap_retcode 749c5c4113dSnw141292 get_namerule_order(char *winname, char *windomain, char *unixname, 750cd37da74Snw141292 int direction, int is_diagonal, int *w2u_order, int *u2w_order) 751cd37da74Snw141292 { 752c5c4113dSnw141292 *w2u_order = 0; 753c5c4113dSnw141292 *u2w_order = 0; 754c5c4113dSnw141292 755c5c4113dSnw141292 /* 756c5c4113dSnw141292 * Windows to UNIX lookup order: 757c5c4113dSnw141292 * 1. winname@domain (or winname) to "" 758c5c4113dSnw141292 * 2. winname@domain (or winname) to unixname 759c5c4113dSnw141292 * 3. winname@* to "" 760c5c4113dSnw141292 * 4. winname@* to unixname 761c5c4113dSnw141292 * 5. *@domain (or *) to * 762c5c4113dSnw141292 * 6. *@domain (or *) to "" 763c5c4113dSnw141292 * 7. *@domain (or *) to unixname 764c5c4113dSnw141292 * 8. *@* to * 765c5c4113dSnw141292 * 9. *@* to "" 766c5c4113dSnw141292 * 10. *@* to unixname 767c5c4113dSnw141292 * 768c5c4113dSnw141292 * winname is a special case of winname@domain when domain is the 769c5c4113dSnw141292 * default domain. Similarly * is a special case of *@domain when 770c5c4113dSnw141292 * domain is the default domain. 771c5c4113dSnw141292 * 772c5c4113dSnw141292 * Note that "" has priority over specific names because "" inhibits 773c5c4113dSnw141292 * mappings and traditionally deny rules always had higher priority. 774c5c4113dSnw141292 */ 775651c0131Sbaban if (direction != IDMAP_DIRECTION_U2W) { 776651c0131Sbaban /* bi-directional or from windows to unix */ 777c5c4113dSnw141292 if (winname == NULL) 778c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 779c5c4113dSnw141292 else if (unixname == NULL) 780c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 781c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 782c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 783c5c4113dSnw141292 else if (*winname == '*' && windomain && *windomain == '*') { 784c5c4113dSnw141292 if (*unixname == '*') 785c5c4113dSnw141292 *w2u_order = 8; 786c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 787c5c4113dSnw141292 *w2u_order = 9; 788c5c4113dSnw141292 else /* unixname == name */ 789c5c4113dSnw141292 *w2u_order = 10; 790c5c4113dSnw141292 } else if (*winname == '*') { 791c5c4113dSnw141292 if (*unixname == '*') 792c5c4113dSnw141292 *w2u_order = 5; 793c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 794c5c4113dSnw141292 *w2u_order = 6; 795c5c4113dSnw141292 else /* name */ 796c5c4113dSnw141292 *w2u_order = 7; 79762c60062Sbaban } else if (windomain != NULL && *windomain == '*') { 798c5c4113dSnw141292 /* winname == name */ 799c5c4113dSnw141292 if (*unixname == '*') 800c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 801c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 802c5c4113dSnw141292 *w2u_order = 3; 803c5c4113dSnw141292 else /* name */ 804c5c4113dSnw141292 *w2u_order = 4; 805c5c4113dSnw141292 } else { 806c5c4113dSnw141292 /* winname == name && windomain == null or name */ 807c5c4113dSnw141292 if (*unixname == '*') 808c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 809c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 810c5c4113dSnw141292 *w2u_order = 1; 811c5c4113dSnw141292 else /* name */ 812c5c4113dSnw141292 *w2u_order = 2; 813c5c4113dSnw141292 } 814cd37da74Snw141292 815c5c4113dSnw141292 } 816c5c4113dSnw141292 817c5c4113dSnw141292 /* 818cd37da74Snw141292 * 1. unixname to "", non-diagonal 819cd37da74Snw141292 * 2. unixname to winname@domain (or winname), non-diagonal 820cd37da74Snw141292 * 3. unixname to "", diagonal 821cd37da74Snw141292 * 4. unixname to winname@domain (or winname), diagonal 822cd37da74Snw141292 * 5. * to *@domain (or *), non-diagonal 823cd37da74Snw141292 * 5. * to *@domain (or *), diagonal 824cd37da74Snw141292 * 7. * to "" 825cd37da74Snw141292 * 8. * to winname@domain (or winname) 826cd37da74Snw141292 * 9. * to "", non-diagonal 827cd37da74Snw141292 * 10. * to winname@domain (or winname), diagonal 828c5c4113dSnw141292 */ 829651c0131Sbaban if (direction != IDMAP_DIRECTION_W2U) { 830cd37da74Snw141292 int diagonal = is_diagonal ? 1 : 0; 831cd37da74Snw141292 832651c0131Sbaban /* bi-directional or from unix to windows */ 833c5c4113dSnw141292 if (unixname == NULL || EMPTY_NAME(unixname)) 834c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 835c5c4113dSnw141292 else if (winname == NULL) 836c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 83762c60062Sbaban else if (windomain != NULL && *windomain == '*') 838651c0131Sbaban return (IDMAP_ERR_U2W_NAMERULE); 839c5c4113dSnw141292 else if (*unixname == '*') { 840c5c4113dSnw141292 if (*winname == '*') 841cd37da74Snw141292 *u2w_order = 5 + diagonal; 842c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 843cd37da74Snw141292 *u2w_order = 7 + 2 * diagonal; 844c5c4113dSnw141292 else 845cd37da74Snw141292 *u2w_order = 8 + 2 * diagonal; 846c5c4113dSnw141292 } else { 847c5c4113dSnw141292 if (*winname == '*') 848c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 849c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 850cd37da74Snw141292 *u2w_order = 1 + 2 * diagonal; 851c5c4113dSnw141292 else 852cd37da74Snw141292 *u2w_order = 2 + 2 * diagonal; 853c5c4113dSnw141292 } 854c5c4113dSnw141292 } 855c5c4113dSnw141292 return (IDMAP_SUCCESS); 856c5c4113dSnw141292 } 857c5c4113dSnw141292 858c5c4113dSnw141292 /* 859c5c4113dSnw141292 * Generate and execute SQL statement to add name-based mapping rule 860c5c4113dSnw141292 */ 861c5c4113dSnw141292 idmap_retcode 862cd37da74Snw141292 add_namerule(sqlite *db, idmap_namerule *rule) 863cd37da74Snw141292 { 864c5c4113dSnw141292 char *sql = NULL; 865c5c4113dSnw141292 idmap_stat retcode; 8668e228215Sdm199847 char *dom = NULL; 867c5c4113dSnw141292 int w2u_order, u2w_order; 868c5c4113dSnw141292 char w2ubuf[11], u2wbuf[11]; 869c5c4113dSnw141292 8708e228215Sdm199847 retcode = get_namerule_order(rule->winname, rule->windomain, 871cd37da74Snw141292 rule->unixname, rule->direction, 872cd37da74Snw141292 rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order); 873c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 874c5c4113dSnw141292 goto out; 875c5c4113dSnw141292 876c5c4113dSnw141292 if (w2u_order) 877c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order); 878c5c4113dSnw141292 if (u2w_order) 879c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order); 880c5c4113dSnw141292 88162c60062Sbaban /* 88262c60062Sbaban * For the triggers on namerules table to work correctly: 88362c60062Sbaban * 1) Use NULL instead of 0 for w2u_order and u2w_order 88462c60062Sbaban * 2) Use "" instead of NULL for "no domain" 88562c60062Sbaban */ 88662c60062Sbaban 887e8c27ec8Sbaban if (!EMPTY_STRING(rule->windomain)) 8888e228215Sdm199847 dom = rule->windomain; 889cd37da74Snw141292 else if (lookup_wksids_name2sid(rule->winname, NULL, NULL, NULL, NULL) 89062c60062Sbaban == IDMAP_SUCCESS) { 89162c60062Sbaban /* well-known SIDs don't need domain */ 89262c60062Sbaban dom = ""; 89362c60062Sbaban } 894c5c4113dSnw141292 895c5c4113dSnw141292 RDLOCK_CONFIG(); 89662c60062Sbaban if (dom == NULL) { 897c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain) 898c8e26105Sjp151216 dom = _idmapdstate.cfg->pgcfg.default_domain; 899c5c4113dSnw141292 else 900c5c4113dSnw141292 dom = ""; 90162c60062Sbaban } 90284decf41Sjp151216 sql = sqlite_mprintf("INSERT into namerules " 903cd37da74Snw141292 "(is_user, is_wuser, windomain, winname_display, is_nt4, " 904c5c4113dSnw141292 "unixname, w2u_order, u2w_order) " 905cd37da74Snw141292 "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);", 906cd37da74Snw141292 rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom, 907cd37da74Snw141292 rule->winname, rule->is_nt4 ? 1 : 0, rule->unixname, 908cd37da74Snw141292 w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL); 909c5c4113dSnw141292 UNLOCK_CONFIG(); 910c5c4113dSnw141292 911c5c4113dSnw141292 if (sql == NULL) { 912c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 913c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 914c5c4113dSnw141292 goto out; 915c5c4113dSnw141292 } 916c5c4113dSnw141292 91771590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 918c5c4113dSnw141292 919c5c4113dSnw141292 if (retcode == IDMAP_ERR_OTHER) 920c5c4113dSnw141292 retcode = IDMAP_ERR_CFG; 921c5c4113dSnw141292 922c5c4113dSnw141292 out: 92362c60062Sbaban if (sql != NULL) 924c5c4113dSnw141292 sqlite_freemem(sql); 925c5c4113dSnw141292 return (retcode); 926c5c4113dSnw141292 } 927c5c4113dSnw141292 928c5c4113dSnw141292 /* 929c5c4113dSnw141292 * Flush name-based mapping rules 930c5c4113dSnw141292 */ 931c5c4113dSnw141292 idmap_retcode 932cd37da74Snw141292 flush_namerules(sqlite *db) 933cd37da74Snw141292 { 934c5c4113dSnw141292 idmap_stat retcode; 935c5c4113dSnw141292 93671590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "DELETE FROM namerules;"); 937c5c4113dSnw141292 938c5c4113dSnw141292 return (retcode); 939c5c4113dSnw141292 } 940c5c4113dSnw141292 941c5c4113dSnw141292 /* 942c5c4113dSnw141292 * Generate and execute SQL statement to remove a name-based mapping rule 943c5c4113dSnw141292 */ 944c5c4113dSnw141292 idmap_retcode 945cd37da74Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule) 946cd37da74Snw141292 { 947c5c4113dSnw141292 char *sql = NULL; 948c5c4113dSnw141292 idmap_stat retcode; 949c5c4113dSnw141292 char buf[80]; 950cd37da74Snw141292 char *expr = NULL; 951c5c4113dSnw141292 9528e228215Sdm199847 if (rule->direction < 0 && EMPTY_STRING(rule->windomain) && 9538e228215Sdm199847 EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname)) 954c5c4113dSnw141292 return (IDMAP_SUCCESS); 955c5c4113dSnw141292 956c5c4113dSnw141292 buf[0] = 0; 957cd37da74Snw141292 958cd37da74Snw141292 if (rule->direction == IDMAP_DIRECTION_BI) 959c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 960c5c4113dSnw141292 " AND u2w_order > 0"); 961cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_W2U) 962c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 963c5c4113dSnw141292 " AND (u2w_order = 0 OR u2w_order ISNULL)"); 964cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_U2W) 965c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND u2w_order > 0" 966c5c4113dSnw141292 " AND (w2u_order = 0 OR w2u_order ISNULL)"); 967c5c4113dSnw141292 968cd37da74Snw141292 retcode = gen_sql_expr_from_rule(rule, &expr); 969cd37da74Snw141292 if (retcode != IDMAP_SUCCESS) 970c5c4113dSnw141292 goto out; 971c5c4113dSnw141292 972cd37da74Snw141292 sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s %s;", expr, 973c5c4113dSnw141292 buf); 974c5c4113dSnw141292 975c5c4113dSnw141292 if (sql == NULL) { 976c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 977c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 978c5c4113dSnw141292 goto out; 979c5c4113dSnw141292 } 980c5c4113dSnw141292 981cd37da74Snw141292 98271590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 983c5c4113dSnw141292 984c5c4113dSnw141292 out: 985cd37da74Snw141292 if (expr != NULL) 986cd37da74Snw141292 sqlite_freemem(expr); 98762c60062Sbaban if (sql != NULL) 988c5c4113dSnw141292 sqlite_freemem(sql); 989c5c4113dSnw141292 return (retcode); 990c5c4113dSnw141292 } 991c5c4113dSnw141292 992c5c4113dSnw141292 /* 993c5c4113dSnw141292 * Compile the given SQL query and step just once. 994c5c4113dSnw141292 * 995c5c4113dSnw141292 * Input: 996c5c4113dSnw141292 * db - db handle 997c5c4113dSnw141292 * sql - SQL statement 998c5c4113dSnw141292 * 999c5c4113dSnw141292 * Output: 1000c5c4113dSnw141292 * vm - virtual SQL machine 1001c5c4113dSnw141292 * ncol - number of columns in the result 1002c5c4113dSnw141292 * values - column values 1003c5c4113dSnw141292 * 1004c5c4113dSnw141292 * Return values: 1005c5c4113dSnw141292 * IDMAP_SUCCESS 1006c5c4113dSnw141292 * IDMAP_ERR_NOTFOUND 1007c5c4113dSnw141292 * IDMAP_ERR_INTERNAL 1008c5c4113dSnw141292 */ 1009c5c4113dSnw141292 1010cd37da74Snw141292 static 1011cd37da74Snw141292 idmap_retcode 1012c5c4113dSnw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol, 1013cd37da74Snw141292 int reqcol, const char ***values) 1014cd37da74Snw141292 { 1015c5c4113dSnw141292 char *errmsg = NULL; 101684decf41Sjp151216 int r; 1017c5c4113dSnw141292 101884decf41Sjp151216 if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) { 1019cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1020cd37da74Snw141292 CHECK_NULL(errmsg)); 1021c5c4113dSnw141292 sqlite_freemem(errmsg); 1022c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1023c5c4113dSnw141292 } 1024c5c4113dSnw141292 1025c5c4113dSnw141292 r = sqlite_step(*vm, ncol, values, NULL); 102684decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 1027c5c4113dSnw141292 102884decf41Sjp151216 if (r == SQLITE_ROW) { 102962c60062Sbaban if (ncol != NULL && *ncol < reqcol) { 1030c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1031c5c4113dSnw141292 *vm = NULL; 1032c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1033c5c4113dSnw141292 } 1034c5c4113dSnw141292 /* Caller will call finalize after using the results */ 1035c5c4113dSnw141292 return (IDMAP_SUCCESS); 1036c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 1037c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1038c5c4113dSnw141292 *vm = NULL; 1039c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1040c5c4113dSnw141292 } 1041c5c4113dSnw141292 1042c5c4113dSnw141292 (void) sqlite_finalize(*vm, &errmsg); 1043c5c4113dSnw141292 *vm = NULL; 1044cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1045cd37da74Snw141292 CHECK_NULL(errmsg)); 1046c5c4113dSnw141292 sqlite_freemem(errmsg); 1047c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1048c5c4113dSnw141292 } 1049c5c4113dSnw141292 105062c60062Sbaban /* 1051479ac375Sdm199847 * Load config in the state. 1052e8c27ec8Sbaban * 1053479ac375Sdm199847 * nm_siduid and nm_sidgid fields: 1054e8c27ec8Sbaban * state->nm_siduid represents mode used by sid2uid and uid2sid 1055e8c27ec8Sbaban * requests for directory-based name mappings. Similarly, 1056e8c27ec8Sbaban * state->nm_sidgid represents mode used by sid2gid and gid2sid 1057e8c27ec8Sbaban * requests. 1058e8c27ec8Sbaban * 1059e8c27ec8Sbaban * sid2uid/uid2sid: 1060e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1061e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixuser_attr 1062e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr 1063e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixuser_attr 1064e8c27ec8Sbaban * 1065e8c27ec8Sbaban * sid2gid/gid2sid: 1066e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1067e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixgroup_attr 1068e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr 1069e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr 1070e8c27ec8Sbaban */ 1071e8c27ec8Sbaban idmap_retcode 1072479ac375Sdm199847 load_cfg_in_state(lookup_state_t *state) 1073e8c27ec8Sbaban { 1074e8c27ec8Sbaban state->nm_siduid = IDMAP_NM_NONE; 1075e8c27ec8Sbaban state->nm_sidgid = IDMAP_NM_NONE; 1076e8c27ec8Sbaban RDLOCK_CONFIG(); 1077479ac375Sdm199847 10784aa0a5e7Snw141292 state->eph_map_unres_sids = 0; 10794aa0a5e7Snw141292 if (_idmapdstate.cfg->pgcfg.eph_map_unres_sids) 10804aa0a5e7Snw141292 state->eph_map_unres_sids = 1; 10814aa0a5e7Snw141292 1082479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 1083479ac375Sdm199847 state->defdom = 1084479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.default_domain); 1085479ac375Sdm199847 if (state->defdom == NULL) { 1086479ac375Sdm199847 UNLOCK_CONFIG(); 1087479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1088479ac375Sdm199847 } 1089479ac375Sdm199847 } else { 1090479ac375Sdm199847 UNLOCK_CONFIG(); 1091dc03a638Sdm199847 return (IDMAP_SUCCESS); 1092479ac375Sdm199847 } 1093e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ds_name_mapping_enabled == FALSE) { 1094e8c27ec8Sbaban UNLOCK_CONFIG(); 1095e8c27ec8Sbaban return (IDMAP_SUCCESS); 1096e8c27ec8Sbaban } 1097e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1098e8c27ec8Sbaban state->nm_siduid = 1099e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1100e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1101e8c27ec8Sbaban state->nm_sidgid = 1102e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1103e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1104e8c27ec8Sbaban } else { 1105e8c27ec8Sbaban state->nm_siduid = 1106e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1107e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1108e8c27ec8Sbaban state->nm_sidgid = 1109e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1110e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1111e8c27ec8Sbaban } 1112e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) { 1113e8c27ec8Sbaban state->ad_unixuser_attr = 1114e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr); 1115e8c27ec8Sbaban if (state->ad_unixuser_attr == NULL) { 1116e8c27ec8Sbaban UNLOCK_CONFIG(); 1117e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1118e8c27ec8Sbaban } 1119e8c27ec8Sbaban } 1120e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) { 1121e8c27ec8Sbaban state->ad_unixgroup_attr = 1122e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr); 1123e8c27ec8Sbaban if (state->ad_unixgroup_attr == NULL) { 1124e8c27ec8Sbaban UNLOCK_CONFIG(); 1125e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1126e8c27ec8Sbaban } 1127e8c27ec8Sbaban } 1128479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1129479ac375Sdm199847 state->nldap_winname_attr = 1130479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr); 1131479ac375Sdm199847 if (state->nldap_winname_attr == NULL) { 1132479ac375Sdm199847 UNLOCK_CONFIG(); 1133479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1134479ac375Sdm199847 } 1135479ac375Sdm199847 } 1136e8c27ec8Sbaban UNLOCK_CONFIG(); 1137e8c27ec8Sbaban return (IDMAP_SUCCESS); 1138e8c27ec8Sbaban } 1139e8c27ec8Sbaban 1140e8c27ec8Sbaban /* 114148258c6bSjp151216 * Set the rule with sepecified values. 114248258c6bSjp151216 * All the strings are copied. 114348258c6bSjp151216 */ 114448258c6bSjp151216 static void 114548258c6bSjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain, 114648258c6bSjp151216 const char *winname, const char *unixname, boolean_t is_user, 114748258c6bSjp151216 boolean_t is_wuser, boolean_t is_nt4, int direction) 114848258c6bSjp151216 { 114948258c6bSjp151216 /* 115048258c6bSjp151216 * Only update if they differ because we have to free 115148258c6bSjp151216 * and duplicate the strings 115248258c6bSjp151216 */ 115348258c6bSjp151216 if (rule->windomain == NULL || windomain == NULL || 115448258c6bSjp151216 strcmp(rule->windomain, windomain) != 0) { 115548258c6bSjp151216 if (rule->windomain != NULL) { 115648258c6bSjp151216 free(rule->windomain); 115748258c6bSjp151216 rule->windomain = NULL; 115848258c6bSjp151216 } 115948258c6bSjp151216 if (windomain != NULL) 116048258c6bSjp151216 rule->windomain = strdup(windomain); 116148258c6bSjp151216 } 116248258c6bSjp151216 116348258c6bSjp151216 if (rule->winname == NULL || winname == NULL || 116448258c6bSjp151216 strcmp(rule->winname, winname) != 0) { 116548258c6bSjp151216 if (rule->winname != NULL) { 116648258c6bSjp151216 free(rule->winname); 116748258c6bSjp151216 rule->winname = NULL; 116848258c6bSjp151216 } 116948258c6bSjp151216 if (winname != NULL) 117048258c6bSjp151216 rule->winname = strdup(winname); 117148258c6bSjp151216 } 117248258c6bSjp151216 117348258c6bSjp151216 if (rule->unixname == NULL || unixname == NULL || 117448258c6bSjp151216 strcmp(rule->unixname, unixname) != 0) { 117548258c6bSjp151216 if (rule->unixname != NULL) { 117648258c6bSjp151216 free(rule->unixname); 117748258c6bSjp151216 rule->unixname = NULL; 117848258c6bSjp151216 } 117948258c6bSjp151216 if (unixname != NULL) 118048258c6bSjp151216 rule->unixname = strdup(unixname); 118148258c6bSjp151216 } 118248258c6bSjp151216 118348258c6bSjp151216 rule->is_user = is_user; 118448258c6bSjp151216 rule->is_wuser = is_wuser; 118548258c6bSjp151216 rule->is_nt4 = is_nt4; 118648258c6bSjp151216 rule->direction = direction; 118748258c6bSjp151216 } 118848258c6bSjp151216 118948258c6bSjp151216 119048258c6bSjp151216 /* 119162c60062Sbaban * Table for well-known SIDs. 119262c60062Sbaban * 119362c60062Sbaban * Background: 119462c60062Sbaban * 119576b27f93Sbaban * Some of the well-known principals are stored under: 119662c60062Sbaban * cn=WellKnown Security Principals, cn=Configuration, dc=<forestRootDomain> 119762c60062Sbaban * They belong to objectClass "foreignSecurityPrincipal". They don't have 119862c60062Sbaban * "samAccountName" nor "userPrincipalName" attributes. Their names are 119962c60062Sbaban * available in "cn" and "name" attributes. Some of these principals have a 120062c60062Sbaban * second entry under CN=ForeignSecurityPrincipals,dc=<forestRootDomain> and 120162c60062Sbaban * these duplicate entries have the stringified SID in the "name" and "cn" 120262c60062Sbaban * attributes instead of the actual name. 120362c60062Sbaban * 120476b27f93Sbaban * Those of the form S-1-5-32-X are Builtin groups and are stored in the 120576b27f93Sbaban * cn=builtin container (except, Power Users which is not stored in AD) 120662c60062Sbaban * 120776b27f93Sbaban * These principals are and will remain constant. Therefore doing AD lookups 120876b27f93Sbaban * provides no benefit. Also, using hard-coded table (and thus avoiding AD 120976b27f93Sbaban * lookup) improves performance and avoids additional complexity in the 121076b27f93Sbaban * adutils.c code. Moreover these SIDs can be used when no Active Directory 121176b27f93Sbaban * is available (such as the CIFS server's "workgroup" mode). 121276b27f93Sbaban * 121376b27f93Sbaban * Notes: 121476b27f93Sbaban * 1. Currently we don't support localization of well-known SID names, 121562c60062Sbaban * unlike Windows. 121662c60062Sbaban * 121776b27f93Sbaban * 2. Other well-known SIDs i.e. S-1-5-<domain>-<w-k RID> are not stored 121876b27f93Sbaban * here. AD does have normal user/group objects for these objects and 121976b27f93Sbaban * can be looked up using the existing AD lookup code. 1220e8c27ec8Sbaban * 1221e8c27ec8Sbaban * 3. See comments above lookup_wksids_sid2pid() for more information 1222e8c27ec8Sbaban * on how we lookup the wksids table. 122362c60062Sbaban */ 122462c60062Sbaban static wksids_table_t wksids[] = { 1225e8c27ec8Sbaban {"S-1-0", 0, "Nobody", 0, SENTINEL_PID, -1, 1}, 1226e8c27ec8Sbaban {"S-1-1", 0, "Everyone", 0, SENTINEL_PID, -1, -1}, 1227e8c27ec8Sbaban {"S-1-3", 0, "Creator Owner", 1, IDMAP_WK_CREATOR_OWNER_UID, 1, 0}, 1228e8c27ec8Sbaban {"S-1-3", 1, "Creator Group", 0, IDMAP_WK_CREATOR_GROUP_GID, 0, 0}, 1229e8c27ec8Sbaban {"S-1-3", 2, "Creator Owner Server", 1, SENTINEL_PID, -1, -1}, 1230e8c27ec8Sbaban {"S-1-3", 3, "Creator Group Server", 0, SENTINEL_PID, -1, 1}, 1231e8c27ec8Sbaban {"S-1-3", 4, "Owner Rights", 0, SENTINEL_PID, -1, -1}, 1232e8c27ec8Sbaban {"S-1-5", 1, "Dialup", 0, SENTINEL_PID, -1, -1}, 1233e8c27ec8Sbaban {"S-1-5", 2, "Network", 0, SENTINEL_PID, -1, -1}, 1234e8c27ec8Sbaban {"S-1-5", 3, "Batch", 0, SENTINEL_PID, -1, -1}, 1235e8c27ec8Sbaban {"S-1-5", 4, "Interactive", 0, SENTINEL_PID, -1, -1}, 1236e8c27ec8Sbaban {"S-1-5", 6, "Service", 0, SENTINEL_PID, -1, -1}, 1237e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, GID_NOBODY, 0, 0}, 1238e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, UID_NOBODY, 1, 0}, 1239e8c27ec8Sbaban {"S-1-5", 8, "Proxy", 0, SENTINEL_PID, -1, -1}, 1240e8c27ec8Sbaban {"S-1-5", 9, "Enterprise Domain Controllers", 0, SENTINEL_PID, -1, -1}, 1241e8c27ec8Sbaban {"S-1-5", 10, "Self", 0, SENTINEL_PID, -1, -1}, 1242e8c27ec8Sbaban {"S-1-5", 11, "Authenticated Users", 0, SENTINEL_PID, -1, -1}, 1243e8c27ec8Sbaban {"S-1-5", 12, "Restricted Code", 0, SENTINEL_PID, -1, -1}, 1244e8c27ec8Sbaban {"S-1-5", 13, "Terminal Server User", 0, SENTINEL_PID, -1, -1}, 1245e8c27ec8Sbaban {"S-1-5", 14, "Remote Interactive Logon", 0, SENTINEL_PID, -1, -1}, 1246e8c27ec8Sbaban {"S-1-5", 15, "This Organization", 0, SENTINEL_PID, -1, -1}, 1247e8c27ec8Sbaban {"S-1-5", 17, "IUSR", 0, SENTINEL_PID, -1, -1}, 1248e8c27ec8Sbaban {"S-1-5", 18, "Local System", 0, IDMAP_WK_LOCAL_SYSTEM_GID, 0, 0}, 1249e8c27ec8Sbaban {"S-1-5", 19, "Local Service", 0, SENTINEL_PID, -1, -1}, 1250e8c27ec8Sbaban {"S-1-5", 20, "Network Service", 0, SENTINEL_PID, -1, -1}, 1251e8c27ec8Sbaban {"S-1-5", 1000, "Other Organization", 0, SENTINEL_PID, -1, -1}, 1252e8c27ec8Sbaban {"S-1-5-32", 544, "Administrators", 0, SENTINEL_PID, -1, -1}, 1253e8c27ec8Sbaban {"S-1-5-32", 545, "Users", 0, SENTINEL_PID, -1, -1}, 1254e8c27ec8Sbaban {"S-1-5-32", 546, "Guests", 0, SENTINEL_PID, -1, -1}, 1255e8c27ec8Sbaban {"S-1-5-32", 547, "Power Users", 0, SENTINEL_PID, -1, -1}, 1256e8c27ec8Sbaban {"S-1-5-32", 548, "Account Operators", 0, SENTINEL_PID, -1, -1}, 1257e8c27ec8Sbaban {"S-1-5-32", 549, "Server Operators", 0, SENTINEL_PID, -1, -1}, 1258e8c27ec8Sbaban {"S-1-5-32", 550, "Print Operators", 0, SENTINEL_PID, -1, -1}, 1259e8c27ec8Sbaban {"S-1-5-32", 551, "Backup Operators", 0, SENTINEL_PID, -1, -1}, 1260e8c27ec8Sbaban {"S-1-5-32", 552, "Replicator", 0, SENTINEL_PID, -1, -1}, 126176b27f93Sbaban {"S-1-5-32", 554, "Pre-Windows 2000 Compatible Access", 0, 1262e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1263e8c27ec8Sbaban {"S-1-5-32", 555, "Remote Desktop Users", 0, SENTINEL_PID, -1, -1}, 126476b27f93Sbaban {"S-1-5-32", 556, "Network Configuration Operators", 0, 1265e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 126676b27f93Sbaban {"S-1-5-32", 557, "Incoming Forest Trust Builders", 0, 1267e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1268e8c27ec8Sbaban {"S-1-5-32", 558, "Performance Monitor Users", 0, SENTINEL_PID, -1, -1}, 1269e8c27ec8Sbaban {"S-1-5-32", 559, "Performance Log Users", 0, SENTINEL_PID, -1, -1}, 127076b27f93Sbaban {"S-1-5-32", 560, "Windows Authorization Access Group", 0, 1271e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 127276b27f93Sbaban {"S-1-5-32", 561, "Terminal Server License Servers", 0, 1273e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1274e8c27ec8Sbaban {"S-1-5-32", 561, "Distributed COM Users", 0, SENTINEL_PID, -1, -1}, 1275e8c27ec8Sbaban {"S-1-5-32", 568, "IIS_IUSRS", 0, SENTINEL_PID, -1, -1}, 1276e8c27ec8Sbaban {"S-1-5-32", 569, "Cryptographic Operators", 0, SENTINEL_PID, -1, -1}, 1277e8c27ec8Sbaban {"S-1-5-32", 573, "Event Log Readers", 0, SENTINEL_PID, -1, -1}, 127876b27f93Sbaban {"S-1-5-32", 574, "Certificate Service DCOM Access", 0, 1279e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1280e8c27ec8Sbaban {"S-1-5-64", 21, "Digest Authentication", 0, SENTINEL_PID, -1, -1}, 1281e8c27ec8Sbaban {"S-1-5-64", 10, "NTLM Authentication", 0, SENTINEL_PID, -1, -1}, 1282e8c27ec8Sbaban {"S-1-5-64", 14, "SChannel Authentication", 0, SENTINEL_PID, -1, -1}, 1283e8c27ec8Sbaban {NULL, UINT32_MAX, NULL, -1, SENTINEL_PID, -1, -1} 1284c5c4113dSnw141292 }; 1285c5c4113dSnw141292 1286e8c27ec8Sbaban /* 1287e8c27ec8Sbaban * Lookup well-known SIDs table either by winname or by SID. 1288e8c27ec8Sbaban * If the given winname or SID is a well-known SID then we set wksid 1289e8c27ec8Sbaban * variable and then proceed to see if the SID has a hard mapping to 1290e8c27ec8Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to 1291e8c27ec8Sbaban * fixed ephemeral ids). If we find such mapping then we return 1292e8c27ec8Sbaban * success otherwise notfound. If a well-known SID is mapped to 1293e8c27ec8Sbaban * SENTINEL_PID and the direction field is set (bi-directional or 1294e8c27ec8Sbaban * win2unix) then we treat it as inhibited mapping and return no 1295e8c27ec8Sbaban * mapping (Ex. S-1-0-0). 1296e8c27ec8Sbaban */ 1297cd37da74Snw141292 static 1298cd37da74Snw141292 idmap_retcode 1299e8c27ec8Sbaban lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *wksid) 1300cd37da74Snw141292 { 1301c5c4113dSnw141292 int i; 130262c60062Sbaban 1303e8c27ec8Sbaban *wksid = 0; 1304e8c27ec8Sbaban 1305e8c27ec8Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1306e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1307e8c27ec8Sbaban if ((strcasecmp(wksids[i].sidprefix, 1308e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix) != 0) || 1309e8c27ec8Sbaban wksids[i].rid != req->id1.idmap_id_u.sid.rid) 1310e8c27ec8Sbaban /* this is not our SID */ 1311e8c27ec8Sbaban continue; 1312e8c27ec8Sbaban if (req->id1name == NULL) { 1313e8c27ec8Sbaban req->id1name = strdup(wksids[i].winname); 1314e8c27ec8Sbaban if (req->id1name == NULL) 1315e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1316e8c27ec8Sbaban } 1317e8c27ec8Sbaban } else if (req->id1name != NULL) { 1318e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, req->id1name) != 0) 1319e8c27ec8Sbaban /* this is not our winname */ 1320e8c27ec8Sbaban continue; 1321e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = 1322e8c27ec8Sbaban strdup(wksids[i].sidprefix); 1323e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) 1324e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1325e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = wksids[i].rid; 1326e8c27ec8Sbaban } 1327e8c27ec8Sbaban 1328e8c27ec8Sbaban *wksid = 1; 1329e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1330e8c27ec8Sbaban 1331e8c27ec8Sbaban req->id1.idtype = (wksids[i].is_wuser) ? 1332e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1333e8c27ec8Sbaban 1334e8c27ec8Sbaban if (wksids[i].pid == SENTINEL_PID) { 1335e8c27ec8Sbaban if (wksids[i].direction == IDMAP_DIRECTION_BI || 1336e8c27ec8Sbaban wksids[i].direction == IDMAP_DIRECTION_W2U) 1337e8c27ec8Sbaban /* Inhibited */ 1338e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 1339e8c27ec8Sbaban /* Not mapped */ 1340479ac375Sdm199847 if (res->id.idtype == IDMAP_POSIXID) { 1341479ac375Sdm199847 res->id.idtype = 1342479ac375Sdm199847 (wksids[i].is_wuser) ? 1343479ac375Sdm199847 IDMAP_UID : IDMAP_GID; 1344479ac375Sdm199847 } 1345e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 1346e8c27ec8Sbaban } else if (wksids[i].direction == IDMAP_DIRECTION_U2W) 134762c60062Sbaban continue; 134862c60062Sbaban 1349e8c27ec8Sbaban switch (res->id.idtype) { 1350c5c4113dSnw141292 case IDMAP_UID: 135162c60062Sbaban if (wksids[i].is_user == 0) 135262c60062Sbaban continue; 135362c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 135462c60062Sbaban res->direction = wksids[i].direction; 135548258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 135648258c6bSjp151216 res->info.how.map_type = 135748258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 135848258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 135948258c6bSjp151216 } 1360c5c4113dSnw141292 return (IDMAP_SUCCESS); 1361c5c4113dSnw141292 case IDMAP_GID: 136262c60062Sbaban if (wksids[i].is_user == 1) 136362c60062Sbaban continue; 136462c60062Sbaban res->id.idmap_id_u.gid = wksids[i].pid; 136562c60062Sbaban res->direction = wksids[i].direction; 136648258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 136748258c6bSjp151216 res->info.how.map_type = 136848258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 136948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 137048258c6bSjp151216 } 1371c5c4113dSnw141292 return (IDMAP_SUCCESS); 1372c5c4113dSnw141292 case IDMAP_POSIXID: 137362c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 137462c60062Sbaban res->id.idtype = (!wksids[i].is_user) ? 1375c5c4113dSnw141292 IDMAP_GID : IDMAP_UID; 137662c60062Sbaban res->direction = wksids[i].direction; 137748258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 137848258c6bSjp151216 res->info.how.map_type = 137948258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 138048258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 138148258c6bSjp151216 } 1382c5c4113dSnw141292 return (IDMAP_SUCCESS); 1383c5c4113dSnw141292 default: 1384c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 1385c5c4113dSnw141292 } 1386c5c4113dSnw141292 } 1387c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1388c5c4113dSnw141292 } 1389c5c4113dSnw141292 1390cd37da74Snw141292 1391cd37da74Snw141292 static 1392cd37da74Snw141292 idmap_retcode 1393cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user) 1394cd37da74Snw141292 { 1395c5c4113dSnw141292 int i; 1396e8c27ec8Sbaban if (req->id1.idmap_id_u.uid == SENTINEL_PID) 1397e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 139862c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 139962c60062Sbaban if (wksids[i].pid == req->id1.idmap_id_u.uid && 140062c60062Sbaban wksids[i].is_user == is_user && 140162c60062Sbaban wksids[i].direction != IDMAP_DIRECTION_W2U) { 1402e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) { 1403e8c27ec8Sbaban res->id.idtype = (wksids[i].is_wuser) ? 1404e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1405e8c27ec8Sbaban } 140662c60062Sbaban res->id.idmap_id_u.sid.rid = wksids[i].rid; 1407c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 140862c60062Sbaban strdup(wksids[i].sidprefix); 1409c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 1410c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1411c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 1412c5c4113dSnw141292 } 141362c60062Sbaban res->direction = wksids[i].direction; 141448258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 141548258c6bSjp151216 res->info.how.map_type = 141648258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 141748258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 141848258c6bSjp151216 } 1419c5c4113dSnw141292 return (IDMAP_SUCCESS); 1420c5c4113dSnw141292 } 1421c5c4113dSnw141292 } 142262c60062Sbaban return (IDMAP_ERR_NOTFOUND); 142362c60062Sbaban } 142462c60062Sbaban 1425cd37da74Snw141292 idmap_retcode 1426cd37da74Snw141292 lookup_wksids_name2sid(const char *name, char **canonname, char **sidprefix, 1427cd37da74Snw141292 idmap_rid_t *rid, int *type) 1428cd37da74Snw141292 { 142962c60062Sbaban int i; 1430479ac375Sdm199847 1431479ac375Sdm199847 if ((strncasecmp(name, "BUILTIN\\", 8) == 0) || 1432479ac375Sdm199847 (strncasecmp(name, "BUILTIN/", 8) == 0)) 1433479ac375Sdm199847 name += 8; 1434479ac375Sdm199847 143562c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1436e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, name) != 0) 1437e8c27ec8Sbaban continue; 1438e8c27ec8Sbaban if (sidprefix != NULL && 1439e8c27ec8Sbaban (*sidprefix = strdup(wksids[i].sidprefix)) == NULL) { 144062c60062Sbaban idmapdlog(LOG_ERR, "Out of memory"); 144162c60062Sbaban return (IDMAP_ERR_MEMORY); 144262c60062Sbaban } 1443cd37da74Snw141292 if (canonname != NULL && 1444cd37da74Snw141292 (*canonname = strdup(wksids[i].winname)) == NULL) { 1445cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1446e8c27ec8Sbaban if (sidprefix != NULL) { 1447e8c27ec8Sbaban free(*sidprefix); 1448e8c27ec8Sbaban *sidprefix = NULL; 1449e8c27ec8Sbaban } 1450cd37da74Snw141292 return (IDMAP_ERR_MEMORY); 1451cd37da74Snw141292 } 145262c60062Sbaban if (type != NULL) 1453e8c27ec8Sbaban *type = (wksids[i].is_wuser) ? 145462c60062Sbaban _IDMAP_T_USER : _IDMAP_T_GROUP; 145562c60062Sbaban if (rid != NULL) 145662c60062Sbaban *rid = wksids[i].rid; 145762c60062Sbaban return (IDMAP_SUCCESS); 145862c60062Sbaban } 1459c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1460c5c4113dSnw141292 } 1461c5c4113dSnw141292 1462cd37da74Snw141292 static 1463cd37da74Snw141292 idmap_retcode 1464cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1465cd37da74Snw141292 { 1466c5c4113dSnw141292 char *end; 1467c5c4113dSnw141292 char *sql = NULL; 1468c5c4113dSnw141292 const char **values; 1469c5c4113dSnw141292 sqlite_vm *vm = NULL; 1470c5c4113dSnw141292 int ncol, is_user; 1471c5c4113dSnw141292 uid_t pid; 1472c5c4113dSnw141292 time_t curtime, exp; 1473c5c4113dSnw141292 idmap_retcode retcode; 1474042addd6Sbaban char *is_user_string, *lower_name; 1475c5c4113dSnw141292 1476c5c4113dSnw141292 /* Current time */ 1477c5c4113dSnw141292 errno = 0; 1478c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1479cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1480c5c4113dSnw141292 strerror(errno)); 1481c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1482c5c4113dSnw141292 goto out; 1483c5c4113dSnw141292 } 1484c5c4113dSnw141292 1485e8c27ec8Sbaban switch (res->id.idtype) { 1486cd37da74Snw141292 case IDMAP_UID: 1487cd37da74Snw141292 is_user_string = "1"; 1488cd37da74Snw141292 break; 1489cd37da74Snw141292 case IDMAP_GID: 1490cd37da74Snw141292 is_user_string = "0"; 1491cd37da74Snw141292 break; 1492cd37da74Snw141292 case IDMAP_POSIXID: 1493cd37da74Snw141292 /* the non-diagonal mapping */ 1494cd37da74Snw141292 is_user_string = "is_wuser"; 1495cd37da74Snw141292 break; 1496cd37da74Snw141292 default: 1497cd37da74Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 1498cd37da74Snw141292 goto out; 1499cd37da74Snw141292 } 1500cd37da74Snw141292 1501c5c4113dSnw141292 /* SQL to lookup the cache */ 150248258c6bSjp151216 1503e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1504e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 150548258c6bSjp151216 "unixname, u2w, is_wuser, " 150648258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 150748258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1508cd37da74Snw141292 "FROM idmap_cache WHERE is_user = %s AND " 1509c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 1510c5c4113dSnw141292 "(pid >= 2147483648 OR " 1511c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1512c5c4113dSnw141292 "expiration > %d));", 1513e8c27ec8Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix, 1514e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, curtime); 1515e8c27ec8Sbaban } else if (req->id1name != NULL) { 1516042addd6Sbaban if ((lower_name = tolower_u8(req->id1name)) == NULL) 1517042addd6Sbaban lower_name = req->id1name; 1518e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 151948258c6bSjp151216 "unixname, u2w, is_wuser, " 152048258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 152148258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1522e8c27ec8Sbaban "FROM idmap_cache WHERE is_user = %s AND " 1523e8c27ec8Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND " 1524e8c27ec8Sbaban "(pid >= 2147483648 OR " 1525e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 1526e8c27ec8Sbaban "expiration > %d));", 152748258c6bSjp151216 is_user_string, lower_name, req->id1domain, 152848258c6bSjp151216 curtime); 1529042addd6Sbaban if (lower_name != req->id1name) 1530042addd6Sbaban free(lower_name); 1531e8c27ec8Sbaban } else { 1532e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 1533e8c27ec8Sbaban goto out; 1534e8c27ec8Sbaban } 1535c5c4113dSnw141292 if (sql == NULL) { 1536c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1537c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1538c5c4113dSnw141292 goto out; 1539c5c4113dSnw141292 } 154048258c6bSjp151216 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 154148258c6bSjp151216 14, &values); 1542c5c4113dSnw141292 sqlite_freemem(sql); 1543c5c4113dSnw141292 1544c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 1545c5c4113dSnw141292 goto out; 1546c5c4113dSnw141292 } else if (retcode == IDMAP_SUCCESS) { 1547c5c4113dSnw141292 /* sanity checks */ 1548c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 1549c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1550c5c4113dSnw141292 goto out; 1551c5c4113dSnw141292 } 1552c5c4113dSnw141292 1553c5c4113dSnw141292 pid = strtoul(values[0], &end, 10); 1554c5c4113dSnw141292 is_user = strncmp(values[1], "0", 2) ? 1 : 0; 1555c5c4113dSnw141292 1556cd37da74Snw141292 if (is_user) { 1557cd37da74Snw141292 res->id.idtype = IDMAP_UID; 1558cd37da74Snw141292 res->id.idmap_id_u.uid = pid; 1559cd37da74Snw141292 } else { 1560cd37da74Snw141292 res->id.idtype = IDMAP_GID; 1561cd37da74Snw141292 res->id.idmap_id_u.gid = pid; 1562cd37da74Snw141292 } 1563cd37da74Snw141292 1564c5c4113dSnw141292 /* 1565c5c4113dSnw141292 * We may have an expired ephemeral mapping. Consider 1566c5c4113dSnw141292 * the expired entry as valid if we are not going to 1567c5c4113dSnw141292 * perform name-based mapping. But do not renew the 1568c5c4113dSnw141292 * expiration. 1569c5c4113dSnw141292 * If we will be doing name-based mapping then store the 1570c5c4113dSnw141292 * ephemeral pid in the result so that we can use it 1571c5c4113dSnw141292 * if we end up doing dynamic mapping again. 1572c5c4113dSnw141292 */ 1573c5c4113dSnw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) && 1574cd37da74Snw141292 !AVOID_NAMESERVICE(req) && 1575cd37da74Snw141292 IS_EPHEMERAL(pid) && values[2] != NULL) { 1576c5c4113dSnw141292 exp = strtoll(values[2], &end, 10); 1577c5c4113dSnw141292 if (exp && exp <= curtime) { 1578c5c4113dSnw141292 /* Store the ephemeral pid */ 1579651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 1580cd37da74Snw141292 req->direction |= is_user 1581cd37da74Snw141292 ? _IDMAP_F_EXP_EPH_UID 1582cd37da74Snw141292 : _IDMAP_F_EXP_EPH_GID; 1583c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 1584c5c4113dSnw141292 } 1585c5c4113dSnw141292 } 1586c5c4113dSnw141292 } 1587c5c4113dSnw141292 1588c5c4113dSnw141292 out: 1589c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 159062c60062Sbaban if (values[4] != NULL) 1591c5c4113dSnw141292 res->direction = 1592651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 1593651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 1594c5c4113dSnw141292 else 1595651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 1596c5c4113dSnw141292 159762c60062Sbaban if (values[3] != NULL) { 1598e8c27ec8Sbaban if (req->id2name != NULL) 1599e8c27ec8Sbaban free(req->id2name); 16008e228215Sdm199847 req->id2name = strdup(values[3]); 16018e228215Sdm199847 if (req->id2name == NULL) { 1602c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1603c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1604c5c4113dSnw141292 } 1605c5c4113dSnw141292 } 1606e8c27ec8Sbaban 1607e8c27ec8Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ? 1608e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 160948258c6bSjp151216 161048258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 161148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 161248258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 161348258c6bSjp151216 switch (res->info.how.map_type) { 161448258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 161548258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 161648258c6bSjp151216 strdup(values[7]); 161748258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 161848258c6bSjp151216 strdup(values[8]); 161948258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 162048258c6bSjp151216 strdup(values[9]); 162148258c6bSjp151216 break; 162248258c6bSjp151216 162348258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 162448258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 162548258c6bSjp151216 strdup(values[7]); 162648258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 162748258c6bSjp151216 strdup(values[8]); 162848258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 162948258c6bSjp151216 strdup(values[9]); 163048258c6bSjp151216 break; 163148258c6bSjp151216 163248258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 163348258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 163448258c6bSjp151216 strdup(values[10]); 163548258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 163648258c6bSjp151216 strdup(values[11]); 163748258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 163848258c6bSjp151216 strdup(values[12]); 163948258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 164048258c6bSjp151216 strtoul(values[13], &end, 1); 164148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 164248258c6bSjp151216 is_user; 164348258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 164448258c6bSjp151216 strtoul(values[5], &end, 1); 164548258c6bSjp151216 break; 164648258c6bSjp151216 164748258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 164848258c6bSjp151216 break; 164948258c6bSjp151216 165048258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 165148258c6bSjp151216 break; 165248258c6bSjp151216 165348258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 165448258c6bSjp151216 break; 165548258c6bSjp151216 165648258c6bSjp151216 default: 165748258c6bSjp151216 /* Unknow mapping type */ 165848258c6bSjp151216 assert(FALSE); 165948258c6bSjp151216 } 166048258c6bSjp151216 } 1661c5c4113dSnw141292 } 166262c60062Sbaban if (vm != NULL) 1663c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1664c5c4113dSnw141292 return (retcode); 1665c5c4113dSnw141292 } 1666c5c4113dSnw141292 1667cd37da74Snw141292 static 1668cd37da74Snw141292 idmap_retcode 166962c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid, 1670cd37da74Snw141292 char **name, char **domain, int *type) 1671cd37da74Snw141292 { 1672c5c4113dSnw141292 char *end; 1673c5c4113dSnw141292 char *sql = NULL; 1674c5c4113dSnw141292 const char **values; 1675c5c4113dSnw141292 sqlite_vm *vm = NULL; 1676c5c4113dSnw141292 int ncol; 1677c5c4113dSnw141292 time_t curtime; 1678c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 1679c5c4113dSnw141292 1680c5c4113dSnw141292 /* Get current time */ 1681c5c4113dSnw141292 errno = 0; 1682c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1683cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1684c5c4113dSnw141292 strerror(errno)); 1685c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1686c5c4113dSnw141292 goto out; 1687c5c4113dSnw141292 } 1688c5c4113dSnw141292 1689c5c4113dSnw141292 /* SQL to lookup the cache */ 1690cd37da74Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type " 1691cd37da74Snw141292 "FROM name_cache WHERE " 1692c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND " 1693c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1694c5c4113dSnw141292 "expiration > %d);", 1695c5c4113dSnw141292 sidprefix, rid, curtime); 1696c5c4113dSnw141292 if (sql == NULL) { 1697c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1698c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1699c5c4113dSnw141292 goto out; 1700c5c4113dSnw141292 } 1701c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values); 1702c5c4113dSnw141292 sqlite_freemem(sql); 1703c5c4113dSnw141292 1704c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 170562c60062Sbaban if (type != NULL) { 1706c5c4113dSnw141292 if (values[2] == NULL) { 1707c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1708c5c4113dSnw141292 goto out; 1709c5c4113dSnw141292 } 1710c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 1711c5c4113dSnw141292 } 1712c5c4113dSnw141292 171362c60062Sbaban if (name != NULL && values[0] != NULL) { 1714c5c4113dSnw141292 if ((*name = strdup(values[0])) == NULL) { 1715c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1716c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1717c5c4113dSnw141292 goto out; 1718c5c4113dSnw141292 } 1719c5c4113dSnw141292 } 1720c5c4113dSnw141292 172162c60062Sbaban if (domain != NULL && values[1] != NULL) { 1722c5c4113dSnw141292 if ((*domain = strdup(values[1])) == NULL) { 172362c60062Sbaban if (name != NULL && *name) { 1724c5c4113dSnw141292 free(*name); 1725c5c4113dSnw141292 *name = NULL; 1726c5c4113dSnw141292 } 1727c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1728c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1729c5c4113dSnw141292 goto out; 1730c5c4113dSnw141292 } 1731c5c4113dSnw141292 } 1732c5c4113dSnw141292 } 1733c5c4113dSnw141292 1734c5c4113dSnw141292 out: 173562c60062Sbaban if (vm != NULL) 1736c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1737c5c4113dSnw141292 return (retcode); 1738c5c4113dSnw141292 } 1739c5c4113dSnw141292 1740c5c4113dSnw141292 /* 1741e8c27ec8Sbaban * Given SID, find winname using name_cache OR 1742e8c27ec8Sbaban * Given winname, find SID using name_cache. 1743e8c27ec8Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and 1744e8c27ec8Sbaban * req->id2 is unix id 1745c5c4113dSnw141292 */ 1746cd37da74Snw141292 static 1747cd37da74Snw141292 idmap_retcode 1748e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1749cd37da74Snw141292 { 1750c5c4113dSnw141292 int type = -1; 1751c5c4113dSnw141292 idmap_retcode retcode; 1752e8c27ec8Sbaban char *sidprefix = NULL; 1753c5c4113dSnw141292 idmap_rid_t rid; 1754c5c4113dSnw141292 char *name = NULL, *domain = NULL; 1755c5c4113dSnw141292 1756e8c27ec8Sbaban /* Done if we've both sid and winname */ 1757e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) 1758e8c27ec8Sbaban return (IDMAP_SUCCESS); 1759c5c4113dSnw141292 1760e8c27ec8Sbaban /* Lookup sid to winname */ 1761e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1762e8c27ec8Sbaban retcode = lookup_cache_sid2name(cache, 1763e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix, 1764e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type); 176562c60062Sbaban goto out; 1766e8c27ec8Sbaban } 176762c60062Sbaban 1768e8c27ec8Sbaban /* Lookup winame to sid */ 1769e8c27ec8Sbaban retcode = lookup_cache_name2sid(cache, req->id1name, req->id1domain, 1770e8c27ec8Sbaban &name, &sidprefix, &rid, &type); 1771c5c4113dSnw141292 177262c60062Sbaban out: 1773e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1774c5c4113dSnw141292 free(name); 1775c5c4113dSnw141292 free(domain); 1776e8c27ec8Sbaban free(sidprefix); 1777e8c27ec8Sbaban return (retcode); 1778e8c27ec8Sbaban } 1779e8c27ec8Sbaban 1780e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) { 1781e8c27ec8Sbaban res->id.idtype = (type == _IDMAP_T_USER) ? 1782e8c27ec8Sbaban IDMAP_UID : IDMAP_GID; 1783e8c27ec8Sbaban } 1784e8c27ec8Sbaban req->id1.idtype = (type == _IDMAP_T_USER) ? 1785e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1786e8c27ec8Sbaban 1787e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1788e8c27ec8Sbaban if (name != NULL) { 1789e8c27ec8Sbaban free(req->id1name); /* Free existing winname */ 1790e8c27ec8Sbaban req->id1name = name; /* and use canonical name instead */ 1791e8c27ec8Sbaban } 1792e8c27ec8Sbaban if (req->id1domain == NULL) 1793e8c27ec8Sbaban req->id1domain = domain; 1794e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) { 1795e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix; 1796e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = rid; 1797c5c4113dSnw141292 } 1798c5c4113dSnw141292 return (retcode); 1799c5c4113dSnw141292 } 1800c5c4113dSnw141292 1801e8c27ec8Sbaban /* 1802e8c27ec8Sbaban * Batch AD lookups 1803e8c27ec8Sbaban */ 1804c5c4113dSnw141292 idmap_retcode 1805e8c27ec8Sbaban ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch, 1806cd37da74Snw141292 idmap_ids_res *result) 1807cd37da74Snw141292 { 1808c5c4113dSnw141292 idmap_retcode retcode; 1809e8c27ec8Sbaban int i, add, type, is_wuser, is_user; 1810e8c27ec8Sbaban int retries = 0, eunixtype; 1811e8c27ec8Sbaban char **unixname; 1812c5c4113dSnw141292 idmap_mapping *req; 1813c5c4113dSnw141292 idmap_id_res *res; 1814e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 181548258c6bSjp151216 idmap_how *how; 1816479ac375Sdm199847 char **dn, **attr, **value; 1817e8c27ec8Sbaban 1818e8c27ec8Sbaban /* 1819e8c27ec8Sbaban * Since req->id2.idtype is unused, we will use it here 1820e8c27ec8Sbaban * to retrieve the value of sid_type. But it needs to be 1821e8c27ec8Sbaban * reset to IDMAP_NONE before we return to prevent xdr 1822e8c27ec8Sbaban * from mis-interpreting req->id2 when it tries to free 1823e8c27ec8Sbaban * the input argument. Other option is to allocate an 1824e8c27ec8Sbaban * array of integers and use it instead for the batched 1825e8c27ec8Sbaban * call. But why un-necessarily allocate memory. That may 1826e8c27ec8Sbaban * be an option if req->id2.idtype cannot be re-used in 1827e8c27ec8Sbaban * future. 1828e8c27ec8Sbaban */ 1829c5c4113dSnw141292 1830c5c4113dSnw141292 if (state->ad_nqueries == 0) 1831c5c4113dSnw141292 return (IDMAP_SUCCESS); 1832c5c4113dSnw141292 183396c3a9a0Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 183496c3a9a0Sbaban req = &batch->idmap_mapping_batch_val[i]; 183596c3a9a0Sbaban res = &result->ids.ids_val[i]; 183696c3a9a0Sbaban 183796c3a9a0Sbaban /* Skip if not marked for AD lookup or already in error. */ 183896c3a9a0Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD) || 183996c3a9a0Sbaban res->retcode != IDMAP_SUCCESS) 184096c3a9a0Sbaban continue; 184196c3a9a0Sbaban 184296c3a9a0Sbaban /* Init status */ 184396c3a9a0Sbaban res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR; 184496c3a9a0Sbaban } 184596c3a9a0Sbaban 1846c5c4113dSnw141292 retry: 1847e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, state->ad_nqueries, 1848e8c27ec8Sbaban &qs); 1849e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 18500dcc7149Snw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 18510dcc7149Snw141292 goto retry; 1852349d5d8fSnw141292 degrade_svc(1, "failed to create batch for AD lookup"); 1853e8c27ec8Sbaban goto out; 1854c5c4113dSnw141292 } 1855c5c4113dSnw141292 1856c8e26105Sjp151216 restore_svc(); 1857c8e26105Sjp151216 1858e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 1859e8c27ec8Sbaban state->ad_unixgroup_attr); 1860e8c27ec8Sbaban 1861e8c27ec8Sbaban for (i = 0, add = 0; i < batch->idmap_mapping_batch_len; i++) { 1862c5c4113dSnw141292 req = &batch->idmap_mapping_batch_val[i]; 1863c5c4113dSnw141292 res = &result->ids.ids_val[i]; 186448258c6bSjp151216 how = &res->info.how; 186548258c6bSjp151216 1866e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 1867e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1868c5c4113dSnw141292 1869e8c27ec8Sbaban /* Skip if not marked for AD lookup */ 1870e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1871e8c27ec8Sbaban continue; 1872e8c27ec8Sbaban 187396c3a9a0Sbaban if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR) 1874c5c4113dSnw141292 continue; 1875e8c27ec8Sbaban 1876e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1877e8c27ec8Sbaban 1878479ac375Sdm199847 /* win2unix request: */ 1879e8c27ec8Sbaban 1880479ac375Sdm199847 unixname = dn = attr = value = NULL; 1881e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 1882e8c27ec8Sbaban if (req->id2name == NULL) { 1883e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID && 1884e8c27ec8Sbaban AD_OR_MIXED(state->nm_siduid)) { 1885e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 1886e8c27ec8Sbaban unixname = &req->id2name; 1887e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GID && 1888e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1889e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 1890e8c27ec8Sbaban unixname = &req->id2name; 1891e8c27ec8Sbaban } else if (AD_OR_MIXED(state->nm_siduid) || 1892e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1893e8c27ec8Sbaban unixname = &req->id2name; 1894e8c27ec8Sbaban } 1895e8c27ec8Sbaban } 1896e8c27ec8Sbaban add = 1; 1897479ac375Sdm199847 if (unixname != NULL) { 1898479ac375Sdm199847 /* 1899479ac375Sdm199847 * Get how info for DS-based name 1900479ac375Sdm199847 * mapping only if AD or MIXED 1901479ac375Sdm199847 * mode is enabled. 1902479ac375Sdm199847 */ 1903479ac375Sdm199847 idmap_info_free(&res->info); 190448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 190548258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1906479ac375Sdm199847 dn = &how->idmap_how_u.ad.dn; 1907479ac375Sdm199847 attr = &how->idmap_how_u.ad.attr; 1908479ac375Sdm199847 value = &how->idmap_how_u.ad.value; 1909479ac375Sdm199847 } 1910479ac375Sdm199847 if (req->id1.idmap_id_u.sid.prefix != NULL) { 1911479ac375Sdm199847 /* Lookup AD by SID */ 1912c5c4113dSnw141292 retcode = idmap_sid2name_batch_add1( 1913e8c27ec8Sbaban qs, req->id1.idmap_id_u.sid.prefix, 1914e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, eunixtype, 1915479ac375Sdm199847 dn, attr, value, 1916479ac375Sdm199847 (req->id1name == NULL) ? 1917479ac375Sdm199847 &req->id1name : NULL, 1918479ac375Sdm199847 (req->id1domain == NULL) ? 1919479ac375Sdm199847 &req->id1domain : NULL, 1920479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1921479ac375Sdm199847 &res->retcode); 1922479ac375Sdm199847 } else { 1923479ac375Sdm199847 /* Lookup AD by winname */ 1924479ac375Sdm199847 assert(req->id1name != NULL); 1925479ac375Sdm199847 retcode = idmap_name2sid_batch_add1( 1926479ac375Sdm199847 qs, req->id1name, req->id1domain, 1927479ac375Sdm199847 eunixtype, 1928479ac375Sdm199847 dn, attr, value, 1929479ac375Sdm199847 &req->id1name, 1930479ac375Sdm199847 &req->id1.idmap_id_u.sid.prefix, 1931479ac375Sdm199847 &req->id1.idmap_id_u.sid.rid, 1932479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1933479ac375Sdm199847 &res->retcode); 1934479ac375Sdm199847 } 1935c5c4113dSnw141292 1936e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 1937479ac375Sdm199847 1938479ac375Sdm199847 /* unix2win request: */ 1939e8c27ec8Sbaban 1940e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL && 1941e8c27ec8Sbaban req->id2name != NULL) { 1942e8c27ec8Sbaban /* Already have SID and winname -- done */ 1943e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 1944e8c27ec8Sbaban continue; 1945c5c4113dSnw141292 } 1946c5c4113dSnw141292 1947e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) { 1948cd37da74Snw141292 /* 1949e8c27ec8Sbaban * SID but no winname -- lookup AD by 1950e8c27ec8Sbaban * SID to get winname. 1951479ac375Sdm199847 * how info is not needed here because 1952479ac375Sdm199847 * we are not retrieving unixname from 1953479ac375Sdm199847 * AD. 1954cd37da74Snw141292 */ 1955e8c27ec8Sbaban add = 1; 1956e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1( 1957e8c27ec8Sbaban qs, res->id.idmap_id_u.sid.prefix, 1958e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 195948258c6bSjp151216 _IDMAP_T_UNDEF, 1960479ac375Sdm199847 NULL, NULL, NULL, 196148258c6bSjp151216 &req->id2name, 1962e8c27ec8Sbaban &req->id2domain, (int *)&req->id2.idtype, 1963e8c27ec8Sbaban NULL, &res->retcode); 1964e8c27ec8Sbaban } else if (req->id2name != NULL) { 1965e8c27ec8Sbaban /* 1966e8c27ec8Sbaban * winname but no SID -- lookup AD by 1967e8c27ec8Sbaban * winname to get SID. 1968479ac375Sdm199847 * how info is not needed here because 1969479ac375Sdm199847 * we are not retrieving unixname from 1970479ac375Sdm199847 * AD. 1971e8c27ec8Sbaban */ 1972e8c27ec8Sbaban add = 1; 1973e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1( 1974e8c27ec8Sbaban qs, req->id2name, req->id2domain, 197548258c6bSjp151216 _IDMAP_T_UNDEF, 1976479ac375Sdm199847 NULL, NULL, NULL, NULL, 1977e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1978e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1979e8c27ec8Sbaban (int *)&req->id2.idtype, NULL, 1980e8c27ec8Sbaban &res->retcode); 1981e8c27ec8Sbaban } else if (req->id1name != NULL) { 1982e8c27ec8Sbaban /* 1983e8c27ec8Sbaban * No SID and no winname but we've unixname -- 1984e8c27ec8Sbaban * lookup AD by unixname to get SID. 1985e8c27ec8Sbaban */ 1986e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 1987e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 1988e8c27ec8Sbaban is_wuser = 1; 1989e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 1990e8c27ec8Sbaban is_wuser = 0; 1991e8c27ec8Sbaban else 1992e8c27ec8Sbaban is_wuser = is_user; 1993e8c27ec8Sbaban add = 1; 1994479ac375Sdm199847 idmap_info_free(&res->info); 199548258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 199648258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1997e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1( 1998e8c27ec8Sbaban qs, req->id1name, is_user, is_wuser, 199948258c6bSjp151216 &how->idmap_how_u.ad.dn, 200048258c6bSjp151216 &how->idmap_how_u.ad.attr, 200148258c6bSjp151216 &how->idmap_how_u.ad.value, 2002e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 2003e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 2004e8c27ec8Sbaban &req->id2name, &req->id2domain, 2005e8c27ec8Sbaban (int *)&req->id2.idtype, &res->retcode); 2006e8c27ec8Sbaban } 2007e8c27ec8Sbaban } 2008e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2009e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 2010e8c27ec8Sbaban break; 2011e8c27ec8Sbaban } 2012cd37da74Snw141292 } 2013cd37da74Snw141292 201496c3a9a0Sbaban if (retcode == IDMAP_SUCCESS) { 201596c3a9a0Sbaban /* add keeps track if we added an entry to the batch */ 201696c3a9a0Sbaban if (add) 20170dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 201896c3a9a0Sbaban else 201996c3a9a0Sbaban idmap_lookup_release_batch(&qs); 202096c3a9a0Sbaban } 2021cd37da74Snw141292 2022c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 2023c5c4113dSnw141292 goto retry; 2024c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 2025349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 2026c5c4113dSnw141292 2027e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2028e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests"); 2029c5c4113dSnw141292 2030c5c4113dSnw141292 out: 2031e8c27ec8Sbaban /* 2032e8c27ec8Sbaban * This loop does the following: 2033479ac375Sdm199847 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request. 2034479ac375Sdm199847 * 2. Reset req->id2.idtype to IDMAP_NONE 2035479ac375Sdm199847 * 3. If batch_start or batch_add failed then set the status 2036479ac375Sdm199847 * of each request marked for AD lookup to that error. 2037479ac375Sdm199847 * 4. Evaluate the type of the AD object (i.e. user or group) and 2038479ac375Sdm199847 * update the idtype in request. 2039e8c27ec8Sbaban */ 2040e8c27ec8Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 2041e8c27ec8Sbaban req = &batch->idmap_mapping_batch_val[i]; 2042e8c27ec8Sbaban type = req->id2.idtype; 2043e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 20445e0794bcSbaban res = &result->ids.ids_val[i]; 204548258c6bSjp151216 how = &res->info.how; 2046e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 2047e8c27ec8Sbaban continue; 2048e8c27ec8Sbaban 2049479ac375Sdm199847 /* Reset AD lookup flag */ 2050479ac375Sdm199847 req->direction &= ~(_IDMAP_F_LOOKUP_AD); 2051479ac375Sdm199847 2052479ac375Sdm199847 /* 2053479ac375Sdm199847 * If batch_start or batch_add failed then set the status 2054479ac375Sdm199847 * of each request marked for AD lookup to that error. 2055479ac375Sdm199847 */ 2056e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2057e8c27ec8Sbaban res->retcode = retcode; 2058e8c27ec8Sbaban continue; 2059e8c27ec8Sbaban } 2060e8c27ec8Sbaban 2061e8c27ec8Sbaban if (!add) 2062e8c27ec8Sbaban continue; 2063e8c27ec8Sbaban 206448258c6bSjp151216 if (res->retcode == IDMAP_ERR_NOTFOUND) { 206548258c6bSjp151216 /* Nothing found - remove the preset info */ 2066479ac375Sdm199847 idmap_info_free(&res->info); 206748258c6bSjp151216 } 206848258c6bSjp151216 2069e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 2070e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) 2071e8c27ec8Sbaban continue; 2072479ac375Sdm199847 /* Evaluate result type */ 2073e8c27ec8Sbaban switch (type) { 2074e8c27ec8Sbaban case _IDMAP_T_USER: 2075e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2076e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 2077e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 2078e8c27ec8Sbaban break; 2079e8c27ec8Sbaban case _IDMAP_T_GROUP: 2080e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2081e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 2082e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 2083e8c27ec8Sbaban break; 2084e8c27ec8Sbaban default: 2085e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2086e8c27ec8Sbaban break; 2087e8c27ec8Sbaban } 2088479ac375Sdm199847 if (res->retcode == IDMAP_SUCCESS && 2089479ac375Sdm199847 req->id1name != NULL && 2090479ac375Sdm199847 (req->id2name == NULL || 2091479ac375Sdm199847 res->id.idmap_id_u.uid == SENTINEL_PID) && 2092479ac375Sdm199847 NLDAP_MODE(res->id.idtype, state)) { 2093479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2094479ac375Sdm199847 state->nldap_nqueries++; 2095479ac375Sdm199847 } 2096e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 2097e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2098e8c27ec8Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) && 2099e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix == NULL && 2100e8c27ec8Sbaban req->id2name == NULL && /* no winname */ 2101e8c27ec8Sbaban req->id1name != NULL) /* unixname */ 2102e8c27ec8Sbaban /* 2103479ac375Sdm199847 * If AD lookup by unixname failed 2104479ac375Sdm199847 * with non fatal error then clear 2105479ac375Sdm199847 * the error (i.e set res->retcode 2106479ac375Sdm199847 * to success). This allows the next 2107479ac375Sdm199847 * pass to process other mapping 2108479ac375Sdm199847 * mechanisms for this request. 2109e8c27ec8Sbaban */ 2110e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 2111e8c27ec8Sbaban continue; 2112e8c27ec8Sbaban } 2113479ac375Sdm199847 /* Evaluate result type */ 2114e8c27ec8Sbaban switch (type) { 2115e8c27ec8Sbaban case _IDMAP_T_USER: 2116e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2117e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 2118e8c27ec8Sbaban break; 2119e8c27ec8Sbaban case _IDMAP_T_GROUP: 2120e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2121e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 2122e8c27ec8Sbaban break; 2123e8c27ec8Sbaban default: 2124e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2125e8c27ec8Sbaban break; 2126e8c27ec8Sbaban } 2127e8c27ec8Sbaban } 2128e8c27ec8Sbaban } 2129e8c27ec8Sbaban 2130e8c27ec8Sbaban /* AD lookups done. Reset state->ad_nqueries and return */ 2131e8c27ec8Sbaban state->ad_nqueries = 0; 2132c5c4113dSnw141292 return (retcode); 2133c5c4113dSnw141292 } 2134c5c4113dSnw141292 2135cd37da74Snw141292 /* 2136cd37da74Snw141292 * Convention when processing win2unix requests: 2137cd37da74Snw141292 * 2138cd37da74Snw141292 * Windows identity: 2139cd37da74Snw141292 * req->id1name = 2140cd37da74Snw141292 * winname if given otherwise winname found will be placed 2141cd37da74Snw141292 * here. 2142cd37da74Snw141292 * req->id1domain = 2143cd37da74Snw141292 * windomain if given otherwise windomain found will be 2144cd37da74Snw141292 * placed here. 2145cd37da74Snw141292 * req->id1.idtype = 2146cd37da74Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll 2147cd37da74Snw141292 * be set to IDMAP_USID/GSID depending upon whether the 2148cd37da74Snw141292 * given SID is user or group respectively. The user/group-ness 2149cd37da74Snw141292 * is determined either when looking up well-known SIDs table OR 2150479ac375Sdm199847 * if the SID is found in namecache OR by ad_lookup_one() OR by 2151cd37da74Snw141292 * ad_lookup_batch(). 2152cd37da74Snw141292 * req->id1..sid.[prefix, rid] = 2153cd37da74Snw141292 * SID if given otherwise SID found will be placed here. 2154cd37da74Snw141292 * 2155cd37da74Snw141292 * Unix identity: 2156cd37da74Snw141292 * req->id2name = 2157cd37da74Snw141292 * unixname found will be placed here. 2158cd37da74Snw141292 * req->id2domain = 2159cd37da74Snw141292 * NOT USED 2160cd37da74Snw141292 * res->id.idtype = 2161cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 2162cd37da74Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found 2163cd37da74Snw141292 * will be placed here. 2164cd37da74Snw141292 * res->id..[uid or gid] = 2165cd37da74Snw141292 * UID/GID found will be placed here. 2166cd37da74Snw141292 * 2167cd37da74Snw141292 * Others: 2168cd37da74Snw141292 * res->retcode = 2169cd37da74Snw141292 * Return status for this request will be placed here. 2170cd37da74Snw141292 * res->direction = 2171cd37da74Snw141292 * Direction found will be placed here. Direction 2172cd37da74Snw141292 * meaning whether the resultant mapping is valid 2173cd37da74Snw141292 * only from win2unix or bi-directional. 2174cd37da74Snw141292 * req->direction = 2175cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 2176cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 2177cd37da74Snw141292 * of the request. 2178cd37da74Snw141292 * req->id2.idtype = 2179cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 2180cd37da74Snw141292 * type and is used to initialize res->id.idtype. 2181cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 2182cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 2183cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 2184cd37da74Snw141292 * mis-interpreting the contents of req->id2. 2185cd37da74Snw141292 * req->id2..[uid or gid or sid] = 2186cd37da74Snw141292 * NOT USED 2187cd37da74Snw141292 */ 2188cd37da74Snw141292 2189cd37da74Snw141292 /* 2190cd37da74Snw141292 * This function does the following: 2191cd37da74Snw141292 * 1. Lookup well-known SIDs table. 2192cd37da74Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it. 2193cd37da74Snw141292 * 3. Lookup cache. 2194cd37da74Snw141292 * 4. Check if the client does not want new mapping to be allocated 2195cd37da74Snw141292 * in which case this pass is the final pass. 2196cd37da74Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs 2197cd37da74Snw141292 * to do AD lookup. 2198cd37da74Snw141292 */ 2199c5c4113dSnw141292 idmap_retcode 2200479ac375Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req, 2201cd37da74Snw141292 idmap_id_res *res) 2202cd37da74Snw141292 { 2203c5c4113dSnw141292 idmap_retcode retcode; 2204e8c27ec8Sbaban int wksid; 2205c5c4113dSnw141292 2206e8c27ec8Sbaban /* Initialize result */ 2207e8c27ec8Sbaban res->id.idtype = req->id2.idtype; 2208e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2209e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 2210e8c27ec8Sbaban wksid = 0; 2211c5c4113dSnw141292 2212cf5b5989Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) { 2213e8c27ec8Sbaban if (req->id1name == NULL) { 2214e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 2215c5c4113dSnw141292 goto out; 2216c5c4113dSnw141292 } 2217e8c27ec8Sbaban /* sanitize sidprefix */ 2218e8c27ec8Sbaban free(req->id1.idmap_id_u.sid.prefix); 2219e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = NULL; 2220e8c27ec8Sbaban } 2221c5c4113dSnw141292 2222e8c27ec8Sbaban /* Lookup well-known SIDs table */ 2223e8c27ec8Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid); 2224c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2225c5c4113dSnw141292 goto out; 2226c5c4113dSnw141292 2227e8c27ec8Sbaban /* Check if this is a localsid */ 2228e8c27ec8Sbaban if (!wksid) { 2229e8c27ec8Sbaban retcode = lookup_localsid2pid(req, res); 2230e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2231e8c27ec8Sbaban goto out; 2232e8c27ec8Sbaban } 2233e8c27ec8Sbaban 2234e8c27ec8Sbaban /* Lookup cache */ 2235479ac375Sdm199847 retcode = lookup_cache_sid2pid(state->cache, req, res); 2236c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2237c5c4113dSnw141292 goto out; 2238c5c4113dSnw141292 2239c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 224048258c6bSjp151216 retcode = IDMAP_ERR_NONEGENERATED; 2241c5c4113dSnw141292 goto out; 2242c5c4113dSnw141292 } 2243c5c4113dSnw141292 2244c5c4113dSnw141292 /* 2245e8c27ec8Sbaban * Failed to find non-expired entry in cache. Next step is 2246e8c27ec8Sbaban * to determine if this request needs to be batched for AD lookup. 2247e8c27ec8Sbaban * 2248e8c27ec8Sbaban * At this point we have either sid or winname or both. If we don't 2249e8c27ec8Sbaban * have both then lookup name_cache for the sid or winname 2250e8c27ec8Sbaban * whichever is missing. If not found then this request will be 2251e8c27ec8Sbaban * batched for AD lookup. 2252e8c27ec8Sbaban */ 2253479ac375Sdm199847 retcode = lookup_name_cache(state->cache, req, res); 2254e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS && retcode != IDMAP_ERR_NOTFOUND) 2255e8c27ec8Sbaban goto out; 2256e8c27ec8Sbaban 2257e8c27ec8Sbaban /* 2258e8c27ec8Sbaban * Set the flag to indicate that we are not done yet so that 2259e8c27ec8Sbaban * subsequent passes considers this request for name-based 2260e8c27ec8Sbaban * mapping and ephemeral mapping. 2261c5c4113dSnw141292 */ 2262c5c4113dSnw141292 state->sid2pid_done = FALSE; 2263e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 2264c5c4113dSnw141292 2265c5c4113dSnw141292 /* 2266e8c27ec8Sbaban * Even if we have both sid and winname, we still may need to batch 2267e8c27ec8Sbaban * this request for AD lookup if we don't have unixname and 2268e8c27ec8Sbaban * directory-based name mapping (AD or mixed) is enabled. 2269e8c27ec8Sbaban * We avoid AD lookup for well-known SIDs because they don't have 2270e8c27ec8Sbaban * regular AD objects. 2271c5c4113dSnw141292 */ 2272e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS || 2273e8c27ec8Sbaban (!wksid && req->id2name == NULL && 2274e8c27ec8Sbaban AD_OR_MIXED_MODE(res->id.idtype, state))) { 2275c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 2276e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 2277c5c4113dSnw141292 state->ad_nqueries++; 2278479ac375Sdm199847 } else if (NLDAP_MODE(res->id.idtype, state)) { 2279479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2280479ac375Sdm199847 state->nldap_nqueries++; 2281c5c4113dSnw141292 } 2282c5c4113dSnw141292 2283c5c4113dSnw141292 2284c5c4113dSnw141292 out: 2285c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2286e8c27ec8Sbaban /* 2287e8c27ec8Sbaban * If we are done and there was an error then set fallback pid 2288e8c27ec8Sbaban * in the result. 2289e8c27ec8Sbaban */ 2290e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 2291e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2292c5c4113dSnw141292 return (retcode); 2293c5c4113dSnw141292 } 2294c5c4113dSnw141292 2295c5c4113dSnw141292 /* 2296c5c4113dSnw141292 * Generate SID using the following convention 2297c5c4113dSnw141292 * <machine-sid-prefix>-<1000 + uid> 2298c5c4113dSnw141292 * <machine-sid-prefix>-<2^31 + gid> 2299c5c4113dSnw141292 */ 2300cd37da74Snw141292 static 2301cd37da74Snw141292 idmap_retcode 230248258c6bSjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user, 230348258c6bSjp151216 int fallback) 2304cd37da74Snw141292 { 2305e8c27ec8Sbaban free(res->id.idmap_id_u.sid.prefix); 2306e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix = NULL; 2307e8c27ec8Sbaban 2308e8c27ec8Sbaban /* 2309e8c27ec8Sbaban * Diagonal mapping for localSIDs not supported because of the 2310e8c27ec8Sbaban * way we generate localSIDs. 2311e8c27ec8Sbaban */ 2312e8c27ec8Sbaban if (is_user && res->id.idtype == IDMAP_GSID) 2313e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2314e8c27ec8Sbaban if (!is_user && res->id.idtype == IDMAP_USID) 2315e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2316e8c27ec8Sbaban 2317c5c4113dSnw141292 /* Skip 1000 UIDs */ 2318c5c4113dSnw141292 if (is_user && req->id1.idmap_id_u.uid > 2319c5c4113dSnw141292 (INT32_MAX - LOCALRID_MIN)) 232062c60062Sbaban return (IDMAP_ERR_NOMAPPING); 2321c5c4113dSnw141292 2322c5c4113dSnw141292 RDLOCK_CONFIG(); 2323e8c27ec8Sbaban /* 2324e8c27ec8Sbaban * machine_sid is never NULL because if it is we won't be here. 2325e8c27ec8Sbaban * No need to assert because stdrup(NULL) will core anyways. 2326e8c27ec8Sbaban */ 2327c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 2328c5c4113dSnw141292 strdup(_idmapdstate.cfg->pgcfg.machine_sid); 2329c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 2330c5c4113dSnw141292 UNLOCK_CONFIG(); 2331c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2332c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 2333c5c4113dSnw141292 } 2334c5c4113dSnw141292 UNLOCK_CONFIG(); 2335c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 2336c5c4113dSnw141292 (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_MIN : 2337c5c4113dSnw141292 req->id1.idmap_id_u.gid + INT32_MAX + 1; 2338651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 2339e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2340e8c27ec8Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 2341c5c4113dSnw141292 234248258c6bSjp151216 if (!fallback && req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 234348258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 234448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 234548258c6bSjp151216 } 234648258c6bSjp151216 2347c5c4113dSnw141292 /* 2348c5c4113dSnw141292 * Don't update name_cache because local sids don't have 2349c5c4113dSnw141292 * valid windows names. 2350c5c4113dSnw141292 */ 2351e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 2352947c7bc0Sbaban return (IDMAP_SUCCESS); 2353c5c4113dSnw141292 } 2354c5c4113dSnw141292 2355cd37da74Snw141292 static 2356cd37da74Snw141292 idmap_retcode 2357cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res) 2358cd37da74Snw141292 { 2359c5c4113dSnw141292 char *sidprefix; 2360c5c4113dSnw141292 uint32_t rid; 2361c5c4113dSnw141292 int s; 2362c5c4113dSnw141292 2363c5c4113dSnw141292 /* 2364c5c4113dSnw141292 * If the sidprefix == localsid then UID = last RID - 1000 or 2365c5c4113dSnw141292 * GID = last RID - 2^31. 2366c5c4113dSnw141292 */ 2367e8c27ec8Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL) 2368e8c27ec8Sbaban /* This means we are looking up by winname */ 2369e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2370c5c4113dSnw141292 rid = req->id1.idmap_id_u.sid.rid; 2371c5c4113dSnw141292 2372c5c4113dSnw141292 RDLOCK_CONFIG(); 2373c5c4113dSnw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ? 2374cd37da74Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1; 2375c5c4113dSnw141292 UNLOCK_CONFIG(); 2376c5c4113dSnw141292 2377e8c27ec8Sbaban /* 2378e8c27ec8Sbaban * If the given sidprefix does not match machine_sid then this is 2379e8c27ec8Sbaban * not a local SID. 2380e8c27ec8Sbaban */ 2381e8c27ec8Sbaban if (s != 0) 2382e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2383e8c27ec8Sbaban 2384e8c27ec8Sbaban switch (res->id.idtype) { 2385c5c4113dSnw141292 case IDMAP_UID: 2386e8c27ec8Sbaban if (rid > INT32_MAX || rid < LOCALRID_MIN) 2387e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2388c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2389c5c4113dSnw141292 break; 2390c5c4113dSnw141292 case IDMAP_GID: 2391e8c27ec8Sbaban if (rid <= INT32_MAX) 2392e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2393c5c4113dSnw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2394c5c4113dSnw141292 break; 2395c5c4113dSnw141292 case IDMAP_POSIXID: 2396c5c4113dSnw141292 if (rid > INT32_MAX) { 2397cd37da74Snw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2398c5c4113dSnw141292 res->id.idtype = IDMAP_GID; 2399c5c4113dSnw141292 } else if (rid < LOCALRID_MIN) { 2400e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2401c5c4113dSnw141292 } else { 2402c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2403c5c4113dSnw141292 res->id.idtype = IDMAP_UID; 2404c5c4113dSnw141292 } 2405c5c4113dSnw141292 break; 2406c5c4113dSnw141292 default: 2407c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 2408c5c4113dSnw141292 } 240948258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 241048258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 241148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 241248258c6bSjp151216 } 2413c5c4113dSnw141292 return (IDMAP_SUCCESS); 2414c5c4113dSnw141292 } 2415c5c4113dSnw141292 2416e8c27ec8Sbaban /* 2417e8c27ec8Sbaban * Name service lookup by unixname to get pid 2418e8c27ec8Sbaban */ 2419cd37da74Snw141292 static 2420cd37da74Snw141292 idmap_retcode 2421e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id) 2422cd37da74Snw141292 { 2423cd37da74Snw141292 struct passwd pwd, *pwdp; 2424cd37da74Snw141292 struct group grp, *grpp; 2425c5c4113dSnw141292 char buf[1024]; 2426c5c4113dSnw141292 int errnum; 2427c5c4113dSnw141292 const char *me = "ns_lookup_byname"; 2428c5c4113dSnw141292 2429e8c27ec8Sbaban switch (id->idtype) { 2430e8c27ec8Sbaban case IDMAP_UID: 2431cd37da74Snw141292 pwdp = getpwnam_r(name, &pwd, buf, sizeof (buf)); 2432e8c27ec8Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL && 2433cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2434cd37da74Snw141292 pwdp = getpwnam_r(lower_name, &pwd, buf, sizeof (buf)); 2435cd37da74Snw141292 if (pwdp == NULL) { 2436c5c4113dSnw141292 errnum = errno; 2437c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2438c5c4113dSnw141292 "%s: getpwnam_r(%s) failed (%s).", 2439cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2440c5c4113dSnw141292 if (errnum == 0) 2441c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2442c5c4113dSnw141292 else 2443c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2444c5c4113dSnw141292 } 2445e8c27ec8Sbaban id->idmap_id_u.uid = pwd.pw_uid; 2446e8c27ec8Sbaban break; 2447e8c27ec8Sbaban case IDMAP_GID: 2448cd37da74Snw141292 grpp = getgrnam_r(name, &grp, buf, sizeof (buf)); 2449e8c27ec8Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL && 2450cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2451cd37da74Snw141292 grpp = getgrnam_r(lower_name, &grp, buf, sizeof (buf)); 2452cd37da74Snw141292 if (grpp == NULL) { 2453c5c4113dSnw141292 errnum = errno; 2454c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2455c5c4113dSnw141292 "%s: getgrnam_r(%s) failed (%s).", 2456cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2457c5c4113dSnw141292 if (errnum == 0) 2458c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2459c5c4113dSnw141292 else 2460c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2461c5c4113dSnw141292 } 2462e8c27ec8Sbaban id->idmap_id_u.gid = grp.gr_gid; 2463e8c27ec8Sbaban break; 2464e8c27ec8Sbaban default: 2465e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2466c5c4113dSnw141292 } 2467c5c4113dSnw141292 return (IDMAP_SUCCESS); 2468c5c4113dSnw141292 } 2469c5c4113dSnw141292 2470e8c27ec8Sbaban 2471e8c27ec8Sbaban /* 2472e8c27ec8Sbaban * Name service lookup by pid to get unixname 2473e8c27ec8Sbaban */ 2474e8c27ec8Sbaban static 2475e8c27ec8Sbaban idmap_retcode 2476e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname) 2477e8c27ec8Sbaban { 2478e8c27ec8Sbaban struct passwd pwd; 2479e8c27ec8Sbaban struct group grp; 2480e8c27ec8Sbaban char buf[1024]; 2481e8c27ec8Sbaban int errnum; 2482e8c27ec8Sbaban const char *me = "ns_lookup_bypid"; 2483e8c27ec8Sbaban 2484e8c27ec8Sbaban if (is_user) { 2485e8c27ec8Sbaban errno = 0; 2486e8c27ec8Sbaban if (getpwuid_r(pid, &pwd, buf, sizeof (buf)) == NULL) { 2487e8c27ec8Sbaban errnum = errno; 2488e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2489e8c27ec8Sbaban "%s: getpwuid_r(%u) failed (%s).", 2490e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2491e8c27ec8Sbaban if (errnum == 0) 2492e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2493e8c27ec8Sbaban else 2494e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2495e8c27ec8Sbaban } 2496e8c27ec8Sbaban *unixname = strdup(pwd.pw_name); 2497e8c27ec8Sbaban } else { 2498e8c27ec8Sbaban errno = 0; 2499e8c27ec8Sbaban if (getgrgid_r(pid, &grp, buf, sizeof (buf)) == NULL) { 2500e8c27ec8Sbaban errnum = errno; 2501e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2502e8c27ec8Sbaban "%s: getgrgid_r(%u) failed (%s).", 2503e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2504e8c27ec8Sbaban if (errnum == 0) 2505e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2506e8c27ec8Sbaban else 2507e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2508e8c27ec8Sbaban } 2509e8c27ec8Sbaban *unixname = strdup(grp.gr_name); 2510e8c27ec8Sbaban } 2511e8c27ec8Sbaban if (*unixname == NULL) 2512e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 2513e8c27ec8Sbaban return (IDMAP_SUCCESS); 2514e8c27ec8Sbaban } 2515e8c27ec8Sbaban 2516c5c4113dSnw141292 /* 2517c5c4113dSnw141292 * Name-based mapping 2518c5c4113dSnw141292 * 2519c5c4113dSnw141292 * Case 1: If no rule matches do ephemeral 2520c5c4113dSnw141292 * 2521c5c4113dSnw141292 * Case 2: If rule matches and unixname is "" then return no mapping. 2522c5c4113dSnw141292 * 2523c5c4113dSnw141292 * Case 3: If rule matches and unixname is specified then lookup name 2524c5c4113dSnw141292 * service using the unixname. If unixname not found then return no mapping. 2525c5c4113dSnw141292 * 2526c5c4113dSnw141292 * Case 4: If rule matches and unixname is * then lookup name service 2527c5c4113dSnw141292 * using winname as the unixname. If unixname not found then process 2528c5c4113dSnw141292 * other rules using the lookup order. If no other rule matches then do 2529c5c4113dSnw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4. 2530c5c4113dSnw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping 2531c5c4113dSnw141292 * instead of the default behaviour of doing ephemeral mapping. 2532c5c4113dSnw141292 * 2533c5c4113dSnw141292 * Example 1: 2534c5c4113dSnw141292 * *@sfbay == * 2535c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2536c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to an ephemeral id. 2537c5c4113dSnw141292 * 2538c5c4113dSnw141292 * Example 2: 2539c5c4113dSnw141292 * *@sfbay == * 2540c5c4113dSnw141292 * *@sfbay => guest 2541c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2542c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to guest. 2543c5c4113dSnw141292 * 2544c5c4113dSnw141292 * Example 3: 2545c5c4113dSnw141292 * *@sfbay == * 2546c5c4113dSnw141292 * *@sfbay => "" 2547c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2548c5c4113dSnw141292 * the name service then we will return no mapping for foo@sfbay. 2549c5c4113dSnw141292 * 2550c5c4113dSnw141292 */ 2551cd37da74Snw141292 static 2552cd37da74Snw141292 idmap_retcode 2553479ac375Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state, 2554479ac375Sdm199847 idmap_mapping *req, idmap_id_res *res) 2555cd37da74Snw141292 { 2556cd37da74Snw141292 const char *unixname, *windomain; 2557cd37da74Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL; 2558c5c4113dSnw141292 idmap_retcode retcode; 2559cd37da74Snw141292 char *end, *lower_unixname, *winname; 2560c5c4113dSnw141292 const char **values; 2561c5c4113dSnw141292 sqlite_vm *vm = NULL; 2562cd37da74Snw141292 int ncol, r, i, is_user, is_wuser; 256348258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 256448258c6bSjp151216 int direction; 2565c5c4113dSnw141292 const char *me = "name_based_mapping_sid2pid"; 2566c5c4113dSnw141292 2567e8c27ec8Sbaban assert(req->id1name != NULL); /* We have winname */ 2568e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have unixname */ 2569e8c27ec8Sbaban 25708e228215Sdm199847 winname = req->id1name; 25718e228215Sdm199847 windomain = req->id1domain; 2572cd37da74Snw141292 2573cd37da74Snw141292 switch (req->id1.idtype) { 2574cd37da74Snw141292 case IDMAP_USID: 2575cd37da74Snw141292 is_wuser = 1; 2576cd37da74Snw141292 break; 2577cd37da74Snw141292 case IDMAP_GSID: 2578cd37da74Snw141292 is_wuser = 0; 2579cd37da74Snw141292 break; 2580cd37da74Snw141292 default: 2581e8c27ec8Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the " 2582e8c27ec8Sbaban "given Windows id is user or group.", me); 2583cd37da74Snw141292 return (IDMAP_ERR_INTERNAL); 2584cd37da74Snw141292 } 2585cd37da74Snw141292 2586e8c27ec8Sbaban switch (res->id.idtype) { 2587cd37da74Snw141292 case IDMAP_UID: 2588cd37da74Snw141292 is_user = 1; 2589cd37da74Snw141292 break; 2590cd37da74Snw141292 case IDMAP_GID: 2591cd37da74Snw141292 is_user = 0; 2592cd37da74Snw141292 break; 2593cd37da74Snw141292 case IDMAP_POSIXID: 2594cd37da74Snw141292 is_user = is_wuser; 2595cd37da74Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID; 2596cd37da74Snw141292 break; 2597cd37da74Snw141292 } 2598c5c4113dSnw141292 2599c5c4113dSnw141292 i = 0; 2600479ac375Sdm199847 if (windomain == NULL) 260162c60062Sbaban windomain = ""; 260282da9f60Sbaban else if (state->defdom != NULL && 260382da9f60Sbaban strcasecmp(state->defdom, windomain) == 0) 2604c5c4113dSnw141292 i = 1; 2605c5c4113dSnw141292 2606cd37da74Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL) 2607cd37da74Snw141292 lower_winname = winname; /* hope for the best */ 2608c5c4113dSnw141292 sql = sqlite_mprintf( 260948258c6bSjp151216 "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 " 261048258c6bSjp151216 "FROM namerules WHERE " 2611cd37da74Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND " 2612c5c4113dSnw141292 "(winname = %Q OR winname = '*') AND " 2613c5c4113dSnw141292 "(windomain = %Q OR windomain = '*' %s) " 2614c5c4113dSnw141292 "ORDER BY w2u_order ASC;", 2615cd37da74Snw141292 is_user, is_wuser, lower_winname, windomain, 261662c60062Sbaban i ? "OR windomain ISNULL OR windomain = ''" : ""); 2617c5c4113dSnw141292 if (sql == NULL) { 2618c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2619c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 2620c5c4113dSnw141292 goto out; 2621c5c4113dSnw141292 } 2622c5c4113dSnw141292 2623479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 2624c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2625cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2626cd37da74Snw141292 CHECK_NULL(errmsg)); 2627c5c4113dSnw141292 sqlite_freemem(errmsg); 2628c5c4113dSnw141292 goto out; 2629c5c4113dSnw141292 } 2630c5c4113dSnw141292 263184decf41Sjp151216 for (;;) { 2632c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 263384decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 2634c5c4113dSnw141292 263584decf41Sjp151216 if (r == SQLITE_ROW) { 263648258c6bSjp151216 if (ncol < 5) { 2637c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2638c5c4113dSnw141292 goto out; 2639c5c4113dSnw141292 } 2640c5c4113dSnw141292 if (values[0] == NULL) { 2641c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2642c5c4113dSnw141292 goto out; 2643c5c4113dSnw141292 } 2644c5c4113dSnw141292 264548258c6bSjp151216 if (values[1] != NULL) 264648258c6bSjp151216 direction = 264748258c6bSjp151216 (strtol(values[1], &end, 10) == 0)? 264848258c6bSjp151216 IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 264948258c6bSjp151216 else 265048258c6bSjp151216 direction = IDMAP_DIRECTION_W2U; 265148258c6bSjp151216 2652c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 265348258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 265448258c6bSjp151216 values[0], is_wuser, is_user, 265548258c6bSjp151216 strtol(values[4], &end, 10), 265648258c6bSjp151216 direction); 2657c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2658c5c4113dSnw141292 goto out; 2659c5c4113dSnw141292 } 266048258c6bSjp151216 266148258c6bSjp151216 if (values[0][0] == '*') { 266248258c6bSjp151216 unixname = winname; 266348258c6bSjp151216 lower_unixname = lower_winname; 266448258c6bSjp151216 } else { 266548258c6bSjp151216 unixname = values[0]; 266648258c6bSjp151216 lower_unixname = NULL; 266748258c6bSjp151216 } 266848258c6bSjp151216 2669e8c27ec8Sbaban retcode = ns_lookup_byname(unixname, lower_unixname, 2670e8c27ec8Sbaban &res->id); 2671c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 267248258c6bSjp151216 if (values[0][0] == '*') 2673c5c4113dSnw141292 /* Case 4 */ 2674c5c4113dSnw141292 continue; 267548258c6bSjp151216 else { 2676c5c4113dSnw141292 /* Case 3 */ 267748258c6bSjp151216 idmap_namerule_set(rule, values[3], 267848258c6bSjp151216 values[2], values[0], is_wuser, 267948258c6bSjp151216 is_user, 268048258c6bSjp151216 strtol(values[4], &end, 10), 268148258c6bSjp151216 direction); 2682c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2683c5c4113dSnw141292 } 268448258c6bSjp151216 } 2685c5c4113dSnw141292 goto out; 2686c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 2687c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 2688c5c4113dSnw141292 goto out; 2689c5c4113dSnw141292 } else { 2690c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 2691c5c4113dSnw141292 vm = NULL; 2692cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2693cd37da74Snw141292 CHECK_NULL(errmsg)); 2694c5c4113dSnw141292 sqlite_freemem(errmsg); 2695c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2696c5c4113dSnw141292 goto out; 2697c5c4113dSnw141292 } 2698c5c4113dSnw141292 } 2699c5c4113dSnw141292 2700c5c4113dSnw141292 out: 270148258c6bSjp151216 if (sql != NULL) 2702c5c4113dSnw141292 sqlite_freemem(sql); 270348258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 2704c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 270562c60062Sbaban if (values[1] != NULL) 2706c5c4113dSnw141292 res->direction = 2707651c0131Sbaban (strtol(values[1], &end, 10) == 0)? 2708651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 2709c5c4113dSnw141292 else 2710651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 271148258c6bSjp151216 27128e228215Sdm199847 req->id2name = strdup(unixname); 2713479ac375Sdm199847 if (req->id2name == NULL) { 2714479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 2715479ac375Sdm199847 } 2716479ac375Sdm199847 } 2717479ac375Sdm199847 2718479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 271948258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 272048258c6bSjp151216 values[0], is_wuser, is_user, strtol(values[4], &end, 10), 272148258c6bSjp151216 res->direction); 272248258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 2723c5c4113dSnw141292 } 2724479ac375Sdm199847 2725cd37da74Snw141292 if (lower_winname != NULL && lower_winname != winname) 2726cd37da74Snw141292 free(lower_winname); 272762c60062Sbaban if (vm != NULL) 2728c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 2729c5c4113dSnw141292 return (retcode); 2730c5c4113dSnw141292 } 2731c5c4113dSnw141292 2732c5c4113dSnw141292 static 2733c5c4113dSnw141292 int 2734c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid) 2735c5c4113dSnw141292 { 2736c5c4113dSnw141292 uid_t uid; 2737c5c4113dSnw141292 gid_t gid; 2738c5c4113dSnw141292 int err; 2739c5c4113dSnw141292 2740c5c4113dSnw141292 *next_uid = (uid_t)-1; 2741c5c4113dSnw141292 uid = _idmapdstate.next_uid++; 2742c5c4113dSnw141292 if (uid >= _idmapdstate.limit_uid) { 2743c5c4113dSnw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0) 2744c5c4113dSnw141292 return (err); 2745c5c4113dSnw141292 2746c5c4113dSnw141292 _idmapdstate.limit_uid = uid + 8192; 2747c5c4113dSnw141292 _idmapdstate.next_uid = uid; 2748c5c4113dSnw141292 } 2749c5c4113dSnw141292 *next_uid = uid; 2750c5c4113dSnw141292 2751c5c4113dSnw141292 return (0); 2752c5c4113dSnw141292 } 2753c5c4113dSnw141292 2754c5c4113dSnw141292 static 2755c5c4113dSnw141292 int 2756c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid) 2757c5c4113dSnw141292 { 2758c5c4113dSnw141292 uid_t uid; 2759c5c4113dSnw141292 gid_t gid; 2760c5c4113dSnw141292 int err; 2761c5c4113dSnw141292 2762c5c4113dSnw141292 *next_gid = (uid_t)-1; 2763c5c4113dSnw141292 gid = _idmapdstate.next_gid++; 2764c5c4113dSnw141292 if (gid >= _idmapdstate.limit_gid) { 2765c5c4113dSnw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0) 2766c5c4113dSnw141292 return (err); 2767c5c4113dSnw141292 2768c5c4113dSnw141292 _idmapdstate.limit_gid = gid + 8192; 2769c5c4113dSnw141292 _idmapdstate.next_gid = gid; 2770c5c4113dSnw141292 } 2771c5c4113dSnw141292 *next_gid = gid; 2772c5c4113dSnw141292 2773c5c4113dSnw141292 return (0); 2774c5c4113dSnw141292 } 2775c5c4113dSnw141292 277662c60062Sbaban static 277762c60062Sbaban int 2778cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize) 2779cd37da74Snw141292 { 278062c60062Sbaban uint_t hval, i, len; 278162c60062Sbaban 278262c60062Sbaban if (str == NULL) 278362c60062Sbaban return (0); 278462c60062Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) { 278562c60062Sbaban hval += str[i]; 278662c60062Sbaban hval += (hval << 10); 278762c60062Sbaban hval ^= (hval >> 6); 278862c60062Sbaban } 278962c60062Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) { 279062c60062Sbaban hval += str[i]; 279162c60062Sbaban hval += (hval << 10); 279262c60062Sbaban hval ^= (hval >> 6); 279362c60062Sbaban } 279462c60062Sbaban hval += (hval << 3); 279562c60062Sbaban hval ^= (hval >> 11); 279662c60062Sbaban hval += (hval << 15); 279762c60062Sbaban return (hval % htsize); 279862c60062Sbaban } 279962c60062Sbaban 280062c60062Sbaban static 280162c60062Sbaban int 280262c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid, 2803cd37da74Snw141292 uid_t *pid) 2804cd37da74Snw141292 { 280562c60062Sbaban uint_t next, key; 280662c60062Sbaban uint_t htsize = state->sid_history_size; 280762c60062Sbaban idmap_sid *sid; 280862c60062Sbaban 280962c60062Sbaban next = gethash(prefix, rid, htsize); 281062c60062Sbaban while (next != htsize) { 281162c60062Sbaban key = state->sid_history[next].key; 281262c60062Sbaban if (key == htsize) 281362c60062Sbaban return (0); 281462c60062Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1. 281562c60062Sbaban idmap_id_u.sid; 281662c60062Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) { 281762c60062Sbaban *pid = state->result->ids.ids_val[key].id. 281862c60062Sbaban idmap_id_u.uid; 281962c60062Sbaban return (1); 282062c60062Sbaban } 282162c60062Sbaban next = state->sid_history[next].next; 282262c60062Sbaban } 282362c60062Sbaban return (0); 282462c60062Sbaban } 282562c60062Sbaban 282662c60062Sbaban static 282762c60062Sbaban void 2828cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid) 2829cd37da74Snw141292 { 283062c60062Sbaban uint_t hash, next; 283162c60062Sbaban uint_t htsize = state->sid_history_size; 283262c60062Sbaban 283362c60062Sbaban hash = next = gethash(prefix, rid, htsize); 283462c60062Sbaban while (state->sid_history[next].key != htsize) { 283562c60062Sbaban next++; 283662c60062Sbaban next %= htsize; 283762c60062Sbaban } 283862c60062Sbaban state->sid_history[next].key = state->curpos; 283962c60062Sbaban if (hash == next) 284062c60062Sbaban return; 284162c60062Sbaban state->sid_history[next].next = state->sid_history[hash].next; 284262c60062Sbaban state->sid_history[hash].next = next; 284362c60062Sbaban } 2844c5c4113dSnw141292 2845e8c27ec8Sbaban void 2846e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state) 2847e8c27ec8Sbaban { 2848e8c27ec8Sbaban free(state->sid_history); 2849e8c27ec8Sbaban free(state->ad_unixuser_attr); 2850e8c27ec8Sbaban free(state->ad_unixgroup_attr); 2851479ac375Sdm199847 free(state->nldap_winname_attr); 2852479ac375Sdm199847 free(state->defdom); 2853e8c27ec8Sbaban } 2854e8c27ec8Sbaban 2855c5c4113dSnw141292 /* ARGSUSED */ 2856c5c4113dSnw141292 static 2857c5c4113dSnw141292 idmap_retcode 2858479ac375Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state, 2859cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2860cd37da74Snw141292 { 2861c5c4113dSnw141292 2862c5c4113dSnw141292 uid_t next_pid; 2863c5c4113dSnw141292 2864651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 286562c60062Sbaban 286648258c6bSjp151216 if (IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 286748258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 286848258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 286962c60062Sbaban return (IDMAP_SUCCESS); 287048258c6bSjp151216 } 287162c60062Sbaban 287262c60062Sbaban if (state->sid_history != NULL && 287362c60062Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix, 287462c60062Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) { 287562c60062Sbaban res->id.idmap_id_u.uid = next_pid; 287648258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 287748258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 287862c60062Sbaban return (IDMAP_SUCCESS); 287962c60062Sbaban } 288062c60062Sbaban 288162c60062Sbaban if (res->id.idtype == IDMAP_UID) { 2882c5c4113dSnw141292 if (get_next_eph_uid(&next_pid) != 0) 2883c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2884c5c4113dSnw141292 res->id.idmap_id_u.uid = next_pid; 2885c5c4113dSnw141292 } else { 2886c5c4113dSnw141292 if (get_next_eph_gid(&next_pid) != 0) 2887c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2888c5c4113dSnw141292 res->id.idmap_id_u.gid = next_pid; 2889c5c4113dSnw141292 } 2890c5c4113dSnw141292 289148258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 289248258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 289362c60062Sbaban if (state->sid_history != NULL) 289462c60062Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix, 289562c60062Sbaban req->id1.idmap_id_u.sid.rid); 289662c60062Sbaban 2897c5c4113dSnw141292 return (IDMAP_SUCCESS); 2898c5c4113dSnw141292 } 2899c5c4113dSnw141292 2900c5c4113dSnw141292 idmap_retcode 2901479ac375Sdm199847 sid2pid_second_pass(lookup_state_t *state, 2902cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2903cd37da74Snw141292 { 2904c5c4113dSnw141292 idmap_retcode retcode; 2905c5c4113dSnw141292 2906c5c4113dSnw141292 /* Check if second pass is needed */ 2907e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2908c5c4113dSnw141292 return (res->retcode); 2909c5c4113dSnw141292 2910c5c4113dSnw141292 /* Get status from previous pass */ 2911e8c27ec8Sbaban retcode = res->retcode; 29124aa0a5e7Snw141292 if (retcode != IDMAP_SUCCESS && state->eph_map_unres_sids && 29134aa0a5e7Snw141292 !EMPTY_STRING(req->id1.idmap_id_u.sid.prefix) && 29144aa0a5e7Snw141292 EMPTY_STRING(req->id1name)) { 29154aa0a5e7Snw141292 /* 29164aa0a5e7Snw141292 * We are asked to map an unresolvable SID to a UID or 29174aa0a5e7Snw141292 * GID, but, which? We'll treat all unresolvable SIDs 29184aa0a5e7Snw141292 * as users unless the caller specified which of a UID 29194aa0a5e7Snw141292 * or GID they want. 29204aa0a5e7Snw141292 */ 2921*a7c8bd9fSNicolas Williams if (req->id1.idtype == IDMAP_SID) 2922*a7c8bd9fSNicolas Williams req->id1.idtype = IDMAP_USID; 29234aa0a5e7Snw141292 if (res->id.idtype == IDMAP_POSIXID) 29244aa0a5e7Snw141292 res->id.idtype = IDMAP_UID; 29254aa0a5e7Snw141292 goto do_eph; 29264aa0a5e7Snw141292 } 2927e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2928e8c27ec8Sbaban goto out; 2929c5c4113dSnw141292 2930e8c27ec8Sbaban /* 2931e8c27ec8Sbaban * If directory-based name mapping is enabled then the unixname 2932e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode or 2933e8c27ec8Sbaban * mixed-mode) or from native LDAP object (nldap-mode) -- done. 2934e8c27ec8Sbaban */ 2935e8c27ec8Sbaban if (req->id2name != NULL) { 2936e8c27ec8Sbaban assert(res->id.idtype != IDMAP_POSIXID); 2937e8c27ec8Sbaban if (AD_MODE(res->id.idtype, state)) 2938e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2939e8c27ec8Sbaban else if (NLDAP_MODE(res->id.idtype, state)) 2940e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2941e8c27ec8Sbaban else if (MIXED_MODE(res->id.idtype, state)) 2942e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 2943c5c4113dSnw141292 2944e8c27ec8Sbaban /* 2945e8c27ec8Sbaban * Special case: (1) If the ad_unixuser_attr and 2946e8c27ec8Sbaban * ad_unixgroup_attr uses the same attribute 2947e8c27ec8Sbaban * name and (2) if this is a diagonal mapping 2948e8c27ec8Sbaban * request and (3) the unixname has been retrieved 2949e8c27ec8Sbaban * from the AD object -- then we ignore it and fallback 2950e8c27ec8Sbaban * to name-based mapping rules and ephemeral mapping 2951e8c27ec8Sbaban * 2952e8c27ec8Sbaban * Example: 2953e8c27ec8Sbaban * Properties: 2954e8c27ec8Sbaban * config/ad_unixuser_attr = "unixname" 2955e8c27ec8Sbaban * config/ad_unixgroup_attr = "unixname" 2956e8c27ec8Sbaban * AD user object: 2957e8c27ec8Sbaban * dn: cn=bob ... 2958e8c27ec8Sbaban * objectclass: user 2959e8c27ec8Sbaban * sam: bob 2960e8c27ec8Sbaban * unixname: bob1234 2961e8c27ec8Sbaban * AD group object: 2962e8c27ec8Sbaban * dn: cn=winadmins ... 2963e8c27ec8Sbaban * objectclass: group 2964e8c27ec8Sbaban * sam: winadmins 2965e8c27ec8Sbaban * unixname: unixadmins 2966e8c27ec8Sbaban * 2967e8c27ec8Sbaban * In this example whether "unixname" refers to a unixuser 2968e8c27ec8Sbaban * or unixgroup depends upon the AD object. 2969e8c27ec8Sbaban * 2970e8c27ec8Sbaban * $idmap show -c winname:bob gid 2971e8c27ec8Sbaban * AD lookup by "samAccountName=bob" for 2972e8c27ec8Sbaban * "ad_unixgroup_attr (i.e unixname)" for directory-based 2973e8c27ec8Sbaban * mapping would get "bob1234" which is not what we want. 2974e8c27ec8Sbaban * Now why not getgrnam_r("bob1234") and use it if it 2975e8c27ec8Sbaban * is indeed a unixgroup? That's because Unix can have 2976e8c27ec8Sbaban * users and groups with the same name and we clearly 2977e8c27ec8Sbaban * don't know the intention of the admin here. 2978e8c27ec8Sbaban * Therefore we ignore this and fallback to name-based 2979e8c27ec8Sbaban * mapping rules or ephemeral mapping. 2980e8c27ec8Sbaban */ 2981e8c27ec8Sbaban if ((AD_MODE(res->id.idtype, state) || 2982e8c27ec8Sbaban MIXED_MODE(res->id.idtype, state)) && 2983e8c27ec8Sbaban state->ad_unixuser_attr != NULL && 2984e8c27ec8Sbaban state->ad_unixgroup_attr != NULL && 2985e8c27ec8Sbaban strcasecmp(state->ad_unixuser_attr, 2986e8c27ec8Sbaban state->ad_unixgroup_attr) == 0 && 2987e8c27ec8Sbaban ((req->id1.idtype == IDMAP_USID && 2988e8c27ec8Sbaban res->id.idtype == IDMAP_GID) || 2989e8c27ec8Sbaban (req->id1.idtype == IDMAP_GSID && 2990e8c27ec8Sbaban res->id.idtype == IDMAP_UID))) { 2991e8c27ec8Sbaban free(req->id2name); 2992e8c27ec8Sbaban req->id2name = NULL; 2993e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2994e8c27ec8Sbaban /* fallback */ 2995e8c27ec8Sbaban } else { 2996e8c27ec8Sbaban if (res->id.idmap_id_u.uid == SENTINEL_PID) 2997e8c27ec8Sbaban retcode = ns_lookup_byname(req->id2name, 2998e8c27ec8Sbaban NULL, &res->id); 2999e8c27ec8Sbaban /* 3000479ac375Sdm199847 * If ns_lookup_byname() fails that means the 3001479ac375Sdm199847 * unixname (req->id2name), which was obtained 3002479ac375Sdm199847 * from the AD object by directory-based mapping, 3003479ac375Sdm199847 * is not a valid Unix user/group and therefore 3004479ac375Sdm199847 * we return the error to the client instead of 3005479ac375Sdm199847 * doing rule-based mapping or ephemeral mapping. 3006479ac375Sdm199847 * This way the client can detect the issue. 3007e8c27ec8Sbaban */ 3008c5c4113dSnw141292 goto out; 3009c5c4113dSnw141292 } 3010e8c27ec8Sbaban } 3011c5c4113dSnw141292 301248258c6bSjp151216 /* Free any mapping info from Directory based mapping */ 301348258c6bSjp151216 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 301448258c6bSjp151216 idmap_info_free(&res->info); 301548258c6bSjp151216 3016e8c27ec8Sbaban /* 3017e8c27ec8Sbaban * If we don't have unixname then evaluate local name-based 3018e8c27ec8Sbaban * mapping rules. 3019e8c27ec8Sbaban */ 3020479ac375Sdm199847 retcode = name_based_mapping_sid2pid(state, req, res); 3021e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 3022e8c27ec8Sbaban goto out; 3023e8c27ec8Sbaban 30244aa0a5e7Snw141292 do_eph: 3025c5c4113dSnw141292 /* If not found, do ephemeral mapping */ 3026479ac375Sdm199847 retcode = dynamic_ephemeral_mapping(state, req, res); 3027c5c4113dSnw141292 3028c5c4113dSnw141292 out: 3029c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 3030e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 3031e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 3032e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 3033e8c27ec8Sbaban } 3034e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 3035e8c27ec8Sbaban state->sid2pid_done = FALSE; 3036c5c4113dSnw141292 return (retcode); 3037c5c4113dSnw141292 } 3038c5c4113dSnw141292 3039c5c4113dSnw141292 idmap_retcode 3040479ac375Sdm199847 update_cache_pid2sid(lookup_state_t *state, 3041cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3042cd37da74Snw141292 { 3043c5c4113dSnw141292 char *sql = NULL; 3044c5c4113dSnw141292 idmap_retcode retcode; 304548258c6bSjp151216 char *map_dn = NULL; 304648258c6bSjp151216 char *map_attr = NULL; 304748258c6bSjp151216 char *map_value = NULL; 304848258c6bSjp151216 char *map_windomain = NULL; 304948258c6bSjp151216 char *map_winname = NULL; 305048258c6bSjp151216 char *map_unixname = NULL; 305148258c6bSjp151216 int map_is_nt4 = FALSE; 3052c5c4113dSnw141292 3053c5c4113dSnw141292 /* Check if we need to cache anything */ 3054e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3055c5c4113dSnw141292 return (IDMAP_SUCCESS); 3056c5c4113dSnw141292 3057c5c4113dSnw141292 /* We don't cache negative entries */ 3058c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3059c5c4113dSnw141292 return (IDMAP_SUCCESS); 3060c5c4113dSnw141292 3061e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 306248258c6bSjp151216 assert(req->id1.idmap_id_u.uid != SENTINEL_PID); 306348258c6bSjp151216 assert(res->id.idtype != IDMAP_SID); 306448258c6bSjp151216 306548258c6bSjp151216 assert(res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN); 306648258c6bSjp151216 switch (res->info.how.map_type) { 306748258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 306848258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 306948258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 307048258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 307148258c6bSjp151216 break; 307248258c6bSjp151216 307348258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 307448258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 307548258c6bSjp151216 map_attr = res->info.how.idmap_how_u.nldap.attr; 307648258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 307748258c6bSjp151216 break; 307848258c6bSjp151216 307948258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 308048258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 308148258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 308248258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 308348258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 308448258c6bSjp151216 break; 308548258c6bSjp151216 308648258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 308748258c6bSjp151216 break; 308848258c6bSjp151216 308948258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 309048258c6bSjp151216 break; 309148258c6bSjp151216 309248258c6bSjp151216 default: 309348258c6bSjp151216 /* Dont cache other mapping types */ 309448258c6bSjp151216 assert(FALSE); 309548258c6bSjp151216 } 3096e8c27ec8Sbaban 3097c5c4113dSnw141292 /* 3098c5c4113dSnw141292 * Using NULL for u2w instead of 0 so that our trigger allows 3099c5c4113dSnw141292 * the same pid to be the destination in multiple entries 3100c5c4113dSnw141292 */ 3101c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3102cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 310348258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 310448258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 310548258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3106cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 310748258c6bSjp151216 "strftime('%%s','now') + 600, %q, 1, " 310848258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ", 3109cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3110cd37da74Snw141292 req->id2domain, req->id2name, req->id1.idmap_id_u.uid, 3111cd37da74Snw141292 req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0, 3112e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? 1 : 0, 311348258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 311448258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 311548258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3116c5c4113dSnw141292 3117c5c4113dSnw141292 if (sql == NULL) { 3118c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3119c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3120c5c4113dSnw141292 goto out; 3121c5c4113dSnw141292 } 3122c5c4113dSnw141292 3123479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3124c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3125c5c4113dSnw141292 goto out; 3126c5c4113dSnw141292 3127c5c4113dSnw141292 state->pid2sid_done = FALSE; 3128c5c4113dSnw141292 sqlite_freemem(sql); 3129c5c4113dSnw141292 sql = NULL; 3130c5c4113dSnw141292 3131e8c27ec8Sbaban /* Check if we need to update namecache */ 3132e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3133c5c4113dSnw141292 goto out; 3134c5c4113dSnw141292 31358e228215Sdm199847 if (req->id2name == NULL) 3136c5c4113dSnw141292 goto out; 3137c5c4113dSnw141292 3138c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3139cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3140c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3141cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3142cd37da74Snw141292 req->id2name, req->id2domain, 3143e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3144c5c4113dSnw141292 3145c5c4113dSnw141292 if (sql == NULL) { 3146c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3147c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3148c5c4113dSnw141292 goto out; 3149c5c4113dSnw141292 } 3150c5c4113dSnw141292 3151479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3152c5c4113dSnw141292 3153c5c4113dSnw141292 out: 315448258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 315548258c6bSjp151216 idmap_info_free(&res->info); 315662c60062Sbaban if (sql != NULL) 3157c5c4113dSnw141292 sqlite_freemem(sql); 3158c5c4113dSnw141292 return (retcode); 3159c5c4113dSnw141292 } 3160c5c4113dSnw141292 3161c5c4113dSnw141292 idmap_retcode 3162479ac375Sdm199847 update_cache_sid2pid(lookup_state_t *state, 3163cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3164cd37da74Snw141292 { 3165c5c4113dSnw141292 char *sql = NULL; 3166c5c4113dSnw141292 idmap_retcode retcode; 3167c5c4113dSnw141292 int is_eph_user; 316848258c6bSjp151216 char *map_dn = NULL; 316948258c6bSjp151216 char *map_attr = NULL; 317048258c6bSjp151216 char *map_value = NULL; 317148258c6bSjp151216 char *map_windomain = NULL; 317248258c6bSjp151216 char *map_winname = NULL; 317348258c6bSjp151216 char *map_unixname = NULL; 317448258c6bSjp151216 int map_is_nt4 = FALSE; 3175c5c4113dSnw141292 3176c5c4113dSnw141292 /* Check if we need to cache anything */ 3177e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3178c5c4113dSnw141292 return (IDMAP_SUCCESS); 3179c5c4113dSnw141292 3180c5c4113dSnw141292 /* We don't cache negative entries */ 3181c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3182c5c4113dSnw141292 return (IDMAP_SUCCESS); 3183c5c4113dSnw141292 3184c5c4113dSnw141292 if (req->direction & _IDMAP_F_EXP_EPH_UID) 3185c5c4113dSnw141292 is_eph_user = 1; 3186c5c4113dSnw141292 else if (req->direction & _IDMAP_F_EXP_EPH_GID) 3187c5c4113dSnw141292 is_eph_user = 0; 3188c5c4113dSnw141292 else 3189c5c4113dSnw141292 is_eph_user = -1; 3190c5c4113dSnw141292 3191c5c4113dSnw141292 if (is_eph_user >= 0 && !IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 3192c5c4113dSnw141292 sql = sqlite_mprintf("UPDATE idmap_cache " 3193c5c4113dSnw141292 "SET w2u = 0 WHERE " 3194c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 3195c5c4113dSnw141292 "pid >= 2147483648 AND is_user = %d;", 3196c5c4113dSnw141292 req->id1.idmap_id_u.sid.prefix, 3197c5c4113dSnw141292 req->id1.idmap_id_u.sid.rid, 3198c5c4113dSnw141292 is_eph_user); 3199c5c4113dSnw141292 if (sql == NULL) { 3200c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3201c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3202c5c4113dSnw141292 goto out; 3203c5c4113dSnw141292 } 3204c5c4113dSnw141292 3205479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3206c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3207c5c4113dSnw141292 goto out; 3208c5c4113dSnw141292 3209c5c4113dSnw141292 sqlite_freemem(sql); 3210c5c4113dSnw141292 sql = NULL; 3211c5c4113dSnw141292 } 3212c5c4113dSnw141292 3213e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 321448258c6bSjp151216 assert(res->id.idmap_id_u.uid != SENTINEL_PID); 321548258c6bSjp151216 321648258c6bSjp151216 switch (res->info.how.map_type) { 321748258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 321848258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 321948258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 322048258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 322148258c6bSjp151216 break; 322248258c6bSjp151216 322348258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 322448258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 322548258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 322648258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 322748258c6bSjp151216 break; 322848258c6bSjp151216 322948258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 323048258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 323148258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 323248258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 323348258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 323448258c6bSjp151216 break; 323548258c6bSjp151216 323648258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 323748258c6bSjp151216 break; 323848258c6bSjp151216 323948258c6bSjp151216 default: 324048258c6bSjp151216 /* Dont cache other mapping types */ 324148258c6bSjp151216 assert(FALSE); 324248258c6bSjp151216 } 3243cd37da74Snw141292 3244c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3245cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 324648258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 324748258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 324848258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3249cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 325048258c6bSjp151216 "strftime('%%s','now') + 600, 1, %q, " 325148258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);", 3252cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3253e8c27ec8Sbaban (req->id1domain != NULL) ? req->id1domain : "", req->id1name, 3254e8c27ec8Sbaban res->id.idmap_id_u.uid, req->id2name, 3255e8c27ec8Sbaban (res->id.idtype == IDMAP_UID) ? 1 : 0, 3256cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? 1 : 0, 325748258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 325848258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 325948258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3260c5c4113dSnw141292 3261c5c4113dSnw141292 if (sql == NULL) { 3262c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3263c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3264c5c4113dSnw141292 goto out; 3265c5c4113dSnw141292 } 3266c5c4113dSnw141292 3267479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3268c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3269c5c4113dSnw141292 goto out; 3270c5c4113dSnw141292 3271c5c4113dSnw141292 state->sid2pid_done = FALSE; 3272c5c4113dSnw141292 sqlite_freemem(sql); 3273c5c4113dSnw141292 sql = NULL; 3274c5c4113dSnw141292 3275e8c27ec8Sbaban /* Check if we need to update namecache */ 3276e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3277c5c4113dSnw141292 goto out; 3278c5c4113dSnw141292 3279cf5b5989Sdm199847 if (EMPTY_STRING(req->id1name)) 3280c5c4113dSnw141292 goto out; 3281c5c4113dSnw141292 3282c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3283cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3284c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3285cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3286cd37da74Snw141292 req->id1name, req->id1domain, 3287cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3288c5c4113dSnw141292 3289c5c4113dSnw141292 if (sql == NULL) { 3290c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3291c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3292c5c4113dSnw141292 goto out; 3293c5c4113dSnw141292 } 3294c5c4113dSnw141292 3295479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3296c5c4113dSnw141292 3297c5c4113dSnw141292 out: 329848258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 329948258c6bSjp151216 idmap_info_free(&res->info); 330048258c6bSjp151216 330162c60062Sbaban if (sql != NULL) 3302c5c4113dSnw141292 sqlite_freemem(sql); 3303c5c4113dSnw141292 return (retcode); 3304c5c4113dSnw141292 } 3305c5c4113dSnw141292 3306cd37da74Snw141292 static 3307cd37da74Snw141292 idmap_retcode 3308c5c4113dSnw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res, 3309cd37da74Snw141292 int is_user, int getname) 3310cd37da74Snw141292 { 3311c5c4113dSnw141292 char *end; 3312c5c4113dSnw141292 char *sql = NULL; 3313c5c4113dSnw141292 const char **values; 3314c5c4113dSnw141292 sqlite_vm *vm = NULL; 3315c5c4113dSnw141292 int ncol; 3316c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3317c5c4113dSnw141292 time_t curtime; 3318e8c27ec8Sbaban idmap_id_type idtype; 3319c5c4113dSnw141292 3320c5c4113dSnw141292 /* Current time */ 3321c5c4113dSnw141292 errno = 0; 3322c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3323cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3324c5c4113dSnw141292 strerror(errno)); 3325c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3326c5c4113dSnw141292 goto out; 3327c5c4113dSnw141292 } 3328c5c4113dSnw141292 3329e8c27ec8Sbaban /* SQL to lookup the cache by pid or by unixname */ 3330e8c27ec8Sbaban if (req->id1.idmap_id_u.uid != SENTINEL_PID) { 333148258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 333248258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 333348258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 333448258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3335c5c4113dSnw141292 "FROM idmap_cache WHERE " 3336c5c4113dSnw141292 "pid = %u AND u2w = 1 AND is_user = %d AND " 3337c5c4113dSnw141292 "(pid >= 2147483648 OR " 3338c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3339c5c4113dSnw141292 "expiration > %d));", 3340c5c4113dSnw141292 req->id1.idmap_id_u.uid, is_user, curtime); 3341e8c27ec8Sbaban } else if (req->id1name != NULL) { 334248258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 334348258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 334448258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 334548258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3346e8c27ec8Sbaban "FROM idmap_cache WHERE " 3347e8c27ec8Sbaban "unixname = %Q AND u2w = 1 AND is_user = %d AND " 3348e8c27ec8Sbaban "(pid >= 2147483648 OR " 3349e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 3350e8c27ec8Sbaban "expiration > %d));", 3351e8c27ec8Sbaban req->id1name, is_user, curtime); 335248258c6bSjp151216 } else { 335348258c6bSjp151216 retcode = IDMAP_ERR_ARG; 335448258c6bSjp151216 goto out; 3355e8c27ec8Sbaban } 3356e8c27ec8Sbaban 3357c5c4113dSnw141292 if (sql == NULL) { 3358c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3359c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3360c5c4113dSnw141292 goto out; 3361c5c4113dSnw141292 } 336248258c6bSjp151216 retcode = sql_compile_n_step_once( 336348258c6bSjp151216 cache, sql, &vm, &ncol, 14, &values); 3364c5c4113dSnw141292 sqlite_freemem(sql); 3365c5c4113dSnw141292 3366c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) 3367c5c4113dSnw141292 goto out; 3368c5c4113dSnw141292 else if (retcode == IDMAP_SUCCESS) { 3369c5c4113dSnw141292 /* sanity checks */ 3370c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 3371c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3372c5c4113dSnw141292 goto out; 3373c5c4113dSnw141292 } 3374c5c4113dSnw141292 3375e8c27ec8Sbaban switch (res->id.idtype) { 3376c5c4113dSnw141292 case IDMAP_SID: 3377cd37da74Snw141292 case IDMAP_USID: 3378cd37da74Snw141292 case IDMAP_GSID: 3379e8c27ec8Sbaban idtype = strtol(values[5], &end, 10) == 1 3380cd37da74Snw141292 ? IDMAP_USID : IDMAP_GSID; 3381cd37da74Snw141292 3382e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID && 3383e8c27ec8Sbaban idtype != IDMAP_USID) { 3384cd37da74Snw141292 retcode = IDMAP_ERR_NOTUSER; 3385cd37da74Snw141292 goto out; 3386e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GSID && 3387e8c27ec8Sbaban idtype != IDMAP_GSID) { 3388cd37da74Snw141292 retcode = IDMAP_ERR_NOTGROUP; 3389cd37da74Snw141292 goto out; 3390cd37da74Snw141292 } 3391e8c27ec8Sbaban res->id.idtype = idtype; 3392cd37da74Snw141292 3393c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 3394c5c4113dSnw141292 strtoul(values[1], &end, 10); 3395c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = strdup(values[0]); 3396c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 3397c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3398c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3399c5c4113dSnw141292 goto out; 3400c5c4113dSnw141292 } 3401c5c4113dSnw141292 340262c60062Sbaban if (values[4] != NULL) 3403c5c4113dSnw141292 res->direction = 3404651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 3405651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3406c5c4113dSnw141292 else 3407651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 3408c5c4113dSnw141292 3409c5c4113dSnw141292 if (getname == 0 || values[2] == NULL) 3410c5c4113dSnw141292 break; 34118e228215Sdm199847 req->id2name = strdup(values[2]); 34128e228215Sdm199847 if (req->id2name == NULL) { 3413c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3414c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3415c5c4113dSnw141292 goto out; 3416c5c4113dSnw141292 } 3417c5c4113dSnw141292 3418c5c4113dSnw141292 if (values[3] == NULL) 3419c5c4113dSnw141292 break; 34208e228215Sdm199847 req->id2domain = strdup(values[3]); 34218e228215Sdm199847 if (req->id2domain == NULL) { 3422c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3423c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3424c5c4113dSnw141292 goto out; 3425c5c4113dSnw141292 } 3426cd37da74Snw141292 3427c5c4113dSnw141292 break; 3428c5c4113dSnw141292 default: 3429c5c4113dSnw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 3430c5c4113dSnw141292 break; 3431c5c4113dSnw141292 } 343248258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 343348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 343448258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 343548258c6bSjp151216 switch (res->info.how.map_type) { 343648258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 343748258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 343848258c6bSjp151216 strdup(values[7]); 343948258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 344048258c6bSjp151216 strdup(values[8]); 344148258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 344248258c6bSjp151216 strdup(values[9]); 344348258c6bSjp151216 break; 344448258c6bSjp151216 344548258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 344648258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 344748258c6bSjp151216 strdup(values[7]); 344848258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 344948258c6bSjp151216 strdup(values[8]); 345048258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 345148258c6bSjp151216 strdup(values[9]); 345248258c6bSjp151216 break; 345348258c6bSjp151216 345448258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 345548258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 345648258c6bSjp151216 strdup(values[10]); 345748258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 345848258c6bSjp151216 strdup(values[11]); 345948258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 346048258c6bSjp151216 strdup(values[12]); 346148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 346248258c6bSjp151216 strtoul(values[13], &end, 10); 346348258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 346448258c6bSjp151216 is_user; 346548258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 346648258c6bSjp151216 strtol(values[5], &end, 10); 346748258c6bSjp151216 break; 346848258c6bSjp151216 346948258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 347048258c6bSjp151216 break; 347148258c6bSjp151216 347248258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 347348258c6bSjp151216 break; 347448258c6bSjp151216 347548258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 347648258c6bSjp151216 break; 347748258c6bSjp151216 347848258c6bSjp151216 default: 347948258c6bSjp151216 /* Unknow mapping type */ 348048258c6bSjp151216 assert(FALSE); 348148258c6bSjp151216 } 348248258c6bSjp151216 } 3483c5c4113dSnw141292 } 3484c5c4113dSnw141292 3485c5c4113dSnw141292 out: 348662c60062Sbaban if (vm != NULL) 3487c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3488c5c4113dSnw141292 return (retcode); 3489c5c4113dSnw141292 } 3490c5c4113dSnw141292 3491cd37da74Snw141292 static 3492cd37da74Snw141292 idmap_retcode 3493c5c4113dSnw141292 lookup_cache_name2sid(sqlite *cache, const char *name, const char *domain, 3494cd37da74Snw141292 char **canonname, char **sidprefix, idmap_rid_t *rid, int *type) 3495cd37da74Snw141292 { 3496cd37da74Snw141292 char *end, *lower_name; 3497c5c4113dSnw141292 char *sql = NULL; 3498c5c4113dSnw141292 const char **values; 3499c5c4113dSnw141292 sqlite_vm *vm = NULL; 3500c5c4113dSnw141292 int ncol; 3501c5c4113dSnw141292 time_t curtime; 3502c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3503c5c4113dSnw141292 3504c5c4113dSnw141292 /* Get current time */ 3505c5c4113dSnw141292 errno = 0; 3506c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3507cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3508c5c4113dSnw141292 strerror(errno)); 3509c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3510c5c4113dSnw141292 goto out; 3511c5c4113dSnw141292 } 3512c5c4113dSnw141292 3513c5c4113dSnw141292 /* SQL to lookup the cache */ 3514cd37da74Snw141292 if ((lower_name = tolower_u8(name)) == NULL) 3515cd37da74Snw141292 lower_name = (char *)name; 3516cd37da74Snw141292 sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name " 3517cd37da74Snw141292 "FROM name_cache WHERE name = %Q AND domain = %Q AND " 3518c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3519cd37da74Snw141292 "expiration > %d);", lower_name, domain, curtime); 3520cd37da74Snw141292 if (lower_name != name) 3521cd37da74Snw141292 free(lower_name); 3522c5c4113dSnw141292 if (sql == NULL) { 3523c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3524c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3525c5c4113dSnw141292 goto out; 3526c5c4113dSnw141292 } 3527cd37da74Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values); 3528c5c4113dSnw141292 sqlite_freemem(sql); 3529c5c4113dSnw141292 3530c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 353162c60062Sbaban if (type != NULL) { 3532c5c4113dSnw141292 if (values[2] == NULL) { 3533c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3534c5c4113dSnw141292 goto out; 3535c5c4113dSnw141292 } 3536c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 3537c5c4113dSnw141292 } 3538c5c4113dSnw141292 3539e8c27ec8Sbaban if (values[0] == NULL || values[1] == NULL) { 3540e8c27ec8Sbaban retcode = IDMAP_ERR_CACHE; 3541e8c27ec8Sbaban goto out; 3542e8c27ec8Sbaban } 3543e8c27ec8Sbaban 3544cd37da74Snw141292 if (canonname != NULL) { 3545cd37da74Snw141292 assert(values[3] != NULL); 3546cd37da74Snw141292 if ((*canonname = strdup(values[3])) == NULL) { 3547cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3548cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 3549cd37da74Snw141292 goto out; 3550cd37da74Snw141292 } 3551cd37da74Snw141292 } 3552cd37da74Snw141292 3553c5c4113dSnw141292 if ((*sidprefix = strdup(values[0])) == NULL) { 3554c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3555c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3556e8c27ec8Sbaban if (canonname != NULL) { 3557e8c27ec8Sbaban free(*canonname); 3558e8c27ec8Sbaban *canonname = NULL; 3559e8c27ec8Sbaban } 3560c5c4113dSnw141292 goto out; 3561c5c4113dSnw141292 } 3562c5c4113dSnw141292 *rid = strtoul(values[1], &end, 10); 3563c5c4113dSnw141292 } 3564c5c4113dSnw141292 3565c5c4113dSnw141292 out: 356662c60062Sbaban if (vm != NULL) 3567c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3568c5c4113dSnw141292 return (retcode); 3569c5c4113dSnw141292 } 3570c5c4113dSnw141292 3571cd37da74Snw141292 static 3572cd37da74Snw141292 idmap_retcode 3573e8c27ec8Sbaban ad_lookup_by_winname(lookup_state_t *state, 3574e8c27ec8Sbaban const char *name, const char *domain, int eunixtype, 357548258c6bSjp151216 char **dn, char **attr, char **value, char **canonname, 357648258c6bSjp151216 char **sidprefix, idmap_rid_t *rid, int *wintype, 357748258c6bSjp151216 char **unixname) 3578cd37da74Snw141292 { 3579c5c4113dSnw141292 int retries = 0; 3580c5c4113dSnw141292 idmap_query_state_t *qs = NULL; 3581c5c4113dSnw141292 idmap_retcode rc, retcode; 3582c5c4113dSnw141292 3583c5c4113dSnw141292 retry: 3584e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, 1, &qs); 3585e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 35860dcc7149Snw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 35870dcc7149Snw141292 goto retry; 3588349d5d8fSnw141292 degrade_svc(1, "failed to create request for AD lookup " 35890dcc7149Snw141292 "by winname"); 3590e8c27ec8Sbaban return (retcode); 3591c5c4113dSnw141292 } 3592c5c4113dSnw141292 3593c8e26105Sjp151216 restore_svc(); 3594c8e26105Sjp151216 3595e8c27ec8Sbaban if (state != NULL) 3596e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 3597e8c27ec8Sbaban state->ad_unixgroup_attr); 3598c5c4113dSnw141292 3599e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1(qs, name, domain, eunixtype, 360048258c6bSjp151216 dn, attr, value, canonname, sidprefix, rid, wintype, unixname, &rc); 3601c5c4113dSnw141292 3602e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 360384decf41Sjp151216 idmap_lookup_release_batch(&qs); 3604c5c4113dSnw141292 else 36050dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 3606c5c4113dSnw141292 3607c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 3608c5c4113dSnw141292 goto retry; 3609c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 3610349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 3611c5c4113dSnw141292 3612c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) { 3613e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by winname failed"); 3614c5c4113dSnw141292 return (retcode); 3615e8c27ec8Sbaban } 3616c5c4113dSnw141292 return (rc); 3617c5c4113dSnw141292 } 3618c5c4113dSnw141292 3619cd37da74Snw141292 idmap_retcode 3620c5c4113dSnw141292 lookup_name2sid(sqlite *cache, const char *name, const char *domain, 3621cd37da74Snw141292 int *is_wuser, char **canonname, char **sidprefix, 3622479ac375Sdm199847 idmap_rid_t *rid, idmap_mapping *req, int local_only) 3623cd37da74Snw141292 { 3624c5c4113dSnw141292 int type; 3625c5c4113dSnw141292 idmap_retcode retcode; 3626c5c4113dSnw141292 3627cd37da74Snw141292 *sidprefix = NULL; 3628e8c27ec8Sbaban if (canonname != NULL) 3629cd37da74Snw141292 *canonname = NULL; 3630cd37da74Snw141292 3631e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3632cd37da74Snw141292 retcode = lookup_wksids_name2sid(name, canonname, sidprefix, rid, 3633cd37da74Snw141292 &type); 363462c60062Sbaban if (retcode == IDMAP_SUCCESS) { 3635e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 363662c60062Sbaban goto out; 363762c60062Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 363862c60062Sbaban return (retcode); 363962c60062Sbaban } 364062c60062Sbaban 3641e8c27ec8Sbaban /* Lookup cache */ 3642cd37da74Snw141292 retcode = lookup_cache_name2sid(cache, name, domain, canonname, 3643cd37da74Snw141292 sidprefix, rid, &type); 3644e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 3645e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 3646e8c27ec8Sbaban goto out; 3647e8c27ec8Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 3648e8c27ec8Sbaban return (retcode); 3649e8c27ec8Sbaban } 3650e8c27ec8Sbaban 3651479ac375Sdm199847 /* 3652479ac375Sdm199847 * The caller may be using this function to determine if this 3653479ac375Sdm199847 * request needs to be marked for AD lookup or not 3654479ac375Sdm199847 * (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this 3655479ac375Sdm199847 * function to AD lookup now. 3656479ac375Sdm199847 */ 3657479ac375Sdm199847 if (local_only) 3658479ac375Sdm199847 return (retcode); 3659479ac375Sdm199847 3660e8c27ec8Sbaban /* Lookup AD */ 3661e8c27ec8Sbaban retcode = ad_lookup_by_winname(NULL, name, domain, _IDMAP_T_UNDEF, 366248258c6bSjp151216 NULL, NULL, NULL, canonname, sidprefix, rid, &type, NULL); 3663c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3664c5c4113dSnw141292 return (retcode); 3665c5c4113dSnw141292 366662c60062Sbaban out: 3667c5c4113dSnw141292 /* 3668c5c4113dSnw141292 * Entry found (cache or Windows lookup) 3669cd37da74Snw141292 * is_wuser is both input as well as output parameter 3670c5c4113dSnw141292 */ 3671e8c27ec8Sbaban if (*is_wuser == 1 && type != _IDMAP_T_USER) 3672e8c27ec8Sbaban retcode = IDMAP_ERR_NOTUSER; 3673e8c27ec8Sbaban else if (*is_wuser == 0 && type != _IDMAP_T_GROUP) 3674e8c27ec8Sbaban retcode = IDMAP_ERR_NOTGROUP; 3675e8c27ec8Sbaban else if (*is_wuser == -1) { 3676c5c4113dSnw141292 /* Caller wants to know if its user or group */ 3677c5c4113dSnw141292 if (type == _IDMAP_T_USER) 3678cd37da74Snw141292 *is_wuser = 1; 3679c5c4113dSnw141292 else if (type == _IDMAP_T_GROUP) 3680cd37da74Snw141292 *is_wuser = 0; 3681e8c27ec8Sbaban else 3682e8c27ec8Sbaban retcode = IDMAP_ERR_SID; 3683e8c27ec8Sbaban } 3684e8c27ec8Sbaban 3685e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3686cd37da74Snw141292 free(*sidprefix); 3687cd37da74Snw141292 *sidprefix = NULL; 3688e8c27ec8Sbaban if (canonname != NULL) { 3689cd37da74Snw141292 free(*canonname); 3690cd37da74Snw141292 *canonname = NULL; 3691cd37da74Snw141292 } 3692c5c4113dSnw141292 } 3693c5c4113dSnw141292 return (retcode); 3694c5c4113dSnw141292 } 3695c5c4113dSnw141292 3696cd37da74Snw141292 static 3697cd37da74Snw141292 idmap_retcode 3698479ac375Sdm199847 name_based_mapping_pid2sid(lookup_state_t *state, const char *unixname, 3699cd37da74Snw141292 int is_user, idmap_mapping *req, idmap_id_res *res) 3700cd37da74Snw141292 { 3701c5c4113dSnw141292 const char *winname, *windomain; 3702cd37da74Snw141292 char *canonname; 3703c5c4113dSnw141292 char *sql = NULL, *errmsg = NULL; 3704c5c4113dSnw141292 idmap_retcode retcode; 3705c5c4113dSnw141292 char *end; 3706c5c4113dSnw141292 const char **values; 3707c5c4113dSnw141292 sqlite_vm *vm = NULL; 370848258c6bSjp151216 int ncol, r; 3709cd37da74Snw141292 int is_wuser; 3710e8c27ec8Sbaban const char *me = "name_based_mapping_pid2sid"; 371148258c6bSjp151216 int non_wild_match = FALSE; 371248258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 371348258c6bSjp151216 int direction; 3714e8c27ec8Sbaban 3715e8c27ec8Sbaban assert(unixname != NULL); /* We have unixname */ 3716e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have winname */ 3717e8c27ec8Sbaban assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */ 3718c5c4113dSnw141292 3719c5c4113dSnw141292 sql = sqlite_mprintf( 372048258c6bSjp151216 "SELECT winname_display, windomain, w2u_order, " 372148258c6bSjp151216 "is_wuser, unixname, is_nt4 " 372248258c6bSjp151216 "FROM namerules WHERE " 3723c5c4113dSnw141292 "u2w_order > 0 AND is_user = %d AND " 3724c5c4113dSnw141292 "(unixname = %Q OR unixname = '*') " 3725cd37da74Snw141292 "ORDER BY u2w_order ASC;", is_user, unixname); 3726c5c4113dSnw141292 if (sql == NULL) { 3727c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3728c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3729c5c4113dSnw141292 goto out; 3730c5c4113dSnw141292 } 3731c5c4113dSnw141292 3732479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 3733c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3734cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3735cd37da74Snw141292 CHECK_NULL(errmsg)); 3736c5c4113dSnw141292 sqlite_freemem(errmsg); 3737c5c4113dSnw141292 goto out; 3738c5c4113dSnw141292 } 3739c5c4113dSnw141292 374048258c6bSjp151216 for (;;) { 3741c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 374284decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 374384decf41Sjp151216 if (r == SQLITE_ROW) { 374448258c6bSjp151216 if (ncol < 6) { 3745c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3746c5c4113dSnw141292 goto out; 3747c5c4113dSnw141292 } 3748c5c4113dSnw141292 if (values[0] == NULL) { 3749c5c4113dSnw141292 /* values [1] and [2] can be null */ 3750c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3751c5c4113dSnw141292 goto out; 3752c5c4113dSnw141292 } 375348258c6bSjp151216 375448258c6bSjp151216 if (values[2] != NULL) 375548258c6bSjp151216 direction = 375648258c6bSjp151216 (strtol(values[2], &end, 10) == 0)? 375748258c6bSjp151216 IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 375848258c6bSjp151216 else 375948258c6bSjp151216 direction = IDMAP_DIRECTION_U2W; 376048258c6bSjp151216 3761c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 376248258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], 376348258c6bSjp151216 values[4], is_user, 376448258c6bSjp151216 strtol(values[3], &end, 10), 376548258c6bSjp151216 strtol(values[5], &end, 10), 376648258c6bSjp151216 direction); 3767c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 3768c5c4113dSnw141292 goto out; 3769c5c4113dSnw141292 } 3770cd37da74Snw141292 3771cd37da74Snw141292 if (values[0][0] == '*') { 377248258c6bSjp151216 winname = unixname; 377348258c6bSjp151216 if (non_wild_match) { 3774cd37da74Snw141292 /* 377548258c6bSjp151216 * There were non-wildcard rules 377648258c6bSjp151216 * where the Windows identity doesn't 377748258c6bSjp151216 * exist. Return no mapping. 3778cd37da74Snw141292 */ 3779cd37da74Snw141292 retcode = IDMAP_ERR_NOMAPPING; 3780cd37da74Snw141292 goto out; 3781cd37da74Snw141292 } 3782cd37da74Snw141292 } else { 378348258c6bSjp151216 /* Save first non-wild match rule */ 378448258c6bSjp151216 if (!non_wild_match) { 378548258c6bSjp151216 idmap_namerule_set(rule, values[1], 378648258c6bSjp151216 values[0], values[4], 378748258c6bSjp151216 is_user, 378848258c6bSjp151216 strtol(values[3], &end, 10), 378948258c6bSjp151216 strtol(values[5], &end, 10), 379048258c6bSjp151216 direction); 379148258c6bSjp151216 non_wild_match = TRUE; 379248258c6bSjp151216 } 3793cd37da74Snw141292 winname = values[0]; 3794cd37da74Snw141292 } 379548258c6bSjp151216 is_wuser = res->id.idtype == IDMAP_USID ? 1 379648258c6bSjp151216 : res->id.idtype == IDMAP_GSID ? 0 379748258c6bSjp151216 : -1; 379862c60062Sbaban if (values[1] != NULL) 3799c5c4113dSnw141292 windomain = values[1]; 3800479ac375Sdm199847 else if (state->defdom != NULL) 3801479ac375Sdm199847 windomain = state->defdom; 3802c5c4113dSnw141292 else { 3803cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: no domain", me); 3804c5c4113dSnw141292 retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 3805c5c4113dSnw141292 goto out; 3806c5c4113dSnw141292 } 3807cd37da74Snw141292 3808479ac375Sdm199847 retcode = lookup_name2sid(state->cache, 3809479ac375Sdm199847 winname, windomain, 3810cd37da74Snw141292 &is_wuser, &canonname, 3811cd37da74Snw141292 &res->id.idmap_id_u.sid.prefix, 3812479ac375Sdm199847 &res->id.idmap_id_u.sid.rid, req, 0); 3813e8c27ec8Sbaban 3814c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 3815c5c4113dSnw141292 continue; 3816c5c4113dSnw141292 } 3817c5c4113dSnw141292 goto out; 381848258c6bSjp151216 3819c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 382048258c6bSjp151216 /* 382148258c6bSjp151216 * If there were non-wildcard rules where 382248258c6bSjp151216 * Windows identity doesn't exist 382348258c6bSjp151216 * return no mapping. 382448258c6bSjp151216 */ 382548258c6bSjp151216 if (non_wild_match) 382648258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 382748258c6bSjp151216 else 3828c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 3829c5c4113dSnw141292 goto out; 3830c5c4113dSnw141292 } else { 3831c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 3832c5c4113dSnw141292 vm = NULL; 3833cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3834cd37da74Snw141292 CHECK_NULL(errmsg)); 3835c5c4113dSnw141292 sqlite_freemem(errmsg); 3836c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3837c5c4113dSnw141292 goto out; 3838c5c4113dSnw141292 } 3839c5c4113dSnw141292 } 3840c5c4113dSnw141292 3841c5c4113dSnw141292 out: 384262c60062Sbaban if (sql != NULL) 3843c5c4113dSnw141292 sqlite_freemem(sql); 384448258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 3845c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 3846cd37da74Snw141292 res->id.idtype = is_wuser ? IDMAP_USID : IDMAP_GSID; 3847cd37da74Snw141292 384862c60062Sbaban if (values[2] != NULL) 3849c5c4113dSnw141292 res->direction = 3850651c0131Sbaban (strtol(values[2], &end, 10) == 0)? 3851651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3852c5c4113dSnw141292 else 3853651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 38548e228215Sdm199847 3855cd37da74Snw141292 req->id2name = canonname; 38568e228215Sdm199847 if (req->id2name != NULL) { 38578e228215Sdm199847 req->id2domain = strdup(windomain); 3858479ac375Sdm199847 if (req->id2domain == NULL) 3859479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 38608e228215Sdm199847 } 3861c5c4113dSnw141292 } 3862479ac375Sdm199847 3863479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 386448258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], values[4], 386548258c6bSjp151216 is_user, strtol(values[3], &end, 10), 386648258c6bSjp151216 strtol(values[5], &end, 10), 386748258c6bSjp151216 rule->direction); 386848258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 3869c5c4113dSnw141292 } 387062c60062Sbaban if (vm != NULL) 3871c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3872c5c4113dSnw141292 return (retcode); 3873c5c4113dSnw141292 } 3874c5c4113dSnw141292 3875cd37da74Snw141292 /* 3876cd37da74Snw141292 * Convention when processing unix2win requests: 3877cd37da74Snw141292 * 3878cd37da74Snw141292 * Unix identity: 3879cd37da74Snw141292 * req->id1name = 3880cd37da74Snw141292 * unixname if given otherwise unixname found will be placed 3881cd37da74Snw141292 * here. 3882cd37da74Snw141292 * req->id1domain = 3883cd37da74Snw141292 * NOT USED 3884cd37da74Snw141292 * req->id1.idtype = 3885cd37da74Snw141292 * Given type (IDMAP_UID or IDMAP_GID) 3886cd37da74Snw141292 * req->id1..[uid or gid] = 3887cd37da74Snw141292 * UID/GID if given otherwise UID/GID found will be placed here. 3888cd37da74Snw141292 * 3889cd37da74Snw141292 * Windows identity: 3890cd37da74Snw141292 * req->id2name = 3891cd37da74Snw141292 * winname found will be placed here. 3892cd37da74Snw141292 * req->id2domain = 3893cd37da74Snw141292 * windomain found will be placed here. 3894cd37da74Snw141292 * res->id.idtype = 3895cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 3896cd37da74Snw141292 * it is IDMAP_SID then actual type (IDMAP_USID/GSID) found 3897cd37da74Snw141292 * will be placed here. 3898cd37da74Snw141292 * req->id..sid.[prefix, rid] = 3899cd37da74Snw141292 * SID found will be placed here. 3900cd37da74Snw141292 * 3901cd37da74Snw141292 * Others: 3902cd37da74Snw141292 * res->retcode = 3903cd37da74Snw141292 * Return status for this request will be placed here. 3904cd37da74Snw141292 * res->direction = 3905cd37da74Snw141292 * Direction found will be placed here. Direction 3906cd37da74Snw141292 * meaning whether the resultant mapping is valid 3907cd37da74Snw141292 * only from unix2win or bi-directional. 3908cd37da74Snw141292 * req->direction = 3909cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 3910cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 3911cd37da74Snw141292 * of the request. 3912cd37da74Snw141292 * req->id2.idtype = 3913cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 3914cd37da74Snw141292 * type and is used to initialize res->id.idtype. 3915cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 3916cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 3917cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 3918cd37da74Snw141292 * mis-interpreting the contents of req->id2. 3919cd37da74Snw141292 * req->id2..[uid or gid or sid] = 3920cd37da74Snw141292 * NOT USED 3921cd37da74Snw141292 */ 3922cd37da74Snw141292 3923cd37da74Snw141292 /* 3924cd37da74Snw141292 * This function does the following: 3925cd37da74Snw141292 * 1. Lookup well-known SIDs table. 3926cd37da74Snw141292 * 2. Lookup cache. 3927cd37da74Snw141292 * 3. Check if the client does not want new mapping to be allocated 3928cd37da74Snw141292 * in which case this pass is the final pass. 3929e8c27ec8Sbaban * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs 3930e8c27ec8Sbaban * to do AD/NLDAP lookup. 3931cd37da74Snw141292 */ 3932c5c4113dSnw141292 idmap_retcode 3933479ac375Sdm199847 pid2sid_first_pass(lookup_state_t *state, idmap_mapping *req, 3934479ac375Sdm199847 idmap_id_res *res, int is_user, int getname) 3935cd37da74Snw141292 { 3936e8c27ec8Sbaban idmap_retcode retcode; 3937e8c27ec8Sbaban bool_t gen_localsid_on_err = FALSE; 3938c5c4113dSnw141292 3939e8c27ec8Sbaban /* Initialize result */ 3940c5c4113dSnw141292 res->id.idtype = req->id2.idtype; 3941e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 3942c5c4113dSnw141292 3943e8c27ec8Sbaban if (req->id2.idmap_id_u.sid.prefix != NULL) { 3944e8c27ec8Sbaban /* sanitize sidprefix */ 3945e8c27ec8Sbaban free(req->id2.idmap_id_u.sid.prefix); 3946e8c27ec8Sbaban req->id2.idmap_id_u.sid.prefix = NULL; 3947e8c27ec8Sbaban } 3948e8c27ec8Sbaban 394948258c6bSjp151216 /* Find pid */ 395048258c6bSjp151216 if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 395148258c6bSjp151216 if (ns_lookup_byname(req->id1name, NULL, &req->id1) 395248258c6bSjp151216 != IDMAP_SUCCESS) { 395348258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 395448258c6bSjp151216 goto out; 395548258c6bSjp151216 } 395648258c6bSjp151216 } 395748258c6bSjp151216 3958e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3959c5c4113dSnw141292 retcode = lookup_wksids_pid2sid(req, res, is_user); 3960c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3961c5c4113dSnw141292 goto out; 3962c5c4113dSnw141292 3963e8c27ec8Sbaban /* Lookup cache */ 3964479ac375Sdm199847 retcode = lookup_cache_pid2sid(state->cache, req, res, is_user, 3965479ac375Sdm199847 getname); 3966c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3967c5c4113dSnw141292 goto out; 3968c5c4113dSnw141292 3969c5c4113dSnw141292 /* Ephemeral ids cannot be allocated during pid2sid */ 3970c5c4113dSnw141292 if (IS_EPHEMERAL(req->id1.idmap_id_u.uid)) { 397162c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3972c5c4113dSnw141292 goto out; 3973c5c4113dSnw141292 } 3974c5c4113dSnw141292 397548258c6bSjp151216 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) { 397648258c6bSjp151216 retcode = IDMAP_ERR_NONEGENERATED; 397748258c6bSjp151216 goto out; 397848258c6bSjp151216 } 397948258c6bSjp151216 398048258c6bSjp151216 if (AVOID_NAMESERVICE(req)) { 3981e8c27ec8Sbaban gen_localsid_on_err = TRUE; 398262c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3983c5c4113dSnw141292 goto out; 3984c5c4113dSnw141292 } 3985c5c4113dSnw141292 3986e8c27ec8Sbaban /* Set flags for the next stage */ 3987e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) { 3988e8c27ec8Sbaban /* 3989e8c27ec8Sbaban * If AD-based name mapping is enabled then the next stage 3990e8c27ec8Sbaban * will need to lookup AD using unixname to get the 3991e8c27ec8Sbaban * corresponding winname. 3992e8c27ec8Sbaban */ 3993e8c27ec8Sbaban if (req->id1name == NULL) { 3994e8c27ec8Sbaban /* Get unixname if only pid is given. */ 3995e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, 3996e8c27ec8Sbaban is_user, &req->id1name); 3997479ac375Sdm199847 if (retcode != IDMAP_SUCCESS) { 3998479ac375Sdm199847 gen_localsid_on_err = TRUE; 3999e8c27ec8Sbaban goto out; 4000c5c4113dSnw141292 } 4001479ac375Sdm199847 } 4002e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 4003e8c27ec8Sbaban state->ad_nqueries++; 4004e8c27ec8Sbaban } else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) { 4005e8c27ec8Sbaban /* 4006e8c27ec8Sbaban * If native LDAP or mixed mode is enabled for name mapping 4007e8c27ec8Sbaban * then the next stage will need to lookup native LDAP using 4008e8c27ec8Sbaban * unixname/pid to get the corresponding winname. 4009e8c27ec8Sbaban */ 4010e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_NLDAP; 4011e8c27ec8Sbaban state->nldap_nqueries++; 4012c5c4113dSnw141292 } 4013c5c4113dSnw141292 4014e8c27ec8Sbaban /* 4015e8c27ec8Sbaban * Failed to find non-expired entry in cache. Set the flag to 4016e8c27ec8Sbaban * indicate that we are not done yet. 4017e8c27ec8Sbaban */ 4018e8c27ec8Sbaban state->pid2sid_done = FALSE; 4019e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 4020e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 4021e8c27ec8Sbaban 4022e8c27ec8Sbaban out: 4023e8c27ec8Sbaban res->retcode = idmap_stat4prot(retcode); 4024e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 4025e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 402648258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 4027e8c27ec8Sbaban return (retcode); 4028e8c27ec8Sbaban } 4029e8c27ec8Sbaban 4030e8c27ec8Sbaban idmap_retcode 4031479ac375Sdm199847 pid2sid_second_pass(lookup_state_t *state, idmap_mapping *req, 4032479ac375Sdm199847 idmap_id_res *res, int is_user) 4033e8c27ec8Sbaban { 4034e8c27ec8Sbaban bool_t gen_localsid_on_err = TRUE; 4035e8c27ec8Sbaban idmap_retcode retcode = IDMAP_SUCCESS; 4036e8c27ec8Sbaban 4037e8c27ec8Sbaban /* Check if second pass is needed */ 4038e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 4039e8c27ec8Sbaban return (res->retcode); 4040e8c27ec8Sbaban 4041e8c27ec8Sbaban /* Get status from previous pass */ 4042e8c27ec8Sbaban retcode = res->retcode; 4043e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4044e8c27ec8Sbaban goto out; 4045e8c27ec8Sbaban 4046e8c27ec8Sbaban /* 4047e8c27ec8Sbaban * If directory-based name mapping is enabled then the winname 4048e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode) 4049479ac375Sdm199847 * or from native LDAP object (nldap-mode or mixed-mode). 4050479ac375Sdm199847 * Note that if we have winname but no SID then it's an error 4051479ac375Sdm199847 * because this implies that the Native LDAP entry contains 4052479ac375Sdm199847 * winname which does not exist and it's better that we return 4053479ac375Sdm199847 * an error instead of doing rule-based mapping so that the user 4054479ac375Sdm199847 * can detect the issue and take appropriate action. 4055e8c27ec8Sbaban */ 4056479ac375Sdm199847 if (req->id2name != NULL) { 4057479ac375Sdm199847 /* Return notfound if we've winname but no SID. */ 4058479ac375Sdm199847 if (res->id.idmap_id_u.sid.prefix == NULL) { 4059479ac375Sdm199847 retcode = IDMAP_ERR_NOTFOUND; 4060479ac375Sdm199847 goto out; 4061479ac375Sdm199847 } 4062e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) 4063e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4064e8c27ec8Sbaban else if (NLDAP_MODE(req->id1.idtype, state)) 4065e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4066e8c27ec8Sbaban else if (MIXED_MODE(req->id1.idtype, state)) 4067e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 4068e8c27ec8Sbaban goto out; 4069479ac375Sdm199847 } else if (res->id.idmap_id_u.sid.prefix != NULL) { 4070479ac375Sdm199847 /* 4071479ac375Sdm199847 * We've SID but no winname. This is fine because 4072479ac375Sdm199847 * the caller may have only requested SID. 4073479ac375Sdm199847 */ 4074479ac375Sdm199847 goto out; 4075e8c27ec8Sbaban } 4076e8c27ec8Sbaban 4077479ac375Sdm199847 /* Free any mapping info from Directory based mapping */ 4078479ac375Sdm199847 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 4079479ac375Sdm199847 idmap_info_free(&res->info); 4080479ac375Sdm199847 4081e8c27ec8Sbaban if (req->id1name == NULL) { 4082e8c27ec8Sbaban /* Get unixname from name service */ 4083e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user, 4084e8c27ec8Sbaban &req->id1name); 4085e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4086e8c27ec8Sbaban goto out; 4087e8c27ec8Sbaban } else if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 4088e8c27ec8Sbaban /* Get pid from name service */ 4089e8c27ec8Sbaban retcode = ns_lookup_byname(req->id1name, NULL, &req->id1); 4090e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4091e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4092e8c27ec8Sbaban goto out; 4093e8c27ec8Sbaban } 4094e8c27ec8Sbaban } 4095e8c27ec8Sbaban 4096e8c27ec8Sbaban /* Use unixname to evaluate local name-based mapping rules */ 4097479ac375Sdm199847 retcode = name_based_mapping_pid2sid(state, req->id1name, is_user, 4098c5c4113dSnw141292 req, res); 4099c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 410048258c6bSjp151216 retcode = generate_localsid(req, res, is_user, FALSE); 4101e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4102e8c27ec8Sbaban } 4103c5c4113dSnw141292 4104c5c4113dSnw141292 out: 4105c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 4106e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 4107e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 4108479ac375Sdm199847 free(req->id2name); 4109479ac375Sdm199847 req->id2name = NULL; 4110479ac375Sdm199847 free(req->id2domain); 4111479ac375Sdm199847 req->id2domain = NULL; 4112e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 411348258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 4114479ac375Sdm199847 else 4115479ac375Sdm199847 res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 4116e8c27ec8Sbaban } 4117e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 4118e8c27ec8Sbaban state->pid2sid_done = FALSE; 4119c5c4113dSnw141292 return (retcode); 4120c5c4113dSnw141292 } 4121c5c4113dSnw141292 4122cd37da74Snw141292 static 4123cd37da74Snw141292 int 4124651c0131Sbaban copy_mapping_request(idmap_mapping *mapping, idmap_mapping *request) 4125c5c4113dSnw141292 { 4126651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 4127651c0131Sbaban 4128c5c4113dSnw141292 mapping->flag = request->flag; 4129e8c27ec8Sbaban mapping->direction = _IDMAP_F_DONE; 4130651c0131Sbaban mapping->id2.idtype = request->id2.idtype; 4131c5c4113dSnw141292 4132c5c4113dSnw141292 mapping->id1.idtype = request->id1.idtype; 4133cd37da74Snw141292 if (IS_REQUEST_SID(*request, 1)) { 4134c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.rid = 4135c5c4113dSnw141292 request->id1.idmap_id_u.sid.rid; 4136651c0131Sbaban if (!EMPTY_STRING(request->id1.idmap_id_u.sid.prefix)) { 4137c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.prefix = 4138c5c4113dSnw141292 strdup(request->id1.idmap_id_u.sid.prefix); 4139651c0131Sbaban if (mapping->id1.idmap_id_u.sid.prefix == NULL) 41408e228215Sdm199847 goto errout; 4141651c0131Sbaban } 4142c5c4113dSnw141292 } else { 4143c5c4113dSnw141292 mapping->id1.idmap_id_u.uid = request->id1.idmap_id_u.uid; 4144c5c4113dSnw141292 } 4145c5c4113dSnw141292 4146e8c27ec8Sbaban if (!EMPTY_STRING(request->id1domain)) { 41478e228215Sdm199847 mapping->id1domain = strdup(request->id1domain); 41488e228215Sdm199847 if (mapping->id1domain == NULL) 41498e228215Sdm199847 goto errout; 4150e8c27ec8Sbaban } 4151c5c4113dSnw141292 4152e8c27ec8Sbaban if (!EMPTY_STRING(request->id1name)) { 41538e228215Sdm199847 mapping->id1name = strdup(request->id1name); 41548e228215Sdm199847 if (mapping->id1name == NULL) 41558e228215Sdm199847 goto errout; 4156e8c27ec8Sbaban } 4157c5c4113dSnw141292 4158651c0131Sbaban /* We don't need the rest of the request i.e request->id2 */ 4159651c0131Sbaban return (0); 4160c5c4113dSnw141292 4161651c0131Sbaban errout: 41628e228215Sdm199847 if (mapping->id1.idmap_id_u.sid.prefix != NULL) 4163651c0131Sbaban free(mapping->id1.idmap_id_u.sid.prefix); 41648e228215Sdm199847 if (mapping->id1domain != NULL) 41658e228215Sdm199847 free(mapping->id1domain); 41668e228215Sdm199847 if (mapping->id1name != NULL) 41678e228215Sdm199847 free(mapping->id1name); 4168651c0131Sbaban 4169651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 4170651c0131Sbaban return (-1); 4171c5c4113dSnw141292 } 4172c5c4113dSnw141292 4173c5c4113dSnw141292 4174c5c4113dSnw141292 idmap_retcode 4175c5c4113dSnw141292 get_w2u_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 4176cd37da74Snw141292 idmap_mapping *mapping) 4177cd37da74Snw141292 { 4178c5c4113dSnw141292 idmap_id_res idres; 4179c5c4113dSnw141292 lookup_state_t state; 4180dd5829d1Sbaban char *cp; 4181c5c4113dSnw141292 idmap_retcode retcode; 4182c5c4113dSnw141292 const char *winname, *windomain; 4183c5c4113dSnw141292 4184c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4185c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 4186479ac375Sdm199847 state.cache = cache; 4187479ac375Sdm199847 state.db = db; 4188c5c4113dSnw141292 4189e8c27ec8Sbaban /* Get directory-based name mapping info */ 4190479ac375Sdm199847 retcode = load_cfg_in_state(&state); 4191e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4192c5c4113dSnw141292 goto out; 4193c5c4113dSnw141292 4194e8c27ec8Sbaban /* 4195e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 4196e8c27ec8Sbaban * empty strings are not copied from "request" to 4197e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 4198e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 4199e8c27ec8Sbaban * check for empty strings henceforth. 4200e8c27ec8Sbaban */ 4201651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 4202651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 4203651c0131Sbaban goto out; 4204651c0131Sbaban } 4205c5c4113dSnw141292 42068e228215Sdm199847 winname = mapping->id1name; 42078e228215Sdm199847 windomain = mapping->id1domain; 4208c5c4113dSnw141292 4209e8c27ec8Sbaban if (winname == NULL && windomain != NULL) { 4210c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 4211c5c4113dSnw141292 goto out; 4212c5c4113dSnw141292 } 4213c5c4113dSnw141292 4214e8c27ec8Sbaban /* Need atleast winname or sid to proceed */ 4215e8c27ec8Sbaban if (winname == NULL && mapping->id1.idmap_id_u.sid.prefix == NULL) { 4216e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 4217e8c27ec8Sbaban goto out; 4218e8c27ec8Sbaban } 4219e8c27ec8Sbaban 4220e8c27ec8Sbaban /* 4221e8c27ec8Sbaban * If domainname is not given but we have a fully qualified 4222e8c27ec8Sbaban * winname then extract the domainname from the winname, 4223e8c27ec8Sbaban * otherwise use the default_domain from the config 4224e8c27ec8Sbaban */ 4225e8c27ec8Sbaban if (winname != NULL && windomain == NULL) { 42268e228215Sdm199847 retcode = IDMAP_SUCCESS; 4227dd5829d1Sbaban if ((cp = strchr(winname, '@')) != NULL) { 4228dd5829d1Sbaban *cp = '\0'; 42298e228215Sdm199847 mapping->id1domain = strdup(cp + 1); 42308e228215Sdm199847 if (mapping->id1domain == NULL) 42318e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 4232e8c27ec8Sbaban } else if (lookup_wksids_name2sid(winname, NULL, NULL, NULL, 4233e8c27ec8Sbaban NULL) != IDMAP_SUCCESS) { 423482da9f60Sbaban if (state.defdom == NULL) { 423582da9f60Sbaban /* 423682da9f60Sbaban * We have a non-qualified winname which is 423782da9f60Sbaban * neither the name of a well-known SID nor 423882da9f60Sbaban * there is a default domain with which we can 423982da9f60Sbaban * qualify it. 424082da9f60Sbaban */ 424182da9f60Sbaban retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 424282da9f60Sbaban } else { 4243479ac375Sdm199847 mapping->id1domain = strdup(state.defdom); 42448e228215Sdm199847 if (mapping->id1domain == NULL) 42458e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 42468e228215Sdm199847 } 424782da9f60Sbaban } 4248c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 4249c5c4113dSnw141292 goto out; 4250c5c4113dSnw141292 } 4251c5c4113dSnw141292 4252e8c27ec8Sbaban /* 4253e8c27ec8Sbaban * First pass looks up the well-known SIDs table and cache 4254e8c27ec8Sbaban * and handles localSIDs 4255e8c27ec8Sbaban */ 4256c5c4113dSnw141292 state.sid2pid_done = TRUE; 4257479ac375Sdm199847 retcode = sid2pid_first_pass(&state, mapping, &idres); 4258c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 4259c5c4113dSnw141292 goto out; 4260c5c4113dSnw141292 4261479ac375Sdm199847 /* AD lookup */ 4262e8c27ec8Sbaban if (state.ad_nqueries > 0) { 4263479ac375Sdm199847 retcode = ad_lookup_one(&state, mapping, &idres); 4264e8c27ec8Sbaban if (IDMAP_ERROR(retcode)) 4265e8c27ec8Sbaban goto out; 4266c5c4113dSnw141292 } 4267c5c4113dSnw141292 4268479ac375Sdm199847 /* nldap lookup */ 4269479ac375Sdm199847 if (state.nldap_nqueries > 0) { 4270479ac375Sdm199847 retcode = nldap_lookup_one(&state, mapping, &idres); 4271479ac375Sdm199847 if (IDMAP_FATAL_ERROR(retcode)) 4272e8c27ec8Sbaban goto out; 427348258c6bSjp151216 } 4274e8c27ec8Sbaban 4275e8c27ec8Sbaban /* Next pass performs name-based mapping and ephemeral mapping. */ 4276c5c4113dSnw141292 state.sid2pid_done = TRUE; 4277479ac375Sdm199847 retcode = sid2pid_second_pass(&state, mapping, &idres); 4278c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 4279c5c4113dSnw141292 goto out; 4280c5c4113dSnw141292 4281c5c4113dSnw141292 /* Update cache */ 4282479ac375Sdm199847 (void) update_cache_sid2pid(&state, mapping, &idres); 4283c5c4113dSnw141292 4284c5c4113dSnw141292 out: 4285e8c27ec8Sbaban /* 4286e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 4287e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 4288e8c27ec8Sbaban * free idres. 4289e8c27ec8Sbaban */ 4290c5c4113dSnw141292 mapping->direction = idres.direction; 4291c5c4113dSnw141292 mapping->id2 = idres.id; 429248258c6bSjp151216 if (mapping->flag & IDMAP_REQ_FLG_MAPPING_INFO || 429348258c6bSjp151216 retcode != IDMAP_SUCCESS) 429448258c6bSjp151216 (void) idmap_info_mov(&mapping->info, &idres.info); 429548258c6bSjp151216 else 429648258c6bSjp151216 idmap_info_free(&idres.info); 4297c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4298e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 429962c60062Sbaban mapping->id2.idmap_id_u.uid = UID_NOBODY; 4300c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 4301e8c27ec8Sbaban cleanup_lookup_state(&state); 4302c5c4113dSnw141292 return (retcode); 4303c5c4113dSnw141292 } 4304c5c4113dSnw141292 4305c5c4113dSnw141292 idmap_retcode 4306c5c4113dSnw141292 get_u2w_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 4307cd37da74Snw141292 idmap_mapping *mapping, int is_user) 4308cd37da74Snw141292 { 4309c5c4113dSnw141292 idmap_id_res idres; 4310c5c4113dSnw141292 lookup_state_t state; 4311c5c4113dSnw141292 idmap_retcode retcode; 4312c5c4113dSnw141292 4313c5c4113dSnw141292 /* 4314c5c4113dSnw141292 * In order to re-use the pid2sid code, we convert 4315c5c4113dSnw141292 * our input data into structs that are expected by 4316c5c4113dSnw141292 * pid2sid_first_pass. 4317c5c4113dSnw141292 */ 4318c5c4113dSnw141292 4319c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4320c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 4321479ac375Sdm199847 state.cache = cache; 4322479ac375Sdm199847 state.db = db; 4323c5c4113dSnw141292 4324e8c27ec8Sbaban /* Get directory-based name mapping info */ 4325479ac375Sdm199847 retcode = load_cfg_in_state(&state); 4326e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4327e8c27ec8Sbaban goto out; 4328e8c27ec8Sbaban 4329e8c27ec8Sbaban /* 4330e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 4331e8c27ec8Sbaban * empty strings are not copied from "request" to 4332e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 4333e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 4334e8c27ec8Sbaban * check for empty strings henceforth. 4335e8c27ec8Sbaban */ 4336651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 4337651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 4338651c0131Sbaban goto out; 4339651c0131Sbaban } 4340c5c4113dSnw141292 4341e8c27ec8Sbaban /* 4342e8c27ec8Sbaban * For unix to windows mapping request, we need atleast a 4343e8c27ec8Sbaban * unixname or uid/gid to proceed 4344e8c27ec8Sbaban */ 4345e8c27ec8Sbaban if (mapping->id1name == NULL && 4346cf5b5989Sdm199847 mapping->id1.idmap_id_u.uid == SENTINEL_PID) { 4347c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 4348c5c4113dSnw141292 goto out; 4349c5c4113dSnw141292 } 4350c5c4113dSnw141292 4351e8c27ec8Sbaban /* First pass looks up cache and well-known SIDs */ 4352e8c27ec8Sbaban state.pid2sid_done = TRUE; 4353479ac375Sdm199847 retcode = pid2sid_first_pass(&state, mapping, &idres, is_user, 1); 4354e8c27ec8Sbaban if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 4355c5c4113dSnw141292 goto out; 4356e8c27ec8Sbaban 4357479ac375Sdm199847 /* nldap lookup */ 4358e8c27ec8Sbaban if (state.nldap_nqueries > 0) { 4359479ac375Sdm199847 retcode = nldap_lookup_one(&state, mapping, &idres); 4360e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 4361c5c4113dSnw141292 goto out; 4362479ac375Sdm199847 } 4363e8c27ec8Sbaban 4364479ac375Sdm199847 /* AD lookup */ 4365479ac375Sdm199847 if (state.ad_nqueries > 0) { 4366479ac375Sdm199847 retcode = ad_lookup_one(&state, mapping, &idres); 4367e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 4368e8c27ec8Sbaban goto out; 4369c5c4113dSnw141292 } 4370c5c4113dSnw141292 4371e8c27ec8Sbaban /* 4372e8c27ec8Sbaban * Next pass processes the result of the preceding passes/lookups. 4373e8c27ec8Sbaban * It returns if there's nothing more to be done otherwise it 4374e8c27ec8Sbaban * evaluates local name-based mapping rules 4375e8c27ec8Sbaban */ 4376c5c4113dSnw141292 state.pid2sid_done = TRUE; 4377479ac375Sdm199847 retcode = pid2sid_second_pass(&state, mapping, &idres, is_user); 4378c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 4379c5c4113dSnw141292 goto out; 4380c5c4113dSnw141292 4381c5c4113dSnw141292 /* Update cache */ 4382479ac375Sdm199847 (void) update_cache_pid2sid(&state, mapping, &idres); 4383c5c4113dSnw141292 4384c5c4113dSnw141292 out: 4385e8c27ec8Sbaban /* 4386e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 4387e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 4388e8c27ec8Sbaban * free idres. 4389e8c27ec8Sbaban */ 4390c5c4113dSnw141292 mapping->direction = idres.direction; 4391c5c4113dSnw141292 mapping->id2 = idres.id; 439248258c6bSjp151216 if (mapping->flag & IDMAP_REQ_FLG_MAPPING_INFO || 439348258c6bSjp151216 retcode != IDMAP_SUCCESS) 439448258c6bSjp151216 (void) idmap_info_mov(&mapping->info, &idres.info); 439548258c6bSjp151216 else 439648258c6bSjp151216 idmap_info_free(&idres.info); 4397c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4398c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 4399e8c27ec8Sbaban cleanup_lookup_state(&state); 4400e8c27ec8Sbaban return (retcode); 4401e8c27ec8Sbaban } 4402e8c27ec8Sbaban 4403479ac375Sdm199847 /*ARGSUSED*/ 4404e8c27ec8Sbaban static 4405e8c27ec8Sbaban idmap_retcode 4406479ac375Sdm199847 ad_lookup_one(lookup_state_t *state, idmap_mapping *req, idmap_id_res *res) 4407e8c27ec8Sbaban { 4408479ac375Sdm199847 idmap_mapping_batch batch; 4409479ac375Sdm199847 idmap_ids_res result; 4410e8c27ec8Sbaban 4411479ac375Sdm199847 batch.idmap_mapping_batch_len = 1; 4412479ac375Sdm199847 batch.idmap_mapping_batch_val = req; 4413479ac375Sdm199847 result.ids.ids_len = 1; 4414479ac375Sdm199847 result.ids.ids_val = res; 4415479ac375Sdm199847 return (ad_lookup_batch(state, &batch, &result)); 4416c5c4113dSnw141292 } 4417