3 # Workgroup list: file:/$LIB/smb.cgi
4 # Server list: file:/$LIB/smb.cgi?workgroup
5 # Sahre list: file:/$LIB/smb.cgi?//server
6 # file:/$LIB/smb.cgi/server
7 # Directory: file:/$LIB/smb.cgi?//server/share
8 # file:/$LIB/smb.cgi?//server/share/dir...
9 # file:/$LIB/smb.cgi/server/share
10 # Get file: file:/$LIB/smb.cgi?//server/share/dir.../file
11 # file:/$LIB/smb.cgi/server/share/dir.../file
13 # ----- ~/.w3m/smb -----
14 # workgroup = <workgroup>
15 # [ username = <username> ]
16 # [ password = <password> ]
17 # [ password_file = <password_file> ]
18 # ----------------------
19 # --- <password_file> ---
21 # -----------------------
24 # <password> = $PASSWD (Don't use!)
25 # <password_file> = $PASSWD_FILE
29 $MIME_TYPE = "~/.mime.types";
30 $AUTH_FILE = "~/.w3m/smb";
31 $MIME_TYPE =~ s@^~/@$ENV{"HOME"}/@;
32 $AUTH_FILE =~ s@^~/@$ENV{"HOME"}/@;
35 $PASSWD = $ENV{"PASSWD"};
36 $PASSWD_FILE = $ENV{"PASSWD_FILE"};
37 &load_auth_file($AUTH_FILE);
39 $NMBLOOKUP = "nmblookup";
40 $SMBCLIENT = "smbclient";
41 @NMBLOOKUP_OPT = ("-T");
42 @SMBCLIENT_OPT = ("-N");
43 $USE_OPT_A = defined($PASSWD) && (-f $AUTH_FILE) && &check_opt_a();
45 push(@SMBCLIENT_OPT, "-A", $AUTH_FILE);
46 } elsif (-f $PASSWD_FILE) {
48 } elsif (defined($PASSWD)) {
52 if (defined($PASSWD)) {
55 $DEBUG && print <<EOF;
56 DEBUG: NMBLOOKUP=$NMBLOOKUP @NMBLOOKUP_OPT
57 DEBUG: SMBCLIENT=$SMBCLIENT @SMBCLIENT_OPT
58 DEBUG: WORKGROUP=$WORKGROUP
61 DEBUG: PASSWD_FILE=$PASSWD_FILE
62 DEBUG: PASSWD_FD=$PASSWD_FD
68 $CGI = "file://" . &file_encode($ENV{"SCRIPT_NAME"} || $0);
69 $QUERY = $ENV{"QUERY_STRING"};
70 $PATH_INFO = $ENV{"PATH_INFO"};
72 if ($PATH_INFO =~ m@^/@) {
78 $DEBUG && print "DEBUG: PATH_INFO=\"$_\"\n";
82 $_ = &file_decode($QUERY);
83 $DEBUG && print "DEBUG: QUERY_STRING=\"$_\"\n";
88 # if (!$USE_OPT_A && !defined($PASSWD)) {
89 # &print_form("//$server$_");
93 &file_list("//$server/$1", &cleanup($_));
104 local($service, $file) = @_;
106 local($dir, $qservice, $qfile);
109 $DEBUG && print "DEBUG: service=\"$service\" file=\"$file\"\n";
115 @cmd = ($SMBCLIENT, $service, @SMBCLIENT_OPT, "-c", "ls \"$_\"");
116 $F = &open_pipe(1, @cmd);
118 $DEBUG && print "DEBUG: $_";
122 if (s/\s+([A-Z]*) {1,8}\d+ (\w{3} ){2}[ \d]\d \d\d:\d\d:\d\d \d{4}\s*$//
124 &get_file($service, $file);
131 @cmd = ($SMBCLIENT, $service, @SMBCLIENT_OPT, "-c", "ls \"$_\"");
132 $F = &open_pipe(1, @cmd);
135 $DEBUG && print "DEBUG: $_";
143 $qservice = &html_quote($service);
144 $service = &file_encode($service);
145 $qfile = &html_quote($file);
146 $file = &file_encode($file);
148 print "Content-Type: text/html\n\n";
149 print "<title>$qservice$qfile</title>\n";
150 print "<b>$qservice$qfile</b>\n";
153 s/\s+([A-Z]*) {1,8}\d+ (\w{3} ){2}[ \d]\d \d\d:\d\d:\d\d \d{4}\s*$// || next;
157 print "<a href=\"$CGI$Q$service"
158 . &cleanup("$file/" . &file_encode($_)) . "\">"
159 . &html_quote($_) . "</a>"
160 . &html_quote($c) . "\n";
166 local($service, $file) = @_;
167 local($encoding, $type);
172 @cmd = ($SMBCLIENT, $service, @SMBCLIENT_OPT, "-E", "-c", "more \"$_\"");
173 $DEBUG && print "DEBUG: @cmd\n";
175 ($encoding, $type) = &guess_type($file);
178 print "Content-Encoding: $encoding\n" if $encoding;
179 print "Content-Type: $type; name=\"$file\"\n\n";
181 $ENV{"PAGER"} = $PAGER if $PAGER;
188 local($qserver, $_, $d, @c);
190 @share = &get_list(1, $server, "Share");
192 $qserver = &html_quote($server);
193 $server = &file_encode($server);
195 print "Content-Type: text/html\n\n";
196 print "<title>Share list: $qserver</title>\n";
198 print "<tr><td colspan=3><b>$qserver</b>";
200 ($_, $d, @c) = split(" ");
202 print "<tr><td>+ <a href=\"$CGI$Q//$server/"
203 . &file_encode($_) . "\">"
204 . &html_quote($_) . "</a>";
210 . &html_quote($d) . "<td><td>"
211 . &html_quote("@c") . "\n";
218 local($master, @server);
221 $master = &get_master($group);
222 @server = &get_list(0, $master, "Server");
224 $group = &html_quote($group);
226 print "Content-Type: text/html\n\n";
227 print "<title>Server list: $group</title>\n";
229 print "<tr><td colspan=3><b>$group</b>\n";
231 ($_, @c) = split(" ");
232 print "<tr><td>+ <a href=\"$CGI$Q//"
233 . &file_encode($_) . "\">"
234 . &html_quote($_) . "</a><td><td>"
235 . &html_quote("@c") . "\n";
241 local($master, @group);
244 $master = &get_master($WORKGROUP || "-");
245 @group = &get_list(0, $master, "Workgroup");
247 print "Content-Type: text/html\n\n";
248 print "<title>Workgroup list</title>\n";
251 ($_, @c) = split(" ");
252 print "<tr><td><a href=\"$CGI?"
253 . &file_encode($_) . "\">"
254 . &html_quote($_) . "</a><td><td>"
255 . &html_quote("@c") . "\n";
263 @cmd = ($SMBCLIENT, "-h");
264 $F = &open_pipe(0, @cmd);
267 $DEBUG && print "DEBUG: $_";
280 @cmd = ($NMBLOOKUP, "-M", @NMBLOOKUP_OPT, $group);
281 $F = &open_pipe(0, @cmd);
285 ($_) = split(/[,\s]/);
291 local($passwd, $server, $header) = @_;
295 @cmd = ($SMBCLIENT, @SMBCLIENT_OPT, "-L", $server);
296 $F = &open_pipe($passwd, @cmd);
299 $DEBUG && print "DEBUG: $_";
305 $DEBUG && print "DEBUG: $_";
315 local($passwd, @cmd) = @_;
318 $DEBUG && print "DEBUG: @cmd\n";
319 open($F, "-|") || &exec_cmd($passwd, @cmd);
324 local($passwd, @cmd) = @_;
326 $ENV{"LC_ALL"} = "C";
327 $ENV{"USER"} = $USER;
328 if ($passwd && !$USE_OPT_A) {
329 if ($USE_PASSWD_FILE) {
330 $ENV{"PASSWD_FILE"} = $PASSWD_FILE;
331 } elsif ($USE_PASSWD_FD) {
332 $ENV{"PASSWD_FD"} = $PASSWD_FD;
340 open(STDERR, ">/dev/null");
347 local($q) = &html_quote($_);
348 $_ = &file_encode($_);
351 Content-Type: text/html
354 <form action="$CGI$Q$_" method=POST>
356 <tr><td>Workgroup <td>User <td>Password
357 <tr><td><input type=text size=8 name=group value="$WORKGROUP">
358 <td><input type=text size=8 name=user value="$USER">
359 <td><input type=password size=8 name=passwd value="$PASSWD">
360 <td><input type=submit name=OK value=OK>
369 if ($USER =~ s/%(.*)$//) {
370 $PASSWD = $1 unless $PASSWD;
372 open(F, $_) || return;
375 if (s/^workgroup\s*=\s*//i) {
377 } elsif (s/^user(name)?\s*=\s*//i) {
379 } elsif (s/^passw(or)?d\s*=\s*//i) {
381 } elsif (s/^passw(or)?d_file\s*=\s*//i) {
391 local($type, @suffix);
393 open(F, $_) || return ();
397 (($type, @suffix) = split(" ")) >= 2 || next;
408 local(%mime) = &load_mime_type($MIME_TYPE);
409 local($encoding) = undef;
413 } elsif (s/\.Z$//i) {
414 $encoding = "compress";
415 } elsif (s/\.bz2?$//i) {
421 return ($encoding, $mime{$_} || "text/plain");
440 s/[\000-\040\+:#?&%<>"\177-\377]/sprintf('%%%02X', unpack('C', $&))/eg;
447 s/%([\da-f][\da-f])/pack('C', hex($1))/egi;
460 s/[<>&"]/$QUOTE{$&}/g;