aboutsummaryrefslogtreecommitdiff
path: root/src/libssh2.h
blob: c1b7bf2aa835169f2ab902c8ab86bde82d6f22d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
/** \file
 * \brief The libssh2 convenience functions.
 */
/*
* Copyright (c) 2019-2022 David Timber <dxdt@dev.snart.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include <libssh2.h>
#include <pthsem.h>


/**
 * \brief libssh2_session_handshake() wrapper function.
 * \param s The libssh2 session object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_handshake (LIBSSH2_SESSION *s, const int fd, pth_event_t ev);
/**
 * \brief libssh2_userauth_password_ex() wrapper function.
 * \param s The libssh2 session object.
 * \param fd The open file descriptor for the session.
 * \param id The user name.
 * \param pw The password.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ua_pwd (
	LIBSSH2_SESSION *s,
	const int fd,
	const char *id,
	const char *pw,
	pth_event_t ev);
/**
 * \brief libssh2_channel_open_session() wrapper function.
 * \param s The libssh2 session object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \param[out] err The pointer to int for an error code on failure.
 * \return The new libssh2 channel object on success.
 * \retval NULL on failure. The return value of libssh2_session_last_errno() is
 *	returned via \p err if used.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
LIBSSH2_CHANNEL *prne_lssh2_open_ch (
	LIBSSH2_SESSION *s,
	const int fd,
	pth_event_t ev,
	int *err);
/**
 * \brief libssh2_channel_close() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_close_ch (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	pth_event_t ev);
/**
 * \brief libssh2_channel_wait_closed() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ch_wait_closed (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	pth_event_t ev);
/**
 * \brief libssh2_channel_request_pty() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param term
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ch_req_pty (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	const char *term,
	pth_event_t ev);
/**
 * \brief libssh2_channel_shell() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ch_sh (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	pth_event_t ev);
/**
 * \brief libssh2_channel_read() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param s_err The standard error flag. Returns data from the standard error
 *	stream instead of the standard output stream if pass as \c true .
 * \param[out] buf The output buffer.
 * \param[in] len The byte length of \p buf .
 * \param ev The pth event object to use on poll().
 * \return The actual number of bytes read from the stream and written to \p buf
 *	or a negative value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ch_read (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	const bool s_err,
	void *buf,
	const size_t len,
	pth_event_t ev);
/**
 * \brief libssh2_channel_write() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param buf The buffer containing data to write to the standard input stream.
 * \param len The byte length of data to write from \p buf .
 * \param ev The pth event object to use on poll().
 * \return The actual number of bytes written to the stream or a negative value
 *	returned from the library functions or poll() on failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ch_write (
	LIBSSH2_SESSION *s,
	LIBSSH2_CHANNEL *c,
	const int fd,
	const void *buf,
	const size_t len,
	pth_event_t ev);
/**
 * \brief libssh2_session_disconnect_ex() wrapper function.
 * \param s The libssh2 session object.
 * \param c The libssh2 channel object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 0 on success.
 * \retval Non-zero value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_discon (
	LIBSSH2_SESSION *s,
	const int fd,
	const int reason,
	const char *desc,
	const char *lang,
	pth_event_t ev);
/**
 * \brief libssh2_userauth_list() wrapper function.
 * \param s The libssh2 session object.
 * \param fd The open file descriptor for the session.
 * \param username The pointer to the null-terminated user name string.
 * \param ev The pth event object to use on poll().
 * \param[out] err The pointer to an \c int for returning an error occurred
 *	during the function call if any. The \c int will be set to zero if the
 *	the operation was successful (optional)
 * \return The pointer to the internal comma-separated and null-terminated
 *	string of the authentication methods available for the user.
 * \retval NULL if an error has occurred. The \c int at \p err is set to the
 *	\c errno
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
const char *prne_lssh2_ua_list (
	LIBSSH2_SESSION *s,
	const int fd,
	const char *username,
	pth_event_t ev,
	int *err);
/**
 * \brief libssh2_userauth_authenticated() wrapper function.
 * \param s The libssh2 session object.
 * \param fd The open file descriptor for the session.
 * \param ev The pth event object to use on poll().
 * \retval 1 if the session has been authenticated.
 * \retval 0 if the session has not been authenticated.
 * \retval Negative value returned from the library functions or poll() on
 *	failure.
 * \note This is a convenience function that wraps all the operation that
 *	involves polling and calling the function again.
 */
int prne_lssh2_ua_authd (
	LIBSSH2_SESSION *s,
	const int fd,
	pth_event_t ev);

/**
 * \brief Free the libssh2 session object.
 * \param s The libssh2 session object.
 * \note This is a bullshit-free version of libssh2_session_free().
 * \note The function call has no effect if \p s is passed NULL.
 * \see prne_lssh2_cripple_session()
 */
void prne_lssh2_free_session (LIBSSH2_SESSION *s);

/* Workarounds */
/**
 * \brief Cripple the session's IO ability. Used to guarantee that
 *	\c libssh2_session_free() will never return \c EAGAIN .
 * \param s The libssh2 session object.
 * \warning This function renders the session unusuable. The function must be
 *	the last function to call before calling \c libssh2_session_free() to
 *	free the resources.
 * \note This function is used in \c prne_lssh2_free_session() . This function
 *	is exposed just to cover the cases where it's necessary to cripple the
 *	session manually.
 * \see prne_lssh2_free_session()
 * \see libssh2_session_free()
 */
void prne_lssh2_cripple_session (LIBSSH2_SESSION *s);