su.c revision 1.1.1.1 1 1.1 christos /* $NetBSD: su.c,v 1.1.1.1 2011/12/25 21:42:53 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5 1.1 christos * Copyright (c) 2004-2011 Dag-Erling Smrgrav
6 1.1 christos * All rights reserved.
7 1.1 christos *
8 1.1 christos * This software was developed for the FreeBSD Project by ThinkSec AS and
9 1.1 christos * Network Associates Laboratories, the Security Research Division of
10 1.1 christos * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
11 1.1 christos * ("CBOSS"), as part of the DARPA CHATS research program.
12 1.1 christos *
13 1.1 christos * Redistribution and use in source and binary forms, with or without
14 1.1 christos * modification, are permitted provided that the following conditions
15 1.1 christos * are met:
16 1.1 christos * 1. Redistributions of source code must retain the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer.
18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer in the
20 1.1 christos * documentation and/or other materials provided with the distribution.
21 1.1 christos * 3. The name of the author may not be used to endorse or promote
22 1.1 christos * products derived from this software without specific prior written
23 1.1 christos * permission.
24 1.1 christos *
25 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 christos * SUCH DAMAGE.
36 1.1 christos *
37 1.1 christos * Id: su.c 458 2011-11-02 13:10:25Z des
38 1.1 christos */
39 1.1 christos
40 1.1 christos #ifdef HAVE_CONFIG_H
41 1.1 christos # include "config.h"
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos #include <sys/param.h>
45 1.1 christos #include <sys/wait.h>
46 1.1 christos
47 1.1 christos #include <err.h>
48 1.1 christos #include <grp.h>
49 1.1 christos #include <pwd.h>
50 1.1 christos #include <stdio.h>
51 1.1 christos #include <stdlib.h>
52 1.1 christos #include <string.h>
53 1.1 christos #include <syslog.h>
54 1.1 christos #include <unistd.h>
55 1.1 christos
56 1.1 christos #include <security/pam_appl.h>
57 1.1 christos #include <security/openpam.h> /* for openpam_ttyconv() */
58 1.1 christos
59 1.1 christos extern char **environ;
60 1.1 christos
61 1.1 christos static pam_handle_t *pamh;
62 1.1 christos static struct pam_conv pamc;
63 1.1 christos
64 1.1 christos static void
65 1.1 christos usage(void)
66 1.1 christos {
67 1.1 christos
68 1.1 christos fprintf(stderr, "usage: su [login [args]]\n");
69 1.1 christos exit(1);
70 1.1 christos }
71 1.1 christos
72 1.1 christos int
73 1.1 christos main(int argc, char *argv[])
74 1.1 christos {
75 1.1 christos char hostname[MAXHOSTNAMELEN];
76 1.1 christos const char *user, *tty;
77 1.1 christos const void *item;
78 1.1 christos char **args, **pam_envlist, **pam_env;
79 1.1 christos struct passwd *pwd;
80 1.1 christos int o, pam_err, status;
81 1.1 christos pid_t pid;
82 1.1 christos
83 1.1 christos while ((o = getopt(argc, argv, "")) != -1)
84 1.1 christos switch (o) {
85 1.1 christos default:
86 1.1 christos usage();
87 1.1 christos }
88 1.1 christos
89 1.1 christos argc -= optind;
90 1.1 christos argv += optind;
91 1.1 christos
92 1.1 christos if (argc > 0) {
93 1.1 christos user = *argv;
94 1.1 christos --argc;
95 1.1 christos ++argv;
96 1.1 christos } else {
97 1.1 christos user = "root";
98 1.1 christos }
99 1.1 christos
100 1.1 christos /* initialize PAM */
101 1.1 christos pamc.conv = &openpam_ttyconv;
102 1.1 christos pam_start("su", user, &pamc, &pamh);
103 1.1 christos
104 1.1 christos /* set some items */
105 1.1 christos gethostname(hostname, sizeof(hostname));
106 1.1 christos if ((pam_err = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS)
107 1.1 christos goto pamerr;
108 1.1 christos user = getlogin();
109 1.1 christos if ((pam_err = pam_set_item(pamh, PAM_RUSER, user)) != PAM_SUCCESS)
110 1.1 christos goto pamerr;
111 1.1 christos tty = ttyname(STDERR_FILENO);
112 1.1 christos if ((pam_err = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS)
113 1.1 christos goto pamerr;
114 1.1 christos
115 1.1 christos /* authenticate the applicant */
116 1.1 christos if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
117 1.1 christos goto pamerr;
118 1.1 christos if ((pam_err = pam_acct_mgmt(pamh, 0)) == PAM_NEW_AUTHTOK_REQD)
119 1.1 christos pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
120 1.1 christos if (pam_err != PAM_SUCCESS)
121 1.1 christos goto pamerr;
122 1.1 christos
123 1.1 christos /* establish the requested credentials */
124 1.1 christos if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
125 1.1 christos goto pamerr;
126 1.1 christos
127 1.1 christos /* authentication succeeded; open a session */
128 1.1 christos if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
129 1.1 christos goto pamerr;
130 1.1 christos
131 1.1 christos /* get mapped user name; PAM may have changed it */
132 1.1 christos pam_err = pam_get_item(pamh, PAM_USER, &item);
133 1.1 christos if (pam_err != PAM_SUCCESS || (pwd = getpwnam(user = item)) == NULL)
134 1.1 christos goto pamerr;
135 1.1 christos
136 1.1 christos /* export PAM environment */
137 1.1 christos if ((pam_envlist = pam_getenvlist(pamh)) != NULL) {
138 1.1 christos for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env) {
139 1.1 christos putenv(*pam_env);
140 1.1 christos free(*pam_env);
141 1.1 christos }
142 1.1 christos free(pam_envlist);
143 1.1 christos }
144 1.1 christos
145 1.1 christos /* build argument list */
146 1.1 christos if ((args = calloc(argc + 2, sizeof *args)) == NULL) {
147 1.1 christos warn("calloc()");
148 1.1 christos goto err;
149 1.1 christos }
150 1.1 christos *args = pwd->pw_shell;
151 1.1 christos memcpy(args + 1, argv, argc * sizeof *args);
152 1.1 christos
153 1.1 christos /* fork and exec */
154 1.1 christos switch ((pid = fork())) {
155 1.1 christos case -1:
156 1.1 christos warn("fork()");
157 1.1 christos goto err;
158 1.1 christos case 0:
159 1.1 christos /* child: give up privs and start a shell */
160 1.1 christos
161 1.1 christos /* set uid and groups */
162 1.1 christos if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
163 1.1 christos warn("initgroups()");
164 1.1 christos _exit(1);
165 1.1 christos }
166 1.1 christos if (setgid(pwd->pw_gid) == -1) {
167 1.1 christos warn("setgid()");
168 1.1 christos _exit(1);
169 1.1 christos }
170 1.1 christos if (setuid(pwd->pw_uid) == -1) {
171 1.1 christos warn("setuid()");
172 1.1 christos _exit(1);
173 1.1 christos }
174 1.1 christos execve(*args, args, environ);
175 1.1 christos warn("execve()");
176 1.1 christos _exit(1);
177 1.1 christos default:
178 1.1 christos /* parent: wait for child to exit */
179 1.1 christos waitpid(pid, &status, 0);
180 1.1 christos
181 1.1 christos /* close the session and release PAM resources */
182 1.1 christos pam_err = pam_close_session(pamh, 0);
183 1.1 christos pam_end(pamh, pam_err);
184 1.1 christos
185 1.1 christos exit(WEXITSTATUS(status));
186 1.1 christos }
187 1.1 christos
188 1.1 christos pamerr:
189 1.1 christos fprintf(stderr, "Sorry\n");
190 1.1 christos err:
191 1.1 christos pam_end(pamh, pam_err);
192 1.1 christos exit(1);
193 1.1 christos }
194