Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/networking/telnetd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 983 by niro, Fri Apr 24 18:33:46 2009 UTC revision 984 by niro, Sun May 30 11:32:42 2010 UTC
# Line 11  Line 11 
11   *   *
12   * The telnetd manpage says it all:   * The telnetd manpage says it all:
13   *   *
14   *   Telnetd operates by allocating a pseudo-terminal device (see pty(4))  for   * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
15   *   a client, then creating a login process which has the slave side of the   * a client, then creating a login process which has the slave side of the
16   *   pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the   * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
17   *   master side of the pseudo-terminal, implementing the telnet protocol and   * master side of the pseudo-terminal, implementing the telnet protocol and
18   *   passing characters between the remote client and the login process.   * passing characters between the remote client and the login process.
19   *   *
20   * Vladimir Oleynik <dzo@simtreas.ru> 2001   * Vladimir Oleynik <dzo@simtreas.ru> 2001
21   *     Set process group corrections, initial busybox port   * Set process group corrections, initial busybox port
22   */   */
23    
24  #define DEBUG 0  #define DEBUG 0
# Line 32  Line 32 
32  #endif  #endif
33  #include <arpa/telnet.h>  #include <arpa/telnet.h>
34    
 /* Structure that describes a session */  
35  struct tsession {  struct tsession {
36   struct tsession *next;   struct tsession *next;
37   int sockfd_read, sockfd_write, ptyfd;   pid_t shell_pid;
38   int shell_pid;   int sockfd_read;
39     int sockfd_write;
40     int ptyfd;
41    
42   /* two circular buffers */   /* two circular buffers */
43   /*char *buf1, *buf2;*/   /*char *buf1, *buf2;*/
44  /*#define TS_BUF1 ts->buf1*/  /*#define TS_BUF1(ts) ts->buf1*/
45  /*#define TS_BUF2 TS_BUF2*/  /*#define TS_BUF2(ts) TS_BUF2(ts)*/
46  #define TS_BUF1 ((unsigned char*)(ts + 1))  #define TS_BUF1(ts) ((unsigned char*)(ts + 1))
47  #define TS_BUF2 (((unsigned char*)(ts + 1)) + BUFSIZE)  #define TS_BUF2(ts) (((unsigned char*)(ts + 1)) + BUFSIZE)
48   int rdidx1, wridx1, size1;   int rdidx1, wridx1, size1;
49   int rdidx2, wridx2, size2;   int rdidx2, wridx2, size2;
50  };  };
# Line 54  enum { BUFSIZE = (4 * 1024 - sizeof(stru Line 55  enum { BUFSIZE = (4 * 1024 - sizeof(stru
55    
56    
57  /* Globals */  /* Globals */
58  static int maxfd;  struct globals {
59  static struct tsession *sessions;   struct tsession *sessions;
60  static const char *loginpath = "/bin/login";   const char *loginpath;
61  static const char *issuefile = "/etc/issue.net";   const char *issuefile;
62     int maxfd;
63    };
64    #define G (*(struct globals*)&bb_common_bufsiz1)
65    #define INIT_G() do { \
66     G.loginpath = "/bin/login"; \
67     G.issuefile = "/etc/issue.net"; \
68    } while (0)
69    
70    
71  /*  /*
# Line 74  static const char *issuefile = "/etc/iss Line 82  static const char *issuefile = "/etc/iss
82     past (bf + len) then that IAC will be left unprocessed and *processed     past (bf + len) then that IAC will be left unprocessed and *processed
83     will be less than len.     will be less than len.
84    
    FIXME - if we mean to send 0xFF to the terminal then it will be escaped,  
    what is the escape character?  We aren't handling that situation here.  
   
85     CR-LF ->'s CR mapping is also done here, for convenience.     CR-LF ->'s CR mapping is also done here, for convenience.
86    
87     NB: may fail to remove iacs which wrap around buffer!     NB: may fail to remove iacs which wrap around buffer!
# Line 84  static const char *issuefile = "/etc/iss Line 89  static const char *issuefile = "/etc/iss
89  static unsigned char *  static unsigned char *
90  remove_iacs(struct tsession *ts, int *pnum_totty)  remove_iacs(struct tsession *ts, int *pnum_totty)
91  {  {
92   unsigned char *ptr0 = TS_BUF1 + ts->wridx1;   unsigned char *ptr0 = TS_BUF1(ts) + ts->wridx1;
93   unsigned char *ptr = ptr0;   unsigned char *ptr = ptr0;
94   unsigned char *totty = ptr;   unsigned char *totty = ptr;
95   unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);   unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
# Line 121  remove_iacs(struct tsession *ts, int *pn Line 126  remove_iacs(struct tsession *ts, int *pn
126   * TELOPT_NAWS support!   * TELOPT_NAWS support!
127   */   */
128   if ((ptr+2) >= end) {   if ((ptr+2) >= end) {
129   /* only the beginning of the IAC is in the   /* Only the beginning of the IAC is in the
130   buffer we were asked to process, we can't   buffer we were asked to process, we can't
131   process this char. */   process this char */
132   break;   break;
133   }   }
134   /*   /*
# Line 149  remove_iacs(struct tsession *ts, int *pn Line 154  remove_iacs(struct tsession *ts, int *pn
154    
155   num_totty = totty - ptr0;   num_totty = totty - ptr0;
156   *pnum_totty = num_totty;   *pnum_totty = num_totty;
157   /* the difference between ptr and totty is number of iacs   /* The difference between ptr and totty is number of iacs
158     we removed from the stream. Adjust buf1 accordingly. */     we removed from the stream. Adjust buf1 accordingly */
159   if ((ptr - totty) == 0) /* 99.999% of cases */   if ((ptr - totty) == 0) /* 99.999% of cases */
160   return ptr0;   return ptr0;
161   ts->wridx1 += ptr - totty;   ts->wridx1 += ptr - totty;
162   ts->size1 -= ptr - totty;   ts->size1 -= ptr - totty;
163   /* move chars meant for the terminal towards the end of the buffer */   /* Move chars meant for the terminal towards the end of the buffer */
164   return memmove(ptr - num_totty, ptr0, num_totty);   return memmove(ptr - num_totty, ptr0, num_totty);
165  }  }
166    
167    /*
168     * Converting single IAC into double on output
169     */
170    static size_t iac_safe_write(int fd, const char *buf, size_t count)
171    {
172     const char *IACptr;
173     size_t wr, rc, total;
174    
175     total = 0;
176     while (1) {
177     if (count == 0)
178     return total;
179     if (*buf == (char)IAC) {
180     static const char IACIAC[] ALIGN1 = { IAC, IAC };
181     rc = safe_write(fd, IACIAC, 2);
182     if (rc != 2)
183     break;
184     buf++;
185     total++;
186     count--;
187     continue;
188     }
189     /* count != 0, *buf != IAC */
190     IACptr = memchr(buf, IAC, count);
191     wr = count;
192     if (IACptr)
193     wr = IACptr - buf;
194     rc = safe_write(fd, buf, wr);
195     if (rc != wr)
196     break;
197     buf += rc;
198     total += rc;
199     count -= rc;
200     }
201     /* here: rc - result of last short write */
202     if ((ssize_t)rc < 0) { /* error? */
203     if (total == 0)
204     return rc;
205     rc = 0;
206     }
207     return total + rc;
208    }
209    
210    /* Must match getopt32 string */
211    enum {
212     OPT_WATCHCHILD = (1 << 2), /* -K */
213     OPT_INETD      = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
214     OPT_PORT       = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p PORT */
215     OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
216     OPT_SYSLOG     = (1 << 7) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -S */
217     OPT_WAIT       = (1 << 8) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -w SEC */
218    };
219    
220  static struct tsession *  static struct tsession *
221  make_new_session(  make_new_session(
222   USE_FEATURE_TELNETD_STANDALONE(int sock)   IF_FEATURE_TELNETD_STANDALONE(int sock)
223   SKIP_FEATURE_TELNETD_STANDALONE(void)   IF_NOT_FEATURE_TELNETD_STANDALONE(void)
224  ) {  ) {
225   const char *login_argv[2];   const char *login_argv[2];
226   struct termios termbuf;   struct termios termbuf;
# Line 174  make_new_session( Line 231  make_new_session(
231   /*ts->buf1 = (char *)(ts + 1);*/   /*ts->buf1 = (char *)(ts + 1);*/
232   /*ts->buf2 = ts->buf1 + BUFSIZE;*/   /*ts->buf2 = ts->buf1 + BUFSIZE;*/
233    
234   /* Got a new connection, set up a tty. */   /* Got a new connection, set up a tty */
235   fd = xgetpty(tty_name);   fd = xgetpty(tty_name);
236   if (fd > maxfd)   if (fd > G.maxfd)
237   maxfd = fd;   G.maxfd = fd;
238   ts->ptyfd = fd;   ts->ptyfd = fd;
239   ndelay_on(fd);   ndelay_on(fd);
240     close_on_exec_on(fd);
241    
242  #if ENABLE_FEATURE_TELNETD_STANDALONE  #if ENABLE_FEATURE_TELNETD_STANDALONE
  ts->sockfd_read = sock;  
243   /* SO_KEEPALIVE by popular demand */   /* SO_KEEPALIVE by popular demand */
244   setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));   setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
245     ts->sockfd_read = sock;
246   ndelay_on(sock);   ndelay_on(sock);
247   if (!sock) { /* We are called with fd 0 - we are in inetd mode */   if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */
248   sock++; /* so use fd 1 for output */   sock++; /* so use fd 1 for output */
249   ndelay_on(sock);   ndelay_on(sock);
250   }   }
251   ts->sockfd_write = sock;   ts->sockfd_write = sock;
252   if (sock > maxfd)   if (sock > G.maxfd)
253   maxfd = sock;   G.maxfd = sock;
254  #else  #else
255   /* SO_KEEPALIVE by popular demand */   /* SO_KEEPALIVE by popular demand */
256   setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));   setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
# Line 200  make_new_session( Line 259  make_new_session(
259   ndelay_on(0);   ndelay_on(0);
260   ndelay_on(1);   ndelay_on(1);
261  #endif  #endif
262    
263   /* Make the telnet client understand we will echo characters so it   /* Make the telnet client understand we will echo characters so it
264   * should not do it locally. We don't tell the client to run linemode,   * should not do it locally. We don't tell the client to run linemode,
265   * because we want to handle line editing and tab completion and other   * because we want to handle line editing and tab completion and other
# Line 208  make_new_session( Line 268  make_new_session(
268   static const char iacs_to_send[] ALIGN1 = {   static const char iacs_to_send[] ALIGN1 = {
269   IAC, DO, TELOPT_ECHO,   IAC, DO, TELOPT_ECHO,
270   IAC, DO, TELOPT_NAWS,   IAC, DO, TELOPT_NAWS,
271   IAC, DO, TELOPT_LFLOW,   /* This requires telnetd.ctrlSQ.patch (incomplete) */
272     /* IAC, DO, TELOPT_LFLOW, */
273   IAC, WILL, TELOPT_ECHO,   IAC, WILL, TELOPT_ECHO,
274   IAC, WILL, TELOPT_SGA   IAC, WILL, TELOPT_SGA
275   };   };
276   memcpy(TS_BUF2, iacs_to_send, sizeof(iacs_to_send));   /* This confuses iac_safe_write(), it will try to duplicate
277   ts->rdidx2 = sizeof(iacs_to_send);   * each IAC... */
278   ts->size2 = sizeof(iacs_to_send);   //memcpy(TS_BUF2(ts), iacs_to_send, sizeof(iacs_to_send));
279     //ts->rdidx2 = sizeof(iacs_to_send);
280     //ts->size2 = sizeof(iacs_to_send);
281     /* So just stuff it into TCP stream! (no error check...) */
282    #if ENABLE_FEATURE_TELNETD_STANDALONE
283     safe_write(sock, iacs_to_send, sizeof(iacs_to_send));
284    #else
285     safe_write(1, iacs_to_send, sizeof(iacs_to_send));
286    #endif
287     /*ts->rdidx2 = 0; - xzalloc did it */
288     /*ts->size2 = 0;*/
289   }   }
290    
291   fflush(NULL); /* flush all streams */   fflush_all();
292   pid = vfork(); /* NOMMU-friendly */   pid = vfork(); /* NOMMU-friendly */
293   if (pid < 0) {   if (pid < 0) {
294   free(ts);   free(ts);
# Line 235  make_new_session( Line 306  make_new_session(
306   /* Child */   /* Child */
307   /* Careful - we are after vfork! */   /* Careful - we are after vfork! */
308    
309   /* make new session and process group */   /* Restore default signal handling ASAP */
  setsid();  
   
  /* Restore default signal handling */  
310   bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);   bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
311    
312   /* open the child's side of the tty. */   /* Make new session and process group */
313     setsid();
314    
315     /* Open the child's side of the tty */
316   /* NB: setsid() disconnects from any previous ctty's. Therefore   /* NB: setsid() disconnects from any previous ctty's. Therefore
317   * we must open child's side of the tty AFTER setsid! */   * we must open child's side of the tty AFTER setsid! */
318   close(0);   close(0);
# Line 250  make_new_session( Line 321  make_new_session(
321   xdup2(0, 2);   xdup2(0, 2);
322   tcsetpgrp(0, getpid()); /* switch this tty's process group to us */   tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
323    
324   /* The pseudo-terminal allocated to the client is configured to operate in   /* The pseudo-terminal allocated to the client is configured to operate
325   * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */   * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
326   tcgetattr(0, &termbuf);   tcgetattr(0, &termbuf);
327   termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */   termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
328   termbuf.c_oflag |= ONLCR | XTABS;   termbuf.c_oflag |= ONLCR | XTABS;
# Line 260  make_new_session( Line 331  make_new_session(
331   /*termbuf.c_lflag &= ~ICANON;*/   /*termbuf.c_lflag &= ~ICANON;*/
332   tcsetattr_stdin_TCSANOW(&termbuf);   tcsetattr_stdin_TCSANOW(&termbuf);
333    
334   /* Uses FILE-based I/O to stdout, but does fflush(stdout),   /* Uses FILE-based I/O to stdout, but does fflush_all(),
335   * so should be safe with vfork.   * so should be safe with vfork.
336   * I fear, though, that some users will have ridiculously big   * I fear, though, that some users will have ridiculously big
337   * issue files, and they may block writing to fd 1,   * issue files, and they may block writing to fd 1,
338   * (parent is supposed to read it, but parent waits   * (parent is supposed to read it, but parent waits
339   * for vforked child to exec!) */   * for vforked child to exec!) */
340   print_login_issue(issuefile, tty_name);   print_login_issue(G.issuefile, tty_name);
341    
342   /* Exec shell / login / whatever */   /* Exec shell / login / whatever */
343   login_argv[0] = loginpath;   login_argv[0] = G.loginpath;
344   login_argv[1] = NULL;   login_argv[1] = NULL;
345   /* exec busybox applet (if PREFER_APPLETS=y), if that fails,   /* exec busybox applet (if PREFER_APPLETS=y), if that fails,
346   * exec external program */   * exec external program.
347   BB_EXECVP(loginpath, (char **)login_argv);   * NB: sock is either 0 or has CLOEXEC set on it.
348     * fd has CLOEXEC set on it too. These two fds will be closed here.
349     */
350     BB_EXECVP(G.loginpath, (char **)login_argv);
351   /* _exit is safer with vfork, and we shouldn't send message   /* _exit is safer with vfork, and we shouldn't send message
352   * to remote clients anyway */   * to remote clients anyway */
353   _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", loginpath);*/   _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", G.loginpath);*/
354  }  }
355    
 /* Must match getopt32 string */  
 enum {  
  OPT_WATCHCHILD = (1 << 2), /* -K */  
  OPT_INETD      = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */  
  OPT_PORT       = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */  
  OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */  
 };  
   
