clamd/session.c
81131381
 /*
a617b5ea
  *  Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net>
81131381
  *
  *  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.
81131381
  *
  *  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.
81131381
  */
 
67118e92
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
98ac8d19
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
725a2969
 #if defined HAVE_FD_PASSING && defined FDPASS_NEED_XOPEN
 /* to expose BSD 4.4/Unix98 semantics instead of BSD 4.3 semantics */
 #define _XOPEN_SOURCE 500
 #endif
 
81131381
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
67118e92
 #ifdef	HAVE_UNISTD_H
81131381
 #include <unistd.h>
67118e92
 #endif
81131381
 #include <sys/types.h>
67118e92
 #ifndef	C_WINDOWS
8765287e
 #include <dirent.h>
725a2969
 
81131381
 #include <sys/socket.h>
725a2969
 #ifdef HAVE_FD_PASSING
cb4e478c
 #ifdef HAVE_SYS_UIO_H
725a2969
 #include <sys/uio.h>
 #endif
cb4e478c
 #endif
725a2969
 
31e6c6fb
 #include <sys/time.h>
67118e92
 #endif
81131381
 #include <pthread.h>
 #include <time.h>
c238ac42
 #include <errno.h>
67118e92
 #include <stddef.h>
81131381
 
bd8603aa
 #include "libclamav/clamav.h"
 #include "libclamav/str.h"
9e751804
 #include "libclamav/others.h"
bd8603aa
 
 #include "shared/cfgparser.h"
 #include "shared/output.h"
53725d8c
 #include "shared/misc.h"
bd8603aa
 
81131381
 #include "others.h"
 #include "scanner.h"
 #include "server.h"
 #include "session.h"
 
725a2969
 #ifdef HAVE_FD_PASSING
 static int recvfd_and_scan(int desc, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options, const struct cfgstruct *copt)
 {
 	struct msghdr msg;
 	struct cmsghdr *cmsg;
 	unsigned char buf[CMSG_SPACE(sizeof(int))];
 	struct iovec iov[1];
 	char dummy;
 	int ret=-1;
 
 	memset(&msg, 0, sizeof(msg));
 	iov[0].iov_base = &dummy;
 	iov[0].iov_len = 1;
 	msg.msg_iov = iov;
 	msg.msg_iovlen = 1;
 	msg.msg_control = buf;
 	msg.msg_controllen = sizeof(buf);
 
 	if (recvmsg(desc, &msg, 0) == -1) {
 	    logg("recvmsg failed: %s!", strerror(errno));
 	    return -1;
 	}
 	if ((msg.msg_flags & MSG_TRUNC) || (msg.msg_flags & MSG_CTRUNC)) {
 	    logg("control message truncated");
 	    return -1;
 	}
 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
 		if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
 		    cmsg->cmsg_level == SOL_SOCKET &&
 		    cmsg->cmsg_type == SCM_RIGHTS) {
 			int fd = *(int *)CMSG_DATA(cmsg);
 			ret = scanfd(fd, NULL, engine, limits, options, copt, desc);
 			close(fd);
 		}
 	}
 	return ret;
 }
 
 #else
ad4ae516
 static int recvfd_and_scan(int desc, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options, const struct cfgstruct *copt)
725a2969
 {
 	mdprintf(desc, "ERROR: FILDES support not compiled in\n");
ad4ae516
 	return -1;
725a2969
 }
 #endif
 
a57e3d41
 int command(int desc, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options, const struct cfgstruct *copt, int timeout)
