aboutsummaryrefslogtreecommitdiff
path: root/src/domain.c
blob: 8bff39def2ca39efdfacf487dec1926d9b9e41b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * File: domain.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

#include <stdlib.h>

#include "../dlib/dlib.h"
#include "msg.h"
#include "list.h"
#include "domain.h"

typedef struct {
   char *origin;
   char *destination;
} Rule;

static Rule *exceptions = NULL;
static int num_exceptions = 0;
static int num_exceptions_max = 1;

static bool_t default_deny = FALSE;

/*
 * Parse domainrc.
 */
void a_Domain_parse(FILE *fp)
{
   char *line;
   uint_t lineno = 0;

   _MSG("Reading domainrc...\n");

   while ((line = dGetline(fp)) != NULL) {
      ++lineno;

      /* Remove leading and trailing whitespace */
      dStrstrip(line);

      if (line[0] && line[0] != '#') {
         const char *delim = " \t";
         char *tok1 = strtok(line, delim);
         char *tok2 = strtok(NULL, delim);

         if (strtok(NULL, delim) != NULL) {
            MSG("Domain: Ignoring extraneous text at end of line %u.\n",
                lineno);
         }
         if (!tok2) {
            MSG("Domain: Not enough fields in line %u.\n", lineno);
         } else {
            if (dStrAsciiCasecmp(tok1, "default") == 0) {
               if (dStrAsciiCasecmp(tok2, "deny") == 0) {
                  default_deny = TRUE;
                  MSG("Domain: Default deny.\n");
               } else if (dStrAsciiCasecmp(tok2, "accept") == 0) {
                  default_deny = FALSE;
                  MSG("Domain: Default accept.\n");
               } else {
                  MSG("Domain: Default action \"%s\" not recognised.\n", tok2);
               }
            } else {
               a_List_add(exceptions, num_exceptions, num_exceptions_max);
               exceptions[num_exceptions].origin = dStrdup(tok1);
               exceptions[num_exceptions].destination = dStrdup(tok2);
               num_exceptions++;
               _MSG("Domain: Exception from %s to %s.\n", tok1, tok2);
            }
         }
      }
      dFree(line);
   }
}

void a_Domain_freeall(void)
{
   int i = 0;

   for (i = 0; i < num_exceptions; i++) {
      dFree(exceptions[i].origin);
      dFree(exceptions[i].destination);
   }
   dFree(exceptions);
}

/*
 * Wildcard ('*') pattern always matches.
 * "example.org" pattern matches "example.org".
 * ".example.org" pattern matches "example.org" and "sub.example.org".
 */
static bool_t Domain_match(const char *host, const char *pattern) {
   int cmp = strcmp(pattern, "*");

   if (cmp) {
      if (pattern[0] != '.')
         cmp = dStrAsciiCasecmp(host, pattern);
      else {
         int diff = strlen(host) - strlen(pattern);

         if (diff == -1)
            cmp = dStrAsciiCasecmp(host, pattern + 1);
         else if (diff >= 0)
            cmp = dStrAsciiCasecmp(host + diff, pattern);
      }
   }
   return cmp ? FALSE : TRUE;
}

/*
 * Is the resource at 'source' permitted to request the resource at 'dest'?
 */
bool_t a_Domain_permit(const DilloUrl *source, const DilloUrl *dest)
{
   int i;
   bool_t ret;
   const char *source_host, *dest_host;

   if (default_deny == FALSE && num_exceptions == 0)
      return TRUE;

   source_host = URL_HOST(source);
   dest_host = URL_HOST(dest);

   if (dest_host[0] == '\0') {
      ret = source_host[0] == '\0' ||
            !dStrAsciiCasecmp(URL_SCHEME(dest), "data");
      if (ret == FALSE)
         MSG("Domain: DENIED %s -> %s.\n", source_host, URL_STR(dest));
      return ret;
   }

   if (a_Url_same_organization(source, dest))
      return TRUE;

   ret = default_deny ? FALSE : TRUE;

   for (i = 0; i < num_exceptions; i++) {
      if (Domain_match(source_host, exceptions[i].origin) &&
          Domain_match(dest_host, exceptions[i].destination)) {
         ret = default_deny;
         _MSG("Domain: Matched rule from %s to %s.\n", exceptions[i].origin,
              exceptions[i].destination);
         break;
      }
   }

   if (ret == FALSE) {
      const char *src = source_host[0] ? source_host : URL_STR(source);

      MSG("Domain: DENIED %s -> %s.\n", src, dest_host);
   }
   return ret;
}