Janitorial: Get rid of strncpy/strncpyW.
[wine] / dlls / wininet / cookie.c
1 /*
2  * Wininet - cookie handling stuff
3  *
4  * Copyright 2002 TransGaming Technologies Inc.
5  *
6  * David Hammerton
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wininet.h"
37 #include "winerror.h"
38
39 #include "wine/debug.h"
40 #include "internet.h"
41
42 #include "wine/list.h"
43
44 #define RESPONSE_TIMEOUT        30            /* FROM internet.c */
45
46
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
48
49 /* FIXME
50  *     Cookies are currently memory only.
51  *     Cookies are NOT THREAD SAFE
52  *     Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
53  *     Cookies should care about the expiry time
54  */
55
56 typedef struct _cookie_domain cookie_domain;
57 typedef struct _cookie cookie;
58
59 struct _cookie
60 {
61     struct list entry;
62
63     struct _cookie_domain *parent;
64
65     LPWSTR lpCookieName;
66     LPWSTR lpCookieData;
67     time_t expiry; /* FIXME: not used */
68 };
69
70 struct _cookie_domain
71 {
72     struct list entry;
73
74     LPWSTR lpCookieDomain;
75     LPWSTR lpCookiePath;
76     struct list cookie_list;
77 };
78
79 static struct list domain_list = LIST_INIT(domain_list);
80
81 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data);
82 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
83 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
84 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
85 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
86
87
88 /* adds a cookie to the domain */
89 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data)
90 {
91     cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
92
93     list_init(&newCookie->entry);
94     newCookie->lpCookieName = NULL;
95     newCookie->lpCookieData = NULL;
96
97     if (name)
98     {
99         newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
100         lstrcpyW(newCookie->lpCookieName, name);
101     }
102     if (data)
103     {
104         newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
105         lstrcpyW(newCookie->lpCookieData, data);
106     }
107
108     TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
109
110     list_add_tail(&domain->cookie_list, &newCookie->entry);
111     newCookie->parent = domain;
112     return newCookie;
113 }
114
115
116 /* finds a cookie in the domain matching the cookie name */
117 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
118 {
119     struct list * cursor;
120     TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
121
122     LIST_FOR_EACH(cursor, &domain->cookie_list)
123     {
124         cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
125         BOOL candidate = TRUE;
126         if (candidate && lpszCookieName)
127         {
128             if (candidate && !searchCookie->lpCookieName)
129                 candidate = FALSE;
130             if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
131                 candidate = FALSE;
132         }
133         if (candidate)
134             return searchCookie;
135     }
136     return NULL;
137 }
138
139 /* removes a cookie from the list, if its the last cookie we also remove the domain */
140 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
141 {
142     HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
143     HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
144     list_remove(&deadCookie->entry);
145
146     /* special case: last cookie, lets remove the domain to save memory */
147     if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
148         COOKIE_deleteDomain(deadCookie->parent);
149     HeapFree(GetProcessHeap(), 0, deadCookie);
150 }
151
152 /* allocates a domain and adds it to the end */
153 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
154 {
155     cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
156
157     list_init(&newDomain->entry);
158     list_init(&newDomain->cookie_list);
159     newDomain->lpCookieDomain = NULL;
160     newDomain->lpCookiePath = NULL;
161
162     if (domain)
163     {
164         newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
165         strcpyW(newDomain->lpCookieDomain, domain);
166     }
167     if (path)
168     {
169         newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
170         lstrcpyW(newDomain->lpCookiePath, path);
171     }
172
173     list_add_tail(&domain_list, &newDomain->entry);
174
175     TRACE("Adding domain: %p\n", newDomain);
176     return newDomain;
177 }
178
179 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
180 {
181     URL_COMPONENTSW UrlComponents;
182
183     UrlComponents.lpszExtraInfo = NULL;
184     UrlComponents.lpszPassword = NULL;
185     UrlComponents.lpszScheme = NULL;
186     UrlComponents.lpszUrlPath = path;
187     UrlComponents.lpszUserName = NULL;
188     UrlComponents.lpszHostName = hostName;
189     UrlComponents.dwHostNameLength = hostNameLen;
190     UrlComponents.dwUrlPathLength = pathLen;
191
192     InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
193 }
194
195 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
196 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
197                                cookie_domain *searchDomain, BOOL allow_partial)
198 {
199     TRACE("searching on domain %p\n", searchDomain);
200         if (lpszCookieDomain)
201         {
202             if (!searchDomain->lpCookieDomain)
203             return FALSE;
204
205             TRACE("comparing domain %s with %s\n", 
206             debugstr_w(lpszCookieDomain), 
207             debugstr_w(searchDomain->lpCookieDomain));
208
209         if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
210             return FALSE;
211         else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
212             return FALSE;
213         }
214     if (lpszCookiePath)
215     {
216         TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
217         if (!searchDomain->lpCookiePath)
218             return FALSE;
219         if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
220             return FALSE;
221         }
222         return TRUE;
223 }
224
225 /* remove a domain from the list and delete it */
226 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
227 {
228     struct list * cursor;
229     while ((cursor = list_tail(&deadDomain->cookie_list)))
230     {
231         COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
232         list_remove(cursor);
233     }
234
235     HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
236     HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
237
238     list_remove(&deadDomain->entry);
239
240     HeapFree(GetProcessHeap(), 0, deadDomain);
241 }
242
243 /***********************************************************************
244  *           InternetGetCookieW (WININET.@)
245  *
246  * Retrieve cookie from the specified url
247  *
248  *  It should be noted that on windows the lpszCookieName parameter is "not implemented".
249  *    So it won't be implemented here.
250  *
251  * RETURNS
252  *    TRUE  on success
253  *    FALSE on failure
254  *
255  */
256 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
257     LPWSTR lpCookieData, LPDWORD lpdwSize)
258 {
259     struct list * cursor;
260     int cnt = 0, domain_count = 0;
261     int cookie_count = 0;
262     WCHAR hostName[2048], path[2048];
263
264     TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
265           lpCookieData, lpdwSize);
266
267     COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
268
269     LIST_FOR_EACH(cursor, &domain_list)
270     {
271         cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
272         if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
273         {
274             struct list * cursor;
275             domain_count++;
276             TRACE("found domain %p\n", cookiesDomain);
277     
278             LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
279             {
280                 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
281                 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
282                 {
283                     if (cookie_count != 0)
284                         cnt += 2; /* '; ' */
285                     cnt += strlenW(thisCookie->lpCookieName);
286                     cnt += 1; /* = */
287                     cnt += strlenW(thisCookie->lpCookieData);
288                 }
289                 else
290                 {
291                     static const WCHAR szsc[] = { ';',' ',0 };
292                     static const WCHAR szpseq[] = { '%','s','=','%','s',0 };
293                     if (cookie_count != 0)
294                         cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
295                     cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq,
296                                     thisCookie->lpCookieName,
297                                     thisCookie->lpCookieData);
298                     TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData));
299                 }
300                 cookie_count++;
301             }
302         }
303     }
304     if (lpCookieData == NULL)
305     {
306         cnt += 1; /* NULL */
307         *lpdwSize = cnt*sizeof(WCHAR);
308         TRACE("returning\n");
309         return TRUE;
310     }
311
312     if (!domain_count)
313         return FALSE;
314
315     *lpdwSize = (cnt + 1)*sizeof(WCHAR);
316
317     TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count,
318            debugstr_w(lpCookieData));
319
320     return (cnt ? TRUE : FALSE);
321 }
322
323
324 /***********************************************************************
325  *           InternetGetCookieA (WININET.@)
326  *
327  * Retrieve cookie from the specified url
328  *
329  * RETURNS
330  *    TRUE  on success
331  *    FALSE on failure
332  *
333  */
334 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
335     LPSTR lpCookieData, LPDWORD lpdwSize)
336 {
337     DWORD len;
338     LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
339     BOOL r;
340
341     TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
342         lpCookieData);
343
344     if( lpszUrl )
345     {
346         len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
347         szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
348         MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
349     }
350
351     if( lpszCookieName )
352     {
353         len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
354         szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
355         MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
356     }
357
358     r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
359     if( r )
360     {
361         szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
362         if( !szCookieData )
363             return FALSE;
364
365         r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
366
367         *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
368                                 lpCookieData, *lpdwSize, NULL, NULL );
369     }
370
371     HeapFree( GetProcessHeap(), 0, szCookieData );
372     HeapFree( GetProcessHeap(), 0, szCookieName );
373     HeapFree( GetProcessHeap(), 0, szUrl );
374
375     return r;
376 }
377
378
379 /***********************************************************************
380  *           InternetSetCookieW (WININET.@)
381  *
382  * Sets cookie for the specified url
383  *
384  * RETURNS
385  *    TRUE  on success
386  *    FALSE on failure
387  *
388  */
389 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
390     LPCWSTR lpCookieData)
391 {
392     cookie_domain *thisCookieDomain = NULL;
393     cookie *thisCookie;
394     WCHAR hostName[2048], path[2048];
395     struct list * cursor;
396
397     TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
398         debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
399
400     if (!lpCookieData || !strlenW(lpCookieData))
401     {
402         TRACE("no cookie data, not adding\n");
403         return FALSE;
404     }
405     if (!lpszCookieName)
406     {
407         /* some apps (or is it us??) try to add a cookie with no cookie name, but
408          * the cookie data in the form of name=data. */
409         /* FIXME, probably a bug here, for now I don't care */
410         WCHAR *ourCookieName, *ourCookieData;
411         int ourCookieNameSize;
412         BOOL ret;
413
414         if (!(ourCookieData = strchrW(lpCookieData, '=')))
415         {
416             TRACE("something terribly wrong with cookie data %s\n", 
417                    debugstr_w(ourCookieData));
418             return FALSE;
419         }
420         ourCookieNameSize = ourCookieData - lpCookieData;
421         ourCookieData += 1;
422         ourCookieName = HeapAlloc(GetProcessHeap(), 0, 
423                               (ourCookieNameSize + 1)*sizeof(WCHAR));
424         memcpy(ourCookieName, ourCookieData, ourCookieNameSize * sizeof(WCHAR));
425         ourCookieName[ourCookieNameSize] = '\0';
426         TRACE("setting (hacked) cookie of %s, %s\n",
427                debugstr_w(ourCookieName), debugstr_w(ourCookieData));
428         ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
429         HeapFree(GetProcessHeap(), 0, ourCookieName);
430         return ret;
431     }
432
433     COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
434
435     LIST_FOR_EACH(cursor, &domain_list)
436     {
437         thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
438         if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
439             break;
440         thisCookieDomain = NULL;
441     }
442     if (!thisCookieDomain)
443         thisCookieDomain = COOKIE_addDomain(hostName, path);
444
445     if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
446         COOKIE_deleteCookie(thisCookie, FALSE);
447
448     thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
449     return TRUE;
450 }
451
452
453 /***********************************************************************
454  *           InternetSetCookieA (WININET.@)
455  *
456  * Sets cookie for the specified url
457  *
458  * RETURNS
459  *    TRUE  on success
460  *    FALSE on failure
461  *
462  */
463 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
464     LPCSTR lpCookieData)
465 {
466     DWORD len;
467     LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
468     BOOL r;
469
470     TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
471         debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
472
473     if( lpszUrl )
474     {
475         len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
476         szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
477         MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
478     }
479
480     if( lpszCookieName )
481     {
482         len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
483         szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
484         MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
485     }
486
487     if( lpCookieData )
488     {
489         len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
490         szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
491         MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
492     }
493
494     r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
495
496     HeapFree( GetProcessHeap(), 0, szCookieData );
497     HeapFree( GetProcessHeap(), 0, szCookieName );
498     HeapFree( GetProcessHeap(), 0, szUrl );
499
500     return r;
501 }