Samba-LDAP

 view release on metacpan or  search on metacpan

scripts/smbldap-usermod  view on Meta::CPAN

my $ldap_master=connect_ldap_master();

# Read user data
my $user_entry = read_user_entry($user);
if (!defined($user_entry)) {
  print "$0: user $user doesn't exist\n";
  exit (1);
}

my $samba = 0;
if (grep ($_ =~ /^sambaSamAccount$/i, $user_entry->get_value('objectClass'))) {
  $samba = 1;
}

# get the dn of the user
my $dn= $user_entry->dn();

my $tmp;
my @mods;
my @dels;
if (defined($tmp = $Options{'a'})) {
  # Let's connect to the directory first
  my $winmagic = 2147483647;
  my $valpwdcanchange = 0;
  my $valpwdmustchange = $winmagic;
  my $valpwdlastset = 0; 
  my $valacctflags = "[UX]";
  my $user_entry=read_user_entry($user);
  my $uidNumber = $user_entry->get_value('uidNumber');
  my $userRid = 2 * $uidNumber + 1000;
  # apply changes
  my $modify = $ldap_master->modify ( "$dn",
				      changes => [
						  add => [objectClass => 'sambaSAMAccount'],
						  add => [sambaPwdLastSet => "$valpwdlastset"],
						  add => [sambaLogonTime => '0'],
						  add => [sambaLogoffTime => '2147483647'],
						  add => [sambaKickoffTime => '2147483647'],
						  add => [sambaPwdCanChange => "$valpwdcanchange"],
						  add => [sambaPwdMustChange => "$valpwdmustchange"],
						  add => [displayName => "$config{userGecos}"],
						  add => [sambaSID=> "$config{SID}-$userRid"],
						  add => [sambaAcctFlags => "$valacctflags"],
						 ]
				    );
  $modify->code && warn "failed to modify entry: ", $modify->error ;
}

# Process options
my $changed_uid;
my $_userUidNumber;
my $_userRid;
if (defined($tmp = $Options{'u'})) {
  if (defined($Options{'o'})) {
    $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
	
    if ($nscd_status == 0) {
      system "/etc/init.d/nscd stop > /dev/null 2>&1";
    }

    if (getpwuid($tmp)) {
      if ($nscd_status == 0) {
	system "/etc/init.d/nscd start > /dev/null 2>&1";
      }

      print "$0: uid number $tmp exists\n";
      exit (6);
    }
    if ($nscd_status == 0) {
      system "/etc/init.d/nscd start > /dev/null 2>&1";
    }

  }
  push(@mods, 'uidNumber', $tmp);
  $_userUidNumber = $tmp;
  if ($samba) {
    # as rid we use 2 * uid + 1000
    my $_userRid = 2 * $_userUidNumber + 1000;
    if (defined($Options{'x'})) {
      $_userRid= sprint("%x", $_userRid);
    }
    push(@mods, 'sambaSID', $config{SID}.'-'.$_userRid);
  }
  $changed_uid = 1;
}

my $changed_gid;
my $_userGidNumber;
my $_userGroupSID;
if (defined($tmp = $Options{'g'})) {
  $_userGidNumber = parse_group($tmp);
  if ($_userGidNumber < 0) {
    print "$0: group $tmp doesn't exist\n";
    exit (6);
  }
  push(@mods, 'gidNumber', $_userGidNumber);
  if ($samba) {
    # as grouprid we use the sambaSID attribute's value of the group
    my $group_entry = read_group_entry_gid($_userGidNumber);
    my $_userGroupSID = $group_entry->get_value('sambaSID');
    unless ($_userGroupSID) {
      print "Error: sambaPrimaryGroupSid could not be set (sambaSID for group $_userGidNumber does not exist\n";
      exit (7);
    }
    push(@mods, 'sambaPrimaryGroupSid', $_userGroupSID);
  }
  $changed_gid = 1;
}

if (defined($tmp = $Options{'s'})) {
  push(@mods, 'loginShell' => $tmp);
}


if (defined($tmp = $Options{'c'})) {
  push(@mods, 'gecos' => $tmp,
       'description' => $tmp);
  if ($samba == 1) {
    push(@mods, 'displayName' => $tmp);
  }
}



( run in 0.848 second using v1.01-cache-2.11-cpan-f56aa216473 )