bce2194a83dcd603ffc12ccc7ec5064579ea24b1
[pspp] / src / language / utilities / permissions.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Author: John Darrington
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <libpspp/message.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <data/settings.h>
28 #include <language/command.h>
29 #include <libpspp/message.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/misc.h>
32 #include "stat-macros.h"
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 enum PER {PER_RO, PER_RW};
39
40 int change_permissions(const char *file_name, enum PER per);
41
42
43 /* Parses the PERMISSIONS command. */
44 int
45 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
46 {
47   char  *fn = 0;
48
49   lex_match (lexer, '/');
50
51   if (lex_match_id (lexer, "FILE"))
52     lex_match (lexer, '=');
53
54   fn = ds_xstrdup (lex_tokstr (lexer));
55   lex_force_match (lexer, T_STRING);
56
57
58   lex_match (lexer, '/');
59   
60   if ( ! lex_match_id (lexer, "PERMISSIONS"))
61     goto error;
62
63   lex_match (lexer, '=');
64
65   if ( lex_match_id (lexer, "READONLY"))
66     {
67       if ( ! change_permissions(fn, PER_RO ) ) 
68         goto error;
69     }
70   else if ( lex_match_id (lexer, "WRITEABLE"))
71     {
72       if ( ! change_permissions(fn, PER_RW ) ) 
73         goto error;
74     }
75   else
76     {
77       msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
78       goto error;
79     }
80
81   free(fn);
82
83   return CMD_SUCCESS;
84
85  error:
86
87   free(fn);
88
89   return CMD_FAILURE;
90 }
91
92
93
94 int
95 change_permissions(const char *file_name, enum PER per)
96 {
97   struct stat buf;
98   mode_t mode;
99
100   if (get_safer_mode ())
101     {
102       msg (SE, _("This command not allowed when the SAFER option is set."));
103       return CMD_FAILURE;
104     }
105
106
107   if ( -1 == stat(file_name, &buf) ) 
108     {
109       const int errnum = errno;
110       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
111       return 0;
112     }
113
114   if ( per == PER_RW )
115     mode = buf.st_mode | S_IWUSR ;
116   else
117     mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
118
119   if ( -1 == chmod(file_name, mode))
120
121     {
122       const int errnum = errno;
123       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
124       return 0;
125     }
126
127   return 1;
128 }