fe-auth.c 15.6 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * fe-auth.c--
4
 *	   The front-end (client) authorization routines
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.21 1998/08/09 02:59:25 momjian Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */

/*
 * INTERFACE ROUTINES
17 18 19 20 21 22
 *	   frontend (client) routines:
 *		fe_sendauth				send authentication information
 *		fe_getauthname			get user's name according to the client side
 *								of the authentication system
 *		fe_setauthsvc			set frontend authentication service
 *		fe_getauthsvc			get current frontend authentication service
23 24 25 26
 *
 *
 *
 */
Bruce Momjian's avatar
Bruce Momjian committed
27 28 29
#ifdef WIN32
#include "win32.h"
#else
30 31
#include <stdio.h>
#include <string.h>
32
#include <sys/param.h>			/* for MAXHOSTNAMELEN on most */
33
#include <sys/types.h>
34
#ifndef  MAXHOSTNAMELEN
35
#include <netdb.h>				/* for MAXHOSTNAMELEN on some */
36
#endif
37 38
#include <unistd.h>
#include <pwd.h>
Bruce Momjian's avatar
Bruce Momjian committed
39
#endif /* WIN32 */
40 41 42

#include "postgres.h"

43 44 45 46
#include "libpq/pqcomm.h"

#include "libpq-fe.h"
#include "fe-auth.h"
47
#include "fe-connect.h"
48

49 50 51 52 53
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif


54 55 56 57 58
/*----------------------------------------------------------------
 * common definitions for generic fe/be routines
 *----------------------------------------------------------------
 */

59 60
struct authsvc
{
61
	char		name[NAMEDATALEN];	/* service nickname (for command line) */
62 63
	MsgType		msgtype;		/* startup packet header type */
	int			allowed;		/* initially allowed (before command line
64
								 * option parsing)? */
65 66 67 68 69 70 71 72 73 74 75 76 77 78
};

/*
 * Command-line parsing routines use this structure to map nicknames
 * onto service types (and the startup packets to use with them).
 *
 * Programs receiving an authentication request use this structure to
 * decide which authentication service types are currently permitted.
 * By default, all authentication systems compiled into the system are
 * allowed.  Unauthenticated connections are disallowed unless there
 * isn't any authentication system.
 */
static struct authsvc authsvcs[] = {
#ifdef KRB4
79 80 81
	{"krb4", STARTUP_KRB4_MSG, 1},
	{"kerberos", STARTUP_KRB4_MSG, 1},
#endif							/* KRB4 */
82
#ifdef KRB5
83 84 85 86
	{"krb5", STARTUP_KRB5_MSG, 1},
	{"kerberos", STARTUP_KRB5_MSG, 1},
#endif							/* KRB5 */
	{UNAUTHNAME, STARTUP_MSG,
87
#if defined(KRB4) || defined(KRB5)
88 89 90 91 92 93
		0
#else							/* !(KRB4 || KRB5) */
		1
#endif							/* !(KRB4 || KRB5) */
	},
	{"password", STARTUP_PASSWORD_MSG, 0}
94 95
};

96
static n_authsvcs = sizeof(authsvcs) / sizeof(struct authsvc);
97 98 99 100 101 102 103 104 105 106

#ifdef KRB4
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 4
 *----------------------------------------------------------------
 */

#include "krb.h"

/* for some reason, this is not defined in krb.h ... */
107
extern char *tkt_string(void);
108

109 110 111 112 113 114 115
/*
 * pg_krb4_init -- initialization performed before any Kerberos calls are made
 *
 * For v4, all we need to do is make sure the library routines get the right
 * ticket file if we want them to see a special one.  (They will open the file
 * themselves.)
 */
116 117
static void
pg_krb4_init()
118
{
119 120
	char	   *realm;
	static		init_done = 0;
121 122 123 124 125 126 127 128 129 130 131

	if (init_done)
		return;
	init_done = 1;

	/*
	 * If the user set PGREALM, then we use a ticket file with a special
	 * name: <usual-ticket-file-name>@<PGREALM-value>
	 */
	if (realm = getenv("PGREALM"))
	{
132
		char		tktbuf[MAXPATHLEN];
133 134 135 136

		(void) sprintf(tktbuf, "%s@%s", tkt_string(), realm);
		krb_set_tkt_string(tktbuf);
	}
137 138 139 140
}

