The below came in through the Debian BTS.  The erproter would like a way
to have the On*Execute scripts notice the return codes of the failing
event.  The following patch enables it, as well as fixing a missing
Foreground option for freshclam.

The whole bug log can be viewed at
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=295547

Debian specific parts of his patch have been removed here.

Thanks,

----- Forwarded message from Paolo <[EMAIL PROTECTED]> -----

  * can embed %d in On*Execute commands, which gets subs by return code of
    updatedb action.
  * factored out system() calls to such commands in on_x_execute() in
    freshclam.c
  * OnErrorExecute gets called also when we want NotifyClamd but the 
    notification fails.
  * notify() clamd returns different rc (>=90) for each case.
  * fixed missing -f / --foreground option, which gets listed by -h / in man
  * FIXME: that's in the code and in the sample freshclam.conf, none in
    the docs (perhaps better let upstream look into the stuff first...)


diff -urN clamav-0.83/etc/freshclam.conf clamav-0.83.1/etc/freshclam.conf
--- clamav-0.83/etc/freshclam.conf      Sat Feb 12 01:15:13 2005
+++ clamav-0.83.1/etc/freshclam.conf    Fri Feb 18 20:44:07 2005
@@ -83,12 +83,19 @@
 #NotifyClamd /config/file/path
 
 # Run command after successful database update.
+# If %d is present, the command string is evaluated by sprintf() and %d
+# gets substituted by the return code of the updatedb action [always 0 
+# so far]. 
 # Default: disabled
 #OnUpdateExecute command
 
 # Run command when database update process fails.
+# If %d is present, the command string is evaluated by sprintf() and %d
+# gets substituted by the error/return code of the updatedb action. 
 # Default: disabled
 #OnErrorExecute command
+#OnErrorExecute /etc/clamav/onerrorexecute.d/err-dbvirus.sh %d
+#OnErrorExecute /etc/clamav/onerrorexecute.d/%d_err-dbvirus.sh
 
 # Don't fork into background.
 # Default: disabled
diff -urN clamav-0.83/freshclam/freshclam.c clamav-0.83.1/freshclam/freshclam.c
--- clamav-0.83/freshclam/freshclam.c   Sat Feb 12 00:34:32 2005
+++ clamav-0.83.1/freshclam/freshclam.c Fri Feb 18 20:34:25 2005
@@ -48,6 +48,57 @@
 
 static short terminate = 0;
 
+/* call system() with command from cmd string argopt or equivalent config
+   opt cfgopt. Optional %d in cmd string is subs' by return code ret. E.g:
+   argopt = on-error-execute || cfgopt = OnErrorExecute
+   in /etc/clamav/freshclam.conf
+   ...
+   OnErrorExecute echo|mail -s"CLAMAV:$HOSTNAME:freshclam: ERR(%d)" root
+   ...
+   
+   or even:
+   ...
+    OnErrorExecute /etc/clamav/onerrorexecute.d/%d_err.sh
+   ...
+   would email the (return code) in the msg subject
+   Command string must not include any of the %n sequences ;)
+ */
+void on_x_execute (const struct cfgstruct *copt, const struct optstruct *opt,
+       const char *argopt, const char *cfopt, int ret)
+{
+    char *m;
+    struct cfgstruct *cpt;
+    
+    if(argopt != NULL && optl(opt, argopt))
+       m = getargl(opt, argopt);
+    else if(cfopt != NULL && (cpt = cfgopt(copt, cfopt)))
+       m = cpt->strarg;
+    else
+       return;
+
+    if(strstr(m, "%d") != NULL &&
+       strstr(m, "%n") == NULL &&
+       strstr(m, "%ln") == NULL &&
+       strstr(m, "%lln") == NULL &&
+       strstr(m, "%hn") == NULL &&
+       strstr(m, "%hhn") == NULL &&
+       strstr(m, "%qn") == NULL) {
+       char *cmd;
+       /* 'command...%d...\0' -> 'command...-DDD...\0' at most */
+       cmd = (char *) calloc((strlen(m) + 4), sizeof(char));
+       /* sprintf() should be safe here - ret is an int but never assigned
+          a value >255 or < -255
+          system should manage on its own if we bump over max shell
+          string command length
+        */
+       sprintf(cmd,m,ret);
+       /* fprintf(stderr,"%s\n",cmd); */
+       system(cmd);
+       free(cmd);
+    } else {
+       system(m);
+    }
+}
 
 static void daemon_sighandler(int sig) {
        char *action = NULL;
@@ -305,15 +356,9 @@
        while(!terminate) {
            ret = download(copt, opt);
 
-
-           if(optl(opt, "on-error-execute")) {
-               if(ret > 1)
-                   system(getargl(opt, "on-error-execute"));
-
-           } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
-               if(ret > 1)
-                   system(cpt->strarg);
-           }
+           if(ret > 1)
+               on_x_execute(copt, opt,
+                       "on-error-execute", "OnErrorExecute", ret);
 
            logg("--------------------------------------\n");
            sigaction(SIGALRM, &sigact, &oldact);
@@ -340,14 +385,9 @@
     } else
        ret = download(copt, opt);
 
