r8338: - added a substitute_var() js library function for doing hash driven
authorAndrew Tridgell <tridge@samba.org>
Tue, 12 Jul 2005 02:36:07 +0000 (02:36 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:20:08 +0000 (13:20 -0500)
  substitution of variables in strings

- the js provision script now correctly processes provision.ldif
(This used to be commit c2946003e06c4898ba0444cd0b69d3203753be94)

source4/scripting/libjs/base.js
source4/setup/provision

index f5498789c590f18c0a41121f2a219be9a53d3fb0..c6b05755c4e734902e8ad09c41ac9a5a7717ddeb 100644 (file)
@@ -50,3 +50,32 @@ function check_array_zero(a)
                assert(a[i] == 0);
        }
 }
+
+/*
+  substitute strings of the form ${NAME} in str, replacing
+  with substitutions from subobj
+*/
+function substitute_var(str, subobj)
+{
+       var list = split("${", str);
+       var i;
+       for (i=1;i<list.length;i++) {
+               var list2 = split("}", list[i]);
+               if (list2.length < 2) {
+                       return undefined;
+               }
+               var key = list2[0];
+               var val;
+               if (typeof(subobj[key]) == "undefined") {
+                       val = "${" + key + "}";
+               } else if (typeof(subobj[key]) == "string") {
+                       val = subobj[key];
+               } else {
+                       var fn = subobj[key];
+                       val = fn(key);
+               }
+               list2[0] = "" + val;
+               list[i] = join("", list2);
+       }
+       return join("", list);
+}
index b01ec97a86dc4f1ad05db44d75caae7982fd6324..86bc49e5370c66ea3f61d3032659672429d38f1c 100755 (executable)
@@ -5,7 +5,7 @@
        Released under the GNU GPL v2 or later
 */
 
