| 1 | /** |
| 2 | * @file fsdev.c |
| 3 | * @brief fsdev API implementation |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * |
| 6 | */ |
| 7 | /* |
| 8 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 9 | * All Rights Reserved. |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2.1 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | * |
| 25 | * Authors: |
| 26 | * "Daniel Kopecek" <[email protected]> |
| 27 | */ |
| 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include <config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stddef.h> |
| 35 | #include <string.h> |
| 36 | #include <ctype.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <errno.h> |
| 41 | |
| 42 | #if defined(OS_LINUX) |
| 43 | # include <mntent.h> |
| 44 | # include <unistd.h> |
| 45 | # ifndef _PATH_MOUNTED |
| 46 | # define _PATH_MOUNTED MOUNTED |
| 47 | # endif |
| 48 | #elif defined(OS_SOLARIS) |
| 49 | # include <sys/mnttab.h> |
| 50 | # include <sys/mntent.h> |
| 51 | # include <sys/unistd.h> |
| 52 | #elif defined(OS_FREEBSD) || defined(OS_APPLE) |
| 53 | # include <sys/param.h> |
| 54 | # include <sys/ucred.h> |
| 55 | # include <sys/mount.h> |
| 56 | #elif defined(OS_AIX) |
| 57 | # include <unistd.h> |
| 58 | # include <mntent.h> |
| 59 | # include <fshelp.h> |
| 60 | # include <sys/vfs.h> |
| 61 | # include <sys/vmount.h> |
| 62 | # define _PATH_MOUNTED MOUNTED |
| 63 | #else |
| 64 | # error "Sorry, your OS isn't supported." |
| 65 | #endif |
| 66 | |
| 67 | #include "fsdev.h" |
| 68 | #include "common/util.h" |
| 69 | |
| 70 | /** |
| 71 | * Compare two dev_t variables. |
| 72 | */ |
| 73 | static int fsdev_cmp(const void *a, const void *b) |
| 74 | { |
| 75 | return memcmp(a, b, sizeof(dev_t)); |
| 76 | } |
| 77 | |
| 78 | #if defined(OS_LINUX) || defined(OS_AIX) |
| 79 | |
| 80 | #define DEVID_ARRAY_SIZE 16 |
| 81 | #define DEVID_ARRAY_ADD 8 |
| 82 | |
| 83 | #if defined(OS_LINUX) |
| 84 | static int is_local_fs(struct mntent *ment) |
| 85 | { |
| 86 | /* |
| 87 | * When type of the filesystem is autofs, it means the mtab entry |
| 88 | * describes the autofs configuration, which means ment->mnt_fsname |
| 89 | * is a path to the relevant autofs map, eg. /etc/auto.misc. In this |
| 90 | * situation, the following code which analyses ment->mnt_type would |
| 91 | * not work. When the filesystem handled by autofs is mounted, there |
| 92 | * is another different entry in mtab which contains the real block |
| 93 | * special device or remote filesystem in ment->mnt_fsname, and that |
| 94 | * will be parsed in a different call of this function. |
| 95 | */ |
| 96 | if (!strcmp(ment->mnt_type, "autofs")) { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * The following code is inspired by systemd, function fstype_is_network: |
| 102 | * https://github.com/systemd/systemd/blob/21fd6bc263f49b57867d90d2e1f9f255e5509134/src/basic/mountpoint-util.c#L290 |
| 103 | */ |
| 104 | |
| 105 | const char *fstype = ment->mnt_type; |
| 106 | if (oscap_str_startswith(fstype, "fuse.")) { |
| 107 | fstype += strlen("fuse."); |
| 108 | } |
| 109 | // OVAL's idea behind 'local' is to follow the 'df -l' behaviour |
| 110 | const char *pseudo_fs[] = { |
| 111 | "proc", |
| 112 | "sysfs", |
| 113 | NULL |
| 114 | }; |
| 115 | const char *network_fs[] = { |
| 116 | "afs", |
| 117 | "auristorfs", |
| 118 | "ceph", |
| 119 | "cifs", |
| 120 | "smb3", |
| 121 | "smbfs", |
| 122 | "sshfs", |
| 123 | "ncpfs", |
| 124 | "ncp", |
| 125 | "nfs", |
| 126 | "nfs4", |
| 127 | "gfs", |
| 128 | "gfs2", |
| 129 | "glusterfs", |
| 130 | "gpfs", |
| 131 | "pvfs2", /* OrangeFS */ |
| 132 | "ocfs2", |
| 133 | "lustre", |
| 134 | "davfs", |
| 135 | "panfs", |
| 136 | NULL |
| 137 | }; |
| 138 | for (int i = 0; pseudo_fs[i]; i++) { |
| 139 | if (!strcmp(pseudo_fs[i], fstype)) |
| 140 | return 0; |
| 141 | } |
| 142 | for (int i = 0; network_fs[i]; i++) { |
| 143 | if (!strcmp(network_fs[i], fstype)) |
| 144 | return 0; |
| 145 | } |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | #elif defined(OS_AIX) |
| 150 | static int is_local_fs(struct mntent *ment) |
| 151 | { |
| 152 | int i; |
| 153 | struct vfs_ent *e; |
| 154 | |
| 155 | static const int remote_fs_types[] = { |
| 156 | MNT_NFS, |
| 157 | MNT_NFS3, |
| 158 | MNT_AUTOFS, /* remote? */ |
| 159 | MNT_NFS4, |
| 160 | MNT_RFS4, |
| 161 | MNT_CIFS, |
| 162 | MNT_BADVFS /* end mark */ |
| 163 | }; |
| 164 | |
| 165 | for (i = 0; remote_fs_types[i] != MNT_BADVFS; i++) { |
| 166 | e = getvfsbytype(remote_fs_types[i]); |
| 167 | if (e != NULL |
| 168 | && e->vfsent_name != NULL |
| 169 | && strcmp(ment->mnt_type, e->vfsent_name) == 0) { |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | #endif /* OS_AIX */ |
| 178 | |
| 179 | static fsdev_t *__fsdev_init(fsdev_t *lfs) |
| 180 | { |
| 181 | int e; |
| 182 | FILE *fp; |
| 183 | size_t i; |
| 184 | |
| 185 | struct mntent *ment; |
| 186 | struct stat st; |
| 187 | |
| 188 | fp = setmntent(_PATH_MOUNTED, "r"); |
| 189 | if (fp == NULL) { |
| 190 | e = errno; |
| 191 | free(lfs); |
| 192 | errno = e; |
| 193 | return (NULL); |
| 194 | } |
| 195 | |
| 196 | lfs->ids = malloc(sizeof(dev_t) * DEVID_ARRAY_SIZE); |
| 197 | |
| 198 | if (lfs->ids == NULL) { |
| 199 | e = errno; |
| 200 | free(lfs); |
| 201 | endmntent(fp); |
| 202 | errno = e; |
| 203 | return (NULL); |
| 204 | } |
| 205 | |
| 206 | lfs->cnt = DEVID_ARRAY_SIZE; |
| 207 | i = 0; |
| 208 | |
| 209 | while ((ment = getmntent(fp)) != NULL) { |
| 210 | if (!is_local_fs(ment)) |
| 211 | continue; |
| 212 | if (stat(ment->mnt_dir, &st) != 0) |
| 213 | continue; |
| 214 | if (i >= lfs->cnt) { |
| 215 | lfs->cnt += DEVID_ARRAY_ADD; |
| 216 | void *new_ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt); |
| 217 | if (new_ids == NULL) { |
| 218 | e = errno; |
| 219 | free(lfs->ids); |
| 220 | free(lfs); |
| 221 | endmntent(fp); |
| 222 | errno = e; |
| 223 | return (NULL); |
| 224 | } |
| 225 | lfs->ids = new_ids; |
| 226 | } |
| 227 | memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t)); |
| 228 | } |
| 229 | |
| 230 | endmntent(fp); |
| 231 | |
| 232 | void *new_ids = realloc(lfs->ids, sizeof(dev_t) * i); |
| 233 | if (new_ids == NULL && i > 0) { |
| 234 | e = errno; |
| 235 | free(lfs->ids); |
| 236 | free(lfs); |
| 237 | errno = e; |
| 238 | return (NULL); |
| 239 | } |
| 240 | lfs->ids = new_ids; |
| 241 | lfs->cnt = i; |
| 242 | |
| 243 | return (lfs); |
| 244 | } |
| 245 | |
| 246 | #elif defined(OS_FREEBSD) || defined(OS_APPLE) |
| 247 | static fsdev_t *__fsdev_init(fsdev_t *lfs) |
| 248 | { |
| 249 | struct statfs *mntbuf = NULL; |
| 250 | struct stat st; |
| 251 | size_t i; |
| 252 | int e; |
| 253 | |
| 254 | lfs->cnt = getmntinfo(&mntbuf, MNT_NOWAIT); |
| 255 | lfs->ids = malloc(sizeof(dev_t) * lfs->cnt); |
| 256 | |
| 257 | for (i = 0; i < lfs->cnt; ++i) { |
| 258 | if (stat(mntbuf[i].f_mntonname, &st) != 0) |
| 259 | continue; |
| 260 | |
| 261 | memcpy(&(lfs->ids[i]), &st.st_dev, sizeof(dev_t)); |
| 262 | } |
| 263 | |
| 264 | if (i != lfs->cnt) { |
| 265 | void *new_ids = realloc(lfs->ids, sizeof(dev_t) * i); |
| 266 | if (new_ids == NULL) { |
| 267 | e = errno; |
| 268 | free(lfs->ids); |
| 269 | free(lfs); |
| 270 | errno = e; |
| 271 | return (NULL); |
| 272 | } |
| 273 | lfs->ids = new_ids; |
| 274 | lfs->cnt = i; |
| 275 | } |
| 276 | |
| 277 | return (lfs); |
| 278 | } |
| 279 | #elif defined(OS_SOLARIS) |
| 280 | |
| 281 | #define DEVID_ARRAY_SIZE 16 |
| 282 | #define DEVID_ARRAY_ADD 8 |
| 283 | |
| 284 | static fsdev_t *__fsdev_init(fsdev_t *lfs) |
| 285 | { |
| 286 | int e; |
| 287 | FILE *fp; |
| 288 | size_t i; |
| 289 | |
| 290 | struct mnttab mentbuf; |
| 291 | struct stat st; |
| 292 | |
| 293 | fp = fopen(MNTTAB, "r"); |
| 294 | if (fp == NULL) { |
| 295 | e = errno; |
| 296 | free(lfs); |
| 297 | errno = e; |
| 298 | return (NULL); |
| 299 | } |
| 300 | |
| 301 | lfs->ids = malloc(sizeof(dev_t) * DEVID_ARRAY_SIZE); |
| 302 | |
| 303 | if (lfs->ids == NULL) { |
| 304 | e = errno; |
| 305 | free(lfs); |
| 306 | fclose(fp); |
| 307 | errno = e; |
| 308 | return (NULL); |
| 309 | } |
| 310 | |
| 311 | lfs->cnt = DEVID_ARRAY_SIZE; |
| 312 | i = 0; |
| 313 | |
| 314 | while ((getmntent(fp, &mentbuf)) == 0) { |
| 315 | /* TODO: Is this check reliable? */ |
| 316 | if (stat(mentbuf.mnt_special, &st) == 0 && (st.st_mode & S_IFCHR)) { |
| 317 | |
| 318 | if (i >= lfs->cnt) { |
| 319 | lfs->cnt += DEVID_ARRAY_ADD; |
| 320 | void *new_ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt); |
| 321 | if (new_ids == NULL) { |
| 322 | e = errno; |
| 323 | free(lfs->ids); |
| 324 | free(lfs); |
| 325 | fclose(fp); |
| 326 | errno = e; |
| 327 | return (NULL); |
| 328 | } |
| 329 | lfs->ids = new_ids; |
| 330 | } |
| 331 | |
| 332 | memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t)); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | fclose(fp); |
| 337 | |
| 338 | void *new_ids = realloc(lfs->ids, sizeof(dev_t) * i); |
| 339 | if (new_ids == NULL) { |
| 340 | e = errno; |
| 341 | free(lfs->ids); |
| 342 | free(lfs); |
| 343 | errno = e; |
| 344 | return (NULL); |
| 345 | } |
| 346 | lfs->ids = new_ids; |
| 347 | lfs->cnt = i; |
| 348 | |
| 349 | return (lfs); |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | fsdev_t *fsdev_init() |
| 354 | { |
| 355 | fsdev_t *lfs; |
| 356 | |
| 357 | lfs = malloc(sizeof(fsdev_t)); |
| 358 | |
| 359 | if (lfs == NULL) |
| 360 | return (NULL); |
| 361 | |
| 362 | if (__fsdev_init(lfs) == NULL) |
| 363 | return (NULL); |
| 364 | |
| 365 | if (lfs->ids != NULL && lfs->cnt > 1) |
| 366 | qsort(lfs->ids, lfs->cnt, sizeof(dev_t), fsdev_cmp); |
| 367 | |
| 368 | return (lfs); |
| 369 | } |
| 370 | |
| 371 | void fsdev_free(fsdev_t * lfs) |
| 372 | { |
| 373 | if (lfs != NULL) { |
| 374 | free(lfs->ids); |
| 375 | free(lfs); |
| 376 | } |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | int fsdev_search(fsdev_t * lfs, void *id) |
| 381 | { |
| 382 | uint16_t w, s; |
| 383 | int cmp; |
| 384 | |
| 385 | if (!lfs) |
| 386 | return 1; |
| 387 | |
| 388 | w = lfs->cnt; |
| 389 | s = 0; |
| 390 | |
| 391 | while (w > 0) { |
| 392 | cmp = fsdev_cmp(id, &(lfs->ids[s + w / 2])); |
| 393 | if (cmp > 0) { |
| 394 | s += w / 2 + 1; |
| 395 | w = w - w / 2 - 1; |
| 396 | } else if (cmp < 0) { |
| 397 | w = w / 2; |
| 398 | } else { |
| 399 | return (1); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return (0); |
| 404 | } |
| 405 | |
| 406 | int fsdev_path(fsdev_t * lfs, const char *path) |
| 407 | { |
| 408 | struct stat st; |
| 409 | |
| 410 | if (stat(path, &st) != 0) |
| 411 | return (-1); |
| 412 | |
| 413 | return fsdev_search(lfs, &st.st_dev); |
| 414 | } |
| 415 | |
| 416 | int fsdev_fd(fsdev_t * lfs, int fd) |
| 417 | { |
| 418 | struct stat st; |
| 419 | |
| 420 | if (fstat(fd, &st) != 0) |
| 421 | return (-1); |
| 422 | |
| 423 | return fsdev_search(lfs, &st.st_dev); |
| 424 | } |