03458fcef01671f4ea4b263873ff89ebe86a7a2b
[pspp] / src / language / utilities / permissions.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2010 Free Software Foundation, Inc.
3
4    This program 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 of the License, or
7    (at your option) any later version.
8
9    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <libpspp/message.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <data/settings.h>
25 #include <language/command.h>
26 #include <libpspp/message.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/misc.h>
29 #include <libpspp/str.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 enum PER {PER_RO, PER_RW};
35
36 int change_permissions(const char *file_name, enum PER per);
37
38
39 /* Parses the PERMISSIONS command. */
40 int
41 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
42 {
43   char  *fn = 0;
44
45   lex_match (lexer, '/');
46
47   if (lex_match_id (lexer, "FILE"))
48     lex_match (lexer, '=');
49
50   fn = ds_xstrdup (lex_tokstr (lexer));
51   lex_force_match (lexer, T_STRING);
52
53
54   lex_match (lexer, '/');
55
56   if ( ! lex_match_id (lexer, "PERMISSIONS"))
57     goto error;
58
59   lex_match (lexer, '=');
60
61   if ( lex_match_id (lexer, "READONLY"))
62     {
63       if ( ! change_permissions(fn, PER_RO ) )
64         goto error;
65     }
66   else if ( lex_match_id (lexer, "WRITEABLE"))
67     {
68       if ( ! change_permissions(fn, PER_RW ) )
69         goto error;
70     }
71   else
72     {
73       msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
74       goto error;
75     }
76
77   free(fn);
78
79   return CMD_SUCCESS;
80
81  error:
82
83   free(fn);
84
85   return CMD_FAILURE;
86 }
87
88
89
90 int
91 change_permissions (const char *file_name, enum PER per)
92 {
93   struct stat buf;
94   mode_t mode;
95
96   if (settings_get_safer_mode ())
97     {
98       msg (SE, _("This command not allowed when the SAFER option is set."));
99       return CMD_FAILURE;
100     }
101
102
103   if ( -1 == stat(file_name, &buf) )
104     {
105       const int errnum = errno;
106       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
107       return 0;
108     }
109
110   if ( per == PER_RW )
111     mode = buf.st_mode | 0200;
112   else
113     mode = buf.st_mode & ~0222;
114
115   if ( -1 == chmod(file_name, mode))
116
117     {
118       const int errnum = errno;
119       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
120       return 0;
121     }
122
123   return 1;
124 }