Revision 904d298f src/unixserversocket.cpp

View differences:

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()

Also available in: Unified diff