Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
another adduser script..this one adds users with dots &
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Messiah
Tux's lil' helper
Tux's lil' helper


Joined: 30 Apr 2002
Posts: 139

PostPosted: Sun Jun 30, 2002 11:58 pm    Post subject: another adduser script..this one adds users with dots & Reply with quote

I wrote this script (in perl) because I needed to add users with a dot in their username. Also, I always use separate groups per user, so there is a group created with the same name as the username. Even quotas are supported.

This script will add users angroups beginning at uid 2000.

Below are the headers of the script.

Code:

#!/usr/bin/perl
########################################################################
#       Qweb Internet Services         |           info@qweb.nl        #
########################################################################
# Please feel free to use, modify and distribute this script freely.   #
# Please do NOT remove this header!!                                   #
########################################################################
# If you make any changes in this script, please mail a copy with      #
# comments to the e-mailaddress mentioned above.                       #
########################################################################
#                                    adduser                           #
########################################################################
# This script will let you add a user on a Gentoo Linux system (or     #
# any other system that uses /etc/passwd, /etc/shadow and /etc/group)  #
# with a dot (.) in the username. It will also make a group with the   #
# same name as the username, make a home-dir for the user, and copy    #
# a skeleton directory.                                                #
########################################################################
# v0.1 - Initial version                                               #
########################################################################

# First, we have some global variables
#
# The skeleton directory to use
# /etc/skel is often used as a standard
$skeldir = "/etc/skel";

# Master home dir containing the home-directories of all users
# /home is a good one and is standard
$masterhome = "/home";

# Which shell?
$shell = "/bin/bash";

# Use quotas? This will need you to have quotatool
# (http://devel.duluoz.net/quotatool/) installed!
# Must be the path to quotatool, or it won´t be used!
$quotatool = "/usr/local/bin/quotatool";
# On what partition we have quotas?
$quotafilesys = "/";


[edit]
ermm...no option to include an attachment, so I will provide a link:
http://mustang.qweb.nl/scripts/adduser
Back to top
View user's profile Send private message
AutoBot
l33t
l33t


Joined: 22 Apr 2002
Posts: 968
Location: Usually Out

PostPosted: Tue Jul 02, 2002 7:49 am    Post subject: Reply with quote

You could just include the perl script in [code].
_________________
This message self destructed a long time ago.
Back to top
View user's profile Send private message
Messiah
Tux's lil' helper
Tux's lil' helper


Joined: 30 Apr 2002
Posts: 139

PostPosted: Thu Jul 04, 2002 9:43 pm    Post subject: Reply with quote

I even start to love my own script even more. It is even possible to add users with usernames beginning with a number. With the traditional adduse and useradd scripts found on many distributions one cannot do that too.

Actually, this script has helped myself more than I ever could think of. I can't imagine adding users all day long by hand in the config files ;-)
Back to top
View user's profile Send private message
Black666
n00b
n00b


Joined: 15 Jul 2002
Posts: 61
Location: Vienna (Austria)

PostPosted: Wed Jul 24, 2002 9:03 am    Post subject: Reply with quote

Thnx, looked for that one!!

Just one quick question: Is it necessary to have a group with the same name as the user? I've looked through my /etc/group and didn't find any groups matching my username..
Back to top
View user's profile Send private message
Black666
n00b
n00b


Joined: 15 Jul 2002
Posts: 61
Location: Vienna (Austria)

PostPosted: Wed Jul 24, 2002 9:21 am    Post subject: Reply with quote

Tried your script...works fine.
But what would I need to change if I want my users to be in the group "users" and not in the group "john.smith" for example ...
Back to top
View user's profile Send private message
Black666
n00b
n00b


Joined: 15 Jul 2002
Posts: 61
Location: Vienna (Austria)

PostPosted: Wed Jul 24, 2002 9:45 am    Post subject: Reply with quote

Got it working.

I only have some basic bash scripting skills but if you read the perl script often enough you'll be able to understand what it does :D
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Wed Jul 24, 2002 5:38 pm    Post subject: Reply with quote

Redhat does this. Gentoo does not. It is not going to be a problem either way under Gentoo.
Black666 wrote:
Thnx, looked for that one!!

Just one quick question: Is it necessary to have a group with the same name as the user? I've looked through my /etc/group and didn't find any groups matching my username..
Back to top
View user's profile Send private message
atom
Tux's lil' helper
Tux's lil' helper


Joined: 25 Nov 2002
Posts: 122
Location: Columbus, Ohio, USA

PostPosted: Tue Apr 13, 2004 9:18 pm    Post subject: Reply with quote

well, since nobody else has added it to this thread i figured i will before the script disappears from the site.

