Skip to content

Commit 6dd0411

Browse files
committed
refactor: rm run_process_utility_hook from internal module
Partly addresses #79 The `handle_alter_extension` and `handle_drop_extension` were using `run_process_utility_hook` various times unnecessarily. Now this operation is only executed once on each statement case.
1 parent e17a205 commit 6dd0411

File tree

3 files changed

+57
-84
lines changed

3 files changed

+57
-84
lines changed

src/privileged_extensions.c

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -248,56 +248,24 @@ void handle_create_extension(
248248
pfree(filename);
249249
}
250250

251-
void handle_alter_extension(
252-
void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
253-
PROCESS_UTILITY_PARAMS,
254-
const char *extname, const char *privileged_extensions,
255-
const char *superuser) {
256-
257-
if (is_string_in_comma_delimited_string(extname,
258-
privileged_extensions)) {
259-
bool already_switched_to_superuser = false;
260-
switch_to_superuser(superuser,
261-
&already_switched_to_superuser);
262-
263-
run_process_utility_hook(process_utility_hook);
251+
bool all_extensions_are_privileged(List *objects, const char *privileged_extensions){
252+
ListCell *lc;
264253

265-
if (!already_switched_to_superuser) {
266-
switch_to_original_role();
267-
}
268-
} else {
269-
run_process_utility_hook(process_utility_hook);
270-
}
271-
}
254+
if(privileged_extensions == NULL) return false;
272255

273-
void handle_drop_extension(void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
274-
PROCESS_UTILITY_PARAMS,
275-
const char *privileged_extensions,
276-
const char *superuser) {
277-
DropStmt *stmt = (DropStmt *)pstmt->utilityStmt;
278-
bool all_extensions_are_privileged = true;
279-
ListCell *lc;
256+
foreach (lc, objects) {
257+
char *name = strVal(lfirst(lc));
280258

281-
foreach (lc, stmt->objects) {
282-
char *name = strVal(lfirst(lc));
259+
if (!is_string_in_comma_delimited_string(name, privileged_extensions)) {
260+
return false;
261+
}
262+
}
283263

284-
if (!is_string_in_comma_delimited_string(name, privileged_extensions)) {
285-
all_extensions_are_privileged = false;
286-
break;
287-
}
288-
}
289-
290-
if (all_extensions_are_privileged) {
291-
bool already_switched_to_superuser = false;
292-
switch_to_superuser(superuser,
293-
&already_switched_to_superuser);
264+
return true;
265+
}
294266

295-
run_process_utility_hook(process_utility_hook);
267+
bool is_extension_privileged(const char *extname, const char *privileged_extensions){
268+
if(privileged_extensions == NULL) return false;
296269

297-
if (!already_switched_to_superuser) {
298-
switch_to_original_role();
299-
}
300-
} else {
301-
run_process_utility_hook(process_utility_hook);
302-
}
270+
return is_string_in_comma_delimited_string(extname, privileged_extensions);
303271
}

src/privileged_extensions.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@ extern void handle_create_extension(
1111
const char *privileged_extensions_custom_scripts_path,
1212
const extension_parameter_overrides *epos, const size_t total_epos);
1313

14-
extern void
15-
handle_alter_extension(void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
16-
PROCESS_UTILITY_PARAMS,
17-
const char *extname,
18-
const char *privileged_extensions,
19-
const char *superuser);
14+
bool all_extensions_are_privileged(List *objects, const char *privileged_extensions);
2015

21-
extern void
22-
handle_drop_extension(void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
23-
PROCESS_UTILITY_PARAMS, const char *privileged_extensions,
24-
const char *superuser);
16+
bool is_extension_privileged(const char *extname, const char *privileged_extensions);
2517

2618
#endif

src/supautils.c

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -464,39 +464,45 @@ static void supautils_hook(PROCESS_UTILITY_PARAMS) {
464464
if (superuser()) {
465465
break;
466466
}
467-
if (privileged_extensions == NULL) {
468-
break;
469-
}
470467

471468
AlterExtensionStmt *stmt = (AlterExtensionStmt *)pstmt->utilityStmt;
472469

473-
handle_alter_extension(prev_hook,
474-
PROCESS_UTILITY_ARGS,
475-
stmt->extname,
476-
privileged_extensions,
477-
supautils_superuser);
478-
return;
470+
if (is_extension_privileged(stmt->extname, privileged_extensions)) {
471+
bool already_switched_to_superuser = false;
472+
473+
switch_to_superuser(supautils_superuser, &already_switched_to_superuser);
474+
475+
run_process_utility_hook(prev_hook);
476+
477+
if (!already_switched_to_superuser) {
478+
switch_to_original_role();
479+
}
480+
}
481+
482+
break;
479483
}
480484

481485
/*
482486
* ALTER EXTENSION <extension> SET SCHEMA
483487
*/
484488
case T_AlterObjectSchemaStmt: {
489+
if (superuser()) {
490+
break;
491+
}
492+
485493
AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *)pstmt->utilityStmt;
486494

487-
if (stmt->objectType == OBJECT_EXTENSION){
488-
if (superuser()) {
489-
break;
490-
}
491-
if (privileged_extensions == NULL) {
492-
break;
493-
}
495+
if (stmt->objectType == OBJECT_EXTENSION &&
496+
is_extension_privileged(strVal(stmt->object), privileged_extensions)) {
497+
bool already_switched_to_superuser = false;
498+
499+
switch_to_superuser(supautils_superuser, &already_switched_to_superuser);
500+
501+
run_process_utility_hook(prev_hook);
494502

495-
handle_alter_extension(prev_hook,
496-
PROCESS_UTILITY_ARGS,
497-
strVal(stmt->object),
498-
privileged_extensions,
499-
supautils_superuser);
503+
if (!already_switched_to_superuser) {
504+
switch_to_original_role();
505+
}
500506

501507
return;
502508
}
@@ -666,14 +672,21 @@ static void supautils_hook(PROCESS_UTILITY_PARAMS) {
666672
*/
667673
case OBJECT_EXTENSION:
668674
{
669-
if (privileged_extensions == NULL) {
670-
break;
675+
if (all_extensions_are_privileged(stmt->objects, privileged_extensions)) {
676+
bool already_switched_to_superuser = false;
677+
switch_to_superuser(supautils_superuser,
678+
&already_switched_to_superuser);
679+
680+
run_process_utility_hook(prev_hook);
681+
682+
if (!already_switched_to_superuser) {
683+
switch_to_original_role();
684+
}
685+
686+
return;
671687
}
672-
handle_drop_extension(prev_hook,
673-
PROCESS_UTILITY_ARGS,
674-
privileged_extensions,
675-
supautils_superuser);
676-
return;
688+
689+
break;
677690
}
678691

679692
/*

0 commit comments

Comments
 (0)