2 * Copyright 1998 Juniper Networks, Inc.
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the 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
36 * $FreeBSD: src/lib/libpam/modules/pam_tacplus/pam_tacplus.c,v 1.14 2003/05/31 17:19:03 des Exp $
37 * $DragonFly: src/lib/pam_module/pam_tacplus/pam_tacplus.c,v 1.1 2005/07/12 22:34:55 joerg Exp $
40 #include <sys/param.h>
51 #include <security/pam_appl.h>
52 #include <security/pam_modules.h>
53 #include <security/pam_mod_misc.h>
55 #define PAM_OPT_CONF "conf"
56 #define PAM_OPT_TEMPLATE_USER "template_user"
58 typedef int (*set_func)(struct tac_handle *, const char *);
60 static int do_item(pam_handle_t *, struct tac_handle *, int,
61 set_func, const char *);
62 static char *get_msg(struct tac_handle *);
63 static int set_msg(struct tac_handle *, const char *);
66 do_item(pam_handle_t *pamh, struct tac_handle *tach, int item,
67 set_func func, const char *funcname)
72 retval = pam_get_item(pamh, item, &value);
73 if (retval != PAM_SUCCESS)
75 if (value != NULL && (*func)(tach, (const char *)value) == -1) {
76 syslog(LOG_CRIT, "%s: %s", funcname, tac_strerror(tach));
78 return PAM_SERVICE_ERR;
84 get_msg(struct tac_handle *tach)
88 msg = tac_get_msg(tach);
90 syslog(LOG_CRIT, "tac_get_msg: %s", tac_strerror(tach));
98 set_msg(struct tac_handle *tach, const char *msg)
100 if (tac_set_msg(tach, msg) == -1) {
101 syslog(LOG_CRIT, "tac_set_msg: %s", tac_strerror(tach));
109 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
110 int argc __unused, const char *argv[] __unused)
113 struct tac_handle *tach;
114 const char *conf_file, *template_user;
116 conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
117 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
121 syslog(LOG_CRIT, "tac_open failed");
122 return (PAM_SERVICE_ERR);
124 if (tac_config(tach, conf_file) == -1) {
125 syslog(LOG_ALERT, "tac_config: %s", tac_strerror(tach));
127 return (PAM_SERVICE_ERR);
129 if (tac_create_authen(tach, TAC_AUTHEN_LOGIN, TAC_AUTHEN_TYPE_ASCII,
130 TAC_AUTHEN_SVC_LOGIN) == -1) {
131 syslog(LOG_CRIT, "tac_create_authen: %s", tac_strerror(tach));
133 return (PAM_SERVICE_ERR);
136 PAM_LOG("Done tac_open() ... tac_close()");
138 retval = do_item(pamh, tach, PAM_USER, tac_set_user, "tac_set_user");
139 if (retval != PAM_SUCCESS)
142 PAM_LOG("Done user");
144 retval = do_item(pamh, tach, PAM_TTY, tac_set_port, "tac_set_port");
145 if (retval != PAM_SUCCESS)
150 retval = do_item(pamh, tach, PAM_RHOST, tac_set_rem_addr,
152 if (retval != PAM_SUCCESS)
158 const char *user_msg;
163 sflags = tac_send_authen(tach);
165 syslog(LOG_CRIT, "tac_send_authen: %s",
168 return (PAM_AUTHINFO_UNAVAIL);
170 status = TAC_AUTHEN_STATUS(sflags);
171 openpam_set_option(pamh, PAM_OPT_ECHO_PASS,
172 TAC_AUTHEN_NOECHO(sflags) ? NULL : "");
175 case TAC_AUTHEN_STATUS_PASS:
177 if (template_user != NULL) {
181 PAM_LOG("Trying template user: %s",
185 * If the given user name doesn't exist in
186 * the local password database, change it
187 * to the value given in the "template_user"
190 retval = pam_get_item(pamh, PAM_USER, &item);
191 if (retval != PAM_SUCCESS)
193 user = (const char *)item;
194 if (getpwnam(user) == NULL) {
195 pam_set_item(pamh, PAM_USER,
197 PAM_LOG("Using template user");
200 return (PAM_SUCCESS);
202 case TAC_AUTHEN_STATUS_FAIL:
204 PAM_VERBOSE_ERROR("TACACS+ authentication failed");
205 return (PAM_AUTH_ERR);
207 case TAC_AUTHEN_STATUS_GETUSER:
208 case TAC_AUTHEN_STATUS_GETPASS:
209 if ((srvr_msg = get_msg(tach)) == NULL)
210 return (PAM_SERVICE_ERR);
211 if (status == TAC_AUTHEN_STATUS_GETUSER)
212 retval = pam_get_user(pamh, &user_msg,
213 *srvr_msg ? srvr_msg : NULL);
214 else if (status == TAC_AUTHEN_STATUS_GETPASS)
215 retval = pam_get_authtok(pamh,
216 PAM_AUTHTOK, &user_msg,
217 *srvr_msg ? srvr_msg : "Password:");
219 if (retval != PAM_SUCCESS) {
220 /* XXX - send a TACACS+ abort packet */
224 if (set_msg(tach, user_msg) == -1)
225 return (PAM_SERVICE_ERR);
228 case TAC_AUTHEN_STATUS_GETDATA:
229 if ((srvr_msg = get_msg(tach)) == NULL)
230 return (PAM_SERVICE_ERR);
231 retval = pam_prompt(pamh,
232 openpam_get_option(pamh, PAM_OPT_ECHO_PASS) ?
233 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF,
234 &data_msg, "%s", *srvr_msg ? srvr_msg : "Data:");
236 if (retval != PAM_SUCCESS) {
237 /* XXX - send a TACACS+ abort packet */
241 retval = set_msg(tach, data_msg);
242 memset(data_msg, 0, strlen(data_msg));
245 return (PAM_SERVICE_ERR);
248 case TAC_AUTHEN_STATUS_ERROR:
249 srvr_msg = (char *)tac_get_data(tach, &msg_len);
250 if (srvr_msg != NULL && msg_len != 0) {
251 syslog(LOG_CRIT, "tac_send_authen:"
252 " server detected error: %s", srvr_msg);
257 "tac_send_authen: server detected error");
259 return (PAM_AUTHINFO_UNAVAIL);
262 case TAC_AUTHEN_STATUS_RESTART:
263 case TAC_AUTHEN_STATUS_FOLLOW:
266 "tac_send_authen: unexpected status %#x", status);
268 return (PAM_AUTHINFO_UNAVAIL);
274 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
275 int argc __unused, const char *argv[] __unused)
281 PAM_MODULE_ENTRY("pam_tacplus");