api-sessions.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. @c -*-texinfo-*-
  2. @c This file is part of Guile-SSH Reference Manual.
  3. @c Copyright (C) 2014, 2015 Artyom V. Poptsov
  4. @c See the file guile-ssh.texi for copying conditions.
  5. @node Sessions
  6. @section Sessions
  7. @menu
  8. * Session Management::
  9. * Callbacks::
  10. @end menu
  11. @node Session Management
  12. @subsection Session Management
  13. @cindex sessions
  14. @tindex session
  15. A session is an object that holds all the information about connection
  16. to a specified server or a client. This information includes server's
  17. port and address, name of the user, compression level, private keys
  18. and so on.
  19. A session may contain zero or more channels (@pxref{Channels}).
  20. Channels are ``pipes'' that link the client and the server together
  21. and that can be used for transferring of data in both directions.
  22. So the overall picture can be thought like this:
  23. @example
  24. [client] [server]
  25. \____________________________/ SSH session
  26. ============================
  27. ============================ SSH channels
  28. ============================
  29. ____________________________
  30. / \
  31. @end example
  32. libssh docs say that there is no limit to number of channels for a
  33. single session in theory.
  34. This chapter describes session management. The code is in the
  35. @code{(ssh session)} module.
  36. @deffn {Scheme Procedure} session? x
  37. Returns @code{#t} if @var{x} is a Guile-SSH session, @code{#f}
  38. otherwise.
  39. @end deffn
  40. @deffn {Scheme Procedure} %make-session
  41. Create a new Guile-SSH session.
  42. @end deffn
  43. @deffn {Scheme Procedure} make-session [keywords]
  44. Create a new Guile-SSH session and set its options. Options can be
  45. passed as keywords.
  46. Return a new Guile-SSH session.
  47. This procedure is more convenient than primitive @code{%make-session},
  48. but on other hand it should be a bit slower because of additional
  49. checks.
  50. Example:
  51. @lisp
  52. (let ((s (make-session #:user "alice"
  53. #:host "example.com"
  54. #:port 12345
  55. #:identity "/home/alice/.ssh/id_rsa"
  56. #:log-verbosity 'nolog)))
  57. ...)
  58. @end lisp
  59. @end deffn
  60. @deffn {Scheme Procedure} blocking-flush! session timeout
  61. Blocking flush of the outgoing buffer of @var{session}. Return on of
  62. the following symbols:
  63. @table @samp
  64. @item ok
  65. Success.
  66. @item again
  67. @var{timeout} occured.
  68. @item error
  69. An error occured.
  70. @end table
  71. @end deffn
  72. @deffn {Scheme Procedure} session-set! session option value
  73. Set a @var{option} to @code{value} for the given Guile-SSH
  74. @var{session}. Throw a @code{guile-ssh-error} on error. Return value
  75. is undefined.
  76. Here is the description of available options. The description is
  77. based on libssh documentation:
  78. @table @samp
  79. @item host
  80. The hostname or @acronym{IP} address to connect to.
  81. Expected type of @var{value}: string.
  82. @item port
  83. The port to connect to.
  84. Expected type of @var{value}: number.
  85. @item fd
  86. The file descriptor to use.
  87. If you wish to open the socket yourself for a reason or another, set
  88. the file descriptor. Don't forget to set the hostname as the hostname
  89. is used as a key in the known_host mechanism.
  90. Expected type of @var{value}: number.
  91. @item bindaddr
  92. The address to bind the client to.
  93. Expected type of @var{value}: string.
  94. @item user
  95. The username for authentication.
  96. Expected type of @var{value}: string.
  97. @item ssh-dir
  98. Set the SSH directory.
  99. The ssh directory is used for files like known_hosts and identity
  100. (private and public key). It may include @code{%s} which will be
  101. replaced by the user home directory.
  102. Expected type of @var{value}: string.
  103. @item identity
  104. Set the identity file name. By default identity, @file{id_dsa} and
  105. @file{id_rsa} are checked.
  106. The identity file used authenticate with public key. It may include
  107. @code{%s} which will be replaced by the user home directory.
  108. @item knownhosts
  109. Set the known hosts file name. Default value is @file{~/.ssh/known_hosts}.
  110. The known hosts file is used to certify remote hosts are genuine. The
  111. string may include @code{%s} which will be replaced by the user home
  112. directory.
  113. Expected type of @var{value}: string.
  114. @item timeout
  115. Set a timeout for the connection in seconds.
  116. Expected type of @var{value}: number.
  117. @item timeout-usec
  118. Set a timeout for the connection in micro seconds.
  119. Expected type of @var{value}: number.
  120. @item ssh1
  121. Allow or deny the connection to SSH1 servers.
  122. Expected type of @var{value}: boolean.
  123. @item ssh2
  124. Allow or deny the connection to SSH2 servers
  125. Expected type of @var{value}: boolean.
  126. @item log-verbosity
  127. Set the session logging verbosity. Possible values:
  128. @table @samp
  129. @item nolog
  130. No logging at all
  131. @item rare
  132. Only rare and noteworthy events
  133. @item protocol
  134. High level protocol information
  135. @item packet
  136. Lower level protocol infomations, packet level
  137. @item functions
  138. Every function path
  139. @end table
  140. Expected type of @var{value}: symbol.
  141. @item ciphers-c-s
  142. Set the symmetric cipher client to server. The @var{value} must be a
  143. string of comma-separated values.
  144. @item ciphers-s-c
  145. Set the symmetric cipher server to client. The @var{value} must be a
  146. string of comma-separated values.
  147. @item compression-c-s
  148. Set the compression to use for client to server. The @var{value} must
  149. be ``yes'', ``no'' or a specific algorithm name if needed ("zlib",
  150. @verb{|"zlib@openssh.com"|}, "none").
  151. Expected type of @var{value}: string.
  152. @item compression-s-c
  153. Set the compression to use for server to client. The @var{value} must
  154. be ``yes'', ``no'' or a specific algorithm name if needed ("zlib",
  155. @verb{|"zlib@openssh.com"|}, "none").
  156. Expected type of @var{value}: string.
  157. @item proxycommand
  158. Set the command to be executed in order to connect to server.
  159. Expected type of @var{value}: string.
  160. @item stricthostkeycheck
  161. Set the parameter @code{StrictHostKeyChecking} to avoid asking about a
  162. fingerprint.
  163. @item compression
  164. Set the compression to use for both directions communication. The
  165. @var{value} must be ``yes'', ``no'' or a specific algorithm name if
  166. needed ("zlib", @verb{|"zlib@openssh.com"|}, "none").
  167. Expected type of @var{value}: string.
  168. @item compression-level
  169. Set the compression level to use for zlib functions. The @var{value}
  170. is expected to be a number from 1 to 9, 9 being the most efficient but
  171. slower.
  172. @item callbacks
  173. Set callbacks that will be called on related events (@pxref{Callbacks}.)
  174. Expected type of @var{value}: an association list (alist).
  175. @item config
  176. The option specifies whether an SSH config should be parsed or not, and
  177. optionally the path to a config file.
  178. Setting the @var{value} to @code{#t} means that the default
  179. @file{~/.ssh/config} should be parsed; in turn, setting the option to
  180. @code{#f} (the default value) means that the config should not be parsed at
  181. all. If the value is a string, then the string is expected to be a path to
  182. config file.
  183. The procedure reads the config file after all other specified options are set.
  184. When the config file is read, the options for @var{session} are set,
  185. overwriting those that were passed to the procedure.
  186. You @emph{must} specify at least a host name when using this option, otherwise
  187. the procedure will fail.
  188. Optionally you could use @code{session-parse-config!} procedure explicitly to
  189. read the config (see below.)
  190. Expected types of @var{value}: Either a string or a boolean value.
  191. @end table
  192. @end deffn
  193. @deffn {Scheme Procedure} session-parse-config! session [file-name]
  194. Parse an SSH config @var{file-name} and set @var{session} options. If
  195. @var{file-name} is not set, the default SSH @file{~/.ssh/config} is used.
  196. Throw @code{guile-ssh-error} on an error. Return value is undefined.
  197. @end deffn
  198. @deffn {Scheme Procedure} session-get session option
  199. Get value of the @var{option} for @var{session}. The @var{option} is expected
  200. to be a symbol.
  201. Please not that currently not all the possible session options can be gotten
  202. with this procedure. Here is the list of allowed options:
  203. @table @samp
  204. @item host
  205. @item port
  206. @item user
  207. @item identity
  208. @item proxycommand
  209. @item callbacks
  210. @end table
  211. @end deffn
  212. @deffn {Scheme Procedure} connect! session
  213. Connect @var{session} to a SSH server. Return one of the following symbols:
  214. @code{ok}, @code{again}, @code{error}.
  215. @end deffn
  216. @deffn {Scheme Procedure} disconnect! session
  217. Disconnect the @var{session}. This procedure can be used by a client
  218. as well as by a server.
  219. @end deffn
  220. @deffn {Scheme Procedure} authenticate-server session
  221. Authenticate the server.
  222. Throw @code{wrong-type-arg} exception if a disconnected @var{session} is
  223. passed as an argument.
  224. Return one of the following symbols:
  225. @table @samp
  226. @item ok
  227. The server is known and has not changed.
  228. @item known-changed
  229. The server key has changed. Either you are under attack or the
  230. administrator changed the key. You @emph{have} to warn the user about
  231. a possible attack.
  232. @item found-other
  233. The server gave use a key of a type while we had an other type
  234. recorded. It is a possible attack.
  235. @item not-known
  236. The server is unknown. User should confirm the MD5 is correct.
  237. @item file-not-found
  238. The known host file does not exist. The host is thus unknown. File
  239. will be created if host key is accepted.
  240. @item error
  241. An error occured.
  242. @end table
  243. @end deffn
  244. @deffn {Scheme Procedure} get-server-public-key session
  245. Get server public key from a @var{session}. Return the server's
  246. public key. Throw @code{guile-ssh-error} on error.
  247. Also throw @code{wrong-type-arg} exception if a disconnected @var{session} is
  248. passed as an argument.
  249. See also @code{get-public-key-hash} in @pxref{Keys}.
  250. @end deffn
  251. @deffn {Scheme Procedure} write-known-host! session
  252. Write the current server as known in the known hosts file. Throw
  253. @code{guile-ssh-error} on error. Throw @code{wrong-type-arg} exception if a
  254. disconnected session is passed as an argument. Return value is undefined.
  255. @end deffn
  256. @deffn {Scheme Procedure} connected? session
  257. Check if we are connected. Return @code{#f} if we are connected to a
  258. server, @code{#f} if we aren't.
  259. @end deffn
  260. @deffn {Scheme Procedure} get-error session
  261. @cindex handling session errors
  262. Retrieve the error text message from the last error related to
  263. @var{session}.
  264. @end deffn
  265. @deffn {Scheme Procedure} get-protocol-version session
  266. Get version of SSH protocol. Return 1 for SSH1, 2 for SSH2 or
  267. @code{#f} on error.
  268. Throw @code{wrong-type-arg} exception if a disconnected @var{session} is
  269. passed as an argument.
  270. @end deffn
  271. @node Callbacks
  272. @subsection Callbacks
  273. Guile-SSH uses an association list (alist) to represent session callbacks; the
  274. key is a callback name, and the value is expecting to be a procedure.
  275. Session callbacks is the way to handle some events, notably the incoming
  276. reverse port forwarding requests on the server side. Each callback is called
  277. with the optional @code{user-data} argument which can be specified in the
  278. callbacks alist as well.
  279. @deffn {Scheme Procedure} global-request-callback session message user-data
  280. A server-side callback that is called on a global request (e.g. when an SSH
  281. client asks for reverse port forwarding.)
  282. The callback should be set on an accepted Guile-SSH session (@pxref{Servers})
  283. in case when global requests must be handled; note that if the callback is not
  284. set then the server will always deny global requests, which may be confusing.
  285. Example:
  286. @lisp
  287. (define (handle-global-request session message user-data)
  288. (let ((port-number 12345))
  289. (message-reply-success message port-number)))
  290. ;; Let's suppose that the session was created earlier.
  291. ;; Now we can set our callback:
  292. (session-set! session
  293. 'callbacks
  294. `((user-data . #f)
  295. (global-request-callback . ,handle-global-request)))
  296. ;; Note that 'user-data' is optional, so the following example
  297. ;; is valid:
  298. (session-set! session
  299. 'callbacks
  300. `((global-request-callback . ,handle-global-request)))
  301. @end lisp
  302. @end deffn
  303. @deffn {Scheme Procedure} connect-status-callback session status user-data
  304. This callback is called during connection establishment process (that is,
  305. after @code{connect!} is called) with a server. A connection @var{status} is
  306. a number that shows what percentage of connection esablishment is done.
  307. Example:
  308. @lisp
  309. (define (print-status session status user-data)
  310. (let ((percentage (truncate (* status 100))))
  311. (format #t "~a: connecting ... ~a%~%" session percentage)))
  312. ;; Let's suppose that the session was created earlier.
  313. (session-set! session
  314. 'callbacks
  315. `((user-data . #f)
  316. (connect-status-callback . ,print-status)))
  317. ;; Or we can set two callbacks simultaneously:
  318. (define (handle-global-request session message user-data)
  319. (let ((port-number 12345))
  320. (message-reply-success message port-number)))
  321. (session-set! session
  322. 'callbacks
  323. `((user-data . #f)
  324. (connect-status-callback . ,print-status)
  325. (global-request-callback . ,handle-global-request)))
  326. @end lisp
  327. @end deffn
  328. @c Local Variables:
  329. @c TeX-master: "guile-ssh.texi"
  330. @c End: