Do not vasprint stuff where not necessary
[kai/samba-autobuild/.git] / source3 / rpc_server / srv_lsa_hnd.c
index dabc5520ff2c7136019eb0bb769d41eb2e6051c4..d8c48058be9197f589f1f176dd1aee2b981d7f60 100644 (file)
@@ -1,14 +1,13 @@
-
 /* 
- *  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,
+ *  Copyright (C) Jeremy Allison                          2001.
  *  
  *  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"
 
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
 
-extern int DEBUGLEVEL;
-
-#ifndef MAX_OPEN_POLS
-#define MAX_OPEN_POLS 64
-#endif
+/*
+ * Handle database - stored per pipe.
+ */
 
-struct reg_info
-{
-    /* for use by \PIPE\winreg */
-       fstring name; /* name of registry key */
-};
+struct policy {
+       struct policy *next, *prev;
 
-struct samr_info
-{
-    /* for use by the \PIPE\samr policy */
-       DOM_SID sid;
-    uint32 rid; /* relative id associated with the pol_hnd */
-    uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
-};
+       struct policy_handle pol_hnd;
 
-static struct policy
-{
-       struct policy *next, *prev;
-       int pnum;
-       BOOL open;
-       POLICY_HND pol_hnd;
+       uint32_t access_granted;
 
-       union {
-               struct samr_info samr;
-               struct reg_info reg;
-       } dev;
-} *Policy;
+       void *data_ptr;
+};
 
-static struct bitmap *bmap;
+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
 
 /****************************************************************************
-  create a unique policy handle
+ Hack as handles need to be persisant over lsa pipe closes so long as a samr
+ pipe is open. JRA.
 ****************************************************************************/
-static void create_pol_hnd(POLICY_HND *hnd)
-{
-       static uint32 pol_hnd_low  = 0;
-       static uint32 pol_hnd_high = 0;
-
-       if (hnd == NULL) return;
 
-       /* i severely doubt that pol_hnd_high will ever be non-zero... */
-       pol_hnd_low++;
-       if (pol_hnd_low == 0) pol_hnd_high++;
+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));
+}
 
-       SIVAL(hnd->data, 0 , 0x0);  /* first bit must be null */
-       SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
-       SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
-       SIVAL(hnd->data, 12, time(NULL)); /* something random */
-       SIVAL(hnd->data, 16, getpid()); /* something more random */
+size_t num_pipe_handles(struct handle_list *list)
+{
+       if (list == NULL) {
+               return 0;
+       }
+       return list->count;
 }
 
 /****************************************************************************
-  initialise policy handle states...
+ Initialise a policy handle list on a pipe. Handle list is shared between all
+ pipes of the same name.
 ****************************************************************************/
-void init_lsa_policy_hnd(void)
+
+bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
 {
-       bmap = bitmap_allocate(MAX_OPEN_POLS);
-       if (!bmap) {
-               exit_server("out of memory in init_lsa_policy_hnd\n");
+       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 (plist != NULL) {
+               hl = plist->pipe_handles;
+               if (hl == NULL) {
+                       return false;
+               }
+       } else {
+               /*
+                * First open, we have to create the handle list
+                */
+               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", get_pipe_name_from_iface(syntax)));
+       }
+
+       /*
+        * One more pipe is using this list.
+        */
+
+       hl->pipe_ref_count++;
+
+       /*
+        * Point this pipe at this list.
+        */
+
+       p->pipe_handles = hl;
+
+       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 open_lsa_policy_hnd(POLICY_HND *hnd)
+
+static struct policy *create_policy_hnd_internal(pipes_struct *p,
+                                                struct policy_handle *hnd,
+                                                void *data_ptr)
 {
-       int i;
-       struct policy *p;
+       static uint32 pol_hnd_low  = 0;
+       static uint32 pol_hnd_high = 0;
+       time_t t = time(NULL);
 
-       i = bitmap_find(bmap, 1);
+       struct policy *pol;
 
-       if (i == -1) {
-               DEBUG(0,("ERROR: out of Policy Handles!\n"));
-               return False;
+       if (p->pipe_handles->count > MAX_OPEN_POLS) {
+               DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
+                               (int)p->pipe_handles->count));
+               return NULL;
        }
 
-       p = (struct policy *)malloc(sizeof(*p));
-       if (!p) {
-               DEBUG(0,("ERROR: out of memory!\n"));
-               return False;
+       pol = TALLOC_ZERO_P(NULL, struct policy);
+       if (!pol) {
+               DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
+               return NULL;
        }
 
-       ZERO_STRUCTP(p);
+       if (data_ptr != NULL) {
+               pol->data_ptr = talloc_move(pol, &data_ptr);
+       }
 
-       p->open = True;                         
-       p->pnum = i;
+       pol_hnd_low++;
+       if (pol_hnd_low == 0)
+               (pol_hnd_high)++;
 
-       create_pol_hnd(hnd);
-       memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
+       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 */
 
-       bitmap_set(bmap, i);
+       /* split the current time into two 16 bit values */
 
-       DLIST_ADD(Policy, p);
+       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.uuid.node, 2, sys_getpid()); /* something more random */
+
+       DLIST_ADD(p->pipe_handles->Policy, pol);
+       p->pipe_handles->count++;
+
+       *hnd = pol->pol_hnd;
        
-       DEBUG(4,("Opened policy hnd[%x] ", i));
-       dump_data(4, (char *)hnd->data, sizeof(hnd->data));
+       DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
+       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
 
-       return True;
+       return pol;
+}
+
+bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
+                      void *data_ptr)
+{
+       return create_policy_hnd_internal(p, hnd, data_ptr) != NULL;
 }
 
 /****************************************************************************
-  find policy by handle
+  find policy by handle - internal version.
 ****************************************************************************/
