Debian Codegraph @

src/pwmake.c

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