1#!/usr/local/bin/perl
2# smb_server.cgi
3# Called in a pop-up javascript window to display a list of known SMB
4# servers, by calling smbclient to request the browse list from some server
5
6require './mount-lib.pl';
7use Socket;
8&popup_header($text{'smb_choose'});
9print <<EOF;
10<script>
11function choose(f)
12{
13top.opener.ifield.value = f;
14window.close();
15}
16</script>
17EOF
18
19# call smbclient
20if ($config{'browse_server'} eq "*") {
21	# Get from workgroup
22	if ($config{'browse_group'}) {
23		# Find master for workgroup
24		$out = &backquote_command(
25			$config{'nmblookup_path'}." -N ".
26			$config{'browse_group'}." 2>&1 </dev/null");
27		if ($out =~ /(^|\n)([0-9\.]+)\s/) {
28			$host = $2;
29			}
30		else {
31			print "<b>",&text('smb_emaster', $config{'browse_group'}),"</b>\n";
32			exit;
33			}
34		}
35	else {
36		# No idea what to do
37		print "<b>",&text('smb_eworkgroup'),"</b>\n";
38		exit;
39		}
40	}
41elsif ($config{'browse_server'}) {
42	# Fixed host
43	$host = $config{'browse_server'};
44	}
45else {
46	# Poll local samba
47	$host = "localhost";
48	}
49&execute_command("$config{'smbclient_path'} -d 0 -L $host -N",
50		 undef, \$out, \$out);
51if ($?) {
52	print "<b>",&text('smb_elist', $host),"</b>\n";
53	exit;
54	}
55elsif ($out =~ /Unknown host/) {
56	print "<b>",&text('smb_ehost', $host),"</b>\n";
57	exit;
58	}
59elsif ($out =~ /error connecting|connect error/) {
60	print "<b>",&text('smb_edown', $host),"</b>\n";
61	exit;
62	}
63
64# parse server list
65if ($out =~ /Server\s+Comment\n.*\n((.+\n)+)/) {
66	@svlist = split(/\n/, $1);
67	foreach $sv (@svlist) {
68		if ($sv =~ /^\s+(\S+)\s*(.*)$/) {
69			push(@names, $1); push(@comms, $2);
70			}
71		}
72	}
73
74if (@names) {
75	print "<b>$text{'smb_sel'}</b><p>\n";
76	print &ui_columns_start([ $text{'smb_name'}, $text{'smb_desc'} ]);
77	for($i=0; $i<@names; $i++) {
78		print &ui_columns_row([
79            &ui_link("#", $names[$i], undef, "onClick='choose(\"$names[$i]\");return false;'"),
80			&html_escape($comms[$i])
81			]);
82		}
83	print &ui_columns_end();
84	}
85else {
86	print "<b>$text{'smb_none'}</b>.<p>\n";
87	}
88
89&popup_footer();
90
91
92