openpam.h revision 94562
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 2002 Networks Associates Technology, Inc. 31541Srgrimes * All rights reserved. 41541Srgrimes * 51541Srgrimes * This software was developed for the FreeBSD Project by ThinkSec AS and 61541Srgrimes * NAI Labs, the Security Research Division of Network Associates, Inc. 71541Srgrimes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 81541Srgrimes * DARPA CHATS research program. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 3. The name of the author may not be used to endorse or promote 191541Srgrimes * products derived from this software without specific prior written 201541Srgrimes * permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 341541Srgrimes * $P4: //depot/projects/openpam/include/security/openpam.h#17 $ 351541Srgrimes */ 3622521Sdyson 3750477Speter#ifndef _SECURITY_OPENPAM_H_INCLUDED 381541Srgrimes#define _SECURITY_OPENPAM_H_INCLUDED 391541Srgrimes 4022521Sdyson/* 412175Spaul * Annoying but necessary header pollution 422175Spaul */ 432175Spaul#include <stdarg.h> 441541Srgrimes 451541Srgrimes#ifdef __cplusplus 461541Srgrimesextern "C" { 471541Srgrimes#endif 481541Srgrimes 491541Srgrimesstruct passwd; 501541Srgrimes 511541Srgrimes/* 521541Srgrimes * API extensions 531541Srgrimes */ 541541Srgrimesint 5536541Speteropenpam_borrow_cred(pam_handle_t *_pamh, 5636541Speter const struct passwd *_pwd); 571541Srgrimes 5850053Spetervoid 5950053Speteropenpam_free_data(pam_handle_t *_pamh, 6018397Snate void *_data, 619336Sdfr int _status); 6218397Snate 635472Sdgconst char * 6418397Snateopenpam_get_option(pam_handle_t *_pamh, 6550053Speter const char *_option); 6650053Speter 6750053Speterint 6818397Snateopenpam_restore_cred(pam_handle_t *_pamh); 6918397Snate 7018397Snateint 715472Sdgopenpam_set_option(pam_handle_t *_pamh, 729336Sdfr const char *_option, 7350053Speter const char *_value); 741541Srgrimes 7550053Speterint 7650053Speterpam_error(pam_handle_t *_pamh, 7718397Snate const char *_fmt, 7818397Snate ...); 7950053Speter 8050053Speterint 8150053Speterpam_get_authtok(pam_handle_t *_pamh, 8218397Snate int _item, 8318397Snate const char **_authtok, 8450053Speter const char *_prompt); 851541Srgrimes 8647751Speterint 8747751Speterpam_info(pam_handle_t *_pamh, 8847751Speter const char *_fmt, 8950053Speter ...); 9050053Speter 9147751Speterint 9247751Speterpam_prompt(pam_handle_t *_pamh, 9350053Speter int _style, 942175Spaul char **_resp, 952175Spaul const char *_fmt, 96 ...); 97 98int 99pam_setenv(pam_handle_t *_pamh, 100 const char *_name, 101 const char *_value, 102 int _overwrite); 103 104int 105pam_vinfo(pam_handle_t *_pamh, 106 const char *_fmt, 107 va_list _ap); 108 109int 110pam_verror(pam_handle_t *_pamh, 111 const char *_fmt, 112 va_list _ap); 113 114int 115pam_vprompt(pam_handle_t *_pamh, 116 int _style, 117 char **_resp, 118 const char *_fmt, 119 va_list _ap); 120 121/* 122 * Log levels 123 */ 124enum { 125 PAM_LOG_DEBUG, 126 PAM_LOG_VERBOSE, 127 PAM_LOG_NOTICE, 128 PAM_LOG_ERROR 129}; 130 131/* 132 * Log to syslog 133 */ 134void 135_openpam_log(int _level, 136 const char *_func, 137 const char *_fmt, 138 ...); 139 140#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L) 141#define openpam_log(lvl, fmt, ...) \ 142 _openpam_log((lvl), __func__, fmt, __VA_ARGS__) 143#elif defined(__GNUC__) && (__GNUC__ >= 2) && (__GNUC_MINOR__ >= 95) 144#define openpam_log(lvl, fmt...) \ 145 _openpam_log((lvl), __func__, fmt, ##fmt) 146#elif defined(__GNUC__) && defined(__FUNCTION__) 147#define openpam_log(lvl, fmt...) \ 148 _openpam_log((lvl), __FUNCTION__, ##fmt) 149#else 150void 151openpam_log(int _level, 152 const char *_format, 153 ...); 154#endif 155 156/* 157 * Generic conversation function 158 */ 159struct pam_message; 160struct pam_response; 161int openpam_ttyconv(int _n, 162 const struct pam_message **_msg, 163 struct pam_response **_resp, 164 void *_data); 165 166/* 167 * PAM primitives 168 */ 169enum { 170 PAM_SM_AUTHENTICATE, 171 PAM_SM_SETCRED, 172 PAM_SM_ACCT_MGMT, 173 PAM_SM_OPEN_SESSION, 174 PAM_SM_CLOSE_SESSION, 175 PAM_SM_CHAUTHTOK, 176 /* keep this last */ 177 PAM_NUM_PRIMITIVES 178}; 179 180/* 181 * Dummy service module function 182 */ 183#define PAM_SM_DUMMY(type) \ 184PAM_EXTERN int \ 185pam_sm_##type(pam_handle_t *pamh, int flags, \ 186 int argc, const char *argv[]) \ 187{ \ 188 return (PAM_IGNORE); \ 189} 190 191/* 192 * PAM service module functions match this typedef 193 */ 194struct pam_handle; 195typedef int (*pam_func_t)(struct pam_handle *, int, int, const char **); 196 197/* 198 * A struct that describes a module. 199 */ 200typedef struct pam_module pam_module_t; 201struct pam_module { 202 char *path; 203 pam_func_t func[PAM_NUM_PRIMITIVES]; 204 void *dlh; 205 int refcount; 206 pam_module_t *prev; 207 pam_module_t *next; 208}; 209 210/* 211 * Source-code compatibility with Linux-PAM modules 212 */ 213#if defined(PAM_SM_AUTH) || defined(PAM_SM_ACCOUNT) || \ 214 defined(PAM_SM_SESSION) || defined(PAM_SM_PASSWORD) 215#define LINUX_PAM_MODULE 216#endif 217#if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_AUTH) 218#define _PAM_SM_AUTHENTICATE 0 219#define _PAM_SM_SETCRED 0 220#else 221#undef PAM_SM_AUTH 222#define PAM_SM_AUTH 223#define _PAM_SM_AUTHENTICATE pam_sm_authenticate 224#define _PAM_SM_SETCRED pam_sm_setcred 225#endif 226#if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_ACCOUNT) 227#define _PAM_SM_ACCT_MGMT 0 228#else 229#undef PAM_SM_ACCOUNT 230#define PAM_SM_ACCOUNT 231#define _PAM_SM_ACCT_MGMT pam_sm_acct_mgmt 232#endif 233#if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_SESSION) 234#define _PAM_SM_OPEN_SESSION 0 235#define _PAM_SM_CLOSE_SESSION 0 236#else 237#undef PAM_SM_SESSION 238#define PAM_SM_SESSION 239#define _PAM_SM_OPEN_SESSION pam_sm_open_session 240#define _PAM_SM_CLOSE_SESSION pam_sm_close_session 241#endif 242#if defined(LINUX_PAM_MODULE) && !defined(PAM_SM_PASSWORD) 243#define _PAM_SM_CHAUTHTOK 0 244#else 245#undef PAM_SM_PASSWORD 246#define PAM_SM_PASSWORD 247#define _PAM_SM_CHAUTHTOK pam_sm_chauthtok 248#endif 249 250/* 251 * Infrastructure for static modules using GCC linker sets. 252 * You are not expected to understand this. 253 */ 254#if defined(__FreeBSD__) 255#define PAM_SOEXT ".so" 256#else 257#ifndef NO_STATIC_MODULES 258#define NO_STATIC_MODULES 259#endif 260#endif 261#if defined(__GNUC__) && !defined(__PIC__) && !defined(NO_STATIC_MODULES) 262/* gcc, static linking */ 263#include <sys/cdefs.h> 264#include <linker_set.h> 265#define OPENPAM_STATIC_MODULES 266#define PAM_EXTERN static 267#define PAM_MODULE_ENTRY(name) \ 268static char _pam_name[] = name PAM_SOEXT; \ 269static struct pam_module _pam_module = { _pam_name, { \ 270 _PAM_SM_AUTHENTICATE, _PAM_SM_SETCRED, _PAM_SM_ACCT_MGMT, \ 271 _PAM_SM_OPEN_SESSION, _PAM_SM_CLOSE_SESSION, _PAM_SM_CHAUTHTOK }, \ 272 NULL, 0, NULL, NULL }; \ 273DATA_SET(_openpam_static_modules, _pam_module) 274#else 275/* normal case */ 276#define PAM_EXTERN 277#define PAM_MODULE_ENTRY(name) 278#endif 279 280#ifdef __cplusplus 281} 282#endif 283 284#endif 285