1 /* UserSession.c - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "UserSession.h"
18 #include "IOHandler.h"
19 #include "UserClient.h"
20 #include "ConfigParser.h"
22 #include "IRCClient.h"
24 static struct UserSession *usersessions = NULL;
26 static struct UserSession *usersession_add(char *username, char *password, char *nick) {
27 struct UserSession *session;
28 session = calloc(1, sizeof(*session));
29 session->username = strdup(username);
30 session->password = strdup(password);
31 session->nick = strdup(nick);
33 //add UserSession to the list
35 session->next = usersessions;
37 usersessions->prev = session;
38 usersessions = session;
43 static void usersession_close(struct UserSession *session) {
45 userclient_close(session->client);
47 ircclient_close(session->irc);
49 iohandler_close(session->timer);
52 session->prev->next = session->next;
54 usersessions = session->next;
56 session->next->prev = session->prev;
57 free(session->username);
58 free(session->password);
61 free(session->realname);
65 /* ****************** EXTERNAL EVENTS ****************** */
67 static void usersession_timer_callback(struct IOEvent *event);
69 void usersession_error(struct UserSession *session, char *error) {
71 iohandler_printf(session->client->iofd, ":*TransparentIRC!TransIRC@TransparentIRC.system.notification NOTICE %s :[TransparentIRC] ERROR: %s", session->nick, error);
73 usersession_close(session);
76 void usersession_client_close(struct UserSession *session) {
77 session->client = NULL;
78 if(!session->irc || !session->irc->fully_connected) {
79 usersession_close(session);
82 session->idle_since = time(0);
84 int timeout_sec = get_int_field("session.timeout");
86 struct timeval timeout;
87 gettimeofday(&timeout, NULL);
88 timeout.tv_sec += timeout_sec;
89 session->timer = iohandler_timer(timeout, usersession_timer_callback);
90 session->timer->data = session;
95 void usersession_client_raw(struct UserSession *session, char *raw) {
97 iohandler_printf(session->client->iofd, "%s", raw);
101 void usersession_client_notification(struct UserSession *session, char *notification) {
102 if(session->client) {
103 iohandler_printf(session->client->iofd, ":*TransparentIRC!TransIRC@TransparentIRC.system.notification NOTICE %s :[TransparentIRC] %s", session->nick, notification);
107 static void usersession_timer_callback(struct IOEvent *event) {
108 struct UserSession *session = event->iofd->data;
109 switch(event->type) {
110 case IOEVENT_TIMEOUT:
111 usersession_close(session);
118 /* ****************** LOGIN FUNCTIONS ****************** */
120 static void usersession_login_accept(struct UserLogin *login);
121 static void usersession_login_callback(struct IOEvent *event);
123 void usersession_login(struct UserLogin *login) {
124 if(get_int_field("auth.external.enabled")) {
125 char *execute = get_string_field("auth.external.execute");
126 char *parameters = get_string_field("auth.external.parameters");
128 struct variable_replace_map map[] = {
129 {'U', login->username},
130 {'P', login->password},
134 char paramstr[CMDLEN+1];
135 build_var_string(paramstr, parameters, map);
136 char *argv[MAXNUMPARAMS];
137 int argc = parse_line(paramstr, argv+1, 0);
140 int fp = run_external_process(execute, argv);
142 userclient_login_failed(login, "Login Script error.");
145 struct IODescriptor *iofd = iohandler_add(fp, IOTYPE_CLIENT, usersession_login_callback);
147 iofd->read_lines = 1;
148 iofd->state = IO_CONNECTED;
149 int timeout = get_int_field("auth.external.timeout");
151 gettimeofday(&iofd->timeout, NULL);
152 iofd->timeout.tv_sec += timeout;
155 login->login_iofd = iofd;
156 login->client->flags |= USERCLIENT_LOGIN_PROCESSING;
158 userclient_login_failed(login, "Internal error.");
160 usersession_login_accept(login);
163 static void usersession_login_accept(struct UserLogin *login) {
164 //search session for user (or create a new one)
165 struct UserSession *session, *active_session = NULL;
166 int sessioncount = 0;
167 for(session = usersessions; session; session = session->next) {
168 if(!stricmp(login->username, session->username)) {
170 if(!strcmp(login->password, session->password) && !stricmp(login->nick, session->nick)) {
171 active_session = session;
176 if(active_session->client) {
177 iohandler_printf(active_session->client->iofd, "ERROR :[TransparentIRC] Another client logged in.");
178 userclient_close(active_session->client);
179 } //active_session->client is now NULL
180 active_session->client = login->client;
181 if(active_session->timer) {
182 iohandler_close(active_session->timer);
183 active_session->timer = NULL;
185 userclient_login_successful(login, active_session, 1);
187 int sessionlimit = get_int_field("auth.session_limit");
188 if(sessionlimit && sessioncount >= sessionlimit) {
189 userclient_login_failed(login, "Session limit reached.");
192 active_session = usersession_add(login->username, login->password, login->nick);
193 if(!active_session) {
194 userclient_login_failed(login, "Could not create Session.");
197 active_session->client = login->client;
198 active_session->realname = strdup(login->realname);
199 ircclient_initialize(active_session, login);
200 userclient_login_successful(login, active_session, 0);
204 void usersession_login_abort(struct UserLogin *login) {
205 struct IODescriptor *iofd = login->login_iofd;
206 iohandler_close(iofd);
209 static void usersession_login_callback(struct IOEvent *event) {
210 struct UserLogin *login = event->iofd->data;
211 login->client->flags &= ~USERCLIENT_LOGIN_PROCESSING;
213 int login_finished = 0;
214 switch(event->type) {
216 reply = event->data.recv_str;
217 if(!stricmplen(reply, "REJECT", 6)) {
218 if((reply = strchr(reply, ' '))) {
219 char reason[LINELEN];
220 sprintf(reason, "Access denied: %s", reply + 7);
221 login->reject_reason = strdup(reason);
223 login->reject_reason = strdup("Access denied.");
224 } else if(!stricmplen(reply, "ACCEPT", 6)) {
225 if((reply = strchr(reply, ' '))) {
226 char *passwd = strchr(reply, ' ');
230 free(login->password);
231 login->password = strdup(passwd);
233 free(login->username);
234 login->username = strdup(reply);
236 login->login_accepted = 1;
238 char *argv[MAXNUMPARAMS];
239 int argc = parse_line(reply, argv, 0);
241 if(!stricmp(argv[0], "CLASS")) {
242 login->session_class = strdup(argv[1]);
243 } else if(!stricmp(argv[0], "SERVER")) {
244 login->server_address = strdup(argv[1]);
246 login->server_override_port = 1;
247 if(argv[2][0] == '+') {
249 login->server_ssl = 1;
251 login->server_port = atoi(argv[2]);
253 } else if(!stricmp(argv[0], "BIND")) {
254 login->bind_address = strdup(argv[1]);
259 case IOEVENT_TIMEOUT:
260 login->reject_reason = strdup("Login Script timeout.");
264 login->reject_reason = strdup("Login Script error (no reply).");
271 iohandler_close(event->iofd);
272 if(login->login_accepted)
273 usersession_login_accept(login);
275 userclient_login_failed(login, login->reject_reason);