-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.c
226 lines (197 loc) · 5.63 KB
/
init.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
** init.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:10:01 2000 Matthieu Lucotte
** Last update Sun Sep 21 23:36:59 2003
*/
#include "zssh.h"
int gl_master; /* ssh pty fd */
int gl_slave; /* ssh tty fd */
int gl_hook_master; /* hook pty fd */
int gl_hook_slave; /* hook tty fd */
int gl_main_pid;
volatile sig_atomic_t gl_child_output; /* pid of child handling output from the pty */
volatile sig_atomic_t gl_child_shell; /* pid of shell (ssh) */
volatile sig_atomic_t gl_child_rz; /* pid of child forked for use in the local shell */
volatile sig_atomic_t gl_child_read; /* pid of hook pty reader */
int gl_local_shell_mode;
volatile sig_atomic_t gl_interrupt;
volatile sig_atomic_t gl_repeat; /* repeat action forever */
int gl_force; /* don't ask user questions */
int gl_copty; /* attach hook to copty */
struct termios gl_tt; /* initial term */
struct termios gl_rtt; /* raw mode term */
struct termios gl_tt2; /* ssh mode term */
struct winsize gl_win;
sigset_t gl_sig_mask;
char gl_escape; /* gl_escape = 'X' -> escape seq is ^X */
char **gl_shav; /* remote shell argv, defaults to ssh -e none */
void init_gl(int ac, char **av)
{
gl_master = gl_slave = 0;
gl_main_pid = getpid();
gl_child_shell = gl_child_output = 0;
gl_child_rz = gl_child_read = 0;
gl_force = 0;
gl_copty = 0;
sigemptyset(&gl_sig_mask);
sigprocmask(SIG_SETMASK, &gl_sig_mask, 0);
gl_local_shell_mode = 0;
gl_interrupt = 0;
gl_escape = '@';
gl_shav = smalloc(4 * sizeof(char*));
gl_shav[0] = "ssh";
gl_shav[1] = "-e";
gl_shav[2] = "none";
gl_shav[3] = 0;
/* initialize gl_tt from stdin */
if (tcgetattr(0, &gl_tt) < 0)
error(0, "tcgetattr");
/* initialize gl_win from stdin */
if (ioctl(0, TIOCGWINSZ, (char *)&gl_win) < 0)
error(0, "ioctl TIOCGWINSZ");
gl_rtt = gl_tt;
my_cfmakeraw(&gl_rtt);
gl_tt2 = gl_rtt;
}
void version(int exit_prog)
{
printf(PACKAGE_STRING);
printf("\nCopyright (C) 2019-2021 Zero King <l2dy@users.sourceforge.net>");
printf("\nCopyright (C) 2001 Matthieu Lucotte <gounter@users.sourceforge.net>\n");
printf("zssh comes with ABSOLUTELY NO WARRANTY. Use at your own risk.\n");
printf("This is free software, and you are welcome to redistribute it\n");
printf("under certain conditions.\n");
printf("See the GNU General Public License for more details.\n");
if (exit_prog)
exit(0);
}
void usage(void)
{
printf("\
Usage: zssh2 [zssh options] [--] [ssh options]\n\
\n\
Options:\n\
--copty Attach hook program to copty (for special OSes only)\n\
\n\
-f Do not ask user any question\n\
--force \n\
\n\
-h This help\n\
--help \n\
\n\
-s cmd run cmd as remote shell instead of the\n\
--shell cmd default \"ssh -e none\" \n\
ex: zssh -s \"telnet -8 -E\" \n\
\n\
-V show version\n\
--version \n\
\n\
-z ^X set escape sequence to ^X\n\
--zssh-escape ^X \n\
\n\
'--' may be used to separate zssh options from ssh ones \n\
Other options are passed verbatim to ssh/telnet.\n\
See also zssh/ssh/telnet man pages for more details\n\
");
exit(1);
}
/* set escape key
* str : "^X"
* -> set gl_escape to X
*/
int set_escape(char *str)
{
if (!str || !str[0] || str[0] != '^') {
printf("Invalid escape sequence\n");
return -1;
}
gl_escape = str[1];
if ('a' <= gl_escape && gl_escape <= 'z')
gl_escape += 'A' - 'a';
return 0;
}
/* Returns the key to press to generate the esc sequence
* ^@ -> C-Space
* ^X -> C-x
*/
char *escape_help(void)
{
static char str[40];
if (gl_escape == '@')
sprintf(str, "C-Space");
else
sprintf(str, "C-%c", tolower(gl_escape));
return str;
}
void command_line_options(int *argc, char ***argv)
{
int ac = *argc;
char **av = *argv;
int i, j;
int shift;
int endzsshargs = 0;
for (i = 1; i < ac && !endzsshargs; ) {
shift = 0;
if (!strcmp(av[i], "--")) {
endzsshargs = 1;
shift = 1;
}
if ((!strcmp(av[i], "-h") || !strcmp(av[i], "--help")))
usage();
if ((!strcmp(av[i], "-V") || !strcmp(av[i], "--version")))
version(1);
if ((!strcmp(av[i], "-z") || !strcmp(av[i], "--zssh-escape"))) {
if (i + 1 == ac || strlen(av[i + 1]) != 2 || set_escape(av[i + 1]) < 0)
usage();
shift = 2;
}
if (!strcmp(av[i], "-s") || !strcmp(av[i], "--shell")) {
if (i + 1 == ac)
usage();
free(gl_shav);
if (pc_test_escapes(av[i + 1]) < 0)
error("%s: parse error\n", av[i + 1]);
pc_split_words(av[i + 1], &j, &gl_shav);
pc_quote_removal(gl_shav, &j);
shift = 2;
}
if (!strcmp(av[i], "-f") || !strcmp(av[i], "--force")) {
gl_force = 1;
shift = 1;
}
if (!strcmp(av[i], "--copty")) {
gl_copty = 1;
shift = 1;
}
if (shift) {
ac -= shift;
for (j = i + shift; av[j]; j++)
av[j - shift] = av[j];
av[j - shift] = 0;
} else
i++;
}
*argc = ac;
*argv = av;
}
void init(int *argc, char ***argv)
{
char *str;
#ifdef HAVE_LIBREADLINE
initialize_readline();
#endif
init_gl(*argc, *argv);
if ((str = getenv("ZSSHESCAPE")))
if (set_escape(str) < 0)
printf("Warning: invalid ZSSHESCAPE variable value\n");
command_line_options(argc, argv);
getmaster();
getslave();
/* set current tty to raw mode */
tcsetattr(0, TCSAFLUSH, &gl_rtt);
signal(SIGCHLD, sigchld_handler);
}