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 #pragma ident "%Z%%M% %I% %E% SMI" 27c5c4113dSnw141292 28c5c4113dSnw141292 /* 29c5c4113dSnw141292 * Database related utility routines 30c5c4113dSnw141292 */ 31c5c4113dSnw141292 32c5c4113dSnw141292 #include <stdio.h> 33c5c4113dSnw141292 #include <stdlib.h> 34c5c4113dSnw141292 #include <string.h> 35c5c4113dSnw141292 #include <errno.h> 36c5c4113dSnw141292 #include <sys/types.h> 37c5c4113dSnw141292 #include <sys/stat.h> 38c5c4113dSnw141292 #include <rpc/rpc.h> 39c5c4113dSnw141292 #include <sys/sid.h> 40c5c4113dSnw141292 #include <time.h> 41c5c4113dSnw141292 #include <pwd.h> 42c5c4113dSnw141292 #include <grp.h> 4384decf41Sjp151216 #include <pthread.h> 4484decf41Sjp151216 #include <assert.h> 45cd37da74Snw141292 #include <sys/u8_textprep.h> 46c5c4113dSnw141292 47c5c4113dSnw141292 #include "idmapd.h" 48c5c4113dSnw141292 #include "adutils.h" 49c5c4113dSnw141292 #include "string.h" 50c5c4113dSnw141292 #include "idmap_priv.h" 51cd37da74Snw141292 #include "schema.h" 52e8c27ec8Sbaban #include "nldaputils.h" 53c5c4113dSnw141292 5484decf41Sjp151216 55c5c4113dSnw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *, 56c5c4113dSnw141292 sqlite_vm **, int *, int, const char ***); 57479ac375Sdm199847 static idmap_retcode ad_lookup_one(lookup_state_t *, idmap_mapping *, 58479ac375Sdm199847 idmap_id_res *); 59e8c27ec8Sbaban static idmap_retcode lookup_localsid2pid(idmap_mapping *, idmap_id_res *); 60e8c27ec8Sbaban static idmap_retcode lookup_cache_name2sid(sqlite *, const char *, 61e8c27ec8Sbaban const char *, char **, char **, idmap_rid_t *, int *); 62e8c27ec8Sbaban 63c5c4113dSnw141292 64c5c4113dSnw141292 #define EMPTY_NAME(name) (*name == 0 || strcmp(name, "\"\"") == 0) 65c5c4113dSnw141292 66c5c4113dSnw141292 #define DO_NOT_ALLOC_NEW_ID_MAPPING(req)\ 67c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC) 68c5c4113dSnw141292 69c5c4113dSnw141292 #define AVOID_NAMESERVICE(req)\ 70c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE) 71c5c4113dSnw141292 72e8c27ec8Sbaban #define IS_EPHEMERAL(pid) (pid > INT32_MAX && pid != SENTINEL_PID) 73c5c4113dSnw141292 74c5c4113dSnw141292 #define LOCALRID_MIN 1000 75c5c4113dSnw141292 76c5c4113dSnw141292 77c5c4113dSnw141292 typedef enum init_db_option { 78c5c4113dSnw141292 FAIL_IF_CORRUPT = 0, 79c5c4113dSnw141292 REMOVE_IF_CORRUPT = 1 80c5c4113dSnw141292 } init_db_option_t; 81c5c4113dSnw141292 8284decf41Sjp151216 /* 83e8c27ec8Sbaban * Data structure to store well-known SIDs and 84e8c27ec8Sbaban * associated mappings (if any) 85e8c27ec8Sbaban */ 86e8c27ec8Sbaban typedef struct wksids_table { 87e8c27ec8Sbaban const char *sidprefix; 88e8c27ec8Sbaban uint32_t rid; 89e8c27ec8Sbaban const char *winname; 90e8c27ec8Sbaban int is_wuser; 91e8c27ec8Sbaban uid_t pid; 92e8c27ec8Sbaban int is_user; 93e8c27ec8Sbaban int direction; 94e8c27ec8Sbaban } wksids_table_t; 95e8c27ec8Sbaban 96e8c27ec8Sbaban /* 9784decf41Sjp151216 * Thread specfic data to hold the database handles so that the 9884decf41Sjp151216 * databaes are not opened and closed for every request. It also 9984decf41Sjp151216 * contains the sqlite busy handler structure. 10084decf41Sjp151216 */ 10184decf41Sjp151216 10284decf41Sjp151216 struct idmap_busy { 10384decf41Sjp151216 const char *name; 10484decf41Sjp151216 const int *delays; 10584decf41Sjp151216 int delay_size; 10684decf41Sjp151216 int total; 10784decf41Sjp151216 int sec; 10884decf41Sjp151216 }; 10984decf41Sjp151216 11084decf41Sjp151216 11184decf41Sjp151216 typedef struct idmap_tsd { 11284decf41Sjp151216 sqlite *db_db; 11384decf41Sjp151216 sqlite *cache_db; 11484decf41Sjp151216 struct idmap_busy cache_busy; 11584decf41Sjp151216 struct idmap_busy db_busy; 11684decf41Sjp151216 } idmap_tsd_t; 11784decf41Sjp151216 11884decf41Sjp151216 11984decf41Sjp151216 12084decf41Sjp151216 static const int cache_delay_table[] = 12184decf41Sjp151216 { 1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 12284decf41Sjp151216 50, 50, 60, 70, 80, 90, 100}; 12384decf41Sjp151216 12484decf41Sjp151216 static const int db_delay_table[] = 12584decf41Sjp151216 { 5, 10, 15, 20, 30, 40, 55, 70, 100}; 12684decf41Sjp151216 12784decf41Sjp151216 12884decf41Sjp151216 static pthread_key_t idmap_tsd_key; 12984decf41Sjp151216 13084decf41Sjp151216 void 13184decf41Sjp151216 idmap_tsd_destroy(void *key) 13284decf41Sjp151216 { 13384decf41Sjp151216 13484decf41Sjp151216 idmap_tsd_t *tsd = (idmap_tsd_t *)key; 13584decf41Sjp151216 if (tsd) { 13684decf41Sjp151216 if (tsd->db_db) 13784decf41Sjp151216 (void) sqlite_close(tsd->db_db); 13884decf41Sjp151216 if (tsd->cache_db) 13984decf41Sjp151216 (void) sqlite_close(tsd->cache_db); 14084decf41Sjp151216 free(tsd); 14184decf41Sjp151216 } 14284decf41Sjp151216 } 14384decf41Sjp151216 14484decf41Sjp151216 int 145cd37da74Snw141292 idmap_init_tsd_key(void) 146cd37da74Snw141292 { 14784decf41Sjp151216 return (pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy)); 14884decf41Sjp151216 } 14984decf41Sjp151216 15084decf41Sjp151216 15184decf41Sjp151216 15284decf41Sjp151216 idmap_tsd_t * 15384decf41Sjp151216 idmap_get_tsd(void) 15484decf41Sjp151216 { 15584decf41Sjp151216 idmap_tsd_t *tsd; 15684decf41Sjp151216 15784decf41Sjp151216 if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) { 15884decf41Sjp151216 /* No thread specific data so create it */ 15984decf41Sjp151216 if ((tsd = malloc(sizeof (*tsd))) != NULL) { 16084decf41Sjp151216 /* Initialize thread specific data */ 16184decf41Sjp151216 (void) memset(tsd, 0, sizeof (*tsd)); 16284decf41Sjp151216 /* save the trhread specific data */ 16384decf41Sjp151216 if (pthread_setspecific(idmap_tsd_key, tsd) != 0) { 16484decf41Sjp151216 /* Can't store key */ 16584decf41Sjp151216 free(tsd); 16684decf41Sjp151216 tsd = NULL; 16784decf41Sjp151216 } 16884decf41Sjp151216 } else { 16984decf41Sjp151216 tsd = NULL; 17084decf41Sjp151216 } 17184decf41Sjp151216 } 17284decf41Sjp151216 17384decf41Sjp151216 return (tsd); 17484decf41Sjp151216 } 17584decf41Sjp151216 176cd37da74Snw141292 /* 177cd37da74Snw141292 * A simple wrapper around u8_textprep_str() that returns the Unicode 178cd37da74Snw141292 * lower-case version of some string. The result must be freed. 179cd37da74Snw141292 */ 180cd37da74Snw141292 char * 181cd37da74Snw141292 tolower_u8(const char *s) 182cd37da74Snw141292 { 183cd37da74Snw141292 char *res = NULL; 184cd37da74Snw141292 char *outs; 185cd37da74Snw141292 size_t inlen, outlen, inbytesleft, outbytesleft; 186cd37da74Snw141292 int rc, err; 187cd37da74Snw141292 188cd37da74Snw141292 /* 189cd37da74Snw141292 * u8_textprep_str() does not allocate memory. The input and 190cd37da74Snw141292 * output buffers may differ in size (though that would be more 191cd37da74Snw141292 * likely when normalization is done). We have to loop over it... 192cd37da74Snw141292 * 193cd37da74Snw141292 * To improve the chances that we can avoid looping we add 10 194cd37da74Snw141292 * bytes of output buffer room the first go around. 195cd37da74Snw141292 */ 196cd37da74Snw141292 inlen = inbytesleft = strlen(s); 197cd37da74Snw141292 outlen = outbytesleft = inlen + 10; 198cd37da74Snw141292 if ((res = malloc(outlen)) == NULL) 199cd37da74Snw141292 return (NULL); 200cd37da74Snw141292 outs = res; 201cd37da74Snw141292 202cd37da74Snw141292 while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs, 203cd37da74Snw141292 &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 && 204cd37da74Snw141292 err == E2BIG) { 205cd37da74Snw141292 if ((res = realloc(res, outlen + inbytesleft)) == NULL) 206cd37da74Snw141292 return (NULL); 207cd37da74Snw141292 /* adjust input/output buffer pointers */ 208cd37da74Snw141292 s += (inlen - inbytesleft); 209cd37da74Snw141292 outs = res + outlen - outbytesleft; 210cd37da74Snw141292 /* adjust outbytesleft and outlen */ 211cd37da74Snw141292 outlen += inbytesleft; 212cd37da74Snw141292 outbytesleft += inbytesleft; 213cd37da74Snw141292 } 214cd37da74Snw141292 215cd37da74Snw141292 if (rc < 0) { 216cd37da74Snw141292 free(res); 217cd37da74Snw141292 res = NULL; 218cd37da74Snw141292 return (NULL); 219cd37da74Snw141292 } 220cd37da74Snw141292 221cd37da74Snw141292 res[outlen - outbytesleft] = '\0'; 222cd37da74Snw141292 223cd37da74Snw141292 return (res); 224cd37da74Snw141292 } 225cd37da74Snw141292 226cd37da74Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 227cd37da74Snw141292 const char *while_doing); 228cd37da74Snw141292 229c5c4113dSnw141292 230c5c4113dSnw141292 /* 231c5c4113dSnw141292 * Initialize 'dbname' using 'sql' 232c5c4113dSnw141292 */ 233cd37da74Snw141292 static 234cd37da74Snw141292 int 235cd37da74Snw141292 init_db_instance(const char *dbname, int version, 236cd37da74Snw141292 const char *detect_version_sql, char * const *sql, 237cd37da74Snw141292 init_db_option_t opt, int *created, int *upgraded) 238c5c4113dSnw141292 { 239cd37da74Snw141292 int rc, curr_version; 240cd37da74Snw141292 int tries = 1; 241cd37da74Snw141292 int prio = LOG_NOTICE; 242c5c4113dSnw141292 sqlite *db = NULL; 243cd37da74Snw141292 char *errmsg = NULL; 244c5c4113dSnw141292 245cd37da74Snw141292 *created = 0; 246cd37da74Snw141292 *upgraded = 0; 247c5c4113dSnw141292 248cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 249cd37da74Snw141292 tries = 3; 250cd37da74Snw141292 251cd37da74Snw141292 rinse_repeat: 252cd37da74Snw141292 if (tries == 0) { 253cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname); 254c5c4113dSnw141292 return (-1); 255cd37da74Snw141292 } 256cd37da74Snw141292 if (tries-- == 1) 257cd37da74Snw141292 /* Last try, log errors */ 258cd37da74Snw141292 prio = LOG_ERR; 259c5c4113dSnw141292 260cd37da74Snw141292 db = sqlite_open(dbname, 0600, &errmsg); 261cd37da74Snw141292 if (db == NULL) { 262cd37da74Snw141292 idmapdlog(prio, "Error creating database %s (%s)", 263cd37da74Snw141292 dbname, CHECK_NULL(errmsg)); 264cd37da74Snw141292 sqlite_freemem(errmsg); 265cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 266c5c4113dSnw141292 (void) unlink(dbname); 267cd37da74Snw141292 goto rinse_repeat; 268c5c4113dSnw141292 } 269c5c4113dSnw141292 270c5c4113dSnw141292 sqlite_busy_timeout(db, 3000); 271cd37da74Snw141292 272cd37da74Snw141292 /* Detect current version of schema in the db, if any */ 273cd37da74Snw141292 curr_version = 0; 274cd37da74Snw141292 if (detect_version_sql != NULL) { 275cd37da74Snw141292 char *end, **results; 276cd37da74Snw141292 int nrow; 277cd37da74Snw141292 278cd37da74Snw141292 #ifdef IDMAPD_DEBUG 279cd37da74Snw141292 (void) fprintf(stderr, "Schema version detection SQL: %s\n", 280cd37da74Snw141292 detect_version_sql); 281cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 282cd37da74Snw141292 rc = sqlite_get_table(db, detect_version_sql, &results, 283cd37da74Snw141292 &nrow, NULL, &errmsg); 284cd37da74Snw141292 if (rc != SQLITE_OK) { 285cd37da74Snw141292 idmapdlog(prio, 286cd37da74Snw141292 "Error detecting schema version of db %s (%s)", 287cd37da74Snw141292 dbname, errmsg); 288cd37da74Snw141292 sqlite_freemem(errmsg); 289cd37da74Snw141292 sqlite_free_table(results); 290c5c4113dSnw141292 sqlite_close(db); 291cd37da74Snw141292 return (-1); 292cd37da74Snw141292 } 293cd37da74Snw141292 if (nrow != 1) { 294cd37da74Snw141292 idmapdlog(prio, 295cd37da74Snw141292 "Error detecting schema version of db %s", dbname); 296cd37da74Snw141292 sqlite_close(db); 297cd37da74Snw141292 sqlite_free_table(results); 298cd37da74Snw141292 return (-1); 299cd37da74Snw141292 } 300cd37da74Snw141292 curr_version = strtol(results[1], &end, 10); 301cd37da74Snw141292 sqlite_free_table(results); 302c5c4113dSnw141292 } 303c5c4113dSnw141292 304cd37da74Snw141292 if (curr_version < 0) { 305cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 306cd37da74Snw141292 (void) unlink(dbname); 307cd37da74Snw141292 goto rinse_repeat; 308c5c4113dSnw141292 } 309c5c4113dSnw141292 310cd37da74Snw141292 if (curr_version == version) 311cd37da74Snw141292 goto done; 312cd37da74Snw141292 313cd37da74Snw141292 /* Install or upgrade schema */ 314cd37da74Snw141292 #ifdef IDMAPD_DEBUG 315cd37da74Snw141292 (void) fprintf(stderr, "Schema init/upgrade SQL: %s\n", 316cd37da74Snw141292 sql[curr_version]); 317cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 318cd37da74Snw141292 rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname, 319cd37da74Snw141292 (curr_version == 0) ? "installing schema" : "upgrading schema"); 320cd37da74Snw141292 if (rc != 0) { 321cd37da74Snw141292 idmapdlog(prio, "Error %s schema for db %s", dbname, 322cd37da74Snw141292 (curr_version == 0) ? "installing schema" : 323cd37da74Snw141292 "upgrading schema"); 324cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 325cd37da74Snw141292 (void) unlink(dbname); 326cd37da74Snw141292 goto rinse_repeat; 327c5c4113dSnw141292 } 328c5c4113dSnw141292 329cd37da74Snw141292 *upgraded = (curr_version > 0); 330cd37da74Snw141292 *created = (curr_version == 0); 331cd37da74Snw141292 332cd37da74Snw141292 done: 333c5c4113dSnw141292 (void) sqlite_close(db); 334cd37da74Snw141292 return (0); 335c5c4113dSnw141292 } 336c5c4113dSnw141292 33784decf41Sjp151216 33884decf41Sjp151216 /* 33984decf41Sjp151216 * This is the SQLite database busy handler that retries the SQL 34084decf41Sjp151216 * operation until it is successful. 34184decf41Sjp151216 */ 34284decf41Sjp151216 int 34384decf41Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */ 34484decf41Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count) 34584decf41Sjp151216 { 34684decf41Sjp151216 struct idmap_busy *busy = arg; 34784decf41Sjp151216 int delay; 34884decf41Sjp151216 struct timespec rqtp; 34984decf41Sjp151216 35084decf41Sjp151216 if (count == 1) { 35184decf41Sjp151216 busy->total = 0; 35284decf41Sjp151216 busy->sec = 2; 35384decf41Sjp151216 } 35484decf41Sjp151216 if (busy->total > 1000 * busy->sec) { 3552b3ecdebSjp151216 idmapdlog(LOG_DEBUG, 35684decf41Sjp151216 "Thread %d waited %d sec for the %s database", 35784decf41Sjp151216 pthread_self(), busy->sec, busy->name); 35884decf41Sjp151216 busy->sec++; 35984decf41Sjp151216 } 36084decf41Sjp151216 36184decf41Sjp151216 if (count <= busy->delay_size) { 36284decf41Sjp151216 delay = busy->delays[count-1]; 36384decf41Sjp151216 } else { 36484decf41Sjp151216 delay = busy->delays[busy->delay_size - 1]; 36584decf41Sjp151216 } 36684decf41Sjp151216 busy->total += delay; 36784decf41Sjp151216 rqtp.tv_sec = 0; 36884decf41Sjp151216 rqtp.tv_nsec = delay * (NANOSEC / MILLISEC); 36984decf41Sjp151216 (void) nanosleep(&rqtp, NULL); 37084decf41Sjp151216 return (1); 37184decf41Sjp151216 } 37284decf41Sjp151216 37384decf41Sjp151216 374c5c4113dSnw141292 /* 375c5c4113dSnw141292 * Get the database handle 376c5c4113dSnw141292 */ 377c5c4113dSnw141292 idmap_retcode 378cd37da74Snw141292 get_db_handle(sqlite **db) 379cd37da74Snw141292 { 380c5c4113dSnw141292 char *errmsg; 38184decf41Sjp151216 idmap_tsd_t *tsd; 382c5c4113dSnw141292 383c5c4113dSnw141292 /* 38484decf41Sjp151216 * Retrieve the db handle from thread-specific storage 385c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 386c5c4113dSnw141292 */ 38784decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 38884decf41Sjp151216 idmapdlog(LOG_ERR, 389cd37da74Snw141292 "Error getting thread specific data for %s", IDMAP_DBNAME); 39084decf41Sjp151216 return (IDMAP_ERR_MEMORY); 39184decf41Sjp151216 } 392c5c4113dSnw141292 39384decf41Sjp151216 if (tsd->db_db == NULL) { 39484decf41Sjp151216 tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg); 39584decf41Sjp151216 if (tsd->db_db == NULL) { 396cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 397c5c4113dSnw141292 IDMAP_DBNAME, CHECK_NULL(errmsg)); 398c5c4113dSnw141292 sqlite_freemem(errmsg); 399cd37da74Snw141292 return (IDMAP_ERR_DB); 400c5c4113dSnw141292 } 401cd37da74Snw141292 40284decf41Sjp151216 tsd->db_busy.name = IDMAP_DBNAME; 40384decf41Sjp151216 tsd->db_busy.delays = db_delay_table; 40484decf41Sjp151216 tsd->db_busy.delay_size = sizeof (db_delay_table) / 40584decf41Sjp151216 sizeof (int); 40684decf41Sjp151216 sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler, 40784decf41Sjp151216 &tsd->db_busy); 40884decf41Sjp151216 } 40984decf41Sjp151216 *db = tsd->db_db; 410c5c4113dSnw141292 return (IDMAP_SUCCESS); 411c5c4113dSnw141292 } 412c5c4113dSnw141292 413c5c4113dSnw141292 /* 414c5c4113dSnw141292 * Get the cache handle 415c5c4113dSnw141292 */ 416c5c4113dSnw141292 idmap_retcode 417cd37da74Snw141292 get_cache_handle(sqlite **cache) 418cd37da74Snw141292 { 419c5c4113dSnw141292 char *errmsg; 42084decf41Sjp151216 idmap_tsd_t *tsd; 421c5c4113dSnw141292 422c5c4113dSnw141292 /* 42384decf41Sjp151216 * Retrieve the db handle from thread-specific storage 424c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 425c5c4113dSnw141292 */ 42684decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 427cd37da74Snw141292 idmapdlog(LOG_ERR, "Error getting thread specific data for %s", 42884decf41Sjp151216 IDMAP_DBNAME); 42984decf41Sjp151216 return (IDMAP_ERR_MEMORY); 43084decf41Sjp151216 } 431c5c4113dSnw141292 43284decf41Sjp151216 if (tsd->cache_db == NULL) { 43384decf41Sjp151216 tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg); 43484decf41Sjp151216 if (tsd->cache_db == NULL) { 435cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 436c5c4113dSnw141292 IDMAP_CACHENAME, CHECK_NULL(errmsg)); 437c5c4113dSnw141292 sqlite_freemem(errmsg); 438cd37da74Snw141292 return (IDMAP_ERR_DB); 439c5c4113dSnw141292 } 440cd37da74Snw141292 44184decf41Sjp151216 tsd->cache_busy.name = IDMAP_CACHENAME; 44284decf41Sjp151216 tsd->cache_busy.delays = cache_delay_table; 44384decf41Sjp151216 tsd->cache_busy.delay_size = sizeof (cache_delay_table) / 44484decf41Sjp151216 sizeof (int); 44584decf41Sjp151216 sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler, 44684decf41Sjp151216 &tsd->cache_busy); 44784decf41Sjp151216 } 44884decf41Sjp151216 *cache = tsd->cache_db; 449c5c4113dSnw141292 return (IDMAP_SUCCESS); 450c5c4113dSnw141292 } 451c5c4113dSnw141292 452c5c4113dSnw141292 /* 453c5c4113dSnw141292 * Initialize cache and db 454c5c4113dSnw141292 */ 455c5c4113dSnw141292 int 456cd37da74Snw141292 init_dbs() 457cd37da74Snw141292 { 45848258c6bSjp151216 char *sql[4]; 459cd37da74Snw141292 int created, upgraded; 460cd37da74Snw141292 461c5c4113dSnw141292 /* name-based mappings; probably OK to blow away in a pinch(?) */ 462cd37da74Snw141292 sql[0] = DB_INSTALL_SQL; 463cd37da74Snw141292 sql[1] = DB_UPGRADE_FROM_v1_SQL; 46448258c6bSjp151216 sql[2] = NULL; 465cd37da74Snw141292 466cd37da74Snw141292 if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql, 467cd37da74Snw141292 FAIL_IF_CORRUPT, &created, &upgraded) < 0) 468c5c4113dSnw141292 return (-1); 469c5c4113dSnw141292 470c5c4113dSnw141292 /* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */ 471cd37da74Snw141292 sql[0] = CACHE_INSTALL_SQL; 472cd37da74Snw141292 sql[1] = CACHE_UPGRADE_FROM_v1_SQL; 47348258c6bSjp151216 sql[2] = CACHE_UPGRADE_FROM_v2_SQL; 47448258c6bSjp151216 sql[3] = NULL; 47548258c6bSjp151216 476cd37da74Snw141292 if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL, 477cd37da74Snw141292 sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0) 478c5c4113dSnw141292 return (-1); 479c5c4113dSnw141292 480cd37da74Snw141292 _idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0; 481cd37da74Snw141292 482c5c4113dSnw141292 return (0); 483c5c4113dSnw141292 } 484c5c4113dSnw141292 485c5c4113dSnw141292 /* 486c5c4113dSnw141292 * Finalize databases 487c5c4113dSnw141292 */ 488c5c4113dSnw141292 void 489cd37da74Snw141292 fini_dbs() 490cd37da74Snw141292 { 491c5c4113dSnw141292 } 492c5c4113dSnw141292 493c5c4113dSnw141292 /* 494e8c27ec8Sbaban * This table is a listing of status codes that will be returned to the 495c5c4113dSnw141292 * client when a SQL command fails with the corresponding error message. 496c5c4113dSnw141292 */ 497c5c4113dSnw141292 static msg_table_t sqlmsgtable[] = { 49862c60062Sbaban {IDMAP_ERR_U2W_NAMERULE_CONFLICT, 499c5c4113dSnw141292 "columns unixname, is_user, u2w_order are not unique"}, 50062c60062Sbaban {IDMAP_ERR_W2U_NAMERULE_CONFLICT, 501cd37da74Snw141292 "columns winname, windomain, is_user, is_wuser, w2u_order are not" 502cd37da74Snw141292 " unique"}, 503cd37da74Snw141292 {IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"}, 504c5c4113dSnw141292 {-1, NULL} 505c5c4113dSnw141292 }; 506c5c4113dSnw141292 507c5c4113dSnw141292 /* 508c5c4113dSnw141292 * idmapd's version of string2stat to map SQLite messages to 509c5c4113dSnw141292 * status codes 510c5c4113dSnw141292 */ 511c5c4113dSnw141292 idmap_retcode 512cd37da74Snw141292 idmapd_string2stat(const char *msg) 513cd37da74Snw141292 { 514c5c4113dSnw141292 int i; 515c5c4113dSnw141292 for (i = 0; sqlmsgtable[i].msg; i++) { 516c5c4113dSnw141292 if (strcasecmp(sqlmsgtable[i].msg, msg) == 0) 517c5c4113dSnw141292 return (sqlmsgtable[i].retcode); 518c5c4113dSnw141292 } 519c5c4113dSnw141292 return (IDMAP_ERR_OTHER); 520c5c4113dSnw141292 } 521c5c4113dSnw141292 522c5c4113dSnw141292 /* 523cd37da74Snw141292 * Executes some SQL in a transaction. 524cd37da74Snw141292 * 525cd37da74Snw141292 * Returns 0 on success, -1 if it failed but the rollback succeeded, -2 526cd37da74Snw141292 * if the rollback failed. 527cd37da74Snw141292 */ 528cd37da74Snw141292 static 529cd37da74Snw141292 int 530cd37da74Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 531cd37da74Snw141292 const char *while_doing) 532cd37da74Snw141292 { 533cd37da74Snw141292 char *errmsg = NULL; 534cd37da74Snw141292 int rc; 535cd37da74Snw141292 536cd37da74Snw141292 rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg); 537cd37da74Snw141292 if (rc != SQLITE_OK) { 538cd37da74Snw141292 idmapdlog(LOG_ERR, "Begin transaction failed (%s) " 539cd37da74Snw141292 "while %s (%s)", errmsg, while_doing, dbname); 540cd37da74Snw141292 sqlite_freemem(errmsg); 541cd37da74Snw141292 return (-1); 542cd37da74Snw141292 } 543cd37da74Snw141292 544cd37da74Snw141292 rc = sqlite_exec(db, sql, NULL, NULL, &errmsg); 545cd37da74Snw141292 if (rc != SQLITE_OK) { 546cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg, 547cd37da74Snw141292 while_doing, dbname); 548cd37da74Snw141292 sqlite_freemem(errmsg); 549cd37da74Snw141292 errmsg = NULL; 550cd37da74Snw141292 goto rollback; 551cd37da74Snw141292 } 552cd37da74Snw141292 553cd37da74Snw141292 rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg); 554cd37da74Snw141292 if (rc == SQLITE_OK) { 555cd37da74Snw141292 sqlite_freemem(errmsg); 556cd37da74Snw141292 return (0); 557cd37da74Snw141292 } 558cd37da74Snw141292 559cd37da74Snw141292 idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)", 560cd37da74Snw141292 errmsg, while_doing, dbname); 561cd37da74Snw141292 sqlite_freemem(errmsg); 562cd37da74Snw141292 errmsg = NULL; 563cd37da74Snw141292 564cd37da74Snw141292 rollback: 565cd37da74Snw141292 rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg); 566cd37da74Snw141292 if (rc != SQLITE_OK) { 567cd37da74Snw141292 idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)", 568cd37da74Snw141292 errmsg, while_doing, dbname); 569cd37da74Snw141292 sqlite_freemem(errmsg); 570cd37da74Snw141292 return (-2); 571cd37da74Snw141292 } 572cd37da74Snw141292 sqlite_freemem(errmsg); 573cd37da74Snw141292 574cd37da74Snw141292 return (-1); 575cd37da74Snw141292 } 576cd37da74Snw141292 577cd37da74Snw141292 /* 578c5c4113dSnw141292 * Execute the given SQL statment without using any callbacks 579c5c4113dSnw141292 */ 580c5c4113dSnw141292 idmap_retcode 58171590c90Snw141292 sql_exec_no_cb(sqlite *db, const char *dbname, char *sql) 582cd37da74Snw141292 { 583c5c4113dSnw141292 char *errmsg = NULL; 58484decf41Sjp151216 int r; 585c5c4113dSnw141292 idmap_retcode retcode; 586c5c4113dSnw141292 587c5c4113dSnw141292 r = sqlite_exec(db, sql, NULL, NULL, &errmsg); 58884decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 589c5c4113dSnw141292 590c5c4113dSnw141292 if (r != SQLITE_OK) { 59171590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing %s " 59271590c90Snw141292 "(%s)", dbname, sql, CHECK_NULL(errmsg)); 593c5c4113dSnw141292 retcode = idmapd_string2stat(errmsg); 59462c60062Sbaban if (errmsg != NULL) 595c5c4113dSnw141292 sqlite_freemem(errmsg); 596c5c4113dSnw141292 return (retcode); 597c5c4113dSnw141292 } 598c5c4113dSnw141292 599c5c4113dSnw141292 return (IDMAP_SUCCESS); 600c5c4113dSnw141292 } 601c5c4113dSnw141292 602c5c4113dSnw141292 /* 603c5c4113dSnw141292 * Generate expression that can be used in WHERE statements. 604c5c4113dSnw141292 * Examples: 605c5c4113dSnw141292 * <prefix> <col> <op> <value> <suffix> 606c5c4113dSnw141292 * "" "unixuser" "=" "foo" "AND" 607c5c4113dSnw141292 */ 608c5c4113dSnw141292 idmap_retcode 609cd37da74Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out) 610cd37da74Snw141292 { 611cd37da74Snw141292 char *s_windomain = NULL, *s_winname = NULL; 612cd37da74Snw141292 char *s_unixname = NULL; 613cd37da74Snw141292 char *lower_winname; 614cd37da74Snw141292 int retcode = IDMAP_SUCCESS; 615cd37da74Snw141292 616c5c4113dSnw141292 if (out == NULL) 617c5c4113dSnw141292 return (IDMAP_ERR_ARG); 618c5c4113dSnw141292 619c5c4113dSnw141292 620cd37da74Snw141292 if (!EMPTY_STRING(rule->windomain)) { 621cd37da74Snw141292 s_windomain = sqlite_mprintf("AND windomain = %Q ", 622cd37da74Snw141292 rule->windomain); 623cd37da74Snw141292 if (s_windomain == NULL) { 624cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 625cd37da74Snw141292 goto out; 626c5c4113dSnw141292 } 627cd37da74Snw141292 } 628cd37da74Snw141292 629cd37da74Snw141292 if (!EMPTY_STRING(rule->winname)) { 630cd37da74Snw141292 if ((lower_winname = tolower_u8(rule->winname)) == NULL) 631cd37da74Snw141292 lower_winname = rule->winname; 632cd37da74Snw141292 s_winname = sqlite_mprintf( 633cd37da74Snw141292 "AND winname = %Q AND is_wuser = %d ", 634cd37da74Snw141292 lower_winname, rule->is_wuser ? 1 : 0); 635cd37da74Snw141292 if (lower_winname != rule->winname) 636cd37da74Snw141292 free(lower_winname); 637cd37da74Snw141292 if (s_winname == NULL) { 638cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 639cd37da74Snw141292 goto out; 640cd37da74Snw141292 } 641cd37da74Snw141292 } 642cd37da74Snw141292 643cd37da74Snw141292 if (!EMPTY_STRING(rule->unixname)) { 644cd37da74Snw141292 s_unixname = sqlite_mprintf( 645cd37da74Snw141292 "AND unixname = %Q AND is_user = %d ", 646cd37da74Snw141292 rule->unixname, rule->is_user ? 1 : 0); 647cd37da74Snw141292 if (s_unixname == NULL) { 648cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 649cd37da74Snw141292 goto out; 650cd37da74Snw141292 } 651cd37da74Snw141292 } 652cd37da74Snw141292 653cd37da74Snw141292 *out = sqlite_mprintf("%s %s %s", 654cd37da74Snw141292 s_windomain ? s_windomain : "", 655cd37da74Snw141292 s_winname ? s_winname : "", 656cd37da74Snw141292 s_unixname ? s_unixname : ""); 657cd37da74Snw141292 658cd37da74Snw141292 if (*out == NULL) { 659cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 660cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 661cd37da74Snw141292 goto out; 662cd37da74Snw141292 } 663cd37da74Snw141292 664cd37da74Snw141292 out: 665cd37da74Snw141292 if (s_windomain != NULL) 666cd37da74Snw141292 sqlite_freemem(s_windomain); 667cd37da74Snw141292 if (s_winname != NULL) 668cd37da74Snw141292 sqlite_freemem(s_winname); 669cd37da74Snw141292 if (s_unixname != NULL) 670cd37da74Snw141292 sqlite_freemem(s_unixname); 671cd37da74Snw141292 672cd37da74Snw141292 return (retcode); 673cd37da74Snw141292 } 674cd37da74Snw141292 675cd37da74Snw141292 676c5c4113dSnw141292 677c5c4113dSnw141292 /* 678c5c4113dSnw141292 * Generate and execute SQL statement for LIST RPC calls 679c5c4113dSnw141292 */ 680c5c4113dSnw141292 idmap_retcode 68171590c90Snw141292 process_list_svc_sql(sqlite *db, const char *dbname, char *sql, uint64_t limit, 68248258c6bSjp151216 int flag, list_svc_cb cb, void *result) 683cd37da74Snw141292 { 684c5c4113dSnw141292 list_cb_data_t cb_data; 685c5c4113dSnw141292 char *errmsg = NULL; 68684decf41Sjp151216 int r; 687c5c4113dSnw141292 idmap_retcode retcode = IDMAP_ERR_INTERNAL; 688c5c4113dSnw141292 689c5c4113dSnw141292 (void) memset(&cb_data, 0, sizeof (cb_data)); 690c5c4113dSnw141292 cb_data.result = result; 691c5c4113dSnw141292 cb_data.limit = limit; 69248258c6bSjp151216 cb_data.flag = flag; 693c5c4113dSnw141292 69484decf41Sjp151216 695c5c4113dSnw141292 r = sqlite_exec(db, sql, cb, &cb_data, &errmsg); 69684decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 697c5c4113dSnw141292 switch (r) { 698c5c4113dSnw141292 case SQLITE_OK: 699c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 70084decf41Sjp151216 break; 70184decf41Sjp151216 702c5c4113dSnw141292 default: 703c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 70471590c90Snw141292 idmapdlog(LOG_ERR, "Database error on %s while executing " 70571590c90Snw141292 "%s (%s)", dbname, sql, CHECK_NULL(errmsg)); 70684decf41Sjp151216 break; 707c5c4113dSnw141292 } 70862c60062Sbaban if (errmsg != NULL) 709c5c4113dSnw141292 sqlite_freemem(errmsg); 710c5c4113dSnw141292 return (retcode); 711c5c4113dSnw141292 } 712c5c4113dSnw141292 713c5c4113dSnw141292 /* 714c5c4113dSnw141292 * This routine is called by callbacks that process the results of 715c5c4113dSnw141292 * LIST RPC calls to validate data and to allocate memory for 716c5c4113dSnw141292 * the result array. 717c5c4113dSnw141292 */ 718c5c4113dSnw141292 idmap_retcode 719c5c4113dSnw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv, 720cd37da74Snw141292 int ncol, uchar_t **list, size_t valsize) 721cd37da74Snw141292 { 722c5c4113dSnw141292 size_t nsize; 723c5c4113dSnw141292 void *tmplist; 724c5c4113dSnw141292 725c5c4113dSnw141292 if (cb_data->limit > 0 && cb_data->next == cb_data->limit) 726c5c4113dSnw141292 return (IDMAP_NEXT); 727c5c4113dSnw141292 728c5c4113dSnw141292 if (argc < ncol || argv == NULL) { 729c5c4113dSnw141292 idmapdlog(LOG_ERR, "Invalid data"); 730c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 731c5c4113dSnw141292 } 732c5c4113dSnw141292 733c5c4113dSnw141292 /* alloc in bulk to reduce number of reallocs */ 734c5c4113dSnw141292 if (cb_data->next >= cb_data->len) { 735c5c4113dSnw141292 nsize = (cb_data->len + SIZE_INCR) * valsize; 736c5c4113dSnw141292 tmplist = realloc(*list, nsize); 737c5c4113dSnw141292 if (tmplist == NULL) { 738c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 739c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 740c5c4113dSnw141292 } 741c5c4113dSnw141292 *list = tmplist; 742c5c4113dSnw141292 (void) memset(*list + (cb_data->len * valsize), 0, 743c5c4113dSnw141292 SIZE_INCR * valsize); 744c5c4113dSnw141292 cb_data->len += SIZE_INCR; 745c5c4113dSnw141292 } 746c5c4113dSnw141292 return (IDMAP_SUCCESS); 747c5c4113dSnw141292 } 748c5c4113dSnw141292 749cd37da74Snw141292 static 750cd37da74Snw141292 idmap_retcode 751c5c4113dSnw141292 get_namerule_order(char *winname, char *windomain, char *unixname, 752cd37da74Snw141292 int direction, int is_diagonal, int *w2u_order, int *u2w_order) 753cd37da74Snw141292 { 754c5c4113dSnw141292 *w2u_order = 0; 755c5c4113dSnw141292 *u2w_order = 0; 756c5c4113dSnw141292 757c5c4113dSnw141292 /* 758c5c4113dSnw141292 * Windows to UNIX lookup order: 759c5c4113dSnw141292 * 1. winname@domain (or winname) to "" 760c5c4113dSnw141292 * 2. winname@domain (or winname) to unixname 761c5c4113dSnw141292 * 3. winname@* to "" 762c5c4113dSnw141292 * 4. winname@* to unixname 763c5c4113dSnw141292 * 5. *@domain (or *) to * 764c5c4113dSnw141292 * 6. *@domain (or *) to "" 765c5c4113dSnw141292 * 7. *@domain (or *) to unixname 766c5c4113dSnw141292 * 8. *@* to * 767c5c4113dSnw141292 * 9. *@* to "" 768c5c4113dSnw141292 * 10. *@* to unixname 769c5c4113dSnw141292 * 770c5c4113dSnw141292 * winname is a special case of winname@domain when domain is the 771c5c4113dSnw141292 * default domain. Similarly * is a special case of *@domain when 772c5c4113dSnw141292 * domain is the default domain. 773c5c4113dSnw141292 * 774c5c4113dSnw141292 * Note that "" has priority over specific names because "" inhibits 775c5c4113dSnw141292 * mappings and traditionally deny rules always had higher priority. 776c5c4113dSnw141292 */ 777651c0131Sbaban if (direction != IDMAP_DIRECTION_U2W) { 778651c0131Sbaban /* bi-directional or from windows to unix */ 779c5c4113dSnw141292 if (winname == NULL) 780c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 781c5c4113dSnw141292 else if (unixname == NULL) 782c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 783c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 784c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 785c5c4113dSnw141292 else if (*winname == '*' && windomain && *windomain == '*') { 786c5c4113dSnw141292 if (*unixname == '*') 787c5c4113dSnw141292 *w2u_order = 8; 788c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 789c5c4113dSnw141292 *w2u_order = 9; 790c5c4113dSnw141292 else /* unixname == name */ 791c5c4113dSnw141292 *w2u_order = 10; 792c5c4113dSnw141292 } else if (*winname == '*') { 793c5c4113dSnw141292 if (*unixname == '*') 794c5c4113dSnw141292 *w2u_order = 5; 795c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 796c5c4113dSnw141292 *w2u_order = 6; 797c5c4113dSnw141292 else /* name */ 798c5c4113dSnw141292 *w2u_order = 7; 79962c60062Sbaban } else if (windomain != NULL && *windomain == '*') { 800c5c4113dSnw141292 /* winname == name */ 801c5c4113dSnw141292 if (*unixname == '*') 802c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 803c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 804c5c4113dSnw141292 *w2u_order = 3; 805c5c4113dSnw141292 else /* name */ 806c5c4113dSnw141292 *w2u_order = 4; 807c5c4113dSnw141292 } else { 808c5c4113dSnw141292 /* winname == name && windomain == null or name */ 809c5c4113dSnw141292 if (*unixname == '*') 810c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 811c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 812c5c4113dSnw141292 *w2u_order = 1; 813c5c4113dSnw141292 else /* name */ 814c5c4113dSnw141292 *w2u_order = 2; 815c5c4113dSnw141292 } 816cd37da74Snw141292 817c5c4113dSnw141292 } 818c5c4113dSnw141292 819c5c4113dSnw141292 /* 820cd37da74Snw141292 * 1. unixname to "", non-diagonal 821cd37da74Snw141292 * 2. unixname to winname@domain (or winname), non-diagonal 822cd37da74Snw141292 * 3. unixname to "", diagonal 823cd37da74Snw141292 * 4. unixname to winname@domain (or winname), diagonal 824cd37da74Snw141292 * 5. * to *@domain (or *), non-diagonal 825cd37da74Snw141292 * 5. * to *@domain (or *), diagonal 826cd37da74Snw141292 * 7. * to "" 827cd37da74Snw141292 * 8. * to winname@domain (or winname) 828cd37da74Snw141292 * 9. * to "", non-diagonal 829cd37da74Snw141292 * 10. * to winname@domain (or winname), diagonal 830c5c4113dSnw141292 */ 831651c0131Sbaban if (direction != IDMAP_DIRECTION_W2U) { 832cd37da74Snw141292 int diagonal = is_diagonal ? 1 : 0; 833cd37da74Snw141292 834651c0131Sbaban /* bi-directional or from unix to windows */ 835c5c4113dSnw141292 if (unixname == NULL || EMPTY_NAME(unixname)) 836c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 837c5c4113dSnw141292 else if (winname == NULL) 838c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 83962c60062Sbaban else if (windomain != NULL && *windomain == '*') 840651c0131Sbaban return (IDMAP_ERR_U2W_NAMERULE); 841c5c4113dSnw141292 else if (*unixname == '*') { 842c5c4113dSnw141292 if (*winname == '*') 843cd37da74Snw141292 *u2w_order = 5 + diagonal; 844c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 845cd37da74Snw141292 *u2w_order = 7 + 2 * diagonal; 846c5c4113dSnw141292 else 847cd37da74Snw141292 *u2w_order = 8 + 2 * diagonal; 848c5c4113dSnw141292 } else { 849c5c4113dSnw141292 if (*winname == '*') 850c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 851c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 852cd37da74Snw141292 *u2w_order = 1 + 2 * diagonal; 853c5c4113dSnw141292 else 854cd37da74Snw141292 *u2w_order = 2 + 2 * diagonal; 855c5c4113dSnw141292 } 856c5c4113dSnw141292 } 857c5c4113dSnw141292 return (IDMAP_SUCCESS); 858c5c4113dSnw141292 } 859c5c4113dSnw141292 860c5c4113dSnw141292 /* 861c5c4113dSnw141292 * Generate and execute SQL statement to add name-based mapping rule 862c5c4113dSnw141292 */ 863c5c4113dSnw141292 idmap_retcode 864cd37da74Snw141292 add_namerule(sqlite *db, idmap_namerule *rule) 865cd37da74Snw141292 { 866c5c4113dSnw141292 char *sql = NULL; 867c5c4113dSnw141292 idmap_stat retcode; 8688e228215Sdm199847 char *dom = NULL; 869c5c4113dSnw141292 int w2u_order, u2w_order; 870c5c4113dSnw141292 char w2ubuf[11], u2wbuf[11]; 871c5c4113dSnw141292 8728e228215Sdm199847 retcode = get_namerule_order(rule->winname, rule->windomain, 873cd37da74Snw141292 rule->unixname, rule->direction, 874cd37da74Snw141292 rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order); 875c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 876c5c4113dSnw141292 goto out; 877c5c4113dSnw141292 878c5c4113dSnw141292 if (w2u_order) 879c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order); 880c5c4113dSnw141292 if (u2w_order) 881c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order); 882c5c4113dSnw141292 88362c60062Sbaban /* 88462c60062Sbaban * For the triggers on namerules table to work correctly: 88562c60062Sbaban * 1) Use NULL instead of 0 for w2u_order and u2w_order 88662c60062Sbaban * 2) Use "" instead of NULL for "no domain" 88762c60062Sbaban */ 88862c60062Sbaban 889e8c27ec8Sbaban if (!EMPTY_STRING(rule->windomain)) 8908e228215Sdm199847 dom = rule->windomain; 891cd37da74Snw141292 else if (lookup_wksids_name2sid(rule->winname, NULL, NULL, NULL, NULL) 89262c60062Sbaban == IDMAP_SUCCESS) { 89362c60062Sbaban /* well-known SIDs don't need domain */ 89462c60062Sbaban dom = ""; 89562c60062Sbaban } 896c5c4113dSnw141292 897c5c4113dSnw141292 RDLOCK_CONFIG(); 89862c60062Sbaban if (dom == NULL) { 899c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain) 900c8e26105Sjp151216 dom = _idmapdstate.cfg->pgcfg.default_domain; 901c5c4113dSnw141292 else 902c5c4113dSnw141292 dom = ""; 90362c60062Sbaban } 90484decf41Sjp151216 sql = sqlite_mprintf("INSERT into namerules " 905cd37da74Snw141292 "(is_user, is_wuser, windomain, winname_display, is_nt4, " 906c5c4113dSnw141292 "unixname, w2u_order, u2w_order) " 907cd37da74Snw141292 "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);", 908cd37da74Snw141292 rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom, 909cd37da74Snw141292 rule->winname, rule->is_nt4 ? 1 : 0, rule->unixname, 910cd37da74Snw141292 w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL); 911c5c4113dSnw141292 UNLOCK_CONFIG(); 912c5c4113dSnw141292 913c5c4113dSnw141292 if (sql == NULL) { 914c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 915c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 916c5c4113dSnw141292 goto out; 917c5c4113dSnw141292 } 918c5c4113dSnw141292 91971590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 920c5c4113dSnw141292 921c5c4113dSnw141292 if (retcode == IDMAP_ERR_OTHER) 922c5c4113dSnw141292 retcode = IDMAP_ERR_CFG; 923c5c4113dSnw141292 924c5c4113dSnw141292 out: 92562c60062Sbaban if (sql != NULL) 926c5c4113dSnw141292 sqlite_freemem(sql); 927c5c4113dSnw141292 return (retcode); 928c5c4113dSnw141292 } 929c5c4113dSnw141292 930c5c4113dSnw141292 /* 931c5c4113dSnw141292 * Flush name-based mapping rules 932c5c4113dSnw141292 */ 933c5c4113dSnw141292 idmap_retcode 934cd37da74Snw141292 flush_namerules(sqlite *db) 935cd37da74Snw141292 { 936c5c4113dSnw141292 idmap_stat retcode; 937c5c4113dSnw141292 93871590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "DELETE FROM namerules;"); 939c5c4113dSnw141292 940c5c4113dSnw141292 return (retcode); 941c5c4113dSnw141292 } 942c5c4113dSnw141292 943c5c4113dSnw141292 /* 944c5c4113dSnw141292 * Generate and execute SQL statement to remove a name-based mapping rule 945c5c4113dSnw141292 */ 946c5c4113dSnw141292 idmap_retcode 947cd37da74Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule) 948cd37da74Snw141292 { 949c5c4113dSnw141292 char *sql = NULL; 950c5c4113dSnw141292 idmap_stat retcode; 951c5c4113dSnw141292 char buf[80]; 952cd37da74Snw141292 char *expr = NULL; 953c5c4113dSnw141292 9548e228215Sdm199847 if (rule->direction < 0 && EMPTY_STRING(rule->windomain) && 9558e228215Sdm199847 EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname)) 956c5c4113dSnw141292 return (IDMAP_SUCCESS); 957c5c4113dSnw141292 958c5c4113dSnw141292 buf[0] = 0; 959cd37da74Snw141292 960cd37da74Snw141292 if (rule->direction == IDMAP_DIRECTION_BI) 961c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 962c5c4113dSnw141292 " AND u2w_order > 0"); 963cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_W2U) 964c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 965c5c4113dSnw141292 " AND (u2w_order = 0 OR u2w_order ISNULL)"); 966cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_U2W) 967c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND u2w_order > 0" 968c5c4113dSnw141292 " AND (w2u_order = 0 OR w2u_order ISNULL)"); 969c5c4113dSnw141292 970cd37da74Snw141292 retcode = gen_sql_expr_from_rule(rule, &expr); 971cd37da74Snw141292 if (retcode != IDMAP_SUCCESS) 972c5c4113dSnw141292 goto out; 973c5c4113dSnw141292 974cd37da74Snw141292 sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s %s;", expr, 975c5c4113dSnw141292 buf); 976c5c4113dSnw141292 977c5c4113dSnw141292 if (sql == NULL) { 978c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 979c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 980c5c4113dSnw141292 goto out; 981c5c4113dSnw141292 } 982c5c4113dSnw141292 983cd37da74Snw141292 98471590c90Snw141292 retcode = sql_exec_no_cb(db, IDMAP_DBNAME, sql); 985c5c4113dSnw141292 986c5c4113dSnw141292 out: 987cd37da74Snw141292 if (expr != NULL) 988cd37da74Snw141292 sqlite_freemem(expr); 98962c60062Sbaban if (sql != NULL) 990c5c4113dSnw141292 sqlite_freemem(sql); 991c5c4113dSnw141292 return (retcode); 992c5c4113dSnw141292 } 993c5c4113dSnw141292 994c5c4113dSnw141292 /* 995c5c4113dSnw141292 * Compile the given SQL query and step just once. 996c5c4113dSnw141292 * 997c5c4113dSnw141292 * Input: 998c5c4113dSnw141292 * db - db handle 999c5c4113dSnw141292 * sql - SQL statement 1000c5c4113dSnw141292 * 1001c5c4113dSnw141292 * Output: 1002c5c4113dSnw141292 * vm - virtual SQL machine 1003c5c4113dSnw141292 * ncol - number of columns in the result 1004c5c4113dSnw141292 * values - column values 1005c5c4113dSnw141292 * 1006c5c4113dSnw141292 * Return values: 1007c5c4113dSnw141292 * IDMAP_SUCCESS 1008c5c4113dSnw141292 * IDMAP_ERR_NOTFOUND 1009c5c4113dSnw141292 * IDMAP_ERR_INTERNAL 1010c5c4113dSnw141292 */ 1011c5c4113dSnw141292 1012cd37da74Snw141292 static 1013cd37da74Snw141292 idmap_retcode 1014c5c4113dSnw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol, 1015cd37da74Snw141292 int reqcol, const char ***values) 1016cd37da74Snw141292 { 1017c5c4113dSnw141292 char *errmsg = NULL; 101884decf41Sjp151216 int r; 1019c5c4113dSnw141292 102084decf41Sjp151216 if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) { 1021cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1022cd37da74Snw141292 CHECK_NULL(errmsg)); 1023c5c4113dSnw141292 sqlite_freemem(errmsg); 1024c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1025c5c4113dSnw141292 } 1026c5c4113dSnw141292 1027c5c4113dSnw141292 r = sqlite_step(*vm, ncol, values, NULL); 102884decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 1029c5c4113dSnw141292 103084decf41Sjp151216 if (r == SQLITE_ROW) { 103162c60062Sbaban if (ncol != NULL && *ncol < reqcol) { 1032c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1033c5c4113dSnw141292 *vm = NULL; 1034c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1035c5c4113dSnw141292 } 1036c5c4113dSnw141292 /* Caller will call finalize after using the results */ 1037c5c4113dSnw141292 return (IDMAP_SUCCESS); 1038c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 1039c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1040c5c4113dSnw141292 *vm = NULL; 1041c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1042c5c4113dSnw141292 } 1043c5c4113dSnw141292 1044c5c4113dSnw141292 (void) sqlite_finalize(*vm, &errmsg); 1045c5c4113dSnw141292 *vm = NULL; 1046cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1047cd37da74Snw141292 CHECK_NULL(errmsg)); 1048c5c4113dSnw141292 sqlite_freemem(errmsg); 1049c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1050c5c4113dSnw141292 } 1051c5c4113dSnw141292 105262c60062Sbaban /* 1053479ac375Sdm199847 * Load config in the state. 1054e8c27ec8Sbaban * 1055479ac375Sdm199847 * nm_siduid and nm_sidgid fields: 1056e8c27ec8Sbaban * state->nm_siduid represents mode used by sid2uid and uid2sid 1057e8c27ec8Sbaban * requests for directory-based name mappings. Similarly, 1058e8c27ec8Sbaban * state->nm_sidgid represents mode used by sid2gid and gid2sid 1059e8c27ec8Sbaban * requests. 1060e8c27ec8Sbaban * 1061e8c27ec8Sbaban * sid2uid/uid2sid: 1062e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1063e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixuser_attr 1064e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr 1065e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixuser_attr 1066e8c27ec8Sbaban * 1067e8c27ec8Sbaban * sid2gid/gid2sid: 1068e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1069e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixgroup_attr 1070e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr 1071e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr 1072e8c27ec8Sbaban */ 1073e8c27ec8Sbaban idmap_retcode 1074479ac375Sdm199847 load_cfg_in_state(lookup_state_t *state) 1075e8c27ec8Sbaban { 1076e8c27ec8Sbaban state->nm_siduid = IDMAP_NM_NONE; 1077e8c27ec8Sbaban state->nm_sidgid = IDMAP_NM_NONE; 1078e8c27ec8Sbaban RDLOCK_CONFIG(); 1079479ac375Sdm199847 1080*4aa0a5e7Snw141292 state->eph_map_unres_sids = 0; 1081*4aa0a5e7Snw141292 if (_idmapdstate.cfg->pgcfg.eph_map_unres_sids) 1082*4aa0a5e7Snw141292 state->eph_map_unres_sids = 1; 1083*4aa0a5e7Snw141292 1084479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 1085479ac375Sdm199847 state->defdom = 1086479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.default_domain); 1087479ac375Sdm199847 if (state->defdom == NULL) { 1088479ac375Sdm199847 UNLOCK_CONFIG(); 1089479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1090479ac375Sdm199847 } 1091479ac375Sdm199847 } else { 1092479ac375Sdm199847 UNLOCK_CONFIG(); 1093dc03a638Sdm199847 return (IDMAP_SUCCESS); 1094479ac375Sdm199847 } 1095e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ds_name_mapping_enabled == FALSE) { 1096e8c27ec8Sbaban UNLOCK_CONFIG(); 1097e8c27ec8Sbaban return (IDMAP_SUCCESS); 1098e8c27ec8Sbaban } 1099e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1100e8c27ec8Sbaban state->nm_siduid = 1101e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1102e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1103e8c27ec8Sbaban state->nm_sidgid = 1104e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1105e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1106e8c27ec8Sbaban } else { 1107e8c27ec8Sbaban state->nm_siduid = 1108e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1109e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1110e8c27ec8Sbaban state->nm_sidgid = 1111e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1112e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1113e8c27ec8Sbaban } 1114e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) { 1115e8c27ec8Sbaban state->ad_unixuser_attr = 1116e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr); 1117e8c27ec8Sbaban if (state->ad_unixuser_attr == NULL) { 1118e8c27ec8Sbaban UNLOCK_CONFIG(); 1119e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1120e8c27ec8Sbaban } 1121e8c27ec8Sbaban } 1122e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) { 1123e8c27ec8Sbaban state->ad_unixgroup_attr = 1124e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr); 1125e8c27ec8Sbaban if (state->ad_unixgroup_attr == NULL) { 1126e8c27ec8Sbaban UNLOCK_CONFIG(); 1127e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1128e8c27ec8Sbaban } 1129e8c27ec8Sbaban } 1130479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1131479ac375Sdm199847 state->nldap_winname_attr = 1132479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr); 1133479ac375Sdm199847 if (state->nldap_winname_attr == NULL) { 1134479ac375Sdm199847 UNLOCK_CONFIG(); 1135479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1136479ac375Sdm199847 } 1137479ac375Sdm199847 } 1138e8c27ec8Sbaban UNLOCK_CONFIG(); 1139e8c27ec8Sbaban return (IDMAP_SUCCESS); 1140e8c27ec8Sbaban } 1141e8c27ec8Sbaban 1142e8c27ec8Sbaban /* 114348258c6bSjp151216 * Set the rule with sepecified values. 114448258c6bSjp151216 * All the strings are copied. 114548258c6bSjp151216 */ 114648258c6bSjp151216 static void 114748258c6bSjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain, 114848258c6bSjp151216 const char *winname, const char *unixname, boolean_t is_user, 114948258c6bSjp151216 boolean_t is_wuser, boolean_t is_nt4, int direction) 115048258c6bSjp151216 { 115148258c6bSjp151216 /* 115248258c6bSjp151216 * Only update if they differ because we have to free 115348258c6bSjp151216 * and duplicate the strings 115448258c6bSjp151216 */ 115548258c6bSjp151216 if (rule->windomain == NULL || windomain == NULL || 115648258c6bSjp151216 strcmp(rule->windomain, windomain) != 0) { 115748258c6bSjp151216 if (rule->windomain != NULL) { 115848258c6bSjp151216 free(rule->windomain); 115948258c6bSjp151216 rule->windomain = NULL; 116048258c6bSjp151216 } 116148258c6bSjp151216 if (windomain != NULL) 116248258c6bSjp151216 rule->windomain = strdup(windomain); 116348258c6bSjp151216 } 116448258c6bSjp151216 116548258c6bSjp151216 if (rule->winname == NULL || winname == NULL || 116648258c6bSjp151216 strcmp(rule->winname, winname) != 0) { 116748258c6bSjp151216 if (rule->winname != NULL) { 116848258c6bSjp151216 free(rule->winname); 116948258c6bSjp151216 rule->winname = NULL; 117048258c6bSjp151216 } 117148258c6bSjp151216 if (winname != NULL) 117248258c6bSjp151216 rule->winname = strdup(winname); 117348258c6bSjp151216 } 117448258c6bSjp151216 117548258c6bSjp151216 if (rule->unixname == NULL || unixname == NULL || 117648258c6bSjp151216 strcmp(rule->unixname, unixname) != 0) { 117748258c6bSjp151216 if (rule->unixname != NULL) { 117848258c6bSjp151216 free(rule->unixname); 117948258c6bSjp151216 rule->unixname = NULL; 118048258c6bSjp151216 } 118148258c6bSjp151216 if (unixname != NULL) 118248258c6bSjp151216 rule->unixname = strdup(unixname); 118348258c6bSjp151216 } 118448258c6bSjp151216 118548258c6bSjp151216 rule->is_user = is_user; 118648258c6bSjp151216 rule->is_wuser = is_wuser; 118748258c6bSjp151216 rule->is_nt4 = is_nt4; 118848258c6bSjp151216 rule->direction = direction; 118948258c6bSjp151216 } 119048258c6bSjp151216 119148258c6bSjp151216 119248258c6bSjp151216 /* 119362c60062Sbaban * Table for well-known SIDs. 119462c60062Sbaban * 119562c60062Sbaban * Background: 119662c60062Sbaban * 119776b27f93Sbaban * Some of the well-known principals are stored under: 119862c60062Sbaban * cn=WellKnown Security Principals, cn=Configuration, dc=<forestRootDomain> 119962c60062Sbaban * They belong to objectClass "foreignSecurityPrincipal". They don't have 120062c60062Sbaban * "samAccountName" nor "userPrincipalName" attributes. Their names are 120162c60062Sbaban * available in "cn" and "name" attributes. Some of these principals have a 120262c60062Sbaban * second entry under CN=ForeignSecurityPrincipals,dc=<forestRootDomain> and 120362c60062Sbaban * these duplicate entries have the stringified SID in the "name" and "cn" 120462c60062Sbaban * attributes instead of the actual name. 120562c60062Sbaban * 120676b27f93Sbaban * Those of the form S-1-5-32-X are Builtin groups and are stored in the 120776b27f93Sbaban * cn=builtin container (except, Power Users which is not stored in AD) 120862c60062Sbaban * 120976b27f93Sbaban * These principals are and will remain constant. Therefore doing AD lookups 121076b27f93Sbaban * provides no benefit. Also, using hard-coded table (and thus avoiding AD 121176b27f93Sbaban * lookup) improves performance and avoids additional complexity in the 121276b27f93Sbaban * adutils.c code. Moreover these SIDs can be used when no Active Directory 121376b27f93Sbaban * is available (such as the CIFS server's "workgroup" mode). 121476b27f93Sbaban * 121576b27f93Sbaban * Notes: 121676b27f93Sbaban * 1. Currently we don't support localization of well-known SID names, 121762c60062Sbaban * unlike Windows. 121862c60062Sbaban * 121976b27f93Sbaban * 2. Other well-known SIDs i.e. S-1-5-<domain>-<w-k RID> are not stored 122076b27f93Sbaban * here. AD does have normal user/group objects for these objects and 122176b27f93Sbaban * can be looked up using the existing AD lookup code. 1222e8c27ec8Sbaban * 1223e8c27ec8Sbaban * 3. See comments above lookup_wksids_sid2pid() for more information 1224e8c27ec8Sbaban * on how we lookup the wksids table. 122562c60062Sbaban */ 122662c60062Sbaban static wksids_table_t wksids[] = { 1227e8c27ec8Sbaban {"S-1-0", 0, "Nobody", 0, SENTINEL_PID, -1, 1}, 1228e8c27ec8Sbaban {"S-1-1", 0, "Everyone", 0, SENTINEL_PID, -1, -1}, 1229e8c27ec8Sbaban {"S-1-3", 0, "Creator Owner", 1, IDMAP_WK_CREATOR_OWNER_UID, 1, 0}, 1230e8c27ec8Sbaban {"S-1-3", 1, "Creator Group", 0, IDMAP_WK_CREATOR_GROUP_GID, 0, 0}, 1231e8c27ec8Sbaban {"S-1-3", 2, "Creator Owner Server", 1, SENTINEL_PID, -1, -1}, 1232e8c27ec8Sbaban {"S-1-3", 3, "Creator Group Server", 0, SENTINEL_PID, -1, 1}, 1233e8c27ec8Sbaban {"S-1-3", 4, "Owner Rights", 0, SENTINEL_PID, -1, -1}, 1234e8c27ec8Sbaban {"S-1-5", 1, "Dialup", 0, SENTINEL_PID, -1, -1}, 1235e8c27ec8Sbaban {"S-1-5", 2, "Network", 0, SENTINEL_PID, -1, -1}, 1236e8c27ec8Sbaban {"S-1-5", 3, "Batch", 0, SENTINEL_PID, -1, -1}, 1237e8c27ec8Sbaban {"S-1-5", 4, "Interactive", 0, SENTINEL_PID, -1, -1}, 1238e8c27ec8Sbaban {"S-1-5", 6, "Service", 0, SENTINEL_PID, -1, -1}, 1239e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, GID_NOBODY, 0, 0}, 1240e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, UID_NOBODY, 1, 0}, 1241e8c27ec8Sbaban {"S-1-5", 8, "Proxy", 0, SENTINEL_PID, -1, -1}, 1242e8c27ec8Sbaban {"S-1-5", 9, "Enterprise Domain Controllers", 0, SENTINEL_PID, -1, -1}, 1243e8c27ec8Sbaban {"S-1-5", 10, "Self", 0, SENTINEL_PID, -1, -1}, 1244e8c27ec8Sbaban {"S-1-5", 11, "Authenticated Users", 0, SENTINEL_PID, -1, -1}, 1245e8c27ec8Sbaban {"S-1-5", 12, "Restricted Code", 0, SENTINEL_PID, -1, -1}, 1246e8c27ec8Sbaban {"S-1-5", 13, "Terminal Server User", 0, SENTINEL_PID, -1, -1}, 1247e8c27ec8Sbaban {"S-1-5", 14, "Remote Interactive Logon", 0, SENTINEL_PID, -1, -1}, 1248e8c27ec8Sbaban {"S-1-5", 15, "This Organization", 0, SENTINEL_PID, -1, -1}, 1249e8c27ec8Sbaban {"S-1-5", 17, "IUSR", 0, SENTINEL_PID, -1, -1}, 1250e8c27ec8Sbaban {"S-1-5", 18, "Local System", 0, IDMAP_WK_LOCAL_SYSTEM_GID, 0, 0}, 1251e8c27ec8Sbaban {"S-1-5", 19, "Local Service", 0, SENTINEL_PID, -1, -1}, 1252e8c27ec8Sbaban {"S-1-5", 20, "Network Service", 0, SENTINEL_PID, -1, -1}, 1253e8c27ec8Sbaban {"S-1-5", 1000, "Other Organization", 0, SENTINEL_PID, -1, -1}, 1254e8c27ec8Sbaban {"S-1-5-32", 544, "Administrators", 0, SENTINEL_PID, -1, -1}, 1255e8c27ec8Sbaban {"S-1-5-32", 545, "Users", 0, SENTINEL_PID, -1, -1}, 1256e8c27ec8Sbaban {"S-1-5-32", 546, "Guests", 0, SENTINEL_PID, -1, -1}, 1257e8c27ec8Sbaban {"S-1-5-32", 547, "Power Users", 0, SENTINEL_PID, -1, -1}, 1258e8c27ec8Sbaban {"S-1-5-32", 548, "Account Operators", 0, SENTINEL_PID, -1, -1}, 1259e8c27ec8Sbaban {"S-1-5-32", 549, "Server Operators", 0, SENTINEL_PID, -1, -1}, 1260e8c27ec8Sbaban {"S-1-5-32", 550, "Print Operators", 0, SENTINEL_PID, -1, -1}, 1261e8c27ec8Sbaban {"S-1-5-32", 551, "Backup Operators", 0, SENTINEL_PID, -1, -1}, 1262e8c27ec8Sbaban {"S-1-5-32", 552, "Replicator", 0, SENTINEL_PID, -1, -1}, 126376b27f93Sbaban {"S-1-5-32", 554, "Pre-Windows 2000 Compatible Access", 0, 1264e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1265e8c27ec8Sbaban {"S-1-5-32", 555, "Remote Desktop Users", 0, SENTINEL_PID, -1, -1}, 126676b27f93Sbaban {"S-1-5-32", 556, "Network Configuration Operators", 0, 1267e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 126876b27f93Sbaban {"S-1-5-32", 557, "Incoming Forest Trust Builders", 0, 1269e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1270e8c27ec8Sbaban {"S-1-5-32", 558, "Performance Monitor Users", 0, SENTINEL_PID, -1, -1}, 1271e8c27ec8Sbaban {"S-1-5-32", 559, "Performance Log Users", 0, SENTINEL_PID, -1, -1}, 127276b27f93Sbaban {"S-1-5-32", 560, "Windows Authorization Access Group", 0, 1273e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 127476b27f93Sbaban {"S-1-5-32", 561, "Terminal Server License Servers", 0, 1275e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1276e8c27ec8Sbaban {"S-1-5-32", 561, "Distributed COM Users", 0, SENTINEL_PID, -1, -1}, 1277e8c27ec8Sbaban {"S-1-5-32", 568, "IIS_IUSRS", 0, SENTINEL_PID, -1, -1}, 1278e8c27ec8Sbaban {"S-1-5-32", 569, "Cryptographic Operators", 0, SENTINEL_PID, -1, -1}, 1279e8c27ec8Sbaban {"S-1-5-32", 573, "Event Log Readers", 0, SENTINEL_PID, -1, -1}, 128076b27f93Sbaban {"S-1-5-32", 574, "Certificate Service DCOM Access", 0, 1281e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1282e8c27ec8Sbaban {"S-1-5-64", 21, "Digest Authentication", 0, SENTINEL_PID, -1, -1}, 1283e8c27ec8Sbaban {"S-1-5-64", 10, "NTLM Authentication", 0, SENTINEL_PID, -1, -1}, 1284e8c27ec8Sbaban {"S-1-5-64", 14, "SChannel Authentication", 0, SENTINEL_PID, -1, -1}, 1285e8c27ec8Sbaban {NULL, UINT32_MAX, NULL, -1, SENTINEL_PID, -1, -1} 1286c5c4113dSnw141292 }; 1287c5c4113dSnw141292 1288e8c27ec8Sbaban /* 1289e8c27ec8Sbaban * Lookup well-known SIDs table either by winname or by SID. 1290e8c27ec8Sbaban * If the given winname or SID is a well-known SID then we set wksid 1291e8c27ec8Sbaban * variable and then proceed to see if the SID has a hard mapping to 1292e8c27ec8Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to 1293e8c27ec8Sbaban * fixed ephemeral ids). If we find such mapping then we return 1294e8c27ec8Sbaban * success otherwise notfound. If a well-known SID is mapped to 1295e8c27ec8Sbaban * SENTINEL_PID and the direction field is set (bi-directional or 1296e8c27ec8Sbaban * win2unix) then we treat it as inhibited mapping and return no 1297e8c27ec8Sbaban * mapping (Ex. S-1-0-0). 1298e8c27ec8Sbaban */ 1299cd37da74Snw141292 static 1300cd37da74Snw141292 idmap_retcode 1301e8c27ec8Sbaban lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *wksid) 1302cd37da74Snw141292 { 1303c5c4113dSnw141292 int i; 130462c60062Sbaban 1305e8c27ec8Sbaban *wksid = 0; 1306e8c27ec8Sbaban 1307e8c27ec8Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1308e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1309e8c27ec8Sbaban if ((strcasecmp(wksids[i].sidprefix, 1310e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix) != 0) || 1311e8c27ec8Sbaban wksids[i].rid != req->id1.idmap_id_u.sid.rid) 1312e8c27ec8Sbaban /* this is not our SID */ 1313e8c27ec8Sbaban continue; 1314e8c27ec8Sbaban if (req->id1name == NULL) { 1315e8c27ec8Sbaban req->id1name = strdup(wksids[i].winname); 1316e8c27ec8Sbaban if (req->id1name == NULL) 1317e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1318e8c27ec8Sbaban } 1319e8c27ec8Sbaban } else if (req->id1name != NULL) { 1320e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, req->id1name) != 0) 1321e8c27ec8Sbaban /* this is not our winname */ 1322e8c27ec8Sbaban continue; 1323e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = 1324e8c27ec8Sbaban strdup(wksids[i].sidprefix); 1325e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) 1326e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1327e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = wksids[i].rid; 1328e8c27ec8Sbaban } 1329e8c27ec8Sbaban 1330e8c27ec8Sbaban *wksid = 1; 1331e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1332e8c27ec8Sbaban 1333e8c27ec8Sbaban req->id1.idtype = (wksids[i].is_wuser) ? 1334e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1335e8c27ec8Sbaban 1336e8c27ec8Sbaban if (wksids[i].pid == SENTINEL_PID) { 1337e8c27ec8Sbaban if (wksids[i].direction == IDMAP_DIRECTION_BI || 1338e8c27ec8Sbaban wksids[i].direction == IDMAP_DIRECTION_W2U) 1339e8c27ec8Sbaban /* Inhibited */ 1340e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 1341e8c27ec8Sbaban /* Not mapped */ 1342479ac375Sdm199847 if (res->id.idtype == IDMAP_POSIXID) { 1343479ac375Sdm199847 res->id.idtype = 1344479ac375Sdm199847 (wksids[i].is_wuser) ? 1345479ac375Sdm199847 IDMAP_UID : IDMAP_GID; 1346479ac375Sdm199847 } 1347e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 1348e8c27ec8Sbaban } else if (wksids[i].direction == IDMAP_DIRECTION_U2W) 134962c60062Sbaban continue; 135062c60062Sbaban 1351e8c27ec8Sbaban switch (res->id.idtype) { 1352c5c4113dSnw141292 case IDMAP_UID: 135362c60062Sbaban if (wksids[i].is_user == 0) 135462c60062Sbaban continue; 135562c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 135662c60062Sbaban res->direction = wksids[i].direction; 135748258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 135848258c6bSjp151216 res->info.how.map_type = 135948258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 136048258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 136148258c6bSjp151216 } 1362c5c4113dSnw141292 return (IDMAP_SUCCESS); 1363c5c4113dSnw141292 case IDMAP_GID: 136462c60062Sbaban if (wksids[i].is_user == 1) 136562c60062Sbaban continue; 136662c60062Sbaban res->id.idmap_id_u.gid = wksids[i].pid; 136762c60062Sbaban res->direction = wksids[i].direction; 136848258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 136948258c6bSjp151216 res->info.how.map_type = 137048258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 137148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 137248258c6bSjp151216 } 1373c5c4113dSnw141292 return (IDMAP_SUCCESS); 1374c5c4113dSnw141292 case IDMAP_POSIXID: 137562c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 137662c60062Sbaban res->id.idtype = (!wksids[i].is_user) ? 1377c5c4113dSnw141292 IDMAP_GID : IDMAP_UID; 137862c60062Sbaban res->direction = wksids[i].direction; 137948258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 138048258c6bSjp151216 res->info.how.map_type = 138148258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 138248258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 138348258c6bSjp151216 } 1384c5c4113dSnw141292 return (IDMAP_SUCCESS); 1385c5c4113dSnw141292 default: 1386c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 1387c5c4113dSnw141292 } 1388c5c4113dSnw141292 } 1389c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1390c5c4113dSnw141292 } 1391c5c4113dSnw141292 1392cd37da74Snw141292 1393cd37da74Snw141292 static 1394cd37da74Snw141292 idmap_retcode 1395cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user) 1396cd37da74Snw141292 { 1397c5c4113dSnw141292 int i; 1398e8c27ec8Sbaban if (req->id1.idmap_id_u.uid == SENTINEL_PID) 1399e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 140062c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 140162c60062Sbaban if (wksids[i].pid == req->id1.idmap_id_u.uid && 140262c60062Sbaban wksids[i].is_user == is_user && 140362c60062Sbaban wksids[i].direction != IDMAP_DIRECTION_W2U) { 1404e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) { 1405e8c27ec8Sbaban res->id.idtype = (wksids[i].is_wuser) ? 1406e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1407e8c27ec8Sbaban } 140862c60062Sbaban res->id.idmap_id_u.sid.rid = wksids[i].rid; 1409c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 141062c60062Sbaban strdup(wksids[i].sidprefix); 1411c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 1412c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1413c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 1414c5c4113dSnw141292 } 141562c60062Sbaban res->direction = wksids[i].direction; 141648258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 141748258c6bSjp151216 res->info.how.map_type = 141848258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 141948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 142048258c6bSjp151216 } 1421c5c4113dSnw141292 return (IDMAP_SUCCESS); 1422c5c4113dSnw141292 } 1423c5c4113dSnw141292 } 142462c60062Sbaban return (IDMAP_ERR_NOTFOUND); 142562c60062Sbaban } 142662c60062Sbaban 1427cd37da74Snw141292 idmap_retcode 1428cd37da74Snw141292 lookup_wksids_name2sid(const char *name, char **canonname, char **sidprefix, 1429cd37da74Snw141292 idmap_rid_t *rid, int *type) 1430cd37da74Snw141292 { 143162c60062Sbaban int i; 1432479ac375Sdm199847 1433479ac375Sdm199847 if ((strncasecmp(name, "BUILTIN\\", 8) == 0) || 1434479ac375Sdm199847 (strncasecmp(name, "BUILTIN/", 8) == 0)) 1435479ac375Sdm199847 name += 8; 1436479ac375Sdm199847 143762c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1438e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, name) != 0) 1439e8c27ec8Sbaban continue; 1440e8c27ec8Sbaban if (sidprefix != NULL && 1441e8c27ec8Sbaban (*sidprefix = strdup(wksids[i].sidprefix)) == NULL) { 144262c60062Sbaban idmapdlog(LOG_ERR, "Out of memory"); 144362c60062Sbaban return (IDMAP_ERR_MEMORY); 144462c60062Sbaban } 1445cd37da74Snw141292 if (canonname != NULL && 1446cd37da74Snw141292 (*canonname = strdup(wksids[i].winname)) == NULL) { 1447cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1448e8c27ec8Sbaban if (sidprefix != NULL) { 1449e8c27ec8Sbaban free(*sidprefix); 1450e8c27ec8Sbaban *sidprefix = NULL; 1451e8c27ec8Sbaban } 1452cd37da74Snw141292 return (IDMAP_ERR_MEMORY); 1453cd37da74Snw141292 } 145462c60062Sbaban if (type != NULL) 1455e8c27ec8Sbaban *type = (wksids[i].is_wuser) ? 145662c60062Sbaban _IDMAP_T_USER : _IDMAP_T_GROUP; 145762c60062Sbaban if (rid != NULL) 145862c60062Sbaban *rid = wksids[i].rid; 145962c60062Sbaban return (IDMAP_SUCCESS); 146062c60062Sbaban } 1461c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1462c5c4113dSnw141292 } 1463c5c4113dSnw141292 1464cd37da74Snw141292 static 1465cd37da74Snw141292 idmap_retcode 1466cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1467cd37da74Snw141292 { 1468c5c4113dSnw141292 char *end; 1469c5c4113dSnw141292 char *sql = NULL; 1470c5c4113dSnw141292 const char **values; 1471c5c4113dSnw141292 sqlite_vm *vm = NULL; 1472c5c4113dSnw141292 int ncol, is_user; 1473c5c4113dSnw141292 uid_t pid; 1474c5c4113dSnw141292 time_t curtime, exp; 1475c5c4113dSnw141292 idmap_retcode retcode; 1476042addd6Sbaban char *is_user_string, *lower_name; 1477c5c4113dSnw141292 1478c5c4113dSnw141292 /* Current time */ 1479c5c4113dSnw141292 errno = 0; 1480c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1481cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1482c5c4113dSnw141292 strerror(errno)); 1483c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1484c5c4113dSnw141292 goto out; 1485c5c4113dSnw141292 } 1486c5c4113dSnw141292 1487e8c27ec8Sbaban switch (res->id.idtype) { 1488cd37da74Snw141292 case IDMAP_UID: 1489cd37da74Snw141292 is_user_string = "1"; 1490cd37da74Snw141292 break; 1491cd37da74Snw141292 case IDMAP_GID: 1492cd37da74Snw141292 is_user_string = "0"; 1493cd37da74Snw141292 break; 1494cd37da74Snw141292 case IDMAP_POSIXID: 1495cd37da74Snw141292 /* the non-diagonal mapping */ 1496cd37da74Snw141292 is_user_string = "is_wuser"; 1497cd37da74Snw141292 break; 1498cd37da74Snw141292 default: 1499cd37da74Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 1500cd37da74Snw141292 goto out; 1501cd37da74Snw141292 } 1502cd37da74Snw141292 1503c5c4113dSnw141292 /* SQL to lookup the cache */ 150448258c6bSjp151216 1505e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1506e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 150748258c6bSjp151216 "unixname, u2w, is_wuser, " 150848258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 150948258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1510cd37da74Snw141292 "FROM idmap_cache WHERE is_user = %s AND " 1511c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 1512c5c4113dSnw141292 "(pid >= 2147483648 OR " 1513c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1514c5c4113dSnw141292 "expiration > %d));", 1515e8c27ec8Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix, 1516e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, curtime); 1517e8c27ec8Sbaban } else if (req->id1name != NULL) { 1518042addd6Sbaban if ((lower_name = tolower_u8(req->id1name)) == NULL) 1519042addd6Sbaban lower_name = req->id1name; 1520e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 152148258c6bSjp151216 "unixname, u2w, is_wuser, " 152248258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 152348258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1524e8c27ec8Sbaban "FROM idmap_cache WHERE is_user = %s AND " 1525e8c27ec8Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND " 1526e8c27ec8Sbaban "(pid >= 2147483648 OR " 1527e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 1528e8c27ec8Sbaban "expiration > %d));", 152948258c6bSjp151216 is_user_string, lower_name, req->id1domain, 153048258c6bSjp151216 curtime); 1531042addd6Sbaban if (lower_name != req->id1name) 1532042addd6Sbaban free(lower_name); 1533e8c27ec8Sbaban } else { 1534e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 1535e8c27ec8Sbaban goto out; 1536e8c27ec8Sbaban } 1537c5c4113dSnw141292 if (sql == NULL) { 1538c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1539c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1540c5c4113dSnw141292 goto out; 1541c5c4113dSnw141292 } 154248258c6bSjp151216 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 154348258c6bSjp151216 14, &values); 1544c5c4113dSnw141292 sqlite_freemem(sql); 1545c5c4113dSnw141292 1546c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 1547c5c4113dSnw141292 goto out; 1548c5c4113dSnw141292 } else if (retcode == IDMAP_SUCCESS) { 1549c5c4113dSnw141292 /* sanity checks */ 1550c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 1551c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1552c5c4113dSnw141292 goto out; 1553c5c4113dSnw141292 } 1554c5c4113dSnw141292 1555c5c4113dSnw141292 pid = strtoul(values[0], &end, 10); 1556c5c4113dSnw141292 is_user = strncmp(values[1], "0", 2) ? 1 : 0; 1557c5c4113dSnw141292 1558cd37da74Snw141292 if (is_user) { 1559cd37da74Snw141292 res->id.idtype = IDMAP_UID; 1560cd37da74Snw141292 res->id.idmap_id_u.uid = pid; 1561cd37da74Snw141292 } else { 1562cd37da74Snw141292 res->id.idtype = IDMAP_GID; 1563cd37da74Snw141292 res->id.idmap_id_u.gid = pid; 1564cd37da74Snw141292 } 1565cd37da74Snw141292 1566c5c4113dSnw141292 /* 1567c5c4113dSnw141292 * We may have an expired ephemeral mapping. Consider 1568c5c4113dSnw141292 * the expired entry as valid if we are not going to 1569c5c4113dSnw141292 * perform name-based mapping. But do not renew the 1570c5c4113dSnw141292 * expiration. 1571c5c4113dSnw141292 * If we will be doing name-based mapping then store the 1572c5c4113dSnw141292 * ephemeral pid in the result so that we can use it 1573c5c4113dSnw141292 * if we end up doing dynamic mapping again. 1574c5c4113dSnw141292 */ 1575c5c4113dSnw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) && 1576cd37da74Snw141292 !AVOID_NAMESERVICE(req) && 1577cd37da74Snw141292 IS_EPHEMERAL(pid) && values[2] != NULL) { 1578c5c4113dSnw141292 exp = strtoll(values[2], &end, 10); 1579c5c4113dSnw141292 if (exp && exp <= curtime) { 1580c5c4113dSnw141292 /* Store the ephemeral pid */ 1581651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 1582cd37da74Snw141292 req->direction |= is_user 1583cd37da74Snw141292 ? _IDMAP_F_EXP_EPH_UID 1584cd37da74Snw141292 : _IDMAP_F_EXP_EPH_GID; 1585c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 1586c5c4113dSnw141292 } 1587c5c4113dSnw141292 } 1588c5c4113dSnw141292 } 1589c5c4113dSnw141292 1590c5c4113dSnw141292 out: 1591c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 159262c60062Sbaban if (values[4] != NULL) 1593c5c4113dSnw141292 res->direction = 1594651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 1595651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 1596c5c4113dSnw141292 else 1597651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 1598c5c4113dSnw141292 159962c60062Sbaban if (values[3] != NULL) { 1600e8c27ec8Sbaban if (req->id2name != NULL) 1601e8c27ec8Sbaban free(req->id2name); 16028e228215Sdm199847 req->id2name = strdup(values[3]); 16038e228215Sdm199847 if (req->id2name == NULL) { 1604c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1605c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1606c5c4113dSnw141292 } 1607c5c4113dSnw141292 } 1608e8c27ec8Sbaban 1609e8c27ec8Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ? 1610e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 161148258c6bSjp151216 161248258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 161348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 161448258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 161548258c6bSjp151216 switch (res->info.how.map_type) { 161648258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 161748258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 161848258c6bSjp151216 strdup(values[7]); 161948258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 162048258c6bSjp151216 strdup(values[8]); 162148258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 162248258c6bSjp151216 strdup(values[9]); 162348258c6bSjp151216 break; 162448258c6bSjp151216 162548258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 162648258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 162748258c6bSjp151216 strdup(values[7]); 162848258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 162948258c6bSjp151216 strdup(values[8]); 163048258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 163148258c6bSjp151216 strdup(values[9]); 163248258c6bSjp151216 break; 163348258c6bSjp151216 163448258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 163548258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 163648258c6bSjp151216 strdup(values[10]); 163748258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 163848258c6bSjp151216 strdup(values[11]); 163948258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 164048258c6bSjp151216 strdup(values[12]); 164148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 164248258c6bSjp151216 strtoul(values[13], &end, 1); 164348258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 164448258c6bSjp151216 is_user; 164548258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 164648258c6bSjp151216 strtoul(values[5], &end, 1); 164748258c6bSjp151216 break; 164848258c6bSjp151216 164948258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 165048258c6bSjp151216 break; 165148258c6bSjp151216 165248258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 165348258c6bSjp151216 break; 165448258c6bSjp151216 165548258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 165648258c6bSjp151216 break; 165748258c6bSjp151216 165848258c6bSjp151216 default: 165948258c6bSjp151216 /* Unknow mapping type */ 166048258c6bSjp151216 assert(FALSE); 166148258c6bSjp151216 } 166248258c6bSjp151216 } 1663c5c4113dSnw141292 } 166462c60062Sbaban if (vm != NULL) 1665c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1666c5c4113dSnw141292 return (retcode); 1667c5c4113dSnw141292 } 1668c5c4113dSnw141292 1669cd37da74Snw141292 static 1670cd37da74Snw141292 idmap_retcode 167162c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid, 1672cd37da74Snw141292 char **name, char **domain, int *type) 1673cd37da74Snw141292 { 1674c5c4113dSnw141292 char *end; 1675c5c4113dSnw141292 char *sql = NULL; 1676c5c4113dSnw141292 const char **values; 1677c5c4113dSnw141292 sqlite_vm *vm = NULL; 1678c5c4113dSnw141292 int ncol; 1679c5c4113dSnw141292 time_t curtime; 1680c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 1681c5c4113dSnw141292 1682c5c4113dSnw141292 /* Get current time */ 1683c5c4113dSnw141292 errno = 0; 1684c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1685cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1686c5c4113dSnw141292 strerror(errno)); 1687c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1688c5c4113dSnw141292 goto out; 1689c5c4113dSnw141292 } 1690c5c4113dSnw141292 1691c5c4113dSnw141292 /* SQL to lookup the cache */ 1692cd37da74Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type " 1693cd37da74Snw141292 "FROM name_cache WHERE " 1694c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND " 1695c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1696c5c4113dSnw141292 "expiration > %d);", 1697c5c4113dSnw141292 sidprefix, rid, curtime); 1698c5c4113dSnw141292 if (sql == NULL) { 1699c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1700c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1701c5c4113dSnw141292 goto out; 1702c5c4113dSnw141292 } 1703c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values); 1704c5c4113dSnw141292 sqlite_freemem(sql); 1705c5c4113dSnw141292 1706c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 170762c60062Sbaban if (type != NULL) { 1708c5c4113dSnw141292 if (values[2] == NULL) { 1709c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1710c5c4113dSnw141292 goto out; 1711c5c4113dSnw141292 } 1712c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 1713c5c4113dSnw141292 } 1714c5c4113dSnw141292 171562c60062Sbaban if (name != NULL && values[0] != NULL) { 1716c5c4113dSnw141292 if ((*name = strdup(values[0])) == NULL) { 1717c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1718c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1719c5c4113dSnw141292 goto out; 1720c5c4113dSnw141292 } 1721c5c4113dSnw141292 } 1722c5c4113dSnw141292 172362c60062Sbaban if (domain != NULL && values[1] != NULL) { 1724c5c4113dSnw141292 if ((*domain = strdup(values[1])) == NULL) { 172562c60062Sbaban if (name != NULL && *name) { 1726c5c4113dSnw141292 free(*name); 1727c5c4113dSnw141292 *name = NULL; 1728c5c4113dSnw141292 } 1729c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1730c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1731c5c4113dSnw141292 goto out; 1732c5c4113dSnw141292 } 1733c5c4113dSnw141292 } 1734c5c4113dSnw141292 } 1735c5c4113dSnw141292 1736c5c4113dSnw141292 out: 173762c60062Sbaban if (vm != NULL) 1738c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1739c5c4113dSnw141292 return (retcode); 1740c5c4113dSnw141292 } 1741c5c4113dSnw141292 1742c5c4113dSnw141292 /* 1743e8c27ec8Sbaban * Given SID, find winname using name_cache OR 1744e8c27ec8Sbaban * Given winname, find SID using name_cache. 1745e8c27ec8Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and 1746e8c27ec8Sbaban * req->id2 is unix id 1747c5c4113dSnw141292 */ 1748cd37da74Snw141292 static 1749cd37da74Snw141292 idmap_retcode 1750e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1751cd37da74Snw141292 { 1752c5c4113dSnw141292 int type = -1; 1753c5c4113dSnw141292 idmap_retcode retcode; 1754e8c27ec8Sbaban char *sidprefix = NULL; 1755c5c4113dSnw141292 idmap_rid_t rid; 1756c5c4113dSnw141292 char *name = NULL, *domain = NULL; 1757c5c4113dSnw141292 1758e8c27ec8Sbaban /* Done if we've both sid and winname */ 1759e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) 1760e8c27ec8Sbaban return (IDMAP_SUCCESS); 1761c5c4113dSnw141292 1762e8c27ec8Sbaban /* Lookup sid to winname */ 1763e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1764e8c27ec8Sbaban retcode = lookup_cache_sid2name(cache, 1765e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix, 1766e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type); 176762c60062Sbaban goto out; 1768e8c27ec8Sbaban } 176962c60062Sbaban 1770e8c27ec8Sbaban /* Lookup winame to sid */ 1771e8c27ec8Sbaban retcode = lookup_cache_name2sid(cache, req->id1name, req->id1domain, 1772e8c27ec8Sbaban &name, &sidprefix, &rid, &type); 1773c5c4113dSnw141292 177462c60062Sbaban out: 1775e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1776c5c4113dSnw141292 free(name); 1777c5c4113dSnw141292 free(domain); 1778e8c27ec8Sbaban free(sidprefix); 1779e8c27ec8Sbaban return (retcode); 1780e8c27ec8Sbaban } 1781e8c27ec8Sbaban 1782e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) { 1783e8c27ec8Sbaban res->id.idtype = (type == _IDMAP_T_USER) ? 1784e8c27ec8Sbaban IDMAP_UID : IDMAP_GID; 1785e8c27ec8Sbaban } 1786e8c27ec8Sbaban req->id1.idtype = (type == _IDMAP_T_USER) ? 1787e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1788e8c27ec8Sbaban 1789e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1790e8c27ec8Sbaban if (name != NULL) { 1791e8c27ec8Sbaban free(req->id1name); /* Free existing winname */ 1792e8c27ec8Sbaban req->id1name = name; /* and use canonical name instead */ 1793e8c27ec8Sbaban } 1794e8c27ec8Sbaban if (req->id1domain == NULL) 1795e8c27ec8Sbaban req->id1domain = domain; 1796e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) { 1797e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix; 1798e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = rid; 1799c5c4113dSnw141292 } 1800c5c4113dSnw141292 return (retcode); 1801c5c4113dSnw141292 } 1802c5c4113dSnw141292 1803e8c27ec8Sbaban /* 1804e8c27ec8Sbaban * Batch AD lookups 1805e8c27ec8Sbaban */ 1806c5c4113dSnw141292 idmap_retcode 1807e8c27ec8Sbaban ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch, 1808cd37da74Snw141292 idmap_ids_res *result) 1809cd37da74Snw141292 { 1810c5c4113dSnw141292 idmap_retcode retcode; 1811e8c27ec8Sbaban int i, add, type, is_wuser, is_user; 1812e8c27ec8Sbaban int retries = 0, eunixtype; 1813e8c27ec8Sbaban char **unixname; 1814c5c4113dSnw141292 idmap_mapping *req; 1815c5c4113dSnw141292 idmap_id_res *res; 1816e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 181748258c6bSjp151216 idmap_how *how; 1818479ac375Sdm199847 char **dn, **attr, **value; 1819e8c27ec8Sbaban 1820e8c27ec8Sbaban /* 1821e8c27ec8Sbaban * Since req->id2.idtype is unused, we will use it here 1822e8c27ec8Sbaban * to retrieve the value of sid_type. But it needs to be 1823e8c27ec8Sbaban * reset to IDMAP_NONE before we return to prevent xdr 1824e8c27ec8Sbaban * from mis-interpreting req->id2 when it tries to free 1825e8c27ec8Sbaban * the input argument. Other option is to allocate an 1826e8c27ec8Sbaban * array of integers and use it instead for the batched 1827e8c27ec8Sbaban * call. But why un-necessarily allocate memory. That may 1828e8c27ec8Sbaban * be an option if req->id2.idtype cannot be re-used in 1829e8c27ec8Sbaban * future. 1830e8c27ec8Sbaban */ 1831c5c4113dSnw141292 1832c5c4113dSnw141292 if (state->ad_nqueries == 0) 1833c5c4113dSnw141292 return (IDMAP_SUCCESS); 1834c5c4113dSnw141292 183596c3a9a0Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 183696c3a9a0Sbaban req = &batch->idmap_mapping_batch_val[i]; 183796c3a9a0Sbaban res = &result->ids.ids_val[i]; 183896c3a9a0Sbaban 183996c3a9a0Sbaban /* Skip if not marked for AD lookup or already in error. */ 184096c3a9a0Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD) || 184196c3a9a0Sbaban res->retcode != IDMAP_SUCCESS) 184296c3a9a0Sbaban continue; 184396c3a9a0Sbaban 184496c3a9a0Sbaban /* Init status */ 184596c3a9a0Sbaban res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR; 184696c3a9a0Sbaban } 184796c3a9a0Sbaban 1848c5c4113dSnw141292 retry: 1849e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, state->ad_nqueries, 1850e8c27ec8Sbaban &qs); 1851e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 18520dcc7149Snw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 18530dcc7149Snw141292 goto retry; 1854349d5d8fSnw141292 degrade_svc(1, "failed to create batch for AD lookup"); 1855e8c27ec8Sbaban goto out; 1856c5c4113dSnw141292 } 1857c5c4113dSnw141292 1858c8e26105Sjp151216 restore_svc(); 1859c8e26105Sjp151216 1860e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 1861e8c27ec8Sbaban state->ad_unixgroup_attr); 1862e8c27ec8Sbaban 1863e8c27ec8Sbaban for (i = 0, add = 0; i < batch->idmap_mapping_batch_len; i++) { 1864c5c4113dSnw141292 req = &batch->idmap_mapping_batch_val[i]; 1865c5c4113dSnw141292 res = &result->ids.ids_val[i]; 186648258c6bSjp151216 how = &res->info.how; 186748258c6bSjp151216 1868e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 1869e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1870c5c4113dSnw141292 1871e8c27ec8Sbaban /* Skip if not marked for AD lookup */ 1872e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1873e8c27ec8Sbaban continue; 1874e8c27ec8Sbaban 187596c3a9a0Sbaban if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR) 1876c5c4113dSnw141292 continue; 1877e8c27ec8Sbaban 1878e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1879e8c27ec8Sbaban 1880479ac375Sdm199847 /* win2unix request: */ 1881e8c27ec8Sbaban 1882479ac375Sdm199847 unixname = dn = attr = value = NULL; 1883e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 1884e8c27ec8Sbaban if (req->id2name == NULL) { 1885e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID && 1886e8c27ec8Sbaban AD_OR_MIXED(state->nm_siduid)) { 1887e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 1888e8c27ec8Sbaban unixname = &req->id2name; 1889e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GID && 1890e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1891e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 1892e8c27ec8Sbaban unixname = &req->id2name; 1893e8c27ec8Sbaban } else if (AD_OR_MIXED(state->nm_siduid) || 1894e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1895e8c27ec8Sbaban unixname = &req->id2name; 1896e8c27ec8Sbaban } 1897e8c27ec8Sbaban } 1898e8c27ec8Sbaban add = 1; 1899479ac375Sdm199847 if (unixname != NULL) { 1900479ac375Sdm199847 /* 1901479ac375Sdm199847 * Get how info for DS-based name 1902479ac375Sdm199847 * mapping only if AD or MIXED 1903479ac375Sdm199847 * mode is enabled. 1904479ac375Sdm199847 */ 1905479ac375Sdm199847 idmap_info_free(&res->info); 190648258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 190748258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1908479ac375Sdm199847 dn = &how->idmap_how_u.ad.dn; 1909479ac375Sdm199847 attr = &how->idmap_how_u.ad.attr; 1910479ac375Sdm199847 value = &how->idmap_how_u.ad.value; 1911479ac375Sdm199847 } 1912479ac375Sdm199847 if (req->id1.idmap_id_u.sid.prefix != NULL) { 1913479ac375Sdm199847 /* Lookup AD by SID */ 1914c5c4113dSnw141292 retcode = idmap_sid2name_batch_add1( 1915e8c27ec8Sbaban qs, req->id1.idmap_id_u.sid.prefix, 1916e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, eunixtype, 1917479ac375Sdm199847 dn, attr, value, 1918479ac375Sdm199847 (req->id1name == NULL) ? 1919479ac375Sdm199847 &req->id1name : NULL, 1920479ac375Sdm199847 (req->id1domain == NULL) ? 1921479ac375Sdm199847 &req->id1domain : NULL, 1922479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1923479ac375Sdm199847 &res->retcode); 1924479ac375Sdm199847 } else { 1925479ac375Sdm199847 /* Lookup AD by winname */ 1926479ac375Sdm199847 assert(req->id1name != NULL); 1927479ac375Sdm199847 retcode = idmap_name2sid_batch_add1( 1928479ac375Sdm199847 qs, req->id1name, req->id1domain, 1929479ac375Sdm199847 eunixtype, 1930479ac375Sdm199847 dn, attr, value, 1931479ac375Sdm199847 &req->id1name, 1932479ac375Sdm199847 &req->id1.idmap_id_u.sid.prefix, 1933479ac375Sdm199847 &req->id1.idmap_id_u.sid.rid, 1934479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1935479ac375Sdm199847 &res->retcode); 1936479ac375Sdm199847 } 1937c5c4113dSnw141292 1938e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 1939479ac375Sdm199847 1940479ac375Sdm199847 /* unix2win request: */ 1941e8c27ec8Sbaban 1942e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL && 1943e8c27ec8Sbaban req->id2name != NULL) { 1944e8c27ec8Sbaban /* Already have SID and winname -- done */ 1945e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 1946e8c27ec8Sbaban continue; 1947c5c4113dSnw141292 } 1948c5c4113dSnw141292 1949e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) { 1950cd37da74Snw141292 /* 1951e8c27ec8Sbaban * SID but no winname -- lookup AD by 1952e8c27ec8Sbaban * SID to get winname. 1953479ac375Sdm199847 * how info is not needed here because 1954479ac375Sdm199847 * we are not retrieving unixname from 1955479ac375Sdm199847 * AD. 1956cd37da74Snw141292 */ 1957e8c27ec8Sbaban add = 1; 1958e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1( 1959e8c27ec8Sbaban qs, res->id.idmap_id_u.sid.prefix, 1960e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 196148258c6bSjp151216 _IDMAP_T_UNDEF, 1962479ac375Sdm199847 NULL, NULL, NULL, 196348258c6bSjp151216 &req->id2name, 1964e8c27ec8Sbaban &req->id2domain, (int *)&req->id2.idtype, 1965e8c27ec8Sbaban NULL, &res->retcode); 1966e8c27ec8Sbaban } else if (req->id2name != NULL) { 1967e8c27ec8Sbaban /* 1968e8c27ec8Sbaban * winname but no SID -- lookup AD by 1969e8c27ec8Sbaban * winname to get SID. 1970479ac375Sdm199847 * how info is not needed here because 1971479ac375Sdm199847 * we are not retrieving unixname from 1972479ac375Sdm199847 * AD. 1973e8c27ec8Sbaban */ 1974e8c27ec8Sbaban add = 1; 1975e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1( 1976e8c27ec8Sbaban qs, req->id2name, req->id2domain, 197748258c6bSjp151216 _IDMAP_T_UNDEF, 1978479ac375Sdm199847 NULL, NULL, NULL, NULL, 1979e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1980e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1981e8c27ec8Sbaban (int *)&req->id2.idtype, NULL, 1982e8c27ec8Sbaban &res->retcode); 1983e8c27ec8Sbaban } else if (req->id1name != NULL) { 1984e8c27ec8Sbaban /* 1985e8c27ec8Sbaban * No SID and no winname but we've unixname -- 1986e8c27ec8Sbaban * lookup AD by unixname to get SID. 1987e8c27ec8Sbaban */ 1988e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 1989e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 1990e8c27ec8Sbaban is_wuser = 1; 1991e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 1992e8c27ec8Sbaban is_wuser = 0; 1993e8c27ec8Sbaban else 1994e8c27ec8Sbaban is_wuser = is_user; 1995e8c27ec8Sbaban add = 1; 1996479ac375Sdm199847 idmap_info_free(&res->info); 199748258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 199848258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1999e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1( 2000e8c27ec8Sbaban qs, req->id1name, is_user, is_wuser, 200148258c6bSjp151216 &how->idmap_how_u.ad.dn, 200248258c6bSjp151216 &how->idmap_how_u.ad.attr, 200348258c6bSjp151216 &how->idmap_how_u.ad.value, 2004e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 2005e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 2006e8c27ec8Sbaban &req->id2name, &req->id2domain, 2007e8c27ec8Sbaban (int *)&req->id2.idtype, &res->retcode); 2008e8c27ec8Sbaban } 2009e8c27ec8Sbaban } 2010e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2011e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 2012e8c27ec8Sbaban break; 2013e8c27ec8Sbaban } 2014cd37da74Snw141292 } 2015cd37da74Snw141292 201696c3a9a0Sbaban if (retcode == IDMAP_SUCCESS) { 201796c3a9a0Sbaban /* add keeps track if we added an entry to the batch */ 201896c3a9a0Sbaban if (add) 20190dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 202096c3a9a0Sbaban else 202196c3a9a0Sbaban idmap_lookup_release_batch(&qs); 202296c3a9a0Sbaban } 2023cd37da74Snw141292 2024c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 2025c5c4113dSnw141292 goto retry; 2026c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 2027349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 2028c5c4113dSnw141292 2029e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2030e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests"); 2031c5c4113dSnw141292 2032c5c4113dSnw141292 out: 2033e8c27ec8Sbaban /* 2034e8c27ec8Sbaban * This loop does the following: 2035479ac375Sdm199847 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request. 2036479ac375Sdm199847 * 2. Reset req->id2.idtype to IDMAP_NONE 2037479ac375Sdm199847 * 3. If batch_start or batch_add failed then set the status 2038479ac375Sdm199847 * of each request marked for AD lookup to that error. 2039479ac375Sdm199847 * 4. Evaluate the type of the AD object (i.e. user or group) and 2040479ac375Sdm199847 * update the idtype in request. 2041e8c27ec8Sbaban */ 2042e8c27ec8Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 2043e8c27ec8Sbaban req = &batch->idmap_mapping_batch_val[i]; 2044e8c27ec8Sbaban type = req->id2.idtype; 2045e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 20465e0794bcSbaban res = &result->ids.ids_val[i]; 204748258c6bSjp151216 how = &res->info.how; 2048e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 2049e8c27ec8Sbaban continue; 2050e8c27ec8Sbaban 2051479ac375Sdm199847 /* Reset AD lookup flag */ 2052479ac375Sdm199847 req->direction &= ~(_IDMAP_F_LOOKUP_AD); 2053479ac375Sdm199847 2054479ac375Sdm199847 /* 2055479ac375Sdm199847 * If batch_start or batch_add failed then set the status 2056479ac375Sdm199847 * of each request marked for AD lookup to that error. 2057479ac375Sdm199847 */ 2058e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2059e8c27ec8Sbaban res->retcode = retcode; 2060e8c27ec8Sbaban continue; 2061e8c27ec8Sbaban } 2062e8c27ec8Sbaban 2063e8c27ec8Sbaban if (!add) 2064e8c27ec8Sbaban continue; 2065e8c27ec8Sbaban 206648258c6bSjp151216 if (res->retcode == IDMAP_ERR_NOTFOUND) { 206748258c6bSjp151216 /* Nothing found - remove the preset info */ 2068479ac375Sdm199847 idmap_info_free(&res->info); 206948258c6bSjp151216 } 207048258c6bSjp151216 2071e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 2072e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) 2073e8c27ec8Sbaban continue; 2074479ac375Sdm199847 /* Evaluate result type */ 2075e8c27ec8Sbaban switch (type) { 2076e8c27ec8Sbaban case _IDMAP_T_USER: 2077e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2078e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 2079e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 2080e8c27ec8Sbaban break; 2081e8c27ec8Sbaban case _IDMAP_T_GROUP: 2082e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2083e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 2084e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 2085e8c27ec8Sbaban break; 2086e8c27ec8Sbaban default: 2087e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2088e8c27ec8Sbaban break; 2089e8c27ec8Sbaban } 2090479ac375Sdm199847 if (res->retcode == IDMAP_SUCCESS && 2091479ac375Sdm199847 req->id1name != NULL && 2092479ac375Sdm199847 (req->id2name == NULL || 2093479ac375Sdm199847 res->id.idmap_id_u.uid == SENTINEL_PID) && 2094479ac375Sdm199847 NLDAP_MODE(res->id.idtype, state)) { 2095479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2096479ac375Sdm199847 state->nldap_nqueries++; 2097479ac375Sdm199847 } 2098e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 2099e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2100e8c27ec8Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) && 2101e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix == NULL && 2102e8c27ec8Sbaban req->id2name == NULL && /* no winname */ 2103e8c27ec8Sbaban req->id1name != NULL) /* unixname */ 2104e8c27ec8Sbaban /* 2105479ac375Sdm199847 * If AD lookup by unixname failed 2106479ac375Sdm199847 * with non fatal error then clear 2107479ac375Sdm199847 * the error (i.e set res->retcode 2108479ac375Sdm199847 * to success). This allows the next 2109479ac375Sdm199847 * pass to process other mapping 2110479ac375Sdm199847 * mechanisms for this request. 2111e8c27ec8Sbaban */ 2112e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 2113e8c27ec8Sbaban continue; 2114e8c27ec8Sbaban } 2115479ac375Sdm199847 /* Evaluate result type */ 2116e8c27ec8Sbaban switch (type) { 2117e8c27ec8Sbaban case _IDMAP_T_USER: 2118e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2119e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 2120e8c27ec8Sbaban break; 2121e8c27ec8Sbaban case _IDMAP_T_GROUP: 2122e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2123e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 2124e8c27ec8Sbaban break; 2125e8c27ec8Sbaban default: 2126e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2127e8c27ec8Sbaban break; 2128e8c27ec8Sbaban } 2129e8c27ec8Sbaban } 2130e8c27ec8Sbaban } 2131e8c27ec8Sbaban 2132e8c27ec8Sbaban /* AD lookups done. Reset state->ad_nqueries and return */ 2133e8c27ec8Sbaban state->ad_nqueries = 0; 2134c5c4113dSnw141292 return (retcode); 2135c5c4113dSnw141292 } 2136c5c4113dSnw141292 2137cd37da74Snw141292 /* 2138cd37da74Snw141292 * Convention when processing win2unix requests: 2139cd37da74Snw141292 * 2140cd37da74Snw141292 * Windows identity: 2141cd37da74Snw141292 * req->id1name = 2142cd37da74Snw141292 * winname if given otherwise winname found will be placed 2143cd37da74Snw141292 * here. 2144cd37da74Snw141292 * req->id1domain = 2145cd37da74Snw141292 * windomain if given otherwise windomain found will be 2146cd37da74Snw141292 * placed here. 2147cd37da74Snw141292 * req->id1.idtype = 2148cd37da74Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll 2149cd37da74Snw141292 * be set to IDMAP_USID/GSID depending upon whether the 2150cd37da74Snw141292 * given SID is user or group respectively. The user/group-ness 2151cd37da74Snw141292 * is determined either when looking up well-known SIDs table OR 2152479ac375Sdm199847 * if the SID is found in namecache OR by ad_lookup_one() OR by 2153cd37da74Snw141292 * ad_lookup_batch(). 2154cd37da74Snw141292 * req->id1..sid.[prefix, rid] = 2155cd37da74Snw141292 * SID if given otherwise SID found will be placed here. 2156cd37da74Snw141292 * 2157cd37da74Snw141292 * Unix identity: 2158cd37da74Snw141292 * req->id2name = 2159cd37da74Snw141292 * unixname found will be placed here. 2160cd37da74Snw141292 * req->id2domain = 2161cd37da74Snw141292 * NOT USED 2162cd37da74Snw141292 * res->id.idtype = 2163cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 2164cd37da74Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found 2165cd37da74Snw141292 * will be placed here. 2166cd37da74Snw141292 * res->id..[uid or gid] = 2167cd37da74Snw141292 * UID/GID found will be placed here. 2168cd37da74Snw141292 * 2169cd37da74Snw141292 * Others: 2170cd37da74Snw141292 * res->retcode = 2171cd37da74Snw141292 * Return status for this request will be placed here. 2172cd37da74Snw141292 * res->direction = 2173cd37da74Snw141292 * Direction found will be placed here. Direction 2174cd37da74Snw141292 * meaning whether the resultant mapping is valid 2175cd37da74Snw141292 * only from win2unix or bi-directional. 2176cd37da74Snw141292 * req->direction = 2177cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 2178cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 2179cd37da74Snw141292 * of the request. 2180cd37da74Snw141292 * req->id2.idtype = 2181cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 2182cd37da74Snw141292 * type and is used to initialize res->id.idtype. 2183cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 2184cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 2185cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 2186cd37da74Snw141292 * mis-interpreting the contents of req->id2. 2187cd37da74Snw141292 * req->id2..[uid or gid or sid] = 2188cd37da74Snw141292 * NOT USED 2189cd37da74Snw141292 */ 2190cd37da74Snw141292 2191cd37da74Snw141292 /* 2192cd37da74Snw141292 * This function does the following: 2193cd37da74Snw141292 * 1. Lookup well-known SIDs table. 2194cd37da74Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it. 2195cd37da74Snw141292 * 3. Lookup cache. 2196cd37da74Snw141292 * 4. Check if the client does not want new mapping to be allocated 2197cd37da74Snw141292 * in which case this pass is the final pass. 2198cd37da74Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs 2199cd37da74Snw141292 * to do AD lookup. 2200cd37da74Snw141292 */ 2201c5c4113dSnw141292 idmap_retcode 2202479ac375Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req, 2203cd37da74Snw141292 idmap_id_res *res) 2204cd37da74Snw141292 { 2205c5c4113dSnw141292 idmap_retcode retcode; 2206e8c27ec8Sbaban int wksid; 2207c5c4113dSnw141292 2208e8c27ec8Sbaban /* Initialize result */ 2209e8c27ec8Sbaban res->id.idtype = req->id2.idtype; 2210e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2211e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 2212e8c27ec8Sbaban wksid = 0; 2213c5c4113dSnw141292 2214cf5b5989Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) { 2215e8c27ec8Sbaban if (req->id1name == NULL) { 2216e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 2217c5c4113dSnw141292 goto out; 2218c5c4113dSnw141292 } 2219e8c27ec8Sbaban /* sanitize sidprefix */ 2220e8c27ec8Sbaban free(req->id1.idmap_id_u.sid.prefix); 2221e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = NULL; 2222e8c27ec8Sbaban } 2223c5c4113dSnw141292 2224e8c27ec8Sbaban /* Lookup well-known SIDs table */ 2225e8c27ec8Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid); 2226c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2227c5c4113dSnw141292 goto out; 2228c5c4113dSnw141292 2229e8c27ec8Sbaban /* Check if this is a localsid */ 2230e8c27ec8Sbaban if (!wksid) { 2231e8c27ec8Sbaban retcode = lookup_localsid2pid(req, res); 2232e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2233e8c27ec8Sbaban goto out; 2234e8c27ec8Sbaban } 2235e8c27ec8Sbaban 2236e8c27ec8Sbaban /* Lookup cache */ 2237479ac375Sdm199847 retcode = lookup_cache_sid2pid(state->cache, req, res); 2238c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2239c5c4113dSnw141292 goto out; 2240c5c4113dSnw141292 2241c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 224248258c6bSjp151216 retcode = IDMAP_ERR_NONEGENERATED; 2243c5c4113dSnw141292 goto out; 2244c5c4113dSnw141292 } 2245c5c4113dSnw141292 2246c5c4113dSnw141292 /* 2247e8c27ec8Sbaban * Failed to find non-expired entry in cache. Next step is 2248e8c27ec8Sbaban * to determine if this request needs to be batched for AD lookup. 2249e8c27ec8Sbaban * 2250e8c27ec8Sbaban * At this point we have either sid or winname or both. If we don't 2251e8c27ec8Sbaban * have both then lookup name_cache for the sid or winname 2252e8c27ec8Sbaban * whichever is missing. If not found then this request will be 2253e8c27ec8Sbaban * batched for AD lookup. 2254e8c27ec8Sbaban */ 2255479ac375Sdm199847 retcode = lookup_name_cache(state->cache, req, res); 2256e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS && retcode != IDMAP_ERR_NOTFOUND) 2257e8c27ec8Sbaban goto out; 2258e8c27ec8Sbaban 2259e8c27ec8Sbaban /* 2260e8c27ec8Sbaban * Set the flag to indicate that we are not done yet so that 2261e8c27ec8Sbaban * subsequent passes considers this request for name-based 2262e8c27ec8Sbaban * mapping and ephemeral mapping. 2263c5c4113dSnw141292 */ 2264c5c4113dSnw141292 state->sid2pid_done = FALSE; 2265e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 2266c5c4113dSnw141292 2267c5c4113dSnw141292 /* 2268e8c27ec8Sbaban * Even if we have both sid and winname, we still may need to batch 2269e8c27ec8Sbaban * this request for AD lookup if we don't have unixname and 2270e8c27ec8Sbaban * directory-based name mapping (AD or mixed) is enabled. 2271e8c27ec8Sbaban * We avoid AD lookup for well-known SIDs because they don't have 2272e8c27ec8Sbaban * regular AD objects. 2273c5c4113dSnw141292 */ 2274e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS || 2275e8c27ec8Sbaban (!wksid && req->id2name == NULL && 2276e8c27ec8Sbaban AD_OR_MIXED_MODE(res->id.idtype, state))) { 2277c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 2278e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 2279c5c4113dSnw141292 state->ad_nqueries++; 2280479ac375Sdm199847 } else if (NLDAP_MODE(res->id.idtype, state)) { 2281479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2282479ac375Sdm199847 state->nldap_nqueries++; 2283c5c4113dSnw141292 } 2284c5c4113dSnw141292 2285c5c4113dSnw141292 2286c5c4113dSnw141292 out: 2287c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2288e8c27ec8Sbaban /* 2289e8c27ec8Sbaban * If we are done and there was an error then set fallback pid 2290e8c27ec8Sbaban * in the result. 2291e8c27ec8Sbaban */ 2292e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 2293e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2294c5c4113dSnw141292 return (retcode); 2295c5c4113dSnw141292 } 2296c5c4113dSnw141292 2297c5c4113dSnw141292 /* 2298c5c4113dSnw141292 * Generate SID using the following convention 2299c5c4113dSnw141292 * <machine-sid-prefix>-<1000 + uid> 2300c5c4113dSnw141292 * <machine-sid-prefix>-<2^31 + gid> 2301c5c4113dSnw141292 */ 2302cd37da74Snw141292 static 2303cd37da74Snw141292 idmap_retcode 230448258c6bSjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user, 230548258c6bSjp151216 int fallback) 2306cd37da74Snw141292 { 2307e8c27ec8Sbaban free(res->id.idmap_id_u.sid.prefix); 2308e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix = NULL; 2309e8c27ec8Sbaban 2310e8c27ec8Sbaban /* 2311e8c27ec8Sbaban * Diagonal mapping for localSIDs not supported because of the 2312e8c27ec8Sbaban * way we generate localSIDs. 2313e8c27ec8Sbaban */ 2314e8c27ec8Sbaban if (is_user && res->id.idtype == IDMAP_GSID) 2315e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2316e8c27ec8Sbaban if (!is_user && res->id.idtype == IDMAP_USID) 2317e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2318e8c27ec8Sbaban 2319c5c4113dSnw141292 /* Skip 1000 UIDs */ 2320c5c4113dSnw141292 if (is_user && req->id1.idmap_id_u.uid > 2321c5c4113dSnw141292 (INT32_MAX - LOCALRID_MIN)) 232262c60062Sbaban return (IDMAP_ERR_NOMAPPING); 2323c5c4113dSnw141292 2324c5c4113dSnw141292 RDLOCK_CONFIG(); 2325e8c27ec8Sbaban /* 2326e8c27ec8Sbaban * machine_sid is never NULL because if it is we won't be here. 2327e8c27ec8Sbaban * No need to assert because stdrup(NULL) will core anyways. 2328e8c27ec8Sbaban */ 2329c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 2330c5c4113dSnw141292 strdup(_idmapdstate.cfg->pgcfg.machine_sid); 2331c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 2332c5c4113dSnw141292 UNLOCK_CONFIG(); 2333c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2334c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 2335c5c4113dSnw141292 } 2336c5c4113dSnw141292 UNLOCK_CONFIG(); 2337c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 2338c5c4113dSnw141292 (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_MIN : 2339c5c4113dSnw141292 req->id1.idmap_id_u.gid + INT32_MAX + 1; 2340651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 2341e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2342e8c27ec8Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 2343c5c4113dSnw141292 234448258c6bSjp151216 if (!fallback && req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 234548258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 234648258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 234748258c6bSjp151216 } 234848258c6bSjp151216 2349c5c4113dSnw141292 /* 2350c5c4113dSnw141292 * Don't update name_cache because local sids don't have 2351c5c4113dSnw141292 * valid windows names. 2352c5c4113dSnw141292 */ 2353e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 2354947c7bc0Sbaban return (IDMAP_SUCCESS); 2355c5c4113dSnw141292 } 2356c5c4113dSnw141292 2357cd37da74Snw141292 static 2358cd37da74Snw141292 idmap_retcode 2359cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res) 2360cd37da74Snw141292 { 2361c5c4113dSnw141292 char *sidprefix; 2362c5c4113dSnw141292 uint32_t rid; 2363c5c4113dSnw141292 int s; 2364c5c4113dSnw141292 2365c5c4113dSnw141292 /* 2366c5c4113dSnw141292 * If the sidprefix == localsid then UID = last RID - 1000 or 2367c5c4113dSnw141292 * GID = last RID - 2^31. 2368c5c4113dSnw141292 */ 2369e8c27ec8Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL) 2370e8c27ec8Sbaban /* This means we are looking up by winname */ 2371e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2372c5c4113dSnw141292 rid = req->id1.idmap_id_u.sid.rid; 2373c5c4113dSnw141292 2374c5c4113dSnw141292 RDLOCK_CONFIG(); 2375c5c4113dSnw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ? 2376cd37da74Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1; 2377c5c4113dSnw141292 UNLOCK_CONFIG(); 2378c5c4113dSnw141292 2379e8c27ec8Sbaban /* 2380e8c27ec8Sbaban * If the given sidprefix does not match machine_sid then this is 2381e8c27ec8Sbaban * not a local SID. 2382e8c27ec8Sbaban */ 2383e8c27ec8Sbaban if (s != 0) 2384e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2385e8c27ec8Sbaban 2386e8c27ec8Sbaban switch (res->id.idtype) { 2387c5c4113dSnw141292 case IDMAP_UID: 2388e8c27ec8Sbaban if (rid > INT32_MAX || rid < LOCALRID_MIN) 2389e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2390c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2391c5c4113dSnw141292 break; 2392c5c4113dSnw141292 case IDMAP_GID: 2393e8c27ec8Sbaban if (rid <= INT32_MAX) 2394e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2395c5c4113dSnw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2396c5c4113dSnw141292 break; 2397c5c4113dSnw141292 case IDMAP_POSIXID: 2398c5c4113dSnw141292 if (rid > INT32_MAX) { 2399cd37da74Snw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2400c5c4113dSnw141292 res->id.idtype = IDMAP_GID; 2401c5c4113dSnw141292 } else if (rid < LOCALRID_MIN) { 2402e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2403c5c4113dSnw141292 } else { 2404c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2405c5c4113dSnw141292 res->id.idtype = IDMAP_UID; 2406c5c4113dSnw141292 } 2407c5c4113dSnw141292 break; 2408c5c4113dSnw141292 default: 2409c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 2410c5c4113dSnw141292 } 241148258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 241248258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 241348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 241448258c6bSjp151216 } 2415c5c4113dSnw141292 return (IDMAP_SUCCESS); 2416c5c4113dSnw141292 } 2417c5c4113dSnw141292 2418e8c27ec8Sbaban /* 2419e8c27ec8Sbaban * Name service lookup by unixname to get pid 2420e8c27ec8Sbaban */ 2421cd37da74Snw141292 static 2422cd37da74Snw141292 idmap_retcode 2423e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id) 2424cd37da74Snw141292 { 2425cd37da74Snw141292 struct passwd pwd, *pwdp; 2426cd37da74Snw141292 struct group grp, *grpp; 2427c5c4113dSnw141292 char buf[1024]; 2428c5c4113dSnw141292 int errnum; 2429c5c4113dSnw141292 const char *me = "ns_lookup_byname"; 2430c5c4113dSnw141292 2431e8c27ec8Sbaban switch (id->idtype) { 2432e8c27ec8Sbaban case IDMAP_UID: 2433cd37da74Snw141292 pwdp = getpwnam_r(name, &pwd, buf, sizeof (buf)); 2434e8c27ec8Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL && 2435cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2436cd37da74Snw141292 pwdp = getpwnam_r(lower_name, &pwd, buf, sizeof (buf)); 2437cd37da74Snw141292 if (pwdp == NULL) { 2438c5c4113dSnw141292 errnum = errno; 2439c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2440c5c4113dSnw141292 "%s: getpwnam_r(%s) failed (%s).", 2441cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2442c5c4113dSnw141292 if (errnum == 0) 2443c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2444c5c4113dSnw141292 else 2445c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2446c5c4113dSnw141292 } 2447e8c27ec8Sbaban id->idmap_id_u.uid = pwd.pw_uid; 2448e8c27ec8Sbaban break; 2449e8c27ec8Sbaban case IDMAP_GID: 2450cd37da74Snw141292 grpp = getgrnam_r(name, &grp, buf, sizeof (buf)); 2451e8c27ec8Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL && 2452cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2453cd37da74Snw141292 grpp = getgrnam_r(lower_name, &grp, buf, sizeof (buf)); 2454cd37da74Snw141292 if (grpp == NULL) { 2455c5c4113dSnw141292 errnum = errno; 2456c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2457c5c4113dSnw141292 "%s: getgrnam_r(%s) failed (%s).", 2458cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2459c5c4113dSnw141292 if (errnum == 0) 2460c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2461c5c4113dSnw141292 else 2462c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2463c5c4113dSnw141292 } 2464e8c27ec8Sbaban id->idmap_id_u.gid = grp.gr_gid; 2465e8c27ec8Sbaban break; 2466e8c27ec8Sbaban default: 2467e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2468c5c4113dSnw141292 } 2469c5c4113dSnw141292 return (IDMAP_SUCCESS); 2470c5c4113dSnw141292 } 2471c5c4113dSnw141292 2472e8c27ec8Sbaban 2473e8c27ec8Sbaban /* 2474e8c27ec8Sbaban * Name service lookup by pid to get unixname 2475e8c27ec8Sbaban */ 2476e8c27ec8Sbaban static 2477e8c27ec8Sbaban idmap_retcode 2478e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname) 2479e8c27ec8Sbaban { 2480e8c27ec8Sbaban struct passwd pwd; 2481e8c27ec8Sbaban struct group grp; 2482e8c27ec8Sbaban char buf[1024]; 2483e8c27ec8Sbaban int errnum; 2484e8c27ec8Sbaban const char *me = "ns_lookup_bypid"; 2485e8c27ec8Sbaban 2486e8c27ec8Sbaban if (is_user) { 2487e8c27ec8Sbaban errno = 0; 2488e8c27ec8Sbaban if (getpwuid_r(pid, &pwd, buf, sizeof (buf)) == NULL) { 2489e8c27ec8Sbaban errnum = errno; 2490e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2491e8c27ec8Sbaban "%s: getpwuid_r(%u) failed (%s).", 2492e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2493e8c27ec8Sbaban if (errnum == 0) 2494e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2495e8c27ec8Sbaban else 2496e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2497e8c27ec8Sbaban } 2498e8c27ec8Sbaban *unixname = strdup(pwd.pw_name); 2499e8c27ec8Sbaban } else { 2500e8c27ec8Sbaban errno = 0; 2501e8c27ec8Sbaban if (getgrgid_r(pid, &grp, buf, sizeof (buf)) == NULL) { 2502e8c27ec8Sbaban errnum = errno; 2503e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2504e8c27ec8Sbaban "%s: getgrgid_r(%u) failed (%s).", 2505e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2506e8c27ec8Sbaban if (errnum == 0) 2507e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2508e8c27ec8Sbaban else 2509e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2510e8c27ec8Sbaban } 2511e8c27ec8Sbaban *unixname = strdup(grp.gr_name); 2512e8c27ec8Sbaban } 2513e8c27ec8Sbaban if (*unixname == NULL) 2514e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 2515e8c27ec8Sbaban return (IDMAP_SUCCESS); 2516e8c27ec8Sbaban } 2517e8c27ec8Sbaban 2518c5c4113dSnw141292 /* 2519c5c4113dSnw141292 * Name-based mapping 2520c5c4113dSnw141292 * 2521c5c4113dSnw141292 * Case 1: If no rule matches do ephemeral 2522c5c4113dSnw141292 * 2523c5c4113dSnw141292 * Case 2: If rule matches and unixname is "" then return no mapping. 2524c5c4113dSnw141292 * 2525c5c4113dSnw141292 * Case 3: If rule matches and unixname is specified then lookup name 2526c5c4113dSnw141292 * service using the unixname. If unixname not found then return no mapping. 2527c5c4113dSnw141292 * 2528c5c4113dSnw141292 * Case 4: If rule matches and unixname is * then lookup name service 2529c5c4113dSnw141292 * using winname as the unixname. If unixname not found then process 2530c5c4113dSnw141292 * other rules using the lookup order. If no other rule matches then do 2531c5c4113dSnw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4. 2532c5c4113dSnw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping 2533c5c4113dSnw141292 * instead of the default behaviour of doing ephemeral mapping. 2534c5c4113dSnw141292 * 2535c5c4113dSnw141292 * Example 1: 2536c5c4113dSnw141292 * *@sfbay == * 2537c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2538c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to an ephemeral id. 2539c5c4113dSnw141292 * 2540c5c4113dSnw141292 * Example 2: 2541c5c4113dSnw141292 * *@sfbay == * 2542c5c4113dSnw141292 * *@sfbay => guest 2543c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2544c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to guest. 2545c5c4113dSnw141292 * 2546c5c4113dSnw141292 * Example 3: 2547c5c4113dSnw141292 * *@sfbay == * 2548c5c4113dSnw141292 * *@sfbay => "" 2549c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2550c5c4113dSnw141292 * the name service then we will return no mapping for foo@sfbay. 2551c5c4113dSnw141292 * 2552c5c4113dSnw141292 */ 2553cd37da74Snw141292 static 2554cd37da74Snw141292 idmap_retcode 2555479ac375Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state, 2556479ac375Sdm199847 idmap_mapping *req, idmap_id_res *res) 2557cd37da74Snw141292 { 2558cd37da74Snw141292 const char *unixname, *windomain; 2559cd37da74Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL; 2560c5c4113dSnw141292 idmap_retcode retcode; 2561cd37da74Snw141292 char *end, *lower_unixname, *winname; 2562c5c4113dSnw141292 const char **values; 2563c5c4113dSnw141292 sqlite_vm *vm = NULL; 2564cd37da74Snw141292 int ncol, r, i, is_user, is_wuser; 256548258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 256648258c6bSjp151216 int direction; 2567c5c4113dSnw141292 const char *me = "name_based_mapping_sid2pid"; 2568c5c4113dSnw141292 2569e8c27ec8Sbaban assert(req->id1name != NULL); /* We have winname */ 2570e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have unixname */ 2571e8c27ec8Sbaban 25728e228215Sdm199847 winname = req->id1name; 25738e228215Sdm199847 windomain = req->id1domain; 2574cd37da74Snw141292 2575cd37da74Snw141292 switch (req->id1.idtype) { 2576cd37da74Snw141292 case IDMAP_USID: 2577cd37da74Snw141292 is_wuser = 1; 2578cd37da74Snw141292 break; 2579cd37da74Snw141292 case IDMAP_GSID: 2580cd37da74Snw141292 is_wuser = 0; 2581cd37da74Snw141292 break; 2582cd37da74Snw141292 default: 2583e8c27ec8Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the " 2584e8c27ec8Sbaban "given Windows id is user or group.", me); 2585cd37da74Snw141292 return (IDMAP_ERR_INTERNAL); 2586cd37da74Snw141292 } 2587cd37da74Snw141292 2588e8c27ec8Sbaban switch (res->id.idtype) { 2589cd37da74Snw141292 case IDMAP_UID: 2590cd37da74Snw141292 is_user = 1; 2591cd37da74Snw141292 break; 2592cd37da74Snw141292 case IDMAP_GID: 2593cd37da74Snw141292 is_user = 0; 2594cd37da74Snw141292 break; 2595cd37da74Snw141292 case IDMAP_POSIXID: 2596cd37da74Snw141292 is_user = is_wuser; 2597cd37da74Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID; 2598cd37da74Snw141292 break; 2599cd37da74Snw141292 } 2600c5c4113dSnw141292 2601c5c4113dSnw141292 i = 0; 2602479ac375Sdm199847 if (windomain == NULL) 260362c60062Sbaban windomain = ""; 260482da9f60Sbaban else if (state->defdom != NULL && 260582da9f60Sbaban strcasecmp(state->defdom, windomain) == 0) 2606c5c4113dSnw141292 i = 1; 2607c5c4113dSnw141292 2608cd37da74Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL) 2609cd37da74Snw141292 lower_winname = winname; /* hope for the best */ 2610c5c4113dSnw141292 sql = sqlite_mprintf( 261148258c6bSjp151216 "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 " 261248258c6bSjp151216 "FROM namerules WHERE " 2613cd37da74Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND " 2614c5c4113dSnw141292 "(winname = %Q OR winname = '*') AND " 2615c5c4113dSnw141292 "(windomain = %Q OR windomain = '*' %s) " 2616c5c4113dSnw141292 "ORDER BY w2u_order ASC;", 2617cd37da74Snw141292 is_user, is_wuser, lower_winname, windomain, 261862c60062Sbaban i ? "OR windomain ISNULL OR windomain = ''" : ""); 2619c5c4113dSnw141292 if (sql == NULL) { 2620c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2621c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 2622c5c4113dSnw141292 goto out; 2623c5c4113dSnw141292 } 2624c5c4113dSnw141292 2625479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 2626c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2627cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2628cd37da74Snw141292 CHECK_NULL(errmsg)); 2629c5c4113dSnw141292 sqlite_freemem(errmsg); 2630c5c4113dSnw141292 goto out; 2631c5c4113dSnw141292 } 2632c5c4113dSnw141292 263384decf41Sjp151216 for (;;) { 2634c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 263584decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 2636c5c4113dSnw141292 263784decf41Sjp151216 if (r == SQLITE_ROW) { 263848258c6bSjp151216 if (ncol < 5) { 2639c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2640c5c4113dSnw141292 goto out; 2641c5c4113dSnw141292 } 2642c5c4113dSnw141292 if (values[0] == NULL) { 2643c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2644c5c4113dSnw141292 goto out; 2645c5c4113dSnw141292 } 2646c5c4113dSnw141292 264748258c6bSjp151216 if (values[1] != NULL) 264848258c6bSjp151216 direction = 264948258c6bSjp151216 (strtol(values[1], &end, 10) == 0)? 265048258c6bSjp151216 IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 265148258c6bSjp151216 else 265248258c6bSjp151216 direction = IDMAP_DIRECTION_W2U; 265348258c6bSjp151216 2654c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 265548258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 265648258c6bSjp151216 values[0], is_wuser, is_user, 265748258c6bSjp151216 strtol(values[4], &end, 10), 265848258c6bSjp151216 direction); 2659c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2660c5c4113dSnw141292 goto out; 2661c5c4113dSnw141292 } 266248258c6bSjp151216 266348258c6bSjp151216 if (values[0][0] == '*') { 266448258c6bSjp151216 unixname = winname; 266548258c6bSjp151216 lower_unixname = lower_winname; 266648258c6bSjp151216 } else { 266748258c6bSjp151216 unixname = values[0]; 266848258c6bSjp151216 lower_unixname = NULL; 266948258c6bSjp151216 } 267048258c6bSjp151216 2671e8c27ec8Sbaban retcode = ns_lookup_byname(unixname, lower_unixname, 2672e8c27ec8Sbaban &res->id); 2673c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 267448258c6bSjp151216 if (values[0][0] == '*') 2675c5c4113dSnw141292 /* Case 4 */ 2676c5c4113dSnw141292 continue; 267748258c6bSjp151216 else { 2678c5c4113dSnw141292 /* Case 3 */ 267948258c6bSjp151216 idmap_namerule_set(rule, values[3], 268048258c6bSjp151216 values[2], values[0], is_wuser, 268148258c6bSjp151216 is_user, 268248258c6bSjp151216 strtol(values[4], &end, 10), 268348258c6bSjp151216 direction); 2684c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2685c5c4113dSnw141292 } 268648258c6bSjp151216 } 2687c5c4113dSnw141292 goto out; 2688c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 2689c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 2690c5c4113dSnw141292 goto out; 2691c5c4113dSnw141292 } else { 2692c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 2693c5c4113dSnw141292 vm = NULL; 2694cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2695cd37da74Snw141292 CHECK_NULL(errmsg)); 2696c5c4113dSnw141292 sqlite_freemem(errmsg); 2697c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2698c5c4113dSnw141292 goto out; 2699c5c4113dSnw141292 } 2700c5c4113dSnw141292 } 2701c5c4113dSnw141292 2702c5c4113dSnw141292 out: 270348258c6bSjp151216 if (sql != NULL) 2704c5c4113dSnw141292 sqlite_freemem(sql); 270548258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 2706c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 270762c60062Sbaban if (values[1] != NULL) 2708c5c4113dSnw141292 res->direction = 2709651c0131Sbaban (strtol(values[1], &end, 10) == 0)? 2710651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 2711c5c4113dSnw141292 else 2712651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 271348258c6bSjp151216 27148e228215Sdm199847 req->id2name = strdup(unixname); 2715479ac375Sdm199847 if (req->id2name == NULL) { 2716479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 2717479ac375Sdm199847 } 2718479ac375Sdm199847 } 2719479ac375Sdm199847 2720479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 272148258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 272248258c6bSjp151216 values[0], is_wuser, is_user, strtol(values[4], &end, 10), 272348258c6bSjp151216 res->direction); 272448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 2725c5c4113dSnw141292 } 2726479ac375Sdm199847 2727cd37da74Snw141292 if (lower_winname != NULL && lower_winname != winname) 2728cd37da74Snw141292 free(lower_winname); 272962c60062Sbaban if (vm != NULL) 2730c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 2731c5c4113dSnw141292 return (retcode); 2732c5c4113dSnw141292 } 2733c5c4113dSnw141292 2734c5c4113dSnw141292 static 2735c5c4113dSnw141292 int 2736c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid) 2737c5c4113dSnw141292 { 2738c5c4113dSnw141292 uid_t uid; 2739c5c4113dSnw141292 gid_t gid; 2740c5c4113dSnw141292 int err; 2741c5c4113dSnw141292 2742c5c4113dSnw141292 *next_uid = (uid_t)-1; 2743c5c4113dSnw141292 uid = _idmapdstate.next_uid++; 2744c5c4113dSnw141292 if (uid >= _idmapdstate.limit_uid) { 2745c5c4113dSnw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0) 2746c5c4113dSnw141292 return (err); 2747c5c4113dSnw141292 2748c5c4113dSnw141292 _idmapdstate.limit_uid = uid + 8192; 2749c5c4113dSnw141292 _idmapdstate.next_uid = uid; 2750c5c4113dSnw141292 } 2751c5c4113dSnw141292 *next_uid = uid; 2752c5c4113dSnw141292 2753c5c4113dSnw141292 return (0); 2754c5c4113dSnw141292 } 2755c5c4113dSnw141292 2756c5c4113dSnw141292 static 2757c5c4113dSnw141292 int 2758c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid) 2759c5c4113dSnw141292 { 2760c5c4113dSnw141292 uid_t uid; 2761c5c4113dSnw141292 gid_t gid; 2762c5c4113dSnw141292 int err; 2763c5c4113dSnw141292 2764c5c4113dSnw141292 *next_gid = (uid_t)-1; 2765c5c4113dSnw141292 gid = _idmapdstate.next_gid++; 2766c5c4113dSnw141292 if (gid >= _idmapdstate.limit_gid) { 2767c5c4113dSnw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0) 2768c5c4113dSnw141292 return (err); 2769c5c4113dSnw141292 2770c5c4113dSnw141292 _idmapdstate.limit_gid = gid + 8192; 2771c5c4113dSnw141292 _idmapdstate.next_gid = gid; 2772c5c4113dSnw141292 } 2773c5c4113dSnw141292 *next_gid = gid; 2774c5c4113dSnw141292 2775c5c4113dSnw141292 return (0); 2776c5c4113dSnw141292 } 2777c5c4113dSnw141292 277862c60062Sbaban static 277962c60062Sbaban int 2780cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize) 2781cd37da74Snw141292 { 278262c60062Sbaban uint_t hval, i, len; 278362c60062Sbaban 278462c60062Sbaban if (str == NULL) 278562c60062Sbaban return (0); 278662c60062Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) { 278762c60062Sbaban hval += str[i]; 278862c60062Sbaban hval += (hval << 10); 278962c60062Sbaban hval ^= (hval >> 6); 279062c60062Sbaban } 279162c60062Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) { 279262c60062Sbaban hval += str[i]; 279362c60062Sbaban hval += (hval << 10); 279462c60062Sbaban hval ^= (hval >> 6); 279562c60062Sbaban } 279662c60062Sbaban hval += (hval << 3); 279762c60062Sbaban hval ^= (hval >> 11); 279862c60062Sbaban hval += (hval << 15); 279962c60062Sbaban return (hval % htsize); 280062c60062Sbaban } 280162c60062Sbaban 280262c60062Sbaban static 280362c60062Sbaban int 280462c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid, 2805cd37da74Snw141292 uid_t *pid) 2806cd37da74Snw141292 { 280762c60062Sbaban uint_t next, key; 280862c60062Sbaban uint_t htsize = state->sid_history_size; 280962c60062Sbaban idmap_sid *sid; 281062c60062Sbaban 281162c60062Sbaban next = gethash(prefix, rid, htsize); 281262c60062Sbaban while (next != htsize) { 281362c60062Sbaban key = state->sid_history[next].key; 281462c60062Sbaban if (key == htsize) 281562c60062Sbaban return (0); 281662c60062Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1. 281762c60062Sbaban idmap_id_u.sid; 281862c60062Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) { 281962c60062Sbaban *pid = state->result->ids.ids_val[key].id. 282062c60062Sbaban idmap_id_u.uid; 282162c60062Sbaban return (1); 282262c60062Sbaban } 282362c60062Sbaban next = state->sid_history[next].next; 282462c60062Sbaban } 282562c60062Sbaban return (0); 282662c60062Sbaban } 282762c60062Sbaban 282862c60062Sbaban static 282962c60062Sbaban void 2830cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid) 2831cd37da74Snw141292 { 283262c60062Sbaban uint_t hash, next; 283362c60062Sbaban uint_t htsize = state->sid_history_size; 283462c60062Sbaban 283562c60062Sbaban hash = next = gethash(prefix, rid, htsize); 283662c60062Sbaban while (state->sid_history[next].key != htsize) { 283762c60062Sbaban next++; 283862c60062Sbaban next %= htsize; 283962c60062Sbaban } 284062c60062Sbaban state->sid_history[next].key = state->curpos; 284162c60062Sbaban if (hash == next) 284262c60062Sbaban return; 284362c60062Sbaban state->sid_history[next].next = state->sid_history[hash].next; 284462c60062Sbaban state->sid_history[hash].next = next; 284562c60062Sbaban } 2846c5c4113dSnw141292 2847e8c27ec8Sbaban void 2848e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state) 2849e8c27ec8Sbaban { 2850e8c27ec8Sbaban free(state->sid_history); 2851e8c27ec8Sbaban free(state->ad_unixuser_attr); 2852e8c27ec8Sbaban free(state->ad_unixgroup_attr); 2853479ac375Sdm199847 free(state->nldap_winname_attr); 2854479ac375Sdm199847 free(state->defdom); 2855e8c27ec8Sbaban } 2856e8c27ec8Sbaban 2857c5c4113dSnw141292 /* ARGSUSED */ 2858c5c4113dSnw141292 static 2859c5c4113dSnw141292 idmap_retcode 2860479ac375Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state, 2861cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2862cd37da74Snw141292 { 2863c5c4113dSnw141292 2864c5c4113dSnw141292 uid_t next_pid; 2865c5c4113dSnw141292 2866651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 286762c60062Sbaban 286848258c6bSjp151216 if (IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 286948258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 287048258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 287162c60062Sbaban return (IDMAP_SUCCESS); 287248258c6bSjp151216 } 287362c60062Sbaban 287462c60062Sbaban if (state->sid_history != NULL && 287562c60062Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix, 287662c60062Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) { 287762c60062Sbaban res->id.idmap_id_u.uid = next_pid; 287848258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 287948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 288062c60062Sbaban return (IDMAP_SUCCESS); 288162c60062Sbaban } 288262c60062Sbaban 288362c60062Sbaban if (res->id.idtype == IDMAP_UID) { 2884c5c4113dSnw141292 if (get_next_eph_uid(&next_pid) != 0) 2885c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2886c5c4113dSnw141292 res->id.idmap_id_u.uid = next_pid; 2887c5c4113dSnw141292 } else { 2888c5c4113dSnw141292 if (get_next_eph_gid(&next_pid) != 0) 2889c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2890c5c4113dSnw141292 res->id.idmap_id_u.gid = next_pid; 2891c5c4113dSnw141292 } 2892c5c4113dSnw141292 289348258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 289448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 289562c60062Sbaban if (state->sid_history != NULL) 289662c60062Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix, 289762c60062Sbaban req->id1.idmap_id_u.sid.rid); 289862c60062Sbaban 2899c5c4113dSnw141292 return (IDMAP_SUCCESS); 2900c5c4113dSnw141292 } 2901c5c4113dSnw141292 2902c5c4113dSnw141292 idmap_retcode 2903479ac375Sdm199847 sid2pid_second_pass(lookup_state_t *state, 2904cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2905cd37da74Snw141292 { 2906c5c4113dSnw141292 idmap_retcode retcode; 2907c5c4113dSnw141292 2908c5c4113dSnw141292 /* Check if second pass is needed */ 2909e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2910c5c4113dSnw141292 return (res->retcode); 2911c5c4113dSnw141292 2912c5c4113dSnw141292 /* Get status from previous pass */ 2913e8c27ec8Sbaban retcode = res->retcode; 2914*4aa0a5e7Snw141292 if (retcode != IDMAP_SUCCESS && state->eph_map_unres_sids && 2915*4aa0a5e7Snw141292 !EMPTY_STRING(req->id1.idmap_id_u.sid.prefix) && 2916*4aa0a5e7Snw141292 EMPTY_STRING(req->id1name)) { 2917*4aa0a5e7Snw141292 /* 2918*4aa0a5e7Snw141292 * We are asked to map an unresolvable SID to a UID or 2919*4aa0a5e7Snw141292 * GID, but, which? We'll treat all unresolvable SIDs 2920*4aa0a5e7Snw141292 * as users unless the caller specified which of a UID 2921*4aa0a5e7Snw141292 * or GID they want. 2922*4aa0a5e7Snw141292 */ 2923*4aa0a5e7Snw141292 if (res->id.idtype == IDMAP_POSIXID) 2924*4aa0a5e7Snw141292 res->id.idtype = IDMAP_UID; 2925*4aa0a5e7Snw141292 goto do_eph; 2926*4aa0a5e7Snw141292 } 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 3024*4aa0a5e7Snw141292 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