Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea9b028

Browse files
committed
Add an officially exported libpq function to encrypt passwords, and
modify the previous \password patch to use it instead of dependingon a not-officially-exported function. Per discussion.
1 parente80f9df commitea9b028

File tree

6 files changed

+93
-14
lines changed

6 files changed

+93
-14
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.199 2005/11/04 23:14:00 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.200 2005/12/23 01:16:37 tgl Exp $
33
-->
44

55
<chapter id="libpq">
@@ -3565,6 +3565,40 @@ void PQuntrace(PGconn *conn);
35653565

35663566
</sect1>
35673567

3568+
<sect1 id="libpq-misc">
3569+
<title>Miscellaneous Functions</title>
3570+
3571+
<para>
3572+
As always, there are some functions that just don't fit anywhere.
3573+
</para>
3574+
3575+
<variablelist>
3576+
<varlistentry>
3577+
<term><function>pg_make_encrypted_password</function><indexterm><primary>pg_make_encrypted_password</></></term>
3578+
<listitem>
3579+
<para>
3580+
Prepares the encrypted form of a <productname>PostgreSQL</> password.
3581+
<synopsis>
3582+
char *pg_make_encrypted_password(const char *passwd, const char *user);
3583+
</synopsis>
3584+
<function>pg_make_encrypted_password</> is intended to be used by client
3585+
applications that wish to send commands like
3586+
<literal>ALTER USER joe PASSWORD 'pwd'</>.
3587+
It is good practice not to send the original cleartext password in such a
3588+
command, because it might be exposed in command logs, activity displays,
3589+
and so on. Instead, use this function to convert the password to encrypted
3590+
form before it is sent. The arguments are the cleartext password, and the SQL
3591+
name of the user it is for. The return value is a malloc'd string, or NULL if
3592+
out-of-memory. The caller may assume the string doesn't contain any weird
3593+
characters that would require escaping. Use <function>PQfreemem</> to free
3594+
the result when done with it.
3595+
</para>
3596+
</listitem>
3597+
</varlistentry>
3598+
</variablelist>
3599+
3600+
</sect1>
3601+
35683602
<sect1 id="libpq-notice-processing">
35693603
<title>Notice Processing</title>
35703604

‎src/bin/psql/command.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.156 2005/12/18 02:17:16 petere Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.157 2005/12/23 01:16:38 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"command.h"
@@ -12,7 +12,6 @@
1212
#undef mkdir
1313
#endif
1414

15-
#include<errno.h>
1615
#include<ctype.h>
1716
#ifdefHAVE_PWD_H
1817
#include<pwd.h>
@@ -35,7 +34,6 @@
3534

3635
#include"libpq-fe.h"
3736
#include"pqexpbuffer.h"
38-
#include"libpq/crypt.h"
3937
#include"dumputils.h"
4038

