Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aj7RM-000490-7H for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Mar 2016 15:44:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aj7RL-0002cB-G5 for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Mar 2016 15:44:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aj7RI-0002XV-AE; Thu, 24 Mar 2016 15:44:48 +0000 Received: from mail-db3on0092.outbound.protection.outlook.com ([157.55.234.92] helo=emea01-db3-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aj7RE-0000Hp-GH; Thu, 24 Mar 2016 15:44:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gloomberg.onmicrosoft.com; s=selector1-chrullrich-net; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=powF9BB2/sO2XI0il7BnLPYxa5oiX2clVFecQauMU7A=; b=HhfFTjwsDDpXjw5btUzutarxu2iUjHLPfFHYj+UDiM6qxRP0sKKbjK4bZGITQB8hCX5PsC5cvCkDhV2JTXh9Jz+W6yqoTPNBGNYAQhiedDA86wUJ++YXFMDOd/Q33D+533c54wLVOY4GUwbI70rYdfeZgkuleMXmlrvi4/zvrAA= Authentication-Results: postgresql.org; dkim=none (message not signed) header.d=none;postgresql.org; dmarc=none action=none header.from=chrullrich.net; Received: from [192.168.92.135] (217.31.70.82) by DB4PR06MB0701.eurprd06.prod.outlook.com (10.161.14.13) with Microsoft SMTP Server (TLS) id 15.1.443.12; Thu, 24 Mar 2016 15:44:40 +0000 Subject: Re: BUG #13854: SSPI authentication failure: wrong realm name used To: Robbie Harwood , PostgreSQL-development References: <20160108002526.16053.79037@wrigleys.postgresql.org> <56EEC584.1080702@chrullrich.net> CC: pgsql-bugs From: Christian Ullrich Message-ID: <56F40B65.8050909@chrullrich.net> Date: Thu, 24 Mar 2016 16:44:37 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------000006010701060300000002" X-Originating-IP: [217.31.70.82] X-ClientProxiedBy: DB4PR08CA0013.eurprd08.prod.outlook.com (25.161.12.23) To DB4PR06MB0701.eurprd06.prod.outlook.com (25.161.14.13) X-MS-Office365-Filtering-Correlation-Id: c2ddd69b-bd41-46c9-ab7e-08d353fb36c2 X-Microsoft-Exchange-Diagnostics: 1;DB4PR06MB0701;2:w/3DZ9UbEDEyuHBllPnGuipWz932dqD6sRhN7zU4pOVx5LKLp7/TPpy/nIA9mKwi5bz/2OuhfalY+0wASZPkmlSvxvgwPxlKKspLh/AApVuyaVTyraN/XkP+XoHQnMZKsMP7Wk1jLWzVIF8lNJQbwNIWwlEUenZIKPYP3oJodcA41D0AKBjZv6Cx7K59Vgox;3:VLK6dRRTDHYYo9cg7iCW1P026Pjh4+KBf86TRs2k8mZcghYkzdVQu2K/ca2qlivFDY4usAYOLpk0il5Dmips5lnw8w6vRT5A0AXi9k8VfF7/8hxql9S1mFGG9UWDrJJJ;25:3Ru4WfRrpw6GeqRr8/tZTiDosFqYlX39MaZdQdVf2F5MJUZ+8+n9I6qK1cBTmrVy9C+GUketMgcSCCgw9PHXTZSBOH53NG0n4yGEPf52n2QTireBAM67MNSHaGeds7dtekaaxedy3botccge733LjLC/D5DOiXTKnSVhpV2LyZWANcxnuia6MpYm6sdj2YpGyOua/DOQkfqmO+kWnOrg0iALatAXhlFkNSaAmgbJ2J8/gqkxVTG4vwWTM6xV6+1tuhQeYv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0c2sxIBvTAgluCeaj5OkDWWsmMCHgGFbLxNORP+q7k/j5CTuy0/WQFxQDZa6aT3LnhX2XtY1mF+0pMgxvuCkHfzVGTJGWezVQkghC5401Ll57uBNyxK/+5n2i3cUI5qE6EcOqIYuNGbuUAwahGSbkOtBIBLB7ZH3ctj6zwYgWhlAHL4dVRt8lVQB2VGSjwqUNepDseswTkw7+MIHS3wL8/EyXR2H76y5j3RbdVxJ4THHbsJS0cVSr6tdzCLrzxvLS6aOcxUXsbB5C+8xJ+Zf4KpFbL8js9ESiCdzzudokGeBM/qVbzO+NQF+gJA= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(102415293)(102615271)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001);SRVR:DB4PR06MB0701;BCL:0;PCL:0;RULEID:;SRVR:DB4PR06MB0701; X-Forefront-PRVS: 0891BC3F3D X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6049001)(6009001)(377424004)(24454002)(5008740100001)(2476003)(1096002)(6116002)(568964002)(5890100001)(3846002)(66066001)(65956001)(586003)(4326007)(2906002)(270700001)(65806001)(64126003)(2950100001)(92566002)(54356999)(4610100001)(80316001)(19580405001)(84326002)(19580395003)(83506001)(93886004)(76176999)(36756003)(512874002)(5004730100002)(512944002)(65816999)(87266999)(81166005)(50986999)(42186005)(4001350100001)(77096005)(5001770100001)(189998001)(33656002)(86362001)(117156001)(59896002);DIR:OUT;SFP:1101;SCL:1;SRVR:DB4PR06MB0701;H:[192.168.92.135];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DB4PR06MB0701;23:T9vOUKK5sGu+QJmRCBwJ0vxkrn0rfE3KqojknArOp?= =?us-ascii?Q?QuE7Ofw8kYdmCw2p0x/fWt0+zxHSzL8NuS5kb377CuFH+yiovHV1I5otEtZN?= =?us-ascii?Q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ufpXgd5vMolaZNO+C1MCj?= =?us-ascii?Q?UGXMCbp7NryhwJR/X4q32e6W7/93BQOtZ2GeURhkZvOGkHBEqghJHz0JgmeO?= =?us-ascii?Q?dOvZn0Fpy6BEGzNkHbt+X4Z9EMpsEF7/JXrFjYC3DaVfjqtWcjyJh0l0bcAN?= =?us-ascii?Q?nn1ymWNaj5aCXZYXDAYKJ6My/WzZWPeGMC8O1xYUJ/tFwaYhUYkSbO1aoPHz?= =?us-ascii?Q?WvOgmnaCGGy6lIlHvCkWvL9lKMfSoQf31gS8XcXq38OUfIC+18GMqdcsbR6j?= =?us-ascii?Q?DHIRujmfTSlhjzLcEe+W3dvMNwCthZnO2lunB56?= X-Microsoft-Exchange-Diagnostics: 1;DB4PR06MB0701;5:j0N27N0EaBdSRBpn4Eo/nk9YBGqHaE8KI7z+jqRT7zs3l75Ds+BOnSY0OeIHUlNV8yqG5g43MLCWYYWRT/tFedWuo0Fs0jbtGxmfxbv6xkCYkd+nQjF5++KBO7SA1O1cxDWIyEJAX6g9NfrB1eri4Q==;24:pWeC+AlOU0JLJNYvPudKoSaxW9+7BzpoW8nGmV/qmKskr9cWNujtKyE4zUOiGkXeM96QxjJ80qtHocJ8u9legUdvIEL//1mvvYSP80vL5gA= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: chrullrich.net X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2016 15:44:40.8121 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4PR06MB0701 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --------------000006010701060300000002 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit On 2016-03-24 16:35, Christian Ullrich wrote: > * From: Robbie Harwood [mailto:rharwood@redhat.com] > >> Christian Ullrich writes: >>> pg_SSPI_recvauth(Port *port) >>> { >>> int mtype; >>> + int status; >> >> The section of this function for include_realm checking already uses an >> int status return code (retval). I would expect to see them share a >> variable rather than have both "retval" and "status". > > I would not, because retval is local to that last if, but you are right, status > does not need to be in function scope. Moved declaration. >>> + /* Build SAM name (DOMAIN\\user), then translate to UPN >>> + (user@kerberos.realm). The realm name is returned in >>> + lower case, but that is fine because in SSPI auth, >>> + string comparisons are always case-insensitive. */ >> >> Since we're already considering changing things: this is not the comment >> style for this file (though it is otherwise a good comment). > > True. Will fix. Reformatted. >>> + upname = (char*)palloc(upnamesize); >> >> I don't believe this cast is typically included. > > Left over from when this was malloc() before Magnus' first look at it. Removed. Updated patch attached. -- Christian --------------000006010701060300000002 Content-Type: text/plain; charset="UTF-8"; name="sspirealm.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sspirealm.patch" diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml new file mode 100644 index 3b2935c..f7d7b5a *** a/doc/src/sgml/client-auth.sgml --- b/doc/src/sgml/client-auth.sgml *************** omicron bryanh *** 1097,1102 **** --- 1097,1140 ---- + compat_realm + + + If set to 1, the domain's SAM-compatible name (also known as the + NetBIOS name) is used for the include_realm + option. This is the default. If set to 0, the true realm name from + the Kerberos user principal name is used. Leave this option + disabled to maintain compatibility with existing + pg_ident.conf files. + + + Do not enable this option unless your server runs under a domain + account (this includes virtual service accounts on a domain member + system) and all clients authenticating through SSPI are also using + domain accounts, or authentication will fail. + + + + + + upn_username + + + If this option is enabled along with compat_realm, + the user name from the Kerberos UPN is used for authentication. If + it is disabled (the default), the SAM-compatible user name is used. + By default, these two names are identical for new user accounts. + + + Note that libpq uses the SAM-compatible name if no + explicit user name is specified. If you use + libpq (e.g. through the ODBC driver), you should + leave this option disabled. + + + + + map diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c new file mode 100644 index 7f1ae8c..6830764 *** a/src/backend/libpq/auth.c --- b/src/backend/libpq/auth.c *************** typedef SECURITY_STATUS *** 155,160 **** --- 155,165 ---- (WINAPI * QUERY_SECURITY_CONTEXT_TOKEN_FN) ( PCtxtHandle, void **); static int pg_SSPI_recvauth(Port *port); + static int pg_SSPI_make_upn(char *accountname, + size_t accountnamesize, + char *domainname, + size_t domainnamesize, + bool update_accountname); #endif /*---------------------------------------------------------------- *************** pg_SSPI_recvauth(Port *port) *** 1263,1268 **** --- 1268,1282 ---- free(tokenuser); + if (!port->hba->compat_realm) + { + int status = pg_SSPI_make_upn(accountname, sizeof(accountname), + domainname, sizeof(domainname), + port->hba->upn_username); + if (status != STATUS_OK) + return status; + } + /* * Compare realm/domain if requested. In SSPI, always compare case * insensitive. *************** pg_SSPI_recvauth(Port *port) *** 1298,1303 **** --- 1312,1419 ---- else return check_usermap(port->hba->usermap, port->user_name, accountname, true); } + + /* + * Replaces the domainname with the Kerberos realm name, + * and optionally the accountname with the Kerberos user name. + */ + static int pg_SSPI_make_upn(char *accountname, + size_t accountnamesize, + char *domainname, + size_t domainnamesize, + bool update_accountname) + { + char *samname; + char *upname = NULL; + char *p = NULL; + ULONG upnamesize = 0; + size_t upnamerealmsize; + BOOLEAN res; + + /* + * Build SAM name (DOMAIN\\user), then translate to UPN + * (user@kerberos.realm). The realm name is returned in + * lower case, but that is fine because in SSPI auth, + * string comparisons are always case-insensitive. + */ + + samname = psprintf("%s\\%s", domainname, accountname); + res = TranslateName(samname, NameSamCompatible, NameUserPrincipal, + NULL, &upnamesize); + + if ((!res && GetLastError() != ERROR_INSUFFICIENT_BUFFER) + || upnamesize == 0) + { + pfree(samname); + ereport(LOG, + (errcode(ERRCODE_INVALID_ROLE_SPECIFICATION), + errmsg("could not translate name"))); + return STATUS_ERROR; + } + + /* upnamesize includes the NUL. */ + upname = palloc(upnamesize); + + if (!upname) + { + pfree(samname); + ereport(LOG, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + return STATUS_ERROR; + } + + res = TranslateName(samname, NameSamCompatible, NameUserPrincipal, + upname, &upnamesize); + + pfree(samname); + if (res) + p = strchr(upname, '@'); + + if (!res || p == NULL) + { + pfree(upname); + ereport(LOG, + (errcode(ERRCODE_INVALID_ROLE_SPECIFICATION), + errmsg("could not translate name"))); + return STATUS_ERROR; + } + + /* Length of realm name after the '@', including the NUL. */ + upnamerealmsize = upnamesize - (p - upname + 1); + + /* Replace domainname with realm name. */ + if (upnamerealmsize > domainnamesize) + { + pfree(upname); + ereport(LOG, + (errcode(ERRCODE_INVALID_ROLE_SPECIFICATION), + errmsg("realm name too long"))); + return STATUS_ERROR; + } + + /* Length is now safe. */ + strcpy(domainname, p+1); + + /* Replace account name as well (in case UPN != SAM)? */ + if (update_accountname) + { + if ((p - upname + 1) > accountnamesize) + { + pfree(upname); + ereport(LOG, + (errcode(ERRCODE_INVALID_ROLE_SPECIFICATION), + errmsg("translated account name too long"))); + return STATUS_ERROR; + } + + *p = 0; + strcpy(accountname, upname); + } + + pfree(upname); + return STATUS_OK; + } #endif /* ENABLE_SSPI */ diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c new file mode 100644 index 28f9fb5..9ca6925 *** a/src/backend/libpq/hba.c --- b/src/backend/libpq/hba.c *************** parse_hba_line(List *line, int line_num, *** 1287,1292 **** --- 1287,1302 ---- parsedline->auth_method == uaSSPI) parsedline->include_realm = true; + /* + * For SSPI, include_realm defaults to the SAM-compatible domain + * (aka NetBIOS name) and user names instead of the Kerberos + * principal name for compatibility. + */ + if (parsedline->auth_method == uaSSPI) { + parsedline->compat_realm = true; + parsedline->upn_username = false; + } + /* Parse remaining arguments */ while ((field = lnext(field)) != NULL) { *************** parse_hba_auth_opt(char *name, char *val *** 1570,1575 **** --- 1580,1603 ---- else hbaline->include_realm = false; } + else if (strcmp(name, "compat_realm") == 0) + { + if (hbaline->auth_method != uaSSPI) + INVALID_AUTH_OPTION("compat_realm", gettext_noop("sspi")); + if (strcmp(val, "1") == 0) + hbaline->compat_realm = true; + else + hbaline->compat_realm = false; + } + else if (strcmp(name, "upn_username") == 0) + { + if (hbaline->auth_method != uaSSPI) + INVALID_AUTH_OPTION("upn_username", gettext_noop("sspi")); + if (strcmp(val, "1") == 0) + hbaline->upn_username = true; + else + hbaline->upn_username = false; + } else if (strcmp(name, "radiusserver") == 0) { struct addrinfo *gai_result; diff --git a/src/include/libpq/hba.h b/src/include/libpq/hba.h new file mode 100644 index 68a953a..f3868f7 *** a/src/include/libpq/hba.h --- b/src/include/libpq/hba.h *************** typedef struct HbaLine *** 77,82 **** --- 77,84 ---- bool clientcert; char *krb_realm; bool include_realm; + bool compat_realm; + bool upn_username; char *radiusserver; char *radiussecret; char *radiusidentifier; --------------000006010701060300000002 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------000006010701060300000002--