Guitar
Quissh.h
Go to the documentation of this file.
1 #ifndef QUISSH_H
2 #define QUISSH_H
3 #ifdef UNSAFE_ENABLED
4 
5 #include <functional>
6 #include <optional>
7 #include <string>
8 #include <variant>
9 #include <stdint.h>
10 #include <set>
11 
12 class Quissh {
13  friend class DIR;
14 public:
15  struct PasswdAuth {
16  std::string uid;
17  std::string pwd;
18  };
19 
20  struct PubkeyAuth {
21  };
22 
23  using AuthVar = std::variant<PasswdAuth, PubkeyAuth>;
24 
25  struct Auth;
26 
27  struct FileAttribute {
28  std::string name;
29  std::string longname;
30  uint32_t flags = 0;
31  uint8_t type = 5;//SSH_FILEXFER_TYPE_UNKNOWN;
32  uint64_t size = 0;
33  uint32_t uid = 0;
34  uint32_t gid = 0;
35  std::string owner;
36  std::string group;
37  uint32_t permissions = 0;
38  uint64_t atime64 = 0;
39  uint32_t atime = 0;
40  uint32_t atime_nseconds = 0;
41  uint64_t createtime = 0;
42  uint32_t createtime_nseconds = 0;
43  uint64_t mtime64 = 0;
44  uint32_t mtime = 0;
45  uint32_t mtime_nseconds = 0;
46  std::string acl;
47  uint32_t extended_count = 0;
48  std::string extended_type;
49  std::string extended_data;
50 
51  bool exists() const;
52  bool isfile() const;
53  bool isdir() const;
54  bool islink() const;
55  };
56 private:
57  struct Private;
58  Private *m;
59 
60  struct UNLINK {
61  };
62  struct MKDIR {
63  };
64  struct RMDIR {
65  };
66  typedef std::variant<UNLINK, MKDIR, RMDIR> SftpCmd;
67  struct SftpSimpleCommand;
68 
69  void close_scp();
70  void clear_error();
71 public:
72  Quissh();
73  ~Quissh();
74  Quissh(Quissh const &) = delete;
75  Quissh &operator=(Quissh const &) = delete;
76  Quissh(Quissh &&) = delete;
77  Quissh &operator=(Quissh &&) = delete;
78 
79  bool open(const char *host, int port, AuthVar authdata);
80  void close();
81 
82  bool is_connected() const;
83  bool is_sftp_connected() const;
84 
85  void add_allowed_command(const std::string &command);
86 
87  bool exec(char const *cmd, std::function<bool (const char *, int)> writer);
88 
89  class CHANNEL {
90  private:
91  Quissh &ssh_;
92  public:
93  CHANNEL(Quissh &ssh)
94  : ssh_(ssh)
95  {
96  }
97  ~CHANNEL()
98  {
99  close();
100  }
101  bool open()
102  {
103  return ssh_.channel_open();
104  }
105  void close()
106  {
107  ssh_.channel_close();
108  }
109  bool exec(char const *command, std::function<bool(char const *, int)> writer);
110  };
111 private:
112  bool sftp_unlink(const char *name);
113  bool sftp_mkdir(const char *name);
114  bool sftp_rmdir(const char *name);
115 
116  bool scp_push_file(char const *path, std::function<int (char *ptr, int len)> reader, size_t size);
117  bool scp_pull_file(std::function<bool (char const *ptr, int len)> writer);
118 
119  bool sftp_open();
120  bool sftp_close();
121 
122  bool sftp_push_file(char const *path, std::function<int (char *ptr, int len)> reader);
123  bool sftp_pull_file(char const *remote_path, std::function<int (char *ptr, int len)> writer);
124 
125  std::optional<std::vector<FileAttribute>> sftp_ls(char const *path);
126  FileAttribute sftp_stat(const std::string &path);
127 
128  bool channel_open();
129  void channel_close();
130 public:
131  bool push_file(char const *path, std::function<int (char *ptr, int len)> reader);
132  bool pull_file(char const *remote_path, std::function<int (char const *ptr, int len)> writer);
133  std::optional<struct stat> stat(std::string const &path);
134 
135  class SFTP {
136  private:
137  Quissh &ssh_;
138  public:
139  SFTP(Quissh &ssh)
140  : ssh_(ssh)
141  {
142  }
143  ~SFTP()
144  {
145  close();
146  }
147  bool open()
148  {
149  return ssh_.sftp_open();
150  }
151  void close()
152  {
153  ssh_.sftp_close();
154  }
155  bool is_connected() const;
156  bool unlink(char const *name)
157  {
158  return ssh_.sftp_unlink(name);
159  }
160  bool mkdir(char const *name)
161  {
162  return ssh_.sftp_mkdir(name);
163  }
164  bool rmdir(char const *name)
165  {
166  return ssh_.sftp_rmdir(name);
167  }
168  std::optional<std::vector<FileAttribute>> ls(char const *path)
169  {
170  return ssh_.sftp_ls(path);
171  }
172  bool push(char const *path, std::function<int (char *, int)> reader)
173  {
174  return ssh_.sftp_push_file(path, reader);
175  }
176  FileAttribute stat(char const *path)
177  {
178  return ssh_.sftp_stat(path);
179  }
180  bool push(char const *local_path, char const *remote_path);
181  bool pull(char const *remote_path, std::function<int (char const *ptr, int len)> writer)
182  {
183  return ssh_.sftp_pull_file(remote_path, writer);
184  }
185  bool pull(char const *remote_path, char const *local_path);
186  };
187 
188  class DIR {
189  private:
190  struct Private;
191  Private *m;
192  Quissh &quissh_;
193  DIR(DIR const &) = delete;
194  DIR &operator=(DIR const &) = delete;
195  DIR(DIR &&) = delete;
196  DIR &operator=(DIR &&) = delete;
197  public:
198  DIR(Quissh &quissh);
199  ~DIR();
200  bool opendir(char const *path);
201  void closedir();
202  std::optional<FileAttribute> readdir();
203  };
204 
205 };
206 
207 #endif
208 #endif // QUISSH_H