fix case in objectclass name (not that it really matters); patch from Darren Chew...
[nivanova/samba-autobuild/.git] / examples / LDAP / smbldap-tools / smbldap-populate.pl
1 #!/usr/bin/perl -w
2
3 # Populate a LDAP base for Samba-LDAP usage
4 #
5
6 #  This code was developped by IDEALX (http://IDEALX.org/) and
7 #  contributors (their names can be found in the CONTRIBUTORS file).
8 #
9 #                 Copyright (C) 2001-2002 IDEALX
10 #
11 #  This program is free software; you can redistribute it and/or
12 #  modify it under the terms of the GNU General Public License
13 #  as published by the Free Software Foundation; either version 2
14 #  of the License, or (at your option) any later version.
15 #
16 #  This program is distributed in the hope that it will be useful,
17 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #  GNU General Public License for more details.
20 #
21 #  You should have received a copy of the GNU General Public License
22 #  along with this program; if not, write to the Free Software
23 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #  USA.
25
26 #  Purpose :
27 #       . Create an initial LDAP database suitable for Samba 2.2
28 #       . For lazy people, replace ldapadd (with only an ldif parameter)
29
30 use strict;
31 use FindBin;
32 use FindBin qw($RealBin);
33 use lib "$RealBin/";
34 use smbldap_tools;
35 use smbldap_conf;
36 use Getopt::Std;
37 use Net::LDAP::LDIF;
38
39 use vars qw(%oc);
40
41 # objectclass of the suffix
42 %oc = (
43            "ou" => "organizationalUnit",
44            "o" => "organization",
45            "dc" => "dcObject",
46           );
47
48
49 my %Options;
50
51 my $ok = getopts('a:b:?', \%Options);
52 if ( (!$ok) || ($Options{'?'}) ) {
53   print "Usage: $0 [-ab?] [ldif]\n";
54   print "  -a   administrator login name (default: Administrator)\n";
55   print "  -b   guest login name (default: nobody)\n";
56   print "  -?   show this help message\n";
57   print "  ldif file to add to ldap (default: suffix, Groups,";
58   print " Users, Computers and builtin users )\n";
59   exit (1);
60 }
61
62 my $_ldifName;
63 my $tmp_ldif_file="/tmp/$$.ldif";
64
65 if (@ARGV >= 1) {
66   $_ldifName = $ARGV[0];
67 }
68
69 my $adminName = $Options{'a'};
70 if (!defined($adminName)) {
71   $adminName = "Administrator";
72 }
73
74 my $guestName = $Options{'b'};
75 if (!defined($guestName)) {
76   $guestName = "nobody";
77 }
78
79 if (!defined($_ldifName)) {
80   my $attr;
81   my $val;
82   my $objcl;
83
84   print "Using builtin directory structure\n";
85   if ($suffix =~ m/([^=]+)=([^,]+)/) {
86         $attr = $1;
87         $val = $2;
88         $objcl = $oc{$attr} if (exists $oc{$attr});
89         if (!defined($objcl)) {
90           $objcl = "myhardcodedobjectclass";
91         }
92   } else {
93         die "can't extract first attr and value from suffix $suffix";
94   }
95   #print "$attr=$val\n";
96   my ($organisation,$ext) = ($suffix =~ m/dc=(.*),dc=(.*)$/);
97
98   #my $FILE="|cat";
99   my $FILE=$tmp_ldif_file;
100   open (FILE, ">$FILE") || die "Can't open file $FILE: $!\n";
101
102   print FILE <<EOF;
103 dn: $suffix
104 objectClass: $objcl
105 objectclass: organization
106 $attr: $val
107 o: $organisation
108
109 dn: $usersdn
110 objectClass: organizationalUnit
111 ou: $usersou
112
113 dn: $groupsdn
114 objectClass: organizationalUnit
115 ou: $groupsou
116
117 dn: $computersdn
118 objectClass: organizationalUnit
119 ou: $computersou
120
121 dn: uid=$adminName,$usersdn
122 cn: $adminName
123 sn: $adminName
124 objectClass: inetOrgPerson
125 objectClass: sambaSamAccount
126 objectClass: posixAccount
127 gidNumber: 512
128 uid: $adminName
129 uidNumber: 998
130 homeDirectory: $_userHomePrefix
131 sambaPwdLastSet: 0
132 sambaLogonTime: 0
133 sambaLogoffTime: 2147483647
134 sambaKickoffTime: 2147483647
135 sambaPwdCanChange: 0
136 sambaPwdMustChange: 2147483647
137 sambaHomePath: $_userSmbHome
138 sambaHomeDrive: $_userHomeDrive
139 sambaProfilePath: $_userProfile
140 sambaPrimaryGroupSID: $SID-512
141 sambaLMPassword: XXX
142 sambaNTPassword: XXX
143 sambaAcctFlags: [U          ]
144 sambaSID: $SID-2996
145 loginShell: /bin/false
146 gecos: Netbios Domain Administrator
147
148 dn: uid=$guestName,$usersdn
149 cn: $guestName
150 sn: $guestName
151 objectClass: inetOrgPerson
152 objectClass: sambaSamAccount
153 objectClass: posixAccount
154 gidNumber: 514
155 uid: $guestName
156 uidNumber: 999
157 homeDirectory: /dev/null
158 sambaPwdLastSet: 0
159 sambaLogonTime: 0
160 sambaLogoffTime: 2147483647
161 sambaKickoffTime: 2147483647
162 sambaPwdCanChange: 0
163 sambaPwdMustChange: 2147483647
164 sambaHomePath: $_userSmbHome
165 sambaHomeDrive: $_userHomeDrive
166 sambaProfilePath: $_userProfile
167 sambaPrimaryGroupSID: $SID-514
168 sambaLMPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
169 sambaNTPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
170 sambaAcctFlags: [NU         ]
171 sambaSID: $SID-2998
172 loginShell: /bin/false
173
174 dn: cn=Domain Admins,$groupsdn
175 objectClass: posixGroup
176 objectClass: sambaGroupMapping
177 gidNumber: 512
178 cn: Domain Admins
179 memberUid: $adminName
180 description: Netbios Domain Administrators
181 sambaSID: $SID-512
182 sambaGroupType: 2
183 displayName: Domain Admins
184
185 dn: cn=Domain Users,$groupsdn
186 objectClass: posixGroup
187 objectClass: sambaGroupMapping
188 gidNumber: 513
189 cn: Domain Users
190 description: Netbios Domain Users
191 sambaSID: $SID-513
192 sambaGroupType: 2
193 displayName: Domain Users
194
195 dn: cn=Domain Guests,$groupsdn
196 objectClass: posixGroup
197 objectClass: sambaGroupMapping
198 gidNumber: 514
199 cn: Domain Guests
200 description: Netbios Domain Guests Users
201 sambaSID: $SID-514
202 sambaGroupType: 2
203 displayName: Domain Guests
204
205 dn: cn=Administrators,$groupsdn
206 objectClass: posixGroup
207 objectClass: sambaGroupMapping
208 gidNumber: 544
209 cn: Administrators
210 description: Netbios Domain Members can fully administer the computer/sambaDomainName
211 sambaSID: $SID-544
212 sambaGroupType: 2
213 displayName: Administrators
214
215 dn: cn=Users,$groupsdn
216 objectClass: posixGroup
217 objectClass: sambaGroupMapping
218 gidNumber: 545
219 cn: Users
220 description: Netbios Domain Ordinary users
221 sambaSID: $SID-545
222 sambaGroupType: 2
223 displayName: users
224
225 dn: cn=Guests,$groupsdn
226 objectClass: posixGroup
227 objectClass: sambaGroupMapping
228 gidNumber: 546
229 cn: Guests
230 memberUid: $guestName
231 description: Netbios Domain Users granted guest access to the computer/sambaDomainName
232 sambaSID: $SID-546
233 sambaGroupType: 2
234 displayName: Guests
235
236 dn: cn=Power Users,$groupsdn
237 objectClass: posixGroup
238 objectClass: sambaGroupMapping
239 gidNumber: 547
240 cn: Power Users
241 description: Netbios Domain Members can share directories and printers
242 sambaSID: $SID-547
243 sambaGroupType: 2
244 displayName: Power Users
245
246 dn: cn=Account Operators,$groupsdn
247 objectClass: posixGroup
248 objectClass: sambaGroupMapping
249 gidNumber: 548
250 cn: Account Operators
251 description: Netbios Domain Users to manipulate users accounts
252 sambaSID: $SID-548
253 sambaGroupType: 2
254 displayName: Account Operators
255
256 dn: cn=Server Operators,$groupsdn
257 objectClass: posixGroup
258 objectClass: sambaGroupMapping
259 gidNumber: 549
260 cn: Server Operators
261 description: Netbios Domain Server Operators
262 sambaSID: $SID-549
263 sambaGroupType: 2
264 displayName: Server Operators
265
266 dn: cn=Print Operators,$groupsdn
267 objectClass: posixGroup
268 objectClass: sambaGroupMapping
269 gidNumber: 550
270 cn: Print Operators
271 description: Netbios Domain Print Operators
272 sambaSID: $SID-550
273 sambaGroupType: 2
274 displayName: Print Operators
275
276 dn: cn=Backup Operators,$groupsdn
277 objectClass: posixGroup
278 objectClass: sambaGroupMapping
279 gidNumber: 551
280 cn: Backup Operators
281 description: Netbios Domain Members can bypass file security to back up files
282 sambaSID: $SID-551
283 sambaGroupType: 2
284 displayName: Backup Operators
285
286 dn: cn=Replicator,$groupsdn
287 objectClass: posixGroup
288 objectClass: sambaGroupMapping
289 gidNumber: 552
290 cn: Replicator
291 description: Netbios Domain Supports file replication in a sambaDomainName
292 sambaSID: $SID-552
293 sambaGroupType: 2
294 displayName: Replicator
295
296 dn: cn=Domain Computers,$groupsdn
297 objectClass: posixGroup
298 objectClass: sambaGroupMapping
299 gidNumber: 553
300 cn: Domain Computers
301 description: Netbios Domain Computers accounts
302 sambaSID: $SID-553
303 sambaGroupType: 2
304 displayName: Domain Computers
305
306 EOF
307   close FILE;
308 } else {
309   $tmp_ldif_file=$_ldifName;
310 }
311
312 my $ldap_master=connect_ldap_master();
313 my $ldif = Net::LDAP::LDIF->new($tmp_ldif_file, "r", onerror => 'undef' );
314 while( not $ldif->eof() ) {
315         my $entry = $ldif->read_entry();
316         if ( $ldif->error() ) {
317                 print "Error msg: ",$ldif->error(),"\n";
318                 print "Error lines:\n",$ldif->error_lines(),"\n";
319         } else {
320                 my $dn = $entry->dn;
321                 print "adding new entry: $dn\n";
322                 my $result=$ldap_master->add($entry);
323                 $result->code && warn "failed to add entry: ", $result->error ;
324         }
325 }
326 $ldap_master->unbind;
327 system "rm -f $tmp_ldif_file";
328 exit(0);
329
330
331 ########################################
332
333 =head1 NAME
334
335 smbldap-populate.pl - Populate your LDAP database
336
337 =head1 SYNOPSIS
338
339   smbldap-populate.pl [ldif-file]
340
341 =head1 DESCRIPTION
342
343        The smbldap-populate.pl command helps to populate an LDAP server
344        by adding the necessary entries : base suffix (doesn't abort
345        if already there), organizational units for users, groups and
346        computers, builtin users : Administrator and guest, builtin
347        groups (though posixAccount only, no SambaTNG support).
348
349        -a name  Your local administrator login name (default: Administrator)
350        -b name  Your local guest login name (default: nobody)
351
352        If you give an extra parameter, it is assumed to be the ldif
353        file to use instead of the builtin one. Options -a and -b
354        will be ignored. 
355
356 =head1 FILES
357
358        /usr/lib/perl5/site-perl/smbldap_conf.pm : Global parameters.
359
360 =head1 SEE ALSO
361
362        smp(1)
363
364 =cut
365
366 #'
367
368
369
370 # - The End