summaryrefslogtreecommitdiffstats
path: root/private/nw/svcdlls/nwwks/client/ccache.c
blob: 9374352fcecfe7e8f38054efc8ea3e8f54acb58f (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
/*++

Copyright (c) 1993  Microsoft Corporation

Module Name:

    ccache.c

Abstract:

    This module contains the code to keep a cache of the user
    credentials. The cache is used mainly for a user browsing from
    winfile. 

Author:

    Chuck Y Chan (chuckc)    4-Dec-93

Revision History:

    chuckc      Created

--*/

#include <nwclient.h>
#include <nwcanon.h>
#include <nwapi.h>

//-------------------------------------------------------------------//
//                                                                   //
// Local Function Prototypes                                         //
//                                                                   //
//-------------------------------------------------------------------//

DWORD
ExtractServerName(
    IN  LPWSTR RemoteName, 
    OUT LPWSTR ServerName,
    IN  DWORD  ServerNameSize
) ;

//-------------------------------------------------------------------//
//                                                                   //
// Global variables                                                  //
//                                                                   //
//-------------------------------------------------------------------//

static WCHAR CachedPassword[NW_MAX_PASSWORD_LEN+1] ;
static WCHAR CachedUserName[NW_MAX_USERNAME_LEN+1] ;
static WCHAR CachedServerName[NW_MAX_SERVER_LEN+1] ;
static DWORD CachedCredentialsTime ; 
static UNICODE_STRING CachedPasswordUnicodeStr ;
static UCHAR EncodeSeed = 0 ;

//-------------------------------------------------------------------//
//                                                                   //
// Function Bodies                                                   //
//                                                                   //
//-------------------------------------------------------------------//

DWORD
NwpCacheCredentials(
    IN LPWSTR RemoteName,
    IN LPWSTR UserName,
    IN LPWSTR Password
    )
/*++

Routine Description:

    This function caches the user credentials for the particular
    server. 

Arguments:

    RemoteName - path containg the server we are accessing. only the
                 server component is of interest.

    UserName - user name to remember 

    Password - password to remember

Return Value:

    NO_ERROR - Successfully cached the credentials

    Win32 error code otherwise.

--*/
{
    DWORD status ; 

    //
    // various paramter checks
    //
    if (!RemoteName || !UserName || !Password)
    {
        status = ERROR_INVALID_PARAMETER ;
        goto ExitPoint ;
    }

    if (wcslen(UserName) >= sizeof(CachedUserName)/sizeof(CachedUserName[0]))
    {
        status = ERROR_INVALID_PARAMETER ;
        goto ExitPoint ;
    }

    if (wcslen(Password) >= sizeof(CachedPassword)/sizeof(CachedPassword[0]))
    {
        status = ERROR_INVALID_PARAMETER ;
        goto ExitPoint ;
    }

    //
    // extract the server portion of the path
    //
    status = ExtractServerName(
                 RemoteName,
                 CachedServerName,
                 sizeof(CachedServerName)/sizeof(CachedServerName[0])) ;

    if (status != NO_ERROR)
    {
        goto ExitPoint ;
    }

    //
    // save away the credentials
    //
    wcscpy(CachedUserName, UserName) ;
    wcscpy(CachedPassword, Password) ;

    //
    // encode it since it is in page pool
    //
    RtlInitUnicodeString(&CachedPasswordUnicodeStr, CachedPassword) ;
    RtlRunEncodeUnicodeString(&EncodeSeed, &CachedPasswordUnicodeStr) ;

    //
    // mark the time this happened
    //
    CachedCredentialsTime = GetTickCount() ;
   
    return NO_ERROR ;

ExitPoint:
  
    CachedServerName[0] = 0 ;
    return status ;
}

    
BOOL 
NwpRetrieveCachedCredentials(
    IN  LPWSTR RemoteName,
    OUT LPWSTR *UserName,
    OUT LPWSTR *Password
    )
/*++

Routine Description:

    This function retrieves the cached user credentials for the particular
    server. 

Arguments:

    RemoteName - path containg the server we are accessing. only the
                 server component is of interest.

    UserName - used to return user name 

    Password - used to return password 

Return Value:

    NO_ERROR - Successfully returned at least one entry.

    Win32 error code otherwise.

--*/
{
    DWORD status ; 
    DWORD CurrentTime ;
    WCHAR ServerName[NW_MAX_SERVER_LEN+1] ;

    *UserName = NULL ;
    *Password = NULL ;
    CurrentTime = GetTickCount() ; 
   
    if (!RemoteName)
    {
        return FALSE ;
    }

    //
    // if too old, bag out
    //
    if (((CurrentTime > CachedCredentialsTime) && 
         (CurrentTime - CachedCredentialsTime) > 60000) ||
        ((CurrentTime < CachedCredentialsTime) && 
         (CurrentTime + (MAXULONG - CachedCredentialsTime)) >= 60000))
    {
        CachedServerName[0] = 0 ; // reset to nothing
        return FALSE ;
    }

    status = ExtractServerName(
                 RemoteName,
                 ServerName,
                 sizeof(ServerName)/sizeof(ServerName[0])) ;

    if (status != NO_ERROR)
    {
        return FALSE ;
    }

    //
    // if dont compare, bag out
    //
    if (_wcsicmp(ServerName, CachedServerName) != 0)
    {
        return FALSE ;
    }

    //
    // allocate memory to return data
    //
    if (!(*UserName = (LPWSTR) LocalAlloc(
                          LPTR, 
                          (wcslen(CachedUserName)+1) * sizeof(WCHAR))))
    {
        return FALSE ;
    }
    
    if (!(*Password = (LPWSTR) LocalAlloc(
                          LPTR, 
                          (wcslen(CachedPassword)+1) * sizeof(WCHAR))))
    {
        LocalFree((HLOCAL)*UserName) ;
        *UserName = NULL ;
        return FALSE ;
    }
    
    //
    // decode the string,copy it and then reencode it 
    //
    RtlRunDecodeUnicodeString(EncodeSeed, &CachedPasswordUnicodeStr) ;
    wcscpy(*Password, CachedPassword) ;
    RtlRunEncodeUnicodeString(&EncodeSeed, &CachedPasswordUnicodeStr) ;

    wcscpy(*UserName, CachedUserName) ;

    //
    // update the tick count
    //
    CachedCredentialsTime = GetTickCount() ;
    return TRUE ;
}


DWORD
ExtractServerName(
    IN  LPWSTR RemoteName, 
    OUT LPWSTR ServerName,
    IN  DWORD  ServerNameSize
) 
/*++

Routine Description:

    This function extracts the server name out of a remote name

Arguments:

    RemoteName - the input string to extract the server name from.

    ServerName - the return buffer for the server string
 
    ServerNameSize - size o f buffer in chars 

Return Value:

    NO_ERROR - Successfully cached the credentials

    Win32 error code otherwise.

--*/
{
    LPWSTR ServerStart ;
    LPWSTR ServerEnd ;

    //
    // skip initial backslashes, then find next one delimiting the server name
    //
    ServerStart = RemoteName ;

    while (*ServerStart == L'\\')
        ServerStart++ ;
  
    ServerEnd = wcschr(ServerStart, L'\\') ;
    if (ServerEnd)
        *ServerEnd = 0 ;

    //
    // make sure we can fit
    //
    if (wcslen(ServerStart) >= ServerNameSize)
    {
        if (ServerEnd)
            *ServerEnd = L'\\' ;
        return ERROR_INVALID_PARAMETER ;
    }

    //
    // copy and restore the backslash
    //
    wcscpy(ServerName, ServerStart) ;

    if (ServerEnd)
        *ServerEnd = L'\\' ;

    return NO_ERROR ;
}