101
101
#include "common/sha2.h"
102
102
#include "libpq/auth.h"
103
103
#include "libpq/crypt.h"
104
+ #include "libpq/sasl.h"
104
105
#include "libpq/scram.h"
105
106
#include "miscadmin.h"
106
107
#include "utils/builtins.h"
107
108
#include "utils/timestamp.h"
108
109
110
+ static void scram_get_mechanisms (Port * port , StringInfo buf );
111
+ static void * scram_init (Port * port , const char * selected_mech ,
112
+ const char * shadow_pass );
113
+ static int scram_exchange (void * opaq , const char * input , int inputlen ,
114
+ char * * output , int * outputlen , char * * logdetail );
115
+
116
+ /* Mechanism declaration */
117
+ const pg_be_sasl_mech pg_be_scram_mech = {
118
+ scram_get_mechanisms ,
119
+ scram_init ,
120
+ scram_exchange
121
+ };
122
+
109
123
/*
110
124
* Status data for a SCRAM authentication exchange. This should be kept
111
125
* internal to this file.
@@ -170,16 +184,14 @@ static char *sanitize_str(const char *s);
170
184
static char * scram_mock_salt (const char * username );
171
185
172
186
/*
173
- * pg_be_scram_get_mechanisms
174
- *
175
187
* Get a list of SASL mechanisms that this module supports.
176
188
*
177
189
* For the convenience of building the FE/BE packet that lists the
178
190
* mechanisms, the names are appended to the given StringInfo buffer,
179
191
* separated by '\0' bytes.
180
192
*/
181
- void
182
- pg_be_scram_get_mechanisms (Port * port , StringInfo buf )
193
+ static void
194
+ scram_get_mechanisms (Port * port , StringInfo buf )
183
195
{
184
196
/*
185
197
* Advertise the mechanisms in decreasing order of importance. So the
@@ -199,26 +211,22 @@ pg_be_scram_get_mechanisms(Port *port, StringInfo buf)
199
211
}
200
212
201
213
/*
202
- * pg_be_scram_init
203
- *
204
214
* Initialize a new SCRAM authentication exchange status tracker. This
205
215
* needs to be called before doing any exchange. It will be filled later
206
216
* after the beginning of the exchange with authentication information.
207
217
*
208
218
* 'selected_mech' identifies the SASL mechanism that the client selected.
209
219
* It should be one of the mechanisms that we support, as returned by
210
- * pg_be_scram_get_mechanisms ().
220
+ * scram_get_mechanisms ().
211
221
*
212
222
* 'shadow_pass' is the role's stored secret, from pg_authid.rolpassword.
213
223
* The username was provided by the client in the startup message, and is
214
224
* available in port->user_name. If 'shadow_pass' is NULL, we still perform
215
225
* an authentication exchange, but it will fail, as if an incorrect password
216
226
* was given.
217
227
*/
218
- void *
219
- pg_be_scram_init (Port * port ,
220
- const char * selected_mech ,
221
- const char * shadow_pass )
228
+ static void *
229
+ scram_init (Port * port , const char * selected_mech , const char * shadow_pass )
222
230
{
223
231
scram_state * state ;
224
232
bool got_secret ;
@@ -325,9 +333,9 @@ pg_be_scram_init(Port *port,
325
333
* string at *logdetail that will be sent to the postmaster log (but not
326
334
* the client).
327
335
*/
328
- int
329
- pg_be_scram_exchange (void * opaq , const char * input , int inputlen ,
330
- char * * output , int * outputlen , char * * logdetail )
336
+ static int
337
+ scram_exchange (void * opaq , const char * input , int inputlen ,
338
+ char * * output , int * outputlen , char * * logdetail )
331
339
{
332
340
scram_state * state = (scram_state * ) opaq ;
333
341
int result ;
@@ -346,7 +354,7 @@ pg_be_scram_exchange(void *opaq, const char *input, int inputlen,
346
354
347
355
* output = pstrdup ("" );
348
356
* outputlen = 0 ;
349
- return SASL_EXCHANGE_CONTINUE ;
357
+ return PG_SASL_EXCHANGE_CONTINUE ;
350
358
}
351
359
352
360
/*
@@ -379,7 +387,7 @@ pg_be_scram_exchange(void *opaq, const char *input, int inputlen,
379
387
* output = build_server_first_message (state );
380
388
381
389
state -> state = SCRAM_AUTH_SALT_SENT ;
382
- result = SASL_EXCHANGE_CONTINUE ;
390
+ result = PG_SASL_EXCHANGE_CONTINUE ;
383
391
break ;
384
392
385
393
case SCRAM_AUTH_SALT_SENT :
@@ -408,7 +416,8 @@ pg_be_scram_exchange(void *opaq, const char *input, int inputlen,
408
416
* erroring out in an application-specific way. We choose to do
409
417
* the latter, so that the error message for invalid password is
410
418
* the same for all authentication methods. The caller will call
411
- * ereport(), when we return SASL_EXCHANGE_FAILURE with no output.
419
+ * ereport(), when we return PG_SASL_EXCHANGE_FAILURE with no
420
+ * output.
412
421
*
413
422
* NB: the order of these checks is intentional. We calculate the
414
423
* client proof even in a mock authentication, even though it's
@@ -417,24 +426,24 @@ pg_be_scram_exchange(void *opaq, const char *input, int inputlen,
417
426
*/
418
427
if (!verify_client_proof (state ) || state -> doomed )
419
428
{
420
- result = SASL_EXCHANGE_FAILURE ;
429
+ result = PG_SASL_EXCHANGE_FAILURE ;
421
430
break ;
422
431
}
423
432
424
433
/* Build final message for client */
425
434
* output = build_server_final_message (state );
426
435
427
436
/* Success! */
428
- result = SASL_EXCHANGE_SUCCESS ;
437
+ result = PG_SASL_EXCHANGE_SUCCESS ;
429
438
state -> state = SCRAM_AUTH_FINISHED ;
430
439
break ;
431
440
432
441
default :
433
442
elog (ERROR , "invalid SCRAM exchange state" );
434
- result = SASL_EXCHANGE_FAILURE ;
443
+ result = PG_SASL_EXCHANGE_FAILURE ;
435
444
}
436
445
437
- if (result == SASL_EXCHANGE_FAILURE && state -> logdetail && logdetail )
446
+ if (result == PG_SASL_EXCHANGE_FAILURE && state -> logdetail && logdetail )
438
447
* logdetail = state -> logdetail ;
439
448
440
449
if (* output )
0 commit comments