rootdn_access.c 25 KB

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