1 | package LATMOS::Accounts::Acls; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | use LATMOS::Accounts::Acls::Acl; |
---|
7 | |
---|
8 | our $VERSION = (q$Rev: 330 $ =~ /^Rev: (\d+) /)[0]; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | LATMOS::Accounts::Acls - Acl support in L<LATMOS::Accounts> |
---|
13 | |
---|
14 | =head1 FUNCTIONS |
---|
15 | |
---|
16 | =cut |
---|
17 | |
---|
18 | =head2 new ($file) |
---|
19 | |
---|
20 | Instanciate Acls from C<$file> |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub new { |
---|
25 | my ($class, $file) = @_; |
---|
26 | my $acls = bless( |
---|
27 | { |
---|
28 | _acls => [] |
---|
29 | }, $class |
---|
30 | ); |
---|
31 | |
---|
32 | # Default ACLS: |
---|
33 | # $acls->add( '*.log', [ '$uid: read', '*: deny' ] ); |
---|
34 | $acls->add( |
---|
35 | 'user.{userPasswd,userPassword,encryptedPassword,oldPassword}', |
---|
36 | [ '$uid: write', '*: deny' ], |
---|
37 | ); |
---|
38 | |
---|
39 | if ($file) { |
---|
40 | $acls->read_acl_file($file) or return; |
---|
41 | } |
---|
42 | |
---|
43 | $acls |
---|
44 | } |
---|
45 | |
---|
46 | =head1 ACL FILE FORMAT |
---|
47 | |
---|
48 | OBJ_TYPE.ATTRIBUTE |
---|
49 | USER: read,write |
---|
50 | USER2: read |
---|
51 | |
---|
52 | =over 4 |
---|
53 | |
---|
54 | =item OBJ_TYPE is the type the object, '*' match all |
---|
55 | |
---|
56 | =item ATTRIBUTE is either an attribute, either an comma separate attribute |
---|
57 | enclose into bracket, either a '*' to match any attribute. |
---|
58 | |
---|
59 | Special keyword C<@CREATE> and C<@DELETE> can be used to allow or deny object |
---|
60 | creation and deletion. In this case read permission and USER in form C<$...> for |
---|
61 | C<@CREATE> have no effect (see below). C<*> do not include C<@CREATE> and |
---|
62 | C<@DELETE> action. |
---|
63 | |
---|
64 | =item USER can be |
---|
65 | |
---|
66 | =over 4 |
---|
67 | |
---|
68 | =item a username |
---|
69 | |
---|
70 | =item group, prefixed by a '%' |
---|
71 | |
---|
72 | =item an attribute of the accessed object which should contains the username, |
---|
73 | prefixed by '$' |
---|
74 | |
---|
75 | =item C<*> to math any user |
---|
76 | |
---|
77 | =item @authenticated or @anonymous for any authenticated user and non |
---|
78 | authenticated user ('*' include both) |
---|
79 | |
---|
80 | =back |
---|
81 | |
---|
82 | =item Permission are read and or write, or deny. |
---|
83 | |
---|
84 | =back |
---|
85 | |
---|
86 | ACL are applied in the order they appear in the file. |
---|
87 | |
---|
88 | =cut |
---|
89 | |
---|
90 | =head2 read_acl_file ($file) |
---|
91 | |
---|
92 | Load acls from file C<$file>. |
---|
93 | |
---|
94 | =cut |
---|
95 | |
---|
96 | sub read_acl_file { |
---|
97 | my ($self, $file) = @_; |
---|
98 | |
---|
99 | my $acl; |
---|
100 | my $line_nb = 0; |
---|
101 | my $prevline = ""; |
---|
102 | if (open(my $handle, '<', $file)) { |
---|
103 | while(my $realline = <$handle>) { |
---|
104 | $line_nb++; |
---|
105 | chomp($realline); |
---|
106 | if ($realline =~ /\\$/) { |
---|
107 | # continuation line |
---|
108 | $prevline .= $realline; |
---|
109 | $prevline =~ s/\s*\\$//; |
---|
110 | next; |
---|
111 | } |
---|
112 | my $line = $prevline . $realline; # keep track to report malformed file |
---|
113 | $prevline = ""; |
---|
114 | $line =~ s/\s*#.*//; |
---|
115 | if ($line =~ /^(\S.*)/) { |
---|
116 | if ($acl) { |
---|
117 | push(@{$self->{_acls}}, $acl); |
---|
118 | } |
---|
119 | $acl = LATMOS::Accounts::Acls::Acl->new($1) or do { |
---|
120 | la_log(LA_ERR, "Error in %s at line %d", $file, |
---|
121 | $line_nb); |
---|
122 | return; |
---|
123 | }; |
---|
124 | } elsif ($line =~ /^\s+(\S.*)/) { |
---|
125 | if ($acl) { |
---|
126 | $acl->add_perm($line) or do { |
---|
127 | la_log(LA_ERR, "Error in %s at line %d", $file, |
---|
128 | $line_nb); |
---|
129 | return; |
---|
130 | }; |
---|
131 | } else { |
---|
132 | # err no acl |
---|
133 | return; |
---|
134 | } |
---|
135 | } elsif ($line =~ /^\s*$/) { |
---|
136 | # just empty line |
---|
137 | } else { |
---|
138 | } |
---|
139 | } |
---|
140 | if ($acl) { |
---|
141 | push(@{$self->{_acls}}, $acl); |
---|
142 | } |
---|
143 | } else { |
---|
144 | la_log(LA_ERR, "Cannot open acl file %s", $file); |
---|
145 | return; |
---|
146 | } |
---|
147 | 1; |
---|
148 | } |
---|
149 | |
---|
150 | =head2 add ($obj_dot_attr, $list) |
---|
151 | |
---|
152 | A new acl for C<$obj_dot_attr> (in form C<object.attribute>) with C<$list> |
---|
153 | permissions. |
---|
154 | |
---|
155 | =cut |
---|
156 | |
---|
157 | sub add { |
---|
158 | my ($self, $obj, $list) = @_; |
---|
159 | my $acl = LATMOS::Accounts::Acls::Acl->new( |
---|
160 | $obj, $list) or return; |
---|
161 | push(@{$self->{_acls}}, $acl); |
---|
162 | return 1; |
---|
163 | } |
---|
164 | |
---|
165 | =head2 check ($obj, $attr, $perm, $who, $groups) |
---|
166 | |
---|
167 | Return true is this acl apply to C<$obj>/C<$attr> for C<$perm> by user |
---|
168 | C<$who> in groups C<$groups>. |
---|
169 | |
---|
170 | =cut |
---|
171 | |
---|
172 | sub check { |
---|
173 | my ($self, $obj, $attr, $perm, $who, $groups) = @_; |
---|
174 | # Asking 'r' perm over create or delete has no sense: |
---|
175 | $attr =~ /^@(CREATE|DELETE)$/ && $perm eq 'r' and return; |
---|
176 | |
---|
177 | foreach my $acl (@{$self->{_acls}}) { |
---|
178 | my $res = $acl->match($obj, $attr, $perm, $who, $groups); |
---|
179 | defined($res) and return $res; |
---|
180 | } |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | =head2 dump |
---|
185 | |
---|
186 | Return currently load acl as text |
---|
187 | |
---|
188 | =cut |
---|
189 | |
---|
190 | sub dump { |
---|
191 | my ($self) = @_; |
---|
192 | foreach my $acl (@{$self->{_acls}}) { |
---|
193 | print $acl->dump, "\n"; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | 1; |
---|
198 | |
---|
199 | __END__ |
---|
200 | |
---|
201 | =head1 SEE ALSO |
---|
202 | |
---|
203 | L<LATMOS::Accounts::Acls::Acl> |
---|
204 | |
---|
205 | =head1 AUTHOR |
---|
206 | |
---|
207 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
208 | |
---|
209 | =head1 COPYRIGHT AND LICENSE |
---|
210 | |
---|
211 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
212 | |
---|
213 | This library is free software; you can redistribute it and/or modify |
---|
214 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
215 | at your option, any later version of Perl 5 you may have available. |
---|
216 | |
---|
217 | =cut |
---|