Plugtools
view release on metacpan or search on metacpan
lib/Plugtools.pm view on Meta::CPAN
}
#add it
my $mesg=$entry->update($ldap);
if (!$mesg->{errorMessage} eq '') {
$self->{error}=19;
$self->{errorString}='$entry->update($ldap) failed. $mesg->{errorMessage}="'.
$mesg->{errorMessage}.'"';
warn('Plugtools addGroup:19: '.$self->{errorString});
return undef;
}
return 1;
}
=head2 addUser
=head3 args hash
=head4 user
The user to create.
=head4 uid
The numeric user ID for the new user. If this is note defined,
the first free one will be used.
=head4 group
The primary group of user. If this is not defined, the username is
used. If the user is this is not defined, it will be set to the same
as the user.
=head4 gid
If this is defined, the specified GID will be used instead of automatically
assigning one.
=head4 gecos
The gecos field for the user. If this is not defined, it is set to
the user name.
=head4 shell
This is the shell for the user. If this is not defined, the default
one is used.
=head4 home
This is the home directory for the user. If this is not defined, the
home prototype is used.
=head4 createHome
If this is specified, the default value for createHome will be overrode the
defaults or what is specified in the config.
If it exists, it assumes it does not need to be created, but it will still be
chowned.
=head4 skel
Use this instead of the default skeleton or the one specified in the config file.
This is skipped, if the home already exists.
=head4 chmodValue
Overrides the default value for this or the one specified in the config.
=head4 chmodHome
Overrides the default value for this or the one specified in the config.
=head4 chownHome
If home should be chowned. This overrides the value specified in the
config or the default one.
=head4 dump
If this is true, call the dump method on the create Net::LDAP::Entry object.
#the most basic form
$pt->addUser({
user=>'someUser',
})
if($pt->{errpr}){
print "Error!\n";
}
#do more
$pt->addUser({
user=>'someUser',
uid=>'3333',
group=>'someGroup',
gid=>'4444',
dump=>'1',
})
if($pt->{errpr}){
print "Error!\n";
}
=cut
sub addUser{
my $self=$_[0];
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
#blank any previous errors
$self->errorblank;
#error if no user has been specified
if (!defined($args{user})) {
$self->{error}=5;
$self->{errorString}='No user name specified';
warn('Plugtools addUser:5: '.$self->{errorString});
return undef;
}
#error if the user already exists
my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($args{user});
if (defined($name)) {
$self->{error}=9;
$self->{errorString}='The user "'.$args{user}.'" already exists';
warn('Plugtools addUser:9: '.$self->{errorString});
return undef;
}
#make sure we have gecos
if (!defined($args{gecos})) {
$args{gecos}=$args{user};
}
lib/Plugtools.pm view on Meta::CPAN
($gname,$gpasswd,$args{gid},$members) = getgrnam($args{group});
#build the user
$args{home}=$self->{ini}->{''}->{HOMEproto};
$args{home}=~s/\%\%USERNAME\%\%/$args{user}/g;
#initiates the Net::LDAP::posixAccount
my $entrycreator=Net::LDAP::posixAccount->new({ baseDN=>$self->{ini}->{''}->{userbase} });
my $entry=$entrycreator->create({
name=>$args{user},
uid=>$args{uid},
gid=>$args{gid},
home=>$args{home},
loginShell=>$args{shell},
primary=>$self->{ini}->{''}->{userPrimary},
});
#connect to the LDAP server
my $ldap=$self->connect();
if ($self->{error}) {
warn('Plugtools addUser: Failed to connect to LDAP');
return undef;
}
#call a plugin if needed
if (defined($self->{ini}->{''}->{pluginAddUser})) {
$self->plugin({
ldap=>$ldap,
entry=>$entry,
do=>'pluginAddUser',
},
\%args);
if ($self->{error}) {
warn('Plugtools addUser: plugin errored');
return undef;
}
}
#add it
my $mesg=$entry->update($ldap);
if (!$mesg->{errorMessage} eq '') {
$self->{error}=19;
$self->{errorString}='$entry->update($ldap) failed. $mesg->{errorMessage}="'.
$mesg->{errorMessage}.'"';
warn('Plugtools addUser:19: '.$self->{errorString});
return undef;
}
#dump it if needed
if ($args{dump}) {
$entry->dump;
}
#create the home directory if needed, after getting the required values
if (!defined($args{createHome})) {
$args{createHome}=$self->{ini}->{''}->{createHome};
}
if (!defined($args{skel})) {
$args{skel}=$self->{ini}->{''}->{skeletonHome};
}
if (!defined($args{chownHome})) {
$args{chownHome}=$self->{ini}->{''}->{chownHome};
}
if (!defined($args{chmodHome})) {
$args{chmodHome}=$self->{ini}->{''}->{chmodHome};
}
if (!defined($args{chmodValue})) {
$args{chmodValue}=$self->{ini}->{''}->{chmodValue};
}
if ($args{createHome}) {
if (! -e $args{home}) {
#copy it
system( 'cp -r '.shell_quote($args{skel}).' '.shell_quote($args{home}) );
if ($? ne '0') {
$self->{error}=22;
$self->{errorString}='Copying home from "'.$args{skel}.'" to "'.$args{home}.'" failed';
warn('Plugtools addUser:22: '.$self->{errorString});
return undef;
}
#chown it if needed
if ($args{chownHome}) {
system( 'chown -R '.shell_quote($args{user}).':'.shell_quote($args{group})
.' '.shell_quote($args{home}) );
if ($? ne '0') {
$self->{error}=23;
$self->{errorString}='Chowning "'.$args{home}.'" to "'.$args{chmodValue}.'" failed';
warn('Plugtools addUser:22: '.$self->{errorString});
return undef;
}
}
#chmod it if needed
if ($args{chmodHome}) {
system( 'chmod -R '.shell_quote($args{chmodValue}).' '.shell_quote($args{home}) );
if ($? ne '0') {
$self->{error}=24;
$self->{errorString}='Chmoding "'.$args{home}.'" to "'.$args{chmodValue}.'" failed';
warn('Plugtools addUser:22: '.$self->{errorString});
return undef;
}
}
}
}
return 1;
}
=head2 connect
This forms a LDAP connection using the information in
config file.
my $ldap=$pt->connect;
if($pt->{error}){
print "Error!\n";
}
=cut
sub connect{
my $self=$_[0];
#blanks any previous errors
$self->errorblank;
#try to connect
my $ldap = Net::LDAP->new($self->{ini}->{''}->{server}, port=>$self->{ini}->{''}->{port});
#check if it connected or not
if (!$ldap) {
$self->{error}=11;
$self->{errorString}='Failed to connect to LDAP';
warn('Plugtools connect:11: '.$self->{errorString});
return undef;
}
#start TLS if it is needed
my $mesg;
if ($self->{ini}->{''}->{starttls}) {
$mesg=$ldap->start_tls(
lib/Plugtools.pm view on Meta::CPAN
#if it is not defined, use the default one
if (!defined($config)) {
$config=xdg_config_home().'/plugtoolsrc';
}
#reads the config
my $ini=ReadINI($config);
#errors if it is not defined... meaning it errored
if (!defined($ini)) {
$self->{error}=1;
$self->{errorString}='Failed to read the config';
warn('Plugtools readConfig:1: '.$self->{errorString});
return undef;
}
#puts together a array to check for the required ones
my @required;
push(@required, 'bind');
push(@required, 'pass');
push(@required, 'userbase');
push(@required, 'groupbase');
#make sure they are all defined
my $int=0;
while (defined($required[$int])) {
#error if it is not defined
if (!defined($ini->{''}->{$required[$int]})) {
$self->{error}=2;
$self->{errorString}='The required variable "'.$required[$int].'" is not defined in the config, "'.$config.'",';
warn('Plugtools readConfig:2: '.$self->{errorString});
return undef;
}
$int++;
}
#define the defaults if they are not defined
if (!defined($ini->{''}->{UIDstart})) {
$ini->{''}->{UIDstart}='1001';
}
if (!defined($ini->{''}->{GIDstart})) {
$ini->{''}->{GIDstart}='1001';
}
if (!defined($ini->{''}->{defaultShell})) {
$ini->{''}->{defaultShell}='/bin/tcsh';
}
if (!defined($ini->{''}->{HOMEproto})) {
$ini->{''}->{HOMEproto}='/home/%%USERNAME%%/';
}
if (!defined($ini->{''}->{skeletonHome})) {
$ini->{''}->{skeletonHome}='/etc/skel/';
}
if (!defined($ini->{''}->{chmodValue})) {
$ini->{''}->{chmodValue}='640';
}
if (!defined($ini->{''}->{chmodHome})) {
$ini->{''}->{chmodHome}='1';
}
if (!defined($ini->{''}->{chownHome})) {
$ini->{''}->{chownHome}='1';
}
if (!defined($ini->{''}->{createHome})) {
$ini->{''}->{createHome}='1';
}
if (!defined($ini->{''}->{groupPrimary})) {
$ini->{''}->{groupPrimary}='cn';
}
if (!defined($ini->{''}->{userPrimary})) {
$ini->{''}->{userPrimary}='uid';
}
if (!defined($ini->{''}->{server})) {
$ini->{''}->{server}='127.0.0.1';
}
if (!defined($ini->{''}->{port})) {
$ini->{''}->{port}='389';
}
if (!defined($ini->{''}->{TLSverify})) {
$ini->{''}->{TLSverify}='none';
}
if (!defined($ini->{''}->{SSLversion})) {
$ini->{''}->{SSLversion}='tlsv1';
}
if (!defined($ini->{''}->{SSLciphers})) {
$ini->{''}->{SSLciphers}='ALL';
}
if (!defined($ini->{''}->{removeHome})) {
$ini->{''}->{removeHome}='0';
}
if (!defined($ini->{''}->{removeGroup})) {
$ini->{''}->{removeGroup}='1';
}
if (!defined($ini->{''}->{userUpdate})) {
$ini->{''}->{userUpdate}='1';
}
#if we get here, the ini is good... so we save it
$self->{ini}=$ini;
return 1;
}
=head2 userGECOSchange
This changes the UID for a user.
=head3 args hash
=head4 user
The user to act on.
=head4 gecos
The GECOS to change this user to.
=head4 dump
Call the dump method on the group afterwards.
$pt->userGECOSchange({
lib/Plugtools.pm view on Meta::CPAN
GID is not numeric.
=head2 9
User already exists.
=head2 10
Group already exists.
=head2 11
Connecting to LDAP failed.
=head2 12
Net::LDAP::posixGroup failed.
=head2 13
Failed to bind to the LDAP server.
=head2 14
The group does not exist.
=head2 15
The group does not exist in LDAP or under specified group base.
=head2 16
Failed to delete the group's entry.
=head2 17
The user does not exist.
=head2 18
The user does not exist in LDAP or under specified user base.
=head2 19
Adding the new entry failed.
=head2 20
The GID already exists.
=head2 21
Failed to create home.
=head2 22
Copying the skeleton to the home location failed.
=head2 23
Failed to chown the new home directory.
=head2 24
Failed to chmod the new home directory.
=head2 25
Failed to update a entry when removing a memberUid.
=head2 26
Failed to remove the users home directory.
=head2 27
Faild to fetch a list posixGroup objects.
=head2 28
No GID specified.
=head2 29
Failed to update the entry when changing the GID.
=head2 30
No UID specified.
=head2 31
Failed to update the entry when changing the UID.
=head2 32
Failed to fetch the user entry.
=head2 33
No GECOS specified.
=head2 34
Failed to update the entry when changing the GECOS.
=head2 35
No password specified.
=head2 36
Updating the password for the user failed.
=head2 37
Errored when fetching a list of users that may possibly need updated.
=head2 38
No LDAP object given.
lib/Plugtools.pm view on Meta::CPAN
pass=somebl00dyp@ssw0rd
userbase=ou=users,dc=foo,dc=bar
groupbase=ou=groups,dc=foo,dc=bar
=head2 bind
This is the DN to bind as.
=head2 pass
This is the password for the bind DN.
=head2 userbase
This is the base for where the users are located.
=head2 groupbase
This is the base where the groups are located.
=head2 server
This is the LDAP server to connect to. If the server is not
specified, '127.0.0.1' is used.
=head2 port
This is the LDAP port to use. If the port is not specified, '389'
is used.
=head2 UIDstart
This is the first UID to start checking for existing users at. The default is '1001'.
=head2 GIDstart
This is the first GID to start checking for existing groups at. The default is '1001'.
=head2 defaultShell
This is the default shell for a user. The default is '/bin/tcsh'.
=head2 HOMEproto
The prototype for the home directory. %%USERNAME%% is replaced with
the username. The default is '/home/%%USERNAME%%/'.
=head2 skeletonHome
This is the location that will be copied for when creating a new home directory. If this is not defined,
a blanked one will be created. The default is '/etc/skel'.
=head2 chmodValue
This is the numeric value the newly created home directory will be chmoded to. The default is '640'.
=head2 chmodHome
If home should be chmoded. The default value is '1', true.
=head2 chownHome
If home should be chowned. The default value is '1', true.
=head2 createHome
If this is true, it the home directory for the user will be created. The default is '1'.
=head2 groupPrimary
This is the attribute to use for when creating the DN for the group entry. Either 'cn' or
'gidNumber' are currently accepted. The default is 'cn'.
=head2 userPrimary
This is the attribute to use for when creating the DN for the user entry. Either
'cn', 'uid', or 'uidNumber' are currently accepted. The default is 'uid'.
=head2 starttls
Wether or not it should try to do start_tls.
=head2 TLSverify
The verify mode for TLS. The default is 'none'.
=head3 none
The server may provide a certificate but it will not be
checked - this may mean you are be connected to the wrong
server.
=head3 optional
Verify only when the server offers a certificate.
=head3 require
The server must provide a certificate, and it must be valid.
=head2 SSLversion
This is the SSL versions accepted.
'sslv2', 'sslv3', 'sslv2/3', or 'tlsv1' are the possible values. The default
is 'tlsv1'.
=head2 SSLciphers
This is a list of ciphers to accept. The string is in the standard OpenSSL
format. The default value is 'ALL'.
=head2 removeGroup
This determines if it should try to remove the user's primary group after removing the
user.
The default value is '1', true.
=head2 removeHome
This determines if it should try to remove a user's home directory when deleting the
user.
( run in 0.321 second using v1.01-cache-2.11-cpan-71847e10f99 )