Clean up the ldb python bindings to be 64 bit safe.
[samba.git] / source4 / setup / newuser.py
1 #!/usr/bin/python
2 #
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
7 #
8
9 import samba.getopt as options
10 import optparse
11 import pwd
12 import sys
13
14 from auth import system_session
15 from samba.samdb import SamDB
16
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)
25
26 opts, args = parser.parse_args()
27
28 #
29 #  print a message if quiet is not set
30 #
31 def message(text):
32         if not opts.quiet:
33                 print text
34
35 if len(args) == 0:
36         parser.print_usage()
37         sys.exit(1)
38
39 username = args[0]
40 if len(args) > 1:
41         password = args[1]
42 else:
43         random_init(local)
44         options.password = randpass(12)
45         print "chose random password %s\n" % password
46
47 if opts.unixname is None:
48         opts.unixname = username
49
50 try:
51         pwd.getpwnam(opts.unixname)
52 except KeyError:
53         print "ERROR: Unix user '%s' does not exist" % opts.unixname
54         sys.exit(1)
55
56 creds = credopts.get_credentials()
57
58 lp = sambaopts.get_loadparm()
59 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(), 
60               credentials=creds, lp=lp)
61 samdb.newuser(username, opts.unixname, password)