Skip to content

Commit

Permalink
MONITOR: move keyring setup code to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-tikhonov committed Oct 16, 2023
1 parent ce65061 commit bd89ca9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
30 changes: 3 additions & 27 deletions src/monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
#include "monitor/monitor.h"
#include "sss_iface/sss_iface_async.h"

#ifdef USE_KEYRING
#include <keyutils.h>
#endif

#ifdef HAVE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
Expand Down Expand Up @@ -1971,6 +1967,7 @@ static void monitor_restart_service(struct mt_svc *svc)
}

int bootstrap_monitor_process(void);
void setup_keyring(void);

int main(int argc, const char *argv[])
{
Expand Down Expand Up @@ -2081,30 +2078,9 @@ int main(int argc, const char *argv[])
/* default value of 'debug_prg_name' will be used */
DEBUG_INIT(debug_level, opt_logger);

#ifdef USE_KEYRING
/* Do this before all the forks, it sets the session key ring so all
* keys are private to the daemon and cannot be read by any other process
* tree */

/* make a new session */
ret = keyctl_join_session_keyring(NULL);
if (ret == -1) {
sss_log(SSS_LOG_ALERT,
"Could not create private keyring session. "
"If you store password there they may be easily accessible "
"to the root user. (%d, %s)", errno, strerror(errno));
}

ret = keyctl_setperm(KEY_SPEC_SESSION_KEYRING, KEY_POS_ALL);
if (ret == -1) {
sss_log(SSS_LOG_ALERT,
"Could not set permissions on private keyring. "
"If you store password there they may be easily accessible "
"to the root user. (%d, %s)", errno, strerror(errno));
}
#endif
setup_keyring();

/* Check if the SSSD is already running and for nscd conflicts */
/* Check if the SSSD is already running */
ret = check_file(SSSD_PIDFILE, 0, 0, S_IFREG|0600, 0, NULL, false);
if (ret == EOK) {
ret = check_pidfile(SSSD_PIDFILE);
Expand Down
31 changes: 31 additions & 0 deletions src/monitor/monitor_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <errno.h>
#include <string.h>
#include <grp.h>
#ifdef USE_KEYRING
#include <keyutils.h>
#endif

#include "util/util.h"

Expand Down Expand Up @@ -120,3 +123,31 @@ int bootstrap_monitor_process(void)

return 0;
}

void setup_keyring(void)
{
#ifdef USE_KEYRING
int ret;

/* Do this before all the forks, it sets the session key ring so all
* keys are private to the daemon and cannot be read by any other process
* tree */

/* make a new session */
ret = keyctl_join_session_keyring(NULL);
if (ret == -1) {
sss_log(SSS_LOG_ALERT,
"Could not create private keyring session. "
"If you store password there they may be easily accessible "
"to the root user. (%d, %s)", errno, strerror(errno));
}

ret = keyctl_setperm(KEY_SPEC_SESSION_KEYRING, KEY_POS_ALL);
if (ret == -1) {
sss_log(SSS_LOG_ALERT,
"Could not set permissions on private keyring. "
"If you store password there they may be easily accessible "
"to the root user. (%d, %s)", errno, strerror(errno));
}
#endif
}

0 comments on commit bd89ca9

Please sign in to comment.