| 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" |
| 22 | #include "file.h" |
| 23 | #ifdef HAVE_FSTYPE |
| 24 | #include <string.h> |
| 25 | #include <sys/vfs.h> |
| 26 | #include <linux/magic.h> |
| 27 | #ifndef EXFAT_SUPER_MAGIC |
| 28 | #define EXFAT_SUPER_MAGIC 0x2011BAB0 |
| 29 | #endif |
| 30 | #ifndef FUSE_SUPER_MAGIC |
| 31 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 32 | #endif |
| 33 | #ifndef XFS_SUPER_MAGIC |
| 34 | #define XFS_SUPER_MAGIC 0x58465342 |
| 35 | #endif |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | #include "util.h" |
| 39 | #endif |
| 40 | #include <limits.h> |
| 41 | |
| 42 | #ifdef HAVE_FSTYPE |
| 43 | filesystem_t filesystems[] = { |
| 44 | { "9p", V9FS_MAGIC }, |
| 45 | { "autofs", AUTOFS_SUPER_MAGIC }, |
| 46 | { "bcachefs", 0xca451a4e }, |
| 47 | { "binfmt", BINFMTFS_MAGIC }, |
| 48 | { "bpf", BPF_FS_MAGIC }, |
| 49 | { "btrfs", BTRFS_SUPER_MAGIC }, |
| 50 | { "cgroup", CGROUP_SUPER_MAGIC }, |
| 51 | { "cgroup2", CGROUP2_SUPER_MAGIC }, |
| 52 | { "configfs", 0x62656570 }, |
| 53 | { "debugfs", DEBUGFS_MAGIC }, |
| 54 | { "devpts", DEVPTS_SUPER_MAGIC }, |
| 55 | { "efivarfs", EFIVARFS_MAGIC }, |
| 56 | { "exfat", EXFAT_SUPER_MAGIC }, |
| 57 | { "ext", EXT2_SUPER_MAGIC }, /* ext2/ext3/ext4 */ |
| 58 | { "f2fs", F2FS_SUPER_MAGIC }, |
| 59 | { "fuse", FUSE_SUPER_MAGIC }, |
| 60 | { "fusectl", 0x65735543 }, |
| 61 | { "hugetlbfs", HUGETLBFS_MAGIC }, |
| 62 | { "mqueue", 0x19800202 }, |
| 63 | { "nfs", NFS_SUPER_MAGIC }, |
| 64 | { "nilfs", NILFS_SUPER_MAGIC }, |
| 65 | { "overlayfs", OVERLAYFS_SUPER_MAGIC }, |
| 66 | { "proc", PROC_SUPER_MAGIC }, |
| 67 | { "pstore", PSTOREFS_MAGIC }, |
| 68 | { "ramfs", RAMFS_MAGIC }, |
| 69 | { "securityfs", SECURITYFS_MAGIC }, |
| 70 | { "selinuxfs", SELINUX_MAGIC }, |
| 71 | { "squashfs", SQUASHFS_MAGIC }, |
| 72 | { "sysfs", SYSFS_MAGIC }, |
| 73 | { "tmpfs", TMPFS_MAGIC }, |
| 74 | { "tracefs", TRACEFS_MAGIC }, |
| 75 | { "udf", UDF_SUPER_MAGIC }, |
| 76 | { "vfat", MSDOS_SUPER_MAGIC }, |
| 77 | { "xfs", XFS_SUPER_MAGIC }, |
| 78 | }; |
| 79 | int num_filesystems = sizeof(filesystems)/sizeof(filesystem_t); |
| 80 | #endif |
| 81 | |
| 82 | typedef struct { |
| 83 | char c; |
| 84 | char *s; |
| 85 | FT_TYPE r; |
| 86 | mode_t ft; |
| 87 | } f_type_t; |
| 88 | |
| 89 | static f_type_t filetypes[] = { |
| 90 | { 'f', "regular file", FT_REG, S_IFREG }, |
| 91 | { 'd', "directory", FT_DIR, S_IFDIR }, |
| 92 | #ifdef S_IFIFO |
| 93 | { 'p', "FIFO", FT_FIFO, S_IFIFO }, |
| 94 | #endif |
| 95 | { 'l', "symbolic link", FT_LNK, S_IFLNK }, |
| 96 | { 'b', "block device", FT_BLK, S_IFBLK }, |
| 97 | { 'c', "character device", FT_CHR, S_IFCHR }, |
| 98 | #ifdef S_IFSOCK |
| 99 | { 's', "socket", FT_SOCK, S_IFSOCK }, |
| 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_filetypes = sizeof(filetypes)/sizeof(f_type_t); |
| 110 | |
| 111 | char get_f_type_char_from_f_type(FT_TYPE r) { |
| 112 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 113 | if (r == filetypes[i].r) { |
| 114 | return filetypes[i].c; |
| 115 | } |
| 116 | } |
| 117 | return '?'; |
| 118 | } |
| 119 | |
| 120 | char *get_f_type_string_from_f_type(FT_TYPE r) { |
| 121 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 122 | if (r == filetypes[i].r) { |
| 123 | return filetypes[i].s; |
| 124 | } |
| 125 | } |
| 126 | return "unknown file type"; |
| 127 | } |
| 128 | |
| 129 | char get_f_type_char_from_perm(mode_t mode) { |
| 130 | mode_t ft = mode & S_IFMT; |
| 131 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 132 | if (ft == filetypes[i].ft) { |
| 133 | return filetypes[i].c; |
| 134 | } |
| 135 | } |
| 136 | return '?'; |
| 137 | } |
| 138 | |
| 139 | char *get_f_type_string_from_perm(mode_t mode) { |
| 140 | mode_t ft = mode & S_IFMT; |
| 141 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 142 | if (ft == filetypes[i].ft) { |
| 143 | return filetypes[i].s; |
| 144 | } |
| 145 | } |
| 146 | return "unknown file type"; |
| 147 | } |
| 148 | |
| 149 | FT_TYPE get_f_type_from_char(char c) { |
| 150 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 151 | if (c == filetypes[i].c) { |
| 152 | return filetypes[i].r; |
| 153 | } |
| 154 | } |
| 155 | return FT_NULL; |
| 156 | } |
| 157 | |
| 158 | FT_TYPE get_f_type_from_perm(mode_t mode) { |
| 159 | mode_t ft = mode & S_IFMT; |
| 160 | for (int i = 0 ; i < num_filetypes; ++i) { |
| 161 | if (ft == filetypes[i].ft) { |
| 162 | return filetypes[i].r; |
| 163 | } |
| 164 | } |
| 165 | return FT_NULL; |
| 166 | } |
| 167 | |
| 168 | #ifdef HAVE_FSTYPE |
| 169 | FS_TYPE get_fs_type_from_string(const char *fs_type_str) { |
| 170 | for (int i = 0 ; i < num_filesystems; ++i) { |
| 171 | if (strcmp(filesystems[i].str, fs_type_str) == 0) { |
| 172 | return filesystems[i].magic; |
| 173 | } |
| 174 | } |
| 175 | if (strncmp(fs_type_str, "0x", 2) == 0) { |
| 176 | long long ll; |
| 177 | char* e; |
| 178 | ll = strtoll(fs_type_str, &e, 16); |
| 179 | if (*e =='\0' && ll > 0 && ll <= UINT_MAX) { |
| 180 | return (FS_TYPE) ll; |
| 181 | } |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int generate_fs_type_string(FS_TYPE magic, char* str) { |
| 187 | for (int i = 0 ; i < num_filesystems; ++i) { |
| 188 | if (filesystems[i].magic == magic) { |
| 189 | size_t length = strlen(filesystems[i].str)+1; |
| 190 | if (str) { sprintf(str, "%s", filesystems[i].str); } |
| 191 | return length; |
| 192 | } |
| 193 | } |
| 194 | size_t length = snprintf(NULL, 0, "0x%lx", magic)+1; |
| 195 | if (str) { sprintf(str, "0x%lx", magic); } |
| 196 | return length; |
| 197 | } |
| 198 | |
| 199 | char *get_fs_type_string_from_magic(FS_TYPE magic) { |
| 200 | char *str = NULL; |
| 201 | int n = generate_fs_type_string(magic, str); |
| 202 | str = checked_malloc(n); |
| 203 | generate_fs_type_string(magic, str);; |
| 204 | return str; |
| 205 | } |
| 206 | #endif |