Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tGNo0-002dMO-08 for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Nov 2024 19:34:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tGNny-000Uop-I8 for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Nov 2024 19:34:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tGNny-000Uoh-6Z for pgsql-bugs@lists.postgresql.org; Wed, 27 Nov 2024 19:34:30 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tGNnv-004NSy-K2 for pgsql-bugs@lists.postgresql.org; Wed, 27 Nov 2024 19:34:29 +0000 Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-7b66d5f6ec8so7532985a.0 for ; Wed, 27 Nov 2024 11:34:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732736065; x=1733340865; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=o4nw3mXGfGuuRosZ1gOWi/CJCN7EUDdSCbj7fiHPk5Y=; b=ZH7721mRUtobKz6SpWc40zulGgoiBNd3SaTdvNT4hXp4xtiYbReEbkIImZLMntPTA2 C4n0W4LSFkSKE4ELaE4mFt8PlaM76GVbKycgWkZ+LuRUP7/TLSUqNVRvMzoXxsX7oonq qvOloJ6fHvYUtWSGph75tZp475YJcvb+nzOKlogwize0eapK0t+y9gV/qZ5xo0a7xE/W zQAL4nrs7np1qs8wE3R6jIqK/5NCHKu5YC4h7eODgXu2w+AnXmYo2IlgAkt95KH52SkZ WmHALuWYtlsdSLBer5xHcQL0M4zcJK0hBZWIsRiuHR+jPeADQRajEZA9JNJD5/gbZkkP NyXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732736065; x=1733340865; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=o4nw3mXGfGuuRosZ1gOWi/CJCN7EUDdSCbj7fiHPk5Y=; b=pHy5g9Qg//q9CZ5PV8wyMbfgwzPtfU8Yqt8jy05i7rH1XvWnvrm3E+JMl1TVsujjdP Fme1akdnyNWmn3JoJuntvkpFwaQV2gob7e1wTceWFPRIopOMaVqKlAZ6ttdx9aXdYHkN yuyi9a3AanWbUqAxIPFoge1qPLbiAdU4q48PVIypsGkdmEWhldP2GzPtgTCo6Wl+JkEc C77ZYhgh+BlUXCUqD5Eofe40IjFDRI5depzb9ts/LogAUvcq1sTPtUGKRk2ifHNALgas b3FhohzISHQBozg1bVyjH/LH/qcyH0XTGhHaN3CRIkcemUpLXoCOkCnI9oUNtEaCVjXe fphA== X-Forwarded-Encrypted: i=1; AJvYcCXnB7n9NPuwLZ0EEBVs7BtagKr4i4k0kGlupIV6yTf21VWmsxHYn7/79eIBwja9lqkrUtwBgnqkU2ZO@lists.postgresql.org X-Gm-Message-State: AOJu0YzW5m9MiIL8hyy3BIySDgfLyE1sn+Fa/NajLaY7osKnx2Zr5Cwt LL/AfBe0HAEiwaAMTtXt9ER1qfoiIuhZ4h+BIBLtgB0Cjv8adCjc X-Gm-Gg: ASbGncv4LMoheXjDgZtvpTY7NJgl4cE6Nzdj6WjQGz3a1tD/HsuZibNDrmc7bv88PU8 CxJiGkZ1/vQUErAAJdd+unhVoFIaJyrOJm9RUij8BZuVoSU+rE5gBAxKLLnDOQEDAsc1sS9cFh4 tWqtMvVcvsFhMSqTyeEYjieIaznRNH+CzgIuPFZiMRDw0kRGG6G0b8YSQ9KB0KDmJpDeRCPRWtX fyfiWAxaIhdGkGocafj0NTSgxLx2L5kXOWIfKp82fYTnEnI1PKwSyj2D1SUwEH12s5oX19+B9RO LL5gXZ8dZ85fi165W8R/z3eWk4tjfpKDGtU46rBR X-Google-Smtp-Source: AGHT+IHHrKn/BAKkApyqz1oiNhDXoE/mHwxZFO80tJYQ7szokat0PHptxR0wouB4TSeu04v3VySdQA== X-Received: by 2002:a05:620a:6508:b0:7b1:516c:8601 with SMTP id af79cd13be357-7b67c286d81mr773992485a.20.1732736065354; Wed, 27 Nov 2024 11:34:25 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d451b4a11dsm69098376d6.102.2024.11.27.11.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 11:34:24 -0800 (PST) Date: Wed, 27 Nov 2024 13:34:22 -0600 From: Nathan Bossart To: Tom Lane Cc: Bertrand Drouvot , Bruce Momjian , adam@labkey.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18711: Attempting a connection with a database name longer than 63 characters now fails Message-ID: References: <665338.1732306066@sss.pgh.pa.us> <1865227.1732723416@sss.pgh.pa.us> <1896106.1732731438@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="v5KPrh7Od05B3rEB" Content-Disposition: inline In-Reply-To: <1896106.1732731438@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --v5KPrh7Od05B3rEB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Nov 27, 2024 at 01:17:18PM -0500, Tom Lane wrote: > I have an idea that might salvage something from what we've just > been doing. The biggest single problem with the old behavior, > AIUI, was the possibility of sending back encoding-corrupt names > in connection failure reports. How about something like: > > * Don't truncate the names on sight (so we keep 562bee0fc1). > > * Truncate the names at NAMEDATALEN-1 just before catalog lookup > (this restores the old lookup behavior, solving the current report). > > * In lookup-failure messages, be sure to report the un-truncated > name (avoiding the bad-encoding problem). > > * Copy the correctly-truncated names from the catalog to any > persistent storage such as MyProcPort, so that those are used > for all post-connection purposes. One possible issue with truncating the names after-the-fact is that hooks like ClientAuthentication_hook will have already been called with the untruncated names. If it's just the connection failure reports we are worried about, then maybe we should save the "raw" names to MyProcPort before truncating and be sure to use those in the log messages. I attached a rough sketch of what I'm imagining. If we aren't thrilled about adding new fields to "struct Port" in a minor release, we could just do a straight revert on the back-branches and add that part to v18. -- nathan --v5KPrh7Od05B3rEB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="save_untruncated_names.patch" diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index 47e8c91606..d81c3eeba9 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -320,7 +320,7 @@ auth_failed(Port *port, int status, const char *logdetail) ereport(FATAL, (errcode(errcode_return), - errmsg(errstr, port->user_name), + errmsg(errstr, port->user_name_raw), logdetail ? errdetail_log("%s", logdetail) : 0)); /* doesn't return */ @@ -459,14 +459,14 @@ ClientAuthentication(Port *port) (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), /* translator: last %s describes encryption state */ errmsg("pg_hba.conf rejects replication connection for host \"%s\", user \"%s\", %s", - hostinfo, port->user_name, + hostinfo, port->user_name_raw, encryption_state))); else ereport(FATAL, (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), /* translator: last %s describes encryption state */ errmsg("pg_hba.conf rejects connection for host \"%s\", user \"%s\", database \"%s\", %s", - hostinfo, port->user_name, + hostinfo, port->user_name_raw, port->database_name, encryption_state))); break; @@ -526,7 +526,7 @@ ClientAuthentication(Port *port) (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), /* translator: last %s describes encryption state */ errmsg("no pg_hba.conf entry for replication connection from host \"%s\", user \"%s\", %s", - hostinfo, port->user_name, + hostinfo, port->user_name_raw, encryption_state), HOSTNAME_LOOKUP_DETAIL(port))); else @@ -534,7 +534,7 @@ ClientAuthentication(Port *port) (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), /* translator: last %s describes encryption state */ errmsg("no pg_hba.conf entry for host \"%s\", user \"%s\", database \"%s\", %s", - hostinfo, port->user_name, + hostinfo, port->user_name_raw, port->database_name, encryption_state), HOSTNAME_LOOKUP_DETAIL(port))); diff --git a/src/backend/tcop/backend_startup.c b/src/backend/tcop/backend_startup.c index 2a96c81f92..3b60559ae9 100644 --- a/src/backend/tcop/backend_startup.c +++ b/src/backend/tcop/backend_startup.c @@ -830,6 +830,22 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done) if (port->database_name == NULL || port->database_name[0] == '\0') port->database_name = pstrdup(port->user_name); + /* + * Prior to truncating, save the raw database and user names for log + * messages. + */ + port->database_name_raw = pstrdup(port->database_name); + port->user_name_raw = pstrdup(port->user_name); + + /* + * Truncate given database and user names to length of a Postgres name. + * This avoids lookup failures when overlength names are given. + */ + if (strlen(port->database_name) >= NAMEDATALEN) + port->database_name[NAMEDATALEN - 1] = '\0'; + if (strlen(port->user_name) >= NAMEDATALEN) + port->user_name[NAMEDATALEN - 1] = '\0'; + if (am_walsender) MyBackendType = B_WAL_SENDER; else diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 3b7b2ebec0..7e3740fa0f 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -798,7 +798,8 @@ InitializeSessionUserId(const char *rolename, Oid roleid, bool bypass_login_chec return; ereport(FATAL, (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), - errmsg("role \"%s\" does not exist", rolename))); + errmsg("role \"%s\" does not exist", + MyProcPort ? MyProcPort->user_name_raw : rolename))); } } else diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 5b657a3f13..4f949a8f2d 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -1008,7 +1008,8 @@ InitPostgres(const char *in_dbname, Oid dboid, if (!HeapTupleIsValid(tuple)) ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), - errmsg("database \"%s\" does not exist", in_dbname))); + errmsg("database \"%s\" does not exist", + MyProcPort ? MyProcPort->database_name_raw : in_dbname))); dbform = (Form_pg_database) GETSTRUCT(tuple); dboid = dbform->oid; } @@ -1072,7 +1073,8 @@ InitPostgres(const char *in_dbname, Oid dboid, if (in_dbname) ereport(FATAL, (errcode(ERRCODE_UNDEFINED_DATABASE), - errmsg("database \"%s\" does not exist", in_dbname), + errmsg("database \"%s\" does not exist", + MyProcPort ? MyProcPort->database_name_raw : in_dbname), errdetail("It seems to have just been dropped or renamed."))); else ereport(FATAL, diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h index 9109b2c334..9031cabfc7 100644 --- a/src/include/libpq/libpq-be.h +++ b/src/include/libpq/libpq-be.h @@ -232,6 +232,9 @@ typedef struct Port char *raw_buf; ssize_t raw_buf_consumed, raw_buf_remaining; + + char *database_name_raw; + char *user_name_raw; } Port; /* --v5KPrh7Od05B3rEB--