clamd/localserver.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
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>
3b074c78
 #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
 {
 	struct sockaddr_un server;
413ffee7
 	int sockfd = 0, backlog;
a2a004df
 	STATBUF foo;
e3aaff8e
 	char *estr;
ba36ab74
         char *sockdir;
         char *pos;
         struct stat sb;
         int cnt;
e3aaff8e
 
b68375fd
     int num_fd = sd_listen_fds(0);
     if (num_fd > 2)
     {
         logg("!LOCAL: 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_UNIX, SOCK_STREAM, 1) == 1)
             {
                 /* correct socket */
                 break;
             }
             else
             {
                 /* wrong socket */
                 sockfd = -2;
             }
         }
         if (sockfd == -2)
         {
             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 */
e3aaff8e
     memset((char *) &server, 0, sizeof(server));
     server.sun_family = AF_UNIX;
064b4a0c
     strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
72ce4b70
     server.sun_path[sizeof(server.sun_path)-1]='\0';
e3aaff8e
 
616fbf9f
     pos = NULL;
     if ((pos = strstr(server.sun_path, "/")) && (pos = strstr(((char*) pos + 1), "/"))) {
         cnt = 0;
         sockdir = NULL;
         pos = server.sun_path + strlen(server.sun_path);
         while (pos != server.sun_path) {
             if (*pos == '/') {
47a544dc
                 sockdir = cli_strndup(server.sun_path, strlen(server.sun_path) - cnt);
616fbf9f
                 break;
             }
             else {
                 pos--;
                 cnt++;
             }
ba36ab74
         }
 
616fbf9f
         if (stat(sockdir, &sb)) {
             if (errno == ENOENT) {
                 mode_t sock_mode;
                 if(optget(opts, "LocalSocketMode")->enabled) {
                     char *end;
                     sock_mode = strtol(optget(opts, "LocalSocketMode")->strarg, &end, 8);
ba36ab74
 
616fbf9f
                     if(*end) {
                         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");
                     }
                 }
                 else {
                     logg("Localserver: Creating socket directory: %s\n", sockdir);
                 }
ba36ab74
             }
         }
616fbf9f
         free(sockdir);
ba36ab74
     }
 
e3aaff8e
     if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
 	estr = strerror(errno);
e979398c
 	logg("!LOCAL: Socket allocation error: %s\n", estr);
bd8603aa
 	return -1;
e3aaff8e
     }
 
     if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
049a18b9
 	if(errno == EADDRINUSE) {
 	    if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) {
e979398c
 		logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path);
bd8603aa
 		close(sockfd);
 		return -1;
049a18b9
 	    }
064b4a0c
 	    if(optget(opts, "FixStaleSocket")->enabled) {
e979398c
 		logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
049a18b9
 		if(unlink(server.sun_path) == -1) {
 		    estr = strerror(errno);
e979398c
 		    logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr);
bd8603aa
 		    close(sockfd);
 		    return -1;
049a18b9
 		}
 		if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
 		    estr = strerror(errno);
e979398c
 		    logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
bd8603aa
 		    close(sockfd);
 		    return -1;
049a18b9
 		}
d9b6b8c7
 	    } else if(CLAMSTAT(server.sun_path, &foo) != -1) {
e979398c
 		logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
bd8603aa
 		close(sockfd);
 		return -1;
049a18b9
 	    }
 	} else {
 	    estr = strerror(errno);
e979398c
 	    logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr);
bd8603aa
 	    close(sockfd);
 	    return -1;
e3aaff8e
 	}
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
 
     if(listen(sockfd, backlog) == -1) {
 	estr = strerror(errno);
e979398c
 	logg("!LOCAL: listen() error: %s\n", estr);
bd8603aa
 	close(sockfd);
 	return -1;
e3aaff8e
     }
 
57358cc8
     return sockfd;
e3aaff8e
 }
3b074c78
 #endif