Revision 904d298f

View differences:

configure.ac
19 19
AC_SUBST(DVBD_ETC_DIR, "`eval echo ${sysconfdir}/dvbd`")
20 20
AC_DEFINE_UNQUOTED(DEFAULT_CONFIG_FILE, "${DVBD_ETC_DIR}/config", [Default configuration file])
21 21
AC_DEFINE_UNQUOTED(DEFAULT_SOCKET_FILE, "${DVBD_RUN_DIR}/dvbd.socket", [Default socket file])
22
AC_DEFINE_UNQUOTED(DEFAULT_SOCKET_UID, "", [Default user for socket])
23
AC_DEFINE_UNQUOTED(DEFAULT_SOCKET_GID, "", [Default group for socket])
24
AC_DEFINE_UNQUOTED(DEFAULT_SOCKET_MODE, "", [Default permissions for socket])
22 25
AC_DEFINE_UNQUOTED(DEFAULT_PID_FILE, "${DVBD_RUN_DIR}/dvbd.pid", [Default pid file])
23 26
AC_DEFINE_UNQUOTED(DEFAULT_SCHEDULE_FILE, "${DVBD_SPOOL_DIR}/schedule", [Default schedule file])
24 27
AC_DEFINE_UNQUOTED(DEFAULT_LOG_FILE, "${DVBD_LOG_DIR}/dvbd.log", [Default log file])
debian/dirs
1
usr/bin
2
usr/sbin
3
var/spool
4
var/run
5
etc
1
/usr/bin
2
/usr/sbin
3
/var/spool
4
/var/spool/dvbd
5
/var/run
6
/var/run/dvbd
7
/etc
8
/etc/dvbd
dvbd.html
68 68
<dt>socket</dt>
69 69
<dd>the (unix) socket to listen on. Default is /var/run/dvbd/dvbd.socket </dd>
70 70

  
71
<dt>socket-uid</dt>
72
<dd>the user id of the socket file. The default user id is that same as the user id of the process
73
running dvbd. If you user this option, dvbd probably need to run as root.</dd>
74

  
75
<dt>socket-gid</dt>
76
<dd>the group id of the socket file. The default user id is that same as the user id of the process
77
running dvbd. If you user this option, dvbd probably need to run as root.</dd>
78

  
79
<dt>socket-mode</dt>
80
<dd>the mode (in octal) of the socket file. The default is to create the socket with umask of 007</dd>
81

  
71 82
<dt>pid</dt>
72 83
<dd>the pid file to write to once it has become a daemon. Default is /var/run/dvbd/dvbd.pid </dd>
73 84

  
src/dvbd.cpp
38 38
  ConfigFile config;
39 39
  
40 40
  config.set("socket", DEFAULT_SOCKET_FILE);
41
  config.set("socket-uid", DEFAULT_SOCKET_UID);
42
  config.set("socket-gid", DEFAULT_SOCKET_GID);
43
  config.set("socket-mode", DEFAULT_SOCKET_MODE);
41 44
  config.set("pid", DEFAULT_PID_FILE);
42 45
  config.set("log", DEFAULT_LOG_FILE);
43 46
  config.set("schedule", DEFAULT_SCHEDULE_FILE);
......
78 81
  Scheduler scheduler(config.get("schedule"), &tm, &cm);
79 82

  
80 83
  std::string socketFilename = config.get("socket");
81
  Lobby lobby(socketFilename, &tm, &cm, &scheduler);
84
  std::string socketUid = config.get("socket-uid");
85
  std::string socketGid = config.get("socket-gid");
86
  std::string socketMode = config.get("socket-mode");
87
  Lobby lobby(socketFilename, socketUid, socketGid, socketMode, &tm, &cm, &scheduler);
82 88

  
83 89
  setupSignalHandlers();
84 90

  
src/lobby.cpp
36 36
#include <iostream>
37 37
#include <unistd.h>
38 38

  
39
Lobby::Lobby(const std::string &socketFilename, TunerManager *tm, 
40
	     ConnectionManager *cm, Scheduler *scheduler)
39
Lobby::Lobby(const std::string &socketFilename,
40
	     const std::string &socketUid,
41
	     const std::string &socketGid,
42
	     const std::string &socketMode,
43
	     TunerManager *tm, 
44
	     ConnectionManager *cm, 
45
	     Scheduler *scheduler)
