[dovecot-cvs] dovecot/src/lib-storage/index/mbox mbox-list.c, 1.28, 1.29 mbox-storage.c, 1.108, 1.109 mbox-storage.h, 1.32, 1.33

cras at dovecot.org cras at dovecot.org
Wed Dec 29 21:10:30 EET 2004


Update of /var/lib/cvs/dovecot/src/lib-storage/index/mbox
In directory talvi:/tmp/cvs-serv29231/src/lib-storage/index/mbox

Modified Files:
	mbox-list.c mbox-storage.c mbox-storage.h 
Log Message:
Added mail_debug setting. Moved full_filesystem_access from global variable
to flag in mail_create*() functions.



Index: mbox-list.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-list.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -d -r1.28 -r1.29
--- mbox-list.c	11 Sep 2004 10:28:51 -0000	1.28
+++ mbox-list.c	29 Dec 2004 19:10:27 -0000	1.29
@@ -63,8 +63,8 @@
 static const char *
 mbox_get_path(struct index_storage *storage, const char *name)
 {
-	if (!full_filesystem_access || name == NULL ||
-	    (*name != '/' && *name != '~' && *name != '\0'))
+	if ((storage->storage.flags & MAIL_STORAGE_FLAG_FULL_FS_ACCESS) == 0 ||
+	    name == NULL || (*name != '/' && *name != '~' && *name != '\0'))
 		return t_strconcat(storage->dir, "/", name, NULL);
 	else
 		return home_expand(name);
@@ -116,8 +116,8 @@
 	mail_storage_clear_error(storage);
 
 	/* check that we're not trying to do any "../../" lists */
-	if (!mbox_is_valid_mask(ref) ||
-	    !mbox_is_valid_mask(mask)) {
+	if (!mbox_is_valid_mask(storage, ref) ||
+	    !mbox_is_valid_mask(storage, mask)) {
 		mail_storage_set_error(storage, "Invalid mask");
 		return &ctx->mailbox_ctx;
 	}

Index: mbox-storage.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-storage.c,v
retrieving revision 1.108
retrieving revision 1.109
diff -u -d -r1.108 -r1.109
--- mbox-storage.c	26 Dec 2004 09:12:45 -0000	1.108
+++ mbox-storage.c	29 Dec 2004 19:10:27 -0000	1.109
@@ -59,79 +59,166 @@
 	return TRUE;
 }
 
-static int mbox_autodetect(const char *data)
+static int mbox_is_file(const char *path, const char *name, int debug)
 {
-	const char *path;
 	struct stat st;
 
-	data = t_strcut(data, ':');
+	if (stat(path, &st) < 0) {
+		if (debug) {
+			i_info("mbox autodetect: %s: stat(%s) failed: %m",
+			       name, path);
+		}
+		return FALSE;
+	}
+	if (S_ISDIR(st.st_mode)) {
+		if (debug) {
+			i_info("mbox autodetect: %s: is a directory (%s)",
+			       name, path);
+		}
+		return FALSE;
+	}
+	if (access(path, R_OK|W_OK) < 0) {
+		if (debug) {
+			i_info("mbox autodetect: %s: no R/W access (%s)",
+			       name, path);
+		}
+		return FALSE;
+	}
 
-	/* Is it INBOX file? */
-	if (*data != '\0' && stat(data, &st) == 0 && !S_ISDIR(st.st_mode) &&
-	    access(data, R_OK|W_OK) == 0)
-		return TRUE;
+	if (debug)
+		i_info("mbox autodetect: %s: yes (%s)", name, path);
+	return TRUE;
+}
 
-	/* or directory for IMAP folders? */
-	path = t_strconcat(data, "/.imap", NULL);
-	if (stat(path, &st) == 0 && S_ISDIR(st.st_mode) &&
-	    access(path, R_OK|W_OK|X_OK) == 0)
-		return TRUE;
+static int mbox_is_dir(const char *path, const char *name, int debug)
+{
+	struct stat st;
 
-	path = t_strconcat(data, "/inbox", NULL);
-	if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode) &&
-	    access(path, R_OK|W_OK) == 0)
+	if (stat(path, &st) < 0) {
+		if (debug) {
+			i_info("mbox autodetect: %s: stat(%s) failed: %m",
+			       name, path);
+		}
+		return FALSE;
+	}
+	if (!S_ISDIR(st.st_mode)) {
+		if (debug) {
+			i_info("mbox autodetect: %s: is not a directory (%s)",
+			       name, path);
+		}
+		return FALSE;
+	}
+	if (access(path, R_OK|W_OK|X_OK) < 0) {
+		if (debug) {
+			i_info("mbox autodetect: %s: no R/W/X access (%s)",
+			       name, path);
+		}
+		return FALSE;
+	}
+
+	if (debug)
+		i_info("mbox autodetect: %s: yes (%s)", name, path);
+	return TRUE;
+}
+
+static int mbox_autodetect(const char *data, enum mail_storage_flags flags)
+{
+	int debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
+	const char *path;
+
+	path = t_strcut(data, ':');
+
+	if (debug) {
+		if (strchr(data, ':') != NULL) {
+			i_info("mbox autodetect: data=%s, splitting ':' -> %s",
+			       data, path);
+		} else {
+			i_info("mbox autodetect: data=%s", data);
+		}
+	}
+
+	if (*path != '\0' && mbox_is_file(path, "INBOX file", debug))
 		return TRUE;
 
-	path = t_strconcat(data, "/mbox", NULL);
-	if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode) &&
-	    access(path, R_OK|W_OK) == 0)
+	if (mbox_is_dir(t_strconcat(path, "/.imap", NULL), "has .imap/", debug))
+		return TRUE;
+	if (mbox_is_file(t_strconcat(path, "/inbox", NULL), "has inbox", debug))
+		return TRUE;
+	if (mbox_is_file(t_strconcat(path, "/mbox", NULL), "has mbox", debug))
 		return TRUE;
 
 	return FALSE;
 }
 
