rootdn_access.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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(void)
  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(void)
  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_err(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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM,
  134. "rootdn_init - Unable to load plug-in configuration\n");
  135. return -1;
  136. }
  137. slapi_log_err(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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "--> rootdn_start\n");
  154. rootdn_set_plugin_dn(ROOTDN_PLUGIN_DN);
  155. slapi_log_err(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "<-- rootdn_start\n");
  156. return 0;
  157. }
  158. static void
  159. rootdn_free(void)
  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_err(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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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_err(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_err(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] = {0};
  449. char *today = day;
  450. timestr = asctime(timeinfo); // DDD MMM dd hh:mm:ss YYYY
  451. memmove(day, timestr, 3); // we only want the day
  452. today = strToLower(today);
  453. daysAllowed = strToLower(daysAllowed);
  454. if(!strstr(daysAllowed, today)){
  455. slapi_log_err(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  456. "Bind not allowed for today(%s), only allowed on days: %s\n", today, daysAllowed);
  457. return -1;
  458. }
  459. }
  460. /*
  461. * Check the host restrictions, deny always overrides allow
  462. */
  463. if(hosts || hosts_to_deny){
  464. char buf[PR_NETDB_BUF_SIZE];
  465. char *host;
  466. /*
  467. * Get the client address
  468. */
  469. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  470. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  471. slapi_log_err(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  472. "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_err(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  487. "Client address missing hostname\n");
  488. rc = -1;
  489. goto free_and_return;
  490. }
  491. } else {
  492. slapi_log_err(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  493. "client IP address could not be resolved\n");
  494. rc = -1;
  495. goto free_and_return;
  496. }
  497. /*
  498. * Now we have our hostname, now do our checks
  499. */
  500. if(hosts_to_deny){
  501. for(i = 0; hosts_to_deny[i] != NULL; i++){
  502. host = hosts_to_deny[i];
  503. /* check for wild cards */
  504. if(host[0] == '*'){
  505. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  506. /* match, return failure */
  507. rc = -1;
  508. goto free_and_return;
  509. }
  510. } else {
  511. if(strcasecmp(host,dnsName) == 0){
  512. /* we have a match, return failure */
  513. rc = -1;
  514. goto free_and_return;
  515. }
  516. }
  517. }
  518. rc = 0;
  519. }
  520. if(hosts){
  521. for(i = 0; hosts[i] != NULL; i++){
  522. host = hosts[i];
  523. /* check for wild cards */
  524. if(host[0] == '*'){
  525. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  526. /* match */
  527. rc = 0;
  528. goto free_and_return;
  529. }
  530. } else {
  531. if(strcasecmp(host,dnsName) == 0){
  532. /* we have a match, */
  533. rc = 0;
  534. goto free_and_return;
  535. }
  536. }
  537. }
  538. rc = -1;
  539. }
  540. }
  541. /*
  542. * Check the IP address restrictions, deny always overrides allow
  543. */
  544. if(ips || ips_to_deny){
  545. char ip_str[256];
  546. char *ip;
  547. int ip_len, i;
  548. if(client_addr == NULL){
  549. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  550. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  551. slapi_log_err(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get client address for IP.\n" );
  552. rc = -1;
  553. goto free_and_return;
  554. }
  555. }
  556. /*
  557. * Check if we are IPv4, so we can grab the correct IP addr for "ip_str"
  558. */
  559. if ( PR_IsNetAddrType( client_addr, PR_IpAddrV4Mapped ) ) {
  560. PRNetAddr v4addr = {{0}};
  561. v4addr.inet.family = PR_AF_INET;
  562. v4addr.inet.ip = client_addr->ipv6.ip.pr_s6_addr32[3];
  563. if( PR_NetAddrToString( &v4addr, ip_str, sizeof( ip_str )) != PR_SUCCESS){
  564. slapi_log_err(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get IPv4 from client address.\n" );
  565. rc = -1;
  566. goto free_and_return;
  567. }
  568. } else {
  569. if( PR_NetAddrToString(client_addr, ip_str, sizeof(ip_str)) != PR_SUCCESS){
  570. slapi_log_err(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get IPv6 from client address.\n" );
  571. rc = -1;
  572. goto free_and_return;
  573. }
  574. }
  575. /*
  576. * Now we have our IP address, do our checks
  577. */
  578. if(ips_to_deny){
  579. for(i = 0; ips_to_deny[i] != NULL; i++){
  580. ip = ips_to_deny[i];
  581. ip_len = strlen(ip);
  582. if(ip[ip_len - 1] == '*'){
  583. if(rootdn_check_ip_wildcard(ips_to_deny[i], ip_str) == 0){
  584. /* match, return failure */
  585. rc = -1;
  586. goto free_and_return;
  587. }
  588. } else {
  589. if(strcasecmp(ip_str, ip)==0){
  590. /* match, return failure */
  591. rc = -1;
  592. goto free_and_return;
  593. }
  594. }
  595. }
  596. rc = 0;
  597. }
  598. if(ips){
  599. for(i = 0; ips[i] != NULL; i++){
  600. ip = ips[i];
  601. ip_len = strlen(ip);
  602. if(ip[ip_len - 1] == '*'){
  603. if(rootdn_check_ip_wildcard(ip, ip_str) == 0){
  604. /* match, return success */
  605. rc = 0;
  606. goto free_and_return;
  607. }
  608. } else {
  609. if(strcasecmp(ip_str, ip)==0){
  610. /* match, return success */
  611. rc = 0;
  612. goto free_and_return;
  613. }
  614. }
  615. }
  616. rc = -1;
  617. }
  618. }
  619. free_and_return:
  620. slapi_ch_free((void **)&client_addr);
  621. slapi_ch_free((void **)&host_entry);
  622. slapi_ch_free_string(&dnsName);
  623. return rc;
  624. }
  625. static int
  626. rootdn_check_host_wildcard(char *host, char *client_host)
  627. {
  628. int host_len = strlen(host);
  629. int client_len = strlen(client_host);
  630. int i, j;
  631. /*
  632. * Start at the end of the string and move backwards, and skip the first char "*"
  633. */
  634. if(client_len < host_len){
  635. /* this can't be a match */
  636. return -1;
  637. }
  638. for(i = host_len - 1, j = client_len - 1; i > 0; i--, j--){
  639. if(host[i] != client_host[j]){
  640. return -1;
  641. }
  642. }
  643. return 0;
  644. }
  645. static int
  646. rootdn_check_ip_wildcard(char *ip, char *client_ip)
  647. {
  648. size_t ip_len = strlen(ip);
  649. int i;
  650. /*
  651. * Start at the beginning of the string and move forward, and skip the last char "*"
  652. */
  653. if(strlen(client_ip) < ip_len){
  654. /* this can't be a match */
  655. return -1;
  656. }
  657. for(i = 0; i < ip_len - 1; i++){
  658. if(ip[i] != client_ip[i]){
  659. return -1;
  660. }
  661. }
  662. return 0;
  663. }
  664. char *
  665. strToLower(char *str){
  666. size_t i;
  667. for(i = 0; str && i < strlen(str); i++){
  668. str[i] = tolower(str[i]);
  669. }
  670. return str;
  671. }