41 46
  : tm(tm), cm(cm), scheduler(scheduler)
42 47
{
43
  server = new UnixServerSocket(socketFilename);
48
  server = new UnixServerSocket(socketFilename, socketUid, socketGid, socketMode);
44 49
  server->setNonBlocking();
45 50
  setRandomSeed();
46 51
}
src/lobby.h
34 34
class Lobby : public Selectable {
35 35
public:
36 36
  Lobby(const std::string &socketFilename, 
37
	TunerManager *tm, ConnectionManager *cm,
37
	const std::string &socketUid,
38
	const std::string &socketGid,
39
	const std::string &socketMode,
40
	TunerManager *tm,
41
	ConnectionManager *cm,
38 42
	Scheduler *scheduler);
39 43

  
40 44
  virtual ~Lobby();
src/unixserversocket.cpp
22 22
#include <cstring>
23 23
#include <cstdlib>
24 24
#include <cerrno>
25
#include <cstdio>
25 26
#include <unistd.h>
26 27
#include <sys/types.h>
27 28
#include <sys/socket.h>
28 29
#include <sys/un.h>
29 30
#include <fcntl.h>
31
#include <pwd.h>
32
#include <grp.h>
30 33

  
31
UnixServerSocket::UnixServerSocket(const std::string &filename)
34
UnixServerSocket::UnixServerSocket(const std::string &filename,
35
				   const std::string &socketUid,
36
				   const std::string &socketGid,
37
				   const std::string &socketMode)
32 38
  : filename(filename)
33 39
{
34 40
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
......
70 76
  }
71 77

  
72 78
  umask(oldMask);
79

  
80
  // set the mode
81
  if (socketMode != "") {
82
    int mode = 0;
83
    sscanf(socketMode.c_str(), "%o", &mode);
84
    if (chmod(filename.c_str(), mode) != 0) {
85
      std::cerr << "Fatal error: failed to set mode on socket " << filename << "\n"
86
		<< strerror(errno) << std::endl;
87
      exit(1);
88
    }
89
  }
90

  
91
  // set the owner
92
  if (socketUid != "") {
93
    uid_t owner;
94
    if (!sscanf(socketUid.c_str(), "%d", &owner)) {
95
      struct passwd *p = getpwnam(socketUid.c_str());
96
      if (p == NULL) {
97
	std::cerr << "Fatal error: failed to lookup user " << socketUid << " in password database\n"
98
		  << strerror(errno) << std::endl;
99
	exit(1);
100
      }
101
      owner = p->pw_uid;
102
    }
103
      
104
    if (chown(filename.c_str(), owner, (gid_t) -1) != 0) {
105
      std::cerr << "Fatal error: failed to set socket ownership " << socketUid << " on " << filename << "\n"
106
		<< strerror(errno) << std::endl;
107
      exit(1);
108
    }
109
  }
110

  
111
  // set the group
112
  if (socketGid != "") {
113
    uid_t group;
114
    if (!sscanf(socketGid.c_str(), "%d", &group)) {
115
      struct group *p = getgrnam(socketGid.c_str());
116
      if (p == NULL) {
117
	std::cerr << "Fatal error: failed to lookup user " << socketGid << " in group database\n"
118
		  << strerror(errno) << std::endl;
119
	exit(1);
120
      }
121
      group = p->gr_gid;
122
    }
123
      
124
    if (chown(filename.c_str(), (uid_t) -1, group) != 0) {
125
      std::cerr << "Fatal error: failed to set socket group " << socketGid << " on " << filename << "\n"
126
		<< strerror(errno) << std::endl;
127
      exit(1);
128
    }
129
  }
73 130
}
74 131

  
75 132
void UnixServerSocket::setNonBlocking()
src/unixserversocket.h
25 25

  
26 26
class UnixServerSocket : public Selectable {
27 27
public:
28
  UnixServerSocket(const std::string &filename);
28
  UnixServerSocket(const std::string &filename,
29
		   const std::string &socketUid = "",
30
		   const std::string &socketGid = "",
31
		   const std::string &socketMode = "");
29 32
  virtual ~UnixServerSocket();
30 33

  
31 34
  void setNonBlocking();

Also available in: Unified diff