home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.coherent
- Path: sparky!uunet!psinntp!sugar!gilbertc
- From: gilbertc@NeoSoft.com (Gilbert Callaghan)
- Subject: Re: newgrp bug (was: Is there any hope left?)
- Organization: NeoSoft Communications Services -- (713) 684-5900
- Date: Sat, 9 Jan 1993 06:17:34 GMT
- Message-ID: <C0KpHB.2Cr@NeoSoft.com>
- References: <1993Jan2.052714.28037@netcom.com>
- Lines: 113
-
- In article <1993Jan2.052714.28037@netcom.com> messina@netcom.com (Tony Porczyk) writes:
- >
- >Anyway - has anyone noticed that newgrp ignores shell spec in
- >/etc/passwd and execs sh always? Since i have my ksh environment
- >nicely set up, that kinda screws me up and I have to manually exec ksh
- >then.
- >
- >Anyone has any ideas?
-
- Well I *just* got my copy of Coherent and I was looking for something
- to write, so here's my version of newgrp.c:
-
- * compile 'with cc newgrp.c'
- * then 'chmog 4111 root bin newgrp' <-- important!
- * and 'mv newgrp /bin/newgrp' <-- you may wish to save the old version
-
- --------------------------
-
- /*
- * newgrp.c - replacement for COH's newgrp command which seems
- * to ignore the SHELL environment variable.
- *
- * By Gilbert Callaghan - gilbertc@Sugar.NeoSoft.com - 1/8/92
- */
-
- #include <stdio.h>
- #include <errno.h>
- #include <pwd.h>
- #include <grp.h>
- #include <sgtty.h>
-
- struct passwd *getpwuid();
- struct group *getgrnam();
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- struct passwd *pwrec;
- struct group *grrec;
- int gid;
- char **pp, str[80];
- struct sgttyb tty;
-
- if (argc != 2)
- {
- fputs("Usage: newgrp <group_name | group_number>\n", stderr);
- exec_shell();
- }
-
- /* get the desired gid from the given group group number or name */
- gid = atoi(argv[1]);
- if (gid != 0 || argv[1][0] == '0')
- grrec = getgrgid(gid);
- else
- grrec = getgrnam(argv[1]);
-
- if (grrec == (struct group *)0)
- {
- fputs("No such gid\n", stderr);
- exec_shell();
- }
- gid = grrec->gr_gid;
-
- /* but do we have permission (in /etc/group) to change to this group? */
- pwrec = getpwuid(getuid());
- for (pp=grrec->gr_mem; *pp; pp++)
- {
- if (!strcmp(*pp, pwrec->pw_name))
- {
- setgid(gid); /* set the desired gid */
- exec_shell(); /* and start a new shell (using $SHELL) */
- }
- }
-
- /* no, so prompt for password - if one exists in /etc/group */
- if (*(grrec->gr_passwd))
- {
- fputs("password: ", stderr);
- gtty(0, &tty);tty.sg_flags &= ~ECHO; stty(0, &tty);
- gets(str);
- tty.sg_flags ^= ECHO; stty(0, &tty); fputs("\n", stderr);
- if (!strcmp(grrec->gr_passwd, crypt(str, grrec->gr_passwd)))
- {
- setgid(gid); /* set the desired gid */
- exec_shell(); /* and start a new shell */
- }
- else
- {
- fputs("Sorry\n", stderr);
- exec_shell();
- }
- }
-
- /* We don't have the permissions - just start a new shell */
- fputs("Not in access list\n", stderr);
- exec_shell();
- }
-
- exec_shell()
- {
- char shell[80];
-
- setuid(getuid()); /* switch back to original owner */
- strcpy(shell, getenv("SHELL")); /* get shell environment */
- if (!shell[0]) strcpy(shell, "sh"); /* (make one up if not set) */
- execlp(shell, shell, (char *)0); /* and start up the new shell. */
- perror("Can't start shell"); /* Just in case it fails. */
- }
-
- --
- Gilbert Callaghan
- gilbertc@Sugar.NeoSoft.com
-