svr-authpubkey.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. /*
  25. * This file incorporates work covered by the following copyright and
  26. * permission notice:
  27. *
  28. * Copyright (c) 2000 Markus Friedl. All rights reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions
  32. * are met:
  33. * 1. Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * 2. Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in the
  37. * documentation and/or other materials provided with the distribution.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  40. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  41. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  42. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  43. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  48. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. * This copyright and permission notice applies to the code parsing public keys
  51. * options string which can also be found in OpenSSH auth2-pubkey.c file
  52. * (user_key_allowed2). It has been adapted to work with buffers.
  53. *
  54. */
  55. /* Process a pubkey auth request */
  56. #include "includes.h"
  57. #include "session.h"
  58. #include "dbutil.h"
  59. #include "buffer.h"
  60. #include "signkey.h"
  61. #include "auth.h"
  62. #include "ssh.h"
  63. #include "packet.h"
  64. #include "algo.h"
  65. #ifdef ENABLE_SVR_PUBKEY_AUTH
  66. #define MIN_AUTHKEYS_LINE 10 /* "ssh-rsa AB" - short but doesn't matter */
  67. #define MAX_AUTHKEYS_LINE 4200 /* max length of a line in authkeys */
  68. static int checkpubkey(char* algo, unsigned int algolen,
  69. unsigned char* keyblob, unsigned int keybloblen);
  70. static int checkpubkeyperms(void);
  71. static void send_msg_userauth_pk_ok(char* algo, unsigned int algolen,
  72. unsigned char* keyblob, unsigned int keybloblen);
  73. static int checkfileperm(char * filename);
  74. /* process a pubkey auth request, sending success or failure message as
  75. * appropriate */
  76. void svr_auth_pubkey() {
  77. unsigned char testkey; /* whether we're just checking if a key is usable */
  78. char* algo = NULL; /* pubkey algo */
  79. unsigned int algolen;
  80. unsigned char* keyblob = NULL;
  81. unsigned int keybloblen;
  82. unsigned int sign_payload_length;
  83. buffer * signbuf = NULL;
  84. sign_key * key = NULL;
  85. char* fp = NULL;
  86. enum signkey_type type = -1;
  87. TRACE(("enter pubkeyauth"))
  88. /* 0 indicates user just wants to check if key can be used, 1 is an
  89. * actual attempt*/
  90. testkey = (buf_getbool(ses.payload) == 0);
  91. algo = buf_getstring(ses.payload, &algolen);
  92. keybloblen = buf_getint(ses.payload);
  93. keyblob = buf_getptr(ses.payload, keybloblen);
  94. /* check if the key is valid */
  95. if (checkpubkey(algo, algolen, keyblob, keybloblen) == DROPBEAR_FAILURE) {
  96. send_msg_userauth_failure(0, 0);
  97. goto out;
  98. }
  99. /* let them know that the key is ok to use */
  100. if (testkey) {
  101. send_msg_userauth_pk_ok(algo, algolen, keyblob, keybloblen);
  102. goto out;
  103. }
  104. /* now we can actually verify the signature */
  105. /* get the key */
  106. key = new_sign_key();
  107. type = DROPBEAR_SIGNKEY_ANY;
  108. if (buf_get_pub_key(ses.payload, key, &type) == DROPBEAR_FAILURE) {
  109. send_msg_userauth_failure(0, 1);
  110. goto out;
  111. }
  112. /* create the data which has been signed - this a string containing
  113. * session_id, concatenated with the payload packet up to the signature */
  114. assert(ses.payload_beginning <= ses.payload->pos);
  115. sign_payload_length = ses.payload->pos - ses.payload_beginning;
  116. signbuf = buf_new(ses.payload->pos + 4 + ses.session_id->len);
  117. buf_putbufstring(signbuf, ses.session_id);
  118. /* The entire contents of the payload prior. */
  119. buf_setpos(ses.payload, ses.payload_beginning);
  120. buf_putbytes(signbuf,
  121. buf_getptr(ses.payload, sign_payload_length),
  122. sign_payload_length);
  123. buf_incrpos(ses.payload, sign_payload_length);
  124. buf_setpos(signbuf, 0);
  125. /* ... and finally verify the signature */
  126. fp = sign_key_fingerprint(keyblob, keybloblen);
  127. if (buf_verify(ses.payload, key, signbuf) == DROPBEAR_SUCCESS) {
  128. dropbear_log(LOG_NOTICE,
  129. "Pubkey auth succeeded for '%s' with key %s from %s",
  130. ses.authstate.pw_name, fp, svr_ses.addrstring);
  131. send_msg_userauth_success();
  132. } else {
  133. dropbear_log(LOG_WARNING,
  134. "Pubkey auth bad signature for '%s' with key %s from %s",
  135. ses.authstate.pw_name, fp, svr_ses.addrstring);
  136. send_msg_userauth_failure(0, 1);
  137. }
  138. m_free(fp);
  139. out:
  140. /* cleanup stuff */
  141. if (signbuf) {
  142. buf_free(signbuf);
  143. }
  144. if (algo) {
  145. m_free(algo);
  146. }
  147. if (key) {
  148. sign_key_free(key);
  149. key = NULL;
  150. }
  151. TRACE(("leave pubkeyauth"))
  152. }
  153. /* Reply that the key is valid for auth, this is sent when the user sends
  154. * a straight copy of their pubkey to test, to avoid having to perform
  155. * expensive signing operations with a worthless key */
  156. static void send_msg_userauth_pk_ok(char* algo, unsigned int algolen,
  157. unsigned char* keyblob, unsigned int keybloblen) {
  158. TRACE(("enter send_msg_userauth_pk_ok"))
  159. CHECKCLEARTOWRITE();
  160. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_PK_OK);
  161. buf_putstring(ses.writepayload, algo, algolen);
  162. buf_putstring(ses.writepayload, (const char*)keyblob, keybloblen);
  163. encrypt_packet();
  164. TRACE(("leave send_msg_userauth_pk_ok"))
  165. }
  166. /* Checks whether a specified publickey (and associated algorithm) is an
  167. * acceptable key for authentication */
  168. /* Returns DROPBEAR_SUCCESS if key is ok for auth, DROPBEAR_FAILURE otherwise */
  169. static int checkpubkey(char* algo, unsigned int algolen,
  170. unsigned char* keyblob, unsigned int keybloblen) {
  171. FILE * authfile = NULL;
  172. char * filename = NULL;
  173. int ret = DROPBEAR_FAILURE;
  174. buffer * line = NULL;
  175. unsigned int len, pos;
  176. buffer * options_buf = NULL;
  177. int line_num;
  178. uid_t origuid;
  179. gid_t origgid;
  180. TRACE(("enter checkpubkey"))
  181. /* check that we can use the algo */
  182. if (have_algo(algo, algolen, sshhostkey) == DROPBEAR_FAILURE) {
  183. dropbear_log(LOG_WARNING,
  184. "Pubkey auth attempt with unknown algo for '%s' from %s",
  185. ses.authstate.pw_name, svr_ses.addrstring);
  186. goto out;
  187. }
  188. /* check file permissions, also whether file exists */
  189. if (checkpubkeyperms() == DROPBEAR_FAILURE) {
  190. TRACE(("bad authorized_keys permissions, or file doesn't exist"))
  191. goto out;
  192. }
  193. /* we don't need to check pw and pw_dir for validity, since
  194. * its been done in checkpubkeyperms. */
  195. len = strlen(ses.authstate.pw_dir);
  196. /* allocate max required pathname storage,
  197. * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  198. filename = m_malloc(len + 22);
  199. snprintf(filename, len + 22, "%s/.ssh/authorized_keys",
  200. ses.authstate.pw_dir);
  201. /* open the file as the authenticating user. */
  202. origuid = getuid();
  203. origgid = getgid();
  204. if ((setegid(ses.authstate.pw_gid)) < 0 ||
  205. (seteuid(ses.authstate.pw_uid)) < 0) {
  206. dropbear_exit("Failed to set euid");
  207. }
  208. authfile = fopen(filename, "r");
  209. if ((seteuid(origuid)) < 0 ||
  210. (setegid(origgid)) < 0) {
  211. dropbear_exit("Failed to revert euid");
  212. }
  213. if (authfile == NULL) {
  214. goto out;
  215. }
  216. TRACE(("checkpubkey: opened authorized_keys OK"))
  217. line = buf_new(MAX_AUTHKEYS_LINE);
  218. line_num = 0;
  219. /* iterate through the lines */
  220. do {
  221. /* new line : potentially new options */
  222. if (options_buf) {
  223. buf_free(options_buf);
  224. options_buf = NULL;
  225. }
  226. if (buf_getline(line, authfile) == DROPBEAR_FAILURE) {
  227. /* EOF reached */
  228. TRACE(("checkpubkey: authorized_keys EOF reached"))
  229. break;
  230. }
  231. line_num++;
  232. if (line->len < MIN_AUTHKEYS_LINE) {
  233. TRACE(("checkpubkey: line too short"))
  234. continue; /* line is too short for it to be a valid key */
  235. }
  236. /* check the key type - will fail if there are options */
  237. TRACE(("a line!"))
  238. if (strncmp((const char *) buf_getptr(line, algolen), algo, algolen) != 0) {
  239. int is_comment = 0;
  240. unsigned char *options_start = NULL;
  241. int options_len = 0;
  242. int escape, quoted;
  243. /* skip over any comments or leading whitespace */
  244. while (line->pos < line->len) {
  245. const char c = buf_getbyte(line);
  246. if (c == ' ' || c == '\t') {
  247. continue;
  248. } else if (c == '#') {
  249. is_comment = 1;
  250. break;
  251. }
  252. buf_incrpos(line, -1);
  253. break;
  254. }
  255. if (is_comment) {
  256. /* next line */
  257. continue;
  258. }
  259. /* remember start of options */
  260. options_start = buf_getptr(line, 1);
  261. quoted = 0;
  262. escape = 0;
  263. options_len = 0;
  264. /* figure out where the options are */
  265. while (line->pos < line->len) {
  266. const char c = buf_getbyte(line);
  267. if (!quoted && (c == ' ' || c == '\t')) {
  268. break;
  269. }
  270. escape = (!escape && c == '\\');
  271. if (!escape && c == '"') {
  272. quoted = !quoted;
  273. }
  274. options_len++;
  275. }
  276. options_buf = buf_new(options_len);
  277. buf_putbytes(options_buf, options_start, options_len);
  278. /* compare the algorithm. +3 so we have enough bytes to read a space and some base64 characters too. */
  279. if (line->pos + algolen+3 > line->len) {
  280. continue;
  281. }
  282. if (strncmp((const char *) buf_getptr(line, algolen), algo, algolen) != 0) {
  283. continue;
  284. }
  285. }
  286. buf_incrpos(line, algolen);
  287. /* check for space (' ') character */
  288. if (buf_getbyte(line) != ' ') {
  289. TRACE(("checkpubkey: space character expected, isn't there"))
  290. continue;
  291. }
  292. /* truncate the line at the space after the base64 data */
  293. pos = line->pos;
  294. for (len = 0; line->pos < line->len; len++) {
  295. if (buf_getbyte(line) == ' ') break;
  296. }
  297. buf_setpos(line, pos);
  298. buf_setlen(line, line->pos + len);
  299. TRACE(("checkpubkey: line pos = %d len = %d", line->pos, line->len))
  300. ret = cmp_base64_key(keyblob, keybloblen, (const unsigned char *) algo, algolen, line, NULL);
  301. if (ret == DROPBEAR_SUCCESS && options_buf) {
  302. ret = svr_add_pubkey_options(options_buf, line_num, filename);
  303. }
  304. if (ret == DROPBEAR_SUCCESS) {
  305. break;
  306. }
  307. /* We continue to the next line otherwise */
  308. } while (1);
  309. out:
  310. if (authfile) {
  311. fclose(authfile);
  312. }
  313. if (line) {
  314. buf_free(line);
  315. }
  316. m_free(filename);
  317. if (options_buf) {
  318. buf_free(options_buf);
  319. }
  320. TRACE(("leave checkpubkey: ret=%d", ret))
  321. return ret;
  322. }
  323. /* Returns DROPBEAR_SUCCESS if file permissions for pubkeys are ok,
  324. * DROPBEAR_FAILURE otherwise.
  325. * Checks that the user's homedir, ~/.ssh, and
  326. * ~/.ssh/authorized_keys are all owned by either root or the user, and are
  327. * g-w, o-w */
  328. static int checkpubkeyperms() {
  329. char* filename = NULL;
  330. int ret = DROPBEAR_FAILURE;
  331. unsigned int len;
  332. TRACE(("enter checkpubkeyperms"))
  333. if (ses.authstate.pw_dir == NULL) {
  334. goto out;
  335. }
  336. if ((len = strlen(ses.authstate.pw_dir)) == 0) {
  337. goto out;
  338. }
  339. /* allocate max required pathname storage,
  340. * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  341. filename = m_malloc(len + 22);
  342. strncpy(filename, ses.authstate.pw_dir, len+1);
  343. /* check ~ */
  344. if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  345. goto out;
  346. }
  347. /* check ~/.ssh */
  348. strncat(filename, "/.ssh", 5); /* strlen("/.ssh") == 5 */
  349. if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  350. goto out;
  351. }
  352. /* now check ~/.ssh/authorized_keys */
  353. strncat(filename, "/authorized_keys", 16);
  354. if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  355. goto out;
  356. }
  357. /* file looks ok, return success */
  358. ret = DROPBEAR_SUCCESS;
  359. out:
  360. m_free(filename);
  361. TRACE(("leave checkpubkeyperms"))
  362. return ret;
  363. }
  364. /* Checks that a file is owned by the user or root, and isn't writable by
  365. * group or other */
  366. /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  367. static int checkfileperm(char * filename) {
  368. struct stat filestat;
  369. int badperm = 0;
  370. TRACE(("enter checkfileperm(%s)", filename))
  371. if (stat(filename, &filestat) != 0) {
  372. TRACE(("leave checkfileperm: stat() != 0"))
  373. return DROPBEAR_FAILURE;
  374. }
  375. /* check ownership - user or root only*/
  376. if (filestat.st_uid != ses.authstate.pw_uid
  377. && filestat.st_uid != 0) {
  378. badperm = 1;
  379. TRACE(("wrong ownership"))
  380. }
  381. /* check permissions - don't want group or others +w */
  382. if (filestat.st_mode & (S_IWGRP | S_IWOTH)) {
  383. badperm = 1;
  384. TRACE(("wrong perms"))
  385. }
  386. if (badperm) {
  387. if (!ses.authstate.perm_warn) {
  388. ses.authstate.perm_warn = 1;
  389. dropbear_log(LOG_INFO, "%s must be owned by user or root, and not writable by others", filename);
  390. }
  391. TRACE(("leave checkfileperm: failure perms/owner"))
  392. return DROPBEAR_FAILURE;
  393. }
  394. TRACE(("leave checkfileperm: success"))
  395. return DROPBEAR_SUCCESS;
  396. }
  397. #endif