auth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include <stdlib.h> /* getenv */
  39. #include <string.h> /* memcpy */
  40. #include <ldaputil/ldaputil.h> /* LDAPU_SUCCESS, ldapu_VTable_set */
  41. #include <ldaputil/init.h> /* ldaputil_init */
  42. #include <ldaputil/certmap.h> /* ldapu_cert_to_ldap_entry */
  43. #ifndef _WIN32
  44. #include <sys/param.h> /* MAXPATHLEN */
  45. #endif
  46. #include "slap.h" /* slapi_ch_malloc */
  47. #include "fe.h"
  48. char* client_auth_config_file = NULL;
  49. /* forward declarations */
  50. static void generate_id();
  51. static Slapi_ComponentId * auth_get_component_id();
  52. #define internal_ld NULL
  53. static int LDAP_CALL LDAP_CALLBACK
  54. slapu_msgfree( LDAP* ld, LDAPMessage* msg )
  55. {
  56. Slapi_PBlock* pb = (Slapi_PBlock*)msg;
  57. if (ld != internal_ld) {
  58. return ldap_msgfree (msg);
  59. }
  60. if (pb) {
  61. slapi_free_search_results_internal (pb);
  62. slapi_pblock_destroy (pb);
  63. }
  64. return LDAP_SUCCESS;
  65. }
  66. static int LDAP_CALL LDAP_CALLBACK
  67. slapu_search_s( LDAP* ld, const char* baseDN, int scope, const char* filter,
  68. char** attrs, int attrsonly, LDAPMessage** result )
  69. {
  70. int err = LDAP_NO_SUCH_OBJECT;
  71. Slapi_PBlock* pb;
  72. LDAPControl **ctrls;
  73. if (ld != internal_ld) {
  74. return ldap_search_s (ld, baseDN, scope, filter, attrs, attrsonly, result);
  75. }
  76. LDAPDebug (LDAP_DEBUG_TRACE, "=> slapu_search_s (\"%s\", %i, %s)\n",
  77. baseDN, scope, filter);
  78. if (filter == NULL) filter = "objectclass=*";
  79. /* use new internal search API */
  80. pb=slapi_pblock_new();
  81. /* we need to provide managedsait control to avoid returning continuation references */
  82. ctrls = (LDAPControl **)slapi_ch_calloc (2, sizeof (LDAPControl *));
  83. ctrls[0] = (LDAPControl*)slapi_ch_malloc (sizeof (LDAPControl));
  84. ctrls[0]->ldctl_oid = slapi_ch_strdup (LDAP_CONTROL_MANAGEDSAIT);
  85. ctrls[0]->ldctl_value.bv_val = NULL;
  86. ctrls[0]->ldctl_value.bv_len = 0;
  87. ctrls[0]->ldctl_iscritical = '\0';
  88. slapi_search_internal_set_pb(pb, baseDN, scope, (char *)filter, attrs, attrsonly,
  89. ctrls, NULL, auth_get_component_id(), 0 /* actions */);
  90. slapi_search_internal_pb(pb);
  91. if (pb != NULL) {
  92. if (slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &err)) {
  93. err = LDAP_LOCAL_ERROR;
  94. }
  95. if (err != LDAP_SUCCESS) {
  96. slapu_msgfree (ld, (LDAPMessage*)pb);
  97. pb = NULL;
  98. if (scope == LDAP_SCOPE_SUBTREE) {
  99. char ebuf[ BUFSIZ ], fbuf[ BUFSIZ ];
  100. LDAPDebug (LDAP_DEBUG_ANY, "slapi_search_internal (\"%s\", subtree, %s) err %i\n",
  101. escape_string( (char*)baseDN, ebuf ), escape_string( (char*)filter, fbuf ), err);
  102. }
  103. }
  104. } else {
  105. char ebuf[ BUFSIZ ], fbuf[ BUFSIZ ];
  106. LDAPDebug (LDAP_DEBUG_ANY, "slapi_search_internal (\"%s\", %i, %s) NULL\n",
  107. escape_string( (char*)baseDN, ebuf ), scope, escape_string( (char*)filter, fbuf ));
  108. }
  109. *result = (LDAPMessage*)pb;
  110. LDAPDebug (LDAP_DEBUG_TRACE, "<= slapu_search_s %i\n", err, 0, 0);
  111. return err;
  112. }
  113. static int LDAP_CALL LDAP_CALLBACK
  114. slapu_count_entries( LDAP* ld, LDAPMessage* msg )
  115. {
  116. Slapi_Entry** entry = NULL;
  117. int count = 0;
  118. if (ld != internal_ld) {
  119. return ldap_count_entries (ld, msg);
  120. }
  121. if (!slapi_pblock_get ((Slapi_PBlock*)msg, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entry)
  122. && entry) {
  123. for (; *entry; ++entry) ++count;
  124. }
  125. return count;
  126. }
  127. /* slapu_search_s() returns a Slapi_PBlock*, but slapu_first_entry() and
  128. * slapu_next_entry() return a Slapi_Entry** pointing into the same array
  129. * as the PBlock. If one of the iteration (Slapi_Entry**) pointers was
  130. * passed to slapu_msgfree(), havoc would ensue. ldaputil never does this.
  131. * But ldap_msgfree() would support it (no?); so a plugin function might.
  132. * Yet another way this doesn't support plugin functions.
  133. */
  134. static LDAPMessage* LDAP_CALL LDAP_CALLBACK
  135. slapu_first_entry( LDAP* ld, LDAPMessage* msg )
  136. {
  137. Slapi_Entry** entry = NULL;
  138. if (ld != internal_ld) {
  139. return ldap_first_entry (ld, msg);
  140. }
  141. if (!slapi_pblock_get ((Slapi_PBlock*)msg, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entry)
  142. && entry && *entry) {
  143. return (LDAPMessage*)entry;
  144. }
  145. return NULL;
  146. }
  147. static LDAPMessage* LDAP_CALL LDAP_CALLBACK
  148. slapu_next_entry( LDAP* ld, LDAPMessage* msg )
  149. {
  150. Slapi_Entry** entry = (Slapi_Entry**)msg;
  151. if (ld != internal_ld) {
  152. return ldap_next_entry (ld, msg);
  153. }
  154. if (entry && *entry && *++entry) {
  155. return (LDAPMessage*)entry;
  156. }
  157. return NULL;
  158. }
  159. static char* LDAP_CALL LDAP_CALLBACK
  160. slapu_get_dn( LDAP* ld, LDAPMessage* entry )
  161. {
  162. if (ld != internal_ld) {
  163. return ldap_get_dn (ld, entry);
  164. }
  165. return slapi_ch_strdup (slapi_entry_get_dn (*(Slapi_Entry**)entry));
  166. }
  167. static void LDAP_CALL LDAP_CALLBACK
  168. slapu_memfree( LDAP* ld, void* dn )
  169. {
  170. if (ld != internal_ld) {
  171. ldap_memfree (dn);
  172. } else {
  173. free (dn);
  174. }
  175. }
  176. static char*
  177. slapu_attr_get_desc( Slapi_Attr *attr )
  178. {
  179. char* desc = NULL;
  180. if (slapi_attr_get_type (attr, &desc) == LDAP_SUCCESS && desc) {
  181. return slapi_ch_strdup (desc);
  182. }
  183. return NULL;
  184. }
  185. /* slapu_first_attribute and slapu_next_attribute use a Slapi_Attr*
  186. * as an iterator. It is malloc'd by first() and free'd by ber_free().
  187. */
  188. static char* LDAP_CALL LDAP_CALLBACK
  189. slapu_first_attribute( LDAP* ld, LDAPMessage* entry, BerElement** iter )
  190. {
  191. if (ld != internal_ld) {
  192. return ldap_first_attribute (ld, entry, iter);
  193. } else {
  194. Slapi_Attr** attr = (Slapi_Attr**) slapi_ch_malloc (sizeof(Slapi_Attr*));
  195. *iter = (BerElement*) attr;
  196. if (attr && slapi_entry_first_attr (*(Slapi_Entry**)entry, attr) == LDAP_SUCCESS) {
  197. return slapu_attr_get_desc (*attr);
  198. }
  199. }
  200. return NULL;
  201. }
  202. static char* LDAP_CALL LDAP_CALLBACK
  203. slapu_next_attribute( LDAP* ld, LDAPMessage* entry, BerElement* iter)
  204. {
  205. Slapi_Attr** attr = (Slapi_Attr**)iter;
  206. if (ld != internal_ld) {
  207. return ldap_next_attribute (ld, entry, iter);
  208. }
  209. if (attr && slapi_entry_next_attr (*(Slapi_Entry**)entry, *attr, attr) == LDAP_SUCCESS) {
  210. return slapu_attr_get_desc (*attr);
  211. }
  212. return NULL;
  213. }
  214. static void LDAP_CALL LDAP_CALLBACK
  215. slapu_ber_free( LDAP* ld, BerElement* iter, int freebuf )
  216. {
  217. if (ld != internal_ld) {
  218. ldap_ber_free (iter, freebuf);
  219. } else {
  220. free ((Slapi_Attr**)iter);
  221. }
  222. }
  223. static struct berval** LDAP_CALL LDAP_CALLBACK
  224. slapu_get_values_len( LDAP *ld, LDAPMessage *entry, const char *desc )
  225. {
  226. Slapi_Attr* attr = NULL;
  227. if (ld != internal_ld) {
  228. return ldap_get_values_len (ld, entry, desc);
  229. }
  230. if (slapi_entry_attr_find (*(Slapi_Entry**)entry, desc, &attr) == LDAP_SUCCESS
  231. && attr) {
  232. struct berval** values = NULL;
  233. if ( slapi_attr_get_bervals_copy (attr, &values) == 0 ) {
  234. return (values);
  235. }
  236. }
  237. return NULL;
  238. }
  239. static void LDAP_CALL LDAP_CALLBACK
  240. slapu_value_free_len( LDAP* ld, struct berval **values )
  241. {
  242. if (ld != internal_ld) {
  243. ldap_value_free_len (values);
  244. } else {
  245. ber_bvecfree (values);
  246. }
  247. }
  248. void
  249. client_auth_init ()
  250. {
  251. char *instancedir;
  252. int err;
  253. if (client_auth_config_file == NULL) {
  254. char *confdir = config_get_configdir();
  255. if (NULL == confdir) {
  256. LDAPDebug (LDAP_DEBUG_ANY,
  257. "client_auth_init: failed to get configdir\n",
  258. 0, 0, 0);
  259. return;
  260. }
  261. client_auth_config_file = PR_smprintf("%s/certmap.conf", confdir);
  262. if (NULL == client_auth_config_file) {
  263. LDAPDebug (LDAP_DEBUG_ANY,
  264. "client_auth_init: failed to duplicate \"%s/certmap\"\n",
  265. confdir, 0, 0);
  266. return;
  267. }
  268. }
  269. err = ldaputil_init (client_auth_config_file, "", NULL, "slapd", NULL);
  270. if (err != LDAPU_SUCCESS) {
  271. LDAPDebug (LDAP_DEBUG_TRACE, "ldaputil_init(%s,...) %i\n",
  272. client_auth_config_file, err, 0);
  273. } else {
  274. LDAPUVTable_t vtable = {
  275. NULL /* ssl_init */,
  276. NULL /* set_option */,
  277. NULL /* simple_bind_s */,
  278. NULL /* unbind */,
  279. slapu_search_s,
  280. slapu_count_entries,
  281. slapu_first_entry,
  282. slapu_next_entry,
  283. slapu_msgfree,
  284. slapu_get_dn,
  285. slapu_memfree,
  286. slapu_first_attribute,
  287. slapu_next_attribute,
  288. slapu_ber_free,
  289. NULL /* get_values */,
  290. NULL /* value_free */,
  291. slapu_get_values_len,
  292. slapu_value_free_len};
  293. ldapu_VTable_set (&vtable);
  294. }
  295. /* Generate a component id for cert-based authentication */
  296. generate_id();
  297. }
  298. #include <ssl.h>
  299. #include "slapi-plugin.h" /* SLAPI_BERVAL_EQ */
  300. #include "slapi-private.h" /* COMPONENT_CERT_AUTH */
  301. static Slapi_ComponentId * auth_component_id=NULL;
  302. static void generate_id()
  303. {
  304. if (auth_component_id == NULL ) {
  305. auth_component_id=generate_componentid (NULL /* Not a plugin */ , COMPONENT_CERT_AUTH);
  306. }
  307. }
  308. static Slapi_ComponentId * auth_get_component_id() {
  309. return auth_component_id;
  310. }
  311. static char*
  312. subject_of (CERTCertificate* cert)
  313. {
  314. char* dn = NULL;
  315. if (cert != NULL) {
  316. int err = ldapu_get_cert_subject_dn (cert, &dn);
  317. if (err != LDAPU_SUCCESS) {
  318. LDAPDebug (LDAP_DEBUG_ANY, "ldapu_get_cert_subject_dn(%p) %i (%s)\n",
  319. (void*)cert, err, ldapu_err2string (err));
  320. }
  321. }
  322. return dn;
  323. }
  324. static char*
  325. issuer_of (CERTCertificate* cert)
  326. {
  327. char* dn = NULL;
  328. if (cert != NULL) {
  329. int err = ldapu_get_cert_issuer_dn (cert, &dn);
  330. if (err != LDAPU_SUCCESS) {
  331. LDAPDebug (LDAP_DEBUG_ANY, "ldapu_get_cert_issuer_dn(%p) %i (%s)\n",
  332. (void*)cert, err, ldapu_err2string (err));
  333. }
  334. }
  335. return dn;
  336. }
  337. /*
  338. * Log a certificate that was rejected because the client didn't
  339. * authenticate it.
  340. *
  341. * Note: handle_bad_certificate() is called via slapd_ssl_badCertHook().
  342. * A Connection * is passed in client data. That connection must have its
  343. * c_mutex locked.
  344. */
  345. int
  346. handle_bad_certificate (void* clientData, PRFileDesc *prfd)
  347. {
  348. char sbuf[ BUFSIZ ], ibuf[ BUFSIZ ];
  349. Connection* conn = (Connection*) clientData;
  350. CERTCertificate* clientCert = slapd_ssl_peerCertificate (prfd);
  351. PRErrorCode errorCode = PR_GetError();
  352. char* subject = subject_of (clientCert);
  353. char* issuer = issuer_of (clientCert);
  354. slapi_log_access( LDAP_DEBUG_STATS,
  355. "conn=%d " SLAPI_COMPONENT_NAME_NSPR " error %i (%s); unauthenticated client %s; issuer %s\n",
  356. conn->c_connid, errorCode, slapd_pr_strerror(errorCode),
  357. subject ? escape_string( subject, sbuf ) : "NULL",
  358. issuer ? escape_string( issuer, ibuf ) : "NULL" );
  359. if (issuer) free (issuer);
  360. if (subject) free (subject);
  361. if (clientCert) CERT_DestroyCertificate (clientCert);
  362. return -1; /* non-zero means reject this certificate */
  363. }
  364. /*
  365. * Get an identity from the client's certificate (if any was sent).
  366. *
  367. * Note: handle_handshake_done() is called via slapd_ssl_handshakeCallback().
  368. * A Connection * is passed in client data. That connection must have its
  369. * c_mutex locked.
  370. */
  371. void
  372. handle_handshake_done (PRFileDesc *prfd, void* clientData)
  373. {
  374. Connection* conn = (Connection*) clientData;
  375. CERTCertificate* clientCert = slapd_ssl_peerCertificate(prfd);
  376. char* clientDN = NULL;
  377. int keySize = 0;
  378. char* cipher = NULL;
  379. char* extraErrorMsg = "";
  380. SSLChannelInfo channelInfo;
  381. SSLCipherSuiteInfo cipherInfo;
  382. if ( (slapd_ssl_getChannelInfo (prfd, &channelInfo, sizeof(channelInfo))) != SECSuccess ) {
  383. PRErrorCode errorCode = PR_GetError();
  384. slapi_log_access (LDAP_DEBUG_STATS,
  385. "conn=%d SSL failed to obtain channel info; "
  386. SLAPI_COMPONENT_NAME_NSPR " error %i (%s)\n",
  387. conn->c_connid, errorCode, slapd_pr_strerror(errorCode));
  388. return;
  389. }
  390. if ( (slapd_ssl_getCipherSuiteInfo (channelInfo.cipherSuite, &cipherInfo, sizeof(cipherInfo)) )
  391. != SECSuccess) {
  392. PRErrorCode errorCode = PR_GetError();
  393. slapi_log_access (LDAP_DEBUG_STATS,
  394. "conn=%d SSL failed to obtain cipher info; "
  395. SLAPI_COMPONENT_NAME_NSPR " error %i (%s)\n",
  396. conn->c_connid, errorCode, slapd_pr_strerror(errorCode));
  397. return;
  398. }
  399. keySize = cipherInfo.effectiveKeyBits;
  400. cipher = slapi_ch_strdup(cipherInfo.symCipherName);
  401. /* If inside an Start TLS operation, perform the privacy level discovery
  402. * and if the security degree achieved after the handshake is not reckoned
  403. * to be enough, close the SSL connection. */
  404. if ( conn->c_flags & CONN_FLAG_START_TLS ) {
  405. if ( cipherInfo.symKeyBits == 0 ) {
  406. start_tls_graceful_closure( conn, NULL, 1 );
  407. slapi_ch_free((void **)&cipher);
  408. return ;
  409. }
  410. }
  411. if (config_get_SSLclientAuth() == SLAPD_SSLCLIENTAUTH_OFF ) {
  412. slapi_log_access (LDAP_DEBUG_STATS, "conn=%d SSL %i-bit %s\n",
  413. conn->c_connid, keySize, cipher ? cipher : "NULL" );
  414. slapi_ch_free((void **)&cipher);
  415. return;
  416. }
  417. if (clientCert == NULL) {
  418. slapi_log_access (LDAP_DEBUG_STATS, "conn=%d SSL %i-bit %s\n",
  419. conn->c_connid, keySize, cipher ? cipher : "NULL" );
  420. } else {
  421. char* subject = subject_of (clientCert);
  422. {
  423. char* issuer = issuer_of (clientCert);
  424. char sbuf[ BUFSIZ ], ibuf[ BUFSIZ ];
  425. slapi_log_access( LDAP_DEBUG_STATS,
  426. "conn=%d SSL %i-bit %s; client %s; issuer %s\n",
  427. conn->c_connid, keySize, cipher ? cipher : "NULL",
  428. subject ? escape_string( subject, sbuf ) : "NULL",
  429. issuer ? escape_string( issuer, ibuf ) : "NULL");
  430. if (issuer) free (issuer);
  431. }
  432. slapi_dn_normalize (subject);
  433. {
  434. LDAPMessage* chain = NULL;
  435. char *basedn = config_get_basedn();
  436. int err;
  437. err = ldapu_cert_to_ldap_entry
  438. (clientCert, internal_ld, basedn?basedn:""/*baseDN*/, &chain);
  439. if (err == LDAPU_SUCCESS && chain) {
  440. LDAPMessage* entry = slapu_first_entry (internal_ld, chain);
  441. if (entry) {
  442. clientDN = slapu_get_dn (internal_ld, entry);
  443. if (clientDN) slapi_dn_normalize (clientDN);
  444. } else {
  445. extraErrorMsg = "no entry";
  446. LDAPDebug (LDAP_DEBUG_TRACE, "<= ldapu_cert_to_ldap_entry() %s\n",
  447. extraErrorMsg, 0, 0);
  448. }
  449. } else {
  450. extraErrorMsg = ldapu_err2string(err);
  451. LDAPDebug (LDAP_DEBUG_TRACE, "<= ldapu_cert_to_ldap_entry() %i (%s)%s\n",
  452. err, extraErrorMsg, chain ? "" : " NULL");
  453. }
  454. slapi_ch_free((void**)&basedn);
  455. slapu_msgfree (internal_ld, chain);
  456. }
  457. if (subject) free (subject);
  458. }
  459. if (clientDN != NULL) {
  460. char ebuf[ BUFSIZ ];
  461. slapi_log_access (LDAP_DEBUG_STATS, "conn=%d SSL client bound as %s\n",
  462. conn->c_connid, escape_string( clientDN, ebuf ));
  463. } else if (clientCert != NULL) {
  464. slapi_log_access (LDAP_DEBUG_STATS,
  465. "conn=%d SSL failed to map client certificate to LDAP DN (%s)\n",
  466. conn->c_connid, extraErrorMsg );
  467. }
  468. /*
  469. * Associate the new credentials with the connection. Note that
  470. * clientDN and clientCert may be NULL.
  471. */
  472. bind_credentials_set( conn, SLAPD_AUTH_SSL, clientDN,
  473. SLAPD_AUTH_SSL, clientDN, clientCert , NULL);
  474. slapi_ch_free((void **)&cipher);
  475. /* clientDN and clientCert will be freed later */
  476. }