Add the ability to set the group to run as.

This commit is contained in:
david reid 2017-08-25 23:17:17 +01:00 committed by Justin Maggard
parent 6819c6a186
commit 7a27ce3077

View File

@ -68,6 +68,7 @@
#include <limits.h>
#include <libgen.h>
#include <pwd.h>
#include <grp.h>
#include "config.h"
@ -510,6 +511,7 @@ init(int argc, char **argv)
int ifaces = 0;
media_types types;
uid_t uid = 0;
gid_t gid = 0;
/* first check if "-f" option is used */
for (i=2; i<argc; i++)
@ -857,7 +859,7 @@ init(int argc, char **argv)
case 'R':
snprintf(buf, sizeof(buf), "rm -rf %s/files.db %s/art_cache", db_path, db_path);
if (system(buf) != 0)
DPRINTF(E_FATAL, L_GENERAL, "Failed to clean old file cache. EXITING\n");
DPRINTF(E_FATAL, L_GENERAL, "Failed to clean old file cache %s. EXITING\n", db_path);
break;
case 'u':
if (i+1 != argc)
@ -876,6 +878,22 @@ init(int argc, char **argv)
else
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break;
case 'g':
if (i+1 != argc)
{
i++;
gid = strtoul(argv[i], &string, 0);
if (*string)
{
/* Symbolic group given, not GID. */
struct group *grp = getgrnam(argv[i]);
if (!grp)
DPRINTF(E_FATAL, L_GENERAL, "Bad group '%s'.\n", argv[i]);
gid = grp->gr_gid;
}
}
else
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break;
#ifdef __linux__
case 'S':
@ -896,7 +914,7 @@ init(int argc, char **argv)
{
printf("Usage:\n\t"
"%s [-d] [-v] [-f config_file] [-p port]\n"
"\t\t[-i network_interface] [-u uid_to_run_as]\n"
"\t\t[-i network_interface] [-u uid_to_run_as] [-g group_to_run_as]\n"
"\t\t[-t notify_interval] [-P pid_filename]\n"
"\t\t[-s serial] [-m model_number]\n"
#ifdef __linux__
@ -997,6 +1015,10 @@ init(int argc, char **argv)
db_path, uid, strerror(errno));
}
if (gid > 0 && setgid(gid) == -1)
DPRINTF(E_FATAL, L_GENERAL, "Failed to switch to gid '%d'. [%s] EXITING.\n",
gid, strerror(errno));
if (uid > 0 && setuid(uid) == -1)
DPRINTF(E_FATAL, L_GENERAL, "Failed to switch to uid '%d'. [%s] EXITING.\n",
uid, strerror(errno));