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