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

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