/*
 * pg_krb4_authname -- returns a pointer to static space containing whatever
141
 *					   name the user has authenticated to the system
142 143 144
 *
 * We obtain this information by digging around in the ticket file.
 */
145
static char *
146
pg_krb4_authname(char *PQerrormsg)
147
{
148 149 150 151
	char		instance[INST_SZ];
	char		realm[REALM_SZ];
	int			status;
	static char name[SNAME_SZ + 1] = "";
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

	if (name[0])
		return (name);

	pg_krb4_init();

	name[SNAME_SZ] = '\0';
	status = krb_get_tf_fullname(tkt_string(), name, instance, realm);
	if (status != KSUCCESS)
	{
		(void) sprintf(PQerrormsg,
					   "pg_krb4_authname: krb_get_tf_fullname: %s\n",
					   krb_err_txt[status]);
		return ((char *) NULL);
	}
	return (name);
168 169 170 171
}

/*
 * pg_krb4_sendauth -- client routine to send authentication information to
172
 *					   the server
173 174 175 176 177 178 179 180 181 182 183 184
 *
 * This routine does not do mutual authentication, nor does it return enough
 * information to do encrypted connections.  But then, if we want to do
 * encrypted connections, we'll have to redesign the whole RPC mechanism
 * anyway.
 *
 * If the user is too lazy to feed us a hostname, we try to come up with
 * something other than "localhost" since the hostname is used as an
 * instance and instance names in v4 databases are usually actual hostnames
 * (canonicalized to omit all domain suffixes).
 */
static int
185 186 187 188
pg_krb4_sendauth(const char *PQerrormsg, int sock,
				 struct sockaddr_in * laddr,
				 struct sockaddr_in * raddr,
				 const char *hostname)
189
{
190 191 192 193 194
	long		krbopts = 0;	/* one-way authentication */
	KTEXT_ST	clttkt;
	int			status;
	char		hostbuf[MAXHOSTNAMELEN];
	const char *realm = getenv("PGREALM");		/* NULL == current realm */
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

	if (!hostname || !(*hostname))
	{
		if (gethostname(hostbuf, MAXHOSTNAMELEN) < 0)
			strcpy(hostbuf, "localhost");
		hostname = hostbuf;
	}

	pg_krb4_init();

	status = krb_sendauth(krbopts,
						  sock,
						  &clttkt,
						  PG_KRB_SRVNAM,
						  hostname,
						  realm,
						  (u_long) 0,
						  (MSG_DAT *) NULL,
						  (CREDENTIALS *) NULL,
						  (Key_schedule *) NULL,
						  laddr,
						  raddr,
						  PG_KRB4_VERSION);
	if (status != KSUCCESS)
	{
		(void) sprintf(PQerrormsg,
					   "pg_krb4_sendauth: kerberos error: %s\n",
					   krb_err_txt[status]);
		return (STATUS_ERROR);
	}
	return (STATUS_OK);
226 227
}

228
#endif							/* KRB4 */
229 230 231 232 233 234 235 236 237 238 239

#ifdef KRB5
/*----------------------------------------------------------------
 * MIT Kerberos authentication system - protocol version 5
 *----------------------------------------------------------------
 */

#include "krb5/krb5.h"

/*
 * pg_an_to_ln -- return the local name corresponding to an authentication
240
 *				  name
241 242
 *
 * XXX Assumes that the first aname component is the user name.  This is NOT
243 244 245 246 247 248 249
 *	   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.
250
 */
251
static char *
Marc G. Fournier's avatar
Marc G. Fournier committed
252
pg_an_to_ln(const char *aname)
253
{
254
	char	   *p;
255 256 257 258

	if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
		*p = '\0';
	return (aname);
259 260 261 262 263 264 265 266 267
}


/*
 * pg_krb5_init -- initialization performed before any Kerberos calls are made
 *
 * With v5, we can no longer set the ticket (credential cache) file name;
 * we now have to provide a file handle for the open (well, "resolved")
 * ticket file everywhere.
268
 *
269 270
 */
