rootdn_access.c 25 KB

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