Code:
#!/usr/bin/perl
########################################################################
#       Qweb Internet Services         |           info@qweb.nl        #
########################################################################
# Please feel free to use, modify and distribute this script freely.   #
# Please do NOT remove this header!!                                   #
########################################################################
# If you make any changes in this script, please mail a copy with      #
# comments to the e-mailaddress mentioned above.                       #
########################################################################
#                                    adduser                           #
########################################################################
# This script will let you add a user on a Gentoo Linux system (or     #
# any other system that uses /etc/passwd, /etc/shadow and /etc/group)  #
# with a dot (.) in the username. It will also make a group with the   #
# same name as the username, make a home-dir for the user, and copy    #
# a skeleton directory.                                                #
########################################################################
# v0.1 - Initial version                                               #
########################################################################

# First, we have some global variables
#
# The skeleton directory to use
# /etc/skel is often used as a standard
$skeldir = "/etc/skel";

# Master home dir containing the home-directories of all users
# /home is a good one and is standard
$masterhome = "/home";

# Which shell?
$shell = "/bin/bash";

# Use quotas? This will need you to have quotatool
# (http://devel.duluoz.net/quotatool/) installed!
# Must be the path to quotatool, or it won´t be used!
$quotatool = "/usr/local/bin/quotatool";
# On what partition we have quotas?
$quotafilesys = "/";

unless (@ARGV == 2 || @ARGV == 3) {
        die <<EOF
Usage: $0 username password (#size)

Where username is the user you want to add and
password is the password for the user and
optional #size is limit of quota
EOF
;
}

$username=$ARGV[0];
$password=$ARGV[1];
if ($ARGV[2]) { $size=$ARGV[2]; }

$uidtouse = uidtouse();
$gidtouse = gidtouse();

open(D,">>/etc/group") ||  die "Couldn't open the group file, $!\n";
print D "$username:x:$gidtouse:\n";
close(D) || die "$0: Couldn't close the group file, $!\n";

open(D,">>/etc/passwd") ||  die "Couldn't open the passwd file, $!\n";
print D "$username:x:$uidtouse:$gidtouse:\"$username\":$masterhome/$username:$shell\n";
close(D) || die "$0: Couldn't close the passwd file, $!\n";

open(D,">>/etc/shadow") ||  die "Couldn't open the shadow file, $!\n";
print D "$username:!:11868:0:99999:7:::\n";
close(D) || die "$0: Couldn't close the shadow file, $!\n";

$junk=`echo $username:$password |chpasswd`;

$junk = `mkdir $masterhome/$username`;
$junk = `mkdir $masterhome/$username/tmp`;
copyskeldir();
$junk = `chown -R $username:$username $masterhome/$username`;
$junk=`chmod -R 755 $masterhome/$username`;
$junk=`chmod 750 $masterhome/$username`;
$junk=`chmod 700 $masterhome/$username/tmp`;
               
if ($size>0 && -e "$quotatool")
{
  $cmd = "$quotatool -u $uidtouse -bq $size"."M -l $size"."M $quotafilesys";
  $junk = `$cmd`;
  $cmd = "$quotatool -g $gidtouse -bq $size"."M -l $size"."M $quotafilesys";
  $junk = `$cmd`;
}

sub uidtouse {
# This function will get the last uid that was used out of /etc/passwd

my $uid;
my %hash;

while ( ( undef, undef, $uid ) = getpwent() ) {
    if ( $uid >= 2000 && $uid <= 3000 ) {
        $hash{ $uid } = 1;
        }
    }

my @keys = sort {$a <=> $b} keys %hash;

for ( $keys[0] .. $keys[-1] + 1 ) {
    unless ( exists $hash{ $_ } ) {
        $z = $_;
        if ($z eq "1" || $z eq "0") { $z = "2000"; }
        return $z;
        last;
        }
    }
}

sub gidtouse {
# This function will get the last uid that was used out of /etc/passwd

my $gid;
my %hash2;

while ( ( undef, undef, $gid ) = getgrent() ) {
    if ( $gid >= 2000 && $gid <= 3000 ) {
        $hash2{ $gid } = 1;
        }
    }

my @keys2 = sort {$a <=> $b} keys %hash2;

for ( $keys2[0] .. $keys2[-1] + 1 ) {
    unless ( exists $hash2{ $_ } ) {
        $y = $_;
        if ($y eq "1" || $y eq "0") { $y = "2000"; }
        return $y;
        last;
        }
    }
}

sub copyskeldir()
{
opendir(DIR,$skeldir);
@dir = readdir(DIR);
closedir(DIR);
for (my $i = 2; $i < @dir; $i++) {
 if (-f $skeldir."/".$dir[$i]) { $junk = `cp -f $skeldir/$dir[$i] $masterhome/$username/`; }
 else { `cp -Rf $skeldir/$dir[$i] $masterhome/$username/`; }
}
}
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum