auth.c 16.6 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * auth.c--
4
 *	  Routines to handle network authentication
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.31 1998/09/01 04:28:44 momjian Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
 *
17 18
 *	   backend (postmaster) routines:
 *		be_recvauth				receive authentication information
19 20 21
 */
#include <stdio.h>
#include <string.h>
22
#include <sys/param.h>			/* for MAXHOSTNAMELEN on most */
23
#ifndef  MAXHOSTNAMELEN
24
#include <netdb.h>				/* for MAXHOSTNAMELEN on some */
25
#endif
26
#include <pwd.h>
27
#include <ctype.h>				/* isspace() declaration */
28

29
#include <sys/types.h>			/* needed by in.h on Ultrix */
30 31
#include <netinet/in.h>
#include <arpa/inet.h>
M
Marc G. Fournier 已提交
32

M
Marc G. Fournier 已提交
33 34
#include <postgres.h>
#include <miscadmin.h>
M
Marc G. Fournier 已提交
35

M
Marc G. Fournier 已提交
36 37 38
#include <libpq/auth.h>
#include <libpq/libpq.h>
#include <libpq/hba.h>
39
#include <libpq/password.h>
40
#include <libpq/crypt.h>
41 42


M
 
Marc G. Fournier 已提交
43 44 45 46 47 48 49
static void sendAuthRequest(Port *port, AuthRequest areq, PacketDoneProc handler);
static int	handle_done_auth(void *arg, PacketLen len, void *pkt);
static int	handle_krb4_auth(void *arg, PacketLen len, void *pkt);
static int	handle_krb5_auth(void *arg, PacketLen len, void *pkt);
static int	handle_password_auth(void *arg, PacketLen len, void *pkt);
static int	readPasswordPacket(void *arg, PacketLen len, void *pkt);
static int	pg_passwordv0_recvauth(void *arg, PacketLen len, void *pkt);
50 51 52
static int	checkPassword(Port *port, char *user, char *password);
static int	old_be_recvauth(Port *port);
static int	map_old_to_new(Port *port, UserAuth old, int status);
53 54 55


#ifdef KRB4
56 57 58
/* This has to be ifdef'd out because krb.h does exist.  This needs
   to be fixed.
*/
59 60 61 62 63
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 4
 *----------------------------------------------------------------
 */

M
Marc G. Fournier 已提交
64
#include <krb.h>
65 66 67

/*
 * pg_krb4_recvauth -- server routine to receive authentication information
68
 *					   from the client
69 70 71 72 73 74 75
 *
 * 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 已提交
76
pg_krb4_recvauth(Port *port)
77
{
78 79 80 81 82 83 84
	long		krbopts = 0;	/* one-way authentication */
	KTEXT_ST	clttkt;
	char		instance[INST_SZ];
	AUTH_DAT	auth_data;
	Key_schedule key_sched;
	char		version[KRB_SENDAUTH_VLEN];
	int			status;
85 86 87 88

	strcpy(instance, "*");		/* don't care, but arg gets expanded
								 * anyway */
	status = krb_recvauth(krbopts,
89
						  port->sock,
90 91 92
						  &clttkt,
						  PG_KRB_SRVNAM,
						  instance,
93 94
						  &port->raddr.in,
						  &port->laddr.in,
95 96 97 98 99 100 101 102 103 104 105
						  &auth_data,
						  PG_KRB_SRVTAB,
						  key_sched,
						  version);
	if (status != KSUCCESS)
	{
		sprintf(PQerrormsg,
				"pg_krb4_recvauth: kerberos error: %s\n",
				krb_err_txt[status]);
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
106
		return STATUS_ERROR;
107 108 109 110 111 112 113 114
	}
	if (strncmp(version, PG_KRB4_VERSION, KRB_SENDAUTH_VLEN))
	{
		sprintf(PQerrormsg,
				"pg_krb4_recvauth: protocol version != \"%s\"\n",
				PG_KRB4_VERSION);
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
115
		return STATUS_ERROR;
116
	}
