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"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/un.h> // UNIX
36 #include <netinet/in.h> // IP
37 #include <netinet/tcp.h> // TCP
46 PLUGIN_CREATOR<RADIUS> creator;
50 extern "C" PLUGIN * GetPlugin()
52 return creator.GetPlugin();
56 : m_config(m_settings),
62 m_logger(GetPluginLogger(GetStgLogger(), "radius"))
66 int RADIUS::ParseSettings()
69 m_config = STG::Config(m_settings);
70 return reconnect() ? 0 : -1;
71 } catch (const std::runtime_error& ex) {
72 m_logger("Failed to parse settings. %s", ex.what());
82 int res = pthread_create(&m_thread, NULL, run, this);
86 m_error = strerror(res);
87 m_logger("Failed to create thread: '" + m_error + "'.");
98 for (size_t i = 0; i < 25 && !m_stopped; i++) {
99 struct timespec ts = {0, 200000000};
100 nanosleep(&ts, NULL);
104 pthread_join(m_thread, NULL);
108 m_error = "Failed to stop thread.";
112 //-----------------------------------------------------------------------------
113 void* RADIUS::run(void* d)
116 sigfillset(&signalSet);
117 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
119 static_cast<RADIUS *>(d)->runImpl();
124 bool RADIUS::reconnect()
126 if (!m_conns.empty())
128 std::deque<STG::Conn *>::const_iterator it;
129 for (it = m_conns.begin(); it != m_conns.end(); ++it)
133 if (m_listenSocket != 0)
135 shutdown(m_listenSocket, SHUT_RDWR);
136 close(m_listenSocket);
137 if (m_config.connectionType == Config::UNIX)
138 unlink(m_config.bindAddress.c_str());
140 if (m_config.connectionType == Config::UNIX)
141 m_listenSocket = createUNIX();
143 m_listenSocket = createTCP();
144 if (m_listenSocket == 0)
146 if (listen(m_listenSocket, 100) == -1)
148 m_error = std::string("Error starting to listen socket: ") + strerror(errno);
155 int RADIUS::createUNIX() const
157 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
160 m_error = std::string("Error creating UNIX socket: ") + strerror(errno);
164 struct sockaddr_un addr;
165 memset(&addr, 0, sizeof(addr));
166 addr.sun_family = AF_UNIX;
167 strncpy(addr.sun_path, m_config.bindAddress.c_str(), m_config.bindAddress.length());
168 if (bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
170 shutdown(fd, SHUT_RDWR);
172 m_error = std::string("Error binding UNIX socket: ") + strerror(errno);
179 int RADIUS::createTCP() const
182 memset(&hints, 0, sizeof(addrinfo));
184 hints.ai_family = AF_INET; /* Allow IPv4 */
185 hints.ai_socktype = SOCK_STREAM; /* Stream socket */
186 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
187 hints.ai_protocol = 0; /* Any protocol */
188 hints.ai_canonname = NULL;
189 hints.ai_addr = NULL;
190 hints.ai_next = NULL;
192 addrinfo* ais = NULL;
193 int res = getaddrinfo(m_config.bindAddress.c_str(), m_config.portStr.c_str(), &hints, &ais);
196 m_error = "Error resolvin address '" + m_config.bindAddress + "': " + gai_strerror(res);
201 for (addrinfo* ai = ais; ai != NULL; ai = ai->ai_next)
203 int fd = socket(AF_INET, SOCK_STREAM, 0);
206 m_error = std::string("Error creating TCP socket: ") + strerror(errno);
211 if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1)
213 shutdown(fd, SHUT_RDWR);
215 m_error = std::string("Error binding TCP socket: ") + strerror(errno);
223 m_error = "Failed to resolve '" + m_config.bindAddress;
230 void RADIUS::runImpl()
243 int res = select(maxFD() + 1, &fds, NULL, NULL, &tv);
246 m_error = std::string("'select' is failed: '") + strerror(errno) + "'.";
258 for (std::deque<Conn*>::iterator it = m_conns.begin(); it != m_conns.end(); ++it)
268 int RADIUS::maxFD() const
270 int maxFD = m_listenSocket;
271 std::deque<STG::Conn *>::const_iterator it;
272 for (it = m_conns.begin(); it != m_conns.end(); ++it)
273 if (maxFD < (*it)->sock())
274 maxFD = (*it)->sock();
278 void RADIUS::buildFDSet(fd_set & fds) const
281 FD_SET(m_listenSocket, &fds);
282 std::deque<STG::Conn *>::const_iterator it;
283 for (it = m_conns.begin(); it != m_conns.end(); ++it)
284 FD_SET((*it)->sock(), &fds);
287 void RADIUS::cleanupConns()
289 std::deque<STG::Conn *>::iterator pos;
290 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
291 if (!(*pos)->isOk()) {
296 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
297 m_conns.erase(pos, m_conns.end());
300 void RADIUS::handleEvents(const fd_set & fds)
302 if (FD_ISSET(m_listenSocket, &fds))
306 std::deque<STG::Conn *>::iterator it;
307 for (it = m_conns.begin(); it != m_conns.end(); ++it)
308 if (FD_ISSET((*it)->sock(), &fds))
315 void RADIUS::acceptConnection()
317 if (m_config.connectionType == Config::UNIX)
323 void RADIUS::acceptUNIX()
325 struct sockaddr_un addr;
326 memset(&addr, 0, sizeof(addr));
327 socklen_t size = sizeof(addr);
328 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
331 m_error = std::string("Failed to accept UNIX connection: ") + strerror(errno);
335 m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, addr.sun_path));
338 void RADIUS::acceptTCP()
340 struct sockaddr_in addr;
341 memset(&addr, 0, sizeof(addr));
342 socklen_t size = sizeof(addr);
343 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
346 m_error = std::string("Failed to accept TCP connection: ") + strerror(errno);
350 std::string remote = inet_ntostring(addr.sin_addr.s_addr) + ":" + x2str(ntohs(addr.sin_port));
351 m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, remote));