|
1 | 1 | use std::ffi::{c_char, CStr}; |
2 | 2 |
|
3 | | -use pgrx::pg_sys::{AsPgCStr, List, Node, QueryEnvironment, RawStmt}; |
| 3 | +use pgrx::{ |
| 4 | + pg_sys::{ |
| 5 | + bms_add_member, AsPgCStr, CopyGetAttnums, CopyStmt, FirstLowInvalidHeapAttributeNumber, |
| 6 | + List, Node, ParseNamespaceItem, ParseState, PlannedStmt, QueryEnvironment, RawStmt, |
| 7 | + ACL_INSERT, ACL_SELECT, |
| 8 | + }, |
| 9 | + PgBox, PgList, PgRelation, |
| 10 | +}; |
4 | 11 |
|
5 | 12 | pub(crate) fn pg_analyze_and_rewrite( |
6 | 13 | raw_stmt: *mut RawStmt, |
@@ -65,3 +72,89 @@ pub(crate) fn MarkGUCPrefixReserved(guc_prefix: &str) { |
65 | 72 | pgrx::pg_sys::MarkGUCPrefixReserved(guc_prefix.as_pg_cstr()) |
66 | 73 | } |
67 | 74 | } |
| 75 | + |
| 76 | +/// check_copy_table_permission checks if the user has permission to copy from/to the table. |
| 77 | +/// This is taken from the original PostgreSQL DoCopy function. |
| 78 | +#[cfg(any(feature = "pg16", feature = "pg17"))] |
| 79 | +pub(crate) fn check_copy_table_permission( |
| 80 | + p_stmt: &PgBox<PlannedStmt>, |
| 81 | + p_state: &PgBox<ParseState>, |
| 82 | + ns_item: &PgBox<ParseNamespaceItem>, |
| 83 | + relation: &PgRelation, |
| 84 | +) { |
| 85 | + let copy_stmt = unsafe { PgBox::<CopyStmt>::from_pg(p_stmt.utilityStmt as _) }; |
| 86 | + |
| 87 | + // init permissions |
| 88 | + let mut perminfo = |
| 89 | + unsafe { PgBox::<pgrx::pg_sys::RTEPermissionInfo>::from_pg(ns_item.p_perminfo) }; |
| 90 | + |
| 91 | + // set table access mode |
| 92 | + perminfo.requiredPerms = if copy_stmt.is_from { |
| 93 | + ACL_INSERT as _ |
| 94 | + } else { |
| 95 | + ACL_SELECT as _ |
| 96 | + }; |
| 97 | + |
| 98 | + // set column access modes |
| 99 | + let tup_desc = relation.tuple_desc(); |
| 100 | + |
| 101 | + let attnums = |
| 102 | + unsafe { CopyGetAttnums(tup_desc.as_ptr(), relation.as_ptr(), copy_stmt.attlist) }; |
| 103 | + let attnums = unsafe { PgList::<i16>::from_pg(attnums) }; |
| 104 | + |
| 105 | + for attnum in attnums.iter_int() { |
| 106 | + let attno = attnum - FirstLowInvalidHeapAttributeNumber; |
| 107 | + |
| 108 | + if copy_stmt.is_from { |
| 109 | + unsafe { perminfo.insertedCols = bms_add_member(perminfo.insertedCols, attno) }; |
| 110 | + } else { |
| 111 | + unsafe { perminfo.selectedCols = bms_add_member(perminfo.selectedCols, attno) }; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // check permissions |
| 116 | + let mut perm_infos = PgList::<pgrx::pg_sys::RTEPermissionInfo>::new(); |
| 117 | + perm_infos.push(perminfo.as_ptr()); |
| 118 | + |
| 119 | + unsafe { pgrx::pg_sys::ExecCheckPermissions(p_state.p_rtable, perm_infos.as_ptr(), true) }; |
| 120 | +} |
| 121 | + |
| 122 | +#[cfg(any(feature = "pg14", feature = "pg15"))] |
| 123 | +pub(crate) fn check_copy_table_permission( |
| 124 | + p_stmt: &PgBox<PlannedStmt>, |
| 125 | + p_state: &PgBox<ParseState>, |
| 126 | + ns_item: &PgBox<ParseNamespaceItem>, |
| 127 | + relation: &PgRelation, |
| 128 | +) { |
| 129 | + let copy_stmt = unsafe { PgBox::<CopyStmt>::from_pg(p_stmt.utilityStmt as _) }; |
| 130 | + |
| 131 | + // init rte |
| 132 | + let mut rte = unsafe { PgBox::<pgrx::pg_sys::RangeTblEntry>::from_pg(ns_item.p_rte) }; |
| 133 | + |
| 134 | + // set table access mode |
| 135 | + rte.requiredPerms = if copy_stmt.is_from { |
| 136 | + ACL_INSERT as _ |
| 137 | + } else { |
| 138 | + ACL_SELECT as _ |
| 139 | + }; |
| 140 | + |
| 141 | + // set column access modes |
| 142 | + let tup_desc = relation.tuple_desc(); |
| 143 | + |
| 144 | + let attnums = |
| 145 | + unsafe { CopyGetAttnums(tup_desc.as_ptr(), relation.as_ptr(), copy_stmt.attlist) }; |
| 146 | + let attnums = unsafe { PgList::<i16>::from_pg(attnums) }; |
| 147 | + |
| 148 | + for attnum in attnums.iter_int() { |
| 149 | + let attno = attnum - FirstLowInvalidHeapAttributeNumber; |
| 150 | + |
| 151 | + if copy_stmt.is_from { |
| 152 | + unsafe { rte.insertedCols = bms_add_member(rte.insertedCols, attno) }; |
| 153 | + } else { |
| 154 | + unsafe { rte.selectedCols = bms_add_member(rte.selectedCols, attno) }; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // check permissions |
| 159 | + unsafe { pgrx::pg_sys::ExecCheckRTPerms(p_state.p_rtable, true) }; |
| 160 | +} |
0 commit comments