1 /* $NetBSD: pam_exec.c,v 1.3 2005/02/26 16:03:58 thorpej Exp $ */
4 * Copyright (c) 2001,2003 Networks Associates Technology, Inc.
7 * This software was developed for the FreeBSD Project by ThinkSec AS and
8 * NAI Labs, the Security Research Division of Network Associates, Inc.
9 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10 * DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 * products derived from this software without specific prior written
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_exec/pam_exec.c,v 1.4 2005/02/01 10:37:07 des Exp $");
41 __RCSID("$NetBSD: pam_exec.c,v 1.3 2005/02/26 16:03:58 thorpej Exp $");
44 #include <sys/types.h>
53 #include <security/pam_appl.h>
54 #include <security/pam_modules.h>
55 #include <security/openpam.h>
57 #define ENV_ITEM(n) { (n), #n }
62 ENV_ITEM(PAM_SERVICE
),
70 _pam_exec(pam_handle_t
*pamh __unused
, int flags __unused
,
71 int argc
, const char *argv
[])
73 int childerr
, envlen
, i
, nitems
, pam_err
, status
;
74 char **envlist
, **tmp
;
78 return (PAM_SERVICE_ERR
);
81 * XXX For additional credit, divert child's stdin/stdout/stderr
82 * to the conversation function.
86 * Set up the child's environment list. It consists of the PAM
87 * environment, plus a few hand-picked PAM items.
89 envlist
= pam_getenvlist(pamh
);
90 for (envlen
= 0; envlist
[envlen
] != NULL
; ++envlen
)
92 nitems
= sizeof(env_items
) / sizeof(*env_items
);
93 tmp
= realloc(envlist
, (envlen
+ nitems
+ 1) * sizeof **envlist
);
95 openpam_free_envlist(envlist
);
99 for (i
= 0; i
< nitems
; ++i
) {
103 pam_err
= pam_get_item(pamh
, env_items
[i
].item
, &item
);
104 if (pam_err
!= PAM_SUCCESS
|| item
== NULL
)
106 asprintf(&envstr
, "%s=%s", env_items
[i
].name
,
108 if (envstr
== NULL
) {
109 openpam_free_envlist(envlist
);
110 return (PAM_BUF_ERR
);
112 envlist
[envlen
++] = envstr
;
113 envlist
[envlen
] = NULL
;
117 * Fork and run the command. By using vfork() instead of fork(),
118 * we can distinguish between an execve() failure and a non-zero
119 * exit code from the command.
122 if ((pid
= vfork()) == 0) {
123 /*LINTED const cast*/
124 execve(argv
[0], (char **)__UNCONST(argv
), envlist
);
128 openpam_free_envlist(envlist
);
130 openpam_log(PAM_LOG_ERROR
, "vfork(): %m");
131 return (PAM_SYSTEM_ERR
);
133 if (waitpid(pid
, &status
, 0) == -1) {
134 openpam_log(PAM_LOG_ERROR
, "waitpid(): %m");
135 return (PAM_SYSTEM_ERR
);
138 openpam_log(PAM_LOG_ERROR
, "execve(): %m");
139 return (PAM_SYSTEM_ERR
);
141 if (WIFSIGNALED(status
)) {
142 openpam_log(PAM_LOG_ERROR
, "%s caught signal %d%s",
143 argv
[0], WTERMSIG(status
),
144 WCOREDUMP(status
) ? " (core dumped)" : "");
145 return (PAM_SYSTEM_ERR
);
147 if (!WIFEXITED(status
)) {
148 openpam_log(PAM_LOG_ERROR
, "unknown status 0x%x", status
);
149 return (PAM_SYSTEM_ERR
);
151 if (WEXITSTATUS(status
) != 0) {
152 openpam_log(PAM_LOG_ERROR
, "%s returned code %d",
153 argv
[0], WEXITSTATUS(status
));
154 return (PAM_SYSTEM_ERR
);
156 return (PAM_SUCCESS
);
160 pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
161 int argc
, const char *argv
[])
164 return (_pam_exec(pamh
, flags
, argc
, argv
));
168 pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
169 int argc
, const char *argv
[])
172 return (_pam_exec(pamh
, flags
, argc
, argv
));
176 pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
177 int argc
, const char *argv
[])
180 return (_pam_exec(pamh
, flags
, argc
, argv
));
184 pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
185 int argc
, const char *argv
[])
188 return (_pam_exec(pamh
, flags
, argc
, argv
));
192 pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
193 int argc
, const char *argv
[])
196 return (_pam_exec(pamh
, flags
, argc
, argv
));
200 pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
201 int argc
, const char *argv
[])
204 return (_pam_exec(pamh
, flags
, argc
, argv
));
207 PAM_MODULE_ENTRY("pam_exec");