1 | #!/usr/bin/python
|
---|
2 | #
|
---|
3 | # Adds a new user to a Samba4 server
|
---|
4 | # Copyright Jelmer Vernooij 2008
|
---|
5 | #
|
---|
6 | # Based on the original in EJS:
|
---|
7 | # Copyright Andrew Tridgell 2005
|
---|
8 | #
|
---|
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.
|
---|
13 | #
|
---|
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.
|
---|
18 | #
|
---|
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/>.
|
---|
21 |
|
---|
22 | import sys
|
---|
23 |
|
---|
24 | # Find right directory when running from source tree
|
---|
25 | sys.path.insert(0, "bin/python")
|
---|
26 |
|
---|
27 | import samba.getopt as options
|
---|
28 | import optparse
|
---|
29 |
|
---|
30 | from getpass import getpass
|
---|
31 | from samba.auth import system_session
|
---|
32 | from samba.samdb import SamDB
|
---|
33 |
|
---|
34 | parser = optparse.OptionParser("newuser [options] <username> [<password>]")
|
---|
35 | sambaopts = options.SambaOptions(parser)
|
---|
36 | parser.add_option_group(sambaopts)
|
---|
37 | parser.add_option_group(options.VersionOptions(parser))
|
---|
38 | credopts = options.CredentialsOptions(parser)
|
---|
39 | parser.add_option_group(credopts)
|
---|
40 | parser.add_option("-H", help="LDB URL for database or target server", type=str)
|
---|
41 | parser.add_option("--unixname", help="Unix Username", type=str)
|
---|
42 | parser.add_option("--must-change-at-next-login", help="Force password to be changed on next login", action="store_true")
|
---|
43 |
|
---|
44 | opts, args = parser.parse_args()
|
---|
45 |
|
---|
46 | if len(args) == 0:
|
---|
47 | parser.print_usage()
|
---|
48 | sys.exit(1)
|
---|
49 |
|
---|
50 | username = args[0]
|
---|
51 | if len(args) > 1:
|
---|
52 | password = args[1]
|
---|
53 | else:
|
---|
54 | password = getpass("New Password: ")
|
---|
55 |
|
---|
56 | if opts.unixname is None:
|
---|
57 | opts.unixname = username
|
---|
58 |
|
---|
59 | lp = sambaopts.get_loadparm()
|
---|
60 | creds = credopts.get_credentials(lp)
|
---|
61 |
|
---|
62 | if opts.H is not None:
|
---|
63 | url = opts.H
|
---|
64 | else:
|
---|
65 | url = lp.get("sam database")
|
---|
66 |
|
---|
67 | samdb = SamDB(url=url, session_info=system_session(), credentials=creds, lp=lp)
|
---|
68 |
|
---|
69 | samdb.newuser(username, opts.unixname, password, force_password_change_at_next_login_req=opts.must_change_at_next_login)
|
---|