svr-auth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. /* This file (auth.c) handles authentication requests, passing it to the
  25. * particular type (auth-passwd, auth-pubkey). */
  26. #include "includes.h"
  27. #include "dbutil.h"
  28. #include "session.h"
  29. #include "buffer.h"
  30. #include "ssh.h"
  31. #include "packet.h"
  32. #include "auth.h"
  33. #include "runopts.h"
  34. #include "dbrandom.h"
  35. static int checkusername(const char *username, unsigned int userlen);
  36. /* initialise the first time for a session, resetting all parameters */
  37. void svr_authinitialise() {
  38. memset(&ses.authstate, 0, sizeof(ses.authstate));
  39. #if DROPBEAR_SVR_PUBKEY_AUTH
  40. ses.authstate.authtypes |= AUTH_TYPE_PUBKEY;
  41. #endif
  42. #if DROPBEAR_SVR_PASSWORD_AUTH || DROPBEAR_SVR_PAM_AUTH
  43. if (!svr_opts.noauthpass) {
  44. ses.authstate.authtypes |= AUTH_TYPE_PASSWORD;
  45. }
  46. #endif
  47. }
  48. /* Send a banner message if specified to the client. The client might
  49. * ignore this, but possibly serves as a legal "no trespassing" sign */
  50. void send_msg_userauth_banner(const buffer *banner) {
  51. TRACE(("enter send_msg_userauth_banner"))
  52. CHECKCLEARTOWRITE();
  53. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_BANNER);
  54. buf_putbufstring(ses.writepayload, banner);
  55. buf_putstring(ses.writepayload, "en", 2);
  56. encrypt_packet();
  57. TRACE(("leave send_msg_userauth_banner"))
  58. }
  59. /* handle a userauth request, check validity, pass to password or pubkey
  60. * checking, and handle success or failure */
  61. void recv_msg_userauth_request() {
  62. char *username = NULL, *servicename = NULL, *methodname = NULL;
  63. unsigned int userlen, servicelen, methodlen;
  64. int valid_user = 0;
  65. TRACE(("enter recv_msg_userauth_request"))
  66. /* for compensating failure delay */
  67. gettime_wrapper(&ses.authstate.auth_starttime);
  68. /* ignore packets if auth is already done */
  69. if (ses.authstate.authdone == 1) {
  70. TRACE(("leave recv_msg_userauth_request: authdone already"))
  71. return;
  72. }
  73. /* send the banner if it exists, it will only exist once */
  74. if (svr_opts.banner) {
  75. send_msg_userauth_banner(svr_opts.banner);
  76. buf_free(svr_opts.banner);
  77. svr_opts.banner = NULL;
  78. }
  79. username = buf_getstring(ses.payload, &userlen);
  80. servicename = buf_getstring(ses.payload, &servicelen);
  81. methodname = buf_getstring(ses.payload, &methodlen);
  82. /* only handle 'ssh-connection' currently */
  83. if (servicelen != SSH_SERVICE_CONNECTION_LEN
  84. && (strncmp(servicename, SSH_SERVICE_CONNECTION,
  85. SSH_SERVICE_CONNECTION_LEN) != 0)) {
  86. /* TODO - disconnect here */
  87. m_free(username);
  88. m_free(servicename);
  89. m_free(methodname);
  90. dropbear_exit("unknown service in auth");
  91. }
  92. /* check username is good before continuing.
  93. * the 'incrfail' varies depending on the auth method to
  94. * avoid giving away which users exist on the system through
  95. * the time delay. */
  96. if (checkusername(username, userlen) == DROPBEAR_SUCCESS) {
  97. valid_user = 1;
  98. }
  99. /* user wants to know what methods are supported */
  100. if (methodlen == AUTH_METHOD_NONE_LEN &&
  101. strncmp(methodname, AUTH_METHOD_NONE,
  102. AUTH_METHOD_NONE_LEN) == 0) {
  103. TRACE(("recv_msg_userauth_request: 'none' request"))
  104. if (valid_user
  105. && svr_opts.allowblankpass
  106. && !svr_opts.noauthpass
  107. && !(svr_opts.norootpass && ses.authstate.pw_uid == 0)
  108. && ses.authstate.pw_passwd[0] == '\0')
  109. {
  110. dropbear_log(LOG_NOTICE,
  111. "Auth succeeded with blank password for '%s' from %s",
  112. ses.authstate.pw_name,
  113. svr_ses.addrstring);
  114. send_msg_userauth_success();
  115. goto out;
  116. }
  117. else
  118. {
  119. /* 'none' has no failure delay */
  120. send_msg_userauth_failure(0, 0);
  121. goto out;
  122. }
  123. }
  124. #if DROPBEAR_SVR_PASSWORD_AUTH
  125. if (!svr_opts.noauthpass &&
  126. !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
  127. /* user wants to try password auth */
  128. if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
  129. strncmp(methodname, AUTH_METHOD_PASSWORD,
  130. AUTH_METHOD_PASSWORD_LEN) == 0) {
  131. svr_auth_password(valid_user);
  132. goto out;
  133. }
  134. }
  135. #endif
  136. #if DROPBEAR_SVR_PAM_AUTH
  137. if (!svr_opts.noauthpass &&
  138. !(svr_opts.norootpass && ses.authstate.pw_uid == 0) ) {
  139. /* user wants to try password auth */
  140. if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
  141. strncmp(methodname, AUTH_METHOD_PASSWORD,
  142. AUTH_METHOD_PASSWORD_LEN) == 0) {
  143. svr_auth_pam(valid_user);
  144. goto out;
  145. }
  146. }
  147. #endif
  148. #if DROPBEAR_SVR_PUBKEY_AUTH
  149. /* user wants to try pubkey auth */
  150. if (methodlen == AUTH_METHOD_PUBKEY_LEN &&
  151. strncmp(methodname, AUTH_METHOD_PUBKEY,
  152. AUTH_METHOD_PUBKEY_LEN) == 0) {
  153. svr_auth_pubkey(valid_user);
  154. goto out;
  155. }
  156. #endif
  157. /* nothing matched, we just fail with a delay */
  158. send_msg_userauth_failure(0, 1);
  159. out:
  160. m_free(username);
  161. m_free(servicename);
  162. m_free(methodname);
  163. }
  164. #ifdef HAVE_GETGROUPLIST
  165. /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  166. static int check_group_membership(gid_t check_gid, const char* username, gid_t user_gid) {
  167. int ngroups, i, ret;
  168. gid_t *grouplist = NULL;
  169. int match = DROPBEAR_FAILURE;
  170. for (ngroups = 32; ngroups <= DROPBEAR_NGROUP_MAX; ngroups *= 2) {
  171. grouplist = m_malloc(sizeof(gid_t) * ngroups);
  172. /* BSD returns ret==0 on success. Linux returns ret==ngroups on success */
  173. ret = getgrouplist(username, user_gid, grouplist, &ngroups);
  174. if (ret >= 0) {
  175. break;
  176. }
  177. m_free(grouplist);
  178. grouplist = NULL;
  179. }
  180. if (!grouplist) {
  181. dropbear_log(LOG_ERR, "Too many groups for user '%s'", username);
  182. return DROPBEAR_FAILURE;
  183. }
  184. for (i = 0; i < ngroups; i++) {
  185. if (grouplist[i] == check_gid) {
  186. match = DROPBEAR_SUCCESS;
  187. break;
  188. }
  189. }
  190. m_free(grouplist);
  191. return match;
  192. }
  193. #endif
  194. /* Check that the username exists and isn't disallowed (root), and has a valid shell.
  195. * returns DROPBEAR_SUCCESS on valid username, DROPBEAR_FAILURE on failure */
  196. static int checkusername(const char *username, unsigned int userlen) {
  197. char* listshell = NULL;
  198. char* usershell = NULL;
  199. uid_t uid;
  200. TRACE(("enter checkusername"))
  201. if (userlen > MAX_USERNAME_LEN) {
  202. return DROPBEAR_FAILURE;
  203. }
  204. if (strlen(username) != userlen) {
  205. dropbear_exit("Attempted username with a null byte");
  206. }
  207. if (ses.authstate.username == NULL) {
  208. /* first request */
  209. fill_passwd(username);
  210. ses.authstate.username = m_strdup(username);
  211. } else {
  212. /* check username hasn't changed */
  213. if (strcmp(username, ses.authstate.username) != 0) {
  214. dropbear_exit("Client trying multiple usernames");
  215. }
  216. }
  217. /* avoids cluttering logs with repeated failure messages from
  218. consecutive authentication requests in a sesssion */
  219. if (ses.authstate.checkusername_failed) {
  220. TRACE(("checkusername: returning cached failure"))
  221. return DROPBEAR_FAILURE;
  222. }
  223. /* check that user exists */
  224. if (!ses.authstate.pw_name) {
  225. TRACE(("leave checkusername: user '%s' doesn't exist", username))
  226. dropbear_log(LOG_WARNING,
  227. "Login attempt for nonexistent user");
  228. ses.authstate.checkusername_failed = 1;
  229. return DROPBEAR_FAILURE;
  230. }
  231. /* check if we are running as non-root, and login user is different from the server */
  232. uid = geteuid();
  233. if (!(DROPBEAR_SVR_MULTIUSER && uid == 0) && uid != ses.authstate.pw_uid) {
  234. TRACE(("running as nonroot, only server uid is allowed"))
  235. dropbear_log(LOG_WARNING,
  236. "Login attempt with wrong user %s",
  237. ses.authstate.pw_name);
  238. ses.authstate.checkusername_failed = 1;
  239. return DROPBEAR_FAILURE;
  240. }
  241. /* check for non-root if desired */
  242. if (svr_opts.norootlogin && ses.authstate.pw_uid == 0) {
  243. TRACE(("leave checkusername: root login disabled"))
  244. dropbear_log(LOG_WARNING, "root login rejected");
  245. ses.authstate.checkusername_failed = 1;
  246. return DROPBEAR_FAILURE;
  247. }
  248. /* check for login restricted to certain group if desired */
  249. #ifdef HAVE_GETGROUPLIST
  250. if (svr_opts.restrict_group) {
  251. if (check_group_membership(svr_opts.restrict_group_gid,
  252. ses.authstate.pw_name, ses.authstate.pw_gid) == DROPBEAR_FAILURE) {
  253. dropbear_log(LOG_WARNING,
  254. "Logins are restricted to the group %s but user '%s' is not a member",
  255. svr_opts.restrict_group, ses.authstate.pw_name);
  256. ses.authstate.checkusername_failed = 1;
  257. return DROPBEAR_FAILURE;
  258. }
  259. }
  260. #endif /* HAVE_GETGROUPLIST */
  261. TRACE(("shell is %s", ses.authstate.pw_shell))
  262. /* check that the shell is set */
  263. usershell = ses.authstate.pw_shell;
  264. if (usershell[0] == '\0') {
  265. /* empty shell in /etc/passwd means /bin/sh according to passwd(5) */
  266. usershell = "/bin/sh";
  267. }
  268. /* check the shell is valid. If /etc/shells doesn't exist, getusershell()
  269. * should return some standard shells like "/bin/sh" and "/bin/csh" (this
  270. * is platform-specific) */
  271. setusershell();
  272. while ((listshell = getusershell()) != NULL) {
  273. TRACE(("test shell is '%s'", listshell))
  274. if (strcmp(listshell, usershell) == 0) {
  275. /* have a match */
  276. goto goodshell;
  277. }
  278. }
  279. /* no matching shell */
  280. endusershell();
  281. TRACE(("no matching shell"))
  282. ses.authstate.checkusername_failed = 1;
  283. dropbear_log(LOG_WARNING, "User '%s' has invalid shell, rejected",
  284. ses.authstate.pw_name);
  285. return DROPBEAR_FAILURE;
  286. goodshell:
  287. endusershell();
  288. TRACE(("matching shell"))
  289. TRACE(("uid = %d", ses.authstate.pw_uid))
  290. TRACE(("leave checkusername"))
  291. return DROPBEAR_SUCCESS;
  292. }
  293. /* Send a failure message to the client, in responds to a userauth_request.
  294. * Partial indicates whether to set the "partial success" flag,
  295. * incrfail is whether to count this failure in the failure count (which
  296. * is limited. This function also handles disconnection after too many
  297. * failures */
  298. void send_msg_userauth_failure(int partial, int incrfail) {
  299. buffer *typebuf = NULL;
  300. TRACE(("enter send_msg_userauth_failure"))
  301. CHECKCLEARTOWRITE();
  302. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_FAILURE);
  303. /* put a list of allowed types */
  304. typebuf = buf_new(30); /* long enough for PUBKEY and PASSWORD */
  305. if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) {
  306. buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN);
  307. if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
  308. buf_putbyte(typebuf, ',');
  309. }
  310. }
  311. if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) {
  312. buf_putbytes(typebuf, (const unsigned char *)AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN);
  313. }
  314. buf_putbufstring(ses.writepayload, typebuf);
  315. TRACE(("auth fail: methods %d, '%.*s'", ses.authstate.authtypes,
  316. typebuf->len, typebuf->data))
  317. buf_free(typebuf);
  318. buf_putbyte(ses.writepayload, partial ? 1 : 0);
  319. encrypt_packet();
  320. if (incrfail) {
  321. /* The SSH_MSG_AUTH_FAILURE response is delayed to attempt to
  322. avoid user enumeration and slow brute force attempts.
  323. The delay is adjusted by the time already spent in processing
  324. authentication (ses.authstate.auth_starttime timestamp). */
  325. /* Desired total delay 300ms +-50ms (in nanoseconds).
  326. Beware of integer overflow if increasing these values */
  327. const unsigned int mindelay = 250000000;
  328. const unsigned int vardelay = 100000000;
  329. unsigned int rand_delay;
  330. struct timespec delay;
  331. gettime_wrapper(&delay);
  332. delay.tv_sec -= ses.authstate.auth_starttime.tv_sec;
  333. delay.tv_nsec -= ses.authstate.auth_starttime.tv_nsec;
  334. /* carry */
  335. if (delay.tv_nsec < 0) {
  336. delay.tv_nsec += 1000000000;
  337. delay.tv_sec -= 1;
  338. }
  339. genrandom((unsigned char*)&rand_delay, sizeof(rand_delay));
  340. rand_delay = mindelay + (rand_delay % vardelay);
  341. if (delay.tv_sec == 0 && delay.tv_nsec <= mindelay) {
  342. /* Compensate for elapsed time */
  343. delay.tv_nsec = rand_delay - delay.tv_nsec;
  344. } else {
  345. /* No time left or time went backwards, just delay anyway */
  346. delay.tv_sec = 0;
  347. delay.tv_nsec = rand_delay;
  348. }
  349. #if DROPBEAR_FUZZ
  350. if (!fuzz.fuzzing)
  351. #endif
  352. {
  353. while (nanosleep(&delay, &delay) == -1 && errno == EINTR) { /* Go back to sleep */ }
  354. }
  355. ses.authstate.failcount++;
  356. }
  357. if (ses.authstate.failcount >= svr_opts.maxauthtries) {
  358. char * userstr;
  359. /* XXX - send disconnect ? */
  360. TRACE(("Max auth tries reached, exiting"))
  361. if (ses.authstate.pw_name == NULL) {
  362. userstr = "is invalid";
  363. } else {
  364. userstr = ses.authstate.pw_name;
  365. }
  366. dropbear_exit("Max auth tries reached - user '%s'",
  367. userstr);
  368. }
  369. TRACE(("leave send_msg_userauth_failure"))
  370. }
  371. /* Send a success message to the user, and set the "authdone" flag */
  372. void send_msg_userauth_success() {
  373. TRACE(("enter send_msg_userauth_success"))
  374. CHECKCLEARTOWRITE();
  375. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_SUCCESS);
  376. encrypt_packet();
  377. /* authdone must be set after encrypt_packet() for
  378. * delayed-zlib mode */
  379. ses.authstate.authdone = 1;
  380. ses.connect_time = 0;
  381. if (ses.authstate.pw_uid == 0) {
  382. ses.allowprivport = 1;
  383. }
  384. /* Remove from the list of pre-auth sockets. Should be m_close(), since if
  385. * we fail, we might end up leaking connection slots, and disallow new
  386. * logins - a nasty situation. */
  387. m_close(svr_ses.childpipe);
  388. TRACE(("leave send_msg_userauth_success"))
  389. }