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_TERMIOS_H
00084 /* for HP-UX (some versions) the extern C is needed, and for other
00085    platforms it doesn't hurt */
00086 extern "C" {
00087 # include <termios.h>
00088 }
00089 #endif
00090 
00091 #if !defined(__osf__)
00092 # ifdef HAVE_TERMIO_H
00093 /* needed at least on AIX */
00094 #  include <termio.h>
00095 # endif
00096 #endif
00097 
00098 #if defined(HAVE_TCGETATTR)
00099 # define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
00100 #elif defined(TIOCGETA)
00101 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
00102 #elif defined(TCGETS)
00103 # define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
00104 #else
00105 # error
00106 #endif
00107 
00108 #if defined(HAVE_TCSETATTR) && defined(TCSANOW)
00109 # define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
00110 #elif defined(TIOCSETA)
00111 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
00112 #elif defined(TCSETS)
00113 # define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
00114 #else
00115 # error
00116 #endif
00117 
00118 #if defined (_HPUX_SOURCE)
00119 # define _TERMIOS_INCLUDED
00120 # include <bsdtty.h>
00121 #endif
00122 
00123 #if defined(HAVE_PTY_H)
00124 # include <pty.h>
00125 #endif
00126 
00127 #include <kdebug.h>
00128 #include <kstandarddirs.h>  // locate
00129 
00130 #ifndef CINTR
00131 #define CINTR   0x03
00132 #endif
00133 #ifndef CQUIT
00134 #define CQUIT   0x1c
00135 #endif
00136 #ifndef CERASE
00137 #define CERASE  0x7f
00138 #endif
00139 
00140 #define TTY_GROUP "tty"
00141 
00143 // private functions //
00145 
00146 #ifdef HAVE_UTEMPTER
00147 class KProcess_Utmp : public KProcess
00148 {
00149 public:
00150    int commSetupDoneC()
00151    {
00152      dup2(cmdFd, 0);
00153      dup2(cmdFd, 1);
00154      dup2(cmdFd, 3);
00155      return 1;
00156    }
00157    int cmdFd;
00158 };
00159 #endif
00160 
00161 #define BASE_CHOWN "kgrantpty"
00162 
00163 
00164 
00166 // private data //
00168 
00169 struct KPtyPrivate {
00170    KPtyPrivate() :
00171      xonXoff(false),
00172      utf8(false),
00173      masterFd(-1), slaveFd(-1)
00174    {
00175      memset(&winSize, 0, sizeof(winSize));
00176      winSize.ws_row = 24;
00177      winSize.ws_col = 80;
00178    }
00179 
00180    bool xonXoff : 1;
00181    bool utf8    : 1;
00182    int masterFd;
00183    int slaveFd;
00184    struct winsize winSize;
00185 
00186    QCString ttyName;
00187 };
00188 
00190 // public member functions //
00192 
00193 KPty::KPty()
00194 {
00195   d = new KPtyPrivate;
00196 }
00197 
00198 KPty::~KPty()
00199 {
00200   close();
00201   delete d;
00202 }
00203 
00204 bool KPty::setPty(int pty_master)
00205 {
00206    kdWarning(175)
00207       << "setPty()" << endl;
00208    // a pty is already open
00209    if(d->masterFd >= 0) {
00210       kdWarning(175)
00211      << "d->masterFd >= 0" << endl;
00212       return false;
00213    }
00214    d->masterFd = pty_master;
00215    return _attachPty(pty_master);
00216 }
00217 
00218 bool KPty::_attachPty(int pty_master)
00219 {
00220   QCString ptyName;
00221 
00222     kdWarning(175)
00223        << "_attachPty() " << pty_master << endl;
00224 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00225     char *ptsn = ptsname(d->masterFd);
00226     if (ptsn) {
00227         grantpt(d->masterFd);
00228         d->ttyName = ptsn;
00229     } else {
00230        ::close(d->masterFd);
00231        d->masterFd = -1;
00232     }
00233 #endif
00234 
00235   struct stat st;
00236   if (stat(d->ttyName.data(), &st))
00237     return false; // this just cannot happen ... *cough*  Yeah right, I just
00238                   // had it happen when pty #349 was allocated.  I guess
00239                   // there was some sort of leak?  I only had a few open.
00240   if (((st.st_uid != getuid()) ||
00241        (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
00242       !chownpty(true))
00243   {
00244     kdWarning(175)
00245       << "chownpty failed for device " << ptyName << "::" << d->ttyName
00246       << "\nThis means the communication can be eavesdropped." << endl;
00247   }
00248 
00249 #ifdef BSD
00250   revoke(d->ttyName.data());
00251 #endif
00252 
00253 #ifdef HAVE_UNLOCKPT
00254   unlockpt(d->masterFd);
00255 #endif
00256 
00257   d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
00258   if (d->slaveFd < 0)
00259   {
00260     kdWarning(175) << "Can't open slave pseudo teletype" << endl;
00261     ::close(d->masterFd);
00262     d->masterFd = -1;
00263     return false;
00264   }
00265 
00266 #if (defined(__svr4__) || defined(__sgi__))
00267   // Solaris
00268   ioctl(d->slaveFd, I_PUSH, "ptem");
00269   ioctl(d->slaveFd, I_PUSH, "ldterm");
00270 #endif
00271 
00272     // set xon/xoff & control keystrokes
00273   // without the '::' some version of HP-UX thinks, this declares
00274   // the struct in this class, in this method, and fails to find
00275   // the correct tc[gs]etattr
00276   struct ::termios ttmode;
00277 
00278   _tcgetattr(d->slaveFd, &ttmode);
00279 
00280   if (!d->xonXoff)
00281     ttmode.c_iflag &= ~(IXOFF | IXON);
00282   else
00283     ttmode.c_iflag |= (IXOFF | IXON);
00284 
00285 #ifdef IUTF8
00286   if (!d->utf8)
00287     ttmode.c_iflag &= ~IUTF8;
00288   else
00289     ttmode.c_iflag |= IUTF8;
00290 #endif
00291 
00292   ttmode.c_cc[VINTR] = CINTR;
00293   ttmode.c_cc[VQUIT] = CQUIT;
00294   ttmode.c_cc[VERASE] = CERASE;
00295 
00296   _tcsetattr(d->slaveFd, &ttmode);
00297 
00298   // set screen size
00299   ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00300 
00301   fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
00302   fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
00303 
00304   return true;
00305 }
00306 
00307 bool KPty::open()
00308 {
00309   if (d->masterFd >= 0)
00310     return true;
00311 
00312   QCString ptyName;
00313 
00314   // Find a master pty that we can open ////////////////////////////////
00315 
00316   // Because not all the pty animals are created equal, they want to
00317   // be opened by several different methods.
00318 
00319   // We try, as we know them, one by one.
00320 
00321 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00322 #ifdef _AIX
00323   d->masterFd = ::open("/dev/ptc",O_RDWR);
00324 #else
00325   d->masterFd = ::open("/dev/ptmx",O_RDWR);
00326 #endif
00327   if (d->masterFd >= 0)
00328   {
00329     char *ptsn = ptsname(d->masterFd);
00330     if (ptsn) {
00331         grantpt(d->masterFd);
00332         d->ttyName = ptsn;
00333         goto gotpty;
00334     } else {
00335        ::close(d->masterFd);
00336        d->masterFd = -1;
00337     }
00338   }
00339 #endif
00340 
00341   // Linux device names, FIXME: Trouble on other systems?
00342   for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
00343   {
00344     for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
00345     {
00346       ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
00347       d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
00348 
00349       d->masterFd = ::open(ptyName.data(), O_RDWR);
00350       if (d->masterFd >= 0)
00351       {
00352 #ifdef __sun
00353         /* Need to check the process group of the pty.
00354          * If it exists, then the slave pty is in use,
00355          * and we need to get another one.
00356          */
00357         int pgrp_rtn;
00358         if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
00359           ::close(d->masterFd);
00360           d->masterFd = -1;
00361           continue;
00362         }
00363 #endif /* sun */
00364         if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
00365         {
00366           if (!geteuid())
00367           {
00368             struct group* p = getgrnam(TTY_GROUP);
00369             if (!p)
00370               p = getgrnam("wheel");
00371             gid_t gid = p ? p->gr_gid : getgid ();
00372 
00373             chown(d->ttyName.data(), getuid(), gid);
00374             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
00375           }
00376           goto gotpty;
00377         }
00378         ::close(d->masterFd);
00379         d->masterFd = -1;
00380       }
00381     }
00382   }
00383 
00384   kdWarning(175) << "Can't open a pseudo teletype" << endl;
00385   return false;
00386 
00387  gotpty:
00388   return  _attachPty(d->masterFd);
00389 
00390   return true;
00391 }
00392 
00393 void KPty::close()
00394 {
00395    if (d->masterFd < 0)
00396       return;
00397    // don't bother resetting unix98 pty, it will go away after closing master anyway.
00398    if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
00399       if (!geteuid()) {
00400          struct stat st;
00401          if (!stat(d->ttyName.data(), &st)) {
00402             chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
00403             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
00404          }
00405       } else {
00406          fcntl(d->masterFd, F_SETFD, 0);
00407          chownpty(false);
00408       }
00409    }
00410    ::close(d->slaveFd);
00411    ::close(d->masterFd);
00412    d->masterFd = d->slaveFd = -1;
00413 }
00414 
00415 void KPty::setCTty()
00416 {
00417     // Setup job control //////////////////////////////////
00418 
00419     // Become session leader, process group leader,
00420     // and get rid of the old controlling terminal.
00421     setsid();
00422 
00423     // make our slave pty the new controlling terminal.
00424 #ifdef TIOCSCTTY
00425     ioctl(d->slaveFd, TIOCSCTTY, 0);
00426 #else
00427     // SVR4 hack: the first tty opened after setsid() becomes controlling tty
00428     ::close(::open(d->ttyName, O_WRONLY, 0));
00429 #endif
00430 
00431     // make our new process group the foreground group on the pty
00432     int pgrp = getpid();
00433 #if defined(_POSIX_VERSION) || defined(__svr4__)
00434     tcsetpgrp (d->slaveFd, pgrp);
00435 #elif defined(TIOCSPGRP)
00436     ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
00437 #endif
00438 }
00439 
00440 void KPty::login(const char *user, const char *remotehost)
00441 {
00442 #ifdef HAVE_UTEMPTER
00443     KProcess_Utmp utmp;
00444     utmp.cmdFd = d->masterFd;
00445     utmp << "/usr/sbin/utempter" << "-a" << d->ttyName << "";
00446     utmp.start(KProcess::Block);
00447     Q_UNUSED(user);
00448     Q_UNUSED(remotehost);
00449 #elif defined(USE_LOGIN)
00450     const char *str_ptr;
00451     struct utmp l_struct;
00452     memset(&l_struct, 0, sizeof(struct utmp));
00453     // note: strncpy without terminators _is_ correct here. man 4 utmp
00454 
00455     if (user)
00456       strncpy(l_struct.ut_name, user, UT_NAMESIZE);
00457 
00458     if (remotehost)
00459       strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
00460 
00461 # ifndef __GLIBC__
00462     str_ptr = d->ttyName.data();
00463     if (!memcmp(str_ptr, "/dev/", 5))
00464         str_ptr += 5;
00465     strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
00466 # endif
00467 
00468     // Handle 64-bit time_t properly, where it may be larger
00469     // than the integral type of ut_time.
00470     {
00471         time_t ut_time_temp;
00472         time(&ut_time_temp);
00473         l_struct.ut_time=ut_time_temp;
00474     }
00475 
00476     ::login(&l_struct);
00477 #else
00478     Q_UNUSED(user);
00479     Q_UNUSED(remotehost);
00480 #endif
00481 }
00482 
00483 void KPty::logout()
00484 {
00485 #ifdef HAVE_UTEMPTER
00486     KProcess_Utmp utmp;
00487     utmp.cmdFd = d->masterFd;
00488     utmp << "/usr/sbin/utempter" << "-d" << d->ttyName;
00489     utmp.start(KProcess::Block);
00490 #elif defined(USE_LOGIN)
00491     const char *str_ptr = d->ttyName.data();
00492     if (!memcmp(str_ptr, "/dev/", 5))
00493         str_ptr += 5;
00494 # ifdef __GLIBC__
00495     else {
00496         const char *sl_ptr = strrchr(str_ptr, '/');
00497         if (sl_ptr)
00498             str_ptr = sl_ptr + 1;
00499     }
00500 # endif
00501     ::logout(str_ptr);
00502 #endif
00503 }
00504 
00505 void KPty::setWinSize(int lines, int columns)
00506 {
00507   d->winSize.ws_row = (unsigned short)lines;
00508   d->winSize.ws_col = (unsigned short)columns;
00509   if (d->masterFd >= 0)
00510     ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
00511 }
00512 
00513 void KPty::setXonXoff(bool useXonXoff)
00514 {
00515   d->xonXoff = useXonXoff;
00516   if (d->masterFd >= 0) {
00517     // without the '::' some version of HP-UX thinks, this declares
00518     // the struct in this class, in this method, and fails to find
00519     // the correct tc[gs]etattr
00520     struct ::termios ttmode;
00521 
00522     _tcgetattr(d->masterFd, &ttmode);
00523 
00524     if (!useXonXoff)
00525       ttmode.c_iflag &= ~(IXOFF | IXON);
00526     else
00527       ttmode.c_iflag |= (IXOFF | IXON);
00528 
00529     _tcsetattr(d->masterFd, &ttmode);
00530   }
00531 }
00532 
00533 void KPty::setUtf8Mode(bool useUtf8)
00534 {
00535   d->utf8 = useUtf8;
00536 #ifdef IUTF8
00537   if (d->masterFd >= 0) {
00538     // without the '::' some version of HP-UX thinks, this declares
00539     // the struct in this class, in this method, and fails to find
00540     // the correct tc[gs]etattr
00541     struct ::termios ttmode;
00542 
00543     _tcgetattr(d->masterFd, &ttmode);
00544 
00545     if (!useUtf8)
00546       ttmode.c_iflag &= ~IUTF8;
00547     else
00548       ttmode.c_iflag |= IUTF8;
00549 
00550     _tcsetattr(d->masterFd, &ttmode);
00551   }
00552 #endif
00553 }
00554 
00555 const char *KPty::ttyName() const
00556 {
00557     return d->ttyName.data();
00558 }
00559 
00560 int KPty::masterFd() const
00561 {
00562     return d->masterFd;
00563 }
00564 
00565 int KPty::slaveFd() const
00566 {
00567     return d->slaveFd;
00568 }
00569 
00570 // private
00571 bool KPty::chownpty(bool grant)
00572 {
00573   KProcess proc;
00574   proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << QString::number(d->masterFd);
00575   return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();
00576 }
00577 
KDE Home | KDE Accessibility Home | Description of Access Keys