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 664520Snw141292 /* ARGSUSED */ 674520Snw141292 bool_t 684520Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) { 694520Snw141292 return (TRUE); 704520Snw141292 } 714520Snw141292 724520Snw141292 #define IS_BATCH_SID(batch, i)\ 734520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID 744520Snw141292 754520Snw141292 #define IS_BATCH_UID(batch, i)\ 764520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID 774520Snw141292 784520Snw141292 #define IS_BATCH_GID(batch, i)\ 794520Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID 804520Snw141292 814520Snw141292 #define IS_REQUEST_SID(request)\ 824520Snw141292 request.id1.idtype == IDMAP_SID 834520Snw141292 844520Snw141292 #define IS_REQUEST_UID(request)\ 854520Snw141292 request.id1.idtype == IDMAP_UID 864520Snw141292 874520Snw141292 #define IS_REQUEST_GID(request)\ 884520Snw141292 request.id1.idtype == IDMAP_GID 894520Snw141292 904520Snw141292 /* ARGSUSED */ 914520Snw141292 bool_t 924520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch, 934520Snw141292 idmap_ids_res *result, struct svc_req *rqstp) { 944520Snw141292 sqlite *cache = NULL, *db = NULL; 954520Snw141292 lookup_state_t state; 964520Snw141292 idmap_retcode retcode, winrc; 97*4864Sbaban uint_t i; 984520Snw141292 994520Snw141292 /* Init */ 1004520Snw141292 (void) memset(result, 0, sizeof (*result)); 1014520Snw141292 (void) memset(&state, 0, sizeof (state)); 1024520Snw141292 1034520Snw141292 /* Return success if nothing was requested */ 1044520Snw141292 if (batch.idmap_mapping_batch_len < 1) 1054520Snw141292 goto out; 1064520Snw141292 1074520Snw141292 /* Get cache handle */ 1084520Snw141292 result->retcode = get_cache_handle(&cache); 1094520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1104520Snw141292 goto out; 1114520Snw141292 1124520Snw141292 /* Get db handle */ 1134520Snw141292 result->retcode = get_db_handle(&db); 1144520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1154520Snw141292 goto out; 1164520Snw141292 1174520Snw141292 /* Allocate result array */ 1184520Snw141292 result->ids.ids_val = calloc(batch.idmap_mapping_batch_len, 1194520Snw141292 sizeof (idmap_id_res)); 1204520Snw141292 if (result->ids.ids_val == NULL) { 1214520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1224520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 1234520Snw141292 goto out; 1244520Snw141292 } 1254520Snw141292 result->ids.ids_len = batch.idmap_mapping_batch_len; 1264520Snw141292 127*4864Sbaban /* Allocate hash table to check for duplicate sids */ 128*4864Sbaban state.sid_history = calloc(batch.idmap_mapping_batch_len, 129*4864Sbaban sizeof (*state.sid_history)); 130*4864Sbaban if (state.sid_history == NULL) { 131*4864Sbaban idmapdlog(LOG_ERR, "Out of memory"); 132*4864Sbaban result->retcode = IDMAP_ERR_MEMORY; 133*4864Sbaban goto out; 134*4864Sbaban } 135*4864Sbaban state.sid_history_size = batch.idmap_mapping_batch_len; 136*4864Sbaban for (i = 0; i < state.sid_history_size; i++) { 137*4864Sbaban state.sid_history[i].key = state.sid_history_size; 138*4864Sbaban state.sid_history[i].next = state.sid_history_size; 139*4864Sbaban } 140*4864Sbaban state.batch = &batch; 141*4864Sbaban state.result = result; 142*4864Sbaban 1434520Snw141292 /* Init our 'done' flags */ 1444520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 1454520Snw141292 1464520Snw141292 /* First stage */ 1474520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 148*4864Sbaban state.curpos = i; 1494520Snw141292 if (IS_BATCH_SID(batch, i)) { 1504520Snw141292 retcode = sid2pid_first_pass( 1514520Snw141292 &state, 1524520Snw141292 cache, 1534520Snw141292 &batch.idmap_mapping_batch_val[i], 1544520Snw141292 &result->ids.ids_val[i]); 1554520Snw141292 } else if (IS_BATCH_UID(batch, i)) { 1564520Snw141292 retcode = pid2sid_first_pass( 1574520Snw141292 &state, 1584520Snw141292 cache, 1594520Snw141292 db, 1604520Snw141292 &batch.idmap_mapping_batch_val[i], 1614520Snw141292 &result->ids.ids_val[i], 1, 0); 1624520Snw141292 } else if (IS_BATCH_GID(batch, i)) { 1634520Snw141292 retcode = pid2sid_first_pass( 1644520Snw141292 &state, 1654520Snw141292 cache, 1664520Snw141292 db, 1674520Snw141292 &batch.idmap_mapping_batch_val[i], 1684520Snw141292 &result->ids.ids_val[i], 0, 0); 1694520Snw141292 } else { 1704520Snw141292 result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE; 1714520Snw141292 continue; 1724520Snw141292 } 1734520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 1744520Snw141292 result->retcode = retcode; 1754520Snw141292 goto out; 1764520Snw141292 } 1774520Snw141292 } 1784520Snw141292 1794520Snw141292 /* Check if we are done */ 1804520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 1814520Snw141292 goto out; 1824520Snw141292 1834520Snw141292 /* Process Windows server lookups for sid2name */ 1844520Snw141292 if (state.ad_nqueries) { 1854520Snw141292 winrc = lookup_win_batch_sid2name(&state, &batch, 1864520Snw141292 result); 1874520Snw141292 if (IDMAP_FATAL_ERROR(winrc)) { 1884520Snw141292 result->retcode = winrc; 1894520Snw141292 goto out; 1904520Snw141292 } 1914520Snw141292 } else 1924520Snw141292 winrc = IDMAP_SUCCESS; 1934520Snw141292 1944520Snw141292 /* Reset sid2pid 'done' flag */ 1954520Snw141292 state.sid2pid_done = TRUE; 1964520Snw141292 1974520Snw141292 /* Second stage */ 1984520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 199*4864Sbaban state.curpos = i; 2004520Snw141292 /* Process sid to pid ONLY */ 2014520Snw141292 if (IS_BATCH_SID(batch, i)) { 2024520Snw141292 if (IDMAP_ERROR(winrc)) 2034520Snw141292 result->ids.ids_val[i].retcode = winrc; 2044520Snw141292 retcode = sid2pid_second_pass( 2054520Snw141292 &state, 2064520Snw141292 cache, 2074520Snw141292 db, 2084520Snw141292 &batch.idmap_mapping_batch_val[i], 2094520Snw141292 &result->ids.ids_val[i]); 2104520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 2114520Snw141292 result->retcode = retcode; 2124520Snw141292 goto out; 2134520Snw141292 } 2144520Snw141292 } 2154520Snw141292 } 2164520Snw141292 2174520Snw141292 /* Check if we are done */ 2184520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 2194520Snw141292 goto out; 2204520Snw141292 2214520Snw141292 /* Reset our 'done' flags */ 2224520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 2234520Snw141292 2244520Snw141292 /* Update cache in a single transaction */ 2254520Snw141292 if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS) 2264520Snw141292 goto out; 2274520Snw141292 2284520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 229*4864Sbaban state.curpos = i; 2304520Snw141292 if (IS_BATCH_SID(batch, i)) { 2314520Snw141292 (void) update_cache_sid2pid( 2324520Snw141292 &state, 2334520Snw141292 cache, 2344520Snw141292 &batch.idmap_mapping_batch_val[i], 2354520Snw141292 &result->ids.ids_val[i]); 2364520Snw141292 } else if ((IS_BATCH_UID(batch, i)) || 2374520Snw141292 (IS_BATCH_GID(batch, i))) { 2384520Snw141292 (void) update_cache_pid2sid( 2394520Snw141292 &state, 2404520Snw141292 cache, 2414520Snw141292 &batch.idmap_mapping_batch_val[i], 2424520Snw141292 &result->ids.ids_val[i]); 2434520Snw141292 } 2444520Snw141292 } 2454520Snw141292 2464520Snw141292 /* Commit if we have atleast one successful update */ 2474520Snw141292 if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE) 2484520Snw141292 (void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;"); 2494520Snw141292 else 2504520Snw141292 (void) sql_exec_no_cb(cache, "END TRANSACTION;"); 2514520Snw141292 2524520Snw141292 out: 253*4864Sbaban if (state.sid_history) 254*4864Sbaban free(state.sid_history); 2554520Snw141292 if (IDMAP_ERROR(result->retcode)) { 2564520Snw141292 xdr_free(xdr_idmap_ids_res, (caddr_t)result); 2574520Snw141292 result->ids.ids_len = 0; 2584520Snw141292 result->ids.ids_val = NULL; 2594520Snw141292 } 2604520Snw141292 if (cache) 2614520Snw141292 (void) sqlite_close(cache); 2624520Snw141292 if (db) 2634520Snw141292 (void) sqlite_close(db); 2644520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 2654520Snw141292 return (TRUE); 2664520Snw141292 } 2674520Snw141292 2684520Snw141292 2694520Snw141292 /* ARGSUSED */ 2704520Snw141292 static int 2714520Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) { 2724520Snw141292 list_cb_data_t *cb_data; 2734520Snw141292 char *str; 2744520Snw141292 idmap_mappings_res *result; 2754520Snw141292 idmap_utf8str *ptr; 2764520Snw141292 idmap_retcode retcode; 2774520Snw141292 int w2u, u2w; 2784520Snw141292 char *end; 2794520Snw141292 2804520Snw141292 cb_data = (list_cb_data_t *)parg; 2814520Snw141292 result = (idmap_mappings_res *)cb_data->result; 2824520Snw141292 2834520Snw141292 _VALIDATE_LIST_CB_DATA(9, &result->mappings.mappings_val, 2844520Snw141292 sizeof (idmap_mapping)); 2854520Snw141292 2864520Snw141292 result->mappings.mappings_len++; 2874520Snw141292 2884520Snw141292 if ((str = strdup(argv[1])) == NULL) 2894520Snw141292 return (1); 2904520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix = 2914520Snw141292 str; 2924520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid = 2934520Snw141292 strtoul(argv[2], &end, 10); 2944520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idtype = IDMAP_SID; 2954520Snw141292 2964520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid = 2974520Snw141292 strtoul(argv[3], &end, 10); 2984520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idtype = IDMAP_UID; 2994520Snw141292 3004520Snw141292 w2u = argv[4]?strtol(argv[4], &end, 10):0; 3014520Snw141292 u2w = argv[5]?strtol(argv[5], &end, 10):0; 3024520Snw141292 3034520Snw141292 if (w2u > 0 && u2w == 0) 3044644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3054644Sbaban IDMAP_DIRECTION_W2U; 3064520Snw141292 else if (w2u == 0 && u2w > 0) 3074644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3084644Sbaban IDMAP_DIRECTION_U2W; 3094520Snw141292 else 3104644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3114644Sbaban IDMAP_DIRECTION_BI; 3124520Snw141292 3134520Snw141292 ptr = &result->mappings.mappings_val[cb_data->next].id1domain; 3144520Snw141292 if (idmap_str2utf8(&ptr, argv[6], 0) != IDMAP_SUCCESS) 3154520Snw141292 return (1); 3164520Snw141292 3174520Snw141292 ptr = &result->mappings.mappings_val[cb_data->next].id1name; 3184520Snw141292 if (idmap_str2utf8(&ptr, argv[7], 0) != IDMAP_SUCCESS) 3194520Snw141292 return (1); 3204520Snw141292 3214520Snw141292 ptr = &result->mappings.mappings_val[cb_data->next].id2name; 3224520Snw141292 if (idmap_str2utf8(&ptr, argv[8], 0) != IDMAP_SUCCESS) 3234520Snw141292 return (1); 3244520Snw141292 3254520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 3264520Snw141292 cb_data->next++; 3274520Snw141292 result->retcode = IDMAP_SUCCESS; 3284520Snw141292 return (0); 3294520Snw141292 } 3304520Snw141292 3314520Snw141292 3324520Snw141292 /* ARGSUSED */ 3334520Snw141292 bool_t 3344520Snw141292 idmap_list_mappings_1_svc(bool_t is_user, int64_t lastrowid, 3354520Snw141292 uint64_t limit, idmap_mappings_res *result, 3364520Snw141292 struct svc_req *rqstp) { 3374520Snw141292 sqlite *cache = NULL; 3384520Snw141292 char lbuf[30], rbuf[30]; 3394520Snw141292 uint64_t maxlimit; 3404520Snw141292 idmap_retcode retcode; 3414520Snw141292 char *sql = NULL; 3424520Snw141292 3434520Snw141292 (void) memset(result, 0, sizeof (*result)); 3444520Snw141292 lbuf[0] = rbuf[0] = 0; 3454520Snw141292 3464520Snw141292 RDLOCK_CONFIG(); 3474520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 3484520Snw141292 UNLOCK_CONFIG(); 3494520Snw141292 3504520Snw141292 /* Get cache handle */ 3514520Snw141292 result->retcode = get_cache_handle(&cache); 3524520Snw141292 if (result->retcode != IDMAP_SUCCESS) 3534520Snw141292 goto out; 3544520Snw141292 3554520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 3564520Snw141292 3574520Snw141292 /* Create LIMIT expression. */ 3584520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 3594520Snw141292 limit = maxlimit; 3604520Snw141292 if (limit > 0) 3614520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 3624520Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 3634520Snw141292 3644520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 3654520Snw141292 3664520Snw141292 /* 3674520Snw141292 * Combine all the above into a giant SELECT statement that 3684520Snw141292 * will return the requested mappings 3694520Snw141292 */ 3704520Snw141292 sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w," 3714520Snw141292 " windomain, winname, unixname" 3724520Snw141292 " FROM idmap_cache WHERE " 3734520Snw141292 " %s AND is_user = %d %s;", 3744520Snw141292 rbuf, is_user?1:0, lbuf); 3754520Snw141292 if (sql == NULL) { 3764520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 3774520Snw141292 goto out; 3784520Snw141292 } 3794520Snw141292 3804520Snw141292 /* Execute the SQL statement and update the return buffer */ 3814520Snw141292 PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb, 3824520Snw141292 result, result->mappings.mappings_len); 3834520Snw141292 3844520Snw141292 out: 3854520Snw141292 if (sql) 3864520Snw141292 sqlite_freemem(sql); 3874520Snw141292 if (IDMAP_ERROR(result->retcode)) 3884520Snw141292 (void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 3894520Snw141292 if (cache) 3904520Snw141292 (void) sqlite_close(cache); 3914520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 3924520Snw141292 return (TRUE); 3934520Snw141292 } 3944520Snw141292 3954520Snw141292 3964520Snw141292 /* ARGSUSED */ 3974520Snw141292 static int 3984520Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) { 3994520Snw141292 list_cb_data_t *cb_data; 4004520Snw141292 idmap_namerules_res *result; 4014520Snw141292 idmap_retcode retcode; 4024520Snw141292 idmap_utf8str *ptr; 4034520Snw141292 int w2u_order, u2w_order; 4044520Snw141292 char *end; 4054520Snw141292 4064520Snw141292 cb_data = (list_cb_data_t *)parg; 4074520Snw141292 result = (idmap_namerules_res *)cb_data->result; 4084520Snw141292 4094520Snw141292 _VALIDATE_LIST_CB_DATA(8, &result->rules.rules_val, 4104520Snw141292 sizeof (idmap_namerule)); 4114520Snw141292 4124520Snw141292 result->rules.rules_len++; 4134520Snw141292 4144520Snw141292 result->rules.rules_val[cb_data->next].is_user = 4154520Snw141292 strtol(argv[1], &end, 10); 4164520Snw141292 4174520Snw141292 ptr = &result->rules.rules_val[cb_data->next].windomain; 4184520Snw141292 if (idmap_str2utf8(&ptr, argv[2], 0) != IDMAP_SUCCESS) 4194520Snw141292 return (1); 4204520Snw141292 4214520Snw141292 ptr = &result->rules.rules_val[cb_data->next].winname; 4224520Snw141292 if (idmap_str2utf8(&ptr, argv[3], 0) != IDMAP_SUCCESS) 4234520Snw141292 return (1); 4244520Snw141292 4254520Snw141292 result->rules.rules_val[cb_data->next].is_nt4 = 4264520Snw141292 strtol(argv[4], &end, 10); 4274520Snw141292 4284520Snw141292 ptr = &result->rules.rules_val[cb_data->next].unixname; 4294520Snw141292 if (idmap_str2utf8(&ptr, argv[5], 0) != IDMAP_SUCCESS) 4304520Snw141292 return (1); 4314520Snw141292 4324520Snw141292 w2u_order = argv[6]?strtol(argv[6], &end, 10):0; 4334520Snw141292 u2w_order = argv[7]?strtol(argv[7], &end, 10):0; 4344520Snw141292 4354520Snw141292 if (w2u_order > 0 && u2w_order == 0) 4364644Sbaban result->rules.rules_val[cb_data->next].direction = 4374644Sbaban IDMAP_DIRECTION_W2U; 4384520Snw141292 else if (w2u_order == 0 && u2w_order > 0) 4394644Sbaban result->rules.rules_val[cb_data->next].direction = 4404644Sbaban IDMAP_DIRECTION_U2W; 4414520Snw141292 else 4424644Sbaban result->rules.rules_val[cb_data->next].direction = 4434644Sbaban IDMAP_DIRECTION_BI; 4444520Snw141292 4454520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 4464520Snw141292 cb_data->next++; 4474520Snw141292 result->retcode = IDMAP_SUCCESS; 4484520Snw141292 return (0); 4494520Snw141292 } 4504520Snw141292 4514520Snw141292 4524520Snw141292 /* ARGSUSED */ 4534520Snw141292 bool_t 4544520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid, 4554520Snw141292 uint64_t limit, idmap_namerules_res *result, 4564520Snw141292 struct svc_req *rqstp) { 4574520Snw141292 4584520Snw141292 sqlite *db = NULL; 4594520Snw141292 char w2ubuf[15], u2wbuf[15]; 4604520Snw141292 char lbuf[30], rbuf[30]; 4614520Snw141292 char *sql = NULL; 4624520Snw141292 char *s_windomain = NULL, *s_winname = NULL; 4634520Snw141292 char *s_unixname = NULL; 4644520Snw141292 uint64_t maxlimit; 4654520Snw141292 idmap_retcode retcode; 4664520Snw141292 4674520Snw141292 (void) memset(result, 0, sizeof (*result)); 4684520Snw141292 lbuf[0] = rbuf[0] = 0; 4694520Snw141292 4704520Snw141292 RDLOCK_CONFIG(); 4714520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 4724520Snw141292 UNLOCK_CONFIG(); 4734520Snw141292 4744520Snw141292 /* Get db handle */ 4754520Snw141292 result->retcode = get_db_handle(&db); 4764520Snw141292 if (result->retcode != IDMAP_SUCCESS) 4774520Snw141292 goto out; 4784520Snw141292 4794520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 4804520Snw141292 4814520Snw141292 if (rule.direction < 0) { 4824520Snw141292 w2ubuf[0] = u2wbuf[0] = 0; 4834644Sbaban } else if (rule.direction == IDMAP_DIRECTION_BI) { 4844520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 4854520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 4864644Sbaban } else if (rule.direction == IDMAP_DIRECTION_W2U) { 4874520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 4884520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), 4894520Snw141292 "AND (u2w_order = 0 OR u2w_order ISNULL)"); 4904644Sbaban } else if (rule.direction == IDMAP_DIRECTION_U2W) { 4914520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), 4924520Snw141292 "AND (w2u_order = 0 OR w2u_order ISNULL)"); 4934520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 4944520Snw141292 } 4954520Snw141292 4964520Snw141292 /* Create where statement for windomain */ 4974520Snw141292 if (rule.windomain.idmap_utf8str_len > 0) { 4984520Snw141292 if (gen_sql_expr_from_utf8str("AND", "windomain", "=", 4994520Snw141292 &rule.windomain, 5004520Snw141292 "", &s_windomain) != IDMAP_SUCCESS) 5014520Snw141292 goto out; 5024520Snw141292 } 5034520Snw141292 5044520Snw141292 /* Create where statement for winname */ 5054520Snw141292 if (rule.winname.idmap_utf8str_len > 0) { 5064520Snw141292 if (gen_sql_expr_from_utf8str("AND", "winname", "=", 5074520Snw141292 &rule.winname, 5084520Snw141292 "", &s_winname) != IDMAP_SUCCESS) 5094520Snw141292 goto out; 5104520Snw141292 } 5114520Snw141292 5124520Snw141292 /* Create where statement for unixname */ 5134520Snw141292 if (rule.unixname.idmap_utf8str_len > 0) { 5144520Snw141292 if (gen_sql_expr_from_utf8str("AND", "unixname", "=", 5154520Snw141292 &rule.unixname, 5164520Snw141292 "", &s_unixname) != IDMAP_SUCCESS) 5174520Snw141292 goto out; 5184520Snw141292 } 5194520Snw141292 5204520Snw141292 /* Create LIMIT expression. */ 5214520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 5224520Snw141292 limit = maxlimit; 5234520Snw141292 if (limit > 0) 5244520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 5254520Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 5264520Snw141292 5274520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 5284520Snw141292 5294520Snw141292 /* 5304520Snw141292 * Combine all the above into a giant SELECT statement that 5314520Snw141292 * will return the requested rules 5324520Snw141292 */ 5334520Snw141292 sql = sqlite_mprintf("SELECT rowid, is_user, windomain, winname, " 5344520Snw141292 "is_nt4, unixname, w2u_order, u2w_order " 5354520Snw141292 "FROM namerules WHERE " 5364520Snw141292 " %s AND is_user = %d %s %s %s %s %s %s;", 5374520Snw141292 rbuf, rule.is_user?1:0, 5384520Snw141292 s_windomain?s_windomain:"", 5394520Snw141292 s_winname?s_winname:"", 5404520Snw141292 s_unixname?s_unixname:"", 5414520Snw141292 w2ubuf, u2wbuf, lbuf); 5424520Snw141292 if (sql == NULL) { 5434520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 5444520Snw141292 goto out; 5454520Snw141292 } 5464520Snw141292 5474520Snw141292 /* Execute the SQL statement and update the return buffer */ 5484520Snw141292 PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb, 5494520Snw141292 result, result->rules.rules_len); 5504520Snw141292 5514520Snw141292 out: 5524520Snw141292 if (s_windomain) 5534520Snw141292 sqlite_freemem(s_windomain); 5544520Snw141292 if (s_winname) 5554520Snw141292 sqlite_freemem(s_winname); 5564520Snw141292 if (s_unixname) 5574520Snw141292 sqlite_freemem(s_unixname); 5584520Snw141292 if (sql) 5594520Snw141292 sqlite_freemem(sql); 5604520Snw141292 if (IDMAP_ERROR(result->retcode)) 5614520Snw141292 (void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result); 5624520Snw141292 if (db) 5634520Snw141292 (void) sqlite_close(db); 5644520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 5654520Snw141292 return (TRUE); 5664520Snw141292 } 5674520Snw141292 5684520Snw141292 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules" 5694520Snw141292 static int 5704520Snw141292 verify_rules_auth(struct svc_req *rqstp) { 5714520Snw141292 ucred_t *uc = NULL; 5724520Snw141292 uid_t uid; 5734520Snw141292 char buf[1024]; 5744520Snw141292 struct passwd pwd; 5754520Snw141292 const char *me = "verify_rules_auth"; 5764520Snw141292 5774520Snw141292 if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) { 5784520Snw141292 idmapdlog(LOG_ERR, 5794520Snw141292 "%s: svc_getcallerucred failed (errno=%d)", 5804520Snw141292 me, errno); 5814520Snw141292 return (-1); 5824520Snw141292 } 5834520Snw141292 5844520Snw141292 uid = ucred_geteuid(uc); 5854520Snw141292 if (uid == (uid_t)-1) { 5864520Snw141292 idmapdlog(LOG_ERR, 5874520Snw141292 "%s: ucred_geteuid failed (errno=%d)", 5884520Snw141292 me, errno); 5894520Snw141292 ucred_free(uc); 5904520Snw141292 return (-1); 5914520Snw141292 } 5924520Snw141292 5934520Snw141292 if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) { 5944520Snw141292 idmapdlog(LOG_ERR, 5954520Snw141292 "%s: getpwuid_r(%u) failed (errno=%d)", 5964520Snw141292 me, uid, errno); 5974520Snw141292 ucred_free(uc); 5984520Snw141292 return (-1); 5994520Snw141292 } 6004520Snw141292 6014520Snw141292 if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) { 6024520Snw141292 idmapdlog(LOG_INFO, 6034520Snw141292 "%s: %s does not have authorization.", 6044520Snw141292 me, pwd.pw_name); 6054520Snw141292 ucred_free(uc); 6064520Snw141292 return (-1); 6074520Snw141292 } 6084520Snw141292 6094520Snw141292 ucred_free(uc); 6104520Snw141292 return (1); 6114520Snw141292 } 6124520Snw141292 6134520Snw141292 /* ARGSUSED */ 6144520Snw141292 bool_t 6154520Snw141292 idmap_update_1_svc(idmap_update_batch batch, idmap_retcode *result, 6164520Snw141292 struct svc_req *rqstp) { 6174520Snw141292 sqlite *db = NULL; 6184520Snw141292 idmap_update_op *up; 6194520Snw141292 int i; 6204520Snw141292 6214520Snw141292 if (verify_rules_auth(rqstp) < 0) { 6224520Snw141292 *result = IDMAP_ERR_PERMISSION_DENIED; 6234520Snw141292 goto out; 6244520Snw141292 } 6254520Snw141292 6264520Snw141292 if (batch.idmap_update_batch_len == 0 || 6274520Snw141292 batch.idmap_update_batch_val == NULL) { 6284520Snw141292 *result = IDMAP_SUCCESS; 6294520Snw141292 goto out; 6304520Snw141292 } 6314520Snw141292 6324520Snw141292 /* Get db handle */ 6334520Snw141292 *result = get_db_handle(&db); 6344520Snw141292 if (*result != IDMAP_SUCCESS) 6354520Snw141292 goto out; 6364520Snw141292 6374520Snw141292 *result = sql_exec_no_cb(db, "BEGIN TRANSACTION;"); 6384520Snw141292 if (*result != IDMAP_SUCCESS) 6394520Snw141292 goto out; 6404520Snw141292 6414520Snw141292 for (i = 0; i < batch.idmap_update_batch_len; i++) { 6424520Snw141292 up = &batch.idmap_update_batch_val[i]; 6434520Snw141292 switch (up->opnum) { 6444520Snw141292 case OP_NONE: 6454520Snw141292 *result = IDMAP_SUCCESS; 6464520Snw141292 break; 6474520Snw141292 case OP_ADD_NAMERULE: 6484520Snw141292 *result = add_namerule(db, 6494520Snw141292 &up->idmap_update_op_u.rule); 6504520Snw141292 break; 6514520Snw141292 case OP_RM_NAMERULE: 6524520Snw141292 *result = rm_namerule(db, 6534520Snw141292 &up->idmap_update_op_u.rule); 6544520Snw141292 break; 6554520Snw141292 case OP_FLUSH_NAMERULES: 6564520Snw141292 *result = flush_namerules(db, 6574520Snw141292 up->idmap_update_op_u.is_user); 6584520Snw141292 break; 6594520Snw141292 default: 6604520Snw141292 *result = IDMAP_ERR_NOTSUPPORTED; 6614520Snw141292 goto out; 6624520Snw141292 }; 6634520Snw141292 6644520Snw141292 if (*result != IDMAP_SUCCESS) 6654520Snw141292 goto out; 6664520Snw141292 } 6674520Snw141292 6684520Snw141292 out: 6694520Snw141292 if (*result == IDMAP_SUCCESS && db) { 6704520Snw141292 *result = sql_exec_no_cb(db, "COMMIT TRANSACTION;"); 6714520Snw141292 } 6724520Snw141292 6734520Snw141292 if (db) 6744520Snw141292 (void) sqlite_close(db); 6754520Snw141292 *result = idmap_stat4prot(*result); 6764520Snw141292 return (TRUE); 6774520Snw141292 } 6784520Snw141292 6794520Snw141292 6804520Snw141292 /* ARGSUSED */ 6814520Snw141292 bool_t 6824520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request, 6834520Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) { 6844520Snw141292 sqlite *cache = NULL, *db = NULL; 6854520Snw141292 6864520Snw141292 /* Init */ 6874520Snw141292 (void) memset(result, 0, sizeof (*result)); 6884520Snw141292 6894520Snw141292 /* Get cache handle */ 6904520Snw141292 result->retcode = get_cache_handle(&cache); 6914520Snw141292 if (result->retcode != IDMAP_SUCCESS) 6924520Snw141292 goto out; 6934520Snw141292 6944520Snw141292 /* Get db handle */ 6954520Snw141292 result->retcode = get_db_handle(&db); 6964520Snw141292 if (result->retcode != IDMAP_SUCCESS) 6974520Snw141292 goto out; 6984520Snw141292 6994520Snw141292 /* Allocate result */ 7004520Snw141292 result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping)); 7014520Snw141292 if (result->mappings.mappings_val == NULL) { 7024520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 7034520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 7044520Snw141292 goto out; 7054520Snw141292 } 7064520Snw141292 result->mappings.mappings_len = 1; 7074520Snw141292 7084520Snw141292 if (IS_REQUEST_SID(request)) { 7094520Snw141292 result->retcode = get_w2u_mapping( 7104520Snw141292 cache, 7114520Snw141292 db, 7124520Snw141292 &request, 7134520Snw141292 result->mappings.mappings_val); 7144520Snw141292 } else if (IS_REQUEST_UID(request)) { 7154520Snw141292 result->retcode = get_u2w_mapping( 7164520Snw141292 cache, 7174520Snw141292 db, 7184520Snw141292 &request, 7194520Snw141292 result->mappings.mappings_val, 7204520Snw141292 1); 7214520Snw141292 } else if (IS_REQUEST_GID(request)) { 7224520Snw141292 result->retcode = get_u2w_mapping( 7234520Snw141292 cache, 7244520Snw141292 db, 7254520Snw141292 &request, 7264520Snw141292 result->mappings.mappings_val, 7274520Snw141292 0); 7284520Snw141292 } else { 7294520Snw141292 result->retcode = IDMAP_ERR_IDTYPE; 7304520Snw141292 } 7314520Snw141292 7324520Snw141292 out: 7334520Snw141292 if (IDMAP_FATAL_ERROR(result->retcode)) { 7344520Snw141292 xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 7354520Snw141292 result->mappings.mappings_len = 0; 7364520Snw141292 result->mappings.mappings_val = NULL; 7374520Snw141292 } 7384520Snw141292 if (cache) 7394520Snw141292 (void) sqlite_close(cache); 7404520Snw141292 if (db) 7414520Snw141292 (void) sqlite_close(db); 7424520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 7434520Snw141292 return (TRUE); 7444520Snw141292 } 7454520Snw141292 7464520Snw141292 7474520Snw141292 /* ARGSUSED */ 7484520Snw141292 int 7494520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, 7504520Snw141292 caddr_t result) { 7514520Snw141292 (void) xdr_free(xdr_result, result); 7524520Snw141292 return (TRUE); 7534520Snw141292 } 754