ConnectSMB also comes with a set of SMB Applications that can either be used as examples on how to integrate the SMB APIs into customer applications or they can be used as binaries that can be integrated as system calls into a deployed system.
The SMBCP applications are maintained as a separate git repo:
https://github.com/connectedway/smbcp.git
The smbcp applications require ConnectSMB SMB client libraries to be installed first. There are 4 applications:
The smbcp applications are dynamically linked. The applications themselves are built seperately from the ConnectSMB framework and they are simply linked with the framework on the target system.
The syntax and decription of the ConnectSMB SMB commands are describe here.
Copy a file to a destination file. The syntax of smbcp is:
$ smbcp [-a] <source> <destination>
Where -a signifies that the copy operation should be done asynchronously using multiple overlapped buffers. The absense of -a specifies that the copy is done synchronously. Only one I/O is outstanding at a time in synchronous mode.
The target file must be fully specified. In other words, you must provide the target file name. Simply specifying the target directory is not sufficient.
ConnectSMB can operate in one of two authentication mode: Active Directory and NTLM. Active Directory authentication requires a kerberos stack preinstalled on your target system and active domain controllers with users and target computers. Before running any of the smbcp applications in Active Directory mode, you must first obtain a user ticket. On linux this can be done with the kinit kerberos utility.
$ kinit <user>@<domain>
The utility will prompt for your domain password.
smbcp can copy a file from local or remote locations to a file that resides locally or remotely. A file specification is of the form:
[//username:password:domain@server/share/]path.../file
If authenticated with Active Directory (or samba ad/dc), you will not need to specify a username or password in the URL. And if using Active Directory the domain portion of the URL is used to specify the path to the kerberos ticket cache to use. If not specified, the default cache is used.
If authenticating with NTLM (this is called standalone authentication), the username and password is required. The domain portion is optional but allows the target to authenticate the user against the specified domain.
Path can be any depth as long as the total file path is less than 256 bytes. For example:
$ smbcp //me:secret@remote/share/subdirectory/picture.jpg ./picture.jpg
This command will access the server named 'remote' and log in using the username 'me' and the password 'secret'. It will implicitly mount the remote share named 'share' and will access the file 'subdirectory/picture.jpg' relative to the share. It will copy the file locally to the filename picture.jpg.
The source path and destination path can be either local or remote so copies from a remote to a local, from a local to a remote, from a local to a local, and from a remote to a remote are all possible.
Find the freespace of the target file system. The syntax of smbfree is:
$ smbfree <target>
Where target is a path that include a remote share. The format of the URL is the same as described in the smbcp command.
List the contents of a target directory. The syntax of smbls is:
$ smbls <directory>
Where directory is a path that is a path to a directory. The format of the URL is the same as described in the smbcp command.
Delete a target file. The syntax of smbrm is:
$ smbrm <path>
Where path is a path that is a path to a file. The format of the URL is the same as described in the smbcp command.
If you are building a yocto based distribution using the of_manifests repo configuration, the smbcp application will be built and included in your distribution.
If you are deploying ConnectSMB in a Linux environment, you will need to clone, build, and install smbcp on your Linux machine.
To clone the repo, simply issue:
$ git clone https://github.com/connectedway/smbcp.git $ cd smbcp
Note that if you wish to build the smbcp application, you will have had to successfully build and installed the ConnectSMB Framework. You can find more here and here.
Then simply issue:
$ make
You can install the application into your system directories by issuing:
$ sudo make install
The application will be install, by default, in /usr/local/bin/openfiles. You may wish to add this to your path variable:
$ export PATH=$PATH:/usr/local/bin/openfiles
Update your shell startup scripts accordingly
The following applications will be installed:
You can uninstall the application from your system directories by issuing:
$ sudo make uninstall
You can clean up build artifacts from your build directory by issuing:
$ make clean
You can run the application by following the description in this document.
The implementatoin of the smbcp application is relevant for integrating customer applications with the Linux based ConnectSMB framework.
The makefile is a basic make. The smbcp application consists of one object file for each utility: smbcp.o, smbfree.o, smbls.o and smbrm.o. The object file is compiled using default CFLAGS which includes a specification of the sysroot which will contain the necessary ConnectSMB header files. The executable is linked with five shared libraries:
The makefile, as written, links with the openssl crypto libraries. Open Files supports opensl, mbedtl, and gnutls. See the (see config) if you wish to uses a different crypto library and update this Makefile accordingly.
We specify the linker option --no-as-needed which directs the linker to include the libraries whether they are explicity referenced in the object files or not. This is needed if we wish to utilize implicit library constructors. As a conveniece to openfile application developers, we will initialize the stack implicitly upon library load rather than requiring the developer to initialize the stack within the application itself.
This readme will walk through the implemenation of the smbcp.c application. The other applications will have similar APIs and are not described here.
This readme will not go through line by line within the smbcp.c source file. Rather, we'll call out relevant sections.
An ConnectSMB application can use standard C libraries and with respect to smbcp, we use five. The wchar.h file is included so we can convert ASCII characters to wide characters used as part of the ConnectSMB API. NOTE that ConnectSMB exposes an ascii API as well but wide characters is recommended.
There are seven ConnectSMB header files used by this application. ConnectSMB provides a robust set of APIs for many services. We recommend viewing the /ref openiles documentation for more detail. The ConnectSMB recipe and makefiles installs a subset of the available APIs into the yocto sysroot. If you find that particular headers are not available in your sysroot, Connected Way support will be glad to export them for you.
A brief description of the headers used:
In the simple case, this is the full set of APIs needed to interact with the ConnectSMB framework. The main set of APIs is contained in the header file.h. For the most part, the file API of ConnectSMB is based on the Windows File APIs.
The smbcp.c source file contains code for both the asynchronous file copy and synchronous file copy modes.
The synchronous file copy is simple. It simply opens up the source and destination files and then enters a loop where a buffer is read from the read file, and written to the write file. The loop continues till an eof on the read file or an error on either. The entry point of the synchronous copy is:
static OFC_DWORD copy_sync(OFC_CTCHAR *rfilename, OFC_CTCHAR *wfilename)
Pseudo code for the simple file copy follows. Also see: smbcp.c
Begin
Open the Read File as read_file
Open the Write File as write_fil
while Read from read_file into buffer and length is True
Write to write_file from buffer and length
Close write_file
Close read_file
Done
The asynchronous code supports arbitrary queue depth of arbitrary buffer size (up to a max of OFC_MAX_IO). If you have questions on the operation of the asynchrous copy, please contact Connected Way support. The buffer management of the asynchronous copy is where most of the complication to this utility lies. The buffer management utilizes the ConnectSMB framework, thus the need for the ConnectSMB headers.
The entry point to the asynchronous file copy is
static OFC_DWORD copy_async(OFC_CTCHAR *rfilename, OFC_CTCHAR *wfilename)
The main entry point parses the arguments, converts the ascii file names into wide character file names and calls the respective asynchronous or synchronous entry points.
The APIs used by smbcp are:
APIs for Synchronous File Copy:
APIs for Asynchronous File Copy:
It is clear from the list of APIs used between synchronous and asynchronous file copy that the asynchronous mode is more complex but it can offer considerable performance improvements
This is the full list of APIs required for either mode. This should help in understanding the level of effort in integrating ConnectSMB with a customer application.
There are a few features of ConnectSMB which allow this simple set of APIs: