Fix crash in T-Test after parsing a null terminated string.
[pspp] / src / language / utilities / permissions.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2010, 2011 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
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "data/settings.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 enum PER {PER_RO, PER_RW};
38
39 int change_permissions(const char *file_name, enum PER per);
40
41
42 /* Parses the PERMISSIONS command. */
43 int
44 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
45 {
46   char  *fn = NULL;
47   const char *str = NULL;
48   lex_match (lexer, T_SLASH);
49
50   if (lex_match_id (lexer, "FILE"))
51     lex_match (lexer, T_EQUALS);
52
53   str = lex_tokcstr (lexer);
54   fn = strdup (str);
55
56   if (!lex_force_match (lexer, T_STRING))
57     goto error;
58
59   lex_match (lexer, T_SLASH);
60
61   if ( ! lex_match_id (lexer, "PERMISSIONS"))
62     goto error;
63
64   lex_match (lexer, T_EQUALS);
65
66   if ( lex_match_id (lexer, "READONLY"))
67     {
68       if (! change_permissions (fn, PER_RO))
69         goto error;
70     }
71   else if (lex_match_id (lexer, "WRITEABLE"))
72     {
73       if (! change_permissions (fn, PER_RW ))
74         goto error;
75     }
76   else
77     {
78       lex_error_expecting (lexer, "WRITEABLE", "READONLY", NULL_SENTINEL);
79       goto error;
80     }
81
82   free (fn);
83
84   return CMD_SUCCESS;
85
86  error:
87
88   free(fn);
89
90   return CMD_FAILURE;
91 }
92
93
94
95 int
96 change_permissions (const char *file_name, enum PER per)
97 {
98   char *locale_file_name;
99   struct stat buf;
100   mode_t mode;
101
102   if (settings_get_safer_mode ())
103     {
104       msg (SE, _("This command not allowed when the %s option is set."), "SAFER");
105       return 0;
106     }
107
108   locale_file_name = utf8_to_filename (file_name);
109   if ( -1 == stat(locale_file_name, &buf) )
110     {
111       const int errnum = errno;
112       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
113       free (locale_file_name);
114       return 0;
115     }
116
117   if ( per == PER_RW )
118     mode = buf.st_mode | 0200;
119   else
120     mode = buf.st_mode & ~0222;
121
122   if ( -1 == chmod(locale_file_name, mode))
123
124     {
125       const int errnum = errno;
126       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
127       free (locale_file_name);
128       return 0;
129     }
130
131   free (locale_file_name);
132
133   return 1;
134 }