util-linux/login-utils/newgrp.c

106 lines
2.3 KiB
C
Raw Normal View History

2006-12-06 17:25:32 -06:00
/* setgrp.c - by Michael Haardt. Set the gid if possible */
/* Added a bit more error recovery/reporting - poe */
/* Vesa Roukonen added code for asking password */
/* Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/ */
2006-12-06 17:25:39 -06:00
/* 1999-02-22 Arkadiusz Mi<4D>kiewicz <misiek@misiek.eu.org>
* - added Native Language Support
*/
2006-12-06 17:25:32 -06:00
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "pathnames.h"
2006-12-06 17:25:35 -06:00
#include "my_crypt.h"
2006-12-06 17:25:39 -06:00
#include "nls.h"
2006-12-06 17:25:32 -06:00
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
static int
allow_setgid(struct passwd *pe, struct group *ge)
{
char **look;
int notfound = 1;
if (getuid() == 0) return TRUE; /* root may do anything */
look = ge->gr_mem;
while (*look && (notfound = strcmp(*look++,pe->pw_name)));
if(!notfound) return TRUE; /* member of group => OK */
/* Ask for password. Often there is no password in /etc/group, so
contrary to login et al. we let an empty password mean the same
as * in /etc/passwd */
if(ge->gr_passwd && ge->gr_passwd[0] != 0) {
if(strcmp(ge->gr_passwd,
2006-12-06 17:25:39 -06:00
crypt(getpass(_("Password: ")), ge->gr_passwd)) == 0) {
2006-12-06 17:25:32 -06:00
return TRUE; /* password accepted */
}
}
return FALSE; /* default to denial */
}
int
main(int argc, char *argv[])
{
struct passwd *pw_entry;
struct group *gr_entry;
char *shell;
2006-12-06 17:25:39 -06:00
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
2006-12-06 17:25:32 -06:00
if (!(pw_entry = getpwuid(getuid()))) {
2006-12-06 17:25:39 -06:00
perror(_("newgrp: Who are you?"));
2006-12-06 17:25:32 -06:00
exit(1);
}
shell = (pw_entry->pw_shell[0] ? pw_entry->pw_shell : _PATH_BSHELL);
if (argc < 2) {
if(setgid(pw_entry->pw_gid) < 0) {
2006-12-06 17:25:39 -06:00
perror(_("newgrp: setgid"));
2006-12-06 17:25:32 -06:00
exit(1);
}
} else {
if (!(gr_entry = getgrnam(argv[1]))) {
2006-12-06 17:25:39 -06:00
perror(_("newgrp: No such group."));
2006-12-06 17:25:32 -06:00
exit(1);
} else {
if(allow_setgid(pw_entry, gr_entry)) {
if(setgid(gr_entry->gr_gid) < 0) {
2006-12-06 17:25:39 -06:00
perror(_("newgrp: setgid"));
2006-12-06 17:25:32 -06:00
exit(1);
}
} else {
2006-12-06 17:25:39 -06:00
puts(_("newgrp: Permission denied"));
2006-12-06 17:25:32 -06:00
exit(1);
}
}
}
if(setuid(getuid()) < 0) {
2006-12-06 17:25:39 -06:00
perror(_("newgrp: setuid"));
2006-12-06 17:25:32 -06:00
exit(1);
}
fflush(stdout); fflush(stderr);
execl(shell,shell,(char*)0);
2006-12-06 17:25:39 -06:00
perror(_("No shell"));
2006-12-06 17:25:32 -06:00
fflush(stderr);
exit(1);
}