5 # Copyright Jelmer Vernooij 2010 <jelmer@samba.org>
6 # Copyright Theresa Halloran 2011 <theresahalloran@gmail.com>
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.
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.
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/>.
22 import samba.getopt as options
24 from samba.auth import system_session
25 from samba.samdb import SamDB
28 from samba.net import Net
30 from samba.netcmd import (
37 class cmd_user_add(Command):
38 """Create a new user."""
39 synopsis = "%prog user add <name> [<password>]"
41 takes_optiongroups = {
42 "sambaopts": options.SambaOptions,
43 "credopts": options.CredentialsOptions,
44 "versionopts": options.VersionOptions,
47 takes_args = ["name", "password?"]
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)
54 if password is not None:
55 net.set_password(name, creds.get_domain(), password, creds)
58 class cmd_user_delete(Command):
60 synopsis = "%prog user delete <name>"
62 takes_optiongroups = {
63 "sambaopts": options.SambaOptions,
64 "credopts": options.CredentialsOptions,
65 "versionopts": options.VersionOptions,
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)
76 except RuntimeError, msg:
77 raise CommandError("Failed to delete user %s: %s" % (name, msg))
79 class cmd_user_enable(Command):
82 synopsis = "%prog user enable <username> [options]"
85 takes_optiongroups = {
86 "sambaopts": options.SambaOptions,
87 "versionopts": options.VersionOptions,
88 "credopts": options.CredentialsOptions,
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),
96 takes_args = ["username?"]
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!")
104 filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
106 lp = sambaopts.get_loadparm()
107 creds = credopts.get_credentials(lp, fallback_machine=True)
109 samdb = SamDB(url=H, session_info=system_session(),
110 credentials=creds, lp=lp)
111 samdb.enable_account(filter)
115 class cmd_user(SuperCommand):
116 """User management [server connection needed]"""
119 subcommands["add"] = cmd_user_add()
120 subcommands["delete"] = cmd_user_delete()
121 subcommands["enable"] = cmd_user_enable()