summaryrefslogtreecommitdiffstats
path: root/private/newsam/passfilt/passfilt.c
blob: cdab847429572f70326b350c7455e35ea2ccce8f (plain) (blame)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*++

   Copyright (c) 1995, 1996  Microsoft Corporation

   Module Name:

       pswdntfy.c

   Abstract:

       This module illustrates how to implement password change
       notification and password filtering in Windows NT 4.0.

       Password change notification is useful for synchronization of
       non-Windows NT account databases.

       Password change filtering is useful for enforcing quality or
       strength of passwords in an Windows NT account database.

       This sample illustrates one approach to enforcing additional
       password quality.

   Author:

       Scott Field (sfield)            14-May-96
       Gurdeep Singh Pall (gurdeep)    9/30/96 Added code to enforce the following discipline.

       The new " Strong" Password must meet the following criteria:
       1. Password must be at least 6 characters long.
       2. Password must contain characters from at least 3 of the following 4 classes:

        Description     Examples
        1       English Upper Case Letters      A, B, C,   Z
        2       English Lower Case Letters      a, b, c,  z
        3       Westernized Arabic Numerals     0, 1, 2,  9
        4       Non-alphanumeric ("Special characters") E.g., punctuation symbols.

        Mike Swift (mikesw)             10/31/96 Cleaned Up


   --*/

#include <windows.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ntsecapi.h>
#include <lmcons.h>

#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS  ((NTSTATUS)0x00000000L)
#endif



//+-------------------------------------------------------------------------
//
//  Function:   LocalStrTok
//
//  Synopsis:   takes a pointer to a string, returns a pointer to the next
//              token in the string and sets StringStart to point to the
//              end of the string.
//
//  Effects:
//
//  Arguments:
//
//  Requires:
//
//  Returns:
//
//  Notes:
//
//
//--------------------------------------------------------------------------


LPSTR
LocalStrTok(
    LPSTR String,
    LPSTR Token,
    LPSTR * NextStringStart
    )
{
    ULONG Index;
    ULONG Tokens;
    LPSTR StartString;
    LPSTR EndString;
    BOOLEAN Found;

    if (String == NULL)
    {
        *NextStringStart = NULL;
        return(NULL);
    }
    Tokens = lstrlenA(Token);

    //
    // Find the beginning of the string.
    //

    StartString = (LPSTR) String;
    while (*StartString != '\0')
    {
        Found = FALSE;
        for (Index = 0; Index < Tokens;  Index++)
        {
            if (*StartString == Token[Index])
            {
                StartString++;
                Found = TRUE;
                break;
            }
        }
        if (!Found)
        {
            break;
        }
    }

    //
    // There are no more tokens in this string.
    //

    if (*StartString == '\0')
    {
        *NextStringStart = NULL;
        return(NULL);
    }

    EndString = StartString + 1;
    while (*EndString != '\0')
    {
        for (Index = 0; Index < Tokens;  Index++)
        {
            if (*EndString == Token[Index])
            {
                *EndString = '\0';
                *NextStringStart = EndString+1;
                return(StartString);
            }
        }
        EndString++;
    }
    *NextStringStart = NULL;
    return(StartString);

}

BOOLEAN
NTAPI
PasswordFilter(
    PUNICODE_STRING UserName,
    PUNICODE_STRING FullName,
    PUNICODE_STRING Password,
    BOOLEAN SetOperation
    )
