]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec_channel.cc
rec: ensure correct service user on debian
[thirdparty/pdns.git] / pdns / rec_channel.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "rec_channel.hh"
5 #include "utility.hh"
6 #include <sys/socket.h>
7 #include <cerrno>
8 #include "misc.hh"
9 #include <string.h>
10 #include <cstdlib>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <iostream>
15
16 #include "pdnsexception.hh"
17
18 #include "namespaces.hh"
19
20 RecursorControlChannel::RecursorControlChannel()
21 {
22 d_fd=-1;
23 *d_local.sun_path=0;
24 d_local.sun_family=0;
25 }
26
27 RecursorControlChannel::~RecursorControlChannel()
28 {
29 if(d_fd > 0)
30 close(d_fd);
31 if(*d_local.sun_path)
32 unlink(d_local.sun_path);
33 }
34
35 static void setSocketBuffer(int fd, int optname, uint32_t size)
36 {
37 uint32_t psize=0;
38 socklen_t len=sizeof(psize);
39
40 if (getsockopt(fd, SOL_SOCKET, optname, (void*)&psize, &len))
41 throw PDNSException("Unable to getsocket buffer size: "+stringerror());
42
43 if (psize > size)
44 return;
45
46 // failure to raise is not fatal
47 setsockopt(fd, SOL_SOCKET, optname, (const void*)&size, sizeof(size));
48 }
49
50
51 static void setSocketReceiveBuffer(int fd, uint32_t size)
52 {
53 setSocketBuffer(fd, SO_RCVBUF, size);
54 }
55
56 static void setSocketSendBuffer(int fd, uint32_t size)
57 {
58 setSocketBuffer(fd, SO_SNDBUF, size);
59 }
60
61 int RecursorControlChannel::listen(const string& fname)
62 {
63 d_fd=socket(AF_UNIX,SOCK_DGRAM,0);
64 setCloseOnExec(d_fd);
65
66 if(d_fd < 0)
67 throw PDNSException("Creating UNIX domain socket: "+stringerror());
68
69 int tmp=1;
70 if(setsockopt(d_fd, SOL_SOCKET, SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
71 throw PDNSException("Setsockopt failed: "+stringerror());
72
73 int err=unlink(fname.c_str());
74 if(err < 0 && errno!=ENOENT)
75 throw PDNSException("Can't remove (previous) controlsocket '"+fname+"': "+stringerror() + " (try --socket-dir)");
76
77 if(makeUNsockaddr(fname, &d_local))
78 throw PDNSException("Unable to bind to controlsocket, path '"+fname+"' is not a valid UNIX socket path.");
79
80 if(bind(d_fd, (sockaddr*)&d_local,sizeof(d_local))<0)
81 throw PDNSException("Unable to bind to controlsocket '"+fname+"': "+stringerror());
82
83 // receive buf should be size of max datagram plus address size
84 setSocketReceiveBuffer(d_fd, 60 * 1024);
85 setSocketSendBuffer(d_fd, 64 * 1024);
86
87 return d_fd;
88 }
89
90 void RecursorControlChannel::connect(const string& path, const string& fname)
91 {
92 struct sockaddr_un remote;
93
94 d_fd=socket(AF_UNIX,SOCK_DGRAM,0);
95 setCloseOnExec(d_fd);
96
97 if(d_fd < 0)
98 throw PDNSException("Creating UNIX domain socket: "+string(strerror(errno)));
99
100 try {
101 int tmp=1;
102 if(setsockopt(d_fd, SOL_SOCKET, SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
103 throw PDNSException("Setsockopt failed: "+stringerror());
104
105 string localname=path+"/lsockXXXXXX";
106 *d_local.sun_path=0;
107 if (makeUNsockaddr(localname, &d_local))
108 throw PDNSException("Unable to bind to local temporary file, path '"+localname+"' is not a valid UNIX socket path.");
109
110 if(mkstemp(d_local.sun_path) < 0)
111 throw PDNSException("Unable to generate local temporary file in directory '"+path+"': "+stringerror());
112
113 int err=unlink(d_local.sun_path);
114 if(err < 0 && errno!=ENOENT)
115 throw PDNSException("Unable to remove local controlsocket: "+stringerror());
116
117 if(bind(d_fd, (sockaddr*)&d_local,sizeof(d_local))<0)
118 throw PDNSException("Unable to bind to local temporary file: "+stringerror());
119
120 if(chmod(d_local.sun_path,0666)<0) // make sure that pdns can reply!
121 throw PDNSException("Unable to chmod local temporary socket: "+stringerror());
122
123 string remotename=path+"/"+fname;
124 if (makeUNsockaddr(remotename, &remote))
125 throw PDNSException("Unable to connect to controlsocket, path '"+remotename+"' is not a valid UNIX socket path.");
126
127 if(::connect(d_fd, (sockaddr*)&remote, sizeof(remote)) < 0) {
128 if(*d_local.sun_path)
129 unlink(d_local.sun_path);
130 throw PDNSException("Unable to connect to remote '"+string(remote.sun_path)+"': "+stringerror());
131 }
132
133 // receive buf should be size of max datagram plus address size
134 setSocketReceiveBuffer(d_fd, 60 * 1024);
135 setSocketSendBuffer(d_fd, 64 * 1024);
136
137 } catch (...) {
138 close(d_fd);
139 d_fd=-1;
140 d_local.sun_path[0]=0;
141 throw;
142 }
143 }
144
145 void RecursorControlChannel::send(const std::string& msg, const std::string* remote, unsigned int timeout)
146 {
147 int ret = waitForRWData(d_fd, false, timeout, 0);
148 if(ret == 0) {
149 throw PDNSException("Timeout sending message over control channel");
150 }
151 else if(ret < 0) {
152 throw PDNSException("Error sending message over control channel:" + string(strerror(errno)));
153 }
154
155 if(remote) {
156 struct sockaddr_un remoteaddr;
157 memset(&remoteaddr, 0, sizeof(remoteaddr));
158
159 remoteaddr.sun_family=AF_UNIX;
160 strncpy(remoteaddr.sun_path, remote->c_str(), sizeof(remoteaddr.sun_path)-1);
161 remoteaddr.sun_path[sizeof(remoteaddr.sun_path)-1] = '\0';
162
163 if(::sendto(d_fd, msg.c_str(), msg.length(), 0, (struct sockaddr*) &remoteaddr, sizeof(remoteaddr) ) < 0)
164 throw PDNSException("Unable to send message over control channel '"+string(remoteaddr.sun_path)+"': "+string(strerror(errno)));
165 }
166 else if(::send(d_fd, msg.c_str(), msg.length(), 0) < 0)
167 throw PDNSException("Unable to send message over control channel: "+string(strerror(errno)));
168 }
169
170 string RecursorControlChannel::recv(std::string* remote, unsigned int timeout)
171 {
172 char buffer[16384];
173 ssize_t len;
174 struct sockaddr_un remoteaddr;
175 socklen_t addrlen=sizeof(remoteaddr);
176
177 int ret=waitForData(d_fd, timeout, 0);
178 if(ret==0)
179 throw PDNSException("Timeout waiting for answer from control channel");
180
181 if( ret < 0 || (len=::recvfrom(d_fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&remoteaddr, &addrlen)) < 0)
182 throw PDNSException("Unable to receive message over control channel: "+string(strerror(errno)));
183
184 if(remote)
185 *remote=remoteaddr.sun_path;
186
187 return string(buffer, buffer+len);
188 }
189