Skip to content

Commit

Permalink
[main] Allow specifying the group to use in the configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Ryl669 committed Oct 22, 2023
1 parent 912e00d commit e976234
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Note: if for some reason you've installed the avahi port, you need to
add `--without-avahi` to configure above.

Edit `/usr/local/etc/owntone.conf` and change the `uid` to a nice
system daemon (eg: unknown), and run the following:
system daemon (eg: unknown) and optionnally the `gid`, and run the following:

```bash
sudo mkdir -p /usr/local/var/run
Expand Down
5 changes: 5 additions & 0 deletions owntone.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ general {
# Make sure the user has read access to the library directories you set
# below, and full access to the databases, log and local audio
uid = "@OWNTONE_USER@"

# User group
# The library might be shared by multiple users that all belong to this group.
# Default to the first group declared for the user
# gid = "@OWNTONE_USER@"

# Database location
# db_path = "@localstatedir@/cache/@PACKAGE@/songs3.db"
Expand Down
1 change: 1 addition & 0 deletions src/conffile.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static int cb_loglevel(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *resu
static cfg_opt_t sec_general[] =
{
CFG_STR("uid", "nobody", CFGF_NONE),
CFG_STR("gid", "", CFGF_NONE),
CFG_STR("db_path", STATEDIR "/cache/" PACKAGE "/songs3.db", CFGF_NONE),
CFG_STR("db_backup_path", NULL, CFGF_NONE),
CFG_STR("logfile", STATEDIR "/log/" PACKAGE ".log", CFGF_NONE),
Expand Down
24 changes: 20 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ daemonize(bool background, char *pidfile)
int fd;
int ret;
char *runas;
char *runasgroup;

if (background)
{
Expand Down Expand Up @@ -189,12 +190,27 @@ daemonize(bool background, char *pidfile)
{
runas = cfg_getstr(cfg_getsec(cfg, "general"), "uid");

ret = initgroups(runas, runas_gid);
if (ret != 0)
runasgroup = cfg_getstr(cfg_getsec(cfg, "general"), "gid");
if (strlen(runasgroup))
{
struct group * grp = getgrnam(runasgroup);
if (grp != NULL)
runas_gid = grp->gr_gid;
else
{
DPRINTF(E_FATAL, L_MAIN, "getgrnam() failed: %s\n", strerror(errno));

return -1;
}
} else
{
DPRINTF(E_FATAL, L_MAIN, "initgroups() failed: %s\n", strerror(errno));
ret = initgroups(runas, runas_gid);
if (ret != 0)
{
DPRINTF(E_FATAL, L_MAIN, "initgroups() failed: %s\n", strerror(errno));

return -1;
return -1;
}
}

ret = setegid(runas_gid);
Expand Down

0 comments on commit e976234

Please sign in to comment.