]>
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 #define _XOPEN_SOURCE /* for crypt() */
13 #include "pathnames.h"
24 allow_setgid(struct passwd
*pe
, struct group
*ge
)
29 if (getuid() == 0) return TRUE
; /* root may do anything */
32 while (*look
&& (notfound
= strcmp(*look
++,pe
->pw_name
)));
34 if(!notfound
) return TRUE
; /* member of group => OK */
36 /* Ask for password. Often there is no password in /etc/group, so
37 contrary to login et al. we let an empty password mean the same
38 as * in /etc/passwd */
40 if(ge
->gr_passwd
&& ge
->gr_passwd
[0] != 0) {
41 if(strcmp(ge
->gr_passwd
,
42 crypt(getpass("Password: "), ge
->gr_passwd
)) == 0) {
43 return TRUE
; /* password accepted */
47 return FALSE
; /* default to denial */
51 main(int argc
, char *argv
[])
53 struct passwd
*pw_entry
;
54 struct group
*gr_entry
;
57 if (!(pw_entry
= getpwuid(getuid()))) {
58 perror("newgrp: Who are you?");
62 shell
= (pw_entry
->pw_shell
[0] ? pw_entry
->pw_shell
: _PATH_BSHELL
);
65 if(setgid(pw_entry
->pw_gid
) < 0) {
66 perror("newgrp: setgid");
70 if (!(gr_entry
= getgrnam(argv
[1]))) {
71 perror("newgrp: No such group.");
74 if(allow_setgid(pw_entry
, gr_entry
)) {
75 if(setgid(gr_entry
->gr_gid
) < 0) {
76 perror("newgrp: setgid");
80 puts("newgrp: Permission denied");
86 if(setuid(getuid()) < 0) {
87 perror("newgrp: setuid");
91 fflush(stdout
); fflush(stderr
);
92 execl(shell
,shell
,(char*)0);