PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/utils_password.c

https://gitlab.com/nmccallu/cryptsetup
C | 294 lines | 215 code | 47 blank | 32 comment | 46 complexity | 3975cef3b5d8b9c6ba45da3be0949bbe MD5 | raw file
  1. /*
  2. * Password quality check wrapper
  3. *
  4. * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2012-2014, Milan Broz
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "cryptsetup.h"
  22. #include <termios.h>
  23. int opt_force_password = 0;
  24. #if defined ENABLE_PWQUALITY
  25. #include <pwquality.h>
  26. static int tools_check_pwquality(const char *password)
  27. {
  28. int r;
  29. void *auxerror;
  30. pwquality_settings_t *pwq;
  31. log_dbg("Checking new password using default pwquality settings.");
  32. pwq = pwquality_default_settings();
  33. if (!pwq)
  34. return -EINVAL;
  35. r = pwquality_read_config(pwq, NULL, &auxerror);
  36. if (r) {
  37. log_err(_("Cannot check password quality: %s\n"),
  38. pwquality_strerror(NULL, 0, r, auxerror));
  39. pwquality_free_settings(pwq);
  40. return -EINVAL;
  41. }
  42. r = pwquality_check(pwq, password, NULL, NULL, &auxerror);
  43. if (r < 0) {
  44. log_err(_("Password quality check failed:\n %s\n"),
  45. pwquality_strerror(NULL, 0, r, auxerror));
  46. r = -EPERM;
  47. } else {
  48. log_dbg("New password libpwquality score is %d.", r);
  49. r = 0;
  50. }
  51. pwquality_free_settings(pwq);
  52. return r;
  53. }
  54. #elif defined ENABLE_PASSWDQC
  55. #include <passwdqc.h>
  56. static int tools_check_pwquality(const char *password)
  57. {
  58. passwdqc_params_t params;
  59. char *parse_reason;
  60. const char *check_reason;
  61. const char *config = PASSWDQC_CONFIG_FILE;
  62. passwdqc_params_reset(&params);
  63. if (*config && passwdqc_params_load(&params, &parse_reason, config)) {
  64. log_err(_("Cannot check password quality: %s\n"),
  65. (parse_reason ? parse_reason : "Out of memory"));
  66. free(parse_reason);
  67. return -EINVAL;
  68. }
  69. check_reason = passwdqc_check(&params.qc, password, NULL, NULL);
  70. if (check_reason) {
  71. log_err(_("Password quality check failed: Bad passphrase (%s)\n"),
  72. check_reason);
  73. return -EPERM;
  74. }
  75. return 0;
  76. }
  77. #else /* !(ENABLE_PWQUALITY || ENABLE_PASSWDQC) */
  78. static int tools_check_pwquality(const char *password)
  79. {
  80. return 0;
  81. }
  82. #endif /* ENABLE_PWQUALITY || ENABLE_PASSWDQC */
  83. int tools_is_cipher_null(const char *cipher)
  84. {
  85. if (!cipher)
  86. return 0;
  87. return !strcmp(cipher, "cipher_null") ? 1 : 0;
  88. }
  89. /*
  90. * Keyfile - is standard input treated as a binary file (no EOL handling).
  91. */
  92. int tools_is_stdin(const char *key_file)
  93. {
  94. if (!key_file)
  95. return 1;
  96. return strcmp(key_file, "-") ? 0 : 1;
  97. }
  98. /* Password reading helpers */
  99. static int untimed_read(int fd, char *pass, size_t maxlen)
  100. {
  101. ssize_t i;
  102. i = read(fd, pass, maxlen);
  103. if (i > 0) {
  104. pass[i-1] = '\0';
  105. i = 0;
  106. } else if (i == 0) { /* EOF */
  107. *pass = 0;
  108. i = -1;
  109. }
  110. return i;
  111. }
  112. static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
  113. {
  114. struct timeval t;
  115. fd_set fds = {}; /* Just to avoid scan-build false report for FD_SET */
  116. int failed = -1;
  117. FD_ZERO(&fds);
  118. FD_SET(fd, &fds);
  119. t.tv_sec = timeout;
  120. t.tv_usec = 0;
  121. if (select(fd+1, &fds, NULL, NULL, &t) > 0)
  122. failed = untimed_read(fd, pass, maxlen);
  123. return failed;
  124. }
  125. static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
  126. long timeout)
  127. {
  128. struct termios orig, tmp;
  129. int failed = -1;
  130. int infd, outfd;
  131. if (maxlen < 1)
  132. return failed;
  133. /* Read and write to /dev/tty if available */
  134. infd = open("/dev/tty", O_RDWR);
  135. if (infd == -1) {
  136. infd = STDIN_FILENO;
  137. outfd = STDERR_FILENO;
  138. } else
  139. outfd = infd;
  140. if (tcgetattr(infd, &orig))
  141. goto out_err;
  142. memcpy(&tmp, &orig, sizeof(tmp));
  143. tmp.c_lflag &= ~ECHO;
  144. if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
  145. goto out_err;
  146. tcsetattr(infd, TCSAFLUSH, &tmp);
  147. if (timeout)
  148. failed = timed_read(infd, pass, maxlen, timeout);
  149. else
  150. failed = untimed_read(infd, pass, maxlen);
  151. tcsetattr(infd, TCSAFLUSH, &orig);
  152. out_err:
  153. if (!failed && write(outfd, "\n", 1)) {};
  154. if (infd != STDIN_FILENO)
  155. close(infd);
  156. return failed;
  157. }
  158. static int crypt_get_key_tty(const char *prompt,
  159. char **key, size_t *key_size,
  160. int timeout, int verify,
  161. struct crypt_device *cd)
  162. {
  163. int key_size_max = DEFAULT_PASSPHRASE_SIZE_MAX;
  164. int r = -EINVAL;
  165. char *pass = NULL, *pass_verify = NULL;
  166. *key = NULL;
  167. *key_size = 0;
  168. log_dbg("Interactive passphrase entry requested.");
  169. pass = crypt_safe_alloc(key_size_max + 1);
  170. if (!pass) {
  171. log_err( _("Out of memory while reading passphrase.\n"));
  172. return -ENOMEM;
  173. }
  174. if (interactive_pass(prompt, pass, key_size_max, timeout)) {
  175. log_err(_("Error reading passphrase from terminal.\n"));
  176. goto out_err;
  177. }
  178. pass[key_size_max] = '\0';
  179. if (verify) {
  180. pass_verify = crypt_safe_alloc(key_size_max);
  181. if (!pass_verify) {
  182. log_err(_("Out of memory while reading passphrase.\n"));
  183. r = -ENOMEM;
  184. goto out_err;
  185. }
  186. if (interactive_pass(_("Verify passphrase: "),
  187. pass_verify, key_size_max, timeout)) {
  188. log_err(_("Error reading passphrase from terminal.\n"));
  189. goto out_err;
  190. }
  191. if (strncmp(pass, pass_verify, key_size_max)) {
  192. log_err(_("Passphrases do not match.\n"));
  193. r = -EPERM;
  194. goto out_err;
  195. }
  196. }
  197. *key = pass;
  198. *key_size = strlen(pass);
  199. r = 0;
  200. out_err:
  201. crypt_safe_free(pass_verify);
  202. if (r)
  203. crypt_safe_free(pass);
  204. return r;
  205. }
  206. /*
  207. * Note: --key-file=- is interpreted as a read from a binary file (stdin)
  208. * key_size_max == 0 means detect maximum according to input type (tty/file)
  209. */
  210. int tools_get_key(const char *prompt,
  211. char **key, size_t *key_size,
  212. size_t keyfile_offset, size_t keyfile_size_max,
  213. const char *key_file,
  214. int timeout, int verify, int pwquality,
  215. struct crypt_device *cd)
  216. {
  217. int r = -EINVAL, block;
  218. block = tools_signals_blocked();
  219. if (block)
  220. set_int_block(0);
  221. if (tools_is_stdin(key_file)) {
  222. if (isatty(STDIN_FILENO)) {
  223. if (keyfile_offset) {
  224. log_err(_("Cannot use offset with terminal input.\n"));
  225. } else {
  226. //FIXME:if (!prompt) "Enter passphrase for %s: "
  227. if (!prompt)
  228. prompt = "Enter passphrase:";
  229. r = crypt_get_key_tty(prompt, key, key_size, timeout, verify, cd);
  230. }
  231. } else {
  232. log_dbg("STDIN descriptor passphrase entry requested.");
  233. /* No keyfile means STDIN with EOL handling (\n will end input)). */
  234. r = crypt_keyfile_read(cd, NULL, key, key_size, keyfile_offset, keyfile_size_max,
  235. key_file ? 0 : CRYPT_KEYFILE_STOP_EOL);
  236. }
  237. } else {
  238. log_dbg("File descriptor passphrase entry requested.");
  239. r = crypt_keyfile_read(cd, key_file, key, key_size, keyfile_offset, keyfile_size_max, 0);
  240. }
  241. if (block && !quit)
  242. set_int_block(1);
  243. /* Check pwquality for password (not keyfile) */
  244. if (pwquality && !opt_force_password && !key_file && !r)
  245. r = tools_check_pwquality(*key);
  246. return r;
  247. }