1999-09-23 13:31:59 -04:00
|
|
|
/*
|
2011-03-11 23:59:49 -05:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
1999-09-23 13:31:59 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 02:37:05 -04:00
|
|
|
*
|
1999-09-23 13:31:59 -04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 03:53:12 -05:00
|
|
|
*
|
1999-09-23 13:31:59 -04:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-04-27 00:57:32 -04:00
|
|
|
/*! \file */
|
1999-09-23 13:31:59 -04:00
|
|
|
|
2000-07-26 22:04:36 -04:00
|
|
|
#include <ctype.h>
|
1999-09-23 13:31:59 -04:00
|
|
|
#include <errno.h>
|
2022-12-16 06:04:39 -05:00
|
|
|
#include <netdb.h>
|
2020-03-09 11:17:26 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
1999-10-31 14:29:48 -05:00
|
|
|
#include <unistd.h>
|
1999-09-23 13:31:59 -04:00
|
|
|
|
|
|
|
|
#include <isc/dir.h>
|
2000-05-11 11:09:30 -04:00
|
|
|
#include <isc/magic.h>
|
2000-05-08 10:38:29 -04:00
|
|
|
#include <isc/string.h>
|
2000-04-27 21:12:23 -04:00
|
|
|
#include <isc/util.h>
|
1999-09-23 13:31:59 -04:00
|
|
|
|
2000-05-11 11:09:30 -04:00
|
|
|
#include "errno2result.h"
|
|
|
|
|
|
2001-06-04 15:33:39 -04:00
|
|
|
#define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
|
2000-05-11 11:09:30 -04:00
|
|
|
#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
|
1999-09-23 13:31:59 -04:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
isc_dir_init(isc_dir_t *dir) {
|
|
|
|
|
REQUIRE(dir != NULL);
|
|
|
|
|
|
|
|
|
|
dir->entry.name[0] = '\0';
|
|
|
|
|
dir->entry.length = 0;
|
|
|
|
|
|
|
|
|
|
dir->handle = NULL;
|
|
|
|
|
|
|
|
|
|
dir->magic = ISC_DIR_MAGIC;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 00:57:32 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Allocate workspace and open directory stream. If either one fails,
|
1999-09-23 13:31:59 -04:00
|
|
|
* NULL will be returned.
|
|
|
|
|
*/
|
|
|
|
|
isc_result_t
|
1999-10-31 14:08:17 -05:00
|
|
|
isc_dir_open(isc_dir_t *dir, const char *dirname) {
|
2005-09-04 20:12:29 -04:00
|
|
|
char *p;
|
1999-09-23 13:31:59 -04:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
|
|
REQUIRE(VALID_DIR(dir));
|
1999-10-31 14:08:17 -05:00
|
|
|
REQUIRE(dirname != NULL);
|
1999-09-23 13:31:59 -04:00
|
|
|
|
2005-09-04 20:12:29 -04:00
|
|
|
/*
|
|
|
|
|
* Copy directory name. Need to have enough space for the name,
|
|
|
|
|
* a possible path separator, the wildcard, and the final NUL.
|
|
|
|
|
*/
|
2017-09-13 03:14:37 -04:00
|
|
|
if (strlen(dirname) + 3 > sizeof(dir->dirname)) {
|
2005-09-04 20:12:29 -04:00
|
|
|
/* XXXDCL ? */
|
|
|
|
|
return (ISC_R_NOSPACE);
|
2017-09-13 03:14:37 -04:00
|
|
|
}
|
|
|
|
|
strlcpy(dir->dirname, dirname, sizeof(dir->dirname));
|
2005-09-04 20:12:29 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Append path separator, if needed, and "*".
|
|
|
|
|
*/
|
|
|
|
|
p = dir->dirname + strlen(dir->dirname);
|
|
|
|
|
if (dir->dirname < p && *(p - 1) != '/') {
|
|
|
|
|
*p++ = '/';
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
2005-09-04 20:12:29 -04:00
|
|
|
*p++ = '*';
|
2011-03-11 01:11:27 -05:00
|
|
|
*p = '\0';
|
2005-09-04 20:12:29 -04:00
|
|
|
|
1999-09-23 13:31:59 -04:00
|
|
|
/*
|
|
|
|
|
* Open stream.
|
|
|
|
|
*/
|
|
|
|
|
dir->handle = opendir(dirname);
|
|
|
|
|
|
2017-09-13 03:14:37 -04:00
|
|
|
if (dir->handle == NULL) {
|
|
|
|
|
return (isc__errno2result(errno));
|
|
|
|
|
}
|
1999-09-23 13:31:59 -04:00
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 00:57:32 -04:00
|
|
|
/*!
|
2008-12-01 18:47:45 -05:00
|
|
|
* \brief Return previously retrieved file or get next one.
|
2020-02-13 15:48:23 -05:00
|
|
|
*
|
2005-04-27 00:57:32 -04:00
|
|
|
* Unix's dirent has
|
1999-09-23 13:31:59 -04:00
|
|
|
* separate open and read functions, but the Win32 and DOS interfaces open
|
|
|
|
|
* the dir stream and reads the first file in one operation.
|
|
|
|
|
*/
|
|
|
|
|
isc_result_t
|
|
|
|
|
isc_dir_read(isc_dir_t *dir) {
|
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
|
|
REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fetch next file in directory.
|
|
|
|
|
*/
|
|
|
|
|
entry = readdir(dir->handle);
|
|
|
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
|
return (ISC_R_NOMORE);
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
1999-09-23 13:31:59 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make sure that the space for the name is long enough.
|
|
|
|
|
*/
|
1999-09-30 21:12:04 -04:00
|
|
|
if (sizeof(dir->entry.name) <= strlen(entry->d_name)) {
|
2017-09-13 03:14:37 -04:00
|
|
|
return (ISC_R_UNEXPECTED);
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
1999-09-23 13:31:59 -04:00
|
|
|
|
2017-09-13 03:14:37 -04:00
|
|
|
strlcpy(dir->entry.name, entry->d_name, sizeof(dir->entry.name));
|
1999-09-23 17:35:19 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Some dirents have d_namlen, but it is not portable.
|
|
|
|
|
*/
|
|
|
|
|
dir->entry.length = strlen(entry->d_name);
|
1999-09-23 13:31:59 -04:00
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 00:57:32 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Close directory stream.
|
1999-09-23 13:31:59 -04:00
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
isc_dir_close(isc_dir_t *dir) {
|
|
|
|
|
REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
|
|
|
|
|
|
1999-10-31 14:08:17 -05:00
|
|
|
(void)closedir(dir->handle);
|
1999-09-23 13:31:59 -04:00
|
|
|
dir->handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 00:57:32 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Reposition directory stream at start.
|
1999-09-23 13:31:59 -04:00
|
|
|
*/
|
|
|
|
|
isc_result_t
|
|
|
|
|
isc_dir_reset(isc_dir_t *dir) {
|
|
|
|
|
REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
|
|
|
|
|
|
|
|
|
|
rewinddir(dir->handle);
|
|
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
}
|
1999-10-31 14:08:17 -05:00
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
|
isc_dir_chdir(const char *dirname) {
|
2005-04-27 00:57:32 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Change the current directory to 'dirname'.
|
1999-10-31 14:08:17 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
REQUIRE(dirname != NULL);
|
|
|
|
|
|
2000-05-11 11:09:30 -04:00
|
|
|
if (chdir(dirname) < 0) {
|
|
|
|
|
return (isc__errno2result(errno));
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
1999-10-31 14:08:17 -05:00
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
}
|
2000-07-26 22:04:36 -04:00
|
|
|
|
2001-01-28 22:17:45 -05:00
|
|
|
isc_result_t
|
|
|
|
|
isc_dir_chroot(const char *dirname) {
|
2017-02-03 14:26:37 -05:00
|
|
|
#ifdef HAVE_CHROOT
|
|
|
|
|
void *tmp;
|
|
|
|
|
#endif /* ifdef HAVE_CHROOT */
|
2001-01-28 22:17:45 -05:00
|
|
|
|
|
|
|
|
REQUIRE(dirname != NULL);
|
|
|
|
|
|
2008-11-30 22:53:32 -05:00
|
|
|
#ifdef HAVE_CHROOT
|
2017-02-02 22:22:03 -05:00
|
|
|
/*
|
|
|
|
|
* Try to use getservbyname and getprotobyname before chroot.
|
|
|
|
|
* If WKS records are used in a zone under chroot, Name Service Switch
|
|
|
|
|
* may fail to load library in chroot.
|
|
|
|
|
* Do not report errors if it fails, we do not need any result now.
|
|
|
|
|
*/
|
2017-02-03 14:26:37 -05:00
|
|
|
tmp = getprotobyname("udp");
|
|
|
|
|
if (tmp != NULL) {
|
|
|
|
|
(void)getservbyname("domain", "udp");
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
2017-02-02 22:22:03 -05:00
|
|
|
|
2009-02-15 21:01:16 -05:00
|
|
|
if (chroot(dirname) < 0 || chdir("/") < 0) {
|
2001-01-28 22:17:45 -05:00
|
|
|
return (isc__errno2result(errno));
|
2020-02-13 15:48:23 -05:00
|
|
|
}
|
2001-01-28 22:17:45 -05:00
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2008-11-30 22:53:32 -05:00
|
|
|
#else /* ifdef HAVE_CHROOT */
|
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
#endif /* ifdef HAVE_CHROOT */
|
2001-01-28 22:17:45 -05:00
|
|
|
}
|