Debian Codegraph @

src/pwscore.c

raw
1 /*
2 * pwscore - a simple tool for password scoring
3 *
4 * See the end of the file for Copyright and License Information
5 */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <libgen.h>
16 #include <locale.h>
17
18 #include "pwquality.h"
19
20 void
21 usage(const char *progname) {
22 fprintf(stderr, _("Usage: %s [user]\n"), progname);
23 fprintf(stderr, _(" The command reads the password to be scored from the standard input.\n"));
24 }
25
26 /* score a password */
27 int
28 main(int argc, char *argv[])
29 {
30 pwquality_settings_t *pwq;
31 int rv;
32 void *auxerror;
33 char buf[1024];
34 size_t len;
35 char *user = NULL;
36
37 #ifdef ENABLE_NLS
38 setlocale(LC_ALL, "");
39 bindtextdomain("libpwquality", "/usr/share/locale");
40 textdomain("libpwquality");
41 #endif
42
43 if (argc > 2) {
44 usage(basename(argv[0]));
45 exit(3);
46 }
47
48 if (argc == 2) {
49 user = argv[1];
50 }
51
52 if (fgets(buf, sizeof(buf), stdin) == NULL || (len = strlen(buf)) == 0) {
53 fprintf(stderr, _("Error: %s\n"), _("Could not obtain the password to be scored"));
54 exit(4);
55 }
56 if (buf[len - 1] == '\n')
57 buf[len - 1] = '\0';
58
59 pwq = pwquality_default_settings();
60 if (pwq == NULL) {
61 fprintf(stderr, _("Error: %s\n"), pwquality_strerror(NULL, 0, PWQ_ERROR_MEM_ALLOC, NULL));
62 exit(2);
63 }
64
65 if ((rv=pwquality_read_config(pwq, NULL, &auxerror)) != 0) {
66 pwquality_free_settings(pwq);
67 fprintf(stderr, _("Error: %s\n"), pwquality_strerror(NULL, 0, rv, auxerror));
68 exit(3);
69 }
70
71 rv = pwquality_check(pwq, buf, NULL, user, &auxerror);
72 pwquality_free_settings(pwq);
73
74 if (rv < 0) {
75 fprintf(stderr, _("Password quality check failed:\n %s\n"),
76 pwquality_strerror(NULL, 0, rv, auxerror));
77 exit(1);
78 }
79
80 printf("%d\n", rv);
81 return 0;
82 }
83
84 /*
85 * Copyright (c) Red Hat, Inc, 2011
86 * Copyright (c) Tomas Mraz <[email protected]>, 2011
87 *
88 * Redistribution and use in source and binary forms, with or without
89 * modification, are permitted provided that the following conditions
90 * are met:
91 * 1. Redistributions of source code must retain the above copyright
92 * notice, and the entire permission notice in its entirety,
93 * including the disclaimer of warranties.
94 * 2. Redistributions in binary form must reproduce the above copyright
95 * notice, this list of conditions and the following disclaimer in the
96 * documentation and/or other materials provided with the distribution.
97 * 3. The name of the author may not be used to endorse or promote
98 * products derived from this software without specific prior
99 * written permission.
100 *
101 * ALTERNATIVELY, this product may be distributed under the terms of
102 * the GNU General Public License version 2 or later, in which case the
103 * provisions of the GPL are required INSTEAD OF the above restrictions.
104 *
105 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
106 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
107 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
108 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
109 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
110 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
111 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
112 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
113 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
114 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
115 * OF THE POSSIBILITY OF SUCH DAMAGE.
116 */