This file is indexed.

/usr/lib/python3/dist-packages/cracklib.py is in python3-cracklib 2.9.2-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#
# A Python binding for cracklib.
#
# Parts of this code are based on work Copyright (c) 2003 by Domenico
# Andreoli.
#
# Copyright (c) 2008, 2009, 2012 Jan Dittberner <jan@dittberner.info>
#
# This file is part of cracklib.
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"""Python extensions for the cracklib binding.
"""

import string
from _cracklib import FascistCheck

__version__ = '2.8.19'

ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"

DIFF_OK = 5
MIN_LENGTH = 9
DIG_CREDIT = 1
UP_CREDIT = 1
LOW_CREDIT = 1
OTH_CREDIT = 1


def palindrome(sample):
    """Checks whether the given string is a palindrome.
    """
    for i in range(len(sample)):
        if sample[i] != sample[-i - 1]:
            return 0
    return 1


def distdifferent(old, new, i, j):
    """Calculate how different two strings are in terms of the number
    of character removals, additions, and changes needed to go from one
    to the other."""
    if i == 0 or len(old) <= i:
        cval = 0
    else:
        cval = old[i - 1]

    if j == 0 or len(new) <= j:
        dval = 0
    else:
        dval = new[j - 1]

    return cval != dval


def distcalculate(distances, old, new, i, j):
    """Calculates the distance between two strings.
    """
    tmp = 0

    if distances[i][j] != -1:
        return distances[i][j]

    tmp =          distcalculate(distances, old, new, i - 1, j - 1)
    tmp = min(tmp, distcalculate(distances, old, new, i    , j - 1))
    tmp = min(tmp, distcalculate(distances, old, new, i - 1, j    ))
    tmp = tmp + distdifferent(old, new, i, j)

    distances[i][j] = tmp

    return tmp


def distance(old, new):
    """Gets the distance of two given strings.
    """
    oldlength = len(old)
    newlength = len(new)

    distances = [ [] for i in range(oldlength + 1) ]
    for i in range(oldlength + 1):
        distances[i] = [ -1 for j in range(newlength + 1) ]

    for i in range(oldlength + 1):
        distances[i][0] = i
    for j in range(newlength + 1):
        distances[0][j] = j
    distances[0][0] = 0

    retval = distcalculate(distances, old, new, oldlength, newlength)

    for i in range(len(distances)):
        for j in range(len(distances[i])):
            distances[i][j] = 0

    return retval


def similar(old, new):
    """Calculates whether the given strings are similar.
    """
    if distance(old, new) >= DIFF_OK:
        return 0

    if len(new) >= (len(old) * 2):
        return 0

    # passwords are too similar
    return 1


def simple(new):
    """Checks whether the given string is simple or not.
    """
    digits = 0
    uppers = 0
    lowers = 0
    others = 0

    for character in new:
        if character in string.digits:
            digits = digits + 1
        elif character in ASCII_UPPERCASE:
            uppers = uppers + 1
        elif character in ASCII_LOWERCASE:
            lowers = lowers + 1
        else:
            others = others + 1

    # The scam was this - a password of only one character type
    # must be 8 letters long.  Two types, 7, and so on.
    # This is now changed, the base size and the credits or defaults
    # see the docs on the module for info on these parameters, the
    # defaults cause the effect to be the same as before the change

    if DIG_CREDIT >= 0 and digits > DIG_CREDIT:
        digits = DIG_CREDIT

    if UP_CREDIT >= 0 and uppers > UP_CREDIT:
        uppers = UP_CREDIT

    if LOW_CREDIT >= 0 and lowers > LOW_CREDIT:
        lowers = LOW_CREDIT

    if OTH_CREDIT >= 0 and others > OTH_CREDIT:
        others = OTH_CREDIT

    size = MIN_LENGTH

    if DIG_CREDIT >= 0:
        size = size - digits
    elif digits < (DIG_CREDIT * -1):
        return 1

    if UP_CREDIT >= 0:
        size = size - uppers
    elif uppers < (UP_CREDIT * -1):
        return 1

    if LOW_CREDIT >= 0:
        size = size - lowers
    elif lowers < (LOW_CREDIT * -1):
        return 1

    if OTH_CREDIT >= 0:
        size = size - others
    elif others < (OTH_CREDIT * -1):
        return 1

    if len(new) < size:
        return 1

    return 0


def VeryFascistCheck(new, old = None, dictpath = None):
    """Extends the FascistCheck function with other checks implemented
    in this module.
    """
    if old != None:
        if new == old:
            raise ValueError("is the same as the old one")

        oldmono = old.lower()
        newmono = new.lower()
        wrapped = old + old

        if newmono == oldmono:
            raise ValueError("case changes only")
        if wrapped.find(new) != -1:
            raise ValueError("is rotated")
        if similar(oldmono, newmono):
            raise ValueError("is too similar to the old one")

    if dictpath == None:
        FascistCheck(new)
    else:
        FascistCheck(new, dictpath)

    if palindrome(new):
        raise ValueError("is a palindrome")
    if simple(new):
        raise ValueError("is too simple")

    return new


def test(verbosity=1, repeat=1, dictpath=None):
    """Test cracklib methods."""
    import test_cracklib
    import sys
    result = test_cracklib.run(verbosity=verbosity, repeat=repeat, use_dictpath=dictpath)
    if result.wasSuccessful():
        sys.exit(0)
    sys.exit(1)