elabinelab_adminusers.in 3.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use English;
use Getopt::Std;

#
# ElabInElab: This is run on the inner boss to add admin users.
#
sub usage()
{
    print STDERR "Usage: $0 [-d]\n";
    exit(1);
}
my $optlist = "d";
my $debug   = 0;
sub mysystem($);
sub fatal($);

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $ELABINELAB  = @ELABINELAB@;
my $SAVEUID     = $UID;
my $tmpdir      = "/tmp/users.$$";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Turn off line buffering on output
$| = 1;

use lib "@prefix@/lib";
use libdb;
use libtestbed;
use User;
use Project;
use Group;

# Defined in libdb ...
my $TBOPSPID    = TBOPSPID();

if (!$ELABINELAB) {
    fatal("This script can only run on an inner Emulab!");
}
# Only admin types!
if (!TBAdmin($UID)) {
    fatal("Only TB administrators can run this script!");
}

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}

usage()
    if (scalar(@ARGV));

my $project = Project->Lookup($TBOPSPID);
if (!defined($project)) {
    fatal("Could not lookup project $TBOPSPID");
}
my $group = $project->GetProjectGroup();

#
# We know the xml files are in a tar file in the "stuff" directory,
# which was put there by rc.mkelab.
#
mysystem("mkdir $tmpdir");
mysystem("tar xzf $TB/stuff/users.tar.gz -C $tmpdir");

opendir(DIR, $tmpdir) or
    fatal("Cannot opendir $tmpdir: $!");
my @files = grep { $_ ne "." && $_ ne ".." } readdir(DIR);
closedir(DIR);

foreach my $file (@files) {
    my $uid;
    
    if ($file =~ /^([-\w]+)\.xml$/) {
	$uid = $1;
    }
    else {
	fatal("Odd file file $file in $tmpdir");
    }
    mysystem("$TB/sbin/newuser -s -r -P $tmpdir/$file");
    my $user = User->Lookup($uid);
    if (!defined($user)) {
	fatal("Could not look of $uid after newuser");
    }
    $user->SetStatus(USERSTATUS_ACTIVE());
    $user->Update({"admin" => 1});
    mysystem("$TB/sbin/tbacct -s -b add $uid");
    # This switches the shell from the paperbag.
    mysystem("$TB/sbin/tbacct -s -b mod $uid");

    $group->AddMemberShip($user, $Group::MemberShip::TRUSTSTRING_GROUPROOT)
	== 0 or fatal("Could not add $uid to $TBOPSPID");

    mysystem("$TB/sbin/setgroups $uid");
}
mysystem("$TB/sbin/genelists -a");
exit(0);

#
# Run a command string.
#
sub mysystem($)
{
    my ($command) = @_;

    if ($debug) {
	print "Command: '$command\'\n";
    }

    system($command);
    if ($?) {
	die("*** $0:\n".
	    "    Command failed: $? - $command\n");
    }
}

sub fatal($)
{
    my ($mesg) = @_;

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}