# 
# SMArT
# 
# Apply changes
# 
# Copyright 2001 Wilmer van der Gaast (lintux@lintux.cx)
# 
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#

sub handle_request()
{
	if( $c[1] eq 'general' )
	{
		setc( 2, server_name );
		setc( 3, internal_net );
		setc( 16, test );
		setc( 210, timing_down );
		setc( 211, timing_warn );
		delconfigline( 6 );
		if( $p{version} ne '' )
		{
			if( $p{burst_enabled} eq 'on' )
				{ addconfigline( '6 ' . $p{version} . ' 1' ); }
			else
				{ addconfigline( '6 ' . $p{version} . ' 0' ); }
		}
		delconfigline( 30 );
		if( $p{burst_read} ne '' )
			{ addconfigline( '30 ' . $p{burst_read} . ' ' . $p{burst_write} ); }
	}
	elsif( $c[1] eq 'dirs' )
	{
		setc( 40, path_cache );
		setc( 41, share_lock );
		setc( 42, spool );
		setc( 45, bindery );
		setc( 46, attribute );
		setc( 47, trustee );
	}
	elsif( $c[1] eq 'configh' )
	{
		setc( 60, max_conn );
		setc( 61, max_vol );
		setc( 63, max_dirbase );
		if( $p{mmap} eq 'on' )
			{ $p{mmap} = 1;	}
		else
			{ $p{mmap} = 0; }
		setc( 68, mmap );
		setc( 69, sap );
		setc( 70, net_serial );
		setc( 71, net_app );
	}
	elsif( $c[1] eq 'security' )
	{
		setc( 7, encrypt );
		delconfigline( 9 );
		if( $p{creat_dir} ne '' )
			{ addconfigline( '9 ' . $p{creat_dir} . ' ' . $p{creat_file} ); }
	}
	elsif( $c[1] eq 'susers' )
	{
		setc( 10, guest_group );
		setc( 11, guest_user );
		delconfigline( 12 );
		if( ( $p{root_user} ne '' ) and ( $p{root_name} ne '' ) )
			{ addconfigline( '12 ' . $p{root_name} . ' ' . $p{root_user} . ' ' . $p{root_password} ); }
		delconfigline( 15 );
		if( $p{map} ne '' )
			{ addconfigline( '15 ' . $p{map} . ' ' . $p{map_password} ); }
	}
	elsif( $c[1] eq 'volumes' )
	{
		if( $c[2] ne '' )
		{
			delconfigline( '1 ' . $c[2] );
		}
		if( $p{name} ne '' )
		{
			cbc( 'm' );
			cbc( 'n' );
			cbc( 'o' );
			cbc( 'p' );
			cbc( 'r' );
			cbc( 'O' );
			cbc( 'N' );
			addconfigline( '1 ' . $p{name} . ' ' . $p{path} . ' ' . $p{case} . $p{trustee} . $p{m} . $p{n} . $p{o} . $p{p} . $p{r} . $p{O} . $p{N} );
		}
		redirect( '/settings/volumes' );
	}
	elsif( $c[1] eq 'devices' )
	{
		if( $c[2] ne '' )
		{
			delconfigline( '4 ' . $c[2] );
		}
		if( defined( $p{interface_manual} ) && $p{interface_manual} ne '' )
		{
			$p{interface} = $p{interface_manual};
		}
		$p{interface} =~ s/[^-_\.A-Za-z0-9:\*]//g;
		if( $p{number} ne '' )
		{
			addconfigline( '4 ' . $p{number} . ' ' . $p{interface} . ' ' . $p{frametype} . ' ' . $p{delay} );
		}
		redirect( '/settings/devices' );
	}
	elsif( $c[1] eq 'logging' )
	{
		setc( 100, ipx );
		setc( 101, nwserv );
		setc( 102, ncpserv );
		setc( 103, nwconn );
		setc( 104, nwclient );
		setc( 105, nwbind );
		setc( 106, nwrouted );
		setc( 200, startup );
		setc( 201, general_log );
		delconfigline( 202 );
		addconfigline( 202 . ' ' . ( $p{g_creat} + 2 * $p{error} ) );
		setc( 300, routing_interval );
		setc( 301, routing_log );
		delconfigline( 302 );
		addconfigline( 302 . ' ' . ( $p{r_creat} + 2 * $p{r_file} ) );
	}
	elsif( $c[1] eq 'smart' )
	{
		if( $p{mars_config} ne $mars_config )
		{
			# Just append the line. Messy but easy. ;)
			open( FILE, '>>' . $smart_conf_path ) or die "Could not open $smart_conf_path: $!";
			print( FILE "\n" . '$mars_config = \'' . $p{mars_config} . '\';' . "\n" );
			close( FILE );
		}
		
		open( FILE, '>' . $smart_nwclient_path ) or die "Could not open $smart_nwclient_path: $!";
		print( FILE $p{bind_server} . '/' . $p{bind_user} . ' ' . $p{bind_pass } . "\n" );
		close( FILE );
		
		chown( scalar( getpwnam( $nonroot_user ) ), 0, $smart_nwclient_path );
		chmod( 0600, $smart_nwclient_path );
	}
	elsif( $c[1] eq 'users' )
	{
		drop_root();
		
		$server = get_server();
		if( $c[2] eq 'add_new' )
		{
			system( 'nwbocreate', '-S', $server, '-t', '1', '-o', $p{name} );
			$c[2] = $p{name};
		}
		else
		{
			system( 'nwbprm', '-S', $server, '-t', '1', '-o', $c[2], '-p', 'UNIX_USER' );
			system( 'nwbprm', '-S', $server, '-t', '1', '-o', $c[2], '-p', 'IDENTIFICATION' );
			system( 'nwbprm', '-S', $server, '-t', '1', '-o', $c[2], '-p', 'GROUPS_I\'M_IN' );
			system( 'nwbprm', '-S', $server, '-t', '1', '-o', $c[2], '-p', 'SECURITY_EQUALS' );
		}
		
		$id = ( split( ' ', ( grep( /^$c[2] /, split( "\n", `nwbols -S $server` ) ) )[0] ) )[1];	# Woohoo... ;)
		$id =~ s/[^ ][^ ]/$& /g;
		$id = join( '', reverse( split( ' ', $id ) ) );
#		$id =~ s/ //g;
		
		if( scalar( keys( %p ) ) == 0 )
		{
			system( 'nwborm', '-S', $server, '-t', '1', '-o', $c[2] );
		}
		else
		{
			write_property_string( $c[2], 1, 'UNIX_USER', 0, 30, $p{unix_user} );
			write_property_string( $c[2], 1, 'IDENTIFICATION', 0, 30, $p{fullname} );
			
			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0001
$c[2]
GROUPS_I'M_IN
2
31
EOF
			@g = grep( /^group_/, keys( %p ) );
			foreach $g ( @g )
			{
				$g =~ s/^group_//;
				print( FILE '0002' . "\n", $g . "\n" );

				system( 'nwbpadd -S ' . $server . ' -o ' . $g . ' -t 2 -p GROUP_MEMBERS ' . $id . ' > /dev/null' );
			}
			close( FILE );
			
			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0001
$c[2]
SECURITY_EQUALS
2
31
EOF
			@g = grep( /^group_/, keys( %p ) );
			foreach $g ( @g )
			{
				$g =~ s/^group_//;
				print( FILE '0002' . "\n", $g . "\n" );
			}
			close( FILE );
			
			if( $p{password} ne '' )
			{
				$p = get_bindery_password();
				open( FILE, '|' . 'nwpasswd -O ' . $c[2] . ' > /dev/null' );
				print( STDERR 'nwpasswd -O ' . $c[2] . ' > /dev/null' . "\n" );
				print( FILE $p . "\n" . $p{password} . "\n" . $p{password} . "\n" );
				close( FILE );
			}
		}
		
		redirect( '/settings/users' );
	}
	elsif( $c[1] eq 'groups' )
	{
		drop_root();
		
		$server = get_server();
		if( $c[2] eq 'add_new' )
		{
			system( 'nwbocreate', '-S', $server, '-t', '2', '-o', $p{name} );
			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0002
$p{name}
GROUP_MEMBERS
2
31
EOF
			close( FILE );
			$c[2] = $p{name};
		}
		else
		{
			system( 'nwbprm', '-S', $server, '-t', '2', '-o', $c[2], '-p', 'IDENTIFICATION' );
			system( 'nwbprm', '-S', $server, '-t', '2', '-o', $c[2], '-p', 'OBJ_SUPERVISORS' );
		}
		
		if( scalar( keys( %p ) ) == 0 )
		{
			system( 'nwborm', '-S', $server, '-t', '2', '-o', $c[2] );
		}
		else
		{
			write_property_string( $c[2], 2, 'IDENTIFICATION', 0, 30, $p{fullname} );
			
			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0002
$c[2]
OBJ_SUPERVISORS
2
31
EOF
			@g = grep( /^man_/, keys( %p ) );
			foreach $g ( @g )
			{
				my @g = split( '_', $g );
				
				print( FILE $g[1] . "\n", $g[2] . "\n" );
			}
			close( FILE );
		}
		
		redirect( '/settings/groups' );
	}
	elsif( $c[1] eq 'queues' )
	{
		drop_root();
		
		$server = get_server();
		if( $c[2] eq 'add_new' )
		{
			system( 'nwbocreate', '-S', $server, '-t', '3', '-o', $p{name} );
			$c[2] = $p{name};

			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0003
$c[2]
Q_SERVERS
2
31
EOF
			close( FILE );

			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0003
$c[2]
Q_USERS
2
31
0002
EVERYONE
EOF
			close( FILE );

			open( FILE, '|' . 'nwbpset -S ' . $server );
			print FILE <<EOF;
0003
$c[2]
Q_OPERATORS
2
31
0001
SUPERVISOR
EOF
			close( FILE );
		}
		else
		{
			system( 'nwbprm', '-S', $server, '-t', '3', '-o', $c[2], '-p', 'Q_UNIX_PRINT' );
			system( 'nwbprm', '-S', $server, '-t', '3', '-o', $c[2], '-p', 'Q_DIRECTORY' );
		}
		
		if( scalar( keys( %p ) ) == 0 )
		{
			system( 'nwborm', '-S', $server, '-t', '3', '-o', $c[2] );
		}
		else
		{
			
			if( $p{spool_dir} eq '' )
			{
				$id = ( split( ' ', ( grep( /^$c[2] /, split( "\n", `nwbols -S $server` ) ) )[0] ) )[1];	# Woohoo... ;)
				$p{spool_dir} = 'SYS:SYSTEM/' . $id . '.QDR';
			}
			
			write_property_string( $c[2], 3, 'Q_UNIX_PRINT', 0, 31, $p{unix_print} );
			write_property_string( $c[2], 3, 'Q_DIRECTORY', 0, 31, $p{spool_dir} );
		}
		
		redirect( '/settings/queues' );
	}
	else
	{
		putp();
		return( 0 );
	}
	redirect( '/static/start.html' );
	
	writeconfig();
}

sub putp()
{
	$cc =~ s/.*\?//;
	$cc =~ s/&/\n/g;
	print <<EOF;
HTTP/1.0 200 OK
Content-Type: text/plain

$cc
EOF
}

sub setc( $$ )
{
	delconfigline( $_[0] );
	if( $p{$_[1]} ne '' )
		{ addconfigline( $_[0] . ' ' . $p{$_[1]} ); }
}

sub cbc( $ )
{
	if( $p{$_[0]} ne '' )
		{ $p{$_[0]} = $_[0]; }
}

