clamd/clamuko.c
b151ef55
 /*
8b242bb9
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #ifdef CLAMUKO
 
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <signal.h>
 #include <pthread.h>
 #include <clamav.h>
 
 #include "server.h"
 #include "others.h"
36f2038b
 #include "cfgparser.h"
b151ef55
 #include "dazukoio.h"
 #include "clamuko.h"
 #include "defaults.h"
36f2038b
 #include "output.h"
b151ef55
 
e6b842b3
 struct dazuko_access *acc;
b151ef55
 
 void clamuko_exit(int sig)
 {
 
     logg("*Clamuko: clamuko_exit(), signal %d\n", sig);
 
     if(clamuko_scanning) {
e6b842b3
 	logg("*Clamuko: stopped while scanning %s\n", acc->filename);
 	acc->deny = 0;
b151ef55
 	dazukoReturnAccess(&acc); /* is it needed ? */
     }
 
e6b842b3
     if(dazukoUnregister())
 	logg("!Can't unregister with Dazuko\n");
 
5aad47ca
     logg("Clamuko stopped.\n");
b5ad6489
 
     pthread_exit(NULL);
b151ef55
 }
 
 void *clamukoth(void *arg)
 {
 	struct thrarg *tharg = (struct thrarg *) arg;
 	sigset_t sigset;
5aad82e2
 	const char *virname;
b151ef55
         struct sigaction act;
 	unsigned long mask = 0;
 	const struct cfgstruct *pt;
 	short int scan;
599f27c8
 	int sizelimit = 0;
b151ef55
 	struct stat sb;
 
 
5aad47ca
     clamuko_scanning = 0;
b151ef55
 
     /* ignore all signals except SIGUSR1 */
     sigfillset(&sigset);
     sigdelset(&sigset, SIGUSR1);
530534f5
     sigdelset(&sigset, SIGSEGV);
b151ef55
     pthread_sigmask(SIG_SETMASK, &sigset, NULL);
     act.sa_handler = clamuko_exit;
     sigfillset(&(act.sa_mask));
     sigaction(SIGUSR1, &act, NULL);
530534f5
     sigaction(SIGSEGV, &act, NULL);
b151ef55
 
     /* register */
e6b842b3
     if(dazukoRegister("ClamAV", "r+")) {
b151ef55
 	logg("!Clamuko: Can't register with Dazuko\n");
 	return NULL;
     } else
 	logg("Clamuko: Correctly registered with Dazuko.\n");
 
     /* access mask */
     if(cfgopt(tharg->copt, "ClamukoScanOnOpen")) {
 	logg("Clamuko: Scan-on-open mode activated.\n");
e6b842b3
 	mask |= DAZUKO_ON_OPEN;
b151ef55
     }
     if(cfgopt(tharg->copt, "ClamukoScanOnClose")) {
 	logg("Clamuko: Scan-on-close mode activated.\n");
e6b842b3
 	mask |= DAZUKO_ON_CLOSE;
b151ef55
     }
     if(cfgopt(tharg->copt, "ClamukoScanOnExec")) {
 	logg("Clamuko: Scan-on-exec mode activated.\n");
e6b842b3
 	mask |= DAZUKO_ON_EXEC;
b151ef55
     }
 
     if(!mask) {
 	logg("!Access mask is not configured properly.\n");
89d7acc6
 	dazukoUnregister();
b151ef55
 	return NULL;
     }
 
     if(dazukoSetAccessMask(mask)) {
 	logg("!Clamuko: Can't set access mask in Dazuko.\n");
89d7acc6
 	dazukoUnregister();
b151ef55
 	return NULL;
     }
 
     if((pt = cfgopt(tharg->copt, "ClamukoIncludePath"))) {
 	while(pt) {
 	    if((dazukoAddIncludePath(pt->strarg))) {
 		logg("!Clamuko: Dazuko -> Can't include path %s\n", pt->strarg);
89d7acc6
 		dazukoUnregister();
b151ef55
 		return NULL;
 	    } else
 		logg("Clamuko: Included path %s\n", pt->strarg);
 
 	    pt = (struct cfgstruct *) pt->nextarg;
 	}
     } else {
 	logg("!Clamuko: please include at least one path.\n");
89d7acc6
 	dazukoUnregister();
b151ef55
 	return NULL;
     }
 
     if((pt = cfgopt(tharg->copt, "ClamukoExcludePath"))) {
 	while(pt) {
 	    if((dazukoAddExcludePath(pt->strarg))) {
 		logg("!Clamuko: Dazuko -> Can't exclude path %s\n", pt->strarg);
89d7acc6
 		dazukoUnregister();
b151ef55
 		return NULL;
 	    } else
 		logg("Clamuko: Excluded path %s\n", pt->strarg);
 
 	    pt = (struct cfgstruct *) pt->nextarg;
 	}
     }
 
     if((pt = cfgopt(tharg->copt, "ClamukoMaxFileSize"))) {
 	sizelimit = pt->numarg;
     } else
 	sizelimit = CL_DEFAULT_CLAMUKOMAXFILESIZE;
 
     if(sizelimit)
 	logg("Clamuko: Max file size limited to %d bytes.\n", sizelimit);
     else
 	logg("Clamuko: File size limit disabled.\n");
 
     while(1) {
 
 	if(dazukoGetAccess(&acc) == 0) {
 	    clamuko_scanning = 1;
 	    scan = 1;
 
 	    if(sizelimit) {
e6b842b3
 		stat(acc->filename, &sb);
b151ef55
 		if(sb.st_size > sizelimit) {
 		    scan = 0;
e6b842b3
 		    logg("*Clamuko: %s skipped (too big)\n", acc->filename);
b151ef55
 		}
 	    }
 
599f27c8
 	    if(scan && cl_scanfile(acc->filename, &virname, NULL, tharg->root, tharg->limits, tharg->options) == CL_VIRUS) {
e6b842b3
 		logg("Clamuko: %s: %s FOUND\n", acc->filename, virname);
a500bc14
 		virusaction(acc->filename, virname, tharg->copt);
e6b842b3
 		acc->deny = 1;
b151ef55
 	    } else
e6b842b3
 		acc->deny = 0;
b151ef55
 
 	    if(dazukoReturnAccess(&acc)) {
 		logg("!Can't return access to Dazuko.\n");
 		logg("Clamuko stopped.\n");
 		dazukoUnregister();
 		clamuko_scanning = 0;
 		return NULL;
 	    }
 
 	    clamuko_scanning = 0;
 	}
     }
 
     /* can't be ;) */
     return NULL;
 }
 
 #endif