/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2019-2020 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <sys/stat.h>2122#include <errno.h>23#include <fcntl.h>24#include <unistd.h>2526#include <sudo_compat.h>2728#ifndef HAVE_FCHMODAT29int30sudo_fchmodat(int dfd, const char *path, mode_t mode, int flag)31{32int odfd, ret = -1;3334if (ISSET(flag, AT_SYMLINK_NOFOLLOW)) {35errno = ENOTSUP;36return -1;37}3839if (dfd == (int)AT_FDCWD)40return chmod(path, mode);4142/* Save cwd (cannot use O_PATH on older Linux kernels). */43if ((odfd = open(".", O_RDONLY|O_DIRECTORY)) == -1)44goto done;4546if (fchdir(dfd) == -1)47goto done;4849ret = chmod(path, mode);5051/* Restore cwd */52if (fchdir(odfd) == -1) {53/* Should not happen */54ret = -1;55}5657done:58if (odfd != -1)59close(odfd);6061return ret;62}63#endif /* HAVE_FCHMODAT */646566