home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / linux / 9991 < prev    next >
Encoding:
Text File  |  1992-09-08  |  4.3 KB  |  148 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!Sirius.dfn.de!chx400!bernina!almesber
  2. From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
  3. Newsgroups: comp.os.linux
  4. Subject: Re: suid for fdformat?
  5. Message-ID: <1992Sep6.130207.11486@bernina.ethz.ch>
  6. Date: 6 Sep 92 13:02:07 GMT
  7. References: <1992Aug30.062444.346@athena.mit.edu>
  8. Sender: news@bernina.ethz.ch (USENET News System)
  9. Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
  10. Lines: 136
  11.  
  12. In article <1992Aug30.062444.346@athena.mit.edu> hammond@kwhpc.caseng.com (Kevin W. Hammond) writes:
  13. > Should fdformat be installed as a suid program so regular users can format
  14. > floppies, or is it more typically found installed so only root can format
  15. > floppies?
  16.  
  17. It depends. Formatting is inherently more timing-critical than regular
  18. floppy accesses because some errors can only be detected in the
  19. verification pass. Therefore, it's considered an operation that needs
  20. some awareness on the user's part.
  21.  
  22. If you can live with users creating bad media on a loaded system,
  23. running fdformat suid isn't a problem. However, you should use the
  24. slightly modified version I've appended to this posting. It assures
  25. that only floppy devices can be formatted (the ioctls may have some
  26. random effect on other devices) and only if the user has write access
  27. to them.
  28.  
  29. - Werner
  30.  
  31. ---------------------------------- cut here -----------------------------------
  32.  
  33. /* fdformat.c  -  Low-level formats a floppy disk. */
  34.  
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #include <sys/stat.h>
  41. #include <linux/fd.h>
  42.  
  43.  
  44. static int ctrl;
  45. struct floppy_struct param;
  46.  
  47.  
  48. #define FLOPPY_MAJOR 2
  49. #define SECTOR_SIZE 512
  50. #define PERROR(msg) { perror(msg); exit(1); }
  51.  
  52.  
  53. static void format_disk(char *name)
  54. {
  55.     struct format_descr descr;
  56.     int track;
  57.     char dummy;
  58.  
  59.     printf("Formatting ... ");
  60.     fflush(stdout);
  61.     if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
  62.     for (track = 0; track < param.track; track++) {
  63.     descr.track = track;
  64.     descr.head = 0;
  65.     if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0) PERROR("\nioctl(FDFMTTRK)");
  66.     printf("%3d\b\b\b",track);
  67.     fflush(stdout);
  68.     if (param.head == 2) {
  69.         descr.head = 1;
  70.         if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0)
  71.         PERROR("\nioctl(FDFMTTRK)");
  72.     }
  73.     }
  74.     if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
  75.     printf("done\n");
  76. }
  77.  
  78.  
  79. static void verify_disk(char *name)
  80. {
  81.     unsigned char *data;
  82.     int fd,cyl_size,cyl,count;
  83.  
  84.     cyl_size = param.sect*param.head*512;
  85.     if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
  86.     printf("Verifying ... ");
  87.     fflush(stdout);
  88.     if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
  89.     for (cyl = 0; cyl < param.track; cyl++) {
  90.     printf("%3d\b\b\b",cyl);
  91.     fflush(stdout);
  92.     if (read(fd,data,cyl_size) != cyl_size) PERROR("read");
  93.     for (count = 0; count < cyl_size; count++)
  94.         if (data[count] != FD_FILL_BYTE) {
  95.         printf("bad data in cyl %d\nContinuing ... ",cyl);
  96.         fflush(stdout);
  97.         break;
  98.         }
  99.     }
  100.     printf("done\n");
  101.     if (close(fd) < 0) PERROR("close");
  102. }
  103.  
  104.  
  105. static void usage(char *name)
  106. {
  107.     char *this;
  108.  
  109.     if (this = strrchr(name,'/')) name = this+1;
  110.     fprintf(stderr,"usage: %s [ -n ] device\n",name);
  111.     exit(1);
  112. }
  113.  
  114.  
  115. main(int argc,char **argv)
  116. {
  117.     int verify;
  118.     char *name;
  119.     struct stat st;
  120.  
  121.     name = argv[0];
  122.     verify = 1;
  123.     if (argc > 1 && argv[1][0] == '-') {
  124.     if (argv[1][1] != 'n') usage(name);
  125.     verify = 0;
  126.     argc--;
  127.     argv++;
  128.     }
  129.     if (argc != 2) usage(name);
  130.     if (lstat(argv[1],&st) < 0) PERROR(argv[1]);
  131.     if (!S_ISBLK(st.st_mode) || st.st_rdev >> 8 != FLOPPY_MAJOR) {
  132.     fprintf(stderr,"%s: not a floppy device\n",argv[1]);
  133.     exit(1);
  134.     }
  135.     if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
  136.     if ((ctrl = open(argv[1],3)) < 0) PERROR(argv[1]);
  137.     if (ioctl(ctrl,FDGETPRM,(int) ¶m) < 0) PERROR("ioctl(FDGETPRM)");
  138.     printf("%sle-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
  139.       param.head ? "Doub" : "Sing",param.track,param.sect,param.size >> 1);
  140.     format_disk(argv[1]);
  141.     if (verify) verify_disk(argv[1]);
  142. }
  143. -- 
  144.    _________________________________________________________________________
  145.   / Werner Almesberger, ETH Zuerich, CH      almesber@nessie.cs.id.ethz.ch /
  146.  / IFW A44  Tel. +41 1 254 7213                 almesberger@rzvax.ethz.ch /
  147. /_BITNET:_ALMESBER@CZHETH5A__HEPNET/CHADNET:_[20579::]57414::ALMESBERGER_/
  148.