Fix more asprintf warnings and some error path errors.
[jra/samba/.git] / source3 / rpc_server / srv_lsa_hnd.c
index d53264a73e1ca6d16bd819aa02a123194b69e093..839833ce542fd839c4ffb8d7180b8c30cc8f845c 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;
-
+/* This is the max handles across all instances of a pipe name. */
 #ifndef MAX_OPEN_POLS
-#define MAX_OPEN_POLS 64
+#define MAX_OPEN_POLS 1024
 #endif
 
-struct reg_info
-{
-    /* for use by \PIPE\winreg */
-       fstring name; /* name of registry key */
-};
+/****************************************************************************
+ Hack as handles need to be persisant over lsa pipe closes so long as a samr
+ pipe is open. JRA.
+****************************************************************************/
 
-struct samr_info
+static bool is_samr_lsa_pipe(const char *pipe_name)
 {
-    /* 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 */
-};
+       return (strstr(pipe_name, "samr") || strstr(pipe_name, "lsa"));
+}
+
+/****************************************************************************
+ Initialise a policy handle list on a pipe. Handle list is shared between all
+ pipes of the same name.
+****************************************************************************/
 
-static struct policy
+bool init_pipe_handle_list(pipes_struct *p, const char *pipe_name)
 {
-       struct policy *next, *prev;
-       int pnum;
-       BOOL open;
-       POLICY_HND pol_hnd;
+       pipes_struct *plist = get_first_internal_pipe();
+       struct handle_list *hl = NULL;
+
+       for (plist = get_first_internal_pipe(); plist; plist = get_next_internal_pipe(plist)) {
+               if (strequal( plist->name, pipe_name) ||
+                               (is_samr_lsa_pipe(plist->name) && is_samr_lsa_pipe(pipe_name))) {
+                       if (!plist->pipe_handles) {
+                               char *msg;
+                               if (asprintf(&msg, "init_pipe_handles: NULL "
+                                                "pipe_handle pointer in pipe %s",
+                                                pipe_name) != -1) {
+                                       smb_panic(msg);
+                               } else {
+                                       smb_panic("init_pipe_handle_list");
+                               }
+                       }
+                       hl = plist->pipe_handles;
+                       break;
+               }
+       }
 
-       union {
-               struct samr_info samr;
-               struct reg_info reg;
-       } dev;
-} *Policy;
+       if (!hl) {
+               /*
+                * No handle list for this pipe (first open of pipe).
+                * Create list.
+                */
 
-static struct bitmap *bmap;
+               if ((hl = SMB_MALLOC_P(struct handle_list)) == NULL)
+                       return False;
+               ZERO_STRUCTP(hl);
 
+               DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
+       }
 
-/****************************************************************************
-  create a unique policy handle
-****************************************************************************/
-static void create_pol_hnd(POLICY_HND *hnd)
-{
-       static uint32 pol_hnd_low  = 0;
-       static uint32 pol_hnd_high = 0;
+       /*
+        * One more pipe is using this list.
+        */
 
-       if (hnd == NULL) return;
+       hl->pipe_ref_count++;
 
-       /* i severely doubt that pol_hnd_high will ever be non-zero... */
-       pol_hnd_low++;
-       if (pol_hnd_low == 0) pol_hnd_high++;
+       /*
+        * Point this pipe at this list.
+        */
 
-       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 */
-}
+       p->pipe_handles = hl;
 
-/****************************************************************************
-  initialise policy handle states...
-****************************************************************************/
-void init_lsa_policy_hnd(void)
-{
-       bmap = bitmap_allocate(MAX_OPEN_POLS);
-       if (!bmap) {
-               exit_server("out of memory in init_lsa_policy_hnd\n");
-       }
+       DEBUG(10,("init_pipe_handles: pipe_handles ref count = %lu for pipe %s\n",
+                 (unsigned long)p->pipe_handles->pipe_ref_count, pipe_name ));
+
+       return True;
 }
 
 /****************************************************************************
   find first available policy slot.  creates a policy handle for you.
 ****************************************************************************/
-BOOL open_lsa_policy_hnd(POLICY_HND *hnd)
+
+bool create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *), 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"));
+       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 False;
        }
 
