summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/util.c
diff options
context:
space:
mode:
authorGilles Chehade <gilles@cvs.openbsd.org>2012-09-16 11:53:58 +0000
committerGilles Chehade <gilles@cvs.openbsd.org>2012-09-16 11:53:58 +0000
commit0bf4819e90622a32a98bdfbd34cdd6208c807ed1 (patch)
treeffa90b6c0b072144fa5a125fb4dd6efcc8fa2cf8 /usr.sbin/smtpd/util.c
parent4f00614d05871c0103e38198685c7aacffa83440 (diff)
replace BSD-licensed mkdir_p() with ISC-licensed mkdirs(), this allows us
to avoid a dual-licensed util.c for no reason ok chl@
Diffstat (limited to 'usr.sbin/smtpd/util.c')
-rw-r--r--usr.sbin/smtpd/util.c123
1 files changed, 50 insertions, 73 deletions
diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c
index 4da4dcba48e..3458a54599f 100644
--- a/usr.sbin/smtpd/util.c
+++ b/usr.sbin/smtpd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.76 2012/09/15 15:12:11 eric Exp $ */
+/* $OpenBSD: util.c,v 1.77 2012/09/16 11:53:57 gilles Exp $ */
/*
* Copyright (c) 2000,2001 Markus Friedl. All rights reserved.
@@ -19,37 +19,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-/* the mkdir_p() function is based on bin/mkdir/mkdir.c that is covered
- * by the following license: */
-/*
- * Copyright (c) 1983, 1992, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
#include <sys/types.h>
#include <sys/param.h>
#include <sys/queue.h>
@@ -134,59 +103,67 @@ bsnprintf(char *str, size_t size, const char *format, ...)
return 1;
}
-/*
- * mkdir -p. Based on bin/mkdir/mkdir.c:mkpath()
- */
-int
-mkdir_p(char *path, mode_t mode)
+
+static int
+mkdirs_component(char *path, mode_t mode)
{
- struct stat sb;
- char *slash;
- int done, exists;
- mode_t dir_mode;
+ struct stat sb;
- dir_mode = mode | S_IWUSR | S_IXUSR;
+ if (stat(path, &sb) == -1) {
+ if (errno != ENOENT)
+ return 0;
+ if (mkdir(path, mode | S_IWUSR | S_IXUSR) == -1)
+ return 0;
+ }
+ else if (! S_ISDIR(sb.st_mode))
+ return 0;
- slash = path;
+ return 1;
+}
- for (;;) {
- slash += strspn(slash, "/");
- slash += strcspn(slash, "/");
+int
+mkdirs(char *path, mode_t mode)
+{
+ char buf[MAXPATHLEN];
+ int i = 0;
+ int done = 0;
+ char *p;
- done = (*slash == '\0');
- *slash = '\0';
+ /* absolute path required */
+ if (*path != '/')
+ return 0;
- /* skip existing path components */
- exists = !stat(path, &sb);
- if (!done && exists && S_ISDIR(sb.st_mode)) {
- *slash = '/';
- continue;
- }
+ /* make sure we don't exceed MAXPATHLEN */
+ if (strlen(path) >= sizeof buf)
+ return 0;
- if (mkdir(path, done ? mode : dir_mode) == 0) {
- if (mode > 0777 && chmod(path, mode) < 0)
- return (-1);
- } else {
- if (!exists) {
- /* Not there */
- return (-1);
- }
- if (!S_ISDIR(sb.st_mode)) {
- /* Is there, but isn't a directory */
- errno = ENOTDIR;
- return (-1);
- }
+ bzero(buf, sizeof buf);
+ for (p = path; *p; p++) {
+ if (*p == '/') {
+ if (buf[0] != '\0')
+ if (! mkdirs_component(buf, mode))
+ return 0;
+ while (*p == '/')
+ p++;
+ buf[i++] = '/';
+ buf[i++] = *p;
+ if (*p == '\0' && ++done)
+ break;
+ continue;
}
-
- if (done)
- break;
-
- *slash = '/';
+ buf[i++] = *p;
}
+ if (! done)
+ if (! mkdirs_component(buf, mode))
+ return 0;
+
+ if (chmod(path, mode) == -1)
+ return 0;
- return (0);
+ return 1;
}
+
int
ckdir(const char *path, mode_t mode, uid_t owner, gid_t group, int create)
{