Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[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 Cwd;
20 use Daemon::Generic;
21 use Data::Dumper;
22 use DateTime;
23 use English qw(-no_match_vars);
24 use POSIX qw(setuid setgid);
25 use SL::Auth;
26 use SL::DB::BackgroundJob;
27 use SL::BackgroundJob::ALL;
28 use SL::Form;
29 use SL::Helper::DateTime;
30 use SL::LXDebug;
31 use SL::LxOfficeConf;
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   $::lxdebug = LXDebug->new;
46   $::locale  = Locale->new($::lx_office_conf{system}->{language});
47   $::cgi     = CGI->new qw();
48   $::form    = Form->new;
49   $::auth    = SL::Auth->new;
50
51   die 'cannot reach auth db'               unless $::auth->session_tables_present;
52
53   $::auth->restore_session;
54
55   require "bin/mozilla/common.pl";
56
57   die "cannot find user $login"            unless %::myconfig = $::auth->read_user($login);
58   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
59 }
60
61 sub drop_privileges {
62   my $user = $lx_office_conf{task_server}->{run_as};
63   return unless $user;
64
65   my ($uid, $gid);
66   while (my @details = getpwent()) {
67     next unless $details[0] eq $user;
68     ($uid, $gid) = @details[2, 3];
69     last;
70   }
71   endpwent();
72
73   if (!$uid) {
74     print "Error: Cannot drop privileges to ${user}: user does not exist\n";
75     exit 1;
76   }
77
78   if (!setgid($gid)) {
79     print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
80     exit 1;
81   }
82
83   if (!setuid($uid)) {
84     print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
85     exit 1;
86   }
87 }
88
89 sub gd_preconfig {
90   my $self = shift;
91
92   SL::LxOfficeConf->read;
93
94   die "Missing section [task_server] in config file"                unless $lx_office_conf{task_server};
95   die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
96
97   drop_privileges();
98   lxinit();
99
100   return ();
101 }
102
103 sub gd_run {
104   while (1) {
105     my $ok = eval {
106       $::lxdebug->message(0, "Retrieving jobs") if $lx_office_conf{task_server}->{debug};
107
108       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
109
110       $::lxdebug->message(0, "  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if $lx_office_conf{task_server}->{debug} && @{ $jobs };
111
112       foreach my $job (@{ $jobs }) {
113         # Provide fresh global variables in case legacy code modifies
114         # them somehow.
115         $::locale = Locale->new($::lx_office_conf{system}->{language});
116         $::form   = Form->new;
117
118         $job->run;
119       }
120
121       1;
122     };
123
124     if ($lx_office_conf{task_server}->{debug}) {
125       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
126       $::lxdebug->message(0, "Sleeping");
127     }
128
129     my $seconds = 60 - (localtime)[0];
130     sleep($seconds < 30 ? $seconds + 60 : $seconds);
131   }
132 }
133
134 my $cwd     = getcwd();
135 my $pidbase = "${cwd}/users/pid";
136
137 mkdir($pidbase) if !-d $pidbase;
138
139 my $file = -f "${cwd}/config/lx_office.conf" ? "${cwd}/config/lx_office.conf" : "${cwd}/config/lx_office.conf.default";
140 newdaemon(configfile => $file,
141           progname   => 'lx-office-task-server',
142           pidbase    => "${pidbase}/",
143           );
144
145 1;