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 /* 22c5c4113dSnw141292 * Copyright 2007 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 32c8e26105Sjp151216 #include <atomic.h> 33c5c4113dSnw141292 #include <stdio.h> 34c5c4113dSnw141292 #include <stdlib.h> 35c5c4113dSnw141292 #include <string.h> 36c5c4113dSnw141292 #include <errno.h> 37c5c4113dSnw141292 #include <sys/types.h> 38c5c4113dSnw141292 #include <sys/stat.h> 39c5c4113dSnw141292 #include <rpc/rpc.h> 40c5c4113dSnw141292 #include <sys/sid.h> 41c5c4113dSnw141292 #include <time.h> 42c5c4113dSnw141292 #include <pwd.h> 43c5c4113dSnw141292 #include <grp.h> 4484decf41Sjp151216 #include <pthread.h> 4584decf41Sjp151216 #include <assert.h> 46cd37da74Snw141292 #include <sys/u8_textprep.h> 47c5c4113dSnw141292 48c5c4113dSnw141292 #include "idmapd.h" 49c5c4113dSnw141292 #include "adutils.h" 50c5c4113dSnw141292 #include "string.h" 51c5c4113dSnw141292 #include "idmap_priv.h" 52cd37da74Snw141292 #include "schema.h" 53e8c27ec8Sbaban #include "nldaputils.h" 54c5c4113dSnw141292 5584decf41Sjp151216 56c8e26105Sjp151216 static int degraded = 0; /* whether the FMRI has been marked degraded */ 57c8e26105Sjp151216 58c5c4113dSnw141292 static idmap_retcode sql_compile_n_step_once(sqlite *, char *, 59c5c4113dSnw141292 sqlite_vm **, int *, int, const char ***); 60cd37da74Snw141292 static idmap_retcode lookup_wksids_name2sid(const char *, char **, char **, 6162c60062Sbaban idmap_rid_t *, int *); 62e8c27ec8Sbaban static idmap_retcode ad_lookup(lookup_state_t *, idmap_mapping *, 63e8c27ec8Sbaban idmap_id_res *, int, int, int); 64e8c27ec8Sbaban static idmap_retcode lookup_localsid2pid(idmap_mapping *, idmap_id_res *); 65e8c27ec8Sbaban static idmap_retcode lookup_cache_name2sid(sqlite *, const char *, 66e8c27ec8Sbaban const char *, char **, char **, idmap_rid_t *, int *); 67e8c27ec8Sbaban 68c5c4113dSnw141292 69c5c4113dSnw141292 #define EMPTY_NAME(name) (*name == 0 || strcmp(name, "\"\"") == 0) 70c5c4113dSnw141292 71c5c4113dSnw141292 #define DO_NOT_ALLOC_NEW_ID_MAPPING(req)\ 72c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC) 73c5c4113dSnw141292 74c5c4113dSnw141292 #define AVOID_NAMESERVICE(req)\ 75c5c4113dSnw141292 (req->flag & IDMAP_REQ_FLG_NO_NAMESERVICE) 76c5c4113dSnw141292 77e8c27ec8Sbaban #define IS_EPHEMERAL(pid) (pid > INT32_MAX && pid != SENTINEL_PID) 78c5c4113dSnw141292 79c5c4113dSnw141292 #define LOCALRID_MIN 1000 80c5c4113dSnw141292 81c5c4113dSnw141292 82c5c4113dSnw141292 typedef enum init_db_option { 83c5c4113dSnw141292 FAIL_IF_CORRUPT = 0, 84c5c4113dSnw141292 REMOVE_IF_CORRUPT = 1 85c5c4113dSnw141292 } init_db_option_t; 86c5c4113dSnw141292 8784decf41Sjp151216 /* 88e8c27ec8Sbaban * Data structure to store well-known SIDs and 89e8c27ec8Sbaban * associated mappings (if any) 90e8c27ec8Sbaban */ 91e8c27ec8Sbaban typedef struct wksids_table { 92e8c27ec8Sbaban const char *sidprefix; 93e8c27ec8Sbaban uint32_t rid; 94e8c27ec8Sbaban const char *winname; 95e8c27ec8Sbaban int is_wuser; 96e8c27ec8Sbaban uid_t pid; 97e8c27ec8Sbaban int is_user; 98e8c27ec8Sbaban int direction; 99e8c27ec8Sbaban } wksids_table_t; 100e8c27ec8Sbaban 101e8c27ec8Sbaban /* 10284decf41Sjp151216 * Thread specfic data to hold the database handles so that the 10384decf41Sjp151216 * databaes are not opened and closed for every request. It also 10484decf41Sjp151216 * contains the sqlite busy handler structure. 10584decf41Sjp151216 */ 10684decf41Sjp151216 10784decf41Sjp151216 struct idmap_busy { 10884decf41Sjp151216 const char *name; 10984decf41Sjp151216 const int *delays; 11084decf41Sjp151216 int delay_size; 11184decf41Sjp151216 int total; 11284decf41Sjp151216 int sec; 11384decf41Sjp151216 }; 11484decf41Sjp151216 11584decf41Sjp151216 11684decf41Sjp151216 typedef struct idmap_tsd { 11784decf41Sjp151216 sqlite *db_db; 11884decf41Sjp151216 sqlite *cache_db; 11984decf41Sjp151216 struct idmap_busy cache_busy; 12084decf41Sjp151216 struct idmap_busy db_busy; 12184decf41Sjp151216 } idmap_tsd_t; 12284decf41Sjp151216 12384decf41Sjp151216 12484decf41Sjp151216 12584decf41Sjp151216 static const int cache_delay_table[] = 12684decf41Sjp151216 { 1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 12784decf41Sjp151216 50, 50, 60, 70, 80, 90, 100}; 12884decf41Sjp151216 12984decf41Sjp151216 static const int db_delay_table[] = 13084decf41Sjp151216 { 5, 10, 15, 20, 30, 40, 55, 70, 100}; 13184decf41Sjp151216 13284decf41Sjp151216 13384decf41Sjp151216 static pthread_key_t idmap_tsd_key; 13484decf41Sjp151216 13584decf41Sjp151216 void 13684decf41Sjp151216 idmap_tsd_destroy(void *key) 13784decf41Sjp151216 { 13884decf41Sjp151216 13984decf41Sjp151216 idmap_tsd_t *tsd = (idmap_tsd_t *)key; 14084decf41Sjp151216 if (tsd) { 14184decf41Sjp151216 if (tsd->db_db) 14284decf41Sjp151216 (void) sqlite_close(tsd->db_db); 14384decf41Sjp151216 if (tsd->cache_db) 14484decf41Sjp151216 (void) sqlite_close(tsd->cache_db); 14584decf41Sjp151216 free(tsd); 14684decf41Sjp151216 } 14784decf41Sjp151216 } 14884decf41Sjp151216 14984decf41Sjp151216 int 150cd37da74Snw141292 idmap_init_tsd_key(void) 151cd37da74Snw141292 { 15284decf41Sjp151216 return (pthread_key_create(&idmap_tsd_key, idmap_tsd_destroy)); 15384decf41Sjp151216 } 15484decf41Sjp151216 15584decf41Sjp151216 15684decf41Sjp151216 15784decf41Sjp151216 idmap_tsd_t * 15884decf41Sjp151216 idmap_get_tsd(void) 15984decf41Sjp151216 { 16084decf41Sjp151216 idmap_tsd_t *tsd; 16184decf41Sjp151216 16284decf41Sjp151216 if ((tsd = pthread_getspecific(idmap_tsd_key)) == NULL) { 16384decf41Sjp151216 /* No thread specific data so create it */ 16484decf41Sjp151216 if ((tsd = malloc(sizeof (*tsd))) != NULL) { 16584decf41Sjp151216 /* Initialize thread specific data */ 16684decf41Sjp151216 (void) memset(tsd, 0, sizeof (*tsd)); 16784decf41Sjp151216 /* save the trhread specific data */ 16884decf41Sjp151216 if (pthread_setspecific(idmap_tsd_key, tsd) != 0) { 16984decf41Sjp151216 /* Can't store key */ 17084decf41Sjp151216 free(tsd); 17184decf41Sjp151216 tsd = NULL; 17284decf41Sjp151216 } 17384decf41Sjp151216 } else { 17484decf41Sjp151216 tsd = NULL; 17584decf41Sjp151216 } 17684decf41Sjp151216 } 17784decf41Sjp151216 17884decf41Sjp151216 return (tsd); 17984decf41Sjp151216 } 18084decf41Sjp151216 181cd37da74Snw141292 static 182cd37da74Snw141292 const char * 183cd37da74Snw141292 get_fmri(void) 184cd37da74Snw141292 { 185cd37da74Snw141292 static char *fmri = NULL; 186cd37da74Snw141292 static char buf[60]; 187cd37da74Snw141292 char *s; 188cd37da74Snw141292 189cd37da74Snw141292 membar_consumer(); 190cd37da74Snw141292 s = fmri; 191cd37da74Snw141292 if (s != NULL && *s == '\0') 192cd37da74Snw141292 return (NULL); 193cd37da74Snw141292 else if (s != NULL) 194cd37da74Snw141292 return (s); 195cd37da74Snw141292 196cd37da74Snw141292 if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf)) 197cd37da74Snw141292 buf[0] = '\0'; 198cd37da74Snw141292 else 199cd37da74Snw141292 (void) strlcpy(buf, s, sizeof (buf)); 200cd37da74Snw141292 201cd37da74Snw141292 membar_producer(); 202cd37da74Snw141292 fmri = buf; 203cd37da74Snw141292 204cd37da74Snw141292 return (get_fmri()); 205cd37da74Snw141292 } 206cd37da74Snw141292 207c8e26105Sjp151216 /* 208c8e26105Sjp151216 * Wrappers for smf_degrade/restore_instance() 209c8e26105Sjp151216 * 210c8e26105Sjp151216 * smf_restore_instance() is too heavy duty to be calling every time we 211c8e26105Sjp151216 * have a successful AD name<->SID lookup. 212c8e26105Sjp151216 */ 213c8e26105Sjp151216 void 214c8e26105Sjp151216 degrade_svc(void) 215c8e26105Sjp151216 { 216cd37da74Snw141292 const char *fmri; 217cd37da74Snw141292 218cd37da74Snw141292 if ((fmri = get_fmri()) == NULL) 219cd37da74Snw141292 return; 220cd37da74Snw141292 221c8e26105Sjp151216 membar_consumer(); 222c8e26105Sjp151216 if (degraded) 223c8e26105Sjp151216 return; 224cd37da74Snw141292 (void) smf_degrade_instance(fmri, 0); 225c8e26105Sjp151216 membar_producer(); 226c8e26105Sjp151216 degraded = 1; 227c8e26105Sjp151216 } 228c8e26105Sjp151216 229c8e26105Sjp151216 230c8e26105Sjp151216 void 231c8e26105Sjp151216 restore_svc(void) 232c8e26105Sjp151216 { 233cd37da74Snw141292 const char *fmri; 234cd37da74Snw141292 235cd37da74Snw141292 if ((fmri = get_fmri()) == NULL) 236cd37da74Snw141292 return; 237cd37da74Snw141292 238c8e26105Sjp151216 membar_consumer(); 239c8e26105Sjp151216 if (!degraded) 240c8e26105Sjp151216 return; 241cd37da74Snw141292 (void) smf_restore_instance(fmri); 242c8e26105Sjp151216 membar_producer(); 243c8e26105Sjp151216 degraded = 0; 244c8e26105Sjp151216 } 24584decf41Sjp151216 246cd37da74Snw141292 /* 247cd37da74Snw141292 * A simple wrapper around u8_textprep_str() that returns the Unicode 248cd37da74Snw141292 * lower-case version of some string. The result must be freed. 249cd37da74Snw141292 */ 250cd37da74Snw141292 char * 251cd37da74Snw141292 tolower_u8(const char *s) 252cd37da74Snw141292 { 253cd37da74Snw141292 char *res = NULL; 254cd37da74Snw141292 char *outs; 255cd37da74Snw141292 size_t inlen, outlen, inbytesleft, outbytesleft; 256cd37da74Snw141292 int rc, err; 257cd37da74Snw141292 258cd37da74Snw141292 /* 259cd37da74Snw141292 * u8_textprep_str() does not allocate memory. The input and 260cd37da74Snw141292 * output buffers may differ in size (though that would be more 261cd37da74Snw141292 * likely when normalization is done). We have to loop over it... 262cd37da74Snw141292 * 263cd37da74Snw141292 * To improve the chances that we can avoid looping we add 10 264cd37da74Snw141292 * bytes of output buffer room the first go around. 265cd37da74Snw141292 */ 266cd37da74Snw141292 inlen = inbytesleft = strlen(s); 267cd37da74Snw141292 outlen = outbytesleft = inlen + 10; 268cd37da74Snw141292 if ((res = malloc(outlen)) == NULL) 269cd37da74Snw141292 return (NULL); 270cd37da74Snw141292 outs = res; 271cd37da74Snw141292 272cd37da74Snw141292 while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs, 273cd37da74Snw141292 &outbytesleft, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST, &err)) < 0 && 274cd37da74Snw141292 err == E2BIG) { 275cd37da74Snw141292 if ((res = realloc(res, outlen + inbytesleft)) == NULL) 276cd37da74Snw141292 return (NULL); 277cd37da74Snw141292 /* adjust input/output buffer pointers */ 278cd37da74Snw141292 s += (inlen - inbytesleft); 279cd37da74Snw141292 outs = res + outlen - outbytesleft; 280cd37da74Snw141292 /* adjust outbytesleft and outlen */ 281cd37da74Snw141292 outlen += inbytesleft; 282cd37da74Snw141292 outbytesleft += inbytesleft; 283cd37da74Snw141292 } 284cd37da74Snw141292 285cd37da74Snw141292 if (rc < 0) { 286cd37da74Snw141292 free(res); 287cd37da74Snw141292 res = NULL; 288cd37da74Snw141292 return (NULL); 289cd37da74Snw141292 } 290cd37da74Snw141292 291cd37da74Snw141292 res[outlen - outbytesleft] = '\0'; 292cd37da74Snw141292 293cd37da74Snw141292 return (res); 294cd37da74Snw141292 } 295cd37da74Snw141292 296cd37da74Snw141292 static int sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 297cd37da74Snw141292 const char *while_doing); 298cd37da74Snw141292 299c5c4113dSnw141292 300c5c4113dSnw141292 /* 301c5c4113dSnw141292 * Initialize 'dbname' using 'sql' 302c5c4113dSnw141292 */ 303cd37da74Snw141292 static 304cd37da74Snw141292 int 305cd37da74Snw141292 init_db_instance(const char *dbname, int version, 306cd37da74Snw141292 const char *detect_version_sql, char * const *sql, 307cd37da74Snw141292 init_db_option_t opt, int *created, int *upgraded) 308c5c4113dSnw141292 { 309cd37da74Snw141292 int rc, curr_version; 310cd37da74Snw141292 int tries = 1; 311cd37da74Snw141292 int prio = LOG_NOTICE; 312c5c4113dSnw141292 sqlite *db = NULL; 313cd37da74Snw141292 char *errmsg = NULL; 314c5c4113dSnw141292 315cd37da74Snw141292 *created = 0; 316cd37da74Snw141292 *upgraded = 0; 317c5c4113dSnw141292 318cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 319cd37da74Snw141292 tries = 3; 320cd37da74Snw141292 321cd37da74Snw141292 rinse_repeat: 322cd37da74Snw141292 if (tries == 0) { 323cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to initialize db %s", dbname); 324c5c4113dSnw141292 return (-1); 325cd37da74Snw141292 } 326cd37da74Snw141292 if (tries-- == 1) 327cd37da74Snw141292 /* Last try, log errors */ 328cd37da74Snw141292 prio = LOG_ERR; 329c5c4113dSnw141292 330cd37da74Snw141292 db = sqlite_open(dbname, 0600, &errmsg); 331cd37da74Snw141292 if (db == NULL) { 332cd37da74Snw141292 idmapdlog(prio, "Error creating database %s (%s)", 333cd37da74Snw141292 dbname, CHECK_NULL(errmsg)); 334cd37da74Snw141292 sqlite_freemem(errmsg); 335cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 336c5c4113dSnw141292 (void) unlink(dbname); 337cd37da74Snw141292 goto rinse_repeat; 338c5c4113dSnw141292 } 339c5c4113dSnw141292 340c5c4113dSnw141292 sqlite_busy_timeout(db, 3000); 341cd37da74Snw141292 342cd37da74Snw141292 /* Detect current version of schema in the db, if any */ 343cd37da74Snw141292 curr_version = 0; 344cd37da74Snw141292 if (detect_version_sql != NULL) { 345cd37da74Snw141292 char *end, **results; 346cd37da74Snw141292 int nrow; 347cd37da74Snw141292 348cd37da74Snw141292 #ifdef IDMAPD_DEBUG 349cd37da74Snw141292 (void) fprintf(stderr, "Schema version detection SQL: %s\n", 350cd37da74Snw141292 detect_version_sql); 351cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 352cd37da74Snw141292 rc = sqlite_get_table(db, detect_version_sql, &results, 353cd37da74Snw141292 &nrow, NULL, &errmsg); 354cd37da74Snw141292 if (rc != SQLITE_OK) { 355cd37da74Snw141292 idmapdlog(prio, 356cd37da74Snw141292 "Error detecting schema version of db %s (%s)", 357cd37da74Snw141292 dbname, errmsg); 358cd37da74Snw141292 sqlite_freemem(errmsg); 359cd37da74Snw141292 sqlite_free_table(results); 360c5c4113dSnw141292 sqlite_close(db); 361cd37da74Snw141292 return (-1); 362cd37da74Snw141292 } 363cd37da74Snw141292 if (nrow != 1) { 364cd37da74Snw141292 idmapdlog(prio, 365cd37da74Snw141292 "Error detecting schema version of db %s", dbname); 366cd37da74Snw141292 sqlite_close(db); 367cd37da74Snw141292 sqlite_free_table(results); 368cd37da74Snw141292 return (-1); 369cd37da74Snw141292 } 370cd37da74Snw141292 curr_version = strtol(results[1], &end, 10); 371cd37da74Snw141292 sqlite_free_table(results); 372c5c4113dSnw141292 } 373c5c4113dSnw141292 374cd37da74Snw141292 if (curr_version < 0) { 375cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 376cd37da74Snw141292 (void) unlink(dbname); 377cd37da74Snw141292 goto rinse_repeat; 378c5c4113dSnw141292 } 379c5c4113dSnw141292 380cd37da74Snw141292 if (curr_version == version) 381cd37da74Snw141292 goto done; 382cd37da74Snw141292 383cd37da74Snw141292 /* Install or upgrade schema */ 384cd37da74Snw141292 #ifdef IDMAPD_DEBUG 385cd37da74Snw141292 (void) fprintf(stderr, "Schema init/upgrade SQL: %s\n", 386cd37da74Snw141292 sql[curr_version]); 387cd37da74Snw141292 #endif /* IDMAPD_DEBUG */ 388cd37da74Snw141292 rc = sql_exec_tran_no_cb(db, sql[curr_version], dbname, 389cd37da74Snw141292 (curr_version == 0) ? "installing schema" : "upgrading schema"); 390cd37da74Snw141292 if (rc != 0) { 391cd37da74Snw141292 idmapdlog(prio, "Error %s schema for db %s", dbname, 392cd37da74Snw141292 (curr_version == 0) ? "installing schema" : 393cd37da74Snw141292 "upgrading schema"); 394cd37da74Snw141292 if (opt == REMOVE_IF_CORRUPT) 395cd37da74Snw141292 (void) unlink(dbname); 396cd37da74Snw141292 goto rinse_repeat; 397c5c4113dSnw141292 } 398c5c4113dSnw141292 399cd37da74Snw141292 *upgraded = (curr_version > 0); 400cd37da74Snw141292 *created = (curr_version == 0); 401cd37da74Snw141292 402cd37da74Snw141292 done: 403c5c4113dSnw141292 (void) sqlite_close(db); 404cd37da74Snw141292 return (0); 405c5c4113dSnw141292 } 406c5c4113dSnw141292 40784decf41Sjp151216 40884decf41Sjp151216 /* 40984decf41Sjp151216 * This is the SQLite database busy handler that retries the SQL 41084decf41Sjp151216 * operation until it is successful. 41184decf41Sjp151216 */ 41284decf41Sjp151216 int 41384decf41Sjp151216 /* LINTED E_FUNC_ARG_UNUSED */ 41484decf41Sjp151216 idmap_sqlite_busy_handler(void *arg, const char *table_name, int count) 41584decf41Sjp151216 { 41684decf41Sjp151216 struct idmap_busy *busy = arg; 41784decf41Sjp151216 int delay; 41884decf41Sjp151216 struct timespec rqtp; 41984decf41Sjp151216 42084decf41Sjp151216 if (count == 1) { 42184decf41Sjp151216 busy->total = 0; 42284decf41Sjp151216 busy->sec = 2; 42384decf41Sjp151216 } 42484decf41Sjp151216 if (busy->total > 1000 * busy->sec) { 42584decf41Sjp151216 idmapdlog(LOG_ERR, 42684decf41Sjp151216 "Thread %d waited %d sec for the %s database", 42784decf41Sjp151216 pthread_self(), busy->sec, busy->name); 42884decf41Sjp151216 busy->sec++; 42984decf41Sjp151216 } 43084decf41Sjp151216 43184decf41Sjp151216 if (count <= busy->delay_size) { 43284decf41Sjp151216 delay = busy->delays[count-1]; 43384decf41Sjp151216 } else { 43484decf41Sjp151216 delay = busy->delays[busy->delay_size - 1]; 43584decf41Sjp151216 } 43684decf41Sjp151216 busy->total += delay; 43784decf41Sjp151216 rqtp.tv_sec = 0; 43884decf41Sjp151216 rqtp.tv_nsec = delay * (NANOSEC / MILLISEC); 43984decf41Sjp151216 (void) nanosleep(&rqtp, NULL); 44084decf41Sjp151216 return (1); 44184decf41Sjp151216 } 44284decf41Sjp151216 44384decf41Sjp151216 444c5c4113dSnw141292 /* 445c5c4113dSnw141292 * Get the database handle 446c5c4113dSnw141292 */ 447c5c4113dSnw141292 idmap_retcode 448cd37da74Snw141292 get_db_handle(sqlite **db) 449cd37da74Snw141292 { 450c5c4113dSnw141292 char *errmsg; 45184decf41Sjp151216 idmap_tsd_t *tsd; 452c5c4113dSnw141292 453c5c4113dSnw141292 /* 45484decf41Sjp151216 * Retrieve the db handle from thread-specific storage 455c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 456c5c4113dSnw141292 */ 45784decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 45884decf41Sjp151216 idmapdlog(LOG_ERR, 459cd37da74Snw141292 "Error getting thread specific data for %s", IDMAP_DBNAME); 46084decf41Sjp151216 return (IDMAP_ERR_MEMORY); 46184decf41Sjp151216 } 462c5c4113dSnw141292 46384decf41Sjp151216 if (tsd->db_db == NULL) { 46484decf41Sjp151216 tsd->db_db = sqlite_open(IDMAP_DBNAME, 0, &errmsg); 46584decf41Sjp151216 if (tsd->db_db == NULL) { 466cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 467c5c4113dSnw141292 IDMAP_DBNAME, CHECK_NULL(errmsg)); 468c5c4113dSnw141292 sqlite_freemem(errmsg); 469cd37da74Snw141292 return (IDMAP_ERR_DB); 470c5c4113dSnw141292 } 471cd37da74Snw141292 47284decf41Sjp151216 tsd->db_busy.name = IDMAP_DBNAME; 47384decf41Sjp151216 tsd->db_busy.delays = db_delay_table; 47484decf41Sjp151216 tsd->db_busy.delay_size = sizeof (db_delay_table) / 47584decf41Sjp151216 sizeof (int); 47684decf41Sjp151216 sqlite_busy_handler(tsd->db_db, idmap_sqlite_busy_handler, 47784decf41Sjp151216 &tsd->db_busy); 47884decf41Sjp151216 } 47984decf41Sjp151216 *db = tsd->db_db; 480c5c4113dSnw141292 return (IDMAP_SUCCESS); 481c5c4113dSnw141292 } 482c5c4113dSnw141292 483c5c4113dSnw141292 /* 484c5c4113dSnw141292 * Get the cache handle 485c5c4113dSnw141292 */ 486c5c4113dSnw141292 idmap_retcode 487cd37da74Snw141292 get_cache_handle(sqlite **cache) 488cd37da74Snw141292 { 489c5c4113dSnw141292 char *errmsg; 49084decf41Sjp151216 idmap_tsd_t *tsd; 491c5c4113dSnw141292 492c5c4113dSnw141292 /* 49384decf41Sjp151216 * Retrieve the db handle from thread-specific storage 494c5c4113dSnw141292 * If none exists, open and store in thread-specific storage. 495c5c4113dSnw141292 */ 49684decf41Sjp151216 if ((tsd = idmap_get_tsd()) == NULL) { 497cd37da74Snw141292 idmapdlog(LOG_ERR, "Error getting thread specific data for %s", 49884decf41Sjp151216 IDMAP_DBNAME); 49984decf41Sjp151216 return (IDMAP_ERR_MEMORY); 50084decf41Sjp151216 } 501c5c4113dSnw141292 50284decf41Sjp151216 if (tsd->cache_db == NULL) { 50384decf41Sjp151216 tsd->cache_db = sqlite_open(IDMAP_CACHENAME, 0, &errmsg); 50484decf41Sjp151216 if (tsd->cache_db == NULL) { 505cd37da74Snw141292 idmapdlog(LOG_ERR, "Error opening database %s (%s)", 506c5c4113dSnw141292 IDMAP_CACHENAME, CHECK_NULL(errmsg)); 507c5c4113dSnw141292 sqlite_freemem(errmsg); 508cd37da74Snw141292 return (IDMAP_ERR_DB); 509c5c4113dSnw141292 } 510cd37da74Snw141292 51184decf41Sjp151216 tsd->cache_busy.name = IDMAP_CACHENAME; 51284decf41Sjp151216 tsd->cache_busy.delays = cache_delay_table; 51384decf41Sjp151216 tsd->cache_busy.delay_size = sizeof (cache_delay_table) / 51484decf41Sjp151216 sizeof (int); 51584decf41Sjp151216 sqlite_busy_handler(tsd->cache_db, idmap_sqlite_busy_handler, 51684decf41Sjp151216 &tsd->cache_busy); 51784decf41Sjp151216 } 51884decf41Sjp151216 *cache = tsd->cache_db; 519c5c4113dSnw141292 return (IDMAP_SUCCESS); 520c5c4113dSnw141292 } 521c5c4113dSnw141292 522c5c4113dSnw141292 /* 523c5c4113dSnw141292 * Initialize cache and db 524c5c4113dSnw141292 */ 525c5c4113dSnw141292 int 526cd37da74Snw141292 init_dbs() 527cd37da74Snw141292 { 528cd37da74Snw141292 char *sql[2]; 529cd37da74Snw141292 int created, upgraded; 530cd37da74Snw141292 531c5c4113dSnw141292 /* name-based mappings; probably OK to blow away in a pinch(?) */ 532cd37da74Snw141292 sql[0] = DB_INSTALL_SQL; 533cd37da74Snw141292 sql[1] = DB_UPGRADE_FROM_v1_SQL; 534cd37da74Snw141292 535cd37da74Snw141292 if (init_db_instance(IDMAP_DBNAME, DB_VERSION, DB_VERSION_SQL, sql, 536cd37da74Snw141292 FAIL_IF_CORRUPT, &created, &upgraded) < 0) 537c5c4113dSnw141292 return (-1); 538c5c4113dSnw141292 539c5c4113dSnw141292 /* mappings, name/SID lookup cache + ephemeral IDs; OK to blow away */ 540cd37da74Snw141292 sql[0] = CACHE_INSTALL_SQL; 541cd37da74Snw141292 sql[1] = CACHE_UPGRADE_FROM_v1_SQL; 542cd37da74Snw141292 if (init_db_instance(IDMAP_CACHENAME, CACHE_VERSION, CACHE_VERSION_SQL, 543cd37da74Snw141292 sql, REMOVE_IF_CORRUPT, &created, &upgraded) < 0) 544c5c4113dSnw141292 return (-1); 545c5c4113dSnw141292 546cd37da74Snw141292 _idmapdstate.new_eph_db = (created || upgraded) ? 1 : 0; 547cd37da74Snw141292 548c5c4113dSnw141292 return (0); 549c5c4113dSnw141292 } 550c5c4113dSnw141292 551c5c4113dSnw141292 /* 552c5c4113dSnw141292 * Finalize databases 553c5c4113dSnw141292 */ 554c5c4113dSnw141292 void 555cd37da74Snw141292 fini_dbs() 556cd37da74Snw141292 { 557c5c4113dSnw141292 } 558c5c4113dSnw141292 559c5c4113dSnw141292 /* 560e8c27ec8Sbaban * This table is a listing of status codes that will be returned to the 561c5c4113dSnw141292 * client when a SQL command fails with the corresponding error message. 562c5c4113dSnw141292 */ 563c5c4113dSnw141292 static msg_table_t sqlmsgtable[] = { 56462c60062Sbaban {IDMAP_ERR_U2W_NAMERULE_CONFLICT, 565c5c4113dSnw141292 "columns unixname, is_user, u2w_order are not unique"}, 56662c60062Sbaban {IDMAP_ERR_W2U_NAMERULE_CONFLICT, 567cd37da74Snw141292 "columns winname, windomain, is_user, is_wuser, w2u_order are not" 568cd37da74Snw141292 " unique"}, 569cd37da74Snw141292 {IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"}, 570c5c4113dSnw141292 {-1, NULL} 571c5c4113dSnw141292 }; 572c5c4113dSnw141292 573c5c4113dSnw141292 /* 574c5c4113dSnw141292 * idmapd's version of string2stat to map SQLite messages to 575c5c4113dSnw141292 * status codes 576c5c4113dSnw141292 */ 577c5c4113dSnw141292 idmap_retcode 578cd37da74Snw141292 idmapd_string2stat(const char *msg) 579cd37da74Snw141292 { 580c5c4113dSnw141292 int i; 581c5c4113dSnw141292 for (i = 0; sqlmsgtable[i].msg; i++) { 582c5c4113dSnw141292 if (strcasecmp(sqlmsgtable[i].msg, msg) == 0) 583c5c4113dSnw141292 return (sqlmsgtable[i].retcode); 584c5c4113dSnw141292 } 585c5c4113dSnw141292 return (IDMAP_ERR_OTHER); 586c5c4113dSnw141292 } 587c5c4113dSnw141292 588c5c4113dSnw141292 /* 589cd37da74Snw141292 * Executes some SQL in a transaction. 590cd37da74Snw141292 * 591cd37da74Snw141292 * Returns 0 on success, -1 if it failed but the rollback succeeded, -2 592cd37da74Snw141292 * if the rollback failed. 593cd37da74Snw141292 */ 594cd37da74Snw141292 static 595cd37da74Snw141292 int 596cd37da74Snw141292 sql_exec_tran_no_cb(sqlite *db, char *sql, const char *dbname, 597cd37da74Snw141292 const char *while_doing) 598cd37da74Snw141292 { 599cd37da74Snw141292 char *errmsg = NULL; 600cd37da74Snw141292 int rc; 601cd37da74Snw141292 602cd37da74Snw141292 rc = sqlite_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errmsg); 603cd37da74Snw141292 if (rc != SQLITE_OK) { 604cd37da74Snw141292 idmapdlog(LOG_ERR, "Begin transaction failed (%s) " 605cd37da74Snw141292 "while %s (%s)", errmsg, while_doing, dbname); 606cd37da74Snw141292 sqlite_freemem(errmsg); 607cd37da74Snw141292 return (-1); 608cd37da74Snw141292 } 609cd37da74Snw141292 610cd37da74Snw141292 rc = sqlite_exec(db, sql, NULL, NULL, &errmsg); 611cd37da74Snw141292 if (rc != SQLITE_OK) { 612cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error (%s) while %s (%s)", errmsg, 613cd37da74Snw141292 while_doing, dbname); 614cd37da74Snw141292 sqlite_freemem(errmsg); 615cd37da74Snw141292 errmsg = NULL; 616cd37da74Snw141292 goto rollback; 617cd37da74Snw141292 } 618cd37da74Snw141292 619cd37da74Snw141292 rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL, &errmsg); 620cd37da74Snw141292 if (rc == SQLITE_OK) { 621cd37da74Snw141292 sqlite_freemem(errmsg); 622cd37da74Snw141292 return (0); 623cd37da74Snw141292 } 624cd37da74Snw141292 625cd37da74Snw141292 idmapdlog(LOG_ERR, "Database commit error (%s) while s (%s)", 626cd37da74Snw141292 errmsg, while_doing, dbname); 627cd37da74Snw141292 sqlite_freemem(errmsg); 628cd37da74Snw141292 errmsg = NULL; 629cd37da74Snw141292 630cd37da74Snw141292 rollback: 631cd37da74Snw141292 rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, &errmsg); 632cd37da74Snw141292 if (rc != SQLITE_OK) { 633cd37da74Snw141292 idmapdlog(LOG_ERR, "Rollback failed (%s) while %s (%s)", 634cd37da74Snw141292 errmsg, while_doing, dbname); 635cd37da74Snw141292 sqlite_freemem(errmsg); 636cd37da74Snw141292 return (-2); 637cd37da74Snw141292 } 638cd37da74Snw141292 sqlite_freemem(errmsg); 639cd37da74Snw141292 640cd37da74Snw141292 return (-1); 641cd37da74Snw141292 } 642cd37da74Snw141292 643cd37da74Snw141292 /* 644c5c4113dSnw141292 * Execute the given SQL statment without using any callbacks 645c5c4113dSnw141292 */ 646c5c4113dSnw141292 idmap_retcode 647cd37da74Snw141292 sql_exec_no_cb(sqlite *db, char *sql) 648cd37da74Snw141292 { 649c5c4113dSnw141292 char *errmsg = NULL; 65084decf41Sjp151216 int r; 651c5c4113dSnw141292 idmap_retcode retcode; 652c5c4113dSnw141292 653c5c4113dSnw141292 r = sqlite_exec(db, sql, NULL, NULL, &errmsg); 65484decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 655c5c4113dSnw141292 656c5c4113dSnw141292 if (r != SQLITE_OK) { 657cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 658cd37da74Snw141292 CHECK_NULL(errmsg)); 659c5c4113dSnw141292 retcode = idmapd_string2stat(errmsg); 66062c60062Sbaban if (errmsg != NULL) 661c5c4113dSnw141292 sqlite_freemem(errmsg); 662c5c4113dSnw141292 return (retcode); 663c5c4113dSnw141292 } 664c5c4113dSnw141292 665c5c4113dSnw141292 return (IDMAP_SUCCESS); 666c5c4113dSnw141292 } 667c5c4113dSnw141292 668c5c4113dSnw141292 /* 669c5c4113dSnw141292 * Generate expression that can be used in WHERE statements. 670c5c4113dSnw141292 * Examples: 671c5c4113dSnw141292 * <prefix> <col> <op> <value> <suffix> 672c5c4113dSnw141292 * "" "unixuser" "=" "foo" "AND" 673c5c4113dSnw141292 */ 674c5c4113dSnw141292 idmap_retcode 675cd37da74Snw141292 gen_sql_expr_from_rule(idmap_namerule *rule, char **out) 676cd37da74Snw141292 { 677cd37da74Snw141292 char *s_windomain = NULL, *s_winname = NULL; 678cd37da74Snw141292 char *s_unixname = NULL; 679cd37da74Snw141292 char *lower_winname; 680cd37da74Snw141292 int retcode = IDMAP_SUCCESS; 681cd37da74Snw141292 682c5c4113dSnw141292 if (out == NULL) 683c5c4113dSnw141292 return (IDMAP_ERR_ARG); 684c5c4113dSnw141292 685c5c4113dSnw141292 686cd37da74Snw141292 if (!EMPTY_STRING(rule->windomain)) { 687cd37da74Snw141292 s_windomain = sqlite_mprintf("AND windomain = %Q ", 688cd37da74Snw141292 rule->windomain); 689cd37da74Snw141292 if (s_windomain == NULL) { 690cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 691cd37da74Snw141292 goto out; 692c5c4113dSnw141292 } 693cd37da74Snw141292 } 694cd37da74Snw141292 695cd37da74Snw141292 if (!EMPTY_STRING(rule->winname)) { 696cd37da74Snw141292 if ((lower_winname = tolower_u8(rule->winname)) == NULL) 697cd37da74Snw141292 lower_winname = rule->winname; 698cd37da74Snw141292 s_winname = sqlite_mprintf( 699cd37da74Snw141292 "AND winname = %Q AND is_wuser = %d ", 700cd37da74Snw141292 lower_winname, rule->is_wuser ? 1 : 0); 701cd37da74Snw141292 if (lower_winname != rule->winname) 702cd37da74Snw141292 free(lower_winname); 703cd37da74Snw141292 if (s_winname == NULL) { 704cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 705cd37da74Snw141292 goto out; 706cd37da74Snw141292 } 707cd37da74Snw141292 } 708cd37da74Snw141292 709cd37da74Snw141292 if (!EMPTY_STRING(rule->unixname)) { 710cd37da74Snw141292 s_unixname = sqlite_mprintf( 711cd37da74Snw141292 "AND unixname = %Q AND is_user = %d ", 712cd37da74Snw141292 rule->unixname, rule->is_user ? 1 : 0); 713cd37da74Snw141292 if (s_unixname == NULL) { 714cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 715cd37da74Snw141292 goto out; 716cd37da74Snw141292 } 717cd37da74Snw141292 } 718cd37da74Snw141292 719cd37da74Snw141292 *out = sqlite_mprintf("%s %s %s", 720cd37da74Snw141292 s_windomain ? s_windomain : "", 721cd37da74Snw141292 s_winname ? s_winname : "", 722cd37da74Snw141292 s_unixname ? s_unixname : ""); 723cd37da74Snw141292 724cd37da74Snw141292 if (*out == NULL) { 725cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 726cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 727cd37da74Snw141292 goto out; 728cd37da74Snw141292 } 729cd37da74Snw141292 730cd37da74Snw141292 out: 731cd37da74Snw141292 if (s_windomain != NULL) 732cd37da74Snw141292 sqlite_freemem(s_windomain); 733cd37da74Snw141292 if (s_winname != NULL) 734cd37da74Snw141292 sqlite_freemem(s_winname); 735cd37da74Snw141292 if (s_unixname != NULL) 736cd37da74Snw141292 sqlite_freemem(s_unixname); 737cd37da74Snw141292 738cd37da74Snw141292 return (retcode); 739cd37da74Snw141292 } 740cd37da74Snw141292 741cd37da74Snw141292 742c5c4113dSnw141292 743c5c4113dSnw141292 /* 744c5c4113dSnw141292 * Generate and execute SQL statement for LIST RPC calls 745c5c4113dSnw141292 */ 746c5c4113dSnw141292 idmap_retcode 747c5c4113dSnw141292 process_list_svc_sql(sqlite *db, char *sql, uint64_t limit, 748cd37da74Snw141292 list_svc_cb cb, void *result) 749cd37da74Snw141292 { 750c5c4113dSnw141292 list_cb_data_t cb_data; 751c5c4113dSnw141292 char *errmsg = NULL; 75284decf41Sjp151216 int r; 753c5c4113dSnw141292 idmap_retcode retcode = IDMAP_ERR_INTERNAL; 754c5c4113dSnw141292 755c5c4113dSnw141292 (void) memset(&cb_data, 0, sizeof (cb_data)); 756c5c4113dSnw141292 cb_data.result = result; 757c5c4113dSnw141292 cb_data.limit = limit; 758c5c4113dSnw141292 75984decf41Sjp151216 760c5c4113dSnw141292 r = sqlite_exec(db, sql, cb, &cb_data, &errmsg); 76184decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 762c5c4113dSnw141292 switch (r) { 763c5c4113dSnw141292 case SQLITE_OK: 764c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 76584decf41Sjp151216 break; 76684decf41Sjp151216 767c5c4113dSnw141292 default: 768c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 769cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 770cd37da74Snw141292 CHECK_NULL(errmsg)); 77184decf41Sjp151216 break; 772c5c4113dSnw141292 } 77362c60062Sbaban if (errmsg != NULL) 774c5c4113dSnw141292 sqlite_freemem(errmsg); 775c5c4113dSnw141292 return (retcode); 776c5c4113dSnw141292 } 777c5c4113dSnw141292 778c5c4113dSnw141292 /* 779c5c4113dSnw141292 * This routine is called by callbacks that process the results of 780c5c4113dSnw141292 * LIST RPC calls to validate data and to allocate memory for 781c5c4113dSnw141292 * the result array. 782c5c4113dSnw141292 */ 783c5c4113dSnw141292 idmap_retcode 784c5c4113dSnw141292 validate_list_cb_data(list_cb_data_t *cb_data, int argc, char **argv, 785cd37da74Snw141292 int ncol, uchar_t **list, size_t valsize) 786cd37da74Snw141292 { 787c5c4113dSnw141292 size_t nsize; 788c5c4113dSnw141292 void *tmplist; 789c5c4113dSnw141292 790c5c4113dSnw141292 if (cb_data->limit > 0 && cb_data->next == cb_data->limit) 791c5c4113dSnw141292 return (IDMAP_NEXT); 792c5c4113dSnw141292 793c5c4113dSnw141292 if (argc < ncol || argv == NULL) { 794c5c4113dSnw141292 idmapdlog(LOG_ERR, "Invalid data"); 795c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 796c5c4113dSnw141292 } 797c5c4113dSnw141292 798c5c4113dSnw141292 /* alloc in bulk to reduce number of reallocs */ 799c5c4113dSnw141292 if (cb_data->next >= cb_data->len) { 800c5c4113dSnw141292 nsize = (cb_data->len + SIZE_INCR) * valsize; 801c5c4113dSnw141292 tmplist = realloc(*list, nsize); 802c5c4113dSnw141292 if (tmplist == NULL) { 803c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 804c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 805c5c4113dSnw141292 } 806c5c4113dSnw141292 *list = tmplist; 807c5c4113dSnw141292 (void) memset(*list + (cb_data->len * valsize), 0, 808c5c4113dSnw141292 SIZE_INCR * valsize); 809c5c4113dSnw141292 cb_data->len += SIZE_INCR; 810c5c4113dSnw141292 } 811c5c4113dSnw141292 return (IDMAP_SUCCESS); 812c5c4113dSnw141292 } 813c5c4113dSnw141292 814cd37da74Snw141292 static 815cd37da74Snw141292 idmap_retcode 816c5c4113dSnw141292 get_namerule_order(char *winname, char *windomain, char *unixname, 817cd37da74Snw141292 int direction, int is_diagonal, int *w2u_order, int *u2w_order) 818cd37da74Snw141292 { 819c5c4113dSnw141292 *w2u_order = 0; 820c5c4113dSnw141292 *u2w_order = 0; 821c5c4113dSnw141292 822c5c4113dSnw141292 /* 823c5c4113dSnw141292 * Windows to UNIX lookup order: 824c5c4113dSnw141292 * 1. winname@domain (or winname) to "" 825c5c4113dSnw141292 * 2. winname@domain (or winname) to unixname 826c5c4113dSnw141292 * 3. winname@* to "" 827c5c4113dSnw141292 * 4. winname@* to unixname 828c5c4113dSnw141292 * 5. *@domain (or *) to * 829c5c4113dSnw141292 * 6. *@domain (or *) to "" 830c5c4113dSnw141292 * 7. *@domain (or *) to unixname 831c5c4113dSnw141292 * 8. *@* to * 832c5c4113dSnw141292 * 9. *@* to "" 833c5c4113dSnw141292 * 10. *@* to unixname 834c5c4113dSnw141292 * 835c5c4113dSnw141292 * winname is a special case of winname@domain when domain is the 836c5c4113dSnw141292 * default domain. Similarly * is a special case of *@domain when 837c5c4113dSnw141292 * domain is the default domain. 838c5c4113dSnw141292 * 839c5c4113dSnw141292 * Note that "" has priority over specific names because "" inhibits 840c5c4113dSnw141292 * mappings and traditionally deny rules always had higher priority. 841c5c4113dSnw141292 */ 842651c0131Sbaban if (direction != IDMAP_DIRECTION_U2W) { 843651c0131Sbaban /* bi-directional or from windows to unix */ 844c5c4113dSnw141292 if (winname == NULL) 845c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 846c5c4113dSnw141292 else if (unixname == NULL) 847c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 848c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 849c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 850c5c4113dSnw141292 else if (*winname == '*' && windomain && *windomain == '*') { 851c5c4113dSnw141292 if (*unixname == '*') 852c5c4113dSnw141292 *w2u_order = 8; 853c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 854c5c4113dSnw141292 *w2u_order = 9; 855c5c4113dSnw141292 else /* unixname == name */ 856c5c4113dSnw141292 *w2u_order = 10; 857c5c4113dSnw141292 } else if (*winname == '*') { 858c5c4113dSnw141292 if (*unixname == '*') 859c5c4113dSnw141292 *w2u_order = 5; 860c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 861c5c4113dSnw141292 *w2u_order = 6; 862c5c4113dSnw141292 else /* name */ 863c5c4113dSnw141292 *w2u_order = 7; 86462c60062Sbaban } else if (windomain != NULL && *windomain == '*') { 865c5c4113dSnw141292 /* winname == name */ 866c5c4113dSnw141292 if (*unixname == '*') 867c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 868c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 869c5c4113dSnw141292 *w2u_order = 3; 870c5c4113dSnw141292 else /* name */ 871c5c4113dSnw141292 *w2u_order = 4; 872c5c4113dSnw141292 } else { 873c5c4113dSnw141292 /* winname == name && windomain == null or name */ 874c5c4113dSnw141292 if (*unixname == '*') 875c5c4113dSnw141292 return (IDMAP_ERR_W2U_NAMERULE); 876c5c4113dSnw141292 else if (EMPTY_NAME(unixname)) 877c5c4113dSnw141292 *w2u_order = 1; 878c5c4113dSnw141292 else /* name */ 879c5c4113dSnw141292 *w2u_order = 2; 880c5c4113dSnw141292 } 881cd37da74Snw141292 882c5c4113dSnw141292 } 883c5c4113dSnw141292 884c5c4113dSnw141292 /* 885cd37da74Snw141292 * 1. unixname to "", non-diagonal 886cd37da74Snw141292 * 2. unixname to winname@domain (or winname), non-diagonal 887cd37da74Snw141292 * 3. unixname to "", diagonal 888cd37da74Snw141292 * 4. unixname to winname@domain (or winname), diagonal 889cd37da74Snw141292 * 5. * to *@domain (or *), non-diagonal 890cd37da74Snw141292 * 5. * to *@domain (or *), diagonal 891cd37da74Snw141292 * 7. * to "" 892cd37da74Snw141292 * 8. * to winname@domain (or winname) 893cd37da74Snw141292 * 9. * to "", non-diagonal 894cd37da74Snw141292 * 10. * to winname@domain (or winname), diagonal 895c5c4113dSnw141292 */ 896651c0131Sbaban if (direction != IDMAP_DIRECTION_W2U) { 897cd37da74Snw141292 int diagonal = is_diagonal ? 1 : 0; 898cd37da74Snw141292 899651c0131Sbaban /* bi-directional or from unix to windows */ 900c5c4113dSnw141292 if (unixname == NULL || EMPTY_NAME(unixname)) 901c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 902c5c4113dSnw141292 else if (winname == NULL) 903c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 90462c60062Sbaban else if (windomain != NULL && *windomain == '*') 905651c0131Sbaban return (IDMAP_ERR_U2W_NAMERULE); 906c5c4113dSnw141292 else if (*unixname == '*') { 907c5c4113dSnw141292 if (*winname == '*') 908cd37da74Snw141292 *u2w_order = 5 + diagonal; 909c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 910cd37da74Snw141292 *u2w_order = 7 + 2 * diagonal; 911c5c4113dSnw141292 else 912cd37da74Snw141292 *u2w_order = 8 + 2 * diagonal; 913c5c4113dSnw141292 } else { 914c5c4113dSnw141292 if (*winname == '*') 915c5c4113dSnw141292 return (IDMAP_ERR_U2W_NAMERULE); 916c5c4113dSnw141292 else if (EMPTY_NAME(winname)) 917cd37da74Snw141292 *u2w_order = 1 + 2 * diagonal; 918c5c4113dSnw141292 else 919cd37da74Snw141292 *u2w_order = 2 + 2 * diagonal; 920c5c4113dSnw141292 } 921c5c4113dSnw141292 } 922c5c4113dSnw141292 return (IDMAP_SUCCESS); 923c5c4113dSnw141292 } 924c5c4113dSnw141292 925c5c4113dSnw141292 /* 926c5c4113dSnw141292 * Generate and execute SQL statement to add name-based mapping rule 927c5c4113dSnw141292 */ 928c5c4113dSnw141292 idmap_retcode 929cd37da74Snw141292 add_namerule(sqlite *db, idmap_namerule *rule) 930cd37da74Snw141292 { 931c5c4113dSnw141292 char *sql = NULL; 932c5c4113dSnw141292 idmap_stat retcode; 9338e228215Sdm199847 char *dom = NULL; 934c5c4113dSnw141292 int w2u_order, u2w_order; 935c5c4113dSnw141292 char w2ubuf[11], u2wbuf[11]; 936c5c4113dSnw141292 9378e228215Sdm199847 retcode = get_namerule_order(rule->winname, rule->windomain, 938cd37da74Snw141292 rule->unixname, rule->direction, 939cd37da74Snw141292 rule->is_user == rule->is_wuser ? 0 : 1, &w2u_order, &u2w_order); 940c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 941c5c4113dSnw141292 goto out; 942c5c4113dSnw141292 943c5c4113dSnw141292 if (w2u_order) 944c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "%d", w2u_order); 945c5c4113dSnw141292 if (u2w_order) 946c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "%d", u2w_order); 947c5c4113dSnw141292 94862c60062Sbaban /* 94962c60062Sbaban * For the triggers on namerules table to work correctly: 95062c60062Sbaban * 1) Use NULL instead of 0 for w2u_order and u2w_order 95162c60062Sbaban * 2) Use "" instead of NULL for "no domain" 95262c60062Sbaban */ 95362c60062Sbaban 954e8c27ec8Sbaban if (!EMPTY_STRING(rule->windomain)) 9558e228215Sdm199847 dom = rule->windomain; 956cd37da74Snw141292 else if (lookup_wksids_name2sid(rule->winname, NULL, NULL, NULL, NULL) 95762c60062Sbaban == IDMAP_SUCCESS) { 95862c60062Sbaban /* well-known SIDs don't need domain */ 95962c60062Sbaban dom = ""; 96062c60062Sbaban } 961c5c4113dSnw141292 962c5c4113dSnw141292 RDLOCK_CONFIG(); 96362c60062Sbaban if (dom == NULL) { 964c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain) 965c8e26105Sjp151216 dom = _idmapdstate.cfg->pgcfg.default_domain; 966c5c4113dSnw141292 else 967c5c4113dSnw141292 dom = ""; 96862c60062Sbaban } 96984decf41Sjp151216 sql = sqlite_mprintf("INSERT into namerules " 970cd37da74Snw141292 "(is_user, is_wuser, windomain, winname_display, is_nt4, " 971c5c4113dSnw141292 "unixname, w2u_order, u2w_order) " 972cd37da74Snw141292 "VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);", 973cd37da74Snw141292 rule->is_user ? 1 : 0, rule->is_wuser ? 1 : 0, dom, 974cd37da74Snw141292 rule->winname, rule->is_nt4 ? 1 : 0, rule->unixname, 975cd37da74Snw141292 w2u_order ? w2ubuf : NULL, u2w_order ? u2wbuf : NULL); 976c5c4113dSnw141292 UNLOCK_CONFIG(); 977c5c4113dSnw141292 978c5c4113dSnw141292 if (sql == NULL) { 979c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 980c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 981c5c4113dSnw141292 goto out; 982c5c4113dSnw141292 } 983c5c4113dSnw141292 984c5c4113dSnw141292 retcode = sql_exec_no_cb(db, sql); 985c5c4113dSnw141292 986c5c4113dSnw141292 if (retcode == IDMAP_ERR_OTHER) 987c5c4113dSnw141292 retcode = IDMAP_ERR_CFG; 988c5c4113dSnw141292 989c5c4113dSnw141292 out: 99062c60062Sbaban if (sql != NULL) 991c5c4113dSnw141292 sqlite_freemem(sql); 992c5c4113dSnw141292 return (retcode); 993c5c4113dSnw141292 } 994c5c4113dSnw141292 995c5c4113dSnw141292 /* 996c5c4113dSnw141292 * Flush name-based mapping rules 997c5c4113dSnw141292 */ 998c5c4113dSnw141292 idmap_retcode 999cd37da74Snw141292 flush_namerules(sqlite *db) 1000cd37da74Snw141292 { 1001c5c4113dSnw141292 idmap_stat retcode; 1002c5c4113dSnw141292 1003cd37da74Snw141292 retcode = sql_exec_no_cb(db, "DELETE FROM namerules;"); 1004c5c4113dSnw141292 1005c5c4113dSnw141292 return (retcode); 1006c5c4113dSnw141292 } 1007c5c4113dSnw141292 1008c5c4113dSnw141292 /* 1009c5c4113dSnw141292 * Generate and execute SQL statement to remove a name-based mapping rule 1010c5c4113dSnw141292 */ 1011c5c4113dSnw141292 idmap_retcode 1012cd37da74Snw141292 rm_namerule(sqlite *db, idmap_namerule *rule) 1013cd37da74Snw141292 { 1014c5c4113dSnw141292 char *sql = NULL; 1015c5c4113dSnw141292 idmap_stat retcode; 1016c5c4113dSnw141292 char buf[80]; 1017cd37da74Snw141292 char *expr = NULL; 1018c5c4113dSnw141292 10198e228215Sdm199847 if (rule->direction < 0 && EMPTY_STRING(rule->windomain) && 10208e228215Sdm199847 EMPTY_STRING(rule->winname) && EMPTY_STRING(rule->unixname)) 1021c5c4113dSnw141292 return (IDMAP_SUCCESS); 1022c5c4113dSnw141292 1023c5c4113dSnw141292 buf[0] = 0; 1024cd37da74Snw141292 1025cd37da74Snw141292 if (rule->direction == IDMAP_DIRECTION_BI) 1026c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 1027c5c4113dSnw141292 " AND u2w_order > 0"); 1028cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_W2U) 1029c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND w2u_order > 0" 1030c5c4113dSnw141292 " AND (u2w_order = 0 OR u2w_order ISNULL)"); 1031cd37da74Snw141292 else if (rule->direction == IDMAP_DIRECTION_U2W) 1032c5c4113dSnw141292 (void) snprintf(buf, sizeof (buf), "AND u2w_order > 0" 1033c5c4113dSnw141292 " AND (w2u_order = 0 OR w2u_order ISNULL)"); 1034c5c4113dSnw141292 1035cd37da74Snw141292 retcode = gen_sql_expr_from_rule(rule, &expr); 1036cd37da74Snw141292 if (retcode != IDMAP_SUCCESS) 1037c5c4113dSnw141292 goto out; 1038c5c4113dSnw141292 1039cd37da74Snw141292 sql = sqlite_mprintf("DELETE FROM namerules WHERE 1 %s %s;", expr, 1040c5c4113dSnw141292 buf); 1041c5c4113dSnw141292 1042c5c4113dSnw141292 if (sql == NULL) { 1043c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1044c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1045c5c4113dSnw141292 goto out; 1046c5c4113dSnw141292 } 1047c5c4113dSnw141292 1048cd37da74Snw141292 1049c5c4113dSnw141292 retcode = sql_exec_no_cb(db, sql); 1050c5c4113dSnw141292 1051c5c4113dSnw141292 out: 1052cd37da74Snw141292 if (expr != NULL) 1053cd37da74Snw141292 sqlite_freemem(expr); 105462c60062Sbaban if (sql != NULL) 1055c5c4113dSnw141292 sqlite_freemem(sql); 1056c5c4113dSnw141292 return (retcode); 1057c5c4113dSnw141292 } 1058c5c4113dSnw141292 1059c5c4113dSnw141292 /* 1060c5c4113dSnw141292 * Compile the given SQL query and step just once. 1061c5c4113dSnw141292 * 1062c5c4113dSnw141292 * Input: 1063c5c4113dSnw141292 * db - db handle 1064c5c4113dSnw141292 * sql - SQL statement 1065c5c4113dSnw141292 * 1066c5c4113dSnw141292 * Output: 1067c5c4113dSnw141292 * vm - virtual SQL machine 1068c5c4113dSnw141292 * ncol - number of columns in the result 1069c5c4113dSnw141292 * values - column values 1070c5c4113dSnw141292 * 1071c5c4113dSnw141292 * Return values: 1072c5c4113dSnw141292 * IDMAP_SUCCESS 1073c5c4113dSnw141292 * IDMAP_ERR_NOTFOUND 1074c5c4113dSnw141292 * IDMAP_ERR_INTERNAL 1075c5c4113dSnw141292 */ 1076c5c4113dSnw141292 1077cd37da74Snw141292 static 1078cd37da74Snw141292 idmap_retcode 1079c5c4113dSnw141292 sql_compile_n_step_once(sqlite *db, char *sql, sqlite_vm **vm, int *ncol, 1080cd37da74Snw141292 int reqcol, const char ***values) 1081cd37da74Snw141292 { 1082c5c4113dSnw141292 char *errmsg = NULL; 108384decf41Sjp151216 int r; 1084c5c4113dSnw141292 108584decf41Sjp151216 if ((r = sqlite_compile(db, sql, NULL, vm, &errmsg)) != SQLITE_OK) { 1086cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1087cd37da74Snw141292 CHECK_NULL(errmsg)); 1088c5c4113dSnw141292 sqlite_freemem(errmsg); 1089c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1090c5c4113dSnw141292 } 1091c5c4113dSnw141292 1092c5c4113dSnw141292 r = sqlite_step(*vm, ncol, values, NULL); 109384decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 1094c5c4113dSnw141292 109584decf41Sjp151216 if (r == SQLITE_ROW) { 109662c60062Sbaban if (ncol != NULL && *ncol < reqcol) { 1097c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1098c5c4113dSnw141292 *vm = NULL; 1099c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1100c5c4113dSnw141292 } 1101c5c4113dSnw141292 /* Caller will call finalize after using the results */ 1102c5c4113dSnw141292 return (IDMAP_SUCCESS); 1103c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 1104c5c4113dSnw141292 (void) sqlite_finalize(*vm, NULL); 1105c5c4113dSnw141292 *vm = NULL; 1106c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1107c5c4113dSnw141292 } 1108c5c4113dSnw141292 1109c5c4113dSnw141292 (void) sqlite_finalize(*vm, &errmsg); 1110c5c4113dSnw141292 *vm = NULL; 1111cd37da74Snw141292 idmapdlog(LOG_ERR, "Database error during %s (%s)", sql, 1112cd37da74Snw141292 CHECK_NULL(errmsg)); 1113c5c4113dSnw141292 sqlite_freemem(errmsg); 1114c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 1115c5c4113dSnw141292 } 1116c5c4113dSnw141292 111762c60062Sbaban /* 1118e8c27ec8Sbaban * Update nm_siduid and nm_sidgid fields in the lookup state. 1119e8c27ec8Sbaban * 1120e8c27ec8Sbaban * state->nm_siduid represents mode used by sid2uid and uid2sid 1121e8c27ec8Sbaban * requests for directory-based name mappings. Similarly, 1122e8c27ec8Sbaban * state->nm_sidgid represents mode used by sid2gid and gid2sid 1123e8c27ec8Sbaban * requests. 1124e8c27ec8Sbaban * 1125e8c27ec8Sbaban * sid2uid/uid2sid: 1126e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1127e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixuser_attr 1128e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixuser_attr 1129e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixuser_attr 1130e8c27ec8Sbaban * 1131e8c27ec8Sbaban * sid2gid/gid2sid: 1132e8c27ec8Sbaban * none -> ds_name_mapping_enabled != true 1133e8c27ec8Sbaban * AD-mode -> !nldap_winname_attr && ad_unixgroup_attr 1134e8c27ec8Sbaban * nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr 1135e8c27ec8Sbaban * mixed-mode -> nldap_winname_attr && ad_unixgroup_attr 1136e8c27ec8Sbaban */ 1137e8c27ec8Sbaban idmap_retcode 1138e8c27ec8Sbaban get_ds_namemap_type(lookup_state_t *state) 1139e8c27ec8Sbaban { 1140e8c27ec8Sbaban state->nm_siduid = IDMAP_NM_NONE; 1141e8c27ec8Sbaban state->nm_sidgid = IDMAP_NM_NONE; 1142e8c27ec8Sbaban RDLOCK_CONFIG(); 1143e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ds_name_mapping_enabled == FALSE) { 1144e8c27ec8Sbaban UNLOCK_CONFIG(); 1145e8c27ec8Sbaban return (IDMAP_SUCCESS); 1146e8c27ec8Sbaban } 1147e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.nldap_winname_attr != NULL) { 1148e8c27ec8Sbaban state->nm_siduid = 1149e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1150e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1151e8c27ec8Sbaban state->nm_sidgid = 1152e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1153e8c27ec8Sbaban ? IDMAP_NM_MIXED : IDMAP_NM_NLDAP; 1154e8c27ec8Sbaban } else { 1155e8c27ec8Sbaban state->nm_siduid = 1156e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) 1157e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1158e8c27ec8Sbaban state->nm_sidgid = 1159e8c27ec8Sbaban (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) 1160e8c27ec8Sbaban ? IDMAP_NM_AD : IDMAP_NM_NONE; 1161e8c27ec8Sbaban } 1162e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixuser_attr != NULL) { 1163e8c27ec8Sbaban state->ad_unixuser_attr = 1164e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixuser_attr); 1165e8c27ec8Sbaban if (state->ad_unixuser_attr == NULL) { 1166e8c27ec8Sbaban UNLOCK_CONFIG(); 1167e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1168e8c27ec8Sbaban } 1169e8c27ec8Sbaban } 1170e8c27ec8Sbaban if (_idmapdstate.cfg->pgcfg.ad_unixgroup_attr != NULL) { 1171e8c27ec8Sbaban state->ad_unixgroup_attr = 1172e8c27ec8Sbaban strdup(_idmapdstate.cfg->pgcfg.ad_unixgroup_attr); 1173e8c27ec8Sbaban if (state->ad_unixgroup_attr == NULL) { 1174e8c27ec8Sbaban UNLOCK_CONFIG(); 1175e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1176e8c27ec8Sbaban } 1177e8c27ec8Sbaban } 1178e8c27ec8Sbaban UNLOCK_CONFIG(); 1179e8c27ec8Sbaban return (IDMAP_SUCCESS); 1180e8c27ec8Sbaban } 1181e8c27ec8Sbaban 1182e8c27ec8Sbaban /* 118362c60062Sbaban * Table for well-known SIDs. 118462c60062Sbaban * 118562c60062Sbaban * Background: 118662c60062Sbaban * 118776b27f93Sbaban * Some of the well-known principals are stored under: 118862c60062Sbaban * cn=WellKnown Security Principals, cn=Configuration, dc=<forestRootDomain> 118962c60062Sbaban * They belong to objectClass "foreignSecurityPrincipal". They don't have 119062c60062Sbaban * "samAccountName" nor "userPrincipalName" attributes. Their names are 119162c60062Sbaban * available in "cn" and "name" attributes. Some of these principals have a 119262c60062Sbaban * second entry under CN=ForeignSecurityPrincipals,dc=<forestRootDomain> and 119362c60062Sbaban * these duplicate entries have the stringified SID in the "name" and "cn" 119462c60062Sbaban * attributes instead of the actual name. 119562c60062Sbaban * 119676b27f93Sbaban * Those of the form S-1-5-32-X are Builtin groups and are stored in the 119776b27f93Sbaban * cn=builtin container (except, Power Users which is not stored in AD) 119862c60062Sbaban * 119976b27f93Sbaban * These principals are and will remain constant. Therefore doing AD lookups 120076b27f93Sbaban * provides no benefit. Also, using hard-coded table (and thus avoiding AD 120176b27f93Sbaban * lookup) improves performance and avoids additional complexity in the 120276b27f93Sbaban * adutils.c code. Moreover these SIDs can be used when no Active Directory 120376b27f93Sbaban * is available (such as the CIFS server's "workgroup" mode). 120476b27f93Sbaban * 120576b27f93Sbaban * Notes: 120676b27f93Sbaban * 1. Currently we don't support localization of well-known SID names, 120762c60062Sbaban * unlike Windows. 120862c60062Sbaban * 120976b27f93Sbaban * 2. Other well-known SIDs i.e. S-1-5-<domain>-<w-k RID> are not stored 121076b27f93Sbaban * here. AD does have normal user/group objects for these objects and 121176b27f93Sbaban * can be looked up using the existing AD lookup code. 1212e8c27ec8Sbaban * 1213e8c27ec8Sbaban * 3. See comments above lookup_wksids_sid2pid() for more information 1214e8c27ec8Sbaban * on how we lookup the wksids table. 121562c60062Sbaban */ 121662c60062Sbaban static wksids_table_t wksids[] = { 1217e8c27ec8Sbaban {"S-1-0", 0, "Nobody", 0, SENTINEL_PID, -1, 1}, 1218e8c27ec8Sbaban {"S-1-1", 0, "Everyone", 0, SENTINEL_PID, -1, -1}, 1219e8c27ec8Sbaban {"S-1-3", 0, "Creator Owner", 1, IDMAP_WK_CREATOR_OWNER_UID, 1, 0}, 1220e8c27ec8Sbaban {"S-1-3", 1, "Creator Group", 0, IDMAP_WK_CREATOR_GROUP_GID, 0, 0}, 1221e8c27ec8Sbaban {"S-1-3", 2, "Creator Owner Server", 1, SENTINEL_PID, -1, -1}, 1222e8c27ec8Sbaban {"S-1-3", 3, "Creator Group Server", 0, SENTINEL_PID, -1, 1}, 1223e8c27ec8Sbaban {"S-1-3", 4, "Owner Rights", 0, SENTINEL_PID, -1, -1}, 1224e8c27ec8Sbaban {"S-1-5", 1, "Dialup", 0, SENTINEL_PID, -1, -1}, 1225e8c27ec8Sbaban {"S-1-5", 2, "Network", 0, SENTINEL_PID, -1, -1}, 1226e8c27ec8Sbaban {"S-1-5", 3, "Batch", 0, SENTINEL_PID, -1, -1}, 1227e8c27ec8Sbaban {"S-1-5", 4, "Interactive", 0, SENTINEL_PID, -1, -1}, 1228e8c27ec8Sbaban {"S-1-5", 6, "Service", 0, SENTINEL_PID, -1, -1}, 1229e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, GID_NOBODY, 0, 0}, 1230e8c27ec8Sbaban {"S-1-5", 7, "Anonymous Logon", 0, UID_NOBODY, 1, 0}, 1231e8c27ec8Sbaban {"S-1-5", 8, "Proxy", 0, SENTINEL_PID, -1, -1}, 1232e8c27ec8Sbaban {"S-1-5", 9, "Enterprise Domain Controllers", 0, SENTINEL_PID, -1, -1}, 1233e8c27ec8Sbaban {"S-1-5", 10, "Self", 0, SENTINEL_PID, -1, -1}, 1234e8c27ec8Sbaban {"S-1-5", 11, "Authenticated Users", 0, SENTINEL_PID, -1, -1}, 1235e8c27ec8Sbaban {"S-1-5", 12, "Restricted Code", 0, SENTINEL_PID, -1, -1}, 1236e8c27ec8Sbaban {"S-1-5", 13, "Terminal Server User", 0, SENTINEL_PID, -1, -1}, 1237e8c27ec8Sbaban {"S-1-5", 14, "Remote Interactive Logon", 0, SENTINEL_PID, -1, -1}, 1238e8c27ec8Sbaban {"S-1-5", 15, "This Organization", 0, SENTINEL_PID, -1, -1}, 1239e8c27ec8Sbaban {"S-1-5", 17, "IUSR", 0, SENTINEL_PID, -1, -1}, 1240e8c27ec8Sbaban {"S-1-5", 18, "Local System", 0, IDMAP_WK_LOCAL_SYSTEM_GID, 0, 0}, 1241e8c27ec8Sbaban {"S-1-5", 19, "Local Service", 0, SENTINEL_PID, -1, -1}, 1242e8c27ec8Sbaban {"S-1-5", 20, "Network Service", 0, SENTINEL_PID, -1, -1}, 1243e8c27ec8Sbaban {"S-1-5", 1000, "Other Organization", 0, SENTINEL_PID, -1, -1}, 1244e8c27ec8Sbaban {"S-1-5-32", 544, "Administrators", 0, SENTINEL_PID, -1, -1}, 1245e8c27ec8Sbaban {"S-1-5-32", 545, "Users", 0, SENTINEL_PID, -1, -1}, 1246e8c27ec8Sbaban {"S-1-5-32", 546, "Guests", 0, SENTINEL_PID, -1, -1}, 1247e8c27ec8Sbaban {"S-1-5-32", 547, "Power Users", 0, SENTINEL_PID, -1, -1}, 1248e8c27ec8Sbaban {"S-1-5-32", 548, "Account Operators", 0, SENTINEL_PID, -1, -1}, 1249e8c27ec8Sbaban {"S-1-5-32", 549, "Server Operators", 0, SENTINEL_PID, -1, -1}, 1250e8c27ec8Sbaban {"S-1-5-32", 550, "Print Operators", 0, SENTINEL_PID, -1, -1}, 1251e8c27ec8Sbaban {"S-1-5-32", 551, "Backup Operators", 0, SENTINEL_PID, -1, -1}, 1252e8c27ec8Sbaban {"S-1-5-32", 552, "Replicator", 0, SENTINEL_PID, -1, -1}, 125376b27f93Sbaban {"S-1-5-32", 554, "Pre-Windows 2000 Compatible Access", 0, 1254e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1255e8c27ec8Sbaban {"S-1-5-32", 555, "Remote Desktop Users", 0, SENTINEL_PID, -1, -1}, 125676b27f93Sbaban {"S-1-5-32", 556, "Network Configuration Operators", 0, 1257e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 125876b27f93Sbaban {"S-1-5-32", 557, "Incoming Forest Trust Builders", 0, 1259e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1260e8c27ec8Sbaban {"S-1-5-32", 558, "Performance Monitor Users", 0, SENTINEL_PID, -1, -1}, 1261e8c27ec8Sbaban {"S-1-5-32", 559, "Performance Log Users", 0, SENTINEL_PID, -1, -1}, 126276b27f93Sbaban {"S-1-5-32", 560, "Windows Authorization Access Group", 0, 1263e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 126476b27f93Sbaban {"S-1-5-32", 561, "Terminal Server License Servers", 0, 1265e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1266e8c27ec8Sbaban {"S-1-5-32", 561, "Distributed COM Users", 0, SENTINEL_PID, -1, -1}, 1267e8c27ec8Sbaban {"S-1-5-32", 568, "IIS_IUSRS", 0, SENTINEL_PID, -1, -1}, 1268e8c27ec8Sbaban {"S-1-5-32", 569, "Cryptographic Operators", 0, SENTINEL_PID, -1, -1}, 1269e8c27ec8Sbaban {"S-1-5-32", 573, "Event Log Readers", 0, SENTINEL_PID, -1, -1}, 127076b27f93Sbaban {"S-1-5-32", 574, "Certificate Service DCOM Access", 0, 1271e8c27ec8Sbaban SENTINEL_PID, -1, -1}, 1272e8c27ec8Sbaban {"S-1-5-64", 21, "Digest Authentication", 0, SENTINEL_PID, -1, -1}, 1273e8c27ec8Sbaban {"S-1-5-64", 10, "NTLM Authentication", 0, SENTINEL_PID, -1, -1}, 1274e8c27ec8Sbaban {"S-1-5-64", 14, "SChannel Authentication", 0, SENTINEL_PID, -1, -1}, 1275e8c27ec8Sbaban {NULL, UINT32_MAX, NULL, -1, SENTINEL_PID, -1, -1} 1276c5c4113dSnw141292 }; 1277c5c4113dSnw141292 1278e8c27ec8Sbaban /* 1279e8c27ec8Sbaban * Lookup well-known SIDs table either by winname or by SID. 1280e8c27ec8Sbaban * If the given winname or SID is a well-known SID then we set wksid 1281e8c27ec8Sbaban * variable and then proceed to see if the SID has a hard mapping to 1282e8c27ec8Sbaban * a particular UID/GID (Ex: Creator Owner/Creator Group mapped to 1283e8c27ec8Sbaban * fixed ephemeral ids). If we find such mapping then we return 1284e8c27ec8Sbaban * success otherwise notfound. If a well-known SID is mapped to 1285e8c27ec8Sbaban * SENTINEL_PID and the direction field is set (bi-directional or 1286e8c27ec8Sbaban * win2unix) then we treat it as inhibited mapping and return no 1287e8c27ec8Sbaban * mapping (Ex. S-1-0-0). 1288e8c27ec8Sbaban */ 1289cd37da74Snw141292 static 1290cd37da74Snw141292 idmap_retcode 1291e8c27ec8Sbaban lookup_wksids_sid2pid(idmap_mapping *req, idmap_id_res *res, int *wksid) 1292cd37da74Snw141292 { 1293c5c4113dSnw141292 int i; 129462c60062Sbaban 1295e8c27ec8Sbaban *wksid = 0; 1296e8c27ec8Sbaban 1297e8c27ec8Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1298e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1299e8c27ec8Sbaban if ((strcasecmp(wksids[i].sidprefix, 1300e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix) != 0) || 1301e8c27ec8Sbaban wksids[i].rid != req->id1.idmap_id_u.sid.rid) 1302e8c27ec8Sbaban /* this is not our SID */ 1303e8c27ec8Sbaban continue; 1304e8c27ec8Sbaban if (req->id1name == NULL) { 1305e8c27ec8Sbaban req->id1name = strdup(wksids[i].winname); 1306e8c27ec8Sbaban if (req->id1name == NULL) 1307e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1308e8c27ec8Sbaban } 1309e8c27ec8Sbaban } else if (req->id1name != NULL) { 1310e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, req->id1name) != 0) 1311e8c27ec8Sbaban /* this is not our winname */ 1312e8c27ec8Sbaban continue; 1313e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = 1314e8c27ec8Sbaban strdup(wksids[i].sidprefix); 1315e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) 1316e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 1317e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = wksids[i].rid; 1318e8c27ec8Sbaban } 1319e8c27ec8Sbaban 1320e8c27ec8Sbaban *wksid = 1; 1321e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1322e8c27ec8Sbaban 1323e8c27ec8Sbaban req->id1.idtype = (wksids[i].is_wuser) ? 1324e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1325e8c27ec8Sbaban 1326e8c27ec8Sbaban if (wksids[i].pid == SENTINEL_PID) { 1327e8c27ec8Sbaban if (wksids[i].direction == IDMAP_DIRECTION_BI || 1328e8c27ec8Sbaban wksids[i].direction == IDMAP_DIRECTION_W2U) 1329e8c27ec8Sbaban /* Inhibited */ 1330e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 1331e8c27ec8Sbaban /* Not mapped */ 1332e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 1333e8c27ec8Sbaban } else if (wksids[i].direction == IDMAP_DIRECTION_U2W) 133462c60062Sbaban continue; 133562c60062Sbaban 1336e8c27ec8Sbaban switch (res->id.idtype) { 1337c5c4113dSnw141292 case IDMAP_UID: 133862c60062Sbaban if (wksids[i].is_user == 0) 133962c60062Sbaban continue; 134062c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 134162c60062Sbaban res->direction = wksids[i].direction; 1342c5c4113dSnw141292 return (IDMAP_SUCCESS); 1343c5c4113dSnw141292 case IDMAP_GID: 134462c60062Sbaban if (wksids[i].is_user == 1) 134562c60062Sbaban continue; 134662c60062Sbaban res->id.idmap_id_u.gid = wksids[i].pid; 134762c60062Sbaban res->direction = wksids[i].direction; 1348c5c4113dSnw141292 return (IDMAP_SUCCESS); 1349c5c4113dSnw141292 case IDMAP_POSIXID: 135062c60062Sbaban res->id.idmap_id_u.uid = wksids[i].pid; 135162c60062Sbaban res->id.idtype = (!wksids[i].is_user) ? 1352c5c4113dSnw141292 IDMAP_GID : IDMAP_UID; 135362c60062Sbaban res->direction = wksids[i].direction; 1354c5c4113dSnw141292 return (IDMAP_SUCCESS); 1355c5c4113dSnw141292 default: 1356c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 1357c5c4113dSnw141292 } 1358c5c4113dSnw141292 } 1359c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1360c5c4113dSnw141292 } 1361c5c4113dSnw141292 1362cd37da74Snw141292 1363cd37da74Snw141292 static 1364cd37da74Snw141292 idmap_retcode 1365cd37da74Snw141292 lookup_wksids_pid2sid(idmap_mapping *req, idmap_id_res *res, int is_user) 1366cd37da74Snw141292 { 1367c5c4113dSnw141292 int i; 1368e8c27ec8Sbaban if (req->id1.idmap_id_u.uid == SENTINEL_PID) 1369e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 137062c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 137162c60062Sbaban if (wksids[i].pid == req->id1.idmap_id_u.uid && 137262c60062Sbaban wksids[i].is_user == is_user && 137362c60062Sbaban wksids[i].direction != IDMAP_DIRECTION_W2U) { 1374e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) { 1375e8c27ec8Sbaban res->id.idtype = (wksids[i].is_wuser) ? 1376e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1377e8c27ec8Sbaban } 137862c60062Sbaban res->id.idmap_id_u.sid.rid = wksids[i].rid; 1379c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 138062c60062Sbaban strdup(wksids[i].sidprefix); 1381c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 1382c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1383c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 1384c5c4113dSnw141292 } 138562c60062Sbaban res->direction = wksids[i].direction; 1386c5c4113dSnw141292 return (IDMAP_SUCCESS); 1387c5c4113dSnw141292 } 1388c5c4113dSnw141292 } 138962c60062Sbaban return (IDMAP_ERR_NOTFOUND); 139062c60062Sbaban } 139162c60062Sbaban 1392cd37da74Snw141292 static 1393cd37da74Snw141292 idmap_retcode 1394cd37da74Snw141292 lookup_wksids_name2sid(const char *name, char **canonname, char **sidprefix, 1395cd37da74Snw141292 idmap_rid_t *rid, int *type) 1396cd37da74Snw141292 { 139762c60062Sbaban int i; 139862c60062Sbaban for (i = 0; wksids[i].sidprefix != NULL; i++) { 1399e8c27ec8Sbaban if (strcasecmp(wksids[i].winname, name) != 0) 1400e8c27ec8Sbaban continue; 1401e8c27ec8Sbaban if (sidprefix != NULL && 1402e8c27ec8Sbaban (*sidprefix = strdup(wksids[i].sidprefix)) == NULL) { 140362c60062Sbaban idmapdlog(LOG_ERR, "Out of memory"); 140462c60062Sbaban return (IDMAP_ERR_MEMORY); 140562c60062Sbaban } 1406cd37da74Snw141292 if (canonname != NULL && 1407cd37da74Snw141292 (*canonname = strdup(wksids[i].winname)) == NULL) { 1408cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1409e8c27ec8Sbaban if (sidprefix != NULL) { 1410e8c27ec8Sbaban free(*sidprefix); 1411e8c27ec8Sbaban *sidprefix = NULL; 1412e8c27ec8Sbaban } 1413cd37da74Snw141292 return (IDMAP_ERR_MEMORY); 1414cd37da74Snw141292 } 141562c60062Sbaban if (type != NULL) 1416e8c27ec8Sbaban *type = (wksids[i].is_wuser) ? 141762c60062Sbaban _IDMAP_T_USER : _IDMAP_T_GROUP; 141862c60062Sbaban if (rid != NULL) 141962c60062Sbaban *rid = wksids[i].rid; 142062c60062Sbaban return (IDMAP_SUCCESS); 142162c60062Sbaban } 1422c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 1423c5c4113dSnw141292 } 1424c5c4113dSnw141292 1425cd37da74Snw141292 static 1426cd37da74Snw141292 idmap_retcode 1427cd37da74Snw141292 lookup_cache_sid2pid(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1428cd37da74Snw141292 { 1429c5c4113dSnw141292 char *end; 1430c5c4113dSnw141292 char *sql = NULL; 1431c5c4113dSnw141292 const char **values; 1432c5c4113dSnw141292 sqlite_vm *vm = NULL; 1433c5c4113dSnw141292 int ncol, is_user; 1434c5c4113dSnw141292 uid_t pid; 1435c5c4113dSnw141292 time_t curtime, exp; 1436c5c4113dSnw141292 idmap_retcode retcode; 1437cd37da74Snw141292 char *is_user_string; 1438c5c4113dSnw141292 1439c5c4113dSnw141292 /* Current time */ 1440c5c4113dSnw141292 errno = 0; 1441c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1442cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1443c5c4113dSnw141292 strerror(errno)); 1444c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1445c5c4113dSnw141292 goto out; 1446c5c4113dSnw141292 } 1447c5c4113dSnw141292 1448e8c27ec8Sbaban switch (res->id.idtype) { 1449cd37da74Snw141292 case IDMAP_UID: 1450cd37da74Snw141292 is_user_string = "1"; 1451cd37da74Snw141292 break; 1452cd37da74Snw141292 case IDMAP_GID: 1453cd37da74Snw141292 is_user_string = "0"; 1454cd37da74Snw141292 break; 1455cd37da74Snw141292 case IDMAP_POSIXID: 1456cd37da74Snw141292 /* the non-diagonal mapping */ 1457cd37da74Snw141292 is_user_string = "is_wuser"; 1458cd37da74Snw141292 break; 1459cd37da74Snw141292 default: 1460cd37da74Snw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 1461cd37da74Snw141292 goto out; 1462cd37da74Snw141292 } 1463cd37da74Snw141292 1464c5c4113dSnw141292 /* SQL to lookup the cache */ 1465e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1466e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 1467e8c27ec8Sbaban "unixname, u2w, is_wuser " 1468cd37da74Snw141292 "FROM idmap_cache WHERE is_user = %s AND " 1469c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 1470c5c4113dSnw141292 "(pid >= 2147483648 OR " 1471c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1472c5c4113dSnw141292 "expiration > %d));", 1473e8c27ec8Sbaban is_user_string, req->id1.idmap_id_u.sid.prefix, 1474e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, curtime); 1475e8c27ec8Sbaban } else if (req->id1name != NULL) { 1476e8c27ec8Sbaban sql = sqlite_mprintf("SELECT pid, is_user, expiration, " 1477e8c27ec8Sbaban "unixname, u2w, is_wuser " 1478e8c27ec8Sbaban "FROM idmap_cache WHERE is_user = %s AND " 1479e8c27ec8Sbaban "winname = %Q AND windomain = %Q AND w2u = 1 AND " 1480e8c27ec8Sbaban "(pid >= 2147483648 OR " 1481e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 1482e8c27ec8Sbaban "expiration > %d));", 1483e8c27ec8Sbaban is_user_string, req->id1name, req->id1domain, curtime); 1484e8c27ec8Sbaban } else { 1485e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 1486e8c27ec8Sbaban goto out; 1487e8c27ec8Sbaban } 1488e8c27ec8Sbaban 1489c5c4113dSnw141292 if (sql == NULL) { 1490c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1491c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1492c5c4113dSnw141292 goto out; 1493c5c4113dSnw141292 } 1494e8c27ec8Sbaban retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 6, &values); 1495c5c4113dSnw141292 sqlite_freemem(sql); 1496c5c4113dSnw141292 1497c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 1498c5c4113dSnw141292 goto out; 1499c5c4113dSnw141292 } else if (retcode == IDMAP_SUCCESS) { 1500c5c4113dSnw141292 /* sanity checks */ 1501c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 1502c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1503c5c4113dSnw141292 goto out; 1504c5c4113dSnw141292 } 1505c5c4113dSnw141292 1506c5c4113dSnw141292 pid = strtoul(values[0], &end, 10); 1507c5c4113dSnw141292 is_user = strncmp(values[1], "0", 2) ? 1 : 0; 1508c5c4113dSnw141292 1509cd37da74Snw141292 if (is_user) { 1510cd37da74Snw141292 res->id.idtype = IDMAP_UID; 1511cd37da74Snw141292 res->id.idmap_id_u.uid = pid; 1512cd37da74Snw141292 } else { 1513cd37da74Snw141292 res->id.idtype = IDMAP_GID; 1514cd37da74Snw141292 res->id.idmap_id_u.gid = pid; 1515cd37da74Snw141292 } 1516cd37da74Snw141292 1517c5c4113dSnw141292 /* 1518c5c4113dSnw141292 * We may have an expired ephemeral mapping. Consider 1519c5c4113dSnw141292 * the expired entry as valid if we are not going to 1520c5c4113dSnw141292 * perform name-based mapping. But do not renew the 1521c5c4113dSnw141292 * expiration. 1522c5c4113dSnw141292 * If we will be doing name-based mapping then store the 1523c5c4113dSnw141292 * ephemeral pid in the result so that we can use it 1524c5c4113dSnw141292 * if we end up doing dynamic mapping again. 1525c5c4113dSnw141292 */ 1526c5c4113dSnw141292 if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) && 1527cd37da74Snw141292 !AVOID_NAMESERVICE(req) && 1528cd37da74Snw141292 IS_EPHEMERAL(pid) && values[2] != NULL) { 1529c5c4113dSnw141292 exp = strtoll(values[2], &end, 10); 1530c5c4113dSnw141292 if (exp && exp <= curtime) { 1531c5c4113dSnw141292 /* Store the ephemeral pid */ 1532651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 1533cd37da74Snw141292 req->direction |= is_user 1534cd37da74Snw141292 ? _IDMAP_F_EXP_EPH_UID 1535cd37da74Snw141292 : _IDMAP_F_EXP_EPH_GID; 1536c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 1537c5c4113dSnw141292 } 1538c5c4113dSnw141292 } 1539c5c4113dSnw141292 } 1540c5c4113dSnw141292 1541c5c4113dSnw141292 out: 1542c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 154362c60062Sbaban if (values[4] != NULL) 1544c5c4113dSnw141292 res->direction = 1545651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 1546651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 1547c5c4113dSnw141292 else 1548651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 1549c5c4113dSnw141292 155062c60062Sbaban if (values[3] != NULL) { 1551e8c27ec8Sbaban if (req->id2name != NULL) 1552e8c27ec8Sbaban free(req->id2name); 15538e228215Sdm199847 req->id2name = strdup(values[3]); 15548e228215Sdm199847 if (req->id2name == NULL) { 1555c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1556c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1557c5c4113dSnw141292 } 1558c5c4113dSnw141292 } 1559e8c27ec8Sbaban 1560e8c27ec8Sbaban req->id1.idtype = strncmp(values[5], "0", 2) ? 1561e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1562c5c4113dSnw141292 } 156362c60062Sbaban if (vm != NULL) 1564c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1565c5c4113dSnw141292 return (retcode); 1566c5c4113dSnw141292 } 1567c5c4113dSnw141292 1568cd37da74Snw141292 static 1569cd37da74Snw141292 idmap_retcode 157062c60062Sbaban lookup_cache_sid2name(sqlite *cache, const char *sidprefix, idmap_rid_t rid, 1571cd37da74Snw141292 char **name, char **domain, int *type) 1572cd37da74Snw141292 { 1573c5c4113dSnw141292 char *end; 1574c5c4113dSnw141292 char *sql = NULL; 1575c5c4113dSnw141292 const char **values; 1576c5c4113dSnw141292 sqlite_vm *vm = NULL; 1577c5c4113dSnw141292 int ncol; 1578c5c4113dSnw141292 time_t curtime; 1579c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 1580c5c4113dSnw141292 1581c5c4113dSnw141292 /* Get current time */ 1582c5c4113dSnw141292 errno = 0; 1583c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 1584cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 1585c5c4113dSnw141292 strerror(errno)); 1586c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 1587c5c4113dSnw141292 goto out; 1588c5c4113dSnw141292 } 1589c5c4113dSnw141292 1590c5c4113dSnw141292 /* SQL to lookup the cache */ 1591cd37da74Snw141292 sql = sqlite_mprintf("SELECT canon_name, domain, type " 1592cd37da74Snw141292 "FROM name_cache WHERE " 1593c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND " 1594c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 1595c5c4113dSnw141292 "expiration > %d);", 1596c5c4113dSnw141292 sidprefix, rid, curtime); 1597c5c4113dSnw141292 if (sql == NULL) { 1598c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1599c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1600c5c4113dSnw141292 goto out; 1601c5c4113dSnw141292 } 1602c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 3, &values); 1603c5c4113dSnw141292 sqlite_freemem(sql); 1604c5c4113dSnw141292 1605c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 160662c60062Sbaban if (type != NULL) { 1607c5c4113dSnw141292 if (values[2] == NULL) { 1608c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 1609c5c4113dSnw141292 goto out; 1610c5c4113dSnw141292 } 1611c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 1612c5c4113dSnw141292 } 1613c5c4113dSnw141292 161462c60062Sbaban if (name != NULL && values[0] != NULL) { 1615c5c4113dSnw141292 if ((*name = strdup(values[0])) == NULL) { 1616c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1617c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1618c5c4113dSnw141292 goto out; 1619c5c4113dSnw141292 } 1620c5c4113dSnw141292 } 1621c5c4113dSnw141292 162262c60062Sbaban if (domain != NULL && values[1] != NULL) { 1623c5c4113dSnw141292 if ((*domain = strdup(values[1])) == NULL) { 162462c60062Sbaban if (name != NULL && *name) { 1625c5c4113dSnw141292 free(*name); 1626c5c4113dSnw141292 *name = NULL; 1627c5c4113dSnw141292 } 1628c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 1629c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 1630c5c4113dSnw141292 goto out; 1631c5c4113dSnw141292 } 1632c5c4113dSnw141292 } 1633c5c4113dSnw141292 } 1634c5c4113dSnw141292 1635c5c4113dSnw141292 out: 163662c60062Sbaban if (vm != NULL) 1637c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 1638c5c4113dSnw141292 return (retcode); 1639c5c4113dSnw141292 } 1640c5c4113dSnw141292 1641c5c4113dSnw141292 /* 1642e8c27ec8Sbaban * Given SID, find winname using name_cache OR 1643e8c27ec8Sbaban * Given winname, find SID using name_cache. 1644e8c27ec8Sbaban * Used when mapping win to unix i.e. req->id1 is windows id and 1645e8c27ec8Sbaban * req->id2 is unix id 1646c5c4113dSnw141292 */ 1647cd37da74Snw141292 static 1648cd37da74Snw141292 idmap_retcode 1649e8c27ec8Sbaban lookup_name_cache(sqlite *cache, idmap_mapping *req, idmap_id_res *res) 1650cd37da74Snw141292 { 1651c5c4113dSnw141292 int type = -1; 1652c5c4113dSnw141292 idmap_retcode retcode; 1653e8c27ec8Sbaban char *sidprefix = NULL; 1654c5c4113dSnw141292 idmap_rid_t rid; 1655c5c4113dSnw141292 char *name = NULL, *domain = NULL; 1656c5c4113dSnw141292 1657e8c27ec8Sbaban /* Done if we've both sid and winname */ 1658e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL && req->id1name != NULL) 1659e8c27ec8Sbaban return (IDMAP_SUCCESS); 1660c5c4113dSnw141292 1661e8c27ec8Sbaban /* Lookup sid to winname */ 1662e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 1663e8c27ec8Sbaban retcode = lookup_cache_sid2name(cache, 1664e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix, 1665e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, &name, &domain, &type); 166662c60062Sbaban goto out; 1667e8c27ec8Sbaban } 166862c60062Sbaban 1669e8c27ec8Sbaban /* Lookup winame to sid */ 1670e8c27ec8Sbaban retcode = lookup_cache_name2sid(cache, req->id1name, req->id1domain, 1671e8c27ec8Sbaban &name, &sidprefix, &rid, &type); 1672c5c4113dSnw141292 167362c60062Sbaban out: 1674e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1675c5c4113dSnw141292 free(name); 1676c5c4113dSnw141292 free(domain); 1677e8c27ec8Sbaban free(sidprefix); 1678e8c27ec8Sbaban return (retcode); 1679e8c27ec8Sbaban } 1680e8c27ec8Sbaban 1681e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) { 1682e8c27ec8Sbaban res->id.idtype = (type == _IDMAP_T_USER) ? 1683e8c27ec8Sbaban IDMAP_UID : IDMAP_GID; 1684e8c27ec8Sbaban } 1685e8c27ec8Sbaban req->id1.idtype = (type == _IDMAP_T_USER) ? 1686e8c27ec8Sbaban IDMAP_USID : IDMAP_GSID; 1687e8c27ec8Sbaban 1688e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 1689e8c27ec8Sbaban if (name != NULL) { 1690e8c27ec8Sbaban free(req->id1name); /* Free existing winname */ 1691e8c27ec8Sbaban req->id1name = name; /* and use canonical name instead */ 1692e8c27ec8Sbaban } 1693e8c27ec8Sbaban if (req->id1domain == NULL) 1694e8c27ec8Sbaban req->id1domain = domain; 1695e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix == NULL) { 1696e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = sidprefix; 1697e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid = rid; 1698c5c4113dSnw141292 } 1699c5c4113dSnw141292 return (retcode); 1700c5c4113dSnw141292 } 1701c5c4113dSnw141292 1702e8c27ec8Sbaban /* 1703e8c27ec8Sbaban * Batch AD lookups 1704e8c27ec8Sbaban */ 1705c5c4113dSnw141292 idmap_retcode 1706e8c27ec8Sbaban ad_lookup_batch(lookup_state_t *state, idmap_mapping_batch *batch, 1707cd37da74Snw141292 idmap_ids_res *result) 1708cd37da74Snw141292 { 1709c5c4113dSnw141292 idmap_retcode retcode; 1710e8c27ec8Sbaban int i, add, type, is_wuser, is_user; 1711e8c27ec8Sbaban int retries = 0, eunixtype; 1712e8c27ec8Sbaban char **unixname; 1713c5c4113dSnw141292 idmap_mapping *req; 1714c5c4113dSnw141292 idmap_id_res *res; 1715e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 1716e8c27ec8Sbaban 1717e8c27ec8Sbaban /* 1718e8c27ec8Sbaban * Since req->id2.idtype is unused, we will use it here 1719e8c27ec8Sbaban * to retrieve the value of sid_type. But it needs to be 1720e8c27ec8Sbaban * reset to IDMAP_NONE before we return to prevent xdr 1721e8c27ec8Sbaban * from mis-interpreting req->id2 when it tries to free 1722e8c27ec8Sbaban * the input argument. Other option is to allocate an 1723e8c27ec8Sbaban * array of integers and use it instead for the batched 1724e8c27ec8Sbaban * call. But why un-necessarily allocate memory. That may 1725e8c27ec8Sbaban * be an option if req->id2.idtype cannot be re-used in 1726e8c27ec8Sbaban * future. 1727e8c27ec8Sbaban */ 1728c5c4113dSnw141292 1729c5c4113dSnw141292 if (state->ad_nqueries == 0) 1730c5c4113dSnw141292 return (IDMAP_SUCCESS); 1731c5c4113dSnw141292 1732c5c4113dSnw141292 retry: 1733e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, state->ad_nqueries, 1734e8c27ec8Sbaban &qs); 1735e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1736c8e26105Sjp151216 degrade_svc(); 1737c5c4113dSnw141292 idmapdlog(LOG_ERR, 1738e8c27ec8Sbaban "Failed to create batch for AD lookup"); 1739e8c27ec8Sbaban goto out; 1740c5c4113dSnw141292 } 1741c5c4113dSnw141292 1742c8e26105Sjp151216 restore_svc(); 1743c8e26105Sjp151216 1744e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 1745e8c27ec8Sbaban state->ad_unixgroup_attr); 1746e8c27ec8Sbaban 1747e8c27ec8Sbaban for (i = 0, add = 0; i < batch->idmap_mapping_batch_len; i++) { 1748c5c4113dSnw141292 req = &batch->idmap_mapping_batch_val[i]; 1749c5c4113dSnw141292 res = &result->ids.ids_val[i]; 1750e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 1751e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1752c5c4113dSnw141292 1753e8c27ec8Sbaban /* Skip if not marked for AD lookup */ 1754e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1755e8c27ec8Sbaban continue; 1756e8c27ec8Sbaban 1757c5c4113dSnw141292 if (retries == 0) 1758c5c4113dSnw141292 res->retcode = IDMAP_ERR_RETRIABLE_NET_ERR; 1759c5c4113dSnw141292 else if (res->retcode != IDMAP_ERR_RETRIABLE_NET_ERR) 1760c5c4113dSnw141292 continue; 1761e8c27ec8Sbaban 1762e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1763e8c27ec8Sbaban /* win to unix */ 1764e8c27ec8Sbaban 1765e8c27ec8Sbaban assert(req->id1.idmap_id_u.sid.prefix != NULL); 1766e8c27ec8Sbaban 1767e8c27ec8Sbaban /* Lookup AD by SID */ 1768e8c27ec8Sbaban unixname = NULL; 1769e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 1770e8c27ec8Sbaban if (req->id2name == NULL) { 1771e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID && 1772e8c27ec8Sbaban AD_OR_MIXED(state->nm_siduid)) { 1773e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 1774e8c27ec8Sbaban unixname = &req->id2name; 1775e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GID && 1776e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1777e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 1778e8c27ec8Sbaban unixname = &req->id2name; 1779e8c27ec8Sbaban } else if (AD_OR_MIXED(state->nm_siduid) || 1780e8c27ec8Sbaban AD_OR_MIXED(state->nm_sidgid)) { 1781e8c27ec8Sbaban unixname = &req->id2name; 1782e8c27ec8Sbaban } 1783e8c27ec8Sbaban } 1784e8c27ec8Sbaban add = 1; 1785c5c4113dSnw141292 retcode = idmap_sid2name_batch_add1( 1786e8c27ec8Sbaban qs, req->id1.idmap_id_u.sid.prefix, 1787e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, eunixtype, 1788e8c27ec8Sbaban (req->id1name == NULL) ? &req->id1name : NULL, 1789e8c27ec8Sbaban (req->id1domain == NULL) ? &req->id1domain : NULL, 1790e8c27ec8Sbaban (int *)&req->id2.idtype, unixname, &res->retcode); 1791c5c4113dSnw141292 1792e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 1793e8c27ec8Sbaban /* unix to win */ 1794e8c27ec8Sbaban 1795e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL && 1796e8c27ec8Sbaban req->id2name != NULL) { 1797e8c27ec8Sbaban /* Already have SID and winname -- done */ 1798e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 1799e8c27ec8Sbaban continue; 1800c5c4113dSnw141292 } 1801c5c4113dSnw141292 1802e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL) { 1803cd37da74Snw141292 /* 1804e8c27ec8Sbaban * SID but no winname -- lookup AD by 1805e8c27ec8Sbaban * SID to get winname. 1806cd37da74Snw141292 */ 1807e8c27ec8Sbaban add = 1; 1808e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1( 1809e8c27ec8Sbaban qs, res->id.idmap_id_u.sid.prefix, 1810e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1811e8c27ec8Sbaban _IDMAP_T_UNDEF, &req->id2name, 1812e8c27ec8Sbaban &req->id2domain, (int *)&req->id2.idtype, 1813e8c27ec8Sbaban NULL, &res->retcode); 1814e8c27ec8Sbaban } else if (req->id2name != NULL) { 1815e8c27ec8Sbaban /* 1816e8c27ec8Sbaban * winname but no SID -- lookup AD by 1817e8c27ec8Sbaban * winname to get SID. 1818e8c27ec8Sbaban */ 1819e8c27ec8Sbaban add = 1; 1820e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1( 1821e8c27ec8Sbaban qs, req->id2name, req->id2domain, 1822e8c27ec8Sbaban _IDMAP_T_UNDEF, NULL, 1823e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1824e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1825e8c27ec8Sbaban (int *)&req->id2.idtype, NULL, 1826e8c27ec8Sbaban &res->retcode); 1827e8c27ec8Sbaban } else if (req->id1name != NULL) { 1828e8c27ec8Sbaban /* 1829e8c27ec8Sbaban * No SID and no winname but we've unixname -- 1830e8c27ec8Sbaban * lookup AD by unixname to get SID. 1831e8c27ec8Sbaban */ 1832e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 1833e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 1834e8c27ec8Sbaban is_wuser = 1; 1835e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 1836e8c27ec8Sbaban is_wuser = 0; 1837e8c27ec8Sbaban else 1838e8c27ec8Sbaban is_wuser = is_user; 1839e8c27ec8Sbaban add = 1; 1840e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1( 1841e8c27ec8Sbaban qs, req->id1name, is_user, is_wuser, 1842e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix, 1843e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, 1844e8c27ec8Sbaban &req->id2name, &req->id2domain, 1845e8c27ec8Sbaban (int *)&req->id2.idtype, &res->retcode); 1846e8c27ec8Sbaban } 1847e8c27ec8Sbaban } 1848e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1849e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 1850e8c27ec8Sbaban break; 1851e8c27ec8Sbaban } 1852cd37da74Snw141292 } 1853cd37da74Snw141292 1854e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS && add) 1855e8c27ec8Sbaban retcode = idmap_lookup_batch_end(&qs, NULL); 1856cd37da74Snw141292 1857c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 1858c5c4113dSnw141292 goto retry; 1859c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 1860c8e26105Sjp151216 degrade_svc(); 1861c5c4113dSnw141292 1862e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 1863e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "Failed to batch AD lookup requests"); 1864c5c4113dSnw141292 1865c5c4113dSnw141292 out: 1866e8c27ec8Sbaban /* 1867e8c27ec8Sbaban * This loop does the following: 1868e8c27ec8Sbaban * 1) If there are errors in creating or submitting the batch then 1869e8c27ec8Sbaban * we set the retcode for each request (res->retcode) that's part 1870e8c27ec8Sbaban * of the batch to that error. If there were no such errors then 1871e8c27ec8Sbaban * res->retcode for each request will reflect the status of AD 1872e8c27ec8Sbaban * lookup for that particular request. Initial value of 1873e8c27ec8Sbaban * res->retcode is IDMAP_ERR_RETRIABLE_NET_ERR. 1874e8c27ec8Sbaban * 2) If AD lookup for a given request succeeds then evaluate the 1875e8c27ec8Sbaban * type of the AD object (i.e user or group) and update the 1876e8c27ec8Sbaban * idtype in res and req. 1877e8c27ec8Sbaban * 3) Reset req->id2.idtype to IDMAP_NONE. 1878e8c27ec8Sbaban */ 1879e8c27ec8Sbaban for (i = 0; i < batch->idmap_mapping_batch_len; i++) { 1880e8c27ec8Sbaban req = &batch->idmap_mapping_batch_val[i]; 1881e8c27ec8Sbaban type = req->id2.idtype; 1882e8c27ec8Sbaban req->id2.idtype = IDMAP_NONE; 1883*5e0794bcSbaban res = &result->ids.ids_val[i]; 1884*5e0794bcSbaban 1885e8c27ec8Sbaban if (!(req->direction & _IDMAP_F_LOOKUP_AD)) 1886e8c27ec8Sbaban /* Entry that wasn't marked for AD lookup - skip */ 1887e8c27ec8Sbaban continue; 1888e8c27ec8Sbaban 1889e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 1890e8c27ec8Sbaban res->retcode = retcode; 1891e8c27ec8Sbaban continue; 1892e8c27ec8Sbaban } 1893e8c27ec8Sbaban 1894e8c27ec8Sbaban if (!add) 1895e8c27ec8Sbaban continue; 1896e8c27ec8Sbaban 1897e8c27ec8Sbaban 1898e8c27ec8Sbaban if (IS_REQUEST_SID(*req, 1)) { 1899e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) 1900e8c27ec8Sbaban continue; 1901e8c27ec8Sbaban switch (type) { 1902e8c27ec8Sbaban case _IDMAP_T_USER: 1903e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 1904e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 1905e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 1906e8c27ec8Sbaban break; 1907e8c27ec8Sbaban case _IDMAP_T_GROUP: 1908e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 1909e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 1910e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 1911e8c27ec8Sbaban break; 1912e8c27ec8Sbaban default: 1913e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 1914e8c27ec8Sbaban break; 1915e8c27ec8Sbaban } 1916e8c27ec8Sbaban } else if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) { 1917e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 1918e8c27ec8Sbaban if ((!(IDMAP_FATAL_ERROR(res->retcode))) && 1919e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix == NULL && 1920e8c27ec8Sbaban req->id2name == NULL && /* no winname */ 1921e8c27ec8Sbaban req->id1name != NULL) /* unixname */ 1922e8c27ec8Sbaban /* 1923e8c27ec8Sbaban * Here we have a pid2sid request 1924e8c27ec8Sbaban * that was marked for AD lookup 1925e8c27ec8Sbaban * with no SID, no winname but with 1926e8c27ec8Sbaban * unixname. This request was 1927e8c27ec8Sbaban * added to the batch to do AD lookup 1928e8c27ec8Sbaban * by unixname but it failed with non 1929e8c27ec8Sbaban * fatal error. In such case we 1930e8c27ec8Sbaban * ignore the error (i.e set 1931e8c27ec8Sbaban * res->retcode to success) so that 1932e8c27ec8Sbaban * next pass considers it for name 1933e8c27ec8Sbaban * based mapping rules or ephemeral 1934e8c27ec8Sbaban * mapping. 1935e8c27ec8Sbaban */ 1936e8c27ec8Sbaban res->retcode = IDMAP_SUCCESS; 1937e8c27ec8Sbaban continue; 1938e8c27ec8Sbaban } 1939e8c27ec8Sbaban switch (type) { 1940e8c27ec8Sbaban case _IDMAP_T_USER: 1941e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 1942e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 1943e8c27ec8Sbaban break; 1944e8c27ec8Sbaban case _IDMAP_T_GROUP: 1945e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 1946e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 1947e8c27ec8Sbaban break; 1948e8c27ec8Sbaban default: 1949e8c27ec8Sbaban res->retcode = IDMAP_ERR_SID; 1950e8c27ec8Sbaban break; 1951e8c27ec8Sbaban } 1952e8c27ec8Sbaban } 1953e8c27ec8Sbaban } 1954e8c27ec8Sbaban 1955e8c27ec8Sbaban /* AD lookups done. Reset state->ad_nqueries and return */ 1956e8c27ec8Sbaban state->ad_nqueries = 0; 1957c5c4113dSnw141292 return (retcode); 1958c5c4113dSnw141292 } 1959c5c4113dSnw141292 1960cd37da74Snw141292 /* 1961cd37da74Snw141292 * Convention when processing win2unix requests: 1962cd37da74Snw141292 * 1963cd37da74Snw141292 * Windows identity: 1964cd37da74Snw141292 * req->id1name = 1965cd37da74Snw141292 * winname if given otherwise winname found will be placed 1966cd37da74Snw141292 * here. 1967cd37da74Snw141292 * req->id1domain = 1968cd37da74Snw141292 * windomain if given otherwise windomain found will be 1969cd37da74Snw141292 * placed here. 1970cd37da74Snw141292 * req->id1.idtype = 1971cd37da74Snw141292 * Either IDMAP_SID/USID/GSID. If this is IDMAP_SID then it'll 1972cd37da74Snw141292 * be set to IDMAP_USID/GSID depending upon whether the 1973cd37da74Snw141292 * given SID is user or group respectively. The user/group-ness 1974cd37da74Snw141292 * is determined either when looking up well-known SIDs table OR 1975cd37da74Snw141292 * if the SID is found in namecache OR by ad_lookup() OR by 1976cd37da74Snw141292 * ad_lookup_batch(). 1977cd37da74Snw141292 * req->id1..sid.[prefix, rid] = 1978cd37da74Snw141292 * SID if given otherwise SID found will be placed here. 1979cd37da74Snw141292 * 1980cd37da74Snw141292 * Unix identity: 1981cd37da74Snw141292 * req->id2name = 1982cd37da74Snw141292 * unixname found will be placed here. 1983cd37da74Snw141292 * req->id2domain = 1984cd37da74Snw141292 * NOT USED 1985cd37da74Snw141292 * res->id.idtype = 1986cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 1987cd37da74Snw141292 * it is IDMAP_POSIXID then actual type (IDMAP_UID/GID) found 1988cd37da74Snw141292 * will be placed here. 1989cd37da74Snw141292 * res->id..[uid or gid] = 1990cd37da74Snw141292 * UID/GID found will be placed here. 1991cd37da74Snw141292 * 1992cd37da74Snw141292 * Others: 1993cd37da74Snw141292 * res->retcode = 1994cd37da74Snw141292 * Return status for this request will be placed here. 1995cd37da74Snw141292 * res->direction = 1996cd37da74Snw141292 * Direction found will be placed here. Direction 1997cd37da74Snw141292 * meaning whether the resultant mapping is valid 1998cd37da74Snw141292 * only from win2unix or bi-directional. 1999cd37da74Snw141292 * req->direction = 2000cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 2001cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 2002cd37da74Snw141292 * of the request. 2003cd37da74Snw141292 * req->id2.idtype = 2004cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 2005cd37da74Snw141292 * type and is used to initialize res->id.idtype. 2006cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 2007cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 2008cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 2009cd37da74Snw141292 * mis-interpreting the contents of req->id2. 2010cd37da74Snw141292 * req->id2..[uid or gid or sid] = 2011cd37da74Snw141292 * NOT USED 2012cd37da74Snw141292 */ 2013cd37da74Snw141292 2014cd37da74Snw141292 /* 2015cd37da74Snw141292 * This function does the following: 2016cd37da74Snw141292 * 1. Lookup well-known SIDs table. 2017cd37da74Snw141292 * 2. Check if the given SID is a local-SID and if so extract UID/GID from it. 2018cd37da74Snw141292 * 3. Lookup cache. 2019cd37da74Snw141292 * 4. Check if the client does not want new mapping to be allocated 2020cd37da74Snw141292 * in which case this pass is the final pass. 2021cd37da74Snw141292 * 5. Set AD lookup flag if it determines that the next stage needs 2022cd37da74Snw141292 * to do AD lookup. 2023cd37da74Snw141292 */ 2024c5c4113dSnw141292 idmap_retcode 2025c5c4113dSnw141292 sid2pid_first_pass(lookup_state_t *state, sqlite *cache, idmap_mapping *req, 2026cd37da74Snw141292 idmap_id_res *res) 2027cd37da74Snw141292 { 2028c5c4113dSnw141292 idmap_retcode retcode; 2029e8c27ec8Sbaban int wksid; 2030c5c4113dSnw141292 2031e8c27ec8Sbaban /* Initialize result */ 2032e8c27ec8Sbaban res->id.idtype = req->id2.idtype; 2033e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2034e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 2035e8c27ec8Sbaban wksid = 0; 2036c5c4113dSnw141292 2037cf5b5989Sdm199847 if (EMPTY_STRING(req->id1.idmap_id_u.sid.prefix)) { 2038e8c27ec8Sbaban if (req->id1name == NULL) { 2039e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 2040c5c4113dSnw141292 goto out; 2041c5c4113dSnw141292 } 2042e8c27ec8Sbaban /* sanitize sidprefix */ 2043e8c27ec8Sbaban free(req->id1.idmap_id_u.sid.prefix); 2044e8c27ec8Sbaban req->id1.idmap_id_u.sid.prefix = NULL; 2045e8c27ec8Sbaban } 2046c5c4113dSnw141292 2047e8c27ec8Sbaban /* Lookup well-known SIDs table */ 2048e8c27ec8Sbaban retcode = lookup_wksids_sid2pid(req, res, &wksid); 2049c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2050c5c4113dSnw141292 goto out; 2051c5c4113dSnw141292 2052e8c27ec8Sbaban /* Check if this is a localsid */ 2053e8c27ec8Sbaban if (!wksid) { 2054e8c27ec8Sbaban retcode = lookup_localsid2pid(req, res); 2055e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2056e8c27ec8Sbaban goto out; 2057e8c27ec8Sbaban } 2058e8c27ec8Sbaban 2059e8c27ec8Sbaban /* Lookup cache */ 2060c5c4113dSnw141292 retcode = lookup_cache_sid2pid(cache, req, res); 2061c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 2062c5c4113dSnw141292 goto out; 2063c5c4113dSnw141292 2064c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 2065e8c27ec8Sbaban retcode = IDMAP_ERR_NOMAPPING; 2066c5c4113dSnw141292 goto out; 2067c5c4113dSnw141292 } 2068c5c4113dSnw141292 2069c5c4113dSnw141292 /* 2070e8c27ec8Sbaban * Failed to find non-expired entry in cache. Next step is 2071e8c27ec8Sbaban * to determine if this request needs to be batched for AD lookup. 2072e8c27ec8Sbaban * 2073e8c27ec8Sbaban * At this point we have either sid or winname or both. If we don't 2074e8c27ec8Sbaban * have both then lookup name_cache for the sid or winname 2075e8c27ec8Sbaban * whichever is missing. If not found then this request will be 2076e8c27ec8Sbaban * batched for AD lookup. 2077e8c27ec8Sbaban */ 2078e8c27ec8Sbaban retcode = lookup_name_cache(cache, req, res); 2079e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS && retcode != IDMAP_ERR_NOTFOUND) 2080e8c27ec8Sbaban goto out; 2081e8c27ec8Sbaban 2082e8c27ec8Sbaban /* 2083e8c27ec8Sbaban * Set the flag to indicate that we are not done yet so that 2084e8c27ec8Sbaban * subsequent passes considers this request for name-based 2085e8c27ec8Sbaban * mapping and ephemeral mapping. 2086c5c4113dSnw141292 */ 2087c5c4113dSnw141292 state->sid2pid_done = FALSE; 2088e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 2089c5c4113dSnw141292 2090c5c4113dSnw141292 /* 2091e8c27ec8Sbaban * Even if we have both sid and winname, we still may need to batch 2092e8c27ec8Sbaban * this request for AD lookup if we don't have unixname and 2093e8c27ec8Sbaban * directory-based name mapping (AD or mixed) is enabled. 2094e8c27ec8Sbaban * We avoid AD lookup for well-known SIDs because they don't have 2095e8c27ec8Sbaban * regular AD objects. 2096c5c4113dSnw141292 */ 2097e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS || 2098e8c27ec8Sbaban (!wksid && req->id2name == NULL && 2099e8c27ec8Sbaban AD_OR_MIXED_MODE(res->id.idtype, state))) { 2100c5c4113dSnw141292 retcode = IDMAP_SUCCESS; 2101e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 2102c5c4113dSnw141292 state->ad_nqueries++; 2103c5c4113dSnw141292 } 2104c5c4113dSnw141292 2105c5c4113dSnw141292 2106c5c4113dSnw141292 out: 2107c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2108e8c27ec8Sbaban /* 2109e8c27ec8Sbaban * If we are done and there was an error then set fallback pid 2110e8c27ec8Sbaban * in the result. 2111e8c27ec8Sbaban */ 2112e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 2113e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2114c5c4113dSnw141292 return (retcode); 2115c5c4113dSnw141292 } 2116c5c4113dSnw141292 2117c5c4113dSnw141292 /* 2118c5c4113dSnw141292 * Generate SID using the following convention 2119c5c4113dSnw141292 * <machine-sid-prefix>-<1000 + uid> 2120c5c4113dSnw141292 * <machine-sid-prefix>-<2^31 + gid> 2121c5c4113dSnw141292 */ 2122cd37da74Snw141292 static 2123cd37da74Snw141292 idmap_retcode 2124cd37da74Snw141292 generate_localsid(idmap_mapping *req, idmap_id_res *res, int is_user) 2125cd37da74Snw141292 { 2126e8c27ec8Sbaban free(res->id.idmap_id_u.sid.prefix); 2127e8c27ec8Sbaban res->id.idmap_id_u.sid.prefix = NULL; 2128e8c27ec8Sbaban 2129e8c27ec8Sbaban /* 2130e8c27ec8Sbaban * Diagonal mapping for localSIDs not supported because of the 2131e8c27ec8Sbaban * way we generate localSIDs. 2132e8c27ec8Sbaban */ 2133e8c27ec8Sbaban if (is_user && res->id.idtype == IDMAP_GSID) 2134e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2135e8c27ec8Sbaban if (!is_user && res->id.idtype == IDMAP_USID) 2136e8c27ec8Sbaban return (IDMAP_ERR_NOMAPPING); 2137e8c27ec8Sbaban 2138c5c4113dSnw141292 /* Skip 1000 UIDs */ 2139c5c4113dSnw141292 if (is_user && req->id1.idmap_id_u.uid > 2140c5c4113dSnw141292 (INT32_MAX - LOCALRID_MIN)) 214162c60062Sbaban return (IDMAP_ERR_NOMAPPING); 2142c5c4113dSnw141292 2143c5c4113dSnw141292 RDLOCK_CONFIG(); 2144e8c27ec8Sbaban /* 2145e8c27ec8Sbaban * machine_sid is never NULL because if it is we won't be here. 2146e8c27ec8Sbaban * No need to assert because stdrup(NULL) will core anyways. 2147e8c27ec8Sbaban */ 2148c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = 2149c5c4113dSnw141292 strdup(_idmapdstate.cfg->pgcfg.machine_sid); 2150c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 2151c5c4113dSnw141292 UNLOCK_CONFIG(); 2152c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2153c5c4113dSnw141292 return (IDMAP_ERR_MEMORY); 2154c5c4113dSnw141292 } 2155c5c4113dSnw141292 UNLOCK_CONFIG(); 2156c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 2157c5c4113dSnw141292 (is_user) ? req->id1.idmap_id_u.uid + LOCALRID_MIN : 2158c5c4113dSnw141292 req->id1.idmap_id_u.gid + INT32_MAX + 1; 2159651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 2160e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 2161e8c27ec8Sbaban res->id.idtype = is_user ? IDMAP_USID : IDMAP_GSID; 2162c5c4113dSnw141292 2163c5c4113dSnw141292 /* 2164c5c4113dSnw141292 * Don't update name_cache because local sids don't have 2165c5c4113dSnw141292 * valid windows names. 2166c5c4113dSnw141292 */ 2167e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 2168947c7bc0Sbaban return (IDMAP_SUCCESS); 2169c5c4113dSnw141292 } 2170c5c4113dSnw141292 2171cd37da74Snw141292 static 2172cd37da74Snw141292 idmap_retcode 2173cd37da74Snw141292 lookup_localsid2pid(idmap_mapping *req, idmap_id_res *res) 2174cd37da74Snw141292 { 2175c5c4113dSnw141292 char *sidprefix; 2176c5c4113dSnw141292 uint32_t rid; 2177c5c4113dSnw141292 int s; 2178c5c4113dSnw141292 2179c5c4113dSnw141292 /* 2180c5c4113dSnw141292 * If the sidprefix == localsid then UID = last RID - 1000 or 2181c5c4113dSnw141292 * GID = last RID - 2^31. 2182c5c4113dSnw141292 */ 2183e8c27ec8Sbaban if ((sidprefix = req->id1.idmap_id_u.sid.prefix) == NULL) 2184e8c27ec8Sbaban /* This means we are looking up by winname */ 2185e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2186c5c4113dSnw141292 rid = req->id1.idmap_id_u.sid.rid; 2187c5c4113dSnw141292 2188c5c4113dSnw141292 RDLOCK_CONFIG(); 2189c5c4113dSnw141292 s = (_idmapdstate.cfg->pgcfg.machine_sid) ? 2190cd37da74Snw141292 strcasecmp(sidprefix, _idmapdstate.cfg->pgcfg.machine_sid) : 1; 2191c5c4113dSnw141292 UNLOCK_CONFIG(); 2192c5c4113dSnw141292 2193e8c27ec8Sbaban /* 2194e8c27ec8Sbaban * If the given sidprefix does not match machine_sid then this is 2195e8c27ec8Sbaban * not a local SID. 2196e8c27ec8Sbaban */ 2197e8c27ec8Sbaban if (s != 0) 2198e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2199e8c27ec8Sbaban 2200e8c27ec8Sbaban switch (res->id.idtype) { 2201c5c4113dSnw141292 case IDMAP_UID: 2202e8c27ec8Sbaban if (rid > INT32_MAX || rid < LOCALRID_MIN) 2203e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2204c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2205c5c4113dSnw141292 break; 2206c5c4113dSnw141292 case IDMAP_GID: 2207e8c27ec8Sbaban if (rid <= INT32_MAX) 2208e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2209c5c4113dSnw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2210c5c4113dSnw141292 break; 2211c5c4113dSnw141292 case IDMAP_POSIXID: 2212c5c4113dSnw141292 if (rid > INT32_MAX) { 2213cd37da74Snw141292 res->id.idmap_id_u.gid = rid - INT32_MAX - 1; 2214c5c4113dSnw141292 res->id.idtype = IDMAP_GID; 2215c5c4113dSnw141292 } else if (rid < LOCALRID_MIN) { 2216e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2217c5c4113dSnw141292 } else { 2218c5c4113dSnw141292 res->id.idmap_id_u.uid = rid - LOCALRID_MIN; 2219c5c4113dSnw141292 res->id.idtype = IDMAP_UID; 2220c5c4113dSnw141292 } 2221c5c4113dSnw141292 break; 2222c5c4113dSnw141292 default: 2223c5c4113dSnw141292 return (IDMAP_ERR_NOTSUPPORTED); 2224c5c4113dSnw141292 } 2225c5c4113dSnw141292 return (IDMAP_SUCCESS); 2226c5c4113dSnw141292 } 2227c5c4113dSnw141292 2228e8c27ec8Sbaban /* 2229e8c27ec8Sbaban * Name service lookup by unixname to get pid 2230e8c27ec8Sbaban */ 2231cd37da74Snw141292 static 2232cd37da74Snw141292 idmap_retcode 2233e8c27ec8Sbaban ns_lookup_byname(const char *name, const char *lower_name, idmap_id *id) 2234cd37da74Snw141292 { 2235cd37da74Snw141292 struct passwd pwd, *pwdp; 2236cd37da74Snw141292 struct group grp, *grpp; 2237c5c4113dSnw141292 char buf[1024]; 2238c5c4113dSnw141292 int errnum; 2239c5c4113dSnw141292 const char *me = "ns_lookup_byname"; 2240c5c4113dSnw141292 2241e8c27ec8Sbaban switch (id->idtype) { 2242e8c27ec8Sbaban case IDMAP_UID: 2243cd37da74Snw141292 pwdp = getpwnam_r(name, &pwd, buf, sizeof (buf)); 2244e8c27ec8Sbaban if (pwdp == NULL && errno == 0 && lower_name != NULL && 2245cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2246cd37da74Snw141292 pwdp = getpwnam_r(lower_name, &pwd, buf, sizeof (buf)); 2247cd37da74Snw141292 if (pwdp == NULL) { 2248c5c4113dSnw141292 errnum = errno; 2249c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2250c5c4113dSnw141292 "%s: getpwnam_r(%s) failed (%s).", 2251cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2252c5c4113dSnw141292 if (errnum == 0) 2253c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2254c5c4113dSnw141292 else 2255c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2256c5c4113dSnw141292 } 2257e8c27ec8Sbaban id->idmap_id_u.uid = pwd.pw_uid; 2258e8c27ec8Sbaban break; 2259e8c27ec8Sbaban case IDMAP_GID: 2260cd37da74Snw141292 grpp = getgrnam_r(name, &grp, buf, sizeof (buf)); 2261e8c27ec8Sbaban if (grpp == NULL && errno == 0 && lower_name != NULL && 2262cd37da74Snw141292 name != lower_name && strcmp(name, lower_name) != 0) 2263cd37da74Snw141292 grpp = getgrnam_r(lower_name, &grp, buf, sizeof (buf)); 2264cd37da74Snw141292 if (grpp == NULL) { 2265c5c4113dSnw141292 errnum = errno; 2266c5c4113dSnw141292 idmapdlog(LOG_WARNING, 2267c5c4113dSnw141292 "%s: getgrnam_r(%s) failed (%s).", 2268cd37da74Snw141292 me, name, errnum ? strerror(errnum) : "not found"); 2269c5c4113dSnw141292 if (errnum == 0) 2270c5c4113dSnw141292 return (IDMAP_ERR_NOTFOUND); 2271c5c4113dSnw141292 else 2272c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2273c5c4113dSnw141292 } 2274e8c27ec8Sbaban id->idmap_id_u.gid = grp.gr_gid; 2275e8c27ec8Sbaban break; 2276e8c27ec8Sbaban default: 2277e8c27ec8Sbaban return (IDMAP_ERR_ARG); 2278c5c4113dSnw141292 } 2279c5c4113dSnw141292 return (IDMAP_SUCCESS); 2280c5c4113dSnw141292 } 2281c5c4113dSnw141292 2282e8c27ec8Sbaban 2283e8c27ec8Sbaban /* 2284e8c27ec8Sbaban * Name service lookup by pid to get unixname 2285e8c27ec8Sbaban */ 2286e8c27ec8Sbaban static 2287e8c27ec8Sbaban idmap_retcode 2288e8c27ec8Sbaban ns_lookup_bypid(uid_t pid, int is_user, char **unixname) 2289e8c27ec8Sbaban { 2290e8c27ec8Sbaban struct passwd pwd; 2291e8c27ec8Sbaban struct group grp; 2292e8c27ec8Sbaban char buf[1024]; 2293e8c27ec8Sbaban int errnum; 2294e8c27ec8Sbaban const char *me = "ns_lookup_bypid"; 2295e8c27ec8Sbaban 2296e8c27ec8Sbaban if (is_user) { 2297e8c27ec8Sbaban errno = 0; 2298e8c27ec8Sbaban if (getpwuid_r(pid, &pwd, buf, sizeof (buf)) == NULL) { 2299e8c27ec8Sbaban errnum = errno; 2300e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2301e8c27ec8Sbaban "%s: getpwuid_r(%u) failed (%s).", 2302e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2303e8c27ec8Sbaban if (errnum == 0) 2304e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2305e8c27ec8Sbaban else 2306e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2307e8c27ec8Sbaban } 2308e8c27ec8Sbaban *unixname = strdup(pwd.pw_name); 2309e8c27ec8Sbaban } else { 2310e8c27ec8Sbaban errno = 0; 2311e8c27ec8Sbaban if (getgrgid_r(pid, &grp, buf, sizeof (buf)) == NULL) { 2312e8c27ec8Sbaban errnum = errno; 2313e8c27ec8Sbaban idmapdlog(LOG_WARNING, 2314e8c27ec8Sbaban "%s: getgrgid_r(%u) failed (%s).", 2315e8c27ec8Sbaban me, pid, errnum ? strerror(errnum) : "not found"); 2316e8c27ec8Sbaban if (errnum == 0) 2317e8c27ec8Sbaban return (IDMAP_ERR_NOTFOUND); 2318e8c27ec8Sbaban else 2319e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 2320e8c27ec8Sbaban } 2321e8c27ec8Sbaban *unixname = strdup(grp.gr_name); 2322e8c27ec8Sbaban } 2323e8c27ec8Sbaban if (*unixname == NULL) 2324e8c27ec8Sbaban return (IDMAP_ERR_MEMORY); 2325e8c27ec8Sbaban return (IDMAP_SUCCESS); 2326e8c27ec8Sbaban } 2327e8c27ec8Sbaban 2328c5c4113dSnw141292 /* 2329c5c4113dSnw141292 * Name-based mapping 2330c5c4113dSnw141292 * 2331c5c4113dSnw141292 * Case 1: If no rule matches do ephemeral 2332c5c4113dSnw141292 * 2333c5c4113dSnw141292 * Case 2: If rule matches and unixname is "" then return no mapping. 2334c5c4113dSnw141292 * 2335c5c4113dSnw141292 * Case 3: If rule matches and unixname is specified then lookup name 2336c5c4113dSnw141292 * service using the unixname. If unixname not found then return no mapping. 2337c5c4113dSnw141292 * 2338c5c4113dSnw141292 * Case 4: If rule matches and unixname is * then lookup name service 2339c5c4113dSnw141292 * using winname as the unixname. If unixname not found then process 2340c5c4113dSnw141292 * other rules using the lookup order. If no other rule matches then do 2341c5c4113dSnw141292 * ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4. 2342c5c4113dSnw141292 * This allows us to specify a fallback unixname per _domain_ or no mapping 2343c5c4113dSnw141292 * instead of the default behaviour of doing ephemeral mapping. 2344c5c4113dSnw141292 * 2345c5c4113dSnw141292 * Example 1: 2346c5c4113dSnw141292 * *@sfbay == * 2347c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2348c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to an ephemeral id. 2349c5c4113dSnw141292 * 2350c5c4113dSnw141292 * Example 2: 2351c5c4113dSnw141292 * *@sfbay == * 2352c5c4113dSnw141292 * *@sfbay => guest 2353c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2354c5c4113dSnw141292 * the name service then foo@sfbay will be mapped to guest. 2355c5c4113dSnw141292 * 2356c5c4113dSnw141292 * Example 3: 2357c5c4113dSnw141292 * *@sfbay == * 2358c5c4113dSnw141292 * *@sfbay => "" 2359c5c4113dSnw141292 * If looking up windows users foo@sfbay and foo does not exists in 2360c5c4113dSnw141292 * the name service then we will return no mapping for foo@sfbay. 2361c5c4113dSnw141292 * 2362c5c4113dSnw141292 */ 2363cd37da74Snw141292 static 2364cd37da74Snw141292 idmap_retcode 2365cd37da74Snw141292 name_based_mapping_sid2pid(sqlite *db, idmap_mapping *req, idmap_id_res *res) 2366cd37da74Snw141292 { 2367cd37da74Snw141292 const char *unixname, *windomain; 2368cd37da74Snw141292 char *sql = NULL, *errmsg = NULL, *lower_winname = NULL; 2369c5c4113dSnw141292 idmap_retcode retcode; 2370cd37da74Snw141292 char *end, *lower_unixname, *winname; 2371c5c4113dSnw141292 const char **values; 2372c5c4113dSnw141292 sqlite_vm *vm = NULL; 2373cd37da74Snw141292 int ncol, r, i, is_user, is_wuser; 2374c5c4113dSnw141292 const char *me = "name_based_mapping_sid2pid"; 2375c5c4113dSnw141292 2376e8c27ec8Sbaban assert(req->id1name != NULL); /* We have winname */ 2377e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have unixname */ 2378e8c27ec8Sbaban 23798e228215Sdm199847 winname = req->id1name; 23808e228215Sdm199847 windomain = req->id1domain; 2381cd37da74Snw141292 2382cd37da74Snw141292 switch (req->id1.idtype) { 2383cd37da74Snw141292 case IDMAP_USID: 2384cd37da74Snw141292 is_wuser = 1; 2385cd37da74Snw141292 break; 2386cd37da74Snw141292 case IDMAP_GSID: 2387cd37da74Snw141292 is_wuser = 0; 2388cd37da74Snw141292 break; 2389cd37da74Snw141292 default: 2390e8c27ec8Sbaban idmapdlog(LOG_ERR, "%s: Unable to determine if the " 2391e8c27ec8Sbaban "given Windows id is user or group.", me); 2392cd37da74Snw141292 return (IDMAP_ERR_INTERNAL); 2393cd37da74Snw141292 } 2394cd37da74Snw141292 2395e8c27ec8Sbaban switch (res->id.idtype) { 2396cd37da74Snw141292 case IDMAP_UID: 2397cd37da74Snw141292 is_user = 1; 2398cd37da74Snw141292 break; 2399cd37da74Snw141292 case IDMAP_GID: 2400cd37da74Snw141292 is_user = 0; 2401cd37da74Snw141292 break; 2402cd37da74Snw141292 case IDMAP_POSIXID: 2403cd37da74Snw141292 is_user = is_wuser; 2404cd37da74Snw141292 res->id.idtype = is_user ? IDMAP_UID : IDMAP_GID; 2405cd37da74Snw141292 break; 2406cd37da74Snw141292 } 2407c5c4113dSnw141292 2408c5c4113dSnw141292 i = 0; 240962c60062Sbaban if (windomain == NULL) { 241062c60062Sbaban windomain = ""; 241162c60062Sbaban } else { 241262c60062Sbaban RDLOCK_CONFIG(); 2413c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 2414c8e26105Sjp151216 if (strcasecmp(_idmapdstate.cfg->pgcfg.default_domain, 2415c5c4113dSnw141292 windomain) == 0) 2416c5c4113dSnw141292 i = 1; 2417c5c4113dSnw141292 } 2418c5c4113dSnw141292 UNLOCK_CONFIG(); 241962c60062Sbaban } 2420c5c4113dSnw141292 2421cd37da74Snw141292 if ((lower_winname = tolower_u8(winname)) == NULL) 2422cd37da74Snw141292 lower_winname = winname; /* hope for the best */ 2423c5c4113dSnw141292 sql = sqlite_mprintf( 2424c5c4113dSnw141292 "SELECT unixname, u2w_order FROM namerules WHERE " 2425cd37da74Snw141292 "w2u_order > 0 AND is_user = %d AND is_wuser = %d AND " 2426c5c4113dSnw141292 "(winname = %Q OR winname = '*') AND " 2427c5c4113dSnw141292 "(windomain = %Q OR windomain = '*' %s) " 2428c5c4113dSnw141292 "ORDER BY w2u_order ASC;", 2429cd37da74Snw141292 is_user, is_wuser, lower_winname, windomain, 243062c60062Sbaban i ? "OR windomain ISNULL OR windomain = ''" : ""); 2431c5c4113dSnw141292 if (sql == NULL) { 2432c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2433c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 2434c5c4113dSnw141292 goto out; 2435c5c4113dSnw141292 } 2436c5c4113dSnw141292 2437c5c4113dSnw141292 if (sqlite_compile(db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 2438c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2439cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2440cd37da74Snw141292 CHECK_NULL(errmsg)); 2441c5c4113dSnw141292 sqlite_freemem(errmsg); 2442c5c4113dSnw141292 goto out; 2443c5c4113dSnw141292 } 2444c5c4113dSnw141292 244584decf41Sjp151216 for (; ; ) { 2446c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 244784decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 2448c5c4113dSnw141292 244984decf41Sjp151216 if (r == SQLITE_ROW) { 2450c5c4113dSnw141292 if (ncol < 2) { 2451c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2452c5c4113dSnw141292 goto out; 2453c5c4113dSnw141292 } 2454c5c4113dSnw141292 if (values[0] == NULL) { 2455c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2456c5c4113dSnw141292 goto out; 2457c5c4113dSnw141292 } 2458c5c4113dSnw141292 2459c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 2460c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2461c5c4113dSnw141292 goto out; 2462c5c4113dSnw141292 } 2463c5c4113dSnw141292 unixname = (values[0][0] == '*') ? winname : values[0]; 2464cd37da74Snw141292 lower_unixname = (values[0][0] == '*') ? 2465cd37da74Snw141292 lower_winname : NULL; 2466e8c27ec8Sbaban retcode = ns_lookup_byname(unixname, lower_unixname, 2467e8c27ec8Sbaban &res->id); 2468c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 2469c5c4113dSnw141292 if (unixname == winname) 2470c5c4113dSnw141292 /* Case 4 */ 2471c5c4113dSnw141292 continue; 2472c5c4113dSnw141292 else 2473c5c4113dSnw141292 /* Case 3 */ 2474c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 2475c5c4113dSnw141292 } 2476c5c4113dSnw141292 goto out; 2477c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 2478c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 2479c5c4113dSnw141292 goto out; 2480c5c4113dSnw141292 } else { 2481c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 2482c5c4113dSnw141292 vm = NULL; 2483cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 2484cd37da74Snw141292 CHECK_NULL(errmsg)); 2485c5c4113dSnw141292 sqlite_freemem(errmsg); 2486c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2487c5c4113dSnw141292 goto out; 2488c5c4113dSnw141292 } 2489c5c4113dSnw141292 } 2490c5c4113dSnw141292 2491c5c4113dSnw141292 out: 2492c5c4113dSnw141292 sqlite_freemem(sql); 2493c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 249462c60062Sbaban if (values[1] != NULL) 2495c5c4113dSnw141292 res->direction = 2496651c0131Sbaban (strtol(values[1], &end, 10) == 0)? 2497651c0131Sbaban IDMAP_DIRECTION_W2U:IDMAP_DIRECTION_BI; 2498c5c4113dSnw141292 else 2499651c0131Sbaban res->direction = IDMAP_DIRECTION_W2U; 25008e228215Sdm199847 req->id2name = strdup(unixname); 2501c5c4113dSnw141292 } 2502cd37da74Snw141292 if (lower_winname != NULL && lower_winname != winname) 2503cd37da74Snw141292 free(lower_winname); 250462c60062Sbaban if (vm != NULL) 2505c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 2506c5c4113dSnw141292 return (retcode); 2507c5c4113dSnw141292 } 2508c5c4113dSnw141292 2509c5c4113dSnw141292 static 2510c5c4113dSnw141292 int 2511c5c4113dSnw141292 get_next_eph_uid(uid_t *next_uid) 2512c5c4113dSnw141292 { 2513c5c4113dSnw141292 uid_t uid; 2514c5c4113dSnw141292 gid_t gid; 2515c5c4113dSnw141292 int err; 2516c5c4113dSnw141292 2517c5c4113dSnw141292 *next_uid = (uid_t)-1; 2518c5c4113dSnw141292 uid = _idmapdstate.next_uid++; 2519c5c4113dSnw141292 if (uid >= _idmapdstate.limit_uid) { 2520c5c4113dSnw141292 if ((err = allocids(0, 8192, &uid, 0, &gid)) != 0) 2521c5c4113dSnw141292 return (err); 2522c5c4113dSnw141292 2523c5c4113dSnw141292 _idmapdstate.limit_uid = uid + 8192; 2524c5c4113dSnw141292 _idmapdstate.next_uid = uid; 2525c5c4113dSnw141292 } 2526c5c4113dSnw141292 *next_uid = uid; 2527c5c4113dSnw141292 2528c5c4113dSnw141292 return (0); 2529c5c4113dSnw141292 } 2530c5c4113dSnw141292 2531c5c4113dSnw141292 static 2532c5c4113dSnw141292 int 2533c5c4113dSnw141292 get_next_eph_gid(gid_t *next_gid) 2534c5c4113dSnw141292 { 2535c5c4113dSnw141292 uid_t uid; 2536c5c4113dSnw141292 gid_t gid; 2537c5c4113dSnw141292 int err; 2538c5c4113dSnw141292 2539c5c4113dSnw141292 *next_gid = (uid_t)-1; 2540c5c4113dSnw141292 gid = _idmapdstate.next_gid++; 2541c5c4113dSnw141292 if (gid >= _idmapdstate.limit_gid) { 2542c5c4113dSnw141292 if ((err = allocids(0, 0, &uid, 8192, &gid)) != 0) 2543c5c4113dSnw141292 return (err); 2544c5c4113dSnw141292 2545c5c4113dSnw141292 _idmapdstate.limit_gid = gid + 8192; 2546c5c4113dSnw141292 _idmapdstate.next_gid = gid; 2547c5c4113dSnw141292 } 2548c5c4113dSnw141292 *next_gid = gid; 2549c5c4113dSnw141292 2550c5c4113dSnw141292 return (0); 2551c5c4113dSnw141292 } 2552c5c4113dSnw141292 255362c60062Sbaban static 255462c60062Sbaban int 2555cd37da74Snw141292 gethash(const char *str, uint32_t num, uint_t htsize) 2556cd37da74Snw141292 { 255762c60062Sbaban uint_t hval, i, len; 255862c60062Sbaban 255962c60062Sbaban if (str == NULL) 256062c60062Sbaban return (0); 256162c60062Sbaban for (len = strlen(str), hval = 0, i = 0; i < len; i++) { 256262c60062Sbaban hval += str[i]; 256362c60062Sbaban hval += (hval << 10); 256462c60062Sbaban hval ^= (hval >> 6); 256562c60062Sbaban } 256662c60062Sbaban for (str = (const char *)&num, i = 0; i < sizeof (num); i++) { 256762c60062Sbaban hval += str[i]; 256862c60062Sbaban hval += (hval << 10); 256962c60062Sbaban hval ^= (hval >> 6); 257062c60062Sbaban } 257162c60062Sbaban hval += (hval << 3); 257262c60062Sbaban hval ^= (hval >> 11); 257362c60062Sbaban hval += (hval << 15); 257462c60062Sbaban return (hval % htsize); 257562c60062Sbaban } 257662c60062Sbaban 257762c60062Sbaban static 257862c60062Sbaban int 257962c60062Sbaban get_from_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid, 2580cd37da74Snw141292 uid_t *pid) 2581cd37da74Snw141292 { 258262c60062Sbaban uint_t next, key; 258362c60062Sbaban uint_t htsize = state->sid_history_size; 258462c60062Sbaban idmap_sid *sid; 258562c60062Sbaban 258662c60062Sbaban next = gethash(prefix, rid, htsize); 258762c60062Sbaban while (next != htsize) { 258862c60062Sbaban key = state->sid_history[next].key; 258962c60062Sbaban if (key == htsize) 259062c60062Sbaban return (0); 259162c60062Sbaban sid = &state->batch->idmap_mapping_batch_val[key].id1. 259262c60062Sbaban idmap_id_u.sid; 259362c60062Sbaban if (sid->rid == rid && strcmp(sid->prefix, prefix) == 0) { 259462c60062Sbaban *pid = state->result->ids.ids_val[key].id. 259562c60062Sbaban idmap_id_u.uid; 259662c60062Sbaban return (1); 259762c60062Sbaban } 259862c60062Sbaban next = state->sid_history[next].next; 259962c60062Sbaban } 260062c60062Sbaban return (0); 260162c60062Sbaban } 260262c60062Sbaban 260362c60062Sbaban static 260462c60062Sbaban void 2605cd37da74Snw141292 add_to_sid_history(lookup_state_t *state, const char *prefix, uint32_t rid) 2606cd37da74Snw141292 { 260762c60062Sbaban uint_t hash, next; 260862c60062Sbaban uint_t htsize = state->sid_history_size; 260962c60062Sbaban 261062c60062Sbaban hash = next = gethash(prefix, rid, htsize); 261162c60062Sbaban while (state->sid_history[next].key != htsize) { 261262c60062Sbaban next++; 261362c60062Sbaban next %= htsize; 261462c60062Sbaban } 261562c60062Sbaban state->sid_history[next].key = state->curpos; 261662c60062Sbaban if (hash == next) 261762c60062Sbaban return; 261862c60062Sbaban state->sid_history[next].next = state->sid_history[hash].next; 261962c60062Sbaban state->sid_history[hash].next = next; 262062c60062Sbaban } 2621c5c4113dSnw141292 2622e8c27ec8Sbaban void 2623e8c27ec8Sbaban cleanup_lookup_state(lookup_state_t *state) 2624e8c27ec8Sbaban { 2625e8c27ec8Sbaban free(state->sid_history); 2626e8c27ec8Sbaban free(state->ad_unixuser_attr); 2627e8c27ec8Sbaban free(state->ad_unixgroup_attr); 2628e8c27ec8Sbaban } 2629e8c27ec8Sbaban 2630c5c4113dSnw141292 /* ARGSUSED */ 2631c5c4113dSnw141292 static 2632c5c4113dSnw141292 idmap_retcode 263362c60062Sbaban dynamic_ephemeral_mapping(lookup_state_t *state, sqlite *cache, 2634cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2635cd37da74Snw141292 { 2636c5c4113dSnw141292 2637c5c4113dSnw141292 uid_t next_pid; 2638c5c4113dSnw141292 2639651c0131Sbaban res->direction = IDMAP_DIRECTION_BI; 264062c60062Sbaban 264162c60062Sbaban if (IS_EPHEMERAL(res->id.idmap_id_u.uid)) 264262c60062Sbaban return (IDMAP_SUCCESS); 264362c60062Sbaban 264462c60062Sbaban if (state->sid_history != NULL && 264562c60062Sbaban get_from_sid_history(state, req->id1.idmap_id_u.sid.prefix, 264662c60062Sbaban req->id1.idmap_id_u.sid.rid, &next_pid)) { 264762c60062Sbaban res->id.idmap_id_u.uid = next_pid; 264862c60062Sbaban return (IDMAP_SUCCESS); 264962c60062Sbaban } 265062c60062Sbaban 265162c60062Sbaban if (res->id.idtype == IDMAP_UID) { 2652c5c4113dSnw141292 if (get_next_eph_uid(&next_pid) != 0) 2653c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2654c5c4113dSnw141292 res->id.idmap_id_u.uid = next_pid; 2655c5c4113dSnw141292 } else { 2656c5c4113dSnw141292 if (get_next_eph_gid(&next_pid) != 0) 2657c5c4113dSnw141292 return (IDMAP_ERR_INTERNAL); 2658c5c4113dSnw141292 res->id.idmap_id_u.gid = next_pid; 2659c5c4113dSnw141292 } 2660c5c4113dSnw141292 266162c60062Sbaban if (state->sid_history != NULL) 266262c60062Sbaban add_to_sid_history(state, req->id1.idmap_id_u.sid.prefix, 266362c60062Sbaban req->id1.idmap_id_u.sid.rid); 266462c60062Sbaban 2665c5c4113dSnw141292 return (IDMAP_SUCCESS); 2666c5c4113dSnw141292 } 2667c5c4113dSnw141292 2668c5c4113dSnw141292 idmap_retcode 2669c5c4113dSnw141292 sid2pid_second_pass(lookup_state_t *state, sqlite *cache, sqlite *db, 2670cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2671cd37da74Snw141292 { 2672c5c4113dSnw141292 idmap_retcode retcode; 2673c5c4113dSnw141292 2674c5c4113dSnw141292 /* Check if second pass is needed */ 2675e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2676c5c4113dSnw141292 return (res->retcode); 2677c5c4113dSnw141292 2678c5c4113dSnw141292 /* Get status from previous pass */ 2679e8c27ec8Sbaban retcode = res->retcode; 2680e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 2681e8c27ec8Sbaban goto out; 2682c5c4113dSnw141292 2683e8c27ec8Sbaban /* 2684e8c27ec8Sbaban * If directory-based name mapping is enabled then the unixname 2685e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode or 2686e8c27ec8Sbaban * mixed-mode) or from native LDAP object (nldap-mode) -- done. 2687e8c27ec8Sbaban */ 2688e8c27ec8Sbaban if (req->id2name != NULL) { 2689e8c27ec8Sbaban assert(res->id.idtype != IDMAP_POSIXID); 2690e8c27ec8Sbaban if (AD_MODE(res->id.idtype, state)) 2691e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2692e8c27ec8Sbaban else if (NLDAP_MODE(res->id.idtype, state)) 2693e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 2694e8c27ec8Sbaban else if (MIXED_MODE(res->id.idtype, state)) 2695e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 2696c5c4113dSnw141292 2697e8c27ec8Sbaban /* 2698e8c27ec8Sbaban * Special case: (1) If the ad_unixuser_attr and 2699e8c27ec8Sbaban * ad_unixgroup_attr uses the same attribute 2700e8c27ec8Sbaban * name and (2) if this is a diagonal mapping 2701e8c27ec8Sbaban * request and (3) the unixname has been retrieved 2702e8c27ec8Sbaban * from the AD object -- then we ignore it and fallback 2703e8c27ec8Sbaban * to name-based mapping rules and ephemeral mapping 2704e8c27ec8Sbaban * 2705e8c27ec8Sbaban * Example: 2706e8c27ec8Sbaban * Properties: 2707e8c27ec8Sbaban * config/ad_unixuser_attr = "unixname" 2708e8c27ec8Sbaban * config/ad_unixgroup_attr = "unixname" 2709e8c27ec8Sbaban * AD user object: 2710e8c27ec8Sbaban * dn: cn=bob ... 2711e8c27ec8Sbaban * objectclass: user 2712e8c27ec8Sbaban * sam: bob 2713e8c27ec8Sbaban * unixname: bob1234 2714e8c27ec8Sbaban * AD group object: 2715e8c27ec8Sbaban * dn: cn=winadmins ... 2716e8c27ec8Sbaban * objectclass: group 2717e8c27ec8Sbaban * sam: winadmins 2718e8c27ec8Sbaban * unixname: unixadmins 2719e8c27ec8Sbaban * 2720e8c27ec8Sbaban * In this example whether "unixname" refers to a unixuser 2721e8c27ec8Sbaban * or unixgroup depends upon the AD object. 2722e8c27ec8Sbaban * 2723e8c27ec8Sbaban * $idmap show -c winname:bob gid 2724e8c27ec8Sbaban * AD lookup by "samAccountName=bob" for 2725e8c27ec8Sbaban * "ad_unixgroup_attr (i.e unixname)" for directory-based 2726e8c27ec8Sbaban * mapping would get "bob1234" which is not what we want. 2727e8c27ec8Sbaban * Now why not getgrnam_r("bob1234") and use it if it 2728e8c27ec8Sbaban * is indeed a unixgroup? That's because Unix can have 2729e8c27ec8Sbaban * users and groups with the same name and we clearly 2730e8c27ec8Sbaban * don't know the intention of the admin here. 2731e8c27ec8Sbaban * Therefore we ignore this and fallback to name-based 2732e8c27ec8Sbaban * mapping rules or ephemeral mapping. 2733e8c27ec8Sbaban */ 2734e8c27ec8Sbaban if ((AD_MODE(res->id.idtype, state) || 2735e8c27ec8Sbaban MIXED_MODE(res->id.idtype, state)) && 2736e8c27ec8Sbaban state->ad_unixuser_attr != NULL && 2737e8c27ec8Sbaban state->ad_unixgroup_attr != NULL && 2738e8c27ec8Sbaban strcasecmp(state->ad_unixuser_attr, 2739e8c27ec8Sbaban state->ad_unixgroup_attr) == 0 && 2740e8c27ec8Sbaban ((req->id1.idtype == IDMAP_USID && 2741e8c27ec8Sbaban res->id.idtype == IDMAP_GID) || 2742e8c27ec8Sbaban (req->id1.idtype == IDMAP_GSID && 2743e8c27ec8Sbaban res->id.idtype == IDMAP_UID))) { 2744e8c27ec8Sbaban free(req->id2name); 2745e8c27ec8Sbaban req->id2name = NULL; 2746e8c27ec8Sbaban res->id.idmap_id_u.uid = SENTINEL_PID; 2747e8c27ec8Sbaban /* fallback */ 2748e8c27ec8Sbaban } else { 2749e8c27ec8Sbaban if (res->id.idmap_id_u.uid == SENTINEL_PID) 2750e8c27ec8Sbaban retcode = ns_lookup_byname(req->id2name, 2751e8c27ec8Sbaban NULL, &res->id); 2752e8c27ec8Sbaban /* 2753e8c27ec8Sbaban * We don't fallback to name-based mapping rules 2754e8c27ec8Sbaban * or ephemeral mapping. 2755e8c27ec8Sbaban */ 2756c5c4113dSnw141292 goto out; 2757c5c4113dSnw141292 } 2758e8c27ec8Sbaban } 2759c5c4113dSnw141292 2760e8c27ec8Sbaban /* 2761e8c27ec8Sbaban * If we don't have unixname then evaluate local name-based 2762e8c27ec8Sbaban * mapping rules. 2763e8c27ec8Sbaban */ 2764c5c4113dSnw141292 retcode = name_based_mapping_sid2pid(db, req, res); 2765e8c27ec8Sbaban if (retcode != IDMAP_ERR_NOTFOUND) 2766e8c27ec8Sbaban goto out; 2767e8c27ec8Sbaban 2768c5c4113dSnw141292 /* If not found, do ephemeral mapping */ 276962c60062Sbaban retcode = dynamic_ephemeral_mapping(state, cache, req, res); 2770c5c4113dSnw141292 2771c5c4113dSnw141292 out: 2772c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 2773e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 2774e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 2775e8c27ec8Sbaban res->id.idmap_id_u.uid = UID_NOBODY; 2776e8c27ec8Sbaban } 2777e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 2778e8c27ec8Sbaban state->sid2pid_done = FALSE; 2779c5c4113dSnw141292 return (retcode); 2780c5c4113dSnw141292 } 2781c5c4113dSnw141292 2782c5c4113dSnw141292 idmap_retcode 2783c5c4113dSnw141292 update_cache_pid2sid(lookup_state_t *state, sqlite *cache, 2784cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2785cd37da74Snw141292 { 2786c5c4113dSnw141292 char *sql = NULL; 2787c5c4113dSnw141292 idmap_retcode retcode; 2788c5c4113dSnw141292 2789c5c4113dSnw141292 /* Check if we need to cache anything */ 2790e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2791c5c4113dSnw141292 return (IDMAP_SUCCESS); 2792c5c4113dSnw141292 2793c5c4113dSnw141292 /* We don't cache negative entries */ 2794c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 2795c5c4113dSnw141292 return (IDMAP_SUCCESS); 2796c5c4113dSnw141292 2797e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 2798e8c27ec8Sbaban 2799c5c4113dSnw141292 /* 2800c5c4113dSnw141292 * Using NULL for u2w instead of 0 so that our trigger allows 2801c5c4113dSnw141292 * the same pid to be the destination in multiple entries 2802c5c4113dSnw141292 */ 2803c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 2804cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 2805cd37da74Snw141292 "is_user, is_wuser, expiration, w2u, u2w) " 2806cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 2807c5c4113dSnw141292 "strftime('%%s','now') + 600, %q, 1); ", 2808cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 2809cd37da74Snw141292 req->id2domain, req->id2name, req->id1.idmap_id_u.uid, 2810cd37da74Snw141292 req->id1name, (req->id1.idtype == IDMAP_UID) ? 1 : 0, 2811e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? 1 : 0, 2812c5c4113dSnw141292 (res->direction == 0) ? "1" : NULL); 2813c5c4113dSnw141292 2814c5c4113dSnw141292 if (sql == NULL) { 2815c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2816c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2817c5c4113dSnw141292 goto out; 2818c5c4113dSnw141292 } 2819c5c4113dSnw141292 2820c5c4113dSnw141292 retcode = sql_exec_no_cb(cache, sql); 2821c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 2822c5c4113dSnw141292 goto out; 2823c5c4113dSnw141292 2824c5c4113dSnw141292 state->pid2sid_done = FALSE; 2825c5c4113dSnw141292 sqlite_freemem(sql); 2826c5c4113dSnw141292 sql = NULL; 2827c5c4113dSnw141292 2828e8c27ec8Sbaban /* Check if we need to update namecache */ 2829e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 2830c5c4113dSnw141292 goto out; 2831c5c4113dSnw141292 28328e228215Sdm199847 if (req->id2name == NULL) 2833c5c4113dSnw141292 goto out; 2834c5c4113dSnw141292 2835c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 2836cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 2837c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 2838cd37da74Snw141292 res->id.idmap_id_u.sid.prefix, res->id.idmap_id_u.sid.rid, 2839cd37da74Snw141292 req->id2name, req->id2domain, 2840e8c27ec8Sbaban (res->id.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 2841c5c4113dSnw141292 2842c5c4113dSnw141292 if (sql == NULL) { 2843c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2844c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2845c5c4113dSnw141292 goto out; 2846c5c4113dSnw141292 } 2847c5c4113dSnw141292 2848c5c4113dSnw141292 retcode = sql_exec_no_cb(cache, sql); 2849c5c4113dSnw141292 2850c5c4113dSnw141292 out: 285162c60062Sbaban if (sql != NULL) 2852c5c4113dSnw141292 sqlite_freemem(sql); 2853c5c4113dSnw141292 return (retcode); 2854c5c4113dSnw141292 } 2855c5c4113dSnw141292 2856c5c4113dSnw141292 idmap_retcode 2857c5c4113dSnw141292 update_cache_sid2pid(lookup_state_t *state, sqlite *cache, 2858cd37da74Snw141292 idmap_mapping *req, idmap_id_res *res) 2859cd37da74Snw141292 { 2860c5c4113dSnw141292 char *sql = NULL; 2861c5c4113dSnw141292 idmap_retcode retcode; 2862c5c4113dSnw141292 int is_eph_user; 2863c5c4113dSnw141292 2864c5c4113dSnw141292 /* Check if we need to cache anything */ 2865e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 2866c5c4113dSnw141292 return (IDMAP_SUCCESS); 2867c5c4113dSnw141292 2868c5c4113dSnw141292 /* We don't cache negative entries */ 2869c5c4113dSnw141292 if (res->retcode != IDMAP_SUCCESS) 2870c5c4113dSnw141292 return (IDMAP_SUCCESS); 2871c5c4113dSnw141292 2872c5c4113dSnw141292 if (req->direction & _IDMAP_F_EXP_EPH_UID) 2873c5c4113dSnw141292 is_eph_user = 1; 2874c5c4113dSnw141292 else if (req->direction & _IDMAP_F_EXP_EPH_GID) 2875c5c4113dSnw141292 is_eph_user = 0; 2876c5c4113dSnw141292 else 2877c5c4113dSnw141292 is_eph_user = -1; 2878c5c4113dSnw141292 2879c5c4113dSnw141292 if (is_eph_user >= 0 && !IS_EPHEMERAL(res->id.idmap_id_u.uid)) { 2880c5c4113dSnw141292 sql = sqlite_mprintf("UPDATE idmap_cache " 2881c5c4113dSnw141292 "SET w2u = 0 WHERE " 2882c5c4113dSnw141292 "sidprefix = %Q AND rid = %u AND w2u = 1 AND " 2883c5c4113dSnw141292 "pid >= 2147483648 AND is_user = %d;", 2884c5c4113dSnw141292 req->id1.idmap_id_u.sid.prefix, 2885c5c4113dSnw141292 req->id1.idmap_id_u.sid.rid, 2886c5c4113dSnw141292 is_eph_user); 2887c5c4113dSnw141292 if (sql == NULL) { 2888c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2889c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2890c5c4113dSnw141292 goto out; 2891c5c4113dSnw141292 } 2892c5c4113dSnw141292 2893c5c4113dSnw141292 retcode = sql_exec_no_cb(cache, sql); 2894c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 2895c5c4113dSnw141292 goto out; 2896c5c4113dSnw141292 2897c5c4113dSnw141292 sqlite_freemem(sql); 2898c5c4113dSnw141292 sql = NULL; 2899c5c4113dSnw141292 } 2900c5c4113dSnw141292 2901e8c27ec8Sbaban assert(res->direction != IDMAP_DIRECTION_UNDEF); 2902cd37da74Snw141292 2903c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into idmap_cache " 2904cd37da74Snw141292 "(sidprefix, rid, windomain, canon_winname, pid, unixname, " 2905cd37da74Snw141292 "is_user, is_wuser, expiration, w2u, u2w) " 2906cd37da74Snw141292 "VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, " 2907c5c4113dSnw141292 "strftime('%%s','now') + 600, 1, %q); ", 2908cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 2909e8c27ec8Sbaban (req->id1domain != NULL) ? req->id1domain : "", req->id1name, 2910e8c27ec8Sbaban res->id.idmap_id_u.uid, req->id2name, 2911e8c27ec8Sbaban (res->id.idtype == IDMAP_UID) ? 1 : 0, 2912cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? 1 : 0, 2913c5c4113dSnw141292 (res->direction == 0) ? "1" : NULL); 2914c5c4113dSnw141292 2915c5c4113dSnw141292 if (sql == NULL) { 2916c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2917c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2918c5c4113dSnw141292 goto out; 2919c5c4113dSnw141292 } 2920c5c4113dSnw141292 2921c5c4113dSnw141292 retcode = sql_exec_no_cb(cache, sql); 2922c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 2923c5c4113dSnw141292 goto out; 2924c5c4113dSnw141292 2925c5c4113dSnw141292 state->sid2pid_done = FALSE; 2926c5c4113dSnw141292 sqlite_freemem(sql); 2927c5c4113dSnw141292 sql = NULL; 2928c5c4113dSnw141292 2929e8c27ec8Sbaban /* Check if we need to update namecache */ 2930e8c27ec8Sbaban if (req->direction & _IDMAP_F_DONT_UPDATE_NAMECACHE) 2931c5c4113dSnw141292 goto out; 2932c5c4113dSnw141292 2933cf5b5989Sdm199847 if (EMPTY_STRING(req->id1name)) 2934c5c4113dSnw141292 goto out; 2935c5c4113dSnw141292 2936c5c4113dSnw141292 sql = sqlite_mprintf("INSERT OR REPLACE into name_cache " 2937cd37da74Snw141292 "(sidprefix, rid, canon_name, domain, type, expiration) " 2938c5c4113dSnw141292 "VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + 3600); ", 2939cd37da74Snw141292 req->id1.idmap_id_u.sid.prefix, req->id1.idmap_id_u.sid.rid, 2940cd37da74Snw141292 req->id1name, req->id1domain, 2941cd37da74Snw141292 (req->id1.idtype == IDMAP_USID) ? _IDMAP_T_USER : _IDMAP_T_GROUP); 2942c5c4113dSnw141292 2943c5c4113dSnw141292 if (sql == NULL) { 2944c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2945c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 2946c5c4113dSnw141292 goto out; 2947c5c4113dSnw141292 } 2948c5c4113dSnw141292 2949c5c4113dSnw141292 retcode = sql_exec_no_cb(cache, sql); 2950c5c4113dSnw141292 2951c5c4113dSnw141292 out: 295262c60062Sbaban if (sql != NULL) 2953c5c4113dSnw141292 sqlite_freemem(sql); 2954c5c4113dSnw141292 return (retcode); 2955c5c4113dSnw141292 } 2956c5c4113dSnw141292 2957cd37da74Snw141292 static 2958cd37da74Snw141292 idmap_retcode 2959c5c4113dSnw141292 lookup_cache_pid2sid(sqlite *cache, idmap_mapping *req, idmap_id_res *res, 2960cd37da74Snw141292 int is_user, int getname) 2961cd37da74Snw141292 { 2962c5c4113dSnw141292 char *end; 2963c5c4113dSnw141292 char *sql = NULL; 2964c5c4113dSnw141292 const char **values; 2965c5c4113dSnw141292 sqlite_vm *vm = NULL; 2966c5c4113dSnw141292 int ncol; 2967c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 2968c5c4113dSnw141292 time_t curtime; 2969e8c27ec8Sbaban idmap_id_type idtype; 2970c5c4113dSnw141292 2971c5c4113dSnw141292 /* Current time */ 2972c5c4113dSnw141292 errno = 0; 2973c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 2974cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 2975c5c4113dSnw141292 strerror(errno)); 2976c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 2977c5c4113dSnw141292 goto out; 2978c5c4113dSnw141292 } 2979c5c4113dSnw141292 2980e8c27ec8Sbaban /* SQL to lookup the cache by pid or by unixname */ 2981e8c27ec8Sbaban if (req->id1.idmap_id_u.uid != SENTINEL_PID) { 2982e8c27ec8Sbaban sql = sqlite_mprintf("SELECT sidprefix, rid, canon_winname, " 2983e8c27ec8Sbaban "windomain, w2u, is_wuser " 2984c5c4113dSnw141292 "FROM idmap_cache WHERE " 2985c5c4113dSnw141292 "pid = %u AND u2w = 1 AND is_user = %d AND " 2986c5c4113dSnw141292 "(pid >= 2147483648 OR " 2987c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 2988c5c4113dSnw141292 "expiration > %d));", 2989c5c4113dSnw141292 req->id1.idmap_id_u.uid, is_user, curtime); 2990e8c27ec8Sbaban } else if (req->id1name != NULL) { 2991e8c27ec8Sbaban sql = sqlite_mprintf("SELECT sidprefix, rid, canon_winname, " 2992e8c27ec8Sbaban "windomain, w2u, is_wuser " 2993e8c27ec8Sbaban "FROM idmap_cache WHERE " 2994e8c27ec8Sbaban "unixname = %Q AND u2w = 1 AND is_user = %d AND " 2995e8c27ec8Sbaban "(pid >= 2147483648 OR " 2996e8c27ec8Sbaban "(expiration = 0 OR expiration ISNULL OR " 2997e8c27ec8Sbaban "expiration > %d));", 2998e8c27ec8Sbaban req->id1name, is_user, curtime); 2999e8c27ec8Sbaban } 3000e8c27ec8Sbaban 3001c5c4113dSnw141292 if (sql == NULL) { 3002c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3003c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3004c5c4113dSnw141292 goto out; 3005c5c4113dSnw141292 } 3006c5c4113dSnw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 5, &values); 3007c5c4113dSnw141292 sqlite_freemem(sql); 3008c5c4113dSnw141292 3009c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) 3010c5c4113dSnw141292 goto out; 3011c5c4113dSnw141292 else if (retcode == IDMAP_SUCCESS) { 3012c5c4113dSnw141292 /* sanity checks */ 3013c5c4113dSnw141292 if (values[0] == NULL || values[1] == NULL) { 3014c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3015c5c4113dSnw141292 goto out; 3016c5c4113dSnw141292 } 3017c5c4113dSnw141292 3018e8c27ec8Sbaban switch (res->id.idtype) { 3019c5c4113dSnw141292 case IDMAP_SID: 3020cd37da74Snw141292 case IDMAP_USID: 3021cd37da74Snw141292 case IDMAP_GSID: 3022e8c27ec8Sbaban idtype = strtol(values[5], &end, 10) == 1 3023cd37da74Snw141292 ? IDMAP_USID : IDMAP_GSID; 3024cd37da74Snw141292 3025e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID && 3026e8c27ec8Sbaban idtype != IDMAP_USID) { 3027cd37da74Snw141292 retcode = IDMAP_ERR_NOTUSER; 3028cd37da74Snw141292 goto out; 3029e8c27ec8Sbaban } else if (res->id.idtype == IDMAP_GSID && 3030e8c27ec8Sbaban idtype != IDMAP_GSID) { 3031cd37da74Snw141292 retcode = IDMAP_ERR_NOTGROUP; 3032cd37da74Snw141292 goto out; 3033cd37da74Snw141292 } 3034e8c27ec8Sbaban res->id.idtype = idtype; 3035cd37da74Snw141292 3036c5c4113dSnw141292 res->id.idmap_id_u.sid.rid = 3037c5c4113dSnw141292 strtoul(values[1], &end, 10); 3038c5c4113dSnw141292 res->id.idmap_id_u.sid.prefix = strdup(values[0]); 3039c5c4113dSnw141292 if (res->id.idmap_id_u.sid.prefix == NULL) { 3040c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3041c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3042c5c4113dSnw141292 goto out; 3043c5c4113dSnw141292 } 3044c5c4113dSnw141292 304562c60062Sbaban if (values[4] != NULL) 3046c5c4113dSnw141292 res->direction = 3047651c0131Sbaban (strtol(values[4], &end, 10) == 0)? 3048651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3049c5c4113dSnw141292 else 3050651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 3051c5c4113dSnw141292 3052c5c4113dSnw141292 if (getname == 0 || values[2] == NULL) 3053c5c4113dSnw141292 break; 30548e228215Sdm199847 req->id2name = strdup(values[2]); 30558e228215Sdm199847 if (req->id2name == NULL) { 3056c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3057c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3058c5c4113dSnw141292 goto out; 3059c5c4113dSnw141292 } 3060c5c4113dSnw141292 3061c5c4113dSnw141292 if (values[3] == NULL) 3062c5c4113dSnw141292 break; 30638e228215Sdm199847 req->id2domain = strdup(values[3]); 30648e228215Sdm199847 if (req->id2domain == NULL) { 3065c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3066c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3067c5c4113dSnw141292 goto out; 3068c5c4113dSnw141292 } 3069cd37da74Snw141292 3070c5c4113dSnw141292 break; 3071c5c4113dSnw141292 default: 3072c5c4113dSnw141292 retcode = IDMAP_ERR_NOTSUPPORTED; 3073c5c4113dSnw141292 break; 3074c5c4113dSnw141292 } 3075c5c4113dSnw141292 } 3076c5c4113dSnw141292 3077c5c4113dSnw141292 out: 307862c60062Sbaban if (vm != NULL) 3079c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3080c5c4113dSnw141292 return (retcode); 3081c5c4113dSnw141292 } 3082c5c4113dSnw141292 3083cd37da74Snw141292 static 3084cd37da74Snw141292 idmap_retcode 3085c5c4113dSnw141292 lookup_cache_name2sid(sqlite *cache, const char *name, const char *domain, 3086cd37da74Snw141292 char **canonname, char **sidprefix, idmap_rid_t *rid, int *type) 3087cd37da74Snw141292 { 3088cd37da74Snw141292 char *end, *lower_name; 3089c5c4113dSnw141292 char *sql = NULL; 3090c5c4113dSnw141292 const char **values; 3091c5c4113dSnw141292 sqlite_vm *vm = NULL; 3092c5c4113dSnw141292 int ncol; 3093c5c4113dSnw141292 time_t curtime; 3094c5c4113dSnw141292 idmap_retcode retcode = IDMAP_SUCCESS; 3095c5c4113dSnw141292 3096c5c4113dSnw141292 /* Get current time */ 3097c5c4113dSnw141292 errno = 0; 3098c5c4113dSnw141292 if ((curtime = time(NULL)) == (time_t)-1) { 3099cd37da74Snw141292 idmapdlog(LOG_ERR, "Failed to get current time (%s)", 3100c5c4113dSnw141292 strerror(errno)); 3101c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3102c5c4113dSnw141292 goto out; 3103c5c4113dSnw141292 } 3104c5c4113dSnw141292 3105c5c4113dSnw141292 /* SQL to lookup the cache */ 3106cd37da74Snw141292 if ((lower_name = tolower_u8(name)) == NULL) 3107cd37da74Snw141292 lower_name = (char *)name; 3108cd37da74Snw141292 sql = sqlite_mprintf("SELECT sidprefix, rid, type, canon_name " 3109cd37da74Snw141292 "FROM name_cache WHERE name = %Q AND domain = %Q AND " 3110c5c4113dSnw141292 "(expiration = 0 OR expiration ISNULL OR " 3111cd37da74Snw141292 "expiration > %d);", lower_name, domain, curtime); 3112cd37da74Snw141292 if (lower_name != name) 3113cd37da74Snw141292 free(lower_name); 3114c5c4113dSnw141292 if (sql == NULL) { 3115c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3116c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3117c5c4113dSnw141292 goto out; 3118c5c4113dSnw141292 } 3119cd37da74Snw141292 retcode = sql_compile_n_step_once(cache, sql, &vm, &ncol, 4, &values); 3120c5c4113dSnw141292 sqlite_freemem(sql); 3121c5c4113dSnw141292 3122c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 312362c60062Sbaban if (type != NULL) { 3124c5c4113dSnw141292 if (values[2] == NULL) { 3125c5c4113dSnw141292 retcode = IDMAP_ERR_CACHE; 3126c5c4113dSnw141292 goto out; 3127c5c4113dSnw141292 } 3128c5c4113dSnw141292 *type = strtol(values[2], &end, 10); 3129c5c4113dSnw141292 } 3130c5c4113dSnw141292 3131e8c27ec8Sbaban if (values[0] == NULL || values[1] == NULL) { 3132e8c27ec8Sbaban retcode = IDMAP_ERR_CACHE; 3133e8c27ec8Sbaban goto out; 3134e8c27ec8Sbaban } 3135e8c27ec8Sbaban 3136cd37da74Snw141292 if (canonname != NULL) { 3137cd37da74Snw141292 assert(values[3] != NULL); 3138cd37da74Snw141292 if ((*canonname = strdup(values[3])) == NULL) { 3139cd37da74Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3140cd37da74Snw141292 retcode = IDMAP_ERR_MEMORY; 3141cd37da74Snw141292 goto out; 3142cd37da74Snw141292 } 3143cd37da74Snw141292 } 3144cd37da74Snw141292 3145c5c4113dSnw141292 if ((*sidprefix = strdup(values[0])) == NULL) { 3146c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3147c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3148e8c27ec8Sbaban if (canonname != NULL) { 3149e8c27ec8Sbaban free(*canonname); 3150e8c27ec8Sbaban *canonname = NULL; 3151e8c27ec8Sbaban } 3152c5c4113dSnw141292 goto out; 3153c5c4113dSnw141292 } 3154c5c4113dSnw141292 *rid = strtoul(values[1], &end, 10); 3155c5c4113dSnw141292 } 3156c5c4113dSnw141292 3157c5c4113dSnw141292 out: 315862c60062Sbaban if (vm != NULL) 3159c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3160c5c4113dSnw141292 return (retcode); 3161c5c4113dSnw141292 } 3162c5c4113dSnw141292 3163cd37da74Snw141292 static 3164cd37da74Snw141292 idmap_retcode 3165e8c27ec8Sbaban ad_lookup_by_winname(lookup_state_t *state, 3166e8c27ec8Sbaban const char *name, const char *domain, int eunixtype, 3167e8c27ec8Sbaban char **canonname, char **sidprefix, 3168e8c27ec8Sbaban idmap_rid_t *rid, int *wintype, char **unixname) 3169cd37da74Snw141292 { 3170c5c4113dSnw141292 int retries = 0; 3171c5c4113dSnw141292 idmap_query_state_t *qs = NULL; 3172c5c4113dSnw141292 idmap_retcode rc, retcode; 3173c5c4113dSnw141292 3174c5c4113dSnw141292 retry: 3175e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, 1, &qs); 3176e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3177c8e26105Sjp151216 degrade_svc(); 3178c5c4113dSnw141292 idmapdlog(LOG_ERR, 3179e8c27ec8Sbaban "Failed to create request for AD lookup by winname"); 3180e8c27ec8Sbaban return (retcode); 3181c5c4113dSnw141292 } 3182c5c4113dSnw141292 3183c8e26105Sjp151216 restore_svc(); 3184c8e26105Sjp151216 3185e8c27ec8Sbaban if (state != NULL) 3186e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 3187e8c27ec8Sbaban state->ad_unixgroup_attr); 3188c5c4113dSnw141292 3189e8c27ec8Sbaban retcode = idmap_name2sid_batch_add1(qs, name, domain, eunixtype, 3190e8c27ec8Sbaban canonname, sidprefix, rid, wintype, unixname, &rc); 3191c5c4113dSnw141292 3192e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 319384decf41Sjp151216 idmap_lookup_release_batch(&qs); 3194c5c4113dSnw141292 else 3195c5c4113dSnw141292 retcode = idmap_lookup_batch_end(&qs, NULL); 3196c5c4113dSnw141292 3197c5c4113dSnw141292 if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 3198c5c4113dSnw141292 goto retry; 3199c8e26105Sjp151216 else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 3200c8e26105Sjp151216 degrade_svc(); 3201c5c4113dSnw141292 3202c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) { 3203e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by winname failed"); 3204c5c4113dSnw141292 return (retcode); 3205e8c27ec8Sbaban } 3206c5c4113dSnw141292 return (rc); 3207c5c4113dSnw141292 } 3208c5c4113dSnw141292 3209cd37da74Snw141292 static 3210cd37da74Snw141292 idmap_retcode 3211c5c4113dSnw141292 lookup_name2sid(sqlite *cache, const char *name, const char *domain, 3212cd37da74Snw141292 int *is_wuser, char **canonname, char **sidprefix, 3213cd37da74Snw141292 idmap_rid_t *rid, idmap_mapping *req) 3214cd37da74Snw141292 { 3215c5c4113dSnw141292 int type; 3216c5c4113dSnw141292 idmap_retcode retcode; 3217c5c4113dSnw141292 3218cd37da74Snw141292 *sidprefix = NULL; 3219e8c27ec8Sbaban if (canonname != NULL) 3220cd37da74Snw141292 *canonname = NULL; 3221cd37da74Snw141292 3222e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3223cd37da74Snw141292 retcode = lookup_wksids_name2sid(name, canonname, sidprefix, rid, 3224cd37da74Snw141292 &type); 322562c60062Sbaban if (retcode == IDMAP_SUCCESS) { 3226e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 322762c60062Sbaban goto out; 322862c60062Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 322962c60062Sbaban return (retcode); 323062c60062Sbaban } 323162c60062Sbaban 3232e8c27ec8Sbaban /* Lookup cache */ 3233cd37da74Snw141292 retcode = lookup_cache_name2sid(cache, name, domain, canonname, 3234cd37da74Snw141292 sidprefix, rid, &type); 3235e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 3236e8c27ec8Sbaban req->direction |= _IDMAP_F_DONT_UPDATE_NAMECACHE; 3237e8c27ec8Sbaban goto out; 3238e8c27ec8Sbaban } else if (retcode != IDMAP_ERR_NOTFOUND) { 3239e8c27ec8Sbaban return (retcode); 3240e8c27ec8Sbaban } 3241e8c27ec8Sbaban 3242e8c27ec8Sbaban /* Lookup AD */ 3243e8c27ec8Sbaban retcode = ad_lookup_by_winname(NULL, name, domain, _IDMAP_T_UNDEF, 3244e8c27ec8Sbaban canonname, sidprefix, rid, &type, NULL); 3245c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3246c5c4113dSnw141292 return (retcode); 3247e8c27ec8Sbaban /* Don't need to set req->direction |= _IDMAP_F_LOOKUP_AD; */ 3248c5c4113dSnw141292 324962c60062Sbaban out: 3250c5c4113dSnw141292 /* 3251c5c4113dSnw141292 * Entry found (cache or Windows lookup) 3252cd37da74Snw141292 * is_wuser is both input as well as output parameter 3253c5c4113dSnw141292 */ 3254e8c27ec8Sbaban if (*is_wuser == 1 && type != _IDMAP_T_USER) 3255e8c27ec8Sbaban retcode = IDMAP_ERR_NOTUSER; 3256e8c27ec8Sbaban else if (*is_wuser == 0 && type != _IDMAP_T_GROUP) 3257e8c27ec8Sbaban retcode = IDMAP_ERR_NOTGROUP; 3258e8c27ec8Sbaban else if (*is_wuser == -1) { 3259c5c4113dSnw141292 /* Caller wants to know if its user or group */ 3260c5c4113dSnw141292 if (type == _IDMAP_T_USER) 3261cd37da74Snw141292 *is_wuser = 1; 3262c5c4113dSnw141292 else if (type == _IDMAP_T_GROUP) 3263cd37da74Snw141292 *is_wuser = 0; 3264e8c27ec8Sbaban else 3265e8c27ec8Sbaban retcode = IDMAP_ERR_SID; 3266e8c27ec8Sbaban } 3267e8c27ec8Sbaban 3268e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3269cd37da74Snw141292 free(*sidprefix); 3270cd37da74Snw141292 *sidprefix = NULL; 3271e8c27ec8Sbaban if (canonname != NULL) { 3272cd37da74Snw141292 free(*canonname); 3273cd37da74Snw141292 *canonname = NULL; 3274cd37da74Snw141292 } 3275c5c4113dSnw141292 } 3276c5c4113dSnw141292 return (retcode); 3277c5c4113dSnw141292 } 3278c5c4113dSnw141292 3279cd37da74Snw141292 static 3280cd37da74Snw141292 idmap_retcode 3281c5c4113dSnw141292 name_based_mapping_pid2sid(sqlite *db, sqlite *cache, const char *unixname, 3282cd37da74Snw141292 int is_user, idmap_mapping *req, idmap_id_res *res) 3283cd37da74Snw141292 { 3284c5c4113dSnw141292 const char *winname, *windomain; 3285cd37da74Snw141292 char *canonname; 3286c8e26105Sjp151216 char *default_domain = NULL; 3287c5c4113dSnw141292 char *sql = NULL, *errmsg = NULL; 3288c5c4113dSnw141292 idmap_retcode retcode; 3289c5c4113dSnw141292 char *end; 3290c5c4113dSnw141292 const char **values; 3291c5c4113dSnw141292 sqlite_vm *vm = NULL; 3292cd37da74Snw141292 int ncol, r, nrow; 3293cd37da74Snw141292 int is_wuser; 3294e8c27ec8Sbaban const char *me = "name_based_mapping_pid2sid"; 3295e8c27ec8Sbaban 3296e8c27ec8Sbaban assert(unixname != NULL); /* We have unixname */ 3297e8c27ec8Sbaban assert(req->id2name == NULL); /* We don't have winname */ 3298e8c27ec8Sbaban assert(res->id.idmap_id_u.sid.prefix == NULL); /* No SID either */ 3299c5c4113dSnw141292 3300c5c4113dSnw141292 RDLOCK_CONFIG(); 3301c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 3302c8e26105Sjp151216 default_domain = 3303c8e26105Sjp151216 strdup(_idmapdstate.cfg->pgcfg.default_domain); 3304c8e26105Sjp151216 if (default_domain == NULL) { 3305c5c4113dSnw141292 UNLOCK_CONFIG(); 3306c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3307c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3308c5c4113dSnw141292 goto out; 3309c5c4113dSnw141292 } 3310c5c4113dSnw141292 } 3311c5c4113dSnw141292 UNLOCK_CONFIG(); 3312c5c4113dSnw141292 3313c5c4113dSnw141292 sql = sqlite_mprintf( 3314cd37da74Snw141292 "SELECT winname_display, windomain, w2u_order FROM namerules WHERE " 3315c5c4113dSnw141292 "u2w_order > 0 AND is_user = %d AND " 3316c5c4113dSnw141292 "(unixname = %Q OR unixname = '*') " 3317cd37da74Snw141292 "ORDER BY u2w_order ASC;", is_user, unixname); 3318c5c4113dSnw141292 if (sql == NULL) { 3319c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 3320c5c4113dSnw141292 retcode = IDMAP_ERR_MEMORY; 3321c5c4113dSnw141292 goto out; 3322c5c4113dSnw141292 } 3323c5c4113dSnw141292 3324c5c4113dSnw141292 if (sqlite_compile(db, sql, NULL, &vm, &errmsg) != SQLITE_OK) { 3325c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3326cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3327cd37da74Snw141292 CHECK_NULL(errmsg)); 3328c5c4113dSnw141292 sqlite_freemem(errmsg); 3329c5c4113dSnw141292 goto out; 3330c5c4113dSnw141292 } 3331c5c4113dSnw141292 3332cd37da74Snw141292 for (nrow = 0; ; ) { 3333c5c4113dSnw141292 r = sqlite_step(vm, &ncol, &values, NULL); 333484decf41Sjp151216 assert(r != SQLITE_LOCKED && r != SQLITE_BUSY); 333584decf41Sjp151216 if (r == SQLITE_ROW) { 3336cd37da74Snw141292 nrow++; 3337c5c4113dSnw141292 if (ncol < 3) { 3338c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3339c5c4113dSnw141292 goto out; 3340c5c4113dSnw141292 } 3341c5c4113dSnw141292 if (values[0] == NULL) { 3342c5c4113dSnw141292 /* values [1] and [2] can be null */ 3343c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3344c5c4113dSnw141292 goto out; 3345c5c4113dSnw141292 } 3346c5c4113dSnw141292 if (EMPTY_NAME(values[0])) { 3347c5c4113dSnw141292 retcode = IDMAP_ERR_NOMAPPING; 3348c5c4113dSnw141292 goto out; 3349c5c4113dSnw141292 } 3350cd37da74Snw141292 3351cd37da74Snw141292 if (values[0][0] == '*') { 3352cd37da74Snw141292 if (nrow > 1) { 3353cd37da74Snw141292 /* 3354cd37da74Snw141292 * There were non-wildcard rules where 3355cd37da74Snw141292 * windows identity doesn't exist 3356cd37da74Snw141292 */ 3357cd37da74Snw141292 retcode = IDMAP_ERR_NOMAPPING; 3358cd37da74Snw141292 goto out; 3359cd37da74Snw141292 } 3360cd37da74Snw141292 winname = unixname; 3361cd37da74Snw141292 } else { 3362cd37da74Snw141292 winname = values[0]; 3363cd37da74Snw141292 } 3364cd37da74Snw141292 336562c60062Sbaban if (values[1] != NULL) 3366c5c4113dSnw141292 windomain = values[1]; 3367c8e26105Sjp151216 else if (default_domain != NULL) 3368c8e26105Sjp151216 windomain = default_domain; 3369c5c4113dSnw141292 else { 3370cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: no domain", me); 3371c5c4113dSnw141292 retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 3372c5c4113dSnw141292 goto out; 3373c5c4113dSnw141292 } 3374c5c4113dSnw141292 /* Lookup winname@domain to sid */ 3375cd37da74Snw141292 3376cd37da74Snw141292 is_wuser = res->id.idtype == IDMAP_USID ? 1 3377cd37da74Snw141292 : res->id.idtype == IDMAP_GSID ? 0 3378cd37da74Snw141292 : -1; 3379cd37da74Snw141292 3380c5c4113dSnw141292 retcode = lookup_name2sid(cache, winname, windomain, 3381cd37da74Snw141292 &is_wuser, &canonname, 3382cd37da74Snw141292 &res->id.idmap_id_u.sid.prefix, 3383c5c4113dSnw141292 &res->id.idmap_id_u.sid.rid, req); 3384e8c27ec8Sbaban 3385c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 3386c5c4113dSnw141292 continue; 3387c5c4113dSnw141292 } 3388cd37da74Snw141292 3389c5c4113dSnw141292 goto out; 3390c5c4113dSnw141292 } else if (r == SQLITE_DONE) { 3391c5c4113dSnw141292 retcode = IDMAP_ERR_NOTFOUND; 3392c5c4113dSnw141292 goto out; 3393c5c4113dSnw141292 } else { 3394c5c4113dSnw141292 (void) sqlite_finalize(vm, &errmsg); 3395c5c4113dSnw141292 vm = NULL; 3396cd37da74Snw141292 idmapdlog(LOG_ERR, "%s: database error (%s)", me, 3397cd37da74Snw141292 CHECK_NULL(errmsg)); 3398c5c4113dSnw141292 sqlite_freemem(errmsg); 3399c5c4113dSnw141292 retcode = IDMAP_ERR_INTERNAL; 3400c5c4113dSnw141292 goto out; 3401c5c4113dSnw141292 } 3402c5c4113dSnw141292 } 3403c5c4113dSnw141292 3404c5c4113dSnw141292 out: 340562c60062Sbaban if (sql != NULL) 3406c5c4113dSnw141292 sqlite_freemem(sql); 3407c5c4113dSnw141292 if (retcode == IDMAP_SUCCESS) { 3408cd37da74Snw141292 res->id.idtype = is_wuser ? IDMAP_USID : IDMAP_GSID; 3409cd37da74Snw141292 341062c60062Sbaban if (values[2] != NULL) 3411c5c4113dSnw141292 res->direction = 3412651c0131Sbaban (strtol(values[2], &end, 10) == 0)? 3413651c0131Sbaban IDMAP_DIRECTION_U2W:IDMAP_DIRECTION_BI; 3414c5c4113dSnw141292 else 3415651c0131Sbaban res->direction = IDMAP_DIRECTION_U2W; 34168e228215Sdm199847 3417cd37da74Snw141292 req->id2name = canonname; 34188e228215Sdm199847 if (req->id2name != NULL) { 3419c8e26105Sjp151216 if (windomain == default_domain) { 34208e228215Sdm199847 req->id2domain = (char *)windomain; 3421c8e26105Sjp151216 default_domain = NULL; 34228e228215Sdm199847 } else { 34238e228215Sdm199847 req->id2domain = strdup(windomain); 34248e228215Sdm199847 } 3425c5c4113dSnw141292 } 3426c5c4113dSnw141292 } 342762c60062Sbaban if (vm != NULL) 3428c5c4113dSnw141292 (void) sqlite_finalize(vm, NULL); 3429c8e26105Sjp151216 if (default_domain != NULL) 3430c8e26105Sjp151216 free(default_domain); 3431c5c4113dSnw141292 return (retcode); 3432c5c4113dSnw141292 } 3433c5c4113dSnw141292 3434cd37da74Snw141292 /* 3435cd37da74Snw141292 * Convention when processing unix2win requests: 3436cd37da74Snw141292 * 3437cd37da74Snw141292 * Unix identity: 3438cd37da74Snw141292 * req->id1name = 3439cd37da74Snw141292 * unixname if given otherwise unixname found will be placed 3440cd37da74Snw141292 * here. 3441cd37da74Snw141292 * req->id1domain = 3442cd37da74Snw141292 * NOT USED 3443cd37da74Snw141292 * req->id1.idtype = 3444cd37da74Snw141292 * Given type (IDMAP_UID or IDMAP_GID) 3445cd37da74Snw141292 * req->id1..[uid or gid] = 3446cd37da74Snw141292 * UID/GID if given otherwise UID/GID found will be placed here. 3447cd37da74Snw141292 * 3448cd37da74Snw141292 * Windows identity: 3449cd37da74Snw141292 * req->id2name = 3450cd37da74Snw141292 * winname found will be placed here. 3451cd37da74Snw141292 * req->id2domain = 3452cd37da74Snw141292 * windomain found will be placed here. 3453cd37da74Snw141292 * res->id.idtype = 3454cd37da74Snw141292 * Target type initialized from req->id2.idtype. If 3455cd37da74Snw141292 * it is IDMAP_SID then actual type (IDMAP_USID/GSID) found 3456cd37da74Snw141292 * will be placed here. 3457cd37da74Snw141292 * req->id..sid.[prefix, rid] = 3458cd37da74Snw141292 * SID found will be placed here. 3459cd37da74Snw141292 * 3460cd37da74Snw141292 * Others: 3461cd37da74Snw141292 * res->retcode = 3462cd37da74Snw141292 * Return status for this request will be placed here. 3463cd37da74Snw141292 * res->direction = 3464cd37da74Snw141292 * Direction found will be placed here. Direction 3465cd37da74Snw141292 * meaning whether the resultant mapping is valid 3466cd37da74Snw141292 * only from unix2win or bi-directional. 3467cd37da74Snw141292 * req->direction = 3468cd37da74Snw141292 * INTERNAL USE. Used by idmapd to set various 3469cd37da74Snw141292 * flags (_IDMAP_F_xxxx) to aid in processing 3470cd37da74Snw141292 * of the request. 3471cd37da74Snw141292 * req->id2.idtype = 3472cd37da74Snw141292 * INTERNAL USE. Initially this is the requested target 3473cd37da74Snw141292 * type and is used to initialize res->id.idtype. 3474cd37da74Snw141292 * ad_lookup_batch() uses this field temporarily to store 3475cd37da74Snw141292 * sid_type obtained by the batched AD lookups and after 3476cd37da74Snw141292 * use resets it to IDMAP_NONE to prevent xdr from 3477cd37da74Snw141292 * mis-interpreting the contents of req->id2. 3478cd37da74Snw141292 * req->id2..[uid or gid or sid] = 3479cd37da74Snw141292 * NOT USED 3480cd37da74Snw141292 */ 3481cd37da74Snw141292 3482cd37da74Snw141292 /* 3483cd37da74Snw141292 * This function does the following: 3484cd37da74Snw141292 * 1. Lookup well-known SIDs table. 3485cd37da74Snw141292 * 2. Lookup cache. 3486cd37da74Snw141292 * 3. Check if the client does not want new mapping to be allocated 3487cd37da74Snw141292 * in which case this pass is the final pass. 3488e8c27ec8Sbaban * 4. Set AD/NLDAP lookup flags if it determines that the next stage needs 3489e8c27ec8Sbaban * to do AD/NLDAP lookup. 3490cd37da74Snw141292 */ 3491c5c4113dSnw141292 idmap_retcode 3492e8c27ec8Sbaban pid2sid_first_pass(lookup_state_t *state, sqlite *cache, 3493c5c4113dSnw141292 idmap_mapping *req, idmap_id_res *res, int is_user, 3494cd37da74Snw141292 int getname) 3495cd37da74Snw141292 { 3496e8c27ec8Sbaban idmap_retcode retcode; 3497e8c27ec8Sbaban bool_t gen_localsid_on_err = FALSE; 3498c5c4113dSnw141292 3499e8c27ec8Sbaban /* Initialize result */ 3500c5c4113dSnw141292 res->id.idtype = req->id2.idtype; 3501e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_UNDEF; 3502c5c4113dSnw141292 3503e8c27ec8Sbaban if (req->id2.idmap_id_u.sid.prefix != NULL) { 3504e8c27ec8Sbaban /* sanitize sidprefix */ 3505e8c27ec8Sbaban free(req->id2.idmap_id_u.sid.prefix); 3506e8c27ec8Sbaban req->id2.idmap_id_u.sid.prefix = NULL; 3507e8c27ec8Sbaban } 3508e8c27ec8Sbaban 3509e8c27ec8Sbaban /* Lookup well-known SIDs table */ 3510c5c4113dSnw141292 retcode = lookup_wksids_pid2sid(req, res, is_user); 3511c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3512c5c4113dSnw141292 goto out; 3513c5c4113dSnw141292 3514e8c27ec8Sbaban /* Lookup cache */ 3515c5c4113dSnw141292 retcode = lookup_cache_pid2sid(cache, req, res, is_user, getname); 3516c5c4113dSnw141292 if (retcode != IDMAP_ERR_NOTFOUND) 3517c5c4113dSnw141292 goto out; 3518c5c4113dSnw141292 3519c5c4113dSnw141292 /* Ephemeral ids cannot be allocated during pid2sid */ 3520c5c4113dSnw141292 if (IS_EPHEMERAL(req->id1.idmap_id_u.uid)) { 352162c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3522c5c4113dSnw141292 goto out; 3523c5c4113dSnw141292 } 3524c5c4113dSnw141292 3525c5c4113dSnw141292 if (DO_NOT_ALLOC_NEW_ID_MAPPING(req) || AVOID_NAMESERVICE(req)) { 3526e8c27ec8Sbaban gen_localsid_on_err = TRUE; 352762c60062Sbaban retcode = IDMAP_ERR_NOMAPPING; 3528c5c4113dSnw141292 goto out; 3529c5c4113dSnw141292 } 3530c5c4113dSnw141292 3531e8c27ec8Sbaban /* Set flags for the next stage */ 3532e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) { 3533e8c27ec8Sbaban /* 3534e8c27ec8Sbaban * If AD-based name mapping is enabled then the next stage 3535e8c27ec8Sbaban * will need to lookup AD using unixname to get the 3536e8c27ec8Sbaban * corresponding winname. 3537e8c27ec8Sbaban */ 3538e8c27ec8Sbaban if (req->id1name == NULL) { 3539e8c27ec8Sbaban /* Get unixname if only pid is given. */ 3540e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, 3541e8c27ec8Sbaban is_user, &req->id1name); 3542e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3543e8c27ec8Sbaban goto out; 3544c5c4113dSnw141292 } 3545e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_AD; 3546e8c27ec8Sbaban state->ad_nqueries++; 3547e8c27ec8Sbaban } else if (NLDAP_OR_MIXED_MODE(req->id1.idtype, state)) { 3548e8c27ec8Sbaban /* 3549e8c27ec8Sbaban * If native LDAP or mixed mode is enabled for name mapping 3550e8c27ec8Sbaban * then the next stage will need to lookup native LDAP using 3551e8c27ec8Sbaban * unixname/pid to get the corresponding winname. 3552e8c27ec8Sbaban */ 3553e8c27ec8Sbaban req->direction |= _IDMAP_F_LOOKUP_NLDAP; 3554e8c27ec8Sbaban state->nldap_nqueries++; 3555c5c4113dSnw141292 } 3556c5c4113dSnw141292 3557e8c27ec8Sbaban /* 3558e8c27ec8Sbaban * Failed to find non-expired entry in cache. Set the flag to 3559e8c27ec8Sbaban * indicate that we are not done yet. 3560e8c27ec8Sbaban */ 3561e8c27ec8Sbaban state->pid2sid_done = FALSE; 3562e8c27ec8Sbaban req->direction |= _IDMAP_F_NOTDONE; 3563e8c27ec8Sbaban retcode = IDMAP_SUCCESS; 3564e8c27ec8Sbaban 3565e8c27ec8Sbaban out: 3566e8c27ec8Sbaban res->retcode = idmap_stat4prot(retcode); 3567e8c27ec8Sbaban if (ARE_WE_DONE(req->direction) && res->retcode != IDMAP_SUCCESS) 3568e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 3569e8c27ec8Sbaban (void) generate_localsid(req, res, is_user); 3570e8c27ec8Sbaban return (retcode); 3571e8c27ec8Sbaban } 3572e8c27ec8Sbaban 3573e8c27ec8Sbaban idmap_retcode 3574e8c27ec8Sbaban pid2sid_second_pass(lookup_state_t *state, sqlite *cache, sqlite *db, 3575e8c27ec8Sbaban idmap_mapping *req, idmap_id_res *res, int is_user) 3576e8c27ec8Sbaban { 3577e8c27ec8Sbaban bool_t gen_localsid_on_err = TRUE; 3578e8c27ec8Sbaban idmap_retcode retcode = IDMAP_SUCCESS; 3579e8c27ec8Sbaban 3580e8c27ec8Sbaban /* Check if second pass is needed */ 3581e8c27ec8Sbaban if (ARE_WE_DONE(req->direction)) 3582e8c27ec8Sbaban return (res->retcode); 3583e8c27ec8Sbaban 3584e8c27ec8Sbaban /* Get status from previous pass */ 3585e8c27ec8Sbaban retcode = res->retcode; 3586e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3587e8c27ec8Sbaban goto out; 3588e8c27ec8Sbaban 3589e8c27ec8Sbaban /* 3590e8c27ec8Sbaban * If directory-based name mapping is enabled then the winname 3591e8c27ec8Sbaban * may already have been retrieved from the AD object (AD-mode) 3592e8c27ec8Sbaban * or from native LDAP object (nldap-mode or mixed-mode) -- done. 3593e8c27ec8Sbaban */ 3594e8c27ec8Sbaban if (res->id.idmap_id_u.sid.prefix != NULL || req->id2name != NULL) { 3595e8c27ec8Sbaban if (AD_MODE(req->id1.idtype, state)) 3596e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 3597e8c27ec8Sbaban else if (NLDAP_MODE(req->id1.idtype, state)) 3598e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_BI; 3599e8c27ec8Sbaban else if (MIXED_MODE(req->id1.idtype, state)) 3600e8c27ec8Sbaban res->direction = IDMAP_DIRECTION_W2U; 3601e8c27ec8Sbaban goto out; 3602e8c27ec8Sbaban } 3603e8c27ec8Sbaban 3604e8c27ec8Sbaban if (req->id1name == NULL) { 3605e8c27ec8Sbaban /* Get unixname from name service */ 3606e8c27ec8Sbaban retcode = ns_lookup_bypid(req->id1.idmap_id_u.uid, is_user, 3607e8c27ec8Sbaban &req->id1name); 3608e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3609e8c27ec8Sbaban goto out; 3610e8c27ec8Sbaban } else if (req->id1.idmap_id_u.uid == SENTINEL_PID) { 3611e8c27ec8Sbaban /* Get pid from name service */ 3612e8c27ec8Sbaban retcode = ns_lookup_byname(req->id1name, NULL, &req->id1); 3613e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3614e8c27ec8Sbaban gen_localsid_on_err = FALSE; 3615e8c27ec8Sbaban goto out; 3616e8c27ec8Sbaban } 3617e8c27ec8Sbaban } 3618e8c27ec8Sbaban 3619e8c27ec8Sbaban /* Use unixname to evaluate local name-based mapping rules */ 3620e8c27ec8Sbaban retcode = name_based_mapping_pid2sid(db, cache, req->id1name, is_user, 3621c5c4113dSnw141292 req, res); 3622c5c4113dSnw141292 if (retcode == IDMAP_ERR_NOTFOUND) { 3623c5c4113dSnw141292 retcode = generate_localsid(req, res, is_user); 3624e8c27ec8Sbaban gen_localsid_on_err = FALSE; 3625e8c27ec8Sbaban } 3626c5c4113dSnw141292 3627c5c4113dSnw141292 out: 3628c5c4113dSnw141292 res->retcode = idmap_stat4prot(retcode); 3629e8c27ec8Sbaban if (res->retcode != IDMAP_SUCCESS) { 3630e8c27ec8Sbaban req->direction = _IDMAP_F_DONE; 3631e8c27ec8Sbaban if (gen_localsid_on_err == TRUE) 3632e8c27ec8Sbaban (void) generate_localsid(req, res, is_user); 3633e8c27ec8Sbaban } 3634e8c27ec8Sbaban if (!ARE_WE_DONE(req->direction)) 3635e8c27ec8Sbaban state->pid2sid_done = FALSE; 3636c5c4113dSnw141292 return (retcode); 3637c5c4113dSnw141292 } 3638c5c4113dSnw141292 3639cd37da74Snw141292 static 3640cd37da74Snw141292 idmap_retcode 3641e8c27ec8Sbaban ad_lookup_by_sid(lookup_state_t *state, 3642e8c27ec8Sbaban const char *sidprefix, idmap_rid_t rid, int eunixtype, 3643e8c27ec8Sbaban char **name, char **domain, int *type, char **unixname) 3644cd37da74Snw141292 { 3645e8c27ec8Sbaban int retries = 0; 3646c5c4113dSnw141292 idmap_query_state_t *qs = NULL; 3647c5c4113dSnw141292 idmap_retcode rc, retcode; 3648c5c4113dSnw141292 3649e8c27ec8Sbaban retry: 3650e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, 1, &qs); 3651e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3652c8e26105Sjp151216 degrade_svc(); 3653c5c4113dSnw141292 idmapdlog(LOG_ERR, 3654e8c27ec8Sbaban "Failed to create request for AD lookup by SID"); 3655e8c27ec8Sbaban return (retcode); 3656c5c4113dSnw141292 } 3657c5c4113dSnw141292 3658c8e26105Sjp151216 restore_svc(); 3659c8e26105Sjp151216 3660e8c27ec8Sbaban if (state != NULL) 3661e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 3662e8c27ec8Sbaban state->ad_unixgroup_attr); 3663c5c4113dSnw141292 3664e8c27ec8Sbaban retcode = idmap_sid2name_batch_add1(qs, sidprefix, &rid, eunixtype, 3665e8c27ec8Sbaban name, domain, type, unixname, &rc); 3666e8c27ec8Sbaban 3667e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3668e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 3669e8c27ec8Sbaban else 3670e8c27ec8Sbaban retcode = idmap_lookup_batch_end(&qs, NULL); 3671e8c27ec8Sbaban 3672e8c27ec8Sbaban if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 3673e8c27ec8Sbaban goto retry; 3674e8c27ec8Sbaban else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 3675c8e26105Sjp151216 degrade_svc(); 3676c5c4113dSnw141292 3677e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 3678e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by SID failed"); 3679c5c4113dSnw141292 return (retcode); 3680c5c4113dSnw141292 } 3681e8c27ec8Sbaban return (rc); 3682e8c27ec8Sbaban } 3683c5c4113dSnw141292 3684cd37da74Snw141292 static 3685cd37da74Snw141292 int 3686651c0131Sbaban copy_mapping_request(idmap_mapping *mapping, idmap_mapping *request) 3687c5c4113dSnw141292 { 3688651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 3689651c0131Sbaban 3690c5c4113dSnw141292 mapping->flag = request->flag; 3691e8c27ec8Sbaban mapping->direction = _IDMAP_F_DONE; 3692651c0131Sbaban mapping->id2.idtype = request->id2.idtype; 3693c5c4113dSnw141292 3694c5c4113dSnw141292 mapping->id1.idtype = request->id1.idtype; 3695cd37da74Snw141292 if (IS_REQUEST_SID(*request, 1)) { 3696c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.rid = 3697c5c4113dSnw141292 request->id1.idmap_id_u.sid.rid; 3698651c0131Sbaban if (!EMPTY_STRING(request->id1.idmap_id_u.sid.prefix)) { 3699c5c4113dSnw141292 mapping->id1.idmap_id_u.sid.prefix = 3700c5c4113dSnw141292 strdup(request->id1.idmap_id_u.sid.prefix); 3701651c0131Sbaban if (mapping->id1.idmap_id_u.sid.prefix == NULL) 37028e228215Sdm199847 goto errout; 3703651c0131Sbaban } 3704c5c4113dSnw141292 } else { 3705c5c4113dSnw141292 mapping->id1.idmap_id_u.uid = request->id1.idmap_id_u.uid; 3706c5c4113dSnw141292 } 3707c5c4113dSnw141292 3708e8c27ec8Sbaban if (!EMPTY_STRING(request->id1domain)) { 37098e228215Sdm199847 mapping->id1domain = strdup(request->id1domain); 37108e228215Sdm199847 if (mapping->id1domain == NULL) 37118e228215Sdm199847 goto errout; 3712e8c27ec8Sbaban } 3713c5c4113dSnw141292 3714e8c27ec8Sbaban if (!EMPTY_STRING(request->id1name)) { 37158e228215Sdm199847 mapping->id1name = strdup(request->id1name); 37168e228215Sdm199847 if (mapping->id1name == NULL) 37178e228215Sdm199847 goto errout; 3718e8c27ec8Sbaban } 3719c5c4113dSnw141292 3720651c0131Sbaban /* We don't need the rest of the request i.e request->id2 */ 3721651c0131Sbaban return (0); 3722c5c4113dSnw141292 3723651c0131Sbaban errout: 37248e228215Sdm199847 if (mapping->id1.idmap_id_u.sid.prefix != NULL) 3725651c0131Sbaban free(mapping->id1.idmap_id_u.sid.prefix); 37268e228215Sdm199847 if (mapping->id1domain != NULL) 37278e228215Sdm199847 free(mapping->id1domain); 37288e228215Sdm199847 if (mapping->id1name != NULL) 37298e228215Sdm199847 free(mapping->id1name); 3730651c0131Sbaban 3731651c0131Sbaban (void) memset(mapping, 0, sizeof (*mapping)); 3732651c0131Sbaban return (-1); 3733c5c4113dSnw141292 } 3734c5c4113dSnw141292 3735c5c4113dSnw141292 3736c5c4113dSnw141292 idmap_retcode 3737c5c4113dSnw141292 get_w2u_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 3738cd37da74Snw141292 idmap_mapping *mapping) 3739cd37da74Snw141292 { 3740c5c4113dSnw141292 idmap_id_res idres; 3741c5c4113dSnw141292 lookup_state_t state; 3742dd5829d1Sbaban char *cp; 3743c5c4113dSnw141292 idmap_retcode retcode; 3744c5c4113dSnw141292 const char *winname, *windomain; 3745c5c4113dSnw141292 3746c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 3747c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 3748c5c4113dSnw141292 3749e8c27ec8Sbaban /* Get directory-based name mapping info */ 3750e8c27ec8Sbaban retcode = get_ds_namemap_type(&state); 3751e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3752c5c4113dSnw141292 goto out; 3753c5c4113dSnw141292 3754e8c27ec8Sbaban /* 3755e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 3756e8c27ec8Sbaban * empty strings are not copied from "request" to 3757e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 3758e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 3759e8c27ec8Sbaban * check for empty strings henceforth. 3760e8c27ec8Sbaban */ 3761651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 3762651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 3763651c0131Sbaban goto out; 3764651c0131Sbaban } 3765c5c4113dSnw141292 37668e228215Sdm199847 winname = mapping->id1name; 37678e228215Sdm199847 windomain = mapping->id1domain; 3768c5c4113dSnw141292 3769e8c27ec8Sbaban if (winname == NULL && windomain != NULL) { 3770c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 3771c5c4113dSnw141292 goto out; 3772c5c4113dSnw141292 } 3773c5c4113dSnw141292 3774e8c27ec8Sbaban /* Need atleast winname or sid to proceed */ 3775e8c27ec8Sbaban if (winname == NULL && mapping->id1.idmap_id_u.sid.prefix == NULL) { 3776e8c27ec8Sbaban retcode = IDMAP_ERR_ARG; 3777e8c27ec8Sbaban goto out; 3778e8c27ec8Sbaban } 3779e8c27ec8Sbaban 3780e8c27ec8Sbaban /* 3781e8c27ec8Sbaban * If domainname is not given but we have a fully qualified 3782e8c27ec8Sbaban * winname then extract the domainname from the winname, 3783e8c27ec8Sbaban * otherwise use the default_domain from the config 3784e8c27ec8Sbaban */ 3785e8c27ec8Sbaban if (winname != NULL && windomain == NULL) { 37868e228215Sdm199847 retcode = IDMAP_SUCCESS; 3787dd5829d1Sbaban if ((cp = strchr(winname, '@')) != NULL) { 3788dd5829d1Sbaban *cp = '\0'; 37898e228215Sdm199847 mapping->id1domain = strdup(cp + 1); 37908e228215Sdm199847 if (mapping->id1domain == NULL) 37918e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 3792e8c27ec8Sbaban } else if (lookup_wksids_name2sid(winname, NULL, NULL, NULL, 3793e8c27ec8Sbaban NULL) != IDMAP_SUCCESS) { 3794e8c27ec8Sbaban /* well-known SIDs don't need domain */ 37958e228215Sdm199847 RDLOCK_CONFIG(); 3796c8e26105Sjp151216 if (_idmapdstate.cfg->pgcfg.default_domain != NULL) { 37978e228215Sdm199847 mapping->id1domain = 37988e228215Sdm199847 strdup(_idmapdstate.cfg-> 3799c8e26105Sjp151216 pgcfg.default_domain); 38008e228215Sdm199847 if (mapping->id1domain == NULL) 38018e228215Sdm199847 retcode = IDMAP_ERR_MEMORY; 3802e8c27ec8Sbaban } else { 3803e8c27ec8Sbaban retcode = IDMAP_ERR_DOMAIN_NOTFOUND; 38048e228215Sdm199847 } 3805c5c4113dSnw141292 UNLOCK_CONFIG(); 38068e228215Sdm199847 } 3807c5c4113dSnw141292 if (retcode != IDMAP_SUCCESS) 3808c5c4113dSnw141292 goto out; 3809c5c4113dSnw141292 } 3810c5c4113dSnw141292 3811e8c27ec8Sbaban /* 3812e8c27ec8Sbaban * First pass looks up the well-known SIDs table and cache 3813e8c27ec8Sbaban * and handles localSIDs 3814e8c27ec8Sbaban */ 3815c5c4113dSnw141292 state.sid2pid_done = TRUE; 3816c5c4113dSnw141292 retcode = sid2pid_first_pass(&state, cache, mapping, &idres); 3817c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 3818c5c4113dSnw141292 goto out; 3819c5c4113dSnw141292 3820e8c27ec8Sbaban /* AD lookup by winname or by sid */ 3821e8c27ec8Sbaban if (state.ad_nqueries > 0) { 3822e8c27ec8Sbaban retcode = ad_lookup(&state, mapping, &idres, 1, 1, 0); 3823e8c27ec8Sbaban if (IDMAP_ERROR(retcode)) 3824e8c27ec8Sbaban goto out; 3825c5c4113dSnw141292 } 3826c5c4113dSnw141292 3827e8c27ec8Sbaban /* 3828e8c27ec8Sbaban * If nldap-based name mapping is enabled then lookup native LDAP 3829e8c27ec8Sbaban * directory service by winname to get pid and unixname. Ignore 3830e8c27ec8Sbaban * non-fatal errors in which case we simply fallback to evaluating 3831e8c27ec8Sbaban * local name-based mapping rules. 3832e8c27ec8Sbaban */ 3833e8c27ec8Sbaban if (mapping->id1name != NULL && NLDAP_MODE(idres.id.idtype, (&state))) { 3834e8c27ec8Sbaban retcode = nldap_lookup(mapping, &idres, 1, 1); 3835e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 3836e8c27ec8Sbaban goto out; 3837e8c27ec8Sbaban idres.retcode = IDMAP_SUCCESS; 3838e8c27ec8Sbaban } 3839e8c27ec8Sbaban 3840e8c27ec8Sbaban /* Next pass performs name-based mapping and ephemeral mapping. */ 3841c5c4113dSnw141292 state.sid2pid_done = TRUE; 3842c5c4113dSnw141292 retcode = sid2pid_second_pass(&state, cache, db, mapping, &idres); 3843c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.sid2pid_done == TRUE) 3844c5c4113dSnw141292 goto out; 3845c5c4113dSnw141292 3846c5c4113dSnw141292 /* Update cache */ 3847c5c4113dSnw141292 (void) update_cache_sid2pid(&state, cache, mapping, &idres); 3848c5c4113dSnw141292 3849c5c4113dSnw141292 out: 3850e8c27ec8Sbaban /* 3851e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 3852e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 3853e8c27ec8Sbaban * free idres. 3854e8c27ec8Sbaban */ 3855c5c4113dSnw141292 mapping->direction = idres.direction; 3856c5c4113dSnw141292 mapping->id2 = idres.id; 3857c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 3858e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 385962c60062Sbaban mapping->id2.idmap_id_u.uid = UID_NOBODY; 3860c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 3861e8c27ec8Sbaban cleanup_lookup_state(&state); 3862c5c4113dSnw141292 return (retcode); 3863c5c4113dSnw141292 } 3864c5c4113dSnw141292 3865c5c4113dSnw141292 idmap_retcode 3866c5c4113dSnw141292 get_u2w_mapping(sqlite *cache, sqlite *db, idmap_mapping *request, 3867cd37da74Snw141292 idmap_mapping *mapping, int is_user) 3868cd37da74Snw141292 { 3869c5c4113dSnw141292 idmap_id_res idres; 3870c5c4113dSnw141292 lookup_state_t state; 3871c5c4113dSnw141292 idmap_retcode retcode; 3872e8c27ec8Sbaban char *canonname; 3873e8c27ec8Sbaban int is_wuser; 3874c5c4113dSnw141292 3875c5c4113dSnw141292 /* 3876c5c4113dSnw141292 * In order to re-use the pid2sid code, we convert 3877c5c4113dSnw141292 * our input data into structs that are expected by 3878c5c4113dSnw141292 * pid2sid_first_pass. 3879c5c4113dSnw141292 */ 3880c5c4113dSnw141292 3881c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 3882c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 3883c5c4113dSnw141292 3884e8c27ec8Sbaban /* Get directory-based name mapping info */ 3885e8c27ec8Sbaban retcode = get_ds_namemap_type(&state); 3886e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 3887e8c27ec8Sbaban goto out; 3888e8c27ec8Sbaban 3889e8c27ec8Sbaban /* 3890e8c27ec8Sbaban * Copy data from "request" to "mapping". Note that 3891e8c27ec8Sbaban * empty strings are not copied from "request" to 3892e8c27ec8Sbaban * "mapping" and therefore the coresponding strings in 3893e8c27ec8Sbaban * "mapping" will be NULL. This eliminates having to 3894e8c27ec8Sbaban * check for empty strings henceforth. 3895e8c27ec8Sbaban */ 3896651c0131Sbaban if (copy_mapping_request(mapping, request) < 0) { 3897651c0131Sbaban retcode = IDMAP_ERR_MEMORY; 3898651c0131Sbaban goto out; 3899651c0131Sbaban } 3900c5c4113dSnw141292 3901e8c27ec8Sbaban /* 3902e8c27ec8Sbaban * For unix to windows mapping request, we need atleast a 3903e8c27ec8Sbaban * unixname or uid/gid to proceed 3904e8c27ec8Sbaban */ 3905e8c27ec8Sbaban if (mapping->id1name == NULL && 3906cf5b5989Sdm199847 mapping->id1.idmap_id_u.uid == SENTINEL_PID) { 3907c5c4113dSnw141292 retcode = IDMAP_ERR_ARG; 3908c5c4113dSnw141292 goto out; 3909c5c4113dSnw141292 } 3910c5c4113dSnw141292 3911e8c27ec8Sbaban /* First pass looks up cache and well-known SIDs */ 3912e8c27ec8Sbaban state.pid2sid_done = TRUE; 3913e8c27ec8Sbaban retcode = pid2sid_first_pass(&state, cache, mapping, &idres, 3914e8c27ec8Sbaban is_user, 1); 3915e8c27ec8Sbaban if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 3916c5c4113dSnw141292 goto out; 3917e8c27ec8Sbaban 3918e8c27ec8Sbaban if (state.nldap_nqueries > 0) { 3919e8c27ec8Sbaban /* 3920e8c27ec8Sbaban * This means directory-based name mapping (nldap or mixed 3921e8c27ec8Sbaban * mode) is enabled. Lookup native LDAP directory service 3922e8c27ec8Sbaban * by unixname or pid to get the winname. Ignore non-fatal 3923e8c27ec8Sbaban * errors in which case we simply fallback to local name 3924e8c27ec8Sbaban * based mapping rules. 3925e8c27ec8Sbaban */ 3926e8c27ec8Sbaban retcode = nldap_lookup(mapping, &idres, 0, 0); 3927e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 3928c5c4113dSnw141292 goto out; 3929e8c27ec8Sbaban idres.retcode = IDMAP_SUCCESS; 3930e8c27ec8Sbaban 3931e8c27ec8Sbaban /* 3932e8c27ec8Sbaban * If we've winname then get SID. We use lookup_name2sid() 3933e8c27ec8Sbaban * instead of ad_lookup() in order to first resolve name2sid 3934e8c27ec8Sbaban * using well-known SIDs table and name_cache before trying 3935e8c27ec8Sbaban * AD. 3936e8c27ec8Sbaban */ 3937e8c27ec8Sbaban if (mapping->id2name != NULL) { 3938e8c27ec8Sbaban canonname = NULL; 3939e8c27ec8Sbaban is_wuser = -1; 3940e8c27ec8Sbaban retcode = lookup_name2sid(cache, mapping->id2name, 3941e8c27ec8Sbaban mapping->id2domain, &is_wuser, &canonname, 3942e8c27ec8Sbaban &idres.id.idmap_id_u.sid.prefix, 3943e8c27ec8Sbaban &idres.id.idmap_id_u.sid.rid, mapping); 3944e8c27ec8Sbaban if (canonname != NULL) { 3945e8c27ec8Sbaban free(mapping->id2name); 3946e8c27ec8Sbaban mapping->id2name = canonname; 3947c5c4113dSnw141292 } 3948e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) 3949e8c27ec8Sbaban idres.id.idtype = is_wuser ? IDMAP_USID : 3950e8c27ec8Sbaban IDMAP_GSID; 3951e8c27ec8Sbaban idres.retcode = retcode; 3952c5c4113dSnw141292 } 3953e8c27ec8Sbaban } else if (state.ad_nqueries > 0) { 3954e8c27ec8Sbaban /* 3955e8c27ec8Sbaban * This means AD-based name mapping is enabled. 3956e8c27ec8Sbaban * Lookup AD by unixname to get winname and sid. 3957e8c27ec8Sbaban * Ignore non-fatal errors in which case we simply fallback 3958e8c27ec8Sbaban * to local name based mapping rules. 3959e8c27ec8Sbaban */ 3960e8c27ec8Sbaban retcode = ad_lookup(&state, mapping, &idres, 0, 0, 1); 3961e8c27ec8Sbaban if (IDMAP_FATAL_ERROR(retcode)) 3962e8c27ec8Sbaban goto out; 3963e8c27ec8Sbaban idres.retcode = IDMAP_SUCCESS; 3964c5c4113dSnw141292 } 3965c5c4113dSnw141292 3966e8c27ec8Sbaban /* 3967e8c27ec8Sbaban * Next pass processes the result of the preceding passes/lookups. 3968e8c27ec8Sbaban * It returns if there's nothing more to be done otherwise it 3969e8c27ec8Sbaban * evaluates local name-based mapping rules 3970e8c27ec8Sbaban */ 3971c5c4113dSnw141292 state.pid2sid_done = TRUE; 3972e8c27ec8Sbaban retcode = pid2sid_second_pass(&state, cache, db, mapping, &idres, 3973e8c27ec8Sbaban is_user); 3974c5c4113dSnw141292 if (IDMAP_ERROR(retcode) || state.pid2sid_done == TRUE) 3975c5c4113dSnw141292 goto out; 3976c5c4113dSnw141292 3977c5c4113dSnw141292 /* Update cache */ 3978c5c4113dSnw141292 (void) update_cache_pid2sid(&state, cache, mapping, &idres); 3979c5c4113dSnw141292 3980c5c4113dSnw141292 out: 3981e8c27ec8Sbaban /* 3982e8c27ec8Sbaban * Note that "mapping" is returned to the client. Therefore 3983e8c27ec8Sbaban * copy whatever we have in "idres" to mapping->id2 and 3984e8c27ec8Sbaban * free idres. 3985e8c27ec8Sbaban */ 3986c5c4113dSnw141292 mapping->direction = idres.direction; 3987c5c4113dSnw141292 mapping->id2 = idres.id; 3988c5c4113dSnw141292 (void) memset(&idres, 0, sizeof (idres)); 3989c5c4113dSnw141292 xdr_free(xdr_idmap_id_res, (caddr_t)&idres); 3990e8c27ec8Sbaban cleanup_lookup_state(&state); 3991e8c27ec8Sbaban return (retcode); 3992e8c27ec8Sbaban } 3993e8c27ec8Sbaban 3994e8c27ec8Sbaban static 3995e8c27ec8Sbaban idmap_retcode 3996e8c27ec8Sbaban ad_lookup_by_unixname(lookup_state_t *state, 3997e8c27ec8Sbaban const char *unixname, int is_user, int is_wuser, 3998e8c27ec8Sbaban char **sidprefix, idmap_rid_t *rid, char **winname, 3999e8c27ec8Sbaban char **domain, int *type) 4000e8c27ec8Sbaban { 4001e8c27ec8Sbaban /* Lookup AD by unixname */ 4002e8c27ec8Sbaban int retries = 0; 4003e8c27ec8Sbaban idmap_query_state_t *qs = NULL; 4004e8c27ec8Sbaban idmap_retcode rc, retcode; 4005e8c27ec8Sbaban 4006e8c27ec8Sbaban retry: 4007e8c27ec8Sbaban retcode = idmap_lookup_batch_start(_idmapdstate.ad, 1, &qs); 4008e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4009e8c27ec8Sbaban degrade_svc(); 4010e8c27ec8Sbaban idmapdlog(LOG_ERR, 4011e8c27ec8Sbaban "Failed to create request for AD lookup by unixname"); 4012e8c27ec8Sbaban return (IDMAP_ERR_INTERNAL); 4013e8c27ec8Sbaban } 4014e8c27ec8Sbaban 4015e8c27ec8Sbaban restore_svc(); 4016e8c27ec8Sbaban 4017e8c27ec8Sbaban if (state != NULL) 4018e8c27ec8Sbaban idmap_lookup_batch_set_unixattr(qs, state->ad_unixuser_attr, 4019e8c27ec8Sbaban state->ad_unixgroup_attr); 4020e8c27ec8Sbaban 4021e8c27ec8Sbaban retcode = idmap_unixname2sid_batch_add1(qs, unixname, is_user, 4022e8c27ec8Sbaban is_wuser, sidprefix, rid, winname, domain, type, &rc); 4023e8c27ec8Sbaban 4024e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) 4025e8c27ec8Sbaban idmap_lookup_release_batch(&qs); 4026e8c27ec8Sbaban else 4027e8c27ec8Sbaban retcode = idmap_lookup_batch_end(&qs, NULL); 4028e8c27ec8Sbaban 4029e8c27ec8Sbaban if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR && retries++ < 2) 4030e8c27ec8Sbaban goto retry; 4031e8c27ec8Sbaban else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR) 4032e8c27ec8Sbaban degrade_svc(); 4033e8c27ec8Sbaban 4034e8c27ec8Sbaban if (retcode != IDMAP_SUCCESS) { 4035e8c27ec8Sbaban idmapdlog(LOG_NOTICE, "AD lookup by unixname failed"); 4036e8c27ec8Sbaban return (retcode); 4037e8c27ec8Sbaban } 4038e8c27ec8Sbaban return (rc); 4039e8c27ec8Sbaban } 4040e8c27ec8Sbaban 4041e8c27ec8Sbaban /* 4042e8c27ec8Sbaban * This function is called whenever a non-batched AD lookup needs to be 4043e8c27ec8Sbaban * done (e.g. get_w2u_mapping() and get_u2w_mapping()). It's already 4044e8c27ec8Sbaban * determined by the caller before calling this function that AD lookup is 4045e8c27ec8Sbaban * needed and has already searched the well-known SIDs table and name_cache. 4046e8c27ec8Sbaban */ 4047e8c27ec8Sbaban static 4048e8c27ec8Sbaban idmap_retcode 4049e8c27ec8Sbaban ad_lookup(lookup_state_t *state, idmap_mapping *req, idmap_id_res *res, 4050e8c27ec8Sbaban int w2u, int getunixattr, int byunixattr) 4051e8c27ec8Sbaban { 4052e8c27ec8Sbaban idmap_retcode retcode; 4053e8c27ec8Sbaban int type, eunixtype, is_user, is_wuser; 4054e8c27ec8Sbaban char *canonname = NULL; 4055e8c27ec8Sbaban 4056e8c27ec8Sbaban if (w2u) { 4057e8c27ec8Sbaban /* 4058e8c27ec8Sbaban * win2unix lookup requests (get_w2u_mapping()) calls this 4059e8c27ec8Sbaban * function to lookup by sid OR winname and to retrieve 4060e8c27ec8Sbaban * SID, winname, sidtype (user or group) and unixnames 4061e8c27ec8Sbaban * from AD. 4062e8c27ec8Sbaban */ 4063e8c27ec8Sbaban 4064e8c27ec8Sbaban /* Set the expected unixtype */ 4065e8c27ec8Sbaban if (res->id.idtype == IDMAP_UID) 4066e8c27ec8Sbaban eunixtype = _IDMAP_T_USER; 4067e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GID) 4068e8c27ec8Sbaban eunixtype = _IDMAP_T_GROUP; 4069e8c27ec8Sbaban else 4070e8c27ec8Sbaban eunixtype = _IDMAP_T_UNDEF; 4071e8c27ec8Sbaban 4072e8c27ec8Sbaban if (req->id1.idmap_id_u.sid.prefix != NULL) { 4073e8c27ec8Sbaban /* AD lookup by sid */ 4074e8c27ec8Sbaban retcode = ad_lookup_by_sid( 4075e8c27ec8Sbaban state, req->id1.idmap_id_u.sid.prefix, 4076e8c27ec8Sbaban req->id1.idmap_id_u.sid.rid, eunixtype, 4077e8c27ec8Sbaban (req->id1name == NULL) ? &req->id1name : NULL, 4078e8c27ec8Sbaban (req->id1domain == NULL) ? &req->id1domain : NULL, 4079e8c27ec8Sbaban &type, (getunixattr && req->id2name == NULL) 4080e8c27ec8Sbaban ? &req->id2name : NULL); 4081e8c27ec8Sbaban } else { 4082e8c27ec8Sbaban assert(req->id1name != NULL); 4083e8c27ec8Sbaban /* AD lookup by winname */ 4084e8c27ec8Sbaban retcode = ad_lookup_by_winname( 4085e8c27ec8Sbaban state, req->id1name, req->id1domain, eunixtype, 4086e8c27ec8Sbaban &canonname, &req->id1.idmap_id_u.sid.prefix, 4087e8c27ec8Sbaban &req->id1.idmap_id_u.sid.rid, &type, 4088e8c27ec8Sbaban (getunixattr && req->id2name == NULL) 4089e8c27ec8Sbaban ? &req->id2name : NULL); 4090e8c27ec8Sbaban 4091e8c27ec8Sbaban if (canonname != NULL) { 4092e8c27ec8Sbaban free(req->id1name); 4093e8c27ec8Sbaban req->id1name = canonname; 4094e8c27ec8Sbaban } 4095e8c27ec8Sbaban } 4096e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 4097e8c27ec8Sbaban switch (type) { 4098e8c27ec8Sbaban case _IDMAP_T_USER: 4099e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 4100e8c27ec8Sbaban res->id.idtype = IDMAP_UID; 4101e8c27ec8Sbaban req->id1.idtype = IDMAP_USID; 4102e8c27ec8Sbaban break; 4103e8c27ec8Sbaban case _IDMAP_T_GROUP: 4104e8c27ec8Sbaban if (res->id.idtype == IDMAP_POSIXID) 4105e8c27ec8Sbaban res->id.idtype = IDMAP_GID; 4106e8c27ec8Sbaban req->id1.idtype = IDMAP_GSID; 4107e8c27ec8Sbaban break; 4108e8c27ec8Sbaban default: 4109e8c27ec8Sbaban return (IDMAP_ERR_SID); 4110e8c27ec8Sbaban } 4111e8c27ec8Sbaban } 4112e8c27ec8Sbaban return (retcode); 4113e8c27ec8Sbaban } 4114e8c27ec8Sbaban 4115e8c27ec8Sbaban /* 4116e8c27ec8Sbaban * unix2win lookup requests (get_u2w_mapping()) calls this 4117e8c27ec8Sbaban * function to lookup AD by unixname and to retrieve 4118e8c27ec8Sbaban * SID, winname, and sidtype (user or group) from AD. 4119e8c27ec8Sbaban */ 4120e8c27ec8Sbaban 4121e8c27ec8Sbaban /* Set the expected unixtype */ 4122e8c27ec8Sbaban eunixtype = (req->id1.idtype == IDMAP_UID) ? _IDMAP_T_USER : 4123e8c27ec8Sbaban _IDMAP_T_GROUP; 4124e8c27ec8Sbaban 4125e8c27ec8Sbaban if (byunixattr) { 4126e8c27ec8Sbaban /* AD lookup by unixname */ 4127e8c27ec8Sbaban is_user = (IS_REQUEST_UID(*req)) ? 1 : 0; 4128e8c27ec8Sbaban if (res->id.idtype == IDMAP_USID) 4129e8c27ec8Sbaban is_wuser = 1; 4130e8c27ec8Sbaban else if (res->id.idtype == IDMAP_GSID) 4131e8c27ec8Sbaban is_wuser = 0; 4132e8c27ec8Sbaban else 4133e8c27ec8Sbaban is_wuser = is_user; 4134e8c27ec8Sbaban retcode = ad_lookup_by_unixname( 4135e8c27ec8Sbaban state, req->id1name, is_user, is_wuser, 4136e8c27ec8Sbaban (res->id.idmap_id_u.sid.prefix == NULL) ? 4137e8c27ec8Sbaban &res->id.idmap_id_u.sid.prefix : NULL, 4138e8c27ec8Sbaban (res->id.idmap_id_u.sid.prefix == NULL) ? 4139e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid : NULL, 4140e8c27ec8Sbaban (req->id2name == NULL) ? &req->id2name : NULL, 4141e8c27ec8Sbaban (req->id2domain == NULL) ? &req->id2domain : NULL, NULL); 4142e8c27ec8Sbaban } else if (res->id.idmap_id_u.sid.prefix != NULL) { 4143e8c27ec8Sbaban /* AD lookup by sid */ 4144e8c27ec8Sbaban retcode = ad_lookup_by_sid( 4145e8c27ec8Sbaban state, res->id.idmap_id_u.sid.prefix, 4146e8c27ec8Sbaban res->id.idmap_id_u.sid.rid, eunixtype, 4147e8c27ec8Sbaban (req->id2name == NULL) ? &req->id2name : NULL, 4148e8c27ec8Sbaban (req->id2domain == NULL) ? &req->id2domain : NULL, 4149e8c27ec8Sbaban NULL, (getunixattr && req->id1name == NULL) 4150e8c27ec8Sbaban ? &req->id1name : NULL); 4151e8c27ec8Sbaban } else { 4152e8c27ec8Sbaban /* AD lookup by winname */ 4153e8c27ec8Sbaban assert(req->id2name != NULL); 4154e8c27ec8Sbaban retcode = ad_lookup_by_winname( 4155e8c27ec8Sbaban state, req->id2name, req->id2domain, eunixtype, 4156e8c27ec8Sbaban &canonname, &res->id.idmap_id_u.sid.prefix, 4157e8c27ec8Sbaban &res->id.idmap_id_u.sid.rid, NULL, 4158e8c27ec8Sbaban (getunixattr && req->id1name == NULL) 4159e8c27ec8Sbaban ? &req->id1name : NULL); 4160e8c27ec8Sbaban 4161e8c27ec8Sbaban if (canonname != NULL) { 4162e8c27ec8Sbaban free(req->id2name); 4163e8c27ec8Sbaban req->id2name = canonname; 4164e8c27ec8Sbaban } 4165e8c27ec8Sbaban } 4166e8c27ec8Sbaban 4167e8c27ec8Sbaban if (retcode == IDMAP_SUCCESS) { 4168e8c27ec8Sbaban switch (type) { 4169e8c27ec8Sbaban case _IDMAP_T_USER: 4170e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 4171e8c27ec8Sbaban res->id.idtype = IDMAP_USID; 4172e8c27ec8Sbaban break; 4173e8c27ec8Sbaban case _IDMAP_T_GROUP: 4174e8c27ec8Sbaban if (res->id.idtype == IDMAP_SID) 4175e8c27ec8Sbaban res->id.idtype = IDMAP_GSID; 4176e8c27ec8Sbaban break; 4177e8c27ec8Sbaban default: 4178e8c27ec8Sbaban return (IDMAP_ERR_SID); 4179e8c27ec8Sbaban } 4180e8c27ec8Sbaban } 4181c5c4113dSnw141292 return (retcode); 4182c5c4113dSnw141292 } 4183