14520Snw141292 /* 24520Snw141292 * CDDL HEADER START 34520Snw141292 * 44520Snw141292 * The contents of this file are subject to the terms of the 54520Snw141292 * Common Development and Distribution License (the "License"). 64520Snw141292 * You may not use this file except in compliance with the License. 74520Snw141292 * 84520Snw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 94520Snw141292 * or http://www.opensolaris.org/os/licensing. 104520Snw141292 * See the License for the specific language governing permissions 114520Snw141292 * and limitations under the License. 124520Snw141292 * 134520Snw141292 * When distributing Covered Code, include this CDDL HEADER in each 144520Snw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 154520Snw141292 * If applicable, add the following below this CDDL HEADER, with the 164520Snw141292 * fields enclosed by brackets "[]" replaced with your own identifying 174520Snw141292 * information: Portions Copyright [yyyy] [name of copyright owner] 184520Snw141292 * 194520Snw141292 * CDDL HEADER END 204520Snw141292 */ 214520Snw141292 /* 224520Snw141292 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 234520Snw141292 * Use is subject to license terms. 244520Snw141292 */ 254520Snw141292 264520Snw141292 #pragma ident "%Z%%M% %I% %E% SMI" 274520Snw141292 284520Snw141292 /* 294520Snw141292 * Service routines 304520Snw141292 */ 314520Snw141292 324520Snw141292 #include "idmapd.h" 334520Snw141292 #include "idmap_priv.h" 344520Snw141292 #include <signal.h> 354520Snw141292 #include <thread.h> 364520Snw141292 #include <string.h> 374520Snw141292 #include <strings.h> 384520Snw141292 #include <errno.h> 394520Snw141292 #include <assert.h> 404520Snw141292 #include <sys/types.h> 414520Snw141292 #include <sys/stat.h> 424520Snw141292 #include <ucred.h> 434520Snw141292 #include <pwd.h> 444520Snw141292 #include <auth_attr.h> 454520Snw141292 #include <secdb.h> 464520Snw141292 474520Snw141292 #define _VALIDATE_LIST_CB_DATA(col, val, siz)\ 484520Snw141292 retcode = validate_list_cb_data(cb_data, argc, argv, col,\ 494520Snw141292 (uchar_t **)val, siz);\ 504520Snw141292 if (retcode == IDMAP_NEXT) {\ 514520Snw141292 result->retcode = IDMAP_NEXT;\ 524520Snw141292 return (0);\ 534520Snw141292 } else if (retcode < 0) {\ 544520Snw141292 result->retcode = retcode;\ 554520Snw141292 return (1);\ 564520Snw141292 } 574520Snw141292 584520Snw141292 #define PROCESS_LIST_SVC_SQL(rcode, db, sql, limit, cb, res, len)\ 594520Snw141292 rcode = process_list_svc_sql(db, sql, limit, cb, res);\ 604520Snw141292 if (rcode == IDMAP_ERR_BUSY)\ 614520Snw141292 res->retcode = IDMAP_ERR_BUSY;\ 624520Snw141292 else if (rcode == IDMAP_SUCCESS && len == 0)\ 634520Snw141292 res->retcode = IDMAP_ERR_NOTFOUND; 644520Snw141292 654520Snw141292 665064Sdm199847 #define STRDUP_OR_FAIL(to, from) \ 675064Sdm199847 if ((from) == NULL) \ 685064Sdm199847 to = NULL; \ 695064Sdm199847 else { \ 705064Sdm199847 if ((to = strdup(from)) == NULL) \ 715064Sdm199847 return (1); \ 725064Sdm199847 } 735064Sdm199847 744520Snw141292 /* ARGSUSED */ 754520Snw141292 bool_t 764520Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) { 774520Snw141292 return (TRUE); 784520Snw141292 } 794520Snw141292 804520Snw141292 #define IS_BATCH_SID(batch, i)\ 814520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID 824520Snw141292 834520Snw141292 #define IS_BATCH_UID(batch, i)\ 844520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID 854520Snw141292 864520Snw141292 #define IS_BATCH_GID(batch, i)\ 874520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID 884520Snw141292 894520Snw141292 #define IS_REQUEST_SID(request)\ 904520Snw141292 request.id1.idtype == IDMAP_SID 914520Snw141292 924520Snw141292 #define IS_REQUEST_UID(request)\ 934520Snw141292 request.id1.idtype == IDMAP_UID 944520Snw141292 954520Snw141292 #define IS_REQUEST_GID(request)\ 964520Snw141292 request.id1.idtype == IDMAP_GID 974520Snw141292 984520Snw141292 /* ARGSUSED */ 994520Snw141292 bool_t 1004520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch, 1014520Snw141292 idmap_ids_res *result, struct svc_req *rqstp) { 1024520Snw141292 sqlite *cache = NULL, *db = NULL; 1034520Snw141292 lookup_state_t state; 1044520Snw141292 idmap_retcode retcode, winrc; 1054864Sbaban uint_t i; 1064520Snw141292 1074520Snw141292 /* Init */ 1084520Snw141292 (void) memset(result, 0, sizeof (*result)); 1094520Snw141292 (void) memset(&state, 0, sizeof (state)); 1104520Snw141292 1114520Snw141292 /* Return success if nothing was requested */ 1124520Snw141292 if (batch.idmap_mapping_batch_len < 1) 1134520Snw141292 goto out; 1144520Snw141292 1154520Snw141292 /* Get cache handle */ 1164520Snw141292 result->retcode = get_cache_handle(&cache); 1174520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1184520Snw141292 goto out; 1194520Snw141292 1204520Snw141292 /* Get db handle */ 1214520Snw141292 result->retcode = get_db_handle(&db); 1224520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1234520Snw141292 goto out; 1244520Snw141292 1254520Snw141292 /* Allocate result array */ 1264520Snw141292 result->ids.ids_val = calloc(batch.idmap_mapping_batch_len, 1274520Snw141292 sizeof (idmap_id_res)); 1284520Snw141292 if (result->ids.ids_val == NULL) { 1294520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1304520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 1314520Snw141292 goto out; 1324520Snw141292 } 1334520Snw141292 result->ids.ids_len = batch.idmap_mapping_batch_len; 1344520Snw141292 1354864Sbaban /* Allocate hash table to check for duplicate sids */ 1364864Sbaban state.sid_history = calloc(batch.idmap_mapping_batch_len, 1374864Sbaban sizeof (*state.sid_history)); 1384864Sbaban if (state.sid_history == NULL) { 1394864Sbaban idmapdlog(LOG_ERR, "Out of memory"); 1404864Sbaban result->retcode = IDMAP_ERR_MEMORY; 1414864Sbaban goto out; 1424864Sbaban } 1434864Sbaban state.sid_history_size = batch.idmap_mapping_batch_len; 1444864Sbaban for (i = 0; i < state.sid_history_size; i++) { 1454864Sbaban state.sid_history[i].key = state.sid_history_size; 1464864Sbaban state.sid_history[i].next = state.sid_history_size; 1474864Sbaban } 1484864Sbaban state.batch = &batch; 1494864Sbaban state.result = result; 1504864Sbaban 1514520Snw141292 /* Init our 'done' flags */ 1524520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 1534520Snw141292 1544520Snw141292 /* First stage */ 1554520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 1564864Sbaban state.curpos = i; 1574520Snw141292 if (IS_BATCH_SID(batch, i)) { 1584520Snw141292 retcode = sid2pid_first_pass( 1594520Snw141292 &state, 1604520Snw141292 cache, 1614520Snw141292 &batch.idmap_mapping_batch_val[i], 1624520Snw141292 &result->ids.ids_val[i]); 1634520Snw141292 } else if (IS_BATCH_UID(batch, i)) { 1644520Snw141292 retcode = pid2sid_first_pass( 1654520Snw141292 &state, 1664520Snw141292 cache, 1674520Snw141292 db, 1684520Snw141292 &batch.idmap_mapping_batch_val[i], 1694520Snw141292 &result->ids.ids_val[i], 1, 0); 1704520Snw141292 } else if (IS_BATCH_GID(batch, i)) { 1714520Snw141292 retcode = pid2sid_first_pass( 1724520Snw141292 &state, 1734520Snw141292 cache, 1744520Snw141292 db, 1754520Snw141292 &batch.idmap_mapping_batch_val[i], 1764520Snw141292 &result->ids.ids_val[i], 0, 0); 1774520Snw141292 } else { 1784520Snw141292 result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE; 1794520Snw141292 continue; 1804520Snw141292 } 1814520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 1824520Snw141292 result->retcode = retcode; 1834520Snw141292 goto out; 1844520Snw141292 } 1854520Snw141292 } 1864520Snw141292 1874520Snw141292 /* Check if we are done */ 1884520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 1894520Snw141292 goto out; 1904520Snw141292 1914520Snw141292 /* Process Windows server lookups for sid2name */ 1924520Snw141292 if (state.ad_nqueries) { 1934520Snw141292 winrc = lookup_win_batch_sid2name(&state, &batch, 1944520Snw141292 result); 1954520Snw141292 if (IDMAP_FATAL_ERROR(winrc)) { 1964520Snw141292 result->retcode = winrc; 1974520Snw141292 goto out; 1984520Snw141292 } 1994520Snw141292 } else 2004520Snw141292 winrc = IDMAP_SUCCESS; 2014520Snw141292 2024520Snw141292 /* Reset sid2pid 'done' flag */ 2034520Snw141292 state.sid2pid_done = TRUE; 2044520Snw141292 2054520Snw141292 /* Second stage */ 2064520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 2074864Sbaban state.curpos = i; 2084520Snw141292 /* Process sid to pid ONLY */ 2094520Snw141292 if (IS_BATCH_SID(batch, i)) { 2104520Snw141292 if (IDMAP_ERROR(winrc)) 2114520Snw141292 result->ids.ids_val[i].retcode = winrc; 2124520Snw141292 retcode = sid2pid_second_pass( 2134520Snw141292 &state, 2144520Snw141292 cache, 2154520Snw141292 db, 2164520Snw141292 &batch.idmap_mapping_batch_val[i], 2174520Snw141292 &result->ids.ids_val[i]); 2184520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 2194520Snw141292 result->retcode = retcode; 2204520Snw141292 goto out; 2214520Snw141292 } 2224520Snw141292 } 2234520Snw141292 } 2244520Snw141292 2254520Snw141292 /* Check if we are done */ 2264520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 2274520Snw141292 goto out; 2284520Snw141292 2294520Snw141292 /* Reset our 'done' flags */ 2304520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 2314520Snw141292 2324520Snw141292 /* Update cache in a single transaction */ 2334520Snw141292 if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS) 2344520Snw141292 goto out; 2354520Snw141292 2364520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 2374864Sbaban state.curpos = i; 2384520Snw141292 if (IS_BATCH_SID(batch, i)) { 2394520Snw141292 (void) update_cache_sid2pid( 2404520Snw141292 &state, 2414520Snw141292 cache, 2424520Snw141292 &batch.idmap_mapping_batch_val[i], 2434520Snw141292 &result->ids.ids_val[i]); 2444520Snw141292 } else if ((IS_BATCH_UID(batch, i)) || 2454520Snw141292 (IS_BATCH_GID(batch, i))) { 2464520Snw141292 (void) update_cache_pid2sid( 2474520Snw141292 &state, 2484520Snw141292 cache, 2494520Snw141292 &batch.idmap_mapping_batch_val[i], 2504520Snw141292 &result->ids.ids_val[i]); 2514520Snw141292 } 2524520Snw141292 } 2534520Snw141292 254*5331Samw /* Commit if we have at least one successful update */ 2554520Snw141292 if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE) 2564520Snw141292 (void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;"); 2574520Snw141292 else 2584520Snw141292 (void) sql_exec_no_cb(cache, "END TRANSACTION;"); 2594520Snw141292 2604520Snw141292 out: 2614864Sbaban if (state.sid_history) 2624864Sbaban free(state.sid_history); 2634520Snw141292 if (IDMAP_ERROR(result->retcode)) { 2644520Snw141292 xdr_free(xdr_idmap_ids_res, (caddr_t)result); 2654520Snw141292 result->ids.ids_len = 0; 2664520Snw141292 result->ids.ids_val = NULL; 2674520Snw141292 } 2684520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 2694520Snw141292 return (TRUE); 2704520Snw141292 } 2714520Snw141292 2724520Snw141292 2734520Snw141292 /* ARGSUSED */ 2744520Snw141292 static int 2754520Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) { 2764520Snw141292 list_cb_data_t *cb_data; 2774520Snw141292 char *str; 2784520Snw141292 idmap_mappings_res *result; 2794520Snw141292 idmap_retcode retcode; 2804520Snw141292 int w2u, u2w; 2814520Snw141292 char *end; 2824520Snw141292 2834520Snw141292 cb_data = (list_cb_data_t *)parg; 2844520Snw141292 result = (idmap_mappings_res *)cb_data->result; 2854520Snw141292 2864520Snw141292 _VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val, 2874520Snw141292 sizeof (idmap_mapping)); 2884520Snw141292 2894520Snw141292 result->mappings.mappings_len++; 2904520Snw141292 2914520Snw141292 if ((str = strdup(argv[1])) == NULL) 2924520Snw141292 return (1); 2934520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix = 2944520Snw141292 str; 2954520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid = 2964520Snw141292 strtoul(argv[2], &end, 10); 2974520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID; 2984520Snw141292 2994520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid = 3004520Snw141292 strtoul(argv[3], &end, 10); 3014520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID; 3024520Snw141292 3034520Snw141292 w2u = argv[4]?strtol(argv[4], &end, 10):0; 3044520Snw141292 u2w = argv[5]?strtol(argv[5], &end, 10):0; 3054520Snw141292 3064520Snw141292 if (w2u > 0 && u2w == 0) 3074644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3084644Sbaban IDMAP_DIRECTION_W2U; 3094520Snw141292 else if (w2u == 0 && u2w > 0) 3104644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3114644Sbaban IDMAP_DIRECTION_U2W; 3124520Snw141292 else 3134644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3144644Sbaban IDMAP_DIRECTION_BI; 3154520Snw141292 3165064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain, 3175064Sdm199847 argv[6]); 3184520Snw141292 3195064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name, 3205064Sdm199847 argv[7]); 3214520Snw141292 3225064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name, 3235064Sdm199847 argv[8]); 3245064Sdm199847 3254520Snw141292 3264520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 3274520Snw141292 cb_data->next++; 3284520Snw141292 result->retcode = IDMAP_SUCCESS; 3294520Snw141292 return (0); 3304520Snw141292 } 3314520Snw141292 3324520Snw141292 3334520Snw141292 /* ARGSUSED */ 3344520Snw141292 bool_t 3354520Snw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid, 3364520Snw141292 uint64_t limit, idmap_mappings_res *result, 3374520Snw141292 struct svc_req *rqstp) { 3384520Snw141292 sqlite *cache = NULL; 3394520Snw141292 char lbuf[30], rbuf[30]; 3404520Snw141292 uint64_t maxlimit; 3414520Snw141292 idmap_retcode retcode; 3424520Snw141292 char *sql = NULL; 3434520Snw141292 3444520Snw141292 (void) memset(result, 0, sizeof (*result)); 3454520Snw141292 lbuf[0] = rbuf[0] = 0; 3464520Snw141292 3474520Snw141292 RDLOCK_CONFIG(); 3484520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 3494520Snw141292 UNLOCK_CONFIG(); 3504520Snw141292 3514520Snw141292 /* Get cache handle */ 3524520Snw141292 result->retcode = get_cache_handle(&cache); 3534520Snw141292 if (result->retcode != IDMAP_SUCCESS) 3544520Snw141292 goto out; 3554520Snw141292 3564520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 3574520Snw141292 3584520Snw141292 /* Create LIMIT expression. */ 3594520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 3604520Snw141292 limit = maxlimit; 3614520Snw141292 if (limit > 0) 3624520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 3634520Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 3644520Snw141292 3654520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 3664520Snw141292 3674520Snw141292 /* 3684520Snw141292 * Combine all the above into a giant SELECT statement that 3694520Snw141292 * will return the requested mappings 3704520Snw141292 */ 3714520Snw141292 sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w," 3724520Snw141292 " windomain, winname, unixname" 3734520Snw141292 " FROM idmap_cache WHERE " 3744520Snw141292 " %s AND is_user = %d %s;", 3754520Snw141292 rbuf, is_user?1:0, lbuf); 3764520Snw141292 if (sql == NULL) { 3774520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3784520Snw141292 goto out; 3794520Snw141292 } 3804520Snw141292 3814520Snw141292 /* Execute the SQL statement and update the return buffer */ 3824520Snw141292 PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb, 3834520Snw141292 result, result->mappings.mappings_len); 3844520Snw141292 3854520Snw141292 out: 3864520Snw141292 if (sql) 3874520Snw141292 sqlite_freemem(sql); 3884520Snw141292 if (IDMAP_ERROR(result->retcode)) 3894520Snw141292 (void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 3904520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 3914520Snw141292 return (TRUE); 3924520Snw141292 } 3934520Snw141292 3944520Snw141292 3954520Snw141292 /* ARGSUSED */ 3964520Snw141292 static int 3974520Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) { 3984520Snw141292 list_cb_data_t *cb_data; 3994520Snw141292 idmap_namerules_res *result; 4004520Snw141292 idmap_retcode retcode; 4014520Snw141292 int w2u_order, u2w_order; 4024520Snw141292 char *end; 4034520Snw141292 4044520Snw141292 cb_data = (list_cb_data_t *)parg; 4054520Snw141292 result = (idmap_namerules_res *)cb_data->result; 4064520Snw141292 4074520Snw141292 _VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val, 4084520Snw141292 sizeof (idmap_namerule)); 4094520Snw141292 4104520Snw141292 result->rules.rules_len++; 4114520Snw141292 4124520Snw141292 result->rules.rules_val[cb_data->next].is_user = 4134520Snw141292 strtol(argv[1], &end, 10); 4144520Snw141292 4155064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain, 4165064Sdm199847 argv[2]); 4174520Snw141292 4185064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname, 4195064Sdm199847 argv[3]); 4204520Snw141292 4214520Snw141292 result->rules.rules_val[cb_data->next].is_nt4 = 4224520Snw141292 strtol(argv[4], &end, 10); 4234520Snw141292 4245064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname, 4255064Sdm199847 argv[5]); 4264520Snw141292 4274520Snw141292 w2u_order = argv[6]?strtol(argv[6], &end, 10):0; 4284520Snw141292 u2w_order = argv[7]?strtol(argv[7], &end, 10):0; 4294520Snw141292 4304520Snw141292 if (w2u_order > 0 && u2w_order == 0) 4314644Sbaban result->rules.rules_val[cb_data->next].direction = 4324644Sbaban IDMAP_DIRECTION_W2U; 4334520Snw141292 else if (w2u_order == 0 && u2w_order > 0) 4344644Sbaban result->rules.rules_val[cb_data->next].direction = 4354644Sbaban IDMAP_DIRECTION_U2W; 4364520Snw141292 else 4374644Sbaban result->rules.rules_val[cb_data->next].direction = 4384644Sbaban IDMAP_DIRECTION_BI; 4394520Snw141292 4404520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 4414520Snw141292 cb_data->next++; 4424520Snw141292 result->retcode = IDMAP_SUCCESS; 4434520Snw141292 return (0); 4444520Snw141292 } 4454520Snw141292 4464520Snw141292 4474520Snw141292 /* ARGSUSED */ 4484520Snw141292 bool_t 4494520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid, 4504520Snw141292 uint64_t limit, idmap_namerules_res *result, 4514520Snw141292 struct svc_req *rqstp) { 4524520Snw141292 4534520Snw141292 sqlite *db = NULL; 4544520Snw141292 char w2ubuf[15], u2wbuf[15]; 4554520Snw141292 char lbuf[30], rbuf[30]; 4564520Snw141292 char *sql = NULL; 4574520Snw141292 char *s_windomain = NULL, *s_winname = NULL; 4584520Snw141292 char *s_unixname = NULL; 4594520Snw141292 uint64_t maxlimit; 4604520Snw141292 idmap_retcode retcode; 4614520Snw141292 4624520Snw141292 (void) memset(result, 0, sizeof (*result)); 4634520Snw141292 lbuf[0] = rbuf[0] = 0; 4644520Snw141292 4654520Snw141292 RDLOCK_CONFIG(); 4664520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 4674520Snw141292 UNLOCK_CONFIG(); 4684520Snw141292 4694520Snw141292 /* Get db handle */ 4704520Snw141292 result->retcode = get_db_handle(&db); 4714520Snw141292 if (result->retcode != IDMAP_SUCCESS) 4724520Snw141292 goto out; 4734520Snw141292 4744520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 4754520Snw141292 4764520Snw141292 if (rule.direction < 0) { 4774520Snw141292 w2ubuf[0] = u2wbuf[0] = 0; 4784644Sbaban } else if (rule.direction == IDMAP_DIRECTION_BI) { 4794520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 4804520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 4814644Sbaban } else if (rule.direction == IDMAP_DIRECTION_W2U) { 4824520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 4834520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), 4844520Snw141292 "AND (u2w_order = 0 OR u2w_order ISNULL)"); 4854644Sbaban } else if (rule.direction == IDMAP_DIRECTION_U2W) { 4864520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), 4874520Snw141292 "AND (w2u_order = 0 OR w2u_order ISNULL)"); 4884520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 4894520Snw141292 } 4904520Snw141292 4914520Snw141292 /* Create where statement for windomain */ 4925064Sdm199847 if (!EMPTY_STRING(rule.windomain)) { 4934520Snw141292 if (gen_sql_expr_from_utf8str("AND", "windomain", "=", 4945064Sdm199847 rule.windomain, 4954520Snw141292 "", &s_windomain) != IDMAP_SUCCESS) 4964520Snw141292 goto out; 4974520Snw141292 } 4984520Snw141292 4994520Snw141292 /* Create where statement for winname */ 5005064Sdm199847 if (!EMPTY_STRING(rule.winname)) { 5014520Snw141292 if (gen_sql_expr_from_utf8str("AND", "winname", "=", 5025064Sdm199847 rule.winname, 5034520Snw141292 "", &s_winname) != IDMAP_SUCCESS) 5044520Snw141292 goto out; 5054520Snw141292 } 5064520Snw141292 5074520Snw141292 /* Create where statement for unixname */ 5085064Sdm199847 if (!EMPTY_STRING(rule.unixname)) { 5094520Snw141292 if (gen_sql_expr_from_utf8str("AND", "unixname", "=", 5105064Sdm199847 rule.unixname, 5114520Snw141292 "", &s_unixname) != IDMAP_SUCCESS) 5124520Snw141292 goto out; 5134520Snw141292 } 5144520Snw141292 5154520Snw141292 /* Create LIMIT expression. */ 5164520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 5174520Snw141292 limit = maxlimit; 5184520Snw141292 if (limit > 0) 5194520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 5204520Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 5214520Snw141292 5224520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 5234520Snw141292 5244520Snw141292 /* 5254520Snw141292 * Combine all the above into a giant SELECT statement that 5264520Snw141292 * will return the requested rules 5274520Snw141292 */ 5284520Snw141292 sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, " 5294520Snw141292 "is_nt4, unixname, w2u_order, u2w_order " 5304520Snw141292 "FROM namerules WHERE " 5314520Snw141292 " %s AND is_user = %d %s %s %s %s %s %s;", 5324520Snw141292 rbuf, rule.is_user?1:0, 5334520Snw141292 s_windomain?s_windomain:"", 5344520Snw141292 s_winname?s_winname:"", 5354520Snw141292 s_unixname?s_unixname:"", 5364520Snw141292 w2ubuf, u2wbuf, lbuf); 5374520Snw141292 if (sql == NULL) { 5384520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 5394520Snw141292 goto out; 5404520Snw141292 } 5414520Snw141292 5424520Snw141292 /* Execute the SQL statement and update the return buffer */ 5434520Snw141292 PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb, 5444520Snw141292 result, result->rules.rules_len); 5454520Snw141292 5464520Snw141292 out: 5474520Snw141292 if (s_windomain) 5484520Snw141292 sqlite_freemem(s_windomain); 5494520Snw141292 if (s_winname) 5504520Snw141292 sqlite_freemem(s_winname); 5514520Snw141292 if (s_unixname) 5524520Snw141292 sqlite_freemem(s_unixname); 5534520Snw141292 if (sql) 5544520Snw141292 sqlite_freemem(sql); 5554520Snw141292 if (IDMAP_ERROR(result->retcode)) 5564520Snw141292 (void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result); 5574520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 5584520Snw141292 return (TRUE); 5594520Snw141292 } 5604520Snw141292 5614520Snw141292 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules" 5624520Snw141292 static int 5634520Snw141292 verify_rules_auth(struct svc_req *rqstp) { 5644520Snw141292 ucred_t *uc = NULL; 5654520Snw141292 uid_t uid; 5664520Snw141292 char buf[1024]; 5674520Snw141292 struct passwd pwd; 5684520Snw141292 const char *me = "verify_rules_auth"; 5694520Snw141292 5704520Snw141292 if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) { 5714520Snw141292 idmapdlog(LOG_ERR, 5724520Snw141292 "%s: svc_getcallerucred failed (errno=%d)", 5734520Snw141292 me, errno); 5744520Snw141292 return (-1); 5754520Snw141292 } 5764520Snw141292 5774520Snw141292 uid = ucred_geteuid(uc); 5784520Snw141292 if (uid == (uid_t)-1) { 5794520Snw141292 idmapdlog(LOG_ERR, 5804520Snw141292 "%s: ucred_geteuid failed (errno=%d)", 5814520Snw141292 me, errno); 5824520Snw141292 ucred_free(uc); 5834520Snw141292 return (-1); 5844520Snw141292 } 5854520Snw141292 5864520Snw141292 if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) { 5874520Snw141292 idmapdlog(LOG_ERR, 5884520Snw141292 "%s: getpwuid_r(%u) failed (errno=%d)", 5894520Snw141292 me, uid, errno); 5904520Snw141292 ucred_free(uc); 5914520Snw141292 return (-1); 5924520Snw141292 } 5934520Snw141292 5944520Snw141292 if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) { 5954520Snw141292 idmapdlog(LOG_INFO, 5964520Snw141292 "%s: %s does not have authorization.", 5974520Snw141292 me, pwd.pw_name); 5984520Snw141292 ucred_free(uc); 5994520Snw141292 return (-1); 6004520Snw141292 } 6014520Snw141292 6024520Snw141292 ucred_free(uc); 6034520Snw141292 return (1); 6044520Snw141292 } 6054520Snw141292 6065064Sdm199847 /* 6075064Sdm199847 * Meaning of the return values is the following: For retcode == 6085064Sdm199847 * IDMAP_SUCCESS, everything went OK and error_index is 6095064Sdm199847 * undefined. Otherwise, error_index >=0 shows the failed batch 6105064Sdm199847 * element. errro_index == -1 indicates failure at the beginning, 6115064Sdm199847 * error_index == -2 at the end. 6125064Sdm199847 */ 6135064Sdm199847 6144520Snw141292 /* ARGSUSED */ 6154520Snw141292 bool_t 6165064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res, 6174520Snw141292 struct svc_req *rqstp) { 6184520Snw141292 sqlite *db = NULL; 6194520Snw141292 idmap_update_op *up; 6204520Snw141292 int i; 6214884Sjp151216 int trans = FALSE; 6224520Snw141292 6235064Sdm199847 res->error_index = -1; 6245064Sdm199847 (void) memset(&res->error_rule, 0, sizeof (res->error_rule)); 6255064Sdm199847 (void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule)); 6265064Sdm199847 6274520Snw141292 if (verify_rules_auth(rqstp) < 0) { 6285064Sdm199847 res->retcode = IDMAP_ERR_PERMISSION_DENIED; 6294520Snw141292 goto out; 6304520Snw141292 } 6314520Snw141292 6324520Snw141292 if (batch.idmap_update_batch_len == 0 || 6334520Snw141292 batch.idmap_update_batch_val == NULL) { 6345064Sdm199847 res->retcode = IDMAP_SUCCESS; 6354520Snw141292 goto out; 6364520Snw141292 } 6374520Snw141292 6384520Snw141292 /* Get db handle */ 6395064Sdm199847 res->retcode = get_db_handle(&db); 6405064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 6414520Snw141292 goto out; 6424520Snw141292 6435064Sdm199847 res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;"); 6445064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 6454520Snw141292 goto out; 6464884Sjp151216 trans = TRUE; 6474520Snw141292 6484520Snw141292 for (i = 0; i < batch.idmap_update_batch_len; i++) { 6494520Snw141292 up = &batch.idmap_update_batch_val[i]; 6504520Snw141292 switch (up->opnum) { 6514520Snw141292 case OP_NONE: 6525064Sdm199847 res->retcode = IDMAP_SUCCESS; 6534520Snw141292 break; 6544520Snw141292 case OP_ADD_NAMERULE: 6555064Sdm199847 res->retcode = add_namerule(db, 6564520Snw141292 &up->idmap_update_op_u.rule); 6574520Snw141292 break; 6584520Snw141292 case OP_RM_NAMERULE: 6595064Sdm199847 res->retcode = rm_namerule(db, 6604520Snw141292 &up->idmap_update_op_u.rule); 6614520Snw141292 break; 6624520Snw141292 case OP_FLUSH_NAMERULES: 6635064Sdm199847 res->retcode = flush_namerules(db, 6644520Snw141292 up->idmap_update_op_u.is_user); 6654520Snw141292 break; 6664520Snw141292 default: 6675064Sdm199847 res->retcode = IDMAP_ERR_NOTSUPPORTED; 6685064Sdm199847 break; 6694520Snw141292 }; 6704520Snw141292 6715064Sdm199847 if (res->retcode != IDMAP_SUCCESS) { 6725064Sdm199847 res->error_index = i; 6735064Sdm199847 if (up->opnum == OP_ADD_NAMERULE || 6745064Sdm199847 up->opnum == OP_RM_NAMERULE) { 6755064Sdm199847 idmap_stat r2 = 6765064Sdm199847 idmap_namerule_cpy(&res->error_rule, 6775064Sdm199847 &up->idmap_update_op_u.rule); 6785064Sdm199847 if (r2 != IDMAP_SUCCESS) 6795064Sdm199847 res->retcode = r2; 6805064Sdm199847 } 6814520Snw141292 goto out; 6825064Sdm199847 } 6834520Snw141292 } 6844520Snw141292 6854520Snw141292 out: 6864884Sjp151216 if (trans) { 6875064Sdm199847 if (res->retcode == IDMAP_SUCCESS) { 6885064Sdm199847 res->retcode = 6895064Sdm199847 sql_exec_no_cb(db, "COMMIT TRANSACTION;"); 6905064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 6915064Sdm199847 res->error_index = -2; 6925064Sdm199847 } 6934884Sjp151216 else 6944884Sjp151216 (void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;"); 6954520Snw141292 } 6965064Sdm199847 6975064Sdm199847 res->retcode = idmap_stat4prot(res->retcode); 6985064Sdm199847 6994520Snw141292 return (TRUE); 7004520Snw141292 } 7014520Snw141292 7024520Snw141292 7034520Snw141292 /* ARGSUSED */ 7044520Snw141292 bool_t 7054520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request, 7064520Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) { 7074520Snw141292 sqlite *cache = NULL, *db = NULL; 7084520Snw141292 7094520Snw141292 /* Init */ 7104520Snw141292 (void) memset(result, 0, sizeof (*result)); 7114520Snw141292 7124520Snw141292 /* Get cache handle */ 7134520Snw141292 result->retcode = get_cache_handle(&cache); 7144520Snw141292 if (result->retcode != IDMAP_SUCCESS) 7154520Snw141292 goto out; 7164520Snw141292 7174520Snw141292 /* Get db handle */ 7184520Snw141292 result->retcode = get_db_handle(&db); 7194520Snw141292 if (result->retcode != IDMAP_SUCCESS) 7204520Snw141292 goto out; 7214520Snw141292 7224520Snw141292 /* Allocate result */ 7234520Snw141292 result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping)); 7244520Snw141292 if (result->mappings.mappings_val == NULL) { 7254520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 7264520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 7274520Snw141292 goto out; 7284520Snw141292 } 7294520Snw141292 result->mappings.mappings_len = 1; 7304520Snw141292 7314520Snw141292 if (IS_REQUEST_SID(request)) { 7324520Snw141292 result->retcode = get_w2u_mapping( 7334520Snw141292 cache, 7344520Snw141292 db, 7354520Snw141292 &request, 7364520Snw141292 result->mappings.mappings_val); 7374520Snw141292 } else if (IS_REQUEST_UID(request)) { 7384520Snw141292 result->retcode = get_u2w_mapping( 7394520Snw141292 cache, 7404520Snw141292 db, 7414520Snw141292 &request, 7424520Snw141292 result->mappings.mappings_val, 7434520Snw141292 1); 7444520Snw141292 } else if (IS_REQUEST_GID(request)) { 7454520Snw141292 result->retcode = get_u2w_mapping( 7464520Snw141292 cache, 7474520Snw141292 db, 7484520Snw141292 &request, 7494520Snw141292 result->mappings.mappings_val, 7504520Snw141292 0); 7514520Snw141292 } else { 7524520Snw141292 result->retcode = IDMAP_ERR_IDTYPE; 7534520Snw141292 } 7544520Snw141292 7554520Snw141292 out: 7564520Snw141292 if (IDMAP_FATAL_ERROR(result->retcode)) { 7574520Snw141292 xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 7584520Snw141292 result->mappings.mappings_len = 0; 7594520Snw141292 result->mappings.mappings_val = NULL; 7604520Snw141292 } 7614520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 7624520Snw141292 return (TRUE); 7634520Snw141292 } 7644520Snw141292 7654520Snw141292 7664520Snw141292 /* ARGSUSED */ 7674520Snw141292 int 7684520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, 7694520Snw141292 caddr_t result) { 7704520Snw141292 (void) xdr_free(xdr_result, result); 7714520Snw141292 return (TRUE); 7724520Snw141292 } 773