removed IGnet functions from text client
[citadel.git] / citadel / modules / network / serv_netfilter.c
1 /*
2  * A server-side module for Citadel designed to filter idiots off the network.
3  * 
4  * Copyright (c) 2002-2017 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <sys/wait.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <libcitadel.h>
40 #include "citadel.h"
41 #include "server.h"
42 #include "citserver.h"
43 #include "support.h"
44 #include "config.h"
45 #include "control.h"
46 #include "user_ops.h"
47 #include "database.h"
48 #include "msgbase.h"
49
50
51 #include "ctdl_module.h"
52
53 typedef struct FilterList FilterList;
54
55 struct FilterList {
56         FilterList *next;
57         char fl_user[SIZ];
58         char fl_room[SIZ];
59         char fl_node[SIZ];
60 };
61
62 struct FilterList *filterlist = NULL;
63
64 /*
65  * Keep track of what messages to reject
66  */
67 FilterList *load_filter_list(void) {
68         char *serialized_list = NULL;
69         int i;
70         char buf[SIZ];
71         FilterList *newlist = NULL;
72         FilterList *nptr;
73
74         serialized_list = CtdlGetSysConfig(FILTERLIST);
75         if (serialized_list == NULL) return(NULL); /* if null, no entries */
76
77         /* Use the string tokenizer to grab one line at a time */
78         for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
79                 extract_token(buf, serialized_list, i, '\n', sizeof buf);
80                 nptr = (FilterList *) malloc(sizeof(FilterList));
81                 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
82                 striplt(nptr->fl_user);
83                 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
84                 striplt(nptr->fl_room);
85                 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
86                 striplt(nptr->fl_node);
87
88                 /* Cowardly refuse to add an any/any/any entry that would
89                  * end up filtering every single message.
90                  */
91                 if (IsEmptyStr(nptr->fl_user) && 
92                     IsEmptyStr(nptr->fl_room) &&
93                     IsEmptyStr(nptr->fl_node)) {
94                         free(nptr);
95                 }
96                 else {
97                         nptr->next = newlist;
98                         newlist = nptr;
99                 }
100         }
101
102         free(serialized_list);
103         return newlist;
104 }
105
106
107 void free_filter_list(FilterList *fl) {
108         if (fl == NULL) return;
109         free_filter_list(fl->next);
110         free(fl);
111 }
112
113 void free_netfilter_list(void)
114 {
115         free_filter_list(filterlist);
116         filterlist = NULL;
117 }
118
119 void load_network_filter_list(void)
120 {
121         filterlist = load_filter_list();
122 }
123
124
125 /*
126  * This handler detects whether an incoming network message is from some
127  * moron user who the site operator has elected to filter out.  If a match
128  * is found, the message is rejected.
129  */
130 int filter_the_idiots(struct CtdlMessage *msg, char *target_room) {
131         FilterList *fptr;
132         int zap_user = 0;
133         int zap_room = 0;
134         int zap_node = 0;
135
136         if ( (msg == NULL) || (filterlist == NULL) ) {
137                 return(0);
138         }
139
140         for (fptr = filterlist; fptr != NULL; fptr = fptr->next) {
141
142                 zap_user = 0;
143                 zap_room = 0;
144                 zap_node = 0;
145
146                 if (!CM_IsEmpty(msg, eAuthor)) {
147                         if ( (!strcasecmp(msg->cm_fields[eAuthor], fptr->fl_user))
148                            || (fptr->fl_user[0] == 0) ) {
149                                 zap_user = 1;
150                         }
151                 }
152
153                 if (!CM_IsEmpty(msg, eRemoteRoom)) {
154                         if ( (!strcasecmp(msg->cm_fields[eRemoteRoom], fptr->fl_room))
155                            || (fptr->fl_room[0] == 0) ) {
156                                 zap_room = 1;
157                         }
158                 }
159
160                 if (!CM_IsEmpty(msg, eOriginalRoom)) {
161                         if ( (!strcasecmp(msg->cm_fields[eOriginalRoom], fptr->fl_room))
162                            || (fptr->fl_room[0] == 0) ) {
163                                 zap_room = 1;
164                         }
165                 }
166
167                 if (!CM_IsEmpty(msg, eNodeName)) {
168                         if ( (!strcasecmp(msg->cm_fields[eNodeName], fptr->fl_node))
169                            || (fptr->fl_node[0] == 0) ) {
170                                 zap_node = 1;
171                         }
172                 }
173         
174                 if (zap_user + zap_room + zap_node == 3) return(1);
175
176         }
177
178         return(0);
179 }
180
181
182 CTDL_MODULE_INIT(netfilter)
183 {
184         if (!threading)
185         {
186 /*
187   currently unsupported.
188                 CtdlRegisterNetprocHook(filter_the_idiots);
189 */
190         }
191         
192         /* return our module name for the log */
193         return "netfilter";
194 }