rootdn_access.c 25 KB

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