newgrp: fix coding style
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
3f9c237dcb
commit
b7cfaaf85b
|
@ -1,32 +1,30 @@
|
||||||
/* setgrp.c - by Michael Haardt. Set the gid if possible */
|
/* setgrp.c - by Michael Haardt. Set the gid if possible
|
||||||
/* Added a bit more error recovery/reporting - poe */
|
* Added a bit more error recovery/reporting - poe
|
||||||
/* Vesa Roukonen added code for asking password */
|
* Vesa Roukonen added code for asking password */
|
||||||
/* Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/ */
|
|
||||||
|
|
||||||
/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
|
/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
|
||||||
* - added Native Language Support
|
* - added Native Language Support
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <pwd.h>
|
|
||||||
#include <grp.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <grp.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef HAVE_CRYPT_H
|
#ifdef HAVE_CRYPT_H
|
||||||
# include <crypt.h>
|
# include <crypt.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "pathnames.h"
|
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "pathnames.h"
|
||||||
|
|
||||||
/* try to read password from gshadow */
|
/* try to read password from gshadow */
|
||||||
static char *
|
static char *get_gshadow_pwd(char *groupname)
|
||||||
get_gshadow_pwd(char *groupname)
|
|
||||||
{
|
{
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
char *pwd = NULL;
|
char *pwd = NULL;
|
||||||
|
@ -35,19 +33,20 @@ get_gshadow_pwd(char *groupname)
|
||||||
if (groupname == NULL || *groupname == '\0' || f == NULL)
|
if (groupname == NULL || *groupname == '\0' || f == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
while(fgets(buf, sizeof buf, f))
|
while (fgets(buf, sizeof buf, f)) {
|
||||||
{
|
|
||||||
char *cp = strchr(buf, ':');
|
char *cp = strchr(buf, ':');
|
||||||
if (!cp)
|
if (!cp)
|
||||||
continue; /* any junk in gshadow? */
|
/* any junk in gshadow? */
|
||||||
|
continue;
|
||||||
*cp = '\0';
|
*cp = '\0';
|
||||||
if (strcmp(buf, groupname) == 0)
|
if (strcmp(buf, groupname) == 0) {
|
||||||
{
|
|
||||||
if (cp - buf >= BUFSIZ)
|
if (cp - buf >= BUFSIZ)
|
||||||
break; /* only group name on line */
|
/* only group name on line */
|
||||||
|
break;
|
||||||
pwd = cp + 1;
|
pwd = cp + 1;
|
||||||
if ((cp = strchr(pwd, ':')) && pwd == cp + 1)
|
if ((cp = strchr(pwd, ':')) && pwd == cp + 1)
|
||||||
pwd = NULL; /* empty password */
|
/* empty password */
|
||||||
|
pwd = NULL;
|
||||||
else if (cp)
|
else if (cp)
|
||||||
*cp = '\0';
|
*cp = '\0';
|
||||||
break;
|
break;
|
||||||
|
@ -57,35 +56,41 @@ get_gshadow_pwd(char *groupname)
|
||||||
return pwd ? strdup(pwd) : NULL;
|
return pwd ? strdup(pwd) : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int allow_setgid(struct passwd *pe, struct group *ge)
|
||||||
allow_setgid(struct passwd *pe, struct group *ge)
|
|
||||||
{
|
{
|
||||||
char **look;
|
char **look;
|
||||||
int notfound = 1;
|
int notfound = 1;
|
||||||
char *pwd, *xpwd;
|
char *pwd, *xpwd;
|
||||||
|
|
||||||
if (getuid() == 0) return TRUE; /* root may do anything */
|
if (getuid() == 0)
|
||||||
if (ge->gr_gid == pe->pw_gid) return TRUE; /* You can switch back to your default group */
|
/* root may do anything */
|
||||||
|
return TRUE;
|
||||||
|
if (ge->gr_gid == pe->pw_gid)
|
||||||
|
/* You can switch back to your default group */
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
look = ge->gr_mem;
|
look = ge->gr_mem;
|
||||||
while (*look && (notfound = strcmp(*look++, pe->pw_name))) ;
|
while (*look && (notfound = strcmp(*look++, pe->pw_name))) ;
|
||||||
|
|
||||||
if(!notfound) return TRUE; /* member of group => OK */
|
if (!notfound)
|
||||||
|
/* member of group => OK */
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
/* Ask for password. Often there is no password in /etc/group, so
|
/* 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
|
* contrary to login et al. we let an empty password mean the same
|
||||||
as * in /etc/passwd */
|
* as in /etc/passwd */
|
||||||
|
|
||||||
/* check /etc/gshadow */
|
/* check /etc/gshadow */
|
||||||
if (!(pwd = get_gshadow_pwd(ge->gr_name)))
|
if (!(pwd = get_gshadow_pwd(ge->gr_name)))
|
||||||
pwd = ge->gr_passwd;
|
pwd = ge->gr_passwd;
|
||||||
|
|
||||||
if(pwd && *pwd && (xpwd = getpass(_("Password: ")))) {
|
if (pwd && *pwd && (xpwd = getpass(_("Password: "))))
|
||||||
if (strcmp(pwd, crypt(xpwd, pwd)) == 0)
|
if (strcmp(pwd, crypt(xpwd, pwd)) == 0)
|
||||||
return TRUE; /* password accepted */
|
/* password accepted */
|
||||||
}
|
return TRUE;
|
||||||
|
|
||||||
return FALSE; /* default to denial */
|
/* default to denial */
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
||||||
|
@ -99,8 +104,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
||||||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main(int argc, char *argv[])
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
{
|
||||||
struct passwd *pw_entry;
|
struct passwd *pw_entry;
|
||||||
struct group *gr_entry;
|
struct group *gr_entry;
|
||||||
|
@ -141,7 +145,8 @@ main(int argc, char *argv[])
|
||||||
if (errno)
|
if (errno)
|
||||||
err(EXIT_FAILURE, _("no such group"));
|
err(EXIT_FAILURE, _("no such group"));
|
||||||
else
|
else
|
||||||
errx(EXIT_FAILURE, _("no such group")); /* No group */
|
/* No group */
|
||||||
|
errx(EXIT_FAILURE, _("no such group"));
|
||||||
} else {
|
} else {
|
||||||
if (allow_setgid(pw_entry, gr_entry)) {
|
if (allow_setgid(pw_entry, gr_entry)) {
|
||||||
if (setgid(gr_entry->gr_gid) < 0)
|
if (setgid(gr_entry->gr_gid) < 0)
|
||||||
|
@ -154,7 +159,8 @@ main(int argc, char *argv[])
|
||||||
if (setuid(getuid()) < 0)
|
if (setuid(getuid()) < 0)
|
||||||
err(EXIT_FAILURE, _("setuid failed"));
|
err(EXIT_FAILURE, _("setuid failed"));
|
||||||
|
|
||||||
fflush(stdout); fflush(stderr);
|
fflush(stdout);
|
||||||
|
fflush(stderr);
|
||||||
execl(shell, shell, (char *)0);
|
execl(shell, shell, (char *)0);
|
||||||
warn(_("exec %s failed"), shell);
|
warn(_("exec %s failed"), shell);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
|
|
Loading…
Reference in New Issue