#!/usr/bin/perl

$discus_conf = '/usr2/people/aparkin/webadmin/discus_admin/discus.conf';

$pro_fileid = '592889414030';

$pro_license = '0005-2207-2202';

#DISCUS PRO board authorization script (board-auth.cgi)

#-------------------------------------------------------------------------------

# DISCUS VERSION 3.10 COPYRIGHT NOTICE

#

# Discus 3.10 is copyright (c) 2000 by DiscusWare, LLC, all rights reserved.

# The use of Discus is governed by the Discus License Agreement which is

# available from the Discus WWW site at:

#    http://www.discusware.com/discus/license

#

# Pursuant to the Discus License Agreement, this copyright notice may not be

# removed or altered in any way.

#-------------------------------------------------------------------------------

if (open (FILE, "$discus_conf")) {

	@file = <FILE>;

	close (FILE);

	foreach $line (@file) {

		if ($line =~ /^(\w+)=(.*)/) {

			$varname = $1;

			$value = $2;

			$value =~ s/\r//g;

			${$varname} = $value;

		}

	}

	require "$admin_dir/source/src-board-subs-common";

} else {

	print "Content-type: text/html\n\n";

	print "<HTML><HEAD><TITLE>Script Execution Error</TITLE></HEAD>\n";

	print "<BODY BGCOLOR=#ffffff TEXT=#000000>\n";

	print "<H1>Script Execution Error</H1>\n";

	print "Discus scripts could not execute because the discus.conf file\n";

	print "could not be opened.";

	print "<P>Reason: <FONT COLOR=#ff0000><B>$!</B></FONT>" if $!;

	print "<P>This generally indicates a setup error of some kind.\n";

	print "Consult the <A HREF=\"http://www.discusware.com/support\">Discus ";

	print "Resource Center</A> for troubleshooting information.</BODY></HTML>\n";

	exit(0);

}

&parse_form;

&read_cookie;

$file = $FORM{'file'};

if ($file =~ /[^\w\-.\/]/) {

	&error_message("Invalid File Request", "The requested file, <B>$file</B>, is invalid!");

} elsif ($file =~ /\.\./) {

	&error_message("Invalid File Request", "The requested file, <B>$file</B>, is invalid!");

} elsif ($file =~ /\.skin$/ || $file =~ m|\.conf$| || $file =~ m|/passwd\.txt$|) {

	&error_message("Invalid File Request", "The requested file, <B>$file</B>, is invalid!");

} elsif (!-e "$secdir$file" && !-e "$message_dir$file") {

	&error_message("Invalid File Request", "The requested file, <B>$file</B>, does not exist!");

} elsif ($file eq "" && !-e "$secdir$file") {

	&error_message("Invalid File Request", "The requested file does not exist!");

} elsif (-e "$message_dir$file") {

	&seturl("$message_url$file");

	exit(0);

}

if (!$pro) {

	&error_message("$L{FEATURE_NOT_SUPPORTED}", "$L{FEATURE_NOT_SUPPORTED_DESCR}");

}

$file =~ m|^/(\d+)/|; $topic = $1;

@auth = &ex('get_authorized_topics', 1);

if (!grep(/^$topic$/, @auth)) {

	$FORM{'username'} = $COOKIE{'user' . $COOKIE_ID} if ($FORM{'username'} eq "" && $COOKIE{'user' . $COOKIE_ID} ne "");

	($a) = &ex('verify_postread_privileges', $topic, $FORM{'username'}, $FORM{'password'}, $FORM{'special_password'}, "security", 1);

	if ($a == 1) {

		push (@auth, $topic);

		if (open(FILE, "$secdir/$topic/passwd.txt")) {

			@file = <FILE>;

			close (FILE);

			$pass = $file[0]; $pass =~ s/\s+$//;

			$guess = crypt($pass, "cookie");

			$COOKIE_STRING .= "Set-Cookie: auth$topic$COOKIE_ID=$guess; path=/\n";

		}

	} elsif ($a == 2) {

		$this_ip_banned = 1;

	}

}

