r3592: auto-cleanup the test.$$ log files in these test scripts on control-C
[samba.git] / source4 / script / rootdse.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5
6 my $opt_hostname = `hostname`;
7 chomp $opt_hostname;
8 my $netbiosname;
9 my $opt_realm;
10 my $opt_domain;
11 my $dnsdomain;
12 my $dnsname;
13 my $basedn;
14
15 sub ldaptime()
16 {
17         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =  gmtime(time);
18         return sprintf "%04u%02u%02u%02u%02u%02u.0Z",
19         $year+1900, $mon+1, $mday, $hour, $min, $sec;
20 }
21
22 #######################
23 # substitute a single variable
24 sub substitute($)
25 {
26         my $var = shift;
27
28         if ($var eq "BASEDN") {
29                 return $basedn;
30         }
31
32         if ($var eq "NETBIOSNAME") {
33                 return $netbiosname;
34         }
35
36         if ($var eq "DNSNAME") {
37                 return $dnsname;
38         }
39
40         if ($var eq "DNSDOMAIN") {
41                 return $dnsdomain;
42         }
43
44         die "ERROR: Uknown substitution variable $var\n";
45 }
46
47 #####################################################################
48 # write a string into a file
49 sub FileSave($$)
50 {
51     my($filename) = shift;
52     my($v) = shift;
53     local(*FILE);
54     open(FILE, ">$filename") || die "can't open $filename";    
55     print FILE $v;
56     close(FILE);
57 }
58
59 #####################################################################
60 # read a file into a string
61 sub FileLoad($)
62 {
63     my($filename) = shift;
64     local(*INPUTFILE);
65     open(INPUTFILE, $filename) || return undef;
66     my($saved_delim) = $/;
67     undef $/;
68     my($data) = <INPUTFILE>;
69     close(INPUTFILE);
70     $/ = $saved_delim;
71     return $data;
72 }
73
74 ############################################
75 # show some help
76 sub ShowHelp()
77 {
78         print "
79 Samba4 provisioning
80
81 rootdse.pl [options]
82   --realm       REALM        set realm
83   --domain      DOMAIN       set domain
84   --hostname    HOSTNAME     set hostname
85
86 You must provide at least a realm and domain
87
88 ";
89         exit(1);
90 }
91
92 my $opt_help;
93
94 GetOptions(
95             'help|h|?' => \$opt_help, 
96             'realm=s' => \$opt_realm,
97             'domain=s' => \$opt_domain,
98             'hostname=s' => \$opt_hostname,
99             );
100
101 if ($opt_help || 
102     !$opt_realm ||
103     !$opt_domain ||
104     !$opt_hostname) {
105         ShowHelp();
106 }
107
108 $opt_realm=uc($opt_realm);
109 $opt_domain=uc($opt_domain);
110 $opt_hostname=lc($opt_hostname);
111 $netbiosname=uc($opt_hostname);
112
113 print "Provisioning host '$opt_hostname' with netbios name '$netbiosname' for domain '$opt_domain' in realm '$opt_realm'\n";
114
115 print "generating ldif ...\n";
116
117 $dnsdomain = lc($opt_realm);
118 $dnsname = $opt_hostname.".".$dnsdomain;
119 $basedn = "DC=" . join(",DC=", split(/\./, $opt_realm));
120
121 my $data = FileLoad("rootdse.ldif") || die "Unable to load rootdse.ldif\n";
122
123 my $res = "";
124
125 print "applying substitutions ...\n";
126
127 while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
128         my $sub = substitute($2);
129         $res .= "$1$sub";
130         $data = $3;
131 }
132 $res .= $data;
133
134 print "saving ldif to newrootdse.ldif ...\n";
135
136 FileSave("newrootdse.ldif", $res);
137
138 unlink("newrootdse.ldb");
139
140 print "creating newrootdse.ldb ...\n";
141
142 # allow provisioning to be run from the source directory
143 $ENV{"PATH"} .= ":bin";
144
145 system("ldbadd -H newrootdse.ldb newrootdse.ldif");
146
147 print "done
148
149 Please move newrootdse.ldb to rootdse.ldb in the lib/private/ directory of your
150 Samba4 installation
151 ";
152