| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 2016,2020,2021,2024,2025 Hannes von Haugwitz |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h"cxx . . $ `<file>/include/config.h`/ |
| 22 | #include "file.h"cxx . . $ `<file>/include/file.h`/ |
| 23 | #ifdef HAVE_FSTYPEcxx . . $ `include/config.h:53:9`! |
| 24 | #include <string.h> |
| 25 | #include <sys/vfs.h> |
| 26 | #include <linux/magic.h> |
| 27 | #ifndef EXFAT_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:49:9`! |
| 28 | #define EXFAT_SUPER_MAGIC 0x2011BAB0 |
| 29 | #endif |
| 30 | #ifndef FUSE_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:39:9`! |
| 31 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 32 | #endif |
| 33 | #ifndef XFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:34:9`! |
| 34 | #define XFS_SUPER_MAGIC 0x58465342 |
| 35 | #endif |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | #include "util.h"cxx . . $ `<file>/include/util.h`/ |
| 39 | #endif |
| 40 | #include <limits.h> |
| 41 | |
| 42 | #ifdef HAVE_FSTYPEcxx . . $ `./include/config.h:53:9`! |
| 43 | filesystem_tcxx . . $ filesystem_t# filesystemscxx . . $ filesystems.[] = { |
| 44 | { "9p", V9FS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:79:9`! }, |
| 45 | { "autofs", AUTOFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:8:9`! }, |
| 46 | { "bcachefs", 0xca451a4e }, |
| 47 | { "binfmt", BINFMTFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:83:9`! }, |
| 48 | { "bpf", BPF_FS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:96:9`! }, |
| 49 | { "btrfs", BTRFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:28:9`! }, |
| 50 | { "cgroup", CGROUP_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:70:9`! }, |
| 51 | { "cgroup2", CGROUP2_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:71:9`! }, |
| 52 | { "configfs", 0x62656570 }, |
| 53 | { "debugfs", DEBUGFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:13:9`! }, |
| 54 | { "devpts", DEVPTS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:84:9`! }, |
| 55 | { "efivarfs", EFIVARFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:36:9`! }, |
| 56 | { "exfat", EXFAT_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:49:9`! }, |
| 57 | { "ext", EXT2_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:24:9`! }, /* ext2/ext3/ext4 */ |
| 58 | { "f2fs", F2FS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:30:9`! }, |
| 59 | { "fuse", FUSE_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:39:9`! }, |
| 60 | { "fusectl", 0x65735543 }, |
| 61 | { "hugetlbfs", HUGETLBFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:19:9`! }, |
| 62 | { "mqueue", 0x19800202 }, |
| 63 | { "nfs", NFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:51:9`! }, |
| 64 | { "nilfs", NILFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:29:9`! }, |
| 65 | { "overlayfs", OVERLAYFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:38:9`! }, |
| 66 | { "proc", PROC_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:88:9`! }, |
| 67 | { "pstore", PSTOREFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:35:9`! }, |
| 68 | { "ramfs", RAMFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:17:9`! }, |
| 69 | { "securityfs", SECURITYFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:14:9`! }, |
| 70 | { "selinuxfs", SELINUX_MAGICcxx . . $ `<external>/706109818625493630/magic.h:15:9`! }, |
| 71 | { "squashfs", SQUASHFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:20:9`! }, |
| 72 | { "sysfs", SYSFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:90:9`! }, |
| 73 | { "tmpfs", TMPFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:18:9`! }, |
| 74 | { "tracefs", TRACEFS_MAGICcxx . . $ `<external>/706109818625493630/magic.h:77:9`! }, |
| 75 | { "udf", UDF_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:101:9`! }, |
| 76 | { "vfat", MSDOS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:48:9`! }, |
| 77 | { "xfs", XFS_SUPER_MAGICcxx . . $ `<external>/706109818625493630/magic.h:34:9`! }, |
| 78 | }; |
| 79 | int num_filesystemscxx . . $ num_filesystems. = sizeof(filesystemscxx . . $ filesystems.)/sizeof(filesystem_tcxx . . $ filesystem_t#); |
| 80 | #endif |
| 81 | |
| 82 | typedef structcxx . . $ $anonymous_type_eda923acc5e3be58_0# { |
| 83 | char ccxx . . $ $anonymous_type_eda923acc5e3be58_0#c.; |
| 84 | char *scxx . . $ $anonymous_type_eda923acc5e3be58_0#s.; |
| 85 | FT_TYPEcxx . . $ `include/file.h:28:9`! rcxx . . $ $anonymous_type_eda923acc5e3be58_0#r.; |
| 86 | mode_tcxx . . $ mode_t# ftcxx . . $ $anonymous_type_eda923acc5e3be58_0#ft.; |
| 87 | } f_type_tcxx . . $ f_type_t#; |
| 88 | |
| 89 | static f_type_tcxx . . $ f_type_t# filetypescxx . . $ filetypes.[] = { |
| 90 | { 'f', "regular file", FT_REGcxx . . $ `include/file.h:31:9`!, S_IFREGcxx . . $ `<external>/531885356569766532/stat.h:108:10`! }, |
| 91 | { 'd', "directory", FT_DIRcxx . . $ `include/file.h:32:9`!, S_IFDIRcxx . . $ `<external>/531885356569766532/stat.h:105:10`! }, |
| 92 | #ifdef S_IFIFOcxx . . $ `<external>/531885356569766532/stat.h:110:11`! |
| 93 | { 'p', "FIFO", FT_FIFOcxx . . $ `include/file.h:33:9`!, S_IFIFOcxx . . $ `<external>/531885356569766532/stat.h:110:11`! }, |
| 94 | #endif |
| 95 | { 'l', "symbolic link", FT_LNKcxx . . $ `include/file.h:34:9`!, S_IFLNKcxx . . $ `<external>/531885356569766532/stat.h:113:11`! }, |
| 96 | { 'b', "block device", FT_BLKcxx . . $ `include/file.h:35:9`!, S_IFBLKcxx . . $ `<external>/531885356569766532/stat.h:107:10`! }, |
| 97 | { 'c', "character device", FT_CHRcxx . . $ `include/file.h:36:9`!, S_IFCHRcxx . . $ `<external>/531885356569766532/stat.h:106:10`! }, |
| 98 | #ifdef S_IFSOCKcxx . . $ `<external>/531885356569766532/stat.h:117:11`! |
| 99 | { 's', "socket", FT_SOCKcxx . . $ `include/file.h:37:9`!, S_IFSOCKcxx . . $ `<external>/531885356569766532/stat.h:117:11`! }, |
| 100 | #endif |
| 101 | #ifdef S_IFDOOR |
| 102 | { 'D', "door", FT_DOOR, S_IFDOOR }, |
| 103 | #endif |
| 104 | #ifdef S_IFPORT |
| 105 | { 'P', "port", FT_PORT, S_IFPORT }, |
| 106 | #endif |
| 107 | }; |
| 108 | |
| 109 | static int num_filetypescxx . . $ num_filetypes. = sizeof(filetypescxx . . $ filetypes.)/sizeof(f_type_tcxx . . $ f_type_t#); |
| 110 | |
| 111 | char get_f_type_char_from_f_typecxx . . $ get_f_type_char_from_f_type(1cb8f0426fc6e5a2).(FT_TYPEcxx . . $ `include/file.h:28:9`! r) { |
| 112 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 113 | if (r == filetypescxx . . $ filetypes.[i].rcxx . . $ $anonymous_type_eda923acc5e3be58_0#r.) { |
| 114 | return filetypescxx . . $ filetypes.[i].ccxx . . $ $anonymous_type_eda923acc5e3be58_0#c.; |
| 115 | } |
| 116 | } |
| 117 | return '?'; |
| 118 | } |
| 119 | |
| 120 | char *get_f_type_string_from_f_typecxx . . $ get_f_type_string_from_f_type(f109d7e0a0ca42b3).(FT_TYPEcxx . . $ `include/file.h:28:9`! r) { |
| 121 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 122 | if (r == filetypescxx . . $ filetypes.[i].rcxx . . $ $anonymous_type_eda923acc5e3be58_0#r.) { |
| 123 | return filetypescxx . . $ filetypes.[i].scxx . . $ $anonymous_type_eda923acc5e3be58_0#s.; |
| 124 | } |
| 125 | } |
| 126 | return "unknown file type"; |
| 127 | } |
| 128 | |
| 129 | char get_f_type_char_from_permcxx . . $ get_f_type_char_from_perm(1cb8f0426fc6e5a2).(mode_tcxx . . $ mode_t# mode) { |
| 130 | mode_tcxx . . $ mode_t# ft = mode & S_IFMTcxx . . $ `<external>/531885356569766532/stat.h:104:10`!; |
| 131 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 132 | if (ft == filetypescxx . . $ filetypes.[i].ftcxx . . $ $anonymous_type_eda923acc5e3be58_0#ft.) { |
| 133 | return filetypescxx . . $ filetypes.[i].ccxx . . $ $anonymous_type_eda923acc5e3be58_0#c.; |
| 134 | } |
| 135 | } |
| 136 | return '?'; |
| 137 | } |
| 138 | |
| 139 | char *get_f_type_string_from_permcxx . . $ get_f_type_string_from_perm(f109d7e0a0ca42b3).(mode_tcxx . . $ mode_t# mode) { |
| 140 | mode_tcxx . . $ mode_t# ft = mode & S_IFMTcxx . . $ `<external>/531885356569766532/stat.h:104:10`!; |
| 141 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 142 | if (ft == filetypescxx . . $ filetypes.[i].ftcxx . . $ $anonymous_type_eda923acc5e3be58_0#ft.) { |
| 143 | return filetypescxx . . $ filetypes.[i].scxx . . $ $anonymous_type_eda923acc5e3be58_0#s.; |
| 144 | } |
| 145 | } |
| 146 | return "unknown file type"; |
| 147 | } |
| 148 | |
| 149 | FT_TYPEcxx . . $ `include/file.h:28:9`! get_f_type_from_charcxx . . $ get_f_type_from_char(f1d64ee6cf94df31).(char c) { |
| 150 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 151 | if (c == filetypescxx . . $ filetypes.[i].ccxx . . $ $anonymous_type_eda923acc5e3be58_0#c.) { |
| 152 | return filetypescxx . . $ filetypes.[i].rcxx . . $ $anonymous_type_eda923acc5e3be58_0#r.; |
| 153 | } |
| 154 | } |
| 155 | return FT_NULLcxx . . $ `include/file.h:29:9`!; |
| 156 | } |
| 157 | |
| 158 | FT_TYPEcxx . . $ `include/file.h:28:9`! get_f_type_from_permcxx . . $ get_f_type_from_perm(2141b656ce2cc063).(mode_tcxx . . $ mode_t# mode) { |
| 159 | mode_tcxx . . $ mode_t# ft = mode & S_IFMTcxx . . $ `<external>/531885356569766532/stat.h:104:10`!; |
| 160 | for (int i = 0 ; i < num_filetypescxx . . $ num_filetypes.; ++i) { |
| 161 | if (ft == filetypescxx . . $ filetypes.[i].ftcxx . . $ $anonymous_type_eda923acc5e3be58_0#ft.) { |
| 162 | return filetypescxx . . $ filetypes.[i].rcxx . . $ $anonymous_type_eda923acc5e3be58_0#r.; |
| 163 | } |
| 164 | } |
| 165 | return FT_NULLcxx . . $ `include/file.h:29:9`!; |
| 166 | } |
| 167 | |
| 168 | #ifdef HAVE_FSTYPEcxx . . $ `./include/config.h:53:9`! |
| 169 | FS_TYPEcxx . . $ `include/file.h:49:9`! get_fs_type_from_stringcxx . . $ get_fs_type_from_string(751346f8406fd082).(const char *fs_type_str) { |
| 170 | for (int i = 0 ; i < num_filesystemscxx . . $ num_filesystems.; ++i) { |
| 171 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(filesystemscxx . . $ filesystems.[i].strcxx . . $ $anonymous_type_865fa0d4a79f80f3_0#str., fs_type_str) == 0) { |
| 172 | return filesystemscxx . . $ filesystems.[i].magiccxx . . $ $anonymous_type_865fa0d4a79f80f3_0#magic.; |
| 173 | } |
| 174 | } |
| 175 | if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(fs_type_str, "0x", 2) == 0) { |
| 176 | long long ll; |
| 177 | char* e; |
| 178 | ll = strtollcxx . . $ strtoll(c50972e7488cbac6).(fs_type_str, &e, 16); |
| 179 | if (*e =='\0' && ll > 0 && ll <= UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!) { |
| 180 | return (FS_TYPEcxx . . $ `include/file.h:49:9`!) ll; |
| 181 | } |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int generate_fs_type_stringcxx . . $ generate_fs_type_string(42a4fda062c6f95b).(FS_TYPEcxx . . $ `include/file.h:49:9`! magic, char* str) { |
| 187 | for (int i = 0 ; i < num_filesystemscxx . . $ num_filesystems.; ++i) { |
| 188 | if (filesystemscxx . . $ filesystems.[i].magiccxx . . $ $anonymous_type_865fa0d4a79f80f3_0#magic. == magic) { |
| 189 | size_t length = strlencxx . . $ strlen(751346f8406fd082).(filesystemscxx . . $ filesystems.[i].strcxx . . $ $anonymous_type_865fa0d4a79f80f3_0#str.)+1; |
| 190 | if (str) { sprintfcxx . . $ sprintf(26c8642d26409d6d).(str, "%s", filesystemscxx . . $ filesystems.[i].strcxx . . $ $anonymous_type_865fa0d4a79f80f3_0#str.); } |
| 191 | return length; |
| 192 | } |
| 193 | } |
| 194 | size_t length = snprintfcxx . . $ snprintf(ea502d73e74c754c).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 0, "0x%lx", magic)+1; |
| 195 | if (str) { sprintfcxx . . $ sprintf(26c8642d26409d6d).(str, "0x%lx", magic); } |
| 196 | return length; |
| 197 | } |
| 198 | |
| 199 | char *get_fs_type_string_from_magiccxx . . $ get_fs_type_string_from_magic(4b58d6ff543a4736).(FS_TYPEcxx . . $ `include/file.h:49:9`! magic) { |
| 200 | char *str = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 201 | int n = generate_fs_type_stringcxx . . $ generate_fs_type_string(42a4fda062c6f95b).(magic, str); |
| 202 | str = checked_malloccxx . . $ checked_malloc(bfcfa4d6b7f7ef64).(n); |
| 203 | generate_fs_type_stringcxx . . $ generate_fs_type_string(42a4fda062c6f95b).(magic, str);; |
| 204 | return str; |
| 205 | } |
| 206 | #endif |