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 1080479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 1081479ac375Sdm199847 state->defdom = 1082479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.default_domain); 1083479ac375Sdm199847 if (state->defdom == NULL) { 1084479ac375Sdm199847 UNLOCK_CONFIG(); 1085479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1086479ac375Sdm199847 } 1087479ac375Sdm199847 } else { 1088479ac375Sdm199847 UNLOCK_CONFIG(); 1089dc03a638Sdm199847 return (IDMAP_SUCCESS); 1090479ac375Sdm199847 } 1091e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ds_name_mapping_enabled == FALSE) { 1092e8c27ec8Sbaban UNLOCK_CONFIG(); 1093e8c27ec8Sbaban return (IDMAP_SUCCESS); 1094e8c27ec8Sbaban } 1095e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1096e8c27ec8Sbaban state->nm_siduid = 1097e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1098e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1099e8c27ec8Sbaban state->nm_sidgid = 1100e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1101e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1102e8c27ec8Sbaban } else { 1103e8c27ec8Sbaban state->nm_siduid = 1104e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1105e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1106e8c27ec8Sbaban state->nm_sidgid = 1107e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1108e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1109e8c27ec8Sbaban } 1110e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) { 1111e8c27ec8Sbaban state->ad_unixuser_attr = 1112e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr); 1113e8c27ec8Sbaban if (state->ad_unixuser_attr == NULL) { 1114e8c27ec8Sbaban UNLOCK_CONFIG(); 1115e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1116e8c27ec8Sbaban } 1117e8c27ec8Sbaban } 1118e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) { 1119e8c27ec8Sbaban state->ad_unixgroup_attr = 1120e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr); 1121e8c27ec8Sbaban if (state->ad_unixgroup_attr == NULL) { 1122e8c27ec8Sbaban UNLOCK_CONFIG(); 1123e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1124e8c27ec8Sbaban } 1125e8c27ec8Sbaban } 1126479ac375Sdm199847 if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1127479ac375Sdm199847 state->nldap_winname_attr = 1128479ac375Sdm199847 strdup(_idmapdstate.cfg->pgcfg.nldap_winname_attr); 1129479ac375Sdm199847 if (state->nldap_winname_attr == NULL) { 1130479ac375Sdm199847 UNLOCK_CONFIG(); 1131479ac375Sdm199847 return (IDMAP_ERR_MEMORY); 1132479ac375Sdm199847 } 1133479ac375Sdm199847 } 1134e8c27ec8Sbaban UNLOCK_CONFIG(); 1135e8c27ec8Sbaban return (IDMAP_SUCCESS); 1136e8c27ec8Sbaban } 1137e8c27ec8Sbaban 1138e8c27ec8Sbaban /* 113948258c6bSjp151216 * Set the rule with sepecified values. 114048258c6bSjp151216 * All the strings are copied. 114148258c6bSjp151216 */ 114248258c6bSjp151216 static void 114348258c6bSjp151216 idmap_namerule_set(idmap_namerule *rule, const char *windomain, 114448258c6bSjp151216 const char *winname, const char *unixname, boolean_t is_user, 114548258c6bSjp151216 boolean_t is_wuser, boolean_t is_nt4, int direction) 114648258c6bSjp151216 { 114748258c6bSjp151216 /* 114848258c6bSjp151216 * Only update if they differ because we have to free 114948258c6bSjp151216 * and duplicate the strings 115048258c6bSjp151216 */ 115148258c6bSjp151216 if (rule->windomain == NULL || windomain == NULL || 115248258c6bSjp151216 strcmp(rule->windomain, windomain) != 0) { 115348258c6bSjp151216 if (rule->windomain != NULL) { 115448258c6bSjp151216 free(rule->windomain); 115548258c6bSjp151216 rule->windomain = NULL; 115648258c6bSjp151216 } 115748258c6bSjp151216 if (windomain != NULL) 115848258c6bSjp151216 rule->windomain = strdup(windomain); 115948258c6bSjp151216 } 116048258c6bSjp151216 116148258c6bSjp151216 if (rule->winname == NULL || winname == NULL || 116248258c6bSjp151216 strcmp(rule->winname, winname) != 0) { 116348258c6bSjp151216 if (rule->winname != NULL) { 116448258c6bSjp151216 free(rule->winname); 116548258c6bSjp151216 rule->winname = NULL; 116648258c6bSjp151216 } 116748258c6bSjp151216 if (winname != NULL) 116848258c6bSjp151216 rule->winname = strdup(winname); 116948258c6bSjp151216 } 117048258c6bSjp151216 117148258c6bSjp151216 if (rule->unixname == NULL || unixname == NULL || 117248258c6bSjp151216 strcmp(rule->unixname, unixname) != 0) { 117348258c6bSjp151216 if (rule->unixname != NULL) { 117448258c6bSjp151216 free(rule->unixname); 117548258c6bSjp151216 rule->unixname = NULL; 117648258c6bSjp151216 } 117748258c6bSjp151216 if (unixname != NULL) 117848258c6bSjp151216 rule->unixname = strdup(unixname); 117948258c6bSjp151216 } 118048258c6bSjp151216 118148258c6bSjp151216 rule->is_user = is_user; 118248258c6bSjp151216 rule->is_wuser = is_wuser; 118348258c6bSjp151216 rule->is_nt4 = is_nt4; 118448258c6bSjp151216 rule->direction = direction; 118548258c6bSjp151216 } 118648258c6bSjp151216 118748258c6bSjp151216 118848258c6bSjp151216 /* 118962c60062Sbaban * Table for well-known SIDs. 119062c60062Sbaban * 119162c60062Sbaban * Background: 119262c60062Sbaban * 119376b27f93Sbaban * Some of the well-known principals are stored under: 119462c60062Sbaban * cn=WellKnown Security Principals, cn=Configuration, dc=<forestRootDomain> 119562c60062Sbaban * They belong to objectClass "foreignSecurityPrincipal". They don't have 119662c60062Sbaban * "samAccountName" nor "userPrincipalName" attributes. Their names are 119762c60062Sbaban * available in "cn" and "name" attributes. Some of these principals have a 119862c60062Sbaban * second entry under CN=ForeignSecurityPrincipals,dc=<forestRootDomain> and 119962c60062Sbaban * these duplicate entries have the stringified SID in the "name" and "cn" 120062c60062Sbaban * attributes instead of the actual name. 120162c60062Sbaban * 120276b27f93Sbaban * Those of the form S-1-5-32-X are Builtin groups and are stored in the 120376b27f93Sbaban * cn=builtin container (except, Power Users which is not stored in AD) 120462c60062Sbaban * 120576b27f93Sbaban * These principals are and will remain constant. Therefore doing AD lookups 120676b27f93Sbaban * provides no benefit. Also, using hard-coded table (and thus avoiding AD 120776b27f93Sbaban * lookup) improves performance and avoids additional complexity in the 120876b27f93Sbaban * adutils.c code. Moreover these SIDs can be used when no Active Directory 120976b27f93Sbaban * is available (such as the CIFS server's "workgroup" mode). 121076b27f93Sbaban * 121176b27f93Sbaban * Notes: 121276b27f93Sbaban * 1. Currently we don't support localization of well-known SID names, 121362c60062Sbaban * unlike Windows. 121462c60062Sbaban * 121576b27f93Sbaban * 2. Other well-known SIDs i.e. S-1-5-<domain>-<w-k RID> are not stored 121676b27f93Sbaban * here. AD does have normal user/group objects for these objects and 121776b27f93Sbaban * can be looked up using the existing AD lookup code. 1218e8c27ec8Sbaban * 1219e8c27ec8Sbaban * 3. See comments above lookup_wksids_sid2pid() for more information 1220e8c27ec8Sbaban * on how we lookup the wksids table. 122162c60062Sbaban */ 122262c60062Sbaban static wksids_table_t wksids[] = { 1223e8c27ec8Sbaban {"S-1-0", 0, "Nobody", 0, SENTINEL_PID, -1, 1}, 1224e8c27ec8Sbaban {"S-1-1", 0, "Everyone", 0, SENTINEL_PID, -1, -1}, 1225e8c27ec8Sbaban {"S-1-3", 0, "Creator Owner", 1, IDMAP_WK_CREATOR_OWNER_UID, 1, 0}, 1226e8c27ec8Sbaban {"S-1-3", 1, "Creator Group", 0, IDMAP_WK_CREATOR_GROUP_GID, 0, 0}, 1227e8c27ec8Sbaban {"S-1-3", 2, "Creator Owner Server", 1, SENTINEL_PID, -1, -1}, 1228e8c27ec8Sbaban {"S-1-3", 3, "Creator Group Server", 0, SENTINEL_PID, -1, 1}, 1229e8c27ec8Sbaban {"S-1-3", 4, "Owner Rights", 0, SENTINEL_PID, -1, -1}, 1230e8c27ec8Sbaban {"S-1-5", 1, "Dialup", 0, SENTINEL_PID, -1, -1}, 1231e8c27ec8Sbaban {"S-1-5", 2, "Network", 0, SENTINEL_PID, -1, -1}, 1232e8c27ec8Sbaban {"S-1-5", 3, "Batch", 0, SENTINEL_PID, -1, -1}, 1233e8c27ec8Sbaban {"S-1-5", 4, "Interactive", 0, SENTINEL_PID, -1, -1}, 1234e8c27ec8Sbaban {"S-1-5", 6, "Service", 0, SENTINEL_PID, -1, -1}, 1235e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, GID_NOBODY, 0, 0}, 1236e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, UID_NOBODY, 1, 0}, 1237e8c27ec8Sbaban {"S-1-5", 8, "Proxy", 0, SENTINEL_PID, -1, -1}, 1238e8c27ec8Sbaban {"S-1-5", 9, "Enterprise Domain Controllers", 0, SENTINEL_PID, -1, -1}, 1239e8c27ec8Sbaban {"S-1-5", 10, "Self", 0, SENTINEL_PID, -1, -1}, 1240e8c27ec8Sbaban {"S-1-5", 11, "Authenticated Users", 0, SENTINEL_PID, -1, -1}, 1241e8c27ec8Sbaban {"S-1-5", 12, "Restricted Code", 0, SENTINEL_PID, -1, -1}, 1242e8c27ec8Sbaban {"S-1-5", 13, "Terminal Server User", 0, SENTINEL_PID, -1, -1}, 1243e8c27ec8Sbaban {"S-1-5", 14, "Remote Interactive Logon", 0, SENTINEL_PID, -1, -1}, 1244e8c27ec8Sbaban {"S-1-5", 15, "This Organization", 0, SENTINEL_PID, -1, -1}, 1245e8c27ec8Sbaban {"S-1-5", 17, "IUSR", 0, SENTINEL_PID, -1, -1}, 1246e8c27ec8Sbaban {"S-1-5", 18, "Local System", 0, IDMAP_WK_LOCAL_SYSTEM_GID, 0, 0}, 1247e8c27ec8Sbaban {"S-1-5", 19, "Local Service", 0, SENTINEL_PID, -1, -1}, 1248e8c27ec8Sbaban {"S-1-5", 20, "Network Service", 0, SENTINEL_PID, -1, -1}, 1249e8c27ec8Sbaban {"S-1-5", 1000, "Other Organization", 0, SENTINEL_PID, -1, -1}, 1250e8c27ec8Sbaban {"S-1-5-32", 544, "Administrators", 0, SENTINEL_PID, -1, -1}, 1251e8c27ec8Sbaban {"S-1-5-32", 545, "Users", 0, SENTINEL_PID, -1, -1}, 1252e8c27ec8Sbaban {"S-1-5-32", 546, "Guests", 0, SENTINEL_PID, -1, -1}, 1253e8c27ec8Sbaban {"S-1-5-32", 547, "Power Users", 0, SENTINEL_PID, -1, -1}, 1254e8c27ec8Sbaban {"S-1-5-32", 548, "Account Operators", 0, SENTINEL_PID, -1, -1}, 1255e8c27ec8Sbaban {"S-1-5-32", 549, "Server Operators", 0, SENTINEL_PID, -1, -1}, 1256e8c27ec8Sbaban {"S-1-5-32", 550, "Print Operators", 0, SENTINEL_PID, -1, -1}, 1257e8c27ec8Sbaban {"S-1-5-32", 551, "Backup Operators", 0, SENTINEL_PID, -1, -1}, 1258e8c27ec8Sbaban {"S-1-5-32", 552, "Replicator", 0, SENTINEL_PID, -1, -1}, 125976b27f93Sbaban {"S-1-5-32", 554, "Pre-Windows 2000 Compatible Access", 0, 1260e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1261e8c27ec8Sbaban {"S-1-5-32", 555, "Remote Desktop Users", 0, SENTINEL_PID, -1, -1}, 126276b27f93Sbaban {"S-1-5-32", 556, "Network Configuration Operators", 0, 1263e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 126476b27f93Sbaban {"S-1-5-32", 557, "Incoming Forest Trust Builders", 0, 1265e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1266e8c27ec8Sbaban {"S-1-5-32", 558, "Performance Monitor Users", 0, SENTINEL_PID, -1, -1}, 1267e8c27ec8Sbaban {"S-1-5-32", 559, "Performance Log Users", 0, SENTINEL_PID, -1, -1}, 126876b27f93Sbaban {"S-1-5-32", 560, "Windows Authorization Access Group", 0, 1269e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 127076b27f93Sbaban {"S-1-5-32", 561, "Terminal Server License Servers", 0, 1271e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1272e8c27ec8Sbaban {"S-1-5-32", 561, "Distributed COM Users", 0, SENTINEL_PID, -1, -1}, 1273e8c27ec8Sbaban {"S-1-5-32", 568, "IIS_IUSRS", 0, SENTINEL_PID, -1, -1}, 1274e8c27ec8Sbaban {"S-1-5-32", 569, "Cryptographic Operators", 0, SENTINEL_PID, -1, -1}, 1275e8c27ec8Sbaban {"S-1-5-32", 573, "Event Log Readers", 0, SENTINEL_PID, -1, -1}, 127676b27f93Sbaban {"S-1-5-32", 574, "Certificate Service DCOM Access", 0, 1277e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1278e8c27ec8Sbaban {"S-1-5-64", 21, "Digest Authentication", 0, SENTINEL_PID, -1, -1}, 1279e8c27ec8Sbaban {"S-1-5-64", 10, "NTLM Authentication", 0, SENTINEL_PID, -1, -1}, 1280e8c27ec8Sbaban {"S-1-5-64", 14, "SChannel Authentication", 0, SENTINEL_PID, -1, -1}, 1281e8c27ec8Sbaban {NULL, UINT32_MAX, NULL, -1, SENTINEL_PID, -1, -1} 1282c5c4113dSnw141292 }; 1283c5c4113dSnw141292 1284e8c27ec8Sbaban /* 1285e8c27ec8Sbaban * Lookup well-known SIDs table either by winname or by SID. 1286e8c27ec8Sbaban * If the given winname or SID is a well-known SID then we set wksid 1287e8c27ec8Sbaban * variable and then proceed to see if the SID has a hard mapping to 1288e8c27ec8Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to 1289e8c27ec8Sbaban * fixed ephemeral ids). If we find such mapping then we return 1290e8c27ec8Sbaban * success otherwise notfound. If a well-known SID is mapped to 1291e8c27ec8Sbaban * SENTINEL_PID and the direction field is set (bi-directional or 1292e8c27ec8Sbaban * win2unix) then we treat it as inhibited mapping and return no 1293e8c27ec8Sbaban * mapping (Ex. S-1-0-0). 1294e8c27ec8Sbaban */ 1295cd37da74Snw141292 static 1296cd37da74Snw141292 idmap_retcode 1297e8c27ec8Sbaban lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *wksid) 1298cd37da74Snw141292 { 1299c5c4113dSnw141292 int i; 130062c60062Sbaban 1301e8c27ec8Sbaban *wksid = 0; 1302e8c27ec8Sbaban 1303e8c27ec8Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1304e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1305e8c27ec8Sbaban if ((strcasecmp(wksids[i].sidprefix, 1306e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix) != 0) || 1307e8c27ec8Sbaban wksids[i].rid != req->id1.idmap_id_u.sid.rid) 1308e8c27ec8Sbaban /* this is not our SID */ 1309e8c27ec8Sbaban continue; 1310e8c27ec8Sbaban if (req->id1name == NULL) { 1311e8c27ec8Sbaban req->id1name = strdup(wksids[i].winname); 1312e8c27ec8Sbaban if (req->id1name == NULL) 1313e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1314e8c27ec8Sbaban } 1315e8c27ec8Sbaban } else if (req->id1name != NULL) { 1316e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, req->id1name) != 0) 1317e8c27ec8Sbaban /* this is not our winname */ 1318e8c27ec8Sbaban continue; 1319e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = 1320e8c27ec8Sbaban strdup(wksids[i].sidprefix); 1321e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) 1322e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1323e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = wksids[i].rid; 1324e8c27ec8Sbaban } 1325e8c27ec8Sbaban 1326e8c27ec8Sbaban *wksid = 1; 1327e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1328e8c27ec8Sbaban 1329e8c27ec8Sbaban req->id1.idtype = (wksids[i].is_wuser) ? 1330e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1331e8c27ec8Sbaban 1332e8c27ec8Sbaban if (wksids[i].pid == SENTINEL_PID) { 1333e8c27ec8Sbaban if (wksids[i].direction == IDMAP_DIRECTION_BI || 1334e8c27ec8Sbaban wksids[i].direction == IDMAP_DIRECTION_W2U) 1335e8c27ec8Sbaban /* Inhibited */ 1336e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 1337e8c27ec8Sbaban /* Not mapped */ 1338479ac375Sdm199847 if (res->id.idtype == IDMAP_POSIXID) { 1339479ac375Sdm199847 res->id.idtype = 1340479ac375Sdm199847 (wksids[i].is_wuser) ? 1341479ac375Sdm199847 IDMAP_UID : IDMAP_GID; 1342479ac375Sdm199847 } 1343e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 1344e8c27ec8Sbaban } else if (wksids[i].direction == IDMAP_DIRECTION_U2W) 134562c60062Sbaban continue; 134662c60062Sbaban 1347e8c27ec8Sbaban switch (res->id.idtype) { 1348c5c4113dSnw141292 case IDMAP_UID: 134962c60062Sbaban if (wksids[i].is_user == 0) 135062c60062Sbaban continue; 135162c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 135262c60062Sbaban res->direction = wksids[i].direction; 135348258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 135448258c6bSjp151216 res->info.how.map_type = 135548258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 135648258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 135748258c6bSjp151216 } 1358c5c4113dSnw141292 return (IDMAP_SUCCESS); 1359c5c4113dSnw141292 case IDMAP_GID: 136062c60062Sbaban if (wksids[i].is_user == 1) 136162c60062Sbaban continue; 136262c60062Sbaban res->id.idmap_id_u.gid = wksids[i].pid; 136362c60062Sbaban res->direction = wksids[i].direction; 136448258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 136548258c6bSjp151216 res->info.how.map_type = 136648258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 136748258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 136848258c6bSjp151216 } 1369c5c4113dSnw141292 return (IDMAP_SUCCESS); 1370c5c4113dSnw141292 case IDMAP_POSIXID: 137162c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 137262c60062Sbaban res->id.idtype = (!wksids[i].is_user) ? 1373c5c4113dSnw141292 IDMAP_GID : IDMAP_UID; 137462c60062Sbaban res->direction = wksids[i].direction; 137548258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 137648258c6bSjp151216 res->info.how.map_type = 137748258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 137848258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 137948258c6bSjp151216 } 1380c5c4113dSnw141292 return (IDMAP_SUCCESS); 1381c5c4113dSnw141292 default: 1382c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 1383c5c4113dSnw141292 } 1384c5c4113dSnw141292 } 1385c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1386c5c4113dSnw141292 } 1387c5c4113dSnw141292 1388cd37da74Snw141292 1389cd37da74Snw141292 static 1390cd37da74Snw141292 idmap_retcode 1391cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user) 1392cd37da74Snw141292 { 1393c5c4113dSnw141292 int i; 1394e8c27ec8Sbaban if (req->id1.idmap_id_u.uid == SENTINEL_PID) 1395e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 139662c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 139762c60062Sbaban if (wksids[i].pid == req->id1.idmap_id_u.uid && 139862c60062Sbaban wksids[i].is_user == is_user && 139962c60062Sbaban wksids[i].direction != IDMAP_DIRECTION_W2U) { 1400e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) { 1401e8c27ec8Sbaban res->id.idtype = (wksids[i].is_wuser) ? 1402e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1403e8c27ec8Sbaban } 140462c60062Sbaban res->id.idmap_id_u.sid.rid = wksids[i].rid; 1405c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 140662c60062Sbaban strdup(wksids[i].sidprefix); 1407c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 1408c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1409c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 1410c5c4113dSnw141292 } 141162c60062Sbaban res->direction = wksids[i].direction; 141248258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 141348258c6bSjp151216 res->info.how.map_type = 141448258c6bSjp151216 IDMAP_MAP_TYPE_KNOWN_SID; 141548258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_HARD_CODED; 141648258c6bSjp151216 } 1417c5c4113dSnw141292 return (IDMAP_SUCCESS); 1418c5c4113dSnw141292 } 1419c5c4113dSnw141292 } 142062c60062Sbaban return (IDMAP_ERR_NOTFOUND); 142162c60062Sbaban } 142262c60062Sbaban 1423cd37da74Snw141292 idmap_retcode 1424cd37da74Snw141292 lookup_wksids_name2sid(const char *name, char **canonname, char **sidprefix, 1425cd37da74Snw141292 idmap_rid_t *rid, int *type) 1426cd37da74Snw141292 { 142762c60062Sbaban int i; 1428479ac375Sdm199847 1429479ac375Sdm199847 if ((strncasecmp(name, "BUILTIN\\", 8) == 0) || 1430479ac375Sdm199847 (strncasecmp(name, "BUILTIN/", 8) == 0)) 1431479ac375Sdm199847 name += 8; 1432479ac375Sdm199847 143362c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1434e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, name) != 0) 1435e8c27ec8Sbaban continue; 1436e8c27ec8Sbaban if (sidprefix != NULL && 1437e8c27ec8Sbaban (*sidprefix = strdup(wksids[i].sidprefix)) == NULL) { 143862c60062Sbaban idmapdlog(LOG_ERR, "Out of memory"); 143962c60062Sbaban return (IDMAP_ERR_MEMORY); 144062c60062Sbaban } 1441cd37da74Snw141292 if (canonname != NULL && 1442cd37da74Snw141292 (*canonname = strdup(wksids[i].winname)) == NULL) { 1443cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1444e8c27ec8Sbaban if (sidprefix != NULL) { 1445e8c27ec8Sbaban free(*sidprefix); 1446e8c27ec8Sbaban *sidprefix = NULL; 1447e8c27ec8Sbaban } 1448cd37da74Snw141292 return (IDMAP_ERR_MEMORY); 1449cd37da74Snw141292 } 145062c60062Sbaban if (type != NULL) 1451e8c27ec8Sbaban *type = (wksids[i].is_wuser) ? 145262c60062Sbaban _IDMAP_T_USER : _IDMAP_T_GROUP; 145362c60062Sbaban if (rid != NULL) 145462c60062Sbaban *rid = wksids[i].rid; 145562c60062Sbaban return (IDMAP_SUCCESS); 145662c60062Sbaban } 1457c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1458c5c4113dSnw141292 } 1459c5c4113dSnw141292 1460cd37da74Snw141292 static 1461cd37da74Snw141292 idmap_retcode 1462cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1463cd37da74Snw141292 { 1464c5c4113dSnw141292 char *end; 1465c5c4113dSnw141292 char *sql = NULL; 1466c5c4113dSnw141292 const char **values; 1467c5c4113dSnw141292 sqlite_vm *vm = NULL; 1468c5c4113dSnw141292 int ncol, is_user; 1469c5c4113dSnw141292 uid_t pid; 1470c5c4113dSnw141292 time_t curtime, exp; 1471c5c4113dSnw141292 idmap_retcode retcode; 1472042addd6Sbaban char *is_user_string, *lower_name; 1473c5c4113dSnw141292 1474c5c4113dSnw141292 /* Current time */ 1475c5c4113dSnw141292 errno = 0; 1476c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1477cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1478c5c4113dSnw141292 strerror(errno)); 1479c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1480c5c4113dSnw141292 goto out; 1481c5c4113dSnw141292 } 1482c5c4113dSnw141292 1483e8c27ec8Sbaban switch (res->id.idtype) { 1484cd37da74Snw141292 case IDMAP_UID: 1485cd37da74Snw141292 is_user_string = "1"; 1486cd37da74Snw141292 break; 1487cd37da74Snw141292 case IDMAP_GID: 1488cd37da74Snw141292 is_user_string = "0"; 1489cd37da74Snw141292 break; 1490cd37da74Snw141292 case IDMAP_POSIXID: 1491cd37da74Snw141292 /* the non-diagonal mapping */ 1492cd37da74Snw141292 is_user_string = "is_wuser"; 1493cd37da74Snw141292 break; 1494cd37da74Snw141292 default: 1495cd37da74Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 1496cd37da74Snw141292 goto out; 1497cd37da74Snw141292 } 1498cd37da74Snw141292 1499c5c4113dSnw141292 /* SQL to lookup the cache */ 150048258c6bSjp151216 1501e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1502e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 150348258c6bSjp151216 "unixname, u2w, is_wuser, " 150448258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 150548258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1506cd37da74Snw141292 "FROM idmap_cache WHERE is_user = %s AND " 1507c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 1508c5c4113dSnw141292 "(pid >= 2147483648 OR " 1509c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1510c5c4113dSnw141292 "expiration > %d));", 1511e8c27ec8Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix, 1512e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, curtime); 1513e8c27ec8Sbaban } else if (req->id1name != NULL) { 1514042addd6Sbaban if ((lower_name = tolower_u8(req->id1name)) == NULL) 1515042addd6Sbaban lower_name = req->id1name; 1516e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 151748258c6bSjp151216 "unixname, u2w, is_wuser, " 151848258c6bSjp151216 "map_type, map_dn, map_attr, map_value, " 151948258c6bSjp151216 "map_windomain, map_winname, map_unixname, map_is_nt4 " 1520e8c27ec8Sbaban "FROM idmap_cache WHERE is_user = %s AND " 1521e8c27ec8Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND " 1522e8c27ec8Sbaban "(pid >= 2147483648 OR " 1523e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 1524e8c27ec8Sbaban "expiration > %d));", 152548258c6bSjp151216 is_user_string, lower_name, req->id1domain, 152648258c6bSjp151216 curtime); 1527042addd6Sbaban if (lower_name != req->id1name) 1528042addd6Sbaban free(lower_name); 1529e8c27ec8Sbaban } else { 1530e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 1531e8c27ec8Sbaban goto out; 1532e8c27ec8Sbaban } 1533c5c4113dSnw141292 if (sql == NULL) { 1534c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1535c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1536c5c4113dSnw141292 goto out; 1537c5c4113dSnw141292 } 153848258c6bSjp151216 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 153948258c6bSjp151216 14, &values); 1540c5c4113dSnw141292 sqlite_freemem(sql); 1541c5c4113dSnw141292 1542c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 1543c5c4113dSnw141292 goto out; 1544c5c4113dSnw141292 } else if (retcode == IDMAP_SUCCESS) { 1545c5c4113dSnw141292 /* sanity checks */ 1546c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 1547c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1548c5c4113dSnw141292 goto out; 1549c5c4113dSnw141292 } 1550c5c4113dSnw141292 1551c5c4113dSnw141292 pid = strtoul(values[0], &end, 10); 1552c5c4113dSnw141292 is_user = strncmp(values[1], "0", 2) ? 1 : 0; 1553c5c4113dSnw141292 1554cd37da74Snw141292 if (is_user) { 1555cd37da74Snw141292 res->id.idtype = IDMAP_UID; 1556cd37da74Snw141292 res->id.idmap_id_u.uid = pid; 1557cd37da74Snw141292 } else { 1558cd37da74Snw141292 res->id.idtype = IDMAP_GID; 1559cd37da74Snw141292 res->id.idmap_id_u.gid = pid; 1560cd37da74Snw141292 } 1561cd37da74Snw141292 1562c5c4113dSnw141292 /* 1563c5c4113dSnw141292 * We may have an expired ephemeral mapping. Consider 1564c5c4113dSnw141292 * the expired entry as valid if we are not going to 1565c5c4113dSnw141292 * perform name-based mapping. But do not renew the 1566c5c4113dSnw141292 * expiration. 1567c5c4113dSnw141292 * If we will be doing name-based mapping then store the 1568c5c4113dSnw141292 * ephemeral pid in the result so that we can use it 1569c5c4113dSnw141292 * if we end up doing dynamic mapping again. 1570c5c4113dSnw141292 */ 1571c5c4113dSnw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) && 1572cd37da74Snw141292 !AVOID_NAMESERVICE(req) && 1573cd37da74Snw141292 IS_EPHEMERAL(pid) && values[2] != NULL) { 1574c5c4113dSnw141292 exp = strtoll(values[2], &end, 10); 1575c5c4113dSnw141292 if (exp && exp <= curtime) { 1576c5c4113dSnw141292 /* Store the ephemeral pid */ 1577651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 1578cd37da74Snw141292 req->direction |= is_user 1579cd37da74Snw141292 ? _IDMAP_F_EXP_EPH_UID 1580cd37da74Snw141292 : _IDMAP_F_EXP_EPH_GID; 1581c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 1582c5c4113dSnw141292 } 1583c5c4113dSnw141292 } 1584c5c4113dSnw141292 } 1585c5c4113dSnw141292 1586c5c4113dSnw141292 out: 1587c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 158862c60062Sbaban if (values[4] != NULL) 1589c5c4113dSnw141292 res->direction = 1590651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 1591651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 1592c5c4113dSnw141292 else 1593651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 1594c5c4113dSnw141292 159562c60062Sbaban if (values[3] != NULL) { 1596e8c27ec8Sbaban if (req->id2name != NULL) 1597e8c27ec8Sbaban free(req->id2name); 15988e228215Sdm199847 req->id2name = strdup(values[3]); 15998e228215Sdm199847 if (req->id2name == NULL) { 1600c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1601c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1602c5c4113dSnw141292 } 1603c5c4113dSnw141292 } 1604e8c27ec8Sbaban 1605e8c27ec8Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ? 1606e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 160748258c6bSjp151216 160848258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 160948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 161048258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 161148258c6bSjp151216 switch (res->info.how.map_type) { 161248258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 161348258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 161448258c6bSjp151216 strdup(values[7]); 161548258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 161648258c6bSjp151216 strdup(values[8]); 161748258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 161848258c6bSjp151216 strdup(values[9]); 161948258c6bSjp151216 break; 162048258c6bSjp151216 162148258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 162248258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 162348258c6bSjp151216 strdup(values[7]); 162448258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 162548258c6bSjp151216 strdup(values[8]); 162648258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 162748258c6bSjp151216 strdup(values[9]); 162848258c6bSjp151216 break; 162948258c6bSjp151216 163048258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 163148258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 163248258c6bSjp151216 strdup(values[10]); 163348258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 163448258c6bSjp151216 strdup(values[11]); 163548258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 163648258c6bSjp151216 strdup(values[12]); 163748258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 163848258c6bSjp151216 strtoul(values[13], &end, 1); 163948258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 164048258c6bSjp151216 is_user; 164148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 164248258c6bSjp151216 strtoul(values[5], &end, 1); 164348258c6bSjp151216 break; 164448258c6bSjp151216 164548258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 164648258c6bSjp151216 break; 164748258c6bSjp151216 164848258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 164948258c6bSjp151216 break; 165048258c6bSjp151216 165148258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 165248258c6bSjp151216 break; 165348258c6bSjp151216 165448258c6bSjp151216 default: 165548258c6bSjp151216 /* Unknow mapping type */ 165648258c6bSjp151216 assert(FALSE); 165748258c6bSjp151216 } 165848258c6bSjp151216 } 1659c5c4113dSnw141292 } 166062c60062Sbaban if (vm != NULL) 1661c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1662c5c4113dSnw141292 return (retcode); 1663c5c4113dSnw141292 } 1664c5c4113dSnw141292 1665cd37da74Snw141292 static 1666cd37da74Snw141292 idmap_retcode 166762c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid, 1668cd37da74Snw141292 char **name, char **domain, int *type) 1669cd37da74Snw141292 { 1670c5c4113dSnw141292 char *end; 1671c5c4113dSnw141292 char *sql = NULL; 1672c5c4113dSnw141292 const char **values; 1673c5c4113dSnw141292 sqlite_vm *vm = NULL; 1674c5c4113dSnw141292 int ncol; 1675c5c4113dSnw141292 time_t curtime; 1676c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 1677c5c4113dSnw141292 1678c5c4113dSnw141292 /* Get current time */ 1679c5c4113dSnw141292 errno = 0; 1680c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1681cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1682c5c4113dSnw141292 strerror(errno)); 1683c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1684c5c4113dSnw141292 goto out; 1685c5c4113dSnw141292 } 1686c5c4113dSnw141292 1687c5c4113dSnw141292 /* SQL to lookup the cache */ 1688cd37da74Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type " 1689cd37da74Snw141292 "FROM name_cache WHERE " 1690c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND " 1691c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1692c5c4113dSnw141292 "expiration > %d);", 1693c5c4113dSnw141292 sidprefix, rid, curtime); 1694c5c4113dSnw141292 if (sql == NULL) { 1695c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1696c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1697c5c4113dSnw141292 goto out; 1698c5c4113dSnw141292 } 1699c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values); 1700c5c4113dSnw141292 sqlite_freemem(sql); 1701c5c4113dSnw141292 1702c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 170362c60062Sbaban if (type != NULL) { 1704c5c4113dSnw141292 if (values[2] == NULL) { 1705c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1706c5c4113dSnw141292 goto out; 1707c5c4113dSnw141292 } 1708c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 1709c5c4113dSnw141292 } 1710c5c4113dSnw141292 171162c60062Sbaban if (name != NULL && values[0] != NULL) { 1712c5c4113dSnw141292 if ((*name = strdup(values[0])) == NULL) { 1713c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1714c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1715c5c4113dSnw141292 goto out; 1716c5c4113dSnw141292 } 1717c5c4113dSnw141292 } 1718c5c4113dSnw141292 171962c60062Sbaban if (domain != NULL && values[1] != NULL) { 1720c5c4113dSnw141292 if ((*domain = strdup(values[1])) == NULL) { 172162c60062Sbaban if (name != NULL && *name) { 1722c5c4113dSnw141292 free(*name); 1723c5c4113dSnw141292 *name = NULL; 1724c5c4113dSnw141292 } 1725c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1726c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1727c5c4113dSnw141292 goto out; 1728c5c4113dSnw141292 } 1729c5c4113dSnw141292 } 1730c5c4113dSnw141292 } 1731c5c4113dSnw141292 1732c5c4113dSnw141292 out: 173362c60062Sbaban if (vm != NULL) 1734c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1735c5c4113dSnw141292 return (retcode); 1736c5c4113dSnw141292 } 1737c5c4113dSnw141292 1738c5c4113dSnw141292 /* 1739e8c27ec8Sbaban * Given SID, find winname using name_cache OR 1740e8c27ec8Sbaban * Given winname, find SID using name_cache. 1741e8c27ec8Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and 1742e8c27ec8Sbaban * req->id2 is unix id 1743c5c4113dSnw141292 */ 1744cd37da74Snw141292 static 1745cd37da74Snw141292 idmap_retcode 1746e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1747cd37da74Snw141292 { 1748c5c4113dSnw141292 int type = -1; 1749c5c4113dSnw141292 idmap_retcode retcode; 1750e8c27ec8Sbaban char *sidprefix = NULL; 1751c5c4113dSnw141292 idmap_rid_t rid; 1752c5c4113dSnw141292 char *name = NULL, *domain = NULL; 1753c5c4113dSnw141292 1754e8c27ec8Sbaban /* Done if we've both sid and winname */ 1755e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) 1756e8c27ec8Sbaban return (IDMAP_SUCCESS); 1757c5c4113dSnw141292 1758e8c27ec8Sbaban /* Lookup sid to winname */ 1759e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1760e8c27ec8Sbaban retcode = lookup_cache_sid2name(cache, 1761e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix, 1762e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type); 176362c60062Sbaban goto out; 1764e8c27ec8Sbaban } 176562c60062Sbaban 1766e8c27ec8Sbaban /* Lookup winame to sid */ 1767e8c27ec8Sbaban retcode = lookup_cache_name2sid(cache, req->id1name, req->id1domain, 1768e8c27ec8Sbaban &name, &sidprefix, &rid, &type); 1769c5c4113dSnw141292 177062c60062Sbaban out: 1771e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1772c5c4113dSnw141292 free(name); 1773c5c4113dSnw141292 free(domain); 1774e8c27ec8Sbaban free(sidprefix); 1775e8c27ec8Sbaban return (retcode); 1776e8c27ec8Sbaban } 1777e8c27ec8Sbaban 1778e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) { 1779e8c27ec8Sbaban res->id.idtype = (type == _IDMAP_T_USER) ? 1780e8c27ec8Sbaban IDMAP_UID : IDMAP_GID; 1781e8c27ec8Sbaban } 1782e8c27ec8Sbaban req->id1.idtype = (type == _IDMAP_T_USER) ? 1783e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1784e8c27ec8Sbaban 1785e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1786e8c27ec8Sbaban if (name != NULL) { 1787e8c27ec8Sbaban free(req->id1name); /* Free existing winname */ 1788e8c27ec8Sbaban req->id1name = name; /* and use canonical name instead */ 1789e8c27ec8Sbaban } 1790e8c27ec8Sbaban if (req->id1domain == NULL) 1791e8c27ec8Sbaban req->id1domain = domain; 1792e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) { 1793e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix; 1794e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = rid; 1795c5c4113dSnw141292 } 1796c5c4113dSnw141292 return (retcode); 1797c5c4113dSnw141292 } 1798c5c4113dSnw141292 1799e8c27ec8Sbaban /* 1800e8c27ec8Sbaban * Batch AD lookups 1801e8c27ec8Sbaban */ 1802c5c4113dSnw141292 idmap_retcode 1803e8c27ec8Sbaban ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch, 1804cd37da74Snw141292 idmap_ids_res *result) 1805cd37da74Snw141292 { 1806c5c4113dSnw141292 idmap_retcode retcode; 1807e8c27ec8Sbaban int i, add, type, is_wuser, is_user; 1808e8c27ec8Sbaban int retries = 0, eunixtype; 1809e8c27ec8Sbaban char **unixname; 1810c5c4113dSnw141292 idmap_mapping *req; 1811c5c4113dSnw141292 idmap_id_res *res; 1812e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 181348258c6bSjp151216 idmap_how *how; 1814479ac375Sdm199847 char **dn, **attr, **value; 1815e8c27ec8Sbaban 1816e8c27ec8Sbaban /* 1817e8c27ec8Sbaban * Since req->id2.idtype is unused, we will use it here 1818e8c27ec8Sbaban * to retrieve the value of sid_type. But it needs to be 1819e8c27ec8Sbaban * reset to IDMAP_NONE before we return to prevent xdr 1820e8c27ec8Sbaban * from mis-interpreting req->id2 when it tries to free 1821e8c27ec8Sbaban * the input argument. Other option is to allocate an 1822e8c27ec8Sbaban * array of integers and use it instead for the batched 1823e8c27ec8Sbaban * call. But why un-necessarily allocate memory. That may 1824e8c27ec8Sbaban * be an option if req->id2.idtype cannot be re-used in 1825e8c27ec8Sbaban * future. 1826e8c27ec8Sbaban */ 1827c5c4113dSnw141292 1828c5c4113dSnw141292 if (state->ad_nqueries == 0) 1829c5c4113dSnw141292 return (IDMAP_SUCCESS); 1830c5c4113dSnw141292 1831c5c4113dSnw141292 retry: 1832e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, state->ad_nqueries, 1833e8c27ec8Sbaban &qs); 1834e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 18350dcc7149Snw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 18360dcc7149Snw141292 goto retry; 1837349d5d8fSnw141292 degrade_svc(1, "failed to create batch for AD lookup"); 1838e8c27ec8Sbaban goto out; 1839c5c4113dSnw141292 } 1840c5c4113dSnw141292 1841c8e26105Sjp151216 restore_svc(); 1842c8e26105Sjp151216 1843e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 1844e8c27ec8Sbaban state->ad_unixgroup_attr); 1845e8c27ec8Sbaban 1846e8c27ec8Sbaban for (i = 0, add = 0; i < batch->idmap_mapping_batch_len; i++) { 1847c5c4113dSnw141292 req = &batch->idmap_mapping_batch_val[i]; 1848c5c4113dSnw141292 res = &result->ids.ids_val[i]; 184948258c6bSjp151216 how = &res->info.how; 185048258c6bSjp151216 1851e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 1852e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1853c5c4113dSnw141292 1854e8c27ec8Sbaban /* Skip if not marked for AD lookup */ 1855e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1856e8c27ec8Sbaban continue; 1857e8c27ec8Sbaban 1858c5c4113dSnw141292 if (retries == 0) 1859c5c4113dSnw141292 res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR; 1860c5c4113dSnw141292 else if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR) 1861c5c4113dSnw141292 continue; 1862e8c27ec8Sbaban 1863e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1864e8c27ec8Sbaban 1865479ac375Sdm199847 /* win2unix request: */ 1866e8c27ec8Sbaban 1867479ac375Sdm199847 unixname = dn = attr = value = NULL; 1868e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 1869e8c27ec8Sbaban if (req->id2name == NULL) { 1870e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID && 1871e8c27ec8Sbaban AD_OR_MIXED(state->nm_siduid)) { 1872e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 1873e8c27ec8Sbaban unixname = &req->id2name; 1874e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GID && 1875e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1876e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 1877e8c27ec8Sbaban unixname = &req->id2name; 1878e8c27ec8Sbaban } else if (AD_OR_MIXED(state->nm_siduid) || 1879e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1880e8c27ec8Sbaban unixname = &req->id2name; 1881e8c27ec8Sbaban } 1882e8c27ec8Sbaban } 1883e8c27ec8Sbaban add = 1; 1884479ac375Sdm199847 if (unixname != NULL) { 1885479ac375Sdm199847 /* 1886479ac375Sdm199847 * Get how info for DS-based name 1887479ac375Sdm199847 * mapping only if AD or MIXED 1888479ac375Sdm199847 * mode is enabled. 1889479ac375Sdm199847 */ 1890479ac375Sdm199847 idmap_info_free(&res->info); 189148258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 189248258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1893479ac375Sdm199847 dn = &how->idmap_how_u.ad.dn; 1894479ac375Sdm199847 attr = &how->idmap_how_u.ad.attr; 1895479ac375Sdm199847 value = &how->idmap_how_u.ad.value; 1896479ac375Sdm199847 } 1897479ac375Sdm199847 if (req->id1.idmap_id_u.sid.prefix != NULL) { 1898479ac375Sdm199847 /* Lookup AD by SID */ 1899c5c4113dSnw141292 retcode = idmap_sid2name_batch_add1( 1900e8c27ec8Sbaban qs, req->id1.idmap_id_u.sid.prefix, 1901e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, eunixtype, 1902479ac375Sdm199847 dn, attr, value, 1903479ac375Sdm199847 (req->id1name == NULL) ? 1904479ac375Sdm199847 &req->id1name : NULL, 1905479ac375Sdm199847 (req->id1domain == NULL) ? 1906479ac375Sdm199847 &req->id1domain : NULL, 1907479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1908479ac375Sdm199847 &res->retcode); 1909479ac375Sdm199847 } else { 1910479ac375Sdm199847 /* Lookup AD by winname */ 1911479ac375Sdm199847 assert(req->id1name != NULL); 1912479ac375Sdm199847 retcode = idmap_name2sid_batch_add1( 1913479ac375Sdm199847 qs, req->id1name, req->id1domain, 1914479ac375Sdm199847 eunixtype, 1915479ac375Sdm199847 dn, attr, value, 1916479ac375Sdm199847 &req->id1name, 1917479ac375Sdm199847 &req->id1.idmap_id_u.sid.prefix, 1918479ac375Sdm199847 &req->id1.idmap_id_u.sid.rid, 1919479ac375Sdm199847 (int *)&req->id2.idtype, unixname, 1920479ac375Sdm199847 &res->retcode); 1921479ac375Sdm199847 } 1922c5c4113dSnw141292 1923e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 1924479ac375Sdm199847 1925479ac375Sdm199847 /* unix2win request: */ 1926e8c27ec8Sbaban 1927e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL && 1928e8c27ec8Sbaban req->id2name != NULL) { 1929e8c27ec8Sbaban /* Already have SID and winname -- done */ 1930e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 1931e8c27ec8Sbaban continue; 1932c5c4113dSnw141292 } 1933c5c4113dSnw141292 1934e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) { 1935cd37da74Snw141292 /* 1936e8c27ec8Sbaban * SID but no winname -- lookup AD by 1937e8c27ec8Sbaban * SID to get winname. 1938479ac375Sdm199847 * how info is not needed here because 1939479ac375Sdm199847 * we are not retrieving unixname from 1940479ac375Sdm199847 * AD. 1941cd37da74Snw141292 */ 1942e8c27ec8Sbaban add = 1; 1943e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1( 1944e8c27ec8Sbaban qs, res->id.idmap_id_u.sid.prefix, 1945e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 194648258c6bSjp151216 _IDMAP_T_UNDEF, 1947479ac375Sdm199847 NULL, NULL, NULL, 194848258c6bSjp151216 &req->id2name, 1949e8c27ec8Sbaban &req->id2domain, (int *)&req->id2.idtype, 1950e8c27ec8Sbaban NULL, &res->retcode); 1951e8c27ec8Sbaban } else if (req->id2name != NULL) { 1952e8c27ec8Sbaban /* 1953e8c27ec8Sbaban * winname but no SID -- lookup AD by 1954e8c27ec8Sbaban * winname to get SID. 1955479ac375Sdm199847 * how info is not needed here because 1956479ac375Sdm199847 * we are not retrieving unixname from 1957479ac375Sdm199847 * AD. 1958e8c27ec8Sbaban */ 1959e8c27ec8Sbaban add = 1; 1960e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1( 1961e8c27ec8Sbaban qs, req->id2name, req->id2domain, 196248258c6bSjp151216 _IDMAP_T_UNDEF, 1963479ac375Sdm199847 NULL, NULL, NULL, NULL, 1964e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1965e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1966e8c27ec8Sbaban (int *)&req->id2.idtype, NULL, 1967e8c27ec8Sbaban &res->retcode); 1968e8c27ec8Sbaban } else if (req->id1name != NULL) { 1969e8c27ec8Sbaban /* 1970e8c27ec8Sbaban * No SID and no winname but we've unixname -- 1971e8c27ec8Sbaban * lookup AD by unixname to get SID. 1972e8c27ec8Sbaban */ 1973e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 1974e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 1975e8c27ec8Sbaban is_wuser = 1; 1976e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 1977e8c27ec8Sbaban is_wuser = 0; 1978e8c27ec8Sbaban else 1979e8c27ec8Sbaban is_wuser = is_user; 1980e8c27ec8Sbaban add = 1; 1981479ac375Sdm199847 idmap_info_free(&res->info); 198248258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 198348258c6bSjp151216 how->map_type = IDMAP_MAP_TYPE_DS_AD; 1984e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1( 1985e8c27ec8Sbaban qs, req->id1name, is_user, is_wuser, 198648258c6bSjp151216 &how->idmap_how_u.ad.dn, 198748258c6bSjp151216 &how->idmap_how_u.ad.attr, 198848258c6bSjp151216 &how->idmap_how_u.ad.value, 1989e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1990e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1991e8c27ec8Sbaban &req->id2name, &req->id2domain, 1992e8c27ec8Sbaban (int *)&req->id2.idtype, &res->retcode); 1993e8c27ec8Sbaban } 1994e8c27ec8Sbaban } 1995e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1996e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 1997e8c27ec8Sbaban break; 1998e8c27ec8Sbaban } 1999cd37da74Snw141292 } 2000cd37da74Snw141292 2001e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS && add) 20020dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 2003cd37da74Snw141292 2004c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 2005c5c4113dSnw141292 goto retry; 2006c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 2007349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 2008c5c4113dSnw141292 2009e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2010e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests"); 2011c5c4113dSnw141292 2012c5c4113dSnw141292 out: 2013e8c27ec8Sbaban /* 2014e8c27ec8Sbaban * This loop does the following: 2015479ac375Sdm199847 * 1. Reset _IDMAP_F_LOOKUP_AD flag from the request. 2016479ac375Sdm199847 * 2. Reset req->id2.idtype to IDMAP_NONE 2017479ac375Sdm199847 * 3. If batch_start or batch_add failed then set the status 2018479ac375Sdm199847 * of each request marked for AD lookup to that error. 2019479ac375Sdm199847 * 4. Evaluate the type of the AD object (i.e. user or group) and 2020479ac375Sdm199847 * update the idtype in request. 2021e8c27ec8Sbaban */ 2022e8c27ec8Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 2023e8c27ec8Sbaban req = &batch->idmap_mapping_batch_val[i]; 2024e8c27ec8Sbaban type = req->id2.idtype; 2025e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 20265e0794bcSbaban res = &result->ids.ids_val[i]; 202748258c6bSjp151216 how = &res->info.how; 2028e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 2029e8c27ec8Sbaban continue; 2030e8c27ec8Sbaban 2031479ac375Sdm199847 /* Reset AD lookup flag */ 2032479ac375Sdm199847 req->direction &= ~(_IDMAP_F_LOOKUP_AD); 2033479ac375Sdm199847 2034479ac375Sdm199847 /* 2035479ac375Sdm199847 * If batch_start or batch_add failed then set the status 2036479ac375Sdm199847 * of each request marked for AD lookup to that error. 2037479ac375Sdm199847 */ 2038e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 2039e8c27ec8Sbaban res->retcode = retcode; 2040e8c27ec8Sbaban continue; 2041e8c27ec8Sbaban } 2042e8c27ec8Sbaban 2043e8c27ec8Sbaban if (!add) 2044e8c27ec8Sbaban continue; 2045e8c27ec8Sbaban 204648258c6bSjp151216 if (res->retcode == IDMAP_ERR_NOTFOUND) { 204748258c6bSjp151216 /* Nothing found - remove the preset info */ 2048479ac375Sdm199847 idmap_info_free(&res->info); 204948258c6bSjp151216 } 205048258c6bSjp151216 2051e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 2052e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) 2053e8c27ec8Sbaban continue; 2054479ac375Sdm199847 /* Evaluate result type */ 2055e8c27ec8Sbaban switch (type) { 2056e8c27ec8Sbaban case _IDMAP_T_USER: 2057e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2058e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 2059e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 2060e8c27ec8Sbaban break; 2061e8c27ec8Sbaban case _IDMAP_T_GROUP: 2062e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 2063e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 2064e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 2065e8c27ec8Sbaban break; 2066e8c27ec8Sbaban default: 2067e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2068e8c27ec8Sbaban break; 2069e8c27ec8Sbaban } 2070479ac375Sdm199847 if (res->retcode == IDMAP_SUCCESS && 2071479ac375Sdm199847 req->id1name != NULL && 2072479ac375Sdm199847 (req->id2name == NULL || 2073479ac375Sdm199847 res->id.idmap_id_u.uid == SENTINEL_PID) && 2074479ac375Sdm199847 NLDAP_MODE(res->id.idtype, state)) { 2075479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2076479ac375Sdm199847 state->nldap_nqueries++; 2077479ac375Sdm199847 } 2078e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 2079e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2080e8c27ec8Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) && 2081e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix == NULL && 2082e8c27ec8Sbaban req->id2name == NULL && /* no winname */ 2083e8c27ec8Sbaban req->id1name != NULL) /* unixname */ 2084e8c27ec8Sbaban /* 2085479ac375Sdm199847 * If AD lookup by unixname failed 2086479ac375Sdm199847 * with non fatal error then clear 2087479ac375Sdm199847 * the error (i.e set res->retcode 2088479ac375Sdm199847 * to success). This allows the next 2089479ac375Sdm199847 * pass to process other mapping 2090479ac375Sdm199847 * mechanisms for this request. 2091e8c27ec8Sbaban */ 2092e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 2093e8c27ec8Sbaban continue; 2094e8c27ec8Sbaban } 2095479ac375Sdm199847 /* Evaluate result type */ 2096e8c27ec8Sbaban switch (type) { 2097e8c27ec8Sbaban case _IDMAP_T_USER: 2098e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2099e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 2100e8c27ec8Sbaban break; 2101e8c27ec8Sbaban case _IDMAP_T_GROUP: 2102e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2103e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 2104e8c27ec8Sbaban break; 2105e8c27ec8Sbaban default: 2106e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 2107e8c27ec8Sbaban break; 2108e8c27ec8Sbaban } 2109e8c27ec8Sbaban } 2110e8c27ec8Sbaban } 2111e8c27ec8Sbaban 2112e8c27ec8Sbaban /* AD lookups done. Reset state->ad_nqueries and return */ 2113e8c27ec8Sbaban state->ad_nqueries = 0; 2114c5c4113dSnw141292 return (retcode); 2115c5c4113dSnw141292 } 2116c5c4113dSnw141292 2117cd37da74Snw141292 /* 2118cd37da74Snw141292 * Convention when processing win2unix requests: 2119cd37da74Snw141292 * 2120cd37da74Snw141292 * Windows identity: 2121cd37da74Snw141292 * req->id1name = 2122cd37da74Snw141292 * winname if given otherwise winname found will be placed 2123cd37da74Snw141292 * here. 2124cd37da74Snw141292 * req->id1domain = 2125cd37da74Snw141292 * windomain if given otherwise windomain found will be 2126cd37da74Snw141292 * placed here. 2127cd37da74Snw141292 * req->id1.idtype = 2128cd37da74Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll 2129cd37da74Snw141292 * be set to IDMAP_USID/GSID depending upon whether the 2130cd37da74Snw141292 * given SID is user or group respectively. The user/group-ness 2131cd37da74Snw141292 * is determined either when looking up well-known SIDs table OR 2132479ac375Sdm199847 * if the SID is found in namecache OR by ad_lookup_one() OR by 2133cd37da74Snw141292 * ad_lookup_batch(). 2134cd37da74Snw141292 * req->id1..sid.[prefix, rid] = 2135cd37da74Snw141292 * SID if given otherwise SID found will be placed here. 2136cd37da74Snw141292 * 2137cd37da74Snw141292 * Unix identity: 2138cd37da74Snw141292 * req->id2name = 2139cd37da74Snw141292 * unixname found will be placed here. 2140cd37da74Snw141292 * req->id2domain = 2141cd37da74Snw141292 * NOT USED 2142cd37da74Snw141292 * res->id.idtype = 2143cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 2144cd37da74Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found 2145cd37da74Snw141292 * will be placed here. 2146cd37da74Snw141292 * res->id..[uid or gid] = 2147cd37da74Snw141292 * UID/GID found will be placed here. 2148cd37da74Snw141292 * 2149cd37da74Snw141292 * Others: 2150cd37da74Snw141292 * res->retcode = 2151cd37da74Snw141292 * Return status for this request will be placed here. 2152cd37da74Snw141292 * res->direction = 2153cd37da74Snw141292 * Direction found will be placed here. Direction 2154cd37da74Snw141292 * meaning whether the resultant mapping is valid 2155cd37da74Snw141292 * only from win2unix or bi-directional. 2156cd37da74Snw141292 * req->direction = 2157cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 2158cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 2159cd37da74Snw141292 * of the request. 2160cd37da74Snw141292 * req->id2.idtype = 2161cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 2162cd37da74Snw141292 * type and is used to initialize res->id.idtype. 2163cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 2164cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 2165cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 2166cd37da74Snw141292 * mis-interpreting the contents of req->id2. 2167cd37da74Snw141292 * req->id2..[uid or gid or sid] = 2168cd37da74Snw141292 * NOT USED 2169cd37da74Snw141292 */ 2170cd37da74Snw141292 2171cd37da74Snw141292 /* 2172cd37da74Snw141292 * This function does the following: 2173cd37da74Snw141292 * 1. Lookup well-known SIDs table. 2174cd37da74Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it. 2175cd37da74Snw141292 * 3. Lookup cache. 2176cd37da74Snw141292 * 4. Check if the client does not want new mapping to be allocated 2177cd37da74Snw141292 * in which case this pass is the final pass. 2178cd37da74Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs 2179cd37da74Snw141292 * to do AD lookup. 2180cd37da74Snw141292 */ 2181c5c4113dSnw141292 idmap_retcode 2182479ac375Sdm199847 sid2pid_first_pass(lookup_state_t *state, idmap_mapping *req, 2183cd37da74Snw141292 idmap_id_res *res) 2184cd37da74Snw141292 { 2185c5c4113dSnw141292 idmap_retcode retcode; 2186e8c27ec8Sbaban int wksid; 2187c5c4113dSnw141292 2188e8c27ec8Sbaban /* Initialize result */ 2189e8c27ec8Sbaban res->id.idtype = req->id2.idtype; 2190e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2191e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 2192e8c27ec8Sbaban wksid = 0; 2193c5c4113dSnw141292 2194cf5b5989Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) { 2195e8c27ec8Sbaban if (req->id1name == NULL) { 2196e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 2197c5c4113dSnw141292 goto out; 2198c5c4113dSnw141292 } 2199e8c27ec8Sbaban /* sanitize sidprefix */ 2200e8c27ec8Sbaban free(req->id1.idmap_id_u.sid.prefix); 2201e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = NULL; 2202e8c27ec8Sbaban } 2203c5c4113dSnw141292 2204e8c27ec8Sbaban /* Lookup well-known SIDs table */ 2205e8c27ec8Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid); 2206c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2207c5c4113dSnw141292 goto out; 2208c5c4113dSnw141292 2209e8c27ec8Sbaban /* Check if this is a localsid */ 2210e8c27ec8Sbaban if (!wksid) { 2211e8c27ec8Sbaban retcode = lookup_localsid2pid(req, res); 2212e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2213e8c27ec8Sbaban goto out; 2214e8c27ec8Sbaban } 2215e8c27ec8Sbaban 2216e8c27ec8Sbaban /* Lookup cache */ 2217479ac375Sdm199847 retcode = lookup_cache_sid2pid(state->cache, req, res); 2218c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2219c5c4113dSnw141292 goto out; 2220c5c4113dSnw141292 2221c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 222248258c6bSjp151216 retcode = IDMAP_ERR_NONEGENERATED; 2223c5c4113dSnw141292 goto out; 2224c5c4113dSnw141292 } 2225c5c4113dSnw141292 2226c5c4113dSnw141292 /* 2227e8c27ec8Sbaban * Failed to find non-expired entry in cache. Next step is 2228e8c27ec8Sbaban * to determine if this request needs to be batched for AD lookup. 2229e8c27ec8Sbaban * 2230e8c27ec8Sbaban * At this point we have either sid or winname or both. If we don't 2231e8c27ec8Sbaban * have both then lookup name_cache for the sid or winname 2232e8c27ec8Sbaban * whichever is missing. If not found then this request will be 2233e8c27ec8Sbaban * batched for AD lookup. 2234e8c27ec8Sbaban */ 2235479ac375Sdm199847 retcode = lookup_name_cache(state->cache, req, res); 2236e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS && retcode != IDMAP_ERR_NOTFOUND) 2237e8c27ec8Sbaban goto out; 2238e8c27ec8Sbaban 2239e8c27ec8Sbaban /* 2240e8c27ec8Sbaban * Set the flag to indicate that we are not done yet so that 2241e8c27ec8Sbaban * subsequent passes considers this request for name-based 2242e8c27ec8Sbaban * mapping and ephemeral mapping. 2243c5c4113dSnw141292 */ 2244c5c4113dSnw141292 state->sid2pid_done = FALSE; 2245e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 2246c5c4113dSnw141292 2247c5c4113dSnw141292 /* 2248e8c27ec8Sbaban * Even if we have both sid and winname, we still may need to batch 2249e8c27ec8Sbaban * this request for AD lookup if we don't have unixname and 2250e8c27ec8Sbaban * directory-based name mapping (AD or mixed) is enabled. 2251e8c27ec8Sbaban * We avoid AD lookup for well-known SIDs because they don't have 2252e8c27ec8Sbaban * regular AD objects. 2253c5c4113dSnw141292 */ 2254e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS || 2255e8c27ec8Sbaban (!wksid && req->id2name == NULL && 2256e8c27ec8Sbaban AD_OR_MIXED_MODE(res->id.idtype, state))) { 2257c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 2258e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 2259c5c4113dSnw141292 state->ad_nqueries++; 2260479ac375Sdm199847 } else if (NLDAP_MODE(res->id.idtype, state)) { 2261479ac375Sdm199847 req->direction |= _IDMAP_F_LOOKUP_NLDAP; 2262479ac375Sdm199847 state->nldap_nqueries++; 2263c5c4113dSnw141292 } 2264c5c4113dSnw141292 2265c5c4113dSnw141292 2266c5c4113dSnw141292 out: 2267c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2268e8c27ec8Sbaban /* 2269e8c27ec8Sbaban * If we are done and there was an error then set fallback pid 2270e8c27ec8Sbaban * in the result. 2271e8c27ec8Sbaban */ 2272e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 2273e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2274c5c4113dSnw141292 return (retcode); 2275c5c4113dSnw141292 } 2276c5c4113dSnw141292 2277c5c4113dSnw141292 /* 2278c5c4113dSnw141292 * Generate SID using the following convention 2279c5c4113dSnw141292 * <machine-sid-prefix>-<1000 + uid> 2280c5c4113dSnw141292 * <machine-sid-prefix>-<2^31 + gid> 2281c5c4113dSnw141292 */ 2282cd37da74Snw141292 static 2283cd37da74Snw141292 idmap_retcode 228448258c6bSjp151216 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user, 228548258c6bSjp151216 int fallback) 2286cd37da74Snw141292 { 2287e8c27ec8Sbaban free(res->id.idmap_id_u.sid.prefix); 2288e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix = NULL; 2289e8c27ec8Sbaban 2290e8c27ec8Sbaban /* 2291e8c27ec8Sbaban * Diagonal mapping for localSIDs not supported because of the 2292e8c27ec8Sbaban * way we generate localSIDs. 2293e8c27ec8Sbaban */ 2294e8c27ec8Sbaban if (is_user && res->id.idtype == IDMAP_GSID) 2295e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2296e8c27ec8Sbaban if (!is_user && res->id.idtype == IDMAP_USID) 2297e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2298e8c27ec8Sbaban 2299c5c4113dSnw141292 /* Skip 1000 UIDs */ 2300c5c4113dSnw141292 if (is_user && req->id1.idmap_id_u.uid > 2301c5c4113dSnw141292 (INT32_MAX - LOCALRID_MIN)) 230262c60062Sbaban return (IDMAP_ERR_NOMAPPING); 2303c5c4113dSnw141292 2304c5c4113dSnw141292 RDLOCK_CONFIG(); 2305e8c27ec8Sbaban /* 2306e8c27ec8Sbaban * machine_sid is never NULL because if it is we won't be here. 2307e8c27ec8Sbaban * No need to assert because stdrup(NULL) will core anyways. 2308e8c27ec8Sbaban */ 2309c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 2310c5c4113dSnw141292 strdup(_idmapdstate.cfg->pgcfg.machine_sid); 2311c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 2312c5c4113dSnw141292 UNLOCK_CONFIG(); 2313c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2314c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 2315c5c4113dSnw141292 } 2316c5c4113dSnw141292 UNLOCK_CONFIG(); 2317c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 2318c5c4113dSnw141292 (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_MIN : 2319c5c4113dSnw141292 req->id1.idmap_id_u.gid + INT32_MAX + 1; 2320651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 2321e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2322e8c27ec8Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 2323c5c4113dSnw141292 232448258c6bSjp151216 if (!fallback && req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 232548258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 232648258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 232748258c6bSjp151216 } 232848258c6bSjp151216 2329c5c4113dSnw141292 /* 2330c5c4113dSnw141292 * Don't update name_cache because local sids don't have 2331c5c4113dSnw141292 * valid windows names. 2332c5c4113dSnw141292 */ 2333e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 2334947c7bc0Sbaban return (IDMAP_SUCCESS); 2335c5c4113dSnw141292 } 2336c5c4113dSnw141292 2337cd37da74Snw141292 static 2338cd37da74Snw141292 idmap_retcode 2339cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res) 2340cd37da74Snw141292 { 2341c5c4113dSnw141292 char *sidprefix; 2342c5c4113dSnw141292 uint32_t rid; 2343c5c4113dSnw141292 int s; 2344c5c4113dSnw141292 2345c5c4113dSnw141292 /* 2346c5c4113dSnw141292 * If the sidprefix == localsid then UID = last RID - 1000 or 2347c5c4113dSnw141292 * GID = last RID - 2^31. 2348c5c4113dSnw141292 */ 2349e8c27ec8Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL) 2350e8c27ec8Sbaban /* This means we are looking up by winname */ 2351e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2352c5c4113dSnw141292 rid = req->id1.idmap_id_u.sid.rid; 2353c5c4113dSnw141292 2354c5c4113dSnw141292 RDLOCK_CONFIG(); 2355c5c4113dSnw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ? 2356cd37da74Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1; 2357c5c4113dSnw141292 UNLOCK_CONFIG(); 2358c5c4113dSnw141292 2359e8c27ec8Sbaban /* 2360e8c27ec8Sbaban * If the given sidprefix does not match machine_sid then this is 2361e8c27ec8Sbaban * not a local SID. 2362e8c27ec8Sbaban */ 2363e8c27ec8Sbaban if (s != 0) 2364e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2365e8c27ec8Sbaban 2366e8c27ec8Sbaban switch (res->id.idtype) { 2367c5c4113dSnw141292 case IDMAP_UID: 2368e8c27ec8Sbaban if (rid > INT32_MAX || rid < LOCALRID_MIN) 2369e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2370c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2371c5c4113dSnw141292 break; 2372c5c4113dSnw141292 case IDMAP_GID: 2373e8c27ec8Sbaban if (rid <= INT32_MAX) 2374e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2375c5c4113dSnw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2376c5c4113dSnw141292 break; 2377c5c4113dSnw141292 case IDMAP_POSIXID: 2378c5c4113dSnw141292 if (rid > INT32_MAX) { 2379cd37da74Snw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2380c5c4113dSnw141292 res->id.idtype = IDMAP_GID; 2381c5c4113dSnw141292 } else if (rid < LOCALRID_MIN) { 2382e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2383c5c4113dSnw141292 } else { 2384c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2385c5c4113dSnw141292 res->id.idtype = IDMAP_UID; 2386c5c4113dSnw141292 } 2387c5c4113dSnw141292 break; 2388c5c4113dSnw141292 default: 2389c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 2390c5c4113dSnw141292 } 239148258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 239248258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_LOCAL_SID; 239348258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_ALGORITHMIC; 239448258c6bSjp151216 } 2395c5c4113dSnw141292 return (IDMAP_SUCCESS); 2396c5c4113dSnw141292 } 2397c5c4113dSnw141292 2398e8c27ec8Sbaban /* 2399e8c27ec8Sbaban * Name service lookup by unixname to get pid 2400e8c27ec8Sbaban */ 2401cd37da74Snw141292 static 2402cd37da74Snw141292 idmap_retcode 2403e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id) 2404cd37da74Snw141292 { 2405cd37da74Snw141292 struct passwd pwd, *pwdp; 2406cd37da74Snw141292 struct group grp, *grpp; 2407c5c4113dSnw141292 char buf[1024]; 2408c5c4113dSnw141292 int errnum; 2409c5c4113dSnw141292 const char *me = "ns_lookup_byname"; 2410c5c4113dSnw141292 2411e8c27ec8Sbaban switch (id->idtype) { 2412e8c27ec8Sbaban case IDMAP_UID: 2413cd37da74Snw141292 pwdp = getpwnam_r(name, &pwd, buf, sizeof (buf)); 2414e8c27ec8Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL && 2415cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2416cd37da74Snw141292 pwdp = getpwnam_r(lower_name, &pwd, buf, sizeof (buf)); 2417cd37da74Snw141292 if (pwdp == NULL) { 2418c5c4113dSnw141292 errnum = errno; 2419c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2420c5c4113dSnw141292 "%s: getpwnam_r(%s) failed (%s).", 2421cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2422c5c4113dSnw141292 if (errnum == 0) 2423c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2424c5c4113dSnw141292 else 2425c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2426c5c4113dSnw141292 } 2427e8c27ec8Sbaban id->idmap_id_u.uid = pwd.pw_uid; 2428e8c27ec8Sbaban break; 2429e8c27ec8Sbaban case IDMAP_GID: 2430cd37da74Snw141292 grpp = getgrnam_r(name, &grp, buf, sizeof (buf)); 2431e8c27ec8Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL && 2432cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2433cd37da74Snw141292 grpp = getgrnam_r(lower_name, &grp, buf, sizeof (buf)); 2434cd37da74Snw141292 if (grpp == NULL) { 2435c5c4113dSnw141292 errnum = errno; 2436c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2437c5c4113dSnw141292 "%s: getgrnam_r(%s) failed (%s).", 2438cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2439c5c4113dSnw141292 if (errnum == 0) 2440c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2441c5c4113dSnw141292 else 2442c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2443c5c4113dSnw141292 } 2444e8c27ec8Sbaban id->idmap_id_u.gid = grp.gr_gid; 2445e8c27ec8Sbaban break; 2446e8c27ec8Sbaban default: 2447e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2448c5c4113dSnw141292 } 2449c5c4113dSnw141292 return (IDMAP_SUCCESS); 2450c5c4113dSnw141292 } 2451c5c4113dSnw141292 2452e8c27ec8Sbaban 2453e8c27ec8Sbaban /* 2454e8c27ec8Sbaban * Name service lookup by pid to get unixname 2455e8c27ec8Sbaban */ 2456e8c27ec8Sbaban static 2457e8c27ec8Sbaban idmap_retcode 2458e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname) 2459e8c27ec8Sbaban { 2460e8c27ec8Sbaban struct passwd pwd; 2461e8c27ec8Sbaban struct group grp; 2462e8c27ec8Sbaban char buf[1024]; 2463e8c27ec8Sbaban int errnum; 2464e8c27ec8Sbaban const char *me = "ns_lookup_bypid"; 2465e8c27ec8Sbaban 2466e8c27ec8Sbaban if (is_user) { 2467e8c27ec8Sbaban errno = 0; 2468e8c27ec8Sbaban if (getpwuid_r(pid, &pwd, buf, sizeof (buf)) == NULL) { 2469e8c27ec8Sbaban errnum = errno; 2470e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2471e8c27ec8Sbaban "%s: getpwuid_r(%u) failed (%s).", 2472e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2473e8c27ec8Sbaban if (errnum == 0) 2474e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2475e8c27ec8Sbaban else 2476e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2477e8c27ec8Sbaban } 2478e8c27ec8Sbaban *unixname = strdup(pwd.pw_name); 2479e8c27ec8Sbaban } else { 2480e8c27ec8Sbaban errno = 0; 2481e8c27ec8Sbaban if (getgrgid_r(pid, &grp, buf, sizeof (buf)) == NULL) { 2482e8c27ec8Sbaban errnum = errno; 2483e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2484e8c27ec8Sbaban "%s: getgrgid_r(%u) failed (%s).", 2485e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2486e8c27ec8Sbaban if (errnum == 0) 2487e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2488e8c27ec8Sbaban else 2489e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2490e8c27ec8Sbaban } 2491e8c27ec8Sbaban *unixname = strdup(grp.gr_name); 2492e8c27ec8Sbaban } 2493e8c27ec8Sbaban if (*unixname == NULL) 2494e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 2495e8c27ec8Sbaban return (IDMAP_SUCCESS); 2496e8c27ec8Sbaban } 2497e8c27ec8Sbaban 2498c5c4113dSnw141292 /* 2499c5c4113dSnw141292 * Name-based mapping 2500c5c4113dSnw141292 * 2501c5c4113dSnw141292 * Case 1: If no rule matches do ephemeral 2502c5c4113dSnw141292 * 2503c5c4113dSnw141292 * Case 2: If rule matches and unixname is "" then return no mapping. 2504c5c4113dSnw141292 * 2505c5c4113dSnw141292 * Case 3: If rule matches and unixname is specified then lookup name 2506c5c4113dSnw141292 * service using the unixname. If unixname not found then return no mapping. 2507c5c4113dSnw141292 * 2508c5c4113dSnw141292 * Case 4: If rule matches and unixname is * then lookup name service 2509c5c4113dSnw141292 * using winname as the unixname. If unixname not found then process 2510c5c4113dSnw141292 * other rules using the lookup order. If no other rule matches then do 2511c5c4113dSnw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4. 2512c5c4113dSnw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping 2513c5c4113dSnw141292 * instead of the default behaviour of doing ephemeral mapping. 2514c5c4113dSnw141292 * 2515c5c4113dSnw141292 * Example 1: 2516c5c4113dSnw141292 * *@sfbay == * 2517c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2518c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to an ephemeral id. 2519c5c4113dSnw141292 * 2520c5c4113dSnw141292 * Example 2: 2521c5c4113dSnw141292 * *@sfbay == * 2522c5c4113dSnw141292 * *@sfbay => guest 2523c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2524c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to guest. 2525c5c4113dSnw141292 * 2526c5c4113dSnw141292 * Example 3: 2527c5c4113dSnw141292 * *@sfbay == * 2528c5c4113dSnw141292 * *@sfbay => "" 2529c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2530c5c4113dSnw141292 * the name service then we will return no mapping for foo@sfbay. 2531c5c4113dSnw141292 * 2532c5c4113dSnw141292 */ 2533cd37da74Snw141292 static 2534cd37da74Snw141292 idmap_retcode 2535479ac375Sdm199847 name_based_mapping_sid2pid(lookup_state_t *state, 2536479ac375Sdm199847 idmap_mapping *req, idmap_id_res *res) 2537cd37da74Snw141292 { 2538cd37da74Snw141292 const char *unixname, *windomain; 2539cd37da74Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL; 2540c5c4113dSnw141292 idmap_retcode retcode; 2541cd37da74Snw141292 char *end, *lower_unixname, *winname; 2542c5c4113dSnw141292 const char **values; 2543c5c4113dSnw141292 sqlite_vm *vm = NULL; 2544cd37da74Snw141292 int ncol, r, i, is_user, is_wuser; 254548258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 254648258c6bSjp151216 int direction; 2547c5c4113dSnw141292 const char *me = "name_based_mapping_sid2pid"; 2548c5c4113dSnw141292 2549e8c27ec8Sbaban assert(req->id1name != NULL); /* We have winname */ 2550e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have unixname */ 2551e8c27ec8Sbaban 25528e228215Sdm199847 winname = req->id1name; 25538e228215Sdm199847 windomain = req->id1domain; 2554cd37da74Snw141292 2555cd37da74Snw141292 switch (req->id1.idtype) { 2556cd37da74Snw141292 case IDMAP_USID: 2557cd37da74Snw141292 is_wuser = 1; 2558cd37da74Snw141292 break; 2559cd37da74Snw141292 case IDMAP_GSID: 2560cd37da74Snw141292 is_wuser = 0; 2561cd37da74Snw141292 break; 2562cd37da74Snw141292 default: 2563e8c27ec8Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the " 2564e8c27ec8Sbaban "given Windows id is user or group.", me); 2565cd37da74Snw141292 return (IDMAP_ERR_INTERNAL); 2566cd37da74Snw141292 } 2567cd37da74Snw141292 2568e8c27ec8Sbaban switch (res->id.idtype) { 2569cd37da74Snw141292 case IDMAP_UID: 2570cd37da74Snw141292 is_user = 1; 2571cd37da74Snw141292 break; 2572cd37da74Snw141292 case IDMAP_GID: 2573cd37da74Snw141292 is_user = 0; 2574cd37da74Snw141292 break; 2575cd37da74Snw141292 case IDMAP_POSIXID: 2576cd37da74Snw141292 is_user = is_wuser; 2577cd37da74Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID; 2578cd37da74Snw141292 break; 2579cd37da74Snw141292 } 2580c5c4113dSnw141292 2581c5c4113dSnw141292 i = 0; 2582479ac375Sdm199847 if (windomain == NULL) 258362c60062Sbaban windomain = ""; 2584*82da9f60Sbaban else if (state->defdom != NULL && 2585*82da9f60Sbaban strcasecmp(state->defdom, windomain) == 0) 2586c5c4113dSnw141292 i = 1; 2587c5c4113dSnw141292 2588cd37da74Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL) 2589cd37da74Snw141292 lower_winname = winname; /* hope for the best */ 2590c5c4113dSnw141292 sql = sqlite_mprintf( 259148258c6bSjp151216 "SELECT unixname, u2w_order, winname_display, windomain, is_nt4 " 259248258c6bSjp151216 "FROM namerules WHERE " 2593cd37da74Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND " 2594c5c4113dSnw141292 "(winname = %Q OR winname = '*') AND " 2595c5c4113dSnw141292 "(windomain = %Q OR windomain = '*' %s) " 2596c5c4113dSnw141292 "ORDER BY w2u_order ASC;", 2597cd37da74Snw141292 is_user, is_wuser, lower_winname, windomain, 259862c60062Sbaban i ? "OR windomain ISNULL OR windomain = ''" : ""); 2599c5c4113dSnw141292 if (sql == NULL) { 2600c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2601c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 2602c5c4113dSnw141292 goto out; 2603c5c4113dSnw141292 } 2604c5c4113dSnw141292 2605479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 2606c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2607cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2608cd37da74Snw141292 CHECK_NULL(errmsg)); 2609c5c4113dSnw141292 sqlite_freemem(errmsg); 2610c5c4113dSnw141292 goto out; 2611c5c4113dSnw141292 } 2612c5c4113dSnw141292 261384decf41Sjp151216 for (;;) { 2614c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 261584decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 2616c5c4113dSnw141292 261784decf41Sjp151216 if (r == SQLITE_ROW) { 261848258c6bSjp151216 if (ncol < 5) { 2619c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2620c5c4113dSnw141292 goto out; 2621c5c4113dSnw141292 } 2622c5c4113dSnw141292 if (values[0] == NULL) { 2623c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2624c5c4113dSnw141292 goto out; 2625c5c4113dSnw141292 } 2626c5c4113dSnw141292 262748258c6bSjp151216 if (values[1] != NULL) 262848258c6bSjp151216 direction = 262948258c6bSjp151216 (strtol(values[1], &end, 10) == 0)? 263048258c6bSjp151216 IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 263148258c6bSjp151216 else 263248258c6bSjp151216 direction = IDMAP_DIRECTION_W2U; 263348258c6bSjp151216 2634c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 263548258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 263648258c6bSjp151216 values[0], is_wuser, is_user, 263748258c6bSjp151216 strtol(values[4], &end, 10), 263848258c6bSjp151216 direction); 2639c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2640c5c4113dSnw141292 goto out; 2641c5c4113dSnw141292 } 264248258c6bSjp151216 264348258c6bSjp151216 if (values[0][0] == '*') { 264448258c6bSjp151216 unixname = winname; 264548258c6bSjp151216 lower_unixname = lower_winname; 264648258c6bSjp151216 } else { 264748258c6bSjp151216 unixname = values[0]; 264848258c6bSjp151216 lower_unixname = NULL; 264948258c6bSjp151216 } 265048258c6bSjp151216 2651e8c27ec8Sbaban retcode = ns_lookup_byname(unixname, lower_unixname, 2652e8c27ec8Sbaban &res->id); 2653c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 265448258c6bSjp151216 if (values[0][0] == '*') 2655c5c4113dSnw141292 /* Case 4 */ 2656c5c4113dSnw141292 continue; 265748258c6bSjp151216 else { 2658c5c4113dSnw141292 /* Case 3 */ 265948258c6bSjp151216 idmap_namerule_set(rule, values[3], 266048258c6bSjp151216 values[2], values[0], is_wuser, 266148258c6bSjp151216 is_user, 266248258c6bSjp151216 strtol(values[4], &end, 10), 266348258c6bSjp151216 direction); 2664c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2665c5c4113dSnw141292 } 266648258c6bSjp151216 } 2667c5c4113dSnw141292 goto out; 2668c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 2669c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 2670c5c4113dSnw141292 goto out; 2671c5c4113dSnw141292 } else { 2672c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 2673c5c4113dSnw141292 vm = NULL; 2674cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2675cd37da74Snw141292 CHECK_NULL(errmsg)); 2676c5c4113dSnw141292 sqlite_freemem(errmsg); 2677c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2678c5c4113dSnw141292 goto out; 2679c5c4113dSnw141292 } 2680c5c4113dSnw141292 } 2681c5c4113dSnw141292 2682c5c4113dSnw141292 out: 268348258c6bSjp151216 if (sql != NULL) 2684c5c4113dSnw141292 sqlite_freemem(sql); 268548258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 2686c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 268762c60062Sbaban if (values[1] != NULL) 2688c5c4113dSnw141292 res->direction = 2689651c0131Sbaban (strtol(values[1], &end, 10) == 0)? 2690651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 2691c5c4113dSnw141292 else 2692651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 269348258c6bSjp151216 26948e228215Sdm199847 req->id2name = strdup(unixname); 2695479ac375Sdm199847 if (req->id2name == NULL) { 2696479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 2697479ac375Sdm199847 } 2698479ac375Sdm199847 } 2699479ac375Sdm199847 2700479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 270148258c6bSjp151216 idmap_namerule_set(rule, values[3], values[2], 270248258c6bSjp151216 values[0], is_wuser, is_user, strtol(values[4], &end, 10), 270348258c6bSjp151216 res->direction); 270448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 2705c5c4113dSnw141292 } 2706479ac375Sdm199847 2707cd37da74Snw141292 if (lower_winname != NULL && lower_winname != winname) 2708cd37da74Snw141292 free(lower_winname); 270962c60062Sbaban if (vm != NULL) 2710c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 2711c5c4113dSnw141292 return (retcode); 2712c5c4113dSnw141292 } 2713c5c4113dSnw141292 2714c5c4113dSnw141292 static 2715c5c4113dSnw141292 int 2716c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid) 2717c5c4113dSnw141292 { 2718c5c4113dSnw141292 uid_t uid; 2719c5c4113dSnw141292 gid_t gid; 2720c5c4113dSnw141292 int err; 2721c5c4113dSnw141292 2722c5c4113dSnw141292 *next_uid = (uid_t)-1; 2723c5c4113dSnw141292 uid = _idmapdstate.next_uid++; 2724c5c4113dSnw141292 if (uid >= _idmapdstate.limit_uid) { 2725c5c4113dSnw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0) 2726c5c4113dSnw141292 return (err); 2727c5c4113dSnw141292 2728c5c4113dSnw141292 _idmapdstate.limit_uid = uid + 8192; 2729c5c4113dSnw141292 _idmapdstate.next_uid = uid; 2730c5c4113dSnw141292 } 2731c5c4113dSnw141292 *next_uid = uid; 2732c5c4113dSnw141292 2733c5c4113dSnw141292 return (0); 2734c5c4113dSnw141292 } 2735c5c4113dSnw141292 2736c5c4113dSnw141292 static 2737c5c4113dSnw141292 int 2738c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid) 2739c5c4113dSnw141292 { 2740c5c4113dSnw141292 uid_t uid; 2741c5c4113dSnw141292 gid_t gid; 2742c5c4113dSnw141292 int err; 2743c5c4113dSnw141292 2744c5c4113dSnw141292 *next_gid = (uid_t)-1; 2745c5c4113dSnw141292 gid = _idmapdstate.next_gid++; 2746c5c4113dSnw141292 if (gid >= _idmapdstate.limit_gid) { 2747c5c4113dSnw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0) 2748c5c4113dSnw141292 return (err); 2749c5c4113dSnw141292 2750c5c4113dSnw141292 _idmapdstate.limit_gid = gid + 8192; 2751c5c4113dSnw141292 _idmapdstate.next_gid = gid; 2752c5c4113dSnw141292 } 2753c5c4113dSnw141292 *next_gid = gid; 2754c5c4113dSnw141292 2755c5c4113dSnw141292 return (0); 2756c5c4113dSnw141292 } 2757c5c4113dSnw141292 275862c60062Sbaban static 275962c60062Sbaban int 2760cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize) 2761cd37da74Snw141292 { 276262c60062Sbaban uint_t hval, i, len; 276362c60062Sbaban 276462c60062Sbaban if (str == NULL) 276562c60062Sbaban return (0); 276662c60062Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) { 276762c60062Sbaban hval += str[i]; 276862c60062Sbaban hval += (hval << 10); 276962c60062Sbaban hval ^= (hval >> 6); 277062c60062Sbaban } 277162c60062Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) { 277262c60062Sbaban hval += str[i]; 277362c60062Sbaban hval += (hval << 10); 277462c60062Sbaban hval ^= (hval >> 6); 277562c60062Sbaban } 277662c60062Sbaban hval += (hval << 3); 277762c60062Sbaban hval ^= (hval >> 11); 277862c60062Sbaban hval += (hval << 15); 277962c60062Sbaban return (hval % htsize); 278062c60062Sbaban } 278162c60062Sbaban 278262c60062Sbaban static 278362c60062Sbaban int 278462c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid, 2785cd37da74Snw141292 uid_t *pid) 2786cd37da74Snw141292 { 278762c60062Sbaban uint_t next, key; 278862c60062Sbaban uint_t htsize = state->sid_history_size; 278962c60062Sbaban idmap_sid *sid; 279062c60062Sbaban 279162c60062Sbaban next = gethash(prefix, rid, htsize); 279262c60062Sbaban while (next != htsize) { 279362c60062Sbaban key = state->sid_history[next].key; 279462c60062Sbaban if (key == htsize) 279562c60062Sbaban return (0); 279662c60062Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1. 279762c60062Sbaban idmap_id_u.sid; 279862c60062Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) { 279962c60062Sbaban *pid = state->result->ids.ids_val[key].id. 280062c60062Sbaban idmap_id_u.uid; 280162c60062Sbaban return (1); 280262c60062Sbaban } 280362c60062Sbaban next = state->sid_history[next].next; 280462c60062Sbaban } 280562c60062Sbaban return (0); 280662c60062Sbaban } 280762c60062Sbaban 280862c60062Sbaban static 280962c60062Sbaban void 2810cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid) 2811cd37da74Snw141292 { 281262c60062Sbaban uint_t hash, next; 281362c60062Sbaban uint_t htsize = state->sid_history_size; 281462c60062Sbaban 281562c60062Sbaban hash = next = gethash(prefix, rid, htsize); 281662c60062Sbaban while (state->sid_history[next].key != htsize) { 281762c60062Sbaban next++; 281862c60062Sbaban next %= htsize; 281962c60062Sbaban } 282062c60062Sbaban state->sid_history[next].key = state->curpos; 282162c60062Sbaban if (hash == next) 282262c60062Sbaban return; 282362c60062Sbaban state->sid_history[next].next = state->sid_history[hash].next; 282462c60062Sbaban state->sid_history[hash].next = next; 282562c60062Sbaban } 2826c5c4113dSnw141292 2827e8c27ec8Sbaban void 2828e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state) 2829e8c27ec8Sbaban { 2830e8c27ec8Sbaban free(state->sid_history); 2831e8c27ec8Sbaban free(state->ad_unixuser_attr); 2832e8c27ec8Sbaban free(state->ad_unixgroup_attr); 2833479ac375Sdm199847 free(state->nldap_winname_attr); 2834479ac375Sdm199847 free(state->defdom); 2835e8c27ec8Sbaban } 2836e8c27ec8Sbaban 2837c5c4113dSnw141292 /* ARGSUSED */ 2838c5c4113dSnw141292 static 2839c5c4113dSnw141292 idmap_retcode 2840479ac375Sdm199847 dynamic_ephemeral_mapping(lookup_state_t *state, 2841cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2842cd37da74Snw141292 { 2843c5c4113dSnw141292 2844c5c4113dSnw141292 uid_t next_pid; 2845c5c4113dSnw141292 2846651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 284762c60062Sbaban 284848258c6bSjp151216 if (IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 284948258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 285048258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 285162c60062Sbaban return (IDMAP_SUCCESS); 285248258c6bSjp151216 } 285362c60062Sbaban 285462c60062Sbaban if (state->sid_history != NULL && 285562c60062Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix, 285662c60062Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) { 285762c60062Sbaban res->id.idmap_id_u.uid = next_pid; 285848258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 285948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 286062c60062Sbaban return (IDMAP_SUCCESS); 286162c60062Sbaban } 286262c60062Sbaban 286362c60062Sbaban if (res->id.idtype == IDMAP_UID) { 2864c5c4113dSnw141292 if (get_next_eph_uid(&next_pid) != 0) 2865c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2866c5c4113dSnw141292 res->id.idmap_id_u.uid = next_pid; 2867c5c4113dSnw141292 } else { 2868c5c4113dSnw141292 if (get_next_eph_gid(&next_pid) != 0) 2869c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2870c5c4113dSnw141292 res->id.idmap_id_u.gid = next_pid; 2871c5c4113dSnw141292 } 2872c5c4113dSnw141292 287348258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_EPHEMERAL; 287448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 287562c60062Sbaban if (state->sid_history != NULL) 287662c60062Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix, 287762c60062Sbaban req->id1.idmap_id_u.sid.rid); 287862c60062Sbaban 2879c5c4113dSnw141292 return (IDMAP_SUCCESS); 2880c5c4113dSnw141292 } 2881c5c4113dSnw141292 2882c5c4113dSnw141292 idmap_retcode 2883479ac375Sdm199847 sid2pid_second_pass(lookup_state_t *state, 2884cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2885cd37da74Snw141292 { 2886c5c4113dSnw141292 idmap_retcode retcode; 2887c5c4113dSnw141292 2888c5c4113dSnw141292 /* Check if second pass is needed */ 2889e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2890c5c4113dSnw141292 return (res->retcode); 2891c5c4113dSnw141292 2892c5c4113dSnw141292 /* Get status from previous pass */ 2893e8c27ec8Sbaban retcode = res->retcode; 2894e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2895e8c27ec8Sbaban goto out; 2896c5c4113dSnw141292 2897e8c27ec8Sbaban /* 2898e8c27ec8Sbaban * If directory-based name mapping is enabled then the unixname 2899e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode or 2900e8c27ec8Sbaban * mixed-mode) or from native LDAP object (nldap-mode) -- done. 2901e8c27ec8Sbaban */ 2902e8c27ec8Sbaban if (req->id2name != NULL) { 2903e8c27ec8Sbaban assert(res->id.idtype != IDMAP_POSIXID); 2904e8c27ec8Sbaban if (AD_MODE(res->id.idtype, state)) 2905e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2906e8c27ec8Sbaban else if (NLDAP_MODE(res->id.idtype, state)) 2907e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2908e8c27ec8Sbaban else if (MIXED_MODE(res->id.idtype, state)) 2909e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 2910c5c4113dSnw141292 2911e8c27ec8Sbaban /* 2912e8c27ec8Sbaban * Special case: (1) If the ad_unixuser_attr and 2913e8c27ec8Sbaban * ad_unixgroup_attr uses the same attribute 2914e8c27ec8Sbaban * name and (2) if this is a diagonal mapping 2915e8c27ec8Sbaban * request and (3) the unixname has been retrieved 2916e8c27ec8Sbaban * from the AD object -- then we ignore it and fallback 2917e8c27ec8Sbaban * to name-based mapping rules and ephemeral mapping 2918e8c27ec8Sbaban * 2919e8c27ec8Sbaban * Example: 2920e8c27ec8Sbaban * Properties: 2921e8c27ec8Sbaban * config/ad_unixuser_attr = "unixname" 2922e8c27ec8Sbaban * config/ad_unixgroup_attr = "unixname" 2923e8c27ec8Sbaban * AD user object: 2924e8c27ec8Sbaban * dn: cn=bob ... 2925e8c27ec8Sbaban * objectclass: user 2926e8c27ec8Sbaban * sam: bob 2927e8c27ec8Sbaban * unixname: bob1234 2928e8c27ec8Sbaban * AD group object: 2929e8c27ec8Sbaban * dn: cn=winadmins ... 2930e8c27ec8Sbaban * objectclass: group 2931e8c27ec8Sbaban * sam: winadmins 2932e8c27ec8Sbaban * unixname: unixadmins 2933e8c27ec8Sbaban * 2934e8c27ec8Sbaban * In this example whether "unixname" refers to a unixuser 2935e8c27ec8Sbaban * or unixgroup depends upon the AD object. 2936e8c27ec8Sbaban * 2937e8c27ec8Sbaban * $idmap show -c winname:bob gid 2938e8c27ec8Sbaban * AD lookup by "samAccountName=bob" for 2939e8c27ec8Sbaban * "ad_unixgroup_attr (i.e unixname)" for directory-based 2940e8c27ec8Sbaban * mapping would get "bob1234" which is not what we want. 2941e8c27ec8Sbaban * Now why not getgrnam_r("bob1234") and use it if it 2942e8c27ec8Sbaban * is indeed a unixgroup? That's because Unix can have 2943e8c27ec8Sbaban * users and groups with the same name and we clearly 2944e8c27ec8Sbaban * don't know the intention of the admin here. 2945e8c27ec8Sbaban * Therefore we ignore this and fallback to name-based 2946e8c27ec8Sbaban * mapping rules or ephemeral mapping. 2947e8c27ec8Sbaban */ 2948e8c27ec8Sbaban if ((AD_MODE(res->id.idtype, state) || 2949e8c27ec8Sbaban MIXED_MODE(res->id.idtype, state)) && 2950e8c27ec8Sbaban state->ad_unixuser_attr != NULL && 2951e8c27ec8Sbaban state->ad_unixgroup_attr != NULL && 2952e8c27ec8Sbaban strcasecmp(state->ad_unixuser_attr, 2953e8c27ec8Sbaban state->ad_unixgroup_attr) == 0 && 2954e8c27ec8Sbaban ((req->id1.idtype == IDMAP_USID && 2955e8c27ec8Sbaban res->id.idtype == IDMAP_GID) || 2956e8c27ec8Sbaban (req->id1.idtype == IDMAP_GSID && 2957e8c27ec8Sbaban res->id.idtype == IDMAP_UID))) { 2958e8c27ec8Sbaban free(req->id2name); 2959e8c27ec8Sbaban req->id2name = NULL; 2960e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2961e8c27ec8Sbaban /* fallback */ 2962e8c27ec8Sbaban } else { 2963e8c27ec8Sbaban if (res->id.idmap_id_u.uid == SENTINEL_PID) 2964e8c27ec8Sbaban retcode = ns_lookup_byname(req->id2name, 2965e8c27ec8Sbaban NULL, &res->id); 2966e8c27ec8Sbaban /* 2967479ac375Sdm199847 * If ns_lookup_byname() fails that means the 2968479ac375Sdm199847 * unixname (req->id2name), which was obtained 2969479ac375Sdm199847 * from the AD object by directory-based mapping, 2970479ac375Sdm199847 * is not a valid Unix user/group and therefore 2971479ac375Sdm199847 * we return the error to the client instead of 2972479ac375Sdm199847 * doing rule-based mapping or ephemeral mapping. 2973479ac375Sdm199847 * This way the client can detect the issue. 2974e8c27ec8Sbaban */ 2975c5c4113dSnw141292 goto out; 2976c5c4113dSnw141292 } 2977e8c27ec8Sbaban } 2978c5c4113dSnw141292 297948258c6bSjp151216 /* Free any mapping info from Directory based mapping */ 298048258c6bSjp151216 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 298148258c6bSjp151216 idmap_info_free(&res->info); 298248258c6bSjp151216 2983e8c27ec8Sbaban /* 2984e8c27ec8Sbaban * If we don't have unixname then evaluate local name-based 2985e8c27ec8Sbaban * mapping rules. 2986e8c27ec8Sbaban */ 2987479ac375Sdm199847 retcode = name_based_mapping_sid2pid(state, req, res); 2988e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2989e8c27ec8Sbaban goto out; 2990e8c27ec8Sbaban 2991c5c4113dSnw141292 /* If not found, do ephemeral mapping */ 2992479ac375Sdm199847 retcode = dynamic_ephemeral_mapping(state, req, res); 2993c5c4113dSnw141292 2994c5c4113dSnw141292 out: 2995c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2996e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2997e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 2998e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2999e8c27ec8Sbaban } 3000e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 3001e8c27ec8Sbaban state->sid2pid_done = FALSE; 3002c5c4113dSnw141292 return (retcode); 3003c5c4113dSnw141292 } 3004c5c4113dSnw141292 3005c5c4113dSnw141292 idmap_retcode 3006479ac375Sdm199847 update_cache_pid2sid(lookup_state_t *state, 3007cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3008cd37da74Snw141292 { 3009c5c4113dSnw141292 char *sql = NULL; 3010c5c4113dSnw141292 idmap_retcode retcode; 301148258c6bSjp151216 char *map_dn = NULL; 301248258c6bSjp151216 char *map_attr = NULL; 301348258c6bSjp151216 char *map_value = NULL; 301448258c6bSjp151216 char *map_windomain = NULL; 301548258c6bSjp151216 char *map_winname = NULL; 301648258c6bSjp151216 char *map_unixname = NULL; 301748258c6bSjp151216 int map_is_nt4 = FALSE; 3018c5c4113dSnw141292 3019c5c4113dSnw141292 /* Check if we need to cache anything */ 3020e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3021c5c4113dSnw141292 return (IDMAP_SUCCESS); 3022c5c4113dSnw141292 3023c5c4113dSnw141292 /* We don't cache negative entries */ 3024c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3025c5c4113dSnw141292 return (IDMAP_SUCCESS); 3026c5c4113dSnw141292 3027e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 302848258c6bSjp151216 assert(req->id1.idmap_id_u.uid != SENTINEL_PID); 302948258c6bSjp151216 assert(res->id.idtype != IDMAP_SID); 303048258c6bSjp151216 303148258c6bSjp151216 assert(res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN); 303248258c6bSjp151216 switch (res->info.how.map_type) { 303348258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 303448258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 303548258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 303648258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 303748258c6bSjp151216 break; 303848258c6bSjp151216 303948258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 304048258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 304148258c6bSjp151216 map_attr = res->info.how.idmap_how_u.nldap.attr; 304248258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 304348258c6bSjp151216 break; 304448258c6bSjp151216 304548258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 304648258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 304748258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 304848258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 304948258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 305048258c6bSjp151216 break; 305148258c6bSjp151216 305248258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 305348258c6bSjp151216 break; 305448258c6bSjp151216 305548258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 305648258c6bSjp151216 break; 305748258c6bSjp151216 305848258c6bSjp151216 default: 305948258c6bSjp151216 /* Dont cache other mapping types */ 306048258c6bSjp151216 assert(FALSE); 306148258c6bSjp151216 } 3062e8c27ec8Sbaban 3063c5c4113dSnw141292 /* 3064c5c4113dSnw141292 * Using NULL for u2w instead of 0 so that our trigger allows 3065c5c4113dSnw141292 * the same pid to be the destination in multiple entries 3066c5c4113dSnw141292 */ 3067c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3068cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 306948258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 307048258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 307148258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3072cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 307348258c6bSjp151216 "strftime('%%s','now') + 600, %q, 1, " 307448258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ", 3075cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3076cd37da74Snw141292 req->id2domain, req->id2name, req->id1.idmap_id_u.uid, 3077cd37da74Snw141292 req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0, 3078e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? 1 : 0, 307948258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 308048258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 308148258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3082c5c4113dSnw141292 3083c5c4113dSnw141292 if (sql == NULL) { 3084c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3085c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3086c5c4113dSnw141292 goto out; 3087c5c4113dSnw141292 } 3088c5c4113dSnw141292 3089479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3090c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3091c5c4113dSnw141292 goto out; 3092c5c4113dSnw141292 3093c5c4113dSnw141292 state->pid2sid_done = FALSE; 3094c5c4113dSnw141292 sqlite_freemem(sql); 3095c5c4113dSnw141292 sql = NULL; 3096c5c4113dSnw141292 3097e8c27ec8Sbaban /* Check if we need to update namecache */ 3098e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3099c5c4113dSnw141292 goto out; 3100c5c4113dSnw141292 31018e228215Sdm199847 if (req->id2name == NULL) 3102c5c4113dSnw141292 goto out; 3103c5c4113dSnw141292 3104c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3105cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3106c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3107cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 3108cd37da74Snw141292 req->id2name, req->id2domain, 3109e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3110c5c4113dSnw141292 3111c5c4113dSnw141292 if (sql == NULL) { 3112c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3113c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3114c5c4113dSnw141292 goto out; 3115c5c4113dSnw141292 } 3116c5c4113dSnw141292 3117479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3118c5c4113dSnw141292 3119c5c4113dSnw141292 out: 312048258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 312148258c6bSjp151216 idmap_info_free(&res->info); 312262c60062Sbaban if (sql != NULL) 3123c5c4113dSnw141292 sqlite_freemem(sql); 3124c5c4113dSnw141292 return (retcode); 3125c5c4113dSnw141292 } 3126c5c4113dSnw141292 3127c5c4113dSnw141292 idmap_retcode 3128479ac375Sdm199847 update_cache_sid2pid(lookup_state_t *state, 3129cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 3130cd37da74Snw141292 { 3131c5c4113dSnw141292 char *sql = NULL; 3132c5c4113dSnw141292 idmap_retcode retcode; 3133c5c4113dSnw141292 int is_eph_user; 313448258c6bSjp151216 char *map_dn = NULL; 313548258c6bSjp151216 char *map_attr = NULL; 313648258c6bSjp151216 char *map_value = NULL; 313748258c6bSjp151216 char *map_windomain = NULL; 313848258c6bSjp151216 char *map_winname = NULL; 313948258c6bSjp151216 char *map_unixname = NULL; 314048258c6bSjp151216 int map_is_nt4 = FALSE; 3141c5c4113dSnw141292 3142c5c4113dSnw141292 /* Check if we need to cache anything */ 3143e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3144c5c4113dSnw141292 return (IDMAP_SUCCESS); 3145c5c4113dSnw141292 3146c5c4113dSnw141292 /* We don't cache negative entries */ 3147c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 3148c5c4113dSnw141292 return (IDMAP_SUCCESS); 3149c5c4113dSnw141292 3150c5c4113dSnw141292 if (req->direction & _IDMAP_F_EXP_EPH_UID) 3151c5c4113dSnw141292 is_eph_user = 1; 3152c5c4113dSnw141292 else if (req->direction & _IDMAP_F_EXP_EPH_GID) 3153c5c4113dSnw141292 is_eph_user = 0; 3154c5c4113dSnw141292 else 3155c5c4113dSnw141292 is_eph_user = -1; 3156c5c4113dSnw141292 3157c5c4113dSnw141292 if (is_eph_user >= 0 && !IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 3158c5c4113dSnw141292 sql = sqlite_mprintf("UPDATE idmap_cache " 3159c5c4113dSnw141292 "SET w2u = 0 WHERE " 3160c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 3161c5c4113dSnw141292 "pid >= 2147483648 AND is_user = %d;", 3162c5c4113dSnw141292 req->id1.idmap_id_u.sid.prefix, 3163c5c4113dSnw141292 req->id1.idmap_id_u.sid.rid, 3164c5c4113dSnw141292 is_eph_user); 3165c5c4113dSnw141292 if (sql == NULL) { 3166c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3167c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3168c5c4113dSnw141292 goto out; 3169c5c4113dSnw141292 } 3170c5c4113dSnw141292 3171479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3172c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3173c5c4113dSnw141292 goto out; 3174c5c4113dSnw141292 3175c5c4113dSnw141292 sqlite_freemem(sql); 3176c5c4113dSnw141292 sql = NULL; 3177c5c4113dSnw141292 } 3178c5c4113dSnw141292 3179e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 318048258c6bSjp151216 assert(res->id.idmap_id_u.uid != SENTINEL_PID); 318148258c6bSjp151216 318248258c6bSjp151216 switch (res->info.how.map_type) { 318348258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 318448258c6bSjp151216 map_dn = res->info.how.idmap_how_u.ad.dn; 318548258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 318648258c6bSjp151216 map_value = res->info.how.idmap_how_u.ad.value; 318748258c6bSjp151216 break; 318848258c6bSjp151216 318948258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 319048258c6bSjp151216 map_dn = res->info.how.idmap_how_u.nldap.dn; 319148258c6bSjp151216 map_attr = res->info.how.idmap_how_u.ad.attr; 319248258c6bSjp151216 map_value = res->info.how.idmap_how_u.nldap.value; 319348258c6bSjp151216 break; 319448258c6bSjp151216 319548258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 319648258c6bSjp151216 map_windomain = res->info.how.idmap_how_u.rule.windomain; 319748258c6bSjp151216 map_winname = res->info.how.idmap_how_u.rule.winname; 319848258c6bSjp151216 map_unixname = res->info.how.idmap_how_u.rule.unixname; 319948258c6bSjp151216 map_is_nt4 = res->info.how.idmap_how_u.rule.is_nt4; 320048258c6bSjp151216 break; 320148258c6bSjp151216 320248258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 320348258c6bSjp151216 break; 320448258c6bSjp151216 320548258c6bSjp151216 default: 320648258c6bSjp151216 /* Dont cache other mapping types */ 320748258c6bSjp151216 assert(FALSE); 320848258c6bSjp151216 } 3209cd37da74Snw141292 3210c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 3211cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 321248258c6bSjp151216 "is_user, is_wuser, expiration, w2u, u2w, " 321348258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 321448258c6bSjp151216 "map_winname, map_unixname, map_is_nt4) " 3215cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 321648258c6bSjp151216 "strftime('%%s','now') + 600, 1, %q, " 321748258c6bSjp151216 "%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);", 3218cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3219e8c27ec8Sbaban (req->id1domain != NULL) ? req->id1domain : "", req->id1name, 3220e8c27ec8Sbaban res->id.idmap_id_u.uid, req->id2name, 3221e8c27ec8Sbaban (res->id.idtype == IDMAP_UID) ? 1 : 0, 3222cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? 1 : 0, 322348258c6bSjp151216 (res->direction == 0) ? "1" : NULL, 322448258c6bSjp151216 res->info.how.map_type, map_dn, map_attr, map_value, 322548258c6bSjp151216 map_windomain, map_winname, map_unixname, map_is_nt4); 3226c5c4113dSnw141292 3227c5c4113dSnw141292 if (sql == NULL) { 3228c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3229c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3230c5c4113dSnw141292 goto out; 3231c5c4113dSnw141292 } 3232c5c4113dSnw141292 3233479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3234c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3235c5c4113dSnw141292 goto out; 3236c5c4113dSnw141292 3237c5c4113dSnw141292 state->sid2pid_done = FALSE; 3238c5c4113dSnw141292 sqlite_freemem(sql); 3239c5c4113dSnw141292 sql = NULL; 3240c5c4113dSnw141292 3241e8c27ec8Sbaban /* Check if we need to update namecache */ 3242e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 3243c5c4113dSnw141292 goto out; 3244c5c4113dSnw141292 3245cf5b5989Sdm199847 if (EMPTY_STRING(req->id1name)) 3246c5c4113dSnw141292 goto out; 3247c5c4113dSnw141292 3248c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 3249cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 3250c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 3251cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 3252cd37da74Snw141292 req->id1name, req->id1domain, 3253cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 3254c5c4113dSnw141292 3255c5c4113dSnw141292 if (sql == NULL) { 3256c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3257c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3258c5c4113dSnw141292 goto out; 3259c5c4113dSnw141292 } 3260c5c4113dSnw141292 3261479ac375Sdm199847 retcode = sql_exec_no_cb(state->cache, IDMAP_CACHENAME, sql); 3262c5c4113dSnw141292 3263c5c4113dSnw141292 out: 326448258c6bSjp151216 if (!(req->flag & IDMAP_REQ_FLG_MAPPING_INFO)) 326548258c6bSjp151216 idmap_info_free(&res->info); 326648258c6bSjp151216 326762c60062Sbaban if (sql != NULL) 3268c5c4113dSnw141292 sqlite_freemem(sql); 3269c5c4113dSnw141292 return (retcode); 3270c5c4113dSnw141292 } 3271c5c4113dSnw141292 3272cd37da74Snw141292 static 3273cd37da74Snw141292 idmap_retcode 3274c5c4113dSnw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res, 3275cd37da74Snw141292 int is_user, int getname) 3276cd37da74Snw141292 { 3277c5c4113dSnw141292 char *end; 3278c5c4113dSnw141292 char *sql = NULL; 3279c5c4113dSnw141292 const char **values; 3280c5c4113dSnw141292 sqlite_vm *vm = NULL; 3281c5c4113dSnw141292 int ncol; 3282c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3283c5c4113dSnw141292 time_t curtime; 3284e8c27ec8Sbaban idmap_id_type idtype; 3285c5c4113dSnw141292 3286c5c4113dSnw141292 /* Current time */ 3287c5c4113dSnw141292 errno = 0; 3288c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3289cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3290c5c4113dSnw141292 strerror(errno)); 3291c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3292c5c4113dSnw141292 goto out; 3293c5c4113dSnw141292 } 3294c5c4113dSnw141292 3295e8c27ec8Sbaban /* SQL to lookup the cache by pid or by unixname */ 3296e8c27ec8Sbaban if (req->id1.idmap_id_u.uid != SENTINEL_PID) { 329748258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 329848258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 329948258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 330048258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3301c5c4113dSnw141292 "FROM idmap_cache WHERE " 3302c5c4113dSnw141292 "pid = %u AND u2w = 1 AND is_user = %d AND " 3303c5c4113dSnw141292 "(pid >= 2147483648 OR " 3304c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3305c5c4113dSnw141292 "expiration > %d));", 3306c5c4113dSnw141292 req->id1.idmap_id_u.uid, is_user, curtime); 3307e8c27ec8Sbaban } else if (req->id1name != NULL) { 330848258c6bSjp151216 sql = sqlite_mprintf("SELECT sidprefix, rid, " 330948258c6bSjp151216 "canon_winname, windomain, w2u, is_wuser, " 331048258c6bSjp151216 "map_type, map_dn, map_attr, map_value, map_windomain, " 331148258c6bSjp151216 "map_winname, map_unixname, map_is_nt4 " 3312e8c27ec8Sbaban "FROM idmap_cache WHERE " 3313e8c27ec8Sbaban "unixname = %Q AND u2w = 1 AND is_user = %d AND " 3314e8c27ec8Sbaban "(pid >= 2147483648 OR " 3315e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 3316e8c27ec8Sbaban "expiration > %d));", 3317e8c27ec8Sbaban req->id1name, is_user, curtime); 331848258c6bSjp151216 } else { 331948258c6bSjp151216 retcode = IDMAP_ERR_ARG; 332048258c6bSjp151216 goto out; 3321e8c27ec8Sbaban } 3322e8c27ec8Sbaban 3323c5c4113dSnw141292 if (sql == NULL) { 3324c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3325c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3326c5c4113dSnw141292 goto out; 3327c5c4113dSnw141292 } 332848258c6bSjp151216 retcode = sql_compile_n_step_once( 332948258c6bSjp151216 cache, sql, &vm, &ncol, 14, &values); 3330c5c4113dSnw141292 sqlite_freemem(sql); 3331c5c4113dSnw141292 3332c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) 3333c5c4113dSnw141292 goto out; 3334c5c4113dSnw141292 else if (retcode == IDMAP_SUCCESS) { 3335c5c4113dSnw141292 /* sanity checks */ 3336c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 3337c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3338c5c4113dSnw141292 goto out; 3339c5c4113dSnw141292 } 3340c5c4113dSnw141292 3341e8c27ec8Sbaban switch (res->id.idtype) { 3342c5c4113dSnw141292 case IDMAP_SID: 3343cd37da74Snw141292 case IDMAP_USID: 3344cd37da74Snw141292 case IDMAP_GSID: 3345e8c27ec8Sbaban idtype = strtol(values[5], &end, 10) == 1 3346cd37da74Snw141292 ? IDMAP_USID : IDMAP_GSID; 3347cd37da74Snw141292 3348e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID && 3349e8c27ec8Sbaban idtype != IDMAP_USID) { 3350cd37da74Snw141292 retcode = IDMAP_ERR_NOTUSER; 3351cd37da74Snw141292 goto out; 3352e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GSID && 3353e8c27ec8Sbaban idtype != IDMAP_GSID) { 3354cd37da74Snw141292 retcode = IDMAP_ERR_NOTGROUP; 3355cd37da74Snw141292 goto out; 3356cd37da74Snw141292 } 3357e8c27ec8Sbaban res->id.idtype = idtype; 3358cd37da74Snw141292 3359c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 3360c5c4113dSnw141292 strtoul(values[1], &end, 10); 3361c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = strdup(values[0]); 3362c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 3363c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3364c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3365c5c4113dSnw141292 goto out; 3366c5c4113dSnw141292 } 3367c5c4113dSnw141292 336862c60062Sbaban if (values[4] != NULL) 3369c5c4113dSnw141292 res->direction = 3370651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 3371651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3372c5c4113dSnw141292 else 3373651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 3374c5c4113dSnw141292 3375c5c4113dSnw141292 if (getname == 0 || values[2] == NULL) 3376c5c4113dSnw141292 break; 33778e228215Sdm199847 req->id2name = strdup(values[2]); 33788e228215Sdm199847 if (req->id2name == NULL) { 3379c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3380c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3381c5c4113dSnw141292 goto out; 3382c5c4113dSnw141292 } 3383c5c4113dSnw141292 3384c5c4113dSnw141292 if (values[3] == NULL) 3385c5c4113dSnw141292 break; 33868e228215Sdm199847 req->id2domain = strdup(values[3]); 33878e228215Sdm199847 if (req->id2domain == NULL) { 3388c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3389c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3390c5c4113dSnw141292 goto out; 3391c5c4113dSnw141292 } 3392cd37da74Snw141292 3393c5c4113dSnw141292 break; 3394c5c4113dSnw141292 default: 3395c5c4113dSnw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 3396c5c4113dSnw141292 break; 3397c5c4113dSnw141292 } 339848258c6bSjp151216 if (req->flag & IDMAP_REQ_FLG_MAPPING_INFO) { 339948258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_CACHE; 340048258c6bSjp151216 res->info.how.map_type = strtoul(values[6], &end, 10); 340148258c6bSjp151216 switch (res->info.how.map_type) { 340248258c6bSjp151216 case IDMAP_MAP_TYPE_DS_AD: 340348258c6bSjp151216 res->info.how.idmap_how_u.ad.dn = 340448258c6bSjp151216 strdup(values[7]); 340548258c6bSjp151216 res->info.how.idmap_how_u.ad.attr = 340648258c6bSjp151216 strdup(values[8]); 340748258c6bSjp151216 res->info.how.idmap_how_u.ad.value = 340848258c6bSjp151216 strdup(values[9]); 340948258c6bSjp151216 break; 341048258c6bSjp151216 341148258c6bSjp151216 case IDMAP_MAP_TYPE_DS_NLDAP: 341248258c6bSjp151216 res->info.how.idmap_how_u.nldap.dn = 341348258c6bSjp151216 strdup(values[7]); 341448258c6bSjp151216 res->info.how.idmap_how_u.nldap.attr = 341548258c6bSjp151216 strdup(values[8]); 341648258c6bSjp151216 res->info.how.idmap_how_u.nldap.value = 341748258c6bSjp151216 strdup(values[9]); 341848258c6bSjp151216 break; 341948258c6bSjp151216 342048258c6bSjp151216 case IDMAP_MAP_TYPE_RULE_BASED: 342148258c6bSjp151216 res->info.how.idmap_how_u.rule.windomain = 342248258c6bSjp151216 strdup(values[10]); 342348258c6bSjp151216 res->info.how.idmap_how_u.rule.winname = 342448258c6bSjp151216 strdup(values[11]); 342548258c6bSjp151216 res->info.how.idmap_how_u.rule.unixname = 342648258c6bSjp151216 strdup(values[12]); 342748258c6bSjp151216 res->info.how.idmap_how_u.rule.is_nt4 = 342848258c6bSjp151216 strtoul(values[13], &end, 10); 342948258c6bSjp151216 res->info.how.idmap_how_u.rule.is_user = 343048258c6bSjp151216 is_user; 343148258c6bSjp151216 res->info.how.idmap_how_u.rule.is_wuser = 343248258c6bSjp151216 strtol(values[5], &end, 10); 343348258c6bSjp151216 break; 343448258c6bSjp151216 343548258c6bSjp151216 case IDMAP_MAP_TYPE_EPHEMERAL: 343648258c6bSjp151216 break; 343748258c6bSjp151216 343848258c6bSjp151216 case IDMAP_MAP_TYPE_LOCAL_SID: 343948258c6bSjp151216 break; 344048258c6bSjp151216 344148258c6bSjp151216 case IDMAP_MAP_TYPE_KNOWN_SID: 344248258c6bSjp151216 break; 344348258c6bSjp151216 344448258c6bSjp151216 default: 344548258c6bSjp151216 /* Unknow mapping type */ 344648258c6bSjp151216 assert(FALSE); 344748258c6bSjp151216 } 344848258c6bSjp151216 } 3449c5c4113dSnw141292 } 3450c5c4113dSnw141292 3451c5c4113dSnw141292 out: 345262c60062Sbaban if (vm != NULL) 3453c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3454c5c4113dSnw141292 return (retcode); 3455c5c4113dSnw141292 } 3456c5c4113dSnw141292 3457cd37da74Snw141292 static 3458cd37da74Snw141292 idmap_retcode 3459c5c4113dSnw141292 lookup_cache_name2sid(sqlite *cache, const char *name, const char *domain, 3460cd37da74Snw141292 char **canonname, char **sidprefix, idmap_rid_t *rid, int *type) 3461cd37da74Snw141292 { 3462cd37da74Snw141292 char *end, *lower_name; 3463c5c4113dSnw141292 char *sql = NULL; 3464c5c4113dSnw141292 const char **values; 3465c5c4113dSnw141292 sqlite_vm *vm = NULL; 3466c5c4113dSnw141292 int ncol; 3467c5c4113dSnw141292 time_t curtime; 3468c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3469c5c4113dSnw141292 3470c5c4113dSnw141292 /* Get current time */ 3471c5c4113dSnw141292 errno = 0; 3472c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3473cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3474c5c4113dSnw141292 strerror(errno)); 3475c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3476c5c4113dSnw141292 goto out; 3477c5c4113dSnw141292 } 3478c5c4113dSnw141292 3479c5c4113dSnw141292 /* SQL to lookup the cache */ 3480cd37da74Snw141292 if ((lower_name = tolower_u8(name)) == NULL) 3481cd37da74Snw141292 lower_name = (char *)name; 3482cd37da74Snw141292 sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name " 3483cd37da74Snw141292 "FROM name_cache WHERE name = %Q AND domain = %Q AND " 3484c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3485cd37da74Snw141292 "expiration > %d);", lower_name, domain, curtime); 3486cd37da74Snw141292 if (lower_name != name) 3487cd37da74Snw141292 free(lower_name); 3488c5c4113dSnw141292 if (sql == NULL) { 3489c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3490c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3491c5c4113dSnw141292 goto out; 3492c5c4113dSnw141292 } 3493cd37da74Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values); 3494c5c4113dSnw141292 sqlite_freemem(sql); 3495c5c4113dSnw141292 3496c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 349762c60062Sbaban if (type != NULL) { 3498c5c4113dSnw141292 if (values[2] == NULL) { 3499c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3500c5c4113dSnw141292 goto out; 3501c5c4113dSnw141292 } 3502c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 3503c5c4113dSnw141292 } 3504c5c4113dSnw141292 3505e8c27ec8Sbaban if (values[0] == NULL || values[1] == NULL) { 3506e8c27ec8Sbaban retcode = IDMAP_ERR_CACHE; 3507e8c27ec8Sbaban goto out; 3508e8c27ec8Sbaban } 3509e8c27ec8Sbaban 3510cd37da74Snw141292 if (canonname != NULL) { 3511cd37da74Snw141292 assert(values[3] != NULL); 3512cd37da74Snw141292 if ((*canonname = strdup(values[3])) == NULL) { 3513cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3514cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 3515cd37da74Snw141292 goto out; 3516cd37da74Snw141292 } 3517cd37da74Snw141292 } 3518cd37da74Snw141292 3519c5c4113dSnw141292 if ((*sidprefix = strdup(values[0])) == NULL) { 3520c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3521c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3522e8c27ec8Sbaban if (canonname != NULL) { 3523e8c27ec8Sbaban free(*canonname); 3524e8c27ec8Sbaban *canonname = NULL; 3525e8c27ec8Sbaban } 3526c5c4113dSnw141292 goto out; 3527c5c4113dSnw141292 } 3528c5c4113dSnw141292 *rid = strtoul(values[1], &end, 10); 3529c5c4113dSnw141292 } 3530c5c4113dSnw141292 3531c5c4113dSnw141292 out: 353262c60062Sbaban if (vm != NULL) 3533c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3534c5c4113dSnw141292 return (retcode); 3535c5c4113dSnw141292 } 3536c5c4113dSnw141292 3537cd37da74Snw141292 static 3538cd37da74Snw141292 idmap_retcode 3539e8c27ec8Sbaban ad_lookup_by_winname(lookup_state_t *state, 3540e8c27ec8Sbaban const char *name, const char *domain, int eunixtype, 354148258c6bSjp151216 char **dn, char **attr, char **value, char **canonname, 354248258c6bSjp151216 char **sidprefix, idmap_rid_t *rid, int *wintype, 354348258c6bSjp151216 char **unixname) 3544cd37da74Snw141292 { 3545c5c4113dSnw141292 int retries = 0; 3546c5c4113dSnw141292 idmap_query_state_t *qs = NULL; 3547c5c4113dSnw141292 idmap_retcode rc, retcode; 3548c5c4113dSnw141292 3549c5c4113dSnw141292 retry: 3550e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, 1, &qs); 3551e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 35520dcc7149Snw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 35530dcc7149Snw141292 goto retry; 3554349d5d8fSnw141292 degrade_svc(1, "failed to create request for AD lookup " 35550dcc7149Snw141292 "by winname"); 3556e8c27ec8Sbaban return (retcode); 3557c5c4113dSnw141292 } 3558c5c4113dSnw141292 3559c8e26105Sjp151216 restore_svc(); 3560c8e26105Sjp151216 3561e8c27ec8Sbaban if (state != NULL) 3562e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 3563e8c27ec8Sbaban state->ad_unixgroup_attr); 3564c5c4113dSnw141292 3565e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1(qs, name, domain, eunixtype, 356648258c6bSjp151216 dn, attr, value, canonname, sidprefix, rid, wintype, unixname, &rc); 3567c5c4113dSnw141292 3568e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 356984decf41Sjp151216 idmap_lookup_release_batch(&qs); 3570c5c4113dSnw141292 else 35710dcc7149Snw141292 retcode = idmap_lookup_batch_end(&qs); 3572c5c4113dSnw141292 3573c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 3574c5c4113dSnw141292 goto retry; 3575c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 3576349d5d8fSnw141292 degrade_svc(1, "some AD lookups timed out repeatedly"); 3577c5c4113dSnw141292 3578c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) { 3579e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by winname failed"); 3580c5c4113dSnw141292 return (retcode); 3581e8c27ec8Sbaban } 3582c5c4113dSnw141292 return (rc); 3583c5c4113dSnw141292 } 3584c5c4113dSnw141292 3585cd37da74Snw141292 idmap_retcode 3586c5c4113dSnw141292 lookup_name2sid(sqlite *cache, const char *name, const char *domain, 3587cd37da74Snw141292 int *is_wuser, char **canonname, char **sidprefix, 3588479ac375Sdm199847 idmap_rid_t *rid, idmap_mapping *req, int local_only) 3589cd37da74Snw141292 { 3590c5c4113dSnw141292 int type; 3591c5c4113dSnw141292 idmap_retcode retcode; 3592c5c4113dSnw141292 3593cd37da74Snw141292 *sidprefix = NULL; 3594e8c27ec8Sbaban if (canonname != NULL) 3595cd37da74Snw141292 *canonname = NULL; 3596cd37da74Snw141292 3597e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3598cd37da74Snw141292 retcode = lookup_wksids_name2sid(name, canonname, sidprefix, rid, 3599cd37da74Snw141292 &type); 360062c60062Sbaban if (retcode == IDMAP_SUCCESS) { 3601e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 360262c60062Sbaban goto out; 360362c60062Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 360462c60062Sbaban return (retcode); 360562c60062Sbaban } 360662c60062Sbaban 3607e8c27ec8Sbaban /* Lookup cache */ 3608cd37da74Snw141292 retcode = lookup_cache_name2sid(cache, name, domain, canonname, 3609cd37da74Snw141292 sidprefix, rid, &type); 3610e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 3611e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 3612e8c27ec8Sbaban goto out; 3613e8c27ec8Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 3614e8c27ec8Sbaban return (retcode); 3615e8c27ec8Sbaban } 3616e8c27ec8Sbaban 3617479ac375Sdm199847 /* 3618479ac375Sdm199847 * The caller may be using this function to determine if this 3619479ac375Sdm199847 * request needs to be marked for AD lookup or not 3620479ac375Sdm199847 * (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this 3621479ac375Sdm199847 * function to AD lookup now. 3622479ac375Sdm199847 */ 3623479ac375Sdm199847 if (local_only) 3624479ac375Sdm199847 return (retcode); 3625479ac375Sdm199847 3626e8c27ec8Sbaban /* Lookup AD */ 3627e8c27ec8Sbaban retcode = ad_lookup_by_winname(NULL, name, domain, _IDMAP_T_UNDEF, 362848258c6bSjp151216 NULL, NULL, NULL, canonname, sidprefix, rid, &type, NULL); 3629c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3630c5c4113dSnw141292 return (retcode); 3631c5c4113dSnw141292 363262c60062Sbaban out: 3633c5c4113dSnw141292 /* 3634c5c4113dSnw141292 * Entry found (cache or Windows lookup) 3635cd37da74Snw141292 * is_wuser is both input as well as output parameter 3636c5c4113dSnw141292 */ 3637e8c27ec8Sbaban if (*is_wuser == 1 && type != _IDMAP_T_USER) 3638e8c27ec8Sbaban retcode = IDMAP_ERR_NOTUSER; 3639e8c27ec8Sbaban else if (*is_wuser == 0 && type != _IDMAP_T_GROUP) 3640e8c27ec8Sbaban retcode = IDMAP_ERR_NOTGROUP; 3641e8c27ec8Sbaban else if (*is_wuser == -1) { 3642c5c4113dSnw141292 /* Caller wants to know if its user or group */ 3643c5c4113dSnw141292 if (type == _IDMAP_T_USER) 3644cd37da74Snw141292 *is_wuser = 1; 3645c5c4113dSnw141292 else if (type == _IDMAP_T_GROUP) 3646cd37da74Snw141292 *is_wuser = 0; 3647e8c27ec8Sbaban else 3648e8c27ec8Sbaban retcode = IDMAP_ERR_SID; 3649e8c27ec8Sbaban } 3650e8c27ec8Sbaban 3651e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3652cd37da74Snw141292 free(*sidprefix); 3653cd37da74Snw141292 *sidprefix = NULL; 3654e8c27ec8Sbaban if (canonname != NULL) { 3655cd37da74Snw141292 free(*canonname); 3656cd37da74Snw141292 *canonname = NULL; 3657cd37da74Snw141292 } 3658c5c4113dSnw141292 } 3659c5c4113dSnw141292 return (retcode); 3660c5c4113dSnw141292 } 3661c5c4113dSnw141292 3662cd37da74Snw141292 static 3663cd37da74Snw141292 idmap_retcode 3664479ac375Sdm199847 name_based_mapping_pid2sid(lookup_state_t *state, const char *unixname, 3665cd37da74Snw141292 int is_user, idmap_mapping *req, idmap_id_res *res) 3666cd37da74Snw141292 { 3667c5c4113dSnw141292 const char *winname, *windomain; 3668cd37da74Snw141292 char *canonname; 3669c5c4113dSnw141292 char *sql = NULL, *errmsg = NULL; 3670c5c4113dSnw141292 idmap_retcode retcode; 3671c5c4113dSnw141292 char *end; 3672c5c4113dSnw141292 const char **values; 3673c5c4113dSnw141292 sqlite_vm *vm = NULL; 367448258c6bSjp151216 int ncol, r; 3675cd37da74Snw141292 int is_wuser; 3676e8c27ec8Sbaban const char *me = "name_based_mapping_pid2sid"; 367748258c6bSjp151216 int non_wild_match = FALSE; 367848258c6bSjp151216 idmap_namerule *rule = &res->info.how.idmap_how_u.rule; 367948258c6bSjp151216 int direction; 3680e8c27ec8Sbaban 3681e8c27ec8Sbaban assert(unixname != NULL); /* We have unixname */ 3682e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have winname */ 3683e8c27ec8Sbaban assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */ 3684c5c4113dSnw141292 3685c5c4113dSnw141292 sql = sqlite_mprintf( 368648258c6bSjp151216 "SELECT winname_display, windomain, w2u_order, " 368748258c6bSjp151216 "is_wuser, unixname, is_nt4 " 368848258c6bSjp151216 "FROM namerules WHERE " 3689c5c4113dSnw141292 "u2w_order > 0 AND is_user = %d AND " 3690c5c4113dSnw141292 "(unixname = %Q OR unixname = '*') " 3691cd37da74Snw141292 "ORDER BY u2w_order ASC;", is_user, unixname); 3692c5c4113dSnw141292 if (sql == NULL) { 3693c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3694c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3695c5c4113dSnw141292 goto out; 3696c5c4113dSnw141292 } 3697c5c4113dSnw141292 3698479ac375Sdm199847 if (sqlite_compile(state->db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 3699c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3700cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3701cd37da74Snw141292 CHECK_NULL(errmsg)); 3702c5c4113dSnw141292 sqlite_freemem(errmsg); 3703c5c4113dSnw141292 goto out; 3704c5c4113dSnw141292 } 3705c5c4113dSnw141292 370648258c6bSjp151216 for (;;) { 3707c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 370884decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 370984decf41Sjp151216 if (r == SQLITE_ROW) { 371048258c6bSjp151216 if (ncol < 6) { 3711c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3712c5c4113dSnw141292 goto out; 3713c5c4113dSnw141292 } 3714c5c4113dSnw141292 if (values[0] == NULL) { 3715c5c4113dSnw141292 /* values [1] and [2] can be null */ 3716c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3717c5c4113dSnw141292 goto out; 3718c5c4113dSnw141292 } 371948258c6bSjp151216 372048258c6bSjp151216 if (values[2] != NULL) 372148258c6bSjp151216 direction = 372248258c6bSjp151216 (strtol(values[2], &end, 10) == 0)? 372348258c6bSjp151216 IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 372448258c6bSjp151216 else 372548258c6bSjp151216 direction = IDMAP_DIRECTION_U2W; 372648258c6bSjp151216 3727c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 372848258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], 372948258c6bSjp151216 values[4], is_user, 373048258c6bSjp151216 strtol(values[3], &end, 10), 373148258c6bSjp151216 strtol(values[5], &end, 10), 373248258c6bSjp151216 direction); 3733c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 3734c5c4113dSnw141292 goto out; 3735c5c4113dSnw141292 } 3736cd37da74Snw141292 3737cd37da74Snw141292 if (values[0][0] == '*') { 373848258c6bSjp151216 winname = unixname; 373948258c6bSjp151216 if (non_wild_match) { 3740cd37da74Snw141292 /* 374148258c6bSjp151216 * There were non-wildcard rules 374248258c6bSjp151216 * where the Windows identity doesn't 374348258c6bSjp151216 * exist. Return no mapping. 3744cd37da74Snw141292 */ 3745cd37da74Snw141292 retcode = IDMAP_ERR_NOMAPPING; 3746cd37da74Snw141292 goto out; 3747cd37da74Snw141292 } 3748cd37da74Snw141292 } else { 374948258c6bSjp151216 /* Save first non-wild match rule */ 375048258c6bSjp151216 if (!non_wild_match) { 375148258c6bSjp151216 idmap_namerule_set(rule, values[1], 375248258c6bSjp151216 values[0], values[4], 375348258c6bSjp151216 is_user, 375448258c6bSjp151216 strtol(values[3], &end, 10), 375548258c6bSjp151216 strtol(values[5], &end, 10), 375648258c6bSjp151216 direction); 375748258c6bSjp151216 non_wild_match = TRUE; 375848258c6bSjp151216 } 3759cd37da74Snw141292 winname = values[0]; 3760cd37da74Snw141292 } 376148258c6bSjp151216 is_wuser = res->id.idtype == IDMAP_USID ? 1 376248258c6bSjp151216 : res->id.idtype == IDMAP_GSID ? 0 376348258c6bSjp151216 : -1; 376462c60062Sbaban if (values[1] != NULL) 3765c5c4113dSnw141292 windomain = values[1]; 3766479ac375Sdm199847 else if (state->defdom != NULL) 3767479ac375Sdm199847 windomain = state->defdom; 3768c5c4113dSnw141292 else { 3769cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: no domain", me); 3770c5c4113dSnw141292 retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 3771c5c4113dSnw141292 goto out; 3772c5c4113dSnw141292 } 3773cd37da74Snw141292 3774479ac375Sdm199847 retcode = lookup_name2sid(state->cache, 3775479ac375Sdm199847 winname, windomain, 3776cd37da74Snw141292 &is_wuser, &canonname, 3777cd37da74Snw141292 &res->id.idmap_id_u.sid.prefix, 3778479ac375Sdm199847 &res->id.idmap_id_u.sid.rid, req, 0); 3779e8c27ec8Sbaban 3780c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 3781c5c4113dSnw141292 continue; 3782c5c4113dSnw141292 } 3783c5c4113dSnw141292 goto out; 378448258c6bSjp151216 3785c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 378648258c6bSjp151216 /* 378748258c6bSjp151216 * If there were non-wildcard rules where 378848258c6bSjp151216 * Windows identity doesn't exist 378948258c6bSjp151216 * return no mapping. 379048258c6bSjp151216 */ 379148258c6bSjp151216 if (non_wild_match) 379248258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 379348258c6bSjp151216 else 3794c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 3795c5c4113dSnw141292 goto out; 3796c5c4113dSnw141292 } else { 3797c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 3798c5c4113dSnw141292 vm = NULL; 3799cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3800cd37da74Snw141292 CHECK_NULL(errmsg)); 3801c5c4113dSnw141292 sqlite_freemem(errmsg); 3802c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3803c5c4113dSnw141292 goto out; 3804c5c4113dSnw141292 } 3805c5c4113dSnw141292 } 3806c5c4113dSnw141292 3807c5c4113dSnw141292 out: 380862c60062Sbaban if (sql != NULL) 3809c5c4113dSnw141292 sqlite_freemem(sql); 381048258c6bSjp151216 res->info.how.map_type = IDMAP_MAP_TYPE_RULE_BASED; 3811c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 3812cd37da74Snw141292 res->id.idtype = is_wuser ? IDMAP_USID : IDMAP_GSID; 3813cd37da74Snw141292 381462c60062Sbaban if (values[2] != NULL) 3815c5c4113dSnw141292 res->direction = 3816651c0131Sbaban (strtol(values[2], &end, 10) == 0)? 3817651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3818c5c4113dSnw141292 else 3819651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 38208e228215Sdm199847 3821cd37da74Snw141292 req->id2name = canonname; 38228e228215Sdm199847 if (req->id2name != NULL) { 38238e228215Sdm199847 req->id2domain = strdup(windomain); 3824479ac375Sdm199847 if (req->id2domain == NULL) 3825479ac375Sdm199847 retcode = IDMAP_ERR_MEMORY; 38268e228215Sdm199847 } 3827c5c4113dSnw141292 } 3828479ac375Sdm199847 3829479ac375Sdm199847 if (retcode == IDMAP_SUCCESS) { 383048258c6bSjp151216 idmap_namerule_set(rule, values[1], values[0], values[4], 383148258c6bSjp151216 is_user, strtol(values[3], &end, 10), 383248258c6bSjp151216 strtol(values[5], &end, 10), 383348258c6bSjp151216 rule->direction); 383448258c6bSjp151216 res->info.src = IDMAP_MAP_SRC_NEW; 3835c5c4113dSnw141292 } 383662c60062Sbaban if (vm != NULL) 3837c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3838c5c4113dSnw141292 return (retcode); 3839c5c4113dSnw141292 } 3840c5c4113dSnw141292 3841cd37da74Snw141292 /* 3842cd37da74Snw141292 * Convention when processing unix2win requests: 3843cd37da74Snw141292 * 3844cd37da74Snw141292 * Unix identity: 3845cd37da74Snw141292 * req->id1name = 3846cd37da74Snw141292 * unixname if given otherwise unixname found will be placed 3847cd37da74Snw141292 * here. 3848cd37da74Snw141292 * req->id1domain = 3849cd37da74Snw141292 * NOT USED 3850cd37da74Snw141292 * req->id1.idtype = 3851cd37da74Snw141292 * Given type (IDMAP_UID or IDMAP_GID) 3852cd37da74Snw141292 * req->id1..[uid or gid] = 3853cd37da74Snw141292 * UID/GID if given otherwise UID/GID found will be placed here. 3854cd37da74Snw141292 * 3855cd37da74Snw141292 * Windows identity: 3856cd37da74Snw141292 * req->id2name = 3857cd37da74Snw141292 * winname found will be placed here. 3858cd37da74Snw141292 * req->id2domain = 3859cd37da74Snw141292 * windomain found will be placed here. 3860cd37da74Snw141292 * res->id.idtype = 3861cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 3862cd37da74Snw141292 * it is IDMAP_SID then actual type (IDMAP_USID/GSID) found 3863cd37da74Snw141292 * will be placed here. 3864cd37da74Snw141292 * req->id..sid.[prefix, rid] = 3865cd37da74Snw141292 * SID found will be placed here. 3866cd37da74Snw141292 * 3867cd37da74Snw141292 * Others: 3868cd37da74Snw141292 * res->retcode = 3869cd37da74Snw141292 * Return status for this request will be placed here. 3870cd37da74Snw141292 * res->direction = 3871cd37da74Snw141292 * Direction found will be placed here. Direction 3872cd37da74Snw141292 * meaning whether the resultant mapping is valid 3873cd37da74Snw141292 * only from unix2win or bi-directional. 3874cd37da74Snw141292 * req->direction = 3875cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 3876cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 3877cd37da74Snw141292 * of the request. 3878cd37da74Snw141292 * req->id2.idtype = 3879cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 3880cd37da74Snw141292 * type and is used to initialize res->id.idtype. 3881cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 3882cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 3883cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 3884cd37da74Snw141292 * mis-interpreting the contents of req->id2. 3885cd37da74Snw141292 * req->id2..[uid or gid or sid] = 3886cd37da74Snw141292 * NOT USED 3887cd37da74Snw141292 */ 3888cd37da74Snw141292 3889cd37da74Snw141292 /* 3890cd37da74Snw141292 * This function does the following: 3891cd37da74Snw141292 * 1. Lookup well-known SIDs table. 3892cd37da74Snw141292 * 2. Lookup cache. 3893cd37da74Snw141292 * 3. Check if the client does not want new mapping to be allocated 3894cd37da74Snw141292 * in which case this pass is the final pass. 3895e8c27ec8Sbaban * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs 3896e8c27ec8Sbaban * to do AD/NLDAP lookup. 3897cd37da74Snw141292 */ 3898c5c4113dSnw141292 idmap_retcode 3899479ac375Sdm199847 pid2sid_first_pass(lookup_state_t *state, idmap_mapping *req, 3900479ac375Sdm199847 idmap_id_res *res, int is_user, int getname) 3901cd37da74Snw141292 { 3902e8c27ec8Sbaban idmap_retcode retcode; 3903e8c27ec8Sbaban bool_t gen_localsid_on_err = FALSE; 3904c5c4113dSnw141292 3905e8c27ec8Sbaban /* Initialize result */ 3906c5c4113dSnw141292 res->id.idtype = req->id2.idtype; 3907e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 3908c5c4113dSnw141292 3909e8c27ec8Sbaban if (req->id2.idmap_id_u.sid.prefix != NULL) { 3910e8c27ec8Sbaban /* sanitize sidprefix */ 3911e8c27ec8Sbaban free(req->id2.idmap_id_u.sid.prefix); 3912e8c27ec8Sbaban req->id2.idmap_id_u.sid.prefix = NULL; 3913e8c27ec8Sbaban } 3914e8c27ec8Sbaban 391548258c6bSjp151216 /* Find pid */ 391648258c6bSjp151216 if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 391748258c6bSjp151216 if (ns_lookup_byname(req->id1name, NULL, &req->id1) 391848258c6bSjp151216 != IDMAP_SUCCESS) { 391948258c6bSjp151216 retcode = IDMAP_ERR_NOMAPPING; 392048258c6bSjp151216 goto out; 392148258c6bSjp151216 } 392248258c6bSjp151216 } 392348258c6bSjp151216 3924e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3925c5c4113dSnw141292 retcode = lookup_wksids_pid2sid(req, res, is_user); 3926c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3927c5c4113dSnw141292 goto out; 3928c5c4113dSnw141292 3929e8c27ec8Sbaban /* Lookup cache */ 3930479ac375Sdm199847 retcode = lookup_cache_pid2sid(state->cache, req, res, is_user, 3931479ac375Sdm199847 getname); 3932c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3933c5c4113dSnw141292 goto out; 3934c5c4113dSnw141292 3935c5c4113dSnw141292 /* Ephemeral ids cannot be allocated during pid2sid */ 3936c5c4113dSnw141292 if (IS_EPHEMERAL(req->id1.idmap_id_u.uid)) { 393762c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3938c5c4113dSnw141292 goto out; 3939c5c4113dSnw141292 } 3940c5c4113dSnw141292 394148258c6bSjp151216 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) { 394248258c6bSjp151216 retcode = IDMAP_ERR_NONEGENERATED; 394348258c6bSjp151216 goto out; 394448258c6bSjp151216 } 394548258c6bSjp151216 394648258c6bSjp151216 if (AVOID_NAMESERVICE(req)) { 3947e8c27ec8Sbaban gen_localsid_on_err = TRUE; 394862c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3949c5c4113dSnw141292 goto out; 3950c5c4113dSnw141292 } 3951c5c4113dSnw141292 3952e8c27ec8Sbaban /* Set flags for the next stage */ 3953e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) { 3954e8c27ec8Sbaban /* 3955e8c27ec8Sbaban * If AD-based name mapping is enabled then the next stage 3956e8c27ec8Sbaban * will need to lookup AD using unixname to get the 3957e8c27ec8Sbaban * corresponding winname. 3958e8c27ec8Sbaban */ 3959e8c27ec8Sbaban if (req->id1name == NULL) { 3960e8c27ec8Sbaban /* Get unixname if only pid is given. */ 3961e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, 3962e8c27ec8Sbaban is_user, &req->id1name); 3963479ac375Sdm199847 if (retcode != IDMAP_SUCCESS) { 3964479ac375Sdm199847 gen_localsid_on_err = TRUE; 3965e8c27ec8Sbaban goto out; 3966c5c4113dSnw141292 } 3967479ac375Sdm199847 } 3968e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 3969e8c27ec8Sbaban state->ad_nqueries++; 3970e8c27ec8Sbaban } else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) { 3971e8c27ec8Sbaban /* 3972e8c27ec8Sbaban * If native LDAP or mixed mode is enabled for name mapping 3973e8c27ec8Sbaban * then the next stage will need to lookup native LDAP using 3974e8c27ec8Sbaban * unixname/pid to get the corresponding winname. 3975e8c27ec8Sbaban */ 3976e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_NLDAP; 3977e8c27ec8Sbaban state->nldap_nqueries++; 3978c5c4113dSnw141292 } 3979c5c4113dSnw141292 3980e8c27ec8Sbaban /* 3981e8c27ec8Sbaban * Failed to find non-expired entry in cache. Set the flag to 3982e8c27ec8Sbaban * indicate that we are not done yet. 3983e8c27ec8Sbaban */ 3984e8c27ec8Sbaban state->pid2sid_done = FALSE; 3985e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 3986e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 3987e8c27ec8Sbaban 3988e8c27ec8Sbaban out: 3989e8c27ec8Sbaban res->retcode = idmap_stat4prot(retcode); 3990e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 3991e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 399248258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 3993e8c27ec8Sbaban return (retcode); 3994e8c27ec8Sbaban } 3995e8c27ec8Sbaban 3996e8c27ec8Sbaban idmap_retcode 3997479ac375Sdm199847 pid2sid_second_pass(lookup_state_t *state, idmap_mapping *req, 3998479ac375Sdm199847 idmap_id_res *res, int is_user) 3999e8c27ec8Sbaban { 4000e8c27ec8Sbaban bool_t gen_localsid_on_err = TRUE; 4001e8c27ec8Sbaban idmap_retcode retcode = IDMAP_SUCCESS; 4002e8c27ec8Sbaban 4003e8c27ec8Sbaban /* Check if second pass is needed */ 4004e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 4005e8c27ec8Sbaban return (res->retcode); 4006e8c27ec8Sbaban 4007e8c27ec8Sbaban /* Get status from previous pass */ 4008e8c27ec8Sbaban retcode = res->retcode; 4009e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4010e8c27ec8Sbaban goto out; 4011e8c27ec8Sbaban 4012e8c27ec8Sbaban /* 4013e8c27ec8Sbaban * If directory-based name mapping is enabled then the winname 4014e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode) 4015479ac375Sdm199847 * or from native LDAP object (nldap-mode or mixed-mode). 4016479ac375Sdm199847 * Note that if we have winname but no SID then it's an error 4017479ac375Sdm199847 * because this implies that the Native LDAP entry contains 4018479ac375Sdm199847 * winname which does not exist and it's better that we return 4019479ac375Sdm199847 * an error instead of doing rule-based mapping so that the user 4020479ac375Sdm199847 * can detect the issue and take appropriate action. 4021e8c27ec8Sbaban */ 4022479ac375Sdm199847 if (req->id2name != NULL) { 4023479ac375Sdm199847 /* Return notfound if we've winname but no SID. */ 4024479ac375Sdm199847 if (res->id.idmap_id_u.sid.prefix == NULL) { 4025479ac375Sdm199847 retcode = IDMAP_ERR_NOTFOUND; 4026479ac375Sdm199847 goto out; 4027479ac375Sdm199847 } 4028e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) 4029e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4030e8c27ec8Sbaban else if (NLDAP_MODE(req->id1.idtype, state)) 4031e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 4032e8c27ec8Sbaban else if (MIXED_MODE(req->id1.idtype, state)) 4033e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 4034e8c27ec8Sbaban goto out; 4035479ac375Sdm199847 } else if (res->id.idmap_id_u.sid.prefix != NULL) { 4036479ac375Sdm199847 /* 4037479ac375Sdm199847 * We've SID but no winname. This is fine because 4038479ac375Sdm199847 * the caller may have only requested SID. 4039479ac375Sdm199847 */ 4040479ac375Sdm199847 goto out; 4041e8c27ec8Sbaban } 4042e8c27ec8Sbaban 4043479ac375Sdm199847 /* Free any mapping info from Directory based mapping */ 4044479ac375Sdm199847 if (res->info.how.map_type != IDMAP_MAP_TYPE_UNKNOWN) 4045479ac375Sdm199847 idmap_info_free(&res->info); 4046479ac375Sdm199847 4047e8c27ec8Sbaban if (req->id1name == NULL) { 4048e8c27ec8Sbaban /* Get unixname from name service */ 4049e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user, 4050e8c27ec8Sbaban &req->id1name); 4051e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4052e8c27ec8Sbaban goto out; 4053e8c27ec8Sbaban } else if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 4054e8c27ec8Sbaban /* Get pid from name service */ 4055e8c27ec8Sbaban retcode = ns_lookup_byname(req->id1name, NULL, &req->id1); 4056e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4057e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4058e8c27ec8Sbaban goto out; 4059e8c27ec8Sbaban } 4060e8c27ec8Sbaban } 4061e8c27ec8Sbaban 4062e8c27ec8Sbaban /* Use unixname to evaluate local name-based mapping rules */ 4063479ac375Sdm199847 retcode = name_based_mapping_pid2sid(state, req->id1name, is_user, 4064c5c4113dSnw141292 req, res); 4065c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 406648258c6bSjp151216 retcode = generate_localsid(req, res, is_user, FALSE); 4067e8c27ec8Sbaban gen_localsid_on_err = FALSE; 4068e8c27ec8Sbaban } 4069c5c4113dSnw141292 4070c5c4113dSnw141292 out: 4071c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 4072e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 4073e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 4074479ac375Sdm199847 free(req->id2name); 4075479ac375Sdm199847 req->id2name = NULL; 4076479ac375Sdm199847 free(req->id2domain); 4077479ac375Sdm199847 req->id2domain = NULL; 4078e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 407948258c6bSjp151216 (void) generate_localsid(req, res, is_user, TRUE); 4080479ac375Sdm199847 else 4081479ac375Sdm199847 res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 4082e8c27ec8Sbaban } 4083e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 4084e8c27ec8Sbaban state->pid2sid_done = FALSE; 4085c5c4113dSnw141292 return (retcode); 4086c5c4113dSnw141292 } 4087c5c4113dSnw141292 4088cd37da74Snw141292 static 4089cd37da74Snw141292 int 4090651c0131Sbaban copy_mapping_request(idmap_mapping *mapping, idmap_mapping *request) 4091c5c4113dSnw141292 { 4092651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 4093651c0131Sbaban 4094c5c4113dSnw141292 mapping->flag = request->flag; 4095e8c27ec8Sbaban mapping->direction = _IDMAP_F_DONE; 4096651c0131Sbaban mapping->id2.idtype = request->id2.idtype; 4097c5c4113dSnw141292 4098c5c4113dSnw141292 mapping->id1.idtype = request->id1.idtype; 4099cd37da74Snw141292 if (IS_REQUEST_SID(*request, 1)) { 4100c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.rid = 4101c5c4113dSnw141292 request->id1.idmap_id_u.sid.rid; 4102651c0131Sbaban if (!EMPTY_STRING(request->id1.idmap_id_u.sid.prefix)) { 4103c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.prefix = 4104c5c4113dSnw141292 strdup(request->id1.idmap_id_u.sid.prefix); 4105651c0131Sbaban if (mapping->id1.idmap_id_u.sid.prefix == NULL) 41068e228215Sdm199847 goto errout; 4107651c0131Sbaban } 4108c5c4113dSnw141292 } else { 4109c5c4113dSnw141292 mapping->id1.idmap_id_u.uid = request->id1.idmap_id_u.uid; 4110c5c4113dSnw141292 } 4111c5c4113dSnw141292 4112e8c27ec8Sbaban if (!EMPTY_STRING(request->id1domain)) { 41138e228215Sdm199847 mapping->id1domain = strdup(request->id1domain); 41148e228215Sdm199847 if (mapping->id1domain == NULL) 41158e228215Sdm199847 goto errout; 4116e8c27ec8Sbaban } 4117c5c4113dSnw141292 4118e8c27ec8Sbaban if (!EMPTY_STRING(request->id1name)) { 41198e228215Sdm199847 mapping->id1name = strdup(request->id1name); 41208e228215Sdm199847 if (mapping->id1name == NULL) 41218e228215Sdm199847 goto errout; 4122e8c27ec8Sbaban } 4123c5c4113dSnw141292 4124651c0131Sbaban /* We don't need the rest of the request i.e request->id2 */ 4125651c0131Sbaban return (0); 4126c5c4113dSnw141292 4127651c0131Sbaban errout: 41288e228215Sdm199847 if (mapping->id1.idmap_id_u.sid.prefix != NULL) 4129651c0131Sbaban free(mapping->id1.idmap_id_u.sid.prefix); 41308e228215Sdm199847 if (mapping->id1domain != NULL) 41318e228215Sdm199847 free(mapping->id1domain); 41328e228215Sdm199847 if (mapping->id1name != NULL) 41338e228215Sdm199847 free(mapping->id1name); 4134651c0131Sbaban 4135651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 4136651c0131Sbaban return (-1); 4137c5c4113dSnw141292 } 4138c5c4113dSnw141292 4139c5c4113dSnw141292 4140c5c4113dSnw141292 idmap_retcode 4141c5c4113dSnw141292 get_w2u_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 4142cd37da74Snw141292 idmap_mapping *mapping) 4143cd37da74Snw141292 { 4144c5c4113dSnw141292 idmap_id_res idres; 4145c5c4113dSnw141292 lookup_state_t state; 4146dd5829d1Sbaban char *cp; 4147c5c4113dSnw141292 idmap_retcode retcode; 4148c5c4113dSnw141292 const char *winname, *windomain; 4149c5c4113dSnw141292 4150c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4151c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 4152479ac375Sdm199847 state.cache = cache; 4153479ac375Sdm199847 state.db = db; 4154c5c4113dSnw141292 4155e8c27ec8Sbaban /* Get directory-based name mapping info */ 4156479ac375Sdm199847 retcode = load_cfg_in_state(&state); 4157e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4158c5c4113dSnw141292 goto out; 4159c5c4113dSnw141292 4160e8c27ec8Sbaban /* 4161e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 4162e8c27ec8Sbaban * empty strings are not copied from "request" to 4163e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 4164e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 4165e8c27ec8Sbaban * check for empty strings henceforth. 4166e8c27ec8Sbaban */ 4167651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 4168651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 4169651c0131Sbaban goto out; 4170651c0131Sbaban } 4171c5c4113dSnw141292 41728e228215Sdm199847 winname = mapping->id1name; 41738e228215Sdm199847 windomain = mapping->id1domain; 4174c5c4113dSnw141292 4175e8c27ec8Sbaban if (winname == NULL && windomain != NULL) { 4176c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 4177c5c4113dSnw141292 goto out; 4178c5c4113dSnw141292 } 4179c5c4113dSnw141292 4180e8c27ec8Sbaban /* Need atleast winname or sid to proceed */ 4181e8c27ec8Sbaban if (winname == NULL && mapping->id1.idmap_id_u.sid.prefix == NULL) { 4182e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 4183e8c27ec8Sbaban goto out; 4184e8c27ec8Sbaban } 4185e8c27ec8Sbaban 4186e8c27ec8Sbaban /* 4187e8c27ec8Sbaban * If domainname is not given but we have a fully qualified 4188e8c27ec8Sbaban * winname then extract the domainname from the winname, 4189e8c27ec8Sbaban * otherwise use the default_domain from the config 4190e8c27ec8Sbaban */ 4191e8c27ec8Sbaban if (winname != NULL && windomain == NULL) { 41928e228215Sdm199847 retcode = IDMAP_SUCCESS; 4193dd5829d1Sbaban if ((cp = strchr(winname, '@')) != NULL) { 4194dd5829d1Sbaban *cp = '\0'; 41958e228215Sdm199847 mapping->id1domain = strdup(cp + 1); 41968e228215Sdm199847 if (mapping->id1domain == NULL) 41978e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 4198e8c27ec8Sbaban } else if (lookup_wksids_name2sid(winname, NULL, NULL, NULL, 4199e8c27ec8Sbaban NULL) != IDMAP_SUCCESS) { 4200*82da9f60Sbaban if (state.defdom == NULL) { 4201*82da9f60Sbaban /* 4202*82da9f60Sbaban * We have a non-qualified winname which is 4203*82da9f60Sbaban * neither the name of a well-known SID nor 4204*82da9f60Sbaban * there is a default domain with which we can 4205*82da9f60Sbaban * qualify it. 4206*82da9f60Sbaban */ 4207*82da9f60Sbaban retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 4208*82da9f60Sbaban } else { 4209479ac375Sdm199847 mapping->id1domain = strdup(state.defdom); 42108e228215Sdm199847 if (mapping->id1domain == NULL) 42118e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 42128e228215Sdm199847 } 4213*82da9f60Sbaban } 4214c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 4215c5c4113dSnw141292 goto out; 4216c5c4113dSnw141292 } 4217c5c4113dSnw141292 4218e8c27ec8Sbaban /* 4219e8c27ec8Sbaban * First pass looks up the well-known SIDs table and cache 4220e8c27ec8Sbaban * and handles localSIDs 4221e8c27ec8Sbaban */ 4222c5c4113dSnw141292 state.sid2pid_done = TRUE; 4223479ac375Sdm199847 retcode = sid2pid_first_pass(&state, mapping, &idres); 4224c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 4225c5c4113dSnw141292 goto out; 4226c5c4113dSnw141292 4227479ac375Sdm199847 /* AD lookup */ 4228e8c27ec8Sbaban if (state.ad_nqueries > 0) { 4229479ac375Sdm199847 retcode = ad_lookup_one(&state, mapping, &idres); 4230e8c27ec8Sbaban if (IDMAP_ERROR(retcode)) 4231e8c27ec8Sbaban goto out; 4232c5c4113dSnw141292 } 4233c5c4113dSnw141292 4234479ac375Sdm199847 /* nldap lookup */ 4235479ac375Sdm199847 if (state.nldap_nqueries > 0) { 4236479ac375Sdm199847 retcode = nldap_lookup_one(&state, mapping, &idres); 4237479ac375Sdm199847 if (IDMAP_FATAL_ERROR(retcode)) 4238e8c27ec8Sbaban goto out; 423948258c6bSjp151216 } 4240e8c27ec8Sbaban 4241e8c27ec8Sbaban /* Next pass performs name-based mapping and ephemeral mapping. */ 4242c5c4113dSnw141292 state.sid2pid_done = TRUE; 4243479ac375Sdm199847 retcode = sid2pid_second_pass(&state, mapping, &idres); 4244c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 4245c5c4113dSnw141292 goto out; 4246c5c4113dSnw141292 4247c5c4113dSnw141292 /* Update cache */ 4248479ac375Sdm199847 (void) update_cache_sid2pid(&state, mapping, &idres); 4249c5c4113dSnw141292 4250c5c4113dSnw141292 out: 4251e8c27ec8Sbaban /* 4252e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 4253e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 4254e8c27ec8Sbaban * free idres. 4255e8c27ec8Sbaban */ 4256c5c4113dSnw141292 mapping->direction = idres.direction; 4257c5c4113dSnw141292 mapping->id2 = idres.id; 425848258c6bSjp151216 if (mapping->flag & IDMAP_REQ_FLG_MAPPING_INFO || 425948258c6bSjp151216 retcode != IDMAP_SUCCESS) 426048258c6bSjp151216 (void) idmap_info_mov(&mapping->info, &idres.info); 426148258c6bSjp151216 else 426248258c6bSjp151216 idmap_info_free(&idres.info); 4263c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4264e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 426562c60062Sbaban mapping->id2.idmap_id_u.uid = UID_NOBODY; 4266c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 4267e8c27ec8Sbaban cleanup_lookup_state(&state); 4268c5c4113dSnw141292 return (retcode); 4269c5c4113dSnw141292 } 4270c5c4113dSnw141292 4271c5c4113dSnw141292 idmap_retcode 4272c5c4113dSnw141292 get_u2w_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 4273cd37da74Snw141292 idmap_mapping *mapping, int is_user) 4274cd37da74Snw141292 { 4275c5c4113dSnw141292 idmap_id_res idres; 4276c5c4113dSnw141292 lookup_state_t state; 4277c5c4113dSnw141292 idmap_retcode retcode; 4278c5c4113dSnw141292 4279c5c4113dSnw141292 /* 4280c5c4113dSnw141292 * In order to re-use the pid2sid code, we convert 4281c5c4113dSnw141292 * our input data into structs that are expected by 4282c5c4113dSnw141292 * pid2sid_first_pass. 4283c5c4113dSnw141292 */ 4284c5c4113dSnw141292 4285c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4286c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 4287479ac375Sdm199847 state.cache = cache; 4288479ac375Sdm199847 state.db = db; 4289c5c4113dSnw141292 4290e8c27ec8Sbaban /* Get directory-based name mapping info */ 4291479ac375Sdm199847 retcode = load_cfg_in_state(&state); 4292e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4293e8c27ec8Sbaban goto out; 4294e8c27ec8Sbaban 4295e8c27ec8Sbaban /* 4296e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 4297e8c27ec8Sbaban * empty strings are not copied from "request" to 4298e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 4299e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 4300e8c27ec8Sbaban * check for empty strings henceforth. 4301e8c27ec8Sbaban */ 4302651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 4303651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 4304651c0131Sbaban goto out; 4305651c0131Sbaban } 4306c5c4113dSnw141292 4307e8c27ec8Sbaban /* 4308e8c27ec8Sbaban * For unix to windows mapping request, we need atleast a 4309e8c27ec8Sbaban * unixname or uid/gid to proceed 4310e8c27ec8Sbaban */ 4311e8c27ec8Sbaban if (mapping->id1name == NULL && 4312cf5b5989Sdm199847 mapping->id1.idmap_id_u.uid == SENTINEL_PID) { 4313c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 4314c5c4113dSnw141292 goto out; 4315c5c4113dSnw141292 } 4316c5c4113dSnw141292 4317e8c27ec8Sbaban /* First pass looks up cache and well-known SIDs */ 4318e8c27ec8Sbaban state.pid2sid_done = TRUE; 4319479ac375Sdm199847 retcode = pid2sid_first_pass(&state, mapping, &idres, is_user, 1); 4320e8c27ec8Sbaban if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 4321c5c4113dSnw141292 goto out; 4322e8c27ec8Sbaban 4323479ac375Sdm199847 /* nldap lookup */ 4324e8c27ec8Sbaban if (state.nldap_nqueries > 0) { 4325479ac375Sdm199847 retcode = nldap_lookup_one(&state, mapping, &idres); 4326e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 4327c5c4113dSnw141292 goto out; 4328479ac375Sdm199847 } 4329e8c27ec8Sbaban 4330479ac375Sdm199847 /* AD lookup */ 4331479ac375Sdm199847 if (state.ad_nqueries > 0) { 4332479ac375Sdm199847 retcode = ad_lookup_one(&state, mapping, &idres); 4333e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 4334e8c27ec8Sbaban goto out; 4335c5c4113dSnw141292 } 4336c5c4113dSnw141292 4337e8c27ec8Sbaban /* 4338e8c27ec8Sbaban * Next pass processes the result of the preceding passes/lookups. 4339e8c27ec8Sbaban * It returns if there's nothing more to be done otherwise it 4340e8c27ec8Sbaban * evaluates local name-based mapping rules 4341e8c27ec8Sbaban */ 4342c5c4113dSnw141292 state.pid2sid_done = TRUE; 4343479ac375Sdm199847 retcode = pid2sid_second_pass(&state, mapping, &idres, is_user); 4344c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 4345c5c4113dSnw141292 goto out; 4346c5c4113dSnw141292 4347c5c4113dSnw141292 /* Update cache */ 4348479ac375Sdm199847 (void) update_cache_pid2sid(&state, mapping, &idres); 4349c5c4113dSnw141292 4350c5c4113dSnw141292 out: 4351e8c27ec8Sbaban /* 4352e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 4353e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 4354e8c27ec8Sbaban * free idres. 4355e8c27ec8Sbaban */ 4356c5c4113dSnw141292 mapping->direction = idres.direction; 4357c5c4113dSnw141292 mapping->id2 = idres.id; 435848258c6bSjp151216 if (mapping->flag & IDMAP_REQ_FLG_MAPPING_INFO || 435948258c6bSjp151216 retcode != IDMAP_SUCCESS) 436048258c6bSjp151216 (void) idmap_info_mov(&mapping->info, &idres.info); 436148258c6bSjp151216 else 436248258c6bSjp151216 idmap_info_free(&idres.info); 4363c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 4364c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 4365e8c27ec8Sbaban cleanup_lookup_state(&state); 4366e8c27ec8Sbaban return (retcode); 4367e8c27ec8Sbaban } 4368e8c27ec8Sbaban 4369479ac375Sdm199847 /*ARGSUSED*/ 4370e8c27ec8Sbaban static 4371e8c27ec8Sbaban idmap_retcode 4372479ac375Sdm199847 ad_lookup_one(lookup_state_t *state, idmap_mapping *req, idmap_id_res *res) 4373e8c27ec8Sbaban { 4374479ac375Sdm199847 idmap_mapping_batch batch; 4375479ac375Sdm199847 idmap_ids_res result; 4376e8c27ec8Sbaban 4377479ac375Sdm199847 batch.idmap_mapping_batch_len = 1; 4378479ac375Sdm199847 batch.idmap_mapping_batch_val = req; 4379479ac375Sdm199847 result.ids.ids_len = 1; 4380479ac375Sdm199847 result.ids.ids_val = res; 4381479ac375Sdm199847 return (ad_lookup_batch(state, &batch, &result)); 4382c5c4113dSnw141292 } 4383