| 1 | /*- |
|---|
| 2 | * Copyright (c) 2002 Networks Associates Technology, Inc. |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * This software was developed for the FreeBSD Project by ThinkSec AS and |
|---|
| 6 | * NAI Labs, the Security Research Division of Network Associates, Inc. |
|---|
| 7 | * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the |
|---|
| 8 | * DARPA CHATS research program. |
|---|
| 9 | * |
|---|
| 10 | * Redistribution and use in source and binary forms, with or without |
|---|
| 11 | * modification, are permitted provided that the following conditions |
|---|
| 12 | * are met: |
|---|
| 13 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 14 | * notice, this list of conditions and the following disclaimer. |
|---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 16 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 17 | * documentation and/or other materials provided with the distribution. |
|---|
| 18 | * 3. The name of the author may not be used to endorse or promote |
|---|
| 19 | * products derived from this software without specific prior written |
|---|
| 20 | * permission. |
|---|
| 21 | * |
|---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 32 | * SUCH DAMAGE. |
|---|
| 33 | * |
|---|
| 34 | * $P4: //depot/projects/openpam/include/security/openpam.h#15 $ |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | #ifndef _SECURITY_OPENPAM_H_INCLUDED |
|---|
| 38 | #define _SECURITY_OPENPAM_H_INCLUDED |
|---|
| 39 | |
|---|
| 40 | /* |
|---|
| 41 | * Annoying but necessary header pollution |
|---|
| 42 | */ |
|---|
| 43 | #include <stdarg.h> |
|---|
| 44 | |
|---|
| 45 | #ifdef __cplusplus |
|---|
| 46 | extern "C" { |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | struct passwd; |
|---|
| 50 | |
|---|
| 51 | /* |
|---|
| 52 | * API extensions |
|---|
| 53 | */ |
|---|
| 54 | int |
|---|
| 55 | openpam_borrow_cred(pam_handle_t *_pamh, |
|---|
| 56 | const struct passwd *_pwd); |
|---|
| 57 | |
|---|
| 58 | void |
|---|
| 59 | openpam_free_data(pam_handle_t *_pamh, |
|---|
| 60 | void *_data, |
|---|
| 61 | int _status); |
|---|
| 62 | |
|---|
| 63 | const char * |
|---|
| 64 | openpam_get_option(pam_handle_t *_pamh, |
|---|
| 65 | const char *_option); |
|---|
| 66 | |
|---|
| 67 | int |
|---|
| 68 | openpam_restore_cred(pam_handle_t *_pamh); |
|---|
| 69 | |
|---|
| 70 | int |
|---|
| 71 | openpam_set_option(pam_handle_t *_pamh, |
|---|
| 72 | const char *_option, |
|---|
| 73 | const char *_value); |
|---|
| 74 | |
|---|
| 75 | int |
|---|
| 76 | pam_error(pam_handle_t *_pamh, |
|---|
| 77 | const char *_fmt, |
|---|
| 78 | ...); |
|---|
| 79 | |
|---|
| 80 | int |
|---|
| 81 | pam_get_authtok(pam_handle_t *_pamh, |
|---|
| 82 | int _item, |
|---|
| 83 | const char **_authtok, |
|---|
| 84 | const char *_prompt); |
|---|
| 85 | |
|---|
| 86 | int |
|---|
| 87 | pam_info(pam_handle_t *_pamh, |
|---|
| 88 | const char *_fmt, |
|---|
| 89 | ...); |
|---|
| 90 | |
|---|
| 91 | int |
|---|
| 92 | pam_prompt(pam_handle_t *_pamh, |
|---|
| 93 | int _style, |
|---|
| 94 | char **_resp, |
|---|
| 95 | const char *_fmt, |
|---|
| 96 | ...); |
|---|
| 97 | |
|---|
| 98 | int |
|---|
| 99 | pam_setenv(pam_handle_t *_pamh, |
|---|
| 100 | const char *_name, |
|---|
| 101 | const char *_value, |
|---|
| 102 | int _overwrite); |
|---|
| 103 | |
|---|
| 104 | int |
|---|
| 105 | pam_vinfo(pam_handle_t *_pamh, |
|---|
| 106 | const char *_fmt, |
|---|
| 107 | va_list _ap); |
|---|
| 108 | |
|---|
| 109 | int |
|---|
| 110 | pam_verror(pam_handle_t *_pamh, |
|---|
| 111 | const char *_fmt, |
|---|
| 112 | va_list _ap); |
|---|
| 113 | |
|---|
| 114 | int |
|---|
| 115 | pam_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 | */ |
|---|
| 124 | enum { |
|---|
| 125 | PAM_LOG_DEBUG, |
|---|
| 126 | PAM_LOG_VERBOSE, |
|---|
| 127 | PAM_LOG_NOTICE, |
|---|
| 128 | PAM_LOG_ERROR |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | /* |
|---|
| 132 | * Log to syslog |
|---|
| 133 | */ |
|---|
| 134 | void |
|---|
| 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 |
|---|
| 150 | void |
|---|
| 151 | openpam_log(int _level, |
|---|
| 152 | const char *_format, |
|---|
| 153 | ...); |
|---|
| 154 | #endif |
|---|
| 155 | |
|---|
| 156 | /* |
|---|
| 157 | * Generic conversation function |
|---|
| 158 | */ |
|---|
| 159 | struct pam_message; |
|---|
| 160 | struct pam_response; |
|---|
| 161 | int openpam_ttyconv(int _n, |
|---|
| 162 | const struct pam_message **_msg, |
|---|
| 163 | struct pam_response **_resp, |
|---|
| 164 | void *_data); |
|---|
| 165 | |
|---|
| 166 | /* |
|---|
| 167 | * PAM primitives |
|---|
| 168 | */ |
|---|
| 169 | enum { |
|---|
| 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) \ |
|---|
| 184 | PAM_EXTERN int \ |
|---|
| 185 | pam_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 | */ |
|---|
| 194 | struct pam_handle; |
|---|
| 195 | typedef int (*pam_func_t)(struct pam_handle *, int, int, const char **); |
|---|
| 196 | |
|---|
| 197 | /* |
|---|
| 198 | * A struct that describes a module. |
|---|
| 199 | */ |
|---|
| 200 | typedef struct pam_module pam_module_t; |
|---|
| 201 | struct 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 | * Infrastructure for static modules using GCC linker sets. |
|---|
| 212 | * You are not expected to understand this. |
|---|
| 213 | */ |
|---|
| 214 | #if defined(__FreeBSD__) |
|---|
| 215 | #define PAM_SOEXT ".so" |
|---|
| 216 | #else |
|---|
| 217 | #ifndef NO_STATIC_MODULES |
|---|
| 218 | #define NO_STATIC_MODULES |
|---|
| 219 | #endif |
|---|
| 220 | #endif |
|---|
| 221 | #if defined(__GNUC__) && !defined(__PIC__) && !defined(NO_STATIC_MODULES) |
|---|
| 222 | /* gcc, static linking */ |
|---|
| 223 | #include <sys/cdefs.h> |
|---|
| 224 | #include <linker_set.h> |
|---|
| 225 | #define OPENPAM_STATIC_MODULES |
|---|
| 226 | #define PAM_EXTERN static |
|---|
| 227 | #define PAM_MODULE_ENTRY(name) \ |
|---|
| 228 | static char _pam_name[] = name PAM_SOEXT; \ |
|---|
| 229 | static struct pam_module _pam_module = { _pam_name, { \ |
|---|
| 230 | pam_sm_authenticate, pam_sm_setcred, pam_sm_acct_mgmt, \ |
|---|
| 231 | pam_sm_open_session, pam_sm_close_session, pam_sm_chauthtok }, \ |
|---|
| 232 | NULL, 0, NULL, NULL }; \ |
|---|
| 233 | DATA_SET(_openpam_static_modules, _pam_module) |
|---|
| 234 | #else |
|---|
| 235 | /* normal case */ |
|---|
| 236 | #define PAM_EXTERN |
|---|
| 237 | #define PAM_MODULE_ENTRY(name) |
|---|
| 238 | #endif |
|---|
| 239 | |
|---|
| 240 | #ifdef __cplusplus |
|---|
| 241 | } |
|---|
| 242 | #endif |
|---|
| 243 | |
|---|
| 244 | #endif |
|---|