telnetd.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1989 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)telnetd.c 5.48 (Berkeley) 3/1/91";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include "telnetd.h"
45 1.1 cgd #include "pathnames.h"
46 1.1 cgd
47 1.1 cgd #if defined(AUTHENTICATE)
48 1.1 cgd #include <libtelnet/auth.h>
49 1.1 cgd int auth_level = 0;
50 1.1 cgd #endif
51 1.1 cgd #if defined(SecurID)
52 1.1 cgd int require_SecurID = 0;
53 1.1 cgd #endif
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * I/O data buffers,
57 1.1 cgd * pointers, and counters.
58 1.1 cgd */
59 1.1 cgd char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
60 1.1 cgd char ptyibuf2[BUFSIZ];
61 1.1 cgd
62 1.1 cgd int hostinfo = 1; /* do we print login banner? */
63 1.1 cgd
64 1.1 cgd #ifdef CRAY
65 1.1 cgd extern int newmap; /* nonzero if \n maps to ^M^J */
66 1.1 cgd int lowpty = 0, highpty; /* low, high pty numbers */
67 1.1 cgd #endif /* CRAY */
68 1.1 cgd
69 1.1 cgd int debug = 0;
70 1.1 cgd int keepalive = 1;
71 1.1 cgd char *progname;
72 1.1 cgd
73 1.1 cgd extern void usage P((void));
74 1.1 cgd
75 1.1 cgd main(argc, argv)
76 1.1 cgd char *argv[];
77 1.1 cgd {
78 1.1 cgd struct sockaddr_in from;
79 1.1 cgd int on = 1, fromlen;
80 1.1 cgd register int ch;
81 1.1 cgd extern char *optarg;
82 1.1 cgd extern int optind;
83 1.1 cgd #if defined(IPPROTO_IP) && defined(IP_TOS)
84 1.1 cgd int tos = -1;
85 1.1 cgd #endif
86 1.1 cgd
87 1.1 cgd pfrontp = pbackp = ptyobuf;
88 1.1 cgd netip = netibuf;
89 1.1 cgd nfrontp = nbackp = netobuf;
90 1.1 cgd #if defined(ENCRYPT)
91 1.1 cgd nclearto = 0;
92 1.1 cgd #endif
93 1.1 cgd
94 1.1 cgd progname = *argv;
95 1.1 cgd
96 1.1 cgd #ifdef CRAY
97 1.1 cgd /*
98 1.1 cgd * Get number of pty's before trying to process options,
99 1.1 cgd * which may include changing pty range.
100 1.1 cgd */
101 1.1 cgd highpty = getnpty();
102 1.1 cgd #endif /* CRAY */
103 1.1 cgd
104 1.1 cgd while ((ch = getopt(argc, argv, "d:a:e:lhnr:I:D:B:sS:a:X:")) != EOF) {
105 1.1 cgd switch(ch) {
106 1.1 cgd
107 1.1 cgd #ifdef AUTHENTICATE
108 1.1 cgd case 'a':
109 1.1 cgd /*
110 1.1 cgd * Check for required authentication level
111 1.1 cgd */
112 1.1 cgd if (strcmp(optarg, "debug") == 0) {
113 1.1 cgd extern int auth_debug_mode;
114 1.1 cgd auth_debug_mode = 1;
115 1.1 cgd } else if (strcasecmp(optarg, "none") == 0) {
116 1.1 cgd auth_level = 0;
117 1.1 cgd } else if (strcasecmp(optarg, "other") == 0) {
118 1.1 cgd auth_level = AUTH_OTHER;
119 1.1 cgd } else if (strcasecmp(optarg, "user") == 0) {
120 1.1 cgd auth_level = AUTH_USER;
121 1.1 cgd } else if (strcasecmp(optarg, "valid") == 0) {
122 1.1 cgd auth_level = AUTH_VALID;
123 1.1 cgd } else if (strcasecmp(optarg, "off") == 0) {
124 1.1 cgd /*
125 1.1 cgd * This hack turns off authentication
126 1.1 cgd */
127 1.1 cgd auth_level = -1;
128 1.1 cgd } else {
129 1.1 cgd fprintf(stderr,
130 1.1 cgd "telnetd: unknown authorization level for -a\n");
131 1.1 cgd }
132 1.1 cgd break;
133 1.1 cgd #endif /* AUTHENTICATE */
134 1.1 cgd
135 1.1 cgd #ifdef BFTPDAEMON
136 1.1 cgd case 'B':
137 1.1 cgd bftpd++;
138 1.1 cgd break;
139 1.1 cgd #endif /* BFTPDAEMON */
140 1.1 cgd
141 1.1 cgd case 'd':
142 1.1 cgd if (strcmp(optarg, "ebug") == 0) {
143 1.1 cgd debug++;
144 1.1 cgd break;
145 1.1 cgd }
146 1.1 cgd usage();
147 1.1 cgd /* NOTREACHED */
148 1.1 cgd break;
149 1.1 cgd
150 1.1 cgd #ifdef DIAGNOSTICS
151 1.1 cgd case 'D':
152 1.1 cgd /*
153 1.1 cgd * Check for desired diagnostics capabilities.
154 1.1 cgd */
155 1.1 cgd if (!strcmp(optarg, "report")) {
156 1.1 cgd diagnostic |= TD_REPORT|TD_OPTIONS;
157 1.1 cgd } else if (!strcmp(optarg, "exercise")) {
158 1.1 cgd diagnostic |= TD_EXERCISE;
159 1.1 cgd } else if (!strcmp(optarg, "netdata")) {
160 1.1 cgd diagnostic |= TD_NETDATA;
161 1.1 cgd } else if (!strcmp(optarg, "ptydata")) {
162 1.1 cgd diagnostic |= TD_PTYDATA;
163 1.1 cgd } else if (!strcmp(optarg, "options")) {
164 1.1 cgd diagnostic |= TD_OPTIONS;
165 1.1 cgd } else {
166 1.1 cgd usage();
167 1.1 cgd /* NOT REACHED */
168 1.1 cgd }
169 1.1 cgd break;
170 1.1 cgd #endif /* DIAGNOSTICS */
171 1.1 cgd
172 1.1 cgd #ifdef AUTHENTICATE
173 1.1 cgd case 'e':
174 1.1 cgd if (strcmp(optarg, "debug") == 0) {
175 1.1 cgd extern int encrypt_debug_mode;
176 1.1 cgd encrypt_debug_mode = 1;
177 1.1 cgd break;
178 1.1 cgd }
179 1.1 cgd usage();
180 1.1 cgd /* NOTREACHED */
181 1.1 cgd break;
182 1.1 cgd #endif /* AUTHENTICATE */
183 1.1 cgd
184 1.1 cgd case 'h':
185 1.1 cgd hostinfo = 0;
186 1.1 cgd break;
187 1.1 cgd
188 1.1 cgd #if defined(CRAY) && defined(NEWINIT)
189 1.1 cgd case 'I':
190 1.1 cgd {
191 1.1 cgd extern char *gen_id;
192 1.1 cgd gen_id = optarg;
193 1.1 cgd break;
194 1.1 cgd }
195 1.1 cgd #endif /* defined(CRAY) && defined(NEWINIT) */
196 1.1 cgd
197 1.1 cgd #ifdef LINEMODE
198 1.1 cgd case 'l':
199 1.1 cgd alwayslinemode = 1;
200 1.1 cgd break;
201 1.1 cgd #endif /* LINEMODE */
202 1.1 cgd
203 1.1 cgd case 'n':
204 1.1 cgd keepalive = 0;
205 1.1 cgd break;
206 1.1 cgd
207 1.1 cgd #ifdef CRAY
208 1.1 cgd case 'r':
209 1.1 cgd {
210 1.1 cgd char *strchr();
211 1.1 cgd char *c;
212 1.1 cgd
213 1.1 cgd /*
214 1.1 cgd * Allow the specification of alterations
215 1.1 cgd * to the pty search range. It is legal to
216 1.1 cgd * specify only one, and not change the
217 1.1 cgd * other from its default.
218 1.1 cgd */
219 1.1 cgd c = strchr(optarg, '-');
220 1.1 cgd if (c) {
221 1.1 cgd *c++ = '\0';
222 1.1 cgd highpty = atoi(c);
223 1.1 cgd }
224 1.1 cgd if (*optarg != '\0')
225 1.1 cgd lowpty = atoi(optarg);
226 1.1 cgd if ((lowpty > highpty) || (lowpty < 0) ||
227 1.1 cgd (highpty > 32767)) {
228 1.1 cgd usage();
229 1.1 cgd /* NOT REACHED */
230 1.1 cgd }
231 1.1 cgd break;
232 1.1 cgd }
233 1.1 cgd #endif /* CRAY */
234 1.1 cgd
235 1.1 cgd #ifdef SecurID
236 1.1 cgd case 's':
237 1.1 cgd /* SecurID required */
238 1.1 cgd require_SecurID = 1;
239 1.1 cgd break;
240 1.1 cgd #endif /* SecurID */
241 1.1 cgd case 'S':
242 1.1 cgd #ifdef HAS_GETTOS
243 1.1 cgd if ((tos = parsetos(optarg, "tcp")) < 0)
244 1.1 cgd fprintf(stderr, "%s%s%s\n",
245 1.1 cgd "telnetd: Bad TOS argument '", optarg,
246 1.1 cgd "'; will try to use default TOS");
247 1.1 cgd #else
248 1.1 cgd fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
249 1.1 cgd "-S flag not supported\n");
250 1.1 cgd #endif
251 1.1 cgd break;
252 1.1 cgd
253 1.1 cgd #ifdef AUTHENTICATE
254 1.1 cgd case 'X':
255 1.1 cgd /*
256 1.1 cgd * Check for invalid authentication types
257 1.1 cgd */
258 1.1 cgd auth_disable_name(optarg);
259 1.1 cgd break;
260 1.1 cgd #endif /* AUTHENTICATE */
261 1.1 cgd
262 1.1 cgd default:
263 1.1 cgd fprintf(stderr, "telnetd: %s: unknown option\n", ch);
264 1.1 cgd /* FALLTHROUGH */
265 1.1 cgd case '?':
266 1.1 cgd usage();
267 1.1 cgd /* NOTREACHED */
268 1.1 cgd }
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd argc -= optind;
272 1.1 cgd argv += optind;
273 1.1 cgd
274 1.1 cgd if (debug) {
275 1.1 cgd int s, ns, foo;
276 1.1 cgd struct servent *sp;
277 1.1 cgd static struct sockaddr_in sin = { AF_INET };
278 1.1 cgd
279 1.1 cgd if (argc > 1) {
280 1.1 cgd usage();
281 1.1 cgd /* NOT REACHED */
282 1.1 cgd } else if (argc == 1) {
283 1.1 cgd if (sp = getservbyname(*argv, "tcp")) {
284 1.1 cgd sin.sin_port = sp->s_port;
285 1.1 cgd } else {
286 1.1 cgd sin.sin_port = atoi(*argv);
287 1.1 cgd if ((int)sin.sin_port <= 0) {
288 1.1 cgd fprintf(stderr, "telnetd: %s: bad port #\n", *argv);
289 1.1 cgd usage();
290 1.1 cgd /* NOT REACHED */
291 1.1 cgd }
292 1.1 cgd sin.sin_port = htons((u_short)sin.sin_port);
293 1.1 cgd }
294 1.1 cgd } else {
295 1.1 cgd sp = getservbyname("telnet", "tcp");
296 1.1 cgd if (sp == 0) {
297 1.1 cgd fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
298 1.1 cgd exit(1);
299 1.1 cgd }
300 1.1 cgd sin.sin_port = sp->s_port;
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd s = socket(AF_INET, SOCK_STREAM, 0);
304 1.1 cgd if (s < 0) {
305 1.1 cgd perror("telnetd: socket");;
306 1.1 cgd exit(1);
307 1.1 cgd }
308 1.1 cgd (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
309 1.1 cgd if (bind(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
310 1.1 cgd perror("bind");
311 1.1 cgd exit(1);
312 1.1 cgd }
313 1.1 cgd if (listen(s, 1) < 0) {
314 1.1 cgd perror("listen");
315 1.1 cgd exit(1);
316 1.1 cgd }
317 1.1 cgd foo = sizeof sin;
318 1.1 cgd ns = accept(s, (struct sockaddr *)&sin, &foo);
319 1.1 cgd if (ns < 0) {
320 1.1 cgd perror("accept");
321 1.1 cgd exit(1);
322 1.1 cgd }
323 1.1 cgd (void) dup2(ns, 0);
324 1.1 cgd (void) close(ns);
325 1.1 cgd (void) close(s);
326 1.1 cgd #ifdef convex
327 1.1 cgd } else if (argc == 1) {
328 1.1 cgd ; /* VOID*/ /* Just ignore the host/port name */
329 1.1 cgd #endif
330 1.1 cgd } else if (argc > 0) {
331 1.1 cgd usage();
332 1.1 cgd /* NOT REACHED */
333 1.1 cgd }
334 1.1 cgd
335 1.1 cgd openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
336 1.1 cgd fromlen = sizeof (from);
337 1.1 cgd if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
338 1.1 cgd fprintf(stderr, "%s: ", progname);
339 1.1 cgd perror("getpeername");
340 1.1 cgd _exit(1);
341 1.1 cgd }
342 1.1 cgd if (keepalive &&
343 1.1 cgd setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
344 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
345 1.1 cgd }
346 1.1 cgd
347 1.1 cgd #if defined(IPPROTO_IP) && defined(IP_TOS)
348 1.1 cgd {
349 1.1 cgd # if defined(HAS_GETTOS)
350 1.1 cgd struct tosent *tp;
351 1.1 cgd if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
352 1.1 cgd tos = tp->t_tos;
353 1.1 cgd # endif
354 1.1 cgd if (tos < 0)
355 1.1 cgd tos = 020; /* Low Delay bit */
356 1.1 cgd if (tos
357 1.1 cgd && (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
358 1.1 cgd && (errno != ENOPROTOOPT) )
359 1.1 cgd syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
360 1.1 cgd }
361 1.1 cgd #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
362 1.1 cgd net = 0;
363 1.1 cgd doit(&from);
364 1.1 cgd /* NOTREACHED */
365 1.1 cgd } /* end of main */
366 1.1 cgd
367 1.1 cgd void
368 1.1 cgd usage()
369 1.1 cgd {
370 1.1 cgd fprintf(stderr, "Usage: telnetd");
371 1.1 cgd #ifdef AUTHENTICATE
372 1.1 cgd fprintf(stderr, " [-a (debug|other|user|valid|off)]\n\t");
373 1.1 cgd #endif
374 1.1 cgd #ifdef BFTPDAEMON
375 1.1 cgd fprintf(stderr, " [-B]");
376 1.1 cgd #endif
377 1.1 cgd fprintf(stderr, " [-debug]");
378 1.1 cgd #ifdef DIAGNOSTICS
379 1.1 cgd fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
380 1.1 cgd #endif
381 1.1 cgd #ifdef AUTHENTICATE
382 1.1 cgd fprintf(stderr, " [-edebug]");
383 1.1 cgd #endif
384 1.1 cgd fprintf(stderr, " [-h]");
385 1.1 cgd #if defined(CRAY) && defined(NEWINIT)
386 1.1 cgd fprintf(stderr, " [-Iinitid]");
387 1.1 cgd #endif
388 1.1 cgd #ifdef LINEMODE
389 1.1 cgd fprintf(stderr, " [-l]");
390 1.1 cgd #endif
391 1.1 cgd fprintf(stderr, " [-n]");
392 1.1 cgd #ifdef CRAY
393 1.1 cgd fprintf(stderr, " [-r[lowpty]-[highpty]]");
394 1.1 cgd #endif
395 1.1 cgd #ifdef SecurID
396 1.1 cgd fprintf(stderr, " [-s]");
397 1.1 cgd #endif
398 1.1 cgd #ifdef AUTHENTICATE
399 1.1 cgd fprintf(stderr, " [-X auth-type]");
400 1.1 cgd #endif
401 1.1 cgd fprintf(stderr, " [port]\n");
402 1.1 cgd exit(1);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd /*
406 1.1 cgd * getterminaltype
407 1.1 cgd *
408 1.1 cgd * Ask the other end to send along its terminal type and speed.
409 1.1 cgd * Output is the variable terminaltype filled in.
410 1.1 cgd */
411 1.1 cgd static char ttytype_sbbuf[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE };
412 1.1 cgd
413 1.1 cgd int
414 1.1 cgd getterminaltype(name)
415 1.1 cgd char *name;
416 1.1 cgd {
417 1.1 cgd int retval = -1;
418 1.1 cgd void _gettermname();
419 1.1 cgd
420 1.1 cgd settimer(baseline);
421 1.1 cgd #if defined(AUTHENTICATE)
422 1.1 cgd /*
423 1.1 cgd * Handle the Authentication option before we do anything else.
424 1.1 cgd */
425 1.1 cgd send_do(TELOPT_AUTHENTICATION, 1);
426 1.1 cgd while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
427 1.1 cgd ttloop();
428 1.1 cgd if (his_state_is_will(TELOPT_AUTHENTICATION)) {
429 1.1 cgd retval = auth_wait(name);
430 1.1 cgd }
431 1.1 cgd #endif
432 1.1 cgd
433 1.1 cgd #if defined(ENCRYPT)
434 1.1 cgd send_will(TELOPT_ENCRYPT, 1);
435 1.1 cgd #endif
436 1.1 cgd send_do(TELOPT_TTYPE, 1);
437 1.1 cgd send_do(TELOPT_TSPEED, 1);
438 1.1 cgd send_do(TELOPT_XDISPLOC, 1);
439 1.1 cgd send_do(TELOPT_ENVIRON, 1);
440 1.1 cgd while (
441 1.1 cgd #if defined(ENCRYPT)
442 1.1 cgd his_do_dont_is_changing(TELOPT_ENCRYPT) ||
443 1.1 cgd #endif
444 1.1 cgd his_will_wont_is_changing(TELOPT_TTYPE) ||
445 1.1 cgd his_will_wont_is_changing(TELOPT_TSPEED) ||
446 1.1 cgd his_will_wont_is_changing(TELOPT_XDISPLOC) ||
447 1.1 cgd his_will_wont_is_changing(TELOPT_ENVIRON)) {
448 1.1 cgd ttloop();
449 1.1 cgd }
450 1.1 cgd #if defined(ENCRYPT)
451 1.1 cgd /*
452 1.1 cgd * Wait for the negotiation of what type of encryption we can
453 1.1 cgd * send with. If autoencrypt is not set, this will just return.
454 1.1 cgd */
455 1.1 cgd if (his_state_is_will(TELOPT_ENCRYPT)) {
456 1.1 cgd encrypt_wait();
457 1.1 cgd }
458 1.1 cgd #endif
459 1.1 cgd if (his_state_is_will(TELOPT_TSPEED)) {
460 1.1 cgd static char sbbuf[] = { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
461 1.1 cgd
462 1.1 cgd bcopy(sbbuf, nfrontp, sizeof sbbuf);
463 1.1 cgd nfrontp += sizeof sbbuf;
464 1.1 cgd }
465 1.1 cgd if (his_state_is_will(TELOPT_XDISPLOC)) {
466 1.1 cgd static char sbbuf[] = { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
467 1.1 cgd
468 1.1 cgd bcopy(sbbuf, nfrontp, sizeof sbbuf);
469 1.1 cgd nfrontp += sizeof sbbuf;
470 1.1 cgd }
471 1.1 cgd if (his_state_is_will(TELOPT_ENVIRON)) {
472 1.1 cgd static char sbbuf[] = { IAC, SB, TELOPT_ENVIRON, TELQUAL_SEND, IAC, SE };
473 1.1 cgd
474 1.1 cgd bcopy(sbbuf, nfrontp, sizeof sbbuf);
475 1.1 cgd nfrontp += sizeof sbbuf;
476 1.1 cgd }
477 1.1 cgd if (his_state_is_will(TELOPT_TTYPE)) {
478 1.1 cgd
479 1.1 cgd bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf);
480 1.1 cgd nfrontp += sizeof ttytype_sbbuf;
481 1.1 cgd }
482 1.1 cgd if (his_state_is_will(TELOPT_TSPEED)) {
483 1.1 cgd while (sequenceIs(tspeedsubopt, baseline))
484 1.1 cgd ttloop();
485 1.1 cgd }
486 1.1 cgd if (his_state_is_will(TELOPT_XDISPLOC)) {
487 1.1 cgd while (sequenceIs(xdisplocsubopt, baseline))
488 1.1 cgd ttloop();
489 1.1 cgd }
490 1.1 cgd if (his_state_is_will(TELOPT_ENVIRON)) {
491 1.1 cgd while (sequenceIs(environsubopt, baseline))
492 1.1 cgd ttloop();
493 1.1 cgd }
494 1.1 cgd if (his_state_is_will(TELOPT_TTYPE)) {
495 1.1 cgd char first[256], last[256];
496 1.1 cgd
497 1.1 cgd while (sequenceIs(ttypesubopt, baseline))
498 1.1 cgd ttloop();
499 1.1 cgd
500 1.1 cgd /*
501 1.1 cgd * If the other side has already disabled the option, then
502 1.1 cgd * we have to just go with what we (might) have already gotten.
503 1.1 cgd */
504 1.1 cgd if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
505 1.1 cgd (void) strncpy(first, terminaltype, sizeof(first));
506 1.1 cgd for(;;) {
507 1.1 cgd /*
508 1.1 cgd * Save the unknown name, and request the next name.
509 1.1 cgd */
510 1.1 cgd (void) strncpy(last, terminaltype, sizeof(last));
511 1.1 cgd _gettermname();
512 1.1 cgd if (terminaltypeok(terminaltype))
513 1.1 cgd break;
514 1.1 cgd if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
515 1.1 cgd his_state_is_wont(TELOPT_TTYPE)) {
516 1.1 cgd /*
517 1.1 cgd * We've hit the end. If this is the same as
518 1.1 cgd * the first name, just go with it.
519 1.1 cgd */
520 1.1 cgd if (strncmp(first, terminaltype, sizeof(first)) == 0)
521 1.1 cgd break;
522 1.1 cgd /*
523 1.1 cgd * Get the terminal name one more time, so that
524 1.1 cgd * RFC1091 compliant telnets will cycle back to
525 1.1 cgd * the start of the list.
526 1.1 cgd */
527 1.1 cgd _gettermname();
528 1.1 cgd if (strncmp(first, terminaltype, sizeof(first)) != 0)
529 1.1 cgd (void) strncpy(terminaltype, first, sizeof(first));
530 1.1 cgd break;
531 1.1 cgd }
532 1.1 cgd }
533 1.1 cgd }
534 1.1 cgd }
535 1.1 cgd return(retval);
536 1.1 cgd } /* end of getterminaltype */
537 1.1 cgd
538 1.1 cgd void
539 1.1 cgd _gettermname()
540 1.1 cgd {
541 1.1 cgd /*
542 1.1 cgd * If the client turned off the option,
543 1.1 cgd * we can't send another request, so we
544 1.1 cgd * just return.
545 1.1 cgd */
546 1.1 cgd if (his_state_is_wont(TELOPT_TTYPE))
547 1.1 cgd return;
548 1.1 cgd settimer(baseline);
549 1.1 cgd bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf);
550 1.1 cgd nfrontp += sizeof ttytype_sbbuf;
551 1.1 cgd while (sequenceIs(ttypesubopt, baseline))
552 1.1 cgd ttloop();
553 1.1 cgd }
554 1.1 cgd
555 1.1 cgd int
556 1.1 cgd terminaltypeok(s)
557 1.1 cgd char *s;
558 1.1 cgd {
559 1.1 cgd char buf[1024];
560 1.1 cgd
561 1.1 cgd if (terminaltype == NULL)
562 1.1 cgd return(1);
563 1.1 cgd
564 1.1 cgd /*
565 1.1 cgd * tgetent() will return 1 if the type is known, and
566 1.1 cgd * 0 if it is not known. If it returns -1, it couldn't
567 1.1 cgd * open the database. But if we can't open the database,
568 1.1 cgd * it won't help to say we failed, because we won't be
569 1.1 cgd * able to verify anything else. So, we treat -1 like 1.
570 1.1 cgd */
571 1.1 cgd if (tgetent(buf, s) == 0)
572 1.1 cgd return(0);
573 1.1 cgd return(1);
574 1.1 cgd }
575 1.1 cgd
576 1.1 cgd #ifndef MAXHOSTNAMELEN
577 1.1 cgd #define MAXHOSTNAMELEN 64
578 1.1 cgd #endif /* MAXHOSTNAMELEN */
579 1.1 cgd
580 1.1 cgd char *hostname;
581 1.1 cgd char host_name[MAXHOSTNAMELEN];
582 1.1 cgd char remote_host_name[MAXHOSTNAMELEN];
583 1.1 cgd
584 1.1 cgd #ifndef convex
585 1.1 cgd extern void telnet P((int, int));
586 1.1 cgd #else
587 1.1 cgd extern void telnet P((int, int, char *));
588 1.1 cgd #endif
589 1.1 cgd
590 1.1 cgd /*
591 1.1 cgd * Get a pty, scan input lines.
592 1.1 cgd */
593 1.1 cgd doit(who)
594 1.1 cgd struct sockaddr_in *who;
595 1.1 cgd {
596 1.1 cgd char *host, *inet_ntoa();
597 1.1 cgd int t;
598 1.1 cgd struct hostent *hp;
599 1.1 cgd int level;
600 1.1 cgd char user_name[256];
601 1.1 cgd
602 1.1 cgd /*
603 1.1 cgd * Find an available pty to use.
604 1.1 cgd */
605 1.1 cgd #ifndef convex
606 1.1 cgd pty = getpty();
607 1.1 cgd if (pty < 0)
608 1.1 cgd fatal(net, "All network ports in use");
609 1.1 cgd #else
610 1.1 cgd for (;;) {
611 1.1 cgd char *lp;
612 1.1 cgd extern char *line, *getpty();
613 1.1 cgd
614 1.1 cgd if ((lp = getpty()) == NULL)
615 1.1 cgd fatal(net, "Out of ptys");
616 1.1 cgd
617 1.1 cgd if ((pty = open(lp, 2)) >= 0) {
618 1.1 cgd strcpy(line,lp);
619 1.1 cgd line[5] = 't';
620 1.1 cgd break;
621 1.1 cgd }
622 1.1 cgd }
623 1.1 cgd #endif
624 1.1 cgd
625 1.1 cgd /* get name of connected client */
626 1.1 cgd hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr),
627 1.1 cgd who->sin_family);
628 1.1 cgd if (hp)
629 1.1 cgd host = hp->h_name;
630 1.1 cgd else
631 1.1 cgd host = inet_ntoa(who->sin_addr);
632 1.1 cgd /*
633 1.1 cgd * We must make a copy because Kerberos is probably going
634 1.1 cgd * to also do a gethost* and overwrite the static data...
635 1.1 cgd */
636 1.1 cgd strncpy(remote_host_name, host, sizeof(remote_host_name)-1);
637 1.1 cgd remote_host_name[sizeof(remote_host_name)-1] = 0;
638 1.1 cgd host = remote_host_name;
639 1.1 cgd
640 1.1 cgd (void) gethostname(host_name, sizeof (host_name));
641 1.1 cgd hostname = host_name;
642 1.1 cgd
643 1.1 cgd #if defined(AUTHENTICATE) || defined(ENCRYPT)
644 1.1 cgd auth_encrypt_init(hostname, host, "TELNETD", 1);
645 1.1 cgd #endif
646 1.1 cgd
647 1.1 cgd init_env();
648 1.1 cgd /*
649 1.1 cgd * get terminal type.
650 1.1 cgd */
651 1.1 cgd *user_name = 0;
652 1.1 cgd level = getterminaltype(user_name);
653 1.1 cgd setenv("TERM", terminaltype ? terminaltype : "network", 1);
654 1.1 cgd
655 1.1 cgd /*
656 1.1 cgd * Start up the login process on the slave side of the terminal
657 1.1 cgd */
658 1.1 cgd #ifndef convex
659 1.1 cgd startslave(host, level, user_name);
660 1.1 cgd
661 1.1 cgd telnet(net, pty); /* begin server processing */
662 1.1 cgd #else
663 1.1 cgd telnet(net, pty, host);
664 1.1 cgd #endif
665 1.1 cgd /*NOTREACHED*/
666 1.1 cgd } /* end of doit */
667 1.1 cgd
668 1.1 cgd #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
669 1.1 cgd int
670 1.1 cgd Xterm_output(ibufp, obuf, icountp, ocount)
671 1.1 cgd char **ibufp, *obuf;
672 1.1 cgd int *icountp, ocount;
673 1.1 cgd {
674 1.1 cgd int ret;
675 1.1 cgd ret = term_output(*ibufp, obuf, *icountp, ocount);
676 1.1 cgd *ibufp += *icountp;
677 1.1 cgd *icountp = 0;
678 1.1 cgd return(ret);
679 1.1 cgd }
680 1.1 cgd #define term_output Xterm_output
681 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
682 1.1 cgd
683 1.1 cgd /*
684 1.1 cgd * Main loop. Select from pty and network, and
685 1.1 cgd * hand data to telnet receiver finite state machine.
686 1.1 cgd */
687 1.1 cgd void
688 1.1 cgd #ifndef convex
689 1.1 cgd telnet(f, p)
690 1.1 cgd #else
691 1.1 cgd telnet(f, p, host)
692 1.1 cgd #endif
693 1.1 cgd int f, p;
694 1.1 cgd #ifdef convex
695 1.1 cgd char *host;
696 1.1 cgd #endif
697 1.1 cgd {
698 1.1 cgd int on = 1;
699 1.1 cgd #define TABBUFSIZ 512
700 1.1 cgd char defent[TABBUFSIZ];
701 1.1 cgd char defstrs[TABBUFSIZ];
702 1.1 cgd #undef TABBUFSIZ
703 1.1 cgd char *HE;
704 1.1 cgd char *HN;
705 1.1 cgd char *IM;
706 1.1 cgd void netflush();
707 1.1 cgd
708 1.1 cgd /*
709 1.1 cgd * Initialize the slc mapping table.
710 1.1 cgd */
711 1.1 cgd get_slc_defaults();
712 1.1 cgd
713 1.1 cgd /*
714 1.1 cgd * Do some tests where it is desireable to wait for a response.
715 1.1 cgd * Rather than doing them slowly, one at a time, do them all
716 1.1 cgd * at once.
717 1.1 cgd */
718 1.1 cgd if (my_state_is_wont(TELOPT_SGA))
719 1.1 cgd send_will(TELOPT_SGA, 1);
720 1.1 cgd /*
721 1.1 cgd * Is the client side a 4.2 (NOT 4.3) system? We need to know this
722 1.1 cgd * because 4.2 clients are unable to deal with TCP urgent data.
723 1.1 cgd *
724 1.1 cgd * To find out, we send out a "DO ECHO". If the remote system
725 1.1 cgd * answers "WILL ECHO" it is probably a 4.2 client, and we note
726 1.1 cgd * that fact ("WILL ECHO" ==> that the client will echo what
727 1.1 cgd * WE, the server, sends it; it does NOT mean that the client will
728 1.1 cgd * echo the terminal input).
729 1.1 cgd */
730 1.1 cgd send_do(TELOPT_ECHO, 1);
731 1.1 cgd
732 1.1 cgd #ifdef LINEMODE
733 1.1 cgd if (his_state_is_wont(TELOPT_LINEMODE)) {
734 1.1 cgd /* Query the peer for linemode support by trying to negotiate
735 1.1 cgd * the linemode option.
736 1.1 cgd */
737 1.1 cgd linemode = 0;
738 1.1 cgd editmode = 0;
739 1.1 cgd send_do(TELOPT_LINEMODE, 1); /* send do linemode */
740 1.1 cgd }
741 1.1 cgd #endif /* LINEMODE */
742 1.1 cgd
743 1.1 cgd /*
744 1.1 cgd * Send along a couple of other options that we wish to negotiate.
745 1.1 cgd */
746 1.1 cgd send_do(TELOPT_NAWS, 1);
747 1.1 cgd send_will(TELOPT_STATUS, 1);
748 1.1 cgd flowmode = 1; /* default flow control state */
749 1.1 cgd send_do(TELOPT_LFLOW, 1);
750 1.1 cgd
751 1.1 cgd /*
752 1.1 cgd * Spin, waiting for a response from the DO ECHO. However,
753 1.1 cgd * some REALLY DUMB telnets out there might not respond
754 1.1 cgd * to the DO ECHO. So, we spin looking for NAWS, (most dumb
755 1.1 cgd * telnets so far seem to respond with WONT for a DO that
756 1.1 cgd * they don't understand...) because by the time we get the
757 1.1 cgd * response, it will already have processed the DO ECHO.
758 1.1 cgd * Kludge upon kludge.
759 1.1 cgd */
760 1.1 cgd while (his_will_wont_is_changing(TELOPT_NAWS))
761 1.1 cgd ttloop();
762 1.1 cgd
763 1.1 cgd /*
764 1.1 cgd * But...
765 1.1 cgd * The client might have sent a WILL NAWS as part of its
766 1.1 cgd * startup code; if so, we'll be here before we get the
767 1.1 cgd * response to the DO ECHO. We'll make the assumption
768 1.1 cgd * that any implementation that understands about NAWS
769 1.1 cgd * is a modern enough implementation that it will respond
770 1.1 cgd * to our DO ECHO request; hence we'll do another spin
771 1.1 cgd * waiting for the ECHO option to settle down, which is
772 1.1 cgd * what we wanted to do in the first place...
773 1.1 cgd */
774 1.1 cgd if (his_want_state_is_will(TELOPT_ECHO) &&
775 1.1 cgd his_state_is_will(TELOPT_NAWS)) {
776 1.1 cgd while (his_will_wont_is_changing(TELOPT_ECHO))
777 1.1 cgd ttloop();
778 1.1 cgd }
779 1.1 cgd /*
780 1.1 cgd * On the off chance that the telnet client is broken and does not
781 1.1 cgd * respond to the DO ECHO we sent, (after all, we did send the
782 1.1 cgd * DO NAWS negotiation after the DO ECHO, and we won't get here
783 1.1 cgd * until a response to the DO NAWS comes back) simulate the
784 1.1 cgd * receipt of a will echo. This will also send a WONT ECHO
785 1.1 cgd * to the client, since we assume that the client failed to
786 1.1 cgd * respond because it believes that it is already in DO ECHO
787 1.1 cgd * mode, which we do not want.
788 1.1 cgd */
789 1.1 cgd if (his_want_state_is_will(TELOPT_ECHO)) {
790 1.1 cgd DIAG(TD_OPTIONS,
791 1.1 cgd {sprintf(nfrontp, "td: simulating recv\r\n");
792 1.1 cgd nfrontp += strlen(nfrontp);});
793 1.1 cgd willoption(TELOPT_ECHO);
794 1.1 cgd }
795 1.1 cgd
796 1.1 cgd /*
797 1.1 cgd * Finally, to clean things up, we turn on our echo. This
798 1.1 cgd * will break stupid 4.2 telnets out of local terminal echo.
799 1.1 cgd */
800 1.1 cgd
801 1.1 cgd if (my_state_is_wont(TELOPT_ECHO))
802 1.1 cgd send_will(TELOPT_ECHO, 1);
803 1.1 cgd
804 1.1 cgd /*
805 1.1 cgd * Turn on packet mode
806 1.1 cgd */
807 1.1 cgd (void) ioctl(p, TIOCPKT, (char *)&on);
808 1.1 cgd #if defined(LINEMODE) && defined(KLUDGELINEMODE)
809 1.1 cgd /*
810 1.1 cgd * Continuing line mode support. If client does not support
811 1.1 cgd * real linemode, attempt to negotiate kludge linemode by sending
812 1.1 cgd * the do timing mark sequence.
813 1.1 cgd */
814 1.1 cgd if (lmodetype < REAL_LINEMODE)
815 1.1 cgd send_do(TELOPT_TM, 1);
816 1.1 cgd #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
817 1.1 cgd
818 1.1 cgd /*
819 1.1 cgd * Call telrcv() once to pick up anything received during
820 1.1 cgd * terminal type negotiation, 4.2/4.3 determination, and
821 1.1 cgd * linemode negotiation.
822 1.1 cgd */
823 1.1 cgd telrcv();
824 1.1 cgd
825 1.1 cgd (void) ioctl(f, FIONBIO, (char *)&on);
826 1.1 cgd (void) ioctl(p, FIONBIO, (char *)&on);
827 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
828 1.1 cgd init_termdriver(f, p, interrupt, sendbrk);
829 1.1 cgd #endif
830 1.1 cgd
831 1.1 cgd #if defined(SO_OOBINLINE)
832 1.1 cgd (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE, &on, sizeof on);
833 1.1 cgd #endif /* defined(SO_OOBINLINE) */
834 1.1 cgd
835 1.1 cgd #ifdef SIGTSTP
836 1.1 cgd (void) signal(SIGTSTP, SIG_IGN);
837 1.1 cgd #endif
838 1.1 cgd #ifdef SIGTTOU
839 1.1 cgd /*
840 1.1 cgd * Ignoring SIGTTOU keeps the kernel from blocking us
841 1.1 cgd * in ttioct() in /sys/tty.c.
842 1.1 cgd */
843 1.1 cgd (void) signal(SIGTTOU, SIG_IGN);
844 1.1 cgd #endif
845 1.1 cgd
846 1.1 cgd (void) signal(SIGCHLD, cleanup);
847 1.1 cgd
848 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
849 1.1 cgd /*
850 1.1 cgd * Cray-2 will send a signal when pty modes are changed by slave
851 1.1 cgd * side. Set up signal handler now.
852 1.1 cgd */
853 1.1 cgd if ((int)signal(SIGUSR1, termstat) < 0)
854 1.1 cgd perror("signal");
855 1.1 cgd else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0)
856 1.1 cgd perror("ioctl:TCSIGME");
857 1.1 cgd /*
858 1.1 cgd * Make processing loop check terminal characteristics early on.
859 1.1 cgd */
860 1.1 cgd termstat();
861 1.1 cgd #endif
862 1.1 cgd
863 1.1 cgd #ifdef TIOCNOTTY
864 1.1 cgd {
865 1.1 cgd register int t;
866 1.1 cgd t = open(_PATH_TTY, O_RDWR);
867 1.1 cgd if (t >= 0) {
868 1.1 cgd (void) ioctl(t, TIOCNOTTY, (char *)0);
869 1.1 cgd (void) close(t);
870 1.1 cgd }
871 1.1 cgd }
872 1.1 cgd #endif
873 1.1 cgd
874 1.1 cgd #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
875 1.1 cgd (void) setsid();
876 1.1 cgd ioctl(p, TIOCSCTTY, 0);
877 1.1 cgd #endif
878 1.1 cgd
879 1.1 cgd /*
880 1.1 cgd * Show banner that getty never gave.
881 1.1 cgd *
882 1.1 cgd * We put the banner in the pty input buffer. This way, it
883 1.1 cgd * gets carriage return null processing, etc., just like all
884 1.1 cgd * other pty --> client data.
885 1.1 cgd */
886 1.1 cgd
887 1.1 cgd #if !defined(CRAY) || !defined(NEWINIT)
888 1.1 cgd if (getenv("USER"))
889 1.1 cgd hostinfo = 0;
890 1.1 cgd #endif
891 1.1 cgd
892 1.1 cgd if (getent(defent, "default") == 1) {
893 1.1 cgd char *getstr();
894 1.1 cgd char *cp=defstrs;
895 1.1 cgd
896 1.1 cgd HE = getstr("he", &cp);
897 1.1 cgd HN = getstr("hn", &cp);
898 1.1 cgd IM = getstr("im", &cp);
899 1.1 cgd if (HN && *HN)
900 1.1 cgd (void) strcpy(host_name, HN);
901 1.1 cgd if (IM == 0)
902 1.1 cgd IM = "";
903 1.1 cgd } else {
904 1.1 cgd IM = DEFAULT_IM;
905 1.1 cgd HE = 0;
906 1.1 cgd }
907 1.1 cgd edithost(HE, host_name);
908 1.1 cgd if (hostinfo && *IM)
909 1.1 cgd putf(IM, ptyibuf2);
910 1.1 cgd
911 1.1 cgd if (pcc)
912 1.1 cgd (void) strncat(ptyibuf2, ptyip, pcc+1);
913 1.1 cgd ptyip = ptyibuf2;
914 1.1 cgd pcc = strlen(ptyip);
915 1.1 cgd #ifdef LINEMODE
916 1.1 cgd /*
917 1.1 cgd * Last check to make sure all our states are correct.
918 1.1 cgd */
919 1.1 cgd init_termbuf();
920 1.1 cgd localstat();
921 1.1 cgd #endif /* LINEMODE */
922 1.1 cgd
923 1.1 cgd DIAG(TD_REPORT,
924 1.1 cgd {sprintf(nfrontp, "td: Entering processing loop\r\n");
925 1.1 cgd nfrontp += strlen(nfrontp);});
926 1.1 cgd
927 1.1 cgd #ifdef convex
928 1.1 cgd startslave(host);
929 1.1 cgd #endif
930 1.1 cgd
931 1.1 cgd for (;;) {
932 1.1 cgd fd_set ibits, obits, xbits;
933 1.1 cgd register int c;
934 1.1 cgd
935 1.1 cgd if (ncc < 0 && pcc < 0)
936 1.1 cgd break;
937 1.1 cgd
938 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
939 1.1 cgd if (needtermstat)
940 1.1 cgd _termstat();
941 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
942 1.1 cgd FD_ZERO(&ibits);
943 1.1 cgd FD_ZERO(&obits);
944 1.1 cgd FD_ZERO(&xbits);
945 1.1 cgd /*
946 1.1 cgd * Never look for input if there's still
947 1.1 cgd * stuff in the corresponding output buffer
948 1.1 cgd */
949 1.1 cgd if (nfrontp - nbackp || pcc > 0) {
950 1.1 cgd FD_SET(f, &obits);
951 1.1 cgd } else {
952 1.1 cgd FD_SET(p, &ibits);
953 1.1 cgd }
954 1.1 cgd if (pfrontp - pbackp || ncc > 0) {
955 1.1 cgd FD_SET(p, &obits);
956 1.1 cgd } else {
957 1.1 cgd FD_SET(f, &ibits);
958 1.1 cgd }
959 1.1 cgd if (!SYNCHing) {
960 1.1 cgd FD_SET(f, &xbits);
961 1.1 cgd }
962 1.1 cgd if ((c = select(16, &ibits, &obits, &xbits,
963 1.1 cgd (struct timeval *)0)) < 1) {
964 1.1 cgd if (c == -1) {
965 1.1 cgd if (errno == EINTR) {
966 1.1 cgd continue;
967 1.1 cgd }
968 1.1 cgd }
969 1.1 cgd sleep(5);
970 1.1 cgd continue;
971 1.1 cgd }
972 1.1 cgd
973 1.1 cgd /*
974 1.1 cgd * Any urgent data?
975 1.1 cgd */
976 1.1 cgd if (FD_ISSET(net, &xbits)) {
977 1.1 cgd SYNCHing = 1;
978 1.1 cgd }
979 1.1 cgd
980 1.1 cgd /*
981 1.1 cgd * Something to read from the network...
982 1.1 cgd */
983 1.1 cgd if (FD_ISSET(net, &ibits)) {
984 1.1 cgd #if !defined(SO_OOBINLINE)
985 1.1 cgd /*
986 1.1 cgd * In 4.2 (and 4.3 beta) systems, the
987 1.1 cgd * OOB indication and data handling in the kernel
988 1.1 cgd * is such that if two separate TCP Urgent requests
989 1.1 cgd * come in, one byte of TCP data will be overlaid.
990 1.1 cgd * This is fatal for Telnet, but we try to live
991 1.1 cgd * with it.
992 1.1 cgd *
993 1.1 cgd * In addition, in 4.2 (and...), a special protocol
994 1.1 cgd * is needed to pick up the TCP Urgent data in
995 1.1 cgd * the correct sequence.
996 1.1 cgd *
997 1.1 cgd * What we do is: if we think we are in urgent
998 1.1 cgd * mode, we look to see if we are "at the mark".
999 1.1 cgd * If we are, we do an OOB receive. If we run
1000 1.1 cgd * this twice, we will do the OOB receive twice,
1001 1.1 cgd * but the second will fail, since the second
1002 1.1 cgd * time we were "at the mark", but there wasn't
1003 1.1 cgd * any data there (the kernel doesn't reset
1004 1.1 cgd * "at the mark" until we do a normal read).
1005 1.1 cgd * Once we've read the OOB data, we go ahead
1006 1.1 cgd * and do normal reads.
1007 1.1 cgd *
1008 1.1 cgd * There is also another problem, which is that
1009 1.1 cgd * since the OOB byte we read doesn't put us
1010 1.1 cgd * out of OOB state, and since that byte is most
1011 1.1 cgd * likely the TELNET DM (data mark), we would
1012 1.1 cgd * stay in the TELNET SYNCH (SYNCHing) state.
1013 1.1 cgd * So, clocks to the rescue. If we've "just"
1014 1.1 cgd * received a DM, then we test for the
1015 1.1 cgd * presence of OOB data when the receive OOB
1016 1.1 cgd * fails (and AFTER we did the normal mode read
1017 1.1 cgd * to clear "at the mark").
1018 1.1 cgd */
1019 1.1 cgd if (SYNCHing) {
1020 1.1 cgd int atmark;
1021 1.1 cgd
1022 1.1 cgd (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1023 1.1 cgd if (atmark) {
1024 1.1 cgd ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1025 1.1 cgd if ((ncc == -1) && (errno == EINVAL)) {
1026 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1027 1.1 cgd if (sequenceIs(didnetreceive, gotDM)) {
1028 1.1 cgd SYNCHing = stilloob(net);
1029 1.1 cgd }
1030 1.1 cgd }
1031 1.1 cgd } else {
1032 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1033 1.1 cgd }
1034 1.1 cgd } else {
1035 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1036 1.1 cgd }
1037 1.1 cgd settimer(didnetreceive);
1038 1.1 cgd #else /* !defined(SO_OOBINLINE)) */
1039 1.1 cgd ncc = read(net, netibuf, sizeof (netibuf));
1040 1.1 cgd #endif /* !defined(SO_OOBINLINE)) */
1041 1.1 cgd if (ncc < 0 && errno == EWOULDBLOCK)
1042 1.1 cgd ncc = 0;
1043 1.1 cgd else {
1044 1.1 cgd if (ncc <= 0) {
1045 1.1 cgd break;
1046 1.1 cgd }
1047 1.1 cgd netip = netibuf;
1048 1.1 cgd }
1049 1.1 cgd DIAG((TD_REPORT | TD_NETDATA),
1050 1.1 cgd {sprintf(nfrontp, "td: netread %d chars\r\n", ncc);
1051 1.1 cgd nfrontp += strlen(nfrontp);});
1052 1.1 cgd DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1053 1.1 cgd }
1054 1.1 cgd
1055 1.1 cgd /*
1056 1.1 cgd * Something to read from the pty...
1057 1.1 cgd */
1058 1.1 cgd if (FD_ISSET(p, &ibits)) {
1059 1.1 cgd pcc = read(p, ptyibuf, BUFSIZ);
1060 1.1 cgd /*
1061 1.1 cgd * On some systems, if we try to read something
1062 1.1 cgd * off the master side before the slave side is
1063 1.1 cgd * opened, we get EIO.
1064 1.1 cgd */
1065 1.1 cgd if (pcc < 0 && (errno == EWOULDBLOCK || errno == EIO)) {
1066 1.1 cgd pcc = 0;
1067 1.1 cgd } else {
1068 1.1 cgd if (pcc <= 0)
1069 1.1 cgd break;
1070 1.1 cgd #if !defined(CRAY2) || !defined(UNICOS5)
1071 1.1 cgd #ifdef LINEMODE
1072 1.1 cgd /*
1073 1.1 cgd * If ioctl from pty, pass it through net
1074 1.1 cgd */
1075 1.1 cgd if (ptyibuf[0] & TIOCPKT_IOCTL) {
1076 1.1 cgd copy_termbuf(ptyibuf+1, pcc-1);
1077 1.1 cgd localstat();
1078 1.1 cgd pcc = 1;
1079 1.1 cgd }
1080 1.1 cgd #endif /* LINEMODE */
1081 1.1 cgd if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1082 1.1 cgd netclear(); /* clear buffer back */
1083 1.1 cgd #ifndef NO_URGENT
1084 1.1 cgd /*
1085 1.1 cgd * There are client telnets on some
1086 1.1 cgd * operating systems get screwed up
1087 1.1 cgd * royally if we send them urgent
1088 1.1 cgd * mode data.
1089 1.1 cgd */
1090 1.1 cgd *nfrontp++ = IAC;
1091 1.1 cgd *nfrontp++ = DM;
1092 1.1 cgd neturg = nfrontp-1; /* off by one XXX */
1093 1.1 cgd #endif
1094 1.1 cgd }
1095 1.1 cgd if (his_state_is_will(TELOPT_LFLOW) &&
1096 1.1 cgd (ptyibuf[0] &
1097 1.1 cgd (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1098 1.1 cgd (void) sprintf(nfrontp, "%c%c%c%c%c%c",
1099 1.1 cgd IAC, SB, TELOPT_LFLOW,
1100 1.1 cgd ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0,
1101 1.1 cgd IAC, SE);
1102 1.1 cgd nfrontp += 6;
1103 1.1 cgd }
1104 1.1 cgd pcc--;
1105 1.1 cgd ptyip = ptyibuf+1;
1106 1.1 cgd #else /* defined(CRAY2) && defined(UNICOS5) */
1107 1.1 cgd if (!uselinemode) {
1108 1.1 cgd unpcc = pcc;
1109 1.1 cgd unptyip = ptyibuf;
1110 1.1 cgd pcc = term_output(&unptyip, ptyibuf2,
1111 1.1 cgd &unpcc, BUFSIZ);
1112 1.1 cgd ptyip = ptyibuf2;
1113 1.1 cgd } else
1114 1.1 cgd ptyip = ptyibuf;
1115 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1116 1.1 cgd }
1117 1.1 cgd }
1118 1.1 cgd
1119 1.1 cgd while (pcc > 0) {
1120 1.1 cgd if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1121 1.1 cgd break;
1122 1.1 cgd c = *ptyip++ & 0377, pcc--;
1123 1.1 cgd if (c == IAC)
1124 1.1 cgd *nfrontp++ = c;
1125 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1126 1.1 cgd else if (c == '\n' &&
1127 1.1 cgd my_state_is_wont(TELOPT_BINARY) && newmap)
1128 1.1 cgd *nfrontp++ = '\r';
1129 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1130 1.1 cgd *nfrontp++ = c;
1131 1.1 cgd if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1132 1.1 cgd if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1133 1.1 cgd *nfrontp++ = *ptyip++ & 0377;
1134 1.1 cgd pcc--;
1135 1.1 cgd } else
1136 1.1 cgd *nfrontp++ = '\0';
1137 1.1 cgd }
1138 1.1 cgd }
1139 1.1 cgd #if defined(CRAY2) && defined(UNICOS5)
1140 1.1 cgd /*
1141 1.1 cgd * If chars were left over from the terminal driver,
1142 1.1 cgd * note their existence.
1143 1.1 cgd */
1144 1.1 cgd if (!uselinemode && unpcc) {
1145 1.1 cgd pcc = unpcc;
1146 1.1 cgd unpcc = 0;
1147 1.1 cgd ptyip = unptyip;
1148 1.1 cgd }
1149 1.1 cgd #endif /* defined(CRAY2) && defined(UNICOS5) */
1150 1.1 cgd
1151 1.1 cgd if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1152 1.1 cgd netflush();
1153 1.1 cgd if (ncc > 0)
1154 1.1 cgd telrcv();
1155 1.1 cgd if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1156 1.1 cgd ptyflush();
1157 1.1 cgd }
1158 1.1 cgd cleanup(0);
1159 1.1 cgd } /* end of telnet */
1160 1.1 cgd
1161 1.1 cgd #ifndef TCSIG
1162 1.1 cgd # ifdef TIOCSIG
1163 1.1 cgd # define TCSIG TIOCSIG
1164 1.1 cgd # endif
1165 1.1 cgd #endif
1166 1.1 cgd
1167 1.1 cgd /*
1168 1.1 cgd * Send interrupt to process on other side of pty.
1169 1.1 cgd * If it is in raw mode, just write NULL;
1170 1.1 cgd * otherwise, write intr char.
1171 1.1 cgd */
1172 1.1 cgd void
1173 1.1 cgd interrupt()
1174 1.1 cgd {
1175 1.1 cgd ptyflush(); /* half-hearted */
1176 1.1 cgd
1177 1.1 cgd #ifdef TCSIG
1178 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGINT);
1179 1.1 cgd #else /* TCSIG */
1180 1.1 cgd init_termbuf();
1181 1.1 cgd *pfrontp++ = slctab[SLC_IP].sptr ?
1182 1.1 cgd (unsigned char)*slctab[SLC_IP].sptr : '\177';
1183 1.1 cgd #endif /* TCSIG */
1184 1.1 cgd }
1185 1.1 cgd
1186 1.1 cgd /*
1187 1.1 cgd * Send quit to process on other side of pty.
1188 1.1 cgd * If it is in raw mode, just write NULL;
1189 1.1 cgd * otherwise, write quit char.
1190 1.1 cgd */
1191 1.1 cgd void
1192 1.1 cgd sendbrk()
1193 1.1 cgd {
1194 1.1 cgd ptyflush(); /* half-hearted */
1195 1.1 cgd #ifdef TCSIG
1196 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1197 1.1 cgd #else /* TCSIG */
1198 1.1 cgd init_termbuf();
1199 1.1 cgd *pfrontp++ = slctab[SLC_ABORT].sptr ?
1200 1.1 cgd (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1201 1.1 cgd #endif /* TCSIG */
1202 1.1 cgd }
1203 1.1 cgd
1204 1.1 cgd void
1205 1.1 cgd sendsusp()
1206 1.1 cgd {
1207 1.1 cgd #ifdef SIGTSTP
1208 1.1 cgd ptyflush(); /* half-hearted */
1209 1.1 cgd # ifdef TCSIG
1210 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1211 1.1 cgd # else /* TCSIG */
1212 1.1 cgd *pfrontp++ = slctab[SLC_SUSP].sptr ?
1213 1.1 cgd (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1214 1.1 cgd # endif /* TCSIG */
1215 1.1 cgd #endif /* SIGTSTP */
1216 1.1 cgd }
1217 1.1 cgd
1218 1.1 cgd /*
1219 1.1 cgd * When we get an AYT, if ^T is enabled, use that. Otherwise,
1220 1.1 cgd * just send back "[Yes]".
1221 1.1 cgd */
1222 1.1 cgd void
1223 1.1 cgd recv_ayt()
1224 1.1 cgd {
1225 1.1 cgd #if defined(SIGINFO) && defined(TCSIG)
1226 1.1 cgd if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1227 1.1 cgd (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1228 1.1 cgd return;
1229 1.1 cgd }
1230 1.1 cgd #endif
1231 1.1 cgd (void) strcpy(nfrontp, "\r\n[Yes]\r\n");
1232 1.1 cgd nfrontp += 9;
1233 1.1 cgd }
1234 1.1 cgd
1235 1.1 cgd void
1236 1.1 cgd doeof()
1237 1.1 cgd {
1238 1.1 cgd init_termbuf();
1239 1.1 cgd
1240 1.1 cgd #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1241 1.1 cgd if (!tty_isediting()) {
1242 1.1 cgd extern char oldeofc;
1243 1.1 cgd *pfrontp++ = oldeofc;
1244 1.1 cgd return;
1245 1.1 cgd }
1246 1.1 cgd #endif
1247 1.1 cgd *pfrontp++ = slctab[SLC_EOF].sptr ?
1248 1.1 cgd (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1249 1.1 cgd }
1250