117
	if (strncmp(port->user, auth_data.pname, SM_USER))
118 119 120
	{
		sprintf(PQerrormsg,
				"pg_krb4_recvauth: name \"%s\" != \"%s\"\n",
B
Bruce Momjian 已提交
121
				port->user,
122 123 124
				auth_data.pname);
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
125
		return STATUS_ERROR;
126
	}
127
	return STATUS_OK;
128 129
}

130 131
#else
static int
132
pg_krb4_recvauth(Port *port)
133
{
134 135 136 137 138
	sprintf(PQerrormsg,
			"pg_krb4_recvauth: Kerberos not implemented on this "
			"server.\n");
	fputs(PQerrormsg, stderr);
	pqdebug("%s", PQerrormsg);
139

140
	return STATUS_ERROR;
141
}
142

143
#endif	 /* KRB4 */
144

145

146
#ifdef KRB5
147 148 149
/* This needs to be ifdef'd out because krb5.h doesn't exist.  This needs
   to be fixed.
*/
150 151 152 153 154
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 5
 *----------------------------------------------------------------
 */

M
Marc G. Fournier 已提交
155
#include <krb5/krb5.h>
156 157 158

/*
 * pg_an_to_ln -- return the local name corresponding to an authentication
159
 *				  name
160 161
 *
 * XXX Assumes that the first aname component is the user name.  This is NOT
162 163 164 165 166 167 168
 *	   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.
169
 */
170
static char *
171 172
pg_an_to_ln(char *aname)
{
173
	char	   *p;
174 175 176

	if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
		*p = '\0';
177
	return aname;
178 179 180
}

/*
181
 * pg_krb5_recvauth -- server routine to receive authentication information
182
 *					   from the client
183 184
 *
 * We still need to compare the username obtained from the client's setup
185
 * packet to the authenticated name, as described in pg_krb4_recvauth.	This
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
 * is a bit more problematic in v5, as described above in pg_an_to_ln.
 *
 * In addition, as described above in pg_krb5_sendauth, we still need to
 * canonicalize the server name v4-style before constructing a principal
 * from it.  Again, this is kind of iffy.
 *
 * Finally, we need to tangle with the fact that v5 doesn't let you explicitly
 * set server keytab file names -- you have to feed lower-level routines a
 * function to retrieve the contents of a keytab, along with a single argument
 * that allows them to open the keytab.  We assume that a server keytab is
 * always a real file so we can allow people to specify their own filenames.
 * (This is important because the POSTGRES keytab needs to be readable by
 * non-root users/groups; the v4 tools used to force you do dump a whole
 * host's worth of keys into a file, effectively forcing you to use one file,
 * but kdb5_edit allows you to select which principals to dump.  Yay!)
 */