-    if(optl(opt, "on-error-execute")) {
-       if(ret > 1)
-           system(getargl(opt, "on-error-execute"));
-
-    } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
-       if(ret > 1)
-           system(cpt->strarg);
-    }
+    if(ret > 1)
+       on_x_execute(copt, opt, "on-error-execute", "OnErrorExecute", ret);
+
     if (pidfile) {
         unlink(pidfile);
     }
diff -urN clamav-0.83/freshclam/manager.c clamav-0.83.1/freshclam/manager.c
--- clamav-0.83/freshclam/manager.c     Thu Jan 27 01:32:57 2005
+++ clamav-0.83.1/freshclam/manager.c   Fri Feb 18 20:23:42 2005
@@ -49,6 +49,10 @@
 #include "../libclamav/str.h" /* cli_strtok */
 #include "dns.h"
 
+extern void on_x_execute (const struct cfgstruct *copt, 
+       const struct optstruct *opt,
+       const char *argopt, const char *cfopt, int ret);
+
 
 int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, 
const char *hostname)
 {
@@ -162,26 +166,26 @@
            logg("Database updated (%d signatures) from %s (IP: %s)\n", signo, 
hostname, ipaddr);
        }
 
+       on_x_execute(copt, opt, "on-update-execute", "OnUpdateExecute", ret);
 #ifdef BUILD_CLAMD
+       ret = 0;
        if(optl(opt, "daemon-notify")) {
                const char *clamav_conf = getargl(opt, "daemon-notify");
            if(!clamav_conf)
                clamav_conf = CONFDIR"/clamd.conf";
 
-           notify(clamav_conf);
+           ret = notify(clamav_conf);
        } else if((cpt = cfgopt(copt, "NotifyClamd"))) {
                const char *clamav_conf = cpt->strarg;
            if(!clamav_conf)
                clamav_conf = CONFDIR"/clamd.conf";
 
-           notify(clamav_conf);
+           ret = notify(clamav_conf);
        }
+       if(ret != 0)
+           on_x_execute(copt, opt, "on-error-execute", "OnErrorExecute", ret);
 #endif
 
-       if(optl(opt, "on-update-execute"))
-           system(getargl(opt, "on-update-execute"));
-       else if((cpt = cfgopt(copt, "OnUpdateExecute")))
-           system(cpt->strarg);
 
        return 0;
 
