mkacct.in 8.56 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use English;
use Getopt::Std;

#
# Create a user account. All this script does is create the account
# if it does not exist, or update the password and gecos fields.
# No groups processing is done here. The initial account is created in
# the "guest" group; use the setgroups command to set a users groups.
#
# usage: mkacct <userid>
#
# XXX - /users wired in. 
# 
sub usage()
{
17
    print STDOUT "Usage: mkacct <name>\n";
18 19 20 21 22 23 24 25 26 27 28 29
    exit(-1);
}
my  $optlist = "";

#
# Configure variables
#
my $TB      = "@prefix@";
my $TBOPS   = "@TBOPSEMAIL@";
my $CONTROL = "@USERNODE@";

my $HOMEDIR = "/users";
30
my $USERPATH= "$TB/bin";
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
my $PBAG    = "$TB/sbin/paperbag";
my $SSH     = "$TB/bin/sshtb";
my $USERADD = "/usr/sbin/pw useradd";
my $USERMOD = "/usr/sbin/pw usermod";
my $CHPASS  = "/usr/bin/chpass";
my $KEYGEN  = "/usr/bin/ssh-keygen";
my $SETGROUPS = "$TB/sbin/setgroups";
my $GENELISTS = "$TB/sbin/genelists";

my $user;
my @db_row;
my $query_result;

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be setuid! Maybe its a development version?\n");
}

#
# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
# 
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 1) {
    usage();
}
$user = $ARGV[0];

#
# Untaint the argument.
#
if ($user =~ /^([a-z0-9]+)$/i) {
    $user = $1;
}
else {
    die("Invalid uid '$user' contains illegal characters.\n");
}

#
# This script always does the right thing, but we prefer that mere users
# do not run it, except when its for themselves. Otherwise, make sure that
# user has group/project root in at least one project, which indicates they
# have some level of responsibility.
#
if (!TBAdmin($UID)) {
    my $dbuid;

    if (! UNIX2DBUID($UID, \$dbuid)) {
	die("You are not a valid emulab user!\n");
    }
    if ($dbuid ne $user) {
	#
	# Check if group_root/project_root anyplace.
	#
	$query_result =
	    DBQueryFatal("select trust from group_membership ".
			 "where uid='$dbuid' and ".
			 "trust='project_root' or trust='group_root'");

	if ($query_result->numrows == 0) {
	    die("$0: You do not have permission to run this script!\n");
	}
    }
}

#
# Get the user info (the user being created). This join picks out the
# user's earliest project membership to use for the default group.
#
$query_result =
134 135
    DBQueryFatal("select u.usr_pswd,u.unix_uid,u.usr_name, ".
		 " u.usr_email,u.home_pubkey,m.pid ".
136 137 138 139 140 141 142 143 144 145 146 147 148
		 " from users as u ".
		 "left join group_membership as m ".
		 " on u.uid=m.uid and m.pid=m.gid ".
		 "where u.uid='$user' order by date_approved asc limit 1");

if ($query_result->numrows == 0) {
    fatal("$user is not in the DB. This is bad.\n");
}
@db_row         = $query_result->fetchrow_array();
my $pswd        = $db_row[0];
my $user_number = $db_row[1]; 
my $fullname    = $db_row[2];
my $user_email  = $db_row[3];
149 150
my $user_pubkey = $db_row[4];
my $defpid      = $db_row[5];
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

#
# Unix info for users default group.
#
my $default_groupname;
my $default_groupgid;

if (! TBGroupUnixInfo($defpid, $defpid,
		      \$default_groupgid, \$default_groupname)) {
    fatal("No info for default project $defpid!");
}

#
# Note hardwired control node. 
# 
my $control_node = $CONTROL;

#
# All this stuff must be done as root (ssh).
#
$UID = $EUID;

#
# Run genelists to update the email lists. This is a convenient
# spot to do this. Errors are non-fatal; the testbed list will
# will find out about problems via email from genelists. Note that
# this command must be run when EUID==UID==0 because its a setuid
# PERL script.
#
180
system("$GENELISTS -n $user");
181 182 183 184 185 186 187 188 189 190 191 192 193

#
# Make user on local. We don't give them a password since they are not
# allowed to log in, except via paperbag. 
#
if (system("egrep -q -s '^${user}:' /etc/passwd")) {
    print "Adding user $user ($user_number) to local node.\n";

    if (system("$USERADD $user -u $user_number -c \"$fullname\" ".
	       "-k /usr/share/skel -m -d $HOMEDIR/$user ".
	       "-g $default_groupname -s $PBAG")) {
	fatal("Could not add user $user to local node.");
    }
194 195 196 197
    #
    # First time account is made, do some extra work. 
    #
    FirstTime();
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}
else {
    print "Updating user $user ($user_number) on local node.\n";

    #
    # MAKE SURE not to update anything else!
    #
    if (system("$USERMOD $user -c \"$fullname\" ")) {
	fatal("Could not modify user $user on local node.");
    }
}

