home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume18 / superserver < prev    next >
Text File  |  1989-04-19  |  32KB  |  1,458 lines

  1. Subject:  v18i106:  "Super" network server for easy service-building
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: koreth@ssyx.UCSC.EDU (Steven Grimm)
  7. Posting-number: Volume 18, Issue 106
  8. Archive-name: superserver
  9.  
  10. This program allows individual users to set up network services without
  11. having to worry about the intricacies of socket I/O.  It is similar in
  12. function to the "rsh" program, but restricts the commands which can be
  13. executed by remote users.  No .rhosts or password is required, since
  14. the remote user can only execute commands from a specified (presumably
  15. safe) list.
  16.  
  17. The service programs think they're talking to a pipe (because they are).
  18. Stdin and stdout are redirected to the pipe; stderr is mapped to stdout.
  19. Shell scripts can be offered as network services, but make sure you have
  20. execute permission on them and that the line "#!/bin/sh" (or csh, or
  21. whatever shell you're using) appears at the top of the file, or UNIX won't
  22. recognize it as an executable-format file.
  23.  
  24. #!/bin/sh
  25. # shar:    Shell Archiver  (v1.22)
  26. #
  27. #    Run the following text with /bin/sh to create:
  28. #      MANIFEST
  29. #      Makefile
  30. #      NOTES
  31. #      client.1
  32. #      client.c
  33. #      common.h
  34. #      server.1
  35. #      server.c
  36. #      socket.c
  37. #      supersrv.8
  38. #      supersrv.c
  39. #
  40. sed 's/^X//' << 'SHAR_EOF' > MANIFEST &&
  41. XThis shar file should contain:
  42. X
  43. XMANIFEST    This file
  44. XMakefile    To build everything
  45. XNOTES        Various implementation notes, wish list, etc.
  46. Xclient.1    Manual page for the client program
  47. Xclient.c    The client program
  48. Xcommon.h    Definitions common to all three programs
  49. Xserver.1    Manual page for the server program
  50. Xserver.c    The server program
  51. Xsocket.c    Various blackbox IPC routines used by everything
  52. Xsupersrv.8    Manual page for the supersrv program
  53. Xsupersrv.c    The supersrv program
  54. SHAR_EOF
  55. chmod 0600 MANIFEST || echo "restore of MANIFEST fails"
  56. sed 's/^X//' << 'SHAR_EOF' > Makefile &&
  57. X#
  58. X# SuperServer
  59. X#
  60. X# by Steven Grimm, koreth@ssyx.ucsc.edu
  61. X#
  62. X
  63. XCFLAGS = -O
  64. X
  65. XSUPERO = supersrv.o socket.o
  66. XSERVERO = server.o socket.o
  67. XCLIO = client.o socket.o
  68. X
  69. Xall: supersrv server client
  70. X
  71. Xsupersrv: $(SUPERO)
  72. X    $(CC) $(CFLAGS) $(SUPERO) -o $@
  73. X
  74. Xserver: $(SERVERO)
  75. X    $(CC) $(CFLAGS) $(SERVERO) -o $@
  76. X
  77. Xclient: $(CLIO)
  78. X    $(CC) $(CFLAGS) $(CLIO) -o $@
  79. X
  80. Xclean:
  81. X    /bin/rm -f $(SUPERO) $(SERVERO) $(CLIO) supersrv server client core
  82. X
  83. Xinstall: all
  84. X    install -s -c -m 755 supersrv /usr/local
  85. X    install -s -c -m 755 server /usr/local
  86. X    install -s -c -m 755 client /usr/local
  87. SHAR_EOF
  88. chmod 0600 Makefile || echo "restore of Makefile fails"
  89. sed 's/^X//' << 'SHAR_EOF' > NOTES &&
  90. XThis program allows individual users to set up network services without
  91. Xhaving to worry about the intricacies of socket I/O.  It is similar in
  92. Xfunction to the "rsh" program, but restricts the commands which can be
  93. Xexecuted by remote users.  No .rhosts or password is required, since
  94. Xthe remote user can only execute commands from a specified (presumably
  95. Xsafe) list.
  96. X
  97. XThe service programs think they're talking to a pipe (because they are).
  98. XStdin and stdout are redirected to the pipe; stderr is mapped to stdout.
  99. XShell scripts can be offered as network services, but make sure you have
  100. Xexecute permission on them and that the line "#!/bin/sh" (or csh, or
  101. Xwhatever shell you're using) appears at the top of the file, or UNIX won't
  102. Xrecognize it as an executable-format file.
  103. X
  104. XThe server consists of two layers.  The bottom layer (subserver) is
  105. Xexecuted by a user when he wants to advertise services.  It reads a
  106. Xconfiguration file from the user's home directory, which contains a
  107. Xlist of service names and full path specifications.  It then checks for
  108. Xthe existence of the top layer (the super-server), and runs the
  109. Xsuperserver program if it was not already active.  In any case, the
  110. Xserver connects to the superserver, registers its service-names, and
  111. Xawaits a request.
  112. X
  113. XThe superserver accepts connections from remote locations, usually
  114. Xinitiated with the "client" program.  It reads a line of input from the
  115. Xclient program, which specifies the name of the desired service.  If
  116. Xone of the subservers has advertised the requested service, the
  117. Xsuperserver forks off a child process.  The child writes the name of
  118. Xthe requested service to the appropriate subserver, then acts as a
  119. Xmailman, shuffling bytes between the remote user and the subserver
  120. Xuntil one of them disconnects.  Meanwhile, the parent superserver
  121. Xwaits for another connection, and the whole bloody mess starts over.
  122. X
  123. XThe client-supersrv handshaking looks like this (all lines are newline-
  124. Xterminated, so you could talk to supersrv with telnet, for instance):
  125. X
  126. Xclient                supersrv
  127. X------                --------
  128. X                welcome message
  129. Xservice name
  130. Xusername (or empty line)
  131. Xarg1
  132. Xarg2
  133. Xarg3
  134. X .
  135. X .
  136. X .
  137. Xargn
  138. Xblank line
  139. X
  140. X
  141. XIf a superserver process is killed, all its subservers try to restart
  142. Xit until one of them succeeds.  If a server process is killed, the
  143. Xsuperserver removes all its services from the listing.  Some braindamaged
  144. XBSD implementations can take up to five minutes to figure out that a
  145. Xprocess has died and that its socket addresses are no longer in use, so
  146. Xkilling a superserver might result in a short interruption of service.
  147. X
  148. XSending a HUP signal to a server causes it to reload its database.
  149. X
  150. X
  151. XWish list:
  152. X
  153. X    If exec-ing a program fails because of an invalid file format, we
  154. Xshould try to run the program from sh, since it's probably a script without
  155. Xthe #!/bin/sh at the top.
  156. X
  157. X    Some form of logging would be nice, so people can tell who's
  158. Xusing which services.
  159. X
  160. X    Signal-passing and standard error support a la rsh would be neat, too.
  161. X
  162. X    Support for interactive programs by automatically allocating a pty
  163. Xbefore running a program would be helpful.  Some form of environment passing
  164. Xwould also need to be implemented for programs that use termcap.
  165. X
  166. XIf you have questions, comments, bug reports, large amounts of excess cash,
  167. Xhorny women, etc., send a letter to koreth@ssyx.ucsc.edu.
  168. X
  169. SHAR_EOF
  170. chmod 0600 NOTES || echo "restore of NOTES fails"
  171. sed 's/^X//' << 'SHAR_EOF' > client.1 &&
  172. X.TH CLIENT 1 "14 July 1988"
  173. X.SH NAME
  174. Xclient \- use a network service
  175. X.SH SYNOPSIS
  176. X.B client
  177. X[
  178. X.B \-u
  179. X.I username
  180. X]
  181. X.I host
  182. X.I servicename
  183. X[
  184. X.I arguments
  185. X]
  186. X.SH DESCRIPTION
  187. X.I Client
  188. Xinterfaces with services offered through
  189. X.IR server (1).
  190. XSpecify the hostname and service; the service must be offered
  191. Xby someone on the appropriate host.  To get a list of services
  192. Xoffered on 
  193. X.I host,
  194. Xuse the
  195. X.B LIST
  196. Xservice, which is always present.  Any 
  197. X.I arguments
  198. Xafter the
  199. X.I servicename
  200. Xare passed to the service.  Be careful of passing
  201. Xfilenames, as remote machines generally can't read files on the
  202. Xlocal host.
  203. X.SH OPTIONS
  204. X.IP \-u
  205. XIf two services of the same name are offered by different users,
  206. Xuse the
  207. X.I \-u
  208. Xoption to select the desired user.  If this flag is not given,
  209. Xthe client will pick the first service whose name matches the
  210. Xrequested one.
  211. X.SH EXAMPLES
  212. X.ft C
  213. X% client ssyx.ucsc.edu fortune -l
  214. X.ft R
  215. X.PP
  216. XThis connects to machine "ssyx.ucsc.edu" and prints out a long
  217. Xfortune.
  218. X.sp 1
  219. X.ft C
  220. X% client -u geek doofus.foo.bar LIST
  221. X.ft R
  222. X.PP
  223. XThis lists all services offered by user "geek" on host
  224. X"doofus.foo.bar".
  225. X.SH "SEE ALSO"
  226. Xrsh(1), server(1), supersrv(8)
  227. X.SH DIAGNOSTICS
  228. XIf no services are offered on a host,
  229. X.I client
  230. Xsays "clientsock: connection refused".
  231. X.SH AUTHOR
  232. XSteven Grimm (koreth@ssyx.ucsc.edu, ...!ucbvax!ssyx!koreth)
  233. SHAR_EOF
  234. chmod 0600 client.1 || echo "restore of client.1 fails"
  235. sed 's/^X//' << 'SHAR_EOF' > client.c &&
  236. X#include "common.h"
  237. X
  238. Xmain(argc, argv)
  239. Xchar **argv;
  240. X{
  241. X    int thirty;
  242. X    char c, user[16];
  243. X    int fd, i;
  244. X
  245. X    if (argc < 3)
  246. X    {
  247. Xusage:
  248. X        fprintf(stderr, "usage: %s [-u user] host cmd [parms]\n", argv[0]);
  249. X        exit(-1);
  250. X    }
  251. X
  252. X    user[0] = 0;
  253. X
  254. X    if (argv[1][0] == '-')
  255. X        switch(argv[1][1])
  256. X        {
  257. X        case 'u':
  258. X            strncpy(user, argv[2], 15);
  259. X            user[15] = 0;
  260. X            argv += 2;
  261. X            argc -= 2;
  262. X            if (argc < 3)
  263. X                goto usage;
  264. X            break;
  265. X        default:
  266. X            fprintf(stderr, "-%c flag unknown\n", argv[1][1]);
  267. X            break;
  268. X        }
  269. X
  270. X    thirty = 30;
  271. X
  272. X    fd = clientsock(argv[1], SUPERPORT);
  273. X    if (fd < 0)
  274. X    {
  275. X        switch(fd) {
  276. X        case -9999:
  277. X            fprintf(stderr, "%s: host unknown\n", argv[1]);
  278. X            break;
  279. X        case -ECONNREFUSED:
  280. X            fprintf(stderr, "No services on %s\n", argv[1]);
  281. X            break;
  282. X        default:
  283. X            perror("clientsock");
  284. X            break;
  285. X        }
  286. X        exit(-1);
  287. X    }
  288. X
  289. X    setsockopt(fd, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  290. X
  291. X    do
  292. X        read(fd, &c, 1);
  293. X    while (c != '\n');
  294. X
  295. X    writeln(fd, argv[2]);
  296. X    writeln(fd, user);
  297. X
  298. X    if (argc > 3)
  299. X        for (i=3; i<argc; i++)
  300. X            writeln(fd, argv[i]);
  301. X
  302. X    writeln(fd, "");
  303. X
  304. X    shuffle(fd);
  305. X
  306. X    fcntl(0, F_SETFL, 0);
  307. X    close(fd);
  308. X}
  309. X
  310. Xwriteln(fd, string)
  311. Xint fd;
  312. Xchar *string;
  313. X{
  314. X    write(fd, string, strlen(string));
  315. X    write(fd, "\n", 1);
  316. X}
  317. X
  318. X/*
  319. X * Shuffle bytes between stdin/out and the socket.  This forks off
  320. X * once so that one process handles dataflow in each direction (that's
  321. X * how rsh does it, and it makes the code a lot prettier...)
  322. X */
  323. Xshuffle(subsrv)
  324. Xint subsrv;        /* this will probably always be 3... */
  325. X{
  326. X    fd_set reed, tread, other;
  327. X    int pid, numread, buf[BUFSIZ];
  328. X
  329. X    pid = fork();
  330. X    if (pid < 0)
  331. X    {
  332. X        perror("fork");
  333. X        close(subsrv);
  334. X        exit(-1);
  335. X    }
  336. X
  337. X    FD_ZERO(&reed);
  338. X    FD_ZERO(&other);
  339. X
  340. X    if (!pid)
  341. X    {
  342. X        close(1);
  343. X        close(2);
  344. X
  345. X        while (1)
  346. X        {
  347. X            numread = read(0, buf, sizeof(buf));
  348. X            if (numread <= 0)
  349. X                break;
  350. X            write(subsrv, buf, numread);
  351. X        }
  352. X        close(subsrv);
  353. X        exit(0);
  354. X    }
  355. X    else
  356. X    {
  357. X        close(0);
  358. X        close(2);
  359. X
  360. X        while (1)
  361. X        {
  362. X            numread = read(subsrv, buf, sizeof(buf));
  363. X            if (numread <= 0)
  364. X                break;
  365. X            write(1, buf, numread);
  366. X        }
  367. X
  368. X        kill(pid, SIGKILL);
  369. X    }
  370. X}
  371. X
  372. SHAR_EOF
  373. chmod 0600 client.c || echo "restore of client.c fails"
  374. sed 's/^X//' << 'SHAR_EOF' > common.h &&
  375. X/*
  376. X * Common.h
  377. X *
  378. X * Definitions common to both the sub- and superserver.
  379. X */
  380. X
  381. X/*
  382. X * Common include files
  383. X */
  384. X#include <stdio.h>
  385. X#include <signal.h>
  386. X#include <errno.h>
  387. X#include <fcntl.h>
  388. X#include <sys/param.h>
  389. X#include <sys/ioctl.h>
  390. X#include <sys/time.h>
  391. X#include <sys/socket.h>
  392. X
  393. X/*
  394. X * This is the port number that the superserver listens on.
  395. X */
  396. X#define SUPERPORT    3502
  397. X
  398. X/*
  399. X * Common external variables.
  400. X */
  401. Xextern int errno;
  402. X
  403. SHAR_EOF
  404. chmod 0600 common.h || echo "restore of common.h fails"
  405. sed 's/^X//' << 'SHAR_EOF' > server.1 &&
  406. X.TH SERVER 1 "14 July 1988"
  407. X.SH NAME
  408. Xserver \- offer network services
  409. X.SH SYNOPSIS
  410. X.B server
  411. X.SH DESCRIPTION
  412. X.I Server
  413. Xallows users to offer services to other users, possibly on remote
  414. Xhosts.  Any program (or shell script) that a user has execute permission
  415. Xon can be offered as a service.
  416. X.PP
  417. XIf a server is the first to start up on a machine, the super-server
  418. X.IR supersrv (8)
  419. Xis automatically started by
  420. X.I server,
  421. Xwhich then tells
  422. X.I supersrv
  423. Xwhich services the user wants to offer.
  424. X.I Server
  425. Xlooks for a file called
  426. X.I .services
  427. Xin the user's home directory, which consists of a list of service names
  428. Xand pathnames.
  429. X.SH EXAMPLE
  430. X.nf
  431. X.ft C
  432. X% cat .services
  433. Xfortune /usr/games/fortune
  434. Xw       /usr/ucb/w
  435. Xwho     /bin/who
  436. Xcrash   /g/f/v/foobar/kill-the-system
  437. X% server
  438. X%
  439. X.ft R
  440. X.SH DISCUSSION
  441. XMost programs should work without any problems as services.  The service
  442. Xthinks it's talking to a pipe; interactive programs work as long as they
  443. Xdon't attempt to set any special terminal modes or use
  444. X.IR termcap (3)
  445. Xor
  446. X.IR curses (3)
  447. Xto try to update the client's screen.  These restrictions are the same as
  448. Xthose of
  449. X.IR rsh (1);
  450. X.IR client (1)
  451. Xis similar to
  452. X.I rsh
  453. Xexcept that security is handled differently.
  454. X.PP
  455. XIf
  456. X.I supersrv
  457. Xgoes away (usually as a result of a kill command from a user who hasn't
  458. Xread this or doesn't know why there's an extra process running next to his
  459. Xserver), all the
  460. X.I server
  461. Xprocesses on the system will keep trying to restart
  462. X.I supersrv
  463. Xuntil one of them succeeds.  Since it can take a while for TCP ports to
  464. Xclear after their binding process has died on some implementations of
  465. XBSD, killing a
  466. X.I supersrv
  467. Xmay result in a short interruption of service.
  468. X.PP
  469. XSending a hangup signal (SIGHUP) to a
  470. X.I server
  471. Xprocess will cause it to reload its services database, so it isn't
  472. Xnecessary to rerun the program if the services list is changed.
  473. X.SH FILES
  474. X.TS
  475. X$HOME/.services    services list
  476. X.TE
  477. X.SH "SEE ALSO"
  478. Xclient(1), supersrv(8)
  479. X.SH BUGS
  480. X.I Server
  481. Xdoesn't sense when another server is being run by the same user; thus a
  482. Xuser could run several servers at the same time, most of which would never
  483. Xbe used.  Duplicate service names would appear in the service list.
  484. X.PP
  485. XShell scripts offered as services must have the line "#!/bin/sh" (or csh,
  486. Xor whatever shell is being used) at the top, or
  487. X.I server
  488. Xwon't be able to execute them.
  489. X.SH AUTHOR
  490. XSteven Grimm (koreth@ssyx.ucsc.edu, ...!ucbvax!ssyx!koreth)
  491. SHAR_EOF
  492. chmod 0600 server.1 || echo "restore of server.1 fails"
  493. sed 's/^X//' << 'SHAR_EOF' > server.c &&
  494. X#include "common.h"
  495. X#include <sys/file.h>
  496. X#include <pwd.h>
  497. X
  498. X/*
  499. X * SubServer main module.
  500. X */
  501. X
  502. X#define    RCFILE    "/.services"    /* File to grab services from */
  503. X
  504. Xchar *getenv();
  505. X
  506. X/*
  507. X * This linked list structure is used to keep track of the
  508. X * services we're offering.
  509. X */
  510. Xstruct service {
  511. X    char    name[20];        /* Service name */
  512. X    char    path[MAXPATHLEN];    /* Service path */
  513. X    struct service *next;        /* Next element... */
  514. X} *list;
  515. X
  516. X/*
  517. X * Read in a services file and set up the linked list of
  518. X * services.  Test each service to be sure we can offer it.
  519. X * Returns 0 if there are no services offered.
  520. X */
  521. Xgetservices()
  522. X{
  523. X    FILE    *fp;
  524. X    char    filename[MAXPATHLEN], *home;
  525. X
  526. X    home = getenv("HOME");
  527. X    if (! home)
  528. X    {
  529. X        fprintf(stderr, "no home directory\n");
  530. X        return(0);
  531. X    }
  532. X
  533. X    strcpy(filename, home);
  534. X    strcat(filename, RCFILE);
  535. X
  536. X    fp = fopen(filename, "r");
  537. X    if (! fp)
  538. X    {
  539. X        perror("couldn't open services file");
  540. X        return(0);
  541. X    }
  542. X
  543. X    list = NULL;
  544. X
  545. X    while (! feof(fp))
  546. X    {
  547. X        char    servname[20], format[20];
  548. X
  549. X        servname[0] = 0;
  550. X        sprintf(format, "%%20s\t%%%d[^\n]", MAXPATHLEN);
  551. X        fscanf(fp, format, servname, filename);
  552. X        getc(fp);
  553. X        
  554. X        if (servname[0] && filename[0])
  555. X        {
  556. X            struct service *temp;
  557. X
  558. X            if (access(filename, X_OK))
  559. X            {
  560. X                fprintf(stderr, "warning: can't execute %s\n",
  561. X                    filename);
  562. X                continue;
  563. X            }
  564. X
  565. X            temp = (struct service *) malloc(sizeof(struct service));
  566. X            strcpy(temp->name, servname);
  567. X            strcpy(temp->path, filename);
  568. X            temp->next = list;
  569. X            list = temp;
  570. X        }
  571. X    }
  572. X    fclose(fp);
  573. X    return 1;
  574. X}
  575. X
  576. X
  577. X/*
  578. X * Reap children.
  579. X */
  580. Xsigchld()
  581. X{
  582. X    return wait(0);
  583. X}
  584. X
  585. Xint    fd;
  586. X
  587. X/*
  588. X * Reload the database.
  589. X * We do this by killing the old list element by element then calling
  590. X * getservices(), and closing the socket file descriptor so the main
  591. X * loop will have to reregister our services.
  592. X */
  593. Xreload()
  594. X{
  595. X    struct service *cur, *next;
  596. X
  597. X    cur = list;
  598. X    while (cur)
  599. X    {
  600. X        next = cur->next;
  601. X        free(cur);
  602. X        cur = next;
  603. X    }
  604. X
  605. X    if (! getservices())
  606. X        exit(-1);
  607. X    close(fd);
  608. X    fd = -1;    /* prevent another close... */
  609. X}
  610. X
  611. Xmain(argc, argv, envp)
  612. Xchar **argv, **envp;
  613. X{
  614. X    int thirty;
  615. X    struct service *cur;
  616. X    struct passwd *pw;
  617. X    char    service[80], user[16];
  618. X
  619. X    if (! getservices())
  620. X        exit(0);
  621. X
  622. X    if (fork())
  623. X        exit(0);
  624. X
  625. X    close(0);
  626. X    close(1);
  627. X    close(2);
  628. X
  629. X    fd = open("/dev/tty", O_RDWR);
  630. X    if (fd >= 0)
  631. X    {
  632. X        ioctl(fd, TIOCNOTTY, 0);
  633. X        close(fd);
  634. X    }
  635. X    else
  636. X        printf("warning: couldn't disassociate from tty\n");
  637. X
  638. X/*
  639. X * Handle signals.  We want to reap children, so we should handle SIGCHLDs;
  640. X * we also want to let the user reload his services database, which we do
  641. X * with SIGHUP.
  642. X */
  643. X    signal(SIGCHLD, sigchld);
  644. X    signal(SIGHUP, reload);
  645. X
  646. X    thirty = 30;
  647. X
  648. X    pw = getpwuid(getuid());
  649. X    if (pw == NULL)
  650. X    {
  651. X        printf("warning: couldn't get UID\n");
  652. X        user[0] = '\n';
  653. X        user[1] = '\0';
  654. X    }
  655. X    else
  656. X    {
  657. X        strncpy(user, pw->pw_name, sizeof(user)-2);
  658. X        user[sizeof(user)-1] = '\0';
  659. X        strcat(user, "\n");
  660. X    }
  661. X    
  662. X    while (1)
  663. X    {
  664. X        char c;
  665. X
  666. X        fd = clientsock("localhost", SUPERPORT);
  667. X        if (fd < 0)
  668. X            if (errno == ECONNREFUSED)
  669. X            {
  670. X                start_super(argv[0], envp);
  671. X/*
  672. X * Give the superserver time to fire up.
  673. X */
  674. X                sleep(5);
  675. X                continue;
  676. X            }
  677. X            else
  678. X            {
  679. X                perror("superserver connect");
  680. X                exit(-1);
  681. X            }
  682. X
  683. X        thirty = 30;
  684. X        setsockopt(fd, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  685. X        c = 0;
  686. X        do
  687. X            if (read(fd, &c, 1) < 0 && errno != EINTR)
  688. X                break;
  689. X        while (c != '\n');
  690. X
  691. X        if (c != '\n')
  692. X            continue;
  693. X
  694. X        write(fd, "REGISTER\n", 9);
  695. X        write(fd, user, strlen(user));
  696. X
  697. X        for (cur = list; cur; cur = cur->next)
  698. X        {
  699. X            write(fd, cur->name, strlen(cur->name));
  700. X            write(fd, "\n", 1);
  701. X        }
  702. X        write(fd, "\n", 1);
  703. X
  704. X        service[0] = 0;
  705. X        if (read(fd, service, 20) < 0 && errno != EBADF && errno != EINTR)
  706. X        {
  707. X            perror("read");
  708. X            exit(-1);
  709. X        }
  710. X
  711. X        if (service[0])
  712. X            do_service(service, fd, envp);
  713. X        if (fd >= 0)
  714. X            close(fd);
  715. X    }
  716. X}
  717. X
  718. X
  719. X/*
  720. X * Provide the service.  Fork off and keep reading parameters until
  721. X * they are terminated by an empty line, then pass them to the program
  722. X * specified by the service.
  723. X */
  724. Xdo_service(service, fd, envp)
  725. Xchar *service;
  726. Xint fd;
  727. Xchar **envp;
  728. X{
  729. X    struct service *cur;
  730. X    char    *argv[256], input[256];
  731. X    int    curarg = 0, index = 0, thirty = 60;
  732. X
  733. X    if (fork())
  734. X        return;
  735. X
  736. X    argv[curarg++] = service;
  737. X
  738. X    while (1) {
  739. X        read(fd, &input[index], 1);
  740. X        if (input[index] == '\r')
  741. X            continue;
  742. X        if (input[index] == '\n')
  743. X        {
  744. X            if (! index)
  745. X                break;
  746. X
  747. X            argv[curarg] = (char *)malloc(index+1);
  748. X            bcopy(input, argv[curarg], index);
  749. X            argv[curarg][index] = 0;
  750. X
  751. X            index = 0;
  752. X            curarg++;
  753. X        }
  754. X        else
  755. X            index++;
  756. X    }
  757. X
  758. X    dup2(fd, 0);
  759. X    dup2(fd, 1);
  760. X    dup2(fd, 2);
  761. X    setsockopt(0, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  762. X    setsockopt(1, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  763. X    setsockopt(2, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  764. X    if (fd > 2)
  765. X        close(fd);
  766. X
  767. X    argv[curarg] = NULL;
  768. X
  769. X    for (cur = list; cur; cur = cur->next)
  770. X        if (! strcmp(cur->name, service))
  771. X            break;
  772. X
  773. X    if (! cur)        /* service not there */
  774. X        exit(0);
  775. X
  776. X    execve(cur->path, argv, envp);
  777. X    perror("execve");
  778. X    exit(0);
  779. X}
  780. X
  781. Xchar *superv[] = { "/bin/sh", "-c", "supersrv", NULL };
  782. X
  783. Xstart_super(argv0, envp)
  784. Xchar *argv0, **envp;
  785. X{
  786. X    char *lastslash, argcopy[MAXPATHLEN], *rindex();
  787. X
  788. X    strcpy(argcopy, argv0);
  789. X/*
  790. X * If a path was given, try to find the superserver in the
  791. X * same directory as the subserver...
  792. X */
  793. X    if (lastslash = rindex(argcopy, '/'))
  794. X    {
  795. X        char path[MAXPATHLEN];
  796. X
  797. X        *lastslash = 0;
  798. X        strcpy(path, argcopy);
  799. X        strcat(path, "/supersrv");
  800. X        if (! access(path, X_OK))
  801. X        {
  802. X            if (! fork())
  803. X                execve(path, superv+2, envp);
  804. X            return;
  805. X        }
  806. X    }
  807. X
  808. X/*
  809. X * Otherwise, start up a shell to scan along the user's
  810. X * $PATH.
  811. X */
  812. X    if (! fork())
  813. X        execve(superv[0], superv, envp);
  814. X}
  815. X
  816. SHAR_EOF
  817. chmod 0600 server.c || echo "restore of server.c fails"
  818. sed 's/^X//' << 'SHAR_EOF' > socket.c &&
  819. X/*
  820. X** SOCKET.C
  821. X**
  822. X** Written by Steven Grimm (koreth@ssyx.ucsc.edu) on 11-26-87 (Thanksgiving)
  823. X** Please distribute widely, but leave my name here.
  824. X**
  825. X** Various black-box routines for socket manipulation, so I don't have to
  826. X** remember all the structure elements.
  827. X** Of course, I still have to remember how to call these routines.
  828. X*/
  829. X
  830. X#include <sys/types.h>
  831. X#include <sys/time.h>
  832. X#include <sys/socket.h>
  833. X#include <netinet/in.h>
  834. X#include <netdb.h>
  835. X#include <stdio.h>
  836. X
  837. X#ifndef FD_SET        /* for 4.2BSD */
  838. X#define FD_SETSIZE      (sizeof(fd_set) * 8)
  839. X#define FD_SET(n, p)    (((fd_set *) (p))->fds_bits[0] |= (1 << ((n) % 32)))
  840. X#define FD_CLR(n, p)    (((fd_set *) (p))->fds_bits[0] &= ~(1 << ((n) % 32)))
  841. X#define FD_ISSET(n, p)  (((fd_set *) (p))->fds_bits[0] & (1 << ((n) % 32)))
  842. X#define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
  843. X#endif
  844. X
  845. Xextern int errno;
  846. X
  847. X/*
  848. X** serversock()
  849. X**
  850. X** Creates an internet socket, binds it to an address, and prepares it for
  851. X** subsequent accept() calls by calling listen().
  852. X**
  853. X** Input: port number desired, or 0 for a random one
  854. X** Output: file descriptor of socket, or a negative error
  855. X*/
  856. Xint serversock(port)
  857. Xint port;
  858. X{
  859. X    int    sock, x;
  860. X    struct    sockaddr_in server;
  861. X
  862. X    sock = socket(AF_INET, SOCK_STREAM, 0);
  863. X    if (sock < 0)
  864. X        return -errno;
  865. X
  866. X    bzero(&server, sizeof(server));
  867. X    server.sin_family = AF_INET;
  868. X    server.sin_addr.s_addr = INADDR_ANY;
  869. X    server.sin_port = htons(port);
  870. X
  871. X    x = bind(sock, &server, sizeof(server));
  872. X    if (x < 0)
  873. X    {
  874. X        close(sock);
  875. X        return -errno;
  876. X    }
  877. X
  878. X    listen(sock, 5);
  879. X
  880. X    return sock;
  881. X}
  882. X
  883. X/*
  884. X** portnum()
  885. X**
  886. X** Returns the internet port number for a socket.
  887. X**
  888. X** Input: file descriptor of socket
  889. X** Output: inet port number
  890. X*/
  891. Xint portnum(fd)
  892. Xint fd;
  893. X{
  894. X    int    length, err;
  895. X    struct    sockaddr_in address;
  896. X
  897. X    length = sizeof(address);
  898. X    err = getsockname(fd, &address, &length);
  899. X    if (err < 0)
  900. X        return -errno;
  901. X
  902. X    return ntohs(address.sin_port);
  903. X}
  904. X
  905. X/*
  906. X** clientsock()
  907. X**
  908. X** Returns a connected client socket.
  909. X**
  910. X** Input: host name and port number to connect to
  911. X** Output: file descriptor of CONNECTED socket, or a negative error (-9999
  912. X**         if the hostname was bad).
  913. X*/
  914. Xint clientsock(host, port)
  915. Xchar *host;
  916. Xint port;
  917. X{
  918. X    int    sock;
  919. X    struct    sockaddr_in server;
  920. X    struct    hostent *hp, *gethostbyname();
  921. X
  922. X    hp = gethostbyname(host);
  923. X    if (hp == NULL)
  924. X        return -9999;
  925. X
  926. X    sock = socket(AF_INET, SOCK_STREAM, 0);
  927. X    if (sock < 0)
  928. X        return -errno;
  929. X
  930. X    bzero(&server, sizeof(server));
  931. X    server.sin_family = AF_INET;
  932. X    bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
  933. X    server.sin_port = htons(port);
  934. X
  935. X    if (connect(sock, &server, sizeof(server)) < 0)
  936. X    {
  937. X        close(sock);
  938. X        return -errno;
  939. X    }
  940. X
  941. X    return sock;
  942. X}
  943. X
  944. X/*
  945. X** readable()
  946. X**
  947. X** Poll a socket for pending input.  Returns immediately.  This is a front-end
  948. X** to waitread() below.
  949. X**
  950. X** Input: file descriptor to poll
  951. X** Output: 1 if data is available for reading
  952. X*/
  953. Xreadable(fd)
  954. Xint fd;
  955. X{
  956. X    return(waitread(fd, 0));
  957. X}
  958. X
  959. X/*
  960. X** waitread()
  961. X**
  962. X** Wait for data on a file descriptor for a little while.
  963. X**
  964. X** Input: file descriptor to watch
  965. X**      how long to wait, in seconds, before returning
  966. X** Output: 1 if data was available
  967. X**       0 if the timer expired or a signal occurred.
  968. X*/
  969. Xwaitread(fd, time)
  970. Xint fd, time;
  971. X{
  972. X    fd_set readbits, other;
  973. X    struct timeval timer;
  974. X    int ret;
  975. X
  976. X    timerclear(&timer);
  977. X    timer.tv_sec = time;
  978. X    FD_ZERO(&readbits);
  979. X    FD_ZERO(&other);
  980. X    FD_SET(fd, &readbits);
  981. X
  982. X    ret = select(fd+1, &readbits, &other, &other, &timer);
  983. X    if (FD_ISSET(fd, &readbits))
  984. X        return 1;
  985. X    return 0;
  986. X}
  987. X
  988. SHAR_EOF
  989. chmod 0600 socket.c || echo "restore of socket.c fails"
  990. sed 's/^X//' << 'SHAR_EOF' > supersrv.8 &&
  991. X.TH SUPERSRV 8 "July 14, 1988"
  992. X.SH NAME
  993. Xsupersrv \- manage network services
  994. X.SH SYNOPSIS
  995. X.B supersrv
  996. X.SH DESCRIPTION
  997. X.I Supersrv
  998. Xkeeps track of which services (see
  999. X.IR server (1))
  1000. Xare offered on a host.  It is automatically started by
  1001. X.I server
  1002. Xif it isn't already running; users should never have to run
  1003. X.I supersrv
  1004. Xexplicitly.
  1005. X.SH "SEE ALSO"
  1006. Xserver(1), client(1)
  1007. X.SH AUTHOR
  1008. XSteven Grimm (koreth@ssyx.ucsc.edu, ...!ucbvax!ssyx!koreth)
  1009. X.SH DISCUSSION
  1010. XThe superserver accepts connections from remote locations, usually
  1011. Xinitiated with the "telnet" program.  It reads a line of input from the
  1012. Xclient program, which specifies the name of the desired service.  If
  1013. Xone of the subservers has advertised the requested service, the
  1014. Xsuperserver forks off a child process.  The child writes the name of
  1015. Xthe requested service to the appropriate subserver, then acts as a
  1016. Xmailman, shuffling bytes between the remote user and the subserver
  1017. Xuntil one of them disconnects.  Meanwhile, the parent superserver
  1018. Xwaits for another connection, and the whole bloody mess starts over.
  1019. X.PP
  1020. XIf a superserver process is killed, all its subservers try to restart
  1021. Xit until one of them succeeds.
  1022. X.PP
  1023. X.I Supersrv
  1024. Xlistens on port number 3502; it's possible to request services via
  1025. Xthe
  1026. X.IR telnet (1)
  1027. Xprogram, though the
  1028. X.IR client (1)
  1029. Xinterface is preferred.  The fields sent by
  1030. X.I client,
  1031. Xterminated by newlines, are:
  1032. X.PP
  1033. Xservice name or LIST
  1034. X.br
  1035. Xusername to request service from (blank for any)
  1036. X.br
  1037. Xcommand line arguments, one per line (optional)
  1038. X.br
  1039. Xterminating blank line
  1040. X.PP
  1041. XThus, a session could look like this:
  1042. X.PP
  1043. X%
  1044. X.B telnet ucsco.ucsc.edu 3502
  1045. X.br
  1046. XTrying...
  1047. X.br
  1048. XConnected to UCSCO.UCSC.EDU.
  1049. X.br
  1050. XSuperServer -- enter service desired.
  1051. X.br
  1052. X.B webster <newline>
  1053. X.br
  1054. X.B <newline>
  1055. X.br
  1056. X.B topiary
  1057. X.br
  1058. X.B <newline>
  1059. X.br
  1060. X.IP 1.
  1061. Xto.pi.ary \\'to--pe--.er-e-\\ aj [L topiarius, fr. topia ornamental
  1062. Xgardening, irreg. fr. Gk topo]s place : of, relating to, or being the
  1063. Xpractice or art of training, cutting, and trimming trees or shrubs into odd
  1064. Xor ornamental shapes; also : characterized by such work
  1065. X.IP 2.
  1066. Xtopiary n : topiary art or gardening; also : a topiary garden
  1067. X.PP
  1068. XConnection closed by foreign host.
  1069. X.PP
  1070. XNote:
  1071. X.I supersrv
  1072. Xshould be placed in a directory that is in users' search paths, as
  1073. X.I server
  1074. Xneeds to find it.
  1075. X
  1076. SHAR_EOF
  1077. chmod 0600 supersrv.8 || echo "restore of supersrv.8 fails"
  1078. sed 's/^X//' << 'SHAR_EOF' > supersrv.c &&
  1079. X#include "common.h"
  1080. X
  1081. Xextern int errno;
  1082. X
  1083. X/*
  1084. X * SuperServer.
  1085. X */
  1086. X
  1087. X#define WELCOME    "SuperServer -- enter service desired.\n"
  1088. X#define NOSERV    "Service not offered.\n"
  1089. X
  1090. Xint thirty;
  1091. X
  1092. X/*
  1093. X * This structure is used to keep the database of available services.
  1094. X */
  1095. Xstruct service {
  1096. X    char    name[20];    /* Service name */
  1097. X    int    fd;        /* File descriptor that offers it */
  1098. X    struct service *next;    /* Next service in list */
  1099. X} *list = (struct service *)0;
  1100. X
  1101. Xchar users[NOFILE][16];        /* user connected to each fd */
  1102. X
  1103. Xmain(argc, argv)
  1104. Xchar **argv;
  1105. X{
  1106. X    struct itimerval it;        /* Alarm! */
  1107. X    int    fd_so,            /* Socket() file descriptor */
  1108. X        fd_co;            /* Connected file descriptor */
  1109. X    short    portno;            /* Port number to listen on */
  1110. X    char    request[80];
  1111. X    extern int sigchld();
  1112. X
  1113. X/*
  1114. X * First things first: put ourselves in the background.
  1115. X */
  1116. X    if (fork())
  1117. X        exit(0);
  1118. X
  1119. X    portno = SUPERPORT;
  1120. X    thirty = 30;
  1121. X
  1122. X/*
  1123. X * Set up the server socket on the appropriate port number and listen on it.
  1124. X */
  1125. X    fd_so = serversock(portno);
  1126. X    if (fd_so < 0)
  1127. X    {
  1128. X        perror("serversock");
  1129. X        exit(-1);
  1130. X    }
  1131. X
  1132. X    (void)listen(fd_so, 5);
  1133. X    setsockopt(fd_so, SOL_SOCKET, SO_LINGER, &thirty, sizeof(thirty));
  1134. X    fcntl(fd_so, F_SETOWN, getpid());
  1135. X
  1136. X/*
  1137. X * And we'll need to accomodate child processes...
  1138. X */
  1139. X    signal(SIGCHLD, sigchld);
  1140. X
  1141. X/*
  1142. X * Now keep accepting connections and interpreting them.
  1143. X */
  1144. X    while (1)
  1145. X    {
  1146. X        fd_co = getcon(fd_so);
  1147. X
  1148. X        if (fd_co < 0)
  1149. X        {
  1150. X            perror("accept");
  1151. X            exit(0);
  1152. X        }
  1153. X
  1154. X        fcntl(fd_co, F_SETOWN, getpid());
  1155. X        setsockopt(fd_co,SOL_SOCKET,SO_LINGER,&thirty, sizeof(thirty));
  1156. X
  1157. X        do {
  1158. X            write(fd_co, WELCOME, sizeof(WELCOME)-1);
  1159. X        } while (! getline(fd_co, request, sizeof(request)-1));
  1160. X
  1161. X        if (handle(fd_co, request))
  1162. X            close(fd_co);
  1163. X    }
  1164. X}
  1165. X
  1166. X/*
  1167. X * Get a connection, or handle a disconnected server.
  1168. X */
  1169. Xgetcon(old)
  1170. Xint old;
  1171. X{
  1172. X    struct    service *cur;
  1173. X    fd_set    reed, tread, other;
  1174. X    int    firstfd;
  1175. X
  1176. X    FD_ZERO(&reed);
  1177. X    FD_ZERO(&other);
  1178. X
  1179. X    for (cur = list; cur; cur = cur->next)
  1180. X        FD_SET(cur->fd, &reed);
  1181. X    FD_SET(old, &reed);
  1182. X
  1183. X    while (1)
  1184. X    {
  1185. X        tread = reed;
  1186. X        select(NOFILE, &tread, &other, &other, 0);
  1187. X        if (FD_ISSET(old, &tread))
  1188. X            break;
  1189. X        while (firstfd = ffs(tread))
  1190. X        {
  1191. X            killfd(--firstfd);
  1192. X            close(firstfd);
  1193. X            FD_CLR(firstfd, &tread);
  1194. X            FD_CLR(firstfd, &reed);
  1195. X        }
  1196. X    }
  1197. X    return( accept(old, 0, 0) );
  1198. X}
  1199. X
  1200. X
  1201. X/*
  1202. X * Get an input line from a file descriptor.  This is probably very slow.
  1203. X * Since it's only called once, though...
  1204. X */
  1205. Xgetline(fd, buf, len)
  1206. Xint fd, len;
  1207. Xchar *buf;
  1208. X{
  1209. X    int    index = 0;
  1210. X    char    c;
  1211. X
  1212. X    while (read(fd, &c, 1) == 1)
  1213. X    {
  1214. X        if (c == '\n')
  1215. X            break;
  1216. X
  1217. X        if (c == '\r')
  1218. X            continue;
  1219. X
  1220. X        if (index < len)
  1221. X            buf[index++] = c;
  1222. X    }
  1223. X
  1224. X    buf[index] = 0;
  1225. X    return index;
  1226. X}
  1227. X
  1228. X
  1229. X/*
  1230. X * Handle a user request.  This will either be "REGISTER" or some
  1231. X * user-defined function.
  1232. X */
  1233. Xhandle(fd, string)
  1234. Xint fd;
  1235. Xchar *string;
  1236. X{
  1237. X    struct service *cur;
  1238. X    char user[16];
  1239. X
  1240. X/*
  1241. X * If a subserver wants to register itself, grab service
  1242. X * names from it until it outputs an empty line.
  1243. X */
  1244. X    if (!strcmp(string, "REGISTER"))
  1245. X    {
  1246. X        char name[20];
  1247. X
  1248. X        if (! getline(fd, users[fd], 15))
  1249. X            return 1;
  1250. X
  1251. X        while (getline(fd, name, 19))
  1252. X        {
  1253. X            cur = (struct service *)malloc(sizeof(*cur));
  1254. X            strcpy(cur->name, name);
  1255. X            cur->fd = fd;
  1256. X            cur->next = list;
  1257. X            list = cur;
  1258. X        }
  1259. X        return 0;    /* Keep file descriptor open */
  1260. X    }
  1261. X
  1262. X    getline(fd, user, 15);
  1263. X
  1264. X    if (!strcmp(string, "LIST"))
  1265. X    {
  1266. X        char buf[80];
  1267. X
  1268. X        write(fd, "Username\tService\n--------\t-------\n", 34);
  1269. X
  1270. X        for (cur = list; cur; cur = cur->next)
  1271. X        {
  1272. X            if (user[0] && strcmp(user, users[cur->fd]))
  1273. X                continue;
  1274. X            sprintf(buf, "%-8s\t%s\n", users[cur->fd], cur->name);
  1275. X            write(fd, buf, strlen(buf));
  1276. X        }
  1277. X        return 1;
  1278. X    }
  1279. X
  1280. X    for (cur = list; cur; cur=cur->next)
  1281. X        if (! strcmp(string, cur->name))
  1282. X            if ((! user[0]) || (! strcmp(user, users[cur->fd])))
  1283. X                break;
  1284. X
  1285. X    if (! cur)
  1286. X    {
  1287. X        write(fd, NOSERV, sizeof(NOSERV));
  1288. X        return 1;
  1289. X    }
  1290. X
  1291. X    write(cur->fd, string, 20);
  1292. X
  1293. X    shuffle(cur->fd, fd);
  1294. X
  1295. X    return 1;
  1296. X}
  1297. X
  1298. Xsigchld()
  1299. X{
  1300. X    wait(0);
  1301. X}
  1302. X
  1303. X/*
  1304. X * Kill all entries in the linked list with a certain file
  1305. X * descriptor.
  1306. X */
  1307. Xkillfd(fd)
  1308. Xint fd;
  1309. X{
  1310. X    struct service *cur, *temp;
  1311. X
  1312. X    while (list && list->fd == fd)
  1313. X    {
  1314. X        temp = list->next;
  1315. X        free(list);
  1316. X        list = temp;
  1317. X    }
  1318. X
  1319. X    if (list)
  1320. X        for (cur = list; cur; cur = cur->next)
  1321. X            while (cur->next && cur->next->fd == fd)
  1322. X            {
  1323. X                temp = cur->next;
  1324. X                cur->next = cur->next->next;
  1325. X                free(temp);
  1326. X            }
  1327. X}
  1328. X
  1329. X
  1330. X/*
  1331. X * This is the kludgy part.  We want to effectively connect the
  1332. X * client and the appropriate subserver.  Since there's no way to
  1333. X * connect two sockets together, we have to fork off a child and
  1334. X * sit there shuffling bytes back and forth between the two file
  1335. X * descriptors.  When one of them shuts down, we shut the other one
  1336. X * down and die.
  1337. X *
  1338. X * For now, since only one client can be talking to each subserver
  1339. X * at a given time, we erase all the subserver's services from the
  1340. X * service list.  It will reconnect when it's done.
  1341. X */
  1342. X#ifndef MIN
  1343. X#define MIN(x,y)    (((x)>(y))?(y):(x))
  1344. X#endif
  1345. X
  1346. Xshuffle(subsrv, client)
  1347. Xint subsrv, client;
  1348. X{
  1349. X    int        fd;
  1350. X    fd_set        reed, rite, except;
  1351. X    extern void    quit();
  1352. X
  1353. X    killfd(subsrv);
  1354. X
  1355. X    if (fork())
  1356. X    {
  1357. X        close(subsrv);
  1358. X        return;
  1359. X    }
  1360. X
  1361. X    for (fd = 0; fd < NOFILE; fd++)
  1362. X        if (fd != client && fd != subsrv)
  1363. X            close(fd);
  1364. X
  1365. X    FD_ZERO(&reed);
  1366. X    FD_SET(client, &reed);
  1367. X    FD_SET(subsrv, &reed);
  1368. X    FD_ZERO(&rite);
  1369. X    except = reed;
  1370. X
  1371. X    fcntl(client, F_SETOWN, getpid());
  1372. X    fcntl(subsrv, F_SETOWN, getpid());
  1373. X/*    fcntl(client, F_SETFL, FNDELAY);
  1374. X    fcntl(subsrv, F_SETFL, FNDELAY);
  1375. X*/
  1376. X
  1377. X    signal(SIGURG, quit);
  1378. X    signal(SIGPIPE, quit);
  1379. X
  1380. X    while (1)
  1381. X    {
  1382. X        fd_set    tread, twrite, texcept;
  1383. X        int    numbytes, bsize, numread, zero = 0;
  1384. X        char    buf[4096];
  1385. X
  1386. X        tread = reed;
  1387. X        twrite = rite;
  1388. X        texcept = except;
  1389. X
  1390. X        select(NOFILE, &tread, &twrite, &texcept, (void *)0);
  1391. X
  1392. X        if (FD_ISSET(subsrv, &tread))
  1393. X        {
  1394. X            ioctl(subsrv, FIONREAD, &numbytes);
  1395. X            bsize = MIN(numbytes, sizeof(buf));
  1396. X            numread = read(subsrv, buf, bsize);
  1397. X            if (numread < 0 && errno != EWOULDBLOCK)
  1398. X            {
  1399. X                perror("subsrv");
  1400. X                exit(0);
  1401. X            }
  1402. X            if (! numread)
  1403. X            {
  1404. X                shutdown(client, 1);
  1405. X                shutdown(subsrv, 0);
  1406. X                FD_CLR(subsrv, &reed);
  1407. X            }
  1408. X            else
  1409. X                write(client, buf, numread);
  1410. X        }
  1411. X
  1412. X        if (FD_ISSET(client, &tread))
  1413. X        {
  1414. X            ioctl(client, FIONREAD, &numbytes);
  1415. X            bsize = MIN(numbytes, sizeof(buf));
  1416. X            numread = read(client, buf, bsize);
  1417. X            if (numread < 0 && errno != EWOULDBLOCK)
  1418. X            {
  1419. X                perror("client");
  1420. X                exit(0);
  1421. X            }
  1422. X            if (! numread)
  1423. X            {
  1424. X                shutdown(client, 0);
  1425. X                shutdown(subsrv, 1);
  1426. X                FD_CLR(client, &reed);
  1427. X            }
  1428. X            else
  1429. X                write(subsrv, buf, numread);
  1430. X        }
  1431. X
  1432. X/* If both sides were shut down, leave. */
  1433. X        if (! (FD_ISSET(client, &reed) || FD_ISSET(subsrv, &reed)))
  1434. X        {
  1435. X            close(client);
  1436. X            close(subsrv);
  1437. X            exit(0);
  1438. X        }
  1439. X
  1440. X        if (FD_ISSET(client, &texcept) || FD_ISSET(subsrv, &texcept))
  1441. X        {
  1442. X            close(client);
  1443. X            close(subsrv);
  1444. X            exit(0);
  1445. X        }
  1446. X    }
  1447. X}
  1448. X
  1449. Xvoid quit()
  1450. X{
  1451. X    exit(0);
  1452. X}
  1453. X
  1454. SHAR_EOF
  1455. chmod 0600 supersrv.c || echo "restore of supersrv.c fails"
  1456. exit 0
  1457.  
  1458.