auth.c 19.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * auth.c
4
 *	  Routines to handle network authentication
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
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.93 2002/12/06 03:46:24 momjian Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16

#include "postgres.h"
17

18
#include <sys/param.h>
19 20 21
#include <sys/socket.h>
#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
#include <sys/uio.h>
22 23 24
#include <sys/ucred.h>
#include <errno.h>
#endif
25 26
#include <netinet/in.h>
#include <arpa/inet.h>
27

28
#include "libpq/auth.h"
B
Bruce Momjian 已提交
29
#include "libpq/crypt.h"
30
#include "libpq/hba.h"
B
Bruce Momjian 已提交
31
#include "libpq/libpq.h"
32
#include "libpq/password.h"
33
#include "libpq/pqformat.h"
B
Bruce Momjian 已提交
34
#include "miscadmin.h"
35 36
#include "storage/ipc.h"

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

B
Bruce Momjian 已提交
42
char	   *pg_krb_server_keyfile;
43

B
Bruce Momjian 已提交
44 45 46
#ifdef USE_PAM
#include <security/pam_appl.h>

47 48 49 50 51
#define PGSQL_PAM_SERVICE "postgresql"	/* Service name passed to PAM */

static int	CheckPAMAuth(Port *port, char *user, char *password);
static int pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
					 struct pam_response ** resp, void *appdata_ptr);
B
Bruce Momjian 已提交
52 53

static struct pam_conv pam_passw_conv = {
54 55
	&pam_passwd_conv_proc,
	NULL
B
Bruce Momjian 已提交
56 57
};

58 59 60
static char *pam_passwd = NULL; /* Workaround for Solaris 2.6 brokenness */
static Port *pam_port_cludge;	/* Workaround for passing "Port *port"
								 * into pam_passwd_conv_proc */
61
#endif   /* USE_PAM */
62

63 64 65 66 67 68
#ifdef KRB4
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 4
 *----------------------------------------------------------------
 */

69
#include "krb.h"
70 71 72

/*
 * pg_krb4_recvauth -- server routine to receive authentication information
73
 *					   from the client
74 75 76 77 78 79 80
 *
 * Nothing unusual here, except that we compare the username obtained from
 * the client's setup packet to the authenticated name.  (We have to retain
 * the name in the setup packet since we have to retain the ability to handle
 * unauthenticated connections.)
 */
static int
B
Bruce Momjian 已提交
81
pg_krb4_recvauth(Port *port)
82
{
B
Bruce Momjian 已提交
83 84 85 86 87 88 89
	long		krbopts = 0;	/* one-way authentication */
	KTEXT_ST	clttkt;
	char		instance[INST_SZ + 1],
				version[KRB_SENDAUTH_VLEN + 1];
	AUTH_DAT	auth_data;
	Key_schedule key_sched;
	int			status;
90 91 92 93

	strcpy(instance, "*");		/* don't care, but arg gets expanded
								 * anyway */
	status = krb_recvauth(krbopts,
94
						  port->sock,
95 96 97
						  &clttkt,
						  PG_KRB_SRVNAM,
						  instance,
98 99
						  &port->raddr.in,
						  &port->laddr.in,
100
						  &auth_data,
101
						  pg_krb_server_keyfile,
102 103 104 105
						  key_sched,
						  version);
	if (status != KSUCCESS)
	{
106 107
		elog(LOG, "pg_krb4_recvauth: kerberos error: %s",
			 krb_err_txt[status]);
108
		return STATUS_ERROR;
109
	}
110
	if (strncmp(version, PG_KRB4_VERSION, KRB_SENDAUTH_VLEN) != 0)
111
	{
112 113
		elog(LOG, "pg_krb4_recvauth: protocol version \"%s\" != \"%s\"",
			 version, PG_KRB4_VERSION);
114
		return STATUS_ERROR;
115
	}
116
	if (strncmp(port->user, auth_data.pname, SM_DATABASE_USER) != 0)
117
	{
118 119
		elog(LOG, "pg_krb4_recvauth: name \"%s\" != \"%s\"",
			 port->user, auth_data.pname);
120
		return STATUS_ERROR;
121
	}
122
	return STATUS_OK;
123 124
}

