Changeset 264 for trunk


Ignore:
Timestamp:
Dec 14, 2009, 8:54:23 PM (15 years ago)
Author:
katerina
Message:

Fix for ticket #182 (too many UID/GID lookups).

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Makefile.in

    r262 r264  
    16601660
    16611661samhain.o: $(srcsrc)/samhain.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_files.h $(srcinc)/sh_utils.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_getopt.h $(srcinc)/sh_readconf.h $(srcinc)/sh_hash.h $(srcinc)/sh_nmail.h $(srcinc)/sh_tiger.h $(srcinc)/sh_gpg.h $(srcinc)/sh_mem.h $(srcinc)/sh_forward.h $(srcinc)/sh_tools.h $(srcinc)/sh_hash.h $(srcinc)/sh_extern.h $(srcinc)/sh_modules.h $(srcinc)/sh_ignore.h $(srcinc)/sh_prelink.h sh_MK.h $(srcinc)/sh_schedule.h
    1662 sh_unix.o: $(srcsrc)/sh_unix.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_hash.h $(srcinc)/sh_tools.h $(srcinc)/sh_tiger.h $(srcinc)/sh_prelink.h $(srcinc)/sh_pthread.h $(srcinc)/sh_static.h $(srcinc)/sh_prelude.h $(srcinc)/sh_ignore.h
     1662sh_unix.o: $(srcsrc)/sh_unix.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_hash.h $(srcinc)/sh_tools.h $(srcinc)/sh_tiger.h $(srcinc)/sh_prelink.h $(srcinc)/sh_pthread.h $(srcinc)/sh_static.h $(srcinc)/sh_prelude.h $(srcinc)/zAVLTree.h $(srcinc)/sh_ignore.h
    16631663sh_utils.o: $(srcsrc)/sh_utils.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_utils.h $(srcinc)/sh_unix.h $(srcinc)/sh_tiger.h $(srcinc)/sh_entropy.h $(srcinc)/sh_pthread.h
    16641664sh_error.o: $(srcsrc)/sh_error.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_cat.h $(srcinc)/sh_database.h $(srcinc)/sh_error.h $(srcinc)/sh_utils.h $(srcinc)/sh_unix.h $(srcinc)/sh_tiger.h $(srcinc)/sh_nmail.h $(srcinc)/sh_forward.h $(srcinc)/sh_prelude.h $(srcinc)/sh_pthread.h $(srcinc)/sh_tools.h $(srcinc)/sh_extern.h
  • trunk/depend.dep

    r262 r264  
    22# DO NOT DELETE THIS LINE
    33samhain.o: $(srcsrc)/samhain.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_files.h $(srcinc)/sh_utils.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_getopt.h $(srcinc)/sh_readconf.h $(srcinc)/sh_hash.h $(srcinc)/sh_nmail.h $(srcinc)/sh_tiger.h $(srcinc)/sh_gpg.h $(srcinc)/sh_mem.h $(srcinc)/sh_forward.h $(srcinc)/sh_tools.h $(srcinc)/sh_hash.h $(srcinc)/sh_extern.h $(srcinc)/sh_modules.h $(srcinc)/sh_ignore.h $(srcinc)/sh_prelink.h sh_MK.h $(srcinc)/sh_schedule.h
    4 sh_unix.o: $(srcsrc)/sh_unix.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_hash.h $(srcinc)/sh_tools.h $(srcinc)/sh_tiger.h $(srcinc)/sh_prelink.h $(srcinc)/sh_pthread.h $(srcinc)/sh_static.h $(srcinc)/sh_prelude.h $(srcinc)/sh_ignore.h
     4sh_unix.o: $(srcsrc)/sh_unix.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_unix.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_hash.h $(srcinc)/sh_tools.h $(srcinc)/sh_tiger.h $(srcinc)/sh_prelink.h $(srcinc)/sh_pthread.h $(srcinc)/sh_static.h $(srcinc)/sh_prelude.h $(srcinc)/zAVLTree.h $(srcinc)/sh_ignore.h
    55sh_utils.o: $(srcsrc)/sh_utils.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_error.h $(srcinc)/sh_utils.h $(srcinc)/sh_unix.h $(srcinc)/sh_tiger.h $(srcinc)/sh_entropy.h $(srcinc)/sh_pthread.h
    66sh_error.o: $(srcsrc)/sh_error.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_cat.h $(srcinc)/sh_database.h $(srcinc)/sh_error.h $(srcinc)/sh_utils.h $(srcinc)/sh_unix.h $(srcinc)/sh_tiger.h $(srcinc)/sh_nmail.h $(srcinc)/sh_forward.h $(srcinc)/sh_prelude.h $(srcinc)/sh_pthread.h $(srcinc)/sh_tools.h $(srcinc)/sh_extern.h
  • trunk/depend.sum

    r262 r264  
    1 2976518268
     1188553029
  • trunk/include/sh_unix.h

    r197 r264  
    156156extern int sh_unix_check_acl;
    157157
     158/* destroy userid cache
     159 */
     160void sh_userid_destroy ();
     161
    158162/* --- run a command, securely ---
    159163 */
  • trunk/src/samhain.c

    r256 r264  
    21682168        continue;
    21692169     
     2170      /* reset cache
     2171       */
     2172      sh_userid_destroy();
     2173
    21702174      /* go to sleep
    21712175       */
  • trunk/src/sh_forward.c

    r262 r264  
    53805380          tchkold = tcurrent;
    53815381          client_time_check(/* all_clients */);
     5382          /* reset cache */
     5383          sh_userid_destroy();
    53825384        }
    53835385     
  • trunk/src/sh_socket.c

    r252 r264  
    384384      sl_strlcat(sh_sockname, _(".sock"), size);
    385385    }
     386
    386387
    387388  pf_unix_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  • trunk/src/sh_unix.c

    r263 r264  
    14991499#endif
    15001500  int  test;
    1501   struct sigaction act, oldact;
    15021501  int  status;
     1502  struct sigaction act;
     1503#if !defined(SH_PROFILE)
     1504  struct sigaction oldact;
     1505#endif
    15031506
    15041507  sigset_t set_proc;
     
    24802483  struct passwd * tempres;
    24812484  int    status = 0;
     2485
    24822486#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
    24832487  struct passwd pwd;
     
    25242528}
    25252529
    2526 SH_MUTEX_STATIC(mutex_getUIDname, PTHREAD_MUTEX_INITIALIZER);
    2527 
     2530/* ------------------- Caching ----------------*/
     2531#include "zAVLTree.h"
     2532
     2533#define CACHE_GID 0
     2534#define CACHE_UID 1
     2535
     2536struct user_id {
     2537  char  * name;
     2538  uid_t   id;
     2539  struct user_id * next;
     2540};
     2541
     2542static struct user_id  * uid_list = NULL;
     2543static struct user_id  * gid_list = NULL;
     2544
     2545SH_MUTEX_STATIC(mutex_cache, PTHREAD_MUTEX_INITIALIZER);
     2546
     2547static void sh_userid_free(struct user_id * item)
     2548{
     2549  while (item)
     2550    {
     2551      struct user_id * user = item;
     2552      item = item->next;
     2553
     2554      SH_FREE(user->name);
     2555      SH_FREE(user);
     2556    }
     2557  return;
     2558}
     2559
     2560void sh_userid_destroy ()
     2561{
     2562  struct user_id * tmp_uid;
     2563  struct user_id * tmp_gid;
     2564
     2565  SH_MUTEX_LOCK_UNSAFE(mutex_cache);
     2566  tmp_gid  = gid_list;
     2567  gid_list = NULL;
     2568  tmp_uid  = uid_list;
     2569  uid_list = NULL;
     2570  SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
     2571
     2572  sh_userid_free(tmp_uid);
     2573  sh_userid_free(tmp_gid);
     2574  return;
     2575}
     2576
     2577static void sh_userid_additem(struct user_id * list, struct user_id * item)
     2578{
     2579  while (list && list->next)
     2580    list = list->next;
     2581  list->next = item;
     2582  return;
     2583}
     2584
     2585static void sh_userid_add(uid_t id, char * username, int which)
     2586{
     2587  size_t len;
     2588  struct user_id * user = SH_ALLOC(sizeof(struct user_id));
     2589
     2590  if (username)
     2591    len  = strlen(username) + 1;
     2592  else
     2593    len = 1;
     2594
     2595  user->name = SH_ALLOC(len);
     2596  user->id   = id;
     2597  if (username)
     2598    sl_strlcpy(user->name, username, len);
     2599  else
     2600    user->name[0] = '\0';
     2601  user->next = NULL;
     2602
     2603  SH_MUTEX_LOCK(mutex_cache);
     2604  if (which == CACHE_UID)
     2605    {
     2606      if (!uid_list)
     2607        uid_list = user;
     2608      else
     2609        sh_userid_additem(uid_list, user);
     2610    }
     2611  else
     2612    {
     2613      if (!gid_list)
     2614        gid_list = user;
     2615      else
     2616        sh_userid_additem(gid_list, user);
     2617    }
     2618  SH_MUTEX_UNLOCK(mutex_cache);
     2619
     2620  return;
     2621}
     2622
     2623static char * sh_userid_search(struct user_id * list, uid_t id)
     2624{
     2625  while (list)
     2626    {
     2627      if (list->id == id)
     2628        return list->name;
     2629      list = list->next;
     2630    }
     2631  return NULL;
     2632}
     2633
     2634static char * sh_userid_get (uid_t id, int which, char * out, size_t len)
     2635{
     2636  char * user = NULL;
     2637
     2638  SH_MUTEX_LOCK_UNSAFE(mutex_cache);
     2639  if (which == CACHE_UID)
     2640    user = sh_userid_search(uid_list, id);
     2641  else
     2642    user = sh_userid_search(gid_list, id);
     2643  if (user)
     2644    {
     2645      sl_strlcpy(out, user, len);
     2646      user = out;
     2647    }
     2648  SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
     2649
     2650  return user;
     2651}
     2652
     2653/* --------- end caching code --------- */
     2654 
    25282655char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len)
    25292656{
     
    25342661#endif
    25352662  int             status = 0;
    2536   static uid_t    old_uid;
    2537   static char     name[32] = { '\0' };
    25382663  char errbuf[SH_ERRBUF_SIZE];
     2664  char * tmp;
    25392665
    25402666  SL_ENTER(_("sh_unix_getUIDname"));
    25412667
    2542   if ((uid == old_uid) && (name[0] != '\0')) {
    2543     out[0] = '\0';
    2544     SH_MUTEX_LOCK_UNSAFE(mutex_getUIDname);
    2545     if ((uid == old_uid) && (name[0] != '\0')) {
    2546       sl_strlcpy(out, name, len);
    2547     }
    2548     SH_MUTEX_UNLOCK_UNSAFE(mutex_getUIDname);
    2549     if (out[0] != '\0')
    2550       SL_RETURN( out, _("sh_unix_getUIDname"));
    2551   }
     2668  tmp = sh_userid_get(uid, CACHE_UID, out, len);
     2669
     2670  if (tmp)
     2671    {
     2672      if (tmp[0] != '\0')
     2673        {
     2674          SL_RETURN( out, _("sh_unix_getUIDname"));
     2675        }
     2676      else
     2677        {
     2678          SL_RETURN( NULL, _("sh_unix_getUIDname"));
     2679        }
     2680    }
    25522681
    25532682#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     
    25602689#endif
    25612690 
    2562   if (tempres == NULL) {
    2563     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
    2564                      sh_error_message(status, errbuf, sizeof(errbuf)),
    2565                      _("getpwuid"), (long) uid, _("completely missing"));
     2691  if (tempres == NULL)
     2692    {
     2693      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
     2694                       sh_error_message(status, errbuf, sizeof(errbuf)),
     2695                       _("getpwuid"), (long) uid, _("completely missing"));
    25662696#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2567     SH_FREE(buffer);
    2568 #endif
    2569     SL_RETURN( NULL, _("sh_unix_getUIDname"));
    2570   }
    2571 
    2572 
    2573   if (tempres->pw_name != NULL) {
    2574     SH_MUTEX_LOCK_UNSAFE(mutex_getUIDname);
    2575     sl_strlcpy(name, tempres->pw_name, sizeof(name));
    2576     old_uid = uid;
    2577     sl_strlcpy(out, name, len);
    2578     SH_MUTEX_UNLOCK_UNSAFE(mutex_getUIDname);
     2697      SH_FREE(buffer);
     2698#endif
     2699      sh_userid_add(uid, NULL, CACHE_UID);
     2700      SL_RETURN( NULL, _("sh_unix_getUIDname"));
     2701    }
     2702
     2703
     2704  if (tempres->pw_name != NULL)
     2705    {
     2706
     2707      sl_strlcpy(out, tempres->pw_name, len);
     2708      sh_userid_add(uid, out, CACHE_UID);
     2709     
    25792710#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2580     SH_FREE(buffer);
    2581 #endif
    2582     SL_RETURN( out, _("sh_unix_getUIDname"));
    2583   } else {
    2584     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
    2585                      sh_error_message(status, errbuf, sizeof(errbuf)),
    2586                      _("getpwuid"), (long) uid, _("pw_user"));
     2711      SH_FREE(buffer);
     2712#endif
     2713
     2714      SL_RETURN( out, _("sh_unix_getUIDname"));
     2715    }
     2716  else
     2717    {
     2718      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
     2719                       sh_error_message(status, errbuf, sizeof(errbuf)),
     2720                       _("getpwuid"), (long) uid, _("pw_user"));
    25872721#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2588     SH_FREE(buffer);
    2589 #endif
    2590     SL_RETURN( NULL, _("sh_unix_getUIDname"));
    2591   }
    2592 }
    2593 
    2594 SH_MUTEX_STATIC(mutex_getGIDname, PTHREAD_MUTEX_INITIALIZER);
     2722      SH_FREE(buffer);
     2723#endif
     2724      SL_RETURN( NULL, _("sh_unix_getUIDname"));
     2725    }
     2726  /* notreached */
     2727}
    25952728
    25962729char *  sh_unix_getGIDname (int level, gid_t gid, char * out, size_t len)
     
    25982731  struct group  * tempres;
    25992732  int             status = 0;
    2600   static gid_t    old_gid;
    2601   static char     name[32] = { '\0' };
     2733
    26022734#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    26032735  struct group    grp;
     
    26052737#endif
    26062738  char errbuf[SH_ERRBUF_SIZE];
     2739  char * tmp;
    26072740 
    2608 
    26092741  SL_ENTER(_("sh_unix_getGIDname"));
    26102742
    2611   if ((gid == old_gid) && (name[0] != '\0')) {
    2612     out[0] = '\0';
    2613     SH_MUTEX_LOCK_UNSAFE(mutex_getGIDname);
    2614     if ((gid == old_gid) && (name[0] != '\0')) {
    2615       sl_strlcpy(out, name, len);
    2616     }
    2617     SH_MUTEX_UNLOCK_UNSAFE(mutex_getGIDname);
    2618     if (out[0] != '\0')
    2619       SL_RETURN( out, _("sh_unix_getGIDname"));
    2620   }
     2743  tmp = sh_userid_get((uid_t)gid, CACHE_GID, out, len);
     2744
     2745  if (tmp)
     2746    {
     2747      if (tmp[0] != '\0')
     2748        {
     2749          SL_RETURN( out, _("sh_unix_getGIDname"));
     2750        }
     2751      else
     2752        {
     2753          SL_RETURN( NULL, _("sh_unix_getGIDname"));
     2754        }
     2755    }
    26212756
    26222757#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     
    26292764#endif
    26302765
    2631   if (tempres == NULL) {
    2632     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
    2633                      sh_error_message(status, errbuf, sizeof(errbuf)),
    2634                      _("getgrgid"), (long) gid, _("completely missing"));
     2766  if (tempres == NULL)
     2767    {
     2768      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
     2769                       sh_error_message(status, errbuf, sizeof(errbuf)),
     2770                       _("getgrgid"), (long) gid, _("completely missing"));
    26352771     
    26362772#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2637     SH_FREE(buffer);
    2638 #endif
    2639     SL_RETURN( NULL, _("sh_unix_getGIDname"));
    2640   }
    2641 
    2642   if (tempres->gr_name != NULL) {
    2643     SH_MUTEX_LOCK_UNSAFE(mutex_getGIDname);
    2644     sl_strlcpy(name, tempres->gr_name, sizeof(name));
    2645     old_gid = gid;
    2646     sl_strlcpy(out, name, len);
    2647     SH_MUTEX_UNLOCK_UNSAFE(mutex_getGIDname);
     2773      SH_FREE(buffer);
     2774#endif
     2775
     2776      sh_userid_add(gid, NULL, CACHE_GID);
     2777      SL_RETURN( NULL, _("sh_unix_getGIDname"));
     2778    }
     2779
     2780  if (tempres->gr_name != NULL)
     2781    {
     2782
     2783      sl_strlcpy(out, tempres->gr_name, len);
     2784      sh_userid_add((uid_t)gid, out, CACHE_GID);
     2785     
    26482786#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2649     SH_FREE(buffer);
    2650 #endif
    2651     SL_RETURN( out, _("sh_unix_getGIDname"));
    2652   } else {
    2653     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
    2654                      sh_error_message(status, errbuf, sizeof(errbuf)),
    2655                      _("getgrgid"), (long) gid, _("gr_name"));
     2787      SH_FREE(buffer);
     2788#endif
     2789
     2790      SL_RETURN( out, _("sh_unix_getGIDname"));
     2791    }
     2792  else
     2793    {
     2794      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
     2795                       sh_error_message(status, errbuf, sizeof(errbuf)),
     2796                       _("getgrgid"), (long) gid, _("gr_name"));
     2797
    26562798#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
    2657     SH_FREE(buffer);
    2658 #endif
    2659     SL_RETURN( NULL, _("sh_unix_getGIDname"));
    2660   }
     2799      SH_FREE(buffer);
     2800#endif
     2801
     2802      SL_RETURN( NULL, _("sh_unix_getGIDname"));
     2803    }
     2804  /* notreached */
    26612805}
    26622806
Note: See TracChangeset for help on using the changeset viewer.