TCPWrapper は TCPServer を差し替えて使うようになっているのです
が、単に hosts_ctl を呼ぶだけのメソッド(TCPWrapper::hosts_ctl)
も欲しいので追加してみましたが、いかがでしょうか。
ついでに、hosts_allow_table と hosts_deny_table を設定する
TCPWrapper::hosts_allow= と TCPWrapper::hosts_deny= を追加して
います。
--
ごとうゆうぞう
diff -u -p -r1.2 tcpwrap.c
--- tcpwrap.c 24 Jan 2003 03:04:40 -0000 1.2
+++ tcpwrap.c 25 Jan 2003 11:26:47 -0000
@@ -35,6 +35,14 @@
#ifdef HAVE_IDENT_H
#include <ident.h>
#endif
+#if defined(HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+#if defined(HAVE_SYS_PARAM_H)
+# include <sys/param.h>
+#else
+# define MAXPATHLEN 1024
+#endif
#define DEFAULT_RFC1413_QUERY_TIMEOUT 30
@@ -44,6 +52,7 @@ int deny_severity = 0;
static VALUE cTCPServer;
static VALUE eSocket;
static VALUE cTCPWrapper;
+static VALUE eTCPWrapError;
typedef struct tcp_wrapper {
VALUE daemon;
@@ -143,12 +152,77 @@ static VALUE tcpd_accept(VALUE self)
return sock;
}
+static VALUE
+tcpd_s_set_hosts_allow(VALUE self, VALUE s)
+{
+ static char hosts_allow[MAXPATHLEN];
+
+ Check_SafeStr(s);
+ snprintf(hosts_allow, sizeof(hosts_allow), "%s", RSTRING(s)->ptr);
+ if(access(hosts_allow, R_OK) < 0)
+ rb_warning("cannot read %s", hosts_allow);
+ hosts_allow_table = hosts_allow;
+
+ return s;
+}
+
+static VALUE
+tcpd_s_get_hosts_allow(VALUE self)
+{
+ return rb_str_new2(hosts_allow_table);
+}
+
+static VALUE
+tcpd_s_set_hosts_deny(VALUE self, VALUE s)
+{
+ static char hosts_deny[MAXPATHLEN];
+
+ Check_SafeStr(s);
+ snprintf(hosts_deny, sizeof(hosts_deny), "%s", RSTRING(s)->ptr);
+ if(access(hosts_deny, R_OK) < 0)
+ rb_warning("cannot read %s", hosts_deny);
+ hosts_deny_table = hosts_deny;
+
+ return s;
+}
+
+static VALUE
+tcpd_s_get_hosts_deny(VALUE self)
+{
+ return rb_str_new2(hosts_deny_table);
+}
+
+static char*
+str_to_ctlstr(VALUE s)
+{
+ if(NIL_P(s)) return STRING_UNKNOWN;
+ Check_Type(s, T_STRING);
+ return RSTRING(s)->ptr;
+}
+
+static VALUE
+tcpd_s_hosts_ctl(VALUE self, VALUE daemon, VALUE name, VALUE addr, VALUE user)
+{
+ if (!hosts_ctl(str_to_ctlstr(daemon), str_to_ctlstr(name),
+ str_to_ctlstr(addr), str_to_ctlstr(user))){
+ rb_raise(eTCPWrapError, "access denied.");
+ }
+
+ return Qnil;
+}
+
void Init_tcpwrap()
{
rb_require("socket");
cTCPServer = rb_const_get(rb_cObject, rb_intern("TCPServer"));
eSocket = rb_const_get(rb_cObject, rb_intern("SocketError"));
cTCPWrapper = rb_define_class("TCPWrapper", rb_cObject);
+ eTCPWrapError = rb_define_class("TCPWrapError", rb_eStandardError);
rb_define_singleton_method(cTCPWrapper, "new", tcpd_s_new, -1);
rb_define_method(cTCPWrapper, "accept", tcpd_accept, 0);
+ rb_define_singleton_method(cTCPWrapper, "hosts_allow=", tcpd_s_set_hosts_allow, 1);
+ rb_define_singleton_method(cTCPWrapper, "hosts_allow", tcpd_s_get_hosts_allow, 0);
+ rb_define_singleton_method(cTCPWrapper, "hosts_deny=", tcpd_s_set_hosts_deny, 1);
+ rb_define_singleton_method(cTCPWrapper, "hosts_deny", tcpd_s_get_hosts_deny, 0);
+ rb_define_singleton_method(cTCPWrapper, "hosts_ctl", tcpd_s_hosts_ctl, 4);
}