static int
271
			krb5_ccache
272
pg_krb5_init(void)
273
{
274
	krb5_error_code code;
275 276 277
	char	   *realm,
			   *defname;
	char		tktbuf[MAXPATHLEN];
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	static krb5_ccache ccache = (krb5_ccache) NULL;

	if (ccache)
		return (ccache);

	/*
	 * If the user set PGREALM, then we use a ticket file with a special
	 * name: <usual-ticket-file-name>@<PGREALM-value>
	 */
	if (!(defname = krb5_cc_default_name()))
	{
		(void) sprintf(PQerrormsg,
					   "pg_krb5_init: krb5_cc_default_name failed\n");
		return ((krb5_ccache) NULL);
	}
	strcpy(tktbuf, defname);
	if (realm = getenv("PGREALM"))
	{
		strcat(tktbuf, "@");
		strcat(tktbuf, realm);
	}

	if (code = krb5_cc_resolve(tktbuf, &ccache))
	{
		(void) sprintf(PQerrormsg,
				  "pg_krb5_init: Kerberos error %d in krb5_cc_resolve\n",
					   code);
		com_err("pg_krb5_init", code, "in krb5_cc_resolve");
		return ((krb5_ccache) NULL);
	}
	return (ccache);
309 310 311 312
}

/*
 * pg_krb5_authname -- returns a pointer to static space containing whatever
313
 *					   name the user has authenticated to the system
314 315 316
 *
 * We obtain this information by digging around in the ticket file.
 */
Marc G. Fournier's avatar
Marc G. Fournier committed
317
static const char *
318
pg_krb5_authname(const char *PQerrormsg)
319
{
320 321
	krb5_ccache ccache;
	krb5_principal principal;
322
	krb5_error_code code;
323
	static char *authname = (char *) NULL;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

	if (authname)
		return (authname);

	ccache = pg_krb5_init();	/* don't free this */

	if (code = krb5_cc_get_principal(ccache, &principal))
	{
		(void) sprintf(PQerrormsg,
		"pg_krb5_authname: Kerberos error %d in krb5_cc_get_principal\n",
					   code);
		com_err("pg_krb5_authname", code, "in krb5_cc_get_principal");
		return ((char *) NULL);
	}
	if (code = krb5_unparse_name(principal, &authname))
	{
		(void) sprintf(PQerrormsg,
			"pg_krb5_authname: Kerberos error %d in krb5_unparse_name\n",
					   code);
		com_err("pg_krb5_authname", code, "in krb5_unparse_name");
		krb5_free_principal(principal);
		return ((char *) NULL);
	}
347
	krb5_free_principal(principal);
348
	return (pg_an_to_ln(authname));
349 350 351 352
}

/*
 * pg_krb5_sendauth -- client routine to send authentication information to
353
 *					   the server
354 355 356 357 358 359 360
 *
 * This routine does not do mutual authentication, nor does it return enough
 * information to do encrypted connections.  But then, if we want to do
 * encrypted connections, we'll have to redesign the whole RPC mechanism
 * anyway.
 *
 * Server hostnames are canonicalized v4-style, i.e., all domain suffixes
361
 * are simply chopped off.	Hence, we are assuming that you've entered your
362
 * server instances as
363 364
 *		<value-of-PG_KRB_SRVNAM>/<canonicalized-hostname>
 * in the PGREALM (or local) database.	This is probably a bad assumption.
365 366
 */
static int
367 368 369 370
pg_krb5_sendauth(const char *PQerrormsg, int sock,
				 struct sockaddr_in * laddr,
				 struct sockaddr_in * raddr,
				 const char *hostname)
