prot-a.c 9.17 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: prot-a.c,v 0.12 1991/12/17 23:07:40 ceder Exp $
Linus Tolke Y's avatar
Linus Tolke Y committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28
/*
 * Protocol A.
 */

29
static char *rcsid = "$Id: prot-a.c,v 0.12 1991/12/17 23:07:40 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
30 31


Per Cederqvist's avatar
Per Cederqvist committed
32 33 34 35 36
#include <stdio.h>
#include <setjmp.h>

#include <kom-errno.h>
#include <kom-types.h>
37
#include <server/smalloc.h>
Per Cederqvist's avatar
Per Cederqvist committed
38 39 40 41 42 43 44 45
#include "lyskomd.h"
#include "com.h"
#include "connections.h"
#include "isc-parse.h"
#include "prot-a.h"
#include "prot-a-output.h"
#include "prot-a-parse.h"
#include <debug.h>
46
#include "isc-interface.h"
Per Cederqvist's avatar
Per Cederqvist committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "mux.h"

BUGDECL;

void
prot_a_reply(Connection *client,
	     Bool status,
	     Result_holder *res)
{
    /*
     * The function is called. Now return the answer.
     */

    if ( status == OK )
    {
	mux_printf(client, "=%d", client->ref_no);

	switch ( fnc_defs[ client->function ].result )
	{
66
	case rt_number:
Per Cederqvist's avatar
Per Cederqvist committed
67 68 69 70
	    mux_printf(client, " %ld", (u_long)res->number);
	    BUG(("=%d\n", res->number));
	    break;

71
	case rt_success:
Per Cederqvist's avatar
Per Cederqvist committed
72 73 74
	    BUG(("=Success\n"));
	    break;
	    
75
	case rt_person:
Per Cederqvist's avatar
Per Cederqvist committed
76 77 78 79
	    prot_a_output_person(client, &res->person);
	    BUG(("={Person struct not listed}\n"));
	    break;

80
	case rt_membership:
Per Cederqvist's avatar
Per Cederqvist committed
81 82 83 84
	    prot_a_output_membership(client, &res->membership);
	    BUG(("={Membership struct not listed}\n"));
	    break;

85
	case rt_conf_list:
Per Cederqvist's avatar
Per Cederqvist committed
86 87 88 89
	    prot_a_output_conf_list(client, res->conf_list);
	    BUG(("={Conf_list not listed}\n"));
	    break;

90
	case rt_conf_no_list:
Per Cederqvist's avatar
Per Cederqvist committed
91 92 93 94
	    prot_a_output_conf_no_list(client, res->conf_no_list);
	    BUG(("={Conf_no_list not listed}\n"));
	    break;
	    	    
95
	case rt_conference:
Per Cederqvist's avatar
Per Cederqvist committed
96 97 98 99
	    prot_a_output_conference(client, &res->conference);
	    BUG(("={Conference struct not listed}\n"));
	    break;

100
	case rt_string:
Per Cederqvist's avatar
Per Cederqvist committed
101 102 103 104 105 106
	    prot_a_output_string(client, res->string);
	    BUG(("={%dH", res->string.len));
	    BUGSTR(res->string);
	    BUG(("}\n"));
	    break;
	    
107
	case rt_mark_list:
Per Cederqvist's avatar
Per Cederqvist committed
108 109 110 111
	    prot_a_output_mark_list(client, res->mark_list);
	    BUG(("={Mark_list not listed}\n"));
	    break;
	    
112
	case rt_text_stat:
Per Cederqvist's avatar
Per Cederqvist committed
113 114 115 116
	    prot_a_output_text_stat(client, &res->text_stat);
	    BUG(("={Text_stat struct not listed}\n"));
	    break;
	    
117
	case rt_text_list:
Per Cederqvist's avatar
Per Cederqvist committed
118 119 120 121
	    prot_a_output_text_list(client, res->text_list);
	    BUG(("={Text_list not listed}\n"));
	    break;

122
	case rt_who_info_list:
Per Cederqvist's avatar
Per Cederqvist committed
123 124 125 126
	    prot_a_output_who_info_list(client, res->who_info_list);
	    BUG(("={Who_info_list not listed}\n"));
	    break;

127
	case rt_who_info_list_old:
Per Cederqvist's avatar
Per Cederqvist committed
128 129 130 131 132
	    prot_a_output_who_info_list_old(client,
					    res->who_info_list_old);
	    BUG(("={Old who_info_list not listed}\n"));
	    break;

133
	case rt_session_info:
Per Cederqvist's avatar
Per Cederqvist committed
134 135 136 137 138
	    prot_a_output_session_info(client,
				       &res->session_info);
	    BUG(("={Session_info not listed}\n"));
	    break;
    
139
	case rt_info:
Per Cederqvist's avatar
Per Cederqvist committed
140 141 142 143
	    prot_a_output_info(client, &res->info);
	    BUG(("={Who_info struct not listed}\n"));
	    break;

144
	case rt_membership_list:
Per Cederqvist's avatar
Per Cederqvist committed
145 146 147 148
	    prot_a_output_membership_list (client, res->membership_list);
	    BUG (("={Membership_list not listed}\n"));
	    break;
	    
149
	case rt_member_list:
Per Cederqvist's avatar
Per Cederqvist committed
150 151 152 153
	    prot_a_output_member_list (client, res->member_list);
	    BUG(("={Member_list not listed}\n"));
	    break;

154
	case rt_time_date:
Per Cederqvist's avatar
Per Cederqvist committed
155 156 157 158
	    prot_a_output_time (client, res->time_date);
	    BUG(("=(time_t)%d\n", res->time_date));
	    break;

159
	case rt_session_no:
Per Cederqvist's avatar
Per Cederqvist committed
160 161 162
	    prot_a_output_session_no (client, res->session_no);
	    BUG(("=(Session_no)%d\n", res->session_no));
	    break;
163 164 165 166 167

	case rt_text_no:
	    prot_a_output_text_no (client, res->text_no);
	    BUG(("=(Text_no)%d\n", res->text_no));
	    break;
Per Cederqvist's avatar
Per Cederqvist committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	}
	mux_putc('\n', client);
    }
    else
    {
	/* Failure. Give a reply with the error message. */
	mux_printf(client, "%%%d %d %d\n",
		client->ref_no, kom_errno, err_stat);
	BUG(("%%Err %d\n", kom_errno));
    }

    mux_flush(client);
}

