s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / examples / auth / crackcheck / crackcheck.c
blobc3037756f428ce00f6ffab098676836bd5085314
1 #include <memory.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <crack.h>
7 #include <unistd.h>
9 void usage(char *command) {
10 char *c, *comm;
12 comm = command;
13 while ((c = strrchr(comm, '/')) != NULL) {
14 comm = c + 1;
17 fprintf(stderr, "Usage: %s [-c] [-s] [-d <dictionary>]\n\n", comm);
18 fprintf(stderr, " -c enables NT like complexity checks\n");
19 fprintf(stderr, " -d <dictionary file> for cracklib\n");
20 fprintf(stderr, " -s simple check use NT like checks ONLY\n\n");
21 fprintf(stderr, "The password is read via stdin.\n\n");
22 exit(-1);
25 int complexity(char* passwd)
27 /* TG 26.10.2005
28 * check password for complexity like MS Windows NT
31 int c_upper = 0;
32 int c_lower = 0;
33 int c_digit = 0;
34 int c_punct = 0;
35 int c_tot = 0;
36 int i, len;
38 if (!passwd) goto fail;
39 len = strlen(passwd);
41 for (i = 0; i < len; i++) {
43 if (c_tot >= 3) break;
45 if (isupper(passwd[i])) {
46 if (!c_upper) {
47 c_upper = 1;
48 c_tot += 1;
50 continue;
52 if (islower(passwd[i])) {
53 if (!c_lower) {
54 c_lower = 1;
55 c_tot += 1;
57 continue;
59 if (isdigit(passwd[i])) {
60 if (!c_digit) {
61 c_digit = 1;
62 c_tot += 1;
64 continue;
66 if (ispunct(passwd[i])) {
67 if (!c_punct) {
68 c_punct = 1;
69 c_tot += 1;
71 continue;
75 if ((c_tot) < 3) goto fail;
76 return 0;
78 fail:
79 fprintf(stderr, "ERR Complexity check failed\n\n");
80 return -4;
83 int main(int argc, char **argv) {
84 extern char *optarg;
85 int c, ret, complex_check = 0, simplex_check = 0;
87 char f[256];
88 char *dictionary = NULL;
89 char *password;
90 const char *reply;
92 while ( (c = getopt(argc, argv, "d:cs")) != EOF){
93 switch(c) {
94 case 'd':
95 dictionary = strdup(optarg);
96 break;
97 case 'c':
98 complex_check = 1;
99 break;
100 case 's':
101 complex_check = 1;
102 simplex_check = 1;
103 break;
104 default:
105 usage(argv[0]);
109 if (!simplex_check && dictionary == NULL) {
110 fprintf(stderr, "ERR - Missing cracklib dictionary\n\n");
111 usage(argv[0]);
114 fflush(stdin);
115 password = fgets(f, sizeof(f), stdin);
117 if (password == NULL) {
118 fprintf(stderr, "ERR - Failed to read password\n\n");
119 exit(-2);
122 if (complex_check) {
123 ret = complexity(password);
124 if (ret) {
125 exit(ret);
129 if (simplex_check) {
130 exit(0);
133 reply = FascistCheck(password, dictionary);
134 if (reply != NULL) {
135 fprintf(stderr, "ERR - %s\n\n", reply);
136 exit(-3);
139 exit(0);