modgroups.in 6.41 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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Modify groups (add and subtract in DB) for a user, and then call
# other scripts that need to do something about it.
#
# Note that this script does not create accounts or groups. That should
# already have been done with other scripts.
#
sub usage()
{
    print STDOUT
	"Usage: modgroups [-a pid:gid:trust[,pid:gid:trust]...] ".
	                 "[-m pid:gid:trust[,pid:gid:trust]...] ".
	                 "[-r pid:gid[,pid:gid]...] user\n";
    exit(-1);
}
my $optlist = "dr:a:m:";
my $debug   = 0;
my $user;
my @addlist = ();
my @modlist = ();	# Just changing the trust value ...
my @remlist = ();

#
# Configure variables
#
my $TB		   = "@prefix@";
my $TBOPS	   = "@TBOPSEMAIL@";
my $TBLOGS	   = "@TBLOGSEMAIL@";
my $CONTROL        = "@USERNODE@";
my $BOSSNODE       = "@BOSSNODE@";
my $WIKISUPPORT    = @WIKISUPPORT@;
my $BUGDBSUPPORT   = @BUGDBSUPPORT@;
my $CHATSUPPORT    = @CHATSUPPORT@;
43
my $ELABINELAB	   = @ELABINELAB@;
44
my $MODBUDDIES     = "$TB/sbin/modjabberbuddies";
45
my $SETCHATMEMBERS = "$TB/sbin/setchatmembers";
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
my $SETGROUPS	   = "$TB/sbin/setgroups";
my $SSH		   = "$TB/bin/sshtb";
my $SAVEUID	   = $UID;

# Locals
my $dbuid;

#
# 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 libaudit;
use libdb;
use libtestbed;

# Protos
sub fatal($);

#
# 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!\n");
}

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

    foreach my $token (@tokens) {
	#
	# Untaint,
	#
	if ($token =~ /^([-\w]+):([-\w]+):([-\w]+)$/) {
	    push(@addlist, "$1:$2:$3");
	}
	else {
	    die("Bad data in token: $token.");
	}
    }
}
if (defined($options{"m"})) {
    my @tokens = split(",", $options{"m"});

    foreach my $token (@tokens) {
	#
	# Untaint,
	#
	if ($token =~ /^([-\w]+):([-\w]+):([-\w]+)$/) {
	    push(@modlist, "$1:$2:$3");
	}
	else {
	    die("Bad data in token: $token.");
	}
    }
}
if (defined($options{"r"})) {
    my @tokens = split(",", $options{"r"});

    foreach my $token (@tokens) {
	#
	# Untaint,
	#
	if ($token =~ /^([-\w]+):([-\w]+)$/) {
	    push(@remlist, "$1:$2");
	}
	else {
	    die("Bad data in token: $token.");
	}
    }
}
usage()
    if (@ARGV != 1);
$user = $ARGV[0];

# Untaint the user.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
144
if ($user =~ /^([-\w]+)$/) {
145
146
147
148
149
150
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    $user = $1;
}
else {
    die("Bad user name: $user.");
}

#
# Get user DB uid.
#
if (! UNIX2DBUID($UID, \$dbuid)) {
    die("*** $0:\n".
        "    You do not exist in the Emulab Database!\n");
}

#
# Permission checks. Do this later.
#

#
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
    #
    # Parent exits normally
    #
    exit(0);
}

#
# Add groups. Do any necessary callouts.
#
foreach my $token (@addlist) {
    my ($pid,$gid,$trust) = split(":", $token);

    #
    # Look to see if there is already an entry; this happens as the result
    # of either a join request or a new project request. The row is inserted
    # but with a trust value of "none". We probably want a pending table ...
    #
    my $query_result =
	DBQueryFatal("select trust from group_membership ".
		     "where pid='$pid' and gid='$gid' and uid='$user'");

    if ($query_result->num_rows) {
	#
	# Sanity check; better be trust=none.
	#
	my ($curtrust) = $query_result->fetchrow_array();

	fatal("Group membership table problem; ".
	      "$user,$pid,$gid,$trust,$curtrust\n")
196
	    if ($curtrust ne "none" && !$ELABINELAB);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

	DBQueryFatal("update group_membership set ".
		     "     trust='$trust', ".
		     "     date_approved=now() ".
		     "where pid='$pid' and gid='$gid' and uid='$user'");
    }
    else {
	DBQueryFatal("insert into group_membership set ".
		     "     uid='$user', ".
		     "     pid='$pid', ".
		     "     gid='$gid', ".
		     "     trust='$trust', ".
		     "     date_applied=now(), ".
		     "     date_approved=now()");
    }
    #
    # Tell chat subsystem to change groups ...
    #
    if ($CHATSUPPORT) {
	system("$MODBUDDIES -a $pid:$gid $user") == 0 or
	    fatal("$MODBUDDIES '-a $pid:$gid $user' failed!");
218
219
220
221
222
223

	# Does not work yet.
	if (0) {
	    system("$SETCHATMEMBERS -g $gid $pid") == 0 or
		fatal("$SETCHATMEMBERS '-g $gid $pid' failed!");
	}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    }
}

#
# Update groups. Do any necessary callouts.
#
foreach my $token (@modlist) {
    my ($pid,$gid,$trust) = split(":", $token);

    DBQueryFatal("update group_membership set trust='$trust' ".
		 "where pid='$pid' and gid='$gid' and uid='$user'");
}

#
# Remove groups. Do any necessary callouts.
#
foreach my $token (@remlist) {
    my ($pid,$gid)  = split(":", $token);
    my @delgroups   = ();

    #
    # Special case; if pid==gid then delete from the project entirely.
    # However, we want to get a list of the groups about to be deleted
    # so we know what to hand off to the callouts.
    #
    if ($pid eq $gid) {
	my $query_result =
	    DBQueryFatal("select gid from group_membership ".
			 "where uid='$user' and pid='$pid'");

	while (my ($gid) = $query_result->fetchrow_array()) {
	    push(@delgroups, $gid);
	}
    }
    else {
	@delgroups = ($gid);
    }
    
    DBQueryFatal("delete from group_membership ".
		 "where pid='$pid' and ".
		 (($pid ne $gid) ? "gid='$gid' and " : " ") .
		 "      uid='$user'");

    #
    # Tell chat subsystem to change groups ...
    #
    if ($CHATSUPPORT) {
271
	my @optlist = ();
272

273
274
	foreach my $group (@delgroups) {
	    push(@optlist, "$pid:$group");
275

276
277
	    # Does not work yet.
	    if (0) {
278
279
280
		system("$SETCHATMEMBERS -g $group $pid") == 0 or
		    fatal("$SETCHATMEMBERS '-g $group $pid' failed!");
	    }
281
	}
282
283
284
285
286
287
	if (@optlist) {
	    my $chatargs = join(",", @optlist);
	
	    system("$MODBUDDIES -r $chatargs $user") == 0 or
		fatal("$MODBUDDIES '-r $chatargs $user' failed!");
	}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
    }
}

#
# Finally, call setgroups to do the rest.
#
system("$SETGROUPS $user") == 0 or
    fatal("$SETGROUPS $user failed!");

exit(0);

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

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