]>
git.saurik.com Git - apple/system_cmds.git/blob - newgrp.tproj/newgrp.c
2 * Copyright (c) 2002 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * newgrp -- change to a new group
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/usr.bin/newgrp/newgrp.c,v 1.2 2003/10/30 15:14:34 harti Exp $");
34 #include <sys/types.h>
42 #include <login_cap.h>
43 #endif /* !__APPLE__ */
51 #endif /* __APPLE__ */
52 static void addgroup(const char *grpname
);
53 static void doshell(void);
54 static int inarray(gid_t
, const gid_t
[], int);
55 static void loginshell(void);
56 static void restoregrps(void);
57 static void usage(void);
59 static struct passwd
*pwd
;
62 extern char **environ
;
64 /* Manipulate effective user ID. */
65 #define PRIV_START do { \
66 if (seteuid(euid) < 0) \
69 #define PRIV_END do { \
70 if (seteuid(getuid()) < 0) \
75 main(int argc
, char *argv
[])
80 if (seteuid(getuid()) < 0)
83 if ((pwd
= getpwuid(getuid())) == NULL
)
84 errx(1, "unknown user");
87 while ((ch
= getopt(argc
, argv
, "-l")) != -1) {
89 case '-': /* Obsolescent */
111 if (seteuid(euid
) < 0)
113 if (setuid(getuid()) < 0)
129 fprintf(stderr
, "usage: newgrp [-l] [group]\n");
139 initres
= initgroups(pwd
->pw_name
, pwd
->pw_gid
);
140 setres
= setgid(pwd
->pw_gid
);
150 addgroup(const char *grpname
)
152 gid_t grps
[NGROUPS_MAX
];
154 int dbmember
, i
, ngrps
;
162 /* Try it as a group name, then a group id. */
163 if ((grp
= getgrnam(grpname
)) == NULL
)
164 if ((lgid
= strtol(grpname
, &ep
, 10)) <= 0 || *ep
!= '\0' ||
165 (grp
= getgrgid((gid_t
)lgid
)) == NULL
) {
166 warnx("%s: bad group name", grpname
);
171 * If the user is not a member of the requested group and the group
172 * has a password, prompt and check it.
175 if (pwd
->pw_gid
== grp
->gr_gid
)
177 for (p
= grp
->gr_mem
; *p
!= NULL
; p
++)
178 if (strcmp(*p
, pwd
->pw_name
) == 0) {
182 if (!dbmember
&& *grp
->gr_passwd
!= '\0' && getuid() != 0) {
183 pass
= getpass("Password:");
185 strcmp(grp
->gr_passwd
, crypt(pass
, grp
->gr_passwd
)) != 0) {
186 fprintf(stderr
, "Sorry\n");
191 if ((ngrps
= getgroups(NGROUPS_MAX
, (gid_t
*)grps
)) < 0) {
196 /* Remove requested gid from supp. list if it exists. */
197 if (grp
->gr_gid
!= egid
&& inarray(grp
->gr_gid
, grps
, ngrps
)) {
198 for (i
= 0; i
< ngrps
; i
++)
199 if (grps
[i
] == grp
->gr_gid
)
202 memmove(&grps
[i
], &grps
[i
+ 1], (ngrps
- i
) * sizeof(gid_t
));
204 if (setgroups(ngrps
, (const gid_t
*)grps
) < 0) {
213 if (setgid(grp
->gr_gid
)) {
219 grps
[0] = grp
->gr_gid
;
221 /* Add old effective gid to supp. list if it does not exist. */
222 if (egid
!= grp
->gr_gid
&& !inarray(egid
, grps
, ngrps
)) {
223 if (ngrps
== NGROUPS_MAX
)
224 warnx("too many groups");
226 grps
[ngrps
++] = egid
;
228 if (setgroups(ngrps
, (const gid_t
*)grps
)) {
240 inarray(gid_t gid
, const gid_t grps
[], int ngrps
)
244 for (i
= 0; i
< ngrps
; i
++)
251 * Set the environment to what would be expected if the user logged in
252 * again; this performs the same steps as su(1)'s -l option.
257 char *args
[2], **cleanenv
, *term
, *ticket
;
261 #endif /* !__APPLE__ */
262 shell
= pwd
->pw_shell
;
264 shell
= _PATH_BSHELL
;
265 if (chdir(pwd
->pw_dir
) < 0) {
266 warn("%s", pwd
->pw_dir
);
270 term
= getenv("TERM");
271 ticket
= getenv("KRBTKFILE");
273 if ((cleanenv
= calloc(20, sizeof(char *))) == NULL
)
278 lc
= login_getpwclass(pwd
);
279 setusercontext(lc
, pwd
, pwd
->pw_uid
,
280 LOGIN_SETPATH
|LOGIN_SETUMASK
|LOGIN_SETENV
);
282 #endif /* !__APPLE__ */
283 setenv("USER", pwd
->pw_name
, 1);
284 setenv("SHELL", shell
, 1);
285 setenv("HOME", pwd
->pw_dir
, 1);
287 setenv("TERM", term
, 1);
289 setenv("KRBTKFILE", ticket
, 1);
291 if (asprintf(args
, "-%s", basename(shell
)) < 0)
304 shell
= pwd
->pw_shell
;
306 shell
= _PATH_BSHELL
;
307 execl(shell
, basename(shell
), (char *)NULL
);