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 76*5696Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) 77*5696Snw141292 { 784520Snw141292 return (TRUE); 794520Snw141292 } 804520Snw141292 815474Sbaban /* 825474Sbaban * RPC layer allocates empty strings to replace NULL char *. 835474Sbaban * This utility function frees these empty strings. 845474Sbaban */ 855474Sbaban static void 865474Sbaban sanitize_mapping_request(idmap_mapping *req) 875474Sbaban { 885474Sbaban free(req->id1name); 895474Sbaban req->id1name = NULL; 905474Sbaban free(req->id1domain); 915474Sbaban req->id1domain = NULL; 925474Sbaban free(req->id2name); 935474Sbaban req->id2name = NULL; 945474Sbaban free(req->id2domain); 955474Sbaban req->id2domain = NULL; 965474Sbaban } 975474Sbaban 984520Snw141292 /* ARGSUSED */ 994520Snw141292 bool_t 1004520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch, 101*5696Snw141292 idmap_ids_res *result, struct svc_req *rqstp) 102*5696Snw141292 { 1034520Snw141292 sqlite *cache = NULL, *db = NULL; 1044520Snw141292 lookup_state_t state; 1054520Snw141292 idmap_retcode retcode, winrc; 1064864Sbaban uint_t i; 1074520Snw141292 1084520Snw141292 /* Init */ 1094520Snw141292 (void) memset(result, 0, sizeof (*result)); 1104520Snw141292 (void) memset(&state, 0, sizeof (state)); 1114520Snw141292 1124520Snw141292 /* Return success if nothing was requested */ 1134520Snw141292 if (batch.idmap_mapping_batch_len < 1) 1144520Snw141292 goto out; 1154520Snw141292 1164520Snw141292 /* Get cache handle */ 1174520Snw141292 result->retcode = get_cache_handle(&cache); 1184520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1194520Snw141292 goto out; 1204520Snw141292 1214520Snw141292 /* Get db handle */ 1224520Snw141292 result->retcode = get_db_handle(&db); 1234520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1244520Snw141292 goto out; 1254520Snw141292 1264520Snw141292 /* Allocate result array */ 1274520Snw141292 result->ids.ids_val = calloc(batch.idmap_mapping_batch_len, 128*5696Snw141292 sizeof (idmap_id_res)); 1294520Snw141292 if (result->ids.ids_val == NULL) { 1304520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1314520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 1324520Snw141292 goto out; 1334520Snw141292 } 1344520Snw141292 result->ids.ids_len = batch.idmap_mapping_batch_len; 1354520Snw141292 1364864Sbaban /* Allocate hash table to check for duplicate sids */ 1374864Sbaban state.sid_history = calloc(batch.idmap_mapping_batch_len, 138*5696Snw141292 sizeof (*state.sid_history)); 1394864Sbaban if (state.sid_history == NULL) { 1404864Sbaban idmapdlog(LOG_ERR, "Out of memory"); 1414864Sbaban result->retcode = IDMAP_ERR_MEMORY; 1424864Sbaban goto out; 1434864Sbaban } 1444864Sbaban state.sid_history_size = batch.idmap_mapping_batch_len; 1454864Sbaban for (i = 0; i < state.sid_history_size; i++) { 1464864Sbaban state.sid_history[i].key = state.sid_history_size; 1474864Sbaban state.sid_history[i].next = state.sid_history_size; 1484864Sbaban } 1494864Sbaban state.batch = &batch; 1504864Sbaban state.result = result; 1514864Sbaban 1524520Snw141292 /* Init our 'done' flags */ 1534520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 1544520Snw141292 1554520Snw141292 /* First stage */ 1564520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 1574864Sbaban state.curpos = i; 1585474Sbaban (void) sanitize_mapping_request( 1595474Sbaban &batch.idmap_mapping_batch_val[i]); 1604520Snw141292 if (IS_BATCH_SID(batch, i)) { 1614520Snw141292 retcode = sid2pid_first_pass( 162*5696Snw141292 &state, 163*5696Snw141292 cache, 164*5696Snw141292 &batch.idmap_mapping_batch_val[i], 165*5696Snw141292 &result->ids.ids_val[i]); 1664520Snw141292 } else if (IS_BATCH_UID(batch, i)) { 1674520Snw141292 retcode = pid2sid_first_pass( 168*5696Snw141292 &state, 169*5696Snw141292 cache, 170*5696Snw141292 db, 171*5696Snw141292 &batch.idmap_mapping_batch_val[i], 172*5696Snw141292 &result->ids.ids_val[i], 1, 0); 1734520Snw141292 } else if (IS_BATCH_GID(batch, i)) { 1744520Snw141292 retcode = pid2sid_first_pass( 175*5696Snw141292 &state, 176*5696Snw141292 cache, 177*5696Snw141292 db, 178*5696Snw141292 &batch.idmap_mapping_batch_val[i], 179*5696Snw141292 &result->ids.ids_val[i], 0, 0); 1804520Snw141292 } else { 1814520Snw141292 result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE; 1824520Snw141292 continue; 1834520Snw141292 } 1844520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 1854520Snw141292 result->retcode = retcode; 1864520Snw141292 goto out; 1874520Snw141292 } 1884520Snw141292 } 1894520Snw141292 1904520Snw141292 /* Check if we are done */ 1914520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 1924520Snw141292 goto out; 1934520Snw141292 1944520Snw141292 /* Process Windows server lookups for sid2name */ 1954520Snw141292 if (state.ad_nqueries) { 1964520Snw141292 winrc = lookup_win_batch_sid2name(&state, &batch, 197*5696Snw141292 result); 1984520Snw141292 if (IDMAP_FATAL_ERROR(winrc)) { 1994520Snw141292 result->retcode = winrc; 2004520Snw141292 goto out; 2014520Snw141292 } 2024520Snw141292 } else 2034520Snw141292 winrc = IDMAP_SUCCESS; 2044520Snw141292 2054520Snw141292 /* Reset sid2pid 'done' flag */ 2064520Snw141292 state.sid2pid_done = TRUE; 2074520Snw141292 2084520Snw141292 /* Second stage */ 2094520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 2104864Sbaban state.curpos = i; 2114520Snw141292 /* Process sid to pid ONLY */ 2124520Snw141292 if (IS_BATCH_SID(batch, i)) { 2134520Snw141292 if (IDMAP_ERROR(winrc)) 2144520Snw141292 result->ids.ids_val[i].retcode = winrc; 2154520Snw141292 retcode = sid2pid_second_pass( 216*5696Snw141292 &state, 217*5696Snw141292 cache, 218*5696Snw141292 db, 219*5696Snw141292 &batch.idmap_mapping_batch_val[i], 220*5696Snw141292 &result->ids.ids_val[i]); 2214520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 2224520Snw141292 result->retcode = retcode; 2234520Snw141292 goto out; 2244520Snw141292 } 2254520Snw141292 } 2264520Snw141292 } 2274520Snw141292 2284520Snw141292 /* Check if we are done */ 2294520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 2304520Snw141292 goto out; 2314520Snw141292 2324520Snw141292 /* Reset our 'done' flags */ 2334520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 2344520Snw141292 2354520Snw141292 /* Update cache in a single transaction */ 2364520Snw141292 if (sql_exec_no_cb(cache, "BEGIN TRANSACTION;") != IDMAP_SUCCESS) 2374520Snw141292 goto out; 2384520Snw141292 2394520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 2404864Sbaban state.curpos = i; 2414520Snw141292 if (IS_BATCH_SID(batch, i)) { 2424520Snw141292 (void) update_cache_sid2pid( 243*5696Snw141292 &state, 244*5696Snw141292 cache, 245*5696Snw141292 &batch.idmap_mapping_batch_val[i], 246*5696Snw141292 &result->ids.ids_val[i]); 2474520Snw141292 } else if ((IS_BATCH_UID(batch, i)) || 248*5696Snw141292 (IS_BATCH_GID(batch, i))) { 2494520Snw141292 (void) update_cache_pid2sid( 250*5696Snw141292 &state, 251*5696Snw141292 cache, 252*5696Snw141292 &batch.idmap_mapping_batch_val[i], 253*5696Snw141292 &result->ids.ids_val[i]); 2544520Snw141292 } 2554520Snw141292 } 2564520Snw141292 2575331Samw /* Commit if we have at least one successful update */ 2584520Snw141292 if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE) 2594520Snw141292 (void) sql_exec_no_cb(cache, "COMMIT TRANSACTION;"); 2604520Snw141292 else 2614520Snw141292 (void) sql_exec_no_cb(cache, "END TRANSACTION;"); 2624520Snw141292 2634520Snw141292 out: 2644864Sbaban if (state.sid_history) 2654864Sbaban free(state.sid_history); 2664520Snw141292 if (IDMAP_ERROR(result->retcode)) { 2674520Snw141292 xdr_free(xdr_idmap_ids_res, (caddr_t)result); 2684520Snw141292 result->ids.ids_len = 0; 2694520Snw141292 result->ids.ids_val = NULL; 2704520Snw141292 } 2714520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 2724520Snw141292 return (TRUE); 2734520Snw141292 } 2744520Snw141292 2754520Snw141292 2764520Snw141292 /* ARGSUSED */ 277*5696Snw141292 static 278*5696Snw141292 int 279*5696Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) 280*5696Snw141292 { 2814520Snw141292 list_cb_data_t *cb_data; 2824520Snw141292 char *str; 2834520Snw141292 idmap_mappings_res *result; 2844520Snw141292 idmap_retcode retcode; 2854520Snw141292 int w2u, u2w; 2864520Snw141292 char *end; 287*5696Snw141292 static int validated_column_names = 0; 288*5696Snw141292 289*5696Snw141292 if (!validated_column_names) { 290*5696Snw141292 assert(strcmp(colnames[0], "rowid") == 0); 291*5696Snw141292 assert(strcmp(colnames[1], "sidprefix") == 0); 292*5696Snw141292 assert(strcmp(colnames[2], "rid") == 0); 293*5696Snw141292 assert(strcmp(colnames[3], "pid") == 0); 294*5696Snw141292 assert(strcmp(colnames[4], "w2u") == 0); 295*5696Snw141292 assert(strcmp(colnames[5], "u2w") == 0); 296*5696Snw141292 assert(strcmp(colnames[6], "windomain") == 0); 297*5696Snw141292 assert(strcmp(colnames[7], "canon_winname") == 0); 298*5696Snw141292 assert(strcmp(colnames[8], "unixname") == 0); 299*5696Snw141292 assert(strcmp(colnames[9], "is_user") == 0); 300*5696Snw141292 assert(strcmp(colnames[10], "is_wuser") == 0); 301*5696Snw141292 validated_column_names = 1; 302*5696Snw141292 } 303*5696Snw141292 3044520Snw141292 3054520Snw141292 cb_data = (list_cb_data_t *)parg; 3064520Snw141292 result = (idmap_mappings_res *)cb_data->result; 3074520Snw141292 308*5696Snw141292 _VALIDATE_LIST_CB_DATA(11, &result->mappings.mappings_val, 309*5696Snw141292 sizeof (idmap_mapping)); 3104520Snw141292 3114520Snw141292 result->mappings.mappings_len++; 3124520Snw141292 3134520Snw141292 if ((str = strdup(argv[1])) == NULL) 3144520Snw141292 return (1); 3154520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix = 316*5696Snw141292 str; 3174520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid = 318*5696Snw141292 strtoul(argv[2], &end, 10); 319*5696Snw141292 result->mappings.mappings_val[cb_data->next].id1.idtype = 320*5696Snw141292 strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID; 3214520Snw141292 3224520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid = 323*5696Snw141292 strtoul(argv[3], &end, 10); 324*5696Snw141292 result->mappings.mappings_val[cb_data->next].id2.idtype = 325*5696Snw141292 strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID; 3264520Snw141292 327*5696Snw141292 w2u = argv[4] ? strtol(argv[4], &end, 10) : 0; 328*5696Snw141292 u2w = argv[5] ? strtol(argv[5], &end, 10) : 0; 3294520Snw141292 3304520Snw141292 if (w2u > 0 && u2w == 0) 3314644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3324644Sbaban IDMAP_DIRECTION_W2U; 3334520Snw141292 else if (w2u == 0 && u2w > 0) 3344644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3354644Sbaban IDMAP_DIRECTION_U2W; 3364520Snw141292 else 3374644Sbaban result->mappings.mappings_val[cb_data->next].direction = 3384644Sbaban IDMAP_DIRECTION_BI; 3394520Snw141292 3405064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain, 3415064Sdm199847 argv[6]); 3424520Snw141292 3435064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name, 3445064Sdm199847 argv[7]); 3454520Snw141292 3465064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name, 3475064Sdm199847 argv[8]); 3485064Sdm199847 3494520Snw141292 3504520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 3514520Snw141292 cb_data->next++; 3524520Snw141292 result->retcode = IDMAP_SUCCESS; 3534520Snw141292 return (0); 3544520Snw141292 } 3554520Snw141292 3564520Snw141292 3574520Snw141292 /* ARGSUSED */ 3584520Snw141292 bool_t 359*5696Snw141292 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit, 360*5696Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) 361*5696Snw141292 { 3624520Snw141292 sqlite *cache = NULL; 3634520Snw141292 char lbuf[30], rbuf[30]; 3644520Snw141292 uint64_t maxlimit; 3654520Snw141292 idmap_retcode retcode; 3664520Snw141292 char *sql = NULL; 3674520Snw141292 3684520Snw141292 (void) memset(result, 0, sizeof (*result)); 3694520Snw141292 lbuf[0] = rbuf[0] = 0; 3704520Snw141292 3714520Snw141292 RDLOCK_CONFIG(); 3724520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 3734520Snw141292 UNLOCK_CONFIG(); 3744520Snw141292 3754520Snw141292 /* Get cache handle */ 3764520Snw141292 result->retcode = get_cache_handle(&cache); 3774520Snw141292 if (result->retcode != IDMAP_SUCCESS) 3784520Snw141292 goto out; 3794520Snw141292 3804520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 3814520Snw141292 3824520Snw141292 /* Create LIMIT expression. */ 3834520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 3844520Snw141292 limit = maxlimit; 3854520Snw141292 if (limit > 0) 3864520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 387*5696Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 3884520Snw141292 3894520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 3904520Snw141292 3914520Snw141292 /* 3924520Snw141292 * Combine all the above into a giant SELECT statement that 3934520Snw141292 * will return the requested mappings 3944520Snw141292 */ 395*5696Snw141292 sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w, " 396*5696Snw141292 "windomain, canon_winname, unixname, is_user, is_wuser " 397*5696Snw141292 " FROM idmap_cache WHERE " 398*5696Snw141292 " %s %s;", 399*5696Snw141292 rbuf, lbuf); 4004520Snw141292 if (sql == NULL) { 4014520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 4024520Snw141292 goto out; 4034520Snw141292 } 4044520Snw141292 4054520Snw141292 /* Execute the SQL statement and update the return buffer */ 4064520Snw141292 PROCESS_LIST_SVC_SQL(retcode, cache, sql, limit, list_mappings_cb, 407*5696Snw141292 result, result->mappings.mappings_len); 4084520Snw141292 4094520Snw141292 out: 4104520Snw141292 if (sql) 4114520Snw141292 sqlite_freemem(sql); 4124520Snw141292 if (IDMAP_ERROR(result->retcode)) 4134520Snw141292 (void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 4144520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 4154520Snw141292 return (TRUE); 4164520Snw141292 } 4174520Snw141292 4184520Snw141292 4194520Snw141292 /* ARGSUSED */ 420*5696Snw141292 static 421*5696Snw141292 int 422*5696Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) 423*5696Snw141292 { 4244520Snw141292 list_cb_data_t *cb_data; 4254520Snw141292 idmap_namerules_res *result; 4264520Snw141292 idmap_retcode retcode; 4274520Snw141292 int w2u_order, u2w_order; 4284520Snw141292 char *end; 429*5696Snw141292 static int validated_column_names = 0; 430*5696Snw141292 431*5696Snw141292 if (!validated_column_names) { 432*5696Snw141292 assert(strcmp(colnames[0], "rowid") == 0); 433*5696Snw141292 assert(strcmp(colnames[1], "is_user") == 0); 434*5696Snw141292 assert(strcmp(colnames[2], "is_wuser") == 0); 435*5696Snw141292 assert(strcmp(colnames[3], "windomain") == 0); 436*5696Snw141292 assert(strcmp(colnames[4], "winname_display") == 0); 437*5696Snw141292 assert(strcmp(colnames[5], "is_nt4") == 0); 438*5696Snw141292 assert(strcmp(colnames[6], "unixname") == 0); 439*5696Snw141292 assert(strcmp(colnames[7], "w2u_order") == 0); 440*5696Snw141292 assert(strcmp(colnames[8], "u2w_order") == 0); 441*5696Snw141292 validated_column_names = 1; 442*5696Snw141292 } 4434520Snw141292 4444520Snw141292 cb_data = (list_cb_data_t *)parg; 4454520Snw141292 result = (idmap_namerules_res *)cb_data->result; 4464520Snw141292 447*5696Snw141292 _VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val, 448*5696Snw141292 sizeof (idmap_namerule)); 4494520Snw141292 4504520Snw141292 result->rules.rules_len++; 4514520Snw141292 4524520Snw141292 result->rules.rules_val[cb_data->next].is_user = 453*5696Snw141292 strtol(argv[1], &end, 10); 454*5696Snw141292 455*5696Snw141292 result->rules.rules_val[cb_data->next].is_wuser = 456*5696Snw141292 strtol(argv[2], &end, 10); 4574520Snw141292 4585064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain, 4595064Sdm199847 argv[3]); 4604520Snw141292 461*5696Snw141292 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname, 462*5696Snw141292 argv[4]); 463*5696Snw141292 4644520Snw141292 result->rules.rules_val[cb_data->next].is_nt4 = 465*5696Snw141292 strtol(argv[5], &end, 10); 4664520Snw141292 4675064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname, 468*5696Snw141292 argv[6]); 4694520Snw141292 470*5696Snw141292 w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0; 471*5696Snw141292 u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0; 4724520Snw141292 4734520Snw141292 if (w2u_order > 0 && u2w_order == 0) 4744644Sbaban result->rules.rules_val[cb_data->next].direction = 4754644Sbaban IDMAP_DIRECTION_W2U; 4764520Snw141292 else if (w2u_order == 0 && u2w_order > 0) 4774644Sbaban result->rules.rules_val[cb_data->next].direction = 4784644Sbaban IDMAP_DIRECTION_U2W; 4794520Snw141292 else 4804644Sbaban result->rules.rules_val[cb_data->next].direction = 4814644Sbaban IDMAP_DIRECTION_BI; 4824520Snw141292 4834520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 4844520Snw141292 cb_data->next++; 4854520Snw141292 result->retcode = IDMAP_SUCCESS; 4864520Snw141292 return (0); 4874520Snw141292 } 4884520Snw141292 4894520Snw141292 4904520Snw141292 /* ARGSUSED */ 4914520Snw141292 bool_t 4924520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid, 4934520Snw141292 uint64_t limit, idmap_namerules_res *result, 494*5696Snw141292 struct svc_req *rqstp) 495*5696Snw141292 { 4964520Snw141292 4974520Snw141292 sqlite *db = NULL; 4984520Snw141292 char w2ubuf[15], u2wbuf[15]; 4994520Snw141292 char lbuf[30], rbuf[30]; 5004520Snw141292 char *sql = NULL; 501*5696Snw141292 char *expr = NULL; 5024520Snw141292 uint64_t maxlimit; 5034520Snw141292 idmap_retcode retcode; 5044520Snw141292 5054520Snw141292 (void) memset(result, 0, sizeof (*result)); 5064520Snw141292 lbuf[0] = rbuf[0] = 0; 5074520Snw141292 5084520Snw141292 RDLOCK_CONFIG(); 5094520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 5104520Snw141292 UNLOCK_CONFIG(); 5114520Snw141292 5124520Snw141292 /* Get db handle */ 5134520Snw141292 result->retcode = get_db_handle(&db); 5144520Snw141292 if (result->retcode != IDMAP_SUCCESS) 5154520Snw141292 goto out; 5164520Snw141292 5174520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 5184520Snw141292 519*5696Snw141292 w2ubuf[0] = u2wbuf[0] = 0; 520*5696Snw141292 if (rule.direction == IDMAP_DIRECTION_BI) { 5214520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 5224520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 5234644Sbaban } else if (rule.direction == IDMAP_DIRECTION_W2U) { 5244520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 5254520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), 526*5696Snw141292 "AND (u2w_order = 0 OR u2w_order ISNULL)"); 5274644Sbaban } else if (rule.direction == IDMAP_DIRECTION_U2W) { 5284520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), 529*5696Snw141292 "AND (w2u_order = 0 OR w2u_order ISNULL)"); 5304520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 5314520Snw141292 } 5324520Snw141292 533*5696Snw141292 retcode = gen_sql_expr_from_rule(&rule, &expr); 534*5696Snw141292 if (retcode != IDMAP_SUCCESS) 535*5696Snw141292 goto out; 5364520Snw141292 5374520Snw141292 /* Create LIMIT expression. */ 5384520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 5394520Snw141292 limit = maxlimit; 5404520Snw141292 if (limit > 0) 5414520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 542*5696Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 5434520Snw141292 5444520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 5454520Snw141292 5464520Snw141292 /* 5474520Snw141292 * Combine all the above into a giant SELECT statement that 5484520Snw141292 * will return the requested rules 5494520Snw141292 */ 550*5696Snw141292 sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, " 551*5696Snw141292 "winname_display, is_nt4, unixname, w2u_order, u2w_order " 552*5696Snw141292 "FROM namerules WHERE " 553*5696Snw141292 " %s %s %s %s %s;", 554*5696Snw141292 rbuf, expr, w2ubuf, u2wbuf, lbuf); 555*5696Snw141292 5564520Snw141292 if (sql == NULL) { 5574520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 5584520Snw141292 goto out; 5594520Snw141292 } 5604520Snw141292 5614520Snw141292 /* Execute the SQL statement and update the return buffer */ 5624520Snw141292 PROCESS_LIST_SVC_SQL(retcode, db, sql, limit, list_namerules_cb, 563*5696Snw141292 result, result->rules.rules_len); 5644520Snw141292 5654520Snw141292 out: 566*5696Snw141292 if (expr) 567*5696Snw141292 sqlite_freemem(expr); 5684520Snw141292 if (sql) 5694520Snw141292 sqlite_freemem(sql); 5704520Snw141292 if (IDMAP_ERROR(result->retcode)) 5714520Snw141292 (void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result); 5724520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 5734520Snw141292 return (TRUE); 5744520Snw141292 } 5754520Snw141292 5764520Snw141292 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules" 5774520Snw141292 static int 578*5696Snw141292 verify_rules_auth(struct svc_req *rqstp) 579*5696Snw141292 { 5804520Snw141292 ucred_t *uc = NULL; 5814520Snw141292 uid_t uid; 5824520Snw141292 char buf[1024]; 5834520Snw141292 struct passwd pwd; 5844520Snw141292 const char *me = "verify_rules_auth"; 5854520Snw141292 5864520Snw141292 if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) { 5874520Snw141292 idmapdlog(LOG_ERR, 588*5696Snw141292 "%s: svc_getcallerucred failed (errno=%d)", 589*5696Snw141292 me, errno); 5904520Snw141292 return (-1); 5914520Snw141292 } 5924520Snw141292 5934520Snw141292 uid = ucred_geteuid(uc); 5944520Snw141292 if (uid == (uid_t)-1) { 5954520Snw141292 idmapdlog(LOG_ERR, 596*5696Snw141292 "%s: ucred_geteuid failed (errno=%d)", 597*5696Snw141292 me, errno); 5984520Snw141292 ucred_free(uc); 5994520Snw141292 return (-1); 6004520Snw141292 } 6014520Snw141292 6024520Snw141292 if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) { 6034520Snw141292 idmapdlog(LOG_ERR, 604*5696Snw141292 "%s: getpwuid_r(%u) failed (errno=%d)", 605*5696Snw141292 me, uid, errno); 6064520Snw141292 ucred_free(uc); 6074520Snw141292 return (-1); 6084520Snw141292 } 6094520Snw141292 6104520Snw141292 if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) { 6114520Snw141292 idmapdlog(LOG_INFO, 612*5696Snw141292 "%s: %s does not have authorization.", 613*5696Snw141292 me, pwd.pw_name); 6144520Snw141292 ucred_free(uc); 6154520Snw141292 return (-1); 6164520Snw141292 } 6174520Snw141292 6184520Snw141292 ucred_free(uc); 6194520Snw141292 return (1); 6204520Snw141292 } 6214520Snw141292 6225064Sdm199847 /* 6235064Sdm199847 * Meaning of the return values is the following: For retcode == 6245064Sdm199847 * IDMAP_SUCCESS, everything went OK and error_index is 6255064Sdm199847 * undefined. Otherwise, error_index >=0 shows the failed batch 6265064Sdm199847 * element. errro_index == -1 indicates failure at the beginning, 6275064Sdm199847 * error_index == -2 at the end. 6285064Sdm199847 */ 6295064Sdm199847 6304520Snw141292 /* ARGSUSED */ 6314520Snw141292 bool_t 6325064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res, 633*5696Snw141292 struct svc_req *rqstp) 634*5696Snw141292 { 6354520Snw141292 sqlite *db = NULL; 6364520Snw141292 idmap_update_op *up; 6374520Snw141292 int i; 6384884Sjp151216 int trans = FALSE; 6394520Snw141292 6405064Sdm199847 res->error_index = -1; 6415064Sdm199847 (void) memset(&res->error_rule, 0, sizeof (res->error_rule)); 6425064Sdm199847 (void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule)); 6435064Sdm199847 6444520Snw141292 if (verify_rules_auth(rqstp) < 0) { 6455064Sdm199847 res->retcode = IDMAP_ERR_PERMISSION_DENIED; 6464520Snw141292 goto out; 6474520Snw141292 } 6484520Snw141292 6494520Snw141292 if (batch.idmap_update_batch_len == 0 || 650*5696Snw141292 batch.idmap_update_batch_val == NULL) { 6515064Sdm199847 res->retcode = IDMAP_SUCCESS; 6524520Snw141292 goto out; 6534520Snw141292 } 6544520Snw141292 6554520Snw141292 /* Get db handle */ 6565064Sdm199847 res->retcode = get_db_handle(&db); 6575064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 6584520Snw141292 goto out; 6594520Snw141292 6605064Sdm199847 res->retcode = sql_exec_no_cb(db, "BEGIN TRANSACTION;"); 6615064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 6624520Snw141292 goto out; 6634884Sjp151216 trans = TRUE; 6644520Snw141292 6654520Snw141292 for (i = 0; i < batch.idmap_update_batch_len; i++) { 6664520Snw141292 up = &batch.idmap_update_batch_val[i]; 6674520Snw141292 switch (up->opnum) { 6684520Snw141292 case OP_NONE: 6695064Sdm199847 res->retcode = IDMAP_SUCCESS; 6704520Snw141292 break; 6714520Snw141292 case OP_ADD_NAMERULE: 6725064Sdm199847 res->retcode = add_namerule(db, 673*5696Snw141292 &up->idmap_update_op_u.rule); 6744520Snw141292 break; 6754520Snw141292 case OP_RM_NAMERULE: 6765064Sdm199847 res->retcode = rm_namerule(db, 677*5696Snw141292 &up->idmap_update_op_u.rule); 6784520Snw141292 break; 6794520Snw141292 case OP_FLUSH_NAMERULES: 680*5696Snw141292 res->retcode = flush_namerules(db); 6814520Snw141292 break; 6824520Snw141292 default: 6835064Sdm199847 res->retcode = IDMAP_ERR_NOTSUPPORTED; 6845064Sdm199847 break; 6854520Snw141292 }; 6864520Snw141292 6875064Sdm199847 if (res->retcode != IDMAP_SUCCESS) { 6885064Sdm199847 res->error_index = i; 6895064Sdm199847 if (up->opnum == OP_ADD_NAMERULE || 6905064Sdm199847 up->opnum == OP_RM_NAMERULE) { 6915064Sdm199847 idmap_stat r2 = 6925064Sdm199847 idmap_namerule_cpy(&res->error_rule, 693*5696Snw141292 &up->idmap_update_op_u.rule); 6945064Sdm199847 if (r2 != IDMAP_SUCCESS) 6955064Sdm199847 res->retcode = r2; 6965064Sdm199847 } 6974520Snw141292 goto out; 6985064Sdm199847 } 6994520Snw141292 } 7004520Snw141292 7014520Snw141292 out: 7024884Sjp151216 if (trans) { 7035064Sdm199847 if (res->retcode == IDMAP_SUCCESS) { 7045064Sdm199847 res->retcode = 7055064Sdm199847 sql_exec_no_cb(db, "COMMIT TRANSACTION;"); 7065064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 7075064Sdm199847 res->error_index = -2; 7085064Sdm199847 } 7094884Sjp151216 else 7104884Sjp151216 (void) sql_exec_no_cb(db, "ROLLBACK TRANSACTION;"); 7114520Snw141292 } 7125064Sdm199847 7135064Sdm199847 res->retcode = idmap_stat4prot(res->retcode); 7145064Sdm199847 7154520Snw141292 return (TRUE); 7164520Snw141292 } 7174520Snw141292 7184520Snw141292 7194520Snw141292 /* ARGSUSED */ 7204520Snw141292 bool_t 7214520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request, 722*5696Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) 723*5696Snw141292 { 7244520Snw141292 sqlite *cache = NULL, *db = NULL; 7254520Snw141292 7264520Snw141292 /* Init */ 7274520Snw141292 (void) memset(result, 0, sizeof (*result)); 7284520Snw141292 7294520Snw141292 /* Get cache handle */ 7304520Snw141292 result->retcode = get_cache_handle(&cache); 7314520Snw141292 if (result->retcode != IDMAP_SUCCESS) 7324520Snw141292 goto out; 7334520Snw141292 7344520Snw141292 /* Get db handle */ 7354520Snw141292 result->retcode = get_db_handle(&db); 7364520Snw141292 if (result->retcode != IDMAP_SUCCESS) 7374520Snw141292 goto out; 7384520Snw141292 7394520Snw141292 /* Allocate result */ 7404520Snw141292 result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping)); 7414520Snw141292 if (result->mappings.mappings_val == NULL) { 7424520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 7434520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 7444520Snw141292 goto out; 7454520Snw141292 } 7464520Snw141292 result->mappings.mappings_len = 1; 7474520Snw141292 748*5696Snw141292 749*5696Snw141292 if (IS_REQUEST_SID(request, 1)) { 7504520Snw141292 result->retcode = get_w2u_mapping( 751*5696Snw141292 cache, 752*5696Snw141292 db, 753*5696Snw141292 &request, 754*5696Snw141292 result->mappings.mappings_val); 7554520Snw141292 } else if (IS_REQUEST_UID(request)) { 7564520Snw141292 result->retcode = get_u2w_mapping( 757*5696Snw141292 cache, 758*5696Snw141292 db, 759*5696Snw141292 &request, 760*5696Snw141292 result->mappings.mappings_val, 761*5696Snw141292 1); 7624520Snw141292 } else if (IS_REQUEST_GID(request)) { 7634520Snw141292 result->retcode = get_u2w_mapping( 764*5696Snw141292 cache, 765*5696Snw141292 db, 766*5696Snw141292 &request, 767*5696Snw141292 result->mappings.mappings_val, 768*5696Snw141292 0); 7694520Snw141292 } else { 7704520Snw141292 result->retcode = IDMAP_ERR_IDTYPE; 7714520Snw141292 } 7724520Snw141292 7734520Snw141292 out: 7744520Snw141292 if (IDMAP_FATAL_ERROR(result->retcode)) { 7754520Snw141292 xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 7764520Snw141292 result->mappings.mappings_len = 0; 7774520Snw141292 result->mappings.mappings_val = NULL; 7784520Snw141292 } 7794520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 7804520Snw141292 return (TRUE); 7814520Snw141292 } 7824520Snw141292 7834520Snw141292 7844520Snw141292 /* ARGSUSED */ 7854520Snw141292 int 7864520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, 787*5696Snw141292 caddr_t result) 788*5696Snw141292 { 7894520Snw141292 (void) xdr_free(xdr_result, result); 7904520Snw141292 return (TRUE); 7914520Snw141292 } 792