libnet_user.c
来自「samba最新软件」· C语言 代码 · 共 1,198 行 · 第 1/3 页
C
1,198 行
/* Unix SMB/CIFS implementation. Copyright (C) Rafal Szczesniak 2005 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "libnet/libnet.h"#include "libcli/composite/composite.h"#include "auth/credentials/credentials.h"#include "librpc/ndr/libndr.h"#include "librpc/gen_ndr/samr.h"#include "librpc/gen_ndr/ndr_samr_c.h"#include "librpc/gen_ndr/lsa.h"#include "librpc/gen_ndr/ndr_lsa_c.h"#include "libcli/security/security.h"struct create_user_state { struct libnet_CreateUser r; struct libnet_DomainOpen domain_open; struct libnet_rpc_useradd user_add; struct libnet_context *ctx; /* information about the progress */ void (*monitor_fn)(struct monitor_msg *);};static void continue_rpc_useradd(struct composite_context *ctx);static void continue_domain_open_create(struct composite_context *ctx);/** * Sends request to create user account * * @param ctx initialised libnet context * @param mem_ctx memory context of this call * @param r pointer to a structure containing arguments and results of this call * @param monitor function pointer for receiving monitor messages * @return compostite context of this request */struct composite_context* libnet_CreateUser_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_CreateUser *r, void (*monitor)(struct monitor_msg*)){ struct composite_context *c; struct create_user_state *s; struct composite_context *create_req; bool prereq_met = false; /* composite context allocation and setup */ c = composite_create(mem_ctx, ctx->event_ctx); if (c == NULL) return NULL; s = talloc_zero(c, struct create_user_state); if (composite_nomem(s, c)) return c; c->private_data = s; /* store arguments in the state structure */ s->ctx = ctx; s->r = *r; ZERO_STRUCT(s->r.out); /* prerequisite: make sure the domain is opened */ prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open, continue_domain_open_create, monitor); if (!prereq_met) return c; /* prepare arguments for useradd call */ s->user_add.in.username = r->in.user_name; s->user_add.in.domain_handle = ctx->samr.handle; /* send the request */ create_req = libnet_rpc_useradd_send(ctx->samr.pipe, &s->user_add, monitor); if (composite_nomem(create_req, c)) return c; /* set the next stage */ composite_continue(c, create_req, continue_rpc_useradd, c); return c;}/* * Stage 0.5 (optional): receive result of domain open request * and send useradd request */static void continue_domain_open_create(struct composite_context *ctx){ struct composite_context *c; struct create_user_state *s; struct composite_context *create_req; struct monitor_msg msg; c = talloc_get_type(ctx->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct create_user_state); /* receive result of DomainOpen call */ c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open); if (!composite_is_ok(c)) return; /* send monitor message */ if (s->monitor_fn) s->monitor_fn(&msg); /* prepare arguments for useradd call */ s->user_add.in.username = s->r.in.user_name; s->user_add.in.domain_handle = s->ctx->samr.handle; /* send the request */ create_req = libnet_rpc_useradd_send(s->ctx->samr.pipe, &s->user_add, s->monitor_fn); if (composite_nomem(create_req, c)) return; /* set the next stage */ composite_continue(c, create_req, continue_rpc_useradd, c);}/* * Stage 1: receive result of useradd call */static void continue_rpc_useradd(struct composite_context *ctx){ struct composite_context *c; struct create_user_state *s; struct monitor_msg msg; c = talloc_get_type(ctx->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct create_user_state); /* receive result of the call */ c->status = libnet_rpc_useradd_recv(ctx, c, &s->user_add); if (!composite_is_ok(c)) return; /* send monitor message */ if (s->monitor_fn) s->monitor_fn(&msg); /* we're done */ composite_done(c);}/** * Receive result of CreateUser call * * @param c composite context returned by send request routine * @param mem_ctx memory context of this call * @param r pointer to a structure containing arguments and result of this call * @return nt status */NTSTATUS libnet_CreateUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_CreateUser *r){ NTSTATUS status; struct create_user_state *s; r->out.error_string = NULL; /* wait for result of async request and check status code */ status = composite_wait(c); if (!NT_STATUS_IS_OK(status)) { s = talloc_get_type(c->private_data, struct create_user_state); r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status)); } return status;}/** * Synchronous version of CreateUser call * * @param ctx initialised libnet context * @param mem_ctx memory context of this call * @param r pointer to a structure containing arguments and result of this call * @return nt status */NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_CreateUser *r){ struct composite_context *c; c = libnet_CreateUser_send(ctx, mem_ctx, r, NULL); return libnet_CreateUser_recv(c, mem_ctx, r);}struct delete_user_state { struct libnet_DeleteUser r; struct libnet_context *ctx; struct libnet_DomainOpen domain_open; struct libnet_rpc_userdel user_del; /* information about the progress */ void (*monitor_fn)(struct monitor_msg *);};static void continue_rpc_userdel(struct composite_context *ctx);static void continue_domain_open_delete(struct composite_context *ctx);/** * Sends request to delete user account * * @param ctx initialised libnet context * @param mem_ctx memory context of this call * @param r pointer to structure containing arguments and result of this call * @param monitor function pointer for receiving monitor messages */struct composite_context *libnet_DeleteUser_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DeleteUser *r, void (*monitor)(struct monitor_msg*)){ struct composite_context *c; struct delete_user_state *s; struct composite_context *delete_req; bool prereq_met = false; /* composite context allocation and setup */ c = composite_create(mem_ctx, ctx->event_ctx); if (c == NULL) return NULL; s = talloc_zero(c, struct delete_user_state); if (composite_nomem(s, c)) return c; c->private_data = s; /* store arguments in state structure */ s->ctx = ctx; s->r = *r; ZERO_STRUCT(s->r.out); /* prerequisite: make sure the domain is opened before proceeding */ prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open, continue_domain_open_delete, monitor); if (!prereq_met) return c; /* prepare arguments for userdel call */ s->user_del.in.username = r->in.user_name; s->user_del.in.domain_handle = ctx->samr.handle; /* send request */ delete_req = libnet_rpc_userdel_send(ctx->samr.pipe, &s->user_del, monitor); if (composite_nomem(delete_req, c)) return c; /* set the next stage */ composite_continue(c, delete_req, continue_rpc_userdel, c); return c;}/* * Stage 0.5 (optional): receive result of domain open request * and send useradd request */static void continue_domain_open_delete(struct composite_context *ctx){ struct composite_context *c; struct delete_user_state *s; struct composite_context *delete_req; struct monitor_msg msg; c = talloc_get_type(ctx->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct delete_user_state); /* receive result of DomainOpen call */ c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open); if (!composite_is_ok(c)) return; /* send monitor message */ if (s->monitor_fn) s->monitor_fn(&msg); /* prepare arguments for userdel call */ s->user_del.in.username = s->r.in.user_name; s->user_del.in.domain_handle = s->ctx->samr.handle; /* send request */ delete_req = libnet_rpc_userdel_send(s->ctx->samr.pipe, &s->user_del, s->monitor_fn); if (composite_nomem(delete_req, c)) return; /* set the next stage */ composite_continue(c, delete_req, continue_rpc_userdel, c);}/* * Stage 1: receive result of userdel call and finish the composite function */static void continue_rpc_userdel(struct composite_context *ctx){ struct composite_context *c; struct delete_user_state *s; struct monitor_msg msg; c = talloc_get_type(ctx->async.private_data, struct composite_context); s = talloc_get_type(c->private_data, struct delete_user_state); /* receive result of userdel call */ c->status = libnet_rpc_userdel_recv(ctx, c, &s->user_del); if (!composite_is_ok(c)) return; /* send monitor message */ if (s->monitor_fn) s->monitor_fn(&msg); /* we're done */ composite_done(c);}/** * Receives result of asynchronous DeleteUser call * * @param c composite context returned by async DeleteUser call * @param mem_ctx memory context of this call * @param r pointer to structure containing arguments and result */NTSTATUS libnet_DeleteUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_DeleteUser *r){ NTSTATUS status; struct delete_user_state *s; r->out.error_string = NULL; /* wait for result of async request and check status code */ status = composite_wait(c); if (!NT_STATUS_IS_OK(status)) { s = talloc_get_type(c->private_data, struct delete_user_state); r->out.error_string = talloc_steal(mem_ctx, s->r.out.error_string); } return status;}/** * Synchronous version of DeleteUser call * * @param ctx initialised libnet context * @param mem_ctx memory context of this call * @param r pointer to structure containing arguments and result */NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DeleteUser *r){ struct composite_context *c; c = libnet_DeleteUser_send(ctx, mem_ctx, r, NULL); return libnet_DeleteUser_recv(c, mem_ctx, r);}struct modify_user_state { struct libnet_ModifyUser r; struct libnet_context *ctx; struct libnet_DomainOpen domain_open; struct libnet_rpc_userinfo user_info; struct libnet_rpc_usermod user_mod; void (*monitor_fn)(struct monitor_msg *);};static void continue_rpc_usermod(struct composite_context *ctx);static void continue_domain_open_modify(struct composite_context *ctx);static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod, struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r);static void continue_rpc_userinfo(struct composite_context *ctx);/** * Sends request to modify user account * * @param ctx initialised libnet context * @param mem_ctx memory context of this call * @param r pointer to structure containing arguments and result of this call * @param monitor function pointer for receiving monitor messages */struct composite_context *libnet_ModifyUser_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r, void (*monitor)(struct monitor_msg*)){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?