script to add users to unix & samba simultaneously

Nick Kidd nkidd at indiana.edu
Fri Jul 21 15:53:53 GMT 2000


if you know perl, you can edit this to suit your needs.  just change $user
to come from STDIN and create a var $passwd to come from STDIN.  where you
see the command $adduser, create one that also adds a samba user.  my
script works this way b/c all users are on the PDC and do not need a samba
account.  the script looks this way b/c it was intentionally written to be
run suid root and not by root.  you can also change this but it suits my
needs and i dont want to muck w/ it anymore.

 nick

note: it uses suidperl, the script is owned by root.
------------------------------------------------------------
#!/usr/bin/suidperl

use English;
use strict 'subs';


# Set Insecure ENV values and the location of useradd command.
$ENV{'PATH'} = '/bin:/usr/bin';
$ENV{'BASH_ENV'} = '/home/nkidd/.bashrc';
$adduser = '/usr/sbin/useradd';

# Get the user name from samba.
chomp ($user = $ARGV[0]);

if ($user =~ /(\w+)/) {
        $user = $1;
} else {
        die "Bad username in $user";
}

my $sleep_count = 0;

do {
        $pid = open(KID, "-|");
        unless (defined $pid) {
                warn "cannot fork: $!";
                die "bailing out" if $sleep_count++ > 6;
                sleep 10;
        }
} until defined $pid;

$flag = 1;

if ($pid) {  #parent
        while (<KID>) {
                if (!($_ eq "found")) {
                        # ADD USER HERE.
                        system "$adduser -G samba_user $user";
                        $flag = 0;
                        system "echo $user added >> /tmp/userlog";
                }
        }
        close (KID) or warn "kid exited $?";
}
else {  # child
        $iflag = 1;
        ($EUID, $EGID) = ($UID, $GID);  # suid progs only
        open (PASSWD, "</etc/passwd");
        while (<PASSWD>) {
                split /:/,$_;
                if ($_[0] eq $user) {
                        print "found";
                        $iflag = 0;
                        last;
                }
        }
        if ($iflag) { print "nouser"; }
        close(PASSWD) or warn "cannot close /etc/passwd $! $?";
        exit;   #dont forget this.
}


On Fri, 21 Jul 2000, Damen Rund wrote:

> Has anyone written a script to add a new user to linux and samba
> simultaneously?
> 
> I wish I knew more about programming but I figure if the script asks for
> the username and password it could then pass this information to the
> appropriate commands in linux to add the user to linux and samba, no?
> 
> Damen C. Rund
> Computer Service Technician
> 
> Miss Porter's School, Inc.
> Main Street
> Farmington, CT. 06062
> 
> Phone (860) 409-3607
> Fax (509) 561-5881
> E-Mail Damen_Rund at missporters.org
> Internet www.missporters.org
>  
> 
> 
> 





More information about the samba-technical mailing list