dovecot: Simplified mailbox_list_get_permissions() a bit. Now it...

dovecot at dovecot.org dovecot at dovecot.org
Sun Nov 11 15:49:03 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/083681d2f4fb
changeset: 6770:083681d2f4fb
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Nov 11 15:48:59 2007 +0200
description:
Simplified mailbox_list_get_permissions() a bit. Now it returns only
permissions for the root directory. Also UID isn't returned anymore.

diffstat:

4 files changed, 51 insertions(+), 52 deletions(-)
src/lib-storage/mailbox-list-private.h      |    6 +-
src/lib-storage/mailbox-list.c              |   79 +++++++++++++--------------
src/lib-storage/mailbox-list.h              |   13 ++--
src/plugins/acl/acl-backend-vfile-acllist.c |    5 -

diffs (174 lines):

diff -r 3ac33cc68f24 -r 083681d2f4fb src/lib-storage/mailbox-list-private.h
--- a/src/lib-storage/mailbox-list-private.h	Sun Nov 11 02:40:52 2007 +0200
+++ b/src/lib-storage/mailbox-list-private.h	Sun Nov 11 15:48:59 2007 +0200
@@ -73,9 +73,9 @@ struct mailbox_list {
 	struct mailbox_list_settings set;
 	enum mailbox_list_flags flags;
 
-	/* -1 if unset: */
-	uid_t cached_uid;
-	gid_t cached_gid;
+	/* -1 if not set yet. use mailbox_list_get_permissions() to set them */
+	mode_t file_create_mode;
+	gid_t file_create_gid;
 
 	char *error_string;
 	enum mail_error error;
diff -r 3ac33cc68f24 -r 083681d2f4fb src/lib-storage/mailbox-list.c
--- a/src/lib-storage/mailbox-list.c	Sun Nov 11 02:40:52 2007 +0200
+++ b/src/lib-storage/mailbox-list.c	Sun Nov 11 15:48:59 2007 +0200
@@ -170,8 +170,8 @@ void mailbox_list_init(struct mailbox_li
 
 	list->ns = ns;
 	list->flags = flags;
-	list->cached_uid = (uid_t)-1;
-	list->cached_gid = (gid_t)-1;
+	list->file_create_mode = (mode_t)-1;
+	list->file_create_gid = (gid_t)-1;
 
 	/* copy settings */
 	list->set.root_dir = p_strdup(list->pool, set->root_dir);
@@ -246,6 +246,44 @@ struct mail_namespace *mailbox_list_get_
 	return list->ns;
 }
 
+void mailbox_list_get_permissions(struct mailbox_list *list,
+				  mode_t *mode_r, gid_t *gid_r)
+{
+	const char *path;
+	struct stat st;
+
+	if (list->file_create_mode != (mode_t)-1) {
+		*mode_r = list->file_create_mode;
+		*gid_r = list->file_create_gid;
+	}
+
+	path = mailbox_list_get_path(list, NULL, MAILBOX_LIST_PATH_TYPE_DIR);
+	if (stat(path, &st) < 0) {
+		if (!ENOTFOUND(errno)) {
+			mailbox_list_set_critical(list, "stat(%s) failed: %m",
+						  path);
+		}
+		/* return safe defaults */
+		*mode_r = 0600;
+		*gid_r = (gid_t)-1;
+		return;
+	}
+
+	list->file_create_mode = st.st_mode & 0666;
+	if (S_ISDIR(st.st_mode) && (st.st_mode & S_ISGID) != 0) {
+		/* directory's GID is used automatically for new files */
+		list->file_create_gid = (gid_t)-1;
+	} else if (getegid() == st.st_gid) {
+		/* using our own gid, no need to change it */
+		list->file_create_gid = (gid_t)-1;
+	} else {
+		list->file_create_gid = st.st_gid;
+	}
+
+	*mode_r = list->file_create_mode;
+	*gid_r = list->file_create_gid;
+}
+
 bool mailbox_list_is_valid_pattern(struct mailbox_list *list,
 				   const char *pattern)
 {
@@ -270,43 +308,6 @@ const char *mailbox_list_get_path(struct
 	mailbox_list_clear_error(list);
 
 	return list->v.get_path(list, name, type);
-}
-
-int mailbox_list_get_permissions(struct mailbox_list *list, const char *name,
-				 mode_t *mode_r, uid_t *uid_r, gid_t *gid_r)
-{
-	const char *path;
-	struct stat st;
-
-	mailbox_list_clear_error(list);
-
-	path = mailbox_list_get_path(list, name,
-				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	if (*path == '\0')
-		return -1;
-
-	if (stat(path, &st) < 0) {
-		if (ENOTFOUND(errno))
-			return 0;
-		mailbox_list_set_critical(list, "stat(%s) failed: %m", path);
-		return -1;
-	}
-
-	*mode_r = st.st_mode & 0666;
-
-	if (list->cached_uid == (uid_t)-1)
-		list->cached_uid = geteuid();
-	*uid_r = list->cached_uid == st.st_uid ? (uid_t)-1 : st.st_uid;
-
-	if (S_ISDIR(st.st_mode) && (st.st_mode & S_ISGID) != 0) {
-		/* directory's GID is used automatically for new files */
-		*gid_r = (gid_t)-1;
-	} else {
-		if (list->cached_gid == (gid_t)-1)
-			list->cached_gid = getegid();
-		*gid_r = list->cached_gid == st.st_gid ? (gid_t)-1 : st.st_gid;
-	}
-	return 1;
 }
 
 const char *mailbox_list_get_temp_prefix(struct mailbox_list *list)
diff -r 3ac33cc68f24 -r 083681d2f4fb src/lib-storage/mailbox-list.h
--- a/src/lib-storage/mailbox-list.h	Sun Nov 11 02:40:52 2007 +0200
+++ b/src/lib-storage/mailbox-list.h	Sun Nov 11 15:48:59 2007 +0200
@@ -133,6 +133,12 @@ enum mailbox_list_flags mailbox_list_get
 enum mailbox_list_flags mailbox_list_get_flags(struct mailbox_list *list);
 struct mail_namespace *mailbox_list_get_namespace(struct mailbox_list *list);
 
+/* Returns the mode and GID that should be used when creating new global files
+   to the mailbox list root directories. (gid_t)-1 is returned if it's not
+   necessary to change the default */
+void mailbox_list_get_permissions(struct mailbox_list *list,
+				  mode_t *mode_r, gid_t *gid_r);
+
 /* Returns TRUE if the name doesn't contain any invalid characters.
    The create name check can be more strict. */
 bool mailbox_list_is_valid_pattern(struct mailbox_list *list,
@@ -147,13 +153,6 @@ bool mailbox_list_is_valid_create_name(s
    For INDEX=MEMORY it returns "" as the path. */
 const char *mailbox_list_get_path(struct mailbox_list *list, const char *name,
 				  enum mailbox_list_path_type type);
-/* Returns the mode, UID and GID that should be used when creating new files
-   to the given mailbox. (uid_t)-1 and (gid_t)-1 is returned if it's not
-   necessary to change the default UID or GID. The name must be a valid
-   existing mailbox name, or NULL to get global permissions.
-   Returns -1 if error, 0 if mailbox not found, 1 if ok. */
-int mailbox_list_get_permissions(struct mailbox_list *list, const char *name,
-				 mode_t *mode_r, uid_t *uid_r, gid_t *gid_r);
 /* Returns mailbox name status */
 int mailbox_list_get_mailbox_name_status(struct mailbox_list *list,
 					 const char *name,
diff -r 3ac33cc68f24 -r 083681d2f4fb src/plugins/acl/acl-backend-vfile-acllist.c
--- a/src/plugins/acl/acl-backend-vfile-acllist.c	Sun Nov 11 02:40:52 2007 +0200
+++ b/src/plugins/acl/acl-backend-vfile-acllist.c	Sun Nov 11 15:48:59 2007 +0200
@@ -187,11 +187,10 @@ int acl_backend_vfile_acllist_rebuild(st
 	struct stat st;
 	string_t *path;
 	mode_t mode;
-	uid_t uid;
 	gid_t gid;
 	int fd, ret;
 
-	ret = mailbox_list_get_permissions(list, NULL, &mode, &uid, &gid);
+	ret = mailbox_list_get_permissions(list, NULL, &mode, &gid);
 	if (ret <= 0) {
 		/* Return success if the whole root directory was lost */
 		return ret;
@@ -205,7 +204,7 @@ int acl_backend_vfile_acllist_rebuild(st
 	/* Build it into a temporary file and rename() over. There's no need
 	   to use locking, because even if multiple processes are rebuilding
 	   the file at the same time the result should be the same. */
-	fd = safe_mkstemp(path, mode, uid, gid);
+	fd = safe_mkstemp(path, mode, (uid_t)-1, gid);
 	if (fd == -1)
 		return -1;
 	output = o_stream_create_fd_file(fd, 0, FALSE);


More information about the dovecot-cvs mailing list