-static const char *get_root_dir(void)
+static const char *get_root_dir(enum mail_storage_flags flags)
 {
 	const char *home, *path;
-
-	if (mbox_autodetect(""))
-		return "/";
+	int debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 
 	home = getenv("HOME");
 	if (home != NULL) {
 		path = t_strconcat(home, "/mail", NULL);
-		if (access(path, R_OK|W_OK|X_OK) == 0)
+		if (access(path, R_OK|W_OK|X_OK) == 0) {
+			if (debug)
+				i_info("mbox: root exists (%s)", path);
 			return path;
+		}
+		if (debug)
+			i_info("mbox: root: access(%s, rwx) failed: %m", path);
 
 		path = t_strconcat(home, "/Mail", NULL);
-		if (access(path, R_OK|W_OK|X_OK) == 0)
+		if (access(path, R_OK|W_OK|X_OK) == 0) {
+			if (debug)
+				i_info("mbox: root exists (%s)", path);
 			return path;
+		}
+		if (debug)
+			i_info("mbox: root: access(%s, rwx) failed: %m", path);
 	}
 
+	if (debug)
+		i_info("mbox: checking if we are chrooted:");
+	if (mbox_autodetect("", flags))
+		return "/";
+
+	if (debug)
+		i_info("mbox: root directory not found");
+
 	return NULL;
 }
 
