pam_ptpreop.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /*
  41. * pamptpreop.c - bind pre-operation plugin for Pass Through Authentication to PAM
  42. *
  43. */
  44. #include "pam_passthru.h"
  45. static Slapi_PluginDesc pdesc = { "pam_passthruauth", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  46. "PAM pass through authentication plugin" };
  47. static void * pam_passthruauth_plugin_identity = NULL;
  48. /*
  49. * function prototypes
  50. */
  51. static int pam_passthru_bindpreop( Slapi_PBlock *pb );
  52. static int pam_passthru_bindpreop_start( Slapi_PBlock *pb );
  53. static int pam_passthru_bindpreop_close( Slapi_PBlock *pb );
  54. /*
  55. ** Plugin identity mgmt
  56. */
  57. void pam_passthruauth_set_plugin_identity(void * identity)
  58. {
  59. pam_passthruauth_plugin_identity=identity;
  60. }
  61. void * pam_passthruauth_get_plugin_identity()
  62. {
  63. return pam_passthruauth_plugin_identity;
  64. }
  65. /*
  66. * Plugin initialization function (which must be listed in the appropriate
  67. * slapd config file).
  68. */
  69. int
  70. pam_passthruauth_init( Slapi_PBlock *pb )
  71. {
  72. PAM_PASSTHRU_ASSERT( pb != NULL );
  73. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  74. "=> pam_passthruauth_init\n" );
  75. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &pam_passthruauth_plugin_identity);
  76. PR_ASSERT (pam_passthruauth_plugin_identity);
  77. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  78. (void *)SLAPI_PLUGIN_VERSION_01 ) != 0
  79. || slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  80. (void *)&pdesc ) != 0
  81. || slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  82. (void *)pam_passthru_bindpreop_start ) != 0
  83. || slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN,
  84. (void *)pam_passthru_bindpreop ) != 0
  85. || slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN,
  86. (void *)pam_passthru_bindpreop_close ) != 0 ) {
  87. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  88. "pam_passthruauth_init failed\n" );
  89. return( -1 );
  90. }
  91. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  92. "<= pam_passthruauth_init succeeded\n" );
  93. return( 0 );
  94. }
  95. /*
  96. * pam_passthru_bindpreop_start() is called before the directory server
  97. * is fully up. We parse our configuration and initialize any mutexes, etc.
  98. */
  99. static int
  100. pam_passthru_bindpreop_start( Slapi_PBlock *pb )
  101. {
  102. int rc;
  103. Slapi_Entry *config_e = NULL; /* entry containing plugin config */
  104. PAM_PASSTHRU_ASSERT( pb != NULL );
  105. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  106. "=> pam_passthru_bindpreop_start\n" );
  107. if ( slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &config_e ) != 0 ) {
  108. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  109. "missing config entry\n" );
  110. return( -1 );
  111. }
  112. if (( rc = pam_passthru_config( config_e )) != LDAP_SUCCESS ) {
  113. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  114. "configuration failed (%s)\n", ldap_err2string( rc ));
  115. return( -1 );
  116. }
  117. if (( rc = pam_passthru_pam_init()) != LDAP_SUCCESS ) {
  118. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  119. "could not initialize PAM subsystem (%d)\n", rc);
  120. return( -1 );
  121. }
  122. return( 0 );
  123. }
  124. /*
  125. * Called right before the Directory Server shuts down.
  126. */
  127. static int
  128. pam_passthru_bindpreop_close( Slapi_PBlock *pb )
  129. {
  130. PAM_PASSTHRU_ASSERT( pb != NULL );
  131. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  132. "=> pam_passthru_bindpreop_close\n" );
  133. return( 0 );
  134. }
  135. static int
  136. pam_passthru_bindpreop( Slapi_PBlock *pb )
  137. {
  138. int rc, method;
  139. char *normbinddn, *errmsg = NULL;
  140. Pam_PassthruConfig *cfg;
  141. struct berval *creds;
  142. int retcode = PAM_PASSTHRU_OP_NOT_HANDLED;
  143. PAM_PASSTHRU_ASSERT( pb != NULL );
  144. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  145. "=> pam_passthru_bindpreop\n" );
  146. /*
  147. * retrieve parameters for bind operation
  148. */
  149. if ( slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method ) != 0 ||
  150. slapi_pblock_get( pb, SLAPI_BIND_TARGET, &normbinddn ) != 0 ||
  151. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &creds ) != 0 ) {
  152. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  153. "<= not handled (unable to retrieve bind parameters)\n" );
  154. return retcode;
  155. }
  156. /*
  157. * We only handle simple bind requests that include non-NULL binddn and
  158. * credentials. Let the Directory Server itself handle everything else.
  159. */
  160. if ( method != LDAP_AUTH_SIMPLE || *normbinddn == '\0' ||
  161. creds->bv_len == 0 ) {
  162. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  163. "<= not handled (not simple bind or NULL dn/credentials)\n" );
  164. return retcode;
  165. }
  166. /* get the config */
  167. cfg = pam_passthru_get_config();
  168. /* don't lock mutex here - simple integer access - assume atomic */
  169. if (cfg->pamptconfig_secure) { /* is a secure connection required? */
  170. int is_ssl = 0;
  171. slapi_pblock_get(pb, SLAPI_CONN_IS_SSL_SESSION, &is_ssl);
  172. if (!is_ssl) {
  173. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  174. "<= connection not secure (secure connection required; check config)");
  175. return retcode;
  176. }
  177. }
  178. /*
  179. * Check to see if the target DN is one we should "pass through" to
  180. * PAM
  181. */
  182. if ( pam_passthru_check_suffix( cfg, normbinddn ) != LDAP_SUCCESS ) {
  183. slapi_log_error( SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  184. "<= not handled (not one of our suffixes)\n" );
  185. return retcode;
  186. }
  187. /*
  188. * We are now committed to handling this bind request.
  189. * Chain it off to PAM
  190. */
  191. rc = pam_passthru_do_pam_auth(pb, cfg);
  192. /*
  193. * If bind succeeded, change authentication information associated
  194. * with this connection.
  195. */
  196. if (rc == LDAP_SUCCESS) {
  197. char *ndn = slapi_ch_strdup(normbinddn);
  198. if ((slapi_pblock_set(pb, SLAPI_CONN_DN, ndn) != 0) ||
  199. (slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD,
  200. SLAPD_AUTH_SIMPLE) != 0)) {
  201. slapi_ch_free_string(&ndn);
  202. rc = LDAP_OPERATIONS_ERROR;
  203. errmsg = "unable to set connection DN or AUTHTYPE";
  204. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  205. "%s\n", errmsg);
  206. } else {
  207. LDAPControl **reqctrls = NULL;
  208. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqctrls);
  209. if (slapi_control_present(reqctrls, LDAP_CONTROL_AUTH_REQUEST, NULL, NULL)) {
  210. slapi_add_auth_response_control(pb, ndn);
  211. }
  212. }
  213. }
  214. if (rc == LDAP_SUCCESS) {
  215. /* we are handling the result */
  216. slapi_send_ldap_result(pb, rc, NULL, errmsg, 0, NULL);
  217. /* tell bind code we handled the result */
  218. retcode = PAM_PASSTHRU_OP_HANDLED;
  219. } else if (!cfg->pamptconfig_fallback) {
  220. /* tell bind code we already sent back the error result in pam_ptimpl.c */
  221. retcode = PAM_PASSTHRU_OP_HANDLED;
  222. }
  223. slapi_log_error(SLAPI_LOG_PLUGIN, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  224. "<= handled (error %d - %s)\n", rc, ldap_err2string(rc));
  225. return retcode;
  226. }