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