Make "struct policy" private to srv_lsa_hnd.c
[ira/wip.git] / source3 / rpc_server / srv_lsa_hnd.c
index 393f50a498e1f250c2907cc2637be7219a236cab..9891ff39648e8f41962ba6a7c61e2e8fa182cc3c 100644 (file)
@@ -1,6 +1,5 @@
 /* 
- *  Unix SMB/Netbios implementation.
- *  Version 1.9.
+ *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
@@ -8,7 +7,7 @@
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
+/*
+ * Handle database - stored per pipe.
+ */
+
+struct policy {
+       struct policy *next, *prev;
+
+       struct policy_handle pol_hnd;
+
+       void *data_ptr;
+};
+
+struct handle_list {
+       struct policy *Policy;  /* List of policies. */
+       size_t count;                   /* Current number of handles. */
+       size_t pipe_ref_count;  /* Number of pipe handles referring to this list. */
+};
 
 /* This is the max handles across all instances of a pipe name. */
 #ifndef MAX_OPEN_POLS
 #define MAX_OPEN_POLS 1024
 #endif
 
+/****************************************************************************
+ Hack as handles need to be persisant over lsa pipe closes so long as a samr
+ pipe is open. JRA.
+****************************************************************************/
+
+static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
+{
+       return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
+               || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
+}
+
+size_t num_pipe_handles(struct handle_list *list)
+{
+       if (list == NULL) {
+               return 0;
+       }
+       return list->count;
+}
+
 /****************************************************************************
  Initialise a policy handle list on a pipe. Handle list is shared between all
  pipes of the same name.
 ****************************************************************************/
 
-BOOL init_pipe_handle_list(pipes_struct *p, char *pipe_name)
+bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
 {
-       pipes_struct *plist = get_first_pipe();
-       struct handle_list *hl = NULL;
-
-       for (plist = get_first_pipe(); plist; plist = get_next_pipe(plist)) {
-               if (strequal( plist->name, pipe_name)) {
-                       if (!plist->pipe_handles) {
-                               pstring msg;
-                               slprintf(msg, sizeof(msg)-1, "init_pipe_handles: NULL pipe_handle pointer in pipe %s",
-                                               pipe_name );
-                               smb_panic(msg);
-                       }
-                       hl = plist->pipe_handles;
+       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;
+               }
+               if (is_samr_lsa_pipe(&plist->syntax)
+                   && is_samr_lsa_pipe(syntax)) {
+                       /*
+                        * samr and lsa share a handle space (same process
+                        * under Windows?)
+                        */
                        break;
                }
        }
 
-       if (!hl) {
+       if (plist != NULL) {
+               hl = plist->pipe_handles;
+               if (hl == NULL) {
+                       return false;
+               }
+       } else {
                /*
-                * No handle list for this pipe (first open of pipe).
-                * Create list.
+                * First open, we have to create the handle list
                 */
-
-               if ((hl = (struct handle_list *)malloc(sizeof(struct handle_list))) == NULL)
-                       return False;
+               hl = SMB_MALLOC_P(struct handle_list);
+               if (hl == NULL) {
+                       return false;
+               }
                ZERO_STRUCTP(hl);
 
-               DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
+               DEBUG(10,("init_pipe_handles: created handle list for "
+                         "pipe %s\n", get_pipe_name_from_iface(syntax)));
        }
 
        /*
@@ -78,20 +123,26 @@ BOOL init_pipe_handle_list(pipes_struct *p, char *pipe_name)
 
        p->pipe_handles = hl;
 
-       DEBUG(10,("init_pipe_handles: pipe_handles ref count = %u for pipe %s\n",
-                       p->pipe_handles->pipe_ref_count, pipe_name ));
+       DEBUG(10,("init_pipe_handles: pipe_handles ref count = %lu for pipe %s\n",
+                 (unsigned long)p->pipe_handles->pipe_ref_count,
+                 get_pipe_name_from_iface(syntax)));
 
        return True;
 }
 
 /****************************************************************************
   find first available policy slot.  creates a policy handle for you.
+
+  If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
+  talloc_moves this into the handle. If the policy_hnd is closed,
+  data_ptr is TALLOC_FREE()'ed
 ****************************************************************************/
 
-BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *), void *data_ptr)
+bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd, void *data_ptr)
 {
        static uint32 pol_hnd_low  = 0;
        static uint32 pol_hnd_high = 0;
+       time_t t = time(NULL);
 
        struct policy *pol;
 
@@ -101,26 +152,31 @@ BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *)
                return False;
        }
 
-       pol = (struct policy *)malloc(sizeof(*p));
+       pol = TALLOC_ZERO_P(NULL, struct policy);
        if (!pol) {
                DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
                return False;
        }
 
-       ZERO_STRUCTP(pol);
+       if (data_ptr != NULL) {
+               pol->data_ptr = talloc_move(pol, &data_ptr);
+       }
+
+       pol_hnd_low++;
+       if (pol_hnd_low == 0)
+               (pol_hnd_high)++;
+
+       SIVAL(&pol->pol_hnd.handle_type, 0 , 0);  /* first bit must be null */
+       SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
+       SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
+       SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
 
