On Wed, 9 Mar 2005, Andy Fiddaman wrote:
This is looking good, just one problem and one comment.
mcalloc() is unfortunately not portable, it's probably best that you use the ClamAV library function cli_malloc() for consistency.
Hmm. Well, why do other parts of the clamd program then call mcalloc() (which is just a wrapper to malloc)? I would use cli_malloc but I am not even sure that header is visible to clamd (it's libclamav/others.h).
Also, if I fix my code, the problem still remains that other parts of clamd use mcalloc(). :(
Your 'desc->buflen = len;' would be better placed inside the previous block, otherwise if you get a long variable then a short one then one in between, an unecessary memory allocation will be performed.
Yes, you are right. Duh, I feel dumb. Wow you *are* a keen reader of other people's code! :)
Attached is my updated patch.. btw Andy, do you have CVS access to apply it or do I need to re-email Tomasz explicitly?
-Calin
diff -ur clamav-devel/clamd/clamuko.c clamav-devel-modified/clamd/clamuko.c
--- clamav-devel/clamd/clamuko.c 2005-03-09 12:32:41.000000000 -0500
+++ clamav-devel-modified/clamd/clamuko.c 2005-03-09 12:17:53.000000000
-0500
@@ -173,7 +173,7 @@
if(scan && cl_scanfile(acc->filename, &virname, NULL, tharg->root,
tharg->limits, tharg->options) == CL_VIRUS) {
logg("Clamuko: %s: %s FOUND\n", acc->filename, virname);
- virusaction(virname, tharg->copt);
+ virusaction(acc->filename, virname, tharg->copt);
acc->deny = 1;
} else
acc->deny = 0;
diff -ur clamav-devel/clamd/others.c clamav-devel-modified/clamd/others.c
--- clamav-devel/clamd/others.c 2005-03-09 12:32:41.000000000 -0500
+++ clamav-devel-modified/clamd/others.c 2005-03-09 17:33:47.601379304
-0500
@@ -69,7 +69,45 @@
#include "cfgparser.h"
#include "session.h"
-void virusaction(const char *virname, const struct cfgstruct *copt)
+#define ENV_PREFIX "CLAM_VIRUSEVENT_"
+#define ENV_FILENAME (ENV_PREFIX "FILENAME")
+#define ENV_VIRUSNAME (ENV_PREFIX "VIRUSNAME")
+enum env_var { FILENAME = 0, VIRUSNAME, N_ENV_VARS };
+struct env_var_desc {
+ const char * const name;
+ const int namelen;
+ char *buf;
+ int buflen;
+};
+
+static struct env_var_desc env_vars[] = {
+ /* Array is indexed using enum env_var above.. */
+ { ENV_FILENAME, strlen(ENV_FILENAME), 0, -1 },
+ { ENV_VIRUSNAME, strlen(ENV_VIRUSNAME), 0, -1 }
+};
+
+static void put_env_var(int var, const char *value)
+{
+ struct env_var_desc *desc;
+ int len;
+
+ if (var < 0 || var >= N_ENV_VARS) return;
+ if (!value) value = "";
+ desc = &env_vars[var];
+ len = desc->namelen + strlen(value) + sizeof(char)*2;
+ if (len > desc->buflen) {
+ char *newbuf = (char *) mcalloc(len, sizeof(char));
+ strcpy(newbuf, desc->name);
+ putenv(newbuf); /* clear old var... */
+ free(desc->buf); /* safely free old var... */
+ desc->buf = newbuf;
+ desc->buflen = len;
+ }
+ snprintf(desc->buf, desc->buflen, "%s=%s", desc->name, value);
+ putenv(desc->buf);
+}
+
+void virusaction(const char *filename, const char *virname, const struct
cfgstruct *copt)
{
char *buffer, *pt, *cmd;
struct cfgstruct *cpt;
@@ -90,7 +128,12 @@
cmd = strdup(buffer);
free(buffer);
}
-
+ /* Setup environment, note that because of SUSv2 specs the strings
+ needs to be non-const and the actual buffers themselves become part of
+ the environemnt. */
+ put_env_var(FILENAME, filename);
+ put_env_var(VIRUSNAME, virname);
+
/* WARNING: this is uninterruptable ! */
system(cmd);
diff -ur clamav-devel/clamd/others.h clamav-devel-modified/clamd/others.h
--- clamav-devel/clamd/others.h 2005-03-09 12:32:41.000000000 -0500
+++ clamav-devel-modified/clamd/others.h 2005-03-09 12:16:53.000000000
-0500
@@ -28,7 +28,7 @@
int poll_fd(int fd, int timeout_sec);
int is_fd_connected(int fd);
-void virusaction(const char *virname, const struct cfgstruct *copt);
+void virusaction(const char *filename, const char *virname, const struct
cfgstruct *copt);
int writen(int fd, void *buff, unsigned int count);
#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) ||
defined(HAVE_CONTROL_IN_MSGHDR)) && !defined(C_CYGWIN) && !defined(C_OS2)
diff -ur clamav-devel/clamd/scanner.c clamav-devel-modified/clamd/scanner.c
--- clamav-devel/clamd/scanner.c 2005-03-09 12:32:41.000000000 -0500
+++ clamav-devel-modified/clamd/scanner.c 2005-03-09 12:27:45.000000000
-0500
@@ -159,7 +159,7 @@
mdprintf(odesc, "%s: %s FOUND\n", fname,
*virname);
logg("%s: %s FOUND\n", fname, *virname);
- virusaction(*virname, copt);
+ virusaction(fname, *virname, copt);
if(!contscan) {
closedir(dd);
free(fname);
@@ -237,7 +237,7 @@
if(ret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", filename, virname);
logg("%s: %s FOUND\n", filename, virname);
- virusaction(virname, copt);
+ virusaction(filename, virname, copt);
} else if(ret != CL_CLEAN) {
mdprintf(odesc, "%s: %s ERROR\n", filename, cl_strerror(ret));
logg("%s: %s ERROR\n", filename, cl_strerror(ret));
@@ -266,6 +266,7 @@
int ret;
const char *virname;
struct stat statbuf;
+ char fdstr[32];
if(fstat(fd, &statbuf) == -1)
@@ -274,19 +275,21 @@
if(!S_ISREG(statbuf.st_mode))
return -1;
+ snprintf(fdstr, sizeof(fdstr), "fd[%d]", fd);
+
ret = cl_scandesc(fd, &virname, scanned, root, limits, options);
if(ret == CL_VIRUS) {
- mdprintf(odesc, "fd[%d]: %s FOUND\n", fd, virname);
- logg("fd[%d]: %s FOUND\n", fd, virname);
- virusaction(virname, copt);
+ mdprintf(odesc, "%s: %s FOUND\n", fdstr, virname);
+ logg("%s: %s FOUND\n", fdstr, virname);
+ virusaction(fdstr, virname, copt);
} else if(ret != CL_CLEAN) {
- mdprintf(odesc, "fd[%d]: %s ERROR\n", fd, cl_strerror(ret));
- logg("fd[%d]: %s ERROR\n", fd, cl_strerror(ret));
+ mdprintf(odesc, "%s: %s ERROR\n", fdstr, cl_strerror(ret));
+ logg("%s: %s ERROR\n", fdstr, cl_strerror(ret));
} else {
- mdprintf(odesc, "fd[%d]: OK\n", fd);
+ mdprintf(odesc, "%s: OK\n", fdstr);
if(logok)
- logg("fd[%d]: OK\n", fd);
+ logg("%s: OK\n", fdstr);
}
return ret;
@@ -467,7 +470,7 @@
if(ret == CL_VIRUS) {
mdprintf(odesc, "stream: %s FOUND\n", virname);
logg("stream: %s FOUND\n", virname);
- virusaction(virname, copt);
+ virusaction("stream", virname, copt);
} else if(ret != CL_CLEAN) {
mdprintf(odesc, "stream: %s ERROR\n", cl_strerror(ret));
logg("stream: %s ERROR\n", cl_strerror(ret));
_______________________________________________ http://lurker.clamav.net/list/clamav-devel.html
