wikiproxy.in 21.6 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15
# All rights reserved.
#
use English;
use Getopt::Std;
use Errno;
    
#
# A wrapper for messing with the wiki support from boss.
#
sub usage()
{
16
    print "Usage: wikiproxy adduser <uid> <wikiname> <passhash> or\n";
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
    print "       wikiproxy deluser <uid> <wikiname> or\n";
    print "       wikiproxy addproject <pid> <wikiname> or\n";
    print "       wikiproxy setgroups <wikiuser> <wikigroup> ...\n";
    exit(-1);
}
my $optlist = "d";
my $debug   = 0;

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

# Should be configure variables.
my $WIKIDIR  = "/usr/local/www/data/twiki";

# And other stuff.
my $USERTEMPLATE = "$TB/lib/wiki/usertemplate";
my $HOMETEMPLATE = "$TB/lib/wiki/webhometemplate";
my $WIKIDATADIR  = "$WIKIDIR/data";
my $WIKIUSERDIR  = "$WIKIDATADIR/Main";
my $WIKIGROUPDIR = "$WIKIDATADIR/Main";
my $WIKIARCHIVE  = "$WIKIDATADIR/_archive";
my $WIKIPASSWD   = "$WIKIDIR/data/.htpasswd";
42
my $USERMAPDB    = "$WIKIDIR/data/.usermap";
43
my $COOKIEDIR    = "/var/db/cgisess";
44 45 46
my $WIKIUSER     = "nobody";
my $WIKIGROUP    = "nobody";
my $CI		 = "ci";
47
my $TAR		 = "tar";
48 49 50 51 52 53
    
#
# Turn off line buffering on output
#
$| = 1;

54 55 56 57 58 59 60 61 62 63 64 65
# Protos
sub AddWikiUser(@);
sub DelWikiUser(@);
sub AddWikiProject(@);
sub AddWikiGroup(@);
sub FixWikiProject(@);
sub FixWikiUser(@);
sub FixWikiGroup(@);
sub SetWikiGroups(@);
sub CI($$);
sub WikixLogin(@);
sub WikiRemap(@);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
sub UserMapping(@);
67 68
sub fatal($);

69 70 71 72 73 74
#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

75 76 77
# Avoid quota and out of space on /tmp.
$ENV{'TMPDIR'} = "/var/tmp";

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#
# Only real root, cause the script has to read/write a pid file that
# cannot be accessed by the user.
#
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;

93 94 95 96 97 98 99 100 101
# Version of FreeBSD.
my $FBSD_VERSION = 4;
if (`uname -r` =~ /^(\d)/) {
    $FBSD_VERSION = $1;
}
else {
    die("Could not determine what version of FreeBSD you are running!\n");
}

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
#
# 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();
}

my $action = shift(@ARGV);

