0 votes
by (150 points)
edited by

Hi.
Can I add user to sftp server from client if I use your library.
I need to add user only in client side.

Applies to: Rebex SFTP, File Server

1 Answer

0 votes
by (73.6k points)
selected by
 
Best answer

If you want to add new user using a general SFTP client, it is not possible, because SFTP protocol has no such command.

However, you can implement your custom server command (e.g. users) and then use any SSH client to invoke that command.

The custom command implementation can look like this:

private void CustomCommandUsers(FileServer server, ShellCommandEventArgs e)
{
    // check whether current user can manage users
    if (e.User.Name != "admin")
    {
        e.WriteLine("Only 'admin' can manage users.");
        return;
    }

    // when no arguments were specified, display help
    if (e.Arguments.Length < 1)
    {
        e.WriteLine("Options:");
        e.WriteLine("-l           - Lists all users.");
        e.WriteLine("-r <name>    - Removes specified user.");
        e.WriteLine("-a <name> <pwd> <path>  - Adds new user.");
        e.WriteLine();
        return;
    }

    FileServerUser user;
    switch (e.Arguments[0])
    {
        // list all users
        case "-l":
            foreach (var u in server.Users)
            {
                e.WriteLine(u.Name);
            }
            break;

        // remove specified user
        case "-r":
            if (e.Arguments.Length < 2)
            {
                e.WriteLine("Argument <name> required.");
                return;
            }

            user = server.Users[e.Arguments[1]];
            if (user == null)
            {
                e.WriteLine("No such user '{0}'.", e.Arguments[1]);
            }
            else
            {
                server.Users.Remove(user);
                e.WriteLine("User '{0}' removed.", user.Name);
            }
            break;

        // add specified user
        case "-a":
            if (e.Arguments.Length < 4)
            {
                e.WriteLine("Arguments <name>, <password> and <virtualRootPath> required.");
                return;
            }

            user = server.Users[e.Arguments[1]];
            if (user != null)
            {
                e.WriteLine("User '{0}' already exists.", user.Name);
            }
            else
            {
                user = new FileServerUser(e.Arguments[1], e.Arguments[2], e.Arguments[3]);
                server.Users.Add(user);
                e.WriteLine("User '{0}' added.", user.Name);
            }
            break;

        // unknown option
        default:
            e.WriteLine("Unknown option '{0}'.", e.Arguments[0]);
            break;
    }
}

To enable remote command, you have to add FileServerProtocol.Shell to the server.

So the CustomCommandUsers can be used like this:

// create server instance
var server = new FileServer();

// bind SFTP and SSH shell to port 22
server.Bind(22, FileServerProtocol.Shell);
server.Bind(22, FileServerProtocol.Sftp);

// add server keys 
// ...

// add various defined users (e.g. admin accounts)
server.Users.Add(new FileServerUser("admin", "root", "c:/data"));

// register custom 'users' command
server.ShellCommand += (sender, e) =>
{
    if (e.Command == "users")
    {
        CustomCommandUsers(server, e);
    }
};

// start server in the background
server.Start();

You can use any client to invoke the users command (e.g. PuTTY). Using Rebex Terminal emulation, you can do it easily like this:

using (var ssh = new Ssh())
{
    ssh.Connect("localhost");
    ssh.Login("admin","root");
    string response = ssh.RunCommand("users -a tester test c:/data");
    Console.WriteLine(response);
}
...