r9477: Convert popt options to an ejs object. Doesn't seem to break anything
[kai/samba.git] / source / setup / newuser
1 #!/bin/sh
2 exec smbscript "$0" ${1+"$@"}
3 /*
4         add a new user to a Samba4 server
5         Copyright Andrew Tridgell 2005
6         Released under the GNU GPL v2 or later
7 */
8
9 options = GetOptions(ARGV,
10                 "POPT_AUTOHELP",
11                 "POPT_COMMON_SAMBA",
12                 "POPT_COMMON_VERSION",
13                 'username=s',
14                 'unixname=s',
15                 'password=s',
16                 'quiet');
17
18 if (options == undefined) {
19    println("Failed to parse options: " + options.ERROR);
20    return -1;
21 }
22
23 libinclude("base.js");
24 libinclude("provision.js");
25
26
27 /*
28   print a message if quiet is not set
29 */
30 function message() 
31 {
32         if (options["quiet"] == undefined) {
33                 print(vsprintf(arguments));
34         }
35 }
36
37 /*
38  show some help
39 */
40 function ShowHelp()
41 {
42         print("
43 Samba4 newuser
44
45 newuser [options]
46   --username  USERNAME     choose new username
47   --unixname  USERNAME     choose unix name of new user
48   --password  PASSWORD     set password
49
50 You must provide at least a username
51 ");
52         exit(1);
53 }
54
55 if (options['username'] == undefined) {
56         ShowHelp();
57 }
58 if (options['password'] == undefined) {
59         options.password = randpass(12);
60         printf("chose random password %s\n", options.password);
61 }
62 if (options['unixname'] == undefined) {
63         options.unixname = options.username;
64 }
65
66 var nss = nss_init();
67 if (nss.getpwnam(options.unixname) == undefined) {
68         printf("ERROR: Unix user '%s' does not exist\n", options.unixname);
69         exit(1);
70 }
71
72 newuser(options.username, options.unixname, options.password, message);
73
74 return 0;