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