Ticket #89: 00-statok-mustexist

File 00-statok-mustexist, 11.2 kB (added by matthijs, 16 months ago)

Add a mustexist parameter to stat_ok, allowing stat_ok to succeed when a file does not exist

Line 
1Index: vuurmuur/libvuurmuur/plugins/textdir/textdir_list.c
2===================================================================
3--- vuurmuur.orig/libvuurmuur/plugins/textdir/textdir_list.c    2009-04-21 23:14:37.000000000 +0200
4+++ vuurmuur/libvuurmuur/plugins/textdir/textdir_list.c 2009-04-21 23:47:10.000000000 +0200
5@@ -108,7 +108,7 @@
6                             (void)vrprint.debug(__FUNC__, "service '%s', file: '%s'.", name, file_location);
7 
8                         /* now stat it */
9-                        if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
10+                        if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
11                         {
12                             free(file_location);
13 
14@@ -185,7 +185,7 @@
15                                     (void)vrprint.debug(__FUNC__, "interface '%s', file: '%s'.", name, file_location);
16 
17                                 /* now stat it */
18-                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
19+                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
20                                 {
21                                     free(file_location);
22 
23@@ -269,7 +269,7 @@
24                                 (void)vrprint.debug(__FUNC__, "rule '%s', file: '%s'.", name, file_location);
25 
26                             /* now stat it */
27-                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
28+                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
29                             {
30                                 free(file_location);
31 
32@@ -336,7 +336,7 @@
33                                     (void)vrprint.debug(__FUNC__, "host '%s', file: '%s'.", cur_zonename, file_location);
34 
35                                 // now stat it
36-                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
37+                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
38                                 {
39                                     free(file_location);
40 
41@@ -392,7 +392,7 @@
42                                     (void)vrprint.debug(__FUNC__, "group '%s', file: '%s'.", cur_zonename, file_location);
43 
44                                 // now stat it
45-                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
46+                                if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
47                                 {
48                                     free(file_location);
49 
50@@ -463,7 +463,7 @@
51                                 (void)vrprint.debug(__FUNC__, "list_textdir: network '%s', file: '%s'.", cur_zonename, file_location);
52 
53                             // now stat it
54-                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
55+                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
56                             {
57                                 free(file_location);
58 
59@@ -525,7 +525,7 @@
60                                 (void)vrprint.debug(__FUNC__, "zone '%s', file: '%s'.", ptr->cur_zone, file_location);
61 
62                             // now stat it
63-                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET))
64+                            if(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_QUIET, STATOK_MUST_EXIST))
65                             {
66                                 free(file_location);
67 
68Index: vuurmuur/libvuurmuur/plugins/textdir/textdir_plugin.c
69===================================================================
70--- vuurmuur.orig/libvuurmuur/plugins/textdir/textdir_plugin.c  2009-04-21 23:14:37.000000000 +0200
71+++ vuurmuur/libvuurmuur/plugins/textdir/textdir_plugin.c       2009-04-21 23:47:10.000000000 +0200
72@@ -285,7 +285,7 @@
73     }
74 
75     /* see if we like the permissions of the textdirroot */
76-    if(!(stat_ok(debuglvl, ptr->textdirlocation, STATOK_WANT_DIR, STATOK_QUIET)))
77+    if(!(stat_ok(debuglvl, ptr->textdirlocation, STATOK_WANT_DIR, STATOK_QUIET, STATOK_MUST_EXIST)))
78         return(-1);
79 
80     if(ptr->backend_open == 1)
81@@ -440,7 +440,7 @@
82     }
83 
84     /* now stat it */
85-    if(stat_ok(debuglvl, dir_location, STATOK_WANT_DIR, STATOK_VERBOSE) != 1)
86+    if(stat_ok(debuglvl, dir_location, STATOK_WANT_DIR, STATOK_VERBOSE, STATOK_MUST_EXIST) != 1)
87     {
88         (void)vrprint.error(-1, "Error", "checking '%s' failed. Please check if the directory exists and that the permissions are ok.",
89                                 dir_location);
90@@ -865,7 +865,7 @@
91         return(-1);
92 
93     /* see if we like the file permissions */
94-    if(!(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_VERBOSE)))
95+    if(!(stat_ok(debuglvl, file_location, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
96         return(-1);
97 
98     /* name splitting only needed for network and zone, as host and group just use the file_location
99Index: vuurmuur/libvuurmuur/src/blocklist.c
100===================================================================
101--- vuurmuur.orig/libvuurmuur/src/blocklist.c   2009-04-21 23:14:37.000000000 +0200
102+++ vuurmuur/libvuurmuur/src/blocklist.c        2009-04-21 23:47:10.000000000 +0200
103@@ -393,7 +393,7 @@
104         (void)vrprint.debug(__FUNC__, "load_ips: %c, no_refcnt: %c.", load_ips, no_refcnt);
105 
106     /* check the file: it reports error's by itself */
107-    if(stat_ok(debuglvl, conf.blocklist_location, STATOK_WANT_FILE, STATOK_VERBOSE))
108+    if(stat_ok(debuglvl, conf.blocklist_location, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST))
109     {
110         /* open the blocklist-file */
111         if(!(fp = fopen(conf.blocklist_location, "r")))
112Index: vuurmuur/libvuurmuur/src/config.c
113===================================================================
114--- vuurmuur.orig/libvuurmuur/src/config.c      2009-04-21 23:14:37.000000000 +0200
115+++ vuurmuur/libvuurmuur/src/config.c   2009-04-21 23:47:10.000000000 +0200
116@@ -394,7 +394,7 @@
117     fclose(fp);
118 
119     /* check if we like the configfile */
120-    if(!(stat_ok(debuglvl, cnf->configfile, STATOK_WANT_FILE, STATOK_VERBOSE)))
121+    if(!(stat_ok(debuglvl, cnf->configfile, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
122         return(VR_CNF_E_FILE_PERMISSION);
123 
124 
125Index: vuurmuur/libvuurmuur/src/io.c
126===================================================================
127--- vuurmuur.orig/libvuurmuur/src/io.c  2009-04-21 23:14:37.000000000 +0200
128+++ vuurmuur/libvuurmuur/src/io.c       2009-04-21 23:47:10.000000000 +0200
129@@ -95,7 +95,7 @@
130 {
131     DIR *dir_p = NULL;
132 
133-    if(!(stat_ok(debuglvl, name, STATOK_WANT_DIR, STATOK_VERBOSE)))
134+    if(!(stat_ok(debuglvl, name, STATOK_WANT_DIR, STATOK_VERBOSE, STATOK_MUST_EXIST)))
135         return(NULL);
136 
137     /* finally try to open */
138@@ -126,12 +126,16 @@
139         STATOK_VERBOSE
140         STATOK_QUIET
141 
142+    parameters for 'must_exist' are:
143+        STATOK_MUST_EXIST
144+        STATOK_ALLOW_NOTFOUND
145+
146     Returncodes:
147         1: file ok
148         0: file not ok
149 */
150 int
151-stat_ok(const int debuglvl, const char *file_loc, char type, char output)
152+stat_ok(const int debuglvl, const char *file_loc, char type, char output, char must_exist)
153 {
154     struct stat stat_buf;
155     mode_t      mode = 0600;
156@@ -146,8 +150,18 @@
157     /* stat the damn thing */
158     if(lstat(file_loc, &stat_buf) == -1)
159     {
160-        (void)vrprint.error(-1, "Error",  "checking failed for '%s': %s.", file_loc, strerror(errno));
161-        return(0);
162+        if (errno == ENOENT) {
163+            if (must_exist == STATOK_ALLOW_NOTFOUND) {
164+                /* Allow the file to be non-existing. */
165+                return(1);
166+            } else {
167+                (void)vrprint.error(-1, "Error",  "File not found: '%s'.", file_loc);
168+                return(0);
169+            }
170+        } else {
171+            (void)vrprint.error(-1, "Error",  "checking failed for '%s': %s.", file_loc, strerror(errno));
172+            return(0);
173+        }
174     }
175 
176     /* we wont open symbolic links */
177Index: vuurmuur/libvuurmuur/src/vuurmuur.h
178===================================================================
179--- vuurmuur.orig/libvuurmuur/src/vuurmuur.h    2009-04-21 23:14:37.000000000 +0200
180+++ vuurmuur/libvuurmuur/src/vuurmuur.h 2009-04-21 23:47:10.000000000 +0200
181@@ -95,6 +95,9 @@
182 #define STATOK_VERBOSE          (char)0
183 #define STATOK_QUIET            (char)1
184 
185+#define STATOK_ALLOW_NOTFOUND   (char)0
186+#define STATOK_MUST_EXIST       (char)1
187+
188 #define IPTCHK_VERBOSE          (char)0
189 #define IPTCHK_QUIET            (char)1
190 
191@@ -1403,7 +1406,7 @@
192 */
193 FILE *vuurmuur_fopen(const char *path, const char *mode);
194 DIR *vuurmuur_opendir(const int, const char *);
195-int stat_ok(const int, const char *, char, char);
196+int stat_ok(const int, const char *, char, char, char);
197 int check_pidfile(char *pidfile_location);
198 int create_pidfile(char *pidfile_location, int shm_id);
199 int remove_pidfile(char *pidfile_location);
200Index: vuurmuur/vuurmuur/vuurmuur/ruleset.c
201===================================================================
202--- vuurmuur.orig/vuurmuur/vuurmuur/ruleset.c   2009-04-21 23:14:37.000000000 +0200
203+++ vuurmuur/vuurmuur/vuurmuur/ruleset.c        2009-04-21 23:47:10.000000000 +0200
204@@ -1163,7 +1163,7 @@
205     }
206 
207     /* stat_ok */
208-    if(!(stat_ok(debuglvl, path_to_ruleset, STATOK_WANT_FILE, STATOK_VERBOSE)))
209+    if(!(stat_ok(debuglvl, path_to_ruleset, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
210     {
211         (void)vrprint.error(-1, "Error", "serious file problem (in: %s:%d).", __FUNC__, __LINE__);
212         return(-1);
213@@ -1222,7 +1222,7 @@
214     }
215 
216     /* stat_ok */
217-    if(!(stat_ok(debuglvl, path_to_ruleset, STATOK_WANT_FILE, STATOK_VERBOSE)))
218+    if(!(stat_ok(debuglvl, path_to_ruleset, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
219     {
220         (void)vrprint.error(-1, "Error", "serious file problem (in: %s:%d).", __FUNC__, __LINE__);
221         return(-1);
222Index: vuurmuur/vuurmuur/vuurmuur_log/logfile.c
223===================================================================
224--- vuurmuur.orig/vuurmuur/vuurmuur_log/logfile.c       2009-04-21 23:14:37.000000000 +0200
225+++ vuurmuur/vuurmuur/vuurmuur_log/logfile.c    2009-04-21 23:47:10.000000000 +0200
226@@ -141,7 +141,7 @@
227         if(fp != NULL)
228             (void)fclose(fp);
229 
230-        if(!stat_ok(debuglvl, path, STATOK_WANT_FILE, STATOK_VERBOSE))
231+        if(!stat_ok(debuglvl, path, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST))
232         {
233             (void)vrprint.error(-1, "Error", "won't open logfile '%s' because of permission problems (in: %s:%d).", path, __FUNC__, __LINE__);
234             return(NULL);
235Index: vuurmuur/vuurmuur-conf/src/config.c
236===================================================================
237--- vuurmuur.orig/vuurmuur-conf/src/config.c    2009-04-21 23:14:37.000000000 +0200
238+++ vuurmuur/vuurmuur-conf/src/config.c 2009-04-21 23:47:10.000000000 +0200
239@@ -114,7 +114,7 @@
240 
241 
242     /* check if we like the configfile */
243-    if(!(stat_ok(debuglvl, configfile_location, STATOK_WANT_FILE, STATOK_VERBOSE)))
244+    if(!(stat_ok(debuglvl, configfile_location, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
245         return(VR_CNF_E_FILE_PERMISSION);
246 
247