81131381
 {
 	char buff[1025];
bd8603aa
 	int bread, opt;
31e6c6fb
 
442684f8
     bread = readsock(desc, buff, sizeof(buff)-1, '\n', timeout, 0, 1);
     if(bread == -2) /* timeout */
1d8b9788
 	return -2;
442684f8
     if(bread == 0) /* Connection closed */
45905a4a
 	return -1;
3574e737
     if(bread < 0) {
442684f8
 	mdprintf(desc, "ERROR\n");
 	logg("!Command: readsock() failed.\n");
81131381
 	return -1;
     }
 
     buff[bread] = 0;
afb48b28
     cli_chomp(buff);
81131381
 
     if(!strncmp(buff, CMD1, strlen(CMD1))) { /* SCAN */
a617b5ea
 	if(scan(buff + strlen(CMD1) + 1, NULL, engine, limits, options, copt, desc, TYPE_SCAN) == -2)
81837459
 	    if(cfgopt(copt, "ExitOnOOM")->enabled)
f9593781
 		return COMMAND_SHUTDOWN;
81131381
 
     } else if(!strncmp(buff, CMD3, strlen(CMD3))) { /* QUIT */
7c225c08
 	return COMMAND_SHUTDOWN;
81131381
 
     } else if(!strncmp(buff, CMD4, strlen(CMD4))) { /* RELOAD */
 	mdprintf(desc, "RELOADING\n");
 	return COMMAND_RELOAD;
 
     } else if(!strncmp(buff, CMD5, strlen(CMD5))) { /* PING */
 	mdprintf(desc, "PONG\n");
 
     } else if(!strncmp(buff, CMD6, strlen(CMD6))) { /* CONTSCAN */
a617b5ea
 	if(scan(buff + strlen(CMD6) + 1, NULL, engine, limits, options, copt, desc, TYPE_CONTSCAN) == -2)
81837459
 	    if(cfgopt(copt, "ExitOnOOM")->enabled)
f9593781
 		return COMMAND_SHUTDOWN;
81131381
 
     } else if(!strncmp(buff, CMD7, strlen(CMD7))) { /* VERSION */
81837459
 	    const char *dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
7c225c08
 	    char *path;
 	    struct cl_cvd *daily;
 
38432c53
 
8ca8a18e
 	if(!(path = malloc(strlen(dbdir) + 30))) {
7c225c08
 	    mdprintf(desc, "Memory allocation error - SHUTDOWN forced\n");
 	    return COMMAND_SHUTDOWN;
 	}
 
 	sprintf(path, "%s/daily.cvd", dbdir);
cb9d09c2
 	if(access(path, R_OK))
 	    sprintf(path, "%s/daily.cld", dbdir);
7c225c08
 
cb9d09c2
 	if(!access(path, R_OK) && (daily = cl_cvdhead(path))) {
9e751804
 		char timestr[32];
7c225c08
 		time_t t = (time_t) daily->stime;
 
9e751804
 	    mdprintf(desc, "ClamAV "VERSION"/%d/%s", daily->version, cli_ctime(&t, timestr, sizeof(timestr)));
7c225c08
 	    cl_cvdfree(daily);
 	} else {
 	    mdprintf(desc, "ClamAV "VERSION"\n");
 	}
 
 	free(path);
81131381
 
     } else if(!strncmp(buff, CMD8, strlen(CMD8))) { /* STREAM */
a57e3d41
 	if(scanstream(desc, NULL, engine, limits, options, copt) == CL_EMEM)
81837459
 	    if(cfgopt(copt, "ExitOnOOM")->enabled)
f9593781
 		return COMMAND_SHUTDOWN;
81131381
 
     } else if(!strncmp(buff, CMD9, strlen(CMD9))) { /* SESSION */
45905a4a
 	return COMMAND_SESSION;
81131381
 
     } else if(!strncmp(buff, CMD10, strlen(CMD10))) { /* END */
 	return COMMAND_END;
 
     } else if(!strncmp(buff, CMD11, strlen(CMD11))) { /* SHUTDOWN */
7c225c08
 	return COMMAND_SHUTDOWN;
81131381
 
8765287e
     } else if(!strncmp(buff, CMD13, strlen(CMD13))) { /* MULTISCAN */
a617b5ea
 	if(scan(buff + strlen(CMD13) + 1, NULL, engine, limits, options, copt, desc, TYPE_MULTISCAN) == -2)
 	    if(cfgopt(copt, "ExitOnOOM")->enabled)
 		return COMMAND_SHUTDOWN;
8765287e
 
725a2969
     } else if(!strncmp(buff, CMD14, strlen(CMD14))) { /* FILDES */
 	if(recvfd_and_scan(desc, engine, limits, options, copt) == -2)
 	    if(cfgopt(copt, "ExitOnOOM")->enabled)
 		return COMMAND_SHUTDOWN;
81131381
     } else {
 	mdprintf(desc, "UNKNOWN COMMAND\n");
     }
 
     return 0; /* no error and no 'special' command executed */
 }