if (!grep(/^$topic$/, @auth)) {

	open (SECURITY, "$admin_dir/security.txt");

	@security = <SECURITY>;

	close (SECURITY);

	($security) = grep(/^$topic:/, @security);

	$security =~ s/\s+$//;

	($topic_number, $ip, $usrgrp, $modgrp, $password) = split(/:/, $security);

	&header;

	&ex('printuntil',1, 1, 0, "$L{AUTH_UNAUTHORIZED_HEADER}");

	print "<CENTER><FONT SIZE=4><B>$L{AUTH_UNAUTHORIZED_HEADER}</B></FONT></CENTER><HR>\n";

	print $L{AUTH_UNAUTHORIZED_MESSAGE}, "<P>";

	if ($this_ip_banned) {

		print "<P>\n";

		print $L{'AUTH_IP_BANNED'};

		for ($i = 3; $i <= 17; $i += 2) {

			&ex('printuntil', $i, $i, 0,"");

		}

		exit(0);

	}

	if ($ip ne "" && $usrgrp eq "" && $modgrp eq "" && $password eq "") {

		print "<P>\n";

		print $L{AUTH_IPONLY};

		for ($i = 3; $i <= 17; $i += 2) {

			&ex('printuntil', $i, $i, 0,"");

		}

		exit(0);

	}

	if ($usrgrp ne "" || $modgrp ne "") {

		print "<P>\n";

		print "<FORM ACTION=\"$script_url/board-auth.$cgi_extension\" METHOD=POST>\n";

		print "<P><TABLE WIDTH=100% BORDER=1><TR><TD>\n";

		print "<P><TABLE><TR><TD><FONT FACE=\"$face\" SIZE=\"$size\">$L{AUTH_USERNAME}</FONT></TD>";

		print "<TD>";

		print "<INPUT TYPE=TEXT NAME=username VALUE=\"";

		print $COOKIE{'user' . $COOKIE_ID} if $COOKIE{'user' . $COOKIE_ID} ne "";

		print "\" SIZE=15></TD><TD ROWSPAN=3>&nbsp;</TD><TD ROWSPAN=3 VALIGN=TOP><FONT FACE=\"$face\" SIZE=\"$size\">$L{AUTH_ACCOUNT}</TD></TR><TR><TD><FONT FACE=\"$face\" SIZE=\"$size\">";

		print "$L{AUTH_PASSWORD}</TD><TD>\n";

		print "<INPUT TYPE=PASSWORD NAME=password VALUE=\"";

		print $COOKIE{'rpwd' . $COOKIE_ID} if $COOKIE{'rpwd' . $COOKIE_ID} ne "";

		print "\" SIZE=15></TD></TR><TR><TD COLSPAN=2>\n";

		print "<INPUT TYPE=SUBMIT VALUE=\"$L{AUTH_LOGIN}\">\n";

		print "<INPUT TYPE=HIDDEN NAME=action VALUE=\"login\">\n";

		print "<INPUT TYPE=HIDDEN NAME=file VALUE=\"$FORM{'file'}\"></TD></TR></TABLE>\n";

		print "</TD></TR></TABLE></FORM><P>\n";

	}

	if ($password ne "") {

		print "<P>\n";

		print "<FORM ACTION=\"$script_url/board-auth.$cgi_extension\" METHOD=POST>\n";

		print "<P><TABLE WIDTH=100% BORDER=1><TR><TD>\n";

		print "<P><TABLE><TR><TD><FONT FACE=\"$face\" SIZE=\"$size\">";

		print "$L{AUTH_PASSWORD}</TD><TD>";

		print "<INPUT TYPE=PASSWORD NAME=special_password VALUE=\"\" SIZE=15></TD><TD ROWSPAN=2>&nbsp;</TD>";

		print "<TD ROWSPAN=2 VALIGN=TOP><FONT FACE=\"$face\" SIZE=\"$size\">$L{AUTH_SPECIAL_PASSWORD}</TD></TR><TR><TD COLSPAN=2>\n";

		print "<INPUT TYPE=SUBMIT VALUE=\"$L{AUTH_LOGIN}\">\n";

		print "<INPUT TYPE=HIDDEN NAME=action VALUE=\"login\">\n";

		print "<INPUT TYPE=HIDDEN NAME=file VALUE=\"$FORM{'file'}\"></TD></TR></TABLE>\n";

		print "</TD></TR></TABLE>\n";

		print "</FORM><P>\n";

	}

	if ($usrgrp eq "" && $modgrp eq "" && $password eq "") {

		print "<P>\n";

		print $L{'AUTH_DISCUSSION_CLOSED'};

	}

	for ($i = 3; $i <= 17; $i += 2) {

		&ex('printuntil', $i, $i, 0,"");

	}

	exit(0);

}

