newgrp.c revision 1.5 1 1.1 ginsbach /*-
2 1.1 ginsbach * Copyright (c) 2007 The NetBSD Foundation, Inc.
3 1.1 ginsbach * All rights reserved.
4 1.1 ginsbach *
5 1.1 ginsbach * This code is derived from software contributed to The NetBSD Foundation
6 1.1 ginsbach * by Brian Ginsbach.
7 1.1 ginsbach *
8 1.1 ginsbach * Redistribution and use in source and binary forms, with or without
9 1.1 ginsbach * modification, are permitted provided that the following conditions
10 1.1 ginsbach * are met:
11 1.1 ginsbach * 1. Redistributions of source code must retain the above copyright
12 1.1 ginsbach * notice, this list of conditions and the following disclaimer.
13 1.1 ginsbach * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ginsbach * notice, this list of conditions and the following disclaimer in the
15 1.1 ginsbach * documentation and/or other materials provided with the distribution.
16 1.1 ginsbach * 3. All advertising materials mentioning features or use of this software
17 1.1 ginsbach * must display the following acknowledgement:
18 1.1 ginsbach * This product includes software developed by the NetBSD
19 1.1 ginsbach * Foundation, Inc. and its contributors.
20 1.1 ginsbach * 4. Neither the name of The NetBSD Foundation nor the names of its
21 1.1 ginsbach * contributors may be used to endorse or promote products derived
22 1.1 ginsbach * from this software without specific prior written permission.
23 1.1 ginsbach *
24 1.1 ginsbach * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.1 ginsbach * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.1 ginsbach * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.1 ginsbach * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.1 ginsbach * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1 ginsbach * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1 ginsbach * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.1 ginsbach * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.1 ginsbach * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.1 ginsbach * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.1 ginsbach * POSSIBILITY OF SUCH DAMAGE.
35 1.1 ginsbach */
36 1.1 ginsbach
37 1.1 ginsbach #include <sys/cdefs.h>
38 1.1 ginsbach
39 1.1 ginsbach #ifndef lint
40 1.5 christos __RCSID("$NetBSD: newgrp.c,v 1.5 2008/04/05 15:59:39 christos Exp $");
41 1.1 ginsbach #endif /* not lint */
42 1.1 ginsbach
43 1.1 ginsbach #include <sys/param.h>
44 1.1 ginsbach #include <sys/types.h>
45 1.1 ginsbach
46 1.1 ginsbach #include <err.h>
47 1.1 ginsbach #include <grp.h>
48 1.1 ginsbach #include <libgen.h>
49 1.1 ginsbach #include <paths.h>
50 1.1 ginsbach #include <pwd.h>
51 1.1 ginsbach #include <stdio.h>
52 1.1 ginsbach #include <stdlib.h>
53 1.1 ginsbach #include <string.h>
54 1.1 ginsbach #include <unistd.h>
55 1.1 ginsbach
56 1.1 ginsbach #ifdef LOGIN_CAP
57 1.1 ginsbach #include <login_cap.h>
58 1.1 ginsbach #endif
59 1.1 ginsbach
60 1.4 christos #include "grutil.h"
61 1.4 christos
62 1.4 christos static void
63 1.4 christos usage(void)
64 1.4 christos {
65 1.4 christos (void)fprintf(stderr, "usage: %s [-l] [group]\n", getprogname());
66 1.4 christos exit(EXIT_FAILURE);
67 1.4 christos }
68 1.1 ginsbach
69 1.1 ginsbach int
70 1.1 ginsbach main(int argc, char *argv[])
71 1.1 ginsbach {
72 1.1 ginsbach extern char **environ;
73 1.1 ginsbach struct passwd *pwd;
74 1.1 ginsbach int c, lflag;
75 1.1 ginsbach char *shell, sbuf[MAXPATHLEN + 2];
76 1.1 ginsbach uid_t uid;
77 1.1 ginsbach #ifdef LOGIN_CAP
78 1.1 ginsbach login_cap_t *lc;
79 1.1 ginsbach u_int flags = LOGIN_SETUSER;
80 1.1 ginsbach #endif
81 1.1 ginsbach
82 1.1 ginsbach uid = getuid();
83 1.1 ginsbach pwd = getpwuid(uid);
84 1.1 ginsbach if (pwd == NULL)
85 1.4 christos errx(EXIT_FAILURE, "who are you?");
86 1.1 ginsbach
87 1.1 ginsbach #ifdef LOGIN_CAP
88 1.1 ginsbach if ((lc = login_getclass(pwd->pw_class)) == NULL)
89 1.4 christos errx(EXIT_FAILURE, "%s: unknown login class", pwd->pw_class);
90 1.1 ginsbach #endif
91 1.1 ginsbach
92 1.5 christos (void)setprogname(argv[0]);
93 1.1 ginsbach lflag = 0;
94 1.1 ginsbach while ((c = getopt(argc, argv, "-l")) != -1) {
95 1.1 ginsbach switch (c) {
96 1.1 ginsbach case '-':
97 1.1 ginsbach case 'l':
98 1.1 ginsbach if (lflag)
99 1.1 ginsbach usage();
100 1.1 ginsbach lflag = 1;
101 1.1 ginsbach break;
102 1.1 ginsbach default:
103 1.1 ginsbach usage();
104 1.1 ginsbach break;
105 1.1 ginsbach }
106 1.1 ginsbach }
107 1.1 ginsbach
108 1.1 ginsbach argc -= optind;
109 1.1 ginsbach argv += optind;
110 1.1 ginsbach
111 1.1 ginsbach if (argc > 0) {
112 1.4 christos #if 0
113 1.1 ginsbach pwd->pw_gid = newgrp(*argv, pwd);
114 1.1 ginsbach addgrp(pwd->pw_gid);
115 1.1 ginsbach if (setgid(pwd->pw_gid) < 0)
116 1.1 ginsbach err(1, "setgid");
117 1.4 christos #endif
118 1.4 christos #ifdef LOGIN_CAP
119 1.4 christos addgroup(lc, *argv, pwd, getuid(), "Password:");
120 1.4 christos #else
121 1.4 christos addgroup(*argv, pwd, getuid(), "Password:");
122 1.4 christos #endif
123 1.1 ginsbach } else {
124 1.1 ginsbach #ifdef LOGIN_CAP
125 1.1 ginsbach flags |= LOGIN_SETGROUP;
126 1.1 ginsbach #else
127 1.4 christos if (initgroups(pwd->pw_name, pwd->pw_gid) == -1)
128 1.4 christos err(EXIT_FAILURE, "initgroups");
129 1.4 christos if (setgid(pwd->pw_gid) == -1)
130 1.4 christos err(EXIT_FAILURE, "setgid");
131 1.1 ginsbach #endif
132 1.1 ginsbach }
133 1.1 ginsbach
134 1.1 ginsbach #ifdef LOGIN_CAP
135 1.4 christos if (setusercontext(lc, pwd, uid, flags) == -1)
136 1.4 christos err(EXIT_FAILURE, "setusercontext");
137 1.1 ginsbach if (!lflag)
138 1.1 ginsbach login_close(lc);
139 1.1 ginsbach #else
140 1.4 christos if (setuid(pwd->pw_uid) == -1)
141 1.4 christos err(EXIT_FAILURE, "setuid");
142 1.1 ginsbach #endif
143 1.1 ginsbach
144 1.1 ginsbach if (*pwd->pw_shell == '\0') {
145 1.1 ginsbach #ifdef TRUST_ENV_SHELL
146 1.1 ginsbach shell = getenv("SHELL");
147 1.1 ginsbach if (shell != NULL)
148 1.1 ginsbach pwd->pw_shell = shell;
149 1.1 ginsbach else
150 1.1 ginsbach #endif
151 1.4 christos pwd->pw_shell = __UNCONST(_PATH_BSHELL);
152 1.1 ginsbach }
153 1.1 ginsbach
154 1.1 ginsbach shell = pwd->pw_shell;
155 1.1 ginsbach
156 1.1 ginsbach if (lflag) {
157 1.1 ginsbach char *term;
158 1.1 ginsbach #ifdef KERBEROS
159 1.1 ginsbach char *krbtkfile;
160 1.1 ginsbach #endif
161 1.1 ginsbach
162 1.4 christos if (chdir(pwd->pw_dir) == -1)
163 1.1 ginsbach warn("%s", pwd->pw_dir);
164 1.1 ginsbach
165 1.1 ginsbach term = getenv("TERM");
166 1.1 ginsbach #ifdef KERBEROS
167 1.1 ginsbach krbtkfile = getenv("KRBTKFILE");
168 1.1 ginsbach #endif
169 1.1 ginsbach
170 1.1 ginsbach /* create an empty environment */
171 1.1 ginsbach if ((environ = malloc(sizeof(char *))) == NULL)
172 1.4 christos err(EXIT_FAILURE, NULL);
173 1.1 ginsbach environ[0] = NULL;
174 1.1 ginsbach #ifdef LOGIN_CAP
175 1.4 christos if (setusercontext(lc, pwd, uid, LOGIN_SETENV | LOGIN_SETPATH) == -1)
176 1.4 christos err(EXIT_FAILURE, "setusercontext");
177 1.1 ginsbach login_close(lc);
178 1.1 ginsbach #else
179 1.1 ginsbach (void)setenv("PATH", _PATH_DEFPATH, 1);
180 1.1 ginsbach #endif
181 1.1 ginsbach if (term != NULL)
182 1.1 ginsbach (void)setenv("TERM", term, 1);
183 1.1 ginsbach #ifdef KERBEROS
184 1.1 ginsbach if (krbtkfile != NULL)
185 1.1 ginsbach (void)setenv("KRBTKFILE", krbtkfile, 1);
186 1.1 ginsbach #endif
187 1.1 ginsbach
188 1.1 ginsbach (void)setenv("LOGNAME", pwd->pw_name, 1);
189 1.1 ginsbach (void)setenv("USER", pwd->pw_name, 1);
190 1.1 ginsbach (void)setenv("HOME", pwd->pw_dir, 1);
191 1.1 ginsbach (void)setenv("SHELL", pwd->pw_shell, 1);
192 1.1 ginsbach
193 1.1 ginsbach sbuf[0] = '-';
194 1.1 ginsbach (void)strlcpy(sbuf + 1, basename(pwd->pw_shell),
195 1.1 ginsbach sizeof(sbuf) - 1);
196 1.1 ginsbach shell = sbuf;
197 1.1 ginsbach }
198 1.1 ginsbach
199 1.4 christos (void)execl(pwd->pw_shell, shell, NULL);
200 1.4 christos err(EXIT_FAILURE, "%s", pwd->pw_shell);
201 1.4 christos /* NOTREACHED */
202 1.1 ginsbach }
203