Mercurial > hg > toybox
annotate toys/pending/telnetd.c @ 1363:e65f9a9ba62d draft
Cleanup pass on mkpasswd.c
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 25 Jun 2014 22:54:59 -0500 |
parents | 04d754570e50 |
children | 6a06541c090c |
rev | line source |
---|---|
1162
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* telnetd.c - Telnet Server |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * Copyright 2013 Sandeep Sharma <sandeep.jack2756@gmail.com> |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 USE_TELNETD(NEWTOY(telnetd, "w#<0b:p#<0>65535=23f:l:FSKi[!wi]", TOYFLAG_USR|TOYFLAG_BIN)) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 config TELNETD |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 bool "telnetd" |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 default n |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 help |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 Handle incoming telnet connections |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 -l LOGIN Exec LOGIN on connect |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 -K Close connection as soon as login exits |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 -p PORT Port to listen on |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 -b ADDR[:PORT] Address to bind to |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 -F Run in foreground |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 -i Inetd mode |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 -w SEC Inetd 'wait' mode, linger time SEC |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 -S Log to syslog (implied by -i or without -F and -w) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 #define FOR_telnetd |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 #include "toys.h" |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 #include <utmp.h> |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 GLOBALS( |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 char *login_path; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 char *issue_path; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 int port; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 char *host_addr; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 long w_sec; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 int gmax_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 pid_t fork_pid; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 ) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 # define IAC 255 /* interpret as command: */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 # define DONT 254 /* you are not to use option */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 # define DO 253 /* please, you use option */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 # define WONT 252 /* I won't use option */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 # define WILL 251 /* I will use option */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 # define SB 250 /* interpret as subnegotiation */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 # define SE 240 /* end sub negotiation */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 # define NOP 241 /* No Operation */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 # define TELOPT_ECHO 1 /* echo */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 # define TELOPT_SGA 3 /* suppress go ahead */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 # define TELOPT_TTYPE 24 /* terminal type */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 # define TELOPT_NAWS 31 /* window size */ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 #define BUFSIZE 4*1024 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 struct term_session { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 int new_fd, pty_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 pid_t child_pid; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 int buff1_avail, buff2_avail; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 int buff1_written, buff2_written; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 int rem; //unprocessed data from socket |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 char buff1[BUFSIZE], buff2[BUFSIZE]; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 struct term_session *next; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 }; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 struct term_session *session_list = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 static void get_sockaddr(char *host, void *buf) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 in_port_t port_num = htons(TT.port); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 struct addrinfo hints, *result; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 int status, af = AF_UNSPEC; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 char *s; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 // [ipv6]:port or exactly one : |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 if (*host == '[') { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 host++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 s = strchr(host, ']'); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 if (s) *s++ = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 else error_exit("bad address '%s'", host-1); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 af = AF_INET6; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 } else { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 s = strrchr(host, ':'); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 if (s && strchr(host, ':') == s) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 *s = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 af = AF_INET; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 } else if (s && strchr(host, ':') != s) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 af = AF_INET6; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 s = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 if (s++) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 char *ss; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 unsigned long p = strtoul(s, &ss, 0); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 if (!*s || *ss || p > 65535) error_exit("bad port '%s'", s); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 port_num = htons(p); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 memset(&hints, 0 , sizeof(struct addrinfo)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 hints.ai_family = af; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 hints.ai_socktype = SOCK_STREAM; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 status = getaddrinfo(host, NULL, &hints, &result); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 if (status) error_exit("bad address '%s' : %s", host, gai_strerror(status)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 memcpy(buf, result->ai_addr, result->ai_addrlen); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 freeaddrinfo(result); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 if (af == AF_INET) ((struct sockaddr_in*)buf)->sin_port = port_num; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 else ((struct sockaddr_in6*)buf)->sin6_port = port_num; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 |
1300
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
112 static void utmp_entry(void) |
1162
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 struct utmp entry; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 struct utmp *utp_ptr; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 pid_t pid = getpid(); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 utmpname(_PATH_UTMP); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 setutent(); //start from start |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 while ((utp_ptr = getutent()) != NULL) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 if (utp_ptr->ut_pid == pid && utp_ptr->ut_type >= INIT_PROCESS) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 if (!utp_ptr) entry.ut_type = DEAD_PROCESS; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 time(&entry.ut_time); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 setutent(); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 pututline(&entry); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 static int listen_socket(void) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 int s, af = AF_INET, yes = 1; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 char buf[sizeof(struct sockaddr_storage)]; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 memset(buf, 0, sizeof(buf)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 if (toys.optflags & FLAG_b) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 get_sockaddr(TT.host_addr, buf); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 af = ((struct sockaddr *)buf)->sa_family; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 } else { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 ((struct sockaddr_in*)buf)->sin_port = htons(TT.port); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 ((struct sockaddr_in*)buf)->sin_family = af; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 s = xsocket(af, SOCK_STREAM, 0); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) == -1) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 perror_exit("setsockopt"); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 if (bind(s, (struct sockaddr *)buf, ((af == AF_INET)? |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 (sizeof(struct sockaddr_in)):(sizeof(struct sockaddr_in6)))) == -1) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 close(s); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 perror_exit("bind"); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 if (listen(s, 1) < 0) perror_exit("listen"); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 return s; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 static void write_issue(char *tty) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 int size; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 char ch = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 struct utsname u; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 int fd = open(TT.issue_path, O_RDONLY); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 if (fd < 0) return ; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 uname(&u); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 while ((size = readall(fd, &ch, 1)) > 0) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 if (ch == '\\' || ch == '%') { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 if (readall(fd, &ch, 1) <= 0) perror_exit("readall!"); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 if (ch == 's') fputs(u.sysname, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 if (ch == 'n'|| ch == 'h') fputs(u.nodename, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 if (ch == 'r') fputs(u.release, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 if (ch == 'm') fputs(u.machine, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 if (ch == 'l') fputs(tty, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 else if (ch == '\n') { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 fputs("\n\r\0", stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 } else fputc(ch, stdout); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 fflush(NULL); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 close(fd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
181 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 static int new_session(int sockfd) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
183 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
184 char *argv_login[2]; //arguments for execvp cmd, NULL |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
185 char tty_name[30]; //tty name length. |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 int fd, flags, i = 1; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
187 char intial_iacs[] = {IAC, DO, TELOPT_ECHO, IAC, DO, TELOPT_NAWS, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
188 IAC, WILL, TELOPT_ECHO, IAC, WILL, TELOPT_SGA }; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
191 flags = fcntl(sockfd, F_GETFL); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 if (toys.optflags & FLAG_i) fcntl((sockfd + 1), F_SETFL, flags | O_NONBLOCK); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 writeall((toys.optflags & FLAG_i)?1:sockfd, intial_iacs, sizeof(intial_iacs)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 if ((TT.fork_pid = forkpty(&fd, tty_name, NULL, NULL)) > 0) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 flags = fcntl(fd, F_GETFL); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
199 return fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 if (TT.fork_pid < 0) perror_exit("fork"); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 write_issue(tty_name); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
203 argv_login[0] = strdup(TT.login_path); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 argv_login[1] = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 execvp(argv_login[0], argv_login); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 exit(EXIT_FAILURE); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 static int handle_iacs(struct term_session *tm, int c, int fd) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 char *curr ,*start,*end; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 int i = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
214 curr = start = tm->buff2+tm->buff2_avail; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
215 end = tm->buff2 + c -1; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 tm->rem = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 while (curr <= end) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 if (*curr != IAC){ |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 if (*curr != '\r') { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
221 toybuf[i++] = *curr++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
222 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 } else { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 toybuf[i++] = *curr++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 curr++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
226 if (curr < end && (*curr == '\n' || *curr == '\0')) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
227 curr++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
228 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
229 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
230 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 if ((curr + 1) > end) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 tm->rem = 1; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
234 break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
235 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 if (*(curr+1) == IAC) { //IAC as data --> IAC IAC |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 toybuf[i++] = *(curr+1); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
238 curr += 2; //IAC IAC --> 2 bytes |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
240 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
241 if (*(curr + 1) == NOP || *(curr + 1) == SE) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
242 curr += 2; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
243 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
244 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 if (*(curr + 1) == SB ) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
247 if (*(curr+2) == TELOPT_NAWS) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
248 struct winsize ws; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
249 if ((curr+8) >= end) { //ensure we have data to process. |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
250 tm->rem = end - curr; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
251 break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
252 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
253 ws.ws_col = (curr[3] << 8) | curr[4]; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
254 ws.ws_row = (curr[5] << 8) | curr[6]; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
255 ioctl(fd, TIOCSWINSZ, (char *)&ws); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
256 curr += 9; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
257 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
258 } else { //eat non-supported sub neg. options. |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
259 curr++, tm->rem++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
260 while (*curr != IAC && curr <= end) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
261 curr++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 tm->rem++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 if (*curr == IAC) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
265 tm->rem = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
267 } else break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
268 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
269 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
270 curr += 3; //skip non-supported 3 bytes. |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 memcpy(start, toybuf, i); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
273 memcpy(start + i, end - tm->rem, tm->rem); //put remaining if we break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 return i; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
275 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
276 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
277 static int dup_iacs(char *start, int fd, int len) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
278 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
279 char arr[] = {IAC, IAC}; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
280 char *needle = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
281 int ret = 0, c, count = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
282 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 while (len) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
284 if (*start == IAC) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 count = writeall(fd, arr, sizeof(arr)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
286 if (count != 2) break; //short write |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
287 start++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 ret++; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
289 len--; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
290 continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
291 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
292 needle = memchr(start, IAC, len); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
293 if (needle) c = needle - start; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
294 else c = len; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
295 count = writeall(fd, start, c); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
296 if (count < 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
297 len -= count; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
298 ret += count; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
299 start += count; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
300 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
301 return ret; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
302 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
303 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
304 void telnetd_main(void) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
305 { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
306 errno = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
307 fd_set rd, wr; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
308 struct term_session *tm = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
309 struct timeval tv, *tv_ptr = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
310 int pty_fd, new_fd, c = 0, w, master_fd = 0; |
1300
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
311 int inetd_m = toys.optflags & FLAG_i; |
1162
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
312 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 if (!(toys.optflags & FLAG_l)) TT.login_path = "/bin/login"; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
314 if (!(toys.optflags & FLAG_f)) TT.issue_path = "/etc/issue.net"; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 if (toys.optflags & FLAG_w) toys.optflags |= FLAG_F; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
316 if (!inetd_m) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
317 master_fd = listen_socket(); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
318 fcntl(master_fd, F_SETFD, FD_CLOEXEC); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 if (master_fd > TT.gmax_fd) TT.gmax_fd = master_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
320 if (!(toys.optflags & FLAG_F)) daemonize(); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 } else { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
322 pty_fd = new_session(master_fd); //master_fd = 0 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 if (pty_fd > TT.gmax_fd) TT.gmax_fd = pty_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
324 tm = xzalloc(sizeof(struct term_session)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
325 tm->child_pid = TT.fork_pid; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
326 tm->new_fd = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 tm->pty_fd = pty_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
328 if (session_list) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
329 tm->next = session_list; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
330 session_list = tm; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
331 } else session_list = tm; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
332 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
333 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
334 if ((toys.optflags & FLAG_w) && !session_list) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
335 tv.tv_sec = TT.w_sec; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
336 tv.tv_usec = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
337 tv_ptr = &tv; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
338 } |
1300
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
339 signal(SIGCHLD, generic_signal); |
1162
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
340 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
341 for (;;) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
342 FD_ZERO(&rd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
343 FD_ZERO(&wr); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
344 if (!inetd_m) FD_SET(master_fd, &rd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
345 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
346 tm = session_list; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 while (tm) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
348 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
349 if (tm->pty_fd > 0 && tm->buff1_avail < BUFSIZE) FD_SET(tm->pty_fd, &rd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
350 if (tm->new_fd >= 0 && tm->buff2_avail < BUFSIZE) FD_SET(tm->new_fd, &rd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
351 if (tm->pty_fd > 0 && (tm->buff2_avail - tm->buff2_written) > 0) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
352 FD_SET(tm->pty_fd, &wr); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
353 if (tm->new_fd >= 0 && (tm->buff1_avail - tm->buff1_written) > 0) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
354 FD_SET(tm->new_fd, &wr); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
355 tm = tm->next; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
356 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
357 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
358 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
359 int r = select(TT.gmax_fd + 1, &rd, &wr, NULL, tv_ptr); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
360 if (!r) return; //timeout |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
361 if (r < -1) continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
362 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
363 if (!inetd_m && FD_ISSET(master_fd, &rd)) { //accept new connection |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
364 new_fd = accept(master_fd, NULL, NULL); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
365 if (new_fd < 0) continue; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
366 tv_ptr = NULL; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
367 fcntl(new_fd, F_SETFD, FD_CLOEXEC); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
368 if (new_fd > TT.gmax_fd) TT.gmax_fd = new_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
369 pty_fd = new_session(new_fd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
370 if (pty_fd > TT.gmax_fd) TT.gmax_fd = pty_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
371 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
372 tm = xzalloc(sizeof(struct term_session)); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
373 tm->child_pid = TT.fork_pid; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
374 tm->new_fd = new_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
375 tm->pty_fd = pty_fd; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
376 if (session_list) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
377 tm->next = session_list; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
378 session_list = tm; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
379 } else session_list = tm; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
380 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
381 |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
382 tm = session_list; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 for (;tm;tm=tm->next) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
384 if (FD_ISSET(tm->pty_fd, &rd)) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
385 if ((c = read(tm->pty_fd, tm->buff1 + tm->buff1_avail, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
386 BUFSIZE-tm->buff1_avail)) <= 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
387 tm->buff1_avail += c; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
388 if ((w = dup_iacs(tm->buff1 + tm->buff1_written, tm->new_fd + inetd_m, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
389 tm->buff1_avail - tm->buff1_written)) < 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
390 tm->buff1_written += w; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
391 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
392 if (FD_ISSET(tm->new_fd, &rd)) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 if ((c = read(tm->new_fd, tm->buff2+tm->buff2_avail, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
394 BUFSIZE-tm->buff2_avail)) <= 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
395 c = handle_iacs(tm, c, tm->pty_fd); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 tm->buff2_avail += c; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
397 if ((w = write(tm->pty_fd, tm->buff2+ tm->buff2_written, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
398 tm->buff2_avail - tm->buff2_written)) < 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
399 tm->buff2_written += w; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
400 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
401 if (FD_ISSET(tm->pty_fd, &wr)) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
402 if ((w = write(tm->pty_fd, tm->buff2 + tm->buff2_written, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
403 tm->buff2_avail - tm->buff2_written)) < 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
404 tm->buff2_written += w; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
405 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
406 if (FD_ISSET(tm->new_fd, &wr)) { |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
407 if ((w = dup_iacs(tm->buff1 + tm->buff1_written, tm->new_fd + inetd_m, |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
408 tm->buff1_avail - tm->buff1_written)) < 0) break; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
409 tm->buff1_written += w; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
410 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
411 if (tm->buff1_written == tm->buff1_avail) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
412 tm->buff1_written = tm->buff1_avail = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
413 if (tm->buff2_written == tm->buff2_avail) |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
414 tm->buff2_written = tm->buff2_avail = 0; |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
415 fflush(NULL); |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
416 } |
1300
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
417 |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
418 // Loop to handle (unknown number of) SIGCHLD notifications |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
419 while (toys.signal) { |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
420 int status; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
421 struct term_session *prev = NULL; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
422 pid_t pid; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
423 |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
424 // funny little dance to avoid race conditions. |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
425 toys.signal = 0; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
426 pid = waitpid(-1, &status, WNOHANG); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
427 if (pid < 0) break; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
428 toys.signal++; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
429 |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
430 |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
431 for (tm = session_list; tm; tm = tm->next) { |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
432 if (tm->child_pid == pid) break; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
433 prev = tm; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
434 } |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
435 if (!tm) return; // reparented child we don't care about |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
436 |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
437 if (toys.optflags & FLAG_i) exit(EXIT_SUCCESS); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
438 if (!prev) session_list = session_list->next; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
439 else prev->next = tm->next; |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
440 utmp_entry(); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
441 xclose(tm->pty_fd); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
442 xclose(tm->new_fd); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
443 free(tm); |
04d754570e50
Make telnetd use generic_signal(), inline kill_session(), close race window where a SIGCHLD could get lost.
Rob Landley <rob@landley.net>
parents:
1162
diff
changeset
|
444 } |
1162
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
445 } |
213c00e7978e
telnet and telnetd from Ashwini Sharma's guys.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
446 } |