pam_ptimpl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include <security/pam_appl.h>
  41. #include "pam_passthru.h"
  42. /*
  43. * PAM is not thread safe. We have to execute any PAM API calls in
  44. * a critical section. This is the lock that protects that code.
  45. */
  46. static Slapi_Mutex *PAMLock;
  47. /* Utility struct to wrap strings to avoid mallocs if possible - use
  48. stack allocated string space */
  49. #define MY_STATIC_BUF_SIZE 256
  50. typedef struct my_str_buf {
  51. char fixbuf[MY_STATIC_BUF_SIZE];
  52. char *str;
  53. } MyStrBuf;
  54. static char *
  55. init_my_str_buf(MyStrBuf *buf, const char *s)
  56. {
  57. PR_ASSERT(buf);
  58. if (s && (strlen(s) < sizeof(buf->fixbuf))) {
  59. strcpy(buf->fixbuf, s);
  60. buf->str = buf->fixbuf;
  61. } else {
  62. buf->str = slapi_ch_strdup(s);
  63. buf->fixbuf[0] = 0;
  64. }
  65. return buf->str;
  66. }
  67. static void
  68. delete_my_str_buf(MyStrBuf *buf)
  69. {
  70. if (buf->str != buf->fixbuf) {
  71. slapi_ch_free_string(&(buf->str));
  72. }
  73. }
  74. /* for third arg to pam_start */
  75. struct my_pam_conv_str {
  76. Slapi_PBlock *pb;
  77. char *pam_identity;
  78. };
  79. /*
  80. * Get the PAM identity from the value of the leftmost RDN in the BIND DN.
  81. */
  82. static char *
  83. derive_from_bind_dn(Slapi_PBlock *pb, const Slapi_DN *bindsdn, MyStrBuf *pam_id)
  84. {
  85. Slapi_RDN *rdn;
  86. char *type = NULL;
  87. char *value = NULL;
  88. rdn = slapi_rdn_new_sdn(bindsdn);
  89. slapi_rdn_get_first(rdn, &type, &value);
  90. init_my_str_buf(pam_id, value);
  91. slapi_rdn_free(&rdn);
  92. return pam_id->str;
  93. }
  94. static char *
  95. derive_from_bind_entry(Slapi_PBlock *pb, const Slapi_DN *bindsdn,
  96. MyStrBuf *pam_id, char *map_ident_attr, int *locked)
  97. {
  98. Slapi_Entry *entry = NULL;
  99. char *attrs[] = { NULL, NULL };
  100. attrs[0] = map_ident_attr;
  101. int rc = slapi_search_internal_get_entry((Slapi_DN *)bindsdn, attrs, &entry,
  102. pam_passthruauth_get_plugin_identity());
  103. if (rc != LDAP_SUCCESS) {
  104. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  105. "Could not find BIND dn %s (error %d - %s)\n",
  106. slapi_sdn_get_ndn(bindsdn), rc, ldap_err2string(rc));
  107. init_my_str_buf(pam_id, NULL);
  108. } else if (NULL == entry) {
  109. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  110. "Could not find entry for BIND dn %s\n",
  111. slapi_sdn_get_ndn(bindsdn));
  112. init_my_str_buf(pam_id, NULL);
  113. } else if (slapi_check_account_lock( pb, entry, 0, 0, 0 ) == 1) {
  114. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  115. "Account %s inactivated.\n",
  116. slapi_sdn_get_ndn(bindsdn));
  117. init_my_str_buf(pam_id, NULL);
  118. *locked = 1;
  119. } else {
  120. char *val = slapi_entry_attr_get_charptr(entry, map_ident_attr);
  121. init_my_str_buf(pam_id, val);
  122. slapi_ch_free_string(&val);
  123. }
  124. slapi_entry_free(entry);
  125. return pam_id->str;
  126. }
  127. static void
  128. report_pam_error(char *str, int rc, pam_handle_t *pam_handle)
  129. {
  130. if (rc != PAM_SUCCESS) {
  131. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  132. "Error from PAM %s (%d: %s)\n",
  133. str, rc, pam_strerror(pam_handle, rc));
  134. }
  135. }
  136. /* returns a berval value as a null terminated string */
  137. static char *strdupbv(struct berval *bv)
  138. {
  139. char *str = slapi_ch_malloc(bv->bv_len+1);
  140. memcpy(str, bv->bv_val, bv->bv_len);
  141. str[bv->bv_len] = 0;
  142. return str;
  143. }
  144. static void
  145. free_pam_response(int nresp, struct pam_response *resp)
  146. {
  147. int ii;
  148. for (ii = 0; ii < nresp; ++ii) {
  149. if (resp[ii].resp) {
  150. slapi_ch_free((void **)&(resp[ii].resp));
  151. }
  152. }
  153. slapi_ch_free((void **)&resp);
  154. }
  155. /*
  156. * This is the conversation function passed into pam_start(). This is what sets the password
  157. * that PAM uses to authenticate. This function is sort of stupid - it assumes all echo off
  158. * or binary prompts are for the password, and other prompts are for the username. Time will
  159. * tell if this is actually the case.
  160. */
  161. static int
  162. pam_conv_func(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *mydata)
  163. {
  164. int ii;
  165. struct berval *creds;
  166. struct my_pam_conv_str *my_data = (struct my_pam_conv_str *)mydata;
  167. struct pam_response *reply;
  168. int ret = PAM_SUCCESS;
  169. if (num_msg <= 0) {
  170. return PAM_CONV_ERR;
  171. }
  172. /* empty reply structure */
  173. reply = (struct pam_response *)slapi_ch_calloc(num_msg,
  174. sizeof(struct pam_response));
  175. slapi_pblock_get( my_data->pb, SLAPI_BIND_CREDENTIALS, &creds ); /* the password */
  176. for (ii = 0; ii < num_msg; ++ii) {
  177. slapi_log_error(SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  178. "pam msg [%d] = %d %s\n", ii, msg[ii]->msg_style,
  179. msg[ii]->msg);
  180. /* hard to tell what prompt is for . . . */
  181. /* assume prompts for password are either BINARY or ECHO_OFF */
  182. if (msg[ii]->msg_style == PAM_PROMPT_ECHO_OFF) {
  183. reply[ii].resp = strdupbv(creds);
  184. #ifdef LINUX
  185. } else if (msg[ii]->msg_style == PAM_BINARY_PROMPT) {
  186. reply[ii].resp = strdupbv(creds);
  187. #endif
  188. } else if (msg[ii]->msg_style == PAM_PROMPT_ECHO_ON) { /* assume username */
  189. reply[ii].resp = slapi_ch_strdup(my_data->pam_identity);
  190. } else if (msg[ii]->msg_style == PAM_ERROR_MSG) {
  191. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  192. "pam msg [%d] error [%s]\n", ii, msg[ii]->msg);
  193. } else if (msg[ii]->msg_style == PAM_TEXT_INFO) {
  194. slapi_log_error(SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  195. "pam msg [%d] text info [%s]\n", ii, msg[ii]->msg);
  196. } else {
  197. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  198. "Error: unknown pam message type (%d: %s)\n",
  199. msg[ii]->msg_style, msg[ii]->msg);
  200. ret = PAM_CONV_ERR;
  201. }
  202. }
  203. if (ret == PAM_CONV_ERR) {
  204. free_pam_response(num_msg, reply);
  205. reply = NULL;
  206. }
  207. *resp = reply;
  208. return ret;
  209. }
  210. /*
  211. * Do the actual work of authenticating with PAM. First, get the PAM identity
  212. * based on the method used to convert the BIND identity to the PAM identity.
  213. * Set up the structures that pam_start needs and call pam_start(). After
  214. * that, call pam_authenticate and pam_acct_mgmt. Check the various return
  215. * values from these functions and map them to their corresponding LDAP BIND
  216. * return values. Return the appropriate LDAP error code.
  217. * This function will also set the appropriate LDAP response controls in
  218. * the given pblock.
  219. * Since this function can be called multiple times, we only want to return
  220. * the errors and controls to the user if this is the final call, so the
  221. * final_method parameter tells us if this is the last one. Coupled with
  222. * the fallback argument, we can tell if we are able to send the response
  223. * back to the client.
  224. */
  225. static int
  226. do_one_pam_auth(
  227. Slapi_PBlock *pb,
  228. int method, /* get pam identity from ENTRY, RDN, or DN */
  229. PRBool final_method, /* which method is the last one to try */
  230. char *pam_service, /* name of service for pam_start() */
  231. char *map_ident_attr, /* for ENTRY method, name of attribute holding pam identity */
  232. PRBool fallback, /* if true, failure here should fallback to regular bind */
  233. int pw_response_requested /* do we need to send pwd policy resp control */
  234. )
  235. {
  236. MyStrBuf pam_id;
  237. const char *binddn = NULL;
  238. Slapi_DN *bindsdn = NULL;
  239. int rc = PAM_SUCCESS;
  240. int retcode = LDAP_SUCCESS;
  241. pam_handle_t *pam_handle;
  242. struct my_pam_conv_str my_data;
  243. struct pam_conv my_pam_conv = {pam_conv_func, NULL};
  244. char *errmsg = NULL; /* free with PR_smprintf_free */
  245. int locked = 0;
  246. slapi_pblock_get( pb, SLAPI_BIND_TARGET_SDN, &bindsdn );
  247. if (NULL == bindsdn) {
  248. errmsg = PR_smprintf("Null bind dn");
  249. retcode = LDAP_OPERATIONS_ERROR;
  250. pam_id.str = NULL; /* initialize pam_id.str */
  251. goto done; /* skip the pam stuff */
  252. }
  253. binddn = slapi_sdn_get_dn(bindsdn);
  254. if (method == PAMPT_MAP_METHOD_RDN) {
  255. derive_from_bind_dn(pb, bindsdn, &pam_id);
  256. } else if (method == PAMPT_MAP_METHOD_ENTRY) {
  257. derive_from_bind_entry(pb, bindsdn, &pam_id, map_ident_attr, &locked);
  258. } else {
  259. init_my_str_buf(&pam_id, binddn);
  260. }
  261. if (locked) {
  262. errmsg = PR_smprintf("Account inactivated. Contact system administrator.");
  263. retcode = LDAP_UNWILLING_TO_PERFORM; /* user inactivated */
  264. goto done; /* skip the pam stuff */
  265. }
  266. if (!pam_id.str) {
  267. errmsg = PR_smprintf("Bind DN [%s] is invalid or not found", binddn);
  268. retcode = LDAP_NO_SUCH_OBJECT; /* user unknown */
  269. goto done; /* skip the pam stuff */
  270. }
  271. /* do the pam stuff */
  272. my_data.pb = pb;
  273. my_data.pam_identity = pam_id.str;
  274. my_pam_conv.appdata_ptr = &my_data;
  275. slapi_lock_mutex(PAMLock);
  276. /* from this point on we are in the critical section */
  277. rc = pam_start(pam_service, pam_id.str, &my_pam_conv, &pam_handle);
  278. report_pam_error("during pam_start", rc, pam_handle);
  279. if (rc == PAM_SUCCESS) {
  280. /* use PAM_SILENT - there is no user interaction at this point */
  281. rc = pam_authenticate(pam_handle, 0);
  282. report_pam_error("during pam_authenticate", rc, pam_handle);
  283. /* check different types of errors here */
  284. if (rc == PAM_USER_UNKNOWN) {
  285. errmsg = PR_smprintf("User id [%s] for bind DN [%s] does not exist in PAM",
  286. pam_id.str, binddn);
  287. retcode = LDAP_NO_SUCH_OBJECT; /* user unknown */
  288. } else if (rc == PAM_AUTH_ERR) {
  289. errmsg = PR_smprintf("Invalid PAM password for user id [%s], bind DN [%s]",
  290. pam_id.str, binddn);
  291. retcode = LDAP_INVALID_CREDENTIALS; /* invalid creds */
  292. } else if (rc == PAM_MAXTRIES) {
  293. errmsg = PR_smprintf("Authentication retry limit exceeded in PAM for "
  294. "user id [%s], bind DN [%s]",
  295. pam_id.str, binddn);
  296. if (pw_response_requested) {
  297. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_ACCTLOCKED);
  298. }
  299. retcode = LDAP_CONSTRAINT_VIOLATION; /* max retries */
  300. } else if (rc != PAM_SUCCESS) {
  301. errmsg = PR_smprintf("Unknown PAM error [%s] for user id [%s], bind DN [%s]",
  302. pam_strerror(pam_handle, rc), pam_id.str, binddn);
  303. retcode = LDAP_OPERATIONS_ERROR; /* pam config or network problem */
  304. }
  305. }
  306. /* if user authenticated successfully, see if there is anything we need
  307. to report back w.r.t. password or account lockout */
  308. if (rc == PAM_SUCCESS) {
  309. rc = pam_acct_mgmt(pam_handle, 0);
  310. report_pam_error("during pam_acct_mgmt", rc, pam_handle);
  311. /* check different types of errors here */
  312. if (rc == PAM_USER_UNKNOWN) {
  313. errmsg = PR_smprintf("User id [%s] for bind DN [%s] does not exist in PAM",
  314. pam_id.str, binddn);
  315. retcode = LDAP_NO_SUCH_OBJECT; /* user unknown */
  316. } else if (rc == PAM_AUTH_ERR) {
  317. errmsg = PR_smprintf("Invalid PAM password for user id [%s], bind DN [%s]",
  318. pam_id.str, binddn);
  319. retcode = LDAP_INVALID_CREDENTIALS; /* invalid creds */
  320. } else if (rc == PAM_PERM_DENIED) {
  321. errmsg = PR_smprintf("Access denied for PAM user id [%s], bind DN [%s]"
  322. " - see administrator",
  323. pam_id.str, binddn);
  324. if (pw_response_requested) {
  325. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_ACCTLOCKED);
  326. }
  327. retcode = LDAP_UNWILLING_TO_PERFORM;
  328. } else if (rc == PAM_ACCT_EXPIRED) {
  329. errmsg = PR_smprintf("Expired PAM password for user id [%s], bind DN [%s]: "
  330. "reset required",
  331. pam_id.str, binddn);
  332. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  333. if (pw_response_requested) {
  334. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED);
  335. }
  336. retcode = LDAP_INVALID_CREDENTIALS;
  337. } else if (rc == PAM_NEW_AUTHTOK_REQD) { /* handled same way as ACCT_EXPIRED */
  338. errmsg = PR_smprintf("Expired PAM password for user id [%s], bind DN [%s]: "
  339. "reset required",
  340. pam_id.str, binddn);
  341. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  342. if (pw_response_requested) {
  343. slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED);
  344. }
  345. retcode = LDAP_INVALID_CREDENTIALS;
  346. } else if (rc != PAM_SUCCESS) {
  347. errmsg = PR_smprintf("Unknown PAM error [%s] for user id [%s], bind DN [%s]",
  348. pam_strerror(pam_handle, rc), pam_id.str, binddn);
  349. retcode = LDAP_OPERATIONS_ERROR; /* unknown */
  350. }
  351. }
  352. rc = pam_end(pam_handle, rc);
  353. report_pam_error("during pam_end", rc, pam_handle);
  354. slapi_unlock_mutex(PAMLock);
  355. /* not in critical section any more */
  356. done:
  357. delete_my_str_buf(&pam_id);
  358. if ((retcode == LDAP_SUCCESS) && (rc != PAM_SUCCESS)) {
  359. errmsg = PR_smprintf("Unknown PAM error [%d] for user id [%s], bind DN [%s]",
  360. rc, pam_id.str, binddn);
  361. retcode = LDAP_OPERATIONS_ERROR;
  362. }
  363. if (retcode != LDAP_SUCCESS) {
  364. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  365. "%s\n", errmsg);
  366. if (final_method && !fallback) {
  367. slapi_send_ldap_result(pb, retcode, NULL, errmsg, 0, NULL);
  368. }
  369. }
  370. if (errmsg) {
  371. PR_smprintf_free(errmsg);
  372. }
  373. return retcode;
  374. }
  375. /*
  376. * Perform any PAM subsystem initialization that must be done at startup time.
  377. * For now, this means only the PAM mutex since PAM is not thread safe.
  378. */
  379. int
  380. pam_passthru_pam_init( void )
  381. {
  382. if (!(PAMLock = slapi_new_mutex())) {
  383. return LDAP_LOCAL_ERROR;
  384. }
  385. return 0;
  386. }
  387. int
  388. pam_passthru_pam_free( void )
  389. {
  390. slapi_destroy_mutex(PAMLock);
  391. PAMLock = NULL;
  392. return 0;
  393. }
  394. /*
  395. * Entry point into the PAM auth code. Shields the rest of the app
  396. * from PAM API code. Get our config params, then call the actual
  397. * code that does the PAM auth. Can call that code up to 3 times,
  398. * depending on what methods are set in the config.
  399. */
  400. int
  401. pam_passthru_do_pam_auth(Slapi_PBlock *pb, Pam_PassthruConfig *cfg)
  402. {
  403. int rc = LDAP_SUCCESS;
  404. MyStrBuf pam_id_attr; /* avoid malloc if possible */
  405. MyStrBuf pam_service; /* avoid malloc if possible */
  406. int method1, method2, method3;
  407. PRBool final_method;
  408. PRBool fallback = PR_FALSE;
  409. int pw_response_requested;
  410. LDAPControl **reqctrls = NULL;
  411. /* get the methods and other info */
  412. method1 = cfg->pamptconfig_map_method1;
  413. method2 = cfg->pamptconfig_map_method2;
  414. method3 = cfg->pamptconfig_map_method3;
  415. init_my_str_buf(&pam_id_attr, cfg->pamptconfig_pam_ident_attr);
  416. init_my_str_buf(&pam_service, cfg->pamptconfig_service);
  417. fallback = cfg->pamptconfig_fallback;
  418. slapi_pblock_get (pb, SLAPI_REQCONTROLS, &reqctrls);
  419. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pw_response_requested);
  420. /* figure out which method is the last one - we only return error codes, controls
  421. to the client and send a response on the last method */
  422. final_method = (method2 == PAMPT_MAP_METHOD_NONE);
  423. rc = do_one_pam_auth(pb, method1, final_method, pam_service.str, pam_id_attr.str, fallback,
  424. pw_response_requested);
  425. if ((rc != LDAP_SUCCESS) && !final_method) {
  426. final_method = (method3 == PAMPT_MAP_METHOD_NONE);
  427. rc = do_one_pam_auth(pb, method2, final_method, pam_service.str, pam_id_attr.str, fallback,
  428. pw_response_requested);
  429. if ((rc != LDAP_SUCCESS) && !final_method) {
  430. final_method = PR_TRUE;
  431. rc = do_one_pam_auth(pb, method3, final_method, pam_service.str, pam_id_attr.str, fallback,
  432. pw_response_requested);
  433. }
  434. }
  435. delete_my_str_buf(&pam_id_attr);
  436. delete_my_str_buf(&pam_service);
  437. return rc;
  438. }