summaryrefslogtreecommitdiff
path: root/source4/setup/provision
blob: 8766688de9c7c9f306564e8ea596a258b358fceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env smbscript
/*
	provision a Samba4 server
	Copyright Andrew Tridgell 2005
	Released under the GNU GPL v2 or later
*/

var options = new Object();
ok = GetOptions(ARGV, options, 
		"POPT_AUTOHELP",
		"POPT_COMMON_SAMBA",
		"POPT_COMMON_VERSION",
		'realm=s',
		'domain=s',
		'domain-guid=s',
		'domain-sid=s',
		'host-name=s',
		'host-ip=s',
		'host-guid=s',
		'invocationid=s',
		'adminpass=s',
		'krbtgtpass=s',
		'machinepass=s',
		'root=s',
		'nobody=s',
		'nogroup=s',
		'wheel=s',
		'users=s',
		'outputdir=s',
		'quiet');
if (ok == false) {
   println("Failed to parse options: " + options.ERROR);
   return -1;
}

libinclude("base.js");

/* used to generate sequence numbers for records */
next_usn = 1;

/*
  print a message if quiet is not set
*/
function message(s) 
{
	if (options["quiet"] == undefined) {
		println(s);
	}
}

/*
  find a user or group from a list of possibilities
*/
function findnss()
{
	var i;
	assert(arguments.length >= 2);
	var nssfn = arguments[0];
	var name = arguments[1];
	if (options[name] != undefined) {
		return options[name];
	}
	for (i=2;i<arguments.length;i++) {
		if (nssfn(arguments[i]) != undefined) {
			return arguments[i];
		}
	}
	println("Unable to find user/group for " + name);
	exit(1);
}

/*
   add a foreign security principle
 */
function add_foreign(str, sid, desc, unixname)
{
	var add = "
dn: CN=${SID},CN=ForeignSecurityPrincipals,${BASEDN}
objectClass: top
objectClass: foreignSecurityPrincipal
cn: ${SID}
description: ${DESC}
instanceType: 4
whenCreated: ${LDAPTIME}
whenChanged: ${LDAPTIME}
uSNCreated: 1
uSNChanged: 1
showInAdvancedViewOnly: TRUE
name: ${SID}
objectGUID: ${NEWGUID}
objectSid: ${SID}
objectCategory: CN=Foreign-Security-Principal,CN=Schema,CN=Configuration,${BASEDN}
unixName: ${UNIXNAME}
";
	var sub = new Object();
	sub.SID = sid;
	sub.DESC = desc;
	sub.UNIXNAME = unixname;
	return str + substitute_var(add, sub);
}

/*
  return current time as a nt time string
*/
function nttime()
{
	return "" + sys_nttime();
}

/*
  return current time as a ldap time string
*/
function ldaptime()
{
	return sys_ldaptime(sys_nttime());
}

function datestring()
{
	var t = sys_gmtime(sys_nttime());
	return sprintf("%04u%02u%02u%02u",
		       t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour);
}

/*
  return current time as a ldap time string
*/
function nextusn()
{
	next_usn = next_usn+1;
	return next_usn;
}

/*
  return first part of hostname
*/
function hostname()
{
	var s = split(".", sys_hostname());
	return s[0];
}


/*
  setup a ldb in the private dir
 */
function setup_ldb(ldif, dbname, subobj)
{
	var extra = "";
	if (arguments.length == 4) {
		extra = arguments[3];
	}
	printVars(lpGet("setup directory"));

	var db = lpGet("private dir") + "/" + dbname;
	var src = lpGet("setup directory") + "/" + ldif;

	sys_unlink(db);

	var data = sys_file_load(src);
	data = data + extra;
	data = substitute_var(data, subobj);

	message("Creating " + db + "\n from " + src);
	ok = ldbAdd(db, data);
	assert(ok);
}

/*
  setup a file in the private dir
 */
function setup_file(template, fname, subobj)
{
	var f = lpGet("private dir") + "/" + fname;
	var src = lpGet("setup directory") + "/" + template;

	sys_unlink(f);

	var data = sys_file_load(src);
	data = substitute_var(data, subobj);

	message("Creating " + f + "\n from " + src);
	ok = sys_file_save(f, data);
	assert(ok);
}

/*
 show some help
*/
function ShowHelp()
{
	print("
Samba4 provisioning

provision.pl [options]
 --realm	REALM		set realm
 --domain	DOMAIN		set domain
 --domain-guid	GUID		set domainguid (otherwise random)
 --domain-sid	SID		set domainsid (otherwise random)
 --host-name	HOSTNAME	set hostname
 --host-ip	IPADDRESS	set ipaddress
 --host-guid	GUID		set hostguid (otherwise random)
 --invocationid	GUID		set invocationid (otherwise random)
 --outputdir	OUTPUTDIR	set output directory
 --adminpass	PASSWORD	choose admin password (otherwise random)
 --krbtgtpass	PASSWORD	choose krbtgt password (otherwise random)
 --machinepass	PASSWORD	choose machine password (otherwise random)
 --root         USERNAME	choose 'root' unix username
 --nobody	USERNAME	choose 'nobody' user
 --nogroup	GROUPNAME	choose 'nogroup' group
 --wheel	GROUPNAME	choose 'wheel' privileged group
 --users	GROUPNAME	choose 'users' group
 --quiet			Be quiet

You must provide at least a realm and domain

");
	exit(1);
}

if (options['host-name'] == undefined) {
	options['host-name'] = hostname();
}

/*
   main program
*/
if (options["realm"] == undefined ||
    options["domain"] == undefined ||
    options["host-name"] == undefined) {
	ShowHelp();
}

options.realm        = strlower(options.realm);
options['host-name'] = strlower(options['host-name']);
options.domain       = strupper(options.domain);
options.netbiosname  = strupper(options['host-name']);

if (options.hostip == undefined) {
	var list = sys_interfaces();
	options.hostip = list[0];
}

message("Provisioning for " + options.domain + " in realm " + options.realm);

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);
options.dnsname   = strlower(options['host-name']) + "." + options.dnsdomain;
options.basedn    = "DC=" + join(",DC=", split(".", options.realm));

/*
  setup the substitution object
*/
var subobj = new Object();
subobj.DOMAINGUID   = randguid();
subobj.DOMAINSID    = randsid();
subobj.HOSTGUID     = randguid();
subobj.INVOCATIONID = randguid();
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.DATESTRING   = datestring;
subobj.USN          = nextusn;
for (r in options) {
	var key = strupper(join("", split("-", r)));
	subobj[key] = options[r];
}


var extradata = "";
extradata = add_foreign(extradata, "S-1-5-7",  "Anonymous",           "${NOBODY}");
extradata = add_foreign(extradata, "S-1-1-0",  "World",               "${NOGROUP}");
extradata = add_foreign(extradata, "S-1-5-2",  "Network",             "${NOGROUP}");
extradata = add_foreign(extradata, "S-1-5-18", "System",              "${ROOT}");
extradata = add_foreign(extradata, "S-1-5-11", "Authenticated Users", "${USERS}");

message("Using administrator password: " + subobj.ADMINPASS);

setup_ldb("hklm.ldif", "hklm.ldb", subobj);
setup_ldb("provision.ldif", "sam.ldb", subobj, extradata);
setup_ldb("rootdse.ldif", "rootdse.ldb", subobj);
setup_ldb("secrets.ldif", "secrets.ldb", subobj);
setup_file("provision.zone", subobj.DNSDOMAIN + ".zone", subobj);

message("All OK");
return 0;