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