libssh 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,6 +57,14 @@ static VALUE m_initialize(VALUE self) {
57
57
  return self;
58
58
  }
59
59
 
60
+ /*
61
+ * @overload log_verbosity=(verbosity)
62
+ * Set the session logging verbosity.
63
+ * @since 0.1.0
64
+ * @param [Symbol] verbosity +:none+, +:warn+, +:info+, +:debug+, or +:trace+.
65
+ * @return [nil]
66
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_LOG_VERBOSITY)
67
+ */
60
68
  static VALUE m_set_log_verbosity(VALUE self, VALUE verbosity) {
61
69
  ID id_verbosity;
62
70
  int c_verbosity;
@@ -86,16 +94,301 @@ static VALUE m_set_log_verbosity(VALUE self, VALUE verbosity) {
86
94
  return Qnil;
87
95
  }
88
96
 
97
+ static VALUE set_string_option(VALUE self, enum ssh_options_e type, VALUE str) {
98
+ SessionHolder *holder;
99
+ TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
100
+ RAISE_IF_ERROR(ssh_options_set(holder->session, type, StringValueCStr(str)));
101
+ return Qnil;
102
+ }
103
+
104
+ /*
105
+ * @overload host=(host)
106
+ * Set the hostname or IP address to connect to.
107
+ * @since 0.1.0
108
+ * @param [String] host
109
+ * @return [nil]
110
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_HOST)
111
+ */
89
112
  static VALUE m_set_host(VALUE self, VALUE host) {
113
+ return set_string_option(self, SSH_OPTIONS_HOST, host);
114
+ }
115
+
116
+ /*
117
+ * @overload user=(user)
118
+ * Set the username for authentication.
119
+ * @since 0.2.0
120
+ * @param [String] user
121
+ * @return [nil]
122
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_USER)
123
+ */
124
+ static VALUE m_set_user(VALUE self, VALUE user) {
125
+ return set_string_option(self, SSH_OPTIONS_USER, user);
126
+ }
127
+
128
+ static VALUE set_int_option(VALUE self, enum ssh_options_e type, VALUE i) {
90
129
  SessionHolder *holder;
130
+ int j;
91
131
 
132
+ Check_Type(i, T_FIXNUM);
133
+ j = FIX2INT(i);
92
134
  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
93
- RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_HOST,
94
- StringValueCStr(host)));
135
+ RAISE_IF_ERROR(ssh_options_set(holder->session, type, &j));
136
+ return Qnil;
137
+ }
138
+
139
+ /*
140
+ * @overload port=(port)
141
+ * Set the port to connect to.
142
+ * @since 0.2.0
143
+ * @param [Fixnum] port
144
+ * @return [nil]
145
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_PORT)
146
+ */
147
+ static VALUE m_set_port(VALUE self, VALUE port) {
148
+ return set_int_option(self, SSH_OPTIONS_PORT, port);
149
+ }
150
+
151
+ /*
152
+ * @overload bindaddr=(addr)
153
+ * Set the address to bind the client to.
154
+ * @since 0.2.0
155
+ * @param [String] addr
156
+ * @return [nil]
157
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_BINDADDR)
158
+ */
159
+ static VALUE m_set_bindaddr(VALUE self, VALUE addr) {
160
+ return set_string_option(self, SSH_OPTIONS_BINDADDR, addr);
161
+ }
95
162
 
163
+ /*
164
+ * @overload knownhosts=(path)
165
+ * Set the known hosts file name
166
+ * @since 0.2.0
167
+ * @param [String] path
168
+ * @return [nil]
169
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_KNOWNHOSTS)
170
+ */
171
+ static VALUE m_set_knownhosts(VALUE self, VALUE path) {
172
+ return set_string_option(self, SSH_OPTIONS_KNOWNHOSTS, path);
173
+ }
174
+
175
+ static VALUE set_long_option(VALUE self, enum ssh_options_e type, VALUE i) {
176
+ SessionHolder *holder;
177
+ long j;
178
+
179
+ Check_Type(i, T_FIXNUM);
180
+ j = FIX2LONG(i);
181
+ TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
182
+ RAISE_IF_ERROR(ssh_options_set(holder->session, type, &j));
96
183
  return Qnil;
97
184
  }
