1
/*-------------------------------------------------------------------------
4
* The front-end (client) authorization routines
6
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
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/types.h>
31
#include <sys/param.h> /* for MAXHOSTNAMELEN on most */
32
#include <sys/socket.h>
33
#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || defined(HAVE_STRUCT_SOCKCRED)
35
#include <sys/ucred.h>
37
#ifndef MAXHOSTNAMELEN
38
#include <netdb.h> /* for MAXHOSTNAMELEN on some */
45
#include "libpq/md5.h"
50
* MIT Kerberos authentication system - protocol version 5
54
/* Some old versions of Kerberos do not include <com_err.h> in <krb5.h> */
55
#if !defined(__COM_ERR_H) && !defined(__COM_ERR_H__)
60
* Heimdal doesn't have a free function for unparsed names. Just pass it to
61
* standard free() which should work in these cases.
63
#ifndef HAVE_KRB5_FREE_UNPARSED_NAME
65
krb5_free_unparsed_name(krb5_context context, char *val)
72
* pg_an_to_ln -- return the local name corresponding to an authentication
75
* XXX Assumes that the first aname component is the user name. This is NOT
76
* necessarily so, since an aname can actually be something out of your
77
* worst X.400 nightmare, like
78
* ORGANIZATION=U. C. Berkeley/NAME=Paul M. Aoki@CS.BERKELEY.EDU
79
* Note that the MIT an_to_ln code does the same thing if you don't
80
* provide an aname mapping database...it may be a better idea to use
81
* krb5_an_to_ln, except that it punts if multiple components are found,
82
* and we can't afford to punt.
84
* For WIN32, convert username to lowercase because the Win32 kerberos library
85
* generates tickets with the username as the user entered it instead of as
86
* it is entered in the directory.
89
pg_an_to_ln(char *aname)
93
if ((p = strchr(aname, '/')) || (p = strchr(aname, '@')))
96
for (p = aname; *p; p++)
97
*p = pg_tolower((unsigned char) *p);
105
* Various krb5 state which is not connection specific, and a flag to
106
* indicate whether we have initialised it yet.
109
static int pg_krb5_initialised;
110
static krb5_context pg_krb5_context;
111
static krb5_ccache pg_krb5_ccache;
112
static krb5_principal pg_krb5_client;
113
static char *pg_krb5_name;
118
int pg_krb5_initialised;
119
krb5_context pg_krb5_context;
120
krb5_ccache pg_krb5_ccache;
121
krb5_principal pg_krb5_client;
127
pg_krb5_init(PQExpBuffer errorMessage, struct krb5_info * info)
129
krb5_error_code retval;
131
if (info->pg_krb5_initialised)
134
retval = krb5_init_context(&(info->pg_krb5_context));
137
printfPQExpBuffer(errorMessage,
138
"pg_krb5_init: krb5_init_context: %s\n",
139
error_message(retval));
143
retval = krb5_cc_default(info->pg_krb5_context, &(info->pg_krb5_ccache));
146
printfPQExpBuffer(errorMessage,
147
"pg_krb5_init: krb5_cc_default: %s\n",
148
error_message(retval));
149
krb5_free_context(info->pg_krb5_context);
153
retval = krb5_cc_get_principal(info->pg_krb5_context, info->pg_krb5_ccache,
154
&(info->pg_krb5_client));
157
printfPQExpBuffer(errorMessage,
158
"pg_krb5_init: krb5_cc_get_principal: %s\n",
159
error_message(retval));
160
krb5_cc_close(info->pg_krb5_context, info->pg_krb5_ccache);
161
krb5_free_context(info->pg_krb5_context);
165
retval = krb5_unparse_name(info->pg_krb5_context, info->pg_krb5_client, &(info->pg_krb5_name));
168
printfPQExpBuffer(errorMessage,
169
"pg_krb5_init: krb5_unparse_name: %s\n",
170
error_message(retval));
171
krb5_free_principal(info->pg_krb5_context, info->pg_krb5_client);
172
krb5_cc_close(info->pg_krb5_context, info->pg_krb5_ccache);
173
krb5_free_context(info->pg_krb5_context);
177
info->pg_krb5_name = pg_an_to_ln(info->pg_krb5_name);
179
info->pg_krb5_initialised = 1;
184
pg_krb5_destroy(struct krb5_info * info)
186
krb5_free_principal(info->pg_krb5_context, info->pg_krb5_client);
187
krb5_cc_close(info->pg_krb5_context, info->pg_krb5_ccache);
188
krb5_free_unparsed_name(info->pg_krb5_context, info->pg_krb5_name);
189
krb5_free_context(info->pg_krb5_context);
194
* pg_krb5_sendauth -- client routine to send authentication information to
198
pg_krb5_sendauth(PGconn *conn)
200
krb5_error_code retval;
202
krb5_principal server;
203
krb5_auth_context auth_context = NULL;
204
krb5_error *err_ret = NULL;
205
struct krb5_info info;
207
info.pg_krb5_initialised = 0;
211
printfPQExpBuffer(&conn->errorMessage,
212
"pg_krb5_sendauth: hostname must be specified for Kerberos authentication\n");
216
ret = pg_krb5_init(&conn->errorMessage, &info);
217
if (ret != STATUS_OK)
220
retval = krb5_sname_to_principal(info.pg_krb5_context, conn->pghost,
222
KRB5_NT_SRV_HST, &server);
225
printfPQExpBuffer(&conn->errorMessage,
226
"pg_krb5_sendauth: krb5_sname_to_principal: %s\n",
227
error_message(retval));
228
pg_krb5_destroy(&info);
233
* libpq uses a non-blocking socket. But kerberos needs a blocking socket,
234
* and we have to block somehow to do mutual authentication anyway. So we
235
* temporarily make it blocking.
237
if (!pg_set_block(conn->sock))
241
printfPQExpBuffer(&conn->errorMessage,
242
libpq_gettext("could not set socket to blocking mode: %s\n"), pqStrerror(errno, sebuf, sizeof(sebuf)));
243
krb5_free_principal(info.pg_krb5_context, server);
244
pg_krb5_destroy(&info);
248
retval = krb5_sendauth(info.pg_krb5_context, &auth_context,
249
(krb5_pointer) & conn->sock, (char *) conn->krbsrvname,
250
info.pg_krb5_client, server,
251
AP_OPTS_MUTUAL_REQUIRED,
252
NULL, 0, /* no creds, use ccache instead */
253
info.pg_krb5_ccache, &err_ret, NULL, NULL);
256
if (retval == KRB5_SENDAUTH_REJECTED && err_ret)
258
#if defined(HAVE_KRB5_ERROR_TEXT_DATA)
259
printfPQExpBuffer(&conn->errorMessage,
260
libpq_gettext("Kerberos 5 authentication rejected: %*s\n"),
261
(int) err_ret->text.length, err_ret->text.data);
262
#elif defined(HAVE_KRB5_ERROR_E_DATA)
263
printfPQExpBuffer(&conn->errorMessage,
264
libpq_gettext("Kerberos 5 authentication rejected: %*s\n"),
265
(int) err_ret->e_data->length,
266
(const char *) err_ret->e_data->data);
268
#error "bogus configuration"
273
printfPQExpBuffer(&conn->errorMessage,
274
"krb5_sendauth: %s\n", error_message(retval));
278
krb5_free_error(info.pg_krb5_context, err_ret);
283
krb5_free_principal(info.pg_krb5_context, server);
285
if (!pg_set_noblock(conn->sock))
289
printfPQExpBuffer(&conn->errorMessage,
290
libpq_gettext("could not restore non-blocking mode on socket: %s\n"),
291
pqStrerror(errno, sebuf, sizeof(sebuf)));
294
pg_krb5_destroy(&info);
302
* GSSAPI authentication system.
305
#if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
307
* MIT Kerberos GSSAPI DLL doesn't properly export the symbols for MingW
308
* that contain the OIDs required. Redefine here, values copied
309
* from src/athena/auth/krb5/src/lib/gssapi/generic/gssapi_generic.c
311
static const gss_OID_desc GSS_C_NT_HOSTBASED_SERVICE_desc =
312
{10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
313
static GSS_DLLIMP gss_OID GSS_C_NT_HOSTBASED_SERVICE = &GSS_C_NT_HOSTBASED_SERVICE_desc;
317
* Fetch all errors of a specific type and append to "str".
320
pg_GSS_error_int(PQExpBuffer str, const char *mprefix,
321
OM_uint32 stat, int type)
325
gss_buffer_desc lmsg;
326
OM_uint32 msg_ctx = 0;
330
lmaj_s = gss_display_status(&lmin_s, stat, type,
331
GSS_C_NO_OID, &msg_ctx, &lmsg);
332
appendPQExpBuffer(str, "%s: %s\n", mprefix, (char *) lmsg.value);
333
gss_release_buffer(&lmin_s, &lmsg);
338
* GSSAPI errors contain two parts; put both into conn->errorMessage.
341
pg_GSS_error(const char *mprefix, PGconn *conn,
342
OM_uint32 maj_stat, OM_uint32 min_stat)
344
resetPQExpBuffer(&conn->errorMessage);
346
/* Fetch major error codes */
347
pg_GSS_error_int(&conn->errorMessage, mprefix, maj_stat, GSS_C_GSS_CODE);
349
/* Add the minor codes as well */
350
pg_GSS_error_int(&conn->errorMessage, mprefix, min_stat, GSS_C_MECH_CODE);
354
* Continue GSS authentication with next token as needed.
357
pg_GSS_continue(PGconn *conn)
363
maj_stat = gss_init_sec_context(&min_stat,
370
GSS_C_NO_CHANNEL_BINDINGS,
371
(conn->gctx == GSS_C_NO_CONTEXT) ? GSS_C_NO_BUFFER : &conn->ginbuf,
377
if (conn->gctx != GSS_C_NO_CONTEXT)
379
free(conn->ginbuf.value);
380
conn->ginbuf.value = NULL;
381
conn->ginbuf.length = 0;
384
if (conn->goutbuf.length != 0)
387
* GSS generated data to send to the server. We don't care if it's the
388
* first or subsequent packet, just send the same kind of password
391
if (pqPacketSend(conn, 'p',
392
conn->goutbuf.value, conn->goutbuf.length)
395
gss_release_buffer(&lmin_s, &conn->goutbuf);
399
gss_release_buffer(&lmin_s, &conn->goutbuf);
401
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
403
pg_GSS_error(libpq_gettext("GSSAPI continuation error"),
406
gss_release_name(&lmin_s, &conn->gtarg_nam);
408
gss_delete_sec_context(&lmin_s, &conn->gctx, GSS_C_NO_BUFFER);
412
if (maj_stat == GSS_S_COMPLETE)
413
gss_release_name(&lmin_s, &conn->gtarg_nam);
419
* Send initial GSS authentication token
422
pg_GSS_startup(PGconn *conn)
427
gss_buffer_desc temp_gbuf;
431
printfPQExpBuffer(&conn->errorMessage,
432
libpq_gettext("duplicate GSS authentication request\n"));
437
* Import service principal name so the proper ticket can be acquired by
440
maxlen = NI_MAXHOST + strlen(conn->krbsrvname) + 2;
441
temp_gbuf.value = (char *) malloc(maxlen);
442
snprintf(temp_gbuf.value, maxlen, "%s@%s",
443
conn->krbsrvname, conn->pghost);
444
temp_gbuf.length = strlen(temp_gbuf.value);
446
maj_stat = gss_import_name(&min_stat, &temp_gbuf,
447
GSS_C_NT_HOSTBASED_SERVICE, &conn->gtarg_nam);
448
free(temp_gbuf.value);
450
if (maj_stat != GSS_S_COMPLETE)
452
pg_GSS_error(libpq_gettext("GSSAPI name import error"),
459
* Initial packet is the same as a continuation packet with no initial
462
conn->gctx = GSS_C_NO_CONTEXT;
464
return pg_GSS_continue(conn);
466
#endif /* ENABLE_GSS */
471
* SSPI authentication system (Windows only)
475
pg_SSPI_error(PGconn *conn, const char *mprefix, SECURITY_STATUS r)
479
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, r, 0,
480
sysmsg, sizeof(sysmsg), NULL) == 0)
481
printfPQExpBuffer(&conn->errorMessage, "%s: SSPI error %x",
482
mprefix, (unsigned int) r);
484
printfPQExpBuffer(&conn->errorMessage, "%s: %s (%x)",
485
mprefix, sysmsg, (unsigned int) r);
489
* Continue SSPI authentication with next token as needed.
492
pg_SSPI_continue(PGconn *conn)
495
CtxtHandle newContext;
498
SecBufferDesc outbuf;
499
SecBuffer OutBuffers[1];
500
SecBuffer InBuffers[1];
502
if (conn->sspictx != NULL)
505
* On runs other than the first we have some data to send. Put this
506
* data in a SecBuffer type structure.
508
inbuf.ulVersion = SECBUFFER_VERSION;
510
inbuf.pBuffers = InBuffers;
511
InBuffers[0].pvBuffer = conn->ginbuf.value;
512
InBuffers[0].cbBuffer = conn->ginbuf.length;
513
InBuffers[0].BufferType = SECBUFFER_TOKEN;
516
OutBuffers[0].pvBuffer = NULL;
517
OutBuffers[0].BufferType = SECBUFFER_TOKEN;
518
OutBuffers[0].cbBuffer = 0;
520
outbuf.pBuffers = OutBuffers;
521
outbuf.ulVersion = SECBUFFER_VERSION;
523
r = InitializeSecurityContext(conn->sspicred,
526
ISC_REQ_ALLOCATE_MEMORY,
528
SECURITY_NETWORK_DREP,
529
(conn->sspictx == NULL) ? NULL : &inbuf,
536
if (r != SEC_E_OK && r != SEC_I_CONTINUE_NEEDED)
538
pg_SSPI_error(conn, libpq_gettext("SSPI continuation error"), r);
543
if (conn->sspictx == NULL)
545
/* On first run, transfer retreived context handle */
546
conn->sspictx = malloc(sizeof(CtxtHandle));
547
if (conn->sspictx == NULL)
549
printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
552
memcpy(conn->sspictx, &newContext, sizeof(CtxtHandle));
557
* On subsequent runs when we had data to send, free buffers that
558
* contained this data.
560
free(conn->ginbuf.value);
561
conn->ginbuf.value = NULL;
562
conn->ginbuf.length = 0;
566
* If SSPI returned any data to be sent to the server (as it normally
567
* would), send this data as a password packet.
569
if (outbuf.cBuffers > 0)
571
if (outbuf.cBuffers != 1)
574
* This should never happen, at least not for Kerberos
575
* authentication. Keep check in case it shows up with other
576
* authentication methods later.
578
printfPQExpBuffer(&conn->errorMessage, "SSPI returned invalid number of output buffers\n");
583
* If the negotiation is complete, there may be zero bytes to send. The server is
584
* at this point not expecting any more data, so don't send it.
586
if (outbuf.pBuffers[0].cbBuffer > 0)
588
if (pqPacketSend(conn, 'p',
589
outbuf.pBuffers[0].pvBuffer, outbuf.pBuffers[0].cbBuffer))
591
FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
595
FreeContextBuffer(outbuf.pBuffers[0].pvBuffer);
598
/* Cleanup is handled by the code in freePGconn() */
603
* Send initial SSPI authentication token.
604
* If use_negotiate is 0, use kerberos authentication package which is
605
* compatible with Unix. If use_negotiate is 1, use the negotiate package
606
* which supports both kerberos and NTLM, but is not compatible with Unix.
609
pg_SSPI_startup(PGconn *conn, int use_negotiate)
614
conn->sspictx = NULL;
617
* Retreive credentials handle
619
conn->sspicred = malloc(sizeof(CredHandle));
620
if (conn->sspicred == NULL)
622
printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
626
r = AcquireCredentialsHandle(NULL,
627
use_negotiate ? "negotiate" : "kerberos",
628
SECPKG_CRED_OUTBOUND,
637
pg_SSPI_error(conn, libpq_gettext("could not acquire SSPI credentials"), r);
638
free(conn->sspicred);
639
conn->sspicred = NULL;
644
* Compute target principal name. SSPI has a different format from GSSAPI,
645
* but not more complex. We can skip the @REALM part, because Windows will
646
* fill that in for us automatically.
648
if (conn->pghost == NULL)
650
printfPQExpBuffer(&conn->errorMessage, libpq_gettext("host name must be specified\n"));
653
conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(conn->pghost) + 2);
654
if (!conn->sspitarget)
656
printfPQExpBuffer(&conn->errorMessage, libpq_gettext("out of memory\n"));
659
sprintf(conn->sspitarget, "%s/%s", conn->krbsrvname, conn->pghost);
662
* Indicate that we're in SSPI authentication mode to make sure that
663
* pg_SSPI_continue is called next time in the negotiation.
667
return pg_SSPI_continue(conn);
669
#endif /* ENABLE_SSPI */
672
* Respond to AUTH_REQ_SCM_CREDS challenge.
674
* Note: current backends will not use this challenge if HAVE_GETPEEREID
675
* or SO_PEERCRED is defined, but pre-7.4 backends might, so compile the
679
pg_local_sendauth(PGconn *conn)
681
#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || \
682
(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
687
#ifdef HAVE_STRUCT_CMSGCRED
688
/* Prevent padding */
689
char cmsgmem[sizeof(struct cmsghdr) + sizeof(struct cmsgcred)];
691
/* Point to start of first structure */
692
struct cmsghdr *cmsg = (struct cmsghdr *) cmsgmem;
696
* The backend doesn't care what we send here, but it wants exactly one
697
* character to force recvmsg() to block and wait for us.
703
memset(&msg, 0, sizeof(msg));
707
#ifdef HAVE_STRUCT_CMSGCRED
708
/* Create control header, FreeBSD */
709
msg.msg_control = cmsg;
710
msg.msg_controllen = sizeof(cmsgmem);
711
memset(cmsg, 0, sizeof(cmsgmem));
712
cmsg->cmsg_len = sizeof(cmsgmem);
713
cmsg->cmsg_level = SOL_SOCKET;
714
cmsg->cmsg_type = SCM_CREDS;
717
if (sendmsg(conn->sock, &msg, 0) == -1)
721
printfPQExpBuffer(&conn->errorMessage,
722
"pg_local_sendauth: sendmsg: %s\n",
723
pqStrerror(errno, sebuf, sizeof(sebuf)));
728
printfPQExpBuffer(&conn->errorMessage,
729
libpq_gettext("SCM_CRED authentication method not supported\n"));
735
pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
740
/* Encrypt the password if needed. */
748
/* Allocate enough space for two MD5 hashes */
749
crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
752
printfPQExpBuffer(&conn->errorMessage,
753
libpq_gettext("out of memory\n"));
757
crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
758
if (!pg_md5_encrypt(password, conn->pguser,
759
strlen(conn->pguser), crypt_pwd2))
764
if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), conn->md5Salt,
765
sizeof(conn->md5Salt), crypt_pwd))
772
case AUTH_REQ_PASSWORD:
773
/* discard const so we can assign it */
774
crypt_pwd = (char *) password;
779
/* Packet has a message type as of protocol 3.0 */
780
if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
781
ret = pqPacketSend(conn, 'p', crypt_pwd, strlen(crypt_pwd) + 1);
783
ret = pqPacketSend(conn, 0, crypt_pwd, strlen(crypt_pwd) + 1);
784
if (areq == AUTH_REQ_MD5)
791
* client demux routine for outgoing authentication information
794
pg_fe_sendauth(AuthRequest areq, PGconn *conn)
802
printfPQExpBuffer(&conn->errorMessage,
803
libpq_gettext("Kerberos 4 authentication not supported\n"));
809
if (pg_krb5_sendauth(conn) != STATUS_OK)
811
/* Error message already filled in */
818
printfPQExpBuffer(&conn->errorMessage,
819
libpq_gettext("Kerberos 5 authentication not supported\n"));
823
#if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
831
* If we have both GSS and SSPI support compiled in, use SSPI
832
* support by default. This is overridable by a connection
833
* string parameter. Note that when using SSPI we still leave
834
* the negotiate parameter off, since we want SSPI to use the
835
* GSSAPI kerberos protocol. For actual SSPI negotiate
836
* protocol, we use AUTH_REQ_SSPI.
838
#if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
839
if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
840
r = pg_GSS_startup(conn);
842
r = pg_SSPI_startup(conn, 0);
843
#elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
844
r = pg_GSS_startup(conn);
845
#elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
846
r = pg_SSPI_startup(conn, 0);
850
/* Error message already filled in. */
858
case AUTH_REQ_GSS_CONT:
863
#if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
865
r = pg_SSPI_continue(conn);
867
r = pg_GSS_continue(conn);
868
#elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
869
r = pg_GSS_continue(conn);
870
#elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
871
r = pg_SSPI_continue(conn);
875
/* Error message already filled in. */
884
case AUTH_REQ_GSS_CONT:
885
printfPQExpBuffer(&conn->errorMessage,
886
libpq_gettext("GSSAPI authentication not supported\n"));
894
* SSPI has it's own startup message so libpq can decide which
895
* method to use. Indicate to pg_SSPI_startup that we want SSPI
896
* negotiation instead of Kerberos.
899
if (pg_SSPI_startup(conn, 1) != STATUS_OK)
901
/* Error message already filled in. */
909
printfPQExpBuffer(&conn->errorMessage,
910
libpq_gettext("SSPI authentication not supported\n"));
916
printfPQExpBuffer(&conn->errorMessage,
917
libpq_gettext("Crypt authentication not supported\n"));
921
case AUTH_REQ_PASSWORD:
922
conn->password_needed = true;
923
if (conn->pgpass == NULL || conn->pgpass[0] == '\0')
925
printfPQExpBuffer(&conn->errorMessage,
926
PQnoPasswordSupplied);
929
if (pg_password_sendauth(conn, conn->pgpass, areq) != STATUS_OK)
931
printfPQExpBuffer(&conn->errorMessage,
932
"fe_sendauth: error sending password authentication\n");
937
case AUTH_REQ_SCM_CREDS:
938
if (pg_local_sendauth(conn) != STATUS_OK)
943
printfPQExpBuffer(&conn->errorMessage,
944
libpq_gettext("authentication method %u not supported\n"), areq);
953
* pg_fe_getauthname -- returns a pointer to dynamic space containing whatever
954
* name the user has authenticated to the system
956
* if there is an error, return NULL with an error message in errorMessage
959
pg_fe_getauthname(PQExpBuffer errorMessage)
961
const char *name = NULL;
966
DWORD namesize = sizeof(username) - 1;
969
struct passwd pwdstr;
970
struct passwd *pw = NULL;
974
* Some users are using configure
975
* --enable-thread-safety-force, so we might as well do the locking within
976
* our library to protect pqGetpwuid(). In fact, application developers
977
* can use getpwuid() in their application if they use the locking call we
978
* provide, or install their own locking function using
979
* PQregisterThreadLock().
986
if (GetUserName(username, &namesize))
989
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pw) == 0)
994
authn = name ? strdup(name) : NULL;
1003
* PQencryptPassword -- exported routine to encrypt a password
1005
* This is intended to be used by client applications that wish to send
1006
* commands like ALTER USER joe PASSWORD 'pwd'. The password need not
1007
* be sent in cleartext if it is encrypted on the client side. This is
1008
* good because it ensures the cleartext password won't end up in logs,
1009
* pg_stat displays, etc. We export the function so that clients won't
1010
* be dependent on low-level details like whether the enceyption is MD5
1011
* or something else.
1013
* Arguments are the cleartext password, and the SQL name of the user it
1016
* Return value is a malloc'd string, or NULL if out-of-memory. The client
1017
* may assume the string doesn't contain any special characters that would
1021
PQencryptPassword(const char *passwd, const char *user)
1025
crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1029
if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))