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 * Service routines 30c5c4113dSnw141292 */ 31c5c4113dSnw141292 32c5c4113dSnw141292 #include "idmapd.h" 33c5c4113dSnw141292 #include "idmap_priv.h" 34c5c4113dSnw141292 #include <signal.h> 35c5c4113dSnw141292 #include <thread.h> 36c5c4113dSnw141292 #include <string.h> 37c5c4113dSnw141292 #include <strings.h> 38c5c4113dSnw141292 #include <errno.h> 39c5c4113dSnw141292 #include <assert.h> 40c5c4113dSnw141292 #include <sys/types.h> 41c5c4113dSnw141292 #include <sys/stat.h> 42c5c4113dSnw141292 #include <ucred.h> 43c5c4113dSnw141292 #include <pwd.h> 44c5c4113dSnw141292 #include <auth_attr.h> 45c5c4113dSnw141292 #include <secdb.h> 46c5c4113dSnw141292 47c5c4113dSnw141292 #define _VALIDATE_LIST_CB_DATA(col, val, siz)\ 48c5c4113dSnw141292 retcode = validate_list_cb_data(cb_data, argc, argv, col,\ 49c5c4113dSnw141292 (uchar_t **)val, siz);\ 50c5c4113dSnw141292 if (retcode == IDMAP_NEXT) {\ 51c5c4113dSnw141292 result->retcode = IDMAP_NEXT;\ 52c5c4113dSnw141292 return (0);\ 53c5c4113dSnw141292 } else if (retcode < 0) {\ 54c5c4113dSnw141292 result->retcode = retcode;\ 55c5c4113dSnw141292 return (1);\ 56c5c4113dSnw141292 } 57c5c4113dSnw141292 58c5c4113dSnw141292 #define PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\ 59c5c4113dSnw141292 rcode = process_list_svc_sql(db, sql, limit, cb, res);\ 60c5c4113dSnw141292 if (rcode == IDMAP_ERR_BUSY)\ 61c5c4113dSnw141292 res->retcode = IDMAP_ERR_BUSY;\ 62c5c4113dSnw141292 else if (rcode == IDMAP_SUCCESS && len == 0)\ 63c5c4113dSnw141292 res->retcode = IDMAP_ERR_NOTFOUND; 64c5c4113dSnw141292 65c5c4113dSnw141292 66*8e228215Sdm199847 #define STRDUP_OR_FAIL(to, from) \ 67*8e228215Sdm199847 if ((from) == NULL) \ 68*8e228215Sdm199847 to = NULL; \ 69*8e228215Sdm199847 else { \ 70*8e228215Sdm199847 if ((to = strdup(from)) == NULL) \ 71*8e228215Sdm199847 return (1); \ 72*8e228215Sdm199847 } 73*8e228215Sdm199847 74c5c4113dSnw141292 /* ARGSUSED */ 75c5c4113dSnw141292 bool_t 76c5c4113dSnw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) { 77c5c4113dSnw141292 return (TRUE); 78c5c4113dSnw141292 } 79c5c4113dSnw141292 80c5c4113dSnw141292 #define IS_BATCH_SID(batch, i)\ 81c5c4113dSnw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID 82c5c4113dSnw141292 83c5c4113dSnw141292 #define IS_BATCH_UID(batch, i)\ 84c5c4113dSnw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID 85c5c4113dSnw141292 86c5c4113dSnw141292 #define IS_BATCH_GID(batch, i)\ 87c5c4113dSnw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID 88c5c4113dSnw141292 89c5c4113dSnw141292 #define IS_REQUEST_SID(request)\ 90c5c4113dSnw141292 request.id1.idtype == IDMAP_SID 91c5c4113dSnw141292 92c5c4113dSnw141292 #define IS_REQUEST_UID(request)\ 93c5c4113dSnw141292 request.id1.idtype == IDMAP_UID 94c5c4113dSnw141292 95c5c4113dSnw141292 #define IS_REQUEST_GID(request)\ 96c5c4113dSnw141292 request.id1.idtype == IDMAP_GID 97c5c4113dSnw141292 98c5c4113dSnw141292 /* ARGSUSED */ 99c5c4113dSnw141292 bool_t 100c5c4113dSnw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch, 101c5c4113dSnw141292 idmap_ids_res *result, struct svc_req *rqstp) { 102c5c4113dSnw141292 sqlite *cache = NULL, *db = NULL; 103c5c4113dSnw141292 lookup_state_t state; 104c5c4113dSnw141292 idmap_retcode retcode, winrc; 10562c60062Sbaban uint_t i; 106c5c4113dSnw141292 107c5c4113dSnw141292 /* Init */ 108c5c4113dSnw141292 (void) memset(result, 0, sizeof (*result)); 109c5c4113dSnw141292 (void) memset(&state, 0, sizeof (state)); 110c5c4113dSnw141292 111c5c4113dSnw141292 /* Return success if nothing was requested */ 112c5c4113dSnw141292 if (batch.idmap_mapping_batch_len < 1) 113c5c4113dSnw141292 goto out; 114c5c4113dSnw141292 115c5c4113dSnw141292 /* Get cache handle */ 116c5c4113dSnw141292 result->retcode = get_cache_handle(&cache); 117c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 118c5c4113dSnw141292 goto out; 119c5c4113dSnw141292 120c5c4113dSnw141292 /* Get db handle */ 121c5c4113dSnw141292 result->retcode = get_db_handle(&db); 122c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 123c5c4113dSnw141292 goto out; 124c5c4113dSnw141292 125c5c4113dSnw141292 /* Allocate result array */ 126c5c4113dSnw141292 result->ids.ids_val = calloc(batch.idmap_mapping_batch_len, 127c5c4113dSnw141292 sizeof (idmap_id_res)); 128c5c4113dSnw141292 if (result->ids.ids_val == NULL) { 129c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 130c5c4113dSnw141292 result->retcode = IDMAP_ERR_MEMORY; 131c5c4113dSnw141292 goto out; 132c5c4113dSnw141292 } 133c5c4113dSnw141292 result->ids.ids_len = batch.idmap_mapping_batch_len; 134c5c4113dSnw141292 13562c60062Sbaban /* Allocate hash table to check for duplicate sids */ 13662c60062Sbaban state.sid_history = calloc(batch.idmap_mapping_batch_len, 13762c60062Sbaban sizeof (*state.sid_history)); 13862c60062Sbaban if (state.sid_history == NULL) { 13962c60062Sbaban idmapdlog(LOG_ERR, "Out of memory"); 14062c60062Sbaban result->retcode = IDMAP_ERR_MEMORY; 14162c60062Sbaban goto out; 14262c60062Sbaban } 14362c60062Sbaban state.sid_history_size = batch.idmap_mapping_batch_len; 14462c60062Sbaban for (i = 0; i < state.sid_history_size; i++) { 14562c60062Sbaban state.sid_history[i].key = state.sid_history_size; 14662c60062Sbaban state.sid_history[i].next = state.sid_history_size; 14762c60062Sbaban } 14862c60062Sbaban state.batch = &batch; 14962c60062Sbaban state.result = result; 15062c60062Sbaban 151c5c4113dSnw141292 /* Init our 'done' flags */ 152c5c4113dSnw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 153c5c4113dSnw141292 154c5c4113dSnw141292 /* First stage */ 155c5c4113dSnw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 15662c60062Sbaban state.curpos = i; 157c5c4113dSnw141292 if (IS_BATCH_SID(batch, i)) { 158c5c4113dSnw141292 retcode = sid2pid_first_pass( 159c5c4113dSnw141292 &state, 160c5c4113dSnw141292 cache, 161c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 162c5c4113dSnw141292 &result->ids.ids_val[i]); 163c5c4113dSnw141292 } else if (IS_BATCH_UID(batch, i)) { 164c5c4113dSnw141292 retcode = pid2sid_first_pass( 165c5c4113dSnw141292 &state, 166c5c4113dSnw141292 cache, 167c5c4113dSnw141292 db, 168c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 169c5c4113dSnw141292 &result->ids.ids_val[i], 1, 0); 170c5c4113dSnw141292 } else if (IS_BATCH_GID(batch, i)) { 171c5c4113dSnw141292 retcode = pid2sid_first_pass( 172c5c4113dSnw141292 &state, 173c5c4113dSnw141292 cache, 174c5c4113dSnw141292 db, 175c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 176c5c4113dSnw141292 &result->ids.ids_val[i], 0, 0); 177c5c4113dSnw141292 } else { 178c5c4113dSnw141292 result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE; 179c5c4113dSnw141292 continue; 180c5c4113dSnw141292 } 181c5c4113dSnw141292 if (IDMAP_FATAL_ERROR(retcode)) { 182c5c4113dSnw141292 result->retcode = retcode; 183c5c4113dSnw141292 goto out; 184c5c4113dSnw141292 } 185c5c4113dSnw141292 } 186c5c4113dSnw141292 187c5c4113dSnw141292 /* Check if we are done */ 188c5c4113dSnw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 189c5c4113dSnw141292 goto out; 190c5c4113dSnw141292 191c5c4113dSnw141292 /* Process Windows server lookups for sid2name */ 192c5c4113dSnw141292 if (state.ad_nqueries) { 193c5c4113dSnw141292 winrc = lookup_win_batch_sid2name(&state, &batch, 194c5c4113dSnw141292 result); 195c5c4113dSnw141292 if (IDMAP_FATAL_ERROR(winrc)) { 196c5c4113dSnw141292 result->retcode = winrc; 197c5c4113dSnw141292 goto out; 198c5c4113dSnw141292 } 199c5c4113dSnw141292 } else 200c5c4113dSnw141292 winrc = IDMAP_SUCCESS; 201c5c4113dSnw141292 202c5c4113dSnw141292 /* Reset sid2pid 'done' flag */ 203c5c4113dSnw141292 state.sid2pid_done = TRUE; 204c5c4113dSnw141292 205c5c4113dSnw141292 /* Second stage */ 206c5c4113dSnw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 20762c60062Sbaban state.curpos = i; 208c5c4113dSnw141292 /* Process sid to pid ONLY */ 209c5c4113dSnw141292 if (IS_BATCH_SID(batch, i)) { 210c5c4113dSnw141292 if (IDMAP_ERROR(winrc)) 211c5c4113dSnw141292 result->ids.ids_val[i].retcode = winrc; 212c5c4113dSnw141292 retcode = sid2pid_second_pass( 213c5c4113dSnw141292 &state, 214c5c4113dSnw141292 cache, 215c5c4113dSnw141292 db, 216c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 217c5c4113dSnw141292 &result->ids.ids_val[i]); 218c5c4113dSnw141292 if (IDMAP_FATAL_ERROR(retcode)) { 219c5c4113dSnw141292 result->retcode = retcode; 220c5c4113dSnw141292 goto out; 221c5c4113dSnw141292 } 222c5c4113dSnw141292 } 223c5c4113dSnw141292 } 224c5c4113dSnw141292 225c5c4113dSnw141292 /* Check if we are done */ 226c5c4113dSnw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 227c5c4113dSnw141292 goto out; 228c5c4113dSnw141292 229c5c4113dSnw141292 /* Reset our 'done' flags */ 230c5c4113dSnw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 231c5c4113dSnw141292 232c5c4113dSnw141292 /* Update cache in a single transaction */ 233c5c4113dSnw141292 if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS) 234c5c4113dSnw141292 goto out; 235c5c4113dSnw141292 236c5c4113dSnw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 23762c60062Sbaban state.curpos = i; 238c5c4113dSnw141292 if (IS_BATCH_SID(batch, i)) { 239c5c4113dSnw141292 (void) update_cache_sid2pid( 240c5c4113dSnw141292 &state, 241c5c4113dSnw141292 cache, 242c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 243c5c4113dSnw141292 &result->ids.ids_val[i]); 244c5c4113dSnw141292 } else if ((IS_BATCH_UID(batch, i)) || 245c5c4113dSnw141292 (IS_BATCH_GID(batch, i))) { 246c5c4113dSnw141292 (void) update_cache_pid2sid( 247c5c4113dSnw141292 &state, 248c5c4113dSnw141292 cache, 249c5c4113dSnw141292 &batch.idmap_mapping_batch_val[i], 250c5c4113dSnw141292 &result->ids.ids_val[i]); 251c5c4113dSnw141292 } 252c5c4113dSnw141292 } 253c5c4113dSnw141292 254c5c4113dSnw141292 /* Commit if we have atleast one successful update */ 255c5c4113dSnw141292 if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE) 256c5c4113dSnw141292 (void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;"); 257c5c4113dSnw141292 else 258c5c4113dSnw141292 (void) sql_exec_no_cb(cache, "END TRANSACTION;"); 259c5c4113dSnw141292 260c5c4113dSnw141292 out: 26162c60062Sbaban if (state.sid_history) 26262c60062Sbaban free(state.sid_history); 263c5c4113dSnw141292 if (IDMAP_ERROR(result->retcode)) { 264c5c4113dSnw141292 xdr_free(xdr_idmap_ids_res, (caddr_t)result); 265c5c4113dSnw141292 result->ids.ids_len = 0; 266c5c4113dSnw141292 result->ids.ids_val = NULL; 267c5c4113dSnw141292 } 268c5c4113dSnw141292 result->retcode = idmap_stat4prot(result->retcode); 269c5c4113dSnw141292 return (TRUE); 270c5c4113dSnw141292 } 271c5c4113dSnw141292 272c5c4113dSnw141292 273c5c4113dSnw141292 /* ARGSUSED */ 274c5c4113dSnw141292 static int 275c5c4113dSnw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) { 276c5c4113dSnw141292 list_cb_data_t *cb_data; 277c5c4113dSnw141292 char *str; 278c5c4113dSnw141292 idmap_mappings_res *result; 279c5c4113dSnw141292 idmap_retcode retcode; 280c5c4113dSnw141292 int w2u, u2w; 281c5c4113dSnw141292 char *end; 282c5c4113dSnw141292 283c5c4113dSnw141292 cb_data = (list_cb_data_t *)parg; 284c5c4113dSnw141292 result = (idmap_mappings_res *)cb_data->result; 285c5c4113dSnw141292 286c5c4113dSnw141292 _VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val, 287c5c4113dSnw141292 sizeof (idmap_mapping)); 288c5c4113dSnw141292 289c5c4113dSnw141292 result->mappings.mappings_len++; 290c5c4113dSnw141292 291c5c4113dSnw141292 if ((str = strdup(argv[1])) == NULL) 292c5c4113dSnw141292 return (1); 293c5c4113dSnw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix = 294c5c4113dSnw141292 str; 295c5c4113dSnw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid = 296c5c4113dSnw141292 strtoul(argv[2], &end, 10); 297c5c4113dSnw141292 result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID; 298c5c4113dSnw141292 299c5c4113dSnw141292 result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid = 300c5c4113dSnw141292 strtoul(argv[3], &end, 10); 301c5c4113dSnw141292 result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID; 302c5c4113dSnw141292 303c5c4113dSnw141292 w2u = argv[4]?strtol(argv[4], &end, 10):0; 304c5c4113dSnw141292 u2w = argv[5]?strtol(argv[5], &end, 10):0; 305c5c4113dSnw141292 306c5c4113dSnw141292 if (w2u > 0 && u2w == 0) 307651c0131Sbaban result->mappings.mappings_val[cb_data->next].direction = 308651c0131Sbaban IDMAP_DIRECTION_W2U; 309c5c4113dSnw141292 else if (w2u == 0 && u2w > 0) 310651c0131Sbaban result->mappings.mappings_val[cb_data->next].direction = 311651c0131Sbaban IDMAP_DIRECTION_U2W; 312c5c4113dSnw141292 else 313651c0131Sbaban result->mappings.mappings_val[cb_data->next].direction = 314651c0131Sbaban IDMAP_DIRECTION_BI; 315c5c4113dSnw141292 316*8e228215Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain, 317*8e228215Sdm199847 argv[6]); 318c5c4113dSnw141292 319*8e228215Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name, 320*8e228215Sdm199847 argv[7]); 321c5c4113dSnw141292 322*8e228215Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name, 323*8e228215Sdm199847 argv[8]); 324*8e228215Sdm199847 325c5c4113dSnw141292 326c5c4113dSnw141292 result->lastrowid = strtoll(argv[0], &end, 10); 327c5c4113dSnw141292 cb_data->next++; 328c5c4113dSnw141292 result->retcode = IDMAP_SUCCESS; 329c5c4113dSnw141292 return (0); 330c5c4113dSnw141292 } 331c5c4113dSnw141292 332c5c4113dSnw141292 333c5c4113dSnw141292 /* ARGSUSED */ 334c5c4113dSnw141292 bool_t 335c5c4113dSnw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid, 336c5c4113dSnw141292 uint64_t limit, idmap_mappings_res *result, 337c5c4113dSnw141292 struct svc_req *rqstp) { 338c5c4113dSnw141292 sqlite *cache = NULL; 339c5c4113dSnw141292 char lbuf[30], rbuf[30]; 340c5c4113dSnw141292 uint64_t maxlimit; 341c5c4113dSnw141292 idmap_retcode retcode; 342c5c4113dSnw141292 char *sql = NULL; 343c5c4113dSnw141292 344c5c4113dSnw141292 (void) memset(result, 0, sizeof (*result)); 345c5c4113dSnw141292 lbuf[0] = rbuf[0] = 0; 346c5c4113dSnw141292 347c5c4113dSnw141292 RDLOCK_CONFIG(); 348c5c4113dSnw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 349c5c4113dSnw141292 UNLOCK_CONFIG(); 350c5c4113dSnw141292 351c5c4113dSnw141292 /* Get cache handle */ 352c5c4113dSnw141292 result->retcode = get_cache_handle(&cache); 353c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 354c5c4113dSnw141292 goto out; 355c5c4113dSnw141292 356c5c4113dSnw141292 result->retcode = IDMAP_ERR_INTERNAL; 357c5c4113dSnw141292 358c5c4113dSnw141292 /* Create LIMIT expression. */ 359c5c4113dSnw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 360c5c4113dSnw141292 limit = maxlimit; 361c5c4113dSnw141292 if (limit > 0) 362c5c4113dSnw141292 (void) snprintf(lbuf, sizeof (lbuf), 363c5c4113dSnw141292 "LIMIT %" PRIu64, limit + 1ULL); 364c5c4113dSnw141292 365c5c4113dSnw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 366c5c4113dSnw141292 367c5c4113dSnw141292 /* 368c5c4113dSnw141292 * Combine all the above into a giant SELECT statement that 369c5c4113dSnw141292 * will return the requested mappings 370c5c4113dSnw141292 */ 371c5c4113dSnw141292 sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w," 372c5c4113dSnw141292 " windomain, winname, unixname" 373c5c4113dSnw141292 " FROM idmap_cache WHERE " 374c5c4113dSnw141292 " %s AND is_user = %d %s;", 375c5c4113dSnw141292 rbuf, is_user?1:0, lbuf); 376c5c4113dSnw141292 if (sql == NULL) { 377c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 378c5c4113dSnw141292 goto out; 379c5c4113dSnw141292 } 380c5c4113dSnw141292 381c5c4113dSnw141292 /* Execute the SQL statement and update the return buffer */ 382c5c4113dSnw141292 PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb, 383c5c4113dSnw141292 result, result->mappings.mappings_len); 384c5c4113dSnw141292 385c5c4113dSnw141292 out: 386c5c4113dSnw141292 if (sql) 387c5c4113dSnw141292 sqlite_freemem(sql); 388c5c4113dSnw141292 if (IDMAP_ERROR(result->retcode)) 389c5c4113dSnw141292 (void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 390c5c4113dSnw141292 result->retcode = idmap_stat4prot(result->retcode); 391c5c4113dSnw141292 return (TRUE); 392c5c4113dSnw141292 } 393c5c4113dSnw141292 394c5c4113dSnw141292 395c5c4113dSnw141292 /* ARGSUSED */ 396c5c4113dSnw141292 static int 397c5c4113dSnw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) { 398c5c4113dSnw141292 list_cb_data_t *cb_data; 399c5c4113dSnw141292 idmap_namerules_res *result; 400c5c4113dSnw141292 idmap_retcode retcode; 401c5c4113dSnw141292 int w2u_order, u2w_order; 402c5c4113dSnw141292 char *end; 403c5c4113dSnw141292 404c5c4113dSnw141292 cb_data = (list_cb_data_t *)parg; 405c5c4113dSnw141292 result = (idmap_namerules_res *)cb_data->result; 406c5c4113dSnw141292 407c5c4113dSnw141292 _VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val, 408c5c4113dSnw141292 sizeof (idmap_namerule)); 409c5c4113dSnw141292 410c5c4113dSnw141292 result->rules.rules_len++; 411c5c4113dSnw141292 412c5c4113dSnw141292 result->rules.rules_val[cb_data->next].is_user = 413c5c4113dSnw141292 strtol(argv[1], &end, 10); 414c5c4113dSnw141292 415*8e228215Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain, 416*8e228215Sdm199847 argv[2]); 417c5c4113dSnw141292 418*8e228215Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname, 419*8e228215Sdm199847 argv[3]); 420c5c4113dSnw141292 421c5c4113dSnw141292 result->rules.rules_val[cb_data->next].is_nt4 = 422c5c4113dSnw141292 strtol(argv[4], &end, 10); 423c5c4113dSnw141292 424*8e228215Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname, 425*8e228215Sdm199847 argv[5]); 426c5c4113dSnw141292 427c5c4113dSnw141292 w2u_order = argv[6]?strtol(argv[6], &end, 10):0; 428c5c4113dSnw141292 u2w_order = argv[7]?strtol(argv[7], &end, 10):0; 429c5c4113dSnw141292 430c5c4113dSnw141292 if (w2u_order > 0 && u2w_order == 0) 431651c0131Sbaban result->rules.rules_val[cb_data->next].direction = 432651c0131Sbaban IDMAP_DIRECTION_W2U; 433c5c4113dSnw141292 else if (w2u_order == 0 && u2w_order > 0) 434651c0131Sbaban result->rules.rules_val[cb_data->next].direction = 435651c0131Sbaban IDMAP_DIRECTION_U2W; 436c5c4113dSnw141292 else 437651c0131Sbaban result->rules.rules_val[cb_data->next].direction = 438651c0131Sbaban IDMAP_DIRECTION_BI; 439c5c4113dSnw141292 440c5c4113dSnw141292 result->lastrowid = strtoll(argv[0], &end, 10); 441c5c4113dSnw141292 cb_data->next++; 442c5c4113dSnw141292 result->retcode = IDMAP_SUCCESS; 443c5c4113dSnw141292 return (0); 444c5c4113dSnw141292 } 445c5c4113dSnw141292 446c5c4113dSnw141292 447c5c4113dSnw141292 /* ARGSUSED */ 448c5c4113dSnw141292 bool_t 449c5c4113dSnw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid, 450c5c4113dSnw141292 uint64_t limit, idmap_namerules_res *result, 451c5c4113dSnw141292 struct svc_req *rqstp) { 452c5c4113dSnw141292 453c5c4113dSnw141292 sqlite *db = NULL; 454c5c4113dSnw141292 char w2ubuf[15], u2wbuf[15]; 455c5c4113dSnw141292 char lbuf[30], rbuf[30]; 456c5c4113dSnw141292 char *sql = NULL; 457c5c4113dSnw141292 char *s_windomain = NULL, *s_winname = NULL; 458c5c4113dSnw141292 char *s_unixname = NULL; 459c5c4113dSnw141292 uint64_t maxlimit; 460c5c4113dSnw141292 idmap_retcode retcode; 461c5c4113dSnw141292 462c5c4113dSnw141292 (void) memset(result, 0, sizeof (*result)); 463c5c4113dSnw141292 lbuf[0] = rbuf[0] = 0; 464c5c4113dSnw141292 465c5c4113dSnw141292 RDLOCK_CONFIG(); 466c5c4113dSnw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 467c5c4113dSnw141292 UNLOCK_CONFIG(); 468c5c4113dSnw141292 469c5c4113dSnw141292 /* Get db handle */ 470c5c4113dSnw141292 result->retcode = get_db_handle(&db); 471c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 472c5c4113dSnw141292 goto out; 473c5c4113dSnw141292 474c5c4113dSnw141292 result->retcode = IDMAP_ERR_INTERNAL; 475c5c4113dSnw141292 476c5c4113dSnw141292 if (rule.direction < 0) { 477c5c4113dSnw141292 w2ubuf[0] = u2wbuf[0] = 0; 478651c0131Sbaban } else if (rule.direction == IDMAP_DIRECTION_BI) { 479c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 480c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 481651c0131Sbaban } else if (rule.direction == IDMAP_DIRECTION_W2U) { 482c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 483c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), 484c5c4113dSnw141292 "AND (u2w_order = 0 OR u2w_order ISNULL)"); 485651c0131Sbaban } else if (rule.direction == IDMAP_DIRECTION_U2W) { 486c5c4113dSnw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), 487c5c4113dSnw141292 "AND (w2u_order = 0 OR w2u_order ISNULL)"); 488c5c4113dSnw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 489c5c4113dSnw141292 } 490c5c4113dSnw141292 491c5c4113dSnw141292 /* Create where statement for windomain */ 492*8e228215Sdm199847 if (!EMPTY_STRING(rule.windomain)) { 493c5c4113dSnw141292 if (gen_sql_expr_from_utf8str("AND", "windomain", "=", 494*8e228215Sdm199847 rule.windomain, 495c5c4113dSnw141292 "", &s_windomain) != IDMAP_SUCCESS) 496c5c4113dSnw141292 goto out; 497c5c4113dSnw141292 } 498c5c4113dSnw141292 499c5c4113dSnw141292 /* Create where statement for winname */ 500*8e228215Sdm199847 if (!EMPTY_STRING(rule.winname)) { 501c5c4113dSnw141292 if (gen_sql_expr_from_utf8str("AND", "winname", "=", 502*8e228215Sdm199847 rule.winname, 503c5c4113dSnw141292 "", &s_winname) != IDMAP_SUCCESS) 504c5c4113dSnw141292 goto out; 505c5c4113dSnw141292 } 506c5c4113dSnw141292 507c5c4113dSnw141292 /* Create where statement for unixname */ 508*8e228215Sdm199847 if (!EMPTY_STRING(rule.unixname)) { 509c5c4113dSnw141292 if (gen_sql_expr_from_utf8str("AND", "unixname", "=", 510*8e228215Sdm199847 rule.unixname, 511c5c4113dSnw141292 "", &s_unixname) != IDMAP_SUCCESS) 512c5c4113dSnw141292 goto out; 513c5c4113dSnw141292 } 514c5c4113dSnw141292 515c5c4113dSnw141292 /* Create LIMIT expression. */ 516c5c4113dSnw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 517c5c4113dSnw141292 limit = maxlimit; 518c5c4113dSnw141292 if (limit > 0) 519c5c4113dSnw141292 (void) snprintf(lbuf, sizeof (lbuf), 520c5c4113dSnw141292 "LIMIT %" PRIu64, limit + 1ULL); 521c5c4113dSnw141292 522c5c4113dSnw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 523c5c4113dSnw141292 524c5c4113dSnw141292 /* 525c5c4113dSnw141292 * Combine all the above into a giant SELECT statement that 526c5c4113dSnw141292 * will return the requested rules 527c5c4113dSnw141292 */ 528c5c4113dSnw141292 sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, " 529c5c4113dSnw141292 "is_nt4, unixname, w2u_order, u2w_order " 530c5c4113dSnw141292 "FROM namerules WHERE " 531c5c4113dSnw141292 " %s AND is_user = %d %s %s %s %s %s %s;", 532c5c4113dSnw141292 rbuf, rule.is_user?1:0, 533c5c4113dSnw141292 s_windomain?s_windomain:"", 534c5c4113dSnw141292 s_winname?s_winname:"", 535c5c4113dSnw141292 s_unixname?s_unixname:"", 536c5c4113dSnw141292 w2ubuf, u2wbuf, lbuf); 537c5c4113dSnw141292 if (sql == NULL) { 538c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 539c5c4113dSnw141292 goto out; 540c5c4113dSnw141292 } 541c5c4113dSnw141292 542c5c4113dSnw141292 /* Execute the SQL statement and update the return buffer */ 543c5c4113dSnw141292 PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb, 544c5c4113dSnw141292 result, result->rules.rules_len); 545c5c4113dSnw141292 546c5c4113dSnw141292 out: 547c5c4113dSnw141292 if (s_windomain) 548c5c4113dSnw141292 sqlite_freemem(s_windomain); 549c5c4113dSnw141292 if (s_winname) 550c5c4113dSnw141292 sqlite_freemem(s_winname); 551c5c4113dSnw141292 if (s_unixname) 552c5c4113dSnw141292 sqlite_freemem(s_unixname); 553c5c4113dSnw141292 if (sql) 554c5c4113dSnw141292 sqlite_freemem(sql); 555c5c4113dSnw141292 if (IDMAP_ERROR(result->retcode)) 556c5c4113dSnw141292 (void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result); 557c5c4113dSnw141292 result->retcode = idmap_stat4prot(result->retcode); 558c5c4113dSnw141292 return (TRUE); 559c5c4113dSnw141292 } 560c5c4113dSnw141292 561c5c4113dSnw141292 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules" 562c5c4113dSnw141292 static int 563c5c4113dSnw141292 verify_rules_auth(struct svc_req *rqstp) { 564c5c4113dSnw141292 ucred_t *uc = NULL; 565c5c4113dSnw141292 uid_t uid; 566c5c4113dSnw141292 char buf[1024]; 567c5c4113dSnw141292 struct passwd pwd; 568c5c4113dSnw141292 const char *me = "verify_rules_auth"; 569c5c4113dSnw141292 570c5c4113dSnw141292 if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) { 571c5c4113dSnw141292 idmapdlog(LOG_ERR, 572c5c4113dSnw141292 "%s: svc_getcallerucred failed (errno=%d)", 573c5c4113dSnw141292 me, errno); 574c5c4113dSnw141292 return (-1); 575c5c4113dSnw141292 } 576c5c4113dSnw141292 577c5c4113dSnw141292 uid = ucred_geteuid(uc); 578c5c4113dSnw141292 if (uid == (uid_t)-1) { 579c5c4113dSnw141292 idmapdlog(LOG_ERR, 580c5c4113dSnw141292 "%s: ucred_geteuid failed (errno=%d)", 581c5c4113dSnw141292 me, errno); 582c5c4113dSnw141292 ucred_free(uc); 583c5c4113dSnw141292 return (-1); 584c5c4113dSnw141292 } 585c5c4113dSnw141292 586c5c4113dSnw141292 if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) { 587c5c4113dSnw141292 idmapdlog(LOG_ERR, 588c5c4113dSnw141292 "%s: getpwuid_r(%u) failed (errno=%d)", 589c5c4113dSnw141292 me, uid, errno); 590c5c4113dSnw141292 ucred_free(uc); 591c5c4113dSnw141292 return (-1); 592c5c4113dSnw141292 } 593c5c4113dSnw141292 594c5c4113dSnw141292 if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) { 595c5c4113dSnw141292 idmapdlog(LOG_INFO, 596c5c4113dSnw141292 "%s: %s does not have authorization.", 597c5c4113dSnw141292 me, pwd.pw_name); 598c5c4113dSnw141292 ucred_free(uc); 599c5c4113dSnw141292 return (-1); 600c5c4113dSnw141292 } 601c5c4113dSnw141292 602c5c4113dSnw141292 ucred_free(uc); 603c5c4113dSnw141292 return (1); 604c5c4113dSnw141292 } 605c5c4113dSnw141292 606*8e228215Sdm199847 /* 607*8e228215Sdm199847 * Meaning of the return values is the following: For retcode == 608*8e228215Sdm199847 * IDMAP_SUCCESS, everything went OK and error_index is 609*8e228215Sdm199847 * undefined. Otherwise, error_index >=0 shows the failed batch 610*8e228215Sdm199847 * element. errro_index == -1 indicates failure at the beginning, 611*8e228215Sdm199847 * error_index == -2 at the end. 612*8e228215Sdm199847 */ 613*8e228215Sdm199847 614c5c4113dSnw141292 /* ARGSUSED */ 615c5c4113dSnw141292 bool_t 616*8e228215Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res, 617c5c4113dSnw141292 struct svc_req *rqstp) { 618c5c4113dSnw141292 sqlite *db = NULL; 619c5c4113dSnw141292 idmap_update_op *up; 620c5c4113dSnw141292 int i; 62184decf41Sjp151216 int trans = FALSE; 622c5c4113dSnw141292 623*8e228215Sdm199847 res->error_index = -1; 624*8e228215Sdm199847 (void) memset(&res->error_rule, 0, sizeof (res->error_rule)); 625*8e228215Sdm199847 (void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule)); 626*8e228215Sdm199847 627c5c4113dSnw141292 if (verify_rules_auth(rqstp) < 0) { 628*8e228215Sdm199847 res->retcode = IDMAP_ERR_PERMISSION_DENIED; 629c5c4113dSnw141292 goto out; 630c5c4113dSnw141292 } 631c5c4113dSnw141292 632c5c4113dSnw141292 if (batch.idmap_update_batch_len == 0 || 633c5c4113dSnw141292 batch.idmap_update_batch_val == NULL) { 634*8e228215Sdm199847 res->retcode = IDMAP_SUCCESS; 635c5c4113dSnw141292 goto out; 636c5c4113dSnw141292 } 637c5c4113dSnw141292 638c5c4113dSnw141292 /* Get db handle */ 639*8e228215Sdm199847 res->retcode = get_db_handle(&db); 640*8e228215Sdm199847 if (res->retcode != IDMAP_SUCCESS) 641c5c4113dSnw141292 goto out; 642c5c4113dSnw141292 643*8e228215Sdm199847 res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;"); 644*8e228215Sdm199847 if (res->retcode != IDMAP_SUCCESS) 645c5c4113dSnw141292 goto out; 64684decf41Sjp151216 trans = TRUE; 647c5c4113dSnw141292 648c5c4113dSnw141292 for (i = 0; i < batch.idmap_update_batch_len; i++) { 649c5c4113dSnw141292 up = &batch.idmap_update_batch_val[i]; 650c5c4113dSnw141292 switch (up->opnum) { 651c5c4113dSnw141292 case OP_NONE: 652*8e228215Sdm199847 res->retcode = IDMAP_SUCCESS; 653c5c4113dSnw141292 break; 654c5c4113dSnw141292 case OP_ADD_NAMERULE: 655*8e228215Sdm199847 res->retcode = add_namerule(db, 656c5c4113dSnw141292 &up->idmap_update_op_u.rule); 657c5c4113dSnw141292 break; 658c5c4113dSnw141292 case OP_RM_NAMERULE: 659*8e228215Sdm199847 res->retcode = rm_namerule(db, 660c5c4113dSnw141292 &up->idmap_update_op_u.rule); 661c5c4113dSnw141292 break; 662c5c4113dSnw141292 case OP_FLUSH_NAMERULES: 663*8e228215Sdm199847 res->retcode = flush_namerules(db, 664c5c4113dSnw141292 up->idmap_update_op_u.is_user); 665c5c4113dSnw141292 break; 666c5c4113dSnw141292 default: 667*8e228215Sdm199847 res->retcode = IDMAP_ERR_NOTSUPPORTED; 668*8e228215Sdm199847 break; 669c5c4113dSnw141292 }; 670c5c4113dSnw141292 671*8e228215Sdm199847 if (res->retcode != IDMAP_SUCCESS) { 672*8e228215Sdm199847 res->error_index = i; 673*8e228215Sdm199847 if (up->opnum == OP_ADD_NAMERULE || 674*8e228215Sdm199847 up->opnum == OP_RM_NAMERULE) { 675*8e228215Sdm199847 idmap_stat r2 = 676*8e228215Sdm199847 idmap_namerule_cpy(&res->error_rule, 677*8e228215Sdm199847 &up->idmap_update_op_u.rule); 678*8e228215Sdm199847 if (r2 != IDMAP_SUCCESS) 679*8e228215Sdm199847 res->retcode = r2; 680*8e228215Sdm199847 } 681c5c4113dSnw141292 goto out; 682c5c4113dSnw141292 } 683*8e228215Sdm199847 } 684c5c4113dSnw141292 685c5c4113dSnw141292 out: 68684decf41Sjp151216 if (trans) { 687*8e228215Sdm199847 if (res->retcode == IDMAP_SUCCESS) { 688*8e228215Sdm199847 res->retcode = 689*8e228215Sdm199847 sql_exec_no_cb(db, "COMMIT TRANSACTION;"); 690*8e228215Sdm199847 if (res->retcode != IDMAP_SUCCESS) 691*8e228215Sdm199847 res->error_index = -2; 692*8e228215Sdm199847 } 69384decf41Sjp151216 else 69484decf41Sjp151216 (void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;"); 695c5c4113dSnw141292 } 696*8e228215Sdm199847 697*8e228215Sdm199847 res->retcode = idmap_stat4prot(res->retcode); 698*8e228215Sdm199847 699c5c4113dSnw141292 return (TRUE); 700c5c4113dSnw141292 } 701c5c4113dSnw141292 702c5c4113dSnw141292 703c5c4113dSnw141292 /* ARGSUSED */ 704c5c4113dSnw141292 bool_t 705c5c4113dSnw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request, 706c5c4113dSnw141292 idmap_mappings_res *result, struct svc_req *rqstp) { 707c5c4113dSnw141292 sqlite *cache = NULL, *db = NULL; 708c5c4113dSnw141292 709c5c4113dSnw141292 /* Init */ 710c5c4113dSnw141292 (void) memset(result, 0, sizeof (*result)); 711c5c4113dSnw141292 712c5c4113dSnw141292 /* Get cache handle */ 713c5c4113dSnw141292 result->retcode = get_cache_handle(&cache); 714c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 715c5c4113dSnw141292 goto out; 716c5c4113dSnw141292 717c5c4113dSnw141292 /* Get db handle */ 718c5c4113dSnw141292 result->retcode = get_db_handle(&db); 719c5c4113dSnw141292 if (result->retcode != IDMAP_SUCCESS) 720c5c4113dSnw141292 goto out; 721c5c4113dSnw141292 722c5c4113dSnw141292 /* Allocate result */ 723c5c4113dSnw141292 result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping)); 724c5c4113dSnw141292 if (result->mappings.mappings_val == NULL) { 725c5c4113dSnw141292 idmapdlog(LOG_ERR, "Out of memory"); 726c5c4113dSnw141292 result->retcode = IDMAP_ERR_MEMORY; 727c5c4113dSnw141292 goto out; 728c5c4113dSnw141292 } 729c5c4113dSnw141292 result->mappings.mappings_len = 1; 730c5c4113dSnw141292 731c5c4113dSnw141292 if (IS_REQUEST_SID(request)) { 732c5c4113dSnw141292 result->retcode = get_w2u_mapping( 733c5c4113dSnw141292 cache, 734c5c4113dSnw141292 db, 735c5c4113dSnw141292 &request, 736c5c4113dSnw141292 result->mappings.mappings_val); 737c5c4113dSnw141292 } else if (IS_REQUEST_UID(request)) { 738c5c4113dSnw141292 result->retcode = get_u2w_mapping( 739c5c4113dSnw141292 cache, 740c5c4113dSnw141292 db, 741c5c4113dSnw141292 &request, 742c5c4113dSnw141292 result->mappings.mappings_val, 743c5c4113dSnw141292 1); 744c5c4113dSnw141292 } else if (IS_REQUEST_GID(request)) { 745c5c4113dSnw141292 result->retcode = get_u2w_mapping( 746c5c4113dSnw141292 cache, 747c5c4113dSnw141292 db, 748c5c4113dSnw141292 &request, 749c5c4113dSnw141292 result->mappings.mappings_val, 750c5c4113dSnw141292 0); 751c5c4113dSnw141292 } else { 752c5c4113dSnw141292 result->retcode = IDMAP_ERR_IDTYPE; 753c5c4113dSnw141292 } 754c5c4113dSnw141292 755c5c4113dSnw141292 out: 756c5c4113dSnw141292 if (IDMAP_FATAL_ERROR(result->retcode)) { 757c5c4113dSnw141292 xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 758c5c4113dSnw141292 result->mappings.mappings_len = 0; 759c5c4113dSnw141292 result->mappings.mappings_val = NULL; 760c5c4113dSnw141292 } 761c5c4113dSnw141292 result->retcode = idmap_stat4prot(result->retcode); 762c5c4113dSnw141292 return (TRUE); 763c5c4113dSnw141292 } 764c5c4113dSnw141292 765c5c4113dSnw141292 766c5c4113dSnw141292 /* ARGSUSED */ 767c5c4113dSnw141292 int 768c5c4113dSnw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, 769c5c4113dSnw141292 caddr_t result) { 770c5c4113dSnw141292 (void) xdr_free(xdr_result, result); 771c5c4113dSnw141292 return (TRUE); 772c5c4113dSnw141292 } 773