1 const char parsers_rcs[] = "$Id: parsers.c,v 1.1.1.1 2001/05/15 13:59:01 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/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.
44 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
45 * Initial import of version 2.9.3 source tree
48 *********************************************************************/
54 #include <sys/types.h>
72 #include "jbsockets.h"
75 const char parsers_h_rcs[] = PARSERS_H_VERSION;
77 /* Fix a problem with Solaris. There should be no effect on other
79 * Solaris's isspace() is a macro which uses it's argument directly
80 * as an array index. Therefore we need to make sure that high-bit
81 * characters generate +ve values, and ideally we also want to make
82 * the argument match the declared parameter type of "int".
84 * Why did they write a character function that can't take a simple
85 * "char" argument? Doh!
87 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
88 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
91 const struct parsers client_patterns[] = {
92 { "referer:", 8, client_referrer },
93 { "user-agent:", 11, client_uagent },
94 { "ua-", 3, client_ua },
95 { "from:", 5, client_from },
96 { "cookie:", 7, client_send_cookie },
97 { "x-forwarded-for:", 16, client_x_forwarded },
98 { "proxy-connection:", 17, crumble },
100 { "Accept-Encoding: gzip", 21, crumble },
101 #endif /* def DENY_GZIP */
102 #if defined(DETECT_MSIE_IMAGES)
103 { "Accept:", 7, client_accept },
104 #endif /* defined(DETECT_MSIE_IMAGES) */
106 { "Host:", 5, client_host },
107 #endif /* def FORCE_LOAD */
108 /* { "if-modified-since:", 18, crumble }, */
112 const struct interceptors intercept_patterns[] = {
113 { "show-proxy-args", 14, show_proxy_args },
115 { "ij-untrusted-url", 14, ij_untrusted_url },
116 #endif /* def TRUST_FILES */
120 const struct parsers server_patterns[] = {
121 { "set-cookie:", 11, server_set_cookie },
122 { "connection:", 11, crumble },
124 { "Content-Type:", 13, content_type },
125 { "Content-Length:", 15, crumble },
126 #endif /* def PCRS */
131 void (* const add_client_headers[])(struct client_state *) = {
133 client_x_forwarded_adder,
139 void (* const add_server_headers[])(struct client_state *) = {
144 /*********************************************************************
148 * Description : Do a `strncmpic' on every pattern in pats.
151 * 1 : buf = a string to match to a list of patterns
152 * 2 : pats = list of strings to compare against buf.
154 * Returns : Return the matching "struct parsers *",
155 * or NULL if no pattern matches.
157 *********************************************************************/
158 static const struct parsers *match(char *buf, const struct parsers *pats)
160 const struct parsers *v;
165 log_error(LOG_LEVEL_ERROR, "NULL parameter to match()");
169 for (v = pats; v->str ; v++)
171 if (strncmpic(buf, v->str, v->len) == 0)
181 /*********************************************************************
183 * Function : flush_socket
185 * Description : Write any pending "buffered" content.
188 * 1 : fd = file descriptor of the socket to read
189 * 2 : csp = Current client state (buffers, headers, etc...)
191 * Returns : On success, the number of bytes written are returned (zero
192 * indicates nothing was written). On error, -1 is returned,
193 * and errno is set appropriately. If count is zero and the
194 * file descriptor refers to a regular file, 0 will be
195 * returned without causing any other effect. For a special
196 * file, the results are not portable.
198 *********************************************************************/
199 int flush_socket(int fd, struct client_state *csp)
201 struct iob *iob = csp->iob;
202 int n = iob->eod - iob->cur;
209 n = write_socket(fd, iob->cur, n);
210 iob->eod = iob->cur = iob->buf;
216 /*********************************************************************
218 * Function : add_to_iob
220 * Description : Add content to the buffered page.
223 * 1 : csp = Current client state (buffers, headers, etc...)
224 * 2 : buf = holds the content to be added to the page
225 * 3 : n = number of bytes to be added
227 * Returns : Number of bytes in the content buffer.
229 *********************************************************************/
230 int add_to_iob(struct client_state *csp, char *buf, int n)
232 struct iob *iob = csp->iob;
236 have = iob->eod - iob->cur;
245 if ((p = (char *)malloc(need + 1)) == NULL)
247 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
253 /* there is something in the buffer - save it */
254 memcpy(p, iob->cur, have);
256 /* replace the buffer with the new space */
260 /* point to the end of the data */
265 /* the buffer is empty, free it and reinitialize */
270 /* copy the new data into the iob buffer */
273 /* point to the end of the data */
276 /* null terminate == cheap insurance */
279 /* set the pointers to the new values */
288 /*********************************************************************
290 * Function : get_header
292 * Description : This (odd) routine will parse the csp->iob
295 * 1 : csp = Current client state (buffers, headers, etc...)
297 * Returns : Any one of the following:
299 * 1) a pointer to a dynamically allocated string that contains a header line
300 * 2) NULL indicating that the end of the header was reached
301 * 3) "" indicating that the end of the iob was reached before finding
302 * a complete header line.
304 *********************************************************************/
305 char *get_header(struct client_state *csp)
311 if ((iob->cur == NULL)
312 || ((p = strchr(iob->cur, '\n')) == NULL))
314 return(""); /* couldn't find a complete header */
319 ret = strdup(iob->cur);
323 if ((q = strchr(ret, '\r'))) *q = '\0';
325 /* is this a blank linke (i.e. the end of the header) ? */
337 /*********************************************************************
341 * Description : Append a string into a specified string list.
344 * 1 : h = pointer to list 'dummy' header
345 * 2 : s = string to add to the list
349 *********************************************************************/
350 void enlist(struct list *h, const char *s)
352 struct list *n = (struct list *)malloc(sizeof(*n));
375 /*********************************************************************
377 * Function : destroy_list
379 * Description : Destroy a string list (opposite of enlist)
382 * 1 : h = pointer to list 'dummy' header
386 *********************************************************************/
387 void destroy_list(struct list *h)
391 for (p = h->next; p ; p = n)
398 memset(h, '\0', sizeof(*h));
403 /*********************************************************************
405 * Function : list_to_text
407 * Description : "Flaten" a string list into 1 long \r\n delimited string.
410 * 1 : h = pointer to list 'dummy' header
412 * Returns : NULL on malloc error, else new long string.
414 *********************************************************************/
415 static char *list_to_text(struct list *h)
424 for (p = h->next; p ; p = p->next)
428 size += strlen(p->str) + 2;
432 if ((ret = (char *)malloc(size + 1)) == NULL)
441 for (p = h->next; p ; p = p->next)
447 *s++ = '\r'; *s++ = '\n';
456 /*********************************************************************
460 * Description : add, delete or modify lines in the HTTP header streams.
461 * On entry, it receives a linked list of headers space
462 * that was allocated dynamically (both the list nodes
463 * and the header contents).
465 * As a side effect it frees the space used by the original
469 * 1 : pats = list of patterns to match against headers
470 * 2 : more_headers = list of functions to add more
471 * headers (client or server)
472 * 3 : csp = Current client state (buffers, headers, etc...)
474 * Returns : Single pointer to a fully formed header.
476 *********************************************************************/
477 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
480 const struct parsers *v;
484 for (p = csp->headers->next; p ; p = p->next)
486 log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
488 if ((v = match(p->str, pats)))
490 hdr = v->parser(v, p->str, csp);
497 /* place any additional headers on the csp->headers list */
498 for (f = more_headers; *f ; f++)
503 /* add the blank line at the end of the header */
504 enlist(csp->headers, "");
506 hdr = list_to_text(csp->headers);
513 /*********************************************************************
515 * Function : free_http_request
517 * Description : Freez a http_request structure
520 * 1 : http = points to a http_request structure to free
524 *********************************************************************/
525 void free_http_request(struct http_request *http)
530 freez(http->hostport);
537 /*********************************************************************
539 * Function : parse_http_request
541 * Description : Parse out the host and port from the URL. Find the
542 * hostname & path, port (if ':'), and/or password (if '@')
545 * 1 : req = URL (or is it URI?) to break down
546 * 2 : http = pointer to the http structure to hold elements
547 * 3 : csp = Current client state (buffers, headers, etc...)
551 *********************************************************************/
552 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
554 char *buf, *v[10], *url, *p;
557 memset(http, '\0', sizeof(*http));
559 http->cmd = strdup(req);
563 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
567 /* this could be a CONNECT request */
568 if (strcmpic(v[0], "connect") == 0)
571 http->gpc = strdup(v[0]);
572 http->hostport = strdup(v[1]);
573 http->ver = strdup(v[2]);
578 /* This next line is a little ugly, but it simplifies the if statement below. */
579 /* Basically if using webDAV, we want the OR condition to use these too. */
583 * These are the headers as defined in RFC2518 to add webDAV support
586 #define OR_WEBDAV || \
587 (0 == strcmpic(v[0], "propfind")) || \
588 (0 == strcmpic(v[0], "proppatch")) || \
589 (0 == strcmpic(v[0], "move")) || \
590 (0 == strcmpic(v[0], "copy")) || \
591 (0 == strcmpic(v[0], "mkcol")) || \
592 (0 == strcmpic(v[0], "lock")) || \
593 (0 == strcmpic(v[0], "unlock"))
595 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
601 /* or it could be a GET or a POST (possibly webDAV too) */
602 if ((strcmpic(v[0], "get") == 0) ||
603 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
604 (strcmpic(v[0], "post") == 0))
607 http->gpc = strdup(v[0]);
609 http->ver = strdup(v[2]);
611 if (strncmpic(url, "http://", 7) == 0)
615 else if (strncmpic(url, "https://", 8) == 0)
624 if (url && (p = strchr(url, '/')))
626 http->path = strdup(p);
628 http->hostport = strdup(url);
636 if (http->hostport == NULL)
638 free_http_request(http);
642 buf = strdup(http->hostport);
645 /* check if url contains password */
646 n = ssplit(buf, "@", v, SZ(v), 1, 1);
649 char * newbuf = NULL;
650 newbuf = strdup(v[1]);
655 n = ssplit(buf, ":", v, SZ(v), 1, 1);
659 http->host = strdup(v[0]);
665 http->host = strdup(v[0]);
666 http->port = atoi(v[1]);
671 if (http->host == NULL)
673 free_http_request(http);
676 if (http->path == NULL)
678 http->path = strdup("");
684 /* here begins the family of parser functions that reformat header lines */
687 /*********************************************************************
691 * Description : This is called if a header matches a pattern to "crunch"
694 * 1 : v = Pointer to parsers structure, which basically holds
695 * headers (client or server) that we want to "crunch"
696 * 2 : s = header (from sed) to "crunch"
697 * 3 : csp = Current client state (buffers, headers, etc...)
699 * Returns : Always NULL.
701 *********************************************************************/
702 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
704 log_error(LOG_LEVEL_HEADER, "crunch!");
712 /*********************************************************************
714 * Function : content_type
716 * Description : Is this a text/* or javascript MIME Type?
720 * 2 : s = header string we are "considering"
721 * 3 : csp = Current client state (buffers, headers, etc...)
723 * Returns : A duplicate string pointer to this header (ie. pass thru)
725 *********************************************************************/
726 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
728 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
737 #endif /* def PCRS */
740 /*********************************************************************
742 * Function : client_referrer
744 * Description : Handle the "referer" config setting properly.
749 * 2 : s = header (from sed) to "crunch"
750 * 3 : csp = Current client state (buffers, headers, etc...)
752 * Returns : NULL if crunched, or a malloc'ed string with the original
755 *********************************************************************/
756 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
759 /* Since the referrer can include the prefix even
760 * even if the request itself is non-forced, we must
761 * clean it unconditionally
763 strclean(s, FORCE_PREFIX);
764 #endif /* def FORCE_LOAD */
766 csp->referrer = strdup(s);
768 if (referrer == NULL)
770 log_error(LOG_LEVEL_HEADER, "crunch!");
774 if (*referrer == '.')
779 if (*referrer == '@')
781 if (csp->send_user_cookie)
787 log_error(LOG_LEVEL_HEADER, "crunch!");
793 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
794 * to fool stupid checks for in-site links
797 if (*referrer == 'ยง' || *referrer == 'L')
799 if (csp->send_user_cookie)
805 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
806 s = strsav(NULL, "Referer: ");
807 s = strsav(s, "http://");
808 s = strsav(s, csp->http->hostport);
814 log_error(LOG_LEVEL_HEADER, "modified");
816 s = strsav( NULL, "Referer: " );
817 s = strsav( s, referrer );
823 /*********************************************************************
825 * Function : client_uagent
827 * Description : Handle the "user-agent" config setting properly.
832 * 2 : s = header (from sed) to "crunch"
833 * 3 : csp = Current client state (buffers, headers, etc...)
835 * Returns : A malloc'ed pointer to the default agent, or
836 * a malloc'ed string pointer to this header (ie. pass thru).
838 *********************************************************************/
839 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
841 #ifdef DETECT_MSIE_IMAGES
842 if (strstr (s, "MSIE "))
844 /* This is Microsoft Internet Explorer.
845 * Enable auto-detect.
847 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
849 #endif /* def DETECT_MSIE_IMAGES */
853 log_error(LOG_LEVEL_HEADER, "default");
854 return(strdup(DEFAULT_USER_AGENT));
864 if (csp->send_user_cookie)
870 log_error(LOG_LEVEL_HEADER, "default");
871 return(strdup(DEFAULT_USER_AGENT));
875 log_error(LOG_LEVEL_HEADER, "modified");
877 s = strsav( NULL, "User-Agent: " );
878 s = strsav( s, uagent );
884 /*********************************************************************
886 * Function : client_ua
888 * Description : Handle "ua-" headers properly. Called from `sed'.
892 * 2 : s = header (from sed) to "crunch"
893 * 3 : csp = Current client state (buffers, headers, etc...)
895 * Returns : NULL if crunched, or a malloc'ed string to original header
897 *********************************************************************/
898 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
902 log_error(LOG_LEVEL_HEADER, "crunch!");
913 if (csp->send_user_cookie)
919 log_error(LOG_LEVEL_HEADER, "crunch!");
924 log_error(LOG_LEVEL_HEADER, "crunch!");
930 /*********************************************************************
932 * Function : client_from
934 * Description : Handle the "from" config setting properly.
939 * 2 : s = header (from sed) to "crunch"
940 * 3 : csp = Current client state (buffers, headers, etc...)
942 * Returns : NULL if crunched, or a malloc'ed string to
943 * modified/original header.
945 *********************************************************************/
946 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
948 /* if not set, zap it */
951 log_error(LOG_LEVEL_HEADER, "crunch!");
960 log_error(LOG_LEVEL_HEADER, " modified");
962 s = strsav( NULL, "From: " );
963 s = strsav( s, from );
969 /*********************************************************************
971 * Function : client_send_cookie
973 * Description : Handle the "cookie" header properly. Called from `sed'.
974 * If cookie is accepted, add it to the cookie_list,
975 * else we crunch it. Mmmmmmmmmmm ... cookie ......
978 * 1 : v = pattern of cookie `sed' found matching
979 * 2 : s = header (from sed) to "crunch"
980 * 3 : csp = Current client state (buffers, headers, etc...)
982 * Returns : Always NULL.
984 *********************************************************************/
985 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
987 if (csp->send_user_cookie)
989 enlist(csp->cookie_list, s + v->len + 1);
993 log_error(LOG_LEVEL_HEADER, " crunch!");
997 * Always return NULL here. The cookie header
998 * will be sent at the end of the header.
1005 /*********************************************************************
1007 * Function : client_x_forwarded
1009 * Description : Handle the "x-forwarded-for" config setting properly,
1010 * also used in the add_client_headers list. Called from `sed'.
1014 * 2 : s = header (from sed) to "crunch"
1015 * 3 : csp = Current client state (buffers, headers, etc...)
1017 * Returns : Always NULL.
1019 *********************************************************************/
1020 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1024 csp->x_forwarded = strdup(s);
1028 * Always return NULL, since this information
1029 * will be sent at the end of the header.
1036 #if defined(DETECT_MSIE_IMAGES)
1037 /*********************************************************************
1039 * Function : client_accept
1041 * Description : Detect whether the client wants HTML or an image.
1042 * Clients do not always make this information available
1043 * in a sane way. Always passes the header through
1044 * the proxy unchanged.
1048 * 2 : s = Header string. Null terminated.
1049 * 3 : csp = Current client state (buffers, headers, etc...)
1051 * Returns : Duplicate of argument s.
1053 *********************************************************************/
1054 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1056 #ifdef DETECT_MSIE_IMAGES
1057 if (strstr (s, "image/gif"))
1059 /* Client will accept HTML. If this seems counterintuitive,
1062 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1066 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1068 #endif /* def DETECT_MSIE_IMAGES */
1073 #endif /* defined(DETECT_MSIE_IMAGES) */
1077 /* the following functions add headers directly to the header list */
1080 /*********************************************************************
1082 * Function : client_cookie_adder
1084 * Description : Used in the add_client_headers list. Called from `sed'.
1087 * 1 : csp = Current client state (buffers, headers, etc...)
1091 *********************************************************************/
1092 void client_cookie_adder(struct client_state *csp)
1098 for (l = csp->cookie_list->next; l ; l = l->next)
1102 tmp = strsav(tmp, "; ");
1104 tmp = strsav(tmp, l->str);
1107 for (l = wafer_list->next; l ; l = l->next)
1111 tmp = strsav(tmp, "; ");
1114 if ((e = cookie_encode(l->str)))
1116 tmp = strsav(tmp, e);
1125 ret = strdup("Cookie: ");
1126 ret = strsav(ret, tmp);
1127 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1128 enlist(csp->headers, ret);
1136 /*********************************************************************
1138 * Function : client_xtra_adder
1140 * Description : Used in the add_client_headers list. Called from `sed'.
1143 * 1 : csp = Current client state (buffers, headers, etc...)
1147 *********************************************************************/
1148 void client_xtra_adder(struct client_state *csp)
1152 for (l = xtra_list->next; l ; l = l->next)
1154 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1155 enlist(csp->headers, l->str);
1161 /*********************************************************************
1163 * Function : client_x_forwarded_adder
1165 * Description : Used in the add_client_headers list. Called from `sed'.
1168 * 1 : csp = Current client state (buffers, headers, etc...)
1172 *********************************************************************/
1173 void client_x_forwarded_adder(struct client_state *csp)
1177 if (add_forwarded == 0) return;
1179 if (csp->x_forwarded)
1181 p = strsav(p, csp->x_forwarded);
1182 p = strsav(p, ", ");
1183 p = strsav(p, csp->ip_addr_str);
1187 p = strsav(p, "X-Forwarded-For: ");
1188 p = strsav(p, csp->ip_addr_str);
1191 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1192 enlist(csp->headers, p);
1197 /*********************************************************************
1199 * Function : server_set_cookie
1201 * Description : Handle the server "cookie" header properly.
1202 * Log cookie to the jar file. Then "crunch" it,
1203 * or accept it. Called from `sed'.
1206 * 1 : v = parser pattern that matched this header
1207 * 2 : s = header that matched this pattern
1208 * 3 : csp = Current client state (buffers, headers, etc...)
1210 * Returns : `crumble' or a newly malloc'ed string.
1212 *********************************************************************/
1213 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1218 fprintf(jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1220 #endif /* def JAR_FILES */
1222 if (csp->accept_server_cookie == 0)
1224 return(crumble(v, s, csp));
1233 /*********************************************************************
1235 * Function : client_host
1237 * Description : Clean the FORCE_PREFIX out of the 'host' http
1238 * header, if we use force
1242 * 2 : s = header (from sed) to clean
1243 * 3 : csp = Current client state (buffers, headers, etc...)
1245 * Returns : A malloc'ed pointer to the cleaned host header
1247 *********************************************************************/
1248 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1250 char *cleanhost = strdup(s);
1253 strclean(cleanhost, FORCE_PREFIX);
1257 #endif /* def FORCE_LOAD */
1261 /*********************************************************************
1263 * Function : strclean
1265 * Description : In-Situ-Eliminate all occurances of substring in
1269 * 1 : string = string to clean
1270 * 2 : substring = substring to eliminate
1272 * Returns : Number of eliminations
1274 *********************************************************************/
1275 int strclean(const char *string, const char *substring)
1277 int hits = 0, len = strlen(substring);
1280 while((pos = strstr(string, substring)))
1287 while (*p++ != '\0');
1294 #endif /* def FORCE_LOAD */