Dillo v3.1.1-91-g6d5b3ee3
Loading...
Searching...
No Matches
domain.c
Go to the documentation of this file.
1/*
2 * File: domain.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <stdlib.h>
11
12#include "../dlib/dlib.h"
13#include "msg.h"
14#include "list.h"
15#include "domain.h"
16
17typedef struct {
18 char *origin;
19 char *destination;
20} Rule;
21
22static Rule *exceptions = NULL;
23static int num_exceptions = 0;
24static int num_exceptions_max = 1;
25
27
31void a_Domain_parse(FILE *fp)
32{
33 char *line;
34 uint_t lineno = 0;
35
36 _MSG("Reading domainrc...\n");
37
38 while ((line = dGetline(fp)) != NULL) {
39 ++lineno;
40
41 /* Remove leading and trailing whitespace */
42 dStrstrip(line);
43
44 if (line[0] && line[0] != '#') {
45 const char *delim = " \t";
46 char *tok1 = strtok(line, delim);
47 char *tok2 = strtok(NULL, delim);
48
49 if (strtok(NULL, delim) != NULL) {
50 MSG("Domain: Ignoring extraneous text at end of line %u.\n",
51 lineno);
52 }
53 if (!tok2) {
54 MSG("Domain: Not enough fields in line %u.\n", lineno);
55 } else {
56 if (dStrAsciiCasecmp(tok1, "default") == 0) {
57 if (dStrAsciiCasecmp(tok2, "deny") == 0) {
59 MSG("Domain: Default deny.\n");
60 } else if (dStrAsciiCasecmp(tok2, "accept") == 0) {
62 MSG("Domain: Default accept.\n");
63 } else {
64 MSG("Domain: Default action \"%s\" not recognised.\n", tok2);
65 }
66 } else {
68 exceptions[num_exceptions].origin = dStrdup(tok1);
69 exceptions[num_exceptions].destination = dStrdup(tok2);
71 _MSG("Domain: Exception from %s to %s.\n", tok1, tok2);
72 }
73 }
74 }
75 dFree(line);
76 }
77}
78
80{
81 int i = 0;
82
83 for (i = 0; i < num_exceptions; i++) {
84 dFree(exceptions[i].origin);
85 dFree(exceptions[i].destination);
86 }
88}
89
95static bool_t Domain_match(const char *host, const char *pattern) {
96 int cmp = strcmp(pattern, "*");
97
98 if (cmp) {
99 if (pattern[0] != '.')
100 cmp = dStrAsciiCasecmp(host, pattern);
101 else {
102 int diff = strlen(host) - strlen(pattern);
103
104 if (diff == -1)
105 cmp = dStrAsciiCasecmp(host, pattern + 1);
106 else if (diff >= 0)
107 cmp = dStrAsciiCasecmp(host + diff, pattern);
108 }
109 }
110 return cmp ? FALSE : TRUE;
111}
112
116bool_t a_Domain_permit(const DilloUrl *source, const DilloUrl *dest)
117{
118 int i;
119 bool_t ret;
120 const char *source_host, *dest_host;
121
122 if (default_deny == FALSE && num_exceptions == 0)
123 return TRUE;
124
125 source_host = URL_HOST(source);
126 dest_host = URL_HOST(dest);
127
128 if (dest_host[0] == '\0') {
129 ret = source_host[0] == '\0' ||
130 !dStrAsciiCasecmp(URL_SCHEME(dest), "data");
131 if (ret == FALSE)
132 MSG("Domain: DENIED %s -> %s.\n", source_host, URL_STR(dest));
133 return ret;
134 }
135
136 if (a_Url_same_organization(source, dest))
137 return TRUE;
138
139 ret = default_deny ? FALSE : TRUE;
140
141 for (i = 0; i < num_exceptions; i++) {
142 if (Domain_match(source_host, exceptions[i].origin) &&
143 Domain_match(dest_host, exceptions[i].destination)) {
144 ret = default_deny;
145 _MSG("Domain: Matched rule from %s to %s.\n", exceptions[i].origin,
146 exceptions[i].destination);
147 break;
148 }
149 }
150
151 if (ret == FALSE) {
152 const char *src = source_host[0] ? source_host : URL_STR(source);
153
154 MSG("Domain: DENIED %s -> %s.\n", src, dest_host);
155 }
156 return ret;
157}
#define _MSG(...)
Definition bookmarks.c:45
#define MSG(...)
Definition bookmarks.c:46
unsigned int uint_t
Definition d_size.h:20
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:928
void dFree(void *mem)
Definition dlib.c:68
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:203
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
Definition dlib.c:122
char * dStrdup(const char *s)
Definition dlib.c:77
#define TRUE
Definition dlib.h:23
#define FALSE
Definition dlib.h:19
static int num_exceptions_max
Definition domain.c:24
static Rule * exceptions
Definition domain.c:22
static int num_exceptions
Definition domain.c:23
void a_Domain_freeall(void)
Definition domain.c:79
static bool_t Domain_match(const char *host, const char *pattern)
Wildcard ('*') pattern always matches.
Definition domain.c:95
bool_t a_Domain_permit(const DilloUrl *source, const DilloUrl *dest)
Is the resource at 'source' permitted to request the resource at 'dest'?
Definition domain.c:116
static bool_t default_deny
Definition domain.c:26
void a_Domain_parse(FILE *fp)
Parse domainrc.
Definition domain.c:31
#define a_List_add(list, num_items, alloc_step)
Definition cookies.c:68
Fast list methods.
Definition url.h:88
bool_t a_Url_same_organization(const DilloUrl *u1, const DilloUrl *u2)
Definition url.c:798
#define URL_STR(u)
Definition url.h:76
#define URL_SCHEME(u)
Definition url.h:70
#define URL_HOST(u)
Definition url.h:75