Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Daniel Stenberg | 053f6c8 | 2004-01-07 09:19:33 +0000 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 9 | * |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
| 12 | * are also available at http://curl.haxx.se/docs/copyright.html. |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 13 | * |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 16 | * furnished to do so, under the terms of the COPYING file. |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * $Id$ |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 22 | ***************************************************************************/ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 23 | |
| 24 | /*** |
| 25 | |
| 26 | |
| 27 | RECEIVING COOKIE INFORMATION |
| 28 | ============================ |
| 29 | |
| 30 | struct CookieInfo *cookie_init(char *file); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 31 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 32 | Inits a cookie struct to store data in a local file. This is always |
| 33 | called before any cookies are set. |
| 34 | |
| 35 | int cookies_set(struct CookieInfo *cookie, char *cookie_line); |
| 36 | |
| 37 | The 'cookie_line' parameter is a full "Set-cookie:" line as |
| 38 | received from a server. |
| 39 | |
| 40 | The function need to replace previously stored lines that this new |
| 41 | line superceeds. |
| 42 | |
| 43 | It may remove lines that are expired. |
| 44 | |
| 45 | It should return an indication of success/error. |
| 46 | |
| 47 | |
| 48 | SENDING COOKIE INFORMATION |
| 49 | ========================== |
| 50 | |
| 51 | struct Cookies *cookie_getlist(struct CookieInfo *cookie, |
| 52 | char *host, char *path, bool secure); |
| 53 | |
| 54 | For a given host and path, return a linked list of cookies that |
| 55 | the client should send to the server if used now. The secure |
| 56 | boolean informs the cookie if a secure connection is achieved or |
| 57 | not. |
| 58 | |
| 59 | It shall only return cookies that haven't expired. |
| 60 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 61 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 62 | Example set of cookies: |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 63 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 64 | Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure |
| 65 | Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 66 | domain=.fidelity.com; path=/ftgw; secure |
| 67 | Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 68 | domain=.fidelity.com; path=/; secure |
| 69 | Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 70 | domain=.fidelity.com; path=/; secure |
| 71 | Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 72 | domain=.fidelity.com; path=/; secure |
| 73 | Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 74 | domain=.fidelity.com; path=/; secure |
| 75 | Set-cookie: |
| 76 | Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday, |
| 77 | 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure |
| 78 | ****/ |
| 79 | |
Sterling Hughes | ad6fca2 | 2003-03-31 15:59:17 +0000 | [diff] [blame] | 80 | |
Daniel Stenberg | b6e18f2 | 2000-08-24 14:26:33 +0000 | [diff] [blame] | 81 | #include "setup.h" |
| 82 | |
Daniel Stenberg | 08ef208 | 2002-06-11 11:13:01 +0000 | [diff] [blame] | 83 | #ifndef CURL_DISABLE_HTTP |
| 84 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 85 | #include <stdlib.h> |
| 86 | #include <string.h> |
| 87 | #include <ctype.h> |
| 88 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 89 | #include "urldata.h" |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 90 | #include "cookie.h" |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 91 | #include "getdate.h" |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 92 | #include "strequal.h" |
Daniel Stenberg | 870bacd | 2001-05-30 11:06:56 +0000 | [diff] [blame] | 93 | #include "strtok.h" |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 94 | #include "sendf.h" |
Daniel Stenberg | bbafb2e | 2004-05-11 11:30:23 +0000 | [diff] [blame] | 95 | #include "memory.h" |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 96 | |
Daniel Stenberg | 0f8facb | 2000-10-09 11:12:34 +0000 | [diff] [blame] | 97 | /* The last #include file should be: */ |
Daniel Stenberg | 2bd71d7 | 2003-06-26 06:50:32 +0000 | [diff] [blame] | 98 | #ifdef CURLDEBUG |
Daniel Stenberg | 0f8facb | 2000-10-09 11:12:34 +0000 | [diff] [blame] | 99 | #include "memdebug.h" |
| 100 | #endif |
| 101 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 102 | static void freecookie(struct Cookie *co) |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 103 | { |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 104 | if(co->expirestr) |
| 105 | free(co->expirestr); |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 106 | if(co->domain) |
| 107 | free(co->domain); |
| 108 | if(co->path) |
| 109 | free(co->path); |
| 110 | if(co->name) |
| 111 | free(co->name); |
| 112 | if(co->value) |
| 113 | free(co->value); |
| 114 | |
| 115 | free(co); |
| 116 | } |
| 117 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 118 | static bool tailmatch(const char *little, const char *bigone) |
| 119 | { |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 120 | size_t littlelen = strlen(little); |
| 121 | size_t biglen = strlen(bigone); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 122 | |
| 123 | if(littlelen > biglen) |
| 124 | return FALSE; |
| 125 | |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 126 | return (bool)strequal(little, bigone+biglen-littlelen); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 129 | /**************************************************************************** |
| 130 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 131 | * Curl_cookie_add() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 132 | * |
| 133 | * Add a single cookie line to the cookie keeping object. |
| 134 | * |
| 135 | ***************************************************************************/ |
| 136 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 137 | struct Cookie * |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 138 | Curl_cookie_add(struct SessionHandle *data, |
| 139 | /* The 'data' pointer here may be NULL at times, and thus |
| 140 | must only be used very carefully for things that can deal |
| 141 | with data being NULL. Such as infof() and similar */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 142 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 143 | struct CookieInfo *c, |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 144 | bool httpheader, /* TRUE if HTTP header-style line */ |
Daniel Stenberg | d8b2c81 | 2002-07-29 22:22:49 +0000 | [diff] [blame] | 145 | char *lineptr, /* first character of the line */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 146 | char *domain, /* default domain */ |
| 147 | char *path) /* full path used when this cookie is set, |
| 148 | used to get default path for the cookie |
| 149 | unless set */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 150 | { |
| 151 | struct Cookie *clist; |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 152 | char *what; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 153 | char name[MAX_NAME]; |
| 154 | char *ptr; |
| 155 | char *semiptr; |
| 156 | struct Cookie *co; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 157 | struct Cookie *lastc=NULL; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 158 | time_t now = time(NULL); |
| 159 | bool replace_old = FALSE; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 160 | bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 161 | |
| 162 | /* First, alloc and init a new struct for it */ |
Daniel Stenberg | 7d8cd59 | 2004-02-26 14:52:16 +0000 | [diff] [blame] | 163 | co = (struct Cookie *)calloc(sizeof(struct Cookie), 1); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 164 | if(!co) |
| 165 | return NULL; /* bail out if we're this low on memory */ |
| 166 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 167 | if(httpheader) { |
| 168 | /* This line was read off a HTTP-header */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 169 | char *sep; |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 170 | |
| 171 | what = malloc(MAX_COOKIE_LINE); |
| 172 | if(!what) { |
| 173 | free(co); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 177 | semiptr=strchr(lineptr, ';'); /* first, find a semicolon */ |
Daniel Stenberg | d8b2c81 | 2002-07-29 22:22:49 +0000 | [diff] [blame] | 178 | |
| 179 | while(*lineptr && isspace((int)*lineptr)) |
| 180 | lineptr++; |
| 181 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 182 | ptr = lineptr; |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 183 | do { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 184 | /* we have a <what>=<this> pair or a 'secure' word here */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 185 | sep = strchr(ptr, '='); |
| 186 | if(sep && (!semiptr || (semiptr>sep)) ) { |
| 187 | /* |
| 188 | * There is a = sign and if there was a semicolon too, which make sure |
| 189 | * that the semicolon comes _after_ the equal sign. |
| 190 | */ |
| 191 | |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 192 | name[0]=what[0]=0; /* init the buffers */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 193 | if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;=]=%" |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 194 | MAX_COOKIE_LINE_TXT "[^;\r\n]", |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 195 | name, what)) { |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 196 | /* this is a <name>=<what> pair */ |
| 197 | |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 198 | char *whatptr; |
| 199 | |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 200 | /* Strip off trailing whitespace from the 'what' */ |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 201 | size_t len=strlen(what); |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 202 | while(len && isspace((int)what[len-1])) { |
| 203 | what[len-1]=0; |
| 204 | len--; |
| 205 | } |
| 206 | |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 207 | /* Skip leading whitespace from the 'what' */ |
| 208 | whatptr=what; |
| 209 | while(isspace((int)*whatptr)) { |
| 210 | whatptr++; |
| 211 | } |
| 212 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 213 | if(strequal("path", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 214 | co->path=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 215 | if(!co->path) { |
| 216 | badcookie = TRUE; /* out of memory bad */ |
| 217 | break; |
| 218 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 219 | } |
| 220 | else if(strequal("domain", name)) { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 221 | /* note that this name may or may not have a preceeding dot, but |
| 222 | we don't care about that, we treat the names the same anyway */ |
| 223 | |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 224 | const char *domptr=whatptr; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 225 | int dotcount=1; |
| 226 | unsigned int i; |
| 227 | |
| 228 | static const char *seventhree[]= { |
| 229 | "com", "edu", "net", "org", "gov", "mil", "int" |
| 230 | }; |
| 231 | |
| 232 | /* Count the dots, we need to make sure that there are THREE dots |
| 233 | in the normal domains, or TWO in the seventhree-domains. */ |
| 234 | |
| 235 | if('.' == whatptr[0]) |
| 236 | /* don't count the initial dot, assume it */ |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 237 | domptr++; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 238 | |
| 239 | do { |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 240 | domptr = strchr(domptr, '.'); |
| 241 | if(domptr) { |
| 242 | domptr++; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 243 | dotcount++; |
| 244 | } |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 245 | } while(domptr); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 246 | |
| 247 | for(i=0; |
| 248 | i<sizeof(seventhree)/sizeof(seventhree[0]); i++) { |
| 249 | if(tailmatch(seventhree[i], whatptr)) { |
| 250 | dotcount++; /* we allow one dot less for these */ |
| 251 | break; |
| 252 | } |
| 253 | } |
Daniel Stenberg | 98ee12b | 2003-08-04 23:05:57 +0000 | [diff] [blame] | 254 | /* The original Netscape cookie spec defined that this domain name |
| 255 | MUST have three dots (or two if one of the seven holy TLDs), |
| 256 | but it seems that these kinds of cookies are in use "out there" |
| 257 | so we cannot be that strict. I've therefore lowered the check |
| 258 | to not allow less than two dots. */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 259 | |
Daniel Stenberg | 98ee12b | 2003-08-04 23:05:57 +0000 | [diff] [blame] | 260 | if(dotcount < 2) { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 261 | /* Received and skipped a cookie with a domain using too few |
| 262 | dots. */ |
| 263 | badcookie=TRUE; /* mark this as a bad cookie */ |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 264 | infof(data, "skipped cookie with illegal dotcount domain: %s", |
| 265 | whatptr); |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 266 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 267 | else { |
| 268 | /* Now, we make sure that our host is within the given domain, |
| 269 | or the given domain is not valid and thus cannot be set. */ |
| 270 | |
Daniel Stenberg | 755f98e | 2004-05-21 20:40:15 +0000 | [diff] [blame] | 271 | if('.' == whatptr[0]) |
| 272 | whatptr++; /* ignore preceeding dot */ |
| 273 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 274 | if(!domain || tailmatch(whatptr, domain)) { |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 275 | const char *tailptr=whatptr; |
| 276 | if(tailptr[0] == '.') |
| 277 | tailptr++; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 278 | co->domain=strdup(tailptr); /* don't prefix w/dots |
| 279 | internally */ |
| 280 | if(!co->domain) { |
| 281 | badcookie = TRUE; |
| 282 | break; |
| 283 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 284 | co->tailmatch=TRUE; /* we always do that if the domain name was |
| 285 | given */ |
| 286 | } |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 287 | else { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 288 | /* we did not get a tailmatch and then the attempted set domain |
| 289 | is not a domain to which the current host belongs. Mark as |
| 290 | bad. */ |
| 291 | badcookie=TRUE; |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 292 | infof(data, "skipped cookie with bad tailmatch domain: %s", |
| 293 | whatptr); |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 294 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 295 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 296 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 297 | else if(strequal("version", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 298 | co->version=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 299 | if(!co->version) { |
| 300 | badcookie = TRUE; |
| 301 | break; |
| 302 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 303 | } |
| 304 | else if(strequal("max-age", name)) { |
| 305 | /* Defined in RFC2109: |
| 306 | |
| 307 | Optional. The Max-Age attribute defines the lifetime of the |
| 308 | cookie, in seconds. The delta-seconds value is a decimal non- |
| 309 | negative integer. After delta-seconds seconds elapse, the |
| 310 | client should discard the cookie. A value of zero means the |
| 311 | cookie should be discarded immediately. |
| 312 | |
| 313 | */ |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 314 | co->maxage = strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 315 | if(!co->maxage) { |
| 316 | badcookie = TRUE; |
| 317 | break; |
| 318 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 319 | co->expires = |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 320 | atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + now; |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 321 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 322 | else if(strequal("expires", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 323 | co->expirestr=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 324 | if(!co->expirestr) { |
| 325 | badcookie = TRUE; |
| 326 | break; |
| 327 | } |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 328 | co->expires = curl_getdate(what, &now); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 329 | } |
| 330 | else if(!co->name) { |
| 331 | co->name = strdup(name); |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 332 | co->value = strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 333 | if(!co->name || !co->value) { |
| 334 | badcookie = TRUE; |
| 335 | break; |
| 336 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 337 | } |
Daniel Stenberg | c892613 | 2001-08-14 08:17:29 +0000 | [diff] [blame] | 338 | /* |
| 339 | else this is the second (or more) name we don't know |
| 340 | about! */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 341 | } |
| 342 | else { |
| 343 | /* this is an "illegal" <what>=<this> pair */ |
| 344 | } |
| 345 | } |
| 346 | else { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 347 | if(sscanf(ptr, "%" MAX_COOKIE_LINE_TXT "[^;\r\n]", |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 348 | what)) { |
| 349 | if(strequal("secure", what)) |
| 350 | co->secure = TRUE; |
Daniel Stenberg | c892613 | 2001-08-14 08:17:29 +0000 | [diff] [blame] | 351 | /* else, |
| 352 | unsupported keyword without assign! */ |
| 353 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 356 | if(!semiptr || !*semiptr) { |
| 357 | /* we already know there are no more cookies */ |
| 358 | semiptr = NULL; |
| 359 | continue; |
| 360 | } |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 361 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 362 | ptr=semiptr+1; |
| 363 | while(ptr && *ptr && isspace((int)*ptr)) |
| 364 | ptr++; |
| 365 | semiptr=strchr(ptr, ';'); /* now, find the next semicolon */ |
Daniel Stenberg | 8703713 | 2002-01-07 23:05:36 +0000 | [diff] [blame] | 366 | |
| 367 | if(!semiptr && *ptr) |
| 368 | /* There are no more semicolons, but there's a final name=value pair |
| 369 | coming up */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 370 | semiptr=strchr(ptr, '\0'); |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 371 | } while(semiptr); |
Daniel Stenberg | 598e8df | 2001-09-26 07:08:29 +0000 | [diff] [blame] | 372 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 373 | if(!badcookie && !co->domain) { |
| 374 | if(domain) { |
| 375 | /* no domain was given in the header line, set the default */ |
| 376 | co->domain=strdup(domain); |
| 377 | if(!co->domain) |
| 378 | badcookie = TRUE; |
| 379 | } |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 382 | if(!badcookie && !co->path && path) { |
| 383 | /* no path was given in the header line, set the default */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 384 | char *endslash = strrchr(path, '/'); |
| 385 | if(endslash) { |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 386 | size_t pathlen = endslash-path+1; /* include the ending slash */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 387 | co->path=malloc(pathlen+1); /* one extra for the zero byte */ |
| 388 | if(co->path) { |
| 389 | memcpy(co->path, path, pathlen); |
| 390 | co->path[pathlen]=0; /* zero terminate */ |
| 391 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 392 | else |
| 393 | badcookie = TRUE; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 396 | |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 397 | free(what); |
| 398 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 399 | if(badcookie || !co->name) { |
| 400 | /* we didn't get a cookie name or a bad one, |
| 401 | this is an illegal line, bail out */ |
| 402 | freecookie(co); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 406 | } |
| 407 | else { |
| 408 | /* This line is NOT a HTTP header style line, we do offer support for |
| 409 | reading the odd netscape cookies-file format here */ |
| 410 | char *firstptr; |
Daniel Stenberg | d567659 | 2001-05-29 19:17:39 +0000 | [diff] [blame] | 411 | char *tok_buf; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 412 | int fields; |
| 413 | |
| 414 | if(lineptr[0]=='#') { |
| 415 | /* don't even try the comments */ |
| 416 | free(co); |
| 417 | return NULL; |
| 418 | } |
| 419 | /* strip off the possible end-of-line characters */ |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 420 | ptr=strchr(lineptr, '\r'); |
| 421 | if(ptr) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 422 | *ptr=0; /* clear it */ |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 423 | ptr=strchr(lineptr, '\n'); |
| 424 | if(ptr) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 425 | *ptr=0; /* clear it */ |
| 426 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 427 | firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 428 | |
| 429 | /* Here's a quick check to eliminate normal HTTP-headers from this */ |
| 430 | if(!firstptr || strchr(firstptr, ':')) { |
| 431 | free(co); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | /* Now loop through the fields and init the struct we already have |
| 436 | allocated */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 437 | for(ptr=firstptr, fields=0; ptr && !badcookie; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 438 | ptr=strtok_r(NULL, "\t", &tok_buf), fields++) { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 439 | switch(fields) { |
| 440 | case 0: |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 441 | if(ptr[0]=='.') /* skip preceeding dots */ |
| 442 | ptr++; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 443 | co->domain = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 444 | if(!co->domain) |
| 445 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 446 | break; |
| 447 | case 1: |
Daniel Stenberg | 72dec6c | 2001-05-23 13:04:19 +0000 | [diff] [blame] | 448 | /* This field got its explanation on the 23rd of May 2001 by |
| 449 | Andrés GarcÃa: |
| 450 | |
| 451 | flag: A TRUE/FALSE value indicating if all machines within a given |
| 452 | domain can access the variable. This value is set automatically by |
| 453 | the browser, depending on the value you set for the domain. |
| 454 | |
| 455 | As far as I can see, it is set to true when the cookie says |
| 456 | .domain.com and to false when the domain is complete www.domain.com |
Daniel Stenberg | 72dec6c | 2001-05-23 13:04:19 +0000 | [diff] [blame] | 457 | */ |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 458 | co->tailmatch=(bool)strequal(ptr, "TRUE"); /* store information */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 459 | break; |
| 460 | case 2: |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 461 | /* It turns out, that sometimes the file format allows the path |
| 462 | field to remain not filled in, we try to detect this and work |
| 463 | around it! Andrés GarcÃa made us aware of this... */ |
| 464 | if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) { |
| 465 | /* only if the path doesn't look like a boolean option! */ |
| 466 | co->path = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 467 | if(!co->path) |
| 468 | badcookie = TRUE; |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | /* this doesn't look like a path, make one up! */ |
| 472 | co->path = strdup("/"); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 473 | if(!co->path) |
| 474 | badcookie = TRUE; |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 475 | fields++; /* add a field and fall down to secure */ |
| 476 | /* FALLTHROUGH */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 477 | case 3: |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 478 | co->secure = (bool)strequal(ptr, "TRUE"); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 479 | break; |
| 480 | case 4: |
| 481 | co->expires = atoi(ptr); |
| 482 | break; |
| 483 | case 5: |
| 484 | co->name = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 485 | if(!co->name) |
| 486 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 487 | break; |
| 488 | case 6: |
| 489 | co->value = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 490 | if(!co->value) |
| 491 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
| 494 | } |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 495 | if(6 == fields) { |
| 496 | /* we got a cookie with blank contents, fix it */ |
| 497 | co->value = strdup(""); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 498 | if(!co->value) |
| 499 | badcookie = TRUE; |
| 500 | else |
| 501 | fields++; |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 502 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 503 | |
| 504 | if(!badcookie && (7 != fields)) |
| 505 | /* we did not find the sufficient number of fields */ |
| 506 | badcookie = TRUE; |
| 507 | |
| 508 | if(badcookie) { |
| 509 | freecookie(co); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 510 | return NULL; |
| 511 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 512 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 515 | if(!c->running && /* read from a file */ |
| 516 | c->newsession && /* clean session cookies */ |
| 517 | !co->expires) { /* this is a session cookie since it doesn't expire! */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 518 | freecookie(co); |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | } |
| 521 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 522 | co->livecookie = c->running; |
| 523 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 524 | /* now, we have parsed the incoming line, we must now check if this |
| 525 | superceeds an already existing cookie, which it may if the previous have |
| 526 | the same domain and path as this */ |
| 527 | |
| 528 | clist = c->cookies; |
| 529 | replace_old = FALSE; |
| 530 | while(clist) { |
| 531 | if(strequal(clist->name, co->name)) { |
| 532 | /* the names are identical */ |
| 533 | |
| 534 | if(clist->domain && co->domain) { |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 535 | if(strequal(clist->domain, co->domain)) |
| 536 | /* The domains are identical */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 537 | replace_old=TRUE; |
| 538 | } |
| 539 | else if(!clist->domain && !co->domain) |
| 540 | replace_old = TRUE; |
| 541 | |
| 542 | if(replace_old) { |
| 543 | /* the domains were identical */ |
| 544 | |
| 545 | if(clist->path && co->path) { |
| 546 | if(strequal(clist->path, co->path)) { |
| 547 | replace_old = TRUE; |
| 548 | } |
| 549 | else |
| 550 | replace_old = FALSE; |
| 551 | } |
| 552 | else if(!clist->path && !co->path) |
| 553 | replace_old = TRUE; |
| 554 | else |
| 555 | replace_old = FALSE; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 556 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 559 | if(replace_old && !co->livecookie && clist->livecookie) { |
| 560 | /* Both cookies matched fine, except that the already present |
| 561 | cookie is "live", which means it was set from a header, while |
| 562 | the new one isn't "live" and thus only read from a file. We let |
| 563 | live cookies stay alive */ |
| 564 | |
| 565 | /* Free the newcomer and get out of here! */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 566 | freecookie(co); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
| 569 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 570 | if(replace_old) { |
| 571 | co->next = clist->next; /* get the next-pointer first */ |
| 572 | |
| 573 | /* then free all the old pointers */ |
| 574 | if(clist->name) |
| 575 | free(clist->name); |
| 576 | if(clist->value) |
| 577 | free(clist->value); |
| 578 | if(clist->domain) |
| 579 | free(clist->domain); |
| 580 | if(clist->path) |
| 581 | free(clist->path); |
| 582 | if(clist->expirestr) |
| 583 | free(clist->expirestr); |
| 584 | |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 585 | if(clist->version) |
| 586 | free(clist->version); |
| 587 | if(clist->maxage) |
| 588 | free(clist->maxage); |
| 589 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 590 | *clist = *co; /* then store all the new data */ |
Daniel Stenberg | c6822f5 | 2001-10-24 11:36:55 +0000 | [diff] [blame] | 591 | |
| 592 | free(co); /* free the newly alloced memory */ |
| 593 | co = clist; /* point to the previous struct instead */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 594 | |
Daniel Stenberg | d9a7773 | 2002-01-07 14:56:15 +0000 | [diff] [blame] | 595 | /* We have replaced a cookie, now skip the rest of the list but |
| 596 | make sure the 'lastc' pointer is properly set */ |
| 597 | do { |
| 598 | lastc = clist; |
| 599 | clist = clist->next; |
| 600 | } while(clist); |
| 601 | break; |
| 602 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 603 | } |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 604 | lastc = clist; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 605 | clist = clist->next; |
| 606 | } |
| 607 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 608 | if(c->running) |
| 609 | /* Only show this when NOT reading the cookies from a file */ |
| 610 | infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, expire %d\n", |
| 611 | replace_old?"Replaced":"Added", co->name, co->value, |
| 612 | co->domain, co->path, co->expires); |
| 613 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 614 | if(!replace_old) { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 615 | /* then make the last item point on this new one */ |
| 616 | if(lastc) |
| 617 | lastc->next = co; |
| 618 | else |
| 619 | c->cookies = co; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 620 | } |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 621 | |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 622 | c->numcookies++; /* one more cookie in the jar */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 623 | return co; |
| 624 | } |
| 625 | |
| 626 | /***************************************************************************** |
| 627 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 628 | * Curl_cookie_init() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 629 | * |
| 630 | * Inits a cookie struct to read data from a local file. This is always |
| 631 | * called before any cookies are set. File may be NULL. |
| 632 | * |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 633 | * If 'newsession' is TRUE, discard all "session cookies" on read from file. |
| 634 | * |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 635 | ****************************************************************************/ |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 636 | struct CookieInfo *Curl_cookie_init(struct SessionHandle *data, |
| 637 | char *file, |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 638 | struct CookieInfo *inc, |
| 639 | bool newsession) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 640 | { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 641 | struct CookieInfo *c; |
| 642 | FILE *fp; |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 643 | bool fromfile=TRUE; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 644 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 645 | if(NULL == inc) { |
| 646 | /* we didn't get a struct, create one */ |
Daniel Stenberg | ce945bd | 2004-06-30 12:05:07 +0000 | [diff] [blame^] | 647 | c = (struct CookieInfo *)calloc(1, sizeof(struct CookieInfo)); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 648 | if(!c) |
| 649 | return NULL; /* failed to get memory */ |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 650 | c->filename = strdup(file?file:"none"); /* copy the name just in case */ |
| 651 | } |
| 652 | else { |
| 653 | /* we got an already existing one, use that */ |
| 654 | c = inc; |
| 655 | } |
| 656 | c->running = FALSE; /* this is not running, this is init */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 657 | |
Daniel Stenberg | f2a2596 | 2001-10-10 12:48:32 +0000 | [diff] [blame] | 658 | if(file && strequal(file, "-")) { |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 659 | fp = stdin; |
| 660 | fromfile=FALSE; |
| 661 | } |
| 662 | else |
| 663 | fp = file?fopen(file, "r"):NULL; |
| 664 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 665 | c->newsession = newsession; /* new session? */ |
| 666 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 667 | if(fp) { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 668 | char *lineptr; |
| 669 | bool headerline; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 670 | |
Daniel Stenberg | ce945bd | 2004-06-30 12:05:07 +0000 | [diff] [blame^] | 671 | char *line = (char *)malloc(MAX_COOKIE_LINE); |
| 672 | if(line) { |
| 673 | while(fgets(line, MAX_COOKIE_LINE, fp)) { |
| 674 | if(checkprefix("Set-Cookie:", line)) { |
| 675 | /* This is a cookie line, get it! */ |
| 676 | lineptr=&line[11]; |
| 677 | headerline=TRUE; |
| 678 | } |
| 679 | else { |
| 680 | lineptr=line; |
| 681 | headerline=FALSE; |
| 682 | } |
| 683 | while(*lineptr && isspace((int)*lineptr)) |
| 684 | lineptr++; |
| 685 | |
| 686 | Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL); |
| 687 | } |
| 688 | free(line); /* free the line buffer */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 689 | } |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 690 | if(fromfile) |
| 691 | fclose(fp); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 694 | c->running = TRUE; /* now, we're running */ |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 695 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 696 | return c; |
| 697 | } |
| 698 | |
| 699 | /***************************************************************************** |
| 700 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 701 | * Curl_cookie_getlist() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 702 | * |
| 703 | * For a given host and path, return a linked list of cookies that the |
| 704 | * client should send to the server if used now. The secure boolean informs |
| 705 | * the cookie if a secure connection is achieved or not. |
| 706 | * |
| 707 | * It shall only return cookies that haven't expired. |
| 708 | * |
| 709 | ****************************************************************************/ |
| 710 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 711 | struct Cookie *Curl_cookie_getlist(struct CookieInfo *c, |
| 712 | char *host, char *path, bool secure) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 713 | { |
| 714 | struct Cookie *newco; |
| 715 | struct Cookie *co; |
| 716 | time_t now = time(NULL); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 717 | struct Cookie *mainco=NULL; |
| 718 | |
| 719 | if(!c || !c->cookies) |
| 720 | return NULL; /* no cookie struct or no cookies in the struct */ |
| 721 | |
| 722 | co = c->cookies; |
| 723 | |
| 724 | while(co) { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 725 | /* only process this cookie if it is not expired or had no expire |
| 726 | date AND that if the cookie requires we're secure we must only |
| 727 | continue if we are! */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 728 | if( (co->expires<=0 || (co->expires> now)) && |
| 729 | (co->secure?secure:TRUE) ) { |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 730 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 731 | /* now check if the domain is correct */ |
| 732 | if(!co->domain || |
| 733 | (co->tailmatch && tailmatch(co->domain, host)) || |
| 734 | (!co->tailmatch && strequal(host, co->domain)) ) { |
| 735 | /* the right part of the host matches the domain stuff in the |
| 736 | cookie data */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 737 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 738 | /* now check the left part of the path with the cookies path |
| 739 | requirement */ |
| 740 | if(!co->path || |
| 741 | checkprefix(co->path, path) ) { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 742 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 743 | /* and now, we know this is a match and we should create an |
| 744 | entry for the return-linked-list */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 745 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 746 | newco = (struct Cookie *)malloc(sizeof(struct Cookie)); |
| 747 | if(newco) { |
| 748 | /* first, copy the whole source cookie: */ |
| 749 | memcpy(newco, co, sizeof(struct Cookie)); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 750 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 751 | /* then modify our next */ |
| 752 | newco->next = mainco; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 753 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 754 | /* point the main to us */ |
| 755 | mainco = newco; |
| 756 | } |
Daniel Stenberg | 5dcab07 | 2004-05-10 14:04:06 +0000 | [diff] [blame] | 757 | else { |
| 758 | /* failure, clear up the allocated chain and return NULL */ |
| 759 | while(mainco) { |
| 760 | co = mainco->next; |
| 761 | free(mainco); |
| 762 | mainco = co; |
| 763 | } |
| 764 | |
| 765 | return NULL; |
| 766 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | co = co->next; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return mainco; /* return the new list */ |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /***************************************************************************** |
| 778 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 779 | * Curl_cookie_freelist() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 780 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 781 | * Free a list of cookies previously returned by Curl_cookie_getlist(); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 782 | * |
| 783 | ****************************************************************************/ |
| 784 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 785 | void Curl_cookie_freelist(struct Cookie *co) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 786 | { |
| 787 | struct Cookie *next; |
| 788 | if(co) { |
| 789 | while(co) { |
| 790 | next = co->next; |
| 791 | free(co); /* we only free the struct since the "members" are all |
| 792 | just copied! */ |
| 793 | co = next; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /***************************************************************************** |
| 799 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 800 | * Curl_cookie_cleanup() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 801 | * |
| 802 | * Free a "cookie object" previous created with cookie_init(). |
| 803 | * |
| 804 | ****************************************************************************/ |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 805 | void Curl_cookie_cleanup(struct CookieInfo *c) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 806 | { |
| 807 | struct Cookie *co; |
| 808 | struct Cookie *next; |
| 809 | if(c) { |
| 810 | if(c->filename) |
| 811 | free(c->filename); |
| 812 | co = c->cookies; |
| 813 | |
| 814 | while(co) { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 815 | next = co->next; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 816 | freecookie(co); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 817 | co = next; |
| 818 | } |
Daniel Stenberg | 0f8facb | 2000-10-09 11:12:34 +0000 | [diff] [blame] | 819 | free(c); /* free the base struct as well */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 823 | /* |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 824 | * Curl_cookie_output() |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 825 | * |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 826 | * Writes all internally known cookies to the specified file. Specify |
| 827 | * "-" as file name to write to stdout. |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 828 | * |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 829 | * The function returns non-zero on write failure. |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 830 | */ |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 831 | int Curl_cookie_output(struct CookieInfo *c, char *dumphere) |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 832 | { |
| 833 | struct Cookie *co; |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 834 | FILE *out; |
| 835 | bool use_stdout=FALSE; |
| 836 | |
Daniel Stenberg | 3f5227d | 2001-09-10 07:43:08 +0000 | [diff] [blame] | 837 | if((NULL == c) || (0 == c->numcookies)) |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 838 | /* If there are no known cookies, we don't write or even create any |
| 839 | destination file */ |
| 840 | return 0; |
| 841 | |
| 842 | if(strequal("-", dumphere)) { |
| 843 | /* use stdout */ |
| 844 | out = stdout; |
| 845 | use_stdout=TRUE; |
| 846 | } |
| 847 | else { |
| 848 | out = fopen(dumphere, "w"); |
| 849 | if(!out) |
| 850 | return 1; /* failure */ |
| 851 | } |
| 852 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 853 | if(c) { |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 854 | fputs("# Netscape HTTP Cookie File\n" |
| 855 | "# http://www.netscape.com/newsref/std/cookie_spec.html\n" |
Daniel Stenberg | e719f41 | 2001-10-08 06:43:22 +0000 | [diff] [blame] | 856 | "# This file was generated by libcurl! Edit at your own risk.\n\n", |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 857 | out); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 858 | co = c->cookies; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 859 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 860 | while(co) { |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 861 | fprintf(out, |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 862 | "%s%s\t" /* domain */ |
| 863 | "%s\t" /* tailmatch */ |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 864 | "%s\t" /* path */ |
| 865 | "%s\t" /* secure */ |
| 866 | "%u\t" /* expires */ |
| 867 | "%s\t" /* name */ |
| 868 | "%s\n", /* value */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 869 | |
| 870 | /* Make sure all domains are prefixed with a dot if they allow |
| 871 | tailmatching. This is Mozilla-style. */ |
| 872 | (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"", |
Daniel Stenberg | 598e8df | 2001-09-26 07:08:29 +0000 | [diff] [blame] | 873 | co->domain?co->domain:"unknown", |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 874 | co->tailmatch?"TRUE":"FALSE", |
Daniel Stenberg | 598e8df | 2001-09-26 07:08:29 +0000 | [diff] [blame] | 875 | co->path?co->path:"/", |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 876 | co->secure?"TRUE":"FALSE", |
| 877 | (unsigned int)co->expires, |
| 878 | co->name, |
Daniel Stenberg | 598e8df | 2001-09-26 07:08:29 +0000 | [diff] [blame] | 879 | co->value?co->value:""); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 880 | |
| 881 | co=co->next; |
| 882 | } |
| 883 | } |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 884 | |
| 885 | if(!use_stdout) |
| 886 | fclose(out); |
| 887 | |
| 888 | return 0; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Daniel Stenberg | 08ef208 | 2002-06-11 11:13:01 +0000 | [diff] [blame] | 891 | #endif /* CURL_DISABLE_HTTP */ |