#
# Make user account on control node. We do the password setup as separate
# step since thats easier than trying to do both via ssh.
#
# Quote special chars for ssh and the shell on the other side
#
$fullname =~ s/\"/\'/g;
$fullname =~ s/([^\\])([\'\"\(\)])/$1\\$2/g;

if (system("$SSH $control_node egrep -q -s '^${user}:' /etc/passwd")) {
    print "Adding user $user ($user_number) to $control_node.\n";

    if (system("$SSH $control_node ".
	       "'$USERADD $user -u $user_number -c \"$fullname\" ".
	       "-k /usr/share/skel -m -d $HOMEDIR/$user ".
	       "-g $default_groupname -s /bin/tcsh'")) {
	fatal("Could not add user $user ($user_number) to $control_node.\n");
    }
}
else {
    print "Updating user $user ($user_number) on $control_node.\n";

    #
    # MAKE SURE not to update anything else!
    #
    if (system("$SSH $control_node ".
	       "'$USERMOD $user -c \"$fullname\"'")) {
	fatal("Could not modify user $user record on $control_node.");
    }
}

print "Updating user $user password on $control_node.\n";

if (system("$SSH $control_node $CHPASS -p $pswd $user")) {
    fatal("Could not change password for user $user on $control_node.\n");
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
#
# Update the DB with the users public key.
#
if (-e "$HOMEDIR/$user/.ssh/identity.pub" ) {
    my $key = `cat $HOMEDIR/$user/.ssh/identity.pub`;

    if ($key =~ /^([-\@\w\s\.]+)$/) {
	$key = $1;
    }
    else {
	fatal("Bad public key: $key");
    }
    chomp $key;

    DBQueryFatal("update users set emulab_pubkey='$key' where uid='$user'");
}

264 265 266 267 268 269 270 271 272
exit(0);

sub fatal {
    local($msg) = $_[0];

    SENDMAIL($TBOPS, "mkacct Failed", $msg);
    die("$0: $msg\n");
}

273
#
274
# Do some new account stuff.
275
#
276 277 278 279 280 281 282 283 284
sub FirstTime()
{
    my $dossh = 0;
    
    #
    # Set up the ssh key, but only if not done so already.
    #
    if (! -e "$HOMEDIR/$user/.ssh/" ) {
	print "Setting up ssh configuration for $user.\n";
285
    
286 287 288 289 290 291 292
	mkdir("$HOMEDIR/$user/.ssh", 0700) or
	    fatal("Could not mkdir $HOMEDIR/$user/.ssh: $!");
	chown($user_number, $default_groupgid, "$HOMEDIR/$user/.ssh") or
	    fatal("Could not chown $HOMEDIR/$user/.ssh: $!");
	
	$dossh = 1;
    }
293
    
294 295 296 297 298 299 300 301 302 303 304 305
    #
    # The rest of this needs to be done as the user, so fork a child.
    #
    $mypid = fork();
    if ($mypid) {
	waitpid($mypid, 0);
	if ($?) {
	    exit($? >> 0);
	}
	return;
    }

306 307 308
    $EUID = $user_number;
    $UID  = $EUID;

309 310 311 312 313 314 315 316 317
    if ($dossh) {
	if (system("$KEYGEN -P '' -f $HOMEDIR/$user/.ssh/identity")) {
	    fatal("Failure in ssh-keygen");
	}
	if (system("/bin/cp $HOMEDIR/$user/.ssh/identity.pub ".
		   "$HOMEDIR/$user/.ssh/authorized_keys")) {
	    fatal("Copying over $HOMEDIR/$user/.ssh/identity.pub ".
		  "to authorized_keys");
	}
318 319 320 321
	if (defined($user_pubkey)) {
	    system("echo \"$user_pubkey\" >> ".
		   "$HOMEDIR/$user/.ssh/authorized_keys");
	}
322 323
	chmod(0600, "$HOMEDIR/$user/.ssh/authorized_keys") or
	    fatal("Could not chmod $HOMEDIR/$user/.ssh/authorized_keys: $!");
324 325
    }

326 327 328 329 330
    #
    # Set up a .forward file so that any email to them gets forwarded off.
    #
    if (! -e "$HOMEDIR/$user/.forward" ) {
	print "Setting up .forward file for $user.\n";
331

332 333 334 335 336
	if (system("echo \"$user_email\" > $HOMEDIR/$user/.forward")) {
	    fatal("Could not create $HOMEDIR/$user/.forward");
	}
	chmod(0644, "$HOMEDIR/$user/.forward") or
	    fatal("Could not chmod $HOMEDIR/$user/.forward");
337 338
    }

339 340 341 342 343 344 345 346
    #
    # Add testbed path to .cshrc and .profile.
    #
    my $cpathstr = "set path = ($USERPATH \$path)";
    if (-e "$HOMEDIR/$user/.cshrc" &&
	system("egrep -q -s '$USERPATH' $HOMEDIR/$user/.cshrc")) {
	system("echo '$cpathstr' >> $HOMEDIR/$user/.cshrc");
    }
347

348 349 350 351 352
    my $spathstr = "PATH=$USERPATH:\$PATH";
    if (-e "$HOMEDIR/$user/.profile" &&
	system("egrep -q -s '$USERPATH' $HOMEDIR/$user/.profile")) {
	system("echo '$spathstr' >> $HOMEDIR/$user/.profile");
    }
353

354
    exit(0);
355
}