2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define XT_REJECT_FILE ("rejected.txt")
20 #define XT_COOKIE_FILE ("cookies.txt")
22 /* hooked functions */
23 void (*_soup_cookie_jar_add_cookie
)(SoupCookieJar
*, SoupCookie
*);
24 void (*_soup_cookie_jar_delete_cookie
)(SoupCookieJar
*,
28 print_cookie(char *msg
, SoupCookie
*c
)
34 DNPRINTF(XT_D_COOKIE
, "%s\n", msg
);
35 DNPRINTF(XT_D_COOKIE
, "name : %s\n", c
->name
);
36 DNPRINTF(XT_D_COOKIE
, "value : %s\n", c
->value
);
37 DNPRINTF(XT_D_COOKIE
, "domain : %s\n", c
->domain
);
38 DNPRINTF(XT_D_COOKIE
, "path : %s\n", c
->path
);
39 DNPRINTF(XT_D_COOKIE
, "expires : %s\n",
40 c
->expires
? soup_date_to_string(c
->expires
, SOUP_DATE_HTTP
) : "");
41 DNPRINTF(XT_D_COOKIE
, "secure : %d\n", c
->secure
);
42 DNPRINTF(XT_D_COOKIE
, "http_only: %d\n", c
->http_only
);
43 DNPRINTF(XT_D_COOKIE
, "====================================\n");
47 set_hook(void **hook
, char *name
)
53 *hook
= dlsym(RTLD_NEXT
, name
);
55 errx(1, "can't hook %s", name
);
59 /* override libsoup soup_cookie_equal because it doesn't look at domain */
61 soup_cookie_equal(SoupCookie
*cookie1
, SoupCookie
*cookie2
)
63 g_return_val_if_fail(cookie1
, FALSE
);
64 g_return_val_if_fail(cookie2
, FALSE
);
66 return (!strcmp (cookie1
->name
, cookie2
->name
) &&
67 !strcmp (cookie1
->value
, cookie2
->value
) &&
68 !strcmp (cookie1
->path
, cookie2
->path
) &&
69 !strcmp (cookie1
->domain
, cookie2
->domain
));
73 transfer_cookies(void)
78 cf
= soup_cookie_jar_all_cookies(p_cookiejar
);
80 for (;cf
; cf
= cf
->next
) {
82 sc
= soup_cookie_copy(pc
);
83 _soup_cookie_jar_add_cookie(s_cookiejar
, sc
);
86 soup_cookies_free(cf
);
90 soup_cookie_jar_delete_cookie(SoupCookieJar
*jar
, SoupCookie
*c
)
95 print_cookie("soup_cookie_jar_delete_cookie", c
);
97 if (cookies_enabled
== 0)
100 if (jar
== NULL
|| c
== NULL
)
103 /* find and remove from persistent jar */
104 cf
= soup_cookie_jar_all_cookies(p_cookiejar
);
106 for (;cf
; cf
= cf
->next
) {
108 if (soup_cookie_equal(ci
, c
)) {
109 _soup_cookie_jar_delete_cookie(p_cookiejar
, ci
);
114 soup_cookies_free(cf
);
116 /* delete from session jar */
117 _soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
121 soup_cookie_jar_add_cookie(SoupCookieJar
*jar
, SoupCookie
*cookie
)
123 struct domain
*d
= NULL
;
127 DNPRINTF(XT_D_COOKIE
, "soup_cookie_jar_add_cookie: %p %p %p\n",
128 jar
, p_cookiejar
, s_cookiejar
);
130 if (cookies_enabled
== 0)
133 /* see if we are up and running */
134 if (p_cookiejar
== NULL
) {
135 _soup_cookie_jar_add_cookie(jar
, cookie
);
138 /* disallow p_cookiejar adds, shouldn't happen */
139 if (jar
== p_cookiejar
)
143 if (jar
== NULL
|| cookie
== NULL
)
146 if (enable_cookie_whitelist
&&
147 (d
= wl_find(cookie
->domain
, &c_wl
)) == NULL
) {
149 DNPRINTF(XT_D_COOKIE
,
150 "soup_cookie_jar_add_cookie: reject %s\n",
152 if (save_rejected_cookies
) {
153 if ((r_cookie_f
= fopen(rc_fname
, "a+")) == NULL
) {
154 show_oops(NULL
, "can't open reject cookie file");
157 fseek(r_cookie_f
, 0, SEEK_END
);
158 fprintf(r_cookie_f
, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
159 cookie
->http_only
? "#HttpOnly_" : "",
161 *cookie
->domain
== '.' ? "TRUE" : "FALSE",
163 cookie
->secure
? "TRUE" : "FALSE",
165 (gulong
)soup_date_to_time_t(cookie
->expires
) :
172 if (!allow_volatile_cookies
)
176 if (cookie
->expires
== NULL
&& session_timeout
) {
177 soup_cookie_set_expires(cookie
,
178 soup_date_new_from_now(session_timeout
));
179 print_cookie("modified add cookie", cookie
);
182 /* see if we are white listed for persistence */
183 if ((d
&& d
->handy
) || (enable_cookie_whitelist
== 0)) {
184 /* add to persistent jar */
185 c
= soup_cookie_copy(cookie
);
186 print_cookie("soup_cookie_jar_add_cookie p_cookiejar", c
);
187 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
190 /* add to session jar */
191 print_cookie("soup_cookie_jar_add_cookie s_cookiejar", cookie
);
192 _soup_cookie_jar_add_cookie(s_cookiejar
, cookie
);
200 set_hook((void *)&_soup_cookie_jar_add_cookie
,
201 "soup_cookie_jar_add_cookie");
202 set_hook((void *)&_soup_cookie_jar_delete_cookie
,
203 "soup_cookie_jar_delete_cookie");
205 if (cookies_enabled
== 0)
209 * the following code is intricate due to overriding several libsoup
211 * do not alter order of these operations.
214 /* rejected cookies */
215 if (save_rejected_cookies
)
216 snprintf(rc_fname
, sizeof file
, "%s/%s", work_dir
,
219 /* persistent cookies */
220 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_COOKIE_FILE
);
221 p_cookiejar
= soup_cookie_jar_text_new(file
, read_only_cookies
);
223 /* session cookies */
224 s_cookiejar
= soup_cookie_jar_new();
225 g_object_set(G_OBJECT(s_cookiejar
), SOUP_COOKIE_JAR_ACCEPT_POLICY
,
226 cookie_policy
, (void *)NULL
);
229 soup_session_add_feature(session
, (SoupSessionFeature
*)s_cookiejar
);