Home | History | Annotate | Line # | Download | only in security
openpam.h revision 1.1
      1  1.1  christos /*	$NetBSD: openpam.h,v 1.1 2011/12/25 21:42:55 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
      5  1.1  christos  * Copyright (c) 2004-2011 Dag-Erling Smrgrav
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * This software was developed for the FreeBSD Project by ThinkSec AS and
      9  1.1  christos  * Network Associates Laboratories, the Security Research Division of
     10  1.1  christos  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
     11  1.1  christos  * ("CBOSS"), as part of the DARPA CHATS research program.
     12  1.1  christos  *
     13  1.1  christos  * Redistribution and use in source and binary forms, with or without
     14  1.1  christos  * modification, are permitted provided that the following conditions
     15  1.1  christos  * are met:
     16  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     18  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  christos  *    documentation and/or other materials provided with the distribution.
     21  1.1  christos  * 3. The name of the author may not be used to endorse or promote
     22  1.1  christos  *    products derived from this software without specific prior written
     23  1.1  christos  *    permission.
     24  1.1  christos  *
     25  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     29  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  christos  * SUCH DAMAGE.
     36  1.1  christos  *
     37  1.1  christos  * Id: openpam.h 455 2011-10-29 18:31:11Z des
     38  1.1  christos  */
     39  1.1  christos 
     40  1.1  christos #ifndef SECURITY_OPENPAM_H_INCLUDED
     41  1.1  christos #define SECURITY_OPENPAM_H_INCLUDED
     42  1.1  christos 
     43  1.1  christos /*
     44  1.1  christos  * Annoying but necessary header pollution
     45  1.1  christos  */
     46  1.1  christos #include <stdarg.h>
     47  1.1  christos 
     48  1.1  christos #include <security/openpam_attr.h>
     49  1.1  christos 
     50  1.1  christos #ifdef __cplusplus
     51  1.1  christos extern "C" {
     52  1.1  christos #endif
     53  1.1  christos 
     54  1.1  christos struct passwd;
     55  1.1  christos 
     56  1.1  christos /*
     57  1.1  christos  * API extensions
     58  1.1  christos  */
     59  1.1  christos int
     60  1.1  christos openpam_borrow_cred(pam_handle_t *_pamh,
     61  1.1  christos 	const struct passwd *_pwd)
     62  1.1  christos 	OPENPAM_NONNULL((1,2));
     63  1.1  christos 
     64  1.1  christos int
     65  1.1  christos openpam_subst(const pam_handle_t *_pamh,
     66  1.1  christos 	char *_buf,
     67  1.1  christos 	size_t *_bufsize,
     68  1.1  christos 	const char *_template);
     69  1.1  christos 
     70  1.1  christos void
     71  1.1  christos openpam_free_data(pam_handle_t *_pamh,
     72  1.1  christos 	void *_data,
     73  1.1  christos 	int _status);
     74  1.1  christos 
     75  1.1  christos void
     76  1.1  christos openpam_free_envlist(char **_envlist);
     77  1.1  christos 
     78  1.1  christos const char *
     79  1.1  christos openpam_get_option(pam_handle_t *_pamh,
     80  1.1  christos 	const char *_option);
     81  1.1  christos 
     82  1.1  christos int
     83  1.1  christos openpam_restore_cred(pam_handle_t *_pamh)
     84  1.1  christos 	OPENPAM_NONNULL((1));
     85  1.1  christos 
     86  1.1  christos int
     87  1.1  christos openpam_set_option(pam_handle_t *_pamh,
     88  1.1  christos 	const char *_option,
     89  1.1  christos 	const char *_value);
     90  1.1  christos 
     91  1.1  christos int
     92  1.1  christos pam_error(const pam_handle_t *_pamh,
     93  1.1  christos 	const char *_fmt,
     94  1.1  christos 	...)
     95  1.1  christos 	OPENPAM_FORMAT ((__printf__, 2, 3))
     96  1.1  christos 	OPENPAM_NONNULL((1,2));
     97  1.1  christos 
     98  1.1  christos int
     99  1.1  christos pam_get_authtok(pam_handle_t *_pamh,
    100  1.1  christos 	int _item,
    101  1.1  christos 	const char **_authtok,
    102  1.1  christos 	const char *_prompt)
    103  1.1  christos 	OPENPAM_NONNULL((1,3));
    104  1.1  christos 
    105  1.1  christos int
    106  1.1  christos pam_info(const pam_handle_t *_pamh,
    107  1.1  christos 	const char *_fmt,
    108  1.1  christos 	...)
    109  1.1  christos 	OPENPAM_FORMAT ((__printf__, 2, 3))
    110  1.1  christos 	OPENPAM_NONNULL((1,2));
    111  1.1  christos 
    112  1.1  christos int
    113  1.1  christos pam_prompt(const pam_handle_t *_pamh,
    114  1.1  christos 	int _style,
    115  1.1  christos 	char **_resp,
    116  1.1  christos 	const char *_fmt,
    117  1.1  christos 	...)
    118  1.1  christos 	OPENPAM_FORMAT ((__printf__, 4, 5))
    119  1.1  christos 	OPENPAM_NONNULL((1,4));
    120  1.1  christos 
    121  1.1  christos int
    122  1.1  christos pam_setenv(pam_handle_t *_pamh,
    123  1.1  christos 	const char *_name,
    124  1.1  christos 	const char *_value,
    125  1.1  christos 	int _overwrite)
    126  1.1  christos 	OPENPAM_NONNULL((1,2,3));
    127  1.1  christos 
    128  1.1  christos int
    129  1.1  christos pam_vinfo(const pam_handle_t *_pamh,
    130  1.1  christos 	const char *_fmt,
    131  1.1  christos 	va_list _ap)
    132  1.1  christos 	OPENPAM_FORMAT ((__printf__, 2, 0))
    133  1.1  christos 	OPENPAM_NONNULL((1,2));
    134  1.1  christos 
    135  1.1  christos int
    136  1.1  christos pam_verror(const pam_handle_t *_pamh,
    137  1.1  christos 	const char *_fmt,
    138  1.1  christos 	va_list _ap)
    139  1.1  christos 	OPENPAM_FORMAT ((__printf__, 2, 0))
    140  1.1  christos 	OPENPAM_NONNULL((1,2));
    141  1.1  christos 
    142  1.1  christos int
    143  1.1  christos pam_vprompt(const pam_handle_t *_pamh,
    144  1.1  christos 	int _style,
    145  1.1  christos 	char **_resp,
    146  1.1  christos 	const char *_fmt,
    147  1.1  christos 	va_list _ap)
    148  1.1  christos 	OPENPAM_FORMAT ((__printf__, 4, 0))
    149  1.1  christos 	OPENPAM_NONNULL((1,4));
    150  1.1  christos 
    151  1.1  christos /*
    152  1.1  christos  * Read cooked lines.
    153  1.1  christos  * Checking for _IOFBF is a fairly reliable way to detect the presence
    154  1.1  christos  * of <stdio.h>, as SUSv3 requires it to be defined there.
    155  1.1  christos  */
    156  1.1  christos #ifdef _IOFBF
    157  1.1  christos char *
    158  1.1  christos openpam_readline(FILE *_f,
    159  1.1  christos 	int *_lineno,
    160  1.1  christos 	size_t *_lenp)
    161  1.1  christos 	OPENPAM_NONNULL((1));
    162  1.1  christos #endif
    163  1.1  christos 
    164  1.1  christos /*
    165  1.1  christos  * Log levels
    166  1.1  christos  */
    167  1.1  christos enum {
    168  1.1  christos 	PAM_LOG_DEBUG,
    169  1.1  christos 	PAM_LOG_VERBOSE,
    170  1.1  christos 	PAM_LOG_NOTICE,
    171  1.1  christos 	PAM_LOG_ERROR
    172  1.1  christos };
    173  1.1  christos 
    174  1.1  christos /*
    175  1.1  christos  * Log to syslog
    176  1.1  christos  */
    177  1.1  christos void
    178  1.1  christos _openpam_log(int _level,
    179  1.1  christos 	const char *_func,
    180  1.1  christos 	const char *_fmt,
    181  1.1  christos 	...)
    182  1.1  christos 	OPENPAM_FORMAT ((__printf__, 3, 4))
    183  1.1  christos 	OPENPAM_NONNULL((3));
    184  1.1  christos 
    185  1.1  christos #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
    186  1.1  christos #define openpam_log(lvl, ...) \
    187  1.1  christos 	_openpam_log((lvl), __func__, __VA_ARGS__)
    188  1.1  christos #elif defined(__GNUC__) && (__GNUC__ >= 3)
    189  1.1  christos #define openpam_log(lvl, ...) \
    190  1.1  christos 	_openpam_log((lvl), __func__, __VA_ARGS__)
    191  1.1  christos #elif defined(__GNUC__) && (__GNUC__ >= 2) && (__GNUC_MINOR__ >= 95)
    192  1.1  christos #define openpam_log(lvl, fmt...) \
    193  1.1  christos 	_openpam_log((lvl), __func__, ##fmt)
    194  1.1  christos #elif defined(__GNUC__) && defined(__FUNCTION__)
    195  1.1  christos #define openpam_log(lvl, fmt...) \
    196  1.1  christos 	_openpam_log((lvl), __FUNCTION__, ##fmt)
    197  1.1  christos #else
    198  1.1  christos void
    199  1.1  christos openpam_log(int _level,
    200  1.1  christos 	const char *_format,
    201  1.1  christos  	...)
    202  1.1  christos  	OPENPAM_FORMAT ((__printf__, 2, 3))
    203  1.1  christos 	OPENPAM_NONNULL((2));
    204  1.1  christos #endif
    205  1.1  christos 
    206  1.1  christos /*
    207  1.1  christos  * Generic conversation function
    208  1.1  christos  */
    209  1.1  christos struct pam_message;
    210  1.1  christos struct pam_response;
    211  1.1  christos int openpam_ttyconv(int _n,
    212  1.1  christos 	const struct pam_message **_msg,
    213  1.1  christos 	struct pam_response **_resp,
    214  1.1  christos 	void *_data);
    215  1.1  christos 
    216  1.1  christos extern int openpam_ttyconv_timeout;
    217  1.1  christos 
    218  1.1  christos /*
    219  1.1  christos  * Null conversation function
    220  1.1  christos  */
    221  1.1  christos int openpam_nullconv(int _n,
    222  1.1  christos 	const struct pam_message **_msg,
    223  1.1  christos 	struct pam_response **_resp,
    224  1.1  christos 	void *_data);
    225  1.1  christos 
    226  1.1  christos /*
    227  1.1  christos  * PAM primitives
    228  1.1  christos  */
    229  1.1  christos enum {
    230  1.1  christos 	PAM_SM_AUTHENTICATE,
    231  1.1  christos 	PAM_SM_SETCRED,
    232  1.1  christos 	PAM_SM_ACCT_MGMT,
    233  1.1  christos 	PAM_SM_OPEN_SESSION,
    234  1.1  christos 	PAM_SM_CLOSE_SESSION,
    235  1.1  christos 	PAM_SM_CHAUTHTOK,
    236  1.1  christos 	/* keep this last */
    237  1.1  christos 	PAM_NUM_PRIMITIVES
    238  1.1  christos };
    239  1.1  christos 
    240  1.1  christos /*
    241  1.1  christos  * Dummy service module function
    242  1.1  christos  */
    243  1.1  christos #define PAM_SM_DUMMY(type)						\
    244  1.1  christos PAM_EXTERN int								\
    245  1.1  christos pam_sm_##type(pam_handle_t *pamh, int flags,				\
    246  1.1  christos     int argc, const char *argv[])					\
    247  1.1  christos {									\
    248  1.1  christos 									\
    249  1.1  christos 	(void)pamh;							\
    250  1.1  christos 	(void)flags;							\
    251  1.1  christos 	(void)argc;							\
    252  1.1  christos 	(void)argv;							\
    253  1.1  christos 	return (PAM_IGNORE);						\
    254  1.1  christos }
    255  1.1  christos 
    256  1.1  christos /*
    257  1.1  christos  * PAM service module functions match this typedef
    258  1.1  christos  */
    259  1.1  christos struct pam_handle;
    260  1.1  christos typedef int (*pam_func_t)(struct pam_handle *, int, int, const char **);
    261  1.1  christos 
    262  1.1  christos /*
    263  1.1  christos  * A struct that describes a module.
    264  1.1  christos  */
    265  1.1  christos typedef struct pam_module pam_module_t;
    266  1.1  christos struct pam_module {
    267  1.1  christos 	char		*path;
    268  1.1  christos 	pam_func_t	 func[PAM_NUM_PRIMITIVES];
    269  1.1  christos 	void		*dlh;
    270  1.1  christos };
    271  1.1  christos 
    272  1.1  christos /*
    273  1.1  christos  * Source-code compatibility with Linux-PAM modules
    274  1.1  christos  */
    275  1.1  christos #if defined(PAM_SM_AUTH) || defined(PAM_SM_ACCOUNT) || \
    276  1.1  christos 	defined(PAM_SM_SESSION) || defined(PAM_SM_PASSWORD)
    277  1.1  christos # define LINUX_PAM_MODULE
    278  1.1  christos #endif
    279  1.1  christos 
    280  1.1  christos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_AUTH)
    281  1.1  christos # define _PAM_SM_AUTHENTICATE	0
    282  1.1  christos # define _PAM_SM_SETCRED	0
    283  1.1  christos #else
    284  1.1  christos # undef PAM_SM_AUTH
    285  1.1  christos # define PAM_SM_AUTH
    286  1.1  christos # define _PAM_SM_AUTHENTICATE	pam_sm_authenticate
    287  1.1  christos # define _PAM_SM_SETCRED	pam_sm_setcred
    288  1.1  christos #endif
    289  1.1  christos 
    290  1.1  christos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_ACCOUNT)
    291  1.1  christos # define _PAM_SM_ACCT_MGMT	0
    292  1.1  christos #else
    293  1.1  christos # undef PAM_SM_ACCOUNT
    294  1.1  christos # define PAM_SM_ACCOUNT
    295  1.1  christos # define _PAM_SM_ACCT_MGMT	pam_sm_acct_mgmt
    296  1.1  christos #endif
    297  1.1  christos 
    298  1.1  christos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_SESSION)
    299  1.1  christos # define _PAM_SM_OPEN_SESSION	0
    300  1.1  christos # define _PAM_SM_CLOSE_SESSION	0
    301  1.1  christos #else
    302  1.1  christos # undef PAM_SM_SESSION
    303  1.1  christos # define PAM_SM_SESSION
    304  1.1  christos # define _PAM_SM_OPEN_SESSION	pam_sm_open_session
    305  1.1  christos # define _PAM_SM_CLOSE_SESSION	pam_sm_close_session
    306  1.1  christos #endif
    307  1.1  christos 
    308  1.1  christos #if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_PASSWORD)
    309  1.1  christos # define _PAM_SM_CHAUTHTOK	0
    310  1.1  christos #else
    311  1.1  christos # undef PAM_SM_PASSWORD
    312  1.1  christos # define PAM_SM_PASSWORD
    313  1.1  christos # define _PAM_SM_CHAUTHTOK	pam_sm_chauthtok
    314  1.1  christos #endif
    315  1.1  christos 
    316  1.1  christos /*
    317  1.1  christos  * Infrastructure for static modules using GCC linker sets.
    318  1.1  christos  * You are not expected to understand this.
    319  1.1  christos  */
    320  1.1  christos #if !defined(PAM_SOEXT)
    321  1.1  christos # define PAM_SOEXT ".so"
    322  1.1  christos #endif
    323  1.1  christos 
    324  1.1  christos #if defined(OPENPAM_STATIC_MODULES)
    325  1.1  christos # if !defined(__GNUC__)
    326  1.1  christos #  error "Don't know how to build static modules on non-GNU compilers"
    327  1.1  christos # endif
    328  1.1  christos /* gcc, static linking */
    329  1.1  christos # include <sys/cdefs.h>
    330  1.1  christos # include <linker_set.h>
    331  1.1  christos # define PAM_EXTERN static
    332  1.1  christos # define PAM_MODULE_ENTRY(name)						\
    333  1.1  christos 	static char _pam_name[] = name PAM_SOEXT;			\
    334  1.1  christos 	static struct pam_module _pam_module = {			\
    335  1.1  christos 		.path = _pam_name,					\
    336  1.1  christos 		.func = {						\
    337  1.1  christos 			[PAM_SM_AUTHENTICATE] = _PAM_SM_AUTHENTICATE,	\
    338  1.1  christos 			[PAM_SM_SETCRED] = _PAM_SM_SETCRED,		\
    339  1.1  christos 			[PAM_SM_ACCT_MGMT] = _PAM_SM_ACCT_MGMT,		\
    340  1.1  christos 			[PAM_SM_OPEN_SESSION] = _PAM_SM_OPEN_SESSION,	\
    341  1.1  christos 			[PAM_SM_CLOSE_SESSION] = _PAM_SM_CLOSE_SESSION, \
    342  1.1  christos 			[PAM_SM_CHAUTHTOK] = _PAM_SM_CHAUTHTOK		\
    343  1.1  christos 		},							\
    344  1.1  christos 	};								\
    345  1.1  christos 	DATA_SET(_openpam_static_modules, _pam_module)
    346  1.1  christos #else
    347  1.1  christos /* normal case */
    348  1.1  christos # define PAM_EXTERN
    349  1.1  christos # define PAM_MODULE_ENTRY(name)
    350  1.1  christos #endif
    351  1.1  christos 
    352  1.1  christos #ifdef __cplusplus
    353  1.1  christos }
    354  1.1  christos #endif
    355  1.1  christos 
    356  1.1  christos #endif /* !SECURITY_OPENPAM_H_INCLUDED */
    357