=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/rdistd/server.c,v retrieving revision 1.37 retrieving revision 1.38 diff -c -r1.37 -r1.38 *** src/usr.bin/rdistd/server.c 2015/01/21 04:08:37 1.37 --- src/usr.bin/rdistd/server.c 2015/11/15 06:18:19 1.38 *************** *** 1,4 **** ! /* $OpenBSD: server.c,v 1.37 2015/01/21 04:08:37 guenther Exp $ */ /* * Copyright (c) 1983 Regents of the University of California. --- 1,4 ---- ! /* $OpenBSD: server.c,v 1.38 2015/11/15 06:18:19 guenther Exp $ */ /* * Copyright (c) 1983 Regents of the University of California. *************** *** 106,134 **** static int setownership(char *file, int fd, uid_t uid, gid_t gid, int islink) { int status = -1; /* * We assume only the Superuser can change uid ownership. */ ! if (getuid() != 0) uid = -1; - if (islink) - status = lchown(file, uid, gid); - if (fd != -1 && !islink) status = fchown(fd, uid, gid); - if (status < 0 && !islink) - status = chown(file, uid, gid); - if (status < 0) { if (uid == (uid_t)-1) message(MT_NOTICE, "%s: chgrp %d failed: %s", target, gid, SYSERR); else ! message(MT_NOTICE, "%s: chown %d.%d failed: %s", target, uid, gid, SYSERR); return(-1); } --- 106,142 ---- static int setownership(char *file, int fd, uid_t uid, gid_t gid, int islink) { + static int is_root = -1; int status = -1; /* * We assume only the Superuser can change uid ownership. */ ! switch (is_root) { ! case -1: ! is_root = getuid() == 0; ! if (is_root) ! break; ! /* FALLTHROUGH */ ! case 0: uid = -1; + break; + case 1: + break; + } if (fd != -1 && !islink) status = fchown(fd, uid, gid); + else + status = fchownat(AT_FDCWD, file, uid, gid, + AT_SYMLINK_NOFOLLOW); if (status < 0) { if (uid == (uid_t)-1) message(MT_NOTICE, "%s: chgrp %d failed: %s", target, gid, SYSERR); else ! message(MT_NOTICE, "%s: chown %d:%d failed: %s", target, uid, gid, SYSERR); return(-1); } *************** *** 693,699 **** for (i = 1; i < 1000; i++) { (void) snprintf(savefile, sizeof(savefile), "%s;%.3d", file, i); ! if (stat(savefile, &st) == -1 && errno == ENOENT) break; } --- 701,707 ---- for (i = 1; i < 1000; i++) { (void) snprintf(savefile, sizeof(savefile), "%s;%.3d", file, i); ! if (lstat(savefile, &st) == -1 && errno == ENOENT) break; } *************** *** 1271,1277 **** error("%s: unlink failed: %s", target, SYSERR); return; } ! if (link(expbuf, target) < 0) { error("%s: cannot link to %s: %s", target, oldname, SYSERR); return; } --- 1279,1285 ---- error("%s: unlink failed: %s", target, SYSERR); return; } ! if (linkat(AT_FDCWD, expbuf, AT_FDCWD, target, 0) < 0) { error("%s: cannot link to %s: %s", target, oldname, SYSERR); return; }