1997-12-04 01:34:01 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* crypt.c--
|
|
|
|
* Look into pg_user and check the encrypted password with the one
|
|
|
|
* passed in from the frontend.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef HAVE_CRYPT_H
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
|
|
|
|
1997-12-09 04:11:25 +01:00
|
|
|
#include "postgres.h"
|
1997-12-12 17:26:36 +01:00
|
|
|
#include "miscadmin.h"
|
1997-12-09 04:11:25 +01:00
|
|
|
#include "libpq/crypt.h"
|
|
|
|
#include "utils/nabstime.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "storage/fd.h"
|
1997-12-04 01:34:01 +01:00
|
|
|
|
|
|
|
char* crypt_getpwdfilename() {
|
|
|
|
|
|
|
|
static char* filename = NULL;
|
|
|
|
|
|
|
|
if (!filename) {
|
1997-12-11 18:03:52 +01:00
|
|
|
filename = (char*)palloc(strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2);
|
|
|
|
sprintf(filename, "%s/%s", DataDir, CRYPT_PWD_FILE);
|
1997-12-04 01:34:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static
|
|
|
|
FILE* crypt_openpwdfile() {
|
|
|
|
|
|
|
|
char* filename;
|
|
|
|
|
|
|
|
filename = crypt_getpwdfilename();
|
1997-12-09 04:11:25 +01:00
|
|
|
return (AllocateFile(filename, "r"));
|
1997-12-04 01:34:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static
|
|
|
|
void crypt_parsepwdfile(FILE* datafile, char** login, char** pwd, char** valdate) {
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
char* parse;
|
|
|
|
int count,
|
|
|
|
i;
|
|
|
|
|
|
|
|
fgets(buffer, 256, datafile);
|
|
|
|
parse = buffer;
|
|
|
|
|
|
|
|
/* store a copy of user login to return
|
|
|
|
*/
|
|
|
|
count = strcspn(parse, "#");
|
1997-12-09 04:11:25 +01:00
|
|
|
*login = (char*)palloc(count + 1);
|
1997-12-04 01:34:01 +01:00
|
|
|
strncpy(*login, parse, count);
|
|
|
|
(*login)[count] = '\0';
|
|
|
|
parse += (count + 1);
|
|
|
|
|
|
|
|
/* skip to the password field
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
parse += (strcspn(parse, "#") + 1);
|
|
|
|
|
|
|
|
/* store a copy of user password to return
|
|
|
|
*/
|
|
|
|
count = strcspn(parse, "#");
|
1997-12-09 04:11:25 +01:00
|
|
|
*pwd = (char*)palloc(count + 1);
|
1997-12-04 01:34:01 +01:00
|
|
|
strncpy(*pwd, parse, count);
|
|
|
|
(*pwd)[count] = '\0';
|
|
|
|
parse += (count + 1);
|
|
|
|
|
|
|
|
/* store a copy of date login becomes invalid
|
|
|
|
*/
|
|
|
|
count = strcspn(parse, "#");
|
1997-12-09 04:11:25 +01:00
|
|
|
*valdate = (char*)palloc(count + 1);
|
1997-12-04 01:34:01 +01:00
|
|
|
strncpy(*valdate, parse, count);
|
|
|
|
(*valdate)[count] = '\0';
|
|
|
|
parse += (count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static
|
|
|
|
void crypt_getloginfo(const char* user, char** passwd, char** valuntil) {
|
|
|
|
|
|
|
|
FILE* datafile;
|
|
|
|
char* login;
|
|
|
|
char* pwd;
|
|
|
|
char* valdate;
|
|
|
|
|
|
|
|
*passwd = NULL;
|
|
|
|
*valuntil = NULL;
|
|
|
|
|
|
|
|
if (!(datafile = crypt_openpwdfile()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (!feof(datafile)) {
|
|
|
|
crypt_parsepwdfile(datafile, &login, &pwd, &valdate);
|
|
|
|
if (!strcmp(login, user)) {
|
1997-12-09 04:11:25 +01:00
|
|
|
pfree((void*)login);
|
1997-12-04 01:34:01 +01:00
|
|
|
*passwd = pwd;
|
|
|
|
*valuntil = valdate;
|
|
|
|
fclose(datafile);
|
|
|
|
return;
|
|
|
|
}
|
1997-12-09 04:11:25 +01:00
|
|
|
pfree((void*)login);
|
|
|
|
pfree((void*)pwd);
|
|
|
|
pfree((void*)valdate);
|
1997-12-04 01:34:01 +01:00
|
|
|
}
|
|
|
|
fclose(datafile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
MsgType crypt_salt(const char* user) {
|
|
|
|
|
|
|
|
char* passwd;
|
|
|
|
char* valuntil;
|
|
|
|
|
|
|
|
crypt_getloginfo(user, &passwd, &valuntil);
|
|
|
|
|
1997-12-12 17:26:36 +01:00
|
|
|
if (passwd == NULL || *passwd == '\0' || !strcmp(passwd, "\\N")) {
|
1997-12-09 04:11:25 +01:00
|
|
|
if (passwd) pfree((void*)passwd);
|
|
|
|
if (valuntil) pfree((void*)valuntil);
|
1997-12-04 01:34:01 +01:00
|
|
|
return STARTUP_UNSALT_MSG;
|
|
|
|
}
|
|
|
|
|
1997-12-09 04:11:25 +01:00
|
|
|
pfree((void*)passwd);
|
|
|
|
if (valuntil) pfree((void*)valuntil);
|
1997-12-04 01:34:01 +01:00
|
|
|
return STARTUP_SALT_MSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
int crypt_verify(Port* port, const char* user, const char* pgpass) {
|
|
|
|
|
|
|
|
char* passwd;
|
|
|
|
char* valuntil;
|
|
|
|
char* crypt_pwd;
|
|
|
|
int retval = STATUS_ERROR;
|
|
|
|
AbsoluteTime vuntil,
|
|
|
|
current;
|
|
|
|
|
|
|
|
crypt_getloginfo(user, &passwd, &valuntil);
|
|
|
|
|
|
|
|
if (passwd == NULL || *passwd == '\0') {
|
1997-12-09 04:11:25 +01:00
|
|
|
if (passwd) pfree((void*)passwd);
|
|
|
|
if (valuntil) pfree((void*)valuntil);
|
1997-12-04 01:34:01 +01:00
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
crypt_pwd = crypt(passwd, port->salt);
|
|
|
|
if (!strcmp(pgpass, crypt_pwd)) {
|
|
|
|
/* check here to be sure we are not past valuntil
|
|
|
|
*/
|
|
|
|
if (!valuntil)
|
|
|
|
vuntil = INVALID_ABSTIME;
|
|
|
|
else
|
|
|
|
vuntil = nabstimein(valuntil);
|
|
|
|
current = GetCurrentAbsoluteTime();
|
|
|
|
if (vuntil != INVALID_ABSTIME && vuntil < current)
|
|
|
|
retval = STATUS_ERROR;
|
|
|
|
else
|
|
|
|
retval = STATUS_OK;
|
|
|
|
}
|
|
|
|
|
1997-12-09 04:11:25 +01:00
|
|
|
pfree((void*)passwd);
|
|
|
|
if (valuntil) pfree((void*)valuntil);
|
1997-12-04 01:34:01 +01:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|