Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[utility] Add functions for probing encrypted home
In some situations DSME needs to act differently depending on whether
home partition is encrypted or not.

Implement a predicate function for testing encrypted home and make
it available for dsme-server process and plugins is loads.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed May 16, 2019
1 parent 637d288 commit f662b6c
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 4 deletions.
14 changes: 14 additions & 0 deletions .depend
Expand Up @@ -124,6 +124,16 @@ dsme/timers.pic.o:\
include/dsme/modules.h\
include/dsme/timers.h\

dsme/utility.o:\
dsme/utility.c\
include/dsme/logging.h\
dsme/utility.h\

dsme/utility.pic.o:\
dsme/utility.c\
include/dsme/logging.h\
dsme/utility.h\

getbootstate/getbootstate.o:\
getbootstate/getbootstate.c\

Expand Down Expand Up @@ -167,6 +177,8 @@ modules/alarmtracker.pic.o:\
modules/batterytracker.o:\
modules/batterytracker.c\
include/dsme/logging.h\
include/dsme/modulebase.h\
include/dsme/modules.h\
include/dsme/modules.h\
include/dsme/timers.h\
modules/dbusproxy.h\
Expand All @@ -175,6 +187,8 @@ modules/batterytracker.o:\
modules/batterytracker.pic.o:\
modules/batterytracker.c\
include/dsme/logging.h\
include/dsme/modulebase.h\
include/dsme/modules.h\
include/dsme/modules.h\
include/dsme/timers.h\
modules/dbusproxy.h\
Expand Down
4 changes: 4 additions & 0 deletions Makefile.custom
Expand Up @@ -219,6 +219,7 @@ PKG_NAMES += libiphb
PKG_NAMES += systemd
PKG_NAMES += libsystemd
PKG_NAMES += libngf0
PKG_NAMES += libcryptsetup

maintenance = normalize clean distclean mostlyclean protos-post
intersection = $(strip $(foreach w,$1, $(filter $w,$2)))
Expand Down Expand Up @@ -263,6 +264,7 @@ dsme-server-OBJS += dsme/mainloop.o
dsme-server-OBJS += dsme/modulebase.o
dsme-server-OBJS += dsme/oom.o
dsme-server-OBJS += dsme/timers.o
dsme-server-OBJS += dsme/utility.o

dsme/dsme-server : LDFLAGS += -rdynamic

Expand Down Expand Up @@ -324,6 +326,7 @@ getbootstate/getbootstate: getbootstate/getbootstate.o
CLEAN_SOURCES += dsme/dsme-rd-mode.c
CLEAN_SOURCES += dsme/dsmesock.c
CLEAN_SOURCES += dsme/timers.c
CLEAN_SOURCES += dsme/utility.c
CLEAN_SOURCES += modules/abootsettings.c
CLEAN_SOURCES += modules/batterytracker.c
CLEAN_SOURCES += modules/dbusautoconnector.c
Expand All @@ -345,6 +348,7 @@ CLEAN_SOURCES += util/dsmetool.c
CLEAN_HEADERS += dsme/dsme-wdd-wd.h
CLEAN_HEADERS += dsme/dsme-wdd.h
CLEAN_HEADERS += dsme/dsme-server.h
CLEAN_HEADERS += dsme/utility.h
CLEAN_HEADERS += dsme/dsme-rd-mode.h
CLEAN_HEADERS += modules/powerontimer_backend.h
CLEAN_HEADERS += modules/thermalmanager.h
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -203,6 +203,7 @@ AC_CONFIG_MACRO_DIR([m4])
AM_PROG_CC_C_O()

# Pkgconf checks
PKG_CHECK_MODULES(LIBCRYPTSETUP, libcryptsetup)
PKG_CHECK_MODULES(GLIB, glib-2.0)
PKG_CHECK_MODULES(DBUS, dbus-1)
PKG_CHECK_MODULES(DBUSGLIB, dbus-glib-1)
Expand Down
9 changes: 5 additions & 4 deletions dsme/Makefile.am
Expand Up @@ -38,15 +38,17 @@ dsme_LDADD = $(LIBSYSTEMD_LIBS)
# dsme-server
#
dsme_server_SOURCES = dsme-server.c modulebase.c timers.c logging.c oom.c \
mainloop.c dsmesock.c dsme-rd-mode.c
mainloop.c dsmesock.c dsme-rd-mode.c utility.c

