Commit 5eb34056 5eb340568f0e025fd0ceec4b1f13efceab093739 by Sergey Poznyakoff

Improve conversion between file mode and safety criteria and vice versa.

* include/mailutils/util.h (mu_file_safety_code_to_name): New proto.
* libmailutils/base/filesafety.c: Remove static functions which
checked file mode bits.  Use safety_checker.mode for that directly.
(mu_file_safety_code_to_name): New function.
(mu_file_mode_to_safety_criteria): Bugfix.
(mu_safety_criteria_to_file_mode): Change algorithm.
* libmailutils/tests/.gitignore: Update
* libmailutils/tests/fsaftomod.at: New file.
* libmailutils/tests/fsaftomod.c: New file.
* libmailutils/tests/modtofsaf.at: New file.
* libmailutils/tests/modtofsaf.c: New file.
* libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add
fsaftomod and modtofsaf.
(TESTSUITE_AT): Add fsaftomod.at and modtofsaf.at
* libmailutils/tests/testsuite.at: Include fsaftomod.at and
modtofsaf.at
1 parent b84a8723
......@@ -200,6 +200,7 @@ int mu_onexit (mu_onexit_t func, void *data);
int mu_file_safety_check (const char *filename, int mode,
uid_t uid,
mu_list_t idlist);
const char *mu_file_safety_code_to_name (int code);
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);
......
......@@ -42,30 +42,6 @@ struct file_check_buffer
};
static int
_check_grdfil (struct file_check_buffer *fb)
{
return fb->filst.st_mode & S_IRGRP;
}
static int
_check_ardfil (struct file_check_buffer *fb)
{
return fb->filst.st_mode & S_IROTH;
}
static int
_check_gwrfil (struct file_check_buffer *fb)
{
return fb->filst.st_mode & S_IWGRP;
}
static int
_check_awrfil (struct file_check_buffer *fb)
{
return fb->filst.st_mode & S_IWOTH;
}
static int
_check_linkwrdir (struct file_check_buffer *fb)
{
return ((((fb->filst.st_mode & S_IFMT) == S_IFLNK) ||
......@@ -98,13 +74,13 @@ struct safety_checker
static struct safety_checker file_safety_check_tab[] = {
{ "grdfil", MU_FILE_SAFETY_GROUP_READABLE, MU_ERR_PERM_GROUP_READABLE,
0040, 0, _check_grdfil },
S_IRGRP },
{ "ardfil", MU_FILE_SAFETY_WORLD_READABLE, MU_ERR_PERM_WORLD_READABLE,
0004, 0, _check_ardfil },
S_IROTH },
{ "gwrfil", MU_FILE_SAFETY_GROUP_WRITABLE, MU_ERR_PERM_GROUP_WRITABLE,
0020, 0, _check_gwrfil },
S_IWGRP },
{ "awrfil", MU_FILE_SAFETY_WORLD_WRITABLE, MU_ERR_PERM_WORLD_WRITABLE,
0002, 0, _check_awrfil },
S_IWOTH },
{ "linkwrdir", MU_FILE_SAFETY_LINKED_WRDIR, MU_ERR_PERM_LINKED_WRDIR,
0, 1, _check_linkwrdir },
{ "gwrdir", MU_FILE_SAFETY_DIR_IWGRP, MU_ERR_PERM_DIR_IWGRP,
......@@ -167,6 +143,18 @@ _find_safety_checker (const char *name)
return NULL;
}
const char *
mu_file_safety_code_to_name (int code)
{
struct safety_checker *pck;
for (pck = file_safety_check_tab; pck->flag; pck++)
{
if (pck->flag == code)
return pck->name;
}
return NULL;
}
int
mu_file_safety_name_to_code (const char *name, int *pcode)
{
......@@ -244,7 +232,8 @@ mu_file_safety_check (const char *filename, int mode,
return errno;
buf.cdir = 1;
}
if (pck->fun (&buf))
if ((pck->fun && pck->fun (&buf)) ||
(buf.filst.st_mode & pck->mode))
return pck->err;
}
if (idlist)
......@@ -261,7 +250,7 @@ mu_file_mode_to_safety_criteria (int mode)
struct safety_checker *pck;
for (pck = file_safety_check_tab; pck->name; pck++)
if (mode & pck->mode)
if (!(mode & pck->mode))
fl |= pck->flag;
return fl;
}
......@@ -269,11 +258,11 @@ mu_file_mode_to_safety_criteria (int mode)
int
mu_safety_criteria_to_file_mode (int crit)
{
int mode = 0600;
int mode = 0666;
struct safety_checker *pck;
for (pck = file_safety_check_tab; pck->name; pck++)
if (crit & pck->flag)
mode |= pck->mode;
if (pck->flag && (crit & pck->flag))
mode &= ~pck->mode;
return mode;
}
......
......@@ -12,11 +12,13 @@ decode2047
encode2047
fltst
fsaf
fsaftomod
fsfolder
imapio
listop
mailcap
mimehdr
modtofsaf
prop
scantime
strftime
......
......@@ -47,11 +47,13 @@ noinst_PROGRAMS = \
encode2047\
fltst\
fsaf\
fsaftomod\
fsfolder\
imapio\
listop\
mailcap\
mimehdr\
modtofsaf\
msgset\
prop\
scantime\
......@@ -81,6 +83,7 @@ TESTSUITE_AT = \
encode2047.at\
fromflt.at\
fsaf.at\
fsaftomod.at\
fsfolder00.at\
fsfolder01.at\
fsfolder02.at\
......@@ -91,6 +94,7 @@ TESTSUITE_AT = \
list.at\
mailcap.at\
mimehdr.at\
modtofsaf.at\
msgset.at\
prop.at\
scantime.at\
......
# This file is part of GNU Mailutils. -*- Autotest -*-
# Copyright (C) 2011-2012 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# GNU Mailutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
AT_BANNER(File Safety to File Mode)
dnl ------------------------------------------------------------
dnl FSAFTOMODE(CRITERIA, MODE)
dnl
m4_pushdef([FSAFTOMODE],[
AT_SETUP([$1])
AT_KEYWORDS([fsaf fsaftomod fsaftomode])
AT_CHECK([
fsaftomod $1
],
[0],
[$2
])
AT_CLEANUP
])
dnl ------------------------------------------------------------
FSAFTOMODE(all, 600)
FSAFTOMODE(none, 666)
FSAFTOMODE([grdfil ardfil], 622)
FSAFTOMODE([grdfil ardfil awrfil], 620)
FSAFTOMODE([grdfil gwrfil ardfil awrfil], 600)
dnl ------------------------------------------------------------
m4_popdef([FSAFTOMODE])
\ No newline at end of file
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#define SAFDEFAULT \
(MU_FILE_SAFETY_GROUP_WRITABLE | \
MU_FILE_SAFETY_WORLD_WRITABLE | \
MU_FILE_SAFETY_GROUP_READABLE | \
MU_FILE_SAFETY_WORLD_READABLE | \
MU_FILE_SAFETY_LINKED_WRDIR | \
MU_FILE_SAFETY_DIR_IWGRP | \
MU_FILE_SAFETY_DIR_IWOTH )
int
main (int argc, char **argv)
{
int i;
int input = 0;
if (argc == 1)
{
mu_error ("usage: %s [+-]mode...", argv[0]);
exit (1);
}
for (i = 1; i < argc; i++)
{
int rc = mu_file_safety_compose (&input, argv[i], SAFDEFAULT);
if (rc)
{
mu_error ("%s: %s", argv[i], mu_strerror (rc));
exit (1);
}
}
printf ("%03o\n", mu_safety_criteria_to_file_mode (input));
return 0;
}
# This file is part of GNU Mailutils. -*- Autotest -*-
# Copyright (C) 2011-2012 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# GNU Mailutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
AT_BANNER(File Mode to File Safety)
dnl ------------------------------------------------------------
dnl MODETOFSAF(MODE, CRITERIA)
dnl
m4_pushdef([MODETOFSAF],[
AT_SETUP([$1])
AT_KEYWORDS([fsaf modtofsaf modetofsaf])
AT_CHECK([
modtofsaf $1
],
[0],
[$2])
AT_CLEANUP
])
dnl ------------------------------------------------------------
MODETOFSAF(600,[gwrfil
awrfil
grdfil
ardfil
])
MODETOFSAF(666)
MODETOFSAF(622,[grdfil
ardfil
])
MODETOFSAF(644,[gwrfil
awrfil
])
MODETOFSAF(620,[awrfil
grdfil
ardfil
])
dnl ------------------------------------------------------------
m4_popdef([MODETOFSAF])
\ No newline at end of file
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#define SAFMASK ( \
MU_FILE_SAFETY_GROUP_WRITABLE | \
MU_FILE_SAFETY_WORLD_WRITABLE | \
MU_FILE_SAFETY_GROUP_READABLE | \
MU_FILE_SAFETY_WORLD_READABLE )
int
main (int argc, char **argv)
{
int i, crit;
if (argc != 2)
{
mu_error ("usage: %s mode", argv[0]);
exit (1);
}
crit = mu_file_mode_to_safety_criteria (strtoul (argv[1], NULL, 8)) &
SAFMASK;
for (i = 1; crit && i != 0; i <<= 1)
{
if (crit & i)
{
const char *s = mu_file_safety_code_to_name (i);
printf ("%s\n", s ? s : "UNKNOWN");
}
crit &= ~i;
}
return 0;
}
......@@ -96,6 +96,8 @@ m4_include([scantime.at])
m4_include([strftime.at])
m4_include([fsaf.at])
m4_include([fsaftomod.at])
m4_include([modtofsaf.at])
m4_include([mimehdr.at])
......