3 # add a new user to a Samba4 server
4 # Copyright Andrew Tridgell 2005
5 # Copyright Jelmer Vernooij 2008
6 # Released under the GNU GPL v2 or later
9 import samba.getopt as options
13 from getpass import getpass
14 from auth import system_session
15 from samba.samdb import SamDB
17 parser = optparse.OptionParser("newuser [options] <username> [<password>]")
18 sambaopts = options.SambaOptions(parser)
19 parser.add_option_group(sambaopts)
20 parser.add_option_group(options.VersionOptions(parser))
21 credopts = options.CredentialsOptions(parser)
22 parser.add_option_group(credopts)
23 parser.add_option("--quiet", help="Be quiet", action="store_true")
24 parser.add_option("--unixname", help="Unix Username", type=str)
26 opts, args = parser.parse_args()
29 # print a message if quiet is not set
43 password = getpass("New Password: ")
45 if opts.unixname is None:
46 opts.unixname = username
49 pwd.getpwnam(opts.unixname)
51 print "ERROR: Unix user '%s' does not exist" % opts.unixname
54 creds = credopts.get_credentials()
56 lp = sambaopts.get_loadparm()
57 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(),
58 credentials=creds, lp=lp)
59 samdb.newuser(username, opts.unixname, password)