Author: dbkr
Date: 2006-08-03 19:42:01 +0000 (Thu, 03 Aug 2006)
New Revision: 9872
Modified:
trunk/apps/Freemail/src/freemail/AccountManager.java
trunk/apps/Freemail/src/freemail/Freemail.java
trunk/apps/Freemail/src/freemail/MailSite.java
trunk/apps/Freemail/src/freemail/OutboundContact.java
trunk/apps/Freemail/src/freemail/SingleAccountWatcher.java
trunk/apps/Freemail/src/freemail/fcp/HighLevelFCPClient.java
trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
Log:
Make short addresses (KSK mailsites) work
Modified: trunk/apps/Freemail/src/freemail/AccountManager.java
===================================================================
--- trunk/apps/Freemail/src/freemail/AccountManager.java 2006-08-03
19:37:24 UTC (rev 9871)
+++ trunk/apps/Freemail/src/freemail/AccountManager.java 2006-08-03
19:42:01 UTC (rev 9872)
@@ -118,6 +118,16 @@
return new
EmailAddress("anything@"+Base32.encode(mailsite.getKeyBody().getBytes())+".freemail");
}
+ public static EmailAddress getKSKFreemailAddress(File accdir) {
+ PropsFile accfile = getAccountFile(accdir);
+
+ String alias = accfile.get("domain_alias");
+
+ if (alias == null) return null;
+
+ return new EmailAddress("anything@"+alias+".freemail");
+ }
+
public static RSAKeyParameters getPrivateKey(File accdir) {
PropsFile props = getAccountFile(accdir);
@@ -194,6 +204,21 @@
System.out.println("Account creation completed.");
}
+ public static void addShortAddress(String username, String alias)
throws Exception {
+ File accountdir = new File(DATADIR, username);
+ if (!accountdir.exists()) {
+ throw new Exception("No such account - "+username+".");
+ }
+
+ PropsFile accfile = getAccountFile(accountdir);
+
+ MailSite ms = new MailSite(accfile);
+
+ if (ms.insertAlias(alias)) {
+ accfile.put("domain_alias", alias);
+ }
+ }
+
public static boolean authenticate(String username, String password) {
if (!validate_username(username)) return false;
Modified: trunk/apps/Freemail/src/freemail/Freemail.java
===================================================================
--- trunk/apps/Freemail/src/freemail/Freemail.java 2006-08-03 19:37:24 UTC
(rev 9871)
+++ trunk/apps/Freemail/src/freemail/Freemail.java 2006-08-03 19:42:01 UTC
(rev 9872)
@@ -32,6 +32,7 @@
String action = "";
String account = null;
String newpasswd = null;
+ String alias = null;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--newaccount")) {
@@ -52,6 +53,15 @@
}
account = args[i - 1];
newpasswd = args[i];
+ } else if (args[i].equals("--shortaddress")) {
+ action = args[i];
+ i = i + 2;
+ if (args.length - 1 < i) {
+ System.out.println("Usage:
--shortaddress <name>");
+ return;
+ }
+ account = args[i - 1];
+ alias = args[i];
} else if (args[i].equals("-h")) {
i++;
if (args.length - 1 < i) {
@@ -100,6 +110,16 @@
e.printStackTrace();
}
return;
+ } else if (action.equals("--shortaddress")) {
+ try {
+ AccountManager.addShortAddress(account, alias);
+ } catch (Exception e) {
+ System.out.println("Couldn't add short address
for "+account+". "+e.getMessage());
+ e.printStackTrace();
+ return;
+ }
+ System.out.println("Your short Freemail address is:
'anything"+alias+".freemail'. Your long address will continue to work.");
+ return;
}
File globaldatadir = new File(GLOBALDATADIR);
Modified: trunk/apps/Freemail/src/freemail/MailSite.java
===================================================================
--- trunk/apps/Freemail/src/freemail/MailSite.java 2006-08-03 19:37:24 UTC
(rev 9871)
+++ trunk/apps/Freemail/src/freemail/MailSite.java 2006-08-03 19:42:01 UTC
(rev 9872)
@@ -1,11 +1,13 @@
package freemail;
+import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import freemail.utils.PropsFile;
import freemail.fcp.HighLevelFCPClient;
import freemail.fcp.FCPInsertErrorMessage;
+import freemail.fcp.FCPBadFileException;
public class MailSite {
private final PropsFile accprops;
@@ -77,31 +79,47 @@
// leave this out for now, until we know whether we're doing it
// with real USK redirects or fake put-a-USK-in-a-KSK redirect
// are we set up to use a KSK domain alias too?
- /*String alias = this.accprops.get("domain_alias");
+ String alias = this.accprops.get("domain_alias");
if (alias != null) {
- String targetKey = this.accprops.get("mailsite.pubkey");
- if (targetKey == null) return -1;
- FreenetURI furi;
- try {
- furi = new FreenetURI(targetKey);
- } catch (MalformedURLException mfue) {
- return -1;
- }
- targetKey =
"USK@"+furi.getKeyBody()+"/"+AccountManager.MAILSITE_SUFFIX+"/-1/"+MAILPAGE;
+ if (!this.insertAlias(alias)) return -1;
+ }
+
+ return actualslot;
+ }
+
+ public boolean insertAlias(String alias) {
+ String targetKey = this.accprops.get("mailsite.pubkey");
+ if (targetKey == null) return false;
+ FreenetURI furi;
+ try {
+ furi = new FreenetURI(targetKey);
+ } catch (MalformedURLException mfue) {
+ return false;
+ }
+ targetKey =
"USK@"+furi.getKeyBody()+"/"+AccountManager.MAILSITE_SUFFIX+"/-1/"+MAILPAGE;
- System.out.println("Inserting mailsite redirect from
"+"KSK@"+alias+ALIAS_SUFFIX+" to "+targetKey);
+ ByteArrayInputStream bis = new
ByteArrayInputStream(targetKey.getBytes());
- FCPInsertErrorMessage err =
cli.putRedirect("KSK@"+alias+ALIAS_SUFFIX, targetKey);
+ System.out.println("Inserting mailsite redirect from
"+"KSK@"+alias+ALIAS_SUFFIX+" to "+targetKey);
+
+ HighLevelFCPClient cli = new HighLevelFCPClient();
- if (err == null) {
- System.out.println("Mailsite redirect inserted
successfully");
- } else if (err.errorcode ==
FCPInsertErrorMessage.COLLISION) {
- System.out.println("Mailsite alias collided -
somebody is already using that alias! Choose another one!");
- } else {
- System.out.println("Mailsite redirect insert
failed, but did not collide.");
- }
- }*/
-
- return actualslot;
+ FCPInsertErrorMessage err = null;
+ try {
+ err = cli.put(bis, "KSK@"+alias+ALIAS_SUFFIX);
+ } catch (FCPBadFileException bfe) {
+ // impossible
+ }
+
+ if (err == null) {
+ System.out.println("Mailsite redirect inserted
successfully");
+ return true;
+ } else if (err.errorcode == FCPInsertErrorMessage.COLLISION) {
+ System.out.println("Mailsite alias collided - somebody
is already using that alias! Choose another one!");
+ return false;
+ } else {
+ System.out.println("Mailsite redirect insert failed,
but did not collide.");
+ return false;
+ }
}
}
Modified: trunk/apps/Freemail/src/freemail/OutboundContact.java
===================================================================
--- trunk/apps/Freemail/src/freemail/OutboundContact.java 2006-08-03
19:37:24 UTC (rev 9871)
+++ trunk/apps/Freemail/src/freemail/OutboundContact.java 2006-08-03
19:42:01 UTC (rev 9872)
@@ -3,6 +3,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.BufferedReader;
+import java.io.FileReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
@@ -58,7 +60,7 @@
this.accdir = accdir;
- if (this.address.getMailsiteKey() == null) {
+ if (!this.address.is_freemail_address()) {
this.contactfile = null;
throw new BadFreemailAddressException();
} else {
@@ -70,7 +72,7 @@
if (!outbounddir.exists())
outbounddir.mkdir();
- File obctdir = new File(outbounddir,
this.address.getMailsiteKey());
+ File obctdir = new File(outbounddir,
this.address.getSubDomain());
if (!obctdir.exists())
obctdir.mkdir();
@@ -85,7 +87,7 @@
public OutboundContact(File accdir, File ctdir) {
this.accdir = accdir;
this.address = new EmailAddress();
- this.address.domain =
Base32.encode(ctdir.getName().getBytes())+".freemail";
+ this.address.domain = ctdir.getName()+".freemail";
this.contactfile = new PropsFile(new File(ctdir,
PROPSFILE_NAME));
@@ -127,7 +129,7 @@
}
} else {
- System.out.println("Sucessfully received CTS
for "+this.address.getMailsiteKey());
+ System.out.println("Sucessfully received CTS
for "+this.address.getSubDomain());
cts.delete();
this.contactfile.put("status", "cts-received");
// delete inital slot for forward secrecy
@@ -271,7 +273,7 @@
rtsmessage.append("messagetype=rts\r\n");
// must include who this RTS is to, otherwise we're vulnerable
to surruptitious forwarding
- rtsmessage.append("to="+this.address.getMailsiteKey()+"\r\n");
+ rtsmessage.append("to="+this.address.getSubDomain()+"\r\n");
// get our mailsite URI
String our_mailsite_uri =
AccountManager.getAccountFile(this.accdir).get("mailsite.pubkey");
@@ -378,6 +380,36 @@
return false;
}
+ if (!this.address.is_ssk_address()) {
+ // presumably a KSK 'redirect'. Follow it.
+ BufferedReader br = null;
+ try {
+ br = new BufferedReader(new
FileReader(mailsite_file));
+ } catch (FileNotFoundException fnfe) {
+ // impossible
+ }
+
+ if (mailsite_file.length() > 512) {
+ System.out.println("Fatal: mailsite redirect
too long. Ignoring.");
+ throw new
OutboundContactFatalException("Mailsite redirect too long.");
+ }
+
+ String addr;
+ try {
+ addr = br.readLine();
+ br.close();
+ } catch (IOException ioe) {
+ System.out.println("Warning: IO exception
whilst reading mailsite redirect file: "+ioe.getMessage());
+ return false;
+ }
+ mailsite_file.delete();
+ mailsite_file = cli.fetch(addr);
+ if (mailsite_file == null) {
+ System.out.println("Failed to retrieve
redirected mailsite "+addr);
+ return false;
+ }
+ }
+
System.out.println("got mailsite");
PropsFile mailsite = new PropsFile(mailsite_file);
Modified: trunk/apps/Freemail/src/freemail/SingleAccountWatcher.java
===================================================================
--- trunk/apps/Freemail/src/freemail/SingleAccountWatcher.java 2006-08-03
19:37:24 UTC (rev 9871)
+++ trunk/apps/Freemail/src/freemail/SingleAccountWatcher.java 2006-08-03
19:42:01 UTC (rev 9872)
@@ -4,6 +4,7 @@
import java.lang.InterruptedException;
import freemail.utils.PropsFile;
+import freemail.utils.EmailAddress;
public class SingleAccountWatcher implements Runnable {
public static final String CONTACTS_DIR = "contacts";
@@ -52,7 +53,14 @@
//this.mf = new MailFetcher(this.mb, inbound_dir,
Freemail.getFCPConnection());
// temporary info message until there's a nicer UI :)
- System.out.println("Freemail address:
"+AccountManager.getFreemailAddress(accdir));
+ System.out.println("Secure Freemail address:
"+AccountManager.getFreemailAddress(accdir));
+
+ EmailAddress shortaddr =
AccountManager.getKSKFreemailAddress(accdir);
+ if (shortaddr != null) {
+ System.out.println("Short Freemail address (*probably*
secure): "+shortaddr);
+ } else {
+ System.out.println("You don't have a short Freemail
address. You could get one by running Freemail with the --shortaddress option,
followed by your account name and the name you'd like. For example, 'java -jar
freemail.jar --shortaddress bob bob' will give you the address 'anything at
bob.freemail'. Try to pick something unique!");
+ }
}
public void run() {
Modified: trunk/apps/Freemail/src/freemail/fcp/HighLevelFCPClient.java
===================================================================
--- trunk/apps/Freemail/src/freemail/fcp/HighLevelFCPClient.java
2006-08-03 19:37:24 UTC (rev 9871)
+++ trunk/apps/Freemail/src/freemail/fcp/HighLevelFCPClient.java
2006-08-03 19:42:01 UTC (rev 9872)
@@ -137,42 +137,6 @@
}
}
- public synchronized FCPInsertErrorMessage putRedirect(String fromKey,
String targetKey) {
- FCPMessage msg = this.conn.getMessage("ClientPut");
- msg.headers.put("URI", fromKey);
- msg.headers.put("Persistence", "connection");
- msg.headers.put("UploadFrom", "redirect");
- msg.headers.put("TargetURI", targetKey);
-
- while (true) {
- try {
- this.conn.doRequest(this, msg);
- break;
- } catch (NoNodeConnectionException nnce) {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException ie) {
- }
- } catch (FCPBadFileException bfe) {
- // impossible
- }
- }
-
- this.donemsg = null;
- while (this.donemsg == null) {
- try {
- this.wait();
- } catch (InterruptedException ie) {
- }
- }
-
- if (this.donemsg.getType().equalsIgnoreCase("PutSuccessful")) {
- return null;
- } else {
- return new FCPInsertErrorMessage(donemsg);
- }
- }
-
public int SlotInsert(File data, String basekey, int minslot, String
suffix) {
int slot = minslot;
boolean carryon = true;
Modified: trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
===================================================================
--- trunk/apps/Freemail/src/freemail/utils/EmailAddress.java 2006-08-03
19:37:24 UTC (rev 9871)
+++ trunk/apps/Freemail/src/freemail/utils/EmailAddress.java 2006-08-03
19:42:01 UTC (rev 9872)
@@ -68,7 +68,7 @@
return this.getSubDomain().equalsIgnoreCase("nim");
}
- private boolean is_ssk_address() {
+ public boolean is_ssk_address() {
if (!this.is_freemail_address()) return false;
String key;
try {
@@ -85,7 +85,7 @@
}
// get the part of the domain before the '.freemail'
- private String getSubDomain() {
+ public String getSubDomain() {
String[] domparts = this.domain.split("\\.", 2);
if (domparts.length < 2) return null;
@@ -93,18 +93,15 @@
return domparts[0];
}
- public String getMailsiteKey() {
- if (this.is_ssk_address()) {
- return new String (Base32.decode(this.getSubDomain()));
- } else {
- return this.getSubDomain();
- }
- }
-
public String getMailpageKey() {
if (this.is_ssk_address()) {
+ System.out.println("detected ssk address");
+
return "USK@"+new String
(Base32.decode(this.getSubDomain()))+"/"+AccountManager.MAILSITE_SUFFIX+"/"+AccountManager.MAILSITE_VERSION+"/"+MailSite.MAILPAGE;
} else {
+ System.out.println("detected ksk address");
+
System.out.println("KSK@"+this.getSubDomain()+MailSite.ALIAS_SUFFIX);
+
return "KSK@"+this.getSubDomain()+MailSite.ALIAS_SUFFIX;
}
}