openpam_restore_cred.c revision 115619
1234285Sdim/*-
2234285Sdim * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3234285Sdim * All rights reserved.
4234285Sdim *
5234285Sdim * This software was developed for the FreeBSD Project by ThinkSec AS and
6234285Sdim * Network Associates Laboratories, the Security Research Division of
7234285Sdim * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
8234285Sdim * ("CBOSS"), as part of the DARPA CHATS research program.
9234285Sdim *
10234285Sdim * Redistribution and use in source and binary forms, with or without
11234285Sdim * modification, are permitted provided that the following conditions
12234285Sdim * are met:
13234285Sdim * 1. Redistributions of source code must retain the above copyright
14234285Sdim *    notice, this list of conditions and the following disclaimer.
15249423Sdim * 2. Redistributions in binary form must reproduce the above copyright
16249423Sdim *    notice, this list of conditions and the following disclaimer in the
17234285Sdim *    documentation and/or other materials provided with the distribution.
18249423Sdim * 3. The name of the author may not be used to endorse or promote
19249423Sdim *    products derived from this software without specific prior written
20234285Sdim *    permission.
21243830Sdim *
22234285Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23234285Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24234285Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25234285Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26249423Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27234285Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28234285Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29234285Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30239462Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31234285Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32234285Sdim * SUCH DAMAGE.
33234285Sdim *
34234285Sdim * $P4: //depot/projects/openpam/lib/openpam_restore_cred.c#8 $
35234285Sdim */
36234285Sdim
37234285Sdim#include <sys/param.h>
38234285Sdim
39239462Sdim#include <grp.h>
40234285Sdim#include <pwd.h>
41234285Sdim#include <stdlib.h>
42234285Sdim#include <unistd.h>
43234285Sdim
44243830Sdim#include <security/pam_appl.h>
45243830Sdim
46243830Sdim#include "openpam_impl.h"
47243830Sdim
48243830Sdim/*
49249423Sdim * OpenPAM extension
50243830Sdim *
51249423Sdim * Restore credentials
52249423Sdim */
53249423Sdim
54243830Sdimint
55243830Sdimopenpam_restore_cred(pam_handle_t *pamh)
56249423Sdim{
57249423Sdim	struct pam_saved_cred *scred;
58249423Sdim	int r;
59249423Sdim
60234285Sdim	ENTER();
61234285Sdim	r = pam_get_data(pamh, PAM_SAVED_CRED, (const void **)&scred);
62234285Sdim	if (r != PAM_SUCCESS)
63234285Sdim		RETURNC(r);
64234285Sdim	if (scred == NULL)
65239462Sdim		RETURNC(PAM_SYSTEM_ERR);
66234285Sdim	if (scred->euid != geteuid()) {
67234285Sdim		if (seteuid(scred->euid) < 0 ||
68234285Sdim		    setgroups(scred->ngroups, scred->groups) < 0 ||
69234285Sdim		    setegid(scred->egid) < 0)
70234285Sdim			RETURNC(PAM_SYSTEM_ERR);
71234285Sdim	}
72234285Sdim	pam_set_data(pamh, PAM_SAVED_CRED, NULL, NULL);
73234285Sdim	RETURNC(PAM_SUCCESS);
74234285Sdim}
75234285Sdim
76234285Sdim/*
77234285Sdim * Error codes:
78243830Sdim *
79243830Sdim *	=pam_get_data
80243830Sdim *	PAM_SYSTEM_ERR
81234285Sdim */
82234285Sdim
83234285Sdim/**
84234285Sdim * The =openpam_restore_cred function restores the credentials saved by
85234285Sdim * =openpam_borrow_cred.
86234285Sdim *
87234285Sdim * >setegid
88234982Sdim * >seteuid
89234982Sdim * >setgroups
90234982Sdim */
91234982Sdim