Changeset 488 for trunk/src/sh_audit.c


Ignore:
Timestamp:
Sep 18, 2015, 7:39:03 PM (9 years ago)
Author:
katerina
Message:

Fix for tickets #386 (silent check) and #387 (linux audit support).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_audit.c

    r434 r488  
    2828#include <errno.h>
    2929#include <unistd.h>
     30#include <fcntl.h>
     31#include <sys/stat.h>
     32
     33#include "samhain.h"
     34#include "sh_error.h"
    3035
    3136#if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB)
    3237#include <auparse.h>
    3338
    34 #include "samhain.h"
    35 #include "sh_error.h"
    3639#include "sh_extern.h"
    3740#include "sh_utils.h"
     
    103106}
    104107   
    105 static char * doAuparse (char * file, time_t time, char * result, size_t rsize)
     108static char * doAuparse (const char * file, time_t time, int tol, char * result, size_t rsize, int redo_flag)
    106109{
    107110  struct recordState state;
    108111  struct recordState stateFetched;
    109 
     112  unsigned int       found_flag = 0;
     113 
    110114  auparse_state_t * au = auparse_init(AUSOURCE_LOGS, NULL);
    111115
     
    131135  if (time != 0)
    132136    {
    133       ausearch_add_timestamp_item(au, ">=", time-1, 0, AUSEARCH_RULE_AND);
    134       ausearch_add_timestamp_item(au, "<=", time+1, 0, AUSEARCH_RULE_AND);
     137      ausearch_add_timestamp_item(au, ">=", time-tol, 0, AUSEARCH_RULE_AND);
     138      ausearch_add_timestamp_item(au, "<=", time+tol, 0, AUSEARCH_RULE_AND);
    135139    }
    136140
     
    152156        {
    153157          memcpy(&state, &stateFetched, sizeof(state));
     158          ++found_flag;
    154159        }
    155160      auparse_next_event(au);
    156161    }
    157162
     163  if (found_flag == 0 && redo_flag == S_FALSE)
     164    {
     165      size_t len = strlen(file);
     166      char * path = SH_ALLOC(len + 2);
     167      char * altres;
     168     
     169      sl_strlcpy(path, file, len+2);
     170      path[len] = '/'; path[len+1] = '\0';
     171      auparse_destroy(au);
     172     
     173      altres = doAuparse(path, time, tol, result, rsize, S_TRUE);
     174
     175      SH_FREE(path);
     176      return altres;
     177    }
     178 
    158179  if (0 == strcmp(state.success, "yes"))
    159180    {
     
    189210 * The 'result' array should be sized ~256 char.
    190211 */
    191 char * sh_audit_fetch (char * file, time_t time, char * result, size_t rsize)
     212char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, char * result, size_t rsize)
    192213{
    193214  char * res = NULL;
     
    195216  if (sh_audit_checkdaemon() >= 0)
    196217    {
    197       res = doAuparse (file, time, result, rsize);
     218      time_t new;
     219      char buf[64];
     220     
     221      if (mtime >= ctime) { new = mtime; }
     222      else                { new = ctime; }
     223
     224      res = doAuparse (file, new, 1, result, rsize, S_FALSE);
    198225
    199226      if (!res)
    200227        {
    201           res = doAuparse (file, 0, result, rsize);
     228          res = doAuparse (file, new, 3, result, rsize, S_FALSE);
    202229        }
     230
    203231    }
    204232  return res;
     
    226254}
    227255
     256static int  sh_audit_isdir(const char * file)
     257{
     258  struct stat buf;
     259
     260  if ( (0 == lstat (file, &buf)) && S_ISDIR(buf.st_mode))
     261    return S_TRUE;
     262  return S_FALSE;
     263}
     264
    228265static void sh_audit_mark_int (const char * file)
    229266{
     
    246283      char * safe;
    247284      char   ctl[64];
     285      char   a1[32];
     286      char   a2[32];
     287      char   a3[32];
    248288
    249289      sl_snprintf(command, len, _("%s -w %s -p wa -k samhain"),
     
    256296                       safe,
    257297                       _("sh_audit_mark") );
     298
    258299      SH_FREE(safe);
    259300
     
    261302      sl_strlcpy(command, file, len);
    262303
    263       sh_ext_system(ctl, ctl, "-w", command, "-p", "wa", "-k", _("samhain"), NULL);
    264 
     304      sl_strlcpy(a3, _("samhain"), sizeof(a3));
     305      sh_ext_system(ctl, ctl, "-w", command, "-p", "wa", "-k", a3, NULL);
     306
     307      /* Placing a watch on a directory will not place a watch on the
     308       * directory inode, so we do this explicitely.
     309       */
     310      if (S_TRUE == sh_audit_isdir(file))
     311        {
     312          safe = sh_util_safe_name(file);
     313          sh_error_handle (SH_ERR_ALL, FIL__, __LINE__,
     314                           0, MSG_E_SUBGPATH,
     315                           _("Add path watch for directory"),
     316                           _("sh_audit_mark_int"), safe );
     317          SH_FREE(safe);
     318          sl_strlcpy(command, _("path="), len);
     319          sl_strlcat(command, file, len);
     320          sl_strlcpy(a1, _("always,exit"), sizeof(a1));
     321          sl_strlcpy(a2, _("perm=wa"), sizeof(a2));
     322          sh_ext_system(ctl, ctl, "-a", a1, "-F", command, "-F", a2, "-k", a3, NULL);
     323        }
    265324      SH_FREE(command);
    266325    }
     
    278337{
    279338  struct aud_list * this = SH_ALLOC(sizeof(struct aud_list));
     339  size_t len = strlen(file);
     340
    280341  this->file = sh_util_strdup(file);
     342  if ((len > 1) && (file[len-1] == '/'))
     343    this->file[len-1] = '\0';
     344 
    281345  this->next = mark_these;
    282346  mark_these = this;
     
    284348}
    285349
     350/* Check whether it is already covered by a higher directory
     351 */
    286352static int test_exchange (struct aud_list * this, char * file)
    287353{
     
    312378           if (0 == strncmp(s0, s1, len1 + 1))
    313379             {
     380               size_t len = strlen(file);
    314381               SH_FREE(this->file);
    315382               this->file = sh_util_strdup(file);
     383               if ((len > 1) && (file[len-1] == '/'))
     384                 this->file[len-1] = '\0';
    316385               ret = 0;
    317386             }
     
    324393}
    325394
     395/* Place a path on the list of of paths to be watched
     396 */
    326397void sh_audit_mark (char * file)
    327398{
     
    335406  while (this)
    336407    {
     408      /* Check whether it is already covered by a higher
     409       * directory
     410       */
    337411      if (0 == test_exchange(this, file))
    338412        return;
     
    464538/* HAVE_AUPARSE_H */
    465539#else
    466 char * sh_audit_fetch (char * file, time_t time, char * result, size_t rsize)
     540char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, char * result, size_t rsize)
    467541{
    468542  (void) file;
    469   (void) time;
     543  (void) mtime;
     544  (void) ctime;
    470545  (void) result;
    471546  (void) rsize;
     
    476551{
    477552  (void) file;
     553  sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
     554                  _("Setting audit watch not supported"),
     555                  _("sh_audit_mark"));
    478556  return;
    479557}
Note: See TracChangeset for help on using the changeset viewer.