2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "stg/store.h"
24 #include "stg/users.h"
25 #include "stg/plugin_creator.h"
26 #include "stg/common.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/un.h> // UNIX
37 #include <netinet/in.h> // IP
38 #include <netinet/tcp.h> // TCP
47 PLUGIN_CREATOR<RADIUS> creator;
51 extern "C" PLUGIN * GetPlugin()
53 return creator.GetPlugin();
63 m_logger(GetPluginLogger(GetStgLogger(), "radius"))
67 int RADIUS::ParseSettings()
70 m_config = STG::Config(m_settings);
71 return reconnect() ? 0 : -1;
72 } catch (const std::runtime_error& ex) {
73 m_logger("Failed to parse settings. %s", ex.what());
83 int res = pthread_create(&m_thread, NULL, run, this);
87 m_error = strerror(res);
88 m_logger("Failed to create thread: '" + m_error + "'.");
99 for (size_t i = 0; i < 25 && !m_stopped; i++) {
100 struct timespec ts = {0, 200000000};
101 nanosleep(&ts, NULL);
105 pthread_join(m_thread, NULL);
109 if (m_config.connectionType == Config::UNIX)
110 unlink(m_config.bindAddress.c_str());
112 m_error = "Failed to stop thread.";
116 //-----------------------------------------------------------------------------
117 void* RADIUS::run(void* d)
120 sigfillset(&signalSet);
121 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
123 static_cast<RADIUS *>(d)->runImpl();
128 bool RADIUS::reconnect()
130 if (!m_conns.empty())
132 std::deque<STG::Conn *>::const_iterator it;
133 for (it = m_conns.begin(); it != m_conns.end(); ++it)
137 if (m_listenSocket != 0)
139 shutdown(m_listenSocket, SHUT_RDWR);
140 close(m_listenSocket);
142 if (m_config.connectionType == Config::UNIX)
143 m_listenSocket = createUNIX();
145 m_listenSocket = createTCP();
146 if (m_listenSocket == 0)
148 if (listen(m_listenSocket, 100) == -1)
150 m_error = std::string("Error starting to listen socket: ") + strerror(errno);
157 int RADIUS::createUNIX() const
159 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
162 m_error = std::string("Error creating UNIX socket: ") + strerror(errno);
166 struct sockaddr_un addr;
167 memset(&addr, 0, sizeof(addr));
168 addr.sun_family = AF_UNIX;
169 strncpy(addr.sun_path, m_config.bindAddress.c_str(), m_config.bindAddress.length());
170 unlink(m_config.bindAddress.c_str());
171 if (bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
173 shutdown(fd, SHUT_RDWR);
175 m_error = std::string("Error binding UNIX socket: ") + strerror(errno);
182 int RADIUS::createTCP() const
185 memset(&hints, 0, sizeof(addrinfo));
187 hints.ai_family = AF_INET; /* Allow IPv4 */
188 hints.ai_socktype = SOCK_STREAM; /* Stream socket */
189 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
190 hints.ai_protocol = 0; /* Any protocol */
191 hints.ai_canonname = NULL;
192 hints.ai_addr = NULL;
193 hints.ai_next = NULL;
195 addrinfo* ais = NULL;
196 int res = getaddrinfo(m_config.bindAddress.c_str(), m_config.portStr.c_str(), &hints, &ais);
199 m_error = "Error resolving address '" + m_config.bindAddress + "': " + gai_strerror(res);
204 for (addrinfo* ai = ais; ai != NULL; ai = ai->ai_next)
206 int fd = socket(AF_INET, SOCK_STREAM, 0);
209 m_error = std::string("Error creating TCP socket: ") + strerror(errno);
214 if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1)
216 shutdown(fd, SHUT_RDWR);
218 m_error = std::string("Error binding TCP socket: ") + strerror(errno);
226 m_error = "Failed to resolve '" + m_config.bindAddress;
233 void RADIUS::runImpl()
247 int res = select(maxFD() + 1, &fds, NULL, NULL, &tv);
252 m_error = std::string("'select' is failed: '") + strerror(errno) + "'.";
264 for (std::deque<Conn*>::iterator it = m_conns.begin(); it != m_conns.end(); ++it)
274 int RADIUS::maxFD() const
276 int maxFD = m_listenSocket;
277 std::deque<STG::Conn *>::const_iterator it;
278 for (it = m_conns.begin(); it != m_conns.end(); ++it)
279 if (maxFD < (*it)->sock())
280 maxFD = (*it)->sock();
284 void RADIUS::buildFDSet(fd_set & fds) const
287 FD_SET(m_listenSocket, &fds);
288 std::deque<STG::Conn *>::const_iterator it;
289 for (it = m_conns.begin(); it != m_conns.end(); ++it)
290 FD_SET((*it)->sock(), &fds);
293 void RADIUS::cleanupConns()
295 std::deque<STG::Conn *>::iterator pos;
296 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
297 if (!(*pos)->isOk()) {
302 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
303 m_conns.erase(pos, m_conns.end());
306 void RADIUS::handleEvents(const fd_set & fds)
308 if (FD_ISSET(m_listenSocket, &fds))
312 std::deque<STG::Conn *>::iterator it;
313 for (it = m_conns.begin(); it != m_conns.end(); ++it)
314 if (FD_ISSET((*it)->sock(), &fds))
321 void RADIUS::acceptConnection()
323 if (m_config.connectionType == Config::UNIX)
329 void RADIUS::acceptUNIX()
331 struct sockaddr_un addr;
332 memset(&addr, 0, sizeof(addr));
333 socklen_t size = sizeof(addr);
334 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
337 m_error = std::string("Failed to accept UNIX connection: ") + strerror(errno);
341 printfd(__FILE__, "New UNIX connection: '%s'\n", addr.sun_path);
342 m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, addr.sun_path));
345 void RADIUS::acceptTCP()
347 struct sockaddr_in addr;
348 memset(&addr, 0, sizeof(addr));
349 socklen_t size = sizeof(addr);
350 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
353 m_error = std::string("Failed to accept TCP connection: ") + strerror(errno);
357 std::string remote = inet_ntostring(addr.sin_addr.s_addr) + ":" + x2str(ntohs(addr.sin_port));
358 printfd(__FILE__, "New TCP connection: '%s'\n", remote.c_str());
359 m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, remote));