ewmscp  ..
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
libsshCommon Class Reference

#include <libsshCommon.h>

Inheritance diagram for libsshCommon:
[legend]
Collaboration diagram for libsshCommon:
[legend]

Classes

class  sshOptions
 

Public Member Functions

void reconnectSessions ()
 
template<typename T >
std::unique_ptr< T, void(*)(T *)> getUniquePtr (T *obj, void(*deleter)(T *))
 
 libsshCommon (sshOptions &aOpt)
 
 ~libsshCommon () override
 
bool pathExists (const std::string &path) override
 
std::unique_ptr< const genericStatgetStat (const std::string &path, bool followLink) override
 
const sftp_statvfs_struct & getVfsStat (const std::string &path)
 
- Public Member Functions inherited from pathHandler
virtual ~pathHandler ()=default
 
virtual std::string getXattr (const std::string &, const std::string &)
 

Public Attributes

ssh_session session
 
sftp_session sftp
 

Protected Member Functions

void openSessions ()
 
void closeSessions ()
 

Protected Attributes

sshOptionsopt
 
bool reconnecting
 

Detailed Description

Definition at line 25 of file libsshCommon.h.

Constructor & Destructor Documentation

◆ libsshCommon()

libsshCommon::libsshCommon ( sshOptions aOpt)

Definition at line 113 of file libsshCommon.cpp.

113  : opt(aOpt), reconnecting(false) {
114  openSessions();
115 }

References openSessions().

Here is the call graph for this function:

◆ ~libsshCommon()

libsshCommon::~libsshCommon ( )
override

Definition at line 116 of file libsshCommon.cpp.

116  {
117  closeSessions();
118 }

References closeSessions().

Here is the call graph for this function:

Member Function Documentation

◆ closeSessions()

void libsshCommon::closeSessions ( )
protected

Definition at line 91 of file libsshCommon.cpp.

91  {
92  sftp_free(sftp);
93  sftp = nullptr;
94  ssh_disconnect(session);
95  ssh_free(session);
96  session = nullptr;
97 }

References session, and sftp.

Referenced by reconnectSessions(), and ~libsshCommon().

Here is the caller graph for this function:

◆ getStat()

std::unique_ptr< const genericStat > libsshCommon::getStat ( const std::string &  path,
bool  followLink 
)
overridevirtual

Implements pathHandler.

Definition at line 130 of file libsshCommon.cpp.

131  {
132  sftp_attributes stat;
133  if (followLink) {
134  timerInst(sftp_stat);
135  stat = sftp_stat(sftp, path.c_str());
136  } else {
137  timerInst(sftp_lstat);
138  stat = sftp_lstat(sftp, path.c_str());
139  }
140  if (stat == nullptr) {
141  if (sftp_get_error(sftp) == SSH_FX_NO_SUCH_FILE) {
142  return nullptr;
143  }
144  return nullptr;
145  throwcall::sftp::badval(nullptr, nullptr, *this, "can't stat ", path);
146  }
147  auto retval = new genericSftpStat(*stat, getVfsStat(path));
148  sftp_attributes_free(stat);
149  return std::unique_ptr<const genericStat>(retval);
150 };

References throwcall::sftp::badval(), getVfsStat(), sftp, and timerInst.

Here is the call graph for this function:

◆ getUniquePtr()

template<typename T >
std::unique_ptr<T, void(*)(T*)> libsshCommon::getUniquePtr ( T *  obj,
void(*)(T *)  deleter 
)
inline

Definition at line 45 of file libsshCommon.h.

45  {
46  return std::unique_ptr<T, void(*)(T*)>(obj, deleter);
47  };

Referenced by outputHandler::libssh::ensureParentDirs(), pathExists(), outputHandler::libssh::remove(), and outputHandler::libssh::rename().

Here is the caller graph for this function:

◆ getVfsStat()

const sftp_statvfs_struct & libsshCommon::getVfsStat ( const std::string &  path)

Definition at line 152 of file libsshCommon.cpp.

