rootdn_access.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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, *closeTime;
  194. char hour[3], min[3];
  195. int result = 0;
  196. int i;
  197. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "--> rootdn_load_config\n");
  198. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &e) == 0) && e){
  199. /*
  200. * Grab our plugin settings
  201. */
  202. openTime = slapi_entry_attr_get_charptr(e, "rootdn-open-time");
  203. closeTime = slapi_entry_attr_get_charptr(e, "rootdn-close-time");
  204. daysAllowed = slapi_entry_attr_get_charptr(e, "rootdn-days-allowed");
  205. hosts = slapi_entry_attr_get_charray(e, "rootdn-allow-host");
  206. hosts_to_deny = slapi_entry_attr_get_charray(e, "rootdn-deny-host");
  207. ips = slapi_entry_attr_get_charray(e, "rootdn-allow-ip");
  208. ips_to_deny = slapi_entry_attr_get_charray(e, "rootdn-deny-ip");
  209. /*
  210. * Validate out settings
  211. */
  212. if(daysAllowed){
  213. if(strcspn(daysAllowed, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,")){
  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",closeTime);
  216. slapi_ch_free_string(&daysAllowed);
  217. return -1;
  218. }
  219. daysAllowed = strToLower(daysAllowed);
  220. }
  221. if(openTime){
  222. if (strcspn(openTime, "0123456789")){
  223. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  224. "invalid rootdn-open-time value (%s), must be all digits\n",openTime);
  225. slapi_ch_free_string(&openTime);
  226. return -1;
  227. }
  228. if(strlen(openTime) != 4){
  229. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  230. "invalid format for rootdn-open-time value (%s). Should be HHMM\n", openTime);
  231. slapi_ch_free_string(&openTime);
  232. return -1;
  233. }
  234. /*
  235. * convert the time to all seconds
  236. */
  237. strncpy(hour, openTime,2);
  238. strncpy(min, openTime+2,2);
  239. open_time = (atoi(hour) * 3600) + (atoi(min) * 60);
  240. }
  241. if(closeTime){
  242. if (strcspn(closeTime, "0123456789")){
  243. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  244. "invalid rootdn-open-time value (%s), must be all digits, and should be HHMM\n",closeTime);
  245. slapi_ch_free_string(&closeTime);
  246. return -1;
  247. }
  248. if(strlen(closeTime) != 4){
  249. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  250. "invalid format for rootdn-open-time value (%s), should be HHMM\n", closeTime);
  251. slapi_ch_free_string(&closeTime);
  252. return -1;
  253. }
  254. /*
  255. * convert the time to all seconds
  256. */
  257. strncpy(hour, closeTime,2);
  258. strncpy(min, closeTime+2,2);
  259. close_time = (atoi(hour) * 3600) + (atoi(min) * 60);
  260. }
  261. if((openTime && closeTime == NULL) || (openTime == NULL && closeTime)){
  262. /* If you are using TOD access control, you must have a open and close time */
  263. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  264. "there must be a open and a close time\n");
  265. slapi_ch_free_string(&closeTime);
  266. slapi_ch_free_string(&openTime);
  267. return -1;
  268. }
  269. if(close_time && open_time && close_time <= open_time){
  270. /* Make sure the closing time is greater than the open time */
  271. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  272. "the close time must be greater than the open time\n");
  273. result = -1;
  274. }
  275. if(hosts){
  276. for(i = 0; hosts[i] != NULL; i++){
  277. if(strcspn(hosts[i], "0123456789.*-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")){
  278. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  279. "hostname (%s) contians invalid characters, skipping\n",hosts[i]);
  280. slapi_ch_free_string(&hosts[i]);
  281. hosts[i] = slapi_ch_strdup("!invalid");
  282. continue;
  283. }
  284. }
  285. }
  286. if(hosts_to_deny){
  287. for(i = 0; hosts_to_deny[i] != NULL; i++){
  288. if(strcspn(hosts_to_deny[i], "0123456789.*-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")){
  289. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  290. "hostname (%s) contians invalid characters, skipping\n",hosts_to_deny[i]);
  291. slapi_ch_free_string(&hosts_to_deny[i]);
  292. hosts_to_deny[i] = slapi_ch_strdup("!invalid");
  293. continue;
  294. }
  295. }
  296. }
  297. if(ips){
  298. for(i = 0; ips[i] != NULL; i++){
  299. if(strcspn(ips[i], "0123456789:ABCDEFabcdef.*")){
  300. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  301. "IP address contains invalid characters (%s), skipping\n", ips[i]);
  302. slapi_ch_free_string(&ips[i]);
  303. ips[i] = slapi_ch_strdup("!invalid");
  304. continue;
  305. }
  306. if(strstr(ips[i],":") == 0){
  307. /*
  308. * IPv4 - make sure it's just numbers, dots, and wildcard
  309. */
  310. if(strcspn(ips[i], "0123456789.*")){
  311. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  312. "IPv4 address contains invalid characters (%s), skipping\n", ips[i]);
  313. slapi_ch_free_string(&ips[i]);
  314. ips[i] = slapi_ch_strdup("!invalid");
  315. continue;
  316. }
  317. }
  318. }
  319. }
  320. if(ips_to_deny){
  321. for(i = 0; ips_to_deny[i] != NULL; i++){
  322. if(strcspn(ips_to_deny[i], "0123456789:ABCDEFabcdef.*")){
  323. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  324. "IP address contains invalid characters (%s), skipping\n", ips_to_deny[i]);
  325. slapi_ch_free_string(&ips_to_deny[i]);
  326. ips_to_deny[i] = slapi_ch_strdup("!invalid");
  327. continue;
  328. }
  329. if(strstr(ips_to_deny[i],":") == 0){
  330. /*
  331. * IPv4 - make sure it's just numbers, dots, and wildcard
  332. */
  333. if(strcspn(ips_to_deny[i], "0123456789.*")){
  334. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  335. "IPv4 address contains invalid characters (%s), skipping\n", ips_to_deny[i]);
  336. slapi_ch_free_string(&ips_to_deny[i]);
  337. ips_to_deny[i] = slapi_ch_strdup("!invalid");
  338. continue;
  339. }
  340. }
  341. }
  342. }
  343. slapi_ch_free_string(&openTime);
  344. slapi_ch_free_string(&closeTime);
  345. } else {
  346. /* failed to get the plugin entry */
  347. result = -1;
  348. }
  349. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "<-- rootdn_load_config (%d)\n", result);
  350. return result;
  351. }
  352. static int
  353. rootdn_check_access(Slapi_PBlock *pb){
  354. PRNetAddr *client_addr = NULL;
  355. PRHostEnt *host_entry = NULL;
  356. time_t curr_time;
  357. struct tm *timeinfo;
  358. char *dnsName = NULL;
  359. int isRoot = 0;
  360. int rc = 0;
  361. int i;
  362. /*
  363. * Verify this is a root DN
  364. */
  365. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  366. if(!isRoot){
  367. return 0;
  368. }
  369. /*
  370. * grab the current time/info if we need it
  371. */
  372. if(open_time || daysAllowed){
  373. time(&curr_time);
  374. timeinfo = localtime(&curr_time);
  375. }
  376. /*
  377. * First check TOD restrictions, continue through if we are in the open "window"
  378. */
  379. if(open_time){
  380. int curr_total;
  381. curr_total = (timeinfo->tm_hour * 3600) + (timeinfo->tm_min * 60);
  382. if((curr_total < open_time) || (curr_total >= close_time)){
  383. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not in the "
  384. "allowed time window\n");
  385. return -1;
  386. }
  387. }
  388. /*
  389. * Check if today is an allowed day
  390. */
  391. if(daysAllowed){
  392. char *timestr;
  393. char day[4];
  394. char *today = day;
  395. timestr = asctime(timeinfo); // DDD MMM dd hh:mm:ss YYYY
  396. memmove(day, timestr, 3); // we only want the day
  397. today = strToLower(today);
  398. if(!strstr(today, daysAllowed)){
  399. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not allowed for today\n");
  400. return -1;
  401. }
  402. }
  403. /*
  404. * Check the host restrictions, deny always overrides allow
  405. */
  406. if(hosts || hosts_to_deny){
  407. char buf[PR_NETDB_BUF_SIZE];
  408. char *host;
  409. /*
  410. * Get the client address
  411. */
  412. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  413. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  414. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for hosts.\n" );
  415. rc = -1;
  416. goto free_and_return;
  417. }
  418. /*
  419. * Get the hostname from the client address
  420. */
  421. host_entry = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
  422. if ( PR_GetHostByAddr(client_addr, buf, sizeof(buf), host_entry ) == PR_SUCCESS ) {
  423. if ( host_entry->h_name != NULL ) {
  424. /* copy the hostname */
  425. dnsName = slapi_ch_strdup( host_entry->h_name );
  426. } else {
  427. /* no hostname */
  428. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client address missing hostname\n");
  429. rc = -1;
  430. goto free_and_return;
  431. }
  432. } else {
  433. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client IP address could not be resolved\n");
  434. rc = -1;
  435. goto free_and_return;
  436. }
  437. /*
  438. * Now we have our hostname, now do our checks
  439. */
  440. if(hosts_to_deny){
  441. for(i = 0; hosts_to_deny[i] != NULL; i++){
  442. host = hosts_to_deny[i];
  443. /* check for wild cards */
  444. if(host[0] == '*'){
  445. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  446. /* match, return failure */
  447. rc = -1;
  448. goto free_and_return;
  449. }
  450. } else {
  451. if(strcasecmp(host,dnsName) == 0){
  452. /* we have a match, return failure */
  453. rc = -1;
  454. goto free_and_return;
  455. }
  456. }
  457. }
  458. rc = 0;
  459. }
  460. if(hosts){
  461. for(i = 0; hosts[i] != NULL; i++){
  462. host = hosts[i];
  463. /* check for wild cards */
  464. if(host[0] == '*'){
  465. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  466. /* match */
  467. rc = 0;
  468. goto free_and_return;
  469. }
  470. } else {
  471. if(strcasecmp(host,dnsName) == 0){
  472. /* we have a match, */
  473. rc = 0;
  474. goto free_and_return;
  475. }
  476. }
  477. }
  478. rc = -1;
  479. }
  480. }
  481. /*
  482. * Check the IP address restrictions, deny always overrides allow
  483. */
  484. if(ips || ips_to_deny){
  485. char ip_str[256];
  486. char *ip;
  487. int ip_len, i;
  488. if(client_addr == NULL){
  489. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  490. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  491. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for IP.\n" );
  492. rc = -1;
  493. goto free_and_return;
  494. }
  495. }
  496. /*
  497. * Check if we are IPv4, so we can grab the correct IP addr for "ip_str"
  498. */
  499. if ( PR_IsNetAddrType( client_addr, PR_IpAddrV4Mapped ) ) {
  500. PRNetAddr v4addr;
  501. memset( &v4addr, 0, sizeof( v4addr ) );
  502. v4addr.inet.family = PR_AF_INET;
  503. v4addr.inet.ip = client_addr->ipv6.ip.pr_s6_addr32[3];
  504. if( PR_NetAddrToString( &v4addr, ip_str, sizeof( ip_str )) != PR_SUCCESS){
  505. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv4 from client address.\n" );
  506. rc = -1;
  507. goto free_and_return;
  508. }
  509. } else {
  510. if( PR_NetAddrToString(client_addr, ip_str, sizeof(ip_str)) != PR_SUCCESS){
  511. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv6 from client address.\n" );
  512. rc = -1;
  513. goto free_and_return;
  514. }
  515. }
  516. /*
  517. * Now we have our IP address, do our checks
  518. */
  519. if(ips_to_deny){
  520. for(i = 0; ips_to_deny[i] != NULL; i++){
  521. ip = ips_to_deny[i];
  522. ip_len = strlen(ip);
  523. if(ip[ip_len - 1] == '*'){
  524. if(rootdn_check_ip_wildcard(ips_to_deny[i], ip_str) == 0){
  525. /* match, return failure */
  526. rc = -1;
  527. goto free_and_return;
  528. }
  529. } else {
  530. if(strcasecmp(ip_str, ip)==0){
  531. /* match, return failure */
  532. rc = -1;
  533. goto free_and_return;
  534. }
  535. }
  536. }
  537. rc = 0;
  538. }
  539. if(ips){
  540. for(i = 0; ips[i] != NULL; i++){
  541. ip = ips[i];
  542. ip_len = strlen(ip);
  543. if(ip[ip_len - 1] == '*'){
  544. if(rootdn_check_ip_wildcard(ip, ip_str) == 0){
  545. /* match, return success */
  546. rc = 0;
  547. goto free_and_return;
  548. }
  549. } else {
  550. if(strcasecmp(ip_str, ip)==0){
  551. /* match, return success */
  552. rc = 0;
  553. goto free_and_return;
  554. }
  555. }
  556. }
  557. rc = -1;
  558. }
  559. }
  560. free_and_return:
  561. slapi_ch_free((void **)&client_addr);
  562. slapi_ch_free((void **)&host_entry);
  563. slapi_ch_free_string(&dnsName);
  564. return rc;
  565. }
  566. static int
  567. rootdn_check_host_wildcard(char *host, char *client_host)
  568. {
  569. int host_len = strlen(host);
  570. int client_len = strlen(client_host);
  571. int i, j;
  572. /*
  573. * Start at the end of the string and move backwards, and skip the first char "*"
  574. */
  575. if(client_len < host_len){
  576. /* this can't be a match */
  577. return -1;
  578. }
  579. for(i = host_len - 1, j = client_len - 1; i > 0; i--, j--){
  580. if(host[i] != client_host[j]){
  581. return -1;
  582. }
  583. }
  584. return 0;
  585. }
  586. static int
  587. rootdn_check_ip_wildcard(char *ip, char *client_ip)
  588. {
  589. int ip_len = strlen(ip);
  590. int i;
  591. /*
  592. * Start at the beginning of the string and move forward, and skip the last char "*"
  593. */
  594. if(strlen(client_ip) < ip_len){
  595. /* this can't be a match */
  596. return -1;
  597. }
  598. for(i = 0; i < ip_len - 1; i++){
  599. if(ip[i] != client_ip[i]){
  600. return -1;
  601. }
  602. }
  603. return 0;
  604. }
  605. char *
  606. strToLower(char *str){
  607. int i;
  608. for(i = 0; i < strlen(str); i++){
  609. str[i] = tolower(str[i]);
  610. }
  611. return str;
  612. }