-options = new Object();
+var options = new Object();
 ok = GetOptions(ARGV, options, 
                "POPT_AUTOHELP",
                "POPT_COMMON_SAMBA",
@@ -35,6 +35,9 @@ if (ok == false) {
 
 libinclude("base.js");
 
+/* used to generate sequence numbers for records */
+next_usn = 1;
+
 /*
   print a message if quiet is not set
 */
@@ -46,139 +49,88 @@ function message(s)
 }
 
 /*
-  find a username from a list of possibilities
+  find a user or group from a list of possibilities
 */
-function finduser()
+function findnss()
 {
-       var i, name = arguments[0];
-       if (options[name] != undefined) {
-               return options[name];
-       }
-       for (i=1;i<arguments.length;i++) {
-               if (getpwnam(arguments[i]) != undefined) {
-                       return arguments[i];
-               }
-       }
-       println("Unable to find user for " + name);
-       exit(1);
-}
-
-/*
-  find a group from a list of possibilities
-*/
-function findgroup()
-{
-       var i, name = arguments[0];
+       var i;
+       assert(arguments.length >= 2);
+       var nssfn = arguments[0];
+       var name = arguments[1];
        if (options[name] != undefined) {
                return options[name];
        }
-       for (i=1;i<arguments.length;i++) {
-               if (getgrnam(arguments[i]) != undefined) {
+       for (i=2;i<arguments.length;i++) {
+               if (nssfn(arguments[i]) != undefined) {
                        return arguments[i];
                }
        }
-       println("Unable to find group for " + name);
+       println("Unable to find user/group for " + name);
        exit(1);
 }
 
-/*
-  return a variable substitution
-*/
-function sub_callback(key)
-{
-       var lkey = strlower(key);
-       if (options[lkey] != undefined) {
-               return options[lkey];
-       }
-       println(key);
-       return "NOTFOUND{" + key + "}";
-}
-
-
 /*
    add a foreign security principle
  */
 function add_foreign(str, sid, desc, unixname)
 {
-       return str + "
-dn: CN=$sid,CN=ForeignSecurityPrincipals,${BASEDN}
+       var add = "
+dn: CN=${SID},CN=ForeignSecurityPrincipals,${BASEDN}
 objectClass: top
 objectClass: foreignSecurityPrincipal
-cn: $sid
-description: $desc
+cn: ${SID}
+description: ${DESC}
 instanceType: 4
 whenCreated: ${LDAPTIME}
 whenChanged: ${LDAPTIME}
 uSNCreated: 1
 uSNChanged: 1
 showInAdvancedViewOnly: TRUE
-name: $sid
+name: ${SID}
 objectGUID: ${NEWGUID}
-objectSid: $sid
+objectSid: ${SID}
 objectCategory: CN=Foreign-Security-Principal,CN=Schema,CN=Configuration,${BASEDN}
-unixName: $unixname
-
+unixName: ${UNIXNAME}
 ";
+       var sub = new Object();
+       sub.SID = sid;
+       sub.DESC = desc;
+       sub.UNIXNAME = unixname;
+       return str + substitute_var(add, sub);
 }
 
 /*
-  generate a random guid
+  return current time as a nt time string
 */
-function randguid()
+function nttime()
 {
-       return "009876-7656";
+       return "" + sys_nttime();
 }
 
 /*
-  generate a random sid
+  return current time as a ldap time string
 */
-function randsid()
+function ldaptime()
 {
-       return "1-2-3";
+       return sys_ldaptime(sys_nttime());
 }
 
 /*
-  generate a random password
+  return current time as a ldap time string
 */
-function randpass()
+function nextusn()
 {
-       return "penguin";
+       next_usn = next_usn+1;
+       return next_usn;
 }
 
 /*
-  return current time as a nt time string
+  return first part of hostname
 */
-function nttime()
+function hostname()
 {
-       return "1st Feb";
-}
-
-/*
-  substitute strings of the form ${NAME} in str, replacing
-  with substitutions from subobj
-*/
-function substitute_var(str)
-{
-       var list = split("${", str);
-       var i;
-       for (i=1;i<list.length;i++) {
-               var list2 = split("}", list[i]);
-               if (list2.length < 2) {
-                       return undefined;
-               }
-               var key = list2[0];
-               if (typeof(subobj[key]) == "string") {
-                       list2[0] = subobj[key];
-               } else {
-                       println("KEY=" + key);
-                       var fn = subobj[key];
-                       list2[0] = fn();
-                       println("list2fn=" + list2[0]);
-               }
-               list[i] = join("", list2);
-               println("XXX[" + key + "]=" + list[i]);
-       }
-       return join("", list);
+       var s = split(".", sys_hostname());
+       return s[0];
 }
 
 /*
@@ -215,6 +167,10 @@ You must provide at least a realm and domain
        exit(1);
 }
 
+if (options['host-name'] == undefined) {
+       options['host-name'] = hostname();
+}
+
 /*
    main program
 */
@@ -227,20 +183,20 @@ if (options["realm"] == undefined ||
 options.realm        = strlower(options.realm);
 options['host-name'] = strlower(options['host-name']);
 options.domain       = strupper(options.domain);
-options.netbiosname  = strupper(options.hostname);
+options.netbiosname  = strupper(options['host-name']);
 
 if (options.hostip == undefined) {
-       var list = IfaceList();
+       var list = sys_interfaces();
        options.hostip = list[0];
 }
 
 message("Provisioning for " + options.domain + " in realm " + options.realm);
 
-options.root    = finduser("root", "root");
-options.nobody  = finduser("nobody", "nobody");
-options.nogroup = findgroup("nogroup", "nogroup", "nobody");
-options.wheel   = findgroup("wheel", "wheel", "root");
-options.users   = findgroup("users", "users", "guest", "other");
+options.root    = findnss(getpwnam, "root", "root");
+options.nobody  = findnss(getpwnam, "nobody", "nobody");
+options.nogroup = findnss(getgrnam, "nogroup", "nogroup", "nobody");
+options.wheel   = findnss(getgrnam, "wheel", "wheel", "root");
+options.users   = findnss(getgrnam, "users", "users", "guest", "other");
 
 
 options.dnsdomain = strlower(options.realm);
@@ -256,21 +212,24 @@ if (data == undefined) {
 /*
   setup the substitution object
 */
-subobj = new Object();
+var subobj = new Object();
 subobj.DOMAINGUID   = randguid();
 subobj.DOMAINSID    = randsid();
 subobj.HOSTGUID     = randguid();
 subobj.INVOCATIONID = randguid();
-subobj.KRBTGTPASS   = randpass();
-subobj.MACHINEPASS  = randpass();
-subobj.ADMINPASS    = randpass();
+subobj.KRBTGTPASS   = randpass(12);
+subobj.MACHINEPASS  = randpass(12);
+subobj.ADMINPASS    = randpass(12);
 subobj.DEFAULTSITE  = "Default-First-Site-Name";
 subobj.NEWGUID      = randguid;
 subobj.NTTIME       = nttime;
+subobj.LDAPTIME     = ldaptime;
+subobj.USN          = nextusn;
 for (r in options) {
-       subobj[strupper(join("", split("-", r)))] = options[r];
+       var key = strupper(join("", split("-", r)));
+       subobj[key] = options[r];
 }
-printVars(subobj);
+
 
 data = add_foreign(data, "S-1-5-7",  "Anonymous",           "${NOBODY}");
 data = add_foreign(data, "S-1-1-0",  "World",               "${NOGROUP}");
@@ -278,7 +237,7 @@ data = add_foreign(data, "S-1-5-2",  "Network",             "${NOGROUP}");
 data = add_foreign(data, "S-1-5-18", "System",              "${ROOT}");
 data = add_foreign(data, "S-1-5-11", "Authenticated Users", "${USERS}");
 
-newdata = substitute_var(data);
+newdata = substitute_var(data, subobj);
 
-print(newdata);
+println(newdata);
 return 0;