00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00043
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>
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
00085
00086 extern "C" {
00087 # include <termios.h>
00088 }
00089 #endif
00090
00091 #if !defined(__osf__)
00092 # ifdef HAVE_TERMIO_H
00093
00094 # include <termio.h>
00095 # endif
00096 #endif
00097
00098 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00099 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
00100 #else
00101 # if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__)
00102 # define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
00103 # else
00104 # define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
00105 # endif
00106 #endif
00107
00108 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00109 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
00110 #else
00111 # if defined(_HPUX_SOURCE) || defined(__CYGWIN__)
00112 # define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
00113 # else
00114 # define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
00115 # endif
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>
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
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
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
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::open()
00205 {
00206 if (d->masterFd >= 0)
00207 return true;
00208
00209 QCString ptyName;
00210
00211
00212
00213
00214
00215
00216
00217
00218 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00219 #ifdef _AIX
00220 d->masterFd = ::open("/dev/ptc",O_RDWR);
00221 #else
00222 d->masterFd = ::open("/dev/ptmx",O_RDWR);
00223 #endif
00224 if (d->masterFd >= 0)
00225 {
00226 char *ptsn = ptsname(d->masterFd);
00227 if (ptsn) {
00228 grantpt(d->masterFd);
00229 d->ttyName = ptsn;
00230 goto gotpty;
00231 } else {
00232 ::close(d->masterFd);
00233 d->masterFd = -1;
00234 }
00235 }
00236 #endif
00237
00238
00239 for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
00240 {
00241 for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
00242 {
00243 ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
00244 d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
00245
00246 d->masterFd = ::open(ptyName.data(), O_RDWR);
00247 if (d->masterFd >= 0)
00248 {
00249 #ifdef __sun
00250
00251
00252
00253
00254 int pgrp_rtn;
00255 if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
00256 ::close(d->masterFd);
00257 d->masterFd = -1;
00258 continue;
00259 }
00260 #endif
00261 if (!access(d->ttyName.data(),R_OK|W_OK))
00262 {
00263 if (!geteuid())
00264 {
00265 struct group* p = getgrnam(TTY_GROUP);
00266 if (!p)
00267 p = getgrnam("wheel");
00268 gid_t gid = p ? p->gr_gid : getgid ();
00269
00270 chown(d->ttyName.data(), getuid(), gid);
00271 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
00272 }
00273 goto gotpty;
00274 }
00275 ::close(d->masterFd);
00276 d->masterFd = -1;
00277 }
00278 }
00279 }
00280
00281 kdWarning(175) << "Can't open a pseudo teletype" << endl;
00282 return false;
00283
00284 gotpty:
00285 struct stat st;
00286 if (stat(d->ttyName.data(), &st))
00287 return false;
00288
00289
00290 if (((st.st_uid != getuid()) ||
00291 (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
00292 !chownpty(true))
00293 {
00294 kdWarning(175)
00295 << "chownpty failed for device " << ptyName << "::" << d->ttyName
00296 << "\nThis means the communication can be eavesdropped." << endl;
00297 }
00298
00299 #ifdef BSD
00300 revoke(d->ttyName.data());
00301 #endif
00302
00303 #ifdef HAVE_UNLOCKPT
00304 unlockpt(d->masterFd);
00305 #endif
00306
00307 d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
00308 if (d->slaveFd < 0)
00309 {
00310 kdWarning(175) << "Can't open slave pseudo teletype" << endl;
00311 ::close(d->masterFd);
00312 d->masterFd = -1;
00313 return false;
00314 }
00315
00316 #if (defined(__svr4__) || defined(__sgi__))
00317
00318 ioctl(d->slaveFd, I_PUSH, "ptem");
00319 ioctl(d->slaveFd, I_PUSH, "ldterm");
00320 #endif
00321
00322
00323
00324
00325
00326 struct ::termios ttmode;
00327
00328 _tcgetattr(d->slaveFd, &ttmode);
00329
00330 if (!d->xonXoff)
00331 ttmode.c_iflag &= ~(IXOFF | IXON);
00332 else
00333 ttmode.c_iflag |= (IXOFF | IXON);
00334
00335 #ifdef IUTF8
00336 if (!d->utf8)
00337 ttmode.c_iflag &= ~IUTF8;
00338 else
00339 ttmode.c_iflag |= IUTF8;
00340 #endif
00341
00342 ttmode.c_cc[VINTR] = CINTR;
00343 ttmode.c_cc[VQUIT] = CQUIT;
00344 ttmode.c_cc[VERASE] = CERASE;
00345
00346 _tcsetattr(d->slaveFd, &ttmode);
00347
00348
00349 ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00350
00351 fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
00352 fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
00353
00354 return true;
00355 }
00356
00357 void KPty::close()
00358 {
00359 if (d->masterFd < 0)
00360 return;
00361
00362 if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
00363 if (!geteuid()) {
00364 struct stat st;
00365 if (!stat(d->ttyName.data(), &st)) {
00366 chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
00367 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
00368 }
00369 } else {
00370 fcntl(d->masterFd, F_SETFD, 0);
00371 chownpty(false);
00372 }
00373 }
00374 ::close(d->slaveFd);
00375 ::close(d->masterFd);
00376 d->masterFd = d->slaveFd = -1;
00377 }
00378
00379 void KPty::setCTty()
00380 {
00381
00382
00383
00384
00385 setsid();
00386
00387
00388 #ifdef TIOCSCTTY
00389 ioctl(d->slaveFd, TIOCSCTTY, 0);
00390 #else
00391
00392 ::close(::open(d->ttyName, O_WRONLY, 0));
00393 #endif
00394
00395
00396 int pgrp = getpid();
00397 #if defined(_POSIX_VERSION) || defined(__svr4__)
00398 tcsetpgrp (d->slaveFd, pgrp);
00399 #elif defined(TIOCSPGRP)
00400 ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
00401 #endif
00402 }
00403
00404 void KPty::login(const char *user, const char *remotehost)
00405 {
00406 #ifdef HAVE_UTEMPTER
00407 KProcess_Utmp utmp;
00408 utmp.cmdFd = d->masterFd;
00409 utmp << "/usr/sbin/utempter" << "-a" << d->ttyName << "";
00410 utmp.start(KProcess::Block);
00411 Q_UNUSED(user);
00412 Q_UNUSED(remotehost);
00413 #elif defined(USE_LOGIN)
00414 const char *str_ptr;
00415 struct utmp l_struct;
00416 memset(&l_struct, 0, sizeof(struct utmp));
00417
00418
00419 if (user)
00420 strncpy(l_struct.ut_name, user, UT_NAMESIZE);
00421
00422 if (remotehost)
00423 strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
00424
00425 # ifndef __GLIBC__
00426 str_ptr = d->ttyName.data();
00427 if (!memcmp(str_ptr, "/dev/", 5))
00428 str_ptr += 5;
00429 strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
00430 # endif
00431
00432
00433
00434 {
00435 time_t ut_time_temp;
00436 time(&ut_time_temp);
00437 l_struct.ut_time=ut_time_temp;
00438 }
00439
00440 ::login(&l_struct);
00441 #else
00442 Q_UNUSED(user);
00443 Q_UNUSED(remotehost);
00444 #endif
00445 }
00446
00447 void KPty::logout()
00448 {
00449 #ifdef HAVE_UTEMPTER
00450 KProcess_Utmp utmp;
00451 utmp.cmdFd = d->masterFd;
00452 utmp << "/usr/sbin/utempter" << "-d" << d->ttyName;
00453 utmp.start(KProcess::Block);
00454 #elif defined(USE_LOGIN)
00455 const char *str_ptr = d->ttyName.data();
00456 if (!memcmp(str_ptr, "/dev/", 5))
00457 str_ptr += 5;
00458 # ifdef __GLIBC__
00459 else {
00460 const char *sl_ptr = strrchr(str_ptr, '/');
00461 if (sl_ptr)
00462 str_ptr = sl_ptr + 1;
00463 }
00464 # endif
00465 ::logout(str_ptr);
00466 #endif
00467 }
00468
00469 void KPty::setWinSize(int lines, int columns)
00470 {
00471 d->winSize.ws_row = (unsigned short)lines;
00472 d->winSize.ws_col = (unsigned short)columns;
00473 if (d->masterFd >= 0)
00474 ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
00475 }
00476
00477 void KPty::setXonXoff(bool useXonXoff)
00478 {
00479 d->xonXoff = useXonXoff;
00480 if (d->masterFd >= 0) {
00481
00482
00483
00484 struct ::termios ttmode;
00485
00486 _tcgetattr(d->masterFd, &ttmode);
00487
00488 if (!useXonXoff)
00489 ttmode.c_iflag &= ~(IXOFF | IXON);
00490 else
00491 ttmode.c_iflag |= (IXOFF | IXON);
00492
00493 _tcsetattr(d->masterFd, &ttmode);
00494 }
00495 }
00496
00497 void KPty::setUtf8Mode(bool useUtf8)
00498 {
00499 d->utf8 = useUtf8;
00500 #ifdef IUTF8
00501 if (d->masterFd >= 0) {
00502
00503
00504
00505 struct ::termios ttmode;
00506
00507 _tcgetattr(d->masterFd, &ttmode);
00508
00509 if (!useUtf8)
00510 ttmode.c_iflag &= ~IUTF8;
00511 else
00512 ttmode.c_iflag |= IUTF8;
00513
00514 _tcsetattr(d->masterFd, &ttmode);
00515 }
00516 #endif
00517 }
00518
00519 const char *KPty::ttyName() const
00520 {
00521 return d->ttyName.data();
00522 }
00523
00524 int KPty::masterFd() const
00525 {
00526 return d->masterFd;
00527 }
00528
00529 int KPty::slaveFd() const
00530 {
00531 return d->slaveFd;
00532 }
00533
00534
00535 bool KPty::chownpty(bool grant)
00536 {
00537 KProcess proc;
00538 proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << QString::number(d->masterFd);
00539 return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();
00540 }
00541