-       p = (struct policy *)malloc(sizeof(*p));
-       if (!p) {
-               DEBUG(0,("ERROR: out of memory!\n"));
+       pol = SMB_MALLOC_P(struct policy);
+       if (!pol) {
+               DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
                return False;
        }
 
-       ZERO_STRUCTP(p);
+       ZERO_STRUCTP(pol);
+
+       pol->data_ptr = data_ptr;
+       pol->free_fn = free_fn;
 
-       p->open = True;                         
-       p->pnum = i;
+       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 */
+
+       /* split the current time into two 16 bit values */
 
-       create_pol_hnd(hnd);
-       memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
+       SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
+       SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
 
-       bitmap_set(bmap, i);
+       SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
 
-       DLIST_ADD(Policy, p);
+       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;
 }
 
 /****************************************************************************
-  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, POLICY_HND *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;
-}
 
-/****************************************************************************
-  set samr rid
-****************************************************************************/
-BOOL set_lsa_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
+bool find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
 {
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open) {
-               DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
-                        rid, p->pnum));
-
-               p->dev.samr.rid = rid;
-               return True;
-       }
-
-       DEBUG(3,("Error setting policy rid=%x\n",rid));
-       return False;
+       return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
 }
 
-
 /****************************************************************************
-  set samr pol status.  absolutely no idea what this is.
+  Close a policy.
 ****************************************************************************/
-BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
-{
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open) {
-               DEBUG(3,("Setting policy status=%x pnum=%x\n",
-                         pol_status, p->pnum));
-
-               p->dev.samr.status = pol_status;
-               return True;
-       } 
-
-       DEBUG(3,("Error setting policy status=%x\n",
-                pol_status));
-       return False;
-}
 
-/****************************************************************************
-  set samr sid
-****************************************************************************/
-BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
+bool close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
 {
-       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));
+       struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
 
-               memcpy(&p->dev.samr.sid, sid, sizeof(*sid));
-               return True;
+       if (!pol) {
+               DEBUG(3,("Error closing policy\n"));
+               return False;
        }
 
-       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)
-{
-       struct policy *p = find_lsa_policy(hnd);
-
-       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));
+       DEBUG(3,("Closed policy\n"));
 
-               return True;
-       }
+       if (pol->free_fn && pol->data_ptr)
+               (*pol->free_fn)(pol->data_ptr);
 
-       DEBUG(3,("Error getting policy\n"));
-       return False;
-}
+       p->pipe_handles->count--;
 
-/****************************************************************************
-  get samr rid
-****************************************************************************/
-uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
-{
-       struct policy *p = find_lsa_policy(hnd);
+       DLIST_REMOVE(p->pipe_handles->Policy, pol);
 
-       if (p && p->open) {
-               uint32 rid = p->dev.samr.rid;
-               DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
-                         rid, p->pnum));
+       ZERO_STRUCTP(pol);
 
-               return rid;
-       }
+       SAFE_FREE(pol);
 
-       DEBUG(3,("Error getting policy\n"));
-       return 0xffffffff;
+       return True;
 }
 
 /****************************************************************************
-  set reg name 
+ Close a pipe - free the handle list if it was the last pipe reference.
 ****************************************************************************/
-BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
-{
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open)
-       {
-               DEBUG(3,("Getting policy pnum=%x\n",
-                        p->pnum));
-
-               fstrcpy(p->dev.reg.name, name);
-               return True;
-       }
-
-       DEBUG(3,("Error setting policy name=%s\n", name));
-       return False;
-}
 
-/****************************************************************************
-  set reg name 
-****************************************************************************/
-BOOL get_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
+void close_policy_by_pipe(pipes_struct *p)
 {
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (p && p->open)
-       {
-               DEBUG(3,("Setting policy pnum=%x name=%s\n",
-                        p->pnum, name));
+       p->pipe_handles->pipe_ref_count--;
 
-               fstrcpy(name, p->dev.reg.name);
-               DEBUG(5,("getting policy reg name=%s\n", name));
-               return True;
-       }
+       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);
 
-       DEBUG(3,("Error getting policy reg name\n"));
-       return False;
-}
+               p->pipe_handles->Policy = NULL;
+               p->pipe_handles->count = 0;
 
-/****************************************************************************
-  close an lsa policy
-****************************************************************************/
-BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
-{
-       struct policy *p = find_lsa_policy(hnd);
-
-       if (!p)
-       {
-               DEBUG(3,("Error closing policy\n"));
-               return False;
+               SAFE_FREE(p->pipe_handles);
+               DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
        }
+}
 
-       DEBUG(3,("Closed policy name pnum=%x\n",  p->pnum));
+/*******************************************************************
+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.
+********************************************************************/
 
-       DLIST_REMOVE(Policy, p);
+bool pipe_access_check(pipes_struct *p)
+{
+       /* Don't let anonymous users access this RPC if restrict
+          anonymous > 0 */
 
-       bitmap_clear(bmap, p->pnum);
+       if (lp_restrict_anonymous() > 0) {
 
-       ZERO_STRUCTP(p);
-       ZERO_STRUCTP(hnd);
+               /* schannel, so we must be ok */
+               if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
+                       return True;
+               }
 
-       free(p);
+               if (p->server_info->guest) {
+                       return False;
+               }
+       }
 
        return True;
 }
-