static int
203
pg_krb5_recvauth(Port *port)
204
{
205 206 207 208
	char		servbuf[MAXHOSTNAMELEN + 1 +
									sizeof(PG_KRB_SRVNAM)];
	char	   *hostp,
			   *kusername = (char *) NULL;
209
	krb5_error_code code;
210 211 212
	krb5_principal client,
				server;
	krb5_address sender_addr;
213
	krb5_rdreq_key_proc keyproc = (krb5_rdreq_key_proc) NULL;
214
	krb5_pointer keyprocarg = (krb5_pointer) NULL;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	/*
	 * Set up server side -- since we have no ticket file to make this
	 * easy, we construct our own name and parse it.  See note on
	 * canonicalization above.
	 */
	strcpy(servbuf, PG_KRB_SRVNAM);
	*(hostp = servbuf + (sizeof(PG_KRB_SRVNAM) - 1)) = '/';
	if (gethostname(++hostp, MAXHOSTNAMELEN) < 0)
		strcpy(hostp, "localhost");
	if (hostp = strchr(hostp, '.'))
		*hostp = '\0';
	if (code = krb5_parse_name(servbuf, &server))
	{
		sprintf(PQerrormsg,
			  "pg_krb5_recvauth: Kerberos error %d in krb5_parse_name\n",
				code);
		com_err("pg_krb5_recvauth", code, "in krb5_parse_name");
233
		return STATUS_ERROR;
234 235 236 237 238 239
	}

	/*
	 * krb5_sendauth needs this to verify the address in the client
	 * authenticator.
	 */
240 241 242
	sender_addr.addrtype = port->raddr.in.sin_family;
	sender_addr.length = sizeof(port->raddr.in.sin_addr);
	sender_addr.contents = (krb5_octet *) & (port->raddr.in.sin_addr);
243 244 245 246 247 248 249

	if (strcmp(PG_KRB_SRVTAB, ""))
	{
		keyproc = krb5_kt_read_service_key;
		keyprocarg = PG_KRB_SRVTAB;
	}

250
	if (code = krb5_recvauth((krb5_pointer) & port->sock,
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
							 PG_KRB5_VERSION,
							 server,
							 &sender_addr,
							 (krb5_pointer) NULL,
							 keyproc,
							 keyprocarg,
							 (char *) NULL,
							 (krb5_int32 *) NULL,
							 &client,
							 (krb5_ticket **) NULL,
							 (krb5_authenticator **) NULL))
	{
		sprintf(PQerrormsg,
				"pg_krb5_recvauth: Kerberos error %d in krb5_recvauth\n",
				code);
		com_err("pg_krb5_recvauth", code, "in krb5_recvauth");
		krb5_free_principal(server);
268
		return STATUS_ERROR;
269
	}
270
	krb5_free_principal(server);
271 272 273 274 275 276 277 278 279 280 281 282 283

	/*
	 * The "client" structure comes out of the ticket and is therefore
	 * authenticated.  Use it to check the username obtained from the
	 * postmaster startup packet.
	 */
	if ((code = krb5_unparse_name(client, &kusername)))
	{
		sprintf(PQerrormsg,
			"pg_krb5_recvauth: Kerberos error %d in krb5_unparse_name\n",
				code);
		com_err("pg_krb5_recvauth", code, "in krb5_unparse_name");
		krb5_free_principal(client);
284
		return STATUS_ERROR;
285
	}
286
	krb5_free_principal(client);
287 288 289 290 291 292
	if (!kusername)
	{
		sprintf(PQerrormsg,
				"pg_krb5_recvauth: could not decode username\n");
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
293
		return STATUS_ERROR;
294 295
	}
	kusername = pg_an_to_ln(kusername);
296
	if (strncmp(username, kusername, SM_USER))
297 298 299
	{
		sprintf(PQerrormsg,
				"pg_krb5_recvauth: name \"%s\" != \"%s\"\n",
B
Bruce Momjian 已提交
300
				port->user, kusername);
301 302
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
303
		pfree(kusername);
304
		return STATUS_ERROR;
305
	}
306
	pfree(kusername);
307
	return STATUS_OK;
308 309
}

310
#else
311
static int
312
pg_krb5_recvauth(Port *port)
313
{
314 315 316 317 318
	sprintf(PQerrormsg,
			"pg_krb5_recvauth: Kerberos not implemented on this "
			"server.\n");
	fputs(PQerrormsg, stderr);
	pqdebug("%s", PQerrormsg);
319

320
	return STATUS_ERROR;
321
}
322

323
#endif	 /* KRB5 */
324

325 326 327 328 329

/*
 * Handle a v0 password packet.
 */

M
 
