rootdn_access.c 25 KB

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