r9735: More work on generating a valid Samba4 configuration using the
[sfrench/samba-autobuild/.git] / source4 / scripting / libjs / upgrade.js
1 /*
2         backend code for upgrading from Samba3
3         Copyright Jelmer Vernooij 2005
4         Released under the GNU GPL v2 or later
5 */
6
7 libinclude("base.js");
8
9 function regkey_to_dn(name)
10 {
11         var dn = "hive=NONE";
12         var i = 0;
13
14         var as = split("/", name);
15
16         for (i in as) {
17                 if (i > 0) {
18                         dn = sprintf("key=%s,", as[i]) + dn;
19                 }
20         }
21
22         return dn;
23 }
24
25 /* Where prefix is any of:
26  * - HKLM
27  *   HKU
28  *   HKCR
29  *   HKPD
30  *   HKPT
31  */
32
33 function upgrade_registry(regdb,prefix)
34 {
35         var prefix_up = strupper(prefix);
36
37         var ldif = "";
38
39         for (var i in regdb.keys) {
40                 var rk = regdb.keys[i];
41                 /* Only handle selected hive */
42                 if (strncmp(prefix_up, rk.name, strlen(prefix_up)) != 0) {
43                         continue;
44                 }
45
46                 var keydn = regkey_to_dn(rk.name);
47
48                 var pts = split("/", rk.name);
49
50                 /* Convert key name to dn */
51                 ldif = ldif + sprintf("
52 dn: %s
53 name: %s
54
55 ", keydn, pts[0]);
56                 
57                 for (var j in rk.values) {
58                         var rv = rk.values[j];
59
60                         ldif = ldif + sprintf("
61 dn: %s,value=%s
62 value: %s
63 type: %d
64 data:: %s", keydn, rv.value, rv.type, base64(rv.data));
65                 }
66         }
67
68         return ldif;
69 }
70
71 function upgrade_sam_domain(samba3)
72 {
73         var ldif = sprintf("
74 dn: %s
75 dc: FIXME
76 objectClass: top
77 objectClass: domain
78 objectSid: %s
79 objectGUID: %s
80 name: %s
81 oEMInformation: Provisioned by Samba4 (upgraded from Samba3)
82 minPwdLength: %d
83 pwdHistoryLength: %d
84 minPwdAge: %d
85 maxPwdAge: %d
86 lockoutDuration: %d
87 samba3ResetCountMinutes: %d
88 samba3UserMustLogonToChangePassword: %d
89 samba3BadLockoutMinutes: %d
90 samba3DisconnectTime: %d
91 samba3RefuseMachinePwdChange: %d
92
93 ", domaindn, domsec.sid, domsec.guid, domainname, samba3.policy.min_password_length, 
94         samba3.policy.password_history, samba3.policy.minimum_password_age,
95         samba3.policy.maximum_password_age, samba3.policy.lockout_duration,
96         samba3.policy.reset_count_minutes, samba3.policy.user_must_logon_to_change_password,
97         samba3.policy.bad_lockout_minutes, samba3.policy.disconnect_time, 
98         samba3.policy.refuse_machine_password_change
99 );
100
101         return ldif;
102 }
103
104 function upgrade_sam_account(acc,domaindn)
105 {
106         var ldif = sprintf(
107 "dn: cn=%s,%s
108 objectClass: top
109 objectClass: person
110 objectClass: user
111 lastLogon: %d
112 lastLogoff: %d
113 unixName: %s
114 name: %s
115 cn: %s
116 description: %s
117 primaryGroupID: %d
118 badPwdcount: %d
119 logonCount: %d
120 samba3Domain: %s
121 samba3DirDrive: %s
122 samba3MungedDial: %s
123 samba3Homedir: %s
124 samba3LogonScript: %s
125 samba3ProfilePath: %s
126 samba3Workstations: %s
127 samba3KickOffTime: %d
128 samba3BadPwdTime: %d
129 samba3PassLastSetTime: %d
130 samba3PassCanChangeTime: %d
131 samba3PassMustChangeTime: %d
132 samba3Rid: %d
133
134 ", acc.fullname, domaindn, sam.logon_time, acc.logoff_time, acc.username, acc.nt_username, 
135 acc.fullname, acc.acct_desc, acc.group_rid, acc.bad_password_count, acc.logon_count,
136 acc.domain, acc.dir_drive, acc.munged_dial, acc.homedir, acc.logon_script, 
137 acc.profile_path, acc.workstations, acc.kickoff_time, acc.bad_password_time, 
138 acc.pass_last_set_time, acc.pass_can_change_time, acc.pass_must_change_time, acc.user_rid); 
139
140                 /* FIXME: Passwords */
141
142         return ldif;
143 }
144
145 function upgrade_sam_group(grp,domaindn)
146 {
147         var ldif = sprintf(
148 "dn: cn=%s,%s
149 objectClass: top
150 objectClass: group
151 description: %s
152 cn: %s
153 objectSid: %s
154 unixName: FIXME
155 samba3SidNameUse: %d", grp.nt_name, domaindn, 
156 grp.comment, grp.nt_name, grp.sid, grp.sid_name_use);
157
158         return ldif;
159 }
160
161 function upgrade_sam(samba3,domaindn)
162 {
163         domainname = samba3.get_param("global", "workgroup");
164
165         if (domainname == undefined) {
166                 DEBUG(0, ("No domain name specified in smb.conf!\n"));
167                 return -1;
168         }
169
170         domsec = samba3.find_domainsecrets(domainname);
171
172         var ldif = upgrade_sam_domain(samba3,domaindn);
173
174         /* Users */
175         for (var i in samba3.samaccounts) {
176                 ldif = ldif + upgrade_sam_account(samba3.samaccounts[i],domaindn);
177         }
178
179         /* Groups */
180         for (var i in samba3.group.groupmappings) {
181                 ldif = ldif + upgrade_sam_group(samba3.group.groupmappings[i],domaindn);
182
183         }
184
185         return count;
186 }
187
188 function upgrade_winbind(samba3,domaindn)
189 {
190         var ldif = sprintf("
191                 
192 dn: dc=none
193 userHwm: %d
194 groupHwm: %d
195
196 ", samba3.idmap.user_hwm, samba3.idmap.group_hwm);
197
198         for (var i in samba3.idmap.mappings) {
199                 var m = samba3.idmap.mappings[i];
200                 ldif = ldif + sprintf("
201 dn: SID=%s,%s
202 SID: %s
203 type: %d
204 unixID: %d", m.sid, domaindn, m.sid, m.type, m.unix_id);
205         }
206         
207         return ldif;
208 }
209 */
210
211 function upgrade_wins(samba3)
212 {
213         var ldif = "";
214         for (i in samba3.winsentries) {
215                 var e = samba3.winsentries[i];
216                 
217                 ldif = ldif + sprintf("
218 dn: type=%d,name=%s
219 name: %s
220 objectClass: wins
221 nbFlags: %x
222 expires: %s", e.type, e.name, e.name, e.type, e.nb_flags, sys.ldap_time(e.ttl));
223
224                 for (var i in e.ips) {
225                         ldif = ldif + sprintf("address: %s\n", e.ips[i]);
226                 }
227         }
228
229         return ldif;
230 }
231
232 function upgrade_provision(samba3)
233 {
234         var subobj = new Object();
235         var nss = nss_init();
236         var lp = loadparm_init();
237         var rdn_list;
238
239         var domainname = samba3.get_param("global", "workgroup");
240         var domsec = samba3.find_domainsecrets(domainname);
241         var hostsec = samba3.find_domainsecrets(hostname());
242         var realm = samba3.get_param("global", "realm");
243         random_init(local);
244
245         subobj.REALM        = realm;
246         subobj.DOMAIN       = domainname;
247         subobj.HOSTNAME     = hostname();
248
249         assert(subobj.REALM);
250         assert(subobj.DOMAIN);
251         assert(subobj.HOSTNAME);
252
253         subobj.HOSTIP       = hostip();
254         subobj.DOMAINGUID   = domsec.guid;
255         subobj.DOMAINSID    = domsec.sid;
256         subobj.HOSTGUID     = hostsec.guid;
257         subobj.INVOCATIONID = randguid();
258         subobj.KRBTGTPASS   = randpass(12);
259         subobj.MACHINEPASS  = randpass(12);
260         subobj.ADMINPASS    = randpass(12);
261         subobj.DEFAULTSITE  = "Default-First-Site-Name";
262         subobj.NEWGUID      = randguid;
263         subobj.NTTIME       = nttime;
264         subobj.LDAPTIME     = ldaptime;
265         subobj.DATESTRING   = datestring;
266         subobj.USN          = nextusn;
267         subobj.ROOT         = findnss(nss.getpwnam, split(samba3.get_param("global", "admin users")));
268         subobj.NOBODY       = findnss(nss.getpwnam, "nobody");
269         subobj.NOGROUP      = findnss(nss.getgrnam, "nogroup", "nobody");
270         subobj.WHEEL        = findnss(nss.getgrnam, "wheel", "root");
271         subobj.USERS        = findnss(nss.getgrnam, "users", "guest", "other");
272         subobj.DNSDOMAIN    = strlower(subobj.REALM);
273         subobj.DNSNAME      = sprintf("%s.%s", 
274                                       strlower(subobj.HOSTNAME), 
275                                       subobj.DNSDOMAIN);
276         subobj.BASEDN       = "DC=" + join(",DC=", split(".", subobj.REALM));
277         rdn_list = split(".", subobj.REALM);
278         subobj.RDN_DC       = rdn_list[0];
279         return subobj;
280 }