125
#else
126

127
static int
128
pg_krb4_recvauth(Port *port)
129
{
130
	elog(LOG, "pg_krb4_recvauth: Kerberos not implemented on this server");
131
	return STATUS_ERROR;
132
}
133
#endif   /* KRB4 */
134

135

136 137 138 139 140 141
#ifdef KRB5
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 5
 *----------------------------------------------------------------
 */

B
Bruce Momjian 已提交
142 143
#include <krb5.h>
#include <com_err.h>
144 145 146

/*
 * pg_an_to_ln -- return the local name corresponding to an authentication
147
 *				  name
148 149
 *
 * XXX Assumes that the first aname component is the user name.  This is NOT
150 151 152 153 154 155 156
 *	   necessarily so, since an aname can actually be something out of your
 *	   worst X.400 nightmare, like
 *		  ORGANIZATION=U. C. Berkeley/NAME=Paul M. Aoki@CS.BERKELEY.EDU
 *	   Note that the MIT an_to_ln code does the same thing if you don't
 *	   provide an aname mapping database...it may be a better idea to use
 *	   krb5_an_to_ln, except that it punts if multiple components are found,
 *	   and we can't afford to punt.
157
 */
158
static char *
159 160
pg_an_to_ln(char *aname)
{
161
	char	   *p;
162 163 164

	if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
		*p = '\0';
165
	return aname;
166 167
}

B
Bruce Momjian 已提交
168

B
Bruce Momjian 已提交
169
/*
B
Bruce Momjian 已提交
170 171
 * Various krb5 state which is not connection specfic, and a flag to
 * indicate whether we have initialised it yet.
B
Bruce Momjian 已提交
172
 */
B
Bruce Momjian 已提交
173
static int	pg_krb5_initialised;
B
Bruce Momjian 已提交
174 175 176 177 178
static krb5_context pg_krb5_context;
static krb5_keytab pg_krb5_keytab;
static krb5_principal pg_krb5_server;


B
Bruce Momjian 已提交
179
static int
B
Bruce Momjian 已提交
180
pg_krb5_init(void)
B
Bruce Momjian 已提交
181
{
B
Bruce Momjian 已提交
182
	krb5_error_code retval;
183

B
Bruce Momjian 已提交
184 185 186 187
	if (pg_krb5_initialised)
		return STATUS_OK;

	retval = krb5_init_context(&pg_krb5_context);
B
Bruce Momjian 已提交
188 189
	if (retval)
	{
190 191
		elog(LOG, "pg_krb5_init: krb5_init_context returned Kerberos error %d",
			 retval);
B
Bruce Momjian 已提交
192
		com_err("postgres", retval, "while initializing krb5");
B
Bruce Momjian 已提交
193
		return STATUS_ERROR;
194 195
	}

196
	retval = krb5_kt_resolve(pg_krb5_context, pg_krb_server_keyfile, &pg_krb5_keytab);
B
Bruce Momjian 已提交
197 198
	if (retval)
	{
199 200
		elog(LOG, "pg_krb5_init: krb5_kt_resolve returned Kerberos error %d",
			 retval);
B
Bruce Momjian 已提交
201
		com_err("postgres", retval, "while resolving keytab file %s",
202
				pg_krb_server_keyfile);
B
Bruce Momjian 已提交
203 204
		krb5_free_context(pg_krb5_context);
		return STATUS_ERROR;
205 206
	}

B
Bruce Momjian 已提交
207
	retval = krb5_sname_to_principal(pg_krb5_context, NULL, PG_KRB_SRVNAM,
B
Bruce Momjian 已提交
208
									 KRB5_NT_SRV_HST, &pg_krb5_server);
B
Bruce Momjian 已提交
209 210
	if (retval)
	{
211 212
		elog(LOG, "pg_krb5_init: krb5_sname_to_principal returned Kerberos error %d",
			 retval);
B
Bruce Momjian 已提交
213
		com_err("postgres", retval,
B
Bruce Momjian 已提交
214
				"while getting server principal for service %s",
215
				PG_KRB_SRVNAM);
B
Bruce Momjian 已提交
216 217
		krb5_kt_close(pg_krb5_context, pg_krb5_keytab);
		krb5_free_context(pg_krb5_context);
218 219
		return STATUS_ERROR;
	}
B
Bruce Momjian 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	pg_krb5_initialised = 1;
	return STATUS_OK;
}


