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"
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
44 extern "C" STG::Plugin* GetPlugin()
56 m_logger(STG::PluginLogger::get("radius"))
60 int RADIUS::ParseSettings()
63 m_config = STG::Config(m_settings);
64 return reconnect() ? 0 : -1;
65 } catch (const std::runtime_error& ex) {
66 m_logger("Failed to parse settings. %s", ex.what());
76 int res = pthread_create(&m_thread, NULL, run, this);
80 m_error = strerror(res);
81 m_logger("Failed to create thread: '" + m_error + "'.");
87 std::set<std::string>::const_iterator it = m_logins.begin();
88 for (; it != m_logins.end(); ++it)
89 m_users->Unauthorize(*it, this, "Stopping RADIUS plugin.");
97 for (size_t i = 0; i < 25 && !m_stopped; i++) {
98 struct timespec ts = {0, 200000000};
103 pthread_join(m_thread, NULL);
107 if (m_config.connectionType == Config::UNIX)
108 unlink(m_config.bindAddress.c_str());
110 m_error = "Failed to stop thread.";
114 //-----------------------------------------------------------------------------
115 void* RADIUS::run(void* d)
118 sigfillset(&signalSet);
119 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
121 static_cast<RADIUS *>(d)->runImpl();
126 bool RADIUS::reconnect()
128 if (!m_conns.empty())
130 std::deque<STG::Conn *>::const_iterator it;
131 for (it = m_conns.begin(); it != m_conns.end(); ++it)
135 if (m_listenSocket != 0)
137 shutdown(m_listenSocket, SHUT_RDWR);
138 close(m_listenSocket);
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 unlink(m_config.bindAddress.c_str());
169 if (bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
171 shutdown(fd, SHUT_RDWR);
173 m_error = std::string("Error binding UNIX socket: ") + strerror(errno);
177 chown(m_config.bindAddress.c_str(), m_config.sockUID, m_config.sockGID);
178 if (m_config.sockMode != static_cast<mode_t>(-1))
179 chmod(m_config.bindAddress.c_str(), m_config.sockMode);
183 int RADIUS::createTCP() const
186 memset(&hints, 0, sizeof(addrinfo));
188 hints.ai_family = AF_INET; /* Allow IPv4 */
189 hints.ai_socktype = SOCK_STREAM; /* Stream socket */
190 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
191 hints.ai_protocol = 0; /* Any protocol */
192 hints.ai_canonname = NULL;
193 hints.ai_addr = NULL;
194 hints.ai_next = NULL;
196 addrinfo* ais = NULL;
197 int res = getaddrinfo(m_config.bindAddress.c_str(), m_config.portStr.c_str(), &hints, &ais);
200 m_error = "Error resolving address '" + m_config.bindAddress + "': " + gai_strerror(res);
205 for (addrinfo* ai = ais; ai != NULL; ai = ai->ai_next)
207 int fd = socket(AF_INET, SOCK_STREAM, 0);
210 m_error = std::string("Error creating TCP socket: ") + strerror(errno);
215 if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1)
217 shutdown(fd, SHUT_RDWR);
219 m_error = std::string("Error binding TCP socket: ") + strerror(errno);
227 m_error = "Failed to resolve '" + m_config.bindAddress;
234 void RADIUS::runImpl()
248 int res = select(maxFD() + 1, &fds, NULL, NULL, &tv);
253 m_error = std::string("'select' is failed: '") + strerror(errno) + "'.";
265 for (std::deque<Conn*>::iterator it = m_conns.begin(); it != m_conns.end(); ++it)
275 int RADIUS::maxFD() const
277 int maxFD = m_listenSocket;
278 std::deque<STG::Conn *>::const_iterator it;
279 for (it = m_conns.begin(); it != m_conns.end(); ++it)
280 if (maxFD < (*it)->sock())
281 maxFD = (*it)->sock();
285 void RADIUS::buildFDSet(fd_set & fds) const
288 FD_SET(m_listenSocket, &fds);
289 std::deque<STG::Conn *>::const_iterator it;
290 for (it = m_conns.begin(); it != m_conns.end(); ++it)
291 FD_SET((*it)->sock(), &fds);
294 void RADIUS::cleanupConns()
296 std::deque<STG::Conn *>::iterator pos;
297 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
298 if (!(*pos)->isOk()) {
303 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
304 m_conns.erase(pos, m_conns.end());
307 void RADIUS::handleEvents(const fd_set & fds)
309 if (FD_ISSET(m_listenSocket, &fds))
313 std::deque<STG::Conn *>::iterator it;
314 for (it = m_conns.begin(); it != m_conns.end(); ++it)
315 if (FD_ISSET((*it)->sock(), &fds))
322 void RADIUS::acceptConnection()
324 if (m_config.connectionType == Config::UNIX)
330 void RADIUS::acceptUNIX()
332 struct sockaddr_un addr;
333 memset(&addr, 0, sizeof(addr));
334 socklen_t size = sizeof(addr);
335 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
338 m_error = std::string("Failed to accept UNIX connection: ") + strerror(errno);
342 printfd(__FILE__, "New UNIX connection: '%s'\n", addr.sun_path);
343 m_conns.push_back(new Conn(*m_users, m_logger, *this, m_config, res, addr.sun_path));
346 void RADIUS::acceptTCP()
348 struct sockaddr_in addr;
349 memset(&addr, 0, sizeof(addr));
350 socklen_t size = sizeof(addr);
351 int res = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&addr), &size);
354 m_error = std::string("Failed to accept TCP connection: ") + strerror(errno);
358 std::string remote = inet_ntostring(addr.sin_addr.s_addr) + ":" + std::to_string(ntohs(addr.sin_port));
359 printfd(__FILE__, "New TCP connection: '%s'\n", remote.c_str());
360 m_conns.push_back(new Conn(*m_users, m_logger, *this, m_config, res, remote));
363 void RADIUS::authorize(const STG::User& user)
366 const std::string& login(user.GetLogin());
367 if (!m_users->Authorize(login, ip, 0xffFFffFF, this))
369 m_error = "Unable to authorize user '" + login + "' with ip " + inet_ntostring(ip) + ".";
373 m_logins.insert(login);
376 void RADIUS::unauthorize(const std::string& login, const std::string& reason)
378 const std::set<std::string>::const_iterator it = m_logins.find(login);
379 if (it == m_logins.end())
382 m_users->Unauthorize(login, this, reason);