4139
#include"common.h"
@@ -638,14 +636,16 @@ exec_command(const char *cmd,
638636
{
639637
char*opt0=psql_scan_slash_option(scan_state,OT_SQLID,NULL, true);
640638
char*user;
641-
charencrypted_password[MD5_PASSWD_LEN+1];
639+
char*encrypted_password;
642640

643641
if (opt0)
644642
user=opt0;
645643
else
646644
user=PQuser(pset.db);
647645

648-
if (!pg_md5_encrypt(pw1,user,strlen(user),encrypted_password))
646+
encrypted_password=pg_make_encrypted_password(pw1,user);
647+
648+
if (!encrypted_password)
649649
{
650650
fprintf(stderr,_("Password encryption failed.\n"));
651651
success= false;
@@ -656,14 +656,15 @@ exec_command(const char *cmd,
656656
PGresult*res;
657657

658658
initPQExpBuffer(&buf);
659-
printfPQExpBuffer(&buf,"ALTERROLE %s PASSWORD '%s';",
659+
printfPQExpBuffer(&buf,"ALTERUSER %s PASSWORD '%s';",
660660
fmtId(user),encrypted_password);
661661
res=PSQLexec(buf.data, false);
662662
termPQExpBuffer(&buf);
663663
if (!res)
664664
success= false;
665665
else
666666
PQclear(res);
667+
PQfreemem(encrypted_password);
667668
}
668669
}
669670

‎src/bin/scripts/createuser.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
8-
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.24 2005/12/18 02:17:16 petere Exp $
8+
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.25 2005/12/23 01:16:38 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
1212

1313
#include"postgres_fe.h"
1414
#include"common.h"
1515
#include"dumputils.h"
16-
#include"libpq/crypt.h"
1716

1817

1918
staticvoidhelp(constchar*progname);
@@ -250,14 +249,17 @@ main(int argc, char *argv[])
250249

251250
if (encrypted!=TRI_NO)
252251
{
253-
charencrypted_password[MD5_PASSWD_LEN+1];
252+
char*encrypted_password;
254253

255-
if (!pg_md5_encrypt(newpassword,newuser,strlen(newuser),encrypted_password))
254+
encrypted_password=pg_make_encrypted_password(newpassword,
255+
newuser);
256+
if (!encrypted_password)
256257
{
257258
fprintf(stderr,_("Password encryption failed.\n"));
258259
exit(1);
259260
}
260261
appendStringLiteral(&sql,encrypted_password, false);
262+
PQfreemem(encrypted_password);
261263
}
262264
else
263265
appendStringLiteral(&sql,newpassword, false);

‎src/interfaces/libpq/exports.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.5 2005/10/21 15:21:21 tgl Exp $
1+
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.6 2005/12/23 01:16:38 tgl Exp $
22
# Functions to be exported by libpq DLLs
33
PQconnectdb 1
44
PQsetdbLogin 2
@@ -125,3 +125,4 @@ PQcancel 122
125125
lo_create 123
126126
PQinitSSL 124
127127
PQregisterThreadLock 125
128+
pg_make_encrypted_password 126

‎src/interfaces/libpq/fe-auth.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.108 2005/11/22 18:17:32 momjian Exp $
13+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.109 2005/12/23 01:16:38 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -531,3 +531,40 @@ pg_fe_getauthname(char *PQerrormsg)
531531

532532
returnauthn;
533533
}
534+
535+
536+
/*
537+
* pg_make_encrypted_password -- exported routine to encrypt a password
538+
*
539+
* This is intended to be used by client applications that wish to send
540+
* commands like ALTER USER joe PASSWORD 'pwd'. The password need not
541+
* be sent in cleartext if it is encrypted on the client side. This is
542+
* good because it ensures the cleartext password won't end up in logs,
543+
* pg_stat displays, etc. We export the function so that clients won't
544+
* be dependent on low-level details like whether the enceyption is MD5
545+
* or something else.
546+
*
547+
* Arguments are the cleartext password, and the SQL name of the user it
548+
* is for.
549+
*
550+
* Return value is a malloc'd string, or NULL if out-of-memory. The client
551+
* may assume the string doesn't contain any weird characters that would
552+
* require escaping.
553+
*/
554+
char*
555+
pg_make_encrypted_password(constchar*passwd,constchar*user)
556+
{
557+
char*crypt_pwd;
558+
559+
crypt_pwd=malloc(MD5_PASSWD_LEN+1);
560+
if (!crypt_pwd)
561+
returnNULL;
562+
563+
if (!pg_md5_encrypt(passwd,user,strlen(user),crypt_pwd))
564+
{
565+
free(crypt_pwd);
566+
returnNULL;
567+
}
568+
569+
returncrypt_pwd;
570+
}

‎src/interfaces/libpq/libpq-fe.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.122 2005/11/2304:23:28 momjian Exp $
10+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.123 2005/12/2301:16:38 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -495,6 +495,10 @@ extern intPQdsplen(const char *s, int encoding);
495495
/* Get encoding id from environment variable PGCLIENTENCODING */
496496
externintPQenv2encoding(void);
497497

498+
/* === in fe-auth.c === */
499+
500+
externchar*pg_make_encrypted_password(constchar*passwd,constchar*user);
501+
498502
#ifdef__cplusplus
499503
}
500504
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp