Diff

Differences From Artifact [ba3da51d17]:

To Artifact [83f84a530b]:


1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED
#include <features.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>

#include "putty.h"

#ifndef FALSE











>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED
#include <features.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>

#include "putty.h"

#ifndef FALSE
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
 */
static char *pty_init(char *host, int port, char **realhost, int nodelay)
{
    int slavefd;
    char name[FILENAME_MAX];
    pid_t pid, pgrp;





























    pty_master_fd = open("/dev/ptmx", O_RDWR);

    if (pty_master_fd < 0) {
	perror("/dev/ptmx: open");
	exit(1);
    }

    if (grantpt(pty_master_fd) < 0) {
	perror("grantpt");
	exit(1);
    }
    
    if (unlockpt(pty_master_fd) < 0) {
	perror("unlockpt");
	exit(1);
    }

    name[FILENAME_MAX-1] = '\0';
    strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);


    /*
     * Fork and execute the command.
     */
    pid = fork();
    if (pid < 0) {
	perror("fork");







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



















>







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
 */
static char *pty_init(char *host, int port, char **realhost, int nodelay)
{
    int slavefd;
    char name[FILENAME_MAX];
    pid_t pid, pgrp;

#ifdef BSD_PTYS
    {
	const char chars1[] = "pqrstuvwxyz";
	const char chars2[] = "0123456789abcdef";
	const char *p1, *p2;
	char master_name[20];

	for (p1 = chars1; *p1; p1++)
	    for (p2 = chars2; *p2; p2++) {
		sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
		pty_master_fd = open(master_name, O_RDWR);
		if (pty_master_fd >= 0) {
		    if (geteuid() == 0 ||
			access(master_name, R_OK | W_OK) == 0)
			goto got_one;
		    close(pty_master_fd);
		}
	    }

	/* If we get here, we couldn't get a tty at all. */
	fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
	exit(1);

	got_one:
	strcpy(name, master_name);
	name[5] = 't';		       /* /dev/ptyXX -> /dev/ttyXX */
    }
#else
    pty_master_fd = open("/dev/ptmx", O_RDWR);

    if (pty_master_fd < 0) {
	perror("/dev/ptmx: open");
	exit(1);
    }

    if (grantpt(pty_master_fd) < 0) {
	perror("grantpt");
	exit(1);
    }
    
    if (unlockpt(pty_master_fd) < 0) {
	perror("unlockpt");
	exit(1);
    }

    name[FILENAME_MAX-1] = '\0';
    strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
#endif

    /*
     * Fork and execute the command.
     */
    pid = fork();
    if (pid < 0) {
	perror("fork");
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
	 */

	slavefd = open(name, O_RDWR);
	if (slavefd < 0) {
	    perror("slave pty: open");
	    exit(1);
	}










	close(pty_master_fd);
	close(0);
	close(1);
	close(2);
	fcntl(slavefd, F_SETFD, 0);    /* don't close on exec */
	dup2(slavefd, 0);
	dup2(slavefd, 1);
	dup2(slavefd, 2);
	setsid();
	ioctl(slavefd, TIOCSCTTY, 1);
	pgrp = getpid();
	tcsetpgrp(slavefd, pgrp);
	setpgrp();
	close(open(name, O_WRONLY, 0));
	setpgrp();



	/* Close everything _else_, for tidiness. */
	for (i = 3; i < 1024; i++)
	    close(i);
	{
	    char term_env_var[10 + sizeof(cfg.termtype)];
	    sprintf(term_env_var, "TERM=%s", cfg.termtype);
	    putenv(term_env_var);







>
>
>
>
>
>
>
>
>
















>
>
>







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
	 */

	slavefd = open(name, O_RDWR);
	if (slavefd < 0) {
	    perror("slave pty: open");
	    exit(1);
	}

#ifdef BSD_PTYS
	/* We need to chown/chmod the /dev/ttyXX device. */
	{
	    struct group *gp = getgrnam("tty");
	    fchown(slavefd, getuid(), gp ? gp->gr_gid : -1);
	    fchmod(slavefd, 0600);
	}
#endif

	close(pty_master_fd);
	close(0);
	close(1);
	close(2);
	fcntl(slavefd, F_SETFD, 0);    /* don't close on exec */
	dup2(slavefd, 0);
	dup2(slavefd, 1);
	dup2(slavefd, 2);
	setsid();
	ioctl(slavefd, TIOCSCTTY, 1);
	pgrp = getpid();
	tcsetpgrp(slavefd, pgrp);
	setpgrp();
	close(open(name, O_WRONLY, 0));
	setpgrp();
	/* In case we were setgid-utmp or setuid-root, drop privs. */
	setgid(getgid());
	setuid(getuid());
	/* Close everything _else_, for tidiness. */
	for (i = 3; i < 1024; i++)
	    close(i);
	{
	    char term_env_var[10 + sizeof(cfg.termtype)];
	    sprintf(term_env_var, "TERM=%s", cfg.termtype);
	    putenv(term_env_var);