summaryrefslogtreecommitdiff
path: root/cpukit/telnetd/telnetd.c
blob: aadf33deb7aff70df1e748bd30c7418bcea7354a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/***********************************************************/
/*
 *
 *  The telnet DAEMON
 *
 *  Author: 17,may 2001
 *
 *   WORK: fernando.ruiz@ctv.es
 *   HOME: correo@fernando-ruiz.com
 *
 * After start the net you can start this daemon.
 * It uses the previously inited pseudo-terminales (pty.c)
 * getting a new terminal with getpty(). This function
 * gives a terminal name passing a opened socket like parameter.
 *
 * With register_telnetd() you add a new command in the shell to start
 * this daemon interactively. (Login in /dev/console of course)
 *
 * Sorry but OOB is not still implemented. (This is the first version)
 *
 * Till Straumann <strauman@slac.stanford.edu>
 *  - made the 'shell' interface more generic, i.e. it is now
 *    possible to have 'telnetd' run an arbitrary 'shell'
 *    program.
 *
 * Copyright (c) 2009 embedded brains GmbH and others.
 *
 * embedded brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#include <rtems.h>
#include <rtems/error.h>
#include <rtems/pty.h>
#include <rtems/shell.h>
#include <rtems/telnetd.h>
#include <rtems/userenv.h>

#ifdef RTEMS_NETWORKING
#include <rtems/rtems_bsdnet.h>
#endif

#define PARANOIA

typedef struct telnetd_context telnetd_context;

typedef struct telnetd_session {
  rtems_pty_context  pty;
  char               peername[16];
  telnetd_context   *ctx;
} telnetd_session;

struct telnetd_context {
  rtems_telnetd_config_table config;
  int                        server_socket;
  uint16_t                   active_clients;
};

typedef union uni_sa {
  struct sockaddr_in sin;
  struct sockaddr     sa;
} uni_sa;

static telnetd_session *grab_a_Connection(telnetd_context *ctx)
{
  telnetd_session *session;
  uni_sa peer;
  socklen_t address_len;
  int acp_sock;

  if (ctx->active_clients >= ctx->config.client_maximum) {
    return NULL;
  }

  session = malloc(sizeof(*session));
  if (session == NULL) {
    perror("telnetd:malloc");
    return NULL;
  }

  address_len = sizeof(peer.sin);
  acp_sock = accept(ctx->server_socket, &peer.sa, &address_len);
  if (acp_sock<0) {
    perror("telnetd:accept");
    free(session);
    return NULL;
  };

  if (telnet_get_pty(&session->pty, acp_sock) == NULL) {
    syslog( LOG_DAEMON | LOG_ERR, "telnetd: unable to obtain PTY");
    /* NOTE: failing 'do_get_pty()' closed the socket */
    free(session);
    return NULL;
  }

  if (
    inet_ntop(
      AF_INET,
      &peer.sin.sin_addr,
      session->peername,
      sizeof(session->peername)
    ) == NULL
  ) {
    strlcpy(session->peername, "<UNKNOWN>", sizeof(session->peername));
  }

#ifdef PARANOIA
  syslog(LOG_DAEMON | LOG_INFO,
      "telnetd: accepted connection from %s on %s",
      session->peername,
      session->pty.name);
#endif

  ++ctx->active_clients;
  session->ctx = ctx;
  return session;
}


static void release_a_Connection(
  telnetd_context *ctx,
  telnetd_session *session,
  FILE **pstd,
  int n
)
{
#ifdef PARANOIA
  syslog(
    LOG_DAEMON | LOG_INFO,
    "telnetd: releasing connection from %s on %s",
    session->peername,
    session->pty.name
  );
#endif

  --ctx->active_clients;

  while (--n>=0)
    if (pstd[n]) fclose(pstd[n]);

  unlink(session->pty.name);
}

static rtems_id telnetd_spawn_task(
  rtems_name name,
  rtems_task_priority priority,
  size_t stack_size,
  rtems_task_entry entry,
  void *arg
)
{
  rtems_status_code sc;
  rtems_id task_id;

  sc = rtems_task_create(
    name,
    priority,
    stack_size,
    RTEMS_DEFAULT_MODES,
    RTEMS_FLOATING_POINT,
    &task_id
  );
  if (sc != RTEMS_SUCCESSFUL) {
    return RTEMS_ID_NONE;
  }

  (void)rtems_task_start(task_id, entry, (rtems_task_argument) arg);
  return task_id;
}

static void
telnetd_session_task(rtems_task_argument arg);

/***********************************************************/
static void
telnetd_server_task(rtems_task_argument arg)
{
  telnetd_session   *session = NULL;
  rtems_id           task_id;
  telnetd_context   *ctx = (telnetd_context *) arg;

  /* we don't redirect stdio as this probably
   * was started from the console anyway ..
   */
  do {
    session = grab_a_Connection(ctx);

    if (session == NULL) {
      /* if something went wrong, sleep for some time */
      sleep(10);
      continue;
    }

    task_id = telnetd_spawn_task(
      rtems_build_name('T', 'N', 'T', 'a'),
      ctx->config.priority,
      ctx->config.stack_size,
      telnetd_session_task,
      session
    );
    if (task_id == RTEMS_ID_NONE) {
      FILE *dummy;

      /* hmm - the pty driver slot can only be
       * released by opening and subsequently
       * closing the PTY - this also closes
       * the underlying socket. So we mock up
       * a stream...
       */

      if ( !(dummy=fopen(session->pty.name,"r+")) )
        perror("Unable to dummy open the pty, losing a slot :-(");
      release_a_Connection(ctx, session, &dummy, 1);
      free(session);
      sleep(2); /* don't accept connections too fast */
    }
  } while(1);
}

