]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/newgrp.c
1 /* setgrp.c - by Michael Haardt. Set the gid if possible */
2 /* Added a bit more error recovery/reporting - poe */
3 /* Vesa Roukonen added code for asking password */
4 /* Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/ */
6 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
7 * - added Native Language Support
17 #include "pathnames.h"
30 allow_setgid(struct passwd
*pe
, struct group
*ge
)
35 if (getuid() == 0) return TRUE
; /* root may do anything */
38 while (*look
&& (notfound
= strcmp(*look
++,pe
->pw_name
)));
40 if(!notfound
) return TRUE
; /* member of group => OK */
42 /* Ask for password. Often there is no password in /etc/group, so
43 contrary to login et al. we let an empty password mean the same
44 as * in /etc/passwd */
46 if(ge
->gr_passwd
&& ge
->gr_passwd
[0] != 0) {
47 if(strcmp(ge
->gr_passwd
,
48 crypt(getpass(_("Password: ")), ge
->gr_passwd
)) == 0) {
49 return TRUE
; /* password accepted */
53 return FALSE
; /* default to denial */
57 main(int argc
, char *argv
[])
59 struct passwd
*pw_entry
;
60 struct group
*gr_entry
;
63 setlocale(LC_ALL
, "");
64 bindtextdomain(PACKAGE
, LOCALEDIR
);
67 if (!(pw_entry
= getpwuid(getuid()))) {
68 perror(_("newgrp: Who are you?"));
72 shell
= (pw_entry
->pw_shell
[0] ? pw_entry
->pw_shell
: _PATH_BSHELL
);
75 if(setgid(pw_entry
->pw_gid
) < 0) {
76 perror(_("newgrp: setgid"));
80 if (!(gr_entry
= getgrnam(argv
[1]))) {
81 perror(_("newgrp: No such group."));
84 if(allow_setgid(pw_entry
, gr_entry
)) {
85 if(setgid(gr_entry
->gr_gid
) < 0) {
86 perror(_("newgrp: setgid"));
90 puts(_("newgrp: Permission denied"));
96 if(setuid(getuid()) < 0) {
97 perror(_("newgrp: setuid"));
101 fflush(stdout
); fflush(stderr
);
102 execl(shell
,shell
,(char*)0);
103 perror(_("No shell"));