1
0

acct_usability.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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) 2011 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Account Usability Control plug-in
  42. */
  43. #include <string.h>
  44. #include "acct_usability.h"
  45. #include <nspr.h>
  46. #include <time.h>
  47. /*
  48. * Plug-in globals
  49. */
  50. static void *_PluginID = NULL;
  51. static char *_PluginDN = NULL;
  52. static Slapi_PluginDesc pdesc = { AUC_FEATURE_DESC,
  53. VENDOR,
  54. DS_PACKAGE_VERSION,
  55. AUC_PLUGIN_DESC };
  56. /*
  57. * Plug-in management functions
  58. */
  59. int auc_init(Slapi_PBlock * pb);
  60. static int auc_start(Slapi_PBlock * pb);
  61. static int auc_close(Slapi_PBlock * pb);
  62. /*
  63. * Operation callbacks (where the real work is done)
  64. */
  65. static int auc_pre_search(Slapi_PBlock * pb);
  66. static int auc_pre_entry(Slapi_PBlock *pb);
  67. /*
  68. * Plugin identity functions
  69. */
  70. void
  71. auc_set_plugin_id(void *pluginID)
  72. {
  73. _PluginID = pluginID;
  74. }
  75. void *
  76. auc_get_plugin_id()
  77. {
  78. return _PluginID;
  79. }
  80. void
  81. auc_set_plugin_dn(char *pluginDN)
  82. {
  83. _PluginDN = pluginDN;
  84. }
  85. char *
  86. auc_get_plugin_dn()
  87. {
  88. return _PluginDN;
  89. }
  90. /*
  91. * Plug-in initialization functions
  92. */
  93. int
  94. auc_init(Slapi_PBlock *pb)
  95. {
  96. int status = 0;
  97. char *plugin_identity = NULL;
  98. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  99. "--> auc_init\n");
  100. /* Store the plugin identity for later use.
  101. * Used for internal operations. */
  102. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  103. PR_ASSERT(plugin_identity);
  104. auc_set_plugin_id(plugin_identity);
  105. /* Register callbacks */
  106. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  107. SLAPI_PLUGIN_VERSION_01) != 0 ||
  108. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  109. (void *) auc_start) != 0 ||
  110. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  111. (void *) auc_close) != 0 ||
  112. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  113. (void *) &pdesc) != 0 ||
  114. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
  115. (void *) auc_pre_search) != 0 ||
  116. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ENTRY_FN,
  117. (void *) auc_pre_entry) != 0
  118. ) {
  119. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  120. "auc_init: failed to register plugin\n");
  121. status = -1;
  122. }
  123. if (status == 0) {
  124. slapi_register_supported_control(AUC_OID, SLAPI_OPERATION_SEARCH);
  125. }
  126. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  127. "<-- auc_init\n");
  128. return status;
  129. }
  130. /*
  131. * auc_start()
  132. */
  133. static int
  134. auc_start(Slapi_PBlock * pb)
  135. {
  136. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  137. "--> auc_start\n");
  138. slapi_log_error(SLAPI_LOG_PLUGIN, AUC_PLUGIN_SUBSYSTEM,
  139. "account usability control plug-in: ready for service\n");
  140. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  141. "<-- auc_start\n");
  142. return 0;
  143. }
  144. /*
  145. * auc_close()
  146. */
  147. static int
  148. auc_close(Slapi_PBlock * pb)
  149. {
  150. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  151. "--> auc_close\n");
  152. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  153. "<-- auc_close\n");
  154. return 0;
  155. }
  156. /*
  157. * Helper Functions
  158. */
  159. /*
  160. * auc_incompatible_ctrl()
  161. *
  162. * Check if control oid is incompatible with the account
  163. * usability control.
  164. */
  165. static int
  166. auc_incompatible_ctrl(const char *oid)
  167. {
  168. return 0; /* no known incompatible ctrls yet */
  169. }
  170. /*
  171. * auc_create_response_ctrl()
  172. *
  173. * Generates the response control for the passed in DN.
  174. *
  175. * ACCOUNT_USABLE_RESPONSE ::= CHOICE {
  176. * is_available [0] INTEGER, -- Seconds before expiration --
  177. * is_not_available [1] MORE_INFO }
  178. *
  179. * MORE_INFO ::= SEQUENCE {
  180. * inactive [0] BOOLEAN DEFAULT FALSE,
  181. * reset [1] BOOLEAN DEFAULT FALSE,
  182. * expired [2] BOOLEAN DEFAULT_FALSE,
  183. * remaining_grace [3] INTEGER OPTIONAL,
  184. * seconds_before_unlock [4] INTEGER OPTIONAL }
  185. */
  186. static LDAPControl *auc_create_response_ctrl(Slapi_Entry *e)
  187. {
  188. BerElement *ctrlber = NULL;
  189. LDAPControl *ctrl = NULL;
  190. int is_available = 0;
  191. int seconds_before_expiration = 0;
  192. int inactive = 0;
  193. int reset = 0;
  194. int expired = 0;
  195. int remaining_grace = 0;
  196. int seconds_before_unlock = 0;
  197. Slapi_PWPolicy *pwpolicy = NULL;
  198. time_t expire_time = (time_t)0;
  199. time_t unlock_time = (time_t)0;
  200. time_t now = slapi_current_time();
  201. if (!e) {
  202. slapi_log_error(SLAPI_LOG_PLUGIN, AUC_PLUGIN_SUBSYSTEM,
  203. "auc_create_response_ctrl: NULL entry specified.\n");
  204. goto bail;
  205. }
  206. /* Fetch password policy info */
  207. pwpolicy = slapi_get_pwpolicy(slapi_entry_get_sdn(e));
  208. if (pwpolicy) {
  209. expired = slapi_pwpolicy_is_expired(pwpolicy, e, &expire_time, &remaining_grace);
  210. inactive = slapi_pwpolicy_is_locked(pwpolicy, e, &unlock_time);
  211. reset = slapi_pwpolicy_is_reset(pwpolicy, e);
  212. slapi_pwpolicy_free(pwpolicy);
  213. }
  214. /* Calculate the seconds before expiration or unlock if needed. */
  215. if (!expired && !inactive && !reset) {
  216. is_available = 1;
  217. if (expire_time > 0) {
  218. seconds_before_expiration = expire_time - now;
  219. }
  220. } else if (inactive && unlock_time) {
  221. if (unlock_time > 0) {
  222. seconds_before_unlock = unlock_time - now;
  223. }
  224. }
  225. /* Create the control value */
  226. ctrlber = ber_alloc();
  227. if (is_available) {
  228. ber_printf(ctrlber, "ti", AUC_TAG_AVAILABLE, seconds_before_expiration);
  229. } else {
  230. /* Fill in reason account is not available */
  231. ber_printf(ctrlber, "t{", AUC_TAG_NOT_AVAILABLE);
  232. ber_printf(ctrlber, "tb", AUC_TAG_INACTIVE, inactive);
  233. ber_printf(ctrlber, "tb", AUC_TAG_RESET, reset);
  234. ber_printf(ctrlber, "tb", AUC_TAG_EXPIRED, expired);
  235. if (expired) {
  236. ber_printf(ctrlber, "ti", AUC_TAG_GRACE, remaining_grace);
  237. }
  238. if (inactive) {
  239. ber_printf(ctrlber, "ti", AUC_TAG_UNLOCK, seconds_before_unlock);
  240. }
  241. ber_printf(ctrlber, "}");
  242. }
  243. slapi_build_control(AUC_OID, ctrlber, 0, &ctrl);
  244. ber_free(ctrlber, 1);
  245. bail:
  246. return ctrl;
  247. }
  248. /*
  249. * Operation callback functions
  250. */
  251. /*
  252. * auc_pre_search()
  253. *
  254. * See if the account usability control has been specified.
  255. * If so, parse it, and check to make sure it meets the
  256. * protocol specification (no duplicate attributes, etc.).
  257. * We also check to see if the requestor is allowed to use
  258. * the control.
  259. */
  260. static int
  261. auc_pre_search(Slapi_PBlock *pb)
  262. {
  263. int ldapcode = LDAP_SUCCESS;
  264. const LDAPControl **reqctrls = NULL;
  265. const LDAPControl *aucctrl = NULL;
  266. const char *ldaperrtext = "Unknown error";
  267. const char *incompatible = NULL;
  268. int isroot = 0;
  269. int ii;
  270. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  271. "--> auc_pre_search\n");
  272. /* See if the requestor is the root DN. */
  273. slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  274. /* see if the auc request control is in the list of
  275. controls - if so, validate it */
  276. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqctrls);
  277. for (ii = 0; (ldapcode == LDAP_SUCCESS) && reqctrls && reqctrls[ii]; ++ii) {
  278. const LDAPControl *ctrl = reqctrls[ii];
  279. if (!strcmp(ctrl->ldctl_oid, AUC_OID)) {
  280. if (aucctrl) { /* already specified */
  281. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  282. "The account usability control was specified more than "
  283. "once - it must be specified only once in the search request\n");
  284. ldapcode = LDAP_PROTOCOL_ERROR;
  285. ldaperrtext = "The account usability control cannot be specified more than once";
  286. aucctrl = NULL;
  287. } else if (ctrl->ldctl_value.bv_len > 0) {
  288. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  289. "Non-null control value specified for account usability control\n");
  290. ldapcode = LDAP_PROTOCOL_ERROR;
  291. ldaperrtext = "The account usability control must not have a value";
  292. } else {
  293. aucctrl = ctrl;
  294. }
  295. } else if (auc_incompatible_ctrl(ctrl->ldctl_oid)) {
  296. incompatible = ctrl->ldctl_oid;
  297. }
  298. }
  299. if (aucctrl && incompatible) {
  300. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  301. "Cannot use the account usability control and control [%s] for the same search operation\n",
  302. incompatible);
  303. /* not sure if this is a hard failure - the current spec says:
  304. The semantics of the criticality field are specified in [RFC4511].
  305. In detail, the criticality of the control determines whether the
  306. control will or will not be used, and if it will not be used, whether
  307. the operation will continue without returning the control in the
  308. response, or fail, returning unavailableCriticalExtension. If the
  309. control is appropriate for an operation and, for any reason, it
  310. cannot be applied in its entirety to a single SearchResultEntry
  311. response, it MUST NOT be applied to that specific SearchResultEntry
  312. response, without affecting its application to any subsequent
  313. SearchResultEntry response.
  314. */
  315. /* so for now, just return LDAP_SUCCESS and don't do anything else */
  316. aucctrl = NULL;
  317. }
  318. /* Check access control if all the above parsing went OK.
  319. * Skip this for the root DN. */
  320. if (aucctrl && (ldapcode == LDAP_SUCCESS) && !isroot) {
  321. char dn[128];
  322. Slapi_Entry *feature = NULL;
  323. /* Fetch the feature entry and see if the requestor is allowed access. */
  324. PR_snprintf(dn, sizeof(dn), "dn: oid=%s,cn=features,cn=config", AUC_OID);
  325. if ((feature = slapi_str2entry(dn,0)) != NULL) {
  326. char *dummy_attr = "1.1";
  327. ldapcode = slapi_access_allowed(pb, feature, dummy_attr, NULL, SLAPI_ACL_READ);
  328. }
  329. /* If the feature entry does not exist, deny use of the control. Only
  330. * the root DN will be allowed to use the control in this case. */
  331. if ((feature == NULL) || (ldapcode != LDAP_SUCCESS)) {
  332. ldapcode = LDAP_INSUFFICIENT_ACCESS;
  333. ldaperrtext = "Insufficient access rights to use the account usability request control";
  334. }
  335. slapi_entry_free(feature);
  336. }
  337. if (ldapcode != LDAP_SUCCESS) {
  338. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &ldapcode);
  339. slapi_send_ldap_result(pb, ldapcode, NULL, (char *)ldaperrtext, 0, NULL);
  340. }
  341. slapi_log_error(SLAPI_LOG_TRACE, AUC_PLUGIN_SUBSYSTEM,
  342. "<-- auc_pre_op\n");
  343. return ldapcode;
  344. }
  345. static int
  346. auc_pre_entry(Slapi_PBlock *pb)
  347. {
  348. int ii = 0;
  349. int need_response = 0;
  350. LDAPControl *ctrl = NULL;
  351. const LDAPControl **reqctrls = NULL;
  352. const LDAPControl **searchctrls = NULL;
  353. LDAPControl **newsearchctrls = NULL;
  354. /* See if the account usability request control was specified. */
  355. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqctrls);
  356. for (ii = 0; reqctrls && reqctrls[ii]; ++ii) {
  357. if (!strcmp(reqctrls[ii]->ldctl_oid, AUC_OID)) {
  358. need_response = 1;
  359. break;
  360. }
  361. }
  362. /* Generate the response control if requested. */
  363. if (need_response) {
  364. Slapi_Entry *e = NULL;
  365. /* grab the entry to be returned */
  366. slapi_pblock_get(pb, SLAPI_SEARCH_ENTRY_ORIG, &e);
  367. if (!e) {
  368. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  369. "auc_pre_entry: Unable to fetch entry.\n");
  370. goto bail;
  371. }
  372. /* create the respose control */
  373. ctrl = auc_create_response_ctrl(e);
  374. if (!ctrl) {
  375. slapi_log_error(SLAPI_LOG_FATAL, AUC_PLUGIN_SUBSYSTEM,
  376. "auc_pre_entry: Error creating response control for entry \"%s\".\n",
  377. slapi_entry_get_ndn(e) ? slapi_entry_get_ndn(e) : "null");
  378. goto bail;
  379. }
  380. /* get the list of controls */
  381. slapi_pblock_get(pb, SLAPI_SEARCH_CTRLS, &searchctrls);
  382. /* dup them */
  383. slapi_add_controls(&newsearchctrls, (LDAPControl **)searchctrls, 1);
  384. /* add our control */
  385. slapi_add_control_ext(&newsearchctrls, ctrl, 0);
  386. ctrl = NULL; /* newsearchctrls owns it now */
  387. /* set the controls in the pblock */
  388. slapi_pblock_set(pb, SLAPI_SEARCH_CTRLS, newsearchctrls);
  389. }
  390. bail:
  391. return 0;
  392. }