Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Aug 2001 23:01:44 -0400
From:      "David Nagrosst" <nagrosst@blast.net>
To:        <freebsd-hackers@freebsd.org>
Subject:   Scripts to Ease Account deactivation/reactivation administration. I hacked these scripts up for an ISP I used to work at.
Message-ID:  <NFBBKNOFBCDIJEPNPDDKKEELCAAA.nagrosst@blast.net>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

------=_NextPart_000_0000_01C131A7.BD4994A0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Scripts to Ease Account deactivation/reactivation.  I hacked these scripts
up for an ISP I used to work at.

Just thought I would pass the scripts on, just in case someone else needed
an easy way to deactivate and reactivate accounts, typically you would do
this when users did not pay there bills.

David

------=_NextPart_000_0000_01C131A7.BD4994A0
Content-Type: text/plain;
	name="unstaruser.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="unstaruser.txt"

#!/usr/bin/perl

#Edited and modified by David S. Nagrosst April 2 2001
#Orignally Edited/Hacked from the rmuser script
# -*- perl -*-
# Copyright 1995, 1996, 1997 Guy Helmer, Ames, Iowa 50014.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer as
#    the first lines of this file unmodified.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the =
distribution.
# 3. The name of the author may not be used to endorse or promote =
products
#    derived from this software without specific prior written =
permission.
#
# THIS SOFTWARE IS PROVIDED BY GUY HELMER ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED =
WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE =
DISCLAIMED.
# IN NO EVENT SHALL GUY HELMER BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, =
BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF =
USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE =
OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# rmuser - Perl script to remove users
#
# Guy Helmer <ghelmer@cs.iastate.edu>, 02/23/97
#
# $FreeBSD: src/usr.sbin/adduser/rmuser.perl,v 1.8.2.1 2000/03/20 =
13:00:36 peter=20

sub LOCK_SH {0x01;}
sub LOCK_EX {0x02;}
sub LOCK_NB {0x04;}
sub LOCK_UN {0x08;}
sub F_SETFD {2;}

$ENV{"PATH"} =3D "/bin:/sbin:/usr/bin:/usr/sbin";
umask(022);
$whoami =3D $0;
$passwd_file =3D "/etc/master.passwd";
$new_passwd_file =3D "${passwd_file}.new.$$";
$affirm =3D 0;

sub cleanup {
    local($sig) =3D @_;

    print STDERR "Caught signal SIG$sig -- cleaning up.\n";
    &unlockpw;
    if (-e $new_passwd_file) {
	unlink $new_passwd_file;
    }
    exit(0);
}

sub lockpw {
    # Open the password file for reading
    if (!open(MASTER_PW, "$passwd_file")) {
	print STDERR "${whoami}: Error: Couldn't open ${passwd_file}: $!\n";
	exit(1);
    }
    # Set the close-on-exec flag just in case
    fcntl(MASTER_PW, &F_SETFD, 1);
    # Apply an advisory lock the password file
    if (!flock(MASTER_PW, &LOCK_EX|&LOCK_NB)) {
	print STDERR "${whoami}: Error: Couldn't lock ${passwd_file}: $!\n";
	exit(1);
    }
}

sub unlockpw {
    flock(MASTER_PW, &LOCK_UN);
}

$SIG{'INT'} =3D 'cleanup';
$SIG{'QUIT'} =3D 'cleanup';
$SIG{'HUP'} =3D 'cleanup';
$SIG{'TERM'} =3D 'cleanup';

if ($#ARGV =3D=3D 1 && $ARGV[0] eq '-y') {
    shift @ARGV;
    $affirm =3D 1;
}

if ($#ARGV > 0) {
    print STDERR "usage: ${whoami} [-y] [username]\n";
    exit(1);
}

if ($< !=3D 0) {
    print STDERR "${whoami}: Error: you must be root to use =
${whoami}\n";
    exit(1);
}

&lockpw;

