3
0

obscure.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 tarball for details.
  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-is or capitalized */
  42. if (strcasecmp(p1, p2) == 0
  43. /* as sub-string */
  44. || strcasestr(p2, p1) != NULL
  45. /* invert in case haystack is shorter than needle */
  46. || strcasestr(p1, p2) != NULL)
  47. return 1;
  48. return 0;
  49. }
  50. static int string_checker(const char *p1, const char *p2)
  51. {
  52. int size;
  53. /* check string */
  54. int ret = string_checker_helper(p1, p2);
  55. /* Make our own copy */
  56. char *p = xstrdup(p1);
  57. /* reverse string */
  58. size = strlen(p);
  59. while (size--) {
  60. *p = p1[size];
  61. p++;
  62. }
  63. /* restore pointer */
  64. p -= strlen(p1);
  65. /* check reversed string */
  66. ret |= string_checker_helper(p, p2);
  67. /* clean up */
  68. memset(p, 0, strlen(p1));
  69. free(p);
  70. return ret;
  71. }
  72. #define LOWERCASE 1
  73. #define UPPERCASE 2
  74. #define NUMBERS 4
  75. #define SPECIAL 8
  76. static const char *obscure_msg(const char *old_p, const char *new_p, const struct passwd *pw)
  77. {
  78. int i;
  79. int c;
  80. int length;
  81. int mixed = 0;
  82. /* Add 2 for each type of characters to the minlen of password */
  83. int size = CONFIG_PASSWORD_MINLEN + 8;
  84. const char *p;
  85. char hostname[255];
  86. /* size */
  87. if (!new_p || (length = strlen(new_p)) < CONFIG_PASSWORD_MINLEN)
  88. return "too short";
  89. /* no username as-is, as sub-string, reversed, capitalized, doubled */
  90. if (string_checker(new_p, pw->pw_name)) {
  91. return "similar to username";
  92. }
  93. /* no gecos as-is, as sub-string, reversed, capitalized, doubled */
  94. if (*pw->pw_gecos && string_checker(new_p, pw->pw_gecos)) {
  95. return "similar to gecos";
  96. }
  97. /* hostname as-is, as sub-string, reversed, capitalized, doubled */
  98. if (gethostname(hostname, 255) == 0) {
  99. hostname[254] = '\0';
  100. if (string_checker(new_p, hostname)) {
  101. return "similar to hostname";
  102. }
  103. }
  104. /* Should / Must contain a mix of: */
  105. for (i = 0; i < length; i++) {
  106. if (islower(new_p[i])) { /* a-z */
  107. mixed |= LOWERCASE;
  108. } else if (isupper(new_p[i])) { /* A-Z */
  109. mixed |= UPPERCASE;
  110. } else if (isdigit(new_p[i])) { /* 0-9 */
  111. mixed |= NUMBERS;
  112. } else { /* special characters */
  113. mixed |= SPECIAL;
  114. }
  115. /* More than 50% similar characters ? */
  116. c = 0;
  117. p = new_p;
  118. while (1) {
  119. if ((p = strchr(p, new_p[i])) == NULL) {
  120. break;
  121. }
  122. c++;
  123. if (!++p) {
  124. break; /* move past the matched char if possible */
  125. }
  126. }
  127. if (c >= (length / 2)) {
  128. return "too many similar characters";
  129. }
  130. }
  131. for (i=0; i<4; i++)
  132. if (mixed & (1<<i)) size -= 2;
  133. if (length < size)
  134. return "too weak";
  135. if (old_p && old_p[0] != '\0') {
  136. /* check vs. old password */
  137. if (string_checker(new_p, old_p)) {
  138. return "similar to old password";
  139. }
  140. }
  141. return NULL;
  142. }
  143. int obscure(const char *old, const char *newval, const struct passwd *pw)
  144. {
  145. const char *msg;
  146. msg = obscure_msg(old, newval, pw);
  147. if (msg) {
  148. printf("Bad password: %s\n", msg);
  149. return 1;
  150. }
  151. return 0;
  152. }