-static struct policy *find_lsa_policy(POLICY_HND *hnd)
-{
-       struct policy *p;
 
-       for (p=Policy;p;p=p->next) {
-               if (memcmp(&p->pol_hnd, hnd, sizeof(*hnd)) == 0) {
-                       DEBUG(4,("Found policy hnd[%x] ", p->pnum));
-                       dump_data(4, (char *)hnd->data, sizeof(hnd->data));
-                       return 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;
+
+       if (data_p)
+               *data_p = NULL;
+
+       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, (uint8 *)hnd, sizeof(*hnd));
+                       if (data_p)
+                               *data_p = pol->data_ptr;
+                       return pol;
                }
        }
 
        DEBUG(4,("Policy not found: "));
-       dump_data(4, (char *)hnd->data, sizeof(hnd->data));
+       dump_data(4, (uint8 *)hnd, sizeof(*hnd));
+
+       p->bad_handle_fault_state = True;
 
        return NULL;
 }
 
 /****************************************************************************
-  find policy index by handle
+  find policy by handle
 ****************************************************************************/
-int find_lsa_policy_by_hnd(POLICY_HND *hnd)
-{
-       struct policy *p = find_lsa_policy(hnd);
 
-       return p?p->pnum:-1;
+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;
 }
 
 /****************************************************************************
-  set samr rid
+  Close a policy.
 ****************************************************************************/
-BOOL set_lsa_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
-{
-       struct policy *p = find_lsa_policy(hnd);
 
-       if (p && p->open) {
-               DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
-                        rid, p->pnum));
+bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
+{
+       struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
 
-               p->dev.samr.rid = rid;
-               return True;
+       if (!pol) {
+               DEBUG(3,("Error closing policy\n"));
+               return False;
        }
 
-       DEBUG(3,("Error setting policy rid=%x\n",rid));
-       return False;
-}
-
+       DEBUG(3,("Closed policy\n"));
 
-/****************************************************************************
-  set samr pol status.  absolutely no idea what this is.
-****************************************************************************/
-BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
-{
-       struct policy *p = find_lsa_policy(hnd);
+       p->pipe_handles->count--;
 
-       if (p && p->open) {
-               DEBUG(3,("Setting policy status=%x pnum=%x\n",
-                         pol_status, p->pnum));
+       DLIST_REMOVE(p->pipe_handles->Policy, pol);
 
-               p->dev.samr.status = pol_status;
-               return True;
-       } 
+       TALLOC_FREE(pol);
 
-       DEBUG(3,("Error setting policy status=%x\n",
-                pol_status));
-       return False;
+       return True;
 }
 
 /****************************************************************************
-  set samr sid
+ Close a pipe - free the handle list if it was the last pipe reference.
 ****************************************************************************/
-BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
-{
-       pstring sidstr;
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open) {
-               DEBUG(3,("Setting policy sid=%s pnum=%x\n",
-                        sid_to_string(sidstr, sid), p->pnum));
-
-               memcpy(&p->dev.samr.sid, sid, sizeof(*sid));
-               return True;
-       }
-
-       DEBUG(3,("Error setting policy sid=%s\n",
-                 sid_to_string(sidstr, sid)));
-       return False;
-}
 