static void telnetd_destroy_context(telnetd_context *ctx)
{
  if (ctx->server_socket >= 0) {
    close(ctx->server_socket);
  }

  free(ctx);
}

static rtems_status_code telnetd_create_server_socket(telnetd_context *ctx)
{
  uni_sa srv;
  socklen_t address_len;
  int enable;

  ctx->server_socket = socket(PF_INET, SOCK_STREAM, 0);
  if (ctx->server_socket < 0) {
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: cannot create server socket");
    return RTEMS_UNSATISFIED;
  }

  enable = 1;
  (void)setsockopt(
    ctx->server_socket,
    SOL_SOCKET,
    SO_KEEPALIVE,
    &enable,
    sizeof(enable)
  );

  memset(&srv, 0, sizeof(srv));
  srv.sin.sin_family = AF_INET;
  srv.sin.sin_port = htons(23);
  address_len = sizeof(srv.sin);

  if (bind(ctx->server_socket, &srv.sa, address_len) != 0) {
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: cannot bind server socket");
    return RTEMS_RESOURCE_IN_USE;
  };

  if (listen(ctx->server_socket, ctx->config.client_maximum) != 0) {
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: cannot listen on server socket");
    return RTEMS_UNSATISFIED;
  };

  return RTEMS_SUCCESSFUL;
}

rtems_status_code rtems_telnetd_start(const rtems_telnetd_config_table* config)
{
  telnetd_context *ctx;
  rtems_id task_id;
  rtems_status_code sc;

  if (config->command == NULL) {
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: configuration with invalid command");
    return RTEMS_INVALID_ADDRESS;
  }

  ctx = calloc(1, sizeof(*ctx));
  if (ctx == NULL) {
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: cannot allocate server context");
    return RTEMS_UNSATISFIED;
  }

  ctx->config = *config;
  ctx->server_socket = -1;

  /* Check priority */
#ifdef RTEMS_NETWORKING
  if (ctx->config.priority == 0) {
    ctx->config.priority = rtems_bsdnet_config.network_task_priority;
  }
#endif
  if (ctx->config.priority == 0) {
    ctx->config.priority = 100;
  }

  /* Check stack size */
  if (ctx->config.stack_size == 0) {
    ctx->config.stack_size = (size_t)32 * 1024;
  }

  if (ctx->config.client_maximum == 0) {
    ctx->config.client_maximum = 5;
  }

  sc = telnetd_create_server_socket(ctx);
  if (sc != RTEMS_SUCCESSFUL) {
    telnetd_destroy_context(ctx);
    return sc;
  }

  task_id = telnetd_spawn_task(
    rtems_build_name('T', 'N', 'T', 'D'),
    ctx->config.priority,
    RTEMS_MINIMUM_STACK_SIZE,
    telnetd_server_task,
    ctx
  );
  if (task_id == RTEMS_ID_NONE) {
    ctx->config.command = NULL;
    syslog(LOG_DAEMON | LOG_ERR, "telnetd: cannot create server task");
    telnetd_destroy_context(ctx);
    return RTEMS_UNSATISFIED;
  }

  syslog(LOG_DAEMON | LOG_INFO, "telnetd: started successfully");
  return RTEMS_SUCCESSFUL;
}

/* utility wrapper */
static void
telnetd_session_task(rtems_task_argument arg)
{
  rtems_status_code    sc;
  FILE                *nstd[3]={0};
  FILE                *ostd[3]={ stdin, stdout, stderr };
  int                  i=0;
  telnetd_session    *session = (telnetd_session *) arg;
  telnetd_context    *ctx = session->ctx;
  bool login_failed = false;
  bool start = true;

  sc=rtems_libio_set_private_env();

  /* newlib hack/workaround. Before we change stdin/out/err we must make
         * sure the internal data are initialized (fileno(stdout) has this sideeffect).
   * This should probably be done from RTEMS' libc support layer...
   * (T.S., newlibc-1.13; 2005/10)
         */

  fileno(stdout);

  if (RTEMS_SUCCESSFUL != sc) {
    rtems_error(sc,"rtems_libio_set_private_env");
    goto cleanup;
  }

  /* redirect stdio */
  for (i=0; i<3; i++) {
    if ( !(nstd[i]=fopen(session->pty.name,"r+")) ) {
      perror("unable to open stdio");
      goto cleanup;
    }
  }

  stdin  = nstd[0];
  stdout = nstd[1];
  stderr = nstd[2];

  /* call their routine */
  if (ctx->config.login_check != NULL) {
    start = rtems_shell_login_prompt(
      stdin,
      stderr,
      session->pty.name,
      ctx->config.login_check
    );
    login_failed = !start;
  }
  if (start) {
    ctx->config.command( session->pty.name, ctx->config.arg);
  }

  stdin  = ostd[0];
  stdout = ostd[1];
  stderr = ostd[2];

  if (login_failed) {
    syslog(
      LOG_AUTHPRIV | LOG_WARNING,
      "telnetd: to many wrong passwords entered from %s",
      session->peername
    );
  }

cleanup:
  release_a_Connection(ctx, session, nstd, i);
  free(session);
}