Skip to content

Instantly share code, notes, and snippets.

@battila
Created February 24, 2014 15:53
Show Gist options
  • Select an option

  • Save battila/9190944 to your computer and use it in GitHub Desktop.

Select an option

Save battila/9190944 to your computer and use it in GitHub Desktop.

Revisions

  1. battila created this gist Feb 24, 2014.
    72 changes: 72 additions & 0 deletions ldap2local.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/usr/bin/perl -w
    #============================================================
    #
    # FILE: ldap2local.pl
    #
    # USAGE: ./ldap2local.pl
    #
    # DESCRIPTION: convert ldap user to local
    #
    #
    # AUTHOR: Attila Bardi
    # VERSION: 0.1
    # CREATED: 2014/02/24
    # Last Update: 2014/02/24
    #============================================================

    use strict;
    use warnings;

    my %ldap_config = (
    5 => '/etc/ldap.conf',
    6 => '/etc/pam_ldap.conf',
    );


    sub check_if_the_system_connected_to_ldap {

    system("grep ^passwd /etc/nsswitch.conf|grep ldap>/dev/null");

    if (!$?) {
    $_ = `cat /etc/*-release`;
    foreach my $key ( keys %ldap_config) {
    if ( /Server release ($key)/ ) {
    if (-e $ldap_config{$1} ) {
    return $1;
    }
    }
    }
    }
    print "Fuck bitches!\n";
    undef;
    }


    sub get_ldap_group {

    open my $LDAP, $ldap_config{$_[0]} or die "Could not open $ldap_config{$_[0]}: $!";

    foreach (grep /^pam_groupdn/, <$LDAP>) {
    ($_) = split ',';
    (undef, $_) = split '=';
    return $_;
    }
    }

    $_ = &check_if_the_system_connected_to_ldap;

    if ( $_ ) {

    my $group = &get_ldap_group($_);
    chomp (my $gid = `getent group $group | cut -d: -f3`);
    print "Creating group.\n";
    print "groupadd -g $gid $group\n";
    # system 'groupadd -g ', $_, $group_name;

    chomp ($_ = `getent group $group | cut -d: -f4`);
    foreach ( sort split ',' ) {
    chomp (my $uid = `getent passwd $_|cut -d: -f3`);
    print "useradd -u $uid -c $_ -d /home/$_ -g $gid -s /bin/bash $_\n";
    # system 'uuseradd -u $uid -c $_ -d /home/$_ -g $gid -s /bin/bash $_';
    }
    }