Marc G. Fournier 已提交
330 331
static int
pg_passwordv0_recvauth(void *arg, PacketLen len, void *pkt)
332
{
333
	Port	   *port;
334
	PasswordPacketV0 *pp;
335 336 337 338
	char	   *user,
			   *password,
			   *cp,
			   *start;
339

340 341
	port = (Port *) arg;
	pp = (PasswordPacketV0 *) pkt;
342 343 344 345 346 347 348 349

	/*
	 * The packet is supposed to comprise the user name and the password
	 * as C strings.  Be careful the check that this is the case.
	 */

	user = password = NULL;

350
	len -= sizeof(pp->unused);
351 352 353

	cp = start = pp->data;

354 355
	while (len-- > 0)
		if (*cp++ == '\0')
356
		{
357 358 359 360 361 362 363
			if (user == NULL)
				user = start;
			else
			{
				password = start;
				break;
			}
364

365 366
			start = cp;
		}
367 368

	if (user == NULL || password == NULL)
369 370
	{
		sprintf(PQerrormsg,
371
				"pg_password_recvauth: badly formed password packet.\n");
372 373
		fputs(PQerrormsg, stderr);
		pqdebug("%s", PQerrormsg);
374 375

		auth_failed(port);
376
	}
377 378
	else
	{
379 380
		int			status;
		UserAuth	saved;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

		/* Check the password. */

		saved = port->auth_method;
		port->auth_method = uaPassword;

		status = checkPassword(port, user, password);

		port->auth_method = saved;

		/* Adjust the result if necessary. */

		if (map_old_to_new(port, uaPassword, status) != STATUS_OK)
			auth_failed(port);
	}
M
 
Marc G. Fournier 已提交
396

397
	return STATUS_OK;			/* don't close the connection yet */
398
}
399 400


401 402 403
/*
 * Tell the user the authentication failed, but not why.
 */
404

405 406
void
auth_failed(Port *port)
407
{
408
	PacketSendError(&port->pktInfo, "User authentication failed");
409 410
}

411

412 413 414
/*
 * be_recvauth -- server demux routine for incoming authentication information
 */
415 416
void
be_recvauth(Port *port)
417
{
418

419
	/*
420 421
	 * Get the authentication method to use for this frontend/database
	 * combination.
422
	 */
423

424
	if (hba_getauthmethod(&port->raddr, port->user, port->database,
425
						port->auth_arg, &port->auth_method) != STATUS_OK)
426
		PacketSendError(&port->pktInfo, "Missing or mis-configured pg_hba.conf file");
427

428
	else if (PG_PROTOCOL_MAJOR(port->proto) == 0)
429
	{
430 431
		/* Handle old style authentication. */

432 433
		if (old_be_recvauth(port) != STATUS_OK)
			auth_failed(port);
434
	}
435
	else
436
	{
437
		AuthRequest areq;
M
 
Marc G. Fournier 已提交
438
		PacketDoneProc auth_handler;
439

440
		/* Keep the compiler quiet. */
441 442 443

		areq = AUTH_REQ_OK;

444 445 446 447 448
		/* Handle new style authentication. */

		auth_handler = NULL;

		switch (port->auth_method)
449
		{
450 451
			case uaReject:
				break;
452

453 454 455 456
			case uaKrb4:
				areq = AUTH_REQ_KRB4;
				auth_handler = handle_krb4_auth;
				break;
457

458 459 460 461
			case uaKrb5:
				areq = AUTH_REQ_KRB5;
				auth_handler = handle_krb5_auth;
				break;
462

463
			case uaTrust:
464 465
				areq = AUTH_REQ_OK;
				auth_handler = handle_done_auth;
466
				break;
467

468 469 470 471 472 473 474
			case uaIdent:
				if (authident(&port->raddr.in, &port->laddr.in,
							  port->user, port->auth_arg) == STATUS_OK)
				{
					areq = AUTH_REQ_OK;
					auth_handler = handle_done_auth;
				}
475

476
				break;
477

478 479 480 481 482 483 484 485 486 487
			case uaPassword:
				areq = AUTH_REQ_PASSWORD;
				auth_handler = handle_password_auth;
				break;

			case uaCrypt:
				areq = AUTH_REQ_CRYPT;
				auth_handler = handle_password_auth;
				break;
		}
488 489 490 491 492 493 494 495

		/* Tell the frontend what we want next. */

		if (auth_handler != NULL)
			sendAuthRequest(port, areq, auth_handler);
		else
			auth_failed(port);
	}
496
}
497

