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

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

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


Revision 1.4 - (hide annotations) (download) (as text)
Mon Apr 7 07:20:11 2003 UTC (16 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.3: +1 -0 lines
File MIME type: text/x-chdr
* Renamed match_nick to irccmp
* Added a default fake005 in case it's not set in the config file

1 stdarg 1.1 #include <eggdrop/eggdrop.h>
2 stdarg 1.3 #include "core_config.h"
3 stdarg 1.1
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 stdarg 1.3 telnet_idx = egg_server(core_config.telnet_vhost, core_config.telnet_port, &telnet_port);
83 stdarg 1.1 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 stdarg 1.3 if (core_config.telnet_stealth) {
115 stdarg 1.1 session->state = STATE_RESOLVE;
116     }
117     else {
118 stdarg 1.2 sockbuf_write(newidx, "\r\nPlease enter your nickname.\r\n", -1);
119 stdarg 1.1 session->state = STATE_NICKNAME;
120     session->count = 0;
121     }
122    
123     /* Start lookups. */
124 stdarg 1.3 egg_ident_lookup(peer_ip, peer_port, telnet_port, -1, ident_result, session);
125 stdarg 1.2 egg_dns_reverse(peer_ip, -1, dns_result, session);
126 stdarg 1.1
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 stdarg 1.2 static int dns_result(void *client_data, const char *ip, const char *host)
141 stdarg 1.1 {
142     telnet_session_t *session = client_data;
143    
144 stdarg 1.4 if (!host) host = ip;
145 stdarg 1.1 session->host = strdup(host);
146     process_results(session);
147     return(0);
148     }
149    
150     static int process_results(telnet_session_t *session)
151     {
152     char *fakehost;
153    
154     if (!session->ident || !session->host) return(0);
155    
156     if (session->state == STATE_PARTYLINE) {
157     partyline_update_info(session->pid, session->ident, session->host);
158     return(0);
159     }
160     if (session->flags & STEALTH_LOGIN) {
161     fakehost = msprintf("-telnet!%s@%s", session->ident, session->host);
162     if (!user_lookup_by_irchost(fakehost)) {
163     kill_session(session);
164     return(0);
165     }
166 stdarg 1.2 sockbuf_write(session->idx, "\r\nPlease enter your nickname.\r\n", -1);
167 stdarg 1.1 session->state = STATE_NICKNAME;
168     }
169     return(0);
170     }
171    
172     static int telnet_on_read(void *client_data, int idx, char *data, int len)
173     {
174     telnet_session_t *session = client_data;
175    
176     switch (session->state) {
177     case STATE_PARTYLINE:
178     partyline_on_input(session->pid, data, len);
179     break;
180     case STATE_NICKNAME:
181     session->nick = strdup(data);
182     session->state = STATE_PASSWORD;
183     sockbuf_write(session->idx, TELNET_ECHO_OFF, -1);
184     sockbuf_write(session->idx, "Please enter your password.\r\n", -1);
185     break;
186     case STATE_PASSWORD:
187     sockbuf_write(session->idx, TELNET_ECHO_ON, -1);
188     session->user = user_lookup_authed(session->nick, data);
189     if (!session->user) {
190     sockbuf_write(session->idx, "Invalid username/password.\r\n\r\n", -1);
191     session->count++;
192 stdarg 1.3 if (session->count > core_config.telnet_max_retries) {
193 stdarg 1.1 kill_session(session);
194     break;
195     }
196     free(session->nick);
197     session->nick = NULL;
198     sockbuf_write(session->idx, "Please enter your nickname.\r\n", -1);
199     session->state = STATE_NICKNAME;
200     }
201     else {
202 stdarg 1.2 session->pid = partyline_connect(session->idx, -1, session->user, session->nick, session->ident ? session->ident : "~telnet", session->host ? session->host : session->ip);
203 stdarg 1.1 session->state = STATE_PARTYLINE;
204 stdarg 1.2 egg_iprintf(idx, "\r\nWelcome to the telnet partyline interface!\r\n");
205     if (session->ident) egg_iprintf(idx, "Your ident is: %s\r\n", session->ident);
206     if (session->host) egg_iprintf(idx, "Your hostname is: %s\r\n", session->host);
207 stdarg 1.1 }
208     break;
209     }
210     return(0);
211     }
212    
213     static int telnet_on_eof(void *client_data, int idx, int err, const char *errmsg)
214     {
215     telnet_session_t *session = client_data;
216    
217 stdarg 1.2 if (session->state == STATE_PARTYLINE) partyline_disconnect(session->pid, err ? errmsg : NULL);
218 stdarg 1.1 kill_session(session);
219     return(0);
220     }
221    
222     static int telnet_filter_read(void *client_data, int idx, char *data, int len)
223     {
224     char *cmd;
225     int type, arg, remove, flags;
226    
227     flags = *(int *)client_data;
228     cmd = data;
229     while ((cmd = memchr(cmd, TELNET_CMD, len))) {
230     type = *(unsigned char *)(cmd+1);
231     if (type == TELNET_CMD) remove = 1;
232     else if (type == TELNET_DO) {
233     arg = *(cmd+2);
234     if (arg == TELNET_ECHO) flags |= TFLAG_ECHO;
235     remove = 3;
236     }
237     else if (type == TELNET_DONT) {
238     arg = *(cmd+2);
239     if (arg == TELNET_ECHO) flags &= ~TFLAG_ECHO;
240     remove = 3;
241     }
242     else if (type == TELNET_WILL || type == TELNET_WONT) {
243     remove = 3;
244     }
245     else {
246     remove = 2;
247     }
248     memmove(cmd, cmd+remove, len - (cmd - data) - remove + 1);
249     len -= remove;
250     }
251     if (len) {
252     if (flags & TFLAG_ECHO) sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, len);
253     sockbuf_on_read(idx, TELNET_FILTER_LEVEL, data, len);
254     }
255     return(0);
256     }
257    
258     /* We replace \n with \r\n and \255 with \255\255. */
259     static int telnet_filter_write(void *client_data, int idx, const char *data, int len)
260     {
261     const char *newline;
262 stdarg 1.2 int left, linelen, r, r2;
263 stdarg 1.1
264     newline = data;
265     r = 0;
266 stdarg 1.2 left = len;
267     while ((newline = memchr(newline, '\n', left))) {
268 stdarg 1.1 linelen = newline - data;
269     if (linelen > 0 && newline[-1] == '\r') {
270     newline++;
271 stdarg 1.2 left = len - linelen - 1;
272 stdarg 1.1 continue;
273     }
274    
275     r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, linelen);
276     if (r2 < 0) return(r2);
277     r += r2;
278     r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, "\r\n", 2);
279     if (r2 < 0) return(r2);
280     r += r2;
281     data = newline+1;
282     newline = data;
283     len -= linelen+1;
284 stdarg 1.2 left = len;
285 stdarg 1.1 }
286     if (len > 0) {
287     r2 = sockbuf_on_write(idx, TELNET_FILTER_LEVEL, data, len);
288     if (r2 < 0) return(r2);
289     r += r2;
290     }
291     return(r);
292     }
293    
294     static int telnet_filter_delete(void *client_data, int idx)
295     {
296     free(client_data);
297     return(0);
298     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23