aboutsummaryrefslogtreecommitdiff
path: root/src/server.c
blob: e514f37307286901d9c7c5cd6cbfe3db87659892 (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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* This file is part of tagr.
   Copyright (C) 2000, 2005, 2006, 2009 Max Bouglacoff, Sergey Poznyakoff

   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, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include <tagr.h>
#include <report.h>
#include <inttypes.h>
#include <c-strcase.h>
#include <wordsplit.h>

unsigned stream_idle_timeout = 10;

struct tagr_server
{
  struct tagr_server *next;
  char *id;
  enum tagr_server_type type;
  int fd;
  struct grecs_sockaddr addr;
};
  
static struct tagr_server *server_head, *server_tail;

struct server_class
{
  int (*srv_open) (struct tagr_server *);
  void (*srv_run) (struct tagr_server *);
  void (*srv_close) (struct tagr_server *);
};

static void
common_close (struct tagr_server *srv)
{
  close (srv->fd);
}

static int
setup_socket (struct tagr_server *srv)
{
  int rc;
  
  if ((rc = fcntl (srv->fd, F_GETFD, 0)) == -1
      || fcntl (srv->fd, F_SETFD, rc | FD_CLOEXEC) == -1)
    logmsg (L_ERR, _("cannot set close-on-exec: %s"),
	    strerror (errno));
  
  switch (srv->addr.sa->sa_family)
    {
    case PF_INET:
      {
	int yes = 1;
	if (setsockopt (srv->fd, SOL_SOCKET, SO_REUSEADDR,
			(void *) &yes, sizeof(yes)) == -1)
	  logmsg (L_ERR, _("setting reuseaddr failed: %s"),
		  strerror (errno));
      }
      break;

    case PF_UNIX:
      {
	struct sockaddr_un *s_un =
	  (struct sockaddr_un *) srv->addr.sa;
	if (unlink (s_un->sun_path))
	  logmsg (L_ERR, _("cannot remove socket %s: %s"),
		  s_un->sun_path, strerror (errno));
      }
    }
  
  if (bind (srv->fd, srv->addr.sa, srv->addr.len) < 0)
    {
      logmsg (L_ERR, "bind: %s", strerror (errno));
      close (srv->fd);
      srv->fd = -1;
      return 1;
    }
  return 0;
}  

static int udp_open (struct tagr_server *);
static void udp_run (struct tagr_server *);
static int tcp_open (struct tagr_server *);
static void tcp_run (struct tagr_server *);

struct server_class server_tab[] = {
  { udp_open, udp_run, common_close },
  { tcp_open, tcp_run, common_close }
};


void
register_server (const char *id,
		 enum tagr_server_type type, struct grecs_sockaddr addr)
{
  struct tagr_server *srv = xmalloc (sizeof (*srv));
  srv->id = xstrdup (id);
  srv->type = type;
  srv->fd = -1;
  srv->addr = addr;

  srv->next = NULL;
  if (server_tail)
    server_tail->next =srv;
  else
    server_head = srv;
  server_tail = srv;
}

void
open_servers ()
{
  struct tagr_server *prev = NULL;
  struct tagr_server *srv = server_head;
  
  while (srv)
    {
      struct tagr_server *next = srv->next;
      if (server_tab[srv->type].srv_open (srv))
	{
	  free (srv->id);
	  free (srv->addr.sa);
	  free (srv);
	  if (prev)
	    prev->next = next;
	  else
	    server_head = next;
	}
      else
	prev = srv;
      srv = next;
    }

  if (!server_head)
    logmsg (L_CRIT, _("no servers configured"));
  
  server_tail = prev;
}

void
close_servers ()
{
  struct tagr_server *srv = server_head;
  
  while (srv)
    {
      struct tagr_server *next = srv->next;
      server_tab[srv->type].srv_close (srv);
      free (srv->id);
      free (srv->addr.sa);
      free (srv);
      srv = next;
    }
  server_head = server_tail = NULL;
}

void
subprocess (void (*fun) (void *data), void *data, int fd)
{
  int child = 0;
  if (!single_process_option)
    {
      pid_t pid = fork ();
      if (pid > 0)
	return;
      else if (pid < 0)
	logmsg (L_ERR, _("cannot fork: %s"), strerror (errno));
      else
	{
	  signal (SIGHUP, SIG_DFL);
	  signal (SIGCHLD, SIG_DFL);
	  signal (SIGALRM, SIG_DFL);
	  child = 1;
	  if (fd != -1)
	    close (fd);
	}
    }
  fun (data);
  if (child)
    exit (0);
}


void
decode_buffer (void *recv_buffer)
{
  int i;
  Stat_reply *reply;
  Stat *sp;
  int child = 0;

  reply = (Stat_reply *) recv_buffer;
  reply->n_addr = ntohl (reply->n_addr);

  if (reply->n_addr > MAXADDR)
    {
      logmsg (L_NOTICE, _("got invalid packet: n_addr = %d"),
	      reply->n_addr);
      return;
    }
  reply->timestamp = ntohl (reply->timestamp);
  if (verbose_level)
    {
      char tbuf[sizeof("2009-04-01 00:00:00")];
      strftime (tbuf, sizeof tbuf, "%Y-%m-%d %H:%M:%S",
		gmtime (&reply->timestamp));
      logmsg (L_INFO, _("Received packet: %d %lu - %s"), reply->n_addr,
	      (unsigned long) reply->timestamp, tbuf);
    }
  
  sp = reply->stat;

  if (open_db (TAGR_DB_WR) == 0)
    {
      for (i = 0; i < reply->n_addr; i++, sp++)
	{
	  sp->in = ntohl (sp->in);
	  sp->out = ntohl (sp->out);
	  verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out);
	  report (sp, reply->timestamp);
	}
      close_db ();
    }
}

