|
6 | 6 | *
|
7 | 7 | *
|
8 | 8 | * IDENTIFICATION
|
9 |
| - * $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.10 1998/06/15 19:29:06 momjian Exp $ |
| 9 | + * $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.11 1998/08/24 01:37:56 momjian Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
|
18 | 18 | #include "utils/syscache.h" /* for SearchSysCache */
|
19 | 19 | #include "rewrite/locks.h" /* for rewrite specific lock defns */
|
20 | 20 |
|
| 21 | +#include "access/heapam.h" /* for ACL checking */ |
| 22 | +#include "utils/syscache.h" |
| 23 | +#include "utils/acl.h" |
| 24 | +#include "utils/builtins.h" |
| 25 | +#include "catalog/pg_shadow.h" |
| 26 | + |
| 27 | +static void checkLockPerms(List *locks, Query *parsetree, int rt_index); |
| 28 | + |
21 | 29 | /*
|
22 | 30 | * ThisLockWasTriggered
|
23 | 31 | *
|
@@ -156,5 +164,98 @@ matchLocks(CmdType event,
|
156 | 164 | }
|
157 | 165 | }
|
158 | 166 |
|
| 167 | + checkLockPerms(real_locks, parsetree, varno); |
| 168 | + |
159 | 169 | return (real_locks);
|
160 | 170 | }
|
| 171 | + |
| 172 | + |
| 173 | +static void |
| 174 | +checkLockPerms(List *locks, Query *parsetree, int rt_index) |
| 175 | +{ |
| 176 | + Relation ev_rel; |
| 177 | + HeapTuple usertup; |
| 178 | + char *evowner; |
| 179 | + RangeTblEntry *rte; |
| 180 | + int32 reqperm; |
| 181 | + int32 aclcheck_res; |
| 182 | + int i; |
| 183 | + List *l; |
| 184 | + |
| 185 | + if (locks == NIL) |
| 186 | + return; |
| 187 | + |
| 188 | + /* |
| 189 | + * Get the usename of the rules event relation owner |
| 190 | + */ |
| 191 | + rte = (RangeTblEntry *)nth(rt_index - 1, parsetree->rtable); |
| 192 | + ev_rel = heap_openr(rte->relname); |
| 193 | + usertup = SearchSysCacheTuple(USESYSID, |
| 194 | + ObjectIdGetDatum(ev_rel->rd_rel->relowner), |
| 195 | + 0, 0, 0); |
| 196 | + if (!HeapTupleIsValid(usertup)) |
| 197 | + { |
| 198 | + elog(ERROR, "cache lookup for userid %d failed", |
| 199 | + ev_rel->rd_rel->relowner); |
| 200 | + } |
| 201 | + heap_close(ev_rel); |
| 202 | + evowner = nameout(&(((Form_pg_shadow) GETSTRUCT(usertup))->usename)); |
| 203 | + |
| 204 | + /* |
| 205 | + * Check all the locks, that should get fired on this query |
| 206 | + */ |
| 207 | + foreach (l, locks) { |
| 208 | + RewriteRule *onelock = (RewriteRule *)lfirst(l); |
| 209 | + List *action; |
| 210 | + |
| 211 | + /* |
| 212 | + * In each lock check every action |
| 213 | + */ |
| 214 | + foreach (action, onelock->actions) { |
| 215 | + Query *query = (Query *)lfirst(action); |
| 216 | + |
| 217 | + /* |
| 218 | + * In each action check every rangetable entry |
| 219 | + * for read/write permission of the event relations |
| 220 | + * owner depending on if it's the result relation |
| 221 | + * (write) or not (read) |
| 222 | + */ |
| 223 | + for (i = 2; i < length(query->rtable); i++) { |
| 224 | + if (i + 1 == query->resultRelation) |
| 225 | + switch (query->resultRelation) { |
| 226 | + case CMD_INSERT: |
| 227 | + reqperm = ACL_AP; |
| 228 | + break; |
| 229 | + default: |
| 230 | + reqperm = ACL_WR; |
| 231 | + break; |
| 232 | + } |
| 233 | + else |
| 234 | + reqperm = ACL_RD; |
| 235 | + |
| 236 | + rte = (RangeTblEntry *)nth(i, query->rtable); |
| 237 | + aclcheck_res = pg_aclcheck(rte->relname, |
| 238 | + evowner, reqperm); |
| 239 | + if (aclcheck_res != ACLCHECK_OK) { |
| 240 | + elog(ERROR, "%s: %s", |
| 241 | + rte->relname, |
| 242 | + aclcheck_error_strings[aclcheck_res]); |
| 243 | + } |
| 244 | + |
| 245 | + /* |
| 246 | + * So this is allowed due to the permissions |
| 247 | + * of the rules event relation owner. But |
| 248 | + * let's see if the next one too |
| 249 | + */ |
| 250 | + rte->skipAcl = TRUE; |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + /* |
| 256 | + * Phew, that was close |
| 257 | + */ |
| 258 | + return; |
| 259 | +} |
| 260 | + |
| 261 | + |
0 commit comments