Blame view

libmailutils/server/ipsrv.c 10.8 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 2007, 2008, 2010 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
24
#include <sys/stat.h>
25
#include <netinet/in.h>
26
#include <sys/un.h>
27 28 29 30 31 32 33 34 35
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/acl.h>
#include <mailutils/server.h>
#include <mailutils/debug.h>
#include <mailutils/diag.h>
#include <mailutils/errno.h>
36
#include <mailutils/nls.h>
37 38


Sergey Poznyakoff authored
39
struct _mu_ip_server
40 41
{
  char *ident;
42 43
  struct sockaddr *addr;
  int addrlen;
Sergey Poznyakoff authored
44 45
  int fd;
  int type;
46
  mu_acl_t acl;
Sergey Poznyakoff authored
47 48 49
  mu_ip_server_conn_fp f_conn;
  mu_ip_server_intr_fp f_intr;
  mu_ip_server_free_fp f_free;
50
  void *data;
Sergey Poznyakoff authored
51 52 53 54 55 56 57 58 59 60 61 62 63
  union
  {
    struct
    {
      int backlog;
    } tcp_data;
    struct
    {
      char *buf;
      size_t bufsize;
      ssize_t rdsize;
    } udp_data;
  } v;
64 65 66 67 68
};

#define IDENTSTR(s) ((s)->ident ? (s)->ident : "default")

int
Sergey Poznyakoff authored
69 70
mu_ip_server_create (mu_ip_server_t *psrv, struct sockaddr *addr,
		     int addrlen, int type)
71
{
Sergey Poznyakoff authored
72 73 74 75 76 77 78 79 80 81 82
  struct _mu_ip_server *srv;

  switch (type)
    {
    case MU_IP_UDP:
    case MU_IP_TCP:
      break;
      
    default:
      return EINVAL;
    }
83 84 85 86
  
  srv = calloc (1, sizeof *srv);
  if (!srv)
    return ENOMEM;
87 88 89 90 91 92 93 94
  srv->addr = calloc (1, addrlen);
  if (!srv->addr)
    {
      free (srv);
      return ENOMEM;
    }
  memcpy (srv->addr, addr, addrlen);
  srv->addrlen = addrlen;
Sergey Poznyakoff authored
95
  srv->type = type;
96
  srv->fd = -1;
Sergey Poznyakoff authored
97 98 99 100 101 102 103 104 105 106
  switch (type)
    {
    case MU_IP_UDP:
      srv->v.udp_data.bufsize = 4096;
      break;
      
    case MU_IP_TCP:
      srv->v.tcp_data.backlog = 4;
    }
      
107 108 109 110 111
  *psrv = srv;
  return 0;
}

int
Sergey Poznyakoff authored
112
mu_ip_server_destroy (mu_ip_server_t *psrv)
113
{
Sergey Poznyakoff authored
114
  mu_ip_server_t srv;
115 116 117 118 119 120 121 122
  if (!psrv)
    return EINVAL;
  srv = *psrv;
  if (!srv)
    return 0;
  if (srv->f_free)
    srv->f_free (srv->data);
  close (srv->fd);
123
  free (srv->addr);
124
  free (srv->ident);
Sergey Poznyakoff authored
125 126
  if (srv->type == MU_IP_UDP && srv->v.udp_data.buf)
    free (srv->v.udp_data.buf);
127 128 129 130 131 132
  free (srv);
  *psrv = NULL;
  return 0;
}

int
Sergey Poznyakoff authored
133
mu_ip_server_get_type (mu_ip_server_t srv, int *ptype)
134 135 136
{
  if (!srv)
    return EINVAL;
Sergey Poznyakoff authored
137
  *ptype = srv->type;
138 139 140 141
  return 0;
}

int
Sergey Poznyakoff authored
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
mu_tcp_server_set_backlog (mu_ip_server_t srv, int backlog)
{
  if (!srv || srv->type != MU_IP_TCP)
    return EINVAL;
  srv->v.tcp_data.backlog = backlog;
  return 0;
}

