s4-net: Convert user subcommand to Python.
authorJelmer Vernooij <jelmer@samba.org>
Mon, 1 Mar 2010 22:03:41 +0000 (23:03 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 9 Apr 2010 09:53:00 +0000 (11:53 +0200)
source4/libnet/py_net.c
source4/scripting/python/samba/netcmd/__init__.py
source4/scripting/python/samba/netcmd/user.py [new file with mode: 0644]
source4/utils/net/config.mk
source4/utils/net/net.c
source4/utils/net/net_user.c [deleted file]
source4/utils/net/wscript_build

index d6bd134fe5b85555bf21c9e41787b1e1acae80e2..bd916efe522d0489fdcbf87756d8ae55ab4cbcb8 100644 (file)
@@ -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 }
 };
 
index f9c644582c3fa29e017c0023178c4b5e9571cf3c..d4e21c1b4e523e13613c1c2bdabf52f3653cc0f1 100644 (file)
@@ -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 (file)
index 0000000..319e8b0
--- /dev/null
@@ -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()
+
index 496d339bf8e2a2f90bac2b7338e6a0913ac980a0..5b9414092e13887f987428d641933c879ec8addd 100644 (file)
@@ -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)))
index 1eafe4dd789ecd15d2aaaa98513f9d8b5b21f552..545bc0f5235fe27fdc32c717afa4fe0fed963468 100644 (file)
@@ -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 (file)
index c4b8ecb..0000000
+++ /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;
-}
index 8f63608ea69ff300bb5df08581b03c8451034906..a7cdb10c2c9e4e704deba7418cfc257520de29ca 100644 (file)
@@ -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',