1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
#!/usr/bin/perl -w
# This code was developped by Jerome Tournier (jtournier@gmail.com) and
# contributors (their names can be found in the CONTRIBUTORS file).
# This was first contributed by IDEALX (http://www.opentrust.com/)
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
# Purpose :
# . Create an initial LDAP database suitable for Samba 2.2
# . For lazy people, replace ldapadd (with only an ldif parameter)
use strict;
use FindBin;
use FindBin qw($RealBin);
use lib "$RealBin/";
use smbldap_tools;
use Getopt::Std;
use Net::LDAP::LDIF;
use vars qw(%oc);
# objectclass of the suffix
%oc = (
"ou" => "organizationalUnit",
"o" => "organization",
"dc" => "dcObject",
);
my %Options;
my $ok = getopts('a:b:e:i:k:l:m:u:g:r:?', \%Options);
if ( (!$ok) || ($Options{'?'}) ) {
print_banner;
print "Usage: $0 [-abeiklug?] [ldif]\n";
print " -u uidNumber first uidNumber to allocate (default: 1000)\n";
print " -g gidNumber first uidNumber to allocate (default: 1000)\n";
print " -r ridNumber first sambaNextRid to allocate (default: 1000)\n";
print " -a user administrator login name (default: root)\n";
print " -b user guest login name (default: nobody)\n";
print " -k uidNumber administrator's uidNumber (default: 0)\n";
print " -l uidNumber guest's uidNumber (default: 999)\n";
print " -m gidNumber administrator's gidNumber (default: 0)\n";
print " -e file export ldif file\n";
print " -i file import ldif file\n";
print " -? show this help message\n";
exit (1);
}
# sanity checks
my $domain = $config{sambaDomain};
if (! defined $domain) {
print STDERR "error: domain name not found !\n";
print STDERR "possible reasons are:\n";
print STDERR ". incorrect 'sambaDomain' parameter in smbldap.conf\n";
print STDERR ". incorrect 'samba_conf' definition in smbldap_tools.pm\n";
die;
}
#$config{sambaUnixIdPooldn}="sambaDomainName=$domain,$config{suffix}";
my $firstuidNumber=$Options{'u'};
if (!defined($firstuidNumber)) {
$firstuidNumber=1000;
}
my $firstgidNumber=$Options{'g'};
if (!defined($firstgidNumber)) {
$firstgidNumber=1000;
}
my $firstridNumber=$Options{'r'};
if (!defined($firstridNumber)) {
$firstridNumber=1000;
}
my $tmp_ldif_file=$Options{'e'};
if (!defined($tmp_ldif_file)) {
$tmp_ldif_file="/tmp/$$.ldif";
}
my $adminName = $Options{'a'};
if (!defined($adminName)) {
$adminName = "root";
}
my $guestName = $Options{'b'};
if (!defined($guestName)) {
$guestName = "nobody";
}
my $adminUidNumber=$Options{'k'};
my $adminrid;
if (!defined($adminUidNumber)) {
$adminUidNumber = "0";
$adminrid= "500";
} else {
$adminrid=(2*$adminUidNumber+ 1000)
}
my $guestUidNumber=$Options{'l'};
if (!defined($guestUidNumber)) {
$guestUidNumber = "999";
}
my $adminGidNumber=$Options{'m'};
if (!defined($adminGidNumber)) {
$adminGidNumber = "0";
}
my $_ldifName = $Options{'i'};
my $exportFile = $Options{'e'};
if (!defined($exportFile)) {
$exportFile = "base.ldif";
}
print "Populating LDAP directory for domain $domain ($config{SID})\n";
if (!defined($_ldifName)) {
my $attr;
my $val;
my $objcl;
print "(using builtin directory structure)\n\n";
if ($config{suffix} =~ m/([^=]+)=([^,]+)/) {
$attr = $1;
$val = $2;
$objcl = $oc{$attr} if (exists $oc{$attr});
if (!defined($objcl)) {
$objcl = "myhardcodedobjectclass";
}
} else {
die "can't extract first attr and value from suffix $config{suffix}";
}
#print "$attr=$val\n";
my ($type,$ou_users,$ou_groups,$ou_computers,$ou_idmap,$cnsambaUnixIdPool);
($type,$ou_users)=($config{usersdn}=~/(.*)=(.*),$config{suffix}/);
($type,$ou_groups)=($config{groupsdn}=~/(.*)=(.*),$config{suffix}/);
($type,$ou_computers)=($config{computersdn}=~/(.*)=(.*),$config{suffix}/);
if (defined $config{idmapdn}) {
($type,$ou_idmap)=($config{idmapdn}=~/(.*)=(.*),$config{suffix}/);
}
($type,$cnsambaUnixIdPool)=($config{sambaUnixIdPooldn}=~/(.*)=(.*),$config{suffix}/);
my $org;
my ($organisation,$ext);
if ($config{suffix} =~ m/dc=([^=]+),dc=(.*)$/) {
($organisation,$ext) = ($config{suffix} =~ m/dc=([^=]+),dc=(.*)$/);
} elsif ($config{suffix} =~ m/dc=(.*)$/) {
$organisation=$1;
}
if ($organisation ne '') {
$org = "\nobjectclass: organization\no: $organisation";
}
#my $FILE="|cat";
my $entries="dn: $config{suffix}
objectClass: $objcl$org
$attr: $val
dn: $config{usersdn}
objectClass: top
objectClass: organizationalUnit
ou: $ou_users
dn: $config{groupsdn}
objectClass: top
objectClass: organizationalUnit
ou: $ou_groups
dn: $config{computersdn}
objectClass: top
objectClass: organizationalUnit
ou: $ou_computers\n";
if (defined $config{idmapdn}) {
$entries.="\ndn: $config{idmapdn}
objectClass: top
objectClass: organizationalUnit
ou: $ou_idmap\n";
}
$entries.="\ndn: uid=$adminName,$config{usersdn}
cn: $adminName
sn: $adminName
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: sambaSAMAccount
objectClass: posixAccount
objectClass: shadowAccount
gidNumber: $adminGidNumber
uid: $adminName
uidNumber: $adminUidNumber\n";
if (defined $config{userHome} and $config{userHome} ne "") {
my $userHome=$config{userHome};
$userHome=~s/\%U/$adminName/;
$entries.="homeDirectory: $userHome\n";
} else {
$entries.="homeDirectory: /dev/null\n";
}
$entries.="sambaPwdLastSet: 0
sambaLogonTime: 0
sambaLogoffTime: 2147483647
sambaKickoffTime: 2147483647
sambaPwdCanChange: 0
sambaPwdMustChange: 2147483647\n";
if (defined $config{userSmbHome} and $config{userSmbHome} ne "") {
my $userSmbHome=$config{userSmbHome};
$userSmbHome=~s/\%U/$adminName/;
$entries.="sambaHomePath: $userSmbHome\n";
}
if (defined $config{userHomeDrive} and $config{userHomeDrive} ne "") {
$entries.="sambaHomeDrive: $config{userHomeDrive}\n";
}
if (defined $config{userProfile} and $config{userProfile} ne "") {
my $userProfile=$config{userProfile};
$userProfile=~s/\%U/$adminName/;
$entries.="sambaProfilePath: $userProfile\n";
}
$entries.="sambaPrimaryGroupSID: $config{SID}-512
sambaLMPassword: XXX
sambaNTPassword: XXX
sambaAcctFlags: [U ]
sambaSID: $config{SID}-$adminrid
loginShell: /bin/false
gecos: Netbios Domain Administrator
dn: uid=$guestName,$config{usersdn}
cn: $guestName
sn: $guestName
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: sambaSAMAccount
objectClass: posixAccount
objectClass: shadowAccount
gidNumber: 514
uid: $guestName
uidNumber: $guestUidNumber
homeDirectory: /dev/null
sambaPwdLastSet: 0
sambaLogonTime: 0
sambaLogoffTime: 2147483647
sambaKickoffTime: 2147483647
sambaPwdCanChange: 0
sambaPwdMustChange: 2147483647\n";
if (defined $config{userSmbHome} and $config{userSmbHome} ne "") {
my $userSmbHome=$config{userSmbHome};
$userSmbHome=~s/\%U/$guestName/;
$entries.="sambaHomePath: $userSmbHome\n";
}
if (defined $config{userHomeDrive} and $config{userHomeDrive} ne "") {
$entries.="sambaHomeDrive: $config{userHomeDrive}\n";
}
if (defined $config{userProfile} and $config{userProfile} ne "") {
my $userProfile=$config{userProfile};
$userProfile=~s/\%U/$guestName/;
$entries.="sambaProfilePath: $userProfile\n";
}
$entries.="sambaPrimaryGroupSID: $config{SID}-514
sambaLMPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
sambaNTPassword: NO PASSWORDXXXXXXXXXXXXXXXXXXXXX
# account disabled by default
sambaAcctFlags: [NUD ]
sambaSID: $config{SID}-2998
loginShell: /bin/false
dn: cn=Domain Admins,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 512
cn: Domain Admins
memberUid: $adminName
description: Netbios Domain Administrators
sambaSID: $config{SID}-512
sambaGroupType: 2
displayName: Domain Admins
dn: cn=Domain Users,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 513
cn: Domain Users
description: Netbios Domain Users
sambaSID: $config{SID}-513
sambaGroupType: 2
displayName: Domain Users
dn: cn=Domain Guests,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 514
cn: Domain Guests
description: Netbios Domain Guests Users
sambaSID: $config{SID}-514
sambaGroupType: 2
displayName: Domain Guests
dn: cn=Domain Computers,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 515
cn: Domain Computers
description: Netbios Domain Computers accounts
sambaSID: $config{SID}-515
sambaGroupType: 2
displayName: Domain Computers
dn: cn=Administrators,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 544
cn: Administrators
description: Netbios Domain Members can fully administer the computer/sambaDomainName
sambaSID: S-1-5-32-544
sambaGroupType: 5
displayName: Administrators
#dn: cn=Users,$config{groupsdn}
#objectClass: top
#objectClass: posixGroup
#objectClass: sambaGroupMapping
#gidNumber: 545
#cn: Users
#description: Netbios Domain Ordinary users
#sambaSID: S-1-5-32-545
#sambaGroupType: 5
#displayName: users
#dn: cn=Guests,$config{groupsdn}
#objectClass: top
#objectClass: posixGroup
#objectClass: sambaGroupMapping
#gidNumber: 546
#cn: Guests
#memberUid: $guestName
#description: Netbios Domain Users granted guest access to the computer/sambaDomainName
#sambaSID: S-1-5-32-546
#sambaGroupType: 5
#displayName: Guests
#dn: cn=Power Users,$config{groupsdn}
#objectClass: top
#objectClass: posixGroup
#objectClass: sambaGroupMapping
#gidNumber: 547
#cn: Power Users
#description: Netbios Domain Members can share directories and printers
#sambaSID: S-1-5-32-547
#sambaGroupType: 5
#displayName: Power Users
dn: cn=Account Operators,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 548
cn: Account Operators
description: Netbios Domain Users to manipulate users accounts
sambaSID: S-1-5-32-548
sambaGroupType: 5
displayName: Account Operators
#dn: cn=System Operators,$config{groupsdn}
#objectClass: top
#objectClass: posixGroup
#objectClass: sambaGroupMapping
#gidNumber: 549
#cn: System Operators
#description: Netbios Domain System Operators
#sambaSID: S-1-5-32-549
#sambaGroupType: 5
#displayName: System Operators
dn: cn=Print Operators,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 550
cn: Print Operators
description: Netbios Domain Print Operators
sambaSID: S-1-5-32-550
sambaGroupType: 5
displayName: Print Operators
dn: cn=Backup Operators,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 551
cn: Backup Operators
description: Netbios Domain Members can bypass file security to back up files
sambaSID: S-1-5-32-551
sambaGroupType: 5
displayName: Backup Operators
dn: cn=Replicators,$config{groupsdn}
objectClass: top
objectClass: posixGroup
objectClass: sambaGroupMapping
gidNumber: 552
cn: Replicators
description: Netbios Domain Supports file replication in a sambaDomainName
sambaSID: S-1-5-32-552
sambaGroupType: 5
displayName: Replicators
";
if ("sambaDomainName=$domain,$config{suffix}" eq $config{sambaUnixIdPooldn}) {
$entries.="dn: sambaDomainName=$domain,$config{suffix}
objectClass: top
objectClass: sambaDomain
objectClass: sambaUnixIdPool
sambaDomainName: $domain
sambaSID: $config{SID}
uidNumber: $firstuidNumber
gidNumber: $firstgidNumber
sambaNextRid: $firstridNumber";
} else {
$entries.="dn: $config{sambaUnixIdPooldn}
objectClass: inetOrgPerson
objectClass: sambaUnixIdPool
uidNumber: $firstuidNumber
gidNumber: $firstgidNumber
sambaNextRid: $firstridNumber
cn: $cnsambaUnixIdPool
sn: $cnsambaUnixIdPool";
}
open (FILE, ">$tmp_ldif_file") || die "Can't open file $tmp_ldif_file: $!\n";
print FILE <<EOF;
$entries
EOF
close FILE;
} else {
$tmp_ldif_file=$_ldifName;
}
if (!defined $Options{'e'}) {
my $ldap_master=connect_ldap_master();
my $ldif = Net::LDAP::LDIF->new($tmp_ldif_file, "r", 'undef' );
while ( not $ldif->eof() ) {
my $entry = $ldif->read_entry();
if ( $ldif->error() ) {
print "Error msg: ",$ldif->error(),"\n";
print "Error lines:\n",$ldif->error_lines(),"\n";
} else {
my $dn = $entry->dn;
# we first check if the entry exist
my $mesg = $ldap_master->search (
base => "$dn",
scope => "base",
filter => "objectclass=*"
);
$mesg->code;
my $nb=$mesg->count;
if ($nb == 1 ) {
print "entry $dn already exist. ";
if ($dn eq $config{sambaUnixIdPooldn}) {
print "Updating it...\n";
my @mods;
foreach my $attr_tmp ($entry->attributes) {
push(@mods,$attr_tmp=>[$entry->get_value("$attr_tmp")]);
}
my $modify = $ldap_master->modify ( "$dn",
'replace' => { @mods },
);
$modify->code && warn "failed to modify entry: ", $modify->error ;
} else {
print "\n";
}
} else {
print "adding new entry: $dn\n";
my $result=$ldap_master->add($entry);
$result->code && warn "failed to add entry: ", $result->error ;
}
}
}
$ldap_master->unbind;
if (!defined $Options{'i'}) {
system "rm -f $tmp_ldif_file";
}
# secure the admin account
print "\nPlease provide a password for the domain $adminName: \n";
system("$RealBin/smbldap-passwd $adminName");
} else {
print "exported ldif file: $tmp_ldif_file\n";
}
exit(0);
########################################
=head1 NAME
smbldap-populate - Populate your LDAP database
=head1 SYNOPSIS
smbldap-populate [ldif-file]
=head1 DESCRIPTION
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).
-a name
Your local administrator login name (default: Administrator)
-b name
Your local guest login name (default: nobody)
-e file
export an ldif file
-i file
import an ldif file (Options -a and -b will be ignored)
=head1 FILES
/etc/smbldap-tools/smbldap.conf : main configuration
/etc/smbldap-tools/smbldap_bind.conf : credentials for binding to the directory
=head1 SEE ALSO
smb.conf(5)
=cut
#'
# - The End
|