1 /*-------------------------------------------------------------------------
4 * The front-end (client) authorization routines
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/interfaces/libpq/fe-auth.c
12 *-------------------------------------------------------------------------
17 * frontend (client) routines:
18 * pg_fe_sendauth send authentication information
19 * pg_fe_getauthname get user's name according to the client side
20 * of the authentication system
23 #include "postgres_fe.h"
30 #include <sys/param.h> /* for MAXHOSTNAMELEN on most */
31 #include <sys/socket.h>
32 #ifdef HAVE_SYS_UCRED_H
33 #include <sys/ucred.h>
35 #ifndef MAXHOSTNAMELEN
36 #include <netdb.h> /* for MAXHOSTNAMELEN on some */
43 #include "libpq/md5.h"
48 * GSSAPI authentication system.
51 #if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
53 * MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
54 * that contain the OIDs required. Redefine here, values copied
55 * from src/athena/auth/krb5/src/lib/gssapi/generic/gssapi_generic.c
57 static const gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_desc =
58 {10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
59 static GSS_DLLIMP gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
63 * Fetch all errors of a specific type and append to "str".
66 pg_GSS_error_int(PQExpBuffer str, const char *mprefix,
67 OM_uint32 stat, int type)
71 OM_uint32 msg_ctx = 0;
75 gss_display_status(&lmin_s, stat, type,
76 GSS_C_NO_OID, &msg_ctx, &lmsg);
77 appendPQExpBuffer(str, "%s: %s\n", mprefix, (char *) lmsg.value);
78 gss_release_buffer(&lmin_s, &lmsg);
83 * GSSAPI errors contain two parts; put both into conn->errorMessage.
86 pg_GSS_error(const char *mprefix, PGconn *conn,
87 OM_uint32 maj_stat, OM_uint32 min_stat)
89 resetPQExpBuffer(&conn->errorMessage);
91 /* Fetch major error codes */
92 pg_GSS_error_int(&conn->errorMessage, mprefix, maj_stat, GSS_C_GSS_CODE);
94 /* Add the minor codes as well */
95 pg_GSS_error_int(&conn->errorMessage, mprefix, min_stat, GSS_C_MECH_CODE);
99 * Continue GSS authentication with next token as needed.
102 pg_GSS_continue(PGconn *conn)
108 maj_stat = gss_init_sec_context(&min_stat,
115 GSS_C_NO_CHANNEL_BINDINGS,
116 (conn->gctx == GSS_C_NO_CONTEXT) ? GSS_C_NO_BUFFER : &conn->ginbuf,
122 if (conn->gctx != GSS_C_NO_CONTEXT)
124 free(conn->ginbuf.value);
125 conn->ginbuf.value = NULL;
126 conn->ginbuf.length = 0;
129 if (conn->goutbuf.length != 0)
132 * GSS generated data to send to the server. We don't care if it's the
133 * first or subsequent packet, just send the same kind of password
136 if (pqPacketSend(conn, 'p',
137 conn->goutbuf.value, conn->goutbuf.length)
140 gss_release_buffer(&lmin_s, &conn->goutbuf);
144 gss_release_buffer(&lmin_s, &conn->goutbuf);
146 if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
148 pg_GSS_error(libpq_gettext("GSSAPI continuation error"),
151 gss_release_name(&lmin_s, &conn->gtarg_nam);
153 gss_delete_sec_context(&lmin_s, &conn->gctx, GSS_C_NO_BUFFER);
157 if (maj_stat == GSS_S_COMPLETE)
158 gss_release_name(&lmin_s, &conn->gtarg_nam);
164 * Send initial GSS authentication token
167 pg_GSS_startup(PGconn *conn)
172 gss_buffer_desc temp_gbuf;
174 if (!(conn->pghost && conn->pghost[0] != '\0'))
176 printfPQExpBuffer(&conn->errorMessage,
177 libpq_gettext("host name must be specified\n"));
183 printfPQExpBuffer(&conn->errorMessage,
184 libpq_gettext("duplicate GSS authentication request\n"));
189 * Import service principal name so the proper ticket can be acquired by
192 maxlen = NI_MAXHOST + strlen(conn->krbsrvname) + 2;
193 temp_gbuf.value = (char *) malloc(maxlen);
194 if (!temp_gbuf.value)
196 printfPQExpBuffer(&conn->errorMessage,
197 libpq_gettext("out of memory\n"));
200 snprintf(temp_gbuf.value, maxlen, "%s@%s",
201 conn->krbsrvname, conn->pghost);
202 temp_gbuf.length = strlen(temp_gbuf.value);
204 maj_stat = gss_import_name(&min_stat, &temp_gbuf,
205 GSS_C_NT_HOSTBASED_SERVICE, &conn->gtarg_nam);
206 free(temp_gbuf.value);
208 if (maj_stat != GSS_S_COMPLETE)
210 pg_GSS_error(libpq_gettext("GSSAPI name import error"),
217 * Initial packet is the same as a continuation packet with no initial
220 conn->gctx = GSS_C_NO_CONTEXT;
222 return pg_GSS_continue(conn);
224 #endif /* ENABLE_GSS */
229 * SSPI authentication system (Windows only)
233 pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
237 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
238 sysmsg, sizeof(sysmsg), NULL) == 0)
239 printfPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x",
240 mprefix, (unsigned int) r);
242 printfPQExpBuffer(&conn->errorMessage, "%s: %s (%x)",
243 mprefix, sysmsg, (unsigned int) r);
247 * Continue SSPI authentication with next token as needed.
250 pg_SSPI_continue(PGconn *conn)
253 CtxtHandle newContext;
256 SecBufferDesc outbuf;
257 SecBuffer OutBuffers[1];
258 SecBuffer InBuffers[1];
260 if (conn->sspictx != NULL)
263 * On runs other than the first we have some data to send. Put this
264 * data in a SecBuffer type structure.
266 inbuf.ulVersion = SECBUFFER_VERSION;
268 inbuf.pBuffers = InBuffers;
269 InBuffers[0].pvBuffer = conn->ginbuf.value;
270 InBuffers[0].cbBuffer = conn->ginbuf.length;
271 InBuffers[0].BufferType = SECBUFFER_TOKEN;
274 OutBuffers[0].pvBuffer = NULL;
275 OutBuffers[0].BufferType = SECBUFFER_TOKEN;
276 OutBuffers[0].cbBuffer = 0;
278 outbuf.pBuffers = OutBuffers;
279 outbuf.ulVersion = SECBUFFER_VERSION;
281 r = InitializeSecurityContext(conn->sspicred,
284 ISC_REQ_ALLOCATE_MEMORY,
286 SECURITY_NETWORK_DREP,
287 (conn->sspictx == NULL) ? NULL : &inbuf,
294 if (r != SEC_E_OK && r != SEC_I_CONTINUE_NEEDED)
296 pg_SSPI_error(conn, libpq_gettext("SSPI continuation error"), r);
301 if (conn->sspictx == NULL)
303 /* On first run, transfer retreived context handle */
304 conn->sspictx = malloc(sizeof(CtxtHandle));
305 if (conn->sspictx == NULL)
307 printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
310 memcpy(conn->sspictx, &newContext, sizeof(CtxtHandle));
315 * On subsequent runs when we had data to send, free buffers that
316 * contained this data.
318 free(conn->ginbuf.value);
319 conn->ginbuf.value = NULL;
320 conn->ginbuf.length = 0;
324 * If SSPI returned any data to be sent to the server (as it normally
325 * would), send this data as a password packet.
327 if (outbuf.cBuffers > 0)
329 if (outbuf.cBuffers != 1)
332 * This should never happen, at least not for Kerberos
333 * authentication. Keep check in case it shows up with other
334 * authentication methods later.
336 printfPQExpBuffer(&conn->errorMessage, "SSPI returned invalid number of output buffers\n");
341 * If the negotiation is complete, there may be zero bytes to send.
342 * The server is at this point not expecting any more data, so don't
345 if (outbuf.pBuffers[0].cbBuffer > 0)
347 if (pqPacketSend(conn, 'p',
348 outbuf.pBuffers[0].pvBuffer, outbuf.pBuffers[0].cbBuffer))
350 FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
354 FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
357 /* Cleanup is handled by the code in freePGconn() */
362 * Send initial SSPI authentication token.
363 * If use_negotiate is 0, use kerberos authentication package which is
364 * compatible with Unix. If use_negotiate is 1, use the negotiate package
365 * which supports both kerberos and NTLM, but is not compatible with Unix.
368 pg_SSPI_startup(PGconn *conn, int use_negotiate)
373 conn->sspictx = NULL;
376 * Retreive credentials handle
378 conn->sspicred = malloc(sizeof(CredHandle));
379 if (conn->sspicred == NULL)
381 printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
385 r = AcquireCredentialsHandle(NULL,
386 use_negotiate ? "negotiate" : "kerberos",
387 SECPKG_CRED_OUTBOUND,
396 pg_SSPI_error(conn, libpq_gettext("could not acquire SSPI credentials"), r);
397 free(conn->sspicred);
398 conn->sspicred = NULL;
403 * Compute target principal name. SSPI has a different format from GSSAPI,
404 * but not more complex. We can skip the @REALM part, because Windows will
405 * fill that in for us automatically.
407 if (!(conn->pghost && conn->pghost[0] != '\0'))
409 printfPQExpBuffer(&conn->errorMessage,
410 libpq_gettext("host name must be specified\n"));
413 conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(conn->pghost) + 2);
414 if (!conn->sspitarget)
416 printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
419 sprintf(conn->sspitarget, "%s/%s", conn->krbsrvname, conn->pghost);
422 * Indicate that we're in SSPI authentication mode to make sure that
423 * pg_SSPI_continue is called next time in the negotiation.
427 return pg_SSPI_continue(conn);
429 #endif /* ENABLE_SSPI */
432 * Respond to AUTH_REQ_SCM_CREDS challenge.
434 * Note: this is dead code as of Postgres 9.1, because current backends will
435 * never send this challenge. But we must keep it as long as libpq needs to
436 * interoperate with pre-9.1 servers. It is believed to be needed only on
437 * Debian/kFreeBSD (ie, FreeBSD kernel with Linux userland, so that the
438 * getpeereid() function isn't provided by libc).
441 pg_local_sendauth(PGconn *conn)
443 #ifdef HAVE_STRUCT_CMSGCRED
447 struct cmsghdr *cmsg;
451 unsigned char buf[CMSG_SPACE(sizeof(struct cmsgcred))];
455 * The backend doesn't care what we send here, but it wants exactly one
456 * character to force recvmsg() to block and wait for us.
462 memset(&msg, 0, sizeof(msg));
466 /* We must set up a message that will be filled in by kernel */
467 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
468 msg.msg_control = &cmsgbuf.buf;
469 msg.msg_controllen = sizeof(cmsgbuf.buf);
470 cmsg = CMSG_FIRSTHDR(&msg);
471 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
472 cmsg->cmsg_level = SOL_SOCKET;
473 cmsg->cmsg_type = SCM_CREDS;
475 if (sendmsg(conn->sock, &msg, 0) == -1)
479 printfPQExpBuffer(&conn->errorMessage,
480 "pg_local_sendauth: sendmsg: %s\n",
481 pqStrerror(errno, sebuf, sizeof(sebuf)));
486 printfPQExpBuffer(&conn->errorMessage,
487 libpq_gettext("SCM_CRED authentication method not supported\n"));
493 pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
496 char *crypt_pwd = NULL;
497 const char *pwd_to_send;
499 /* Encrypt the password if needed. */
507 /* Allocate enough space for two MD5 hashes */
508 crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
511 printfPQExpBuffer(&conn->errorMessage,
512 libpq_gettext("out of memory\n"));
516 crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
517 if (!pg_md5_encrypt(password, conn->pguser,
518 strlen(conn->pguser), crypt_pwd2))
523 if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), conn->md5Salt,
524 sizeof(conn->md5Salt), crypt_pwd))
530 pwd_to_send = crypt_pwd;
533 case AUTH_REQ_PASSWORD:
534 pwd_to_send = password;
539 /* Packet has a message type as of protocol 3.0 */
540 if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
541 ret = pqPacketSend(conn, 'p', pwd_to_send, strlen(pwd_to_send) + 1);
543 ret = pqPacketSend(conn, 0, pwd_to_send, strlen(pwd_to_send) + 1);
551 * client demux routine for outgoing authentication information
554 pg_fe_sendauth(AuthRequest areq, PGconn *conn)
562 printfPQExpBuffer(&conn->errorMessage,
563 libpq_gettext("Kerberos 4 authentication not supported\n"));
567 printfPQExpBuffer(&conn->errorMessage,
568 libpq_gettext("Kerberos 5 authentication not supported\n"));
571 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
573 #if !defined(ENABLE_SSPI)
574 /* no native SSPI, so use GSSAPI library for it */
583 * If we have both GSS and SSPI support compiled in, use SSPI
584 * support by default. This is overridable by a connection
585 * string parameter. Note that when using SSPI we still leave
586 * the negotiate parameter off, since we want SSPI to use the
587 * GSSAPI kerberos protocol. For actual SSPI negotiate
588 * protocol, we use AUTH_REQ_SSPI.
590 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
591 if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
592 r = pg_GSS_startup(conn);
594 r = pg_SSPI_startup(conn, 0);
595 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
596 r = pg_GSS_startup(conn);
597 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
598 r = pg_SSPI_startup(conn, 0);
602 /* Error message already filled in. */
610 case AUTH_REQ_GSS_CONT:
615 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
617 r = pg_SSPI_continue(conn);
619 r = pg_GSS_continue(conn);
620 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
621 r = pg_GSS_continue(conn);
622 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
623 r = pg_SSPI_continue(conn);
627 /* Error message already filled in. */
634 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
635 /* No GSSAPI *or* SSPI support */
637 case AUTH_REQ_GSS_CONT:
638 printfPQExpBuffer(&conn->errorMessage,
639 libpq_gettext("GSSAPI authentication not supported\n"));
641 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
647 * SSPI has it's own startup message so libpq can decide which
648 * method to use. Indicate to pg_SSPI_startup that we want SSPI
649 * negotiation instead of Kerberos.
652 if (pg_SSPI_startup(conn, 1) != STATUS_OK)
654 /* Error message already filled in. */
663 * No SSPI support. However, if we have GSSAPI but not SSPI
664 * support, AUTH_REQ_SSPI will have been handled in the codepath
665 * for AUTH_REQ_GSSAPI above, so don't duplicate the case label in
668 #if !defined(ENABLE_GSS)
670 printfPQExpBuffer(&conn->errorMessage,
671 libpq_gettext("SSPI authentication not supported\n"));
673 #endif /* !define(ENABLE_GSSAPI) */
674 #endif /* ENABLE_SSPI */
678 printfPQExpBuffer(&conn->errorMessage,
679 libpq_gettext("Crypt authentication not supported\n"));
683 case AUTH_REQ_PASSWORD:
684 conn->password_needed = true;
685 if (conn->pgpass == NULL || conn->pgpass[0] == '\0')
687 printfPQExpBuffer(&conn->errorMessage,
688 PQnoPasswordSupplied);
691 if (pg_password_sendauth(conn, conn->pgpass, areq) != STATUS_OK)
693 printfPQExpBuffer(&conn->errorMessage,
694 "fe_sendauth: error sending password authentication\n");
699 case AUTH_REQ_SCM_CREDS:
700 if (pg_local_sendauth(conn) != STATUS_OK)
705 printfPQExpBuffer(&conn->errorMessage,
706 libpq_gettext("authentication method %u not supported\n"), areq);
717 * Returns a pointer to malloc'd space containing whatever name the user
718 * has authenticated to the system. If there is an error, return NULL,
719 * and put a suitable error message in *errorMessage if that's not NULL.
722 pg_fe_getauthname(PQExpBuffer errorMessage)
725 const char *name = NULL;
728 /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
729 char username[256 + 1];
730 DWORD namesize = sizeof(username);
732 uid_t user_id = geteuid();
734 struct passwd pwdstr;
735 struct passwd *pw = NULL;
740 * Some users are using configure --enable-thread-safety-force, so we
741 * might as well do the locking within our library to protect
742 * pqGetpwuid(). In fact, application developers can use getpwuid() in
743 * their application if they use the locking call we provide, or install
744 * their own locking function using PQregisterThreadLock().
749 if (GetUserName(username, &namesize))
751 else if (errorMessage)
752 printfPQExpBuffer(errorMessage,
753 libpq_gettext("user name lookup failure: error code %lu\n"),
756 pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
759 else if (errorMessage)
762 printfPQExpBuffer(errorMessage,
763 libpq_gettext("could not look up local user ID %d: %s\n"),
765 pqStrerror(pwerr, pwdbuf, sizeof(pwdbuf)));
767 printfPQExpBuffer(errorMessage,
768 libpq_gettext("local user with ID %d does not exist\n"),
775 result = strdup(name);
776 if (result == NULL && errorMessage)
777 printfPQExpBuffer(errorMessage,
778 libpq_gettext("out of memory\n"));
788 * PQencryptPassword -- exported routine to encrypt a password
790 * This is intended to be used by client applications that wish to send
791 * commands like ALTER USER joe PASSWORD 'pwd'. The password need not
792 * be sent in cleartext if it is encrypted on the client side. This is
793 * good because it ensures the cleartext password won't end up in logs,
794 * pg_stat displays, etc. We export the function so that clients won't
795 * be dependent on low-level details like whether the enceyption is MD5
798 * Arguments are the cleartext password, and the SQL name of the user it
801 * Return value is a malloc'd string, or NULL if out-of-memory. The client
802 * may assume the string doesn't contain any special characters that would
806 PQencryptPassword(const char *passwd, const char *user)
810 crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
814 if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))