123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- #include "includes.h"
- #include "dbutil.h"
- #include "session.h"
- #include "buffer.h"
- #include "ssh.h"
- #include "packet.h"
- #include "auth.h"
- #include "runopts.h"
- #include "dbrandom.h"
- static int checkusername(const char *username, unsigned int userlen);
- void svr_authinitialise() {
- memset(&ses.authstate, 0, sizeof(ses.authstate));
- #if DROPBEAR_SVR_PUBKEY_AUTH
- ses.authstate.authtypes |= AUTH_TYPE_PUBKEY;
- #endif
- #if DROPBEAR_SVR_PASSWORD_AUTH || DROPBEAR_SVR_PAM_AUTH
- if (!svr_opts.noauthpass) {
- ses.authstate.authtypes |= AUTH_TYPE_PASSWORD;
- }
- #endif
- }
- void send_msg_userauth_banner(const buffer *banner) {
- TRACE(("enter send_msg_userauth_banner"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_BANNER);
- buf_putbufstring(ses.writepayload, banner);
- buf_putstring(ses.writepayload, "en", 2);
- encrypt_packet();
- TRACE(("leave send_msg_userauth_banner"))
- }
- void recv_msg_userauth_request() {
- char *username = NULL, *servicename = NULL, *methodname = NULL;
- unsigned int userlen, servicelen, methodlen;
- int valid_user = 0;
- TRACE(("enter recv_msg_userauth_request"))
-
- gettime_wrapper(&ses.authstate.auth_starttime);
-
- if (ses.authstate.authdone == 1) {
- TRACE(("leave recv_msg_userauth_request: authdone already"))
- return;
- }
-
- if (svr_opts.banner) {
- send_msg_userauth_banner(svr_opts.banner);
- buf_free(svr_opts.banner);
- svr_opts.banner = NULL;
- }
- username = buf_getstring(ses.payload, &userlen);
- servicename = buf_getstring(ses.payload, &servicelen);
- methodname = buf_getstring(ses.payload, &methodlen);
-
- if (servicelen != SSH_SERVICE_CONNECTION_LEN
- && (strncmp(servicename, SSH_SERVICE_CONNECTION,
- SSH_SERVICE_CONNECTION_LEN) != 0)) {
-
-
- m_free(username);
- m_free(servicename);
- m_free(methodname);
- dropbear_exit("unknown service in auth");
- }
-
- if (checkusername(username, userlen) == DROPBEAR_SUCCESS) {
- valid_user = 1;
- }
-
- if (methodlen == AUTH_METHOD_NONE_LEN &&
- strncmp(methodname, AUTH_METHOD_NONE,
- AUTH_METHOD_NONE_LEN) == 0) {
- TRACE(("recv_msg_userauth_request: 'none' request"))
- if (valid_user
- && svr_opts.allowblankpass
- && !svr_opts.noauthpass
- && !(svr_opts.norootpass && ses.authstate.pw_uid == 0)
- && ses.authstate.pw_passwd[0] == '\0')
- {
- dropbear_log(LOG_NOTICE,
- "Auth succeeded with blank password for '%s' from %s",
- ses.authstate.pw_name,
- svr_ses.addrstring);
- send_msg_userauth_success();
- goto out;
- }
- else
- {
-
- send_msg_userauth_failure(0, 0);
- goto out;
- }
- }
-
- #if DROPBEAR_SVR_PASSWORD_AUTH
- if (!svr_opts.noauthpass &&
- !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
-
- if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
- strncmp(methodname, AUTH_METHOD_PASSWORD,
- AUTH_METHOD_PASSWORD_LEN) == 0) {
- svr_auth_password(valid_user);
- goto out;
- }
- }
- #endif
- #if DROPBEAR_SVR_PAM_AUTH
- if (!svr_opts.noauthpass &&
- !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
-
- if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
- strncmp(methodname, AUTH_METHOD_PASSWORD,
- AUTH_METHOD_PASSWORD_LEN) == 0) {
- svr_auth_pam(valid_user);
- goto out;
- }
- }
- #endif
- #if DROPBEAR_SVR_PUBKEY_AUTH
-
- if (methodlen == AUTH_METHOD_PUBKEY_LEN &&
- strncmp(methodname, AUTH_METHOD_PUBKEY,
- AUTH_METHOD_PUBKEY_LEN) == 0) {
- svr_auth_pubkey(valid_user);
- goto out;
- }
- #endif
-
- send_msg_userauth_failure(0, 1);
- out:
- m_free(username);
- m_free(servicename);
- m_free(methodname);
- }
- #ifdef HAVE_GETGROUPLIST
- static int check_group_membership(gid_t check_gid, const char* username, gid_t user_gid) {
- int ngroups, i, ret;
- gid_t *grouplist = NULL;
- int match = DROPBEAR_FAILURE;
- for (ngroups = 32; ngroups <= DROPBEAR_NGROUP_MAX; ngroups *= 2) {
- grouplist = m_malloc(sizeof(gid_t) * ngroups);
-
- ret = getgrouplist(username, user_gid, grouplist, &ngroups);
- if (ret >= 0) {
- break;
- }
- m_free(grouplist);
- grouplist = NULL;
- }
- if (!grouplist) {
- dropbear_log(LOG_ERR, "Too many groups for user '%s'", username);
- return DROPBEAR_FAILURE;
- }
- for (i = 0; i < ngroups; i++) {
- if (grouplist[i] == check_gid) {
- match = DROPBEAR_SUCCESS;
- break;
- }
- }
- m_free(grouplist);
- return match;
- }
- #endif
- static int checkusername(const char *username, unsigned int userlen) {
- char* listshell = NULL;
- char* usershell = NULL;
- uid_t uid;
- TRACE(("enter checkusername"))
- if (userlen > MAX_USERNAME_LEN) {
- return DROPBEAR_FAILURE;
- }
- if (strlen(username) != userlen) {
- dropbear_exit("Attempted username with a null byte");
- }
- if (ses.authstate.username == NULL) {
-
- fill_passwd(username);
- ses.authstate.username = m_strdup(username);
- } else {
-
- if (strcmp(username, ses.authstate.username) != 0) {
- dropbear_exit("Client trying multiple usernames");
- }
- }
-
- if (ses.authstate.checkusername_failed) {
- TRACE(("checkusername: returning cached failure"))
- return DROPBEAR_FAILURE;
- }
-
- if (!ses.authstate.pw_name) {
- TRACE(("leave checkusername: user '%s' doesn't exist", username))
- dropbear_log(LOG_WARNING,
- "Login attempt for nonexistent user");
- ses.authstate.checkusername_failed = 1;
- return DROPBEAR_FAILURE;
- }
-
- uid = geteuid();
- if (!(DROPBEAR_SVR_MULTIUSER && uid == 0) && uid != ses.authstate.pw_uid) {
- TRACE(("running as nonroot, only server uid is allowed"))
- dropbear_log(LOG_WARNING,
- "Login attempt with wrong user %s",
- ses.authstate.pw_name);
- ses.authstate.checkusername_failed = 1;
- return DROPBEAR_FAILURE;
- }
-
- if (svr_opts.norootlogin && ses.authstate.pw_uid == 0) {
- TRACE(("leave checkusername: root login disabled"))
- dropbear_log(LOG_WARNING, "root login rejected");
- ses.authstate.checkusername_failed = 1;
- return DROPBEAR_FAILURE;
- }
-
- #ifdef HAVE_GETGROUPLIST
- if (svr_opts.restrict_group) {
- if (check_group_membership(svr_opts.restrict_group_gid,
- ses.authstate.pw_name, ses.authstate.pw_gid) == DROPBEAR_FAILURE) {
- dropbear_log(LOG_WARNING,
- "Logins are restricted to the group %s but user '%s' is not a member",
- svr_opts.restrict_group, ses.authstate.pw_name);
- ses.authstate.checkusername_failed = 1;
- return DROPBEAR_FAILURE;
- }
- }
- #endif
- TRACE(("shell is %s", ses.authstate.pw_shell))
-
- usershell = ses.authstate.pw_shell;
- if (usershell[0] == '\0') {
-
- usershell = "/bin/sh";
- }
-
- setusershell();
- while ((listshell = getusershell()) != NULL) {
- TRACE(("test shell is '%s'", listshell))
- if (strcmp(listshell, usershell) == 0) {
-
- goto goodshell;
- }
- }
-
- endusershell();
- TRACE(("no matching shell"))
- ses.authstate.checkusername_failed = 1;
- dropbear_log(LOG_WARNING, "User '%s' has invalid shell, rejected",
- ses.authstate.pw_name);
- return DROPBEAR_FAILURE;
-
- goodshell:
- endusershell();
- TRACE(("matching shell"))
- TRACE(("uid = %d", ses.authstate.pw_uid))
- TRACE(("leave checkusername"))
- return DROPBEAR_SUCCESS;
- }
- void send_msg_userauth_failure(int partial, int incrfail) {
- buffer *typebuf = NULL;
- TRACE(("enter send_msg_userauth_failure"))
- CHECKCLEARTOWRITE();
-
- buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_FAILURE);
-
- typebuf = buf_new(30);
- if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) {
- buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN);
- if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
- buf_putbyte(typebuf, ',');
- }
- }
-
- if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
- buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN);
- }
- buf_putbufstring(ses.writepayload, typebuf);
- TRACE(("auth fail: methods %d, '%.*s'", ses.authstate.authtypes,
- typebuf->len, typebuf->data))
- buf_free(typebuf);
- buf_putbyte(ses.writepayload, partial ? 1 : 0);
- encrypt_packet();
- if (incrfail) {
-
-
- const unsigned int mindelay = 250000000;
- const unsigned int vardelay = 100000000;
- unsigned int rand_delay;
- struct timespec delay;
- gettime_wrapper(&delay);
- delay.tv_sec -= ses.authstate.auth_starttime.tv_sec;
- delay.tv_nsec -= ses.authstate.auth_starttime.tv_nsec;
-
- if (delay.tv_nsec < 0) {
- delay.tv_nsec += 1000000000;
- delay.tv_sec -= 1;
- }
- genrandom((unsigned char*)&rand_delay, sizeof(rand_delay));
- rand_delay = mindelay + (rand_delay % vardelay);
- if (delay.tv_sec == 0 && delay.tv_nsec <= mindelay) {
-
- delay.tv_nsec = rand_delay - delay.tv_nsec;
- } else {
-
- delay.tv_sec = 0;
- delay.tv_nsec = rand_delay;
- }
- #if DROPBEAR_FUZZ
- if (!fuzz.fuzzing)
- #endif
- {
- while (nanosleep(&delay, &delay) == -1 && errno == EINTR) { }
- }
- ses.authstate.failcount++;
- }
- if (ses.authstate.failcount >= svr_opts.maxauthtries) {
- char * userstr;
-
- TRACE(("Max auth tries reached, exiting"))
- if (ses.authstate.pw_name == NULL) {
- userstr = "is invalid";
- } else {
- userstr = ses.authstate.pw_name;
- }
- dropbear_exit("Max auth tries reached - user '%s'",
- userstr);
- }
-
- TRACE(("leave send_msg_userauth_failure"))
- }
- void send_msg_userauth_success() {
- TRACE(("enter send_msg_userauth_success"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_SUCCESS);
- encrypt_packet();
-
- ses.authstate.authdone = 1;
- ses.connect_time = 0;
- if (ses.authstate.pw_uid == 0) {
- ses.allowprivport = 1;
- }
-
-
- m_close(svr_ses.childpipe);
- TRACE(("leave send_msg_userauth_success"))
- }
|