"Fossies" - the Fresh Open Source Software Archive 
Member "scanssh-2.1/telnet.c" (5 Nov 2004, 6647 Bytes) of package /linux/privat/old/scanssh-2.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Copyright 2000-2004 (c) Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/tree.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
37 #include <sys/time.h>
38 #include <netinet/in.h>
39
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <signal.h>
45 #include <err.h>
46
47 #include <event.h>
48 #include <dnet.h>
49
50 #include "scanssh.h"
51 #include "socks.h"
52
53 #ifdef DEBUG
54 extern int debug;
55 #define DFPRINTF(x) if (debug) fprintf x
56 #else
57 #define DFPRINTF(x)
58 #endif
59
60 extern rand_t *ss_rand;
61
62 void telnet_init(struct bufferevent *bev, struct argument *arg);
63 void telnet_finalize(struct bufferevent *bev, struct argument *arg);
64 void telnet_readcb(struct bufferevent *bev, void *parameter);
65 void telnet_writecb(struct bufferevent *bev, void *parameter);
66 void telnet_errorcb(struct bufferevent *bev, short what, void *parameter);
67
68 #define TELNET_WAITING_RESPONSE 0x0001
69 #define TELNET_WAITING_CONNECT 0x0002
70 #define TELNET_READING_CONNECT 0x0004
71 #define TELNET_WRITING_COMMAND 0x0008
72
73 int http_bufferanalyse(struct bufferevent *bev, struct argument *arg);
74
75 struct telnet_state {
76 char *response;
77 char *connect_wait;
78 };
79
80 #define CCPROXY "CCProxy Telnet>"
81 #define GATEWAY1 "host_name:port"
82 #define GATEWAY2 "host[:port]:"
83 #define WINGATE "WinGate>"
84
85 int
86 telnet_makeconnect(struct bufferevent *bev, struct argument *arg)
87 {
88 extern struct addr *socks_dst_addr;
89 struct evbuffer *input = EVBUFFER_INPUT(bev);
90 struct telnet_state *state = arg->a_state;
91 ip_addr_t address;
92
93 socks_resolveaddress("www.google.com", &address);
94
95 if (evbuffer_find(input, CCPROXY, strlen(CCPROXY)) != NULL) {
96 state->response = "telnet-proxy: CCproxy";
97 state->connect_wait = "OK!";
98 evbuffer_add_printf(EVBUFFER_OUTPUT(bev),
99 "open %s:80\r\n", addr_ntoa(socks_dst_addr));
100 bufferevent_enable(bev, EV_WRITE);
101 return (1);
102 } else if (evbuffer_find(input, GATEWAY1, strlen(GATEWAY1)) != NULL) {
103 state->response = "telnet-proxy: Gateway";
104 state->connect_wait = "Connected to:";
105 evbuffer_add_printf(EVBUFFER_OUTPUT(bev),
106 "%s:80\r\n", addr_ntoa(socks_dst_addr));
107 bufferevent_enable(bev, EV_WRITE);
108 return (1);
109 } else if (evbuffer_find(input, GATEWAY2, strlen(GATEWAY2)) != NULL) {
110 state->response = "telnet-proxy: Gateway";
111 /*
112 * We do not get a connection confirmation, just the echoed
113 * string. So, we wait for the echo and then send our command.
114 */
115 state->connect_wait = ":80";
116 evbuffer_add_printf(EVBUFFER_OUTPUT(bev),
117 "%s:80\r\n", addr_ntoa(socks_dst_addr));
118 bufferevent_enable(bev, EV_WRITE);
119 return (1);
120 } else if (evbuffer_find(input, WINGATE, strlen(WINGATE)) != NULL) {
121 state->response = "telnet-proxy: WinGate";
122 state->connect_wait = "...Connected";
123 evbuffer_add_printf(EVBUFFER_OUTPUT(bev),
124 "%s:80\r\n", addr_ntoa(socks_dst_addr));
125 bufferevent_enable(bev, EV_WRITE);
126 return (1);
127 } else if (EVBUFFER_LENGTH(input) > 512) {
128 return (-1);
129 }
130
131 return (0);
132 }
133
134 /* Scanner related functions */
135
136 void
137 telnet_init(struct bufferevent *bev, struct argument *arg)
138 {
139 if ((arg->a_state = calloc(1, sizeof(struct telnet_state))) == NULL)
140 err(1, "%s: calloc", __func__);
141 arg->a_flags = 0;
142 }
143
144 void
145 telnet_finalize(struct bufferevent *bev, struct argument *arg)
146 {
147 free(arg->a_state);
148 arg->a_state = NULL;
149 arg->a_flags = 0;
150 }
151
152 void
153 telnet_errorcb(struct bufferevent *bev, short what, void *parameter)
154 {
155 struct argument *arg = parameter;
156
157 DFPRINTF((stderr, "%s: called\n", __func__));
158
159 postres(arg, "<telnet proxy error>");
160 scanhost_return(bev, arg, 0);
161 }
162
163 /* TELNET Connect method */
164
165 void
166 telnet_readcb(struct bufferevent *bev, void *parameter)
167 {
168 struct argument *arg = parameter;
169 struct evbuffer *input = EVBUFFER_INPUT(bev);
170 struct telnet_state *state = arg->a_state;
171
172 DFPRINTF((stderr, "%s: called\n", __func__));
173
174 if (arg->a_flags == 0) {
175 int res = telnet_makeconnect(bev, arg);
176 if (res == -1) {
177 evbuffer_add(input, "", 1);
178 printres(arg, arg->a_ports[0].port,
179 EVBUFFER_DATA(input));
180 scanhost_return(bev, arg, 0);
181 return;
182 } else if (res == 1) {
183 arg->a_flags = TELNET_WAITING_CONNECT;
184 bufferevent_disable(bev, EV_READ);
185 }
186 } else if (arg->a_flags & TELNET_READING_CONNECT) {
187 if (evbuffer_find(input, state->connect_wait,
188 strlen(state->connect_wait)) == NULL)
189 return;
190 evbuffer_drain(input, EVBUFFER_LENGTH(input));
191
192 arg->a_flags = TELNET_WRITING_COMMAND;
193 bufferevent_disable(bev, EV_READ);
194 http_makerequest(bev, arg, socks_getword(), 0);
195 } else if (arg->a_flags & TELNET_WAITING_RESPONSE) {
196 int res = http_bufferanalyse(bev, arg);
197 if (res == -1)
198 return;
199 if (res == 1) {
200 postres(arg, state->response);
201 scanhost_return(bev, arg, 1);
202 }
203 }
204
205 return;
206 }
207
208 void
209 telnet_writecb(struct bufferevent *bev, void *parameter)
210 {
211 struct argument *arg = parameter;
212
213 DFPRINTF((stderr, "%s: called\n", __func__));
214
215 if (arg->a_flags == 0) {
216 return;
217 } else if (arg->a_flags & TELNET_WAITING_CONNECT) {
218 bufferevent_enable(bev, EV_READ);
219 arg->a_flags = TELNET_READING_CONNECT;
220 } else if (arg->a_flags & TELNET_WRITING_COMMAND) {
221 bufferevent_enable(bev, EV_READ);
222 arg->a_flags = TELNET_WAITING_RESPONSE;
223 }
224 }
225