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 /** 18 * @file ajp.h 19 * @brief Apache Jserv Protocol 20 * 21 * @defgroup AJP_defines mod_proxy AJP definitions 22 * @ingroup APACHE_INTERNAL 23 * @{ 24 */ 25 26 #ifndef AJP_H 27 #define AJP_H 28 29 #include "apr_version.h" 30 #include "apr.h" 31 32 #include "apr_hooks.h" 33 #include "apr_lib.h" 34 #include "apr_strings.h" 35 #include "apr_buckets.h" 36 #include "apr_md5.h" 37 #include "apr_network_io.h" 38 #include "apr_poll.h" 39 #include "apr_pools.h" 40 #include "apr_strings.h" 41 #include "apr_uri.h" 42 #include "apr_date.h" 43 #include "apr_fnmatch.h" 44 #define APR_WANT_STRFUNC 45 #include "apr_want.h" 46 47 #if APR_HAVE_NETINET_IN_H 48 #include <netinet/in.h> 49 #endif 50 #if APR_HAVE_ARPA_INET_H 51 #include <arpa/inet.h> 52 #endif 53 54 #define AJP13_DEF_HOST "127.0.0.1" 55 #ifdef NETWARE 56 #define AJP13_DEF_PORT 9009 /* default to 9009 since 8009 is used by OS */ 57 #else 58 #define AJP13_DEF_PORT 8009 59 #endif 60 61 /* The following environment variables match mod_ssl! */ 62 #define AJP13_HTTPS_INDICATOR "HTTPS" 63 #define AJP13_SSL_PROTOCOL_INDICATOR "SSL_PROTOCOL" 64 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT" 65 #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER" 66 #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID" 67 #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE" 68 69 #ifdef AJP_USE_HTTPD_WRAP 70 #include "httpd_wrap.h" 71 #else 72 #include "httpd.h" 73 #include "http_config.h" 74 #include "http_request.h" 75 #include "http_core.h" 76 #include "http_protocol.h" 77 #include "http_main.h" 78 #include "http_log.h" 79 #endif 80 81 #include "mod_proxy.h" 82 #include "util_ebcdic.h" 83 84 /** AJP Specific error codes 85 */ 86 /** Buffer overflow exception */ 87 #define AJP_EOVERFLOW (APR_OS_START_USERERR + 1) 88 /** Destination Buffer is to small */ 89 #define AJP_ETOSMALL (APR_OS_START_USERERR + 2) 90 /** Invalid input parameters */ 91 #define AJP_EINVAL (APR_OS_START_USERERR + 3) 92 /** Bad message signature */ 93 #define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4) 94 /** Incoming message too bg */ 95 #define AJP_ETOBIG (APR_OS_START_USERERR + 5) 96 /** Missing message header */ 97 #define AJP_ENO_HEADER (APR_OS_START_USERERR + 6) 98 /** Bad message header */ 99 #define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7) 100 /** Bad message */ 101 #define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8) 102 /** Cant log via AJP14 */ 103 #define AJP_ELOGFAIL (APR_OS_START_USERERR + 9) 104 /** Bad request method */ 105 #define AJP_EBAD_METHOD (APR_OS_START_USERERR + 10) 106 107 108 /** A structure that represents ajp message */ 109 typedef struct ajp_msg ajp_msg_t; 110 111 /** A structure that represents ajp message */ 112 struct ajp_msg 113 { 114 /** The buffer holding a AJP message */ 115 apr_byte_t *buf; 116 /** The length of AJP message header (defaults to AJP_HEADER_LEN) */ 117 apr_size_t header_len; 118 /** The length of AJP message */ 119 apr_size_t len; 120 /** The current read position */ 121 apr_size_t pos; 122 /** Flag indicating the origing of the message */ 123 int server_side; 124 /** The size of the buffer */ 125 apr_size_t max_size; 126 }; 127 128 /** 129 * Signature for the messages sent from Apache to tomcat 130 */ 131 #define AJP13_WS_HEADER 0x1234 132 #define AJP_HEADER_LEN 4 133 #define AJP_HEADER_SZ_LEN 2 134 #define AJP_HEADER_SZ 6 135 #define AJP_MSG_BUFFER_SZ 8192 136 #define AJP_MAX_BUFFER_SZ 65536 137 #define AJP13_MAX_SEND_BODY_SZ (AJP_MAX_BUFFER_SZ - AJP_HEADER_SZ) 138 #define AJP_PING_PONG_SZ 128 139 140 /** Send a request from web server to container*/ 141 #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2 142 /** Write a body chunk from the servlet container to the web server */ 143 #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3 144 /** Send response headers from the servlet container to the web server. */ 145 #define CMD_AJP13_SEND_HEADERS (unsigned char)4 146 /** Marks the end of response. */ 147 #define CMD_AJP13_END_RESPONSE (unsigned char)5 148 /** Get further data from the web server if it hasn't all been transferred yet. */ 149 #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6 150 /** The web server asks the container to shut itself down. */ 151 #define CMD_AJP13_SHUTDOWN (unsigned char)7 152 /** Webserver ask container to take control (logon phase) */ 153 #define CMD_AJP13_PING (unsigned char)8 154 /** Container response to cping request */ 155 #define CMD_AJP13_CPONG (unsigned char)9 156 /** Webserver check if container is alive, since container should respond by cpong */ 157 #define CMD_AJP13_CPING (unsigned char)10 158 159 /** @} */ 160 161 /** 162 * @defgroup AJP_api AJP API functions 163 * @ingroup MOD_PROXY 164 * @{ 165 */ 166 /** 167 * Check a new AJP Message by looking at signature and return its size 168 * 169 * @param msg AJP Message to check 170 * @param len Pointer to returned len 171 * @return APR_SUCCESS or error 172 */ 173 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len); 174 175 /** 176 * Reset an AJP Message 177 * 178 * @param msg AJP Message to reset 179 * @return APR_SUCCESS or error 180 */ 181 apr_status_t ajp_msg_reset(ajp_msg_t *msg); 182 183 /** 184 * Reuse an AJP Message 185 * 186 * @param msg AJP Message to reuse 187 * @return APR_SUCCESS or error 188 */ 189 apr_status_t ajp_msg_reuse(ajp_msg_t *msg); 190 191 /** 192 * Mark the end of an AJP Message 193 * 194 * @param msg AJP Message to end 195 * @return APR_SUCCESS or error 196 */ 197 apr_status_t ajp_msg_end(ajp_msg_t *msg); 198 199 /** 200 * Add an unsigned 32bits value to AJP Message 201 * 202 * @param msg AJP Message to get value from 203 * @param value value to add to AJP Message 204 * @return APR_SUCCESS or error 205 */ 206 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value); 207 208 /** 209 * Add an unsigned 16bits value to AJP Message 210 * 211 * @param msg AJP Message to get value from 212 * @param value value to add to AJP Message 213 * @return APR_SUCCESS or error 214 */ 215 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value); 216 217 /** 218 * Add an unsigned 8bits value to AJP Message 219 * 220 * @param msg AJP Message to get value from 221 * @param value value to add to AJP Message 222 * @return APR_SUCCESS or error 223 */ 224 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value); 225 226 /** 227 * Add a String in AJP message, and transform the String in ASCII 228 * if convert is set and we're on an EBCDIC machine 229 * 230 * @param msg AJP Message to get value from 231 * @param value Pointer to String 232 * @param convert When set told to convert String to ASCII 233 * @return APR_SUCCESS or error 234 */ 235 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value, 236 int convert); 237 /** 238 * Add a String in AJP message, and transform 239 * the String in ASCII if we're on an EBCDIC machine 240 */ 241 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1) 242 243 /** 244 * Add a String in AJP message. 245 */ 246 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0) 247 248 /** 249 * Add a Byte array to AJP Message 250 * 251 * @param msg AJP Message to get value from 252 * @param value Pointer to Byte array 253 * @param valuelen Byte array len 254 * @return APR_SUCCESS or error 255 */ 256 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value, 257 apr_size_t valuelen); 258 259 /** 260 * Get a 32bits unsigned value from AJP Message 261 * 262 * @param msg AJP Message to get value from 263 * @param rvalue Pointer where value will be returned 264 * @return APR_SUCCESS or error 265 */ 266 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue); 267 268 /** 269 * Get a 16bits unsigned value from AJP Message 270 * 271 * @param msg AJP Message to get value from 272 * @param rvalue Pointer where value will be returned 273 * @return APR_SUCCESS or error 274 */ 275 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue); 276 277 /** 278 * Peek a 16bits unsigned value from AJP Message, position in message 279 * is not updated 280 * 281 * @param msg AJP Message to get value from 282 * @param rvalue Pointer where value will be returned 283 * @return APR_SUCCESS or error 284 */ 285 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue); 286 287 /** 288 * Get a 8bits unsigned value from AJP Message 289 * 290 * @param msg AJP Message to get value from 291 * @param rvalue Pointer where value will be returned 292 * @return APR_SUCCESS or error 293 */ 294 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue); 295 296 /** 297 * Peek a 8bits unsigned value from AJP Message, position in message 298 * is not updated 299 * 300 * @param msg AJP Message to get value from 301 * @param rvalue Pointer where value will be returned 302 * @return APR_SUCCESS or error 303 */ 304 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue); 305 306 /** 307 * Get a String value from AJP Message 308 * 309 * @param msg AJP Message to get value from 310 * @param rvalue Pointer where value will be returned 311 * @return APR_SUCCESS or error 312 */ 313 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue); 314 315 316 /** 317 * Get a Byte array from AJP Message 318 * 319 * @param msg AJP Message to get value from 320 * @param rvalue Pointer where value will be returned 321 * @param rvalue_len Pointer where Byte array len will be returned 322 * @return APR_SUCCESS or error 323 */ 324 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue, 325 apr_size_t *rvalue_len); 326 327 /** 328 * Create an AJP Message from pool 329 * 330 * @param pool memory pool to allocate AJP message from 331 * @param size size of the buffer to create 332 * @param rmsg Pointer to newly created AJP message 333 * @return APR_SUCCESS or error 334 */ 335 apr_status_t ajp_msg_create(apr_pool_t *pool, apr_size_t size, ajp_msg_t **rmsg); 336 337 /** 338 * Recopy an AJP Message to another 339 * 340 * @param smsg source AJP message 341 * @param dmsg destination AJP message 342 * @return APR_SUCCESS or error 343 */ 344 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg); 345 346 /** 347 * Serialize in an AJP Message a PING command 348 * 349 * +-----------------------+ 350 * | PING CMD (1 byte) | 351 * +-----------------------+ 352 * 353 * @param msg AJP message to put serialized message 354 * @return APR_SUCCESS or error 355 */ 356 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg); 357 358 /** 359 * Serialize in an AJP Message a CPING command 360 * 361 * +-----------------------+ 362 * | CPING CMD (1 byte) | 363 * +-----------------------+ 364 * 365 * @param msg AJP message to put serialized message 366 * @return APR_SUCCESS or error 367 */ 368 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg); 369 370 /** 371 * Dump up to the first 1024 bytes on an AJP Message 372 * 373 * @param pool pool to allocate from 374 * @param msg AJP Message to dump 375 * @param err error string to display 376 * @param count the number of bytes to dump 377 * @param buf buffer pointer for dump message 378 * @return APR_SUCCESS or error 379 */ 380 apr_status_t ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err, 381 apr_size_t count, char **buf); 382 383 /** 384 * Log an AJP message 385 * 386 * @param r The current request 387 * @param msg AJP Message to dump 388 * @param err error string to display 389 * @return APR_SUCCESS or error 390 */ 391 apr_status_t ajp_msg_log(request_rec *r, ajp_msg_t *msg, char *err); 392 393 /** 394 * Send an AJP message to backend 395 * 396 * @param sock backend socket 397 * @param msg AJP message to put serialized message 398 * @return APR_SUCCESS or error 399 */ 400 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg); 401 402 /** 403 * Receive an AJP message from backend 404 * 405 * @param sock backend socket 406 * @param msg AJP message to put serialized message 407 * @return APR_SUCCESS or error 408 */ 409 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg); 410 411 /** 412 * Build the ajp header message and send it 413 * @param sock backend socket 414 * @param r current request 415 * @param buffsize max size of the AJP packet. 416 * @param uri requested uri 417 * @param secret authentication secret 418 * @return APR_SUCCESS or error 419 */ 420 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r, 421 apr_size_t buffsize, 422 apr_uri_t *uri, 423 const char *secret); 424 425 /** 426 * Read the ajp message and return the type of the message. 427 * @param sock backend socket 428 * @param r current request 429 * @param buffsize size of the buffer. 430 * @param msg returned AJP message 431 * @return APR_SUCCESS or error 432 */ 433 apr_status_t ajp_read_header(apr_socket_t *sock, 434 request_rec *r, 435 apr_size_t buffsize, 436 ajp_msg_t **msg); 437 438 /** 439 * Allocate a msg to send data 440 * @param pool pool to allocate from 441 * @param ptr data buffer 442 * @param len the length of allocated data buffer 443 * @param msg returned AJP message 444 * @return APR_SUCCESS or error 445 */ 446 apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr, 447 apr_size_t *len, ajp_msg_t **msg); 448 449 /** 450 * Send the data message 451 * @param sock backend socket 452 * @param msg AJP message to send 453 * @param len AJP message length 454 * @return APR_SUCCESS or error 455 */ 456 apr_status_t ajp_send_data_msg(apr_socket_t *sock, 457 ajp_msg_t *msg, apr_size_t len); 458 459 /** 460 * Parse the message type 461 * @param r current request 462 * @param msg AJP message 463 * @return AJP message type. 464 */ 465 int ajp_parse_type(request_rec *r, ajp_msg_t *msg); 466 467 /** 468 * Parse the header message from container 469 * @param r current request 470 * @param conf proxy config 471 * @param msg AJP message 472 * @return APR_SUCCESS or error 473 */ 474 apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf, 475 ajp_msg_t *msg); 476 477 /** 478 * Parse the message body and return data address and length 479 * @param r current request 480 * @param msg AJP message 481 * @param len returned AJP message length 482 * @param ptr returned data 483 * @return APR_SUCCESS or error 484 */ 485 apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg, 486 apr_uint16_t *len, char **ptr); 487 488 489 /** 490 * Check the reuse flag in CMD_AJP13_END_RESPONSE 491 * @param r current request 492 * @param msg AJP message 493 * @param reuse returned reuse flag 494 * @return APR_SUCCESS or error 495 */ 496 apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg, 497 apr_byte_t *reuse); 498 499 500 /** 501 * Handle the CPING/CPONG messages 502 * @param sock backend socket 503 * @param r current request 504 * @param timeout time window for receiving cpong reply 505 * @return APR_SUCCESS or error 506 */ 507 apr_status_t ajp_handle_cping_cpong(apr_socket_t *sock, 508 request_rec *r, 509 apr_interval_time_t timeout); 510 511 512 /** 513 * Convert numeric message type into string 514 * @param type AJP message type 515 * @return AJP message type as a string 516 */ 517 const char *ajp_type_str(int type); 518 519 /** @} */ 520 521 #endif /* AJP_H */ 522 523