/usr/share/perl5/EBox/SambaLdapUser.pm is in zentyal-samba 2.3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 | # Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# 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
package EBox::SambaLdapUser;
use strict;
use warnings;
use EBox::Sudo;
use EBox::Global;
use EBox::Model::ModelManager;
use EBox::Network;
use EBox::Exceptions::InvalidData;
use EBox::Exceptions::Internal;
use EBox::Exceptions::DataExists;
use EBox::Exceptions::DataMissing;
use EBox::Gettext;
use Perl6::Junction qw(any all);
use Error qw(:try);
use Crypt::SmbHash qw(nthash ntlmgen);
# Default values for samba user
use constant SMBLOGONTIME => '0';
use constant SMBLOGOFFTIME => '2147483647';
use constant SMBKICKOFFTIME => '2147483647';
use constant SMBPWDCANCHANGE => '0';
use constant SMBPWDMUSTCHANGE => '2147483647';
use constant SMBGROUP => '513';
use constant SMBACCTFLAGS => '[U]';
use constant SMBACCTFLAGSDISABLED => '[UD]';
use constant USERGROUP => 513;
# Home path for users and groups
use constant BASEPATH => '/home/samba';
use constant USERSPATH => '/home';
use constant GROUPSPATH => BASEPATH . '/groups';
use constant PROFILESPATH => BASEPATH . '/profiles';
BEGIN
{
use Exporter ();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@ISA = qw(Exporter);
@EXPORT = qw{ USERSPATH GROUPSPATH PROFILESPATH };
%EXPORT_TAGS = ( DEFAULT => \@EXPORT );
@EXPORT_OK = qw();
$VERSION = EBox::Config::version;
}
use base qw(EBox::LdapUserBase);
sub new
{
my $class = shift;
my $self = {};
$self->{ldap} = EBox::Global->modInstance('users')->ldap();
$self->{samba} = EBox::Global->modInstance('samba');
bless($self, $class);
return $self;
}
sub _smbHomes
{
my $samba = EBox::Global->modInstance('samba');
return "\\\\" . $samba->netbios() . "\\homes\\";
}
sub _smbProfiles
{
my $samba = EBox::Global->modInstance('samba');
return "\\\\" . $samba->netbios() . "\\profiles\\";
}
# FIXME Implement userInGroup function in UsersAndGroups
sub _domainUser
{
my ($self, $user) = @_;
($user) or return;
my $usermod = EBox::Global->modInstance('users');
my @domainUsers;
@domainUsers = @{$usermod->usersInGroup('Domain Users')};
foreach my $u (@domainUsers) {
return 1 if ($u eq $user);
}
return undef;
}
sub _userCommonLdapAttrs
{
my ($self) = @_;
my $defaultFlags;
my $model =
EBox::Model::ModelManager::instance()->model('samba/SambaUser');
if ($model->enabledValue()) {
$defaultFlags = SMBACCTFLAGS;
} else {
$defaultFlags = SMBACCTFLAGSDISABLED;
}
my $attrs = {
sambaLogonTime => SMBLOGONTIME,
sambaLogoffTime => SMBLOGOFFTIME,
sambaKickoffTime => SMBKICKOFFTIME,
sambaPwdCanChange => SMBPWDCANCHANGE,
sambaPwdMustChange => SMBPWDMUSTCHANGE,
sambaPasswordHistory => '0' x56,
sambaPwdLastSet => time(),
sambaAcctFlags => $defaultFlags,
};
return $attrs;
}
sub _addUserLdapAttrs
{
my ($self, $user, $unixuid, $password) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $dn = "uid=$user," . $users->usersDn;
my $rid = 2 * $unixuid + 1000;
my $sid = alwaysGetSID();
my $sambaSID = $sid . '-' . $rid;
my $userinfo = $users->userInfo($user);
my ($lm ,$nt);
if (not defined($password) or EBox::UsersAndGroups::isHashed($password)) {
$lm = $userinfo->{extra_passwords}->{'lm'};
$nt = $userinfo->{extra_passwords}->{'nt'};
} else {
($lm, $nt) = ntlmgen($password);
}
my %userCommonAttrs = %{ $self->_userCommonLdapAttrs() };
unless ($self->_isSambaObject('sambaSamAccount', $dn)) {
my %attrs = (
changes => [
add => [
objectClass => 'sambaSamAccount',
%userCommonAttrs,
sambaHomePath => _smbHomes() . $user,
sambaPrimaryGroupSID => $sid . '-' . SMBGROUP,
sambaLMPassword => $lm,
sambaNTPassword => $nt,
sambaSID => $sambaSID,
],
]
);
my $add = $ldap->modify($dn, \%attrs );
}
else {
# upgrade from previous versions
# XXX currently only the user common attributes are upgraded
my %searchParams = (base => $dn,
attrs => [keys %userCommonAttrs],
filter => "(objectclass=*)",
scope =>'base');
my $result = $ldap->search(\%searchParams);
my $entry = $result->pop_entry();
defined $entry or
throw EBox::Exceptions::Internal("Cannot retrieve user with DN= $dn");
my $changed = 0;
while (my ($attr, $defaultValue) = each %userCommonAttrs) {
my $value = $entry->get_value($attr);
if (not defined $value) {
$entry->add($attr, $defaultValue);
$changed = 1;
}
}
if ($changed) {
$entry->update($ldap->ldapCon);
}
}
}
# Implements LdapUserBase interface
sub _addUser
{
my ($self, $userName, $password) = @_;
return unless ($self->{samba}->configured());
my $users = EBox::Global->modInstance('users');
my $userInfo = $users->userInfo($userName);
my $unixuid = $userInfo->{uid};
$self->_addUserLdapAttrs($userName, $unixuid, $password);
# Add user to Domain Users group
unless ($self->_domainUser($userName)) {
$users->addUserToGroup($userName, 'Domain Users');
}
$self->_createDir(PROFILESPATH . "/$userName",
$unixuid, USERGROUP, '0700');
$self->_createDir(PROFILESPATH . "/$userName.V2",
$unixuid, USERGROUP, '0700');
}
sub _modifyUser
{
my ($self, $user, $password) = @_;
return unless ($self->{samba}->configured());
my $users = EBox::Global->modInstance('users');
my $userInfo = $users->userInfo($user);
my $dn = "uid=$user," . $users->usersDn;
my $ldap = $self->{ldap};
my $curNT = $userInfo->{extra_passwords}->{nt};
my $curLM = $userInfo->{extra_passwords}->{lm};
my %attrs = (
base => $dn,
filter => "(objectclass=*)",
attrs => [ 'sambaNTPassword', 'sambaLMPassword'],
scope => 'base'
);
my $result = $ldap->search(\%attrs);
my $entry = $result->pop_entry();
$entry->replace( sambaNTPassword => $curNT, sambaLMPassword => $curLM );
$entry->update($ldap->ldapCon);
}
sub _delUser
{
my ($self, $user) = @_;
return unless ($self->{samba}->configured());
my @cmds;
if (-d BASEPATH . "/profiles/$user") {
push (@cmds, "rm -rf \'" . BASEPATH . "/profiles/$user\'");
}
if (-d BASEPATH . "/profiles/$user.V2") {
push (@cmds, "rm -rf \'" . BASEPATH . "/profiles/$user.V2\'");
}
EBox::Sudo::root(@cmds) if (@cmds);
# Remove user from printers
my $samba = EBox::Global->modInstance('samba');
$samba->setPrintersForUser($user, []);
}
sub _delUserWarning
{
my ($self, $user) = @_;
return unless ($self->{samba}->configured());
my $path = BASEPATH . "/users/$user";
my $txt = __('This user has a sharing directory associated ' .
'which contains data.');
unless ($self->_directoryEmpty($path)) {
return ($txt);
}
return undef;
}
sub _addGroup
{
my ($self, $group) = @_;
return unless ($self->{samba}->configured());
$self->addGroupLdapAttrs($group);
}
sub addGroupLdapAttrs
{
my ($self, $group, %params) = @_;
my $sambaSID = delete $params{SID};
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $groupInfo = $users->groupInfo($group);
my $unixgid = $groupInfo->{gid};
my $rid = 2 * $unixgid + 1001;
if (not defined $sambaSID) {
my $baseSid = alwaysGetSID();
$sambaSID = $baseSid . '-' . $rid;
}
my $dn = "cn=$group," . $users->groupsDn;
unless ($self->_isSambaObject('sambaGroupMapping', $dn)) {
my %attrs = (
changes => [
add => [
objectClass => 'sambaGroupMapping',
objectClass => 'eboxGroup',
sambaSID => $sambaSID,
sambaGroupType => '2',
displayName => $group,
]
]
);
$ldap->modify($dn, \%attrs);
}
}
sub setUserSID
{
my ($self, $user, $sid) = @_;
checkSID($sid) or
throw EBox::Exceptions::External(__('Incorrect SID: {s}', 's' => $sid));
my $users = EBox::Global->modInstance('users');
$users->userExists($user) or
throw EBox::Exceptions::External(__x('User {u} does not exists', 'u' => $user));
my $dn = $users->userDn($user);
$self->_isSambaObject('sambaSamAccount', $dn) or
throw EBox::Exceptions::External(__x('User {u} is not a samba user', 'u' => $user));
$self->_setSID($dn, $sid);
}
sub setGroupSID
{
my ($self, $group, $sid) = @_;
checkSID($sid) or
throw EBox::Exceptions::External(__('Incorrect SID: {s}', 's' => $sid));
my $users = EBox::Global->modInstance('users');
$users->groupExists($group) or
throw EBox::Exceptions::External(__x('Group {g} does not exists', 'g' => $group));
my $dn = $users->groupDn($group);
$self->_isSambaObject('sambaGroupMapping', $dn) or
throw EBox::Exceptions::External(__x('Group {g} is not a samba group', 'g' => $group));
$self->_setSID($dn, $sid);
}
sub _setSID
{
my ($self, $dn, $sambaSID) = @_;
my %attrs = (
changes => [
replace => [
sambaSID => $sambaSID,
]
]
);
$self->{ldap}->modify($dn, \%attrs);
}
sub _delGroup
{
my ($self, $group) = @_;
return unless ($self->{samba}->configured());
if (-d BASEPATH . "/groups/$group") {
EBox::Sudo::root("rm -rf \'" . BASEPATH . "/groups/$group\'");
}
# Remove group from printers
my $samba = EBox::Global->modInstance('samba');
$samba->setPrintersForGroup($group, []);
}
sub _delGroupWarning
{
my ($self, $group) = @_;
return unless ($self->{samba}->configured());
my $path = BASEPATH . "/groups/$group";
my $txt = __('This group has a sharing directory associated ' .
'that contains data.');
unless ($self->_directoryEmpty($path)) {
return ($txt);
}
return undef;
}
sub _userAddOns
{
my ($self, $username) = @_;
return unless ($self->{samba}->configured());
my $users = EBox::Global->modInstance('users');
my $samba = EBox::Global->modInstance('samba');
my @args;
my $share = $self->_userSharing($username) ? "yes" : "no";
my $printers = $samba->_printersForUser($username);
my $args = {
'username' => $username,
'share' => $share,
'is_admin' => $samba->adminUser($username),
'service' => $samba->isEnabled,
'printers' => $printers,
'printerService' => $samba->printerService,
};
return { path => '/samba/samba.mas', params => $args };
}
sub _groupAddOns
{
my ($self, $groupname) = @_;
return unless ($self->{samba}->configured());
my $samba = EBox::Global->modInstance('samba');
my @args;
my $share = $self->_groupSharing($groupname) ? "yes" : "no";
my $printers = $samba->_printersForGroup($groupname);
my $args = {
'groupname' => $groupname,
'share' => $share,
'sharename' => $self->sharingName($groupname),
'service' => $samba->isEnabled,
'printers' => $printers,
'printerService' => $samba->printerService,
};
return { path => '/samba/samba.mas', params => $args };
}
sub _createDir
{
my ($self, $path, $uid, $gid, $chmod) = @_;
if (EBox::Sudo::fileTest('-d', $path)) {
return;
}
my @cmds;
push (@cmds, "/bin/mkdir \'$path\'");
push (@cmds, "/bin/chown $uid:$gid \'$path\'");
if ($chmod) {
push (@cmds, "/bin/chmod $chmod \'$path\'");
}
EBox::Sudo::root(@cmds);
}
sub _directoryEmpty
{
my ($self, $path) = @_;
opendir(DIR, $path) or return 1;
my @ent = readdir(DIR);
return ($#ent == 1);
}
sub migrateUsers
{
my ($self) = @_;
my $users = EBox::Global->modInstance('users');
my $samba = EBox::Global->modInstance('samba');
# update users
foreach my $user ($users->users) {
my $username = $user->{'username'};
$self->_addUser($username);
# check if we have old administrator rights
try {
$samba->adminUser($username);
}
catch EBox::Exceptions::Internal with {
my $ex = shift @_;
if ( $user ne all @{$users->usersInGroup('Administrators')} ) {
$users->addUserToGroup($username, 'Administrators');
# to be sure that isn't another error...
$samba->adminUser($username);
}
else {
$ex->throw();
}
};
}
# update groups
foreach my $group ($users->groups) {
my $groupname = $group->{'account'};
$self->_addGroup($groupname);
}
}
sub checkDomainSID
{
my ($sid) = @_;
defined $sid or return 0;
my @parts = split '-', $sid;
if (@parts < 7) {
return 0;
}
return 1;
}
sub checkSID
{
# we do not discrimintae now etween domainSID and a element SID
return checkDomainSID(@_);
}
sub getSID
{
my $samba = EBox::Global->modInstance('samba');
if (not defined $samba) { # this method can't be called in package
# postinstallation when samba module is not fully installed
die 'cannot get samba module';
# return undef;
return generateSID();
}
my $domain = $samba->workgroup();
# FIXME wtf
my $res = `sudo net getlocalsid $domain 2>&1 `;
if ($? != 0) {
# return undef;
return generateSID();
}
chomp $res;
my @parts = split '\s', $res;
my $sid = pop @parts;
if (not checkDomainSID($sid)) {
# return undef;
# return generateSID();
throw EBox::Exceptions::Internal("Bad net getlocalsid output: @parts");
}
return $sid;
}
sub alwaysGetSID
{
my $sid;
$sid = getSID();
defined $sid or $sid = generateSID();
return $sid;
}
sub generateSID
{
# FIXME: Hardcore SID for testing purposes
return 'S-1-5-21-3818554400-921237426-3143208535';
}
sub getGroupSID
{
my ($self, $group) = @_;
my $usersAndGroups = EBox::Global->modInstance('users');
$usersAndGroups->groupExists($group) or
throw EBox::Exceptions::External(__x('{g} group does not exist',
'g' => $group));
my $sidAttr = 'sambaSID';
my $ldap = $self->{ldap};
my %searchArgs = (
base => $usersAndGroups->groupsDn,
filter => "&(objectclass=eboxGroup)(cn=$group)",
scope => 'sub',
attrs => [$sidAttr],
);
my $result = $ldap->search(\%searchArgs);
my $groupEntry = $result->entry(0);
return $groupEntry->get_value($sidAttr);
}
sub _groupSharing
{
my ($self, $group) = @_;
return $self->sharingName($group) ? 1 : undef;
}
# Checks if a resource name exists
sub _sharingResourceExists
{
my ($self, $name) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $dn = $users->groupsDn;
my %attrs = (
base => $dn,
filter => "(&(objectclass=eboxGroup) " .
"(displayResource=$name))",
scope => 'one'
);
my $result = $ldap->search(\%attrs);
return ($result->count == 1)
}
# TODO Find another name more self-explanatory, this one is crap
sub userShareDirectories
{
my ($self) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $dn = $users->usersDn;
my %attrs = (
base => $dn,
filter => '(&(objectclass=posixAccount) (objectclass=sambaSamAccount))',
attrs => [ 'uid', 'homedirectory'],
scope => 'one'
);
my $result = $ldap->search(\%attrs);
my @share;
foreach my $entry ($result->entries) {
my $user = $entry->get_value('uid');
my $home = $entry->get_value('homeDirectory');
push (@share, { path => $home,
username => $user,
sharename => $user
});
}
return \@share;
}
# TODO Find another name more self-explanatory, this one is crap
sub groupShareDirectories
{
my ($self) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $dn = $users->groupsDn;
my %attrs = (
base => $dn,
filter => '(&(objectclass=posixGroup) (objectclass=eboxGroup))',
attrs => [ 'cn', 'displayResource', 'description' ],
scope => 'one'
);
my $result = $ldap->search(\%attrs);
my @share;
foreach my $entry ($result->entries) {
my $group = $entry->get_value('cn');
my $name = $entry->get_value('displayResource');
my $comment = $entry->get_value('description');
($name) or next;
push (@share, { path => BASEPATH . "/groups/$group",
groupname => $group,
sharename => $name,
groupcomment => $comment,
});
}
return \@share;
}
sub sharingName
{
my ($self, $group) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
unless ($users->groupExists($group)) {
throw EBox::Exceptions::DataNotFound('data' => __('group name'),
'value' => $group);
}
my $dn = "cn=$group," . $users->groupsDn;
my %attrs = (
base => $dn,
filter => "(objectclass=eboxGroup)",
attrs => ['displayResource'],
scope => 'base'
);
my $result = $ldap->search(\%attrs);
my $entry = $result->entry(0);
my $value = $entry->get_value('displayResource');
return $value ? $value : "";
}
# Sets the name for a sharing resource in a group
sub setSharingName
{
my ($self, $group, $name) = @_;
my $users = EBox::Global->modInstance('users');
unless ($users->groupExists($group)) {
throw EBox::Exceptions::DataNotFound('data' => __('group name'),
'value' => $group);
}
if ((not defined $name) or ( $name =~ /^\s*$/)) {
throw EBox::Exceptions::External(__("A name should be provided for the share."));
}
my $oldname = $self->sharingName($group);
return if ($oldname and $oldname eq $name);
if ($self->_sharingResourceExists($name)) {
throw EBox::Exceptions::DataExists(
'data' => __('sharing resource'),
'value' => $name);
}
my $dn = "cn=$group," . $users->groupsDn;
my %attrs;
if ($self->_groupSharing($group)) {
%attrs = ( changes =>
[ replace => [ displayResource => $name ]]);
} else {
%attrs = ( changes =>
[ add => [ displayResource => $name ]]);
}
my $add = $self->{ldap}->modify($dn, \%attrs);
# we need to set the module as changed to regenConfig when saving
# Above stuff is stored in ldap so global has no idea its conf
# has changed.
my $global = EBox::Global->modInstance('global');
$global->modChange('samba');
unless ( -d BASEPATH . "/groups/$group"){
my $uid = getpwnam(EBox::Config::user);
my $gid = $users->groupGid($group);
$self->_createDir(BASEPATH . "/groups/$group",
$uid, $gid, "g+w");
}
}
sub removeSharingName
{
my ($self, $group) = @_;
my $users = EBox::Global->modInstance('users');
unless ($users->groupExists($group)) {
throw EBox::Exceptions::DataNotFound('data' => __('group name'),
'value' => $group);
}
my $dn = "cn=$group," . $users->groupsDn;
my %attrs = ( changes => [ delete => [ displayResource => [] ]]);
$self->{ldap}->modify($dn, \%attrs);
# we need to set the module as changed to regenConfig when saving
# Above stuff is stored in ldap so global has no idea its conf
# has changed.
my $global = EBox::Global->modInstance('global');
$global->modChange('samba');
}
sub _pwdChanged
{
my ($self, $entry, $newPassword) = @_;
my $ntpwd = $entry->get_value('sambaNTPassword');
return ($ntpwd ne nthash($newPassword));
}
sub _getAccountFlags
{
my ($self, $username) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
my $dn = "uid=$username," . $users->usersDn;
my %attrs = (
base => $dn,
filter => "(objectclass=*)",
attrs => [ 'sambaAcctFlags'],
scope => 'base'
);
my $result = $ldap->search(\%attrs);
my $entry = $result->entry(0);
return $entry->get_value('sambaAcctFlags');
}
sub _userSharing
{
my ($self, $username) = @_;
my $flags = $self->_getAccountFlags($username);
return (not ($flags =~ /D/));
}
sub setUserSharing
{
my ($self, $username, $share) = @_;
my $ldap = $self->{ldap};
my $users = EBox::Global->modInstance('users');
unless ($users->userExists($username)){
throw EBox::Exceptions::DataNotFound(
'data' => __('group'),
'value' => $username);
}
my $dn = "uid=$username," . $users->usersDn;
my $flags;
if ($share eq 'yes') {
return if $self->_userSharing($username);
$flags = $self->_getAccountFlags($username);
$flags =~ s/D//g;
} else {
return unless $self->_userSharing($username);
$flags = $self->_getAccountFlags($username);
$flags =~ s/U/UD/g;
}
my %attrs = ( replace => { 'sambaAcctFlags' => $flags });
$ldap->modify($dn, \%attrs );
}
# Method: sambaDomainName
#
# Fetch the samba domain name
#
# Returns:
#
# string - samba domain name
#
sub sambaDomainName
{
my ($self) = @_;
my $ldap = $self->{ldap};
my %attrs = (
base => $ldap->dn(),
filter => "(objectclass=sambaDomain)",
scope => "sub"
);
my $entry = $ldap->search(\%attrs)->pop_entry();
if ($entry) {
return $entry->get_value('sambaDomainName');
} else {
return undef;
}
}
# Method: setSambaDomainName
#
# Set the samba domain name. The entry is created if it does not
# exits
#
# Parameters:
#
# name - string containing the domain name
#
# Throws:
#
# InvalidData - wrong domain name
sub setSambaDomainName
{
my ($self, $domain) = @_;
my $ldap = $self->{ldap};
my $sid = getSID();
my %domainAttrs = %{$self->_fetchDomainAttrs($domain)};
$self->deleteSambaDomainNameAttrs();
$self->deleteSambaDomains();
my $users = EBox::Global->modInstance('users');
my %attrs = (
attr => [
'sambaDomainName' => $domain,
'sambaSID' => $sid,
'uidNumber' => $users->lastUid,
'gidNumber' => $users->lastGid,
'objectClass' => ['sambaDomain',
'sambaUnixidPool'],
%domainAttrs
]
);
my $dn = "sambaDomainName=$domain," . $ldap->dn();
$ldap->add($dn, \%attrs);
}
sub _fetchDomainAttrs
{
my ($self, $domain) = @_;
my $ldap = $self->{ldap};
my @attrs = qw/sambaPwdHistoryLength sambaMaxPwdAge
sambaMinPwdLength sambaLockoutThreshold/;
my $result = $ldap->search(
{
base => $ldap->dn(),
filter => "sambaDomainName=$domain",
scope => 'sub',
attrs => [@attrs],
}
);
my $entry = $result->pop_entry();
return {} unless defined($entry);
my %attributes;
for my $attr (@attrs) {
my $value = $entry->get_value($attr);
if (defined($value)) {
$attributes{$attr} = $value;
}
}
return \%attributes;
}
sub deleteSambaDomainNameAttrs
{
my ($self) = @_;
my $ldap = $self->{ldap};
my $attr = 'sambaDomainName';
my $result = $ldap->search(
{
base => $ldap->dn(),
filter => "$attr=*",
scope => 'sub',
attrs => [$attr],
}
);
foreach my $entry ($result->entries()) {
my @values = $entry->get_value($attr);
@values or
next;
$entry->delete( $attr => \@values );
$entry->update($ldap->ldapCon);
}
# my $ldap = $self->{ldap};
# my %searchParams = (
# base => $ldap->dn(),
# filter => "(sambaDomainName=*)",
# attrs => ['sambaDomainName'],
# scope => "sub"
# );
# foreach my $entry ($ldap->search(\%searchParams)->entries()) {
# my $dn = 'sambaDomainName=' .
# $entry->get_value('sambaDomainName') . ',' . $ldap->dn();
# $ldap->delete($dn);
# }
}
sub deleteSambaDomains
{
my ($self) = @_;
my $ldap = $self->{ldap};
my %searchParams = (
base => $ldap->dn(),
filter => "objectClass=sambaDomain",
scope => "sub"
);
foreach my $entry ($ldap->search(\%searchParams)->entries()) {
$entry->delete;
$entry->update($ldap->ldapCon);
}
}
# Method: setSambaDomain
#
# Set the samba domain name. The entry is created if it does not
# exits
#
# Parameters:
#
# name - string containing the domain name
#
# Throws:
#
# InvalidData - wrong domain name
# sub setSambaDomains
# {
# my ($self) = @_;
# my $users = EBox::Global->modInstance('users');
# my $lastUid = $users->lastUid;
# my $lastGid = $users->lastGid;
# my $modifyParams = {
# replace => {
# uidNumber => $lastUid,
# gidNumber => $lastGid,
# },
# };
# my $ldap = $self->{ldap};
# my %attrs = (
# base => $ldap->dn,
# filter => "objectClass=sambaDomain",
# scope => "sub"
# );
# my @sambaDomains = $ldap->search(\%attrs)->entries();
# if (not @sambaDomains) {
# return 0;
# }
# foreach my $entry (@sambaDomains) {
# my $dn = $entry->dn;
# if (not $ldap->isObjectClass($dn, 'sambaUnixIdPool')) {
# $ldap->modify($dn,
# { add => {
# objectClass => 'sambaUnixIdPool',
# uidNumber => $lastUid,
# gidNumber => $lastGid,
# }
# }
# );
# }
# else {
# $ldap->modify($dn, $modifyParams);
# }
# }
# return 1;
# }
# Method: updateNetbiosName
#
# Update in LDAP all those attributes which contain the netbios name
#
# Parameters:
#
# netbios - string containing the new netbios name
#
# Throws:
#
# InvalidData - wrong domain name
sub updateNetbiosName
{
my ($self, $netbios) = @_;
my $users = EBox::Global->modInstance('users');
my $ldap = $self->{'ldap'};
my %attrs = (
base => $users->usersDn(),
filter => "(objectclass=sambaSamAccount)",
attrs => ['uid'],
scope => 'sub'
);
for my $entry ($ldap->search(\%attrs)->entries()) {
my $dn = $entry->dn();
my $username = $entry->get_value('uid');
$ldap->modifyAttribute($dn, 'sambaHomePath',
"\\\\$netbios\\homes\\$username");
# XXX Check if we have to add or not, instead of
# add + [ delete ]
$ldap->modifyAttribute($dn, 'sambaProfilePath',
"\\\\$netbios\\profiles\\$username");
unless ($self->{samba}->roamingProfiles()) {
my %attrs = ( changes => [ delete =>
[ sambaProfilePath => [] ]]);
$ldap->modify($dn, \%attrs);
}
}
}
# Method: updateSIDEntries
#
# Check and correct if there's any user or group with a wrong SID. Note
# that depending on when the user/group is created the SID might change.
# This method should be run in regenConfig
#
sub updateSIDEntries
{
my ($self) = @_;
my $users = EBox::Global->modInstance('users');
my $ldap = $self->{'ldap'};
my $userDN = $users->usersDn();
my $sid = uc(getSID());
$sid = uc($sid);
my %attrs = (
base => $userDN,
filter => "(&(objectclass=sambaSamAccount)(!(sambaSID=$sid*)))",
attrs => ['sambaSID', 'sambaPrimaryGroupSID', 'dn'],
scope => 'sub'
);
my $result = $ldap->search(\%attrs);
for my $entry ($result->entries()) {
my $oldSID = $entry->get_value('sambaSID');
my $oldGroupSID = $entry->get_value('sambaPrimaryGroupSID');
my ($lastNumbers) = $oldSID =~ /.*-(\d+)$/;
my $newSID = "$sid-$lastNumbers";
my ($lastNumbersGroup) = $oldGroupSID =~ /.*-(\d+)$/;
my $newGroupSID = "$sid-$lastNumbersGroup";
$ldap->modifyAttribute($entry->dn(), 'sambaSID', $newSID);
$ldap->modifyAttribute($entry->dn(),
'sambaPrimaryGroupSID',
$newGroupSID);
}
my $groupDN = $users->groupsDn();
%attrs = (
base => $groupDN,
filter => "(&(objectclass=sambaGroupMapping)(!(sambaSID=$sid*)))",
attrs => ['sambaSID', 'cn'],
scope => 'sub'
);
$result = $ldap->search(\%attrs);
my %groupsToSkip = ('Administrators' => 1,
'Account Operators' => 1,
'Print Operators' => 1,
'Backup Operators' => 1,
'Replicators' => 1);
for my $entry ($result->entries()) {
next if $groupsToSkip{$entry->get_value('cn')};
my $oldSID = $entry->get_value('sambaSID');
my ($lastNumbers) = $oldSID =~ /.*-(\d+)$/;
my $newSID = "$sid-$lastNumbers";
$ldap->modifyAttribute($entry->dn(), 'sambaSID', $newSID);
}
}
sub _isSambaObject
{
my ($self, $object, $dn) = @_;
my $ldap = $self->{ldap};
my %attrs = (
base => $dn,
filter => "(objectclass=$object)",
attrs => [ 'objectClass'],
scope => 'base'
);
my $ret;
try {
my $result = $ldap->search(\%attrs);
if ($result->count == 1) {
$ret = 1;
}
} otherwise {};
return $ret;
}
# return a ref to a list of the paths of all (users and group) shared directories
sub sharedDirectories
{
my ($self) = @_;
my @dirs;
@dirs = map {
$_->{path}
} @{ $self->groupShareDirectories() };
my $users = EBox::Global->modInstance('users');
defined $users or throw EBox::Exceptions::Internal('Cannot get users and groups module');
my @homedirs = map { $_->{homeDirectory}} $users->users();
push @dirs, @homedirs;
return \@dirs;
}
sub basePath
{
return BASEPATH;
}
sub usersPath
{
return USERSPATH;
}
sub groupsPath
{
return GROUPSPATH;
}
sub schemas
{
return [
EBox::Config::share() . "zentyal-samba/samba.ldif",
EBox::Config::share() . "zentyal-samba/ebox.ldif",
];
}
sub indexes
{
return ['sambaSID', 'sambaGroupType', 'sambaSIDList', 'sambaDomainName'];
}
sub acls
{
my ($self) = @_;
return [ "to attrs=sambaNTPassword,sambaLMPassword " .
"by dn=\"" . $self->{ldap}->rootDn() . "\" write by self write " .
"by * none" ];
}
# Method: defaultUserModel
#
# Overrides <EBox::UsersAndGrops::LdapUserBase::defaultUserModel>
# to return our default user template
sub defaultUserModel
{
return 'samba/SambaUser';
}
1;
|