static int
udp_open (struct tagr_server *srv)
{
  srv->fd = socket (srv->addr.sa->sa_family, SOCK_DGRAM, 0);

  if (srv->fd < 0)
    {
      logmsg (L_ERR, "socket: %s", strerror (errno));
      return 1;
    }

  return setup_socket (srv);
}

static void
udp_run (struct tagr_server *srv)
{
  char recv_buffer[reply_size (MAXADDR)];
  struct sockaddr saremote;
  int salen = sizeof saremote;
  int rc = recvfrom (srv->fd, recv_buffer,
		     sizeof (recv_buffer), 0, &saremote, &salen);
  if (rc > 0)
    subprocess (decode_buffer, recv_buffer, -1);
  else if (rc < 0 && errno != EINTR)
    logmsg (L_ERR, "recvfrom: %s", strerror (errno));
}



static int
tcp_open (struct tagr_server *srv)
{
  srv->fd = socket (srv->addr.sa->sa_family, SOCK_STREAM, 0);

  if (srv->fd < 0)
    {
      logmsg (L_ERR, "socket: %s", strerror (errno));
      return 1;
    }

  if (setup_socket (srv))
    return 1;

  if (listen (srv->fd, 8))
    {
      logmsg (L_ERR, "listen: %s", strerror (errno));
      close (srv->fd);
      return 1;
    }
  return 0;
}

void
trim_crlf (char *buf)
{
  size_t len = strlen (buf);
  if (len > 0 && buf[len-1] == '\n')
    {
      buf[--len] = 0;
      if (len > 0 && buf[len-1] == '\r')
	buf[--len] = 0;
    }
}

static int
convert_ts (const char *str, time_t *pt)
{
  uintmax_t val;
  char *p;
  
  val = strtoumax (str, &p, 10);
  if (*p)
    return 1;
  *pt = val;
  if (*pt != val)
    return 1;
  return 0;
}
  
static int
convert_ulong (const char *str, unsigned long *pt)
{
  uintmax_t val;
  char *p;
  
  val = strtoumax (str, &p, 10);
  if (*p)
    return 1;
  *pt = val;
  if (*pt != val)
    return 1;
  return 0;
}

static RETSIGTYPE
sig_child_alrm (int sig)
{
  logmsg (L_NOTICE, _("child timed out"));
  exit (EX_TEMPFAIL);
}

enum tagr_dialog_state
  {
    initial_dialog_state,
    auth_dialog_state,
    quit_dialog_state
  };

struct tagr_dialog
{
  enum tagr_dialog_state state;
  FILE *in;
  FILE *out;
};

#define STATEMASK(n) (1u<<(n))
#define ANYSTATE ((unsigned)~0)

struct command
{
  const char *name;
  unsigned statemask;
  int minargs;
  int maxargs;
  void (*handler) (struct tagr_dialog *, struct command *,
		   struct wordsplit *);
};

static void
cmd_auth (struct tagr_dialog *dlg, struct command *cmd,
	  struct wordsplit *ws)
{
  if (tagr_auth (ws->ws_wordv[1], ws->ws_wordv[2]) == 0)
    {
      dlg->state = auth_dialog_state;
      fprintf (dlg->out, "+OK welcome, %s\r\n", ws->ws_wordv[1]);
    }
  else
    fprintf (dlg->out, "-ERR authorization failed\r\n");
}

static void
cmd_sample (struct tagr_dialog *dlg, struct command *cmd,
	    struct wordsplit *ws)
{
  Stat st;
  time_t t;
  
  if (strlen (ws->ws_wordv[1]) > MAX_NAME_LENGTH)
    {
      fprintf (dlg->out, "-ERR id too long\r\n");
      return;
    }

