]> granicus.if.org Git - postgresql/blob - src/backend/libpq/auth.c
216343f36c97bab18d380a61f85f4123b6859fdd
[postgresql] / src / backend / libpq / auth.c
1 /*-------------------------------------------------------------------------
2  *
3  * auth.c
4  *        Routines to handle network authentication
5  *
6  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.88 2002/08/30 16:00:41 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/socket.h>
21 #if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
22 #include <sys/uio.h>
23 #include <sys/ucred.h>
24 #include <errno.h>
25 #endif
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28
29 #include "libpq/auth.h"
30 #include "libpq/crypt.h"
31 #include "libpq/hba.h"
32 #include "libpq/libpq.h"
33 #include "libpq/password.h"
34 #include "libpq/pqformat.h"
35 #include "miscadmin.h"
36 #include "storage/ipc.h"
37
38
39 static void sendAuthRequest(Port *port, AuthRequest areq);
40 static void auth_failed(Port *port, int status);
41 static int      recv_and_check_password_packet(Port *port);
42
43 char       *pg_krb_server_keyfile;
44
45 #ifdef USE_PAM
46 #include <security/pam_appl.h>
47
48 #define PGSQL_PAM_SERVICE "postgresql"  /* Service name passed to PAM */
49
50 static int      CheckPAMAuth(Port *port, char *user, char *password);
51 static int pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
52                                          struct pam_response ** resp, void *appdata_ptr);
53
54 static struct pam_conv pam_passw_conv = {
55         &pam_passwd_conv_proc,
56         NULL
57 };
58
59 static char *pam_passwd = NULL; /* Workaround for Solaris 2.6 brokenness */
60 static Port *pam_port_cludge;   /* Workaround for passing "Port *port"
61                                                                  * into pam_passwd_conv_proc */
62 #endif   /* USE_PAM */
63
64 #ifdef KRB4
65 /*----------------------------------------------------------------
66  * MIT Kerberos authentication system - protocol version 4
67  *----------------------------------------------------------------
68  */
69
70 #include "krb.h"
71
72 /*
73  * pg_krb4_recvauth -- server routine to receive authentication information
74  *                                         from the client
75  *
76  * Nothing unusual here, except that we compare the username obtained from
77  * the client's setup packet to the authenticated name.  (We have to retain
78  * the name in the setup packet since we have to retain the ability to handle
79  * unauthenticated connections.)
80  */
81 static int
82 pg_krb4_recvauth(Port *port)
83 {
84         long            krbopts = 0;    /* one-way authentication */
85         KTEXT_ST        clttkt;
86         char            instance[INST_SZ + 1],
87                                 version[KRB_SENDAUTH_VLEN + 1];
88         AUTH_DAT        auth_data;
89         Key_schedule key_sched;
90         int                     status;
91
92         strcpy(instance, "*");          /* don't care, but arg gets expanded
93                                                                  * anyway */
94         status = krb_recvauth(krbopts,
95                                                   port->sock,
96                                                   &clttkt,
97                                                   PG_KRB_SRVNAM,
98                                                   instance,
99                                                   &port->raddr.in,
100                                                   &port->laddr.in,
101                                                   &auth_data,
102                                                   pg_krb_server_keyfile,
103                                                   key_sched,
104                                                   version);
105         if (status != KSUCCESS)
106         {
107                 elog(LOG, "pg_krb4_recvauth: kerberos error: %s",
108                          krb_err_txt[status]);
109                 return STATUS_ERROR;
110         }
111         if (strncmp(version, PG_KRB4_VERSION, KRB_SENDAUTH_VLEN) != 0)
112         {
113                 elog(LOG, "pg_krb4_recvauth: protocol version \"%s\" != \"%s\"",
114                          version, PG_KRB4_VERSION);
115                 return STATUS_ERROR;
116         }
117         if (strncmp(port->user, auth_data.pname, SM_DATABASE_USER) != 0)
118         {
119                 elog(LOG, "pg_krb4_recvauth: name \"%s\" != \"%s\"",
120                          port->user, auth_data.pname);
121                 return STATUS_ERROR;
122         }
123         return STATUS_OK;
124 }
125
126 #else
127
128 static int
129 pg_krb4_recvauth(Port *port)
130 {
131         elog(LOG, "pg_krb4_recvauth: Kerberos not implemented on this server");
132         return STATUS_ERROR;
133 }
134
135 #endif   /* KRB4 */
136
137
138 #ifdef KRB5
139 /*----------------------------------------------------------------
140  * MIT Kerberos authentication system - protocol version 5
141  *----------------------------------------------------------------
142  */
143
144 #include <krb5.h>
145 #include <com_err.h>
146
147 /*
148  * pg_an_to_ln -- return the local name corresponding to an authentication
149  *                                name
150  *
151  * XXX Assumes that the first aname component is the user name.  This is NOT
152  *         necessarily so, since an aname can actually be something out of your
153  *         worst X.400 nightmare, like
154  *                ORGANIZATION=U. C. Berkeley/NAME=Paul M. Aoki@CS.BERKELEY.EDU
155  *         Note that the MIT an_to_ln code does the same thing if you don't
156  *         provide an aname mapping database...it may be a better idea to use
157  *         krb5_an_to_ln, except that it punts if multiple components are found,
158  *         and we can't afford to punt.
159  */
160 static char *
161 pg_an_to_ln(char *aname)
162 {
163         char       *p;
164
165         if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
166                 *p = '\0';
167         return aname;
168 }
169
170
171 /*
172  * Various krb5 state which is not connection specfic, and a flag to
173  * indicate whether we have initialised it yet.
174  */
175 static int      pg_krb5_initialised;
176 static krb5_context pg_krb5_context;
177 static krb5_keytab pg_krb5_keytab;
178 static krb5_principal pg_krb5_server;
179
180
181 static int
182 pg_krb5_init(void)
183 {
184         krb5_error_code retval;
185
186         if (pg_krb5_initialised)
187                 return STATUS_OK;
188
189         retval = krb5_init_context(&pg_krb5_context);
190         if (retval)
191         {
192                 elog(LOG, "pg_krb5_init: krb5_init_context returned Kerberos error %d",
193                          retval);
194                 com_err("postgres", retval, "while initializing krb5");
195                 return STATUS_ERROR;
196         }
197
198         retval = krb5_kt_resolve(pg_krb5_context, pg_krb_server_keyfile, &pg_krb5_keytab);
199         if (retval)
200         {
201                 elog(LOG, "pg_krb5_init: krb5_kt_resolve returned Kerberos error %d",
202                          retval);
203                 com_err("postgres", retval, "while resolving keytab file %s",
204                                 pg_krb_server_keyfile);
205                 krb5_free_context(pg_krb5_context);
206                 return STATUS_ERROR;
207         }
208
209         retval = krb5_sname_to_principal(pg_krb5_context, NULL, PG_KRB_SRVNAM,
210                                                                          KRB5_NT_SRV_HST, &pg_krb5_server);
211         if (retval)
212         {
213                 elog(LOG, "pg_krb5_init: krb5_sname_to_principal returned Kerberos error %d",
214                          retval);
215                 com_err("postgres", retval,
216                                 "while getting server principal for service %s",
217                                 PG_KRB_SRVNAM);
218                 krb5_kt_close(pg_krb5_context, pg_krb5_keytab);
219                 krb5_free_context(pg_krb5_context);
220                 return STATUS_ERROR;
221         }
222
223         pg_krb5_initialised = 1;
224         return STATUS_OK;
225 }
226
227
228 /*
229  * pg_krb5_recvauth -- server routine to receive authentication information
230  *                                         from the client
231  *
232  * We still need to compare the username obtained from the client's setup
233  * packet to the authenticated name, as described in pg_krb4_recvauth.  This
234  * is a bit more problematic in v5, as described above in pg_an_to_ln.
235  *
236  * We have our own keytab file because postgres is unlikely to run as root,
237  * and so cannot read the default keytab.
238  */
239 static int
240 pg_krb5_recvauth(Port *port)
241 {
242         krb5_error_code retval;
243         int                     ret;
244         krb5_auth_context auth_context = NULL;
245         krb5_ticket *ticket;
246         char       *kusername;
247
248         ret = pg_krb5_init();
249         if (ret != STATUS_OK)
250                 return ret;
251
252         retval = krb5_recvauth(pg_krb5_context, &auth_context,
253                                                    (krb5_pointer) & port->sock, PG_KRB_SRVNAM,
254                                                    pg_krb5_server, 0, pg_krb5_keytab, &ticket);
255         if (retval)
256         {
257                 elog(LOG, "pg_krb5_recvauth: krb5_recvauth returned Kerberos error %d",
258                          retval);
259                 com_err("postgres", retval, "from krb5_recvauth");
260                 return STATUS_ERROR;
261         }
262
263         /*
264          * The "client" structure comes out of the ticket and is therefore
265          * authenticated.  Use it to check the username obtained from the
266          * postmaster startup packet.
267          *
268          * I have no idea why this is considered necessary.
269          */
270 #if defined(HAVE_KRB5_TICKET_ENC_PART2)
271         retval = krb5_unparse_name(pg_krb5_context,
272                                                            ticket->enc_part2->client, &kusername);
273 #elif defined(HAVE_KRB5_TICKET_CLIENT)
274         retval = krb5_unparse_name(pg_krb5_context,
275                                                            ticket->client, &kusername);
276 #else
277 #error "bogus configuration"
278 #endif
279         if (retval)
280         {
281                 elog(LOG, "pg_krb5_recvauth: krb5_unparse_name returned Kerberos error %d",
282                          retval);
283                 com_err("postgres", retval, "while unparsing client name");
284                 krb5_free_ticket(pg_krb5_context, ticket);
285                 krb5_auth_con_free(pg_krb5_context, auth_context);
286                 return STATUS_ERROR;
287         }
288
289         kusername = pg_an_to_ln(kusername);
290         if (strncmp(port->user, kusername, SM_DATABASE_USER))
291         {
292                 elog(LOG, "pg_krb5_recvauth: user name \"%s\" != krb5 name \"%s\"",
293                          port->user, kusername);
294                 ret = STATUS_ERROR;
295         }
296         else
297                 ret = STATUS_OK;
298
299         krb5_free_ticket(pg_krb5_context, ticket);
300         krb5_auth_con_free(pg_krb5_context, auth_context);
301         free(kusername);
302
303         return ret;
304 }
305
306 #else
307
308 static int
309 pg_krb5_recvauth(Port *port)
310 {
311         elog(LOG, "pg_krb5_recvauth: Kerberos not implemented on this server");
312         return STATUS_ERROR;
313 }
314
315 #endif   /* KRB5 */
316
317
318 /*
319  * Tell the user the authentication failed, but not (much about) why.
320  *
321  * There is a tradeoff here between security concerns and making life
322  * unnecessarily difficult for legitimate users.  We would not, for example,
323  * want to report the password we were expecting to receive...
324  * But it seems useful to report the username and authorization method
325  * in use, and these are items that must be presumed known to an attacker
326  * anyway.
327  * Note that many sorts of failure report additional information in the
328  * postmaster log, which we hope is only readable by good guys.
329  */
330 static void
331 auth_failed(Port *port, int status)
332 {
333         const char *authmethod = "Unknown auth method:";
334
335         /*
336          * If we failed due to EOF from client, just quit; there's no point in
337          * trying to send a message to the client, and not much point in
338          * logging the failure in the postmaster log.  (Logging the failure
339          * might be desirable, were it not for the fact that libpq closes the
340          * connection unceremoniously if challenged for a password when it
341          * hasn't got one to send.  We'll get a useless log entry for every
342          * psql connection under password auth, even if it's perfectly
343          * successful, if we log STATUS_EOF events.)
344          */
345         if (status == STATUS_EOF)
346                 proc_exit(0);
347
348         switch (port->auth_method)
349         {
350                 case uaReject:
351                         authmethod = "Rejected host:";
352                         break;
353                 case uaKrb4:
354                         authmethod = "Kerberos4";
355                         break;
356                 case uaKrb5:
357                         authmethod = "Kerberos5";
358                         break;
359                 case uaTrust:
360                         authmethod = "Trusted";
361                         break;
362                 case uaIdent:
363                         authmethod = "IDENT";
364                         break;
365                 case uaMD5:
366                 case uaCrypt:
367                 case uaPassword:
368                         authmethod = "Password";
369                         break;
370 #ifdef USE_PAM
371                 case uaPAM:
372                         authmethod = "PAM";
373                         break;
374 #endif   /* USE_PAM */
375         }
376
377         elog(FATAL, "%s authentication failed for user \"%s\"",
378                  authmethod, port->user);
379         /* doesn't return */
380 }
381
382
383 /*
384  * Client authentication starts here.  If there is an error, this
385  * function does not return and the backend process is terminated.
386  */
387 void
388 ClientAuthentication(Port *port)
389 {
390         int                     status = STATUS_ERROR;
391
392         /*
393          * Get the authentication method to use for this frontend/database
394          * combination.  Note: a failure return indicates a problem with the
395          * hba config file, not with the request.  hba.c should have dropped
396          * an error message into the postmaster logfile if it failed.
397          */
398         if (hba_getauthmethod(port) != STATUS_OK)
399                 elog(FATAL, "Missing or erroneous pg_hba.conf file, see postmaster log for details");
400
401         switch (port->auth_method)
402         {
403                 case uaReject:
404
405                         /*
406                          * This could have come from an explicit "reject" entry in
407                          * pg_hba.conf, but more likely it means there was no matching
408                          * entry.  Take pity on the poor user and issue a helpful
409                          * error message.  NOTE: this is not a security breach,
410                          * because all the info reported here is known at the frontend
411                          * and must be assumed known to bad guys. We're merely helping
412                          * out the less clueful good guys.
413                          */
414                         {
415                                 const char *hostinfo = "localhost";
416
417                                 if (port->raddr.sa.sa_family == AF_INET)
418                                         hostinfo = inet_ntoa(port->raddr.in.sin_addr);
419                                 elog(FATAL,
420                                          "No pg_hba.conf entry for host %s, user %s, database %s",
421                                          hostinfo, port->user, port->database);
422                                 break;
423                         }
424
425                 case uaKrb4:
426                         sendAuthRequest(port, AUTH_REQ_KRB4);
427                         status = pg_krb4_recvauth(port);
428                         break;
429
430                 case uaKrb5:
431                         sendAuthRequest(port, AUTH_REQ_KRB5);
432                         status = pg_krb5_recvauth(port);
433                         break;
434
435                 case uaIdent:
436 #if !defined(SO_PEERCRED) && (defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || (defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS)))
437
438                         /*
439                          * If we are doing ident on unix-domain sockets, use SCM_CREDS
440                          * only if it is defined and SO_PEERCRED isn't.
441                          */
442 #if defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
443
444                         /*
445                          * Receive credentials on next message receipt, BSD/OS,
446                          * NetBSD. We need to set this before the client sends the
447                          * next packet.
448                          */
449                         {
450                                 int                     on = 1;
451
452                                 if (setsockopt(port->sock, 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
453                                         elog(FATAL, "pg_local_sendauth: can't do setsockopt: %m");
454                         }
455 #endif
456                         if (port->raddr.sa.sa_family == AF_UNIX)
457                                 sendAuthRequest(port, AUTH_REQ_SCM_CREDS);
458 #endif
459                         status = authident(port);
460                         break;
461
462                 case uaMD5:
463                         sendAuthRequest(port, AUTH_REQ_MD5);
464                         status = recv_and_check_password_packet(port);
465                         break;
466
467                 case uaCrypt:
468                         sendAuthRequest(port, AUTH_REQ_CRYPT);
469                         status = recv_and_check_password_packet(port);
470                         break;
471
472                 case uaPassword:
473                         sendAuthRequest(port, AUTH_REQ_PASSWORD);
474                         status = recv_and_check_password_packet(port);
475                         break;
476
477 #ifdef USE_PAM
478                 case uaPAM:
479                         pam_port_cludge = port;
480                         status = CheckPAMAuth(port, port->user, "");
481                         break;
482 #endif   /* USE_PAM */
483
484                 case uaTrust:
485                         status = STATUS_OK;
486                         break;
487         }
488
489         if (status == STATUS_OK)
490                 sendAuthRequest(port, AUTH_REQ_OK);
491         else
492                 auth_failed(port, status);
493 }
494
495
496 /*
497  * Send an authentication request packet to the frontend.
498  */
499 static void
500 sendAuthRequest(Port *port, AuthRequest areq)
501 {
502         StringInfoData buf;
503
504         pq_beginmessage(&buf);
505         pq_sendbyte(&buf, 'R');
506         pq_sendint(&buf, (int32) areq, sizeof(int32));
507
508         /* Add the salt for encrypted passwords. */
509         if (areq == AUTH_REQ_MD5)
510                 pq_sendbytes(&buf, port->md5Salt, 4);
511         else if (areq == AUTH_REQ_CRYPT)
512                 pq_sendbytes(&buf, port->cryptSalt, 2);
513
514         pq_endmessage(&buf);
515
516         /*
517          * Flush message so client will see it, except for AUTH_REQ_OK,
518          * which need not be sent until we are ready for queries.
519          */
520         if (areq != AUTH_REQ_OK)
521                 pq_flush();
522 }
523
524
525 #ifdef USE_PAM
526
527 /*
528  * PAM conversation function
529  */
530
531 static int
532 pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr)
533 {
534         StringInfoData buf;
535         int32           len;
536
537         if (num_msg != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF)
538         {
539                 switch (msg[0]->msg_style)
540                 {
541                         case PAM_ERROR_MSG:
542                                 elog(LOG, "pam_passwd_conv_proc: Error from underlying PAM layer: '%s'",
543                                          msg[0]->msg);
544                                 return PAM_CONV_ERR;
545                         default:
546                                 elog(LOG, "pam_passwd_conv_proc: Unexpected PAM conversation %d/'%s'",
547                                          msg[0]->msg_style, msg[0]->msg);
548                                 return PAM_CONV_ERR;
549                 }
550         }
551
552         if (!appdata_ptr)
553         {
554                 /*
555                  * Workaround for Solaris 2.6 where the PAM library is broken and
556                  * does not pass appdata_ptr to the conversation routine
557                  */
558                 appdata_ptr = pam_passwd;
559         }
560
561         /*
562          * Password wasn't passed to PAM the first time around - let's go ask
563          * the client to send a password, which we then stuff into PAM.
564          */
565         if (strlen(appdata_ptr) == 0)
566         {
567                 sendAuthRequest(pam_port_cludge, AUTH_REQ_PASSWORD);
568                 if (pq_eof() == EOF || pq_getint(&len, 4) == EOF)
569                 {
570                         return PAM_CONV_ERR;    /* client didn't want to send password */
571                 }
572
573                 initStringInfo(&buf);
574                 pq_getstr(&buf);
575
576                 /* Do not echo failed password to logs, for security. */
577                 elog(DEBUG5, "received PAM packet");
578
579                 if (strlen(buf.data) == 0)
580                 {
581                         elog(LOG, "pam_passwd_conv_proc: no password");
582                         return PAM_CONV_ERR;
583                 }
584                 appdata_ptr = buf.data;
585         }
586
587         /*
588          * Explicitly not using palloc here - PAM will free this memory in
589          * pam_end()
590          */
591         *resp = calloc(num_msg, sizeof(struct pam_response));
592         if (!*resp)
593         {
594                 elog(LOG, "pam_passwd_conv_proc: Out of memory!");
595                 if (buf.data)
596                         pfree(buf.data);
597                 return PAM_CONV_ERR;
598         }
599
600         (*resp)[0].resp = strdup((char *) appdata_ptr);
601         (*resp)[0].resp_retcode = 0;
602
603         return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
604 }
605
606
607 /*
608  * Check authentication against PAM.
609  */
610 static int
611 CheckPAMAuth(Port *port, char *user, char *password)
612 {
613         int                     retval;
614         pam_handle_t *pamh = NULL;
615
616         /*
617          * Apparently, Solaris 2.6 is broken, and needs ugly static variable
618          * workaround
619          */
620         pam_passwd = password;
621
622         /*
623          * Set the application data portion of the conversation struct This is
624          * later used inside the PAM conversation to pass the password to the
625          * authentication module.
626          */
627         pam_passw_conv.appdata_ptr = (char *) password;         /* from password above,
628                                                                                                                  * not allocated */
629
630         /* Optionally, one can set the service name in pg_hba.conf */
631         if (port->auth_arg[0] == '\0')
632                 retval = pam_start(PGSQL_PAM_SERVICE, "pgsql@", &pam_passw_conv, &pamh);
633         else
634                 retval = pam_start(port->auth_arg, "pgsql@", &pam_passw_conv, &pamh);
635
636         if (retval != PAM_SUCCESS)
637         {
638                 elog(LOG, "CheckPAMAuth: Failed to create PAM authenticator: '%s'",
639                          pam_strerror(pamh, retval));
640                 pam_passwd = NULL;              /* Unset pam_passwd */
641                 return STATUS_ERROR;
642         }
643
644         retval = pam_set_item(pamh, PAM_USER, user);
645
646         if (retval != PAM_SUCCESS)
647         {
648                 elog(LOG, "CheckPAMAuth: pam_set_item(PAM_USER) failed: '%s'",
649                          pam_strerror(pamh, retval));
650                 pam_passwd = NULL;              /* Unset pam_passwd */
651                 return STATUS_ERROR;
652         }
653
654         retval = pam_set_item(pamh, PAM_CONV, &pam_passw_conv);
655
656         if (retval != PAM_SUCCESS)
657         {
658                 elog(LOG, "CheckPAMAuth: pam_set_item(PAM_CONV) failed: '%s'",
659                          pam_strerror(pamh, retval));
660                 pam_passwd = NULL;              /* Unset pam_passwd */
661                 return STATUS_ERROR;
662         }
663
664         retval = pam_authenticate(pamh, 0);
665
666         if (retval != PAM_SUCCESS)
667         {
668                 elog(LOG, "CheckPAMAuth: pam_authenticate failed: '%s'",
669                          pam_strerror(pamh, retval));
670                 pam_passwd = NULL;              /* Unset pam_passwd */
671                 return STATUS_ERROR;
672         }
673
674         retval = pam_acct_mgmt(pamh, 0);
675
676         if (retval != PAM_SUCCESS)
677         {
678                 elog(LOG, "CheckPAMAuth: pam_acct_mgmt failed: '%s'",
679                          pam_strerror(pamh, retval));
680                 pam_passwd = NULL;              /* Unset pam_passwd */
681                 return STATUS_ERROR;
682         }
683
684         retval = pam_end(pamh, retval);
685
686         if (retval != PAM_SUCCESS)
687         {
688                 elog(LOG, "CheckPAMAuth: Failed to release PAM authenticator: '%s'",
689                          pam_strerror(pamh, retval));
690         }
691
692         pam_passwd = NULL;              /* Unset pam_passwd */
693
694         return (retval == PAM_SUCCESS ? STATUS_OK : STATUS_ERROR);
695 }
696 #endif   /* USE_PAM */
697
698
699 /*
700  * Called when we have received the password packet.
701  */
702 static int
703 recv_and_check_password_packet(Port *port)
704 {
705         StringInfoData buf;
706         int32           len;
707         int                     result;
708
709         if (pq_eof() == EOF || pq_getint(&len, 4) == EOF)
710                 return STATUS_EOF;              /* client didn't want to send password */
711
712         initStringInfo(&buf);
713         if (pq_getstr(&buf) == EOF) /* receive password */
714         {
715                 pfree(buf.data);
716                 return STATUS_EOF;
717         }
718         /*
719          * We don't actually use the password packet length the frontend
720          * sent us; however, it's a reasonable sanity check to ensure that
721          * we actually read as much data as we expected to.
722          *
723          * The password packet size is the length of the buffer, plus the
724          * size field itself (4 bytes), plus a 1-byte terminator.
725          */
726         if (len != (buf.len + 4 + 1))
727                 elog(LOG, "unexpected password packet size: read %d, expected %d",
728                          buf.len + 4 + 1, len);
729
730         /* Do not echo password to logs, for security. */
731         elog(DEBUG5, "received password packet");
732
733         result = md5_crypt_verify(port, port->user, buf.data);
734
735         pfree(buf.data);
736         return result;
737 }