Skip to content

Commit

Permalink
1.1.0 correção de defeitos e melhorias
Browse files Browse the repository at this point in the history
Alguns defeitos que causavam deadlock nos processos foram encontrados e
corrigidos. Com esta correção, a base de dados nova não será compatível
com versões anteriores, apesar da base da versão anterior ser compatível
com a nova versão.

O serviço de denúncia por HTTP foi impementado. Para usar esta
funcionalidade é necessário determinar uma porta para a variável
"http_port" do arquivo de configuração "spfbl.conf" e também determinar
um hostname válido e roteável no mesmo arquivo. Quando o serviço SPFBL
for iniciado desta forma, os tickes serão enviados com prefixo HTTP do
hostname de origem do ticket, facilitando assim o desenvolvimento de
ferramentas de denúncia por parte do destinatário e também a criação de
alternadores de serviço SPFBL:

Received-SPFBL: http://<hostname>[:<port>]/spam/<ticket>

O LOG será gerado com o nome do processo a partir desta versão e também
com os milisegundos do inicio do processo:

yyyy-MM-dd'T'HH:mm:ss.SSSSZ <latencia> <processo> <tag> ...

Isso facilitará a investigação de novos defeitos relacionados ao
deadlock.

O protocolo P2P vai passar a rejeitar domínios reservados. Além disso o
bloqueio, manual ou automático, e a inclusão do IGNORE vai fazer com que
a lista de retenção dos peers cadastrados sejam limpas com os mesmos
identificadores adicionados nestas duas listas.
  • Loading branch information
leonamp committed Nov 18, 2015
1 parent 3ace1b1 commit b022b3c
Show file tree
Hide file tree
Showing 21 changed files with 1,682 additions and 986 deletions.
Binary file modified dist/SPFBL.jar
Binary file not shown.
5 changes: 2 additions & 3 deletions src/net/spfbl/core/AdministrationTCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.sql.Connection;

