package INSTALL::user; use INSTALL::common; use INSTALL::os; my $debug = 0; #============================================================================== # Miscelaneous / Helpers #============================================================================== sub random_password { srand(time); my @alphabet = ('A' .. 'Z', 'a' .. 'z', '0' .. '9', '_', '-', '!', '\@', '\#', '\$', '\%', '^', '&', '*', '(', ')', '+', '=', '~', '\`'); my $length = $#alphabet + 1; my $result = ""; my $digit; for($i=0; $i < 8; $i++) { $digit = $alphabet[int(rand() * $length)]; $result .= $digit; } if ($debug) { print "Randomly generated password: $result\n"; } return($result); } #============================================================================== # Linux - internal #============================================================================== sub add_linux { my ($username, $password, $shell, $home, $group, $comment) = @_; DIST_SWITCH: for($INSTALL::os::Details{distribution_flavor}) { (/RedHat/i || /SuSE/i || /Slackware/i || /Debian/i) && do { my $flags = ""; ($flags .= "-c '$comment' ") if $comment; ($flags .= "-d '$home' ") if $home; ($flags .= "-s '$shell' ") if $shell; ($flags .= "-g '$group' ") if $group; $flags .= $username; (not system_log("/usr/sbin/useradd $flags")) || return(error("Unable to create user: $username")); open(SH,"|/usr/sbin/chpasswd") || system_log("/usr/sbin/userdel $username") || return(error("Unable to set password of user $username. Deleting user.")); print SH "$username:$password\n"; close(SH); last DIST_SWITCH; }; return(error("Distribution flavor not handled in INSTALL::user::add_linux ". "[ $INSTALL::os::Details{distribution_flavor} ].")); } return(1); } sub del_linux { my ($username) = @_; DIST_SWITCH: for($INSTALL::os::Details{distribution_flavor}) { (/RedHat/i || /SuSE/i || /Slackware/i || /Debian/i) && do { (not system_log("/usr/sbin/userdel $username")) || return(error("Unable to delete user: $username")); last DIST_SWITCH; }; return(error("Distribution flavor not handled in INSTALL::user::del_linux [ ", $INSTALL::os::Details{distribution_flavor}, "].")); } return(1); } #============================================================================== # Solaris - internal #============================================================================== sub add_solaris { return(error("Unimplemented: INSTALL::user::add_solaris.")); } sub del_solaris { return(error("Unimplemented: INSTALL::user::del_solaris.")); } #============================================================================== # AIX - internal #============================================================================== sub add_aix { return(error("Unimplemented: INSTALL::user::add_aix.")); } sub del_aix { return(error("Unimplemented: INSTALL::user::del_aix.")); } #============================================================================== # HP/UX - internal #============================================================================== sub add_hpux { return(error("Unimplemented: INSTALL::user::add_hpux.")); } sub del_hpux { return(error("Unimplemented: INSTALL::user::del_hpux.")); } #------------------------------------------------------------------------------ #============================================================================== # Exposed interfaces #============================================================================== #------------------------------------------------------------------------------ # Returns non-zero on success. sub add { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # username N # password Y # shell Y # home directory Y # group Y # comment "" Y my $username; my $password = random_password; my $shell; my $home; my $group; my $comment; my $length = $#_; if ($length == 0) { ($username) = @_; } elsif ($length == 1) { ($username,$password) = @_; } elsif ($length == 2) { ($username,$password,$shell) = @_; } elsif ($length == 3) { ($username,$password,$shell,$home) = @_; } elsif ($length == 4) { ($username,$password,$shell,$home,$group) = @_; } elsif ($length == 5) { ($username,$password,$shell,$home,$group,$comment) = @_; } else { return(error("Invalid number of parameters to INSTALL::user::add [ ", ($length + 1), "].")); } if (INSTALL::os::detect) { OS_SWITCH: for($INSTALL::os::Details{os}) { /Linux/i && return(add_linux($username,$password,$shell,$home,$group,$comment)); /SunOs/i && return(add_solaris($username,$password,$shell,$home,$group,$comment)); /AIX/i && return(add_aix($username,$password,$shell,$home,$group,$comment)); } } return(error("Unknown operating system encountered in INSTALL::user::add.")); } # Return non-zero on success. sub del { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # username N if ($#_ != 0) { return(error("Invalid number of parameters to INSTALL::user::del [ ", ($#_ + 1), "].")); } my ($username) = @_; if (INSTALL::os::detect) { OS_SWITCH: for($INSTALL::os::Details{os}) { /Linux/i && return(del_linux($username)); /SunOs/i && return(del_solaris($username)); /AIX/i && return(del_aix($username)); } } print $INSTALL::os::Details{os},($INSTALL::os::Details{os} =~ /Linux/i),"\n"; return(error("Unknown operating system encountered in INSTALL::user::del.")); } # Returns non-zero on success. sub exists { #--------------------------------------------------------------------------- # Parameter: Default value: Optional: #--------------------------------------------------------------------------- # username N if ($#_ != 0) { return(error("Invalid number of parameters to INSTALL::user::exists [ ", ($#_ + 1), "].")); } my ($username) = @_; if (getpwnam($username)) { return(1); } else { return(0); } } # Returns non-zero, if the user has been updated. sub updated { #--------------------------------------------------------------------------- # Parameter: Optional: #--------------------------------------------------------------------------- # username N # password Y # shell Y # home directory Y # group Y # comment Y my $username; my $password = random_password; my $shell; my $home; my $group; my $comment; my $length = $#_; if ($length == 0) { ($username) = @_; } elsif ($length == 1) { ($username,$password) = @_; } elsif ($length == 2) { ($username,$password,$shell) = @_; } elsif ($length == 3) { ($username,$password,$shell,$home) = @_; } elsif ($length == 4) { ($username,$password,$shell,$home,$comment) = @_; } elsif ($length == 5) { ($username,$password,$shell,$home,$group,$comment) = @_; } else { return(error("Invalid number of parameters to INSTALL::user::updated [ ", ($length + 1), "].")); } if (INSTALL::user::exists($username)) { my ($ca_name,$ca_passwd,$ca_uid,$ca_gid,$ca_quota,$ca_comment,$ca_gcos,$ca_home,$ca_shell,$ca_expire) = getpwnam("chiliasp"); if ($ca_passwd eq "x") { # This probably means they are using shadow passwords. open(SHADOW,") { if (/$username:([^:]*):/) { $ca_passwd = $1; } } close(SHADOW); } if ((defined $password && (crypt($password,$ca_passwd) ne $ca_passwd)) || (defined $shell && ($shell ne $ca_shell)) || (defined $home && ($home ne $ca_home)) || (defined $comment && ($comment ne $ca_comment))) { return(1); } } return(0); } 1;