dsme_server_LDFLAGS = $(AM_LDFLAGS) -rdynamic `pkg-config --libs gthread-2.0` -Wl,--as-needed
dsme_server_CPPFLAGS = $(CPP_GENFLAGS) $(GLIB_CFLAGS)
dsme_server_LDADD = $(GLIB_LIBS) -ldsme -ldl
dsme_server_CPPFLAGS = $(CPP_GENFLAGS) $(GLIB_CFLAGS) $(LIBCRYPTSETUP_CFLAGS)
dsme_server_LDADD = $(GLIB_LIBS) $(LIBCRYPTSETUP_LIBS) -ldsme -ldl

#
# Headers used by all
#
noinst_HEADERS = dsme-rd-mode.h \
utility.h \
../include/dsme/mainloop.h \
../include/dsme/modulebase.h \
../include/dsme/modules.h \
Expand All @@ -55,7 +57,6 @@ noinst_HEADERS = dsme-rd-mode.h \
../include/dsme/oom.h \
../include/dsme/timers.h


#
# Data
#
Expand Down
106 changes: 106 additions & 0 deletions dsme/utility.c
@@ -0,0 +1,106 @@
/**
* @file utility.h
*
* Generic functions needed by dsme core and/or multiple plugings.
*
* <p>
* Copyright (C) 2019 Jolla Ltd.
*
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This file is part of Dsme.
*
* Dsme is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* Dsme 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 Dsme. If not, see <http://www.gnu.org/licenses/>.
*/

#include "utility.h"

#include "../include/dsme/logging.h"

#include <string.h>

#include <libcryptsetup.h>

/* ========================================================================= *
* Prototypes
* ========================================================================= */

/* ------------------------------------------------------------------------- *
* UTILITY
* ------------------------------------------------------------------------- */

static void dsme_free_crypt_device (struct crypt_device *cdev);
static struct crypt_device *dsme_get_crypt_device_for_home(void);
bool dsme_home_is_encrypted (void);

/* ========================================================================= *
* Probing for encrypted home partition
* ========================================================================= */

static const char homeLuksContainer[] = "/dev/sailfish/home";

static void
dsme_free_crypt_device(struct crypt_device *cdev)
{
if( cdev )
crypt_free(cdev);
}

static struct crypt_device *
dsme_get_crypt_device_for_home(void)
{
struct crypt_device *cdev = 0;
struct crypt_device *work = 0;

int rc;

if( (rc = crypt_init(&work, homeLuksContainer)) < 0 ) {
dsme_log(LOG_WARNING, "%s: could not initialize crypt device: %s",
homeLuksContainer, strerror(-rc));
goto EXIT;
}

if( (rc = crypt_load(work, 0, 0)) < 0 ) {
dsme_log(LOG_WARNING, "%s: could not load crypt device info: %s",
homeLuksContainer, strerror(-rc));
goto EXIT;
}

cdev = work, work = 0;

EXIT:

dsme_free_crypt_device(work);

return cdev;
}

bool
dsme_home_is_encrypted(void)
{
static bool is_encrypted = false;
static bool was_probed = false;

if( !was_probed ) {
was_probed = true;

struct crypt_device *cdev = dsme_get_crypt_device_for_home();
is_encrypted = (cdev != 0);
dsme_free_crypt_device(cdev);

dsme_log(LOG_WARNING, "HOME is encrypted: %s",
is_encrypted ? "True" : "False");
}

return is_encrypted;
}
41 changes: 41 additions & 0 deletions dsme/utility.h
@@ -0,0 +1,41 @@
/**
* @file utility.h
*
* Generic functions needed by dsme core and/or multiple plugings.
*
* <p>
* Copyright (C) 2019 Jolla Ltd.
*
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This file is part of Dsme.
*
* Dsme is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* Dsme 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 Dsme. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DSME_UTILITY_H_
# define DSME_UTILITY_H_

# include <stdbool.h>

/* ========================================================================= *
* Prototypes
* ========================================================================= */

/* ------------------------------------------------------------------------- *
* UTILITY
* ------------------------------------------------------------------------- */

bool dsme_home_is_encrypted(void);

#endif /* DSME_UTILITY_H_ */
1 change: 1 addition & 0 deletions rpm/dsme.spec
Expand Up @@ -26,6 +26,7 @@ BuildRequires: python
BuildRequires: autoconf
BuildRequires: libtool
BuildRequires: automake
BuildRequires: pkgconfig(libcryptsetup)

%description
Device State Management Entity (with debug symbols). This package contains the Device State Management Entity which provides state management features such as service monitoring, process watchdog and inactivity tracking.
Expand Down

0 comments on commit f662b6c

Please sign in to comment.