home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / pty / part01 / patch / telnetd.90.06.28.patch
Encoding:
Text File  |  1990-10-09  |  13.3 KB  |  577 lines

  1. *** pathnames.h.original    Tue Aug 21 15:45:25 1990
  2. --- pathnames.h    Tue Aug 21 22:24:30 1990
  3. ***************
  4. *** 35,37 ****
  5. --- 35,40 ----
  6.   #ifdef BFTPDAEMON
  7.   #define        BFTPPATH    "/usr/ucb/bftp"
  8.   #endif  /* BFTPDAEMON */
  9. + #ifdef USEPTY
  10. + #define _PATH_PTY "/usr/local/pty"
  11. + #endif
  12. *** sys_term.c.original    Tue Aug 21 15:45:31 1990
  13. --- sys_term.c    Tue Aug 21 22:00:56 1990
  14. ***************
  15. *** 23,28 ****
  16. --- 23,32 ----
  17.   
  18.   #include "telnetd.h"
  19.   #include "pathnames.h"
  20. + #ifdef USEPTY
  21. + #include "sock.h"
  22. + extern char *ttyname();
  23. + #endif
  24.   
  25.   #ifdef    NEWINIT
  26.   #include <initreq.h>
  27. ***************
  28. *** 354,359 ****
  29. --- 358,364 ----
  30.   
  31.   getpty()
  32.   {
  33. + #ifndef USEPTY
  34.       register int p;
  35.   #ifndef CRAY
  36.       register char c, *p1, *p2;
  37. ***************
  38. *** 398,403 ****
  39. --- 403,416 ----
  40.       }
  41.   #endif    /* CRAY */
  42.       return(-1);
  43. + #else USEPTY
  44. + /* In this new 'n' improved pty-based telnetd, we've already called */
  45. + /* startslave, which in turn waited to grab pseudo-terminals from the */
  46. + /* pty program. Global variable pty (grrrr) already has our tty. line */
  47. + /* has already been filled in by startslave. In fact, there's no point */
  48. + /* in calling getpty() at all. */
  49. +  return pty;
  50. + #endif USEPTY
  51.   }
  52.   
  53.   #ifdef    LINEMODE
  54. ***************
  55. *** 764,772 ****
  56. --- 777,788 ----
  57.    */
  58.   getptyslave()
  59.   {
  60. + #ifndef USEPTY
  61.       register int t = -1;
  62. + #endif
  63.   
  64.   #ifndef    CRAY
  65. + #ifndef USEPTY
  66.       /*
  67.        * Disassociate self from control terminal and open ttyp side.
  68.        * Set important flags on ttyp and ptyp.
  69. ***************
  70. *** 791,796 ****
  71. --- 807,815 ----
  72.           fatalperror(net, line);
  73.   #endif
  74.   
  75. + #else USEPTY
  76. +  /* In a pty-based world, this function is rendered similarly useless. */
  77. + #endif USEPTY
  78.       init_termbuf();
  79.   #ifndef    USE_TERMIO
  80.       termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
  81. ***************
  82. *** 812,821 ****
  83. --- 831,846 ----
  84.   #endif
  85.       set_termbuf();
  86.   #else    /* CRAY */
  87. + #ifndef USEPTY
  88.       (void) chown(line, 0, 0);
  89.       (void) chmod(line, 0600);
  90. + #endif
  91.   #endif    /* CRAY */
  92. + #ifndef USEPTY
  93.       return(t);
  94. + #else USEPTY
  95. +  /* What's there to return? We've thrown away fdsty. */
  96. + #endif
  97.   }
  98.   
  99.   #ifdef    NEWINIT
  100. ***************
  101. *** 829,842 ****
  102. --- 854,891 ----
  103.    * is necessary to startup the login process on the slave side of the pty.
  104.    */
  105.   
  106. + #ifdef USEPTY
  107. + int fdpass[2];
  108. + int siglerpid;
  109. + #endif USEPTY
  110.   /* ARGSUSED */
  111. + #ifndef USEPTY
  112.   startslave(t, host)
  113.   int t;
  114. + #else USEPTY
  115. + startslave(host)
  116. + #endif USEPTY
  117.   char *host;
  118.   {
  119.       register int i;
  120.       long time();
  121.   
  122. + #ifdef USEPTY
  123. +  char strfdpass[10];
  124. +  char ch;
  125. +  int fdmty;
  126. +  int fdsty;
  127. +  char *ttyn;
  128. +  int pgrp;
  129. +  int fd;
  130. + #endif USEPTY
  131. + #ifdef USEPTY
  132. +  /* Prepare for receiving pseudo-terminal descriptors from pty. */
  133. +  if (socketpair(AF_UNIX,SOCK_STREAM,0,fdpass) == -1) /* virtually impossible */
  134. +    fatalperror(net,"pty-socketpair");
  135. +  (void) sprintf(strfdpass,"%d",fdpass[1]);
  136. + #endif USEPTY
  137.   #ifndef    NEWINIT
  138.   # ifdef    CRAY
  139.       utmp_sig_init();
  140. ***************
  141. *** 845,850 ****
  142. --- 894,902 ----
  143.       if ((i = fork()) < 0)
  144.           fatalperror(net, "fork");
  145.       if (i) {
  146. + #ifdef USEPTY
  147. +    siglerpid = i;
  148. + #endif
  149.   # ifdef    CRAY
  150.           /*
  151.            * Cray parent will create utmp entry for child and send
  152. ***************
  153. *** 873,881 ****
  154. --- 925,972 ----
  155.           }
  156.           utmp_sig_notify(pid);
  157.   # endif    /* CRAY */
  158. + #ifndef USEPTY
  159.           (void) close(t);
  160. + #else USEPTY
  161. + /* Now we'll receive the descriptors. */
  162. +    (void) close(fdpass[1]);
  163. +    (void) sprintf(line,"/NONexistent");
  164. +    fdmty = fdsty = -1;
  165. +    if (pty_getch(fdpass[0],&ch) == -1)
  166. +      cleanup(); /* XXX: telnetd's finishup handling really sucks. */
  167. +    if (pty_putgetint(fdpass[0],'G',&siglerpid) == -1)
  168. +      cleanup();
  169. +    if (pty_getch(fdpass[0],&ch) == -1)
  170. +      cleanup();
  171. +    if (pty_putgetfd(fdpass[0],'m',&fdmty) == -1)
  172. +      cleanup();
  173. +    if (pty_getch(fdpass[0],&ch) == -1)
  174. +      cleanup();
  175. +    if (pty_putgetfd(fdpass[0],'s',&fdsty) == -1)
  176. +      cleanup();
  177. +    if (!(ttyn = ttyname(fdsty)))
  178. +      cleanup();
  179. +    (void) strncpy(line,ttyn,14);
  180. +    if ((fd = open("/dev/tty",O_RDWR)) != -1)
  181. +     {
  182. +      (void) ioctl(fd,TIOCNOTTY,0); /* detach */
  183. +      (void) close(fd);
  184. +     }
  185. +    (void) close(open(ttyn,O_RDWR)); /* attach ourselves to tty */
  186. +    pty = fdmty;
  187. +    (void) ioctl(fdsty,TIOCGPGRP,&pgrp);
  188. +    (void) setpgrp(0,pgrp);
  189. +    (void) close(fdsty); /* we won't need it for anything else */
  190. +    (void) fcntl(fdpass[0],F_SETFL,FNDELAY); /* XXX */
  191. + #endif USEPTY
  192.       } else {
  193. + #ifndef USEPTY
  194.           start_login(t, host);
  195. + #else USEPTY
  196. +    (void) close(fdpass[0]);
  197. +         start_login(host,strfdpass);
  198. + #endif USEPTY
  199.           /*NOTREACHED*/
  200.       }
  201.   #else    /* NEWINIT */
  202. ***************
  203. *** 986,994 ****
  204. --- 1077,1095 ----
  205.    * function will turn us into the login process.
  206.    */
  207.   
  208. + #ifndef USEPTY
  209.   start_login(t, host)
  210.   int t;
  211. + #else USEPTY
  212. + /* Added parameter, for dealing with pty: strfdpass, a printable version */
  213. + /* of the file descriptor for passing more descriptors up to us. */
  214. + start_login(host,strfdpass)
  215. + #endif USEPTY
  216.   char *host;
  217. + #ifdef USEPTY
  218. + char *strfdpass;
  219. + #endif USEPTY
  220.   {
  221.       register char *cp;
  222.       register char **argv;
  223. ***************
  224. *** 996,1001 ****
  225. --- 1097,1103 ----
  226.   #ifdef    CRAY
  227.       register char **cpp, **cpp2;
  228.       utmp_sig_wait();
  229. + #ifndef USEPTY
  230.   # ifndef TCVHUP
  231.       setpgrp();
  232.   # endif
  233. ***************
  234. *** 1028,1038 ****
  235. --- 1130,1142 ----
  236.       termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
  237.       termbuf.c_cflag = EXTB|HUPCL|CS8;
  238.       set_termbuf();
  239. + #endif USEPTY
  240.   #endif    /* CRAY */
  241.   
  242.       /*
  243.        * set up standard paths before forking to login
  244.        */
  245. + #ifndef USEPTY
  246.   #ifndef    NO_SETSID
  247.       if (setsid() < 0)
  248.           fatalperror(net, "setsid");
  249. ***************
  250. *** 1042,1053 ****
  251. --- 1146,1167 ----
  252.       if (ioctl(t, TIOCSCTTY, (char *)0) < 0)
  253.           fatalperror(net, "ioctl(sctty)");
  254.   #endif
  255. + #else USEPTY
  256. +  /* The POSIXish setsid() handling here is pty's responsibility. */
  257. + #endif USEPTY
  258.       (void) close(net);
  259.       (void) close(pty);
  260. + #ifndef USEPTY
  261.       (void) dup2(t, 0);
  262.       (void) dup2(t, 1);
  263.       (void) dup2(t, 2);
  264.       (void) close(t);
  265. + #else USEPTY
  266. +         (void) close(0); /* ahhh, life is easy */
  267. +         (void) close(1);
  268. +         (void) close(2);
  269. +     /* but absolutely don't close fdpass. */
  270. + #endif USEPTY
  271.       /*
  272.        * -h : pass on name of host.
  273.        *        WARNING:  -h is accepted by login if and only if
  274. ***************
  275. *** 1054,1060 ****
  276. --- 1168,1183 ----
  277.        *            getuid() == 0.
  278.        * -p : don't clobber the environment (so terminal type stays set).
  279.        */
  280. + #ifdef USEPTY
  281. +  /* Stick the pty magic in front. */
  282. +  argv = addarg(0,   "pty"); /* this addarg stuff is stupid */
  283. +  argv = addarg(argv,"-sdf");
  284. +  argv = addarg(argv,strfdpass);
  285. +  argv = addarg(argv,"-xRCUWX"); /* XXX: -xr? */
  286. +  argv = addarg(argv,_PATH_LOGIN);
  287. + #else
  288.       argv = addarg(0, "login");
  289. + #endif
  290.       argv = addarg(argv, "-h");
  291.       argv = addarg(argv, host);
  292.   #if    !defined(CRAY) && !defined(NO_LOGIN_P)
  293. ***************
  294. *** 1084,1090 ****
  295. --- 1207,1217 ----
  296.       }
  297.   #endif
  298.   
  299. + #ifdef USEPTY
  300. +  execv(_PATH_PTY,argv);
  301. + #else USEPTY
  302.       execv(_PATH_LOGIN, argv);
  303. + #endif USEPTY
  304.   
  305.       syslog(LOG_ERR, "%s: %m\n", _PATH_LOGIN);
  306.       fatalperror(net, _PATH_LOGIN);
  307. ***************
  308. *** 1142,1155 ****
  309. --- 1269,1294 ----
  310.       p = line + sizeof("/dev/") - 1;
  311.       if (logout(p))
  312.           logwtmp(p, "", "");
  313. + #ifndef USEPTY
  314.       (void)chmod(line, 0666);
  315. + #else USEPTY
  316. +     (void) chmod(line,0600); /* banish these security holes! */
  317. + #endif USEPTY
  318.       (void)chown(line, 0, 0);
  319.       *p = 'p';
  320. + #ifndef USEPTY
  321.       (void)chmod(line, 0666);
  322. + #else USEPTY
  323. +     (void) chmod(line,0600);
  324. + #endif USEPTY
  325.       (void)chown(line, 0, 0);
  326.   # else
  327.       rmut();
  328. + #ifndef USEPTY
  329.       vhangup();    /* XXX */
  330. + #else USEPTY
  331. + /*    vhangup();        XXX: This is also pty's responsibility. */
  332. + #endif USEPTY
  333.   # endif
  334.       (void) shutdown(net, 2);
  335.   #else    /* CRAY */
  336. ***************
  337. *** 1156,1162 ****
  338. --- 1295,1306 ----
  339.   # ifndef NEWINIT
  340.       rmut(line);
  341.       (void) shutdown(net, 2);
  342. + #ifndef USEPTY
  343.       kill(0, SIGHUP);
  344. + #else USEPTY
  345. + /* kill(0, SIGHUP);    The pty master makes this decision. */
  346. +  (void) kill(siglerpid,SIGHUP); /* but we do have to tell the sigler. */
  347. + #endif USEPTY
  348.   # else    /* NEWINIT */
  349.       (void) shutdown(net, 2);
  350.   # endif    /* NEWINT */
  351. ***************
  352. *** 1273,1282 ****
  353. --- 1417,1434 ----
  354.               (void) close(f);
  355.           }
  356.       }
  357. + #ifndef USEPTY
  358.       (void) chmod(line, 0666);
  359. + #else USEPTY
  360. +     (void) chmod(line, 0600);
  361. + #endif USEPTY
  362.       (void) chown(line, 0, 0);
  363.       line[strlen("/dev/")] = 'p';
  364. + #ifndef USEPTY
  365.       (void) chmod(line, 0666);
  366. + #else USEPTY
  367. +     (void) chmod(line, 0600);
  368. + #endif USEPTY
  369.       (void) chown(line, 0, 0);
  370.   }  /* end of rmut */
  371.   #endif    /* CRAY */
  372. *** telnetd.c.original    Tue Aug 21 15:45:36 1990
  373. --- telnetd.c    Tue Aug 21 21:27:48 1990
  374. ***************
  375. *** 28,33 ****
  376. --- 28,37 ----
  377.   #endif /* not lint */
  378.   
  379.   #include "telnetd.h"
  380. + #ifdef USEPTY
  381. + #include "sock.h"
  382. + extern char *ttyname();
  383. + #endif USEPTY
  384.   
  385.   /*
  386.    * I/O data buffers,
  387. ***************
  388. *** 462,467 ****
  389. --- 466,472 ----
  390.       int t;
  391.       struct hostent *hp;
  392.   
  393. + #ifndef USEPTY
  394.       /*
  395.        * Find an available pty to use.
  396.        */
  397. ***************
  398. *** 470,475 ****
  399. --- 475,481 ----
  400.           fatal(net, "All network ports in use");
  401.   
  402.       t = getptyslave();
  403. + #endif USEPTY
  404.   
  405.       /* get name of connected client */
  406.       hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr),
  407. ***************
  408. *** 489,500 ****
  409. --- 495,533 ----
  410.       /*
  411.        * Start up the login process on the slave side of the terminal
  412.        */
  413. + #ifndef USEPTY
  414.       startslave(t, host);
  415. + #else USEPTY
  416. +     startslave(host);
  417. + #endif USEPTY
  418.   
  419. + #ifdef USEPTY
  420. + /* In this pty-based version of the world, life is easy. startslave() */
  421. + /* actually dumps its process under the pty program, which handles all */
  422. + /* those nasty details of finding a pty and getting it started. pty */
  423. + /* then passes its pty master side up to telnetd (the ``controller'') */
  424. + /* so that we have full control. Joy to the world. */
  425. +     /*
  426. +      * Find an available pty to use.
  427. +      */
  428. +     pty = getpty(); /* noop */
  429. +     if (pty < 0) /* impossible */
  430. +         fatal(net, "All network ports in use");
  431. +     getptyslave();
  432. + #endif USEPTY
  433.       telnet(net, pty);  /* begin server processing */
  434.       /*NOTREACHED*/
  435.   }  /* end of doit */
  436.   
  437. + #ifdef USEPTY
  438. + extern int fdpass[2];
  439. + extern int siglerpid;
  440. + char ch;
  441. + #endif USEPTY
  442.   #ifndef    MAXHOSTNAMELEN
  443.   #define    MAXHOSTNAMELEN 64
  444.   #endif    MAXHOSTNAMELEN
  445. ***************
  446. *** 775,780 ****
  447. --- 808,819 ----
  448.           if (!SYNCHing) {
  449.               FD_SET(f, &xbits);
  450.           }
  451. + #ifdef USEPTY
  452. +      FD_SET(fdpass[0],&ibits);
  453. +      /* When we get input on fdpass, we're switching ttys. */
  454. +      /* This FD_SET seems logical, but it invites bugs under many */
  455. +      /* implementations. XXX. */
  456. + #endif USEPTY
  457.           if ((c = select(16, &ibits, &obits, &xbits,
  458.                           (struct timeval *)0)) < 1) {
  459.               if (c == -1) {
  460. ***************
  461. *** 785,791 ****
  462. --- 824,899 ----
  463.               sleep(5);
  464.               continue;
  465.           }
  466. + #ifdef USEPTY
  467. +         if (pty_getch(fdpass[0],&ch) != -1)
  468. +          {
  469. +           /* reconnect... */
  470. +           int fdmty;
  471. +           int fdsty;
  472. +           char *ttyn;
  473. +           int pgrp;
  474. +           int fd;
  475.   
  476. +           fdmty = fdsty = -1;
  477. +           (void) fcntl(fdpass[0],F_SETFL,0); /*XXX*/
  478. +           if (ch != 'G')
  479. +             if (pty_getch(fdpass[0],&ch) == -1)
  480. +               ;
  481. +           if (pty_putgetint(fdpass[0],'G',&siglerpid) == -1)
  482. +             cleanup();
  483. +           if (pty_getch(fdpass[0],&ch) == -1)
  484. +             cleanup();
  485. +           if (pty_putgetfd(fdpass[0],'m',&fdmty) == -1)
  486. +             cleanup();
  487. +           if (pty_getch(fdpass[0],&ch) == -1)
  488. +             cleanup();
  489. +           if (pty_putgetfd(fdpass[0],'s',&fdsty) == -1)
  490. +             cleanup();
  491. +           if (!(ttyn = ttyname(fdsty)))
  492. +             cleanup();
  493. +           /* It's impossible to do anything sensible with utmp; */
  494. +           /* this shows very clearly why utmp's remote field */
  495. +           /* is inconsistent. So we stick to the same ``line.'' */
  496. +           fd = 0;
  497. +               (void) ioctl(p, TIOCPKT, (char *)&fd);
  498. +           /* Packet mode, like NDELAY, is not a polite thing to */
  499. +           /* give someone else. One of those unstated conventions */
  500. +           /* that people should learn... */
  501. +           if ((fd = open("/dev/tty",O_RDWR)) != -1)
  502. +            {
  503. +             (void) ioctl(fd,TIOCNOTTY,0); /* detach */
  504. +             (void) close(fd);
  505. +            }
  506. +           (void) close(open(ttyn,O_RDWR)); /* attach */
  507. +                   (void) close(pty);
  508. +           pty = fdmty;
  509. +           p = pty; /* XXX: Blame DAB for this. Whither modularity? */
  510. +               (void) ioctl(p, TIOCPKT, (char *)&on);
  511. +           /* If we didn't do this, telnetd would become very */
  512. +           /* confused, because it's expecting packet-mode stuff. */
  513. +           (void) fcntl(fdpass[0],F_SETFL,FNDELAY); /* XXX */
  514. +           (void) ioctl(fdsty,TIOCGPGRP,&pgrp);
  515. +           (void) setpgrp(0,pgrp);
  516. +           /* XXX: If this messes up Linemode, TOUGH LUCK! */
  517. +               init_termbuf(); /* XXX */
  518. + #ifdef LINEMODE
  519. +               localstat(); /* XXX */
  520. + #endif
  521. +           FD_ZERO(&xbits);
  522. +           FD_ZERO(&ibits); /* XXX */
  523. +           FD_ZERO(&obits);
  524. +           /* XXX: anything else to do? */
  525. +          }
  526. + #endif USEPTY
  527.           /*
  528.            * Any urgent data?
  529.            */
  530. ***************
  531. *** 881,888 ****
  532. --- 989,1004 ----
  533.               if (pcc < 0 && errno == EWOULDBLOCK)
  534.                   pcc = 0;
  535.               else {
  536. + #ifndef USEPTY
  537.                   if (pcc <= 0)
  538.                       break;
  539. + #else USEPTY
  540. +                 if (pcc <= 0)
  541. +                  {
  542. +                   pcc = 0;
  543. +                   goto wtfiseofpty;
  544. +                  }
  545. + #endif USEPTY
  546.   #if    !defined(CRAY2) || !defined(UNICOS5)
  547.   #ifdef    LINEMODE
  548.                   /*
  549. ***************
  550. *** 932,937 ****
  551. --- 1048,1056 ----
  552.                       ptyip = ptyibuf;
  553.   #endif    /* defined(CRAY2) && defined(UNICOS5) */
  554.               }
  555. + #ifdef USEPTY
  556. +  wtfiseofpty: ;
  557. + #endif USEPTY
  558.           }
  559.   
  560.           while (pcc > 0) {
  561.