diff options
author | Andrew Bartlett <abartlet@samba.org> | 2006-01-13 03:39:49 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:50:59 -0500 |
commit | 58f78fa182c4b4a046b957c89988d34ea2125696 (patch) | |
tree | 095fc563234da507b8f366ba98a0eeb0781e339b /source4 | |
parent | 792951a36535c9345e6027e0940d97d511282302 (diff) | |
download | samba-58f78fa182c4b4a046b957c89988d34ea2125696.tar.gz samba-58f78fa182c4b4a046b957c89988d34ea2125696.tar.bz2 samba-58f78fa182c4b4a046b957c89988d34ea2125696.zip |
r12892: Add a 'Migrate from Windows' page to our installation section in SWAT.
Doing this required reworking ejsnet, particularly so it could take a
set of credentials, not just a username and password argument.
This required fixing the ejsnet.js test script, which now adds and
deletes a user, and is run from 'make test'. This should prevent it
being broken again.
Deleting a user from ejsnet required that the matching backend be
added to libnet, hooking fortunetly onto already existing code for the
actual deletion.
The js credentials interface now handles the 'set machine account' flag.
New functions have been added to provision.js to wrap the basic
operations (so we can write a command line version, as well as the web
based version).
Andrew Bartlett
(This used to be commit a5e7c17c348c45e61699cc1626a0d5eae2df4636)
Diffstat (limited to 'source4')
-rw-r--r-- | source4/libnet/libnet_user.c | 51 | ||||
-rw-r--r-- | source4/libnet/libnet_user.h | 18 | ||||
-rwxr-xr-x | source4/script/tests/test_ejs.sh | 6 | ||||
-rw-r--r-- | source4/script/tests/tests_all.sh | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/ejsnet.c | 219 | ||||
-rw-r--r-- | source4/scripting/ejs/ejsnet.h | 2 | ||||
-rw-r--r-- | source4/scripting/ejs/smbcalls_creds.c | 22 | ||||
-rw-r--r-- | source4/scripting/libjs/provision.js | 26 | ||||
-rw-r--r-- | source4/utils/net/net_user.c | 42 |
9 files changed, 337 insertions, 51 deletions
diff --git a/source4/libnet/libnet_user.c b/source4/libnet/libnet_user.c index 50cb14d290..ba9fd0cc83 100644 --- a/source4/libnet/libnet_user.c +++ b/source4/libnet/libnet_user.c @@ -75,3 +75,54 @@ NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru return status; } + +NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DeleteUser *r) +{ + NTSTATUS status; + struct libnet_RpcConnect cn; + struct libnet_rpc_domain_open dom_io; + struct libnet_rpc_userdel user_io; + + /* connect rpc service of remote DC */ + cn.level = LIBNET_RPC_CONNECT_PDC; + cn.in.name = talloc_strdup(mem_ctx, r->in.domain_name); + cn.in.dcerpc_iface = &dcerpc_table_samr; + + status = libnet_RpcConnect(ctx, mem_ctx, &cn); + if (!NT_STATUS_IS_OK(status)) { + r->out.error_string = talloc_asprintf(mem_ctx, + "Connection to SAMR pipe domain '%s' PDC failed: %s\n", + r->in.domain_name, nt_errstr(status)); + return status; + } + + ctx->pipe = cn.out.dcerpc_pipe; + + /* open connected domain */ + dom_io.in.domain_name = r->in.domain_name; + dom_io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; + + status = libnet_rpc_domain_open(ctx->pipe, mem_ctx, &dom_io); + if (!NT_STATUS_IS_OK(status)) { + r->out.error_string = talloc_asprintf(mem_ctx, + "Opening domain to delete user account failed: %s\n", + nt_errstr(status)); + return status; + } + + ctx->domain_handle = dom_io.out.domain_handle; + + /* create user */ + user_io.in.username = r->in.user_name; + user_io.in.domain_handle = dom_io.out.domain_handle; + + status = libnet_rpc_userdel(ctx->pipe, mem_ctx, &user_io); + if (!NT_STATUS_IS_OK(status)) { + r->out.error_string = talloc_asprintf(mem_ctx, + "Deleting user account failed: %s\n", + nt_errstr(status)); + return status; + } + + return status; +} diff --git a/source4/libnet/libnet_user.h b/source4/libnet/libnet_user.h index 358f0d0b0f..2182ccb4e4 100644 --- a/source4/libnet/libnet_user.h +++ b/source4/libnet/libnet_user.h @@ -36,3 +36,21 @@ struct libnet_CreateUser { const char *error_string; } out; }; + +enum libnet_DeleteUser_level { + LIBNET_DELETE_USER_GENERIC, + LIBNET_DELETE_USER_SAMR, +}; + + +struct libnet_DeleteUser { + enum libnet_DeleteUser_level level; + + struct { + const char *user_name; + const char *domain_name; + } in; + struct { + const char *error_string; + } out; +}; diff --git a/source4/script/tests/test_ejs.sh b/source4/script/tests/test_ejs.sh index 2f1a2abcad..6df202122a 100755 --- a/source4/script/tests/test_ejs.sh +++ b/source4/script/tests/test_ejs.sh @@ -3,12 +3,12 @@ if [ $# -lt 3 ]; then cat <<EOF -Usage: test_ejs.sh SERVER USERNAME PASSWORD +Usage: test_ejs.sh DOMAIN USERNAME PASSWORD EOF exit 1; fi -SERVER="$1" +DOMAIN="$1" USERNAME="$2" PASSWORD="$3" @@ -24,6 +24,8 @@ for f in samr.js echo.js ldb.js; do testit "$f" $SCRIPTDIR/$f $CONFIGURATION ncalrpc: -U$USERNAME%$PASSWORD || failed=`expr $failed + 1` done +testit "ejsnet.js" $SCRIPTDIR/ejsnet.js $CONFIGURATION -U$USERNAME%$PASSWORD $DOMAIN ejstestuser || failed=`expr $failed + 1` + testit "winreg" scripting/bin/winreg $CONFIGURATION ncalrpc: 'HKLM' -U$USERNAME%$PASSWORD || failed=`expr $failed + 1` testok $0 $failed diff --git a/source4/script/tests/tests_all.sh b/source4/script/tests/tests_all.sh index 8abebf0dce..70e72e7240 100644 --- a/source4/script/tests/tests_all.sh +++ b/source4/script/tests/tests_all.sh @@ -1,4 +1,4 @@ - $SRCDIR/script/tests/test_ejs.sh $SERVER $USERNAME $PASSWORD || failed=`expr $failed + $?` + $SRCDIR/script/tests/test_ejs.sh $DOMAIN $USERNAME $PASSWORD || failed=`expr $failed + $?` $SRCDIR/script/tests/test_ldap.sh $SERVER $USERNAME $PASSWORD || failed=`expr $failed + $?` $SRCDIR/script/tests/test_nbt.sh $SERVER || failed=`expr $failed + $?` $SRCDIR/script/tests/test_quick.sh //$SERVER/cifs $USERNAME $PASSWORD "" || failed=`expr $failed + $?` diff --git a/source4/scripting/ejs/ejsnet.c b/source4/scripting/ejs/ejsnet.c index 5768fb4442..e129ba6867 100644 --- a/source4/scripting/ejs/ejsnet.c +++ b/source4/scripting/ejs/ejsnet.c @@ -27,70 +27,160 @@ #include "libnet/libnet.h" +static int ejs_net_userman(MprVarHandle, int, struct MprVar**); +static int ejs_net_createuser(MprVarHandle, int, char**); +static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv); +static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv); +static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv); + +/* Usage: + net = NetContext(credentials); +*/ + static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv) { struct cli_credentials *creds; struct libnet_context *ctx; - struct MprVar *obj; + struct MprVar obj; + /* TODO: Need to get the right event context in here */ ctx = libnet_context_init(NULL); - creds = talloc(ctx, struct cli_credentials); - ctx->cred = creds; - - ctx->name_res_methods = str_list_copy(ctx, lp_name_resolve_order()); if (argc == 0) { + creds = cli_credentials_init(ctx); + if (creds == NULL) { + ejsSetErrorMsg(eid, "cli_credential_init() failed"); + talloc_free(ctx); + return -1; + } + cli_credentials_set_conf(creds); cli_credentials_set_anonymous(creds); + } else if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) { + /* get credential values from credentials object */ + creds = mprGetPtr(argv[0], "creds"); + if (creds == NULL) { + ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter"); + talloc_free(ctx); + return -1; + } + } else { + ejsSetErrorMsg(eid, "NetContext invalid arguments, this function requires an object."); + talloc_free(ctx); + return -1; + } + ctx->cred = creds; - } else if (argc == 2 || argc == 4 ) { - - cli_credentials_set_workstation(creds, lp_netbios_name(), CRED_SPECIFIED); + obj = mprObject("NetCtx"); + mprSetPtrChild(&obj, "ctx", ctx); + + mprSetCFunction(&obj, "UserMgr", ejs_net_userman); + mprSetCFunction(&obj, "JoinDomain", ejs_net_join_domain); + mprSetCFunction(&obj, "SamSyncLdb", ejs_net_samsync_ldb); + mpr_Return(eid, obj); - if (!mprVarIsString(argv[0]->type)) { - ejsSetErrorMsg(eid, "argument 1 must be a string"); - goto done; - } - cli_credentials_set_username(creds, argv[0]->string, CRED_SPECIFIED); + return 0; +} - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "argument 2 must be a string"); - goto done; +static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv) +{ + TALLOC_CTX *mem_ctx; + struct libnet_context *ctx; + struct libnet_Join *join; + NTSTATUS status; + ctx = mprGetThisPtr(eid, "ctx"); + mem_ctx = talloc_new(mprMemCtx()); + + join = talloc(mem_ctx, struct libnet_Join); + if (!join) { + talloc_free(mem_ctx); + return -1; + } + + /* prepare parameters for the join */ + join->in.netbios_name = NULL; + join->in.join_type = SEC_CHAN_WKSTA; + join->in.domain_name = cli_credentials_get_domain(ctx->cred); + join->in.level = LIBNET_JOINDOMAIN_AUTOMATIC; + join->out.error_string = NULL; + + if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) { + MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL); + MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL); + MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL); + if (netbios_name) { + join->in.netbios_name = mprToString(netbios_name); + } + if (domain_name) { + join->in.domain_name = mprToString(domain_name); + } + if (join_type) { + join->in.join_type = mprToInt(join_type); } - cli_credentials_set_password(creds, argv[1]->string, CRED_SPECIFIED); + } - } else { - ejsSetErrorMsg(eid, "invalid number of arguments"); - goto done; + if (!join->in.domain_name) { + ejsSetErrorMsg(eid, "a domain must be specified for to join"); + talloc_free(mem_ctx); + return -1; } + /* do the domain join */ + status = libnet_Join(ctx, join, join); + + if (!NT_STATUS_IS_OK(status)) { + MprVar error_string = mprString(join->out.error_string); - if (argc == 4) { + mprSetPropertyValue(argv[0], "error_string", error_string); + mpr_Return(eid, mprCreateBoolVar(False)); + } else { + mpr_Return(eid, mprCreateBoolVar(True)); + } + talloc_free(mem_ctx); + return 0; +} - if (!mprVarIsString(argv[2]->type)) { - ejsSetErrorMsg(eid, "argument 3 must be a string"); - goto done; - } - cli_credentials_set_domain(creds, argv[2]->string, CRED_SPECIFIED); - - if (!mprVarIsString(argv[3]->type)) { - ejsSetErrorMsg(eid, "argument 4 must be a string"); - goto done; - } - cli_credentials_set_realm(creds, argv[3]->string, CRED_SPECIFIED); +static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv) +{ + TALLOC_CTX *mem_ctx; + struct libnet_context *ctx; + struct libnet_samsync_ldb *samsync; + NTSTATUS status; + ctx = mprGetThisPtr(eid, "ctx"); + mem_ctx = talloc_new(mprMemCtx()); + + samsync = talloc(mem_ctx, struct libnet_samsync_ldb); + if (!samsync) { + talloc_free(mem_ctx); + return -1; } - obj = mprInitObject(eid, "NetCtx", argc, argv); - mprSetPtrChild(obj, "ctx", ctx); + /* prepare parameters for the samsync */ + samsync->in.machine_account = NULL; + samsync->in.binding_string = NULL; + samsync->out.error_string = NULL; - mprSetCFunction(obj, "UserMgr", ejs_net_userman); + if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) { + MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL); + if (credentials) { + samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials); + } + } + /* do the domain samsync */ + status = libnet_samsync_ldb(ctx, samsync, samsync); + + if (!NT_STATUS_IS_OK(status)) { + MprVar error_string = mprString(samsync->out.error_string); + + mprSetPropertyValue(argv[0], "error_string", error_string); + mpr_Return(eid, mprCreateBoolVar(False)); + } else { + mpr_Return(eid, mprCreateBoolVar(True)); + } + talloc_free(mem_ctx); return 0; -done: - talloc_free(ctx); - return -1; } - static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv) { TALLOC_CTX *mem_ctx; @@ -99,7 +189,7 @@ static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv) struct MprVar *obj = NULL; ctx = mprGetThisPtr(eid, "ctx"); - mem_ctx = talloc_init(NULL); + mem_ctx = talloc_new(mprMemCtx()); if (argc == 0) { userman_domain = cli_credentials_get_domain(ctx->cred); @@ -122,6 +212,7 @@ static int ejs_net_userman(MprVarHandle eid, int argc, struct MprVar **argv) mprSetPtrChild(obj, "domain", userman_domain); mprSetStringCFunction(obj, "Create", ejs_net_createuser); + mprSetStringCFunction(obj, "Delete", ejs_net_deleteuser); return 0; done: @@ -140,22 +231,22 @@ static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv) if (argc != 1) { ejsSetErrorMsg(eid, "argument 1 must be a string"); - goto done; + return -1; } ctx = mprGetThisPtr(eid, "ctx"); if (!ctx) { ejsSetErrorMsg(eid, "ctx property returns null pointer"); - goto done; + return -1; } userman_domain = mprGetThisPtr(eid, "domain"); if (!userman_domain) { ejsSetErrorMsg(eid, "domain property returns null pointer"); - goto done; + return -1; } - mem_ctx = talloc_init(NULL); + mem_ctx = talloc_new(mprMemCtx()); req.in.domain_name = userman_domain; req.in.user_name = argv[0]; @@ -168,10 +259,46 @@ static int ejs_net_createuser(MprVarHandle eid, int argc, char **argv) talloc_free(mem_ctx); mpr_Return(eid, mprNTSTATUS(status)); return 0; +} + +static int ejs_net_deleteuser(MprVarHandle eid, int argc, char **argv) +{ + NTSTATUS status = NT_STATUS_UNSUCCESSFUL; + TALLOC_CTX *mem_ctx; + struct libnet_context *ctx; + const char *userman_domain = NULL; + struct libnet_DeleteUser req; + + if (argc != 1) { + ejsSetErrorMsg(eid, "argument 1 must be a string"); + return -1; + } + + ctx = mprGetThisPtr(eid, "ctx"); + if (!ctx) { + ejsSetErrorMsg(eid, "ctx property returns null pointer"); + return -1; + } + + userman_domain = mprGetThisPtr(eid, "domain"); + if (!userman_domain) { + ejsSetErrorMsg(eid, "domain property returns null pointer"); + return -1; + } + + mem_ctx = talloc_new(mprMemCtx()); + + req.in.domain_name = userman_domain; + req.in.user_name = argv[0]; + + status = libnet_DeleteUser(ctx, mem_ctx, &req); + if (!NT_STATUS_IS_OK(status)) { + ejsSetErrorMsg(eid, "error when creating user: %s", nt_errstr(status)); + } -done: talloc_free(mem_ctx); - return -1; + mpr_Return(eid, mprNTSTATUS(status)); + return 0; } diff --git a/source4/scripting/ejs/ejsnet.h b/source4/scripting/ejs/ejsnet.h index 50978c648d..7d4bc32753 100644 --- a/source4/scripting/ejs/ejsnet.h +++ b/source4/scripting/ejs/ejsnet.h @@ -24,5 +24,3 @@ void ejsnet_setup(void); -static int ejs_net_userman(MprVarHandle, int, struct MprVar**); -static int ejs_net_createuser(MprVarHandle, int, char**); diff --git a/source4/scripting/ejs/smbcalls_creds.c b/source4/scripting/ejs/smbcalls_creds.c index bec70bc6f2..4b0312bf83 100644 --- a/source4/scripting/ejs/smbcalls_creds.c +++ b/source4/scripting/ejs/smbcalls_creds.c @@ -181,6 +181,25 @@ static int ejs_creds_get_workstation(MprVarHandle eid, int argc, struct MprVar * return 0; } +/* + set machine account +*/ +static int ejs_creds_set_machine_account(MprVarHandle eid, int argc, char **argv) +{ + struct cli_credentials *creds = ejs_creds_get_credentials(eid); + if (argc != 0) { + ejsSetErrorMsg(eid, "bad arguments to set_machine_account"); + return -1; + } + + if (NT_STATUS_IS_OK(cli_credentials_set_machine_account(creds))) { + mpr_Return(eid, mprCreateBoolVar(True)); + } else { + mpr_Return(eid, mprCreateBoolVar(False)); + } + return 0; +} + /* initialise credentials ejs object @@ -200,6 +219,7 @@ static int ejs_credentials_obj(struct MprVar *obj, struct cli_credentials *creds mprSetStringCFunction(obj, "set_realm", ejs_creds_set_realm); mprSetCFunction(obj, "get_workstation", ejs_creds_get_workstation); mprSetStringCFunction(obj, "set_workstation", ejs_creds_set_workstation); + mprSetCFunction(obj, "set_machine_account", ejs_creds_set_machine_account); return 0; } @@ -228,6 +248,8 @@ static int ejs_credentials_init(MprVarHandle eid, int argc, struct MprVar **argv return -1; } + cli_credentials_set_conf(creds); + return ejs_credentials_obj(obj, creds); } diff --git a/source4/scripting/libjs/provision.js b/source4/scripting/libjs/provision.js index 8b1d93c056..60f267f8d5 100644 --- a/source4/scripting/libjs/provision.js +++ b/source4/scripting/libjs/provision.js @@ -327,6 +327,7 @@ function provision(subobj, message, blank, paths, session_info, credentials) message("Setting up sam.ldb users and groups\n"); setup_ldb("provision_users.ldif", info, paths.samdb, data, false); } + return true; } /* @@ -516,5 +517,30 @@ function provision_validate(subobj, message) return true; } +function join_domain(domain, netbios_name, join_type, creds, writefln) +{ + ctx = NetContext(creds); + join = new Object(); + join.domain = domain; + join.join_type = join_type; + join.netbios_name = netbios_name; + if (!ctx.JoinDomain(join)) { + writefln("Domain Join failed: " + join.error_string); + return false; + } + return true; +} + +function vampire(machine_creds, writefln) +{ + var ctx = NetContext(); + vampire = new Object(); + vampire.machine_creds = machine_creds; + if (!ctx.SamSyncLdb(vampire)) { + writefln("Migration of remote domain to Samba failed: " + vampire.error_string); + return false; + } + return true; +} return 0; diff --git a/source4/utils/net/net_user.c b/source4/utils/net/net_user.c index 82ee208534..ecc1834fcf 100644 --- a/source4/utils/net/net_user.c +++ b/source4/utils/net/net_user.c @@ -64,9 +64,51 @@ static int net_user_add(struct net_context *ctx, int argc, const char **argv) return 0; } +static int net_user_delete(struct net_context *ctx, int argc, const char **argv) +{ + NTSTATUS status; + struct libnet_context *lnet_ctx; + struct libnet_DeleteUser r; + char *user_name; + + /* command line argument preparation */ + switch (argc) { + case 0: + return net_user_usage(ctx, argc, argv); + break; + case 1: + user_name = talloc_strdup(ctx->mem_ctx, argv[0]); + break; + default: + return net_user_usage(ctx, argc, argv); + } + + /* libnet context init and its params */ + lnet_ctx = libnet_context_init(NULL); + if (!lnet_ctx) return -1; + + lnet_ctx->cred = ctx->credentials; + + /* calling DeleteUser function */ + r.level = LIBNET_DELETE_USER_GENERIC; + r.in.user_name = user_name; + r.in.domain_name = cli_credentials_get_domain(lnet_ctx->cred); + + status = libnet_DeleteUser(lnet_ctx, ctx->mem_ctx, &r); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0, ("Failed to delete user account: %s\n", + r.out.error_string)); + return -1; + } + + talloc_free(lnet_ctx); + return 0; +} + static const struct net_functable net_user_functable[] = { { "add", "create new user account\n", net_user_add, net_user_usage }, + { "delete", "delete an existing user account\n", net_user_delete, net_user_usage }, { NULL, NULL } }; |