home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / privileges.c < prev    next >
C/C++ Source or Header  |  1997-11-13  |  4KB  |  163 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "patchlevel.h"
  5.  
  6. /*#define PRIV_DEBUG*/
  7.  
  8. #if 0
  9. #undef HAVE_SETEUID
  10. #define HAVE_SETRESUID
  11. #include <asm/unistd.h>
  12. int setresuid(int a, int b, int c)
  13. {
  14.     syscall(164, a, b, c);
  15.  
  16. }
  17. #endif
  18.  
  19. static inline void print_privs(const char *message)
  20. {
  21. #ifdef PRIV_DEBUG
  22.     /* for debugging purposes only */
  23.     fprintf(stderr,"%s egid=%d rgid=%d\n", message, getegid(), getgid());
  24.     fprintf(stderr,"%s euid=%d ruid=%d\n", message, geteuid(), getuid());
  25. #endif
  26. }
  27.  
  28. static int rgid, egid, ruid, euid;
  29.  
  30. /* privilege management routines for SunOS and Solaris.  These are
  31.  * needed in order to issue raw SCSI read/write ioctls.  Mtools drops
  32.  * its privileges at the beginning, and reclaims them just for the
  33.  * above-mentioned ioctl's.  Before popen(), exec() or system, it
  34.  * drops its privileges completely, and issues a warning.
  35.  */
  36.  
  37.  
  38. /* group id handling is lots easyer, as long as we don't use group 0.
  39.  * If you want to use group id's, create a *new* group mtools or
  40.  * floppy.  Chgrp any devices that you only want to be accessible to
  41.  * mtools to this group, and give them the appropriate privs.  Make
  42.  * sure this group doesn't own any other files: be aware that any user
  43.  * with access to mtools may mformat these files!
  44.  */
  45.  
  46.  
  47. static inline void Setuid(int uid)
  48. {
  49. #if defined HAVE_SETEUID || defined HAVE_SETRESUID
  50.     if(euid == 0) {
  51. #ifdef HAVE_SETEUID
  52.         seteuid(uid);
  53. #else
  54.         setresuid(ruid, uid, euid);
  55. #endif
  56.     } else
  57. #endif
  58.         setuid(uid);
  59. }
  60.  
  61. /* In reclaim_privs and drop privs, we have to manipulate group privileges
  62.  * when having no root privileges, else we might lose them */
  63.  
  64. void reclaim_privs(void)
  65. {
  66. #ifndef _OSK
  67.     setgid(egid);
  68.     Setuid(euid);
  69.     print_privs("after reclaim privs, both uids should be 0 ");
  70. #endif
  71. }
  72.  
  73. void drop_privs(void)
  74. {
  75. #ifndef _OSK
  76.     Setuid(ruid);
  77.     setgid(rgid);
  78.     print_privs("after drop_privs, real should be 0, effective should not ");
  79. #endif
  80. }
  81.  
  82. void destroy_privs(void)
  83. {
  84.  
  85. #if defined HAVE_SETEUID || defined HAVE_SETRESUID
  86.     if(euid == 0) {
  87. #ifdef HAVE_SETEUID
  88.         setuid(0); /* get the necessary privs to drop real root id */
  89.         setuid(ruid); /* this should be enough to get rid of the three
  90.                    * ids */
  91.         seteuid(ruid); /* for good measure... just in case we came
  92.                 * accross a system which implemented sane
  93.                 * semantics instead of POSIXly broken
  94.                 * semantics for setuid */
  95. #else
  96.         setresuid(ruid, ruid, ruid);
  97. #endif
  98.     }
  99. #endif
  100.  
  101.     /* we also destroy group privileges */
  102.     drop_privs();
  103.  
  104.     /* saved set [ug]id will go away by itself on exec */
  105.  
  106.     print_privs("destroy_privs, no uid should be zero  ");
  107. }
  108.  
  109.  
  110. void init_privs(void)
  111. {
  112. #ifndef _OSK
  113.     euid = geteuid();
  114.     ruid = getuid();
  115.     egid = getegid();
  116.     rgid = getgid();
  117.  
  118. #ifndef FD_CLOEXEC
  119.     if(euid != euid) {
  120.         fprintf(stderr,
  121.             "Setuid installation not supported on this platform\n");
  122.         fprintf(stderr,
  123.             "Missing FD_CLOEXEC");
  124.     }
  125.     exit(1);
  126. #endif
  127.     
  128.     if(euid == 0 && ruid != 0) {
  129. #ifdef HAVE_SETEUID
  130.         setuid(0); /* set real uid to 0 */
  131. #else
  132. #ifndef HAVE_SETRESUID
  133.         /* on this machine, it is not possible to reversibly drop
  134.          * root privileges.  We print an error and quit */
  135.  
  136.         /* BEOS is no longer a special case, as both euid and ruid
  137.          * return 0, and thus we do not get any longer into this
  138.          * branch */
  139.         fprintf(stderr,
  140.             "Seteuid call not supported on this architecture.\n");
  141.         fprintf(stderr,
  142.             "Mtools cannot be installed setuid root.\n");
  143.         fprintf(stderr,
  144.             "However, it can be installed setuid to a non root");
  145.         fprintf(stderr,
  146.             "user or setgid to any id.\n");
  147.         exit(1);
  148. #endif
  149. #endif
  150.     }
  151.     
  152.     drop_privs();
  153.     print_privs("after init, real should be 0, effective should not ");
  154. #endif
  155. }
  156.  
  157. void closeExec(int fd)
  158. {
  159. #ifdef FD_CLOEXEC
  160.     fcntl(fd, F_SETFL, FD_CLOEXEC);
  161. #endif
  162. }
  163.