~ubuntu-branches/ubuntu/lucid/samba/lucid-proposed

« back to all changes in this revision

Viewing changes to examples/LDAP/smbldap-tools-0.8.7/smbldap-populate

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-07-21 17:53:23 UTC
  • mfrom: (0.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050721175323-m3oh6aoigywohfnq
Tags: 3.0.14a-6ubuntu1
Resynchronise with Debian, resolving merge conflicts (#12360)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
# Populate a LDAP base for Samba-LDAP usage
 
4
#
 
5
# $Id: smbldap-populate,v 1.21 2005/02/13 14:10:39 jtournier 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 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:e:i:k:l:u:g:?', \%Options);
 
52
if ( (!$ok) || ($Options{'?'}) ) {
 
53
  print_banner;
 
54
  print "Usage: $0 [-abeiklug?] [ldif]\n";
 
55
  print "  -u uidNumber first uidNumber to allocate (default: 1000)\n";
 
56
  print "  -g gidNumber first uidNumber to allocate (default: 1000)\n";
 
57
  print "  -a user      administrator login name (default: Administrator)\n";
 
58
  print "  -b user      guest login name (default: nobody)\n";
 
59
  print "  -k uidNumber administrator's uidNumber (default: 998)\n";
 
60
  print "  -l uidNumber guest's uidNumber (default: 999)\n";
 
61
  print "  -e file      export ldif file\n";
 
62
  print "  -i file      import ldif file\n";
 
63
  print "  -?           show this help message\n";
 
64
  exit (1);
 
65
}
 
66
 
 
67
sub read_workgroup_from_sambaconf
 
68
  {
 
69
    my %conf;
 
70
    my $smbconf="/etc/samba/smb.conf";
 
71
    open (CONFIGFILE, "$smbconf") || die "Unable to open $smbconf for reading !\n";
 
72
    while (<CONFIGFILE>) {
 
73
      chomp($_);
 
74
      ## throw away comments
 
75
      next if ( ! /workgroup/i );
 
76
      ## check for a param = value
 
77
      my ($parameter,$value)=read_parameter($_);
 
78
      $value = &subst_configvar($value, \%conf);
 
79
      $conf{$parameter}=$value;
 
80
    }
 
81
    close (CONFIGFILE);
 
82
    return(%conf);
 
83
  }
 
84
 
 
85
my $sambaDomainName;
 
86
if ($config{sambaUnixIdPooldn} and $config{sambaUnixIdPooldn} =~ /^sambaDomainName=([^,]*),(.*)/) {
 
87
  $sambaDomainName=$1;
 
88
  print "Using workgroup name from sambaUnixIdPooldn (smbldap.conf): sambaDomainName=$sambaDomainName\n";
 
89
} else {
 
90
  my %conf_smbconf=read_workgroup_from_sambaconf();
 
91
  $sambaDomainName=$conf_smbconf{workgroup};
 
92
  print "Using workgroup name from smb.conf: sambaDomainName=$sambaDomainName\n";
 
93
  if ($config{sambaUnixIdPooldn} ne "sambaDomainName=$sambaDomainName,$config{suffix}") {
 
94
    print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
 
95
    print "=> Warning: you must update smbldap.conf configuration file to :\n";
 
96
    print "=> sambaUnixIdPooldn parameter must be set to \"sambaDomainName=$sambaDomainName,$config{suffix}\"\n";
 
97
    print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
 
98
    $config{sambaUnixIdPooldn}="sambaDomainName=$sambaDomainName,$config{suffix}";
 
99
  }
 
100
}
 
101
 
 
102
my $firstuidNumber=$Options{'u'};
 
103
if (!defined($firstuidNumber)) {
 
104
  $firstuidNumber=1000;
 
105
}
 
106
 
 
107
my $firstgidNumber=$Options{'g'};
 
108
if (!defined($firstgidNumber)) {
 
109
  $firstgidNumber=1000;
 
110
}
 
111
 
 
112
my $tmp_ldif_file=$Options{'e'};
 
113
if (!defined($tmp_ldif_file)) {
 
114
  $tmp_ldif_file="/tmp/$$.ldif";
 
115
}
 
116
 
 
117
my $adminName = $Options{'a'};
 
118
if (!defined($adminName)) {
 
119
  $adminName = "Administrator";
 
120
}
 
121
 
 
122
my $guestName = $Options{'b'};
 
123
if (!defined($guestName)) {
 
124
  $guestName = "nobody";
 
125
}
 
126
 
 
127
my $adminUidNumber=$Options{'k'};
 
128
if (!defined($adminUidNumber)) {
 
129
  $adminUidNumber = "998";
 
130
}
 
131
 
 
132
my $guestUidNumber=$Options{'l'};
 
133
if (!defined($guestUidNumber)) {
 
134
  $guestUidNumber = "999";
 
135
}
 
136
 
 
137
my $_ldifName = $Options{'i'};
 
138
 
 
139
my $exportFile = $Options{'e'};
 
140
if (!defined($exportFile)) {
 
141
  $exportFile = "base.ldif";
 
142
}
 
143
 
 
144
if (!defined($_ldifName)) {
 
145
  my $attr;
 
146
  my $val;
 
147
  my $objcl;
 
148
 
 
149
  print "Using builtin directory structure\n";
 
150
  if ($config{suffix} =~ m/([^=]+)=([^,]+)/) {
 
151
    $attr = $1;
 
152
    $val = $2;
 
153
    $objcl = $oc{$attr} if (exists $oc{$attr});
 
154
    if (!defined($objcl)) {
 
155
      $objcl = "myhardcodedobjectclass";
 
156
    }
 
157
  } else {
 
158
    die "can't extract first attr and value from suffix $config{suffix}";
 
159
  }
 
160
  #print "$attr=$val\n";
 
161
  my ($type,$ou_users,$ou_groups,$ou_computers,$ou_idmap,$cnsambaUnixIdPool);
 
162
  ($type,$ou_users)=($config{usersdn}=~/(.*)=(.*),$config{suffix}/);
 
163
  ($type,$ou_groups)=($config{groupsdn}=~/(.*)=(.*),$config{suffix}/);
 
164
  ($type,$ou_computers)=($config{computersdn}=~/(.*)=(.*),$config{suffix}/);
 
165
  ($type,$ou_idmap)=($config{idmapdn}=~/(.*)=(.*),$config{suffix}/);
 
166
  ($type,$cnsambaUnixIdPool)=($config{sambaUnixIdPooldn}=~/(.*)=(.*),$config{suffix}/);
 
167
  my $org;
 
168
  my ($organisation,$ext) = ($config{suffix} =~ m/dc=(.*),dc=(.*)$/);
 
169
  if ($organisation ne '') {
 
170
    $org = "\nobjectclass: organization\no: $organisation";
 
171
  }
 
172
  #my $FILE="|cat";
 
173
  my $entries="dn: $config{suffix}
 
174
objectClass: $objcl$org
 
175
$attr: $val
 
176
 
 
177
dn: $config{usersdn}
 
178
objectClass: organizationalUnit
 
179
ou: $ou_users
 
180
 
 
181
dn: $config{groupsdn}
 
182
objectClass: organizationalUnit
 
183
ou: $ou_groups
 
184
 
 
185
dn: $config{computersdn}
 
186
objectClass: organizationalUnit
 
187
ou: $ou_computers
 
188
 
 
189
dn: $config{idmapdn}
 
190
objectClass: organizationalUnit
 
191
ou: $ou_idmap
 
192
 
 
193
dn: $config{sambaUnixIdPooldn}
 
194
objectClass: sambaDomain
 
195
objectClass: sambaUnixIdPool
 
196
sambaDomainName: $sambaDomainName
 
197
sambaSID: $config{SID}
 
198
uidNumber: $firstuidNumber
 
199
gidNumber: $firstgidNumber
 
200
 
 
201
dn: uid=$adminName,$config{usersdn}
 
202
cn: $adminName
 
203
sn: $adminName
 
204
objectClass: inetOrgPerson
 
205
objectClass: sambaSAMAccount
 
206
objectClass: posixAccount
 
207
objectClass: shadowAccount
 
208
gidNumber: 512
 
209
uid: $adminName
 
210
uidNumber: $adminUidNumber\n";
 
211
  if (defined $config{userHome} and $config{userHome} ne "") {
 
212
    my $userHome=$config{userHome};
 
213
    $userHome=~s/\%U/$adminName/;
 
214
    $entries.="homeDirectory: $userHome\n";
 
215
  } else {
 
216
    $entries.="homeDirectory: /dev/null\n";
 
217
  }
 
218
  $entries.="sambaPwdLastSet: 0
 
219
sambaLogonTime: 0
 
220
sambaLogoffTime: 2147483647
 
221
sambaKickoffTime: 2147483647
 
222
sambaPwdCanChange: 0
 
223
sambaPwdMustChange: 2147483647\n";
 
224
  if (defined $config{userSmbHome} and $config{userSmbHome} ne "") {
 
225
    my $userSmbHome=$config{userSmbHome};
 
226
    $userSmbHome=~s/\%U/$adminName/;
 
227
    $entries.="sambaHomePath: $userSmbHome\n";
 
228
  }
 
229
  if (defined $config{userHomeDrive} and $config{userHomeDrive} ne "") {
 
230
    $entries.="sambaHomeDrive: $config{userHomeDrive}\n";
 
231
  }
 
232
  if (defined $config{userProfile} and $config{userProfile} ne "") {
 
233
    my $userProfile=$config{userProfile};
 
234
    $userProfile=~s/\%U/$adminName/;
 
235
    $entries.="sambaProfilePath: $userProfile\\\n";     
 
236
  }
 
237
  $entries.="sambaPrimaryGroupSID: $config{SID}-512
 
238
sambaLMPassword: XXX
 
239
sambaNTPassword: XXX
 
240
sambaAcctFlags: [U          ]
 
241
sambaSID: $config{SID}-2996
 
242
loginShell: /bin/false
 
243
gecos: Netbios Domain Administrator
 
244
 
 
245
dn: uid=$guestName,$config{usersdn}
 
246
cn: $guestName
 
247
sn: $guestName
 
248
objectClass: inetOrgPerson
 
249
objectClass: sambaSAMAccount
 
250
objectClass: posixAccount
 
251
objectClass: shadowAccount
 
252
gidNumber: 514
 
253
uid: $guestName
 
254
uidNumber: $guestUidNumber
 
255
homeDirectory: /dev/null
 
256
sambaPwdLastSet: 0
 
257
sambaLogonTime: 0
 
258
sambaLogoffTime: 2147483647
 
259
sambaKickoffTime: 2147483647
 
260
sambaPwdCanChange: 0
 
261
sambaPwdMustChange: 2147483647\n";
 
262
  if (defined $config{userSmbHome} and $config{userSmbHome} ne "") {
 
263
    my $userSmbHome=$config{userSmbHome};
 
264
    $userSmbHome=~s/\%U/$guestName/;
 
265
    $entries.="sambaHomePath: $userSmbHome\n";
 
266
  }
 
267
  if (defined $config{userHomeDrive} and $config{userHomeDrive} ne "") {
 
268
    $entries.="sambaHomeDrive: $config{userHomeDrive}\n";
 
269
  }
 
270
  if (defined $config{userProfile} and $config{userProfile} ne "") {
 
271
    my $userProfile=$config{userProfile};
 
272
    $userProfile=~s/\%U/$guestName/;
 
273
    $entries.="sambaProfilePath: $userProfile\n";
 
274
  }
 
275
  $entries.="sambaPrimaryGroupSID: $config{SID}-514
 
276
sambaLMPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
 
277
sambaNTPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
 
278
sambaAcctFlags: [NU         ]
 
279
sambaSID: $config{SID}-2998
 
280
loginShell: /bin/false
 
281
 
 
282
dn: cn=Domain Admins,$config{groupsdn}
 
283
objectClass: posixGroup
 
284
objectClass: sambaGroupMapping
 
285
gidNumber: 512
 
286
cn: Domain Admins
 
287
memberUid: $adminName
 
288
description: Netbios Domain Administrators
 
289
sambaSID: $config{SID}-512
 
290
sambaGroupType: 2
 
291
displayName: Domain Admins
 
292
 
 
293
dn: cn=Domain Users,$config{groupsdn}
 
294
objectClass: posixGroup
 
295
objectClass: sambaGroupMapping
 
296
gidNumber: 513
 
297
cn: Domain Users
 
298
description: Netbios Domain Users
 
299
sambaSID: $config{SID}-513
 
300
sambaGroupType: 2
 
301
displayName: Domain Users
 
302
 
 
303
dn: cn=Domain Guests,$config{groupsdn}
 
304
objectClass: posixGroup
 
305
objectClass: sambaGroupMapping
 
306
gidNumber: 514
 
307
cn: Domain Guests
 
308
description: Netbios Domain Guests Users
 
309
sambaSID: $config{SID}-514
 
310
sambaGroupType: 2
 
311
displayName: Domain Guests
 
312
 
 
313
dn: cn=Domain Computers,$config{groupsdn}
 
314
objectClass: posixGroup
 
315
objectClass: sambaGroupMapping
 
316
gidNumber: 515
 
317
cn: Domain Computers
 
318
description: Netbios Domain Computers accounts
 
319
sambaSID: $config{SID}-515
 
320
sambaGroupType: 2
 
321
displayName: Domain Computers
 
322
 
 
323
dn: cn=Administrators,$config{groupsdn}
 
324
objectClass: posixGroup
 
325
objectClass: sambaGroupMapping
 
326
gidNumber: 544
 
327
cn: Administrators
 
328
description: Netbios Domain Members can fully administer the computer/sambaDomainName
 
329
sambaSID: S-1-5-32-544
 
330
sambaGroupType: 5
 
331
displayName: Administrators
 
332
 
 
333
#dn: cn=Users,$config{groupsdn}
 
334
#objectClass: posixGroup
 
335
#objectClass: sambaGroupMapping
 
336
#gidNumber: 545
 
337
#cn: Users
 
338
#description: Netbios Domain Ordinary users
 
339
#sambaSID: S-1-5-32-545
 
340
#sambaGroupType: 5
 
341
#displayName: users
 
342
 
 
343
#dn: cn=Guests,$config{groupsdn}
 
344
#objectClass: posixGroup
 
345
#objectClass: sambaGroupMapping
 
346
#gidNumber: 546
 
347
#cn: Guests
 
348
#memberUid: $guestName
 
349
#description: Netbios Domain Users granted guest access to the computer/sambaDomainName
 
350
#sambaSID: S-1-5-32-546
 
351
#sambaGroupType: 5
 
352
#displayName: Guests
 
353
 
 
354
#dn: cn=Power Users,$config{groupsdn}
 
355
#objectClass: posixGroup
 
356
#objectClass: sambaGroupMapping
 
357
#gidNumber: 547
 
358
#cn: Power Users
 
359
#description: Netbios Domain Members can share directories and printers
 
360
#sambaSID: S-1-5-32-547
 
361
#sambaGroupType: 5
 
362
#displayName: Power Users
 
363
 
 
364
#dn: cn=Account Operators,$config{groupsdn}
 
365
#objectClass: posixGroup
 
366
#objectClass: sambaGroupMapping
 
367
#gidNumber: 548
 
368
#cn: Account Operators
 
369
#description: Netbios Domain Users to manipulate users accounts
 
370
#sambaSID: S-1-5-32-548
 
371
#sambaGroupType: 5
 
372
#displayName: Account Operators
 
373
 
 
374
#dn: cn=System Operators,$config{groupsdn}
 
375
#objectClass: posixGroup
 
376
#objectClass: sambaGroupMapping
 
377
#gidNumber: 549
 
378
#cn: System Operators
 
379
#description: Netbios Domain System Operators
 
380
#sambaSID: S-1-5-32-549
 
381
#sambaGroupType: 5
 
382
#displayName: System Operators
 
383
 
 
384
dn: cn=Print Operators,$config{groupsdn}
 
385
objectClass: posixGroup
 
386
objectClass: sambaGroupMapping
 
387
gidNumber: 550
 
388
cn: Print Operators
 
389
description: Netbios Domain Print Operators
 
390
sambaSID: S-1-5-32-550
 
391
sambaGroupType: 5
 
392
displayName: Print Operators
 
393
 
 
394
dn: cn=Backup Operators,$config{groupsdn}
 
395
objectClass: posixGroup
 
396
objectClass: sambaGroupMapping
 
397
gidNumber: 551
 
398
cn: Backup Operators
 
399
description: Netbios Domain Members can bypass file security to back up files
 
400
sambaSID: S-1-5-32-551
 
401
sambaGroupType: 5
 
402
displayName: Backup Operators
 
403
 
 
404
dn: cn=Replicators,$config{groupsdn}
 
405
objectClass: posixGroup
 
406
objectClass: sambaGroupMapping
 
407
gidNumber: 552
 
408
cn: Replicators
 
409
description: Netbios Domain Supports file replication in a sambaDomainName
 
410
sambaSID: S-1-5-32-552
 
411
sambaGroupType: 5
 
412
displayName: Replicators
 
413
";
 
414
  open (FILE, ">$tmp_ldif_file") || die "Can't open file $tmp_ldif_file: $!\n";
 
415
 
 
416
  print FILE <<EOF;
 
417
$entries
 
418
EOF
 
419
  close FILE;
 
420
} else {
 
421
  $tmp_ldif_file=$_ldifName;
 
422
}
 
423
 
 
424
if (!defined $Options{'e'}) {
 
425
  my $ldap_master=connect_ldap_master();
 
426
  my $ldif = Net::LDAP::LDIF->new($tmp_ldif_file, "r", onerror => 'undef' );
 
427
  while ( not $ldif->eof() ) {
 
428
    my $entry = $ldif->read_entry();
 
429
    if ( $ldif->error() ) {
 
430
      print "Error msg: ",$ldif->error(),"\n";
 
431
      print "Error lines:\n",$ldif->error_lines(),"\n";
 
432
    } else {
 
433
      my $dn = $entry->dn;
 
434
      # we first check if the entry exist
 
435
      my $mesg = $ldap_master->search (
 
436
                                       base => "$dn",
 
437
                                       scope => "base",
 
438
                                       filter => "objectclass=*"
 
439
                                      );
 
440
      $mesg->code;
 
441
      my $nb=$mesg->count;
 
442
      if ($nb == 1 ) {
 
443
        print "entry $dn already exist. ";
 
444
        if ($dn eq $config{sambaUnixIdPooldn}) {
 
445
          print "Updating it...\n";
 
446
          my @mods;
 
447
          foreach my $attr_tmp ($entry->attributes) {
 
448
            push(@mods,$attr_tmp=>[$entry->get_value("$attr_tmp")]);
 
449
          }
 
450
          my $modify = $ldap_master->modify ( "$dn",
 
451
                                              'replace' => { @mods },
 
452
                                            );
 
453
          $modify->code && warn "failed to modify entry: ", $modify->error ;
 
454
        } else {
 
455
          print "\n";
 
456
        }
 
457
      } else {
 
458
        print "adding new entry: $dn\n";
 
459
        my $result=$ldap_master->add($entry);
 
460
        $result->code && warn "failed to add entry: ", $result->error ;
 
461
      }
 
462
    }
 
463
  }
 
464
  $ldap_master->unbind;
 
465
  if (!defined $Options{'i'}) {
 
466
    system "rm -f $tmp_ldif_file";
 
467
  }
 
468
} else {
 
469
  print "exported ldif file: $tmp_ldif_file\n";
 
470
}
 
471
exit(0);
 
472
 
 
473
 
 
474
########################################
 
475
 
 
476
=head1 NAME
 
477
 
 
478
smbldap-populate - Populate your LDAP database
 
479
 
 
480
=head1 SYNOPSIS
 
481
 
 
482
smbldap-populate [ldif-file]
 
483
 
 
484
=head1 DESCRIPTION
 
485
 
 
486
The smbldap-populate command helps to populate an LDAP server by adding the necessary entries : base suffix (doesn't abort if already there), organizational units for users, groups and computers, builtin users : Administrator and guest, builtin groups (though posixAccount only, no SambaTNG support).
 
487
 
 
488
-a name
 
489
 Your local administrator login name (default: Administrator)
 
490
 
 
491
-b name
 
492
 Your local guest login name (default: nobody)
 
493
 
 
494
-e file
 
495
 export an ldif file
 
496
 
 
497
-i file
 
498
 import an ldif file (Options -a and -b will be ignored)
 
499
 
 
500
=head1 FILES
 
501
 
 
502
       /usr/lib/perl5/site-perl/smbldap_conf.pm : Global parameters.
 
503
 
 
504
=head1 SEE ALSO
 
505
 
 
506
       smp(1)
 
507
 
 
508
=cut
 
509
 
 
510
#'
 
511
 
 
512
 
 
513
 
 
514
# - The End