source: openpam/trunk/bin/su/su.c @ 437

Last change on this file since 437 was 437, checked in by Dag-Erling Smørgrav, 9 years ago

Update copyright and release notes.

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