int
mu_udp_server_get_bufsize (mu_ip_server_t srv, size_t *psize)
{
  if (!srv || srv->type != MU_IP_UDP)
    return EINVAL;
  *psize = srv->v.udp_data.bufsize;
  return 0;
}

int
mu_udp_server_set_bufsize (mu_ip_server_t srv, size_t size)
{
  if (!srv || srv->type != MU_IP_UDP)
    return EINVAL;
  srv->v.udp_data.bufsize = size;
  if (srv->v.udp_data.buf)
    {
      char *p = realloc (srv->v.udp_data.buf, size);
      if (!p)
	return ENOMEM;
      srv->v.udp_data.buf = p;
    }
  return 0;
}  

int
mu_ip_server_set_ident (mu_ip_server_t srv, const char *ident)
177 178 179 180 181 182 183 184 185 186 187 188
{
  if (!srv)
    return EINVAL;
  if (srv->ident)
    free (srv->ident);
  srv->ident = strdup (ident);
  if (!srv->ident)
    return ENOMEM;
  return 0;
}

int
Sergey Poznyakoff authored
189
mu_ip_server_set_acl (mu_ip_server_t srv, mu_acl_t acl)
190 191 192 193 194 195 196 197
{
  if (!srv)
    return EINVAL;
  srv->acl = acl;
  return 0;
}

int
Sergey Poznyakoff authored
198
mu_ip_server_set_conn (mu_ip_server_t srv, mu_ip_server_conn_fp conn)
199 200 201 202 203 204 205 206
{
  if (!srv)
    return EINVAL;
  srv->f_conn = conn;
  return 0;
}

int
Sergey Poznyakoff authored
207
mu_ip_server_set_intr (mu_ip_server_t srv, mu_ip_server_intr_fp intr)
208 209 210 211 212 213 214 215
{
  if (!srv)
    return EINVAL;
  srv->f_intr = intr;
  return 0;
}

int
Sergey Poznyakoff authored
216 217
mu_ip_server_set_data (mu_ip_server_t srv,
			void *data, mu_ip_server_free_fp free)
218 219 220 221 222 223 224 225
{
  if (!srv)
    return EINVAL;
  srv->data = data;
  srv->f_free = free;
  return 0;
}

Sergey Poznyakoff authored
226 227
int
mu_address_family_to_domain (int family)
228 229 230 231 232 233 234 235 236 237 238 239 240 241
{
  switch (family)
    {
    case AF_UNIX:
      return PF_UNIX;

    case AF_INET:
      return PF_INET;

    default:
      abort ();
    }
}

242
int
Sergey Poznyakoff authored
243
mu_ip_server_open (mu_ip_server_t srv)
244 245 246 247 248 249
{
  int fd;
  
  if (!srv || srv->fd != -1)
    return EINVAL;

250
  if (mu_debug_level_p (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0))
251 252
    {
      char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
253
      mu_debug_log ("opening server \"%s\" %s", IDENTSTR (srv), p);
254 255
      free (p);
    }
256

Sergey Poznyakoff authored
257 258
  fd = socket (mu_address_family_to_domain (srv->addr->sa_family),
	       ((srv->type == MU_IP_UDP) ? SOCK_DGRAM : SOCK_STREAM), 0);
259 260
  if (fd == -1)
    {
261 262
      mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		("%s: socket: %s", IDENTSTR (srv), mu_strerror (errno)));
263 264
      return errno;
    }
265 266 267 268 269 270 271 272 273 274 275 276
  
  switch (srv->addr->sa_family)
    {
    case AF_UNIX:
      {
	struct stat st;
	struct sockaddr_un *s_un = (struct sockaddr_un *) srv->addr;
	
	if (stat (s_un->sun_path, &st))
	  {
	    if (errno != ENOENT)
	      {
277 278
		mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
			  ("%s: file %s exists but cannot be stat'd: %s",
279 280
			   IDENTSTR (srv),
			   s_un->sun_path,
281
			   mu_strerror (errno)));
282 283 284 285 286
		return EAGAIN;
	      }
	  }
	else if (!S_ISSOCK (st.st_mode))
	  {
287 288 289
	    mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		      ("%s: file %s is not a socket",
		       IDENTSTR (srv), s_un->sun_path));
290 291 292 293
	    return EAGAIN;
	  }
	else if (unlink (s_un->sun_path))
	  {
294 295 296
            mu_debug (MU_DEBCAT_SERVER,  MU_DEBUG_ERROR,
		      ("%s: cannot unlink file %s: %s",
		       IDENTSTR (srv), s_un->sun_path, mu_strerror (errno)));
297 298 299 300
	    return EAGAIN;
	  }
      }
      break;
