| 1 | /*- |
|---|
| 2 | * Copyright (c) 2002 Networks Associates Technologies, 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 | * $Id$ |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | #include <dlfcn.h> |
|---|
| 38 | #include <stdlib.h> |
|---|
| 39 | #include <string.h> |
|---|
| 40 | |
|---|
| 41 | #include <security/pam_appl.h> |
|---|
| 42 | |
|---|
| 43 | #include "openpam_impl.h" |
|---|
| 44 | |
|---|
| 45 | #ifdef OPENPAM_STATIC_MODULES |
|---|
| 46 | SET_DECLARE(_openpam_modules, pam_module_t); |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | const char *_pam_sm_func_name[PAM_NUM_PRIMITIVES] = { |
|---|
| 50 | "pam_sm_authenticate", |
|---|
| 51 | "pam_sm_setcred", |
|---|
| 52 | "pam_sm_acct_mgmt", |
|---|
| 53 | "pam_sm_open_session", |
|---|
| 54 | "pam_sm_close_session", |
|---|
| 55 | "pam_sm_chauthtok" |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | static pam_module_t *modules; |
|---|
| 59 | |
|---|
| 60 | /* |
|---|
| 61 | * Load a dynamic module, or locate a static one. Keep a list of |
|---|
| 62 | * previously found modules to speed up the process. |
|---|
| 63 | */ |
|---|
| 64 | |
|---|
| 65 | static pam_module_t * |
|---|
| 66 | openpam_load_module(const char *path) |
|---|
| 67 | { |
|---|
| 68 | pam_module_t *module; |
|---|
| 69 | void *dlh; |
|---|
| 70 | int i; |
|---|
| 71 | |
|---|
| 72 | /* check cache first */ |
|---|
| 73 | for (module = modules; module != NULL; module = module->next) |
|---|
| 74 | if (strcmp(module->path, path) == 0) |
|---|
| 75 | goto found; |
|---|
| 76 | |
|---|
| 77 | /* nope; try to load */ |
|---|
| 78 | if ((dlh = dlopen(path, RTLD_NOW)) == NULL) { |
|---|
| 79 | openpam_log(PAM_LOG_ERROR, "dlopen(): %s", dlerror()); |
|---|
| 80 | } else { |
|---|
| 81 | if ((module = calloc(1, sizeof *module)) == NULL) |
|---|
| 82 | goto buf_err; |
|---|
| 83 | if ((module->path = strdup(path)) == NULL) |
|---|
| 84 | goto buf_err; |
|---|
| 85 | module->dlh = dlh; |
|---|
| 86 | for (i = 0; i < PAM_NUM_PRIMITIVES; ++i) |
|---|
| 87 | module->func[i] = dlsym(dlh, _pam_sm_func_name[i]); |
|---|
| 88 | } |
|---|
| 89 | openpam_log(PAM_LOG_DEBUG, "%s dynamic %s", |
|---|
| 90 | (module == NULL) ? "no" : "using", path); |
|---|
| 91 | |
|---|
| 92 | #ifdef OPENPAM_STATIC_MODULES |
|---|
| 93 | /* look for a static module */ |
|---|
| 94 | if (module == NULL && strchr(path, '/') == NULL) { |
|---|
| 95 | pam_module_t **modp; |
|---|
| 96 | |
|---|
| 97 | SET_FOREACH(modp, _openpam_modules) { |
|---|
| 98 | if (strcmp((*modp)->path, path) == 0) { |
|---|
| 99 | module = *modp; |
|---|
| 100 | break; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | openpam_log(PAM_LOG_DEBUG, "%s static %s", |
|---|
| 104 | (module == NULL) ? "no" : "using", path); |
|---|
| 105 | } |
|---|
| 106 | #endif |
|---|
| 107 | if (module == NULL) |
|---|
| 108 | return (NULL); |
|---|
| 109 | module->next = modules; |
|---|
| 110 | module->prev = NULL; |
|---|
| 111 | modules = module; |
|---|
| 112 | found: |
|---|
| 113 | ++module->refcount; |
|---|
| 114 | return (module); |
|---|
| 115 | buf_err: |
|---|
| 116 | openpam_log(PAM_LOG_ERROR, "malloc(): %m"); |
|---|
| 117 | dlclose(dlh); |
|---|
| 118 | free(module); |
|---|
| 119 | return (NULL); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /* |
|---|
| 124 | * Release a module. |
|---|
| 125 | * XXX highly thread-unsafe |
|---|
| 126 | */ |
|---|
| 127 | |
|---|
| 128 | static void |
|---|
| 129 | openpam_release_module(pam_module_t *module) |
|---|
| 130 | { |
|---|
| 131 | if (module == NULL) |
|---|
| 132 | return; |
|---|
| 133 | --module->refcount; |
|---|
| 134 | if (module->refcount > 0) |
|---|
| 135 | /* still in use */ |
|---|
| 136 | return; |
|---|
| 137 | if (module->refcount < 0) { |
|---|
| 138 | openpam_log(PAM_LOG_ERROR, "module %s has negative refcount", |
|---|
| 139 | module->path); |
|---|
| 140 | module->refcount = 0; |
|---|
| 141 | } |
|---|
| 142 | if (module->dlh == NULL) |
|---|
| 143 | /* static module */ |
|---|
| 144 | return; |
|---|
| 145 | dlclose(module->dlh); |
|---|
| 146 | if (module->prev != NULL) |
|---|
| 147 | module->prev->next = module->next; |
|---|
| 148 | if (module->next != NULL) |
|---|
| 149 | module->next->prev = module->prev; |
|---|
| 150 | free(module); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | * Destroy a chain, freeing all its links and releasing the modules |
|---|
| 156 | * they point to. |
|---|
| 157 | */ |
|---|
| 158 | |
|---|
| 159 | static void |
|---|
| 160 | openpam_destroy_chain(pam_chain_t *chain) |
|---|
| 161 | { |
|---|
| 162 | if (chain == NULL) |
|---|
| 163 | return; |
|---|
| 164 | openpam_destroy_chain(chain->next); |
|---|
| 165 | chain->next = NULL; |
|---|
| 166 | while (chain->optc--) |
|---|
| 167 | free(chain->optv[chain->optc]); |
|---|
| 168 | free(chain->optv); |
|---|
| 169 | openpam_release_module(chain->module); |
|---|
| 170 | free(chain); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* |
|---|
| 174 | * Add a module to a chain. |
|---|
| 175 | */ |
|---|
| 176 | |
|---|
| 177 | int |
|---|
| 178 | openpam_add_module(pam_handle_t *pamh, |
|---|
| 179 | int chain, |
|---|
| 180 | int flag, |
|---|
| 181 | const char *modpath, |
|---|
| 182 | int optc, |
|---|
| 183 | const char *optv[]) |
|---|
| 184 | { |
|---|
| 185 | pam_chain_t *new, *iterator; |
|---|
| 186 | |
|---|
| 187 | if ((new = calloc(1, sizeof *new)) == NULL) |
|---|
| 188 | goto buf_err; |
|---|
| 189 | if ((new->optv = malloc(sizeof(char *) * (optc + 1))) == NULL) |
|---|
| 190 | goto buf_err; |
|---|
| 191 | while (optc--) |
|---|
| 192 | if ((new->optv[new->optc++] = strdup(*optv++)) == NULL) |
|---|
| 193 | goto buf_err; |
|---|
| 194 | new->optv[new->optc] = NULL; |
|---|
| 195 | new->flag = flag; |
|---|
| 196 | if ((new->module = openpam_load_module(modpath)) == NULL) { |
|---|
| 197 | openpam_destroy_chain(new); |
|---|
| 198 | return (PAM_OPEN_ERR); |
|---|
| 199 | } |
|---|
| 200 | if ((iterator = pamh->chains[chain]) != NULL) { |
|---|
| 201 | while (iterator->next != NULL) |
|---|
| 202 | iterator = iterator->next; |
|---|
| 203 | iterator->next = new; |
|---|
| 204 | } else { |
|---|
| 205 | pamh->chains[chain] = new; |
|---|
| 206 | } |
|---|
| 207 | return (PAM_SUCCESS); |
|---|
| 208 | |
|---|
| 209 | buf_err: |
|---|
| 210 | openpam_log(PAM_LOG_ERROR, "%m"); |
|---|
| 211 | openpam_destroy_chain(new); |
|---|
| 212 | return (PAM_BUF_ERR); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /* |
|---|
| 217 | * Clear the chains and release the modules |
|---|
| 218 | */ |
|---|
| 219 | |
|---|
| 220 | void |
|---|
| 221 | openpam_clear_chains(pam_handle_t *pamh) |
|---|
| 222 | { |
|---|
| 223 | int i; |
|---|
| 224 | |
|---|
| 225 | for (i = 0; i < PAM_NUM_CHAINS; ++i) |
|---|
| 226 | openpam_destroy_chain(pamh->chains[i]); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | /* |
|---|
| 230 | * NOPARSE |
|---|
| 231 | */ |
|---|