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