if ($#ARGV =3D=3D 0) {
    # Username was given as a parameter
    $login_name =3D pop(@ARGV);
    die "Sorry, login name must contain alphanumeric characters only.\n"
	if ($login_name !~ /^[a-zA-Z0-9_]\w*$/);
} else {
    if ($affirm) {
	print STDERR "${whoami}: Error: -y option given without username!\n";
	&unlockpw;
	exit 1;
    }
}

if (($pw_ent =3D &check_login_name($login_name)) eq '0') {
    print STDERR "${whoami}: Error: User ${login_name} not in password =
database\n";
    &unlockpw;
    exit 1;
}

($name, $password, $uid, $gid, $class, $change, $expire, $gecos, =
$home_dir,
 $shell) =3D split(/:/, $pw_ent);

if ($uid =3D=3D 0) {
    print "${whoami}: Error: I'd rather not remove a user with a uid of =
0.\n";
    &unlockpw;
    exit 1;
}

if (! $affirm) {
    print "Matching password entry:\n\n$pw_ent\n\n";

    $ans =3D &get_yn("Is this the user you wish to Unsuspend? ");

    if ($ans eq 'N') {
	print "${whoami}: Informational: User ${login_name} not =
deactivated.\n";
	&unlockpw;
	exit 0;
    }
}

#
# Copy master password file to new file less removed user's entry

&update_passwd_file;

# All done!

exit 0;

sub check_login_name {
    #
    # Check to see whether login name is in password file
    local($login_name) =3D @_;
    local($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
	  $Mgecos, $Mhome_dir, $Mshell);
    local($i);

    seek(MASTER_PW, 0, 0);
    while ($i =3D <MASTER_PW>) {
	chop $i;
	($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
	 $Mgecos, $Mhome_dir, $Mshell) =3D split(/:/, $i);
	if ($Mname eq $login_name) {
	    seek(MASTER_PW, 0, 0);
	    return($i);		# User is in password database
	}
    }
    seek(MASTER_PW, 0, 0);

    return '0';			# User wasn't found
}

sub get_yn {
    #
    # Get a yes or no answer; return 'Y' or 'N'
    local($prompt) =3D @_;
    local($done, $ans);

    for ($done =3D 0; ! $done; ) {
	print $prompt;
	$ans =3D <>;
	chop $ans;
	$ans =3D~ tr/a-z/A-Z/;
	if (!($ans =3D~ /^[YN]/)) {
	    print STDERR "Please answer (y)es or (n)o.\n";
	} else {
	    $done =3D 1;
	}
    }

    return(substr($ans, 0, 1));
}

sub update_passwd_file {
    local($skipped, $i);

    print STDERR "Updating password file,";
    seek(MASTER_PW, 0, 0);
    open(NEW_PW, ">$new_passwd_file") ||
	die "\n${whoami}: Error: Couldn't open file ${new_passwd_file}:\n =
$!\n";
    chmod(0600, $new_passwd_file) ||
	print STDERR "\n${whoami}: Warning: couldn't set mode of =
$new_passwd_file to 0600 ($!)\n\tcontinuing, but please check mode of =
/etc/master.passwd!\n";
    $skipped =3D 0;

    while ($i =3D <MASTER_PW>) {
	if ($i =3D~ /\n$/) {
        chop $i;
	}
	if ($i ne $pw_ent) {
	    print NEW_PW "$i\n";=20
        } elsif ($i eq $pw_ent) {
          $_ =3D $i;
          s/:\*/:/;
          $i =3D $_; =20
          print NEW_PW "$i\n";
        } else {
	    print STDERR "Dropped entry for $login_name\n" if $debug;
	    $skipped =3D 1;
	}
    }
    close(NEW_PW);
    seek(MASTER_PW, 0, 0);

    if ($skipped =3D=3D 1) {
	print STDERR "\n${whoami}: Whoops! Didn't find ${login_name}'s entry =
second time around!\n";
	unlink($new_passwd_file) ||
	    print STDERR "\n${whoami}: Warning: couldn't unlink =
$new_passwd_file ($!)\n\tPlease investigate, as this file should not be =
left in the filesystem\n";
	&unlockpw;
	exit 1;
    }

    #
    # Run pwd_mkdb to install the updated password files and databases

    print STDERR " updating databases,";
    system('/usr/sbin/pwd_mkdb', '-p', ${new_passwd_file});
    print STDERR " done. Unsuspended\n";

    close(MASTER_PW);		# Not useful anymore
}

