From fbb6651eb0cb2969c5ffbdce1abd8ee6287b2925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 1 Feb 2002 20:41:47 +0000 Subject: [PATCH] Add openpam_ttyconv(), a simple tty-based conversation function. Prototype it in the new header. Move the prototype for openpam_log() there too (as well as the log level constants) so modules and applications can use it if they want to. Have lib/openpam.h include . Sponsored by: DARPA, NAI Labs git-svn-id: svn+ssh://svn.openpam.org/svn/openpam/trunk@9 185d5e19-27fe-0310-9dcf-9bff6b9f3609 --- include/security/openpam.h | 65 ++++++++++++++++++ lib/Makefile | 1 + lib/openpam.h | 10 +-- lib/openpam_ttyconv.c | 131 +++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 include/security/openpam.h create mode 100644 lib/openpam_ttyconv.c diff --git a/include/security/openpam.h b/include/security/openpam.h new file mode 100644 index 0000000..d01354e --- /dev/null +++ b/include/security/openpam.h @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2002 Networks Associates Technologies, Inc. + * All rights reserved. + * + * This software was developed for the FreeBSD Project by ThinkSec AS and + * NAI Labs, the Security Research Division of Network Associates, Inc. + * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the + * DARPA CHATS research program. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#ifndef _SECURITY_OPENPAM_H_INCLUDED +#define _SECURITY_OPENPAM_H_INCLUDED + +/* + * Log levels + */ +enum { + PAM_LOG_DEBUG, + PAM_LOG_VERBOSE, + PAM_LOG_NOTICE, + PAM_LOG_ERROR, +}; + +/* + * Log to syslog + */ +void openpam_log(int _level, + const char *_fmt, + ...); + +/* + * Generic conversation function + */ +int openpam_ttyconv(int _n, + const struct pam_message **_msg, + struct pam_response **_resp, + void *_data); + +#endif diff --git a/lib/Makefile b/lib/Makefile index a8d6712..2c03d6d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -45,6 +45,7 @@ CFLAGS += -I${.CURDIR}/../include SRCS = SRCS += openpam_dispatch.c SRCS += openpam_log.c +SRCS += openpam_ttyconv.c SRCS += pam_acct_mgmt.c SRCS += pam_authenticate.c SRCS += pam_chauthtok.c diff --git a/lib/openpam.h b/lib/openpam.h index 64de59b..88bcd68 100644 --- a/lib/openpam.h +++ b/lib/openpam.h @@ -37,6 +37,8 @@ #ifndef _OPENPAM_H_INCLUDED #define _OPENPAM_H_INCLUDED +#include + /* * Control flags */ @@ -106,14 +108,6 @@ struct pam_handle { #define PAM_OTHER "other" int openpam_dispatch(pam_handle_t *, int, int); - -#define PAM_LOG_DEBUG 0 -#define PAM_LOG_VERBOSE 1 -#define PAM_LOG_NOTICE 2 -#define PAM_LOG_ERROR 3 - -void openpam_log(int, const char *, ...); - int openpam_findenv(pam_handle_t *, const char *, size_t); #endif diff --git a/lib/openpam_ttyconv.c b/lib/openpam_ttyconv.c new file mode 100644 index 0000000..bdd77b7 --- /dev/null +++ b/lib/openpam_ttyconv.c @@ -0,0 +1,131 @@ +/*- + * Copyright (c) 2002 Networks Associates Technologies, Inc. + * All rights reserved. + * + * This software was developed for the FreeBSD Project by ThinkSec AS and + * NAI Labs, the Security Research Division of Network Associates, Inc. + * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the + * DARPA CHATS research program. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include + +/* + * Simple tty-based conversation function. + */ + +int +openpam_ttyconv(int n, + const struct pam_message **msg, + struct pam_response **resp, + void *data) +{ + char buf[PAM_MAX_RESP_SIZE]; + struct termios tattr; + tcflag_t lflag; + int fd, err, i; + size_t len; + + data = data; + if (n <= 0 || n > PAM_MAX_NUM_MSG) + return (PAM_CONV_ERR); + if ((*resp = calloc(n, sizeof **resp)) == NULL) + return (PAM_BUF_ERR); + fd = fileno(stdin); + for (i = 0; i < n; ++i) { + resp[i]->resp_retcode = 0; + resp[i]->resp = NULL; + switch (msg[i]->msg_style) { + case PAM_PROMPT_ECHO_OFF: + case PAM_PROMPT_ECHO_ON: + if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) { + if (tcgetattr(fd, &tattr) != 0) { + openpam_log(PAM_LOG_ERROR, + "tcgetattr(): %m"); + err = PAM_CONV_ERR; + goto fail; + } + lflag = tattr.c_lflag; + tattr.c_lflag &= ~ECHO; + if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) { + openpam_log(PAM_LOG_ERROR, + "tcsetattr(): %m"); + err = PAM_CONV_ERR; + goto fail; + } + } + fputs(msg[i]->msg, stderr); + buf[0] = '\0'; + fgets(buf, sizeof buf, stdin); + if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) { + tattr.c_lflag = lflag; + (void)tcsetattr(fd, TCSANOW, &tattr); + fputs("\n", stderr); + } + if (ferror(stdin)) { + err = PAM_CONV_ERR; + goto fail; + } + for (len = strlen(buf); len > 0; --len) + if (!isspace(buf[len - 1])) + break; + buf[len] = '\0'; + if ((resp[i]->resp = strdup(buf)) == NULL) { + err = PAM_BUF_ERR; + goto fail; + } + break; + case PAM_ERROR_MSG: + fputs(msg[i]->msg, stderr); + break; + case PAM_TEXT_INFO: + fputs(msg[i]->msg, stdout); + break; + default: + err = PAM_BUF_ERR; + goto fail; + } + } + return (PAM_SUCCESS); + fail: + while (i) + free(resp[--i]); + free(*resp); + *resp = NULL; + return (err); +}