/*
 * pg_krb5_recvauth -- server routine to receive authentication information
 *					   from the client
 *
 * We still need to compare the username obtained from the client's setup
 * packet to the authenticated name, as described in pg_krb4_recvauth.	This
 * is a bit more problematic in v5, as described above in pg_an_to_ln.
 *
 * We have our own keytab file because postgres is unlikely to run as root,
 * and so cannot read the default keytab.
 */
static int
pg_krb5_recvauth(Port *port)
{
	krb5_error_code retval;
B
Bruce Momjian 已提交
241
	int			ret;
B
Bruce Momjian 已提交
242 243
	krb5_auth_context auth_context = NULL;
	krb5_ticket *ticket;
B
Bruce Momjian 已提交
244
	char	   *kusername;
B
Bruce Momjian 已提交
245 246 247 248 249 250

	ret = pg_krb5_init();
	if (ret != STATUS_OK)
		return ret;

	retval = krb5_recvauth(pg_krb5_context, &auth_context,
B
Bruce Momjian 已提交
251
						   (krb5_pointer) & port->sock, PG_KRB_SRVNAM,
B
Bruce Momjian 已提交
252
						   pg_krb5_server, 0, pg_krb5_keytab, &ticket);
B
Bruce Momjian 已提交
253 254
	if (retval)
	{
255 256
		elog(LOG, "pg_krb5_recvauth: krb5_recvauth returned Kerberos error %d",
			 retval);
B
Bruce Momjian 已提交
257
		com_err("postgres", retval, "from krb5_recvauth");
B
Bruce Momjian 已提交
258
		return STATUS_ERROR;
B
Bruce Momjian 已提交
259
	}
260 261 262 263 264

	/*
	 * The "client" structure comes out of the ticket and is therefore
	 * authenticated.  Use it to check the username obtained from the
	 * postmaster startup packet.
B
Bruce Momjian 已提交
265 266
	 *
	 * I have no idea why this is considered necessary.
267
	 */
268
#if defined(HAVE_KRB5_TICKET_ENC_PART2)
B
Bruce Momjian 已提交
269
	retval = krb5_unparse_name(pg_krb5_context,
B
Bruce Momjian 已提交
270
							   ticket->enc_part2->client, &kusername);
271 272 273 274 275 276
#elif defined(HAVE_KRB5_TICKET_CLIENT)
	retval = krb5_unparse_name(pg_krb5_context,
							   ticket->client, &kusername);
#else
#error "bogus configuration"
#endif
B
Bruce Momjian 已提交
277 278
	if (retval)
	{
279 280
		elog(LOG, "pg_krb5_recvauth: krb5_unparse_name returned Kerberos error %d",
			 retval);
B
Bruce Momjian 已提交
281
		com_err("postgres", retval, "while unparsing client name");
B
Bruce Momjian 已提交
282 283
		krb5_free_ticket(pg_krb5_context, ticket);
		krb5_auth_con_free(pg_krb5_context, auth_context);
284
		return STATUS_ERROR;
285
	}
B
Bruce Momjian 已提交
286

287
	kusername = pg_an_to_ln(kusername);
288
	if (strncmp(port->user, kusername, SM_DATABASE_USER))
289
	{
290 291
		elog(LOG, "pg_krb5_recvauth: user name \"%s\" != krb5 name \"%s\"",
			 port->user, kusername);
B
Bruce Momjian 已提交
292
		ret = STATUS_ERROR;
293
	}
B
Bruce Momjian 已提交
294 295
	else
		ret = STATUS_OK;
B
Bruce Momjian 已提交
296

B
Bruce Momjian 已提交
297 298 299 300 301
	krb5_free_ticket(pg_krb5_context, ticket);
	krb5_auth_con_free(pg_krb5_context, auth_context);
	free(kusername);

	return ret;
302 303
}

304
#else
305

