setgroups.in 11.7 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3

#
4
# Copyright (c) 2000-2013 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
#

26 27 28 29 30 31 32 33 34 35 36
use English;
use Getopt::Std;

#
# Set groups for users. With just a pid all the users in the group
# are modified. Of course, since we might be removing groups, we actuall
# have to go through the entire set of users in the project. Hence, you
# can provide an optional list of users to operate on; the web interface
# uses this option since it know what users have been changed via the web
# form.
#
37
# Note that this script does not create accounts or groups. That should
38 39 40 41 42
# already have been done with other scripts.
#
sub usage()
{
    print STDOUT
43 44
	"Usage: setgroups -p <pid> [user ...]\n".
        "       setgroups [user ...]\n";
45

46 47
    exit(-1);
}
48 49 50

sub fatal($);

51 52 53
my $optlist = "dp:";
my $debug   = 0;
my $optarg  = "";
54 55 56 57 58 59 60 61

#
# Configure variables
#
my $TB      = "@prefix@";
my $TBOPS   = "@TBOPSEMAIL@";
my $TBLOGS  = "@TBLOGSEMAIL@";
my $CONTROL = "@USERNODE@";
62
my $BOSSNODE= "@BOSSNODE@";
63
my $ADMINGRP= "@TBADMINGROUP@";
64
my $ELABINELAB    = @ELABINELAB@;
65
my $WIKISUPPORT   = @WIKISUPPORT@;
66
my $BUGDBSUPPORT  = @BUGDBSUPPORT@;
67
my $OPSDBSUPPORT  = @OPSDBSUPPORT@;
68
my $SETWIKIGROUPS = "$TB/sbin/setwikigroups";
69
my $SETBUGDBGROUPS= "$TB/sbin/setbugdbgroups";
70
my $OPSDBCONTROL  = "$TB/sbin/opsdb_control";
71 72 73

my $SSH     = "$TB/bin/sshtb";
my $USERMOD = "/usr/sbin/pw usermod";
74
my $CHOWN   = "/usr/sbin/chown";
75
my $SAVEUID = $UID;
76 77

my $dbuid;
78
my @userlist = ();
79
my @modusers = ();
80
my $pid;
81 82 83 84 85
my $logname;
my @db_row;
my $query_result;

#
86 87
# Note hardwired control node.
#
88 89 90 91
my $control_node = $CONTROL;

#
# Untaint the path
92
#
93 94 95 96 97 98 99 100 101
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
102
# Load the Testbed support stuff.
103 104
#
use lib "@prefix@/lib";
105
use libaudit;
106 107
use libdb;
use libtestbed;
108
use User;
109 110

#
111
# We do not want to run this script unless its the real version.
112 113 114 115 116 117 118 119 120
#
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.
121
#
122 123 124 125 126 127 128 129 130 131 132 133 134
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
135 136 137 138
if (defined($options{"d"})) {
    $debug  = 1;
    $optarg = "-d";
}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
if (defined($options{"p"})) {
    $pid = $options{"p"};

    #
    # Untaint,
    #
    if ($pid =~ /^([-\@\w]+)$/) {
	$pid = $1;
    }
    else {
	die("Bad data in pid: $pid.");
    }
}

#
# See if a userlist was provided. This is an optimization. The web interface
# knows which users actually changed, so its quicker to modify that set
# instead of the entire project set.
#
if (@ARGV) {
    # Untaint the users.
    foreach my $user ( @ARGV ) {
	if ($user =~ /^([\w]+)$/) {
	    $user = $1;
	}
	else {
	    die("Bad user name: $user.");
	}
167

168 169 170 171
	push(@userlist, $user);
    }
}

172
if (!defined($pid) && !scalar(@userlist)) {
173 174 175
    usage();
}

176 177 178 179
# Map invoking user to object.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
180
}
181 182
my $user_name  = $this_user->name();
my $user_email = $this_user->email();
183 184 185

#
# This script always does the right thing, so it does not matter who
186
# calls it.
187
#
188 189 190
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
191
    #
192
    # Parent exits normally
193
    #
194
    exit(0);
195 196
}

197
#
198 199 200 201 202 203 204 205
# All this stuff must be done as root (ssh).
#
$UID = $EUID;

