rootdn_access.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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_to_deny);
  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 = NULL, *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. slapi_log_error(SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_load_config: "
  389. "Failed to get plugin entry\n");
  390. result = -1;
  391. }
  392. free_and_return:
  393. slapi_ch_free_string(&openTime);
  394. slapi_ch_free_string(&closeTime);
  395. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "<-- rootdn_load_config (%d)\n", result);
  396. return result;
  397. }
  398. static int
  399. rootdn_check_access(Slapi_PBlock *pb){
  400. PRNetAddr *client_addr = NULL;
  401. PRHostEnt *host_entry = NULL;
  402. time_t curr_time;
  403. struct tm *timeinfo;
  404. char *dnsName = NULL;
  405. int isRoot = 0;
  406. int rc = SLAPI_PLUGIN_SUCCESS;
  407. int i;
  408. /*
  409. * Verify this is a root DN
  410. */
  411. slapi_pblock_get ( pb, SLAPI_REQUESTOR_ISROOT, &isRoot );
  412. if(!isRoot){
  413. return SLAPI_PLUGIN_SUCCESS;
  414. }
  415. /*
  416. * grab the current time/info if we need it
  417. */
  418. if(open_time || daysAllowed){
  419. time(&curr_time);
  420. timeinfo = localtime(&curr_time);
  421. }
  422. /*
  423. * First check TOD restrictions, continue through if we are in the open "window"
  424. */
  425. if(open_time){
  426. int curr_total;
  427. curr_total = (timeinfo->tm_hour * 3600) + (timeinfo->tm_min * 60);
  428. if((curr_total < open_time) || (curr_total >= close_time)){
  429. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not in the "
  430. "allowed time window\n");
  431. return -1;
  432. }
  433. }
  434. /*
  435. * Check if today is an allowed day
  436. */
  437. if(daysAllowed){
  438. char *timestr;
  439. char day[4];
  440. char *today = day;
  441. timestr = asctime(timeinfo); // DDD MMM dd hh:mm:ss YYYY
  442. memmove(day, timestr, 3); // we only want the day
  443. today = strToLower(today);
  444. if(!strstr(daysAllowed, today)){
  445. slapi_log_error(SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: bind not allowed for today(%s), "
  446. "only allowed on days: %s\n", today, daysAllowed);
  447. return -1;
  448. }
  449. }
  450. /*
  451. * Check the host restrictions, deny always overrides allow
  452. */
  453. if(hosts || hosts_to_deny){
  454. char buf[PR_NETDB_BUF_SIZE];
  455. char *host;
  456. /*
  457. * Get the client address
  458. */
  459. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  460. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  461. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for hosts.\n" );
  462. rc = -1;
  463. goto free_and_return;
  464. }
  465. /*
  466. * Get the hostname from the client address
  467. */
  468. host_entry = (PRHostEnt *)slapi_ch_malloc( sizeof(PRHostEnt) );
  469. if ( PR_GetHostByAddr(client_addr, buf, sizeof(buf), host_entry ) == PR_SUCCESS ) {
  470. if ( host_entry->h_name != NULL ) {
  471. /* copy the hostname */
  472. dnsName = slapi_ch_strdup( host_entry->h_name );
  473. } else {
  474. /* no hostname */
  475. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client address missing hostname\n");
  476. rc = -1;
  477. goto free_and_return;
  478. }
  479. } else {
  480. slapi_log_error( SLAPI_LOG_PLUGIN, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: client IP address could not be resolved\n");
  481. rc = -1;
  482. goto free_and_return;
  483. }
  484. /*
  485. * Now we have our hostname, now do our checks
  486. */
  487. if(hosts_to_deny){
  488. for(i = 0; hosts_to_deny[i] != NULL; i++){
  489. host = hosts_to_deny[i];
  490. /* check for wild cards */
  491. if(host[0] == '*'){
  492. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  493. /* match, return failure */
  494. rc = -1;
  495. goto free_and_return;
  496. }
  497. } else {
  498. if(strcasecmp(host,dnsName) == 0){
  499. /* we have a match, return failure */
  500. rc = -1;
  501. goto free_and_return;
  502. }
  503. }
  504. }
  505. rc = 0;
  506. }
  507. if(hosts){
  508. for(i = 0; hosts[i] != NULL; i++){
  509. host = hosts[i];
  510. /* check for wild cards */
  511. if(host[0] == '*'){
  512. if(rootdn_check_host_wildcard(host, dnsName) == 0){
  513. /* match */
  514. rc = 0;
  515. goto free_and_return;
  516. }
  517. } else {
  518. if(strcasecmp(host,dnsName) == 0){
  519. /* we have a match, */
  520. rc = 0;
  521. goto free_and_return;
  522. }
  523. }
  524. }
  525. rc = -1;
  526. }
  527. }
  528. /*
  529. * Check the IP address restrictions, deny always overrides allow
  530. */
  531. if(ips || ips_to_deny){
  532. char ip_str[256];
  533. char *ip;
  534. int ip_len, i;
  535. if(client_addr == NULL){
  536. client_addr = (PRNetAddr *)slapi_ch_malloc(sizeof(PRNetAddr));
  537. if ( slapi_pblock_get( pb, SLAPI_CONN_CLIENTNETADDR, client_addr ) != 0 ) {
  538. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get client address for IP.\n" );
  539. rc = -1;
  540. goto free_and_return;
  541. }
  542. }
  543. /*
  544. * Check if we are IPv4, so we can grab the correct IP addr for "ip_str"
  545. */
  546. if ( PR_IsNetAddrType( client_addr, PR_IpAddrV4Mapped ) ) {
  547. PRNetAddr v4addr;
  548. memset( &v4addr, 0, sizeof( v4addr ) );
  549. v4addr.inet.family = PR_AF_INET;
  550. v4addr.inet.ip = client_addr->ipv6.ip.pr_s6_addr32[3];
  551. if( PR_NetAddrToString( &v4addr, ip_str, sizeof( ip_str )) != PR_SUCCESS){
  552. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv4 from client address.\n" );
  553. rc = -1;
  554. goto free_and_return;
  555. }
  556. } else {
  557. if( PR_NetAddrToString(client_addr, ip_str, sizeof(ip_str)) != PR_SUCCESS){
  558. slapi_log_error( SLAPI_LOG_FATAL, ROOTDN_PLUGIN_SUBSYSTEM, "rootdn_check_access: Could not get IPv6 from client address.\n" );
  559. rc = -1;
  560. goto free_and_return;
  561. }
  562. }
  563. /*
  564. * Now we have our IP address, do our checks
  565. */
  566. if(ips_to_deny){
  567. for(i = 0; ips_to_deny[i] != NULL; i++){
  568. ip = ips_to_deny[i];
  569. ip_len = strlen(ip);
  570. if(ip[ip_len - 1] == '*'){
  571. if(rootdn_check_ip_wildcard(ips_to_deny[i], ip_str) == 0){
  572. /* match, return failure */
  573. rc = -1;
  574. goto free_and_return;
  575. }
  576. } else {
  577. if(strcasecmp(ip_str, ip)==0){
  578. /* match, return failure */
  579. rc = -1;
  580. goto free_and_return;
  581. }
  582. }
  583. }
  584. rc = 0;
  585. }
  586. if(ips){
  587. for(i = 0; ips[i] != NULL; i++){
  588. ip = ips[i];
  589. ip_len = strlen(ip);
  590. if(ip[ip_len - 1] == '*'){
  591. if(rootdn_check_ip_wildcard(ip, ip_str) == 0){
  592. /* match, return success */
  593. rc = 0;
  594. goto free_and_return;
  595. }
  596. } else {
  597. if(strcasecmp(ip_str, ip)==0){
  598. /* match, return success */
  599. rc = 0;
  600. goto free_and_return;
  601. }
  602. }
  603. }
  604. rc = -1;
  605. }
  606. }
  607. free_and_return:
  608. slapi_ch_free((void **)&client_addr);
  609. slapi_ch_free((void **)&host_entry);
  610. slapi_ch_free_string(&dnsName);
  611. return rc;
  612. }
  613. static int
  614. rootdn_check_host_wildcard(char *host, char *client_host)
  615. {
  616. int host_len = strlen(host);
  617. int client_len = strlen(client_host);
  618. int i, j;
  619. /*
  620. * Start at the end of the string and move backwards, and skip the first char "*"
  621. */
  622. if(client_len < host_len){
  623. /* this can't be a match */
  624. return -1;
  625. }
  626. for(i = host_len - 1, j = client_len - 1; i > 0; i--, j--){
  627. if(host[i] != client_host[j]){
  628. return -1;
  629. }
  630. }
  631. return 0;
  632. }
  633. static int
  634. rootdn_check_ip_wildcard(char *ip, char *client_ip)
  635. {
  636. int ip_len = strlen(ip);
  637. int i;
  638. /*
  639. * Start at the beginning of the string and move forward, and skip the last char "*"
  640. */
  641. if(strlen(client_ip) < ip_len){
  642. /* this can't be a match */
  643. return -1;
  644. }
  645. for(i = 0; i < ip_len - 1; i++){
  646. if(ip[i] != client_ip[i]){
  647. return -1;
  648. }
  649. }
  650. return 0;
  651. }
  652. char *
  653. strToLower(char *str){
  654. int i;
  655. for(i = 0; str && i < strlen(str); i++){
  656. str[i] = tolower(str[i]);
  657. }
  658. return str;
  659. }