Blame lib/isc/unix/dir.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
/*! \file */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
#include <sys/types.h>
Packit Service ae04f2
#include <sys/stat.h>
Packit Service ae04f2
Packit Service ae04f2
#include <ctype.h>
Packit Service ae04f2
#include <errno.h>
Packit Service ae04f2
#include <unistd.h>
Packit Service ae04f2
Packit Service ae04f2
#include <isc/dir.h>
Packit Service ae04f2
#include <isc/magic.h>
Packit Service ae04f2
#include <isc/netdb.h>
Packit Service ae04f2
#include <isc/print.h>
Packit Service ae04f2
#include <isc/string.h>
Packit Service ae04f2
#include <isc/util.h>
Packit Service ae04f2
Packit Service ae04f2
#include "errno2result.h"
Packit Service ae04f2
Packit Service ae04f2
#define ISC_DIR_MAGIC		ISC_MAGIC('D', 'I', 'R', '*')
Packit Service ae04f2
#define VALID_DIR(dir)		ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_dir_init(isc_dir_t *dir) {
Packit Service ae04f2
	REQUIRE(dir != NULL);
Packit Service ae04f2
Packit Service ae04f2
	dir->entry.name[0] = '\0';
Packit Service ae04f2
	dir->entry.length = 0;
Packit Service ae04f2
Packit Service ae04f2
	dir->handle = NULL;
Packit Service ae04f2
Packit Service ae04f2
	dir->magic = ISC_DIR_MAGIC;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Allocate workspace and open directory stream. If either one fails,
Packit Service ae04f2
 * NULL will be returned.
