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, CommandError, Option
26 from getpass import getpass
27 from samba.auth import system_session
28 from samba.samdb import SamDB
30 class cmd_newuser(Command):
31 """Creates a new user"""
33 synopsis = "newuser [options] <username> [<password>]"
35 takes_optiongroups = {
36 "sambaopts": options.SambaOptions,
37 "versionopts": options.VersionOptions,
38 "credopts": options.CredentialsOptions,
42 Option("-H", help="LDB URL for database or target server", type=str),
43 Option("--must-change-at-next-login",
44 help="Force password to be changed on next login",
46 Option("--use-username-as-cn",
47 help="Force use of username as user's CN",
50 help="Alternative location (without domainDN counterpart) to default CN=Users in which new user object will be created",
52 Option("--surname", help="User's surname", type=str),
53 Option("--given-name", help="User's given name", type=str),
54 Option("--initials", help="User's initials", type=str),
55 Option("--profile-path", help="User's profile path", type=str),
56 Option("--script-path", help="User's logon script path", type=str),
57 Option("--home-drive", help="User's home drive letter", type=str),
58 Option("--home-directory", help="User's home directory path", type=str),
59 Option("--job-title", help="User's job title", type=str),
60 Option("--department", help="User's department", type=str),
61 Option("--company", help="User's company", type=str),
62 Option("--description", help="User's description", type=str),
63 Option("--mail-address", help="User's email address", type=str),
64 Option("--internet-address", help="User's home page", type=str),
65 Option("--telephone-number", help="User's phone number", type=str),
66 Option("--physical-delivery-office", help="User's office location", type=str),
69 takes_args = ["username", "password?"]
71 def run(self, username, password=None, credopts=None, sambaopts=None,
72 versionopts=None, H=None, must_change_at_next_login=None,
73 use_username_as_cn=None, userou=None, surname=None, given_name=None, initials=None,
74 profile_path=None, script_path=None, home_drive=None, home_directory=None,
75 job_title=None, department=None, company=None, description=None,
76 mail_address=None, internet_address=None, telephone_number=None, physical_delivery_office=None):
79 password = getpass("New Password: ")
81 lp = sambaopts.get_loadparm()
82 creds = credopts.get_credentials(lp)
85 samdb = SamDB(url=H, session_info=system_session(),
86 credentials=creds, lp=lp)
87 samdb.newuser(username, password,
88 force_password_change_at_next_login_req=must_change_at_next_login,
89 useusernameascn=use_username_as_cn, userou=userou, surname=surname, givenname=given_name, initials=initials,
90 profilepath=profile_path, homedrive=home_drive, scriptpath=script_path, homedirectory=home_directory,
91 jobtitle=job_title, department=department, company=company, description=description,
92 mailaddress=mail_address, internetaddress=internet_address,
93 telephonenumber=telephone_number, physicaldeliveryoffice=physical_delivery_office)
94 except ldb.LdbError, (num, msg):
95 raise CommandError('Failed to create user "%s" : %s' % (