306
static int
307
pg_krb5_recvauth(Port *port)
308
{
309
	elog(LOG, "pg_krb5_recvauth: Kerberos not implemented on this server");
310
	return STATUS_ERROR;
311
}
312
#endif   /* KRB5 */
313

314 315

/*
316 317 318 319 320 321 322 323 324 325
 * Tell the user the authentication failed, but not (much about) why.
 *
 * There is a tradeoff here between security concerns and making life
 * unnecessarily difficult for legitimate users.  We would not, for example,
 * want to report the password we were expecting to receive...
 * But it seems useful to report the username and authorization method
 * in use, and these are items that must be presumed known to an attacker
 * anyway.
 * Note that many sorts of failure report additional information in the
 * postmaster log, which we hope is only readable by good guys.
326
 */
327
static void
328
auth_failed(Port *port, int status)
329
{
330 331
	const char *authmethod = "Unknown auth method:";

332
	/*
333 334
	 * If we failed due to EOF from client, just quit; there's no point in
	 * trying to send a message to the client, and not much point in
335 336 337
	 * logging the failure in the postmaster log.  (Logging the failure
	 * might be desirable, were it not for the fact that libpq closes the
	 * connection unceremoniously if challenged for a password when it
338 339
	 * hasn't got one to send.  We'll get a useless log entry for every
	 * psql connection under password auth, even if it's perfectly
340 341 342 343 344
	 * successful, if we log STATUS_EOF events.)
	 */
	if (status == STATUS_EOF)
		proc_exit(0);

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	switch (port->auth_method)
	{
		case uaReject:
			authmethod = "Rejected host:";
			break;
		case uaKrb4:
			authmethod = "Kerberos4";
			break;
		case uaKrb5:
			authmethod = "Kerberos5";
			break;
		case uaTrust:
			authmethod = "Trusted";
			break;
		case uaIdent:
			authmethod = "IDENT";
			break;
362
		case uaMD5:
363
		case uaCrypt:
364
		case uaPassword:
365 366
			authmethod = "Password";
			break;
B
Bruce Momjian 已提交
367 368 369 370
#ifdef USE_PAM
		case uaPAM:
			authmethod = "PAM";
			break;
371
#endif   /* USE_PAM */
372 373
	}

374 375
	elog(FATAL, "%s authentication failed for user \"%s\"",
		 authmethod, port->user);
376
	/* doesn't return */
377 378
}

379

380
/*
381 382
 * Client authentication starts here.  If there is an error, this
 * function does not return and the backend process is terminated.
383
 */
384
void
385
ClientAuthentication(Port *port)
386
{
387
	int			status = STATUS_ERROR;
388

389
	/*
390
	 * Get the authentication method to use for this frontend/database
B
Bruce Momjian 已提交
391 392 393
	 * combination.  Note: a failure return indicates a problem with the
	 * hba config file, not with the request.  hba.c should have dropped
	 * an error message into the postmaster logfile if it failed.
394
	 */
395
	if (hba_getauthmethod(port) != STATUS_OK)
396
		elog(FATAL, "Missing or erroneous pg_hba.conf file, see postmaster log for details");
397

398 399 400 401
	switch (port->auth_method)
	{
		case uaReject:

402 403 404 405 406 407 408 409 410 411 412
			/*
			 * This could have come from an explicit "reject" entry in
			 * pg_hba.conf, but more likely it means there was no matching
			 * entry.  Take pity on the poor user and issue a helpful
			 * error message.  NOTE: this is not a security breach,
			 * because all the info reported here is known at the frontend
			 * and must be assumed known to bad guys. We're merely helping
			 * out the less clueful good guys.
			 */
			{
				const char *hostinfo = "localhost";
413 414 415 416 417
				char ip_hostinfo[INET6_ADDRSTRLEN];
				if (isAF_INETx(&port->raddr.sa) ){
				  hostinfo = SockAddr_ntop(&port->raddr, ip_hostinfo,
							   INET6_ADDRSTRLEN, 1);
				}
418 419

				elog(FATAL,
B
Bruce Momjian 已提交
420
				"No pg_hba.conf entry for host %s, user %s, database %s",
421 422 423
					 hostinfo, port->user, port->database);
				break;
			}
424

425 426 427 428
		case uaKrb4:
			sendAuthRequest(port, AUTH_REQ_KRB4);
			status = pg_krb4_recvauth(port);
			break;
429

430 431 432 433
		case uaKrb5:
			sendAuthRequest(port, AUTH_REQ_KRB5);
			status = pg_krb5_recvauth(port);
			break;
434

435
		case uaIdent:
436 437 438
#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || \
	(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS)) && \
	!defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED)