371
{
372 373 374 375
	char		servbuf[MAXHOSTNAMELEN + 1 +
									sizeof(PG_KRB_SRVNAM)];
	const char *hostp;
	const char *realm;
376
	krb5_error_code code;
377 378 379 380
	krb5_principal client,
				server;
	krb5_ccache ccache;
	krb5_error *error = (krb5_error *) NULL;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

	ccache = pg_krb5_init();	/* don't free this */

	/*
	 * set up client -- this is easy, we can get it out of the ticket
	 * file.
	 */
	if (code = krb5_cc_get_principal(ccache, &client))
	{
		(void) sprintf(PQerrormsg,
		"pg_krb5_sendauth: Kerberos error %d in krb5_cc_get_principal\n",
					   code);
		com_err("pg_krb5_sendauth", code, "in krb5_cc_get_principal");
		return (STATUS_ERROR);
	}

	/*
	 * set up server -- canonicalize as described above
	 */
	strcpy(servbuf, PG_KRB_SRVNAM);
	*(hostp = servbuf + (sizeof(PG_KRB_SRVNAM) - 1)) = '/';
	if (hostname || *hostname)
		strncpy(++hostp, hostname, MAXHOSTNAMELEN);
	else
	{
		if (gethostname(++hostp, MAXHOSTNAMELEN) < 0)
			strcpy(hostp, "localhost");
	}
	if (hostp = strchr(hostp, '.'))
		*hostp = '\0';
	if (realm = getenv("PGREALM"))
	{
		strcat(servbuf, "@");
		strcat(servbuf, realm);
	}
	if (code = krb5_parse_name(servbuf, &server))
	{
		(void) sprintf(PQerrormsg,
			  "pg_krb5_sendauth: Kerberos error %d in krb5_parse_name\n",
					   code);
		com_err("pg_krb5_sendauth", code, "in krb5_parse_name");
		krb5_free_principal(client);
		return (STATUS_ERROR);
	}

	/*
	 * The only thing we want back from krb5_sendauth is an error status
	 * and any error messages.
	 */
	if (code = krb5_sendauth((krb5_pointer) & sock,
							 PG_KRB5_VERSION,
							 client,
							 server,
							 (krb5_flags) 0,
							 (krb5_checksum *) NULL,
							 (krb5_creds *) NULL,
							 ccache,
							 (krb5_int32 *) NULL,
							 (krb5_keyblock **) NULL,
							 &error,
							 (krb5_ap_rep_enc_part **) NULL))
	{
		if ((code == KRB5_SENDAUTH_REJECTED) && error)
		{
			(void) sprintf(PQerrormsg,
				  "pg_krb5_sendauth: authentication rejected: \"%*s\"\n",
						   error->text.length, error->text.data);
		}
		else
		{
			(void) sprintf(PQerrormsg,
				"pg_krb5_sendauth: Kerberos error %d in krb5_sendauth\n",
						   code);
			com_err("pg_krb5_sendauth", code, "in krb5_sendauth");
		}
456
	}
457 458 459
	krb5_free_principal(client);
	krb5_free_principal(server);
	return (code ? STATUS_ERROR : STATUS_OK);
460 461
}

462
#endif							/* KRB5 */
463

464
static int
465
pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
466
{
467
	/* Encrypt the password if needed. */
468

469 470
	if (areq == AUTH_REQ_CRYPT)
		password = crypt(password, conn->salt);
471

472
	return packetSend(conn, password, strlen(password) + 1);
473
}
474 475 476 477 478

/*
 * fe_sendauth -- client demux routine for outgoing authentication information
 */
int
479
fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname,
480
			const char *password, char *PQerrormsg)
