project.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. #ifndef PROJECT_H_INCLUDED
  2. #define PROJECT_H_INCLUDED
  3. /*********************************************************************
  4. *
  5. * File : $Source: /cvsroot/ijbswa/current/project.h,v $
  6. *
  7. * Purpose : Defines data structures which are widely used in the
  8. * project. Does not define any variables or functions
  9. * (though it does declare some macros).
  10. *
  11. * Copyright : Written by and Copyright (C) 2001-2014 the
  12. * Privoxy team. https://www.privoxy.org/
  13. *
  14. * Based on the Internet Junkbuster originally written
  15. * by and Copyright (C) 1997 Anonymous Coders and
  16. * Junkbusters Corporation. http://www.junkbusters.com
  17. *
  18. * This program is free software; you can redistribute it
  19. * and/or modify it under the terms of the GNU General
  20. * Public License as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at
  22. * your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will
  25. * be useful, but WITHOUT ANY WARRANTY; without even the
  26. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  27. * PARTICULAR PURPOSE. See the GNU General Public
  28. * License for more details.
  29. *
  30. * The GNU General Public License should be included with
  31. * this file. If not, you can view it at
  32. * http://www.gnu.org/copyleft/gpl.html
  33. * or write to the Free Software Foundation, Inc., 59
  34. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  35. *
  36. *********************************************************************/
  37. /* Declare struct FILE for vars and funcs. */
  38. #include <stdio.h>
  39. /* Need time_t for file_list */
  40. #include <time.h>
  41. /* Needed for pcre choice */
  42. #include "config.h"
  43. #ifdef FEATURE_HTTPS_INSPECTION
  44. /*
  45. * Macros for SSL structures
  46. */
  47. #define CERT_INFO_BUF_SIZE 4096
  48. #define CERT_FILE_BUF_SIZE 16384
  49. #define ISSUER_NAME_BUF_SIZE 2048
  50. #define HASH_OF_HOST_BUF_SIZE 16
  51. #endif /* FEATURE_HTTPS_INSPECTION */
  52. #ifdef FEATURE_PTHREAD
  53. # include <pthread.h>
  54. typedef pthread_mutex_t privoxy_mutex_t;
  55. #else
  56. # ifdef _WIN32
  57. # include <windows.h>
  58. # endif
  59. typedef CRITICAL_SECTION privoxy_mutex_t;
  60. #endif
  61. #ifdef FEATURE_HTTPS_INSPECTION_MBEDTLS
  62. #include "mbedtls/net_sockets.h"
  63. #include "mbedtls/entropy.h"
  64. #include "mbedtls/ctr_drbg.h"
  65. #if defined(MBEDTLS_SSL_CACHE_C)
  66. #include "mbedtls/ssl_cache.h"
  67. #endif
  68. #endif /* FEATURE_HTTPS_INSPECTION_MBEDTLS */
  69. #ifdef FEATURE_HTTPS_INSPECTION_OPENSSL
  70. #ifdef _WIN32
  71. #include <wincrypt.h>
  72. #undef X509_NAME
  73. #undef X509_EXTENSIONS
  74. #endif
  75. #include <openssl/ssl.h>
  76. #include <openssl/bio.h>
  77. #include <openssl/err.h>
  78. #endif /* FEATURE_HTTPS_INSPECTION_OPENSSL */
  79. /* Need for struct sockaddr_storage */
  80. #ifdef HAVE_RFC2553
  81. # ifndef _WIN32
  82. # include <netdb.h>
  83. # include <sys/socket.h>
  84. # else
  85. # include <stdint.h>
  86. # include <ws2tcpip.h>
  87. typedef unsigned short in_port_t;
  88. # endif
  89. #endif
  90. /*
  91. * Include appropriate regular expression libraries.
  92. * Note that pcrs and pcre (native) are needed for cgi
  93. * and are included anyway.
  94. */
  95. #ifdef STATIC_PCRE
  96. # include "pcre.h"
  97. #else
  98. # ifdef PCRE_H_IN_SUBDIR
  99. # include <pcre/pcre.h>
  100. # else
  101. # include <pcre.h>
  102. # endif
  103. #endif
  104. #ifdef STATIC_PCRS
  105. # include "pcrs.h"
  106. #else
  107. # include <pcrs.h>
  108. #endif
  109. #ifdef STATIC_PCRE
  110. # include "pcreposix.h"
  111. #else
  112. # ifdef PCRE_H_IN_SUBDIR
  113. # include <pcre/pcreposix.h>
  114. # else
  115. # include <pcreposix.h>
  116. # endif
  117. #endif
  118. #ifdef _WIN32
  119. /*
  120. * I don't want to have to #include all this just for the declaration
  121. * of SOCKET. However, it looks like we have to...
  122. */
  123. #ifndef STRICT
  124. #define STRICT
  125. #endif
  126. #include <windows.h>
  127. #endif
  128. #ifdef _WIN32
  129. typedef SOCKET jb_socket;
  130. #define JB_INVALID_SOCKET INVALID_SOCKET
  131. #else /* ndef _WIN32 */
  132. /**
  133. * The type used by sockets. On UNIX it's an int. Microsoft decided to
  134. * make it an unsigned.
  135. */
  136. typedef int jb_socket;
  137. /**
  138. * The error value used for variables of type jb_socket. On UNIX this
  139. * is -1, however Microsoft decided to make socket handles unsigned, so
  140. * they use a different value.
  141. */
  142. #define JB_INVALID_SOCKET (-1)
  143. #endif /* ndef _WIN32 */
  144. /**
  145. * A standard error code. This should be JB_ERR_OK or one of the JB_ERR_xxx
  146. * series of errors.
  147. */
  148. enum privoxy_err
  149. {
  150. JB_ERR_OK = 0, /**< Success, no error */
  151. JB_ERR_MEMORY = 1, /**< Out of memory */
  152. JB_ERR_CGI_PARAMS = 2, /**< Missing or corrupt CGI parameters */
  153. JB_ERR_FILE = 3, /**< Error opening, reading or writing a file */
  154. JB_ERR_PARSE = 4, /**< Error parsing file */
  155. JB_ERR_MODIFIED = 5, /**< File has been modified outside of the
  156. CGI actions editor. */
  157. JB_ERR_COMPRESS = 6 /**< Error on decompression */
  158. };
  159. typedef enum privoxy_err jb_err;
  160. /**
  161. * This macro is used to free a pointer that may be NULL.
  162. * It also sets the variable to NULL after it's been freed.
  163. * The parameter should be a simple variable without side effects.
  164. */
  165. #define freez(X) { if(X) { free((void*)X); X = NULL ; } }
  166. /**
  167. * Macro definitions for platforms where isspace() and friends
  168. * are macros that use their argument directly as an array index
  169. * and thus better be positive. Supposedly that's the case on
  170. * some unspecified Solaris versions.
  171. * Note: Remember to #include <ctype.h> if you use these macros.
  172. */
  173. #define privoxy_isdigit(__X) isdigit((int)(unsigned char)(__X))
  174. #define privoxy_isupper(__X) isupper((int)(unsigned char)(__X))
  175. #define privoxy_toupper(__X) toupper((int)(unsigned char)(__X))
  176. #define privoxy_tolower(__X) tolower((int)(unsigned char)(__X))
  177. #define privoxy_isspace(__X) isspace((int)(unsigned char)(__X))
  178. /**
  179. * Use for statically allocated buffers if you have no other choice.
  180. * Remember to check the length of what you write into the buffer
  181. * - we don't want any buffer overflows!
  182. */
  183. #define BUFFER_SIZE 5000
  184. /**
  185. * Max length of CGI parameters (arbitrary limit).
  186. */
  187. #define CGI_PARAM_LEN_MAX 500U
  188. /**
  189. * Buffer size for capturing struct hostent data in the
  190. * gethostby(name|addr)_r library calls. Since we don't
  191. * loop over gethostbyname_r, the buffer must be sufficient
  192. * to accommodate multiple IN A RRs, as used in DNS round robin
  193. * load balancing. W3C's wwwlib uses 1K, so that should be
  194. * good enough for us, too.
  195. */
  196. /**
  197. * XXX: Temporary doubled, for some configurations
  198. * 1K is still too small and we didn't get the
  199. * real fix ready for inclusion.
  200. */
  201. #define HOSTENT_BUFFER_SIZE 2048
  202. /**
  203. * Default TCP/IP address to listen on, as a string.
  204. * Set to "127.0.0.1:8118".
  205. */
  206. #define HADDR_DEFAULT "127.0.0.1:8118"
  207. /* Forward def for struct client_state */
  208. struct configuration_spec;
  209. /**
  210. * Entry in a linked list of strings.
  211. */
  212. struct list_entry
  213. {
  214. /**
  215. * The string pointer. It must point to a dynamically malloc()ed
  216. * string or be NULL for the list functions to work. In the latter
  217. * case, just be careful next time you iterate through the list in
  218. * your own code.
  219. */
  220. char *str;
  221. /** Next entry in the linked list, or NULL if no more. */
  222. struct list_entry *next;
  223. };
  224. /**
  225. * A header for a linked list of strings.
  226. */
  227. struct list
  228. {
  229. /** First entry in the list, or NULL if the list is empty. */
  230. struct list_entry *first;
  231. /** Last entry in the list, or NULL if the list is empty. */
  232. struct list_entry *last;
  233. };
  234. /**
  235. * An entry in a map. This is a name=value pair.
  236. */
  237. struct map_entry
  238. {
  239. /** The key for the map. */
  240. const char *name;
  241. /** The value associated with that key. */
  242. const char *value;
  243. /** The next map entry, or NULL if none. */
  244. struct map_entry *next;
  245. };
  246. /**
  247. * A map from a string to another string.
  248. * This is used for the parameters passed in a HTTP GET request, and
  249. * to store the exports when the CGI interface is filling in a template.
  250. */
  251. struct map
  252. {
  253. /** The first map entry, or NULL if the map is empty. */
  254. struct map_entry *first;
  255. /** The last map entry, or NULL if the map is empty. */
  256. struct map_entry *last;
  257. };
  258. #ifdef FEATURE_HTTPS_INSPECTION_MBEDTLS
  259. /*
  260. * Struct of attributes necessary for TLS/SSL connection
  261. */
  262. typedef struct {
  263. mbedtls_ssl_context ssl;
  264. mbedtls_ssl_config conf;
  265. mbedtls_net_context socket_fd;
  266. mbedtls_x509_crt server_cert;
  267. mbedtls_x509_crt ca_cert;
  268. mbedtls_pk_context prim_key;
  269. int *ciphersuites_list;
  270. #if defined(MBEDTLS_SSL_CACHE_C)
  271. mbedtls_ssl_cache_context cache;
  272. #endif
  273. } mbedtls_connection_attr;
  274. #endif /* FEATURE_HTTPS_INSPECTION_MBEDTLS */
  275. #ifdef FEATURE_HTTPS_INSPECTION_OPENSSL
  276. /*
  277. * Struct of attributes necessary for TLS/SSL connection
  278. */
  279. typedef struct {
  280. SSL_CTX *ctx;
  281. BIO *bio;
  282. } openssl_connection_attr;
  283. #endif /* FEATURE_HTTPS_INSPECTION_OPENSSL */
  284. /**
  285. * A HTTP request. This includes the method (GET, POST) and
  286. * the parsed URL.
  287. *
  288. * This is also used whenever we want to match a URL against a
  289. * URL pattern. This always contains the URL to match, and never
  290. * a URL pattern. (See struct url_spec).
  291. */
  292. struct http_request
  293. {
  294. char *cmd; /**< Whole command line: method, URL, Version */
  295. char *ocmd; /**< Backup of original cmd for CLF logging */
  296. char *gpc; /**< HTTP method: GET, POST, ... */
  297. char *url; /**< The URL */
  298. char *version; /**< Protocol version */
  299. int status; /**< HTTP Status */
  300. char *host; /**< Host part of URL */
  301. int port; /**< Port of URL or 80 (default) */
  302. char *path; /**< Path of URL */
  303. char *hostport; /**< host[:port] */
  304. int ssl; /**< Flag if protocol is https */
  305. char *host_ip_addr_str; /**< String with dotted decimal representation
  306. of host's IP. NULL before connect_to() */
  307. char *dbuffer; /**< Buffer with '\0'-delimited domain name. */
  308. char **dvec; /**< List of pointers to the strings in dbuffer. */
  309. int dcount; /**< How many parts to this domain? (length of dvec) */
  310. #ifdef FEATURE_HTTPS_INSPECTION
  311. int client_ssl; /**< Flag if we should communicate with client over ssl */
  312. int server_ssl; /**< Flag if we should communicate with server over ssl */
  313. unsigned char hash_of_host_hex[(HASH_OF_HOST_BUF_SIZE * 2) + 1]; /**< chars for hash in hex string and one for '\0' */
  314. unsigned char hash_of_host[HASH_OF_HOST_BUF_SIZE+1]; /**< chars for bytes of hash and one for '\0' */
  315. #endif
  316. };
  317. #ifdef FEATURE_HTTPS_INSPECTION
  318. /*
  319. * Struct for linked list containing certificates
  320. */
  321. typedef struct certs_chain {
  322. char info_buf[CERT_INFO_BUF_SIZE]; /* text info about properties of certificate */
  323. char file_buf[CERT_FILE_BUF_SIZE]; /* buffer for whole certificate - format to save in file */
  324. struct certs_chain *next; /* next certificate in chain of trust */
  325. } certs_chain_t;
  326. #endif
  327. /**
  328. * Reasons for generating a http_response instead of delivering
  329. * the requested resource. Mostly ordered the way they are checked
  330. * for in chat().
  331. */
  332. enum crunch_reason
  333. {
  334. UNSUPPORTED,
  335. BLOCKED,
  336. UNTRUSTED,
  337. REDIRECTED,
  338. CGI_CALL,
  339. NO_SUCH_DOMAIN,
  340. FORWARDING_FAILED,
  341. CONNECT_FAILED,
  342. OUT_OF_MEMORY,
  343. INTERNAL_ERROR,
  344. CONNECTION_TIMEOUT,
  345. NO_SERVER_DATA
  346. };
  347. /**
  348. * Response generated by CGI, blocker, or error handler
  349. */
  350. struct http_response
  351. {
  352. char *status; /**< HTTP status (string). */
  353. struct list headers[1]; /**< List of header lines. */
  354. char *head; /**< Formatted http response head. */
  355. size_t head_length; /**< Length of http response head. */
  356. char *body; /**< HTTP document body. */
  357. size_t content_length; /**< Length of body, REQUIRED if binary body. */
  358. int is_static; /**< Nonzero if the content will never change and
  359. should be cached by the browser (e.g. images). */
  360. enum crunch_reason crunch_reason; /**< Why the response was generated in the first place. */
  361. };
  362. struct url_spec
  363. {
  364. #ifdef FEATURE_PCRE_HOST_PATTERNS
  365. regex_t *host_regex;/**< Regex for host matching */
  366. enum host_regex_type { VANILLA_HOST_PATTERN, PCRE_HOST_PATTERN } host_regex_type;
  367. #endif /* defined FEATURE_PCRE_HOST_PATTERNS */
  368. char *dbuffer; /**< Buffer with '\0'-delimited domain name, or NULL to match all hosts. */
  369. char **dvec; /**< List of pointers to the strings in dbuffer. */
  370. int dcount; /**< How many parts to this domain? (length of dvec) */
  371. int unanchored; /**< Bitmap - flags are ANCHOR_LEFT and ANCHOR_RIGHT. */
  372. char *port_list; /**< List of acceptable ports, or NULL to match all ports */
  373. regex_t *preg; /**< Regex for matching path part */
  374. };
  375. /**
  376. * A URL or a tag pattern.
  377. */
  378. struct pattern_spec
  379. {
  380. /** The string which was parsed to produce this pattern_spec.
  381. Used for debugging or display only. */
  382. char *spec;
  383. union
  384. {
  385. struct url_spec url_spec;
  386. regex_t *tag_regex;
  387. } pattern;
  388. unsigned int flags; /**< Bitmap with various pattern properties. */
  389. };
  390. /**
  391. * Constant for host part matching in URLs. If set, indicates that the start of
  392. * the pattern must match the start of the URL. E.g. this is not set for the
  393. * pattern ".example.com", so that it will match both "example.com" and
  394. * "www.example.com". It is set for the pattern "example.com", which makes it
  395. * match "example.com" only, not "www.example.com".
  396. */
  397. #define ANCHOR_LEFT 1
  398. /**
  399. * Constant for host part matching in URLs. If set, indicates that the end of
  400. * the pattern must match the end of the URL. E.g. this is not set for the
  401. * pattern "ad.", so that it will match any host called "ad", irrespective
  402. * of how many subdomains are in the fully-qualified domain name.
  403. */
  404. #define ANCHOR_RIGHT 2
  405. /** Pattern spec bitmap: It's an URL pattern. */
  406. #define PATTERN_SPEC_URL_PATTERN 0x00000001UL
  407. /** Pattern spec bitmap: It's a TAG pattern. */
  408. #define PATTERN_SPEC_TAG_PATTERN 0x00000002UL
  409. /** Pattern spec bitmap: It's a NO-REQUEST-TAG pattern. */
  410. #define PATTERN_SPEC_NO_REQUEST_TAG_PATTERN 0x00000004UL
  411. /** Pattern spec bitmap: It's a NO-RESPONSE-TAG pattern. */
  412. #define PATTERN_SPEC_NO_RESPONSE_TAG_PATTERN 0x00000008UL
  413. /** Pattern spec bitmap: It's a CLIENT-TAG pattern. */
  414. #define PATTERN_SPEC_CLIENT_TAG_PATTERN 0x00000010UL
  415. /**
  416. * An I/O buffer. Holds a string which can be appended to, and can have data
  417. * removed from the beginning.
  418. */
  419. struct iob
  420. {
  421. char *buf; /**< Start of buffer */
  422. char *cur; /**< Start of relevant data */
  423. char *eod; /**< End of relevant data */
  424. size_t size; /**< Size as malloc()ed */
  425. };
  426. /* Bits for csp->content_type bitmask: */
  427. #define CT_TEXT 0x0001U /**< Suitable for pcrs filtering. */
  428. #define CT_GIF 0x0002U /**< Suitable for GIF filtering. */
  429. #define CT_TABOO 0x0004U /**< DO NOT filter, irrespective of other flags. */
  430. /* Although these are not, strictly speaking, content types
  431. * (they are content encodings), it is simple to handle them
  432. * as such.
  433. */
  434. #define CT_GZIP 0x0010U /**< gzip-compressed data. */
  435. #define CT_DEFLATE 0x0020U /**< zlib-compressed data. */
  436. #define CT_BROTLI 0x0040U /**< Brotli-compressed data. */
  437. /**
  438. * Flag to signal that the server declared the content type,
  439. * so we can differentiate between unknown and undeclared
  440. * content types.
  441. */
  442. #define CT_DECLARED 0x0080U
  443. /**
  444. * The mask which includes all actions.
  445. */
  446. #define ACTION_MASK_ALL (~0UL)
  447. /**
  448. * The most compatible set of actions - i.e. none.
  449. */
  450. #define ACTION_MOST_COMPATIBLE 0x00000000UL
  451. /** Action bitmap: Block the request. */
  452. #define ACTION_BLOCK 0x00000001UL
  453. /** Action bitmap: Deanimate if it's a GIF. */
  454. #define ACTION_DEANIMATE 0x00000002UL
  455. /** Action bitmap: Downgrade HTTP/1.1 to 1.0. */
  456. #define ACTION_DOWNGRADE 0x00000004UL
  457. /** Action bitmap: Fast redirects. */
  458. #define ACTION_FAST_REDIRECTS 0x00000008UL
  459. /** Action bitmap: Remove or add "X-Forwarded-For" header. */
  460. #define ACTION_CHANGE_X_FORWARDED_FOR 0x00000010UL
  461. /** Action bitmap: Hide "From" header. */
  462. #define ACTION_HIDE_FROM 0x00000020UL
  463. /** Action bitmap: Hide "Referer" header. (sic - follow HTTP, not English). */
  464. #define ACTION_HIDE_REFERER 0x00000040UL
  465. /** Action bitmap: Hide "User-Agent" and similar headers. */
  466. #define ACTION_HIDE_USER_AGENT 0x00000080UL
  467. /** Action bitmap: This is an image. */
  468. #define ACTION_IMAGE 0x00000100UL
  469. /** Action bitmap: Sets the image blocker. */
  470. #define ACTION_IMAGE_BLOCKER 0x00000200UL
  471. /** Action bitmap: Prevent compression. */
  472. #define ACTION_NO_COMPRESSION 0x00000400UL
  473. /** Action bitmap: Change cookies to session only cookies. */
  474. #define ACTION_SESSION_COOKIES_ONLY 0x00000800UL
  475. /** Action bitmap: Block cookies coming from the client. */
  476. #define ACTION_CRUNCH_OUTGOING_COOKIES 0x00001000UL
  477. /** Action bitmap: Block cookies coming from the server. */
  478. #define ACTION_CRUNCH_INCOMING_COOKIES 0x00002000UL
  479. /** Action bitmap: Override the forward settings in the config file */
  480. #define ACTION_FORWARD_OVERRIDE 0x00004000UL
  481. /** Action bitmap: Block as empty document */
  482. #define ACTION_HANDLE_AS_EMPTY_DOCUMENT 0x00008000UL
  483. /** Action bitmap: Limit CONNECT requests to safe ports. */
  484. #define ACTION_LIMIT_CONNECT 0x00010000UL
  485. /** Action bitmap: Redirect request. */
  486. #define ACTION_REDIRECT 0x00020000UL
  487. /** Action bitmap: Crunch or modify "if-modified-since" header. */
  488. #define ACTION_HIDE_IF_MODIFIED_SINCE 0x00040000UL
  489. /** Action bitmap: Overwrite Content-Type header. */
  490. #define ACTION_CONTENT_TYPE_OVERWRITE 0x00080000UL
  491. /** Action bitmap: Crunch specified server header. */
  492. #define ACTION_CRUNCH_SERVER_HEADER 0x00100000UL
  493. /** Action bitmap: Crunch specified client header */
  494. #define ACTION_CRUNCH_CLIENT_HEADER 0x00200000UL
  495. /** Action bitmap: Enable text mode by force */
  496. #define ACTION_FORCE_TEXT_MODE 0x00400000UL
  497. /** Action bitmap: Remove the "If-None-Match" header. */
  498. #define ACTION_CRUNCH_IF_NONE_MATCH 0x00800000UL
  499. /** Action bitmap: Enable content-disposition crunching */
  500. #define ACTION_HIDE_CONTENT_DISPOSITION 0x01000000UL
  501. /** Action bitmap: Replace or block Last-Modified header */
  502. #define ACTION_OVERWRITE_LAST_MODIFIED 0x02000000UL
  503. /** Action bitmap: Replace or block Accept-Language header */
  504. #define ACTION_HIDE_ACCEPT_LANGUAGE 0x04000000UL
  505. /** Action bitmap: Limit the cookie lifetime */
  506. #define ACTION_LIMIT_COOKIE_LIFETIME 0x08000000UL
  507. /** Action bitmap: Delay writes */
  508. #define ACTION_DELAY_RESPONSE 0x10000000UL
  509. /** Action bitmap: Turn https inspection on */
  510. #define ACTION_HTTPS_INSPECTION 0x20000000UL
  511. /** Action bitmap: Turn certificates verification off */
  512. #define ACTION_IGNORE_CERTIFICATE_ERRORS 0x40000000UL
  513. /** Action string index: How to deanimate GIFs */
  514. #define ACTION_STRING_DEANIMATE 0
  515. /** Action string index: Replacement for "From:" header */
  516. #define ACTION_STRING_FROM 1
  517. /** Action string index: How to block images */
  518. #define ACTION_STRING_IMAGE_BLOCKER 2
  519. /** Action string index: Replacement for "Referer:" header */
  520. #define ACTION_STRING_REFERER 3
  521. /** Action string index: Replacement for "User-Agent:" header */
  522. #define ACTION_STRING_USER_AGENT 4
  523. /** Action string index: Legal CONNECT ports. */
  524. #define ACTION_STRING_LIMIT_CONNECT 5
  525. /** Action string index: Server headers containing this pattern are crunched*/
  526. #define ACTION_STRING_SERVER_HEADER 6
  527. /** Action string index: Client headers containing this pattern are crunched*/
  528. #define ACTION_STRING_CLIENT_HEADER 7
  529. /** Action string index: Replacement for the "Accept-Language:" header*/
  530. #define ACTION_STRING_LANGUAGE 8
  531. /** Action string index: Replacement for the "Content-Type:" header*/
  532. #define ACTION_STRING_CONTENT_TYPE 9
  533. /** Action string index: Replacement for the "content-disposition:" header*/
  534. #define ACTION_STRING_CONTENT_DISPOSITION 10
  535. /** Action string index: Replacement for the "If-Modified-Since:" header*/
  536. #define ACTION_STRING_IF_MODIFIED_SINCE 11
  537. /** Action string index: Replacement for the "Last-Modified:" header. */
  538. #define ACTION_STRING_LAST_MODIFIED 12
  539. /** Action string index: Redirect URL */
  540. #define ACTION_STRING_REDIRECT 13
  541. /** Action string index: Decode before redirect? */
  542. #define ACTION_STRING_FAST_REDIRECTS 14
  543. /** Action string index: Overriding forward rule. */
  544. #define ACTION_STRING_FORWARD_OVERRIDE 15
  545. /** Action string index: Reason for the block. */
  546. #define ACTION_STRING_BLOCK 16
  547. /** Action string index: what to do with the "X-Forwarded-For" header. */
  548. #define ACTION_STRING_CHANGE_X_FORWARDED_FOR 17
  549. /** Action string index: how many minutes cookies should be valid. */
  550. #define ACTION_STRING_LIMIT_COOKIE_LIFETIME 18
  551. /** Action string index: how many milliseconds writes should be delayed. */
  552. #define ACTION_STRING_DELAY_RESPONSE 19
  553. /** Number of string actions. */
  554. #define ACTION_STRING_COUNT 20
  555. /* To make the ugly hack in sed easier to understand */
  556. #define CHECK_EVERY_HEADER_REMAINING 0
  557. /** Index into current_action_spec::multi[] for headers to add. */
  558. #define ACTION_MULTI_ADD_HEADER 0
  559. /** Index into current_action_spec::multi[] for content filters to apply. */
  560. #define ACTION_MULTI_FILTER 1
  561. /** Index into current_action_spec::multi[] for server-header filters to apply. */
  562. #define ACTION_MULTI_SERVER_HEADER_FILTER 2
  563. /** Index into current_action_spec::multi[] for client-header filters to apply. */
  564. #define ACTION_MULTI_CLIENT_HEADER_FILTER 3
  565. /** Index into current_action_spec::multi[] for client-header tags to apply. */
  566. #define ACTION_MULTI_CLIENT_HEADER_TAGGER 4
  567. /** Index into current_action_spec::multi[] for server-header tags to apply. */
  568. #define ACTION_MULTI_SERVER_HEADER_TAGGER 5
  569. /** Number of multi-string actions. */
  570. #define ACTION_MULTI_EXTERNAL_FILTER 6
  571. /** Number of multi-string actions. */
  572. #define ACTION_MULTI_COUNT 7
  573. /**
  574. * This structure contains a list of actions to apply to a URL.
  575. * It only contains positive instructions - no "-" options.
  576. * It is not used to store the actions list itself, only for
  577. * url_actions() to return the current values.
  578. */
  579. struct current_action_spec
  580. {
  581. /** Actions to apply. A bit set to "1" means perform the action. */
  582. unsigned long flags;
  583. /**
  584. * Parameters for those actions that require them.
  585. * Each entry is valid if & only if the corresponding entry in "flags" is
  586. * set.
  587. */
  588. char * string[ACTION_STRING_COUNT];
  589. /** Lists of strings for multi-string actions. */
  590. struct list multi[ACTION_MULTI_COUNT][1];
  591. };
  592. /**
  593. * This structure contains a set of changes to actions.
  594. * It can contain both positive and negative instructions.
  595. * It is used to store an entry in the actions list.
  596. */
  597. struct action_spec
  598. {
  599. unsigned long mask; /**< Actions to keep. A bit set to "0" means remove action. */
  600. unsigned long add; /**< Actions to add. A bit set to "1" means add action. */
  601. /**
  602. * Parameters for those actions that require them.
  603. * Each entry is valid if & only if the corresponding entry in "flags" is
  604. * set.
  605. */
  606. char * string[ACTION_STRING_COUNT];
  607. /** Lists of strings to remove, for multi-string actions. */
  608. struct list multi_remove[ACTION_MULTI_COUNT][1];
  609. /** If nonzero, remove *all* strings from the multi-string action. */
  610. int multi_remove_all[ACTION_MULTI_COUNT];
  611. /** Lists of strings to add, for multi-string actions. */
  612. struct list multi_add[ACTION_MULTI_COUNT][1];
  613. };
  614. /**
  615. * This structure is used to store action files.
  616. *
  617. * It contains an URL or tag pattern, and the changes to
  618. * the actions. It's a linked list and should only be
  619. * free'd through unload_actions_file() unless there's
  620. * only a single entry.
  621. */
  622. struct url_actions
  623. {
  624. struct pattern_spec url[1]; /**< The URL or tag pattern. */
  625. struct action_spec *action; /**< Action settings that might be shared with
  626. the list entry before or after the current
  627. one and can't be free'd willy nilly. */
  628. struct url_actions *next; /**< Next action section in file, or NULL. */
  629. };
  630. enum forwarder_type {
  631. /**< Don't use a SOCKS server, forward to a HTTP proxy directly */
  632. SOCKS_NONE = 0,
  633. /**< original SOCKS 4 protocol */
  634. SOCKS_4 = 40,
  635. /**< SOCKS 4A, DNS resolution is done by the SOCKS server */
  636. SOCKS_4A = 41,
  637. /**< SOCKS 5 with hostnames, DNS resolution is done by the SOCKS server */
  638. SOCKS_5 = 50,
  639. /**< Like SOCKS5, but uses non-standard Tor extensions (currently only optimistic data) */
  640. SOCKS_5T,
  641. /**<
  642. * Don't use a SOCKS server, forward to the specified webserver.
  643. * The difference to SOCKS_NONE is that a request line without
  644. * full URL is sent.
  645. */
  646. FORWARD_WEBSERVER,
  647. };
  648. /*
  649. * Structure to hold the server socket and the information
  650. * required to make sure we only reuse the connection if
  651. * the host and forwarding settings are the same.
  652. */
  653. struct reusable_connection
  654. {
  655. jb_socket sfd;
  656. int in_use;
  657. time_t timestamp; /* XXX: rename? */
  658. time_t request_sent;
  659. time_t response_received;
  660. /*
  661. * Number of seconds after which this
  662. * connection will no longer be reused.
  663. */
  664. unsigned int keep_alive_timeout;
  665. /*
  666. * Number of requests that were sent to this connection.
  667. * This is currently only for debugging purposes.
  668. */
  669. unsigned int requests_sent_total;
  670. char *host;
  671. int port;
  672. enum forwarder_type forwarder_type;
  673. char *gateway_host;
  674. int gateway_port;
  675. char *auth_username;
  676. char *auth_password;
  677. char *forward_host;
  678. int forward_port;
  679. };
  680. /*
  681. * Flags for use in csp->flags
  682. */
  683. /**
  684. * Flag for csp->flags: Set if this client is processing data.
  685. * Cleared when the thread associated with this structure dies.
  686. */
  687. #define CSP_FLAG_ACTIVE 0x01U
  688. /**
  689. * Flag for csp->flags: Set if the server's reply is in "chunked"
  690. * transfer encoding
  691. */
  692. #define CSP_FLAG_CHUNKED 0x02U
  693. /**
  694. * Flag for csp->flags: Set if this request was enforced, although it would
  695. * normally have been blocked.
  696. */
  697. #define CSP_FLAG_FORCED 0x04U
  698. /**
  699. * Flag for csp->flags: Set if any modification to the body was done.
  700. */
  701. #define CSP_FLAG_MODIFIED 0x08U
  702. /**
  703. * Flag for csp->flags: Set if request was blocked.
  704. */
  705. #define CSP_FLAG_REJECTED 0x10U
  706. /**
  707. * Flag for csp->flags: Set if we are toggled on (FEATURE_TOGGLE).
  708. */
  709. #define CSP_FLAG_TOGGLED_ON 0x20U
  710. /**
  711. * Flag for csp->flags: Set if an acceptable Connection header
  712. * has already been set by the client.
  713. */
  714. #define CSP_FLAG_CLIENT_CONNECTION_HEADER_SET 0x00000040U
  715. /**
  716. * Flag for csp->flags: Set if an acceptable Connection header
  717. * has already been set by the server.
  718. */
  719. #define CSP_FLAG_SERVER_CONNECTION_HEADER_SET 0x00000080U
  720. /**
  721. * Flag for csp->flags: Signals header parsers whether they
  722. * are parsing server or client headers.
  723. */
  724. #define CSP_FLAG_CLIENT_HEADER_PARSING_DONE 0x00000100U
  725. /**
  726. * Flag for csp->flags: Set if adding the Host: header
  727. * isn't necessary.
  728. */
  729. #define CSP_FLAG_HOST_HEADER_IS_SET 0x00000200U
  730. /**
  731. * Flag for csp->flags: Set if filtering is disabled by X-Filter: No
  732. * XXX: As we now have tags we might as well ditch this.
  733. */
  734. #define CSP_FLAG_NO_FILTERING 0x00000400U
  735. /**
  736. * Flag for csp->flags: Set the client IP has appended to
  737. * an already existing X-Forwarded-For header in which case
  738. * no new header has to be generated.
  739. */
  740. #define CSP_FLAG_X_FORWARDED_FOR_APPENDED 0x00000800U
  741. /**
  742. * Flag for csp->flags: Set if the server wants to keep
  743. * the connection alive.
  744. */
  745. #define CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE 0x00001000U
  746. /**
  747. * Flag for csp->flags: Set if the server specified the
  748. * content length.
  749. */
  750. #define CSP_FLAG_SERVER_CONTENT_LENGTH_SET 0x00002000U
  751. /**
  752. * Flag for csp->flags: Set if we know the content length,
  753. * either because the server set it, or we figured it out
  754. * on our own.
  755. */
  756. #define CSP_FLAG_CONTENT_LENGTH_SET 0x00004000U
  757. /**
  758. * Flag for csp->flags: Set if the client wants to keep
  759. * the connection alive.
  760. */
  761. #define CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE 0x00008000U
  762. /**
  763. * Flag for csp->flags: Set if we think we got the whole
  764. * client request and shouldn't read any additional data
  765. * coming from the client until the current request has
  766. * been dealt with.
  767. */
  768. #define CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ 0x00010000U
  769. /**
  770. * Flag for csp->flags: Set if the server promised us to
  771. * keep the connection open for a known number of seconds.
  772. */
  773. #define CSP_FLAG_SERVER_KEEP_ALIVE_TIMEOUT_SET 0x00020000U
  774. /**
  775. * Flag for csp->flags: Set if we think we can't reuse
  776. * the server socket. XXX: It's also set after sabotaging
  777. * pipelining attempts which is somewhat inconsistent with
  778. * the name.
  779. */
  780. #define CSP_FLAG_SERVER_SOCKET_TAINTED 0x00040000U
  781. /**
  782. * Flag for csp->flags: Set if the Proxy-Connection header
  783. * is among the server headers.
  784. */
  785. #define CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET 0x00080000U
  786. /**
  787. * Flag for csp->flags: Set if the client reused its connection.
  788. */
  789. #define CSP_FLAG_REUSED_CLIENT_CONNECTION 0x00100000U
  790. /**
  791. * Flag for csp->flags: Set if the supports deflate compression.
  792. */
  793. #define CSP_FLAG_CLIENT_SUPPORTS_DEFLATE 0x00200000U
  794. /**
  795. * Flag for csp->flags: Set if the content has been deflated by Privoxy
  796. */
  797. #define CSP_FLAG_BUFFERED_CONTENT_DEFLATED 0x00400000U
  798. /**
  799. * Flag for csp->flags: Set if we already read (parts of)
  800. * a pipelined request in which case the client obviously
  801. * isn't done talking.
  802. */
  803. #define CSP_FLAG_PIPELINED_REQUEST_WAITING 0x00800000U
  804. /**
  805. * Flag for csp->flags: Set if the client body is chunk-encoded
  806. */
  807. #define CSP_FLAG_CHUNKED_CLIENT_BODY 0x01000000U
  808. /**
  809. * Flag for csp->flags: Set if the client set the Expect header
  810. */
  811. #define CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION 0x02000000U
  812. /**
  813. * Flag for csp->flags: Set if we answered the request ourselves.
  814. */
  815. #define CSP_FLAG_CRUNCHED 0x04000000U
  816. #ifdef FUZZ
  817. /**
  818. * Flag for csp->flags: Set if we are working with fuzzed input
  819. */
  820. #define CSP_FLAG_FUZZED_INPUT 0x08000000U
  821. #endif
  822. /*
  823. * Flags for use in return codes of child processes
  824. */
  825. /**
  826. * Flag for process return code: Set if exiting process has been toggled
  827. * during its lifetime.
  828. */
  829. #define RC_FLAG_TOGGLED 0x10
  830. /**
  831. * Flag for process return code: Set if exiting process has blocked its
  832. * request.
  833. */
  834. #define RC_FLAG_BLOCKED 0x20
  835. /**
  836. * Maximum number of actions/filter files. This limit is arbitrary - it's just used
  837. * to size an array.
  838. */
  839. #define MAX_AF_FILES 100
  840. /**
  841. * Maximum number of sockets to listen to. This limit is arbitrary - it's just used
  842. * to size an array.
  843. */
  844. #define MAX_LISTENING_SOCKETS 10
  845. struct ssl_attr {
  846. #ifdef FEATURE_HTTPS_INSPECTION_MBEDTLS
  847. mbedtls_connection_attr mbedtls_attr; /* Mbed TLS attrs*/
  848. #endif /* FEATURE_HTTPS_INSPECTION_MBEDTLS */
  849. #ifdef FEATURE_HTTPS_INSPECTION_OPENSSL
  850. openssl_connection_attr openssl_attr; /* OpenSSL atrrs */
  851. #endif /* FEATURE_HTTPS_INSPECTION_OPENSSL */
  852. };
  853. /**
  854. * The state of a Privoxy processing thread.
  855. */
  856. struct client_state
  857. {
  858. /** The proxy's configuration */
  859. struct configuration_spec * config;
  860. /** The actions to perform on the current request */
  861. struct current_action_spec action[1];
  862. /** socket to talk to client (web browser) */
  863. jb_socket cfd;
  864. /** Number of requests received on the client socket. */
  865. unsigned int requests_received_total;
  866. /** current connection to the server (may go through a proxy) */
  867. struct reusable_connection server_connection;
  868. /** Multi-purpose flag container, see CSP_FLAG_* above */
  869. unsigned int flags;
  870. /** Client PC's IP address, as reported by the accept() function.
  871. As a string. */
  872. char *ip_addr_str;
  873. #ifdef HAVE_RFC2553
  874. /** Client PC's TCP address, as reported by the accept() function.
  875. As a sockaddr. */
  876. struct sockaddr_storage tcp_addr;
  877. #else
  878. /** Client PC's IP address, as reported by the accept() function.
  879. As a number. */
  880. unsigned long ip_addr_long;
  881. #endif /* def HAVE_RFC2553 */
  882. /** The host name and port (as a string of the form '<hostname>:<port>')
  883. of the server socket to which the client connected. */
  884. char *listen_addr_str;
  885. /** The URL that was requested */
  886. struct http_request http[1];
  887. /*
  888. * The final forwarding settings.
  889. * XXX: Currently this is only used for forward-override,
  890. * so we can free the space in sweep.
  891. */
  892. struct forward_spec * fwd;
  893. /** An I/O buffer used for buffering data read from the server */
  894. /* XXX: should be renamed to server_iob */
  895. struct iob iob[1];
  896. struct ssl_attr ssl_server_attr; /* attributes for connection to server */
  897. struct ssl_attr ssl_client_attr; /* attributes for connection to client */
  898. /** An I/O buffer used for buffering data read from the client */
  899. struct iob client_iob[1];
  900. /** Buffer used to briefly store data read from the network
  901. * before forwarding or processing it.
  902. */
  903. char *receive_buffer;
  904. size_t receive_buffer_size;
  905. /** List of all headers for this request */
  906. struct list headers[1];
  907. #ifdef FEATURE_HTTPS_INSPECTION
  908. /** List of all encrypted headers for this request */
  909. struct list https_headers[1];
  910. #endif
  911. /** List of all tags that apply to this request */
  912. struct list tags[1];
  913. #ifdef FEATURE_CLIENT_TAGS
  914. /** List of all tags that apply to this client (assigned based on address) */
  915. struct list client_tags[1];
  916. /** The address of the client the request (presumably) came from.
  917. * Either the address returned by accept(), or the address provided
  918. * with the X-Forwarded-For header, provided Privoxy has been configured
  919. * to use it.
  920. */
  921. char *client_address;
  922. #endif
  923. /** MIME-Type key, see CT_* above */
  924. unsigned int content_type;
  925. /** Actions files associated with this client */
  926. struct file_list *actions_list[MAX_AF_FILES];
  927. /** pcrs job files. */
  928. struct file_list *rlist[MAX_AF_FILES];
  929. /** Length after content modification. */
  930. unsigned long long content_length;
  931. /* XXX: is this the right location? */
  932. /** Expected length of content after which we
  933. * should stop reading from the server socket.
  934. */
  935. unsigned long long expected_content_length;
  936. /** Expected length of content after which we
  937. * should stop reading from the client socket.
  938. */
  939. unsigned long long expected_client_content_length;
  940. #ifdef FEATURE_TRUST
  941. /** Trust file. */
  942. struct file_list *tlist;
  943. #endif /* def FEATURE_TRUST */
  944. /**
  945. * Failure reason to embedded in the CGI error page,
  946. * or NULL. Currently only used for socks errors.
  947. */
  948. char *error_message;
  949. #ifdef FEATURE_HTTPS_INSPECTION
  950. /* Result of server certificate verification
  951. *
  952. * Values for flag determining certificate validity
  953. * are compatible with return value of function
  954. * mbedtls_ssl_get_verify_result() for mbedtls
  955. * and SSL_get_verify_result() for openssl.
  956. * There are no values for "invalid certificate", they are
  957. * set by the functions mentioned above.
  958. */
  959. #define SSL_CERT_VALID 0
  960. #ifdef FEATURE_HTTPS_INSPECTION_MBEDTLS
  961. #define SSL_CERT_NOT_VERIFIED 0xFFFFFFFF
  962. uint32_t server_cert_verification_result;
  963. #endif /* FEATURE_HTTPS_INSPECTION_MBEDTLS */
  964. #ifdef FEATURE_HTTPS_INSPECTION_OPENSSL
  965. #define SSL_CERT_NOT_VERIFIED ~0L
  966. long server_cert_verification_result;
  967. #endif /* FEATURE_HTTPS_INSPECTION_OPENSSL */
  968. /* Flag for certificate validity checking */
  969. int dont_verify_certificate;
  970. /*
  971. * Flags if SSL connection with server or client is opened.
  972. * Thanks to this flags, we can call function to close both connections
  973. * and we don't have to care about more details.
  974. */
  975. int ssl_with_server_is_opened;
  976. int ssl_with_client_is_opened;
  977. /*
  978. * Server certificate chain of trust including strings with certificates
  979. * information and string with whole certificate file
  980. */
  981. struct certs_chain server_certs_chain;
  982. #endif
  983. };
  984. /**
  985. * List of client states so the main thread can keep
  986. * track of them and garbage collect their resources.
  987. */
  988. struct client_states
  989. {
  990. struct client_states *next;
  991. struct client_state csp;
  992. };
  993. /**
  994. * A function to add a header
  995. */
  996. typedef jb_err (*add_header_func_ptr)(struct client_state *);
  997. /**
  998. * A function to process a header
  999. */
  1000. typedef jb_err (*parser_func_ptr )(struct client_state *, char **);
  1001. /**
  1002. * List of available CGI functions.
  1003. */
  1004. struct cgi_dispatcher
  1005. {
  1006. /** The URL of the CGI, relative to the CGI root. */
  1007. const char * const name;
  1008. /** The handler function for the CGI */
  1009. jb_err (* const handler)(struct client_state *csp, struct http_response *rsp, const struct map *parameters);
  1010. /** The description of the CGI, to appear on the main menu, or NULL to hide it. */
  1011. const char * const description;
  1012. /** A flag that indicates whether unintentional calls to this CGI can cause damage */
  1013. int harmless;
  1014. };
  1015. /**
  1016. * A data file used by Privoxy. Kept in a linked list.
  1017. */
  1018. struct file_list
  1019. {
  1020. /**
  1021. * This is a pointer to the data structures associated with the file.
  1022. * Read-only once the structure has been created.
  1023. */
  1024. void *f;
  1025. /**
  1026. * The unloader function.
  1027. * Normally NULL. When we are finished with file (i.e. when we have
  1028. * loaded a new one), set to a pointer to an unloader function.
  1029. * Unloader will be called by sweep() (called from main loop) when
  1030. * all clients using this file are done. This prevents threading
  1031. * problems.
  1032. */
  1033. void (*unloader)(void *);
  1034. /**
  1035. * Used internally by sweep(). Do not access from elsewhere.
  1036. */
  1037. int active;
  1038. /**
  1039. * File last-modified time, so we can check if file has been changed.
  1040. * Read-only once the structure has been created.
  1041. */
  1042. time_t lastmodified;
  1043. /**
  1044. * The full filename.
  1045. */
  1046. char * filename;
  1047. /**
  1048. * Pointer to next entry in the linked list of all "file_list"s.
  1049. * This linked list is so that sweep() can navigate it.
  1050. * Since sweep() can remove items from the list, we must be careful
  1051. * to only access this value from main thread (when we know sweep
  1052. * won't be running).
  1053. */
  1054. struct file_list *next;
  1055. };
  1056. #ifdef FEATURE_TRUST
  1057. /**
  1058. * The format of a trust file when loaded into memory.
  1059. */
  1060. struct block_spec
  1061. {
  1062. struct pattern_spec url[1]; /**< The URL pattern */
  1063. int reject; /**< FIXME: Please document this! */
  1064. struct block_spec *next; /**< Next entry in linked list */
  1065. };
  1066. /**
  1067. * Arbitrary limit for the number of trusted referrers.
  1068. */
  1069. #define MAX_TRUSTED_REFERRERS 512
  1070. #endif /* def FEATURE_TRUST */
  1071. /**
  1072. * How to forward a connection to a parent proxy.
  1073. */
  1074. struct forward_spec
  1075. {
  1076. /** URL pattern that this forward_spec is for. */
  1077. struct pattern_spec url[1];
  1078. /** Connection type. Must be SOCKS_NONE, SOCKS_4, SOCKS_4A or SOCKS_5. */
  1079. enum forwarder_type type;
  1080. /** SOCKS server hostname. Only valid if "type" is SOCKS_4 or SOCKS_4A. */
  1081. char *gateway_host;
  1082. /** SOCKS server port. */
  1083. int gateway_port;
  1084. /** SOCKS5 username. */
  1085. char *auth_username;
  1086. /** SOCKS5 password. */
  1087. char *auth_password;
  1088. /** Parent HTTP proxy hostname, or NULL for none. */
  1089. char *forward_host;
  1090. /** Parent HTTP proxy port. */
  1091. int forward_port;
  1092. /** Next entry in the linked list. */
  1093. struct forward_spec *next;
  1094. };
  1095. /* Supported filter types */
  1096. enum filter_type
  1097. {
  1098. FT_CONTENT_FILTER = 0,
  1099. FT_CLIENT_HEADER_FILTER = 1,
  1100. FT_SERVER_HEADER_FILTER = 2,
  1101. FT_CLIENT_HEADER_TAGGER = 3,
  1102. FT_SERVER_HEADER_TAGGER = 4,
  1103. #ifdef FEATURE_EXTERNAL_FILTERS
  1104. FT_EXTERNAL_CONTENT_FILTER = 5,
  1105. #endif
  1106. FT_INVALID_FILTER = 42,
  1107. };
  1108. #ifdef FEATURE_EXTERNAL_FILTERS
  1109. #define MAX_FILTER_TYPES 6
  1110. #else
  1111. #define MAX_FILTER_TYPES 5
  1112. #endif
  1113. /**
  1114. * This struct represents one filter (one block) from
  1115. * the re_filterfile. If there is more than one filter
  1116. * in the file, the file will be represented by a
  1117. * chained list of re_filterfile specs.
  1118. */
  1119. struct re_filterfile_spec
  1120. {
  1121. char *name; /**< Name from FILTER: statement in re_filterfile. */
  1122. char *description; /**< Description from FILTER: statement in re_filterfile. */
  1123. struct list patterns[1]; /**< The patterns from the re_filterfile. */
  1124. pcrs_job *joblist; /**< The resulting compiled pcrs_jobs. */
  1125. enum filter_type type; /**< Filter type (content, client-header, server-header). */
  1126. int dynamic; /**< Set to one if the pattern might contain variables
  1127. and has to be recompiled for every request. */
  1128. struct re_filterfile_spec *next; /**< The pointer for chaining. */
  1129. };
  1130. #ifdef FEATURE_ACL
  1131. #define ACL_PERMIT 1 /**< Accept connection request */
  1132. #define ACL_DENY 2 /**< Reject connection request */
  1133. /**
  1134. * An IP address pattern. Used to specify networks in the ACL.
  1135. */
  1136. struct access_control_addr
  1137. {
  1138. #ifdef HAVE_RFC2553
  1139. struct sockaddr_storage addr; /* <The TCP address in network order. */
  1140. struct sockaddr_storage mask; /* <The TCP mask in network order. */
  1141. #else
  1142. unsigned long addr; /**< The IP address as an integer. */
  1143. unsigned long mask; /**< The network mask as an integer. */
  1144. unsigned long port; /**< The port number. */
  1145. #endif /* HAVE_RFC2553 */
  1146. };
  1147. /**
  1148. * An access control list (ACL) entry.
  1149. *
  1150. * This is a linked list.
  1151. */
  1152. struct access_control_list
  1153. {
  1154. struct access_control_addr src[1]; /**< Client IP address */
  1155. struct access_control_addr dst[1]; /**< Website or parent proxy IP address */
  1156. #ifdef HAVE_RFC2553
  1157. int wildcard_dst; /** < dst address is wildcard */
  1158. #endif
  1159. short action; /**< ACL_PERMIT or ACL_DENY */
  1160. struct access_control_list *next; /**< The next entry in the ACL. */
  1161. };
  1162. #endif /* def FEATURE_ACL */
  1163. /** Maximum number of loaders (actions, re_filter, ...) */
  1164. #define NLOADERS 8
  1165. /**
  1166. * This struct represents a client-spcific-tag and it's description
  1167. */
  1168. struct client_tag_spec
  1169. {
  1170. char *name; /**< Name from "client-specific-tag bla" directive */
  1171. char *description; /**< Description from "client-specific-tag-description " directive */
  1172. struct client_tag_spec *next; /**< The pointer for chaining. */
  1173. };
  1174. /** configuration_spec::feature_flags: CGI actions editor. */
  1175. #define RUNTIME_FEATURE_CGI_EDIT_ACTIONS 1U
  1176. /** configuration_spec::feature_flags: Web-based toggle. */
  1177. #define RUNTIME_FEATURE_CGI_TOGGLE 2U
  1178. /** configuration_spec::feature_flags: HTTP-header-based toggle. */
  1179. #define RUNTIME_FEATURE_HTTP_TOGGLE 4U
  1180. /** configuration_spec::feature_flags: Split large forms to limit the number of GET arguments. */
  1181. #define RUNTIME_FEATURE_SPLIT_LARGE_FORMS 8U
  1182. /** configuration_spec::feature_flags: Check the host header for requests with host-less request lines. */
  1183. #define RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS 16U
  1184. /** configuration_spec::feature_flags: Don't allow to circumvent blocks with the force prefix. */
  1185. #define RUNTIME_FEATURE_ENFORCE_BLOCKS 32U
  1186. /** configuration_spec::feature_flags: Allow to block or redirect CGI requests. */
  1187. #define RUNTIME_FEATURE_CGI_CRUNCHING 64U
  1188. /** configuration_spec::feature_flags: Try to keep the connection to the server alive. */
  1189. #define RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE 128U
  1190. /** configuration_spec::feature_flags: Share outgoing connections between different client connections. */
  1191. #define RUNTIME_FEATURE_CONNECTION_SHARING 256U
  1192. /** configuration_spec::feature_flags: Pages blocked with +handle-as-empty-doc get a return status of 200 OK. */
  1193. #define RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK 512U
  1194. /** configuration_spec::feature_flags: Buffered content is sent compressed if the client supports it. */
  1195. #define RUNTIME_FEATURE_COMPRESSION 1024U
  1196. /** configuration_spec::feature_flags: Pipelined requests are served instead of being discarded. */
  1197. #define RUNTIME_FEATURE_TOLERATE_PIPELINING 2048U
  1198. /** configuration_spec::feature_flags: Proxy authentication headers are forwarded instead of removed. */
  1199. #define RUNTIME_FEATURE_FORWARD_PROXY_AUTHENTICATION_HEADERS 4096U
  1200. /**
  1201. * Data loaded from the configuration file.
  1202. *
  1203. * (Anomaly: toggle is still handled through a global, not this structure)
  1204. */
  1205. struct configuration_spec
  1206. {
  1207. /** What to log */
  1208. int debug;
  1209. /** Nonzero to enable multithreading. */
  1210. int multi_threaded;
  1211. /** Bitmask of features that can be controlled through the config file. */
  1212. unsigned feature_flags;
  1213. /** The log file name. */
  1214. const char *logfile;
  1215. /** The config file directory. */
  1216. const char *confdir;
  1217. /** The directory for customized CGI templates. */
  1218. const char *templdir;
  1219. /** "Cross-origin resource sharing" (CORS) allowed origin */
  1220. const char *cors_allowed_origin;
  1221. #ifdef FEATURE_EXTERNAL_FILTERS
  1222. /** The template used to create temporary files. */
  1223. const char *temporary_directory;
  1224. #endif
  1225. /** The log file directory. */
  1226. const char *logdir;
  1227. /** The full paths to the actions files. */
  1228. const char *actions_file[MAX_AF_FILES];
  1229. /** The short names of the actions files. */
  1230. const char *actions_file_short[MAX_AF_FILES];
  1231. /** The administrator's email address */
  1232. char *admin_address;
  1233. /** A URL with info on this proxy */
  1234. char *proxy_info_url;
  1235. /** URL to the user manual (on our website or local copy) */
  1236. char *usermanual;
  1237. /** The file names of the pcre filter files. */
  1238. const char *re_filterfile[MAX_AF_FILES];
  1239. /** The short names of the pcre filter files. */
  1240. const char *re_filterfile_short[MAX_AF_FILES];
  1241. /**< List of ordered client header names. */
  1242. struct list ordered_client_headers[1];
  1243. /** The hostname to show on CGI pages, or NULL to use the real one. */
  1244. const char *hostname;
  1245. /** IP addresses to bind to. Defaults to HADDR_DEFAULT == 127.0.0.1. */
  1246. const char *haddr[MAX_LISTENING_SOCKETS];
  1247. /** Trusted referring site that can be used to reach CGI
  1248. * pages that aren't marked as harmful.
  1249. */
  1250. const char *trusted_cgi_referrer;
  1251. /** Ports to bind to. Defaults to HADDR_PORT == 8118. */
  1252. int hport[MAX_LISTENING_SOCKETS];
  1253. /** Size limit for IOB */
  1254. size_t buffer_limit;
  1255. /** Size of the receive buffer */
  1256. size_t receive_buffer_size;
  1257. /** Use accf_http(4) if available */
  1258. int enable_accept_filter;
  1259. /** Backlog passed to listen() */
  1260. int listen_backlog;
  1261. #ifdef FEATURE_TRUST
  1262. /** The file name of the trust file. */
  1263. const char * trustfile;
  1264. /** FIXME: DOCME: Document this. */
  1265. struct list trust_info[1];
  1266. /** FIXME: DOCME: Document this. */
  1267. struct pattern_spec *trust_list[MAX_TRUSTED_REFERRERS];
  1268. #endif /* def FEATURE_TRUST */
  1269. #ifdef FEATURE_CLIENT_TAGS
  1270. struct client_tag_spec client_tags[1];
  1271. /* Maximum number of seconds a temporarily enabled tag stays enabled. */
  1272. unsigned int client_tag_lifetime;
  1273. #endif /* def FEATURE_CLIENT_TAGS */
  1274. int trust_x_forwarded_for;
  1275. #ifdef FEATURE_ACL
  1276. /** The access control list (ACL). */
  1277. struct access_control_list *acl;
  1278. #endif /* def FEATURE_ACL */
  1279. /** Information about parent proxies (forwarding). */
  1280. struct forward_spec *forward;
  1281. /** Number of retries in case a forwarded connection attempt fails */
  1282. int forwarded_connect_retries;
  1283. /** Maximum number of client connections. */
  1284. int max_client_connections;
  1285. /* Timeout when waiting on sockets for data to become available. */
  1286. int socket_timeout;
  1287. #ifdef FEATURE_CONNECTION_KEEP_ALIVE
  1288. /* Maximum number of seconds after which an open connection will no longer be reused. */
  1289. unsigned int keep_alive_timeout;
  1290. /* Assumed server-side keep alive timeout if none is specified. */
  1291. unsigned int default_server_timeout;
  1292. #endif
  1293. #ifdef FEATURE_COMPRESSION
  1294. int compression_level;
  1295. #endif
  1296. /** All options from the config file, HTML-formatted. */
  1297. char *proxy_args;
  1298. /** The configuration file object. */
  1299. struct file_list *config_file_list;
  1300. /** List of loaders */
  1301. int (*loaders[NLOADERS])(struct client_state *);
  1302. /** Nonzero if we need to bind() to the new port. */
  1303. int need_bind;
  1304. #ifdef FEATURE_HTTPS_INSPECTION
  1305. /** Password for proxy ca file **/
  1306. char * ca_password;
  1307. /** Directory with files of ca **/
  1308. char *ca_directory;
  1309. /** Filename of ca certificate **/
  1310. char * ca_cert_file;
  1311. /** Filename of ca key **/
  1312. char * ca_key_file;
  1313. /** Directory for saving certificates and keys for each webpage **/
  1314. char *certificate_directory;
  1315. /** Cipher list to use **/
  1316. char *cipher_list;
  1317. /** Filename of trusted CAs certificates **/
  1318. char * trusted_cas_file;
  1319. #endif
  1320. };
  1321. /** Calculates the number of elements in an array, using sizeof. */
  1322. #define SZ(X) (sizeof(X) / sizeof(*X))
  1323. /** The force load URL prefix. Not behind an ifdef because
  1324. * it's always used for the show-status page. */
  1325. #define FORCE_PREFIX "/PRIVOXY-FORCE"
  1326. #ifdef FEATURE_NO_GIFS
  1327. /** The MIME type for images ("image/png" or "image/gif"). */
  1328. #define BUILTIN_IMAGE_MIMETYPE "image/png"
  1329. #else
  1330. #define BUILTIN_IMAGE_MIMETYPE "image/gif"
  1331. #endif /* def FEATURE_NO_GIFS */
  1332. /*
  1333. * Hardwired URLs
  1334. */
  1335. /** URL for the Privoxy home page. */
  1336. #define HOME_PAGE_URL "https://www.privoxy.org/"
  1337. /** URL for the Privoxy user manual. */
  1338. #define USER_MANUAL_URL HOME_PAGE_URL VERSION "/user-manual/"
  1339. /** Prefix for actions help links (append to USER_MANUAL_URL). */
  1340. #define ACTIONS_HELP_PREFIX "actions-file.html#"
  1341. /** Prefix for config option help links (append to USER_MANUAL_URL). */
  1342. #define CONFIG_HELP_PREFIX "config.html#"
  1343. /*
  1344. * The "hosts" to intercept and display CGI pages.
  1345. * First one is a hostname only, second one can specify host and path.
  1346. *
  1347. * Notes:
  1348. * 1) Do not specify the http: prefix
  1349. * 2) CGI_SITE_2_PATH must not end with /, one will be added automatically.
  1350. * 3) CGI_SITE_2_PATH must start with /, unless it is the empty string.
  1351. */
  1352. #define CGI_SITE_1_HOST "p.p"
  1353. #define CGI_SITE_2_HOST "config.privoxy.org"
  1354. #define CGI_SITE_2_PATH ""
  1355. /**
  1356. * The prefix for CGI pages. Written out in generated HTML.
  1357. * INCLUDES the trailing slash.
  1358. */
  1359. #define CGI_PREFIX "http://" CGI_SITE_2_HOST CGI_SITE_2_PATH "/"
  1360. #define CGI_PREFIX_HTTPS "https://" CGI_SITE_2_HOST CGI_SITE_2_PATH "/"
  1361. #endif /* ndef PROJECT_H_INCLUDED */
  1362. /*
  1363. Local Variables:
  1364. tab-width: 3
  1365. end:
  1366. */