From ffa73c412e1190024ae0bf4758174d1b21c16e13 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 1 Mar 2010 23:03:41 +0100 Subject: s4-net: Convert user subcommand to Python. --- source4/libnet/py_net.c | 70 ++++++++++++ source4/scripting/python/samba/netcmd/__init__.py | 2 + source4/scripting/python/samba/netcmd/user.py | 74 +++++++++++++ source4/utils/net/config.mk | 3 +- source4/utils/net/net.c | 1 - source4/utils/net/net_user.c | 125 ---------------------- source4/utils/net/wscript_build | 2 +- 7 files changed, 148 insertions(+), 129 deletions(-) create mode 100644 source4/scripting/python/samba/netcmd/user.py delete mode 100644 source4/utils/net/net_user.c 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 +# +# 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 . +# + +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 " + + 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 " + + 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 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 . -*/ - -#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 [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', -- cgit