home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / coherent / 6699 < prev    next >
Encoding:
Text File  |  1993-01-09  |  3.4 KB  |  124 lines

  1. Newsgroups: comp.os.coherent
  2. Path: sparky!uunet!psinntp!sugar!gilbertc
  3. From: gilbertc@NeoSoft.com (Gilbert Callaghan)
  4. Subject: Re: newgrp bug (was: Is there any hope left?)
  5. Organization: NeoSoft Communications Services -- (713) 684-5900
  6. Date: Sat, 9 Jan 1993 06:17:34 GMT
  7. Message-ID: <C0KpHB.2Cr@NeoSoft.com>
  8. References: <1993Jan2.052714.28037@netcom.com>
  9. Lines: 113
  10.  
  11. In article <1993Jan2.052714.28037@netcom.com> messina@netcom.com (Tony Porczyk) writes:
  12. >
  13. >Anyway - has anyone noticed that newgrp ignores shell spec in
  14. >/etc/passwd and execs sh always?  Since i have my ksh environment
  15. >nicely set up, that kinda screws me up and I have to manually exec ksh
  16. >then.  
  17. >
  18. >Anyone has any ideas?
  19.  
  20. Well I *just* got my copy of Coherent and I was looking for something
  21. to write, so here's my version of newgrp.c:
  22.  
  23. * compile 'with cc newgrp.c'
  24. * then 'chmog 4111 root bin newgrp'   <-- important!
  25. * and 'mv newgrp /bin/newgrp'         <-- you may wish to save the old version
  26.  
  27. --------------------------
  28.  
  29. /*
  30.  *  newgrp.c - replacement for COH's newgrp command which seems
  31.  *             to ignore the SHELL environment variable.
  32.  *
  33.  *  By Gilbert Callaghan - gilbertc@Sugar.NeoSoft.com - 1/8/92
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <errno.h>
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include <sgtty.h>
  41.  
  42. struct passwd *getpwuid();
  43. struct group *getgrnam();
  44.  
  45. main(argc, argv)
  46. int argc;
  47. char *argv[];
  48. {
  49. struct passwd *pwrec;
  50. struct group *grrec;
  51. int gid;
  52. char **pp, str[80];
  53. struct sgttyb tty;
  54.  
  55.     if (argc != 2)
  56.     {
  57.         fputs("Usage: newgrp <group_name | group_number>\n", stderr);
  58.         exec_shell();
  59.     }
  60.  
  61.     /* get the desired gid from the given group group number or name */
  62.     gid = atoi(argv[1]);
  63.     if (gid != 0 || argv[1][0] == '0')
  64.         grrec = getgrgid(gid);
  65.     else
  66.         grrec = getgrnam(argv[1]);
  67.  
  68.     if (grrec == (struct group *)0)
  69.     {
  70.         fputs("No such gid\n", stderr);
  71.         exec_shell();
  72.     }
  73.     gid = grrec->gr_gid;
  74.  
  75.     /* but do we have permission (in /etc/group) to change to this group? */
  76.     pwrec = getpwuid(getuid());
  77.     for (pp=grrec->gr_mem; *pp; pp++)
  78.     {
  79.         if (!strcmp(*pp, pwrec->pw_name))
  80.         {
  81.             setgid(gid);            /* set the desired gid */
  82.             exec_shell();           /* and start a new shell (using $SHELL) */
  83.         }
  84.     }
  85.  
  86.     /* no, so prompt for password - if one exists in /etc/group */
  87.     if (*(grrec->gr_passwd))
  88.     {
  89.         fputs("password: ", stderr);
  90.         gtty(0, &tty);tty.sg_flags &= ~ECHO; stty(0, &tty);
  91.         gets(str);
  92.     tty.sg_flags ^= ECHO; stty(0, &tty); fputs("\n", stderr);
  93.         if (!strcmp(grrec->gr_passwd, crypt(str, grrec->gr_passwd)))
  94.         {
  95.             setgid(gid);            /* set the desired gid */
  96.             exec_shell();           /* and start a new shell */
  97.         }
  98.         else
  99.         {
  100.             fputs("Sorry\n", stderr);
  101.             exec_shell();
  102.         }
  103.     }
  104.  
  105.     /* We don't have the permissions - just start a new shell */
  106.     fputs("Not in access list\n", stderr);
  107.     exec_shell();
  108. }
  109.  
  110. exec_shell()
  111. {
  112. char shell[80];
  113.  
  114.     setuid(getuid());                   /* switch back to original owner */
  115.     strcpy(shell, getenv("SHELL"));     /* get shell environment */
  116.     if (!shell[0]) strcpy(shell, "sh"); /* (make one up if not set) */
  117.     execlp(shell, shell, (char *)0);    /* and start up the new shell. */
  118.     perror("Can't start shell");        /* Just in case it fails. */
  119. }
  120.  
  121. --
  122. Gilbert Callaghan
  123. gilbertc@Sugar.NeoSoft.com
  124.