2 * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2008, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include "prototypes.h"
44 #include "exitcodes.h"
48 extern char **newenvp;
49 extern char **environ;
53 static GETGROUPS_T *grouplist;
57 static bool is_newgrp;
63 /* local function prototypes */
64 static void usage (void);
65 static void check_perms (const struct group *grp,
67 const char *groupname);
68 static void syslog_sg (const char *name, const char *group);
71 * usage - print command usage message
73 static void usage (void)
76 fputs (_("Usage: newgrp [-] [group]\n"), stderr);
78 fputs (_("Usage: sg group [[-c] command]\n"), stderr);
83 * find_matching_group - search all groups of a given group id for
84 * membership of a given username
86 static struct group *find_matching_group (const char *name, gid_t gid)
93 while ((gr = getgrent ()) != NULL) {
94 if (gr->gr_gid != gid) {
99 * A group with matching GID was found.
100 * Test for membership of 'name'.
103 while ((NULL != *look) && notfound) {
104 notfound = (strcmp (*look, name) != 0);
116 * check_perms - check if the user is allowed to switch to this group
118 * If needed, the user will be authenticated.
120 * It will not return if the user could not be authenticated.
122 static void check_perms (const struct group *grp,
124 const char *groupname)
126 bool needspasswd = false;
132 * see if she is a member of this group (i.e. in the list of
133 * members of the group, or if the group is her primary group).
135 * If she isn't a member, she needs to provide the group password.
136 * If there is no group password, she will be denied access
140 if ( (grp->gr_gid != pwd->pw_gid)
141 && !is_on_list (grp->gr_mem, pwd->pw_name)) {
146 * If she does not have either a shadowed password, or a regular
147 * password, and the group has a password, she needs to give the
150 spwd = xgetspnam (pwd->pw_name);
152 pwd->pw_passwd = spwd->sp_pwdp;
155 if ((pwd->pw_passwd[0] == '\0') && (grp->gr_passwd[0] != '\0')) {
160 * Now I see about letting her into the group she requested. If she
161 * is the root user, I'll let her in without having to prompt for
162 * the password. Otherwise I ask for a password if she flunked one
163 * of the tests above.
165 if ((getuid () != 0) && needspasswd) {
167 * get the password from her, and set the salt for
168 * the decryption from the group file.
170 cp = getpass (_("Password: "));
176 * encrypt the key she gave us using the salt from the
177 * password in the group file. The result of this encryption
178 * must match the previously encrypted value in the file.
180 cpasswd = pw_encrypt (cp, grp->gr_passwd);
183 if (grp->gr_passwd[0] == '\0' ||
184 strcmp (cpasswd, grp->gr_passwd) != 0) {
186 snprintf (audit_buf, sizeof(audit_buf),
187 "authentication new-gid=%d", grp->gr_gid);
188 audit_logger (AUDIT_GRP_AUTH, Prog,
189 audit_buf, NULL, getuid (), 0);
192 "Invalid password for group `%s' from `%s'",
193 groupname, pwd->pw_name));
195 fputs (_("Invalid password.\n"), stderr);
199 snprintf (audit_buf, sizeof(audit_buf),
200 "authentication new-gid=%d", grp->gr_gid);
201 audit_logger (AUDIT_GRP_AUTH, Prog,
202 audit_buf, NULL, getuid (), 1);
209 /* The closelog is probably unnecessary, but it does no
215 snprintf (audit_buf, sizeof(audit_buf),
216 "changing new-group=%s", groupname);
217 audit_logger (AUDIT_CHGRP_ID, Prog,
218 audit_buf, NULL, getuid (), 0);
220 audit_logger (AUDIT_CHGRP_ID, Prog, "changing",
229 * syslog_sg - log the change of group to syslog
231 * The loggout will also be logged when the user will quit the
234 static void syslog_sg (const char *name, const char *group)
236 const char *loginname = getlogin ();
237 const char *tty = ttyname (0);
239 if (loginname != NULL) {
240 loginname = xstrdup (loginname);
246 if (loginname == NULL) {
251 } else if (strncmp (tty, "/dev/", 5) == 0) {
255 "user `%s' (login `%s' on %s) switched to group `%s'",
256 name, loginname, tty, group));
259 * We want to fork and exec the new shell in the child, leaving the
260 * parent waiting to log the session close.
262 * The parent must ignore signals generated from the console
263 * (SIGINT, SIGQUIT, SIGHUP) which might make the parent terminate
264 * before its child. When bash is exec'ed as the subshell, it
265 * generates a new process group id for itself, and consequently
266 * only SIGHUP, which is sent to all process groups in the session,
267 * can reach the parent. However, since arbitrary programs can be
268 * specified as login shells, there is no such guarantee in general.
269 * For the same reason, we must also ignore stop signals generated
270 * from the console (SIGTSTP, SIGTTIN, and SIGTTOU) in order to
271 * avoid any possibility of the parent being stopped when it
272 * receives SIGCHLD from the terminating subshell. -- JWP
277 signal (SIGINT, SIG_IGN);
278 signal (SIGQUIT, SIG_IGN);
279 signal (SIGHUP, SIG_IGN);
280 signal (SIGTSTP, SIG_IGN);
281 signal (SIGTTIN, SIG_IGN);
282 signal (SIGTTOU, SIG_IGN);
285 /* error in fork() */
286 fprintf (stderr, _("%s: failure forking: %s\n"),
287 is_newgrp ? "newgrp" : "sg", strerror (errno));
290 snprintf (audit_buf, sizeof(audit_buf),
291 "changing new-group=%s", group);
292 audit_logger (AUDIT_CHGRP_ID, Prog,
293 audit_buf, NULL, getuid (), 0);
295 audit_logger (AUDIT_CHGRP_ID, Prog, "changing",
300 } else if (child != 0) {
301 /* parent - wait for child to finish, then log session close */
303 gid_t gid = getgid();
304 struct group *grp = getgrgid (gid);
308 pid = waitpid (child, &cst, WUNTRACED);
309 if ((pid == child) && (WIFSTOPPED (cst) != 0)) {
310 /* stop when child stops */
311 kill (getpid (), WSTOPSIG(cst));
312 /* wake child when resumed */
313 kill (child, SIGCONT);
315 } while ( ((pid == child) && (WIFSTOPPED (cst) != 0))
316 || ((pid != child) && (errno == EINTR)));
317 /* local, no need for xgetgrgid */
320 "user `%s' (login `%s' on %s) returned to group `%s'",
321 name, loginname, tty, grp->gr_name));
324 "user `%s' (login `%s' on %s) returned to group `%d'",
325 name, loginname, tty, gid));
326 /* Either the user's passwd entry has a
327 * GID that does not match with any group,
328 * or the group was deleted while the user
329 * was in a newgrp session.*/
331 "unknown GID `%u' used by user `%s'",
338 /* child - restore signals to their default state */
339 signal (SIGINT, SIG_DFL);
340 signal (SIGQUIT, SIG_DFL);
341 signal (SIGHUP, SIG_DFL);
342 signal (SIGTSTP, SIG_DFL);
343 signal (SIGTTIN, SIG_DFL);
344 signal (SIGTTOU, SIG_DFL);
348 #endif /* USE_SYSLOG */
351 * newgrp - change the invokers current real and effective group id
353 int main (int argc, char **argv)
355 bool initflag = false;
361 const char *name, *prog;
363 char *command = NULL;
364 char **envp = environ;
375 (void) setlocale (LC_ALL, "");
376 (void) bindtextdomain (PACKAGE, LOCALEDIR);
377 (void) textdomain (PACKAGE);
380 * Save my name for error messages and save my real gid incase of
381 * errors. If there is an error i have to exec a new login shell for
382 * the user since her old shell won't have fork'd to create the
383 * process. Skip over the program name to the next command line
386 * This historical comment, and the code itself, suggest that the
387 * behavior of the system/shell on which it was written differed
388 * significantly from the one I am using. If this process was
389 * started from a shell (including the login shell), it was fork'ed
390 * and exec'ed as a child by that shell. In order to get the user
391 * back to that shell, it is only necessary to exit from this
392 * process which terminates the child of the fork. The parent shell,
393 * which is blocked waiting for a signal, will then receive a
394 * SIGCHLD and will continue; any changes made to the process
395 * persona or the environment after the fork never occurred in the
398 * Bottom line: we want to save the name and real gid for messages,
399 * but we do not need to restore the previous process persona and we
400 * don't need to re-exec anything. -- JWP
402 Prog = Basename (argv[0]);
403 is_newgrp = (strcmp (Prog, "newgrp") == 0);
404 OPENLOG (is_newgrp ? "newgrp" : "sg");
411 pwd = get_my_pwent ();
413 fprintf (stderr, _("unknown UID: %u\n"), getuid ());
415 audit_logger (AUDIT_CHGRP_ID, Prog, "changing", NULL,
418 SYSLOG ((LOG_WARN, "unknown UID %u", getuid ()));
425 * Parse the command line. There are two accepted flags. The first
426 * is "-", which for newgrp means to re-create the entire
427 * environment as though a login had been performed, and "-c", which
428 * for sg causes a command string to be executed.
430 * The next argument, if present, must be the new group name. Any
431 * remaining remaining arguments will be used to execute a command
432 * as the named group. If the group name isn't present, I just use
433 * the login group ID of the current user.
435 * The valid syntax are
436 * newgrp [-] [groupid]
437 * newgrp [-l] [groupid]
439 * sg [-] groupid [[-c command]
442 && ( (strcmp (argv[0], "-") == 0)
443 || (strcmp (argv[0], "-l") == 0))) {
450 * Do the command line for everything that is
453 if ((argc > 0) && (argv[0][0] != '-')) {
465 * skip -c if specified so both forms work:
466 * "sg group -c command" (as in the man page) or
467 * "sg group command" (as in the usage message).
469 if ((argc > 1) && (strcmp (argv[0], "-c") == 0)) {
478 * Do the command line for "newgrp". It's just making sure
479 * there aren't any flags and getting the new group name.
481 if ((argc > 0) && (argv[0][0] == '-')) {
484 } else if (argv[0] != (char *) 0) {
488 * get the group file entry for her login group id.
489 * the entry must exist, simply to be annoying.
491 * Perhaps in the past, but the default behavior now depends on the
492 * group entry, so it had better exist. -- JWP
494 grp = xgetgrgid (pwd->pw_gid);
496 fprintf (stderr, _("unknown GID: %lu\n"),
497 (unsigned long) pwd->pw_gid);
498 SYSLOG ((LOG_CRIT, "unknown GID: %lu",
499 (unsigned long) pwd->pw_gid));
502 group = grp->gr_name;
507 #ifdef HAVE_SETGROUPS
509 * get the current users groupset. The new group will be added to
510 * the concurrent groupset if there is room, otherwise you get a
511 * nasty message but at least your real and effective group id's are
514 /* don't use getgroups(0, 0) - it doesn't work on some systems */
517 grouplist = (GETGROUPS_T *) xmalloc (i * sizeof (GETGROUPS_T));
518 ngroups = getgroups (i, grouplist);
519 if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
522 /* not enough room, so try allocating a larger buffer */
527 perror ("getgroups");
530 snprintf (audit_buf, sizeof(audit_buf),
531 "changing new-group=%s", group);
532 audit_logger (AUDIT_CHGRP_ID, Prog,
533 audit_buf, NULL, getuid (), 0);
535 audit_logger (AUDIT_CHGRP_ID, Prog,
536 "changing", NULL, getuid (), 0);
541 #endif /* HAVE_SETGROUPS */
544 * now we put her in the new group. The password file entry for her
545 * current user id has been gotten. If there was no optional group
546 * argument she will have her real and effective group id set to the
547 * set to the value from her password file entry.
549 * If run as newgrp, or as sg with no command, this process exec's
550 * an interactive subshell with the effective GID of the new group.
551 * If run as sg with a command, that command is exec'ed in this
552 * subshell. When this process terminates, either because the user
553 * exits, or the command completes, the parent of this process
554 * resumes with the current GID.
556 * If a group is explicitly specified on the command line, the
557 * interactive shell or command is run with that effective GID.
558 * Access will be denied if no entry for that group can be found in
559 * /etc/group. If the current user name appears in the members list
560 * for that group, access will be granted immediately; if not, the
561 * user will be challenged for that group's password. If the
562 * password response is incorrect, if the specified group does not
563 * have a password, or if that group has been locked by gpasswd -R,
564 * access will be denied. This is true even if the group specified
565 * has the user's login GID (as shown in /etc/passwd). If no group
566 * is explicitly specified on the command line, the effect is
567 * exactly the same as if a group name matching the user's login GID
568 * had been explicitly specified. Root, however, is never
569 * challenged for passwords, and is always allowed access.
571 * The previous behavior was to allow access to the login group if
572 * no explicit group was specified, irrespective of the group
573 * control file(s). This behavior is usually not desirable. A user
574 * wishing to return to the login group has only to exit back to the
575 * login shell. Generating yet more shell levels in order to
576 * provide a convenient "return" to the default group has the
577 * undesirable side effects of confusing the user, scrambling the
578 * history file, and consuming system resources. The default now is
579 * to lock out such behavior. A sys admin can allow it by explicitly
580 * including the user's name in the member list of the user's login
583 grp = getgrnam (group); /* local, no need for xgetgrnam */
585 fprintf (stderr, _("unknown group: %s\n"), group);
590 * For splitted groups (due to limitations of NIS), check all
591 * groups of the same GID like the requested group for
592 * membership of the current user.
594 grp = find_matching_group (name, grp->gr_gid);
597 * No matching group found. As we already know that
598 * the group exists, this happens only in the case
599 * of a requested group where the user is not member.
601 * Re-read the group entry for further processing.
603 grp = xgetgrnam (group);
606 sgrp = getsgnam (group);
608 grp->gr_passwd = sgrp->sg_passwd;
609 grp->gr_mem = sgrp->sg_mem;
614 * Check if the user is allowed to access this group.
616 check_perms (grp, pwd, group);
619 * all successful validations pass through this point. The group id
620 * will be set, and the group added to the concurrent groupset.
623 if (getdef_bool ("SYSLOG_SG_ENAB")) {
624 syslog_sg (name, group);
626 #endif /* USE_SYSLOG */
630 #ifdef HAVE_SETGROUPS
632 * I am going to try to add her new group id to her concurrent group
633 * set. If the group id is already present i'll just skip this part.
634 * If the group doesn't fit, i'll complain loudly and skip this
637 for (i = 0; i < ngroups; i++) {
638 if (gid == grouplist[i]) {
643 if (ngroups >= sysconf (_SC_NGROUPS_MAX)) {
644 fputs (_("too many groups\n"), stderr);
646 grouplist[ngroups++] = gid;
647 if (setgroups (ngroups, grouplist)) {
648 perror ("setgroups");
655 * Set the effective GID to the new group id and the effective UID
656 * to the real UID. For root, this also sets the real GID to the
659 if (setgid (gid) != 0) {
662 snprintf (audit_buf, sizeof(audit_buf),
663 "changing new-gid=%d", gid);
664 audit_logger (AUDIT_CHGRP_ID, Prog,
665 audit_buf, NULL, getuid (), 0);
670 if (setuid (getuid ()) != 0) {
673 snprintf (audit_buf, sizeof(audit_buf),
674 "changing new-gid=%d", gid);
675 audit_logger (AUDIT_CHGRP_ID, Prog,
676 audit_buf, NULL, getuid (), 0);
682 * See if the "-c" flag was used. If it was, i just create a shell
683 * command for her using the argument that followed the "-c" flag.
687 execl ("/bin/sh", "sh", "-c", command, (char *) 0);
689 snprintf (audit_buf, sizeof(audit_buf),
690 "changing new-gid=%d", gid);
691 audit_logger (AUDIT_CHGRP_ID, Prog,
692 audit_buf, NULL, getuid (), 0);
695 exit (errno == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
699 * I have to get the pathname of her login shell. As a favor, i'll
700 * try her environment for a $SHELL value first, and then try the
701 * password file entry. Obviously this shouldn't be in the
702 * restricted command directory since it could be used to leave the
703 * restricted environment.
705 * Note that the following assumes this user's entry in /etc/passwd
706 * does not have a chroot * prefix. If it does, the * will be copied
707 * verbatim into the exec path. This is probably not an issue
708 * because if this user is operating in a chroot jail, her entry in
709 * the version of /etc/passwd that is accessible here should
710 * probably never have a chroot shell entry (but entries for other
711 * users might). If I have missed something, and this causes you a
712 * problem, try using $SHELL as a workaround; also please notify me
713 * at jparmele@wildbear.com -- JWP
715 cp = getenv ("SHELL");
716 if (!initflag && (NULL != cp)) {
718 } else if ((NULL != pwd->pw_shell) && ('\0' != pwd->pw_shell[0])) {
719 prog = pwd->pw_shell;
725 * Now I try to find the basename of the login shell. This will
726 * become argv[0] of the spawned command.
728 cp = Basename ((char *) prog);
738 * Switch back to her home directory if i am doing login
742 if (chdir (pwd->pw_dir) != 0) {
746 while (NULL != *envp) {
747 if (strncmp (*envp, "PATH=", 5) == 0 ||
748 strncmp (*envp, "HOME=", 5) == 0 ||
749 strncmp (*envp, "SHELL=", 6) == 0 ||
750 strncmp (*envp, "TERM=", 5) == 0)
751 addenv (*envp, NULL);
756 while (NULL != *envp) {
757 addenv (*envp, NULL);
763 snprintf (audit_buf, sizeof(audit_buf), "changing new-gid=%d", gid);
764 audit_logger (AUDIT_CHGRP_ID, Prog, audit_buf, NULL, getuid (), 1);
767 * Exec the login shell and go away. We are trying to get back to
768 * the previous environment which should be the user's login shell.
770 err = shell (prog, initflag ? (char *) 0 : cp, newenvp);
771 exit (err == ENOENT ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
776 * The previous code, when run as newgrp, re-exec'ed the shell in
777 * the current process with the original gid on error conditions.
778 * See the comment above. This historical behavior now has the
779 * effect of creating unlogged extraneous shell layers when the
780 * command line has an error or there is an authentication failure.
781 * We now just want to exit with error status back to the parent
782 * process. The closelog is probably unnecessary, but it does no
788 snprintf (audit_buf, sizeof(audit_buf),
789 "changing new-group=%s", group);
790 audit_logger (AUDIT_CHGRP_ID, Prog,
791 audit_buf, NULL, getuid (), 0);
793 audit_logger (AUDIT_CHGRP_ID, Prog,
794 "changing", NULL, getuid (), 0);