@@ -34,6 +34,7 @@ static void AddAcl(PQExpBuffer aclbuf, const char *keyword,
34
34
*
35
35
* name: the object name, in the form to use in the commands (already quoted)
36
36
* subname: the sub-object name, if any (already quoted); NULL if none
37
+ * nspname: the namespace the object is in (NULL if none); not pre-quoted
37
38
* type: the object type (as seen in GRANT command: must be one of
38
39
* TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
39
40
<
1241
/td> * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT)
@@ -54,7 +55,7 @@ static void AddAcl(PQExpBuffer aclbuf, const char *keyword,
54
55
* since this routine uses fmtId() internally.
55
56
*/
56
57
bool
57
- buildACLCommands (const char * name , const char * subname ,
58
+ buildACLCommands (const char * name , const char * subname , const char * nspname ,
58
59
const char * type , const char * acls , const char * racls ,
59
60
const char * owner , const char * prefix , int remoteVersion ,
60
61
PQExpBuffer sql )
@@ -154,7 +155,10 @@ buildACLCommands(const char *name, const char *subname,
154
155
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
155
156
if (subname )
156
157
appendPQExpBuffer (firstsql , "(%s)" , subname );
157
- appendPQExpBuffer (firstsql , " ON %s %s FROM PUBLIC;\n" , type , name );
158
+ appendPQExpBuffer (firstsql , " ON %s " , type );
159
+ if (nspname && * nspname )
160
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
161
+ appendPQExpBuffer (firstsql , "%s FROM PUBLIC;\n" , name );
158
162
}
159
163
else
160
164
{
@@ -172,8 +176,11 @@ buildACLCommands(const char *name, const char *subname,
172
176
{
173
177
if (privs -> len > 0 )
174
178
{
175
- appendPQExpBuffer (firstsql , "%sREVOKE %s ON %s %s FROM " ,
176
- prefix , privs -> data , type , name );
179
+ appendPQExpBuffer (firstsql , "%sREVOKE %s ON %s " ,
180
+ prefix , privs -> data , type );
181
+ if (nspname && * nspname )
182
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
183
+ appendPQExpBuffer (firstsql , "%s FROM " , name );
177
184
if (grantee -> len == 0 )
178
185
appendPQExpBufferStr (firstsql , "PUBLIC;\n" );
179
186
else if (strncmp (grantee -> data , "group " ,
@@ -187,8 +194,11 @@ buildACLCommands(const char *name, const char *subname,
187
194
if (privswgo -> len > 0 )
188
195
{
189
196
appendPQExpBuffer (firstsql ,
190
- "%sREVOKE GRANT OPTION FOR %s ON %s %s FROM " ,
191
- prefix , privswgo -> data , type , name );
197
+ "%sREVOKE GRANT OPTION FOR %s ON %s " ,
198
+ prefix , privswgo -> data , type );
199
+ if (nspname && * nspname )
200
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
201
+ appendPQExpBuffer (firstsql , "%s FROM " , name );
192
202
if (grantee -> len == 0 )
193
203
appendPQExpBufferStr (firstsql , "PUBLIC" );
194
204
else if (strncmp (grantee -> data , "group " ,
@@ -255,18 +265,33 @@ buildACLCommands(const char *name, const char *subname,
255
265
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
256
266
if (subname )
257
267
appendPQExpBuffer (firstsql , "(%s)" , subname );
258
- appendPQExpBuffer (firstsql , " ON %s %s FROM %s;\n" ,
259
- type , name , fmtId (grantee -> data ));
268
+ appendPQExpBuffer (firstsql , " ON %s " , type );
269
+ if (nspname && * nspname )
270
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
271
+ appendPQExpBuffer (firstsql , "%s FROM %s;\n" ,
272
+ name , fmtId (grantee -> data ));
260
273
if (privs -> len > 0 )
274
+ {
261
275
appendPQExpBuffer (firstsql ,
262
- "%sGRANT %s ON %s %s TO %s;\n" ,
263
- prefix , privs -> data , type , name ,
264
- fmtId (grantee -> data ));
276
+ "%sGRANT %s ON %s " ,
277
+ prefix , privs -> data , type );
278
+ if (nspname && * nspname )
279
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
280
+ appendPQExpBuffer (firstsql ,
281
+ "%s TO %s;\n" ,
282
+ name , fmtId (grantee -> data ));
283
+ }
265
284
if (privswgo -> len > 0 )
285
+ {
266
286
appendPQExpBuffer (firstsql ,
267
- "%sGRANT %s ON %s %s TO %s WITH GRANT OPTION;\n" ,
268
- prefix , privswgo -> data , type , name ,
269
- fmtId (grantee -> data ));
287
+ "%sGRANT %s ON %s " ,
288
+ prefix , privswgo -> data , type );
289
+ if (nspname && * nspname )
290
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
291
+ appendPQExpBuffer (firstsql ,
292
+ "%s TO %s WITH GRANT OPTION;\n" ,
293
+ name , fmtId (grantee -> data ));
294
+ }
270
295
}
271
296
}
272
297
else
@@ -288,8 +313,11 @@ buildACLCommands(const char *name, const char *subname,
288
313
289
314
if (privs -> len > 0 )
290
315
{
291
- appendPQExpBuffer (secondsql , "%sGRANT %s ON %s %s TO " ,
292
- prefix , privs -> data , type , name );
316
+ appendPQExpBuffer (secondsql , "%sGRANT %s ON %s " ,
317
+ prefix , privs -> data , type );
318
+ if (nspname && * nspname )
319
+ appendPQExpBuffer (secondsql , "%s." , fmtId (nspname ));
320
+ appendPQExpBuffer (secondsql , "%s TO " , name );
293
321
if (grantee -> len == 0 )
294
322
appendPQExpBufferStr (secondsql , "PUBLIC;\n" );
295
323
else if (strncmp (grantee -> data , "group " ,
@@ -301,8 +329,11 @@ buildACLCommands(const char *name, const char *subname,
301
329
}
302
330
if (privswgo -> len > 0 )
303
331
{
304
- appendPQExpBuffer (secondsql , "%sGRANT %s ON %s %s TO " ,
305
- prefix , privswgo -> data , type , name );
332
+ appendPQExpBuffer (secondsql , "%sGRANT %s ON %s " ,
333
+ prefix , privswgo -> data , type );
334
+ if (nspname && * nspname )
335
+ appendPQExpBuffer (secondsql , "%s." , fmtId (nspname ));
336
+ appendPQExpBuffer (secondsql , "%s TO " , name );
306
337
if (grantee -> len == 0 )
307
338
appendPQExpBufferStr (secondsql , "PUBLIC" );
308
339
else if (strncmp (grantee -> data , "group " ,
@@ -332,8 +363,11 @@ buildACLCommands(const char *name, const char *subname,
332
363
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
333
364
if (subname )
334
365
appendPQExpBuffer (firstsql , "(%s)" , subname );
335
- appendPQExpBuffer (firstsql , " ON %s %s FROM %s;\n" ,
336
- type , name , fmtId (owner ));
366
+ appendPQExpBuffer (firstsql , " ON %s " , type );
367
+ if (nspname && * nspname )
368
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
369
+ appendPQExpBuffer (firstsql , "%s FROM %s;\n" ,
370
+ name , fmtId (owner ));
337
371
}
338
372
339
373
destroyPQExpBuffer (grantee );
@@ -392,7 +426,8 @@ buildDefaultACLCommands(const char *type, const char *nspname,
392
426
if (strlen (initacls ) != 0 || strlen (initracls ) != 0 )
393
427
{
394
428
appendPQExpBuffer (sql , "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n" );
395
- if (!buildACLCommands ("" , NULL , type , initacls , initracls , owner ,
429
+ if (!buildACLCommands ("" , NULL , NULL , type ,
430
+ initacls , initracls , owner ,
396
431
prefix -> data , remoteVersion , sql ))
397
432
{
398
433
destroyPQExpBuffer (prefix );
@@ -401,7 +436,8 @@ buildDefaultACLCommands(const char *type, const char *nspname,
401
436
appendPQExpBuffer (sql , "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n" );
402
437
}
403
438
404
- if (!buildACLCommands ("" , NULL , type , acls , racls , owner ,
439
+ if (!buildACLCommands ("" , NULL , NULL , type ,
440
+ acls , racls , owner ,
405
441
prefix -> data , remoteVersion , sql ))
406
442
{
407
443
destroyPQExpBuffer (prefix );
@@ -645,26 +681,32 @@ AddAcl(PQExpBuffer aclbuf, const char *keyword, const char *subname)
645
681
* buildShSecLabelQuery
646
682
*
647
683
* Build a query to retrieve security labels for a shared object.
684
+ * The object is identified by its OID plus the name of the catalog
685
+ * it can be found in (e.g., "pg_database" for database names).
686
+ * The query is appended to "sql". (We don't execute it here so as to
687
+ * keep this file free of assumptions about how to deal with SQL errors.)
648
688
*/
649
689
void
650
- buildShSecLabelQuery (PGconn * conn , const char * catalog_name , uint32 objectId ,
690
+ buildShSecLabelQuery (PGconn * conn , const char * catalog_name , Oid objectId ,
651
691
PQExpBuffer sql )
652
692
{
653
693
appendPQExpBuffer (sql ,
654
694
"SELECT provider, label FROM pg_catalog.pg_shseclabel "
655
- "WHERE classoid = '%s'::pg_catalog.regclass AND "
656
- "objoid = %u " , catalog_name , objectId );
695
+ "WHERE classoid = 'pg_catalog. %s'::pg_catalog.regclass "
696
+ "AND objoid = '%u' " , catalog_name , objectId );
657
697
}
658
698
659
699
/*
660
700
* emitShSecLabels
661
701
*
662
- * Format security label data retrieved by the query generated in
663
- * buildShSecLabelQuery.
702
+ * Construct SECURITY LABEL commands using the data retrieved by the query
703
+ * generated by buildShSecLabelQuery, and append them to "buffer".
704
+ * Here, the target object is identified by its type name (e.g. "DATABASE")
705
+ * and its name (not pre-quoted).
664
706
*/
665
707
void
666
708
emitShSecLabels (PGconn * conn , PGresult * res , PQExpBuffer buffer ,
667
- const char * target , const char * objname )
709
+ const char * objtype , const char * objname )
668
710
{
669
711
int i ;
670
712
@@ -676,7 +718,7 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer,
676
718
/* must use fmtId result before calling it again */
677
719
appendPQExpBuffer (buffer ,
678
720
"SECURITY LABEL FOR %s ON %s" ,
679
- fmtId (provider ), target );
721
+ fmtId (provider ), objtype );
680
722
appendPQExpBuffer (buffer ,
681
723
" %s IS " ,
682
724
fmtId (objname ));
0 commit comments