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