-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulma
executable file
·767 lines (559 loc) · 17.3 KB
/
pulma
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
#!/usr/bin/perl
=head1 Pulma
Part of Pulma system
Main FastCGI server script: handler of all incoming FastCGI requests
Copyright (C) 2011 Fedor A. Fetisov <faf@ossg.ru>. All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
=cut
use strict;
use warnings;
use vars qw( $VERSION );
$VERSION = "0.3";
# set this flag to prevent script to go into background
my $_devel = 0;
BEGIN {
use FindBin;
use lib $FindBin::Bin;
chdir($FindBin::Bin);
# get config
# (config file could be specified as command-line parameter.
# default value: './pulma.conf')
my $config_file = $ARGV[0] || './pulma.conf';
our $config = do($config_file);
die "Bad configuration format: $@\n" if ($@);
die "Can't read config: $!" unless defined $config;
# FastCGI server parameters
$ENV{FCGI_SOCKET_PATH} = $config->{'fcgi'}->{'socket_path'};
$ENV{FCGI_LISTEN_QUEUE} = $config->{'fcgi'}->{'listen_queue'};
}
# 'monkey patch' for SIGHUP handling
# see http://search.cpan.org/~gbjk/FCGI-ProcManager-0.19/ProcManager.pm for
# details
use FCGI;
no strict 'refs';
no warnings;
*{'FCGI::Request'} = sub (;***$*$) {
my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, FAIL_ACCEPT_ON_INTR());
$_[4] = fileno($_[4]) if defined($_[4]) && defined(fileno($_[4]));
splice @defaults,0,@_,@_;
RequestX(@defaults);
};
use strict 'refs';
use warnings;
# end of 'monkey patch'
use CGI::Cookie;
use CGI::Fast qw(remote_addr header cookie redirect cgi_error escapeHTML);
use Clone qw( clone );
use Encode qw(_utf8_off);
use FCGI::ProcManager qw(pm_manage pm_pre_dispatch pm_post_dispatch);
use POSIX;
# requests mapping
use Pulma::Core::Mapper;
# requests parser
use Pulma::Core::Requests;
# output generator
use Pulma::Output;
# standard constants
use Pulma::Service::Constants;
# parser for internal data exchange
use Pulma::Service::Data::Parser;
# standard functions
use Pulma::Service::Functions;
# system logging utility
use Pulma::Service::Log;
# pulma configuration hash
our $config;
# configure Syslog options for logging
$Pulma::Service::Log::Level = $config->{'syslog'}->{'log_level'};
$Pulma::Service::Log::Ident = $config->{'syslog'}->{'syslog_ident'};
$Pulma::Service::Log::Logopt = $config->{'syslog'}->{'syslog_logopt'};
$Pulma::Service::Log::Facility = $config->{'syslog'}->{'syslog_facility'};
# configure external logging (if need to)
if ( exists($config->{'external_log'}) ) {
if (ref($config->{'external_log'}) eq 'CODE') {
# external logging as code (anonymous function)
*ext_log = $config->{'external_log'};
# test given code
eval "ext_log('debug', 'Check external logging');";
if ($@) {
log_it( 'err',
"Failed to use given anonymous function for external logging: %s",
$@ );
}
else {
# test passed, set function for external logging
$Pulma::Service::Log::ExtLog = \&ext_log;
}
}
elsif (ref($config->{'external_log'}) eq '') {
# external logging as function in external module
my ($module, $func, @args) = split('/', $config->{'external_log'});
# test external module
eval "require $module";
if ($@) {
log_it( 'err',
"Failed to require module %s for external logging: %s",
$module, $@ );
}
else {
# test function in external module (see Pulma::Service::Log for details)
my $test_args = '';
if (scalar(@args) > 0) {
$test_args = "''," . "''," x (scalar(@args) - 1);
}
*ext_log = $module . '::' . $func;
eval "ext_log(" . $test_args . "'debug', 'Check external logging');";
if ($@) {
log_it( 'err',
"Failed to use given function '%s' in external module '%s' for external logging: %s",
$module, $func, $@ );
}
else {
# test passed, set function for external logging
$Pulma::Service::Log::ExtLog = \&ext_log;
@Pulma::Service::Log::ExtLogArgs = @args;
}
}
}
else {
log_it( 'err',
"Invalid external logging settings. Expected anonymous function or function name, got: %s",
ref($config->{'external_log'}) );
}
}
# chroot if need to
if (defined $config->{'fcgi'}->{'chroot'}) {
unless (chroot($config->{'fcgi'}->{'chroot'})) {
log_it( 'err',
"Can't chroot to %s: %s",
$config->{'fcgi'}->{'chroot'}, $! );
}
else {
log_it( 'debug',
"Chroot to %s successful",
$config->{'fcgi'}->{'chroot'} );
}
}
# set GID and UID
my $user = $>;
my $group = $);
# check UID to run as
if (!defined $config->{'fcgi'}->{'user'}) {
log_it('info', "User not defined, use default: %s", $user);
}
else {
if ($config->{'fcgi'}->{'user'} =~ /^([\w-]+)$/) {
$user = check_number($config->{'fcgi'}->{'user'}) ?
$config->{'fcgi'}->{'user'} :
getpwnam($config->{'fcgi'}->{'user'});
unless (defined $user) {
log_it( 'err',
"Can't determine UID of user %s: no such user",
$config->{'fcgi'}->{'user'} );
exit(1);
}
}
else {
log_it( 'err',
"Invalid user %s",
$config->{'fcgi'}->{'user'} );
exit(1);
}
log_it( 'debug', "Determined UID to start as: %s", $user );
}
# check GID to run as
if (!defined $config->{'fcgi'}->{'group'}) {
log_it( 'info',
"Group not defined, use default: %s",
$group );
}
else {
if ($config->{'fcgi'}->{'group'} =~ /^([\w-]+)$/) {
$group = check_number($config->{'fcgi'}->{'group'}) ?
$config->{'fcgi'}->{'group'} :
getgrnam($config->{'fcgi'}->{'group'});
unless (defined $group) {
log_it( 'err',
"Can't determine GID of group %s: no such group",
$config->{'fcgi'}->{'group'} );
exit(1);
}
}
else {
log_it( 'err',
"Invalid group %s",
$config->{'fcgi'}->{'group'} );
exit(1);
}
log_it('debug', "Determined GID to start as: %s", $group);
}
# try to set GID
POSIX::setgid($group);
unless ( ($group == $() && ($group == $)) ) {
log_it('err', "Can't set GID %s: %s", $group, $!);
exit(1);
}
else {
log_it('info', "Changed GID to %s", $group);
}
# try to set SID
POSIX::setuid($user);
unless ( ($user == $>) && ($user == $<) ) {
log_it('err', sprintf("Can't set UID %s: %s", $user, $!));
exit(1);
}
else {
log_it('info', "Changed UID to %s", $user);
}
# check old PID file
if (-e $config->{'fcgi'}->{'pid_file'}) {
# PID file exists - try to check PID written there
unless (open(IN, '<' . $config->{'fcgi'}->{'pid_file'})) {
log_it( 'err',
"Old PID file %s exists and isn't readable: %s",
$config->{'fcgi'}->{'pid_file'},
$! );
exit(1);
}
my $old_pid = <IN>;
unless (close IN) {
log_it( 'err',
"Can't close PID file %s opened for read: %s",
$config->{'fcgi'}->{'pid_file'}, $! );
exit(1);
}
# PID in PID file is valid and there is an appropriate process - leave
if ( check_number($old_pid) && kill(0, $old_pid) ) {
log_it( 'err',
"FastCGI server already running with PID %s",
$old_pid );
exit(1);
}
else {
# PID in PID file isn't valid - try to remove old PID file
unless ( unlink $config->{'fcgi'}->{'pid_file'} ) {
log_it( 'err',
"Can't delete PID file %s of the dead FastCGI server process: %s",
$config->{'fcgi'}->{'pid_file'}, $! );
exit(1);
}
}
}
my $pid;
unless ($_devel) {
# go to background
FORK: {
$pid = fork;
if ($pid) {
log_it('info', "Gone to background with PID %s", $pid);
# open PID file to write there PID
unless (open(OUT, '>' . $config->{'fcgi'}->{'pid_file'})) {
log_it( 'err',
"Can't open PID file %s: %s",
$config->{'fcgi'}->{'pid_file'}, $! );
exit(1);
}
print OUT $pid;
close OUT || log_it( 'warning',
"Can't close PID file %s opened for write: %s",
$config->{'fcgi'}->{'pid_file'}, $! );
exit;
}
if (!defined $pid) {
if ($! =~ /No more process/) {
sleep 5;
redo FORK;
}
else {
log_it('err', "Can't fork: %s", $!);
exit(1);
}
}
}
# start new session without terminal
unless (POSIX::setsid()) {
log_it('err', "Can't set sid: %s", $!);
exit(1);
}
# ignore system signals
$SIG{INT} = $SIG{PIPE} = 'IGNORE';
# redirect all standard descriptors to /dev/null
for my $handle (*STDIN, *STDOUT, *STDERR) {
unless (open($handle, "+>", "/dev/null")) {
log_it( 'err',
"Can't reopen descriptor %s to /dev/null: %s",
$handle, $! );
}
else {
log_it('debug', "Descriptor %s reopened to /dev/null", $handle);
}
}
}
# start FastCGI processes
log_it('debug', 'Starting process manager');
pm_manage(%{$config->{'fcgi'}->{'proc_manager'}});
# initialize mapper object
log_it('debug', 'Initializing mapper object');
my $mapper = Pulma::Core::Mapper->new($config->{'map'});
unless (defined $mapper) {
log_it('err', "Can't initialize mapper object!");
exit(1);
}
# initialize output generator
log_it('debug', 'Initializing output generator');
my $output = Pulma::Output->new( {
'templates' => $config->{'templates'}->{'path'},
'cache' => $config->{'templates'}->{'cache'}
} );
unless (defined $output) {
log_it('err', "Can't initialize output generator!");
exit(1);
}
# initialize requests parser
log_it('debug', 'Initializing requests parser');
my $requests_parser = Pulma::Core::Requests->new( { 'root' => $config->{'map'}->{'root'} } );
unless (defined $requests_parser) {
log_it('err', "Can't initialize output generator!");
exit(1);
}
log_it('info', "CBC FastCGI requests handler with PID %s started.", $$);
# hash with names of the already required modules
my $modules = {};
# hash with already initialized data handlers
my $objects = {};
# common built-in cache
my $cache = {};
# add 'nodeid' value to configuration of all data sources (for future generation
# of entity ids)
foreach ('auth', 'data', 'logger') {
$config->{$_}->{'nodeid'} = $config->{'fcgi'}->{'nodeid'};
}
# initial resulting hash structure
my $initial_result_hash = {
'status' => ERROR,
'mime_type' => 'text/html; charset=UTF-8',
'cookies' => [],
'template' => '500.html',
'document' => '',
'use_output' => 1,
'binary_data' => 0,
'headers' => {}
};
# take initial steps before actually start to handle requests
log_it('debug', "Take initial steps");
my $init_steps = $mapper->init_steps();
if (scalar(@$init_steps)) {
_take_steps( $init_steps, { 'request' => { },
'pulma' => { 'data' => { } },
'result' => clone($initial_result_hash),
'stop' => 0 } );
}
# initialize requests counter
my $cnt = 0;
log_it('info', "CBC FastCGI requests handler with PID %s initialized.", $$);
# main loop
my $request;
while($request = CGI::Fast->new()) {
# pre-request operations
pm_pre_dispatch();
# pseudo-unique request id: for logging
my $requestid = $$ . '-' . $config->{'fcgi'}->{'nodeid'} . '-' . (++$cnt);
# parse request
my $incoming = $requests_parser->request($requestid);
# get steps for request
my $steps = $mapper->steps(\$incoming);
# initialize data structure
my $data = {
'request' => $incoming,
'pulma' => {
'data' => {}
},
'result' => clone($initial_result_hash),
'stop' => 0
};
# take steps
$data = _take_steps($steps, $data);
# output data
if ($data->{'result'}->{'use_output'}) {
# use standard output object if not explicitly told otherwise
$data = $output->generate($data);
}
# prepare main header if need to
$data->{'result'}->{'headers'}->{'-status'} ||= $data->{'result'}->{'status'};
$data->{'result'}->{'headers'}->{'-type'} ||= $data->{'result'}->{'mime_type'};
# prepare cookies to store
foreach my $cookie (@{$data->{'result'}->{'cookies'}}) {
my $structure = { -name => $cookie->{'name'} || 'pulma',
-secure => $cookie->{'secure'} || 0,
-httponly => $cookie->{'httponly'} || 0,
-value => exists ($cookie->{'value'}) ?
$cookie->{'value'} :
'pulma',
-path => $cookie->{'path'} ||
$config->{'map'}->{'root'} ||
'/'
};
if (!exists($cookie->{'expires'})) {
if (exists($cookie->{'delete'})) {
$structure->{-expires} = '-' . 120 . 's';
}
}
else {
$structure->{-expires} = ($cookie->{'delete'} ? '-' : '+') .
$cookie->{'expires'} . 's';
}
push( @{$data->{'result'}->{'headers'}->{'-cookie'}},
cookie( $structure ) );
}
# send output to client
if ($data->{'result'}->{'headers'}->{'-status'} == REDIRECT) {
print redirect($data->{'result'}->{'headers'});
}
else {
print header($data->{'result'}->{'headers'});
my $document = $data->{'result'}->{'document'} || '';
if ($data->{'result'}->{'binary_data'}) {
binmode(STDOUT);
}
else {
binmode(STDOUT, ":utf8");
_utf8_off($document);
}
print $document;
}
# write brief info about request into log
my $time = localtime(time);
log_it( 'notice',
"%s: \"%s\" \"%s\" \"%s %s %s\" %s %s \"%s\"",
$requestid,
$incoming->{'remoteip'},
$time,
$incoming->{'method'},
$incoming->{'fullurl'},
$ENV{'SERVER_PROTOCOL'},
$data->{'result'}->{'status'},
length($data->{'result'}->{'document'}) || 0,
$incoming->{'useragent'} );
# post-request operations
pm_post_dispatch();
}
############################## Private functions ##################################
# Function: _take_steps
# Description
# Function to handle request
# Argument(s)
# 1. (link to array) steps to take (detailed list of data handlers)
# 2. (link to hash) data structure
# Returns
# (link to hash) data structure after request handling
sub _take_steps {
my $steps = shift;
my $data = shift;
# pass data structure through all handlers (step by step)
foreach my $step (@$steps) {
# look for step details
unless (exists($step->{'details'})) {
log_it('err', 'Step without details provided!');
next;
}
# check step type and details
unless ($step->{'type'} eq 'module') {
log_it( 'warning',
"Step is of type %s which is currently unsupported",
$step->{'type'} );
next;
}
elsif (!exists($step->{'details'}->{'module'})) {
log_it('err', 'Step module not specified!');
next;
}
else {
# step type is module:
# try to require step's module for the first time (if need to)
unless (exists($modules->{$step->{'details'}->{'module'}})) {
eval "require $step->{'details'}->{'module'};";
if ($@) {
log_it( 'err',
"Failed to require module %s : %s",
$step->{'details'}->{'module'}, $@ );
# remove bad module from INC hash (to try to load it once again without need
# of full framework restart)
my $temp = $step->{'details'}->{'module'};
$temp =~ s~::~/~g;
delete($INC{$temp . '.pm'});
next;
}
else {
$modules->{$step->{'details'}->{'module'}} = 1;
}
}
# try to initialize data handler for the first time (if need to)
# (data handler object can be told to use auth, logger and data sources)
# for prototype data handler see Pulma::Actions::Prototype
unless ( exists($objects->{$step->{'details'}->{'module'} .
($step->{'auth'} ? 1 : 0) .
($step->{'data'} ? 1 : 0) .
($step->{'logger'} ? 1 : 0)}) ) {
my $conf = {};
foreach ('auth', 'data', 'logger') {
$conf->{$_} = $config->{$_} if ($step->{$_});
}
my $obj = $step->{'details'}->{'module'}->new($conf, \$cache, \$output);
if (defined $obj) {
$objects->{$step->{'details'}->{'module'} .
($step->{'auth'} ? 1 : 0) .
($step->{'data'} ? 1 : 0) .
($step->{'logger'} ? 1 : 0)} = $obj;
}
else {
log_it( 'err',
"Failed to initialize object of class %s : undefined",
$step->{'details'}->{'module'} );
next;
}
}
# check step details, should be hash
unless ( defined($step->{'details'}->{'data'}) &&
(ref($step->{'details'}->{'data'}) eq 'HASH') ) {
$step->{'details'}->{'data'} = {};
}
# handle data
$data = $objects->{$step->{'details'}->{'module'} .
($step->{'auth'} ? 1 : 0) .
($step->{'data'} ? 1 : 0) .
($step->{'logger'} ? 1 : 0)}->action( $data,
$step->{'details'}->{'data'} );
# stop if module at this step explicitly say that
last if $data->{'stop'};
}
}
unless (scalar(@$steps)) {
# valid steps for request not found...
log_it('err', "No steps to take for incoming request!");
}
return $data;
}
################################## END Block ######################################
END {
# in case of sudden death due to malformed request print error before exit
my $error = $request ? cgi_error() : undef;
if ($error) {
print header(-status => $error);
print "Invalid request: " . escapeHTML($error);
}
# main FastCGI script should delete PID file when dying
if ((defined $pid) && ($pid == $$)) {
unlink($config->{'fcgi'}->{'pid_file'}) ||
log_it( 'err',
"Can't delete PID file %s: %s",
$config->{'fcgi'}->{'pid_file'}, $! );
}
}