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
|
--- src/sudosh.c.orig Tue Mar 22 21:05:05 2005
+++ src/sudosh.c Thu Mar 24 14:54:25 2005
@@ -24,6 +24,9 @@
#include <unistd.h>
#include <signal.h>
#include <pwd.h>
+#ifdef __FreeBSD__
+#include <sys/param.h>
+#endif
#include "config.h"
@@ -75,6 +78,12 @@
#define SIGCHLD SIGCLD
#endif
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <libutil.h>
+#endif
+
static struct termios termorig;
static struct winsize winorig;
@@ -105,6 +114,9 @@
static int findms(struct pst *);
void mysyslog(int, const char *, ...);
void mklogdir(void);
+#ifdef __FreeBSD__
+static void sanemode(int ttyfd);
+#endif
int main(int argc, char *argv[], char *environ[])
{
@@ -265,8 +277,9 @@
mysyslog(LOG_INFO, start_msg);
mysyslog(LOG_INFO, "to view this session type: sudosh-replay %s-%s-%i",
sudo_user, user_name, now);
-
+#ifndef __FreeBSD__
rawmode(0);
+#endif
if (findms(&pspair) < 0) {
perror("open pty failed");
@@ -283,6 +296,9 @@
default:
close(pspair.sfd);
}
+#ifdef __FreeBSD__
+ rawmode (0);
+#endif
setuid(getuid());
@@ -372,15 +388,30 @@
if ((p->mfd = open("/dev/ptmx", O_RDWR)) == -1) {
if ((p->mfd = open("/dev/ptc", O_RDWR)) == -1) {
- perror("Cannot open cloning master pty");
- return -1;
+#ifdef __FreeBSD__
+#define PTYLEN 16
+ if ((int)(sname= malloc(PTYLEN)) == -1) {
+ perror("Cannot allocate memory");
+ return -1;
+ }
+ if (openpty(&p->mfd, &p->sfd, sname, NULL, NULL) == -1) {
+#endif
+ perror("Cannot open cloning master pty");
+ return -1;
+#ifdef __FreeBSD__
+ }
+#endif
}
}
+#if !defined(__FreeBSD_version) || (defined(__FreeBSD_version) && __FreeBSD_version >= 500000)
(void) unlockpt(p->mfd);
(void) grantpt(p->mfd);
+#endif
+#ifndef __FreeBSD__
sname = (char *) ptsname(p->mfd);
+#endif
if ((p->sfd = open(sname, O_RDWR)) == -1) {
perror("open slave pty");
@@ -447,10 +478,46 @@
abort();
}
+#ifdef __FreeBSD__
+static void sanemode(int ttyfd)
+{
+ static struct termios termnew;
+
+ if (tcgetattr(ttyfd, &termnew) == -1) {
+ perror ("tcgetattr failed");
+ exit (1);
+ }
+ termnew.c_cflag = TTYDEF_CFLAG | (termnew.c_cflag & CLOCAL);
+ termnew.c_iflag = TTYDEF_IFLAG;
+ termnew.c_iflag |= ICRNL;
+ /* preserve user-preference flags in lflag */
+#define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
+ termnew.c_lflag = TTYDEF_LFLAG | (termnew.c_lflag & LKEEP);
+ termnew.c_oflag = TTYDEF_OFLAG;
+ if (tcsetattr(ttyfd, TCSAFLUSH, &termnew) == -1) {
+ perror ("tcsetattr (sane) failed");
+ exit (1);
+ }
+}
+#endif
+
static void rawmode(int ttyfd)
{
static struct termios termnew;
+#ifdef __FreeBSD__
+ if (tcgetattr(ttyfd, &termnew) == -1) {
+ perror ("tcgetattr failed");
+ exit (1);
+ }
+ cfmakeraw(&termnew);
+ termnew.c_cflag &= ~(CSIZE|PARENB);
+ termnew.c_cflag |= CS8;
+ if (tcsetattr(ttyfd, TCSADRAIN, &termnew) == -1) {
+ perror ("tcsetattr (raw) failed");
+ exit (1);
+ }
+#else __FreeBSD__
#ifdef TCGETS
if (ioctl(ttyfd, TCGETS, &termorig) == -1) {
perror("ioctl TCGETS failed");
@@ -472,12 +539,16 @@
#ifdef TCSETS
(void) ioctl(ttyfd, TCSETS, &termnew);
#endif
+#endif __FreeBSD__
}
static void bye(int signum)
{
#ifdef TCSETS
(void) ioctl(0, TCSETS, &termorig);
+#endif
+#ifdef __FreeBSD__
+ sanemode(0);
#endif
fclose(fttime);
|