llng-fastcgi-server 6.56 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6
#!/usr/bin/env perl

use Plack::Runner;
use strict;
use warnings;
use POSIX;
Yadd's avatar
Yadd committed
7
use Getopt::Long;
Yadd's avatar
Yadd committed
8
use Lemonldap::NG::Handler::Main::Reload;
Yadd's avatar
Yadd committed
9

Yadd's avatar
Yadd committed
10
our $VERSION = '2.0.0';
Yadd's avatar
Yadd committed
11

Yadd's avatar
Yadd committed
12 13 14 15 16 17
our (
    $foreground, $engine,              $nproc,
    $pidFile,    $socket,              $user,
    $group,      $customFunctionsFile, %plackOptions
);
my %_apps;
Yadd's avatar
Yadd committed
18

Yadd's avatar
Yadd committed
19 20
$SIG{'PIPE'} = 'IGNORE';

Yadd's avatar
Yadd committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
$foreground = 0;
$engine  ||= $ENV{ENGINE} || 'FCGI';
$nproc   ||= $ENV{NPROC}  || 7;
$pidFile ||= $ENV{PID}    || '__FASTCGISOCKDIR__/llng-fastcgi.pid';
$socket  ||= $ENV{SOCKET} || '__FASTCGISOCKDIR__/llng-fastcgi.sock';
$user    ||= $ENV{USER};
$group   ||= $ENV{GROUP};
$customFunctionsFile ||= $ENV{CUSTOM_FUNCTIONS_FILE};

#Getopt::Long::Configure ("bundling_values");
GetOptions(
    'foreground'              => \$foreground,
    'engine|e=s'              => \$engine,
    'proc|n=s'                => \$nproc,
    'pid|p=s'                 => \$pidFile,
    'socket|s=s'              => \$socket,
    'user|u=s'                => \$user,
    'group|g=s'               => \$group,
    'customFunctionsFile|f=s' => \$customFunctionsFile,
    'plackOptions=s'          => \%plackOptions,
);
Yadd's avatar
Yadd committed
42

Yadd's avatar
Yadd committed
43 44
if ($group) {
    my $grp = getgrnam($group) or warn "Can't change uid to $group";
Yadd's avatar
Yadd committed
45
    POSIX::setgid($grp);
Yadd's avatar
Yadd committed
46 47
}

Yadd's avatar
Yadd committed
48 49
if ($user) {
    my $uid = getpwnam($user) or warn "Can't change uid to $user";
Yadd's avatar
Yadd committed
50 51 52
    POSIX::setuid($uid);
}

Yadd's avatar
Yadd committed
53 54
if ($customFunctionsFile) {
    eval { require $customFunctionsFile };
Yadd's avatar
Yadd committed
55
    die $@ if ($@);
56 57
}

Yadd's avatar
Yadd committed
58 59 60 61 62
my %builder = (
    handler => sub {
        require Lemonldap::NG::Handler::Nginx;
        return Lemonldap::NG::Handler::Nginx->run( {} );
    },
Yadd's avatar
Yadd committed
63 64 65 66
    authbasic => sub {
        require Lemonldap::NG::Handler::Nginx::AuthBasic;
        return Lemonldap::NG::Handler::Nginx::AuthBasic->run( {} );
    },
Yadd's avatar
Yadd committed
67 68 69 70
    reload => sub {
        require Lemonldap::NG::Handler::Nginx;
        return Lemonldap::NG::Handler::Nginx->reload();
    },
Yadd's avatar
Yadd committed
71 72 73 74
    status => sub {
        require Lemonldap::NG::Handler::Nginx;
        return Lemonldap::NG::Handler::Nginx->status();
    },
Yadd's avatar
Yadd committed
75 76 77 78
    manager => sub {
        require Lemonldap::NG::Manager;
        return Lemonldap::NG::Manager->run( {} );
    },
79
    cgi => sub {
Yadd's avatar
Yadd committed
80 81
        require CGI::Emulate::PSGI;
        require CGI::Compile;
82
        return sub {
83
            my $script = $_[0]->{SCRIPT_FILENAME};
84 85 86
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} =
              CGI::Emulate::PSGI->handler( CGI::Compile->compile($script) );
87
            return $_apps{$script}->(@_);
88
        };
Yadd's avatar
Yadd committed
89
    },
Yadd's avatar
Yadd committed
90 91 92 93 94 95 96 97
    psgi => sub {
        return sub {
            my $script = $_[0]->{SCRIPT_FILENAME};
            return $_apps{$script}->(@_) if ( $_apps{$script} );
            $_apps{$script} = do $script;
            return $_apps{$script}->(@_);
          }
    },
Yadd's avatar
Yadd committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
);