-static const char *get_inbox_file(const char *root_dir, int only_root)
+static const char *
+get_inbox_file(const char *root_dir, int only_root, int debug)
 {
 	const char *user, *path;
 
-	if (!only_root) {
-		user = getenv("USER");
-		if (user != NULL) {
-			path = t_strconcat("/var/mail/", user, NULL);
-			if (access(path, R_OK|W_OK) == 0)
-				return path;
+	if (!only_root && (user = getenv("USER")) != NULL) {
+		path = t_strconcat("/var/mail/", user, NULL);
+		if (access(path, R_OK|W_OK) == 0) {
+			if (debug)
+				i_info("mbox: INBOX exists (%s)", path);
+			return path;
+		}
+		if (debug)
+			i_info("mbox: INBOX: access(%s, rw) failed: %m", path);
 
-			path = t_strconcat("/var/spool/mail/", user, NULL);
-			if (access(path, R_OK|W_OK) == 0)
-				return path;
+		path = t_strconcat("/var/spool/mail/", user, NULL);
+		if (access(path, R_OK|W_OK) == 0) {
+			if (debug)
+				i_info("mbox: INBOX exists (%s)", path);
+			return path;
 		}
+		if (debug)
+			i_info("mbox: INBOX: access(%s, rw) failed: %m", path);
 	}
 
-	return t_strconcat(root_dir, "/inbox", NULL);
+	path = t_strconcat(root_dir, "/inbox", NULL);
+	if (debug)
+		i_info("mbox: INBOX defaulted to %s", path);
+	return path;
 }
 
-static const char *create_root_dir(void)
+static const char *create_root_dir(int debug)
 {
 	const char *home, *path;
 
@@ -148,11 +235,15 @@
 		return NULL;
 	}
 
+	if (debug)
+		i_info("mbox: root directory created: %s", path);
 	return path;
 }
 
-static struct mail_storage *mbox_create(const char *data, const char *user)
+static struct mail_storage *
+mbox_create(const char *data, const char *user, enum mail_storage_flags flags)
 {
+	int debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 	struct index_storage *storage;
 	const char *root_dir, *inbox_file, *index_dir, *p;
 	struct stat st;
@@ -165,21 +256,23 @@
 		/* we'll need to figure out the mail location ourself.
 		   it's root dir if we've already chroot()ed, otherwise
 		   either $HOME/mail or $HOME/Mail */
-		root_dir = get_root_dir();
+		root_dir = get_root_dir(flags);
 	} else {
 		/* <root folder> | <INBOX path>
 		   [:INBOX=<path>] [:INDEX=<dir>] */
+		if (debug)
+			i_info("mbox: data=%s", data);
 		p = strchr(data, ':');
 		if (p == NULL) {
 			if (stat(data, &st) < 0) {
-				i_error("Invalid mbox file %s: %m", data);
+				i_error("Invalid mbox path %s: %m", data);
 				return NULL;
 			}
 
 			if (S_ISDIR(st.st_mode))
 				root_dir = data;
 			else {
-				root_dir = get_root_dir();
+				root_dir = get_root_dir(flags);
 				inbox_file = data;
 			}
 		} else {
@@ -196,7 +289,7 @@
 	}
 
 	if (root_dir == NULL) {
-		root_dir = create_root_dir();
+		root_dir = create_root_dir(debug);
 		if (root_dir == NULL)
 			return NULL;
 	} else {
@@ -214,13 +307,19 @@
 	}
 
 	if (inbox_file == NULL)
-		inbox_file = get_inbox_file(root_dir, !autodetect);
+		inbox_file = get_inbox_file(root_dir, !autodetect, debug);
 
 	if (index_dir == NULL)
 		index_dir = root_dir;
 	else if (strcmp(index_dir, "MEMORY") == 0)
 		index_dir = NULL;
 
+	if (debug) {
+		i_info("mbox: root=%s, index=%s, inbox=%s",
+		       root_dir, index_dir == NULL ? "" : index_dir,
+		       inbox_file == NULL ? "" : inbox_file);
+	}
+
 	storage = i_new(struct index_storage, 1);
 	storage->storage = mbox_storage;
 
@@ -229,7 +328,7 @@
 	storage->index_dir = i_strdup(home_expand(index_dir));
 	storage->user = i_strdup(user);
 	storage->callbacks = i_new(struct mail_storage_callbacks, 1);
-	index_storage_init(storage);
+	index_storage_init(storage, flags);
 	return &storage->storage;
 }
 
@@ -247,12 +346,12 @@
 	i_free(storage);
 }
 
-int mbox_is_valid_mask(const char *mask)
+int mbox_is_valid_mask(struct mail_storage *storage, const char *mask)
 {
 	const char *p;
 	int newdir;
 
-	if (full_filesystem_access)
+	if ((storage->flags & MAIL_STORAGE_FLAG_FULL_FS_ACCESS) != 0)
 		return TRUE;
 
 	/* make sure it's not absolute path */
@@ -270,7 +369,8 @@
 	return TRUE;
 }
 
-static int mbox_is_valid_create_name(const char *name)
+static int mbox_is_valid_create_name(struct mail_storage *storage,
+				     const char *name)
 {
 	size_t len;
 
@@ -279,10 +379,11 @@
 	    strchr(name, '*') != NULL || strchr(name, '%') != NULL)
 		return FALSE;
 
-	return mbox_is_valid_mask(name);
+	return mbox_is_valid_mask(storage, name);
 }
 
