diff --git a/minecraft/download.cgi b/minecraft/download.cgi index b0bdc18dd..970f593b4 100644 --- a/minecraft/download.cgi +++ b/minecraft/download.cgi @@ -9,13 +9,28 @@ our $server_jar_url; our $progress_callback_url; &ReadParse(); +# Validate inputs +if ($in{'new'}) { + $in{'dir'} =~ /^\/\S+$/ || &error($text{'download_edir'}); + getpwnam($in{'user'}) || &error($text{'download_euser'}); + } + &ui_print_unbuffered_header(undef, $text{'download_title'}, ""); -if ($in{'new'} && -d $config{'minecraft_dir'}) { +if ($in{'new'}) { + # Save the config + $config{'minecraft_dir'} = $in{'dir'}; + $config{'unix_user'} = $in{'user'}; + &save_module_config(\%config); + } + +if ($in{'new'} && !-d $config{'minecraft_dir'}) { # Create install dir print &text('download_mkdir', $config{'minecraft_dir'}),"
\n"; &make_dir($config{'minecraft_dir'}, 0755) || &error($text{'download_emkdir'}); + &set_ownership_permissions($config{'unix_user'}, undef, 0755, + $config{'minecraft_dir'}); } # Download to temp file @@ -35,8 +50,14 @@ if ($old_md5 eq $new_md5) { } else { ©_source_dest($temp, $jar); + &set_ownership_permissions($config{'unix_user'}, undef, undef, $jar); - print &text('download_done', $jar),"
\n"; + if ($in{'new'}) { + print &text('download_done2', $jar),"
\n"; + } + else { + print &text('download_done', $jar),"
\n"; + } if ($in{'new'}) { print $text{'download_start'},"
\n"; print &ui_form_start("start.cgi"); diff --git a/minecraft/edit_world.cgi b/minecraft/edit_world.cgi index 057e0faf5..998df8a72 100644 --- a/minecraft/edit_world.cgi +++ b/minecraft/edit_world.cgi @@ -23,7 +23,7 @@ else { } print &ui_form_start("save_world.cgi", "form-data"); -print &ui_table_start($text{'world_header'}, "width=50%", 2); +print &ui_table_start($text{'world_header'}, undef, 2); print &ui_hidden("new", $in{'new'}); if ($in{'new'}) { diff --git a/minecraft/index.cgi b/minecraft/index.cgi index a892ecc5f..26a9d0413 100644 --- a/minecraft/index.cgi +++ b/minecraft/index.cgi @@ -22,7 +22,13 @@ elsif ($err) { if (&has_command($config{'java_cmd'})) { print &ui_form_start("download.cgi"); print &ui_hidden("new", 1); - print &text('index_offer', $config{'minecraft_dir'}),"
\n"; + print $text{'index_offer'},"
\n"; + print &ui_table_start(undef, undef, 2); + print &ui_table_row($text{'index_dir'}, + &ui_textbox("dir", $config{'minecraft_dir'}, 40)); + print &ui_table_row($text{'index_user'}, + &ui_user_textbox("user", "nobody")); + print &ui_table_end(); print &ui_form_end([ [ undef, $text{'index_install'} ] ]); } else { diff --git a/minecraft/lang/en b/minecraft/lang/en index 0184d011b..2bb588d20 100644 --- a/minecraft/lang/en +++ b/minecraft/lang/en @@ -2,7 +2,9 @@ index_title=Minecraft Server index_cerr=An error was detected with your Minecraft server : $1. You may need to adjust the module configuration. index_cerr2=The Minecraft server does not appear to be installed on your system. If it is already running, you will need to adjust the module configuration to use the correct paths. index_nojava=Also, the Java command at $1 needed to run Minecraft was not found. -index_offer=Alternatively, Webmin can automatically download and install the Minecraft server for you, into the directory $1. +index_offer=Alternatively, Webmin can automatically download and install the Minecraft server for you. +index_dir=Install into directory +index_user=Run as Unix user index_install=Download and Install index_stop=Stop Minecraft Server index_stopdesc=Click this button to stop the running Minecraft server. All clients will be immediately disconnected. @@ -243,6 +245,9 @@ download_restart=The new version will only be used when the server is restarted. download_start=The server must be started before it can be used. download_mkdir=Creating base directory $1 .. download_emkdir=Directory creation failed! +download_err=Failed to setup Minecraft server +download_edir=Missing or non-absolute install directory +download_euser=User to run as does not exist log_conf=Changed server configuration log_stop=Stopped Minecraft server diff --git a/minecraft/list_worlds.cgi b/minecraft/list_worlds.cgi index ecf2d6c32..f1df3bfca 100644 --- a/minecraft/list_worlds.cgi +++ b/minecraft/list_worlds.cgi @@ -40,7 +40,9 @@ if (@worlds) { print &ui_columns_end(); print &ui_links_row(\@links); print &ui_form_end([ [ undef, $text{'worlds_change'} ], - [ 'apply', $text{'worlds_change2'} ] ]); + &is_minecraft_server_running() ? + ( [ 'apply', $text{'worlds_change2'} ] ) : + ( ) ]); } else { print "$text{'worlds_none'}
\n"; diff --git a/minecraft/minecraft-lib.pl b/minecraft/minecraft-lib.pl index 33af58f60..de935afd8 100644 --- a/minecraft/minecraft-lib.pl +++ b/minecraft/minecraft-lib.pl @@ -1,8 +1,8 @@ # Functions for editing the minecraft config # -# XXX plugins? # XXX run as Unix user -# XXX initial setup mode +# XXX which user to write PID as? +# XXX how to edit init script java args BEGIN { push(@INC, ".."); }; use strict; @@ -31,6 +31,23 @@ my $jar = $config{'minecraft_jar'} || return undef; } +# is_minecraft_port_in_use() +# If any server is using the default Minecraft port or looks like it is running +# minecraft_server.jar, return the PID. +sub is_minecraft_port_in_use +{ +&foreign_require("proc"); +my ($pid) = &proc::find_socket_processes("tcp:25565"); +return $pid if ($pid); +my @procs = &proc::list_processes(); +foreach my $p (@procs) { + if ($p->{'args'} =~ /^java.*minecraft_server.jar/) { + return $p->{'pid'}; + } + } +return undef; +} + # is_minecraft_server_running() # If the minecraft server is running, return the PID sub is_minecraft_server_running @@ -142,6 +159,9 @@ my $rv = "(test -e ".$ififo." || mkfifo ".$ififo.") ; ". " -jar ".$jar." nogui ". $config{'jar_args'}." ". ">> server.out 2>&1 )"; +if ($config{'unix_user'} ne 'root') { + $rv = &command_as_user($config{'unix_user'}, 0, $rv); + } return $rv; } @@ -174,6 +194,7 @@ my $fh = "PID"; &open_tempfile($fh, ">$pidfile"); &print_tempfile($fh, $pid."\n"); &close_tempfile($fh); +&set_ownership_permissions($config{'unix_user'}, undef, undef, $pidfile); return undef; } @@ -392,6 +413,8 @@ my ($users) = @_; my $lref = &read_file_lines(&get_whitelist_file()); @$lref = @$users; &flush_file_lines(&get_whitelist_file()); +&set_ownership_permissions($config{'unix_user'}, undef, undef, + &get_whitelist_file()); } sub get_op_file @@ -415,6 +438,8 @@ my ($users) = @_; my $lref = &read_file_lines(&get_op_file()); @$lref = @$users; &flush_file_lines(&get_op_file()); +&set_ownership_permissions($config{'unix_user'}, undef, undef, + &get_op_file()); } # list_worlds() diff --git a/minecraft/save_world.cgi b/minecraft/save_world.cgi index f9b9412de..85f9e6039 100644 --- a/minecraft/save_world.cgi +++ b/minecraft/save_world.cgi @@ -31,6 +31,8 @@ if ($in{'new'}) { my $fh = "EMPTY"; &open_tempfile($fh, ">$dir/level.dat", 0, 1); &close_tempfile($fh); + &set_ownership_permissions($config{'unix_user'}, undef, 0755, + $dir, "$dir/level.dat"); } elsif ($in{'src'} == 1) { # Clone existing world @@ -42,6 +44,8 @@ if ($in{'new'}) { } ©_source_dest("$config{'minecraft_dir'}/$in{'world'}", $dir); + &system_logged( + "chown -R $config{'unix_user'} ".quotemeta($dir)); if (&is_minecraft_server_running() && $def eq $in{'world'}) { # Re-enable world writes @@ -70,6 +74,8 @@ if ($in{'new'}) { &error($text{'world_edat'}); my $copysrc = $1; ©_source_dest($copysrc, $dir); + &system_logged( + "chown -R $config{'unix_user'} ".quotemeta($dir)); } &redirect("list_worlds.cgi"); }