/*++

Routine Description:

    This (optional) routine is notified of a password change.

Arguments:

    UserName - Name of user whose password changed

    FullName - Full name of the user whose password changed

    NewPassword - Cleartext new password for the user

    SetOperation - TRUE if the password was SET rather than CHANGED

Return Value:

    TRUE if the specified Password is suitable (complex, long, etc).
     The system will continue to evaluate the password update request
     through any other installed password change packages.

    FALSE if the specified Password is unsuitable. The password change
     on the specified account will fail.

--*/
{

    BOOLEAN bComplex = FALSE; // assume the password in not complex enough
    DWORD cchPassword;
    DWORD i;
    DWORD j;
    DWORD count;
    DWORD dwNum = 0;
    DWORD dwUpper = 0;
    DWORD dwLower = 0;
    DWORD dwSpecialChar = 0 ;
    PCHAR token ;
    CHAR _password[PWLEN+1];
    CHAR _username[UNLEN+1];
    PCHAR _fullname = NULL;
    WORD CharType[PWLEN+1];
    PCHAR TempString;

    //
    // If the password was explicitly set, allow it through.
    //

    if (SetOperation)
    {
        bComplex = TRUE;
        goto end;
    }

    //
    // Make sure the password and username will fit in our local buffers
    //

    if (Password->Length > PWLEN * sizeof(WCHAR))
    {
        goto end;
    }

    if (UserName->Length > UNLEN * sizeof(WCHAR))
    {
        goto end;
    }

    _fullname = HeapAlloc(GetProcessHeap(), 0, FullName->Length + sizeof(WCHAR));
    if (_fullname == NULL)
    {
        goto end;
    }


    //
    // check if the password is complex enough for our liking by
    // checking that at least two of the four character types are
    // present.
    //

    cchPassword = Password->Length / sizeof(WCHAR);


    if(GetStringTypeW(
        CT_CTYPE1,
        Password->Buffer,
        cchPassword,
        CharType
        )) {

        for(i = 0 ; i < cchPassword ; i++) {

            //
            // keep track of what type of characters we have encountered
            //

            if(CharType[i] & C1_DIGIT) {
                dwNum = 1;
                continue;
            }

            if(CharType[i] & C1_UPPER) {
                dwUpper = 1;
                continue;
            }

            if(CharType[i] & C1_LOWER) {
                dwLower = 1;
                continue;
            }

        } // for

        //
        // Indicate whether we encountered enough password complexity
        //

        if( (dwNum + dwLower + dwUpper) < 2) {

            bComplex = FALSE ;
            goto end ;

        } else {

            //
            // now we resort to more complex checking
            //
            wcstombs(_password, Password->Buffer, PWLEN+1) ;
            wcstombs(_username, UserName->Buffer, UNLEN+1) ;
            wcstombs(_fullname, FullName->Buffer, 1+FullName->Length/sizeof(WCHAR)) ;

            _strupr(_password) ;
            _password[Password->Length/sizeof(WCHAR)] = '\0' ;
            _strupr(_username) ;
            _username[UserName->Length/sizeof(WCHAR)] = '\0' ;
            _strupr(_fullname) ;
            _fullname[FullName->Length/sizeof(WCHAR)] = '\0' ;

            if (strpbrk (_password, "(`~!@#$%^&*_-+=|\\{}[]:;\"'<>,.?)") != NULL) {
                dwSpecialChar = 1 ;
            }

            if ((dwNum + dwLower + dwUpper + dwSpecialChar) < 3) {
                bComplex = FALSE ;
                goto end ;
            }

            if ((UserName->Length >= 3 * sizeof(WCHAR)) && strstr (_password, _username)) {

                bComplex = FALSE ;
                goto end ;
            }


            //
            // Tokenize the full name and check if the password is derived from it
            //

            token = LocalStrTok(_fullname, " ,.\t-_#",&TempString);
            while( token != NULL ) {

                if (lstrlenA(token) > 3 && strstr(_password, token)) {
                    bComplex = FALSE ;
                    goto end ;
                }

                token = LocalStrTok(NULL, " ,.\t-_#",&TempString);
            }

            bComplex = TRUE ;

        }


    } // if

end:

    ZeroMemory( CharType, Password->Length );
    ZeroMemory( _password, Password->Length );
    HeapFree(GetProcessHeap(), 0, _fullname);

    return bComplex;
}



BOOL WINAPI DllMain(
    HINSTANCE   hinstDLL,                               // DLL instance handle
    DWORD       fdwReason,                              // Why is it called
    LPVOID      lpvReserved
    ) {

    return TRUE ;

}