Handle hostnames with upper-case letters
[webmin.git] / at / at-lib.pl
1 # at-lib.pl
2 # Functions for listing and creating at jobs
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 %access = &get_module_acl();
8
9 do "$config{'at_style'}-lib.pl";
10
11 # wrap_lines(text, width)
12 # Given a multi-line string, return an array of lines wrapped to
13 # the given width
14 sub wrap_lines
15 {
16 local @rv;
17 local $w = $_[1];
18 foreach $rest (split(/\n/, $_[0])) {
19         if ($rest =~ /\S/) {
20                 while(length($rest) > $w) {
21                         push(@rv, substr($rest, 0, $w));
22                         $rest = substr($rest, $w);
23                         }
24                 push(@rv, $rest);
25                 }
26         else {
27                 # Empty line .. keep as it is
28                 push(@rv, $rest);
29                 }
30         }
31 return @rv;
32 }
33
34 # can_edit_user(&access, user)
35 sub can_edit_user
36 {
37 local %umap;
38 map { $umap{$_}++; } split(/\s+/, $_[0]->{'users'});
39 if ($_[0]->{'mode'} == 1 && !$umap{$_[1]} ||
40     $_[0]->{'mode'} == 2 && $umap{$_[1]}) { return 0; }
41 elsif ($_[0]->{'mode'} == 3) {
42         return $remote_user eq $_[1];
43         }
44 else {
45         return 1;
46         }
47 }
48
49 # list_allowed()
50 # Returns a list of all users in the cron allow file
51 sub list_allowed
52 {
53 local(@rv, $_);
54 &open_readfile(ALLOW, $config{allow_file});
55 while(<ALLOW>) {
56         next if (/^\s*#/);
57         chop; push(@rv, $_) if (/\S/);
58         }
59 close(ALLOW);
60 return @rv;
61 }
62
63
64 # list_denied()
65 # Return a list of users from the cron deny file
66 sub list_denied
67 {
68 local(@rv, $_);
69 &open_readfile(DENY, $config{deny_file});
70 while(<DENY>) {
71         next if (/^\s*#/);
72         chop; push(@rv, $_) if (/\S/);
73         }
74 close(DENY);
75 return @rv;
76 }
77
78
79 # save_allowed(user, user, ...)
80 # Save the list of allowed users
81 sub save_allowed
82 {
83 &lock_file($config{allow_file});
84 if (@_) {
85         local($_);
86         &open_tempfile(ALLOW, ">$config{allow_file}");
87         foreach my $u (@_) {
88                 &print_tempfile(ALLOW, $u,"\n");
89                 }
90         &close_tempfile(ALLOW);
91         chmod(0444, $config{allow_file});
92         }
93 else {
94         &unlink_file($config{allow_file});
95         }
96 &unlock_file($config{allow_file});
97 }
98
99
100 # save_denied(user, user, ...)
101 # Save the list of denied users
102 sub save_denied
103 {
104 &lock_file($config{deny_file});
105 if (@_ || !-r $config{'allow_file'}) {
106         &open_tempfile(DENY, ">$config{deny_file}");
107         foreach my $u (@_) {
108                 &print_tempfile(DENY, $u,"\n");
109                 }
110         &close_tempfile(DENY);
111         chmod(0444, $config{deny_file});
112         }
113 else {
114         &unlink_file($config{deny_file});
115         }
116 &unlock_file($config{deny_file});
117 }
118
119 # can_use_at(user)
120 # Returns 1 if some user is allowed to use At jobs, based on the allow
121 # any deny files.
122 sub can_use_at
123 {
124 local ($user) = @_;
125 if (!$config{'allow_file'}) {
126         return 1;       # not supported by OS
127         }
128 elsif (@allow = &list_allowed()) {
129         return &indexof($user, @allow) >= 0;    # check allowed list
130         }
131 elsif (@deny = &list_denied()) {
132         return &indexof($user, @denied) < 0;    # check denied list
133         }
134 else {
135         return 1;       # if neither exists, fall back to allowing all
136         }
137 }
138
139 1;
140