ewmscp  ..
davixCommon.cpp
Go to the documentation of this file.
1 #include "davixCommon.h"
2 #include <termios.h>
3 #include <throwcall.h>
4 
5 decltype(errorReport::withTiming) errorReport::withTiming('\0', "davixTimed",
6  "show davix cmd timing info");
7 
8 
9 
10 
11 
13  p12Path('\0', parPrefix + "X509credP12", "path to X509 cert in .p12 format"),
14  privKeyPath('\0', parPrefix + "X509privKey", "path to X509 key in .pem format"),
15  credentialPath('\0', parPrefix + "X509credPem", "path to X509 cert in .pem format"),
16  proxy('\0', parPrefix + "X509Proxy", "path to proxy credential") {
23 }
24 void davixCfgX509::applySingle(Davix::RequestParams& params, bool &accessConfigDone) {
25  if (p12Path.fIsSet() || privKeyPath.fIsSet()) {
26  std::string password;
27  davixCommon::getPassword("X509 certificate password:", password);
28  if (p12Path.fIsSet()) {
29  errorReport report(__func__, "load X509 from p12 file", p12Path);
30  if (credential.loadFromFileP12(p12Path, password, report)) {
31  report.throwUp();
32  }
33 
34  } else {
35  errorReport report(__func__, "load X509 from pem files", privKeyPath);
36  if (credential.loadFromFilePEM(privKeyPath, credentialPath, password, report)) {
37  report.throwUp();
38  }
39  }
40  params.setClientCertX509(credential);
41  accessConfigDone = true;
42  } else if (proxy.fIsSet()) {
43  errorReport report(__func__, "load X509 from proxy file", proxy);
44  if (credential.loadFromFilePEM(proxy, proxy, "", report)) {
45  report.throwUp();
46  }
47  params.setClientCertX509(credential);
48  accessConfigDone = true;
49  }
50 }
51 
52 
53 
54 
55 
56 
58  Protocol(parPrefix, "Protocol",
59  "set the request protocol (ex : Webdav, Http, S3) ",
60  &Davix::RequestParams::setProtocol, {
61  {"Auto", Davix::RequestProtocol::Protocol::Auto},
62  {"Http", Davix::RequestProtocol::Protocol::Http},
63  {"Webdav", Davix::RequestProtocol::Protocol::Webdav},
64  {"AwsS3", Davix::RequestProtocol::Protocol::AwsS3},
65  #ifdef DAVIX_API_VERSION
66  #if DAVIX_API_VERSION >= 20160621
67  {"Azure", Davix::RequestProtocol::Protocol::Azure},
68  {"Gcloud", Davix::RequestProtocol::Protocol::Gcloud},
69  #endif
70  #endif
71 }
72  ),
73  X509Cert(parPrefix),
74  CertificateAuthorityPath(parPrefix, "CertificateAuthorityPath",
75  "add to path list for CA directories",
76  &Davix::RequestParams::addCertificateAuthorityPath),
77  SSLCAcheck(parPrefix, "NoSSLCAcheck",
78  "disable the certificate authority validity check for the https request",
79  &Davix::RequestParams::setSSLCAcheck,
80  true),
81  OperationRetry(parPrefix, "OperationRetry",
82  "set number of retries",
83  &Davix::RequestParams::setOperationRetry),
84  OperationTimeout(parPrefix, "OperationTimeout",
85  "set the operation timeout",
86  &Davix::RequestParams::setOperationTimeout),
87  ConnectionTimeout(parPrefix, "ConnectionTimeout",
88  "set the connection timeout",
89  &Davix::RequestParams::setConnectionTimeout),
90  noAuth('\0',parPrefix + "NoAuth",parPrefix + " don't use authentification",false),
91  accessConfigDone(false) {
92 }
93 
94 void davixConfigObject::apply(Davix::RequestParams& params) {
95  Protocol.applySingle(params);
98  SSLCAcheck.applySingle(params);
102 }
104  return accessConfigDone;
105 }
106 
108  posix(&context) {
109  configObj.apply(params);
110  if ((! configObj.accessConfigured()) && (! configObj.noAuth)) {
111  params.setClientLoginPasswordCallback(loginCallback, this);
113  }
114 }
115 
116 int davixCommon::loginCallback(void* userdata,
117  const Davix::SessionInfo & /*info*/,
118  std::string & login,
119  std::string & password,
120  int count,
121  Davix::DavixError** /*err*/) {
122  return static_cast<davixCommon*>(userdata)->loginCallbackInner(login, password, count);
123 }
124 
125 void davixCommon::getPassword(const std::string& prompt, std::string & password) {
126  std::cerr << prompt;
127  std::cerr.flush();
128  struct termios old_term, new_term;
129  throwcall::good0(tcgetattr(0, &old_term), "tcgetattr on stdin");
130  new_term = old_term;
131  new_term.c_lflag &= ~ECHO;
132  throwcall::good0(tcsetattr(0, TCSAFLUSH, &new_term), "tcsetattr on stdin");
133  std::cin >> password;
134  throwcall::good0(tcsetattr(0, TCSAFLUSH, &old_term), "tcsetattr on stdin");
135  std::cerr << "\n";
136 }
137 
138 int davixCommon::loginCallbackInner(std::string & login,
139  std::string & password,
140  int count) {
141  if (count < 1) {
142  if (passWord.empty() || loginName.empty()) {
143  std::cerr << "Basic authentication - server is asking for username and password:\n";
144  }
145  } else {
146  std::cerr << "Authentication failure, try again:\n";
147  passWord.clear();
148  loginName.clear();
149  }
150  if (loginName.empty()) {
151  std::cerr << "Login: ";
152  std::cerr.flush();
153  std::cin >> login;
154  if (login.empty()) {
155  login = getenv("USER");
156  std::cerr << "using '" << login << "' as login name\n";
157  }
158  loginName = login;
159  } else {
160  login = loginName;
161  }
162  if (passWord.empty()) {
163  getPassword("Password:", password);
164  passWord = password;
165  } else {
166  password = passWord;
167  }
168  return 0;
169 }
170 
171 
172 
173 
174 bool davixCommon::pathExists(const std::string& path) {
175  errorReport report(__func__, "stat", path);
176  Davix::StatInfo statBuf;
177  if (posix.stat64(&params, path, &statBuf, report)) {
178  if (report->getStatus() == Davix::StatusCode::FileNotFound) {
179  return false;
180  }
181  report.throwUp();
182  }
183  return true;
184 };
185 std::unique_ptr<const genericStat> davixCommon::getStat(const std::string& path,
186  bool /*followLink*/) {
187  errorReport report(__func__, "stat", path);
188  Davix::StatInfo statBuf;
189  if (posix.stat64(&params, path, &statBuf, report)) {
190  report.throwUp();
191  }
192  auto stat = new genericStat();
193  auto prePathPart = path.find("//");
194  if (prePathPart != std::remove_reference<decltype(path)>::type::npos) {
195  prePathPart = path.find("/", prePathPart + 2);
196  }
197  stat->device = std::hash<std::string> {}(path.substr(0, prePathPart));
198  stat->size = statBuf.size;
199  stat->sizeOnDisk = statBuf.size;
200  stat->blksize = 16 * 1024 * 1024;
201  stat->aTime = genericStat::clock_type::time_point(std::chrono::seconds(statBuf.atime));
202  stat->mTime = genericStat::clock_type::time_point(std::chrono::seconds(statBuf.mtime));
203  stat->timeResolution = std::chrono::seconds(1);
204  stat->mode = statBuf.mode;
205  #ifdef DAVIX_API_VERSION
206  #if DAVIX_API_VERSION >= 20160621
207  stat->ownerUid = statBuf.owner;
208  stat->ownerGid = statBuf.group;
209  #endif
210  #endif
211  return std::unique_ptr<const genericStat>(stat);
212 };
213 
214 davixIoCommon::davixIoCommon(const std::string& aPath, davixCommon& aHandler) :
215  path(aPath),
216  handler(aHandler) {
217 }
218 
219 
220 std::unique_ptr<const genericStat> davixIoCommon::getStat() {
221  return handler.getStat(path, true);
222 }
223 
davixCommon::loginName
std::string loginName
Definition: davixCommon.h:200
davixCfgPar< struct timespec * >::applySingle
void applySingle(Davix::RequestParams &params)
Definition: davixCommon.h:105
davixCfgX509::credentialPath
options::single< std::string > credentialPath
Definition: davixCommon.h:159
davixCfgEnum::applySingle
void applySingle(Davix::RequestParams &params)
Definition: davixCommon.h:131
genericStat
generic stat abstraction class Used to abstract the variants of the stat structure.
Definition: genericStat.h:12
davixConfigObject::accessConfigured
bool accessConfigured() const
Definition: davixCommon.cpp:103
davixIoCommon::getStat
std::unique_ptr< const genericStat > getStat() override
Definition: davixCommon.cpp:220
davixCommon
Definition: davixCommon.h:193
davixCommon::loginCallback
static int loginCallback(void *userdata, const Davix::SessionInfo &info, std::string &login, std::string &password, int count, Davix::DavixError **err)
Definition: davixCommon.cpp:116
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
davixConfigObject::X509Cert
davixCfgX509 X509Cert
Definition: davixCommon.h:173
davixConfigObject::accessConfigDone
bool accessConfigDone
Definition: davixCommon.h:182
davixConfigObject::noAuth
options::single< bool > noAuth
Definition: davixCommon.h:180
davixCfgX509::applySingle
void applySingle(Davix::RequestParams &params, bool &accessConfigDone)
Definition: davixCommon.cpp:24
davixCommon::getPassword
static void getPassword(const std::string &prompt, std::string &password)
Definition: davixCommon.cpp:125
davixConfigObject::OperationTimeout
davixCfgPar< struct timespec * > OperationTimeout
Definition: davixCommon.h:177
davixCfgStrings::applySingle
void applySingle(Davix::RequestParams &params)
Definition: davixCommon.h:149
options::base::fForbid
virtual void fForbid(const base *aOtherOption)
forbid aOtherOption when this option is set
Definition: Options.cpp:617
davixConfigObject::Protocol
davixCfgEnum< const Davix::RequestProtocol::Protocol > Protocol
Definition: davixCommon.h:172
davixCfgX509::proxy
options::single< std::string > proxy
Definition: davixCommon.h:160
options::base::fRequire
virtual void fRequire(const base *aOtherOption)
require aOtherOption when this option is set
Definition: Options.cpp:611
davixConfigObject::OperationRetry
davixCfgPar< int > OperationRetry
Definition: davixCommon.h:176
errorReport
class for easy error handling with davix ensures proper cleanup of the error report when going out of...
Definition: davixCommon.h:12
davixIoCommon::handler
davixCommon & handler
Definition: davixCommon.h:227
davixCommon::passWord
std::string passWord
Definition: davixCommon.h:201
throwcall.h
errorReport::withTiming
static options::single< bool > withTiming
Definition: davixCommon.h:13
davixCommon.h
davixCfgX509::privKeyPath
options::single< std::string > privKeyPath
Definition: davixCommon.h:158
parPrefix
static const std::string parPrefix("dCap")
davixCommon::pathExists
bool pathExists(const std::string &path) override
Definition: davixCommon.cpp:174
davixCommon::posix
Davix::DavPosix posix
Definition: davixCommon.h:198
davixIoCommon::davixIoCommon
davixIoCommon(const std::string &aPath, davixCommon &aHandler)
Definition: davixCommon.cpp:214
davixConfigObject::CertificateAuthorityPath
davixCfgStrings< const std::string & > CertificateAuthorityPath
Definition: davixCommon.h:174
davixCfgX509::credential
Davix::X509Credential credential
Definition: davixCommon.h:161
davixCommon::davixCommon
davixCommon(davixConfigObject &configObj)
Definition: davixCommon.cpp:107
davixCfgPar::applySingle
void applySingle(Davix::RequestParams &params)
Definition: davixCommon.h:86
davixConfigObject::davixConfigObject
davixConfigObject(const std::string &parPrefix)
Definition: davixCommon.cpp:57
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40
davixConfigObject::ConnectionTimeout
davixCfgPar< struct timespec * > ConnectionTimeout
Definition: davixCommon.h:178
davixCfgX509::p12Path
options::single< std::string > p12Path
Definition: davixCommon.h:157
davixConfigObject::SSLCAcheck
davixCfgPar< bool > SSLCAcheck
Definition: davixCommon.h:175
davixCommon::params
Davix::RequestParams params
Definition: davixCommon.h:197
davixCommon::getStat
std::unique_ptr< const genericStat > getStat(const std::string &path, bool followLink) override
Definition: davixCommon.cpp:185
davixCfgX509::davixCfgX509
davixCfgX509(const std::string &parPrefix)
Definition: davixCommon.cpp:12
davixConfigObject
class for configuring one davix instance holds all options necessary for that
Definition: davixCommon.h:169
davixIoCommon::path
const std::string & path
Definition: davixCommon.h:226
davixCommon::loginCallbackInner
int loginCallbackInner(std::string &login, std::string &password, int count)
Definition: davixCommon.cpp:138
davixConfigObject::apply
void apply(Davix::RequestParams &params)
Definition: davixCommon.cpp:94
errorReport::throwUp
void throwUp()
Definition: davixCommon.h:45