301

302 303 304 305 306 307 308 309 310 311
    case AF_INET:
      {
	int t;
	
	t = 1;	 
	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &t, sizeof (t));
      }
    }
  
  if (bind (fd, srv->addr, srv->addrlen) == -1)
312
    {
313 314
      mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		("%s: bind: %s", IDENTSTR (srv), mu_strerror (errno)));
315 316 317 318
      close (fd);
      return errno;
    }

Sergey Poznyakoff authored
319
  if (srv->type == MU_IP_TCP)
320
    {
Sergey Poznyakoff authored
321 322
      if (listen (fd, srv->v.tcp_data.backlog) == -1) 
	{
323 324
	  mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		    ("%s: listen: %s", IDENTSTR (srv), mu_strerror (errno)));
Sergey Poznyakoff authored
325 326 327
	  close (fd);
	  return errno;
	}
328 329 330 331 332 333 334
    }
  
  srv->fd = fd;
  return 0;
}

int
Sergey Poznyakoff authored
335
mu_ip_server_shutdown (mu_ip_server_t srv)
336 337 338
{
  if (!srv || srv->fd != -1)
    return EINVAL;
339
  if (mu_debug_level_p (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0))
340 341
    {
      char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
342
      mu_debug_log ("closing server \"%s\" %s", IDENTSTR (srv), p);
343 344
      free (p);
    }
345 346 347 348 349
  close (srv->fd);
  return 0;
}

int
Sergey Poznyakoff authored
350
mu_ip_tcp_accept (mu_ip_server_t srv, void *call_data)
351 352 353
{
  int rc;
  int connfd;
354 355 356
  union
  {
    struct sockaddr sa;
Sergey Poznyakoff authored
357 358
    struct sockaddr_in s_in;
    struct sockaddr_un s_un;
359 360
  } client;
  
361 362
  socklen_t size = sizeof (client);
  
Sergey Poznyakoff authored
363
  if (!srv || srv->fd == -1 || srv->type == MU_IP_UDP)
364 365
    return EINVAL;

366
  connfd = accept (srv->fd, &client.sa, &size);
367 368 369 370 371 372
  if (connfd == -1)
    {
      int ec = errno;
      if (ec == EINTR)
	{
	  if (srv->f_intr && srv->f_intr (srv->data, call_data))
Sergey Poznyakoff authored
373
	    mu_ip_server_shutdown (srv);
374 375 376 377 378 379 380
	}
      return ec;
    }

  if (srv->acl)
    {
      mu_acl_result_t res;
381
      int rc = mu_acl_check_sockaddr (srv->acl, &client.sa, size, &res);
382
      if (rc)
383 384 385
	mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		  ("%s: mu_acl_check_sockaddr: %s",
		   IDENTSTR (srv), strerror (rc)));
386 387
      if (res == mu_acl_result_deny)
	{
388
	  char *p = mu_sockaddr_to_astr (&client.sa, size);
389 390 391
	  mu_diag_output (MU_DIAG_INFO, "Denying connection from %s", p);
	  free (p);
	  
392 393 394 395
	  close (connfd);
	  return 0;
	}
    }
396
  rc = srv->f_conn (connfd, &client.sa, size, srv->data, call_data, srv);
397
  close (connfd);
398
  return rc;
399 400 401
}

