+
+/****************************************************************************
+ Processes a finished PDU stored in p->in_data.pdu.
+****************************************************************************/
+
+void process_complete_pdu(struct pipes_struct *p)
+{
+ struct ncacn_packet *pkt = NULL;
+ NTSTATUS status;
+ bool reply = False;
+
+ if(p->fault_state) {
+ DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
+ get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
+ goto done;
+ }
+
+ pkt = talloc(p->mem_ctx, struct ncacn_packet);
+ if (!pkt) {
+ DEBUG(0, ("Out of memory!\n"));
+ goto done;
+ }
+
+ /*
+ * Ensure we're using the corrent endianness for both the
+ * RPC header flags and the raw data we will be reading from.
+ */
+ if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
+ p->endian = RPC_LITTLE_ENDIAN;
+ } else {
+ p->endian = RPC_BIG_ENDIAN;
+ }
+ DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
+
+ status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
+ pkt, p->endian);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
+ nt_errstr(status)));
+ goto done;
+ }
+
+ /* Store the call_id */
+ p->call_id = pkt->call_id;
+
+ DEBUG(10, ("Processing packet type %d\n", (int)pkt->ptype));
+
+ switch (pkt->ptype) {
+ case DCERPC_PKT_REQUEST:
+ reply = process_request_pdu(p, pkt);
+ break;
+
+ case DCERPC_PKT_PING: /* CL request - ignore... */
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "Connectionless packet type %d received on "
+ "pipe %s.\n", (int)pkt->ptype,
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+ case DCERPC_PKT_RESPONSE: /* No responses here. */
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "DCERPC_PKT_RESPONSE received from client "
+ "on pipe %s.\n",
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+ case DCERPC_PKT_FAULT:
+ case DCERPC_PKT_WORKING:
+ /* CL request - reply to a ping when a call in process. */
+ case DCERPC_PKT_NOCALL:
+ /* CL - server reply to a ping call. */
+ case DCERPC_PKT_REJECT:
+ case DCERPC_PKT_ACK:
+ case DCERPC_PKT_CL_CANCEL:
+ case DCERPC_PKT_FACK:
+ case DCERPC_PKT_CANCEL_ACK:
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "Connectionless packet type %u received on "
+ "pipe %s.\n", (unsigned int)pkt->ptype,
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+ case DCERPC_PKT_BIND:
+ /*
+ * We assume that a pipe bind is only in one pdu.
+ */
+ if (pipe_init_outgoing_data(p)) {
+ reply = api_pipe_bind_req(p, pkt);
+ }
+ break;
+
+ case DCERPC_PKT_BIND_ACK:
+ case DCERPC_PKT_BIND_NAK:
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "DCERPC_PKT_BINDACK/DCERPC_PKT_BINDNACK "
+ "packet type %u received on pipe %s.\n",
+ (unsigned int)pkt->ptype,
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+
+ case DCERPC_PKT_ALTER:
+ /*
+ * We assume that a pipe bind is only in one pdu.
+ */
+ if (pipe_init_outgoing_data(p)) {
+ reply = api_pipe_alter_context(p, pkt);
+ }
+ break;
+
+ case DCERPC_PKT_ALTER_RESP:
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "DCERPC_PKT_ALTER_RESP on pipe %s: "
+ "Should only be server -> client.\n",
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+ case DCERPC_PKT_AUTH3:
+ /*
+ * The third packet in an auth exchange.
+ */
+ if (pipe_init_outgoing_data(p)) {
+ reply = api_pipe_bind_auth3(p, pkt);
+ }
+ break;
+
+ case DCERPC_PKT_SHUTDOWN:
+ DEBUG(0, ("process_complete_pdu: Error. "
+ "DCERPC_PKT_SHUTDOWN on pipe %s: "
+ "Should only be server -> client.\n",
+ get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ break;
+
+ case DCERPC_PKT_CO_CANCEL:
+ /* For now just free all client data and continue
+ * processing. */
+ DEBUG(3,("process_complete_pdu: DCERPC_PKT_CO_CANCEL."
+ " Abandoning rpc call.\n"));
+ /* As we never do asynchronous RPC serving, we can
+ * never cancel a call (as far as I know).
+ * If we ever did we'd have to send a cancel_ack reply.
+ * For now, just free all client data and continue
+ * processing. */
+ reply = True;
+ break;
+
+#if 0
+ /* Enable this if we're doing async rpc. */
+ /* We must check the outstanding callid matches. */
+ if (pipe_init_outgoing_data(p)) {
+ /* Send a cancel_ack PDU reply. */
+ /* We should probably check the auth-verifier here. */
+ reply = setup_cancel_ack_reply(p, pkt);
+ }
+ break;
+#endif
+
+ case DCERPC_PKT_ORPHANED:
+ /* We should probably check the auth-verifier here.
+ * For now just free all client data and continue
+ * processing. */
+ DEBUG(3, ("process_complete_pdu: DCERPC_PKT_ORPHANED."
+ " Abandoning rpc call.\n"));
+ reply = True;
+ break;
+
+ default:
+ DEBUG(0, ("process_complete_pdu: "
+ "Unknown rpc type = %u received.\n",
+ (unsigned int)pkt->ptype));
+ break;
+ }
+
+done:
+ if (!reply) {
+ DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on "
+ "pipe %s\n", get_pipe_name_from_syntax(talloc_tos(),
+ &p->syntax)));
+ set_incoming_fault(p);
+ setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
+ TALLOC_FREE(pkt);
+ } else {
+ /*
+ * Reset the lengths. We're ready for a new pdu.
+ */
+ TALLOC_FREE(p->in_data.pdu.data);
+ p->in_data.pdu_needed_len = 0;
+ p->in_data.pdu.length = 0;
+ }
+
+ TALLOC_FREE(pkt);
+}
+