home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a524 / 28.ddi / admin / osh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-04  |  2.9 KB  |  113 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: osh.c,v 6.2 89/02/23 00:24:21 aespinei Exp $ osh.c Copyr (c) 1986 Oracle";
  4. #endif  /* RCSID */
  5.  
  6. /*
  7. ** Copyright (c) 1986  by Oracle Corporation
  8. **
  9. ** osh - Routine to modify a user's ulimit
  10. **
  11. ** Under UNIX the largest negating factor for large database is
  12. ** the process's "ulimit" parameter. Currently this only can be reset
  13. ** by the "super-user". This program needs to be run with the ownership
  14. ** of root and the "set uid" bit 'on'. The recommended mode of this program
  15. ** 4711 (-rws--x--x) which only allows this program to be "executed" but
  16. ** not copied. It raises the ulimit to a theoretical maximum and then
  17. ** "exec's" the shell specified in its name. "osh" will exec "sh", "ocsh"
  18. ** will exec "csh", etc. If the program is run by anybody other than the
  19. ** super-user, it automatically resets the program's uid to that of the
  20. ** person executing "osh" so as not to leave the user with unauthorized
  21. ** privelages.
  22. **
  23. ** PLEASE NOTE: Any program with a set uid bit set is a POTENTIAL security
  24. **              problem and needs to be carefully controlled.
  25. **
  26. ** Written:
  27. **    11/01/84 Abbajay
  28. ** Modified:
  29. **      07/20/86 Abbajay  - Added optional "SHELL" variable lookup.
  30. **      02/22/89 Andres   - Added -l flag to print current ulimit.
  31. **
  32. ** Compile: cc -O osh.c -o osh
  33. ** Install: with set uid root (4711)
  34. */
  35.  
  36. #include <stdio.h>
  37. #include <errno.h>
  38.  
  39. #define NEWLIMIT 2113674        /* define newlimit for upping ulimit */
  40. #define DEFAULT_SHELL "/bin/sh"        /* define deault shell */
  41.  
  42. char *getenv();
  43.  
  44. main(argc, argv, envp)
  45. int argc;
  46. char **argv;
  47. char **envp;
  48.  
  49. {
  50.   register char *cmd;            /* command to execute */
  51.   register long newlimit=NEWLIMIT;    /* define newlimit for upping ulimit */
  52.   char *shell;
  53.   register int i;
  54.   char *nargv[4096];
  55.   long ulimit(), oldlimit;
  56.   
  57.   if((argc >= 1) && (strcmp(argv[1],"-l") == 0))
  58.   {
  59.      if ( (oldlimit = ulimit(1)) == -1)
  60.      {
  61.      fprintf(stderr, "%s: Couldn't get process ulimit.\n",argv[0]);
  62.      exit(-1);
  63.      } else {
  64.      printf("%d\n",oldlimit);
  65.      exit(0);
  66.      }
  67.   }
  68.   
  69.   if(ulimit(2, newlimit) == -1)
  70.   {
  71.      if (errno == EPERM)
  72.      fprintf(stderr, "%s: Can't raise ulimit. Probably not 'suid root'.\n",
  73.             argv[0]);
  74.      else
  75.      perror(argv[0]);
  76.      exit(-1);
  77.   }
  78.   if(setuid(getuid()))            /* set uid back to original */
  79.   {
  80.      perror(argv[0]);
  81.      exit(-1);
  82.   }
  83.   if (argc == 1)
  84.   {
  85.      shell = nargv[0] = (nargv[0] = getenv("SHELL")) ? nargv[0] : DEFAULT_SHELL;
  86.      nargv[1] = NULL;
  87.      if (execve(shell, nargv, envp))
  88.      {
  89.          perror(argv[0]);
  90.          exit(-1);
  91.      }
  92.   }
  93.   else
  94.   {
  95.      i=1;
  96.      while (argv[i] != NULL)  {
  97.        nargv[i-1] = argv[i];
  98.        i++;
  99.      }
  100.      nargv[i]=NULL;
  101.      if (execvp(nargv[0], nargv))
  102.      {
  103.         if (errno != ENOTTY)        /* abort on any other error */
  104.         {
  105.           perror(argv[0]);
  106.           exit(-1);
  107.         }
  108.      }
  109.   }
  110.   exit(0);
  111. }
  112.                 /* END PROGRAM */
  113.