/**
* Servidor de commandos em TCP.
Expand All @@ -47,7 +46,7 @@ public final class AdministrationTCP extends Server {
* @throws java.io.IOException se houver falha durante o bind.
*/
public AdministrationTCP(int port) throws IOException {
super("ServerADMIN");
super("SERVERADM");
PORT = port;
setPriority(Thread.MIN_PRIORITY);
// Criando conexões.
Expand Down Expand Up @@ -134,7 +133,7 @@ private boolean isTimeout() {
return false;
} else {
int interval = (int) (System.currentTimeMillis() - time) / 1000;
return interval > 10;
return interval > 600;
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/net/spfbl/core/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ public synchronized static TreeSet<Client> getSet() {
return clientSet;
}

public static TreeSet<Client> dropAll() throws ProcessException {
TreeSet<Client> clientSet = new TreeSet<Client>();
for (Client client : getSet()) {
if (client != null) {
String cidr = client.getCIDR();
client = drop(cidr);
if (client != null) {
clientSet.add(client);
}
}
}
return clientSet;
}

public static Client drop(String cidr) throws ProcessException {
if (cidr == null || !Subnet.isValidCIDR(cidr)) {
throw new ProcessException("ERROR: INVALID CIDR");
Expand Down Expand Up @@ -381,9 +395,9 @@ public String getFrequencyLiteral() {
if (hasFrequency()) {
int frequencyInt = frequency.getMaximumInt();
int idleTimeInt = getIdleTimeMillis();
if (idleTimeInt > Server.DAY_TIME) {
if (idleTimeInt > frequencyInt * 5) {
return "DEAD";
} else if (idleTimeInt > frequencyInt * 2) {
} else if (idleTimeInt > frequencyInt * 3) {
return "IDLE";
} else if (frequencyInt < limit) {
return "<" + limit + "ms";
Expand Down
143 changes: 130 additions & 13 deletions src/net/spfbl/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import net.spfbl.spf.PeerUDP;
import net.spfbl.whois.QueryTCP;
import net.spfbl.spf.QuerySPF;
import java.util.Properties;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import net.spfbl.dnsbl.QueryDNSBL;
import net.spfbl.http.ComplainHTTP;
import net.spfbl.spf.SPF;
import net.spfbl.whois.Domain;
import net.spfbl.whois.SubnetIPv4;
import net.spfbl.whois.SubnetIPv6;

/**
* Classe principal de inicilização do serviço.
Expand All @@ -38,8 +46,8 @@
public class Core {

private static final byte VERSION = 1;
private static final byte SUBVERSION = 0;
private static final byte RELEASE = 4;
private static final byte SUBVERSION = 1;
private static final byte RELEASE = 0;

public static String getAplication() {
return "SPFBL-" + getVersion();
Expand All @@ -62,7 +70,21 @@ public static void sendTokenToPeer(
}

public static String getPeerConnection() {
return peerUDP.getConnection();
if (peerUDP == null) {
return null;
} else {
return peerUDP.getConnection();
}
}

private static ComplainHTTP complainHTTP = null;

public static String getSpamURL() {
if (complainHTTP == null) {
return null;
} else {
return complainHTTP.getSpamURL();
}
}

private static AdministrationTCP administrationTCP = null;
Expand Down Expand Up @@ -91,6 +113,8 @@ private static void startConfiguration() {
Core.setPortWHOIS(properties.getProperty("whois_port"));
Core.setPortSPFBL(properties.getProperty("spfbl_port"));
Core.setPortDNSBL(properties.getProperty("dnsbl_port"));
Core.setPortHTTP(properties.getProperty("http_port"));
Core.setMaxUDP(properties.getProperty("udp_max"));
QueryDNSBL.setConnectionLimit(properties.getProperty("dnsbl_limit"));
QuerySPF.setConnectionLimit(properties.getProperty("spfbl_limit"));
} finally {
Expand All @@ -106,20 +130,74 @@ public static String getAdminEmail() {
return ADMIN_EMAIL;
}

private static String HOSTNAME = "localhost";
private static String HOSTNAME = null;
private static String ADMIN_EMAIL = null;
private static short PORT_ADMIN = 9875;
private static short PORT_WHOIS = 0;
private static short PORT_SPFBL = 9877;
private static short PORT_DNSBL = 0;
private static short PORT_HTTP = 0;
private static short UDP_MAX = 512; // UDP max size packet.

private static boolean isRouteable(String hostame) {
try {
Attributes attributesA = Server.getAttributesDNS(
hostame, new String[]{"A"});
Attribute attributeA = attributesA.get("A");
if (attributeA == null) {
Attributes attributesAAAA = Server.getAttributesDNS(
hostame, new String[]{"AAAA"});
Attribute attributeAAAA = attributesAAAA.get("AAAA");
if (attributeAAAA != null) {
for (int i = 0; i < attributeAAAA.size(); i++) {
String host6Address = (String) attributeAAAA.get(i);
if (SubnetIPv6.isValidIPv6(host6Address)) {
try {
InetAddress address = InetAddress.getByName(host6Address);
if (address.isLinkLocalAddress()) {
return false;
} else if (address.isLoopbackAddress()) {
return false;
}
} catch (UnknownHostException ex) {
}
} else {
return false;
}
}
}
} else {
for (int i = 0; i < attributeA.size(); i++) {
String host4Address = (String) attributeA.get(i);
if (SubnetIPv4.isValidIPv4(host4Address)) {
try {
InetAddress address = InetAddress.getByName(host4Address);
if (address.isLinkLocalAddress()) {
return false;
} else if (address.isLoopbackAddress()) {
return false;
}
} catch (UnknownHostException ex) {
}
} else {
return false;
}
}
}
return true;
} catch (NamingException ex) {
return false;
}
}

public static synchronized void setHostname(String hostame) {
if (hostame != null && hostame.length() > 0) {
if (Domain.isHostname(hostame)) {
Core.HOSTNAME = Domain.extractHost(hostame, false);
} else {
if (!Domain.isHostname(hostame)) {
Server.logError("invalid hostame '" + hostame + "'.");
} else if (!isRouteable(hostame)) {
Server.logError("unrouteable hostname '" + hostame + "'.");
} else {
Core.HOSTNAME = Domain.extractHost(hostame, false);
}
}
}
Expand All @@ -134,7 +212,7 @@ public static synchronized void setAdminEmail(String email) {
}
}

public static synchronized void setPortAdmin(String port) {
public static void setPortAdmin(String port) {
if (port != null && port.length() > 0) {
try {
setPortAdmin(Integer.parseInt(port));
Expand All @@ -152,7 +230,7 @@ public static synchronized void setPortAdmin(int port) {
}
}

public static synchronized void setPortWHOIS(String port) {
public static void setPortWHOIS(String port) {
if (port != null && port.length() > 0) {
try {
setPortWHOIS(Integer.parseInt(port));
Expand All @@ -170,7 +248,7 @@ public static synchronized void setPortWHOIS(int port) {
}
}

public static synchronized void setPortSPFBL(String port) {
public static void setPortSPFBL(String port) {
if (port != null && port.length() > 0) {
try {
setPortSPFBL(Integer.parseInt(port));
Expand All @@ -188,7 +266,7 @@ public static synchronized void setPortSPFBL(int port) {
}
}

public static synchronized void setPortDNSBL(String port) {
public static void setPortDNSBL(String port) {
if (port != null && port.length() > 0) {
try {
setPortDNSBL(Integer.parseInt(port));
Expand All @@ -206,6 +284,42 @@ public static synchronized void setPortDNSBL(int port) {
}
}

public static void setPortHTTP(String port) {
if (port != null && port.length() > 0) {
try {
setPortHTTP(Integer.parseInt(port));
} catch (Exception ex) {
Server.logError("invalid HTTP port '" + port + "'.");
}
}
}

public static synchronized void setPortHTTP(int port) {
if (port < 1 || port > Short.MAX_VALUE) {
Server.logError("invalid HTTP port '" + port + "'.");
} else {
Core.PORT_HTTP = (short) port;
}
}

public static void setMaxUDP(String max) {
if (max != null && max.length() > 0) {
try {
setMaxUDP(Integer.parseInt(max));
} catch (Exception ex) {
Server.logError("invalid UDP max size '" + max + "'.");
}
}
}

public static synchronized void setMaxUDP(int max) {
if (max < 128 || max > Short.MAX_VALUE) {
Server.logError("invalid UDP max size '" + max + "'.");
} else {
Core.UDP_MAX = (short) max;
}
}

private static class ApplicationMessageHandler implements MessageHandler {
@Override
public synchronized String handle(String message) {
Expand All @@ -220,6 +334,7 @@ public synchronized String handle(String message) {
* @param args the command line arguments
*/
public static void main(String[] args) {
Thread.currentThread().setName("SYSTEMCOR");
try {
String appId = Server.class.getCanonicalName();
ApplicationMessageHandler messageHandler = new ApplicationMessageHandler();
Expand Down Expand Up @@ -250,9 +365,11 @@ public static void main(String[] args) {
if (PORT_DNSBL > 0) {
new QueryDNSBL(PORT_DNSBL).start();
}
if (!Peer.sendHeloToAll()) {
Server.logDebug("the hostname '" + HOSTNAME + "' has non global scope address.");
if (PORT_HTTP > 0 ) {
complainHTTP = new ComplainHTTP(HOSTNAME, PORT_HTTP);
complainHTTP.start();
}
Peer.sendHeloToAll();
SPF.startTimer();
}
} catch (Exception ex) {
Expand Down
28 changes: 21 additions & 7 deletions src/net/spfbl/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.HashMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.spfbl.spf.SPF;
import net.spfbl.spf.SPF.Distribution;
import net.spfbl.whois.Domain;
Expand Down Expand Up @@ -271,7 +269,9 @@ public static TreeSet<String> rejectAll(String token) {

public String release(String token) {
if (dropExact(token)) {
if (SPF.addBlockExact(token)) {
if (SPF.isIgnore(token)) {
return "IGNORED";
} else if (SPF.addBlockExact(token)) {
if (isReceiveRepass()) {
sendToOthers(token);
return "REPASSED";
Expand Down Expand Up @@ -417,6 +417,16 @@ public TreeSet<Peer> getRepassSet() {
return peerSet;
}

public static TreeSet<Peer> dropAll() {
TreeSet<Peer> peerSet = new TreeSet<Peer>();
for (Peer peer : getSet()) {
if (peer.drop()) {
peerSet.add(peer);
}
}
return peerSet;
}

public synchronized static Peer drop(String address) {
Peer peer = MAP.remove(address);
if (peer != null) {
Expand Down Expand Up @@ -619,8 +629,8 @@ public String processReceive(String token) {
try {
if (!isValid(token)) {
return "INVALID";
} else if (Domain.isTLD(token)) {
return "TLD";
} else if (Domain.isReserved(token)) {
return "RESERVED";
} else if (SPF.isIgnore(token)) {
return "IGNORED";
} else if (isReceiveReject()) {
Expand Down Expand Up @@ -724,6 +734,10 @@ public static synchronized void load() {
}
}

public boolean hasEmail() {
return email != null;
}

public boolean hasFrequency() {
return frequency != null;
}
Expand All @@ -740,9 +754,9 @@ public String getFrequencyLiteral() {
if (hasFrequency()) {
int frequencyInt = frequency.getMaximumInt();
int idleTimeInt = getIdleTimeMillis();
if (idleTimeInt > Server.DAY_TIME) {
if (idleTimeInt > frequencyInt * 5) {
return "DEAD";
} else if (idleTimeInt > frequencyInt * 2) {
} else if (idleTimeInt > frequencyInt * 3) {
return "IDLE";
} else if (frequencyInt < limit) {
return "<" + limit + "ms";
Expand Down
Loading

0 comments on commit b022b3c

Please sign in to comment.