cli-auth.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * Copyright (c) 2004 by Mihnea Stoenescu
  6. * All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE. */
  25. #include "includes.h"
  26. #include "session.h"
  27. #include "auth.h"
  28. #include "dbutil.h"
  29. #include "buffer.h"
  30. #include "ssh.h"
  31. #include "packet.h"
  32. #include "runopts.h"
  33. void cli_authinitialise() {
  34. memset(&ses.authstate, 0, sizeof(ses.authstate));
  35. }
  36. /* Send a "none" auth request to get available methods */
  37. void cli_auth_getmethods() {
  38. TRACE(("enter cli_auth_getmethods"))
  39. CHECKCLEARTOWRITE();
  40. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
  41. buf_putstring(ses.writepayload, cli_opts.username,
  42. strlen(cli_opts.username));
  43. buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
  44. SSH_SERVICE_CONNECTION_LEN);
  45. buf_putstring(ses.writepayload, "none", 4); /* 'none' method */
  46. encrypt_packet();
  47. #ifdef DROPBEAR_CLI_IMMEDIATE_AUTH
  48. /* We can't haven't two auth requests in-flight with delayed zlib mode
  49. since if the first one succeeds then the remote side will
  50. expect the second one to be compressed.
  51. Race described at
  52. http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/zlib-openssh.html
  53. */
  54. if (ses.keys->trans.algo_comp != DROPBEAR_COMP_ZLIB_DELAY) {
  55. ses.authstate.authtypes = AUTH_TYPE_PUBKEY;
  56. if (getenv(DROPBEAR_PASSWORD_ENV)) {
  57. ses.authstate.authtypes |= AUTH_TYPE_PASSWORD | AUTH_TYPE_INTERACT;
  58. }
  59. if (cli_auth_try() == DROPBEAR_SUCCESS) {
  60. TRACE(("skipped initial none auth query"))
  61. /* Note that there will be two auth responses in-flight */
  62. cli_ses.ignore_next_auth_response = 1;
  63. }
  64. }
  65. #endif
  66. TRACE(("leave cli_auth_getmethods"))
  67. }
  68. void recv_msg_userauth_banner() {
  69. char* banner = NULL;
  70. unsigned int bannerlen;
  71. unsigned int i, linecount;
  72. TRACE(("enter recv_msg_userauth_banner"))
  73. if (ses.authstate.authdone) {
  74. TRACE(("leave recv_msg_userauth_banner: banner after auth done"))
  75. return;
  76. }
  77. banner = buf_getstring(ses.payload, &bannerlen);
  78. buf_eatstring(ses.payload); /* The language string */
  79. if (bannerlen > MAX_BANNER_SIZE) {
  80. TRACE(("recv_msg_userauth_banner: bannerlen too long: %d", bannerlen))
  81. goto out;
  82. }
  83. cleantext(banner);
  84. /* Limit to 25 lines */
  85. linecount = 1;
  86. for (i = 0; i < bannerlen; i++) {
  87. if (banner[i] == '\n') {
  88. if (linecount >= MAX_BANNER_LINES) {
  89. banner[i] = '\0';
  90. break;
  91. }
  92. linecount++;
  93. }
  94. }
  95. fprintf(stderr, "%s\n", banner);
  96. out:
  97. m_free(banner);
  98. TRACE(("leave recv_msg_userauth_banner"))
  99. }
  100. /* This handles the message-specific types which
  101. * all have a value of 60. These are
  102. * SSH_MSG_USERAUTH_PASSWD_CHANGEREQ,
  103. * SSH_MSG_USERAUTH_PK_OK, &
  104. * SSH_MSG_USERAUTH_INFO_REQUEST. */
  105. void recv_msg_userauth_specific_60() {
  106. #ifdef ENABLE_CLI_PUBKEY_AUTH
  107. if (cli_ses.lastauthtype == AUTH_TYPE_PUBKEY) {
  108. recv_msg_userauth_pk_ok();
  109. return;
  110. }
  111. #endif
  112. #ifdef ENABLE_CLI_INTERACT_AUTH
  113. if (cli_ses.lastauthtype == AUTH_TYPE_INTERACT) {
  114. recv_msg_userauth_info_request();
  115. return;
  116. }
  117. #endif
  118. #ifdef ENABLE_CLI_PASSWORD_AUTH
  119. if (cli_ses.lastauthtype == AUTH_TYPE_PASSWORD) {
  120. /* Eventually there could be proper password-changing
  121. * support. However currently few servers seem to
  122. * implement it, and password auth is last-resort
  123. * regardless - keyboard-interactive is more likely
  124. * to be used anyway. */
  125. dropbear_close("Your password has expired.");
  126. }
  127. #endif
  128. dropbear_exit("Unexpected userauth packet");
  129. }
  130. void recv_msg_userauth_failure() {
  131. char * methods = NULL;
  132. char * tok = NULL;
  133. unsigned int methlen = 0;
  134. unsigned int partial = 0;
  135. unsigned int i = 0;
  136. TRACE(("<- MSG_USERAUTH_FAILURE"))
  137. TRACE(("enter recv_msg_userauth_failure"))
  138. if (ses.authstate.authdone) {
  139. TRACE(("leave recv_msg_userauth_failure, already authdone."))
  140. return;
  141. }
  142. if (cli_ses.state != USERAUTH_REQ_SENT) {
  143. /* Perhaps we should be more fatal? */
  144. dropbear_exit("Unexpected userauth failure");
  145. }
  146. /* When DROPBEAR_CLI_IMMEDIATE_AUTH is set there will be an initial response for
  147. the "none" auth request, and then a response to the immediate auth request.
  148. We need to be careful handling them. */
  149. if (cli_ses.ignore_next_auth_response) {
  150. cli_ses.state = USERAUTH_REQ_SENT;
  151. cli_ses.ignore_next_auth_response = 0;
  152. TRACE(("leave recv_msg_userauth_failure, ignored response, state set to USERAUTH_REQ_SENT"));
  153. return;
  154. } else {
  155. #ifdef ENABLE_CLI_PUBKEY_AUTH
  156. /* If it was a pubkey auth request, we should cross that key
  157. * off the list. */
  158. if (cli_ses.lastauthtype == AUTH_TYPE_PUBKEY) {
  159. cli_pubkeyfail();
  160. }
  161. #endif
  162. #ifdef ENABLE_CLI_INTERACT_AUTH
  163. /* If we get a failure message for keyboard interactive without
  164. * receiving any request info packet, then we don't bother trying
  165. * keyboard interactive again */
  166. if (cli_ses.lastauthtype == AUTH_TYPE_INTERACT
  167. && !cli_ses.interact_request_received) {
  168. TRACE(("setting auth_interact_failed = 1"))
  169. cli_ses.auth_interact_failed = 1;
  170. }
  171. #endif
  172. cli_ses.state = USERAUTH_FAIL_RCVD;
  173. cli_ses.lastauthtype = AUTH_TYPE_NONE;
  174. }
  175. methods = buf_getstring(ses.payload, &methlen);
  176. partial = buf_getbool(ses.payload);
  177. if (partial) {
  178. dropbear_log(LOG_INFO, "Authentication partially succeeded, more attempts required");
  179. } else {
  180. ses.authstate.failcount++;
  181. }
  182. TRACE(("Methods (len %d): '%s'", methlen, methods))
  183. ses.authstate.authdone=0;
  184. ses.authstate.authtypes=0;
  185. /* Split with nulls rather than commas */
  186. for (i = 0; i < methlen; i++) {
  187. if (methods[i] == ',') {
  188. methods[i] = '\0';
  189. }
  190. }
  191. tok = methods; /* tok stores the next method we'll compare */
  192. for (i = 0; i <= methlen; i++) {
  193. if (methods[i] == '\0') {
  194. TRACE(("auth method '%s'", tok))
  195. #ifdef ENABLE_CLI_PUBKEY_AUTH
  196. if (strncmp(AUTH_METHOD_PUBKEY, tok,
  197. AUTH_METHOD_PUBKEY_LEN) == 0) {
  198. ses.authstate.authtypes |= AUTH_TYPE_PUBKEY;
  199. }
  200. #endif
  201. #ifdef ENABLE_CLI_INTERACT_AUTH
  202. if (strncmp(AUTH_METHOD_INTERACT, tok,
  203. AUTH_METHOD_INTERACT_LEN) == 0) {
  204. ses.authstate.authtypes |= AUTH_TYPE_INTERACT;
  205. }
  206. #endif
  207. #ifdef ENABLE_CLI_PASSWORD_AUTH
  208. if (strncmp(AUTH_METHOD_PASSWORD, tok,
  209. AUTH_METHOD_PASSWORD_LEN) == 0) {
  210. ses.authstate.authtypes |= AUTH_TYPE_PASSWORD;
  211. }
  212. #endif
  213. tok = &methods[i+1]; /* Must make sure we don't use it after the
  214. last loop, since it'll point to something
  215. undefined */
  216. }
  217. }
  218. m_free(methods);
  219. TRACE(("leave recv_msg_userauth_failure"))
  220. }
  221. void recv_msg_userauth_success() {
  222. /* This function can validly get called multiple times
  223. if DROPBEAR_CLI_IMMEDIATE_AUTH is set */
  224. TRACE(("received msg_userauth_success"))
  225. /* Note: in delayed-zlib mode, setting authdone here
  226. * will enable compression in the transport layer */
  227. ses.authstate.authdone = 1;
  228. cli_ses.state = USERAUTH_SUCCESS_RCVD;
  229. cli_ses.lastauthtype = AUTH_TYPE_NONE;
  230. #ifdef ENABLE_CLI_PUBKEY_AUTH
  231. cli_auth_pubkey_cleanup();
  232. #endif
  233. }
  234. int cli_auth_try() {
  235. int finished = 0;
  236. TRACE(("enter cli_auth_try"))
  237. CHECKCLEARTOWRITE();
  238. /* Order to try is pubkey, interactive, password.
  239. * As soon as "finished" is set for one, we don't do any more. */
  240. #ifdef ENABLE_CLI_PUBKEY_AUTH
  241. if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) {
  242. finished = cli_auth_pubkey();
  243. cli_ses.lastauthtype = AUTH_TYPE_PUBKEY;
  244. }
  245. #endif
  246. #ifdef ENABLE_CLI_PASSWORD_AUTH
  247. if (!finished && (ses.authstate.authtypes & AUTH_TYPE_PASSWORD)) {
  248. if (ses.keys->trans.algo_crypt->cipherdesc == NULL) {
  249. fprintf(stderr, "Sorry, I won't let you use password auth unencrypted.\n");
  250. } else {
  251. cli_auth_password();
  252. finished = 1;
  253. cli_ses.lastauthtype = AUTH_TYPE_PASSWORD;
  254. }
  255. }
  256. #endif
  257. #ifdef ENABLE_CLI_INTERACT_AUTH
  258. if (!finished && (ses.authstate.authtypes & AUTH_TYPE_INTERACT)) {
  259. if (ses.keys->trans.algo_crypt->cipherdesc == NULL) {
  260. fprintf(stderr, "Sorry, I won't let you use interactive auth unencrypted.\n");
  261. } else {
  262. if (!cli_ses.auth_interact_failed) {
  263. cli_auth_interactive();
  264. cli_ses.lastauthtype = AUTH_TYPE_INTERACT;
  265. finished = 1;
  266. }
  267. }
  268. }
  269. #endif
  270. TRACE(("cli_auth_try lastauthtype %d", cli_ses.lastauthtype))
  271. if (finished) {
  272. TRACE(("leave cli_auth_try success"))
  273. return DROPBEAR_SUCCESS;
  274. }
  275. TRACE(("leave cli_auth_try failure"))
  276. return DROPBEAR_FAILURE;
  277. }
  278. #if defined(ENABLE_CLI_PASSWORD_AUTH) || defined(ENABLE_CLI_INTERACT_AUTH)
  279. /* A helper for getpass() that exits if the user cancels. The returned
  280. * password is statically allocated by getpass() */
  281. char* getpass_or_cancel(char* prompt)
  282. {
  283. char* password = NULL;
  284. #ifdef DROPBEAR_PASSWORD_ENV
  285. /* Password provided in an environment var */
  286. password = getenv(DROPBEAR_PASSWORD_ENV);
  287. if (password)
  288. {
  289. return password;
  290. }
  291. #endif
  292. password = getpass(prompt);
  293. /* 0x03 is a ctrl-c character in the buffer. */
  294. if (password == NULL || strchr(password, '\3') != NULL) {
  295. dropbear_close("Interrupted.");
  296. }
  297. return password;
  298. }
  299. #endif