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