librpc/rpc: add dcerpc_binding_handle_raw_call_in_*
[metze/samba/wip.git] / librpc / rpc / rpc_common.h
index aab7c1001d2994c769af61a9822fb59a0d2a36ed..8ea2ee344f107efe24bf4a97b7ded3a8957baf80 100644 (file)
 #ifndef __DEFAULT_LIBRPC_RPCCOMMON_H__
 #define __DEFAULT_LIBRPC_RPCCOMMON_H__
 
+#include "gen_ndr/dcerpc.h"
+#include "lib/util/attr.h"
+
 struct dcerpc_binding_handle;
+struct dcerpc_pipe_handle;
+struct dcerpc_pipe_handle_connection;
 struct GUID;
 struct ndr_interface_table;
 struct ndr_interface_call;
@@ -33,6 +38,7 @@ struct epm_floor;
 struct epm_tower;
 struct tevent_context;
 struct tstream_context;
+struct gensec_security;
 
 enum dcerpc_transport_t {
        NCA_UNKNOWN, NCACN_NP, NCACN_IP_TCP, NCACN_IP_UDP, NCACN_VNS_IPC, 
@@ -40,18 +46,7 @@ enum dcerpc_transport_t {
        NCADG_UNIX_DGRAM, NCACN_HTTP, NCADG_IPX, NCACN_SPX, NCACN_INTERNAL };
 
 /** this describes a binding to a particular transport/pipe */
-struct dcerpc_binding {
-       enum dcerpc_transport_t transport;
-       struct ndr_syntax_id object;
-       const char *host;
-       const char *target_hostname;
-       const char *target_principal;
-       const char *endpoint;
-       const char **options;
-       const char *localaddress;
-       uint32_t flags;
-       uint32_t assoc_group_id;
-};
+struct dcerpc_binding;
 
 /* dcerpc pipe flags */
 #define DCERPC_DEBUG_PRINT_IN          (1<<0)
@@ -104,9 +99,6 @@ struct dcerpc_binding {
 /* use NDR64 transport */
 #define DCERPC_NDR64                   (1<<21)
 
-/* specify binding interface */
-#define        DCERPC_LOCALADDRESS            (1<<22)
-
 /* handle upgrades or downgrades automatically */
 #define DCERPC_SCHANNEL_AUTO           (1<<23)
 
@@ -116,15 +108,20 @@ struct dcerpc_binding {
 /* this triggers the DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag in the bind request */
 #define DCERPC_PROPOSE_HEADER_SIGNING          (1<<25)
 
+#define DCERPC_PACKET                  (1<<26)
+
+#define DCERPC_SMB1                    (1<<27)
+
 /* The following definitions come from ../librpc/rpc/dcerpc_error.c  */
 
 const char *dcerpc_errstr(TALLOC_CTX *mem_ctx, uint32_t fault_code);
 NTSTATUS dcerpc_fault_to_nt_status(uint32_t fault_code);
+uint32_t dcerpc_fault_from_nt_status(NTSTATUS nt_status);
 
 /* The following definitions come from ../librpc/rpc/binding.c  */
 
 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
-const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
+char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot);
 struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
                                          const struct dcerpc_binding *b);
@@ -136,8 +133,47 @@ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
                                   struct dcerpc_binding **b_out);
 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out);
 char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b);
+struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b);
+NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
+                                  struct GUID object);
+enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b);
+NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
+                                     enum dcerpc_transport_t transport);
+void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
+                                 enum dcerpc_AuthType *_auth_type,
+                                 enum dcerpc_AuthLevel *_auth_level);
+uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b);
+NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
+                                          uint32_t assoc_group_id);
+struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b);
+NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
+                                           const struct ndr_syntax_id *syntax);
+const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
+                                            const char *name);
+char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
+                                       const struct dcerpc_binding *b,
+                                       const char *name);
+NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
+                                         const char *name,
+                                         const char *value);
+void *_dcerpc_binding_get_pointer_option(const struct dcerpc_binding *b,
+                                        const char *name,
+                                        const char *type);
+#define dcerpc_binding_get_pointer_option(b, name, type) \
+       talloc_get_type(_dcerpc_binding_get_pointer_option(b, name, #type), type)
+NTSTATUS _dcerpc_binding_set_pointer_option(struct dcerpc_binding *b,
+                                           const char *name,
+                                           const char *type,
+                                           const void *vp);
+#define dcerpc_binding_set_pointer_option(b, name, type, vp) \
+       _dcerpc_binding_set_pointer_option(b, name, #type, vp)
+uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b);
+NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
+                                 uint32_t additional,
+                                 uint32_t clear);
 NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor, struct ndr_syntax_id *syntax);
 const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t);
+enum dcerpc_transport_t dcerpc_transport_by_name(const char *name);
 enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower);
 
 /* The following definitions come from ../librpc/rpc/dcerpc_util.c  */
@@ -145,11 +181,19 @@ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
 void dcerpc_set_frag_length(DATA_BLOB *blob, uint16_t v);
 uint16_t dcerpc_get_frag_length(const DATA_BLOB *blob);
 void dcerpc_set_auth_length(DATA_BLOB *blob, uint16_t v);
+uint16_t dcerpc_get_auth_length(const DATA_BLOB *blob);
 uint8_t dcerpc_get_endian_flag(DATA_BLOB *blob);
+uint8_t dcerpc_get_auth_type(const DATA_BLOB *blob);
+uint8_t dcerpc_get_auth_level(const DATA_BLOB *blob);
+uint32_t dcerpc_get_auth_context_id(const DATA_BLOB *blob);
 const char *dcerpc_default_transport_endpoint(TALLOC_CTX *mem_ctx,
                                              enum dcerpc_transport_t transport,
                                              const struct ndr_interface_table *table);
 
+NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
+                                 const DATA_BLOB *blob,
+                                 struct ncacn_packet *r);
+
 /**
 * @brief       Pull a dcerpc_auth structure, taking account of any auth
 *              padding in the blob. For request/response packets we pass
@@ -169,12 +213,35 @@ const char *dcerpc_default_transport_endpoint(TALLOC_CTX *mem_ctx,
 *
 * @return              - A NTSTATUS error code.
 */
-NTSTATUS dcerpc_pull_auth_trailer(struct ncacn_packet *pkt,
+NTSTATUS dcerpc_pull_auth_trailer(const struct ncacn_packet *pkt,
                                  TALLOC_CTX *mem_ctx,
-                                 DATA_BLOB *pkt_trailer,
+                                 const DATA_BLOB *pkt_trailer,
                                  struct dcerpc_auth *auth,
                                  uint32_t *auth_length,
                                  bool auth_data_only);
+NTSTATUS dcerpc_verify_ncacn_packet_header(const struct ncacn_packet *pkt,
+                                          enum dcerpc_pkt_type ptype,
+                                          size_t max_auth_info,
+                                          uint8_t required_flags,
+                                          uint8_t optional_flags);
+NTSTATUS dcerpc_ncacn_pull_pkt_auth(const struct dcerpc_auth *auth_state,
+                                   struct gensec_security *gensec,
+                                   TALLOC_CTX *mem_ctx,
+                                   enum dcerpc_pkt_type ptype,
+                                   uint8_t required_flags,
+                                   uint8_t optional_flags,
+                                   uint8_t payload_offset,
+                                   DATA_BLOB *payload_and_verifier,
+                                   DATA_BLOB *raw_packet,
+                                   const struct ncacn_packet *pkt);
+NTSTATUS dcerpc_ncacn_push_pkt_auth(const struct dcerpc_auth *auth_state,
+                                   struct gensec_security *gensec,
+                                   TALLOC_CTX *mem_ctx,
+                                   DATA_BLOB *raw_packet,
+                                   size_t sig_size,
+                                   uint8_t payload_offset,
+                                   const DATA_BLOB *payload,
+                                   const struct ncacn_packet *pkt);
 struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
                                                 struct tevent_context *ev,
                                                 struct tstream_context *stream);