-static int mbox_is_valid_existing_name(const char *name)
+static int mbox_is_valid_existing_name(struct mail_storage *storage,
+				       const char *name)
 {
 	size_t len;
 
@@ -290,7 +391,7 @@
 	if (name[0] == '\0' || name[len-1] == '/')
 		return FALSE;
 
-	return mbox_is_valid_mask(name);
+	return mbox_is_valid_mask(storage, name);
 }
 
 static const char *mbox_get_index_dir(struct index_storage *storage,
@@ -301,7 +402,8 @@
 	if (storage->index_dir == NULL)
 		return NULL;
 
-	if (full_filesystem_access && (*name == '/' || *name == '~')) {
+	if ((storage->storage.flags & MAIL_STORAGE_FLAG_FULL_FS_ACCESS) != 0 &&
+	    (*name == '/' || *name == '~')) {
 		name = home_expand(name);
 		p = strrchr(name, '/');
 		return t_strconcat(t_strdup_until(name, p),
@@ -361,7 +463,8 @@
 {
 	if (strcmp(name, "INBOX") == 0)
 		return storage->inbox_path;
-	if (full_filesystem_access && (*name == '/' || *name == '~'))
+	if ((storage->storage.flags & MAIL_STORAGE_FLAG_FULL_FS_ACCESS) != 0 &&
+	    (*name == '/' || *name == '~'))
 		return home_expand(name);
 	return t_strconcat(storage->dir, "/", name, NULL);
 }
@@ -449,7 +552,7 @@
 		return mbox_open(storage, "INBOX", flags);
 	}
 
-	if (!mbox_is_valid_existing_name(name)) {
+	if (!mbox_is_valid_existing_name(_storage, name)) {
 		mail_storage_set_error(_storage, "Invalid mailbox name");
 		return NULL;
 	}
@@ -490,7 +593,7 @@
 
 	mail_storage_clear_error(_storage);
 
-	if (!mbox_is_valid_create_name(name)) {
+	if (!mbox_is_valid_create_name(_storage, name)) {
 		mail_storage_set_error(_storage, "Invalid mailbox name");
 		return -1;
 	}
@@ -573,7 +676,7 @@
 		return -1;
 	}
 
-	if (!mbox_is_valid_existing_name(name)) {
+	if (!mbox_is_valid_existing_name(_storage, name)) {
 		mail_storage_set_error(_storage, "Invalid mailbox name");
 		return -1;
 	}
@@ -660,8 +763,8 @@
 
 	mail_storage_clear_error(_storage);
 
-	if (!mbox_is_valid_existing_name(oldname) ||
-	    !mbox_is_valid_create_name(newname)) {
+	if (!mbox_is_valid_existing_name(_storage, oldname) ||
+	    !mbox_is_valid_create_name(_storage, newname)) {
 		mail_storage_set_error(_storage, "Invalid mailbox name");
 		return -1;
 	}
@@ -757,7 +860,7 @@
 
 	mail_storage_clear_error(_storage);
 
-	if (!mbox_is_valid_existing_name(name)) {
+	if (!mbox_is_valid_existing_name(_storage, name)) {
 		*status = MAILBOX_NAME_INVALID;
 		return 0;
 	}
@@ -768,7 +871,7 @@
 		return 0;
 	}
 
-	if (!mbox_is_valid_create_name(name)) {
+	if (!mbox_is_valid_create_name(_storage, name)) {
 		*status = MAILBOX_NAME_INVALID;
 		return 0;
 	}
@@ -843,6 +946,7 @@
 	index_storage_get_last_error,
 
 	NULL,
+	0,
 	0
 };
 

Index: mbox-storage.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-storage/index/mbox/mbox-storage.h,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- mbox-storage.h	26 Dec 2004 09:12:45 -0000	1.32
+++ mbox-storage.h	29 Dec 2004 19:10:27 -0000	1.33
@@ -53,6 +53,6 @@
 int mbox_transaction_save_commit(struct mbox_save_context *ctx);
 void mbox_transaction_save_rollback(struct mbox_save_context *ctx);
 
-int mbox_is_valid_mask(const char *mask);
+int mbox_is_valid_mask(struct mail_storage *storage, const char *mask);
 
 #endif



More information about the dovecot-cvs mailing list