-       pol->data_ptr = data_ptr;
-       pol->free_fn = free_fn;
+       /* split the current time into two 16 bit values */
 
-    pol_hnd_low++;
-    if (pol_hnd_low == 0) (pol_hnd_high)++;
+       SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
+       SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
 
-    SIVAL(&pol->pol_hnd.data1, 0 , 0);  /* first bit must be null */
-    SIVAL(&pol->pol_hnd.data2, 0 , pol_hnd_low ); /* second bit is incrementing */
-    SSVAL(&pol->pol_hnd.data3, 0 , pol_hnd_high); /* second bit is incrementing */
-    SSVAL(&pol->pol_hnd.data4, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
-    SIVAL(pol->pol_hnd.data5, 0, time(NULL)); /* something random */
-    SIVAL(pol->pol_hnd.data5, 4, sys_getpid()); /* something more random */
+       SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
 
        DLIST_ADD(p->pipe_handles->Policy, pol);
        p->pipe_handles->count++;
@@ -128,7 +184,7 @@ BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *)
        *hnd = pol->pol_hnd;
        
        DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
-       dump_data(4, (char *)hnd, sizeof(*hnd));
+       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
 
        return True;
 }
@@ -137,7 +193,9 @@ BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *)
   find policy by handle - internal version.
 ****************************************************************************/
 
-static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *hnd, void **data_p)
+static struct policy *find_policy_by_hnd_internal(pipes_struct *p,
+                                                 const struct policy_handle *hnd,
+                                                 void **data_p)
 {
        struct policy *pol;
        size_t i;
@@ -148,7 +206,7 @@ static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *h
        for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
                if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
                        DEBUG(4,("Found policy hnd[%d] ", (int)i));
-                       dump_data(4, (char *)hnd, sizeof(*hnd));
+                       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
                        if (data_p)
                                *data_p = pol->data_ptr;
                        return pol;
@@ -156,7 +214,9 @@ static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *h
        }
 
        DEBUG(4,("Policy not found: "));
-       dump_data(4, (char *)hnd, sizeof(*hnd));
+       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
+
+       p->bad_handle_fault_state = True;
 
        return NULL;
 }
@@ -165,7 +225,8 @@ static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *h
   find policy by handle
 ****************************************************************************/
 
-BOOL find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
+bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
+                       void **data_p)
 {
        return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
 }
@@ -174,7 +235,7 @@ BOOL find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
   Close a policy.
 ****************************************************************************/
 
-BOOL close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
+bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
 {
        struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
 
@@ -185,16 +246,11 @@ BOOL close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
 
        DEBUG(3,("Closed policy\n"));
 
-       if (pol->free_fn && pol->data_ptr)
-               (*pol->free_fn)(pol->data_ptr);
-
        p->pipe_handles->count--;
 
        DLIST_REMOVE(p->pipe_handles->Policy, pol);
 
-       ZERO_STRUCTP(pol);
-
-       free(pol);
+       TALLOC_FREE(pol);
 
        return True;
 }
@@ -217,8 +273,81 @@ void close_policy_by_pipe(pipes_struct *p)
                p->pipe_handles->Policy = NULL;
                p->pipe_handles->count = 0;
 
-               free(p->pipe_handles);
-               p->pipe_handles = NULL;
-               DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
+               SAFE_FREE(p->pipe_handles);
+               DEBUG(10,("close_policy_by_pipe: deleted handle list for "
+                         "pipe %s\n", get_pipe_name_from_iface(&p->syntax)));
+       }
+}
+
+/*******************************************************************
+Shall we allow access to this rpc?  Currently this function
+implements the 'restrict anonymous' setting by denying access to
+anonymous users if the restrict anonymous level is > 0.  Further work
+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)
+{
+       /* Don't let anonymous users access this RPC if restrict
+          anonymous > 0 */
+
+       if (lp_restrict_anonymous() > 0) {
+
+               /* schannel, so we must be ok */
+               if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
+                       return True;
+               }
+
+               if (p->server_info->guest) {
+                       return False;
+               }
+       }
+
+       return True;
+}
+
+NTSTATUS _policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
+                              void *pdata, size_t data_size, const char *type)
+{
+       void **ppdata = (void **)pdata;
+       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_iface(&p->syntax)));
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
+       }
+
+       data = talloc_size(talloc_tos(), data_size);
+       if (data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       talloc_set_name(data, type);
+
+       if (!create_policy_hnd(p, hnd, data)) {
+               TALLOC_FREE(data);
+               return NT_STATUS_NO_MEMORY;
+       }
+       *ppdata = data;
+       return NT_STATUS_OK;
+}
+
+void *_policy_handle_find(struct pipes_struct *p,
+                         const struct policy_handle *hnd,
+                         const char *name)
+{
+       void *data;
+
+       if (find_policy_by_hnd_internal(p, hnd, &data) == NULL) {
+               return NULL;
+       }
+       if (strcmp(name, talloc_get_name(data)) != 0) {
+               DEBUG(10, ("expected %s, got %s\n", name,
+                          talloc_get_name(data)));
+               return NULL;
        }
+       DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
+       return data;
 }