98
185
 
186
+ /*
187
+ * @overload timeout=(sec)
188
+ * Set a timeout for the connection in seconds
189
+ * @since 0.2.0
190
+ * @param [Fixnum] sec
191
+ * @return [nil]
192
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_TIMEOUT)
193
+ */
194
+ static VALUE m_set_timeout(VALUE self, VALUE sec) {
195
+ return set_long_option(self, SSH_OPTIONS_TIMEOUT, sec);
196
+ }
197
+
198
+ /*
199
+ * @overload timeout_usec=(usec)
200
+ * Set a timeout for the connection in micro seconds
201
+ * @since 0.2.0
202
+ * @param [Fixnum] usec
203
+ * @return [nil]
204
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_TIMEOUT_USEC)
205
+ */
206
+ static VALUE m_set_timeout_usec(VALUE self, VALUE usec) {
207
+ return set_long_option(self, SSH_OPTIONS_TIMEOUT_USEC, usec);
208
+ }
209
+
210
+ /*
211
+ * @overload protocol=(protocol)
212
+ * Set allowed SSH protocols
213
+ * @since 0.2.0
214
+ * @param [Array<Integer>] protocol
215
+ * @return [nil]
216
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_SSH1)
217
+ */
218
+ static VALUE m_set_protocol(VALUE self, VALUE protocols) {
219
+ SessionHolder *holder;
220
+ VALUE protocol;
221
+ int i, ssh1 = 0, ssh2 = 0;
222
+
223
+ Check_Type(protocols, T_ARRAY);
224
+
225
+ for (i = 0; i < RARRAY_LEN(protocols); i++) {
226
+ protocol = rb_ary_entry(protocols, i);
227
+ Check_Type(protocol, T_FIXNUM);
228
+ switch (FIX2INT(protocol)) {
229
+ case 1:
230
+ ssh1 = 1;
231
+ break;
232
+ case 2:
233
+ ssh2 = 1;
234
+ break;
235
+ default:
236
+ rb_raise(rb_eArgError, "protocol should be 1 or 2");
237
+ }
238
+ }
239
+
240
+ TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
241
+ RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_SSH1, &ssh1));
242
+ RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_SSH2, &ssh2));
243
+
244
+ return Qnil;
245
+ }
246
+
247
+ static VALUE set_comma_separated_option(VALUE self, enum ssh_options_e type,
248
+ VALUE ary) {
249
+ VALUE str;
250
+
251
+ Check_Type(ary, T_ARRAY);
252
+ str = rb_ary_join(ary, rb_str_new_cstr(","));
253
+
254
+ return set_string_option(self, type, str);
255
+ }
256
+
257
+ /*
258
+ * @overload key_exchange=(methods)
259
+ * Set the key exchange method to be used
260
+ * @since 0.2.0
261
+ * @param [Array<String>] methods
262
+ * @return [nil]
263
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_KEY_EXCHANGE)
264
+ */
265
+ static VALUE m_set_key_exchange(VALUE self, VALUE kex) {
266
+ return set_comma_separated_option(self, SSH_OPTIONS_KEY_EXCHANGE, kex);
267
+ }
268
+
269
+ /*
270
+ * @overload hostkeys=(key_types)
271
+ * Set the preferred server host key types
272
+ * @since 0.2.0
273
+ * @param [Array<String>] key_types
274
+ * @return [nil]
275
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_HOSTKEYS)
276
+ */
277
+ static VALUE m_set_hostkeys(VALUE self, VALUE hostkeys) {
278
+ return set_comma_separated_option(self, SSH_OPTIONS_HOSTKEYS, hostkeys);
279
+ }
280
+
281
+ /*
282
+ * @overload compression=(algorithm)
283
+ * Set the compression to use for both directions communication
284
+ * @since 0.2.0
285
+ * @param [TrueClass, FalseClass] algorithm
286
+ * @param [String] algorithm e.g. "yes", "no", "zlib", "zlib@openssh.com", "none"
287
+ * @return [nil]
288
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_COMPRESSION)
289
+ */
290
+ static VALUE m_set_compression(VALUE self, VALUE compression) {
291
+ SessionHolder *holder;
292
+ char *val;
293
+ if (compression == Qtrue || compression == Qfalse) {
294
+ if (compression == Qtrue) {
295
+ val = "yes";
296
+ } else {
297
+ val = "no";
298
+ }
299
+
300
+ TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
301
+ RAISE_IF_ERROR(
302
+ ssh_options_set(holder->session, SSH_OPTIONS_COMPRESSION, val));
303
+
304
+ return Qnil;
305
+ } else {
306
+ return set_string_option(self, SSH_OPTIONS_COMPRESSION, compression);
307
+ }
308
+ }
309
+
310
+ /*
311
+ * @overload compression_level=(level)
312
+ * Set the compression level to use for zlib functions
313
+ * @since 0.2.0
314
+ * @param [Fixnum] level
315
+ * @return [nil]
316
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_COMPRESSION_LEVEL)
317
+ */
318
+ static VALUE m_set_compression_level(VALUE self, VALUE level) {
319
+ return set_int_option(self, SSH_OPTIONS_COMPRESSION_LEVEL, level);
320
+ }
321
+
322
+ /*
323
+ * @overload stricthostkeycheck=(enable)
324
+ * Set the parameter StrictHostKeyChecking to avoid asking about a fingerprint
325
+ * @since 0.2.0
326
+ * @param [TrueClass, FalseClass] enable
327
+ * @return [nil]
328
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_STRICTHOSTKEYCHECK)
329
+ */
330
+ static VALUE m_set_stricthostkeycheck(VALUE self, VALUE enable) {
331
+ return set_int_option(self, SSH_OPTIONS_STRICTHOSTKEYCHECK,
332
+ INT2FIX(RTEST(enable) ? 1 : 0));
333
+ }
334
+
335
+ /*
336
+ * @overload proxycommand=(command)
337
+ * Set the command to be executed in order to connect to server
338
+ * @since 0.2.0
339
+ * @param [String] command
340
+ * @return [nil]
341
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_PROXYCOMMAND)
342
+ */
343
+ static VALUE m_set_proxycommand(VALUE self, VALUE proxycommand) {
344
+ return set_string_option(self, SSH_OPTIONS_PROXYCOMMAND, proxycommand);
345
+ }
346
+
347
+ /*
348
+ * @overload gssapi_client_identity=(identity)
349
+ * Set the GSSAPI client identity that libssh should expect when connecting to the server
350
+ * @since 0.2.0
351
+ * @param [String] identity
352
+ * @return [nil]
353
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY)
354
+ */
355
+ static VALUE m_set_gssapi_client_identity(VALUE self, VALUE identity) {
356
+ return set_string_option(self, SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY, identity);
357
+ }
358
+
359
+ /*
360
+ * @overload gssapi_server_identity=(identity)
361
+ * Set the GSSAPI server identity that libssh should expect when connecting to the server
362
+ * @since 0.2.0
363
+ * @param [String] identity
364
+ * @return [nil]
365
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_GSSAPI_SERVER_IDENTITY)
366
+ */
367
+ static VALUE m_set_gssapi_server_identity(VALUE self, VALUE identity) {
368
+ return set_string_option(self, SSH_OPTIONS_GSSAPI_SERVER_IDENTITY, identity);
369
+ }
370
+
371
+ /*
372
+ * @overload gssapi_delegate_credentials=(enable)
373
+ * Set whether GSSAPI should delegate credentials to the server
374
+ * @since 0.2.0
375
+ * @param [FalseClass, TrueClass] enable
376
+ * @return [nil]
377
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS)
378
+ */
379
+ static VALUE m_set_gssapi_delegate_credentials(VALUE self, VALUE enable) {
380
+ return set_int_option(self, SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS,
381
+ INT2FIX(RTEST(enable) ? 1 : 0));
382
+ }
383
+
384
+ /*
385
+ * @overload parse_config(path = nil)
386
+ * Parse the ssh_config file.
387
+ * @since 0.1.0
388
+ * @param [String, nil] Path to ssh_config. If +nil+, the default ~/.ssh/config will be used.
389
+ * @return [Boolean] Parsing the ssh_config was successful or not.
390
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_parse_config
391
+ */
99
392
  static VALUE m_parse_config(int argc, VALUE *argv, VALUE self) {
100
393
  SessionHolder *holder;
101
394
  VALUE path;
@@ -117,6 +410,14 @@ static VALUE m_parse_config(int argc, VALUE *argv, VALUE self) {
117
410
  }
118
411
  }
