DAViCal
Loading...
Searching...
No Matches
caldav-ACL.php
1<?php
11dbg_error_log("ACL", "method handler");
12
13require_once('DAVResource.php');
14
15$request->NeedPrivilege('DAV::write-acl');
16
17if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['put']) && $c->dbg['put'])) ) {
18 $fh = fopen('/var/log/davical/MOVE.debug','w');
19 if ( $fh ) {
20 fwrite($fh,$request->raw_post);
21 fclose($fh);
22 }
23}
24
25$resource = new DAVResource( $request->path );
26
90
91$xmltree = BuildXMLTree( $request->xml_tags );
92$aces = $xmltree->GetPath("/DAV::acl/*");
93
94$grantor = new DAVResource($request->path);
95if ( ! $grantor->Exists() ) $request->DoResponse( 404 );
96if ( ! $grantor->IsCollection() )
97 $request->PreconditionFailed(403,'not-supported-privilege','ACLs are only supported on Principals or Collections');
98
99$grantor->NeedPrivilege('write-acl');
100
101$cache_delete_list = array();
102
103$qry = new AwlQuery('BEGIN');
104$qry->Exec('ACL',__LINE__,__FILE__);
105
106function process_ace( $grantor, $by_principal, $by_collection, $ace ) {
107 global $cache_delete_list, $request;
108
109 $elements = $ace->GetContent();
110 $principal_node = $elements[0];
111 $grant = $elements[1];
112 if ( $principal_node->GetNSTag() != 'DAV::principal' ) $request->MalformedRequest('ACL request must contain a principal, not '.$principal->GetNSTag());
113 $grant_tag = $grant->GetNSTag();
114 if ( $grant_tag == 'DAV::deny' ) $request->PreconditionFailed(403,'grant-only');
115 if ( $grant_tag == 'DAV::invert' ) $request->PreconditionFailed(403,'no-invert');
116 if ( $grant->GetNSTag() != 'DAV::grant' ) $request->MalformedRequest('ACL request must contain a principal for each ACE');
117
118 $privilege_names = array();
119 $xml_privs = $grant->GetPath("/DAV::grant/DAV::privilege/*");
120 foreach( $xml_privs AS $k => $priv ) {
121 $privilege_names[] = $priv->GetNSTag();
122 }
123 $privileges = privilege_to_bits($privilege_names);
124
125 $principal_content = $principal_node->GetContent();
126 if ( count($principal_content) != 1 ) $request->MalformedRequest('ACL request must contain exactly one principal per ACE');
127 $principal_content = $principal_content[0];
128
129 dbg_error_log( 'ACE', 'NSTag: "%s", by_collection: %s, by_principal: %s', $principal_content->GetNSTag(), $by_collection ?? 'Null', $by_principal ?? 'Null');
130
131 switch( $principal_content->GetNSTag() ) {
132 case 'DAV::property':
133 $principal_property = $principal_content->GetContent();
134 if ( $principal_property[0]->GetNSTag() != 'DAV::owner' ) $request->PreconditionFailed(403, 'recognized-principal' );
135 if ( privilege_to_bits('all') != $privileges ) {
136 $request->PreconditionFailed(403, 'no-protected-ace-conflict', 'Owner must always have all permissions' );
137 }
138 break; // and then we ignore it, since it's protected
139
140 case 'DAV::unauthenticated':
141 $request->PreconditionFailed(403, 'allowed-principal', 'May not set privileges for unauthenticated users' );
142 break;
143
144 case 'DAV::href':
145 $principal_type = 'href';
146 $grantee = new DAVResource( DeconstructURL($principal_content->GetContent()) );
147 $grantee_id = $grantee->getProperty('principal_id');
148
149 if ( !$grantee->Exists() || !$grantee->IsPrincipal() )
150 $request->PreconditionFailed(403,'recognized-principal', 'Principal "' . $principal_content->GetContent() . '" not found.');
151
152 $sqlparms = array( ':to_principal' => $grantee_id);
153 $where = 'WHERE to_principal=:to_principal AND ';
154 if ( isset($by_principal) ) {
155 $sqlparms[':by_principal'] = $by_principal;
156 $where .= 'by_principal = :by_principal';
157 }
158 else {
159 $sqlparms[':by_collection'] = $by_collection;
160 $where .= 'by_collection = :by_collection';
161 }
162
163 $qry = new AwlQuery('SELECT privileges FROM grants '.$where, $sqlparms);
164 if ( $qry->Exec('ACL',__LINE__,__FILE__) && $qry->rows() == 1 && $current = $qry->Fetch() ) {
165 $sql = 'UPDATE grants SET privileges=:privileges::INT::BIT(24) '.$where;
166 }
167 else {
168 $sqlparms[':by_principal'] = $by_principal;
169 $sqlparms[':by_collection'] = $by_collection;
170 $sql = 'INSERT INTO grants (by_principal, by_collection, to_principal, privileges) VALUES(:by_principal, :by_collection, :to_principal, :privileges::INT::BIT(24))';
171 }
172 $sqlparms[':privileges'] = $privileges;
173 $qry = new AwlQuery($sql, $sqlparms);
174 if ( $qry->Exec('ACL',__LINE__,__FILE__) ) {
175 Principal::cacheDelete('dav_name',$grantee->dav_name());
176 Principal::cacheFlush('principal_id IN (SELECT member_id FROM group_member WHERE group_id = ?)', array($grantee_id));
177 }
178
182 $cache = getCacheInstance();
183 $cache->flush();
184
185 #Principal::cacheFlush('TRUE');
186
187 break;
188
189 case 'DAV::authenticated':
190 $principal_type = 'authenticated';
191 $grant_default_privs = $grantor->GetProperty('default_privileges');
192 if ( isset($grant_default_privs) && bindec($grant_default_privs) == $privileges ) break; // There is no change, so skip it
193 $sqlparms = array( ':privileges' => $privileges );
194 if ( isset($by_collection) ) {
195 $sql = 'UPDATE collection SET default_privileges=:privileges::INT::BIT(24) WHERE collection_id=:by_collection';
196 $sqlparms[':by_collection'] = $by_collection;
197 }
198 else {
199 $sql = 'UPDATE principal SET default_privileges=:privileges::INT::BIT(24) WHERE principal_id=:by_principal';
200 $sqlparms[':by_principal'] = $by_principal;
201 }
202 $qry = new AwlQuery($sql, $sqlparms);
203 if ( $qry->Exec('ACL',__LINE__,__FILE__) ) {
207 Principal::cacheFlush('TRUE');
208 }
209 break;
210
211 case 'DAV::all':
212// $principal_type = 'all';
213 $request->PreconditionFailed(403, 'allowed-principal', 'May not set privileges for unauthenticated users' );
214 break;
215
216 default:
217 $request->PreconditionFailed(403, 'recognized-principal' );
218 break;
219 }
220
221}
222
223$by_principal = ($grantor->IsPrincipal() ? $grantor->GetProperty('principal_id') : null);
224$by_collection = ($grantor->IsPrincipal() ? null : $grantor->GetProperty('collection_id'));
225
226foreach( $aces AS $k => $ace ) {
227 process_ace($grantor, $by_principal, $by_collection, $ace);
228}
229
230$qry = new AwlQuery('COMMIT');
231$qry->Exec('ACL',__LINE__,__FILE__);
232
233
234$request->DoResponse( 200 );