summaryrefslogtreecommitdiff
path: root/examples/auth/crackcheck/crackcheck.c
blob: 0636114a17cfb3700f5ebe3f30618c6da51ca2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <crack.h>

void usage(char *command) {
	char *c, *comm;

	comm = command;
	while ((c = strrchr(comm, '/')) != NULL) {
		comm = c + 1;
	}

	fprintf(stderr, "Usage: %s -d dictionary\n\n", comm);
	fprintf(stderr, "     -d dictionary file for cracklib\n\n");
	fprintf(stderr, "	The password is expected to be given via stdin.\n\n");
	exit(-1);
}

int complexity(char* passwd)
{
	/* TG 26.10.2005
	 * check password for complexity like MS Windows NT 
	 */

	int c_upper = 0;
	int c_lower = 0;
	int c_digit = 0;
	int c_punct = 0;
	int c_tot = 0;
	int i, len;

	if (!passwd) goto fail;
	len = strlen(passwd);

	for (i = 0; i < len; i++) {

		if (c_tot >= 3) break;

		if (isupper(passwd[i])) {
			if (!c_upper) {
				c_upper = 1;
				c_tot += 1;
			}
			continue;
		}
		if (islower(passwd[i])) {
			if (!c_lower) {
				c_lower = 1;
				c_tot += 1;
			}
			continue;
		}
		if (isdigit(passwd[i])) {
			if (!c_digit) {
				c_digit = 1;
				c_tot += 1;
			}
			continue;
		}
		if (ispunct(passwd[i])) {
			if (!c_punct) {
				c_punct = 1;
				c_tot += 1;
			}
			continue;
		}
	}

	if ((c_tot) < 3) goto fail;
	return 0;

fail:
	fprintf(stderr, "ERR Complexity check failed\n\n");
	return -4;
}

int main(int argc, char **argv) {
	extern char *optarg;
	int c, ret, complex_check = 0;

	char f[256];
	char *dictionary = NULL;
	char *password;
	char *reply;

	while ( (c = getopt(argc, argv, "d:c")) != EOF){
		switch(c) {
		case 'd':
			dictionary = strdup(optarg);
			break;
		case 'c':
			complex_check = 1;
			break;
		default:
			usage(argv[0]);
		}
	}

	if (dictionary == NULL) {
		fprintf(stderr, "ERR - Wrong Command Line\n\n");
		usage(argv[0]);
	} 

	fflush(stdin);
	password = fgets(f, sizeof(f), stdin);

	if (password == NULL) {
		fprintf(stderr, "ERR - Failed to read password\n\n");
		exit(-2);
	}

	if (complex_check) {
		ret = complexity(password);
		if (ret) {
			exit(ret);
		}
	}

	reply = FascistCheck(password, dictionary);
	if (reply != NULL) {
		fprintf(stderr, "ERR - %s\n\n", reply);
		exit(-3);
	}

	exit(0);

}