forked from mod-epp/mod-epp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_epp.c
More file actions
1526 lines (1254 loc) · 41.5 KB
/
mod_epp.c
File metadata and controls
1526 lines (1254 loc) · 41.5 KB
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002 NIC.at Internet Verwaltungs- und
* Betriebsgesellschaft m. b. H. All rights reserved.
*
* Written by Otmar Lendl <lendl@nic.at>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* NIC.at Internet Verwaltungs- und Betriebsgesellschaft m. b. H."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "mod_epp" and "NIC.at" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact lendl@nic.at
*
* 5. Products derived from this software may not be called "mod_epp",
* nor may "mod_epp" appear in their name, without prior written
* permission of NIC.at.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NIC.AT INTERNET VERWALTUNGS- UND
* BETRIEBSGESELLSCHAFT M.B.H. OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "httpd.h"
#define CORE_PRIVATE
#include "http_protocol.h"
#include "http_config.h"
#include "http_connection.h"
#include "http_core.h"
#include "http_request.h"
#include "http_log.h"
#include "ap_config.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_hash.h"
#include "apr_buckets.h"
#include "apr_xml.h"
#include "apr_general.h"
#include "util_filter.h"
#include "scoreboard.h"
#include "apr_md5.h"
#include "mod_epp.h"
#include <sys/types.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
module AP_MODULE_DECLARE_DATA epp_module;
/*
* table debugging helpers
*/
int epp_dump_table_entry(void *rec, const char *key, const char *value) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
(char *)rec, key, value);
return(1);
}
void epp_dump_table(apr_table_t *t, const char *s) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"%s: Dumping table %lx", s, (long) t);
apr_table_do(&epp_dump_table_entry,(void *) "DUMP: %s: %s\n", t, NULL);
}
/*
* Generate the session identifying cookie
*
* It's a MD5 hash over
* the connection struct
* current time
* process id + parent id
*
* (we don't need unpredictability, just uniqueness)
*
*/
void epp_make_cookie(epp_user_rec *ur)
{
apr_md5_ctx_t md5ctx;
unsigned char hash[APR_MD5_DIGESTSIZE];
const char *hex = "0123456789abcdef";
char *r;
int i;
time_t t;
pid_t pids[2];
time(&t);
pids[0] = getpid();
pids[1] = getppid();
apr_md5_init(&md5ctx);
apr_md5_update(&md5ctx, (void *)ur->c, sizeof(conn_rec));
apr_md5_update(&md5ctx, (void *)&t, sizeof(t));
apr_md5_update(&md5ctx, (void *)pids, sizeof(pids));
apr_md5_final(hash, &md5ctx);
r = apr_cpystrn(ur->cookie, "session=", 9);
for (i = 0; i < APR_MD5_DIGESTSIZE; i++)
{
*r++ = hex[hash[i] >> 4];
*r++ = hex[hash[i] & 0xF];
}
*r = '\0';
}
/* two simple xml helpers from mod_jabber */
char *get_attr(apr_xml_attr *attr, const char *name)
{
if (attr == NULL) return NULL;
if (!strcmp(attr->name,name)) return (char *)attr->value;
return get_attr(attr->next, name);
}
apr_xml_elem *get_elem(apr_xml_elem *elem, const char *name)
{
if (elem == NULL) return NULL;
if (!strcmp(elem->name,name)) return elem;
return get_elem(elem->next, name);
}
/*
* actually, this is probably overkill, we parsed the XML in
* one swoop and thus don't expect fragmented cdata.
*/
void xml_firstcdata_strncat(char *dest, size_t dstsize, apr_xml_elem *elem)
{
apr_text *t;
dstsize--;
dest[dstsize] = 0;
dest[0] = 0;
for (t = elem->first_cdata.first; t; t = t->next)
{
strncat(dest, t->text, dstsize);
dstsize -= strlen(t->text);
if (dstsize < 1) break;
}
}
static request_rec *epp_create_request(epp_user_rec *ur)
{
apr_pool_t *p;
request_rec *r;
apr_pool_create(&p, ur->pool);
apr_pool_tag(p, "mod_epp_request"); /* helps debugging */
r = apr_pcalloc(p, sizeof(*r));
r->pool = p;
r->connection = ur->c;
r->server = ur->c->base_server;
ur->c->keepalive = 0;
r->user = NULL;
r->ap_auth_type = NULL;
r->allowed_methods = ap_make_method_list(p, 2);
r->headers_in = apr_table_make(r->pool, 10);
r->subprocess_env = apr_table_make(r->pool, 10); /* will need this */
r->headers_out = apr_table_make(r->pool, 1);
r->err_headers_out = apr_table_make(r->pool, 1);
r->notes = apr_table_make(r->pool, 5);
r->request_config = ap_create_request_config(r->pool);
ap_run_create_request(r);
r->per_dir_config = r->server->lookup_defaults;
r->sent_bodyct = 1;
r->bytes_sent = 0;
r->output_filters = ur->c->output_filters;
r->input_filters = ur->c->input_filters;
r->status = HTTP_OK; /* Until further notice. */
r->request_time = apr_time_now();
ap_set_module_config(r->request_config, &epp_module, ur);
apr_table_set(r->headers_in, "User-Agent", EPP_USER_AGENT);
return r;
}
/*
* Take an epp request struct and try to find the clTRID.
*
*/
apr_status_t epp_get_cltrid(epp_rec *er)
{
apr_xml_elem *id,*root,*e;
/* default to no cltrid */
er->cltrid[0] = 0;
root = er->doc->root;
if(strcmp("epp",root->name))
return(APR_BADARG);
/*
* got to first level below root.
*/
e = root->first_child;
if (e == NULL)
return(APR_BADARG);
/*
* there should be exactly one element below <epp> ...
*/
if (e->next != NULL)
return(APR_BADARG);
/*
* ... and it should not be "clTRID".
*/
if(!strcmp("clTRID",e->name))
return(APR_BADARG);
id = get_elem(e->first_child, "clTRID");
if (id == NULL)
{
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"EPP: did not find a clTRID.");
return(APR_SUCCESS);
}
xml_firstcdata_strncat(er->cltrid, sizeof(er->cltrid), id);
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"EPP: found clTRID = %s.", er->cltrid);
return(APR_SUCCESS);
}
/*
* Take an XML tree and build a URI path from the commands found
*
* TODO: actually, the tree should have been validated by now, but as
* we don't do XML schema checks right now, we go for *really* simple checks.
*
* This function returns
* APR_SUCCESS just call it
* APR_BADARG schema error
*
*
* The URI will be returned in "path". It will be either based
* on EPPSessionRoot or EPPCommandRoot.
*
* "element" will point to the relevant node in the XML tree.
*
* "login_needed" will be true if the client has to be logged in
* in order to access this URI.
*
*/
apr_status_t epp_translate_xml_to_uri(apr_xml_doc *doc, epp_rec *er,
char *path, apr_size_t path_size, apr_xml_elem **element, int *login_needed)
{
apr_xml_elem *command, *c, *hello;
epp_conn_rec *conf = er->ur->conf;
/*
* default to a schema error and no login needed.
*/
apr_snprintf(path, path_size, "%s/schema", conf->error_root);
*login_needed = 0;
if(strcmp("epp",doc->root->name))
return(APR_BADARG);
/*
* Check for a hello frame
*/
hello = get_elem(doc->root->first_child, "hello");
if (hello != NULL)
{
apr_snprintf(path, path_size, "%s/hello", conf->session_root);
if (element)
*element = hello;
return(APR_SUCCESS);
}
/*
* Not hello? Then it must be a <command>
*/
command = get_elem(doc->root->first_child, "command");
if (command == NULL)
return(APR_BADARG);
c = command->first_child;
while (c != NULL)
{
/*
* These two tags are not relevant in the search for the command.
*/
if ((!strcasecmp(c->name, "clTRID")) ||
(!strcasecmp(c->name, "extension")))
{
c = c->next;
continue;
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"XML: found command = %s.", c->name);
if (element)
*element = c;
if (!strcmp("login",c->name) || !strcmp("logout",c->name))
apr_snprintf(path, path_size, "%s/%s", conf->session_root, c->name);
else
apr_snprintf(path, path_size, "%s/%s", conf->command_root, c->name);
if (strcmp("login",c->name))
*login_needed = 1;
return(APR_SUCCESS);
}
return(APR_BADARG);
}
/*
* Check for connection close signalling
*
*/
void handle_close_request(epp_rec *er, request_rec *r)
{
const char *epp_rc;
const char *connection;
epp_conn_rec *conf = er->ur->conf;
/*
* Check for the EPP Return Code header
*/
epp_rc = apr_table_get(r->err_headers_out, conf->rc_header);
if (!epp_rc)
epp_rc = apr_table_get(r->headers_out, conf->rc_header);
/*
* Scripts can signal with "Connection: close" that they want to tear down
* the epp session.
*
* This does not work if mod_proxy is used. In this case, the remote
* script should set "X-Connection: close". For added confusion, in
* this case, the header appears in headers_out and not err_headers_out.
*
*/
connection = apr_table_get(r->err_headers_out, "Connection");
if (!connection)
connection = apr_table_get(r->headers_out, "X-Connection");
/*
* Alternatively, if they send the EPP return code in a header, use that.
*/
if (connection && !strncmp(connection, "close", 5))
{
er->ur->connection_close = 1;
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"CGI requested a connection close via Connection-header");
return;
}
if (epp_rc && (epp_rc[1] == '5')) /* x5yy means connection close */
{
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"Connection close based on EPP code %s", epp_rc);
er->ur->connection_close = 1;
}
}
/*
* Call an error handler.
*
* Parameters:
*
* script name under EPPErrorRoot
* code EPP error code
* cltrid Client transaction ID
* errmsg Human readable error message
*
* A simple hardcoded message is sent if the full request doesn't work.
*
*/
apr_status_t epp_error_handler(epp_rec *er, char *script, int code, char *cltrid, char *errmsg)
{
request_rec *r;
char req[400];
char *e, *id;
char id_xml[100] = "";
epp_conn_rec *conf = er->ur->conf;
r = epp_create_request(er->ur);
er->r = r;
/*
* html escaping is close enough to XML escaping.
*/
e = (errmsg) ? ap_escape_uri(r->pool, errmsg) : "";
id = (cltrid) ? ap_escape_uri(r->pool, cltrid) : "";
if(cltrid)
apr_snprintf(id_xml, sizeof(id_xml), "<clTRID>%s</clTRID>", id);
apr_snprintf(req, sizeof(req), "%s/%s?code=%d&clTRID=%s&msg=%s", conf->error_root,
script, code, id, e);
ap_parse_uri(r, req);
r->assbackwards = 0; /* I don't want headers. */
r->method = "GET";
r->method_number = M_GET;
r->protocol = "INCLUDED";
r->the_request = req;
apr_table_set(r->headers_in, "Cookie", er->ur->cookie);
ap_add_input_filter("EOS_INPUT", (void *) er, r, r->connection);
ap_update_child_status(r->connection->sbh, SERVER_BUSY_WRITE, r);
ap_process_request(r);
if (ap_extended_status)
ap_increment_counts(r->connection->sbh, r);
if (r->status != HTTP_OK) /* something wrong with the script runtime? Go for simple version. */
{
ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS , NULL,
"epp_error_handler: calling %s failed.", req);
r->status = HTTP_OK; /* tell the output filter that this error
should be framed and not discarded */
apr_snprintf(req, sizeof(req), "%s\n<response><result code=\"%d\"><msg>%s</msg>\n</result><trID>%s</trID></response></epp>",
EPP_BUILTIN_ERROR_HEAD,
code, e, id_xml);
ap_fputs(er->ur->c->output_filters, er->bb_out, req);
APR_BRIGADE_INSERT_TAIL(er->bb_out, apr_bucket_eos_create(r->connection->bucket_alloc));
ap_fflush(er->ur->c->output_filters, er->bb_out);
}
/*
* Did the error handle request the connection to be closed?
*/
handle_close_request(er, r);
apr_pool_destroy(r->pool);
return(APR_SUCCESS);
}
/*
* This function implements the EPP login procedure.
*
* It does not generate the answer to the client, that is left
* to a "normal" handler. Here we just check the password
* and set the internal state.
*
*/
apr_status_t epp_login(epp_rec *er, apr_xml_elem *login)
{
apr_xml_elem *clid_el, *pw_el;
epp_conn_rec *conf = er->ur->conf;
char clid[CLIDSIZE];
char pw[PWSIZE];
char *passwd;
request_rec *r;
apr_status_t res;
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS , NULL,
"epp_login: entering");
if (er->ur->authenticated)
{
epp_error_handler(er, "login", 2002, er->cltrid, "Already logged in. Use <logout> first.");
return EPP_PROT_ERROR;
}
clid_el = get_elem(login->first_child, "clID");
pw_el = get_elem(login->first_child, "pw");
if ((clid_el == NULL) || (pw_el == NULL))
{
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS , NULL,
"epp_login: clid or pw missing");
epp_error_handler(er, "schema", 2001, NULL,
"Error in login (clID and pw must be present).");
return(EPP_PROT_ERROR);
}
xml_firstcdata_strncat(clid, sizeof(clid), clid_el);
xml_firstcdata_strncat(pw, sizeof(pw), pw_el);
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS , NULL,
"epp_login: clid = %s, pw = %s", clid, pw);
passwd = apr_psprintf(er->pool, "%s:%s", clid, pw);
er->ur->auth_string = apr_psprintf(er->ur->pool, "Basic %s", ap_pbase64encode(er->ur->pool, passwd));
if (conf->implicit_login) /* implicit login, no need to do a request here */
{
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS , NULL,
"epp_login: implicit_login set, no request done");
apr_cpystrn(er->ur->clid, clid, sizeof(er->ur->clid));
apr_cpystrn(er->ur->pw, pw, sizeof(er->ur->pw));
return(APR_SUCCESS);
}
r = epp_create_request(er->ur);
er->r = r;
apr_table_set(r->headers_in, "Authorization", er->ur->auth_string);
apr_table_set(r->headers_in, "Cookie", er->ur->cookie);
r->the_request = (char *) er->ur->conf->authuri;
ap_parse_uri(r, (char *) er->ur->conf->authuri);
r->assbackwards = 0; /* I don't want headers. */
r->method = "GET";
r->method_number = M_GET;
r->protocol = "INCLUDED";
apr_table_set(r->headers_in, "Cookie", er->ur->cookie);
/*
* ap_process_request_internal does all the auth checks, but does not
* actually call the handler. Just what we want.
*/
if ((res = ap_process_request_internal(r)) == OK)
{
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS , NULL,
"epp_login (success): after ap_process_request_internal: res = %d", res);
er->ur->authenticated = 1;
apr_cpystrn(er->ur->clid, clid, sizeof(er->ur->clid));
apr_cpystrn(er->ur->pw, pw, sizeof(er->ur->pw));
apr_pool_destroy(r->pool);
return(APR_SUCCESS);
}
else
{
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS , NULL,
"epp_login (fail): after ap_process_request_internal: res = %d", res);
er->ur->authenticated = 0;
apr_pool_destroy(r->pool);
epp_error_handler(er, "login", 2200, er->cltrid, "Username/Password invalid.");
return(APR_BADARG);
}
/* not reached */
}
/*
* more or less a dummy for now
*/
apr_status_t epp_logout(epp_rec *er, apr_xml_elem *login)
{
er->ur->authenticated = 0;
er->ur->clid[0] = 0;
er->ur->pw[0] = 0;
er->ur->auth_string[0] = 0;
return(APR_SUCCESS);
}
/*
* This is the core function.
*
* We have to a full EPP frame, now build the XML object,
* do some rudimentary checking, build an URI, an request
* object and call it.
*
*/
void epp_process_frame(epp_rec *er)
{
epp_conn_rec *conf = er->ur->conf;
apr_xml_parser *xml_p;
apr_xml_doc *doc;
apr_xml_elem *tag = NULL;
int login_needed;
int is_login = 0;
apr_status_t rv;
char errstr[300];
request_rec *r;
const char *epp_rc;
char uri[200];
char content_length[20];
xml_p = apr_xml_parser_create(er->pool);
rv = apr_xml_parser_feed(xml_p, er->orig_xml, er->orig_xml_size);
apr_xml_parser_geterror(xml_p, errstr, sizeof(errstr));
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv , NULL,
"XML parser feed reports: %s", errstr);
rv = apr_xml_parser_done(xml_p,&doc);
apr_xml_parser_geterror(xml_p, errstr, sizeof(errstr));
ap_log_error(APLOG_MARK, APLOG_DEBUG, rv , NULL,
"XML parser done reports: %s", errstr);
if (rv != APR_SUCCESS)
{
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, NULL,
"not valid XML");
epp_error_handler(er, "parse", 2001, NULL, errstr);
return;
}
er->doc = doc;
rv = epp_get_cltrid(er);
if (rv != APR_SUCCESS)
{
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, NULL,
"Schema error while looking for clTRID");
epp_error_handler(er, "schema", 2001, NULL, "Detected a schema error while looking for clTRID");
return;
}
rv = epp_translate_xml_to_uri(doc, er, uri, sizeof(uri), &tag, &login_needed);
ap_log_error(APLOG_MARK, ((rv == APR_SUCCESS) ? APLOG_DEBUG : APLOG_WARNING),
APR_SUCCESS, NULL, "Translated EPP to %s", uri);
/*
* If translation failed, we already have the error URI here, thus we continue.
*/
if (tag && !strcmp("login",tag->name)) /* <login> ? */
{
is_login = 1;
rv = epp_login(er, tag);
if (rv != APR_SUCCESS)
{
return;
}
}
if (!er->ur->authenticated && login_needed) /* need login before continuing ? */
{
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, NULL,
"I can't call %s without prior login.", uri);
epp_error_handler(er, "authrequired", 2002, er->cltrid, "You need to login first.");
return;
}
/*
* now do the actual work
*/
r = epp_create_request(er->ur);
er->r = r;
apr_xml_quote_elem(r->pool,doc->root); /* hat tip Elias Sidenbladh */
apr_xml_to_text(r->pool,doc->root,APR_XML_X2T_FULL_NS_LANG, doc->namespaces ,NULL, &er->serialised_xml,
&er->serialised_xml_size);
/*
* There seems to be a bug somewhere in the size calculations.
* If I use the returned serialised_xml_size, I get trailing garbage every now and then.
* *shrug*, the let's go for strlen, as I hope that there are no null bytes in the XML.
*/
er->serialised_xml_size = strlen(er->serialised_xml);
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"XML: serialized xml size = %lu.", (unsigned long) er->serialised_xml_size);
/*
* Create the multipart/formdata MIME structure for the request
* as the downstream filter might not want to get all of it at once,
* we put it in a temporary bucket-brigade. The input filter will then
* hand off data in the desired chunks.
*/
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0 , NULL,
"epp_process_frame: creating the multipart/form-data (framesize: %lu)", er->serialised_xml_size);
er->bb_formdata = apr_brigade_create(r->pool, r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_immortal_create(EPP_CONTENT_FRAME_CGI,
strlen(EPP_CONTENT_FRAME_CGI), r->connection->bucket_alloc));
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_pool_create(er->serialised_xml,er->serialised_xml_size,
r->pool,r->connection->bucket_alloc));
if (conf->raw_frame) {
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_immortal_create(conf->raw_frame,
strlen(conf->raw_frame), r->connection->bucket_alloc));
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_pool_create(er->orig_xml,er->orig_xml_size,
r->pool,r->connection->bucket_alloc));
}
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_immortal_create(EPP_CONTENT_CLTRID_CGI,
strlen(EPP_CONTENT_CLTRID_CGI), r->connection->bucket_alloc));
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_pool_create(er->cltrid,strlen(er->cltrid),
r->pool,r->connection->bucket_alloc));
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_immortal_create(EPP_CONTENT_POSTFIX_CGI,
strlen(EPP_CONTENT_POSTFIX_CGI), r->connection->bucket_alloc));
APR_BRIGADE_INSERT_TAIL(er->bb_formdata, apr_bucket_eos_create(r->connection->bucket_alloc));
/* fixme, perhap use apr_brigade_length */
sprintf(content_length, "%lu", strlen(EPP_CONTENT_FRAME_CGI)
+ strlen(EPP_CONTENT_CLTRID_CGI)
+ strlen(er->cltrid)
+ strlen(EPP_CONTENT_POSTFIX_CGI)
+ er->serialised_xml_size
+ ((conf->raw_frame) ? (
strlen(conf->raw_frame)
+ er->orig_xml_size) : 0));
apr_table_set(r->headers_in, "Content-Type", "multipart/form-data; boundary=--BOUNDARY--");
apr_table_set(r->headers_in, "Content-Length", content_length);
apr_table_set(r->headers_in, "Cookie", er->ur->cookie);
ap_add_input_filter("XMLCGI_INPUT", (void *) er, r, r->connection);
r->assbackwards = 0; /* I don't want headers. */
r->method = "POST";
r->method_number = M_POST;
r->protocol = "INCLUDED";
ap_parse_uri(r, uri); /* also sets the unparsed_uri field */
r->the_request = uri; /* make sure the logging is correct */
/*
* Fake Basic Auth if authenticated or the backend does user/pass checking.
*/
if (er->ur->authenticated || conf->implicit_login )
{
apr_table_set(r->headers_in, "Authorization", er->ur->auth_string);
/*
* If the actual command or session URIs are not protected, no
* REMOTE_USER CGI environment variable will be produced. Thus we
* fake it here.
*/
r->user = er->ur->clid;
}
ap_update_child_status(r->connection->sbh, SERVER_BUSY_WRITE, r);
ap_process_request(r);
if (ap_extended_status)
ap_increment_counts(r->connection->sbh, r);
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, NULL,
"request status = %d", r->status);
/* debugging stub */
#if 0
epp_dump_table(r->headers_in,"headers_in, after call");
epp_dump_table(r->headers_out,"headers_out, after call");
epp_dump_table(r->err_headers_out,"err_headers_out, after call");
#endif
/*
* Check for the EPP Return Code header
*/
epp_rc = apr_table_get(r->err_headers_out, conf->rc_header);
if (!epp_rc)
epp_rc = apr_table_get(r->headers_out, conf->rc_header);
if (tag && !strcmp("logout",tag->name))
{
rv = epp_logout(er, tag);
}
/*
* was this a <login>?
*/
if (is_login && conf->implicit_login) /* did we try to login with this request */
{
/*
* The logic here is a bit tricky: if we get a http 401, login failed.
* If HTTP_OK, then check for a epp return-code header and analyze it.
* If no header is found, treat the login as succeeded.
*/
if (r->status == HTTP_UNAUTHORIZED)
{
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, NULL,
"epp login failed for %s, error code is %d", er->ur->clid, r->status);
er->ur->authenticated = 0;
}
else if (r->status == HTTP_OK)
{
if (epp_rc)
{
if (epp_rc[0] == '1') /* 1xxx is success */
{
ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, NULL,
"epp login for %s succeeded based on EPP code %s", er->ur->clid, epp_rc);
er->ur->authenticated = 1;
}
else
{
ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, NULL,
"epp login for %s failed based on EPP code %s", er->ur->clid, epp_rc);
er->ur->authenticated = 0;
}
}
else
{
ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, NULL,
"epp login succeeded for %s based on HTTP code %d", er->ur->clid, r->status);
er->ur->authenticated = 1;
}
}
}
/*
* Troubles executing the request
*/
if ((r->status != HTTP_OK) && (r->status != HTTP_UNAUTHORIZED))
{
ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, NULL,
"Could not execute %s", uri);
epp_error_handler(er, "internal", 2400, NULL, "Internal error.");
}
/*
* Did the backend request the connection to be closed?
*/
handle_close_request(er, r);
apr_pool_destroy(r->pool);
}
/*
* This implements the pseudo-hello request (connection open).
*
* We can't recycle epp_process_frame, as this has to be
* a GET request to allow SSL renegotiation.
*
* See the comments in ssl_engine_kernel.c concerning SSL and POST.
*
*/
apr_status_t epp_do_hello(epp_rec *er)
{
request_rec *r;
epp_conn_rec *conf = er->ur->conf;
char uri[200];
apr_snprintf(uri, sizeof(uri), "%s/hello?frame=%s", conf->session_root, EPP_BUILTIN_HELLO);
r = epp_create_request(er->ur);
er->r = r;
ap_parse_uri(r, uri);
r->assbackwards = 0; /* I don't want headers. */
r->method = "GET";
r->method_number = M_GET;
r->protocol = "INCLUDED";
r->the_request = uri; /* make sure the logging is correct */
apr_table_set(r->headers_in, "Cookie", er->ur->cookie);
ap_update_child_status(r->connection->sbh, SERVER_BUSY_WRITE, r);
ap_add_input_filter("EOS_INPUT", (void *) er, r, r->connection);
ap_process_request(r);
if (ap_extended_status)
ap_increment_counts(r->connection->sbh, r);
if (r->connection->aborted) /* probably a SSL error. */
{
return(r->status);
}
if (r->status != HTTP_OK) /* something wrong with the script runtime */
{
epp_error_handler(er, "internal", 2400, NULL, "Internal error producing greeting.");
}
apr_pool_destroy(r->pool);
return(APR_SUCCESS);
}
/*
* Try to implement an aequivalent to the read() system call.
*
* This read the specified number of bytes from the connection
* and stores them in the buffer provided.
*
* Partial reads are considered as errors. APR_SUCCESS is only
* returned on reading exactly count bytes.
*
*/
apr_status_t epp_read(conn_rec *c, apr_pool_t *p, char *buf, apr_size_t count)
{
apr_bucket_brigade *bb;
apr_status_t status;
apr_size_t need_bytes = count;
apr_size_t size;
bb = apr_brigade_create(p, c->bucket_alloc);
while(need_bytes > 0)
{
status = ap_get_brigade(c->input_filters, bb, AP_MODE_READBYTES,
APR_BLOCK_READ, need_bytes);
if (status != APR_SUCCESS)
{
apr_brigade_destroy(bb);
return status;
}
size = need_bytes;
status = apr_brigade_flatten(bb, buf, &size);
if (status != APR_SUCCESS)
{
apr_brigade_destroy(bb);
return status;
}
need_bytes -= size;
buf += size;
apr_brigade_cleanup(bb);
}
apr_brigade_destroy(bb);
return APR_SUCCESS;
}
/*
*
* This is the main conncetion handler.
*
* It first fires off the greeting, then loops over all incoming
* EPP requests.
*
*/
static int epp_process_connection(conn_rec *c)
{
server_rec *s = c->base_server;
epp_user_rec *ur;
epp_rec *er;
unsigned long framelen, framelen_n;
apr_pool_t *p, *p_er;
apr_bucket_brigade *bb_tmp;
apr_bucket_brigade *bb_out;
apr_status_t rv;
char *xml;
epp_conn_rec *conf = (epp_conn_rec *)ap_get_module_config(s->module_config,
&epp_module);
/*
* If EPP isn't turned on, then we decline here and thus fall back to HTTP.