------=_NextPart_000_0000_01C131A7.BD4994A0
Content-Type: text/plain;
	name="staruser.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="staruser.txt"

#!/usr/bin/perl

#Edited and modified by David S. Nagrosst April 2 2001
#Orignally Edited/Hacked from the rmuser script
# -*- perl -*-
# Copyright 1995, 1996, 1997 Guy Helmer, Ames, Iowa 50014.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer as
#    the first lines of this file unmodified.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the =
distribution.
# 3. The name of the author may not be used to endorse or promote =
products
#    derived from this software without specific prior written =
permission.
#
# THIS SOFTWARE IS PROVIDED BY GUY HELMER ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED =
WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE =
DISCLAIMED.
# IN NO EVENT SHALL GUY HELMER BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, =
BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF =
USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE =
OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# rmuser - Perl script to remove users
#
# Guy Helmer <ghelmer@cs.iastate.edu>, 02/23/97
#
# $FreeBSD: src/usr.sbin/adduser/rmuser.perl,v 1.8.2.1 2000/03/20 =
13:00:36 peter=20


sub LOCK_SH {0x01;}
sub LOCK_EX {0x02;}
sub LOCK_NB {0x04;}
sub LOCK_UN {0x08;}
sub F_SETFD {2;}

$ENV{"PATH"} =3D "/bin:/sbin:/usr/bin:/usr/sbin";
umask(022);
$whoami =3D $0;
$passwd_file =3D "/etc/master.passwd";
$new_passwd_file =3D "${passwd_file}.new.$$";
$affirm =3D 0;

sub cleanup {
    local($sig) =3D @_;

    print STDERR "Caught signal SIG$sig -- cleaning up.\n";
    &unlockpw;
    if (-e $new_passwd_file) {
	unlink $new_passwd_file;
    }
    exit(0);
}

sub lockpw {
    # Open the password file for reading
    if (!open(MASTER_PW, "$passwd_file")) {
	print STDERR "${whoami}: Error: Couldn't open ${passwd_file}: $!\n";
	exit(1);
    }
    # Set the close-on-exec flag just in case
    fcntl(MASTER_PW, &F_SETFD, 1);
    # Apply an advisory lock the password file
    if (!flock(MASTER_PW, &LOCK_EX|&LOCK_NB)) {
	print STDERR "${whoami}: Error: Couldn't lock ${passwd_file}: $!\n";
	exit(1);
    }
}

sub unlockpw {
    flock(MASTER_PW, &LOCK_UN);
}

$SIG{'INT'} =3D 'cleanup';
$SIG{'QUIT'} =3D 'cleanup';
$SIG{'HUP'} =3D 'cleanup';
$SIG{'TERM'} =3D 'cleanup';

if ($#ARGV =3D=3D 1 && $ARGV[0] eq '-y') {
    shift @ARGV;
    $affirm =3D 1;
}

if ($#ARGV > 0) {
    print STDERR "usage: ${whoami} [-y] [username]\n";
    exit(1);
}

if ($< !=3D 0) {
    print STDERR "${whoami}: Error: you must be root to use =
${whoami}\n";
    exit(1);
}

&lockpw;

if ($#ARGV =3D=3D 0) {
    # Username was given as a parameter
    $login_name =3D pop(@ARGV);
    die "Sorry, login name must contain alphanumeric characters only.\n"
	if ($login_name !~ /^[a-zA-Z0-9_]\w*$/);
} else {
    if ($affirm) {
	print STDERR "${whoami}: Error: -y option given without username!\n";
	&unlockpw;
	exit 1;
    }
}

if (($pw_ent =3D &check_login_name($login_name)) eq '0') {
    print STDERR "${whoami}: Error: User ${login_name} not in password =
database\n";
    &unlockpw;
    exit 1;
}

($name, $password, $uid, $gid, $class, $change, $expire, $gecos, =
$home_dir,
 $shell) =3D split(/:/, $pw_ent);