  strcpy (st.name, ws->ws_wordv[1]);
  if (convert_ts (ws->ws_wordv[2], &t)
      || convert_ulong (ws->ws_wordv[3], &st.in)
      || convert_ulong (ws->ws_wordv[4], &st.out))
    {
      fprintf (dlg->out, "-ERR invalid input\r\n");
      return;
    }

  if (open_db (TAGR_DB_WR))
    fprintf (dlg->out, "-TEMP database not available\r\n");
  else
    {
      report (&st, t);
      close_db ();
      fprintf (dlg->out, "+OK thank you\r\n");
    }
}

static void
cmd_quit (struct tagr_dialog *dlg, struct command *cmd,
	  struct wordsplit *ws)
{
  dlg->state = quit_dialog_state;
  fprintf (dlg->out, "+OK bye\r\n");
}

static struct command command_tab[] = {
  { "AUTH", STATEMASK (initial_dialog_state), 3, 3, cmd_auth },
  { "SAMPLE", STATEMASK (auth_dialog_state), 5, 5, cmd_sample },
  { "QUIT",   ANYSTATE, 1, 1, cmd_quit },
  { NULL }
};

static struct command *
find_command (const char *name)
{
  struct command *cmd;
  
  for (cmd = command_tab; cmd->name; cmd++)
    if (c_strcasecmp (cmd->name, name) == 0)
      return cmd;
  return NULL;
}
    

static void
tcp_server_loop (void *data)
{
  int fd = *(int*)data;
  struct tagr_dialog dlg;
  size_t bufsize = 0;
  char *buf = NULL;
  const char *authid;

  signal (SIGALRM, sig_child_alrm);

  dlg.state = initial_dialog_state;
  dlg.in = fdopen (fd, "r");
  dlg.out = fdopen (fd, "w");
  setvbuf (dlg.in, NULL, _IOLBF, 0);
  setvbuf (dlg.out, NULL, _IOLBF, 0);

  authid = tagr_auth_init ();
  fprintf (dlg.out, "+OK tagr experimental stream service %s\r\n", authid);
  while (dlg.state != quit_dialog_state
	 && getline (&buf, &bufsize, dlg.in) > 0)
    {
      struct wordsplit ws;
      struct command *cmd;
      
      alarm (stream_idle_timeout);
      trim_crlf (buf);
      if (wordsplit (buf, &ws, WRDSF_DEFFLAGS))
	{
	  logmsg (L_ERR, _("failed to parse input line"));
	  fprintf (dlg.out, "-ERR parsing failed\r\n");
	  continue;
	}

      if (ws.ws_wordc == 0)
	fprintf (dlg.out, "-ERR invalid input\r\n");
      else if (cmd = find_command (ws.ws_wordv[0]))
	{
	  if (!(STATEMASK (dlg.state) & cmd->statemask))
	    fprintf (dlg.out, "-ERR wrong state\r\n");
	  else if (ws.ws_wordc < cmd->minargs
	      || (cmd->maxargs > 0 && ws.ws_wordc > cmd->maxargs))
	    fprintf (dlg.out, "-ERR invalid number of arguments\r\n");
	  else
	    cmd->handler (&dlg, cmd, &ws);
	}
      else
	fprintf (dlg.out, "-ERR invalid input\r\n");	
      wordsplit_free (&ws);
    }
  
  free (buf);
  fclose (dlg.in);
  fclose (dlg.out);
  close (fd);
}

static void
tcp_run (struct tagr_server *srv)
{
  union
  {
    struct sockaddr sa;
    struct sockaddr_in s_in;
    struct sockaddr_un s_un;
  } addr;
  socklen_t len = sizeof (addr);
  
  int fd = accept (srv->fd, &addr.sa, &len);
  
  if (fd == -1)
    {
      logmsg (L_ERR, "accept: %s", strerror (errno));
      return;
    }

  subprocess (tcp_server_loop, &fd, srv->fd);
  close (fd);
}

void
server_loop ()
{
  fd_set read_fds;
  int max_fd = 0;
  struct tagr_server *srv;

  FD_ZERO (&read_fds);
  for (srv = server_head; srv; srv = srv->next)
    {
      FD_SET (srv->fd, &read_fds);
      if (srv->fd > max_fd)
	max_fd = srv->fd;
    }

  while (tagr_idle () == 0)
    {
      fd_set rfds = read_fds;
      int rc = select (max_fd + 1, &rfds, NULL, NULL, NULL);
      if (rc < 0)
	{
	  if (errno != EINTR)
	    logmsg (L_ERR, "select: %s", strerror (errno));
	  continue;
	}

      for (srv = server_head; srv; srv = srv->next)
	if (FD_ISSET (srv->fd, &rfds))
	  server_tab[srv->type].srv_run (srv);
    }
}
	      
	  
  

Return to:

Send suggestions and report system problems to the System administrator.