@@ -32,6 +32,7 @@ static void AddAcl(PQExpBuffer aclbuf, const char *keyword,
32
32
*
33
33
* name: the object name, in the form to use in the commands (already quoted)
34
34
* subname: the sub-object name, if any (already quoted); NULL if none
35
+ * nspname: the namespace the object is in (NULL if none); not pre-quoted
35
36
* type: the object type (as seen in GRANT command: must be one of
36
37
* TABLE, SEQUENCE, FUNCTION, PROCEDURE, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
37
38
* FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT)
@@ -52,7 +53,7 @@ static void AddAcl(PQExpBuffer aclbuf, const char *keyword,
52
53
* since this routine uses fmtId() internally.
53
54
*/
54
55
bool
55
- buildACLCommands (const char * name , const char * subname ,
56
+ buildACLCommands (const char * name , const char * subname , const char * nspname ,
56
57
const char * type , const char * acls , const char * racls ,
57
58
const char * owner , const char * prefix , int remoteVersion ,
58
59
PQExpBuffer sql )
@@ -152,7 +153,10 @@ buildACLCommands(const char *name, const char *subname,
152
153
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
153
154
if (subname )
154
155
appendPQExpBuffer (firstsql , "(%s)" , subname );
155
- appendPQExpBuffer (firstsql , " ON %s %s FROM PUBLIC;\n" , type , name );
156
+ appendPQExpBuffer (firstsql , " ON %s " , type );
157
+ if (nspname && * nspname )
158
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
159
+ appendPQExpBuffer (firstsql , "%s FROM PUBLIC;\n" , name );
156
160
}
157
161
else
158
162
{
@@ -170,8 +174,11 @@ buildACLCommands(const char *name, const char *subname,
170
174
{
171
175
if (privs -> len > 0 )
172
176
{
173
- appendPQExpBuffer (firstsql , "%sREVOKE %s ON %s %s FROM " ,
174
- prefix , privs -> data , type , name );
177
+ appendPQExpBuffer (firstsql , "%sREVOKE %s ON %s " ,
178
+ prefix , privs -> data , type );
179
+ if (nspname && * nspname )
180
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
181
+ appendPQExpBuffer (firstsql , "%s FROM " , name );
175
182
if (grantee -> len == 0 )
176
183
appendPQExpBufferStr (firstsql , "PUBLIC;\n" );
177
184
else if (strncmp (grantee -> data , "group " ,
@@ -185,8 +192,11 @@ buildACLCommands(const char *name, const char *subname,
185
192
if (privswgo -> len > 0 )
186
193
{
187
194
appendPQExpBuffer (firstsql ,
188
- "%sREVOKE GRANT OPTION FOR %s ON %s %s FROM " ,
189
- prefix , privswgo -> data , type , name );
195
+ "%sREVOKE GRANT OPTION FOR %s ON %s " ,
196
+ prefix , privswgo -> data , type );
197
+ if (nspname && * nspname )
198
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
199
+ appendPQExpBuffer (firstsql , "%s FROM " , name );
190
200
if (grantee -> len == 0 )
191
201
appendPQExpBufferStr (firstsql , "PUBLIC" );
192
202
else if (strncmp (grantee -> data , "group " ,
@@ -251,18 +261,33 @@ buildACLCommands(const char *name, const char *subname,
251
261
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
252
262
if (subname )
253
263
appendPQExpBuffer (firstsql , "(%s)" , subname );
254
- appendPQExpBuffer (firstsql , " ON %s %s FROM %s;\n" ,
255
- type , name , fmtId (grantee -> data ));
264
+ appendPQExpBuffer (firstsql , " ON %s " , type );
265
+ if (nspname && * nspname )
266
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
267
+ appendPQExpBuffer (firstsql , "%s FROM %s;\n" ,
268
+ name , fmtId (grantee -> data ));
256
269
if (privs -> len > 0 )
270
+ {
257
271
appendPQExpBuffer (firstsql ,
258
- "%sGRANT %s ON %s %s TO %s;\n" ,
259
- prefix , privs -> data , type , name ,
260
- fmtId (grantee -> data ));
272
+ "%sGRANT %s ON %s " ,
273
+ prefix , privs -> data , type );
274
+ if (nspname && * nspname )
275
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
276
+ appendPQExpBuffer (firstsql ,
277
+ "%s TO %s;\n" ,
278
+ name , fmtId (grantee -> data ));
279
+ }
261
280
if (privswgo -> len > 0 )
281
+ {
262
282
appendPQExpBuffer (firstsql ,
263
- "%sGRANT %s ON %s %s TO %s WITH GRANT OPTION;\n" ,
264
- prefix , privswgo -> data , type , name ,
265
- fmtId (grantee -> data ));
283
+ "%sGRANT %s ON %s " ,
284
+ prefix , privswgo -> data , type );
285
+ if (nspname && * nspname )
286
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
287
+ appendPQExpBuffer (firstsql ,
288
+ "%s TO %s WITH GRANT OPTION;\n" ,
289
+ name , fmtId (grantee -> data ));
290
+ }
266
291
}
267
292
}
268
293
else
@@ -284,8 +309,11 @@ buildACLCommands(const char *name, const char *subname,
284
309
285
310
if (privs -> len > 0 )
286
311
{
287
- appendPQExpBuffer (secondsql , "%sGRANT %s ON %s %s TO " ,
288
- prefix , privs -> data , type , name );
312
+ appendPQExpBuffer (secondsql , "%sGRANT %s ON %s " ,
313
+ prefix , privs -> data , type );
314
+ if (nspname && * nspname )
315
+ appendPQExpBuffer (secondsql , "%s." , fmtId (nspname ));
316
+ appendPQExpBuffer (secondsql , "%s TO " , name );
289
317
if (grantee -> len == 0 )
290
318
appendPQExpBufferStr (secondsql , "PUBLIC;\n" );
291
319
else if (strncmp (grantee -> data , "group " ,
@@ -297,8 +325,11 @@ buildACLCommands(const char *name, const char *subname,
297
325
}
298
326
if (privswgo -> len > 0 )
299
327
{
300
- appendPQExpBuffer (secondsql , "%sGRANT %s ON %s %s TO " ,
301
- prefix , privswgo -> data , type , name );
328
+ appendPQExpBuffer (secondsql , "%sGRANT %s ON %s " ,
329
+ prefix , privswgo -> data , type );
330
+ if (nspname && * nspname )
331
+ appendPQExpBuffer (secondsql , "%s." , fmtId (nspname ));
332
+ appendPQExpBuffer (secondsql , "%s TO " , name );
302
333
if (grantee -> len == 0 )
303
334
appendPQExpBufferStr (secondsql , "PUBLIC" );
304
335
else if (strncmp (grantee -> data , "group " ,
@@ -328,8 +359,11 @@ buildACLCommands(const char *name, const char *subname,
328
359
appendPQExpBuffer (firstsql , "%sREVOKE ALL" , prefix );
329
360
if (subname )
330
361
appendPQExpBuffer (firstsql , "(%s)" , subname );
331
- appendPQExpBuffer (firstsql , " ON %s %s FROM %s;\n" ,
332
- type , name , fmtId (owner ));
362
+ appendPQExpBuffer (firstsql , " ON %s " , type );
363
+ if (nspname && * nspname )
364
+ appendPQExpBuffer (firstsql , "%s." , fmtId (nspname ));
365
+ appendPQExpBuffer (firstsql , "%s FROM %s;\n" ,
366
+ name , fmtId (owner ));
333
367
}
334
368
335
369
destroyPQExpBuffer (grantee );
@@ -388,7 +422,8 @@ buildDefaultACLCommands(const char *type, const char *nspname,
388
422
if (strlen (initacls ) != 0 || strlen (initracls ) != 0 )
389
423
{
390
424
appendPQExpBuffer (sql , "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n" );
391
- if (!buildACLCommands ("" , NULL , type , initacls , initracls , owner ,
425
+ if (!buildACLCommands ("" , NULL , NULL , type ,
426
+ initacls , initracls , owner ,
392
427
prefix -> data , remoteVersion , sql ))
393
428
{
394
429
destroyPQExpBuffer (prefix );
@@ -397,7 +432,8 @@ buildDefaultACLCommands(const char *type, const char *nspname,
397
432
appendPQExpBuffer (sql , "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n" );
398
433
}
399
434
400
- if (!buildACLCommands ("" , NULL , type , acls , racls , owner ,
435
+ if (!buildACLCommands ("" , NULL , NULL , type ,
436
+ acls , racls , owner ,
401
437
prefix -> data , remoteVersion , sql ))
402
438
{
403
439
destroyPQExpBuffer (prefix );
@@ -641,26 +677,32 @@ AddAcl(PQExpBuffer aclbuf, const char *keyword, const char *subname)
641
677
* buildShSecLabelQuery
642
678
*
643
679
* Build a query to retrieve security labels for a shared object.
680
+ * The object is identified by its OID plus the name of the catalog
681
+ * it can be found in (e.g., "pg_database" for database names).
682
+ * The query is appended to "sql". (We don't execute it here so as to
683
+ * keep this file free of assumptions about how to deal with SQL errors.)
644
684
*/
645
685
void
646
- buildShSecLabelQuery (PGconn * conn , const char * catalog_name , uint32 objectId ,
686
+ buildShSecLabelQuery (PGconn * conn , const char * catalog_name , Oid objectId ,
647
687
PQExpBuffer sql )
648
688
{
649
689
appendPQExpBuffer (sql ,
650
690
"SELECT provider, label FROM pg_catalog.pg_shseclabel "
651
- "WHERE classoid = '%s'::pg_catalog.regclass AND "
652
- "objoid = %u " , catalog_name , objectId );
691
+ "WHERE classoid = 'pg_catalog. %s'::pg_catalog.regclass "
692
+ "AND objoid = '%u' " , catalog_name , objectId );
653
693
}
654
694
655
695
/*
656
696
* emitShSecLabels
657
697
*
658
- * Format security label data retrieved by the query generated in
659
- * buildShSecLabelQuery.
698
+ * Construct SECURITY LABEL commands using the data retrieved by the query
699
+ * generated by buildShSecLabelQuery, and append them to "buffer".
700
+ * Here, the target object is identified by its type name (e.g. "DATABASE")
701
+ * and its name (not pre-quoted).
660
702
*/
661
703
void
662
704
emitShSecLabels (PGconn * conn , PGresult * res , PQExpBuffer buffer ,
663
- const char * target , const char * objname )
705
+ const char * objtype , const char * objname )
664
706
{
665
707
int i ;
666
708
@@ -672,7 +714,7 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer,
672
714
/* must use fmtId result before calling it again */
673
715
appendPQExpBuffer (buffer ,
674
716
"SECURITY LABEL FOR %s ON %s" ,
675
- fmtId (provider ), target );
717
+ fmtId (provider ), objtype );
676
718
appendPQExpBuffer (buffer ,
677
719
" %s IS " ,
678
720
fmtId (objname ));
0 commit comments