Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/obscure.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/libbb/obscure.c

    r1765 r2725  
    55 * Copyright (C) 2006 Tito Ragusa <farmatito@tiscali.it>
    66 *
    7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    88 */
    99
     
    4646static int string_checker_helper(const char *p1, const char *p2)
    4747{
     48    /* as sub-string */
     49    if (strcasestr(p2, p1) != NULL
     50    /* invert in case haystack is shorter than needle */
     51     || strcasestr(p1, p2) != NULL
    4852    /* as-is or capitalized */
    49     if (strcasecmp(p1, p2) == 0
    50     /* as sub-string */
    51     || strcasestr(p2, p1) != NULL
    52     /* invert in case haystack is shorter than needle */
    53     || strcasestr(p1, p2) != NULL)
     53    /* || strcasecmp(p1, p2) == 0 - 1st strcasestr should catch this too */
     54    ) {
    5455        return 1;
     56    }
    5557    return 0;
    5658}
     
    5860static int string_checker(const char *p1, const char *p2)
    5961{
    60     int size;
     62    int size, i;
    6163    /* check string */
    6264    int ret = string_checker_helper(p1, p2);
    63     /* Make our own copy */
     65    /* make our own copy */
    6466    char *p = xstrdup(p1);
     67
    6568    /* reverse string */
    66     size = strlen(p);
     69    i = size = strlen(p1);
     70    while (--i >= 0) {
     71        *p++ = p1[i];
     72    }
     73    p -= size; /* restore pointer */
    6774
    68     while (size--) {
    69         *p = p1[size];
    70         p++;
    71     }
    72     /* restore pointer */
    73     p -= strlen(p1);
    7475    /* check reversed string */
    7576    ret |= string_checker_helper(p, p2);
     77
    7678    /* clean up */
    77     memset(p, 0, strlen(p1));
     79    memset(p, 0, size);
    7880    free(p);
     81
    7982    return ret;
    8083}
    8184
    82 #define LOWERCASE          1
    83 #define UPPERCASE          2
    84 #define NUMBERS            4
    85 #define SPECIAL            8
     85#define CATEGORIES  4
     86
     87#define LOWERCASE   1
     88#define UPPERCASE   2
     89#define NUMBERS     4
     90#define SPECIAL     8
     91
     92#define LAST_CAT    8
    8693
    8794static const char *obscure_msg(const char *old_p, const char *new_p, const struct passwd *pw)
    8895{
    89     int i;
    90     int c;
    91     int length;
    92     int mixed = 0;
    93     /* Add 2 for each type of characters to the minlen of password */
    94     int size = CONFIG_PASSWORD_MINLEN + 8;
     96    unsigned length;
     97    unsigned size;
     98    unsigned mixed;
     99    unsigned c;
     100    unsigned i;
    95101    const char *p;
    96     char hostname[255];
     102    char *hostname;
    97103
    98104    /* size */
     
    105111    }
    106112    /* 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)) {
    108114        return "similar to gecos";
    109115    }
    110116    /* hostname as-is, as sub-string, reversed, capitalized, doubled */
    111     if (gethostname(hostname, 255) == 0) {
    112         hostname[254] = '\0';
    113         if (string_checker(new_p, hostname)) {
    114             return "similar to hostname";
    115         }
    116     }
     117    hostname = safe_gethostname();
     118    i = string_checker(new_p, hostname);
     119    free(hostname);
     120    if (i)
     121        return "similar to hostname";
    117122
    118123    /* Should / Must contain a mix of: */
     124    mixed = 0;
    119125    for (i = 0; i < length; i++) {
    120126        if (islower(new_p[i])) {        /* a-z */
     
    127133            mixed |= SPECIAL;
    128134        }
    129         /* More than 50% similar characters ? */
     135        /* Count i'th char */
    130136        c = 0;
    131137        p = new_p;
    132138        while (1) {
    133             if ((p = strchr(p, new_p[i])) == NULL) {
     139            p = strchr(p, new_p[i]);
     140            if (p == NULL) {
    134141                break;
    135142            }
    136143            c++;
    137             if (!++p) {
    138                 break; /* move past the matched char if possible */
     144            p++;
     145            if (!*p) {
     146                break;
    139147            }
    140148        }
    141 
    142         if (c >= (length / 2)) {
     149        /* More than 50% similar characters ? */
     150        if (c*2 >= length) {
    143151            return "too many similar characters";
    144152        }
    145153    }
    146     for (i=0; i<4; i++)
    147         if (mixed & (1<<i)) size -= 2;
     154
     155    size = CONFIG_PASSWORD_MINLEN + 2*CATEGORIES;
     156    for (i = 1; i <= LAST_CAT; i <<= 1)
     157        if (mixed & i)
     158            size -= 2;
    148159    if (length < size)
    149160        return "too weak";
    150161
    151     if (old_p && old_p[0] != '\0') {
     162    if (old_p && old_p[0]) {
    152163        /* check vs. old password */
    153164        if (string_checker(new_p, old_p)) {
     
    155166        }
    156167    }
     168
    157169    return NULL;
    158170}
    159171
    160 int obscure(const char *old, const char *newval, const struct passwd *pw)
     172int FAST_FUNC obscure(const char *old, const char *newval, const struct passwd *pw)
    161173{
    162174    const char *msg;
Note: See TracChangeset for help on using the changeset viewer.