1 const char parsers_rcs[] = "$Id: parsers.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', `free_http_request', `get_header',
14 * `list_to_text', `match', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 *********************************************************************/
51 #include <sys/types.h>
69 #include "jbsockets.h"
72 const char parsers_h_rcs[] = PARSERS_H_VERSION;
74 /* Fix a problem with Solaris. There should be no effect on other
76 * Solaris's isspace() is a macro which uses it's argument directly
77 * as an array index. Therefore we need to make sure that high-bit
78 * characters generate +ve values, and ideally we also want to make
79 * the argument match the declared parameter type of "int".
81 * Why did they write a character function that can't take a simple
82 * "char" argument? Doh!
84 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
85 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
88 const struct parsers client_patterns[] = {
89 { "referer:", 8, client_referrer },
90 { "user-agent:", 11, client_uagent },
91 { "ua-", 3, client_ua },
92 { "from:", 5, client_from },
93 { "cookie:", 7, client_send_cookie },
94 { "x-forwarded-for:", 16, client_x_forwarded },
95 { "proxy-connection:", 17, crumble },
97 { "Accept-Encoding: gzip", 21, crumble },
98 #endif /* def DENY_GZIP */
99 #if defined(DETECT_MSIE_IMAGES)
100 { "Accept:", 7, client_accept },
101 #endif /* defined(DETECT_MSIE_IMAGES) */
103 { "Host:", 5, client_host },
104 #endif /* def FORCE_LOAD */
105 /* { "if-modified-since:", 18, crumble }, */
109 const struct interceptors intercept_patterns[] = {
110 { "show-proxy-args", 14, show_proxy_args },
112 { "ij-untrusted-url", 14, ij_untrusted_url },
113 #endif /* def TRUST_FILES */
117 const struct parsers server_patterns[] = {
118 { "set-cookie:", 11, server_set_cookie },
119 { "connection:", 11, crumble },
121 { "Content-Type:", 13, content_type },
122 { "Content-Length:", 15, crumble },
123 #endif /* def PCRS */
128 void (* const add_client_headers[])(struct client_state *) = {
130 client_x_forwarded_adder,
136 void (* const add_server_headers[])(struct client_state *) = {
141 /*********************************************************************
145 * Description : Do a `strncmpic' on every pattern in pats.
148 * 1 : buf = a string to match to a list of patterns
149 * 2 : pats = list of strings to compare against buf.
151 * Returns : Return the matching "struct parsers *",
152 * or NULL if no pattern matches.
154 *********************************************************************/
155 static const struct parsers *match(char *buf, const struct parsers *pats)
157 const struct parsers *v;
162 log_error(LOG_LEVEL_ERROR, "NULL parameter to match()");
166 for (v = pats; v->str ; v++)
168 if (strncmpic(buf, v->str, v->len) == 0)
178 /*********************************************************************
180 * Function : flush_socket
182 * Description : Write any pending "buffered" content.
185 * 1 : fd = file descriptor of the socket to read
186 * 2 : csp = Current client state (buffers, headers, etc...)
188 * Returns : On success, the number of bytes written are returned (zero
189 * indicates nothing was written). On error, -1 is returned,
190 * and errno is set appropriately. If count is zero and the
191 * file descriptor refers to a regular file, 0 will be
192 * returned without causing any other effect. For a special
193 * file, the results are not portable.
195 *********************************************************************/
196 int flush_socket(int fd, struct client_state *csp)
198 struct iob *iob = csp->iob;
199 int n = iob->eod - iob->cur;
206 n = write_socket(fd, iob->cur, n);
207 iob->eod = iob->cur = iob->buf;
213 /*********************************************************************
215 * Function : add_to_iob
217 * Description : Add content to the buffered page.
220 * 1 : csp = Current client state (buffers, headers, etc...)
221 * 2 : buf = holds the content to be added to the page
222 * 3 : n = number of bytes to be added
224 * Returns : Number of bytes in the content buffer.
226 *********************************************************************/
227 int add_to_iob(struct client_state *csp, char *buf, int n)
229 struct iob *iob = csp->iob;
233 have = iob->eod - iob->cur;
242 if ((p = (char *)malloc(need + 1)) == NULL)
244 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
250 /* there is something in the buffer - save it */
251 memcpy(p, iob->cur, have);
253 /* replace the buffer with the new space */
257 /* point to the end of the data */
262 /* the buffer is empty, free it and reinitialize */
267 /* copy the new data into the iob buffer */
270 /* point to the end of the data */
273 /* null terminate == cheap insurance */
276 /* set the pointers to the new values */
285 /*********************************************************************
287 * Function : get_header
289 * Description : This (odd) routine will parse the csp->iob
292 * 1 : csp = Current client state (buffers, headers, etc...)
294 * Returns : Any one of the following:
296 * 1) a pointer to a dynamically allocated string that contains a header line
297 * 2) NULL indicating that the end of the header was reached
298 * 3) "" indicating that the end of the iob was reached before finding
299 * a complete header line.
301 *********************************************************************/
302 char *get_header(struct client_state *csp)
308 if ((iob->cur == NULL)
309 || ((p = strchr(iob->cur, '\n')) == NULL))
311 return(""); /* couldn't find a complete header */
316 ret = strdup(iob->cur);
320 if ((q = strchr(ret, '\r'))) *q = '\0';
322 /* is this a blank linke (i.e. the end of the header) ? */
334 /*********************************************************************
338 * Description : Append a string into a specified string list.
341 * 1 : h = pointer to list 'dummy' header
342 * 2 : s = string to add to the list
346 *********************************************************************/
347 void enlist(struct list *h, const char *s)
349 struct list *n = (struct list *)malloc(sizeof(*n));
372 /*********************************************************************
374 * Function : destroy_list
376 * Description : Destroy a string list (opposite of enlist)
379 * 1 : h = pointer to list 'dummy' header
383 *********************************************************************/
384 void destroy_list(struct list *h)
388 for (p = h->next; p ; p = n)
395 memset(h, '\0', sizeof(*h));
400 /*********************************************************************
402 * Function : list_to_text
404 * Description : "Flaten" a string list into 1 long \r\n delimited string.
407 * 1 : h = pointer to list 'dummy' header
409 * Returns : NULL on malloc error, else new long string.
411 *********************************************************************/
412 static char *list_to_text(struct list *h)
421 for (p = h->next; p ; p = p->next)
425 size += strlen(p->str) + 2;
429 if ((ret = (char *)malloc(size + 1)) == NULL)
438 for (p = h->next; p ; p = p->next)
444 *s++ = '\r'; *s++ = '\n';
453 /*********************************************************************
457 * Description : add, delete or modify lines in the HTTP header streams.
458 * On entry, it receives a linked list of headers space
459 * that was allocated dynamically (both the list nodes
460 * and the header contents).
462 * As a side effect it frees the space used by the original
466 * 1 : pats = list of patterns to match against headers
467 * 2 : more_headers = list of functions to add more
468 * headers (client or server)
469 * 3 : csp = Current client state (buffers, headers, etc...)
471 * Returns : Single pointer to a fully formed header.
473 *********************************************************************/
474 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
477 const struct parsers *v;
481 for (p = csp->headers->next; p ; p = p->next)
483 log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
485 if ((v = match(p->str, pats)))
487 hdr = v->parser(v, p->str, csp);
494 /* place any additional headers on the csp->headers list */
495 for (f = more_headers; *f ; f++)
500 /* add the blank line at the end of the header */
501 enlist(csp->headers, "");
503 hdr = list_to_text(csp->headers);
510 /*********************************************************************
512 * Function : free_http_request
514 * Description : Freez a http_request structure
517 * 1 : http = points to a http_request structure to free
521 *********************************************************************/
522 void free_http_request(struct http_request *http)
527 freez(http->hostport);
534 /*********************************************************************
536 * Function : parse_http_request
538 * Description : Parse out the host and port from the URL. Find the
539 * hostname & path, port (if ':'), and/or password (if '@')
542 * 1 : req = URL (or is it URI?) to break down
543 * 2 : http = pointer to the http structure to hold elements
544 * 3 : csp = Current client state (buffers, headers, etc...)
548 *********************************************************************/
549 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
551 char *buf, *v[10], *url, *p;
554 memset(http, '\0', sizeof(*http));
556 http->cmd = strdup(req);
560 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
564 /* this could be a CONNECT request */
565 if (strcmpic(v[0], "connect") == 0)
568 http->gpc = strdup(v[0]);
569 http->hostport = strdup(v[1]);
570 http->ver = strdup(v[2]);
575 /* This next line is a little ugly, but it simplifies the if statement below. */
576 /* Basically if using webDAV, we want the OR condition to use these too. */
580 * These are the headers as defined in RFC2518 to add webDAV support
583 #define OR_WEBDAV || \
584 (0 == strcmpic(v[0], "propfind")) || \
585 (0 == strcmpic(v[0], "proppatch")) || \
586 (0 == strcmpic(v[0], "move")) || \
587 (0 == strcmpic(v[0], "copy")) || \
588 (0 == strcmpic(v[0], "mkcol")) || \
589 (0 == strcmpic(v[0], "lock")) || \
590 (0 == strcmpic(v[0], "unlock"))
592 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
598 /* or it could be a GET or a POST (possibly webDAV too) */
599 if ((strcmpic(v[0], "get") == 0) ||
600 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
601 (strcmpic(v[0], "post") == 0))
604 http->gpc = strdup(v[0]);
606 http->ver = strdup(v[2]);
608 if (strncmpic(url, "http://", 7) == 0)
612 else if (strncmpic(url, "https://", 8) == 0)
621 if (url && (p = strchr(url, '/')))
623 http->path = strdup(p);
625 http->hostport = strdup(url);
633 if (http->hostport == NULL)
635 free_http_request(http);
639 buf = strdup(http->hostport);
642 /* check if url contains password */
643 n = ssplit(buf, "@", v, SZ(v), 1, 1);
646 char * newbuf = NULL;
647 newbuf = strdup(v[1]);
652 n = ssplit(buf, ":", v, SZ(v), 1, 1);
656 http->host = strdup(v[0]);
662 http->host = strdup(v[0]);
663 http->port = atoi(v[1]);
668 if (http->host == NULL)
670 free_http_request(http);
673 if (http->path == NULL)
675 http->path = strdup("");
681 /* here begins the family of parser functions that reformat header lines */
684 /*********************************************************************
688 * Description : This is called if a header matches a pattern to "crunch"
691 * 1 : v = Pointer to parsers structure, which basically holds
692 * headers (client or server) that we want to "crunch"
693 * 2 : s = header (from sed) to "crunch"
694 * 3 : csp = Current client state (buffers, headers, etc...)
696 * Returns : Always NULL.
698 *********************************************************************/
699 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
701 log_error(LOG_LEVEL_HEADER, "crunch!");
709 /*********************************************************************
711 * Function : content_type
713 * Description : Is this a text/* or javascript MIME Type?
717 * 2 : s = header string we are "considering"
718 * 3 : csp = Current client state (buffers, headers, etc...)
720 * Returns : A duplicate string pointer to this header (ie. pass thru)
722 *********************************************************************/
723 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
725 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
734 #endif /* def PCRS */
737 /*********************************************************************
739 * Function : client_referrer
741 * Description : Handle the "referer" config setting properly.
746 * 2 : s = header (from sed) to "crunch"
747 * 3 : csp = Current client state (buffers, headers, etc...)
749 * Returns : NULL if crunched, or a malloc'ed string with the original
752 *********************************************************************/
753 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
756 /* Since the referrer can include the prefix even
757 * even if the request itself is non-forced, we must
758 * clean it unconditionally
760 strclean(s, FORCE_PREFIX);
761 #endif /* def FORCE_LOAD */
763 csp->referrer = strdup(s);
765 if (referrer == NULL)
767 log_error(LOG_LEVEL_HEADER, "crunch!");
771 if (*referrer == '.')
776 if (*referrer == '@')
778 if (csp->send_user_cookie)
784 log_error(LOG_LEVEL_HEADER, "crunch!");
790 * New option §: Forge a referer as http://[hostname:port of REQUEST]/
791 * to fool stupid checks for in-site links
794 if (*referrer == '§')
796 if (csp->send_user_cookie)
802 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
803 s = strsav(NULL, "Referer: ");
804 s = strsav(s, "http://");
805 s = strsav(s, csp->http->hostport);
811 log_error(LOG_LEVEL_HEADER, "modified");
813 s = strsav( NULL, "Referer: " );
814 s = strsav( s, referrer );
820 /*********************************************************************
822 * Function : client_uagent
824 * Description : Handle the "user-agent" config setting properly.
829 * 2 : s = header (from sed) to "crunch"
830 * 3 : csp = Current client state (buffers, headers, etc...)
832 * Returns : A malloc'ed pointer to the default agent, or
833 * a malloc'ed string pointer to this header (ie. pass thru).
835 *********************************************************************/
836 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
838 #ifdef DETECT_MSIE_IMAGES
839 if (strstr (s, "MSIE "))
841 /* This is Microsoft Internet Explorer.
842 * Enable auto-detect.
844 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
846 #endif /* def DETECT_MSIE_IMAGES */
850 log_error(LOG_LEVEL_HEADER, "default");
851 return(strdup(DEFAULT_USER_AGENT));
861 if (csp->send_user_cookie)
867 log_error(LOG_LEVEL_HEADER, "default");
868 return(strdup(DEFAULT_USER_AGENT));
872 log_error(LOG_LEVEL_HEADER, "modified");
874 s = strsav( NULL, "User-Agent: " );
875 s = strsav( s, uagent );
881 /*********************************************************************
883 * Function : client_ua
885 * Description : Handle "ua-" headers properly. Called from `sed'.
889 * 2 : s = header (from sed) to "crunch"
890 * 3 : csp = Current client state (buffers, headers, etc...)
892 * Returns : NULL if crunched, or a malloc'ed string to original header
894 *********************************************************************/
895 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
899 log_error(LOG_LEVEL_HEADER, "crunch!");
910 if (csp->send_user_cookie)
916 log_error(LOG_LEVEL_HEADER, "crunch!");
921 log_error(LOG_LEVEL_HEADER, "crunch!");
927 /*********************************************************************
929 * Function : client_from
931 * Description : Handle the "from" config setting properly.
936 * 2 : s = header (from sed) to "crunch"
937 * 3 : csp = Current client state (buffers, headers, etc...)
939 * Returns : NULL if crunched, or a malloc'ed string to
940 * modified/original header.
942 *********************************************************************/
943 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
945 /* if not set, zap it */
948 log_error(LOG_LEVEL_HEADER, "crunch!");
957 log_error(LOG_LEVEL_HEADER, " modified");
959 s = strsav( NULL, "From: " );
960 s = strsav( s, from );
966 /*********************************************************************
968 * Function : client_send_cookie
970 * Description : Handle the "cookie" header properly. Called from `sed'.
971 * If cookie is accepted, add it to the cookie_list,
972 * else we crunch it. Mmmmmmmmmmm ... cookie ......
975 * 1 : v = pattern of cookie `sed' found matching
976 * 2 : s = header (from sed) to "crunch"
977 * 3 : csp = Current client state (buffers, headers, etc...)
979 * Returns : Always NULL.
981 *********************************************************************/
982 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
984 if (csp->send_user_cookie)
986 enlist(csp->cookie_list, s + v->len + 1);
990 log_error(LOG_LEVEL_HEADER, " crunch!");
994 * Always return NULL here. The cookie header
995 * will be sent at the end of the header.
1002 /*********************************************************************
1004 * Function : client_x_forwarded
1006 * Description : Handle the "x-forwarded-for" config setting properly,
1007 * also used in the add_client_headers list. Called from `sed'.
1011 * 2 : s = header (from sed) to "crunch"
1012 * 3 : csp = Current client state (buffers, headers, etc...)
1014 * Returns : Always NULL.
1016 *********************************************************************/
1017 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1021 csp->x_forwarded = strdup(s);
1025 * Always return NULL, since this information
1026 * will be sent at the end of the header.
1033 #if defined(DETECT_MSIE_IMAGES)
1034 /*********************************************************************
1036 * Function : client_accept
1038 * Description : Detect whether the client wants HTML or an image.
1039 * Clients do not always make this information available
1040 * in a sane way. Always passes the header through
1041 * the proxy unchanged.
1045 * 2 : s = Header string. Null terminated.
1046 * 3 : csp = Current client state (buffers, headers, etc...)
1048 * Returns : Duplicate of argument s.
1050 *********************************************************************/
1051 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1053 #ifdef DETECT_MSIE_IMAGES
1054 if (strstr (s, "image/gif"))
1056 /* Client will accept HTML. If this seems counterintuitive,
1059 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1063 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1065 #endif /* def DETECT_MSIE_IMAGES */
1070 #endif /* defined(DETECT_MSIE_IMAGES) */
1074 /* the following functions add headers directly to the header list */
1077 /*********************************************************************
1079 * Function : client_cookie_adder
1081 * Description : Used in the add_client_headers list. Called from `sed'.
1084 * 1 : csp = Current client state (buffers, headers, etc...)
1088 *********************************************************************/
1089 void client_cookie_adder(struct client_state *csp)
1095 for (l = csp->cookie_list->next; l ; l = l->next)
1099 tmp = strsav(tmp, "; ");
1101 tmp = strsav(tmp, l->str);
1104 for (l = wafer_list->next; l ; l = l->next)
1108 tmp = strsav(tmp, "; ");
1111 if ((e = cookie_encode(l->str)))
1113 tmp = strsav(tmp, e);
1122 ret = strdup("Cookie: ");
1123 ret = strsav(ret, tmp);
1124 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1125 enlist(csp->headers, ret);
1133 /*********************************************************************
1135 * Function : client_xtra_adder
1137 * Description : Used in the add_client_headers list. Called from `sed'.
1140 * 1 : csp = Current client state (buffers, headers, etc...)
1144 *********************************************************************/
1145 void client_xtra_adder(struct client_state *csp)
1149 for (l = xtra_list->next; l ; l = l->next)
1151 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1152 enlist(csp->headers, l->str);
1158 /*********************************************************************
1160 * Function : client_x_forwarded_adder
1162 * Description : Used in the add_client_headers list. Called from `sed'.
1165 * 1 : csp = Current client state (buffers, headers, etc...)
1169 *********************************************************************/
1170 void client_x_forwarded_adder(struct client_state *csp)
1174 if (add_forwarded == 0) return;
1176 if (csp->x_forwarded)
1178 p = strsav(p, csp->x_forwarded);
1179 p = strsav(p, ", ");
1180 p = strsav(p, csp->ip_addr_str);
1184 p = strsav(p, "X-Forwarded-For: ");
1185 p = strsav(p, csp->ip_addr_str);
1188 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1189 enlist(csp->headers, p);
1194 /*********************************************************************
1196 * Function : server_set_cookie
1198 * Description : Handle the server "cookie" header properly.
1199 * Log cookie to the jar file. Then "crunch" it,
1200 * or accept it. Called from `sed'.
1203 * 1 : v = parser pattern that matched this header
1204 * 2 : s = header that matched this pattern
1205 * 3 : csp = Current client state (buffers, headers, etc...)
1207 * Returns : `crumble' or a newly malloc'ed string.
1209 *********************************************************************/
1210 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1215 fprintf(jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1217 #endif /* def JAR_FILES */
1219 if (csp->accept_server_cookie == 0)
1221 return(crumble(v, s, csp));
1230 /*********************************************************************
1232 * Function : client_host
1234 * Description : Clean the FORCE_PREFIX out of the 'host' http
1235 * header, if we use force
1239 * 2 : s = header (from sed) to clean
1240 * 3 : csp = Current client state (buffers, headers, etc...)
1242 * Returns : A malloc'ed pointer to the cleaned host header
1244 *********************************************************************/
1245 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1247 char *cleanhost = strdup(s);
1250 strclean(cleanhost, FORCE_PREFIX);
1254 #endif /* def FORCE_LOAD */
1258 /*********************************************************************
1260 * Function : strclean
1262 * Description : In-Situ-Eliminate all occurances of substring in
1266 * 1 : string = string to clean
1267 * 2 : substring = substring to eliminate
1269 * Returns : Number of eliminations
1271 *********************************************************************/
1272 int strclean(const char *string, const char *substring)
1274 int hits = 0, len = strlen(substring);
1277 while((pos = strstr(string, substring)))
1284 while (*p++ != '\0');
1291 #endif /* def FORCE_LOAD */