auth.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* auth.c -- User authentication procedures.
  2. *
  3. * Copyright (C) 2013, 2014 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  4. *
  5. * This file is part of Guile-SSH.
  6. *
  7. * Guile-SSH is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * Guile-SSH is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <libguile.h>
  22. #include <libssh/libssh.h>
  23. #include "error.h"
  24. #include "session-type.h"
  25. #include "key-type.h"
  26. #include "key-func.h"
  27. /* On the username:
  28. Some libssh functions (such as `ssh_userauth_password') expect
  29. username as one of the parameters. But according to libssh 0.6
  30. docs, most server implementations do not permit changing the
  31. username during authentication. Moreover, in some cases username
  32. parameter is already marked as deprecated in libssh 0.5.3. So I
  33. decided to simplify the Guile-SSH Auth API and eliminate username
  34. from parameter list of functions of this module. The username must
  35. be set by `session-set!' call. - avp */
  36. /* Convert SSH authentication result to a Scheme symbol
  37. Return a symbol, or #f on error. */
  38. static SCM
  39. ssh_auth_result_to_symbol (const int res)
  40. {
  41. switch (res)
  42. {
  43. case SSH_AUTH_SUCCESS:
  44. return scm_from_locale_symbol ("success");
  45. case SSH_AUTH_ERROR:
  46. return scm_from_locale_symbol ("error");
  47. case SSH_AUTH_DENIED:
  48. return scm_from_locale_symbol ("denied");
  49. case SSH_AUTH_PARTIAL:
  50. return scm_from_locale_symbol ("partial");
  51. case SSH_AUTH_AGAIN:
  52. return scm_from_locale_symbol ("again");
  53. default:
  54. return SCM_BOOL_F;
  55. }
  56. }
  57. SCM_DEFINE (guile_ssh_userauth_public_key_x, "userauth-public-key!", 2, 0, 0,
  58. (SCM session_smob,
  59. SCM private_key_smob),
  60. "\
  61. Try to authenticate with a public key.\n\
  62. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  63. ")
  64. #define FUNC_NAME s_guile_ssh_userauth_public_key_x
  65. {
  66. struct session_data *session_data = _scm_to_session_data (session_smob);
  67. struct key_data *private_key_data = _scm_to_key_data (private_key_smob);
  68. /* See "On the username" commentary above. */
  69. char *username = NULL;
  70. int res;
  71. /* Check types. */
  72. GSSH_VALIDATE_CONNECTED_SESSION (session_data, session_smob, SCM_ARG1);
  73. SCM_ASSERT (_private_key_p (private_key_data),
  74. private_key_smob, SCM_ARG2, FUNC_NAME);
  75. res = ssh_userauth_publickey (session_data->ssh_session, username,
  76. private_key_data->ssh_key);
  77. return ssh_auth_result_to_symbol (res);
  78. }
  79. #undef FUNC_NAME
  80. SCM_DEFINE (guile_ssh_userauth_public_key_auto_x,
  81. "userauth-public-key/auto!", 1, 0, 0,
  82. (SCM session),
  83. "\
  84. Try to automatically authenticate with \"none\" method first and then with \n\
  85. public keys. If the key is encrypted the user will be asked for a \n\
  86. passphrase. Return one of the following symbols: error, denied, partial, \n\
  87. success.\n\
  88. \n\
  89. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  90. ")
  91. #define FUNC_NAME s_guile_ssh_userauth_public_key_auto_x
  92. {
  93. struct session_data *sd = _scm_to_session_data (session);
  94. char *username = NULL; /* See "On the username" commentary above. */
  95. char *passphrase = NULL;
  96. GSSH_VALIDATE_CONNECTED_SESSION (sd, session, SCM_ARG1);
  97. int res = ssh_userauth_publickey_auto (sd->ssh_session,
  98. username,
  99. passphrase); /* passphrase */
  100. return ssh_auth_result_to_symbol (res);
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (guile_ssh_userauth_public_key_try,
  104. "userauth-public-key/try", 2, 0, 0,
  105. (SCM session, SCM public_key),
  106. "\
  107. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  108. ")
  109. #define FUNC_NAME s_guile_ssh_userauth_public_key_try
  110. {
  111. struct session_data *sd = _scm_to_session_data (session);
  112. struct key_data *kd = _scm_to_key_data (public_key);
  113. char *username = NULL; /* See "On the username" commentary above */
  114. int res;
  115. GSSH_VALIDATE_CONNECTED_SESSION (sd, session, SCM_ARG1);
  116. SCM_ASSERT (_public_key_p (kd), public_key, SCM_ARG2, FUNC_NAME);
  117. if (! ssh_is_connected (sd->ssh_session))
  118. guile_ssh_error1 (FUNC_NAME, "Session is not connected", session);
  119. res = ssh_userauth_try_publickey (sd->ssh_session, username, kd->ssh_key);
  120. return ssh_auth_result_to_symbol (res);
  121. }
  122. #undef FUNC_NAME
  123. SCM_DEFINE (guile_ssh_userauth_agent_x,
  124. "userauth-agent!", 1, 0, 0,
  125. (SCM session),
  126. /* FIXME: Fix the docsring. */
  127. "\
  128. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  129. ")
  130. #define FUNC_NAME s_guile_ssh_userauth_agent_x
  131. {
  132. struct session_data *sd = _scm_to_session_data (session);
  133. char *username = NULL; /* See "On the username" commentary above. */
  134. int res;
  135. GSSH_VALIDATE_CONNECTED_SESSION (sd, session, SCM_ARG1);
  136. res = ssh_userauth_agent (sd->ssh_session, username);
  137. return ssh_auth_result_to_symbol (res);
  138. }
  139. #undef FUNC_NAME
  140. /* Try to authenticate by password. */
  141. SCM_DEFINE (guile_ssh_userauth_password_x, "userauth-password!", 2, 0, 0,
  142. (SCM session, SCM password),
  143. "\
  144. Try to authenticate by password.\n\
  145. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  146. ")
  147. #define FUNC_NAME s_guile_ssh_userauth_password_x
  148. {
  149. struct session_data* session_data = _scm_to_session_data (session);
  150. /* See "On the username" commentary above. */
  151. char *username = NULL;
  152. char *c_password;
  153. int res;
  154. scm_dynwind_begin (0);
  155. /* Check types. */
  156. GSSH_VALIDATE_CONNECTED_SESSION (session_data, session, SCM_ARG1);
  157. SCM_ASSERT (scm_is_string (password), password, SCM_ARG2, FUNC_NAME);
  158. c_password = scm_to_locale_string (password);
  159. scm_dynwind_free (c_password);
  160. res = ssh_userauth_password (session_data->ssh_session,
  161. username,
  162. c_password);
  163. scm_dynwind_end ();
  164. return ssh_auth_result_to_symbol (res);
  165. }
  166. #undef FUNC_NAME
  167. /* Try to authenticate through the "none" method.
  168. Return one of the following symbols: 'success, 'error, 'denied,
  169. 'partial, 'again */
  170. SCM_DEFINE (guile_ssh_userauth_none_x, "userauth-none!", 1, 0, 0,
  171. (SCM arg1),
  172. "\
  173. Try to authenticate through the \"none\" method.\n\
  174. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  175. ")
  176. #define FUNC_NAME s_guile_ssh_userauth_none_x
  177. {
  178. struct session_data *session_data = _scm_to_session_data (arg1);
  179. int res;
  180. GSSH_VALIDATE_CONNECTED_SESSION (session_data, arg1, SCM_ARG1);
  181. /* username is deprecated parameter. Should be set to NULL. */
  182. res = ssh_userauth_none (session_data->ssh_session,
  183. NULL); /* Username */
  184. return ssh_auth_result_to_symbol (res);
  185. }
  186. #undef FUNC_NAME
  187. /* Get available authentication methods for a session SESSION_SMOB.
  188. Return list of available methods. */
  189. SCM_DEFINE (guile_ssh_userauth_get_list, "userauth-get-list", 1, 0, 0,
  190. (SCM session),
  191. "\
  192. Get available authentication methods for a session SESSION.\n\
  193. Throw `wrong-type-arg' if a disconnected SESSION is passed as an argument.\
  194. ")
  195. #define FUNC_NAME s_guile_ssh_userauth_get_list
  196. {
  197. struct session_data *session_data = _scm_to_session_data (session);
  198. SCM auth_list = SCM_EOL;
  199. int res;
  200. GSSH_VALIDATE_CONNECTED_SESSION (session_data, session, SCM_ARG1);
  201. /* The second argument of the function is a username. According to
  202. the documentation for libssh 0.5.3, this argument is deprecated
  203. and must be set to NULL. */
  204. res = ssh_userauth_list (session_data->ssh_session, NULL);
  205. if (res & SSH_AUTH_METHOD_PASSWORD)
  206. {
  207. SCM method = scm_from_locale_symbol ("password");
  208. auth_list = scm_append (scm_list_2 (auth_list, scm_list_1 (method)));
  209. }
  210. if (res & SSH_AUTH_METHOD_PUBLICKEY)
  211. {
  212. SCM method = scm_from_locale_symbol ("public-key");
  213. auth_list = scm_append (scm_list_2 (auth_list, scm_list_1 (method)));
  214. }
  215. if (res & SSH_AUTH_METHOD_HOSTBASED)
  216. {
  217. SCM method = scm_from_locale_symbol ("host-based");
  218. auth_list = scm_append (scm_list_2 (auth_list, scm_list_1 (method)));
  219. }
  220. if (res & SSH_AUTH_METHOD_INTERACTIVE)
  221. {
  222. SCM method = scm_from_locale_symbol ("interactive");
  223. auth_list = scm_append (scm_list_2 (auth_list, scm_list_1 (method)));
  224. }
  225. return auth_list;
  226. }
  227. #undef FUNC_NAME
  228. /* Initialization */
  229. void
  230. init_auth_func (void)
  231. {
  232. #include "auth.x"
  233. }
  234. /* auth.c ends here */