119
412
 
413
+ /*
414
+ * @overload add_identity(path_format)
415
+ * Add the identity file name format.
416
+ * @since 0.1.0
417
+ * @param [String] path_format Format string for identity file.
418
+ * @return [nil]
419
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_options_set(SSH_OPTIONS_ADD_IDENTITY)
420
+ */
120
421
  static VALUE m_add_identity(VALUE self, VALUE path) {
121
422
  SessionHolder *holder;
122
423
 
@@ -138,6 +439,13 @@ static void *nogvl_connect(void *ptr) {
138
439
  return NULL;
139
440
  }
140
441
 
442
+ /*
443
+ * @overload connect
444
+ * Connect to the SSH server.
445
+ * @since 0.1.0
446
+ * @return [nil]
447
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_connect
448
+ */
141
449
  static VALUE m_connect(VALUE self) {
142
450
  SessionHolder *holder;
143
451
  struct nogvl_session_args args;
@@ -150,6 +458,13 @@ static VALUE m_connect(VALUE self) {
150
458
  return Qnil;
151
459
  }
152
460
 
461
+ /*
462
+ * @overload server_known
463
+ * Check if the server is knonw.
464
+ * @since 0.1.0
465
+ * @return [Fixnum]
466
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_is_server_known
467
+ */
153
468
  static VALUE m_server_known(VALUE self) {
154
469
  SessionHolder *holder;
155
470
  int rc;
@@ -160,6 +475,13 @@ static VALUE m_server_known(VALUE self) {
160
475
  return INT2FIX(rc);
161
476
  }
162
477
 
478
+ /*
479
+ * @overload userauth_none
480
+ * Try to authenticate through then "none" method.
481
+ * @since 0.1.0
482
+ * @return [Fixnum]
483
+ * @see http://api.libssh.org/stable/group__libssh__auth.html ssh_userauth_none
484
+ */
163
485
  static VALUE m_userauth_none(VALUE self) {
164
486
  SessionHolder *holder;
165
487
  int rc;
@@ -170,6 +492,13 @@ static VALUE m_userauth_none(VALUE self) {
170
492
  return INT2FIX(rc);
171
493
  }
172
494
 
495
+ /*
496
+ * @overload userauth_list
497
+ * Get available authentication methods from the server.
498
+ * @since 0.1.0
499
+ * @return [Array<Symbol>]
500
+ * @see http://api.libssh.org/stable/group__libssh__auth.html ssh_userauth_list
501
+ */
173
502
  static VALUE m_userauth_list(VALUE self) {
174
503
  SessionHolder *holder;
175
504
  int list;
@@ -207,6 +536,13 @@ static void *nogvl_userauth_publickey_auto(void *ptr) {
207
536
  return NULL;
208
537
  }
209
538
 
539
+ /*
540
+ * @overload userauth_publickey_auto
541
+ * Try to automatically authenticate with public key and "none".
542
+ * @since 0.1.0
543
+ * @return [Fixnum]
544
+ * @see http://api.libssh.org/stable/group__libssh__auth.html ssh_userauth_publickey_auto
545
+ */
210
546
  static VALUE m_userauth_publickey_auto(VALUE self) {
211
547
  SessionHolder *holder;
212
548
  struct nogvl_session_args args;
@@ -218,6 +554,13 @@ static VALUE m_userauth_publickey_auto(VALUE self) {
218
554
  return INT2FIX(args.rc);
219
555
  }
220
556
 
557
+ /*
558
+ * @overload get_publickey
559
+ * Get the server public key from a session.
560
+ * @since 0.1.0
561
+ * @return [Key]
562
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_get_publickey
563
+ */
221
564
  static VALUE m_get_publickey(VALUE self) {
222
565
  SessionHolder *holder;
223
566
  KeyHolder *key_holder;
@@ -230,6 +573,13 @@ static VALUE m_get_publickey(VALUE self) {
230
573
  return key;
231
574
  }
232
575
 
576
+ /*
577
+ * @overload write_knownhost
578
+ * Write the current server as known in the known_hosts file.
579
+ * @since 0.1.0
580
+ * @return [nil]
581
+ * @see http://api.libssh.org/stable/group__libssh__session.html ssh_write_knownhost
582
+ */
233
583
  static VALUE m_write_knownhost(VALUE self) {
234
584
  SessionHolder *holder;
235
585
 
@@ -257,9 +607,43 @@ void Init_libssh_session() {
257
607
 
258
608
  rb_define_method(rb_cLibSSHSession, "initialize",
259
609
  RUBY_METHOD_FUNC(m_initialize), 0);
610
+
260
611
  rb_define_method(rb_cLibSSHSession, "log_verbosity=",
261
612
  RUBY_METHOD_FUNC(m_set_log_verbosity), 1);
262
613
  rb_define_method(rb_cLibSSHSession, "host=", RUBY_METHOD_FUNC(m_set_host), 1);
614
+ rb_define_method(rb_cLibSSHSession, "user=", RUBY_METHOD_FUNC(m_set_user), 1);
615
+ rb_define_method(rb_cLibSSHSession, "port=", RUBY_METHOD_FUNC(m_set_port), 1);
616
+ rb_define_method(rb_cLibSSHSession, "bindaddr=",
617
+ RUBY_METHOD_FUNC(m_set_bindaddr), 1);
618
+ rb_define_method(rb_cLibSSHSession, "knownhosts=",
619
+ RUBY_METHOD_FUNC(m_set_knownhosts), 1);
620
+ rb_define_method(rb_cLibSSHSession, "timeout=",
621
+ RUBY_METHOD_FUNC(m_set_timeout), 1);
622
+ rb_define_method(rb_cLibSSHSession, "timeout_usec=",
623
+ RUBY_METHOD_FUNC(m_set_timeout_usec), 1);
624
+ rb_define_method(rb_cLibSSHSession, "protocol=",
625
+ RUBY_METHOD_FUNC(m_set_protocol), 1);
626
+ rb_define_method(rb_cLibSSHSession, "key_exchange=",
627
+ RUBY_METHOD_FUNC(m_set_key_exchange), 1);
628
+ rb_define_method(rb_cLibSSHSession, "hostkeys=",
629
+ RUBY_METHOD_FUNC(m_set_hostkeys), 1);
630
+ rb_define_method(rb_cLibSSHSession, "compression=",
631
+ RUBY_METHOD_FUNC(m_set_compression), 1);
632
+ rb_define_method(rb_cLibSSHSession, "compression_level=",
633
+ RUBY_METHOD_FUNC(m_set_compression_level), 1);
634
+ rb_define_method(rb_cLibSSHSession, "compression_level=",
635
+ RUBY_METHOD_FUNC(m_set_compression_level), 1);
636
+ rb_define_method(rb_cLibSSHSession, "stricthostkeycheck=",
637
+ RUBY_METHOD_FUNC(m_set_stricthostkeycheck), 1);
638
+ rb_define_method(rb_cLibSSHSession, "proxycommand=",
639
+ RUBY_METHOD_FUNC(m_set_proxycommand), 1);
640
+ rb_define_method(rb_cLibSSHSession, "gssapi_client_identity=",
641
+ RUBY_METHOD_FUNC(m_set_gssapi_client_identity), 1);
642
+ rb_define_method(rb_cLibSSHSession, "gssapi_server_identity=",
643
+ RUBY_METHOD_FUNC(m_set_gssapi_server_identity), 1);
644
+ rb_define_method(rb_cLibSSHSession, "gssapi_delegate_credentials=",
645
+ RUBY_METHOD_FUNC(m_set_gssapi_delegate_credentials), 1);
646
+
263
647
  rb_define_method(rb_cLibSSHSession, "parse_config",
264
648
  RUBY_METHOD_FUNC(m_parse_config), -1);
265
649
  rb_define_method(rb_cLibSSHSession, "add_identity",