100-pubkey_path.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --- a/src/svr-authpubkey.c
  2. +++ b/src/svr-authpubkey.c
  3. @@ -78,6 +78,13 @@ static void send_msg_userauth_pk_ok(cons
  4. const unsigned char* keyblob, unsigned int keybloblen);
  5. static int checkfileperm(char * filename);
  6. +static const char * const global_authkeys_dir = "/etc/dropbear";
  7. +static const int n_global_authkeys_dir = 14; /* + 1 extra byte */
  8. +static const char * const user_authkeys_dir = ".ssh";
  9. +static const int n_user_authkeys_dir = 5; /* + 1 extra byte */
  10. +static const char * const authkeys_file = "authorized_keys";
  11. +static const int n_authkeys_file = 16; /* + 1 extra byte */
  12. +
  13. /* process a pubkey auth request, sending success or failure message as
  14. * appropriate */
  15. void svr_auth_pubkey(int valid_user) {
  16. @@ -462,14 +469,21 @@ static int checkpubkey(const char* keyal
  17. if (checkpubkeyperms() == DROPBEAR_FAILURE) {
  18. TRACE(("bad authorized_keys permissions, or file doesn't exist"))
  19. } else {
  20. - /* we don't need to check pw and pw_dir for validity, since
  21. - * its been done in checkpubkeyperms. */
  22. - len = strlen(ses.authstate.pw_dir);
  23. - /* allocate max required pathname storage,
  24. - * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  25. - filename = m_malloc(len + 22);
  26. - snprintf(filename, len + 22, "%s/.ssh/authorized_keys",
  27. - ses.authstate.pw_dir);
  28. + if (ses.authstate.pw_uid == 0) {
  29. + len = n_global_authkeys_dir + n_authkeys_file;
  30. + filename = m_malloc(len);
  31. + snprintf(filename, len, "%s/%s", global_authkeys_dir, authkeys_file);
  32. + } else {
  33. + /* we don't need to check pw and pw_dir for validity, since
  34. + * its been done in checkpubkeyperms. */
  35. + len = strlen(ses.authstate.pw_dir);
  36. + /* allocate max required pathname storage,
  37. + * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  38. + len += n_user_authkeys_dir + n_authkeys_file + 1;
  39. + filename = m_malloc(len);
  40. + snprintf(filename, len, "%s/%s/%s", ses.authstate.pw_dir,
  41. + user_authkeys_dir, authkeys_file);
  42. + }
  43. authfile = fopen(filename, "r");
  44. if (!authfile) {
  45. @@ -543,27 +557,41 @@ static int checkpubkeyperms() {
  46. goto out;
  47. }
  48. - /* allocate max required pathname storage,
  49. - * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  50. - len += 22;
  51. - filename = m_malloc(len);
  52. - strlcpy(filename, ses.authstate.pw_dir, len);
  53. + if (ses.authstate.pw_uid == 0) {
  54. + if (checkfileperm(global_authkeys_dir) != DROPBEAR_SUCCESS) {
  55. + goto out;
  56. + }
  57. - /* check ~ */
  58. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  59. - goto out;
  60. - }
  61. + len = n_global_authkeys_dir + n_authkeys_file;
  62. + filename = m_malloc(len);
  63. - /* check ~/.ssh */
  64. - strlcat(filename, "/.ssh", len);
  65. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  66. - goto out;
  67. - }
  68. + snprintf(filename, len, "%s/%s", global_authkeys_dir, authkeys_file);
  69. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  70. + goto out;
  71. + }
  72. + } else {
  73. + /* check ~ */
  74. + if (checkfileperm(ses.authstate.pw_dir) != DROPBEAR_SUCCESS) {
  75. + goto out;
  76. + }
  77. - /* now check ~/.ssh/authorized_keys */
  78. - strlcat(filename, "/authorized_keys", len);
  79. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  80. - goto out;
  81. + /* allocate max required pathname storage,
  82. + * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  83. + len += n_user_authkeys_dir + n_authkeys_file + 1;
  84. + filename = m_malloc(len);
  85. +
  86. + /* check ~/.ssh */
  87. + snprintf(filename, len, "%s/%s", ses.authstate.pw_dir, user_authkeys_dir);
  88. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  89. + goto out;
  90. + }
  91. +
  92. + /* now check ~/.ssh/authorized_keys */
  93. + snprintf(filename, len, "%s/%s/%s", ses.authstate.pw_dir,
  94. + user_authkeys_dir, authkeys_file);
  95. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  96. + goto out;
  97. + }
  98. }
  99. /* file looks ok, return success */