15772Sas200622 /* 25772Sas200622 * CDDL HEADER START 35772Sas200622 * 45772Sas200622 * The contents of this file are subject to the terms of the 55772Sas200622 * Common Development and Distribution License (the "License"). 65772Sas200622 * You may not use this file except in compliance with the License. 75772Sas200622 * 85772Sas200622 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95772Sas200622 * or http://www.opensolaris.org/os/licensing. 105772Sas200622 * See the License for the specific language governing permissions 115772Sas200622 * and limitations under the License. 125772Sas200622 * 135772Sas200622 * When distributing Covered Code, include this CDDL HEADER in each 145772Sas200622 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155772Sas200622 * If applicable, add the following below this CDDL HEADER, with the 165772Sas200622 * fields enclosed by brackets "[]" replaced with your own identifying 175772Sas200622 * information: Portions Copyright [yyyy] [name of copyright owner] 185772Sas200622 * 195772Sas200622 * CDDL HEADER END 205772Sas200622 */ 215772Sas200622 /* 225772Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 235772Sas200622 * Use is subject to license terms. 245772Sas200622 */ 255772Sas200622 265772Sas200622 #pragma ident "%Z%%M% %I% %E% SMI" 275772Sas200622 285772Sas200622 /* 295772Sas200622 * Server side RPC handler. 305772Sas200622 */ 315772Sas200622 325772Sas200622 #include <sys/byteorder.h> 335772Sas200622 #include <thread.h> 345772Sas200622 #include <synch.h> 355772Sas200622 #include <stdlib.h> 365772Sas200622 #include <strings.h> 375772Sas200622 #include <string.h> 385772Sas200622 #include <time.h> 395772Sas200622 405772Sas200622 #include <smbsrv/libsmb.h> 415772Sas200622 #include <smbsrv/libmlrpc.h> 425772Sas200622 #include <smbsrv/mlsvc.h> 435772Sas200622 #include <smbsrv/ndr.h> 445772Sas200622 #include <smbsrv/mlrpc.h> 455772Sas200622 #include <smbsrv/mlsvc_util.h> 465772Sas200622 #include <smbsrv/smb_winpipe.h> 475772Sas200622 485772Sas200622 /* 495772Sas200622 * Fragment size (5680: NT style). 505772Sas200622 */ 515772Sas200622 #define MLRPC_FRAG_SZ 5680 525772Sas200622 static unsigned long mlrpc_frag_size = MLRPC_FRAG_SZ; 535772Sas200622 545772Sas200622 /* 555772Sas200622 * Context table. 565772Sas200622 */ 575772Sas200622 #define CTXT_TABLE_ENTRIES 128 585772Sas200622 static struct mlsvc_rpc_context context_table[CTXT_TABLE_ENTRIES]; 595772Sas200622 static mutex_t mlrpc_context_lock; 605772Sas200622 615772Sas200622 static int mlrpc_s_process(struct mlrpc_xaction *); 625772Sas200622 static int mlrpc_s_bind(struct mlrpc_xaction *); 635772Sas200622 static int mlrpc_s_request(struct mlrpc_xaction *); 645772Sas200622 static void mlrpc_reply_prepare_hdr(struct mlrpc_xaction *); 655772Sas200622 static int mlrpc_s_alter_context(struct mlrpc_xaction *); 665772Sas200622 static void mlrpc_reply_bind_ack(struct mlrpc_xaction *); 675772Sas200622 static void mlrpc_reply_fault(struct mlrpc_xaction *, unsigned long); 685772Sas200622 static int mlrpc_build_reply(struct mlrpc_xaction *); 69*6482Samw static void mlrpc_build_frag(struct mlndr_stream *, uint8_t *, uint32_t); 705772Sas200622 715772Sas200622 /* 725772Sas200622 * This is the RPC service server-side entry point. All MSRPC encoded 735772Sas200622 * messages should be passed through here. We use the same context 745772Sas200622 * structure as the client side but we don't need to set up the client 755772Sas200622 * side info. 765772Sas200622 */ 775772Sas200622 struct mlsvc_rpc_context * 785772Sas200622 mlrpc_process(int fid, smb_dr_user_ctx_t *user_ctx) 795772Sas200622 { 805772Sas200622 struct mlsvc_rpc_context *context; 815772Sas200622 struct mlrpc_xaction *mxa; 825772Sas200622 struct mlndr_stream *recv_mlnds; 835772Sas200622 struct mlndr_stream *send_mlnds; 845772Sas200622 char *data; 855772Sas200622 int datalen; 865772Sas200622 875772Sas200622 if ((context = mlrpc_lookup(fid)) == NULL) 885772Sas200622 return (NULL); 895772Sas200622 905772Sas200622 context->user_ctx = user_ctx; 915772Sas200622 data = context->inpipe->sp_data; 925772Sas200622 datalen = context->inpipe->sp_datalen; 935772Sas200622 945772Sas200622 mxa = (struct mlrpc_xaction *)malloc(sizeof (struct mlrpc_xaction)); 955772Sas200622 if (mxa == NULL) 965772Sas200622 return (NULL); 975772Sas200622 985772Sas200622 bzero(mxa, sizeof (struct mlrpc_xaction)); 995772Sas200622 mxa->fid = fid; 1005772Sas200622 mxa->context = context; 1015772Sas200622 mxa->binding_list = context->binding; 1025772Sas200622 1035772Sas200622 if ((mxa->heap = mlrpc_heap_create()) == NULL) { 1045772Sas200622 free(mxa); 1055772Sas200622 return (NULL); 1065772Sas200622 } 1075772Sas200622 1085772Sas200622 recv_mlnds = &mxa->recv_mlnds; 1095772Sas200622 (void) mlnds_initialize(recv_mlnds, datalen, NDR_MODE_CALL_RECV, 1105772Sas200622 mxa->heap); 1115772Sas200622 1125772Sas200622 bcopy(data, recv_mlnds->pdu_base_addr, datalen); 1135772Sas200622 1145772Sas200622 send_mlnds = &mxa->send_mlnds; 1155772Sas200622 (void) mlnds_initialize(send_mlnds, 0, NDR_MODE_RETURN_SEND, mxa->heap); 1165772Sas200622 1175772Sas200622 (void) mlrpc_s_process(mxa); 1185772Sas200622 119*6482Samw context->outpipe->sp_datalen = mlnds_finalize(send_mlnds, 120*6482Samw (uint8_t *)context->outpipe->sp_data, 121*6482Samw SMB_CTXT_PIPE_SZ - sizeof (smb_pipe_t)); 1225772Sas200622 1235772Sas200622 mlnds_destruct(&mxa->recv_mlnds); 1245772Sas200622 mlnds_destruct(&mxa->send_mlnds); 1255772Sas200622 mlrpc_heap_destroy(mxa->heap); 1265772Sas200622 free(mxa); 1275772Sas200622 return (context); 1285772Sas200622 } 1295772Sas200622 1305772Sas200622 /* 1315772Sas200622 * Lookup the context for pipeid. If one exists, return a pointer to it. 1325772Sas200622 * Otherwise attempt to allocate a new context and return it. If the 1335772Sas200622 * context table is full, return a null pointer. 1345772Sas200622 */ 1355772Sas200622 struct mlsvc_rpc_context * 1365772Sas200622 mlrpc_lookup(int fid) 1375772Sas200622 { 1385772Sas200622 struct mlsvc_rpc_context *context; 1395772Sas200622 struct mlsvc_rpc_context *available = NULL; 1405772Sas200622 int i; 1415772Sas200622 1425772Sas200622 (void) mutex_lock(&mlrpc_context_lock); 1435772Sas200622 1445772Sas200622 for (i = 0; i < CTXT_TABLE_ENTRIES; ++i) { 1455772Sas200622 context = &context_table[i]; 1465772Sas200622 1475772Sas200622 if (available == NULL && context->fid == 0) { 1485772Sas200622 available = context; 1495772Sas200622 continue; 1505772Sas200622 } 1515772Sas200622 1525772Sas200622 if (context->fid == fid) { 1535772Sas200622 (void) mutex_unlock(&mlrpc_context_lock); 1545772Sas200622 return (context); 1555772Sas200622 } 1565772Sas200622 } 1575772Sas200622 1585772Sas200622 if (available) { 1595772Sas200622 bzero(available, sizeof (struct mlsvc_rpc_context)); 1606030Sjb150015 available->inpipe = malloc(SMB_CTXT_PIPE_SZ); 1616030Sjb150015 available->outpipe = malloc(SMB_CTXT_PIPE_SZ); 1625772Sas200622 1635772Sas200622 if (available->inpipe == NULL || available->outpipe == NULL) { 1645772Sas200622 free(available->inpipe); 1655772Sas200622 free(available->outpipe); 1665772Sas200622 bzero(available, sizeof (struct mlsvc_rpc_context)); 1675772Sas200622 (void) mutex_unlock(&mlrpc_context_lock); 1685772Sas200622 return (NULL); 1695772Sas200622 } 1705772Sas200622 1715772Sas200622 bzero(available->inpipe, sizeof (smb_pipe_t)); 1725772Sas200622 bzero(available->outpipe, sizeof (smb_pipe_t)); 1735772Sas200622 available->fid = fid; 1745772Sas200622 available->inpipe->sp_pipeid = fid; 1755772Sas200622 available->outpipe->sp_pipeid = fid; 1765772Sas200622 1775772Sas200622 mlrpc_binding_pool_initialize(&available->binding, 1785772Sas200622 available->binding_pool, CTXT_N_BINDING_POOL); 1795772Sas200622 } 1805772Sas200622 1815772Sas200622 (void) mutex_unlock(&mlrpc_context_lock); 1825772Sas200622 return (available); 1835772Sas200622 } 1845772Sas200622 1855772Sas200622 /* 1865772Sas200622 * This function should be called to release the context associated 1875772Sas200622 * with a fid when the client performs a close file. 1885772Sas200622 */ 1895772Sas200622 void 1905772Sas200622 mlrpc_release(int fid) 1915772Sas200622 { 1925772Sas200622 struct mlsvc_rpc_context *context; 1935772Sas200622 int i; 1945772Sas200622 1955772Sas200622 (void) mutex_lock(&mlrpc_context_lock); 1965772Sas200622 1975772Sas200622 for (i = 0; i < CTXT_TABLE_ENTRIES; ++i) { 1985772Sas200622 context = &context_table[i]; 1995772Sas200622 2005772Sas200622 if (context->fid == fid) { 2015772Sas200622 ndr_hdclose(fid); 2025772Sas200622 free(context->inpipe); 2035772Sas200622 free(context->outpipe); 2045772Sas200622 bzero(context, sizeof (struct mlsvc_rpc_context)); 2055772Sas200622 break; 2065772Sas200622 } 2075772Sas200622 } 2085772Sas200622 2095772Sas200622 (void) mutex_unlock(&mlrpc_context_lock); 2105772Sas200622 } 2115772Sas200622 2125772Sas200622 /* 2135772Sas200622 * This is the entry point for all server-side RPC processing. 2145772Sas200622 * It is assumed that the PDU has already been received. 2155772Sas200622 */ 2165772Sas200622 static int 2175772Sas200622 mlrpc_s_process(struct mlrpc_xaction *mxa) 2185772Sas200622 { 2195772Sas200622 int rc; 2205772Sas200622 2215772Sas200622 rc = mlrpc_decode_pdu_hdr(mxa); 2225772Sas200622 if (!MLRPC_DRC_IS_OK(rc)) 2235772Sas200622 return (-1); 2245772Sas200622 2255772Sas200622 (void) mlrpc_reply_prepare_hdr(mxa); 2265772Sas200622 2275772Sas200622 switch (mxa->ptype) { 2285772Sas200622 case MLRPC_PTYPE_BIND: 2295772Sas200622 rc = mlrpc_s_bind(mxa); 2305772Sas200622 break; 2315772Sas200622 2325772Sas200622 case MLRPC_PTYPE_REQUEST: 2335772Sas200622 rc = mlrpc_s_request(mxa); 2345772Sas200622 break; 2355772Sas200622 2365772Sas200622 case MLRPC_PTYPE_ALTER_CONTEXT: 2375772Sas200622 rc = mlrpc_s_alter_context(mxa); 2385772Sas200622 break; 2395772Sas200622 2405772Sas200622 default: 2415772Sas200622 rc = MLRPC_DRC_FAULT_RPCHDR_PTYPE_INVALID; 2425772Sas200622 break; 2435772Sas200622 } 2445772Sas200622 2455772Sas200622 if (MLRPC_DRC_IS_FAULT(rc)) 2465772Sas200622 mlrpc_reply_fault(mxa, rc); 2475772Sas200622 2485772Sas200622 (void) mlrpc_build_reply(mxa); 2495772Sas200622 return (rc); 2505772Sas200622 } 2515772Sas200622 2525772Sas200622 /* 2535772Sas200622 * Multiple p_cont_elem[]s, multiple transfer_syntaxes[] and multiple 2545772Sas200622 * p_results[] not supported. 2555772Sas200622 */ 2565772Sas200622 static int 2575772Sas200622 mlrpc_s_bind(struct mlrpc_xaction *mxa) 2585772Sas200622 { 2595772Sas200622 mlrpc_p_cont_list_t *cont_list; 2605772Sas200622 mlrpc_p_result_list_t *result_list; 2615772Sas200622 mlrpc_p_result_t *result; 2625772Sas200622 unsigned p_cont_id; 2635772Sas200622 struct mlrpc_binding *mbind; 2645772Sas200622 ndr_uuid_t *as_uuid; 2655772Sas200622 ndr_uuid_t *ts_uuid; 2665772Sas200622 char as_buf[64]; 2675772Sas200622 char ts_buf[64]; 2685772Sas200622 int as_vers; 2695772Sas200622 int ts_vers; 2705772Sas200622 struct mlndr_stream *send_mlnds; 2715772Sas200622 struct mlrpc_service *msvc; 2725772Sas200622 int rc; 2735772Sas200622 mlrpc_port_any_t *sec_addr; 2745772Sas200622 2755772Sas200622 /* acquire targets */ 2765772Sas200622 cont_list = &mxa->recv_hdr.bind_hdr.p_context_elem; 2775772Sas200622 result_list = &mxa->send_hdr.bind_ack_hdr.p_result_list; 2785772Sas200622 result = &result_list->p_results[0]; 2795772Sas200622 2805772Sas200622 /* 2815772Sas200622 * Set up temporary secondary address port. 2825772Sas200622 * We will correct this later (below). 2835772Sas200622 */ 2845772Sas200622 send_mlnds = &mxa->send_mlnds; 2855772Sas200622 sec_addr = &mxa->send_hdr.bind_ack_hdr.sec_addr; 2865772Sas200622 sec_addr->length = 13; 2875772Sas200622 (void) strcpy((char *)sec_addr->port_spec, "\\PIPE\\ntsvcs"); 2885772Sas200622 2895772Sas200622 result_list->n_results = 1; 2905772Sas200622 result_list->reserved = 0; 2915772Sas200622 result_list->reserved2 = 0; 2925772Sas200622 result->result = MLRPC_PCDR_ACCEPTANCE; 2935772Sas200622 result->reason = 0; 2945772Sas200622 bzero(&result->transfer_syntax, sizeof (result->transfer_syntax)); 2955772Sas200622 2965772Sas200622 /* sanity check */ 2975772Sas200622 if (cont_list->n_context_elem != 1 || 2985772Sas200622 cont_list->p_cont_elem[0].n_transfer_syn != 1) { 2995772Sas200622 mlndo_trace("mlrpc_s_bind: warning: multiple p_cont_elem"); 3005772Sas200622 } 3015772Sas200622 3025772Sas200622 p_cont_id = cont_list->p_cont_elem[0].p_cont_id; 3035772Sas200622 3045772Sas200622 if ((mbind = mlrpc_find_binding(mxa, p_cont_id)) != NULL) { 3055772Sas200622 /* 3065772Sas200622 * Duplicate p_cont_id. 3075772Sas200622 * Send a bind_ack with a better error. 3085772Sas200622 */ 3095772Sas200622 mlndo_trace("mlrpc_s_bind: duplicate binding"); 3105772Sas200622 return (MLRPC_DRC_FAULT_BIND_PCONT_BUSY); 3115772Sas200622 } 3125772Sas200622 3135772Sas200622 if ((mbind = mlrpc_new_binding(mxa)) == NULL) { 3145772Sas200622 /* 3155772Sas200622 * No free binding slot 3165772Sas200622 */ 3175772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 3185772Sas200622 result->reason = MLRPC_PPR_LOCAL_LIMIT_EXCEEDED; 3195772Sas200622 mlndo_trace("mlrpc_s_bind: no resources"); 3205772Sas200622 return (MLRPC_DRC_OK); 3215772Sas200622 } 3225772Sas200622 3235772Sas200622 as_uuid = &cont_list->p_cont_elem[0].abstract_syntax.if_uuid; 3245772Sas200622 as_vers = cont_list->p_cont_elem[0].abstract_syntax.if_version; 3255772Sas200622 3265772Sas200622 ts_uuid = &cont_list->p_cont_elem[0].transfer_syntaxes[0].if_uuid; 3275772Sas200622 ts_vers = cont_list->p_cont_elem[0].transfer_syntaxes[0].if_version; 3285772Sas200622 3295772Sas200622 msvc = mlrpc_find_service_by_uuids(as_uuid, as_vers, ts_uuid, ts_vers); 3305772Sas200622 if (!msvc) { 3315772Sas200622 mlrpc_uuid_to_str(as_uuid, as_buf); 3325772Sas200622 mlrpc_uuid_to_str(ts_uuid, ts_buf); 3335772Sas200622 3345772Sas200622 mlndo_printf(send_mlnds, 0, "mlrpc_s_bind: unknown service"); 3355772Sas200622 mlndo_printf(send_mlnds, 0, "abs=%s v%d, xfer=%s v%d", 3365772Sas200622 as_buf, as_vers, ts_buf, ts_vers); 3375772Sas200622 3385772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 3395772Sas200622 result->reason = MLRPC_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED; 3405772Sas200622 return (MLRPC_DRC_OK); 3415772Sas200622 } 3425772Sas200622 3435772Sas200622 /* 3445772Sas200622 * We can now use the correct secondary address port. 3455772Sas200622 */ 3465772Sas200622 sec_addr = &mxa->send_hdr.bind_ack_hdr.sec_addr; 3475772Sas200622 sec_addr->length = strlen(msvc->sec_addr_port) + 1; 3485772Sas200622 (void) strlcpy((char *)sec_addr->port_spec, msvc->sec_addr_port, 3495772Sas200622 MLRPC_PORT_ANY_MAX_PORT_SPEC); 3505772Sas200622 3515772Sas200622 mbind->p_cont_id = p_cont_id; 3525772Sas200622 mbind->which_side = MLRPC_BIND_SIDE_SERVER; 3535772Sas200622 /* mbind->context set by app */ 3545772Sas200622 mbind->service = msvc; 3555772Sas200622 mbind->instance_specific = 0; 3565772Sas200622 3575772Sas200622 mxa->binding = mbind; 3585772Sas200622 3595772Sas200622 if (msvc->bind_req) { 3605772Sas200622 /* 3615772Sas200622 * Call the service-specific bind() handler. If 3625772Sas200622 * this fails, we shouild send a specific error 3635772Sas200622 * on the bind ack. 3645772Sas200622 */ 3655772Sas200622 rc = (msvc->bind_req)(mxa); 3665772Sas200622 if (MLRPC_DRC_IS_FAULT(rc)) { 3675772Sas200622 mbind->service = 0; /* free binding slot */ 3685772Sas200622 mbind->which_side = 0; 3695772Sas200622 mbind->p_cont_id = 0; 3705772Sas200622 mbind->instance_specific = 0; 3715772Sas200622 return (rc); 3725772Sas200622 } 3735772Sas200622 } 3745772Sas200622 3755772Sas200622 result->transfer_syntax = 3765772Sas200622 cont_list->p_cont_elem[0].transfer_syntaxes[0]; 3775772Sas200622 3785772Sas200622 /* 3795772Sas200622 * Special rejection of Windows 2000 DSSETUP interface. 3805772Sas200622 * This interface was introduced in Windows 2000 but has 3815772Sas200622 * been subsequently deprecated due to problems. 3825772Sas200622 */ 3835772Sas200622 if (strcmp(msvc->name, "DSSETUP") == 0) { 3845772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 3855772Sas200622 result->reason = MLRPC_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED; 3865772Sas200622 } 3875772Sas200622 3885772Sas200622 return (MLRPC_DRC_BINDING_MADE); 3895772Sas200622 } 3905772Sas200622 3915772Sas200622 /* 3925772Sas200622 * mlrpc_s_alter_context 3935772Sas200622 * 3945772Sas200622 * The alter context request is used to request additional presentation 3955772Sas200622 * context for another interface and/or version. It's very similar to a 3965772Sas200622 * bind request. 3975772Sas200622 * 3985772Sas200622 * We don't fully support multiple contexts so, for now, we reject this 3995772Sas200622 * request. Windows 2000 clients attempt to use an alternate LSA context 4005772Sas200622 * when ACLs are modified. 4015772Sas200622 */ 4025772Sas200622 static int 4035772Sas200622 mlrpc_s_alter_context(struct mlrpc_xaction *mxa) 4045772Sas200622 { 4055772Sas200622 mlrpc_p_result_list_t *result_list; 4065772Sas200622 mlrpc_p_result_t *result; 4075772Sas200622 mlrpc_p_cont_list_t *cont_list; 4085772Sas200622 struct mlrpc_binding *mbind; 4095772Sas200622 struct mlrpc_service *msvc; 4105772Sas200622 unsigned p_cont_id; 4115772Sas200622 ndr_uuid_t *as_uuid; 4125772Sas200622 ndr_uuid_t *ts_uuid; 4135772Sas200622 int as_vers; 4145772Sas200622 int ts_vers; 4155772Sas200622 mlrpc_port_any_t *sec_addr; 4165772Sas200622 4175772Sas200622 result_list = &mxa->send_hdr.bind_ack_hdr.p_result_list; 4185772Sas200622 result_list->n_results = 1; 4195772Sas200622 result_list->reserved = 0; 4205772Sas200622 result_list->reserved2 = 0; 4215772Sas200622 4225772Sas200622 result = &result_list->p_results[0]; 4235772Sas200622 result->result = MLRPC_PCDR_ACCEPTANCE; 4245772Sas200622 result->reason = 0; 4255772Sas200622 bzero(&result->transfer_syntax, sizeof (result->transfer_syntax)); 4265772Sas200622 4275772Sas200622 if (mxa != NULL) { 4285772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 4295772Sas200622 result->reason = MLRPC_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED; 4305772Sas200622 return (MLRPC_DRC_OK); 4315772Sas200622 } 4325772Sas200622 4335772Sas200622 cont_list = &mxa->recv_hdr.bind_hdr.p_context_elem; 4345772Sas200622 p_cont_id = cont_list->p_cont_elem[0].p_cont_id; 4355772Sas200622 4365772Sas200622 if ((mbind = mlrpc_find_binding(mxa, p_cont_id)) != NULL) 4375772Sas200622 return (MLRPC_DRC_FAULT_BIND_PCONT_BUSY); 4385772Sas200622 4395772Sas200622 if ((mbind = mlrpc_new_binding(mxa)) == NULL) { 4405772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 4415772Sas200622 result->reason = MLRPC_PPR_LOCAL_LIMIT_EXCEEDED; 4425772Sas200622 return (MLRPC_DRC_OK); 4435772Sas200622 } 4445772Sas200622 4455772Sas200622 as_uuid = &cont_list->p_cont_elem[0].abstract_syntax.if_uuid; 4465772Sas200622 as_vers = cont_list->p_cont_elem[0].abstract_syntax.if_version; 4475772Sas200622 4485772Sas200622 ts_uuid = &cont_list->p_cont_elem[0].transfer_syntaxes[0].if_uuid; 4495772Sas200622 ts_vers = cont_list->p_cont_elem[0].transfer_syntaxes[0].if_version; 4505772Sas200622 4515772Sas200622 msvc = mlrpc_find_service_by_uuids(as_uuid, as_vers, ts_uuid, ts_vers); 4525772Sas200622 if (msvc == 0) { 4535772Sas200622 result->result = MLRPC_PCDR_PROVIDER_REJECTION; 4545772Sas200622 result->reason = MLRPC_PPR_ABSTRACT_SYNTAX_NOT_SUPPORTED; 4555772Sas200622 return (MLRPC_DRC_OK); 4565772Sas200622 } 4575772Sas200622 4585772Sas200622 mbind->p_cont_id = p_cont_id; 4595772Sas200622 mbind->which_side = MLRPC_BIND_SIDE_SERVER; 4605772Sas200622 /* mbind->context set by app */ 4615772Sas200622 mbind->service = msvc; 4625772Sas200622 mbind->instance_specific = 0; 4635772Sas200622 mxa->binding = mbind; 4645772Sas200622 4655772Sas200622 sec_addr = &mxa->send_hdr.bind_ack_hdr.sec_addr; 4665772Sas200622 sec_addr->length = 0; 4675772Sas200622 bzero(sec_addr->port_spec, MLRPC_PORT_ANY_MAX_PORT_SPEC); 4685772Sas200622 4695772Sas200622 result->transfer_syntax = 4705772Sas200622 cont_list->p_cont_elem[0].transfer_syntaxes[0]; 4715772Sas200622 4725772Sas200622 return (MLRPC_DRC_BINDING_MADE); 4735772Sas200622 } 4745772Sas200622 4755772Sas200622 static int 4765772Sas200622 mlrpc_s_request(struct mlrpc_xaction *mxa) 4775772Sas200622 { 4785772Sas200622 struct mlrpc_binding *mbind; 4795772Sas200622 struct mlrpc_service *msvc; 4805772Sas200622 unsigned p_cont_id; 4815772Sas200622 int rc; 4825772Sas200622 4835772Sas200622 mxa->opnum = mxa->recv_hdr.request_hdr.opnum; 4845772Sas200622 p_cont_id = mxa->recv_hdr.request_hdr.p_cont_id; 4855772Sas200622 4865772Sas200622 if ((mbind = mlrpc_find_binding(mxa, p_cont_id)) == NULL) 4875772Sas200622 return (MLRPC_DRC_FAULT_REQUEST_PCONT_INVALID); 4885772Sas200622 4895772Sas200622 mxa->binding = mbind; 4905772Sas200622 msvc = mbind->service; 4915772Sas200622 4925772Sas200622 /* 4935772Sas200622 * Make room for the response hdr. 4945772Sas200622 */ 4955772Sas200622 mxa->send_mlnds.pdu_scan_offset = MLRPC_RSP_HDR_SIZE; 4965772Sas200622 4975772Sas200622 if (msvc->call_stub) 4985772Sas200622 rc = (*msvc->call_stub)(mxa); 4995772Sas200622 else 5005772Sas200622 rc = mlrpc_generic_call_stub(mxa); 5015772Sas200622 5025772Sas200622 if (MLRPC_DRC_IS_FAULT(rc)) { 5035772Sas200622 mlndo_printf(0, 0, "%s[0x%02x]: 0x%04x", 5045772Sas200622 msvc->name, mxa->opnum, rc); 5055772Sas200622 } 5065772Sas200622 5075772Sas200622 return (rc); 5085772Sas200622 } 5095772Sas200622 5105772Sas200622 /* 5115772Sas200622 * The transaction and the two mlnds streams use the same heap, which 5125772Sas200622 * should already exist at this point. The heap will also be available 5135772Sas200622 * to the stub. 5145772Sas200622 */ 5155772Sas200622 int 5165772Sas200622 mlrpc_generic_call_stub(struct mlrpc_xaction *mxa) 5175772Sas200622 { 5185772Sas200622 struct mlrpc_binding *mbind = mxa->binding; 5195772Sas200622 struct mlrpc_service *msvc = mbind->service; 5205772Sas200622 struct ndr_typeinfo *intf_ti = msvc->interface_ti; 5215772Sas200622 struct mlrpc_stub_table *ste; 5225772Sas200622 int opnum = mxa->opnum; 5235772Sas200622 unsigned p_len = intf_ti->c_size_fixed_part; 5245772Sas200622 char *param; 5255772Sas200622 int rc; 5265772Sas200622 5275772Sas200622 if (mxa->heap == NULL) { 5285772Sas200622 mlndo_printf(0, 0, "%s[0x%02x]: no heap", msvc->name, opnum); 5295772Sas200622 return (MLRPC_DRC_FAULT_OUT_OF_MEMORY); 5305772Sas200622 } 5315772Sas200622 5325772Sas200622 if ((ste = mlrpc_find_stub_in_svc(msvc, opnum)) == NULL) { 5335772Sas200622 mlndo_printf(0, 0, "%s[0x%02x]: invalid opnum", 5345772Sas200622 msvc->name, opnum); 5355772Sas200622 return (MLRPC_DRC_FAULT_REQUEST_OPNUM_INVALID); 5365772Sas200622 } 5375772Sas200622 5385772Sas200622 if ((param = mlrpc_heap_malloc(mxa->heap, p_len)) == NULL) 5395772Sas200622 return (MLRPC_DRC_FAULT_OUT_OF_MEMORY); 5405772Sas200622 5415772Sas200622 bzero(param, p_len); 5425772Sas200622 5435772Sas200622 rc = mlrpc_decode_call(mxa, param); 5445772Sas200622 if (!MLRPC_DRC_IS_OK(rc)) 5455772Sas200622 return (rc); 5465772Sas200622 5475772Sas200622 rc = (*ste->func)(param, mxa); 5485772Sas200622 if (rc == MLRPC_DRC_OK) 5495772Sas200622 rc = mlrpc_encode_return(mxa, param); 5505772Sas200622 5515772Sas200622 return (rc); 5525772Sas200622 } 5535772Sas200622 5545772Sas200622 /* 5555772Sas200622 * We can perform some initial setup of the response header here. 5565772Sas200622 * We also need to cache some of the information from the bind 5575772Sas200622 * negotiation for use during subsequent RPC calls. 5585772Sas200622 */ 5595772Sas200622 static void 5605772Sas200622 mlrpc_reply_prepare_hdr(struct mlrpc_xaction *mxa) 5615772Sas200622 { 5625772Sas200622 mlrpcconn_common_header_t *rhdr = &mxa->recv_hdr.common_hdr; 5635772Sas200622 mlrpcconn_common_header_t *hdr = &mxa->send_hdr.common_hdr; 5645772Sas200622 5655772Sas200622 hdr->rpc_vers = 5; 5665772Sas200622 hdr->rpc_vers_minor = 0; 5675772Sas200622 hdr->pfc_flags = MLRPC_PFC_FIRST_FRAG + MLRPC_PFC_LAST_FRAG; 5685772Sas200622 hdr->packed_drep = rhdr->packed_drep; 5695772Sas200622 hdr->frag_length = 0; 5705772Sas200622 hdr->auth_length = 0; 5715772Sas200622 hdr->call_id = rhdr->call_id; 5725772Sas200622 #ifdef _BIG_ENDIAN 5735772Sas200622 hdr->packed_drep.intg_char_rep = MLRPC_REPLAB_CHAR_ASCII 5745772Sas200622 | MLRPC_REPLAB_INTG_BIG_ENDIAN; 5755772Sas200622 #else 5765772Sas200622 hdr->packed_drep.intg_char_rep = MLRPC_REPLAB_CHAR_ASCII 5775772Sas200622 | MLRPC_REPLAB_INTG_LITTLE_ENDIAN; 5785772Sas200622 #endif 5795772Sas200622 5805772Sas200622 switch (mxa->ptype) { 5815772Sas200622 case MLRPC_PTYPE_BIND: 5825772Sas200622 hdr->ptype = MLRPC_PTYPE_BIND_ACK; 5835772Sas200622 mxa->send_hdr.bind_ack_hdr.max_xmit_frag = 5845772Sas200622 mxa->recv_hdr.bind_hdr.max_xmit_frag; 5855772Sas200622 mxa->send_hdr.bind_ack_hdr.max_recv_frag = 5865772Sas200622 mxa->recv_hdr.bind_hdr.max_recv_frag; 5875772Sas200622 mxa->send_hdr.bind_ack_hdr.assoc_group_id = 5885772Sas200622 mxa->recv_hdr.bind_hdr.assoc_group_id; 5895772Sas200622 5905772Sas200622 if (mxa->send_hdr.bind_ack_hdr.assoc_group_id == 0) 5915772Sas200622 mxa->send_hdr.bind_ack_hdr.assoc_group_id = time(0); 5925772Sas200622 5935772Sas200622 /* 5945772Sas200622 * Save the maximum fragment sizes 5955772Sas200622 * for use with subsequent requests. 5965772Sas200622 */ 5975772Sas200622 mxa->context->max_xmit_frag = 5985772Sas200622 mxa->recv_hdr.bind_hdr.max_xmit_frag; 5995772Sas200622 6005772Sas200622 mxa->context->max_recv_frag = 6015772Sas200622 mxa->recv_hdr.bind_hdr.max_recv_frag; 6025772Sas200622 6035772Sas200622 break; 6045772Sas200622 6055772Sas200622 case MLRPC_PTYPE_REQUEST: 6065772Sas200622 hdr->ptype = MLRPC_PTYPE_RESPONSE; 6075772Sas200622 /* mxa->send_hdr.response_hdr.alloc_hint */ 6085772Sas200622 mxa->send_hdr.response_hdr.p_cont_id = 6095772Sas200622 mxa->recv_hdr.request_hdr.p_cont_id; 6105772Sas200622 mxa->send_hdr.response_hdr.cancel_count = 0; 6115772Sas200622 mxa->send_hdr.response_hdr.reserved = 0; 6125772Sas200622 break; 6135772Sas200622 6145772Sas200622 case MLRPC_PTYPE_ALTER_CONTEXT: 6155772Sas200622 hdr->ptype = MLRPC_PTYPE_ALTER_CONTEXT_RESP; 6165772Sas200622 /* 6175772Sas200622 * The max_xmit_frag, max_recv_frag 6185772Sas200622 * and assoc_group_id are ignored. 6195772Sas200622 */ 6205772Sas200622 break; 6215772Sas200622 6225772Sas200622 default: 6235772Sas200622 hdr->ptype = 0xFF; 6245772Sas200622 } 6255772Sas200622 } 6265772Sas200622 6275772Sas200622 /* 6285772Sas200622 * Finish and encode the bind acknowledge (MLRPC_PTYPE_BIND_ACK) header. 6295772Sas200622 * The frag_length is different from a regular RPC response. 6305772Sas200622 */ 6315772Sas200622 static void 6325772Sas200622 mlrpc_reply_bind_ack(struct mlrpc_xaction *mxa) 6335772Sas200622 { 6345772Sas200622 mlrpcconn_common_header_t *hdr; 6355772Sas200622 mlrpcconn_bind_ack_hdr_t *bahdr; 6365772Sas200622 6375772Sas200622 hdr = &mxa->send_hdr.common_hdr; 6385772Sas200622 bahdr = &mxa->send_hdr.bind_ack_hdr; 6395772Sas200622 hdr->frag_length = mlrpc_bind_ack_hdr_size(bahdr); 6405772Sas200622 } 6415772Sas200622 6425772Sas200622 /* 6435772Sas200622 * Signal an RPC fault. The stream is reset and we overwrite whatever 6445772Sas200622 * was in the response header with the fault information. 6455772Sas200622 */ 6465772Sas200622 static void 6475772Sas200622 mlrpc_reply_fault(struct mlrpc_xaction *mxa, unsigned long drc) 6485772Sas200622 { 6495772Sas200622 mlrpcconn_common_header_t *rhdr = &mxa->recv_hdr.common_hdr; 6505772Sas200622 mlrpcconn_common_header_t *hdr = &mxa->send_hdr.common_hdr; 6515772Sas200622 struct mlndr_stream *mlnds = &mxa->send_mlnds; 6525772Sas200622 unsigned long fault_status; 6535772Sas200622 6545772Sas200622 MLNDS_RESET(mlnds); 6555772Sas200622 6565772Sas200622 hdr->rpc_vers = 5; 6575772Sas200622 hdr->rpc_vers_minor = 0; 6585772Sas200622 hdr->pfc_flags = MLRPC_PFC_FIRST_FRAG + MLRPC_PFC_LAST_FRAG; 6595772Sas200622 hdr->packed_drep = rhdr->packed_drep; 6605772Sas200622 hdr->frag_length = sizeof (mxa->send_hdr.fault_hdr); 6615772Sas200622 hdr->auth_length = 0; 6625772Sas200622 hdr->call_id = rhdr->call_id; 6635772Sas200622 #ifdef _BIG_ENDIAN 6645772Sas200622 hdr->packed_drep.intg_char_rep = MLRPC_REPLAB_CHAR_ASCII 6655772Sas200622 | MLRPC_REPLAB_INTG_BIG_ENDIAN; 6665772Sas200622 #else 6675772Sas200622 hdr->packed_drep.intg_char_rep = MLRPC_REPLAB_CHAR_ASCII 6685772Sas200622 | MLRPC_REPLAB_INTG_LITTLE_ENDIAN; 6695772Sas200622 #endif 6705772Sas200622 6715772Sas200622 switch (drc & MLRPC_DRC_MASK_SPECIFIER) { 6725772Sas200622 case MLRPC_DRC_FAULT_OUT_OF_MEMORY: 6735772Sas200622 case MLRPC_DRC_FAULT_ENCODE_TOO_BIG: 6745772Sas200622 fault_status = MLRPC_FAULT_NCA_OUT_ARGS_TOO_BIG; 6755772Sas200622 break; 6765772Sas200622 6775772Sas200622 case MLRPC_DRC_FAULT_REQUEST_PCONT_INVALID: 6785772Sas200622 fault_status = MLRPC_FAULT_NCA_INVALID_PRES_CONTEXT_ID; 6795772Sas200622 break; 6805772Sas200622 6815772Sas200622 case MLRPC_DRC_FAULT_REQUEST_OPNUM_INVALID: 6825772Sas200622 fault_status = MLRPC_FAULT_NCA_OP_RNG_ERROR; 6835772Sas200622 break; 6845772Sas200622 6855772Sas200622 case MLRPC_DRC_FAULT_DECODE_FAILED: 6865772Sas200622 case MLRPC_DRC_FAULT_ENCODE_FAILED: 6875772Sas200622 fault_status = MLRPC_FAULT_NCA_PROTO_ERROR; 6885772Sas200622 break; 6895772Sas200622 6905772Sas200622 default: 6915772Sas200622 fault_status = MLRPC_FAULT_NCA_UNSPEC_REJECT; 6925772Sas200622 break; 6935772Sas200622 } 6945772Sas200622 6955772Sas200622 mxa->send_hdr.fault_hdr.common_hdr.ptype = MLRPC_PTYPE_FAULT; 6965772Sas200622 mxa->send_hdr.fault_hdr.status = fault_status; 6975772Sas200622 mxa->send_hdr.response_hdr.alloc_hint = hdr->frag_length; 6985772Sas200622 } 6995772Sas200622 7005772Sas200622 static int 7015772Sas200622 mlrpc_build_reply(struct mlrpc_xaction *mxa) 7025772Sas200622 { 7035772Sas200622 mlrpcconn_common_header_t *hdr = &mxa->send_hdr.common_hdr; 7045772Sas200622 struct mlndr_stream *mlnds = &mxa->send_mlnds; 705*6482Samw ndr_frag_t *frag; 706*6482Samw uint8_t *pdu_buf; 7075772Sas200622 unsigned long pdu_size; 7085772Sas200622 unsigned long frag_size; 7095772Sas200622 unsigned long pdu_data_size; 7105772Sas200622 unsigned long frag_data_size; 7115772Sas200622 7125772Sas200622 frag_size = mlrpc_frag_size; 7135772Sas200622 pdu_size = mlnds->pdu_size; 714*6482Samw pdu_buf = mlnds->pdu_base_addr; 7155772Sas200622 7165772Sas200622 if (pdu_size <= frag_size) { 7175772Sas200622 /* 7185772Sas200622 * Single fragment response. The PDU size may be zero 7195772Sas200622 * here (i.e. bind or fault response). So don't make 7205772Sas200622 * any assumptions about it until after the header is 7215772Sas200622 * encoded. 7225772Sas200622 */ 7235772Sas200622 switch (hdr->ptype) { 7245772Sas200622 case MLRPC_PTYPE_BIND_ACK: 7255772Sas200622 mlrpc_reply_bind_ack(mxa); 7265772Sas200622 break; 7275772Sas200622 7285772Sas200622 case MLRPC_PTYPE_FAULT: 7295772Sas200622 /* already setup */ 7305772Sas200622 break; 7315772Sas200622 7325772Sas200622 case MLRPC_PTYPE_RESPONSE: 7335772Sas200622 hdr->frag_length = pdu_size; 7345772Sas200622 mxa->send_hdr.response_hdr.alloc_hint = 7355772Sas200622 hdr->frag_length; 7365772Sas200622 break; 7375772Sas200622 7385772Sas200622 default: 7395772Sas200622 hdr->frag_length = pdu_size; 7405772Sas200622 break; 7415772Sas200622 } 7425772Sas200622 7435772Sas200622 mlnds->pdu_scan_offset = 0; 7445772Sas200622 (void) mlrpc_encode_pdu_hdr(mxa); 745*6482Samw pdu_size = mlnds->pdu_size; 746*6482Samw mlrpc_build_frag(mlnds, pdu_buf, pdu_size); 7475772Sas200622 return (0); 7485772Sas200622 } 7495772Sas200622 7505772Sas200622 /* 7515772Sas200622 * Multiple fragment response. 7525772Sas200622 */ 7535772Sas200622 hdr->pfc_flags = MLRPC_PFC_FIRST_FRAG; 7545772Sas200622 hdr->frag_length = frag_size; 7555772Sas200622 mxa->send_hdr.response_hdr.alloc_hint = pdu_size - MLRPC_RSP_HDR_SIZE; 7565772Sas200622 mlnds->pdu_scan_offset = 0; 7575772Sas200622 (void) mlrpc_encode_pdu_hdr(mxa); 758*6482Samw mlrpc_build_frag(mlnds, pdu_buf, frag_size); 7595772Sas200622 7605772Sas200622 /* 7615772Sas200622 * We need to update the 24-byte header in subsequent fragments. 7625772Sas200622 * 763*6482Samw * pdu_data_size: total data remaining to be handled 764*6482Samw * frag_size: total fragment size including header 765*6482Samw * frag_data_size: data in fragment 7665772Sas200622 * (i.e. frag_size - MLRPC_RSP_HDR_SIZE) 7675772Sas200622 */ 7685772Sas200622 pdu_data_size = pdu_size - MLRPC_RSP_HDR_SIZE; 7695772Sas200622 frag_data_size = frag_size - MLRPC_RSP_HDR_SIZE; 7705772Sas200622 771*6482Samw while (pdu_data_size) { 772*6482Samw mxa->send_hdr.response_hdr.alloc_hint -= frag_data_size; 773*6482Samw pdu_data_size -= frag_data_size; 774*6482Samw pdu_buf += frag_data_size; 7755772Sas200622 776*6482Samw if (pdu_data_size <= frag_data_size) { 777*6482Samw frag_data_size = pdu_data_size; 778*6482Samw frag_size = frag_data_size + MLRPC_RSP_HDR_SIZE; 779*6482Samw hdr->pfc_flags = MLRPC_PFC_LAST_FRAG; 7805772Sas200622 } else { 781*6482Samw hdr->pfc_flags = 0; 7825772Sas200622 } 7835772Sas200622 784*6482Samw hdr->frag_length = frag_size; 785*6482Samw mlnds->pdu_scan_offset = 0; 786*6482Samw mlrpc_encode_pdu_hdr(mxa); 787*6482Samw bcopy(mlnds->pdu_base_addr, pdu_buf, MLRPC_RSP_HDR_SIZE); 7885772Sas200622 789*6482Samw mlrpc_build_frag(mlnds, pdu_buf, frag_size); 7905772Sas200622 791*6482Samw if (hdr->pfc_flags & MLRPC_PFC_LAST_FRAG) 792*6482Samw break; 7935772Sas200622 } 7945772Sas200622 7955772Sas200622 return (0); 7965772Sas200622 } 797*6482Samw 798*6482Samw /* 799*6482Samw * mlrpc_build_frag 800*6482Samw * 801*6482Samw * Build an RPC PDU fragment from the specified buffer. 802*6482Samw * If malloc fails, the client will see a header/pdu inconsistency 803*6482Samw * and report an error. 804*6482Samw */ 805*6482Samw static void 806*6482Samw mlrpc_build_frag(struct mlndr_stream *mlnds, uint8_t *buf, uint32_t len) 807*6482Samw { 808*6482Samw ndr_frag_t *frag; 809*6482Samw int size = sizeof (ndr_frag_t) + len; 810*6482Samw 811*6482Samw if ((frag = (ndr_frag_t *)malloc(size)) == NULL) 812*6482Samw return; 813*6482Samw 814*6482Samw frag->next = NULL; 815*6482Samw frag->buf = (uint8_t *)frag + sizeof (ndr_frag_t); 816*6482Samw frag->len = len; 817*6482Samw bcopy(buf, frag->buf, len); 818*6482Samw 819*6482Samw if (mlnds->head == NULL) { 820*6482Samw mlnds->head = frag; 821*6482Samw mlnds->tail = frag; 822*6482Samw mlnds->nfrag = 1; 823*6482Samw } else { 824*6482Samw mlnds->tail->next = frag; 825*6482Samw mlnds->tail = frag; 826*6482Samw ++mlnds->nfrag; 827*6482Samw } 828*6482Samw } 829