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);
50 session->prev->next = session->next;
52 usersessions = session->next;
54 session->next->prev = session->prev;
55 free(session->username);
56 free(session->password);
59 free(session->realname);
63 /* ****************** EXTERNAL EVENTS ****************** */
65 void usersession_error(struct UserSession *session, char *error) {
67 iohandler_printf(session->client->iofd, ":*TransparentIRC!TransIRC@TransparentIRC.system.notification NOTICE %s :[TransparentIRC] ERROR: %s", session->nick, error);
69 usersession_close(session);
72 void usersession_client_close(struct UserSession *session) {
73 session->client = NULL;
74 session->idle_since = time(0);
78 void usersession_client_raw(struct UserSession *session, char *raw) {
80 iohandler_printf(session->client->iofd, "%s", raw);
84 void usersession_client_notification(struct UserSession *session, char *notification) {
86 iohandler_printf(session->client->iofd, ":*TransparentIRC!TransIRC@TransparentIRC.system.notification NOTICE %s :[TransparentIRC] %s", session->nick, notification);
90 /* ****************** LOGIN FUNCTIONS ****************** */
92 static void usersession_login_accept(struct UserLogin *login);
93 static void usersession_login_callback(struct IOEvent *event);
95 void usersession_login(struct UserLogin *login) {
96 if(get_int_field("auth.external.enabled")) {
97 char *execute = get_string_field("auth.external.execute");
98 char *parameters = get_string_field("auth.external.parameters");
100 struct variable_replace_map map[] = {
101 {'U', login->username},
102 {'P', login->password},
106 char paramstr[CMDLEN+1];
107 build_var_string(paramstr, parameters, map);
108 char *argv[MAXNUMPARAMS];
109 int argc = parse_line(paramstr, argv+1, 0);
112 int fp = run_external_process(execute, argv);
114 userclient_login_failed(login, "Login Script error.");
117 struct IODescriptor *iofd = iohandler_add(fp, IOTYPE_CLIENT, usersession_login_callback);
119 iofd->read_lines = 1;
120 iofd->state = IO_CONNECTED;
121 int timeout = get_int_field("auth.external.timeout");
123 gettimeofday(&iofd->timeout, NULL);
124 iofd->timeout.tv_sec += timeout;
127 login->login_iofd = iofd;
128 login->client->flags |= USERCLIENT_LOGIN_PROCESSING;
130 userclient_login_failed(login, "Internal error.");
132 usersession_login_accept(login);
135 static void usersession_login_accept(struct UserLogin *login) {
136 //search session for user (or create a new one)
137 struct UserSession *session, *active_session = NULL;
138 int sessioncount = 0;
139 for(session = usersessions; session; session = session->next) {
140 if(!stricmp(login->username, session->username)) {
142 if(!strcmp(login->password, session->password) && !stricmp(login->nick, session->nick)) {
143 active_session = session;
148 if(active_session->client) {
149 iohandler_printf(active_session->client->iofd, "ERROR :[TransparentIRC] Another client logged in.");
150 userclient_close(active_session->client);
151 } //active_session->client is now NULL
152 active_session->client = login->client;
153 userclient_login_successful(login, active_session, 1);
155 int sessionlimit = get_int_field("auth.session_limit");
156 if(sessionlimit && sessioncount >= sessionlimit) {
157 userclient_login_failed(login, "Session limit reached.");
160 active_session = usersession_add(login->username, login->password, login->nick);
161 if(!active_session) {
162 userclient_login_failed(login, "Could not create Session.");
165 active_session->client = login->client;
166 active_session->realname = strdup(login->realname);
167 ircclient_initialize(active_session, login);
168 userclient_login_successful(login, active_session, 0);
172 void usersession_login_abort(struct UserLogin *login) {
173 struct IODescriptor *iofd = login->login_iofd;
174 iohandler_close(iofd);
177 static void usersession_login_callback(struct IOEvent *event) {
178 struct UserLogin *login = event->iofd->data;
179 login->client->flags &= ~USERCLIENT_LOGIN_PROCESSING;
181 int login_finished = 0;
182 switch(event->type) {
184 reply = event->data.recv_str;
185 if(!stricmplen(reply, "REJECT", 6)) {
186 if((reply = strchr(reply, ' '))) {
187 char reason[LINELEN];
188 sprintf(reason, "Access denied: %s", reply + 7);
189 login->reject_reason = strdup(reason);
191 login->reject_reason = strdup("Access denied.");
192 } else if(!stricmplen(reply, "ACCEPT", 6)) {
193 if((reply = strchr(reply, ' '))) {
194 char *passwd = strchr(reply, ' ');
198 free(login->password);
199 login->password = strdup(passwd);
201 free(login->username);
202 login->username = strdup(reply);
204 login->login_accepted = 1;
206 char *argv[MAXNUMPARAMS];
207 int argc = parse_line(reply, argv, 0);
209 if(!stricmp(argv[0], "CLASS")) {
210 login->session_class = strdup(argv[1]);
211 } else if(!stricmp(argv[0], "SERVER")) {
212 login->server_address = strdup(argv[1]);
214 login->server_override_port = 1;
215 if(argv[2][0] == '+') {
217 login->server_ssl = 1;
219 login->server_port = atoi(argv[2]);
221 } else if(!stricmp(argv[0], "BIND")) {
222 login->bind_address = strdup(argv[1]);
227 case IOEVENT_TIMEOUT:
228 login->reject_reason = strdup("Login Script timeout.");
232 login->reject_reason = strdup("Login Script error (no reply).");
239 iohandler_close(event->iofd);
240 if(login->login_accepted)
241 usersession_login_accept(login);
243 userclient_login_failed(login, login->reject_reason);