Add basic support for system TCRYPT device.
[platform/upstream/cryptsetup.git] / src / utils_password.c
1 /*
2  * Password quality check wrapper
3  *
4  * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2012, 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  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "cryptsetup.h"
22
23 int opt_force_password = 0;
24
25 #if ENABLE_PWQUALITY
26 #include <pwquality.h>
27
28 static int tools_check_pwquality(const char *password)
29 {
30         int r;
31         void *auxerror;
32         pwquality_settings_t *pwq; 
33
34         log_dbg("Checking new password using default pwquality settings.");
35         pwq = pwquality_default_settings();
36         if (!pwq)
37                 return -EINVAL;
38
39         r = pwquality_read_config(pwq, NULL, &auxerror);
40         if (r) {
41                 log_err(_("Cannot check passsword quality: %s\n"),
42                         pwquality_strerror(NULL, 0, r, auxerror));
43                 pwquality_free_settings(pwq);
44                 return -EINVAL;
45         }
46
47         r = pwquality_check(pwq, password, NULL, NULL, &auxerror);
48         if (r < 0) {
49                 log_err(_("Password quality check failed:\n %s\n"),
50                         pwquality_strerror(NULL, 0, r, auxerror));
51                 r = -EPERM;
52         } else {
53                 log_dbg("New password libpwquality score is %d.", r);
54                 r = 0;
55         }
56
57         pwquality_free_settings(pwq);
58         return r;
59 }
60 #else /* ENABLE_PWQUALITY */
61 static int tools_check_pwquality(const char *password)
62 {
63         return 0;
64 }
65 #endif /* ENABLE_PWQUALITY */
66
67 int tools_get_key(const char *prompt,
68                   char **key, size_t *key_size,
69                   size_t keyfile_offset, size_t keyfile_size_max,
70                   const char *key_file,
71                   int timeout, int verify, int pwquality,
72                   struct crypt_device *cd)
73 {
74         int r, block;
75
76         block = tools_signals_blocked();
77         if (block)
78                 set_int_block(0);
79
80         r = crypt_get_key(prompt, key, key_size, keyfile_offset,
81                           keyfile_size_max, key_file, timeout, verify, cd);
82         if (block && !quit)
83                 set_int_block(1);
84
85         /* Check pwquality for password (not keyfile) */
86         if (pwquality && !opt_force_password && !key_file && !r)
87                 r = tools_check_pwquality(*key);
88
89         return r;
90 }