2 exec smbscript "$0" ${1+"$@"}
4 add a new user to a Samba4 server
5 Copyright Andrew Tridgell 2005
6 Released under the GNU GPL v2 or later
9 options = new Object();
10 ok = GetOptions(ARGV, options,
13 "POPT_COMMON_VERSION",
19 println("Failed to parse options: " + options.ERROR);
23 libinclude("base.js");
25 var samdb = lpGet("sam database");
28 print a message if quiet is not set
32 if (options["quiet"] == undefined) {
33 print(vsprintf(arguments));
38 search for one attribute as a string
40 function search(db, expression, attribute)
42 var attrs = new Array(attribute);
43 res = ldbSearch(db, expression, attrs);
44 if (res.length != 1 ||
45 res[0][attribute] == undefined) {
48 return res[0][attribute];
60 --username USERNAME choose new username
61 --unixname USERNAME choose unix name of new user
62 --password PASSWORD set password
64 You must provide at least a username
69 if (options['username'] == undefined) {
72 if (options['password'] == undefined) {
73 options.password = randpass(12);
74 printf("chose random password %s\n", options.password);
76 if (options['unixname'] == undefined) {
77 options.unixname = options.username;
80 if (getpwnam(options.unixname) == undefined) {
81 printf("ERROR: Unix user '%s' does not exist\n", options.unixname);
85 if (search(samdb, "name=" + options.username, "dn") != undefined) {
86 printf("ERROR: User '%s' already exists\n", options.username);
90 var domain_dn = search(samdb, "objectClass=domainDNS", "dn");
91 assert(domain_dn != undefined);
92 var dom_users = search(samdb, "name=Domain Users", "dn");
93 assert(dom_users != undefined);
95 var user_dn = sprintf("CN=%s,CN=Users,%s", options.username, domain_dn);
98 the new user record. note the reliance on the samdb module to fill
111 user_dn, options.username, options.username, dom_users,
112 options.unixname, randguid(), options.password);
115 add the user to the users group as well
117 var modgroup = sprintf("
122 ", dom_users, user_dn);
127 message("Adding user %s\n", user_dn);
128 ok = ldbAdd(samdb, ldif);
130 message("Failed to add %s\n", user_dn);
134 message("Modifying group %s\n", dom_users);
135 ok = ldbModify(samdb, modgroup);
137 message("Failed to modify %s\n", dom_users);