50
50
#include "commands/extension.h"
51
51
#include "commands/user.h"
52
52
#include "executor/spi.h"
53
+ #include "funcapi.h"
53
54
#include "libpq/auth.h"
54
55
#include "nodes/pg_list.h"
55
56
#include "postmaster/bgworker_internals.h"
96
97
*/
97
98
#define CLIENT_AUTH_USER_ERROR_MAX_STRLEN 256
98
99
99
- static const char * clientauth_shmem_name = "pgtle_clientauth" ;
100
- static const char * clientauth_feature = "clientauth" ;
101
- static const char * clientauth_worker_name = "pg_tle_clientauth worker" ;
102
-
103
- /* Background worker main entry function */
104
- PGDLLEXPORT void clientauth_launcher_main (Datum arg );
105
-
106
- /* Set up our hooks */
107
- static ClientAuthentication_hook_type prev_clientauth_hook = NULL ;
108
- static void clientauth_hook (Port * port , int status );
109
-
110
- static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
111
- static void clientauth_shmem_startup (void );
112
-
113
- #if (PG_VERSION_NUM >= 150000 )
114
- static shmem_request_hook_type prev_shmem_request_hook = NULL ;
115
- static void clientauth_shmem_request (void );
116
- #endif
117
-
118
- /* Helper functions */
119
- static Size clientauth_shared_memsize (void );
120
- static void clientauth_sighup (SIGNAL_ARGS );
121
-
122
- void clientauth_init (void );
123
- static bool can_allow_without_executing (void );
124
- static bool can_reject_without_executing (void );
125
-
126
- /* GUC that determines whether clientauth is enabled */
127
- static int enable_clientauth_feature = FEATURE_OFF ;
128
-
129
- /* GUC that determines which database SPI_exec runs against */
130
- static char * clientauth_database_name = "postgres" ;
131
-
132
- /* GUC that determines the number of background workers */
133
- static int clientauth_num_parallel_workers = 1 ;
134
-
135
- /* GUC that determines users that clientauth feature skips */
136
- static char * clientauth_users_to_skip = "" ;
137
-
138
- /* GUC that determines databases that clientauth feature skips */
139
- static char * clientauth_databases_to_skip = "" ;
140
-
141
- /* Global flags */
142
- static bool clientauth_reload_config = false;
143
-
144
100
/*
145
101
* Fixed-length subset of Port, passed to user function. A corresponding SQL
146
102
* base type is defined. Shared memory structs are required to be fixed-size,
@@ -163,6 +119,7 @@ typedef struct PortSubset
163
119
164
120
char database_name [CLIENT_AUTH_PORT_SUBSET_MAX_STRLEN ];
165
121
char user_name [CLIENT_AUTH_PORT_SUBSET_MAX_STRLEN ];
122
+ char application_name [CLIENT_AUTH_PORT_SUBSET_MAX_STRLEN ];
166
123
} PortSubset ;
167
124
168
125
/* Represents a pending connection */
@@ -233,6 +190,53 @@ typedef struct ClientAuthBgwShmemSharedState
233
190
ClientAuthStatusEntry requests [CLIENT_AUTH_MAX_PENDING_ENTRIES ];
234
191
} ClientAuthBgwShmemSharedState ;
235
192
193
+ static const char * clientauth_shmem_name = "pgtle_clientauth" ;
194
+ static const char * clientauth_feature = "clientauth" ;
195
+ static const char * clientauth_worker_name = "pg_tle_clientauth worker" ;
196
+
197
+ /* Background worker main entry function */
198
+ PGDLLEXPORT void clientauth_launcher_main (Datum arg );
199
+
200
+ /* Set up our hooks */
201
+ static ClientAuthentication_hook_type prev_clientauth_hook = NULL ;
202
+ static void clientauth_hook (Port * port , int status );
203
+
204
+ static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
205
+ static void clientauth_shmem_startup (void );
206
+
207
+ #if (PG_VERSION_NUM >= 150000 )
208
+ static shmem_request_hook_type prev_shmem_request_hook = NULL ;
209
+ static void clientauth_shmem_request (void );
210
+ #endif
211
+
212
+ /* Helper functions */
213
+ static Size clientauth_shared_memsize (void );
214
+ static void clientauth_sighup (SIGNAL_ARGS );
215
+
216
+ void clientauth_init (void );
217
+ static bool can_allow_without_executing (void );
218
+ static bool can_reject_without_executing (void );
219
+
220
+ static char * create_port_subset_str (PortSubset * port );
221
+
222
+ /* GUC that determines whether clientauth is enabled */
223
+ static int enable_clientauth_feature = FEATURE_OFF ;
224
+
225
+ /* GUC that determines which database SPI_exec runs against */
226
+ static char * clientauth_database_name = "postgres" ;
227
+
228
+ /* GUC that determines the number of background workers */
229
+ static int clientauth_num_parallel_workers = 1 ;
230
+
231
+ /* GUC that determines users that clientauth feature skips */
232
+ static char * clientauth_users_to_skip = "" ;
233
+
234
+ /* GUC that determines databases that clientauth feature skips */
235
+ static char * clientauth_databases_to_skip = "" ;
236
+
237
+ /* Global flags */
238
+ static bool clientauth_reload_config = false;
239
+
236
240
static ClientAuthBgwShmemSharedState * clientauth_ss = NULL ;
237
241
238
242
void clientauth_launcher_run_user_functions (bool * error , char (* error_msg )[CLIENT_AUTH_USER_ERROR_MAX_STRLEN ], PortSubset * port , int * status );
@@ -587,14 +591,7 @@ clientauth_launcher_run_user_functions(bool *error, char (*error_msg)[CLIENT_AUT
587
591
func_name ,
588
592
quote_identifier (PG_TLE_NSPNAME ));
589
593
590
- port_subset_str = psprintf ("(%d,%s,%s,%d,%d,%s,%s)" ,
591
- port -> noblock ,
592
- quote_identifier (port -> remote_host ),
593
- quote_identifier (port -> remote_hostname ),
594
- port -> remote_hostname_resolv ,
595
- port -> remote_hostname_errcode ,
596
- quote_identifier (port -> database_name ),
597
- quote_identifier (port -> user_name ));
594
+ port_subset_str = create_port_subset_str (port );
598
595
599
596
hookargs [0 ] = CStringGetTextDatum (port_subset_str );
600
597
hookargs [1 ] = Int32GetDatum (* status );
@@ -718,6 +715,10 @@ clientauth_hook(Port *port, int status)
718
715
CLIENT_AUTH_PORT_SUBSET_MAX_STRLEN ,
719
716
"%s" ,
720
717
port -> user_name == NULL ? "" : port -> user_name );
718
+ snprintf (clientauth_ss -> requests [idx ].port_info .application_name ,
719
+ CLIENT_AUTH_PORT_SUBSET_MAX_STRLEN ,
720
+ "%s" ,
721
+ port -> application_name == NULL ? "" : port -> application_name );
721
722
clientauth_ss -> requests [idx ].port_info .noblock = port -> noblock ;
722
723
clientauth_ss -> requests [idx ].port_info .remote_hostname_resolv = port -> remote_hostname_resolv ;
723
724
clientauth_ss -> requests [idx ].port_info .remote_hostname_errcode = port -> remote_hostname_errcode ;
@@ -894,3 +895,49 @@ can_reject_without_executing()
894
895
895
896
return false;
896
897
}
898
+
899
+ /*
900
+ * Constructs a string representation of the PortSubset composite type and
901
+ * returns it in an allocated string. The signature of the PortSubset type
902
+ * varies based on the pg_tle version, so check the signature first.
903
+ */
904
+ static char *
905
+ create_port_subset_str (PortSubset * port )
906
+ {
907
+ TupleDesc tupdesc =
908
+ RelationNameGetTupleDesc (PG_TLE_NSPNAME "."
909
+ TLE_CLIENTAUTH_PORT_SUBSET_TYPE );
910
+ char * port_subset_str ;
911
+
912
+ if (tupdesc -> natts == 7 )
913
+ port_subset_str = psprintf ("(%d,%s,%s,%d,%d,%s,%s)" ,
914
+ port -> noblock ,
915
+ quote_identifier (port -> remote_host ),
916
+ quote_identifier (port -> remote_hostname ),
917
+ port -> remote_hostname_resolv ,
918
+ port -> remote_hostname_errcode ,
919
+ quote_identifier (port -> database_name ),
920
+ quote_identifier (port -> user_name ));
921
+ else if (tupdesc -> natts == 8 )
922
+ port_subset_str = psprintf ("(%d,%s,%s,%d,%d,%s,%s,%s)" ,
923
+ port -> noblock ,
924
+ quote_identifier (port -> remote_host ),
925
+ quote_identifier (port -> remote_hostname ),
926
+ port -> remote_hostname_resolv ,
927
+ port -> remote_hostname_errcode ,
928
+ quote_identifier (port -> database_name ),
929
+ quote_identifier (port -> user_name ),
930
+ quote_identifier (port -> application_name ));
931
+ else
932
+
933
+ /*
934
+ * Should be unreachable. If we add more fields in the future, we
935
+ * need to modify the logic above.
936
+ */
937
+ ereport (ERROR ,
938
+ errmsg ("\"%s.clientauth\" feature encountered an unexpected number of fields in the \"%s.%s\" composite type: %d" ,
939
+ PG_TLE_NSPNAME , PG_TLE_NSPNAME ,
940
+ TLE_CLIENTAUTH_PORT_SUBSET_TYPE , tupdesc -> natts ));
941
+
942
+ return port_subset_str ;
943
+ }
0 commit comments