PostgreSQL Source Code  git master
fe-auth.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fe-auth.c
4  * The front-end (client) authorization routines
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/interfaces/libpq/fe-auth.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 /*
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
21  */
22 
23 #include "postgres_fe.h"
24 
25 #ifdef WIN32
26 #include "win32.h"
27 #else
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <limits.h>
31 #include <sys/param.h> /* for MAXHOSTNAMELEN on most */
32 #include <sys/socket.h>
33 #ifdef HAVE_SYS_UCRED_H
34 #include <sys/ucred.h>
35 #endif
36 #ifndef MAXHOSTNAMELEN
37 #include <netdb.h> /* for MAXHOSTNAMELEN on some */
38 #endif
39 #endif
40 
41 #include "common/md5.h"
42 #include "common/scram-common.h"
43 #include "fe-auth.h"
44 #include "fe-auth-sasl.h"
45 #include "libpq-fe.h"
46 
47 #ifdef ENABLE_GSS
48 /*
49  * GSSAPI authentication system.
50  */
51 
52 #include "fe-gssapi-common.h"
53 
54 /*
55  * Continue GSS authentication with next token as needed.
56  */
57 static int
58 pg_GSS_continue(PGconn *conn, int payloadlen)
59 {
60  OM_uint32 maj_stat,
61  min_stat,
62  lmin_s,
63  gss_flags = GSS_C_MUTUAL_FLAG;
64  gss_buffer_desc ginbuf;
65  gss_buffer_desc goutbuf;
66 
67  /*
68  * On first call, there's no input token. On subsequent calls, read the
69  * input token into a GSS buffer.
70  */
71  if (conn->gctx != GSS_C_NO_CONTEXT)
72  {
73  ginbuf.length = payloadlen;
74  ginbuf.value = malloc(payloadlen);
75  if (!ginbuf.value)
76  {
77  libpq_append_conn_error(conn, "out of memory allocating GSSAPI buffer (%d)",
78  payloadlen);
79  return STATUS_ERROR;
80  }
81  if (pqGetnchar(ginbuf.value, payloadlen, conn))
82  {
83  /*
84  * Shouldn't happen, because the caller should've ensured that the
85  * whole message is already in the input buffer.
86  */
87  free(ginbuf.value);
88  return STATUS_ERROR;
89  }
90  }
91  else
92  {
93  ginbuf.length = 0;
94  ginbuf.value = NULL;
95  }
96 
97  /* Only try to acquire credentials if GSS delegation isn't disabled. */
98  if (!pg_GSS_have_cred_cache(&conn->gcred))
99  conn->gcred = GSS_C_NO_CREDENTIAL;
100 
101  if (conn->gssdelegation && conn->gssdelegation[0] == '1')
102  gss_flags |= GSS_C_DELEG_FLAG;
103 
104  maj_stat = gss_init_sec_context(&min_stat,
105  conn->gcred,
106  &conn->gctx,
107  conn->gtarg_nam,
108  GSS_C_NO_OID,
109  gss_flags,
110  0,
111  GSS_C_NO_CHANNEL_BINDINGS,
112  (ginbuf.value == NULL) ? GSS_C_NO_BUFFER : &ginbuf,
113  NULL,
114  &goutbuf,
115  NULL,
116  NULL);
117 
118  free(ginbuf.value);
119 
120  if (goutbuf.length != 0)
121  {
122  /*
123  * GSS generated data to send to the server. We don't care if it's the
124  * first or subsequent packet, just send the same kind of password
125  * packet.
126  */
128  goutbuf.value, goutbuf.length) != STATUS_OK)
129  {
130  gss_release_buffer(&lmin_s, &goutbuf);
131  return STATUS_ERROR;
132  }
133  }
134  gss_release_buffer(&lmin_s, &goutbuf);
135 
136  if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
137  {
138  pg_GSS_error(libpq_gettext("GSSAPI continuation error"),
139  conn,
140  maj_stat, min_stat);
141  gss_release_name(&lmin_s, &conn->gtarg_nam);
142  if (conn->gctx)
143  gss_delete_sec_context(&lmin_s, &conn->gctx, GSS_C_NO_BUFFER);
144  return STATUS_ERROR;
145  }
146 
147  if (maj_stat == GSS_S_COMPLETE)
148  {
149  conn->client_finished_auth = true;
150  gss_release_name(&lmin_s, &conn->gtarg_nam);
151  conn->gssapi_used = true;
152  }
153 
154  return STATUS_OK;
155 }
156 
157 /*
158  * Send initial GSS authentication token
159  */
160 static int
161 pg_GSS_startup(PGconn *conn, int payloadlen)
162 {
163  int ret;
164  char *host = conn->connhost[conn->whichhost].host;
165 
166  if (!(host && host[0] != '\0'))
167  {
168  libpq_append_conn_error(conn, "host name must be specified");
169  return STATUS_ERROR;
170  }
171 
172  if (conn->gctx)
173  {
174  libpq_append_conn_error(conn, "duplicate GSS authentication request");
175  return STATUS_ERROR;
176  }
177 
179  if (ret != STATUS_OK)
180  return ret;
181 
182  /*
183  * Initial packet is the same as a continuation packet with no initial
184  * context.
185  */
186  conn->gctx = GSS_C_NO_CONTEXT;
187 
188  return pg_GSS_continue(conn, payloadlen);
189 }
190 #endif /* ENABLE_GSS */
191 
192 
193 #ifdef ENABLE_SSPI
194 /*
195  * SSPI authentication system (Windows only)
196  */
197 
198 static void
199 pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
200 {
201  char sysmsg[256];
202 
203  if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
204  FORMAT_MESSAGE_FROM_SYSTEM,
205  NULL, r, 0,
206  sysmsg, sizeof(sysmsg), NULL) == 0)
207  appendPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x\n",
208  mprefix, (unsigned int) r);
209  else
210  appendPQExpBuffer(&conn->errorMessage, "%s: %s (%x)\n",
211  mprefix, sysmsg, (unsigned int) r);
212 }
213 
214 /*
215  * Continue SSPI authentication with next token as needed.
216  */
217 static int
218 pg_SSPI_continue(PGconn *conn, int payloadlen)
219 {
220  SECURITY_STATUS r;
221  CtxtHandle newContext;
222  ULONG contextAttr;
223  SecBufferDesc inbuf;
224  SecBufferDesc outbuf;
225  SecBuffer OutBuffers[1];
226  SecBuffer InBuffers[1];
227  char *inputbuf = NULL;
228 
229  if (conn->sspictx != NULL)
230  {
231  /*
232  * On runs other than the first we have some data to send. Put this
233  * data in a SecBuffer type structure.
234  */
235  inputbuf = malloc(payloadlen);
236  if (!inputbuf)
237  {
238  libpq_append_conn_error(conn, "out of memory allocating SSPI buffer (%d)",
239  payloadlen);
240  return STATUS_ERROR;
241  }
242  if (pqGetnchar(inputbuf, payloadlen, conn))
243  {
244  /*
245  * Shouldn't happen, because the caller should've ensured that the
246  * whole message is already in the input buffer.
247  */
248  free(inputbuf);
249  return STATUS_ERROR;
250  }
251 
252  inbuf.ulVersion = SECBUFFER_VERSION;
253  inbuf.cBuffers = 1;
254  inbuf.pBuffers = InBuffers;
255  InBuffers[0].pvBuffer = inputbuf;
256  InBuffers[0].cbBuffer = payloadlen;
257  InBuffers[0].BufferType = SECBUFFER_TOKEN;
258  }
259 
260  OutBuffers[0].pvBuffer = NULL;
261  OutBuffers[0].BufferType = SECBUFFER_TOKEN;
262  OutBuffers[0].cbBuffer = 0;
263  outbuf.cBuffers = 1;
264  outbuf.pBuffers = OutBuffers;
265  outbuf.ulVersion = SECBUFFER_VERSION;
266 
267  r = InitializeSecurityContext(conn->sspicred,
268  conn->sspictx,
269  conn->sspitarget,
270  ISC_REQ_ALLOCATE_MEMORY,
271  0,
272  SECURITY_NETWORK_DREP,
273  (conn->sspictx == NULL) ? NULL : &inbuf,
274  0,
275  &newContext,
276  &outbuf,
277  &contextAttr,
278  NULL);
279 
280  /* we don't need the input anymore */
281  free(inputbuf);
282 
283  if (r != SEC_E_OK && r != SEC_I_CONTINUE_NEEDED)
284  {
285  pg_SSPI_error(conn, libpq_gettext("SSPI continuation error"), r);
286 
287  return STATUS_ERROR;
288  }
289 
290  if (conn->sspictx == NULL)
291  {
292  /* On first run, transfer retrieved context handle */
293  conn->sspictx = malloc(sizeof(CtxtHandle));
294  if (conn->sspictx == NULL)
295  {
296  libpq_append_conn_error(conn, "out of memory");
297  return STATUS_ERROR;
298  }
299  memcpy(conn->sspictx, &newContext, sizeof(CtxtHandle));
300  }
301 
302  /*
303  * If SSPI returned any data to be sent to the server (as it normally
304  * would), send this data as a password packet.
305  */
306  if (outbuf.cBuffers > 0)
307  {
308  if (outbuf.cBuffers != 1)
309  {
310  /*
311  * This should never happen, at least not for Kerberos
312  * authentication. Keep check in case it shows up with other
313  * authentication methods later.
314  */
316  "SSPI returned invalid number of output buffers\n");
317  return STATUS_ERROR;
318  }
319 
320  /*
321  * If the negotiation is complete, there may be zero bytes to send.
322  * The server is at this point not expecting any more data, so don't
323  * send it.
324  */
325  if (outbuf.pBuffers[0].cbBuffer > 0)
326  {
328  outbuf.pBuffers[0].pvBuffer, outbuf.pBuffers[0].cbBuffer))
329  {
330  FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
331  return STATUS_ERROR;
332  }
333  }
334  FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
335  }
336 
337  if (r == SEC_E_OK)
338  conn->client_finished_auth = true;
339 
340  /* Cleanup is handled by the code in freePGconn() */
341  return STATUS_OK;
342 }
343 
344 /*
345  * Send initial SSPI authentication token.
346  * If use_negotiate is 0, use kerberos authentication package which is
347  * compatible with Unix. If use_negotiate is 1, use the negotiate package
348  * which supports both kerberos and NTLM, but is not compatible with Unix.
349  */
350 static int
351 pg_SSPI_startup(PGconn *conn, int use_negotiate, int payloadlen)
352 {
353  SECURITY_STATUS r;
354  TimeStamp expire;
355  char *host = conn->connhost[conn->whichhost].host;
356 
357  if (conn->sspictx)
358  {
359  libpq_append_conn_error(conn, "duplicate SSPI authentication request");
360  return STATUS_ERROR;
361  }
362 
363  /*
364  * Retrieve credentials handle
365  */
366  conn->sspicred = malloc(sizeof(CredHandle));
367  if (conn->sspicred == NULL)
368  {
369  libpq_append_conn_error(conn, "out of memory");
370  return STATUS_ERROR;
371  }
372 
373  r = AcquireCredentialsHandle(NULL,
374  use_negotiate ? "negotiate" : "kerberos",
375  SECPKG_CRED_OUTBOUND,
376  NULL,
377  NULL,
378  NULL,
379  NULL,
380  conn->sspicred,
381  &expire);
382  if (r != SEC_E_OK)
383  {
384  pg_SSPI_error(conn, libpq_gettext("could not acquire SSPI credentials"), r);
385  free(conn->sspicred);
386  conn->sspicred = NULL;
387  return STATUS_ERROR;
388  }
389 
390  /*
391  * Compute target principal name. SSPI has a different format from GSSAPI,
392  * but not more complex. We can skip the @REALM part, because Windows will
393  * fill that in for us automatically.
394  */
395  if (!(host && host[0] != '\0'))
396  {
397  libpq_append_conn_error(conn, "host name must be specified");
398  return STATUS_ERROR;
399  }
400  conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(host) + 2);
401  if (!conn->sspitarget)
402  {
403  libpq_append_conn_error(conn, "out of memory");
404  return STATUS_ERROR;
405  }
406  sprintf(conn->sspitarget, "%s/%s", conn->krbsrvname, host);
407 
408  /*
409  * Indicate that we're in SSPI authentication mode to make sure that
410  * pg_SSPI_continue is called next time in the negotiation.
411  */
412  conn->usesspi = 1;
413 
414  return pg_SSPI_continue(conn, payloadlen);
415 }
416 #endif /* ENABLE_SSPI */
417 
418 /*
419  * Initialize SASL authentication exchange.
420  */
421 static int
422 pg_SASL_init(PGconn *conn, int payloadlen)
423 {
424  char *initialresponse = NULL;
425  int initialresponselen;
426  const char *selected_mechanism;
427  PQExpBufferData mechanism_buf;
428  char *password = NULL;
429  SASLStatus status;
430 
431  initPQExpBuffer(&mechanism_buf);
432 
433  if (conn->channel_binding[0] == 'r' && /* require */
434  !conn->ssl_in_use)
435  {
436  libpq_append_conn_error(conn, "channel binding required, but SSL not in use");
437  goto error;
438  }
439 
440  if (conn->sasl_state)
441  {
442  libpq_append_conn_error(conn, "duplicate SASL authentication request");
443  goto error;
444  }
445 
446  /*
447  * Parse the list of SASL authentication mechanisms in the
448  * AuthenticationSASL message, and select the best mechanism that we
449  * support. Mechanisms are listed by order of decreasing importance.
450  */
451  selected_mechanism = NULL;
452  for (;;)
453  {
454  if (pqGets(&mechanism_buf, conn))
455  {
457  "fe_sendauth: invalid authentication request from server: invalid list of authentication mechanisms\n");
458  goto error;
459  }
460  if (PQExpBufferDataBroken(mechanism_buf))
461  goto oom_error;
462 
463  /* An empty string indicates end of list */
464  if (mechanism_buf.data[0] == '\0')
465  break;
466 
467  /*
468  * Select the mechanism to use. Pick SCRAM-SHA-256-PLUS over anything
469  * else if a channel binding type is set and if the client supports it
470  * (and did not set channel_binding=disable). Pick SCRAM-SHA-256 if
471  * nothing else has already been picked. If we add more mechanisms, a
472  * more refined priority mechanism might become necessary.
473  */
474  if (strcmp(mechanism_buf.data, SCRAM_SHA_256_PLUS_NAME) == 0)
475  {
476  if (conn->ssl_in_use)
477  {
478  /* The server has offered SCRAM-SHA-256-PLUS. */
479 
480 #ifdef USE_SSL
481  /*
482  * The client supports channel binding, which is chosen if
483  * channel_binding is not disabled.
484  */
485  if (conn->channel_binding[0] != 'd') /* disable */
486  {
487  selected_mechanism = SCRAM_SHA_256_PLUS_NAME;
488  conn->sasl = &pg_scram_mech;
489  conn->password_needed = true;
490  }
491 #else
492  /*
493  * The client does not support channel binding. If it is
494  * required, complain immediately instead of the error below
495  * which would be confusing as the server is publishing
496  * SCRAM-SHA-256-PLUS.
497  */
498  if (conn->channel_binding[0] == 'r') /* require */
499  {
500  libpq_append_conn_error(conn, "channel binding is required, but client does not support it");
501  goto error;
502  }
503 #endif
504  }
505  else
506  {
507  /*
508  * The server offered SCRAM-SHA-256-PLUS, but the connection
509  * is not SSL-encrypted. That's not sane. Perhaps SSL was
510  * stripped by a proxy? There's no point in continuing,
511  * because the server will reject the connection anyway if we
512  * try authenticate without channel binding even though both
513  * the client and server supported it. The SCRAM exchange
514  * checks for that, to prevent downgrade attacks.
515  */
516  libpq_append_conn_error(conn, "server offered SCRAM-SHA-256-PLUS authentication over a non-SSL connection");
517  goto error;
518  }
519  }
520  else if (strcmp(mechanism_buf.data, SCRAM_SHA_256_NAME) == 0 &&
521  !selected_mechanism)
522  {
523  selected_mechanism = SCRAM_SHA_256_NAME;
524  conn->sasl = &pg_scram_mech;
525  conn->password_needed = true;
526  }
527  }
528 
529  if (!selected_mechanism)
530  {
531  libpq_append_conn_error(conn, "none of the server's SASL authentication mechanisms are supported");
532  goto error;
533  }
534 
535  if (conn->channel_binding[0] == 'r' && /* require */
536  strcmp(selected_mechanism, SCRAM_SHA_256_PLUS_NAME) != 0)
537  {
538  libpq_append_conn_error(conn, "channel binding is required, but server did not offer an authentication method that supports channel binding");
539  goto error;
540  }
541 
542  /*
543  * Now that the SASL mechanism has been chosen for the exchange,
544  * initialize its state information.
545  */
546 
547  /*
548  * First, select the password to use for the exchange, complaining if
549  * there isn't one and the selected SASL mechanism needs it.
550  */
551  if (conn->password_needed)
552  {
554  if (password == NULL)
555  password = conn->pgpass;
556  if (password == NULL || password[0] == '\0')
557  {
560  goto error;
561  }
562  }
563 
564  Assert(conn->sasl);
565 
566  /*
567  * Initialize the SASL state information with all the information gathered
568  * during the initial exchange.
569  *
570  * Note: Only tls-unique is supported for the moment.
571  */
573  password,
574  selected_mechanism);
575  if (!conn->sasl_state)
576  goto oom_error;
577 
578  /* Get the mechanism-specific Initial Client Response, if any */
579  status = conn->sasl->exchange(conn->sasl_state,
580  NULL, -1,
581  &initialresponse, &initialresponselen);
582 
583  if (status == SASL_FAILED)
584  goto error;
585 
586  /*
587  * Build a SASLInitialResponse message, and send it.
588  */
590  goto error;
591  if (pqPuts(selected_mechanism, conn))
592  goto error;
593  if (initialresponse)
594  {
595  if (pqPutInt(initialresponselen, 4, conn))
596  goto error;
597  if (pqPutnchar(initialresponse, initialresponselen, conn))
598  goto error;
599  }
600  if (pqPutMsgEnd(conn))
601  goto error;
602  if (pqFlush(conn))
603  goto error;
604 
605  termPQExpBuffer(&mechanism_buf);
606  free(initialresponse);
607 
608  return STATUS_OK;
609 
610 error:
611  termPQExpBuffer(&mechanism_buf);
612  free(initialresponse);
613  return STATUS_ERROR;
614 
615 oom_error:
616  termPQExpBuffer(&mechanism_buf);
617  free(initialresponse);
618  libpq_append_conn_error(conn, "out of memory");
619  return STATUS_ERROR;
620 }
621 
622 /*
623  * Exchange a message for SASL communication protocol with the backend.
624  * This should be used after calling pg_SASL_init to set up the status of
625  * the protocol.
626  */
627 static int
628 pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
629 {
630  char *output;
631  int outputlen;
632  int res;
633  char *challenge;
634  SASLStatus status;
635 
636  /* Read the SASL challenge from the AuthenticationSASLContinue message. */
637  challenge = malloc(payloadlen + 1);
638  if (!challenge)
639  {
640  libpq_append_conn_error(conn, "out of memory allocating SASL buffer (%d)",
641  payloadlen);
642  return STATUS_ERROR;
643  }
644 
645  if (pqGetnchar(challenge, payloadlen, conn))
646  {
647  free(challenge);
648  return STATUS_ERROR;
649  }
650  /* For safety and convenience, ensure the buffer is NULL-terminated. */
651  challenge[payloadlen] = '\0';
652 
653  status = conn->sasl->exchange(conn->sasl_state,
654  challenge, payloadlen,
655  &output, &outputlen);
656  free(challenge); /* don't need the input anymore */
657 
658  if (final && status == SASL_CONTINUE)
659  {
660  if (outputlen != 0)
661  free(output);
662 
663  libpq_append_conn_error(conn, "AuthenticationSASLFinal received from server, but SASL authentication was not completed");
664  return STATUS_ERROR;
665  }
666 
667  /*
668  * If the exchange is not completed yet, we need to make sure that the
669  * SASL mechanism has generated a message to send back.
670  */
671  if (output == NULL && status == SASL_CONTINUE)
672  {
673  libpq_append_conn_error(conn, "no client response found after SASL exchange success");
674  return STATUS_ERROR;
675  }
676 
677  /*
678  * SASL allows zero-length responses, so this check uses "output" and not
679  * "outputlen" to allow the case of an empty message.
680  */
681  if (output)
682  {
683  /*
684  * Send the SASL response to the server.
685  */
687  free(output);
688 
689  if (res != STATUS_OK)
690  return STATUS_ERROR;
691  }
692 
693  if (status == SASL_FAILED)
694  return STATUS_ERROR;
695 
696  return STATUS_OK;
697 }
698 
699 static int
701 {
702  int ret;
703  char *crypt_pwd = NULL;
704  const char *pwd_to_send;
705  char md5Salt[4];
706 
707  /* Read the salt from the AuthenticationMD5Password message. */
708  if (areq == AUTH_REQ_MD5)
709  {
710  if (pqGetnchar(md5Salt, 4, conn))
711  return STATUS_ERROR; /* shouldn't happen */
712  }
713 
714  /* Encrypt the password if needed. */
715 
716  switch (areq)
717  {
718  case AUTH_REQ_MD5:
719  {
720  char *crypt_pwd2;
721  const char *errstr = NULL;
722 
723  /* Allocate enough space for two MD5 hashes */
724  crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
725  if (!crypt_pwd)
726  {
727  libpq_append_conn_error(conn, "out of memory");
728  return STATUS_ERROR;
729  }
730 
731  crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
733  strlen(conn->pguser), crypt_pwd2,
734  &errstr))
735  {
736  libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
737  free(crypt_pwd);
738  return STATUS_ERROR;
739  }
740  if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), md5Salt,
741  4, crypt_pwd, &errstr))
742  {
743  libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
744  free(crypt_pwd);
745  return STATUS_ERROR;
746  }
747 
748  pwd_to_send = crypt_pwd;
749  break;
750  }
751  case AUTH_REQ_PASSWORD:
752  pwd_to_send = password;
753  break;
754  default:
755  return STATUS_ERROR;
756  }
758  pwd_to_send, strlen(pwd_to_send) + 1);
759  free(crypt_pwd);
760  return ret;
761 }
762 
763 /*
764  * Translate a disallowed AuthRequest code into an error message.
765  */
766 static const char *
768 {
769  switch (areq)
770  {
771  case AUTH_REQ_PASSWORD:
772  return libpq_gettext("server requested a cleartext password");
773  case AUTH_REQ_MD5:
774  return libpq_gettext("server requested a hashed password");
775  case AUTH_REQ_GSS:
776  case AUTH_REQ_GSS_CONT:
777  return libpq_gettext("server requested GSSAPI authentication");
778  case AUTH_REQ_SSPI:
779  return libpq_gettext("server requested SSPI authentication");
780  case AUTH_REQ_SASL:
781  case AUTH_REQ_SASL_CONT:
782  case AUTH_REQ_SASL_FIN:
783  return libpq_gettext("server requested SASL authentication");
784  }
785 
786  return libpq_gettext("server requested an unknown authentication type");
787 }
788 
789 /*
790  * Convenience macro for checking the allowed_auth_methods bitmask. Caller
791  * must ensure that type is not greater than 31 (high bit of the bitmask).
792  */
793 #define auth_method_allowed(conn, type) \
794  (((conn)->allowed_auth_methods & (1 << (type))) != 0)
795 
796 /*
797  * Verify that the authentication request is expected, given the connection
798  * parameters. This is especially important when the client wishes to
799  * authenticate the server before any sensitive information is exchanged.
800  */
801 static bool
803 {
804  bool result = true;
805  const char *reason = NULL;
806 
807  StaticAssertDecl((sizeof(conn->allowed_auth_methods) * CHAR_BIT) > AUTH_REQ_MAX,
808  "AUTH_REQ_MAX overflows the allowed_auth_methods bitmask");
809 
810  if (conn->sslcertmode[0] == 'r' /* require */
811  && areq == AUTH_REQ_OK)
812  {
813  /*
814  * Trade off a little bit of complexity to try to get these error
815  * messages as precise as possible.
816  */
817  if (!conn->ssl_cert_requested)
818  {
819  libpq_append_conn_error(conn, "server did not request an SSL certificate");
820  return false;
821  }
822  else if (!conn->ssl_cert_sent)
823  {
824  libpq_append_conn_error(conn, "server accepted connection without a valid SSL certificate");
825  return false;
826  }
827  }
828 
829  /*
830  * If the user required a specific auth method, or specified an allowed
831  * set, then reject all others here, and make sure the server actually
832  * completes an authentication exchange.
833  */
834  if (conn->require_auth)
835  {
836  switch (areq)
837  {
838  case AUTH_REQ_OK:
839 
840  /*
841  * Check to make sure we've actually finished our exchange (or
842  * else that the user has allowed an authentication-less
843  * connection).
844  *
845  * If the user has allowed both SCRAM and unauthenticated
846  * (trust) connections, then this check will silently accept
847  * partial SCRAM exchanges, where a misbehaving server does
848  * not provide its verifier before sending an OK. This is
849  * consistent with historical behavior, but it may be a point
850  * to revisit in the future, since it could allow a server
851  * that doesn't know the user's password to silently harvest
852  * material for a brute force attack.
853  */
855  break;
856 
857  /*
858  * No explicit authentication request was made by the server
859  * -- or perhaps it was made and not completed, in the case of
860  * SCRAM -- but there is one special case to check. If the
861  * user allowed "gss", then a GSS-encrypted channel also
862  * satisfies the check.
863  */
864 #ifdef ENABLE_GSS
865  if (auth_method_allowed(conn, AUTH_REQ_GSS) && conn->gssenc)
866  {
867  /*
868  * If implicit GSS auth has already been performed via GSS
869  * encryption, we don't need to have performed an
870  * AUTH_REQ_GSS exchange. This allows require_auth=gss to
871  * be combined with gssencmode, since there won't be an
872  * explicit authentication request in that case.
873  */
874  }
875  else
876 #endif
877  {
878  reason = libpq_gettext("server did not complete authentication");
879  result = false;
880  }
881 
882  break;
883 
884  case AUTH_REQ_PASSWORD:
885  case AUTH_REQ_MD5:
886  case AUTH_REQ_GSS:
887  case AUTH_REQ_GSS_CONT:
888  case AUTH_REQ_SSPI:
889  case AUTH_REQ_SASL:
890  case AUTH_REQ_SASL_CONT:
891  case AUTH_REQ_SASL_FIN:
892 
893  /*
894  * We don't handle these with the default case, to avoid
895  * bit-shifting past the end of the allowed_auth_methods mask
896  * if the server sends an unexpected AuthRequest.
897  */
898  result = auth_method_allowed(conn, areq);
899  break;
900 
901  default:
902  result = false;
903  break;
904  }
905  }
906 
907  if (!result)
908  {
909  if (!reason)
910  reason = auth_method_description(areq);
911 
912  libpq_append_conn_error(conn, "authentication method requirement \"%s\" failed: %s",
913  conn->require_auth, reason);
914  return result;
915  }
916 
917  /*
918  * When channel_binding=require, we must protect against two cases: (1) we
919  * must not respond to non-SASL authentication requests, which might leak
920  * information such as the client's password; and (2) even if we receive
921  * AUTH_REQ_OK, we still must ensure that channel binding has happened in
922  * order to authenticate the server.
923  */
924  if (conn->channel_binding[0] == 'r' /* require */ )
925  {
926  switch (areq)
927  {
928  case AUTH_REQ_SASL:
929  case AUTH_REQ_SASL_CONT:
930  case AUTH_REQ_SASL_FIN:
931  break;
932  case AUTH_REQ_OK:
934  {
935  libpq_append_conn_error(conn, "channel binding required, but server authenticated client without channel binding");
936  result = false;
937  }
938  break;
939  default:
940  libpq_append_conn_error(conn, "channel binding required but not supported by server's authentication request");
941  result = false;
942  break;
943  }
944  }
945 
946  return result;
947 }
948 
949 /*
950  * pg_fe_sendauth
951  * client demux routine for processing an authentication request
952  *
953  * The server has sent us an authentication challenge (or OK). Send an
954  * appropriate response. The caller has ensured that the whole message is
955  * now in the input buffer, and has already read the type and length of
956  * it. We are responsible for reading any remaining extra data, specific
957  * to the authentication method. 'payloadlen' is the remaining length in
958  * the message.
959  */
960 int
961 pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn)
962 {
963  int oldmsglen;
964 
965  if (!check_expected_areq(areq, conn))
966  return STATUS_ERROR;
967 
968  switch (areq)
969  {
970  case AUTH_REQ_OK:
971  break;
972 
973  case AUTH_REQ_KRB4:
974  libpq_append_conn_error(conn, "Kerberos 4 authentication not supported");
975  return STATUS_ERROR;
976 
977  case AUTH_REQ_KRB5:
978  libpq_append_conn_error(conn, "Kerberos 5 authentication not supported");
979  return STATUS_ERROR;
980 
981 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
982  case AUTH_REQ_GSS:
983 #if !defined(ENABLE_SSPI)
984  /* no native SSPI, so use GSSAPI library for it */
985  case AUTH_REQ_SSPI:
986 #endif
987  {
988  int r;
989 
990  pglock_thread();
991 
992  /*
993  * If we have both GSS and SSPI support compiled in, use SSPI
994  * support by default. This is overridable by a connection
995  * string parameter. Note that when using SSPI we still leave
996  * the negotiate parameter off, since we want SSPI to use the
997  * GSSAPI kerberos protocol. For actual SSPI negotiate
998  * protocol, we use AUTH_REQ_SSPI.
999  */
1000 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1001  if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
1002  r = pg_GSS_startup(conn, payloadlen);
1003  else
1004  r = pg_SSPI_startup(conn, 0, payloadlen);
1005 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1006  r = pg_GSS_startup(conn, payloadlen);
1007 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1008  r = pg_SSPI_startup(conn, 0, payloadlen);
1009 #endif
1010  if (r != STATUS_OK)
1011  {
1012  /* Error message already filled in. */
1013  pgunlock_thread();
1014  return STATUS_ERROR;
1015  }
1016  pgunlock_thread();
1017  }
1018  break;
1019 
1020  case AUTH_REQ_GSS_CONT:
1021  {
1022  int r;
1023 
1024  pglock_thread();
1025 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1026  if (conn->usesspi)
1027  r = pg_SSPI_continue(conn, payloadlen);
1028  else
1029  r = pg_GSS_continue(conn, payloadlen);
1030 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1031  r = pg_GSS_continue(conn, payloadlen);
1032 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1033  r = pg_SSPI_continue(conn, payloadlen);
1034 #endif
1035  if (r != STATUS_OK)
1036  {
1037  /* Error message already filled in. */
1038  pgunlock_thread();
1039  return STATUS_ERROR;
1040  }
1041  pgunlock_thread();
1042  }
1043  break;
1044 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1045  /* No GSSAPI *or* SSPI support */
1046  case AUTH_REQ_GSS:
1047  case AUTH_REQ_GSS_CONT:
1048  libpq_append_conn_error(conn, "GSSAPI authentication not supported");
1049  return STATUS_ERROR;
1050 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1051 
1052 #ifdef ENABLE_SSPI
1053  case AUTH_REQ_SSPI:
1054 
1055  /*
1056  * SSPI has its own startup message so libpq can decide which
1057  * method to use. Indicate to pg_SSPI_startup that we want SSPI
1058  * negotiation instead of Kerberos.
1059  */
1060  pglock_thread();
1061  if (pg_SSPI_startup(conn, 1, payloadlen) != STATUS_OK)
1062  {
1063  /* Error message already filled in. */
1064  pgunlock_thread();
1065  return STATUS_ERROR;
1066  }
1067  pgunlock_thread();
1068  break;
1069 #else
1070 
1071  /*
1072  * No SSPI support. However, if we have GSSAPI but not SSPI
1073  * support, AUTH_REQ_SSPI will have been handled in the codepath
1074  * for AUTH_REQ_GSS above, so don't duplicate the case label in
1075  * that case.
1076  */
1077 #if !defined(ENABLE_GSS)
1078  case AUTH_REQ_SSPI:
1079  libpq_append_conn_error(conn, "SSPI authentication not supported");
1080  return STATUS_ERROR;
1081 #endif /* !define(ENABLE_GSS) */
1082 #endif /* ENABLE_SSPI */
1083 
1084 
1085  case AUTH_REQ_CRYPT:
1086  libpq_append_conn_error(conn, "Crypt authentication not supported");
1087  return STATUS_ERROR;
1088 
1089  case AUTH_REQ_MD5:
1090  case AUTH_REQ_PASSWORD:
1091  {
1092  char *password;
1093 
1094  conn->password_needed = true;
1096  if (password == NULL)
1097  password = conn->pgpass;
1098  if (password == NULL || password[0] == '\0')
1099  {
1102  return STATUS_ERROR;
1103  }
1105  {
1107  "fe_sendauth: error sending password authentication\n");
1108  return STATUS_ERROR;
1109  }
1110 
1111  /* We expect no further authentication requests. */
1112  conn->client_finished_auth = true;
1113  break;
1114  }
1115 
1116  case AUTH_REQ_SASL:
1117 
1118  /*
1119  * The request contains the name (as assigned by IANA) of the
1120  * authentication mechanism.
1121  */
1122  if (pg_SASL_init(conn, payloadlen) != STATUS_OK)
1123  {
1124  /* pg_SASL_init already set the error message */
1125  return STATUS_ERROR;
1126  }
1127  break;
1128 
1129  case AUTH_REQ_SASL_CONT:
1130  case AUTH_REQ_SASL_FIN:
1131  if (conn->sasl_state == NULL)
1132  {
1134  "fe_sendauth: invalid authentication request from server: AUTH_REQ_SASL_CONT without AUTH_REQ_SASL\n");
1135  return STATUS_ERROR;
1136  }
1137  oldmsglen = conn->errorMessage.len;
1138  if (pg_SASL_continue(conn, payloadlen,
1139  (areq == AUTH_REQ_SASL_FIN)) != STATUS_OK)
1140  {
1141  /* Use this message if pg_SASL_continue didn't supply one */
1142  if (conn->errorMessage.len == oldmsglen)
1144  "fe_sendauth: error in SASL authentication\n");
1145  return STATUS_ERROR;
1146  }
1147  break;
1148 
1149  default:
1150  libpq_append_conn_error(conn, "authentication method %u not supported", areq);
1151  return STATUS_ERROR;
1152  }
1153 
1154  return STATUS_OK;
1155 }
1156 
1157 
1158 /*
1159  * pg_fe_getusername
1160  *
1161  * Returns a pointer to malloc'd space containing the name of the
1162  * specified user_id. If there is an error, return NULL, and append
1163  * a suitable error message to *errorMessage if that's not NULL.
1164  *
1165  * Caution: on Windows, the user_id argument is ignored, and we always
1166  * fetch the current user's name.
1167  */
1168 char *
1169 pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
1170 {
1171  char *result = NULL;
1172  const char *name = NULL;
1173 
1174 #ifdef WIN32
1175  /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1176  char username[256 + 1];
1177  DWORD namesize = sizeof(username);
1178 #else
1179  char pwdbuf[BUFSIZ];
1180 #endif
1181 
1182 #ifdef WIN32
1183  if (GetUserName(username, &namesize))
1184  name = username;
1185  else if (errorMessage)
1186  libpq_append_error(errorMessage,
1187  "user name lookup failure: error code %lu",
1188  GetLastError());
1189 #else
1190  if (pg_get_user_name(user_id, pwdbuf, sizeof(pwdbuf)))
1191  name = pwdbuf;
1192  else if (errorMessage)
1193  appendPQExpBuffer(errorMessage, "%s\n", pwdbuf);
1194 #endif
1195 
1196  if (name)
1197  {
1198  result = strdup(name);
1199  if (result == NULL && errorMessage)
1200  libpq_append_error(errorMessage, "out of memory");
1201  }
1202 
1203  return result;
1204 }
1205 
1206 /*
1207  * pg_fe_getauthname
1208  *
1209  * Returns a pointer to malloc'd space containing whatever name the user
1210  * has authenticated to the system. If there is an error, return NULL,
1211  * and append a suitable error message to *errorMessage if that's not NULL.
1212  */
1213 char *
1215 {
1216 #ifdef WIN32
1217  return pg_fe_getusername(0, errorMessage);
1218 #else
1219  return pg_fe_getusername(geteuid(), errorMessage);
1220 #endif
1221 }
1222 
1223 
1224 /*
1225  * PQencryptPassword -- exported routine to encrypt a password with MD5
1226  *
1227  * This function is equivalent to calling PQencryptPasswordConn with
1228  * "md5" as the encryption method, except that this doesn't require
1229  * a connection object. This function is deprecated, use
1230  * PQencryptPasswordConn instead.
1231  */
1232 char *
1233 PQencryptPassword(const char *passwd, const char *user)
1234 {
1235  char *crypt_pwd;
1236  const char *errstr = NULL;
1237 
1238  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1239  if (!crypt_pwd)
1240  return NULL;
1241 
1242  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
1243  {
1244  free(crypt_pwd);
1245  return NULL;
1246  }
1247 
1248  return crypt_pwd;
1249 }
1250 
1251 /*
1252  * PQencryptPasswordConn -- exported routine to encrypt a password
1253  *
1254  * This is intended to be used by client applications that wish to send
1255  * commands like ALTER USER joe PASSWORD 'pwd'. The password need not
1256  * be sent in cleartext if it is encrypted on the client side. This is
1257  * good because it ensures the cleartext password won't end up in logs,
1258  * pg_stat displays, etc. We export the function so that clients won't
1259  * be dependent on low-level details like whether the encryption is MD5
1260  * or something else.
1261  *
1262  * Arguments are a connection object, the cleartext password, the SQL
1263  * name of the user it is for, and a string indicating the algorithm to
1264  * use for encrypting the password. If algorithm is NULL, this queries
1265  * the server for the current 'password_encryption' value. If you wish
1266  * to avoid that, e.g. to avoid blocking, you can execute
1267  * 'show password_encryption' yourself before calling this function, and
1268  * pass it as the algorithm.
1269  *
1270  * Return value is a malloc'd string. The client may assume the string
1271  * doesn't contain any special characters that would require escaping.
1272  * On error, an error message is stored in the connection object, and
1273  * returns NULL.
1274  */
1275 char *
1276 PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user,
1277  const char *algorithm)
1278 {
1279 #define MAX_ALGORITHM_NAME_LEN 50
1280  char algobuf[MAX_ALGORITHM_NAME_LEN + 1];
1281  char *crypt_pwd = NULL;
1282 
1283  if (!conn)
1284  return NULL;
1285 
1287 
1288  /* If no algorithm was given, ask the server. */
1289  if (algorithm == NULL)
1290  {
1291  PGresult *res;
1292  char *val;
1293 
1294  res = PQexec(conn, "show password_encryption");
1295  if (res == NULL)
1296  {
1297  /* PQexec() should've set conn->errorMessage already */
1298  return NULL;
1299  }
1301  {
1302  /* PQexec() should've set conn->errorMessage already */
1303  PQclear(res);
1304  return NULL;
1305  }
1306  if (PQntuples(res) != 1 || PQnfields(res) != 1)
1307  {
1308  PQclear(res);
1309  libpq_append_conn_error(conn, "unexpected shape of result set returned for SHOW");
1310  return NULL;
1311  }
1312  val = PQgetvalue(res, 0, 0);
1313 
1314  if (strlen(val) > MAX_ALGORITHM_NAME_LEN)
1315  {
1316  PQclear(res);
1317  libpq_append_conn_error(conn, "\"password_encryption\" value too long");
1318  return NULL;
1319  }
1320  strcpy(algobuf, val);
1321  PQclear(res);
1322 
1323  algorithm = algobuf;
1324  }
1325 
1326  /*
1327  * Also accept "on" and "off" as aliases for "md5", because
1328  * password_encryption was a boolean before PostgreSQL 10. We refuse to
1329  * send the password in plaintext even if it was "off".
1330  */
1331  if (strcmp(algorithm, "on") == 0 ||
1332  strcmp(algorithm, "off") == 0)
1333  algorithm = "md5";
1334 
1335  /*
1336  * Ok, now we know what algorithm to use
1337  */
1338  if (strcmp(algorithm, "scram-sha-256") == 0)
1339  {
1340  const char *errstr = NULL;
1341 
1342  crypt_pwd = pg_fe_scram_build_secret(passwd,
1344  &errstr);
1345  if (!crypt_pwd)
1346  libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
1347  }
1348  else if (strcmp(algorithm, "md5") == 0)
1349  {
1350  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1351  if (crypt_pwd)
1352  {
1353  const char *errstr = NULL;
1354 
1355  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd, &errstr))
1356  {
1357  libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
1358  free(crypt_pwd);
1359  crypt_pwd = NULL;
1360  }
1361  }
1362  else
1363  libpq_append_conn_error(conn, "out of memory");
1364  }
1365  else
1366  {
1367  libpq_append_conn_error(conn, "unrecognized password encryption algorithm \"%s\"",
1368  algorithm);
1369  return NULL;
1370  }
1371 
1372  return crypt_pwd;
1373 }
1374 
1375 /*
1376  * PQchangePassword -- exported routine to change a password
1377  *
1378  * This is intended to be used by client applications that wish to
1379  * change the password for a user. The password is not sent in
1380  * cleartext because it is encrypted on the client side. This is
1381  * good because it ensures the cleartext password is never known by
1382  * the server, and therefore won't end up in logs, pg_stat displays,
1383  * etc. The password encryption is performed by PQencryptPasswordConn(),
1384  * which is passed a NULL for the algorithm argument. Hence encryption
1385  * is done according to the server's password_encryption
1386  * setting. We export the function so that clients won't be dependent
1387  * on the implementation specific details with respect to how the
1388  * server changes passwords.
1389  *
1390  * Arguments are a connection object, the SQL name of the target user,
1391  * and the cleartext password.
1392  *
1393  * Return value is the PGresult of the executed ALTER USER statement
1394  * or NULL if we never get there. The caller is responsible to PQclear()
1395  * the returned PGresult.
1396  *
1397  * PQresultStatus() should be called to check the return value for errors,
1398  * and PQerrorMessage() used to get more information about such errors.
1399  */
1400 PGresult *
1401 PQchangePassword(PGconn *conn, const char *user, const char *passwd)
1402 {
1403  char *encrypted_password = PQencryptPasswordConn(conn, passwd,
1404  user, NULL);
1405 
1406  if (!encrypted_password)
1407  {
1408  /* PQencryptPasswordConn() already registered the error */
1409  return NULL;
1410  }
1411  else
1412  {
1413  char *fmtpw = PQescapeLiteral(conn, encrypted_password,
1414  strlen(encrypted_password));
1415 
1416  /* no longer needed, so clean up now */
1417  PQfreemem(encrypted_password);
1418 
1419  if (!fmtpw)
1420  {
1421  /* PQescapeLiteral() already registered the error */
1422  return NULL;
1423  }
1424  else
1425  {
1426  char *fmtuser = PQescapeIdentifier(conn, user, strlen(user));
1427 
1428  if (!fmtuser)
1429  {
1430  /* PQescapeIdentifier() already registered the error */
1431  PQfreemem(fmtpw);
1432  return NULL;
1433  }
1434  else
1435  {
1437  PGresult *res;
1438 
1439  initPQExpBuffer(&buf);
1440  printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD %s",
1441  fmtuser, fmtpw);
1442 
1443  res = PQexec(conn, buf.data);
1444 
1445  /* clean up */
1446  termPQExpBuffer(&buf);
1447  PQfreemem(fmtuser);
1448  PQfreemem(fmtpw);
1449 
1450  return res;
1451  }
1452  }
1453  }
1454 }
void pg_GSS_error(const char *errmsg, OM_uint32 maj_stat, OM_uint32 min_stat)
#define STATUS_OK
Definition: c.h:1169
#define Assert(condition)
Definition: c.h:858
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:936
#define STATUS_ERROR
Definition: c.h:1170
SASLStatus
Definition: fe-auth-sasl.h:29
@ SASL_CONTINUE
Definition: fe-auth-sasl.h:32
@ SASL_FAILED
Definition: fe-auth-sasl.h:31
const pg_fe_sasl_mech pg_scram_mech
Definition: fe-auth-scram.c:32
char * pg_fe_scram_build_secret(const char *password, int iterations, const char **errstr)
#define MAX_ALGORITHM_NAME_LEN
static bool check_expected_areq(AuthRequest areq, PGconn *conn)
Definition: fe-auth.c:802
static int pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
Definition: fe-auth.c:628
PGresult * PQchangePassword(PGconn *conn, const char *user, const char *passwd)
Definition: fe-auth.c:1401
static int pg_SASL_init(PGconn *conn, int payloadlen)
Definition: fe-auth.c:422
int pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn)
Definition: fe-auth.c:961
static int pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
Definition: fe-auth.c:700
char * PQencryptPassword(const char *passwd, const char *user)
Definition: fe-auth.c:1233
char * pg_fe_getauthname(PQExpBuffer errorMessage)
Definition: fe-auth.c:1214
static const char * auth_method_description(AuthRequest areq)
Definition: fe-auth.c:767
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1276
char * pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
Definition: fe-auth.c:1169
#define auth_method_allowed(conn, type)
Definition: fe-auth.c:793
int pqPacketSend(PGconn *conn, char pack_type, const void *buf, size_t buf_len)
Definition: fe-connect.c:5000
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4310
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4304
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
int pg_GSS_load_servicename(PGconn *conn)
bool pg_GSS_have_cred_cache(gss_cred_id_t *cred_out)
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:253
int pqFlush(PGconn *conn)
Definition: fe-misc.c:953
int pqPutMsgStart(char msg_type, PGconn *conn)
Definition: fe-misc.c:458
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:165
int pqGets(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:136
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:202
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:152
void libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
Definition: fe-misc.c:1328
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: fe-misc.c:1357
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:517
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
FILE * output
long val
Definition: informix.c:670
static char * username
Definition: initdb.c:153
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:102
#define PQnoPasswordSupplied
Definition: libpq-fe.h:599
#define libpq_gettext(x)
Definition: libpq-int.h:911
#define pqClearConnErrorState(conn)
Definition: libpq-int.h:884
#define pglock_thread()
Definition: libpq-int.h:694
#define pgunlock_thread()
Definition: libpq-int.h:695
#define MD5_PASSWD_LEN
Definition: md5.h:26
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf, const char **errstr)
Definition: md5_common.c:145
static char * user
Definition: pg_regress.c:120
static char * buf
Definition: pg_test_fsync.c:73
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define sprintf
Definition: port.h:240
bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
Definition: user.c:28
uint32 AuthRequest
Definition: pqcomm.h:121
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
#define PQExpBufferDataBroken(buf)
Definition: pqexpbuffer.h:67
#define AUTH_REQ_SSPI
Definition: protocol.h:83
#define AUTH_REQ_SASL_CONT
Definition: protocol.h:85
#define AUTH_REQ_MAX
Definition: protocol.h:87
#define PqMsg_GSSResponse
Definition: protocol.h:30
#define AUTH_REQ_GSS
Definition: protocol.h:81
#define PqMsg_SASLResponse
Definition: protocol.h:33
#define AUTH_REQ_MD5
Definition: protocol.h:79
#define AUTH_REQ_KRB5
Definition: protocol.h:76
#define AUTH_REQ_OK
Definition: protocol.h:74
#define PqMsg_SASLInitialResponse
Definition: protocol.h:32
#define AUTH_REQ_KRB4
Definition: protocol.h:75
#define AUTH_REQ_PASSWORD
Definition: protocol.h:77
#define AUTH_REQ_GSS_CONT
Definition: protocol.h:82
#define PqMsg_PasswordMessage
Definition: protocol.h:31
#define AUTH_REQ_CRYPT
Definition: protocol.h:78
#define AUTH_REQ_SASL
Definition: protocol.h:84
#define AUTH_REQ_SASL_FIN
Definition: protocol.h:86
#define SCRAM_SHA_256_PLUS_NAME
Definition: scram-common.h:21
#define SCRAM_SHA_256_NAME
Definition: scram-common.h:20
static void error(void)
Definition: sql-dyntest.c:147
static char * password
Definition: streamutil.c:54
PGconn * conn
Definition: streamutil.c:55
char * host
Definition: libpq-int.h:352
char * password
Definition: libpq-int.h:355
char * require_auth
Definition: libpq-int.h:416
char * channel_binding
Definition: libpq-int.h:388
char * gssdelegation
Definition: libpq-int.h:412
const pg_fe_sasl_mech * sasl
Definition: libpq-int.h:557
char * pgpass
Definition: libpq-int.h:386
bool client_finished_auth
Definition: libpq-int.h:491
char * sslcertmode
Definition: libpq-int.h:402
uint32 allowed_auth_methods
Definition: libpq-int.h:489
bool auth_required
Definition: libpq-int.h:487
bool gssapi_used
Definition: libpq-int.h:481
char * pguser
Definition: libpq-int.h:385
PQExpBufferData errorMessage
Definition: libpq-int.h:633
int scram_sha_256_iterations
Definition: libpq-int.h:559
char * krbsrvname
Definition: libpq-int.h:409
char * gsslib
Definition: libpq-int.h:410
bool ssl_cert_requested
Definition: libpq-int.h:568
bool ssl_cert_sent
Definition: libpq-int.h:569
void * sasl_state
Definition: libpq-int.h:558
int whichhost
Definition: libpq-int.h:455
pg_conn_host * connhost
Definition: libpq-int.h:456
bool ssl_in_use
Definition: libpq-int.h:566
bool password_needed
Definition: libpq-int.h:480
bool(* channel_bound)(void *state)
Definition: fe-auth-sasl.h:127
void *(* init)(PGconn *conn, const char *password, const char *mech)
Definition: fe-auth-sasl.h:65
SASLStatus(* exchange)(void *state, char *input, int inputlen, char **output, int *outputlen)
Definition: fe-auth-sasl.h:109
const char * name
int uid_t
Definition: win32_port.h:244