2012-08-05 13:08:29 -04:00
|
|
|
/* This is a sample implementation of a libssh based SSH server */
|
|
|
|
/*
|
|
|
|
Copyright 2003-2009 Aris Adamantiadis
|
|
|
|
|
|
|
|
This file is part of the SSH Library
|
|
|
|
|
|
|
|
You are free to copy this file, modify it in any way, consider it being public
|
|
|
|
domain. This does not apply to the rest of the library though, but it is
|
|
|
|
allowed to cut-and-paste working code from this file to any license of
|
|
|
|
program.
|
|
|
|
The goal is to show the API in action. It's not a reference on how terminal
|
|
|
|
clients must be made or how a client should react.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****
|
|
|
|
* Heavily Modified by Robert Fernie 2012... for retroshare project!
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef RS_SSHD_INTERFACE_H
|
|
|
|
#define RS_SSHD_INTERFACE_H
|
|
|
|
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
#include <libssh/server.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// From inside libretroshare.a
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
#include "rpcsystem.h"
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
#ifndef KEYS_FOLDER
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define KEYS_FOLDER
|
|
|
|
#else
|
|
|
|
#define KEYS_FOLDER "/etc/ssh/"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******
|
|
|
|
*
|
|
|
|
* Minimal Options to start with
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
//#define ALLOW_CLEARPWDS 1
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
class RsSshd;
|
|
|
|
extern RsSshd *rsSshd;
|
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
|
|
|
|
// TODO: NB: THIS FN DOES NOT USE A "SLOW" HASH FUNCTION.
|
|
|
|
// THE FIRST HALF OF THE HASH STRING IS THE SALT
|
|
|
|
int CheckPasswordHash(std::string pwdHashRadix64, std::string password);
|
|
|
|
int GeneratePasswordHash(std::string saltBin, std::string password, std::string &pwdHashRadix64);
|
|
|
|
int GenerateSalt(std::string &saltBin);
|
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
class RsSshd: public RsThread, public RpcComms
|
2012-08-05 13:08:29 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
// NB: This must be called EARLY before all the threads are launched.
|
2014-01-07 17:51:22 -05:00
|
|
|
static RsSshd *InitRsSshd(const std::string& portStr, const std::string &rsakeyfile);
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
// Interface.
|
|
|
|
int setRpcSystem(RpcSystem *s);
|
|
|
|
int adduserpwdhash(std::string username, std::string hash);
|
2012-08-05 13:08:29 -04:00
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
// RsThreads Interface.
|
|
|
|
virtual void run(); /* called once the thread is started */
|
2012-08-05 13:08:29 -04:00
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
// RsComms Interface.
|
|
|
|
virtual int isOkay();
|
2012-09-01 15:35:23 -04:00
|
|
|
virtual int error(uint32_t chan_id, std::string msg);
|
2012-08-09 09:45:05 -04:00
|
|
|
|
2012-09-01 15:35:23 -04:00
|
|
|
virtual int active_channels(std::list<uint32_t> &chan_ids);
|
|
|
|
virtual int recv_ready(uint32_t chan_id);
|
2012-08-20 10:59:41 -04:00
|
|
|
|
2012-09-01 15:35:23 -04:00
|
|
|
virtual int recv(uint32_t chan_id, uint8_t *buffer, int bytes);
|
|
|
|
virtual int recv(uint32_t chan_id, std::string &buffer, int bytes);
|
|
|
|
virtual int recv_blocking(uint32_t chan_id, uint8_t *buffer, int bytes);
|
|
|
|
virtual int recv_blocking(uint32_t chan_id, std::string &buffer, int bytes);
|
2012-08-20 10:59:41 -04:00
|
|
|
|
2012-09-01 15:35:23 -04:00
|
|
|
virtual int send(uint32_t chan_id, uint8_t *buffer, int bytes);
|
|
|
|
virtual int send(uint32_t chan_id, const std::string &buffer);
|
2012-08-20 10:59:41 -04:00
|
|
|
|
|
|
|
virtual int setSleepPeriods(float busy, float idle);
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
private:
|
2012-08-09 09:45:05 -04:00
|
|
|
RsSshd(std::string portStr); /* private constructor => so can only create with */
|
2012-08-05 13:08:29 -04:00
|
|
|
|
2012-10-19 09:22:51 -04:00
|
|
|
int init(const std::string &pathrsakey);
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
// High level operations.
|
|
|
|
int listenConnect();
|
|
|
|
int setupSession();
|
|
|
|
int interactive();
|
|
|
|
|
|
|
|
// Lower Level Operations.
|
|
|
|
int authUser();
|
|
|
|
int setupChannel();
|
|
|
|
int setupShell();
|
|
|
|
int doEcho();
|
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
// Terminal Handling!
|
2012-08-20 10:59:41 -04:00
|
|
|
//int doTermServer();
|
|
|
|
int doRpcSystem();
|
2012-08-09 09:45:05 -04:00
|
|
|
|
2012-08-05 13:08:29 -04:00
|
|
|
int cleanupSession();
|
|
|
|
int cleanupAll();
|
|
|
|
|
|
|
|
/* Password Checking */
|
2012-10-19 09:22:51 -04:00
|
|
|
int auth_password(const char *name, const char *pwd);
|
|
|
|
int auth_password_hashed(const char *name, const char *pwd);
|
2012-08-05 13:08:29 -04:00
|
|
|
#ifdef ALLOW_CLEARPWDS
|
|
|
|
int auth_password_basic(char *name, char *pwd);
|
|
|
|
#endif // ALLOW_CLEARPWDS
|
|
|
|
|
|
|
|
// DATA.
|
|
|
|
|
|
|
|
RsMutex mSshMtx;
|
2012-08-20 10:59:41 -04:00
|
|
|
|
|
|
|
uint32_t mBusyUSleep;
|
|
|
|
uint32_t mIdleUSleep;
|
2012-08-05 13:08:29 -04:00
|
|
|
|
|
|
|
uint32_t mState;
|
|
|
|
uint32_t mBindState;
|
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
std::string mPortStr;
|
2012-08-05 13:08:29 -04:00
|
|
|
ssh_session mSession;
|
|
|
|
ssh_bind mBind;
|
|
|
|
ssh_channel mChannel;
|
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
RpcSystem *mRpcSystem;
|
|
|
|
|
2012-08-05 13:08:29 -04:00
|
|
|
#ifdef ALLOW_CLEARPWDS
|
|
|
|
std::map<std::string, std::string> mPasswords;
|
|
|
|
#endif // ALLOW_CLEARPWDS
|
|
|
|
std::map<std::string, std::string> mPwdHashs;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|