/*
 * Check if it is a legal function.
 *
 * BUG: This should be generated from fncdef.txt.
 */


/*
 * Set up all data structures that are private to protocol A. This
 * function is called from connections.c whenever a connection says
 * that it is of type A.
 */
void
prot_a_init(Connection *conn)
{
    conn->parse_pos = 0;
    conn->fnc_parse_pos = 0;
    conn->array_parse_pos = 0;
    conn->struct_parse_pos = 0;
    conn->string_parse_pos = 0;
    conn->ref_no = 0;
    conn->function = 0;
    conn->num0 = 0;
    conn->num1 = 0;
    conn->num2 = 0;
    conn->num3 = 0;
    conn->c_string0 = EMPTY_STRING;
    conn->c_string1 = EMPTY_STRING;
    conn->string0 = EMPTY_STRING;
    conn->c_misc_info_p = NULL;
    conn->c_local_text_no_p = NULL;
    conn->priv_bits = DEFAULT_PRIV_BITS;
    conn->conf_type = NULL_CONF_TYPE;
}

void
prot_a_destruct(Connection *conn)
{

    /*
     * All strings et c should already by free:d - that is done at the
     * end of each atomic call. But free them anyhow, just in case
     * this client is forced of.
     */

    s_clear(&conn->string0);
    s_clear(&conn->c_string0);
    s_clear(&conn->c_string1);
    sfree(conn->c_misc_info_p);
    sfree(conn->c_local_text_no_p);
}