unless ($>) {
    die "Refuse to run as root. Aborting";
}

my $app = sub {
    my $type = $_[0]->{LLTYPE} || 'handler';
    return $_apps{$type}->(@_) if ( defined $_apps{$type} );
    if ( defined $builder{$type} ) {
        $_apps{$type} = $builder{$type}->();
        return $_apps{$type}->(@_);
    }
    die "Unknown PSGI type $type";
};

114
# Hook for customFunctions initialization
Yadd's avatar
Yadd committed
115
Lemonldap::NG::Handler::Main->onReload(
Yadd's avatar
Yadd committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    bless( {}, 'Lemonldap::NG::Handler::FastCGI::Loader' ),
    'loadCustomLogins' );

my $server = Plack::Runner->new();
$server->parse_options(
    '-s'           => $engine,
    '-E'           => 'deployment',
    '--pid'        => $pidFile,
    '--nproc'      => $nproc,
    '--socket'     => $socket,
    '--proc-title' => 'llng-fastcgi-server',
    ( $foreground ? () : '--daemonize' ),
    '--no-default-middleware',
    %plackOptions,
);

$server->run($app);

package Lemonldap::NG::Handler::FastCGI::Loader;
Yadd's avatar
Yadd committed
135 136

# Load configuration and look if custom handlers have been defined
Yadd's avatar
Yadd committed
137 138
sub loadCustomLogins {
    my ( $obj, $conf ) = @_;
Yadd's avatar
Yadd committed
139 140
    foreach my $lltype ( keys %{ $conf->{nginxCustomHandlers} // {} } ) {
        my $v = $conf->{nginxCustomHandlers}->{$lltype};
Yadd's avatar
Yadd committed
141
        if ( $v =~ m#[/\\\.]# ) {
Yadd's avatar
Yadd committed
142
            eval { require $v; };
Yadd's avatar
Yadd committed
143 144 145 146 147 148 149
        }
        else {
            eval "use $v";
        }
        if ($@) {
            print STDERR "Unable to load $v, skipping: $@\n";
            next;
Yadd's avatar
Yadd committed
150 151 152 153 154 155 156
        }
        $builder{$lltype} = sub {
            require $v;
            return $v->run( {} );
        };
    }
}
Yadd's avatar
Yadd committed
157
__END__
Yadd's avatar
Yadd committed
158

Yadd's avatar
Yadd committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
=head1 NAME

=encoding utf8

llng-fastcgi-server - FastCGI server used to provide Lemonldap::NG services to
Nginx

=head1 SYNOPSIS

  # Start server listening to /run/llng.sock with 10 process
  llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10

=head1 DESCRIPTION

llng-fastcgi-server has been designed provides Lemonldap::NG services to Nginx.
Portal, manager and handler will be compiled only is used. So this FastCGI
server can be used on every Lemonldap::NG server even if it needs only some
parts (isolated handlers, portal,...).

Yadd's avatar
Yadd committed
178 179 180 181 182 183
=head1 PARAMETERS

Each parameter can be set by an option or a environment variable.

=over

Yadd's avatar
Yadd committed
184 185 186
=item --pid -p ($ENV{PID}):       pid file

=item --user -u ($ENV{USER}):     user
Yadd's avatar
Yadd committed
187

Yadd's avatar
Yadd committed
188
=item --group -g ($ENV{GROUP}):   group
Yadd's avatar
Yadd committed
189

Yadd's avatar
Yadd committed
190
=item --proc -n ($ENV{NPROC}):    Number of processus for FCGI
Yadd's avatar
Yadd committed
191

Yadd's avatar
Yadd committed
192
=item --engine -e ($ENV{ENGINE}): Plack::Handler engine, default to FCGI
Yadd's avatar
Yadd committed
193

Yadd's avatar
Yadd committed
194
=item --socket -s ($ENV{SOCKET}): Unix socket
Yadd's avatar
Yadd committed
195

Yadd's avatar
Yadd committed
196 197
=item --customFunctionsFile -f ($ENV{CUSTOM_FUNCTIONS_FILE}): file to load for
custom functions
Yadd's avatar
Yadd committed
198

Yadd's avatar
Yadd committed
199 200
=item --plackOptions: other options to pass to Plack. This multi-valued
parameter must have "key=value" values.
Yadd's avatar
Yadd committed
201 202 203

=back

Yadd's avatar
Yadd committed
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
=head1 SEE ALSO

L<http://lemonldap-ng.org/>

=head1 AUTHORS

=over

=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
L<http://jira.ow2.org>

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

=over

=item Copyright (C) 2008-2016 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=item Copyright (C) 2008-2016 by Clément Oudot, E<lt>clem.oudot@gmail.comE<gt>

=back

This library 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 2, 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
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut