newgrp.c revision 1.4 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.4 christos __RCSID("$NetBSD: newgrp.c,v 1.4 2007/10/17 21:07:49 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.1 ginsbach lflag = 0;
93 1.1 ginsbach while ((c = getopt(argc, argv, "-l")) != -1) {
94 1.1 ginsbach switch (c) {
95 1.1 ginsbach case '-':
96 1.1 ginsbach case 'l':
97 1.1 ginsbach if (lflag)
98 1.1 ginsbach usage();
99 1.1 ginsbach lflag = 1;
100 1.1 ginsbach break;
101 1.1 ginsbach default:
102 1.1 ginsbach usage();
103 1.1 ginsbach break;
104 1.1 ginsbach }
105 1.1 ginsbach }
106 1.1 ginsbach
107 1.1 ginsbach argc -= optind;
108 1.1 ginsbach argv += optind;
109 1.1 ginsbach
110 1.1 ginsbach if (argc > 0) {
111 1.4 christos #if 0
112 1.1 ginsbach pwd->pw_gid = newgrp(*argv, pwd);
113 1.1 ginsbach addgrp(pwd->pw_gid);
114 1.1 ginsbach if (setgid(pwd->pw_gid) < 0)
115 1.1 ginsbach err(1, "setgid");
116 1.4 christos #endif
117 1.4 christos #ifdef LOGIN_CAP
118 1.4 christos addgroup(lc, *argv, pwd, getuid(), "Password:");
119 1.4 christos #else
120 1.4 christos addgroup(*argv, pwd, getuid(), "Password:");
121 1.4 christos #endif
122 1.1 ginsbach } else {
123 1.1 ginsbach #ifdef LOGIN_CAP
124 1.1 ginsbach flags |= LOGIN_SETGROUP;
125 1.1 ginsbach #else
126 1.4 christos if (initgroups(pwd->pw_name, pwd->pw_gid) == -1)
127 1.4 christos err(EXIT_FAILURE, "initgroups");
128 1.4 christos if (setgid(pwd->pw_gid) == -1)
129 1.4 christos err(EXIT_FAILURE, "setgid");
130 1.1 ginsbach #endif
131 1.1 ginsbach }
132 1.1 ginsbach
133 1.1 ginsbach #ifdef LOGIN_CAP
134 1.4 christos if (setusercontext(lc, pwd, uid, flags) == -1)
135 1.4 christos err(EXIT_FAILURE, "setusercontext");
136 1.1 ginsbach if (!lflag)
137 1.1 ginsbach login_close(lc);
138 1.1 ginsbach #else
139 1.4 christos if (setuid(pwd->pw_uid) == -1)
140 1.4 christos err(EXIT_FAILURE, "setuid");
141 1.1 ginsbach #endif
142 1.1 ginsbach
143 1.1 ginsbach if (*pwd->pw_shell == '\0') {
144 1.1 ginsbach #ifdef TRUST_ENV_SHELL
145 1.1 ginsbach shell = getenv("SHELL");
146 1.1 ginsbach if (shell != NULL)
147 1.1 ginsbach pwd->pw_shell = shell;
148 1.1 ginsbach else
149 1.1 ginsbach #endif
150 1.4 christos pwd->pw_shell = __UNCONST(_PATH_BSHELL);
151 1.1 ginsbach }
152 1.1 ginsbach
153 1.1 ginsbach shell = pwd->pw_shell;
154 1.1 ginsbach
155 1.1 ginsbach if (lflag) {
156 1.1 ginsbach char *term;
157 1.1 ginsbach #ifdef KERBEROS
158 1.1 ginsbach char *krbtkfile;
159 1.1 ginsbach #endif
160 1.1 ginsbach
161 1.4 christos if (chdir(pwd->pw_dir) == -1)
162 1.1 ginsbach warn("%s", pwd->pw_dir);
163 1.1 ginsbach
164 1.1 ginsbach term = getenv("TERM");
165 1.1 ginsbach #ifdef KERBEROS
166 1.1 ginsbach krbtkfile = getenv("KRBTKFILE");
167 1.1 ginsbach #endif
168 1.1 ginsbach
169 1.1 ginsbach /* create an empty environment */
170 1.1 ginsbach if ((environ = malloc(sizeof(char *))) == NULL)
171 1.4 christos err(EXIT_FAILURE, NULL);
172 1.1 ginsbach environ[0] = NULL;
173 1.1 ginsbach #ifdef LOGIN_CAP
174 1.4 christos if (setusercontext(lc, pwd, uid, LOGIN_SETENV | LOGIN_SETPATH) == -1)
175 1.4 christos err(EXIT_FAILURE, "setusercontext");
176 1.1 ginsbach login_close(lc);
177 1.1 ginsbach #else
178 1.1 ginsbach (void)setenv("PATH", _PATH_DEFPATH, 1);
179 1.1 ginsbach #endif
180 1.1 ginsbach if (term != NULL)
181 1.1 ginsbach (void)setenv("TERM", term, 1);
182 1.1 ginsbach #ifdef KERBEROS
183 1.1 ginsbach if (krbtkfile != NULL)
184 1.1 ginsbach (void)setenv("KRBTKFILE", krbtkfile, 1);
185 1.1 ginsbach #endif
186 1.1 ginsbach
187 1.1 ginsbach (void)setenv("LOGNAME", pwd->pw_name, 1);
188 1.1 ginsbach (void)setenv("USER", pwd->pw_name, 1);
189 1.1 ginsbach (void)setenv("HOME", pwd->pw_dir, 1);
190 1.1 ginsbach (void)setenv("SHELL", pwd->pw_shell, 1);
191 1.1 ginsbach
192 1.1 ginsbach sbuf[0] = '-';
193 1.1 ginsbach (void)strlcpy(sbuf + 1, basename(pwd->pw_shell),
194 1.1 ginsbach sizeof(sbuf) - 1);
195 1.1 ginsbach shell = sbuf;
196 1.1 ginsbach }
197 1.1 ginsbach
198 1.4 christos (void)execl(pwd->pw_shell, shell, NULL);
199 1.4 christos err(EXIT_FAILURE, "%s", pwd->pw_shell);
200 1.4 christos /* NOTREACHED */
201 1.1 ginsbach }
202