static Bool
prot_a_is_legal_fnc(Call_header fnc)
{
    switch(fnc)
    {
    case call_fnc_login: 
    case call_fnc_logout: 
    case call_fnc_pepsi: 
    case call_fnc_change_name: 
    case call_fnc_change_what_i_am_doing: 
    case call_fnc_create_person: 
    case call_fnc_get_person_stat_old:
    case call_fnc_set_priv_bits:
    case call_fnc_set_passwd: 
    case call_fnc_query_read_texts: 
    case call_fnc_create_conf: 
    case call_fnc_delete_conf: 
    case call_fnc_lookup_name: 
    case call_fnc_get_conf_stat_old: 
    case call_fnc_add_member: 
    case call_fnc_sub_member: 
    case call_fnc_set_presentation: 
    case call_fnc_set_etc_motd: 
    case call_fnc_set_supervisor: 
    case call_fnc_set_permitted_submitters: 
    case call_fnc_set_super_conf: 
    case call_fnc_set_conf_type: 
    case call_fnc_set_garb_nice: 
    case call_fnc_get_marks: 
    case call_fnc_mark_text: 
    case call_fnc_get_text: 
    case call_fnc_get_text_stat: 
    case call_fnc_mark_as_read: 
    case call_fnc_create_text: 
    case call_fnc_delete_text: 
    case call_fnc_add_recipient: 
    case call_fnc_sub_recipient: 
    case call_fnc_add_comment: 
    case call_fnc_sub_comment: 
    case call_fnc_get_map: 
    case call_fnc_get_time: 
    case call_fnc_get_info:
    case call_fnc_add_footnote:
    case call_fnc_sub_footnote:
    case call_fnc_who_is_on_old:
    case call_fnc_set_unread:
    case call_fnc_set_motd_of_lyskom:
    case call_fnc_enable:
    case call_fnc_sync:
    case call_fnc_shutdown:
    case call_fnc_broadcast:
    case call_fnc_get_membership:
    case call_fnc_get_created_texts:
    case call_fnc_get_members:
    case call_fnc_get_person_stat:
    case call_fnc_get_conf_stat:
    case call_fnc_who_is_on:
    case call_fnc_get_unread_confs:
    case call_fnc_send_message:
    case call_fnc_get_session_info:
    case call_fnc_disconnect:
    case call_fnc_who_am_i:
Per Cederqvist's avatar
Per Cederqvist committed
297
    case call_fnc_set_user_area:
298
    case call_fnc_get_last_text:
Per Cederqvist's avatar
Per Cederqvist committed
299
    case call_fnc_create_anonymous_text:
300 301
    case call_fnc_find_next_text_no:
    case call_fnc_find_previous_text_no:
Per Cederqvist's avatar
Per Cederqvist committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	return TRUE;

    default:
	return FALSE;
    }
}    


void
prot_a_parse_packet(Connection *client)
{
    if ( s_empty(client->username) )
    {
	/* Connection not established yet */

	prot_a_parse_string(client, &client->c_string0, USERNAME_LEN);

	client->username = client->c_string0; /* Kludge to deal with */
	client->c_string0 = EMPTY_STRING;     /* "A5B" as first input. */
				/* Protokoll B will not suffer from this... */
	
Per Cederqvist's avatar
Per Cederqvist committed
323 324
	if ( s_strcat(&client->username,
		      s_fcrea_str((const unsigned char *)"@")) != OK )
Per Cederqvist's avatar
Per Cederqvist committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	    restart_kom("prot_a_parse_packet: s_strcat\n");

	if ( s_strcat(&client->username, client->hostname) != OK )
	    restart_kom("prot_a_parse_packet: s_strcat II\n");

	mux_printf(client, "LysKOM\n");
	mux_flush(client);
	BUG(("[Client %d is logged on]\n", client->session_no));
    }	

    switch(client->parse_pos)
    {
    case 0:			/* Get ref_no */
	client->ref_no = prot_a_parse_long(client);
	client->parse_pos = 1;
	/* Fall through */
    case 1:			/* Get fnc_no */
	client->function = prot_a_parse_long(client);
	if ( !prot_a_is_legal_fnc(client->function) )
	    longjmp(parse_env, ISC_PROTOCOL_ERR);
	client->parse_pos = 2;
	/* Fall through */
    case 2:
	/* Call the function that parses the arguments for this call. */
	fnc_defs[client->function].parser(client);
	/* Fall through */
    default:
	client->parse_pos = 0;
    }

}