diff -urN clamav-0.83/freshclam/notify.c clamav-0.83.1/freshclam/notify.c
--- clamav-0.83/freshclam/notify.c      Mon Jul 19 19:54:40 2004
+++ clamav-0.83.1/freshclam/notify.c    Fri Feb 18 00:31:13 2005
@@ -49,12 +49,12 @@
 
     if((copt = parsecfg(cfgfile, 1)) == NULL) {
        mprintf("@Clamd was NOT notified: Can't find or parse configuration 
file %s\n", cfgfile);
-       return 1;
+       return 90;
     }
 
     if(cfgopt(copt, "TCPSocket") && cfgopt(copt, "LocalSocket")) {
        mprintf("@Clamd was NOT notified: Both socket types (TCP and local) 
declared in %s\n", cfgfile);
-       return 1;
+       return 91;
     } else if((cpt = cfgopt(copt, "LocalSocket"))) {
        socktype = "UNIX";
        server.sun_family = AF_UNIX;
@@ -63,14 +63,14 @@
        if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
            mprintf("@Clamd was NOT notified: Can't create socket endpoint for 
%s\n", cpt->strarg);
            perror("socket()");
-           return 1;
+           return 92;
        }
 
        if(connect(sockd, (struct sockaddr *) &server, sizeof(struct 
sockaddr_un)) < 0) {
            close(sockd);
            mprintf("@Clamd was NOT notified: Can't connect to clamd through 
%s\n", cpt->strarg);
            perror("connect()");
-           return 1;
+           return 93;
        }
 
     } else if((cpt = cfgopt(copt, "TCPSocket"))) {
@@ -83,7 +83,7 @@
 #endif
            mprintf("@Clamd was NOT notified: Can't create TCP socket\n");
            perror("socket()");
-           return 1;
+           return 94;
        }
 
        server2.sin_family = AF_INET;
@@ -93,7 +93,7 @@
            if ((he = gethostbyname(cpt->strarg)) == 0) {
                perror("gethostbyname()");
                mprintf("@Clamd was NOT notified: Can't resolve hostname 
'%s'\n", cpt->strarg);
-               return 1;
+               return 95;
            }
            server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
        } else
@@ -105,19 +105,19 @@
            mprintf("@Clamd was NOT notified: Can't connect to clamd on 
%s:%d\n",
                    inet_ntoa(server2.sin_addr), ntohs(server2.sin_port));
            perror("connect()");
-           return 1;
+           return 96;
        }
 
     } else {
        mprintf("@Clamd was NOT notified: No socket specified in %s\n", 
cfgfile);
-       return 1;
+       return 97;
     }
 
     if(write(sockd, "RELOAD", 6) < 0) {
        mprintf("@Clamd was NOT notified: Could not write to %s socket\n", 
socktype);
        perror("write()");
        close(sockd);
-       return 1;
+       return 98;
     }
 
     /* TODO: Handle timeout */
@@ -126,7 +126,7 @@
        if(!strstr(buff, "RELOADING")) {
            mprintf("@Clamd was NOT notified: Unknown answer from clamd: 
'%s'\n", buff);
            close(sockd);
-           return 1;
+           return 99;
        }
 
     close(sockd);
diff -urN clamav-0.83/freshclam/options.c clamav-0.83.1/freshclam/options.c
--- clamav-0.83/freshclam/options.c     Sat Feb 12 00:02:44 2005
+++ clamav-0.83.1/freshclam/options.c   Fri Feb 18 16:16:21 2005
@@ -38,7 +38,7 @@
        int ret, opt_index, i, len;
        struct optstruct *opt;
 
-       const char *getopt_parameters = "hvdp:Vl:c:u:";
+       const char *getopt_parameters = "hvdfp:Vl:c:u:";
 
        static struct option long_options[] = {
            /* 
@@ -55,6 +55,7 @@
            {"log-verbose", 0, 0, 0}, /* not used */
            {"stdout", 0, 0, 0},
            {"daemon", 0, 0, 'd'},
+           {"foreground", 0, 0, 'f'},
            {"pid", 1, 0, 'p'},
            {"user", 1, 0, 'u'}, /* not used */
            {"config-file", 1, 0, 0},


----- End forwarded message -----

-- 
 --------------------------------------------------------------------------
|  Stephen Gran                  |  "Whatever the missing mass of the      |
|  [EMAIL PROTECTED]             | universe is, I hope it's not            |
|  http://www.lobefin.net/~steve | cockroaches!"   -- Mom                  |
 --------------------------------------------------------------------------

Attachment: pgpcNKripO0H8.pgp
Description: PGP signature

_______________________________________________
http://lists.clamav.net/cgi-bin/mailman/listinfo/clamav-devel

Reply via email to