home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1363 / doas.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.4 KB  |  199 lines

  1. /* This is doas, a system management utillity written in 1989
  2.  * by Aaron Sherman (asherman@dino.ulowell.edu). 
  3.  * 
  4.  * Because of the nature of doas, the following precautions
  5.  * are recommended:
  6.  *  
  7.  *  1. Keep it in /usr/etc
  8.  *  2. Keep the group ownership wheel.
  9.  *  3. Keep the protection mode 4750.
  10.  *
  11.  * As an extra nifty you might want to make a group, doas, for
  12.  * this program. 
  13.  *
  14.  * Usage: doas [user [group]] [-e prog [args]]
  15.  *
  16.  * To install a password, grab a password entry from the password file,
  17.  * most likely root's, then put it in the initialization for the 
  18.  * variable 'passwd'
  19.  */
  20.  
  21.  
  22. /*
  23.  * BUGS:
  24.  *  on a system where argv is not NULL terminated, the exec call
  25.  *  may seg-fault. I did not bother to write the fix for this, as
  26.  *  all of the machines that I use have this feature, and you
  27.  *  can danm well fix it yourself!
  28.  *  :-) -AJS
  29.  */
  30.  
  31. #include<pwd.h>
  32. #include<grp.h>
  33. #include<stdio.h>
  34.  
  35. #define STDSHELL "csh"
  36. #ifdef PARANOID
  37. #ifndef LOG_FILE
  38. #define LOG_FILE "/usr/adm/doas.log"
  39. #endif /* LOG_FILE */
  40. #endif
  41.  
  42. /* Please do not remove the next line */
  43. char author[]="\nDoas, by asherman@dino.ulowell.edu\n";
  44.  
  45. char *std[]={ STDSHELL,NULL };
  46. #ifndef DES
  47. char passwd[] = "foobar"; 
  48. #else
  49. #ifdef NO_ETCPASSWD
  50. char passwd[] = "HkiH4rSeJlozo";
  51. #else
  52. char *passwd;
  53. #endif /*ALPO*/
  54. #endif /*DES*/
  55.  
  56. main(argc,argv)
  57.  char **argv;
  58. {
  59.  struct passwd *pwd,*getpwnam();
  60.  struct group *grp,*getgrnam();
  61.  int user,group;
  62.  int pos;
  63.  char *shell,*getenv();
  64. #ifdef PARANOID
  65. #ifdef DES
  66.  char salt[3];
  67. #endif
  68.  char *pass,*getpass();
  69.  struct passwd *getpwuid();
  70.  char *myname,*getlogin();
  71.  
  72.  if ((myname=getlogin()) == NULL)
  73.  {
  74.   if ((pwd=getpwuid(getuid())) != NULL)
  75.   {
  76.    if ((myname=pwd->pw_name) == NULL)
  77.     noname();
  78.   }
  79.   else
  80.    noname();
  81.  }
  82. #endif
  83.  
  84.  if (setuid(0) == -1)
  85.  { 
  86.   fprintf(stderr,"doas: Unable to setuid.\n");
  87.   exit(1);
  88.  }
  89.  
  90. #ifdef PARANOID
  91.  log(myname);
  92.  
  93.  if ((pass=getpass("Password: ")) == NULL)
  94.  {
  95.   fprintf(stderr,"doas: Could not get password.\n");
  96.   exit(1);
  97.  }
  98. #ifdef DES
  99. #ifndef NO_ETCPASSWD
  100.  if ((pwd = getpwnam("root")) == NULL)
  101.  {
  102.   fprintf(stderr,"doas: No root entry!\n");
  103.   exit(1);
  104.  }
  105.  passwd=pwd->pw_passwd;
  106. #endif
  107.  strncpy(salt,passwd,2);
  108.  if (strcmp(passwd,crypt(pass,salt)))
  109. #else
  110.  if (strcmp(pass,passwd))
  111. #endif
  112.  {
  113.   fprintf(stderr,"Sorry...\n");
  114.   exit(1);
  115.  }
  116. #endif
  117.  
  118.  
  119.  if ((shell=getenv("SHELL")) != NULL)
  120.   std[0]=shell;
  121.  
  122.  group=getgid();
  123.  pos=1;
  124.  if (argc == 1) 
  125.  {
  126.   user=0;group=0;
  127.  }
  128.  else
  129.  {
  130.   if (strncmp(argv[1],"-e",2))
  131.   {
  132.    if ((pwd=getpwnam(argv[1])) == NULL)
  133.     user=atoi(argv[1]);
  134.    else
  135.     user=pwd->pw_uid;
  136.    pos++;
  137.    if (argc > 2)
  138.    {
  139.     if (strncmp(argv[2],"-e",2))
  140.     {
  141.      if ((grp=getgrnam(argv[2])) == NULL)
  142.      {
  143.       group=atoi(argv[2]);
  144.      }
  145.      else 
  146.      {
  147.       group=grp->gr_gid;
  148.      }
  149.      pos++;
  150.     }
  151.    }
  152.   }
  153.  }
  154.  
  155.  if (setgid(group) == -1)
  156.   fprintf(stderr,"doas: Group id: %d not set\n",group);
  157.  setuid(user);
  158.  
  159.  pos++;
  160.  if (argc>pos)
  161.  {
  162.   execvp(argv[pos],argv+pos);
  163.   fprintf(stderr,"doas: Was not able to execute %s.\n",argv[pos]);
  164.  }
  165.  else
  166.  {
  167.   execvp(std[0],std);
  168.   fprintf(stderr,"doas: Was not able to execute %s.\n",std[0]);
  169.  }
  170.  exit(1);
  171. }
  172.  
  173.  
  174. #ifdef PARANOID
  175. log(name)
  176.  char *name;
  177. {
  178.  FILE *fp,*fopen();
  179.  long the_time;
  180.  int uid;
  181.  
  182.  the_time=time(NULL);
  183.  if ((fp=fopen(LOG_FILE,"a")) == NULL)
  184.  {
  185.   fprintf(stderr,"doas: Could not open log file!\n");
  186.   exit(1);
  187.  }
  188.  fprintf(fp,"%s using doas at %s",name,ctime(&the_time));
  189.  fclose(fp);
  190. }
  191.  
  192.  
  193. noname()
  194. {
  195.  fprintf(stderr,"doas: Cannot confirm user id.\n");
  196.  exit(1);
  197. }
  198. #endif
  199.