1 /*
2 * Copyright (C) 2013-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
3 * Copyright (C) 2007-2013 Sourcefire, Inc.
4 *
5 * Authors: Tomasz Kojm
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #if HAVE_CONFIG_H
23 #include "clamav-config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifndef _WIN32
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #endif
33 #include <sys/stat.h>
34 #include <errno.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 // libclamav
40 #include "clamav.h"
41 #include "str.h"
42
43 // common
44 #include "optparser.h"
45 #include "output.h"
46 #include "misc.h"
47
48 #include "clamd_others.h"
49 #include "server.h"
50 #include "localserver.h"
51
52 #ifdef _WIN32
localserver(const struct optstruct * opts)53 int localserver(const struct optstruct *opts)
54 {
55 logg("!Localserver is not supported on this platform");
56 return -1;
57 }
58
59 #else
60
localserver(const struct optstruct * opts)61 int localserver(const struct optstruct *opts)
62 {
63 struct sockaddr_un server;
64 int sockfd = 0, backlog;
65 STATBUF foo;
66 char *estr;
67 char *sockdir;
68 char *pos;
69 struct stat sb;
70 int cnt;
71
72 int num_fd = sd_listen_fds(0);
73 if (num_fd > 2) {
74 logg("!LOCAL: Received more than two file descriptors from systemd.\n");
75 return -1;
76 } else if (num_fd > 0) {
77 /* use socket passed by systemd */
78 int i;
79 for (i = 0; i < num_fd; i += 1) {
80 sockfd = SD_LISTEN_FDS_START + i;
81 if (sd_is_socket(sockfd, AF_UNIX, SOCK_STREAM, 1) == 1) {
82 /* correct socket */
83 break;
84 } else {
85 /* wrong socket */
86 sockfd = -2;
87 }
88 }
89 if (sockfd == -2) {
90 logg("#LOCAL: No local AF_UNIX SOCK_STREAM socket received from systemd.\n");
91 return -2;
92 }
93 logg("#LOCAL: Received AF_UNIX SOCK_STREAM socket from systemd.\n");
94 return sockfd;
95 }
96 /* create socket */
97 memset((char *)&server, 0, sizeof(server));
98 server.sun_family = AF_UNIX;
99 strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
100 server.sun_path[sizeof(server.sun_path) - 1] = '\0';
101
102 pos = NULL;
103 if ((pos = strstr(server.sun_path, "/")) && (pos = strstr(((char *)pos + 1), "/"))) {
104 cnt = 0;
105 sockdir = NULL;
106 pos = server.sun_path + strlen(server.sun_path);
107 while (pos != server.sun_path) {
108 if (*pos == '/') {
109 sockdir = CLI_STRNDUP(server.sun_path, strlen(server.sun_path) - cnt);
110 break;
111 } else {
112 pos--;
113 cnt++;
114 }
115 }
116
117 if (stat(sockdir, &sb)) {
118 if (errno == ENOENT) {
119 mode_t old_umask;
120 mode_t sock_mode;
121 if (optget(opts, "LocalSocketMode")->enabled) {
122 char *end;
123 sock_mode = strtol(optget(opts, "LocalSocketMode")->strarg, &end, 8);
124
125 if (*end) {
126 logg("!Invalid LocalSocketMode %s\n", optget(opts, "LocalSocketMode")->strarg);
127 free(sockdir);
128 return -1;
129 }
130 } else {
131 sock_mode = 0777;
132 }
133
134 old_umask = umask(0011); /* allow mode 777 for socket directory */
135 if (mkdir(sockdir, sock_mode)) {
136 logg("!LOCAL: Could not create socket directory: %s: %s\n", sockdir, strerror(errno));
137 if (errno == ENOENT) {
138 logg("!LOCAL: Ensure parent directory exists.\n");
139 }
140 } else {
141 logg("Localserver: Creating socket directory: %s\n", sockdir);
142 }
143 umask(old_umask); /* restore umask */
144 }
145 }
146 free(sockdir);
147 }
148
149 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
150 estr = strerror(errno);
151 logg("!LOCAL: Socket allocation error: %s\n", estr);
152 return -1;
153 }
154
155 if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) == -1) {
156 if (errno == EADDRINUSE) {
157 if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) >= 0) {
158 logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path);
159 close(sockfd);
160 return -1;
161 }
162 if (optget(opts, "FixStaleSocket")->enabled) {
163 logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
164 if (unlink(server.sun_path) == -1) {
165 estr = strerror(errno);
166 logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr);
167 close(sockfd);
168 return -1;
169 }
170 if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) == -1) {
171 estr = strerror(errno);
172 logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
173 close(sockfd);
174 return -1;
175 }
176 } else if (CLAMSTAT(server.sun_path, &foo) != -1) {
177 logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
178 close(sockfd);
179 return -1;
180 }
181 } else {
182 estr = strerror(errno);
183 logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr);
184 close(sockfd);
185 return -1;
186 }
187 }
188
189 logg("#LOCAL: Unix socket file %s\n", server.sun_path);
190
191 backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
192 logg("#LOCAL: Setting connection queue length to %d\n", backlog);
193
194 if (listen(sockfd, backlog) == -1) {
195 estr = strerror(errno);
196 logg("!LOCAL: listen() error: %s\n", estr);
197 close(sockfd);
198 return -1;
199 }
200
201 return sockfd;
202 }
203 #endif
204