pam_ptimpl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #include <security/pam_appl.h>
  38. #include "pam_passthru.h"
  39. /*
  40. * PAM is not thread safe. We have to execute any PAM API calls in
  41. * a critical section. This is the lock that protects that code.
  42. */
  43. static Slapi_Mutex *PAMLock;
  44. /* Utility struct to wrap strings to avoid mallocs if possible - use
  45. stack allocated string space */
  46. #define MY_STATIC_BUF_SIZE 256
  47. typedef struct my_str_buf {
  48. char fixbuf[MY_STATIC_BUF_SIZE];
  49. char *str;
  50. } MyStrBuf;
  51. static char *
  52. init_my_str_buf(MyStrBuf *buf, const char *s)
  53. {
  54. PR_ASSERT(buf);
  55. if (s && (strlen(s) < sizeof(buf->fixbuf))) {
  56. strcpy(buf->fixbuf, s);
  57. buf->str = buf->fixbuf;
  58. } else {
  59. buf->str = slapi_ch_strdup(s);
  60. buf->fixbuf[0] = 0;
  61. }
  62. return buf->str;
  63. }
  64. static void
  65. delete_my_str_buf(MyStrBuf *buf)
  66. {
  67. if (buf->str != buf->fixbuf) {
  68. slapi_ch_free_string(&buf->str);
  69. }
  70. }
  71. /* for third arg to pam_start */
  72. struct my_pam_conv_str {
  73. Slapi_PBlock *pb;
  74. char *pam_identity;
  75. };
  76. /*
  77. * Get the PAM identity from the value of the leftmost RDN in the BIND DN.
  78. */
  79. static char *
  80. derive_from_bind_dn(Slapi_PBlock *pb, char *binddn, MyStrBuf *pam_id)
  81. {
  82. Slapi_RDN *rdn;
  83. char *type = NULL;
  84. char *value = NULL;
  85. rdn = slapi_rdn_new_dn(binddn);
  86. slapi_rdn_get_first(rdn, &type, &value);
  87. init_my_str_buf(pam_id, value);
  88. slapi_rdn_free(&rdn);
  89. return pam_id->str;
  90. }
  91. static char *
  92. derive_from_bind_entry(Slapi_PBlock *pb, char *binddn, MyStrBuf *pam_id, char *map_ident_attr)
  93. {
  94. char buf[BUFSIZ];
  95. Slapi_Entry *entry = NULL;
  96. Slapi_DN *sdn = slapi_sdn_new_dn_byref(binddn);
  97. char *attrs[] = { NULL, NULL };
  98. attrs[0] = map_ident_attr;
  99. int rc = slapi_search_internal_get_entry(sdn, attrs, &entry,
  100. pam_passthruauth_get_plugin_identity());
  101. slapi_sdn_free(&sdn);
  102. if (rc != LDAP_SUCCESS) {
  103. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  104. "Could not find BIND dn %s (error %d - %s)\n",
  105. escape_string(binddn, buf), rc, ldap_err2string(rc));
  106. init_my_str_buf(pam_id, NULL);
  107. } else if (NULL == entry) {
  108. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  109. "Could not find entry for BIND dn %s\n",
  110. escape_string(binddn, buf));
  111. init_my_str_buf(pam_id, NULL);
  112. } else {
  113. char *val = slapi_entry_attr_get_charptr(entry, map_ident_attr);
  114. init_my_str_buf(pam_id, val);
  115. slapi_ch_free_string(&val);
  116. }
  117. slapi_entry_free(entry);
  118. return pam_id->str;
  119. }
  120. static void
  121. report_pam_error(char *str, int rc, pam_handle_t *pam_handle)
  122. {
  123. if (rc != PAM_SUCCESS) {
  124. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  125. "Error from PAM %s (%d: %s)\n",
  126. str, rc, pam_strerror(pam_handle, rc));
  127. }
  128. }
  129. /* returns a berval value as a null terminated string */
  130. static char *strdupbv(struct berval *bv)
  131. {
  132. char *str = malloc(bv->bv_len+1);
  133. memcpy(str, bv->bv_val, bv->bv_len);
  134. str[bv->bv_len] = 0;
  135. return str;
  136. }
  137. static void
  138. free_pam_response(int nresp, struct pam_response *resp)
  139. {
  140. int ii;
  141. for (ii = 0; ii < nresp; ++ii) {
  142. if (resp[ii].resp) {
  143. free(resp[ii].resp);
  144. }
  145. }
  146. free(resp);
  147. }
  148. /*
  149. * This is the conversation function passed into pam_start(). This is what sets the password
  150. * that PAM uses to authenticate. This function is sort of stupid - it assumes all echo off
  151. * or binary prompts are for the password, and other prompts are for the username. Time will
  152. * tell if this is actually the case.
  153. */
  154. static int
  155. pam_conv_func(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *mydata)
  156. {
  157. int ii;
  158. struct berval *creds;
  159. struct my_pam_conv_str *my_data = (struct my_pam_conv_str *)mydata;
  160. struct pam_response *reply;
  161. int ret = PAM_SUCCESS;
  162. if (num_msg <= 0) {
  163. return PAM_CONV_ERR;
  164. }
  165. /* empty reply structure */
  166. reply = (struct pam_response *)calloc(num_msg,
  167. sizeof(struct pam_response));
  168. slapi_pblock_get( my_data->pb, SLAPI_BIND_CREDENTIALS, &creds ); /* the password */
  169. for (ii = 0; ii < num_msg; ++ii) {
  170. slapi_log_error(SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  171. "pam msg [%d] = %d %s\n", ii, msg[ii]->msg_style,
  172. msg[ii]->msg);
  173. /* hard to tell what prompt is for . . . */
  174. /* assume prompts for password are either BINARY or ECHO_OFF */
  175. if (msg[ii]->msg_style == PAM_PROMPT_ECHO_OFF) {
  176. reply[ii].resp = strdupbv(creds);
  177. #ifdef LINUX
  178. } else if (msg[ii]->msg_style == PAM_BINARY_PROMPT) {
  179. reply[ii].resp = strdupbv(creds);
  180. #endif
  181. } else if (msg[ii]->msg_style == PAM_PROMPT_ECHO_ON) { /* assume username */
  182. reply[ii].resp = strdup(my_data->pam_identity);
  183. } else if (msg[ii]->msg_style == PAM_ERROR_MSG) {
  184. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  185. "pam msg [%d] error [%s]\n", ii, msg[ii]->msg);
  186. } else if (msg[ii]->msg_style == PAM_TEXT_INFO) {
  187. slapi_log_error(SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  188. "pam msg [%d] text info [%s]\n", ii, msg[ii]->msg);
  189. } else {
  190. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  191. "Error: unknown pam message type (%d: %s)\n",
  192. msg[ii]->msg_style, msg[ii]->msg);
  193. ret = PAM_CONV_ERR;
  194. }
  195. }
  196. if (ret == PAM_CONV_ERR) {
  197. free_pam_response(num_msg, reply);
  198. reply = NULL;
  199. }
  200. *resp = reply;
  201. return ret;
  202. }
  203. /*
  204. * Do the actual work of authenticating with PAM. First, get the PAM identity
  205. * based on the method used to convert the BIND identity to the PAM identity.
  206. * Set up the structures that pam_start needs and call pam_start(). After
  207. * that, call pam_authenticate and pam_acct_mgmt. Check the various return
  208. * values from these functions and map them to their corresponding LDAP BIND
  209. * return values. Return the appropriate LDAP error code.
  210. * This function will also set the appropriate LDAP response controls in
  211. * the given pblock.
  212. * Since this function can be called multiple times, we only want to return
  213. * the errors and controls to the user if this is the final call, so the
  214. * final_method parameter tells us if this is the last one. Coupled with
  215. * the fallback argument, we can tell if we are able to send the response
  216. * back to the client.
  217. */
  218. static int
  219. do_one_pam_auth(
  220. Slapi_PBlock *pb,
  221. int method, /* get pam identity from ENTRY, RDN, or DN */
  222. PRBool final_method, /* which method is the last one to try */
  223. char *pam_service, /* name of service for pam_start() */
  224. char *map_ident_attr, /* for ENTRY method, name of attribute holding pam identity */
  225. PRBool fallback, /* if true, failure here should fallback to regular bind */
  226. int pw_response_requested /* do we need to send pwd policy resp control */
  227. )
  228. {
  229. MyStrBuf pam_id;
  230. char *binddn = NULL;
  231. int rc;
  232. int retcode = LDAP_SUCCESS;
  233. pam_handle_t *pam_handle;
  234. struct my_pam_conv_str my_data;
  235. struct pam_conv my_pam_conv = {pam_conv_func, NULL};
  236. char buf[BUFSIZ]; /* for error messages */
  237. char *errmsg = NULL; /* free with PR_smprintf_free */
  238. slapi_pblock_get( pb, SLAPI_BIND_TARGET, &binddn );
  239. if (method == PAMPT_MAP_METHOD_RDN) {
  240. derive_from_bind_dn(pb, binddn, &pam_id);
  241. } else if (method == PAMPT_MAP_METHOD_ENTRY) {
  242. derive_from_bind_entry(pb, binddn, &pam_id, map_ident_attr);
  243. } else {
  244. init_my_str_buf(&pam_id, binddn);
  245. }
  246. /* do the pam stuff */
  247. my_data.pb = pb;
  248. my_data.pam_identity = pam_id.str;
  249. my_pam_conv.appdata_ptr = &my_data;
  250. slapi_lock_mutex(PAMLock);
  251. /* from this point on we are in the critical section */
  252. rc = pam_start(pam_service, pam_id.str, &my_pam_conv, &pam_handle);
  253. report_pam_error("during pam_start", rc, pam_handle);
  254. if (rc == PAM_SUCCESS) {
  255. /* use PAM_SILENT - there is no user interaction at this point */
  256. rc = pam_authenticate(pam_handle, 0);
  257. report_pam_error("during pam_authenticate", rc, pam_handle);
  258. /* check different types of errors here */
  259. if (rc == PAM_USER_UNKNOWN) {
  260. errmsg = PR_smprintf("User id [%s] for bind DN [%s] does not exist in PAM",
  261. pam_id.str, escape_string(binddn, buf));
  262. retcode = LDAP_NO_SUCH_OBJECT; /* user unknown */
  263. } else if (rc == PAM_AUTH_ERR) {
  264. errmsg = PR_smprintf("Invalid PAM password for user id [%s], bind DN [%s]",
  265. pam_id.str, escape_string(binddn, buf));
  266. retcode = LDAP_INVALID_CREDENTIALS; /* invalid creds */
  267. } else if (rc == PAM_MAXTRIES) {
  268. errmsg = PR_smprintf("Authentication retry limit exceeded in PAM for "
  269. "user id [%s], bind DN [%s]",
  270. pam_id.str, escape_string(binddn, buf));
  271. if (pw_response_requested) {
  272. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_ACCTLOCKED);
  273. }
  274. retcode = LDAP_CONSTRAINT_VIOLATION; /* max retries */
  275. } else if (rc != PAM_SUCCESS) {
  276. errmsg = PR_smprintf("Unknown PAM error [%s] for user id [%s], bind DN [%s]",
  277. pam_strerror(pam_handle, rc), pam_id.str, escape_string(binddn, buf));
  278. retcode = LDAP_OPERATIONS_ERROR; /* pam config or network problem */
  279. }
  280. }
  281. /* if user authenticated successfully, see if there is anything we need
  282. to report back w.r.t. password or account lockout */
  283. if (rc == PAM_SUCCESS) {
  284. rc = pam_acct_mgmt(pam_handle, 0);
  285. report_pam_error("during pam_acct_mgmt", rc, pam_handle);
  286. /* check different types of errors here */
  287. if (rc == PAM_USER_UNKNOWN) {
  288. errmsg = PR_smprintf("User id [%s] for bind DN [%s] does not exist in PAM",
  289. pam_id.str, escape_string(binddn, buf));
  290. retcode = LDAP_NO_SUCH_OBJECT; /* user unknown */
  291. } else if (rc == PAM_AUTH_ERR) {
  292. errmsg = PR_smprintf("Invalid PAM password for user id [%s], bind DN [%s]",
  293. pam_id.str, escape_string(binddn, buf));
  294. retcode = LDAP_INVALID_CREDENTIALS; /* invalid creds */
  295. } else if (rc == PAM_PERM_DENIED) {
  296. errmsg = PR_smprintf("Access denied for PAM user id [%s], bind DN [%s]"
  297. " - see administrator",
  298. pam_id.str, escape_string(binddn, buf));
  299. if (pw_response_requested) {
  300. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_ACCTLOCKED);
  301. }
  302. retcode = LDAP_UNWILLING_TO_PERFORM;
  303. } else if (rc == PAM_ACCT_EXPIRED) {
  304. errmsg = PR_smprintf("Expired PAM password for user id [%s], bind DN [%s]: "
  305. "reset required",
  306. pam_id.str, escape_string(binddn, buf));
  307. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  308. if (pw_response_requested) {
  309. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED);
  310. }
  311. retcode = LDAP_INVALID_CREDENTIALS;
  312. } else if (rc == PAM_NEW_AUTHTOK_REQD) { /* handled same way as ACCT_EXPIRED */
  313. errmsg = PR_smprintf("Expired PAM password for user id [%s], bind DN [%s]: "
  314. "reset required",
  315. pam_id.str, escape_string(binddn, buf));
  316. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  317. if (pw_response_requested) {
  318. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED);
  319. }
  320. retcode = LDAP_INVALID_CREDENTIALS;
  321. } else if (rc != PAM_SUCCESS) {
  322. errmsg = PR_smprintf("Unknown PAM error [%s] for user id [%s], bind DN [%s]",
  323. pam_strerror(pam_handle, rc), pam_id.str, escape_string(binddn, buf));
  324. retcode = LDAP_OPERATIONS_ERROR; /* unknown */
  325. }
  326. }
  327. rc = pam_end(pam_handle, rc);
  328. report_pam_error("during pam_end", rc, pam_handle);
  329. slapi_unlock_mutex(PAMLock);
  330. /* not in critical section any more */
  331. delete_my_str_buf(&pam_id);
  332. if ((retcode == LDAP_SUCCESS) && (rc != PAM_SUCCESS)) {
  333. errmsg = PR_smprintf("Unknown PAM error [%d] for user id [%d], bind DN [%s]",
  334. rc, pam_id.str, escape_string(binddn, buf));
  335. retcode = LDAP_OPERATIONS_ERROR;
  336. }
  337. if (retcode != LDAP_SUCCESS) {
  338. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  339. "%s\n", errmsg);
  340. if (final_method && !fallback) {
  341. slapi_send_ldap_result(pb, retcode, NULL, errmsg, 0, NULL);
  342. }
  343. }
  344. if (errmsg) {
  345. PR_smprintf_free(errmsg);
  346. }
  347. return retcode;
  348. }
  349. /*
  350. * Perform any PAM subsystem initialization that must be done at startup time.
  351. * For now, this means only the PAM mutex since PAM is not thread safe.
  352. */
  353. int
  354. pam_passthru_pam_init( void )
  355. {
  356. if (!(PAMLock = slapi_new_mutex())) {
  357. return LDAP_LOCAL_ERROR;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * Entry point into the PAM auth code. Shields the rest of the app
  363. * from PAM API code. Get our config params, then call the actual
  364. * code that does the PAM auth. Can call that code up to 3 times,
  365. * depending on what methods are set in the config.
  366. */
  367. int
  368. pam_passthru_do_pam_auth(Slapi_PBlock *pb, Pam_PassthruConfig *cfg)
  369. {
  370. int rc = LDAP_SUCCESS;
  371. MyStrBuf pam_id_attr; /* avoid malloc if possible */
  372. MyStrBuf pam_service; /* avoid malloc if possible */
  373. int method1, method2, method3;
  374. PRBool final_method;
  375. PRBool fallback = PR_FALSE;
  376. int pw_response_requested;
  377. LDAPControl **reqctrls = NULL;
  378. /* first lock and get the methods and other info */
  379. /* we do this so we can acquire and release the lock quickly to
  380. avoid potential deadlocks */
  381. slapi_lock_mutex(cfg->lock);
  382. method1 = cfg->pamptconfig_map_method1;
  383. method2 = cfg->pamptconfig_map_method2;
  384. method3 = cfg->pamptconfig_map_method3;
  385. init_my_str_buf(&pam_id_attr, cfg->pamptconfig_pam_ident_attr);
  386. init_my_str_buf(&pam_service, cfg->pamptconfig_service);
  387. fallback = cfg->pamptconfig_fallback;
  388. slapi_unlock_mutex(cfg->lock);
  389. slapi_pblock_get (pb, SLAPI_REQCONTROLS, &reqctrls);
  390. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pw_response_requested);
  391. /* figure out which method is the last one - we only return error codes, controls
  392. to the client and send a response on the last method */
  393. final_method = (method2 == PAMPT_MAP_METHOD_NONE);
  394. rc = do_one_pam_auth(pb, method1, final_method, pam_service.str, pam_id_attr.str, fallback,
  395. pw_response_requested);
  396. if ((rc != LDAP_SUCCESS) && !final_method) {
  397. final_method = (method3 == PAMPT_MAP_METHOD_NONE);
  398. rc = do_one_pam_auth(pb, method2, final_method, pam_service.str, pam_id_attr.str, fallback,
  399. pw_response_requested);
  400. if ((rc != LDAP_SUCCESS) && !final_method) {
  401. final_method = PR_TRUE;
  402. rc = do_one_pam_auth(pb, method3, final_method, pam_service.str, pam_id_attr.str, fallback,
  403. pw_response_requested);
  404. }
  405. }
  406. delete_my_str_buf(&pam_id_attr);
  407. delete_my_str_buf(&pam_service);
  408. return rc;
  409. }