#!/usr/local/bin/perl
# view_users.cgi
# Display users connected to a share

require './samba-lib.pl';
&ReadParse();

# check acls
%access = &get_module_acl();
&error_setup("<blink><font color=red>$text{'eacl_aviol'}</font></blink>");
if ($in{share}) { # this may be cracked very easy, don't know how to do better :    # per-share acls ...
    # per-share acls ...
	&error("$text{'eacl_np'} $text{'eacl_pconn'}") 
		unless &can('rv',\%access, $in{share});
	}
else {
	&error("$text{'eacl_np'} $text{'eacl_pconn_all'}") 
		unless $access{'view_all_con'};
	} 
&ui_print_header(undef, $text{'viewu_index'}, "");

if (!&has_command($config{samba_status_program})) {
	print &text('viewu_ecmd', $config{'samba_status_program'}, "$gconfig{'webprefix'}/config.cgi?$module_name");
	print "<p>\n";
	&ui_print_footer("", $text{'index_sharelist'});
	exit;
	}

if ($in{share}) {
	print "<h3>",&text('viewu_list',"<tt>$in{share}</tt>"),"</h3>\n";
	@cons = &list_connections($in{'share'});
	}
else {
	@cons = &list_connections();
	}
@locks = &list_locks();

if (@cons) {
	print "<table border width=100%>\n";
	print "<tr $tb> <td><b>$text{'viewu_pid'}</b></td>\n";
	if (!$in{'share'}) { print "<td><b>$text{'viewu_share'}</b></td>\n"; }
	print "<td><b>$text{'viewu_user'}</b></td>\n";
	print "<td><b>$text{'viewu_group'}</b></td>\n";
	print "<td><b>$text{'viewu_from'}</b></td>\n";
	print "<td><b>$text{'viewu_time'}</b></td>\n";
	print "<td><b>$text{'viewu_locks'}</b></td> </tr>\n";
	foreach $c (@cons) {
		print "<tr $cb>\n";
		print "<td><a href=\"kill_user.cgi?share=$in{'share'}&pid=$c->[3]\">",&html_escape($c->[3]),"</a></td>\n";
		if (!$in{'share'})
			{ print "<td>",&html_escape($c->[0]),"</td>\n"; }
		print "<td>",&html_escape($c->[1]),"</td> <td>",
		      &html_escape($c->[2]),"</td>\n";
		print "<td>",&html_escape($c->[4]),"</td> <td>",
		      &html_escape($c->[5]),"</td>\n";
		print "<td>\n";
		@ulocks = grep { $_->[0] == $c->[3] } @locks;
		foreach $l (@ulocks) {
			print &html_escape($l->[4])," (",
			      &html_escape($l->[1]),")<br>\n";
			}
		print $text{'viewu_none'} if (!@ulocks);
		print "</td>\n";
		print "</tr>\n";
		}
	print "</table><p>\n";
	print $text{'viewu_msg1'}, "<br>\n";
	}
else {
	print "<b>$text{'viewu_msg2'}</b>.\n";
	}
print "<p>\n";

if ($in{share}) {
	&ui_print_footer($in{printer} ? "edit_pshare.cgi?share=$in{share}"
			     : "edit_fshare.cgi?share=$in{share}",
		$text{'index_shareconf'},
		"", $text{'index_sharelist'});
	}
else { &ui_print_footer("", $text{'index_sharelist'}); }

