prot-a-parse.c 8.84 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
 * $Id: prot-a-parse.c,v 0.12 1993/10/10 17:04:58 ceder Exp $
Linus Tolke's avatar
Linus Tolke 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
29
30
/*
 * prot-a-parse.c - parse protocol-A messages.
 *
 * BUG: Not all functions are used, I think. /ceder
 */

Per Cederqvist's avatar
Per Cederqvist committed
31
static char *rcsid = "$Id: prot-a-parse.c,v 0.12 1993/10/10 17:04:58 ceder Exp $";
32
33
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
34

Per Cederqvist's avatar
Per Cederqvist committed
35
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
36
#include <setjmp.h>
Per Cederqvist's avatar
Per Cederqvist committed
37
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
38
39

#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
40
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
41
42
43
44
45
#include "com.h"
#include "connections.h"
#include "prot-a-parse.h"
#include "isc-parse.h"
#include "config.h"
Per Cederqvist's avatar
Per Cederqvist committed
46
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
47
#include "minmax.h"
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

/*
 * Return next token from the input stream. Note that the String returned
 * by this call points into data that might be freed by the next call to
 * get_token or any function which reads from the stream.
 */

static String
prot_a_get_token(Connection *client)
{
    String result;
    String_size old_first;

    old_first = client->first_to_parse;
    
    result = s_strtok(client->unparsed, &client->first_to_parse,
		      s_fcrea_str(WHITESPACE));

    /* Check that there was at least one trailing blank. */
    
    if ( client->first_to_parse >= s_strlen(client->unparsed) )
    {
	client->first_to_parse = old_first;
	longjmp(parse_env, ISC_MSG_INCOMPLETE);
    }

    return result;
}


Per Cederqvist's avatar
Per Cederqvist committed
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
long
prot_a_parse_long(Connection *client)
{
    String	token;
    String_size end;
    long	res;

    token = prot_a_get_token(client);
    res = s_strtol(token, &end, PROTOCOL_NUMBER_BASE);
    if (end != s_strlen(token))
	longjmp(parse_env, ISC_PROTOCOL_ERR);
    
    return res;
}

void
prot_a_parse_priv_bits(Connection *client,
		       Priv_bits      *res)
{
    String token;

    token = prot_a_get_token(client);

    if ( s_strlen(token) != 16 )
	longjmp(parse_env, ISC_PROTOCOL_ERR);
    
    res->wheel = token.string[ 0 ] != '0';
    res->admin = token.string[ 1 ] != '0';
    res->statistic = token.string[ 2 ] != '0';
    res->create_pers = token.string[ 3 ] != '0';
    res->create_conf = token.string[ 4 ] != '0';
    res->change_name = token.string[ 5 ] != '0';
110
    res->extern_gw = token.string[ 6 ] != '0';
Per Cederqvist's avatar
Per Cederqvist committed
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
    res->flg8 = token.string[ 7 ] != '0';
    res->flg9 = token.string[ 8 ] != '0';
    res->flg10 = token.string[ 9 ] != '0';
    res->flg11 = token.string[ 10 ] != '0';
    res->flg12 = token.string[ 11 ] != '0';
    res->flg13 = token.string[ 12 ] != '0';
    res->flg14 = token.string[ 13 ] != '0';
    res->flg15 = token.string[ 14 ] != '0';
    res->flg16 = token.string[ 15 ] != '0';
}

void
prot_a_parse_conf_type(Connection *client,
		Conf_type      *res)
{
    String token;

    token = prot_a_get_token(client);
    
    if ( s_strlen(token) != 4 )
	longjmp(parse_env, ISC_PROTOCOL_ERR);
  
    res->rd_prot = token.string[ 0 ] != '0';
    res->original = token.string[ 1 ] != '0';
    res->secret = token.string[ 2 ] != '0';
    res->letter_box = token.string[ 3 ] != '0';
}

/*
 * Parse a string. At most 'maxlen' characters are allowed. If the
 * client sends a longer string only the first 'maxlen+1' characters
Per Cederqvist's avatar
Per Cederqvist committed
142
143
144
145
 * are read. Any remaining characters are discarded.
 */
/*
 * +++ This needs cleaning up. See comments in and above mux_parse_string.
Per Cederqvist's avatar
Per Cederqvist committed
146
147
148
 */
void
prot_a_parse_string(Connection  *client,
Per Cederqvist's avatar
Per Cederqvist committed
149
150
		    String	*result,
		    int		 maxlen)
