123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include "includes.h"
- #include "session.h"
- #include "auth.h"
- #include "dbutil.h"
- #include "buffer.h"
- #include "ssh.h"
- #include "packet.h"
- #include "runopts.h"
- void cli_auth_getmethods() {
- TRACE(("enter cli_auth_getmethods"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
- buf_putstring(ses.writepayload, cli_opts.username,
- strlen(cli_opts.username));
- buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
- SSH_SERVICE_CONNECTION_LEN);
- buf_putstring(ses.writepayload, "none", 4);
- encrypt_packet();
- #if DROPBEAR_CLI_IMMEDIATE_AUTH
-
- if (ses.keys->trans.algo_comp != DROPBEAR_COMP_ZLIB_DELAY) {
- ses.authstate.authtypes = AUTH_TYPE_PUBKEY;
- #if DROPBEAR_USE_PASSWORD_ENV
- if (getenv(DROPBEAR_PASSWORD_ENV)) {
- ses.authstate.authtypes |= AUTH_TYPE_PASSWORD | AUTH_TYPE_INTERACT;
- }
- #endif
- if (cli_auth_try() == DROPBEAR_SUCCESS) {
- TRACE(("skipped initial none auth query"))
-
- cli_ses.ignore_next_auth_response = 1;
- }
- }
- #endif
- TRACE(("leave cli_auth_getmethods"))
- }
- void recv_msg_userauth_banner() {
- char* banner = NULL;
- unsigned int bannerlen;
- unsigned int i, linecount;
- int truncated = 0;
- TRACE(("enter recv_msg_userauth_banner"))
- if (ses.authstate.authdone) {
- TRACE(("leave recv_msg_userauth_banner: banner after auth done"))
- return;
- }
- if (cli_opts.quiet) {
- TRACE(("not showing banner"))
- return;
- }
- banner = buf_getstring(ses.payload, &bannerlen);
- buf_eatstring(ses.payload);
- if (bannerlen > MAX_BANNER_SIZE) {
- TRACE(("recv_msg_userauth_banner: bannerlen too long: %d", bannerlen))
- truncated = 1;
- } else {
- cleantext(banner);
-
- linecount = 1;
- for (i = 0; i < bannerlen; i++) {
- if (banner[i] == '\n') {
- if (linecount >= MAX_BANNER_LINES) {
- banner[i] = '\0';
- truncated = 1;
- break;
- }
- linecount++;
- }
- }
- fprintf(stderr, "%s\n", banner);
- }
- if (truncated) {
- fprintf(stderr, "[Banner from the server is too long]\n");
- }
- m_free(banner);
- TRACE(("leave recv_msg_userauth_banner"))
- }
- void recv_msg_userauth_specific_60() {
- #if DROPBEAR_CLI_PUBKEY_AUTH
- if (cli_ses.lastauthtype == AUTH_TYPE_PUBKEY) {
- recv_msg_userauth_pk_ok();
- return;
- }
- #endif
- #if DROPBEAR_CLI_INTERACT_AUTH
- if (cli_ses.lastauthtype == AUTH_TYPE_INTERACT) {
- recv_msg_userauth_info_request();
- return;
- }
- #endif
- #if DROPBEAR_CLI_PASSWORD_AUTH
- if (cli_ses.lastauthtype == AUTH_TYPE_PASSWORD) {
-
- dropbear_close("Your password has expired.");
- }
- #endif
- dropbear_exit("Unexpected userauth packet");
- }
- void recv_msg_userauth_failure() {
- char * methods = NULL;
- char * tok = NULL;
- unsigned int methlen = 0;
- unsigned int partial = 0;
- unsigned int i = 0;
- TRACE(("<- MSG_USERAUTH_FAILURE"))
- TRACE(("enter recv_msg_userauth_failure"))
- if (ses.authstate.authdone) {
- TRACE(("leave recv_msg_userauth_failure, already authdone."))
- return;
- }
- if (cli_ses.state != USERAUTH_REQ_SENT) {
-
- dropbear_exit("Unexpected userauth failure");
- }
-
- if (cli_ses.ignore_next_auth_response) {
- cli_ses.state = USERAUTH_REQ_SENT;
- cli_ses.ignore_next_auth_response = 0;
- TRACE(("leave recv_msg_userauth_failure, ignored response, state set to USERAUTH_REQ_SENT"));
- return;
- } else {
- #if DROPBEAR_CLI_PUBKEY_AUTH
-
- if (cli_ses.lastauthtype == AUTH_TYPE_PUBKEY) {
- cli_pubkeyfail();
- }
- #endif
- #if DROPBEAR_CLI_INTERACT_AUTH
-
- if (cli_ses.lastauthtype == AUTH_TYPE_INTERACT
- && !cli_ses.interact_request_received) {
- TRACE(("setting auth_interact_failed = 1"))
- cli_ses.auth_interact_failed = 1;
- }
- #endif
- cli_ses.state = USERAUTH_FAIL_RCVD;
- cli_ses.lastauthtype = AUTH_TYPE_NONE;
- }
- methods = buf_getstring(ses.payload, &methlen);
- partial = buf_getbool(ses.payload);
- if (partial) {
- dropbear_log(LOG_INFO, "Authentication partially succeeded, more attempts required");
- } else {
- ses.authstate.failcount++;
- }
- TRACE(("Methods (len %d): '%s'", methlen, methods))
- ses.authstate.authdone=0;
- ses.authstate.authtypes=0;
-
- for (i = 0; i < methlen; i++) {
- if (methods[i] == ',') {
- methods[i] = '\0';
- }
- }
- tok = methods;
- for (i = 0; i <= methlen; i++) {
- if (methods[i] == '\0') {
- TRACE(("auth method '%s'", tok))
- #if DROPBEAR_CLI_PUBKEY_AUTH
- if (strncmp(AUTH_METHOD_PUBKEY, tok,
- AUTH_METHOD_PUBKEY_LEN) == 0) {
- ses.authstate.authtypes |= AUTH_TYPE_PUBKEY;
- }
- #endif
- #if DROPBEAR_CLI_INTERACT_AUTH
- if (strncmp(AUTH_METHOD_INTERACT, tok,
- AUTH_METHOD_INTERACT_LEN) == 0) {
- ses.authstate.authtypes |= AUTH_TYPE_INTERACT;
- }
- #endif
- #if DROPBEAR_CLI_PASSWORD_AUTH
- if (strncmp(AUTH_METHOD_PASSWORD, tok,
- AUTH_METHOD_PASSWORD_LEN) == 0) {
- ses.authstate.authtypes |= AUTH_TYPE_PASSWORD;
- }
- #endif
- tok = &methods[i+1];
- }
- }
- m_free(methods);
-
- TRACE(("leave recv_msg_userauth_failure"))
- }
- void recv_msg_userauth_success() {
-
- DEBUG1(("received msg_userauth_success"))
- if (cli_opts.disable_trivial_auth && cli_ses.is_trivial_auth) {
- dropbear_exit("trivial authentication not allowed");
- }
-
- ses.authstate.authdone = 1;
- cli_ses.state = USERAUTH_SUCCESS_RCVD;
- cli_ses.lastauthtype = AUTH_TYPE_NONE;
- #if DROPBEAR_CLI_PUBKEY_AUTH
- cli_auth_pubkey_cleanup();
- #endif
- }
- int cli_auth_try() {
- int finished = 0;
- TRACE(("enter cli_auth_try"))
- CHECKCLEARTOWRITE();
-
-
- #if DROPBEAR_CLI_PUBKEY_AUTH
- if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) {
- finished = cli_auth_pubkey();
- cli_ses.lastauthtype = AUTH_TYPE_PUBKEY;
- }
- #endif
- #if DROPBEAR_CLI_PASSWORD_AUTH
- if (!finished && (ses.authstate.authtypes & AUTH_TYPE_PASSWORD)) {
- if (ses.keys->trans.algo_crypt->cipherdesc == NULL) {
- fprintf(stderr, "Sorry, I won't let you use password auth unencrypted.\n");
- } else {
- cli_auth_password();
- finished = 1;
- cli_ses.lastauthtype = AUTH_TYPE_PASSWORD;
- }
- }
- #endif
- #if DROPBEAR_CLI_INTERACT_AUTH
- if (!finished && (ses.authstate.authtypes & AUTH_TYPE_INTERACT)) {
- if (ses.keys->trans.algo_crypt->cipherdesc == NULL) {
- fprintf(stderr, "Sorry, I won't let you use interactive auth unencrypted.\n");
- } else {
- if (!cli_ses.auth_interact_failed) {
- cli_auth_interactive();
- cli_ses.lastauthtype = AUTH_TYPE_INTERACT;
- finished = 1;
- }
- }
- }
- #endif
- TRACE(("cli_auth_try lastauthtype %d", cli_ses.lastauthtype))
- if (finished) {
- TRACE(("leave cli_auth_try success"))
- return DROPBEAR_SUCCESS;
- }
- TRACE(("leave cli_auth_try failure"))
- return DROPBEAR_FAILURE;
- }
- #if DROPBEAR_CLI_PASSWORD_AUTH || DROPBEAR_CLI_INTERACT_AUTH
- char* getpass_or_cancel(const char* prompt)
- {
- char* password = NULL;
-
- #if DROPBEAR_USE_PASSWORD_ENV
-
- password = getenv(DROPBEAR_PASSWORD_ENV);
- if (password)
- {
- return password;
- }
- #endif
- password = getpass(prompt);
-
- if (password == NULL || strchr(password, '\3') != NULL) {
- dropbear_close("Interrupted.");
- }
- return password;
- }
- #endif
|