Debian Codegraph @

fastcgi-server/sbin/llng-fastcgi-server

raw
1 #!/usr/bin/perl
2
3 use Plack::Runner;
4 use strict;
5 use warnings;
6 use POSIX;
7 use Getopt::Long;
8 use Lemonldap::NG::Handler::Main::Reload;
9
10 our $VERSION = '2.0.15';
11
12 our (
13 $foreground, $engine, $nproc, $pidFile,
14 $socket, $user, $listen, $group,
15 $procmanager, $customFunctionsFile, %plackOptions, $sourceServer,
16 );
17 my %_apps;
18
19 $SIG{'PIPE'} = 'IGNORE';
20 $ENV{LLNG_DEFAULTLOGGER} ||= 'Lemonldap::NG::Common::Logger::Syslog';
21
22 $foreground = 0;
23 $engine ||= $ENV{ENGINE} || 'FCGI';
24 $nproc ||= $ENV{NPROC} || 7;
25 $pidFile ||= $ENV{PID} || '__FASTCGISOCKDIR__/llng-fastcgi.pid';
26 $socket ||= $ENV{SOCKET} || '__FASTCGISOCKDIR__/llng-fastcgi.sock';
27 $listen ||= $ENV{LISTEN} || undef;
28 $user ||= $ENV{USER};
29 $group ||= $ENV{GROUP};
30 $sourceServer ||= $ENV{SOURCE_SERVER} || 'nginx';
31 $customFunctionsFile ||= $ENV{CUSTOM_FUNCTIONS_FILE};
32
33 # If the user specified any PM_ constrains, run under ::Constrained
34 $procmanager = "FCGI::ProcManager::Constrained" if grep /^PM_/, keys %ENV;
35
36 #Getopt::Long::Configure ("bundling_values");
37 GetOptions(
38 'foreground' => \$foreground,
39 'engine|e=s' => \$engine,
40 'proc|n=s' => \$nproc,
41 'pid|p=s' => \$pidFile,
42 'socket|s=s' => \$socket,
43 'listen|l=s' => \$listen,
44 'user|u=s' => \$user,
45 'group|g=s' => \$group,
46 'customFunctionsFile|f=s' => \$customFunctionsFile,
47 'plackOptions=s' => \%plackOptions,
48 'sourceServer=s' => \$sourceServer,
49 );
50
51 my $class = (
52 $sourceServer eq 'nginx' ? 'Lemonldap::NG::Handler::Server::Nginx'
53 : $sourceServer eq 'traefik' ? 'Lemonldap::NG::Handler::Server::Traefik'
54 : $sourceServer
55 );
56 eval "require $class";
57 if ($@) {
58 die $@ if $class != $sourceServer;
59 print STDERR "Unable to load custom class $class:\n$@";
60 exit 1;
61 }
62
63 if ($group) {
64 my $grp = getgrnam($group) or die "Can't change gid to $group";
65 POSIX::setgid($grp) or die "setgid: $!";
66 }
67
68 if ($user) {
69 my $uid = getpwnam($user) or die "Can't change uid to $user";
70 POSIX::setuid($uid) or die "setuid: $!";
71 }
72
73 unless ($>) {
74 die "Refuse to run as root. Aborting";
75 }
76
77 if ($customFunctionsFile) {
78 eval { require $customFunctionsFile };
79 die $@ if ($@);
80 }
81
82 my %builder = (
83 handler => sub {
84 return $class->run( {} );
85 },
86 reload => sub {
87 return $class->reload();
88 },
89 manager => sub {
90 require Lemonldap::NG::Manager;
91 return Lemonldap::NG::Manager->run( {} );
92 },
93 portal => sub {
94 require Lemonldap::NG::Portal;
95 return Lemonldap::NG::Portal->run( {} );
96 },
97 cgi => sub {
98 require CGI::Emulate::PSGI;
99 require CGI::Compile;
100 return sub {
101 my $script = $_[0]->{SCRIPT_FILENAME};
102 return $_apps{$script}->(@_) if ( $_apps{$script} );
103 $_apps{$script} =
104 CGI::Emulate::PSGI->handler( CGI::Compile->compile($script) );
105 return $_apps{$script}->(@_);
106 };
107 },
108 psgi => sub {
109 return sub {
110 my $script = $_[0]->{SCRIPT_FILENAME};
111 return $_apps{$script}->(@_) if ( $_apps{$script} );
112 $_apps{$script} = do $script;
113 unless ( $_apps{$script} and ref $_apps{$script} ) {
114 my $errmsg = $@ || $! || "unknown error";
115 die("Unable to load $_[0]->{SCRIPT_FILENAME}: $errmsg");
116 }
117 return $_apps{$script}->(@_);
118 }
119 },
120 );
121
122 my $app = sub {
123 $SIG{'PIPE'} = sub {
124 print STDERR "Got a PIPE signal";
125 };
126 my $type = $_[0]->{LLTYPE} || 'handler';
127 return $_apps{$type}->(@_) if ( defined $_apps{$type} );
128 if ( defined $builder{$type} ) {
129 $_apps{$type} = $builder{$type}->();
130 return $_apps{$type}->(@_);
131 }
132 die "Unknown PSGI type $type";
133 };
134
135 # Hook for customFunctions initialization
136 Lemonldap::NG::Handler::Main->onReload(
137 bless( {}, 'Lemonldap::NG::Handler::FastCGI::Loader' ),
138 'loadCustomHandlers' );
139
140 my $server = Plack::Runner->new();
141 $server->parse_options(
142 '-s' => $engine,
143 '-E' => 'deployment',
144 '--pid' => $pidFile,
145 '--nproc' => $nproc,
146 '--socket' => $socket,
147 ( $listen ? ( '--listen', $listen ) : () ),
148 '--proc-title' => 'llng-fastcgi-server',
149 ( $foreground ? () : '--daemonize' ),
150 '--no-default-middleware',
151 ( $procmanager ? ( '--manager', $procmanager ) : () ),
152 %plackOptions,
153 );
154
155 $server->run($app);
156
157 package Lemonldap::NG::Handler::FastCGI::Loader;
158
159 # Load configuration and look if custom handlers have been defined
160 sub loadCustomHandlers {
161 my ( $obj, $conf ) = @_;
162 foreach my $lltype ( keys %{ $conf->{nginxCustomHandlers} || {} } ) {
163 my $v = $conf->{nginxCustomHandlers}->{$lltype};
164 if ( $v =~ m#[/\\\.]# ) {
165 eval { require $v; };
166 }
167 else {
168 eval "use $v";
169 }
170 if ($@) {
171 print STDERR "Unable to load $v, skipping: $@\n";
172 next;
173 }
174 $builder{$lltype} = sub {
175 require $v;
176 return $v->run( {} );
177 };
178 }
179 return 1;
180 }
181 __END__
182
183 =head1 NAME
184
185 =encoding utf8
186
187 llng-fastcgi-server - FastCGI server used for providing LemonLDAP::NG services.
188
189 =head1 SYNOPSIS
190
191 # Start server listening to /run/llng.sock with 10 workers
192 llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10
193
194 =head1 DESCRIPTION
195
196 llng-fastcgi-server has been designed to provide LemonLDAP::NG services to Nginx
197 or DevOps Handler.
198 Portal, Manager and Handler will be compiled just-in-time. So this FastCGI
199 server can be used on every LemonLDAP::NG server even if it needs only some
200 parts (isolated handlers, portal,...).
201
202 =head1 PARAMETERS
203
204 Each parameter can be set by using options or environment variables.
205
206 =over
207
208 =item --pid -p ($ENV{PID}):
209
210 pid file
211
212 =item --user -u ($ENV{USER}):
213
214 user
215
216 =item --group -g ($ENV{GROUP}):
217
218 group
219
220 =item --proc -n ($ENV{NPROC}):
221
222 Number of processus for FCGI
223
224 =item --socket -s ($ENV{SOCKET}):
225
226 Unix socket
227
228 =item --listen -l ($ENV{LISTEN}):
229
230 Listening address (HOST:PORT, :PORT, or PATH)
231
232 =item --customFunctionsFile -f ($ENV{CUSTOM_FUNCTIONS_FILE}):
233
234 file to load for custom functions
235
236 =item --engine -e ($ENV{ENGINE}):
237
238 Plack::Handler engine, default to FCGI (see below)
239
240 =item --plackOptions:
241
242 To pass other options to the Plack handler. This multi-valued parameter must
243 have "key=value" values.
244
245 See Plack::Handler::FCGI to find out list of available options for default FCGI engine
246
247 =back
248
249 =head1 ENGINES
250
251 By default, llng-fastcgi-server uses FCGI (= L<Plack::Handler::FCGI>).
252 Some other engines can be used:
253
254 =head2 FCGI (default)
255
256 It uses L<FCGI::ProcManager> as manager. Other managers:
257
258 =over
259
260 =item L<FCGI::ProcManager::Dynamic>
261
262 llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -e FCGI -n 10 \
263 --plackOptions manager=FCGI::ProcManager::Dynamic
264
265 =back
266
267 =head2 Other FCGI::ProcManager style engines
268
269 =over
270
271 =item FCGI::Engine
272
273 =back
274
275 =head2 Event engines
276
277 =over
278
279 =item AnyEvent::FCGI
280
281 =item FCGI::Async
282
283 =item FCGI::EV
284
285 =back
286
287 =head1 SEE ALSO
288
289 L<http://lemonldap-ng.org/>
290
291 =head1 AUTHORS
292
293 =over
294
295 =item Clement Oudot, E<lt>[email protected]<gt>
296
297 =item Xavier Guimard, E<lt>[email protected]<gt>
298
299 =back
300
301 =head1 BUG REPORT
302
303 Use OW2 system to report bug or ask for features:
304 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
305
306 =head1 DOWNLOAD
307
308 Lemonldap::NG is available at
309 L<https://lemonldap-ng.org/download>
310
311 =head1 COPYRIGHT AND LICENSE
312
313 =over
314
315 =item Copyright (C) 2008-2016 by Xavier Guimard, E<lt>[email protected]<gt>
316
317 =item Copyright (C) 2008-2016 by Clément Oudot, E<lt>[email protected]<gt>
318
319 =back
320
321 This library is free software; you can redistribute it and/or modify
322 it under the terms of the GNU General Public License as published by
323 the Free Software Foundation; either version 2, or (at your option)
324 any later version.
325
326 This program is distributed in the hope that it will be useful,
327 but WITHOUT ANY WARRANTY; without even the implied warranty of
328 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
329 GNU General Public License for more details.
330
331 You should have received a copy of the GNU General Public License
332 along with this program. If not, see L<http://www.gnu.org/licenses/>.
333
334 =cut