481
{
482
	switch (areq)
483
	{
484 485
			case AUTH_REQ_OK:
			break;
486

487
		case AUTH_REQ_KRB4:
488
#ifdef KRB4
489 490
			if (pg_krb4_sendauth(PQerrormsg, conn->sock, &conn->laddr.in,
								 &conn->raddr.in,
491 492 493 494 495 496 497
								 hostname) != STATUS_OK)
			{
				(void) sprintf(PQerrormsg,
							"fe_sendauth: krb4 authentication failed\n");
				return (STATUS_ERROR);
			}
			break;
498
#else
499 500 501
			(void) sprintf(PQerrormsg,
					 "fe_sendauth: krb4 authentication not supported\n");
			return (STATUS_ERROR);
502
#endif
503

504
		case AUTH_REQ_KRB5:
505
#ifdef KRB5
506 507
			if (pg_krb5_sendauth(PQerrormsg, conn->sock, &conn->laddr.in,
								 &conn->raddr.in,
508 509 510 511 512 513 514
								 hostname) != STATUS_OK)
			{
				(void) sprintf(PQerrormsg,
							"fe_sendauth: krb5 authentication failed\n");
				return (STATUS_ERROR);
			}
			break;
515
#else
516 517 518
			(void) sprintf(PQerrormsg,
					 "fe_sendauth: krb5 authentication not supported\n");
			return (STATUS_ERROR);
519
#endif
520

521 522
		case AUTH_REQ_PASSWORD:
		case AUTH_REQ_CRYPT:
523 524 525 526 527 528
			if (password == NULL || *password == '\0')
			{
				(void) sprintf(PQerrormsg,
				 "fe_sendauth: no password supplied\n");
				return (STATUS_ERROR);
			}
529 530 531 532 533 534
			if (pg_password_sendauth(conn, password, areq) != STATUS_OK)
			{
				(void) sprintf(PQerrormsg,
				 "fe_sendauth: error sending password authentication\n");
				return (STATUS_ERROR);
			}
535

536
			break;
537

538 539 540 541 542
		default:
			(void) sprintf(PQerrormsg,
			"fe_sendauth: authentication type %u not supported\n", areq);
			return (STATUS_ERROR);
	}
543

544
	return (STATUS_OK);
545 546 547 548 549 550 551 552 553
}

/*
 * fe_setauthsvc
 * fe_getauthsvc
 *
 * Set/return the authentication service currently selected for use by the
 * frontend. (You can only use one in the frontend, obviously.)
 */
554
static pg_authsvc = -1;
555 556

void
557
fe_setauthsvc(const char *name, char *PQerrormsg)
558
{
559
	int			i;
560 561 562 563 564 565 566 567 568 569 570 571

	for (i = 0; i < n_authsvcs; ++i)
		if (!strcmp(name, authsvcs[i].name))
		{
			pg_authsvc = i;
			break;
		}
	if (i == n_authsvcs)
	{
		(void) sprintf(PQerrormsg,
					   "fe_setauthsvc: invalid name: %s, ignoring...\n",
					   name);
572
	}
573
	return;
574 575 576
}

MsgType
577
fe_getauthsvc(char *PQerrormsg)
578
{
579 580 581
	if (pg_authsvc < 0 || pg_authsvc >= n_authsvcs)
		fe_setauthsvc(DEFAULT_CLIENT_AUTHSVC, PQerrormsg);
	return (authsvcs[pg_authsvc].msgtype);
582 583 584
}

/*
585
 * fe_getauthname -- returns a pointer to dynamic space containing whatever
586
 *					 name the user has authenticated to the system
587 588
 * if there is an error, return the error message in PQerrormsg
 */
589
char *
590
fe_getauthname(char *PQerrormsg)
591
{
592 593 594
	char	   *name = (char *) NULL;
	char	   *authn = (char *) NULL;
	MsgType		authsvc;
595 596 597 598

	authsvc = fe_getauthsvc(PQerrormsg);
	switch ((int) authsvc)
	{
599
#ifdef KRB4
600 601 602
		case STARTUP_KRB4_MSG:
			name = pg_krb4_authname(PQerrormsg);
			break;
603 604
#endif
#ifdef KRB5
605 606 607
		case STARTUP_KRB5_MSG:
			name = pg_krb5_authname(PQerrormsg);
			break;
608
#endif
609 610
		case STARTUP_MSG:
			{
Bruce Momjian's avatar
Bruce Momjian committed
611 612 613 614 615 616 617
#ifdef WIN32
				char username[128];
				DWORD namesize = sizeof(username) - 1;

				if (GetUserName(username,&namesize)) 
					name = username;
#else
618
				struct passwd *pw = getpwuid(geteuid());
619

620 621
				if (pw)
					name = pw->pw_name;
Bruce Momjian's avatar
Bruce Momjian committed
622
#endif
623 624 625 626
			}
			break;
		default:
			(void) sprintf(PQerrormsg,
627
				   "fe_getauthname: invalid authentication system: %d\n",
628 629
						   authsvc);
			break;
630 631
	}

632 633 634 635
	if (name && (authn = (char *) malloc(strlen(name) + 1)))
		strcpy(authn, name);
	return (authn);
}