diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2010-03-01 23:03:41 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2010-04-09 11:53:00 +0200 |
commit | ffa73c412e1190024ae0bf4758174d1b21c16e13 (patch) | |
tree | 21a812f4c00746404d0e9181a77b9704ebb0608c | |
parent | 8149094eddebd9a0e8b7c123c2ed54d00164bb26 (diff) | |
download | samba-ffa73c412e1190024ae0bf4758174d1b21c16e13.tar.gz samba-ffa73c412e1190024ae0bf4758174d1b21c16e13.tar.bz2 samba-ffa73c412e1190024ae0bf4758174d1b21c16e13.zip |
s4-net: Convert user subcommand to Python.
-rw-r--r-- | source4/libnet/py_net.c | 70 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/__init__.py | 2 | ||||
-rw-r--r-- | source4/scripting/python/samba/netcmd/user.py | 74 | ||||
-rw-r--r-- | source4/utils/net/config.mk | 3 | ||||
-rw-r--r-- | source4/utils/net/net.c | 1 | ||||
-rw-r--r-- | source4/utils/net/net_user.c | 125 | ||||
-rw-r--r-- | source4/utils/net/wscript_build | 2 |
7 files changed, 148 insertions, 129 deletions
diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index d6bd134fe5..bd916efe52 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -183,11 +183,81 @@ static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwar static const char py_net_time_doc[] = "time(server_name) -> timestr\n" "Retrieve the remote time on a server"; +static PyObject *py_net_user_create(py_net_Object *self, PyObject *args, PyObject *kwargs) +{ + const char *kwnames[] = { "username", NULL }; + NTSTATUS status; + TALLOC_CTX *mem_ctx; + struct libnet_CreateUser r; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames), + &r.in.user_name)) + return NULL; + + r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred); + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + PyErr_NoMemory(); + return NULL; + } + + status = libnet_CreateUser(self->libnet_ctx, mem_ctx, &r); + if (!NT_STATUS_IS_OK(status)) { + PyErr_SetString(PyExc_RuntimeError, r.out.error_string); + talloc_free(mem_ctx); + return NULL; + } + + talloc_free(mem_ctx); + + Py_RETURN_NONE; +} + +static const char py_net_create_user_doc[] = "create_user(username)\n" +"Create a new user."; + +static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObject *kwargs) +{ + const char *kwnames[] = { "username", NULL }; + NTSTATUS status; + TALLOC_CTX *mem_ctx; + struct libnet_DeleteUser r; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames), + &r.in.user_name)) + return NULL; + + r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred); + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + PyErr_NoMemory(); + return NULL; + } + + status = libnet_DeleteUser(self->libnet_ctx, mem_ctx, &r); + if (!NT_STATUS_IS_OK(status)) { + PyErr_SetString(PyExc_RuntimeError, r.out.error_string); + talloc_free(mem_ctx); + return NULL; + } + + talloc_free(mem_ctx); + + Py_RETURN_NONE; +} + +static const char py_net_delete_user_doc[] = "delete_user(username)\n" +"Delete a user."; + static PyMethodDef net_obj_methods[] = { {"join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc}, {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc}, {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc}, {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc}, + {"create_user", (PyCFunction)py_net_user_create, METH_VARARGS|METH_KEYWORDS, py_net_create_user_doc}, + {"delete_user", (PyCFunction)py_net_user_delete, METH_VARARGS|METH_KEYWORDS, py_net_delete_user_doc}, { NULL } }; diff --git a/source4/scripting/python/samba/netcmd/__init__.py b/source4/scripting/python/samba/netcmd/__init__.py index f9c644582c..d4e21c1b4e 100644 --- a/source4/scripting/python/samba/netcmd/__init__.py +++ b/source4/scripting/python/samba/netcmd/__init__.py @@ -151,3 +151,5 @@ from samba.netcmd.export import cmd_export commands["export"] = cmd_export() from samba.netcmd.time import cmd_time commands["time"] = cmd_time() +from samba.netcmd.user import cmd_user +commands["user"] = cmd_user() diff --git a/source4/scripting/python/samba/netcmd/user.py b/source4/scripting/python/samba/netcmd/user.py new file mode 100644 index 0000000000..319e8b0215 --- /dev/null +++ b/source4/scripting/python/samba/netcmd/user.py @@ -0,0 +1,74 @@ +#!/usr/bin/python +# +# user management +# +# Copyright Jelmer Vernooij 2010 <jelmer@samba.org> +# +# 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/>. +# + +import samba.getopt as options + +from samba.net import Net + +from samba.netcmd import ( + Command, + SuperCommand, + ) + +class cmd_user_create(Command): + """Create a new user.""" + synopsis = "%prog user create <name>" + + takes_optiongroups = { + "sambaopts": options.SambaOptions, + "credopts": options.CredentialsOptions, + "versionopts": options.VersionOptions, + } + + takes_args = ["name"] + + def run(self, name, credopts=None, sambaopts=None, versionopts=None): + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp) + net = Net(creds, lp) + net.create_user(name) + + +class cmd_user_delete(Command): + """Delete a user.""" + synopsis = "%prog user delete <name>" + + takes_optiongroups = { + "sambaopts": options.SambaOptions, + "credopts": options.CredentialsOptions, + "versionopts": options.VersionOptions, + } + + takes_args = ["name"] + + def run(self, name, credopts=None, sambaopts=None, versionopts=None): + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp) + net = Net(creds, lp) + net.delete_user(name) + + +class cmd_user(SuperCommand): + """User management.""" + + subcommands = {} + subcommands["create"] = cmd_user_create() + subcommands["delete"] = cmd_user_delete() + diff --git a/source4/utils/net/config.mk b/source4/utils/net/config.mk index 496d339bf8..5b9414092e 100644 --- a/source4/utils/net/config.mk +++ b/source4/utils/net/config.mk @@ -42,8 +42,7 @@ net_OBJ_FILES = $(addprefix $(utilssrcdir)/net/, \ net_machinepw.o \ net_password.o \ net_join.o \ - net_vampire.o \ - net_user.o) + net_vampire.o) $(eval $(call proto_header_template,$(utilssrcdir)/net/net_proto.h,$(net_OBJ_FILES:.o=.c))) diff --git a/source4/utils/net/net.c b/source4/utils/net/net.c index 1eafe4dd78..545bc0f523 100644 --- a/source4/utils/net/net.c +++ b/source4/utils/net/net.c @@ -201,7 +201,6 @@ static const struct net_functable net_functable[] = { {"samdump", "dump the sam of a domain\n", net_samdump, net_samdump_usage}, {"vampire", "join and syncronise an AD domain onto the local server\n", net_vampire, net_vampire_usage}, {"samsync", "synchronise into the local ldb the sam of an NT4 domain\n", net_samsync_ldb, net_samsync_ldb_usage}, - {"user", "manage user accounts\n", net_user, net_user_usage}, {"machinepw", "Get a machine password out of our SAM\n", net_machinepw, net_machinepw_usage}, {"drs", "Implements functionality offered by repadmin.exe utility in Windows\n", net_drs, net_drs_usage}, {NULL, NULL, NULL, NULL} diff --git a/source4/utils/net/net_user.c b/source4/utils/net/net_user.c deleted file mode 100644 index c4b8ecb0c2..0000000000 --- a/source4/utils/net/net_user.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - Samba Unix/Linux SMB client library - Distributed SMB/CIFS Server Management Utility - - Copyright (C) Rafal Szczesniak <mimir@samba.org> 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 "utils/net/net.h" -#include "libnet/libnet.h" -#include "lib/events/events.h" -#include "auth/credentials/credentials.h" - -static int net_user_add(struct net_context *ctx, int argc, const char **argv) -{ - NTSTATUS status; - struct libnet_context *lnet_ctx; - struct libnet_CreateUser 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, argv[0]); - break; - default: - return net_user_usage(ctx, argc, argv); - } - - /* libnet context init and its params */ - lnet_ctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx); - if (!lnet_ctx) return -1; - - lnet_ctx->cred = ctx->credentials; - - /* calling CreateUser function */ - r.in.user_name = user_name; - r.in.domain_name = cli_credentials_get_domain(lnet_ctx->cred); - - status = libnet_CreateUser(lnet_ctx, ctx, &r); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0, ("Failed to add user account: %s\n", - r.out.error_string)); - return -1; - } - - talloc_free(lnet_ctx); - 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, argv[0]); - break; - default: - return net_user_usage(ctx, argc, argv); - } - - /* libnet context init and its params */ - lnet_ctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx); - if (!lnet_ctx) return -1; - - lnet_ctx->cred = ctx->credentials; - - /* calling DeleteUser function */ - r.in.user_name = user_name; - r.in.domain_name = cli_credentials_get_domain(lnet_ctx->cred); - - status = libnet_DeleteUser(lnet_ctx, 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 } -}; - - -int net_user(struct net_context *ctx, int argc, const char **argv) -{ - return net_run_function(ctx, argc, argv, net_user_functable, net_user_usage); -} - - -int net_user_usage(struct net_context *ctx, int argc, const char **argv) -{ - d_printf("net user <command> [options]\n"); - return 0; -} diff --git a/source4/utils/net/wscript_build b/source4/utils/net/wscript_build index 8f63608ea6..a7cdb10c2c 100644 --- a/source4/utils/net/wscript_build +++ b/source4/utils/net/wscript_build @@ -10,7 +10,7 @@ bld.SAMBA_MODULE('net_drs', bld.SAMBA_BINARY('net', - source='net.c net_machinepw.c net_password.c net_join.c net_vampire.c net_user.c', + source='net.c net_machinepw.c net_password.c net_join.c net_vampire.c', autoproto='net_proto.h', installdir='BINDIR', deps='LIBSAMBA-HOSTCONFIG LIBSAMBA-UTIL LIBSAMBA-NET popt POPT_SAMBA POPT_CREDENTIALS net_drs', |