Commit 76c8d777 76c8d7771f17841fe5ef476db786988233f04851 by Sergey Poznyakoff

Fix creation of Berkeley db files.

* include/mailutils/util.h (mu_file_mode_to_safety_criteria)
(mu_safety_criteria_to_file_mode): New protos.
* libmailutils/base/filesafety.c (safety_checker) <mode>: New member.
(file_safety_check_tab): Update.
(mu_file_mode_to_safety_criteria)
(mu_safety_criteria_to_file_mode): New functions.
* libmu_dbm/berkeley.c (do_bdb_open): Create file prior to locking it.
1 parent 9f726ee0
......@@ -203,6 +203,10 @@ int mu_file_safety_check (const char *filename, int mode,
int mu_file_safety_name_to_code (const char *name, int *pcode);
int mu_file_safety_name_to_error (const char *name, int *pcode);
int mu_file_safety_compose (int *res, const char *name, int defval);
int mu_file_mode_to_safety_criteria (int mode);
int mu_safety_criteria_to_file_mode (int crit);
#ifdef __cplusplus
}
......
......@@ -91,25 +91,26 @@ struct safety_checker
char *name; /* Symbolic name */
int flag; /* MU_FILE_SAFETY_ flag that enables this entry */
int err; /* Corresponding error code */
int mode; /* Corresponding file mode bit */
int cdir; /* True if the function needs dirst member */
int (*fun) (struct file_check_buffer *fb); /* Checker function */
};
static struct safety_checker file_safety_check_tab[] = {
{ "grdfil", MU_FILE_SAFETY_GROUP_READABLE, MU_ERR_PERM_GROUP_READABLE,
0, _check_grdfil },
0040, 0, _check_grdfil },
{ "ardfil", MU_FILE_SAFETY_WORLD_READABLE, MU_ERR_PERM_WORLD_READABLE,
0, _check_ardfil },
0004, 0, _check_ardfil },
{ "gwrfil", MU_FILE_SAFETY_GROUP_WRITABLE, MU_ERR_PERM_GROUP_WRITABLE,
0, _check_gwrfil },
0020, 0, _check_gwrfil },
{ "awrfil", MU_FILE_SAFETY_WORLD_WRITABLE, MU_ERR_PERM_WORLD_WRITABLE,
0, _check_awrfil },
0002, 0, _check_awrfil },
{ "linkwrdir", MU_FILE_SAFETY_LINKED_WRDIR, MU_ERR_PERM_LINKED_WRDIR,
1, _check_linkwrdir },
0, 1, _check_linkwrdir },
{ "gwrdir", MU_FILE_SAFETY_DIR_IWGRP, MU_ERR_PERM_DIR_IWGRP,
1, _check_gwrdir },
0, 1, _check_gwrdir },
{ "awrdir", MU_FILE_SAFETY_DIR_IWOTH, MU_ERR_PERM_DIR_IWOTH,
1, _check_awrdir },
0, 1, _check_awrdir },
{ 0 }
};
......@@ -252,3 +253,27 @@ mu_file_safety_check (const char *filename, int mode,
}
return errno;
}
int
mu_file_mode_to_safety_criteria (int mode)
{
int fl = 0;
struct safety_checker *pck;
for (pck = file_safety_check_tab; pck->name; pck++)
if (mode & pck->mode)
fl |= pck->flag;
return fl;
}
int
mu_safety_criteria_to_file_mode (int crit)
{
int mode = 0600;
struct safety_checker *pck;
for (pck = file_safety_check_tab; pck->name; pck++)
if (crit & pck->flag)
mode |= pck->mode;
return mode;
}
......
......@@ -20,6 +20,7 @@
#endif
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <mailutils/types.h>
#include <mailutils/dbm.h>
#include <mailutils/util.h>
......@@ -29,6 +30,10 @@
#include <mailutils/locker.h>
#include "mudbm.h"
#ifndef O_EXCL
# define O_EXCL 0
#endif
#if defined(WITH_BDB)
#include <db.h>
......@@ -66,7 +71,8 @@ do_bdb_open (mu_dbm_file_t mdb, int flags, int mode)
struct bdb_file *bdb_file = mdb->db_descr;
int f, rc, locker_flags;
enum mu_locker_mode locker_mode;
int tfd = -1;
switch (flags)
{
case MU_STREAM_CREAT:
......@@ -99,7 +105,20 @@ do_bdb_open (mu_dbm_file_t mdb, int flags, int mode)
if (rc)
return rc;
if (access (mdb->db_name, R_OK) && errno == ENOENT)
{
tfd = open (mdb->db_name, O_CREAT|O_RDONLY|O_EXCL,
mu_file_mode_to_safety_criteria (mdb->db_safety_flags));
if (tfd == -1)
{
mu_locker_destroy (&bdb_file->locker);
return errno;
}
}
rc = mu_locker_lock_mode (bdb_file->locker, locker_mode);
if (tfd != -1)
close (tfd);
switch (rc)
{
case 0:
......