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