Changeset 131 for trunk/src


Ignore:
Timestamp:
Oct 22, 2007, 11:19:15 PM (17 years ago)
Author:
rainer
Message:

Use thread-safe libc functions.

Location:
trunk/src
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/samhain.c

    r96 r131  
    6363
    6464#include "samhain.h"
     65#include "sh_pthread.h"
    6566#include "sh_files.h"
    6667#include "sh_utils.h"
     
    450451  if (0 == strcmp (DEFAULT_MAILADDRESS, _("NULL")))
    451452    {
     453#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     454      char * saveptr;
     455      (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
     456      p = strtok_r (q, ", \t", &saveptr);
     457      if (p)
     458        {
     459          (void) sh_mail_setaddress_int (p);
     460          while (NULL != (p = strtok_r (NULL, ", \t", &saveptr)))
     461            (void) sh_mail_setaddress_int (p);
     462        }
     463#else
    452464      (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
    453465      p = strtok (q, ", \t");
     
    458470            (void) sh_mail_setaddress_int (p);
    459471        }
     472#endif
    460473    }
    461474#endif
     
    747760      return NULL;
    748761    }
     762
     763  SH_MUTEX_LOCK(readdir_lock);
     764
    749765  while (NULL != (d = readdir(dp)) && i < 65535)
    750766    {
     
    767783        }
    768784    }
     785
     786  SH_MUTEX_UNLOCK(readdir_lock);
     787
    769788  closedir(dp);
    770789  return pidlist;
     
    12941313      if ( 0 == strcmp(argv[1], NOCL_CODE) )
    12951314        {
     1315#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     1316          char * saveptr;
     1317#endif
    12961318          my_argv[0] = argv[0]; ++my_argc; 
    12971319          command_line[0] = '\0';
     
    12991321          command_line[sizeof(command_line)-1] = '\0';
    13001322          do {
     1323#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
    13011324            my_argv[my_argc] =
    1302               strtok( (my_argc == 1) ? command_line : NULL, " \n");
     1325              strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
     1326#else
     1327            my_argv[my_argc] =
     1328              strtok( (my_argc == 1) ? command_line : NULL, " \n");
     1329#endif
    13031330            if (my_argv[my_argc] != NULL) {
    13041331              ++my_argc;
  • trunk/src/sh_entropy.c

    r102 r131  
    276276#if defined (HAVE_URANDOM)
    277277
    278 #include <setjmp.h>
    279 
    280 static jmp_buf          entropy_timeout;
    281 
    282 static void sh_entropy_alarmhandle (int mysignal)
    283 {
    284   (void) mysignal; /* avoid compiler warning */
    285   longjmp (entropy_timeout, 1);
    286 }
    287 
    288 
    289278int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
    290279{
    291   int count, m_count;
     280  int m_count;
    292281  int fd2;
    293 
    294   struct  sigaction  new_act;
    295   sigset_t           unblock;
    296 
    297   struct sigaction      old_act;
    298   volatile unsigned int old_alarm = 0;
    299 
    300   new_act.sa_handler = sh_entropy_alarmhandle;
    301   sigemptyset( &new_act.sa_mask );       /* set an empty mask       */
    302   new_act.sa_flags = 0;                  /* init sa_flags           */
    303 
    304   sigemptyset(&unblock);
    305   sigaddset  (&unblock, SIGALRM);
    306282
    307283  SL_ENTER(_("read_mbytes"));
     
    314290      if (0 == sh_unix_device_readable(fd2))
    315291        {
    316 
    317           /* alarm was triggered
    318            */
    319           if (setjmp(entropy_timeout) != 0)
    320             {
    321               alarm(0);
    322               sigaction (SIGALRM, &old_act, NULL);
    323               alarm(old_alarm);
    324               sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    325               TPT((0,FIL__,__LINE__, _("msg=<read_mbytes: timeout>\n")));
    326               close (fd2);
    327               SL_RETURN(0, _("read_mbytes"));
    328             }
    329 
    330           /* timeout after 30 seconds
    331            */
    332           old_alarm = alarm(0);
    333           sigaction (SIGALRM, &new_act, &old_act);
    334           alarm(timeout_val);
    335 
    336           m_count = 0;
    337 
    338           while (m_count < nbytes)
    339             {
    340               errno = 0; /* paranoia */
    341               count = read (fd2, &nbuf[m_count], nbytes-m_count);
    342 
    343               switch (count)
    344                 {
    345                 case -1:
    346 #ifdef EWOULDBLOCK
    347                   if (errno == EINTR || errno == EAGAIN ||
    348                       errno == EWOULDBLOCK)
    349 #else
    350                   if (errno == EINTR || errno == EAGAIN)
    351 #endif
    352                     continue;
    353 
    354                   /* if errno == -1 && no continue: fallthrough to this */
    355                 case 0:
    356                   break;
    357                 default:
    358                   m_count += count;
    359                 }
    360             }
    361           close (fd2);
    362 
    363           alarm(0);
    364           sigaction (SIGALRM, &old_act, NULL);
    365           alarm(old_alarm);
    366           sigprocmask(SIG_UNBLOCK, &unblock, NULL);
     292          m_count = sl_read_timeout_fd(fd2, &nbuf, nbytes,
     293                                       timeout_val, SL_FALSE);
     294          if (m_count < 0)
     295            m_count = 0;
    367296        }
    368297      else
     
    371300  else
    372301    m_count = 0;
     302
     303  close(fd2);
    373304
    374305  TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n")));
     
    568499  int pipedes[2];
    569500  FILE *outf = NULL;
    570   struct passwd * tempres;
    571501  char * arg[4];
    572502  char * envp[2];
     
    645575       */
    646576      i = 0;
    647       if (0 == geteuid()) { 
    648         tempres = sh_getpwnam(DEFAULT_IDENT);
    649         if (NULL != tempres) {
    650           i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
    651           if (i == 0)
    652             i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
    653           if (i == 0)
    654             i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
    655           /* make sure we cannot get root again
    656            */
    657           if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
     577      if (0 == geteuid())
     578        {
     579#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     580          struct passwd    pwd;
     581          char             buffer[SH_PWBUF_SIZE];
     582          struct passwd *  tempres;
     583          sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
     584#else
     585          struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
     586#endif
     587 
     588          if (NULL != tempres) {
     589            i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
     590            if (i == 0)
     591              i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
     592            if (i == 0)
     593              i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
     594            /* make sure we cannot get root again
     595             */
     596            if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
     597              i = -1;
     598          } else {
    658599            i = -1;
    659         } else {
    660           i = -1;
     600          }
    661601        }
    662       }
    663602     
    664603      /* some problem ...
  • trunk/src/sh_error.c

    r86 r131  
    257257
    258258  do {
    259     if (num == 0)
    260       {
    261         p = strtok (c, " ,\t");
    262         ++num;
    263       }
    264     else
     259#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     260    char * saveptr;
     261    if (num == 0) {
     262      p = strtok_r (c, " ,\t", &saveptr);
     263      ++num;
     264    } else {
     265      p = strtok_r (NULL, " ,\t", &saveptr);
     266    }
     267#else
     268    if (num == 0) {
     269      p = strtok (c, " ,\t");
     270      ++num;
     271    } else {
    265272      p = strtok (NULL, " ,\t");
     273    }
     274#endif
    266275
    267276    if (p == NULL)
  • trunk/src/sh_extern.c

    r102 r131  
    914914int sh_ext_uid (const char * user, /*@out@*/uid_t * uid, /*@out@*/gid_t * gid)
    915915{
    916   struct passwd * tempres;
     916  struct passwd *  tempres;
     917#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     918  struct passwd    pwd;
     919  char             buffer[SH_PWBUF_SIZE];
     920#endif
    917921
    918922  SL_ENTER(_("sh_ext_uid"));
     
    924928      SL_RETURN (-1, _("sh_ext_uid"));
    925929    }
     930
     931#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     932  sh_getpwnam_r(user, &pwd, buffer, sizeof(buffer), &tempres);
     933#else
    926934  tempres = sh_getpwnam(user);
     935#endif
    927936
    928937  if (NULL != tempres)
     
    959968  do
    960969    {
     970#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     971      char * saveptr;
     972      if (i == 0)
     973        p = strtok_r (new, ", \t", &saveptr);
     974      else
     975        p = strtok_r (NULL, ", \t", &saveptr);
     976#else
    961977      if (i == 0)
    962978        p = strtok (new, ", \t");
    963979      else
    964980        p = strtok (NULL, ", \t");
     981#endif
     982
    965983      if (p == NULL)
    966984        break;
  • trunk/src/sh_files.c

    r114 r131  
    6060#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
    6161
     62#include "sh_pthread.h"
    6263#include "sh_error.h"
    6364#include "sh_utils.h"
     
    191192  SL_RETURN((0), _("sh_files_setrecursion"));
    192193}
    193 
    194194
    195195unsigned long sh_files_chk ()
     
    13401340}
    13411341
     1342/**
    13421343struct sh_dirent {
    1343   /* char               sh_d_name[NAME_MAX + 2]; */
    13441344  char             * sh_d_name;
    13451345  struct sh_dirent * next;
    13461346};
    1347 
    1348 static void kill_sh_dirlist (struct sh_dirent * dirlist)
     1347**/
     1348
     1349void kill_sh_dirlist (struct sh_dirent * dirlist)
    13491350{
    13501351  struct sh_dirent * this;
     
    13621363/* -- add an entry to a directory listing
    13631364 */
    1364 static struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry,
    1365                                             struct sh_dirent * dirlist)
     1365struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry,
     1366                                     struct sh_dirent * dirlist)
    13661367{
    13671368  struct sh_dirent * this;
     
    14851486  dir_type        theDir;
    14861487  ShFileType      checkit;
    1487 
     1488  static unsigned int state = 1;
    14881489
    14891490  file_type       theFile;
     
    16331634  /* ---- read ----
    16341635   */
     1636  SH_MUTEX_LOCK(readdir_lock);
     1637
    16351638  do {
    16361639      thisEntry = readdir (thisDir);
     
    16521655  } while (thisEntry != NULL);
    16531656
     1657  SH_MUTEX_UNLOCK(readdir_lock);
     1658
    16541659  closedir (thisDir);
    16551660
     
    16721677
    16731678    BREAKEXIT(sh_derr);
    1674     if (0 == (rand() % 5))
    1675       (void) sh_derr();
     1679
     1680#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
     1681    if (0 == (rand_r(&state) % 5)) (void) sh_derr();
     1682#else
     1683    if (0 == state * (rand() % 5)) (void) sh_derr();
     1684#endif
    16761685   
    16771686    /* ---- Check the file. ----
     
    19291938  char          * fileName;
    19301939  struct utimbuf  utime_buf;
     1940  static unsigned int state = 1;
    19311941
    19321942  SL_ENTER(_("sh_files_filecheck"));
    19331943
    19341944  BREAKEXIT(sh_derr);
    1935   if (0 == (rand() % 2))
    1936     (void) sh_derr();
     1945
     1946#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_RAND_R)
     1947  if (0 == (rand_r(&state) % 2)) (void) sh_derr();
     1948#else
     1949  if (0 == state * (rand() % 2)) (void) sh_derr();
     1950#endif
    19371951
    19381952  if (dirName && infileName && (dirName[0] == '/') && (dirName[1] == '\0')
  • trunk/src/sh_gpg.c

    r111 r131  
    292292      struct stat lbuf;
    293293      int         status_stat = 0;
     294#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     295      struct passwd    pwd;
     296      char             buffer[SH_PWBUF_SIZE];
     297      struct passwd *  tempres;
     298      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
     299#else
    294300      struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
     301#endif
    295302
    296303      if (!tempres)
     
    844851  while (NULL != fgets(line, sizeof(line), source.pipe))
    845852    {
     853#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     854      char * saveptr = NULL;
     855#endif
    846856      if (line[strlen(line)-1] == '\n')
    847857        line[strlen(line)-1] = ' ';
     
    852862      if (sl_strlen(line) < 18)
    853863        continue;
     864#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     865      ptr = strtok_r (line, " ", &saveptr);
     866#else
    854867      ptr = strtok (line, " ");
     868#endif
    855869      while (ptr)
    856870        {
     871#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     872          ptr = strtok_r (NULL, " ", &saveptr);
     873#else
    857874          ptr = strtok (NULL, " ");
     875#endif
    858876          if (ptr && 0 == sl_strncmp (ptr, _("fingerprint"), 11))
    859877            {
     878#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     879              ptr = strtok_r (NULL, " ", &saveptr); /* to '=' */
     880#else
    860881              ptr = strtok (NULL, " "); /* to '=' */
     882#endif
    861883              sign_fp[0] = '\0';
    862884              while (ptr)
    863885                {
     886#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     887                  ptr = strtok_r (NULL, " ", &saveptr); /* part of fingerprint */
     888#else
    864889                  ptr = strtok (NULL, " "); /* part of fingerprint */
     890#endif
    865891                  sl_strlcat (sign_fp, ptr, SH_MINIBUF+1);
    866892                }
     
    923949#if defined(SH_WITH_SERVER)
    924950  struct passwd * tempres;
    925 #endif
     951#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     952  struct passwd    pwd;
     953  char             buffer[SH_PWBUF_SIZE];
     954#endif
     955#endif
     956
    926957#ifdef USE_FINGERPRINT
    927958#include "sh_gpg_fp.h"
     
    952983      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD1 = %d>\n"), fd1));
    953984#if defined(SH_WITH_SERVER)
     985#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     986      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
     987#else
    954988      tempres = sh_getpwnam(DEFAULT_IDENT);
     989#endif
    955990
    956991      if ((tempres != NULL) && (0 == sl_ret_euid()))
     
    9691004      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD2 = %d>\n"), fd2));
    9701005#if defined(SH_WITH_SERVER)
     1006#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     1007      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
     1008#else
    9711009      tempres = sh_getpwnam(DEFAULT_IDENT);
     1010#endif
    9721011
    9731012      if ((tempres != NULL) && (0 == sl_ret_euid()))
     
    10771116
    10781117#if defined(SH_WITH_SERVER)
     1118#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     1119      struct passwd    e_pwd;
     1120      char             e_buffer[SH_PWBUF_SIZE];
     1121      struct passwd *  e_tempres;
     1122      sh_getpwnam_r(DEFAULT_IDENT, &e_pwd, e_buffer, sizeof(e_buffer), &e_tempres);
     1123#else
    10791124      struct passwd * e_tempres = sh_getpwnam(DEFAULT_IDENT);
     1125#endif
    10801126
    10811127      if ((e_tempres != NULL) && (0 == sl_ret_euid()))   
  • trunk/src/sh_hash.c

    r115 r131  
    36653665  time_t then = (time_t) p->theFile.mtime;
    36663666
     3667#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
     3668  struct tm   * time_ptr;
     3669  struct tm     time_tm;
     3670
     3671  time_ptr = gmtime_r(&then, &time_tm);
     3672  strftime(thetime, 127, _("%b %d  %Y"), time_ptr);
     3673  time_ptr = gmtime_r(&now,  &time_tm);
     3674  strftime(nowtime, 127, _("%b %d  %Y"), time_ptr);
     3675  if (0 == strncmp(&nowtime[7], &thetime[7], 4))
     3676    {
     3677      time_ptr = gmtime_r(&then, &time_tm);
     3678      strftime(thetime, 127, _("%b %d %H:%M"), time_ptr);
     3679    }
     3680#else
    36673681  strftime(thetime, 127, _("%b %d  %Y"), gmtime(&then));
    36683682  strftime(nowtime, 127, _("%b %d  %Y"), gmtime(&now));
    36693683  if (0 == strncmp(&nowtime[7], &thetime[7], 4))
    36703684    strftime(thetime, 127, _("%b %d %H:%M"), gmtime(&then));
     3685#endif
    36713686
    36723687  tmp = sh_util_safe_name(p->fullpath);
  • trunk/src/sh_html.c

    r34 r131  
    3434#endif
    3535#endif
     36#include <unistd.h>
    3637
    3738
     
    9091  time_t    now;
    9192  struct tm   * time_ptr;
     93#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     94  struct tm    time_tm;
     95#endif
    9296
    9397  char    * formatted;
     
    148152      if (!SL_ISERROR(status))
    149153        {
     154#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     155          time_ptr   = localtime_r (&(server_status.start), &time_tm);
     156#else
    150157          time_ptr   = localtime (&(server_status.start));
     158#endif
    151159          if (time_ptr != NULL)
    152160            status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
    153161          now = time(NULL);
     162#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     163          time_ptr   = localtime_r (&now, &time_tm);
     164#else
    154165          time_ptr   = localtime (&now);
     166#endif
    155167          if (time_ptr != NULL)
    156168            status = strftime (ts2, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
     
    174186          if (server_status.last > (time_t) 0)
    175187            {
     188#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     189              time_ptr   = localtime_r (&(server_status.last), &time_tm);
     190#else
    176191              time_ptr   = localtime (&(server_status.last));
     192#endif
    177193              if (time_ptr != NULL)
    178194                status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
  • trunk/src/sh_kern.c

    r114 r131  
    5858
    5959#include "samhain.h"
     60#include "sh_pthread.h"
    6061#include "sh_utils.h"
    6162#include "sh_error.h"
     
    966967  if (df)
    967968    {
     969      SH_MUTEX_LOCK(readdir_lock);
     970
    968971      while (NULL != (entry = readdir(df)))
    969972        {
     
    977980          SH_FREE(pcipath);
    978981        }
     982
     983      SH_MUTEX_UNLOCK(readdir_lock);
     984
    979985      closedir(df);
    980986    }
  • trunk/src/sh_mail.c

    r34 r131  
    654654/* The mailer.
    655655 */
    656 static int sh_mail_end_conn (FILE * connfile);
    657 static FILE * sh_mail_start_conn (int aFlag);
     656static int sh_mail_end_conn (FILE * connfile, int fd);
     657static FILE * sh_mail_start_conn (int aFlag, int * fd);
    658658
    659659static
     
    797797
    798798    static  int ma_block = 0;
     799
     800    int       ma_socket = -1;
    799801
    800802    SH_FIFO * fifo_temp = NULL;
     
    10651067        while (address_list[i] != NULL && i < address_num)
    10661068          {
    1067             connfile = sh_mail_start_conn (i);
     1069            connfile = sh_mail_start_conn (i, &ma_socket);
    10681070           
    10691071            if (NULL != connfile)
     
    10731075                wrlen -= sl_strlen(mailMsg);
    10741076                if (wrlen == 0)
    1075                   status = sh_mail_end_conn (connfile);
     1077                  status = sh_mail_end_conn (connfile, ma_socket);
    10761078                else
    10771079                  status = -1;
     
    11011103    else
    11021104      {
    1103         connfile = sh_mail_start_conn ( -9 );
     1105        connfile = sh_mail_start_conn ( -9 , &ma_socket);
    11041106       
    11051107        if (NULL != connfile)
     
    11081110            wrlen -= sl_strlen(mailMsg);
    11091111            if (wrlen == 0)
    1110               status = sh_mail_end_conn (connfile);
     1112              status = sh_mail_end_conn (connfile, ma_socket);
    11111113            else
    11121114              status = -1;
     
    12871289static time_t time_wait = 300;
    12881290
    1289 static FILE * sh_mail_start_conn (int aFlag)
     1291static FILE * sh_mail_start_conn (int aFlag, int * ma_socket)
    12901292{
    12911293  char       * address;
     
    13021304  FILE       * connFile = NULL;
    13031305  struct tm  * my_tm;
     1306#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     1307  struct tm    time_tm;
     1308#endif
    13041309  time_t       my_time;
    13051310  char         my_tbuf[128];
     
    13121317  SL_ENTER(_("sh_mail_start_conn"));
    13131318
    1314   time_wait = 300;
     1319  *ma_socket = -1;
     1320  time_wait  = 300;
    13151321
    13161322  if (aFlag >= 0)
     
    14161422  /* say HELO to the other socket
    14171423   */
    1418   if (0 == sh_mail_wait (220, connFile))
     1424  if (0 == sh_mail_wait (220, fd))
    14191425    {
    14201426      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    14461452  (void) fflush(connFile);
    14471453
    1448   if (0 == sh_mail_wait(250, connFile))
     1454  if (0 == sh_mail_wait(250, fd))
    14491455    {
    14501456      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    14791485  (void) fflush(connFile);
    14801486
    1481   if (0 == sh_mail_wait(250, connFile))
     1487  if (0 == sh_mail_wait(250, fd))
    14821488    {
    14831489      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15001506      (void) fflush(connFile);
    15011507
    1502       if (0 == sh_mail_wait(250, connFile))
     1508      if (0 == sh_mail_wait(250, fd))
    15031509        {
    15041510          sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15241530          (void) fflush(connFile);
    15251531         
    1526           if (0 == sh_mail_wait(250, connFile))
     1532          if (0 == sh_mail_wait(250, fd))
    15271533            {
    15281534              sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15491555  (void) fflush(connFile);
    15501556
    1551   if (0 == sh_mail_wait(354, connFile))
     1557  if (0 == sh_mail_wait(354, fd))
    15521558    {
    15531559      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
     
    15611567
    15621568  my_time = time(NULL);
     1569#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     1570  my_tm   = localtime_r(&my_time, &time_tm);
     1571#else
    15631572  my_tm   = localtime(&my_time);
     1573#endif
    15641574  (void)    strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
    15651575
     
    15761586          my_tbuf, 13, 10);
    15771587
     1588  *ma_socket = fd;
    15781589  SL_RETURN( connFile, _("sh_mail_start_conn"));
    15791590}
     
    15851596 */
    15861597
    1587 static int sh_mail_end_conn (FILE * connFile)
     1598static int sh_mail_end_conn (FILE * connFile, int fd)
    15881599{
    15891600  SL_ENTER(_("sh_mail_end_conn"));
     
    15971608  TPT(( 0, FIL__, __LINE__, _("msg=<message end written>\n")));
    15981609
    1599   if (0 != sh_mail_wait(250, connFile))
     1610  if (0 != sh_mail_wait(250, fd))
    16001611    { 
    16011612      (void) fflush(connFile);
     
    16251636 */
    16261637
    1627 static jmp_buf wait_timeout;
    1628 
    1629 static void sh_mail_alarmhandle (int mysignal)
    1630 {
    1631   /*@-noeffect@*/
    1632   (void) mysignal; /* avoid compiler warning */
    1633   /*@+noeffect@*/
    1634 
    1635   longjmp(wait_timeout, 1);
    1636 }
    1637 
    1638 static int sh_mail_wait(int code, FILE * ma_socket)
     1638static int sh_mail_wait(int code, int ma_socket)
    16391639{
    16401640  int rcode, g;
     1641
     1642  char c;
    16411643
    16421644  char errmsg[128];
     
    16511653  time_t waited_time;
    16521654
    1653   struct   sigaction          old_act;
    1654   volatile unsigned int       old_alarm = 0;
    1655 
    1656   struct  sigaction  new_act;
    1657   sigset_t           unblock;
    1658 
    1659   (void) sigemptyset(&unblock);
    1660   (void) sigaddset  (&unblock, SIGALRM);
    1661 
    1662   new_act.sa_handler = sh_mail_alarmhandle;
    1663   (void) sigemptyset( &new_act.sa_mask );       /* set an empty mask       */
    1664   new_act.sa_flags = 0;                         /* init sa_flags           */
    1665 
    16661655  SL_ENTER(_("mail_wait"));
    16671656 
    1668   /* alarm was triggered
    1669    */
    1670   if (setjmp(wait_timeout) != 0)
    1671     {
    1672       (void) alarm(0);
    1673       (void) sigaction (SIGALRM, &old_act, NULL);
    1674       (void) alarm(old_alarm);
    1675       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    1676       TPT((0, FIL__, __LINE__, _("msg=<mail_wait: timeout>\n")));
    1677       SL_RETURN( 0, _("mail_wait"));
    1678     }
    1679 
    16801657  waited_time = time(NULL);
    16811658
    16821659  /* timeout after 5 minutes
    16831660   */
    1684   old_alarm = alarm(0);
    1685   (void) sigaction (SIGALRM, &new_act, &old_act);
    1686   (void) alarm((unsigned int) time_wait);
    16871661
    16881662  rcode = 0;
    16891663  state = WAIT_CODE_START;
    16901664
    1691   while (0 == feof(ma_socket) && 0 == ferror(ma_socket)) {
     1665  while (sl_read_timeout_fd (ma_socket, &c, 1, time_wait, SL_FALSE) > 0) {
     1666
     1667    g = (int) c;
    16921668
    16931669    if ( (g=fgetc(ma_socket)) == EOF)
    16941670      {
    1695         (void) alarm(0);
    1696         (void) sigaction (SIGALRM, &old_act, NULL);
    1697         (void) alarm(old_alarm);
    1698         (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    16991671        TPT((0, FIL__, __LINE__, _("msg=<mail_wait: EOF>\n")));
    17001672        SL_RETURN( 0, _("mail_wait"));
     
    17331705        break;
    17341706      /*@-charintliteral@*/
    1735       (void) alarm(0);
    1736       (void) sigaction (SIGALRM, &old_act, NULL);
    1737       (void) alarm(old_alarm);
    1738       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17391707
    17401708      TPT((0, FIL__, __LINE__,
     
    17691737     
    17701738    default:
    1771       (void) alarm(0);
    1772       (void) sigaction (SIGALRM, &old_act, NULL);
    1773       (void) alarm(old_alarm);
    1774       (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17751739
    17761740      TPT((0, FIL__, __LINE__, _("msg=<mail_wait: bad>\n")));
     
    17791743    }
    17801744  }
    1781 
    1782   (void) alarm(0);                            /* Disable alarm       */
    1783   (void) sigaction (SIGALRM, &old_act, NULL);
    1784   (void) alarm(old_alarm);
    1785   (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
    17861745
    17871746  TPT((0, FIL__, __LINE__, _("msg=<mail_wait: failed>\n")));
  • trunk/src/sh_portcheck.c

    r128 r131  
    11451145  char * list;
    11461146  char * p;
     1147#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     1148  char * saveptr;
     1149#endif
    11471150
    11481151  if (!str)
     
    11871190
    11881191  list = sh_util_strdup(&str[ll]);
     1192#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     1193  p    = strtok_r (list, " ,\t", &saveptr);
     1194#else
    11891195  p    = strtok (list, " ,\t");
     1196#endif
    11901197  if (!p)
    11911198    {
     
    12021209          return -1;
    12031210        }
     1211#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     1212      p    = strtok_r (NULL, " ,\t", &saveptr);
     1213#else
    12041214      p    = strtok (NULL, " ,\t");
     1215#endif
    12051216    }
    12061217  SH_FREE(interface);
  • trunk/src/sh_prelink.c

    r107 r131  
    8484    return S_FALSE;
    8585
    86   status = sl_read_timeout (fd, magic, 4, alert_timeout);
     86  status = sl_read_timeout (fd, magic, 4, alert_timeout, SL_FALSE);
    8787  (void) sl_rewind(fd);
    8888  if (status == 4)
  • trunk/src/sh_prelude.c

    r108 r131  
    8383#include "sh_error_min.h"
    8484#include "sh_prelude.h"
     85#define SH_NEED_PWD_GRP 1
     86#include "sh_static.h"
    8587
    8688/*
     
    138140        char * p;
    139141        char * dup = strdup (str);
     142#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     143        char * saveptr;
     144#endif
    140145
    141146        if (!dup)
    142147                return -1;
    143148
     149#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     150        p = strtok_r (dup, ", \t", &saveptr);
     151#else
    144152        p = strtok (dup, ", \t");
     153#endif
    145154        if (p) {
    146155                do {
     
    165174                                return -1;
    166175                        }
     176#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     177                        p = strtok_r (NULL, ", \t", &saveptr);
     178#else
    167179                        p = strtok (NULL, ", \t");
     180#endif
    168181                } while (p);
    169182        }
     
    679692        idmef_node_t *node;
    680693        struct passwd *pw;
     694#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     695        struct passwd pwd;
     696        char buffer[SH_PWBUF_SIZE];
     697#endif
    681698        prelude_string_t *str;
    682699        idmef_user_id_t *user_id;
     
    763780                idmef_user_id_set_type(user_id, IDMEF_USER_ID_TYPE_TARGET_USER);
    764781               
    765                 pw = getpwnam(ptr);
     782#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     783                sh_getpwnam_r(ptr, &pwd, buffer, sizeof(buffer), &pw);
     784#else
     785                pw = sh_getpwnam(ptr);
     786#endif
    766787                if ( pw )
    767788                        idmef_user_id_set_number(user_id, pw->pw_uid);
  • trunk/src/sh_schedule.c

    r34 r131  
    130130  struct tm * tval;
    131131  int count, i, nval;
     132#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     133  struct tm     time_tm;
     134#endif
    132135
    133136  if (!isched)
     
    135138
    136139  now  = time(NULL);
     140#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     141  tval = localtime_r(&now, &time_tm);
     142#else
    137143  tval = localtime(&now);
    138 
     144#endif
    139145  count = 0;
    140146  for (i = 0; i < 5; ++i)
     
    320326  int    i = 0;
    321327  size_t len;
     328#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     329  char * saveptr;
     330#endif
    322331
    323332  if (!ssched || !isched)
     
    332341  sl_strlcpy(copy, ssched, len);
    333342
     343#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     344  p = strtok_r(copy, " \t", &saveptr); /* parse crontab-style schedule */
     345#else
    334346  p = strtok(copy, " \t"); /* parse crontab-style schedule */
     347#endif
     348
    335349  if (!p)
    336350    goto err;
     
    340354  for (i = 1; i < 5; ++i)
    341355    {
     356#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     357      p = strtok_r(NULL, " \t", &saveptr); /* parse crontab-style schedule */
     358#else
    342359      p = strtok(NULL, " \t"); /* parse crontab-style schedule */
     360#endif
    343361      if (!p)
    344362        goto err;
     
    420438    {
    421439      if (test_sched(&isched))
    422         printf("EXECUTE  at: %s", ctime(&(isched.last_exec)));
     440        printf("EXECUTE  at: %s", ctime(&(isched.last_exec))); /* TESTONLY */
    423441      sleep (1); /* TESTONLY */
    424442    }
  • trunk/src/sh_suidchk.c

    r119 r131  
    7171
    7272#include "samhain.h"
     73#include "sh_pthread.h"
    7374#include "sh_utils.h"
    7475#include "sh_error.h"
     
    842843  char            fileHash[2*(KEY_LEN + 1)];
    843844
     845  struct sh_dirent * dirlist = NULL;
     846  struct sh_dirent * dirlist_orig = NULL;
    844847
    845848  SL_ENTER(_("sh_suidchk_check_internal"));
     
    870873  /* Loop over directory entries
    871874   */
     875  SH_MUTEX_LOCK(readdir_lock);
     876
    872877  do {
    873878
     
    886891        continue;
    887892
    888       tmpcat = SH_ALLOC(PATH_MAX);
    889       (void) sl_strlcpy(tmpcat, iname, PATH_MAX);
    890 
    891       if ((sl_strlen(tmpcat) != sl_strlen(iname)) || (tmpcat[0] == '\0'))
    892         {
    893           sl_status = SL_ETRUNC;
    894         }
    895       else
    896         {
    897           if (tmpcat[1] != '\0')
    898             sl_status = sl_strlcat(tmpcat, "/",                 PATH_MAX);
    899         }
    900 
    901       if (! SL_ISERROR(sl_status))
    902         sl_status = sl_strlcat(tmpcat, thisEntry->d_name,   PATH_MAX);
    903 
    904       if (SL_ISERROR(sl_status))
    905         {
    906           tmp = sh_util_safe_name(tmpcat);
    907           sh_error_handle ((-1), FIL__, __LINE__, (int) sl_status,
    908                            MSG_E_SUBGPATH,
    909                            _("path too long"),
    910                            _("sh_suidchk_check_internal"), tmp );
    911           SH_FREE(tmp);
    912           continue;
    913         }
    914 
    915       ++FileLimNum;
    916       ++FileLimTotal;
    917 
    918       /* Rate limit (Fps == Files per second)
    919        */
    920       if ((ShSuidchkFps > 0 && FileLimNum > ShSuidchkFps && FileLimTotal > 0)&&
    921           (ShSuidchkYield == S_FALSE))
    922         {
    923           FileLimNum  = 0;
    924           FileLimNow  = time(NULL);
    925  
    926           if ( (FileLimNow  - FileLimStart) > 0 &&
    927                FileLimTotal/(FileLimNow  - FileLimStart) > ShSuidchkFps )
    928             (void) retry_msleep((int)((FileLimTotal/(FileLimNow-FileLimStart))/
    929                    ShSuidchkFps) , 0);
    930         }
     893      dirlist = addto_sh_dirlist (thisEntry, dirlist);
     894    }
     895
     896  } while (thisEntry != NULL);
     897
     898  SH_MUTEX_UNLOCK(readdir_lock);
     899
     900  closedir(thisDir);
     901
     902  dirlist_orig = dirlist;
     903
     904  do {
     905
     906    /* If the directory is empty, dirlist = NULL
     907     */
     908    if (!dirlist)
     909      break;
     910
     911    tmpcat = SH_ALLOC(PATH_MAX);
     912    (void) sl_strlcpy(tmpcat, iname, PATH_MAX);
     913   
     914    if ((sl_strlen(tmpcat) != sl_strlen(iname)) || (tmpcat[0] == '\0'))
     915      {
     916        sl_status = SL_ETRUNC;
     917      }
     918    else
     919      {
     920        if (tmpcat[1] != '\0')
     921          sl_status = sl_strlcat(tmpcat, "/",                 PATH_MAX);
     922      }
     923
     924    if (! SL_ISERROR(sl_status))
     925      sl_status = sl_strlcat(tmpcat, dirlist->sh_d_name,   PATH_MAX);
     926
     927    if (SL_ISERROR(sl_status))
     928      {
     929        tmp = sh_util_safe_name(tmpcat);
     930        sh_error_handle ((-1), FIL__, __LINE__, (int) sl_status,
     931                         MSG_E_SUBGPATH,
     932                         _("path too long"),
     933                         _("sh_suidchk_check_internal"), tmp );
     934        SH_FREE(tmp);
     935        dirlist = dirlist->next;
     936        continue;
     937      }
     938
     939    ++FileLimNum;
     940    ++FileLimTotal;
     941
     942    /* Rate limit (Fps == Files per second)
     943     */
     944    if ((ShSuidchkFps > 0 && FileLimNum > ShSuidchkFps && FileLimTotal > 0)&&
     945        (ShSuidchkYield == S_FALSE))
     946      {
     947        FileLimNum  = 0;
     948        FileLimNow  = time(NULL);
     949       
     950        if ( (FileLimNow  - FileLimStart) > 0 &&
     951             FileLimTotal/(FileLimNow  - FileLimStart) > ShSuidchkFps )
     952          (void) retry_msleep((int)((FileLimTotal/(FileLimNow-FileLimStart))/
     953                                    ShSuidchkFps) , 0);
     954      }
    931955             
    932       status = (int) retry_lstat(FIL__, __LINE__, tmpcat, &buf);
    933 
    934       if (status != 0)
    935         {
    936           status = errno;
    937           tmp = sh_util_safe_name(tmpcat);
    938           sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
    939                            sh_error_message(status),
    940                            tmpcat );
    941           SH_FREE(tmp);
    942         }
    943       else
    944         {
    945           if (/*@-usedef@*/S_ISDIR(buf.st_mode)/*@+usedef@*/ &&
    946               (ShSuidchkExclude == NULL ||
    947                0 != strcmp(tmpcat, ShSuidchkExclude)))
    948             {
    949               /* fs is a STATIC string or NULL
    950                */
    951               fs = filesystem_type (tmpcat, tmpcat, &buf);
    952               if (fs != NULL
     956    status = (int) retry_lstat(FIL__, __LINE__, tmpcat, &buf);
     957
     958    if (status != 0)
     959      {
     960        status = errno;
     961        tmp = sh_util_safe_name(tmpcat);
     962        sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
     963                         sh_error_message(status),
     964                         tmpcat );
     965        SH_FREE(tmp);
     966      }
     967    else
     968      {
     969        if (/*@-usedef@*/S_ISDIR(buf.st_mode)/*@+usedef@*/ &&
     970            (ShSuidchkExclude == NULL ||
     971             0 != strcmp(tmpcat, ShSuidchkExclude)))
     972          {
     973            /* fs is a STATIC string or NULL
     974             */
     975            fs = filesystem_type (tmpcat, tmpcat, &buf);
     976            if (fs != NULL
    953977#ifndef SH_SUIDTESTDIR
    954                   &&
    955                   0 != strncmp (_("afs"),     fs, 3) &&
    956                   0 != strncmp (_("devfs"),   fs, 5) &&
    957                   0 != strncmp (_("iso9660"), fs, 7) &&
    958                   0 != strncmp (_("lustre"),  fs, 6) &&
    959                   0 != strncmp (_("mmfs"),    fs, 4) &&
    960                   0 != strncmp (_("msdos"),   fs, 5) &&
    961                   0 != strncmp (_("nfs"),     fs, 3) &&
    962                   0 != strncmp (_("proc"),    fs, 4) &&
    963                   0 != strncmp (_("vfat"),    fs, 4)
     978                &&
     979                0 != strncmp (_("afs"),     fs, 3) &&
     980                0 != strncmp (_("devfs"),   fs, 5) &&
     981                0 != strncmp (_("iso9660"), fs, 7) &&
     982                0 != strncmp (_("lustre"),  fs, 6) &&
     983                0 != strncmp (_("mmfs"),    fs, 4) &&
     984                0 != strncmp (_("msdos"),   fs, 5) &&
     985                0 != strncmp (_("nfs"),     fs, 3) &&
     986                0 != strncmp (_("proc"),    fs, 4) &&
     987                0 != strncmp (_("vfat"),    fs, 4)
    964988#endif
     989                )
     990              {
     991                if ((ShSuidchkNosuid == S_TRUE) ||
     992                    (0 != strncmp (_("nosuid"),  fs, 6)))
     993                  /* fprintf(stderr, "%s: %s\n", fs, tmpcat); */
     994                  (void) sh_suidchk_check_internal(tmpcat);
     995              }
     996          }
     997        else if (S_ISREG(buf.st_mode) &&
     998                 (0 !=(S_ISUID & buf.st_mode) ||
     999#if defined(HOST_IS_LINUX)
     1000                  (0 !=(S_ISGID & buf.st_mode) &&
     1001                   0 !=(S_IXGRP & buf.st_mode))
     1002#else 
     1003                  0 !=(S_ISGID & buf.st_mode)
     1004#endif
    9651005                  )
    966                 {
    967                   if ((ShSuidchkNosuid == S_TRUE) ||
    968                       (0 != strncmp (_("nosuid"),  fs, 6)))
    969                     /* fprintf(stderr, "%s: %s\n", fs, tmpcat); */
    970                     (void) sh_suidchk_check_internal(tmpcat);
    971                 }
    972             }
    973           else if (S_ISREG(buf.st_mode) &&
    974                    (0 !=(S_ISUID & buf.st_mode) ||
    975 #if defined(HOST_IS_LINUX)
    976                     (0 !=(S_ISGID & buf.st_mode) &&
    977                      0 !=(S_IXGRP & buf.st_mode))
    978 #else 
    979                     0 !=(S_ISGID & buf.st_mode)
    980 #endif
    981                     )
    982                    )
    983             {
    984              
    985               (void) sl_strlcpy (theFile.fullpath, tmpcat, PATH_MAX);
    986               theFile.check_mask  = sh_files_maskof(SH_LEVEL_READONLY);
    987               CLEAR_SH_FFLAG_REPORTED(theFile.file_reported);
    988               theFile.attr_string = NULL;
    989 
    990               status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_RO],
    991                                         thisEntry->d_name,
    992                                         &theFile, fileHash, 0);
    993 
    994               tmp = sh_util_safe_name(tmpcat);
    995 
    996               if (status != 0)
    997                 {
    998                   sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__,
    999                                    0, MSG_E_SUBGPATH,
    1000                                    _("Could not check suid/sgid file"),
    1001                                    _("sh_suidchk_check_internal"),
    1002                                    tmp);
    1003                 }
    1004               else
    1005                 {
    1006 
    1007                   if ( sh.flag.update   == S_TRUE &&
    1008                       (sh.flag.checkSum == SH_CHECK_INIT  ||
    1009                        sh.flag.checkSum == SH_CHECK_CHECK))
    1010                     {
    1011                       /* Updating database. Report new files that
    1012                        * are not in database already. Then compare
    1013                        * to database and report changes.
    1014                        */
    1015                       if (-1 == sh_hash_have_it (tmpcat))
    1016                         {
    1017                           sh_error_handle ((-1), FIL__, __LINE__,
    1018                                            0, MSG_SUID_FOUND, tmp );
    1019                         }
    1020                       else
    1021                         {
    1022                           sh_error_handle (SH_ERR_ALL, FIL__, __LINE__,
    1023                                            0, MSG_SUID_FOUND, tmp );
    1024                         }
    1025 
    1026                       if (0 == sh_hash_compdata (SH_LEVEL_READONLY,
     1006                 )
     1007          {
     1008           
     1009            (void) sl_strlcpy (theFile.fullpath, tmpcat, PATH_MAX);
     1010            theFile.check_mask  = sh_files_maskof(SH_LEVEL_READONLY);
     1011            CLEAR_SH_FFLAG_REPORTED(theFile.file_reported);
     1012            theFile.attr_string = NULL;
     1013           
     1014            status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_RO],
     1015                                      thisEntry->d_name,
     1016                                      &theFile, fileHash, 0);
     1017           
     1018            tmp = sh_util_safe_name(tmpcat);
     1019           
     1020            if (status != 0)
     1021              {
     1022                sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__,
     1023                                 0, MSG_E_SUBGPATH,
     1024                                 _("Could not check suid/sgid file"),
     1025                                 _("sh_suidchk_check_internal"),
     1026                                 tmp);
     1027              }
     1028            else
     1029              {
     1030               
     1031                if ( sh.flag.update   == S_TRUE &&
     1032                     (sh.flag.checkSum == SH_CHECK_INIT  ||
     1033                      sh.flag.checkSum == SH_CHECK_CHECK))
     1034                  {
     1035                    /* Updating database. Report new files that
     1036                     * are not in database already. Then compare
     1037                     * to database and report changes.
     1038                     */
     1039                    if (-1 == sh_hash_have_it (tmpcat))
     1040                      {
     1041                        sh_error_handle ((-1), FIL__, __LINE__,
     1042                                         0, MSG_SUID_FOUND, tmp );
     1043                      }
     1044                    else
     1045                      {
     1046                        sh_error_handle (SH_ERR_ALL, FIL__, __LINE__,
     1047                                         0, MSG_SUID_FOUND, tmp );
     1048                      }
     1049                   
     1050                    if (0 == sh_hash_compdata (SH_LEVEL_READONLY,
     1051                                               &theFile, fileHash,
     1052                                               _("[SuidCheck]"),
     1053                                               ShSuidchkSeverity))
     1054                      {
     1055                        sh_hash_pushdata_memory (&theFile, fileHash);
     1056                      }
     1057                   
     1058                    sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
     1059                   
     1060                  }
     1061               
     1062                else if (sh.flag.checkSum == SH_CHECK_INIT  &&
     1063                         sh.flag.update == S_FALSE )
     1064                  {
     1065                    /* Running init. Report on files detected.
     1066                     */
     1067                    sh_hash_pushdata (&theFile, fileHash);
     1068                    sh_error_handle ((-1), FIL__, __LINE__,
     1069                                     0, MSG_SUID_FOUND, tmp );
     1070                  }
     1071               
     1072                else if (sh.flag.checkSum == SH_CHECK_CHECK )
     1073                  {
     1074                    /* Running file check. Report on new files
     1075                     * detected, and quarantine them.
     1076                     */
     1077                    sh_error_handle (SH_ERR_ALL, FIL__, __LINE__,
     1078                                     0, MSG_SUID_FOUND, tmp );
     1079                   
     1080                    fflags = sh_hash_getflags(tmpcat);
     1081                   
     1082                    if ( (-1 == fflags) || (!SH_FFLAG_SUIDCHK_SET(fflags)))
     1083                      {
     1084                        if (-1 == fflags)
     1085                          report_file(tmpcat, &theFile, timestrc, timestra, timestrm);
     1086                       
     1087                        /* Quarantine file according to configured method
     1088                         */
     1089                        if (ShSuidchkQEnable == S_TRUE)
     1090                          {
     1091                            switch (ShSuidchkQMethod)
     1092                              {
     1093                              case SH_Q_DELETE:
     1094                                sh_q_delete(theFile.fullpath);
     1095                                break;
     1096                              case SH_Q_CHANGEPERM:
     1097                                sh_q_changeperm(theFile.fullpath);
     1098                                break;
     1099                              case SH_Q_MOVE:
     1100                                sh_q_move(theFile.fullpath, &theFile, timestrc, timestra, timestrm);
     1101                                break;
     1102                              default:
     1103                                sh_error_handle (ShSuidchkSeverity, FIL__,
     1104                                                 __LINE__, 0, MSG_SUID_QREPORT,
     1105                                                 _("Bad quarantine method"), tmp);
     1106                                break;
     1107                              }
     1108                          }
     1109                        else
     1110                          {
     1111                            /* 1.8.1 push file to in-memory database
     1112                             */
     1113                            (void) sh_hash_compdata (SH_LEVEL_READONLY,
     1114                                                     &theFile, fileHash,
     1115                                                     _("[SuidCheck]"),
     1116                                                     ShSuidchkSeverity);
     1117                           
     1118                            sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
     1119                           
     1120                          }
     1121                      }
     1122                    else
     1123                      {
     1124                        /* File exists. Check for modifications.
     1125                         */
     1126                        (void) sh_hash_compdata (SH_LEVEL_READONLY,
    10271127                                                 &theFile, fileHash,
    1028                                                  _("[SuidCheck]"),
    1029                                                  ShSuidchkSeverity))
    1030                         {
    1031                           sh_hash_pushdata_memory (&theFile, fileHash);
    1032                         }
    1033                      
    1034                       sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
    1035                      
    1036                     }
    1037 
    1038                   else if (sh.flag.checkSum == SH_CHECK_INIT  &&
    1039                            sh.flag.update == S_FALSE )
    1040                     {
    1041                       /* Running init. Report on files detected.
    1042                        */
    1043                       sh_hash_pushdata (&theFile, fileHash);
    1044                       sh_error_handle ((-1), FIL__, __LINE__,
    1045                                        0, MSG_SUID_FOUND, tmp );
    1046                     }
    1047 
    1048                   else if (sh.flag.checkSum == SH_CHECK_CHECK )
    1049                     {
    1050                       /* Running file check. Report on new files
    1051                        * detected, and quarantine them.
    1052                        */
    1053                       sh_error_handle (SH_ERR_ALL, FIL__, __LINE__,
    1054                                        0, MSG_SUID_FOUND, tmp );
    1055 
    1056                       fflags = sh_hash_getflags(tmpcat);
    1057 
    1058                       if ( (-1 == fflags) || (!SH_FFLAG_SUIDCHK_SET(fflags)))
    1059                         {
    1060                           if (-1 == fflags)
    1061                             report_file(tmpcat, &theFile, timestrc, timestra, timestrm);
    1062 
    1063                           /* Quarantine file according to configured method
    1064                           */
    1065                           if (ShSuidchkQEnable == S_TRUE)
    1066                             {
    1067                               switch (ShSuidchkQMethod)
    1068                                 {
    1069                                   case SH_Q_DELETE:
    1070                                     sh_q_delete(theFile.fullpath);
    1071                                     break;
    1072                                   case SH_Q_CHANGEPERM:
    1073                                     sh_q_changeperm(theFile.fullpath);
    1074                                     break;
    1075                                   case SH_Q_MOVE:
    1076                                     sh_q_move(theFile.fullpath, &theFile, timestrc, timestra, timestrm);
    1077                                     break;
    1078                                   default:
    1079                                     sh_error_handle (ShSuidchkSeverity, FIL__,
    1080                                                      __LINE__, 0, MSG_SUID_QREPORT,
    1081                                                      _("Bad quarantine method"), tmp);
    1082                                     break;
    1083                                 }
    1084                             }
    1085                           else
    1086                             {
    1087                               /* 1.8.1 push file to in-memory database
    1088                                */
    1089                               (void) sh_hash_compdata (SH_LEVEL_READONLY,
    1090                                                        &theFile, fileHash,
    1091                                                        _("[SuidCheck]"),
    1092                                                        ShSuidchkSeverity);
    1093 
    1094                               sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
    1095 
    1096                             }
    1097                         }
    1098                       else
    1099                         {
    1100                           /* File exists. Check for modifications.
    1101                            */
    1102                           (void) sh_hash_compdata (SH_LEVEL_READONLY,
    1103                                                    &theFile, fileHash,
    1104                                                    _("[SuidCheck]"),
    1105                                                    ShSuidchkSeverity);
    1106                          
    1107                           sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
    1108 
    1109                         }
    1110                     }
    1111                 }
    1112               SH_FREE(tmp);
    1113               if (theFile.attr_string)
    1114                 SH_FREE(theFile.attr_string);
    1115             }
    1116         }
    1117       SH_FREE(tmpcat);
    1118     }
    1119 
     1128                                                 _("[SuidCheck]"),
     1129                                                 ShSuidchkSeverity);
     1130                       
     1131                        sh_hash_addflag(tmpcat, SH_FFLAG_SUIDCHK);
     1132                       
     1133                      }
     1134                  }
     1135              }
     1136            SH_FREE(tmp);
     1137            if (theFile.attr_string)
     1138              SH_FREE(theFile.attr_string);
     1139          }
     1140      }
     1141    SH_FREE(tmpcat);
     1142
     1143 
    11201144#ifdef HAVE_SCHED_YIELD
    11211145    if (ShSuidchkYield == S_TRUE)
     
    11251149            status = errno;
    11261150            sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
    1127                              _("Failed to release time slice"),
     1151                             _("Failed to release time slice"),
    11281152                             _("sh_suidchk_check_internal") );
    11291153          }
    11301154      }
    11311155#endif
    1132 
    1133   }  while (thisEntry != NULL);
    1134 
    1135   (void) closedir (thisDir);
     1156 
     1157    dirlist = dirlist->next;
     1158
     1159  }  while (dirlist != NULL);
     1160
     1161
     1162  kill_sh_dirlist (dirlist_orig);
     1163
    11361164  SL_RETURN( (0), _("sh_suidchk_check_internal"));
    11371165}
  • trunk/src/sh_tiger0.c

    r107 r131  
    206206      {
    207207        if (timeout > 0)
    208           count = sl_read_timeout (fd, buffer, PRIV_MAX, timeout);
     208          count = sl_read_timeout (fd, buffer, PRIV_MAX, timeout, SL_TRUE);
    209209        else
    210210          count = sl_read         (fd, buffer, PRIV_MAX);
  • trunk/src/sh_unix.c

    r114 r131  
    598598  strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
    599599#endif
     600  sh_sig_msg[63] = '\0';
    600601
    601602  sl_stack_print();
     
    648649  if (mysignal == SIGQUIT)
    649650    {
    650       sig_terminate         = 1;
     651      sig_terminate       = 1;
    651652      ++sig_urgent;
    652653    }
     
    770771#endif
    771772#ifdef SIGTERM
    772   retry_sigaction(FIL__, __LINE__, SIGTERM,     &act,  &oldact);
     773  retry_sigaction(FIL__, __LINE__, SIGTERM,    &act,  &oldact);
    773774#endif
    774775
     
    951952  uid_t                     pwid  = (uid_t)-1;
    952953
     954#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     955  struct passwd    pwd;
     956  char             buffer[SH_PWBUF_SIZE];
     957#endif
     958 
    953959  SL_ENTER(_("tf_add_trusted_user_int"));
    954960
    955961  /* First check for a user name.
    956962   */
    957   if ((w = sh_getpwnam(c)) != NULL && ((pwid = w->pw_uid) > 0))
     963#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     964  status = sh_getpwnam_r(c, &pwd, buffer, sizeof(buffer), &w);
     965#else
     966  w = sh_getpwnam(c);
     967#endif
     968
     969  if ((w != NULL) && ((pwid = w->pw_uid) > 0))
    958970    goto succe;
    959971       
     
    978990  char * q;
    979991  char * p = sh_util_strdup (c);
    980  
     992#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     993  char * saveptr;
     994#endif
     995
    981996  SL_ENTER(_("tf_add_trusted_user"));
    982997
     998#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     999  q = strtok_r(p, ", \t", &saveptr);
     1000#else
    9831001  q = strtok(p, ", \t");
     1002#endif
    9841003  if (!q)
    9851004    {
     
    9951014          SL_RETURN((i), _("tf_add_trusted_user"));
    9961015        }
     1016#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
     1017      q = strtok_r(NULL, ", \t", &saveptr);
     1018#else
    9971019      q = strtok(NULL, ", \t");
     1020#endif
    9981021    }
    9991022  SH_FREE(p);
     
    10311054  if (0 == sl_ret_euid())   /* privileges not dropped yet */
    10321055    {
     1056#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     1057      struct passwd    pwd;
     1058      char             buffer[SH_PWBUF_SIZE];
     1059      struct passwd *  tempres;
     1060      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
     1061#else
    10331062      struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
     1063#endif
    10341064
    10351065      if (!tempres)
     
    19641994  SL_ENTER(_("t_zone"));
    19651995
    1966 
     1996#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
     1997  cc = gmtime_r (xx, &aa);
     1998#else
    19671999  cc = gmtime (xx);
    19682000  memcpy (&aa, cc, sizeof(struct tm));
     2001#endif
     2002
     2003#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     2004  cc = localtime_r (xx, &bb);
     2005#else
    19692006  cc = localtime (xx);
    19702007  memcpy (&bb, cc, sizeof(struct tm));
     2008#endif
    19712009
    19722010  /* Check for datum wrap-around.
     
    20642102  time_t        time_now;
    20652103  struct tm   * time_ptr;
     2104#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     2105  struct tm     time_tm;
     2106#endif
    20662107  static char   AsciiTime[81];                       /* local time   */
    20672108  static char   RetTime[81];                         /* local time   */
     
    21642205    SL_RETURN( _(deftime), _("sh_unix_time"));
    21652206  else
    2166     time_ptr   = localtime (&time_now);
    2167 
     2207    {
     2208#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     2209      time_ptr   = localtime_r (&time_now, &time_tm);
     2210#else
     2211      time_ptr   = localtime (&time_now);
     2212#endif
     2213    }
    21682214  if (time_ptr != NULL)
    21692215    {
     
    22082254
    22092255  struct tm   * time_ptr;
     2256#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS)
     2257  struct tm     time_tm;
     2258#endif
    22102259  static char   AsciiTime[81];                       /* GMT time   */
    22112260#ifdef SH_USE_XML
     
    22182267
    22192268  if (sh_unix_use_localtime == S_FALSE)
    2220     time_ptr   = gmtime (&thetime);
     2269    {
     2270#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
     2271      time_ptr   = gmtime_r (&thetime, &time_tm);
     2272#else
     2273      time_ptr   = gmtime (&thetime);
     2274#endif
     2275    }
    22212276  else
    2222     time_ptr   = localtime (&thetime);
    2223 
     2277    {
     2278#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
     2279      time_ptr   = localtime_r (&thetime, &time_tm);
     2280#else
     2281      time_ptr   = localtime (&thetime);
     2282#endif
     2283    }
    22242284  if (time_ptr != NULL)
    22252285    {
     
    22492309  struct passwd * tempres;
    22502310  int    status = 0;
     2311#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     2312  struct passwd pwd;
     2313  char   buffer[SH_PWBUF_SIZE];
     2314#endif
    22512315
    22522316  SL_ENTER(_("sh_unix_getUIDdir"));
    22532317
     2318#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     2319  sh_getpwuid_r(uid, &pwd, buffer, sizeof(buffer), &tempres);
     2320#else
    22542321  errno = 0;
    22552322  tempres = sh_getpwuid(uid);
    22562323  status = errno;
     2324#endif
    22572325
    22582326  if (tempres == NULL) {
     
    22762344{
    22772345  struct passwd * tempres;
     2346#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     2347  struct passwd pwd;
     2348  char   buffer[SH_PWBUF_SIZE];
     2349#endif
    22782350  int             status = 0;
    22792351  static uid_t    old_uid;
     
    22862358  }
    22872359
     2360#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     2361  sh_getpwuid_r(uid, &pwd, buffer, sizeof(buffer), &tempres);
     2362#else
    22882363  errno = 0;
    22892364  tempres = sh_getpwuid(uid);
    22902365  status = errno;
     2366#endif
    22912367 
    22922368  if (tempres == NULL) {
     
    22992375
    23002376  if (tempres->pw_name != NULL) {
     2377    /* NEED_LOCK */
    23012378    sl_strlcpy(name, tempres->pw_name, sizeof(name));
    23022379    old_uid = uid;
     2380    /* END_LOCK  */
    23032381    SL_RETURN( name, _("sh_unix_getUIDname"));
    23042382  } else {
     
    23162394  static gid_t    old_gid;
    23172395  static char     name[32] = { '\0' };
     2396#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     2397  struct group    grp;
     2398  char            buffer[SH_GRBUF_SIZE];
     2399#endif
     2400 
    23182401
    23192402  SL_ENTER(_("sh_unix_getGIDname"));
     
    23232406  }
    23242407 
     2408#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     2409  status = sh_getgrgid_r(gid, &grp, buffer, sizeof(buffer), &tempres);
     2410#else
    23252411  errno = 0;
    23262412  tempres = sh_getgrgid(gid);
    23272413  status = errno;
     2414#endif
    23282415
    23292416  if (tempres == NULL) {
     
    23362423
    23372424  if (tempres->gr_name != NULL) {
     2425    /* NEED_LOCK */
    23382426    sl_strlcpy(name, tempres->gr_name, sizeof(name));
    23392427    old_gid = gid;
     2428    /* END_LOCK  */
    23402429    SL_RETURN( name, _("sh_unix_getGIDname"));
    23412430  } else {
  • trunk/src/slib.c

    r76 r131  
    14251425int sl_policy_get_real(char * user)
    14261426{
    1427   struct passwd * tempres;
    1428 
    14291427  SL_ENTER(_("sl_policy_get_real"));
    14301428  SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
     
    14331431  if (euid == 0 || ruid == 0)
    14341432    {
    1435       tempres = sh_getpwnam(user);
     1433#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     1434      struct passwd    pwd;
     1435      char             buffer[SH_PWBUF_SIZE];
     1436      struct passwd *  tempres;
     1437      sh_getpwnam_r(user, &pwd, buffer, sizeof(buffer), &tempres);
     1438#else
     1439      struct passwd * tempres = sh_getpwnam(user);
     1440#endif
    14361441
    14371442      SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
     
    14711476  if (euid != ruid || egid != rgid)
    14721477    {
    1473       tempres = sh_getpwnam(user);
     1478#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     1479      struct passwd    pwd;
     1480      char             buffer[SH_PWBUF_SIZE];
     1481      struct passwd *  tempres;
     1482      sh_getpwnam_r(user, &pwd, buffer, sizeof(buffer), &tempres);
     1483#else
     1484      struct passwd * tempres = sh_getpwnam(user);
     1485#endif
    14741486
    14751487      SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
    14761488
    1477 #if 0
    1478       rgid = tempres->pw_gid;
    1479       ruid = tempres->pw_uid;
    1480       SL_REQUIRE(sl_unset_suid() == SL_ENONE,
    1481                  _("sl_unset_suid() == SL_ENONE"));
    1482 #endif
    14831489      SL_REQUIRE (sl_drop_privileges() == SL_ENONE,
    14841490                  _("sl_drop_privileges() == SL_ENONE"));
     
    21152121  SL_IRETURN(SL_ENONE, _("sl_read_timeout_prep"));
    21162122}
    2117  
    2118 
    2119 int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count,
    2120                      int timeout)
    2121 {
     2123
     2124
     2125int sl_read_timeout_fd (int fd, void * buf_in, size_t count,
     2126                        int timeout, int is_nonblocking)
     2127{
     2128  int sflags;
    21222129  fd_set readfds;
    21232130  struct timeval tv;
     
    21352142  extern volatile int sig_termfast;
    21362143 
    2137   if (buf_in == NULL || SL_ISERROR(fd = get_the_fd(ticket)))
    2138     {
    2139       if (buf_in == NULL)
    2140         {
    2141           TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
    2142           return (SL_ENULL);
    2143         }
    2144       if (SL_ISERROR(fd = get_the_fd(ticket)))
    2145         {
    2146           TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
    2147           return (fd);
    2148         }
     2144  if (is_nonblocking == SL_FALSE)
     2145    {
     2146      /* set to non-blocking mode
     2147       */
     2148      sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
     2149      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
    21492150    }
    21502151
     
    21902191              else
    21912192                {
     2193                  if (is_nonblocking == SL_FALSE)
     2194                      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    21922195                  return (SL_EREAD);
    21932196                }
     
    22032206      else if (retval == 0)
    22042207        {
     2208          if (is_nonblocking == SL_FALSE)
     2209              retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    22052210          return (SL_TIMEOUT);
    22062211        }
    22072212      else
    22082213        {
     2214          if (is_nonblocking == SL_FALSE)
     2215              retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    22092216          return (SL_EREAD);
    22102217        }
     
    22122219      if (sig_termfast == 1)
    22132220        {
     2221          if (is_nonblocking == SL_FALSE)
     2222              retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    22142223          return (SL_EREAD);
    22152224        }
     
    22202229      if (tdiff > timeout)
    22212230        {
     2231          if (is_nonblocking == SL_FALSE)
     2232              retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    22222233          return (SL_TIMEOUT);
    22232234        }
    22242235    }
    22252236
     2237  if (is_nonblocking == SL_FALSE)
     2238    retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
    22262239  return ((int) bytes);
     2240}
     2241
     2242int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count,
     2243                     int timeout, int is_nonblocking)
     2244{
     2245  int    fd;
     2246 
     2247  if (buf_in == NULL || SL_ISERROR(fd = get_the_fd(ticket)))
     2248    {
     2249      if (buf_in == NULL)
     2250        {
     2251          TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
     2252          return (SL_ENULL);
     2253        }
     2254      if (SL_ISERROR(fd = get_the_fd(ticket)))
     2255        {
     2256          TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
     2257          return (fd);
     2258        }
     2259    }
     2260
     2261  return sl_read_timeout_fd (fd, buf_in, count, timeout, is_nonblocking);
    22272262}
    22282263
  • trunk/src/trustfile.c

    r111 r131  
    9999
    100100#ifndef TRUST_MAIN
     101
    101102#include "slib.h"
    102103#define SH_NEED_PWD_GRP 1
    103104#include "sh_static.h"
     105#define SH_GRBUF_SIZE 4096
     106#define SH_PWBUF_SIZE 4096
    104107
    105108#else
    106109
    107110#define sh_getgrgid   getgrgid
     111#define sh_getgrgid_r getgrgid_r
     112#define sh_getpwnam   getpwnam
     113#define sh_getpwnam_r getpwnam_r
     114#define sh_getpwuid   getpwuid
     115#define sh_getpwuid_r getpwuid_r
    108116#define sh_getpwent   getpwent
    109 #define sh_getpwnam   getpwnam
    110 #define sh_getpwuid   getpwuid
    111117#define sh_endpwent   endpwent
    112118
     
    410416  register struct group *g;     /* pointer to group information */
    411417 
     418#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     419  struct group    gr;
     420  char            buffer[SH_GRBUF_SIZE];
     421  struct passwd   pwd;
     422  char            pbuffer[SH_PWBUF_SIZE];
     423#endif
     424
    412425  SL_ENTER(_("isingrp"));
    413426
    414   if ((g = sh_getgrgid(grp)) == NULL)
     427#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     428  sh_getgrgid_r(grp, &gr, buffer, sizeof(buffer), &g);
     429#else
     430  g = sh_getgrgid(grp);
     431#endif
     432
     433  if (g == NULL)
    415434    {
    416435      SL_IRETURN(SL_FALSE, _("isingrp") );
     
    428447        {
    429448          /* map user name to UID and compare */
     449#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     450          sh_getpwnam_r(*p, &pwd, pbuffer, sizeof(pbuffer), &w);
     451#else
     452          w = sh_getpwnam(*p);
     453#endif
     454
    430455#ifdef TRUST_MAIN
    431           if ((w = sh_getpwnam(*p)) != NULL && *u == (uid_t)(w->pw_uid) )
     456          if (w != NULL && *u == (uid_t)(w->pw_uid) )
    432457            SL_IRETURN(SL_TRUE, _("isingrp"));
    433458#else
    434           if ((w = sh_getpwnam(*p)) != NULL && *u == (uid_t)(w->pw_uid) )
     459          if (w != NULL && *u == (uid_t)(w->pw_uid) )
    435460            {
    436461              SL_IRETURN(SL_TRUE, _("isingrp"));
     
    444469  for(u = ulist; *u != tf_uid_neg; u++)
    445470    {
     471#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
     472      sh_getpwuid_r(*u, &pwd, pbuffer, sizeof(pbuffer), &w);
     473#else
     474      w = sh_getpwuid(*u);
     475#endif
    446476#ifdef TRUST_MAIN
    447       if ((w = sh_getpwuid(*u)) != NULL && grp == (gid_t)(w->pw_gid) )
     477      if (w != NULL && grp == (gid_t)(w->pw_gid) )
    448478        SL_IRETURN(SL_TRUE, _("isingrp"));
    449479#else
    450       if ((w = sh_getpwuid(*u)) != NULL && grp == (gid_t)(w->pw_gid) )
     480      if (w != NULL && grp == (gid_t)(w->pw_gid) )
    451481        {
    452482          SL_IRETURN(SL_TRUE, _("isingrp"));
     
    470500  register int flag = -1;       /* group member found */
    471501
     502#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     503  struct group    gr;
     504  char            buffer[SH_GRBUF_SIZE];
     505  struct group    pw;
     506  char            pbuffer[SH_PWBUF_SIZE];
     507#endif
     508
    472509  SL_ENTER(_("onlytrustedingrp"));
    473510
     
    477514#endif
    478515
    479   if ((g = sh_getgrgid(grp)) == NULL)
     516#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
     517  sh_getgrgid_r(grp, &gr, buffer, sizeof(buffer), &g);
     518#else
     519  g = sh_getgrgid(grp);
     520#endif
     521
     522  if (g == NULL)
    480523    {
    481524#ifdef TRUST_DEBUG
     
    503546      /* map user name to UID and compare
    504547       */
     548#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
     549      sh_getpwnam_r(*p, &pw, pbuffer, sizeof(pbuffer), &w);
     550#else
    505551      w = sh_getpwnam(*p);
    506 #ifndef TRUST_MAIN
    507       if (!w)
    508         w = sh_getpwnam(*p);
    509 #endif
     552#endif
     553
    510554      if (w == NULL)    /* not a valid user, ignore    */
    511555        {
     
    9611005  tf_path[0] = '\0';
    9621006#if defined(SH_WITH_SERVER)
    963   pass = sh_getpwnam(SH_IDENT);
     1007  pass = sh_getpwnam(SH_IDENT);  /* TESTONLY */
    9641008  if (pass != NULL)
    9651009    tf_euid = pass->pw_uid;
Note: See TracChangeset for help on using the changeset viewer.