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
 
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>
81131381
 #include <sys/socket.h>
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"
 
 #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"
 
6d5c43a1
 static pthread_mutex_t ctime_mutex = PTHREAD_MUTEX_INITIALIZER;
81131381
 
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
 
e1fdb94b
 
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, CMD2, strlen(CMD2))) { /* RAWSCAN */
3805ebcb
 	opt = options & ~CL_SCAN_ARCHIVE;
a617b5ea
 	if(scan(buff + strlen(CMD2) + 1, NULL, engine, NULL, opt, 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
 	    struct stat foo;
7c225c08
 
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);
38432c53
 	if(stat(path, &foo) == -1)
 	    sprintf(path, "%s/daily.inc/daily.info", dbdir);
7c225c08
 
 	if((daily = cl_cvdhead(path))) {
 		time_t t = (time_t) daily->stime;
 
 	    pthread_mutex_lock(&ctime_mutex);
 	    mdprintf(desc, "ClamAV "VERSION"/%d/%s", daily->version, ctime(&t));
 	    pthread_mutex_unlock(&ctime_mutex);
 	    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
 
7708ddfc
     } else if(!strncmp(buff, CMD12, strlen(CMD12))) { /* FD */
 	    int fd = atoi(buff + strlen(CMD12) + 1);
 
a57e3d41
 	scanfd(fd, NULL, engine, limits, options, copt, desc);
7708ddfc
 	close(fd); /* FIXME: should we close it here? */
 
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
 
81131381
     } else {
 	mdprintf(desc, "UNKNOWN COMMAND\n");
     }
 
     return 0; /* no error and no 'special' command executed */
 }