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 /* 225968Snw141292 * Copyright 2008 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" 345731Sbaban #include "nldaputils.h" 354520Snw141292 #include <signal.h> 364520Snw141292 #include <thread.h> 374520Snw141292 #include <string.h> 384520Snw141292 #include <strings.h> 394520Snw141292 #include <errno.h> 404520Snw141292 #include <assert.h> 414520Snw141292 #include <sys/types.h> 424520Snw141292 #include <sys/stat.h> 434520Snw141292 #include <ucred.h> 444520Snw141292 #include <pwd.h> 454520Snw141292 #include <auth_attr.h> 464520Snw141292 #include <secdb.h> 475968Snw141292 #include <sys/u8_textprep.h> 484520Snw141292 494520Snw141292 #define _VALIDATE_LIST_CB_DATA(col, val, siz)\ 504520Snw141292 retcode = validate_list_cb_data(cb_data, argc, argv, col,\ 514520Snw141292 (uchar_t **)val, siz);\ 524520Snw141292 if (retcode == IDMAP_NEXT) {\ 534520Snw141292 result->retcode = IDMAP_NEXT;\ 544520Snw141292 return (0);\ 554520Snw141292 } else if (retcode < 0) {\ 564520Snw141292 result->retcode = retcode;\ 574520Snw141292 return (1);\ 584520Snw141292 } 594520Snw141292 60*6017Snw141292 #define PROCESS_LIST_SVC_SQL(rcode, db, dbname, sql, limit, cb, res, len)\ 61*6017Snw141292 rcode = process_list_svc_sql(db, dbname, sql, limit, cb, res);\ 624520Snw141292 if (rcode == IDMAP_ERR_BUSY)\ 634520Snw141292 res->retcode = IDMAP_ERR_BUSY;\ 644520Snw141292 else if (rcode == IDMAP_SUCCESS && len == 0)\ 654520Snw141292 res->retcode = IDMAP_ERR_NOTFOUND; 664520Snw141292 674520Snw141292 685064Sdm199847 #define STRDUP_OR_FAIL(to, from) \ 695064Sdm199847 if ((from) == NULL) \ 705064Sdm199847 to = NULL; \ 715064Sdm199847 else { \ 725064Sdm199847 if ((to = strdup(from)) == NULL) \ 735064Sdm199847 return (1); \ 745064Sdm199847 } 755064Sdm199847 764520Snw141292 /* ARGSUSED */ 774520Snw141292 bool_t 785696Snw141292 idmap_null_1_svc(void *result, struct svc_req *rqstp) 795696Snw141292 { 804520Snw141292 return (TRUE); 814520Snw141292 } 824520Snw141292 835474Sbaban /* 845474Sbaban * RPC layer allocates empty strings to replace NULL char *. 855474Sbaban * This utility function frees these empty strings. 865474Sbaban */ 875731Sbaban static 885731Sbaban void 895474Sbaban sanitize_mapping_request(idmap_mapping *req) 905474Sbaban { 915474Sbaban free(req->id1name); 925474Sbaban req->id1name = NULL; 935474Sbaban free(req->id1domain); 945474Sbaban req->id1domain = NULL; 955474Sbaban free(req->id2name); 965474Sbaban req->id2name = NULL; 975474Sbaban free(req->id2domain); 985474Sbaban req->id2domain = NULL; 995731Sbaban req->direction = _IDMAP_F_DONE; 1005474Sbaban } 1015474Sbaban 1025968Snw141292 static 1035968Snw141292 int 1045968Snw141292 validate_mapped_id_by_name_req(idmap_mapping *req) 1055968Snw141292 { 1065968Snw141292 int e; 1075968Snw141292 1085968Snw141292 if (IS_REQUEST_UID(*req) || IS_REQUEST_GID(*req)) 1095968Snw141292 return (IDMAP_SUCCESS); 1105968Snw141292 1115968Snw141292 if (IS_REQUEST_SID(*req, 1)) { 1125968Snw141292 if (!EMPTY_STRING(req->id1name) && 1135968Snw141292 u8_validate(req->id1name, strlen(req->id1name), 1145968Snw141292 NULL, U8_VALIDATE_ENTIRE, &e) < 0) 1155968Snw141292 return (IDMAP_ERR_BAD_UTF8); 1165968Snw141292 if (!EMPTY_STRING(req->id1domain) && 1175968Snw141292 u8_validate(req->id1domain, strlen(req->id1domain), 1185968Snw141292 NULL, U8_VALIDATE_ENTIRE, &e) < 0) 1195968Snw141292 return (IDMAP_ERR_BAD_UTF8); 1205968Snw141292 } 1215968Snw141292 1225968Snw141292 return (IDMAP_SUCCESS); 1235968Snw141292 } 1245968Snw141292 1255968Snw141292 static 1265968Snw141292 int 1275968Snw141292 validate_rule(idmap_namerule *rule) 1285968Snw141292 { 1295968Snw141292 int e; 1305968Snw141292 1315968Snw141292 if (!EMPTY_STRING(rule->winname) && 1325968Snw141292 u8_validate(rule->winname, strlen(rule->winname), 1335968Snw141292 NULL, U8_VALIDATE_ENTIRE, &e) < 0) 1345968Snw141292 return (IDMAP_ERR_BAD_UTF8); 1355968Snw141292 1365968Snw141292 if (!EMPTY_STRING(rule->windomain) && 1375968Snw141292 u8_validate(rule->windomain, strlen(rule->windomain), 1385968Snw141292 NULL, U8_VALIDATE_ENTIRE, &e) < 0) 1395968Snw141292 return (IDMAP_ERR_BAD_UTF8); 1405968Snw141292 1415968Snw141292 return (IDMAP_SUCCESS); 1425968Snw141292 1435968Snw141292 } 1445968Snw141292 1455968Snw141292 static 1465968Snw141292 bool_t 1475968Snw141292 validate_rules(idmap_update_batch *batch) 1485968Snw141292 { 1495968Snw141292 idmap_update_op *up; 1505968Snw141292 int i; 1515968Snw141292 1525968Snw141292 for (i = 0; i < batch->idmap_update_batch_len; i++) { 1535968Snw141292 up = &(batch->idmap_update_batch_val[i]); 1545968Snw141292 if (validate_rule(&(up->idmap_update_op_u.rule)) 1555968Snw141292 != IDMAP_SUCCESS) 1565968Snw141292 return (IDMAP_ERR_BAD_UTF8); 1575968Snw141292 } 1585968Snw141292 1595968Snw141292 return (IDMAP_SUCCESS); 1605968Snw141292 } 1615968Snw141292 1624520Snw141292 /* ARGSUSED */ 1634520Snw141292 bool_t 1644520Snw141292 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch, 1655696Snw141292 idmap_ids_res *result, struct svc_req *rqstp) 1665696Snw141292 { 1674520Snw141292 sqlite *cache = NULL, *db = NULL; 1684520Snw141292 lookup_state_t state; 1695731Sbaban idmap_retcode retcode; 1704864Sbaban uint_t i; 1714520Snw141292 1724520Snw141292 /* Init */ 1734520Snw141292 (void) memset(result, 0, sizeof (*result)); 1744520Snw141292 (void) memset(&state, 0, sizeof (state)); 1754520Snw141292 1764520Snw141292 /* Return success if nothing was requested */ 1774520Snw141292 if (batch.idmap_mapping_batch_len < 1) 1784520Snw141292 goto out; 1794520Snw141292 1804520Snw141292 /* Get cache handle */ 1814520Snw141292 result->retcode = get_cache_handle(&cache); 1824520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1834520Snw141292 goto out; 1844520Snw141292 1854520Snw141292 /* Get db handle */ 1864520Snw141292 result->retcode = get_db_handle(&db); 1874520Snw141292 if (result->retcode != IDMAP_SUCCESS) 1884520Snw141292 goto out; 1894520Snw141292 1904520Snw141292 /* Allocate result array */ 1914520Snw141292 result->ids.ids_val = calloc(batch.idmap_mapping_batch_len, 1925696Snw141292 sizeof (idmap_id_res)); 1934520Snw141292 if (result->ids.ids_val == NULL) { 1944520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 1954520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 1964520Snw141292 goto out; 1974520Snw141292 } 1984520Snw141292 result->ids.ids_len = batch.idmap_mapping_batch_len; 1994520Snw141292 2004864Sbaban /* Allocate hash table to check for duplicate sids */ 2014864Sbaban state.sid_history = calloc(batch.idmap_mapping_batch_len, 2025696Snw141292 sizeof (*state.sid_history)); 2034864Sbaban if (state.sid_history == NULL) { 2044864Sbaban idmapdlog(LOG_ERR, "Out of memory"); 2054864Sbaban result->retcode = IDMAP_ERR_MEMORY; 2064864Sbaban goto out; 2074864Sbaban } 2084864Sbaban state.sid_history_size = batch.idmap_mapping_batch_len; 2094864Sbaban for (i = 0; i < state.sid_history_size; i++) { 2104864Sbaban state.sid_history[i].key = state.sid_history_size; 2114864Sbaban state.sid_history[i].next = state.sid_history_size; 2124864Sbaban } 2134864Sbaban state.batch = &batch; 2144864Sbaban state.result = result; 2154864Sbaban 2165731Sbaban /* Get directory-based name mapping info */ 2175731Sbaban result->retcode = get_ds_namemap_type(&state); 2185731Sbaban if (result->retcode != IDMAP_SUCCESS) 2195731Sbaban goto out; 2205731Sbaban 2214520Snw141292 /* Init our 'done' flags */ 2224520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 2234520Snw141292 2244520Snw141292 /* First stage */ 2254520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 2264864Sbaban state.curpos = i; 2275474Sbaban (void) sanitize_mapping_request( 2285474Sbaban &batch.idmap_mapping_batch_val[i]); 2294520Snw141292 if (IS_BATCH_SID(batch, i)) { 2304520Snw141292 retcode = sid2pid_first_pass( 2315696Snw141292 &state, 2325696Snw141292 cache, 2335696Snw141292 &batch.idmap_mapping_batch_val[i], 2345696Snw141292 &result->ids.ids_val[i]); 2354520Snw141292 } else if (IS_BATCH_UID(batch, i)) { 2364520Snw141292 retcode = pid2sid_first_pass( 2375696Snw141292 &state, 2385696Snw141292 cache, 2395696Snw141292 &batch.idmap_mapping_batch_val[i], 2405696Snw141292 &result->ids.ids_val[i], 1, 0); 2414520Snw141292 } else if (IS_BATCH_GID(batch, i)) { 2424520Snw141292 retcode = pid2sid_first_pass( 2435696Snw141292 &state, 2445696Snw141292 cache, 2455696Snw141292 &batch.idmap_mapping_batch_val[i], 2465696Snw141292 &result->ids.ids_val[i], 0, 0); 2474520Snw141292 } else { 2484520Snw141292 result->ids.ids_val[i].retcode = IDMAP_ERR_IDTYPE; 2494520Snw141292 continue; 2504520Snw141292 } 2514520Snw141292 if (IDMAP_FATAL_ERROR(retcode)) { 2524520Snw141292 result->retcode = retcode; 2534520Snw141292 goto out; 2544520Snw141292 } 2554520Snw141292 } 2564520Snw141292 2574520Snw141292 /* Check if we are done */ 2584520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 2594520Snw141292 goto out; 2604520Snw141292 2615731Sbaban /* 2625731Sbaban * native LDAP lookups: 2635731Sbaban * If nldap or mixed mode is enabled then pid2sid mapping requests 2645731Sbaban * need to lookup native LDAP directory service by uid/gid to get 2655731Sbaban * winname and unixname. 2665731Sbaban */ 2675731Sbaban if (state.nldap_nqueries) { 2685731Sbaban retcode = nldap_lookup_batch(&state, &batch, result); 2695731Sbaban if (IDMAP_FATAL_ERROR(retcode)) { 2705731Sbaban result->retcode = retcode; 2714520Snw141292 goto out; 2724520Snw141292 } 2735731Sbaban } 2744520Snw141292 2755731Sbaban /* 2765731Sbaban * AD lookups: 2775731Sbaban * 1. The pid2sid requests in the preceding step which successfully 2785731Sbaban * retrieved winname from native LDAP objects will now need to 2795731Sbaban * lookup AD by winname to get sid. 2805731Sbaban * 2. The sid2pid requests will need to lookup AD by sid to get 2815731Sbaban * winname and unixname (AD or mixed mode). 2825731Sbaban * 3. If AD-based name mapping is enabled then pid2sid mapping 2835731Sbaban * requests need to lookup AD by unixname to get winname and sid. 2845731Sbaban */ 2855731Sbaban if (state.ad_nqueries) { 2865731Sbaban retcode = ad_lookup_batch(&state, &batch, result); 2875731Sbaban if (IDMAP_FATAL_ERROR(retcode)) { 2885731Sbaban result->retcode = retcode; 2895731Sbaban goto out; 2905731Sbaban } 2915731Sbaban } 2925731Sbaban 2935731Sbaban /* 2945731Sbaban * native LDAP lookups: 2955731Sbaban * If nldap mode is enabled then sid2pid mapping requests 2965731Sbaban * which successfully retrieved winname from AD objects in the 2975731Sbaban * preceding step, will now need to lookup native LDAP directory 2985731Sbaban * service by winname to get unixname and pid. 2995731Sbaban */ 3005731Sbaban if (state.nldap_nqueries) { 3015731Sbaban retcode = nldap_lookup_batch(&state, &batch, result); 3025731Sbaban if (IDMAP_FATAL_ERROR(retcode)) { 3035731Sbaban result->retcode = retcode; 3045731Sbaban goto out; 3055731Sbaban } 3065731Sbaban } 3075731Sbaban 3085731Sbaban /* Reset 'done' flags */ 3095731Sbaban state.sid2pid_done = state.pid2sid_done = TRUE; 3104520Snw141292 3114520Snw141292 /* Second stage */ 3124520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 3134864Sbaban state.curpos = i; 3144520Snw141292 if (IS_BATCH_SID(batch, i)) { 3154520Snw141292 retcode = sid2pid_second_pass( 3165696Snw141292 &state, 3175696Snw141292 cache, 3185696Snw141292 db, 3195696Snw141292 &batch.idmap_mapping_batch_val[i], 3205696Snw141292 &result->ids.ids_val[i]); 3215731Sbaban } else if (IS_BATCH_UID(batch, i)) { 3225731Sbaban retcode = pid2sid_second_pass( 3235731Sbaban &state, 3245731Sbaban cache, 3255731Sbaban db, 3265731Sbaban &batch.idmap_mapping_batch_val[i], 3275731Sbaban &result->ids.ids_val[i], 1); 3285731Sbaban } else if (IS_BATCH_GID(batch, i)) { 3295731Sbaban retcode = pid2sid_second_pass( 3305731Sbaban &state, 3315731Sbaban cache, 3325731Sbaban db, 3335731Sbaban &batch.idmap_mapping_batch_val[i], 3345731Sbaban &result->ids.ids_val[i], 0); 3355731Sbaban } else { 3365731Sbaban /* First stage has already set the error */ 3375731Sbaban continue; 3385731Sbaban } 3395731Sbaban if (IDMAP_FATAL_ERROR(retcode)) { 3405731Sbaban result->retcode = retcode; 3415731Sbaban goto out; 3424520Snw141292 } 3434520Snw141292 } 3444520Snw141292 3454520Snw141292 /* Check if we are done */ 3464520Snw141292 if (state.sid2pid_done == TRUE && state.pid2sid_done == TRUE) 3474520Snw141292 goto out; 3484520Snw141292 3494520Snw141292 /* Reset our 'done' flags */ 3504520Snw141292 state.sid2pid_done = state.pid2sid_done = TRUE; 3514520Snw141292 3524520Snw141292 /* Update cache in a single transaction */ 353*6017Snw141292 if (sql_exec_no_cb(cache, IDMAP_CACHENAME, "BEGIN TRANSACTION;") 354*6017Snw141292 != IDMAP_SUCCESS) 3554520Snw141292 goto out; 3564520Snw141292 3574520Snw141292 for (i = 0; i < batch.idmap_mapping_batch_len; i++) { 3584864Sbaban state.curpos = i; 3594520Snw141292 if (IS_BATCH_SID(batch, i)) { 3604520Snw141292 (void) update_cache_sid2pid( 3615696Snw141292 &state, 3625696Snw141292 cache, 3635696Snw141292 &batch.idmap_mapping_batch_val[i], 3645696Snw141292 &result->ids.ids_val[i]); 3654520Snw141292 } else if ((IS_BATCH_UID(batch, i)) || 3665696Snw141292 (IS_BATCH_GID(batch, i))) { 3674520Snw141292 (void) update_cache_pid2sid( 3685696Snw141292 &state, 3695696Snw141292 cache, 3705696Snw141292 &batch.idmap_mapping_batch_val[i], 3715696Snw141292 &result->ids.ids_val[i]); 3724520Snw141292 } 3734520Snw141292 } 3744520Snw141292 3755331Samw /* Commit if we have at least one successful update */ 3764520Snw141292 if (state.sid2pid_done == FALSE || state.pid2sid_done == FALSE) 377*6017Snw141292 (void) sql_exec_no_cb(cache, IDMAP_CACHENAME, 378*6017Snw141292 "COMMIT TRANSACTION;"); 3794520Snw141292 else 380*6017Snw141292 (void) sql_exec_no_cb(cache, IDMAP_CACHENAME, 381*6017Snw141292 "END TRANSACTION;"); 3824520Snw141292 3834520Snw141292 out: 3845731Sbaban cleanup_lookup_state(&state); 3854520Snw141292 if (IDMAP_ERROR(result->retcode)) { 3864520Snw141292 xdr_free(xdr_idmap_ids_res, (caddr_t)result); 3874520Snw141292 result->ids.ids_len = 0; 3884520Snw141292 result->ids.ids_val = NULL; 3894520Snw141292 } 3904520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 3914520Snw141292 return (TRUE); 3924520Snw141292 } 3934520Snw141292 3944520Snw141292 3954520Snw141292 /* ARGSUSED */ 3965696Snw141292 static 3975696Snw141292 int 3985696Snw141292 list_mappings_cb(void *parg, int argc, char **argv, char **colnames) 3995696Snw141292 { 4004520Snw141292 list_cb_data_t *cb_data; 4014520Snw141292 char *str; 4024520Snw141292 idmap_mappings_res *result; 4034520Snw141292 idmap_retcode retcode; 4044520Snw141292 int w2u, u2w; 4054520Snw141292 char *end; 4065696Snw141292 static int validated_column_names = 0; 4075696Snw141292 4085696Snw141292 if (!validated_column_names) { 4095696Snw141292 assert(strcmp(colnames[0], "rowid") == 0); 4105696Snw141292 assert(strcmp(colnames[1], "sidprefix") == 0); 4115696Snw141292 assert(strcmp(colnames[2], "rid") == 0); 4125696Snw141292 assert(strcmp(colnames[3], "pid") == 0); 4135696Snw141292 assert(strcmp(colnames[4], "w2u") == 0); 4145696Snw141292 assert(strcmp(colnames[5], "u2w") == 0); 4155696Snw141292 assert(strcmp(colnames[6], "windomain") == 0); 4165696Snw141292 assert(strcmp(colnames[7], "canon_winname") == 0); 4175696Snw141292 assert(strcmp(colnames[8], "unixname") == 0); 4185696Snw141292 assert(strcmp(colnames[9], "is_user") == 0); 4195696Snw141292 assert(strcmp(colnames[10], "is_wuser") == 0); 4205696Snw141292 validated_column_names = 1; 4215696Snw141292 } 4225696Snw141292 4234520Snw141292 4244520Snw141292 cb_data = (list_cb_data_t *)parg; 4254520Snw141292 result = (idmap_mappings_res *)cb_data->result; 4264520Snw141292 4275696Snw141292 _VALIDATE_LIST_CB_DATA(11, &result->mappings.mappings_val, 4285696Snw141292 sizeof (idmap_mapping)); 4294520Snw141292 4304520Snw141292 result->mappings.mappings_len++; 4314520Snw141292 4324520Snw141292 if ((str = strdup(argv[1])) == NULL) 4334520Snw141292 return (1); 4344520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.prefix = 4355696Snw141292 str; 4364520Snw141292 result->mappings.mappings_val[cb_data->next].id1.idmap_id_u.sid.rid = 4375696Snw141292 strtoul(argv[2], &end, 10); 4385696Snw141292 result->mappings.mappings_val[cb_data->next].id1.idtype = 4395696Snw141292 strtol(argv[10], &end, 10) ? IDMAP_USID : IDMAP_GSID; 4404520Snw141292 4414520Snw141292 result->mappings.mappings_val[cb_data->next].id2.idmap_id_u.uid = 4425696Snw141292 strtoul(argv[3], &end, 10); 4435696Snw141292 result->mappings.mappings_val[cb_data->next].id2.idtype = 4445696Snw141292 strtol(argv[9], &end, 10) ? IDMAP_UID : IDMAP_GID; 4454520Snw141292 4465696Snw141292 w2u = argv[4] ? strtol(argv[4], &end, 10) : 0; 4475696Snw141292 u2w = argv[5] ? strtol(argv[5], &end, 10) : 0; 4484520Snw141292 4494520Snw141292 if (w2u > 0 && u2w == 0) 4504644Sbaban result->mappings.mappings_val[cb_data->next].direction = 4514644Sbaban IDMAP_DIRECTION_W2U; 4524520Snw141292 else if (w2u == 0 && u2w > 0) 4534644Sbaban result->mappings.mappings_val[cb_data->next].direction = 4544644Sbaban IDMAP_DIRECTION_U2W; 4554520Snw141292 else 4564644Sbaban result->mappings.mappings_val[cb_data->next].direction = 4574644Sbaban IDMAP_DIRECTION_BI; 4584520Snw141292 4595064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1domain, 4605064Sdm199847 argv[6]); 4614520Snw141292 4625064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id1name, 4635064Sdm199847 argv[7]); 4644520Snw141292 4655064Sdm199847 STRDUP_OR_FAIL(result->mappings.mappings_val[cb_data->next].id2name, 4665064Sdm199847 argv[8]); 4675064Sdm199847 4684520Snw141292 4694520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 4704520Snw141292 cb_data->next++; 4714520Snw141292 result->retcode = IDMAP_SUCCESS; 4724520Snw141292 return (0); 4734520Snw141292 } 4744520Snw141292 4754520Snw141292 4764520Snw141292 /* ARGSUSED */ 4774520Snw141292 bool_t 4785696Snw141292 idmap_list_mappings_1_svc(int64_t lastrowid, uint64_t limit, 4795696Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) 4805696Snw141292 { 4814520Snw141292 sqlite *cache = NULL; 4824520Snw141292 char lbuf[30], rbuf[30]; 4834520Snw141292 uint64_t maxlimit; 4844520Snw141292 idmap_retcode retcode; 4854520Snw141292 char *sql = NULL; 4864520Snw141292 4874520Snw141292 (void) memset(result, 0, sizeof (*result)); 4884520Snw141292 lbuf[0] = rbuf[0] = 0; 4894520Snw141292 4904520Snw141292 RDLOCK_CONFIG(); 4914520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 4924520Snw141292 UNLOCK_CONFIG(); 4934520Snw141292 4944520Snw141292 /* Get cache handle */ 4954520Snw141292 result->retcode = get_cache_handle(&cache); 4964520Snw141292 if (result->retcode != IDMAP_SUCCESS) 4974520Snw141292 goto out; 4984520Snw141292 4994520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 5004520Snw141292 5014520Snw141292 /* Create LIMIT expression. */ 5024520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 5034520Snw141292 limit = maxlimit; 5044520Snw141292 if (limit > 0) 5054520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 5065696Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 5074520Snw141292 5084520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 5094520Snw141292 5104520Snw141292 /* 5114520Snw141292 * Combine all the above into a giant SELECT statement that 5124520Snw141292 * will return the requested mappings 5134520Snw141292 */ 5145696Snw141292 sql = sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, u2w, " 5155696Snw141292 "windomain, canon_winname, unixname, is_user, is_wuser " 5165696Snw141292 " FROM idmap_cache WHERE " 5175696Snw141292 " %s %s;", 5185696Snw141292 rbuf, lbuf); 5194520Snw141292 if (sql == NULL) { 5204520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 5214520Snw141292 goto out; 5224520Snw141292 } 5234520Snw141292 5244520Snw141292 /* Execute the SQL statement and update the return buffer */ 525*6017Snw141292 PROCESS_LIST_SVC_SQL(retcode, cache, IDMAP_CACHENAME, sql, limit, 526*6017Snw141292 list_mappings_cb, result, result->mappings.mappings_len); 5274520Snw141292 5284520Snw141292 out: 5294520Snw141292 if (sql) 5304520Snw141292 sqlite_freemem(sql); 5314520Snw141292 if (IDMAP_ERROR(result->retcode)) 5324520Snw141292 (void) xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 5334520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 5344520Snw141292 return (TRUE); 5354520Snw141292 } 5364520Snw141292 5374520Snw141292 5384520Snw141292 /* ARGSUSED */ 5395696Snw141292 static 5405696Snw141292 int 5415696Snw141292 list_namerules_cb(void *parg, int argc, char **argv, char **colnames) 5425696Snw141292 { 5434520Snw141292 list_cb_data_t *cb_data; 5444520Snw141292 idmap_namerules_res *result; 5454520Snw141292 idmap_retcode retcode; 5464520Snw141292 int w2u_order, u2w_order; 5474520Snw141292 char *end; 5485696Snw141292 static int validated_column_names = 0; 5495696Snw141292 5505696Snw141292 if (!validated_column_names) { 5515696Snw141292 assert(strcmp(colnames[0], "rowid") == 0); 5525696Snw141292 assert(strcmp(colnames[1], "is_user") == 0); 5535696Snw141292 assert(strcmp(colnames[2], "is_wuser") == 0); 5545696Snw141292 assert(strcmp(colnames[3], "windomain") == 0); 5555696Snw141292 assert(strcmp(colnames[4], "winname_display") == 0); 5565696Snw141292 assert(strcmp(colnames[5], "is_nt4") == 0); 5575696Snw141292 assert(strcmp(colnames[6], "unixname") == 0); 5585696Snw141292 assert(strcmp(colnames[7], "w2u_order") == 0); 5595696Snw141292 assert(strcmp(colnames[8], "u2w_order") == 0); 5605696Snw141292 validated_column_names = 1; 5615696Snw141292 } 5624520Snw141292 5634520Snw141292 cb_data = (list_cb_data_t *)parg; 5644520Snw141292 result = (idmap_namerules_res *)cb_data->result; 5654520Snw141292 5665696Snw141292 _VALIDATE_LIST_CB_DATA(9, &result->rules.rules_val, 5675696Snw141292 sizeof (idmap_namerule)); 5684520Snw141292 5694520Snw141292 result->rules.rules_len++; 5704520Snw141292 5714520Snw141292 result->rules.rules_val[cb_data->next].is_user = 5725696Snw141292 strtol(argv[1], &end, 10); 5735696Snw141292 5745696Snw141292 result->rules.rules_val[cb_data->next].is_wuser = 5755696Snw141292 strtol(argv[2], &end, 10); 5764520Snw141292 5775064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].windomain, 5785064Sdm199847 argv[3]); 5794520Snw141292 5805696Snw141292 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].winname, 5815696Snw141292 argv[4]); 5825696Snw141292 5834520Snw141292 result->rules.rules_val[cb_data->next].is_nt4 = 5845696Snw141292 strtol(argv[5], &end, 10); 5854520Snw141292 5865064Sdm199847 STRDUP_OR_FAIL(result->rules.rules_val[cb_data->next].unixname, 5875696Snw141292 argv[6]); 5884520Snw141292 5895696Snw141292 w2u_order = argv[7] ? strtol(argv[7], &end, 10) : 0; 5905696Snw141292 u2w_order = argv[8] ? strtol(argv[8], &end, 10) : 0; 5914520Snw141292 5924520Snw141292 if (w2u_order > 0 && u2w_order == 0) 5934644Sbaban result->rules.rules_val[cb_data->next].direction = 5944644Sbaban IDMAP_DIRECTION_W2U; 5954520Snw141292 else if (w2u_order == 0 && u2w_order > 0) 5964644Sbaban result->rules.rules_val[cb_data->next].direction = 5974644Sbaban IDMAP_DIRECTION_U2W; 5984520Snw141292 else 5994644Sbaban result->rules.rules_val[cb_data->next].direction = 6004644Sbaban IDMAP_DIRECTION_BI; 6014520Snw141292 6024520Snw141292 result->lastrowid = strtoll(argv[0], &end, 10); 6034520Snw141292 cb_data->next++; 6044520Snw141292 result->retcode = IDMAP_SUCCESS; 6054520Snw141292 return (0); 6064520Snw141292 } 6074520Snw141292 6084520Snw141292 6094520Snw141292 /* ARGSUSED */ 6104520Snw141292 bool_t 6114520Snw141292 idmap_list_namerules_1_svc(idmap_namerule rule, uint64_t lastrowid, 6124520Snw141292 uint64_t limit, idmap_namerules_res *result, 6135696Snw141292 struct svc_req *rqstp) 6145696Snw141292 { 6154520Snw141292 6164520Snw141292 sqlite *db = NULL; 6174520Snw141292 char w2ubuf[15], u2wbuf[15]; 6184520Snw141292 char lbuf[30], rbuf[30]; 6194520Snw141292 char *sql = NULL; 6205696Snw141292 char *expr = NULL; 6214520Snw141292 uint64_t maxlimit; 6224520Snw141292 idmap_retcode retcode; 6234520Snw141292 6244520Snw141292 (void) memset(result, 0, sizeof (*result)); 6254520Snw141292 lbuf[0] = rbuf[0] = 0; 6264520Snw141292 6275968Snw141292 result->retcode = validate_rule(&rule); 6285968Snw141292 if (result->retcode != IDMAP_SUCCESS) 6295968Snw141292 goto out; 6305968Snw141292 6314520Snw141292 RDLOCK_CONFIG(); 6324520Snw141292 maxlimit = _idmapdstate.cfg->pgcfg.list_size_limit; 6334520Snw141292 UNLOCK_CONFIG(); 6344520Snw141292 6354520Snw141292 /* Get db handle */ 6364520Snw141292 result->retcode = get_db_handle(&db); 6374520Snw141292 if (result->retcode != IDMAP_SUCCESS) 6384520Snw141292 goto out; 6394520Snw141292 6404520Snw141292 result->retcode = IDMAP_ERR_INTERNAL; 6414520Snw141292 6425696Snw141292 w2ubuf[0] = u2wbuf[0] = 0; 6435696Snw141292 if (rule.direction == IDMAP_DIRECTION_BI) { 6444520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 6454520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 6464644Sbaban } else if (rule.direction == IDMAP_DIRECTION_W2U) { 6474520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), "AND w2u_order > 0"); 6484520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), 6495696Snw141292 "AND (u2w_order = 0 OR u2w_order ISNULL)"); 6504644Sbaban } else if (rule.direction == IDMAP_DIRECTION_U2W) { 6514520Snw141292 (void) snprintf(w2ubuf, sizeof (w2ubuf), 6525696Snw141292 "AND (w2u_order = 0 OR w2u_order ISNULL)"); 6534520Snw141292 (void) snprintf(u2wbuf, sizeof (u2wbuf), "AND u2w_order > 0"); 6544520Snw141292 } 6554520Snw141292 6565968Snw141292 result->retcode = gen_sql_expr_from_rule(&rule, &expr); 6575968Snw141292 if (result->retcode != IDMAP_SUCCESS) 6585696Snw141292 goto out; 6594520Snw141292 6604520Snw141292 /* Create LIMIT expression. */ 6614520Snw141292 if (limit == 0 || (maxlimit > 0 && maxlimit < limit)) 6624520Snw141292 limit = maxlimit; 6634520Snw141292 if (limit > 0) 6644520Snw141292 (void) snprintf(lbuf, sizeof (lbuf), 6655696Snw141292 "LIMIT %" PRIu64, limit + 1ULL); 6664520Snw141292 6674520Snw141292 (void) snprintf(rbuf, sizeof (rbuf), "rowid > %" PRIu64, lastrowid); 6684520Snw141292 6694520Snw141292 /* 6704520Snw141292 * Combine all the above into a giant SELECT statement that 6714520Snw141292 * will return the requested rules 6724520Snw141292 */ 6735696Snw141292 sql = sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, " 6745696Snw141292 "winname_display, is_nt4, unixname, w2u_order, u2w_order " 6755696Snw141292 "FROM namerules WHERE " 6765696Snw141292 " %s %s %s %s %s;", 6775696Snw141292 rbuf, expr, w2ubuf, u2wbuf, lbuf); 6785696Snw141292 6794520Snw141292 if (sql == NULL) { 6804520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 6814520Snw141292 goto out; 6824520Snw141292 } 6834520Snw141292 6844520Snw141292 /* Execute the SQL statement and update the return buffer */ 685*6017Snw141292 PROCESS_LIST_SVC_SQL(retcode, db, IDMAP_DBNAME, sql, limit, 686*6017Snw141292 list_namerules_cb, result, result->rules.rules_len); 6874520Snw141292 6884520Snw141292 out: 6895696Snw141292 if (expr) 6905696Snw141292 sqlite_freemem(expr); 6914520Snw141292 if (sql) 6924520Snw141292 sqlite_freemem(sql); 6934520Snw141292 if (IDMAP_ERROR(result->retcode)) 6944520Snw141292 (void) xdr_free(xdr_idmap_namerules_res, (caddr_t)result); 6954520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 6964520Snw141292 return (TRUE); 6974520Snw141292 } 6984520Snw141292 6994520Snw141292 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules" 7004520Snw141292 static int 7015696Snw141292 verify_rules_auth(struct svc_req *rqstp) 7025696Snw141292 { 7034520Snw141292 ucred_t *uc = NULL; 7044520Snw141292 uid_t uid; 7054520Snw141292 char buf[1024]; 7064520Snw141292 struct passwd pwd; 7074520Snw141292 7084520Snw141292 if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) { 709*6017Snw141292 idmapdlog(LOG_ERR, "svc_getcallerucred failed during " 710*6017Snw141292 "authorization (%s)", strerror(errno)); 7114520Snw141292 return (-1); 7124520Snw141292 } 7134520Snw141292 7144520Snw141292 uid = ucred_geteuid(uc); 7154520Snw141292 if (uid == (uid_t)-1) { 716*6017Snw141292 idmapdlog(LOG_ERR, "ucred_geteuid failed during " 717*6017Snw141292 "authorization (%s)", strerror(errno)); 7184520Snw141292 ucred_free(uc); 7194520Snw141292 return (-1); 7204520Snw141292 } 7214520Snw141292 7224520Snw141292 if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL) { 723*6017Snw141292 idmapdlog(LOG_ERR, "getpwuid_r(%u) failed during " 724*6017Snw141292 "authorization (%s)", uid, strerror(errno)); 7254520Snw141292 ucred_free(uc); 7264520Snw141292 return (-1); 7274520Snw141292 } 7284520Snw141292 7294520Snw141292 if (chkauthattr(IDMAP_RULES_AUTH, pwd.pw_name) != 1) { 730*6017Snw141292 idmapdlog(LOG_INFO, "%s is not authorized (%s)", 731*6017Snw141292 pwd.pw_name, IDMAP_RULES_AUTH); 7324520Snw141292 ucred_free(uc); 7334520Snw141292 return (-1); 7344520Snw141292 } 7354520Snw141292 7364520Snw141292 ucred_free(uc); 7374520Snw141292 return (1); 7384520Snw141292 } 7394520Snw141292 7405064Sdm199847 /* 7415064Sdm199847 * Meaning of the return values is the following: For retcode == 7425064Sdm199847 * IDMAP_SUCCESS, everything went OK and error_index is 7435064Sdm199847 * undefined. Otherwise, error_index >=0 shows the failed batch 7445064Sdm199847 * element. errro_index == -1 indicates failure at the beginning, 7455064Sdm199847 * error_index == -2 at the end. 7465064Sdm199847 */ 7475064Sdm199847 7484520Snw141292 /* ARGSUSED */ 7494520Snw141292 bool_t 7505064Sdm199847 idmap_update_1_svc(idmap_update_batch batch, idmap_update_res *res, 7515696Snw141292 struct svc_req *rqstp) 7525696Snw141292 { 7534520Snw141292 sqlite *db = NULL; 7544520Snw141292 idmap_update_op *up; 7554520Snw141292 int i; 7564884Sjp151216 int trans = FALSE; 7574520Snw141292 7585064Sdm199847 res->error_index = -1; 7595064Sdm199847 (void) memset(&res->error_rule, 0, sizeof (res->error_rule)); 7605064Sdm199847 (void) memset(&res->conflict_rule, 0, sizeof (res->conflict_rule)); 7615064Sdm199847 7624520Snw141292 if (verify_rules_auth(rqstp) < 0) { 7635064Sdm199847 res->retcode = IDMAP_ERR_PERMISSION_DENIED; 7644520Snw141292 goto out; 7654520Snw141292 } 7664520Snw141292 7674520Snw141292 if (batch.idmap_update_batch_len == 0 || 7685696Snw141292 batch.idmap_update_batch_val == NULL) { 7695064Sdm199847 res->retcode = IDMAP_SUCCESS; 7704520Snw141292 goto out; 7714520Snw141292 } 7724520Snw141292 7735968Snw141292 res->retcode = validate_rules(&batch); 7745968Snw141292 if (res->retcode != IDMAP_SUCCESS) 7755968Snw141292 goto out; 7765968Snw141292 7774520Snw141292 /* Get db handle */ 7785064Sdm199847 res->retcode = get_db_handle(&db); 7795064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 7804520Snw141292 goto out; 7814520Snw141292 782*6017Snw141292 res->retcode = sql_exec_no_cb(db, IDMAP_DBNAME, "BEGIN TRANSACTION;"); 7835064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 7844520Snw141292 goto out; 7854884Sjp151216 trans = TRUE; 7864520Snw141292 7874520Snw141292 for (i = 0; i < batch.idmap_update_batch_len; i++) { 7884520Snw141292 up = &batch.idmap_update_batch_val[i]; 7894520Snw141292 switch (up->opnum) { 7904520Snw141292 case OP_NONE: 7915064Sdm199847 res->retcode = IDMAP_SUCCESS; 7924520Snw141292 break; 7934520Snw141292 case OP_ADD_NAMERULE: 7945064Sdm199847 res->retcode = add_namerule(db, 7955696Snw141292 &up->idmap_update_op_u.rule); 7964520Snw141292 break; 7974520Snw141292 case OP_RM_NAMERULE: 7985064Sdm199847 res->retcode = rm_namerule(db, 7995696Snw141292 &up->idmap_update_op_u.rule); 8004520Snw141292 break; 8014520Snw141292 case OP_FLUSH_NAMERULES: 8025696Snw141292 res->retcode = flush_namerules(db); 8034520Snw141292 break; 8044520Snw141292 default: 8055064Sdm199847 res->retcode = IDMAP_ERR_NOTSUPPORTED; 8065064Sdm199847 break; 8074520Snw141292 }; 8084520Snw141292 8095064Sdm199847 if (res->retcode != IDMAP_SUCCESS) { 8105064Sdm199847 res->error_index = i; 8115064Sdm199847 if (up->opnum == OP_ADD_NAMERULE || 8125064Sdm199847 up->opnum == OP_RM_NAMERULE) { 8135064Sdm199847 idmap_stat r2 = 8145064Sdm199847 idmap_namerule_cpy(&res->error_rule, 8155696Snw141292 &up->idmap_update_op_u.rule); 8165064Sdm199847 if (r2 != IDMAP_SUCCESS) 8175064Sdm199847 res->retcode = r2; 8185064Sdm199847 } 8194520Snw141292 goto out; 8205064Sdm199847 } 8214520Snw141292 } 8224520Snw141292 8234520Snw141292 out: 8244884Sjp151216 if (trans) { 8255064Sdm199847 if (res->retcode == IDMAP_SUCCESS) { 8265064Sdm199847 res->retcode = 827*6017Snw141292 sql_exec_no_cb(db, IDMAP_DBNAME, 828*6017Snw141292 "COMMIT TRANSACTION;"); 8295064Sdm199847 if (res->retcode != IDMAP_SUCCESS) 8305064Sdm199847 res->error_index = -2; 8315064Sdm199847 } 8324884Sjp151216 else 833*6017Snw141292 (void) sql_exec_no_cb(db, IDMAP_DBNAME, 834*6017Snw141292 "ROLLBACK TRANSACTION;"); 8354520Snw141292 } 8365064Sdm199847 8375064Sdm199847 res->retcode = idmap_stat4prot(res->retcode); 8385064Sdm199847 8394520Snw141292 return (TRUE); 8404520Snw141292 } 8414520Snw141292 8424520Snw141292 8434520Snw141292 /* ARGSUSED */ 8444520Snw141292 bool_t 8454520Snw141292 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request, 8465696Snw141292 idmap_mappings_res *result, struct svc_req *rqstp) 8475696Snw141292 { 8484520Snw141292 sqlite *cache = NULL, *db = NULL; 8494520Snw141292 8504520Snw141292 /* Init */ 8514520Snw141292 (void) memset(result, 0, sizeof (*result)); 8524520Snw141292 8535968Snw141292 result->retcode = validate_mapped_id_by_name_req(&request); 8545968Snw141292 if (result->retcode != IDMAP_SUCCESS) 8555968Snw141292 goto out; 8565968Snw141292 8574520Snw141292 /* Get cache handle */ 8584520Snw141292 result->retcode = get_cache_handle(&cache); 8594520Snw141292 if (result->retcode != IDMAP_SUCCESS) 8604520Snw141292 goto out; 8614520Snw141292 8624520Snw141292 /* Get db handle */ 8634520Snw141292 result->retcode = get_db_handle(&db); 8644520Snw141292 if (result->retcode != IDMAP_SUCCESS) 8654520Snw141292 goto out; 8664520Snw141292 8674520Snw141292 /* Allocate result */ 8684520Snw141292 result->mappings.mappings_val = calloc(1, sizeof (idmap_mapping)); 8694520Snw141292 if (result->mappings.mappings_val == NULL) { 8704520Snw141292 idmapdlog(LOG_ERR, "Out of memory"); 8714520Snw141292 result->retcode = IDMAP_ERR_MEMORY; 8724520Snw141292 goto out; 8734520Snw141292 } 8744520Snw141292 result->mappings.mappings_len = 1; 8754520Snw141292 8765696Snw141292 8775696Snw141292 if (IS_REQUEST_SID(request, 1)) { 8784520Snw141292 result->retcode = get_w2u_mapping( 8795696Snw141292 cache, 8805696Snw141292 db, 8815696Snw141292 &request, 8825696Snw141292 result->mappings.mappings_val); 8834520Snw141292 } else if (IS_REQUEST_UID(request)) { 8844520Snw141292 result->retcode = get_u2w_mapping( 8855696Snw141292 cache, 8865696Snw141292 db, 8875696Snw141292 &request, 8885696Snw141292 result->mappings.mappings_val, 8895696Snw141292 1); 8904520Snw141292 } else if (IS_REQUEST_GID(request)) { 8914520Snw141292 result->retcode = get_u2w_mapping( 8925696Snw141292 cache, 8935696Snw141292 db, 8945696Snw141292 &request, 8955696Snw141292 result->mappings.mappings_val, 8965696Snw141292 0); 8974520Snw141292 } else { 8984520Snw141292 result->retcode = IDMAP_ERR_IDTYPE; 8994520Snw141292 } 9004520Snw141292 9014520Snw141292 out: 9024520Snw141292 if (IDMAP_FATAL_ERROR(result->retcode)) { 9034520Snw141292 xdr_free(xdr_idmap_mappings_res, (caddr_t)result); 9044520Snw141292 result->mappings.mappings_len = 0; 9054520Snw141292 result->mappings.mappings_val = NULL; 9064520Snw141292 } 9074520Snw141292 result->retcode = idmap_stat4prot(result->retcode); 9084520Snw141292 return (TRUE); 9094520Snw141292 } 9104520Snw141292 9114520Snw141292 9124520Snw141292 /* ARGSUSED */ 9134520Snw141292 int 9144520Snw141292 idmap_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, 9155696Snw141292 caddr_t result) 9165696Snw141292 { 9174520Snw141292 (void) xdr_free(xdr_result, result); 9184520Snw141292 return (TRUE); 9194520Snw141292 } 920