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