|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.106 2004/06/18 06:13:49 tgl Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.107 2004/07/12 20:23:50 momjian Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
|
21 | 21 | #include"catalog/pg_shadow.h"
|
22 | 22 | #include"catalog/pg_type.h"
|
23 | 23 | #include"commands/dbcommands.h"
|
| 24 | +#include"commands/tablespace.h" |
24 | 25 | #include"miscadmin.h"
|
25 | 26 | #include"utils/acl.h"
|
26 | 27 | #include"utils/builtins.h"
|
@@ -54,6 +55,8 @@ static Oidconvert_language_name(text *languagename);
|
54 | 55 | staticAclModeconvert_language_priv_string(text*priv_type_text);
|
55 | 56 | staticOidconvert_schema_name(text*schemaname);
|
56 | 57 | staticAclModeconvert_schema_priv_string(text*priv_type_text);
|
| 58 | +staticOidconvert_tablespace_name(text*tablespacename); |
| 59 | +staticAclModeconvert_tablespace_priv_string(text*priv_type_text); |
57 | 60 |
|
58 | 61 |
|
59 | 62 | /*
|
@@ -2207,3 +2210,204 @@ convert_schema_priv_string(text *priv_type_text)
|
2207 | 2210 | errmsg("unrecognized privilege type: \"%s\"",priv_type)));
|
2208 | 2211 | returnACL_NO_RIGHTS;/* keep compiler quiet */
|
2209 | 2212 | }
|
| 2213 | + |
| 2214 | +/* |
| 2215 | + * has_tablespace_privilege variants |
| 2216 | + *These are all named "has_tablespace_privilege" at the SQL level. |
| 2217 | + *They take various combinations of tablespace name, tablespace OID, |
| 2218 | + *user name, user sysid, or implicit user = current_user. |
| 2219 | + * |
| 2220 | + *The result is a boolean value: true if user has the indicated |
| 2221 | + *privilege, false if not. |
| 2222 | + */ |
| 2223 | + |
| 2224 | +/* |
| 2225 | + * has_tablespace_privilege_name_name |
| 2226 | + *Check user privileges on a tablespace given |
| 2227 | + *name username, text tablespacename, and text priv name. |
| 2228 | + */ |
| 2229 | +Datum |
| 2230 | +has_tablespace_privilege_name_name(PG_FUNCTION_ARGS) |
| 2231 | +{ |
| 2232 | +Nameusername=PG_GETARG_NAME(0); |
| 2233 | +text*tablespacename=PG_GETARG_TEXT_P(1); |
| 2234 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 2235 | +int32usesysid; |
| 2236 | +Oidtablespaceoid; |
| 2237 | +AclModemode; |
| 2238 | +AclResultaclresult; |
| 2239 | + |
| 2240 | +usesysid=get_usesysid(NameStr(*username)); |
| 2241 | +tablespaceoid=convert_tablespace_name(tablespacename); |
| 2242 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2243 | + |
| 2244 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2245 | + |
| 2246 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2247 | +} |
| 2248 | + |
| 2249 | +/* |
| 2250 | + * has_tablespace_privilege_name |
| 2251 | + *Check user privileges on a tablespace given |
| 2252 | + *text tablespacename and text priv name. |
| 2253 | + *current_user is assumed |
| 2254 | + */ |
| 2255 | +Datum |
| 2256 | +has_tablespace_privilege_name(PG_FUNCTION_ARGS) |
| 2257 | +{ |
| 2258 | +text*tablespacename=PG_GETARG_TEXT_P(0); |
| 2259 | +text*priv_type_text=PG_GETARG_TEXT_P(1); |
| 2260 | +AclIdusesysid; |
| 2261 | +Oidtablespaceoid; |
| 2262 | +AclModemode; |
| 2263 | +AclResultaclresult; |
| 2264 | + |
| 2265 | +usesysid=GetUserId(); |
| 2266 | +tablespaceoid=convert_tablespace_name(tablespacename); |
| 2267 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2268 | + |
| 2269 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2270 | + |
| 2271 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2272 | +} |
| 2273 | + |
| 2274 | +/* |
| 2275 | + * has_tablespace_privilege_name_id |
| 2276 | + *Check user privileges on a tablespace given |
| 2277 | + *name usename, tablespace oid, and text priv name. |
| 2278 | + */ |
| 2279 | +Datum |
| 2280 | +has_tablespace_privilege_name_id(PG_FUNCTION_ARGS) |
| 2281 | +{ |
| 2282 | +Nameusername=PG_GETARG_NAME(0); |
| 2283 | +Oidtablespaceoid=PG_GETARG_OID(1); |
| 2284 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 2285 | +int32usesysid; |
| 2286 | +AclModemode; |
| 2287 | +AclResultaclresult; |
| 2288 | + |
| 2289 | +usesysid=get_usesysid(NameStr(*username)); |
| 2290 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2291 | + |
| 2292 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2293 | + |
| 2294 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2295 | +} |
| 2296 | + |
| 2297 | +/* |
| 2298 | + * has_tablespace_privilege_id |
| 2299 | + *Check user privileges on a tablespace given |
| 2300 | + *tablespace oid, and text priv name. |
| 2301 | + *current_user is assumed |
| 2302 | + */ |
| 2303 | +Datum |
| 2304 | +has_tablespace_privilege_id(PG_FUNCTION_ARGS) |
| 2305 | +{ |
| 2306 | +Oidtablespaceoid=PG_GETARG_OID(0); |
| 2307 | +text*priv_type_text=PG_GETARG_TEXT_P(1); |
| 2308 | +AclIdusesysid; |
| 2309 | +AclModemode; |
| 2310 | +AclResultaclresult; |
| 2311 | + |
| 2312 | +usesysid=GetUserId(); |
| 2313 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2314 | + |
| 2315 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2316 | + |
| 2317 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2318 | +} |
| 2319 | + |
| 2320 | +/* |
| 2321 | + * has_tablespace_privilege_id_name |
| 2322 | + *Check user privileges on a tablespace given |
| 2323 | + *usesysid, text tablespacename, and text priv name. |
| 2324 | + */ |
| 2325 | +Datum |
| 2326 | +has_tablespace_privilege_id_name(PG_FUNCTION_ARGS) |
| 2327 | +{ |
| 2328 | +int32usesysid=PG_GETARG_INT32(0); |
| 2329 | +text*tablespacename=PG_GETARG_TEXT_P(1); |
| 2330 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 2331 | +Oidtablespaceoid; |
| 2332 | +AclModemode; |
| 2333 | +AclResultaclresult; |
| 2334 | + |
| 2335 | +tablespaceoid=convert_tablespace_name(tablespacename); |
| 2336 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2337 | + |
| 2338 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2339 | + |
| 2340 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2341 | +} |
| 2342 | + |
| 2343 | +/* |
| 2344 | + * has_tablespace_privilege_id_id |
| 2345 | + *Check user privileges on a tablespace given |
| 2346 | + *usesysid, tablespace oid, and text priv name. |
| 2347 | + */ |
| 2348 | +Datum |
| 2349 | +has_tablespace_privilege_id_id(PG_FUNCTION_ARGS) |
| 2350 | +{ |
| 2351 | +int32usesysid=PG_GETARG_INT32(0); |
| 2352 | +Oidtablespaceoid=PG_GETARG_OID(1); |
| 2353 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 2354 | +AclModemode; |
| 2355 | +AclResultaclresult; |
| 2356 | + |
| 2357 | +mode=convert_tablespace_priv_string(priv_type_text); |
| 2358 | + |
| 2359 | +aclresult=pg_tablespace_aclcheck(tablespaceoid,usesysid,mode); |
| 2360 | + |
| 2361 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 2362 | +} |
| 2363 | + |
| 2364 | +/* |
| 2365 | + *Support routines for has_tablespace_privilege family. |
| 2366 | + */ |
| 2367 | + |
| 2368 | +/* |
| 2369 | + * Given a tablespace name expressed as a string, look it up and return Oid |
| 2370 | + */ |
| 2371 | +staticOid |
| 2372 | +convert_tablespace_name(text*tablespacename) |
| 2373 | +{ |
| 2374 | +char*spcname; |
| 2375 | +Oidoid; |
| 2376 | + |
| 2377 | +spcname=DatumGetCString(DirectFunctionCall1(textout, |
| 2378 | +PointerGetDatum(tablespacename))); |
| 2379 | +oid=get_tablespace_oid(spcname); |
| 2380 | + |
| 2381 | +if (!OidIsValid(oid)) |
| 2382 | +ereport(ERROR, |
| 2383 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2384 | +errmsg("tablespace \"%s\" does not exist",spcname))); |
| 2385 | + |
| 2386 | +returnoid; |
| 2387 | +} |
| 2388 | + |
| 2389 | +/* |
| 2390 | + * convert_tablespace_priv_string |
| 2391 | + *Convert text string to AclMode value. |
| 2392 | + */ |
| 2393 | +staticAclMode |
| 2394 | +convert_tablespace_priv_string(text*priv_type_text) |
| 2395 | +{ |
| 2396 | +char*priv_type; |
| 2397 | + |
| 2398 | +priv_type=DatumGetCString(DirectFunctionCall1(textout, |
| 2399 | +PointerGetDatum(priv_type_text))); |
| 2400 | + |
| 2401 | +/* |
| 2402 | + * Return mode from priv_type string |
| 2403 | + */ |
| 2404 | +if (pg_strcasecmp(priv_type,"CREATE")==0) |
| 2405 | +returnACL_CREATE; |
| 2406 | +if (pg_strcasecmp(priv_type,"CREATE WITH GRANT OPTION")==0) |
| 2407 | +returnACL_GRANT_OPTION_FOR(ACL_CREATE); |
| 2408 | + |
| 2409 | +ereport(ERROR, |
| 2410 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 2411 | +errmsg("unrecognized privilege type: \"%s\"",priv_type))); |
| 2412 | +returnACL_NO_RIGHTS;/* keep compiler quiet */ |
| 2413 | +} |