summaryrefslogtreecommitdiffstats
path: root/src/klammail/client.c
blob: 638278560c7e3edfdbc6c87eade9cdee292a1860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 *  Copyright (C) 2002 Tomasz Kojm <zolw@konarski.edu.pl>
 *
 *  Modified slightly by Robert Hogan <robert@roberthogan.net> as part of
 *  clamdmail package. 2003.
 *
 *  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.
 */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>

//#include "others.h"
#include "cfgparser.h"
#include "memory.h"
#include "options.h"
#include "defaults.h"
//#include "shared.h"
#include <clamav.h>

int client(const char *dirname, struct optstruct *opt, char **virname)
{
	char buff[4096], cwd[200], *file, *scancmd;
	struct sockaddr_un server;
        struct sockaddr_in server2;
	struct cfgstruct *copt, *cpt;
	int sockd, bread;
	/*const char *clamav_conf = getargl(opt, "config-file");*/
	const char *clamav_conf;
	FILE *fd;
	DIR *dd;
	struct dirent *dent;
	struct stat statbuf;
	char *fname;
	int scanret = 0;

/*
    if(!clamav_conf)
	clamav_conf = DEFAULT_CFG;

    if((copt = parsecfg(clamav_conf)) == NULL) {
	mprintf("@Can't parse configuration file.\n");
	return 2;
    }

    if(cfgopt(copt, "TCPSocket") && cfgopt(copt, "LocalSocket")) {
	mprintf("@Clamd is not configured properly.\n");
	return 2;
    } else if((cpt = cfgopt(copt, "LocalSocket"))) {
*/
	server.sun_family = AF_UNIX;
	strncpy(server.sun_path, "/tmp/KlamMailSock", sizeof(server.sun_path));

	if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
	    //perror("socket()");
	    //mprintf("@Can't create the socket.\n");
	    return 2;
	}

	if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
	    close(sockd);
	    //perror("connect()");
	    //mprintf("@Can't connect to clamd.\n");
	    return 2;
	}
/*
    } else if((cpt = cfgopt(copt, "TCPSocket"))) {

#ifdef PF_INET
	if((sockd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
#else
	if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif
	    perror("socket()");
	    mprintf("@Can't create the socket.\n");
	    return 0;
	}

	server2.sin_family = AF_INET;
	server2.sin_addr.s_addr = inet_addr("127.0.0.1");
	server2.sin_port = htons(cpt->numarg);

	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
	    close(sockd);
	    perror("connect()");
	    mprintf("@Can't connect to clamd.\n");
	    return 0;
	}

    } else {
	mprintf("@Clamd is not configured properly.\n");
	return 2;
    }
*/


	scanret = passfile(dirname, sockd, virname);


    return scanret;
}

int passfile(const char *file, int sockd, char **virname)
{

	char buff[4096];
	char *fullpath, cwd[200];
	char *scancmd;
	FILE *fd;
	
// 	fullpath = mcalloc(200 + strlen(file) + 10, sizeof(char));
// 
// 	if(!getcwd(cwd, 200)) {
// 		mprintf("@Can't get absolute pathname of current working directory.\n");
// 		return 0;
// 	}
// 	sprintf(fullpath, "%s/%s", cwd, file);

	scancmd = (char *) mcalloc(strlen(file) + 20, sizeof(char));
	sprintf(scancmd, "CONTSCAN %s", file);
	
	if(write(sockd, scancmd, strlen(scancmd)) < 0) {
	mprintf("@Can't write to the socket.\n");
	close(sockd);
	return 2;
	}
	
	if((fd = fdopen(sockd, "r")) == NULL) {
	mprintf("@Can't open descriptor %d to read.\n", sockd);
	return 2;
	}
	
	while(fgets(buff, sizeof(buff), fd)) {
	if(strstr(buff, "FOUND\n")) {
		strtok(buff, " ");
		*virname = strtok(NULL, " ");
		return 1;
	}
	}
	
	fclose(fd);
        return 0;
}