439

440
			/*
441 442
			 * If we are doing ident on unix-domain sockets, use SCM_CREDS
			 * only if it is defined and SO_PEERCRED isn't.
443
			 */
444
#if defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
445

446
			/*
447 448 449
			 * Receive credentials on next message receipt, BSD/OS,
			 * NetBSD. We need to set this before the client sends the
			 * next packet.
450
			 */
451
			{
452 453
				int			on = 1;

454
				if (setsockopt(port->sock, 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
455
					elog(FATAL, "pg_local_sendauth: can't do setsockopt: %m");
456 457
			}
#endif
458
			if (port->raddr.sa.sa_family == AF_UNIX)
459 460
				sendAuthRequest(port, AUTH_REQ_SCM_CREDS);
#endif
461
			status = authident(port);
462
			break;
463

464 465
		case uaMD5:
			sendAuthRequest(port, AUTH_REQ_MD5);
466
			status = recv_and_check_password_packet(port);
467 468
			break;

469 470 471
		case uaCrypt:
			sendAuthRequest(port, AUTH_REQ_CRYPT);
			status = recv_and_check_password_packet(port);
472 473
			break;

474 475 476 477 478
		case uaPassword:
			sendAuthRequest(port, AUTH_REQ_PASSWORD);
			status = recv_and_check_password_packet(port);
			break;

B
Bruce Momjian 已提交
479 480 481 482
#ifdef USE_PAM
		case uaPAM:
			pam_port_cludge = port;
			status = CheckPAMAuth(port, port->user, "");
483
			break;
484
#endif   /* USE_PAM */
485

486 487 488
		case uaTrust:
			status = STATUS_OK;
			break;
489
	}
490 491 492 493

	if (status == STATUS_OK)
		sendAuthRequest(port, AUTH_REQ_OK);
	else
494
		auth_failed(port, status);
495
}
496

497 498

/*
499
 * Send an authentication request packet to the frontend.
500
 */
501
static void
502
sendAuthRequest(Port *port, AuthRequest areq)
503
{
504
	StringInfoData buf;
505

506 507 508
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'R');
	pq_sendint(&buf, (int32) areq, sizeof(int32));
509 510

	/* Add the salt for encrypted passwords. */
B
Bruce Momjian 已提交
511
	if (areq == AUTH_REQ_MD5)
512 513 514
		pq_sendbytes(&buf, port->md5Salt, 4);
	else if (areq == AUTH_REQ_CRYPT)
		pq_sendbytes(&buf, port->cryptSalt, 2);
515

516
	pq_endmessage(&buf);
517 518

	/*
B
Bruce Momjian 已提交
519 520
	 * Flush message so client will see it, except for AUTH_REQ_OK, which
	 * need not be sent until we are ready for queries.
521 522 523
	 */
	if (areq != AUTH_REQ_OK)
		pq_flush();
524 525
}

526

B
Bruce Momjian 已提交
527 528 529 530 531 532 533
#ifdef USE_PAM

/*
 * PAM conversation function
 */

