1
0

rootdn_access.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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) 2012 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Root DN Access Control plug-in
  42. */
  43. #include "rootdn_access.h"
  44. #include <nspr.h>
  45. #include <time.h>
  46. #include <ctype.h>
  47. #include <string.h>
  48. /*
  49. * Add an entry like the following to dse.ldif to enable this plugin:
  50. *
  51. * dn: cn=RootDN Access Control,cn=plugins,cn=config
  52. * objectclass: top
  53. * objectclass: nsSlapdPlugin
  54. * objectclass: extensibleObject
  55. * cn: RootDN Access Control
  56. * nsslapd-pluginpath: librootdn-access-plugin.so
  57. * nsslapd-plugininitfunc: rootdn_init
  58. * nsslapd-plugintype: rootdnpreoperation
  59. * nsslapd-pluginenabled: on
  60. * nsslapd-plugin-depends-on-type: database
  61. * nsslapd-pluginid: rootdn-access-control
  62. * rootdn-open-time: 0800
  63. * rootdn-close-time: 1700
  64. * rootdn-days-allowed: Mon, Tue, Wed, Thu, Fri
  65. * rootdn-allow-host: *.redhat.com
  66. * rootdn-allow-host: *.fedora.com
  67. * rootdn-deny-host: dangerous.boracle.com
  68. * rootdn-allow-ip: 127.0.0.1
  69. * rootdn-allow-ip: 2000:db8:de30::11
  70. * rootdn-deny-ip: 192.168.1.*
  71. *
  72. */
  73. /*
  74. * Plugin Functions
  75. */
  76. int rootdn_init(Slapi_PBlock *pb);
  77. static int rootdn_start(Slapi_PBlock *pb);
  78. static int rootdn_close(Slapi_PBlock *pb);
  79. static int rootdn_load_config(Slapi_PBlock *pb);
  80. static int rootdn_check_access(Slapi_PBlock *pb);
  81. static int rootdn_check_host_wildcard(char *host, char *client_host);
  82. static int rootdn_check_ip_wildcard(char *ip, char *client_ip);
  83. static int rootdn_preop_bind_init(Slapi_PBlock *pb);
  84. char * strToLower(char *str);
  85. /*
  86. * Plug-in globals
  87. */
  88. static void *_PluginID = NULL;
  89. static char *_PluginDN = NULL;
  90. static int open_time = 0;
  91. static int close_time = 0;
  92. static char *daysAllowed = NULL;
  93. static char **hosts = NULL;
  94. static char **hosts_to_deny = NULL;
  95. static char **ips = NULL;
  96. static char **ips_to_deny = NULL;
  97. static Slapi_PluginDesc pdesc = { ROOTDN_FEATURE_DESC,
  98. VENDOR,
  99. DS_PACKAGE_VERSION,
  100. ROOTDN_PLUGIN_DESC };
  101. /*
  102. * Plugin identity functions
  103. */
  104. void
  105. rootdn_set_plugin_id(void *pluginID)
  106. {
  107. _PluginID = pluginID;
  108. }
  109. void *
  110. rootdn_get_plugin_id()
  111. {
  112. return _PluginID;
  113. }
  114. void
  115. rootdn_set_plugin_dn(char *pluginDN)
  116. {
  117. _PluginDN = pluginDN;
  118. }
  119. char *
  120. rootdn_get_plugin_dn()
  121. {
  122. return _PluginDN;
  123. }
  124. int
  125. rootdn_init(Slapi_PBlock *pb){
  126. int status = 0;
  127. char *plugin_identity = NULL;
  128. slapi_log_error(SLAPI_LOG_TRACE, ROOTDN_PLUGIN_SUBSYSTEM,
  129. "--> rootdn_init\n");
  130. /* Store the plugin identity for later use. Used for internal operations. */
  131. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  132. PR_ASSERT(plugin_identity);
  133. rootdn_set_plugin_id(plugin_identity);
  134. /* Register callbacks */
  135. if(slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01) != 0 ||
  136. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *) rootdn_start) != 0 ||
  137. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, (void *) rootdn_close) != 0 ||
  138. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &pdesc) != 0 )
  139. {
  140. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_init: failed to register plugin\n");
  141. status = -1;
  142. }
  143. /* for this plugin we don't want to skip over root dn's when binding */
  144. slapi_set_plugin_open_rootdn_bind(pb);
  145. if (!status &&
  146. slapi_register_plugin("internalpreoperation", /* op type */
  147. 1, /* Enabled */
  148. "rootdn_preop_bind_init", /* this function desc */
  149. rootdn_preop_bind_init, /* init func */
  150. ROOTDN_PLUGIN_DESC, /* plugin desc */
  151. NULL, /* ? */
  152. plugin_identity /* access control */
  153. )) {
  154. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM,
  155. "rootdn_init: failed to register rootdn preoperation plugin\n");
  156. status = -1;
  157. }
  158. /*
  159. * Load the config
  160. */
  161. if(rootdn_load_config(pb) != 0){
  162. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM,
  163. "rootdn_start: unable to load plug-in configuration\n");
  164. return -1;
  165. }
  166. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM,"<-- rootdn_init\n");
  167. return status;
  168. }
  169. static int
  170. rootdn_preop_bind_init(Slapi_PBlock *pb)
  171. {
  172. if(slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN, (void *) rootdn_check_access) != 0){
  173. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM,"rootdn_preop_bind_init: "
  174. "failed to register function\n");
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. static int
  180. rootdn_start(Slapi_PBlock *pb)
  181. {
  182. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "--> rootdn_start\n");
  183. rootdn_set_plugin_dn(ROOTDN_PLUGIN_DN);
  184. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "<-- rootdn_start\n");
  185. return 0;
  186. }
  187. static void
  188. rootdn_free()
  189. {
  190. slapi_ch_free_string(&daysAllowed);
  191. daysAllowed = NULL;
  192. slapi_ch_array_free(hosts);
  193. hosts = NULL;
  194. slapi_ch_array_free(hosts_to_deny);
  195. hosts_to_deny = NULL;
  196. slapi_ch_array_free(ips);
  197. ips = NULL;
  198. slapi_ch_array_free(ips_to_deny);
  199. ips_to_deny = NULL;
  200. }
  201. static int
  202. rootdn_close(Slapi_PBlock *pb)
  203. {
  204. rootdn_free();
  205. return 0;
  206. }
  207. static int
  208. rootdn_load_config(Slapi_PBlock *pb)
  209. {
  210. Slapi_Entry *e = NULL;
  211. char *daysAllowed_tmp = NULL;
  212. char **hosts_tmp = NULL;
  213. char **hosts_to_deny_tmp = NULL;
  214. char **ips_tmp = NULL;
  215. char **ips_to_deny_tmp = NULL;
  216. char *openTime = NULL;
  217. char *closeTime = NULL;
  218. char *token, *iter = NULL, *copy;
  219. char hour[3], min[3];
  220. size_t end;
  221. int result = 0;
  222. int time;
  223. int i;
  224. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "--> rootdn_load_config\n");
  225. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &e) == 0) && e){
  226. /*
  227. * Grab our plugin settings
  228. */
  229. openTime = slapi_entry_attr_get_charptr(e, "rootdn-open-time");
  230. closeTime = slapi_entry_attr_get_charptr(e, "rootdn-close-time");
  231. daysAllowed_tmp = slapi_entry_attr_get_charptr(e, "rootdn-days-allowed");
  232. hosts_tmp = slapi_entry_attr_get_charray(e, "rootdn-allow-host");
  233. hosts_to_deny_tmp = slapi_entry_attr_get_charray(e, "rootdn-deny-host");
  234. ips_tmp = slapi_entry_attr_get_charray(e, "rootdn-allow-ip");
  235. ips_to_deny_tmp = slapi_entry_attr_get_charray(e, "rootdn-deny-ip");
  236. /*
  237. * Validate out settings
  238. */
  239. if(daysAllowed_tmp){
  240. daysAllowed_tmp = strToLower(daysAllowed_tmp);
  241. end = strspn(daysAllowed_tmp, "abcdefghijklmnopqrstuvwxyz ,");
  242. if(!end || daysAllowed_tmp[end] != '\0'){
  243. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  244. "invalid rootdn-days-allowed value (%s), must be all letters, and comma separators\n", daysAllowed_tmp);
  245. slapi_ch_free_string(&daysAllowed_tmp);
  246. result = -1;
  247. goto free_and_return;
  248. }
  249. /* make sure the "days" are valid "days" */
  250. copy = slapi_ch_strdup(daysAllowed_tmp);
  251. token = ldap_utf8strtok_r(copy, ", ", &iter);
  252. while(token){
  253. if(strstr("mon tue wed thu fri sat sun",token) == 0){
  254. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  255. "invalid rootdn-days-allowed day value(%s), must be \"Mon, Tue, Wed, Thu, Fri, Sat, or Sun\".\n", token);
  256. slapi_ch_free_string(&daysAllowed_tmp);
  257. slapi_ch_free_string(&copy);
  258. result = -1;
  259. goto free_and_return;
  260. }
  261. token = ldap_utf8strtok_r(iter, ", ", &iter);
  262. }
  263. slapi_ch_free_string(&copy);
  264. }
  265. if(openTime){
  266. end = strspn(openTime, "0123456789");
  267. if (!end || openTime[end] != '\0'){
  268. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  269. "invalid rootdn-open-time value (%s), must be all digits\n", openTime);
  270. result = -1;
  271. goto free_and_return;
  272. }
  273. time = atoi(openTime);
  274. if(time > 2359 || time < 0){
  275. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  276. "invalid value for rootdn-open-time value (%s), value must be between 0000-2359\n", openTime);
  277. result = -1;
  278. goto free_and_return;
  279. }
  280. if(strlen(openTime) != 4){
  281. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  282. "invalid format for rootdn-open-time value (%s). Should be HHMM\n", openTime);
  283. result = -1;
  284. goto free_and_return;
  285. }
  286. /*
  287. * convert the time to all seconds
  288. */
  289. strncpy(hour, openTime,2);
  290. strncpy(min, openTime+2,2);
  291. open_time = (atoi(hour) * 3600) + (atoi(min) * 60);
  292. }
  293. if(closeTime){
  294. end = strspn(closeTime, "0123456789");
  295. if (!end || closeTime[end] != '\0'){
  296. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  297. "invalid rootdn-close-time value (%s), must be all digits, and should be HHMM\n",closeTime);
  298. result = -1;
  299. goto free_and_return;
  300. }
  301. time = atoi(closeTime);
  302. if(time > 2359 || time < 0){
  303. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  304. "invalid value for rootdn-close-time value (%s), value must be between 0000-2359\n", closeTime);
  305. result = -1;
  306. goto free_and_return;
  307. }
  308. if(strlen(closeTime) != 4){
  309. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  310. "invalid format for rootdn-close-time value (%s), should be HHMM\n", closeTime);
  311. result = -1;
  312. goto free_and_return;
  313. }
  314. /*
  315. * convert the time to all seconds
  316. */
  317. strncpy(hour, closeTime,2);
  318. strncpy(min, closeTime+2,2);
  319. close_time = (atoi(hour) * 3600) + (atoi(min) * 60);
  320. }
  321. if((openTime && closeTime == NULL) || (openTime == NULL && closeTime)){
  322. /* If you are using TOD access control, you must have a open and close time */
  323. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  324. "there must be a open and a close time. Ignoring time based settings.\n");
  325. slapi_ch_free_string(&closeTime);
  326. slapi_ch_free_string(&openTime);
  327. open_time = 0;
  328. close_time = 0;
  329. result = -1;
  330. goto free_and_return;
  331. }
  332. if(close_time && open_time && close_time <= open_time){
  333. /* Make sure the closing time is greater than the open time */
  334. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  335. "the close time must be greater than the open time\n");
  336. result = -1;
  337. goto free_and_return;
  338. }
  339. if(hosts_tmp){
  340. for(i = 0; hosts_tmp[i] != NULL; i++){
  341. end = strspn(hosts_tmp[i], "0123456789.*-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  342. if(!end || hosts_tmp[i][end] != '\0'){
  343. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  344. "hostname (%s) contains invalid characters, skipping\n",hosts_tmp[i]);
  345. slapi_ch_array_free(hosts_tmp);
  346. result = -1;
  347. goto free_and_return;
  348. }
  349. }
  350. }
  351. if(hosts_to_deny_tmp){
  352. for(i = 0; hosts_to_deny_tmp[i] != NULL; i++){
  353. end = strspn(hosts_to_deny_tmp[i], "0123456789.*-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  354. if(!end || hosts_to_deny_tmp[i][end] != '\0'){
  355. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  356. "hostname (%s) contains invalid characters, skipping\n",hosts_to_deny_tmp[i]);
  357. slapi_ch_array_free(hosts_to_deny_tmp);
  358. result = -1;
  359. goto free_and_return;
  360. }
  361. }
  362. }
  363. if(ips_tmp){
  364. for(i = 0; ips_tmp[i] != NULL; i++){
  365. end = strspn(ips_tmp[i], "0123456789:ABCDEFabcdef.");
  366. if(!end || ips_tmp[i][end] != '\0'){
  367. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  368. "IP address contains invalid characters (%s), skipping\n", ips_tmp[i]);
  369. slapi_ch_array_free(ips_tmp);
  370. result = -1;
  371. goto free_and_return;
  372. }
  373. if(strstr(ips_tmp[i],":") == 0){
  374. /*
  375. * IPv4 - make sure it's just numbers, dots, and wildcard
  376. */
  377. end = strspn(ips_tmp[i], "0123456789.*");
  378. if(!end || ips_tmp[i][end] != '\0'){
  379. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  380. "IPv4 address contains invalid characters (%s), skipping\n", ips_tmp[i]);
  381. slapi_ch_array_free(ips_tmp);
  382. result = -1;
  383. goto free_and_return;
  384. }
  385. }
  386. }
  387. }
  388. if(ips_to_deny_tmp){
  389. for(i = 0; ips_to_deny_tmp[i] != NULL; i++){
  390. end = strspn(ips_to_deny_tmp[i], "0123456789:ABCDEFabcdef.*");
  391. if(!end || ips_to_deny_tmp[i][end] != '\0'){
  392. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  393. "IP address contains invalid characters (%s), skipping\n", ips_to_deny_tmp[i]);
  394. slapi_ch_array_free(ips_to_deny_tmp);
  395. result = -1;
  396. goto free_and_return;
  397. }
  398. if(strstr(ips_to_deny_tmp[i],":") == 0){
  399. /*
  400. * IPv4 - make sure it's just numbers, dots, and wildcard
  401. */
  402. end = strspn(ips_to_deny_tmp[i], "0123456789.*");
  403. if(!end || ips_to_deny_tmp[i][end] != '\0'){
  404. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  405. "IPv4 address contains invalid characters (%s), skipping\n", ips_to_deny_tmp[i]);
  406. slapi_ch_array_free(ips_to_deny_tmp);
  407. result = -1;
  408. goto free_and_return;
  409. }
  410. }
  411. }
  412. }
  413. } else {
  414. /* failed to get the plugin entry */
  415. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  416. "Failed to get plugin entry\n");
  417. result = -1;
  418. }
  419. free_and_return:
  420. if(result == 0){
  421. /*
  422. * Free the existing global vars, and move the new ones over
  423. */
  424. rootdn_free();
  425. daysAllowed = daysAllowed_tmp;
  426. hosts = hosts_tmp;
  427. hosts_to_deny = hosts_to_deny_tmp;
  428. ips = ips_tmp;
  429. ips_to_deny = ips_to_deny_tmp;
  430. }
  431. slapi_ch_free_string(&openTime);
  432. slapi_ch_free_string(&closeTime);
  433. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "<-- rootdn_load_config (%d)\n", result);
  434. return result;
  435. }
  436. static int
  437. rootdn_check_access(Slapi_PBlock *pb){
  438. PRNetAddr *client_addr = NULL;
  439. PRHostEnt *host_entry = NULL;
  440. time_t curr_time;
  441. struct tm *timeinfo;
  442. char *dnsName = NULL;
  443. int isRoot = 0;
  444. int rc = SLAPI_PLUGIN_SUCCESS;
  445. int i;
  446. /*
  447. * Verify this is a root DN
  448. */
  449. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  450. if(!isRoot){
  451. return SLAPI_PLUGIN_SUCCESS;
  452. }
  453. /*
  454. * grab the current time/info if we need it
  455. */
  456. if(open_time || daysAllowed){
  457. time(&curr_time);
  458. timeinfo = localtime(&curr_time);
  459. }
  460. /*
  461. * First check TOD restrictions, continue through if we are in the open "window"
  462. */
  463. if(open_time){
  464. int curr_total;
  465. curr_total = (timeinfo->tm_hour * 3600) + (timeinfo->tm_min * 60);
  466. if((curr_total < open_time) || (curr_total >= close_time)){
  467. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not in the "
  468. "allowed time window\n");
  469. return -1;
  470. }
  471. }
  472. /*
  473. * Check if today is an allowed day
  474. */
  475. if(daysAllowed){
  476. char *timestr;
  477. char day[4];
  478. char *today = day;
  479. timestr = asctime(timeinfo); // DDD MMM dd hh:mm:ss YYYY
  480. memset(day, 0 ,sizeof(day));
  481. memmove(day, timestr, 3); // we only want the day
  482. today = strToLower(today);
  483. daysAllowed = strToLower(daysAllowed);
  484. if(!strstr(daysAllowed, today)){
  485. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not allowed for today(%s), "
  486. "only allowed on days: %s\n", today, daysAllowed);
  487. return -1;
  488. }
  489. }
  490. /*
  491. * Check the host restrictions, deny always overrides allow
  492. */
  493. if(hosts || hosts_to_deny){
  494. char buf[PR_NETDB_BUF_SIZE];
  495. char *host;
  496. /*
  497. * Get the client address
  498. */
  499. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  500. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  501. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for hosts.\n" );
  502. rc = -1;
  503. goto free_and_return;
  504. }
  505. /*
  506. * Get the hostname from the client address
  507. */
  508. host_entry = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
  509. if ( PR_GetHostByAddr(client_addr, buf, sizeof(buf), host_entry ) == PR_SUCCESS ) {
  510. if ( host_entry->h_name != NULL ) {
  511. /* copy the hostname */
  512. dnsName = slapi_ch_strdup( host_entry->h_name );
  513. } else {
  514. /* no hostname */
  515. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client address missing hostname\n");
  516. rc = -1;
  517. goto free_and_return;
  518. }
  519. } else {
  520. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client IP address could not be resolved\n");
  521. rc = -1;
  522. goto free_and_return;
  523. }
  524. /*
  525. * Now we have our hostname, now do our checks
  526. */
  527. if(hosts_to_deny){
  528. for(i = 0; hosts_to_deny[i] != NULL; i++){
  529. host = hosts_to_deny[i];
  530. /* check for wild cards */
  531. if(host[0] == '*'){
  532. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  533. /* match, return failure */
  534. rc = -1;
  535. goto free_and_return;
  536. }
  537. } else {
  538. if(strcasecmp(host,dnsName) == 0){
  539. /* we have a match, return failure */
  540. rc = -1;
  541. goto free_and_return;
  542. }
  543. }
  544. }
  545. rc = 0;
  546. }
  547. if(hosts){
  548. for(i = 0; hosts[i] != NULL; i++){
  549. host = hosts[i];
  550. /* check for wild cards */
  551. if(host[0] == '*'){
  552. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  553. /* match */
  554. rc = 0;
  555. goto free_and_return;
  556. }
  557. } else {
  558. if(strcasecmp(host,dnsName) == 0){
  559. /* we have a match, */
  560. rc = 0;
  561. goto free_and_return;
  562. }
  563. }
  564. }
  565. rc = -1;
  566. }
  567. }
  568. /*
  569. * Check the IP address restrictions, deny always overrides allow
  570. */
  571. if(ips || ips_to_deny){
  572. char ip_str[256];
  573. char *ip;
  574. int ip_len, i;
  575. if(client_addr == NULL){
  576. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  577. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  578. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for IP.\n" );
  579. rc = -1;
  580. goto free_and_return;
  581. }
  582. }
  583. /*
  584. * Check if we are IPv4, so we can grab the correct IP addr for "ip_str"
  585. */
  586. if ( PR_IsNetAddrType( client_addr, PR_IpAddrV4Mapped ) ) {
  587. PRNetAddr v4addr;
  588. memset( &v4addr, 0, sizeof( v4addr ) );
  589. v4addr.inet.family = PR_AF_INET;
  590. v4addr.inet.ip = client_addr->ipv6.ip.pr_s6_addr32[3];
  591. if( PR_NetAddrToString( &v4addr, ip_str, sizeof( ip_str )) != PR_SUCCESS){
  592. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv4 from client address.\n" );
  593. rc = -1;
  594. goto free_and_return;
  595. }
  596. } else {
  597. if( PR_NetAddrToString(client_addr, ip_str, sizeof(ip_str)) != PR_SUCCESS){
  598. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv6 from client address.\n" );
  599. rc = -1;
  600. goto free_and_return;
  601. }
  602. }
  603. /*
  604. * Now we have our IP address, do our checks
  605. */
  606. if(ips_to_deny){
  607. for(i = 0; ips_to_deny[i] != NULL; i++){
  608. ip = ips_to_deny[i];
  609. ip_len = strlen(ip);
  610. if(ip[ip_len - 1] == '*'){
  611. if(rootdn_check_ip_wildcard(ips_to_deny[i], ip_str) == 0){
  612. /* match, return failure */
  613. rc = -1;
  614. goto free_and_return;
  615. }
  616. } else {
  617. if(strcasecmp(ip_str, ip)==0){
  618. /* match, return failure */
  619. rc = -1;
  620. goto free_and_return;
  621. }
  622. }
  623. }
  624. rc = 0;
  625. }
  626. if(ips){
  627. for(i = 0; ips[i] != NULL; i++){
  628. ip = ips[i];
  629. ip_len = strlen(ip);
  630. if(ip[ip_len - 1] == '*'){
  631. if(rootdn_check_ip_wildcard(ip, ip_str) == 0){
  632. /* match, return success */
  633. rc = 0;
  634. goto free_and_return;
  635. }
  636. } else {
  637. if(strcasecmp(ip_str, ip)==0){
  638. /* match, return success */
  639. rc = 0;
  640. goto free_and_return;
  641. }
  642. }
  643. }
  644. rc = -1;
  645. }
  646. }
  647. free_and_return:
  648. slapi_ch_free((void **)&client_addr);
  649. slapi_ch_free((void **)&host_entry);
  650. slapi_ch_free_string(&dnsName);
  651. return rc;
  652. }
  653. static int
  654. rootdn_check_host_wildcard(char *host, char *client_host)
  655. {
  656. int host_len = strlen(host);
  657. int client_len = strlen(client_host);
  658. int i, j;
  659. /*
  660. * Start at the end of the string and move backwards, and skip the first char "*"
  661. */
  662. if(client_len < host_len){
  663. /* this can't be a match */
  664. return -1;
  665. }
  666. for(i = host_len - 1, j = client_len - 1; i > 0; i--, j--){
  667. if(host[i] != client_host[j]){
  668. return -1;
  669. }
  670. }
  671. return 0;
  672. }
  673. static int
  674. rootdn_check_ip_wildcard(char *ip, char *client_ip)
  675. {
  676. int ip_len = strlen(ip);
  677. int i;
  678. /*
  679. * Start at the beginning of the string and move forward, and skip the last char "*"
  680. */
  681. if(strlen(client_ip) < ip_len){
  682. /* this can't be a match */
  683. return -1;
  684. }
  685. for(i = 0; i < ip_len - 1; i++){
  686. if(ip[i] != client_ip[i]){
  687. return -1;
  688. }
  689. }
  690. return 0;
  691. }
  692. char *
  693. strToLower(char *str){
  694. int i;
  695. for(i = 0; str && i < strlen(str); i++){
  696. str[i] = tolower(str[i]);
  697. }
  698. return str;
  699. }