clamd/localserver.c
e3aaff8e
 /*
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
086eab5c
  *
  *  Authors: Tomasz Kojm
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
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
288057e9
 #ifndef _WIN32
e3aaff8e
 #include <sys/socket.h>
 #include <sys/un.h>
477cfb97
 #endif
3b074c78
 #include <sys/stat.h>
e3aaff8e
 #include <errno.h>
ad3c01bf
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
e3aaff8e
 
bd8603aa
 #include "libclamav/clamav.h"
47a544dc
 #include "libclamav/str.h"
bd8603aa
 
064b4a0c
 #include "shared/optparser.h"
3b074c78
 #include "shared/output.h"
b68375fd
 #include "shared/misc.h"
bd8603aa
 
e3aaff8e
 #include "others.h"
 #include "server.h"
fc83da82
 #include "localserver.h"
e3aaff8e
 
3b074c78
 #ifdef _WIN32
064b4a0c
 int localserver(const struct optstruct *opts)
67118e92
 {
     logg("!Localserver is not supported on this platform");
     return -1;
 }
 
 #else
 
064b4a0c
 int localserver(const struct optstruct *opts)
e3aaff8e
 {
288057e9
     struct sockaddr_un server;
     int sockfd = 0, backlog;
     STATBUF foo;
     char *estr;
     char *sockdir;
     char *pos;
     struct stat sb;
     int cnt;
e3aaff8e
 
b68375fd
     int num_fd = sd_listen_fds(0);
288057e9
     if (num_fd > 2) {
b68375fd
         logg("!LOCAL: Received more than two file descriptors from systemd.\n");
         return -1;
288057e9
     } else if (num_fd > 0) {
b68375fd
         /* use socket passed by systemd */
         int i;
288057e9
         for (i = 0; i < num_fd; i += 1) {
b68375fd
             sockfd = SD_LISTEN_FDS_START + i;
288057e9
             if (sd_is_socket(sockfd, AF_UNIX, SOCK_STREAM, 1) == 1) {
b68375fd
                 /* correct socket */
                 break;
288057e9
             } else {
b68375fd
                 /* wrong socket */
                 sockfd = -2;
             }
         }
288057e9
         if (sockfd == -2) {
b68375fd
             logg("#LOCAL: No local AF_UNIX SOCK_STREAM socket received from systemd.\n");
             return -2;
         }
         logg("#LOCAL: Received AF_UNIX SOCK_STREAM socket from systemd.\n");
         return sockfd;
     }
     /* create socket */
288057e9
     memset((char *)&server, 0, sizeof(server));
e3aaff8e
     server.sun_family = AF_UNIX;
064b4a0c
     strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
288057e9
     server.sun_path[sizeof(server.sun_path) - 1] = '\0';
e3aaff8e
 
616fbf9f
     pos = NULL;
288057e9
     if ((pos = strstr(server.sun_path, "/")) && (pos = strstr(((char *)pos + 1), "/"))) {
         cnt     = 0;
616fbf9f
         sockdir = NULL;
288057e9
         pos     = server.sun_path + strlen(server.sun_path);
616fbf9f
         while (pos != server.sun_path) {
             if (*pos == '/') {
2e06875d
                 sockdir = CLI_STRNDUP(server.sun_path, strlen(server.sun_path) - cnt);
616fbf9f
                 break;
288057e9
             } else {
616fbf9f
                 pos--;
                 cnt++;
             }
ba36ab74
         }
 
616fbf9f
         if (stat(sockdir, &sb)) {
             if (errno == ENOENT) {
                 mode_t sock_mode;
288057e9
                 if (optget(opts, "LocalSocketMode")->enabled) {
616fbf9f
                     char *end;
                     sock_mode = strtol(optget(opts, "LocalSocketMode")->strarg, &end, 8);
ba36ab74
 
288057e9
                     if (*end) {
616fbf9f
                         logg("!Invalid LocalSocketMode %s\n", optget(opts, "LocalSocketMode")->strarg);
                         free(sockdir);
                         return -1;
                     }
                 } else {
                     sock_mode = 0777;
ba36ab74
                 }
 
616fbf9f
                 if (mkdir(sockdir, sock_mode)) {
                     logg("!LOCAL: Could not create socket directory: %s: %s\n", sockdir, strerror(errno));
                     if (errno == ENOENT) {
                         logg("!LOCAL: Ensure parent directory exists.\n");
                     }
288057e9
                 } else {
616fbf9f
                     logg("Localserver: Creating socket directory: %s\n", sockdir);
                 }
ba36ab74
             }
         }
616fbf9f
         free(sockdir);
ba36ab74
     }
 
288057e9
     if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
         estr = strerror(errno);
         logg("!LOCAL: Socket allocation error: %s\n", estr);
         return -1;
e3aaff8e
     }
 
288057e9
     if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) == -1) {
         if (errno == EADDRINUSE) {
             if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) >= 0) {
                 logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path);
                 close(sockfd);
                 return -1;
             }
             if (optget(opts, "FixStaleSocket")->enabled) {
                 logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
                 if (unlink(server.sun_path) == -1) {
                     estr = strerror(errno);
                     logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr);
                     close(sockfd);
                     return -1;
                 }
                 if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) == -1) {
                     estr = strerror(errno);
                     logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
                     close(sockfd);
                     return -1;
                 }
             } else if (CLAMSTAT(server.sun_path, &foo) != -1) {
                 logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
                 close(sockfd);
                 return -1;
             }
         } else {
             estr = strerror(errno);
             logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr);
             close(sockfd);
             return -1;
         }
049a18b9
     }
e3aaff8e
 
e979398c
     logg("#LOCAL: Unix socket file %s\n", server.sun_path);
e3aaff8e
 
064b4a0c
     backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
e979398c
     logg("#LOCAL: Setting connection queue length to %d\n", backlog);
e3aaff8e
 
288057e9
     if (listen(sockfd, backlog) == -1) {
         estr = strerror(errno);
         logg("!LOCAL: listen() error: %s\n", estr);
         close(sockfd);
         return -1;
e3aaff8e
     }
 
57358cc8
     return sockfd;
e3aaff8e
 }
3b074c78
 #endif