1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
68
69
70
71
72
73
74
75
76
77
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
110
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
297
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***************************************************************************
* Description: Next generation bi-directional protocol handler. *
* Author: Henri Gomez <hgomez@apache.org> *
* Version: $Revision: 466585 $ *
***************************************************************************/
#include "jk_global.h"
#include "jk_util.h"
#include "jk_map.h"
#include "jk_ajp_common.h"
#include "jk_ajp14.h"
#include "jk_md5.h"
/*
* Compute the MD5 with ENTROPY / SECRET KEY
*/
void ajp14_compute_md5(jk_login_service_t *s, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
jk_md5((const unsigned char *)s->entropy,
(const unsigned char *)s->secret_key, s->computed_key);
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG, "(%s/%s) -> (%s)",
s->entropy, s->secret_key, s->computed_key);
JK_TRACE_EXIT(l);
}
/*
* Build the Login Init Command
*
* +-------------------------+---------------------------+---------------------------+
* | LOGIN INIT CMD (1 byte) | NEGOCIATION DATA (32bits) | WEB SERVER INFO (CString) |
* +-------------------------+---------------------------+---------------------------+
*
*/
int ajp14_marshal_login_init_into_msgb(jk_msg_buf_t *msg,
jk_login_service_t *s, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* LOGIN
*/
if (jk_b_append_byte(msg, AJP14_LOGINIT_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* NEGOCIATION FLAGS
*/
if (jk_b_append_long(msg, s->negociation)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* WEB-SERVER NAME
*/
if (jk_b_append_string(msg, s->web_server_name)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the web_server_name string");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Login Seed Command
*
* +-------------------------+---------------------------+
* | LOGIN SEED CMD (1 byte) | MD5 of entropy (32 chars) |
* +-------------------------+---------------------------+
*
*/
int ajp14_unmarshal_login_seed(jk_msg_buf_t *msg,
jk_login_service_t *s, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
if (jk_b_get_bytes
(msg, (unsigned char *)s->entropy, AJP14_ENTROPY_SEED_LEN) < 0) {
jk_log(l, JK_LOG_ERROR,
"can't get seed");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
s->entropy[AJP14_ENTROPY_SEED_LEN] = 0; /* Just to have a CString */
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Build the Login Computed Command
*
* +-------------------------+---------------------------------------+
* | LOGIN COMP CMD (1 byte) | MD5 of RANDOM + SECRET KEY (32 chars) |
* +-------------------------+---------------------------------------+
*
*/
int ajp14_marshal_login_comp_into_msgb(jk_msg_buf_t *msg,
jk_login_service_t *s, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* LOGIN
*/
if (jk_b_append_byte(msg, AJP14_LOGCOMP_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* COMPUTED-SEED
*/
if (jk_b_append_bytes
(msg, (const unsigned char *)s->computed_key,
AJP14_COMPUTED_KEY_LEN)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the COMPUTED MD5 bytes");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the LogOk Command
*
* +--------------------+------------------------+-------------------------------+
* | LOGOK CMD (1 byte) | NEGOCIED DATA (32bits) | SERVLET ENGINE INFO (CString) |
* +--------------------+------------------------+-------------------------------+
*
*/
int ajp14_unmarshal_log_ok(jk_msg_buf_t *msg,
jk_login_service_t *s, jk_logger_t *l)
{
unsigned long nego;
char *sname;
JK_TRACE_ENTER(l);
nego = jk_b_get_long(msg);
if (nego == 0xFFFFFFFF) {
jk_log(l, JK_LOG_ERROR,
"can't get negociated data");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
sname = (char *)jk_b_get_string(msg);
if (!sname) {
jk_log(l, JK_LOG_ERROR,
"can't get servlet engine name");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
if (s->servlet_engine_name) /* take care of removing previously allocated data */
free(s->servlet_engine_name);
s->servlet_engine_name = strdup(sname);
if (!s->servlet_engine_name) {
jk_log(l, JK_LOG_ERROR,
"can't malloc servlet engine name");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Log Nok Command
*
* +---------------------+-----------------------+
* | LOGNOK CMD (1 byte) | FAILURE CODE (32bits) |
* +---------------------+-----------------------+
*
*/
int ajp14_unmarshal_log_nok(jk_msg_buf_t *msg, jk_logger_t *l)
{
unsigned long status;
JK_TRACE_ENTER(l);
status = jk_b_get_long(msg);
if (status == 0xFFFFFFFF) {
jk_log(l, JK_LOG_ERROR,
"can't get failure code");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
jk_log(l, JK_LOG_INFO, "Can't Log with servlet engine - code %08lx",
status);
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Build the Shutdown Cmd
*
* +-----------------------+---------------------------------------+
* | SHUTDOWN CMD (1 byte) | MD5 of RANDOM + SECRET KEY (32 chars) |
* +-----------------------+---------------------------------------+
*
*/
int ajp14_marshal_shutdown_into_msgb(jk_msg_buf_t *msg,
jk_login_service_t *s, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* SHUTDOWN CMD
*/
if (jk_b_append_byte(msg, AJP14_SHUTDOWN_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* COMPUTED-SEED
*/
if (jk_b_append_bytes
(msg, (const unsigned char *)s->computed_key,
AJP14_COMPUTED_KEY_LEN)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the COMPUTED MD5 bytes");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Shutdown Nok Command
*
* +----------------------+-----------------------+
* | SHUTNOK CMD (1 byte) | FAILURE CODE (32bits) |
* +----------------------+-----------------------+
*
*/
int ajp14_unmarshal_shutdown_nok(jk_msg_buf_t *msg, jk_logger_t *l)
{
unsigned long status;
JK_TRACE_ENTER(l);
status = jk_b_get_long(msg);
if (status == 0xFFFFFFFF) {
jk_log(l, JK_LOG_ERROR,
"can't get failure code");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
jk_log(l, JK_LOG_INFO, "Can't shutdown servlet engine - code %08lx",
status);
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Build the Unknown Packet
*
* +-----------------------------+---------------------------------+------------------------------+
* | UNKNOWN PACKET CMD (1 byte) | UNHANDLED MESSAGE SIZE (16bits) | UNHANDLED MESSAGE (bytes...) |
* +-----------------------------+---------------------------------+------------------------------+
*
*/
int ajp14_marshal_unknown_packet_into_msgb(jk_msg_buf_t *msg,
jk_msg_buf_t *unk, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* UNKNOWN PACKET CMD
*/
if (jk_b_append_byte(msg, AJP14_UNKNOW_PACKET_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* UNHANDLED MESSAGE SIZE
*/
if (jk_b_append_int(msg, (unsigned short)unk->len)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* UNHANDLED MESSAGE (Question : Did we have to send all the message or only part of)
* ( ie: only 1k max )
*/
if (jk_b_append_bytes(msg, unk->buf, unk->len)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the UNHANDLED MESSAGE");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Build the Context Query Cmd (autoconf)
*
* +--------------------------+---------------------------------+
* | CONTEXT QRY CMD (1 byte) | VIRTUAL HOST NAME (CString (*)) |
* +--------------------------+---------------------------------+
*
*/
int ajp14_marshal_context_query_into_msgb(jk_msg_buf_t *msg,
char *virtual, jk_logger_t *l)
{
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* CONTEXT QUERY CMD
*/
if (jk_b_append_byte(msg, AJP14_CONTEXT_QRY_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* VIRTUAL HOST CSTRING
*/
if (jk_b_append_string(msg, virtual)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the virtual host string");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Context Info Cmd (Autoconf)
*
* The Autoconf feature of AJP14, let us know which URL/URI could
* be handled by the servlet-engine
*
* +---------------------------+---------------------------------+----------------------------+-------------------------------+-----------+
* | CONTEXT INFO CMD (1 byte) | VIRTUAL HOST NAME (CString (*)) | CONTEXT NAME (CString (*)) | URL1 [\n] URL2 [\n] URL3 [\n] | NEXT CTX. |
* +---------------------------+---------------------------------+----------------------------+-------------------------------+-----------+
*/
int ajp14_unmarshal_context_info(jk_msg_buf_t *msg,
jk_context_t *c, jk_logger_t *l)
{
char *vname;
char *cname;
char *uri;
vname = (char *)jk_b_get_string(msg);
JK_TRACE_ENTER(l);
jk_log(l, JK_LOG_DEBUG,
"get virtual %s for virtual %s",
vname, c->virt);
if (!vname) {
jk_log(l, JK_LOG_ERROR,
"can't get virtual hostname");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/* Check if we get the correct virtual host */
if (c->virt != NULL && vname != NULL && strcmp(c->virt, vname)) {
/* set the virtual name, better to add to a virtual list ? */
if (context_set_virtual(c, vname) == JK_FALSE) {
jk_log(l, JK_LOG_ERROR,
"can't malloc virtual hostname");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
for (;;) {
cname = (char *)jk_b_get_string(msg);
if (!cname) {
jk_log(l, JK_LOG_ERROR,
"can't get context");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
jk_log(l, JK_LOG_DEBUG,
"get context %s for virtual %s",
cname, vname);
/* grab all contexts up to empty one which indicate end of contexts */
if (!strlen(cname))
break;
/* create new context base (if needed) */
if (context_add_base(c, cname) == JK_FALSE) {
jk_log(l, JK_LOG_ERROR,
"can't add/set context %s",
cname);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
for (;;) {
uri = (char *)jk_b_get_string(msg);
if (!uri) {
jk_log(l, JK_LOG_ERROR,
"can't get URI");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
if (!strlen(uri)) {
jk_log(l, JK_LOG_DEBUG, "No more URI for context %s", cname);
break;
}
jk_log(l, JK_LOG_INFO,
"Got URI (%s) for virtualhost %s and context %s", uri,
vname, cname);
if (context_add_uri(c, cname, uri) == JK_FALSE) {
jk_log(l, JK_LOG_ERROR,
"can't add/set uri (%s) for context %s",
uri, cname);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Build the Context State Query Cmd
*
* We send the list of contexts where we want to know state, empty string end context list*
* If cname is set, only ask about THIS context
*
* +----------------------------+----------------------------------+----------------------------+----+
* | CONTEXT STATE CMD (1 byte) | VIRTUAL HOST NAME (CString (*)) | CONTEXT NAME (CString (*)) | .. |
* +----------------------------+----------------------------------+----------------------------+----+
*
*/
int ajp14_marshal_context_state_into_msgb(jk_msg_buf_t *msg,
jk_context_t *c,
char *cname, jk_logger_t *l)
{
jk_context_item_t *ci;
int i;
JK_TRACE_ENTER(l);
/* To be on the safe side */
jk_b_reset(msg);
/*
* CONTEXT STATE CMD
*/
if (jk_b_append_byte(msg, AJP14_CONTEXT_STATE_CMD)) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* VIRTUAL HOST CSTRING
*/
if (jk_b_append_string(msg, c->virt)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the virtual host string");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
if (cname) {
ci = context_find_base(c, cname);
if (!ci) {
jk_log(l, JK_LOG_ERROR,
"unknown context %s",
cname);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/*
* CONTEXT CSTRING
*/
if (jk_b_append_string(msg, cname)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the context string %s",
cname);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
else { /* Grab all contexts name */
for (i = 0; i < c->size; i++) {
/*
* CONTEXT CSTRING
*/
if (jk_b_append_string(msg, c->contexts[i]->cbase)) {
jk_log(l, JK_LOG_ERROR,
"failed appending the context string %s",
c->contexts[i]->cbase);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
}
/* End of context list, an empty string */
if (jk_b_append_string(msg, "")) {
jk_log(l, JK_LOG_ERROR,
"failed appending end of contexts");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Context State Reply Cmd
*
* We get update of contexts list, empty string end context list*
*
* +----------------------------------+---------------------------------+----------------------------+------------------+----+
* | CONTEXT STATE REPLY CMD (1 byte) | VIRTUAL HOST NAME (CString (*)) | CONTEXT NAME (CString (*)) | UP/DOWN (1 byte) | .. |
* +----------------------------------+---------------------------------+----------------------------+------------------+----+
*
*/
int ajp14_unmarshal_context_state_reply(jk_msg_buf_t *msg,
jk_context_t *c, jk_logger_t *l)
{
char *vname;
char *cname;
jk_context_item_t *ci;
JK_TRACE_ENTER(l);
/* get virtual name */
vname = (char *)jk_b_get_string(msg);
if (!vname) {
jk_log(l, JK_LOG_ERROR,
"can't get virtual hostname");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/* Check if we speak about the correct virtual */
if (strcmp(c->virt, vname)) {
jk_log(l, JK_LOG_ERROR,
"incorrect virtual %s instead of %s",
vname, c->virt);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
for (;;) {
/* get context name */
cname = (char *)jk_b_get_string(msg);
if (!cname) {
jk_log(l, JK_LOG_ERROR,
"can't get context");
JK_TRACE_EXIT(l);
return JK_FALSE;
}
if (!strlen(cname))
break;
ci = context_find_base(c, cname);
if (!ci) {
jk_log(l, JK_LOG_ERROR,
"unknow context %s for virtual %s",
cname, vname);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
ci->status = jk_b_get_int(msg);
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"updated context %s to state %d",
cname, ci->status);
}
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/*
* Decode the Context Update Cmd
*
* +-----------------------------+---------------------------------+----------------------------+------------------+
* | CONTEXT UPDATE CMD (1 byte) | VIRTUAL HOST NAME (CString (*)) | CONTEXT NAME (CString (*)) | UP/DOWN (1 byte) |
* +-----------------------------+---------------------------------+----------------------------+------------------+
*
*/
int ajp14_unmarshal_context_update_cmd(jk_msg_buf_t *msg,
jk_context_t *c, jk_logger_t *l)
{
int rc;
JK_TRACE_ENTER(l);
rc = ajp14_unmarshal_context_state_reply(msg, c, l);
JK_TRACE_EXIT(l);
return rc;
}
|