]>
git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/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.5 2009/12/13 03:14:06 delphij Exp $");
34 #include <sys/types.h>
42 #include <login_cap.h>
43 #endif /* !__APPLE__ */
45 #include <membership.h>
46 #endif /* __APPLE__ */
54 #endif /* __APPLE__ */
56 #include <libiosexec.h>
58 static void addgroup(const char *grpname
);
59 static void doshell(void);
60 static int inarray(gid_t
, const gid_t
[], int);
61 static void loginshell(void);
62 static void restoregrps(void);
63 static void usage(void);
65 static struct passwd
*pwd
;
68 extern char **environ
;
70 /* Manipulate effective user ID. */
71 #define PRIV_START do { \
72 if (seteuid(euid) < 0) \
75 #define PRIV_END do { \
76 if (seteuid(getuid()) < 0) \
81 main(int argc
, char *argv
[])
86 if (seteuid(getuid()) < 0)
89 if ((pwd
= getpwuid(getuid())) == NULL
)
90 errx(1, "unknown user");
93 while ((ch
= getopt(argc
, argv
, "-l")) != -1) {
95 case '-': /* Obsolescent */
117 if (seteuid(euid
) < 0)
119 if (setuid(getuid()) < 0)
135 fprintf(stderr
, "usage: newgrp [-l] [group]\n");
145 initres
= initgroups(pwd
->pw_name
, pwd
->pw_gid
);
146 setres
= setgid(pwd
->pw_gid
);
156 addgroup(const char *grpname
)
159 long lgid
, ngrps_max
;
160 int dbmember
, i
, ngrps
;
176 /* Try it as a group name, then a group id. */
177 if ((grp
= getgrnam(grpname
)) == NULL
)
178 if ((lgid
= strtol(grpname
, &ep
, 10)) <= 0 || *ep
!= '\0' ||
179 (grp
= getgrgid((gid_t
)lgid
)) == NULL
) {
180 warnx("%s: bad group name", grpname
);
185 status
= mbr_uid_to_uuid(pwd
->pw_uid
, user_uuid
);
187 errc(1, status
, "mbr_uid_to_uuid");
189 status
= mbr_gid_to_uuid(grp
->gr_gid
, group_uuid
);
191 errc(1, status
, "mbr_gid_to_uuid");
193 status
= mbr_check_membership(user_uuid
, group_uuid
, &dbmember
);
195 errc(1, status
, "mbr_check_membership");
198 * If the user is not a member of the requested group and the group
199 * has a password, prompt and check it.
202 if (pwd
->pw_gid
== grp
->gr_gid
)
204 for (p
= grp
->gr_mem
; *p
!= NULL
; p
++)
205 if (strcmp(*p
, pwd
->pw_name
) == 0) {
211 grp_passwd
= grp
->gr_passwd
;
212 if ((grp_passwd
== NULL
) || (grp_passwd
[0] == '\0'))
214 if (!dbmember
&& getuid() != 0) {
215 pass
= getpass("Password:");
217 strcmp(grp_passwd
, crypt(pass
, grp_passwd
)) != 0) {
218 fprintf(stderr
, "Sorry\n");
223 ngrps_max
= sysconf(_SC_NGROUPS_MAX
) + 1;
224 if ((grps
= malloc(sizeof(gid_t
) * ngrps_max
)) == NULL
)
226 if ((ngrps
= getgroups((int)ngrps_max
, (gid_t
*)grps
)) < 0) {
231 /* Remove requested gid from supp. list if it exists. */
232 if (grp
->gr_gid
!= egid
&& inarray(grp
->gr_gid
, grps
, ngrps
)) {
233 for (i
= 0; i
< ngrps
; i
++)
234 if (grps
[i
] == grp
->gr_gid
)
237 memmove(&grps
[i
], &grps
[i
+ 1], (ngrps
- i
) * sizeof(gid_t
));
239 if (setgroups(ngrps
, (const gid_t
*)grps
) < 0) {
248 if (setgid(grp
->gr_gid
)) {
254 grps
[0] = grp
->gr_gid
;
256 /* Add old effective gid to supp. list if it does not exist. */
257 if (egid
!= grp
->gr_gid
&& !inarray(egid
, grps
, ngrps
)) {
258 if (ngrps
+ 1 >= ngrps_max
)
259 warnx("too many groups");
261 grps
[ngrps
++] = egid
;
263 if (setgroups(ngrps
, (const gid_t
*)grps
)) {
277 inarray(gid_t gid
, const gid_t grps
[], int ngrps
)
281 for (i
= 0; i
< ngrps
; i
++)
288 * Set the environment to what would be expected if the user logged in
289 * again; this performs the same steps as su(1)'s -l option.
294 char *args
[2], **cleanenv
, *term
, *ticket
;
296 char *prog
, progbuf
[PATH_MAX
];
299 #endif /* !__APPLE__ */
300 shell
= pwd
->pw_shell
;
302 shell
= _PATH_BSHELL
;
303 if (chdir(pwd
->pw_dir
) < 0) {
304 warn("%s", pwd
->pw_dir
);
308 term
= getenv("TERM");
309 ticket
= getenv("KRBTKFILE");
311 if ((cleanenv
= calloc(20, sizeof(char *))) == NULL
)
316 lc
= login_getpwclass(pwd
);
317 setusercontext(lc
, pwd
, pwd
->pw_uid
,
318 LOGIN_SETPATH
|LOGIN_SETUMASK
|LOGIN_SETENV
);
320 #endif /* !__APPLE__ */
321 setenv("USER", pwd
->pw_name
, 1);
322 setenv("SHELL", shell
, 1);
323 setenv("HOME", pwd
->pw_dir
, 1);
325 setenv("TERM", term
, 1);
327 setenv("KRBTKFILE", ticket
, 1);
329 strlcpy(progbuf
, shell
, sizeof(progbuf
));
330 prog
= basename(progbuf
);
332 if (asprintf(args
, "-%s", prog
) < 0)
344 char *prog
, progbuf
[PATH_MAX
];
346 shell
= pwd
->pw_shell
;
348 shell
= _PATH_BSHELL
;
350 strlcpy(progbuf
, shell
, sizeof(progbuf
));
351 prog
= basename(progbuf
);
353 execl(shell
, prog
, (char *)NULL
);