1*0d9d0fd8Schristos /* $NetBSD: openpam.h,v 1.12 2023/06/30 21:46:20 christos Exp $ */ 244269bb5Schristos 344269bb5Schristos /*- 444269bb5Schristos * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 54cb4af11Schristos * Copyright (c) 2004-2015 Dag-Erling Smørgrav 644269bb5Schristos * All rights reserved. 744269bb5Schristos * 844269bb5Schristos * This software was developed for the FreeBSD Project by ThinkSec AS and 944269bb5Schristos * Network Associates Laboratories, the Security Research Division of 1044269bb5Schristos * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 1144269bb5Schristos * ("CBOSS"), as part of the DARPA CHATS research program. 1244269bb5Schristos * 1344269bb5Schristos * Redistribution and use in source and binary forms, with or without 1444269bb5Schristos * modification, are permitted provided that the following conditions 1544269bb5Schristos * are met: 1644269bb5Schristos * 1. Redistributions of source code must retain the above copyright 1744269bb5Schristos * notice, this list of conditions and the following disclaimer. 1844269bb5Schristos * 2. Redistributions in binary form must reproduce the above copyright 1944269bb5Schristos * notice, this list of conditions and the following disclaimer in the 2044269bb5Schristos * documentation and/or other materials provided with the distribution. 2144269bb5Schristos * 3. The name of the author may not be used to endorse or promote 2244269bb5Schristos * products derived from this software without specific prior written 2344269bb5Schristos * permission. 2444269bb5Schristos * 2544269bb5Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2644269bb5Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2744269bb5Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2844269bb5Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2944269bb5Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3044269bb5Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3144269bb5Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3244269bb5Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3344269bb5Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3444269bb5Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3544269bb5Schristos * SUCH DAMAGE. 3644269bb5Schristos */ 3744269bb5Schristos 3844269bb5Schristos #ifndef SECURITY_OPENPAM_H_INCLUDED 3944269bb5Schristos #define SECURITY_OPENPAM_H_INCLUDED 4044269bb5Schristos 4144269bb5Schristos /* 4244269bb5Schristos * Annoying but necessary header pollution 4344269bb5Schristos */ 4444269bb5Schristos #include <stdarg.h> 4544269bb5Schristos 4644269bb5Schristos #include <security/openpam_attr.h> 4744269bb5Schristos 4844269bb5Schristos #ifdef __cplusplus 4944269bb5Schristos extern "C" { 5044269bb5Schristos #endif 5144269bb5Schristos 5244269bb5Schristos struct passwd; 5344269bb5Schristos 5444269bb5Schristos /* 5544269bb5Schristos * API extensions 5644269bb5Schristos */ 5744269bb5Schristos int 5844269bb5Schristos openpam_borrow_cred(pam_handle_t *_pamh, 5944269bb5Schristos const struct passwd *_pwd) 6044269bb5Schristos OPENPAM_NONNULL((1,2)); 6144269bb5Schristos 6244269bb5Schristos int 6344269bb5Schristos openpam_subst(const pam_handle_t *_pamh, 6444269bb5Schristos char *_buf, 6544269bb5Schristos size_t *_bufsize, 6644269bb5Schristos const char *_template); 6744269bb5Schristos 6844269bb5Schristos void 6944269bb5Schristos openpam_free_data(pam_handle_t *_pamh, 7044269bb5Schristos void *_data, 7144269bb5Schristos int _status); 7244269bb5Schristos 7344269bb5Schristos void 7444269bb5Schristos openpam_free_envlist(char **_envlist); 7544269bb5Schristos 7644269bb5Schristos const char * 7744269bb5Schristos openpam_get_option(pam_handle_t *_pamh, 7844269bb5Schristos const char *_option); 7944269bb5Schristos 8044269bb5Schristos int 8144269bb5Schristos openpam_restore_cred(pam_handle_t *_pamh) 8244269bb5Schristos OPENPAM_NONNULL((1)); 8344269bb5Schristos 8444269bb5Schristos int 8544269bb5Schristos openpam_set_option(pam_handle_t *_pamh, 8644269bb5Schristos const char *_option, 8744269bb5Schristos const char *_value); 8844269bb5Schristos 8944269bb5Schristos int 9044269bb5Schristos pam_error(const pam_handle_t *_pamh, 9144269bb5Schristos const char *_fmt, 9244269bb5Schristos ...) 931616498eSchristos OPENPAM_FORMAT ((__syslog__, 2, 3)) 9444269bb5Schristos OPENPAM_NONNULL((1,2)); 9544269bb5Schristos 9644269bb5Schristos int 9744269bb5Schristos pam_get_authtok(pam_handle_t *_pamh, 9844269bb5Schristos int _item, 9944269bb5Schristos const char **_authtok, 10044269bb5Schristos const char *_prompt) 10144269bb5Schristos OPENPAM_NONNULL((1,3)); 10244269bb5Schristos 10344269bb5Schristos int 10444269bb5Schristos pam_info(const pam_handle_t *_pamh, 10544269bb5Schristos const char *_fmt, 10644269bb5Schristos ...) 1071616498eSchristos OPENPAM_FORMAT ((__syslog__, 2, 3)) 10844269bb5Schristos OPENPAM_NONNULL((1,2)); 10944269bb5Schristos 11044269bb5Schristos int 11144269bb5Schristos pam_prompt(const pam_handle_t *_pamh, 11244269bb5Schristos int _style, 11344269bb5Schristos char **_resp, 11444269bb5Schristos const char *_fmt, 11544269bb5Schristos ...) 11644269bb5Schristos OPENPAM_FORMAT ((__printf__, 4, 5)) 11744269bb5Schristos OPENPAM_NONNULL((1,4)); 11844269bb5Schristos 11944269bb5Schristos int 12044269bb5Schristos pam_setenv(pam_handle_t *_pamh, 12144269bb5Schristos const char *_name, 12244269bb5Schristos const char *_value, 12344269bb5Schristos int _overwrite) 12444269bb5Schristos OPENPAM_NONNULL((1,2,3)); 12544269bb5Schristos 12644269bb5Schristos int 12744269bb5Schristos pam_vinfo(const pam_handle_t *_pamh, 12844269bb5Schristos const char *_fmt, 12944269bb5Schristos va_list _ap) 1301616498eSchristos OPENPAM_FORMAT ((__syslog__, 2, 0)) 13144269bb5Schristos OPENPAM_NONNULL((1,2)); 13244269bb5Schristos 13344269bb5Schristos int 13444269bb5Schristos pam_verror(const pam_handle_t *_pamh, 13544269bb5Schristos const char *_fmt, 13644269bb5Schristos va_list _ap) 1371616498eSchristos OPENPAM_FORMAT ((__syslog__, 2, 0)) 13844269bb5Schristos OPENPAM_NONNULL((1,2)); 13944269bb5Schristos 14044269bb5Schristos int 14144269bb5Schristos pam_vprompt(const pam_handle_t *_pamh, 14244269bb5Schristos int _style, 14344269bb5Schristos char **_resp, 14444269bb5Schristos const char *_fmt, 14544269bb5Schristos va_list _ap) 14644269bb5Schristos OPENPAM_FORMAT ((__printf__, 4, 0)) 14744269bb5Schristos OPENPAM_NONNULL((1,4)); 14844269bb5Schristos 14944269bb5Schristos /* 15044269bb5Schristos * Read cooked lines. 15144269bb5Schristos * Checking for _IOFBF is a fairly reliable way to detect the presence 15244269bb5Schristos * of <stdio.h>, as SUSv3 requires it to be defined there. 15344269bb5Schristos */ 15444269bb5Schristos #ifdef _IOFBF 15544269bb5Schristos char * 15644269bb5Schristos openpam_readline(FILE *_f, 15744269bb5Schristos int *_lineno, 15844269bb5Schristos size_t *_lenp) 15944269bb5Schristos OPENPAM_NONNULL((1)); 160e1b25b17Schristos 161e1b25b17Schristos char ** 162e1b25b17Schristos openpam_readlinev(FILE *_f, 163e1b25b17Schristos int *_lineno, 164e1b25b17Schristos int *_lenp) 165e1b25b17Schristos OPENPAM_NONNULL((1)); 166e1b25b17Schristos 167e1b25b17Schristos char * 168e1b25b17Schristos openpam_readword(FILE *_f, 169e1b25b17Schristos int *_lineno, 170e1b25b17Schristos size_t *_lenp) 171e1b25b17Schristos OPENPAM_NONNULL((1)); 17244269bb5Schristos #endif 17344269bb5Schristos 174e1b25b17Schristos int 175e1b25b17Schristos openpam_straddch(char **_str, 176e1b25b17Schristos size_t *_sizep, 177e1b25b17Schristos size_t *_lenp, 178e1b25b17Schristos int ch) 179e1b25b17Schristos OPENPAM_NONNULL((1)); 180e1b25b17Schristos 181e1b25b17Schristos /* 182e1b25b17Schristos * Enable / disable optional features 183e1b25b17Schristos */ 184e1b25b17Schristos enum { 185e1b25b17Schristos OPENPAM_RESTRICT_SERVICE_NAME, 186e1b25b17Schristos OPENPAM_VERIFY_POLICY_FILE, 187e1b25b17Schristos OPENPAM_RESTRICT_MODULE_NAME, 188e1b25b17Schristos OPENPAM_VERIFY_MODULE_FILE, 1894cb4af11Schristos OPENPAM_FALLBACK_TO_OTHER, 190e1b25b17Schristos OPENPAM_NUM_FEATURES 191e1b25b17Schristos }; 192e1b25b17Schristos 193e1b25b17Schristos int 194e1b25b17Schristos openpam_set_feature(int _feature, int _onoff); 195e1b25b17Schristos 196e1b25b17Schristos int 197e1b25b17Schristos openpam_get_feature(int _feature, int *_onoff); 198e1b25b17Schristos 19944269bb5Schristos /* 20044269bb5Schristos * Log levels 20144269bb5Schristos */ 20272d8d6c3Schristos enum openpam_log_primitives { 203e1b25b17Schristos PAM_LOG_LIBDEBUG = -1, 20444269bb5Schristos PAM_LOG_DEBUG, 20544269bb5Schristos PAM_LOG_VERBOSE, 20644269bb5Schristos PAM_LOG_NOTICE, 20744269bb5Schristos PAM_LOG_ERROR 20844269bb5Schristos }; 20944269bb5Schristos 21044269bb5Schristos /* 21144269bb5Schristos * Log to syslog 21244269bb5Schristos */ 21344269bb5Schristos void 21444269bb5Schristos _openpam_log(int _level, 21544269bb5Schristos const char *_func, 21644269bb5Schristos const char *_fmt, 21744269bb5Schristos ...) 2181616498eSchristos OPENPAM_FORMAT ((__syslog__, 3, 4)) 21944269bb5Schristos OPENPAM_NONNULL((3)); 22044269bb5Schristos 22144269bb5Schristos #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 22244269bb5Schristos #define openpam_log(lvl, ...) \ 22344269bb5Schristos _openpam_log((lvl), __func__, __VA_ARGS__) 22444269bb5Schristos #elif defined(__GNUC__) && (__GNUC__ >= 3) 22544269bb5Schristos #define openpam_log(lvl, ...) \ 22644269bb5Schristos _openpam_log((lvl), __func__, __VA_ARGS__) 22744269bb5Schristos #elif defined(__GNUC__) && (__GNUC__ >= 2) && (__GNUC_MINOR__ >= 95) 22844269bb5Schristos #define openpam_log(lvl, fmt...) \ 22944269bb5Schristos _openpam_log((lvl), __func__, ##fmt) 2301ae23164Schristos #elif defined(__GNUC__) && defined(__func__) 23144269bb5Schristos #define openpam_log(lvl, fmt...) \ 2321ae23164Schristos _openpam_log((lvl), __func__, ##fmt) 23344269bb5Schristos #else 23444269bb5Schristos void 23544269bb5Schristos openpam_log(int _level, 23644269bb5Schristos const char *_format, 23744269bb5Schristos ...) 2381616498eSchristos OPENPAM_FORMAT ((__syslog__, 2, 3)) 23944269bb5Schristos OPENPAM_NONNULL((2)); 24044269bb5Schristos #endif 24144269bb5Schristos 24244269bb5Schristos /* 24344269bb5Schristos * Generic conversation function 24444269bb5Schristos */ 24544269bb5Schristos struct pam_message; 24644269bb5Schristos struct pam_response; 24744269bb5Schristos int openpam_ttyconv(int _n, 24844269bb5Schristos const struct pam_message **_msg, 24944269bb5Schristos struct pam_response **_resp, 25044269bb5Schristos void *_data); 25144269bb5Schristos 25244269bb5Schristos extern int openpam_ttyconv_timeout; 25344269bb5Schristos 25444269bb5Schristos /* 25544269bb5Schristos * Null conversation function 25644269bb5Schristos */ 25744269bb5Schristos int openpam_nullconv(int _n, 25844269bb5Schristos const struct pam_message **_msg, 25944269bb5Schristos struct pam_response **_resp, 26044269bb5Schristos void *_data); 26144269bb5Schristos 26244269bb5Schristos /* 26344269bb5Schristos * PAM primitives 26444269bb5Schristos */ 26572d8d6c3Schristos enum openpam_sm_primitives { 26644269bb5Schristos PAM_SM_AUTHENTICATE, 26744269bb5Schristos PAM_SM_SETCRED, 26844269bb5Schristos PAM_SM_ACCT_MGMT, 26944269bb5Schristos PAM_SM_OPEN_SESSION, 27044269bb5Schristos PAM_SM_CLOSE_SESSION, 27144269bb5Schristos PAM_SM_CHAUTHTOK, 27244269bb5Schristos /* keep this last */ 27344269bb5Schristos PAM_NUM_PRIMITIVES 27444269bb5Schristos }; 27544269bb5Schristos 27644269bb5Schristos /* 27744269bb5Schristos * Dummy service module function 27844269bb5Schristos */ 27944269bb5Schristos #define PAM_SM_DUMMY(type) \ 28044269bb5Schristos PAM_EXTERN int \ 28144269bb5Schristos pam_sm_##type(pam_handle_t *pamh, int flags, \ 28244269bb5Schristos int argc, const char *argv[]) \ 28344269bb5Schristos { \ 28444269bb5Schristos \ 28544269bb5Schristos (void)pamh; \ 28644269bb5Schristos (void)flags; \ 28744269bb5Schristos (void)argc; \ 28844269bb5Schristos (void)argv; \ 28944269bb5Schristos return (PAM_IGNORE); \ 29044269bb5Schristos } 29144269bb5Schristos 29244269bb5Schristos /* 29344269bb5Schristos * PAM service module functions match this typedef 29444269bb5Schristos */ 29544269bb5Schristos struct pam_handle; 29644269bb5Schristos typedef int (*pam_func_t)(struct pam_handle *, int, int, const char **); 29744269bb5Schristos 29844269bb5Schristos /* 29944269bb5Schristos * A struct that describes a module. 30044269bb5Schristos */ 30144269bb5Schristos typedef struct pam_module pam_module_t; 30244269bb5Schristos struct pam_module { 30344269bb5Schristos char *path; 30444269bb5Schristos pam_func_t func[PAM_NUM_PRIMITIVES]; 30544269bb5Schristos void *dlh; 30644269bb5Schristos }; 30744269bb5Schristos 30844269bb5Schristos /* 30944269bb5Schristos * Source-code compatibility with Linux-PAM modules 31044269bb5Schristos */ 31144269bb5Schristos #if defined(PAM_SM_AUTH) || defined(PAM_SM_ACCOUNT) || \ 31244269bb5Schristos defined(PAM_SM_SESSION) || defined(PAM_SM_PASSWORD) 31344269bb5Schristos # define LINUX_PAM_MODULE 31444269bb5Schristos #endif 31544269bb5Schristos 31644269bb5Schristos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_AUTH) 31744269bb5Schristos # define _PAM_SM_AUTHENTICATE 0 31844269bb5Schristos # define _PAM_SM_SETCRED 0 31944269bb5Schristos #else 32044269bb5Schristos # undef PAM_SM_AUTH 32144269bb5Schristos # define PAM_SM_AUTH 32244269bb5Schristos # define _PAM_SM_AUTHENTICATE pam_sm_authenticate 32344269bb5Schristos # define _PAM_SM_SETCRED pam_sm_setcred 32444269bb5Schristos #endif 32544269bb5Schristos 32644269bb5Schristos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_ACCOUNT) 32744269bb5Schristos # define _PAM_SM_ACCT_MGMT 0 32844269bb5Schristos #else 32944269bb5Schristos # undef PAM_SM_ACCOUNT 33044269bb5Schristos # define PAM_SM_ACCOUNT 33144269bb5Schristos # define _PAM_SM_ACCT_MGMT pam_sm_acct_mgmt 33244269bb5Schristos #endif 33344269bb5Schristos 33444269bb5Schristos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_SESSION) 33544269bb5Schristos # define _PAM_SM_OPEN_SESSION 0 33644269bb5Schristos # define _PAM_SM_CLOSE_SESSION 0 33744269bb5Schristos #else 33844269bb5Schristos # undef PAM_SM_SESSION 33944269bb5Schristos # define PAM_SM_SESSION 34044269bb5Schristos # define _PAM_SM_OPEN_SESSION pam_sm_open_session 34144269bb5Schristos # define _PAM_SM_CLOSE_SESSION pam_sm_close_session 34244269bb5Schristos #endif 34344269bb5Schristos 34444269bb5Schristos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_PASSWORD) 34544269bb5Schristos # define _PAM_SM_CHAUTHTOK 0 34644269bb5Schristos #else 34744269bb5Schristos # undef PAM_SM_PASSWORD 34844269bb5Schristos # define PAM_SM_PASSWORD 34944269bb5Schristos # define _PAM_SM_CHAUTHTOK pam_sm_chauthtok 35044269bb5Schristos #endif 35144269bb5Schristos 35244269bb5Schristos /* 35344269bb5Schristos * Infrastructure for static modules using GCC linker sets. 35444269bb5Schristos * You are not expected to understand this. 35544269bb5Schristos */ 35644269bb5Schristos #if !defined(PAM_SOEXT) 35744269bb5Schristos # define PAM_SOEXT ".so" 35844269bb5Schristos #endif 35944269bb5Schristos 3600f6461fbSriastradh #if (defined(__GNUC__) || defined(__PCC__)) && defined(OPENPAM_STATIC_MODULES) 36144269bb5Schristos # include <sys/cdefs.h> 3621ae23164Schristos # ifdef __FreeBSD__ 36344269bb5Schristos # include <linker_set.h> 3641ae23164Schristos # endif 3651ae23164Schristos # ifdef __NetBSD__ 3661ae23164Schristos # define DATA_SET(a, b) __link_set_add_data(a, b) 3671ae23164Schristos # define SET_DECLARE(a, b) __link_set_decl(a, b) 3681ae23164Schristos # define SET_FOREACH(a, b) __link_set_foreach(a, b) 3691ae23164Schristos # endif 37044269bb5Schristos # define PAM_EXTERN static 37144269bb5Schristos # define PAM_MODULE_ENTRY(name) \ 37244269bb5Schristos static char _pam_name[] = name PAM_SOEXT; \ 37344269bb5Schristos static struct pam_module _pam_module = { \ 37444269bb5Schristos .path = _pam_name, \ 37544269bb5Schristos .func = { \ 37644269bb5Schristos [PAM_SM_AUTHENTICATE] = _PAM_SM_AUTHENTICATE, \ 37744269bb5Schristos [PAM_SM_SETCRED] = _PAM_SM_SETCRED, \ 37844269bb5Schristos [PAM_SM_ACCT_MGMT] = _PAM_SM_ACCT_MGMT, \ 37944269bb5Schristos [PAM_SM_OPEN_SESSION] = _PAM_SM_OPEN_SESSION, \ 38044269bb5Schristos [PAM_SM_CLOSE_SESSION] = _PAM_SM_CLOSE_SESSION, \ 38144269bb5Schristos [PAM_SM_CHAUTHTOK] = _PAM_SM_CHAUTHTOK \ 38244269bb5Schristos }, \ 38344269bb5Schristos }; \ 384a70b6cddSchristos DATA_SET(openpam_static_modules, _pam_module) 38544269bb5Schristos #else 38644269bb5Schristos /* normal case */ 38744269bb5Schristos # define PAM_EXTERN 38844269bb5Schristos # define PAM_MODULE_ENTRY(name) 38944269bb5Schristos #endif 39044269bb5Schristos 39144269bb5Schristos #ifdef __cplusplus 39244269bb5Schristos } 39344269bb5Schristos #endif 39444269bb5Schristos 39544269bb5Schristos #endif /* !SECURITY_OPENPAM_H_INCLUDED */ 396