File: CFE_knowledge.cf

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (716 lines) | stat: -rw-r--r-- 30,494 bytes parent folder | download | duplicates (2)
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
##################################################################
#
# DO NOT EDIT THIS FILE. All policy files prefixed with CFE_ are maintained
# by CFEngine and its original state is required for internal operations of
# CFEngine. If the file has been modified CFEngine's upgrades may require
# manual intervention. Contact CFEngine support if additional information
# and/or recommendation is needed.
#
##################################################################
##################################################################
#
# cfe_internal_setup_knowledge
#  - populate knowledge bank database (CFE Enterprise)
#
##################################################################

bundle agent cfe_internal_setup_knowledge
# @brief Policy related to enterprise hubs
{

  classes:

      #
      # check when updates arrive, new compared to the database
      #

      "ENT_3572" -> { "ENT-3572" }
        comment => "Hosts with this class need to be sure that the ssl directory
                    is readable and executable by other users",
        or => {
                "enterprise_3_7_3", "enterprise_3_7_4", "enterprise_3_7_5",
                "enterprise_3_10_0"
        };

  vars:
      "install_logs" -> {"ENT-4564"}
        slist => findfiles("/var/log/CFEngine*Install.log"),
        unless => "windows";

  files:

    !mpf_disable_mission_portal_docroot_sync_from_share_gui::
      "$(cfe_internal_hub_vars.docroot)"
      comment => "Copy the basic knowledge base configuration from the installation to doc root",
      handle => "cfe_internal_setup_knowledge_files_doc_root_1",
      copy_from => no_backup_cp_compare("$(sys.workdir)/share/GUI", "binary"),
      depth_search => recurse("inf");

    any::

      "$(install_logs)" -> { "ENT-4506" }
        perms => mog("0600", "root", "root" );

      "$(cfe_internal_hub_vars.docroot)/." -> { "CFE-951" }
      comment => "The top level docroot needs to be readable and executable by the web server.",
      handle => "cfe_internal_setup_knowledge_dir_doc_root",
      perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(sys.workdir)/httpd/php/." -> { "ENT-9703", "cfe_internal_setup_knowledge_dir_doc_root_runalerts_stamp" }
        create => "true",
        comment => "php directory needs to be accessible (execute) to cfapache",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_php",
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(sys.workdir)/httpd/php/runalerts-stamp/." -> { "ENT-9703" }
        create => "true",
        comment => "runalerts-stamp directory needs to be accessible (execute) to cfapache",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_runalerts_stamp",
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/vendor/." -> { "CFE-951" }
        comment => "The vendor directory and sub-directories contains dependencies from the code ignitor framework and the directories need to be searchable by the web server.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_vendor_dirs",
        depth_search => recurse_with_base("inf"),
        file_select => dirs,
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/vendor/." -> { "CFE-951" }
        comment => "The files in the vendor directory contain dependencies from the code ignitor framework and need to be readable by the web server.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_vendor_not_dir",
        depth_search => recurse_with_base("inf"),
        file_select => not_dir,
        perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/themes/." -> { "CFE-951" }
        comment => "The public docroot themes directory needs to be searchable by the web server so that it can find css and images to make Mission Portal look as expected.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public_themes_dirs",
        depth_search => recurse_with_base("inf"),
        file_select => dirs,
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/themes/." -> { "CFE-951" }
        comment => "The public docroot themes directory needs to be searchable by the web server so that it can find css and images to make Mission Portal look as expected.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public_themes_not_dir",
        depth_search => recurse_with_base("inf"),
        file_select => not_dir,
        perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/."
        comment => "The public dir in the docroot needs the to be executable by the webserver",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public",
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/index.php"
        comment => "The public dir in the docroot needs the to be executable by the webserver",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public_index_php",
        perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/images/." -> { "CFE-951" }
        comment => "The public docroot images directory needs to be searchable by the webserver so that Mission Portal can load images and look as expected.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public_images_dirs",
        depth_search => recurse_with_base("inf"),
        file_select => dirs,
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/images/." -> { "CFE-951" }
        comment => "The public docroot images directory needs to be searchable by the webserver so that Mission Portal can load images and look as expected.",
        handle => "cfe_internal_setup_knowledge_dir_doc_root_public_images_not_dir",
        depth_search => recurse_with_base("inf"),
        file_select => not_dir,
        perms => mog("0440", "root", $(def.cf_apache_group) );


      "$(sys.workdir)/httpd/."
      comment => "httpd dir should be 755",
      handle => "cfe_internal_setup_knowledge_dir_httpd",
      perms => mog("755", "root", "root");

      "$(cfe_internal_hub_vars.docroot)/.htaccess"
      comment => "Correct up htaccess file in doc root",
      handle => "cfe_internal_setup_knowledge_files_doc_root_htaccess",
      copy_from => no_backup_cp("$(sys.workdir)/share/GUI/Apache-htaccess");

      "$(cfe_internal_hub_vars.public_docroot)/scripts/." -> { "CFE-951" }
      comment => "Ensure permissions for $(cfe_internal_hub_vars.public_docroot)/scripts",
      handle => "cfe_internal_setup_knowledge_files_doc_root_scripts_dir",
      create => "true",
      perms => mog("0570", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.public_docroot)/scripts/." -> { "CFE-951" }
        comment => "Ensure permissions for $(cfe_internal_hub_vars.public_docroot)/scripts",
        handle => "cfe_internal_setup_knowledge_files_doc_root_scripts_not_dir",
        create => "true",
        file_select => not_dir,
        depth_search => recurse_basedir("inf"),
        perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/static/." -> { "CFE-951" }
      handle => "cfe_internal_setup_knowledge_files_doc_root_static_dir",
      create => "true",
      perms => mog("0770", "root", $(def.cf_apache_group)),
      comment => "Ensure permissions for $(cfe_internal_hub_vars.docroot)/static.
                  This is where exported and scheduled reports generated by Mission Portal
                  (temp files to email)";

      "$(cfe_internal_hub_vars.docroot)/static/." -> { "CFE-951", "ENT-11941" }
        handle => "cfe_internal_setup_knowledge_files_doc_root_static_not_dir",
        create => "true",
        depth_search => recurse_basedir("inf"),
        file_select => not_dir,
        perms => mog("0600", "$(def.cf_apache_user)", "$(def.cf_apache_group)"),
        comment => "Ensure permissions for $(cfe_internal_hub_vars.docroot)/static/*.
 This is where exported and scheduled reports generated by Mission Portal (temp
 files to download or email)";

      "$(cfe_internal_hub_vars.public_docroot)/tmp/." -> { "CFE-951" }
      handle => "cfe_internal_setup_knowledge_files_public_doc_root_tmp_dir",
      create => "true",
      depth_search => recurse_basedir("inf"),
      file_select => dirs,
      perms => mog("0770", $(def.cf_apache_user), $(def.cf_apache_group)),
      comment => "Ensure permissions for $(cfe_internal_hub_vars.public_docroot)/tmp.
                  This is where css and js files generated by Mission Portal";

      "$(cfe_internal_hub_vars.public_docroot)/tmp/." -> { "CFE-951" }
        handle => "cfe_internal_setup_knowledge_files_public_doc_root_tmp_not_dir",
        create => "true",
        depth_search => recurse_basedir("inf"),
        file_select => not_dir,
        perms => mog("0440", $(def.cf_apache_user), $(def.cf_apache_group)),
        comment => "Ensure permissions for $(cfe_internal_hub_vars.public_docroot)/tmp.
                  This is where css and js files generated by Mission Portal";

      "$(cfe_internal_hub_vars.docroot)/application" -> { "CFE-951" }
      comment => "No one should be able to write to the application, and only
                  the webserver needs access",
      handle => "cfe_internal_setup_knowledge_files_all_not_dir_in_application",
      depth_search => cfe_internal_docroot_application_perms,
      file_select => not_dir,
      perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/application" -> { "CFE-951" }
        comment => "No one should be able to write to the application, and only
                  the webserver needs access",
        handle => "cfe_internal_setup_knowledge_files_all_dirs_in_application",
        depth_search => cfe_internal_docroot_application_perms,
        file_select => dirs,
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/application/cache/." -> { "ENT-11442" }
        handle => "cfe_internal_setup_knowledge_files_doc_root_application_cache",
        create => "true",
        perms => mog("0770", $(def.cf_apache_user), $(def.cf_apache_group)),
        comment => "This is where cache generated by Mission Portal is stored.";

      "$(cfe_internal_hub_vars.docroot)/api/." -> { "ENT-4250" }
        comment => "The api directory and it's subdirectories need to be
                    executable by cfapache",
        perms => mog("0550", "root", $(def.cf_apache_group) );


      "$(cfe_internal_hub_vars.docroot)/api/." -> { "ENT-4250", "CFE-951" }
        depth_search => recurse_ignore( "inf", "static" ),
        file_select => dirs,
        comment => "The api subdirectories need to be executable by cfapache,
                    but ignore static here, it needs to be writeable as well,
                    well take care of it separately",
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/ldap/." -> { "ENT-9693" }
        comment => concat( "The ldap directory and it's subdirectories need to be",
                    "executable by cfapache" ),
        depth_search => recurse( "inf" ),
        file_select => cfe_internal_docroot_perms,
        perms => mog("0550", "root", $(def.cf_apache_group) );


      "$(cfe_internal_hub_vars.docroot)/ldap/." -> { "ENT-9693" }
        depth_search => recurse( "inf" ),
        file_select => dirs,
        comment => concat( "The ldap subdirectories need to be executable by cfapache",
                    "but ignore static here, it needs to be writeable as well,",
                    "well take care of it separately" ),
        perms => mog("0550", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/api/." -> { "CFE-951" }
      depth_search => recurse_basedir("inf"),
      handle => "cfe_internal_setup_knowledge_files_doc_root_api",
      file_select => cfe_internal_exclude_sh_pl_scripts,
      perms => mog("0440", "root", $(def.cf_apache_group) ),
      comment => "No one should be able to write to the application config code,
                  and only the webserver needs access to read the config.";

      "$(cfe_internal_hub_vars.docroot)/api/."
      depth_search => recurse_basedir("inf"),
      handle => "cfe_internal_setup_knowledge_files_doc_root_api_scripts",
      file_select => cfe_internal_sh_pl_scripts,
      perms => mog("0550", "root", $(def.cf_apache_group) ),
      comment => "The scripts need to be executable, but only by the
                  webserver and root users.";

      "$(cfe_internal_hub_vars.docroot)/api/static/."
      perms => mog("0770", "root", $(def.cf_apache_group) ),
      comment => "This is where exported PDF and CSV reports from Mission
                  Portal are written, it be writeable by the webserver";

      "$(cfe_internal_hub_vars.docroot)/api/static/." -> { "CFE-951" }
        comment => "Exported reports only need to be readable by the webserver.",
        handle => "cfe_internal_setup_knowledge_files_doc_root_api_static_not_dir",
        depth_search => recurse("inf"),
        file_select => cfe_internal_docroot_api_static_perms,
        perms => mog("0440", "root", $(def.cf_apache_group) );

      "$(cfe_internal_hub_vars.docroot)/api/static/." -> { "ENT-4551", "CFE-951" }
        comment => ".status, .pid, and potentially .abort files need to be writeable so that the async query API will function properly",
        handle => "cfe_internal_setup_knowledge_files_doc_root_api_static_async_query_status",
        depth_search => recurse("inf"),
        file_select => cfe_internal_docroot_api_static_async_query_status_status_perms,
        perms => mog("0660", "root", $(def.cf_apache_group) );

      "$(sys.workdir)/httpd/logs/application/." -> { "ENT-7731", "ENT-2758", "ENT-8908", "CFE-951" }
        comment => "Ensure permissions for $(sys.workdir)/httpd/logs/application/.",
        handle => "cfe_internal_setup_knowledge_files_httpd_application_log_dir",
        create => "true",
        perms => mog("0750", $(def.cf_apache_user), $(def.cf_apache_group));

      "$(sys.workdir)/httpd/logs/application/." -> { "ENT-7730" }
        comment => "Ensure permissions for $(sys.workdir)/httpd/logs/application/.*",
        handle => "cfe_internal_setup_knowledge_files_httpd_application_log_files",
        file_select => plain,
        depth_search => recurse( "inf" ),
        perms => mog("0600", $(def.cf_apache_user), $(def.cf_apache_group));

      "$(sys.workdir)/httpd/logs/." -> { "CFE-951" }
      comment => "Ensure permissions for $(sys.workdir)/httpd/logs",
      handle => "cfe_internal_setup_knowledge_files_httpd_logs_dir",
      create => "true",
      perms => mog("0750", $(def.cf_apache_user), $(def.cf_apache_group));

      "$(sys.workdir)/httpd/logs/." -> { "ENT-7730" }
        comment => "Ensure permissions for $(sys.workdir)/httpd/logs",
        handle => "cfe_internal_setup_knowledge_files_httpd_log_files",
        file_select => plain,
        depth_search => recurse_with_base( "0" ),
        perms => mog("0600", root, root);

      "/var/log/postgresql.log" -> { "ENT-7961" }
        comment => "Ensure permissions for PostgreSQL log",
        handle => "cfe_internal_setup_knowledge_files_postgresql_log_file",
        perms => mog("0600", "cfpostgres", "cfpostgres");

      "$(cfe_internal_hub_vars.docroot)/../ssl/."
        perms => mog("0440", "root", "root" ),
        if => not( "ENT_3572" );

      "$(cfe_internal_hub_vars.docroot)/../ssl/." -> { "ENT-3572" }
        perms => mog("0444", "root", "root" ),
        if => "ENT_3572",
        comment => "Exported be 0 bytes in some versions if the ssl directory is
                    not accessible to all users.";

      "$(cfe_internal_hub_vars.docroot)/../ssl/private/."
        depth_search => recurse_with_base("inf"),
        perms => mog("0440", "root", "root"),
        comment => "Private keys are secrets and should not be accessible by
                    anyone other than root.";

      "$(cfe_internal_hub_vars.docroot)/../ssl/csr/."
        depth_search => recurse_with_base("inf"),
        perms => mog("0440", "root", "root"),
        comment => "Certificate signing requests, while not secrets do not need to
                    be readable by others.";

      "$(cfe_internal_hub_vars.docroot)/../ssl/certs/." -> { "ENT-3050", "Mission Portal" }
        depth_search => recurse_with_base("inf"),
        perms => mog("0444", "root", "root"),
        comment => "Certificates need to be read by any user wishing to validate
                    a request. For example Mission Portals api.";


      "$(cfe_internal_hub_vars.docroot)/." -> { "CFE-951"}
        depth_search => recurse_basedir("inf"),
        handle => "cfe_internal_setup_knowledge_files_doc_root_htaccess_perms",
        file_select => cfe_internal_htaccess,
        perms => mog("0440", "root", $(def.cf_apache_group) ),
        comment => ".htaccess files should only be readable by webserver.";

      "$(cfe_internal_hub_vars.docroot)/ldap/config/settings.ldap.php" -> { "ENT-3400" }
        handle => "cfe_internal_setup_knowledge_files_ldap_config_settings_perms",
        perms => mog("0600", $(def.cf_apache_user), $(def.cf_apache_group) ),
        if => fileexists( "$(cfe_internal_hub_vars.docroot)/ldap/config/settings.ldap.php" ),
        comment => "If the ldap settings are not writeable by
                    $(def.cf_apache_user) then users will not be able to change
                    ldap settings.";


      "$(sys.workdir)/share/GUI/."
        perms => mog("0400", "root", "root" ),
        depth_search => recurse_basedir("inf"),
        comment => "No Mission Portal code in share needs to be accessed by
                    anyone";

      "$(sys.workdir)/." -> { "ENT-3299" }
        perms => mog("755", "root", "root"),
        comment => "Ensure that others (like cfpostgres and cfapache) are able
                    to enter and read from cfengines workdir";

      "$(sys.workdir)/modules/."
        perms => mog("755", "root", "root"),
        comment => "The agent will complain if any other users (group or other)
                    have write access to the modules directory.";

      "/opt/cfengine/notification_scripts/." -> { "ENT-5070" }
        create => "true",
        perms => mog("770", "root", $(def.cf_apache_group) ),
        comment => "If this directory is not present and writable by the
                    web-server, then Mission Portal users will be unable to
                    upload custom action scripts.";

}

bundle agent cfe_internal_permissions
# @brief Specific expectations for permissions and ownership of CFEngine with respect to the Enterprise Edition
{

  vars:

    # nonstandard directories in statedir
    "_statedir_standard_perm_exceptions"
      slist => { "pg", "cf-execd.sockets" };

    # Derive the users that should be able to access cf-execd sockets
    "_cf_execd_socket_allow_users"
      slist => { @(def.control_executor_runagent_socket_allow_users) };

    "_cf_statedir_allow_users"
      slist => { "cfpostgres", "cfapache", @(def.control_executor_runagent_socket_allow_users) },
      if => "enterprise_edition.(policy_server|am_policy_hub)";

    # Derive the aces for users allowed to access cf-execd sockets
    "_cf_execd_socket_dir_user_aces" -> { "ENT-6777" }
      slist => maplist( "user:$(this):rx:allow", @(_cf_execd_socket_allow_users) );
    "_cf_execd_socket_runagent_user_aces" -> { "ENT-6777" }
      slist => maplist( "user:$(this):rw:allow", @(_cf_execd_socket_allow_users) );
    "_cf_statedir_user_aces" -> { "ENT-6777" }
      slist => maplist( "user:$(this):rx:allow", @(_cf_statedir_allow_users) );

  files:

    !(policy_server|am_policy_hub)::
      "$(sys.statedir)/." -> { "ENT-4773", "CFE-951" }
        handle => "state_dir_not_dir_perms",
        perms => state_dir_system_owned_files(),
        # Important to recurse across file system boundaries, as databases and or state are commonly on different filesystems
        depth_search => recurse_with_base( inf ),
        file_select => not_dir;

      "$(sys.statedir)/." -> { "ENT-4773", "CFE-951" }
        handle => "state_dir_dirs_perms",
        perms => state_dir_system_owned_dirs(),
        # Important to recurse across file system boundaries, as databases and or state are commonly on different filesystems
        depth_search => recurse_with_base( inf ),
        file_select => dirs;

    enterprise_edition.(policy_server|am_policy_hub)::

      "$(sys.statedir)/." -> { "CFE-951" }
        perms => mog("0750", "root", "cfpostgres"),
        acl => cf_statedir_acl( @(_cf_statedir_user_aces) ),
        comment => "The database user must be able to read the parent directory of the database or it won't be accessible";

     "$(sys.statedir)/." -> { "CFE-951" }
        perms => state_dir_system_owned_files(),
        depth_search => recurse_except( inf, @(_statedir_standard_perm_exceptions) ),
        file_select => not_dir,
        comment => "The database user must be able to read the parent directory of the database or it won't be accessible";

      "$(sys.statedir)/." -> { "CFE-951" }
        perms => state_dir_system_owned_dirs(),
        depth_search => recurse_except( inf, @(_statedir_standard_perm_exceptions) ),
        file_select => dirs,
        comment => "The database user must be able to read the parent directory of the database or it won't be accessible";

      "$(sys.statedir)/pg/." -> { "CFE-951" }
        perms => mog("0600", "cfpostgres", "cfpostgres"),
        depth_search => recurse_with_base( inf ),
        file_select => not_dir,
        comment => "No one except for the database user needs to access where the db is installed.";

      "$(sys.statedir)/pg/." -> { "CFE-951" }
        perms => mog("0700", "cfpostgres", "cfpostgres"),
        depth_search => recurse_with_base( inf ),
        file_select => dirs,
        comment => "No one except for the database user needs to access where the db is installed.";

      "$(sys.statedir)/cf-execd.sockets/." -> { "ENT-6777" }
        acl => cf_execd_socket_dir_acl( @(_cf_execd_socket_dir_user_aces) ),
        if => isdir( "$(sys.statedir)/cf-execd.sockets" );

      "$(sys.statedir)/cf-execd.sockets/runagent.socket" -> { "ENT-6777" }
        acl => cf_execd_socket_runagent_acl( @(_cf_execd_socket_runagent_user_aces) ),
        if => fileexists( "$(sys.statedir)/cf-execd.sockets/runagent.socket" );
}


body acl cf_statedir_acl( aces )
# @brief Describe ACL for state directory (sys.statedir)
# @param aces A list of additional access control rules that should be used
#
# * User is allowed read, write, execute
# * Group is allowed read, execute
# * Permissions described by @(aces)
# * Other is allowed nothing
#
# **Example:**
#
# ```
# bundle agent example
# {
#   vars:
#     "_cf_statedir_allow_users"
#       slist => { "root", "cfpostgres", "cfapache" };
#     "_cf_statedir_user_aces"
#       slist => maplist( "user:$(this):rx:allow", @(_cf_statedir_allow_users) );
#   files:
#     "$(sys.statedir)/."
#       acl => cf_statedir_aces( @(_cf_statedir_user_aces) );
# }
# ```
#
{
        acl_method => "overwrite";
        acl_type => "posix";
        aces => { "user:*:rwx:allow",
                  "group:*:rx:allow",
                  @(aces),
                  "all:---:allow"
        };
}
body acl cf_execd_socket_dir_acl( aces )
# @brief Describe ACL for cf-execd socket directory
# @param aces A list of additional access control rules that should be used
#
# * Owner is allowed read, write, execute
# * Group is allowed nothing
# * Permissions described by @(aces)
# * Other is allowed nothing
#
# **Example:**
#
# ```
# bundle agent example
# {
#   vars:
#     "_cf_execd_socket_allow_users"
#       slist => { "cfapache" };
#     "_cf_execd_socket_dir_user_aces"
#       slist => maplist( "user:$(this):rx:allow", @(_cf_execd_socket_allow_users) );
#   files:
#     "$(sys.statedir)/cf-execd.sockets/runagent.socket"
#       acl => cf_execd_socket_dir_aces( @(_cf_execd_socket_runagent_dir_aces) );
# }
# ```
#
{
  acl_method => "overwrite";
  acl_type => "posix";
  aces => { "user:*:rwx:allow",
            "group:*:---:allow",
            @(aces),
            "all:---:allow"
          };
}

body acl cf_execd_socket_runagent_acl( aces )
# @brief Describe ACL for cf-execd runagent socket
# @param aces A list of additional access control rules that should be used
#
# * Owner is allowed read, write
# * Group is allowed nothing
# * Permissions described by @(aces)
# * Other is allowed nothing
#
# **Example:**
#
# ```
# bundle agent example
# {
#   vars:
#     "_cf_execd_socket_allow_users"
#       slist => { "cfapache" };
#     "_cf_execd_socket_runagent_user_aces"
#       slist => maplist( "user:$(this):rx:allow", @(_cf_execd_socket_allow_users) );
#   files:
#     "$(sys.statedir)/cf-execd.sockets/runagent.socket"
#       acl => cf_execd_socket_runagent_acl( @(_cf_execd_socket_dir_user_aces) );
# }
# ```
#
{
  acl_method => "overwrite";
  acl_type => "posix";
  aces => { "user:*:rw:allow",
            "group:*:---:allow",
            @(aces),
            "all:---:allow"
          };
}

#############################################################################
body depth_search recurse_except( d, exceptions)
# @breif Recurse (across device boundaires) `d` levels (excluding basedir) excluding `exceptions`
# @param `d` Levels to decend in recursion
# @param `exceptions` List of directories to exclude from depth search
{
        depth => "$(d)";
        include_basedir => "false";
        exclude_dirs => { @(exceptions) };
}

############################################################################

body depth_search recurse_basedir(d)
# @brief Search `d` levels deep including the base dir
# @param `d` Levels to decend in recursion
{
      depth => "$(d)";
      include_basedir => "true";
}

###########################################################################

body depth_search recurse_basedir_exclude(d)
# @brief Search `d` levels deep including the base dir but exclude some specific dirs
{
      depth => "$(d)";
      include_basedir => "true";
      exclude_dirs => { "static" };
}

############################################################################

body file_select cfe_internal_docroot_perms
# @brief Select files (not dirs) not named `.htaccess` or `settings.ldap.php`
{
  leaf_name => { "\.htaccess", "settings.ldap.php", "ha_enabled" };
  path_name => { "$(cfe_internal_hub_vars.docroot)/vendor/.*"};
  file_types => { "dir" };
      # htaccess are going the way of the dodo bird
      # settings.ldap.php permissions are handled explicitly in it's own bundle
      # ha_enabled permissions is handled explicitly by cfengine_enterprise_ha_enabled_semaphore_present
  file_result => "!leaf_name.!file_types.!path_name";
}

############################################################################
body depth_search recurse_exclude(d)
# @brief Search to a depth of `d` excluding known directories
{
      depth => "$(d)";
      exclude_dirs => { "hub" , "graphs", "scripts", "tmp", "static", "logs", "api", "rest", "application", "dc-scripts" };
}

############################################################################

body file_select cfe_internal_exclude_sh_pl_scripts
# @brief select plain files not ending in `.sh` and `.pl`
{
  leaf_name => { ".*\.sh",".*\.pl"};
  file_types => { "plain" };
  file_result => "!leaf_name.file_types";
}

############################################################################

body file_select cfe_internal_sh_pl_scripts
# @brief select plain files ending in `.sh` and `.pl`
{
  leaf_name => { ".*\.sh",".*\.pl" };
  file_types => { "plain" };
  file_result => "leaf_name.file_types";
}

############################################################################

body file_select cfe_internal_htaccess
# @brief select files named `.htaccess`
{
  leaf_name => { "\.htaccess" };
  file_types => { "dir" };
  file_result => "leaf_name.!file_types";
}

############################################################################

body file_select cfe_internal_exclude_index_html
{
  leaf_name => { "index.html" };
  file_result => "!leaf_name";
}

############################################################################

body file_select cfe_internal_docroot_api_static_perms
{
      # ENT-4551 - .status, .pid, and potentially .abort files used by async
      # query mechanism need to be writeable by the webserver, we exclude those
      # files here to avoid continual promise repair.

  leaf_name => { "\.htaccess", "\.status", "\.pid", "\.abort" };
  file_types => { "dir" };
  file_result => "!leaf_name.!file_types";
}
############################################################################

body file_select cfe_internal_docroot_api_static_async_query_status_status_perms
# @brief .status, .pid and .abort files are used by the asynchronous query API and need to be writeable
{
      # ENT-4551 - .status, .pid, and potentially .abort files used by async
      # query mechanism need to be writeable by the webserver

        leaf_name => { "\.status", "\.pid", "\.abort" };
        file_types => { "dir" };
        file_result => "leaf_name.!file_types";
}

############################################################################

body depth_search cfe_internal_docroot_application_perms
{
      depth => "inf";
      exclude_dirs => { "logs", "cache" };
}

############################################################################

body perms state_dir_system_owned_files
{
#+begin_ENT-951
# Remove after 3.20 is not supported
        rxdirs => "true";
@if minimum_version(3.20)
        rxdirs => "false";
@endif
#+end

      mode   => "0600";
      !windows::
        owners => { "root" };

      freebsd|openbsd|netbsd|darwin::
        groups => { "wheel" };

      aix::
        groups => { "system" };

      hpux::
        groups => { "sys" };

      !(freebsd|openbsd|netbsd|darwin|aix|hpux)::
        groups => { "root" };
}
body perms state_dir_system_owned_dirs
{
  inherit_from => state_dir_system_owned_files;
  mode => "0700";
}