Diff for /embedaddon/sudo/src/get_pty.c between versions 1.1.1.1 and 1.1.1.3

version 1.1.1.1, 2012/02/21 16:23:02 version 1.1.1.3, 2013/07/22 10:46:13
Line 1 Line 1
 /*  /*
 * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2009-2012 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 17 Line 17
 #include <config.h>  #include <config.h>
   
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/param.h>  
 #include <sys/stat.h>  #include <sys/stat.h>
 #include <sys/ioctl.h>  #include <sys/ioctl.h>
 #ifdef HAVE_SYS_STROPTS_H  #ifdef HAVE_SYS_STROPTS_H
Line 63  get_pty(int *master, int *slave, char *name, size_t na Line 62  get_pty(int *master, int *slave, char *name, size_t na
 {  {
     struct group *gr;      struct group *gr;
     gid_t ttygid = -1;      gid_t ttygid = -1;
       int rval = 0;
       debug_decl(get_pty, SUDO_DEBUG_PTY)
   
     if ((gr = getgrnam("tty")) != NULL)      if ((gr = getgrnam("tty")) != NULL)
         ttygid = gr->gr_gid;          ttygid = gr->gr_gid;
   
    if (openpty(master, slave, name, NULL, NULL) != 0)    if (openpty(master, slave, name, NULL, NULL) == 0) {
        return 0;        if (chown(name, ttyuid, ttygid) == 0)
    if (chown(name, ttyuid, ttygid) != 0)            rval = 1;
        return 0;    }
    return 1;
     debug_return_bool(rval);
 }  }
   
 #elif defined(HAVE__GETPTY)  #elif defined(HAVE__GETPTY)
Line 79  int Line 81  int
 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)  get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
 {  {
     char *line;      char *line;
       int rval = 0;
       debug_decl(get_pty, SUDO_DEBUG_PTY)
   
     /* IRIX-style dynamic ptys (may fork) */      /* IRIX-style dynamic ptys (may fork) */
     line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);      line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
    if (line == NULL)    if (line != NULL) {
        return 0;        *slave = open(line, O_RDWR|O_NOCTTY, 0);
    *slave = open(line, O_RDWR|O_NOCTTY, 0);        if (*slave != -1) {
    if (*slave == -1) {            (void) chown(line, ttyuid, -1);
        close(*master);            strlcpy(name, line, namesz);
        return 0;            rval = 1;
         } else {
             close(*master);
             *master = -1;
         }
     }      }
    (void) chown(line, ttyuid, -1);    debug_return_bool(rval);
    strlcpy(name, line, namesz); 
    return 1; 
 }  }
 #elif defined(HAVE_GRANTPT)  #elif defined(HAVE_GRANTPT)
 # ifndef HAVE_POSIX_OPENPT  # ifndef HAVE_POSIX_OPENPT
Line 113  int Line 119  int
 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)  get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
 {  {
     char *line;      char *line;
       int rval = 0;
       debug_decl(get_pty, SUDO_DEBUG_PTY)
   
     *master = posix_openpt(O_RDWR|O_NOCTTY);      *master = posix_openpt(O_RDWR|O_NOCTTY);
    if (*master == -1)    if (*master != -1) {
        return 0;        (void) grantpt(*master); /* may fork */
        if (unlockpt(*master) != 0) {
    (void) grantpt(*master); /* may fork */            close(*master);
    if (unlockpt(*master) != 0) {            goto done;
        close(*master);        }
        return 0;        line = ptsname(*master);
    }        if (line == NULL) {
    line = ptsname(*master);            close(*master);
    if (line == NULL) {            goto done;
        close(*master);        }
        return 0;        *slave = open(line, O_RDWR|O_NOCTTY, 0);
    }        if (*slave == -1) {
    *slave = open(line, O_RDWR|O_NOCTTY, 0);            close(*master);
    if (*slave == -1) {            goto done;
        close(*master);        }
        return 0; 
    } 
 # if defined(I_PUSH) && !defined(_AIX)  # if defined(I_PUSH) && !defined(_AIX)
    ioctl(*slave, I_PUSH, "ptem");      /* pseudo tty emulation module */        ioctl(*slave, I_PUSH, "ptem");  /* pseudo tty emulation module */
    ioctl(*slave, I_PUSH, "ldterm");    /* line discipline module */        ioctl(*slave, I_PUSH, "ldterm");        /* line discipline module */
 # endif  # endif
    (void) chown(line, ttyuid, -1);        (void) chown(line, ttyuid, -1);
    strlcpy(name, line, namesz);        strlcpy(name, line, namesz);
    return 1;        rval = 1;
     }
 done:
     debug_return_bool(rval);
 }  }
   
 #else /* Old-style BSD ptys */  #else /* Old-style BSD ptys */
Line 152  get_pty(int *master, int *slave, char *name, size_t na Line 161  get_pty(int *master, int *slave, char *name, size_t na
     char *bank, *cp;      char *bank, *cp;
     struct group *gr;      struct group *gr;
     gid_t ttygid = -1;      gid_t ttygid = -1;
       int rval = 0;
       debug_decl(get_pty, SUDO_DEBUG_PTY)
   
     if ((gr = getgrnam("tty")) != NULL)      if ((gr = getgrnam("tty")) != NULL)
         ttygid = gr->gr_gid;          ttygid = gr->gr_gid;
Line 163  get_pty(int *master, int *slave, char *name, size_t na Line 174  get_pty(int *master, int *slave, char *name, size_t na
             *master = open(line, O_RDWR|O_NOCTTY, 0);              *master = open(line, O_RDWR|O_NOCTTY, 0);
             if (*master == -1) {              if (*master == -1) {
                 if (errno == ENOENT)                  if (errno == ENOENT)
                    return 0; /* out of ptys */                    goto done; /* out of ptys */
                 continue; /* already in use */                  continue; /* already in use */
             }              }
             line[sizeof("/dev/p") - 2] = 't';              line[sizeof("/dev/p") - 2] = 't';
Line 175  get_pty(int *master, int *slave, char *name, size_t na Line 186  get_pty(int *master, int *slave, char *name, size_t na
             *slave = open(line, O_RDWR|O_NOCTTY, 0);              *slave = open(line, O_RDWR|O_NOCTTY, 0);
             if (*slave != -1) {              if (*slave != -1) {
                     strlcpy(name, line, namesz);                      strlcpy(name, line, namesz);
                    return 1; /* success */                    rval = 1; /* success */
                     goto done;
             }              }
             (void) close(*master);              (void) close(*master);
         }          }
     }      }
    return 0;done:
     debug_return(rval);
 }  }
 #endif /* HAVE_OPENPTY */  #endif /* HAVE_OPENPTY */

Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.3


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