152  {
153  static std::map<std::string, sftp_statvfs_struct*> statMap;
154  auto lastSlashAt = path.find_last_of('/');
155  if (lastSlashAt == 0) {
156  lastSlashAt = std::string::npos;
157  }
158  auto dir = path.substr(0, lastSlashAt);
159  auto item = statMap.find(dir);
160  if (item == statMap.end()) {
161  timerInst(sftp_statvfs);
162  auto retval = sftp_statvfs(sftp, dir.c_str());
163  if (retval == nullptr) {
164  std::string msg(ssh_get_error(session));
165  if (msg.find("Operation unsupported")) {
166  static sftp_statvfs_struct fakeData;
167  fakeData.f_bsize = 4096;
169  dir, "statvfs", "not supported, using fake data");
170  auto result = statMap.emplace(dir, &fakeData);
171  item = result.first;
172  } else {
173  throwcall::sftp::badval(retval, nullptr, *this,
174  "can't stat fvs for path ", dir);
175  }
176  } else {
177  auto result = statMap.emplace(dir, retval);
178  item = result.first;
180  dir, "statvfs",
181  "bsize: ", retval->f_bsize,
182  " frsize:", retval->f_frsize,
183  " blocks:", retval->f_blocks,
184  " bfree:", retval->f_bfree,
185  " bavail:", retval->f_bavail,
186  " files:", retval->f_files,
187  " ffree:", retval->f_ffree,
188  " favail:", retval->f_favail,
189  " fsid:", retval->f_fsid,
190  " flag:", retval->f_flag,
191  " namemax:", retval->f_namemax);
192  }
193  }
194  return *(item->second);
195 }

References throwcall::sftp::badval(), errMsg::debug, errMsg::emit(), session, sftp, and timerInst.

Referenced by outputHandler::libssh::getMaxNameLength(), getStat(), libsshIoCommon::getStat(), and outputHandler::libssh::rename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ openSessions()

void libsshCommon::openSessions ( )
protected

Definition at line 42 of file libsshCommon.cpp.

42  {
43  session = throwcall::badval(ssh_new(), nullptr, "create new ssh session");
44  throwcall::good0(ssh_options_set(session, SSH_OPTIONS_HOST, opt.host.c_str()),
45  "can't set host");
46  if (opt.knownHostFile.fIsSet()) {
47  throwcall::ssh::good0(ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS,
48  opt.knownHostFile.c_str()), *this,
49  "can't add global known hosts file");
50  }
51 
52  throwcall::ssh::good0(ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY_STR,
53  opt.verbosity.c_str()),
54  *this, "set verbosity");
55  throwcall::good0(ssh_options_parse_config(session, opt.configFile.empty() ? nullptr : opt.configFile.c_str()),
56  "can't read ssh config file ", opt.configFile.empty() ? "(default)" : opt.configFile.c_str());
57  throwcall::ssh::goodval(ssh_connect(session), SSH_OK, *this,
58  "can't connect to ", opt.host);
59  #if LIBSSH_VERSION_MINOR > 7
60  switch (ssh_session_is_known_server(session)) {
61  case SSH_KNOWN_HOSTS_OK:
62  break;
63  case SSH_KNOWN_HOSTS_CHANGED:
64  throw std::runtime_error("host " + opt.host + " key changed");
65  case SSH_KNOWN_HOSTS_OTHER:
66  throw std::runtime_error("host " + opt.host + " not found but other key exists");
67  case SSH_KNOWN_HOSTS_NOT_FOUND:
68  throw std::runtime_error("known hosts file not found");
69  case SSH_KNOWN_HOSTS_UNKNOWN:
70  throw std::runtime_error("host " + opt.host + " is unknown");
71  case SSH_KNOWN_HOSTS_ERROR:
72  throw std::runtime_error(std::string("host check error ") + ssh_get_error(session));
73  }
74  #else
75  throwcall::ssh::goodval(ssh_is_server_known(session), SSH_SERVER_KNOWN_OK, *this,
76  "can't verify host ", opt.host);
77  #endif
78  if (ssh_userauth_gssapi(session) != SSH_AUTH_SUCCESS) {
79  if (ssh_userauth_publickey_auto(session, nullptr, nullptr) != SSH_AUTH_SUCCESS) {
80  if (ssh_userauth_agent(session, nullptr) != SSH_AUTH_SUCCESS) {
81  throw std::runtime_error("can't authenticate at " + opt.host);
82  }
83  }
84  }
86  "ssh", "connect", "sucessfully authenticated user");
87  sftp = throwcall::ssh::badval(sftp_new(session), nullptr, *this,
88  "create new sftp session");
89  throwcall::sftp::good0(sftp_init(sftp), *this, "can't init sftp session");
90 }

References throwcall::badval(), throwcall::ssh::badval(), libsshCommon::sshOptions::configFile, errMsg::debug, errMsg::emit(), options::base::fIsSet(), throwcall::good0(), throwcall::sftp::good0(), throwcall::ssh::good0(), throwcall::ssh::goodval(), libsshCommon::sshOptions::host, libsshCommon::sshOptions::knownHostFile, opt, session, sftp, and libsshCommon::sshOptions::verbosity.

