1/*
2
3 This file is part of the KDE libraries
4 Copyright (C) 2002 Waldo Bastian <bastian@kde.org>
5 Copyright (C) 2002-2003,2007 Oswald Buddenhagen <ossi@kde.org>
6
7 Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23*/
24
25#include "kpty_p.h"
26
27#include <QtDebug>
28
29
30#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
31#define HAVE_LOGIN
32#define HAVE_LIBUTIL_H
33#endif
34
35#if defined(__OpenBSD__)
36#define HAVE_LOGIN
37#define HAVE_UTIL_H
38#endif
39
40#if defined(__APPLE__)
41#define HAVE_OPENPTY
42#define HAVE_UTIL_H
43#endif
44
45#ifdef __sgi
46#define __svr4__
47#endif
48
49#ifdef __osf__
50#define _OSF_SOURCE
51#include <float.h>
52#endif
53
54#ifdef _AIX
55#define _ALL_SOURCE
56#endif
57
58// __USE_XOPEN isn't defined by default in ICC
59// (needed for ptsname(), grantpt() and unlockpt())
60#ifdef __INTEL_COMPILER
61# ifndef __USE_XOPEN
62# define __USE_XOPEN
63# endif
64#endif
65
66#include <sys/types.h>
67#include <sys/ioctl.h>
68#include <sys/time.h>
69#include <sys/resource.h>
70#include <sys/stat.h>
71#include <sys/param.h>
72
73#include <errno.h>
74#include <fcntl.h>
75#include <time.h>
76#include <stdlib.h>
77#include <stdio.h>
78#include <string.h>
79#include <unistd.h>
80#include <grp.h>
81
82#if defined(HAVE_PTY_H)
83# include <pty.h>
84#endif
85
86#ifdef HAVE_LIBUTIL_H
87# include <libutil.h>
88#elif defined(HAVE_UTIL_H)
89# include <util.h>
90#endif
91
92#ifdef HAVE_UTEMPTER
93extern "C" {
94# include <utempter.h>
95}
96#else
97# include <utmp.h>
98# ifdef HAVE_UTMPX
99# include <utmpx.h>
100# endif
101# if !defined(_PATH_UTMPX) && defined(_UTMPX_FILE)
102# define _PATH_UTMPX _UTMPX_FILE
103# endif
104# ifdef HAVE_UPDWTMPX
105# if !defined(_PATH_WTMPX) && defined(_WTMPX_FILE)
106# define _PATH_WTMPX _WTMPX_FILE
107# endif
108# endif
109#endif
110
111/* for HP-UX (some versions) the extern C is needed, and for other
112 platforms it doesn't hurt */
113extern "C" {
114#include <termios.h>
115#if defined(HAVE_TERMIO_H)
116# include <termio.h> // struct winsize on some systems
117#endif
118}
119
120#if defined (_HPUX_SOURCE)
121# define _TERMIOS_INCLUDED
122# include <bsdtty.h>
123#endif
124
125#ifdef HAVE_SYS_STROPTS_H
126# include <sys/stropts.h> // Defines I_PUSH
127# define _NEW_TTY_CTRL
128#endif
129
130#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
131# define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
132#else
133# if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__) || defined(__GNU__)
134# define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
135# else
136# define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
137# endif
138#endif
139
140#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
141# define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
142#else
143# if defined(_HPUX_SOURCE) || defined(__CYGWIN__) || defined(__GNU__)
144# define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
145# else
146# define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
147# endif
148#endif
149
150//#include <kdebug.h>
151//#include <kstandarddirs.h> // findExe
152
153// not defined on HP-UX for example
154#ifndef CTRL
155# define CTRL(x) ((x) & 037)
156#endif
157
158#define TTY_GROUP "tty"
159
160///////////////////////
161// private functions //
162///////////////////////
163
164//////////////////
165// private data //
166//////////////////
167
168KPtyPrivate::KPtyPrivate(KPty* parent) :
169 masterFd(-1), slaveFd(-1), ownMaster(true), q_ptr(parent)
170{
171}
172
173KPtyPrivate::~KPtyPrivate()
174{
175}
176
177#ifndef HAVE_OPENPTY
178bool KPtyPrivate::chownpty(bool)
179{
180// return !QProcess::execute(KStandardDirs::findExe("kgrantpty"),
181// QStringList() << (grant?"--grant":"--revoke") << QString::number(masterFd));
182 return true;
183}
184#endif
185
186/////////////////////////////
187// public member functions //
188/////////////////////////////
189
190KPty::KPty() :
191 d_ptr(new KPtyPrivate(this))
192{
193}
194
195KPty::KPty(KPtyPrivate *d) :
196 d_ptr(d)
197{
198 d_ptr->q_ptr = this;
199}
200
201KPty::~KPty()
202{
203 close();
204 delete d_ptr;
205}
206
207bool KPty::open()
208{
209 Q_D(KPty);
210
211 if (d->masterFd >= 0)
212 return true;
213
214 d->ownMaster = true;
215
216 QByteArray ptyName;
217
218 // Find a master pty that we can open ////////////////////////////////
219
220 // Because not all the pty animals are created equal, they want to
221 // be opened by several different methods.
222
223 // We try, as we know them, one by one.
224
225#ifdef HAVE_OPENPTY
226
227 char ptsn[PATH_MAX];
228 if (::openpty( &d->masterFd, &d->slaveFd, ptsn, 0, 0)) {
229 d->masterFd = -1;
230 d->slaveFd = -1;
231 qWarning() << "Can't open a pseudo teletype";
232 return false;
233 }
234 d->ttyName = ptsn;
235
236#else
237
238#ifdef HAVE__GETPTY // irix
239
240 char *ptsn = _getpty(&d->masterFd, O_RDWR|O_NOCTTY, S_IRUSR|S_IWUSR, 0);
241 if (ptsn) {
242 d->ttyName = ptsn;
243 goto grantedpt;
244 }
245
246#elif defined(HAVE_PTSNAME) || defined(TIOCGPTN)
247
248#ifdef HAVE_POSIX_OPENPT
249 d->masterFd = ::posix_openpt(O_RDWR|O_NOCTTY);
250#elif defined(HAVE_GETPT)
251 d->masterFd = ::getpt();
252#elif defined(PTM_DEVICE)
253 d->masterFd = ::open(PTM_DEVICE, O_RDWR|O_NOCTTY);
254#else
255# error No method to open a PTY master detected.
256#endif
257 if (d->masterFd >= 0) {
258#ifdef HAVE_PTSNAME
259 char *ptsn = ptsname(d->masterFd);
260 if (ptsn) {
261 d->ttyName = ptsn;
262#else
263 int ptyno;
264 if (!ioctl(d->masterFd, TIOCGPTN, &ptyno)) {
265 d->ttyName = QByteArray("/dev/pts/") + QByteArray::number(ptyno);
266#endif
267#ifdef HAVE_GRANTPT
268 if (!grantpt(d->masterFd)) {
269 goto grantedpt;
270 }
271#else
272
273 goto gotpty;
274#endif
275 }
276 ::close(d->masterFd);
277 d->masterFd = -1;
278 }
279#endif // HAVE_PTSNAME || TIOCGPTN
280
281 // Linux device names, FIXME: Trouble on other systems?
282 for (const char * s3 = "pqrstuvwxyzabcde"; *s3; s3++) {
283 for (const char * s4 = "0123456789abcdef"; *s4; s4++) {
284 ptyName = QString().asprintf("/dev/pty%c%c", *s3, *s4).toUtf8();
285 d->ttyName = QString().asprintf("/dev/tty%c%c", *s3, *s4).toUtf8();
286
287 d->masterFd = ::open(ptyName.data(), O_RDWR);
288 if (d->masterFd >= 0) {
289#ifdef Q_OS_SOLARIS
290 /* Need to check the process group of the pty.
291 * If it exists, then the slave pty is in use,
292 * and we need to get another one.
293 */
294 int pgrp_rtn;
295 if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
296 ::close(d->masterFd);
297 d->masterFd = -1;
298 continue;
299 }
300#endif /* Q_OS_SOLARIS */
301 if (!access(d->ttyName.data(),R_OK|W_OK)) { // checks availability based on permission bits
302 if (!geteuid()) {
303 struct group * p = getgrnam(TTY_GROUP);
304 if (!p) {
305 p = getgrnam("wheel");
306 }
307 gid_t gid = p ? p->gr_gid : getgid ();
308
309 if (!chown(d->ttyName.data(), getuid(), gid)) {
310 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
311 }
312 }
313 goto gotpty;
314 }
315 ::close(d->masterFd);
316 d->masterFd = -1;
317 }
318 }
319 }
320
321 qWarning() << "Can't open a pseudo teletype";
322 return false;
323
324gotpty:
325 struct stat st;
326 if (stat(d->ttyName.data(), &st)) {
327 return false; // this just cannot happen ... *cough* Yeah right, I just
328 // had it happen when pty #349 was allocated. I guess
329 // there was some sort of leak? I only had a few open.
330 }
331 if (((st.st_uid != getuid()) ||
332 (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
333 !d->chownpty(true)) {
334 qWarning()
335 << "chownpty failed for device " << ptyName << "::" << d->ttyName
336 << "\nThis means the communication can be eavesdropped." << endl;
337 }
338
339#if defined (HAVE__GETPTY) || defined (HAVE_GRANTPT)
340grantedpt:
341#endif
342
343#ifdef HAVE_REVOKE
344 revoke(d->ttyName.data());
345#endif
346
347#ifdef HAVE_UNLOCKPT
348 unlockpt(d->masterFd);
349#elif defined(TIOCSPTLCK)
350 int flag = 0;
351 ioctl(d->masterFd, TIOCSPTLCK, &flag);
352#endif
353
354 d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
355 if (d->slaveFd < 0) {
356 qWarning() << "Can't open slave pseudo teletype";
357 ::close(d->masterFd);
358 d->masterFd = -1;
359 return false;
360 }
361
362#if (defined(__svr4__) || defined(__sgi__))
363 // Solaris
364 ioctl(d->slaveFd, I_PUSH, "ptem");
365 ioctl(d->slaveFd, I_PUSH, "ldterm");
366#endif
367
368#endif /* HAVE_OPENPTY */
369
370 fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
371 fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
372
373 return true;
374}
375
376bool KPty::open(int fd)
377{
378#if !defined(HAVE_PTSNAME) && !defined(TIOCGPTN)
379 qWarning() << "Unsupported attempt to open pty with fd" << fd;
380 return false;
381#else
382 Q_D(KPty);
383
384 if (d->masterFd >= 0) {
385 qWarning() << "Attempting to open an already open pty";
386 return false;
387 }
388
389 d->ownMaster = false;
390
391# ifdef HAVE_PTSNAME
392 char *ptsn = ptsname(fd);
393 if (ptsn) {
394 d->ttyName = ptsn;
395# else
396 int ptyno;
397 if (!ioctl(fd, TIOCGPTN, &ptyno)) {
398 char buf[32];
399 sprintf(buf, "/dev/pts/%d", ptyno);
400 d->ttyName = buf;
401# endif
402 } else {
403 qWarning() << "Failed to determine pty slave device for fd" << fd;
404 return false;
405 }
406
407 d->masterFd = fd;
408 if (!openSlave()) {
409
410 d->masterFd = -1;
411 return false;
412 }
413
414 return true;
415#endif
416}
417
418void KPty::closeSlave()
419{
420 Q_D(KPty);
421
422 if (d->slaveFd < 0) {
423 return;
424 }
425 ::close(d->slaveFd);
426 d->slaveFd = -1;
427}
428
429bool KPty::openSlave()
430{
431 Q_D(KPty);
432
433 if (d->slaveFd >= 0)
434 return true;
435 if (d->masterFd < 0) {
436 qDebug() << "Attempting to open pty slave while master is closed";
437 return false;
438 }
439 //d->slaveFd = KDE_open(d->ttyName.data(), O_RDWR | O_NOCTTY);
440 d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
441 if (d->slaveFd < 0) {
442 qDebug() << "Can't open slave pseudo teletype";
443 return false;
444 }
445 fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
446 return true;
447}
448
449void KPty::close()
450{
451 Q_D(KPty);
452
453 if (d->masterFd < 0) {
454 return;
455 }
456 closeSlave();
457 // don't bother resetting unix98 pty, it will go away after closing master anyway.
458 if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
459 if (!geteuid()) {
460 struct stat st;
461 if (!stat(d->ttyName.data(), &st)) {
462 chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
463 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
464 }
465 } else {
466 fcntl(d->masterFd, F_SETFD, 0);
467 d->chownpty(false);
468 }
469 }
470 ::close(d->masterFd);
471 d->masterFd = -1;
472}
473
474void KPty::setCTty()
475{
476 Q_D(KPty);
477
478 // Setup job control //////////////////////////////////
479
480 // Become session leader, process group leader,
481 // and get rid of the old controlling terminal.
482 setsid();
483
484 // make our slave pty the new controlling terminal.
485#ifdef TIOCSCTTY
486 ioctl(d->slaveFd, TIOCSCTTY, 0);
487#else
488 // __svr4__ hack: the first tty opened after setsid() becomes controlling tty
489 ::close(::open(d->ttyName, O_WRONLY, 0));
490#endif
491
492 // make our new process group the foreground group on the pty
493 int pgrp = getpid();
494#if defined(_POSIX_VERSION) || defined(__svr4__)
495 tcsetpgrp(d->slaveFd, pgrp);
496#elif defined(TIOCSPGRP)
497 ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
498#endif
499}
500
501void KPty::login(const char * user, const char * remotehost)
502{
503#ifdef HAVE_UTEMPTER
504 Q_D(KPty);
505
506 addToUtmp(d->ttyName.constData(), remotehost, d->masterFd);
507 Q_UNUSED(user);
508#else
509# ifdef HAVE_UTMPX
510 struct utmpx l_struct;
511# else
512 struct utmp l_struct;
513# endif
514 memset(&l_struct, 0, sizeof(l_struct));
515 // note: strncpy without terminators _is_ correct here. man 4 utmp
516
517 if (user) {
518# ifdef HAVE_UTMPX
519 strncpy(l_struct.ut_user, user, sizeof(l_struct.ut_user));
520# else
521 strncpy(l_struct.ut_name, user, sizeof(l_struct.ut_name));
522# endif
523 }
524
525 if (remotehost) {
526 strncpy(l_struct.ut_host, remotehost, sizeof(l_struct.ut_host));
527# ifdef HAVE_STRUCT_UTMP_UT_SYSLEN
528 l_struct.ut_syslen = qMin(strlen(remotehost), sizeof(l_struct.ut_host));
529# endif
530 }
531
532# ifndef __GLIBC__
533 Q_D(KPty);
534 const char * str_ptr = d->ttyName.data();
535 if (!memcmp(str_ptr, "/dev/", 5)) {
536 str_ptr += 5;
537 }
538 strncpy(l_struct.ut_line, str_ptr, sizeof(l_struct.ut_line));
539# ifdef HAVE_STRUCT_UTMP_UT_ID
540 strncpy(l_struct.ut_id,
541 str_ptr + strlen(str_ptr) - sizeof(l_struct.ut_id),
542 sizeof(l_struct.ut_id));
543# endif
544# endif
545
546# ifdef HAVE_UTMPX
547 gettimeofday(&l_struct.ut_tv, 0);
548# else
549 l_struct.ut_time = time(0);
550# endif
551
552# ifdef HAVE_LOGIN
553# ifdef HAVE_LOGINX
554 ::loginx(&l_struct);
555# else
556 ::login(&l_struct);
557# endif
558# else
559# ifdef HAVE_STRUCT_UTMP_UT_TYPE
560 l_struct.ut_type = USER_PROCESS;
561# endif
562# ifdef HAVE_STRUCT_UTMP_UT_PID
563 l_struct.ut_pid = getpid();
564# ifdef HAVE_STRUCT_UTMP_UT_SESSION
565 l_struct.ut_session = getsid(0);
566# endif
567# endif
568# ifdef HAVE_UTMPX
569 utmpxname(_PATH_UTMPX);
570 setutxent();
571 pututxline(&l_struct);
572 endutxent();
573# ifdef HAVE_UPDWTMPX
574 updwtmpx(_PATH_WTMPX, &l_struct);
575# endif
576# else
577 utmpname(_PATH_UTMP);
578 setutent();
579 pututline(&l_struct);
580 endutent();
581 updwtmp(_PATH_WTMP, &l_struct);
582# endif
583# endif
584#endif
585}
586
587void KPty::logout()
588{
589#ifdef HAVE_UTEMPTER
590 Q_D(KPty);
591
592 removeLineFromUtmp(d->ttyName.constData(), d->masterFd);
593#else
594 Q_D(KPty);
595
596 const char *str_ptr = d->ttyName.data();
597 if (!memcmp(str_ptr, "/dev/", 5)) {
598 str_ptr += 5;
599 }
600# ifdef __GLIBC__
601 else {
602 const char * sl_ptr = strrchr(str_ptr, '/');
603 if (sl_ptr) {
604 str_ptr = sl_ptr + 1;
605 }
606 }
607# endif
608# ifdef HAVE_LOGIN
609# ifdef HAVE_LOGINX
610 ::logoutx(str_ptr, 0, DEAD_PROCESS);
611# else
612 ::logout(str_ptr);
613# endif
614# else
615# ifdef HAVE_UTMPX
616 struct utmpx l_struct, *ut;
617# else
618 struct utmp l_struct, *ut;
619# endif
620 memset(&l_struct, 0, sizeof(l_struct));
621
622 strncpy(l_struct.ut_line, str_ptr, sizeof(l_struct.ut_line));
623
624# ifdef HAVE_UTMPX
625 utmpxname(_PATH_UTMPX);
626 setutxent();
627 if ((ut = getutxline(&l_struct))) {
628# else
629 utmpname(_PATH_UTMP);
630 setutent();
631 if ((ut = getutline(&l_struct))) {
632# endif
633# ifdef HAVE_UTMPX
634 memset(ut->ut_user, 0, sizeof(*ut->ut_user));
635# else
636 memset(ut->ut_name, 0, sizeof(*ut->ut_name));
637# endif
638 memset(ut->ut_host, 0, sizeof(*ut->ut_host));
639# ifdef HAVE_STRUCT_UTMP_UT_SYSLEN
640 ut->ut_syslen = 0;
641# endif
642# ifdef HAVE_STRUCT_UTMP_UT_TYPE
643 ut->ut_type = DEAD_PROCESS;
644# endif
645# ifdef HAVE_UTMPX
646 gettimeofday(&ut->ut_tv, 0);
647 pututxline(ut);
648 }
649 endutxent();
650# else
651 ut->ut_time = time(0);
652 pututline(ut);
653}
654endutent();
655# endif
656# endif
657#endif
658}
659
660// XXX Supposedly, tc[gs]etattr do not work with the master on Solaris.
661// Please verify.
662
663bool KPty::tcGetAttr(struct ::termios * ttmode) const
664{
665 Q_D(const KPty);
666
667 return _tcgetattr(d->masterFd, ttmode) == 0;
668}
669
670bool KPty::tcSetAttr(struct ::termios * ttmode)
671{
672 Q_D(KPty);
673
674 return _tcsetattr(d->masterFd, ttmode) == 0;
675}
676
677bool KPty::setWinSize(int lines, int columns)
678{
679 Q_D(KPty);
680
681 struct winsize winSize;
682 memset(&winSize, 0, sizeof(winSize));
683 winSize.ws_row = (unsigned short)lines;
684 winSize.ws_col = (unsigned short)columns;
685 return ioctl(d->masterFd, TIOCSWINSZ, (char *)&winSize) == 0;
686}
687
688bool KPty::setEcho(bool echo)
689{
690 struct ::termios ttmode;
691 if (!tcGetAttr(&ttmode)) {
692 return false;
693 }
694 if (!echo) {
695 ttmode.c_lflag &= ~ECHO;
696 } else {
697 ttmode.c_lflag |= ECHO;
698 }
699 return tcSetAttr(&ttmode);
700}
701
702const char * KPty::ttyName() const
703{
704 Q_D(const KPty);
705
706 return d->ttyName.data();
707}
708
709int KPty::masterFd() const
710{
711 Q_D(const KPty);
712
713 return d->masterFd;
714}
715
716int KPty::slaveFd() const
717{
718 Q_D(const KPty);
719
720 return d->slaveFd;
721}
722