11c8f6694c8b516b56e48a817cd3e9be8bb0e381
[kivitendo-erp.git] / scripts / task_server.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   require Cwd;
7
8   my $dir =  $0;
9   $dir    =  Cwd::getcwd() . '/' . $dir unless $dir =~ m|^/|;
10   $dir    =~ s|[^/]+$|..|;
11
12   chdir($dir) || die "Cannot change directory to ${dir}\n";
13
14   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
15   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
16 }
17
18 use CGI qw( -no_xhtml);
19 use Config::Std;
20 use Cwd;
21 use Daemon::Generic;
22 use Data::Dumper;
23 use DateTime;
24 use English qw(-no_match_vars);
25 use POSIX qw(setuid setgid);
26 use SL::Auth;
27 use SL::DB::BackgroundJob;
28 use SL::BackgroundJob::ALL;
29 use SL::Form;
30 use SL::Helper::DateTime;
31 use SL::LXDebug;
32 use SL::Locale;
33
34 our %lx_office_conf;
35
36 # this is a cleaned up version of am.pl
37 # it lacks redirection, some html setup and most of the authentication process.
38 # it is assumed that anyone with physical access and execution rights on this script
39 # won't be hindered by authentication anyway.
40 sub lxinit {
41   my $login = $lx_office_conf{task_server}->{login};
42
43   package main;
44
45   eval { require "config/lx-erp.conf";       1; } or die $EVAL_ERROR;
46   eval { require "config/lx-erp-local.conf"; 1; } or die $EVAL_ERROR if -f "config/lx-erp-local.conf";
47
48   $::lxdebug = LXDebug->new;
49   $::locale  = Locale->new($::lx_office_conf{system}->{language});
50   $::cgi     = CGI->new qw();
51   $::form    = Form->new;
52   $::auth    = SL::Auth->new;
53
54   die 'cannot reach auth db'               unless $::auth->session_tables_present;
55
56   $::auth->restore_session;
57
58   require "bin/mozilla/common.pl";
59
60   die "cannot find user $login"            unless %::myconfig = $::auth->read_user($login);
61   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
62 }
63
64 sub drop_privileges {
65   my $user = $lx_office_conf{task_server}->{run_as};
66   return unless $user;
67
68   my ($uid, $gid);
69   while (my @details = getpwent()) {
70     next unless $details[0] eq $user;
71     ($uid, $gid) = @details[2, 3];
72     last;
73   }
74   endpwent();
75
76   if (!$uid) {
77     print "Error: Cannot drop privileges to ${user}: user does not exist\n";
78     exit 1;
79   }
80
81   if (!setgid($gid)) {
82     print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
83     exit 1;
84   }
85
86   if (!setuid($uid)) {
87     print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
88     exit 1;
89   }
90 }
91
92 sub gd_preconfig {
93   my $self = shift;
94
95   read_config $self->{configfile} => %lx_office_conf;
96
97   die "Missing section [task_server] in config file"                unless $lx_office_conf{task_server};
98   die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
99
100   drop_privileges();
101   lxinit();
102
103   return ();
104 }
105
106 sub gd_run {
107   while (1) {
108     my $ok = eval {
109       $::lxdebug->message(0, "Retrieving jobs") if $lx_office_conf{task_server}->{debug};
110
111       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
112
113       $::lxdebug->message(0, "  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if $lx_office_conf{task_server}->{debug} && @{ $jobs };
114
115       foreach my $job (@{ $jobs }) {
116         # Provide fresh global variables in case legacy code modifies
117         # them somehow.
118         $::locale = Locale->new($::lx_office_conf{system}->{language});
119         $::form   = Form->new;
120
121         $job->run;
122       }
123
124       1;
125     };
126
127     if ($lx_office_conf{task_server}->{debug}) {
128       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
129       $::lxdebug->message(0, "Sleeping");
130     }
131
132     my $seconds = 60 - (localtime)[0];
133     sleep($seconds < 30 ? $seconds + 60 : $seconds);
134   }
135 }
136
137 my $cwd     = getcwd();
138 my $pidbase = "${cwd}/users/pid";
139
140 mkdir($pidbase) if !-d $pidbase;
141
142 newdaemon(configfile => "${cwd}/config/lx_office.conf",
143           progname   => 'lx-office-task-server',
144           pidbase    => "${pidbase}/",
145           );
146
147 1;