Per Cederqvist's avatar
Per Cederqvist committed
151
152
153
154
155
{
    String_size hptr;		/* Pointer to 'H' */
    String_size client_len;	/* The len the client is sending. */
    String_size truncated_len;	/* How much the server will receive. */
    String_size to_skip;
Per Cederqvist's avatar
Per Cederqvist committed
156
    static u_long err_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
157
158
159
160

    switch ( client->string_parse_pos )
    {
    case 0:
161
162
163
164
165
166
167
168
169
170
	if ( (result->len != 0 || result->string != NULL) && err_cnt++ < 20 )
	{
	    log ("prot_a_parse_string(): result->len == %lu, "
		 "result->string == %lu. This memory will not be free()'d.\n",
		 (u_long)result->len, (u_long)result->string);
	    *result = EMPTY_STRING;
	    if ( err_cnt == 20 )
		log("Won't log the above warning no more.");
	}

Per Cederqvist's avatar
Per Cederqvist committed
171
	/* Get number and discard trailing 'H' */
Per Cederqvist's avatar
Per Cederqvist committed
172
173
174
175
	client_len = s_strtol(s_fsubstr(client->unparsed,
					client->first_to_parse,
					END_OF_STRING),
			      &hptr, PROTOCOL_NUMBER_BASE);
Per Cederqvist's avatar
Per Cederqvist committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

	if ( hptr == -1
	    || client->first_to_parse + hptr
	    >= s_strlen(client->unparsed) )
	{
	    longjmp(parse_env, ISC_MSG_INCOMPLETE);
	}
	
	/* Check that
	      a) there is a trailing H
	      b) there was at least one digit before the H */

	if ( client->unparsed.string[ client->first_to_parse
					  + hptr ] != 'H'
	    || hptr <= 0 )
	{
	    longjmp(parse_env, ISC_PROTOCOL_ERR);
	}

	client->first_to_parse += 1 + hptr;
	client->string_parse_pos = 1;
197
	result->len = client_len; /* +++ Transfer */
Per Cederqvist's avatar
Per Cederqvist committed
198
199
	/* Fall through */
    case 1:
200
	client_len = result->len;
Per Cederqvist's avatar
Per Cederqvist committed
201
202
203
	/* Check that the entire string is transmitted. */
	/* (Don't care about the trailing part that will be skipped if the
	 *  string is longer than maxlen) */
Per Cederqvist's avatar
Per Cederqvist committed
204
	truncated_len = min(maxlen + 1, client_len);
Per Cederqvist's avatar
Per Cederqvist committed
205
206
207
208
209
210
211
	
	if ( client->first_to_parse + truncated_len
	    > s_strlen(client->unparsed) )
	{
	    longjmp(parse_env, ISC_MSG_INCOMPLETE);
	}

212
	*result = EMPTY_STRING;
Per Cederqvist's avatar
Per Cederqvist committed
213
214
215
216
217

	s_mem_crea_str(result,
		       client->unparsed.string + client->first_to_parse,
		       truncated_len);
	result->len = client_len; /* Ugly! +++ */
Per Cederqvist's avatar
Per Cederqvist committed
218
219
220
221
222
223
224
225

	client->first_to_parse += truncated_len;
	client->string_parse_pos = 2;
	/* Fall through */
    case 2:
	/* Was the string too long? If so, skip the truncated data. */

	client_len = result->len;
226
	truncated_len = min(maxlen+1, client_len);
Per Cederqvist's avatar
Per Cederqvist committed
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
297
	
	if ( client_len > truncated_len )
	{
	    to_skip = min(client_len - truncated_len,
			   client->unparsed.len - client->first_to_parse);
	    client_len -= to_skip;
	    client->first_to_parse += to_skip;
	}

	result->len = client_len;
	
	if ( client_len > truncated_len )
	    longjmp(parse_env, ISC_MSG_INCOMPLETE);
	/* Fall through */
    default:
	client->string_parse_pos = 0;
    }
}


extern void
prot_a_parse_misc_info(Connection *client,
		       Misc_info      *result)
{
    switch ( client->struct_parse_pos )
    {
    case 0:
	result->type = prot_a_parse_long(client);
	client->struct_parse_pos = 1;
	/* Fall through */
    case 1:
	switch( result->type )
	{
	case recpt:
	    result->datum.recipient = prot_a_parse_long(client);
	    break;

	case cc_recpt:
	    result->datum.cc_recipient = prot_a_parse_long(client);
	    break;
	    
	case loc_no:
	    result->datum.local_no = prot_a_parse_long(client);
	    break;
	    
	case comm_to:
	    result->datum.comment_to = prot_a_parse_long(client);
	    break;
	    
	case footn_to:
	    result->datum.footnote_to = prot_a_parse_long(client);
	    break;

	case comm_in:
	case footn_in:
	case rec_time:
	case sent_by:
	case sent_at:
	    /* Fall through */
	    
#ifndef COMPILE_CHECKS
	default:
#endif
	    longjmp(parse_env, ISC_PROTOCOL_ERR);
	}
    default:
	client->struct_parse_pos = 0;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
void
prot_a_parse_time_date(Connection *client,
		       struct tm  *result)
{
    /* A time never occur inside a string, and a string never occur
       inside a time. Thus I use string_parse_pos here instead of
       inventing a new variable. Lacgen will allocate variables as
       needed... */

    switch( client->string_parse_pos )
    {
    case 0:
	result->tm_sec = prot_a_parse_long(client);
	client->string_parse_pos = 1;
	/* Fall through */
    case 1:
	result->tm_min = prot_a_parse_long(client);
	client->string_parse_pos = 2;
	/* Fall through */
    case 2:
	result->tm_hour = prot_a_parse_long(client);
	client->string_parse_pos = 3;
	/* Fall through */
    case 3:
	result->tm_mday = prot_a_parse_long(client);
	client->string_parse_pos = 4;
	/* Fall through */
    case 4:
	result->tm_mon = prot_a_parse_long(client);
	client->string_parse_pos = 5;
	/* Fall through */
    case 5:
	result->tm_year = prot_a_parse_long(client);
	client->string_parse_pos = 6;
	/* Fall through */
    case 6:
	result->tm_wday = prot_a_parse_long(client);
	client->string_parse_pos = 7;
	/* Fall through */
    case 7:
	result->tm_yday = prot_a_parse_long(client);
	client->string_parse_pos = 8;
	/* Fall through */
    case 8:
	result->tm_isdst = prot_a_parse_long(client);
	/* Fall through */
    default:
	client->string_parse_pos = 0;
    }
}