home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / dialout / part01 / dialout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-11  |  4.8 KB  |  228 lines

  1. /* program to kill getty & bring up kermit on a tty line*/
  2. /* probably dynix specific ... don't know, don't care */
  3. /* j. nelson, (nelson@uncw.uucp) (nelson@ecsvax.uncecs.edu) */
  4. /* latest revision 
  5. Sat Mar 10 15:32:08 EST 1990
  6. */
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <pwd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12.  
  13. #include "dialout.h"
  14. /* the dialout.h files should be in specific subdirectories, e.g.*/
  15. /*avatex, courier, etc. */
  16. main(argc,argv)char *argv[];
  17. {
  18.     int i,fd; 
  19.         unsigned long t;
  20.     FILE *fp;
  21.     char buf[120], name[12], *strncpy();
  22.     struct passwd *p, *getpwuid();
  23.     char *q, *getlogin();
  24.  
  25.     (void)argc;
  26.     (void)argv; /*strictly for lint*/
  27.     q=getlogin();
  28.     if(!authorized(q)){
  29.         fprintf(stderr,"%s not authorized to dialout\n",q);
  30.         exit(1);
  31.         }
  32.     {
  33.         fp=fopen("/tmp/dialoutlog","a");
  34.         if(!fp)exit(2);
  35.         t=time(0);
  36.         fprintf(fp," + %s %s",q,ctime(&t));
  37.         fclose(fp);
  38.  
  39.     }
  40.     if(geteuid()!=0){
  41.         fprintf(stderr,"oops, this prog should be setuid root\n");
  42.         error(1);
  43.     }
  44.     fiddle();
  45.     p = getpwuid(getuid());
  46.     (void)strncpy(name,p->pw_name,11);
  47.     for(i=0;i<12;i++)if(name[i]<' ' || name[i]>0176)name[i]=0;
  48.     fprintf(stderr,"%s\n",name);
  49.     if(stat(LCKFILE,(struct stat *)0)>=0)
  50.     {
  51.         fd=open(LCKFILE,0);
  52.         if(fd>=0){
  53.             fprintf(stderr,"oops\n");
  54.             fprintf(stderr,"%s busy\n",LCKFILE);
  55.             i=read(fd,buf,11);
  56.             if(i>0)fprintf(stderr,"by %s ... try later\n",buf);
  57.             error(2);
  58.         }
  59.     }
  60.     else {
  61.         fprintf(stderr,"ok no lock\n");
  62.     }
  63.     (void)system(SED1);
  64. /*    (void)system("    diff /etc/ttys /tmp/dialout.tmp");*/
  65.     (void)system(DIFF1);
  66. /*    (void)system("    cp /etc/ttys /etc/ttys.bak.tst");*/
  67.     (void)system(CP1);
  68. /*    (void)system("    cp /tmp/dialout.tmp /etc/ttys");*/
  69.     (void)system(CP2);
  70.     i=kill(1,1); /*kill(pid,signal)*/
  71.     if(i<0){
  72.         fprintf(stderr,"could not kill(1,1)\n");
  73.         error(3);
  74.     }
  75.     (void)system("    rm -f /tmp/dialout.tmp" );
  76.     fd=open(DEVICE,1);
  77.     if(fd<0){
  78.         error(4);
  79.     }
  80.     i=write(fd,S1,S1L);
  81.     if(i<0)error(5);
  82.     sleep(1);
  83.     (void)system(CAT);
  84.  
  85.     fiddle();
  86. /*    i=fchown(fd,getuid(),getgid());*/
  87. /* apparently SysV does not have fchown() nor fchmod() :-(  */
  88.     i=chown(DEVICE,getuid(),getgid());
  89.     if(i<0)error(6);
  90. /*    i=fchmod(fd,0666);*/
  91.     i=chmod(DEVICE,0666);
  92.     if(i<0)error(7);
  93.     i=close(fd);
  94.     if(i<0)error(8);
  95.  
  96.     if(fork()==0)
  97.     {
  98.         /*child*/
  99.         i=setuid(getuid());/*relinquish root privileges in child*/
  100.         if(i<0 || geteuid()==0)error(9);
  101.         fprintf(stderr,"PLEASE exit from kermit normally so that\n");
  102.         fprintf(stderr,"the modem line can be reset properly.\n");
  103.         fprintf(stderr,
  104.         "The proper way is ctrl-\\, then \"c\", then \"ex\"\n");
  105.  
  106.         (void)execl(KERMIT,"kermit","-l",DEVICE,
  107.         "-b",SPEED,(char *)NULL);
  108.         fprintf(stderr,"exec failed\n");/*should never happen*/
  109.         error(10);/*should never happen*/
  110.     }
  111.     else
  112.     {
  113.         /*parent*/
  114.         /* (parent is still root) */
  115.         (void)wait(0);
  116.         fd=open(DEVICE,1);
  117.         if(fd<0){
  118.             error(11);
  119.         }
  120.         i=write(fd,S2,S2L);
  121.         if(i<0)error(12);
  122.         fprintf(stderr,"wait...\n");
  123.         sleep(2);
  124. /*    i=fchown(fd,0,0);*/
  125.     i=chown(DEVICE,0,0);
  126.     if(i<0)error(6);
  127. /*    i=fchmod(fd,0666);*/
  128.     i=chmod(DEVICE,0666);
  129.     if(i<0)error(7);
  130.         i=close(fd);
  131.         if(i<0)error(13);
  132.         (void)unlink(LCKFILE); /*ignore error, because kermit didit*/
  133.         (void)system(SED2);
  134.  
  135. /*        (void)system("    diff /etc/ttys /tmp/dialout.tmp");*/
  136.         (void)system(DIFF2);
  137. /*        (void)system("    cp /tmp/dialout.tmp /etc/ttys");*/
  138.         (void)system(CP3);
  139.         i=kill(1,1); /*kill(pid,signal)*/
  140.         if(i== -1){
  141.             fprintf(stderr,"could not kill(1,1)\n");
  142.             if(getuid()!=0 && getuid()!=100)error(14);
  143.         }
  144.         (void)system("    rm -f /tmp/dialout.tmp" );
  145.  
  146.     
  147.         fp=fopen("/tmp/dialoutlog","a");
  148.         if(!fp)exit(2);
  149.         t=time(0);
  150.         fprintf(fp," - %s %s",q,ctime(&t));
  151.         fclose(fp);
  152.  
  153.     
  154.     }
  155. }
  156. fiddle()
  157. {
  158.     struct passwd *p;
  159.     struct stat zork;
  160.     int i,c;
  161.     i=stat(DEVICE,&zork);
  162.     if(i<0)error(15);
  163.     if(zork.st_uid != 0){
  164.         p=getpwuid(zork.st_uid);
  165.         fprintf(stderr,
  166.         "the modem seems to be owned by %s\n",/*zork.st_uid*/p->pw_name);
  167.         fprintf(stderr,"do you wish to barge right on through?");
  168.         c=getchar(); while(getchar()!='\n');
  169.         if(c!='y')error(16);
  170.     }
  171.     if((zork.st_mode & 0777) != 0622){
  172.         fprintf(stderr,"%s has wrong mode\n",DEVICE);
  173.         fprintf(stderr,"do you wish to barge right on through?");
  174.         c=getchar(); while(getchar()!='\n');
  175.         if(c!='y')error(17);
  176.     }
  177. }
  178. error(i)
  179. {
  180.     FILE *fp;
  181.     char *q;
  182.     unsigned long t;
  183.     fprintf(stderr,"error %d\n",i);
  184.     (void)fflush(stderr);
  185.         fp=fopen("/tmp/dialoutlog","a");
  186.         if(!fp)exit(2);
  187.         t=time(0);
  188.         q=getlogin();
  189.         fprintf(fp," - error %d %s %s",i,q,ctime(&t));
  190.         fclose(fp);
  191.     exit(i);
  192. }
  193. kindex(s,t) 
  194. char s[],t[];
  195. {
  196.     int c,i,j,k;
  197.     c=s[0];
  198.     if(c==0){return -1;
  199.         }
  200.     c=t[0];
  201.     if(c==0){return -1;
  202.         }
  203.     for(i=0;s[i] !='\0'; i++){
  204.         for(j=i,k=0;t[k] !='\0' && s[j]==t[k];j++,k++)
  205.                 ;
  206.         if(t[k]=='\0')return (i);
  207.     }
  208.         return(-1);
  209. authorized(name)
  210. char *name;
  211. {
  212.     FILE *fp;
  213.     int k;
  214.     char buf[99];
  215.     fp=fopen(AUTHFILE,"r");
  216.     if(!fp)return 0;
  217.     while( fgets(buf,98,fp)==buf){
  218.     if(kindex(buf,name)==0){
  219.         fclose(fp);
  220.         return 1;
  221.     }
  222.     }
  223.     fclose(fp);
  224.     return 0;
  225.  
  226. }
  227.