session.hh 5.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef RSM_RSMCORE_SESSION_HH_
#define RSM_RSMCORE_SESSION_HH_

#include <QObject>

#include "rsmcore/declspec.hh"
#include "rsmcore/sessionlog.hh"

namespace rsm
{
enum class SessionAuthState
{
  SUCCESS = 0,
  DENIED,
  PARTIAL,
  INFO,
  AGAIN,
  ERROR = -1
};

21
22
23
24
25
26
27
#define SessionAuthMethodUNKNOWN 0x0000u
#define SessionAuthMethodNONE 0x0001u
#define SessionAuthMethodPASSWORD 0x0002u
#define SessionAuthMethodPUBLICKEY 0x0004u
#define SessionAuthMethodHOSTBASED 0x0008u
#define SessionAuthMethodINTERACTIVE 0x0010u
#define SessionAuthMethodGSSAPI_MIC 0x0020u
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

/**
 * @brief The SessionHostState enum
 */
enum class SessionHostState
{ /**
   * There had been an error checking the host.
   */
  ERROR = -2,

  /**
   * The known host file does not exist. The host is thus unknown. File will
   * be created if host key is accepted.
   */
  NOT_FOUND = -1,

  /**
   * The server is unknown. User should confirm the public key hash is
   * correct.
   */
  UNKNOWN = 0,

  /**
   * The server is known and has not changed.
   */
  OK,

  /**
   * The server key has changed. Either you are under attack or the
   * administrator changed the key. You HAVE to warn the user about a
   * possible attack.
   */
  CHANGED,

  /**
   * The server gave use a key of a type while we had another type recorded.
   * It is a possible attack.
   */
  OTHER,
};

/** Forward declaration of private implementation */
class ChannelImpl;
class SessionImpl;
class Session;

class RSM_PUBLIC Channel
{
 protected:
  // Private implementation
  ChannelImpl* p;

  // constructor is protected to only be used by "friend Session"
  Channel();

 public:
  ~Channel();
  /**
   * @brief open Opens the channel for execution
   * @return
   */
  bool open();
  /**
   * @brief isOpen Checks if the channel is open for execution
   * @return
   */
  bool isOpen() const;

  /**
   * @brief close Close channel of execution
   * Channel is no longer usable
   * @return
   */
  void close();

  /**
   * @brief exec Execute command on remote system
   * @param command
   * @return true on success, false on failure
   */
  bool exec(QString command);

  /**
   * @brief readExecOut Read execution's standard output
   * @return
   */
  QString readExecOut();
  /**
   * @brief readExecErr Read execution's standard error
   * @return
   */
  QString readExecErr();

  friend Session;
};  // class Channel

class RSM_PUBLIC Session
{
 private:
  // Private implementation
  SessionImpl* p;

 public:
  /**
   * Basic Constructor
   */
  Session();
  /**
   * Contructs with host name
   */
  Session(QString host);
  ~Session();

  /**
   * Set the remote host to connect to
   */
  void setHost(QString host);
  /**
   * @brief host Get the host name of the session
   */
148
  QString host() const;
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  /**
   * Set the Log verbosity
   */
  void setLogVerbosity(SessionVerbosity level);
  /**
   * Set the ssh port number
   */
  void setPort(int port);
  /**
   * Set the user
   * Defaults to system user
   */
  void setUser(QString name);

  /**
   * @brief user get the username
   * @return
   */
167
  QString user() const;
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

  /**
   * Set the proxy command
   * @param command
   */
  void setProxyCommand(QString command);
  /**
   * Perform hand-shake to connect to host.
   */
  bool connect();

  /**
   * @brief isConnected Checks if session is currently connected
   * @return
   */
  bool isConnected() const;

  /**
   * @brief getError Retrieve error message
   * @return
   */
189
  QString getError() const;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  /**
   * Disconnect ssh session
   */
  bool disconnect();

  /**
   * @brief getHexa Retrieve public key hash hexa
   * Returns empty string on error
   * @return QString
   */
  QString getHexa() const;
  /**
   * Perform known host verification.
   * Checks known_host file to validate remote host SHA
   */
  SessionHostState verifyKnownHost() const;
  /**
   * @brief updateKnownHosts Updates the known host file
   * @return true on success, false on failure.
   */
  bool updateKnownHosts();

  /**
   * @brief authenticationMethodList Get the list of authentication methods
   *
   * UNKNOWN
   * NONE
   * PASSWORD
   * PUBLICKEY
   * HOSTBASED
   * INTERACTIVE
   * GSSAPI_MIC
   * @return
   */
  int authenticationMethodList() const;

  /**
   * @brief authenticateWithPublicKey Attempt authentication using public key
   * @return Authentication state
   * SUCCESS = 0,
   * DENIED,
   * PARTIAL,
   * INFO,
   * AGAIN,
   * ERROR = -1
   */
  SessionAuthState authenticateWithPublicKey();

  /**
   * @brief authenticateInteractively Attempt interactive keyboard
   * authentication (multiple prompts)
   * @return Authentication state
   * SUCCESS = 0,
   * DENIED,
   * PARTIAL,
   * INFO,
   * AGAIN,
   * ERROR = -1
   */
  SessionAuthState authenticateInteractively();

  /**
   * @brief keyboardInteractiveName Get name of interactive authentication
   * @return QString
   */
  QString keyboardInteractiveName() const;
  /**
   * @brief keyboardInteractiveInstruction Get instruction for interactive
   * authentication
   * @return QString
   */
  QString keyboardInteractiveInstruction() const;
  /**
   * @brief keyboardInteractivePrompts Get prompts for interactive
   * authentication
   * @return QStringList
   */
  QStringList keyboardInteractivePrompts() const;

  /**
   * @brief authenticateWithPassword Attempt password authentication
   * @return Authentication state
   * SUCCESS = 0,
   * DENIED,
   * PARTIAL,
   * INFO,
   * AGAIN,
   * ERROR = -1
   */
  SessionAuthState authenticateWithPassword(QString pswd);

  /**
   * @brief getIssueBanner Get the login issued banner
   * @return Login banner or empty string
   */
  QString loginIssueBanner() const;

  /**
   * @brief authenticatePrompts
   * @param responses
   * @return
   */
  SessionAuthState authenticatePrompts(QStringList responses);

  /**
   * @brief newChannel Allocates a new execution channel from this session
   * @return
   */
  Channel newChannel();

};  // class SessionWorker
}  // namespace rsm
#endif /* RSM_RSMCORE_SESSION_HH_*/