From: millert Date: Mon, 24 Feb 1997 20:43:27 +0000 (+0000) Subject: Only update owner (via lchown) when dealing with symbolic links. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=a7f40264f0cf2ab09a8deba4bb3128ecca0b44ce;p=openbsd Only update owner (via lchown) when dealing with symbolic links. Fixes core dump problem with ``-p'' and symbolic links. Noted by Carsten Hammer . --- diff --git a/bin/cp/utils.c b/bin/cp/utils.c index c54b07e7f81..7e9d94aa5dd 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.c,v 1.3 1997/01/26 22:04:45 kstailey Exp $ */ +/* $OpenBSD: utils.c,v 1.4 1997/02/24 20:43:27 millert Exp $ */ /* $NetBSD: utils.c,v 1.4 1995/08/02 07:17:02 jtc Exp $ */ /*- @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; #else -static char rcsid[] = "$OpenBSD: utils.c,v 1.3 1997/01/26 22:04:45 kstailey Exp $"; +static char rcsid[] = "$OpenBSD: utils.c,v 1.4 1997/02/24 20:43:27 millert Exp $"; #endif #endif /* not lint */ @@ -200,7 +200,7 @@ copy_link(p, exists) warn("symlink: %s", link); return (1); } - return (pflag ? setfile(NULL, 0) : 0); + return (pflag ? setfile(p->fts_statp, 0) : 0); } int @@ -242,18 +242,26 @@ setfile(fs, fd) int fd; { static struct timeval tv[2]; - int rval; + int rval = 0; - rval = 0; - if (fs != NULL) { - fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; - - TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec); - TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec); - if (utimes(to.p_path, tv)) { - warn("utimes: %s", to.p_path); - rval = 1; + /* Only change the owner for symbolic links. */ + if (S_ISLNK(fs->st_mode)) { + if (lchown(to.p_path, fs->st_uid, fs->st_gid)) { + if (errno != EPERM) { + warn("lchown: %s", to.p_path); + rval = 1; + } } + return (rval); + } + + fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + + TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec); + TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec); + if (utimes(to.p_path, tv)) { + warn("utimes: %s", to.p_path); + rval = 1; } /* * Changing the ownership probably won't succeed, unless we're root @@ -262,17 +270,15 @@ setfile(fs, fd) * chown. If chown fails, lose setuid/setgid bits. */ if (fd ? fchown(fd, fs->st_uid, fs->st_gid) : - lchown(to.p_path, fs->st_uid, fs->st_gid)) { + chown(to.p_path, fs->st_uid, fs->st_gid)) { if (errno != EPERM) { - warn("lchown: %s", to.p_path); + warn("chown: %s", to.p_path); rval = 1; } - if (fs == NULL) - return (rval); fs->st_mode &= ~(S_ISUID | S_ISGID); } if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) { - warn("chown: %s", to.p_path); + warn("chmod: %s", to.p_path); rval = 1; }