if ($action eq "adduser") {
    exit(AddWikiUser(@ARGV));
}
elsif ($action eq "deluser") {
    exit(DelWikiUser(@ARGV));
}
elsif ($action eq "addproject") {
    exit(AddWikiProject(@ARGV));
}
128 129 130
elsif ($action eq "addgroup") {
    exit(AddWikiGroup(@ARGV));
}
131 132 133
elsif ($action eq "setgroups") {
    exit(SetWikiGroups(@ARGV));
}
134 135 136
elsif ($action eq "xlogin") {
    exit(WikixLogin(@ARGV));
}
137 138 139
elsif ($action eq "remap") {
    exit(WikiRemap(@ARGV));
}
140 141 142
elsif ($action eq "fixproject") {
    exit(FixWikiProject(@ARGV));
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144
elsif ($action eq "mapping") {
    exit(UserMapping(@ARGV));
145
}
146 147 148 149 150 151 152 153 154 155 156 157
else {
    die("*** $0:\n".
	"    Do not know what to do with '$action'!\n");
}
exit(0);

#
# Add entry (or update password) for a user.
#
sub AddWikiUser(@)
{
    usage()
158
	if (@_ != 3);
159

160
    my ($user, $wikiname, $passwd) = @_;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    chdir("$WIKIUSERDIR") or
	fatal("Could not chdir to $WIKIUSERDIR");

    #
    # If the password file does not have the entry, just tack it onto
    # the end of the file. Otherwise we have to get fancier so we 
    # change the password atomically. It appears that the TWiki code
    # does not lock the password file when it makes it own changes! 
    #
    if (system("egrep -q -s '^${wikiname}:' $WIKIPASSWD")) {
	print "Adding $user to $WIKIPASSWD\n"
	    if ($debug);
	
	open(PWD, ">> $WIKIPASSWD") or
	    fatal("Could not open $WIKIPASSWD for appending");
	print PWD "${wikiname}:${passwd}\n";
178
	print PWD "${user}:${passwd}\n";
179
	close(PWD);
180 181 182 183 184 185 186 187 188 189 190

	#
	# Add to the user map. 
	#
	dbmopen(%MAPDB, $USERMAPDB, 0644) or
	    fatal("Cannot open $USERMAPDB: $!");

	$MAPDB{$user} = $wikiname;

	# Write the DB back out.
	dbmclose(%MAPDB);	
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    }
    else {
	#
	# Open up the file and read it, creating a new version.
	#
	my $data = "";
	
	print "Updating $wikiname in $WIKIPASSWD\n"
	    if ($debug);
	
	open(PWD, "$WIKIPASSWD") or
	    fatal("Could not open $WIKIPASSWD for reading");
	
	while (<PWD>) {
	    if ($_ =~ /^${wikiname}:.*$/) {
		$data .= "${wikiname}:${passwd}\n";
	    }
208 209 210
  	    elsif ($_ =~ /^${user}:.*$/) {
	        $data .= "${user}:${passwd}\n";
	    }
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 330
	    else {
		$data .= $_;
	    }
	}
	close(PWD);

	open(PWD, "> ${WIKIPASSWD}.$$") or
	    fatal("Could not open ${WIKIPASSWD}.$$ for writing");
	print PWD $data;
	close(PWD);

	system("chown ${WIKIUSER}:${WIKIGROUP} ${WIKIPASSWD}.$$") == 0
	    or fatal("Could not chown ${WIKIPASSWD}.$$");

	rename("${WIKIPASSWD}.$$", $WIKIPASSWD)
	    or fatal("Could not rename ${WIKIPASSWD}.$$");
    }

    #
    # Create the stub topic file for the user if it does not exist.
    #
    return 0
	if (-e "${WIKIUSERDIR}/${wikiname}.txt");

    print "Creating ${WIKIUSERDIR}/${wikiname}.txt\n"
	if ($debug);
    
    system("cp -p ${USERTEMPLATE} ${wikiname}.txt") == 0
	or fatal("Could not copy ${USERTEMPLATE} to ${wikiname}.txt");

    #
    # Set the initial groups list to the user.
    #
    open(PREFS, "${wikiname}.txt") or
	fatal("Could not open ${wikiname}.txt for reading");

    while (<PREFS>) {
	if ($_ =~ /^(.* Set ALLOWTOPIC.*)\r$/ ||
	    $_ =~ /^(.* Set ALLOWTOPIC.*)$/) {
	    $data .= $1 . "%MAINWEB%.${wikiname}\n";
	}
	else {
	    $data .= $_;
	}
    }
    close(PREFS);

    #
    # Now write the file out.
    #
    open(PREFS, "> ${wikiname}.txt") or
	fatal("Could not open ${wikiname}.txt for writing");
    print PREFS $data;
    close(PREFS);
    
    #
    # Check it in (locked).
    #
    CI("${wikiname}.txt", "AddWikiUser $user $wikiname") == 0
        or fatal("Could not ci ${wikiname}.txt");
    
    return 0;
}

#
# Delete entry for a user.
#
sub DelWikiUser(@)
{
    usage()
	if (@_ != 2);

    my ($user, $wikiuser) = @_;

    chdir("$WIKIUSERDIR") or
	fatal("Could not chdir to $WIKIUSERDIR");

    #
    # First, find groups the user is currently a member of, and remove.
    #
    my $currentgroups =
	`egrep -l -s 'GROUP = .*${wikiuser}.*' *Group.txt`;
    $currentgroups =~ s/Group.txt//g;

    if (!$?) {
	my @glist = split(/\s+/, $currentgroups);
	    
	print "Current groups for $wikiuser are @glist\n"
	    if ($debug);

	foreach my $group (@glist) {
	    my $wikiname = "${group}Group";
	    my $data     = "";

	    print "Removing $wikiuser from $group\n";

	    open(GRP, "${wikiname}.txt") or
		fatal("DelWikiUser: ".
		      "Could not open ${wikiname}.txt (read)\n");

	    while (<GRP>) {
		if ($_ =~ /^(\s*\*\s*Set\s*GROUP\s*=\s*)(.*)$/) {
		    my @tokens = split(/,\s*/, $2);

		    # Cut out the user.
		    @tokens = grep {$_ ne $wikiuser} @tokens;

		    # And reform the group list.
		    $data .= $1 . join(", ", @tokens) . "\n";
		}
		else {
		    $data .= $_;
		}
	    }
	    close(GRP);

	    #
	    # Now write the new text back out to the file.
	    #
	    open(GRP, ">${wikiname}.txt") or
331
		fatal("DelWikiUser: ".
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		      "Could not open ${wikiname}.txt (write)\n");
	    print GRP $data;
	    close(GRP);
	    
	    #
	    # Check it in (locked).
	    #
	    CI("${wikiname}.txt", "DelWikiUser $user $wikiuser") == 0
		or fatal("Could not ci ${wikiname}.txt");
	}
    }
    
    #
    # Remove user from the password file.
    #
    print "Removing $user from $WIKIPASSWD\n"
	if ($debug);
	
    open(PWD, "$WIKIPASSWD") or
	fatal("Could not open $WIKIPASSWD for reading");
	
    while (<PWD>) {
354 355
	if ($_ =~ /^${wikiuser}:.*$/ ||
	    $_ =~ /^${user}:.*$/) {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	    next;
	}
	else {
	    $data .= $_;
	}
    }
    close(PWD);

    open(PWD, "> ${WIKIPASSWD}.$$") or
	fatal("Could not open ${WIKIPASSWD}.$$ for writing");
    print PWD $data;
    close(PWD);

    system("chown ${WIKIUSER}:${WIKIGROUP} ${WIKIPASSWD}.$$") == 0
	or fatal("Could not chown ${WIKIPASSWD}.$$");

    rename("${WIKIPASSWD}.$$", $WIKIPASSWD)
	or fatal("Could not rename ${WIKIPASSWD}.$$");

375 376 377 378 379 380 381 382 383 384 385
    #
    # Delete from the user map. 
    #
    dbmopen(%MAPDB, $USERMAPDB, 0644) or
        fatal("Cannot open $USERMAPDB: $!");

    delete($MAPDB{$user});

    # Write the DB back out.
    dbmclose(%MAPDB);	

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    #
    # Archive the user file. Removing users is kinda bogus cause I do not
    # attempt to remove any other files the user created. We could figure
    # this out of course, but not bothering right now.
    #
    if (! -e $WIKIARCHIVE) {
	mkdir($WIKIARCHIVE, 0775) or
	    fatal("Could not mkdir $WIKIDATADIR!");

	system("chown ${WIKIUSER}:${WIKIGROUP} $WIKIARCHIVE") == 0
	    or fatal("Could not chown $WIKIARCHIVE");
    }
    my $wikifile = "${wikiuser}.txt";    
    my $oldname  = "${wikifile},v";
    my $newname  = "${oldname}-" . TBDateTimeFSSafe();

    print "Renaming $oldname to $WIKIARCHIVE/$newname\n"
	if ($debug);

    rename($oldname, "$WIKIARCHIVE/$newname")
	or fatal("Could not rename $oldname to $WIKIARCHIVE/$newname");
    unlink($wikifile)
	or fatal("Could not remove $wikifile");

    return 0;
}

#
# Create a "web" for a new project.
#
sub AddWikiProject(@)
{
    my $data = "";
    
    usage()
	if (@_ != 2);

    my ($pid, $wikiname) = @_;

    chdir("$WIKIDATADIR") or
	fatal("Could not chdir to $WIKIDATADIR");

    # Skip if already there. 
    return 0
	if (-d $wikiname);

    print "Creating ${WIKIDATADIR}/${wikiname}\n"
	if ($debug);

435 436 437 438 439 440
    mkdir("${WIKIDATADIR}/${wikiname}", 0775) or
	fatal("Could not mkdir $WIKIDATADIR/$wikiname!");
    
    system("chown ${WIKIUSER}:${WIKIGROUP} $WIKIDATADIR/$wikiname") == 0
	or fatal("Could not chown $WIKIDATADIR/$wikiname!");

441 442 443 444
    #
    # Copy the _default directory to the new WEB name. This "creates"
    # the new wiki web. 
    #
445
    system("$TAR cf - -C _default . | $TAR xf - -C $wikiname") == 0
446 447 448 449 450 451 452 453
	or fatal("Could not copy _default to $wikiname");

    system("cp -p ${HOMETEMPLATE} ${wikiname}/WebHome.txt") == 0
	or fatal("Could not copy ${HOMETEMPLATE} to ${wikiname}/WebHome.txt");

    #
    # We also need a group for this project to restrict who can access it.
    #
454
    AddWikiGroup($pid, "${wikiname}Group", "${wikiname}Group");
455 456 457 458 459

    #
    # And another group for the project leaders and group roots, who are
    # allowed to change the preferences for the web.
    #
460
    AddWikiGroup("${pid}Root", "${wikiname}RootGroup", "${wikiname}Group");
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

    #
    # Now finish up in the new web directory.
    # 
    chdir("$WIKIDATADIR/$wikiname") or
	fatal("Could not chdir to $WIKIDATADIR/$wikiname");

    #
    # Remove all the RCS control files. They are old and pointless.
    # Create new ones instead. 
    #
    system("rm -f *.txt,v") == 0 or
	fatal("Could not remove old RCS control files!");

    system("chown ${WIKIUSER}:${WIKIGROUP} *.txt")
	== 0 or fatal("Could not chown *.txt to ${WIKIUSER}:${WIKIGROUP}");

    system("sudo -u nobody ci -l '-mInitial Revision by wikiproxy:\n".
	   "AddWikiProject $pid $wikiname' ".
	   "      '-t-Initial Revision by wikiproxy' *.txt") == 0
        or fatal("Could not create new RCS control files.");

    #
    # Now edit the web preferences file to set who is allowed to access
    # this web. Basically, only people in the project (wiki group) and
    # the admin group.
    #
    open(PREFS, "WebPreferences.txt") or
	fatal("Could not open WebPreferences.txt for reading");

    while (<PREFS>) {
	if ($_ =~ /^.* Set (ALLOWWEB.*) = \r$/ ||
	    $_ =~ /^.* Set (ALLOWWEB.*) = $/) {
	    $data .= "\t\t* Set $1 = " . "%MAINWEB%.${wikiname}Group\n";
	}
	elsif ($_ =~ /^(.* Set ALLOWTOPIC.*)\r$/ ||
	       $_ =~ /^(.* Set ALLOWTOPIC.*)$/) {
	    $data .= $1 . "%MAINWEB%.${wikiname}RootGroup\n";
	}
	elsif ($_ =~ /^(.* Set NOSEARCHALL.*)\r$/ ||
	       $_ =~ /^(.* Set NOSEARCHALL.*)$/) {
	    $data .= $1 . "on\n";
	}
	else {
	    $data .= $_;
	}
    }
    close(PREFS);

    #
    # Now write the file out and check it in.
    #
    open(PREFS, ">WebPreferences.txt") or
	fatal("Could not open WebPreferences.txt for writing");
    print PREFS $data;
    close(PREFS);

    CI("WebPreferences.txt", "AddWikiProject $pid $wikiname") == 0
        or fatal("Could not ci WebPreferences.txt");

    return 0;
}

#
# Create a wiki group. 
#
sub AddWikiGroup(@)
{
    usage()
530
	if (@_ < 2 || @_ > 3);
531

532 533 534
    my ($group, $wikiname, $allowed) = @_;
    $allowed = $wikiname
	if (!defined($allowed));
535 536 537 538 539 540

    chdir("$WIKIGROUPDIR") or
	fatal("Could not chdir to $WIKIGROUPDIR");

    # Skip if already there. 
    return 0
541
	if (-e "${wikiname}.txt");
542 543 544 545 546 547 548 549 550 551

    print "Creating ${WIKIGROUPDIR}/${wikiname}.txt\n"
	if ($debug);

    open(GRP, "> ${wikiname}.txt") or
	fatal("Could not open ${WIKIGROUPDIR}/${wikiname}.txt for writing");

    print GRP "*Project/Group Members for ${wikiname}*\n";
    print GRP "\n";
    print GRP "\t* Set GROUP = \n";
552 553
    print GRP "\t* Set ALLOWTOPICVIEW = %MAINWEB%.TWikiAdminGroup, ".
	"%MAINWEB%.${allowed}\n";
554 555 556 557 558 559 560 561 562 563 564 565 566 567
    print GRP "\t* Set ALLOWTOPICCHANGE = %MAINWEB%.TWikiAdminGroup\n";
    print GRP "\t* Set ALLOWTOPICRENAME = %MAINWEB%.TWikiAdminGroup\n";
    print GRP "\n";
    close(GRP);

    #
    # Check it in (locked).
    #
    CI("${wikiname}.txt", "AddWikiGroup $group $wikiname") == 0
        or fatal("Could not ci ${wikiname}.txt");

    return 0;
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
#
# Fix a wiki group. 
#
sub FixWikiProject(@)
{
    usage()
	if (@_ != 2);

    my ($pid, $wikiname) = @_;

    FixWikiGroup($pid, "${wikiname}Group", "${wikiname}Group");

    #
    # And another group for the project leaders and group roots, who are
    # allowed to change the preferences for the web.
    #
    FixWikiGroup("${pid}Root", "${wikiname}RootGroup", "${wikiname}Group");

    return 0;
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
#
# Fix a wiki user home page,
#
sub FixWikiUser(@)
{
    usage()
	if (@_ != 2);

    my ($user, $wikiname, $passwd) = @_;

    chdir("$WIKIUSERDIR") or
	fatal("Could not chdir to $WIKIUSERDIR");

    if (! system("egrep -q -s ".
		 "'<!-- Please leave this line here' ${wikiname}.txt")) {
	return 0;
    }

    system("cp -p ${USERTEMPLATE} ${wikiname}.txt") == 0
	or fatal("Could not copy ${USERTEMPLATE} to ${wikiname}.txt");

    #
    # Set the initial groups list to the user.
    #
    open(PREFS, "${wikiname}.txt") or
	fatal("Could not open ${wikiname}.txt for reading");

    while (<PREFS>) {
	if ($_ =~ /^(.* Set ALLOWTOPIC.*)\r$/ ||
	    $_ =~ /^(.* Set ALLOWTOPIC.*)$/) {
	    $data .= $1 . "%MAINWEB%.${wikiname}\n";
	}
	else {
	    $data .= $_;
	}
    }
    close(PREFS);

    #
    # Now write the file out.
    #
    open(PREFS, "> ${wikiname}.txt") or
	fatal("Could not open ${wikiname}.txt for writing");
    print PREFS $data;
    close(PREFS);
    
    #
    # Check it in (locked).
    #
    CI("${wikiname}.txt", "FixWikiUser $user $wikiname") == 0
        or fatal("Could not ci ${wikiname}.txt");
    
    return 0;
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
#
# Fix a wiki group. 
#
sub FixWikiGroup(@)
{
    usage()
	if (@_ != 3);

    my ($group, $wikiname, $allowed) = @_;

    chdir("$WIKIGROUPDIR") or
	fatal("Could not chdir to $WIKIGROUPDIR");

    return 0
	if (! -e "${wikiname}.txt");

    if (system("egrep -q -s 'ALLOWTOPICVIEW' ${wikiname}.txt")) {
	my @lines = ();
	
	print "Fixing ${WIKIGROUPDIR}/${wikiname}.txt\n";
	
	open(GRP, "< ${wikiname}.txt") or
	    fatal("Could not open ${WIKIGROUPDIR}/${wikiname}.txt");
	while (<GRP>) {
	    push(@lines, $_);

	    if ($_ =~ /ALLOWTOPICCHANGE/) {
		push(@lines,
		     "\t* Set ALLOWTOPICVIEW = %MAINWEB%.TWikiAdminGroup, ".
		     "%MAINWEB%.${allowed}\n");
	    }
	}
	close(GRP);

	open(GRP, "> ${wikiname}.txt") or
	    fatal("Could not open ${WIKIGROUPDIR}/${wikiname}.txt");
	
	foreach my $line (@lines) {
	    print GRP $line
	}
	close(GRP);

	#
	# Check it in (locked).
	#
	CI("${wikiname}.txt", "FixWikiGroup $group $wikiname") == 0
	    or fatal("Could not ci ${wikiname}.txt");
    }
    return 0;
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
#
# Set the wiki groups for a user.
#
sub SetWikiGroups(@)
{
    usage()
	if (@_ < 1);

    my $wikiuser  = shift(@_);
    my @groups    = @_;
    my %grouphash = ();
    
    foreach my $group (@groups) {
	$grouphash{$group} = $group;
    }

    chdir("$WIKIGROUPDIR") or
	fatal("Could not chdir to $WIKIGROUPDIR");

    #
    # First, find groups the user is currently a member of, and remove
    # from those groups not supposed to be in.
    #
    my $currentgroups =
	`egrep -l -s 'GROUP = .*${wikiuser}.*' *Group.txt`;
    $currentgroups =~ s/Group.txt//g;

    if (!$?) {
	my @glist = split(/\s+/, $currentgroups);
	    
	print "Current groups for $wikiuser are @glist\n"
	    if ($debug);

	foreach my $group (@glist) {
	    my $wikiname = "${group}Group";
	    my $data     = "";

	    next
		if (exists($grouphash{$group}));
	    
	    print "Removing $wikiuser from $group\n";

	    open(GRP, "${wikiname}.txt") or
		fatal("SetWikiGroups: ".
		      "Could not open ${wikiname}.txt (read)\n");

	    while (<GRP>) {
		if ($_ =~ /^(\s*\*\s*Set\s*GROUP\s*=\s*)(.*)$/) {
		    my @tokens = split(/,\s*/, $2);

		    # Cut out the user.
		    @tokens = grep {$_ ne $wikiuser} @tokens;

		    # And reform the group list.
		    $data .= $1 . join(", ", @tokens) . "\n";
		}
		else {
		    $data .= $_;
		}
	    }
	    close(GRP);

	    #
	    # Now write the new text back out to the file.
	    #
	    open(GRP, ">${wikiname}.txt") or
		fatal("SetWikiGroups: ".
		      "Could not open ${wikiname}.txt (write)\n");
	    print GRP $data;
	    close(GRP);
	    
	    #
	    # Check it in (locked).
	    #
	    CI("${wikiname}.txt", "SetWikiGroups $wikiuser @groups") == 0
		or fatal("Could not ci ${wikiname}.txt");
	}
    }

    #
    # Now add user to other groups.
    #
    foreach my $group (@groups) {
	my $wikiname = "${group}Group";
	my $data     = "";
	    
	# Error if no such wikigroup
782 783 784 785
	if (! -e "${wikiname}.txt") {
	    print "No group file for wiki group $wikiname! Skipping ...\n";
	    next;
	}
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

	next
	    if (!system("egrep -q -s 'GROUP = .*${wikiuser}.*' ".
			"${wikiname}.txt\n"));
	    
	print "Adding $wikiuser to wikigroup ${wikiname}.txt\n";

	open(GRP, "${wikiname}.txt") or
	    fatal("SetWikiGroups: Could not open ${wikiname}.txt (read)\n");

	while (<GRP>) {
	    if ($_ =~ /^(\s*\*\s*Set\s*GROUP\s*=)\s*$/) {
		# First name in the group.
		$data .= $1 . " $wikiuser\n";
	    }
	    elsif ($_ =~ /^(\s*\*\s*Set\s*GROUP\s*=\s*)(.*)$/) {
		$data .= $1 . "${2}, $wikiuser\n";
	    }
	    else {
		$data .= $_;
	    }
	}
	close(GRP);

	#
	# No write the new text back out to the file.
	#
	open(GRP, ">${wikiname}.txt") or
	    fatal("SetWikiGroups: Could not open ${wikiname}.txt (write)\n");
	print GRP $data;
	close(GRP);

	#
	# Check it in (locked).
	#
	CI("${wikiname}.txt", "SetWikiGroups $wikiuser @groups") == 0
	    or fatal("Could not ci ${wikiname}.txt");
    }

    #
    # And finally, change the home page for the user. This is basically
    # to avoid searching the entire wiki for the membership.
    #
    $data = "";
    
    open(PREFS, "${wikiuser}.txt") or
	fatal("Could not open ${wikiuser}.txt for reading");

    while (<PREFS>) {
835 836
	if ($_ =~ /^__These are your Projects.*$/) {
	    $data .= "__These are your Projects:__\n";
837 838

	    #
839
	    # Add a list of just the Projects. 
840 841 842
	    #
	    foreach my $group (@groups) {
		if (-d "../$group") {
843
		    $data .= "\t* [[${group}.WebHome][${group}]]\n";
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		}
	    }

	    #
	    # And skip the current ones.
	    #
	    while (<PREFS>) {
		last
		    if ($_ =~ /^\s$/);
	    }
	    $data .= "\n";
	}
	else {
	    $data .= $_;
	}
    }
    close(PREFS);

    #
    # Now write the file out.
    #
    open(PREFS, "> ${wikiuser}.txt") or
	fatal("Could not open ${wikiuser}.txt for writing");
    print PREFS $data;
    close(PREFS);
    
    #
    # Check it in (locked).
    #
    CI("${wikiuser}.txt", "SetWikiGroups $wikiuser @groups") == 0 
        or fatal("Could not ci ${wikiuser}.txt");
    
    return 0;
}

sub CI($$) {
    my ($file, $comment) = @_;

    system("chown ${WIKIUSER}:${WIKIGROUP} $file")
	== 0 or fatal("Could not chown $file to ${WIKIUSER}:${WIKIGROUP}");

885 886 887
    my $locking = ($FBSD_VERSION == 4 ? "-l" : "-u");

    my $cmd = "sudo -u nobody ci -u '-mRevision by wikiproxy:\n$comment'".
888 889 890
	"      '-t-Revision by wikiproxy' $file 2>&1";
    my $output = `$cmd`;
    if ($? && $output =~ /no lock set by/ ) {
891 892 893 894
	my $unlockcmd = "rcs -u -M $file";
	print "'$unlockcmd'\n"
	    if ($debug);
	my $output = `$unlockcmd`;
895 896
	return -1
	    if ($?);
897
	my $lockcmd = "rcs -l $file";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
898
	print "'$lockcmd'\n"
899 900
	    if ($debug);
	system("sudo -u nobody $lockcmd");
901 902 903 904 905 906
	return -1
	    if ($?);
        return system($cmd);	
    }
    return $? >> 8;
}
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

#
# Backdoor Login
#
sub WikixLogin(@)
{
    usage()
	if (@_ != 2);

    my ($user, $secretkey) = @_;

    #
    # Create a little file that holds the secret key, named by the user.
    # The TWiki login script will check for the existence of this file,
    # and use the key inside it to match against the key provided by the
    # client browser.
    #
    open(KEY, ">${COOKIEDIR}/$user") or
	fatal("Could not open ${COOKIEDIR}/$user for writing!");

    print KEY "$secretkey\n";
    print KEY time() . "\n";
    close(KEY);
    return 0;
}
932
     
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
#
# Remap user.
#
sub WikiRemap(@)
{
    usage()
	if (@_ != 2);

    my ($user, $wikiname) = @_;

    #
    # Remap user in the user map. 
    #
    dbmopen(%MAPDB, $USERMAPDB, 0644) or
	fatal("Cannot open $USERMAPDB: $!");

    print "Current mapping is ". $MAPDB{$user} . "\n";

    $MAPDB{$user} = $wikiname;

    # Write the DB back out.
    dbmclose(%MAPDB);	

    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

#
# Get user mapping
#
sub UserMapping(@)
{
    usage()
	if (@_ != 1);

    my ($user) = @_;

    dbmopen(%MAPDB, $USERMAPDB, 0644) or
	fatal("Cannot open $USERMAPDB: $!");

    print "Current mapping is ". $MAPDB{$user} . "\n";

    # Write the DB back out.
    dbmclose(%MAPDB);	

    return 0;
}
    
980 981 982 983 984 985 986
sub fatal($)
{
    my($mesg) = $_[0];

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