1c3ee1fc1c2adf61328fe00f074cd85d39315f9d
[pspp-builds.git] / src / 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 "error.h"
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include "settings.h"
29 #include "command.h"
30 #include "error.h"
31 #include "lexer.h"
32 #include "misc.h"
33 #include "str.h"
34
35
36
37 enum PER {PER_RO, PER_RW};
38
39 int change_permissions(const char *filename, enum PER per);
40
41
42 /* Parses the PERMISSIONS command. */
43 int
44 cmd_permissions (void)
45 {
46   char  *fn = 0;
47
48   lex_match ('/');
49
50   if (lex_match_id ("FILE"))
51     lex_match ('=');
52
53   fn = strdup(ds_c_str(&tokstr));
54   lex_force_match(T_STRING);
55
56
57   lex_match ('/');
58   
59   if ( ! lex_match_id ("PERMISSIONS"))
60     goto error;
61
62   lex_match('=');
63
64   if ( lex_match_id("READONLY"))
65     {
66       if ( ! change_permissions(fn, PER_RO ) ) 
67         goto error;
68     }
69   else if ( lex_match_id("WRITEABLE"))
70     {
71       if ( ! change_permissions(fn, PER_RW ) ) 
72         goto error;
73     }
74   else
75     {
76       msg(ME, _("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 *filename, enum PER per)
95 {
96   struct stat buf;
97   mode_t mode;
98
99   if ( 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(filename, &buf) ) 
107     {
108       const int errnum = errno;
109       msg(ME,_("Cannot stat %s: %s"), filename, strerror(errnum));
110       return 0;
111     }
112
113   if ( per == PER_RW )
114     mode = buf.st_mode | S_IWUSR ;
115   else
116     mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
117
118   if ( -1 == chmod(filename, mode))
119
120     {
121       const int errnum = errno;
122       msg(ME,_("Cannot change mode of %s: %s"), filename, strerror(errnum));
123       return 0;
124     }
125
126   return 1;
127 }