1 | /*
|
---|
2 | * Copyright (C) 2009 Sourcefire, Inc.
|
---|
3 | *
|
---|
4 | * Authors: Tomasz Kojm, aCaB
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License version 2 as
|
---|
8 | * published by the Free Software Foundation.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
---|
18 | * MA 02110-1301, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #if HAVE_CONFIG_H
|
---|
22 | #include "clamav-config.h"
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #ifdef HAVE_UNISTD_H
|
---|
28 | #include <unistd.h>
|
---|
29 | #endif
|
---|
30 | #include <string.h>
|
---|
31 | #include <sys/types.h>
|
---|
32 | #include <sys/stat.h>
|
---|
33 | #ifdef HAVE_SYS_LIMITS_H
|
---|
34 | #include <sys/limits.h>
|
---|
35 | #endif
|
---|
36 | #ifdef HAVE_SYS_SELECT_H
|
---|
37 | #include <sys/select.h>
|
---|
38 | #endif
|
---|
39 | #ifndef _WIN32
|
---|
40 | #include <sys/socket.h>
|
---|
41 | #include <sys/un.h>
|
---|
42 | #include <netinet/in.h>
|
---|
43 | #include <arpa/inet.h>
|
---|
44 | #include <netdb.h>
|
---|
45 | #include <utime.h>
|
---|
46 | #endif
|
---|
47 | #include <errno.h>
|
---|
48 | #include <dirent.h>
|
---|
49 | #include <fcntl.h>
|
---|
50 |
|
---|
51 | #ifdef HAVE_SYS_UIO_H
|
---|
52 | #include <sys/uio.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #include "shared/optparser.h"
|
---|
56 | #include "shared/output.h"
|
---|
57 | #include "shared/misc.h"
|
---|
58 | #include "shared/actions.h"
|
---|
59 | #include "shared/clamdcom.h"
|
---|
60 |
|
---|
61 | #include "libclamav/str.h"
|
---|
62 | #include "libclamav/others.h"
|
---|
63 |
|
---|
64 | #include "client.h"
|
---|
65 | #include "proto.h"
|
---|
66 |
|
---|
67 | #ifndef INADDR_LOOPBACK
|
---|
68 | #define INADDR_LOOPBACK 0x7f000001
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | struct sockaddr *mainsa = NULL;
|
---|
72 | int mainsasz;
|
---|
73 | unsigned long int maxstream;
|
---|
74 | #ifndef _WIN32
|
---|
75 | static struct sockaddr_un nixsock;
|
---|
76 | #endif
|
---|
77 | static struct sockaddr_in tcpsock;
|
---|
78 | extern struct optstruct *clamdopts;
|
---|
79 |
|
---|
80 | /* Inits the communication layer
|
---|
81 | * Returns 0 if clamd is local, non zero if clamd is remote */
|
---|
82 | static int isremote(const struct optstruct *opts) {
|
---|
83 | int s, ret;
|
---|
84 | const struct optstruct *opt;
|
---|
85 | const char *clamd_conf = optget(opts, "config-file")->strarg;
|
---|
86 | static struct sockaddr_in testsock;
|
---|
87 |
|
---|
88 | #ifndef _WIN32
|
---|
89 | if((opt = optget(clamdopts, "LocalSocket"))->enabled) {
|
---|
90 | memset((void *)&nixsock, 0, sizeof(nixsock));
|
---|
91 | nixsock.sun_family = AF_UNIX;
|
---|
92 | strncpy(nixsock.sun_path, opt->strarg, sizeof(nixsock.sun_path));
|
---|
93 | nixsock.sun_path[sizeof(nixsock.sun_path)-1]='\0';
|
---|
94 | #ifdef C_OS2
|
---|
95 | if (strncmp(nixsock.sun_path,"\\socket\\",8))
|
---|
96 | sprintf(nixsock.sun_path,"\\socket\\%s",opt->strarg);
|
---|
97 | #endif
|
---|
98 | mainsa = (struct sockaddr *)&nixsock;
|
---|
99 | mainsasz = sizeof(nixsock);
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 | if(!(opt = optget(clamdopts, "TCPSocket"))->enabled)
|
---|
104 | return 0;
|
---|
105 |
|
---|
106 | mainsa = (struct sockaddr *)&tcpsock;
|
---|
107 | mainsasz = sizeof(tcpsock);
|
---|
108 |
|
---|
109 | if (cfg_tcpsock(clamdopts, &tcpsock, INADDR_LOOPBACK) == -1) {
|
---|
110 | logg("!Can't lookup clamd hostname: %s.\n", strerror(errno));
|
---|
111 | mainsa = NULL;
|
---|
112 | return 0;
|
---|
113 | }
|
---|
114 | memcpy((void *)&testsock, (void *)&tcpsock, sizeof(testsock));
|
---|
115 | testsock.sin_port = htons(INADDR_ANY);
|
---|
116 | if(!(s = socket(testsock.sin_family, SOCK_STREAM, 0))) return 0;
|
---|
117 | ret = (bind(s, (struct sockaddr *)&testsock, sizeof(testsock)) != 0);
|
---|
118 | closesocket(s);
|
---|
119 | return ret;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /* Turns a relative path into an absolute one
|
---|
124 | * Returns a pointer to the path (which must be
|
---|
125 | * freed by the caller) or NULL on error */
|
---|
126 | static char *makeabs(const char *basepath) {
|
---|
127 | int namelen;
|
---|
128 | char *ret;
|
---|
129 |
|
---|
130 | if(!(ret = malloc(PATH_MAX + 1))) {
|
---|
131 | logg("^Can't make room for fullpath.\n");
|
---|
132 | return NULL;
|
---|
133 | }
|
---|
134 | if(!cli_is_abspath(basepath)) {
|
---|
135 | if(!getcwd(ret, PATH_MAX)) {
|
---|
136 | logg("^Can't get absolute pathname of current working directory.\n");
|
---|
137 | free(ret);
|
---|
138 | return NULL;
|
---|
139 | }
|
---|
140 | #if defined(_WIN32) || defined(C_OS2)
|
---|
141 | if(*basepath == '\\') {
|
---|
142 | namelen = 2;
|
---|
143 | basepath++;
|
---|
144 | } else
|
---|
145 | #endif
|
---|
146 | namelen = strlen(ret);
|
---|
147 | snprintf(&ret[namelen], PATH_MAX - namelen, PATHSEP"%s", basepath);
|
---|
148 | } else {
|
---|
149 | strncpy(ret, basepath, PATH_MAX);
|
---|
150 | }
|
---|
151 | ret[PATH_MAX] = '\0';
|
---|
152 | return ret;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Recursively scans a path with the given scantype
|
---|
156 | * Returns non zero for serious errors, zero otherwise */
|
---|
157 | static int client_scan(const char *file, int scantype, int *infected, int *err, int maxlevel, int session, int flags) {
|
---|
158 | int ret;
|
---|
159 | char *fullpath = makeabs(file);
|
---|
160 |
|
---|
161 | if(!fullpath)
|
---|
162 | return 0;
|
---|
163 | if (!session)
|
---|
164 | ret = serial_client_scan(fullpath, scantype, infected, err, maxlevel, flags);
|
---|
165 | else
|
---|
166 | ret = parallel_client_scan(fullpath, scantype, infected, err, maxlevel, flags);
|
---|
167 | free(fullpath);
|
---|
168 | return ret;
|
---|
169 | }
|
---|
170 |
|
---|
171 | int get_clamd_version(const struct optstruct *opts)
|
---|
172 | {
|
---|
173 | char *buff;
|
---|
174 | int len, sockd;
|
---|
175 | struct RCVLN rcv;
|
---|
176 |
|
---|
177 | isremote(opts);
|
---|
178 | if(!mainsa) return 2;
|
---|
179 | if((sockd = dconnect()) < 0) return 2;
|
---|
180 | recvlninit(&rcv, sockd);
|
---|
181 |
|
---|
182 | if(sendln(sockd, "zVERSION", 9)) {
|
---|
183 | closesocket(sockd);
|
---|
184 | return 2;
|
---|
185 | }
|
---|
186 |
|
---|
187 | while((len = recvln(&rcv, &buff, NULL))) {
|
---|
188 | if(len == -1) {
|
---|
189 | logg("!Error occoured while receiving version information.\n");
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | printf("%s\n", buff);
|
---|
193 | }
|
---|
194 |
|
---|
195 | closesocket(sockd);
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 |
|
---|
199 | int reload_clamd_database(const struct optstruct *opts)
|
---|
200 | {
|
---|
201 | char *buff;
|
---|
202 | int len, sockd;
|
---|
203 | struct RCVLN rcv;
|
---|
204 |
|
---|
205 | isremote(opts);
|
---|
206 | if(!mainsa) return 2;
|
---|
207 | if((sockd = dconnect()) < 0) return 2;
|
---|
208 | recvlninit(&rcv, sockd);
|
---|
209 |
|
---|
210 | if(sendln(sockd, "zRELOAD", 8)) {
|
---|
211 | closesocket(sockd);
|
---|
212 | return 2;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if(!(len = recvln(&rcv, &buff, NULL)) || len < 10 || memcmp(buff, "RELOADING", 9)) {
|
---|
216 | logg("!Clamd did not reload the database\n");
|
---|
217 | closesocket(sockd);
|
---|
218 | return 2;
|
---|
219 | }
|
---|
220 | closesocket(sockd);
|
---|
221 | return 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | int client(const struct optstruct *opts, int *infected, int *err)
|
---|
225 | {
|
---|
226 | int remote, scantype, session = 0, errors = 0, scandash = 0, maxrec, flags = 0;
|
---|
227 | const char *fname;
|
---|
228 |
|
---|
229 | scandash = (opts->filename && opts->filename[0] && !strcmp(opts->filename[0], "-") && !optget(opts, "file-list")->enabled && !opts->filename[1]);
|
---|
230 | remote = isremote(opts) | optget(opts, "stream")->enabled;
|
---|
231 | #ifdef HAVE_FD_PASSING
|
---|
232 | if(!remote && optget(clamdopts, "LocalSocket")->enabled && (optget(opts, "fdpass")->enabled || scandash)) {
|
---|
233 | scantype = FILDES;
|
---|
234 | session = optget(opts, "multiscan")->enabled;
|
---|
235 | } else
|
---|
236 | #endif
|
---|
237 | if(remote || scandash) {
|
---|
238 | scantype = STREAM;
|
---|
239 | session = optget(opts, "multiscan")->enabled;
|
---|
240 | } else if(optget(opts, "multiscan")->enabled) scantype = MULTI;
|
---|
241 | else scantype = CONT;
|
---|
242 |
|
---|
243 | maxrec = optget(clamdopts, "MaxDirectoryRecursion")->numarg;
|
---|
244 | maxstream = optget(clamdopts, "StreamMaxLength")->numarg;
|
---|
245 | if (optget(clamdopts, "FollowDirectorySymlinks")->enabled)
|
---|
246 | flags |= CLI_FTW_FOLLOW_DIR_SYMLINK;
|
---|
247 | if (optget(clamdopts, "FollowFileSymlinks")->enabled)
|
---|
248 | flags |= CLI_FTW_FOLLOW_FILE_SYMLINK;
|
---|
249 | flags |= CLI_FTW_TRIM_SLASHES;
|
---|
250 |
|
---|
251 | if(!mainsa) {
|
---|
252 | logg("!Clamd is not configured properly.\n");
|
---|
253 | return 2;
|
---|
254 | }
|
---|
255 |
|
---|
256 | *infected = 0;
|
---|
257 |
|
---|
258 | if(scandash) {
|
---|
259 | int sockd, ret;
|
---|
260 | struct stat sb;
|
---|
261 | fstat(0, &sb);
|
---|
262 | if((sb.st_mode & S_IFMT) != S_IFREG) scantype = STREAM;
|
---|
263 | if((sockd = dconnect()) >= 0 && (ret = dsresult(sockd, scantype, NULL, &ret, NULL)) >= 0)
|
---|
264 | *infected = ret;
|
---|
265 | else
|
---|
266 | errors = 1;
|
---|
267 | if(sockd >= 0) closesocket(sockd);
|
---|
268 | } else if(opts->filename || optget(opts, "file-list")->enabled) {
|
---|
269 | if(opts->filename && optget(opts, "file-list")->enabled)
|
---|
270 | logg("^Only scanning files from --file-list (files passed at cmdline are ignored)\n");
|
---|
271 |
|
---|
272 | while((fname = filelist(opts, NULL))) {
|
---|
273 | if(!strcmp(fname, "-")) {
|
---|
274 | logg("!Scanning from standard input requires \"-\" to be the only file argument\n");
|
---|
275 | continue;
|
---|
276 | }
|
---|
277 | errors += client_scan(fname, scantype, infected, err, maxrec, session, flags);
|
---|
278 | /* this may be too strict
|
---|
279 | if(errors >= 10) {
|
---|
280 | logg("!Too many errors\n");
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | */
|
---|
284 | }
|
---|
285 | } else {
|
---|
286 | errors = client_scan("", scantype, infected, err, maxrec, session, flags);
|
---|
287 | }
|
---|
288 | return *infected ? 1 : (errors ? 2 : 0);
|
---|
289 | }
|
---|