Makefile: Add security compiling option (RELRO, SC, and FORTIFY)
[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-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
22 #include "cryptsetup.h"
23
24 int opt_force_password = 0;
25
26 #if ENABLE_PWQUALITY
27 #include <pwquality.h>
28
29 static int tools_check_pwquality(const char *password)
30 {
31         int r;
32         void *auxerror;
33         pwquality_settings_t *pwq; 
34
35         log_dbg("Checking new password using default pwquality settings.");
36         pwq = pwquality_default_settings();
37         if (!pwq)
38                 return -EINVAL;
39
40         r = pwquality_read_config(pwq, NULL, &auxerror);
41         if (r) {
42                 log_err(_("Cannot check password quality: %s\n"),
43                         pwquality_strerror(NULL, 0, r, auxerror));
44                 pwquality_free_settings(pwq);
45                 return -EINVAL;
46         }
47
48         r = pwquality_check(pwq, password, NULL, NULL, &auxerror);
49         if (r < 0) {
50                 log_err(_("Password quality check failed:\n %s\n"),
51                         pwquality_strerror(NULL, 0, r, auxerror));
52                 r = -EPERM;
53         } else {
54                 log_dbg("New password libpwquality score is %d.", r);
55                 r = 0;
56         }
57
58         pwquality_free_settings(pwq);
59         return r;
60 }
61 #else /* ENABLE_PWQUALITY */
62 static int tools_check_pwquality(const char *password)
63 {
64         return 0;
65 }
66 #endif /* ENABLE_PWQUALITY */
67
68 int tools_get_key(const char *prompt,
69                   char **key, size_t *key_size,
70                   size_t keyfile_offset, size_t keyfile_size_max,
71                   const char *key_file,
72                   int timeout, int verify, int pwquality,
73                   struct crypt_device *cd)
74 {
75         int r, block;
76
77         block = tools_signals_blocked();
78         if (block)
79                 set_int_block(0);
80
81         r = crypt_get_key(prompt, key, key_size, keyfile_offset,
82                           keyfile_size_max, key_file, timeout, verify, cd);
83         if (block && !quit)
84                 set_int_block(1);
85
86         /* Check pwquality for password (not keyfile) */
87         if (pwquality && !opt_force_password && !key_file && !r)
88                 r = tools_check_pwquality(*key);
89
90         return r;
91 }