-
/*
- * 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;
+/*
+ * Handle database - stored per pipe.
+ */
-#ifndef MAX_OPEN_POLS
-#define MAX_OPEN_POLS 64
-#endif
+struct policy {
+ struct policy *next, *prev;
-#define POL_NO_INFO 0
-#define POL_REG_INFO 1
-#define POL_SAMR_INFO 2
-#define POL_CLI_INFO 3
+ struct policy_handle pol_hnd;
-struct reg_info
-{
- /* for use by \PIPE\winreg */
- fstring name; /* name of registry key */
-};
+ uint32_t access_granted;
-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 */
+ void *data_ptr;
};
-struct cli_info
-{
- struct cli_state *cli;
- uint16 fnum;
- void (*free)(struct cli_state*, uint16 fnum);
+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. */
};
-static struct policy
-{
- struct policy *next, *prev;
- int pnum;
- BOOL open;
- POLICY_HND pol_hnd;
- int type;
-
- union {
- struct samr_info *samr;
- struct reg_info *reg;
- struct cli_info *cli;
-
- } dev;
-
-} *Policy;
-
-static struct bitmap *bmap;
-
+/* This is the max handles across all instances of a pipe name. */
+#ifndef MAX_OPEN_POLS
+#define MAX_OPEN_POLS 2048
+#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++;
-
- 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 */
+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));
}
-/****************************************************************************
- initialise policy handle states...
-****************************************************************************/
-BOOL init_policy_hnd(int num_pol_hnds)
+size_t num_pipe_handles(struct handle_list *list)
{
- bmap = bitmap_allocate(num_pol_hnds);
-
- return bmap != NULL;
+ if (list == NULL) {
+ return 0;
+ }
+ return list->count;
}
/****************************************************************************
- find first available policy slot. creates a policy handle for you.
+ Initialise a policy handle list on a pipe. Handle list is shared between all
+ pipes of the same name.
****************************************************************************/
-BOOL register_policy_hnd(POLICY_HND *hnd)
-{
- int i;
- struct policy *p;
- i = bitmap_find(bmap, 1);
+bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
+{
+ pipes_struct *plist;
+ struct handle_list *hl;
- if (i == -1) {
- DEBUG(0,("ERROR: out of Policy Handles!\n"));
- return False;
+ 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;
+ }
}
- p = (struct policy *)malloc(sizeof(*p));
- if (!p) {
- DEBUG(0,("ERROR: out of memory!\n"));
- return False;
+ 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)));
}
- ZERO_STRUCTP(p);
+ /*
+ * One more pipe is using this list.
+ */
- p->open = True;
- p->pnum = i;
- p->type = POL_NO_INFO;
+ hl->pipe_ref_count++;
- memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
+ /*
+ * Point this pipe at this list.
+ */
- bitmap_set(bmap, i);
+ p->pipe_handles = hl;
- DLIST_ADD(Policy, p);
-
- DEBUG(4,("Opened policy hnd[%x] ", i));
- dump_data(4, (char *)hnd->data, sizeof(hnd->data));
+ 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.
-****************************************************************************/
-BOOL open_policy_hnd(POLICY_HND *hnd)
-{
- create_pol_hnd(hnd);
- return register_policy_hnd(hnd);
-}
-/****************************************************************************
- find policy by handle
+ 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
****************************************************************************/
-static struct policy *find_policy(const POLICY_HND *hnd)
+
+static struct policy *create_policy_hnd_internal(pipes_struct *p,
+ struct policy_handle *hnd,
+ void *data_ptr)
{
- 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, (const char *)hnd->data,
- sizeof(hnd->data));
- return p;
- }
- }
+ static uint32 pol_hnd_low = 0;
+ static uint32 pol_hnd_high = 0;
+ time_t t = time(NULL);
- DEBUG(4,("Policy not found: "));
- dump_data(4, (const char *)hnd->data, sizeof(hnd->data));
+ struct policy *pol;
- return NULL;
-}
+ 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;
+ }
-/****************************************************************************
- find policy index by handle
-****************************************************************************/
-int find_policy_by_hnd(const POLICY_HND *hnd)
-{
- struct policy *p = find_policy(hnd);
+ pol = TALLOC_ZERO_P(NULL, struct policy);
+ if (!pol) {
+ DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
+ return NULL;
+ }
- return p?p->pnum:-1;
-}
+ if (data_ptr != NULL) {
+ pol->data_ptr = talloc_move(pol, &data_ptr);
+ }
-/****************************************************************************
- set samr rid
-****************************************************************************/
-BOOL set_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
-{
- struct policy *p = find_policy(hnd);
+ pol_hnd_low++;
+ if (pol_hnd_low == 0)
+ (pol_hnd_high)++;
- if (p && p->open)
- {
- DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
- rid, p->pnum));
+ 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 */
- if (p->dev.samr == NULL)
- {
- p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
- }
- if (p->dev.samr == NULL)
- {
- return False;
- }
- p->dev.samr->rid = rid;
- return True;
- }
+ /* split the current time into two 16 bit values */
- DEBUG(3,("Error setting policy rid=%x\n",rid));
- return False;
-}
+ 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 */
-/****************************************************************************
- set samr pol status. absolutely no idea what this is.
-****************************************************************************/
-BOOL set_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
-{
- struct policy *p = find_policy(hnd);
+ DLIST_ADD(p->pipe_handles->Policy, pol);
+ p->pipe_handles->count++;
- if (p && p->open)
- {
- DEBUG(3,("Setting policy status=%x pnum=%x\n",
- pol_status, p->pnum));
+ *hnd = pol->pol_hnd;
+
+ DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
+ dump_data(4, (uint8 *)hnd, sizeof(*hnd));
- if (p->dev.samr == NULL)
- {
- p->type = POL_SAMR_INFO;
- p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
- }
- if (p->dev.samr == NULL)
- {
- return False;
- }
- p->dev.samr->status = pol_status;
- return True;
- }
+ return pol;
+}
- DEBUG(3,("Error setting policy status=%x\n",
- pol_status));
- return False;
+bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
+ void *data_ptr)
+{
+ return create_policy_hnd_internal(p, hnd, data_ptr) != NULL;
}
/****************************************************************************
- set samr sid
+ find policy by handle - internal version.
****************************************************************************/
-BOOL set_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
-{
- pstring sidstr;
- struct policy *p = find_policy(hnd);
- if (p && p->open) {
- DEBUG(3,("Setting policy sid=%s pnum=%x\n",
- sid_to_string(sidstr, sid), p->pnum));
-
- if (p->dev.samr == NULL)
- {
- p->type = POL_SAMR_INFO;
- p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
- }
- if (p->dev.samr == NULL)
- {
- return False;
+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;
}
- 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;
+ DEBUG(4,("Policy not found: "));
+ dump_data(4, (uint8 *)hnd, sizeof(*hnd));
+
+ p->bad_handle_fault_state = True;
+
+ return NULL;
}
/****************************************************************************
- get samr sid
+ find policy by handle
****************************************************************************/
-BOOL get_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
-{
- struct policy *p = find_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));
- return True;
- }
-
- DEBUG(3,("Error getting policy\n"));
- return False;
+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;
}
/****************************************************************************
- get samr rid
+ Close a policy.
****************************************************************************/
-uint32 get_policy_samr_rid(POLICY_HND *hnd)
-{
- struct policy *p = find_policy(hnd);
- if (p && p->open) {
- uint32 rid = p->dev.samr->rid;
- DEBUG(3,("Getting 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);
- return rid;
+ if (!pol) {
+ DEBUG(3,("Error closing policy\n"));
+ return False;
}
- DEBUG(3,("Error getting policy\n"));
- return 0xffffffff;
-}
+ DEBUG(3,("Closed policy\n"));
-/****************************************************************************
- set reg name
-****************************************************************************/
-BOOL set_policy_reg_name(POLICY_HND *hnd, fstring name)
-{
- struct policy *p = find_policy(hnd);
+ p->pipe_handles->count--;
- if (p && p->open)
- {
- DEBUG(3,("Getting policy pnum=%x\n",
- p->pnum));
+ DLIST_REMOVE(p->pipe_handles->Policy, pol);
- if (p->dev.reg == NULL)
- {
- p->type = POL_REG_INFO;
- p->dev.reg = (struct reg_info*)malloc(sizeof(*p->dev.reg));
- }
- if (p->dev.reg == NULL)
- {
- return False;
- }
- fstrcpy(p->dev.reg->name, name);
- return True;
- }
+ TALLOC_FREE(pol);
- DEBUG(3,("Error setting policy name=%s\n", name));
- return False;
+ return True;
}
/****************************************************************************
- set reg name
+ Close a pipe - free the handle list if it was the last pipe reference.
****************************************************************************/
-BOOL get_policy_reg_name(POLICY_HND *hnd, fstring name)
+
+void close_policy_by_pipe(pipes_struct *p)
{
- struct policy *p = find_policy(hnd);
+ p->pipe_handles->pipe_ref_count--;
- if (p && p->open)
- {
- DEBUG(3,("Setting policy pnum=%x name=%s\n",
- p->pnum, name));
+ 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);
- fstrcpy(name, p->dev.reg->name);
- DEBUG(5,("getting policy reg name=%s\n", name));
- return True;
- }
+ p->pipe_handles->Policy = NULL;
+ p->pipe_handles->count = 0;
- DEBUG(3,("Error getting policy reg name\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)));
+ }
}
-/****************************************************************************
- set cli state
-****************************************************************************/
-BOOL set_policy_cli_state(POLICY_HND *hnd, struct cli_state *cli, uint16 fnum,
- void (*free_fn)(struct cli_state *, uint16))
+/*******************************************************************
+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_policy(hnd);
+ /* Don't let anonymous users access this RPC if restrict
+ anonymous > 0 */
- if (p && p->open)
- {
- DEBUG(3,("Setting policy cli state pnum=%x\n", p->pnum));
+ if (lp_restrict_anonymous() > 0) {
- if (p->dev.cli == NULL)
- {
- p->type = POL_CLI_INFO;
- p->dev.cli = (struct cli_info*)malloc(sizeof(*p->dev.cli));
+ /* schannel, so we must be ok */
+ if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
+ return True;
}
- if (p->dev.cli == NULL)
- {
+
+ if (p->server_info->guest) {
return False;
}
- p->dev.cli->cli = cli;
- p->dev.cli->free = free_fn;
- p->dev.cli->fnum = fnum;
- return True;
}
- DEBUG(3,("Error setting policy cli state\n"));
-
- return False;
+ return True;
}
-/****************************************************************************
- get cli state
-****************************************************************************/
-BOOL get_policy_cli_state(const POLICY_HND *hnd, struct cli_state **cli,
- uint16 *fnum)
+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_policy(hnd);
-
- if (p != NULL && p->open)
- {
- DEBUG(3,("Getting cli state pnum=%x\n", p->pnum));
-
- if (cli != NULL)
- {
- (*cli ) = p->dev.cli->cli;
- }
- if (fnum != NULL)
- {
- (*fnum) = p->dev.cli->fnum;
- }
+ 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;
+ }
- 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 getting policy\n"));
- 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_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_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;
}
-
- 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);
-
- switch (p->type)
- {
- case POL_REG_INFO:
- {
- free(p->dev.reg);
- break;
- }
- case POL_SAMR_INFO:
- {
- free(p->dev.samr);
- break;
- }
- case POL_CLI_INFO:
- {
- if (p->dev.cli->free != NULL)
- {
- p->dev.cli->free(p->dev.cli->cli,
- p->dev.cli->fnum);
- }
- free(p->dev.cli);
- break;
+ 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;
}
- 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;
}
-