Skip to content

Commit

Permalink
Merge branch 'directory_config' into 'master'
Browse files Browse the repository at this point in the history
[libsailfishkeyprovider] Allow applications install keys into static dir. Fixes JB#46673

See merge request mer-core/libsailfishkeyprovider!6
  • Loading branch information
rainemak committed Nov 19, 2019
2 parents 9d6a83a + db60f32 commit 86d6dcb
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions lib/src/sailfishkeyprovider.c
Expand Up @@ -32,9 +32,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>

#define STOREDKEYS_WRITABLE_DIRECTORY "%s/.local/share/system/privileged/Keys"
#define STOREDKEYS_WRITABLE_INIFILE "%s/.local/share/system/privileged/Keys/storedkeys.ini"
#define STOREDKEYS_STATIC_CONFIG_DIR "/usr/share/libsailfishkeyprovider/storedkeys.d/"
#define STOREDKEYS_STATIC_INIFILE "/usr/share/libsailfishkeyprovider/storedkeys.ini"
#define STOREDKEYS_ENCODINGSECTION "encoding"
#define STOREDKEYS_ENCODEDKEYSSECTION "encodedkeys"
Expand All @@ -60,6 +62,54 @@ char * build_ini_entry_key(const char *first, const char *second)
return retn;
}

/* Try decode scheme from file*/
void try_read_decoding_scheme(const char *path, const char *psSchemeKey, const char *pSchemeKey, char** scheme)
{
*scheme = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODINGSECTION,
psSchemeKey);
if (*scheme == NULL) {
/* try the fallback key. */
*scheme = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODINGSECTION,
pSchemeKey);
}
}

/* Try decode key form file*/
void try_read_decoding_key(const char *path, const char *psKeyKey, const char *pKeyKey, char** key)
{
*key = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODINGSECTION,
psKeyKey);
if (*key == NULL) {
/* try the fallback key. */
*key = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODINGSECTION,
pKeyKey);
}
}

/* Try read key form file*/
void try_read_encoded_key(const char *path, const char *psKeyName, const char *pKeyName, char** key)
{
*key = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODEDKEYSSECTION,
psKeyName);
if (*key == NULL) {
/* try the fallback key. */
*key = SailfishKeyProvider_ini_read(
path,
STOREDKEYS_ENCODEDKEYSSECTION,
pKeyName);
}
}

/*
* Creates an encoded key given a \a keyValue, \a encodingScheme and
* \a encodingKey. Returns 0 on success, or -1 if any argument is
Expand Down Expand Up @@ -320,6 +370,27 @@ int SailfishKeyProvider_storedKey(
pKeyKey);
}

if (decodingScheme == NULL || decodingKey == NULL) {
DIR *dir;
struct dirent *ent;
if ((dir = opendir (STOREDKEYS_STATIC_CONFIG_DIR)) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
char path[PATH_MAX] = "";
strcat(path, STOREDKEYS_STATIC_CONFIG_DIR);
strcat(path, ent->d_name);

try_read_decoding_scheme(path, psSchemeKey, pSchemeKey, &decodingScheme);
try_read_decoding_key(path, psKeyKey, pKeyKey, &decodingKey);

if (decodingScheme || decodingKey) {
break;
}
}
closedir (dir);
}
}

if (decodingScheme == NULL || decodingKey == NULL) {
/* even the fallback keys were empty. Try reading from the static .ini file */
decodingScheme = SailfishKeyProvider_ini_read(
Expand Down Expand Up @@ -385,6 +456,26 @@ int SailfishKeyProvider_storedKey(
pKeyName);
}

if (encodedKeyValue == NULL) {
DIR *dir;
struct dirent *ent;
if ((dir = opendir (STOREDKEYS_STATIC_CONFIG_DIR)) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
char path[PATH_MAX] = "";
strcat(path, STOREDKEYS_STATIC_CONFIG_DIR);
strcat(path, ent->d_name);

try_read_encoded_key(path, psKeyName, pKeyName, &encodedKeyValue);

if (encodedKeyValue) {
break;
}
}
closedir (dir);
}
}

if (encodedKeyValue == NULL) {
/* even the fallback keys were empty */
free(psKeyName);
Expand Down

0 comments on commit 86d6dcb

Please sign in to comment.