-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_dav_acl.c
1617 lines (1326 loc) · 49.1 KB
/
mod_dav_acl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This is part of a mod_dav_acl library.
*
* Copyright (C) 2006 Nokia Corporation.
*
* Contact: Jari Urpalainen <jari.urpalainen@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#if 0
LoadModule dav_acl_module modules/mod_davacl.so
<Location /principals>
Dav on
AuthType Digest
AuthName "testing"
AuthUserFile /var/www/passwords
Require valid-user
AuthDigestProvider file
FileETag MTime NanoSecsMTime
DAVETagResponse on
DAVACL on
AclOwnerFullRights on
AclLockFile /var/tmp/davacl.lock
AclSharedMemFile /var/tmp/davacl.shm
AclSharedMemSize 16000
AclPrincipals http://example.com/principals
AclPrincipalDir /jfs/principals
AclUsePropertyDB on
AclAggregatedCurrentUserPrivilegeSet on
AclAggregatedAcl off
</Location>
#endif
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"
#include "http_core.h"
#include "apr_shm.h"
#include "apr_rmm.h"
#include "apr_strings.h"
#include "apr_global_mutex.h"
#undef PACKAGE_NAME
#undef PACKAGE_VERSION
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#include "config.h"
#include "mod_dav.h"
#include "unixd.h"
#include <libxml/tree.h>
#include "mod_dav_acl.h"
#include "acl_liveprops.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stddef.h>
#include "acl_private.h"
module AP_MODULE_DECLARE_DATA dav_acl_module[];
/* ACL METHOD index */
static int iM_ACL, iM_HEAD;
#define ACL_FILTER "acl_filter_in"
static dav_error *check_acl(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
dav_error *err = NULL;
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "write-acl" } };
/* Note: depth == 0. Implies no need for a multistatus response. */
if ((err = dav_validate_request(r, (dav_resource *) resource, 0,
NULL, NULL, DAV_VALIDATE_RESOURCE,
NULL)) != NULL) {
/* ### add a higher-level description? */
return dav_new_error(r->pool, err->status, 0, err->aprerr, err->desc);
}
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
static dav_error *parent_check(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos,
const dav_prop_name *privs, int count)
{
dav_resource *parent = NULL;
dav_error *err = NULL;
if ((err = repos->get_parent_resource(resource, &parent)))
return err;
return acl_check_req(r, repos->get_pathname(parent), parent, conf, privs,
count, 0);
}
static dav_error *check_put(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
if (resource->exists) {
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "write-content" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
else {
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "bind" } };
return parent_check(r, resource, conf, repos, ARRAY(privs));
}
}
static dav_error *check_proppatch(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "write-properties" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
static dav_error *check_get(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
const dav_prop_name privs[] = { { NS_DAV, "read" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
static dav_error *check_mkcol(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "bind" } };
return parent_check(r, resource, conf, repos, ARRAY(privs));
}
static dav_error *check_delete(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
const dav_prop_name privs[] = { { NS_DAV, "write" }, { NS_DAV, "unbind" } };
/* this is more liberal than rfc3744, i.e. the write privilege on a
* resource will allow delete to happen */
if (acl_check_req(r, r->filename, resource, conf, privs, 1, 0) == NULL)
return NULL;
return parent_check(r, resource, conf, repos, privs + 1, 1);
}
static dav_error *check_copy(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
if (r->main == NULL) { /* source */
const dav_prop_name privs[] = { { NS_DAV, "read" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
else { /* destination */
const dav_prop_name privs[] = {
{ NS_DAV, "write-content" }, /* 0 */
{ NS_DAV, "write-properties" }, /* 1 */
{ NS_DAV, "write" }, /* 2 */
{ NS_DAV, "bind" } }; /* 3 */
if (resource->exists) {
dav_error *err = acl_check_req(r, r->filename,
resource, conf, privs + 2, 1, 0);
if (err == NULL)
return NULL;
else if (acl_check_req(r, r->filename, resource,
conf, privs, 1, 0) == NULL &&
acl_check_req(r, r->filename, resource,
conf, privs + 1, 1, 0) == NULL)
return NULL;
return err;
}
else {
return parent_check(r, resource, conf, repos, privs + 2, 2);
}
}
}
static dav_error *check_move(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
if (r->main == NULL) { /* source */
const dav_prop_name privs[] = { { NS_DAV, "write" },
{ NS_DAV, "unbind" } };
if (acl_check_req(r, r->filename, resource, conf, privs, 1, 0) == NULL)
return NULL;
return parent_check(r, resource, conf, repos, ARRAY (privs));
}
else { /* destination */
const dav_prop_name privs[] = {
{ NS_DAV, "write" }, /* 0 */
{ NS_DAV, "write-content" }, /* 1 */
{ NS_DAV, "write-properties" }, /* 2 */
{ NS_DAV, "unbind" }, /* 3 */
{ NS_DAV, "write" }, /* 4 */
{ NS_DAV, "bind" } }; /* 5 */
if (resource->exists) {
dav_error *err = NULL;
if (acl_check_req(r, r->filename, resource, conf, privs, 1, 0) == NULL)
return NULL;
else if (acl_check_req(r, r->filename, resource, conf,
privs + 1, 1, 0) == NULL &&
acl_check_req(r, r->filename, resource, conf,
privs + 2, 1, 0) == NULL)
return NULL;
err = parent_check(r, resource, conf, repos, privs + 3, 2);
if (err)
return err;
}
return parent_check(r, resource, conf, repos, privs + 4, 2);
}
}
static dav_error *check_lock(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
if (resource->exists) {
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "write-content" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
else {
const dav_prop_name privs[] = {
{ NS_DAV, "write" },
{ NS_DAV, "bind" } };
return parent_check(r, resource, conf, repos, ARRAY(privs));
}
}
static dav_error *check_unlock(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf,
const dav_hooks_repository *repos)
{
const dav_prop_name privs[] = { { NS_DAV, "unlock" } };
return acl_check_req(r, r->filename, resource, conf, ARRAY(privs), 0);
}
/**
* generic acl checking for different methods (basic set of methods supported)
* returns null if no err
*/
static dav_error *check_methods(request_rec *r, const dav_resource *resource,
davacl_dir_cfg *conf)
{
const dav_hooks_repository *repos = REPOS(conf);
if (repos == NULL || resource == NULL)
return dav_acl_privilege_error(r, "unknown", NULL);
switch (r->method_number) {
default:
if (r->method_number == iM_ACL) {
return check_acl(r, resource, conf, repos);
}
else if (r->method_number == iM_HEAD) {
return check_get(r, resource, conf, repos);
}
else {
TRACE(r, "Unknown methdod:%d", r->method_number);
return NULL;
}
break;
case M_PUT:
return check_put(r, resource, conf, repos);
case M_PROPPATCH:
return check_proppatch(r, resource, conf, repos);
case M_MKCOL:
return check_mkcol(r, resource, conf, repos);
case M_PROPFIND:
/* done with individual properties within dav_get_props() and
* dav_get_allprops */
return NULL;
case M_DELETE:
return check_delete(r, resource, conf, repos);
case M_OPTIONS:
case M_GET:
return check_get(r, resource, conf, repos);
case M_COPY:
return check_copy(r, resource, conf, repos);
case M_MOVE:
return check_move(r, resource, conf, repos);
case M_LOCK:
return check_lock(r, resource, conf, repos);
case M_UNLOCK:
return check_unlock(r, resource, conf, repos);
}
}
static void principal(request_rec *r, const dav_resource *resource,
const davacl_dir_cfg *conf, xmlNode * child,
apr_hash_t *base)
{
xmlNode * uri;
const char *pch;
FOR_CHILD(uri, child) {
if (NODE_NOT_DAV(uri)) {
;
}
else if (NODE_MATCH(uri, "href")) {
xmlChar *href = xmlNodeGetContent(uri);
if (href) {
pch = apr_pstrdup(r->pool, (char *) href);
apr_hash_set(base, pch, APR_HASH_KEY_STRING, pch);
xmlFree(href);
}
}
else if (NODE_MATCH(uri, "property")) {
xmlNode * prop;
FOR_CHILD(prop, uri) {
pch = NULL;
if (NODE_NOT_DAV(prop))
;
else if (NODE_MATCH(prop, "owner"))
pch = acl_get_owner(resource, conf, r->filename);
else if (NODE_MATCH(prop, "group"))
pch = acl_get_group(resource, conf);
if (pch)
apr_hash_set(base, pch, APR_HASH_KEY_STRING, pch);
}
}
}
}
/** locate principal uris from acl */
static dav_error *find_principals(request_rec *r, const dav_resource *resource,
const davacl_dir_cfg *conf,
apr_hash_t **pbase)
{
xmlDoc *doc;
xmlNode *node;
apr_hash_t *base;
const char *pch;
int rc, size;
rc = acl_get_acl(resource, conf, r->filename, &pch, &size);
if (rc < 0)
return dav_new_error(r->pool, HTTP_NOT_FOUND, 0, APR_SUCCESS,
apr_psprintf(r->pool, "No ACL found for the resource %s.",
ap_escape_html(r->pool, r->uri)));
doc = xmlParseMemory(pch, size);
node = doc ? doc->children : NULL;
base = apr_hash_make(r->pool);
for ( ; node; node = node->next) {
xmlNode *ace, *child;
if (NODE_NOT_DAV(node) || NODE_MATCH(node, "acl") == FALSE)
continue;
FOR_CHILD(ace, node) {
if (NODE_NOT_DAV(ace) || NODE_MATCH(ace, "ace") == FALSE)
continue;
FOR_CHILD(child, ace) {
if (NODE_NOT_DAV(child))
;
else if (NODE_MATCH(child, "principal"))
principal(r, resource, conf, child, base);
}
}
}
xmlFreeDoc(doc);
*pbase = base;
return NULL;
}
/** set acl for a resource */
static dav_error *acl_set(request_rec *r, dav_resource *resource,
davacl_dir_cfg *conf)
{
dav_buffer buffer[1] = { { 0 } };
if (dav_acl_read_body(r, buffer) < 0)
return dav_new_error(r->pool, HTTP_BAD_REQUEST, 0, APR_SUCCESS,
"Message's request body could not be read");
return acl_store_acl(r, resource, conf, buffer);
}
/* server config create */
static void *create_server_config(apr_pool_t *p, server_rec *s)
{
return apr_pcalloc(p, sizeof(davacl_server_cfg));
}
/* global lock filename */
static const char *cmd_lock_file(cmd_parms *cmd, void *mconfig, const char *pch)
{
server_rec *s = cmd->server;
davacl_server_cfg *
conf = ap_get_module_config(s->module_config, dav_acl_module);
conf->lock_file = apr_psprintf(cmd->pool, "%s", pch ? pch :
"/var/tmp/davacl.lock");
return NULL;
}
static void *create_dir_config(apr_pool_t *p, char *dirspec)
{
davacl_dir_cfg *conf = apr_pcalloc(p, sizeof(*conf));
conf->pool = p;
#if !HAVE_XATTR
conf->use_std_property_db = TRUE;
#endif
return conf;
}
/* directive */
static const char *cmd_shared_mem_file(cmd_parms *cmd, void *mconfig,
const char *pch)
{
davacl_dir_cfg *conf = mconfig;
conf->shared_mem_file = apr_psprintf(cmd->pool, "%s",
pch ? pch : "/var/tmp/davacl.shm");
return NULL;
}
/* directive */
static const char *cmd_shared_mem_size(cmd_parms *cmd, void *mconfig,
const char *pch)
{
davacl_dir_cfg *conf = mconfig;
conf->shared_mem = pch ? atoi(pch) : 65000;
if (conf->shared_mem < sizeof(apr_rmm_off_t))
conf->shared_mem = sizeof(apr_rmm_off_t) + 500;
return NULL;
}
/* directive */
static const char *cmd_owner_full_rights(cmd_parms *cmd, void *mconfig, int arg)
{
davacl_dir_cfg *conf = mconfig;
conf->owner_full_rights = arg;
return NULL;
}
/* directive */
static const char *cmd_acl_aggregated(cmd_parms *cmd, void *mconfig, int arg)
{
davacl_dir_cfg *conf = mconfig;
conf->acl_aggregated = arg;
return NULL;
}
/* directive */
static const char *cmd_cups_aggregated(cmd_parms *cmd, void *mconfig, int arg)
{
davacl_dir_cfg *conf = mconfig;
conf->cups_aggregated = arg;
return NULL;
}
/* directive */
static const char *cmd_use_property_db(cmd_parms *cmd, void *mconfig, int arg)
{
davacl_dir_cfg *conf = mconfig;
#if HAVE_XATTR
conf->use_std_property_db = arg;
#else
conf->use_std_property_db = TRUE;
#endif
return NULL;
}
/* directive */
static const char *cmd_principal_dir(cmd_parms *cmd, void *mconfig,
const char *pch)
{
davacl_dir_cfg *conf = mconfig;
conf->principal_dir = apr_psprintf(cmd->pool, "%s",
pch ? pch : "/var/www/principals");
if (pch == NULL)
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server,
"mod_dav_acl: directory for principals not given "
"(anticipating %s)", conf->principal_dir);
return NULL;
}
/* directive */
static const char *cmd_principal_host_path(cmd_parms *cmd, void *mconfig,
const char *pch)
{
davacl_dir_cfg *conf = mconfig;
if (pch == NULL) {
char sz[200] = "";
if (gethostname(sz, sizeof(sz) - 1) != 0)
strcpy(sz, "localhost");
conf->principals = apr_psprintf(cmd->pool, "http://%s/principals", sz);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server,
"mod_dav_acl: principal base uri not given "
"(anticipating %s)", conf->principals);
}
else {
conf->principals = apr_psprintf(cmd->pool, "%s", pch);
}
return NULL;
}
/* cmd callbacks */
static const command_rec dav_acl_cmds[] =
{
AP_INIT_TAKE1(
"AclLockFile", /* directive name */
cmd_lock_file, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"lock file name" /* directive description */
),
AP_INIT_TAKE1(
"AclSharedMemFile", /* directive name */
cmd_shared_mem_file, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"shared mem file name" /* directive description */
),
AP_INIT_TAKE1(
"AclSharedMemSize", /* directive name */
cmd_shared_mem_size, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"Shared mem size for user identities"
/* directive description */
),
AP_INIT_FLAG(
"AclOwnerFullRights", /* directive name */
cmd_owner_full_rights, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"Only owner has full rights if no acl is set"
/* directive description */
),
AP_INIT_FLAG(
"AclUsePropertyDB", /* directive name */
cmd_use_property_db, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"Standard property db used for ACLs"
/* directive description */
),
AP_INIT_TAKE1(
"AclPrincipals", /* directive name */
cmd_principal_host_path, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"Base URI for principals (http://example.com/principals)"
/* directive description */
),
AP_INIT_TAKE1(
"AclPrincipalDir", /* directive name */
cmd_principal_dir, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"Local real directory for principals (/var/www/principals)"
/* directive description */
),
AP_INIT_FLAG(
"AclAggregatedCurrentUserPrivilegeSet", /* directive name */
cmd_cups_aggregated, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"current-user-privilege-set aggregated into 'read' privilege"
/* directive description */
),
AP_INIT_FLAG(
"AclAggregatedAcl", /* directive name */
cmd_acl_aggregated, /* config action routine */
NULL, /* argument to include in call */
OR_OPTIONS, /* where available */
"ACLs reading aggregated into 'read' privilege"
/* directive description */
),
{NULL}
};
static inline void init_acl(request_rec *r, davacl_server_cfg **sconf,
davacl_dir_cfg **conf)
{
*sconf = ap_get_module_config(r->server->module_config, dav_acl_module);
*conf = ap_get_module_config(r->per_dir_config, dav_acl_module);
acl_lock(*sconf);
if (*conf && (*conf)->shm == NULL)
acl_shm_init(r, *conf);
}
/** check acl for a method */
static dav_error *check_method(request_rec *r, const dav_resource *resource)
{
dav_error *err;
davacl_server_cfg *sconf;
davacl_dir_cfg *conf;
init_acl(r, &sconf, &conf);
err = check_methods(r, resource, conf);
acl_unlock(sconf);
return err;
}
/** check read privilege */
static dav_error *check_read_common(davacl_dir_cfg *conf, request_rec *r,
const dav_resource *resource)
{
const dav_prop_name privs[] = { { NS_DAV, "read" } };
const dav_hooks_repository *repos = REPOS(conf);
const char *filename = repos ? repos->get_pathname(resource) : NULL;
return acl_check_req(r, filename, resource, conf, ARRAY(privs), 0);
}
/** check prop name privilege */
DAV_DECLARE (dav_error *) dav_acl_check(request_rec *r,
const dav_resource *resource,
const dav_prop_name *privs, int c)
{
dav_error *err;
const char *filename;
davacl_server_cfg *sconf;
davacl_dir_cfg *conf;
const dav_hooks_repository *repos;
init_acl(r, &sconf, &conf);
repos = REPOS(conf);
filename = repos ? repos->get_pathname(resource) : NULL;
err = acl_check_req(r, filename, resource, conf, privs, c, 0);
acl_unlock(sconf);
return err;
}
/** check read privilege with locks */
static dav_error *check_read(request_rec *r, const dav_resource *resource)
{
dav_error *err;
davacl_server_cfg *sconf;
davacl_dir_cfg *conf;
init_acl(r, &sconf, &conf);
err = check_read_common(conf, r, resource);
acl_unlock(sconf);
return err;
}
/** check read privilege without locks */
static dav_error *check_read_nolock(request_rec *r,
const dav_resource *resource)
{
davacl_dir_cfg *conf;
conf = ap_get_module_config(r->per_dir_config, dav_acl_module);
return check_read_common(conf, r, resource);
}
/** check prop privilege */
static dav_error *check_prop_common(davacl_dir_cfg *conf, request_rec *r,
const dav_resource *resource,
const dav_prop_name *name,
dav_prop_insert what)
{
dav_prop_name privs[] = { { NS_DAV, "read-acl" } };
const char *filename;
const dav_hooks_repository *repos;
if (name->ns == NULL || strcmp(name->ns, NS_DAV)) {
return NULL;
}
else {
int acl = strcmp(name->name, "acl") == 0,
cups = strcmp(name->name, "current-user-privilege-set") == 0;
if ((!acl && !cups) || (conf->acl_aggregated && acl) ||
(conf->cups_aggregated && cups))
return NULL;
if (cups)
privs->name = "read-current-user-privilege-set";
}
/* only acl or current-user-privilege-set tested */
repos = REPOS(conf);
filename = repos ? repos->get_pathname(resource) : NULL;
return acl_check_req(r, filename, resource, conf, ARRAY(privs), 0);
}
/** prop privilege without locks */
static dav_error *check_prop_nolock(request_rec *r,
const dav_resource *resource,
const dav_prop_name *name,
dav_prop_insert what)
{
davacl_dir_cfg *conf;
conf = ap_get_module_config(r->per_dir_config, dav_acl_module);
return check_prop_common(conf, r, resource, name, what);
}
/**
* prop privilege with locks for special properties
* only "acl" and "current-user-privilege-set" are checked
*/
static dav_error *check_prop(request_rec *r, const dav_resource *resource,
const dav_prop_name *name, dav_prop_insert what)
{
dav_error *err;
davacl_server_cfg *sconf;
davacl_dir_cfg *conf;
init_acl(r, &sconf, &conf);
err = check_prop_common(conf, r, resource, name, what);
acl_unlock(sconf);
return err;
}
/** proppatch could be optimized as group_member_set is being tracked */
static void post_processing(request_rec *r, const dav_resource *resource,
int fstore_owner)
{
const char *pch;
davacl_server_cfg *sconf;
davacl_dir_cfg *conf;
const dav_hooks_propdb *db_hooks;
if (r == NULL || resource == NULL || r->user == NULL)
return;
init_acl(r, &sconf, &conf);
db_hooks = DBHOOKS(conf);
if (db_hooks == NULL || conf->principals == NULL) {
acl_unlock(sconf);
return;
}
/* successful put, mkcol or e.g. mkcalendar,
* i.e. store the owner of a resource */
if (fstore_owner)
acl_store_owner(r, resource, conf);
/* principal update ? */
pch = acl_get_principal_dir(conf);
if (pch && strncmp(pch, r->filename, strlen(pch)) == 0) {
if ((r->method_number == M_PUT &&
(r->status == 201 || r->status == 204)) ||
(r->method_number == M_PROPPATCH &&
(r->status == 207 || r->status == 200)))
acl_update_principal(r, resource, conf);
else if (r->method_number != M_MKCOL &&
r->method_number != M_PUT &&
r->method_number != M_PROPPATCH)
/* FULL update of principals */
acl_update_all_principals(r, conf);
}
acl_unlock(sconf);
}
/** acl hooks */
static dav_acl_provider acl =
{
check_method,
check_read,
check_prop,
post_processing,
NULL
};
static dav_acl_provider *acl_nolock(void)
{
static dav_acl_provider nolock =
{
.acl_check_read = check_read_nolock,
.acl_check_prop = check_prop_nolock,
0
};
return &nolock;
}
/** principal property search report */
static int dump_principal_search_property_set(request_rec *r)
{
xmlNs *ns;
xmlNode *child, *node;
xmlChar *pch;
int cb;
xmlDoc * doc = xmlNewDoc((const xmlChar *) XML_VERSION);
child = doc->children = xmlNewDocNode(doc, NULL,
(const xmlChar *) "principal-search-property-set", NULL);
xmlSetNs(child, ns = xmlNewNs(child, (const xmlChar *) NS_DAV,
(const xmlChar *) "D"));
node = xmlNewChild(child, ns,
(const xmlChar *) "principal-search-property", NULL);
child = xmlNewChild(node, ns, (const xmlChar *) "prop", NULL);
xmlNewChild(child, ns, (const xmlChar *) "displayname", NULL);
child = xmlNewChild(node, ns, (const xmlChar *) "description",
(const xmlChar *) "Full name");
xmlSetProp(child, (const xmlChar *) "xml:lang", (const xmlChar *) "en");
xmlDocDumpFormatMemoryEnc(doc, &pch, &cb, "UTF-8", 1);
ap_set_content_type(r, "application/xml");
ap_rprintf(r, "%.*s", cb, pch);
xmlFree(pch);
xmlFreeDoc(doc);
r->status_line = ap_get_status_line(r->status);
return DONE;
}
/** send generic props */
static void send_props(apr_bucket_brigade *bb, request_rec *r, request_rec *rf,
dav_resource *resource, apr_pool_t *subpool,
xmlNode * node_prop)
{
dav_response response[1] = { { 0 } };
dav_propdb *propdb = NULL;
rf->user = r->user;
rf->per_dir_config = r->per_dir_config;
rf->server = r->server;
response->href = rf->uri;
if (node_prop) {
apr_xml_doc *adoc = dav_acl_get_prop_doc(r, node_prop);
dav_open_propdb(rf, NULL, resource, 1, adoc->namespaces, &propdb);
if (propdb)
response->propresult = dav_get_props(propdb, adoc);
}
apr_pool_clear(subpool);
dav_send_one_response(response, bb, r, subpool);
if (propdb)
dav_close_propdb(propdb);
}
/** init multistatus response */
static void init_multistatus(apr_bucket_brigade **bb, request_rec *r)
{
if (*bb)
return;
*bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
dav_begin_multistatus(*bb, r, HTTP_MULTI_STATUS, NULL);
}
/** send principal props */
static void send_principal_props(const char *uri, apr_bucket_brigade **bb,
request_rec *r, davacl_dir_cfg *conf,
apr_pool_t *subpool, xmlNode * node_prop)
{
dav_resource *resource = NULL;
dav_error *err = NULL;
request_rec *rf;
const dav_hooks_repository *repos = REPOS(conf);
if (conf->principal_dir == NULL || conf->principals == NULL ||
repos == NULL)
return;
rf = apr_pcalloc(subpool, sizeof(*rf));
rf->user = r->user;
rf->uri = apr_psprintf(rf->pool = subpool, "%s", uri);
rf->filename = apr_psprintf(rf->pool, "%s%s", conf->principal_dir,
rf->uri + strlen(conf->principals));
apr_stat(&rf->finfo, rf->filename, APR_FINFO_MIN, rf->pool);
err = repos->get_resource(rf, NULL, NULL, 0, &resource);
if (err == NULL) {
resource->acls = acl_nolock();
/* don't report principal if no read privilege exists */
if (check_read_common(conf, rf, resource) == NULL) {
init_multistatus(bb, r);
send_props(*bb, r, rf, resource, subpool, node_prop);
}
}
}
/** principal property set report */
static int dump_principal_prop_set(dav_resource *resource, request_rec *r,