home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / linux / 21006 < prev    next >
Encoding:
Text File  |  1992-12-21  |  6.4 KB  |  193 lines

  1. Newsgroups: comp.os.linux
  2. Path: sparky!uunet!stanford.edu!rock!taco!jlnance
  3. From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  4. Subject: ANNOUNCE Normal user floppy file systems
  5. Message-ID: <1992Dec18.200918.14314@ncsu.edu>
  6. Originator: jlnance@volt.ece.ncsu.edu
  7. Keywords: normal floppy
  8. Sender: news@ncsu.edu (USENET News System)
  9. Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  10. Organization: North Carolina State University, Project Eos
  11. Date: Fri, 18 Dec 1992 20:09:18 GMT
  12. Lines: 179
  13.  
  14.  
  15. I read some requests last week for information about allowing a normal user to
  16. mount floppys.  I have written a program which will do this.  I do not think
  17. that it adds any security risk to have it on your system.  If anyone can think
  18. of a way to use it to breach security, send me some mail and I will modify the
  19. program to try to make it more secure.
  20.  
  21. Read the comments for more doccumentation.
  22.  
  23. Jim Nance
  24.  
  25.  
  26.  
  27. /*
  28.  * This program provides a safe way for normal users to create, mount, and
  29.  * unmount floppy file systems.  When the floppy is mounted, this program
  30.  * executes the command chmod -f -R a-s to unset all of the suid/sgid bits.
  31.  * a better way would probably be to mount the floppy with an option to ignore
  32.  * set uid bits, but I don't know how to do this.
  33.  *
  34.  * The executable produced by the program must be owned by root, and have
  35.  * the suid bit set.  Ie:
  36.  *     gcc -O -s -o floppy floppy.c
  37.  *     chown root floppy
  38.  *     chmod 4755 floppy
  39.  *
  40.  * If you have a 3.5" drive, you should change the #define FlopBlocks to
  41.  * 2880, or you will have to use the -b2880 flag when you run the program.
  42.  * By default the program mounts the floppy on /tmp/floppy.  You can over ride
  43.  * this with the -m flag or you can change #define MntPoint to be something
  44.  * else.  If you mount the floppy on a nonexistant mount point, the program
  45.  * will create the mount point, and remove it when you unmount the floppy.  If
  46.  * the mount point exists before the program is run, it is not removed by the
  47.  * program when the floppy is unmounted.
  48.  *
  49.  * Summary of commands.  Assume the executable is called floppy.
  50.  *
  51.  * floppy format          # low  level formats the floppy
  52.  * floppy new             # high level formats the floppy
  53.  * floppy mount           # mounts the floppy
  54.  * floppy unmount         # unmounts the floppy
  55.  */
  56.  
  57. #include "stdlib.h"
  58. #include "string.h"
  59. #include "stdarg.h"
  60. #include "stdio.h"
  61. #include "sys/types.h"
  62. #include "sys/stat.h"
  63. #include "sys/wait.h"
  64. #include "unistd.h"
  65.  
  66. #define   MntPoint       "/tmp/floppy"
  67. #define   FlopBlocks     "2400"
  68. #define   Ssiz           256
  69.  
  70. /*VARARGS1*/
  71. void err_exit(char *fmt, ...) {
  72. va_list args;
  73.  
  74. va_start(args, fmt);
  75. vfprintf(stderr, fmt, args);
  76. va_end(args);
  77. fputs("\n", stderr);
  78. exit(1);
  79. }
  80.  
  81. /*VARARGS1*/
  82. void new_sys(char *fmt, ...) {
  83. char                     cmd[1024];
  84. va_list args;
  85.  
  86. va_start(args, fmt);
  87. vsprintf(cmd, fmt, args);
  88. va_end(args);
  89. printf("system: %s\n", cmd);
  90. system(cmd);
  91. }
  92.  
  93. void print_help(char **argv) {
  94. int                      i;
  95. static char              *cp[] = {
  96.      "\n",
  97.      "options are :\n",
  98.      "    -dX  -> use device X as the floppy drive. Default is /dev/fd0\n",
  99.      "    -eX  -> use program X in place of mkfs    Default is mkfs -c\n",
  100.      "    -bX  -> append X to mkfs command          Default is ",
  101.      FlopBlocks,
  102.      "\n",
  103.      "    -mX  -> mount floppy on directory X       Default is ",
  104.      MntPoint,
  105.      " \n",
  106.      " \n",
  107.      "cmd is one of:\n",
  108.      "    format  -> format the floppy\n",
  109.      "    new     -> create a file system on the floppy\n",
  110.      "    mount   -> mount   the floppy on /tmp/floppy\n",
  111.      "    unmount -> unmount the floppy\n",
  112.      NULL};
  113.  
  114. printf("USAGE: %s {options} cmd", argv[0]);
  115. for(i=0; cp[i]; fputs(cp[i++],stdout));
  116. }
  117.  
  118. int main(int argc, char **argv, char **envp) {
  119. char                     dev[Ssiz+1];
  120. char                     mfs[Ssiz+1];
  121. char                     blk[Ssiz+1];
  122. char                     pth[Ssiz+1+20];
  123. char                     *place;
  124. int                      i;
  125. int                      action=0;
  126. uid_t                    uid = getuid();
  127. gid_t                    gid = getgid();
  128. pid_t                    pid;
  129. struct stat              buf;
  130.  
  131. if(setenv("PATH","/bin:/usr/bin:/usr/local/bin:/etc:/usr/ucb", 1))
  132.      err_exit("Can not set PATH environment variable");
  133.  
  134. strcpy(dev,"/dev/fd0");
  135. strcpy(pth,MntPoint);
  136. strcpy(blk,FlopBlocks);
  137. strcpy(mfs,"mkfs -c");
  138.  
  139. for(i=1; i<argc; i++) {
  140.      if(!strncmp(argv[i], "-d", 2)) strncpy(dev,argv[i]+2,Ssiz)[Ssiz]='0'; else
  141.      if(!strncmp(argv[i], "-b", 2)) strncpy(blk,argv[i]+2,Ssiz)[Ssiz]='0'; else
  142.      if(!strncmp(argv[i], "-e", 2)) strncpy(mfs,argv[i]+2,Ssiz)[Ssiz]='0'; else
  143.      if(!strncmp(argv[i], "-m", 2)) strncpy(pth,argv[i]+2,Ssiz)[Ssiz]='0'; else
  144.      if(!strcmp(argv[i],"format" )) action=1;                              else
  145.      if(!strcmp(argv[i],"new"    )) action=2;                              else
  146.      if(!strcmp(argv[i],"mount"  )) action=3;                              else
  147.      if(!strcmp(argv[i],"unmount")) action=4;                              else
  148.      if(!strcmp(argv[i],"help"   )) {print_help(argv); exit(0);}
  149.      }
  150.  
  151. switch (action) {
  152.      case  1:
  153.           new_sys("fdformat %s", dev);
  154.           break;
  155.      case  2:
  156.           new_sys("%s %s %s",mfs, dev, blk);
  157.           break;
  158.      case  3:
  159.           if(stat(pth, &buf)) if(pid=fork()) wait(pid); else {
  160.                setuid(uid);
  161.                setgid(gid);
  162.                new_sys("mkdir -p %s/floppy.tmp.dir", pth);
  163.                new_sys("chmod 700 %s", pth);
  164.                exit(0);
  165.                }
  166.           if(stat(pth, &buf))        err_exit("Unable to create %s",pth);
  167.           if(buf.st_uid!=uid)        err_exit("You do not own %s", pth);
  168.           if(!(buf.st_mode&S_IFDIR)) err_exit("%s is not a directory",pth);
  169.           new_sys("mount %s %s", dev, pth);
  170.           new_sys("chmod -f -R a-s %s", pth);
  171.           chown(pth, uid, gid);
  172.           break;
  173.      case 4:
  174.           if(stat(pth, &buf))        err_exit("can not stat %s", pth);
  175.           if(buf.st_uid!=uid)        err_exit("You do not own %s", pth);
  176.           if(!(buf.st_mode&S_IFDIR)) err_exit("%s is not a directory",pth);
  177.           new_sys("umount %s", dev);
  178.  
  179.           for(place=pth; *place; place++);
  180.           strcpy(place,"/floppy.tmp.dir");
  181.           if(!stat(pth, &buf)) {
  182.                *place='\0';
  183.                new_sys("rm -rf %s", pth);
  184.                }
  185.           else *place='\0';
  186.  
  187.           break;
  188.      default: print_help(argv);
  189.      }
  190.  
  191. return 0;
  192. }
  193.