su.c revision 1.6 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1988 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.6 mycroft /*static char sccsid[] = "from: @(#)su.c 5.26 (Berkeley) 7/6/91";*/
42 1.6 mycroft static char rcsid[] = "$Id: su.c,v 1.6 1993/08/01 18:08:12 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/time.h>
47 1.1 cgd #include <sys/resource.h>
48 1.1 cgd #include <syslog.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <pwd.h>
51 1.1 cgd #include <grp.h>
52 1.1 cgd #include <string.h>
53 1.1 cgd #include <unistd.h>
54 1.1 cgd #include <paths.h>
55 1.1 cgd
56 1.1 cgd #ifdef KERBEROS
57 1.1 cgd #include <kerberosIV/des.h>
58 1.1 cgd #include <kerberosIV/krb.h>
59 1.1 cgd #include <netdb.h>
60 1.1 cgd
61 1.1 cgd #define ARGSTR "-Kflm"
62 1.1 cgd
63 1.1 cgd int use_kerberos = 1;
64 1.1 cgd #else
65 1.1 cgd #define ARGSTR "-flm"
66 1.1 cgd #endif
67 1.1 cgd
68 1.2 sef extern char *crypt();
69 1.2 sef
70 1.1 cgd main(argc, argv)
71 1.1 cgd int argc;
72 1.1 cgd char **argv;
73 1.1 cgd {
74 1.1 cgd extern char **environ;
75 1.1 cgd extern int errno, optind;
76 1.1 cgd register struct passwd *pwd;
77 1.1 cgd register char *p, **g;
78 1.1 cgd struct group *gr;
79 1.1 cgd uid_t ruid, getuid();
80 1.1 cgd int asme, ch, asthem, fastlogin, prio;
81 1.1 cgd enum { UNSET, YES, NO } iscsh = UNSET;
82 1.2 sef char *user, *shell, *username, *cleanenv[2], **np;
83 1.1 cgd char shellbuf[MAXPATHLEN];
84 1.2 sef char *getpass(), *getenv(), *getlogin(), *ontty();
85 1.1 cgd
86 1.1 cgd asme = asthem = fastlogin = 0;
87 1.1 cgd while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
88 1.1 cgd switch((char)ch) {
89 1.1 cgd #ifdef KERBEROS
90 1.1 cgd case 'K':
91 1.1 cgd use_kerberos = 0;
92 1.1 cgd break;
93 1.1 cgd #endif
94 1.1 cgd case 'f':
95 1.1 cgd fastlogin = 1;
96 1.1 cgd break;
97 1.1 cgd case '-':
98 1.1 cgd case 'l':
99 1.1 cgd asme = 0;
100 1.1 cgd asthem = 1;
101 1.1 cgd break;
102 1.1 cgd case 'm':
103 1.1 cgd asme = 1;
104 1.1 cgd asthem = 0;
105 1.1 cgd break;
106 1.1 cgd case '?':
107 1.1 cgd default:
108 1.1 cgd (void)fprintf(stderr, "usage: su [%s] [login]\n",
109 1.1 cgd ARGSTR);
110 1.1 cgd exit(1);
111 1.1 cgd }
112 1.1 cgd argv += optind;
113 1.1 cgd
114 1.1 cgd errno = 0;
115 1.1 cgd prio = getpriority(PRIO_PROCESS, 0);
116 1.1 cgd if (errno)
117 1.1 cgd prio = 0;
118 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
119 1.1 cgd openlog("su", LOG_CONS, 0);
120 1.1 cgd
121 1.1 cgd /* get current login name and shell */
122 1.1 cgd ruid = getuid();
123 1.1 cgd username = getlogin();
124 1.1 cgd if (username == NULL || (pwd = getpwnam(username)) == NULL ||
125 1.1 cgd pwd->pw_uid != ruid)
126 1.1 cgd pwd = getpwuid(ruid);
127 1.1 cgd if (pwd == NULL) {
128 1.1 cgd fprintf(stderr, "su: who are you?\n");
129 1.1 cgd exit(1);
130 1.1 cgd }
131 1.1 cgd username = strdup(pwd->pw_name);
132 1.1 cgd if (asme)
133 1.1 cgd if (pwd->pw_shell && *pwd->pw_shell)
134 1.1 cgd shell = strcpy(shellbuf, pwd->pw_shell);
135 1.1 cgd else {
136 1.1 cgd shell = _PATH_BSHELL;
137 1.1 cgd iscsh = NO;
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd /* get target login information, default to root */
141 1.1 cgd user = *argv ? *argv : "root";
142 1.2 sef np = *argv ? argv : argv-1;
143 1.2 sef
144 1.1 cgd if ((pwd = getpwnam(user)) == NULL) {
145 1.1 cgd fprintf(stderr, "su: unknown login %s\n", user);
146 1.1 cgd exit(1);
147 1.1 cgd }
148 1.1 cgd
149 1.1 cgd if (ruid) {
150 1.1 cgd #ifdef KERBEROS
151 1.1 cgd if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
152 1.1 cgd #endif
153 1.1 cgd {
154 1.1 cgd /* only allow those in group zero to su to root. */
155 1.1 cgd if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
156 1.1 cgd for (g = gr->gr_mem;; ++g) {
157 1.1 cgd if (!*g) {
158 1.1 cgd (void)fprintf(stderr,
159 1.1 cgd "su: you are not in the correct group to su %s.\n",
160 1.1 cgd user);
161 1.1 cgd exit(1);
162 1.1 cgd }
163 1.1 cgd if (!strcmp(username, *g))
164 1.1 cgd break;
165 1.1 cgd }
166 1.1 cgd /* if target requires a password, verify it */
167 1.1 cgd if (*pwd->pw_passwd) {
168 1.1 cgd p = getpass("Password:");
169 1.1 cgd if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
170 1.1 cgd fprintf(stderr, "Sorry\n");
171 1.1 cgd syslog(LOG_AUTH|LOG_WARNING,
172 1.1 cgd "BAD SU %s to %s%s", username,
173 1.1 cgd user, ontty());
174 1.1 cgd exit(1);
175 1.1 cgd }
176 1.1 cgd }
177 1.1 cgd }
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd if (asme) {
181 1.1 cgd /* if asme and non-standard target shell, must be root */
182 1.1 cgd if (!chshell(pwd->pw_shell) && ruid) {
183 1.1 cgd (void)fprintf(stderr,
184 1.1 cgd "su: permission denied (shell).\n");
185 1.1 cgd exit(1);
186 1.1 cgd }
187 1.1 cgd } else if (pwd->pw_shell && *pwd->pw_shell) {
188 1.1 cgd shell = pwd->pw_shell;
189 1.1 cgd iscsh = UNSET;
190 1.1 cgd } else {
191 1.1 cgd shell = _PATH_BSHELL;
192 1.1 cgd iscsh = NO;
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd /* if we're forking a csh, we want to slightly muck the args */
196 1.1 cgd if (iscsh == UNSET) {
197 1.1 cgd if (p = rindex(shell, '/'))
198 1.1 cgd ++p;
199 1.1 cgd else
200 1.1 cgd p = shell;
201 1.1 cgd iscsh = strcmp(p, "csh") ? NO : YES;
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd /* set permissions */
205 1.1 cgd if (setgid(pwd->pw_gid) < 0) {
206 1.1 cgd perror("su: setgid");
207 1.1 cgd exit(1);
208 1.1 cgd }
209 1.1 cgd if (initgroups(user, pwd->pw_gid)) {
210 1.1 cgd (void)fprintf(stderr, "su: initgroups failed.\n");
211 1.1 cgd exit(1);
212 1.1 cgd }
213 1.1 cgd if (setuid(pwd->pw_uid) < 0) {
214 1.1 cgd perror("su: setuid");
215 1.1 cgd exit(1);
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd if (!asme) {
219 1.1 cgd if (asthem) {
220 1.1 cgd p = getenv("TERM");
221 1.1 cgd cleanenv[0] = _PATH_DEFPATH;
222 1.1 cgd cleanenv[1] = NULL;
223 1.1 cgd environ = cleanenv;
224 1.1 cgd (void)setenv("TERM", p, 1);
225 1.1 cgd if (chdir(pwd->pw_dir) < 0) {
226 1.1 cgd fprintf(stderr, "su: no directory\n");
227 1.1 cgd exit(1);
228 1.1 cgd }
229 1.1 cgd }
230 1.5 jtc if (asthem || pwd->pw_uid)
231 1.1 cgd (void)setenv("USER", pwd->pw_name, 1);
232 1.1 cgd (void)setenv("HOME", pwd->pw_dir, 1);
233 1.1 cgd (void)setenv("SHELL", shell, 1);
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd if (iscsh == YES) {
237 1.1 cgd if (fastlogin)
238 1.1 cgd *np-- = "-f";
239 1.1 cgd if (asme)
240 1.1 cgd *np-- = "-m";
241 1.1 cgd }
242 1.1 cgd
243 1.1 cgd /* csh strips the first character... */
244 1.1 cgd *np = asthem ? "-su" : iscsh == YES ? "_su" : "su";
245 1.1 cgd
246 1.1 cgd if (ruid != 0)
247 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
248 1.1 cgd username, user, ontty());
249 1.1 cgd
250 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, prio);
251 1.1 cgd
252 1.1 cgd execv(shell, np);
253 1.1 cgd (void)fprintf(stderr, "su: %s not found.\n", shell);
254 1.1 cgd exit(1);
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd chshell(sh)
258 1.1 cgd char *sh;
259 1.1 cgd {
260 1.1 cgd register char *cp;
261 1.1 cgd char *getusershell();
262 1.1 cgd
263 1.1 cgd while ((cp = getusershell()) != NULL)
264 1.1 cgd if (!strcmp(cp, sh))
265 1.1 cgd return (1);
266 1.1 cgd return (0);
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd char *
270 1.1 cgd ontty()
271 1.1 cgd {
272 1.1 cgd char *p, *ttyname();
273 1.1 cgd static char buf[MAXPATHLEN + 4];
274 1.1 cgd
275 1.1 cgd buf[0] = 0;
276 1.1 cgd if (p = ttyname(STDERR_FILENO))
277 1.1 cgd sprintf(buf, " on %s", p);
278 1.1 cgd return (buf);
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd #ifdef KERBEROS
282 1.1 cgd kerberos(username, user, uid)
283 1.1 cgd char *username, *user;
284 1.1 cgd int uid;
285 1.1 cgd {
286 1.1 cgd extern char *krb_err_txt[];
287 1.1 cgd KTEXT_ST ticket;
288 1.1 cgd AUTH_DAT authdata;
289 1.1 cgd struct hostent *hp;
290 1.1 cgd register char *p;
291 1.1 cgd int kerno;
292 1.1 cgd u_long faddr;
293 1.1 cgd char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
294 1.1 cgd char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
295 1.1 cgd char *ontty(), *krb_get_phost();
296 1.1 cgd
297 1.1 cgd if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
298 1.1 cgd return (1);
299 1.1 cgd if (koktologin(username, lrealm, user) && !uid) {
300 1.1 cgd (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
301 1.1 cgd return (1);
302 1.1 cgd }
303 1.1 cgd (void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
304 1.1 cgd
305 1.1 cgd (void)setenv("KRBTKFILE", krbtkfile, 1);
306 1.1 cgd (void)krb_set_tkt_string(krbtkfile);
307 1.1 cgd /*
308 1.1 cgd * Set real as well as effective ID to 0 for the moment,
309 1.1 cgd * to make the kerberos library do the right thing.
310 1.1 cgd */
311 1.1 cgd if (setuid(0) < 0) {
312 1.1 cgd perror("su: setuid");
313 1.1 cgd return (1);
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * Little trick here -- if we are su'ing to root,
318 1.1 cgd * we need to get a ticket for "xxx.root", where xxx represents
319 1.1 cgd * the name of the person su'ing. Otherwise (non-root case),
320 1.1 cgd * we need to get a ticket for "yyy.", where yyy represents
321 1.1 cgd * the name of the person being su'd to, and the instance is null
322 1.1 cgd *
323 1.1 cgd * We should have a way to set the ticket lifetime,
324 1.1 cgd * with a system default for root.
325 1.1 cgd */
326 1.1 cgd kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
327 1.1 cgd (uid == 0 ? "root" : ""), lrealm,
328 1.1 cgd "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
329 1.1 cgd
330 1.1 cgd if (kerno != KSUCCESS) {
331 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
332 1.1 cgd fprintf(stderr, "principal unknown: %s.%s@%s\n",
333 1.1 cgd (uid == 0 ? username : user),
334 1.1 cgd (uid == 0 ? "root" : ""), lrealm);
335 1.1 cgd return (1);
336 1.1 cgd }
337 1.1 cgd (void)fprintf(stderr, "su: unable to su: %s\n",
338 1.1 cgd krb_err_txt[kerno]);
339 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
340 1.1 cgd "BAD Kerberos SU: %s to %s%s: %s",
341 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
342 1.1 cgd return (1);
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd if (chown(krbtkfile, uid, -1) < 0) {
346 1.1 cgd perror("su: chown:");
347 1.1 cgd (void)unlink(krbtkfile);
348 1.1 cgd return (1);
349 1.1 cgd }
350 1.1 cgd
351 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
352 1.1 cgd
353 1.1 cgd if (gethostname(hostname, sizeof(hostname)) == -1) {
354 1.1 cgd perror("su: gethostname");
355 1.1 cgd dest_tkt();
356 1.1 cgd return (1);
357 1.1 cgd }
358 1.1 cgd
359 1.1 cgd (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
360 1.1 cgd savehost[sizeof(savehost) - 1] = '\0';
361 1.1 cgd
362 1.1 cgd kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
363 1.1 cgd
364 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
365 1.1 cgd (void)fprintf(stderr, "Warning: TGT not verified.\n");
366 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
367 1.1 cgd "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
368 1.1 cgd username, user, ontty(), krb_err_txt[kerno],
369 1.1 cgd "rcmd", savehost);
370 1.1 cgd } else if (kerno != KSUCCESS) {
371 1.1 cgd (void)fprintf(stderr, "Unable to use TGT: %s\n",
372 1.1 cgd krb_err_txt[kerno]);
373 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
374 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
375 1.1 cgd dest_tkt();
376 1.1 cgd return (1);
377 1.1 cgd } else {
378 1.1 cgd if (!(hp = gethostbyname(hostname))) {
379 1.1 cgd (void)fprintf(stderr, "su: can't get addr of %s\n",
380 1.1 cgd hostname);
381 1.1 cgd dest_tkt();
382 1.1 cgd return (1);
383 1.1 cgd }
384 1.1 cgd (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
385 1.1 cgd
386 1.1 cgd if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
387 1.1 cgd &authdata, "")) != KSUCCESS) {
388 1.1 cgd (void)fprintf(stderr,
389 1.1 cgd "su: unable to verify rcmd ticket: %s\n",
390 1.1 cgd krb_err_txt[kerno]);
391 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
392 1.1 cgd "failed su: %s to %s%s: %s", username,
393 1.1 cgd user, ontty(), krb_err_txt[kerno]);
394 1.1 cgd dest_tkt();
395 1.1 cgd return (1);
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd return (0);
399 1.1 cgd }
400 1.1 cgd
401 1.1 cgd koktologin(name, realm, toname)
402 1.1 cgd char *name, *realm, *toname;
403 1.1 cgd {
404 1.1 cgd register AUTH_DAT *kdata;
405 1.1 cgd AUTH_DAT kdata_st;
406 1.1 cgd
407 1.1 cgd kdata = &kdata_st;
408 1.1 cgd bzero((caddr_t) kdata, sizeof(*kdata));
409 1.1 cgd (void)strcpy(kdata->pname, name);
410 1.1 cgd (void)strcpy(kdata->pinst,
411 1.1 cgd ((strcmp(toname, "root") == 0) ? "root" : ""));
412 1.1 cgd (void)strcpy(kdata->prealm, realm);
413 1.1 cgd return (kuserok(kdata, toname));
414 1.1 cgd }
415 1.1 cgd #endif
416