-/****************************************************************************
-  get samr sid
-****************************************************************************/
-BOOL get_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
+void close_policy_by_pipe(pipes_struct *p)
 {
-       struct policy *p = find_lsa_policy(hnd);
+       p->pipe_handles->pipe_ref_count--;
 
-       if (p != NULL && p->open)
-       {
-               pstring sidstr;
-               memcpy(sid, &p->dev.samr.sid, sizeof(*sid));
-               DEBUG(3,("Getting policy sid=%s pnum=%x\n",
-                        sid_to_string(sidstr, sid), p->pnum));
+       if (p->pipe_handles->pipe_ref_count == 0) {
+               /*
+                * Last pipe open on this list - free the list.
+                */
+               while (p->pipe_handles->Policy)
+                       close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
 
-               return True;
-       }
+               p->pipe_handles->Policy = NULL;
+               p->pipe_handles->count = 0;
 
-       DEBUG(3,("Error getting policy\n"));
-       return False;
+               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)));
+       }
 }
 
-/****************************************************************************
-  get samr rid
-****************************************************************************/
-uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
+/*******************************************************************
+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)
 {
-       struct policy *p = find_lsa_policy(hnd);
+       /* Don't let anonymous users access this RPC if restrict
+          anonymous > 0 */
 
-       if (p && p->open) {
-               uint32 rid = p->dev.samr.rid;
-               DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
-                         rid, p->pnum));
+       if (lp_restrict_anonymous() > 0) {
 
-               return rid;
+               /* 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;
+               }
        }
 
-       DEBUG(3,("Error getting policy\n"));
-       return 0xffffffff;
+       return True;
 }
 
-/****************************************************************************
-  set reg name 
-****************************************************************************/
-BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
+void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
+                           uint32_t access_granted, size_t data_size,
+                           const char *type, NTSTATUS *pstatus)
 {
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open) {
-               DEBUG(3,("Setting policy pnum=%x name=%s\n",
-                        p->pnum, name));
+       struct policy *pol;
+       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)));
+               *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
+               return NULL;
+       }
 
-               fstrcpy(p->dev.reg.name, name);
-               return True;
+       data = talloc_size(talloc_tos(), data_size);
+       if (data == NULL) {
+               *pstatus = NT_STATUS_NO_MEMORY;
+               return NULL;
        }
+       talloc_set_name_const(data, type);
 
-       DEBUG(3,("Error setting policy name=%s\n", name));
-       return False;
+       pol = create_policy_hnd_internal(p, hnd, data);
+       if (pol == NULL) {
+               TALLOC_FREE(data);
+               *pstatus = NT_STATUS_NO_MEMORY;
+               return NULL;
+       }
+       pol->access_granted = access_granted;
+       *pstatus = NT_STATUS_OK;
+       return data;
 }
 
-/****************************************************************************
-  close an lsa policy
-****************************************************************************/
-BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
+void *_policy_handle_find(struct pipes_struct *p,
+                         const struct policy_handle *hnd,
+                         uint32_t access_required,
+                         uint32_t *paccess_granted,
+                         const char *name, const char *location,
+                         NTSTATUS *pstatus)
 {
-       struct policy *p = find_lsa_policy(hnd);
+       struct policy *pol;
+       void *data;
 
-       if (!p)
-       {
-               DEBUG(3,("Error closing policy\n"));
-               return False;
+       pol = find_policy_by_hnd_internal(p, hnd, &data);
+       if (pol == NULL) {
+               *pstatus = NT_STATUS_INVALID_HANDLE;
+               return NULL;
+       }
+       if (strcmp(name, talloc_get_name(data)) != 0) {
+               DEBUG(10, ("expected %s, got %s\n", name,
+                          talloc_get_name(data)));
+               *pstatus = NT_STATUS_INVALID_HANDLE;
+               return NULL;
+       }
+       if ((access_required & pol->access_granted) != access_required) {
+               if (geteuid() == sec_initial_uid()) {
+                       DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
+                                 "%#010x; required: %#010x)\n", location,
+                                 pol->access_granted, access_required));
+                       DEBUGADD(4,("but overwritten by euid == 0\n"));
+                       goto okay;
+               }
+               DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
+                        "%#010x)\n", location, pol->access_granted,
+                        access_required));
+               *pstatus = NT_STATUS_ACCESS_DENIED;
+               return NULL;
        }
 
-       DEBUG(3,("Closed policy name pnum=%x\n",  p->pnum));
-
-       DLIST_REMOVE(Policy, p);
-
-       bitmap_clear(bmap, p->pnum);
-
-       ZERO_STRUCTP(p);
-       ZERO_STRUCTP(hnd);
-
-       free(p);
-
-       return True;
+ okay:
+       DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
+       if (paccess_granted != NULL) {
+               *paccess_granted = pol->access_granted;
+       }
+       *pstatus = NT_STATUS_OK;
+       return data;
 }
-