1
0

acct_usability.c 13 KB

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