source: rtems-libbsd/rtemsbsd/telnetd/telnetd.c @ b78c1d2

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since b78c1d2 was b78c1d2, checked in by Sebastian Huber <sebastian.huber@…>, on 09/18/14 at 07:47:57

telnetd: Avoid rtems_bsdnet_config

  • Property mode set to 100644
File size: 11.7 KB
Line 
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 * Copyright (c) 2009 embedded brains GmbH and others.
27 *
28 * embedded brains GmbH
29 * Obere Lagerstr. 30
30 * D-82178 Puchheim
31 * Germany
32 * <rtems@embedded-brains.de>
33 *
34 * The license and distribution terms for this file may be
35 * found in the file LICENSE in this distribution or at
36 * http://www.rtems.org/license/LICENSE.
37 */
38
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42
43#include <rtems.h>
44#include <rtems/error.h>
45#include <rtems/pty.h>
46#include <rtems/shell.h>
47#include <rtems/telnetd.h>
48#include <rtems/bspIo.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <arpa/inet.h>
52#include <unistd.h>
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
56#include <syslog.h>
57
58#include <rtems/userenv.h>
59#include <rtems/error.h>
60#include <rtems/rtems_bsdnet.h>
61
62#define PARANOIA
63
64extern char *telnet_get_pty(int socket);
65extern int   telnet_pty_initialize(void);
66
67struct shell_args {
68  char *devname;
69  void *arg;
70  char  peername[16];
71  char  delete_myself;
72};
73
74typedef union uni_sa {
75  struct sockaddr_in sin;
76  struct sockaddr     sa;
77} uni_sa;
78
79static int sockpeername(int sock, char *buf, int bufsz);
80
81rtems_id telnetd_dflt_spawn(
82  const char *name,
83  unsigned priority,
84  unsigned stackSize,
85  void (*fn)(void*),
86  void *fnarg
87);
88
89/***********************************************************/
90static rtems_id telnetd_task_id = RTEMS_ID_NONE;
91
92rtems_id (*telnetd_spawn_task)(
93  const char *,
94  unsigned,
95  unsigned,
96  void (*)(void*),
97  void *
98) = telnetd_dflt_spawn;
99
100static char *grab_a_Connection(
101  int des_socket,
102  uni_sa *srv,
103  char *peername,
104  int sz
105)
106{
107  char *rval = 0;
108  socklen_t size_adr = sizeof(srv->sin);
109  int acp_sock;
110
111  acp_sock = accept(des_socket,&srv->sa,&size_adr);
112
113  if (acp_sock<0) {
114    perror("telnetd:accept");
115    goto bailout;
116  };
117
118  if ( !(rval=telnet_get_pty(acp_sock)) ) {
119    syslog( LOG_DAEMON | LOG_ERR, "telnetd: unable to obtain PTY");
120    /* NOTE: failing 'do_get_pty()' closed the socket */
121    goto bailout;
122  }
123
124  if (sockpeername(acp_sock, peername, sz))
125    strncpy(peername, "<UNKNOWN>", sz);
126
127#ifdef PARANOIA
128  syslog(LOG_DAEMON | LOG_INFO,
129      "telnetd: accepted connection from %s on %s",
130      peername,
131      rval);
132#endif
133
134bailout:
135
136  return rval;
137}
138
139
140static void release_a_Connection(char *devname, char *peername, FILE **pstd, int n)
141{
142
143#ifdef PARANOIA
144  syslog( LOG_DAEMON | LOG_INFO,
145      "telnetd: releasing connection from %s on %s",
146      peername,
147      devname );
148#endif
149
150  while (--n>=0)
151    if (pstd[n]) fclose(pstd[n]);
152
153}
154
155static int sockpeername(int sock, char *buf, int bufsz)
156{
157  uni_sa peer;
158  int    rval = sock < 0;
159  socklen_t len  = sizeof(peer.sin);
160
161  if ( !rval )
162    rval = getpeername(sock, &peer.sa, &len);
163
164  if ( !rval )
165    rval = !inet_ntop( AF_INET, &peer.sin.sin_addr, buf, bufsz );
166
167  return rval;
168}
169
170static void
171spawned_shell(void *arg);
172
173/***********************************************************/
174static void
175rtems_task_telnetd(void *task_argument)
176{
177  int                des_socket;
178  uni_sa             srv;
179  char              *devname;
180  char               peername[16];
181  int                i=1;
182  int                size_adr;
183  struct shell_args *arg = NULL;
184
185  if ((des_socket=socket(PF_INET,SOCK_STREAM,0))<0) {
186    perror("telnetd:socket");
187    telnetd_task_id = RTEMS_ID_NONE;
188    rtems_task_delete(RTEMS_SELF);
189  };
190  setsockopt(des_socket,SOL_SOCKET,SO_KEEPALIVE,&i,sizeof(i));
191
192  memset(&srv,0,sizeof(srv));
193  srv.sin.sin_family=AF_INET;
194  srv.sin.sin_port=htons(23);
195  size_adr=sizeof(srv.sin);
196  if ((bind(des_socket,&srv.sa,size_adr))<0) {
197    perror("telnetd:bind");
198    close(des_socket);
199    telnetd_task_id = RTEMS_ID_NONE;
200    rtems_task_delete(RTEMS_SELF);
201  };
202  if ((listen(des_socket,5))<0) {
203    perror("telnetd:listen");
204          close(des_socket);
205    telnetd_task_id = RTEMS_ID_NONE;
206    rtems_task_delete(RTEMS_SELF);
207  };
208
209  /* we don't redirect stdio as this probably
210   * was started from the console anyways..
211   */
212  do {
213    if (rtems_telnetd_config.keep_stdio) {
214      bool start = true;
215      char device_name [32];
216
217      ttyname_r( 1, device_name, sizeof( device_name));
218      if (rtems_telnetd_config.login_check != NULL) {
219        start = rtems_shell_login_prompt(
220          stdin,
221          stderr,
222          device_name,
223          rtems_telnetd_config.login_check
224        );
225      }
226      if (start) {
227        rtems_telnetd_config.command( device_name, arg->arg);
228      } else {
229        syslog(
230          LOG_AUTHPRIV | LOG_WARNING,
231          "telnetd: to many wrong passwords entered from %s",
232          device_name
233        );
234      }
235    } else {
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
244      arg = malloc( sizeof(*arg) );
245
246      arg->devname = devname;
247      arg->arg = rtems_telnetd_config.arg;
248      strncpy(arg->peername, peername, sizeof(arg->peername));
249
250      telnetd_task_id = telnetd_spawn_task(
251        devname,
252        rtems_telnetd_config.priority,
253        rtems_telnetd_config.stack_size,
254        spawned_shell,
255        arg
256      );
257      if (telnetd_task_id == RTEMS_ID_NONE) {
258        FILE *dummy;
259
260        if ( telnetd_spawn_task != telnetd_dflt_spawn ) {
261          fprintf(stderr,"Telnetd: Unable to spawn child task\n");
262        }
263
264        /* hmm - the pty driver slot can only be
265         * released by opening and subsequently
266         * closing the PTY - this also closes
267         * the underlying socket. So we mock up
268         * a stream...
269         */
270
271        if ( !(dummy=fopen(devname,"r+")) )
272          perror("Unable to dummy open the pty, losing a slot :-(");
273        release_a_Connection(devname, peername, &dummy, 1);
274        free(arg);
275        sleep(2); /* don't accept connections too fast */
276      }
277    }
278  } while(1);
279
280  /* TODO: how to free the connection semaphore? But then -
281   *       stopping the daemon is probably only needed during
282   *       development/debugging.
283   *       Finalizer code should collect all the connection semaphore
284   *       counts and eventually clean up...
285   */
286  close(des_socket);
287  telnetd_task_id = RTEMS_ID_NONE;
288}
289
290rtems_status_code rtems_telnetd_initialize( void)
291{
292  if (telnetd_task_id != RTEMS_ID_NONE) {
293    fprintf(stderr, "telnetd already started\n");
294    return RTEMS_RESOURCE_IN_USE;
295  }
296
297  if (rtems_telnetd_config.command == NULL) {
298    fprintf(stderr, "telnetd setup with invalid command\n");
299    return RTEMS_IO_ERROR;
300  }
301
302  if ( !telnet_pty_initialize() ) {
303    fprintf(stderr, "telnetd cannot initialize PTY driver\n");
304    return RTEMS_IO_ERROR;
305  }
306
307  /* Check priority */
308  if (rtems_telnetd_config.priority < 2) {
309    rtems_telnetd_config.priority = 100;
310  }
311
312  /* Check stack size */
313  if (rtems_telnetd_config.stack_size <= 0) {
314    rtems_telnetd_config.stack_size = (size_t)32 * 1024;
315  }
316
317  /* Spawn task */
318  telnetd_task_id = telnetd_spawn_task(
319    "TNTD",
320    rtems_telnetd_config.priority,
321    rtems_telnetd_config.stack_size,
322    rtems_task_telnetd,
323    0
324  );
325  if (telnetd_task_id == RTEMS_ID_NONE) {
326    return RTEMS_IO_ERROR;
327  }
328
329  /* Print status */
330  if (!rtems_telnetd_config.keep_stdio) {
331    fprintf(
332      stderr,
333      "telnetd started with stacksize = %u and priority = %d\n",
334      (unsigned) rtems_telnetd_config.stack_size,
335      (unsigned) rtems_telnetd_config.priority
336    );
337  }
338
339  return RTEMS_SUCCESSFUL;
340}
341
342/* utility wrapper */
343static void
344spawned_shell(void *targ)
345{
346  rtems_status_code    sc;
347  FILE                *nstd[3]={0};
348  FILE                *ostd[3]={ stdin, stdout, stderr };
349  int                  i=0;
350  struct shell_args  *arg = targ;
351  bool login_failed = false;
352  bool start = true;
353
354  sc=rtems_libio_set_private_env();
355
356  /* newlib hack/workaround. Before we change stdin/out/err we must make
357         * sure the internal data are initialized (fileno(stdout) has this sideeffect).
358   * This should probably be done from RTEMS' libc support layer...
359   * (T.S., newlibc-1.13; 2005/10)
360         */
361
362  fileno(stdout);
363
364  if (RTEMS_SUCCESSFUL != sc) {
365    rtems_error(sc,"rtems_libio_set_private_env");
366    goto cleanup;
367  }
368
369  /* redirect stdio */
370  for (i=0; i<3; i++) {
371    if ( !(nstd[i]=fopen(arg->devname,"r+")) ) {
372      perror("unable to open stdio");
373      goto cleanup;
374    }
375  }
376
377  stdin  = nstd[0];
378  stdout = nstd[1];
379  stderr = nstd[2];
380
381  #if 0
382    printk("STDOUT is now %x (%x) (FD %i/%i)\n",
383           stdout,nstd[1],fileno(stdout),fileno(nstd[1]));
384    printf("hello\n");
385    write(fileno(stdout),"hellofd\n",8);
386  #endif
387
388  /* call their routine */
389  if (rtems_telnetd_config.login_check != NULL) {
390    start = rtems_shell_login_prompt(
391      stdin,
392      stderr,
393      arg->devname,
394      rtems_telnetd_config.login_check
395    );
396    login_failed = !start;
397  }
398  if (start) {
399    rtems_telnetd_config.command( arg->devname, arg->arg);
400  }
401
402  stdin  = ostd[0];
403  stdout = ostd[1];
404  stderr = ostd[2];
405
406  if (login_failed) {
407    syslog(
408      LOG_AUTHPRIV | LOG_WARNING,
409      "telnetd: to many wrong passwords entered from %s",
410      arg->peername
411    );
412  }
413
414cleanup:
415  release_a_Connection(arg->devname, arg->peername, nstd, i);
416  free(arg);
417}
418
419struct wrap_delete_args {
420  void (*t)(void *);
421  void           *a;
422};
423
424static rtems_task
425wrap_delete(rtems_task_argument arg)
426{
427  struct wrap_delete_args     *pwa = (struct wrap_delete_args *)arg;
428  register void              (*t)(void *) = pwa->t;
429  register void               *a   = pwa->a;
430
431  /* free argument before calling function (which may never return if
432   * they choose to delete themselves)
433   */
434  free(pwa);
435  t(a);
436  rtems_task_delete(RTEMS_SELF);
437}
438
439rtems_id
440telnetd_dflt_spawn(const char *name, unsigned int priority, unsigned int stackSize, void (*fn)(void *), void* fnarg)
441{
442  rtems_status_code        sc;
443  rtems_id                 task_id = RTEMS_ID_NONE;
444  char                     nm[4] = {'X','X','X','X' };
445  struct wrap_delete_args *pwa = malloc(sizeof(*pwa));
446
447  strncpy(nm, name, 4);
448
449  if ( !pwa ) {
450    perror("Telnetd: no memory\n");
451    return RTEMS_ID_NONE;
452  }
453
454  pwa->t = fn;
455  pwa->a = fnarg;
456
457  if ((sc=rtems_task_create(
458    rtems_build_name(nm[0], nm[1], nm[2], nm[3]),
459      (rtems_task_priority)priority,
460      stackSize,
461      RTEMS_DEFAULT_MODES,
462      RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
463      &task_id)) ||
464    (sc=rtems_task_start(
465      task_id,
466      wrap_delete,
467      (rtems_task_argument)pwa))) {
468        free(pwa);
469        rtems_error(sc,"Telnetd: spawning task failed");
470        return RTEMS_ID_NONE;
471  }
472  return task_id;
473}
474
475/* convenience routines for CEXP (retrieve stdio descriptors
476 * from reent structure)
477 */
478#ifdef stdin
479static __inline__ FILE *
480_stdin(void)  { return stdin; }
481#undef stdin
482FILE *stdin(void)  { return _stdin(); }
483#endif
484#ifdef stdout
485static __inline__ FILE *
486_stdout(void) { return stdout; }
487#undef stdout
488FILE *stdout(void) { return _stdout(); }
489#endif
490#ifdef stderr
491static __inline__ FILE *
492_stderr(void) { return stderr; }
493#undef stderr
494FILE *stderr(void) { return _stderr(); }
495#endif
496
497/* MUST NOT USE stdin & friends below here !!!!!!!!!!!!! */
Note: See TracBrowser for help on using the repository browser.