[862] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 |
|
---|
| 4 | Copyright (C) Rafal Szczesniak 2005
|
---|
| 5 |
|
---|
| 6 | This program is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /*
|
---|
| 21 | a composite function for getting user information via samr pipe
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "includes.h"
|
---|
| 25 | #include "libcli/composite/composite.h"
|
---|
| 26 | #include "librpc/gen_ndr/security.h"
|
---|
| 27 | #include "libcli/security/security.h"
|
---|
| 28 | #include "libnet/libnet.h"
|
---|
| 29 | #include "librpc/gen_ndr/ndr_samr_c.h"
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | struct userinfo_state {
|
---|
| 33 | struct dcerpc_pipe *pipe;
|
---|
| 34 | struct policy_handle domain_handle;
|
---|
| 35 | struct policy_handle user_handle;
|
---|
| 36 | uint16_t level;
|
---|
| 37 | struct samr_LookupNames lookup;
|
---|
| 38 | struct samr_OpenUser openuser;
|
---|
| 39 | struct samr_QueryUserInfo queryuserinfo;
|
---|
| 40 | struct samr_Close samrclose;
|
---|
| 41 | union samr_UserInfo *info;
|
---|
| 42 |
|
---|
| 43 | /* information about the progress */
|
---|
| 44 | void (*monitor_fn)(struct monitor_msg *);
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | static void continue_userinfo_lookup(struct tevent_req *subreq);
|
---|
| 49 | static void continue_userinfo_openuser(struct tevent_req *subreq);
|
---|
| 50 | static void continue_userinfo_getuser(struct tevent_req *subreq);
|
---|
| 51 | static void continue_userinfo_closeuser(struct tevent_req *subreq);
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Stage 1 (optional): Look for a username in SAM server.
|
---|
| 56 | */
|
---|
| 57 | static void continue_userinfo_lookup(struct tevent_req *subreq)
|
---|
| 58 | {
|
---|
| 59 | struct composite_context *c;
|
---|
| 60 | struct userinfo_state *s;
|
---|
| 61 | struct monitor_msg msg;
|
---|
| 62 | struct msg_rpc_lookup_name *msg_lookup;
|
---|
| 63 |
|
---|
| 64 | c = tevent_req_callback_data(subreq, struct composite_context);
|
---|
| 65 | s = talloc_get_type(c->private_data, struct userinfo_state);
|
---|
| 66 |
|
---|
| 67 | /* receive samr_Lookup reply */
|
---|
| 68 | c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
|
---|
| 69 | TALLOC_FREE(subreq);
|
---|
| 70 | if (!composite_is_ok(c)) return;
|
---|
| 71 |
|
---|
| 72 | /* there could be a problem with name resolving itself */
|
---|
| 73 | if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
|
---|
| 74 | composite_error(c, s->lookup.out.result);
|
---|
| 75 | return;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /* issue a monitor message */
|
---|
| 79 | if (s->monitor_fn) {
|
---|
| 80 | msg.type = mon_SamrLookupName;
|
---|
| 81 | msg_lookup = talloc(s, struct msg_rpc_lookup_name);
|
---|
| 82 | msg_lookup->rid = s->lookup.out.rids->ids;
|
---|
| 83 | msg_lookup->count = s->lookup.out.rids->count;
|
---|
| 84 | msg.data = (void*)msg_lookup;
|
---|
| 85 | msg.data_size = sizeof(*msg_lookup);
|
---|
| 86 |
|
---|
| 87 | s->monitor_fn(&msg);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | /* have we actually got name resolved
|
---|
| 92 | - we're looking for only one at the moment */
|
---|
| 93 | if (s->lookup.out.rids->count != s->lookup.in.num_names) {
|
---|
| 94 | composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 | if (s->lookup.out.types->count != s->lookup.in.num_names) {
|
---|
| 98 | composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /* TODO: find proper status code for more than one rid found */
|
---|
| 103 |
|
---|
| 104 | /* prepare parameters for LookupNames */
|
---|
| 105 | s->openuser.in.domain_handle = &s->domain_handle;
|
---|
| 106 | s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
|
---|
| 107 | s->openuser.in.rid = s->lookup.out.rids->ids[0];
|
---|
| 108 | s->openuser.out.user_handle = &s->user_handle;
|
---|
| 109 |
|
---|
| 110 | /* send request */
|
---|
| 111 | subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
|
---|
| 112 | s->pipe->binding_handle,
|
---|
| 113 | &s->openuser);
|
---|
| 114 | if (composite_nomem(subreq, c)) return;
|
---|
| 115 |
|
---|
| 116 | tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Stage 2: Open user policy handle.
|
---|
| 122 | */
|
---|
| 123 | static void continue_userinfo_openuser(struct tevent_req *subreq)
|
---|
| 124 | {
|
---|
| 125 | struct composite_context *c;
|
---|
| 126 | struct userinfo_state *s;
|
---|
| 127 | struct monitor_msg msg;
|
---|
| 128 | struct msg_rpc_open_user *msg_open;
|
---|
| 129 |
|
---|
| 130 | c = tevent_req_callback_data(subreq, struct composite_context);
|
---|
| 131 | s = talloc_get_type(c->private_data, struct userinfo_state);
|
---|
| 132 |
|
---|
| 133 | /* receive samr_OpenUser reply */
|
---|
| 134 | c->status = dcerpc_samr_OpenUser_r_recv(subreq, s);
|
---|
| 135 | TALLOC_FREE(subreq);
|
---|
| 136 | if (!composite_is_ok(c)) return;
|
---|
| 137 |
|
---|
| 138 | if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
|
---|
| 139 | composite_error(c, s->queryuserinfo.out.result);
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /* issue a monitor message */
|
---|
| 144 | if (s->monitor_fn) {
|
---|
| 145 | msg.type = mon_SamrOpenUser;
|
---|
| 146 | msg_open = talloc(s, struct msg_rpc_open_user);
|
---|
| 147 | msg_open->rid = s->openuser.in.rid;
|
---|
| 148 | msg_open->access_mask = s->openuser.in.access_mask;
|
---|
| 149 | msg.data = (void*)msg_open;
|
---|
| 150 | msg.data_size = sizeof(*msg_open);
|
---|
| 151 |
|
---|
| 152 | s->monitor_fn(&msg);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /* prepare parameters for QueryUserInfo call */
|
---|
| 156 | s->queryuserinfo.in.user_handle = &s->user_handle;
|
---|
| 157 | s->queryuserinfo.in.level = s->level;
|
---|
| 158 | s->queryuserinfo.out.info = talloc(s, union samr_UserInfo *);
|
---|
| 159 | if (composite_nomem(s->queryuserinfo.out.info, c)) return;
|
---|
| 160 |
|
---|
| 161 | /* queue rpc call, set event handling and new state */
|
---|
| 162 | subreq = dcerpc_samr_QueryUserInfo_r_send(s, c->event_ctx,
|
---|
| 163 | s->pipe->binding_handle,
|
---|
| 164 | &s->queryuserinfo);
|
---|
| 165 | if (composite_nomem(subreq, c)) return;
|
---|
| 166 |
|
---|
| 167 | tevent_req_set_callback(subreq, continue_userinfo_getuser, c);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Stage 3: Get requested user information.
|
---|
| 173 | */
|
---|
| 174 | static void continue_userinfo_getuser(struct tevent_req *subreq)
|
---|
| 175 | {
|
---|
| 176 | struct composite_context *c;
|
---|
| 177 | struct userinfo_state *s;
|
---|
| 178 | struct monitor_msg msg;
|
---|
| 179 | struct msg_rpc_query_user *msg_query;
|
---|
| 180 |
|
---|
| 181 | c = tevent_req_callback_data(subreq, struct composite_context);
|
---|
| 182 | s = talloc_get_type(c->private_data, struct userinfo_state);
|
---|
| 183 |
|
---|
| 184 | /* receive samr_QueryUserInfo reply */
|
---|
| 185 | c->status = dcerpc_samr_QueryUserInfo_r_recv(subreq, s);
|
---|
| 186 | TALLOC_FREE(subreq);
|
---|
| 187 | if (!composite_is_ok(c)) return;
|
---|
| 188 |
|
---|
| 189 | /* check if queryuser itself went ok */
|
---|
| 190 | if (!NT_STATUS_IS_OK(s->queryuserinfo.out.result)) {
|
---|
| 191 | composite_error(c, s->queryuserinfo.out.result);
|
---|
| 192 | return;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | s->info = talloc_steal(s, *(s->queryuserinfo.out.info));
|
---|
| 196 |
|
---|
| 197 | /* issue a monitor message */
|
---|
| 198 | if (s->monitor_fn) {
|
---|
| 199 | msg.type = mon_SamrQueryUser;
|
---|
| 200 | msg_query = talloc(s, struct msg_rpc_query_user);
|
---|
| 201 | msg_query->level = s->queryuserinfo.in.level;
|
---|
| 202 | msg.data = (void*)msg_query;
|
---|
| 203 | msg.data_size = sizeof(*msg_query);
|
---|
| 204 |
|
---|
| 205 | s->monitor_fn(&msg);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* prepare arguments for Close call */
|
---|
| 209 | s->samrclose.in.handle = &s->user_handle;
|
---|
| 210 | s->samrclose.out.handle = &s->user_handle;
|
---|
| 211 |
|
---|
| 212 | /* queue rpc call, set event handling and new state */
|
---|
| 213 | subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
|
---|
| 214 | s->pipe->binding_handle,
|
---|
| 215 | &s->samrclose);
|
---|
| 216 | if (composite_nomem(subreq, c)) return;
|
---|
| 217 |
|
---|
| 218 | tevent_req_set_callback(subreq, continue_userinfo_closeuser, c);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
| 223 | * Stage 4: Close policy handle associated with opened user.
|
---|
| 224 | */
|
---|
| 225 | static void continue_userinfo_closeuser(struct tevent_req *subreq)
|
---|
| 226 | {
|
---|
| 227 | struct composite_context *c;
|
---|
| 228 | struct userinfo_state *s;
|
---|
| 229 | struct monitor_msg msg;
|
---|
| 230 | struct msg_rpc_close_user *msg_close;
|
---|
| 231 |
|
---|
| 232 | c = tevent_req_callback_data(subreq, struct composite_context);
|
---|
| 233 | s = talloc_get_type(c->private_data, struct userinfo_state);
|
---|
| 234 |
|
---|
| 235 | /* receive samr_Close reply */
|
---|
| 236 | c->status = dcerpc_samr_Close_r_recv(subreq, s);
|
---|
| 237 | TALLOC_FREE(subreq);
|
---|
| 238 | if (!composite_is_ok(c)) return;
|
---|
| 239 |
|
---|
| 240 | if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
|
---|
| 241 | composite_error(c, s->samrclose.out.result);
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /* issue a monitor message */
|
---|
| 246 | if (s->monitor_fn) {
|
---|
| 247 | msg.type = mon_SamrClose;
|
---|
| 248 | msg_close = talloc(s, struct msg_rpc_close_user);
|
---|
| 249 | msg_close->rid = s->openuser.in.rid;
|
---|
| 250 | msg.data = (void*)msg_close;
|
---|
| 251 | msg.data_size = sizeof(*msg_close);
|
---|
| 252 |
|
---|
| 253 | s->monitor_fn(&msg);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | composite_done(c);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * Sends asynchronous userinfo request
|
---|
| 262 | *
|
---|
| 263 | * @param p dce/rpc call pipe
|
---|
| 264 | * @param io arguments and results of the call
|
---|
| 265 | */
|
---|
| 266 | struct composite_context *libnet_rpc_userinfo_send(struct dcerpc_pipe *p,
|
---|
| 267 | struct libnet_rpc_userinfo *io,
|
---|
| 268 | void (*monitor)(struct monitor_msg*))
|
---|
| 269 | {
|
---|
| 270 | struct composite_context *c;
|
---|
| 271 | struct userinfo_state *s;
|
---|
| 272 | struct dom_sid *sid;
|
---|
| 273 | struct tevent_req *subreq;
|
---|
| 274 |
|
---|
| 275 | if (!p || !io) return NULL;
|
---|
| 276 |
|
---|
| 277 | c = composite_create(p, dcerpc_event_context(p));
|
---|
| 278 | if (c == NULL) return c;
|
---|
| 279 |
|
---|
| 280 | s = talloc_zero(c, struct userinfo_state);
|
---|
| 281 | if (composite_nomem(s, c)) return c;
|
---|
| 282 |
|
---|
| 283 | c->private_data = s;
|
---|
| 284 |
|
---|
| 285 | s->level = io->in.level;
|
---|
| 286 | s->pipe = p;
|
---|
| 287 | s->domain_handle = io->in.domain_handle;
|
---|
| 288 | s->monitor_fn = monitor;
|
---|
| 289 |
|
---|
| 290 | if (io->in.sid) {
|
---|
| 291 | sid = dom_sid_parse_talloc(s, io->in.sid);
|
---|
| 292 | if (composite_nomem(sid, c)) return c;
|
---|
| 293 |
|
---|
| 294 | s->openuser.in.domain_handle = &s->domain_handle;
|
---|
| 295 | s->openuser.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
|
---|
| 296 | s->openuser.in.rid = sid->sub_auths[sid->num_auths - 1];
|
---|
| 297 | s->openuser.out.user_handle = &s->user_handle;
|
---|
| 298 |
|
---|
| 299 | /* send request */
|
---|
| 300 | subreq = dcerpc_samr_OpenUser_r_send(s, c->event_ctx,
|
---|
| 301 | p->binding_handle,
|
---|
| 302 | &s->openuser);
|
---|
| 303 | if (composite_nomem(subreq, c)) return c;
|
---|
| 304 |
|
---|
| 305 | tevent_req_set_callback(subreq, continue_userinfo_openuser, c);
|
---|
| 306 |
|
---|
| 307 | } else {
|
---|
| 308 | /* preparing parameters to send rpc request */
|
---|
| 309 | s->lookup.in.domain_handle = &s->domain_handle;
|
---|
| 310 | s->lookup.in.num_names = 1;
|
---|
| 311 | s->lookup.in.names = talloc_array(s, struct lsa_String, 1);
|
---|
| 312 | if (composite_nomem(s->lookup.in.names, c)) return c;
|
---|
| 313 | s->lookup.out.rids = talloc_zero(s, struct samr_Ids);
|
---|
| 314 | s->lookup.out.types = talloc_zero(s, struct samr_Ids);
|
---|
| 315 | if (composite_nomem(s->lookup.out.rids, c)) return c;
|
---|
| 316 | if (composite_nomem(s->lookup.out.types, c)) return c;
|
---|
| 317 |
|
---|
| 318 | s->lookup.in.names[0].string = talloc_strdup(s, io->in.username);
|
---|
| 319 | if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
|
---|
| 320 |
|
---|
| 321 | /* send request */
|
---|
| 322 | subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
|
---|
| 323 | p->binding_handle,
|
---|
| 324 | &s->lookup);
|
---|
| 325 | if (composite_nomem(subreq, c)) return c;
|
---|
| 326 |
|
---|
| 327 | tevent_req_set_callback(subreq, continue_userinfo_lookup, c);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | return c;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 | /**
|
---|
| 335 | * Waits for and receives result of asynchronous userinfo call
|
---|
| 336 | *
|
---|
| 337 | * @param c composite context returned by asynchronous userinfo call
|
---|
| 338 | * @param mem_ctx memory context of the call
|
---|
| 339 | * @param io pointer to results (and arguments) of the call
|
---|
| 340 | * @return nt status code of execution
|
---|
| 341 | */
|
---|
| 342 |
|
---|
| 343 | NTSTATUS libnet_rpc_userinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
|
---|
| 344 | struct libnet_rpc_userinfo *io)
|
---|
| 345 | {
|
---|
| 346 | NTSTATUS status;
|
---|
| 347 | struct userinfo_state *s;
|
---|
| 348 |
|
---|
| 349 | /* wait for results of sending request */
|
---|
| 350 | status = composite_wait(c);
|
---|
| 351 |
|
---|
| 352 | if (NT_STATUS_IS_OK(status) && io) {
|
---|
| 353 | s = talloc_get_type(c->private_data, struct userinfo_state);
|
---|
| 354 | talloc_steal(mem_ctx, s->info);
|
---|
| 355 | io->out.info = *s->info;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /* memory context associated to composite context is no longer needed */
|
---|
| 359 | talloc_free(c);
|
---|
| 360 | return status;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 |
|
---|
| 364 | /**
|
---|
| 365 | * Synchronous version of userinfo call
|
---|
| 366 | *
|
---|
| 367 | * @param pipe dce/rpc call pipe
|
---|
| 368 | * @param mem_ctx memory context for the call
|
---|
| 369 | * @param io arguments and results of the call
|
---|
| 370 | * @return nt status code of execution
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | NTSTATUS libnet_rpc_userinfo(struct dcerpc_pipe *p,
|
---|
| 374 | TALLOC_CTX *mem_ctx,
|
---|
| 375 | struct libnet_rpc_userinfo *io)
|
---|
| 376 | {
|
---|
| 377 | struct composite_context *c = libnet_rpc_userinfo_send(p, io, NULL);
|
---|
| 378 | return libnet_rpc_userinfo_recv(c, mem_ctx, io);
|
---|
| 379 | }
|
---|