Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * auth-oauth.c
4 : : * Server-side implementation of the SASL OAUTHBEARER mechanism.
5 : : *
6 : : * See the following RFC for more details:
7 : : * - RFC 7628: https://datatracker.ietf.org/doc/html/rfc7628
8 : : *
9 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 : : * Portions Copyright (c) 1994, Regents of the University of California
11 : : *
12 : : * src/backend/libpq/auth-oauth.c
13 : : *
14 : : *-------------------------------------------------------------------------
15 : : */
16 : : #include "postgres.h"
17 : :
18 : : #include <unistd.h>
19 : : #include <fcntl.h>
20 : :
21 : : #include "common/oauth-common.h"
22 : : #include "fmgr.h"
23 : : #include "lib/stringinfo.h"
24 : : #include "libpq/auth.h"
25 : : #include "libpq/hba.h"
26 : : #include "libpq/oauth.h"
27 : : #include "libpq/sasl.h"
28 : : #include "miscadmin.h"
29 : : #include "storage/fd.h"
30 : : #include "storage/ipc.h"
31 : : #include "utils/json.h"
32 : : #include "utils/varlena.h"
33 : :
34 : : /* GUC */
35 : : char *oauth_validator_libraries_string = NULL;
36 : :
37 : : static void oauth_get_mechanisms(Port *port, StringInfo buf);
38 : : static void *oauth_init(Port *port, const char *selected_mech, const char *shadow_pass);
39 : : static int oauth_exchange(void *opaq, const char *input, int inputlen,
40 : : char **output, int *outputlen, const char **logdetail);
41 : :
42 : : static void load_validator_library(const char *libname);
43 : : static void shutdown_validator_library(void *arg);
44 : : static bool check_validator_hba_options(Port *port, const char **logdetail);
45 : :
46 : : static ValidatorModuleState *validator_module_state;
47 : : static const OAuthValidatorCallbacks *ValidatorCallbacks;
48 : :
49 : : static MemoryContext ValidatorMemoryContext;
50 : : static List *ValidatorOptions;
51 : : static bool ValidatorOptionsChecked;
52 : :
53 : : /* Mechanism declaration */
54 : : const pg_be_sasl_mech pg_be_oauth_mech = {
55 : : .get_mechanisms = oauth_get_mechanisms,
56 : : .init = oauth_init,
57 : : .exchange = oauth_exchange,
58 : :
59 : : .max_message_length = PG_MAX_AUTH_TOKEN_LENGTH,
60 : : };
61 : :
62 : : /* Valid states for the oauth_exchange() machine. */
63 : : enum oauth_state
64 : : {
65 : : OAUTH_STATE_INIT = 0,
66 : : OAUTH_STATE_ERROR,
67 : : OAUTH_STATE_ERROR_DISCOVERY,
68 : : OAUTH_STATE_FINISHED,
69 : : };
70 : :
71 : : /* Mechanism callback state. */
72 : : struct oauth_ctx
73 : : {
74 : : enum oauth_state state;
75 : : Port *port;
76 : : const char *issuer;
77 : : const char *scope;
78 : : };
79 : :
80 : : static char *sanitize_char(char c);
81 : : static char *parse_kvpairs_for_auth(char **input);
82 : : static void generate_error_response(struct oauth_ctx *ctx, char **output, int *outputlen);
83 : : static bool validate(Port *port, const char *auth, const char **logdetail);
84 : :
85 : : /* Constants seen in an OAUTHBEARER client initial response. */
86 : : #define KVSEP 0x01 /* separator byte for key/value pairs */
87 : : #define AUTH_KEY "auth" /* key containing the Authorization header */
88 : : #define BEARER_SCHEME "Bearer " /* required header scheme (case-insensitive!) */
89 : :
90 : : /*
91 : : * Retrieves the OAUTHBEARER mechanism list (currently a single item).
92 : : *
93 : : * For a full description of the API, see libpq/sasl.h.
94 : : */
95 : : static void
96 : 0 : oauth_get_mechanisms(Port *port, StringInfo buf)
97 : : {
98 : : /* Only OAUTHBEARER is supported. */
99 : 0 : appendStringInfoString(buf, OAUTHBEARER_NAME);
100 : 0 : appendStringInfoChar(buf, '\0');
101 : 0 : }
102 : :
103 : : /*
104 : : * Initializes mechanism state and loads the configured validator module.
105 : : *
106 : : * For a full description of the API, see libpq/sasl.h.
107 : : */
108 : : static void *
109 : 0 : oauth_init(Port *port, const char *selected_mech, const char *shadow_pass)
110 : : {
111 : : struct oauth_ctx *ctx;
112 : : char *err_msg;
113 : :
114 [ # # ]: 0 : if (strcmp(selected_mech, OAUTHBEARER_NAME) != 0)
115 [ # # ]: 0 : ereport(ERROR,
116 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
117 : : errmsg("client selected an invalid SASL authentication mechanism"));
118 : :
119 : : /* Save our memory context for later use by client API calls. */
120 : 0 : ValidatorMemoryContext = CurrentMemoryContext;
121 : :
122 : 0 : ctx = palloc0_object(struct oauth_ctx);
123 : :
124 : 0 : ctx->state = OAUTH_STATE_INIT;
125 : 0 : ctx->port = port;
126 : :
127 : : Assert(port->hba);
128 : 0 : ctx->issuer = port->hba->oauth_issuer;
129 : 0 : ctx->scope = port->hba->oauth_scope;
130 : :
131 : : /*
132 : : * The allowlist is otherwise only enforced when pg_hba.conf is parsed. If
133 : : * a change to oauth_validator_libraries caused the last reload to fail,
134 : : * we're still using the old HBA lines, and their validator may no longer
135 : : * be permitted. Check again before loading it. The details go to the
136 : : * server log only; the client just sees the usual authentication failure.
137 : : */
138 [ # # ]: 0 : if (!check_oauth_validator(port->hba, COMMERROR, &err_msg))
139 [ # # ]: 0 : ereport(ERROR,
140 : : errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
141 : : errmsg("OAuth bearer authentication failed for user \"%s\"",
142 : : port->user_name));
143 : :
144 : 0 : load_validator_library(port->hba->oauth_validator);
145 : :
146 : 0 : return ctx;
147 : : }
148 : :
149 : : /*
150 : : * Implements the OAUTHBEARER SASL exchange (RFC 7628, Sec. 3.2). This pulls
151 : : * apart the client initial response and validates the Bearer token. It also
152 : : * handles the dummy error response for a failed handshake, as described in
153 : : * Sec. 3.2.3.
154 : : *
155 : : * For a full description of the API, see libpq/sasl.h.
156 : : */
157 : : static int
158 : 0 : oauth_exchange(void *opaq, const char *input, int inputlen,
159 : : char **output, int *outputlen, const char **logdetail)
160 : : {
161 : : char *input_copy;
162 : : char *p;
163 : : char cbind_flag;
164 : : char *auth;
165 : : int status;
166 : :
167 : 0 : struct oauth_ctx *ctx = opaq;
168 : :
169 : 0 : *output = NULL;
170 : 0 : *outputlen = -1;
171 : :
172 : : /*
173 : : * If the client didn't include an "Initial Client Response" in the
174 : : * SASLInitialResponse message, send an empty challenge, to which the
175 : : * client will respond with the same data that usually comes in the
176 : : * Initial Client Response.
177 : : */
178 [ # # ]: 0 : if (input == NULL)
179 : : {
180 : : Assert(ctx->state == OAUTH_STATE_INIT);
181 : :
182 : 0 : *output = pstrdup("");
183 : 0 : *outputlen = 0;
184 : 0 : return PG_SASL_EXCHANGE_CONTINUE;
185 : : }
186 : :
187 : : /*
188 : : * Check that the input length agrees with the string length of the input.
189 : : */
190 [ # # ]: 0 : if (inputlen == 0)
191 [ # # ]: 0 : ereport(ERROR,
192 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
193 : : errmsg("malformed OAUTHBEARER message"),
194 : : errdetail("The message is empty."));
195 [ # # ]: 0 : if (inputlen != strlen(input))
196 [ # # ]: 0 : ereport(ERROR,
197 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
198 : : errmsg("malformed OAUTHBEARER message"),
199 : : errdetail("Message length does not match input length."));
200 : :
201 [ # # # ]: 0 : switch (ctx->state)
202 : : {
203 : 0 : case OAUTH_STATE_INIT:
204 : : /* Handle this case below. */
205 : 0 : break;
206 : :
207 : 0 : case OAUTH_STATE_ERROR:
208 : : case OAUTH_STATE_ERROR_DISCOVERY:
209 : :
210 : : /*
211 : : * Only one response is valid for the client during authentication
212 : : * failure: a single kvsep.
213 : : */
214 [ # # # # ]: 0 : if (inputlen != 1 || *input != KVSEP)
215 [ # # ]: 0 : ereport(ERROR,
216 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
217 : : errmsg("malformed OAUTHBEARER message"),
218 : : errdetail("Client did not send a kvsep response."));
219 : :
220 : : /*
221 : : * The (failed) handshake is now complete. Don't report discovery
222 : : * requests in the server log unless the log level is high enough.
223 : : */
224 [ # # ]: 0 : if (ctx->state == OAUTH_STATE_ERROR_DISCOVERY)
225 : : {
226 [ # # ]: 0 : ereport(DEBUG1, errmsg("OAuth issuer discovery requested"));
227 : :
228 : 0 : ctx->state = OAUTH_STATE_FINISHED;
229 : 0 : return PG_SASL_EXCHANGE_ABANDONED;
230 : : }
231 : :
232 : : /* We're not in discovery, so this is just a normal auth failure. */
233 : 0 : ctx->state = OAUTH_STATE_FINISHED;
234 : 0 : return PG_SASL_EXCHANGE_FAILURE;
235 : :
236 : 0 : default:
237 [ # # ]: 0 : elog(ERROR, "invalid OAUTHBEARER exchange state");
238 : : return PG_SASL_EXCHANGE_FAILURE;
239 : : }
240 : :
241 : : /* Handle the client's initial message. */
242 : 0 : p = input_copy = pstrdup(input);
243 : :
244 : : /*
245 : : * OAUTHBEARER does not currently define a channel binding (so there is no
246 : : * OAUTHBEARER-PLUS, and we do not accept a 'p' specifier). We accept a
247 : : * 'y' specifier purely for the remote chance that a future specification
248 : : * could define one; then future clients can still interoperate with this
249 : : * server implementation. 'n' is the expected case.
250 : : */
251 : 0 : cbind_flag = *p;
252 [ # # # ]: 0 : switch (cbind_flag)
253 : : {
254 : 0 : case 'p':
255 [ # # ]: 0 : ereport(ERROR,
256 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
257 : : errmsg("malformed OAUTHBEARER message"),
258 : : errdetail("The server does not support channel binding for OAuth, but the client message includes channel binding data."));
259 : : break;
260 : :
261 : 0 : case 'y': /* fall through */
262 : : case 'n':
263 : 0 : p++;
264 [ # # ]: 0 : if (*p != ',')
265 [ # # ]: 0 : ereport(ERROR,
266 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
267 : : errmsg("malformed OAUTHBEARER message"),
268 : : errdetail("Comma expected, but found character \"%s\".",
269 : : sanitize_char(*p)));
270 : 0 : p++;
271 : 0 : break;
272 : :
273 : 0 : default:
274 [ # # ]: 0 : ereport(ERROR,
275 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
276 : : errmsg("malformed OAUTHBEARER message"),
277 : : errdetail("Unexpected channel-binding flag \"%s\".",
278 : : sanitize_char(cbind_flag)));
279 : : }
280 : :
281 : : /*
282 : : * Forbid optional authzid (authorization identity). We don't support it.
283 : : */
284 [ # # ]: 0 : if (*p == 'a')
285 [ # # ]: 0 : ereport(ERROR,
286 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
287 : : errmsg("client uses authorization identity, but it is not supported"));
288 [ # # ]: 0 : if (*p != ',')
289 [ # # ]: 0 : ereport(ERROR,
290 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
291 : : errmsg("malformed OAUTHBEARER message"),
292 : : errdetail("Unexpected attribute \"%s\" in client-first-message.",
293 : : sanitize_char(*p)));
294 : 0 : p++;
295 : :
296 : : /* All remaining fields are separated by the RFC's kvsep (\x01). */
297 [ # # ]: 0 : if (*p != KVSEP)
298 [ # # ]: 0 : ereport(ERROR,
299 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
300 : : errmsg("malformed OAUTHBEARER message"),
301 : : errdetail("Key-value separator expected, but found character \"%s\".",
302 : : sanitize_char(*p)));
303 : 0 : p++;
304 : :
305 : 0 : auth = parse_kvpairs_for_auth(&p);
306 [ # # ]: 0 : if (!auth)
307 [ # # ]: 0 : ereport(ERROR,
308 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
309 : : errmsg("malformed OAUTHBEARER message"),
310 : : errdetail("Message does not contain an auth value."));
311 : :
312 : : /* We should be at the end of our message. */
313 [ # # ]: 0 : if (*p)
314 [ # # ]: 0 : ereport(ERROR,
315 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
316 : : errmsg("malformed OAUTHBEARER message"),
317 : : errdetail("Message contains additional data after the final terminator."));
318 : :
319 : : /*
320 : : * Make sure all custom HBA options are understood by the validator before
321 : : * continuing, since we couldn't check them during server start/reload.
322 : : */
323 [ # # ]: 0 : if (!check_validator_hba_options(ctx->port, logdetail))
324 : : {
325 : 0 : ctx->state = OAUTH_STATE_FINISHED;
326 : 0 : return PG_SASL_EXCHANGE_FAILURE;
327 : : }
328 : :
329 [ # # ]: 0 : if (auth[0] == '\0')
330 : : {
331 : : /*
332 : : * An empty auth value represents a discovery request; the client
333 : : * expects it to fail. Skip validation entirely and move directly to
334 : : * the error response.
335 : : */
336 : 0 : generate_error_response(ctx, output, outputlen);
337 : :
338 : 0 : ctx->state = OAUTH_STATE_ERROR_DISCOVERY;
339 : 0 : status = PG_SASL_EXCHANGE_CONTINUE;
340 : : }
341 [ # # ]: 0 : else if (!validate(ctx->port, auth, logdetail))
342 : : {
343 : 0 : generate_error_response(ctx, output, outputlen);
344 : :
345 : 0 : ctx->state = OAUTH_STATE_ERROR;
346 : 0 : status = PG_SASL_EXCHANGE_CONTINUE;
347 : : }
348 : : else
349 : : {
350 : 0 : ctx->state = OAUTH_STATE_FINISHED;
351 : 0 : status = PG_SASL_EXCHANGE_SUCCESS;
352 : : }
353 : :
354 : : /* Don't let extra copies of the bearer token hang around. */
355 : 0 : explicit_bzero(input_copy, inputlen);
356 : :
357 : 0 : return status;
358 : : }
359 : :
360 : : /*
361 : : * Convert an arbitrary byte to printable form. For error messages.
362 : : *
363 : : * If it's a printable ASCII character, print it as a single character.
364 : : * otherwise, print it in hex.
365 : : *
366 : : * The returned pointer points to a static buffer.
367 : : */
368 : : static char *
369 : 0 : sanitize_char(char c)
370 : : {
371 : : static char buf[5];
372 : :
373 [ # # # # ]: 0 : if (c >= 0x21 && c <= 0x7E)
374 : 0 : snprintf(buf, sizeof(buf), "'%c'", c);
375 : : else
376 : 0 : snprintf(buf, sizeof(buf), "0x%02x", (unsigned char) c);
377 : 0 : return buf;
378 : : }
379 : :
380 : : /*
381 : : * Performs syntactic validation of a key and value from the initial client
382 : : * response. (Semantic validation of interesting values must be performed
383 : : * later.)
384 : : */
385 : : static void
386 : 0 : validate_kvpair(const char *key, const char *val)
387 : : {
388 : : /*-----
389 : : * From Sec 3.1:
390 : : * key = 1*(ALPHA)
391 : : */
392 : : static const char *key_allowed_set =
393 : : "abcdefghijklmnopqrstuvwxyz"
394 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
395 : :
396 : : size_t span;
397 : :
398 [ # # ]: 0 : if (!key[0])
399 [ # # ]: 0 : ereport(ERROR,
400 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
401 : : errmsg("malformed OAUTHBEARER message"),
402 : : errdetail("Message contains an empty key name."));
403 : :
404 : 0 : span = strspn(key, key_allowed_set);
405 [ # # ]: 0 : if (key[span] != '\0')
406 [ # # ]: 0 : ereport(ERROR,
407 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
408 : : errmsg("malformed OAUTHBEARER message"),
409 : : errdetail("Message contains an invalid key name."));
410 : :
411 : : /*-----
412 : : * From Sec 3.1:
413 : : * value = *(VCHAR / SP / HTAB / CR / LF )
414 : : *
415 : : * The VCHAR (visible character) class is large; a loop is more
416 : : * straightforward than strspn().
417 : : */
418 [ # # ]: 0 : for (; *val; ++val)
419 : : {
420 [ # # # # ]: 0 : if (0x21 <= *val && *val <= 0x7E)
421 : 0 : continue; /* VCHAR */
422 : :
423 [ # # ]: 0 : switch (*val)
424 : : {
425 : 0 : case ' ':
426 : : case '\t':
427 : : case '\r':
428 : : case '\n':
429 : 0 : continue; /* SP, HTAB, CR, LF */
430 : :
431 : 0 : default:
432 [ # # ]: 0 : ereport(ERROR,
433 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
434 : : errmsg("malformed OAUTHBEARER message"),
435 : : errdetail("Message contains an invalid value."));
436 : : }
437 : : }
438 : 0 : }
439 : :
440 : : /*
441 : : * Consumes all kvpairs in an OAUTHBEARER exchange message. If the "auth" key is
442 : : * found, its value is returned.
443 : : */
444 : : static char *
445 : 0 : parse_kvpairs_for_auth(char **input)
446 : : {
447 : 0 : char *pos = *input;
448 : 0 : char *auth = NULL;
449 : :
450 : : /*----
451 : : * The relevant ABNF, from Sec. 3.1:
452 : : *
453 : : * kvsep = %x01
454 : : * key = 1*(ALPHA)
455 : : * value = *(VCHAR / SP / HTAB / CR / LF )
456 : : * kvpair = key "=" value kvsep
457 : : * ;;gs2-header = See RFC 5801
458 : : * client-resp = (gs2-header kvsep *kvpair kvsep) / kvsep
459 : : *
460 : : * By the time we reach this code, the gs2-header and initial kvsep have
461 : : * already been validated. We start at the beginning of the first kvpair.
462 : : */
463 : :
464 [ # # ]: 0 : while (*pos)
465 : : {
466 : : char *end;
467 : : char *sep;
468 : : char *key;
469 : : char *value;
470 : :
471 : : /*
472 : : * Find the end of this kvpair. Note that input is null-terminated by
473 : : * the SASL code, so the strchr() is bounded.
474 : : */
475 : 0 : end = strchr(pos, KVSEP);
476 [ # # ]: 0 : if (!end)
477 [ # # ]: 0 : ereport(ERROR,
478 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
479 : : errmsg("malformed OAUTHBEARER message"),
480 : : errdetail("Message contains an unterminated key/value pair."));
481 : 0 : *end = '\0';
482 : :
483 [ # # ]: 0 : if (pos == end)
484 : : {
485 : : /* Empty kvpair, signifying the end of the list. */
486 : 0 : *input = pos + 1;
487 : 0 : return auth;
488 : : }
489 : :
490 : : /*
491 : : * Find the end of the key name.
492 : : */
493 : 0 : sep = strchr(pos, '=');
494 [ # # ]: 0 : if (!sep)
495 [ # # ]: 0 : ereport(ERROR,
496 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
497 : : errmsg("malformed OAUTHBEARER message"),
498 : : errdetail("Message contains a key without a value."));
499 : 0 : *sep = '\0';
500 : :
501 : : /* Both key and value are now safely terminated. */
502 : 0 : key = pos;
503 : 0 : value = sep + 1;
504 : 0 : validate_kvpair(key, value);
505 : :
506 [ # # ]: 0 : if (strcmp(key, AUTH_KEY) == 0)
507 : : {
508 [ # # ]: 0 : if (auth)
509 [ # # ]: 0 : ereport(ERROR,
510 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
511 : : errmsg("malformed OAUTHBEARER message"),
512 : : errdetail("Message contains multiple auth values."));
513 : :
514 : 0 : auth = value;
515 : : }
516 : : else
517 : : {
518 : : /*
519 : : * The RFC also defines the host and port keys, but they are not
520 : : * required for OAUTHBEARER and we do not use them. Also, per Sec.
521 : : * 3.1, any key/value pairs we don't recognize must be ignored.
522 : : */
523 : : }
524 : :
525 : : /* Move to the next pair. */
526 : 0 : pos = end + 1;
527 : : }
528 : :
529 [ # # ]: 0 : ereport(ERROR,
530 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
531 : : errmsg("malformed OAUTHBEARER message"),
532 : : errdetail("Message did not contain a final terminator."));
533 : :
534 : : pg_unreachable();
535 : : return NULL;
536 : : }
537 : :
538 : : /*
539 : : * Builds the JSON response for failed authentication (RFC 7628, Sec. 3.2.2).
540 : : * This contains the required scopes for entry and a pointer to the OAuth/OpenID
541 : : * discovery document, which the client may use to conduct its OAuth flow.
542 : : */
543 : : static void
544 : 0 : generate_error_response(struct oauth_ctx *ctx, char **output, int *outputlen)
545 : : {
546 : : StringInfoData buf;
547 : : StringInfoData issuer;
548 : :
549 : : /*
550 : : * The admin needs to set an issuer and scope for OAuth to work. There's
551 : : * not really a way to hide this from the user, either, because we can't
552 : : * choose a "default" issuer, so be honest in the failure message. (In
553 : : * practice such configurations are rejected during HBA parsing.)
554 : : */
555 [ # # # # ]: 0 : if (!ctx->issuer || !ctx->scope)
556 [ # # ]: 0 : ereport(FATAL,
557 : : errcode(ERRCODE_INTERNAL_ERROR),
558 : : errmsg("OAuth is not properly configured for this user"),
559 : : errdetail_log("The options \"issuer\" and \"scope\" must be set in pg_hba.conf."));
560 : :
561 : : /*
562 : : * Build a default .well-known URI based on our issuer, unless the HBA has
563 : : * already provided one.
564 : : */
565 : 0 : initStringInfo(&issuer);
566 : 0 : appendStringInfoString(&issuer, ctx->issuer);
567 [ # # ]: 0 : if (strstr(ctx->issuer, "/.well-known/") == NULL)
568 : 0 : appendStringInfoString(&issuer, "/.well-known/openid-configuration");
569 : :
570 : 0 : initStringInfo(&buf);
571 : :
572 : : /*
573 : : * Escaping the string here is belt-and-suspenders defensive programming
574 : : * since escapable characters aren't valid in either the issuer URI or the
575 : : * scope list, but the HBA doesn't enforce that yet.
576 : : */
577 : 0 : appendStringInfoString(&buf, "{ \"status\": \"invalid_token\", ");
578 : :
579 : 0 : appendStringInfoString(&buf, "\"openid-configuration\": ");
580 : 0 : escape_json(&buf, issuer.data);
581 : 0 : pfree(issuer.data);
582 : :
583 : 0 : appendStringInfoString(&buf, ", \"scope\": ");
584 : 0 : escape_json(&buf, ctx->scope);
585 : :
586 : 0 : appendStringInfoString(&buf, " }");
587 : :
588 : 0 : *output = buf.data;
589 : 0 : *outputlen = buf.len;
590 : 0 : }
591 : :
592 : : /*-----
593 : : * Validates the provided Authorization header and returns the token from
594 : : * within it. NULL is returned on validation failure.
595 : : *
596 : : * Only Bearer tokens are accepted. The ABNF is defined in RFC 6750, Sec.
597 : : * 2.1:
598 : : *
599 : : * b64token = 1*( ALPHA / DIGIT /
600 : : * "-" / "." / "_" / "~" / "+" / "/" ) *"="
601 : : * credentials = "Bearer" 1*SP b64token
602 : : *
603 : : * The "credentials" construction is what we receive in our auth value.
604 : : *
605 : : * Since that spec is subordinate to HTTP (i.e. the HTTP Authorization
606 : : * header format; RFC 9110 Sec. 11), the "Bearer" scheme string must be
607 : : * compared case-insensitively. (This is not mentioned in RFC 6750, but the
608 : : * OAUTHBEARER spec points it out: RFC 7628 Sec. 4.)
609 : : *
610 : : * Invalid formats are technically a protocol violation, but we shouldn't
611 : : * reflect any information about the sensitive Bearer token back to the
612 : : * client; log at COMMERROR instead.
613 : : */
614 : : static const char *
615 : 0 : validate_token_format(const char *header)
616 : : {
617 : : size_t span;
618 : : const char *token;
619 : : static const char *const b64token_allowed_set =
620 : : "abcdefghijklmnopqrstuvwxyz"
621 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
622 : : "0123456789-._~+/";
623 : :
624 : : /* Missing auth headers should be handled by the caller. */
625 : : Assert(header);
626 : : /* Empty auth (discovery) should be handled before calling validate(). */
627 : : Assert(header[0] != '\0');
628 : :
629 [ # # ]: 0 : if (pg_strncasecmp(header, BEARER_SCHEME, strlen(BEARER_SCHEME)))
630 : : {
631 [ # # ]: 0 : ereport(COMMERROR,
632 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
633 : : errmsg("malformed OAuth bearer token"),
634 : : errdetail_log("Client response indicated a non-Bearer authentication scheme."));
635 : 0 : return NULL;
636 : : }
637 : :
638 : : /* Pull the bearer token out of the auth value. */
639 : 0 : token = header + strlen(BEARER_SCHEME);
640 : :
641 : : /* Swallow any additional spaces. */
642 [ # # ]: 0 : while (*token == ' ')
643 : 0 : token++;
644 : :
645 : : /* Tokens must not be empty. */
646 [ # # ]: 0 : if (!*token)
647 : : {
648 [ # # ]: 0 : ereport(COMMERROR,
649 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
650 : : errmsg("malformed OAuth bearer token"),
651 : : errdetail_log("Bearer token is empty."));
652 : 0 : return NULL;
653 : : }
654 : :
655 : : /*
656 : : * Make sure the token contains only allowed characters. Tokens may end
657 : : * with any number of '=' characters.
658 : : */
659 : 0 : span = strspn(token, b64token_allowed_set);
660 [ # # ]: 0 : while (token[span] == '=')
661 : 0 : span++;
662 : :
663 [ # # ]: 0 : if (token[span] != '\0')
664 : : {
665 : : /*
666 : : * This error message could be more helpful by printing the
667 : : * problematic character(s), but that'd be a bit like printing a piece
668 : : * of someone's password into the logs.
669 : : */
670 [ # # ]: 0 : ereport(COMMERROR,
671 : : errcode(ERRCODE_PROTOCOL_VIOLATION),
672 : : errmsg("malformed OAuth bearer token"),
673 : : errdetail_log("Bearer token is not in the correct format."));
674 : 0 : return NULL;
675 : : }
676 : :
677 : 0 : return token;
678 : : }
679 : :
680 : : /*
681 : : * Checks that the "auth" kvpair in the client response contains a syntactically
682 : : * valid Bearer token, then passes it along to the loaded validator module for
683 : : * authorization. Returns true if validation succeeds.
684 : : */
685 : : static bool
686 : 0 : validate(Port *port, const char *auth, const char **logdetail)
687 : : {
688 : : int map_status;
689 : : ValidatorModuleResult *ret;
690 : : const char *token;
691 : : bool status;
692 : :
693 : : /* Ensure that we have a correct token to validate */
694 [ # # ]: 0 : if (!(token = validate_token_format(auth)))
695 : 0 : return false;
696 : :
697 : : /*
698 : : * Ensure that we have a validation library loaded, this should always be
699 : : * the case and an error here is indicative of a bug.
700 : : */
701 [ # # # # ]: 0 : if (!ValidatorCallbacks || !ValidatorCallbacks->validate_cb)
702 [ # # ]: 0 : ereport(FATAL,
703 : : errcode(ERRCODE_INTERNAL_ERROR),
704 : : errmsg("validation of OAuth token requested without a validator loaded"));
705 : :
706 : : /* Call the validation function from the validator module */
707 : 0 : ret = palloc0_object(ValidatorModuleResult);
708 [ # # ]: 0 : if (!ValidatorCallbacks->validate_cb(validator_module_state, token,
709 : 0 : port->user_name, ret))
710 : : {
711 [ # # # # ]: 0 : ereport(WARNING,
712 : : errcode(ERRCODE_INTERNAL_ERROR),
713 : : errmsg("internal error in OAuth validator module"),
714 : : ret->error_detail ? errdetail_log("%s", ret->error_detail) : 0);
715 : :
716 : 0 : *logdetail = ret->error_detail;
717 : 0 : return false;
718 : : }
719 : :
720 : : /*
721 : : * Log any authentication results even if the token isn't authorized; it
722 : : * might be useful for auditing or troubleshooting.
723 : : */
724 [ # # ]: 0 : if (ret->authn_id)
725 : 0 : set_authn_id(port, ret->authn_id);
726 : :
727 [ # # ]: 0 : if (!ret->authorized)
728 : : {
729 [ # # ]: 0 : if (ret->error_detail)
730 : 0 : *logdetail = ret->error_detail;
731 : : else
732 : 0 : *logdetail = _("Validator failed to authorize the provided token.");
733 : :
734 : 0 : status = false;
735 : 0 : goto cleanup;
736 : : }
737 : :
738 [ # # ]: 0 : if (port->hba->oauth_skip_usermap)
739 : : {
740 : : /*
741 : : * If the validator is our authorization authority, we're done.
742 : : * Authentication may or may not have been performed depending on the
743 : : * validator implementation; all that matters is that the validator
744 : : * says the user can log in with the target role.
745 : : */
746 : 0 : status = true;
747 : 0 : goto cleanup;
748 : : }
749 : :
750 : : /* Make sure the validator authenticated the user. */
751 [ # # # # ]: 0 : if (ret->authn_id == NULL || ret->authn_id[0] == '\0')
752 : : {
753 : 0 : *logdetail = _("Validator provided no identity.");
754 : :
755 : 0 : status = false;
756 : 0 : goto cleanup;
757 : : }
758 : :
759 : : /* Finally, check the user map. */
760 : 0 : map_status = check_usermap(port->hba->usermap, port->user_name,
761 : : MyClientConnectionInfo.authn_id, false);
762 : 0 : status = (map_status == STATUS_OK);
763 : :
764 : 0 : cleanup:
765 : :
766 : : /*
767 : : * Clear and free the validation result from the validator module once
768 : : * we're done with it.
769 : : */
770 [ # # ]: 0 : if (ret->authn_id != NULL)
771 : 0 : pfree(ret->authn_id);
772 : 0 : pfree(ret);
773 : :
774 : 0 : return status;
775 : : }
776 : :
777 : : /*
778 : : * load_validator_library
779 : : *
780 : : * Load the configured validator library in order to perform token validation.
781 : : * There is no built-in fallback since validation is implementation specific. If
782 : : * no validator library is configured, or if it fails to load, then error out
783 : : * since token validation won't be possible.
784 : : */
785 : : static void
786 : 0 : load_validator_library(const char *libname)
787 : : {
788 : : OAuthValidatorModuleInit validator_init;
789 : : MemoryContextCallback *mcb;
790 : :
791 : : /*
792 : : * The presence, and validity, of libname has already been established by
793 : : * check_oauth_validator so we don't need to perform more than Assert
794 : : * level checking here.
795 : : */
796 : : Assert(libname && *libname);
797 : :
798 : 0 : validator_init = (OAuthValidatorModuleInit)
799 : 0 : load_external_function(libname, "_PG_oauth_validator_module_init",
800 : : false, NULL);
801 : :
802 : : /*
803 : : * The validator init function is required since it will set the callbacks
804 : : * for the validator library.
805 : : */
806 [ # # ]: 0 : if (validator_init == NULL)
807 [ # # ]: 0 : ereport(ERROR,
808 : : errmsg("OAuth validator module \"%s\" must define the symbol \"%s\"",
809 : : libname, "_PG_oauth_validator_module_init"));
810 : :
811 : 0 : ValidatorCallbacks = (*validator_init) ();
812 : : Assert(ValidatorCallbacks);
813 : :
814 : : /*
815 : : * Check the magic number, to protect against break-glass scenarios where
816 : : * the ABI must change within a major version. load_external_function()
817 : : * already checks for compatibility across major versions.
818 : : */
819 [ # # ]: 0 : if (ValidatorCallbacks->magic != PG_OAUTH_VALIDATOR_MAGIC)
820 [ # # ]: 0 : ereport(ERROR,
821 : : errmsg("OAuth validator module \"%s\": magic number mismatch",
822 : : libname),
823 : : errdetail("Server has magic number 0x%08X, module has 0x%08X.",
824 : : PG_OAUTH_VALIDATOR_MAGIC, ValidatorCallbacks->magic));
825 : :
826 : : /*
827 : : * Make sure all required callbacks are present in the ValidatorCallbacks
828 : : * structure. Right now only the validation callback is required.
829 : : */
830 [ # # ]: 0 : if (ValidatorCallbacks->validate_cb == NULL)
831 [ # # ]: 0 : ereport(ERROR,
832 : : errmsg("OAuth validator module \"%s\" must provide a \"%s\" callback",
833 : : libname, "validate_cb"));
834 : :
835 : : /* Allocate memory for validator library private state data */
836 : 0 : validator_module_state = palloc0_object(ValidatorModuleState);
837 : 0 : validator_module_state->sversion = PG_VERSION_NUM;
838 : :
839 [ # # ]: 0 : if (ValidatorCallbacks->startup_cb != NULL)
840 : 0 : ValidatorCallbacks->startup_cb(validator_module_state);
841 : :
842 : : /* Shut down the library before cleaning up its state. */
843 : 0 : mcb = palloc0_object(MemoryContextCallback);
844 : 0 : mcb->func = shutdown_validator_library;
845 : :
846 : 0 : MemoryContextRegisterResetCallback(CurrentMemoryContext, mcb);
847 : 0 : }
848 : :
849 : : /*
850 : : * Call the validator module's shutdown callback, if one is provided. This is
851 : : * invoked during memory context reset.
852 : : */
853 : : static void
854 : 0 : shutdown_validator_library(void *arg)
855 : : {
856 [ # # ]: 0 : if (ValidatorCallbacks->shutdown_cb != NULL)
857 : 0 : ValidatorCallbacks->shutdown_cb(validator_module_state);
858 : :
859 : : /* The backing memory for this is about to disappear. */
860 : 0 : ValidatorOptions = NIL;
861 : 0 : }
862 : :
863 : : /*
864 : : * Ensure an OAuth validator named in the HBA is permitted by the configuration.
865 : : *
866 : : * If the validator is currently unset and exactly one library is declared in
867 : : * oauth_validator_libraries, then that library will be used as the validator.
868 : : * Otherwise the name must be present in the list of oauth_validator_libraries.
869 : : */
870 : : bool
871 : 0 : check_oauth_validator(HbaLine *hbaline, int elevel, char **err_msg)
872 : : {
873 : 0 : int line_num = hbaline->linenumber;
874 : 0 : const char *file_name = hbaline->sourcefile;
875 : : char *rawstring;
876 : 0 : List *elemlist = NIL;
877 : :
878 : 0 : *err_msg = NULL;
879 : :
880 : : /* SplitDirectoriesString needs a modifiable copy */
881 : : Assert(oauth_validator_libraries_string != NULL);
882 : 0 : rawstring = pstrdup(oauth_validator_libraries_string);
883 : :
884 [ # # ]: 0 : if (!SplitDirectoriesString(rawstring, ',', &elemlist))
885 : : {
886 : : /* syntax error in list */
887 [ # # ]: 0 : ereport(elevel,
888 : : errcode(ERRCODE_CONFIG_FILE_ERROR),
889 : : errmsg("invalid list syntax in parameter \"%s\"",
890 : : "oauth_validator_libraries"));
891 : 0 : *err_msg = psprintf("invalid list syntax in parameter \"%s\"",
892 : : "oauth_validator_libraries");
893 : 0 : goto done;
894 : : }
895 : :
896 [ # # ]: 0 : if (elemlist == NIL)
897 : : {
898 [ # # ]: 0 : ereport(elevel,
899 : : errcode(ERRCODE_CONFIG_FILE_ERROR),
900 : : errmsg("parameter \"%s\" must be set for authentication method \"%s\"",
901 : : "oauth_validator_libraries", "oauth"),
902 : : errcontext("line %d of configuration file \"%s\"",
903 : : line_num, file_name));
904 : 0 : *err_msg = psprintf("parameter \"%s\" must be set for authentication method \"%s\"",
905 : : "oauth_validator_libraries", "oauth");
906 : 0 : goto done;
907 : : }
908 : :
909 [ # # # # : 0 : foreach_ptr(char, allowed, elemlist)
# # ]
910 : : {
911 [ # # ]: 0 : if (allowed[0] == '\0')
912 : : {
913 [ # # ]: 0 : ereport(elevel,
914 : : errcode(ERRCODE_CONFIG_FILE_ERROR),
915 : : errmsg("invalid list syntax in parameter \"%s\"",
916 : : "oauth_validator_libraries"),
917 : : errcontext("line %d of configuration file \"%s\"",
918 : : line_num, file_name));
919 : 0 : *err_msg = psprintf("invalid list syntax in parameter \"%s\"",
920 : : "oauth_validator_libraries");
921 : 0 : goto done;
922 : : }
923 : : }
924 : :
925 [ # # ]: 0 : if (!hbaline->oauth_validator)
926 : : {
927 [ # # ]: 0 : if (elemlist->length == 1)
928 : : {
929 : 0 : hbaline->oauth_validator = pstrdup(linitial(elemlist));
930 : 0 : goto done;
931 : : }
932 : :
933 [ # # ]: 0 : ereport(elevel,
934 : : errcode(ERRCODE_CONFIG_FILE_ERROR),
935 : : errmsg("authentication method \"oauth\" requires option \"validator\" to be set when \"%s\" contains multiple options",
936 : : "oauth_validator_libraries"),
937 : : errcontext("line %d of configuration file \"%s\"",
938 : : line_num, file_name));
939 : 0 : *err_msg = psprintf("authentication method \"oauth\" requires option \"validator\" to be set when \"%s\" contains multiple options",
940 : : "oauth_validator_libraries");
941 : 0 : goto done;
942 : : }
943 : :
944 [ # # # # : 0 : foreach_ptr(char, allowed, elemlist)
# # ]
945 : : {
946 [ # # ]: 0 : if (strcmp(allowed, hbaline->oauth_validator) == 0)
947 : 0 : goto done;
948 : : }
949 : :
950 [ # # ]: 0 : ereport(elevel,
951 : : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
952 : : errmsg("validator \"%s\" is not permitted by \"%s\"",
953 : : hbaline->oauth_validator, "oauth_validator_libraries"),
954 : : errcontext("line %d of configuration file \"%s\"",
955 : : line_num, file_name));
956 : 0 : *err_msg = psprintf("validator \"%s\" is not permitted by \"%s\"",
957 : : hbaline->oauth_validator, "oauth_validator_libraries");
958 : :
959 : 0 : done:
960 : 0 : list_free_deep(elemlist);
961 : 0 : pfree(rawstring);
962 : :
963 : 0 : return (*err_msg == NULL);
964 : : }
965 : :
966 : : /*
967 : : * Client APIs for validator implementations
968 : : *
969 : : * Since we're currently not threaded, we only allow one validator in the
970 : : * process at a time. So we can make use of globals for now instead of looking
971 : : * up information using the state pointer. We probably shouldn't assume that the
972 : : * module hasn't temporarily changed memory contexts on us, though; functions
973 : : * here should defensively use an appropriate context when making global
974 : : * allocations.
975 : : */
976 : :
977 : : /*
978 : : * Adds to the list of allowed validator.* HBA options. Used during the
979 : : * startup_cb.
980 : : */
981 : : void
982 : 0 : RegisterOAuthHBAOptions(ValidatorModuleState *state, int num,
983 : : const char *opts[])
984 : : {
985 : : MemoryContext oldcontext;
986 : :
987 [ # # ]: 0 : if (!state)
988 : : {
989 : : Assert(false);
990 : 0 : return;
991 : : }
992 : :
993 : 0 : oldcontext = MemoryContextSwitchTo(ValidatorMemoryContext);
994 : :
995 [ # # ]: 0 : for (int i = 0; i < num; i++)
996 : : {
997 [ # # ]: 0 : if (!valid_oauth_hba_option_name(opts[i]))
998 : : {
999 : : /*
1000 : : * The user can't set this option in the HBA, so GetOAuthHBAOption
1001 : : * would always return NULL.
1002 : : */
1003 [ # # ]: 0 : ereport(WARNING,
1004 : : errmsg("HBA option name \"%s\" is invalid and will be ignored",
1005 : : opts[i]),
1006 : : /* translator: the second %s is a function name */
1007 : : errcontext("validator module \"%s\", in call to %s",
1008 : : MyProcPort->hba->oauth_validator,
1009 : : "RegisterOAuthHBAOptions"));
1010 : 0 : continue;
1011 : : }
1012 : :
1013 : 0 : ValidatorOptions = lappend(ValidatorOptions, pstrdup(opts[i]));
1014 : : }
1015 : :
1016 : 0 : MemoryContextSwitchTo(oldcontext);
1017 : :
1018 : : /*
1019 : : * Wait to validate the HBA against the registered options until later
1020 : : * (see check_validator_hba_options()).
1021 : : *
1022 : : * Delaying allows the validator to make multiple registration calls, to
1023 : : * append to the list; it lets us make the check in a place where we can
1024 : : * report the error without leaking details to the client; and it avoids
1025 : : * exporting the order of operations between HBA matching and the
1026 : : * startup_cb call as an API guarantee. (The last issue may become
1027 : : * relevant with a threaded model.)
1028 : : */
1029 : : }
1030 : :
1031 : : /*
1032 : : * Restrict the names available to custom HBA options, so that we don't
1033 : : * accidentally prevent future syntax extensions to HBA files.
1034 : : */
1035 : : bool
1036 : 0 : valid_oauth_hba_option_name(const char *name)
1037 : : {
1038 : : /*
1039 : : * This list is not incredibly principled, since the goal is just to bound
1040 : : * compatibility guarantees for our HBA parser. Alphanumerics seem
1041 : : * obviously fine, and it's difficult to argue against the punctuation
1042 : : * that's already included in some HBA option names and identifiers.
1043 : : */
1044 : : static const char *name_allowed_set =
1045 : : "abcdefghijklmnopqrstuvwxyz"
1046 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1047 : : "0123456789_-";
1048 : :
1049 : : size_t span;
1050 : :
1051 [ # # ]: 0 : if (!name[0])
1052 : 0 : return false;
1053 : :
1054 : 0 : span = strspn(name, name_allowed_set);
1055 : 0 : return name[span] == '\0';
1056 : : }
1057 : :
1058 : : /*
1059 : : * Verifies that all validator.* HBA options specified by the user were actually
1060 : : * registered by the validator library in use.
1061 : : */
1062 : : static bool
1063 : 0 : check_validator_hba_options(Port *port, const char **logdetail)
1064 : : {
1065 : 0 : HbaLine *hba = port->hba;
1066 : :
1067 [ # # # # : 0 : foreach_ptr(char, key, hba->oauth_opt_keys)
# # ]
1068 : : {
1069 : 0 : bool found = false;
1070 : :
1071 : : /* O(n^2) shouldn't be a problem here in practice. */
1072 [ # # # # : 0 : foreach_ptr(char, optname, ValidatorOptions)
# # ]
1073 : : {
1074 [ # # ]: 0 : if (strcmp(key, optname) == 0)
1075 : : {
1076 : 0 : found = true;
1077 : 0 : break;
1078 : : }
1079 : : }
1080 : :
1081 [ # # ]: 0 : if (!found)
1082 : : {
1083 : : /*
1084 : : * Unknown option name. Mirror the error messages in hba.c here,
1085 : : * keeping in mind that the original "validator." prefix was
1086 : : * stripped from the key during parsing.
1087 : : *
1088 : : * Since this is affecting live connections, which is unusual for
1089 : : * HBA, be noisy with a WARNING. (Warnings aren't sent to clients
1090 : : * prior to successful authentication, so this won't disclose the
1091 : : * server config.) It'll duplicate some of the information in the
1092 : : * logdetail, but that should make it hard to miss the connection
1093 : : * between the two.
1094 : : */
1095 : 0 : char *name = psprintf("validator.%s", key);
1096 : :
1097 : 0 : *logdetail = psprintf(_("unrecognized authentication option name: \"%s\""),
1098 : : name);
1099 [ # # ]: 0 : ereport(WARNING,
1100 : : errcode(ERRCODE_CONFIG_FILE_ERROR),
1101 : : errmsg("unrecognized authentication option name: \"%s\"",
1102 : : name),
1103 : : /* translator: the first %s is the name of the module */
1104 : : errdetail("The installed validator module (\"%s\") did not define an option named \"%s\".",
1105 : : hba->oauth_validator, key),
1106 : : errhint("All OAuth connections matching this line will fail. Correct the option and reload the server configuration."),
1107 : : errcontext("line %d of configuration file \"%s\"",
1108 : : hba->linenumber, hba->sourcefile));
1109 : :
1110 : 0 : return false;
1111 : : }
1112 : : }
1113 : :
1114 : 0 : ValidatorOptionsChecked = true; /* unfetter GetOAuthHBAOption() */
1115 : 0 : return true;
1116 : : }
1117 : :
1118 : : /*
1119 : : * Retrieves the setting for a validator.* HBA option, or NULL if not found.
1120 : : * This may only be used during the validate_cb and shutdown_cb.
1121 : : */
1122 : : const char *
1123 : 0 : GetOAuthHBAOption(const ValidatorModuleState *state, const char *optname)
1124 : : {
1125 : 0 : HbaLine *hba = MyProcPort->hba;
1126 : : ListCell *lc_k;
1127 : : ListCell *lc_v;
1128 : 0 : const char *ret = NULL;
1129 : :
1130 [ # # ]: 0 : if (!ValidatorOptionsChecked)
1131 : : {
1132 : : /*
1133 : : * Prevent the startup_cb from retrieving HBA options that it has just
1134 : : * registered. This probably seems strange -- why refuse to hand out
1135 : : * information we already know? -- but this lets us reserve the
1136 : : * ability to perform the startup_cb call earlier, before we know
1137 : : * which HBA line is matched by a connection, without breaking this
1138 : : * API.
1139 : : */
1140 : 0 : return NULL;
1141 : : }
1142 : :
1143 [ # # # # ]: 0 : if (!state || !hba)
1144 : : {
1145 : : Assert(false);
1146 : 0 : return NULL;
1147 : : }
1148 : :
1149 : : Assert(list_length(hba->oauth_opt_keys) == list_length(hba->oauth_opt_vals));
1150 : :
1151 [ # # # # : 0 : forboth(lc_k, hba->oauth_opt_keys, lc_v, hba->oauth_opt_vals)
# # # # #
# # # #
# ]
1152 : : {
1153 : 0 : const char *key = lfirst(lc_k);
1154 : 0 : const char *val = lfirst(lc_v);
1155 : :
1156 [ # # ]: 0 : if (strcmp(key, optname) == 0)
1157 : : {
1158 : : /*
1159 : : * Don't return yet -- when regular HBA options are specified more
1160 : : * than once, the last one wins. Do the same for these options.
1161 : : */
1162 : 0 : ret = val;
1163 : : }
1164 : : }
1165 : :
1166 : 0 : return ret;
1167 : : }
|