Index: otr.c
===================================================================
--- otr.c	(revision aea22cd1e0e955848c143a8fc36978391e9a4846)
+++ otr.c	(revision 68286eb08dbb6c2aad555f155da6f16ee6f061e8)
@@ -185,4 +185,7 @@
 /* find a private key by fingerprint prefix (given as any number of hex strings) */
 OtrlPrivKey *match_privkey(irc_t *irc, const char **args);
+
+/* check whether a string is safe to use in a path component */
+int strsane(const char *s);
 
 /* functions to be called for certain events */
@@ -275,13 +278,15 @@
 	int kg=0;
 
-	g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick);
-	e = otrl_privkey_read(irc->otr->us, s);
-	if(e && e!=enoent) {
-		irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
-	}
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
-	e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL);
-	if(e && e!=enoent) {
-		irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
+	if(strsane(irc->user->nick)) {
+		g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick);
+		e = otrl_privkey_read(irc->otr->us, s);
+		if(e && e!=enoent) {
+			irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
+		}
+		g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
+		e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL);
+		if(e && e!=enoent) {
+			irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
+		}
 	}
 	
@@ -306,10 +311,12 @@
 	gcry_error_t e;
 
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
-	e = otrl_privkey_write_fingerprints(irc->otr->us, s);
-	if(e) {
-		irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e));
-	}
-	chmod(s, 0600);
+	if(strsane(irc->user->nick)) {
+		g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
+		e = otrl_privkey_write_fingerprints(irc->otr->us, s);
+		if(e) {
+			irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e));
+		}
+		chmod(s, 0600);
+	}
 }
 
@@ -318,8 +325,10 @@
 	char s[512];
 	
-	g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick);
-	unlink(s);
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick);
-	unlink(s);
+	if(strsane(nick)) {
+		g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick);
+		unlink(s);
+		g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick);
+		unlink(s);
+	}
 }
 
@@ -328,10 +337,12 @@
 	char s[512], t[512];
 	
-	g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick);
-	g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick);
-	rename(s,t);
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick);
-	g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick);
-	rename(s,t);
+	if(strsane(nnick) && strsane(onick)) {
+		g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick);
+		g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick);
+		rename(s,t);
+		g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick);
+		g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick);
+		rename(s,t);
+	}
 }
 
@@ -1777,12 +1788,17 @@
 	irc_rootmsg(irc, "%s", msg);
 	if(filename[0]) {
-		char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick);
-		char *tmp = g_strdup_printf("%s.new", kf);
-		copyfile(filename, tmp);
-		unlink(filename);
-		rename(tmp,kf);
-		otrl_privkey_read(irc->otr->us, kf);
-		g_free(kf);
-		g_free(tmp);
+		if(strsane(irc->user->nick)) {
+			char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick);
+			char *tmp = g_strdup_printf("%s.new", kf);
+			copyfile(filename, tmp);
+			unlink(filename);
+			rename(tmp,kf);
+			otrl_privkey_read(irc->otr->us, kf);
+			g_free(kf);
+			g_free(tmp);
+		} else {
+			otrl_privkey_read(irc->otr->us, filename);
+			unlink(filename);
+		}
 	}
 	
@@ -1859,3 +1875,9 @@
 }
 
+/* check whether a string is safe to use in a path component */
+int strsane(const char *s)
+{
+	return strpbrk(s, "/\\") == NULL;
+}
+
 /* vim: set noet ts=4 sw=4: */
