1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/*
** Copyright (C) 2006 Olivier DEMBOUR
** $Id: session.c,v 1.5.4.2 2010/02/11 16:06:38 dembour Exp $
**
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with This program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include "mycrypto.h"
#include "server.h"
#include "myerror.h"
#include "queue.h"
#include "requests.h"
#include "auth.h"
#include "myrand.h"
#include "log.h"
/**
* @brief generate a new session number
* @param[in] conf configuration
* @note not really good
**/
static uint16_t new_sessionid(t_conf *conf)
{
uint16_t rand;
uint32_t try = (1<<16) * 2;
uint8_t bad = 0;
t_simple_list *client;
client = (t_simple_list *)conf->client;
while (!(rand = myrand()));
if (!client)
return (myrand());
do {
for (client = (t_simple_list *)conf->client; client; client = client->next)
{
if (client->session_id == rand)
bad = 1;
}
if (!bad)
return (rand);
while (!(rand = myrand()));
} while (try--);
return (0);
}
/**
* @brief create a session for an incoming (unauthenticated) client
* @param[in] conf configuration
* @param[in] req request
* @param[in] packet
* @retval client
* @retval -1 on error
**/
t_simple_list *create_session(t_conf *conf, t_request *req, t_packet *packet)
{
t_simple_list *client;
uint16_t rand;
if (!(rand = new_sessionid(conf)))
return (0);
client = (t_simple_list *)conf->client;
if (!client)
{
conf->client = list_create_simple_cell();
client = conf->client;
}
else
{
while (client->next)
client = client->next;
if (!(client->next = list_create_simple_cell()))
return (0);
client = client->next;
}
client->session_id = rand;
client->saved_queue = 0;
if (!(client->queue = init_queue()))
{
list_destroy_simple_cell(client);
LOG("No more memory\n");
return (0);
}
client->saved_queue = client->queue;
client->num_seq = 1;
client->sd_tcp = -1; /* No endpoint yet */
client_update_timer(client);
if ((packet->type & USE_COMPRESS) == USE_COMPRESS)
client->control.use_compress = 1;
LOG("Creating session id: 0x%x address = %u.%u.%u.%u (compression %s wanted)", client->session_id,
#ifndef WORDS_BIGENDIAN
(unsigned int) ((req->sa.sin_addr.s_addr) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr >> 8) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr >> 16) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr >> 24) & 0xff)
#else
(unsigned int) ((req->sa.sin_addr.s_addr >> 24) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr >> 16) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr >> 8) & 0xff),
(unsigned int) ((req->sa.sin_addr.s_addr) & 0xff)
#endif
, client->control.use_compress ? "" :"NOT"
);
return (client);
}
/**
* @brief deal session request
* @param[in] conf configuration
* @param[in] request received
* @param[in] data packet structure received
* @retval 0 on success
* @retval -1 on error
**/
int session_request(t_conf *conf, t_request *req, t_data *data)
{
t_packet *packet;
t_simple_list *client;
if (data->len < PACKET_LEN)
return (-1);
packet = (void *)data->buffer;
data->buffer[data->len] = 0;
if (!req->cmd->authenticated)
return (req->cmd->deal_cmd(conf, req, packet, 0));
client = find_client_by_session_id(conf, packet->session_id);
/* ----- Authenticated user below ---- */
if ((!client) || (client->sd_tcp > 0))
{
packet->type = ERR;
return (send_ascii_reply(conf, req, packet, ERR_AUTH_FAILED));
}
if (client && client->control.authenticated)
/* deal_cmd is in request.c */
return (req->cmd->deal_cmd(conf, req, packet, client));
return (-1);
}
|