libnet_domain.c
来自「samba最新软件」· C语言 代码 · 共 1,253 行 · 第 1/3 页
C
1,253 行
/* store arguments in the state structure */ s->name = talloc_strdup(c, io->in.domain_name); s->access_mask = io->in.access_mask; s->ctx = ctx; /* check, if there's lsa pipe opened already, before opening a handle */ if (ctx->lsa.pipe == NULL) { /* attempting to connect a domain controller */ s->rpcconn.level = LIBNET_RPC_CONNECT_DC; s->rpcconn.in.name = talloc_strdup(c, io->in.domain_name); s->rpcconn.in.dcerpc_iface = &ndr_table_lsarpc; /* send rpc pipe connect request */ rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn); if (composite_nomem(rpcconn_req, c)) return c; composite_continue(c, rpcconn_req, continue_rpc_connect_lsa, c); return c; } s->pipe = ctx->lsa.pipe; /* preparing parameters for lsa_OpenPolicy2 rpc call */ s->openpol.in.system_name = s->name; s->openpol.in.access_mask = s->access_mask; s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute); qos = talloc_zero(c, struct lsa_QosInfo); qos->len = 0; qos->impersonation_level = 2; qos->context_mode = 1; qos->effective_only = 0; s->openpol.in.attr->sec_qos = qos; s->openpol.out.handle = &s->handle; /* send rpc request */ openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol); if (composite_nomem(openpol_req, c)) return c; composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c); return c;}/* Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request */static void continue_rpc_connect_lsa(struct composite_context *ctx){ struct composite_context *c; struct domain_open_lsa_state *s; struct lsa_QosInfo *qos; struct rpc_request *openpol_req; c = talloc_get_type(ctx->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct domain_open_lsa_state); /* receive rpc connection */ c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn); if (!composite_is_ok(c)) return; /* RpcConnect function leaves the pipe in libnet context, so get it from there */ s->pipe = s->ctx->lsa.pipe; /* prepare lsa_OpenPolicy2 call */ s->openpol.in.system_name = s->name; s->openpol.in.access_mask = s->access_mask; s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute); qos = talloc_zero(c, struct lsa_QosInfo); qos->len = 0; qos->impersonation_level = 2; qos->context_mode = 1; qos->effective_only = 0; s->openpol.in.attr->sec_qos = qos; s->openpol.out.handle = &s->handle; /* send rpc request */ openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol); if (composite_nomem(openpol_req, c)) return; composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);}/* Stage 1: Lsa policy opened - we're done, if successfully */static void continue_lsa_policy_open(struct rpc_request *req){ struct composite_context *c; struct domain_open_lsa_state *s; c = talloc_get_type(req->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct domain_open_lsa_state); c->status = dcerpc_ndr_request_recv(req); if (!composite_is_ok(c)) return; if (s->monitor_fn) { struct monitor_msg msg; msg.type = mon_LsaOpenPolicy; msg.data = NULL; msg.data_size = 0; s->monitor_fn(&msg); } composite_done(c);}/** * Receives result of asynchronous DomainOpenLsa call * * @param c composite context returned by asynchronous DomainOpenLsa call * @param ctx initialised libnet context * @param mem_ctx memory context of the call * @param io pointer to results (and arguments) of the call * @return nt status code of execution */NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io){ NTSTATUS status; struct domain_open_lsa_state *s; status = composite_wait(c); if (NT_STATUS_IS_OK(status) && io) { /* everything went fine - get the results and return the error string */ s = talloc_get_type(c->private_data, struct domain_open_lsa_state); io->out.domain_handle = s->handle; ctx->lsa.handle = s->handle; ctx->lsa.name = talloc_steal(ctx, s->name); ctx->lsa.access_mask = s->access_mask; io->out.error_string = talloc_strdup(mem_ctx, "Success"); } else if (!NT_STATUS_IS_OK(status)) { /* there was an error, so provide nt status code description */ io->out.error_string = talloc_asprintf(mem_ctx, "Failed to open domain: %s", nt_errstr(status)); } talloc_free(c); return status;}/** * Sends a request to open a domain in desired service * * @param ctx initalised libnet context * @param io arguments and results of the call * @param monitor pointer to monitor function that is passed monitor message */struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx, struct libnet_DomainOpen *io, void (*monitor)(struct monitor_msg*)){ struct composite_context *c; switch (io->in.type) { case DOMAIN_LSA: /* reques to open a policy handle on \pipe\lsarpc */ c = libnet_DomainOpenLsa_send(ctx, io, monitor); break; case DOMAIN_SAMR: default: /* request to open a domain policy handle on \pipe\samr */ c = libnet_DomainOpenSamr_send(ctx, io, monitor); break; } return c;}/** * Receive result of domain open request * * @param c composite context returned by DomainOpen_send function * @param ctx initialised libnet context * @param mem_ctx memory context of the call * @param io results and arguments of the call */NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io){ NTSTATUS status; switch (io->in.type) { case DOMAIN_LSA: status = libnet_DomainOpenLsa_recv(c, ctx, mem_ctx, io); break; case DOMAIN_SAMR: default: status = libnet_DomainOpenSamr_recv(c, ctx, mem_ctx, io); break; } return status;}/** * Synchronous version of DomainOpen call * * @param ctx initialised libnet context * @param mem_ctx memory context for the call * @param io arguments and results of the call * @return nt status code of execution */NTSTATUS libnet_DomainOpen(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io){ struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL); return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);}struct domain_close_lsa_state { struct dcerpc_pipe *pipe; struct lsa_Close close; struct policy_handle handle; void (*monitor_fn)(struct monitor_msg*);};static void continue_lsa_close(struct rpc_request *req);struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx, struct libnet_DomainClose *io, void (*monitor)(struct monitor_msg*)){ struct composite_context *c; struct domain_close_lsa_state *s; struct rpc_request *close_req; /* composite context and state structure allocation */ c = composite_create(ctx, ctx->event_ctx); if (c == NULL) return c; s = talloc_zero(c, struct domain_close_lsa_state); if (composite_nomem(s, c)) return c; c->private_data = s; s->monitor_fn = monitor; /* TODO: check if lsa pipe pointer is non-null */ if (!strequal(ctx->lsa.name, io->in.domain_name)) { composite_error(c, NT_STATUS_INVALID_PARAMETER); return c; } /* get opened lsarpc pipe pointer */ s->pipe = ctx->lsa.pipe; /* prepare close handle call arguments */ s->close.in.handle = &ctx->lsa.handle; s->close.out.handle = &s->handle; /* send the request */ close_req = dcerpc_lsa_Close_send(s->pipe, c, &s->close); if (composite_nomem(close_req, c)) return c; composite_continue_rpc(c, close_req, continue_lsa_close, c); return c;}/* Stage 1: Receive result of lsa close call*/static void continue_lsa_close(struct rpc_request *req){ struct composite_context *c; struct domain_close_lsa_state *s; c = talloc_get_type(req->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct domain_close_lsa_state); c->status = dcerpc_ndr_request_recv(req); if (!composite_is_ok(c)) return; if (s->monitor_fn) { struct monitor_msg msg; msg.type = mon_LsaClose; msg.data = NULL; msg.data_size = 0; s->monitor_fn(&msg); } composite_done(c);}NTSTATUS libnet_DomainCloseLsa_recv(struct composite_context *c, struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io){ NTSTATUS status; status = composite_wait(c); if (NT_STATUS_IS_OK(status) && io) { /* policy handle closed successfully */ ctx->lsa.name = NULL; ZERO_STRUCT(ctx->lsa.handle); io->out.error_string = talloc_asprintf(mem_ctx, "Success"); } else if (!NT_STATUS_IS_OK(status)) { /* there was an error, so return description of the status code */ io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status)); } talloc_free(c); return status;}struct domain_close_samr_state { struct samr_Close close; struct policy_handle handle; void (*monitor_fn)(struct monitor_msg*);};static void continue_samr_close(struct rpc_request *req);struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx, struct libnet_DomainClose *io, void (*monitor)(struct monitor_msg*)){ struct composite_context *c; struct domain_close_samr_state *s; struct rpc_request *close_req; /* composite context and state structure allocation */ c = composite_create(ctx, ctx->event_ctx); if (c == NULL) return c; s = talloc_zero(c, struct domain_close_samr_state); if (composite_nomem(s, c)) return c; c->private_data = s; s->monitor_fn = monitor; /* TODO: check if samr pipe pointer is non-null */ if (!strequal(ctx->samr.name, io->in.domain_name)) { composite_error(c, NT_STATUS_INVALID_PARAMETER); return c; } /* prepare close domain handle call arguments */ ZERO_STRUCT(s->close); s->close.in.handle = &ctx->samr.handle; s->close.out.handle = &s->handle; /* send the request */ close_req = dcerpc_samr_Close_send(ctx->samr.pipe, ctx, &s->close); if (composite_nomem(close_req, c)) return c; composite_continue_rpc(c, close_req, continue_samr_close, c); return c;}/* Stage 1: Receive result of samr close call*/static void continue_samr_close(struct rpc_request *req){ struct composite_context *c; struct domain_close_samr_state *s; c = talloc_get_type(req->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct domain_close_samr_state); c->status = dcerpc_ndr_request_recv(req); if (!composite_is_ok(c)) return; if (s->monitor_fn) { struct monitor_msg msg; msg.type = mon_SamrClose; msg.data = NULL; msg.data_size = 0; s->monitor_fn(&msg); } composite_done(c);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?