summaryrefslogtreecommitdiffstats
path: root/lib/isc/unix/dir.c
blob: b7eabe9a6f1db5bbaa2435b2544c918736691905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * 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/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*! \file */

#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <isc/dir.h>
#include <isc/magic.h>
#include <isc/netdb.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/util.h>

#include "errno2result.h"

#define ISC_DIR_MAGIC  ISC_MAGIC('D', 'I', 'R', '*')
#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)

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;
}

/*!
 * \brief Allocate workspace and open directory stream. If either one fails,
 * NULL will be returned.
 */
isc_result_t
isc_dir_open(isc_dir_t *dir, const char *dirname) {
	char *p;
	isc_result_t result = ISC_R_SUCCESS;

	REQUIRE(VALID_DIR(dir));
	REQUIRE(dirname != NULL);

	/*
	 * Copy directory name.  Need to have enough space for the name,
	 * a possible path separator, the wildcard, and the final NUL.
	 */
	if (strlen(dirname) + 3 > sizeof(dir->dirname)) {
		/* XXXDCL ? */
		return (ISC_R_NOSPACE);
	}
	strlcpy(dir->dirname, dirname, sizeof(dir->dirname));

	/*
	 * Append path separator, if needed, and "*".
	 */
	p = dir->dirname + strlen(dir->dirname);
	if (dir->dirname < p && *(p - 1) != '/') {
		*p++ = '/';
	}
	*p++ = '*';
	*p = '\0';

	/*
	 * Open stream.
	 */
	dir->handle = opendir(dirname);

	if (dir->handle == NULL) {
		return (isc__errno2result(errno));
	}

	return (result);
}

/*!
 * \brief Return previously retrieved file or get next one.
 *
 * Unix's dirent has
 * 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);
	}

	/*
	 * Make sure that the space for the name is long enough.
	 */
	if (sizeof(dir->entry.name) <= strlen(entry->d_name)) {
		return (ISC_R_UNEXPECTED);
	}

	strlcpy(dir->entry.name, entry->d_name, sizeof(dir->entry.name));

	/*
	 * Some dirents have d_namlen, but it is not portable.
	 */
	dir->entry.length = strlen(entry->d_name);

	return (ISC_R_SUCCESS);
}

/*!
 * \brief Close directory stream.
 */
void
isc_dir_close(isc_dir_t *dir) {
	REQUIRE(VALID_DIR(dir) && dir->handle != NULL);

	(void)closedir(dir->handle);
	dir->handle = NULL;
}

/*!
 * \brief Reposition directory stream at start.
 */
isc_result_t
isc_dir_reset(isc_dir_t *dir) {
	REQUIRE(VALID_DIR(dir) && dir->handle != NULL);

	rewinddir(dir->handle);

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_dir_chdir(const char *dirname) {
	/*!
	 * \brief Change the current directory to 'dirname'.
	 */

	REQUIRE(dirname != NULL);

	if (chdir(dirname) < 0) {
		return (isc__errno2result(errno));
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_dir_chroot(const char *dirname) {
#ifdef HAVE_CHROOT
	void *tmp;
#endif /* ifdef HAVE_CHROOT */

	REQUIRE(dirname != NULL);

#ifdef HAVE_CHROOT
	/*
	 * 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.
	 */
	tmp = getprotobyname("udp");
	if (tmp != NULL) {
		(void)getservbyname("domain", "udp");
	}

	if (chroot(dirname) < 0 || chdir("/") < 0) {
		return (isc__errno2result(errno));
	}

	return (ISC_R_SUCCESS);
#else  /* ifdef HAVE_CHROOT */
	return (ISC_R_NOTIMPLEMENTED);
#endif /* ifdef HAVE_CHROOT */
}

isc_result_t
isc_dir_createunique(char *templet) {
	isc_result_t result;
	char *x;
	char *p;
	int i;
	int pid;

	REQUIRE(templet != NULL);

	/*!
	 * \brief mkdtemp is not portable, so this emulates it.
	 */

	pid = getpid();

	/*
	 * Replace trailing Xs with the process-id, zero-filled.
	 */
	for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
	     x--, pid /= 10)
	{
		*x = pid % 10 + '0';
	}

	x++; /* Set x to start of ex-Xs. */

	do {
		i = mkdir(templet, 0700);
		if (i == 0 || errno != EEXIST) {
			break;
		}

		/*
		 * The BSD algorithm.
		 */
		p = x;
		while (*p != '\0') {
			if (isdigit((unsigned char)*p)) {
				*p = 'a';
			} else if (*p != 'z') {
				++*p;
			} else {
				/*
				 * Reset character and move to next.
				 */
				*p++ = 'a';
				continue;
			}

			break;
		}

		if (*p == '\0') {
			/*
			 * Tried all combinations.  errno should already
			 * be EEXIST, but ensure it is anyway for
			 * isc__errno2result().
			 */
			errno = EEXIST;
			break;
		}
	} while (1);

	if (i == -1) {
		result = isc__errno2result(errno);
	} else {
		result = ISC_R_SUCCESS;
	}

	return (result);
}