498 499

/*
500
 * Send an authentication request packet to the frontend.
501
 */
502

503
static void
M
 
Marc G. Fournier 已提交
504
sendAuthRequest(Port *port, AuthRequest areq, PacketDoneProc handler)
505
{
506 507 508 509
	char	   *dp,
			   *sp;
	int			i;
	uint32		net_areq;
510

511 512 513 514 515
	/* Convert to a byte stream. */

	net_areq = htonl(areq);

	dp = port->pktInfo.pkt.ar.data;
516
	sp = (char *) &net_areq;
517 518 519 520 521 522 523 524 525

	*dp++ = 'R';

	for (i = 1; i <= 4; ++i)
		*dp++ = *sp++;

	/* Add the salt for encrypted passwords. */

	if (areq == AUTH_REQ_CRYPT)
526
	{
527 528 529
		*dp++ = port->salt[0];
		*dp++ = port->salt[1];
		i += 2;
530
	}
531

M
 
Marc G. Fournier 已提交
532
	PacketSendSetup(&port->pktInfo, i, handler, (void *) port);
533 534 535 536 537 538 539
}


/*
 * Called when we have told the front end that it is authorised.
 */

M
 
Marc G. Fournier 已提交
540 541
static int
handle_done_auth(void *arg, PacketLen len, void *pkt)
542
{
543

544 545 546 547 548
	/*
	 * Don't generate any more traffic.  This will cause the backend to
	 * start.
	 */

M
 
Marc G. Fournier 已提交
549
	return STATUS_OK;
550 551 552 553 554 555 556 557
}


/*
 * Called when we have told the front end that it should use Kerberos V4
 * authentication.
 */

M
 
Marc G. Fournier 已提交
558 559
static int
handle_krb4_auth(void *arg, PacketLen len, void *pkt)
560
{
M
 
Marc G. Fournier 已提交
561 562
	Port	   *port = (Port *) arg;

563 564 565 566
	if (pg_krb4_recvauth(port) != STATUS_OK)
		auth_failed(port);
	else
		sendAuthRequest(port, AUTH_REQ_OK, handle_done_auth);
M
 
Marc G. Fournier 已提交
567 568

	return STATUS_OK;
569 570 571 572 573 574 575 576
}


/*
 * Called when we have told the front end that it should use Kerberos V5
 * authentication.
 */

M
 
Marc G. Fournier 已提交
577 578
static int
handle_krb5_auth(void *arg, PacketLen len, void *pkt)
579
{
M
 
Marc G. Fournier 已提交
580 581
	Port	   *port = (Port *) arg;

582 583 584 585
	if (pg_krb5_recvauth(port) != STATUS_OK)
		auth_failed(port);
	else
		sendAuthRequest(port, AUTH_REQ_OK, handle_done_auth);
M
 
Marc G. Fournier 已提交
586 587

	return STATUS_OK;
588 589 590 591 592 593 594 595
}


/*
 * Called when we have told the front end that it should use password
 * authentication.
 */

M
 
Marc G. Fournier 已提交
596 597
static int
handle_password_auth(void *arg, PacketLen len, void *pkt)
598
{
M
 
Marc G. Fournier 已提交
599 600
	Port	   *port = (Port *) arg;

601 602
	/* Set up the read of the password packet. */

M
 
Marc G. Fournier 已提交
603 604 605
	PacketReceiveSetup(&port->pktInfo, readPasswordPacket, (void *) port);

	return STATUS_OK;
606 607 608 609 610 611 612
}


