The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

SMB - A humble SMB network protocol implementation in Perl

SYNOPSIS

        # usage #1 - custom server
        use SMB::Server;

        my $server = SMB::Server->new(port => 10445);
        $server->run;


        # usage #2 - custom client
        use SMB::Client;

        my $client = SMB::Client->new('//10.0.2.2/test',
                username => 'test',
                password => 'secret',
                log_level => SMB::LOG_LEVEL_DEBUG,
        );
        my $tree = $client->connect_tree;

        # try another share if the first is not available
        $tree ||= $client->connect_tree(share => 'test2') or die;

        # list all txt files in the share
        for my $file ($tree->find("*.txt")) {
                printf "%-40s %9s %s\n",
                        $file->name, $file->size_string, $file->mtime_string;
        }

        # rotate, download and remove remote log
        use POSIX qw(strftime);
        my $new_filename = strftime("%Y%m%d-%H%M%S.log", localtime);

        $tree->rename("my.log", $new_filename) or die "Failed to rotate";
        $tree->dnload($new_filename) or die "Failed to download";
        $tree->remove($new_filename) or die "Failed to remove";

ABSTRACT

SMB is a network protocol created by Microsoft used to provide shared access to files. It stands for Server Message Block, also called CIFS - Common Internet File System.

This SMB framework in written in pure perl. It allows to receive and send SMB commands, implements authentication protocols used in SMB, provides an object model to conveniently work with local and remote files, trees and more. Some basic SMB server and client functionality is available.

The main purpose of this framework is to simplify creation of automatic tools for serving and fetching files using SMB protocol and for testing existing SMB server and client implementations.

DESCRIPTION

SMB-Perl library is an implementation of SMB protocol, including simple SMB client and server functionality.

SMB is a base class for many SMB::* classes. The following documents this super-class only. See corresponding sub-classes for their own description.

SMB class provides common logging and debugging functionality, and some sugar, like auto-created getter and setter methods for all object fields. It also defines some core SMB protocol constants, like status codes.

METHODS

new [FIELDS]

Class constructor. Creates an instance of the concrete class and initilizes it from FIELDS hash.

The sub-classes may omit a constructor, then this one is used, or they may overload it and call $class->SUPER::new(%options) to obtain the newly created object.

Common fields:

 * log_level - maximal log level to expose to the caller, per object

The default log_level value is 2 (INFO level). To disable logs emitted by the object, pass (log_level => 0) in its constructor.

The log levels are:

 * 0 (SMB::LOG_LEVEL_NONE)
 * 1 (SMB::LOG_LEVEL_ERROR)
 * 2 (SMB::LOG_LEVEL_INFO)
 * 3 (SMB::LOG_LEVEL_DEBUG)
 * 4 (SMB::LOG_LEVEL_TRACE)
log LEVEL FORMAT [ARGS]

This method is used for logging. The message is composed by "sprintf" FORMAT and ARGS, and is by default written to standard output.

The sub-classes may overload this method, to write logs to a file or implement a custom logic when and what to log.

The logging is enabled by default on at most INFO level, this is controllable by passing log_level in the constructor.

The error messages (LEVEL=1) are prefixed with "! ", the normal messages are prefixed with "* ".

err FORMAT [ARGS]

The same as log with LEVEL=1.

msg FORMAT [ARGS]

The same as log with LEVEL=2.

dbg FORMAT [ARGS]

The same as log with LEVEL=3.

trc FORMAT [ARGS]

The same as log with LEVEL=4.

mem BUFFER [LABEL] [LEVEL=4]

If the logging on TRACE (or other explicitely passed) level is enabled, logs a message containg LABEL and BUFFER size in bytes and then a nice memory dump, looking like:

 * NBSS + SMB Negotiate Request (216 bytes):
 000 | 00 00 00 d4 ff 53 4d 42  72 00 00 00 00 18 43 c8 | ___..SMB r____.C. |
 001 | 00 00 00 00 00 00 00 00  00 00 00 00 00 00 fe ff | ________ ______.. |
 002 | 00 00 00 00 00 b1 00 02  50 43 20 4e 45 54 57 4f | _____._. PC NETWO |
 003 | 52 4b 20 50 52 4f 47 52  41 4d 20 31 2e 30 00 02 | RK PROGR AM 1.0_. |
 004 | 4d 49 43 52 4f 53 4f 46  54 20 4e 45 54 57 4f 52 | MICROSOF T NETWOR |
 005 | 4b 53 20 31 2e 30 33 00  02 4d 49 43 52 4f 53 4f | KS 1.03_ .MICROSO |
 006 | 46 54 20 4e 45 54 57 4f  52 4b 53 20 33 2e 30 00 | FT NETWO RKS 3.0_ |
 007 | 02 4c 41 4e 4d 41 4e 31  2e 30 00 02 4c 4d 31 2e | .LANMAN1 .0_.LM1. |
 008 | 32 58 30 30 32 00 02 44  4f 53 20 4c 41 4e 4d 41 | 2X002_.D OS LANMA |
 009 | 4e 32 2e 31 00 02 4c 41  4e 4d 41 4e 32 2e 31 00 | N2.1_.LA NMAN2.1_ |
 00a | 02 53 61 6d 62 61 00 02  4e 54 20 4c 41 4e 4d 41 | .Samba_. NT LANMA |
 00b | 4e 20 31 2e 30 00 02 4e  54 20 4c 4d 20 30 2e 31 | N 1.0_.N T LM 0.1 |
 00c | 32 00 02 53 4d 42 20 32  2e 30 30 32 00 02 53 4d | 2_.SMB 2 .002_.SM |
 00d | 42 20 32 2e 3f 3f 3f 00                          | B 2.???_          |
dump

Returns a neat object's presentation as a multi-line string, like:

 SMB::v2::Command::Close {
     fid => [
         2,
         0,
     ],
     flags => 0,
     header => SMB::v2::Header {
         aid => 0,
         code => 6,
         credit_charge => 1,
         credits => 7802,
         flags => 0,
         log_level => 2,
         mid => 15,
         signature => [
             ("\x00") x 16,
         ],
         status => 0,
         struct_size => 24,
         tid => 2,
         uid => 1,
     },
     log_level => 2,
     name => "Close",
     openfile => undef,
     smb => 2,
 }

The returned string looks mostly as a valid perl with a minimal overhead. Huge arrays, hashes and strings are neatly cut with some info preserved about what was omitted.

FIELD
FIELD NEW_VALUE

For each field in the object, the method of this name is auto-created on demand. This method returns the field value if there are no arguments (getter) and sets NEW_VALUE if there is a single argument (setter).

FUNCTIONS

No functions are exported, so functions and constants (status codes and log levels) should be prefixed by the package namespace, like:

        print SMB::dump_value($nested_array);

        $status = SMB::STATUS_CANNOT_DELETE
                if $status == SMB::STATUS_ACCESS_DENIED;

        my $obj = SMBx::MyClass->new(log_level => SMB::LOG_LEVEL_DEBUG);
dump_value PERL_VALUE

Returns a neat PERL_VALUE presentation as a multi-line string.

Used by dump method.

dump_string STRING

Returns a printable STRING presentation as a one-line string. Long strings over 50 characters are cut, like "very lon..+8". Backslash and quote characters are prefixed with a backslash, and non printable or non-ascii characters are presented in hex, like "\x0a\xa0".

Used by dump_value function.

SEE ALSO

http://migo.sixbit.org/software/smb-perl/

LICENSE

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 3 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, see <http://www.gnu.org/licenses/>.

AUTHOR

Mikhael Goikhman <migo@cpan.org>