To: vim-dev@vim.org Subject: Patch 6.4.002 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.4.002 Problem: Unix: There is a small chance that the ownership of the wrong file is changed. Solution: Use fchown() instead of chown() for the viminfo file and the backup file. Files: src/ex_cmds.c, src/fileio.c *** ../vim-6.4.001/src/ex_cmds.c Thu Jul 21 22:23:54 2005 --- src/ex_cmds.c Tue Nov 29 17:23:48 2005 *************** *** 14,19 **** --- 14,23 ---- #include "vim.h" #include "version.h" + #ifdef HAVE_FCNTL_H + # include + #endif + #ifdef FEAT_EX_EXTRA static int linelen __ARGS((int *has_tab)); #endif *************** *** 1510,1516 **** if (tempname != NULL) { ! fp_out = mch_fopen((char *)tempname, WRITEBIN); /* * If we can't create in the same directory, try creating a --- 1514,1536 ---- if (tempname != NULL) { ! int fd; ! ! /* Use mch_open() to be able to use O_EXCL and set file ! * protection same as original file, but strip s-bit. */ ! #ifdef UNIX ! fd = mch_open((char *)tempname, ! O_CREAT|O_EXTRA|O_EXCL|O_WRONLY, ! (int)((st_old.st_mode & 0777) | 0600)); ! #else ! fd = mch_open((char *)tempname, ! O_CREAT|O_EXTRA|O_EXCL|O_WRONLY, ! 0600); /* r&w for user only */ ! #endif ! if (fd < 0) ! fp_out = NULL; ! else ! fp_out = fdopen(fd, WRITEBIN); /* * If we can't create in the same directory, try creating a *************** *** 1522,1539 **** if ((tempname = vim_tempname('o')) != NULL) fp_out = mch_fopen((char *)tempname, WRITEBIN); } ! #ifdef UNIX /* ! * Set file protection same as original file, but strip s-bit ! * and make sure the owner can read/write it. */ if (fp_out != NULL) ! { ! (void)mch_setperm(tempname, ! (long)((st_old.st_mode & 0777) | 0600)); ! /* this only works for root: */ ! (void)chown((char *)tempname, st_old.st_uid, st_old.st_gid); ! } #endif } } --- 1542,1555 ---- if ((tempname = vim_tempname('o')) != NULL) fp_out = mch_fopen((char *)tempname, WRITEBIN); } ! ! #if defined(UNIX) && defined(HAVE_FCHOWN) /* ! * Make sure the owner can read/write it. This only works for ! * root. */ if (fp_out != NULL) ! (void)fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); #endif } } *** ../vim-6.4.001/src/fileio.c Fri Mar 18 19:16:29 2005 --- src/fileio.c Tue Nov 29 16:51:26 2005 *************** *** 3087,3093 **** if (st_old.st_nlink > 1 || mch_lstat((char *)fname, &st) < 0 || st.st_dev != st_old.st_dev ! || st.st_ino != st_old.st_ino) backup_copy = TRUE; else # endif --- 3087,3098 ---- if (st_old.st_nlink > 1 || mch_lstat((char *)fname, &st) < 0 || st.st_dev != st_old.st_dev ! || st.st_ino != st_old.st_ino ! # ifndef HAVE_FCHOWN ! || st.st_uid != st_old.st_uid ! || st.st_gid != st_old.st_gid ! # endif ! ) backup_copy = TRUE; else # endif *************** *** 3102,3125 **** for (i = 4913; ; i += 123) { sprintf((char *)gettail(IObuff), "%d", i); ! if (mch_stat((char *)IObuff, &st) < 0) break; } fd = mch_open((char *)IObuff, O_CREAT|O_WRONLY|O_EXCL, perm); - close(fd); if (fd < 0) /* can't write in directory */ backup_copy = TRUE; else { # ifdef UNIX ! chown((char *)IObuff, st_old.st_uid, st_old.st_gid); ! (void)mch_setperm(IObuff, perm); if (mch_stat((char *)IObuff, &st) < 0 || st.st_uid != st_old.st_uid || st.st_gid != st_old.st_gid || st.st_mode != perm) backup_copy = TRUE; # endif mch_remove(IObuff); } } --- 3107,3133 ---- for (i = 4913; ; i += 123) { sprintf((char *)gettail(IObuff), "%d", i); ! if (mch_lstat((char *)IObuff, &st) < 0) break; } fd = mch_open((char *)IObuff, O_CREAT|O_WRONLY|O_EXCL, perm); if (fd < 0) /* can't write in directory */ backup_copy = TRUE; else { # ifdef UNIX ! # ifdef HAVE_FCHOWN ! fchown(fd, st_old.st_uid, st_old.st_gid); ! # endif if (mch_stat((char *)IObuff, &st) < 0 || st.st_uid != st_old.st_uid || st.st_gid != st_old.st_gid || st.st_mode != perm) backup_copy = TRUE; # endif + /* Close the file before removing it, on MS-Windows we + * can't delete an open file. */ + close(fd); mch_remove(IObuff); } } *************** *** 3333,3343 **** * bits for the group same as the protection bits for * others. */ ! if (st_new.st_gid != st_old.st_gid && # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ ! fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 ! # else ! chown((char *)backup, (uid_t)-1, st_old.st_gid) != 0 # endif ) mch_setperm(backup, --- 3341,3349 ---- * bits for the group same as the protection bits for * others. */ ! if (st_new.st_gid != st_old.st_gid # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ ! && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 # endif ) mch_setperm(backup, *************** *** 3999,4004 **** --- 4005,4033 ---- } #endif + #ifdef UNIX + /* When creating a new file, set its owner/group to that of the original + * file. Get the new device and inode number. */ + if (backup != NULL && !backup_copy) + { + # ifdef HAVE_FCHOWN + struct stat st; + + /* don't change the owner when it's already OK, some systems remove + * permission or ACL stuff */ + if (mch_stat((char *)wfname, &st) < 0 + || st.st_uid != st_old.st_uid + || st.st_gid != st_old.st_gid) + { + fchown(fd, st_old.st_uid, st_old.st_gid); + if (perm >= 0) /* set permission again, may have changed */ + (void)mch_setperm(wfname, perm); + } + # endif + buf_setino(buf); + } + #endif + if (close(fd) != 0) { errmsg = (char_u *)_("E512: Close failed"); *************** *** 4021,4047 **** * ACL on a file the user doesn't own). */ if (!backup_copy) mch_set_acl(wfname, acl); - #endif - - #ifdef UNIX - /* When creating a new file, set its owner/group to that of the original - * file. Get the new device and inode number. */ - if (backup != NULL && !backup_copy) - { - struct stat st; - - /* don't change the owner when it's already OK, some systems remove - * permission or ACL stuff */ - if (mch_stat((char *)wfname, &st) < 0 - || st.st_uid != st_old.st_uid - || st.st_gid != st_old.st_gid) - { - chown((char *)wfname, st_old.st_uid, st_old.st_gid); - if (perm >= 0) /* set permission again, may have changed */ - (void)mch_setperm(wfname, perm); - } - buf_setino(buf); - } #endif --- 4050,4055 ---- *** ../vim-6.4.001/src/version.c Mon Oct 17 11:09:59 2005 --- src/version.c Tue Nov 29 19:23:24 2005 *************** *** 643,644 **** --- 643,646 ---- { /* Add new patch number below this line */ + /**/ + 2, /**/ -- hundred-and-one symptoms of being an internet addict: 250. You've given up the search for the "perfect woman" and instead, sit in front of the PC until you're just too tired to care. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ download, build and distribute -- http://www.A-A-P.org /// \\\ help me help AIDS victims -- http://www.ICCF.nl ///