| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * RPC Pipe client / server routines
|
|---|
| 4 | * Copyright (C) Andrew Tridgell 1992-1997,
|
|---|
| 5 | * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
|
|---|
| 6 | * Copyright (C) Jeremy Allison 2001.
|
|---|
| 7 | *
|
|---|
| 8 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | * it under the terms of the GNU General Public License as published by
|
|---|
| 10 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | * (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This program is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | * GNU General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU General Public License
|
|---|
| 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | #undef DBGC_CLASS
|
|---|
| 25 | #define DBGC_CLASS DBGC_RPC_SRV
|
|---|
| 26 |
|
|---|
| 27 | /* This is the max handles across all instances of a pipe name. */
|
|---|
| 28 | #ifndef MAX_OPEN_POLS
|
|---|
| 29 | #define MAX_OPEN_POLS 1024
|
|---|
| 30 | #endif
|
|---|
| 31 |
|
|---|
| 32 | /****************************************************************************
|
|---|
| 33 | Hack as handles need to be persisant over lsa pipe closes so long as a samr
|
|---|
| 34 | pipe is open. JRA.
|
|---|
| 35 | ****************************************************************************/
|
|---|
| 36 |
|
|---|
| 37 | static bool is_samr_lsa_pipe(const char *pipe_name)
|
|---|
| 38 | {
|
|---|
| 39 | return (strstr(pipe_name, "samr") || strstr(pipe_name, "lsa"));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /****************************************************************************
|
|---|
| 43 | Initialise a policy handle list on a pipe. Handle list is shared between all
|
|---|
| 44 | pipes of the same name.
|
|---|
| 45 | ****************************************************************************/
|
|---|
| 46 |
|
|---|
| 47 | bool init_pipe_handle_list(pipes_struct *p, const char *pipe_name)
|
|---|
| 48 | {
|
|---|
| 49 | pipes_struct *plist = get_first_internal_pipe();
|
|---|
| 50 | struct handle_list *hl = NULL;
|
|---|
| 51 |
|
|---|
| 52 | for (plist = get_first_internal_pipe(); plist; plist = get_next_internal_pipe(plist)) {
|
|---|
| 53 | if (strequal( plist->name, pipe_name) ||
|
|---|
| 54 | (is_samr_lsa_pipe(plist->name) && is_samr_lsa_pipe(pipe_name))) {
|
|---|
| 55 | if (!plist->pipe_handles) {
|
|---|
| 56 | char *msg;
|
|---|
| 57 | if (asprintf(&msg, "init_pipe_handles: NULL "
|
|---|
| 58 | "pipe_handle pointer in pipe %s",
|
|---|
| 59 | pipe_name) != -1) {
|
|---|
| 60 | smb_panic(msg);
|
|---|
| 61 | } else {
|
|---|
| 62 | smb_panic("init_pipe_handle_list");
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | hl = plist->pipe_handles;
|
|---|
| 66 | break;
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (!hl) {
|
|---|
| 71 | /*
|
|---|
| 72 | * No handle list for this pipe (first open of pipe).
|
|---|
| 73 | * Create list.
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | if ((hl = SMB_MALLOC_P(struct handle_list)) == NULL)
|
|---|
| 77 | return False;
|
|---|
| 78 | ZERO_STRUCTP(hl);
|
|---|
| 79 |
|
|---|
| 80 | DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /*
|
|---|
| 84 | * One more pipe is using this list.
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | hl->pipe_ref_count++;
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | * Point this pipe at this list.
|
|---|
| 91 | */
|
|---|
| 92 |
|
|---|
| 93 | p->pipe_handles = hl;
|
|---|
| 94 |
|
|---|
| 95 | DEBUG(10,("init_pipe_handles: pipe_handles ref count = %lu for pipe %s\n",
|
|---|
| 96 | (unsigned long)p->pipe_handles->pipe_ref_count, pipe_name ));
|
|---|
| 97 |
|
|---|
| 98 | return True;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /****************************************************************************
|
|---|
| 102 | find first available policy slot. creates a policy handle for you.
|
|---|
| 103 | ****************************************************************************/
|
|---|
| 104 |
|
|---|
| 105 | bool create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *), void *data_ptr)
|
|---|
| 106 | {
|
|---|
| 107 | static uint32 pol_hnd_low = 0;
|
|---|
| 108 | static uint32 pol_hnd_high = 0;
|
|---|
| 109 | time_t t = time(NULL);
|
|---|
| 110 |
|
|---|
| 111 | struct policy *pol;
|
|---|
| 112 |
|
|---|
| 113 | if (p->pipe_handles->count > MAX_OPEN_POLS) {
|
|---|
| 114 | DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
|
|---|
| 115 | (int)p->pipe_handles->count));
|
|---|
| 116 | return False;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | pol = SMB_MALLOC_P(struct policy);
|
|---|
| 120 | if (!pol) {
|
|---|
| 121 | DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
|
|---|
| 122 | return False;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | ZERO_STRUCTP(pol);
|
|---|
| 126 |
|
|---|
| 127 | pol->data_ptr = data_ptr;
|
|---|
| 128 | pol->free_fn = free_fn;
|
|---|
| 129 |
|
|---|
| 130 | pol_hnd_low++;
|
|---|
| 131 | if (pol_hnd_low == 0)
|
|---|
| 132 | (pol_hnd_high)++;
|
|---|
| 133 |
|
|---|
| 134 | SIVAL(&pol->pol_hnd.handle_type, 0 , 0); /* first bit must be null */
|
|---|
| 135 | SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
|
|---|
| 136 | SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
|
|---|
| 137 | SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
|
|---|
| 138 |
|
|---|
| 139 | /* split the current time into two 16 bit values */
|
|---|
| 140 |
|
|---|
| 141 | SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
|
|---|
| 142 | SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
|
|---|
| 143 |
|
|---|
| 144 | SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
|
|---|
| 145 |
|
|---|
| 146 | DLIST_ADD(p->pipe_handles->Policy, pol);
|
|---|
| 147 | p->pipe_handles->count++;
|
|---|
| 148 |
|
|---|
| 149 | *hnd = pol->pol_hnd;
|
|---|
| 150 |
|
|---|
| 151 | DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
|
|---|
| 152 | dump_data(4, (uint8 *)hnd, sizeof(*hnd));
|
|---|
| 153 |
|
|---|
| 154 | return True;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /****************************************************************************
|
|---|
| 158 | find policy by handle - internal version.
|
|---|
| 159 | ****************************************************************************/
|
|---|
| 160 |
|
|---|
| 161 | static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *hnd, void **data_p)
|
|---|
| 162 | {
|
|---|
| 163 | struct policy *pol;
|
|---|
| 164 | size_t i;
|
|---|
| 165 |
|
|---|
| 166 | if (data_p)
|
|---|
| 167 | *data_p = NULL;
|
|---|
| 168 |
|
|---|
| 169 | for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
|
|---|
| 170 | if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
|
|---|
| 171 | DEBUG(4,("Found policy hnd[%d] ", (int)i));
|
|---|
| 172 | dump_data(4, (uint8 *)hnd, sizeof(*hnd));
|
|---|
| 173 | if (data_p)
|
|---|
| 174 | *data_p = pol->data_ptr;
|
|---|
| 175 | return pol;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | DEBUG(4,("Policy not found: "));
|
|---|
| 180 | dump_data(4, (uint8 *)hnd, sizeof(*hnd));
|
|---|
| 181 |
|
|---|
| 182 | p->bad_handle_fault_state = True;
|
|---|
| 183 |
|
|---|
| 184 | return NULL;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /****************************************************************************
|
|---|
| 188 | find policy by handle
|
|---|
| 189 | ****************************************************************************/
|
|---|
| 190 |
|
|---|
| 191 | bool find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
|
|---|
| 192 | {
|
|---|
| 193 | return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /****************************************************************************
|
|---|
| 197 | Close a policy.
|
|---|
| 198 | ****************************************************************************/
|
|---|
| 199 |
|
|---|
| 200 | bool close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
|
|---|
| 201 | {
|
|---|
| 202 | struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
|
|---|
| 203 |
|
|---|
| 204 | if (!pol) {
|
|---|
| 205 | DEBUG(3,("Error closing policy\n"));
|
|---|
| 206 | return False;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | DEBUG(3,("Closed policy\n"));
|
|---|
| 210 |
|
|---|
| 211 | if (pol->free_fn && pol->data_ptr)
|
|---|
| 212 | (*pol->free_fn)(pol->data_ptr);
|
|---|
| 213 |
|
|---|
| 214 | p->pipe_handles->count--;
|
|---|
| 215 |
|
|---|
| 216 | DLIST_REMOVE(p->pipe_handles->Policy, pol);
|
|---|
| 217 |
|
|---|
| 218 | ZERO_STRUCTP(pol);
|
|---|
| 219 |
|
|---|
| 220 | SAFE_FREE(pol);
|
|---|
| 221 |
|
|---|
| 222 | return True;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /****************************************************************************
|
|---|
| 226 | Close a pipe - free the handle list if it was the last pipe reference.
|
|---|
| 227 | ****************************************************************************/
|
|---|
| 228 |
|
|---|
| 229 | void close_policy_by_pipe(pipes_struct *p)
|
|---|
| 230 | {
|
|---|
| 231 | p->pipe_handles->pipe_ref_count--;
|
|---|
| 232 |
|
|---|
| 233 | if (p->pipe_handles->pipe_ref_count == 0) {
|
|---|
| 234 | /*
|
|---|
| 235 | * Last pipe open on this list - free the list.
|
|---|
| 236 | */
|
|---|
| 237 | while (p->pipe_handles->Policy)
|
|---|
| 238 | close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
|
|---|
| 239 |
|
|---|
| 240 | p->pipe_handles->Policy = NULL;
|
|---|
| 241 | p->pipe_handles->count = 0;
|
|---|
| 242 |
|
|---|
| 243 | SAFE_FREE(p->pipe_handles);
|
|---|
| 244 | DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /*******************************************************************
|
|---|
| 249 | Shall we allow access to this rpc? Currently this function
|
|---|
| 250 | implements the 'restrict anonymous' setting by denying access to
|
|---|
| 251 | anonymous users if the restrict anonymous level is > 0. Further work
|
|---|
| 252 | will be checking a security descriptor to determine whether a user
|
|---|
| 253 | token has enough access to access the pipe.
|
|---|
| 254 | ********************************************************************/
|
|---|
| 255 |
|
|---|
| 256 | bool pipe_access_check(pipes_struct *p)
|
|---|
| 257 | {
|
|---|
| 258 | /* Don't let anonymous users access this RPC if restrict
|
|---|
| 259 | anonymous > 0 */
|
|---|
| 260 |
|
|---|
| 261 | if (lp_restrict_anonymous() > 0) {
|
|---|
| 262 |
|
|---|
| 263 | /* schannel, so we must be ok */
|
|---|
| 264 | if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
|
|---|
| 265 | return True;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | if (p->server_info->guest) {
|
|---|
| 269 | return False;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | return True;
|
|---|
| 274 | }
|
|---|