@@ -209,6 +276,13 @@ struct dcerpc_binding_handle_ops {
                                  uint8_t **out_data,
                                  size_t *out_length,
                                  uint32_t *out_flags);
+       struct tevent_req *(*raw_call_in_send)(TALLOC_CTX *mem_ctx,
+                                              struct tevent_context *ev,
+                                              struct tevent_req *raw_call_req,
+                                              uint32_t in_flags,
+                                              const uint8_t *in_data,
+                                              size_t in_length);
+       NTSTATUS (*raw_call_in_recv)(struct tevent_req *req);
 
        struct tevent_req *(*disconnect_send)(TALLOC_CTX *mem_ctx,
                                              struct tevent_context *ev,
@@ -293,12 +367,36 @@ NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
                                        uint8_t **out_data,
                                        size_t *out_length,
                                        uint32_t *out_flags);
+struct tevent_req *dcerpc_binding_handle_raw_call_in_send(TALLOC_CTX *mem_ctx,
+                                               struct tevent_context *ev,
+                                               struct tevent_req *raw_call_req,
+                                               uint32_t in_flags,
+                                               const uint8_t *in_data,
+                                               size_t in_length);
+NTSTATUS dcerpc_binding_handle_raw_call_in_recv(struct tevent_req *req);
 
 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
                                                struct tevent_context *ev,
                                                struct dcerpc_binding_handle *h);
 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req);
 
+struct dcerpc_binding_handle_call_params {
+       TALLOC_CTX *r_mem;
+       void *r_ptr;
+       struct {
+               uint32_t num_pipes;
+               struct dcerpc_pipe_handle_connection **pipes;
+       } in, out;
+};
+
+struct tevent_req *dcerpc_binding_handle_call_params_send(TALLOC_CTX *mem_ctx,
+                               struct tevent_context *ev,
+                               struct dcerpc_binding_handle *h,
+                               const struct GUID *object,
+                               const struct ndr_interface_table *table,
+                               uint32_t opnum,
+                               struct dcerpc_binding_handle_call_params *params);
+NTSTATUS dcerpc_binding_handle_call_params_recv(struct tevent_req *req);
 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct dcerpc_binding_handle *h,
