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, fallback_machine=True)
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)
87 raise CommandError('Failed to create group "%s"' % groupname, e)
90 class cmd_group_delete(Command):
93 synopsis = "%prog group delete <groupname>"
95 takes_optiongroups = {
96 "sambaopts": options.SambaOptions,
97 "versionopts": options.VersionOptions,
98 "credopts": options.CredentialsOptions,
102 Option("-H", help="LDB URL for database or target server", type=str),
105 takes_args = ["groupname"]
107 def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
109 lp = sambaopts.get_loadparm()
110 creds = credopts.get_credentials(lp, fallback_machine=True)
113 samdb = SamDB(url=H, session_info=system_session(),
114 credentials=creds, lp=lp)
115 samdb.deletegroup(groupname)
117 raise CommandError('Failed to remove group "%s"' % groupname, e)
120 class cmd_group_add_members(Command):
121 """Add (comma-separated list of) group members"""
123 synopsis = "%prog group addmembers <groupname> <listofmembers>"
125 takes_optiongroups = {
126 "sambaopts": options.SambaOptions,
127 "versionopts": options.VersionOptions,
128 "credopts": options.CredentialsOptions,
132 Option("-H", help="LDB URL for database or target server", type=str),
135 takes_args = ["groupname", "listofmembers"]
137 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
138 versionopts=None, H=None):
140 lp = sambaopts.get_loadparm()
141 creds = credopts.get_credentials(lp, fallback_machine=True)
144 samdb = SamDB(url=H, session_info=system_session(),
145 credentials=creds, lp=lp)
146 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True)
148 raise CommandError('Failed to add members "%s" to group "%s"' % (listofmembers, groupname), e)
151 class cmd_group_remove_members(Command):
152 """Remove (comma-separated list of) group members"""
154 synopsis = "%prog group removemembers <groupname> <listofmembers>"
156 takes_optiongroups = {
157 "sambaopts": options.SambaOptions,
158 "versionopts": options.VersionOptions,
159 "credopts": options.CredentialsOptions,
163 Option("-H", help="LDB URL for database or target server", type=str),
166 takes_args = ["groupname", "listofmembers"]
168 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
169 versionopts=None, H=None):
171 lp = sambaopts.get_loadparm()
172 creds = credopts.get_credentials(lp, fallback_machine=True)
175 samdb = SamDB(url=H, session_info=system_session(),
176 credentials=creds, lp=lp)
177 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False)
179 raise CommandError('Failed to remove members "%s" from group "%s"' % (listofmembers, groupname), e)
182 class cmd_group(SuperCommand):
183 """Group management"""
186 subcommands["add"] = cmd_group_add()
187 subcommands["delete"] = cmd_group_delete()
188 subcommands["addmembers"] = cmd_group_add_members()
189 subcommands["removemembers"] = cmd_group_remove_members()