Referenced by libsshCommon(), and reconnectSessions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pathExists()

bool libsshCommon::pathExists ( const std::string &  path)
overridevirtual

Implements pathHandler.

Definition at line 120 of file libsshCommon.cpp.

120  {
121  auto stat = getUniquePtr(sftp_lstat(sftp, path.c_str()), sftp_attributes_free);
122  if (stat == nullptr) {
123  if (sftp_get_error(sftp) == SSH_FX_NO_SUCH_FILE) {
124  return false;
125  }
126  throwcall::sftp::badval(nullptr, nullptr, *this, "can't stat ", path);
127  }
128  return true;
129 };

References throwcall::sftp::badval(), getUniquePtr(), and sftp.

Referenced by outputHandler::libssh::renameSimple().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reconnectSessions()

void libsshCommon::reconnectSessions ( )

Definition at line 98 of file libsshCommon.cpp.

98  {
99  if (reconnecting) {
101  "ssh", "reconnect", "error while reconnecting");
102  return;
103  }
104  reconnecting = true;
105  closeSessions();
107  "ssh", "reconnect", "trying reconnect");
108  openSessions();
109  reconnecting = false;
110 }

References closeSessions(), errMsg::debug, errMsg::emit(), openSessions(), and reconnecting.

Referenced by throwcall::sftp::badval(), throwcall::ssh::badval(), throwcall::sftp::good0(), throwcall::ssh::good0(), and throwcall::ssh::goodval().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ opt

sshOptions& libsshCommon::opt
protected

Definition at line 39 of file libsshCommon.h.

Referenced by openSessions().

◆ reconnecting

bool libsshCommon::reconnecting
protected

Definition at line 42 of file libsshCommon.h.

Referenced by reconnectSessions().

◆ session

ssh_session libsshCommon::session

◆ sftp

sftp_session libsshCommon::sftp

The documentation for this class was generated from the following files:
libsshCommon::session
ssh_session session
Definition: libsshCommon.h:27
errMsg::location
class for defining the location of a error message in the source code.
Definition: errMsgQueue.h:14
throwcall::badval
T badval(T call, t badvalue, const Args &... args)
template function to wrap system calls that return a special bad value on failure
Definition: throwcall.h:54
libsshCommon::opt
sshOptions & opt
Definition: libsshCommon.h:39
options::base::fIsSet
virtual bool fIsSet() const
check if this option was set, regardless of from command line or config file
Definition: Options.h:263
libsshCommon::reconnecting
bool reconnecting
Definition: libsshCommon.h:42
errMsg::level::debug
@ debug
libsshCommon::openSessions
void openSessions()
Definition: libsshCommon.cpp:42
libsshCommon::closeSessions
void closeSessions()
Definition: libsshCommon.cpp:91
libsshCommon::sshOptions::knownHostFile
options::single< std::string > knownHostFile
Definition: libsshCommon.h:34
throwcall::ssh::badval
T badval(T call, t badvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:120
libsshCommon::sshOptions::verbosity
options::single< std::string > verbosity
Definition: libsshCommon.h:36
errMsg::emit
void emit(level aLogLevel, const location &loc, const std::string &aObject, const std::string &aAction, const Args &... args)
function to create and enqueue a message, this is the only way that messages should be created!
Definition: errMsgQueue.h:148
timerInst
#define timerInst(subfunc)
Definition: timer.h:157
libsshCommon::getUniquePtr
std::unique_ptr< T, void(*)(T *)> getUniquePtr(T *obj, void(*deleter)(T *))
Definition: libsshCommon.h:45
libsshCommon::sftp
sftp_session sftp
Definition: libsshCommon.h:28
genericSftpStat
Definition: libsshCommon.h:16
libsshCommon::sshOptions::configFile
options::single< std::string > configFile
Definition: libsshCommon.h:35
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40
libsshCommon::sshOptions::host
options::single< std::string > host
Definition: libsshCommon.h:33
throwcall::sftp::badval
T badval(T call, t badvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:82
libsshCommon::getVfsStat
const sftp_statvfs_struct & getVfsStat(const std::string &path)
Definition: libsshCommon.cpp:152
throwcall::ssh::goodval
T goodval(T call, t goodvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:108
throwcall::ssh::good0
void good0(T call, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:97
throwcall::sftp::good0
void good0(T call, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:70