| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |  *  Copyright (C) 2007 - 2009 Sourcefire, Inc. | 
					
						
							| 
									
										
										
										
											2008-03-18 15:40:41 +00:00
										 |  |  |  *  Author: Tomasz Kojm <tkojm@clamav.net> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |  *  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 | 
					
						
							| 
									
										
										
										
											2006-04-09 19:59:28 +00:00
										 |  |  |  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | 
					
						
							|  |  |  |  *  MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2003-11-26 13:33:51 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <clamav.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Exit codes: | 
					
						
							|  |  |  |  *  0: clean | 
					
						
							|  |  |  |  *  1: infected | 
					
						
							|  |  |  |  *  2: error | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | int main(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  | 	int fd, ret; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | 	unsigned long int size = 0; | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  | 	unsigned int sigs = 0; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | 	long double mb; | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  | 	const char *virname; | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	struct cl_engine *engine; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if(argc != 2) { | 
					
						
							|  |  |  | 	printf("Usage: %s file\n", argv[0]); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	return 2; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  |     if((fd = open(argv[1], O_RDONLY)) == -1) { | 
					
						
							|  |  |  | 	printf("Can't open file %s\n", argv[1]); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	return 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-23 12:20:58 +00:00
										 |  |  |     if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) { | 
					
						
							|  |  |  | 	printf("Can't initialize libclamav: %s\n", cl_strerror(ret)); | 
					
						
							|  |  |  | 	return 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |     if(!(engine = cl_engine_new())) { | 
					
						
							| 
									
										
										
										
											2009-02-23 12:20:58 +00:00
										 |  |  | 	printf("Can't create new engine\n"); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	return 2; | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* load all available databases from default directory */ | 
					
						
							| 
									
										
										
										
											2009-02-23 12:20:58 +00:00
										 |  |  |     if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2006-08-09 11:48:12 +00:00
										 |  |  | 	printf("cl_load: %s\n", cl_strerror(ret)); | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  | 	close(fd); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |         cl_engine_free(engine); | 
					
						
							|  |  |  | 	return 2; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |     printf("Loaded %u signatures.\n", sigs); | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-19 17:54:40 +00:00
										 |  |  |     /* build engine */ | 
					
						
							| 
									
										
										
										
											2009-02-23 12:20:58 +00:00
										 |  |  |     if((ret = cl_engine_compile(engine)) != CL_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2003-12-02 22:48:56 +00:00
										 |  |  | 	printf("Database initialization error: %s\n", cl_strerror(ret));; | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |         cl_engine_free(engine); | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  | 	close(fd); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	return 2; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  |     /* scan file descriptor */ | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |     if((ret = cl_scandesc(fd, &virname, &size, engine, CL_SCAN_STDOPT)) == CL_VIRUS) { | 
					
						
							| 
									
										
										
										
											2004-03-29 00:00:58 +00:00
										 |  |  | 	printf("Virus detected: %s\n", virname); | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  | 	if(ret == CL_CLEAN) { | 
					
						
							|  |  |  | 	    printf("No virus detected.\n"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 	    printf("Error: %s\n", cl_strerror(ret)); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	    cl_engine_free(engine); | 
					
						
							| 
									
										
										
										
											2006-08-09 11:48:12 +00:00
										 |  |  | 	    close(fd); | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  | 	    return 2; | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-09-04 21:30:11 +00:00
										 |  |  |     close(fd); | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |     /* free memory */ | 
					
						
							|  |  |  |     cl_engine_free(engine); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  |     /* calculate size of scanned data */ | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  |     mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0; | 
					
						
							| 
									
										
										
										
											2006-08-03 21:33:37 +00:00
										 |  |  |     printf("Data scanned: %2.2Lf MB\n", mb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 14:36:53 +00:00
										 |  |  |     return ret == CL_VIRUS ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2003-07-29 15:48:06 +00:00
										 |  |  | } |