PERMISSIONS: Add missing check for string token.
[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   if (!lex_force_string (lexer))
51     return CMD_FAILURE;
52
53   fn = ds_xstrdup (lex_tokstr (lexer));
54   lex_force_match (lexer, T_STRING);
55
56
57   lex_match (lexer, '/');
58
59   if ( ! lex_match_id (lexer, "PERMISSIONS"))
60     goto error;
61
62   lex_match (lexer, '=');
63
64   if ( lex_match_id (lexer, "READONLY"))
65     {
66       if ( ! change_permissions(fn, PER_RO ) )
67         goto error;
68     }
69   else if ( lex_match_id (lexer, "WRITEABLE"))
70     {
71       if ( ! change_permissions(fn, PER_RW ) )
72         goto error;
73     }
74   else
75     {
76       msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
77       goto error;
78     }
79
80   free(fn);
81
82   return CMD_SUCCESS;
83
84  error:
85
86   free(fn);
87
88   return CMD_FAILURE;
89 }
90
91
92
93 int
94 change_permissions (const char *file_name, enum PER per)
95 {
96   struct stat buf;
97   mode_t mode;
98
99   if (settings_get_safer_mode ())
100     {
101       msg (SE, _("This command not allowed when the SAFER option is set."));
102       return CMD_FAILURE;
103     }
104
105
106   if ( -1 == stat(file_name, &buf) )
107     {
108       const int errnum = errno;
109       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
110       return 0;
111     }
112
113   if ( per == PER_RW )
114     mode = buf.st_mode | 0200;
115   else
116     mode = buf.st_mode & ~0222;
117
118   if ( -1 == chmod(file_name, mode))
119
120     {
121       const int errnum = errno;
122       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
123       return 0;
124     }
125
126   return 1;
127 }