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); ...@@ -200,6 +200,7 @@ int mu_onexit (mu_onexit_t func, void *data);
200 int mu_file_safety_check (const char *filename, int mode, 200 int mu_file_safety_check (const char *filename, int mode,
201 uid_t uid, 201 uid_t uid,
202 mu_list_t idlist); 202 mu_list_t idlist);
203 const char *mu_file_safety_code_to_name (int code);
203 int mu_file_safety_name_to_code (const char *name, int *pcode); 204 int mu_file_safety_name_to_code (const char *name, int *pcode);
204 int mu_file_safety_name_to_error (const char *name, int *pcode); 205 int mu_file_safety_name_to_error (const char *name, int *pcode);
205 int mu_file_safety_compose (int *res, const char *name, int defval); 206 int mu_file_safety_compose (int *res, const char *name, int defval);
......
...@@ -42,30 +42,6 @@ struct file_check_buffer ...@@ -42,30 +42,6 @@ struct file_check_buffer
42 }; 42 };
43 43
44 static int 44 static int
45 _check_grdfil (struct file_check_buffer *fb)
46 {
47 return fb->filst.st_mode & S_IRGRP;
48 }
49
50 static int
51 _check_ardfil (struct file_check_buffer *fb)
52 {
53 return fb->filst.st_mode & S_IROTH;
54 }
55
56 static int
57 _check_gwrfil (struct file_check_buffer *fb)
58 {
59 return fb->filst.st_mode & S_IWGRP;
60 }
61
62 static int
63 _check_awrfil (struct file_check_buffer *fb)
64 {
65 return fb->filst.st_mode & S_IWOTH;
66 }
67
68 static int
69 _check_linkwrdir (struct file_check_buffer *fb) 45 _check_linkwrdir (struct file_check_buffer *fb)
70 { 46 {
71 return ((((fb->filst.st_mode & S_IFMT) == S_IFLNK) || 47 return ((((fb->filst.st_mode & S_IFMT) == S_IFLNK) ||
...@@ -98,13 +74,13 @@ struct safety_checker ...@@ -98,13 +74,13 @@ struct safety_checker
98 74
99 static struct safety_checker file_safety_check_tab[] = { 75 static struct safety_checker file_safety_check_tab[] = {
100 { "grdfil", MU_FILE_SAFETY_GROUP_READABLE, MU_ERR_PERM_GROUP_READABLE, 76 { "grdfil", MU_FILE_SAFETY_GROUP_READABLE, MU_ERR_PERM_GROUP_READABLE,
101 0040, 0, _check_grdfil }, 77 S_IRGRP },
102 { "ardfil", MU_FILE_SAFETY_WORLD_READABLE, MU_ERR_PERM_WORLD_READABLE, 78 { "ardfil", MU_FILE_SAFETY_WORLD_READABLE, MU_ERR_PERM_WORLD_READABLE,
103 0004, 0, _check_ardfil }, 79 S_IROTH },
104 { "gwrfil", MU_FILE_SAFETY_GROUP_WRITABLE, MU_ERR_PERM_GROUP_WRITABLE, 80 { "gwrfil", MU_FILE_SAFETY_GROUP_WRITABLE, MU_ERR_PERM_GROUP_WRITABLE,
105 0020, 0, _check_gwrfil }, 81 S_IWGRP },
106 { "awrfil", MU_FILE_SAFETY_WORLD_WRITABLE, MU_ERR_PERM_WORLD_WRITABLE, 82 { "awrfil", MU_FILE_SAFETY_WORLD_WRITABLE, MU_ERR_PERM_WORLD_WRITABLE,
107 0002, 0, _check_awrfil }, 83 S_IWOTH },
108 { "linkwrdir", MU_FILE_SAFETY_LINKED_WRDIR, MU_ERR_PERM_LINKED_WRDIR, 84 { "linkwrdir", MU_FILE_SAFETY_LINKED_WRDIR, MU_ERR_PERM_LINKED_WRDIR,
109 0, 1, _check_linkwrdir }, 85 0, 1, _check_linkwrdir },
110 { "gwrdir", MU_FILE_SAFETY_DIR_IWGRP, MU_ERR_PERM_DIR_IWGRP, 86 { "gwrdir", MU_FILE_SAFETY_DIR_IWGRP, MU_ERR_PERM_DIR_IWGRP,
...@@ -167,6 +143,18 @@ _find_safety_checker (const char *name) ...@@ -167,6 +143,18 @@ _find_safety_checker (const char *name)
167 return NULL; 143 return NULL;
168 } 144 }
169 145
146 const char *
147 mu_file_safety_code_to_name (int code)
148 {
149 struct safety_checker *pck;
150 for (pck = file_safety_check_tab; pck->flag; pck++)
151 {
152 if (pck->flag == code)
153 return pck->name;
154 }
155 return NULL;
156 }
157
170 int 158 int
171 mu_file_safety_name_to_code (const char *name, int *pcode) 159 mu_file_safety_name_to_code (const char *name, int *pcode)
172 { 160 {
...@@ -244,7 +232,8 @@ mu_file_safety_check (const char *filename, int mode, ...@@ -244,7 +232,8 @@ mu_file_safety_check (const char *filename, int mode,
244 return errno; 232 return errno;
245 buf.cdir = 1; 233 buf.cdir = 1;
246 } 234 }
247 if (pck->fun (&buf)) 235 if ((pck->fun && pck->fun (&buf)) ||
236 (buf.filst.st_mode & pck->mode))
248 return pck->err; 237 return pck->err;
249 } 238 }
250 if (idlist) 239 if (idlist)
...@@ -261,7 +250,7 @@ mu_file_mode_to_safety_criteria (int mode) ...@@ -261,7 +250,7 @@ mu_file_mode_to_safety_criteria (int mode)
261 struct safety_checker *pck; 250 struct safety_checker *pck;
262 251
263 for (pck = file_safety_check_tab; pck->name; pck++) 252 for (pck = file_safety_check_tab; pck->name; pck++)
264 if (mode & pck->mode) 253 if (!(mode & pck->mode))
265 fl |= pck->flag; 254 fl |= pck->flag;
266 return fl; 255 return fl;
267 } 256 }
...@@ -269,11 +258,11 @@ mu_file_mode_to_safety_criteria (int mode) ...@@ -269,11 +258,11 @@ mu_file_mode_to_safety_criteria (int mode)
269 int 258 int
270 mu_safety_criteria_to_file_mode (int crit) 259 mu_safety_criteria_to_file_mode (int crit)
271 { 260 {
272 int mode = 0600; 261 int mode = 0666;
273 struct safety_checker *pck; 262 struct safety_checker *pck;
274 263
275 for (pck = file_safety_check_tab; pck->name; pck++) 264 for (pck = file_safety_check_tab; pck->name; pck++)
276 if (crit & pck->flag) 265 if (pck->flag && (crit & pck->flag))
277 mode |= pck->mode; 266 mode &= ~pck->mode;
278 return mode; 267 return mode;
279 } 268 }
......
...@@ -12,11 +12,13 @@ decode2047 ...@@ -12,11 +12,13 @@ decode2047
12 encode2047 12 encode2047
13 fltst 13 fltst
14 fsaf 14 fsaf
15 fsaftomod
15 fsfolder 16 fsfolder
16 imapio 17 imapio
17 listop 18 listop
18 mailcap 19 mailcap
19 mimehdr 20 mimehdr
21 modtofsaf
20 prop 22 prop
21 scantime 23 scantime
22 strftime 24 strftime
......
...@@ -30,7 +30,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac ...@@ -30,7 +30,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
30 echo 'm4_define([AT_PACKAGE_VERSION], [@PACKAGE_VERSION@])'; \ 30 echo 'm4_define([AT_PACKAGE_VERSION], [@PACKAGE_VERSION@])'; \
31 echo 'm4_define([AT_PACKAGE_STRING], [@PACKAGE_STRING@])'; \ 31 echo 'm4_define([AT_PACKAGE_STRING], [@PACKAGE_STRING@])'; \
32 echo 'm4_define([AT_PACKAGE_BUGREPORT], [@PACKAGE_BUGREPORT@])'; \ 32 echo 'm4_define([AT_PACKAGE_BUGREPORT], [@PACKAGE_BUGREPORT@])'; \
33 } >$(srcdir)/package.m4 33 } >$(srcdir)/package.m4
34 34
35 # 35 #
36 36
...@@ -47,11 +47,13 @@ noinst_PROGRAMS = \ ...@@ -47,11 +47,13 @@ noinst_PROGRAMS = \
47 encode2047\ 47 encode2047\
48 fltst\ 48 fltst\
49 fsaf\ 49 fsaf\
50 fsaftomod\
50 fsfolder\ 51 fsfolder\
51 imapio\ 52 imapio\
52 listop\ 53 listop\
53 mailcap\ 54 mailcap\
54 mimehdr\ 55 mimehdr\
56 modtofsaf\
55 msgset\ 57 msgset\
56 prop\ 58 prop\
57 scantime\ 59 scantime\
...@@ -81,6 +83,7 @@ TESTSUITE_AT = \ ...@@ -81,6 +83,7 @@ TESTSUITE_AT = \
81 encode2047.at\ 83 encode2047.at\
82 fromflt.at\ 84 fromflt.at\
83 fsaf.at\ 85 fsaf.at\
86 fsaftomod.at\
84 fsfolder00.at\ 87 fsfolder00.at\
85 fsfolder01.at\ 88 fsfolder01.at\
86 fsfolder02.at\ 89 fsfolder02.at\
...@@ -91,6 +94,7 @@ TESTSUITE_AT = \ ...@@ -91,6 +94,7 @@ TESTSUITE_AT = \
91 list.at\ 94 list.at\
92 mailcap.at\ 95 mailcap.at\
93 mimehdr.at\ 96 mimehdr.at\
97 modtofsaf.at\
94 msgset.at\ 98 msgset.at\
95 prop.at\ 99 prop.at\
96 scantime.at\ 100 scantime.at\
......
1 # This file is part of GNU Mailutils. -*- Autotest -*-
2 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 #
4 # GNU Mailutils is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3, or (at
7 # your option) any later version.
8 #
9 # GNU Mailutils is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER(File Safety to File Mode)
18
19 dnl ------------------------------------------------------------
20 dnl FSAFTOMODE(CRITERIA, MODE)
21 dnl
22
23 m4_pushdef([FSAFTOMODE],[
24 AT_SETUP([$1])
25 AT_KEYWORDS([fsaf fsaftomod fsaftomode])
26 AT_CHECK([
27 fsaftomod $1
28 ],
29 [0],
30 [$2
31 ])
32 AT_CLEANUP
33 ])
34
35 dnl ------------------------------------------------------------
36 FSAFTOMODE(all, 600)
37 FSAFTOMODE(none, 666)
38 FSAFTOMODE([grdfil ardfil], 622)
39 FSAFTOMODE([grdfil ardfil awrfil], 620)
40 FSAFTOMODE([grdfil gwrfil ardfil awrfil], 600)
41
42 dnl ------------------------------------------------------------
43 m4_popdef([FSAFTOMODE])
...\ No newline at end of file ...\ No newline at end of file
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <mailutils/types.h>
24 #include <mailutils/error.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/util.h>
27
28 #define SAFDEFAULT \
29 (MU_FILE_SAFETY_GROUP_WRITABLE | \
30 MU_FILE_SAFETY_WORLD_WRITABLE | \
31 MU_FILE_SAFETY_GROUP_READABLE | \
32 MU_FILE_SAFETY_WORLD_READABLE | \
33 MU_FILE_SAFETY_LINKED_WRDIR | \
34 MU_FILE_SAFETY_DIR_IWGRP | \
35 MU_FILE_SAFETY_DIR_IWOTH )
36
37 int
38 main (int argc, char **argv)
39 {
40 int i;
41 int input = 0;
42
43 if (argc == 1)
44 {
45 mu_error ("usage: %s [+-]mode...", argv[0]);
46 exit (1);
47 }
48
49 for (i = 1; i < argc; i++)
50 {
51 int rc = mu_file_safety_compose (&input, argv[i], SAFDEFAULT);
52 if (rc)
53 {
54 mu_error ("%s: %s", argv[i], mu_strerror (rc));
55 exit (1);
56 }
57 }
58 printf ("%03o\n", mu_safety_criteria_to_file_mode (input));
59 return 0;
60 }
1 # This file is part of GNU Mailutils. -*- Autotest -*-
2 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 #
4 # GNU Mailutils is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3, or (at
7 # your option) any later version.
8 #
9 # GNU Mailutils is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER(File Mode to File Safety)
18
19 dnl ------------------------------------------------------------
20 dnl MODETOFSAF(MODE, CRITERIA)
21 dnl
22
23 m4_pushdef([MODETOFSAF],[
24 AT_SETUP([$1])
25 AT_KEYWORDS([fsaf modtofsaf modetofsaf])
26 AT_CHECK([
27 modtofsaf $1
28 ],
29 [0],
30 [$2])
31 AT_CLEANUP
32 ])
33
34 dnl ------------------------------------------------------------
35 MODETOFSAF(600,[gwrfil
36 awrfil
37 grdfil
38 ardfil
39 ])
40
41 MODETOFSAF(666)
42
43 MODETOFSAF(622,[grdfil
44 ardfil
45 ])
46
47 MODETOFSAF(644,[gwrfil
48 awrfil
49 ])
50
51 MODETOFSAF(620,[awrfil
52 grdfil
53 ardfil
54 ])
55
56 dnl ------------------------------------------------------------
57 m4_popdef([MODETOFSAF])
...\ No newline at end of file ...\ No newline at end of file
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <mailutils/types.h>
24 #include <mailutils/error.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/util.h>
27
28 #define SAFMASK ( \
29 MU_FILE_SAFETY_GROUP_WRITABLE | \
30 MU_FILE_SAFETY_WORLD_WRITABLE | \
31 MU_FILE_SAFETY_GROUP_READABLE | \
32 MU_FILE_SAFETY_WORLD_READABLE )
33
34 int
35 main (int argc, char **argv)
36 {
37 int i, crit;
38
39 if (argc != 2)
40 {
41 mu_error ("usage: %s mode", argv[0]);
42 exit (1);
43 }
44
45 crit = mu_file_mode_to_safety_criteria (strtoul (argv[1], NULL, 8)) &
46 SAFMASK;
47 for (i = 1; crit && i != 0; i <<= 1)
48 {
49 if (crit & i)
50 {
51 const char *s = mu_file_safety_code_to_name (i);
52 printf ("%s\n", s ? s : "UNKNOWN");
53 }
54 crit &= ~i;
55 }
56 return 0;
57 }
...@@ -96,6 +96,8 @@ m4_include([scantime.at]) ...@@ -96,6 +96,8 @@ m4_include([scantime.at])
96 m4_include([strftime.at]) 96 m4_include([strftime.at])
97 97
98 m4_include([fsaf.at]) 98 m4_include([fsaf.at])
99 m4_include([fsaftomod.at])
100 m4_include([modtofsaf.at])
99 101
100 m4_include([mimehdr.at]) 102 m4_include([mimehdr.at])
101 103
......