#
# If no user list provided, we have to do this for the entire project
# member list since we have no idea who got changed.
#
206
if (! scalar(@userlist)) {
207
    $query_result =
208
	DBQueryFatal("select uid_idx from group_membership ".
209 210 211 212 213 214 215 216 217 218 219 220 221
		     "where pid='$pid' and pid=gid");

    $query_result->numrows ||
	fatal("No project members for $pid!\n");

    while (@db_row = $query_result->fetchrow_array() ) {
	push(@userlist, $db_row[0]);
    }
}

#
# Loop through user set, building up the group set and issuing commands.
#
222
foreach my $token (@userlist) {
223 224 225 226 227
    my @groupnames;
    my @grouplist;
    my $groupargument;
    my $project;

228 229
    my $user = User->Lookup($token);
    fatal("Could not map user $token to object")
230 231
	if (!defined($user));
    my $uid_idx = $user->uid_idx();
232
    my $uid     = $user->uid();
233 234

    if ($user->webonly()) {
235 236 237
	print "Skipping $uid; webonly account!\n";
	next;
    }
238 239 240 241 242 243
    if ($user->IsNonLocal()) {
	print "Skipping $uid; nonlocal account!\n";
	next;
    }
    # want to skip nonlocal/webonly in second loop below.
    push(@modusers, $user);
244

245 246 247
    #
    # Form a list of project (group) membership names. We do this in two
    # steps to ensure that we get the default group membership since we
248
    # want that to be the users primary group. Not sure this really matters
249 250
    # all that much, but might as well.
    #
251 252 253
    $query_result =
	DBQueryFatal("select g.unix_name from group_membership as m ".
		     "left join groups as g on m.pid=g.pid and m.gid=g.gid ".
254 255
		     "where m.uid_idx='$uid_idx' and m.pid=m.gid and ".
		     "      m.trust!='none'");
256

257 258
    if (!$query_result->numrows) {
	#
259 260 261
	# See if an active user with no project membership. If so, then
	# set groups to just the guest group. If not active, skip
	# (non-fatal) since there can be group members not approved,
262
	# and this is called from the editgroups web page.
263
	#
264 265
	if ($user->status() ne USERSTATUS_ACTIVE()) {
	    print "Skipping $uid; not an active user yet!\n";
266 267 268
	    next;
	}
	push(@groupnames, "guest");
269
	goto nogroups;
270
    }
271 272
    else {
	while (@db_row = $query_result->fetchrow_array() ) {
273 274 275 276 277 278
	    my $groupname = $db_row[0];

	    next
		if ($ELABINELAB && !getgrnam($groupname));

	    push(@groupnames, $groupname);
279
	}
280 281
    }

282 283
    #
    # Okay, pick up subgroup (pid!=gid) membership.
284
    #
285 286 287
    $query_result =
	DBQueryFatal("select g.unix_name from group_membership as m ".
		     "left join groups as g on m.pid=g.pid and m.gid=g.gid ".
288 289
		     "where m.uid_idx='$uid_idx' and m.pid!=m.gid and ".
		     "      m.trust!='none'");
290 291

    while (@db_row = $query_result->fetchrow_array() ) {
Leigh B. Stoller's avatar
Ditto  
Leigh B. Stoller committed
292 293 294 295 296 297
	    my $groupname = $db_row[0];

	    next
		if ($ELABINELAB && !getgrnam($groupname));

	    push(@groupnames, $groupname);
298 299
    }

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    if (0) {
    #
    # Okay, this join looks for project/group roots in the pid, and finds
    # any subgroups they are not members of. The intent is that project
    # and group roots in the pid, should be able to read files in the
    # subgroups, even if they are not actual members of the group in the DB.
    # The reason they do not want to be actual members in the DB is cause
    # we do *not* want to export home dirs to experimental nodes, or otherwise
    # hand out sensitive info.
    #
    # XXX Not in use cause NGROUPS=16. Too Low!
    #
    $query_result =
	DBQueryFatal("select distinct gn.pid,gn.gid,g.unix_name ".
		     "  from group_membership as gp ".
		     "left join group_membership as gn on ".
		     "     gn.pid=gp.pid and gn.pid!=gn.gid ".
		     "left join group_membership as go on go.uid=gp.uid and ".
		     "     go.pid=gn.pid and go.gid=gn.gid ".
		     "left join groups as g on gn.pid=g.pid and gn.gid=g.gid ".
		     "where go.uid is null and gn.uid is not null and ".
		     "      gp.uid='$uid' and gp.pid=gp.gid and ".
		     "      (gp.trust='group_root' or ".
		     "       gp.trust='project_root')");

    while (@db_row = $query_result->fetchrow_array() ) {
	print "Would also add $uid to $db_row[0]/$db_row[1]\n";
#	push(@groupnames, $db_row[0]);
    }
    }
330

331 332
  nogroups:
    print "Processing user $uid: @groupnames\n";
333 334 335 336 337 338 339 340 341 342 343 344
    #
    # Construct an appropriate group list for the pw commands. Main project
    # is the first on the list, and that becomes the primary group. The rest
    # (if any) of the groups become a comma separated list for the -G option.
    #
    $groupargument = " ";
    $project       = shift @groupnames;
    $grouplist     = join(",",@groupnames);

    #
    # Add special groups. These are listed in the DB so that special local
    # users can have more unix groups than just the projects/groups they are
345
    # in. These groups must already exist.
346
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
347 348 349 350
    my @extragrouplist = ();
    if ($user->UnixGroupList(\@extragrouplist) != 0) {
	fatal("Could not extra group list for $user");
    }
351 352

    #
353 354 355 356
    # Add special admin group. Also add wheel and mysql cause once
    # you become an admin and have a local shell, might as well just
    # do this too. Watch for dups though, since the above mechanism
    # could cause a duplicate entry. No big deal to catch it.
357
    #
358 359 360 361
    if ($user->admin()) {
	foreach my $extragroup ($ADMINGRP, "wheel", "mysql") {
	    push(@extragrouplist, $extragroup)
		if (! grep(/^${extragroup}$/, @extragrouplist));
362 363
	}
    }
364

365
    if (@extragrouplist) {
366
	print "Adding extra groups to list: @extragrouplist\n";
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	if ($grouplist) {
	    $grouplist = "$grouplist," . join(",", @extragrouplist);
	}
	else {
	    $grouplist = join(",", @extragrouplist);
	}
    }

    if ($grouplist) {
	$groupargument = "-G $grouplist";
    }
    else {
	$groupargument = "-G \"\"";
    }

    print "Updating user $uid record on local node.\n";

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

    print "Updating user $uid record on $control_node.\n";

394 395 396 397 398
    if ($control_node ne $BOSSNODE) {
        if (system("$SSH -host $control_node ".
	           "'$USERMOD $uid -g $project $groupargument'")) {
	    fatal("Could not modify user $uid record on $control_node.");
	}
399
    }
400

401 402 403 404 405 406 407 408 409 410 411 412 413
    #
    # Make sure the users dot files and other critical files/dirs
    # are in the correct group. I looked at the source code to
    # chown, and it does not do anything to files that are already
    # set correctly. Thank you chown.
    #
    my @dots = (".login", ".profile", ".cshrc", ".ssl", ".ssh");
    my $homedir = USERROOT() . "/$uid";

    system("$CHOWN $uid:$project $homedir") == 0
	or fatal("Could not chown home dir to $uid:$project");

    foreach my $dot (@dots) {
414 415 416 417
	if (-e "$homedir/$dot") {
	    system("$CHOWN -R $uid:$project $homedir/$dot") == 0
		or fatal("Could not chown $homedir/$dot to $uid:$project");
	}
418 419
    }
    
420 421 422 423 424
    #
    # Now schedule account updates on all the nodes that this person has
    # an account on.
    #
    TBNodeUpdateAccountsByUID($uid);
425 426
}

427 428 429
$UID  = $SAVEUID;
$EUID = $UID;

430
# and the twiki.
431
if ($WIKISUPPORT || $BUGDBSUPPORT || $OPSDBSUPPORT) {
432 433 434 435 436 437
    foreach $user (@modusers) {
	next
	    if ($user->nocollabtools());
	
	my $uid_idx = $user->uid_idx();
	
438
	if ($WIKISUPPORT) {
439
	    system("$SETWIKIGROUPS $optarg $uid_idx") == 0 or
440 441 442
		fatal("$SETWIKIGROUPS $user failed!");
	}
	if ($BUGDBSUPPORT) {
443
	    system("$SETBUGDBGROUPS $optarg $uid_idx") == 0 or
444 445
		fatal("$SETBUGDBGROUPS $user failed!");
	}
446
	if ($OPSDBSUPPORT) {
447
	    system("$OPSDBCONTROL $optarg setgroups $uid_idx") == 0 or
448 449
		fatal("$OPSDBCONTROL setgroups $user failed!");
	}
450 451 452
    }
}

453 454 455
print "Group Update Completed!\n";
exit(0);

456
sub fatal($) {
457 458
    my($mesg) = $_[0];

459 460
    die("*** $0:\n".
	"    $mesg\n");
461
}