File: cmd.c

package info (click to toggle)
bld 0.3.4.1-4
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 576 kB
  • ctags: 268
  • sloc: ansic: 2,333; sh: 175; makefile: 157; perl: 135; python: 36
file content (269 lines) | stat: -rw-r--r-- 6,944 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*-
 * Copyright (c) 2004 Free (Olivier Beyssac)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/param.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include "options.h"
#include "cmd.h"
#include "iptree.h"
#include "ipinfo.h"
#include "utils.h"
#include "netlist.h"
#include "client.h"


#define MSG_BL    (421)
#define MSG_NOBL  (200)
#define MSG_AUTHF (600)


#define exiterror(ident, svc, svl, buf) \
  do {                                                          \
    syslog(LOG_ERR, "invalid request from %s: %s", ident, buf); \
    restorebuf(svc, svl, buf);                                  \
    return 500;                                                 \
  } while(0)


extern struct options opt;


/* Send notifications */
static void send_notifies(const char *ip)
{
  int i;
  pid_t pid;
  struct sigaction sa;

  if (!opt.notify_hosts)
    return;

  if ((pid = fork()) == -1) {
    syslog(LOG_ERR, "fork() error: %s", strerror(errno));
    return;
  }

  if (pid != 0)
    return;

  /* Detach from previous sigactions */
  xsigaction(sa, SIGTERM, SIG_DFL);
  xsigaction(sa, SIGINT, SIG_DFL);
  xsigaction(sa, SIGUSR1, SIG_IGN);
  xsigaction(sa, SIGUSR2, SIG_IGN);
  xsigaction(sa, SIGCHLD, SIG_IGN);

  for (i = 0; opt.notify_hosts[i]; i += 2) {
    int sd, reply;
    char *host = opt.notify_hosts[i];
    char *port = opt.notify_hosts[i+1];
    
    opt.notifies++;

    if ((sd = client_connect(host, port)) == -1) {
      syslog(LOG_ERR, "error (connect) while notifying %s to %s:%s",
	     ip, host, port);
    } else {
      if ((reply = client_send_cmdi(sd, CMD_INSERT, ip)) != 200
	  && reply != 421)
	syslog(LOG_ERR, "error (reply=%d) while notifying %s to %s:%s",
	       reply, ip, host, port);
      else
	syslog(LOG_INFO, "notified %s:%s about %s", host, port, ip);
      
      close(sd);
    }
  }

  exit(EXIT_SUCCESS);
}


/* Do the real stuff after command has been interpreted by read_cmd */
static int cmd_commit(const iptree ipt, const int cmd, const char *val,
		      const char *ident)
{
  struct in_addr inp;
  unsigned long ip;
  time_t t;
  int options = 0;
  int bl = 0;
  int notify = 0;
  int code = 200;
  int wl = 0;
  int count = 1;
  
  t = time(NULL);

  if (!inet_aton(val, &inp)) {
    syslog(LOG_ERR, "invalid IP address submitted by %s: %s", ident, val);
    return 500;
  }
  
  ip = ntohl(inp.s_addr);

  if (iptree_get_bl(ipt, ip, t)) {
    bl = 1;
    code = MSG_BL;
  }

  /* Check against whitelist */
  wl = (netlist_getmode(opt.whitelist, ip) == 1);
  
  if (cmd == CMD_DECR) {
    opt.decrqueries++;
    if (wl) {
      syslog(LOG_INFO, "%s (wl) decremented by %s", val, ident);
      return code;
    }
    count = -1;
  } else if (cmd == CMD_INSERT) {
    opt.insertqueries++;
    options |= IPINFO_OPT_FORCED;
    if (wl) {
      syslog(LOG_INFO, "%s (wl) inserted by %s", val, ident);
      return code;
      
    }
  } else if (cmd == CMD_SUBMIT) {
    opt.submissions++;
    if (wl) {
      syslog(LOG_INFO, "%s (wl) submitted by %s", val, ident);
      return code;
    }
  }
  
  switch (cmd) {
  case CMD_DECR:
    /* Decrement IP count */
  case CMD_INSERT:
    /* IP insertion */
  case CMD_SUBMIT:
    /* IP submission */
    if (iptree_add(ipt, ip, t, t, count, options)) {
      if (cmd == CMD_SUBMIT) {
	syslog(LOG_INFO, "%s submitted by %s", val, ident);
	if (iptree_get_bl(ipt, ip, t)) {
	  notify = 1;
	}
      } else if (cmd == CMD_DECR) {
	syslog(LOG_INFO, "%s decremented by %s", val, ident);
      } else if (cmd == CMD_INSERT && !bl) {
	syslog(LOG_INFO, "%s inserted by %s", val, ident);
	notify = 1;
      }
      /* If the IP has just been put in the blacklist, send notifies */
      if (!bl && notify) {
	send_notifies(val);
	code = MSG_BL;
      }
    } else
      syslog(LOG_ERR, "error in iptree_add IP %s", val);
    
    break;

  case CMD_QUERY:
    /* Check if an IP is blacklisted or not */
    opt.blqueries++;

    if (bl) {
      opt.positive_blqueries++;
      return MSG_BL;
    } else
      return MSG_NOBL;
    break;
  }

  return code;
}


/* Read the command pointed to by cmd and update iptree
   Return the code to show to client */
extern int read_cmd(char *buf, const ssize_t len, const char *ident,
		    const iptree ipt, const int mode)
{
  char *p;
  char svc, svctmp;
  ssize_t svl;
  int cmd = CMD_NO;
  int code;
  
  if (buf[len-1] == '\n' && buf[len-2] == '\r')
    savebufpos(svc, svl, buf, len-2);
  else {
    savebufpos(svc, svl, buf, len);
    exiterror(ident, svc, svl, buf);
  }

  if ((p = strchr(buf, '=')) == NULL)
    exiterror(ident, svc, svl, buf);

  svctmp = *p;
  *p = '\0';
  if (strcmp(buf, "ip") == 0)
    cmd = CMD_SUBMIT;
  else if (strcmp(buf, "ip?") == 0)
    cmd = CMD_QUERY;
  else if (strcmp(buf, "ipbl") == 0)
    cmd = CMD_INSERT;
  else if (strcmp(buf, "ipdecr") == 0)
    cmd = CMD_DECR;
  else {
    opt.bad_requests++;
  }

  *p++ = svctmp;

  /* Check client request against ACL */
  if ((cmd == CMD_SUBMIT && !(mode & ACL_M_SUBMIT))
      || (cmd == CMD_QUERY && !(mode & ACL_M_QUERY))
      || (cmd == CMD_INSERT && !(mode & ACL_M_INSERT))
      || (cmd == CMD_DECR && !(mode & ACL_M_DECR))) {
    syslog(LOG_INFO, "denied request from %s", ident);
    opt.denied_requests++;
    restorebuf(svc, svl, buf);
    return MSG_AUTHF;
  }
    
  if (cmd == CMD_NO || (code = cmd_commit(ipt, cmd, p, ident)) == 0)
    exiterror(ident, svc, svl, buf);

  restorebuf(svc, svl, buf);

  return code;
}