/*
 * Called when we have received the password packet.
 */

M
 
Marc G. Fournier 已提交
613 614
static int
readPasswordPacket(void *arg, PacketLen len, void *pkt)
615
{
616
	char		password[sizeof(PasswordPacket) + 1];
M
 
Marc G. Fournier 已提交
617
	Port	   *port = (Port *) arg;
618 619 620

	/* Silently truncate a password that is too big. */

621 622 623 624
	if (len > sizeof(PasswordPacket))
		len = sizeof(PasswordPacket);

	StrNCpy(password, ((PasswordPacket *) pkt)->passwd, len);
625

626
	if (checkPassword(port, port->user, password) != STATUS_OK)
627 628 629
		auth_failed(port);
	else
		sendAuthRequest(port, AUTH_REQ_OK, handle_done_auth);
M
 
Marc G. Fournier 已提交
630

631
	return STATUS_OK;			/* don't close the connection yet */
632 633
}

634

635 636
/*
 * Use the local flat password file if clear passwords are used and the file is
637
 * specified.  Otherwise use the password in the pg_shadow table, encrypted or
638 639 640
 * not.
 */

641 642
static int
checkPassword(Port *port, char *user, char *password)
643 644 645 646 647 648 649 650
{
	if (port->auth_method == uaPassword && port->auth_arg[0] != '\0')
		return verify_password(port->auth_arg, user, password);

	return crypt_verify(port, user, password);
}


651 652 653 654
/*
 * Server demux routine for incoming authentication information for protocol
 * version 0.
 */
655 656
static int
old_be_recvauth(Port *port)
657
{
658 659
	int			status;
	MsgType		msgtype = (MsgType) port->proto;
660 661 662 663

	/* Handle the authentication that's offered. */

	switch (msgtype)
664 665 666 667
	{
		case STARTUP_KRB4_MSG:
			status = map_old_to_new(port, uaKrb4, pg_krb4_recvauth(port));
			break;
668

669 670 671
		case STARTUP_KRB5_MSG:
			status = map_old_to_new(port, uaKrb5, pg_krb5_recvauth(port));
			break;
672

673 674 675
		case STARTUP_MSG:
			status = map_old_to_new(port, uaTrust, STATUS_OK);
			break;
676

677 678
		case STARTUP_PASSWORD_MSG:
			PacketReceiveSetup(&port->pktInfo, pg_passwordv0_recvauth,
M
 
Marc G. Fournier 已提交
679
							   (void *) port);
680

681
			return STATUS_OK;
682

683 684
		default:
			fprintf(stderr, "Invalid startup message type: %u\n", msgtype);
685

686 687
			return STATUS_OK;
	}
688 689 690

	return status;
}
691

692 693

/*
694
 * The old style authentication has been done.	Modify the result of this (eg.
695 696 697 698
 * allow the connection anyway, disallow it anyway, or use the result)
 * depending on what authentication we really want to use.
 */

699 700
static int
map_old_to_new(Port *port, UserAuth old, int status)
701 702 703
{
	switch (port->auth_method)
	{
704 705
			case uaCrypt:
			case uaReject:
706
			status = STATUS_ERROR;
707
			break;
708

709 710 711 712
		case uaKrb4:
			if (old != uaKrb4)
				status = STATUS_ERROR;
			break;
713

714 715 716 717
		case uaKrb5:
			if (old != uaKrb5)
				status = STATUS_ERROR;
			break;
718

719 720 721
		case uaTrust:
			status = STATUS_OK;
			break;
722

723 724 725 726 727 728 729 730
		case uaIdent:
			status = authident(&port->raddr.in, &port->laddr.in,
							   port->user, port->auth_arg);
			break;

		case uaPassword:
			if (old != uaPassword)
				status = STATUS_ERROR;
731

732
			break;
733
	}
734

735
	return status;
736
}