static int
534
pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg, struct pam_response ** resp, void *appdata_ptr)
B
Bruce Momjian 已提交
535 536
{
	StringInfoData buf;
537
	int32		len;
B
Bruce Momjian 已提交
538

539 540 541 542
	if (num_msg != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF)
	{
		switch (msg[0]->msg_style)
		{
B
Bruce Momjian 已提交
543
			case PAM_ERROR_MSG:
544 545
				elog(LOG, "pam_passwd_conv_proc: Error from underlying PAM layer: '%s'",
					 msg[0]->msg);
B
Bruce Momjian 已提交
546 547
				return PAM_CONV_ERR;
			default:
548 549
				elog(LOG, "pam_passwd_conv_proc: Unexpected PAM conversation %d/'%s'",
					 msg[0]->msg_style, msg[0]->msg);
B
Bruce Momjian 已提交
550 551 552 553
				return PAM_CONV_ERR;
		}
	}

554 555 556 557 558
	if (!appdata_ptr)
	{
		/*
		 * Workaround for Solaris 2.6 where the PAM library is broken and
		 * does not pass appdata_ptr to the conversation routine
B
Bruce Momjian 已提交
559 560 561 562
		 */
		appdata_ptr = pam_passwd;
	}

563 564 565
	/*
	 * Password wasn't passed to PAM the first time around - let's go ask
	 * the client to send a password, which we then stuff into PAM.
B
Bruce Momjian 已提交
566
	 */
567 568
	if (strlen(appdata_ptr) == 0)
	{
B
Bruce Momjian 已提交
569
		sendAuthRequest(pam_port_cludge, AUTH_REQ_PASSWORD);
570
		if (pq_eof() == EOF || pq_getint(&len, 4) == EOF)
571
			return PAM_CONV_ERR;	/* client didn't want to send password */
B
Bruce Momjian 已提交
572

573
		initStringInfo(&buf);
574 575
		if (pq_getstr_bounded(&buf, 1000) == EOF)
			return PAM_CONV_ERR;	/* EOF while reading password */
B
Bruce Momjian 已提交
576

577 578
		/* Do not echo failed password to logs, for security. */
		elog(DEBUG5, "received PAM packet");
B
Bruce Momjian 已提交
579

580 581
		if (strlen(buf.data) == 0)
		{
582
			elog(LOG, "pam_passwd_conv_proc: no password");
B
Bruce Momjian 已提交
583 584 585 586 587
			return PAM_CONV_ERR;
		}
		appdata_ptr = buf.data;
	}

588 589
	/*
	 * Explicitly not using palloc here - PAM will free this memory in
B
Bruce Momjian 已提交
590 591 592
	 * pam_end()
	 */
	*resp = calloc(num_msg, sizeof(struct pam_response));
593 594
	if (!*resp)
	{
595
		elog(LOG, "pam_passwd_conv_proc: Out of memory!");
596
		if (buf.data)
B
Bruce Momjian 已提交
597
			pfree(buf.data);
598
		return PAM_CONV_ERR;
B
Bruce Momjian 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	}

	(*resp)[0].resp = strdup((char *) appdata_ptr);
	(*resp)[0].resp_retcode = 0;

	return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
}


/*
 * Check authentication against PAM.
 */
