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