Browse code

adding libfreshclam

Signed-off-by: Steven Morgan <smorgan@sourcefire.com>

andrey mirtchovski authored on 2016/03/12 05:32:31
Showing 5 changed files
... ...
@@ -27,6 +27,10 @@ if ENABLE_CLAMSUBMIT
27 27
 SUBDIRS += clamsubmit
28 28
 endif
29 29
 
30
+if ENABLE_LIBFRESHCLAM
31
+SUBDIRS += libfreshclam
32
+endif
33
+
30 34
 pkgconfigdir = $(libdir)/pkgconfig
31 35
 pkgconfig_DATA = libclamav.pc
32 36
 
... ...
@@ -135,6 +135,16 @@ m4_include([m4/reorganization/substitutions.m4])
135 135
 
136 136
 AM_CONDITIONAL([ENABLE_CLAMSUBMIT], [test "$have_curl" = "yes"])
137 137
 
138
+AC_ARG_ENABLE([libfreshclam],
139
+			   [AS_HELP_STRING([--enable-libfreshclam], [enable building of libfreshclam])],
140
+			   enable_libfreshclam=$enableval, enable_libfreshclam="no")
141
+
142
+if test "$enable_libfreshclam" = "yes"; then
143
+  AC_CONFIG_FILES([libfreshclam/Makefile])
144
+  AC_DEFINE([ENABLE_LIBFRESHCLAM],1,[enable libfreshclam])
145
+fi
146
+
147
+
138 148
 AC_CONFIG_FILES([
139 149
 clamscan/Makefile
140 150
 database/Makefile
141 151
new file mode 100644
... ...
@@ -0,0 +1,56 @@
0
+#
1
+#  Copyright (C) 2015 Cisco Systems
2
+#
3
+#  This program is free software; you can redistribute it and/or modify
4
+#  it under the terms of the GNU General Public License as published by
5
+#  the Free Software Foundation; either version 2 of the License, or
6
+#  (at your option) any later version.
7
+#
8
+#  This program is distributed in the hope that it will be useful,
9
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#  GNU General Public License for more details.
12
+#
13
+#  You should have received a copy of the GNU General Public License
14
+#  along with this program; if not, write to the Free Software
15
+#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16
+#  MA 02110-1301, USA.
17
+
18
+libfreshclam_la_SOURCES = \
19
+    $(top_srcdir)/shared/output.c \
20
+    $(top_srcdir)/shared/output.h \
21
+    $(top_srcdir)/shared/optparser.c \
22
+    $(top_srcdir)/shared/optparser.h \
23
+    $(top_srcdir)/shared/getopt.c \
24
+    $(top_srcdir)/shared/getopt.h \
25
+    $(top_srcdir)/shared/misc.c \
26
+    $(top_srcdir)/shared/misc.h \
27
+    $(top_srcdir)/shared/cdiff.c \
28
+    $(top_srcdir)/shared/cdiff.h \
29
+    $(top_srcdir)/shared/tar.c \
30
+    $(top_srcdir)/shared/tar.h \
31
+    $(top_srcdir)/shared/clamdcom.c \
32
+    $(top_srcdir)/shared/clamdcom.h \
33
+	$(top_srcdir)/freshclam/freshclamcodes.h \
34
+	$(top_srcdir)/freshclam/manager.c \
35
+	$(top_srcdir)/freshclam/manager.h \
36
+	$(top_srcdir)/freshclam/notify.c \
37
+	$(top_srcdir)/freshclam/notify.h \
38
+	$(top_srcdir)/freshclam/dns.c \
39
+	$(top_srcdir)/freshclam/dns.h \
40
+	$(top_srcdir)/freshclam/execute.c \
41
+	$(top_srcdir)/freshclam/execute.h \
42
+	$(top_srcdir)/freshclam/nonblock.c \
43
+	$(top_srcdir)/freshclam/nonblock.h \
44
+	$(top_srcdir)/freshclam/mirman.c \
45
+	$(top_srcdir)/freshclam/mirman.h \
46
+	libfreshclam.c \
47
+	libfreshclam.h
48
+
49
+lib_LTLIBRARIES = libfreshclam.la
50
+
51
+
52
+AM_CFLAGS=@WERR_CFLAGS@
53
+DEFS = @DEFS@ -DCL_NOTHREADS
54
+AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/shared -I$(top_srcdir)/libclamav @SSL_CPPFLAGS@ @FRESHCLAM_CPPFLAGS@  @JSON_CPPFLAGS@
55
+
0 56
new file mode 100644
... ...
@@ -0,0 +1,344 @@
0
+/*
1
+ *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
2
+ *
3
+ *  This program is free software; you can redistribute it and/or modify
4
+ *  it under the terms of the GNU General Public License version 2 as
5
+ *  published by the Free Software Foundation.
6
+ *
7
+ *  This program is distributed in the hope that it will be useful,
8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
+ *  GNU General Public License for more details.
11
+ *
12
+ *  You should have received a copy of the GNU General Public License
13
+ *  along with this program; if not, write to the Free Software
14
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15
+ *  MA 02110-1301, USA.
16
+ */
17
+
18
+#if HAVE_CONFIG_H
19
+#include "clamav-config.h"
20
+#endif
21
+
22
+#include <stdint.h>
23
+#include <stdio.h>
24
+#include <stdlib.h>
25
+#ifdef	HAVE_UNISTD_H
26
+#include <unistd.h>
27
+#endif
28
+#include <string.h>
29
+#include <errno.h>
30
+#include <signal.h>
31
+#include <time.h>
32
+#include <sys/types.h>
33
+#ifndef	_WIN32
34
+#include <sys/wait.h>
35
+#endif
36
+#include <sys/stat.h>
37
+#include <fcntl.h>
38
+#ifdef	HAVE_PWD_H
39
+#include <pwd.h>
40
+#endif
41
+#ifdef HAVE_GRP_H
42
+#include <grp.h>
43
+#endif
44
+
45
+#if defined(USE_SYSLOG) && !defined(C_AIX)
46
+#include <syslog.h>
47
+#endif
48
+
49
+#include "target.h"
50
+#include "clamav.h"
51
+#include "freshclam/freshclamcodes.h"
52
+
53
+#include "libclamav/others.h"
54
+#include "libclamav/str.h"
55
+
56
+#include "shared/optparser.h"
57
+#include "shared/output.h"
58
+#include "shared/misc.h"
59
+
60
+#include "freshclam/execute.h"
61
+#include "freshclam/manager.h"
62
+#include "freshclam/mirman.h"
63
+#include "libfreshclam.h"
64
+int sigchld_wait = 1;
65
+char updtmpdir[512], dbdir[512];
66
+
67
+
68
+static int
69
+download (const struct optstruct *opts, const char *cfgfile)
70
+{
71
+    int ret = 0, try = 1, maxattempts = 0;
72
+    const struct optstruct *opt;
73
+    
74
+    
75
+    maxattempts = (int)optget (opts, "MaxAttempts")->numarg;
76
+    logg ("*Max retries == %d\n", maxattempts);
77
+    
78
+    if (!(opt = optget (opts, "DatabaseMirror"))->enabled)
79
+    {
80
+        logg ("^You must specify at least one database mirror in %s\n",
81
+              cfgfile);
82
+        return FCE_CONFIG;
83
+    }
84
+    else
85
+    {
86
+        while (opt)
87
+        {
88
+            ret = downloadmanager (opts, opt->strarg, try);
89
+#ifndef _WIN32
90
+            alarm (0);
91
+#endif
92
+            if (ret == FCE_CONNECTION || ret == FCE_BADCVD
93
+                || ret == FCE_FAILEDGET || ret == FCE_MIRRORNOTSYNC)
94
+            {
95
+                if (try < maxattempts)
96
+                {
97
+                    logg ("Trying again in 5 secs...\n");
98
+                    try++;
99
+                    sleep (5);
100
+                    continue;
101
+                }
102
+                else
103
+                {
104
+                    logg ("Giving up on %s...\n", opt->strarg);
105
+                    opt = (struct optstruct *) opt->nextarg;
106
+                    if (!opt)
107
+                    {
108
+                        logg ("Update failed. Your network may be down or none of the mirrors listed in %s is working. Check http://www.clamav.net/support/mirror-problem for possible reasons.\n", cfgfile);
109
+                    }
110
+                }
111
+                
112
+            }
113
+            else
114
+            {
115
+                return ret;
116
+            }
117
+        }
118
+    }
119
+    
120
+    return ret;
121
+}
122
+
123
+
124
+
125
+int download_with_opts(struct optstruct *opts, const char* db_path, const char* db_owner) {
126
+    const struct optstruct *opt;
127
+#ifdef HAVE_PWD_H
128
+    const char *dbowner;
129
+    struct passwd *user;
130
+#endif
131
+    struct mirdat mdat;
132
+    int ret;
133
+    
134
+    
135
+#ifdef HAVE_PWD_H
136
+    if (db_owner) {
137
+        dbowner = db_owner;
138
+    }
139
+    else
140
+    {
141
+        /* freshclam shouldn't work with root privileges */
142
+        dbowner = optget (opts, "DatabaseOwner")->strarg;
143
+    }
144
+    
145
+    if (!geteuid ())
146
+    {
147
+        if ((user = getpwnam (dbowner)) == NULL)
148
+        {
149
+            logg ("^Can't get information about user %s.\n", dbowner);
150
+            optfree (opts);
151
+            return FCE_USERINFO;
152
+        }
153
+        
154
+        if (optget (opts, "AllowSupplementaryGroups")->enabled)
155
+        {
156
+#ifdef HAVE_INITGROUPS
157
+            if (initgroups (dbowner, user->pw_gid))
158
+            {
159
+                logg ("^initgroups() failed.\n");
160
+                optfree (opts);
161
+                return FCE_USERORGROUP;
162
+            }
163
+#endif
164
+        }
165
+        else
166
+        {
167
+        }
168
+    }
169
+#endif /* HAVE_PWD_H */
170
+    
171
+    /* initialize some important variables */
172
+    
173
+    if (optget (opts, "Debug")->enabled || optget (opts, "debug")->enabled)
174
+        cl_debug ();
175
+    
176
+    if (optget (opts, "verbose")->enabled)
177
+        mprintf_verbose = 1;
178
+    
179
+    if (optget (opts, "quiet")->enabled)
180
+        mprintf_quiet = 1;
181
+    
182
+    if (optget (opts, "no-warnings")->enabled)
183
+    {
184
+        mprintf_nowarn = 1;
185
+        logg_nowarn = 1;
186
+    }
187
+    
188
+    if (optget (opts, "stdout")->enabled)
189
+        mprintf_stdout = 1;
190
+    
191
+    /* initialize logger */
192
+    logg_verbose = mprintf_verbose ? 1 : optget (opts, "LogVerbose")->enabled;
193
+    logg_time = optget (opts, "LogTime")->enabled;
194
+    logg_size = optget (opts, "LogFileMaxSize")->numarg;
195
+    if (logg_size)
196
+        logg_rotate = optget(opts, "LogRotate")->enabled;
197
+    
198
+    if ((opt = optget (opts, "UpdateLogFile"))->enabled)
199
+    {
200
+        logg_file = opt->strarg;
201
+        if (logg ("#--------------------------------------\n"))
202
+        {
203
+            mprintf ("!Problem with internal logger (UpdateLogFile = %s).\n",
204
+                     logg_file);
205
+            optfree (opts);
206
+            return FCE_LOGGING;
207
+        }
208
+    }
209
+    else
210
+        logg_file = NULL;
211
+    
212
+#if defined(USE_SYSLOG) && !defined(C_AIX)
213
+    if (optget (opts, "LogSyslog")->enabled)
214
+    {
215
+        int fac = LOG_LOCAL6;
216
+        
217
+        if ((opt = optget (opts, "LogFacility"))->enabled)
218
+        {
219
+            if ((fac = logg_facility (opt->strarg)) == -1)
220
+            {
221
+                mprintf ("!LogFacility: %s: No such facility.\n",
222
+                         opt->strarg);
223
+                optfree (opts);
224
+                return FCE_LOGGING;
225
+            }
226
+        }
227
+        
228
+        openlog ("freshclam", LOG_PID, fac);
229
+        logg_syslog = 1;
230
+    }
231
+#endif
232
+    
233
+    /* change the current working directory */
234
+    if (chdir (optget (opts, "DatabaseDirectory")->strarg))
235
+    {
236
+        logg ("!Can't change dir to %s\n",
237
+              optget (opts, "DatabaseDirectory")->strarg);
238
+        optfree (opts);
239
+        return FCE_DIRECTORY;
240
+    }
241
+    else
242
+    {
243
+        if (db_path)
244
+        {
245
+            if (chdir (db_path))
246
+            {
247
+                logg ("!Can't change dir to %s\n", db_path);
248
+                optfree (opts);
249
+                return FCE_DIRECTORY;
250
+            }
251
+        }
252
+        
253
+        if (!getcwd (dbdir, sizeof (dbdir)))
254
+        {
255
+            logg ("!getcwd() failed\n");
256
+            optfree (opts);
257
+            return FCE_DIRECTORY;
258
+        }
259
+        logg ("*Current working dir is %s\n", dbdir);
260
+    }
261
+    
262
+    
263
+    if (optget (opts, "list-mirrors")->enabled)
264
+    {
265
+        if (mirman_read ("mirrors.dat", &mdat, 1) == -1)
266
+        {
267
+            printf ("Can't read mirrors.dat\n");
268
+            optfree (opts);
269
+            return FCE_FILE;
270
+        }
271
+        mirman_list (&mdat);
272
+        mirman_free (&mdat);
273
+        optfree (opts);
274
+        return 0;
275
+    }
276
+    
277
+    if ((opt = optget (opts, "PrivateMirror"))->enabled)
278
+    {
279
+        struct optstruct *dbm, *opth;
280
+        
281
+        dbm = (struct optstruct *) optget (opts, "DatabaseMirror");
282
+        dbm->active = dbm->enabled = 1;
283
+        do
284
+        {
285
+            if (cli_strbcasestr (opt->strarg, ".clamav.net"))
286
+            {
287
+                logg ("!PrivateMirror: *.clamav.net is not allowed in this mode\n");
288
+                optfree (opts);
289
+                return FCE_PRIVATEMIRROR;
290
+            }
291
+            
292
+            if (dbm->strarg)
293
+                free (dbm->strarg);
294
+            dbm->strarg = strdup (opt->strarg);
295
+            if (!dbm->strarg)
296
+            {
297
+                logg ("!strdup() failed\n");
298
+                optfree (opts);
299
+                return FCE_MEM;
300
+            }
301
+            if (!dbm->nextarg)
302
+            {
303
+                dbm->nextarg =
304
+                (struct optstruct *) calloc (1,
305
+                                             sizeof (struct optstruct));
306
+                if (!dbm->nextarg)
307
+                {
308
+                    logg ("!calloc() failed\n");
309
+                    optfree (opts);
310
+                    return FCE_MEM;
311
+                }
312
+            }
313
+            opth = dbm;
314
+            dbm = dbm->nextarg;
315
+        }
316
+        while ((opt = opt->nextarg));
317
+        
318
+        opth->nextarg = NULL;
319
+        while (dbm)
320
+        {
321
+            free (dbm->name);
322
+            free (dbm->cmd);
323
+            free (dbm->strarg);
324
+            opth = dbm;
325
+            dbm = dbm->nextarg;
326
+            free (opth);
327
+        }
328
+        
329
+        /* disable DNS db checks */
330
+        opth = (struct optstruct *) optget (opts, "no-dns");
331
+        opth->active = opth->enabled = 1;
332
+        
333
+        /* disable scripted updates */
334
+        opth = (struct optstruct *) optget (opts, "ScriptedUpdates");
335
+        opth->active = opth->enabled = 0;
336
+    }
337
+    
338
+    *updtmpdir = 0;
339
+    
340
+    ret = download (opts, NULL);
341
+    optfree (opts);
342
+    return ret;
343
+}
0 344
new file mode 100644
... ...
@@ -0,0 +1,15 @@
0
+//
1
+//  libfreshclam.h
2
+//  freshclam
3
+//
4
+//  Created by msachedi on 2/3/14.
5
+//  Copyright (c) 2014 Sourcefire, Inc. All rights reserved.
6
+//
7
+
8
+#ifndef freshclam_libfreshclam_h
9
+#define freshclam_libfreshclam_h
10
+
11
+int download_with_opts(struct optstruct *opts, const char* db_path, const char* db_owner);
12
+struct optstruct *optadditem(const char *name, const char *arg, int verbose, int toolmask, int ignore,
13
+                          struct optstruct *oldopts);
14
+#endif