rootdn_access.c 25 KB

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