2018-10-10 12:35:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
|
|
|
*
|
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
2019-03-29 15:26:02 +00:00
|
|
|
#include <libgen.h>
|
2018-10-10 12:35:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/errno.h>
|
2019-03-29 15:26:02 +00:00
|
|
|
#include <sys/stat.h>
|
2018-10-10 12:35:40 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2019-03-29 15:26:02 +00:00
|
|
|
#include "profile.h"
|
2018-10-10 12:35:40 +00:00
|
|
|
|
|
|
|
static int mkpath(const char *path, mode_t mode) {
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!path) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (!stat(strdup(path), &sb)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dirname has multiple incompatible implementations.
|
|
|
|
// Some modify input argument some share output buffer.
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *subpath = strdup(dirname(pathdup));
|
|
|
|
mkpath(subpath, mode);
|
|
|
|
free(pathdup);
|
|
|
|
free(subpath);
|
|
|
|
return mkdir(path, mode);
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void profile_init(void) {
|
2019-03-29 15:26:02 +00:00
|
|
|
const char *dir = profile_dir();
|
|
|
|
if (mkpath(dir, 0755)) {
|
|
|
|
perror(dir);
|
|
|
|
printf("!!! Unable to initialize profile directory `%s`. Quitting\n", dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Profile directory: %s\n", dir);
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *profile_dir(void) {
|
2019-03-29 15:26:02 +00:00
|
|
|
static const char *_profile_dir;
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (_profile_dir) {
|
|
|
|
return _profile_dir;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
char *trezor_profile = getenv("TREZOR_PROFILE");
|
|
|
|
if (!trezor_profile || strlen(trezor_profile) < 1) {
|
|
|
|
trezor_profile = PROFILE_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *path;
|
|
|
|
if (trezor_profile[0] == '/') {
|
2018-10-10 12:35:40 +00:00
|
|
|
// TREZOR_PROFILE is a full path to profile directory
|
2019-03-29 15:26:02 +00:00
|
|
|
path = strdup(trezor_profile);
|
|
|
|
} else {
|
2018-10-10 12:35:40 +00:00
|
|
|
// TREZOR_PROFILE is just a profile name and will be put in ~/.trezoremu/
|
2019-03-29 15:26:02 +00:00
|
|
|
int print_length = asprintf(&path, "%s/" PROFILE_HOMEDOT "/%s",
|
|
|
|
getenv("HOME"), trezor_profile);
|
|
|
|
if (print_length == -1) {
|
|
|
|
path = NULL;
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!path) { // last resort fallback
|
|
|
|
path = PROFILE_DEFAULT;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
_profile_dir = path;
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return _profile_dir;
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *profile_flash_path(void) {
|
2019-03-29 15:26:02 +00:00
|
|
|
static char *_flash_path;
|
|
|
|
if (_flash_path) {
|
|
|
|
return _flash_path;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (asprintf(&_flash_path, "%s/trezor.flash", profile_dir()) < 0) {
|
|
|
|
_flash_path = NULL;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!_flash_path) { // last resort fallback
|
|
|
|
_flash_path = PROFILE_DEFAULT "/trezor.flash";
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return _flash_path;
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *profile_sdcard_path(void) {
|
2019-03-29 15:26:02 +00:00
|
|
|
static char *_sdcard_path;
|
|
|
|
if (_sdcard_path) {
|
|
|
|
return _sdcard_path;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (asprintf(&_sdcard_path, "%s/trezor.sdcard", profile_dir()) < 0) {
|
|
|
|
_sdcard_path = NULL;
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!_sdcard_path) { // last resort fallback
|
|
|
|
_sdcard_path = PROFILE_DEFAULT "/trezor.sdcard";
|
|
|
|
}
|
2018-10-10 12:35:40 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return _sdcard_path;
|
2018-10-10 12:35:40 +00:00
|
|
|
}
|