LibSSH.xs

filter: Statementstmt 57.5% 148 / 257 Branchbran 56.9% 98 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.3% 246 / 429 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Net/LibSSH.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_mg_findext
6 #include "ppport.h"
7  
8 #include <libssh/libssh.h>
9 #include <libssh/sftp.h>
10  
11 #define undef &PL_sv_undef
12  
13 /* ====================================================
14 Internal structs (named so Newxz has a type to size)
15 ==================================================== */
16  
17 /* Where the ssh_session is in its one-way life. libssh 0.10.6 cannot
18 reconnect a session it has disconnected -- ssh_connect() sits out the whole
19 timeout -- but measured on the same libssh, disconnect() on a session that
20 was never connected leaves it fully connectable. Only the CONNECTED ->
21 SPENT transition is therefore terminal, which is why this is three states
22 and not a flag. Newxz gives FRESH for free.
23 libssh has nothing to answer this with: ssh_get_status() reports SSH_CLOSED
24 for both a spent session and a never-connected one that saw a disconnect(),
25 and ssh_is_connected() is already 0 by the time connect() would ask. */
26 typedef enum {
27 NLSS_SESSION_FRESH = 0,
28 NLSS_SESSION_CONNECTED,
29 NLSS_SESSION_SPENT
30 } NLSS_SessionState;
31  
32 /* generation / session_generation: ssh_disconnect() frees every ssh_channel
33 the session owns from inside libssh, so a channel or sftp session that
34 outlives a disconnect() holds a non-NULL pointer into freed memory. The
35 session counts its disconnects; each child copies the count it was born
36 under and compares. See nlss_session_stale(). */
37 typedef struct {
38 ssh_session session;
39 unsigned int generation;
40 NLSS_SessionState state;
41 /* The one message this binding raises itself; everything else in error()
42 comes from libssh. Set when connect() refuses a spent session, where
43 ssh_connect() is never called and ssh_get_error() would still hold what
44 stood there before the disconnect (measured: nothing). A string
45 literal, so there is nothing to free, and nothing clears it either --
46 a spent session stays spent, and that is the reason every later call
47 on it fails too. */
48 const char *own_error;
49 /* Mirror of SSH_OPTIONS_STRICTHOSTKEYCHECK, kept here because libssh
50 has no getter for it and connect() has to know. Inverted so that the
51 zero Newxz leaves behind means strict -- the secure default, and
52 libssh's own. Only option('strict_hostkeycheck') writes it. */
53 int hostkey_check_disabled;
54 } NLSS_Session;
55  
56 typedef struct {
57 ssh_channel channel;
58 SV *session_sv;
59 unsigned int session_generation;
60 } NLSS_Channel;
61  
62 typedef struct {
63 sftp_session sftp;
64 SV *session_sv;
65 unsigned int session_generation;
66 } NLSS_SFTP;
67  
68 /* Pointer typedefs with the xsubpp __ → :: naming convention.
69 Using pointer typedefs (not struct typedefs) means XS signatures
70 need no *. The typemap does not derive its vtable names from these:
71 xsubpp maps :: → __ inside ${type} only from 3.60 on, so a generic
72 T_MAGICEXT entry writing &${type}_magic expands to &Net::LibSSH_magic
73 — not valid C — on the older xsubpp this distribution still supports.
74 Every typemap entry therefore spells its vtable out literally. */
75 typedef NLSS_Session *Net__LibSSH;
76 typedef NLSS_Channel *Net__LibSSH__Channel;
77 typedef NLSS_SFTP *Net__LibSSH__SFTP;
78  
79 /* ====================================================
80 Magic vtables — svt_free replaces DESTROY in XS.
81 Perl's GC calls svt_free automatically when the SV
82 is collected; no explicit DESTROY method needed.
83 ==================================================== */
84  
85 static int
86 29 nlss_session_free(pTHX_ SV *sv, MAGIC *mg)
87 {
88 29 NLSS_Session *self = (NLSS_Session *)(void *)mg->mg_ptr;
89 /* No generation bump here: every channel and sftp session holds a
90 reference on this very SV, so this runs only once the last of them is
91 gone. There is nothing left to invalidate. */
92 29 ▶ if (self->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->session) { 29 0 29
93 29 ssh_disconnect(self->session);
94 29 ssh_free(self->session);
95 }
96 29 Safefree(self);
97 29 return 0;
98 }
99 static const MGVTBL Net__LibSSH_magic = { .svt_free = nlss_session_free };
100  
101 /* Has libssh already freed the C object this child holds?
102 The child reaches its session through session_sv -- the blessed,
103 magic-bearing SV it took a reference on in channel()/sftp(). Since 0.003
104 that is the referent, SvRV(ST(0)), not the reference scalar, so the magic
105 sits on session_sv itself and mg_findext takes it directly; a further
106 SvRV() here would walk off the object.
107 Missing magic can only mean the session SV is not the one we stored, so it
108 counts as stale: refusing is the safe answer for both the method guard and
109 the free path. */
110 static int
111 69 nlss_session_stale(pTHX_ SV *session_sv, unsigned int generation)
112 {
113 69 ▶ if (!session_sv || !SvMAGICAL(session_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!session_sv || !SvMAGICAL(session_sv)) 69 0 69
if (!session_sv || !SvMAGICAL(session_sv)) 0 69 69
114 0 return 1;
115 69 MAGIC *mg = mg_findext(session_sv, PERL_MAGIC_ext, &Net__LibSSH_magic);
116 69 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 0 69 69
117 0 return 1;
118 69 return ((NLSS_Session *)(void *)mg->mg_ptr)->generation != generation;
119 }
120  
121 static int
122 6 nlss_channel_free(pTHX_ SV *sv, MAGIC *mg)
123 {
124 6 NLSS_Channel *self = (NLSS_Channel *)(void *)mg->mg_ptr;
125 /* A stale channel was freed inside ssh_disconnect() already; sending eof
126 on it is the crash the caller never asked for. Skip the C teardown
127 only -- the session reference and the struct still have to go, or the
128 crash is merely traded for a leak. The session struct is still there to
129 ask: this child's reference on session_sv is released below, after the
130 question, so nlss_session_free cannot have run yet. */
131 6 ▶ if (self->channel
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel 3 3 6
132 3 ▶ && !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 2 1 3
133 2 ssh_channel_send_eof(self->channel);
134 2 ssh_channel_close(self->channel);
135 2 ssh_channel_free(self->channel);
136 }
137 6 SvREFCNT_dec(self->session_sv);
138 6 Safefree(self);
139 6 return 0;
140 }
141 static const MGVTBL Net__LibSSH__Channel_magic = { .svt_free = nlss_channel_free };
142  
143 static int
144 0 nlss_sftp_free(pTHX_ SV *sv, MAGIC *mg)
145 {
146 0 NLSS_SFTP *self = (NLSS_SFTP *)(void *)mg->mg_ptr;
147 0 ▶ if (self->sftp) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sftp) { 0 0 0
148 /* Unlike a channel, an sftp_session is not freed by ssh_disconnect()
149 -- only the channel inside it is. Skipping sftp_free() outright
150 would trade the crash for a leak (measured: ~1.6 kB per
151 disconnected-and-dropped sftp session), so cut the dangling channel
152 loose first and let sftp_free() release everything else. libssh
153 declares struct sftp_session_struct in its public sftp.h, and its
154 ssh_channel_send_eof/close/free are all NULL-tolerant. */
155 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
156 0 self->sftp->channel = NULL;
157 0 sftp_free(self->sftp);
158 }
159 0 SvREFCNT_dec(self->session_sv);
160 0 Safefree(self);
161 0 return 0;
162 }
163 static const MGVTBL Net__LibSSH__SFTP_magic = { .svt_free = nlss_sftp_free };
164  
165 /* ====================================================
166 Helper functions
167 ==================================================== */
168  
169 /* The one disconnect path, shared by disconnect() and the host-key refusal
170 in connect(): both have to spend a connected session and bump the
171 generation before libssh frees the channels, and having the invariant in
172 one place is what keeps the two from drifting apart. */
173 static void
174 1 nlss_session_disconnect(NLSS_Session *self)
175 {
176 /* Only a session that actually got connected is spent by this: measured
177 on libssh 0.10.6, disconnect() on a never-connected session leaves it
178 connectable, and marking it would refuse a call that works. */
179 1 ▶ if (self->state == NLSS_SESSION_CONNECTED)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_CONNECTED) 1 0 1
180 1 self->state = NLSS_SESSION_SPENT;
181 /* Bump before disconnecting: ssh_disconnect() frees every channel the
182 session owns, so every channel and sftp session opened on it has to be
183 stale by the time that memory goes away. */
184 1 self->generation++;
185 1 ssh_disconnect(self->session);
186 1 }
187  
188 static void
189 8 nlss_croak_error(pTHX_ ssh_session session, const char *prefix)
190 {
191 8 const char *msg = ssh_get_error(session);
192 8 ▶ Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)");
Branch: 1 branch
BranchTrueFalseTotal
Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)"); 8 0 8
193 }
194  
195 static void
196 66 nlss_channel_check_open(pTHX_ NLSS_Channel *self, const char *prefix)
197 {
198 /* Two distinct states, two distinct messages: the caller closed this
199 channel itself, or the session was disconnected out from under it.
200 Collapsing them would leave a caller unable to tell its own teardown
201 from libssh's. */
202 66 ▶ if (!self->channel)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->channel) 3 63 66
203 3 Perl_croak(aTHX_ "%s: channel is closed", prefix);
204 63 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 3 60 63
205 3 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
206 60 }
207  
208 static void
209 0 nlss_sftp_check_open(pTHX_ NLSS_SFTP *self, const char *prefix)
210 {
211 /* There is no SFTP close(), so self->sftp is set for the object's whole
212 lifetime; disconnect() is the only thing that stops it being usable. */
213 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
214 0 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
215 0 }
216  
217 static SV *
218 0 nlss_channel_slurp(pTHX_ ssh_channel ch, int is_stderr)
219 {
220 0 SV *buf = newSVpvs("");
221 char tmp[4096];
222 int n;
223 while (1) {
224 0 n = ssh_channel_read(ch, tmp, sizeof(tmp), is_stderr);
225 0 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 0 0
226 0 break;
227 0 sv_catpvn(buf, tmp, n);
228 }
229 0 return buf;
230 }
231  
232 MODULE = Net::LibSSH PACKAGE = Net::LibSSH
233  
234 PROTOTYPES: DISABLE
235  
236 # CLONE_SKIP: an ithread would clone the ext magic with the same struct
237 # pointer and both copies would run svt_free. Defined here, not in the .pm
238 # files: Channel.pm and SFTP.pm are POD-only and never loaded.
239 int
240 CLONE_SKIP(...)
241 CODE:
242 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
243 OUTPUT:
244 RETVAL
245  
246 Net::LibSSH
247 new(class)
248 SV *class
249 CODE:
250 29 Newxz(RETVAL, 1, NLSS_Session);
251 29 RETVAL->session = ssh_new();
252 29 ▶ if (!RETVAL->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL->session) { 0 29 29
253 0 Safefree(RETVAL);
254 0 Perl_croak(aTHX_ "Net::LibSSH::new: ssh_new() returned NULL");
255 }
256 OUTPUT:
257 RETVAL
258  
259 void
260 option(self, key, value)
261 Net::LibSSH self
262 const char *key
263 SV *value
264 CODE:
265 172 int rc = SSH_OK;
266 172 ▶ if (strcmp(key, "host") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "host") == 0) { 26 146 172
267 26 rc = ssh_options_set(self->session, SSH_OPTIONS_HOST, SvPV_nolen(value));
268 146 ▶ } else if (strcmp(key, "user") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "user") == 0) { 25 121 146
269 25 rc = ssh_options_set(self->session, SSH_OPTIONS_USER, SvPV_nolen(value));
270 121 ▶ } else if (strcmp(key, "port") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "port") == 0) { 27 94 121
271 27 unsigned int port = (unsigned int) SvUV(value);
272 27 rc = ssh_options_set(self->session, SSH_OPTIONS_PORT, &port);
273 94 ▶ } else if (strcmp(key, "knownhosts") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "knownhosts") == 0) { 25 69 94
274 25 rc = ssh_options_set(self->session, SSH_OPTIONS_KNOWNHOSTS, SvPV_nolen(value));
275 69 ▶ } else if (strcmp(key, "timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "timeout") == 0) { 26 43 69
276 26 long t = (long) SvIV(value);
277 26 rc = ssh_options_set(self->session, SSH_OPTIONS_TIMEOUT, &t);
278 43 ▶ } else if (strcmp(key, "compression") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "compression") == 0) { 1 42 43
279 1 rc = ssh_options_set(self->session, SSH_OPTIONS_COMPRESSION, SvPV_nolen(value));
280 42 ▶ } else if (strcmp(key, "log_verbosity") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "log_verbosity") == 0) { 1 41 42
281 1 int v = SvIV(value);
282 1 rc = ssh_options_set(self->session, SSH_OPTIONS_LOG_VERBOSITY, &v);
283 41 ▶ } else if (strcmp(key, "strict_hostkeycheck") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "strict_hostkeycheck") == 0) { 0 41 41
284 0 int v = SvTRUE(value) ? 1 : 0;
285 0 rc = ssh_options_set(self->session, SSH_OPTIONS_STRICTHOSTKEYCHECK, &v);
286 0 ▶ if (rc == SSH_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SSH_OK) 0 0 0
287 0 self->hostkey_check_disabled = !v;
288 41 ▶ } else if (strcmp(key, "kex") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "kex") == 0) { 8 33 41
289 8 rc = ssh_options_set(self->session, SSH_OPTIONS_KEY_EXCHANGE, SvPV_nolen(value));
290 33 ▶ } else if (strcmp(key, "hostkeys") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "hostkeys") == 0) { 5 28 33
291 5 rc = ssh_options_set(self->session, SSH_OPTIONS_HOSTKEYS, SvPV_nolen(value));
292 28 ▶ } else if (strcmp(key, "ciphers_c_s") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ciphers_c_s") == 0) { 6 22 28
293 6 rc = ssh_options_set(self->session, SSH_OPTIONS_CIPHERS_C_S, SvPV_nolen(value));
294 22 ▶ } else if (strcmp(key, "ciphers_s_c") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ciphers_s_c") == 0) { 6 16 22
295 6 rc = ssh_options_set(self->session, SSH_OPTIONS_CIPHERS_S_C, SvPV_nolen(value));
296 16 ▶ } else if (strcmp(key, "hmac_c_s") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "hmac_c_s") == 0) { 7 9 16
297 7 rc = ssh_options_set(self->session, SSH_OPTIONS_HMAC_C_S, SvPV_nolen(value));
298 9 ▶ } else if (strcmp(key, "hmac_s_c") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "hmac_s_c") == 0) { 7 2 9
299 7 rc = ssh_options_set(self->session, SSH_OPTIONS_HMAC_S_C, SvPV_nolen(value));
300 } else {
301 2 Perl_croak(aTHX_ "Net::LibSSH::option: unknown option '%s'", key);
302 }
303 170 ▶ if (rc != SSH_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) 7 163 170
304 7 nlss_croak_error(aTHX_ self->session, "Net::LibSSH::option");
305  
306 int
307 connect(self)
308 Net::LibSSH self
309 CODE:
310 25 ▶ if (self->state == NLSS_SESSION_SPENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_SPENT) { 0 25 25
311 /* Refuse without calling libssh: ssh_connect() on a disconnected
312 session waits out the full timeout and then reports "Timeout
313 connecting to <host>", which names the wrong problem. Still 0 and
314 still no croak -- connect()'s contract is 1 or 0 with the message
315 on error(), and Rex::LibSSH relies on it. */
316 0 self->own_error = "session was disconnected and cannot be reconnected";
317 0 RETVAL = 0;
318 25 ▶ } else if (ssh_connect(self->session) == SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ssh_connect(self->session) == SSH_OK) { 14 11 25
319 /* CONNECTED first: ssh_connect() has completed the key exchange, so
320 a refusal below is a disconnect of a connected session and spends
321 it exactly as disconnect() would. */
322 14 self->state = NLSS_SESSION_CONNECTED;
323 14 RETVAL = 1;
324 /* ssh_connect() negotiates the host key but never checks it against
325 known_hosts -- that is a separate call libssh leaves to the
326 application, and without it SSH_OPTIONS_STRICTHOSTKEYCHECK is
327 decoration. strict_hostkeycheck => 0 is documented as disabling
328 verification outright, so it skips the question entirely: no
329 refusal on CHANGED either, and nothing is written to known_hosts. */
330 14 ▶ if (!self->hostkey_check_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->hostkey_check_disabled) { 14 0 14
331 14 const char *refusal = NULL;
332 14 switch (ssh_session_is_known_server(self->session)) {
333 14 case SSH_KNOWN_HOSTS_OK:
334 14 break;
335 0 case SSH_KNOWN_HOSTS_CHANGED:
336 0 refusal = "host key has changed from the known_hosts entry"
337 " -- possible man-in-the-middle attack";
338 0 break;
339 0 case SSH_KNOWN_HOSTS_OTHER:
340 0 refusal = "host key type differs from the known_hosts entry"
341 " -- possible man-in-the-middle attack";
342 0 break;
343 0 case SSH_KNOWN_HOSTS_ERROR:
344 0 refusal = "could not verify host key against known_hosts";
345 0 break;
346 0 case SSH_KNOWN_HOSTS_NOT_FOUND:
347 case SSH_KNOWN_HOSTS_UNKNOWN:
348 default:
349 0 refusal = "host key is not in known_hosts"
350 " and strict_hostkeycheck is on";
351 0 break;
352 }
353 14 ▶ if (refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if (refusal) { 0 14 14
354 /* Same 1/0 contract as every other connect() failure. The
355 session is spent from here on, so own_error being sticky
356 is right: the later "cannot be reconnected" overwrites it. */
357 0 nlss_session_disconnect(self);
358 0 self->own_error = refusal;
359 0 RETVAL = 0;
360 }
361 }
362 } else {
363 11 RETVAL = 0;
364 }
365 OUTPUT:
366 RETVAL
367  
368 void
369 disconnect(self)
370 Net::LibSSH self
371 CODE:
372 1 nlss_session_disconnect(self);
373  
374 SV *
375 error(self)
376 Net::LibSSH self
377 CODE:
378 38 const char *msg = self->own_error ? self->own_error
379 19 ▶ : ssh_get_error(self->session);
Branch: 1 branch
BranchTrueFalseTotal
: ssh_get_error(self->session); 0 19 19
380 19 ▶ RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 19 0 19
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 11 8 19
381 OUTPUT:
382 RETVAL
383  
384 int
385 auth_password(self, password)
386 Net::LibSSH self
387 const char *password
388 CODE:
389 0 RETVAL = (ssh_userauth_password(self->session, NULL, password)
390 0 ▶ == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
== SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
391 OUTPUT:
392 RETVAL
393  
394 int
395 auth_none(self)
396 Net::LibSSH self
397 CODE:
398 /* Only SSH_AUTH_SUCCESS is a yes: DENIED, PARTIAL, ERROR and AGAIN all
399 mean the session is not authenticated yet, and the reason is on
400 error(). Devices that run their own login dialog on the shell let the
401 SSH layer in this way. */
402 6 ▶ RETVAL = (ssh_userauth_none(self->session, NULL) == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_userauth_none(self->session, NULL) == SSH_AUTH_SUCCESS) ? 1 : 0; 0 6 6
403 OUTPUT:
404 RETVAL
405  
406 int
407 auth_agent(self)
408 Net::LibSSH self
409 CODE:
410 0 int rc = ssh_userauth_agent(self->session, NULL);
411 0 ▶ if (rc != SSH_AUTH_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_AUTH_SUCCESS) 0 0 0
412 0 rc = ssh_userauth_publickey_auto(self->session, NULL, NULL);
413 0 ▶ RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
414 OUTPUT:
415 RETVAL
416  
417 int
418 auth_publickey(self, privkey_path)
419 Net::LibSSH self
420 const char *privkey_path
421 CODE:
422 0 ssh_key key = NULL;
423 0 int rc = ssh_pki_import_privkey_file(privkey_path, NULL, NULL, NULL, &key);
424 0 ▶ if (rc != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) { 0 0 0
425 0 RETVAL = 0;
426 } else {
427 0 rc = ssh_userauth_publickey(self->session, NULL, key);
428 0 ssh_key_free(key);
429 0 RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
430 }
431 OUTPUT:
432 RETVAL
433  
434 Net::LibSSH::Channel
435 channel(self)
436 Net::LibSSH self
437 CODE:
438 6 ssh_channel ch = ssh_channel_new(self->session);
439 6 ▶ if (!ch)
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) 0 6 6
440 0 XSRETURN_UNDEF;
441 6 ▶ if (ssh_channel_open_session(ch) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssh_channel_open_session(ch) != SSH_OK) { 0 6 6
442 0 ssh_channel_free(ch);
443 0 XSRETURN_UNDEF;
444 }
445 6 Newxz(RETVAL, 1, NLSS_Channel);
446 6 RETVAL->channel = ch;
447 6 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
448 6 RETVAL->session_generation = self->generation;
449 OUTPUT:
450 RETVAL
451  
452 Net::LibSSH::SFTP
453 sftp(self)
454 Net::LibSSH self
455 CODE:
456 0 sftp_session sftp = sftp_new(self->session);
457 0 ▶ if (!sftp)
Branch: 1 branch
BranchTrueFalseTotal
if (!sftp) 0 0 0
458 0 XSRETURN_UNDEF;
459 0 ▶ if (sftp_init(sftp) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (sftp_init(sftp) != SSH_OK) { 0 0 0
460 0 sftp_free(sftp);
461 0 XSRETURN_UNDEF;
462 }
463 0 Newxz(RETVAL, 1, NLSS_SFTP);
464 0 RETVAL->sftp = sftp;
465 0 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
466 0 RETVAL->session_generation = self->generation;
467 OUTPUT:
468 RETVAL
469  
470  
471 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::Channel
472  
473 int
474 CLONE_SKIP(...)
475 CODE:
476 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
477 OUTPUT:
478 RETVAL
479  
480 int
481 exec(self, cmd)
482 Net::LibSSH::Channel self
483 const char *cmd
484 CODE:
485 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exec");
486 0 ▶ RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0; 0 0 0
487 OUTPUT:
488 RETVAL
489  
490 SV *
491 read(self, ...)
492 Net::LibSSH::Channel self
493 CODE:
494 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::read");
495 0 int is_stderr = 0;
496 0 int len = -1;
497 0 ▶ if (items >= 2) len = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) len = SvIV(ST(1)); 0 0 0
498 0 ▶ if (items >= 3) is_stderr = SvTRUE(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) is_stderr = SvTRUE(ST(2)); 0 0 0
499 0 ▶ if (len < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { 0 0 0
500 0 RETVAL = nlss_channel_slurp(aTHX_ self->channel, is_stderr);
501 } else {
502 char *buf;
503 int n;
504 0 Newx(buf, len + 1, char);
505 0 n = ssh_channel_read(self->channel, buf, len, is_stderr);
506 0 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 0 0 0
507 0 Safefree(buf);
508 0 RETVAL = newSVpvs("");
509 } else {
510 0 RETVAL = newSVpvn(buf, n);
511 0 Safefree(buf);
512 }
513 }
514 OUTPUT:
515 RETVAL
516  
517 int
518 request_pty(self, ...)
519 Net::LibSSH::Channel self
520 CODE:
521 14 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::request_pty");
522 12 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 4 8 12
523 4 RETVAL = (ssh_channel_request_pty(self->channel) == SSH_OK) ? 1 : 0;
524 } else {
525 /* Columns and rows only make sense together; a lone one is a usage
526 error, not a request for libssh's 80x24. Validated here because
527 SvIV of garbage is a silent 0, and a 0x0 terminal is no terminal. */
528 8 ▶ if (items != 3 && items != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (items != 3 && items != 4) 4 4 8
if (items != 3 && items != 4) 2 2 4
529 2 Perl_croak(aTHX_ "Usage: Net::LibSSH::Channel::request_pty"
530 "(self [, cols, rows [, term]])");
531 6 ▶ if (!looks_like_number(ST(1)) || !looks_like_number(ST(2))
Branch: 2 branches
BranchTrueFalseTotal
if (!looks_like_number(ST(1)) || !looks_like_number(ST(2)) 5 1 6
if (!looks_like_number(ST(1)) || !looks_like_number(ST(2)) 5 0 5
532 5 ▶ || SvIV(ST(1)) < 1 || SvIV(ST(1)) > 65535
Branch: 2 branches
BranchTrueFalseTotal
|| SvIV(ST(1)) < 1 || SvIV(ST(1)) > 65535 4 1 5
|| SvIV(ST(1)) < 1 || SvIV(ST(1)) > 65535 4 0 4
533 4 ▶ || SvIV(ST(2)) < 1 || SvIV(ST(2)) > 65535)
Branch: 2 branches
BranchTrueFalseTotal
|| SvIV(ST(2)) < 1 || SvIV(ST(2)) > 65535) 3 1 4
|| SvIV(ST(2)) < 1 || SvIV(ST(2)) > 65535) 1 2 3
534 4 Perl_croak(aTHX_ "Net::LibSSH::Channel::request_pty: "
535 "cols and rows must be integers from 1 to 65535");
536 2 const char *term = "xterm";
537 2 ▶ if (items == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) { 2 0 2
538 2 term = SvPV_nolen(ST(3));
539 2 ▶ if (!*term)
Branch: 1 branch
BranchTrueFalseTotal
if (!*term) 1 1 2
540 1 Perl_croak(aTHX_ "Net::LibSSH::Channel::request_pty: "
541 "term must not be empty");
542 }
543 1 RETVAL = (ssh_channel_request_pty_size(self->channel, term,
544 2 (int) SvIV(ST(1)), (int) SvIV(ST(2))) == SSH_OK) ? 1 : 0;
545 }
546 OUTPUT:
547 RETVAL
548  
549 int
550 request_shell(self)
551 Net::LibSSH::Channel self
552 CODE:
553 7 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::request_shell");
554 5 ▶ RETVAL = (ssh_channel_request_shell(self->channel) == SSH_OK) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_channel_request_shell(self->channel) == SSH_OK) ? 1 : 0; 0 5 5
555 OUTPUT:
556 RETVAL
557  
558 SV *
559 read_timeout(self, seconds, ...)
560 Net::LibSSH::Channel self
561 SV *seconds
562 CODE:
563 33 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::read_timeout");
564 /* Unlike read(), this one has to say three different things -- data,
565 nothing yet, and nothing ever again -- so its arguments are validated
566 instead of passing through SvNV/SvIV's silent 0. A negative timeout
567 would be libssh's "wait forever", which is never what a typo meant. */
568 31 ▶ if (!looks_like_number(seconds) || !(SvNV(seconds) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!looks_like_number(seconds) || !(SvNV(seconds) >= 0) 29 2 31
if (!looks_like_number(seconds) || !(SvNV(seconds) >= 0) 28 1 29
569 28 ▶ || SvNV(seconds) > 2000000)
Branch: 1 branch
BranchTrueFalseTotal
|| SvNV(seconds) > 2000000) 0 28 28
570 3 Perl_croak(aTHX_ "Net::LibSSH::Channel::read_timeout: "
571 "seconds must be a number from 0 to 2000000");
572 28 int is_stderr = 0;
573 28 int len = 4096;
574 28 ▶ if (items >= 3) len = SvIV(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) len = SvIV(ST(2)); 3 25 28
575 28 ▶ if (items >= 4) is_stderr = SvTRUE(ST(3));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 4) is_stderr = SvTRUE(ST(3)); 1 27 28
576 28 ▶ if (len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 1) 1 27 28
577 1 Perl_croak(aTHX_ "Net::LibSSH::Channel::read_timeout: "
578 "length must be at least 1");
579 {
580 char *buf;
581 int n;
582 27 Newx(buf, len, char);
583 27 n = ssh_channel_read_timeout(self->channel, buf, len, is_stderr,
584 27 (int) (SvNV(seconds) * 1000.0));
585 27 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 16 11 27
586 16 RETVAL = newSVpvn(buf, n);
587 16 Safefree(buf);
588 11 ▶ } else if (n == 0 || n == SSH_AGAIN) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (n == 0 || n == SSH_AGAIN) { 9 2 11
} else if (n == 0 || n == SSH_AGAIN) { 8 1 9
589 /* libssh answers 0 for a timeout and for end of file alike; the
590 channel itself knows which. */
591 10 Safefree(buf);
592 12 ▶ RETVAL = (n == 0 && ssh_channel_is_eof(self->channel))
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (n == 0 && ssh_channel_is_eof(self->channel)) 0 2 2
593 12 ▶ ? undef : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
? undef : newSVpvs(""); 2 8 10
594 } else {
595 1 Safefree(buf);
596 1 nlss_croak_error(aTHX_ ssh_channel_get_session(self->channel),
597 "Net::LibSSH::Channel::read_timeout");
598 }
599 }
600 OUTPUT:
601 RETVAL
602  
603 int
604 write(self, data)
605 Net::LibSSH::Channel self
606 SV *data
607 CODE:
608 11 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::write");
609 STRLEN len;
610 11 const char *ptr = SvPV(data, len);
611 11 RETVAL = ssh_channel_write(self->channel, ptr, (uint32_t) len);
612 OUTPUT:
613 RETVAL
614  
615 void
616 send_eof(self)
617 Net::LibSSH::Channel self
618 CODE:
619 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::send_eof");
620 0 ssh_channel_send_eof(self->channel);
621  
622 int
623 eof(self)
624 Net::LibSSH::Channel self
625 CODE:
626 1 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::eof");
627 1 RETVAL = ssh_channel_is_eof(self->channel);
628 OUTPUT:
629 RETVAL
630  
631 int
632 exit_status(self)
633 Net::LibSSH::Channel self
634 CODE:
635 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exit_status");
636 #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 11, 0)
637 /* 0.11.0 deprecated ssh_channel_get_exit_status() and reduced it to this
638 call with the signal and core-dump outputs NULL'd: any rc but SSH_OK,
639 SSH_AGAIN included, becomes -1. The same mapping is kept here, so the
640 answer does not depend on which side of 0.11.0 the build landed. */
641 uint32_t code;
642 0 ▶ RETVAL = (ssh_channel_get_exit_state(self->channel, &code, NULL, NULL)
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_channel_get_exit_state(self->channel, &code, NULL, NULL) 0 0 0
643 0 ▶ == SSH_OK) ? (int) code : -1;
Branch: 1 branch
BranchTrueFalseTotal
== SSH_OK) ? (int) code : -1; 0 0 0
644 #else
645 RETVAL = ssh_channel_get_exit_status(self->channel);
646 #endif
647 OUTPUT:
648 RETVAL
649  
650 void
651 close(self)
652 Net::LibSSH::Channel self
653 CODE:
654 /* Free the C channel now; svt_free will later release
655 session_sv and Safefree the struct when the SV is GC'd.
656 Deliberately unguarded, so it stays idempotent for svt_free -- but a
657 channel invalidated by disconnect() is only dropped, never freed twice.
658 No croak either: `$ssh->disconnect; $ch->close` is teardown, and
659 teardown that has already happened is not an error. */
660 3 ▶ if (self->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel) { 3 0 3
661 3 ▶ if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 3 0 3
662 3 ssh_channel_send_eof(self->channel);
663 3 ssh_channel_close(self->channel);
664 3 ssh_channel_free(self->channel);
665 }
666 3 self->channel = NULL;
667 }
668  
669  
670 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::SFTP
671  
672 int
673 CLONE_SKIP(...)
674 CODE:
675 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
676 OUTPUT:
677 RETVAL
678  
679 SV *
680 stat(self, path)
681 Net::LibSSH::SFTP self
682 const char *path
683 CODE:
684 0 nlss_sftp_check_open(aTHX_ self, "Net::LibSSH::SFTP::stat");
685 0 sftp_attributes attr = sftp_stat(self->sftp, path);
686 0 ▶ if (!attr)
Branch: 1 branch
BranchTrueFalseTotal
if (!attr) 0 0 0
687 0 XSRETURN_UNDEF;
688 0 HV *h = newHV();
689 0 ▶ hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0)); 0 0 0
690 0 hv_stores(h, "size", newSVuv(attr->size));
691 0 hv_stores(h, "uid", newSVuv(attr->uid));
692 0 hv_stores(h, "gid", newSVuv(attr->gid));
693 0 hv_stores(h, "mode", newSVuv(attr->permissions));
694 0 ▶ hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime)); 0 0 0
695 0 ▶ hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime)); 0 0 0
696 0 sftp_attributes_free(attr);
697 0 RETVAL = newRV_noinc((SV *) h);
698 OUTPUT:
699 RETVAL
↑ Summary blib/lib/Net/LibSSH.pm »