2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-06-24 13:30:11 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2014 Kay Sievers
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
2014-06-24 13:30:11 +02:00
|
|
|
#include <stdlib.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <sys/stat.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <syslog.h>
|
2014-06-24 13:30:11 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-26 23:20:41 +01:00
|
|
|
#include "base-filesystem.h"
|
|
|
|
#include "fd-util.h"
|
2014-06-24 13:30:11 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-26 23:20:41 +01:00
|
|
|
#include "umask-util.h"
|
2015-10-27 00:42:07 +01:00
|
|
|
#include "user-util.h"
|
2014-06-24 13:30:11 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct BaseFilesystem {
|
|
|
|
const char *dir;
|
|
|
|
mode_t mode;
|
|
|
|
const char *target;
|
2014-07-01 12:12:40 +02:00
|
|
|
const char *exists;
|
2015-09-30 15:50:42 +02:00
|
|
|
bool ignore_failure;
|
2014-06-24 13:30:11 +02:00
|
|
|
} BaseFilesystem;
|
|
|
|
|
|
|
|
static const BaseFilesystem table[] = {
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "bin", 0, "usr/bin\0", NULL },
|
|
|
|
{ "lib", 0, "usr/lib\0", NULL },
|
2015-09-30 15:50:42 +02:00
|
|
|
{ "root", 0755, NULL, NULL, true },
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "sbin", 0, "usr/sbin\0", NULL },
|
2015-05-21 16:30:58 +02:00
|
|
|
{ "usr", 0755, NULL, NULL },
|
|
|
|
{ "var", 0755, NULL, NULL },
|
|
|
|
{ "etc", 0755, NULL, NULL },
|
2017-03-05 21:19:29 +01:00
|
|
|
{ "proc", 0755, NULL, NULL, true },
|
|
|
|
{ "sys", 0755, NULL, NULL, true },
|
|
|
|
{ "dev", 0755, NULL, NULL, true },
|
2014-07-01 11:42:58 +02:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
|
|
|
|
"usr/lib64\0", "ld-linux-x86-64.so.2" },
|
2014-07-01 11:42:58 +02:00
|
|
|
#endif
|
2014-06-24 13:30:11 +02:00
|
|
|
};
|
|
|
|
|
2015-05-21 16:30:58 +02:00
|
|
|
int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
|
2014-06-24 13:30:11 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
unsigned i;
|
2015-03-27 12:02:49 +01:00
|
|
|
int r = 0;
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(errno, "Failed to open root file system: %m");
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ELEMENTSOF(table); i ++) {
|
2014-09-03 13:22:40 +02:00
|
|
|
if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
|
|
|
|
continue;
|
|
|
|
|
2014-06-24 13:30:11 +02:00
|
|
|
if (table[i].target) {
|
2014-09-03 19:00:21 +02:00
|
|
|
const char *target = NULL, *s;
|
2014-07-01 11:42:58 +02:00
|
|
|
|
|
|
|
/* check if one of the targets exists */
|
|
|
|
NULSTR_FOREACH(s, table[i].target) {
|
|
|
|
if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-01 12:12:40 +02:00
|
|
|
/* check if a specific file exists at the target path */
|
|
|
|
if (table[i].exists) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
p = strjoin(s, "/", table[i].exists);
|
2014-07-01 12:12:40 +02:00
|
|
|
if (!p)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-01 11:42:58 +02:00
|
|
|
target = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target)
|
2014-06-24 13:30:11 +02:00
|
|
|
continue;
|
|
|
|
|
2014-07-01 11:42:58 +02:00
|
|
|
r = symlinkat(target, fd, table[i].dir);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (r < 0 && errno != EEXIST)
|
|
|
|
return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
|
2015-05-21 16:30:58 +02:00
|
|
|
|
2016-12-08 10:50:29 +01:00
|
|
|
if (uid_is_valid(uid) || gid_is_valid(gid)) {
|
2015-05-21 16:30:58 +02:00
|
|
|
if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:30:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_WITH_UMASK(0000)
|
|
|
|
r = mkdirat(fd, table[i].dir, table[i].mode);
|
2015-09-30 15:50:42 +02:00
|
|
|
if (r < 0 && errno != EEXIST) {
|
|
|
|
log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
|
|
|
|
"Failed to create directory at %s/%s: %m", root, table[i].dir);
|
|
|
|
|
|
|
|
if (!table[i].ignore_failure)
|
|
|
|
return -errno;
|
2017-03-16 03:30:15 +01:00
|
|
|
|
|
|
|
continue;
|
2015-09-30 15:50:42 +02:00
|
|
|
}
|
2015-05-21 16:30:58 +02:00
|
|
|
|
|
|
|
if (uid != UID_INVALID || gid != UID_INVALID) {
|
|
|
|
if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
|
|
|
|
}
|
2014-06-24 13:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|