Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH]: restore permissions and mode on HISTFILE when renaming it



Hi,

There is one annoying thing in handling history file in zsh. When it uses 
tmpfile (not append mode) and just does rename(".historyz.new", ".history") 
it looses all permissions and user/owner of the original .historyz file.

Exaple, I'm working as user arekm and .historyz has arekm:users user/group but 
if I do sudo zsh; ls; exit then it's renamed and I end up with .historyz 
being root:root and since I've exited sudo and I'm back as arekm:users my 
history is no longer saved.

Attached patch makes zsh restore original permissions/owner/group of HISTFILE 
after sucessful rename. It's against HEAD (4.3) cvs version.

Any races here (between stat and chown/chmod) shouldn't be unsafe I guess.

Thanks!
-- 
Arkadiusz Miśkiewicz                    PLD/Linux Team
http://www.t17.ds.pwr.wroc.pl/~misiek/  http://ftp.pld-linux.org/
--- zsh.org/Src/hist.c.org	2005-12-16 10:51:24.680963000 +0100
+++ zsh/Src/hist.c	2005-12-16 10:59:14.100963000 +0100
@@ -2127,8 +2127,18 @@
 	}
 	fclose(out);
 	if (tmpfile) {
+	    struct stat sb;
+	    int restore = 0;
+	    if (stat(unmeta(fn), &sb) == 0)
+		    restore = 1;
 	    if (rename(tmpfile, unmeta(fn)) < 0)
 		zerr("can't rename %s.new to $HISTFILE", fn, 0);
+	    else if (restore) {
+		    if (chown(unmeta(fn), sb.st_uid, sb.st_gid) < 0)
+			    zerr("can't restore user/group on $HISTFILE", NULL, 0);
+		    if (chmod(unmeta(fn), sb.st_mode) < 0)
+			    zerr("can't restore permissions on $HISTFILE", NULL, 0);
+	    }
 	    free(tmpfile);
 	}
 


Messages sorted by: Reverse Date, Date, Thread, Author