rootdn_access.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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_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_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_error(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_error(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM,
  134. "rootdn_init - 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_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_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(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_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_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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_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 - "
  457. "Bind not allowed for today(%s), 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_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  473. "Could not get client address for hosts.\n" );
  474. rc = -1;
  475. goto free_and_return;
  476. }
  477. /*
  478. * Get the hostname from the client address
  479. */
  480. host_entry = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
  481. if ( PR_GetHostByAddr(client_addr, buf, sizeof(buf), host_entry ) == PR_SUCCESS ) {
  482. if ( host_entry->h_name != NULL ) {
  483. /* copy the hostname */
  484. dnsName = slapi_ch_strdup( host_entry->h_name );
  485. } else {
  486. /* no hostname */
  487. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  488. "Client address missing hostname\n");
  489. rc = -1;
  490. goto free_and_return;
  491. }
  492. } else {
  493. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - "
  494. "client IP address could not be resolved\n");
  495. rc = -1;
  496. goto free_and_return;
  497. }
  498. /*
  499. * Now we have our hostname, now do our checks
  500. */
  501. if(hosts_to_deny){
  502. for(i = 0; hosts_to_deny[i] != NULL; i++){
  503. host = hosts_to_deny[i];
  504. /* check for wild cards */
  505. if(host[0] == '*'){
  506. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  507. /* match, return failure */
  508. rc = -1;
  509. goto free_and_return;
  510. }
  511. } else {
  512. if(strcasecmp(host,dnsName) == 0){
  513. /* we have a match, return failure */
  514. rc = -1;
  515. goto free_and_return;
  516. }
  517. }
  518. }
  519. rc = 0;
  520. }
  521. if(hosts){
  522. for(i = 0; hosts[i] != NULL; i++){
  523. host = hosts[i];
  524. /* check for wild cards */
  525. if(host[0] == '*'){
  526. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  527. /* match */
  528. rc = 0;
  529. goto free_and_return;
  530. }
  531. } else {
  532. if(strcasecmp(host,dnsName) == 0){
  533. /* we have a match, */
  534. rc = 0;
  535. goto free_and_return;
  536. }
  537. }
  538. }
  539. rc = -1;
  540. }
  541. }
  542. /*
  543. * Check the IP address restrictions, deny always overrides allow
  544. */
  545. if(ips || ips_to_deny){
  546. char ip_str[256];
  547. char *ip;
  548. int ip_len, i;
  549. if(client_addr == NULL){
  550. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  551. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  552. slapi_log_error(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get client address for IP.\n" );
  553. rc = -1;
  554. goto free_and_return;
  555. }
  556. }
  557. /*
  558. * Check if we are IPv4, so we can grab the correct IP addr for "ip_str"
  559. */
  560. if ( PR_IsNetAddrType( client_addr, PR_IpAddrV4Mapped ) ) {
  561. PRNetAddr v4addr;
  562. memset( &v4addr, 0, sizeof( v4addr ) );
  563. v4addr.inet.family = PR_AF_INET;
  564. v4addr.inet.ip = client_addr->ipv6.ip.pr_s6_addr32[3];
  565. if( PR_NetAddrToString( &v4addr, ip_str, sizeof( ip_str )) != PR_SUCCESS){
  566. slapi_log_error(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get IPv4 from client address.\n" );
  567. rc = -1;
  568. goto free_and_return;
  569. }
  570. } else {
  571. if( PR_NetAddrToString(client_addr, ip_str, sizeof(ip_str)) != PR_SUCCESS){
  572. slapi_log_error(SLAPI_LOG_ERR, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access - Could not get IPv6 from client address.\n" );
  573. rc = -1;
  574. goto free_and_return;
  575. }
  576. }
  577. /*
  578. * Now we have our IP address, do our checks
  579. */
  580. if(ips_to_deny){
  581. for(i = 0; ips_to_deny[i] != NULL; i++){
  582. ip = ips_to_deny[i];
  583. ip_len = strlen(ip);
  584. if(ip[ip_len - 1] == '*'){
  585. if(rootdn_check_ip_wildcard(ips_to_deny[i], ip_str) == 0){
  586. /* match, return failure */
  587. rc = -1;
  588. goto free_and_return;
  589. }
  590. } else {
  591. if(strcasecmp(ip_str, ip)==0){
  592. /* match, return failure */
  593. rc = -1;
  594. goto free_and_return;
  595. }
  596. }
  597. }
  598. rc = 0;
  599. }
  600. if(ips){
  601. for(i = 0; ips[i] != NULL; i++){
  602. ip = ips[i];
  603. ip_len = strlen(ip);
  604. if(ip[ip_len - 1] == '*'){
  605. if(rootdn_check_ip_wildcard(ip, ip_str) == 0){
  606. /* match, return success */
  607. rc = 0;
  608. goto free_and_return;
  609. }
  610. } else {
  611. if(strcasecmp(ip_str, ip)==0){
  612. /* match, return success */
  613. rc = 0;
  614. goto free_and_return;
  615. }
  616. }
  617. }
  618. rc = -1;
  619. }
  620. }
  621. free_and_return:
  622. slapi_ch_free((void **)&client_addr);
  623. slapi_ch_free((void **)&host_entry);
  624. slapi_ch_free_string(&dnsName);
  625. return rc;
  626. }
  627. static int
  628. rootdn_check_host_wildcard(char *host, char *client_host)
  629. {
  630. int host_len = strlen(host);
  631. int client_len = strlen(client_host);
  632. int i, j;
  633. /*
  634. * Start at the end of the string and move backwards, and skip the first char "*"
  635. */
  636. if(client_len < host_len){
  637. /* this can't be a match */
  638. return -1;
  639. }
  640. for(i = host_len - 1, j = client_len - 1; i > 0; i--, j--){
  641. if(host[i] != client_host[j]){
  642. return -1;
  643. }
  644. }
  645. return 0;
  646. }
  647. static int
  648. rootdn_check_ip_wildcard(char *ip, char *client_ip)
  649. {
  650. size_t ip_len = strlen(ip);
  651. int i;
  652. /*
  653. * Start at the beginning of the string and move forward, and skip the last char "*"
  654. */
  655. if(strlen(client_ip) < ip_len){
  656. /* this can't be a match */
  657. return -1;
  658. }
  659. for(i = 0; i < ip_len - 1; i++){
  660. if(ip[i] != client_ip[i]){
  661. return -1;
  662. }
  663. }
  664. return 0;
  665. }
  666. char *
  667. strToLower(char *str){
  668. size_t i;
  669. for(i = 0; str && i < strlen(str); i++){
  670. str[i] = tolower(str[i]);
  671. }
  672. return str;
  673. }