su.c revision 1.2 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.1 cgd static char sccsid[] = "@(#)su.c 5.26 (Berkeley) 7/6/91";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/time.h>
46 1.1 cgd #include <sys/resource.h>
47 1.1 cgd #include <syslog.h>
48 1.1 cgd #include <stdio.h>
49 1.1 cgd #include <pwd.h>
50 1.1 cgd #include <grp.h>
51 1.1 cgd #include <string.h>
52 1.1 cgd #include <unistd.h>
53 1.1 cgd #include <paths.h>
54 1.1 cgd
55 1.1 cgd #ifdef KERBEROS
56 1.1 cgd #include <kerberosIV/des.h>
57 1.1 cgd #include <kerberosIV/krb.h>
58 1.1 cgd #include <netdb.h>
59 1.1 cgd
60 1.1 cgd #define ARGSTR "-Kflm"
61 1.1 cgd
62 1.1 cgd int use_kerberos = 1;
63 1.1 cgd #else
64 1.1 cgd #define ARGSTR "-flm"
65 1.1 cgd #endif
66 1.1 cgd
67 1.2 sef #ifdef DES
68 1.2 sef extern char *crypt();
69 1.2 sef #else
70 1.2 sef # define crypt(a,b) (a)
71 1.2 sef #endif
72 1.2 sef
73 1.1 cgd main(argc, argv)
74 1.1 cgd int argc;
75 1.1 cgd char **argv;
76 1.1 cgd {
77 1.1 cgd extern char **environ;
78 1.1 cgd extern int errno, optind;
79 1.1 cgd register struct passwd *pwd;
80 1.1 cgd register char *p, **g;
81 1.1 cgd struct group *gr;
82 1.1 cgd uid_t ruid, getuid();
83 1.1 cgd int asme, ch, asthem, fastlogin, prio;
84 1.1 cgd enum { UNSET, YES, NO } iscsh = UNSET;
85 1.2 sef char *user, *shell, *username, *cleanenv[2], **np;
86 1.1 cgd char shellbuf[MAXPATHLEN];
87 1.2 sef char *getpass(), *getenv(), *getlogin(), *ontty();
88 1.1 cgd
89 1.1 cgd asme = asthem = fastlogin = 0;
90 1.1 cgd while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
91 1.1 cgd switch((char)ch) {
92 1.1 cgd #ifdef KERBEROS
93 1.1 cgd case 'K':
94 1.1 cgd use_kerberos = 0;
95 1.1 cgd break;
96 1.1 cgd #endif
97 1.1 cgd case 'f':
98 1.1 cgd fastlogin = 1;
99 1.1 cgd break;
100 1.1 cgd case '-':
101 1.1 cgd case 'l':
102 1.1 cgd asme = 0;
103 1.1 cgd asthem = 1;
104 1.1 cgd break;
105 1.1 cgd case 'm':
106 1.1 cgd asme = 1;
107 1.1 cgd asthem = 0;
108 1.1 cgd break;
109 1.1 cgd case '?':
110 1.1 cgd default:
111 1.1 cgd (void)fprintf(stderr, "usage: su [%s] [login]\n",
112 1.1 cgd ARGSTR);
113 1.1 cgd exit(1);
114 1.1 cgd }
115 1.1 cgd argv += optind;
116 1.1 cgd
117 1.1 cgd errno = 0;
118 1.1 cgd prio = getpriority(PRIO_PROCESS, 0);
119 1.1 cgd if (errno)
120 1.1 cgd prio = 0;
121 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
122 1.1 cgd openlog("su", LOG_CONS, 0);
123 1.1 cgd
124 1.1 cgd /* get current login name and shell */
125 1.1 cgd ruid = getuid();
126 1.1 cgd username = getlogin();
127 1.1 cgd if (username == NULL || (pwd = getpwnam(username)) == NULL ||
128 1.1 cgd pwd->pw_uid != ruid)
129 1.1 cgd pwd = getpwuid(ruid);
130 1.1 cgd if (pwd == NULL) {
131 1.1 cgd fprintf(stderr, "su: who are you?\n");
132 1.1 cgd exit(1);
133 1.1 cgd }
134 1.1 cgd username = strdup(pwd->pw_name);
135 1.1 cgd if (asme)
136 1.1 cgd if (pwd->pw_shell && *pwd->pw_shell)
137 1.1 cgd shell = strcpy(shellbuf, pwd->pw_shell);
138 1.1 cgd else {
139 1.1 cgd shell = _PATH_BSHELL;
140 1.1 cgd iscsh = NO;
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd /* get target login information, default to root */
144 1.1 cgd user = *argv ? *argv : "root";
145 1.2 sef np = *argv ? argv : argv-1;
146 1.2 sef
147 1.1 cgd if ((pwd = getpwnam(user)) == NULL) {
148 1.1 cgd fprintf(stderr, "su: unknown login %s\n", user);
149 1.1 cgd exit(1);
150 1.1 cgd }
151 1.1 cgd
152 1.1 cgd if (ruid) {
153 1.1 cgd #ifdef KERBEROS
154 1.1 cgd if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
155 1.1 cgd #endif
156 1.1 cgd {
157 1.1 cgd /* only allow those in group zero to su to root. */
158 1.1 cgd if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
159 1.1 cgd for (g = gr->gr_mem;; ++g) {
160 1.1 cgd if (!*g) {
161 1.1 cgd (void)fprintf(stderr,
162 1.1 cgd "su: you are not in the correct group to su %s.\n",
163 1.1 cgd user);
164 1.1 cgd exit(1);
165 1.1 cgd }
166 1.1 cgd if (!strcmp(username, *g))
167 1.1 cgd break;
168 1.1 cgd }
169 1.1 cgd /* if target requires a password, verify it */
170 1.1 cgd if (*pwd->pw_passwd) {
171 1.1 cgd p = getpass("Password:");
172 1.1 cgd if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
173 1.1 cgd fprintf(stderr, "Sorry\n");
174 1.1 cgd syslog(LOG_AUTH|LOG_WARNING,
175 1.1 cgd "BAD SU %s to %s%s", username,
176 1.1 cgd user, ontty());
177 1.1 cgd exit(1);
178 1.1 cgd }
179 1.1 cgd }
180 1.1 cgd }
181 1.1 cgd }
182 1.1 cgd
183 1.1 cgd if (asme) {
184 1.1 cgd /* if asme and non-standard target shell, must be root */
185 1.1 cgd if (!chshell(pwd->pw_shell) && ruid) {
186 1.1 cgd (void)fprintf(stderr,
187 1.1 cgd "su: permission denied (shell).\n");
188 1.1 cgd exit(1);
189 1.1 cgd }
190 1.1 cgd } else if (pwd->pw_shell && *pwd->pw_shell) {
191 1.1 cgd shell = pwd->pw_shell;
192 1.1 cgd iscsh = UNSET;
193 1.1 cgd } else {
194 1.1 cgd shell = _PATH_BSHELL;
195 1.1 cgd iscsh = NO;
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd /* if we're forking a csh, we want to slightly muck the args */
199 1.1 cgd if (iscsh == UNSET) {
200 1.1 cgd if (p = rindex(shell, '/'))
201 1.1 cgd ++p;
202 1.1 cgd else
203 1.1 cgd p = shell;
204 1.1 cgd iscsh = strcmp(p, "csh") ? NO : YES;
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd /* set permissions */
208 1.1 cgd if (setgid(pwd->pw_gid) < 0) {
209 1.1 cgd perror("su: setgid");
210 1.1 cgd exit(1);
211 1.1 cgd }
212 1.1 cgd if (initgroups(user, pwd->pw_gid)) {
213 1.1 cgd (void)fprintf(stderr, "su: initgroups failed.\n");
214 1.1 cgd exit(1);
215 1.1 cgd }
216 1.1 cgd if (setuid(pwd->pw_uid) < 0) {
217 1.1 cgd perror("su: setuid");
218 1.1 cgd exit(1);
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd if (!asme) {
222 1.1 cgd if (asthem) {
223 1.1 cgd p = getenv("TERM");
224 1.1 cgd cleanenv[0] = _PATH_DEFPATH;
225 1.1 cgd cleanenv[1] = NULL;
226 1.1 cgd environ = cleanenv;
227 1.1 cgd (void)setenv("TERM", p, 1);
228 1.1 cgd if (chdir(pwd->pw_dir) < 0) {
229 1.1 cgd fprintf(stderr, "su: no directory\n");
230 1.1 cgd exit(1);
231 1.1 cgd }
232 1.1 cgd }
233 1.1 cgd if (asthem || pwd->pw_uid)
234 1.1 cgd (void)setenv("USER", pwd->pw_name, 1);
235 1.1 cgd (void)setenv("HOME", pwd->pw_dir, 1);
236 1.1 cgd (void)setenv("SHELL", shell, 1);
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd if (iscsh == YES) {
240 1.1 cgd if (fastlogin)
241 1.1 cgd *np-- = "-f";
242 1.1 cgd if (asme)
243 1.1 cgd *np-- = "-m";
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /* csh strips the first character... */
247 1.1 cgd *np = asthem ? "-su" : iscsh == YES ? "_su" : "su";
248 1.1 cgd
249 1.1 cgd if (ruid != 0)
250 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
251 1.1 cgd username, user, ontty());
252 1.1 cgd
253 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, prio);
254 1.1 cgd
255 1.1 cgd execv(shell, np);
256 1.1 cgd (void)fprintf(stderr, "su: %s not found.\n", shell);
257 1.1 cgd exit(1);
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd chshell(sh)
261 1.1 cgd char *sh;
262 1.1 cgd {
263 1.1 cgd register char *cp;
264 1.1 cgd char *getusershell();
265 1.1 cgd
266 1.1 cgd while ((cp = getusershell()) != NULL)
267 1.1 cgd if (!strcmp(cp, sh))
268 1.1 cgd return (1);
269 1.1 cgd return (0);
270 1.1 cgd }
271 1.1 cgd
272 1.1 cgd char *
273 1.1 cgd ontty()
274 1.1 cgd {
275 1.1 cgd char *p, *ttyname();
276 1.1 cgd static char buf[MAXPATHLEN + 4];
277 1.1 cgd
278 1.1 cgd buf[0] = 0;
279 1.1 cgd if (p = ttyname(STDERR_FILENO))
280 1.1 cgd sprintf(buf, " on %s", p);
281 1.1 cgd return (buf);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd #ifdef KERBEROS
285 1.1 cgd kerberos(username, user, uid)
286 1.1 cgd char *username, *user;
287 1.1 cgd int uid;
288 1.1 cgd {
289 1.1 cgd extern char *krb_err_txt[];
290 1.1 cgd KTEXT_ST ticket;
291 1.1 cgd AUTH_DAT authdata;
292 1.1 cgd struct hostent *hp;
293 1.1 cgd register char *p;
294 1.1 cgd int kerno;
295 1.1 cgd u_long faddr;
296 1.1 cgd char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
297 1.1 cgd char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
298 1.1 cgd char *ontty(), *krb_get_phost();
299 1.1 cgd
300 1.1 cgd if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
301 1.1 cgd return (1);
302 1.1 cgd if (koktologin(username, lrealm, user) && !uid) {
303 1.1 cgd (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
304 1.1 cgd return (1);
305 1.1 cgd }
306 1.1 cgd (void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
307 1.1 cgd
308 1.1 cgd (void)setenv("KRBTKFILE", krbtkfile, 1);
309 1.1 cgd (void)krb_set_tkt_string(krbtkfile);
310 1.1 cgd /*
311 1.1 cgd * Set real as well as effective ID to 0 for the moment,
312 1.1 cgd * to make the kerberos library do the right thing.
313 1.1 cgd */
314 1.1 cgd if (setuid(0) < 0) {
315 1.1 cgd perror("su: setuid");
316 1.1 cgd return (1);
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * Little trick here -- if we are su'ing to root,
321 1.1 cgd * we need to get a ticket for "xxx.root", where xxx represents
322 1.1 cgd * the name of the person su'ing. Otherwise (non-root case),
323 1.1 cgd * we need to get a ticket for "yyy.", where yyy represents
324 1.1 cgd * the name of the person being su'd to, and the instance is null
325 1.1 cgd *
326 1.1 cgd * We should have a way to set the ticket lifetime,
327 1.1 cgd * with a system default for root.
328 1.1 cgd */
329 1.1 cgd kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
330 1.1 cgd (uid == 0 ? "root" : ""), lrealm,
331 1.1 cgd "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
332 1.1 cgd
333 1.1 cgd if (kerno != KSUCCESS) {
334 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
335 1.1 cgd fprintf(stderr, "principal unknown: %s.%s@%s\n",
336 1.1 cgd (uid == 0 ? username : user),
337 1.1 cgd (uid == 0 ? "root" : ""), lrealm);
338 1.1 cgd return (1);
339 1.1 cgd }
340 1.1 cgd (void)fprintf(stderr, "su: unable to su: %s\n",
341 1.1 cgd krb_err_txt[kerno]);
342 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
343 1.1 cgd "BAD Kerberos SU: %s to %s%s: %s",
344 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
345 1.1 cgd return (1);
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd if (chown(krbtkfile, uid, -1) < 0) {
349 1.1 cgd perror("su: chown:");
350 1.1 cgd (void)unlink(krbtkfile);
351 1.1 cgd return (1);
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
355 1.1 cgd
356 1.1 cgd if (gethostname(hostname, sizeof(hostname)) == -1) {
357 1.1 cgd perror("su: gethostname");
358 1.1 cgd dest_tkt();
359 1.1 cgd return (1);
360 1.1 cgd }
361 1.1 cgd
362 1.1 cgd (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
363 1.1 cgd savehost[sizeof(savehost) - 1] = '\0';
364 1.1 cgd
365 1.1 cgd kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
366 1.1 cgd
367 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
368 1.1 cgd (void)fprintf(stderr, "Warning: TGT not verified.\n");
369 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
370 1.1 cgd "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
371 1.1 cgd username, user, ontty(), krb_err_txt[kerno],
372 1.1 cgd "rcmd", savehost);
373 1.1 cgd } else if (kerno != KSUCCESS) {
374 1.1 cgd (void)fprintf(stderr, "Unable to use TGT: %s\n",
375 1.1 cgd krb_err_txt[kerno]);
376 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
377 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
378 1.1 cgd dest_tkt();
379 1.1 cgd return (1);
380 1.1 cgd } else {
381 1.1 cgd if (!(hp = gethostbyname(hostname))) {
382 1.1 cgd (void)fprintf(stderr, "su: can't get addr of %s\n",
383 1.1 cgd hostname);
384 1.1 cgd dest_tkt();
385 1.1 cgd return (1);
386 1.1 cgd }
387 1.1 cgd (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
388 1.1 cgd
389 1.1 cgd if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
390 1.1 cgd &authdata, "")) != KSUCCESS) {
391 1.1 cgd (void)fprintf(stderr,
392 1.1 cgd "su: unable to verify rcmd ticket: %s\n",
393 1.1 cgd krb_err_txt[kerno]);
394 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
395 1.1 cgd "failed su: %s to %s%s: %s", username,
396 1.1 cgd user, ontty(), krb_err_txt[kerno]);
397 1.1 cgd dest_tkt();
398 1.1 cgd return (1);
399 1.1 cgd }
400 1.1 cgd }
401 1.1 cgd return (0);
402 1.1 cgd }
403 1.1 cgd
404 1.1 cgd koktologin(name, realm, toname)
405 1.1 cgd char *name, *realm, *toname;
406 1.1 cgd {
407 1.1 cgd register AUTH_DAT *kdata;
408 1.1 cgd AUTH_DAT kdata_st;
409 1.1 cgd
410 1.1 cgd kdata = &kdata_st;
411 1.1 cgd bzero((caddr_t) kdata, sizeof(*kdata));
412 1.1 cgd (void)strcpy(kdata->pname, name);
413 1.1 cgd (void)strcpy(kdata->pinst,
414 1.1 cgd ((strcmp(toname, "root") == 0) ? "root" : ""));
415 1.1 cgd (void)strcpy(kdata->prealm, realm);
416 1.1 cgd return (kuserok(kdata, toname));
417 1.1 cgd }
418 1.1 cgd #endif
419