Changeset 215 for trunk/src


Ignore:
Timestamp:
Feb 18, 2009, 7:11:26 PM (16 years ago)
Author:
katerina
Message:

Consolidate filtering code (ticket #142) and match on regular expressions (ticket #143).

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/samhain.c

    r214 r215  
    746746#if defined(SH_WITH_SERVER)
    747747  sh_forward_free_all ();
     748#endif
     749#if defined(SH_WITH_MAIL)
     750  sh_nmail_free();
    748751#endif
    749752  delete_cache();
     
    17491752              (void) sh_ext_cleanup();
    17501753#endif
     1754#if defined(SH_WITH_MAIL)
     1755              sh_nmail_free();
     1756#endif
     1757
    17511758              /* delete the file list, make all database
    17521759               * entries visible (allignore = FALSE)
  • trunk/src/sh_extern.c

    r214 r215  
    9292#include "sh_extern.h"
    9393#include "sh_calls.h"
     94#include "sh_filter.h"
    9495#define SH_NEED_PWD_GRP 1
    9596#include "sh_static.h"
     
    867868  char     type[4];
    868869
    869   int      for_c;
    870   char   * for_v[32];
    871   int      fand_c;
    872   char   * fand_v[32];
    873   int      fnot_c;
    874   char   * fnot_v[32];
     870  sh_filter_type * filter;
     871
    875872  time_t   deadtime;
    876873  time_t   last_run;
     
    900897sh_com_t * command_init(void)
    901898{
    902   int         i;
    903899  uid_t       ff_euid;
    904900  sh_com_t  * ext_com = NULL;
     
    924920
    925921  set3(ext_com->type, 'l', 'o', 'g');
    926   ext_com->for_c        = 0;
    927   ext_com->fand_c       = 0;
    928   ext_com->fnot_c       = 0;
     922  ext_com->filter       = NULL;
    929923  ext_com->deadtime     = 0;
    930924  ext_com->last_run     = 0;
    931925
    932   for (i = 0; i < 32; ++i)
    933     {
    934       ext_com->for_v[i]         = NULL;
    935       ext_com->fand_v[i]        = NULL;
    936       ext_com->fnot_v[i]        = NULL;
    937     }
    938926  ext_com->next             = NULL;
    939927
     
    11281116int sh_ext_cleanup(void)
    11291117{
    1130   int i;
    11311118  sh_com_t * retval;
    11321119
     
    11401127      sh_ext_tas_free (&(retval->tas));
    11411128
    1142       for (i = 0; i < 32; ++i)
    1143         {
    1144           if (NULL != retval->for_v[i])  SH_FREE(retval->for_v[i]);
    1145           if (NULL != retval->fand_v[i]) SH_FREE(retval->fand_v[i]);
    1146           if (NULL != retval->fnot_v[i]) SH_FREE(retval->fnot_v[i]);
    1147         }
     1129      if (retval->filter)
     1130        sh_filter_free (retval->filter);
    11481131
    11491132      SH_FREE(retval);
     
    11731156  if (ext_coms == NULL || ext_failed == (-1))
    11741157    return (-1);
    1175   return (sh_ext_add (str, &(ext_coms->for_c), ext_coms->for_v));
     1158  if (ext_coms->filter == NULL)
     1159    ext_coms->filter = sh_filter_alloc();
     1160  return (sh_filter_add(str, ext_coms->filter, SH_FILT_OR));
    11761161}
    11771162
     
    11831168  if (ext_coms == NULL || ext_failed == (-1))
    11841169    return (-1);
    1185   return (sh_ext_add (str, &(ext_coms->fand_c), ext_coms->fand_v));
     1170  if (ext_coms->filter == NULL)
     1171    ext_coms->filter = sh_filter_alloc();
     1172  return (sh_filter_add(str, ext_coms->filter, SH_FILT_AND));
    11861173}
    11871174
     
    11931180  if (ext_coms == NULL || ext_failed == (-1))
    11941181    return (-1);
    1195   return (sh_ext_add (str, &(ext_coms->fnot_c), ext_coms->fnot_v));
     1182  if (ext_coms->filter == NULL)
     1183    ext_coms->filter = sh_filter_alloc();
     1184  return (sh_filter_add(str, ext_coms->filter, SH_FILT_NOT));
    11961185}
    11971186
     
    13541343static int sh_ext_filter (char * message, sh_com_t * task)
    13551344{
    1356   int i;
    1357   int j = 0;
    13581345  time_t now_time;
    13591346
    13601347  SL_ENTER(_("sh_ext_filter"));
    13611348
    1362   /* Presence of any of these keywords prevents execution.
    1363    */
    1364   if (task->fnot_c > 0)
    1365     {
    1366       for (i = 0; i < task->fnot_c; ++i)
     1349  if (task->filter)
     1350    {
     1351      if (0 != sh_filter_filter (message, task->filter))
    13671352        {
    1368           if (NULL != sl_strstr(message, task->fnot_v[i]))
    1369             {
    1370               SL_RETURN ((-1), _("sh_ext_filter"));
    1371             }
     1353          SL_RETURN ((-1), _("sh_ext_filter"));
    13721354        }
    13731355    }
    13741356
    1375   /* Presence of all of these keywords is required for execution.
    1376    */
    1377   if (task->fand_c > 0)
    1378     {
    1379       j = 0;
    1380 
    1381       for (i = 0; i < task->fand_c; ++i)
    1382         {
    1383           if (NULL == sl_strstr(message, task->fand_v[i]))
    1384             {
    1385               SL_RETURN ((-1), _("sh_ext_filter"));
    1386             }
    1387         }
    1388 
    1389     }
    1390 
    1391   /* Presence of at least one of these keywords is required for execution.
    1392    */
    1393   if (task->for_c > 0)
    1394     {
    1395       for (i = 0; i < task->for_c; ++i)
    1396         {
    1397           if (NULL != sl_strstr(message, task->for_v[i]))
    1398             {
    1399               goto checkdeadtime;
    1400             }
    1401         }
    1402       SL_RETURN ((-1), _("sh_ext_filter"));
    1403     }
    1404 
    1405  checkdeadtime:
    1406   if (task->deadtime != (time_t) 0)   /* deadtime */
     1357  /* Filter passed, check deadtime */
     1358
     1359  if (task->deadtime != (time_t) 0)
    14071360    {
    14081361      now_time = time (NULL);
  • trunk/src/sh_forward.c

    r214 r215  
    50565056            (void) sh_ext_cleanup();
    50575057#endif
     5058#if defined(SH_WITH_MAIL)
     5059              sh_nmail_free();
     5060#endif
    50585061            /* - mark all clients dead
    50595062             * - read configuration file
  • trunk/src/sh_mail.c

    r214 r215  
    5959#include "sh_tools.h"
    6060#include "sh_pthread.h"
     61#include "sh_filter.h"
    6162#include "sh_mail_int.h"
    6263#include "sh_nmail.h"
     
    245246
    246247  /*@notreached@*/
    247 }
    248 
    249 void sh_filter_filterfree (sh_filter_type * filter)
    250 {
    251   int i;
    252 
    253   if (filter)
    254     {
    255       for (i = 0; i < filter->for_c; ++i) {
    256         if (filter->for_v[i])
    257           SH_FREE(filter->for_v[i]);
    258         filter->for_v[i] = NULL;
    259       }
    260       filter->for_c = 0;
    261       for (i = 0; i < filter->fand_c; ++i) {
    262         if (filter->fand_v[i])
    263           SH_FREE(filter->fand_v[i]);
    264         filter->fand_v[i] = NULL;
    265       }
    266       filter->fand_c = 0;
    267       for (i = 0; i < filter->fnot_c; ++i) {
    268         if (filter->fnot_v[i])
    269           SH_FREE(filter->fnot_v[i]);
    270         filter->fnot_v[i] = NULL;
    271       }
    272       filter->fnot_c = 0;
    273     }
    274 }
    275 
    276 int sh_filter_filteradd (const char * argstring,
    277                          sh_filter_type * filter, int ftype)
    278 {
    279   int     i = 0;
    280   int     flag = 0;
    281   size_t  s;
    282 
    283   char  * dupp;
    284   char  * p;
    285   char  * end;
    286   int   * ntok;
    287   char ** stok;
    288 
    289   SL_ENTER(_("sh_filter_filteradd"));
    290 
    291   if (NULL == argstring)
    292     {
    293       SL_RETURN((-1), _("sh_filter_filteradd"));
    294     }
    295 
    296   if (ftype == SH_FILT_OR) {
    297     ntok = &(filter->for_c);
    298     stok = filter->for_v;
    299   }
    300   else if (ftype == SH_FILT_AND) {
    301     ntok = &(filter->fand_c);
    302     stok = filter->fand_v;
    303   }
    304   else if (ftype == SH_FILT_NOT) {
    305     ntok = &(filter->fnot_c);
    306     stok = filter->fnot_v;
    307   }
    308   else {
    309     SL_RETURN((-1), _("sh_filter_filteradd"));
    310   }
    311 
    312   *ntok = 0;
    313 
    314   dupp = sh_util_strdup(argstring);
    315   p   = dupp;
    316 
    317   do
    318     {
    319       while (*p == ',' || *p == ' ' || *p == '\t')
    320         ++p;
    321       if (*p == '\0')
    322         break;
    323 
    324       end = p; ++end;
    325       if (*end == '\0')
    326         break;
    327 
    328       if (*p == '\'')
    329         {
    330           ++p; end = p; ++end;
    331           if (*p == '\0' || *end == '\0')
    332             break;
    333           while (*end != '\0' && *end != '\'')
    334             ++end;
    335         }
    336       else if (*p == '"')
    337         {
    338           ++p; end = p; ++end;
    339           if (*p == '\0' || *end == '\0')
    340             break;
    341           while (*end != '\0' && *end != '"')
    342             ++end;
    343         }
    344       else
    345         {
    346           while (*end != '\0' && *end != ',' && *end != ' ' && *end != '\t')
    347             ++end;
    348         }
    349       if (*end == '\0')
    350         flag = 1;
    351       else
    352         *end = '\0';
    353 
    354       s = strlen(p) + 1;
    355       if (stok[i] != NULL)
    356         SH_FREE(stok[i]);
    357       stok[i] = SH_ALLOC(s);
    358       (void) sl_strlcpy(stok[i], p, s);
    359 
    360       p = end; ++p;
    361 
    362       ++i;
    363       if (i == SH_FILT_NUM)
    364         break;
    365     }
    366   while (p != NULL && *p != '\0' && flag == 0);
    367 
    368   *ntok = i;
    369   SH_FREE(dupp);
    370 
    371   SL_RETURN (0, _("sh_filter_filteradd"));
    372 }
    373 
    374 /*
    375  * -- Check filters. Returns 0 if message passes.
    376  */
    377 int sh_filter_filter (const char * message, sh_filter_type * filter)
    378 {
    379   int i;
    380 
    381   SL_ENTER(_("sh_filter_filter"));
    382 
    383   if (filter)
    384     {
    385 
    386       /* Presence of any of these keywords prevents execution.
    387        */
    388       if (filter->fnot_c > 0)
    389         {
    390           for (i = 0; i < filter->fnot_c; ++i)
    391             {
    392               if (NULL != sl_strstr(message, filter->fnot_v[i]))
    393                 {
    394                   SL_RETURN ((-1), _("sh_filter_filter"));
    395                 }
    396             }
    397         }
    398      
    399       /* Presence of all of these keywords is required for execution.
    400        */
    401       if (filter->fand_c > 0)
    402         {
    403           for (i = 0; i < filter->fand_c; ++i)
    404             {
    405               if (NULL == sl_strstr(message, filter->fand_v[i]))
    406                 {
    407                   SL_RETURN ((-1), _("sh_filter_filter"));
    408                 }
    409             }
    410         }
    411      
    412       /* Presence of at least one of these keywords is required for execution.
    413        */
    414       if (filter->for_c > 0)
    415         {
    416           for (i = 0; i < filter->for_c; ++i)
    417             {
    418               if (NULL != sl_strstr(message, filter->for_v[i]))
    419                 {
    420                   goto isok;
    421                 }
    422             }
    423           SL_RETURN ((-1), _("sh_filter_filter"));
    424         }
    425     }
    426 
    427  isok:
    428   SL_RETURN ((0), _("sh_filter_filter"));
    429248}
    430249
  • trunk/src/sh_readconf.c

    r214 r215  
    11071107  { N_("setmailsender"),     SH_SECTION_MAIL,  SH_SECTION_MISC,
    11081108    sh_mail_set_sender },
     1109  { N_("setmailalias"),       SH_SECTION_MAIL,  SH_SECTION_MISC,
     1110    sh_nmail_add_alias },
    11091111  { N_("setmailaddress"),    SH_SECTION_MAIL,  SH_SECTION_MISC,
    11101112    sh_nmail_add_recipient },
  • trunk/src/sh_string.c

    r185 r215  
    127127 * If nfields < actual fields, last string will be remainder.
    128128 */
    129 char ** split_array_ws(char *line, unsigned int * nfields, size_t * lengths)
     129
     130#define SH_SPLIT_LIST 0
     131#define SH_SPLIT_WS   1
     132
     133char ** split_array_ws_int (char *line,
     134                            unsigned int * nfields, size_t * lengths,
     135                            int isList)
    130136{
    131137  char *a, *e, *s;
     
    144150      /* skip leading WS
    145151       */
    146       if ( *s && isspace((int)*s) )
    147         {
    148           do {
    149             ++s;
    150           } while ( *s && isspace((int)*s) );
    151         }
     152      if (isList == SH_SPLIT_WS)
     153        {
     154          if ( *s && isspace((int)*s) )
     155            {
     156              do {
     157                ++s;
     158              } while ( *s && isspace((int)*s) );
     159            }
     160        }
     161      else
     162        {
     163          if ( *s && (*s == ' ' || *s == '\t' || *s == ','))
     164            {
     165              do {
     166                ++s;
     167              } while ( *s && (*s == ' ' || *s == '\t' || *s == ','));
     168            }
     169        }
    152170
    153171      if (*s)
     
    157175           */
    158176          a = s;
    159           do {
    160             a++;
    161           } while ( *a && (!isspace((int)*a)) );
     177          if (isList == SH_SPLIT_WS)
     178            {
     179              do {
     180                a++;
     181              } while ( *a && (!isspace((int)*a)) );
     182            }
     183          else
     184            {
     185              do {
     186                a++;
     187              } while ( *a && (*a != ' ' && *a != '\t' && *a != ','));
     188            }
    162189
    163190          /* next token, *a is either ws or '\0'
     
    201228}
    202229
     230char ** split_array_ws (char *line,
     231                        unsigned int * nfields, size_t * lengths)
     232{
     233  return split_array_ws_int (line, nfields, lengths, SH_SPLIT_WS);
     234}
     235
     236char ** split_array_list (char *line,
     237                          unsigned int * nfields, size_t * lengths)
     238{
     239  return split_array_ws_int (line, nfields, lengths, SH_SPLIT_LIST);
     240}
    203241
    204242#define SH_STRING_PARCEL 120
Note: See TracChangeset for help on using the changeset viewer.