]> granicus.if.org Git - apache/blob - modules/proxy/ajp_msg.c
axe some useless or debug-inhibiting error checking
[apache] / modules / proxy / ajp_msg.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "ajp.h"
18
19 APLOG_USE_MODULE(proxy_ajp);
20
21 static char *hex_table = "0123456789ABCDEF";
22
23 /**
24  * Dump up to the first 1024 bytes on an AJP Message
25  *
26  * @param pool      pool to allocate from
27  * @param msg       AJP Message to dump
28  * @param err       error string to display
29  * @return          dump message
30  */
31 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err)
32 {
33     apr_size_t  i, j;
34     char        line[80];
35     char        *current;
36     char        *rv, *p;
37     apr_size_t  bl = 8192;
38     apr_byte_t  x;
39     apr_size_t  len = msg->len;
40
41     /* Display only first 1024 bytes */
42     if (len > 1024)
43         len = 1024;
44     rv = apr_palloc(pool, bl);
45     apr_snprintf(rv, bl,
46                  "ajp_msg_dump(): %s pos=%" APR_SIZE_T_FMT
47                  " len=%" APR_SIZE_T_FMT " max=%" APR_SIZE_T_FMT "\n",
48                  err, msg->pos, msg->len, msg->max_size);
49     bl -= strlen(rv);
50     p = rv + strlen(rv);
51     for (i = 0; i < len; i += 16) {
52         current = line;
53
54         for (j = 0; j < 16; j++) {
55              x = msg->buf[i + j];
56
57             *current++ = hex_table[x >> 4];
58             *current++ = hex_table[x & 0x0f];
59             *current++ = ' ';
60         }
61         *current++ = ' ';
62         *current++ = '-';
63         *current++ = ' ';
64         for (j = 0; j < 16; j++) {
65             x = msg->buf[i + j];
66
67             if (x > 0x20 && x < 0x7F) {
68                 *current++ = x;
69             }
70             else {
71                 *current++ = '.';
72             }
73         }
74
75         *current++ = '\0';
76         apr_snprintf(p, bl,
77                      "ajp_msg_dump(): %.4lx    %s\n",
78                      (unsigned long)i, line);
79         bl -= strlen(rv);
80         p = rv + strlen(rv);
81
82     }
83
84     return rv;
85 }
86
87
88 /**
89  * Check a new AJP Message by looking at signature and return its size
90  *
91  * @param msg       AJP Message to check
92  * @param len       Pointer to returned len
93  * @return          APR_SUCCESS or error
94  */
95 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
96 {
97     apr_byte_t *head = msg->buf;
98     apr_size_t msglen;
99
100     if (!((head[0] == 0x41 && head[1] == 0x42) ||
101           (head[0] == 0x12 && head[1] == 0x34))) {
102
103         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
104                       "ajp_msg_check_header() got bad signature %02x%02x",
105                       head[0], head[1]);
106
107         return AJP_EBAD_SIGNATURE;
108     }
109
110     msglen  = ((head[2] & 0xff) << 8);
111     msglen += (head[3] & 0xFF);
112
113     if (msglen > msg->max_size) {
114         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
115                      "ajp_msg_check_header() incoming message is "
116                      "too big %" APR_SIZE_T_FMT ", max is %" APR_SIZE_T_FMT,
117                      msglen, msg->max_size);
118         return AJP_ETOBIG;
119     }
120
121     msg->len = msglen + AJP_HEADER_LEN;
122     msg->pos = AJP_HEADER_LEN;
123     *len     = msglen;
124
125     return APR_SUCCESS;
126 }
127
128 /**
129  * Reset an AJP Message
130  *
131  * @param msg       AJP Message to reset
132  * @return          APR_SUCCESS or error
133  */
134 apr_status_t ajp_msg_reset(ajp_msg_t *msg)
135 {
136     msg->len = AJP_HEADER_LEN;
137     msg->pos = AJP_HEADER_LEN;
138
139     return APR_SUCCESS;
140 }
141
142 /**
143  * Reuse an AJP Message
144  *
145  * @param msg       AJP Message to reuse
146  * @return          APR_SUCCESS or error
147  */
148 apr_status_t ajp_msg_reuse(ajp_msg_t *msg)
149 {
150     apr_byte_t *buf;
151     apr_size_t max_size;
152
153     buf = msg->buf;
154     max_size = msg->max_size;
155     memset(msg, 0, sizeof(ajp_msg_t));
156     msg->buf = buf;
157     msg->max_size = max_size;
158     msg->header_len = AJP_HEADER_LEN;
159     ajp_msg_reset(msg);
160     return APR_SUCCESS;
161 }
162
163 /**
164  * Mark the end of an AJP Message
165  *
166  * @param msg       AJP Message to end
167  * @return          APR_SUCCESS or error
168  */
169 apr_status_t ajp_msg_end(ajp_msg_t *msg)
170 {
171     apr_size_t len = msg->len - AJP_HEADER_LEN;
172
173     if (msg->server_side) {
174         msg->buf[0] = 0x41;
175         msg->buf[1] = 0x42;
176     }
177     else {
178         msg->buf[0] = 0x12;
179         msg->buf[1] = 0x34;
180     }
181
182     msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF);
183     msg->buf[3] = (apr_byte_t)(len & 0xFF);
184
185     return APR_SUCCESS;
186 }
187
188 static APR_INLINE int ajp_log_overflow(ajp_msg_t *msg, const char *context)
189 {
190     ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
191                  "%s(): BufferOverflowException %" APR_SIZE_T_FMT
192                  " %" APR_SIZE_T_FMT,
193                  context, msg->pos, msg->len);
194     return AJP_EOVERFLOW;
195 }
196
197 /**
198  * Add an unsigned 32bits value to AJP Message
199  *
200  * @param msg       AJP Message to get value from
201  * @param value     value to add to AJP Message
202  * @return          APR_SUCCESS or error
203  */
204 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value)
205 {
206     apr_size_t len = msg->len;
207
208     if ((len + 4) > msg->max_size) {
209         return ajp_log_overflow(msg, "ajp_msg_append_uint32");
210     }
211
212     msg->buf[len]     = (apr_byte_t)((value >> 24) & 0xFF);
213     msg->buf[len + 1] = (apr_byte_t)((value >> 16) & 0xFF);
214     msg->buf[len + 2] = (apr_byte_t)((value >> 8) & 0xFF);
215     msg->buf[len + 3] = (apr_byte_t)(value & 0xFF);
216
217     msg->len += 4;
218
219     return APR_SUCCESS;
220 }
221
222 /**
223  * Add an unsigned 16bits value to AJP Message
224  *
225  * @param msg       AJP Message to get value from
226  * @param value     value to add to AJP Message
227  * @return          APR_SUCCESS or error
228  */
229 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value)
230 {
231     apr_size_t len = msg->len;
232
233     if ((len + 2) > msg->max_size) {
234         return ajp_log_overflow(msg, "ajp_msg_append_uint16");
235     }
236
237     msg->buf[len]     = (apr_byte_t)((value >> 8) & 0xFF);
238     msg->buf[len + 1] = (apr_byte_t)(value & 0xFF);
239
240     msg->len += 2;
241
242     return APR_SUCCESS;
243 }
244
245 /**
246  * Add an unsigned 8bits value to AJP Message
247  *
248  * @param msg       AJP Message to get value from
249  * @param value     value to add to AJP Message
250  * @return          APR_SUCCESS or error
251  */
252 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
253 {
254     apr_size_t len = msg->len;
255
256     if ((len + 1) > msg->max_size) {
257         return ajp_log_overflow(msg, "ajp_msg_append_uint8");
258     }
259
260     msg->buf[len] = value;
261     msg->len += 1;
262
263     return APR_SUCCESS;
264 }
265
266 /**
267  *  Add a String in AJP message, and transform the String in ASCII
268  *  if convert is set and we're on an EBCDIC machine
269  *
270  * @param msg       AJP Message to get value from
271  * @param value     Pointer to String
272  * @param convert   When set told to convert String to ASCII
273  * @return          APR_SUCCESS or error
274  */
275 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
276                                       int convert)
277 {
278     size_t len;
279
280     if (value == NULL) {
281         return(ajp_msg_append_uint16(msg, 0xFFFF));
282     }
283
284     len = strlen(value);
285     if ((msg->len + len + 3) > msg->max_size) {
286         return ajp_log_overflow(msg, "ajp_msg_append_cvt_string");
287     }
288
289     /* ignore error - we checked once */
290     ajp_msg_append_uint16(msg, (apr_uint16_t)len);
291
292     /* We checked for space !!  */
293     memcpy(msg->buf + msg->len, value, len + 1); /* including \0 */
294
295     if (convert) {
296         /* convert from EBCDIC if needed */
297         ap_xlate_proto_to_ascii((char *)msg->buf + msg->len, len + 1);
298     }
299
300     msg->len += len + 1;
301
302     return APR_SUCCESS;
303 }
304
305 /**
306  * Add a Byte array to AJP Message
307  *
308  * @param msg       AJP Message to get value from
309  * @param value     Pointer to Byte array
310  * @param valuelen  Byte array len
311  * @return          APR_SUCCESS or error
312  */
313 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
314                                   apr_size_t valuelen)
315 {
316     if (! valuelen) {
317         return APR_SUCCESS; /* Shouldn't we indicate an error ? */
318     }
319
320     if ((msg->len + valuelen) > msg->max_size) {
321         return ajp_log_overflow(msg, "ajp_msg_append_bytes");
322     }
323
324     /* We checked for space !!  */
325     memcpy(msg->buf + msg->len, value, valuelen);
326     msg->len += valuelen;
327
328     return APR_SUCCESS;
329 }
330
331 /**
332  * Get a 32bits unsigned value from AJP Message
333  *
334  * @param msg       AJP Message to get value from
335  * @param rvalue    Pointer where value will be returned
336  * @return          APR_SUCCESS or error
337  */
338 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
339 {
340     apr_uint32_t value;
341
342     if ((msg->pos + 3) > msg->len) {
343         return ajp_log_overflow(msg, "ajp_msg_get_uint32");
344     }
345
346     value  = ((msg->buf[(msg->pos++)] & 0xFF) << 24);
347     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 16);
348     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 8);
349     value |= ((msg->buf[(msg->pos++)] & 0xFF));
350
351     *rvalue = value;
352     return APR_SUCCESS;
353 }
354
355
356 /**
357  * Get a 16bits unsigned value from AJP Message
358  *
359  * @param msg       AJP Message to get value from
360  * @param rvalue    Pointer where value will be returned
361  * @return          APR_SUCCESS or error
362  */
363 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
364 {
365     apr_uint16_t value;
366
367     if ((msg->pos + 1) > msg->len) {
368         return ajp_log_overflow(msg, "ajp_msg_get_uint16");
369     }
370
371     value  = ((msg->buf[(msg->pos++)] & 0xFF) << 8);
372     value += ((msg->buf[(msg->pos++)] & 0xFF));
373
374     *rvalue = value;
375     return APR_SUCCESS;
376 }
377
378 /**
379  * Peek a 16bits unsigned value from AJP Message, position in message
380  * is not updated
381  *
382  * @param msg       AJP Message to get value from
383  * @param rvalue    Pointer where value will be returned
384  * @return          APR_SUCCESS or error
385  */
386 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
387 {
388     apr_uint16_t value;
389
390     if ((msg->pos + 1) > msg->len) {
391         return ajp_log_overflow(msg, "ajp_msg_peek_uint16");
392     }
393
394     value = ((msg->buf[(msg->pos)] & 0xFF) << 8);
395     value += ((msg->buf[(msg->pos + 1)] & 0xFF));
396
397     *rvalue = value;
398     return APR_SUCCESS;
399 }
400
401 /**
402  * Peek a 8bits unsigned value from AJP Message, position in message
403  * is not updated
404  *
405  * @param msg       AJP Message to get value from
406  * @param rvalue    Pointer where value will be returned
407  * @return          APR_SUCCESS or error
408  */
409 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
410 {
411     if (msg->pos > msg->len) {
412         return ajp_log_overflow(msg, "ajp_msg_peek_uint8");
413     }
414
415     *rvalue = msg->buf[msg->pos];
416     return APR_SUCCESS;
417 }
418
419 /**
420  * Get a 8bits unsigned value from AJP Message
421  *
422  * @param msg       AJP Message to get value from
423  * @param rvalue    Pointer where value will be returned
424  * @return          APR_SUCCESS or error
425  */
426 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
427 {
428
429     if (msg->pos > msg->len) {
430         return ajp_log_overflow(msg, "ajp_msg_get_uint8");
431     }
432
433     *rvalue = msg->buf[msg->pos++];
434     return APR_SUCCESS;
435 }
436
437
438 /**
439  * Get a String value from AJP Message
440  *
441  * @param msg       AJP Message to get value from
442  * @param rvalue    Pointer where value will be returned
443  * @return          APR_SUCCESS or error
444  */
445 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue)
446 {
447     apr_uint16_t size;
448     apr_size_t   start;
449     apr_status_t status;
450
451     status = ajp_msg_get_uint16(msg, &size);
452     start = msg->pos;
453
454     if ((status != APR_SUCCESS) || (size + start > msg->max_size)) {
455         return ajp_log_overflow(msg, "ajp_msg_get_string");
456     }
457
458     msg->pos += (apr_size_t)size;
459     msg->pos++;                   /* a String in AJP is NULL terminated */
460
461     *rvalue = (const char *)(msg->buf + start);
462     return APR_SUCCESS;
463 }
464
465
466 /**
467  * Get a Byte array from AJP Message
468  *
469  * @param msg       AJP Message to get value from
470  * @param rvalue    Pointer where value will be returned
471  * @param rvalueLen Pointer where Byte array len will be returned
472  * @return          APR_SUCCESS or error
473  */
474 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
475                                apr_size_t *rvalue_len)
476 {
477     apr_uint16_t size;
478     apr_size_t   start;
479     apr_status_t status;
480
481     status = ajp_msg_get_uint16(msg, &size);
482     /* save the current position */
483     start = msg->pos;
484
485     if ((status != APR_SUCCESS) || (size + start > msg->max_size)) {
486         return ajp_log_overflow(msg, "ajp_msg_get_bytes");
487     }
488     msg->pos += (apr_size_t)size;   /* only bytes, no trailer */
489
490     *rvalue     = msg->buf + start;
491     *rvalue_len = size;
492
493     return APR_SUCCESS;
494 }
495
496
497 /**
498  * Create an AJP Message from pool
499  *
500  * @param pool      memory pool to allocate AJP message from
501  * @param size      size of the buffer to create
502  * @param rmsg      Pointer to newly created AJP message
503  * @return          APR_SUCCESS or error
504  */
505 apr_status_t ajp_msg_create(apr_pool_t *pool, apr_size_t size, ajp_msg_t **rmsg)
506 {
507     ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t));
508
509     msg->server_side = 0;
510
511     msg->buf = (apr_byte_t *)apr_palloc(pool, size);
512     msg->len = 0;
513     msg->header_len = AJP_HEADER_LEN;
514     msg->max_size = size;
515     *rmsg = msg;
516
517     return APR_SUCCESS;
518 }
519
520 /**
521  * Recopy an AJP Message to another
522  *
523  * @param smsg      source AJP message
524  * @param dmsg      destination AJP message
525  * @return          APR_SUCCESS or error
526  */
527 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg)
528 {
529     if (smsg->len > smsg->max_size) {
530         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
531                      "ajp_msg_copy(): destination buffer too "
532                      "small %" APR_SIZE_T_FMT ", max size is %" APR_SIZE_T_FMT,
533                      smsg->len, smsg->max_size);
534         return  AJP_ETOSMALL;
535     }
536
537     memcpy(dmsg->buf, smsg->buf, smsg->len);
538     dmsg->len = smsg->len;
539     dmsg->pos = smsg->pos;
540
541     return APR_SUCCESS;
542 }
543
544
545 /**
546  * Serialize in an AJP Message a PING command
547  *
548  * +-----------------------+
549  * | PING CMD (1 byte)     |
550  * +-----------------------+
551  *
552  * @param smsg      AJP message to put serialized message
553  * @return          APR_SUCCESS or error
554  */
555 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg)
556 {
557     apr_status_t rc;
558     ajp_msg_reset(msg);
559
560     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_PING)) != APR_SUCCESS)
561         return rc;
562
563     return APR_SUCCESS;
564 }
565
566 /**
567  * Serialize in an AJP Message a CPING command
568  *
569  * +-----------------------+
570  * | CPING CMD (1 byte)    |
571  * +-----------------------+
572  *
573  * @param smsg      AJP message to put serialized message
574  * @return          APR_SUCCESS or error
575  */
576 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg)
577 {
578     apr_status_t rc;
579     ajp_msg_reset(msg);
580
581     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_CPING)) != APR_SUCCESS)
582         return rc;
583
584     return APR_SUCCESS;
585 }