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
|
#!/usr/bin/env smbscript
/*
provision a Samba4 server
Copyright Andrew Tridgell 2005
Released under the GNU GPL v2 or later
*/
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");
/*
print a message if quiet is not set
*/
function message(s)
{
if (options["quiet"] == undefined) {
println(s);
}
}
/*
find a username from a list of possibilities
*/
function finduser()
{
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];
if (options[name] != undefined) {
return options[name];
}
for (i=1;i<arguments.length;i++) {
if (getgrnam(arguments[i]) != undefined) {
return arguments[i];
}
}
println("Unable to find 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}
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
";
}
/*
generate a random guid
*/
function randguid()
{
return "009876-7656";
}
/*
generate a random sid
*/
function randsid()
{
return "1-2-3";
}
/*
generate a random password
*/
function randpass()
{
return "penguin";
}
/*
return current time as a nt time string
*/
function nttime()
{
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);
}
/*
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);
}
/*
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.hostname);
if (options.hostip == undefined) {
var list = IfaceList();
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.dnsdomain = strlower(options.realm);
options.dnsname = strlower(options['host-name']) + "." + options.dnsdomain;
options.basedn = "DC=" + join(",DC=", split(".", options.realm));
var data = FileLoad("setup/provision.ldif");
if (data == undefined) {
println("Unable to load provision.ldif");
exit(1);
}
/*
setup the substitution object
*/
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.DEFAULTSITE = "Default-First-Site-Name";
subobj.NEWGUID = randguid;
subobj.NTTIME = nttime;
for (r in options) {
subobj[strupper(join("", split("-", r)))] = options[r];
}
printVars(subobj);
data = add_foreign(data, "S-1-5-7", "Anonymous", "${NOBODY}");
data = add_foreign(data, "S-1-1-0", "World", "${NOGROUP}");
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);
print(newdata);
return 0;
|