home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part04 / INSTALL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  6.1 KB  |  279 lines

  1. #include <pwd.h>
  2. #include <grp.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/file.h>
  6. #include <sys/stat.h>
  7. #include "config/ptybin.h"
  8. #include "config/ptydir.h"
  9. #include "config/ptygroup.h"
  10. #include "config/sessconnfile.h"
  11. #include "config/sessfile.h"
  12. #include <utmp.h>
  13. #include "config/utmpfile.h"
  14. #include "config/wtmpfile.h"
  15. #include "config/ptygroup.h"
  16.  
  17. char ptybin[] = PTYBIN;
  18. char ptydir[] = PTYDIR;
  19. char sessconnnow[] = SESSCONNNOW_FILE;
  20. char sessconnlog[] = SESSCONNLOG_FILE;
  21. char sessnow[] = SESSNOW_FILE;
  22. char sesslog[] = SESSLOG_FILE;
  23. char utmp[] = UTMP_FILE;
  24. char wtmp[] = WTMP_FILE;
  25.  
  26. static int num = 0;
  27.  
  28. void section(s)
  29. char *s;
  30. {
  31.  ++num;
  32.  printf("\n%d. %s.\n",num,s);
  33. }
  34.  
  35. int dontskip(s,t,u)
  36. char *s;
  37. char *t;
  38. char *u;
  39. {
  40.  char buf[100];
  41.  char format[200];
  42.  sprintf(format,"! %s: ",s);
  43.  printf(format,t,u);
  44.  if (fgets(buf,sizeof(buf),stdin) == 0)
  45.    return 0;
  46.  if (buf[0] == 'o')
  47.   {
  48.    puts("Okay.");
  49.    return 1;
  50.   }
  51.  if (buf[0] == 's')
  52.   {
  53.    puts("Skipped.");
  54.    return 0;
  55.   }
  56.  return 1;
  57. }
  58.  
  59. copyf2d(fn,dirfn)
  60. char *fn;
  61. char *dirfn;
  62. {
  63.  int fdold;
  64.  int fdnew;
  65.  int r;
  66.  int n;
  67.  int w;
  68.  char buf[16384];
  69.  
  70.  fdold = open(fn,O_RDONLY);
  71.  if (fdold == -1)
  72.    return -1;
  73.  fdnew = open(dirfn,O_WRONLY | O_CREAT | O_TRUNC,0600);
  74.  if (fdnew == -1)
  75.   { close(fdold); return -1; }
  76.  while ((r = read(fdold,buf,sizeof(buf))) > 0)
  77.   {
  78.    n = 0;
  79.    while (n < r)
  80.     {
  81.      w = write(fdnew,buf + n,r - n);
  82.      if (w == -1)
  83.       {
  84.        close(fdold); close(fdnew); return -1;
  85.       }
  86.      n += w;
  87.     }
  88.   }
  89.  close(fdnew);
  90.  close(fdold);
  91.  if (r == -1)
  92.    return -1;
  93.  return 0;
  94. }
  95.  
  96. static char *ptyuname = "pty";
  97.  
  98. chownpty(fn)
  99. char *fn;
  100. {
  101.  struct passwd *own;
  102.  own = getpwnam(ptyuname);
  103.  if (!own)
  104.    return -1;
  105.  return chown(fn,own->pw_uid,-1);
  106. }
  107.  
  108. chgrptty(fn)
  109. char *fn;
  110. {
  111.  struct group *grp;
  112.  grp = getgrnam("tty");
  113.  if (!grp)
  114.    return -1;
  115.  return chown(fn,-1,grp->gr_gid);
  116. }
  117.  
  118. touch(fn)
  119. char *fn;
  120. {
  121.  int fd;
  122.  fd = open(fn,O_WRONLY | O_CREAT,0644);
  123.  if (fd == -1)
  124.    return -1;
  125.  close(fd);
  126.  if (chmod(fn,0644) == -1)
  127.    return -1;
  128.  if (chownpty(fn) == -1)
  129.    return -1;
  130.  return 0;
  131. }
  132.  
  133. static char CHOPTYSS[100] = "chown pty %s/%s";
  134.  
  135. dobin(fn,level)
  136. char *fn;
  137. int level;
  138. {
  139.  char dirfn[sizeof(ptybin) + 50];
  140.  sprintf(dirfn,"%s/%s",ptybin,fn);
  141.  if (dontskip("cp %s %s",fn,ptybin))
  142.    if (copyf2d(fn,dirfn) == -1)
  143.      perror("copy failed");
  144.  switch(level)
  145.   {
  146.    case 0:
  147.      if (dontskip("chmod 755 %s/%s",ptybin,fn))
  148.        if (chmod(dirfn,0755) == -1)
  149.      perror("chmod: cannot change mode");
  150.      break;
  151.    case 1:
  152.      if (dontskip("chmod 755 %s/%s",ptybin,fn))
  153.        if (chmod(dirfn,0755) == -1)
  154.      perror("chmod: cannot change mode");
  155.      break;
  156.    case 2:
  157.      if (dontskip("chgrp tty %s/%s",ptybin,fn))
  158.        if (chgrptty(dirfn) == -1)
  159.      perror("chgrp: cannot change group");
  160.      if (dontskip("chmod 2755 %s/%s",ptybin,fn))
  161.        if (chmod(dirfn,02755) == -1)
  162.      perror("chmod: cannot change mode");
  163.      break;
  164.    case 3:
  165.      if (dontskip(CHOPTYSS,ptybin,fn))
  166.        if (chownpty(dirfn) == -1)
  167.      perror("chown: cannot change owner");
  168.      if (dontskip("chmod 4755 %s/%s",ptybin,fn))
  169.        if (chmod(dirfn,04755) == -1)
  170.      perror("chmod: cannot change mode");
  171.      break;
  172.   }
  173. }
  174.  
  175. main(argc,argv)
  176. int argc;
  177. char *argv[];
  178. {
  179.  if (argv[1])
  180.   {
  181.    ptyuname = argv[1];
  182.    sprintf(CHOPTYSS,"chown %s %%s/%%s",ptyuname);
  183.   }
  184.  printf("I assume you've already set up a %s user and a tty (%d) group.\n\n",ptyuname,PTYGROUP);
  185.  printf("Each action will be printed before it is run. Press return to proceed.\n");
  186.  printf("Type skip (or anything beginning with an s) to skip a step.\n");
  187.  
  188.  section("Make pty session directory");
  189.  if (dontskip("mkdir %s",ptydir,""))
  190.    if (mkdir(ptydir,0700) == -1)
  191.      perror("mkdir: cannot create directory");
  192.  if (dontskip("chown %s %s",ptyuname,ptydir))
  193.    if (chownpty(ptydir) == -1)
  194.      perror("chown: cannot change owner");
  195.  
  196.  section("Make pty binary directory");
  197.  if (dontskip("mkdir %s",ptybin,""))
  198.    if (mkdir(ptybin,0700) == -1)
  199.      perror("mkdir: cannot create directory");
  200.  if (dontskip("chmod 755 %s",ptybin,""))
  201.    if (chmod(ptybin,0755) == -1)
  202.      perror("chmod: cannot change mode");
  203.  
  204.  section("Make session and session-connection log files");
  205.  if (dontskip("touch %s",sessnow,""))
  206.    if (touch(sessnow) == -1)
  207.      perror("touch: cannot touch file");
  208.  if (dontskip("touch %s",sesslog,""))
  209.    if (touch(sesslog) == -1)
  210.      perror("touch: cannot touch file");
  211.  if (dontskip("touch %s",sessconnnow,""))
  212.    if (touch(sessconnnow) == -1)
  213.      perror("touch: cannot touch file");
  214.  if (dontskip("touch %s",sessconnlog,""))
  215.    if (touch(sessconnlog) == -1)
  216.      perror("touch: cannot touch file");
  217.  
  218.  section("Make utmp and wtmp files (note: utmp will be owned by pty)");
  219.  if (dontskip("touch %s",utmp,""))
  220.    if (touch(utmp) == -1)
  221.      perror("touch: cannot touch file");
  222.  if (dontskip("touch %s",wtmp,""))
  223.    if (touch(wtmp) == -1)
  224.      perror("touch: cannot touch file");
  225.  
  226.  section("Copy executables into pty binary directory");
  227.  dobin("argv0",1);
  228.  dobin("biff",1);
  229.  dobin("checkptys",1);
  230.  dobin("condom",0);
  231.  dobin("ctrlv",1);
  232.  dobin("disconnect",3);
  233.  dobin("excloff",1);
  234.  dobin("exclon",1);
  235.  dobin("lock",1);
  236.  dobin("mesg",1);
  237.  dobin("nobuf",0);
  238.  dobin("pty",3);
  239.  dobin("reconnect",3);
  240.  dobin("script",0);
  241.  dobin("script.tidy",0);
  242.  dobin("sess",0);
  243.  dobin("sesskill",3);
  244.  dobin("sesslist",3);
  245.  dobin("sessmenu",1);
  246.  dobin("sessname",3);
  247.  dobin("sesswhere",1);
  248.  dobin("sesswho",1);
  249.  dobin("tiocsti",1);
  250.  dobin("tplay",1);
  251.  dobin("trecord",1);
  252.  dobin("tscript",0);
  253.  dobin("tty",1);
  254.  dobin("ttydetach",1);
  255.  dobin("ttyprotect",0);
  256.  dobin("users",1);
  257.  dobin("utmpinit",1);
  258.  dobin("waitfor",1);
  259.  dobin("wall",2);
  260.  dobin("who",1);
  261.  dobin("whoami",1);
  262.  dobin("write",2);
  263.  dobin("wtmprotate",0);
  264.  dobin("sessrotate",0);
  265.  dobin("sclogrotate",0);
  266.  dobin("sessnowinit",0);
  267.  dobin("scnowinit",0);
  268.  
  269.  section("Add log file rotations to daily, weekly, or monthly cron scripts");
  270.  printf("I'll leave this to you.\n");
  271.  printf("You may want to invoke wtmprotate, sessrotate, or sclogrotate.\n");
  272.  
  273.  section("Add utmp/sessnow/scnow initializations to /etc/rc.local");
  274.  printf("I'll leave this to you.\n");
  275.  printf("You may want to invoke utmpinit, sessnowinit, or scnowinit.\n");
  276.  
  277.  exit(0);
  278. }
  279.