]> git.saurik.com Git - apple/system_cmds.git/blob - newgrp.tproj/newgrp.c
system_cmds-433.8.tar.gz
[apple/system_cmds.git] / newgrp.tproj / newgrp.c
1 /*-
2 * Copyright (c) 2002 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * newgrp -- change to a new group
29 */
30
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 $");
33
34 #include <sys/types.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <grp.h>
39 #include <libgen.h>
40 #include <limits.h>
41 #ifndef __APPLE__
42 #include <login_cap.h>
43 #endif /* !__APPLE__ */
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #ifdef __APPLE__
50 #include <paths.h>
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);
58
59 static struct passwd *pwd;
60 static uid_t euid;
61
62 extern char **environ;
63
64 /* Manipulate effective user ID. */
65 #define PRIV_START do { \
66 if (seteuid(euid) < 0) \
67 err(1, "seteuid"); \
68 } while (0)
69 #define PRIV_END do { \
70 if (seteuid(getuid()) < 0) \
71 err(1, "seteuid"); \
72 } while (0)
73
74 int
75 main(int argc, char *argv[])
76 {
77 int ch, login;
78
79 euid = geteuid();
80 if (seteuid(getuid()) < 0)
81 err(1, "seteuid");
82
83 if ((pwd = getpwuid(getuid())) == NULL)
84 errx(1, "unknown user");
85
86 login = 0;
87 while ((ch = getopt(argc, argv, "-l")) != -1) {
88 switch (ch) {
89 case '-': /* Obsolescent */
90 case 'l':
91 login = 1;
92 break;
93 default:
94 usage();
95 }
96 }
97 argc -= optind;
98 argv += optind;
99
100 switch (argc) {
101 case 0:
102 restoregrps();
103 break;
104 case 1:
105 addgroup(*argv);
106 break;
107 default:
108 usage();
109 }
110
111 if (seteuid(euid) < 0)
112 err(1, "seteuid");
113 if (setuid(getuid()) < 0)
114 err(1, "setuid");
115
116 if (login)
117 loginshell();
118 else
119 doshell();
120
121 /*NOTREACHED*/
122 exit(1);
123 }
124
125 static void
126 usage(void)
127 {
128
129 fprintf(stderr, "usage: newgrp [-l] [group]\n");
130 exit(1);
131 }
132
133 static void
134 restoregrps(void)
135 {
136 int initres, setres;
137
138 PRIV_START;
139 initres = initgroups(pwd->pw_name, pwd->pw_gid);
140 setres = setgid(pwd->pw_gid);
141 PRIV_END;
142
143 if (initres < 0)
144 warn("initgroups");
145 if (setres < 0)
146 warn("setgroups");
147 }
148
149 static void
150 addgroup(const char *grpname)
151 {
152 gid_t grps[NGROUPS_MAX];
153 long lgid;
154 int dbmember, i, ngrps;
155 gid_t egid;
156 struct group *grp;
157 char *ep, *pass;
158 char **p;
159 char *grp_passwd;
160
161 egid = getegid();
162
163 /* Try it as a group name, then a group id. */
164 if ((grp = getgrnam(grpname)) == NULL)
165 if ((lgid = strtol(grpname, &ep, 10)) <= 0 || *ep != '\0' ||
166 (grp = getgrgid((gid_t)lgid)) == NULL ) {
167 warnx("%s: bad group name", grpname);
168 return;
169 }
170
171 /*
172 * If the user is not a member of the requested group and the group
173 * has a password, prompt and check it.
174 */
175 dbmember = 0;
176 if (pwd->pw_gid == grp->gr_gid)
177 dbmember = 1;
178 for (p = grp->gr_mem; *p != NULL; p++)
179 if (strcmp(*p, pwd->pw_name) == 0) {
180 dbmember = 1;
181 break;
182 }
183 grp_passwd = grp->gr_passwd;
184 if ((grp_passwd == NULL) || (grp_passwd[0] == '\0'))
185 grp_passwd = "*";
186 if (!dbmember && getuid() != 0) {
187 pass = getpass("Password:");
188 if (pass == NULL ||
189 strcmp(grp_passwd, crypt(pass, grp_passwd)) != 0) {
190 fprintf(stderr, "Sorry\n");
191 return;
192 }
193 }
194
195 if ((ngrps = getgroups(NGROUPS_MAX, (gid_t *)grps)) < 0) {
196 warn("getgroups");
197 return;
198 }
199
200 /* Remove requested gid from supp. list if it exists. */
201 if (grp->gr_gid != egid && inarray(grp->gr_gid, grps, ngrps)) {
202 for (i = 0; i < ngrps; i++)
203 if (grps[i] == grp->gr_gid)
204 break;
205 ngrps--;
206 memmove(&grps[i], &grps[i + 1], (ngrps - i) * sizeof(gid_t));
207 PRIV_START;
208 if (setgroups(ngrps, (const gid_t *)grps) < 0) {
209 PRIV_END;
210 warn("setgroups");
211 return;
212 }
213 PRIV_END;
214 }
215
216 PRIV_START;
217 if (setgid(grp->gr_gid)) {
218 PRIV_END;
219 warn("setgid");
220 return;
221 }
222 PRIV_END;
223 grps[0] = grp->gr_gid;
224
225 /* Add old effective gid to supp. list if it does not exist. */
226 if (egid != grp->gr_gid && !inarray(egid, grps, ngrps)) {
227 if (ngrps == NGROUPS_MAX)
228 warnx("too many groups");
229 else {
230 grps[ngrps++] = egid;
231 PRIV_START;
232 if (setgroups(ngrps, (const gid_t *)grps)) {
233 PRIV_END;
234 warn("setgroups");
235 return;
236 }
237 PRIV_END;
238 }
239 }
240
241 }
242
243 static int
244 inarray(gid_t gid, const gid_t grps[], int ngrps)
245 {
246 int i;
247
248 for (i = 0; i < ngrps; i++)
249 if (grps[i] == gid)
250 return (1);
251 return (0);
252 }
253
254 /*
255 * Set the environment to what would be expected if the user logged in
256 * again; this performs the same steps as su(1)'s -l option.
257 */
258 static void
259 loginshell(void)
260 {
261 char *args[2], **cleanenv, *term, *ticket;
262 const char *shell;
263 #ifndef __APPLE__
264 login_cap_t *lc;
265 #endif /* !__APPLE__ */
266 shell = pwd->pw_shell;
267 if (*shell == '\0')
268 shell = _PATH_BSHELL;
269 if (chdir(pwd->pw_dir) < 0) {
270 warn("%s", pwd->pw_dir);
271 chdir("/");
272 }
273
274 term = getenv("TERM");
275 ticket = getenv("KRBTKFILE");
276
277 if ((cleanenv = calloc(20, sizeof(char *))) == NULL)
278 err(1, "calloc");
279 *cleanenv = NULL;
280 environ = cleanenv;
281 #ifndef __APPLE__
282 lc = login_getpwclass(pwd);
283 setusercontext(lc, pwd, pwd->pw_uid,
284 LOGIN_SETPATH|LOGIN_SETUMASK|LOGIN_SETENV);
285 login_close(lc);
286 #endif /* !__APPLE__ */
287 setenv("USER", pwd->pw_name, 1);
288 setenv("SHELL", shell, 1);
289 setenv("HOME", pwd->pw_dir, 1);
290 if (term != NULL)
291 setenv("TERM", term, 1);
292 if (ticket != NULL)
293 setenv("KRBTKFILE", ticket, 1);
294
295 if (asprintf(args, "-%s", basename(shell)) < 0)
296 err(1, "asprintf");
297 args[1] = NULL;
298
299 execv(shell, args);
300 err(1, "%s", shell);
301 }
302
303 static void
304 doshell(void)
305 {
306 const char *shell;
307
308 shell = pwd->pw_shell;
309 if (*shell == '\0')
310 shell = _PATH_BSHELL;
311 execl(shell, basename(shell), (char *)NULL);
312 err(1, "%s", shell);
313 }