/[cvs]/eggdrop1.9/src/telnet.c
ViewVC logotype

Contents of /eggdrop1.9/src/telnet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Feb 11 02:32:05 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.2: +5 -18 lines
File MIME type: text/x-chdr
* Some initial config code to play around with.

1 #include <eggdrop/eggdrop.h>
2 #include "core_config.h"
3
4 /* Possible states of the connection. */
5 #define STATE_RESOLVE 0
6 #define STATE_NICKNAME 1
7 #define STATE_PASSWORD 2
8 #define STATE_PARTYLINE 3
9
10 /* Telnet strings for controlling echo behavior. */
11 #define TELNET_ECHO 1
12 #define TELNET_AYT 246
13 #define TELNET_WILL 251
14 #define TELNET_WONT 252
15 #define TELNET_DO 253
16 #define TELNET_DONT 254
17 #define TELNET_CMD 255
18
19 #define TELNET_ECHO_OFF "\377\373\001"
20 #define TELNET_ECHO_ON "\377\374\001"
21
22 #define TFLAG_ECHO 1
23
24 /* Flags for sessions. */
25 #define STEALTH_LOGIN 1
26
27 typedef struct {
28 /* Who we're connected to. */
29 user_t *user;
30 char *nick, *ident, *host, *ip;
31 int port;
32 int idx;
33 int pid;
34
35 /* Flags for this connection. */
36 int flags;
37
38 /* Connection state we're in. */
39 int state, count;
40 } telnet_session_t;
41
42 static int telnet_idx = -1;
43 static int telnet_port = 0;
44
45 static void kill_session(telnet_session_t *session);
46
47 static int telnet_on_newclient(void *client_data, int idx, int newidx, const char *peer_ip, int peer_port);
48 static int telnet_on_read(void *client_data, int idx, char *data, int len);
49 static int telnet_on_eof(void *client_data, int idx, int err, const char *errmsg);
50 static int telnet_filter_read(void *client_data, int idx, char *data, int len);
51 static int telnet_filter_write(void *client_data, int idx, const char *data, int len);
52 static int telnet_filter_delete(void *client_data, int idx);
53
54 static int ident_result(void *client_data, const char *ip, int port, const char *reply);
55 static int dns_result(void *client_data, const char *ip, const char *host);
56 static int process_results(telnet_session_t *session);
57
58 static sockbuf_handler_t server_handler = {
59 "telnet server",
60 NULL, NULL, telnet_on_newclient,
61 NULL, NULL
62 };
63
64 #define TELNET_FILTER_LEVEL SOCKBUF_LEVEL_TEXT_ALTERATION
65
66 static sockbuf_filter_t telnet_filter = {
67 "telnet filter", TELNET_FILTER_LEVEL,
68 NULL, NULL, NULL,
69 telnet_filter_read, telnet_filter_write, NULL,
70 NULL, telnet_filter_delete
71 };
72
73 static sockbuf_handler_t client_handler = {
74 "telnet",
75 NULL, telnet_on_eof, NULL,
76 telnet_on_read, NULL
77 };
78
79 int telnet_init()
80 {
81 /* Open our listening socket. */
82 telnet_idx = egg_server(core_config.telnet_vhost, core_config.telnet_port, &telnet_port);
83 sockbuf_set_handler(telnet_idx, &server_handler, NULL);
84 return(0);
85 }
86
87 static void kill_session(telnet_session_t *session)
88 {
89 sockbuf_delete(session->idx);
90 if (session->ip) free(session->ip);
91 if (session->host) free(session->host);
92 if (session->ident) free(session->ident);
93 if (session->nick) free(session->nick);
94 free(session);
95 }
96
97 static int telnet_on_newclient(void *client_data, int idx, int newidx, const char *peer_ip, int peer_port)
98 {
99 telnet_session_t *session;
100 int *flags;
101
102 session = calloc(1, sizeof(*session));
103 session->ip = strdup(peer_ip);
104 session->port = peer_port;
105 session->idx = newidx;
106
107 sockbuf_set_handler(newidx, &client_handler, session);
108 flags = calloc(1, sizeof(*flags));
109 sockbuf_attach_filter(newidx, &telnet_filter, flags);
110 linemode_on(newidx);
111
112 /* Stealth logins are where we don't say anything until we know they
113 * are a valid user. */
114 if (core_config.telnet_stealth) {
115 session->state = STATE_RESOLVE;
116 }
117 else {
118 sockbuf_write(newidx, "\r\nPlease enter your nickname.\r\n", -1);
119 session->state = STATE_NICKNAME;
120 session->count = 0;
121 }
122
123 /* Start lookups. */
124 egg_ident_lookup(peer_ip, peer_port, telnet_port, -1, ident_result, session);
125 egg_dns_reverse(peer_ip, -1, dns_result, session);
126
127 return(0);
128 }
129
130 static int ident_result(void *client_data, const char *ip, int port, const char *reply)
131 {
132 telnet_session_t *session = client_data;
133
134 if (reply) session->ident = strdup(reply);
135 else session->ident = strdup("~telnet");
136 process_results(session);
137 return(0);
138 }
139
140 static int dns_result(void *client_data, const char *ip, const char *host)
141 {
142 telnet_session_t *session = client_data;
143
144 session->host = strdup(host);
145 process_results(session);
146 return(0);
147 }
148
149 static int process_results(telnet_session_t *session)
150 {
151 char *fakehost;
152
153 if (!session->ident || !session->host) return(0);
154
155 if (session->state == STATE_PARTYLINE) {
156 partyline_update_info(session->pid, session->ident, session->host);
157 return(0);
158 }
159 if (session->flags & STEALTH_LOGIN) {
160 fakehost = msprintf("-telnet!%s@%s", session->ident, session->host);
161 if (!user_lookup_by_irchost(fakehost)) {
162 kill_session(session);
163 return(0);
164 }
165 sockbuf_write(session->idx, "\r\nPlease enter your nickname.\r\n", -1);
166 session->state = STATE_NICKNAME;
167 }
168 return(0);
169 }
170
171 static int telnet_on_read(void *client_data, int idx, char *data, int len)
172 {
173 telnet_session_t *session = client_data;
174
175 switch (session->state) {
176 case STATE_PARTYLINE:
177 partyline_on_input(session->pid, data, len);
178 break;
179 case STATE_NICKNAME:
180 session->nick = strdup(data);
181 session->state = STATE_PASSWORD;
182 sockbuf_write(session->idx, TELNET_ECHO_OFF, -1);
183 sockbuf_write(session->idx, "Please enter your password.\r\n", -1);
184 break;
185 case STATE_PASSWORD:
186 sockbuf_write(session->idx, TELNET_ECHO_ON, -1);
187 session->user = user_lookup_authed(session->nick, data);
188 if (!session->user) {
189 sockbuf_write(session->idx, "Invalid username/password.\r\n\r\n", -1);
190 session->count++;
191 if (session->count > core_config.telnet_max_retries) {
192 kill_session(session);
193 break;
194 }
195 free(session->nick);
196 session->nick = NULL;
197 sockbuf_write(session->idx, "Please enter your nickname.\r\n", -1);
198 session->state = STATE_NICKNAME;
199 }
200 else {
201 session->pid = partyline_connect(session->idx, -1, session->user, session->nick, session->ident ? session->ident : "~telnet", session->host ? session->host : session->ip);
202 session->state = STATE_PARTYLINE;
203 egg_iprintf(idx, "\r\nWelcome to the telnet partyline interface!\r\n");
204 if (session->ident) egg_iprintf(idx, "Your ident is: %s\r\n", session->ident);
205 if (session->host) egg_iprintf(idx, "Your hostname is: %s\r\n", session->host);
206 }
207 break;
208 }
209 return(0);
210 }
211
212 static int telnet_on_eof(void *client_data, int idx, int err, const char *errmsg)
213 {
214 telnet_session_t *session = client_data;
215
216 if (session->state == STATE_PARTYLINE) partyline_disconnect(session->pid, err ? errmsg : NULL);
217 kill_session(session);
218 return(0);
219 }
220
221 static int telnet_filter_read(void *client_data, int idx, char *data, int len)
222 {
223 char *cmd;
224 int type, arg, remove, flags;
225
226 flags = *(int *)client_data;
227 cmd = data;
228 while ((cmd = memchr(cmd, TELNET_CMD, len))) {
229 type = *(unsigned char *)(cmd+1);
230 if (type == TELNET_CMD) remove = 1;
231 else if (type == TELNET_DO) {
232 arg = *(cmd+2);
233 if (arg == TELNET_ECHO) flags |= TFLAG_ECHO;
234 remove = 3;
235 }
236 else if (type == TELNET_DONT) {
237 arg = *(cmd+2);
238 if (arg == TELNET_ECHO) flags &= ~TFLAG_ECHO;
239 remove = 3;
240 }
241 else if (type == TELNET_WILL || type == TELNET_WONT) {
242 remove = 3;
243 }
244 else {
245 remove = 2;
246 }
247 memmove(cmd, cmd+remove, len - (cmd - data) - remove + 1);
248 len -= remove;
249 }
250 if (len) {
251 if (flags & TFLAG_ECHO) sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, len);
252 sockbuf_on_read(idx, TELNET_FILTER_LEVEL, data, len);
253 }
254 return(0);
255 }
256
257 /* We replace \n with \r\n and \255 with \255\255. */
258 static int telnet_filter_write(void *client_data, int idx, const char *data, int len)
259 {
260 const char *newline;
261 int left, linelen, r, r2;
262
263 newline = data;
264 r = 0;
265 left = len;
266 while ((newline = memchr(newline, '\n', left))) {
267 linelen = newline - data;
268 if (linelen > 0 && newline[-1] == '\r') {
269 newline++;
270 left = len - linelen - 1;
271 continue;
272 }
273
274 r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, linelen);
275 if (r2 < 0) return(r2);
276 r += r2;
277 r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, "\r\n", 2);
278 if (r2 < 0) return(r2);
279 r += r2;
280 data = newline+1;
281 newline = data;
282 len -= linelen+1;
283 left = len;
284 }
285 if (len > 0) {
286 r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, len);
287 if (r2 < 0) return(r2);
288 r += r2;
289 }
290 return(r);
291 }
292
293 static int telnet_filter_delete(void *client_data, int idx)
294 {
295 free(client_data);
296 return(0);
297 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23