Diff for /embedaddon/sudo/plugins/sudoers/iolog.c between versions 1.1.1.5 and 1.1.1.6

version 1.1.1.5, 2013/10/14 07:56:34 version 1.1.1.6, 2014/06/15 16:12:54
Line 1 Line 1
 /*  /*
 * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2009-2014 Todd C. Miller <Todd.Miller@courtesan.com>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 37 Line 37
 #ifdef HAVE_UNISTD_H  #ifdef HAVE_UNISTD_H
 # include <unistd.h>  # include <unistd.h>
 #endif /* HAVE_UNISTD_H */  #endif /* HAVE_UNISTD_H */
#if TIME_WITH_SYS_TIME#ifdef TIME_WITH_SYS_TIME
 # include <time.h>  # include <time.h>
 #endif  #endif
 #include <errno.h>  #include <errno.h>
Line 136  io_mkdirs(char *path, mode_t mode, bool is_temp) Line 136  io_mkdirs(char *path, mode_t mode, bool is_temp)
 int  int
 io_set_max_sessid(const char *maxval)  io_set_max_sessid(const char *maxval)
 {  {
    unsigned long ulval;    const char *errstr;
    char *ep;    unsigned int value;
     debug_decl(io_set_max_sessid, SUDO_DEBUG_UTIL)
   
    errno = 0;    value = strtonum(maxval, 0, SESSID_MAX, &errstr);
    ulval = strtoul(maxval, &ep, 0);    if (errstr != NULL) {
    if (*maxval != '\0' && *ep == '\0' &&        if (errno != ERANGE) {
        (errno != ERANGE || ulval != ULONG_MAX)) {            sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
        sessid_max = MIN((unsigned int)ulval, SESSID_MAX);                "bad maxseq: %s: %s", maxval, errstr);
        return true;            debug_return_bool(false);
         }
         /* Out of range, clamp to SESSID_MAX as documented. */
         value = SESSID_MAX;
     }      }
    return false;    sessid_max = value;
     debug_return_bool(true);
 }  }
   
 /*  /*
Line 176  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c Line 181  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c
      * Open sequence file       * Open sequence file
      */       */
     len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", iolog_dir);      len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", iolog_dir);
    if (len <= 0 || len >= sizeof(pathbuf)) {    if (len <= 0 || (size_t)len >= sizeof(pathbuf)) {
         errno = ENAMETOOLONG;          errno = ENAMETOOLONG;
         log_fatal(USE_ERRNO, "%s/seq", pathbuf);          log_fatal(USE_ERRNO, "%s/seq", pathbuf);
     }      }
Line 196  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c Line 201  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c
   
         len = snprintf(fallback, sizeof(fallback), "%s/seq",          len = snprintf(fallback, sizeof(fallback), "%s/seq",
             iolog_dir_fallback);              iolog_dir_fallback);
        if (len > 0 && len < sizeof(fallback)) {        if (len > 0 && (size_t)len < sizeof(fallback)) {
             int fd2 = open(fallback, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);              int fd2 = open(fallback, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
             if (fd2 != -1) {              if (fd2 != -1) {
                nread = read(fd2, buf, sizeof(buf));                nread = read(fd2, buf, sizeof(buf) - 1);
                 if (nread > 0) {                  if (nread > 0) {
                       if (buf[nread - 1] == '\n')
                           nread--;
                       buf[nread] = '\0';
                     id = strtoul(buf, &ep, 36);                      id = strtoul(buf, &ep, 36);
                    if (buf == ep || id >= sessid_max)                    if (ep == buf || *ep != '\0' || id >= sessid_max) {
                         sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
                             "%s: bad sequence number: %s", fallback, buf);
                         id = 0;                          id = 0;
                       }
                 }                  }
                 close(fd2);                  close(fd2);
             }              }
Line 212  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c Line 223  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c
   
     /* Read current seq number (base 36). */      /* Read current seq number (base 36). */
     if (id == 0) {      if (id == 0) {
        nread = read(fd, buf, sizeof(buf));        nread = read(fd, buf, sizeof(buf) - 1);
         if (nread != 0) {          if (nread != 0) {
             if (nread == -1)              if (nread == -1)
                 log_fatal(USE_ERRNO, N_("unable to read %s"), pathbuf);                  log_fatal(USE_ERRNO, N_("unable to read %s"), pathbuf);
               if (buf[nread - 1] == '\n')
                   nread--;
               buf[nread] = '\0';
             id = strtoul(buf, &ep, 36);              id = strtoul(buf, &ep, 36);
            if (buf == ep || id >= sessid_max)            if (ep == buf || *ep != '\0' || id >= sessid_max) {
                 sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
                     "%s: bad sequence number: %s", pathbuf, buf);
                 id = 0;                  id = 0;
               }
         }          }
     }      }
     id++;      id++;
Line 237  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c Line 254  io_nextid(char *iolog_dir, char *iolog_dir_fallback, c
     memcpy(sessid, buf, 6);      memcpy(sessid, buf, 6);
     sessid[6] = '\0';      sessid[6] = '\0';
   
    /* Rewind and overwrite old seq file. */    /* Rewind and overwrite old seq file, including the NUL byte. */
     if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1 || write(fd, buf, 7) != 7)      if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1 || write(fd, buf, 7) != 7)
         log_fatal(USE_ERRNO, N_("unable to write to %s"), pathbuf);          log_fatal(USE_ERRNO, N_("unable to write to %s"), pathbuf);
     close(fd);      close(fd);
