clamd/tcpserver.c
e3aaff8e
 /*
c442ca9c
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
086eab5c
  *
  *  Authors: Tomasz Kojm, Török Edvin
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  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, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
98ac8d19
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
ad3c01bf
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
e0bb54d7
 #ifndef	_WIN32
e3aaff8e
 #include <sys/socket.h>
 #include <netinet/in.h>
2d70a403
 #include <arpa/inet.h>
f8f80da9
 #include <netdb.h>
67118e92
 #endif
e0bb54d7
 #include <errno.h>
e3aaff8e
 
bd8603aa
 #include "libclamav/clamav.h"
 
064b4a0c
 #include "shared/optparser.h"
bd8603aa
 #include "shared/output.h"
3b074c78
 #include "shared/misc.h"
bd8603aa
 
e3aaff8e
 #include "others.h"
 #include "server.h"
fc83da82
 #include "tcpserver.h"
a9d3aa14
 
8c241c57
 int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struct optstruct *opts)
e3aaff8e
 {
8c241c57
     struct addrinfo hints, *info, *p;
d6968402
     char host[NI_MAXHOST], serv[NI_MAXSERV];
8c241c57
     int *sockets;
413ffee7
     int sockfd = 0, backlog;
8c241c57
     int *t;
     char *estr, port[10];
     int yes = 1;
     int res;
f0e1c2e6
     unsigned int i=0;
b68375fd
 	int num_fd;
e3aaff8e
 
8c241c57
     sockets = *lsockets;
e3aaff8e
 
b68375fd
     num_fd = sd_listen_fds(0);
     if (num_fd > 2)
     {
         logg("!TCP: Received more than two file descriptors from systemd.\n");
         return -1;
     }
     else if (num_fd > 0)
     {
         /* use socket passed by systemd */
         int i;
         for(i = 0; i < num_fd; i += 1)
         {
             sockfd = SD_LISTEN_FDS_START + i;
             if (sd_is_socket(sockfd, AF_INET, SOCK_STREAM, 1) == 1)
             {
                 /* correct socket */
                 logg("#TCP: Received AF_INET SOCK_STREAM socket from systemd.\n");
                 break;
             }
             else if (sd_is_socket(sockfd, AF_INET6, SOCK_STREAM, 1) == 1)
             {
                 /* correct socket */
                 logg("#TCP: Received AF_INET6 SOCK_STREAM socket from systemd.\n");
                 break;
             }
             else
             {
                 /* wrong socket */
                 sockfd = -2;
             }
         }
         if (sockfd == -2)
         {
             logg("#TCP: No tcp AF_INET/AF_INET6 SOCK_STREAM socket received from systemd.\n");
             return -2;
         }
 
         t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
         if (!(t)) {
             return -1;
         }
         sockets = t;
 
         sockets[*nlsockets] = sockfd;
         (*nlsockets)++;
         *lsockets = sockets;
         return 0;
     }
 
     /* create socket */
8c241c57
     snprintf(port, sizeof(port), "%lld", optget(opts, "TCPSocket")->numarg);
e3aaff8e
 
8c241c57
     memset(&hints, 0x00, sizeof(struct addrinfo));
     hints.ai_family = AF_UNSPEC;
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_flags = AI_PASSIVE;
0ee809e8
 
d6968402
 #ifdef AI_ADDRCONFIG
     hints.ai_flags |= AI_ADDRCONFIG;
f0e1c2e6
 #endif
 
8c241c57
     if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
d6968402
         logg("!TCP: getaddrinfo failed: %s\n", gai_strerror(res));
8c241c57
         return -1;
8139fd99
     }
e3aaff8e
 
f0e1c2e6
     for (p = info; p != NULL; p = p->ai_next, i++) {
8c241c57
         t = realloc(sockets, sizeof(int) * (*nlsockets + 1));
         if (!(t)) {
3a831904
             for (i=0; i < *nlsockets; i++)
                 close(sockets[i]);
 
8c241c57
             return -1;
         }
         sockets = t;
 
         if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
             estr = strerror(errno);
             logg("!TCP: socket() error: %s\n", estr);
             continue;
         }
 
         if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &yes, sizeof(yes)) == -1) {
             logg("!TCP: setsocktopt(SO_REUSEADDR) error: %s\n", strerror(errno));
         }
 
d6968402
 #ifdef IPV6_V6ONLY
         if (p->ai_family == AF_INET6 &&
             setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
             estr = strerror(errno);
             logg("!TCP: setsocktopt(IPV6_V6ONLY) error: %s\n", estr);
         }
 #endif /* IPV6_V6ONLY */
 
cc071bf1
 #ifdef HAVE_GETNAMEINFO
d6968402
         if ((res = getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host),
                                serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV))) {
             logg("!TCP: getnameinfo failed: %s\n", gai_strerror(res));
             host[0] = '\0';
             serv[0] = '\0';
         }
cc071bf1
 #else
1fdcd49f
 		if (ipaddr) {
 			strncpy(host, ipaddr, sizeof(host));
 			host[sizeof(host)-1] = '\0';
 		} else
 			host[0] = '\0';
cc071bf1
         snprintf(serv, sizeof(serv), "%u", (unsigned int)(optget(opts, "TCPSocket")->numarg));
 #endif
8c241c57
         if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
             estr = strerror(errno);
d6968402
             logg("!TCP: Cannot bind to [%s]:%s: %s\n", host, serv, estr);
8c241c57
             closesocket(sockfd);
e3aaff8e
 
8c241c57
             continue;
         }
d6968402
         logg("#TCP: Bound to [%s]:%s\n", host, serv);
8c241c57
 
         backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
         logg("#TCP: Setting connection queue length to %d\n", backlog);
 
         if(listen(sockfd, backlog) == -1) {
             estr = strerror(errno);
d6968402
             logg("!TCP: Cannot listen on [%s]:%s: %s\n", host, serv, estr);
8c241c57
             closesocket(sockfd);
 
             continue;
         }
 
         sockets[*nlsockets] = sockfd;
         (*nlsockets)++;
e3aaff8e
     }
 
8c241c57
     freeaddrinfo(info);
     *lsockets = sockets;
 
     return 0;
e3aaff8e
 }