1 | /***********************************************************/ |
---|
2 | /* |
---|
3 | * |
---|
4 | * The telnet DAEMON |
---|
5 | * |
---|
6 | * Author: 17,may 2001 |
---|
7 | * |
---|
8 | * WORK: fernando.ruiz@ctv.es |
---|
9 | * HOME: correo@fernando-ruiz.com |
---|
10 | * |
---|
11 | * After start the net you can start this daemon. |
---|
12 | * It uses the previously inited pseudo-terminales (pty.c) |
---|
13 | * getting a new terminal with getpty(). This function |
---|
14 | * gives a terminal name passing a opened socket like parameter. |
---|
15 | * |
---|
16 | * With register_telnetd() you add a new command in the shell to start |
---|
17 | * this daemon interactively. (Login in /dev/console of course) |
---|
18 | * |
---|
19 | * Sorry but OOB is not still implemented. (This is the first version) |
---|
20 | * |
---|
21 | * Till Straumann <strauman@slac.stanford.edu> |
---|
22 | * - made the 'shell' interface more generic, i.e. it is now |
---|
23 | * possible to have 'telnetd' run an arbitrary 'shell' |
---|
24 | * program. |
---|
25 | */ |
---|
26 | |
---|
27 | /* |
---|
28 | LICENSE INFORMATION |
---|
29 | |
---|
30 | RTEMS is free software; you can redistribute it and/or modify it under |
---|
31 | terms of the GNU General Public License as published by the |
---|
32 | Free Software Foundation; either version 2, or (at your option) any |
---|
33 | later version. RTEMS is distributed in the hope that it will be useful, |
---|
34 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
35 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
36 | General Public License for more details. You should have received |
---|
37 | a copy of the GNU General Public License along with RTEMS; see |
---|
38 | file COPYING. If not, write to the Free Software Foundation, 675 |
---|
39 | Mass Ave, Cambridge, MA 02139, USA. |
---|
40 | |
---|
41 | As a special exception, including RTEMS header files in a file, |
---|
42 | instantiating RTEMS generics or templates, or linking other files |
---|
43 | with RTEMS objects to produce an executable application, does not |
---|
44 | by itself cause the resulting executable application to be covered |
---|
45 | by the GNU General Public License. This exception does not |
---|
46 | however invalidate any other reasons why the executable file might be |
---|
47 | covered by the GNU Public License. |
---|
48 | */ |
---|
49 | |
---|
50 | #ifdef HAVE_CONFIG_H |
---|
51 | #include "config.h" |
---|
52 | #endif |
---|
53 | |
---|
54 | #include <rtems.h> |
---|
55 | #include <rtems/error.h> |
---|
56 | #include <rtems/pty.h> |
---|
57 | #include <rtems/shell.h> |
---|
58 | #include <rtems/telnetd.h> |
---|
59 | #include <rtems/bspIo.h> |
---|
60 | #include <sys/socket.h> |
---|
61 | #include <netinet/in.h> |
---|
62 | #include <arpa/inet.h> |
---|
63 | #include <unistd.h> |
---|
64 | #include <stdlib.h> |
---|
65 | #include <stdio.h> |
---|
66 | #include <assert.h> |
---|
67 | #include <string.h> |
---|
68 | #include <syslog.h> |
---|
69 | |
---|
70 | #include <rtems/userenv.h> |
---|
71 | #include <rtems/error.h> |
---|
72 | #include <rtems/rtems_bsdnet.h> |
---|
73 | |
---|
74 | #define PARANOIA |
---|
75 | |
---|
76 | extern char *telnet_get_pty(int socket); |
---|
77 | extern int telnet_pty_initialize(); |
---|
78 | extern int telnet_pty_finalize(); |
---|
79 | |
---|
80 | struct shell_args { |
---|
81 | char *devname; |
---|
82 | void *arg; |
---|
83 | char peername[16]; |
---|
84 | char delete_myself; |
---|
85 | }; |
---|
86 | |
---|
87 | typedef union uni_sa { |
---|
88 | struct sockaddr_in sin; |
---|
89 | struct sockaddr sa; |
---|
90 | } uni_sa; |
---|
91 | |
---|
92 | static int sockpeername(int sock, char *buf, int bufsz); |
---|
93 | |
---|
94 | static int initialize_telnetd(); |
---|
95 | |
---|
96 | void * telnetd_dflt_spawn(const char *name, unsigned priority, unsigned stackSize, void (*fn)(void*), void *fnarg); |
---|
97 | |
---|
98 | /***********************************************************/ |
---|
99 | rtems_id telnetd_task_id =0; |
---|
100 | uint32_t telnetd_stack_size =32000; |
---|
101 | rtems_task_priority telnetd_task_priority=0; |
---|
102 | int telnetd_dont_spawn =0; |
---|
103 | void (*telnetd_shell)(char *, void*)=0; |
---|
104 | void *telnetd_shell_arg =0; |
---|
105 | void * (*telnetd_spawn_task)(const char *, unsigned, unsigned, void (*)(void*), void *) = telnetd_dflt_spawn; |
---|
106 | |
---|
107 | static char *grab_a_Connection(int des_socket, uni_sa *srv, char *peername, int sz) |
---|
108 | { |
---|
109 | char *rval = 0; |
---|
110 | #if 0 |
---|
111 | socklen_t |
---|
112 | #else |
---|
113 | /* 4.6 doesn't have socklen_t */ |
---|
114 | uint32_t |
---|
115 | #endif |
---|
116 | size_adr = sizeof(srv->sin); |
---|
117 | int acp_sock; |
---|
118 | |
---|
119 | acp_sock = accept(des_socket,&srv->sa,&size_adr); |
---|
120 | |
---|
121 | if (acp_sock<0) { |
---|
122 | perror("telnetd:accept"); |
---|
123 | goto bailout; |
---|
124 | }; |
---|
125 | |
---|
126 | if ( !(rval=telnet_get_pty(acp_sock)) ) { |
---|
127 | syslog( LOG_DAEMON | LOG_ERR, "telnetd: unable to obtain PTY"); |
---|
128 | /* NOTE: failing 'do_get_pty()' closed the socket */ |
---|
129 | goto bailout; |
---|
130 | } |
---|
131 | |
---|
132 | if (sockpeername(acp_sock, peername, sz)) |
---|
133 | strncpy(peername, "<UNKNOWN>", sz); |
---|
134 | |
---|
135 | #ifdef PARANOIA |
---|
136 | syslog(LOG_DAEMON | LOG_INFO, |
---|
137 | "telnetd: accepted connection from %s on %s", |
---|
138 | peername, |
---|
139 | rval); |
---|
140 | #endif |
---|
141 | |
---|
142 | bailout: |
---|
143 | |
---|
144 | return rval; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | static void release_a_Connection(char *devname, char *peername, FILE **pstd, int n) |
---|
149 | { |
---|
150 | |
---|
151 | #ifdef PARANOIA |
---|
152 | syslog( LOG_DAEMON | LOG_INFO, |
---|
153 | "telnetd: releasing connection from %s on %s", |
---|
154 | peername, |
---|
155 | devname ); |
---|
156 | #endif |
---|
157 | |
---|
158 | while (--n>=0) |
---|
159 | if (pstd[n]) fclose(pstd[n]); |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | static int sockpeername(int sock, char *buf, int bufsz) |
---|
164 | { |
---|
165 | uni_sa peer; |
---|
166 | #if 0 |
---|
167 | socklen_t |
---|
168 | #else |
---|
169 | /* 4.6 doesn't have socklen_t */ |
---|
170 | uint32_t |
---|
171 | #endif |
---|
172 | len = sizeof(peer.sin); |
---|
173 | |
---|
174 | int rval = sock < 0; |
---|
175 | |
---|
176 | if ( !rval) |
---|
177 | rval = getpeername(sock, &peer.sa, &len); |
---|
178 | |
---|
179 | if ( !rval ) |
---|
180 | rval = !inet_ntop( AF_INET, &peer.sin.sin_addr, buf, bufsz ); |
---|
181 | |
---|
182 | return rval; |
---|
183 | } |
---|
184 | |
---|
185 | #if 1 |
---|
186 | #define INSIDE_TELNETD |
---|
187 | #include "check_passwd.c" |
---|
188 | #else |
---|
189 | #define check_passwd(arg) 0 |
---|
190 | #endif |
---|
191 | |
---|
192 | |
---|
193 | static void |
---|
194 | spawned_shell(void *arg); |
---|
195 | |
---|
196 | /***********************************************************/ |
---|
197 | static void |
---|
198 | rtems_task_telnetd(void *task_argument) |
---|
199 | { |
---|
200 | int des_socket; |
---|
201 | uni_sa srv; |
---|
202 | char *devname; |
---|
203 | char peername[16]; |
---|
204 | int i=1; |
---|
205 | int size_adr; |
---|
206 | struct shell_args *arg; |
---|
207 | |
---|
208 | if ((des_socket=socket(PF_INET,SOCK_STREAM,0))<0) { |
---|
209 | perror("telnetd:socket"); |
---|
210 | telnetd_task_id=0; |
---|
211 | rtems_task_delete(RTEMS_SELF); |
---|
212 | }; |
---|
213 | setsockopt(des_socket,SOL_SOCKET,SO_KEEPALIVE,&i,sizeof(i)); |
---|
214 | |
---|
215 | memset(&srv,0,sizeof(srv)); |
---|
216 | srv.sin.sin_family=AF_INET; |
---|
217 | srv.sin.sin_port=htons(23); |
---|
218 | size_adr=sizeof(srv.sin); |
---|
219 | if ((bind(des_socket,&srv.sa,size_adr))<0) { |
---|
220 | perror("telnetd:bind"); |
---|
221 | close(des_socket); |
---|
222 | telnetd_task_id=0; |
---|
223 | rtems_task_delete(RTEMS_SELF); |
---|
224 | }; |
---|
225 | if ((listen(des_socket,5))<0) { |
---|
226 | perror("telnetd:listen"); |
---|
227 | close(des_socket); |
---|
228 | telnetd_task_id=0; |
---|
229 | rtems_task_delete(RTEMS_SELF); |
---|
230 | }; |
---|
231 | |
---|
232 | /* we don't redirect stdio as this probably |
---|
233 | * was started from the console anyways.. |
---|
234 | */ |
---|
235 | do { |
---|
236 | devname = grab_a_Connection(des_socket, &srv, peername, sizeof(peername)); |
---|
237 | |
---|
238 | if ( !devname ) { |
---|
239 | /* if something went wrong, sleep for some time */ |
---|
240 | sleep(10); |
---|
241 | continue; |
---|
242 | } |
---|
243 | if ( telnetd_dont_spawn ) { |
---|
244 | if ( 0 == check_passwd(peername) ) |
---|
245 | telnetd_shell(devname, telnetd_shell_arg); |
---|
246 | } else { |
---|
247 | arg = malloc( sizeof(*arg) ); |
---|
248 | |
---|
249 | arg->devname = devname; |
---|
250 | arg->arg = telnetd_shell_arg; |
---|
251 | strncpy(arg->peername, peername, sizeof(arg->peername)); |
---|
252 | |
---|
253 | if ( !telnetd_spawn_task(&devname[5], telnetd_task_priority, telnetd_stack_size, spawned_shell, arg) ) { |
---|
254 | |
---|
255 | FILE *dummy; |
---|
256 | |
---|
257 | if ( telnetd_spawn_task != telnetd_dflt_spawn ) { |
---|
258 | fprintf(stderr,"Telnetd: Unable to spawn child task\n"); |
---|
259 | } |
---|
260 | |
---|
261 | /* hmm - the pty driver slot can only be |
---|
262 | * released by opening and subsequently |
---|
263 | * closing the PTY - this also closes |
---|
264 | * the underlying socket. So we mock up |
---|
265 | * a stream... |
---|
266 | */ |
---|
267 | |
---|
268 | if ( !(dummy=fopen(devname,"r+")) ) |
---|
269 | perror("Unable to dummy open the pty, losing a slot :-("); |
---|
270 | release_a_Connection(devname, peername, &dummy, 1); |
---|
271 | free(arg); |
---|
272 | sleep(2); /* don't accept connections too fast */ |
---|
273 | } |
---|
274 | } |
---|
275 | } while(1); |
---|
276 | /* TODO: how to free the connection semaphore? But then - |
---|
277 | * stopping the daemon is probably only needed during |
---|
278 | * development/debugging. |
---|
279 | * Finalizer code should collect all the connection semaphore |
---|
280 | * counts and eventually clean up... |
---|
281 | */ |
---|
282 | close(des_socket); |
---|
283 | telnetd_task_id=0; |
---|
284 | } |
---|
285 | /***********************************************************/ |
---|
286 | static int initialize_telnetd(void) { |
---|
287 | |
---|
288 | if (telnetd_task_id ) return RTEMS_RESOURCE_IN_USE; |
---|
289 | if (telnetd_stack_size<=0 ) telnetd_stack_size =32000; |
---|
290 | |
---|
291 | if ( !telnetd_spawn_task("TNTD", telnetd_task_priority, RTEMS_MINIMUM_STACK_SIZE, rtems_task_telnetd, 0) ) { |
---|
292 | return -1; |
---|
293 | } |
---|
294 | return 0; |
---|
295 | } |
---|
296 | |
---|
297 | /***********************************************************/ |
---|
298 | int startTelnetd(void (*cmd)(char *, void *), void *arg, int dontSpawn, int stack, int priority) |
---|
299 | { |
---|
300 | rtems_status_code sc; |
---|
301 | |
---|
302 | printf("This is rtems-telnetd (modified by Till Straumann)\n"); |
---|
303 | printf("$Id$\n"); |
---|
304 | printf("Release $Name$\n"); |
---|
305 | |
---|
306 | if ( !telnetd_shell && !cmd ) { |
---|
307 | fprintf(stderr,"startTelnetd(): setup error - NO SHELL; bailing out\n"); |
---|
308 | return 1; |
---|
309 | } |
---|
310 | |
---|
311 | if (telnetd_task_id) { |
---|
312 | fprintf(stderr,"ERROR:telnetd already started\n"); |
---|
313 | return 1; |
---|
314 | }; |
---|
315 | |
---|
316 | if ( !telnet_pty_initialize() ) { |
---|
317 | fprintf(stderr,"PTY driver probably not properly registered\n"); |
---|
318 | return 1; |
---|
319 | } |
---|
320 | |
---|
321 | if (cmd) |
---|
322 | telnetd_shell = cmd; |
---|
323 | telnetd_shell_arg = arg; |
---|
324 | telnetd_stack_size = stack; |
---|
325 | if ( !priority ) { |
---|
326 | priority = rtems_bsdnet_config.network_task_priority; |
---|
327 | } |
---|
328 | if ( priority < 2 ) |
---|
329 | priority=100; |
---|
330 | telnetd_task_priority = priority; |
---|
331 | telnetd_dont_spawn = dontSpawn; |
---|
332 | |
---|
333 | sc=initialize_telnetd(); |
---|
334 | if (sc!=RTEMS_SUCCESSFUL) return sc; |
---|
335 | printf("rtems_telnetd() started with stacksize=%u,priority=%d\n", |
---|
336 | (unsigned)telnetd_stack_size,(int)telnetd_task_priority); |
---|
337 | return 0; |
---|
338 | } |
---|
339 | |
---|
340 | /* utility wrapper */ |
---|
341 | static void |
---|
342 | spawned_shell(void *targ) |
---|
343 | { |
---|
344 | rtems_status_code sc; |
---|
345 | FILE *nstd[3]={0}; |
---|
346 | FILE *ostd[3]={ stdin, stdout, stderr }; |
---|
347 | int i=0; |
---|
348 | struct shell_args *arg = targ; |
---|
349 | |
---|
350 | sc=rtems_libio_set_private_env(); |
---|
351 | |
---|
352 | /* newlib hack/workaround. Before we change stdin/out/err we must make |
---|
353 | * sure the internal data are initialized (fileno(stdout) has this sideeffect). |
---|
354 | * This should probably be done from RTEMS' libc support layer... |
---|
355 | * (T.S., newlibc-1.13; 2005/10) |
---|
356 | */ |
---|
357 | |
---|
358 | fileno(stdout); |
---|
359 | |
---|
360 | if (RTEMS_SUCCESSFUL != sc) { |
---|
361 | rtems_error(sc,"rtems_libio_set_private_env"); |
---|
362 | goto cleanup; |
---|
363 | } |
---|
364 | |
---|
365 | /* redirect stdio */ |
---|
366 | for (i=0; i<3; i++) { |
---|
367 | if ( !(nstd[i]=fopen(arg->devname,"r+")) ) { |
---|
368 | perror("unable to open stdio"); |
---|
369 | goto cleanup; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | stdin = nstd[0]; |
---|
374 | stdout = nstd[1]; |
---|
375 | stderr = nstd[2]; |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | #if 0 |
---|
380 | printk("STDOUT is now %x (%x) (FD %i/%i)\n",stdout,nstd[1],fileno(stdout),fileno(nstd[1])); |
---|
381 | printf("hello\n"); |
---|
382 | write(fileno(stdout),"hellofd\n",8); |
---|
383 | #endif |
---|
384 | |
---|
385 | /* call their routine */ |
---|
386 | if ( 0 == check_passwd(arg->peername) ) |
---|
387 | telnetd_shell(arg->devname, arg->arg); |
---|
388 | |
---|
389 | stdin = ostd[0]; |
---|
390 | stdout = ostd[1]; |
---|
391 | stderr = ostd[2]; |
---|
392 | |
---|
393 | cleanup: |
---|
394 | release_a_Connection(arg->devname, arg->peername, nstd, i); |
---|
395 | free(arg); |
---|
396 | } |
---|
397 | |
---|
398 | struct wrap_delete_args { |
---|
399 | void (*t)(void *); |
---|
400 | void *a; |
---|
401 | }; |
---|
402 | |
---|
403 | static rtems_task |
---|
404 | wrap_delete(rtems_task_argument arg) |
---|
405 | { |
---|
406 | struct wrap_delete_args *pwa = (struct wrap_delete_args *)arg; |
---|
407 | register void (*t)(void *) = pwa->t; |
---|
408 | register void *a = pwa->a; |
---|
409 | |
---|
410 | /* free argument before calling function (which may never return if |
---|
411 | * they choose to delete themselves) |
---|
412 | */ |
---|
413 | free(pwa); |
---|
414 | t(a); |
---|
415 | rtems_task_delete(RTEMS_SELF); |
---|
416 | } |
---|
417 | |
---|
418 | void * |
---|
419 | telnetd_dflt_spawn(const char *name, unsigned int priority, unsigned int stackSize, void (*fn)(void *), void* fnarg) |
---|
420 | { |
---|
421 | rtems_status_code sc; |
---|
422 | rtems_id task_id; |
---|
423 | char nm[4] = {'X','X','X','X' }; |
---|
424 | struct wrap_delete_args *pwa = malloc(sizeof(*pwa)); |
---|
425 | |
---|
426 | strncpy(nm, name, 4); |
---|
427 | |
---|
428 | if ( !pwa ) { |
---|
429 | perror("Telnetd: no memory\n"); |
---|
430 | return 0; |
---|
431 | } |
---|
432 | |
---|
433 | pwa->t = fn; |
---|
434 | pwa->a = fnarg; |
---|
435 | |
---|
436 | if ((sc=rtems_task_create( |
---|
437 | rtems_build_name(nm[0], nm[1], nm[2], nm[3]), |
---|
438 | (rtems_task_priority)priority, |
---|
439 | stackSize, |
---|
440 | RTEMS_DEFAULT_MODES, |
---|
441 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
442 | &task_id)) || |
---|
443 | (sc=rtems_task_start( |
---|
444 | task_id, |
---|
445 | wrap_delete, |
---|
446 | (rtems_task_argument)pwa))) { |
---|
447 | free(pwa); |
---|
448 | rtems_error(sc,"Telnetd: spawning task failed"); |
---|
449 | return 0; |
---|
450 | } |
---|
451 | return (void*)task_id; |
---|
452 | } |
---|
453 | |
---|
454 | /* convenience routines for CEXP (retrieve stdio descriptors |
---|
455 | * from reent structure) |
---|
456 | * |
---|
457 | */ |
---|
458 | #ifdef stdin |
---|
459 | static __inline__ FILE * |
---|
460 | _stdin(void) { return stdin; } |
---|
461 | #undef stdin |
---|
462 | FILE *stdin(void) { return _stdin(); } |
---|
463 | #endif |
---|
464 | #ifdef stdout |
---|
465 | static __inline__ FILE * |
---|
466 | _stdout(void) { return stdout; } |
---|
467 | #undef stdout |
---|
468 | FILE *stdout(void) { return _stdout(); } |
---|
469 | #endif |
---|
470 | #ifdef stderr |
---|
471 | static __inline__ FILE * |
---|
472 | _stderr(void) { return stderr; } |
---|
473 | #undef stderr |
---|
474 | FILE *stderr(void) { return _stderr(); } |
---|
475 | #endif |
---|
476 | |
---|
477 | /* MUST NOT USE stdin & friends below here !!!!!!!!!!!!! */ |
---|