new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / fs_op.c
1 /* Copyright (C) 2014 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10
11 /* Functions to retrieve information about the filesystem
12  */
13
14
15 #include "shared.h"
16 #ifndef ARGV0
17 #define ARGV0 "fs_op"
18 #endif
19
20 const struct file_system_type network_file_systems[] = {
21     {.name="NFS",  .f_type=0x6969,     .flag=1},
22     {.name="CIFS", .f_type=0xFF534D42, .flag=1},
23
24     /*  The last entry must be name=NULL */
25     {.name=NULL, .f_type=0, .flag=0}
26 };
27
28 /* List of filesystem to skip the link count test */
29 const struct file_system_type skip_file_systems[] = {
30     {.name="BTRFS", .f_type=0x9123683E, .flag=1},
31
32     /*  The last entry must be name=NULL */
33     {.name=NULL, .f_type=0, .flag=0}
34 };
35
36 short IsNFS(const char *dir_name)
37 {
38 #if !defined(WIN32) && (defined(Linux) || defined(FreeBSD) || defined(OpenBSD))
39     struct statfs stfs;
40
41     /* ignore NFS (0x6969) or CIFS (0xFF534D42) mounts */
42     if ( ! statfs(dir_name, &stfs) )
43     {
44         int i;
45         for ( i=0; network_file_systems[i].name != NULL; i++ ) {
46 #if __OpenBSD__
47             if(strcasecmp(network_file_systems[i].name, stfs.f_fstypename) == 0) {
48 #else
49             if(network_file_systems[i].f_type == stfs.f_type ) {
50 #endif  // __OpenBSD
51                 return network_file_systems[i].flag;
52             }
53         }
54         return(0);
55     }
56     else
57     {
58         /* If the file exists, throw an error and retreat! If the file does not exist, there
59          * is no reason to spam the log with these errors. */
60         if(errno != ENOENT) {
61             merror("ERROR: statfs('%s') produced error: %s", dir_name, strerror(errno));
62         }
63         return(-1);
64     }
65 #else
66     verbose(
67         "INFO: Attempted to check NFS status for '%s', but we don't know how on this OS.",
68         dir_name
69     );
70 #endif
71     return(0);
72 }
73
74 short skipFS(const char *dir_name)
75 {
76 #if !defined(WIN32) && (defined(Linux) || defined(FreeBSD) || defined(OpenBSD))
77     struct statfs stfs;
78
79     if ( ! statfs(dir_name, &stfs) )
80     {
81         int i;
82         for ( i=0; skip_file_systems[i].name != NULL; i++ ) {
83 #if __OpenBSD__
84             if(strcasecmp(skip_file_systems[i].name, stfs.f_fstypename) == 0) {
85 #else
86             if(skip_file_systems[i].f_type == stfs.f_type ) {
87 #endif  // __OpenBSD__
88                 debug1("%s: Skipping dir (FS %s): %s ", ARGV0, skip_file_systems[i].name, dir_name);
89                 return skip_file_systems[i].flag;
90             }
91         }
92         return(0);
93     }
94     else
95     {
96         /* If the file exists, throw an error and retreat! If the file does not exist, there
97          * is no reason to spam the log with these errors. */
98         if(errno != ENOENT) {
99             merror("ERROR: statfs('%s') produced error: %s", dir_name, strerror(errno));
100         }
101         return(-1);
102     }
103 #else
104     verbose(
105         "INFO: Attempted to check FS status for '%s', but we don't know how on this OS.",
106         dir_name
107     );
108 #endif
109     return(0);
110 }
111
112
113 /* EOF */