| 878 | */ |
| 879 | PG_FUNCTION_INFO_V1(sepgsql_restorecon); |
| 880 | Datum |
| 881 | sepgsql_restorecon(PG_FUNCTION_ARGS) |
| 882 | { |
| 883 | struct selabel_handle *sehnd; |
| 884 | struct selinux_opt seopts; |
| 885 | |
| 886 | /* |
| 887 | * SELinux has to be enabled on the running platform. |
| 888 | */ |
| 889 | if (!sepgsql_is_enabled()) |
| 890 | ereport(ERROR, |
| 891 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 892 | errmsg("sepgsql is not currently enabled"))); |
| 893 | |
| 894 | /* |
| 895 | * Check DAC permission. Only superuser can set up initial security |
| 896 | * labels, like root-user in filesystems |
| 897 | */ |
| 898 | if (!superuser()) |
| 899 | ereport(ERROR, |
| 900 | (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 901 | errmsg("SELinux: must be superuser to restore initial contexts"))); |
| 902 | |
| 903 | /* |
| 904 | * Open selabel_lookup(3) stuff. It provides a set of mapping between an |
| 905 | * initial security label and object class/name due to the system setting. |
| 906 | */ |
| 907 | if (PG_ARGISNULL(0)) |
| 908 | { |
| 909 | seopts.type = SELABEL_OPT_UNUSED; |
| 910 | seopts.value = NULL; |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | seopts.type = SELABEL_OPT_PATH; |
| 915 | seopts.value = TextDatumGetCString(PG_GETARG_DATUM(0)); |
| 916 | } |
| 917 | sehnd = selabel_open(SELABEL_CTX_DB, &seopts, 1); |
| 918 | if (!sehnd) |
| 919 | ereport(ERROR, |
| 920 | (errcode(ERRCODE_INTERNAL_ERROR), |
| 921 | errmsg("SELinux: failed to initialize labeling handle: %m"))); |
| 922 | PG_TRY(); |
| 923 | { |
| 924 | exec_object_restorecon(sehnd, DatabaseRelationId); |
| 925 | exec_object_restorecon(sehnd, NamespaceRelationId); |
| 926 | exec_object_restorecon(sehnd, RelationRelationId); |
| 927 | exec_object_restorecon(sehnd, AttributeRelationId); |
| 928 | exec_object_restorecon(sehnd, ProcedureRelationId); |
| 929 | } |
| 930 | PG_FINALLY(); |
| 931 | { |
| 932 | selabel_close(sehnd); |
| 933 | } |
| 934 | PG_END_TRY(); |
| 935 | |
| 936 | PG_RETURN_BOOL(true); |
| 937 | } |
nothing calls this directly
no test coverage detected