int
Sergey Poznyakoff authored
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
mu_ip_udp_accept (mu_ip_server_t srv, void *call_data)
{
  int rc;
  union
  {
    struct sockaddr sa;
    struct sockaddr_in s_in;
    struct sockaddr_un s_un;
  } client;
  fd_set rdset;
  
  socklen_t salen = sizeof (client);
  ssize_t size;

  if (!srv->v.udp_data.buf)
    {
      srv->v.udp_data.buf = malloc (srv->v.udp_data.bufsize);
      if (!srv->v.udp_data.buf)
	return ENOMEM;
    }
  
  FD_ZERO (&rdset);
  FD_SET (srv->fd, &rdset);
  for (;;)
    {
      rc = select (srv->fd + 1, &rdset, NULL, NULL, NULL);
      if (rc == -1)
	{
	  if (errno == EINTR)
	    {
	      if (srv->f_intr && srv->f_intr (srv->data, call_data))
		break;
	      else
		continue;
	    }
	}
      else
	break;
    }

  if (rc == -1)
    return errno;

  size = recvfrom (srv->fd, srv->v.udp_data.buf, srv->v.udp_data.bufsize,
		   0, &client.sa, &salen);
  if (size < 0)
    {
449 450 451
      mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		("%s: recvfrom: %s",
		 IDENTSTR (srv), strerror (errno)));
Sergey Poznyakoff authored
452 453 454 455 456 457 458 459 460
      return MU_ERR_FAILURE;
    }
  srv->v.udp_data.rdsize = size;
  
  if (srv->acl)
    {
      mu_acl_result_t res;
      int rc = mu_acl_check_sockaddr (srv->acl, &client.sa, size, &res);
      if (rc)
461 462 463
	mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
		  ("%s: mu_acl_check_sockaddr: %s\n",
		   IDENTSTR (srv), strerror (rc)));
Sergey Poznyakoff authored
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
      if (res == mu_acl_result_deny)
	{
	  char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
	  mu_diag_output (MU_DIAG_INFO, "Denying connection from %s", p);
	  free (p);
	  return 0;
	}
    }
  rc = srv->f_conn (-1, &client.sa, size, srv->data, call_data, srv);
  return rc;
}

int
mu_ip_server_accept (mu_ip_server_t srv, void *call_data)
{
  int rc;
  if (!srv || srv->fd == -1)
    return EINVAL;
  switch (srv->type)
    {
    case MU_IP_UDP:
      rc = mu_ip_udp_accept (srv, call_data);
      break;

    case MU_IP_TCP:
      rc = mu_ip_tcp_accept (srv, call_data);
    }
  
  if (rc)
    mu_ip_server_shutdown (srv);
  return rc;
}

int
mu_ip_server_loop (mu_ip_server_t srv, void *call_data)
499 500 501 502 503
{
  if (!srv)
    return EINVAL;
  while (srv->fd != -1)
    {
Sergey Poznyakoff authored
504
      int rc = mu_ip_server_accept (srv, call_data);
505 506
      if (rc && rc != EINTR)
	{
Sergey Poznyakoff authored
507
	  mu_ip_server_shutdown (srv);
508 509 510 511 512 513 514
	  return rc;
	}
    }
  return 0;
}

int
Sergey Poznyakoff authored
515
mu_ip_server_get_fd (mu_ip_server_t srv)
516 517 518 519 520
{
  return srv->fd;
}

int
Sergey Poznyakoff authored
521 522 523 524 525 526 527 528 529 530 531
mu_udp_server_get_rdata (mu_ip_server_t srv, char **pbuf, size_t *pbufsize)
{
  if (!srv || srv->type != MU_IP_UDP)
    return EINVAL;
  *pbuf = srv->v.udp_data.buf;
  *pbufsize = srv->v.udp_data.rdsize;
  return 0;
}

int
mu_ip_server_get_sockaddr (mu_ip_server_t srv, struct sockaddr *s, int *size)
532
{
533 534
  int len;
  
535 536
  if (!srv || !s)
    return EINVAL;
537 538 539 540
  if (s == 0)
    len = srv->addrlen;
  else
    {
Sergey Poznyakoff authored
541 542 543
      len = srv->addrlen;
      if (*size < len)
	return MU_ERR_BUFSPACE;
544 545 546
      memcpy (s, srv->addr, len);
    }
  *size = len;
547 548 549
  return 0;
}