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

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

Add a sample application.

Sponsored by: DARPA, NAI Labs

File size: 4.7 KB
Line 
1/*-
2 * Copyright (c) 2002 Networks Associates Technologies, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * 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 * $Id$
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 <syslog.h>
44#include <unistd.h>
45
46#include <security/pam_appl.h>
47#include <security/openpam.h>
48
49static pam_handle_t *pamh;
50static struct pam_conv pamc;
51
52static void
53usage(void)
54{
55        fprintf(stderr, "Usage: su [login [args]]\n");
56        exit(1);
57}
58
59static int
60check(const char *func, int pam_err)
61{
62        if (pam_err == PAM_SUCCESS || pam_err == PAM_NEW_AUTHTOK_REQD)
63                return pam_err;
64        openlog("su", LOG_CONS, LOG_AUTH);
65        syslog(LOG_ERR, "%s(): %s", func, pam_strerror(pamh, pam_err));
66        errx(1, "Sorry.");
67}
68
69int
70main(int argc, char *argv[])
71{
72        char hostname[MAXHOSTNAMELEN];
73        const char *user, *tty;
74        struct passwd *pwd;
75        int o, status;
76        pid_t pid;
77
78        while ((o = getopt(argc, argv, "h")) != -1)
79                switch (o) {
80                case 'h':
81                default:
82                        usage();
83                }
84
85        argc -= optind;
86        argv += optind;
87
88        /* initialize PAM */
89        pamc.conv = &openpam_ttyconv;
90        pam_start("su", argc ? *argv : "root", &pamc, &pamh);
91
92        /* set some items */
93        gethostname(hostname, sizeof hostname);
94        check("pam_set_item", pam_set_item(pamh, PAM_RHOST, hostname));
95        user = getlogin();
96        check("pam_set_item", pam_set_item(pamh, PAM_RUSER, user));
97        tty = ttyname(STDERR_FILENO);
98        check("pam_set_item", pam_set_item(pamh, PAM_TTY, tty));
99
100        /* authenticate the applicant */
101        check("pam_authenticate", pam_authenticate(pamh, 0));
102        if (check("pam_acct_mgmt", pam_acct_mgmt(pamh, 0)) ==
103            PAM_NEW_AUTHTOK_REQD)
104                check("pam_chauthtok",
105                    pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK));
106       
107        /* establish the requested credentials */
108        check("pam_setcred", pam_setcred(pamh, PAM_ESTABLISH_CRED));
109       
110        /* authentication succeeded; open a session */
111        check("pam_open_session", pam_open_session(pamh, 0));
112
113        if (initgroups(pwd->pw_name, pwd->pw_gid) == -1)
114                err(1, "initgroups()");
115        if (setuid(pwd->pw_uid) == -1)
116                err(1, "setuid()");
117       
118        /* XXX export environment variables */
119       
120        switch ((pid = fork())) {
121        case -1:
122                err(1, "fork()");
123        case 0:
124                /* child: start a shell */
125                *argv = pwd->pw_shell;
126                execvp(*argv, argv);
127                err(1, "execvp()");
128        default:
129                /* parent: wait for child to exit */
130                waitpid(pid, &status, 0);
131                if (WIFEXITED(status))
132                        status = WEXITSTATUS(status);
133                else
134                        status = 1;
135        }
136
137        /* close the session and release PAM resources */
138        check("pam_close_session", pam_close_session(pamh, 0));
139        check("pam_end", pam_end(pamh, 0));
140
141        exit(status);
142}
Note: See TracBrowser for help on using the repository browser.