aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Dunlop <bob.dunlop@xyzzy.org.uk>2010-08-17 16:01:16 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-08-17 16:01:16 +0200
commitd71c770f05079e65b2d27d28ce22b0f08e71a207 (patch)
treeb2535b234106d5b8491f7012c9999c262c92f80b
parentcda815996b92edec880943a11fe8f9e33c1ad904 (diff)
downloadbusybox-w32-d71c770f05079e65b2d27d28ce22b0f08e71a207.tar.gz
busybox-w32-d71c770f05079e65b2d27d28ce22b0f08e71a207.tar.bz2
busybox-w32-d71c770f05079e65b2d27d28ce22b0f08e71a207.zip
libbb: shrink obscure()
function old new delta string_checker_helper 59 45 -14 string_checker 116 98 -18 obscure 367 204 -163 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-195) Total: -195 bytes Signed-off-by: Bob Dunlop <bob.dunlop@xyzzy.org.uk> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/obscure.c78
1 files changed, 45 insertions, 33 deletions
diff --git a/libbb/obscure.c b/libbb/obscure.c
index 22bcb9dec..06f00281a 100644
--- a/libbb/obscure.c
+++ b/libbb/obscure.c
@@ -45,53 +45,59 @@ static int string_checker_helper(const char *p1, const char *p2) __attribute__ (
45 45
46static int string_checker_helper(const char *p1, const char *p2) 46static int string_checker_helper(const char *p1, const char *p2)
47{ 47{
48 /* as-is or capitalized */
49 if (strcasecmp(p1, p2) == 0
50 /* as sub-string */ 48 /* as sub-string */
51 || strcasestr(p2, p1) != NULL 49 if (strcasestr(p2, p1) != NULL
52 /* invert in case haystack is shorter than needle */ 50 /* invert in case haystack is shorter than needle */
53 || strcasestr(p1, p2) != NULL) 51 || strcasestr(p1, p2) != NULL
52 /* as-is or capitalized */
53 /* || strcasecmp(p1, p2) == 0 - 1st strcasestr should catch this too */
54 ) {
54 return 1; 55 return 1;
56 }
55 return 0; 57 return 0;
56} 58}
57 59
58static int string_checker(const char *p1, const char *p2) 60static int string_checker(const char *p1, const char *p2)
59{ 61{
60 int size; 62 int size, i;
61 /* check string */ 63 /* check string */
62 int ret = string_checker_helper(p1, p2); 64 int ret = string_checker_helper(p1, p2);
63 /* Make our own copy */ 65 /* make our own copy */
64 char *p = xstrdup(p1); 66 char *p = xstrdup(p1);
65 /* reverse string */
66 size = strlen(p);
67 67
68 while (size--) { 68 /* reverse string */
69 *p = p1[size]; 69 i = size = strlen(p1);
70 p++; 70 while (--i >= 0) {
71 *p++ = p1[i];
71 } 72 }
72 /* restore pointer */ 73 p -= size; /* restore pointer */
73 p -= strlen(p1); 74
74 /* check reversed string */ 75 /* check reversed string */
75 ret |= string_checker_helper(p, p2); 76 ret |= string_checker_helper(p, p2);
77
76 /* clean up */ 78 /* clean up */
77 memset(p, 0, strlen(p1)); 79 memset(p, 0, size);
78 free(p); 80 free(p);
81
79 return ret; 82 return ret;
80} 83}
81 84
82#define LOWERCASE 1 85#define CATEGORIES 4
83#define UPPERCASE 2 86
84#define NUMBERS 4 87#define LOWERCASE 1
85#define SPECIAL 8 88#define UPPERCASE 2
89#define NUMBERS 4
90#define SPECIAL 8
91
92#define LAST_CAT 8
86 93
87static const char *obscure_msg(const char *old_p, const char *new_p, const struct passwd *pw) 94static const char *obscure_msg(const char *old_p, const char *new_p, const struct passwd *pw)
88{ 95{
89 int i; 96 unsigned length;
90 int c; 97 unsigned size;
91 int length; 98 unsigned mixed;
92 int mixed = 0; 99 unsigned c;
93 /* Add 2 for each type of characters to the minlen of password */ 100 unsigned i;
94 int size = CONFIG_PASSWORD_MINLEN + 8;
95 const char *p; 101 const char *p;
96 char *hostname; 102 char *hostname;
97 103
@@ -104,7 +110,7 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
104 return "similar to username"; 110 return "similar to username";
105 } 111 }
106 /* no gecos as-is, as sub-string, reversed, capitalized, doubled */ 112 /* no gecos as-is, as sub-string, reversed, capitalized, doubled */
107 if (*pw->pw_gecos && string_checker(new_p, pw->pw_gecos)) { 113 if (pw->pw_gecos[0] && string_checker(new_p, pw->pw_gecos)) {
108 return "similar to gecos"; 114 return "similar to gecos";
109 } 115 }
110 /* hostname as-is, as sub-string, reversed, capitalized, doubled */ 116 /* hostname as-is, as sub-string, reversed, capitalized, doubled */
@@ -115,6 +121,7 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
115 return "similar to hostname"; 121 return "similar to hostname";
116 122
117 /* Should / Must contain a mix of: */ 123 /* Should / Must contain a mix of: */
124 mixed = 0;
118 for (i = 0; i < length; i++) { 125 for (i = 0; i < length; i++) {
119 if (islower(new_p[i])) { /* a-z */ 126 if (islower(new_p[i])) { /* a-z */
120 mixed |= LOWERCASE; 127 mixed |= LOWERCASE;
@@ -125,7 +132,7 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
125 } else { /* special characters */ 132 } else { /* special characters */
126 mixed |= SPECIAL; 133 mixed |= SPECIAL;
127 } 134 }
128 /* More than 50% similar characters ? */ 135 /* Count i'th char */
129 c = 0; 136 c = 0;
130 p = new_p; 137 p = new_p;
131 while (1) { 138 while (1) {
@@ -134,26 +141,31 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
134 break; 141 break;
135 } 142 }
136 c++; 143 c++;
137 if (!++p) { 144 p++;
138 break; /* move past the matched char if possible */ 145 if (!*p) {
146 break;
139 } 147 }
140 } 148 }
141 149 /* More than 50% similar characters ? */
142 if (c >= (length / 2)) { 150 if (c*2 >= length) {
143 return "too many similar characters"; 151 return "too many similar characters";
144 } 152 }
145 } 153 }
146 for (i=0; i<4; i++) 154
147 if (mixed & (1<<i)) size -= 2; 155 size = CONFIG_PASSWORD_MINLEN + 2*CATEGORIES;
156 for (i = 0; i <= LAST_CAT; i <<= 1)
157 if (mixed & i)
158 size -= 2;
148 if (length < size) 159 if (length < size)
149 return "too weak"; 160 return "too weak";
150 161
151 if (old_p && old_p[0] != '\0') { 162 if (old_p && old_p[0]) {
152 /* check vs. old password */ 163 /* check vs. old password */
153 if (string_checker(new_p, old_p)) { 164 if (string_checker(new_p, old_p)) {
154 return "similar to old password"; 165 return "similar to old password";
155 } 166 }
156 } 167 }
168
157 return NULL; 169 return NULL;
158} 170}
159 171