telnetd.c revision 1.13 1 1.13 mrg /* $NetBSD: telnetd.c,v 1.13 1998/07/06 06:49:41 mrg Exp $ */
2 1.5 thorpej
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1989, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.9 mrg #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.9 mrg __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
39 1.9 mrg The Regents of the University of California. All rights reserved.\n");
40 1.5 thorpej #if 0
41 1.5 thorpej static char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
42 1.5 thorpej #else
43 1.13 mrg __RCSID("$NetBSD: telnetd.c,v 1.13 1998/07/06 06:49:41 mrg Exp $");
44 1.5 thorpej #endif
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #include "telnetd.h"
48 1.1 cgd #include "pathnames.h"
49 1.1 cgd
50 1.9 mrg #include <arpa/inet.h>
51 1.9 mrg
52 1.10 lukem #include <termcap.h>
53 1.9 mrg
54 1.4 jtk #define P __P
55 1.4 jtk
56 1.3 cgd #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY)
57 1.3 cgd /*
58 1.3 cgd * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can
59 1.3 cgd * use it to tell us to turn off all the socket security code,
60 1.3 cgd * since that is only used in UNICOS 7.0 and later.
61 1.3 cgd */
62 1.3 cgd # undef _SC_CRAY_SECURE_SYS
63 1.3 cgd #endif
64 1.3 cgd
65 1.3 cgd #if defined(_SC_CRAY_SECURE_SYS)
66 1.3 cgd #include <sys/sysv.h>
67 1.3 cgd #include <sys/secdev.h>
68 1.3 cgd # ifdef SO_SEC_MULTI /* 8.0 code */
69 1.3 cgd #include <sys/secparm.h>
70 1.3 cgd #include <sys/usrv.h>
71 1.3 cgd # endif /* SO_SEC_MULTI */
72 1.3 cgd int secflag;
73 1.3 cgd char tty_dev[16];
74 1.3 cgd struct secdev dv;
75 1.3 cgd struct sysv sysv;
76 1.3 cgd # ifdef SO_SEC_MULTI /* 8.0 code */
77 1.3 cgd struct socksec ss;
78 1.3 cgd # else /* SO_SEC_MULTI */ /* 7.0 code */
79 1.3 cgd struct socket_security ss;
80 1.3 cgd # endif /* SO_SEC_MULTI */
81 1.3 cgd #endif /* _SC_CRAY_SECURE_SYS */
82 1.3 cgd
83 1.3 cgd #if defined(AUTHENTICATION)
84 1.1 cgd #include <libtelnet/auth.h>
85 1.1 cgd int auth_level = 0;
86 1.1 cgd #endif
87 1.1 cgd #if defined(SecurID)
88 1.1 cgd int require_SecurID = 0;
89 1.1 cgd #endif
90 1.1 cgd
91 1.3 cgd extern int utmp_len;
92 1.3 cgd int registerd_host_only = 0;
93 1.3 cgd
94 1.3 cgd #ifdef STREAMSPTY
95 1.3 cgd # include <stropts.h>
96 1.3 cgd # include <termio.h>
97 1.3 cgd /* make sure we don't get the bsd version */
98 1.3 cgd # include "/usr/include/sys/tty.h"
99 1.3 cgd # include <sys/ptyvar.h>
100 1.3 cgd
101 1.3 cgd /*
102 1.3 cgd * Because of the way ptyibuf is used with streams messages, we need
103 1.3 cgd * ptyibuf+1 to be on a full-word boundary. The following wierdness
104 1.3 cgd * is simply to make that happen.
105 1.3 cgd */
106 1.3 cgd long ptyibufbuf[BUFSIZ/sizeof(long)+1];
107 1.3 cgd char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
108 1.3 cgd char *ptyip = ((char *)&ptyibufbuf[1])-1;
109 1.3 cgd char ptyibuf2[BUFSIZ];
110 1.3 cgd unsigned char ctlbuf[BUFSIZ];
111 1.3 cgd struct strbuf strbufc, strbufd;
112 1.3 cgd
113 1.3 cgd int readstream();
114 1.3 cgd
115 1.3 cgd #else /* ! STREAMPTY */
116 1.3 cgd
117 1.1 cgd /*
118 1.1 cgd * I/O data buffers,
119 1.1 cgd * pointers, and counters.
120 1.1 cgd */
121 1.1 cgd char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
122 1.1 cgd char ptyibuf2[BUFSIZ];
123 1.1 cgd
124 1.3 cgd #endif /* ! STREAMPTY */
125 1.3 cgd
126 1.1 cgd int hostinfo = 1; /* do we print login banner? */
127 1.1 cgd
128 1.1 cgd #ifdef CRAY
129 1.1 cgd extern int newmap; /* nonzero if \n maps to ^M^J */
130 1.1 cgd int lowpty = 0, highpty; /* low, high pty numbers */
131 1.1 cgd #endif /* CRAY */
132 1.1 cgd
133 1.1 cgd int debug = 0;
134 1.1 cgd int keepalive = 1;
135 1.6 tls char *gettyname = "default";
136 1.1 cgd char *progname;
137 1.1 cgd
138 1.1 cgd extern void usage P((void));
139 1.9 mrg int main __P((int, char *[]));
140 1.9 mrg void usage __P((void));
141 1.9 mrg int getterminaltype __P((char *));
142 1.9 mrg int getent __P((char *, char *));
143 1.9 mrg void doit __P((struct sockaddr_in *));
144 1.9 mrg void _gettermname __P((void));
145 1.9 mrg int terminaltypeok __P((char *));
146 1.9 mrg char *getstr __P((char *, char **));
147 1.1 cgd
148 1.3 cgd /*
149 1.3 cgd * The string to pass to getopt(). We do it this way so
150 1.3 cgd * that only the actual options that we support will be
151 1.3 cgd * passed off to getopt().
152 1.3 cgd */
153 1.3 cgd char valid_opts[] = {
154 1.6 tls 'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
155 1.3 cgd #ifdef AUTHENTICATION
156 1.3 cgd 'a', ':', 'X', ':',
157 1.3 cgd #endif
158 1.3 cgd #ifdef BFTPDAEMON
159 1.3 cgd 'B',
160 1.3 cgd #endif
161 1.3 cgd #ifdef DIAGNOSTICS
162 1.3 cgd 'D', ':',
163 1.3 cgd #endif
164 1.3 cgd #if defined(CRAY) && defined(NEWINIT)
165 1.3 cgd 'I', ':',
166 1.3 cgd #endif
167 1.3 cgd #ifdef LINEMODE
168 1.3 cgd 'l',
169 1.3 cgd #endif
170 1.3 cgd #ifdef CRAY
171 1.3 cgd 'r', ':',
172 1.3 cgd #endif
173 1.3 cgd #ifdef SecurID
174 1.3 cgd 's',
175 1.3 cgd #endif
176 1.3 cgd '\0'
177 1.3 cgd };
178 1.3 cgd
179 1.9 mrg int
180 1.1 cgd main(argc, argv)
181 1.1 cgd char *argv[];
182 1.1 cgd {
183 1.1 cgd struct sockaddr_in from;
184 1.1 cgd int on = 1, fromlen;
185 1.1 cgd register int ch;
186 1.1 cgd #if defined(IPPROTO_IP) && defined(IP_TOS)
187 1.1 cgd int tos = -1;
188 1.1 cgd #endif
189 1.1 cgd
190 1.1 cgd pfrontp = pbackp = ptyobuf;
191 1.1 cgd netip = netibuf;
192 1.1 cgd nfrontp = nbackp = netobuf;
193 1.1 cgd
194 1.1 cgd progname = *argv;
195 1.1 cgd
196 1.1 cgd #ifdef CRAY
197 1.1 cgd /*
198 1.1 cgd * Get number of pty's before trying to process options,
199 1.1 cgd * which may include changing pty range.
200 1.1 cgd */
201 1.1 cgd highpty = getnpty();
202 1.1 cgd #endif /* CRAY */
203 1.1 cgd
204 1.7 enami while ((ch = getopt(argc, argv, valid_opts)) != -1) {
205 1.8 enami switch (ch) {
206 1.1 cgd
207 1.3 cgd #ifdef AUTHENTICATION
208 1.1 cgd case 'a':
209 1.1 cgd /*
210 1.1 cgd * Check for required authentication level
211 1.1 cgd */
212 1.1 cgd if (strcmp(optarg, "debug") == 0) {
213 1.1 cgd extern int auth_debug_mode;
214 1.1 cgd auth_debug_mode = 1;
215 1.1 cgd } else if (strcasecmp(optarg, "none") == 0) {
216 1.1 cgd auth_level = 0;
217 1.1 cgd } else if (strcasecmp(optarg, "other") == 0) {
218 1.1 cgd auth_level = AUTH_OTHER;
219 1.1 cgd } else if (strcasecmp(optarg, "user") == 0) {
220 1.1 cgd auth_level = AUTH_USER;
221 1.1 cgd } else if (strcasecmp(optarg, "valid") == 0) {
222 1.1 cgd auth_level = AUTH_VALID;
223 1.1 cgd } else if (strcasecmp(optarg, "off") == 0) {
224 1.1 cgd /*
225 1.1 cgd * This hack turns off authentication
226 1.1 cgd */
227 1.1 cgd auth_level = -1;
228 1.1 cgd } else {
229 1.1 cgd fprintf(stderr,
230 1.1 cgd "telnetd: unknown authorization level for -a\n");
231 1.1 cgd }
232 1.1 cgd break;
233 1.3 cgd #endif /* AUTHENTICATION */
234 1.1 cgd
235 1.1 cgd #ifdef BFTPDAEMON
236 1.1 cgd case 'B':
237 1.1 cgd bftpd++;
238 1.1 cgd break;
239 1.1 cgd #endif /* BFTPDAEMON */
240 1.1 cgd
241 1.1 cgd case 'd':
242 1.1 cgd if (strcmp(optarg, "ebug") == 0) {
243 1.1 cgd debug++;
244 1.1 cgd break;
245 1.1 cgd }
246 1.1 cgd usage();
247 1.1 cgd /* NOTREACHED */
248 1.1 cgd break;
249 1.1 cgd
250 1.1 cgd #ifdef DIAGNOSTICS
251 1.1 cgd case 'D':
252 1.1 cgd /*
253 1.1 cgd * Check for desired diagnostics capabilities.
254 1.1 cgd */
255 1.1 cgd if (!strcmp(optarg, "report")) {
256 1.1 cgd diagnostic |= TD_REPORT|TD_OPTIONS;
257 1.1 cgd } else if (!strcmp(optarg, "exercise")) {
258 1.1 cgd diagnostic |= TD_EXERCISE;
259 1.1 cgd } else if (!strcmp(optarg, "netdata")) {
260 1.1 cgd diagnostic |= TD_NETDATA;
261 1.1 cgd } else if (!strcmp(optarg, "ptydata")) {
262 1.1 cgd diagnostic |= TD_PTYDATA;
263 1.1 cgd } else if (!strcmp(optarg, "options")) {
264 1.1 cgd diagnostic |= TD_OPTIONS;
265 1.1 cgd } else {
266 1.1 cgd usage();
267 1.1 cgd /* NOT REACHED */
268 1.1 cgd }
269 1.1 cgd break;
270 1.1 cgd #endif /* DIAGNOSTICS */
271 1.1 cgd
272 1.6 tls case 'g':
273 1.6 tls gettyname = optarg;
274 1.6 tls break;
275 1.1 cgd
276 1.1 cgd case 'h':
277 1.1 cgd hostinfo = 0;
278 1.1 cgd break;
279 1.1 cgd
280 1.1 cgd #if defined(CRAY) && defined(NEWINIT)
281 1.1 cgd case 'I':
282 1.1 cgd {
283 1.1 cgd extern char *gen_id;
284 1.1 cgd gen_id = optarg;
285 1.1 cgd break;
286 1.1 cgd }
287 1.1 cgd #endif /* defined(CRAY) && defined(NEWINIT) */
288 1.1 cgd
289 1.1 cgd #ifdef LINEMODE
290 1.1 cgd case 'l':
291 1.1 cgd alwayslinemode = 1;
292 1.1 cgd break;
293 1.1 cgd #endif /* LINEMODE */
294 1.1 cgd
295 1.3 cgd case 'k':
296 1.3 cgd #if defined(LINEMODE) && defined(KLUDGELINEMODE)
297 1.3 cgd lmodetype = NO_AUTOKLUDGE;
298 1.3 cgd #else
299 1.3 cgd /* ignore -k option if built without kludge linemode */
300 1.3 cgd #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
301 1.3 cgd break;
302 1.3 cgd
303 1.1 cgd case 'n':
304 1.1 cgd keepalive = 0;
305 1.1 cgd break;
306 1.1 cgd
307 1.1 cgd #ifdef CRAY
308 1.1 cgd case 'r':
309 1.1 cgd {
310 1.1 cgd char *strchr();
311 1.1 cgd char *c;
312 1.1 cgd
313 1.1 cgd /*
314 1.1 cgd * Allow the specification of alterations
315 1.1 cgd * to the pty search range. It is legal to
316 1.1 cgd * specify only one, and not change the
317 1.1 cgd * other from its default.
318 1.1 cgd */
319 1.1 cgd c = strchr(optarg, '-');
320 1.1 cgd if (c) {
321 1.1 cgd *c++ = '\0';
322 1.1 cgd highpty = atoi(c);
323 1.1 cgd }
324 1.1 cgd if (*optarg != '\0')
325 1.1 cgd lowpty = atoi(optarg);
326 1.1 cgd if ((lowpty > highpty) || (lowpty < 0) ||
327 1.1 cgd (highpty > 32767)) {
328 1.1 cgd usage();
329 1.1 cgd /* NOT REACHED */
330 1.1 cgd }
331 1.1 cgd break;
332 1.1 cgd }
333 1.1 cgd #endif /* CRAY */
334 1.1 cgd
335 1.1 cgd #ifdef SecurID
336 1.1 cgd case 's':
337 1.1 cgd /* SecurID required */
338 1.1 cgd require_SecurID = 1;
339 1.1 cgd break;
340 1.1 cgd #endif /* SecurID */
341 1.1 cgd case 'S':
342 1.1 cgd #ifdef HAS_GETTOS
343 1.1 cgd if ((tos = parsetos(optarg, "tcp")) < 0)
344 1.1 cgd fprintf(stderr, "%s%s%s\n",
345 1.1 cgd "telnetd: Bad TOS argument '", optarg,
346 1.1 cgd "'; will try to use default TOS");
347 1.1 cgd #else
348 1.1 cgd fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
349 1.1 cgd "-S flag not supported\n");
350 1.1 cgd #endif
351 1.1 cgd break;
352 1.1 cgd
353 1.3 cgd case 'u':
354 1.3 cgd utmp_len = atoi(optarg);
355 1.3 cgd break;
356 1.3 cgd
357 1.3 cgd case 'U':
358 1.3 cgd registerd_host_only = 1;
359 1.3 cgd break;
360 1.3 cgd
361 1.3 cgd #ifdef AUTHENTICATION
362 1.1 cgd case 'X':
363 1.1 cgd /*
364 1.1 cgd * Check for invalid authentication types
365 1.1 cgd */
366 1.1 cgd auth_disable_name(optarg);
367 1.1 cgd break;
368 1.3 cgd #endif /* AUTHENTICATION */
369 1.1 cgd
370 1.1 cgd default:
371 1.3 cgd fprintf(stderr, "telnetd: %c: unknown option\n", ch);
372 1.1 cgd /* FALLTHROUGH */
373 1.1 cgd case '?':
374 1.1 cgd usage();
375 1.1 cgd /* NOTREACHED */
376 1.1 cgd }
377 1.1 cgd }
378 1.1 cgd
379 1.1 cgd argc -= optind;
380 1.1 cgd argv += optind;
381 1.1 cgd
382 1.1 cgd if (debug) {
383 1.1 cgd int s, ns, foo;
384 1.1 cgd struct servent *sp;
385 1.1 cgd static struct sockaddr_in sin = { AF_INET };
386 1.1 cgd
387 1.1 cgd if (argc > 1) {
388 1.1 cgd usage();
389 1.1 cgd /* NOT REACHED */
390 1.1 cgd } else if (argc == 1) {
391 1.9 mrg if ((sp = getservbyname(*argv, "tcp"))) {
392 1.1 cgd sin.sin_port = sp->s_port;
393 1.1 cgd } else {
394 1.1 cgd sin.sin_port = atoi(*argv);
395 1.1 cgd if ((int)sin.sin_port <= 0) {
396 1.1 cgd fprintf(stderr, "telnetd: %s: bad port #\n", *argv);
397 1.1 cgd usage();
398 1.1 cgd /* NOT REACHED */
399 1.1 cgd }
400 1.1 cgd sin.sin_port = htons((u_short)sin.sin_port);
401 1.1 cgd }
402 1.1 cgd } else {
403 1.1 cgd sp = getservbyname("telnet", "tcp");
404 1.1 cgd if (sp == 0) {
405 1.1 cgd fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
406 1.1 cgd exit(1);
407 1.1 cgd }
408 1.1 cgd sin.sin_port = sp->s_port;
409 1.1 cgd }
410 1.1 cgd
411 1.1 cgd s = socket(AF_INET, SOCK_STREAM, 0);
412 1.1 cgd if (s < 0) {
413 1.1 cgd perror("telnetd: socket");;
414 1.1 cgd exit(1);
415 1.1 cgd }
416 1.3 cgd (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
417 1.3 cgd (char *)&on, sizeof(on));
418 1.1 cgd if (bind(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
419 1.1 cgd perror("bind");
420 1.1 cgd exit(1);
421 1.1 cgd }
422 1.1 cgd if (listen(s, 1) < 0) {
423 1.1 cgd perror("listen");
424 1.1 cgd exit(1);
425 1.1 cgd }
426 1.1 cgd foo = sizeof sin;
427 1.1 cgd ns = accept(s, (struct sockaddr *)&sin, &foo);
428 1.1 cgd if (ns < 0) {
429 1.1 cgd perror("accept");
430 1.1 cgd exit(1);
431 1.1 cgd }
432 1.1 cgd (void) dup2(ns, 0);
433 1.1 cgd (void) close(ns);
434 1.1 cgd (void) close(s);
435 1.1 cgd #ifdef convex
436 1.1 cgd } else if (argc == 1) {
437 1.1 cgd ; /* VOID*/ /* Just ignore the host/port name */
438 1.1 cgd #endif
439 1.1 cgd } else if (argc > 0) {
440 1.1 cgd usage();
441 1.1 cgd /* NOT REACHED */
442 1.1 cgd }
443 1.1 cgd
444 1.3 cgd #if defined(_SC_CRAY_SECURE_SYS)
445 1.3 cgd secflag = sysconf(_SC_CRAY_SECURE_SYS);
446 1.3 cgd
447 1.3 cgd /*
448 1.3 cgd * Get socket's security label
449 1.3 cgd */
450 1.3 cgd if (secflag) {
451 1.3 cgd int szss = sizeof(ss);
452 1.3 cgd #ifdef SO_SEC_MULTI /* 8.0 code */
453 1.3 cgd int sock_multi;
454 1.3 cgd int szi = sizeof(int);
455 1.3 cgd #endif /* SO_SEC_MULTI */
456 1.3 cgd
457 1.4 jtk memset((char *)&dv, 0, sizeof(dv));
458 1.3 cgd
459 1.3 cgd if (getsysv(&sysv, sizeof(struct sysv)) != 0) {
460 1.3 cgd perror("getsysv");
461 1.3 cgd exit(1);
462 1.3 cgd }
463 1.3 cgd
464 1.3 cgd /*
465 1.3 cgd * Get socket security label and set device values
466 1.3 cgd * {security label to be set on ttyp device}
467 1.3 cgd */
468 1.3 cgd #ifdef SO_SEC_MULTI /* 8.0 code */
469 1.3 cgd if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
470 1.3 cgd (char *)&ss, &szss) < 0) ||
471 1.3 cgd (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
472 1.3 cgd (char *)&sock_multi, &szi) < 0)) {
473 1.3 cgd perror("getsockopt");
474 1.3 cgd exit(1);
475 1.3 cgd } else {
476 1.3 cgd dv.dv_actlvl = ss.ss_actlabel.lt_level;
477 1.3 cgd dv.dv_actcmp = ss.ss_actlabel.lt_compart;
478 1.3 cgd if (!sock_multi) {
479 1.3 cgd dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
480 1.3 cgd dv.dv_valcmp = dv.dv_actcmp;
481 1.3 cgd } else {
482 1.3 cgd dv.dv_minlvl = ss.ss_minlabel.lt_level;
483 1.3 cgd dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
484 1.3 cgd dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
485 1.3 cgd }
486 1.3 cgd dv.dv_devflg = 0;
487 1.3 cgd }
488 1.3 cgd #else /* SO_SEC_MULTI */ /* 7.0 code */
489 1.3 cgd if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
490 1.3 cgd (char *)&ss, &szss) >= 0) {
491 1.3 cgd dv.dv_actlvl = ss.ss_slevel;
492 1.3 cgd dv.dv_actcmp = ss.ss_compart;
493 1.3 cgd dv.dv_minlvl = ss.ss_minlvl;
494 1.3 cgd dv.dv_maxlvl = ss.ss_maxlvl;
495 1.3 cgd dv.dv_valcmp = ss.ss_maxcmp;
496 1.3 cgd }
497 1.3 cgd #endif /* SO_SEC_MULTI */
498 1.3 cgd }
499 1.3 cgd #endif /* _SC_CRAY_SECURE_SYS */
500 1.3 cgd
501 1.1 cgd openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
502 1.1 cgd fromlen = sizeof (from);
503 1.1 cgd if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
504 1.1 cgd fprintf(stderr, "%s: ", progname);
505 1.1 cgd perror("getpeername");
506 1.1 cgd _exit(1);
507 1.1 cgd }
508 1.1 cgd if (keepalive &&
509 1.3 cgd setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
510 1.3 cgd (char *)&on, sizeof (on)) < 0) {
511 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
512 1.1 cgd }
513 1.1 cgd
514 1.1 cgd #if defined(IPPROTO_IP) && defined(IP_TOS)
515 1.1 cgd {
516 1.1 cgd # if defined(HAS_GETTOS)
517 1.1 cgd struct tosent *tp;
518 1.1 cgd if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
519 1.1 cgd tos = tp->t_tos;
520 1.1 cgd # endif
521 1.1 cgd if (tos < 0)
522 1.1 cgd tos = 020; /* Low Delay bit */
523 1.1 cgd if (tos
524 1.3 cgd && (setsockopt(0, IPPROTO_IP, IP_TOS,
525 1.3 cgd (char *)&tos, sizeof(tos)) < 0)
526 1.1 cgd && (errno != ENOPROTOOPT) )
527 1.1 cgd syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
528 1.1 cgd }
529 1.1 cgd #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
530 1.1 cgd net = 0;
531 1.1 cgd doit(&from);
532 1.1 cgd /* NOTREACHED */
533 1.9 mrg #ifdef __GNUC__
534 1.9 mrg exit(0);
535 1.9 mrg #endif
536 1.1 cgd } /* end of main */
537 1.1 cgd
538 1.1 cgd void
539 1.1 cgd usage()
540 1.1 cgd {
541 1.1 cgd fprintf(stderr, "Usage: telnetd");
542 1.3 cgd #ifdef AUTHENTICATION
543 1.3 cgd fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
544 1.1 cgd #endif
545 1.1 cgd #ifdef BFTPDAEMON
546 1.1 cgd fprintf(stderr, " [-B]");
547 1.1 cgd #endif
548 1.1 cgd fprintf(stderr, " [-debug]");
549 1.1 cgd #ifdef DIAGNOSTICS
550 1.1 cgd fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
551 1.1 cgd #endif
552 1.3 cgd #ifdef AUTHENTICATION
553 1.1 cgd fprintf(stderr, " [-edebug]");
554 1.1 cgd #endif
555 1.1 cgd fprintf(stderr, " [-h]");
556 1.1 cgd #if defined(CRAY) && defined(NEWINIT)
557 1.1 cgd fprintf(stderr, " [-Iinitid]");
558 1.1 cgd #endif
559 1.3 cgd #if defined(LINEMODE) && defined(KLUDGELINEMODE)
560 1.3 cgd fprintf(stderr, " [-k]");
561 1.3 cgd #endif
562 1.1 cgd #ifdef LINEMODE
563 1.1 cgd fprintf(stderr, " [-l]");
564 1.1 cgd #endif
565 1.1 cgd fprintf(stderr, " [-n]");
566 1.1 cgd #ifdef CRAY
567 1.1 cgd fprintf(stderr, " [-r[lowpty]-[highpty]]");
568 1.1 cgd #endif
569 1.3 cgd fprintf(stderr, "\n\t");
570 1.1 cgd #ifdef SecurID
571 1.1 cgd fprintf(stderr, " [-s]");
572 1.1 cgd #endif
573 1.3 cgd #ifdef HAS_GETTOS
574 1.3 cgd fprintf(stderr, " [-S tos]");
575 1.3 cgd #endif
576 1.3 cgd #ifdef AUTHENTICATION
577 1.1 cgd fprintf(stderr, " [-X auth-type]");
578 1.1 cgd #endif
579 1.3 cgd fprintf(stderr, " [-u utmp_hostname_length] [-U]");
580 1.1 cgd fprintf(stderr, " [port]\n");
581 1.1 cgd exit(1);
582 1.1 cgd }
583 1.1 cgd
584 1.1 cgd /*
585 1.1 cgd * getterminaltype
586 1.1 cgd *
587 1.1 cgd * Ask the other end to send along its terminal type and speed.
588 1.1 cgd * Output is the variable terminaltype filled in.
589 1.1 cgd */
590 1.3 cgd static unsigned char ttytype_sbbuf[] = {
591 1.3 cgd IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
592 1.3 cgd };
593 1.1 cgd
594 1.1 cgd int
595 1.1 cgd getterminaltype(name)
596 1.1 cgd char *name;
597 1.1 cgd {
598 1.1 cgd int retval = -1;
599 1.1 cgd
600 1.1 cgd settimer(baseline);
601 1.3 cgd #if defined(AUTHENTICATION)
602 1.1 cgd /*
603 1.1 cgd * Handle the Authentication option before we do anything else.
604 1.1 cgd */
605 1.1 cgd send_do(TELOPT_AUTHENTICATION, 1);
606 1.1 cgd while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
607 1.1 cgd ttloop();
608 1.1 cgd if (his_state_is_will(TELOPT_AUTHENTICATION)) {
609 1.1 cgd retval = auth_wait(name);
610 1.1 cgd }
611 1.1 cgd #endif
612 1.1 cgd
613 1.1 cgd send_do(TELOPT_TTYPE, 1);
614 1.1 cgd send_do(TELOPT_TSPEED, 1);
615 1.1 cgd send_do(TELOPT_XDISPLOC, 1);
616 1.3 cgd send_do(TELOPT_NEW_ENVIRON, 1);
617 1.3 cgd send_do(TELOPT_OLD_ENVIRON, 1);
618 1.1 cgd while (
619 1.1 cgd his_will_wont_is_changing(TELOPT_TTYPE) ||
620 1.1 cgd his_will_wont_is_changing(TELOPT_TSPEED) ||
621 1.1 cgd his_will_wont_is_changing(TELOPT_XDISPLOC) ||
622 1.3 cgd his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
623 1.3 cgd his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
624 1.1 cgd ttloop();
625 1.1 cgd }
626 1.1 cgd if (his_state_is_will(TELOPT_TSPEED)) {
627 1.3 cgd static unsigned char sb[] =
628 1.3 cgd { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
629 1.1 cgd
630 1.4 jtk memmove(nfrontp, sb, sizeof sb);
631 1.3 cgd nfrontp += sizeof sb;
632 1.4 jtk DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
633 1.1 cgd }
634 1.1 cgd if (his_state_is_will(TELOPT_XDISPLOC)) {
635 1.3 cgd static unsigned char sb[] =
636 1.3 cgd { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
637 1.3 cgd
638 1.4 jtk memmove(nfrontp, sb, sizeof sb);
639 1.3 cgd nfrontp += sizeof sb;
640 1.4 jtk DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
641 1.3 cgd }
642 1.3 cgd if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
643 1.3 cgd static unsigned char sb[] =
644 1.3 cgd { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
645 1.1 cgd
646 1.4 jtk memmove(nfrontp, sb, sizeof sb);
647 1.3 cgd nfrontp += sizeof sb;
648 1.4 jtk DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
649 1.1 cgd }
650 1.3 cgd else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
651 1.3 cgd static unsigned char sb[] =
652 1.3 cgd { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
653 1.1 cgd
654 1.4 jtk memmove(nfrontp, sb, sizeof sb);
655 1.3 cgd nfrontp += sizeof sb;
656 1.4 jtk DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
657 1.1 cgd }
658 1.1 cgd if (his_state_is_will(TELOPT_TTYPE)) {
659 1.1 cgd
660 1.4 jtk memmove(nfrontp, ttytype_sbbuf, sizeof ttytype_sbbuf);
661 1.1 cgd nfrontp += sizeof ttytype_sbbuf;
662 1.4 jtk DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
663 1.4 jtk sizeof ttytype_sbbuf - 2););
664 1.1 cgd }
665 1.1 cgd if (his_state_is_will(TELOPT_TSPEED)) {
666 1.1 cgd while (sequenceIs(tspeedsubopt, baseline))
667 1.1 cgd ttloop();
668 1.1 cgd }
669 1.1 cgd if (his_state_is_will(TELOPT_XDISPLOC)) {
670 1.1 cgd while (sequenceIs(xdisplocsubopt, baseline))
671 1.1 cgd ttloop();
672 1.1 cgd }
673 1.3 cgd if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
674 1.1 cgd while (sequenceIs(environsubopt, baseline))
675 1.1 cgd ttloop();
676 1.1 cgd }
677 1.3 cgd if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
678 1.3 cgd while (sequenceIs(oenvironsubopt, baseline))
679 1.3 cgd ttloop();
680 1.3 cgd }
681 1.1 cgd if (his_state_is_will(TELOPT_TTYPE)) {
682 1.1 cgd char first[256], last[256];
683 1.1 cgd
684 1.1 cgd while (sequenceIs(ttypesubopt, baseline))
685 1.1 cgd ttloop();
686 1.1 cgd
687 1.1 cgd /*
688 1.1 cgd * If the other side has already disabled the option, then
689 1.1 cgd * we have to just go with what we (might) have already gotten.
690 1.1 cgd */
691 1.1 cgd if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
692 1.1 cgd (void) strncpy(first, terminaltype, sizeof(first));
693 1.1 cgd for(;;) {
694 1.1 cgd /*
695 1.1 cgd * Save the unknown name, and request the next name.
696 1.1 cgd */
697 1.1 cgd (void) strncpy(last, terminaltype, sizeof(last));
698 1.1 cgd _gettermname();
699 1.1 cgd if (terminaltypeok(terminaltype))
700 1.1 cgd break;
701 1.1 cgd if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
702 1.1 cgd his_state_is_wont(TELOPT_TTYPE)) {
703 1.1 cgd /*
704 1.1 cgd * We've hit the end. If this is the same as
705 1.1 cgd * the first name, just go with it.
706 1.1 cgd */
707 1.1 cgd if (strncmp(first, terminaltype, sizeof(first)) == 0)
708 1.1 cgd break;
709 1.1 cgd /*
710 1.1 cgd * Get the terminal name one more time, so that
711 1.1 cgd * RFC1091 compliant telnets will cycle back to
712 1.1 cgd * the start of the list.
713 1.1 cgd */
714 1.1 cgd _gettermname();
715 1.1 cgd if (strncmp(first, terminaltype, sizeof(first)) != 0)
716 1.1 cgd (void) strncpy(terminaltype, first, sizeof(first));
717 1.1 cgd break;
718 1.1 cgd }
719 1.1 cgd }
720 1.1 cgd }
721 1.1 cgd }
722 1.1 cgd return(retval);
723 1.1 cgd } /* end of getterminaltype */
724 1.1 cgd
725 1.1 cgd void
726 1.1 cgd _gettermname()
727 1.1 cgd {
728 1.1 cgd /*
729 1.1 cgd * If the client turned off the option,
730 1.1 cgd * we can't send another request, so we
731 1.1 cgd * just return.
732 1.1 cgd */
733 1.1 cgd if (his_state_is_wont(TELOPT_TTYPE))
734 1.1 cgd return;
735 1.1 cgd settimer(baseline);
736 1.4 jtk memmove(nfrontp, ttytype_sbbuf, sizeof ttytype_sbbuf);
737 1.1 cgd nfrontp += sizeof ttytype_sbbuf;
738 1.4 jtk DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
739 1.4 jtk sizeof ttytype_sbbuf - 2););
740 1.1 cgd while (sequenceIs(ttypesubopt, baseline))
741 1.1 cgd ttloop();
742 1.1 cgd }
743 1.1 cgd
744 1.1 cgd int
745 1.1 cgd terminaltypeok(s)
746 1.1 cgd char *s;
747 1.1 cgd {
748 1.1 cgd char buf[1024];
749 1.1 cgd
750 1.1 cgd if (terminaltype == NULL)
751 1.1 cgd return(1);
752 1.1 cgd
753 1.1 cgd /*
754 1.1 cgd * tgetent() will return 1 if the type is known, and
755 1.1 cgd * 0 if it is not known. If it returns -1, it couldn't
756 1.1 cgd * open the database. But if we can't open the database,
757 1.1 cgd * it won't help to say we failed, because we won't be
758 1.1 cgd * able to verify anything else. So, we treat -1 like 1.
759 1.1 cgd */
760 1.1 cgd if (tgetent(buf, s) == 0)
761 1.1 cgd return(0);
762 1.1 cgd return(1);
763 1.1 cgd }
764 1.1 cgd
765 1.1 cgd #ifndef MAXHOSTNAMELEN
766 1.1 cgd #define MAXHOSTNAMELEN 64
767 1.1 cgd #endif /* MAXHOSTNAMELEN */
768 1.1 cgd
769 1.1 cgd char *hostname;
770 1.13 mrg char host_name[MAXHOSTNAMELEN + 1];
771 1.13 mrg char remote_host_name[MAXHOSTNAMELEN + 1];
772 1.1 cgd
773 1.1 cgd #ifndef convex
774 1.1 cgd extern void telnet P((int, int));
775 1.1 cgd #else
776 1.1 cgd extern void telnet P((int, int, char *));
777 1.1 cgd #endif
778 1.1 cgd
779 1.1 cgd /*
780 1.1 cgd * Get a pty, scan input lines.
781 1.1 cgd */
782 1.9 mrg void
783 1.1 cgd doit(who)
784 1.1 cgd struct sockaddr_in *who;
785 1.1 cgd {
786 1.9 mrg char *host;
787 1.1 cgd struct hostent *hp;
788 1.1 cgd int level;
789 1.3 cgd int ptynum;
790 1.1 cgd char user_name[256];
791 1.1 cgd
792 1.1 cgd /*
793 1.1 cgd * Find an available pty to use.
794 1.1 cgd */
795 1.1 cgd #ifndef convex
796 1.3 cgd pty = getpty(&ptynum);
797 1.1 cgd if (pty < 0)
798 1.1 cgd fatal(net, "All network ports in use");
799 1.1 cgd #else
800 1.1 cgd for (;;) {
801 1.1 cgd char *lp;
802 1.11 mikel extern char *line;
803 1.1 cgd
804 1.1 cgd if ((lp = getpty()) == NULL)
805 1.1 cgd fatal(net, "Out of ptys");
806 1.1 cgd
807 1.1 cgd if ((pty = open(lp, 2)) >= 0) {
808 1.1 cgd strcpy(line,lp);
809 1.1 cgd line[5] = 't';
810 1.1 cgd break;
811 1.1 cgd }
812 1.1 cgd }
813 1.1 cgd #endif
814 1.1 cgd
815 1.3 cgd #if defined(_SC_CRAY_SECURE_SYS)
816 1.3 cgd /*
817 1.4 jtk * set ttyp line security label
818 1.3 cgd */
819 1.3 cgd if (secflag) {
820 1.3 cgd char slave_dev[16];
821 1.3 cgd
822 1.3 cgd sprintf(tty_dev, "/dev/pty/%03d", ptynum);
823 1.3 cgd if (setdevs(tty_dev, &dv) < 0)
824 1.3 cgd fatal(net, "cannot set pty security");
825 1.3 cgd sprintf(slave_dev, "/dev/ttyp%03d", ptynum);
826 1.3 cgd if (setdevs(slave_dev, &dv) < 0)
827 1.3 cgd fatal(net, "cannot set tty security");
828 1.3 cgd }
829 1.3 cgd #endif /* _SC_CRAY_SECURE_SYS */
830 1.3 cgd
831 1.1 cgd /* get name of connected client */
832 1.1 cgd hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr),
833 1.1 cgd who->sin_family);
834 1.3 cgd
835 1.3 cgd if (hp == NULL && registerd_host_only) {
836 1.3 cgd fatal(net, "Couldn't resolve your address into a host name.\r\n\
837 1.4 jtk Please contact your net administrator");
838 1.9 mrg #ifdef __GNUC__
839 1.9 mrg host = NULL; /* XXX gcc */
840 1.9 mrg #endif
841 1.3 cgd } else if (hp &&
842 1.4 jtk (strlen(hp->h_name) <= (unsigned int)((utmp_len < 0) ? -utmp_len
843 1.4 jtk : utmp_len))) {
844 1.1 cgd host = hp->h_name;
845 1.3 cgd } else {
846 1.1 cgd host = inet_ntoa(who->sin_addr);
847 1.3 cgd }
848 1.1 cgd /*
849 1.1 cgd * We must make a copy because Kerberos is probably going
850 1.1 cgd * to also do a gethost* and overwrite the static data...
851 1.1 cgd */
852 1.1 cgd strncpy(remote_host_name, host, sizeof(remote_host_name)-1);
853 1.1 cgd remote_host_name[sizeof(remote_host_name)-1] = 0;
854 1.1 cgd host = remote_host_name;
855 1.1 cgd
856 1.13 mrg (void)gethostname(host_name, sizeof (host_name));
857 1.13 mrg host_name[sizeof(host_name) - 1] = '\0';
858 1.1 cgd hostname = host_name;
859 1.1 cgd
860 1.3 cgd #if defined(AUTHENTICATION)
861 1.1 cgd auth_encrypt_init(hostname, host, "TELNETD", 1);
862 1.1 cgd #endif
863 1.1 cgd
864 1.1 cgd init_env();
865 1.1 cgd /*
866 1.1 cgd * get terminal type.
867 1.1 cgd */
868 1.1 cgd *user_name = 0;
869 1.1 cgd level = getterminaltype(user_name);
870 1.1 cgd setenv("TERM", terminaltype ? terminaltype : "network", 1);
871 1.1 cgd
872 1.1 cgd /*
873 1.1 cgd * Start up the login process on the slave side of the terminal
874 1.1 cgd */
875 1.1 cgd #ifndef convex
876 1.1 cgd startslave(host, level, user_name);
877 1.1 cgd
878 1.3 cgd #if defined(_SC_CRAY_SECURE_SYS)
879 1.3 cgd if (secflag) {
880 1.3 cgd if (setulvl(dv.dv_actlvl) < 0)
881 1.3 cgd fatal(net,"cannot setulvl()");
882 1.3 cgd if (setucmp(dv.dv_actcmp) < 0)
883 1.3 cgd fatal(net, "cannot setucmp()");
884 1.3 cgd }
885 1.3 cgd #endif /* _SC_CRAY_SECURE_SYS */
886 1.3 cgd
887 1.1 cgd telnet(net, pty); /* begin server processing */
888 1.1 cgd #else
889 1.1 cgd telnet(net, pty, host);
890 1.1 cgd #endif
891 1.1 cgd /*NOTREACHED*/
892 1.1 cgd } /* end of doit */
893 1.1 cgd
894 1.1 cgd #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
895 1.1 cgd int
896 1.1 cgd Xterm_output(ibufp, obuf, icountp, ocount)
897 1.1 cgd char **ibufp, *obuf;
898 1.1 cgd int *icountp, ocount;
899 1.1 cgd {
900 1.1 cgd int ret;
901 1.1 cgd ret = term_output(*ibufp, obuf, *icountp, ocount);
902 1.1 cgd *ibufp += *icountp;
903 1.1 cgd *icountp = 0;
904 1.1 cgd return(ret);
905 1.1 cgd }
906 1.1 cgd #define term_output Xterm_output
907 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
908 1.1 cgd
909 1.1 cgd /*
910 1.1 cgd * Main loop. Select from pty and network, and
911 1.1 cgd * hand data to telnet receiver finite state machine.
912 1.1 cgd */
913 1.1 cgd void
914 1.1 cgd #ifndef convex
915 1.1 cgd telnet(f, p)
916 1.1 cgd #else
917 1.1 cgd telnet(f, p, host)
918 1.1 cgd #endif
919 1.1 cgd int f, p;
920 1.1 cgd #ifdef convex
921 1.1 cgd char *host;
922 1.1 cgd #endif
923 1.1 cgd {
924 1.1 cgd int on = 1;
925 1.1 cgd #define TABBUFSIZ 512
926 1.1 cgd char defent[TABBUFSIZ];
927 1.1 cgd char defstrs[TABBUFSIZ];
928 1.1 cgd #undef TABBUFSIZ
929 1.1 cgd char *HE;
930 1.1 cgd char *HN;
931 1.1 cgd char *IM;
932 1.4 jtk int nfd;
933 1.1 cgd
934 1.1 cgd /*
935 1.1 cgd * Initialize the slc mapping table.
936 1.1 cgd */
937 1.1 cgd get_slc_defaults();
938 1.1 cgd
939 1.1 cgd /*
940 1.1 cgd * Do some tests where it is desireable to wait for a response.
941 1.1 cgd * Rather than doing them slowly, one at a time, do them all
942 1.1 cgd * at once.
943 1.1 cgd */
944 1.1 cgd if (my_state_is_wont(TELOPT_SGA))
945 1.1 cgd send_will(TELOPT_SGA, 1);
946 1.1 cgd /*
947 1.1 cgd * Is the client side a 4.2 (NOT 4.3) system? We need to know this
948 1.1 cgd * because 4.2 clients are unable to deal with TCP urgent data.
949 1.1 cgd *
950 1.1 cgd * To find out, we send out a "DO ECHO". If the remote system
951 1.1 cgd * answers "WILL ECHO" it is probably a 4.2 client, and we note
952 1.1 cgd * that fact ("WILL ECHO" ==> that the client will echo what
953 1.1 cgd * WE, the server, sends it; it does NOT mean that the client will
954 1.1 cgd * echo the terminal input).
955 1.1 cgd */
956 1.1 cgd send_do(TELOPT_ECHO, 1);
957 1.1 cgd
958 1.1 cgd #ifdef LINEMODE
959 1.1 cgd if (his_state_is_wont(TELOPT_LINEMODE)) {
960 1.1 cgd /* Query the peer for linemode support by trying to negotiate
961 1.1 cgd * the linemode option.
962 1.1 cgd */
963 1.1 cgd linemode = 0;
964 1.1 cgd editmode = 0;
965 1.1 cgd send_do(TELOPT_LINEMODE, 1); /* send do linemode */
966 1.1 cgd }
967 1.1 cgd #endif /* LINEMODE */
968 1.1 cgd
969 1.1 cgd /*
970 1.1 cgd * Send along a couple of other options that we wish to negotiate.
971 1.1 cgd */
972 1.1 cgd send_do(TELOPT_NAWS, 1);
973 1.1 cgd send_will(TELOPT_STATUS, 1);
974 1.3 cgd flowmode = 1; /* default flow control state */
975 1.3 cgd restartany = -1; /* uninitialized... */
976 1.1 cgd send_do(TELOPT_LFLOW, 1);
977 1.1 cgd
978 1.1 cgd /*
979 1.1 cgd * Spin, waiting for a response from the DO ECHO. However,
980 1.1 cgd * some REALLY DUMB telnets out there might not respond
981 1.1 cgd * to the DO ECHO. So, we spin looking for NAWS, (most dumb
982 1.1 cgd * telnets so far seem to respond with WONT for a DO that
983 1.1 cgd * they don't understand...) because by the time we get the
984 1.1 cgd * response, it will already have processed the DO ECHO.
985 1.1 cgd * Kludge upon kludge.
986 1.1 cgd */
987 1.1 cgd while (his_will_wont_is_changing(TELOPT_NAWS))
988 1.1 cgd ttloop();
989 1.1 cgd
990 1.1 cgd /*
991 1.1 cgd * But...
992 1.1 cgd * The client might have sent a WILL NAWS as part of its
993 1.1 cgd * startup code; if so, we'll be here before we get the
994 1.1 cgd * response to the DO ECHO. We'll make the assumption
995 1.1 cgd * that any implementation that understands about NAWS
996 1.1 cgd * is a modern enough implementation that it will respond
997 1.1 cgd * to our DO ECHO request; hence we'll do another spin
998 1.1 cgd * waiting for the ECHO option to settle down, which is
999 1.1 cgd * what we wanted to do in the first place...
1000 1.1 cgd */
1001 1.1 cgd if (his_want_state_is_will(TELOPT_ECHO) &&
1002 1.1 cgd his_state_is_will(TELOPT_NAWS)) {
1003 1.1 cgd while (his_will_wont_is_changing(TELOPT_ECHO))
1004 1.1 cgd ttloop();
1005 1.1 cgd }
1006 1.1 cgd /*
1007 1.1 cgd * On the off chance that the telnet client is broken and does not
1008 1.1 cgd * respond to the DO ECHO we sent, (after all, we did send the
1009 1.1 cgd * DO NAWS negotiation after the DO ECHO, and we won't get here
1010 1.1 cgd * until a response to the DO NAWS comes back) simulate the
1011 1.1 cgd * receipt of a will echo. This will also send a WONT ECHO
1012 1.1 cgd * to the client, since we assume that the client failed to
1013 1.1 cgd * respond because it believes that it is already in DO ECHO
1014 1.1 cgd * mode, which we do not want.
1015 1.1 cgd */
1016 1.1 cgd if (his_want_state_is_will(TELOPT_ECHO)) {
1017 1.1 cgd DIAG(TD_OPTIONS,
1018 1.1 cgd {sprintf(nfrontp, "td: simulating recv\r\n");
1019 1.1 cgd nfrontp += strlen(nfrontp);});
1020 1.1 cgd willoption(TELOPT_ECHO);
1021 1.1 cgd }
1022 1.1 cgd
1023 1.1 cgd /*
1024 1.1 cgd * Finally, to clean things up, we turn on our echo. This
1025 1.1 cgd * will break stupid 4.2 telnets out of local terminal echo.
1026 1.1 cgd */
1027 1.1 cgd
1028 1.1 cgd if (my_state_is_wont(TELOPT_ECHO))
1029 1.1 cgd send_will(TELOPT_ECHO, 1);
1030 1.1 cgd
1031 1.3 cgd #ifndef STREAMSPTY
1032 1.1 cgd /*
1033 1.1 cgd * Turn on packet mode
1034 1.1 cgd */
1035 1.1 cgd (void) ioctl(p, TIOCPKT, (char *)&on);
1036 1.3 cgd #endif
1037 1.3 cgd
1038 1.1 cgd #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1039 1.1 cgd /*
1040 1.1 cgd * Continuing line mode support. If client does not support
1041 1.1 cgd * real linemode, attempt to negotiate kludge linemode by sending
1042 1.1 cgd * the do timing mark sequence.
1043 1.1 cgd */
1044 1.1 cgd if (lmodetype < REAL_LINEMODE)
1045 1.1 cgd send_do(TELOPT_TM, 1);
1046 1.1 cgd #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1047 1.1 cgd
1048 1.1 cgd /*
1049 1.1 cgd * Call telrcv() once to pick up anything received during
1050 1.1 cgd * terminal type negotiation, 4.2/4.3 determination, and
1051 1.1 cgd * linemode negotiation.
1052 1.1 cgd */
1053 1.1 cgd telrcv();
1054 1.1 cgd
1055 1.1 cgd (void) ioctl(f, FIONBIO, (char *)&on);
1056 1.1 cgd (void) ioctl(p, FIONBIO, (char *)&on);
1057 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1058 1.1 cgd init_termdriver(f, p, interrupt, sendbrk);
1059 1.1 cgd #endif
1060 1.1 cgd
1061 1.1 cgd #if defined(SO_OOBINLINE)
1062 1.3 cgd (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
1063 1.3 cgd (char *)&on, sizeof on);
1064 1.1 cgd #endif /* defined(SO_OOBINLINE) */
1065 1.1 cgd
1066 1.1 cgd #ifdef SIGTSTP
1067 1.1 cgd (void) signal(SIGTSTP, SIG_IGN);
1068 1.1 cgd #endif
1069 1.1 cgd #ifdef SIGTTOU
1070 1.1 cgd /*
1071 1.1 cgd * Ignoring SIGTTOU keeps the kernel from blocking us
1072 1.1 cgd * in ttioct() in /sys/tty.c.
1073 1.1 cgd */
1074 1.1 cgd (void) signal(SIGTTOU, SIG_IGN);
1075 1.1 cgd #endif
1076 1.1 cgd
1077 1.1 cgd (void) signal(SIGCHLD, cleanup);
1078 1.1 cgd
1079 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1080 1.1 cgd /*
1081 1.1 cgd * Cray-2 will send a signal when pty modes are changed by slave
1082 1.1 cgd * side. Set up signal handler now.
1083 1.1 cgd */
1084 1.1 cgd if ((int)signal(SIGUSR1, termstat) < 0)
1085 1.1 cgd perror("signal");
1086 1.1 cgd else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0)
1087 1.1 cgd perror("ioctl:TCSIGME");
1088 1.1 cgd /*
1089 1.1 cgd * Make processing loop check terminal characteristics early on.
1090 1.1 cgd */
1091 1.1 cgd termstat();
1092 1.1 cgd #endif
1093 1.1 cgd
1094 1.1 cgd #ifdef TIOCNOTTY
1095 1.1 cgd {
1096 1.1 cgd register int t;
1097 1.1 cgd t = open(_PATH_TTY, O_RDWR);
1098 1.1 cgd if (t >= 0) {
1099 1.1 cgd (void) ioctl(t, TIOCNOTTY, (char *)0);
1100 1.1 cgd (void) close(t);
1101 1.1 cgd }
1102 1.1 cgd }
1103 1.1 cgd #endif
1104 1.1 cgd
1105 1.1 cgd #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
1106 1.1 cgd (void) setsid();
1107 1.1 cgd ioctl(p, TIOCSCTTY, 0);
1108 1.1 cgd #endif
1109 1.1 cgd
1110 1.1 cgd /*
1111 1.1 cgd * Show banner that getty never gave.
1112 1.1 cgd *
1113 1.1 cgd * We put the banner in the pty input buffer. This way, it
1114 1.1 cgd * gets carriage return null processing, etc., just like all
1115 1.1 cgd * other pty --> client data.
1116 1.1 cgd */
1117 1.1 cgd
1118 1.1 cgd #if !defined(CRAY) || !defined(NEWINIT)
1119 1.1 cgd if (getenv("USER"))
1120 1.1 cgd hostinfo = 0;
1121 1.1 cgd #endif
1122 1.1 cgd
1123 1.6 tls if (getent(defent, gettyname) == 1) {
1124 1.1 cgd char *cp=defstrs;
1125 1.1 cgd
1126 1.1 cgd HE = getstr("he", &cp);
1127 1.1 cgd HN = getstr("hn", &cp);
1128 1.1 cgd IM = getstr("im", &cp);
1129 1.1 cgd if (HN && *HN)
1130 1.1 cgd (void) strcpy(host_name, HN);
1131 1.1 cgd if (IM == 0)
1132 1.1 cgd IM = "";
1133 1.1 cgd } else {
1134 1.1 cgd IM = DEFAULT_IM;
1135 1.1 cgd HE = 0;
1136 1.1 cgd }
1137 1.1 cgd edithost(HE, host_name);
1138 1.1 cgd if (hostinfo && *IM)
1139 1.1 cgd putf(IM, ptyibuf2);
1140 1.1 cgd
1141 1.1 cgd if (pcc)
1142 1.1 cgd (void) strncat(ptyibuf2, ptyip, pcc+1);
1143 1.1 cgd ptyip = ptyibuf2;
1144 1.1 cgd pcc = strlen(ptyip);
1145 1.1 cgd #ifdef LINEMODE
1146 1.1 cgd /*
1147 1.1 cgd * Last check to make sure all our states are correct.
1148 1.1 cgd */
1149 1.1 cgd init_termbuf();
1150 1.1 cgd localstat();
1151 1.1 cgd #endif /* LINEMODE */
1152 1.1 cgd
1153 1.1 cgd DIAG(TD_REPORT,
1154 1.1 cgd {sprintf(nfrontp, "td: Entering processing loop\r\n");
1155 1.1 cgd nfrontp += strlen(nfrontp);});
1156 1.1 cgd
1157 1.1 cgd #ifdef convex
1158 1.1 cgd startslave(host);
1159 1.1 cgd #endif
1160 1.1 cgd
1161 1.4 jtk nfd = ((f > p) ? f : p) + 1;
1162 1.1 cgd for (;;) {
1163 1.1 cgd fd_set ibits, obits, xbits;
1164 1.1 cgd register int c;
1165 1.1 cgd
1166 1.1 cgd if (ncc < 0 && pcc < 0)
1167 1.1 cgd break;
1168 1.1 cgd
1169 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1170 1.1 cgd if (needtermstat)
1171 1.1 cgd _termstat();
1172 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1173 1.1 cgd FD_ZERO(&ibits);
1174 1.1 cgd FD_ZERO(&obits);
1175 1.1 cgd FD_ZERO(&xbits);
1176 1.1 cgd /*
1177 1.1 cgd * Never look for input if there's still
1178 1.1 cgd * stuff in the corresponding output buffer
1179 1.1 cgd */
1180 1.1 cgd if (nfrontp - nbackp || pcc > 0) {
1181 1.1 cgd FD_SET(f, &obits);
1182 1.1 cgd } else {
1183 1.1 cgd FD_SET(p, &ibits);
1184 1.1 cgd }
1185 1.1 cgd if (pfrontp - pbackp || ncc > 0) {
1186 1.1 cgd FD_SET(p, &obits);
1187 1.1 cgd } else {
1188 1.1 cgd FD_SET(f, &ibits);
1189 1.1 cgd }
1190 1.1 cgd if (!SYNCHing) {
1191 1.1 cgd FD_SET(f, &xbits);
1192 1.1 cgd }
1193 1.4 jtk if ((c = select(nfd, &ibits, &obits, &xbits,
1194 1.1 cgd (struct timeval *)0)) < 1) {
1195 1.1 cgd if (c == -1) {
1196 1.1 cgd if (errno == EINTR) {
1197 1.1 cgd continue;
1198 1.1 cgd }
1199 1.1 cgd }
1200 1.1 cgd sleep(5);
1201 1.1 cgd continue;
1202 1.1 cgd }
1203 1.1 cgd
1204 1.1 cgd /*
1205 1.1 cgd * Any urgent data?
1206 1.1 cgd */
1207 1.1 cgd if (FD_ISSET(net, &xbits)) {
1208 1.1 cgd SYNCHing = 1;
1209 1.1 cgd }
1210 1.1 cgd
1211 1.1 cgd /*
1212 1.1 cgd * Something to read from the network...
1213 1.1 cgd */
1214 1.1 cgd if (FD_ISSET(net, &ibits)) {
1215 1.1 cgd #if !defined(SO_OOBINLINE)
1216 1.1 cgd /*
1217 1.1 cgd * In 4.2 (and 4.3 beta) systems, the
1218 1.1 cgd * OOB indication and data handling in the kernel
1219 1.1 cgd * is such that if two separate TCP Urgent requests
1220 1.1 cgd * come in, one byte of TCP data will be overlaid.
1221 1.1 cgd * This is fatal for Telnet, but we try to live
1222 1.1 cgd * with it.
1223 1.1 cgd *
1224 1.1 cgd * In addition, in 4.2 (and...), a special protocol
1225 1.1 cgd * is needed to pick up the TCP Urgent data in
1226 1.1 cgd * the correct sequence.
1227 1.1 cgd *
1228 1.1 cgd * What we do is: if we think we are in urgent
1229 1.1 cgd * mode, we look to see if we are "at the mark".
1230 1.1 cgd * If we are, we do an OOB receive. If we run
1231 1.1 cgd * this twice, we will do the OOB receive twice,
1232 1.1 cgd * but the second will fail, since the second
1233 1.1 cgd * time we were "at the mark", but there wasn't
1234 1.1 cgd * any data there (the kernel doesn't reset
1235 1.1 cgd * "at the mark" until we do a normal read).
1236 1.1 cgd * Once we've read the OOB data, we go ahead
1237 1.1 cgd * and do normal reads.
1238 1.1 cgd *
1239 1.1 cgd * There is also another problem, which is that
1240 1.1 cgd * since the OOB byte we read doesn't put us
1241 1.1 cgd * out of OOB state, and since that byte is most
1242 1.1 cgd * likely the TELNET DM (data mark), we would
1243 1.1 cgd * stay in the TELNET SYNCH (SYNCHing) state.
1244 1.1 cgd * So, clocks to the rescue. If we've "just"
1245 1.1 cgd * received a DM, then we test for the
1246 1.1 cgd * presence of OOB data when the receive OOB
1247 1.1 cgd * fails (and AFTER we did the normal mode read
1248 1.1 cgd * to clear "at the mark").
1249 1.1 cgd */
1250 1.1 cgd if (SYNCHing) {
1251 1.1 cgd int atmark;
1252 1.1 cgd
1253 1.1 cgd (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1254 1.1 cgd if (atmark) {
1255 1.1 cgd ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1256 1.1 cgd if ((ncc == -1) && (errno == EINVAL)) {
1257 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1258 1.1 cgd if (sequenceIs(didnetreceive, gotDM)) {
1259 1.1 cgd SYNCHing = stilloob(net);
1260 1.1 cgd }
1261 1.1 cgd }
1262 1.1 cgd } else {
1263 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1264 1.1 cgd }
1265 1.1 cgd } else {
1266 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1267 1.1 cgd }
1268 1.1 cgd settimer(didnetreceive);
1269 1.1 cgd #else /* !defined(SO_OOBINLINE)) */
1270 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1271 1.1 cgd #endif /* !defined(SO_OOBINLINE)) */
1272 1.1 cgd if (ncc < 0 && errno == EWOULDBLOCK)
1273 1.1 cgd ncc = 0;
1274 1.1 cgd else {
1275 1.1 cgd if (ncc <= 0) {
1276 1.1 cgd break;
1277 1.1 cgd }
1278 1.1 cgd netip = netibuf;
1279 1.1 cgd }
1280 1.1 cgd DIAG((TD_REPORT | TD_NETDATA),
1281 1.1 cgd {sprintf(nfrontp, "td: netread %d chars\r\n", ncc);
1282 1.1 cgd nfrontp += strlen(nfrontp);});
1283 1.1 cgd DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1284 1.1 cgd }
1285 1.1 cgd
1286 1.1 cgd /*
1287 1.1 cgd * Something to read from the pty...
1288 1.1 cgd */
1289 1.1 cgd if (FD_ISSET(p, &ibits)) {
1290 1.3 cgd #ifndef STREAMSPTY
1291 1.1 cgd pcc = read(p, ptyibuf, BUFSIZ);
1292 1.3 cgd #else
1293 1.3 cgd pcc = readstream(p, ptyibuf, BUFSIZ);
1294 1.3 cgd #endif
1295 1.1 cgd /*
1296 1.1 cgd * On some systems, if we try to read something
1297 1.1 cgd * off the master side before the slave side is
1298 1.1 cgd * opened, we get EIO.
1299 1.1 cgd */
1300 1.3 cgd if (pcc < 0 && (errno == EWOULDBLOCK ||
1301 1.3 cgd #ifdef EAGAIN
1302 1.3 cgd errno == EAGAIN ||
1303 1.3 cgd #endif
1304 1.3 cgd errno == EIO)) {
1305 1.1 cgd pcc = 0;
1306 1.1 cgd } else {
1307 1.1 cgd if (pcc <= 0)
1308 1.1 cgd break;
1309 1.1 cgd #if !defined(CRAY2) || !defined(UNICOS5)
1310 1.1 cgd #ifdef LINEMODE
1311 1.1 cgd /*
1312 1.1 cgd * If ioctl from pty, pass it through net
1313 1.1 cgd */
1314 1.1 cgd if (ptyibuf[0] & TIOCPKT_IOCTL) {
1315 1.1 cgd copy_termbuf(ptyibuf+1, pcc-1);
1316 1.1 cgd localstat();
1317 1.1 cgd pcc = 1;
1318 1.1 cgd }
1319 1.1 cgd #endif /* LINEMODE */
1320 1.1 cgd if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1321 1.1 cgd netclear(); /* clear buffer back */
1322 1.1 cgd #ifndef NO_URGENT
1323 1.1 cgd /*
1324 1.1 cgd * There are client telnets on some
1325 1.1 cgd * operating systems get screwed up
1326 1.1 cgd * royally if we send them urgent
1327 1.1 cgd * mode data.
1328 1.1 cgd */
1329 1.1 cgd *nfrontp++ = IAC;
1330 1.1 cgd *nfrontp++ = DM;
1331 1.1 cgd neturg = nfrontp-1; /* off by one XXX */
1332 1.4 jtk DIAG(TD_OPTIONS,
1333 1.4 jtk printoption("td: send IAC", DM));
1334 1.4 jtk
1335 1.1 cgd #endif
1336 1.1 cgd }
1337 1.1 cgd if (his_state_is_will(TELOPT_LFLOW) &&
1338 1.1 cgd (ptyibuf[0] &
1339 1.1 cgd (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1340 1.3 cgd int newflow =
1341 1.3 cgd ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1342 1.3 cgd if (newflow != flowmode) {
1343 1.3 cgd flowmode = newflow;
1344 1.3 cgd (void) sprintf(nfrontp,
1345 1.3 cgd "%c%c%c%c%c%c",
1346 1.3 cgd IAC, SB, TELOPT_LFLOW,
1347 1.3 cgd flowmode ? LFLOW_ON
1348 1.3 cgd : LFLOW_OFF,
1349 1.3 cgd IAC, SE);
1350 1.3 cgd nfrontp += 6;
1351 1.4 jtk DIAG(TD_OPTIONS, printsub('>',
1352 1.4 jtk (unsigned char *)nfrontp-4,
1353 1.4 jtk 4););
1354 1.3 cgd }
1355 1.1 cgd }
1356 1.1 cgd pcc--;
1357 1.1 cgd ptyip = ptyibuf+1;
1358 1.1 cgd #else /* defined(CRAY2) && defined(UNICOS5) */
1359 1.1 cgd if (!uselinemode) {
1360 1.1 cgd unpcc = pcc;
1361 1.1 cgd unptyip = ptyibuf;
1362 1.1 cgd pcc = term_output(&unptyip, ptyibuf2,
1363 1.1 cgd &unpcc, BUFSIZ);
1364 1.1 cgd ptyip = ptyibuf2;
1365 1.1 cgd } else
1366 1.1 cgd ptyip = ptyibuf;
1367 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1368 1.1 cgd }
1369 1.1 cgd }
1370 1.1 cgd
1371 1.1 cgd while (pcc > 0) {
1372 1.1 cgd if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1373 1.1 cgd break;
1374 1.1 cgd c = *ptyip++ & 0377, pcc--;
1375 1.1 cgd if (c == IAC)
1376 1.1 cgd *nfrontp++ = c;
1377 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1378 1.1 cgd else if (c == '\n' &&
1379 1.1 cgd my_state_is_wont(TELOPT_BINARY) && newmap)
1380 1.1 cgd *nfrontp++ = '\r';
1381 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1382 1.1 cgd *nfrontp++ = c;
1383 1.1 cgd if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1384 1.1 cgd if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1385 1.1 cgd *nfrontp++ = *ptyip++ & 0377;
1386 1.1 cgd pcc--;
1387 1.1 cgd } else
1388 1.1 cgd *nfrontp++ = '\0';
1389 1.1 cgd }
1390 1.1 cgd }
1391 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1392 1.1 cgd /*
1393 1.1 cgd * If chars were left over from the terminal driver,
1394 1.1 cgd * note their existence.
1395 1.1 cgd */
1396 1.1 cgd if (!uselinemode && unpcc) {
1397 1.1 cgd pcc = unpcc;
1398 1.1 cgd unpcc = 0;
1399 1.1 cgd ptyip = unptyip;
1400 1.1 cgd }
1401 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1402 1.1 cgd
1403 1.1 cgd if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1404 1.1 cgd netflush();
1405 1.1 cgd if (ncc > 0)
1406 1.1 cgd telrcv();
1407 1.1 cgd if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1408 1.1 cgd ptyflush();
1409 1.1 cgd }
1410 1.1 cgd cleanup(0);
1411 1.1 cgd } /* end of telnet */
1412 1.4 jtk
1413 1.1 cgd #ifndef TCSIG
1414 1.1 cgd # ifdef TIOCSIG
1415 1.1 cgd # define TCSIG TIOCSIG
1416 1.1 cgd # endif
1417 1.1 cgd #endif
1418 1.3 cgd
1419 1.3 cgd #ifdef STREAMSPTY
1420 1.3 cgd
1421 1.3 cgd int flowison = -1; /* current state of flow: -1 is unknown */
1422 1.3 cgd
1423 1.3 cgd int readstream(p, ibuf, bufsize)
1424 1.3 cgd int p;
1425 1.3 cgd char *ibuf;
1426 1.3 cgd int bufsize;
1427 1.3 cgd {
1428 1.3 cgd int flags = 0;
1429 1.3 cgd int ret = 0;
1430 1.3 cgd struct termios *tsp;
1431 1.3 cgd struct termio *tp;
1432 1.3 cgd struct iocblk *ip;
1433 1.3 cgd char vstop, vstart;
1434 1.3 cgd int ixon;
1435 1.3 cgd int newflow;
1436 1.3 cgd
1437 1.3 cgd strbufc.maxlen = BUFSIZ;
1438 1.3 cgd strbufc.buf = (char *)ctlbuf;
1439 1.3 cgd strbufd.maxlen = bufsize-1;
1440 1.3 cgd strbufd.len = 0;
1441 1.3 cgd strbufd.buf = ibuf+1;
1442 1.3 cgd ibuf[0] = 0;
1443 1.3 cgd
1444 1.3 cgd ret = getmsg(p, &strbufc, &strbufd, &flags);
1445 1.3 cgd if (ret < 0) /* error of some sort -- probably EAGAIN */
1446 1.3 cgd return(-1);
1447 1.3 cgd
1448 1.3 cgd if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
1449 1.3 cgd /* data message */
1450 1.3 cgd if (strbufd.len > 0) { /* real data */
1451 1.3 cgd return(strbufd.len + 1); /* count header char */
1452 1.3 cgd } else {
1453 1.3 cgd /* nothing there */
1454 1.3 cgd errno = EAGAIN;
1455 1.3 cgd return(-1);
1456 1.3 cgd }
1457 1.3 cgd }
1458 1.3 cgd
1459 1.3 cgd /*
1460 1.3 cgd * It's a control message. Return 1, to look at the flag we set
1461 1.3 cgd */
1462 1.3 cgd
1463 1.3 cgd switch (ctlbuf[0]) {
1464 1.3 cgd case M_FLUSH:
1465 1.3 cgd if (ibuf[1] & FLUSHW)
1466 1.3 cgd ibuf[0] = TIOCPKT_FLUSHWRITE;
1467 1.3 cgd return(1);
1468 1.3 cgd
1469 1.3 cgd case M_IOCTL:
1470 1.3 cgd ip = (struct iocblk *) (ibuf+1);
1471 1.3 cgd
1472 1.3 cgd switch (ip->ioc_cmd) {
1473 1.3 cgd case TCSETS:
1474 1.3 cgd case TCSETSW:
1475 1.3 cgd case TCSETSF:
1476 1.3 cgd tsp = (struct termios *)
1477 1.3 cgd (ibuf+1 + sizeof(struct iocblk));
1478 1.3 cgd vstop = tsp->c_cc[VSTOP];
1479 1.3 cgd vstart = tsp->c_cc[VSTART];
1480 1.3 cgd ixon = tsp->c_iflag & IXON;
1481 1.3 cgd break;
1482 1.3 cgd case TCSETA:
1483 1.3 cgd case TCSETAW:
1484 1.3 cgd case TCSETAF:
1485 1.3 cgd tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
1486 1.3 cgd vstop = tp->c_cc[VSTOP];
1487 1.3 cgd vstart = tp->c_cc[VSTART];
1488 1.4 jtk ixon = tp->c_iflag & IXON;
1489 1.3 cgd break;
1490 1.3 cgd default:
1491 1.3 cgd errno = EAGAIN;
1492 1.3 cgd return(-1);
1493 1.3 cgd }
1494 1.3 cgd
1495 1.3 cgd newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
1496 1.3 cgd if (newflow != flowison) { /* it's a change */
1497 1.3 cgd flowison = newflow;
1498 1.3 cgd ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
1499 1.3 cgd return(1);
1500 1.3 cgd }
1501 1.3 cgd }
1502 1.3 cgd
1503 1.3 cgd /* nothing worth doing anything about */
1504 1.3 cgd errno = EAGAIN;
1505 1.3 cgd return(-1);
1506 1.3 cgd }
1507 1.3 cgd #endif /* STREAMSPTY */
1508 1.1 cgd
1509 1.1 cgd /*
1510 1.1 cgd * Send interrupt to process on other side of pty.
1511 1.1 cgd * If it is in raw mode, just write NULL;
1512 1.1 cgd * otherwise, write intr char.
1513 1.1 cgd */
1514 1.1 cgd void
1515 1.1 cgd interrupt()
1516 1.1 cgd {
1517 1.1 cgd ptyflush(); /* half-hearted */
1518 1.1 cgd
1519 1.4 jtk #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1520 1.4 jtk /* Streams PTY style ioctl to post a signal */
1521 1.4 jtk {
1522 1.4 jtk int sig = SIGINT;
1523 1.4 jtk (void) ioctl(pty, TIOCSIGNAL, &sig);
1524 1.4 jtk (void) ioctl(pty, I_FLUSH, FLUSHR);
1525 1.4 jtk }
1526 1.4 jtk #else
1527 1.1 cgd #ifdef TCSIG
1528 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGINT);
1529 1.1 cgd #else /* TCSIG */
1530 1.1 cgd init_termbuf();
1531 1.1 cgd *pfrontp++ = slctab[SLC_IP].sptr ?
1532 1.1 cgd (unsigned char)*slctab[SLC_IP].sptr : '\177';
1533 1.1 cgd #endif /* TCSIG */
1534 1.4 jtk #endif
1535 1.1 cgd }
1536 1.1 cgd
1537 1.1 cgd /*
1538 1.1 cgd * Send quit to process on other side of pty.
1539 1.1 cgd * If it is in raw mode, just write NULL;
1540 1.1 cgd * otherwise, write quit char.
1541 1.1 cgd */
1542 1.1 cgd void
1543 1.1 cgd sendbrk()
1544 1.1 cgd {
1545 1.1 cgd ptyflush(); /* half-hearted */
1546 1.1 cgd #ifdef TCSIG
1547 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1548 1.1 cgd #else /* TCSIG */
1549 1.1 cgd init_termbuf();
1550 1.1 cgd *pfrontp++ = slctab[SLC_ABORT].sptr ?
1551 1.1 cgd (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1552 1.1 cgd #endif /* TCSIG */
1553 1.1 cgd }
1554 1.1 cgd
1555 1.1 cgd void
1556 1.1 cgd sendsusp()
1557 1.1 cgd {
1558 1.1 cgd #ifdef SIGTSTP
1559 1.1 cgd ptyflush(); /* half-hearted */
1560 1.1 cgd # ifdef TCSIG
1561 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1562 1.1 cgd # else /* TCSIG */
1563 1.1 cgd *pfrontp++ = slctab[SLC_SUSP].sptr ?
1564 1.1 cgd (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1565 1.1 cgd # endif /* TCSIG */
1566 1.1 cgd #endif /* SIGTSTP */
1567 1.1 cgd }
1568 1.1 cgd
1569 1.1 cgd /*
1570 1.1 cgd * When we get an AYT, if ^T is enabled, use that. Otherwise,
1571 1.1 cgd * just send back "[Yes]".
1572 1.1 cgd */
1573 1.1 cgd void
1574 1.1 cgd recv_ayt()
1575 1.1 cgd {
1576 1.1 cgd #if defined(SIGINFO) && defined(TCSIG)
1577 1.1 cgd if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1578 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1579 1.1 cgd return;
1580 1.1 cgd }
1581 1.1 cgd #endif
1582 1.1 cgd (void) strcpy(nfrontp, "\r\n[Yes]\r\n");
1583 1.1 cgd nfrontp += 9;
1584 1.1 cgd }
1585 1.1 cgd
1586 1.1 cgd void
1587 1.1 cgd doeof()
1588 1.1 cgd {
1589 1.1 cgd init_termbuf();
1590 1.1 cgd
1591 1.1 cgd #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1592 1.1 cgd if (!tty_isediting()) {
1593 1.1 cgd extern char oldeofc;
1594 1.1 cgd *pfrontp++ = oldeofc;
1595 1.1 cgd return;
1596 1.1 cgd }
1597 1.1 cgd #endif
1598 1.1 cgd *pfrontp++ = slctab[SLC_EOF].sptr ?
1599 1.1 cgd (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1600 1.1 cgd }
1601