Changeset 8a775c27 in rtems for cpukit/telnetd/telnetd.c


Ignore:
Timestamp:
Mar 27, 2009, 1:45:31 PM (11 years ago)
Author:
Joel Sherrill <joel.sherrill@…>
Branches:
4.10, 4.11, master
Children:
2649eef
Parents:
8916bdc7
Message:

2009-03-27 Sebastian Huber <sebastian.huber@…>

  • Makefile.am, preinstall.am, libmisc/Makefile.am, libmisc/shell/shell.c, libmisc/shell/shell.h, telnetd/check_passwd.c, telnetd/telnetd.c, telnetd/telnetd.h: Generalized login check.
  • libmisc/shell/login.h, libmisc/shell/login_check.c, libmisc/shell/login_prompt.c: New files.
  • libmisc/stackchk/check.c: Changed format for blown stack message.
  • libcsupport/src/libio_sockets.c: Removed superfluous cast.
  • libnetworking/rtems/ftpfs.h: Documentation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/telnetd/telnetd.c

    r8916bdc7 r8a775c27  
    66 *  Author: 17,may 2001
    77 *
    8  *   WORK: fernando.ruiz@ctv.es 
     8 *   WORK: fernando.ruiz@ctv.es
    99 *   HOME: correo@fernando-ruiz.com
    1010 *
     
    1616 * With register_telnetd() you add a new command in the shell to start
    1717 * this daemon interactively. (Login in /dev/console of course)
    18  * 
     18 *
    1919 * Sorry but OOB is not still implemented. (This is the first version)
    2020 *
     
    2424 *    program.
    2525 *
    26  *  This program is distributed in the hope that it will be useful,
    27  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    28  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    29  *
    30  *  $Id$
     26 * Copyright (c) 2009
     27 * embedded brains GmbH
     28 * Obere Lagerstr. 30
     29 * D-82178 Puchheim
     30 * Germany
     31 * <rtems@embedded-brains.de>
     32 *
     33 * Modified by Sebastian Huber <sebastian.huber@embedded-brains.de>.
     34 *
     35 * The license and distribution terms for this file may be
     36 * found in the file LICENSE in this distribution or at
     37 * http://www.rtems.com/license/LICENSE.
     38 *
     39 * $Id$
    3140 */
    3241
     
    7483static int sockpeername(int sock, char *buf, int bufsz);
    7584
    76 static int initialize_telnetd(void);
    77 static int telnetd_askForPassword;
    78 
    79 void * telnetd_dflt_spawn(
     85void *telnetd_dflt_spawn(
    8086  const char *name,
    8187  unsigned priority,
     
    8692
    8793/***********************************************************/
    88 rtems_id            telnetd_task_id                 = 0;
    89 uint32_t            telnetd_stack_size              = 32000;
    90 rtems_task_priority telnetd_task_priority           = 0;
    91 bool                telnetd_remain_on_caller_stdio  = false;
    92 void                (*telnetd_shell)(char *, void*) = 0;
    93 void                *telnetd_shell_arg              = NULL;
    94 void *              (*telnetd_spawn_task)(
     94static rtems_id telnetd_task_id = RTEMS_ID_NONE;
     95
     96void *(*telnetd_spawn_task)(
    9597  const char *,
    9698  unsigned,
    9799  unsigned,
    98100  void (*)(void*),
    99   void *) = telnetd_dflt_spawn;
     101  void *
     102) = telnetd_dflt_spawn;
    100103
    101104static char *grab_a_Connection(
     
    179182}
    180183
    181 #if 1
    182 #define INSIDE_TELNETD
    183 #include "check_passwd.c"
    184 #else
    185 #define check_passwd(arg) 0
    186 #endif
    187 
    188 
    189184static void
    190185spawned_shell(void *arg);
     
    204199  if ((des_socket=socket(PF_INET,SOCK_STREAM,0))<0) {
    205200    perror("telnetd:socket");
    206     telnetd_task_id=0;
     201    telnetd_task_id = RTEMS_ID_NONE;
    207202    rtems_task_delete(RTEMS_SELF);
    208203  };
     
    216211    perror("telnetd:bind");
    217212    close(des_socket);
    218     telnetd_task_id=0;
     213    telnetd_task_id = RTEMS_ID_NONE;
    219214    rtems_task_delete(RTEMS_SELF);
    220215  };
     
    222217    perror("telnetd:listen");
    223218          close(des_socket);
    224     telnetd_task_id=0;
     219    telnetd_task_id = RTEMS_ID_NONE;
    225220    rtems_task_delete(RTEMS_SELF);
    226221  };
     
    230225   */
    231226  do {
    232     if ( telnetd_remain_on_caller_stdio ) {
    233       char device_name[32];
    234       ttyname_r( 1, device_name, sizeof(device_name) );
    235       if ( !telnetd_askForPassword || (0 == check_passwd(arg->peername)) )
    236         telnetd_shell(device_name, telnetd_shell_arg);
     227    if (rtems_telnetd_config.keep_stdio) {
     228      bool start = true;
     229      char device_name [32];
     230
     231      ttyname_r( 1, device_name, sizeof( device_name));
     232      if (rtems_telnetd_config.login_check != NULL) {
     233        start = rtems_shell_login_prompt(
     234          stdin,
     235          stderr,
     236          device_name,
     237          rtems_telnetd_config.login_check
     238        );
     239      }
     240      if (start) {
     241        rtems_telnetd_config.command( device_name, arg->arg);
     242      } else {
     243        syslog(
     244          LOG_AUTHPRIV | LOG_WARNING,
     245          "telnetd: to many wrong passwords entered from %s",
     246          device_name
     247        );
     248      }
    237249    } else {
    238250      devname = grab_a_Connection(des_socket, &srv, peername, sizeof(peername));
    239251
    240252      if ( !devname ) {
    241         /* if something went wrong, sleep for some time */
    242         sleep(10);
    243         continue;
     253        /* if something went wrong, sleep for some time */
     254        sleep(10);
     255        continue;
    244256      }
    245257
     
    247259
    248260      arg->devname = devname;
    249       arg->arg = telnetd_shell_arg;
     261      arg->arg = rtems_telnetd_config.arg;
    250262      strncpy(arg->peername, peername, sizeof(arg->peername));
    251263
    252       if ( !telnetd_spawn_task( &devname[5], telnetd_task_priority,
    253                telnetd_stack_size, spawned_shell, arg) ) {
     264      telnetd_task_id = (rtems_id) telnetd_spawn_task(
     265        devname,
     266        rtems_telnetd_config.priority,
     267        rtems_telnetd_config.stack_size,
     268        spawned_shell,
     269        arg
     270      );
     271      if (telnetd_task_id == RTEMS_ID_NONE) {
    254272        FILE *dummy;
    255273
     
    274292  } while(1);
    275293
    276   /* TODO: how to free the connection semaphore? But then - 
     294  /* TODO: how to free the connection semaphore? But then -
    277295   *       stopping the daemon is probably only needed during
    278296   *       development/debugging.
     
    281299   */
    282300  close(des_socket);
    283   telnetd_task_id=0;
    284 }
    285 
    286 /***********************************************************/
    287 static int initialize_telnetd(void) {
    288  
    289   if (telnetd_task_id         ) return RTEMS_RESOURCE_IN_USE;
    290   if (telnetd_stack_size<=0   ) telnetd_stack_size   =32000;
    291 
    292   if ( !telnetd_spawn_task("TNTD", telnetd_task_priority,
    293           RTEMS_MINIMUM_STACK_SIZE, rtems_task_telnetd, 0) ) {
    294     return -1;
    295   }
    296   return 0;
    297 }
    298 
    299 /***********************************************************/
    300 int rtems_telnetd_initialize(
    301   void               (*cmd)(char *, void *),
    302   void                *arg,
    303   bool                 remainOnCallerSTDIO,
    304   size_t               stack,
    305   rtems_task_priority  priority,
    306   bool                 askForPassword
    307 )
    308 {
    309   rtems_status_code sc;
    310 
    311 #if 0
    312   printf("This is rtems-telnetd (modified by Till Straumann)\n");
    313   printf("$Id$\n");
    314   printf("Release $Name$\n");
    315 #endif
    316 
    317   if ( !telnetd_shell && !cmd ) {
    318     fprintf(stderr,"startTelnetd(): setup error - NO SHELL; bailing out\n");
    319     return 1;
    320   }
    321 
    322   if (telnetd_task_id) {
    323     fprintf(stderr,"ERROR:telnetd already started\n");
    324     return 1;
    325   };
     301  telnetd_task_id = RTEMS_ID_NONE;
     302}
     303
     304rtems_status_code rtems_telnetd_initialize( void)
     305{
     306  rtems_status_code sc = RTEMS_SUCCESSFUL;
     307
     308  if (telnetd_task_id != RTEMS_ID_NONE) {
     309    fprintf(stderr, "telnetd already started\n");
     310    return RTEMS_RESOURCE_IN_USE;
     311  }
     312
     313  if (rtems_telnetd_config.command == NULL) {
     314    fprintf(stderr, "telnetd setup with invalid command\n");
     315    return RTEMS_IO_ERROR;
     316  }
    326317
    327318  if ( !telnet_pty_initialize() ) {
    328     fprintf(stderr,"PTY driver probably not properly registered\n");
    329     return 1;
    330   }
    331 
    332   telnetd_askForPassword = askForPassword;
    333 
    334   if (cmd)
    335     telnetd_shell = cmd;
    336   telnetd_shell_arg     = arg;
    337   telnetd_stack_size    = stack;
    338   if ( !priority ) {
    339     priority = rtems_bsdnet_config.network_task_priority;
    340   }
    341   if ( priority < 2 )
    342     priority = 100;
    343   telnetd_task_priority          = priority;
    344   telnetd_remain_on_caller_stdio = remainOnCallerSTDIO;
    345 
    346   sc = initialize_telnetd();
    347   if (sc != RTEMS_SUCCESSFUL) return sc;
    348 
    349   if ( !telnetd_remain_on_caller_stdio )
    350     fprintf(stderr, "rtems_telnetd() started with stacksize=%u,priority=%d\n",
    351       (unsigned)telnetd_stack_size,(int)telnetd_task_priority);
    352   return 0;
     319    fprintf(stderr, "telnetd cannot initialize PTY driver\n");
     320    return RTEMS_IO_ERROR;
     321  }
     322
     323  /* Check priority */
     324  if (rtems_telnetd_config.priority <= 0) {
     325    rtems_telnetd_config.priority = rtems_bsdnet_config.network_task_priority;
     326  }
     327  if (rtems_telnetd_config.priority < 2) {
     328    rtems_telnetd_config.priority = 100;
     329  }
     330
     331  /* Check stack size */
     332  if (rtems_telnetd_config.stack_size <= 0) {
     333    rtems_telnetd_config.stack_size = 32 * 1024;
     334  }
     335
     336  /* Spawn task */
     337  telnetd_task_id = (rtems_id) telnetd_spawn_task(
     338    "TNTD",
     339    rtems_telnetd_config.priority,
     340    RTEMS_MINIMUM_STACK_SIZE,
     341    rtems_task_telnetd,
     342    0
     343  );
     344  if (telnetd_task_id == RTEMS_ID_NONE) {
     345    return RTEMS_IO_ERROR;
     346  }
     347
     348  /* Print status */
     349  if (!rtems_telnetd_config.keep_stdio) {
     350    fprintf(
     351      stderr,
     352      "telnetd started with stacksize = %u and priority = %d\n",
     353      (unsigned) rtems_telnetd_config.stack_size,
     354      (unsigned) rtems_telnetd_config.priority
     355    );
     356  }
     357
     358  return RTEMS_SUCCESSFUL;
    353359}
    354360
     
    362368  int                  i=0;
    363369  struct shell_args  *arg = targ;
     370  bool login_failed = false;
     371  bool start = true;
    364372
    365373  sc=rtems_libio_set_private_env();
     
    398406
    399407  /* call their routine */
    400   if ( !telnetd_askForPassword || (0 == check_passwd(arg->peername)) )
    401     telnetd_shell(arg->devname, arg->arg);
     408  if (rtems_telnetd_config.login_check != NULL) {
     409    start = rtems_shell_login_prompt(
     410      stdin,
     411      stderr,
     412      arg->devname,
     413      rtems_telnetd_config.login_check
     414    );
     415    login_failed = !start;
     416  }
     417  if (start) {
     418    rtems_telnetd_config.command( arg->devname, arg->arg);
     419  }
    402420
    403421  stdin  = ostd[0];
    404422  stdout = ostd[1];
    405423  stderr = ostd[2];
     424
     425  if (login_failed) {
     426    syslog(
     427      LOG_AUTHPRIV | LOG_WARNING,
     428      "telnetd: to many wrong passwords entered from %s",
     429      arg->peername
     430    );
     431  }
    406432
    407433cleanup:
     
    434460{
    435461  rtems_status_code        sc;
    436   rtems_id                 task_id;
     462  rtems_id                 task_id = RTEMS_ID_NONE;
    437463  char                     nm[4] = {'X','X','X','X' };
    438464  struct wrap_delete_args *pwa = malloc(sizeof(*pwa));
     
    442468  if ( !pwa ) {
    443469    perror("Telnetd: no memory\n");
    444     return 0;
     470    return (void *) RTEMS_ID_NONE;
    445471  }
    446472
     
    461487        free(pwa);
    462488        rtems_error(sc,"Telnetd: spawning task failed");
    463         return 0;
    464   }
    465   return (void*)task_id;
     489        return (void *) RTEMS_ID_NONE;
     490  }
     491  return (void *) task_id;
    466492}
    467493
Note: See TracChangeset for help on using the changeset viewer.