Citadel API clean up.
[citadel.git] / citadel / modules / netfilter / serv_netfilter.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel designed to filter idiots off the network.
5  * 
6  * Copyright (c) 2002-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "user_ops.h"
55 #include "policy.h"
56 #include "database.h"
57 #include "msgbase.h"
58 #include "serv_network.h"       /* Needed for defenition of FilterList */
59
60
61 #include "ctdl_module.h"
62
63
64 /*
65  * This handler detects whether an incoming network message is from some
66  * moron user who the site operator has elected to filter out.  If a match
67  * is found, the message is rejected.
68  */
69 int filter_the_idiots(struct CtdlMessage *msg, char *target_room) {
70         FilterList *fptr;
71         int zap_user = 0;
72         int zap_room = 0;
73         int zap_node = 0;
74
75         if ( (msg == NULL) || (filterlist == NULL) ) {
76                 return(0);
77         }
78
79         for (fptr = filterlist; fptr != NULL; fptr = fptr->next) {
80
81                 zap_user = 0;
82                 zap_room = 0;
83                 zap_node = 0;
84
85                 if (msg->cm_fields['A'] != NULL) {
86                         if ( (!strcasecmp(msg->cm_fields['A'], fptr->fl_user))
87                            || (fptr->fl_user[0] == 0) ) {
88                                 zap_user = 1;
89                         }
90                 }
91
92                 if (msg->cm_fields['C'] != NULL) {
93                         if ( (!strcasecmp(msg->cm_fields['C'], fptr->fl_room))
94                            || (fptr->fl_room[0] == 0) ) {
95                                 zap_room = 1;
96                         }
97                 }
98
99                 if (msg->cm_fields['O'] != NULL) {
100                         if ( (!strcasecmp(msg->cm_fields['O'], fptr->fl_room))
101                            || (fptr->fl_room[0] == 0) ) {
102                                 zap_room = 1;
103                         }
104                 }
105
106                 if (msg->cm_fields['N'] != NULL) {
107                         if ( (!strcasecmp(msg->cm_fields['N'], fptr->fl_node))
108                            || (fptr->fl_node[0] == 0) ) {
109                                 zap_node = 1;
110                         }
111                 }
112         
113                 if (zap_user + zap_room + zap_node == 3) return(1);
114
115         }
116
117         return(0);
118 }
119
120
121 CTDL_MODULE_INIT(netfilter)
122 {
123         if (!threading)
124         {
125                 CtdlRegisterNetprocHook(filter_the_idiots);
126         }
127         
128         /* return our Subversion id for the Log */
129         return "$Id$";
130 }