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

#
# Initial jabber setup.
#
sub usage()
{
    print STDOUT "Usage: jabbersetup\n";
    exit(-1);
}
my $optlist  = "d";
my $debug    = 0;
my $impotent = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $CONTROL     = "@USERNODE@";
my $BOSSNODE	= "@BOSSNODE@";
my $CHATSUPPORT = @CHATSUPPORT@;
my $SSH         = "$TB/bin/sshtb";
my $ADDUSER     = "$TB/sbin/addjabberuser";
32 33
my $ADDCHAT     = "$TB/sbin/addjabberchat";
my $SETCHATMEMB = "$TB/sbin/setchatmembers";
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
my $MODBUDDIES  = "$TB/sbin/modjabberbuddies";

# Protos
sub fatal($);

#
# 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;

#
# If no jabber support, just exit. 
#
if (! $CHATSUPPORT) {
    print "Chat support is not enabled. Exit ...\n";
    exit(0);
}

#
# Only testbed admins.
#
if (!TBAdmin($UID)) {
    die("*** $0:\n".
	"    Must be a testbed admin to run this script\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 (@ARGV) {
    usage();
}

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

#
# First add the users.
101
#
102 103 104 105 106
$query_result =
    DBQueryFatal("select distinct u.uid ".
		 "  from group_membership as g ".
		 "left join users as u on u.uid=g.uid ".
		 "where u.status='active' ".
107 108 109
#		 "  and (g.pid='testbed' or g.pid='emulab-ops' or ".
#		 "       g.pid='tbres' or g.pid='utahstud' or ".
#		 "       g.pid='lbsbox')" .
110 111 112 113 114 115 116
		 "order by u.admin");

while (my ($uid) = $query_result->fetchrow_array()) {
    system("$ADDUSER $uid") == 0
	or fatal("Could not create jabber account for $uid");
}

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
#
# Now add chatrooms for each pid/gid.
#
# This is turned off for now; the chatroom plugin is too fragile for this.
#
if (0) {
$query_result =
    DBQueryFatal("select g.pid,g.gid from groups as g ".
		 "left join projects as p on p.pid=g.pid ".
		 "where p.approved=1 ".
		 "and (p.pid='testbed' or p.pid='emulab-ops' or ".
		 "     p.pid='tbres' or p.pid='utahstud' or ".
		 "       g.pid='lbsbox')" .
		 "");

while (my ($pid,$gid) = $query_result->fetchrow_array()) {
    print "Creating a chatroom for for $pid/$gid\n"
	if ($debug);

    system("$ADDCHAT -g $gid $pid") == 0
	or fatal("Could not add jabber chatroom for $pid/$gid");

    print "Setting the members of chatroom $pid/$gid\n"
	if ($debug);

   system("$SETCHATMEMB -g $gid $pid") == 0
	or fatal("Could not set members of jabber chatroom for $pid/$gid");
}
}

147 148 149 150 151 152 153
#
# Now add buddies for each user in each project.
#
$query_result =
    DBQueryFatal("select g.pid,g.gid from groups as g ".
		 "left join projects as p on p.pid=g.pid ".
		 "where p.approved=1 ".
154 155 156
#		 "and (p.pid='testbed' or p.pid='emulab-ops' or ".
#		 "     p.pid='tbres' or p.pid='utahstud' or ".
#		 "       g.pid='lbsbox')" .
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
		 "");

while (my ($pid,$gid) = $query_result->fetchrow_array()) {
    next
	if ($pid ne $gid);
    
    print "Setting up buddy lists for members of $pid\n"
	if ($debug);

    my $users_results =
	DBQueryFatal("select g.uid from group_membership as g ".
		     "left join users as u on u.uid=g.uid ".
		     "where g.pid='$pid' and g.gid='$gid' and ".
		     "      u.status='active'");
    
    while (my ($uid) = $users_results->fetchrow_array()) {
	system("$MODBUDDIES -a $pid:$gid $uid") == 0 or
	    fatal("$MODBUDDIES '-a $pid:$gid $uid' failed!");
    }
}

exit(0);

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

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