Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Added support for user/group settings. #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ use the provided `init_script` file as a starting point.
For systems running systemd, there is a spacenavd.service file under
`contrib/systemd`. Follow your system documentation for how to use it.

Spacenavd (in daemonmode) can drop root privileges and run under an other user or
group. For this, start spacenavd with -u <username> / -g <group> or specify the
user / group in `/etc/spnavrc`.

Configuration
-------------
The spacenavd daemon reads a number of options from `/etc/spnavrc`. If
Expand Down
9 changes: 9 additions & 0 deletions doc/example-spnavrc
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@
# Combine multiple options by listing them on the same line.
#
#log = file, syslog


# User and group settings
#
# When a user or a group is specified, spacenavd drops for serveral functions
# the root privileges and run this codeparts under the given user- and groupname.
#
#user = <valid-username>
#group = <valid-groupname>
27 changes: 26 additions & 1 deletion src/cfgfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "cfgfile.h"
#include "logger.h"
#include "spnavd.h"
#include "userpriv.h"

enum {TX, TY, TZ, RX, RY, RZ};

Expand Down Expand Up @@ -84,6 +85,8 @@ int read_cfg(const char *fname, struct cfg *cfg)
int num_devid = 0;
/*int num_devnames = 0;*/

int username_from_cfg = 0, groupname_from_cfg = 0;

default_cfg(cfg);

logmsg(LOG_INFO, "reading config file: %s\n", fname);
Expand Down Expand Up @@ -313,7 +316,23 @@ int read_cfg(const char *fname, struct cfg *cfg)
continue;
}

} else {
} else if(strcmp(key_str, "user") == 0) {
if(user_set_by_cmdline() == 1) {
logmsg(LOG_WARNING, "started with -u option - ignoring user setting in configfile\n");
} else if(set_runas_uid (val_str) == 0) {
logmsg(LOG_WARNING, "invalid configuration value for %s, expected a valid username (%s doesn't exists) - resuming with invoked user\n", key_str, val_str);
} else {
username_from_cfg = 1;
}
} else if(strcmp(key_str, "group") == 0) {
if(group_set_by_cmdline() == 1) {
logmsg(LOG_WARNING, "started with -g option - ignoring group setting in configfile\n");
} else if(set_runas_gid (val_str) == 0) {
logmsg(LOG_WARNING, "invalid configuration value for %s, expected a valid groupname (%s doesn't exists) - resuming with invoked group\n", key_str, val_str);
} else {
groupname_from_cfg = 1;
}
} else {
logmsg(LOG_WARNING, "unrecognized config option: %s\n", key_str);
}
}
Expand All @@ -325,6 +344,12 @@ int read_cfg(const char *fname, struct cfg *cfg)
fcntl(fileno(fp), F_SETLK, &flk);

fclose(fp);

if(username_from_cfg || groupname_from_cfg) {
test_initial_user_privileges();
start_daemon_privileges();
}

return 0;
}

Expand Down
7 changes: 7 additions & 0 deletions src/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "userpriv.h"
#include "dev.h"
#include "dev_usb.h"
#include "dev_serial.h"
Expand All @@ -42,6 +43,9 @@ int init_devices(void)
int i, device_added = 0;
struct usb_device_info *usblist, *usbdev;

/* restore initial uid / gid */
stop_daemon_privileges();

