s4/samba-tool: Move samba-tool enableaccount to samba-tool user enable command.
[amitay/samba.git] / source4 / scripting / python / samba / netcmd / user.py
1 #!/usr/bin/env python
2 #
3 # user management
4 #
5 # Copyright Jelmer Vernooij 2010 <jelmer@samba.org>
6 # Copyright Theresa Halloran 2011 <theresahalloran@gmail.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 import samba.getopt as options
23 import sys
24 from samba.auth import system_session
25 from samba.samdb import SamDB
26
27
28 from samba.net import Net
29
30 from samba.netcmd import (
31     Command,
32     CommandError,
33     SuperCommand,
34     Option,
35     )
36
37 class cmd_user_add(Command):
38     """Create a new user."""
39     synopsis = "%prog user add <name> [<password>]"
40
41     takes_optiongroups = {
42         "sambaopts": options.SambaOptions,
43         "credopts": options.CredentialsOptions,
44         "versionopts": options.VersionOptions,
45         }
46
47     takes_args = ["name", "password?"]
48
49     def run(self, name, password=None, credopts=None, sambaopts=None, versionopts=None):
50         lp = sambaopts.get_loadparm()
51         creds = credopts.get_credentials(lp )
52         net = Net(creds, lp, server=credopts.ipaddress)
53         net.create_user(name)
54         if password is not None:
55             net.set_password(name, creds.get_domain(), password, creds)
56
57
58 class cmd_user_delete(Command):
59     """Delete a user."""
60     synopsis = "%prog user delete <name>"
61
62     takes_optiongroups = {
63         "sambaopts": options.SambaOptions,
64         "credopts": options.CredentialsOptions,
65         "versionopts": options.VersionOptions,
66         }
67
68     takes_args = ["name"]
69
70     def run(self, name, credopts=None, sambaopts=None, versionopts=None):
71         lp = sambaopts.get_loadparm()
72         creds = credopts.get_credentials(lp, fallback_machine=True)
73         net = Net(creds, lp, server=credopts.ipaddress)
74         try:
75             net.delete_user(name)
76         except RuntimeError, msg:
77             raise CommandError("Failed to delete user %s: %s" % (name, msg))
78
79 class cmd_user_enable(Command):
80     """Enables a user"""
81
82     synopsis = "%prog user enable <username> [options]"
83
84
85     takes_optiongroups = {
86         "sambaopts": options.SambaOptions,
87         "versionopts": options.VersionOptions,
88         "credopts": options.CredentialsOptions,
89     }
90
91     takes_options = [
92         Option("-H", help="LDB URL for database or target server", type=str),
93         Option("--filter", help="LDAP Filter to set password on", type=str),
94         ]
95
96     takes_args = ["username?"]
97
98     def run(self, username=None, sambaopts=None, credopts=None,
99             versionopts=None, filter=None, H=None):
100         if username is None and filter is None:
101             raise CommandError("Either the username or '--filter' must be specified!")
102
103         if filter is None:
104             filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
105
106         lp = sambaopts.get_loadparm()
107         creds = credopts.get_credentials(lp, fallback_machine=True)
108
109         samdb = SamDB(url=H, session_info=system_session(),
110             credentials=creds, lp=lp)
111         samdb.enable_account(filter)
112
113
114
115 class cmd_user(SuperCommand):
116     """User management [server connection needed]"""
117
118     subcommands = {}
119     subcommands["add"] = cmd_user_add()
120     subcommands["delete"] = cmd_user_delete()
121     subcommands["enable"] = cmd_user_enable()
122