Packit Service ae04f2
 */
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_open(isc_dir_t *dir, const char *dirname) {
Packit Service ae04f2
	char *p;
Packit Service ae04f2
	isc_result_t result = ISC_R_SUCCESS;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(VALID_DIR(dir));
Packit Service ae04f2
	REQUIRE(dirname != NULL);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Copy directory name.  Need to have enough space for the name,
Packit Service ae04f2
	 * a possible path separator, the wildcard, and the final NUL.
Packit Service ae04f2
	 */
Packit Service ae04f2
	if (strlen(dirname) + 3 > sizeof(dir->dirname)) {
Packit Service ae04f2
		/* XXXDCL ? */
Packit Service ae04f2
		return (ISC_R_NOSPACE);
Packit Service ae04f2
	}
Packit Service ae04f2
	strlcpy(dir->dirname, dirname, sizeof(dir->dirname));
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Append path separator, if needed, and "*".
Packit Service ae04f2
	 */
Packit Service ae04f2
	p = dir->dirname + strlen(dir->dirname);
Packit Service ae04f2
	if (dir->dirname < p && *(p - 1) != '/')
Packit Service ae04f2
		*p++ = '/';
Packit Service ae04f2
	*p++ = '*';
Packit Service ae04f2
	*p = '\0';
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Open stream.
Packit Service ae04f2
	 */
Packit Service ae04f2
	dir->handle = opendir(dirname);
Packit Service ae04f2
Packit Service ae04f2
	if (dir->handle == NULL) {
Packit Service ae04f2
		return (isc__errno2result(errno));
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (result);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Return previously retrieved file or get next one.
Packit Service ae04f2
Packit Service ae04f2
 * Unix's dirent has
Packit Service ae04f2
 * separate open and read functions, but the Win32 and DOS interfaces open
Packit Service ae04f2
 * the dir stream and reads the first file in one operation.
Packit Service ae04f2
 */
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_read(isc_dir_t *dir) {
Packit Service ae04f2
	struct dirent *entry;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Fetch next file in directory.
Packit Service ae04f2
	 */
Packit Service ae04f2
	entry = readdir(dir->handle);
Packit Service ae04f2
Packit Service ae04f2
	if (entry == NULL)
Packit Service ae04f2
		return (ISC_R_NOMORE);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Make sure that the space for the name is long enough.
Packit Service ae04f2
	 */
Packit Service ae04f2
	if (sizeof(dir->entry.name) <= strlen(entry->d_name))
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
Packit Service ae04f2
	strlcpy(dir->entry.name, entry->d_name, sizeof(dir->entry.name));
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Some dirents have d_namlen, but it is not portable.
Packit Service ae04f2
	 */
Packit Service ae04f2
	dir->entry.length = strlen(entry->d_name);
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Close directory stream.
Packit Service ae04f2
 */
Packit Service ae04f2
void
Packit Service ae04f2
isc_dir_close(isc_dir_t *dir) {
Packit Service ae04f2
       REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
Packit Service ae04f2
Packit Service ae04f2
       (void)closedir(dir->handle);
Packit Service ae04f2
       dir->handle = NULL;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Reposition directory stream at start.
Packit Service ae04f2
 */
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_reset(isc_dir_t *dir) {
Packit Service ae04f2
	REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
Packit Service ae04f2
Packit Service ae04f2
	rewinddir(dir->handle);
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_chdir(const char *dirname) {
Packit Service ae04f2
	/*!
Packit Service ae04f2
	 * \brief Change the current directory to 'dirname'.
Packit Service ae04f2
	 */
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(dirname != NULL);
Packit Service ae04f2
Packit Service ae04f2
	if (chdir(dirname) < 0)
Packit Service ae04f2
		return (isc__errno2result(errno));
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_chroot(const char *dirname) {
Packit Service ae04f2
#ifdef HAVE_CHROOT
Packit Service ae04f2
	void *tmp;
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(dirname != NULL);
Packit Service ae04f2
Packit Service ae04f2
#ifdef HAVE_CHROOT
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Try to use getservbyname and getprotobyname before chroot.
Packit Service ae04f2
	 * If WKS records are used in a zone under chroot, Name Service Switch
Packit Service ae04f2
	 * may fail to load library in chroot.
Packit Service ae04f2
	 * Do not report errors if it fails, we do not need any result now.
Packit Service ae04f2
	 */
Packit Service ae04f2
	tmp = getprotobyname("udp");
Packit Service ae04f2
	if (tmp != NULL)
Packit Service ae04f2
		(void) getservbyname("domain", "udp");
Packit Service ae04f2
Packit Service ae04f2
	if (chroot(dirname) < 0 || chdir("/") < 0)
Packit Service ae04f2
		return (isc__errno2result(errno));
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
#else
Packit Service ae04f2
	return (ISC_R_NOTIMPLEMENTED);
Packit Service ae04f2
#endif
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_dir_createunique(char *templet) {
Packit Service ae04f2
	isc_result_t result;
Packit Service ae04f2
	char *x;
Packit Service ae04f2
	char *p;
Packit Service ae04f2
	int i;
Packit Service ae04f2
	int pid;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(templet != NULL);
Packit Service ae04f2
Packit Service ae04f2
	/*!
Packit Service ae04f2
	 * \brief mkdtemp is not portable, so this emulates it.
Packit Service ae04f2
	 */
Packit Service ae04f2
Packit Service ae04f2
	pid = getpid();
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Replace trailing Xs with the process-id, zero-filled.
Packit Service ae04f2
	 */
Packit Service ae04f2
	for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
Packit Service ae04f2
	     x--, pid /= 10)
Packit Service ae04f2
		*x = pid % 10 + '0';
Packit Service ae04f2
Packit Service ae04f2
	x++;			/* Set x to start of ex-Xs. */
Packit Service ae04f2
Packit Service ae04f2
	do {
Packit Service ae04f2
		i = mkdir(templet, 0700);
Packit Service ae04f2
		if (i == 0 || errno != EEXIST)
Packit Service ae04f2
			break;
Packit Service ae04f2
Packit Service ae04f2
		/*
Packit Service ae04f2
		 * The BSD algorithm.
Packit Service ae04f2
		 */
Packit Service ae04f2
		p = x;
Packit Service ae04f2
		while (*p != '\0') {
Packit Service ae04f2
			if (isdigit(*p & 0xff))
Packit Service ae04f2
				*p = 'a';
Packit Service ae04f2
			else if (*p != 'z')
Packit Service ae04f2
				++*p;
Packit Service ae04f2
			else {
Packit Service ae04f2
				/*
Packit Service ae04f2
				 * Reset character and move to next.
Packit Service ae04f2
				 */
Packit Service ae04f2
				*p++ = 'a';
Packit Service ae04f2
				continue;
Packit Service ae04f2
			}
Packit Service ae04f2
Packit Service ae04f2
			break;
Packit Service ae04f2
		}
Packit Service ae04f2
Packit Service ae04f2
		if (*p == '\0') {
Packit Service ae04f2
			/*
Packit Service ae04f2
			 * Tried all combinations.  errno should already
Packit Service ae04f2
			 * be EEXIST, but ensure it is anyway for
Packit Service ae04f2
			 * isc__errno2result().
Packit Service ae04f2
			 */
Packit Service ae04f2
			errno = EEXIST;
Packit Service ae04f2
			break;
Packit Service ae04f2
		}
Packit Service ae04f2
	} while (1);
Packit Service ae04f2
Packit Service ae04f2
	if (i == -1)
Packit Service ae04f2
		result = isc__errno2result(errno);
Packit Service ae04f2
	else
Packit Service ae04f2
		result = ISC_R_SUCCESS;
Packit Service ae04f2
Packit Service ae04f2
	return (result);
Packit Service ae04f2
}