/* try to open a serial device if specified in the config file */
if(cfg.serial_dev[0]) {
if(!dev_path_in_use(cfg.serial_dev)) {
Expand Down Expand Up @@ -86,6 +90,9 @@ int init_devices(void)

free_usb_devices_list(usblist);

/* use daemon uid / gid again */
start_daemon_privileges();

if(!usblist) {
logmsg(LOG_ERR, "failed to find any supported devices\n");
return -1;
Expand Down
11 changes: 11 additions & 0 deletions src/proto_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sys/stat.h>
#include <sys/un.h>
#include "proto_unix.h"
#include "userpriv.h"
#include "spnavd.h"

enum {
Expand All @@ -44,8 +45,11 @@ int init_unix(void)

if(lsock >= 0) return 0;

stop_daemon_privileges();

if((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
logmsg(LOG_ERR, "failed to create socket: %s\n", strerror(errno));
start_daemon_privileges();
return -1;
}

Expand All @@ -60,9 +64,12 @@ int init_unix(void)
if(bind(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
logmsg(LOG_ERR, "failed to bind unix socket: %s: %s\n", SOCK_NAME, strerror(errno));
close(s);
start_daemon_privileges();
return -1;
}

start_daemon_privileges();

umask(prev_umask);

if(listen(s, 8) == -1) {
Expand All @@ -79,10 +86,14 @@ int init_unix(void)
void close_unix(void)
{
if(lsock != -1) {
stop_daemon_privileges();

close(lsock);
lsock = -1;

unlink(SOCK_NAME);

start_daemon_privileges();
}
}

Expand Down
65 changes: 64 additions & 1 deletion src/spnavd.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sys/un.h>
#include "spnavd.h"
#include "logger.h"
#include "userpriv.h"
#include "dev.h"
#include "hotplug.h"
#include "client.h"
Expand All @@ -48,9 +49,20 @@ static char *fix_path(char *str);
static char *cfgfile = DEF_CFGFILE;
static char *logfile = DEF_LOGFILE;

/* struct for privilege changes */
userid_struct *userids = NULL;

int main(int argc, char **argv)
{
int i, pid, ret, become_daemon = 1;
int use_username = 0;
int use_groupname = 0;

/* allocate memory the userprivilege struct */
userids = malloc(sizeof(userid_struct));

/* prefill the userid struct */
set_initial_user_privileges();

for(i=1; i<argc; i++) {
if(argv[i][0] == '-') {
Expand Down Expand Up @@ -87,6 +99,34 @@ int main(int argc, char **argv)
verbose = 1;
break;

case 'u':
/* optional username for daemonize */
use_username = 1;
if(!argv[++i]) {
fprintf(stderr, "-u must be followed by a username\n");
return 1;
}
if(set_runas_uid(argv[i]) == 0)
{
fprintf(stderr, "Invalid username: %s\n", argv[i]);
return 1;
}
break;

case 'g':
/* optional groupname for daemonize */
use_groupname = 1;
if(!argv[++i]) {
fprintf(stderr, "-g must be followed by a groupname\n");
return 1;
}
if(set_runas_gid(argv[i]) == 0)
{
fprintf(stderr, "Invalid groupname: %s\n", argv[i]);
return 1;
}
break;

case 'V':
printf("spacenavd " VERSION "\n");
return 0;
Expand All @@ -98,6 +138,8 @@ int main(int argc, char **argv)
printf(" -c <file>: config file path (default: " DEF_CFGFILE ")\n");
printf(" -l <file>|syslog: log file path or log to syslog (default: " DEF_LOGFILE ")\n");
printf(" -v: verbose output\n");
printf(" -u <username>: username for daemonize (optional)\n");
printf(" -g <groupname>: groupname for daemonize (optional)\n");
printf(" -V,-version: print version number and exit\n");
printf(" -h: print usage information and exit\n");
return 0;
Expand All @@ -121,6 +163,20 @@ int main(int argc, char **argv)
}
}

if ((use_username || use_groupname) && !become_daemon) {
fprintf(stderr, "-u / -g needs daemonmode (omit -d option) - not valid in standalone mode\n");
free(userids);
return 1;
}

if(userids != NULL) {
userids->runas_daemon = become_daemon;
userids->has_cmd_user = use_username;
userids->has_cmd_group = use_groupname;
}

test_initial_user_privileges();

if((pid = find_running_daemon()) != -1) {
fprintf(stderr, "Spacenav daemon already running (pid: %d). Aborting.\n", pid);
return 1;
Expand All @@ -129,7 +185,10 @@ int main(int argc, char **argv)
if(become_daemon) {
daemonize();
}
write_pid_file();
write_pid_file();

/* change uid / gid */
start_daemon_privileges();

logmsg(LOG_INFO, "Spacenav daemon " VERSION "\n");

Expand Down Expand Up @@ -257,7 +316,11 @@ static void cleanup(void)
remove_device(tmp);
}

stop_daemon_privileges();
remove(PIDFILE);
start_daemon_privileges();

free(userids);
}

static void daemonize(void)
Expand Down
1 change: 0 additions & 1 deletion src/spnavd.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif



struct cfg cfg;
int verbose;

Expand Down
Loading