static int
CheckPAMAuth(Port *port, char *user, char *password)
{
614
	int			retval;
B
Bruce Momjian 已提交
615 616 617
	pam_handle_t *pamh = NULL;

	/*
618 619
	 * Apparently, Solaris 2.6 is broken, and needs ugly static variable
	 * workaround
B
Bruce Momjian 已提交
620 621 622
	 */
	pam_passwd = password;

623 624 625 626
	/*
	 * Set the application data portion of the conversation struct This is
	 * later used inside the PAM conversation to pass the password to the
	 * authentication module.
B
Bruce Momjian 已提交
627
	 */
628 629
	pam_passw_conv.appdata_ptr = (char *) password;		/* from password above,
														 * not allocated */
B
Bruce Momjian 已提交
630 631

	/* Optionally, one can set the service name in pg_hba.conf */
632
	if (port->auth_arg[0] == '\0')
B
Bruce Momjian 已提交
633
		retval = pam_start(PGSQL_PAM_SERVICE, "pgsql@", &pam_passw_conv, &pamh);
634
	else
B
Bruce Momjian 已提交
635 636
		retval = pam_start(port->auth_arg, "pgsql@", &pam_passw_conv, &pamh);

637 638
	if (retval != PAM_SUCCESS)
	{
639 640
		elog(LOG, "CheckPAMAuth: Failed to create PAM authenticator: '%s'",
			 pam_strerror(pamh, retval));
641
		pam_passwd = NULL;		/* Unset pam_passwd */
B
Bruce Momjian 已提交
642 643 644
		return STATUS_ERROR;
	}

645 646 647
	retval = pam_set_item(pamh, PAM_USER, user);

	if (retval != PAM_SUCCESS)
648
	{
649 650
		elog(LOG, "CheckPAMAuth: pam_set_item(PAM_USER) failed: '%s'",
			 pam_strerror(pamh, retval));
651
		pam_passwd = NULL;		/* Unset pam_passwd */
B
Bruce Momjian 已提交
652 653
		return STATUS_ERROR;
	}
654 655 656 657

	retval = pam_set_item(pamh, PAM_CONV, &pam_passw_conv);

	if (retval != PAM_SUCCESS)
658
	{
659 660
		elog(LOG, "CheckPAMAuth: pam_set_item(PAM_CONV) failed: '%s'",
			 pam_strerror(pamh, retval));
661
		pam_passwd = NULL;		/* Unset pam_passwd */
B
Bruce Momjian 已提交
662 663
		return STATUS_ERROR;
	}
664 665 666 667

	retval = pam_authenticate(pamh, 0);

	if (retval != PAM_SUCCESS)
668
	{
669 670
		elog(LOG, "CheckPAMAuth: pam_authenticate failed: '%s'",
			 pam_strerror(pamh, retval));
671
		pam_passwd = NULL;		/* Unset pam_passwd */
B
Bruce Momjian 已提交
672 673
		return STATUS_ERROR;
	}
674 675 676 677

	retval = pam_acct_mgmt(pamh, 0);

	if (retval != PAM_SUCCESS)
678
	{
679 680
		elog(LOG, "CheckPAMAuth: pam_acct_mgmt failed: '%s'",
			 pam_strerror(pamh, retval));
681
		pam_passwd = NULL;		/* Unset pam_passwd */
B
Bruce Momjian 已提交
682 683 684
		return STATUS_ERROR;
	}

685
	retval = pam_end(pamh, retval);
B
Bruce Momjian 已提交
686

687 688
	if (retval != PAM_SUCCESS)
	{
689 690
		elog(LOG, "CheckPAMAuth: Failed to release PAM authenticator: '%s'",
			 pam_strerror(pamh, retval));
B
Bruce Momjian 已提交
691
	}
692

B
Bruce Momjian 已提交
693
	pam_passwd = NULL;			/* Unset pam_passwd */
694 695

	return (retval == PAM_SUCCESS ? STATUS_OK : STATUS_ERROR);
B
Bruce Momjian 已提交
696
}
697
#endif   /* USE_PAM */
698

699

700 701 702
/*
 * Called when we have received the password packet.
 */
M
 
Marc G. Fournier 已提交
703
static int
704
recv_and_check_password_packet(Port *port)
705
{
706 707 708 709
	StringInfoData buf;
	int32		len;
	int			result;

710
	if (pq_eof() == EOF || pq_getint(&len, 4) == EOF)
711 712
		return STATUS_EOF;		/* client didn't want to send password */

713
	initStringInfo(&buf);
714
	if (pq_getstr_bounded(&buf, 1000) == EOF) /* receive password */
715 716 717 718
	{
		pfree(buf.data);
		return STATUS_EOF;
	}
B
Bruce Momjian 已提交
719

720
	/*
B
Bruce Momjian 已提交
721 722 723
	 * We don't actually use the password packet length the frontend sent
	 * us; however, it's a reasonable sanity check to ensure that we
	 * actually read as much data as we expected to.
724
	 *
B
Bruce Momjian 已提交
725 726
	 * The password packet size is the length of the buffer, plus the size
	 * field itself (4 bytes), plus a 1-byte terminator.
727 728 729 730
	 */
	if (len != (buf.len + 4 + 1))
		elog(LOG, "unexpected password packet size: read %d, expected %d",
			 buf.len + 4 + 1, len);
731

732
	/* Do not echo password to logs, for security. */
733
	elog(DEBUG5, "received password packet");
734

B
Bruce Momjian 已提交
735 736
	result = md5_crypt_verify(port, port->user, buf.data);

737 738
	pfree(buf.data);
	return result;
739
}