Move routines to manipulate WAL into PostgreSQL::Test::Cluster
[pgsql.git] / src / interfaces / libpq / fe-auth.c
blob7e478489b71a1451b79841d58d063a3c2e0a080d
1 /*-------------------------------------------------------------------------
3 * fe-auth.c
4 * The front-end (client) authorization routines
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * IDENTIFICATION
10 * src/interfaces/libpq/fe-auth.c
12 *-------------------------------------------------------------------------
16 * INTERFACE ROUTINES
17 * frontend (client) routines:
18 * pg_fe_sendauth send authentication information
19 * pg_fe_getauthname get user's name according to the client side
20 * of the authentication system
23 #include "postgres_fe.h"
25 #ifdef WIN32
26 #include "win32.h"
27 #else
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <sys/param.h> /* for MAXHOSTNAMELEN on most */
33 #include <sys/socket.h>
34 #ifdef HAVE_SYS_UCRED_H
35 #include <sys/ucred.h>
36 #endif
37 #ifndef MAXHOSTNAMELEN
38 #include <netdb.h> /* for MAXHOSTNAMELEN on some */
39 #endif
40 #endif
42 #include "common/md5.h"
43 #include "common/scram-common.h"
44 #include "fe-auth.h"
45 #include "fe-auth-sasl.h"
46 #include "libpq-fe.h"
48 #ifdef ENABLE_GSS
50 * GSSAPI authentication system.
53 #include "fe-gssapi-common.h"
56 * Continue GSS authentication with next token as needed.
58 static int
59 pg_GSS_continue(PGconn *conn, int payloadlen)
61 OM_uint32 maj_stat,
62 min_stat,
63 lmin_s,
64 gss_flags = GSS_C_MUTUAL_FLAG;
65 gss_buffer_desc ginbuf;
66 gss_buffer_desc goutbuf;
69 * On first call, there's no input token. On subsequent calls, read the
70 * input token into a GSS buffer.
72 if (conn->gctx != GSS_C_NO_CONTEXT)
74 ginbuf.length = payloadlen;
75 ginbuf.value = malloc(payloadlen);
76 if (!ginbuf.value)
78 libpq_append_conn_error(conn, "out of memory allocating GSSAPI buffer (%d)",
79 payloadlen);
80 return STATUS_ERROR;
82 if (pqGetnchar(ginbuf.value, payloadlen, conn))
85 * Shouldn't happen, because the caller should've ensured that the
86 * whole message is already in the input buffer.
88 free(ginbuf.value);
89 return STATUS_ERROR;
92 else
94 ginbuf.length = 0;
95 ginbuf.value = NULL;
98 /* finished parsing, trace server-to-client message */
99 if (conn->Pfdebug)
100 pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false);
102 /* Only try to acquire credentials if GSS delegation isn't disabled. */
103 if (!pg_GSS_have_cred_cache(&conn->gcred))
104 conn->gcred = GSS_C_NO_CREDENTIAL;
106 if (conn->gssdelegation && conn->gssdelegation[0] == '1')
107 gss_flags |= GSS_C_DELEG_FLAG;
109 maj_stat = gss_init_sec_context(&min_stat,
110 conn->gcred,
111 &conn->gctx,
112 conn->gtarg_nam,
113 GSS_C_NO_OID,
114 gss_flags,
116 GSS_C_NO_CHANNEL_BINDINGS,
117 (ginbuf.value == NULL) ? GSS_C_NO_BUFFER : &ginbuf,
118 NULL,
119 &goutbuf,
120 NULL,
121 NULL);
123 free(ginbuf.value);
125 if (goutbuf.length != 0)
128 * GSS generated data to send to the server. We don't care if it's the
129 * first or subsequent packet, just send the same kind of password
130 * packet.
132 conn->current_auth_response = AUTH_RESPONSE_GSS;
133 if (pqPacketSend(conn, PqMsg_GSSResponse,
134 goutbuf.value, goutbuf.length) != STATUS_OK)
136 gss_release_buffer(&lmin_s, &goutbuf);
137 return STATUS_ERROR;
140 gss_release_buffer(&lmin_s, &goutbuf);
142 if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
144 pg_GSS_error(libpq_gettext("GSSAPI continuation error"),
145 conn,
146 maj_stat, min_stat);
147 gss_release_name(&lmin_s, &conn->gtarg_nam);
148 if (conn->gctx)
149 gss_delete_sec_context(&lmin_s, &conn->gctx, GSS_C_NO_BUFFER);
150 return STATUS_ERROR;
153 if (maj_stat == GSS_S_COMPLETE)
155 conn->client_finished_auth = true;
156 gss_release_name(&lmin_s, &conn->gtarg_nam);
157 conn->gssapi_used = true;
160 return STATUS_OK;
164 * Send initial GSS authentication token
166 static int
167 pg_GSS_startup(PGconn *conn, int payloadlen)
169 int ret;
170 char *host = conn->connhost[conn->whichhost].host;
172 if (!(host && host[0] != '\0'))
174 libpq_append_conn_error(conn, "host name must be specified");
175 return STATUS_ERROR;
178 if (conn->gctx)
180 libpq_append_conn_error(conn, "duplicate GSS authentication request");
181 return STATUS_ERROR;
184 ret = pg_GSS_load_servicename(conn);
185 if (ret != STATUS_OK)
186 return ret;
189 * Initial packet is the same as a continuation packet with no initial
190 * context.
192 conn->gctx = GSS_C_NO_CONTEXT;
194 return pg_GSS_continue(conn, payloadlen);
196 #endif /* ENABLE_GSS */
199 #ifdef ENABLE_SSPI
201 * SSPI authentication system (Windows only)
204 static void
205 pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
207 char sysmsg[256];
209 if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
210 FORMAT_MESSAGE_FROM_SYSTEM,
211 NULL, r, 0,
212 sysmsg, sizeof(sysmsg), NULL) == 0)
213 appendPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x\n",
214 mprefix, (unsigned int) r);
215 else
216 appendPQExpBuffer(&conn->errorMessage, "%s: %s (%x)\n",
217 mprefix, sysmsg, (unsigned int) r);
221 * Continue SSPI authentication with next token as needed.
223 static int
224 pg_SSPI_continue(PGconn *conn, int payloadlen)
226 SECURITY_STATUS r;
227 CtxtHandle newContext;
228 ULONG contextAttr;
229 SecBufferDesc inbuf;
230 SecBufferDesc outbuf;
231 SecBuffer OutBuffers[1];
232 SecBuffer InBuffers[1];
233 char *inputbuf = NULL;
235 if (conn->sspictx != NULL)
238 * On runs other than the first we have some data to send. Put this
239 * data in a SecBuffer type structure.
241 inputbuf = malloc(payloadlen);
242 if (!inputbuf)
244 libpq_append_conn_error(conn, "out of memory allocating SSPI buffer (%d)",
245 payloadlen);
246 return STATUS_ERROR;
248 if (pqGetnchar(inputbuf, payloadlen, conn))
251 * Shouldn't happen, because the caller should've ensured that the
252 * whole message is already in the input buffer.
254 free(inputbuf);
255 return STATUS_ERROR;
258 inbuf.ulVersion = SECBUFFER_VERSION;
259 inbuf.cBuffers = 1;
260 inbuf.pBuffers = InBuffers;
261 InBuffers[0].pvBuffer = inputbuf;
262 InBuffers[0].cbBuffer = payloadlen;
263 InBuffers[0].BufferType = SECBUFFER_TOKEN;
266 /* finished parsing, trace server-to-client message */
267 if (conn->Pfdebug)
268 pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false);
270 OutBuffers[0].pvBuffer = NULL;
271 OutBuffers[0].BufferType = SECBUFFER_TOKEN;
272 OutBuffers[0].cbBuffer = 0;
273 outbuf.cBuffers = 1;
274 outbuf.pBuffers = OutBuffers;
275 outbuf.ulVersion = SECBUFFER_VERSION;
277 r = InitializeSecurityContext(conn->sspicred,
278 conn->sspictx,
279 conn->sspitarget,
280 ISC_REQ_ALLOCATE_MEMORY,
282 SECURITY_NETWORK_DREP,
283 (conn->sspictx == NULL) ? NULL : &inbuf,
285 &newContext,
286 &outbuf,
287 &contextAttr,
288 NULL);
290 /* we don't need the input anymore */
291 free(inputbuf);
293 if (r != SEC_E_OK && r != SEC_I_CONTINUE_NEEDED)
295 pg_SSPI_error(conn, libpq_gettext("SSPI continuation error"), r);
297 return STATUS_ERROR;
300 if (conn->sspictx == NULL)
302 /* On first run, transfer retrieved context handle */
303 conn->sspictx = malloc(sizeof(CtxtHandle));
304 if (conn->sspictx == NULL)
306 libpq_append_conn_error(conn, "out of memory");
307 return STATUS_ERROR;
309 memcpy(conn->sspictx, &newContext, sizeof(CtxtHandle));
313 * If SSPI returned any data to be sent to the server (as it normally
314 * would), send this data as a password packet.
316 if (outbuf.cBuffers > 0)
318 if (outbuf.cBuffers != 1)
321 * This should never happen, at least not for Kerberos
322 * authentication. Keep check in case it shows up with other
323 * authentication methods later.
325 appendPQExpBufferStr(&conn->errorMessage,
326 "SSPI returned invalid number of output buffers\n");
327 return STATUS_ERROR;
331 * If the negotiation is complete, there may be zero bytes to send.
332 * The server is at this point not expecting any more data, so don't
333 * send it.
335 if (outbuf.pBuffers[0].cbBuffer > 0)
337 conn->current_auth_response = AUTH_RESPONSE_GSS;
338 if (pqPacketSend(conn, PqMsg_GSSResponse,
339 outbuf.pBuffers[0].pvBuffer, outbuf.pBuffers[0].cbBuffer))
341 FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
342 return STATUS_ERROR;
345 FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
348 if (r == SEC_E_OK)
349 conn->client_finished_auth = true;
351 /* Cleanup is handled by the code in freePGconn() */
352 return STATUS_OK;
356 * Send initial SSPI authentication token.
357 * If use_negotiate is 0, use kerberos authentication package which is
358 * compatible with Unix. If use_negotiate is 1, use the negotiate package
359 * which supports both kerberos and NTLM, but is not compatible with Unix.
361 static int
362 pg_SSPI_startup(PGconn *conn, int use_negotiate, int payloadlen)
364 SECURITY_STATUS r;
365 TimeStamp expire;
366 char *host = conn->connhost[conn->whichhost].host;
368 if (conn->sspictx)
370 libpq_append_conn_error(conn, "duplicate SSPI authentication request");
371 return STATUS_ERROR;
375 * Retrieve credentials handle
377 conn->sspicred = malloc(sizeof(CredHandle));
378 if (conn->sspicred == NULL)
380 libpq_append_conn_error(conn, "out of memory");
381 return STATUS_ERROR;
384 r = AcquireCredentialsHandle(NULL,
385 use_negotiate ? "negotiate" : "kerberos",
386 SECPKG_CRED_OUTBOUND,
387 NULL,
388 NULL,
389 NULL,
390 NULL,
391 conn->sspicred,
392 &expire);
393 if (r != SEC_E_OK)
395 pg_SSPI_error(conn, libpq_gettext("could not acquire SSPI credentials"), r);
396 free(conn->sspicred);
397 conn->sspicred = NULL;
398 return STATUS_ERROR;
402 * Compute target principal name. SSPI has a different format from GSSAPI,
403 * but not more complex. We can skip the @REALM part, because Windows will
404 * fill that in for us automatically.
406 if (!(host && host[0] != '\0'))
408 libpq_append_conn_error(conn, "host name must be specified");
409 return STATUS_ERROR;
411 conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(host) + 2);
412 if (!conn->sspitarget)
414 libpq_append_conn_error(conn, "out of memory");
415 return STATUS_ERROR;
417 sprintf(conn->sspitarget, "%s/%s", conn->krbsrvname, host);
420 * Indicate that we're in SSPI authentication mode to make sure that
421 * pg_SSPI_continue is called next time in the negotiation.
423 conn->usesspi = 1;
425 return pg_SSPI_continue(conn, payloadlen);
427 #endif /* ENABLE_SSPI */
430 * Initialize SASL authentication exchange.
432 static int
433 pg_SASL_init(PGconn *conn, int payloadlen)
435 char *initialresponse = NULL;
436 int initialresponselen;
437 const char *selected_mechanism;
438 PQExpBufferData mechanism_buf;
439 char *password = NULL;
440 SASLStatus status;
442 initPQExpBuffer(&mechanism_buf);
444 if (conn->channel_binding[0] == 'r' && /* require */
445 !conn->ssl_in_use)
447 libpq_append_conn_error(conn, "channel binding required, but SSL not in use");
448 goto error;
451 if (conn->sasl_state)
453 libpq_append_conn_error(conn, "duplicate SASL authentication request");
454 goto error;
458 * Parse the list of SASL authentication mechanisms in the
459 * AuthenticationSASL message, and select the best mechanism that we
460 * support. Mechanisms are listed by order of decreasing importance.
462 selected_mechanism = NULL;
463 for (;;)
465 if (pqGets(&mechanism_buf, conn))
467 appendPQExpBufferStr(&conn->errorMessage,
468 "fe_sendauth: invalid authentication request from server: invalid list of authentication mechanisms\n");
469 goto error;
471 if (PQExpBufferDataBroken(mechanism_buf))
472 goto oom_error;
474 /* An empty string indicates end of list */
475 if (mechanism_buf.data[0] == '\0')
476 break;
479 * Select the mechanism to use. Pick SCRAM-SHA-256-PLUS over anything
480 * else if a channel binding type is set and if the client supports it
481 * (and did not set channel_binding=disable). Pick SCRAM-SHA-256 if
482 * nothing else has already been picked. If we add more mechanisms, a
483 * more refined priority mechanism might become necessary.
485 if (strcmp(mechanism_buf.data, SCRAM_SHA_256_PLUS_NAME) == 0)
487 if (conn->ssl_in_use)
489 /* The server has offered SCRAM-SHA-256-PLUS. */
491 #ifdef USE_SSL
493 * The client supports channel binding, which is chosen if
494 * channel_binding is not disabled.
496 if (conn->channel_binding[0] != 'd') /* disable */
498 selected_mechanism = SCRAM_SHA_256_PLUS_NAME;
499 conn->sasl = &pg_scram_mech;
500 conn->password_needed = true;
502 #else
504 * The client does not support channel binding. If it is
505 * required, complain immediately instead of the error below
506 * which would be confusing as the server is publishing
507 * SCRAM-SHA-256-PLUS.
509 if (conn->channel_binding[0] == 'r') /* require */
511 libpq_append_conn_error(conn, "channel binding is required, but client does not support it");
512 goto error;
514 #endif
516 else
519 * The server offered SCRAM-SHA-256-PLUS, but the connection
520 * is not SSL-encrypted. That's not sane. Perhaps SSL was
521 * stripped by a proxy? There's no point in continuing,
522 * because the server will reject the connection anyway if we
523 * try authenticate without channel binding even though both
524 * the client and server supported it. The SCRAM exchange
525 * checks for that, to prevent downgrade attacks.
527 libpq_append_conn_error(conn, "server offered SCRAM-SHA-256-PLUS authentication over a non-SSL connection");
528 goto error;
531 else if (strcmp(mechanism_buf.data, SCRAM_SHA_256_NAME) == 0 &&
532 !selected_mechanism)
534 selected_mechanism = SCRAM_SHA_256_NAME;
535 conn->sasl = &pg_scram_mech;
536 conn->password_needed = true;
540 if (!selected_mechanism)
542 libpq_append_conn_error(conn, "none of the server's SASL authentication mechanisms are supported");
543 goto error;
546 if (conn->channel_binding[0] == 'r' && /* require */
547 strcmp(selected_mechanism, SCRAM_SHA_256_PLUS_NAME) != 0)
549 libpq_append_conn_error(conn, "channel binding is required, but server did not offer an authentication method that supports channel binding");
550 goto error;
554 * Now that the SASL mechanism has been chosen for the exchange,
555 * initialize its state information.
559 * First, select the password to use for the exchange, complaining if
560 * there isn't one and the selected SASL mechanism needs it.
562 if (conn->password_needed && !conn->scram_client_key_binary)
564 password = conn->connhost[conn->whichhost].password;
565 if (password == NULL)
566 password = conn->pgpass;
567 if (password == NULL || password[0] == '\0')
569 appendPQExpBufferStr(&conn->errorMessage,
570 PQnoPasswordSupplied);
571 goto error;
575 /* finished parsing, trace server-to-client message */
576 if (conn->Pfdebug)
577 pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false);
579 Assert(conn->sasl);
582 * Initialize the SASL state information with all the information gathered
583 * during the initial exchange.
585 * Note: Only tls-unique is supported for the moment.
587 conn->sasl_state = conn->sasl->init(conn,
588 password,
589 selected_mechanism);
590 if (!conn->sasl_state)
591 goto oom_error;
593 /* Get the mechanism-specific Initial Client Response, if any */
594 status = conn->sasl->exchange(conn->sasl_state,
595 NULL, -1,
596 &initialresponse, &initialresponselen);
598 if (status == SASL_FAILED)
599 goto error;
602 * Build a SASLInitialResponse message, and send it.
604 if (pqPutMsgStart(PqMsg_SASLInitialResponse, conn))
605 goto error;
606 if (pqPuts(selected_mechanism, conn))
607 goto error;
608 if (initialresponse)
610 if (pqPutInt(initialresponselen, 4, conn))
611 goto error;
612 if (pqPutnchar(initialresponse, initialresponselen, conn))
613 goto error;
615 conn->current_auth_response = AUTH_RESPONSE_SASL_INITIAL;
616 if (pqPutMsgEnd(conn))
617 goto error;
619 if (pqFlush(conn))
620 goto error;
622 termPQExpBuffer(&mechanism_buf);
623 free(initialresponse);
625 return STATUS_OK;
627 error:
628 termPQExpBuffer(&mechanism_buf);
629 free(initialresponse);
630 return STATUS_ERROR;
632 oom_error:
633 termPQExpBuffer(&mechanism_buf);
634 free(initialresponse);
635 libpq_append_conn_error(conn, "out of memory");
636 return STATUS_ERROR;
640 * Exchange a message for SASL communication protocol with the backend.
641 * This should be used after calling pg_SASL_init to set up the status of
642 * the protocol.
644 static int
645 pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
647 char *output;
648 int outputlen;
649 int res;
650 char *challenge;
651 SASLStatus status;
653 /* Read the SASL challenge from the AuthenticationSASLContinue message. */
654 challenge = malloc(payloadlen + 1);
655 if (!challenge)
657 libpq_append_conn_error(conn, "out of memory allocating SASL buffer (%d)",
658 payloadlen);
659 return STATUS_ERROR;
662 if (pqGetnchar(challenge, payloadlen, conn))
664 free(challenge);
665 return STATUS_ERROR;
668 /* finished parsing, trace server-to-client message */
669 if (conn->Pfdebug)
670 pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false);
672 /* For safety and convenience, ensure the buffer is NULL-terminated. */
673 challenge[payloadlen] = '\0';
675 status = conn->sasl->exchange(conn->sasl_state,
676 challenge, payloadlen,
677 &output, &outputlen);
678 free(challenge); /* don't need the input anymore */
680 if (final && status == SASL_CONTINUE)
682 if (outputlen != 0)
683 free(output);
685 libpq_append_conn_error(conn, "AuthenticationSASLFinal received from server, but SASL authentication was not completed");
686 return STATUS_ERROR;
690 * If the exchange is not completed yet, we need to make sure that the
691 * SASL mechanism has generated a message to send back.
693 if (output == NULL && status == SASL_CONTINUE)
695 libpq_append_conn_error(conn, "no client response found after SASL exchange success");
696 return STATUS_ERROR;
700 * SASL allows zero-length responses, so this check uses "output" and not
701 * "outputlen" to allow the case of an empty message.
703 if (output)
706 * Send the SASL response to the server.
708 conn->current_auth_response = AUTH_RESPONSE_SASL;
709 res = pqPacketSend(conn, PqMsg_SASLResponse, output, outputlen);
710 free(output);
712 if (res != STATUS_OK)
713 return STATUS_ERROR;
716 if (status == SASL_FAILED)
717 return STATUS_ERROR;
719 return STATUS_OK;
722 static int
723 pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
725 int ret;
726 char *crypt_pwd = NULL;
727 const char *pwd_to_send;
728 char md5Salt[4];
730 /* Read the salt from the AuthenticationMD5Password message. */
731 if (areq == AUTH_REQ_MD5)
733 if (pqGetnchar(md5Salt, 4, conn))
734 return STATUS_ERROR; /* shouldn't happen */
737 /* finished parsing, trace server-to-client message */
738 if (conn->Pfdebug)
739 pqTraceOutputMessage(conn, conn->inBuffer + conn->inStart, false);
741 /* Encrypt the password if needed. */
743 switch (areq)
745 case AUTH_REQ_MD5:
747 char *crypt_pwd2;
748 const char *errstr = NULL;
750 /* Allocate enough space for two MD5 hashes */
751 crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
752 if (!crypt_pwd)
754 libpq_append_conn_error(conn, "out of memory");
755 return STATUS_ERROR;
758 crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
759 if (!pg_md5_encrypt(password, conn->pguser,
760 strlen(conn->pguser), crypt_pwd2,
761 &errstr))
763 libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
764 free(crypt_pwd);
765 return STATUS_ERROR;
767 if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), md5Salt,
768 4, crypt_pwd, &errstr))
770 libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
771 free(crypt_pwd);
772 return STATUS_ERROR;
775 pwd_to_send = crypt_pwd;
776 break;
778 case AUTH_REQ_PASSWORD:
779 pwd_to_send = password;
780 break;
781 default:
782 return STATUS_ERROR;
784 conn->current_auth_response = AUTH_RESPONSE_PASSWORD;
785 ret = pqPacketSend(conn, PqMsg_PasswordMessage,
786 pwd_to_send, strlen(pwd_to_send) + 1);
787 free(crypt_pwd);
788 return ret;
792 * Translate a disallowed AuthRequest code into an error message.
794 static const char *
795 auth_method_description(AuthRequest areq)
797 switch (areq)
799 case AUTH_REQ_PASSWORD:
800 return libpq_gettext("server requested a cleartext password");
801 case AUTH_REQ_MD5:
802 return libpq_gettext("server requested a hashed password");
803 case AUTH_REQ_GSS:
804 case AUTH_REQ_GSS_CONT:
805 return libpq_gettext("server requested GSSAPI authentication");
806 case AUTH_REQ_SSPI:
807 return libpq_gettext("server requested SSPI authentication");
808 case AUTH_REQ_SASL:
809 case AUTH_REQ_SASL_CONT:
810 case AUTH_REQ_SASL_FIN:
811 return libpq_gettext("server requested SASL authentication");
814 return libpq_gettext("server requested an unknown authentication type");
818 * Convenience macro for checking the allowed_auth_methods bitmask. Caller
819 * must ensure that type is not greater than 31 (high bit of the bitmask).
821 #define auth_method_allowed(conn, type) \
822 (((conn)->allowed_auth_methods & (1 << (type))) != 0)
825 * Verify that the authentication request is expected, given the connection
826 * parameters. This is especially important when the client wishes to
827 * authenticate the server before any sensitive information is exchanged.
829 static bool
830 check_expected_areq(AuthRequest areq, PGconn *conn)
832 bool result = true;
833 const char *reason = NULL;
835 StaticAssertDecl((sizeof(conn->allowed_auth_methods) * CHAR_BIT) > AUTH_REQ_MAX,
836 "AUTH_REQ_MAX overflows the allowed_auth_methods bitmask");
838 if (conn->sslcertmode[0] == 'r' /* require */
839 && areq == AUTH_REQ_OK)
842 * Trade off a little bit of complexity to try to get these error
843 * messages as precise as possible.
845 if (!conn->ssl_cert_requested)
847 libpq_append_conn_error(conn, "server did not request an SSL certificate");
848 return false;
850 else if (!conn->ssl_cert_sent)
852 libpq_append_conn_error(conn, "server accepted connection without a valid SSL certificate");
853 return false;
858 * If the user required a specific auth method, or specified an allowed
859 * set, then reject all others here, and make sure the server actually
860 * completes an authentication exchange.
862 if (conn->require_auth)
864 switch (areq)
866 case AUTH_REQ_OK:
869 * Check to make sure we've actually finished our exchange (or
870 * else that the user has allowed an authentication-less
871 * connection).
873 * If the user has allowed both SCRAM and unauthenticated
874 * (trust) connections, then this check will silently accept
875 * partial SCRAM exchanges, where a misbehaving server does
876 * not provide its verifier before sending an OK. This is
877 * consistent with historical behavior, but it may be a point
878 * to revisit in the future, since it could allow a server
879 * that doesn't know the user's password to silently harvest
880 * material for a brute force attack.
882 if (!conn->auth_required || conn->client_finished_auth)
883 break;
886 * No explicit authentication request was made by the server
887 * -- or perhaps it was made and not completed, in the case of
888 * SCRAM -- but there is one special case to check. If the
889 * user allowed "gss", then a GSS-encrypted channel also
890 * satisfies the check.
892 #ifdef ENABLE_GSS
893 if (auth_method_allowed(conn, AUTH_REQ_GSS) && conn->gssenc)
896 * If implicit GSS auth has already been performed via GSS
897 * encryption, we don't need to have performed an
898 * AUTH_REQ_GSS exchange. This allows require_auth=gss to
899 * be combined with gssencmode, since there won't be an
900 * explicit authentication request in that case.
903 else
904 #endif
906 reason = libpq_gettext("server did not complete authentication");
907 result = false;
910 break;
912 case AUTH_REQ_PASSWORD:
913 case AUTH_REQ_MD5:
914 case AUTH_REQ_GSS:
915 case AUTH_REQ_GSS_CONT:
916 case AUTH_REQ_SSPI:
917 case AUTH_REQ_SASL:
918 case AUTH_REQ_SASL_CONT:
919 case AUTH_REQ_SASL_FIN:
922 * We don't handle these with the default case, to avoid
923 * bit-shifting past the end of the allowed_auth_methods mask
924 * if the server sends an unexpected AuthRequest.
926 result = auth_method_allowed(conn, areq);
927 break;
929 default:
930 result = false;
931 break;
935 if (!result)
937 if (!reason)
938 reason = auth_method_description(areq);
940 libpq_append_conn_error(conn, "authentication method requirement \"%s\" failed: %s",
941 conn->require_auth, reason);
942 return result;
946 * When channel_binding=require, we must protect against two cases: (1) we
947 * must not respond to non-SASL authentication requests, which might leak
948 * information such as the client's password; and (2) even if we receive
949 * AUTH_REQ_OK, we still must ensure that channel binding has happened in
950 * order to authenticate the server.
952 if (conn->channel_binding[0] == 'r' /* require */ )
954 switch (areq)
956 case AUTH_REQ_SASL:
957 case AUTH_REQ_SASL_CONT:
958 case AUTH_REQ_SASL_FIN:
959 break;
960 case AUTH_REQ_OK:
961 if (!conn->sasl || !conn->sasl->channel_bound(conn->sasl_state))
963 libpq_append_conn_error(conn, "channel binding required, but server authenticated client without channel binding");
964 result = false;
966 break;
967 default:
968 libpq_append_conn_error(conn, "channel binding required but not supported by server's authentication request");
969 result = false;
970 break;
974 return result;
978 * pg_fe_sendauth
979 * client demux routine for processing an authentication request
981 * The server has sent us an authentication challenge (or OK). Send an
982 * appropriate response. The caller has ensured that the whole message is
983 * now in the input buffer, and has already read the type and length of
984 * it. We are responsible for reading any remaining extra data, specific
985 * to the authentication method. 'payloadlen' is the remaining length in
986 * the message.
989 pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn)
991 int oldmsglen;
993 if (!check_expected_areq(areq, conn))
994 return STATUS_ERROR;
996 switch (areq)
998 case AUTH_REQ_OK:
999 break;
1001 case AUTH_REQ_KRB4:
1002 libpq_append_conn_error(conn, "Kerberos 4 authentication not supported");
1003 return STATUS_ERROR;
1005 case AUTH_REQ_KRB5:
1006 libpq_append_conn_error(conn, "Kerberos 5 authentication not supported");
1007 return STATUS_ERROR;
1009 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
1010 case AUTH_REQ_GSS:
1011 #if !defined(ENABLE_SSPI)
1012 /* no native SSPI, so use GSSAPI library for it */
1013 case AUTH_REQ_SSPI:
1014 #endif
1016 int r;
1018 pglock_thread();
1021 * If we have both GSS and SSPI support compiled in, use SSPI
1022 * support by default. This is overridable by a connection
1023 * string parameter. Note that when using SSPI we still leave
1024 * the negotiate parameter off, since we want SSPI to use the
1025 * GSSAPI kerberos protocol. For actual SSPI negotiate
1026 * protocol, we use AUTH_REQ_SSPI.
1028 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1029 if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
1030 r = pg_GSS_startup(conn, payloadlen);
1031 else
1032 r = pg_SSPI_startup(conn, 0, payloadlen);
1033 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1034 r = pg_GSS_startup(conn, payloadlen);
1035 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1036 r = pg_SSPI_startup(conn, 0, payloadlen);
1037 #endif
1038 if (r != STATUS_OK)
1040 /* Error message already filled in. */
1041 pgunlock_thread();
1042 return STATUS_ERROR;
1044 pgunlock_thread();
1046 break;
1048 case AUTH_REQ_GSS_CONT:
1050 int r;
1052 pglock_thread();
1053 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1054 if (conn->usesspi)
1055 r = pg_SSPI_continue(conn, payloadlen);
1056 else
1057 r = pg_GSS_continue(conn, payloadlen);
1058 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1059 r = pg_GSS_continue(conn, payloadlen);
1060 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1061 r = pg_SSPI_continue(conn, payloadlen);
1062 #endif
1063 if (r != STATUS_OK)
1065 /* Error message already filled in. */
1066 pgunlock_thread();
1067 return STATUS_ERROR;
1069 pgunlock_thread();
1071 break;
1072 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1073 /* No GSSAPI *or* SSPI support */
1074 case AUTH_REQ_GSS:
1075 case AUTH_REQ_GSS_CONT:
1076 libpq_append_conn_error(conn, "GSSAPI authentication not supported");
1077 return STATUS_ERROR;
1078 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1080 #ifdef ENABLE_SSPI
1081 case AUTH_REQ_SSPI:
1084 * SSPI has its own startup message so libpq can decide which
1085 * method to use. Indicate to pg_SSPI_startup that we want SSPI
1086 * negotiation instead of Kerberos.
1088 pglock_thread();
1089 if (pg_SSPI_startup(conn, 1, payloadlen) != STATUS_OK)
1091 /* Error message already filled in. */
1092 pgunlock_thread();
1093 return STATUS_ERROR;
1095 pgunlock_thread();
1096 break;
1097 #else
1100 * No SSPI support. However, if we have GSSAPI but not SSPI
1101 * support, AUTH_REQ_SSPI will have been handled in the codepath
1102 * for AUTH_REQ_GSS above, so don't duplicate the case label in
1103 * that case.
1105 #if !defined(ENABLE_GSS)
1106 case AUTH_REQ_SSPI:
1107 libpq_append_conn_error(conn, "SSPI authentication not supported");
1108 return STATUS_ERROR;
1109 #endif /* !define(ENABLE_GSS) */
1110 #endif /* ENABLE_SSPI */
1113 case AUTH_REQ_CRYPT:
1114 libpq_append_conn_error(conn, "Crypt authentication not supported");
1115 return STATUS_ERROR;
1117 case AUTH_REQ_MD5:
1118 case AUTH_REQ_PASSWORD:
1120 char *password;
1122 conn->password_needed = true;
1123 password = conn->connhost[conn->whichhost].password;
1124 if (password == NULL)
1125 password = conn->pgpass;
1126 if (password == NULL || password[0] == '\0')
1128 appendPQExpBufferStr(&conn->errorMessage,
1129 PQnoPasswordSupplied);
1130 return STATUS_ERROR;
1132 if (pg_password_sendauth(conn, password, areq) != STATUS_OK)
1134 appendPQExpBufferStr(&conn->errorMessage,
1135 "fe_sendauth: error sending password authentication\n");
1136 return STATUS_ERROR;
1139 /* We expect no further authentication requests. */
1140 conn->client_finished_auth = true;
1141 break;
1144 case AUTH_REQ_SASL:
1147 * The request contains the name (as assigned by IANA) of the
1148 * authentication mechanism.
1150 if (pg_SASL_init(conn, payloadlen) != STATUS_OK)
1152 /* pg_SASL_init already set the error message */
1153 return STATUS_ERROR;
1155 break;
1157 case AUTH_REQ_SASL_CONT:
1158 case AUTH_REQ_SASL_FIN:
1159 if (conn->sasl_state == NULL)
1161 appendPQExpBufferStr(&conn->errorMessage,
1162 "fe_sendauth: invalid authentication request from server: AUTH_REQ_SASL_CONT without AUTH_REQ_SASL\n");
1163 return STATUS_ERROR;
1165 oldmsglen = conn->errorMessage.len;
1166 if (pg_SASL_continue(conn, payloadlen,
1167 (areq == AUTH_REQ_SASL_FIN)) != STATUS_OK)
1169 /* Use this message if pg_SASL_continue didn't supply one */
1170 if (conn->errorMessage.len == oldmsglen)
1171 appendPQExpBufferStr(&conn->errorMessage,
1172 "fe_sendauth: error in SASL authentication\n");
1173 return STATUS_ERROR;
1175 break;
1177 default:
1178 libpq_append_conn_error(conn, "authentication method %u not supported", areq);
1179 return STATUS_ERROR;
1182 return STATUS_OK;
1187 * pg_fe_getusername
1189 * Returns a pointer to malloc'd space containing the name of the
1190 * specified user_id. If there is an error, return NULL, and append
1191 * a suitable error message to *errorMessage if that's not NULL.
1193 * Caution: on Windows, the user_id argument is ignored, and we always
1194 * fetch the current user's name.
1196 char *
1197 pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
1199 char *result = NULL;
1200 const char *name = NULL;
1202 #ifdef WIN32
1203 /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1204 char username[256 + 1];
1205 DWORD namesize = sizeof(username);
1206 #else
1207 struct passwd pwbuf;
1208 struct passwd *pw = NULL;
1209 char buf[1024];
1210 int rc;
1211 #endif
1213 #ifdef WIN32
1214 if (GetUserName(username, &namesize))
1215 name = username;
1216 else if (errorMessage)
1217 libpq_append_error(errorMessage,
1218 "user name lookup failure: error code %lu",
1219 GetLastError());
1220 #else
1221 rc = getpwuid_r(user_id, &pwbuf, buf, sizeof buf, &pw);
1222 if (rc != 0)
1224 errno = rc;
1225 if (errorMessage)
1226 libpq_append_error(errorMessage, "could not look up local user ID %ld: %m", (long) user_id);
1228 else if (!pw)
1230 if (errorMessage)
1231 libpq_append_error(errorMessage, "local user with ID %ld does not exist", (long) user_id);
1233 else
1234 name = pw->pw_name;
1235 #endif
1237 if (name)
1239 result = strdup(name);
1240 if (result == NULL && errorMessage)
1241 libpq_append_error(errorMessage, "out of memory");
1244 return result;
1248 * pg_fe_getauthname
1250 * Returns a pointer to malloc'd space containing whatever name the user
1251 * has authenticated to the system. If there is an error, return NULL,
1252 * and append a suitable error message to *errorMessage if that's not NULL.
1254 char *
1255 pg_fe_getauthname(PQExpBuffer errorMessage)
1257 #ifdef WIN32
1258 return pg_fe_getusername(0, errorMessage);
1259 #else
1260 return pg_fe_getusername(geteuid(), errorMessage);
1261 #endif
1266 * PQencryptPassword -- exported routine to encrypt a password with MD5
1268 * This function is equivalent to calling PQencryptPasswordConn with
1269 * "md5" as the encryption method, except that this doesn't require
1270 * a connection object. This function is deprecated, use
1271 * PQencryptPasswordConn instead.
1273 char *
1274 PQencryptPassword(const char *passwd, const char *user)
1276 char *crypt_pwd;
1277 const char *errstr = NULL;
1279 crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1280 if (!crypt_pwd)
1281 return NULL;
1283 if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
1285 free(crypt_pwd);
1286 return NULL;
1289 return crypt_pwd;
1293 * PQencryptPasswordConn -- exported routine to encrypt a password
1295 * This is intended to be used by client applications that wish to send
1296 * commands like ALTER USER joe PASSWORD 'pwd'. The password need not
1297 * be sent in cleartext if it is encrypted on the client side. This is
1298 * good because it ensures the cleartext password won't end up in logs,
1299 * pg_stat displays, etc. We export the function so that clients won't
1300 * be dependent on low-level details like whether the encryption is MD5
1301 * or something else.
1303 * Arguments are a connection object, the cleartext password, the SQL
1304 * name of the user it is for, and a string indicating the algorithm to
1305 * use for encrypting the password. If algorithm is NULL, this queries
1306 * the server for the current 'password_encryption' value. If you wish
1307 * to avoid that, e.g. to avoid blocking, you can execute
1308 * 'show password_encryption' yourself before calling this function, and
1309 * pass it as the algorithm.
1311 * Return value is a malloc'd string. The client may assume the string
1312 * doesn't contain any special characters that would require escaping.
1313 * On error, an error message is stored in the connection object, and
1314 * returns NULL.
1316 char *
1317 PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user,
1318 const char *algorithm)
1320 #define MAX_ALGORITHM_NAME_LEN 50
1321 char algobuf[MAX_ALGORITHM_NAME_LEN + 1];
1322 char *crypt_pwd = NULL;
1324 if (!conn)
1325 return NULL;
1327 pqClearConnErrorState(conn);
1329 /* If no algorithm was given, ask the server. */
1330 if (algorithm == NULL)
1332 PGresult *res;
1333 char *val;
1335 res = PQexec(conn, "show password_encryption");
1336 if (res == NULL)
1338 /* PQexec() should've set conn->errorMessage already */
1339 return NULL;
1341 if (PQresultStatus(res) != PGRES_TUPLES_OK)
1343 /* PQexec() should've set conn->errorMessage already */
1344 PQclear(res);
1345 return NULL;
1347 if (PQntuples(res) != 1 || PQnfields(res) != 1)
1349 PQclear(res);
1350 libpq_append_conn_error(conn, "unexpected shape of result set returned for SHOW");
1351 return NULL;
1353 val = PQgetvalue(res, 0, 0);
1355 if (strlen(val) > MAX_ALGORITHM_NAME_LEN)
1357 PQclear(res);
1358 libpq_append_conn_error(conn, "\"password_encryption\" value too long");
1359 return NULL;
1361 strcpy(algobuf, val);
1362 PQclear(res);
1364 algorithm = algobuf;
1368 * Also accept "on" and "off" as aliases for "md5", because
1369 * password_encryption was a boolean before PostgreSQL 10. We refuse to
1370 * send the password in plaintext even if it was "off".
1372 if (strcmp(algorithm, "on") == 0 ||
1373 strcmp(algorithm, "off") == 0)
1374 algorithm = "md5";
1377 * Ok, now we know what algorithm to use
1379 if (strcmp(algorithm, "scram-sha-256") == 0)
1381 const char *errstr = NULL;
1383 crypt_pwd = pg_fe_scram_build_secret(passwd,
1384 conn->scram_sha_256_iterations,
1385 &errstr);
1386 if (!crypt_pwd)
1387 libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
1389 else if (strcmp(algorithm, "md5") == 0)
1391 crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1392 if (crypt_pwd)
1394 const char *errstr = NULL;
1396 if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
1398 libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
1399 free(crypt_pwd);
1400 crypt_pwd = NULL;
1403 else
1404 libpq_append_conn_error(conn, "out of memory");
1406 else
1408 libpq_append_conn_error(conn, "unrecognized password encryption algorithm \"%s\"",
1409 algorithm);
1410 return NULL;
1413 return crypt_pwd;
1417 * PQchangePassword -- exported routine to change a password
1419 * This is intended to be used by client applications that wish to
1420 * change the password for a user. The password is not sent in
1421 * cleartext because it is encrypted on the client side. This is
1422 * good because it ensures the cleartext password is never known by
1423 * the server, and therefore won't end up in logs, pg_stat displays,
1424 * etc. The password encryption is performed by PQencryptPasswordConn(),
1425 * which is passed a NULL for the algorithm argument. Hence encryption
1426 * is done according to the server's password_encryption
1427 * setting. We export the function so that clients won't be dependent
1428 * on the implementation specific details with respect to how the
1429 * server changes passwords.
1431 * Arguments are a connection object, the SQL name of the target user,
1432 * and the cleartext password.
1434 * Return value is the PGresult of the executed ALTER USER statement
1435 * or NULL if we never get there. The caller is responsible to PQclear()
1436 * the returned PGresult.
1438 * PQresultStatus() should be called to check the return value for errors,
1439 * and PQerrorMessage() used to get more information about such errors.
1441 PGresult *
1442 PQchangePassword(PGconn *conn, const char *user, const char *passwd)
1444 char *encrypted_password = PQencryptPasswordConn(conn, passwd,
1445 user, NULL);
1447 if (!encrypted_password)
1449 /* PQencryptPasswordConn() already registered the error */
1450 return NULL;
1452 else
1454 char *fmtpw = PQescapeLiteral(conn, encrypted_password,
1455 strlen(encrypted_password));
1457 /* no longer needed, so clean up now */
1458 PQfreemem(encrypted_password);
1460 if (!fmtpw)
1462 /* PQescapeLiteral() already registered the error */
1463 return NULL;
1465 else
1467 char *fmtuser = PQescapeIdentifier(conn, user, strlen(user));
1469 if (!fmtuser)
1471 /* PQescapeIdentifier() already registered the error */
1472 PQfreemem(fmtpw);
1473 return NULL;
1475 else
1477 PQExpBufferData buf;
1478 PGresult *res;
1480 initPQExpBuffer(&buf);
1481 printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD %s",
1482 fmtuser, fmtpw);
1484 res = PQexec(conn, buf.data);
1486 /* clean up */
1487 termPQExpBuffer(&buf);
1488 PQfreemem(fmtuser);
1489 PQfreemem(fmtpw);
1491 return res;