3
0

obscure.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini weak password checker implementation for busybox
  4. *
  5. * Copyright (C) 2006 Tito Ragusa <farmatito@tiscali.it>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* A good password:
  10. 1) should contain at least six characters (man passwd);
  11. 2) empty passwords are not permitted;
  12. 3) should contain a mix of four different types of characters
  13. upper case letters,
  14. lower case letters,
  15. numbers,
  16. special characters such as !@#$%^&*,;".
  17. This password types should not be permitted:
  18. a) pure numbers: birthdates, social security number, license plate, phone numbers;
  19. b) words and all letters only passwords (uppercase, lowercase or mixed)
  20. as palindromes, consecutive or repetitive letters
  21. or adjacent letters on your keyboard;
  22. c) username, real name, company name or (e-mail?) address
  23. in any form (as-is, reversed, capitalized, doubled, etc.).
  24. (we can check only against username, gecos and hostname)
  25. d) common and obvious letter-number replacements
  26. (e.g. replace the letter O with number 0)
  27. such as "M1cr0$0ft" or "P@ssw0rd" (CAVEAT: we cannot check for them
  28. without the use of a dictionary).
  29. For each missing type of characters an increase of password length is
  30. requested.
  31. If user is root we warn only.
  32. CAVEAT: some older versions of crypt() truncates passwords to 8 chars,
  33. so that aaaaaaaa1Q$ is equal to aaaaaaaa making it possible to fool
  34. some of our checks. We don't test for this special case as newer versions
  35. of crypt do not truncate passwords.
  36. */
  37. #include "libbb.h"
  38. static int string_checker_helper(const char *p1, const char *p2) __attribute__ ((__pure__));
  39. static int string_checker_helper(const char *p1, const char *p2)
  40. {
  41. /* as sub-string */
  42. if (strcasestr(p2, p1) != NULL
  43. /* invert in case haystack is shorter than needle */
  44. || strcasestr(p1, p2) != NULL
  45. /* as-is or capitalized */
  46. /* || strcasecmp(p1, p2) == 0 - 1st strcasestr should catch this too */
  47. ) {
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static int string_checker(const char *p1, const char *p2)
  53. {
  54. int size, i;
  55. /* check string */
  56. int ret = string_checker_helper(p1, p2);
  57. /* make our own copy */
  58. char *p = xstrdup(p1);
  59. /* reverse string */
  60. i = size = strlen(p1);
  61. while (--i >= 0) {
  62. *p++ = p1[i];
  63. }
  64. p -= size; /* restore pointer */
  65. /* check reversed string */
  66. ret |= string_checker_helper(p, p2);
  67. /* clean up */
  68. memset(p, 0, size);
  69. free(p);
  70. return ret;
  71. }
  72. #define CATEGORIES 4
  73. #define LOWERCASE 1
  74. #define UPPERCASE 2
  75. #define NUMBERS 4
  76. #define SPECIAL 8
  77. #define LAST_CAT 8
  78. static const char *obscure_msg(const char *old_p, const char *new_p, const struct passwd *pw)
  79. {
  80. unsigned length;
  81. unsigned size;
  82. unsigned mixed;
  83. unsigned c;
  84. unsigned i;
  85. const char *p;
  86. char *hostname;
  87. /* size */
  88. if (!new_p || (length = strlen(new_p)) < CONFIG_PASSWORD_MINLEN)
  89. return "too short";
  90. /* no username as-is, as sub-string, reversed, capitalized, doubled */
  91. if (string_checker(new_p, pw->pw_name)) {
  92. return "similar to username";
  93. }
  94. #ifndef __BIONIC__
  95. /* no gecos as-is, as sub-string, reversed, capitalized, doubled */
  96. if (pw->pw_gecos[0] && string_checker(new_p, pw->pw_gecos)) {
  97. return "similar to gecos";
  98. }
  99. #endif
  100. /* hostname as-is, as sub-string, reversed, capitalized, doubled */
  101. hostname = safe_gethostname();
  102. i = string_checker(new_p, hostname);
  103. free(hostname);
  104. if (i)
  105. return "similar to hostname";
  106. /* Should / Must contain a mix of: */
  107. mixed = 0;
  108. for (i = 0; i < length; i++) {
  109. if (islower(new_p[i])) { /* a-z */
  110. mixed |= LOWERCASE;
  111. } else if (isupper(new_p[i])) { /* A-Z */
  112. mixed |= UPPERCASE;
  113. } else if (isdigit(new_p[i])) { /* 0-9 */
  114. mixed |= NUMBERS;
  115. } else { /* special characters */
  116. mixed |= SPECIAL;
  117. }
  118. /* Count i'th char */
  119. c = 0;
  120. p = new_p;
  121. while (1) {
  122. p = strchr(p, new_p[i]);
  123. if (p == NULL) {
  124. break;
  125. }
  126. c++;
  127. p++;
  128. if (!*p) {
  129. break;
  130. }
  131. }
  132. /* More than 50% similar characters ? */
  133. if (c*2 >= length) {
  134. return "too many similar characters";
  135. }
  136. }
  137. size = CONFIG_PASSWORD_MINLEN + 2*CATEGORIES;
  138. for (i = 1; i <= LAST_CAT; i <<= 1)
  139. if (mixed & i)
  140. size -= 2;
  141. if (length < size)
  142. return "too weak";
  143. if (old_p && old_p[0]) {
  144. /* check vs. old password */
  145. if (string_checker(new_p, old_p)) {
  146. return "similar to old password";
  147. }
  148. }
  149. return NULL;
  150. }
  151. int FAST_FUNC obscure(const char *old, const char *newval, const struct passwd *pw)
  152. {
  153. const char *msg;
  154. msg = obscure_msg(old, newval, pw);
  155. if (msg) {
  156. printf("Bad password: %s\n", msg);
  157. return 1;
  158. }
  159. return 0;
  160. }