Line 288  open_io_fd(char *pathbuf, size_t len, struct io_log_fi Line 305  open_io_fd(char *pathbuf, size_t len, struct io_log_fi
     pathbuf[len] = '\0';      pathbuf[len] = '\0';
     strlcat(pathbuf, iol->suffix, PATH_MAX);      strlcat(pathbuf, iol->suffix, PATH_MAX);
     if (iol->enabled) {      if (iol->enabled) {
        fd = open(pathbuf, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);        fd = open(pathbuf, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
         if (fd != -1) {          if (fd != -1) {
             fcntl(fd, F_SETFD, FD_CLOEXEC);              fcntl(fd, F_SETFD, FD_CLOEXEC);
 #ifdef HAVE_ZLIB_H  #ifdef HAVE_ZLIB_H
Line 332  iolog_deserialize_info(struct iolog_details *details,  Line 349  iolog_deserialize_info(struct iolog_details *details, 
         switch (**cur) {          switch (**cur) {
         case 'c':          case 'c':
             if (strncmp(*cur, "cols=", sizeof("cols=") - 1) == 0) {              if (strncmp(*cur, "cols=", sizeof("cols=") - 1) == 0) {
                details->cols = atoi(*cur + sizeof("cols=") - 1);                int n = strtonum(*cur + sizeof("cols=") - 1, 1, INT_MAX, NULL);
                 if (n > 0)
                     details->cols = n;
                 continue;                  continue;
             }              }
             if (strncmp(*cur, "cwd=", sizeof("cwd=") - 1) == 0) {              if (strncmp(*cur, "cwd=", sizeof("cwd=") - 1) == 0) {
Line 342  iolog_deserialize_info(struct iolog_details *details,  Line 361  iolog_deserialize_info(struct iolog_details *details, 
             break;              break;
         case 'l':          case 'l':
             if (strncmp(*cur, "lines=", sizeof("lines=") - 1) == 0) {              if (strncmp(*cur, "lines=", sizeof("lines=") - 1) == 0) {
                details->lines = atoi(*cur + sizeof("lines=") - 1);                int n = strtonum(*cur + sizeof("lines=") - 1, 1, INT_MAX, NULL);
                 if (n > 0)
                     details->lines = n;
                 continue;                  continue;
             }              }
             break;              break;
Line 438  iolog_deserialize_info(struct iolog_details *details,  Line 459  iolog_deserialize_info(struct iolog_details *details, 
     if (runas_uid_str != NULL) {      if (runas_uid_str != NULL) {
         id = atoid(runas_uid_str, NULL, NULL, &errstr);          id = atoid(runas_uid_str, NULL, NULL, &errstr);
         if (errstr != NULL)          if (errstr != NULL)
            warningx("runas uid %s: %s", runas_uid_str, _(errstr));            warningx("runas uid %s: %s", runas_uid_str, U_(errstr));
         else          else
             runas_uid = (uid_t)id;              runas_uid = (uid_t)id;
     }      }
Line 447  iolog_deserialize_info(struct iolog_details *details,  Line 468  iolog_deserialize_info(struct iolog_details *details, 
     if (runas_gid_str != NULL) {      if (runas_gid_str != NULL) {
         id = atoid(runas_gid_str, NULL, NULL, &errstr);          id = atoid(runas_gid_str, NULL, NULL, &errstr);
         if (errstr != NULL)          if (errstr != NULL)
            warningx("runas gid %s: %s", runas_gid_str, _(errstr));            warningx("runas gid %s: %s", runas_gid_str, U_(errstr));
         else          else
             runas_gid = (gid_t)id;              runas_gid = (gid_t)id;
     }      }
Line 486  write_info_log(char *pathbuf, size_t len, struct iolog Line 507  write_info_log(char *pathbuf, size_t len, struct iolog
   
     pathbuf[len] = '\0';      pathbuf[len] = '\0';
     strlcat(pathbuf, "/log", PATH_MAX);      strlcat(pathbuf, "/log", PATH_MAX);
    fd = open(pathbuf, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);    fd = open(pathbuf, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
     if (fd == -1 || (fp = fdopen(fd, "w")) == NULL)      if (fd == -1 || (fp = fdopen(fd, "w")) == NULL)
         log_fatal(USE_ERRNO, N_("unable to create %s"), pathbuf);          log_fatal(USE_ERRNO, N_("unable to create %s"), pathbuf);
   
Line 581  sudoers_io_open(unsigned int version, sudo_conv_t conv Line 602  sudoers_io_open(unsigned int version, sudo_conv_t conv
     gettimeofday(&last_time, NULL);      gettimeofday(&last_time, NULL);
     write_info_log(pathbuf, len, &details, argv, &last_time);      write_info_log(pathbuf, len, &details, argv, &last_time);
   
    /* Create the timing I/O log files. */    /* Create the timing and I/O log files. */
     for (i = 0; i < IOFD_MAX; i++)      for (i = 0; i < IOFD_MAX; i++)
         open_io_fd(pathbuf, len, &io_log_files[i], iolog_compress);          open_io_fd(pathbuf, len, &io_log_files[i], iolog_compress);
   
Line 679  sudoers_io_log(const char *buf, unsigned int len, int  Line 700  sudoers_io_log(const char *buf, unsigned int len, int 
     else      else
 #endif  #endif
         ignore_result(fwrite(buf, 1, len, io_log_files[idx].fd.f));          ignore_result(fwrite(buf, 1, len, io_log_files[idx].fd.f));
    delay.tv_sec = now.tv_sec;    sudo_timevalsub(&now, &last_time, &delay);
    delay.tv_usec = now.tv_usec; 
    timevalsub(&delay, &last_time); 
 #ifdef HAVE_ZLIB_H  #ifdef HAVE_ZLIB_H
     if (iolog_compress)      if (iolog_compress)
        gzprintf(io_log_files[IOFD_TIMING].fd.g, "%d %f %d\n", idx,        gzprintf(io_log_files[IOFD_TIMING].fd.g, "%d %f %u\n", idx,
             delay.tv_sec + ((double)delay.tv_usec / 1000000), len);              delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
     else      else
 #endif  #endif
        fprintf(io_log_files[IOFD_TIMING].fd.f, "%d %f %d\n", idx,        fprintf(io_log_files[IOFD_TIMING].fd.f, "%d %f %u\n", idx,
             delay.tv_sec + ((double)delay.tv_usec / 1000000), len);              delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
     last_time.tv_sec = now.tv_sec;      last_time.tv_sec = now.tv_sec;
     last_time.tv_usec = now.tv_usec;      last_time.tv_usec = now.tv_usec;

Removed from v.1.1.1.5  
changed lines
  Added in v.1.1.1.6


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>