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