2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 1998 Juniper Networks, Inc.
6 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
9 * Portions of this software were developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote
23 * products derived from this software without specific prior written
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
53 #include <security/pam_appl.h>
54 #include <security/pam_modules.h>
55 #include <security/pam_mod_misc.h>
57 #define PAM_OPT_CONF "conf"
58 #define PAM_OPT_TEMPLATE_USER "template_user"
60 typedef int (*set_func)(struct tac_handle *, const char *);
62 static int do_item(pam_handle_t *, struct tac_handle *, int,
63 set_func, const char *);
64 static char *get_msg(struct tac_handle *);
65 static int set_msg(struct tac_handle *, const char *);
68 do_item(pam_handle_t *pamh, struct tac_handle *tach, int item,
69 set_func func, const char *funcname)
74 retval = pam_get_item(pamh, item, &value);
75 if (retval != PAM_SUCCESS)
77 if (value != NULL && (*func)(tach, (const char *)value) == -1) {
78 syslog(LOG_CRIT, "%s: %s", funcname, tac_strerror(tach));
80 return PAM_SERVICE_ERR;
86 get_msg(struct tac_handle *tach)
90 msg = tac_get_msg(tach);
92 syslog(LOG_CRIT, "tac_get_msg: %s", tac_strerror(tach));
100 set_msg(struct tac_handle *tach, const char *msg)
102 if (tac_set_msg(tach, msg) == -1) {
103 syslog(LOG_CRIT, "tac_set_msg: %s", tac_strerror(tach));
111 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
112 int argc __unused, const char *argv[] __unused)
115 struct tac_handle *tach;
116 const char *conf_file, *template_user;
118 conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
119 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
123 syslog(LOG_CRIT, "tac_open failed");
124 return (PAM_SERVICE_ERR);
126 if (tac_config(tach, conf_file) == -1) {
127 syslog(LOG_ALERT, "tac_config: %s", tac_strerror(tach));
129 return (PAM_SERVICE_ERR);
131 if (tac_create_authen(tach, TAC_AUTHEN_LOGIN, TAC_AUTHEN_TYPE_ASCII,
132 TAC_AUTHEN_SVC_LOGIN) == -1) {
133 syslog(LOG_CRIT, "tac_create_authen: %s", tac_strerror(tach));
135 return (PAM_SERVICE_ERR);
138 PAM_LOG("Done tac_open() ... tac_close()");
140 retval = do_item(pamh, tach, PAM_USER, tac_set_user, "tac_set_user");
141 if (retval != PAM_SUCCESS)
144 PAM_LOG("Done user");
146 retval = do_item(pamh, tach, PAM_TTY, tac_set_port, "tac_set_port");
147 if (retval != PAM_SUCCESS)
152 retval = do_item(pamh, tach, PAM_RHOST, tac_set_rem_addr,
154 if (retval != PAM_SUCCESS)
160 const char *user_msg;
165 sflags = tac_send_authen(tach);
167 syslog(LOG_CRIT, "tac_send_authen: %s",
170 return (PAM_AUTHINFO_UNAVAIL);
172 status = TAC_AUTHEN_STATUS(sflags);
173 openpam_set_option(pamh, PAM_OPT_ECHO_PASS,
174 TAC_AUTHEN_NOECHO(sflags) ? NULL : "");
177 case TAC_AUTHEN_STATUS_PASS:
179 if (template_user != NULL) {
183 PAM_LOG("Trying template user: %s",
187 * If the given user name doesn't exist in
188 * the local password database, change it
189 * to the value given in the "template_user"
192 retval = pam_get_item(pamh, PAM_USER, &item);
193 if (retval != PAM_SUCCESS)
195 user = (const char *)item;
196 if (getpwnam(user) == NULL) {
197 pam_set_item(pamh, PAM_USER,
199 PAM_LOG("Using template user");
202 return (PAM_SUCCESS);
204 case TAC_AUTHEN_STATUS_FAIL:
206 PAM_VERBOSE_ERROR("TACACS+ authentication failed");
207 return (PAM_AUTH_ERR);
209 case TAC_AUTHEN_STATUS_GETUSER:
210 case TAC_AUTHEN_STATUS_GETPASS:
211 if ((srvr_msg = get_msg(tach)) == NULL)
212 return (PAM_SERVICE_ERR);
213 if (status == TAC_AUTHEN_STATUS_GETUSER)
214 retval = pam_get_user(pamh, &user_msg,
215 *srvr_msg ? srvr_msg : NULL);
216 else if (status == TAC_AUTHEN_STATUS_GETPASS)
217 retval = pam_get_authtok(pamh,
218 PAM_AUTHTOK, &user_msg,
219 *srvr_msg ? srvr_msg : "Password:");
221 if (retval != PAM_SUCCESS) {
222 /* XXX - send a TACACS+ abort packet */
226 if (set_msg(tach, user_msg) == -1)
227 return (PAM_SERVICE_ERR);
230 case TAC_AUTHEN_STATUS_GETDATA:
231 if ((srvr_msg = get_msg(tach)) == NULL)
232 return (PAM_SERVICE_ERR);
233 retval = pam_prompt(pamh,
234 openpam_get_option(pamh, PAM_OPT_ECHO_PASS) ?
235 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF,
236 &data_msg, "%s", *srvr_msg ? srvr_msg : "Data:");
238 if (retval != PAM_SUCCESS) {
239 /* XXX - send a TACACS+ abort packet */
243 retval = set_msg(tach, data_msg);
244 memset(data_msg, 0, strlen(data_msg));
247 return (PAM_SERVICE_ERR);
250 case TAC_AUTHEN_STATUS_ERROR:
251 srvr_msg = (char *)tac_get_data(tach, &msg_len);
252 if (srvr_msg != NULL && msg_len != 0) {
253 syslog(LOG_CRIT, "tac_send_authen:"
254 " server detected error: %s", srvr_msg);
259 "tac_send_authen: server detected error");
261 return (PAM_AUTHINFO_UNAVAIL);
264 case TAC_AUTHEN_STATUS_RESTART:
265 case TAC_AUTHEN_STATUS_FOLLOW:
268 "tac_send_authen: unexpected status %#x", status);
270 return (PAM_AUTHINFO_UNAVAIL);
276 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
277 int argc __unused, const char *argv[] __unused)
283 PAM_MODULE_ENTRY("pam_tacplus");