@@ -338,4 +436,120 @@ struct dcerpc_sec_vt_header2 dcerpc_sec_vt_header2_from_ncacn_packet(const struc
 bool dcerpc_sec_vt_header2_equal(const struct dcerpc_sec_vt_header2 *v1,
                                 const struct dcerpc_sec_vt_header2 *v2);
 
+/**
+ * Check for consistency of the security verification trailer with the PDU header.
+ * See <a href="http://msdn.microsoft.com/en-us/library/cc243559.aspx">MS-RPCE 2.2.2.13</a>.
+ * A check with an empty trailer succeeds.
+ *
+ * @param[in] vt a pointer to the security verification trailer.
+ * @param[in] bitmask1 which flags were negotiated on the connection.
+ * @param[in] pcontext the syntaxes negotiatied for the presentation context.
+ * @param[in] header2 some fields from the PDU header.
+ *
+ * @retval true on success.
+ */
+bool dcerpc_sec_verification_trailer_check(
+               const struct dcerpc_sec_verification_trailer *vt,
+               const uint32_t *bitmask1,
+               const struct dcerpc_sec_vt_pcontext *pcontext,
+               const struct dcerpc_sec_vt_header2 *header2);
+
+/**
+ * @brief check and optionally extract the Bind Time Features from
+ * the given ndr_syntax_id.
+ *
+ * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
+ *
+ * @param[in]  s the syntax that should be checked.
+ *
+ * @param[out] features This is optional, it will be filled with the extracted
+ *                      features the on success, otherwise it's filled with 0.
+ *
+ * @return true if the syntax matches the 6CB71C2C-9812-4540 prefix with version 1, false otherwise.
+ *
+ * @see dcerpc_construct_bind_time_features
+ */
+bool dcerpc_extract_bind_time_features(struct ndr_syntax_id syntax, uint64_t *features);
+
+/**
+ * @brief Construct a ndr_syntax_id used for Bind Time Features Negotiation.
+ *
+ * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
+ *
+ * @param[in] features The supported features.
+ *
+ * @return The ndr_syntax_id with the given features.
+ *
+ * @see dcerpc_extract_bind_time_features
+ */
+struct ndr_syntax_id dcerpc_construct_bind_time_features(uint64_t features);
+
+#define DCERPC_AUTH_PAD_LENGTH(stub_length) (\
+       (((stub_length) % DCERPC_AUTH_PAD_ALIGNMENT) > 0)?\
+       (DCERPC_AUTH_PAD_ALIGNMENT - (stub_length) % DCERPC_AUTH_PAD_ALIGNMENT):\
+       0)
+
+struct dcerpc_pipe_handle_connection *dcerpc_pipe_handle_connection_create(
+                                       TALLOC_CTX *mem_ctx,
+                                       const char *chunk_struct_name,
+                                       size_t chunk_struct_size);
+bool _dcerpc_pipe_handle_connection_connect(
+                               struct dcerpc_pipe_handle_connection *pc,
+                               const char *chunk_struct_name,
+                               size_t chunk_struct_size,
+                               struct dcerpc_pipe_handle *p,
+                               bool push);
+#define dcerpc_pipe_handle_connection_push_connect(pc, csn, css, p) \
+       _dcerpc_pipe_handle_connection_connect(pc, csn, css, p, true)
+#define dcerpc_pipe_handle_connection_pull_connect(pc, csn, css, p) \
+       _dcerpc_pipe_handle_connection_connect(pc, csn, css, p, false)
+void dcerpc_pipe_handle_connection_disconnect(
+                               struct dcerpc_pipe_handle_connection *pc);
+
+struct dcerpc_pipe_handle_ops {
+       const char *name;
+
+       struct tevent_req *(*chunk_push_send)(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           struct dcerpc_pipe_handle *p,
+                                           const void *chunk_ptr);
+       NTSTATUS (*chunk_push_recv)(struct tevent_req *req);
+
+       struct tevent_req *(*chunk_pull_send)(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           struct dcerpc_pipe_handle *p,
+                                           void *chunk_mem,
+                                           void *chunk_ptr);
+       NTSTATUS (*chunk_pull_recv)(struct tevent_req *req);
+};
+
+struct dcerpc_pipe_handle *_dcerpc_pipe_handle_create(
+                                       TALLOC_CTX *mem_ctx,
+                                       const struct dcerpc_pipe_handle_ops *ops,
+                                       void *pstate,
+                                       size_t psize,
+                                       const char *type,
+                                       const char *location);
+#define dcerpc_pipe_handle_create(mem_ctx, ops, \
+                                 state, type) \
+       _dcerpc_pipe_handle_create(mem_ctx, ops, \
+                               state, sizeof(type), #type, __location__)
+
+void *_dcerpc_pipe_handle_data(struct dcerpc_pipe_handle *p);
+#define dcerpc_pipe_handle_data(_p, _type) \
+       talloc_get_type_abort(_dcerpc_pipe_handle_data(_p), _type)
+
+struct tevent_req *dcerpc_pipe_handle_push_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct dcerpc_pipe_handle_connection *pc,
+                                       const void *chunk_ptr);
+NTSTATUS dcerpc_pipe_handle_push_recv(struct tevent_req *req);
+
+struct tevent_req *dcerpc_pipe_handle_pull_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct dcerpc_pipe_handle_connection *pc,
+                                       void *chunk_mem,
+                                       void *chunk_ptr);
+NTSTATUS dcerpc_pipe_handle_pull_recv(struct tevent_req *req);
+
 #endif /* __DEFAULT_LIBRPC_RPCCOMMON_H__ */