clamav-devel/clamd/session.c
81131381
 /*
e871e527
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
81131381
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  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
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
81131381
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
31e6c6fb
 #include <sys/time.h>
81131381
 #include <pthread.h>
 #include <time.h>
 #include <signal.h>
c238ac42
 #include <errno.h>
81131381
 
afb48b28
 #include "cfgparser.h"
81131381
 #include "others.h"
 #include "scanner.h"
 #include "server.h"
 #include "clamuko.h"
 #include "session.h"
afb48b28
 #include "str.h" /* libclamav */
7c225c08
 #include "clamav.h"
afb48b28
 #include "output.h"
7c225c08
 #include "memory.h"
81131381
 
6d5c43a1
 static pthread_mutex_t ctime_mutex = PTHREAD_MUTEX_INITIALIZER;
81131381
 
e1fdb94b
 int command(int desc, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt, int timeout)
81131381
 {
 	char buff[1025];
6a31c2b4
 	int bread, opt, retval;
1e35d807
 	struct cfgstruct *cpt;
31e6c6fb
 
e1fdb94b
 
1e35d807
     retval = poll_fd(desc, timeout);
c5e7d5cd
     switch (retval) {
     case 0: /* timeout */
1d8b9788
 	return -2;
c5e7d5cd
     case -1:
 	mdprintf(desc, "ERROR\n");
 	logg("!Command: poll_fd failed.\n");
 	return -1;
c238ac42
     }
81131381
 
7708ddfc
     while((bread = readsock(desc, buff, 1024)) == -1 && errno == EINTR);
d6b5ae47
 
45905a4a
     if(bread == 0) {
 	/* Connection closed */
 	return -1;
     }
3574e737
 
     if(bread < 0) {
81131381
 	logg("!Command parser: read() failed.\n");
 	/* at least try to display this error message */
6c9455c2
 	/* mdprintf(desc, "ERROR: Command parser: read() failed.\n"); */
81131381
 	return -1;
     }
 
     buff[bread] = 0;
afb48b28
     cli_chomp(buff);
81131381
 
     if(!strncmp(buff, CMD1, strlen(CMD1))) { /* SCAN */
6c507c40
 	if(scan(buff + strlen(CMD1) + 1, NULL, root, limits, options, copt, desc, 0) == -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;
6c507c40
 	if(scan(buff + strlen(CMD2) + 1, NULL, root, NULL, opt, copt, desc, 0) == -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 */
6c507c40
 	if(scan(buff + strlen(CMD6) + 1, NULL, root, limits, options, copt, desc, 1) == -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;
 
 	if(!(path = mmalloc(strlen(dbdir) + 11))) {
 	    mdprintf(desc, "Memory allocation error - SHUTDOWN forced\n");
 	    return COMMAND_SHUTDOWN;
 	}
 
 	sprintf(path, "%s/daily.cvd", dbdir);
 
 	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 */
6c507c40
 	if(scanstream(desc, NULL, root, 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);
 
 	scanfd(fd, NULL, root, limits, options, copt, desc, 0);
 	close(fd); /* FIXME: should we close it here? */
 
81131381
     } else {
 	mdprintf(desc, "UNKNOWN COMMAND\n");
     }
 
     return 0; /* no error and no 'special' command executed */
 }