3 # Adds a new user to a Samba4 server
4 # Copyright Jelmer Vernooij 2008
6 # Based on the original in EJS:
7 # Copyright Andrew Tridgell 2005
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import samba.getopt as options
23 from samba.netcmd import Command, SuperCommand, CommandError, Option
26 from getpass import getpass
27 from samba.auth import system_session
28 from samba.samdb import SamDB
29 from samba.dsdb import (
30 GTYPE_SECURITY_DOMAIN_LOCAL_GROUP,
31 GTYPE_SECURITY_GLOBAL_GROUP,
32 GTYPE_SECURITY_UNIVERSAL_GROUP,
33 GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP,
34 GTYPE_DISTRIBUTION_GLOBAL_GROUP,
35 GTYPE_DISTRIBUTION_UNIVERSAL_GROUP,
38 security_group = dict({"Domain": GTYPE_SECURITY_DOMAIN_LOCAL_GROUP, "Global": GTYPE_SECURITY_GLOBAL_GROUP, "Universal": GTYPE_SECURITY_UNIVERSAL_GROUP})
39 distribution_group = dict({"Domain": GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP, "Global": GTYPE_DISTRIBUTION_GLOBAL_GROUP, "Universal": GTYPE_DISTRIBUTION_UNIVERSAL_GROUP})
42 class cmd_group_add(Command):
43 """Creates a new group"""
45 synopsis = "%prog group add [options] <groupname>"
47 takes_optiongroups = {
48 "sambaopts": options.SambaOptions,
49 "versionopts": options.VersionOptions,
50 "credopts": options.CredentialsOptions,
54 Option("-H", help="LDB URL for database or target server", type=str),
56 help="Alternative location (without domainDN counterpart) to default CN=Users in which new user object will be created",
58 Option("--group-scope", type="choice", choices=["Domain", "Global", "Universal"],
59 help="Group scope (Domain | Global | Universal)"),
60 Option("--group-type", type="choice", choices=["Security", "Distribution"],
61 help="Group type (Security | Distribution)"),
62 Option("--description", help="Group's description", type=str),
63 Option("--mail-address", help="Group's email address", type=str),
64 Option("--notes", help="Groups's notes", type=str),
67 takes_args = ["groupname"]
69 def run(self, groupname, credopts=None, sambaopts=None,
70 versionopts=None, H=None, groupou=None, group_scope=None,
71 group_type=None, description=None, mail_address=None, notes=None):
73 if (group_type or "Security") == "Security":
74 gtype = security_group.get(group_scope, GTYPE_SECURITY_GLOBAL_GROUP)
76 gtype = distribution_group.get(group_scope, GTYPE_DISTRIBUTION_GLOBAL_GROUP)
78 lp = sambaopts.get_loadparm()
79 creds = credopts.get_credentials(lp)
82 samdb = SamDB(url=H, session_info=system_session(),
83 credentials=creds, lp=lp)
84 samdb.newgroup(groupname, groupou=groupou, grouptype = gtype,
85 description=description, mailaddress=mail_address, notes=notes)
86 except ldb.LdbError, (num, msg):
87 raise CommandError('Failed to create group "%s" : %s' % (
91 class cmd_group_delete(Command):
94 synopsis = "%prog group delete <groupname>"
96 takes_optiongroups = {
97 "sambaopts": options.SambaOptions,
98 "versionopts": options.VersionOptions,
99 "credopts": options.CredentialsOptions,
103 Option("-H", help="LDB URL for database or target server", type=str),
106 takes_args = ["groupname"]
108 def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
110 lp = sambaopts.get_loadparm()
111 creds = credopts.get_credentials(lp)
114 samdb = SamDB(url=H, session_info=system_session(),
115 credentials=creds, lp=lp)
116 samdb.deletegroup(groupname)
117 except ldb.LdbError, (num, msg):
118 raise CommandError('Failed to remove group "%s": %s' % (
122 class cmd_group_add_members(Command):
123 """Add (comma-separated list of) group members"""
125 synopsis = "%prog group addmembers <groupname> <listofmembers>"
127 takes_optiongroups = {
128 "sambaopts": options.SambaOptions,
129 "versionopts": options.VersionOptions,
130 "credopts": options.CredentialsOptions,
134 Option("-H", help="LDB URL for database or target server", type=str),
137 takes_args = ["groupname", "listofmembers"]
139 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
140 versionopts=None, H=None):
142 lp = sambaopts.get_loadparm()
143 creds = credopts.get_credentials(lp)
146 samdb = SamDB(url=H, session_info=system_session(),
147 credentials=creds, lp=lp)
148 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True)
149 except ldb.LdbError, (num, msg):
150 raise CommandError('Failed to add members "%s" to group "%s": %s' % (
151 listofmembers, groupname , msg))
154 class cmd_group_remove_members(Command):
155 """Remove (comma-separated list of) group members"""
157 synopsis = "%prog group removemembers <groupname> <listofmembers>"
159 takes_optiongroups = {
160 "sambaopts": options.SambaOptions,
161 "versionopts": options.VersionOptions,
162 "credopts": options.CredentialsOptions,
166 Option("-H", help="LDB URL for database or target server", type=str),
169 takes_args = ["groupname", "listofmembers"]
171 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
172 versionopts=None, H=None):
174 lp = sambaopts.get_loadparm()
175 creds = credopts.get_credentials(lp)
178 samdb = SamDB(url=H, session_info=system_session(),
179 credentials=creds, lp=lp)
180 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False)
181 except ldb.LdbError, (num, msg):
182 raise CommandError('Failed to remove members "%s" from group "%s": %s' % (
183 listofmembers, groupname , msg))
186 class cmd_group(SuperCommand):
187 """Group management"""
190 subcommands["add"] = cmd_group_add()
191 subcommands["delete"] = cmd_group_delete()
192 subcommands["addmembers"] = cmd_group_add_members()
193 subcommands["removemembers"] = cmd_group_remove_members()