356  #if ENABLE_FEATURE_TELNETD_STANDALONE  #if ENABLE_FEATURE_TELNETD_STANDALONE
357    
358  static void  static void
359  free_session(struct tsession *ts)  free_session(struct tsession *ts)
360  {  {
361   struct tsession *t = sessions;   struct tsession *t = G.sessions;
362    
363   if (option_mask32 & OPT_INETD)   if (option_mask32 & OPT_INETD)
364   exit(EXIT_SUCCESS);   exit(EXIT_SUCCESS);
365    
366   /* Unlink this telnet session from the session list */   /* Unlink this telnet session from the session list */
367   if (t == ts)   if (t == ts)
368   sessions = ts->next;   G.sessions = ts->next;
369   else {   else {
370   while (t->next != ts)   while (t->next != ts)
371   t = t->next;   t = t->next;
# Line 311  free_session(struct tsession *ts) Line 377  free_session(struct tsession *ts)
377   * doesn't send SIGKILL. When we close ptyfd,   * doesn't send SIGKILL. When we close ptyfd,
378   * kernel sends SIGHUP to processes having slave side opened. */   * kernel sends SIGHUP to processes having slave side opened. */
379   kill(ts->shell_pid, SIGKILL);   kill(ts->shell_pid, SIGKILL);
380   wait4(ts->shell_pid, NULL, 0, NULL);   waitpid(ts->shell_pid, NULL, 0);
381  #endif  #endif
382   close(ts->ptyfd);   close(ts->ptyfd);
383   close(ts->sockfd_read);   close(ts->sockfd_read);
# Line 321  free_session(struct tsession *ts) Line 387  free_session(struct tsession *ts)
387   free(ts);   free(ts);
388    
389   /* Scan all sessions and find new maxfd */   /* Scan all sessions and find new maxfd */
390   maxfd = 0;   G.maxfd = 0;
391   ts = sessions;   ts = G.sessions;
392   while (ts) {   while (ts) {
393   if (maxfd < ts->ptyfd)   if (G.maxfd < ts->ptyfd)
394   maxfd = ts->ptyfd;   G.maxfd = ts->ptyfd;
395   if (maxfd < ts->sockfd_read)   if (G.maxfd < ts->sockfd_read)
396   maxfd = ts->sockfd_read;   G.maxfd = ts->sockfd_read;
397  #if 0  #if 0
398   /* Again, sockfd_write == sockfd_read here */   /* Again, sockfd_write == sockfd_read here */
399   if (maxfd < ts->sockfd_write)   if (G.maxfd < ts->sockfd_write)
400   maxfd = ts->sockfd_write;   G.maxfd = ts->sockfd_write;
401  #endif  #endif
402   ts = ts->next;   ts = ts->next;
403   }   }
# Line 354  static void handle_sigchld(int sig UNUSE Line 420  static void handle_sigchld(int sig UNUSE
420   pid = wait_any_nohang(NULL);   pid = wait_any_nohang(NULL);
421   if (pid <= 0)   if (pid <= 0)
422   break;   break;
423   ts = sessions;   ts = G.sessions;
424   while (ts) {   while (ts) {
425   if (ts->shell_pid == pid) {   if (ts->shell_pid == pid) {
426   ts->shell_pid = -1;   ts->shell_pid = -1;
# Line 374  int telnetd_main(int argc UNUSED_PARAM, Line 440  int telnetd_main(int argc UNUSED_PARAM,
440   struct tsession *ts;   struct tsession *ts;
441  #if ENABLE_FEATURE_TELNETD_STANDALONE  #if ENABLE_FEATURE_TELNETD_STANDALONE
442  #define IS_INETD (opt & OPT_INETD)  #define IS_INETD (opt & OPT_INETD)
443   int master_fd = master_fd; /* be happy, gcc */   int master_fd = master_fd; /* for compiler */
444   unsigned portnbr = 23;   int sec_linger = sec_linger;
445   char *opt_bindaddr = NULL;   char *opt_bindaddr = NULL;
446   char *opt_portnbr;   char *opt_portnbr;
447  #else  #else
448   enum {   enum {
449   IS_INETD = 1,   IS_INETD = 1,
450   master_fd = -1,   master_fd = -1,
  portnbr = 23,  
451   };   };
452  #endif  #endif
453     INIT_G();
454    
455     /* -w NUM, and implies -F. -w and -i don't mix */
456     IF_FEATURE_TELNETD_INETD_WAIT(opt_complementary = "wF:w+:i--w:w--i";)
457   /* Even if !STANDALONE, we accept (and ignore) -i, thus people   /* Even if !STANDALONE, we accept (and ignore) -i, thus people
458   * don't need to guess whether it's ok to pass -i to us */   * don't need to guess whether it's ok to pass -i to us */
459   opt = getopt32(argv, "f:l:Ki" USE_FEATURE_TELNETD_STANDALONE("p:b:F"),   opt = getopt32(argv, "f:l:Ki"
460   &issuefile, &loginpath   IF_FEATURE_TELNETD_STANDALONE("p:b:F")
461   USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));   IF_FEATURE_TELNETD_INETD_WAIT("Sw:"),
462     &G.issuefile, &G.loginpath
463     IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
464     IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)
465     );
466   if (!IS_INETD /*&& !re_execed*/) {   if (!IS_INETD /*&& !re_execed*/) {
467   /* inform that we start in standalone mode?   /* inform that we start in standalone mode?
468   * May be useful when people forget to give -i */   * May be useful when people forget to give -i */
# Line 401  int telnetd_main(int argc UNUSED_PARAM, Line 474  int telnetd_main(int argc UNUSED_PARAM,
474   }   }
475   }   }
476   /* Redirect log to syslog early, if needed */   /* Redirect log to syslog early, if needed */
477   if (IS_INETD || !(opt & OPT_FOREGROUND)) {   if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) {
478   openlog(applet_name, 0, LOG_USER);   openlog(applet_name, LOG_PID, LOG_DAEMON);
479   logmode = LOGMODE_SYSLOG;   logmode = LOGMODE_SYSLOG;
480   }   }
  USE_FEATURE_TELNETD_STANDALONE(  
  if (opt & OPT_PORT)  
  portnbr = xatou16(opt_portnbr);  
  );  
   
  /* Used to check access(loginpath, X_OK) here. Pointless.  
  * exec will do this for us for free later. */  
   
481  #if ENABLE_FEATURE_TELNETD_STANDALONE  #if ENABLE_FEATURE_TELNETD_STANDALONE
482   if (IS_INETD) {   if (IS_INETD) {
483   sessions = make_new_session(0);   G.sessions = make_new_session(0);
484   if (!sessions) /* pty opening or vfork problem, exit */   if (!G.sessions) /* pty opening or vfork problem, exit */
485   return 1; /* make_new_session prints error message */   return 1; /* make_new_session printed error message */
486   } else {   } else {
487   master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);   master_fd = 0;
488   xlisten(master_fd, 1);   if (!(opt & OPT_WAIT)) {
489     unsigned portnbr = 23;
490     if (opt & OPT_PORT)
491     portnbr = xatou16(opt_portnbr);
492     master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
493     xlisten(master_fd, 1);
494     }
495     close_on_exec_on(master_fd);
496   }   }
497  #else  #else
498   sessions = make_new_session();   G.sessions = make_new_session();
499   if (!sessions) /* pty opening or vfork problem, exit */   if (!G.sessions) /* pty opening or vfork problem, exit */
500   return 1; /* make_new_session prints error message */   return 1; /* make_new_session printed error message */
501  #endif  #endif
502    
503   /* We don't want to die if just one session is broken */   /* We don't want to die if just one session is broken */
# Line 437  int telnetd_main(int argc UNUSED_PARAM, Line 509  int telnetd_main(int argc UNUSED_PARAM,
509   signal(SIGCHLD, SIG_IGN);   signal(SIGCHLD, SIG_IGN);
510    
511  /*  /*
512     This is how the buffers are used. The arrows indicate the movement     This is how the buffers are used. The arrows indicate data flow.
513     of data.  
514     +-------+     wridx1++     +------+     rdidx1++     +----------+     +-------+     wridx1++     +------+     rdidx1++     +----------+
515     |       | <--------------  | buf1 | <--------------  |          |     |       | <--------------  | buf1 | <--------------  |          |
516     |       |     size1--      +------+     size1++      |          |     |       |     size1--      +------+     size1++      |          |
# Line 462  int telnetd_main(int argc UNUSED_PARAM, Line 534  int telnetd_main(int argc UNUSED_PARAM,
534   * ptys if there is room in their session buffers.   * ptys if there is room in their session buffers.
535   * NB: scalability problem: we recalculate entire bitmap   * NB: scalability problem: we recalculate entire bitmap
536   * before each select. Can be a problem with 500+ connections. */   * before each select. Can be a problem with 500+ connections. */
537   ts = sessions;   ts = G.sessions;
538   while (ts) {   while (ts) {
539   struct tsession *next = ts->next; /* in case we free ts. */   struct tsession *next = ts->next; /* in case we free ts */
540   if (ts->shell_pid == -1) {   if (ts->shell_pid == -1) {
541   /* Child died and we detected that */   /* Child died and we detected that */
542   free_session(ts);   free_session(ts);
# Line 485  int telnetd_main(int argc UNUSED_PARAM, Line 557  int telnetd_main(int argc UNUSED_PARAM,
557   /* This is needed because free_session() does not   /* This is needed because free_session() does not
558   * take master_fd into account when it finds new   * take master_fd into account when it finds new
559   * maxfd among remaining fd's */   * maxfd among remaining fd's */
560   if (master_fd > maxfd)   if (master_fd > G.maxfd)
561   maxfd = master_fd;   G.maxfd = master_fd;
562   }   }
563    
564   count = select(maxfd + 1, &rdfdset, &wrfdset, NULL, NULL);   {
565     struct timeval *tv_ptr = NULL;
566    #if ENABLE_FEATURE_TELNETD_INETD_WAIT
567     struct timeval tv;
568     if ((opt & OPT_WAIT) && !G.sessions) {
569     tv.tv_sec = sec_linger;
570     tv.tv_usec = 0;
571     tv_ptr = &tv;
572     }
573    #endif
574     count = select(G.maxfd + 1, &rdfdset, &wrfdset, NULL, tv_ptr);
575     }
576     if (count == 0) /* "telnetd -w SEC" timed out */
577     return 0;
578   if (count < 0)   if (count < 0)
579   goto again; /* EINTR or ENOMEM */   goto again; /* EINTR or ENOMEM */
580    
581  #if ENABLE_FEATURE_TELNETD_STANDALONE  #if ENABLE_FEATURE_TELNETD_STANDALONE
582   /* First check for and accept new sessions. */   /* Check for and accept new sessions */
583   if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {   if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
584   int fd;   int fd;
585   struct tsession *new_ts;   struct tsession *new_ts;
# Line 502  int telnetd_main(int argc UNUSED_PARAM, Line 587  int telnetd_main(int argc UNUSED_PARAM,
587   fd = accept(master_fd, NULL, NULL);   fd = accept(master_fd, NULL, NULL);
588   if (fd < 0)   if (fd < 0)
589   goto again;   goto again;
590   /* Create a new session and link it into our active list */   close_on_exec_on(fd);
591    
592     /* Create a new session and link it into active list */
593   new_ts = make_new_session(fd);   new_ts = make_new_session(fd);
594   if (new_ts) {   if (new_ts) {
595   new_ts->next = sessions;   new_ts->next = G.sessions;
596   sessions = new_ts;   G.sessions = new_ts;
597   } else {   } else {
598   close(fd);   close(fd);
599   }   }
600   }   }
601  #endif  #endif
602    
603   /* Then check for data tunneling. */   /* Then check for data tunneling */
604   ts = sessions;   ts = G.sessions;
605   while (ts) { /* For all sessions... */   while (ts) { /* For all sessions... */
606   struct tsession *next = ts->next; /* in case we free ts. */   struct tsession *next = ts->next; /* in case we free ts */
607    
608   if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {   if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
609   int num_totty;   int num_totty;
610   unsigned char *ptr;   unsigned char *ptr;
611   /* Write to pty from buffer 1. */   /* Write to pty from buffer 1 */
612   ptr = remove_iacs(ts, &num_totty);   ptr = remove_iacs(ts, &num_totty);
613   count = safe_write(ts->ptyfd, ptr, num_totty);   count = safe_write(ts->ptyfd, ptr, num_totty);
614   if (count < 0) {   if (count < 0) {
# Line 536  int telnetd_main(int argc UNUSED_PARAM, Line 623  int telnetd_main(int argc UNUSED_PARAM,
623   }   }
624   skip1:   skip1:
625   if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {   if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
626   /* Write to socket from buffer 2. */   /* Write to socket from buffer 2 */
627   count = MIN(BUFSIZE - ts->wridx2, ts->size2);   count = MIN(BUFSIZE - ts->wridx2, ts->size2);
628   count = safe_write(ts->sockfd_write, TS_BUF2 + ts->wridx2, count);   count = iac_safe_write(ts->sockfd_write, (void*)(TS_BUF2(ts) + ts->wridx2), count);
629   if (count < 0) {   if (count < 0) {
630   if (errno == EAGAIN)   if (errno == EAGAIN)
631   goto skip2;   goto skip2;
# Line 566  int telnetd_main(int argc UNUSED_PARAM, Line 653  int telnetd_main(int argc UNUSED_PARAM,
653   }   }
654    
655   if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {   if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
656   /* Read from socket to buffer 1. */   /* Read from socket to buffer 1 */
657   count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);   count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
658   count = safe_read(ts->sockfd_read, TS_BUF1 + ts->rdidx1, count);   count = safe_read(ts->sockfd_read, TS_BUF1(ts) + ts->rdidx1, count);
659   if (count <= 0) {   if (count <= 0) {
660   if (count < 0 && errno == EAGAIN)   if (count < 0 && errno == EAGAIN)
661   goto skip3;   goto skip3;
662   goto kill_session;   goto kill_session;
663   }   }
664   /* Ignore trailing NUL if it is there */   /* Ignore trailing NUL if it is there */
665   if (!TS_BUF1[ts->rdidx1 + count - 1]) {   if (!TS_BUF1(ts)[ts->rdidx1 + count - 1]) {
666   --count;   --count;
667   }   }
668   ts->size1 += count;   ts->size1 += count;
# Line 585  int telnetd_main(int argc UNUSED_PARAM, Line 672  int telnetd_main(int argc UNUSED_PARAM,
672   }   }
673   skip3:   skip3:
674   if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {   if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
675   /* Read from pty to buffer 2. */   /* Read from pty to buffer 2 */
676   count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);   count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
677   count = safe_read(ts->ptyfd, TS_BUF2 + ts->rdidx2, count);   count = safe_read(ts->ptyfd, TS_BUF2(ts) + ts->rdidx2, count);
678   if (count <= 0) {   if (count <= 0) {
679   if (count < 0 && errno == EAGAIN)   if (count < 0 && errno == EAGAIN)
680   goto skip4;   goto skip4;

Legend:
Removed from v.983  
changed lines
  Added in v.984