kpty.cpp

00001 /*
00002 
00003    This file is part of the KDE libraries
00004    Copyright (C) 1997-2002 The Konsole Developers
00005    Copyright (C) 2002 Waldo Bastian <bastian@kde.org>
00006    Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <config.h>
00025 
00026 #include "kpty.h"
00027 #include "kprocess.h"
00028 
00029 #ifdef __sgi
00030 #define __svr4__
00031 #endif
00032 
00033 #ifdef __osf__
00034 #define _OSF_SOURCE
00035 #include <float.h>
00036 #endif
00037 
00038 #ifdef _AIX
00039 #define _ALL_SOURCE
00040 #endif
00041 
00042 // __USE_XOPEN isn't defined by default in ICC
00043 // (needed for ptsname(), grantpt() and unlockpt())
00044 #ifdef __INTEL_COMPILER
00045 #  ifndef __USE_XOPEN
00046 #    define __USE_XOPEN
00047 #  endif
00048 #endif
00049 
00050 #include <sys/types.h>
00051 #include <sys/ioctl.h>
00052 #include <sys/time.h>
00053 #include <sys/resource.h>
00054 #include <sys/stat.h>
00055 #include <sys/param.h>
00056 
00057 #ifdef HAVE_SYS_STROPTS_H
00058 # include <sys/stropts.h>   // Defines I_PUSH
00059 # define _NEW_TTY_CTRL
00060 #endif
00061 
00062 #include <errno.h>
00063 #include <fcntl.h>
00064 #include <time.h>
00065 #include <stdlib.h>
00066 #include <stdio.h>
00067 #include <string.h>
00068 #include <unistd.h>
00069 #include <grp.h>
00070 
00071 #ifdef HAVE_LIBUTIL_H
00072 # include <libutil.h>
00073 # define USE_LOGIN
00074 #elif defined(HAVE_UTIL_H)
00075 # include <util.h>
00076 # define USE_LOGIN
00077 #endif
00078 
00079 #ifdef USE_LOGIN
00080 # include <utmp.h>
00081 #endif
00082 
00083 #ifdef HAVE_UTEMPTER
00084 # include <utempter.h>
00085 #endif
00086 
00087 #ifdef HAVE_TERMIOS_H
00088 /* for HP-UX (some versions) the extern C is needed, and for other
00089    platforms it doesn't hurt */
00090 extern "C" {
00091 # include <termios.h>
00092 }
00093 #endif
00094 
00095 #if !defined(__osf__)
00096 # ifdef HAVE_TERMIO_H
00097 /* needed at least on AIX */
00098 #  include <termio.h>
00099 # endif
00100 #endif
00101 
00102 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00103 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
00104 #else
00105 # if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__)
00106 #  define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
00107 # else
00108 #  define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
00109 # endif
00110 #endif
00111 
00112 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00113 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
00114 #else
00115 # if defined(_HPUX_SOURCE) || defined(__CYGWIN__)
00116 #  define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode) 
00117 # else
00118 #  define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
00119 # endif
00120 #endif
00121 
00122 #if defined (_HPUX_SOURCE)
00123 # define _TERMIOS_INCLUDED
00124 # include <bsdtty.h>
00125 #endif
00126 
00127 #if defined(HAVE_PTY_H)
00128 # include <pty.h>
00129 #endif
00130 
00131 #include <kdebug.h>
00132 #include <kstandarddirs.h>  // locate
00133 
00134 #ifndef CINTR
00135 #define CINTR   0x03
00136 #endif
00137 #ifndef CQUIT
00138 #define CQUIT   0x1c
00139 #endif
00140 #ifndef CERASE
00141 #define CERASE  0x7f
00142 #endif
00143 
00144 #define TTY_GROUP "tty"
00145 
00147 // private functions //
00149 
00150 #define BASE_CHOWN "kgrantpty"
00151 
00152 
00153 
00155 // private data //
00157 
00158 struct KPtyPrivate {
00159    KPtyPrivate() :
00160      xonXoff(false),
00161      utf8(false),
00162      masterFd(-1), slaveFd(-1)
00163    {
00164      memset(&winSize, 0, sizeof(winSize));
00165      winSize.ws_row = 24;
00166      winSize.ws_col = 80;
00167    }
00168 
00169    bool xonXoff : 1;
00170    bool utf8    : 1;
00171    int masterFd;
00172    int slaveFd;
00173    struct winsize winSize;
00174 
00175    QCString ttyName;
00176 };
00177 
00179 // public member functions //
00181 
00182 KPty::KPty()
00183 {
00184   d = new KPtyPrivate;
00185 }
00186 
00187 KPty::~KPty()
00188 {
00189   close();
00190   delete d;
00191 }
00192 
00193 bool KPty::open()
00194 {
00195   if (d->masterFd >= 0)
00196     return true;
00197 
00198   QCString ptyName;
00199 
00200   // Find a master pty that we can open ////////////////////////////////
00201 
00202   // Because not all the pty animals are created equal, they want to
00203   // be opened by several different methods.
00204 
00205   // We try, as we know them, one by one.
00206 
00207 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00208 #ifdef _AIX
00209   d->masterFd = ::open("/dev/ptc",O_RDWR);
00210 #else
00211   d->masterFd = ::open("/dev/ptmx",O_RDWR);
00212 #endif
00213   if (d->masterFd >= 0)
00214   {
00215     char *ptsn = ptsname(d->masterFd);
00216     if (ptsn) {
00217         grantpt(d->masterFd);
00218         d->ttyName = ptsn;
00219         goto gotpty;
00220     } else {
00221        ::close(d->masterFd);
00222        d->masterFd = -1;
00223     }
00224   }
00225 #endif
00226 
00227   // Linux device names, FIXME: Trouble on other systems?
00228   for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
00229   {
00230     for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
00231     {
00232       ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
00233       d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
00234 
00235       d->masterFd = ::open(ptyName.data(), O_RDWR);
00236       if (d->masterFd >= 0)
00237       {
00238 #ifdef __sun
00239         /* Need to check the process group of the pty.
00240          * If it exists, then the slave pty is in use,
00241          * and we need to get another one.
00242          */
00243         int pgrp_rtn;
00244         if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
00245           ::close(d->masterFd);
00246           d->masterFd = -1;
00247           continue;
00248         }
00249 #endif /* sun */
00250         if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
00251         {
00252           if (!geteuid())
00253           {
00254             struct group* p = getgrnam(TTY_GROUP);
00255             if (!p)
00256               p = getgrnam("wheel");
00257             gid_t gid = p ? p->gr_gid : getgid ();
00258 
00259             chown(d->ttyName.data(), getuid(), gid);
00260             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
00261           }
00262           goto gotpty;
00263         }
00264         ::close(d->masterFd);
00265         d->masterFd = -1;
00266       }
00267     }
00268   }
00269 
00270   kdWarning(175) << "Can't open a pseudo teletype" << endl;
00271   return false;
00272 
00273  gotpty:
00274   struct stat st;
00275   if (stat(d->ttyName.data(), &st))
00276     return false; // this just cannot happen ... *cough*  Yeah right, I just
00277                   // had it happen when pty #349 was allocated.  I guess
00278                   // there was some sort of leak?  I only had a few open.
00279   if (((st.st_uid != getuid()) ||
00280        (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
00281       !chownpty(true))
00282   {
00283     kdWarning(175)
00284       << "chownpty failed for device " << ptyName << "::" << d->ttyName
00285       << "\nThis means the communication can be eavesdropped." << endl;
00286   }
00287 
00288 #ifdef BSD
00289   revoke(d->ttyName.data());
00290 #endif
00291 
00292 #ifdef HAVE_UNLOCKPT
00293   unlockpt(d->masterFd);
00294 #endif
00295 
00296   d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
00297   if (d->slaveFd < 0)
00298   {
00299     kdWarning(175) << "Can't open slave pseudo teletype" << endl;
00300     ::close(d->masterFd);
00301     d->masterFd = -1;
00302     return false;
00303   }
00304 
00305 #if (defined(__svr4__) || defined(__sgi__))
00306   // Solaris
00307   ioctl(d->slaveFd, I_PUSH, "ptem");
00308   ioctl(d->slaveFd, I_PUSH, "ldterm");
00309 #endif
00310 
00311     // set xon/xoff & control keystrokes
00312   // without the '::' some version of HP-UX thinks, this declares
00313   // the struct in this class, in this method, and fails to find
00314   // the correct tc[gs]etattr
00315   struct ::termios ttmode;
00316 
00317   _tcgetattr(d->slaveFd, &ttmode);
00318 
00319   if (!d->xonXoff)
00320     ttmode.c_iflag &= ~(IXOFF | IXON);
00321   else
00322     ttmode.c_iflag |= (IXOFF | IXON);
00323 
00324 #ifdef IUTF8
00325   if (!d->utf8)
00326     ttmode.c_iflag &= ~IUTF8;
00327   else
00328     ttmode.c_iflag |= IUTF8;
00329 #endif
00330 
00331   ttmode.c_cc[VINTR] = CINTR;
00332   ttmode.c_cc[VQUIT] = CQUIT;
00333   ttmode.c_cc[VERASE] = CERASE;
00334 
00335   _tcsetattr(d->slaveFd, &ttmode);
00336 
00337   // set screen size
00338   ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00339 
00340   fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
00341   fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
00342 
00343   return true;
00344 }
00345 
00346 void KPty::close()
00347 {
00348    if (d->masterFd < 0)
00349       return;
00350    // don't bother resetting unix98 pty, it will go away after closing master anyway.
00351    if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
00352       if (!geteuid()) {
00353          struct stat st;
00354          if (!stat(d->ttyName.data(), &st)) {
00355             chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
00356             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
00357          }
00358       } else {
00359          fcntl(d->masterFd, F_SETFD, 0);
00360          chownpty(false);
00361       }
00362    }
00363    ::close(d->slaveFd);
00364    ::close(d->masterFd);
00365    d->masterFd = d->slaveFd = -1;
00366 }
00367 
00368 void KPty::setCTty()
00369 {
00370     // Setup job control //////////////////////////////////
00371 
00372     // Become session leader, process group leader,
00373     // and get rid of the old controlling terminal.
00374     setsid();
00375 
00376     // make our slave pty the new controlling terminal.
00377 #ifdef TIOCSCTTY
00378     ioctl(d->slaveFd, TIOCSCTTY, 0);
00379 #else
00380     // SVR4 hack: the first tty opened after setsid() becomes controlling tty
00381     ::close(::open(d->ttyName, O_WRONLY, 0));
00382 #endif
00383 
00384     // make our new process group the foreground group on the pty
00385     int pgrp = getpid();
00386 #if defined(_POSIX_VERSION) || defined(__svr4__)
00387     tcsetpgrp (d->slaveFd, pgrp);
00388 #elif defined(TIOCSPGRP)
00389     ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
00390 #endif
00391 }
00392 
00393 void KPty::login(const char *user, const char *remotehost)
00394 {
00395 #ifdef HAVE_UTEMPTER
00396     utempter_add_record (d->masterFd, remotehost);
00397     Q_UNUSED(user);
00398 #elif defined(USE_LOGIN)
00399     const char *str_ptr;
00400     struct utmp l_struct;
00401     memset(&l_struct, 0, sizeof(struct utmp));
00402     // note: strncpy without terminators _is_ correct here. man 4 utmp
00403 
00404     if (user)
00405       strncpy(l_struct.ut_name, user, UT_NAMESIZE);
00406 
00407     if (remotehost)
00408       strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
00409 
00410 # ifndef __GLIBC__
00411     str_ptr = d->ttyName.data();
00412     if (!memcmp(str_ptr, "/dev/", 5))
00413         str_ptr += 5;
00414     strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
00415 # endif
00416 
00417     // Handle 64-bit time_t properly, where it may be larger
00418     // than the integral type of ut_time.
00419     {
00420         time_t ut_time_temp;
00421         time(&ut_time_temp);
00422         l_struct.ut_time=ut_time_temp;
00423     }
00424 
00425     ::login(&l_struct);
00426 #else
00427     Q_UNUSED(user);
00428     Q_UNUSED(remotehost);
00429 #endif
00430 }
00431 
00432 void KPty::logout()
00433 {
00434 #ifdef HAVE_UTEMPTER
00435     utempter_remove_record (d->masterFd);
00436 #elif defined(USE_LOGIN)
00437     const char *str_ptr = d->ttyName.data();
00438     if (!memcmp(str_ptr, "/dev/", 5))
00439         str_ptr += 5;
00440 # ifdef __GLIBC__
00441     else {
00442         const char *sl_ptr = strrchr(str_ptr, '/');
00443         if (sl_ptr)
00444             str_ptr = sl_ptr + 1;
00445     }
00446 # endif
00447     ::logout(str_ptr);
00448 #endif
00449 }
00450 
00451 void KPty::setWinSize(int lines, int columns)
00452 {
00453   d->winSize.ws_row = (unsigned short)lines;
00454   d->winSize.ws_col = (unsigned short)columns;
00455   if (d->masterFd >= 0)
00456     ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
00457 }
00458 
00459 void KPty::setXonXoff(bool useXonXoff)
00460 {
00461   d->xonXoff = useXonXoff;
00462   if (d->masterFd >= 0) {
00463     // without the '::' some version of HP-UX thinks, this declares
00464     // the struct in this class, in this method, and fails to find
00465     // the correct tc[gs]etattr
00466     struct ::termios ttmode;
00467 
00468     _tcgetattr(d->masterFd, &ttmode);
00469 
00470     if (!useXonXoff)
00471       ttmode.c_iflag &= ~(IXOFF | IXON);
00472     else
00473       ttmode.c_iflag |= (IXOFF | IXON);
00474 
00475     _tcsetattr(d->masterFd, &ttmode);
00476   }
00477 }
00478 
00479 void KPty::setUtf8Mode(bool useUtf8)
00480 {
00481   d->utf8 = useUtf8;
00482 #ifdef IUTF8
00483   if (d->masterFd >= 0) {
00484     // without the '::' some version of HP-UX thinks, this declares
00485     // the struct in this class, in this method, and fails to find
00486     // the correct tc[gs]etattr
00487     struct ::termios ttmode;
00488 
00489     _tcgetattr(d->masterFd, &ttmode);
00490 
00491     if (!useUtf8)
00492       ttmode.c_iflag &= ~IUTF8;
00493     else
00494       ttmode.c_iflag |= IUTF8;
00495 
00496     _tcsetattr(d->masterFd, &ttmode);
00497   }
00498 #endif
00499 }
00500 
00501 const char *KPty::ttyName() const
00502 {
00503     return d->ttyName.data();
00504 }
00505 
00506 int KPty::masterFd() const
00507 {
00508     return d->masterFd;
00509 }
00510 
00511 int KPty::slaveFd() const
00512 {
00513     return d->slaveFd;
00514 }
00515 
00516 // private
00517 bool KPty::chownpty(bool grant)
00518 {
00519   KProcess proc;
00520   proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << QString::number(d->masterFd);
00521   return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();
00522 }
00523 
KDE Home | KDE Accessibility Home | Description of Access Keys