c00511f3735598e1bb7060d1ee745e390a05d6fa
[samba.git] / source4 / script / provision.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Socket;
5 use Getopt::Long;
6
7 my $opt_hostname = `hostname`;
8 chomp $opt_hostname;
9 my $opt_hostip;
10 my $opt_realm;
11 my $opt_domain;
12 my $opt_adminpass;
13 my $opt_nobody;
14 my $opt_nogroup;
15 my $opt_wheel;
16 my $opt_users;
17 my $dnsdomain;
18 my $netbiosname;
19 my $dnsname;
20 my $basedn;
21 my $defaultsite = "Default-First-Site-Name";
22
23 # return the current NTTIME as an integer
24 sub nttime()
25 {
26         my $t = time();
27         $t += (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60));
28         $t *= 1.0e7;
29         return sprintf("%lld", $t);
30 }
31
32 # generate a random guid. Not a good algorithm.
33 sub randguid()
34 {
35         my $r1 = int(rand(2**32));
36         my $r2 = int(rand(2**16));
37         my $r3 = int(rand(2**16));
38         my $r4 = int(rand(2**16));
39         my $r5 = int(rand(2**32));
40         my $r6 = int(rand(2**16));
41         return sprintf("%08x-%04x-%04x-%04x-%08x%04x", $r1, $r2, $r3, $r4, $r5, $r6);
42 }
43
44 my $opt_domainguid = randguid();
45 my $hostguid = randguid();
46
47 sub randsid()
48 {
49         return sprintf("S-1-5-21-%d-%d-%d", 
50                        int(rand(10**8)), int(rand(10**8)), int(rand(10**8)));
51 }
52
53 my $opt_domainsid = randsid();
54
55 # generate a random password. Poor algorithm :(
56 sub randpass()
57 {
58         my $pass = "";
59         my $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%\$!~";
60         for (my $i=0;$i<8;$i++) {
61                 my $c = int(rand(length($chars)));
62                 $pass .= substr($chars, $c, 1);
63         }
64         return $pass;
65 }
66
67 sub ldaptime()
68 {
69         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =  gmtime(time);
70         return sprintf "%04u%02u%02u%02u%02u%02u.0Z",
71         $year+1900, $mon+1, $mday, $hour, $min, $sec;
72 }
73
74 #######################
75 # substitute a single variable
76 sub substitute($)
77 {
78         my $var = shift;
79
80         if ($var eq "BASEDN") {
81                 return $basedn;
82         }
83
84         if ($var eq "DOMAINSID") {
85                 return $opt_domainsid;
86         }
87
88         if ($var eq "DOMAIN") {
89                 return $opt_domain;
90         }
91
92         if ($var eq "REALM") {
93                 return $opt_realm;
94         }
95
96         if ($var eq "DNSDOMAIN") {
97                 return $dnsdomain;
98         }
99
100         if ($var eq "HOSTNAME") {
101                 return $opt_hostname;
102         }
103
104         if ($var eq "NETBIOSNAME") {
105                 return $netbiosname;
106         }
107
108         if ($var eq "DNSNAME") {
109                 return $dnsname;
110         }
111
112         if ($var eq "HOSTIP") {
113                 return $opt_hostip;
114         }
115
116         if ($var eq "LDAPTIME") {
117                 return ldaptime();
118         }
119
120         if ($var eq "NEWGUID") {
121                 return randguid();
122         }
123
124         if ($var eq "DOMAINGUID") {
125                 return $opt_domainguid;
126         }
127
128         if ($var eq "HOSTGUID") {
129                 return $hostguid;
130         }
131
132         if ($var eq "DEFAULTSITE") {
133                 return $defaultsite;
134         }
135
136         if ($var eq "ADMINPASS") {
137                 return $opt_adminpass;
138         }
139
140         if ($var eq "RANDPASS") {
141             return randpass();
142         }
143
144         if ($var eq "NTTIME") {
145                 return "" . nttime();
146         }
147
148         if ($var eq "WHEEL") {
149                 return $opt_wheel;
150         }
151
152         if ($var eq "NOBODY") {
153                 return $opt_nobody;
154         }
155
156         if ($var eq "NOGROUP") {
157                 return $opt_nogroup;
158         }
159
160         if ($var eq "USERS") {
161                 return $opt_users;
162         }
163
164         die "ERROR: Uknown substitution variable $var\n";
165 }
166
167 #####################################################################
168 # write a string into a file
169 sub FileSave($$)
170 {
171     my($filename) = shift;
172     my($v) = shift;
173     local(*FILE);
174     open(FILE, ">$filename") || die "can't open $filename";    
175     print FILE $v;
176     close(FILE);
177 }
178
179 #####################################################################
180 # read a file into a string
181 sub FileLoad($)
182 {
183     my($filename) = shift;
184     local(*INPUTFILE);
185     open(INPUTFILE, $filename) || return undef;
186     my($saved_delim) = $/;
187     undef $/;
188     my($data) = <INPUTFILE>;
189     close(INPUTFILE);
190     $/ = $saved_delim;
191     return $data;
192 }
193
194 #######################################################################
195 # add a foreign security principle
196 sub add_foreign($$$)
197 {
198         my $sid = shift;
199         my $desc = shift;
200         my $unixname = shift;
201         return "
202 dn: CN=$sid,CN=ForeignSecurityPrincipals,\${BASEDN}
203 objectClass: top
204 objectClass: foreignSecurityPrincipal
205 cn: $sid
206 description: $desc
207 distinguishedName: CN=$sid,CN=ForeignSecurityPrincipals,\${BASEDN}
208 instanceType: 4
209 whenCreated: \${LDAPTIME}
210 whenChanged: \${LDAPTIME}
211 uSNCreated: 1
212 uSNChanged: 1
213 showInAdvancedViewOnly: TRUE
214 name: $sid
215 objectGUID: \${NEWGUID}
216 objectSid: $sid
217 objectCategory: CN=Foreign-Security-Principal,CN=Schema,CN=Configuration,\${BASEDN}
218 unixName: $unixname
219
220 ";
221 }
222
223 ############################################
224 # show some help
225 sub ShowHelp()
226 {
227         print "
228 Samba4 provisioning
229
230 provision.pl [options]
231   --realm     REALM        set realm
232   --domain    DOMAIN       set domain
233   --hostname  HOSTNAME     set hostname
234   --hostip    IPADDRESS    set ipaddress
235   --adminpass PASSWORD     choose admin password (otherwise random)
236   --nobody    USERNAME     choose 'nobody' user
237   --nogroup   GROUPNAME    choose 'nogroup' group
238   --wheel     GROUPNAME    choose 'wheel' privileged group
239   --users     GROUPNAME    choose 'users' group
240
241 You must provide at least a realm and domain
242
243 ";
244         exit(1);
245 }
246
247 my $opt_help;
248
249 GetOptions(
250             'help|h|?' => \$opt_help, 
251             'realm=s' => \$opt_realm,
252             'domain=s' => \$opt_domain,
253             'domain-guid=s' => \$opt_domainguid,
254             'domain-sid=s' => \$opt_domainsid,
255             'hostname=s' => \$opt_hostname,
256             'hostip=s' => \$opt_hostip,
257             'adminpass=s' => \$opt_adminpass,
258             'nobody=s' => \$opt_nobody,
259             'nogroup=s' => \$opt_nogroup,
260             'wheel=s' => \$opt_wheel,
261             'users=s' => \$opt_users,
262             );
263
264 if ($opt_help || 
265     !$opt_realm ||
266     !$opt_domain ||
267     !$opt_hostname) {
268         ShowHelp();
269 }
270
271 $opt_realm=uc($opt_realm);
272 $opt_domain=uc($opt_domain);
273 $opt_hostname=lc($opt_hostname);
274 $netbiosname=uc($opt_hostname);
275
276 if (!$opt_hostip) {
277         my $hip = gethostbyname($opt_hostname);
278         if (defined $hip) {
279                 $opt_hostip = inet_ntoa($hip);
280         } else {
281                 $opt_hostip = "<0.0.0.0>";
282         }
283 }
284
285 print "Provisioning host '$opt_hostname'[$opt_hostip] for domain '$opt_domain' in realm '$opt_realm'\n";
286
287 if (!$opt_nobody) {
288         if (defined getpwnam("nobody")) {
289                 $opt_nobody = "nobody";
290         }
291 }
292
293 if (!$opt_nogroup) {
294         if (defined getgrnam("nogroup")) {
295                 $opt_nogroup = "nogroup";
296         } elsif (defined getgrnam("nobody")) {
297                 $opt_nogroup = "nobody";
298         }
299 }
300
301 if (!$opt_wheel) {
302         if (defined getgrnam("wheel")) {
303                 $opt_wheel = "wheel";
304         } elsif (defined getgrnam("root")) {
305                 $opt_wheel = "root";
306         }
307 }
308
309 if (!$opt_users) {
310         if (defined getgrnam("users")) {
311                 $opt_users = "users";
312         }
313 }
314
315 $opt_nobody || die "Unable to determine a user for 'nobody'\n";
316 $opt_nogroup || die "Unable to determine a group for 'nogroup'\n";
317 $opt_users || die "Unable to determine a group for 'user'\n";
318 $opt_wheel || die "Unable to determine a group for 'wheel'\n";
319
320 print "Using nobody='$opt_nobody'  nogroup='$opt_nogroup'  wheel='$opt_wheel'  users='$opt_users'\n";
321
322 print "generating ldif ...\n";
323
324 $dnsdomain = lc($opt_realm);
325 $dnsname = lc($opt_hostname).".".$dnsdomain;
326 $basedn = "DC=" . join(",DC=", split(/\./, $opt_realm));
327
328 my $data = FileLoad("provision.ldif") || die "Unable to load provision.ldif\n";
329
330 $data .= add_foreign("S-1-5-7", "Anonymous", "\${NOBODY}");
331 $data .= add_foreign("S-1-1-0", "World", "\${NOGROUP}");
332 $data .= add_foreign("S-1-5-2", "Network", "\${NOGROUP}");
333 $data .= add_foreign("S-1-5-18", "System", "root");
334 $data .= add_foreign("S-1-5-11", "Authenticated Users", "\${USERS}");
335
336 if (!$opt_adminpass) {
337         $opt_adminpass = randpass();
338         print "chose random Administrator password '$opt_adminpass'\n";
339 }
340
341 my $res = "";
342
343 print "applying substitutions ...\n";
344
345 while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
346         my $sub = substitute($2);
347         $res .= "$1$sub";
348         $data = $3;
349 }
350 $res .= $data;
351
352 print "saving ldif to newsam.ldif ...\n";
353
354 FileSave("newsam.ldif", $res);
355
356 unlink("newsam.ldb");
357
358 print "creating newsam.ldb ...\n";
359
360 # allow provisioning to be run from the source directory
361 $ENV{"PATH"} .= ":bin";
362
363 system("ldbadd -H newsam.ldb newsam.ldif");
364
365 print "done\n";
366
367 print "generating dns zone file ...\n";
368
369 $data = FileLoad("provision.zone") || die "Unable to load provision.zone\n";
370
371 $res = "";
372
373 print "applying substitutions ...\n";
374
375 while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
376         my $sub = substitute($2);
377         $res .= "$1$sub";
378         $data = $3;
379 }
380 $res .= $data;
381
382 print "saving dns zone to newdns.zone ...\n";
383
384 FileSave("$dnsdomain.zone", $res);
385
386 print "done
387
388 Installation:
389 - Please move newsam.ldb to sam.ldb in the lib/private/ directory of your
390   Samba4 installation
391 - Please use $dnsdomain.zone to in BIND dns server
392 ";