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