[dovecot-cvs] dovecot/src/lib safe-mkdir.c,NONE,1.1 safe-mkdir.h,NONE,1.1 Makefile.am,1.21,1.22 unlink-directory.c,1.4,1.5 unlink-directory.h,1.1.1.1,1.2

cras at procontrol.fi cras at procontrol.fi
Sat Dec 21 14:14:00 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv29177/src/lib

Modified Files:
	Makefile.am unlink-directory.c unlink-directory.h 
Added Files:
	safe-mkdir.c safe-mkdir.h 
Log Message:
If used base/login directories exist already, make sure they're with correct
permissions. Always delete contents in login directory at startup.

Added safe_mkdir(). changed unlink_directory() behaviour, and fixed a race
condition which could have made it follow symlinks to other directories and
delete them. Didn't really harm us so far, we never used it for deleting
unsafe directories.



--- NEW FILE: safe-mkdir.c ---
/*
    Copyright (c) 2002 Timo Sirainen

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "lib.h"
#include "safe-mkdir.h"

#include <sys/stat.h>
#include <unistd.h>

int safe_mkdir(const char *dir, mode_t mode, uid_t uid, gid_t gid)
{
	struct stat st;
	int ret = 1;

	if (lstat(dir, &st) < 0) {
		if (errno != ENOENT)
			i_fatal("lstat() failed for %s: %m", dir);

		if (mkdir(dir, mode) < 0)
			i_fatal("Can't create directory %s: %m", dir);

		if (lchown(dir, uid, gid) < 0)
			i_fatal("lchown() failed for %s: %m", dir);
	} else {
		/* make sure it's permissions are correct */
		if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
			i_fatal("Not a directory %s", dir);

		if (st.st_uid != uid || st.st_gid != gid) {
			if (lchown(dir, uid, gid) < 0)
				i_fatal("lchown() failed for %s: %m", dir);
			ret = 0;
		}

		if (st.st_mode != mode) {
			if (chmod(dir, mode) < 0)
				i_fatal("chmod() failed for %s: %m", dir);
			ret = 0;
		}
	}

	/* make sure we succeeded in everything. chown() and chmod()
	   are racy: user owned 0777 file - change either and the user
	   can still change it back. */
	if (lstat(dir, &st) < 0)
		i_fatal("lstat() check failed for %s: %m", dir);

	if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) {
		i_fatal("safe_mkdir() failed: %s is still not a directory",
			dir);
	}

	if (st.st_mode != mode) {
		i_fatal("safe_mkdir() failed: %s (%o) is still not mode %o",
			dir, (int)st.st_mode, (int)mode);
	}
	if (st.st_uid != uid || st.st_gid != gid) {
		i_fatal("safe_mkdir() failed: %s (%s, %s) "
			"is still not owned by %s.%s",
			dir, dec2str(st.st_uid), dec2str(st.st_gid),
			dec2str(uid), dec2str(gid));
	}

	return ret;
}

--- NEW FILE: safe-mkdir.h ---
#ifndef __SAFE_MKDIR
#define __SAFE_MKDIR

/* Either create a directory or make sure that it already exists with given
   permissions. If anything fails, the i_fatal() is called. Returns 1 if
   directory was created, 2 if it already existed with correct permissions,
   0 if we changed permissions. */
int safe_mkdir(const char *dir, mode_t mode, uid_t uid, gid_t gid);

#endif

Index: Makefile.am
===================================================================
RCS file: /home/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- Makefile.am	18 Dec 2002 10:40:43 -0000	1.21
+++ Makefile.am	21 Dec 2002 12:13:58 -0000	1.22
@@ -42,6 +42,7 @@
 	restrict-access.c \
 	restrict-process-size.c \
 	safe-memset.c \
+	safe-mkdir.c \
 	sendfile-util.c \
 	strfuncs.c \
 	temp-string.c \
@@ -87,6 +88,7 @@
 	restrict-access.h \
 	restrict-process-size.h \
 	safe-memset.h \
+	safe-mkdir.h \
 	sendfile-util.h \
 	strfuncs.h \
 	temp-string.h \

Index: unlink-directory.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/unlink-directory.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- unlink-directory.c	20 Dec 2002 07:53:52 -0000	1.4
+++ unlink-directory.c	21 Dec 2002 12:13:58 -0000	1.5
@@ -30,7 +30,7 @@
 #include <dirent.h>
 #include <sys/stat.h>
 
-int unlink_directory(const char *dir)
+int unlink_directory(const char *dir, int unlink_dir)
 {
 	DIR *dirp;
 	struct dirent *d;
@@ -39,7 +39,7 @@
 
 	dirp = opendir(dir);
 	if (dirp == NULL)
-		return errno == ENOENT;
+		return errno == ENOENT ? 0 : -1;
 
 	while ((d = readdir(dirp)) != NULL) {
 		if (d->d_name[0] == '.' &&
@@ -50,26 +50,30 @@
 		}
 
 		if (str_path(path, sizeof(path), dir, d->d_name) < 0)
-			return FALSE;
+			return -1;
 
 		if (unlink(path) == -1 && errno != ENOENT) {
 			int old_errno = errno;
 
-			if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
-				if (!unlink_directory(path))
-					return FALSE;
+			if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+				if (unlink_directory(path, TRUE) < 0)
+					return -1;
 			} else {
 				/* so it wasn't a directory, unlink() again
 				   to get correct errno */
 				errno = old_errno;
-				return FALSE;
+				return -1;
 			}
 		}
 	}
 
-	(void)closedir(dirp);
+	if (closedir(dirp) < 0)
+		return -1;
 
-	if (rmdir(dir) == -1 && errno != ENOENT)
-		return FALSE;
-	return TRUE;
+	if (unlink_dir) {
+		if (rmdir(dir) == -1 && errno != ENOENT)
+			return -1;
+	}
+
+	return 0;
 }

Index: unlink-directory.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/unlink-directory.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- unlink-directory.h	9 Aug 2002 09:15:50 -0000	1.1.1.1
+++ unlink-directory.h	21 Dec 2002 12:13:58 -0000	1.2
@@ -1,7 +1,8 @@
 #ifndef __UNLINK_DIRECTORY_H
 #define __UNLINK_DIRECTORY_H
 
-/* Unlink directory with everything under it. Returns TRUE if successful. */
-int unlink_directory(const char *dir);
+/* Unlink directory and/or everything under it.
+   Returns 0 if successful, -1 if error. */
+int unlink_directory(const char *dir, int unlink_dir);
 
 #endif




More information about the dovecot-cvs mailing list