if ($file =~ m|\.$ext| && !$FORM{'type'}) {

	open (FILE, "$secdir$file");

	@file = <FILE>;

	close (FILE);

	foreach $line (@file) {

		while ($line =~ m|<A HREF="$message_url/(\d+)/(\d+)\.$ext([\?#]?)(\w*)|) {

			($tn, $pn, $qm, $lm) = ($1, $2, $3, $4);

			if (-e "$secdir/$tn/$pn.$ext") {

				$lmstr = "&lm=$lm";

				$line = join("", $`, "<A HREF=\"$script_url/board-auth\.$cgi_extension?file=/$tn/$pn.$ext$lmstr", $');

			} else {

				$line = join("", $`, "<a  href=\"$message_url/$tn/$pn.$ext", $');

			}

		}

		while ($line =~ m|<IMG SRC="$message_url/(\d+)/(\d+)\.(\w+)"|) {

			($tn, $gn, $format) = ($1, $2, $3);

			if (-e "$secdir/$tn/$gn.$format") {

				$line = join("", $`, "<IMG SRC=\"$script_url/board-auth.$cgi_extension?file=/$tn/$gn.$format\"", $');

			} else {

				$line = join("", $`, "<img  src=\"$message_url/$tn/$gn.$format", $');

			}

		}

		while ($line =~ m|<!-Attachment: ([^!]+)-!><CENTER><TABLE BORDER=1><TR><TD><IMG SRC="$html_url/icons/([^"]+)\.gif" ALIGN=LEFT ALT="([^"]+)">([^<]*)<BR><A HREF="$message_url/(\d+)/[^"]+"><B>([^<]+)</B></A> \(([^>]+)\)</TD></TR></TABLE></CENTER><!-/Attachment-!>|) {

			($file, $icon, $type, $descr, $topic, $fakefile, $size) = ($1, $2, $3, $4, $5, $6, $7);

			$line = join("", $`, "<CENTER><TABLE BORDER=1><TR><TD><IMG SRC=\"$html_url/icons/$icon\.gif\" ALIGN=LEFT>$descr<BR><A HREF=\"$script_url/board-auth.$cgi_extension?type=$type&file=/$topic/$file\"><B>$fakefile</B></A> ($size)</TD></TR></TABLE></CENTER>", $');

		}

	}

	&header;

	foreach $line (@file) {

		print $line;

	}

} elsif ($FORM{'type'}) {

	print "Content-type: $FORM{'type'}\n";

	$file =~ m|(.*)/|; $filename = $';

	if ($ENV{'HTTP_USER_AGENT'} =~ m|MSIE|) {

		print "\n";

	} else {

		print "Content-disposition: inline; filename=$filename\n\n";

	}

	binmode(STDOUT);

	open (FILE, "$secdir$file");

	binmode(FILE);

	@file = <FILE>;

	print @file;

	close (FILE);

} else {

	$fmt = "gif" if $file =~ m|\.gif|;

	$fmt = "jpeg" if $file =~ m|\.jpg|;

	&error_message("Unknown file format") if $fmt eq "";

	print "Content-type: image/$fmt\n";

	$file =~ m|(.*)/|; $filename = $';

	if ($ENV{'HTTP_USER_AGENT'} =~ m|MSIE|) {

		print "\n";

	} else {

		print "Content-disposition: inline; filename=$filename\n\n";

	}

	binmode(STDOUT);

	open (FILE, "$secdir$file");

	binmode(FILE);

	@file = <FILE>;

	print @file;

	close (FILE);

}

sub verify_owner {

	my ($owner, $username) = @_;

	my (@group_data, $group, $usernames, @username, $groupname);

	open (GROUP, "$admin_dir/groups.txt") || &error_message("File Error", "Cannot open group file (groups.txt)!");

	@group_data = <GROUP>;

	close (GROUP);

	foreach $group (@group_data) {

		chop ($group) if $group =~ /\n$/;

		($groupname, $usernames) = split(/:/, $group);

		if ($groupname eq $owner) {

			@username = split(/,/, $usernames);

			if (grep (/^$username$/, @username)) {

				return 1;

			} else {

				return 0;

			}

		}

	}

	return 0;

}

# END - FILE IS CORRECTLY UPLOADED #

