2015-04-26 18:14:08 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
2018-08-10 20:15:21 +02:00
|
|
|
#include <stdio.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2015-04-26 18:14:08 +02:00
|
|
|
#include <fcntl.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2015-04-26 18:14:08 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2018-11-01 22:37:11 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-04-26 18:14:08 +02:00
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2018-10-26 21:34:00 +02:00
|
|
|
#include "src/common.h"
|
|
|
|
#include "src/storage.h"
|
2015-04-26 18:14:08 +02:00
|
|
|
|
|
|
|
#define STORAGE_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
|
|
|
|
#define STORAGE_FILE_MODE (S_IRUSR | S_IWUSR)
|
|
|
|
|
|
|
|
int create_dirs(const char *filename)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *dir;
|
|
|
|
const char *prev, *next;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (filename[0] != '/')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = stat(filename, &st);
|
|
|
|
if (!err && S_ISREG(st.st_mode))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dir = l_malloc(strlen(filename) + 1);
|
|
|
|
strcpy(dir, "/");
|
|
|
|
|
|
|
|
for (prev = filename; (next = strchr(prev + 1, '/')); prev = next) {
|
|
|
|
/* Skip consecutive '/' characters */
|
|
|
|
if (next - prev == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strncat(dir, prev + 1, next - prev);
|
|
|
|
|
|
|
|
if (mkdir(dir, STORAGE_DIR_MODE) == -1 && errno != EEXIST) {
|
|
|
|
l_free(dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l_free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t read_file(void *buffer, size_t len, const char *path_fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *path;
|
|
|
|
ssize_t r;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
va_start(ap, path_fmt);
|
|
|
|
path = l_strdup_vprintf(path_fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2019-05-24 18:52:40 +02:00
|
|
|
fd = L_TFR(open(path, O_RDONLY));
|
2015-04-26 18:14:08 +02:00
|
|
|
|
|
|
|
l_free(path);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
2019-05-24 18:52:40 +02:00
|
|
|
r = L_TFR(read(fd, buffer, len));
|
2015-04-26 18:14:08 +02:00
|
|
|
|
2019-05-24 18:52:40 +02:00
|
|
|
L_TFR(close(fd));
|
2015-04-26 18:14:08 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a buffer to a file in a transactionally safe form
|
|
|
|
*
|
|
|
|
* Given a buffer, write it to a file named after
|
|
|
|
* @path_fmt+args. However, to make sure the file contents are
|
|
|
|
* consistent (ie: a crash right after opening or during write()
|
|
|
|
* doesn't leave a file half baked), the contents are written to a
|
|
|
|
* file with a temporary name and when closed, it is renamed to the
|
|
|
|
* specified name (@path_fmt+args).
|
|
|
|
*/
|
|
|
|
ssize_t write_file(const void *buffer, size_t len,
|
|
|
|
const char *path_fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *tmp_path, *path;
|
|
|
|
ssize_t r;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
va_start(ap, path_fmt);
|
|
|
|
path = l_strdup_vprintf(path_fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
tmp_path = l_strdup_printf("%s.XXXXXX.tmp", path);
|
|
|
|
|
|
|
|
r = -1;
|
|
|
|
if (create_dirs(path) != 0)
|
|
|
|
goto error_create_dirs;
|
|
|
|
|
2019-05-28 21:06:22 +02:00
|
|
|
fd = L_TFR(mkostemps(tmp_path, 4, O_CLOEXEC));
|
2015-04-26 18:14:08 +02:00
|
|
|
if (fd == -1)
|
|
|
|
goto error_mkostemps;
|
|
|
|
|
2019-05-28 21:06:22 +02:00
|
|
|
r = L_TFR(write(fd, buffer, len));
|
|
|
|
L_TFR(close(fd));
|
2015-04-26 18:14:08 +02:00
|
|
|
|
|
|
|
if (r != (ssize_t) len) {
|
|
|
|
r = -1;
|
|
|
|
goto error_write;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that the file contents are written, rename to the real
|
|
|
|
* file name; this way we are uniquely sure that the whole
|
|
|
|
* thing is there.
|
2018-08-10 20:15:21 +02:00
|
|
|
* conserve @r's value from 'write'
|
2015-04-26 18:14:08 +02:00
|
|
|
*/
|
|
|
|
|
2018-08-10 20:15:21 +02:00
|
|
|
if (rename(tmp_path, path) == -1)
|
2015-04-26 18:14:08 +02:00
|
|
|
r = -1;
|
|
|
|
|
|
|
|
error_write:
|
2018-08-10 20:15:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
unlink(tmp_path);
|
2015-04-26 18:14:08 +02:00
|
|
|
error_mkostemps:
|
|
|
|
error_create_dirs:
|
|
|
|
l_free(tmp_path);
|
|
|
|
l_free(path);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:33:43 +02:00
|
|
|
char *storage_get_network_file_path(enum security type, const char *ssid)
|
2016-05-24 00:54:08 +02:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
const char *c;
|
|
|
|
char *hex = NULL;
|
|
|
|
|
|
|
|
for (c = ssid; *c; c++)
|
|
|
|
if (!isalnum(*c) && !strchr("-_ ", *c))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*c) {
|
|
|
|
hex = l_util_hexstring((const unsigned char *) ssid,
|
|
|
|
strlen(ssid));
|
|
|
|
|
2018-09-14 17:58:44 +02:00
|
|
|
path = l_strdup_printf(DAEMON_STORAGEDIR "/=%s.%s", hex,
|
2018-08-01 03:56:41 +02:00
|
|
|
security_to_str(type));
|
2016-05-24 00:54:08 +02:00
|
|
|
|
|
|
|
l_free(hex);
|
|
|
|
} else
|
2018-09-14 17:58:44 +02:00
|
|
|
path = l_strdup_printf(DAEMON_STORAGEDIR "/%s.%s", ssid,
|
2018-08-01 03:56:41 +02:00
|
|
|
security_to_str(type));
|
2016-05-24 00:54:08 +02:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:59:59 +02:00
|
|
|
const char *storage_network_ssid_from_path(const char *path,
|
|
|
|
enum security *type)
|
2016-06-09 19:55:24 +02:00
|
|
|
{
|
|
|
|
const char *filename = strrchr(path, '/');
|
|
|
|
const char *c, *end;
|
|
|
|
char *decoded;
|
|
|
|
static char buf[67];
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
filename++; /* Skip the / */
|
|
|
|
else
|
|
|
|
filename = path;
|
|
|
|
|
|
|
|
end = strchr(filename, '.');
|
|
|
|
|
|
|
|
if (!end || !security_from_str(end + 1, type))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (filename[0] != '=') {
|
|
|
|
if (end == filename || end - filename > 32)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (c = filename; c < end; c++)
|
|
|
|
if (!isalnum(*c) && !strchr("-_ ", *c))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (c < end)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(buf, filename, end - filename);
|
|
|
|
buf[end - filename] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end - filename <= 1 || end - filename > 65)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(buf, filename + 1, end - filename - 1);
|
|
|
|
buf[end - filename - 1] = '0';
|
|
|
|
buf[end - filename + 0] = '0';
|
|
|
|
buf[end - filename + 1] = '\0';
|
|
|
|
|
|
|
|
decoded = (char *) l_util_from_hexstring(buf, NULL);
|
|
|
|
if (!decoded)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!l_utf8_validate(decoded, (end - filename) / 2, NULL)) {
|
|
|
|
l_free(decoded);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(buf, decoded);
|
|
|
|
l_free(decoded);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
struct l_settings *storage_network_open(enum security type, const char *ssid)
|
2015-04-26 18:14:08 +02:00
|
|
|
{
|
|
|
|
struct l_settings *settings;
|
|
|
|
char *path;
|
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
if (ssid == NULL)
|
2015-04-26 18:14:08 +02:00
|
|
|
return NULL;
|
|
|
|
|
2018-08-09 16:33:43 +02:00
|
|
|
path = storage_get_network_file_path(type, ssid);
|
2015-04-26 18:14:08 +02:00
|
|
|
settings = l_settings_new();
|
|
|
|
|
2017-10-31 23:02:58 +01:00
|
|
|
if (!l_settings_load_from_file(settings, path)) {
|
|
|
|
l_settings_free(settings);
|
2018-06-15 03:53:18 +02:00
|
|
|
settings = NULL;
|
2017-10-31 23:02:58 +01:00
|
|
|
}
|
|
|
|
|
2015-04-26 18:14:08 +02:00
|
|
|
l_free(path);
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
int storage_network_touch(enum security type, const char *ssid)
|
2015-06-17 02:14:33 +02:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int ret;
|
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
if (ssid == NULL)
|
2015-06-17 02:14:33 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-08-09 16:33:43 +02:00
|
|
|
path = storage_get_network_file_path(type, ssid);
|
2015-06-17 02:14:33 +02:00
|
|
|
ret = utimensat(0, path, NULL, 0);
|
|
|
|
l_free(path);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
void storage_network_sync(enum security type, const char *ssid,
|
2015-04-26 18:14:08 +02:00
|
|
|
struct l_settings *settings)
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
size_t length = 0;
|
2016-05-24 00:54:08 +02:00
|
|
|
char *path;
|
2015-04-26 18:14:08 +02:00
|
|
|
|
2018-08-09 16:33:43 +02:00
|
|
|
path = storage_get_network_file_path(type, ssid);
|
2015-04-26 18:14:08 +02:00
|
|
|
data = l_settings_to_data(settings, &length);
|
2016-05-24 00:54:08 +02:00
|
|
|
write_file(data, length, "%s", path);
|
2015-04-26 18:14:08 +02:00
|
|
|
l_free(data);
|
2016-05-24 00:54:08 +02:00
|
|
|
l_free(path);
|
2015-04-26 18:14:08 +02:00
|
|
|
}
|
2016-06-07 05:29:59 +02:00
|
|
|
|
2018-08-01 03:56:41 +02:00
|
|
|
int storage_network_remove(enum security type, const char *ssid)
|
2016-06-07 05:29:59 +02:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int ret;
|
|
|
|
|
2018-08-09 16:33:43 +02:00
|
|
|
path = storage_get_network_file_path(type, ssid);
|
2016-06-07 05:29:59 +02:00
|
|
|
ret = unlink(path);
|
|
|
|
l_free(path);
|
|
|
|
|
|
|
|
return ret < 0 ? -errno : 0;
|
|
|
|
}
|
2019-03-15 19:32:01 +01:00
|
|
|
|
|
|
|
static const char *known_freq_file_path =
|
|
|
|
DAEMON_STORAGEDIR "/.known_network.freq";
|
|
|
|
|
|
|
|
struct l_settings *storage_known_frequencies_load(void)
|
|
|
|
{
|
|
|
|
struct l_settings *known_freqs;
|
|
|
|
|
|
|
|
known_freqs = l_settings_new();
|
|
|
|
|
|
|
|
if (!l_settings_load_from_file(known_freqs, known_freq_file_path)) {
|
|
|
|
l_settings_free(known_freqs);
|
|
|
|
known_freqs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return known_freqs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void storage_known_frequencies_sync(struct l_settings *known_freqs)
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!known_freqs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
data = l_settings_to_data(known_freqs, &len);
|
|
|
|
write_file(data, len, "%s", known_freq_file_path);
|
|
|
|
l_free(data);
|
|
|
|
}
|