mux-parse.c 5.76 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
4
5
6
7
8
9
/*
 ** mux-parse.c                                    Handle the MUX protocol
 * This is a hack. Clean this mess up with lacgen.
 */
 
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include "lyskomd.h"
10
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
11
12
13
14
15
16
17
18
19
20
21
22
#include <kom-types.h>
#include "com.h"
#include "connections.h"
#include "isc.h"
#include "mux.h"
#include "smalloc.h"
#include <setjmp.h>
#include <string.h>
#include <stdio.h>
#include "mux-parse.h"
#include "minmax.h"
#include "config.h"
23
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
24
25
26
27
28
29
30
31
32
33
34
35

jmp_buf mux_parse_env;

static String
mux_get_token(Mux *mux)
{
    String result;
    String_size old_first;

    old_first = mux->parse.first_to_parse;
    
    result = s_strtok(mux->parse.unparsed, &mux->parse.first_to_parse,
36
		      s_fcrea_str(WHITESPACE));
Per Cederqvist's avatar
Per Cederqvist committed
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

    /* Check that there was at least one trailing blank. */
    
    if ( mux->parse.first_to_parse >= s_strlen(mux->parse.unparsed) )
    {
	mux->parse.first_to_parse = old_first;
	longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
    }

    return result;
}

static long
mux_parse_long(Mux *mux)
{
    String	token;
    String_size end;
    long	res;

    token = mux_get_token(mux);
    res = s_strtol(token, &end, PROTOCOL_NUMBER_BASE);
    if (end != s_strlen(token))
	longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
    
    return res;
}


/*
 * Parse a string. At most 'maxlen' characters are allowed. If the
 * mux sends a longer string only the first 'maxlen+1' characters
68
69
70
71
72
 * are read. Any remaining characters are discarded.
 *
 * longjmp if not enough characters are available or if there
 * is an error. Result is used to save state in that case.
 * +++ Result should not be used to save state.
Per Cederqvist's avatar
Per Cederqvist committed
73
74
75
76
77
78
79
80
81
82
 */
static void
mux_parse_string(Mux	    *mux,
		 String	    *result,
		 int	     maxlen)
{
    String_size hptr;		/* Pointer to 'H' */
    String_size mux_len;	/* The len the mux is sending. */
    String_size truncated_len;	/* How much the server will receive. */
    String_size to_skip;
83
    static u_long err_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
84
85
86
87

    switch ( mux->parse.string_parse_pos )
    {
    case 0:
88
89
90
91
92
93
94
95
96
97
	if ( ( result->len != 0 || result->string != NULL) && err_cnt++ < 20 )
	{
	    log ("mux_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
98
	/* Get number and discard trailing 'H' */
99
100
101
102
	mux_len = s_strtol(s_fsubstr(mux->parse.unparsed,
				     mux->parse.first_to_parse,
				     END_OF_STRING),
			   &hptr, PROTOCOL_NUMBER_BASE);
Per Cederqvist's avatar
Per Cederqvist committed
103
104
105
106
107
108
109
110
111
112
113
114

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

115
116
	/* +++ Use s-string.c to extract the char. */
	/* +++ Fix prot_a_parse_string also. */
Per Cederqvist's avatar
Per Cederqvist committed
117
118
119
120
121
122
123
124
125
	if ( mux->parse.unparsed.string[ mux->parse.first_to_parse
					  + hptr ] != 'H'
	    || hptr <= 0 )
	{
	    longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
	}

	mux->parse.first_to_parse += 1 + hptr;
	mux->parse.string_parse_pos = 1;
126
	result->len = mux_len;	/* +++ Transfer mux_len. */
Per Cederqvist's avatar
Per Cederqvist committed
127
128
	/* Fall through */
    case 1:
129
	mux_len = result->len;
Per Cederqvist's avatar
Per Cederqvist committed
130
131
132
	/* 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) */
133
	truncated_len = min(maxlen + 1, mux_len);
Per Cederqvist's avatar
Per Cederqvist committed
134
135
136
137
138
139
140
	
	if ( mux->parse.first_to_parse + truncated_len
	    > s_strlen(mux->parse.unparsed) )
	{
	    longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
	}

141
142
	*result = EMPTY_STRING;

143
144
145
146
	s_mem_crea_str(result,
		       mux->parse.unparsed.string + mux->parse.first_to_parse,
		       truncated_len);
	
Per Cederqvist's avatar
Per Cederqvist committed
147
148
	mux->parse.first_to_parse += truncated_len;
	mux->parse.string_parse_pos = 2;
149
150
151
152
153
	result->len = mux_len;	/* Can't transfer the local mux_len across
				 * call boundary. +++ There should be room
				 * for mux_len in the per-connection
				 * protocol data.
				 */
Per Cederqvist's avatar
Per Cederqvist committed
154
155
156
157
	/* Fall through */
    case 2:
	/* Was the string too long? If so, skip the truncated data. */

158
	mux_len = result->len;	/* +++ shouldn't modify ->len */
159
	truncated_len = min(maxlen+1, mux_len);
Per Cederqvist's avatar
Per Cederqvist committed
160
161
162
163
	
	if ( mux_len > truncated_len )
	{
	    to_skip = min(mux_len - truncated_len,
164
165
166
167
			  mux->parse.unparsed.len - mux->parse.first_to_parse);
	    /* From now on, mux_len is length of result + numer of
	       chars to skip. Clean up this! +++
	     */
Per Cederqvist's avatar
Per Cederqvist committed
168
169
170
	    mux_len -= to_skip;
	    mux->parse.first_to_parse += to_skip;
	}
171
 
Per Cederqvist's avatar
Per Cederqvist committed
172
173
174
	result->len = mux_len;
	
	if ( mux_len > truncated_len )
175
176
	{
	    /* +++ mux_len is transferred in result->len. */
Per Cederqvist's avatar
Per Cederqvist committed
177
	    longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
178
179
	}
	
Per Cederqvist's avatar
Per Cederqvist committed
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
	/* Fall through */
    default:
	mux->parse.string_parse_pos = 0;
    }
}





static Bool
mux_is_legal_fnc(int fnc)
{
    switch(fnc)
    {
    case 0:
    case 1:
    case 2:
    case 3:
	return TRUE;

    default:
	return FALSE;
    }
}    

static void
mux_parse_client_message(Mux *mux)
{
    switch(mux->parse.parse_pos_2)
    {
    case 0:
	mux->parse.num = mux_parse_long(mux);
	mux->parse.parse_pos_2 = 1;
	/* Fall through */
    case 1:
	mux_parse_string(mux, &mux->parse.string, 10000);
	/* Fall through */
    default:
	mux->parse.parse_pos_2 = 0;
    }
}

void
mux_parse_packet(Mux *mux)
{
    switch(mux->parse.parse_pos)
    {
    case 0:			/* Get fnc_no */
	mux->parse.function = mux_parse_long(mux);
	if ( !mux_is_legal_fnc(mux->parse.function) )
	    longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
	mux->parse.parse_pos = 1;
	/* Fall through */
    case 1:
	/* Call the function that parses the arguments for this call. */
	switch(mux->parse.function)
	{
	case 0:
	    break;

	case 2:
	    mux->parse.num = mux_parse_long(mux);
	    break;

	case 1:
	case 3:
	    mux_parse_client_message(mux);
	    break;
	}
	/* Fall through */
    default:
	mux->parse.parse_pos = 0;
    }
}