if ($uid =3D=3D 0) {
    print "${whoami}: Error: I'd rather not remove a user with a uid of =
0.\n";
    &unlockpw;
    exit 1;
}

$checkstar=3D$password;

$checkstar=3D substr($checkstar,0,1);

if ($checkstar eq "*") {=20
    die "Sorry, that account has already been starred.\n"  }=20

if (! $affirm) {
    print "Matching password entry:\n\n$pw_ent\n\n";

    $ans =3D &get_yn("Is this the User you wish to Suspend? ");

    if ($ans eq 'N') {
	print "${whoami}: Informational: User ${login_name} not =
deactivated.\n";
	&unlockpw;
	exit 0;
    }
}

#
# Copy master password file to new file less removed user's entry

&update_passwd_file;

# All done!

exit 0;

sub check_login_name {
    #
    # Check to see whether login name is in password file
    local($login_name) =3D @_;
    local($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
	  $Mgecos, $Mhome_dir, $Mshell);
    local($i);

    seek(MASTER_PW, 0, 0);
    while ($i =3D <MASTER_PW>) {
	chop $i;
	($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
	 $Mgecos, $Mhome_dir, $Mshell) =3D split(/:/, $i);
	if ($Mname eq $login_name) {
	    seek(MASTER_PW, 0, 0);
	    return($i);		# User is in password database
	}
    }
    seek(MASTER_PW, 0, 0);

    return '0';			# User wasn't found
}

sub get_yn {
    #
    # Get a yes or no answer; return 'Y' or 'N'
    local($prompt) =3D @_;
    local($done, $ans);

    for ($done =3D 0; ! $done; ) {
	print $prompt;
	$ans =3D <>;
	chop $ans;
	$ans =3D~ tr/a-z/A-Z/;
	if (!($ans =3D~ /^[YN]/)) {
	    print STDERR "Please answer (y)es or (n)o.\n";
	} else {
	    $done =3D 1;
	}
    }

    return(substr($ans, 0, 1));
}

sub update_passwd_file {
    local($skipped, $i);

    print STDERR "Updating password file,";
    seek(MASTER_PW, 0, 0);
    open(NEW_PW, ">$new_passwd_file") ||
	die "\n${whoami}: Error: Couldn't open file ${new_passwd_file}:\n =
$!\n";
    chmod(0600, $new_passwd_file) ||
	print STDERR "\n${whoami}: Warning: couldn't set mode of =
$new_passwd_file to 0600 ($!)\n\tcontinuing, but please check mode of =
/etc/master.passwd!\n";
    $skipped =3D 0;

    while ($i =3D <MASTER_PW>) {
	if ($i =3D~ /\n$/) {
        chop $i;
	}
	if ($i ne $pw_ent) {
	    print NEW_PW "$i\n";=20
        } elsif ($i eq $pw_ent) {
          $_ =3D $i;
          s/:/:*/;
          $i =3D $_; =20
          print NEW_PW "$i\n";
        } else {
	    print STDERR "Dropped entry for $login_name\n" if $debug;
	    $skipped =3D 1;
	}
    }
    close(NEW_PW);
    seek(MASTER_PW, 0, 0);

    if ($skipped =3D=3D 1) {
	print STDERR "\n${whoami}: Whoops! Didn't find ${login_name}'s entry =
second time around!\n";
	unlink($new_passwd_file) ||
	    print STDERR "\n${whoami}: Warning: couldn't unlink =
$new_passwd_file ($!)\n\tPlease investigate, as this file should not be =
left in the filesystem\n";
	&unlockpw;
	exit 1;
    }

    #
    # Run pwd_mkdb to install the updated password files and databases

    print STDERR " updating databases,";
    system('/usr/sbin/pwd_mkdb', '-p', ${new_passwd_file});
    print STDERR " done. Suspended \n";

    close(MASTER_PW);		# Not useful anymore
}

------=_NextPart_000_0000_01C131A7.BD4994A0--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?NFBBKNOFBCDIJEPNPDDKKEELCAAA.nagrosst>