modified IOMultiplexer (added epoll & kevent support)
[TransparentIRC.git] / src / UserSession.c
1 /* UserSession.c - TransparentIRC 0.1
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17 #include "UserSession.h"
18 #include "IOHandler.h"
19 #include "UserClient.h"
20 #include "ConfigParser.h"
21 #include "tools.h"
22 #include "IRCClient.h"
23
24 static struct UserSession *usersessions = NULL;
25
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);
32     
33     //add UserSession to the list
34     session->prev = NULL;
35     session->next = usersessions;
36     if(usersessions)
37         usersessions->prev = session;
38     usersessions = session;
39     
40     return session;
41 }
42
43 static void usersession_close(struct UserSession *session) {
44     if(session->client)
45         userclient_close(session->client);
46     if(session->irc)
47         ircclient_close(session->irc);
48     if(session->timer)
49         iohandler_close(session->timer);
50     
51     if(session->prev)
52         session->prev->next = session->next;
53     else
54         usersessions = session->next;
55     if(session->next)
56         session->next->prev = session->prev;
57     free(session->username);
58     free(session->password);
59     free(session->nick);
60     if(session->realname)
61         free(session->realname);
62     free(session);
63 }
64
65 /* ****************** EXTERNAL EVENTS ****************** */
66
67 static void usersession_timer_callback(struct IOEvent *event);
68
69 void usersession_error(struct UserSession *session, char *error) {
70     if(session->client) {
71         iohandler_printf(session->client->iofd, ":*TransparentIRC!TransIRC@TransparentIRC.system.notification NOTICE %s :[TransparentIRC] ERROR: %s", session->nick, error);
72     }
73     usersession_close(session);
74 }
75
76 void usersession_client_close(struct UserSession *session) {
77     session->client = NULL;
78     if(!session->irc || !session->irc->fully_connected) {
79         usersession_close(session);
80         return;
81     }
82     session->idle_since = time(0);
83     if(!session->timer) {
84         int timeout_sec = get_int_field("session.timeout");
85         if(timeout_sec) {
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;
91         }
92     }
93 }
94
95 void usersession_client_raw(struct UserSession *session, char *raw) {
96     if(session->client) {
97         iohandler_printf(session->client->iofd, "%s", raw);
98     }
99 }
100
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);
104     }
105 }
106
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);
112             break;
113         default:
114             break;
115     }
116 }
117
118 /* ****************** LOGIN FUNCTIONS ****************** */
119
120 static void usersession_login_accept(struct UserLogin *login);
121 static void usersession_login_callback(struct IOEvent *event);
122
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");
127         
128         struct variable_replace_map map[] = {
129             {'U', login->username},
130             {'P', login->password},
131             {'N', login->nick},
132             {0, NULL}
133         };
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);
138         argv[0] = execute;
139         argv[argc+1] = NULL;
140         int fp = run_external_process(execute, argv);
141         if(fp < 0) {
142             userclient_login_failed(login, "Login Script error.");
143             return;
144         }
145         struct IODescriptor *iofd = iohandler_add(fp, IOTYPE_CLIENT, NULL, usersession_login_callback);
146         if(iofd) {
147             iofd->read_lines = 1;
148             iofd->state = IO_CONNECTED;
149             int timeout = get_int_field("auth.external.timeout");
150             if(timeout) {
151                 struct timeval tv_timeout;
152                 gettimeofday(&tv_timeout, NULL);
153                 tv_timeout.tv_sec += timeout;
154                 iohandler_set_timeout(iofd, &tv_timeout);
155             }
156             iofd->data = login;
157             login->login_iofd = iofd;
158             login->client->flags |= USERCLIENT_LOGIN_PROCESSING;
159         } else
160             userclient_login_failed(login, "Internal error.");
161     } else 
162         usersession_login_accept(login);
163 }
164
165 static void usersession_login_accept(struct UserLogin *login) {
166     //search session for user (or create a new one)
167     struct UserSession *session, *active_session = NULL;
168     int sessioncount = 0;
169     for(session = usersessions; session; session = session->next) {
170         if(!stricmp(login->username, session->username)) {
171             sessioncount++;
172             if(!strcmp(login->password, session->password) && !stricmp(login->nick, session->nick)) {
173                 active_session = session;
174             }
175         }
176     }
177     if(active_session) {
178         if(active_session->client) {
179             iohandler_printf(active_session->client->iofd, "ERROR :[TransparentIRC] Another client logged in.");
180             userclient_close(active_session->client);
181         } //active_session->client is now NULL
182         active_session->client = login->client;
183         if(active_session->timer) {
184             iohandler_close(active_session->timer);
185             active_session->timer = NULL;
186         }
187         userclient_login_successful(login, active_session, 1);
188     } else {
189         int sessionlimit = get_int_field("auth.session_limit");
190         if(sessionlimit && sessioncount >= sessionlimit) {
191             userclient_login_failed(login, "Session limit reached.");
192             return;
193         }
194         active_session = usersession_add(login->username, login->password, login->nick);
195         if(!active_session) {
196             userclient_login_failed(login, "Could not create Session.");
197             return;
198         }
199         active_session->client = login->client;
200         active_session->realname = strdup(login->realname);
201         ircclient_initialize(active_session, login);
202         userclient_login_successful(login, active_session, 0);
203     }
204 }
205
206 void usersession_login_abort(struct UserLogin *login) {
207     struct IODescriptor *iofd = login->login_iofd;
208     iohandler_close(iofd);
209 }
210
211 static void usersession_login_callback(struct IOEvent *event) {
212     struct UserLogin *login = event->iofd->data;
213     login->client->flags &= ~USERCLIENT_LOGIN_PROCESSING;
214     char *reply;
215     int login_finished = 0;
216     switch(event->type) {
217         case IOEVENT_RECV:
218             reply = event->data.recv_str;
219             if(!stricmplen(reply, "REJECT", 6)) {
220                 if((reply = strchr(reply, ' '))) {
221                     char reason[LINELEN];
222                     sprintf(reason, "Access denied: %s", reply + 7);
223                     login->reject_reason = strdup(reason);
224                 } else
225                     login->reject_reason = strdup("Access denied.");
226             } else if(!stricmplen(reply, "ACCEPT", 6)) {
227                 if((reply = strchr(reply, ' '))) {
228                     char *passwd = strchr(reply, ' ');
229                     if(passwd) {
230                         *passwd = '\0';
231                         passwd++;
232                         free(login->password);
233                         login->password = strdup(passwd);
234                     }
235                     free(login->username);
236                     login->username = strdup(reply);
237                 }
238                 login->login_accepted = 1;
239             } else {
240                 char *argv[MAXNUMPARAMS];
241                 int argc = parse_line(reply, argv, 0);
242                 if(argc < 2) return;
243                 if(!stricmp(argv[0], "CLASS")) {
244                     login->session_class = strdup(argv[1]);
245                 } else if(!stricmp(argv[0], "SERVER")) {
246                     login->server_address = strdup(argv[1]);
247                     if(argc > 2) {
248                         login->server_override_port = 1;
249                         if(argv[2][0] == '+') {
250                             argv[2]++;
251                             login->server_ssl = 1;
252                         }
253                         login->server_port = atoi(argv[2]);
254                     }
255                 } else if(!stricmp(argv[0], "BIND")) {
256                     login->bind_address = strdup(argv[1]);
257                 }
258                 
259             }
260             break;
261         case IOEVENT_TIMEOUT:
262             login->reject_reason = strdup("Login Script timeout.");
263             login_finished = 1;
264             break;
265         case IOEVENT_CLOSED:
266             login->reject_reason = strdup("Login Script error (no reply).");
267             login_finished = 1;
268             break;
269         default:
270             return;
271     }
272     if(login_finished) {
273         iohandler_close(event->iofd);
274         if(login->login_accepted)
275             usersession_login_accept(login);
276         else
277             userclient_login_failed(login, login->reject_reason);
278     }
279 }