/[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.1 - (show annotations) (download) (as text)
Sun Nov 3 23:16:09 2002 UTC (16 years, 8 months ago) by stdarg
Branch: MAIN
File MIME type: text/x-chdr
* Moved md5 code to lib/eggdrop so I can use it for password hashing
* Passwords are stored as standard hex encoding of md5(salt + password)
* Started moving script interface functions to modules/script

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23