2 * Copyright 1998 Juniper Networks, Inc.
4 * Copyright (c) 2001 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.2.6.1 2002/07/03 21:41:31 des Exp $
37 * $DragonFly: src/lib/libpam/modules/pam_tacplus/Attic/pam_tacplus.c,v 1.2 2003/06/17 04:26:51 dillon Exp $
40 #include <sys/param.h>
50 #define PAM_SM_ACCOUNT
51 #define PAM_SM_SESSION
52 #define PAM_SM_PASSWORD
54 #include <security/pam_appl.h>
55 #include <security/pam_modules.h>
56 #include <security/pam_mod_misc.h>
58 enum { PAM_OPT_CONF=PAM_OPT_STD_MAX, PAM_OPT_TEMPLATE_USER };
60 static struct opttab other_options[] = {
61 { "conf", PAM_OPT_CONF },
62 { "template_user", PAM_OPT_TEMPLATE_USER },
66 typedef int (*set_func)(struct tac_handle *, const char *);
68 static int do_item(pam_handle_t *, struct tac_handle *, int,
69 set_func, const char *);
70 static char *get_msg(struct tac_handle *);
71 static int set_msg(struct tac_handle *, const char *);
74 do_item(pam_handle_t *pamh, struct tac_handle *tach, int item,
75 set_func func, const char *funcname)
80 retval = pam_get_item(pamh, item, &value);
81 if (retval != PAM_SUCCESS)
83 if (value != NULL && (*func)(tach, (const char *)value) == -1) {
84 syslog(LOG_CRIT, "%s: %s", funcname, tac_strerror(tach));
86 return PAM_SERVICE_ERR;
92 get_msg(struct tac_handle *tach)
96 msg = tac_get_msg(tach);
98 syslog(LOG_CRIT, "tac_get_msg: %s", tac_strerror(tach));
106 set_msg(struct tac_handle *tach, const char *msg)
108 if (tac_set_msg(tach, msg) == -1) {
109 syslog(LOG_CRIT, "tac_set_msg: %s", tac_strerror(tach));
117 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, int argc,
120 struct options options;
122 struct tac_handle *tach;
126 pam_std_option(&options, other_options, argc, argv);
128 PAM_LOG("Options processed");
131 pam_test_option(&options, PAM_OPT_CONF, &conf_file);
132 template_user = NULL;
133 pam_test_option(&options, PAM_OPT_TEMPLATE_USER, &template_user);
137 syslog(LOG_CRIT, "tac_open failed");
138 PAM_RETURN(PAM_SERVICE_ERR);
140 if (tac_config(tach, conf_file) == -1) {
141 syslog(LOG_ALERT, "tac_config: %s", tac_strerror(tach));
143 PAM_RETURN(PAM_SERVICE_ERR);
145 if (tac_create_authen(tach, TAC_AUTHEN_LOGIN, TAC_AUTHEN_TYPE_ASCII,
146 TAC_AUTHEN_SVC_LOGIN) == -1) {
147 syslog(LOG_CRIT, "tac_create_authen: %s", tac_strerror(tach));
149 PAM_RETURN(PAM_SERVICE_ERR);
152 PAM_LOG("Done tac_open() ... tac_close()");
154 retval = do_item(pamh, tach, PAM_USER, tac_set_user, "tac_set_user");
155 if (retval != PAM_SUCCESS)
158 PAM_LOG("Done user");
160 retval = do_item(pamh, tach, PAM_TTY, tac_set_port, "tac_set_port");
161 if (retval != PAM_SUCCESS)
166 retval = do_item(pamh, tach, PAM_RHOST, tac_set_rem_addr,
168 if (retval != PAM_SUCCESS)
174 const char *user_msg;
179 sflags = tac_send_authen(tach);
181 syslog(LOG_CRIT, "tac_send_authen: %s",
184 PAM_RETURN(PAM_AUTHINFO_UNAVAIL);
186 status = TAC_AUTHEN_STATUS(sflags);
187 if (!TAC_AUTHEN_NOECHO(sflags))
188 pam_set_option(&options, PAM_OPT_ECHO_PASS);
191 case TAC_AUTHEN_STATUS_PASS:
193 if (template_user != NULL) {
197 PAM_LOG("Trying template user: %s",
201 * If the given user name doesn't exist in
202 * the local password database, change it
203 * to the value given in the "template_user"
206 retval = pam_get_item(pamh, PAM_USER, &item);
207 if (retval != PAM_SUCCESS)
209 user = (const char *)item;
210 if (getpwnam(user) == NULL) {
211 pam_set_item(pamh, PAM_USER,
213 PAM_LOG("Using template user");
216 PAM_RETURN(PAM_SUCCESS);
218 case TAC_AUTHEN_STATUS_FAIL:
220 PAM_VERBOSE_ERROR("TACACS+ authentication failed");
221 PAM_RETURN(PAM_AUTH_ERR);
223 case TAC_AUTHEN_STATUS_GETUSER:
224 case TAC_AUTHEN_STATUS_GETPASS:
225 if ((srvr_msg = get_msg(tach)) == NULL)
226 PAM_RETURN(PAM_SERVICE_ERR);
227 if (status == TAC_AUTHEN_STATUS_GETUSER)
228 retval = pam_get_user(pamh, &user_msg,
229 srvr_msg[0] != '\0' ? srvr_msg : NULL);
230 else if (status == TAC_AUTHEN_STATUS_GETPASS)
231 retval = pam_get_pass(pamh, &user_msg,
232 srvr_msg[0] != '\0' ? srvr_msg :
233 "Password:", &options);
235 if (retval != PAM_SUCCESS) {
236 /* XXX - send a TACACS+ abort packet */
240 if (set_msg(tach, user_msg) == -1)
241 PAM_RETURN(PAM_SERVICE_ERR);
244 case TAC_AUTHEN_STATUS_GETDATA:
245 if ((srvr_msg = get_msg(tach)) == NULL)
246 PAM_RETURN(PAM_SERVICE_ERR);
247 retval = pam_prompt(pamh,
248 pam_test_option(&options, PAM_OPT_ECHO_PASS, NULL)
249 ? PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF,
250 srvr_msg[0] != '\0' ? srvr_msg : "Data:",
253 if (retval != PAM_SUCCESS) {
254 /* XXX - send a TACACS+ abort packet */
258 retval = set_msg(tach, data_msg);
259 memset(data_msg, 0, strlen(data_msg));
262 PAM_RETURN(PAM_SERVICE_ERR);
265 case TAC_AUTHEN_STATUS_ERROR:
266 srvr_msg = (char *)tac_get_data(tach, &msg_len);
267 if (srvr_msg != NULL && msg_len != 0) {
268 syslog(LOG_CRIT, "tac_send_authen:"
269 " server detected error: %s", srvr_msg);
274 "tac_send_authen: server detected error");
276 PAM_RETURN(PAM_AUTHINFO_UNAVAIL);
279 case TAC_AUTHEN_STATUS_RESTART:
280 case TAC_AUTHEN_STATUS_FOLLOW:
283 "tac_send_authen: unexpected status %#x", status);
285 PAM_RETURN(PAM_AUTHINFO_UNAVAIL);
291 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
293 struct options options;
295 pam_std_option(&options, NULL, argc, argv);
297 PAM_LOG("Options processed");
299 PAM_RETURN(PAM_IGNORE);
303 pam_sm_acct_mgmt(pam_handle_t *pamh __unused, int flags __unused, int argc ,const char **argv)
305 struct options options;
307 pam_std_option(&options, NULL, argc, argv);
309 PAM_LOG("Options processed");
311 PAM_RETURN(PAM_IGNORE);
315 pam_sm_chauthtok(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
317 struct options options;
319 pam_std_option(&options, NULL, argc, argv);
321 PAM_LOG("Options processed");
323 PAM_RETURN(PAM_IGNORE);
327 pam_sm_open_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
329 struct options options;
331 pam_std_option(&options, NULL, argc, argv);
333 PAM_LOG("Options processed");
335 PAM_RETURN(PAM_IGNORE);
339 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
341 struct options options;
343 pam_std_option(&options, NULL, argc, argv);
345 PAM_LOG("Options processed");
347 PAM_RETURN(PAM_IGNORE);
350 PAM_MODULE_ENTRY("pam_tacplus");