rootdn_access.c 23 KB

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