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