s3-librpc Call SPENGO/GSSAPI via the auth_generic layer and gensec
[kai/samba.git] / source3 / rpc_server / rpc_handles.c
index bfdc7a85611332b0eaf26aa1a6714395ecd47b2a..d547f18dc1e5ccb053b7570198f235d89ee60d3e 100644 (file)
  */
 
 #include "includes.h"
+#include "system/passwd.h" /* uid_wrapper */
 #include "../librpc/gen_ndr/ndr_lsa.h"
 #include "../librpc/gen_ndr/ndr_samr.h"
+#include "auth.h"
+#include "rpc_server/rpc_pipes.h"
+#include "../libcli/security/security.h"
+#include "lib/tsocket/tsocket.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
+static struct pipes_struct *InternalPipes;
+
+/* TODO
+ * the following prototypes are declared here to avoid
+ * code being moved about too much for a patch to be
+ * disrupted / less obvious.
+ *
+ * these functions, and associated functions that they
+ * call, should be moved behind a .so module-loading
+ * system _anyway_.  so that's the next step...
+ */
+
+int make_base_pipes_struct(TALLOC_CTX *mem_ctx,
+                          struct messaging_context *msg_ctx,
+                          const char *pipe_name,
+                          enum dcerpc_transport_t transport,
+                          bool endian, bool ncalrpc_as_system,
+                          const struct tsocket_address *remote_address,
+                          const struct tsocket_address *local_address,
+                          struct pipes_struct **_p)
+{
+       struct pipes_struct *p;
+
+       p = talloc_zero(mem_ctx, struct pipes_struct);
+       if (!p) {
+               return ENOMEM;
+       }
+
+       p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
+       if (!p->mem_ctx) {
+               talloc_free(p);
+               return ENOMEM;
+       }
+
+       p->msg_ctx = msg_ctx;
+       p->transport = transport;
+       p->endian = endian;
+       p->ncalrpc_as_system = ncalrpc_as_system;
+
+       p->remote_address = tsocket_address_copy(remote_address, p);
+       if (p->remote_address == NULL) {
+               talloc_free(p);
+               return ENOMEM;
+       }
+
+       if (local_address) {
+               p->local_address = tsocket_address_copy(local_address, p);
+               if (p->local_address == NULL) {
+                       talloc_free(p);
+                       return ENOMEM;
+               }
+       }
+
+       DLIST_ADD(InternalPipes, p);
+       talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
+
+       *_p = p;
+       return 0;
+}
+
+
+bool check_open_pipes(void)
+{
+       struct pipes_struct *p;
+
+       for (p = InternalPipes; p != NULL; p = p->next) {
+               if (num_pipe_handles(p) != 0) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+/****************************************************************************
+ Close an rpc pipe.
+****************************************************************************/
+
+int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
+{
+       if (!p) {
+               DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
+               return False;
+       }
+
+       /* Free the handles database. */
+       close_policy_by_pipe(p);
+
+       DLIST_REMOVE(InternalPipes, p);
+
+       return 0;
+}
+
 /*
  * Handle database - stored per pipe.
  */
@@ -60,7 +157,7 @@ static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
                || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
 }
 
-size_t num_pipe_handles(pipes_struct *p)
+size_t num_pipe_handles(struct pipes_struct *p)
 {
        if (p->pipe_handles == NULL) {
                return 0;
@@ -73,23 +170,34 @@ size_t num_pipe_handles(pipes_struct *p)
  pipes of the same name.
 ****************************************************************************/
 
-bool init_pipe_handles(pipes_struct *p, const struct ndr_syntax_id *syntax)
+bool init_pipe_handles(struct pipes_struct *p, const struct ndr_syntax_id *syntax)
 {
-       pipes_struct *plist;
+       struct pipes_struct *plist;
        struct handle_list *hl;
 
-       for (plist = get_first_internal_pipe();
-            plist;
-            plist = get_next_internal_pipe(plist)) {
-               if (ndr_syntax_id_equal(syntax, &plist->syntax)) {
-                       break;
+       for (plist = InternalPipes; plist; plist = plist->next) {
+               struct pipe_rpc_fns *p_ctx;
+               bool stop = false;
+
+               for (p_ctx = plist->contexts;
+                    p_ctx != NULL;
+                    p_ctx = p_ctx->next) {
+                       if (ndr_syntax_id_equal(syntax, &p_ctx->syntax)) {
+                               stop = true;
+                               break;
+                       }
+                       if (is_samr_lsa_pipe(&p_ctx->syntax)
+                           && is_samr_lsa_pipe(syntax)) {
+                               /*
+                                * samr and lsa share a handle space (same process
+                                * under Windows?)
+                                */
+                               stop = true;
+                               break;
+                       }
                }
-               if (is_samr_lsa_pipe(&plist->syntax)
-                   && is_samr_lsa_pipe(syntax)) {
-                       /*
-                        * samr and lsa share a handle space (same process
-                        * under Windows?)
-                        */
+
+               if (stop) {
                        break;
                }
        }
@@ -103,7 +211,7 @@ bool init_pipe_handles(pipes_struct *p, const struct ndr_syntax_id *syntax)
                /*
                 * First open, we have to create the handle list
                 */
-               hl = talloc_zero(p, struct handle_list);
+               hl = talloc_zero(NULL, struct handle_list);
                if (hl == NULL) {
                        return false;
                }
@@ -140,7 +248,7 @@ bool init_pipe_handles(pipes_struct *p, const struct ndr_syntax_id *syntax)
   data_ptr is TALLOC_FREE()'ed
 ****************************************************************************/
 
-static struct dcesrv_handle *create_rpc_handle_internal(pipes_struct *p,
+static struct dcesrv_handle *create_rpc_handle_internal(struct pipes_struct *p,
                                struct policy_handle *hnd, void *data_ptr)
 {
        struct dcesrv_handle *rpc_hnd;
@@ -197,7 +305,7 @@ static struct dcesrv_handle *create_rpc_handle_internal(pipes_struct *p,
        return rpc_hnd;
 }
 
-bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
+bool create_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd,
                       void *data_ptr)
 {
        struct dcesrv_handle *rpc_hnd;
@@ -213,7 +321,7 @@ bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
   find policy by handle - internal version.
 ****************************************************************************/
 
-static struct dcesrv_handle *find_policy_by_hnd_internal(pipes_struct *p,
+static struct dcesrv_handle *find_policy_by_hnd_internal(struct pipes_struct *p,
                                const struct policy_handle *hnd, void **data_p)
 {
        struct dcesrv_handle *h;
@@ -227,7 +335,7 @@ static struct dcesrv_handle *find_policy_by_hnd_internal(pipes_struct *p,
        for (h = p->pipe_handles->handles; h != NULL; h = h->next) {
                if (memcmp(&h->wire_handle, hnd, sizeof(*hnd)) == 0) {
                        DEBUG(4,("Found policy hnd[%u] ", count));
-                       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
+                       dump_data(4, (const uint8 *)hnd, sizeof(*hnd));
                        if (data_p) {
                                *data_p = h->data;
                        }
@@ -237,7 +345,7 @@ static struct dcesrv_handle *find_policy_by_hnd_internal(pipes_struct *p,
        }
 
        DEBUG(4,("Policy not found: "));
-       dump_data(4, (uint8_t *)hnd, sizeof(*hnd));
+       dump_data(4, (const uint8_t *)hnd, sizeof(*hnd));
 
        p->bad_handle_fault_state = true;
 
@@ -248,7 +356,7 @@ static struct dcesrv_handle *find_policy_by_hnd_internal(pipes_struct *p,
   find policy by handle
 ****************************************************************************/
 
-bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
+bool find_policy_by_hnd(struct pipes_struct *p, const struct policy_handle *hnd,
                        void **data_p)
 {
        struct dcesrv_handle *rpc_hnd;
@@ -264,7 +372,7 @@ bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
   Close a policy.
 ****************************************************************************/
 
-bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
+bool close_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd)
 {
        struct dcesrv_handle *rpc_hnd;
 
@@ -289,8 +397,12 @@ bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
  Close a pipe - free the handle set if it was the last pipe reference.
 ****************************************************************************/
 
-void close_policy_by_pipe(pipes_struct *p)
+void close_policy_by_pipe(struct pipes_struct *p)
 {
+       if (p->pipe_handles == NULL) {
+               return;
+       }
+
        p->pipe_handles->pipe_ref_count--;
 
        if (p->pipe_handles->pipe_ref_count == 0) {
@@ -299,9 +411,9 @@ void close_policy_by_pipe(pipes_struct *p)
                 */
                TALLOC_FREE(p->pipe_handles);
 
-               DEBUG(10,("close_policy_by_pipe: deleted handle list for "
-                         "pipe %s\n",
-                         get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
+               DEBUG(10,("Deleted handle list for RPC connection %s\n",
+                         get_pipe_name_from_syntax(talloc_tos(),
+                                                   &p->contexts->syntax)));
        }
 }
 
@@ -313,7 +425,7 @@ will be checking a security descriptor to determine whether a user
 token has enough access to access the pipe.
 ********************************************************************/
 
-bool pipe_access_check(pipes_struct *p)
+bool pipe_access_check(struct pipes_struct *p)
 {
        /* Don't let anonymous users access this RPC if restrict
           anonymous > 0 */
@@ -321,11 +433,12 @@ bool pipe_access_check(pipes_struct *p)
        if (lp_restrict_anonymous() > 0) {
 
                /* schannel, so we must be ok */
-               if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
+               if (p->pipe_bound &&
+                   (p->auth.auth_type == DCERPC_AUTH_TYPE_SCHANNEL)) {
                        return True;
                }
 
-               if (p->server_info->guest) {
+               if (security_session_user_level(p->session_info, NULL) < SECURITY_USER) {
                        return False;
                }
        }
@@ -341,9 +454,10 @@ void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
        void *data;
 
        if (p->pipe_handles->count > MAX_OPEN_POLS) {
-               DEBUG(0, ("policy_handle_create: ERROR: too many handles (%d) "
-                         "on pipe %s.\n", (int)p->pipe_handles->count,
-                         get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
+               DEBUG(0, ("ERROR: Too many handles (%d) for RPC connection %s\n",
+                         (int) p->pipe_handles->count,
+                         get_pipe_name_from_syntax(talloc_tos(),
+                                                   &p->contexts->syntax)));
                *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
                return NULL;
        }