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