File: pty.c

package info (click to toggle)
twin 0.4.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,804 kB
  • ctags: 23,904
  • sloc: ansic: 61,860; cpp: 1,023; makefile: 777; sh: 552; lex: 302; yacc: 231
file content (213 lines) | stat: -rw-r--r-- 4,618 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *  parts of code taken from:
 *
 *  rxvt-2.4.7 sources (various authors)
 *  available at ftp://ftp.math.fu-berlin.de/pub/rxvt/
 *  and usual Linux mirrors
 *
 *
 * other parts from Twin autor:
 *
 *  Copyright (C) 1993-1999  Massimiliano Ghilardi
 *
 */

#include <grp.h>
#include <signal.h>
#include <sys/stat.h>

#ifdef CONF_TERM_DEVPTS
# define __USE_XOPEN
# include <stdlib.h>
#endif

#include "tty_ioctl.h"

#include "twin.h"
#include "main.h"
#include "printk.h"
#include "util.h"

/* pseudo-teletype connections handling functions */

/* 0. variables */
static char *ptydev, *ttydev;
static int ptyfd, ttyfd;

/* 1. Acquire a pseudo-teletype from the system. */
/*
 * On failure, returns FALSE.
 * On success, fills ttydev and ptydev with the names of the
 * master and slave parts and sets ptyfd to the pty file descriptor
 */
static byte get_pty(void)
{
    int fd = -1, sfd = -1;
#ifdef CONF_TERM_DEVPTS
    
    /* open master pty /dev/ptmx */
    if ((fd = open("/dev/ptmx", O_RDWR|O_NOCTTY)) >= 0) {
	if (grantpt(fd) == 0 && unlockpt(fd) == 0) {
	    ptydev = ttydev = ptsname(fd);
	    if ((sfd = open(ptydev, O_RDWR|O_NOCTTY)) >= 0)
		goto Found;
	}
	close(fd);
    }
#else
    static char     pty_name[] = "/dev/pty??";
    static char     tty_name[] = "/dev/tty??";
    int             len = strlen(tty_name);
    char           *c1, *c2;

    ptydev = pty_name;
    ttydev = tty_name;

# define PTYCHAR1	"pqrstuvwxyzabcde"
# define PTYCHAR2	"0123456789abcdef"
    for (c1 = PTYCHAR1; *c1; c1++) {
	ptydev[len - 2] = ttydev[len - 2] = *c1;
	for (c2 = PTYCHAR2; *c2; c2++) {
	    ptydev[len - 1] = ttydev[len - 1] = *c2;
	    if ((fd = open(ptydev, O_RDWR|O_NOCTTY)) >= 0) {
		if ((sfd = open(ttydev, O_RDWR|O_NOCTTY)) >= 0)
		    /* access(ttydev, R_OK|W_OK) won't do as it checks against REAL uid */
		    goto Found;
		close(fd);
	    }
	}
    }
#endif
    return FALSE;

Found:
    fcntl(fd, F_SETFL, O_NDELAY);
    fcntl(fd, F_SETFD, FD_CLOEXEC);
    ttyfd = sfd;
    ptyfd = fd;
    return TRUE;
}

/* 2. Fixup permission for pty master/slave pairs */
static byte fixup_pty(void) {
    /* from util.c */
    extern gid_t get_tty_grgid(void);
    
    uid_t id = getuid();
    gid_t tty_gid = get_tty_grgid();
    
    if (tty_gid != (gid_t)-1 &&
#ifndef CONF_TERM_DEVPTS
	chown(ptydev, id, 0) == 0 && chmod(ptydev, 0600) == 0 &&
#endif
	chown(ttydev, id, tty_gid) == 0 && chmod(ttydev, 0620) == 0)
	return TRUE;
    return FALSE;
}

/* 3. Setup tty size and termios settings */
/*
 * do it before the fork() and NOT in the child to avoid
 * races with future tty resizes performed by the parent!
 */
static byte setup_tty(udat x, udat y) {
    struct winsize wsiz;
    /* from hw.c, ttysave is the console original state */
    extern struct termios ttysave;
    
    wsiz.ws_col = x;
    wsiz.ws_row = y;
    wsiz.ws_xpixel = 0;
    wsiz.ws_ypixel = 0;
    
    return ioctl(ptyfd, TIOCSWINSZ, &wsiz) >= 0 &&
	tty_setioctl(ttyfd, &ttysave) >= 0;
}

/* 4. Establish ttyfd as controlling teletype for new session and switch to it */
static byte switchto_tty(void)
{
    int i;
    pid_t pid;

    pid = setsid();
    if (pid < 0)
	return FALSE;

    /*
     * Hope all other file descriptors are set to fcntl(fd, F_SETFD, FD_CLOEXEC)
     */
    for (i=0; i<=2; i++) {
	if (i != ttyfd) {
	    close(i);
	    dup2(ttyfd, i);
	}
    }
    if (ttyfd > 2)
	close(ttyfd);

#ifdef TIOCSCTTY
    ioctl(0, TIOCSCTTY, 0);
#endif

/* set process group */
#if defined (_POSIX_VERSION) || defined (__svr4__)
    tcsetpgrp(0, pid);
#elif defined (TIOCSPGRP)
    ioctl(0, TIOCSPGRP, &pid);
#endif

    return TRUE;
}

/* exported API: fork() a program in a pseudo-teletype */
byte SpawnInWindow(window Window, CONST byte *arg0, byte * CONST *argv) {
    pid_t childpid;
    remotedata *data;

    /* 0 */
    if (flag_secure) {
	printk(flag_secure_msg);
	return FALSE;
    }
    GainPrivileges();
    
    /* 1 */
    if (!get_pty()) {
	DropPrivileges();
	return FALSE;
    }
    /* 2 */
    (void)fixup_pty();
    DropPrivileges();

    /* 3 */
    if (setup_tty(Window->USE.C.TtyData->SizeX, Window->USE.C.TtyData->SizeY)) {
	switch ((childpid = fork())) {
	  case -1:
	    /* failed */
	    close(ptyfd);
	    ptyfd = -1;
	    break;
	  case 0:
	    /* child */
	    /* 4 */
	    if (switchto_tty())
		execvp((CONST char *)arg0, (char * CONST *)argv);
	    exit(1);
	    break;
	  default:
	    /* father */
	    data = &Window->RemoteData;
	    data->Fd = ptyfd;
	    data->ChildPid = childpid;
	    break;
	}
    } else {
	close(ptyfd);
	ptyfd = -1;
    }
    close(ttyfd);
    return ptyfd != -1;
}