1 /*-------------------------------------------------------------------------
4 * The front-end (client) authorization routines
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * NOTE: the error message strings returned by this module must not
10 * exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
13 * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.61 2001/09/26 19:54:12 momjian Exp $
15 *-------------------------------------------------------------------------
20 * frontend (client) routines:
21 * fe_sendauth send authentication information
22 * fe_getauthname get user's name according to the client side
23 * of the authentication system
24 * fe_setauthsvc set frontend authentication service
25 * fe_getauthsvc get current frontend authentication service
31 #include "postgres_fe.h"
39 #include <sys/types.h>
40 #include <sys/param.h> /* for MAXHOSTNAMELEN on most */
41 #include <sys/socket.h>
42 #if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
44 #include <sys/ucred.h>
46 #ifndef MAXHOSTNAMELEN
47 #include <netdb.h> /* for MAXHOSTNAMELEN on some */
57 #include "libpq-int.h"
59 #include "libpq/crypt.h"
63 * common definitions for generic fe/be routines
68 char name[NAMEDATALEN]; /* service nickname (for command
70 MsgType msgtype; /* startup packet header type */
71 int allowed; /* initially allowed (before command line
76 * Command-line parsing routines use this structure to map nicknames
77 * onto service types (and the startup packets to use with them).
79 * Programs receiving an authentication request use this structure to
80 * decide which authentication service types are currently permitted.
81 * By default, all authentication systems compiled into the system are
82 * allowed. Unauthenticated connections are disallowed unless there
83 * isn't any authentication system.
85 static const struct authsvc authsvcs[] = {
87 {"krb4", STARTUP_KRB4_MSG, 1},
88 {"kerberos", STARTUP_KRB4_MSG, 1},
91 {"krb5", STARTUP_KRB5_MSG, 1},
92 {"kerberos", STARTUP_KRB5_MSG, 1},
94 {UNAUTHNAME, STARTUP_MSG,
95 #if defined(KRB4) || defined(KRB5)
97 #else /* !(KRB4 || KRB5) */
99 #endif /* !(KRB4 || KRB5) */
101 {"password", STARTUP_PASSWORD_MSG, 0}
104 static const int n_authsvcs = sizeof(authsvcs) / sizeof(struct authsvc);
108 * MIT Kerberos authentication system - protocol version 4
113 /* for some reason, this is not defined in krb.h ... */
114 extern char *tkt_string(void);
117 * pg_krb4_init -- initialization performed before any Kerberos calls are made
119 * For v4, all we need to do is make sure the library routines get the right
120 * ticket file if we want them to see a special one. (They will open the file
127 static int init_done = 0;
134 * If the user set PGREALM, then we use a ticket file with a special
135 * name: <usual-ticket-file-name>@<PGREALM-value>
137 if ((realm = getenv("PGREALM")))
139 char tktbuf[MAXPGPATH];
141 (void) sprintf(tktbuf, "%s@%s", tkt_string(), realm);
142 krb_set_tkt_string(tktbuf);
147 * pg_krb4_authname -- returns a pointer to static space containing whatever
148 * name the user has authenticated to the system
150 * We obtain this information by digging around in the ticket file.
153 pg_krb4_authname(char *PQerrormsg)
155 char instance[INST_SZ + 1];
156 char realm[REALM_SZ + 1];
158 static char name[SNAME_SZ + 1] = "";
165 name[SNAME_SZ] = '\0';
166 status = krb_get_tf_fullname(tkt_string(), name, instance, realm);
167 if (status != KSUCCESS)
169 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
170 "pg_krb4_authname: krb_get_tf_fullname: %s\n",
171 krb_err_txt[status]);
172 return (char *) NULL;
178 * pg_krb4_sendauth -- client routine to send authentication information to
181 * This routine does not do mutual authentication, nor does it return enough
182 * information to do encrypted connections. But then, if we want to do
183 * encrypted connections, we'll have to redesign the whole RPC mechanism
186 * If the user is too lazy to feed us a hostname, we try to come up with
187 * something other than "localhost" since the hostname is used as an
188 * instance and instance names in v4 databases are usually actual hostnames
189 * (canonicalized to omit all domain suffixes).
192 pg_krb4_sendauth(char *PQerrormsg, int sock,
193 struct sockaddr_in * laddr,
194 struct sockaddr_in * raddr,
195 const char *hostname)
197 long krbopts = 0; /* one-way authentication */
200 char hostbuf[MAXHOSTNAMELEN];
201 const char *realm = getenv("PGREALM"); /* NULL == current realm */
203 if (!hostname || !(*hostname))
205 if (gethostname(hostbuf, MAXHOSTNAMELEN) < 0)
206 strcpy(hostbuf, "localhost");
212 status = krb_sendauth(krbopts,
220 (CREDENTIALS *) NULL,
225 if (status != KSUCCESS)
227 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
228 libpq_gettext("Kerberos 4 error: %s\n"),
229 krb_err_txt[status]);
239 * MIT Kerberos authentication system - protocol version 5
246 * pg_an_to_ln -- return the local name corresponding to an authentication
249 * XXX Assumes that the first aname component is the user name. This is NOT
250 * necessarily so, since an aname can actually be something out of your
251 * worst X.400 nightmare, like
252 * ORGANIZATION=U. C. Berkeley/NAME=Paul M. Aoki@CS.BERKELEY.EDU
253 * Note that the MIT an_to_ln code does the same thing if you don't
254 * provide an aname mapping database...it may be a better idea to use
255 * krb5_an_to_ln, except that it punts if multiple components are found,
256 * and we can't afford to punt.
259 pg_an_to_ln(char *aname)
263 if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
270 * Various krb5 state which is not connection specfic, and a flag to
271 * indicate whether we have initialised it yet.
273 static int pg_krb5_initialised;
274 static krb5_context pg_krb5_context;
275 static krb5_ccache pg_krb5_ccache;
276 static krb5_principal pg_krb5_client;
277 static char *pg_krb5_name;
281 pg_krb5_init(char *PQerrormsg)
283 krb5_error_code retval;
285 if (pg_krb5_initialised)
288 retval = krb5_init_context(&pg_krb5_context);
291 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
292 "pg_krb5_init: krb5_init_context: %s\n",
293 error_message(retval));
297 retval = krb5_cc_default(pg_krb5_context, &pg_krb5_ccache);
300 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
301 "pg_krb5_init: krb5_cc_default: %s\n",
302 error_message(retval));
303 krb5_free_context(pg_krb5_context);
307 retval = krb5_cc_get_principal(pg_krb5_context, pg_krb5_ccache,
311 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
312 "pg_krb5_init: krb5_cc_get_principal: %s\n",
313 error_message(retval));
314 krb5_cc_close(pg_krb5_context, pg_krb5_ccache);
315 krb5_free_context(pg_krb5_context);
319 retval = krb5_unparse_name(pg_krb5_context, pg_krb5_client, &pg_krb5_name);
322 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
323 "pg_krb5_init: krb5_unparse_name: %s\n",
324 error_message(retval));
325 krb5_free_principal(pg_krb5_context, pg_krb5_client);
326 krb5_cc_close(pg_krb5_context, pg_krb5_ccache);
327 krb5_free_context(pg_krb5_context);
331 pg_krb5_name = pg_an_to_ln(pg_krb5_name);
333 pg_krb5_initialised = 1;
339 * pg_krb5_authname -- returns a pointer to static space containing whatever
340 * name the user has authenticated to the system
343 pg_krb5_authname(char *PQerrormsg)
345 if (pg_krb5_init(PQerrormsg) != STATUS_OK)
353 * pg_krb5_sendauth -- client routine to send authentication information to
357 pg_krb5_sendauth(char *PQerrormsg, int sock,
358 struct sockaddr_in * laddr,
359 struct sockaddr_in * raddr,
360 const char *hostname)
362 krb5_error_code retval;
364 krb5_principal server;
365 krb5_auth_context auth_context = NULL;
366 krb5_error *err_ret = NULL;
369 ret = pg_krb5_init(PQerrormsg);
370 if (ret != STATUS_OK)
373 retval = krb5_sname_to_principal(pg_krb5_context, hostname, PG_KRB_SRVNAM,
374 KRB5_NT_SRV_HST, &server);
377 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
378 "pg_krb5_sendauth: krb5_sname_to_principal: %s\n",
379 error_message(retval));
384 * libpq uses a non-blocking socket. But kerberos needs a blocking
385 * socket, and we have to block somehow to do mutual authentication
386 * anyway. So we temporarily make it blocking.
388 flags = fcntl(sock, F_GETFL);
389 if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK)))
391 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
392 libpq_gettext("could not set socket to blocking mode: %s\n"), strerror(errno));
393 krb5_free_principal(pg_krb5_context, server);
397 retval = krb5_sendauth(pg_krb5_context, &auth_context,
398 (krb5_pointer) & sock, PG_KRB_SRVNAM,
399 pg_krb5_client, server,
400 AP_OPTS_MUTUAL_REQUIRED,
401 NULL, 0, /* no creds, use ccache instead */
402 pg_krb5_ccache, &err_ret, NULL, NULL);
405 if (retval == KRB5_SENDAUTH_REJECTED && err_ret)
407 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
408 libpq_gettext("Kerberos 5 authentication rejected: %*s\n"),
409 err_ret->text.length, err_ret->text.data);
413 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
414 "krb5_sendauth: %s\n", error_message(retval));
418 krb5_free_error(pg_krb5_context, err_ret);
423 krb5_free_principal(pg_krb5_context, server);
425 if (fcntl(sock, F_SETFL, (long) flags))
427 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
428 libpq_gettext("could not restore non-blocking mode on socket: %s\n"),
439 pg_local_sendauth(char *PQerrormsg, PGconn *conn)
441 #if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || (defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
445 #ifdef HAVE_STRUCT_CMSGCRED
446 /* Prevent padding */
447 char cmsgmem[sizeof(struct cmsghdr) + sizeof(struct cmsgcred)];
448 /* Point to start of first structure */
449 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgmem;
451 #ifdef HAVE_STRUCT_SOCKCRED
452 /* Prevent padding */
453 char cmsgmem[sizeof(struct cmsghdr) + sizeof(struct sockcred)];
454 /* Point to start of first structure */
455 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgmem;
459 * The backend doesn't care what we send here, but it wants
460 * exactly one character to force recvmsg() to block and wait
467 memset(&msg, 0, sizeof(msg));
471 #ifdef HAVE_STRUCT_CMSGCRED
472 /* Create control header, FreeBSD */
473 msg.msg_control = cmsg;
474 msg.msg_controllen = sizeof(cmsgmem);
475 memset(cmsg, 0, sizeof(cmsgmem));
476 cmsg->cmsg_len = sizeof(cmsgmem);
477 cmsg->cmsg_level = SOL_SOCKET;
478 cmsg->cmsg_type = SCM_CREDS;
481 if (sendmsg(conn->sock, &msg, 0) == -1)
483 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
484 "pg_local_sendauth: sendmsg: %s\n", strerror(errno));
489 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
490 libpq_gettext("SCM_CRED authentication method not supported\n"));
496 pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
501 /* Encrypt the password if needed. */
509 if (!(crypt_pwd = malloc(MD5_PASSWD_LEN+1)) ||
510 !(crypt_pwd2 = malloc(MD5_PASSWD_LEN+1)))
515 if (!EncryptMD5(password, conn->pguser,
516 strlen(conn->pguser), crypt_pwd2))
522 if (!EncryptMD5(crypt_pwd2 + strlen("md5"), conn->md5Salt,
523 sizeof(conn->md5Salt), crypt_pwd))
536 StrNCpy(salt, conn->cryptSalt,3);
537 crypt_pwd = crypt(password, salt);
540 case AUTH_REQ_PASSWORD:
541 /* discard const so we can assign it */
542 crypt_pwd = (char *)password;
547 ret = pqPacketSend(conn, crypt_pwd, strlen(crypt_pwd) + 1);
548 if (areq == AUTH_REQ_MD5)
554 * fe_sendauth -- client demux routine for outgoing authentication information
557 fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname,
558 const char *password, char *PQerrormsg)
560 #if !defined(KRB4) && !defined(KRB5)
561 (void) hostname; /* not used */
571 if (pg_krb4_sendauth(PQerrormsg, conn->sock, &conn->laddr.in,
573 hostname) != STATUS_OK)
575 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
576 libpq_gettext("Kerberos 4 authentication failed\n"));
581 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
582 libpq_gettext("Kerberos 4 authentication not supported\n"));
588 if (pg_krb5_sendauth(PQerrormsg, conn->sock, &conn->laddr.in,
590 hostname) != STATUS_OK)
592 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
593 libpq_gettext("Kerberos 5 authentication failed\n"));
598 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
599 libpq_gettext("Kerberos 5 authentication not supported\n"));
605 case AUTH_REQ_PASSWORD:
606 if (password == NULL || *password == '\0')
608 (void) sprintf(PQerrormsg,
609 "fe_sendauth: no password supplied\n");
612 if (pg_password_sendauth(conn, password, areq) != STATUS_OK)
614 (void) sprintf(PQerrormsg,
615 "fe_sendauth: error sending password authentication\n");
620 case AUTH_REQ_SCM_CREDS:
621 if (pg_local_sendauth(PQerrormsg, conn) != STATUS_OK)
626 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
627 libpq_gettext("authentication method %u not supported\n"), areq);
638 * Set/return the authentication service currently selected for use by the
639 * frontend. (You can only use one in the frontend, obviously.)
641 * NB: This is not thread-safe if different threads try to select different
642 * authentication services! It's OK for fe_getauthsvc to select the default,
643 * since that will be the same for all threads, but direct application use
644 * of fe_setauthsvc is not thread-safe. However, use of fe_setauthsvc is
645 * deprecated anyway...
648 static int pg_authsvc = -1;
651 fe_setauthsvc(const char *name, char *PQerrormsg)
655 for (i = 0; i < n_authsvcs; ++i)
656 if (strcmp(name, authsvcs[i].name) == 0)
663 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
664 libpq_gettext("invalid authentication service name \"%s\", ignored\n"),
671 fe_getauthsvc(char *PQerrormsg)
673 if (pg_authsvc < 0 || pg_authsvc >= n_authsvcs)
674 fe_setauthsvc(DEFAULT_CLIENT_AUTHSVC, PQerrormsg);
675 return authsvcs[pg_authsvc].msgtype;
679 * fe_getauthname -- returns a pointer to dynamic space containing whatever
680 * name the user has authenticated to the system
681 * if there is an error, return the error message in PQerrormsg
684 fe_getauthname(char *PQerrormsg)
686 char *name = (char *) NULL;
687 char *authn = (char *) NULL;
690 authsvc = fe_getauthsvc(PQerrormsg);
693 if (authsvc == STARTUP_KRB4_MSG)
694 name = pg_krb4_authname(PQerrormsg);
697 if (authsvc == STARTUP_KRB5_MSG)
698 name = pg_krb5_authname(PQerrormsg);
701 if (authsvc == STARTUP_MSG
702 || (authsvc == STARTUP_KRB4_MSG && !name)
703 || (authsvc == STARTUP_KRB5_MSG && !name))
707 DWORD namesize = sizeof(username) - 1;
709 if (GetUserName(username, &namesize))
712 struct passwd *pw = getpwuid(geteuid());
719 if (authsvc != STARTUP_MSG && authsvc != STARTUP_KRB4_MSG && authsvc != STARTUP_KRB5_MSG)
720 snprintf(PQerrormsg, PQERRORMSG_LENGTH,
721 libpq_gettext("fe_getauthname: invalid authentication system: %d\n"),
724 if (name && (authn = (char *) malloc(strlen(name) + 1)))