Whitespace and indentation changes.

Also changed line-endings from DOS CRLF to only LF.
In addition, made functions const correct.
This commit is contained in:
Johan Wikman
2015-11-09 13:55:59 +02:00
parent 14b8dbc4d8
commit 90a8646ac2
2 changed files with 437 additions and 425 deletions

View File

@ -38,10 +38,10 @@ secrets_randomchar()
static int static int
secrets_random_str(unsigned char *output, int len) secrets_random_str(unsigned char *output, int len)
{ {
int i; int i;
srand((unsigned long )time(0L) ^ (unsigned long )output); srand((unsigned long )time(0L) ^ (unsigned long )output);
for ( i = 0; i < len; ++i ) for (i = 0; i < len; ++i)
{ {
output[i] = secrets_randomchar(); output[i] = secrets_randomchar();
} }
@ -56,19 +56,25 @@ int i;
* @return The keys structure or NULL on error * @return The keys structure or NULL on error
*/ */
static MAXKEYS * static MAXKEYS *
secrets_readKeys(char* path) secrets_readKeys(const char* path)
{ {
char secret_file[PATH_MAX+1]; char secret_file[PATH_MAX+1];
char *home; char *home;
MAXKEYS *keys; MAXKEYS *keys;
struct stat secret_stats; struct stat secret_stats;
int fd; int fd;
int len; int len;
static int reported = 0; static int reported = 0;
if(path != NULL)
if (path != NULL)
{
snprintf(secret_file, PATH_MAX, "%s/.secrets", path); snprintf(secret_file, PATH_MAX, "%s/.secrets", path);
}
else else
{
snprintf(secret_file, PATH_MAX, "%s/.secrets", get_datadir()); snprintf(secret_file, PATH_MAX, "%s/.secrets", get_datadir());
}
/* Try to access secrets file */ /* Try to access secrets file */
if (access(secret_file, R_OK) == -1) if (access(secret_file, R_OK) == -1)
{ {
@ -150,6 +156,7 @@ static int reported = 0;
strerror_r(eno, errbuf, sizeof(errbuf))))); strerror_r(eno, errbuf, sizeof(errbuf)))));
return NULL; return NULL;
} }
if (secret_stats.st_mode != (S_IRUSR|S_IFREG)) if (secret_stats.st_mode != (S_IRUSR|S_IFREG))
{ {
close(fd); close(fd);
@ -224,18 +231,18 @@ static int reported = 0;
* @param secret_file The file with secret keys * @param secret_file The file with secret keys
* @return 0 on success and 1 on failure * @return 0 on success and 1 on failure
*/ */
int secrets_writeKeys(char *path) int secrets_writeKeys(const char *path)
{ {
int fd,randfd; int fd,randfd;
unsigned int randval; unsigned int randval;
MAXKEYS key; MAXKEYS key;
char secret_file[PATH_MAX + 10]; char secret_file[PATH_MAX + 10];
if(strlen(path) > PATH_MAX) if (strlen(path) > PATH_MAX)
{ {
skygw_log_write(LOGFILE_ERROR,"Error: Pathname too long."); skygw_log_write(LOGFILE_ERROR,"Error: Pathname too long.");
return 1; return 1;
} }
snprintf(secret_file,PATH_MAX + 9,"%s/.secrets",path); snprintf(secret_file,PATH_MAX + 9,"%s/.secrets",path);
secret_file[PATH_MAX + 9] = '\0'; secret_file[PATH_MAX + 9] = '\0';
@ -267,7 +274,7 @@ if(strlen(path) > PATH_MAX)
return 1; return 1;
} }
if(read(randfd,(void*)&randval,sizeof(unsigned int)) < 1) if (read(randfd,(void*)&randval,sizeof(unsigned int)) < 1)
{ {
LOGIF(LE, (skygw_log_write_flush( LOGIF(LE, (skygw_log_write_flush(
LOGFILE_ERROR, LOGFILE_ERROR,
@ -310,7 +317,7 @@ if(strlen(path) > PATH_MAX)
strerror_r(errno, errbuf, sizeof(errbuf))))); strerror_r(errno, errbuf, sizeof(errbuf)))));
} }
if( chmod(secret_file, S_IRUSR) < 0) if (chmod(secret_file, S_IRUSR) < 0)
{ {
char errbuf[STRERROR_BUFLEN]; char errbuf[STRERROR_BUFLEN];
LOGIF(LE, (skygw_log_write_flush( LOGIF(LE, (skygw_log_write_flush(
@ -337,18 +344,20 @@ if(strlen(path) > PATH_MAX)
* @return The decrypted password * @return The decrypted password
*/ */
char * char *
decryptPassword(char *crypt) decryptPassword(const char *crypt)
{ {
MAXKEYS *keys; MAXKEYS *keys;
AES_KEY aeskey; AES_KEY aeskey;
unsigned char *plain; unsigned char *plain;
char *ptr; const char *ptr;
unsigned char encrypted[80]; unsigned char encrypted[80];
int enlen; int enlen;
keys = secrets_readKeys(NULL); keys = secrets_readKeys(NULL);
if (!keys) if (!keys)
{
return strdup(crypt); return strdup(crypt);
}
/* /*
** If the input is not a HEX string return the input ** If the input is not a HEX string return the input
** it probably was not encrypted ** it probably was not encrypted
@ -388,17 +397,19 @@ int enlen;
* @return The encrypted password * @return The encrypted password
*/ */
char * char *
encryptPassword(char* path, char *password) encryptPassword(const char* path, const char *password)
{ {
MAXKEYS *keys; MAXKEYS *keys;
AES_KEY aeskey; AES_KEY aeskey;
int padded_len; int padded_len;
char *hex_output; char *hex_output;
unsigned char padded_passwd[80]; unsigned char padded_passwd[80];
unsigned char encrypted[80]; unsigned char encrypted[80];
if ((keys = secrets_readKeys(path)) == NULL) if ((keys = secrets_readKeys(path)) == NULL)
{
return NULL; return NULL;
}
memset(padded_passwd, 0, 80); memset(padded_passwd, 0, 80);
strncpy((char *)padded_passwd, password, 79); strncpy((char *)padded_passwd, password, 79);

View File

@ -46,12 +46,13 @@
/** /**
* The key structure held in the secrets file * The key structure held in the secrets file
*/ */
typedef struct maxkeys { typedef struct maxkeys
{
unsigned char enckey[MAXSCALE_KEYLEN]; unsigned char enckey[MAXSCALE_KEYLEN];
unsigned char initvector[MAXSCALE_IV_LEN]; unsigned char initvector[MAXSCALE_IV_LEN];
} MAXKEYS; } MAXKEYS;
extern int secrets_writeKeys(char *filename); extern int secrets_writeKeys(const char *filename);
extern char *decryptPassword(char *); extern char *decryptPassword(const char *);
extern char *encryptPassword(char*,char *); extern char *encryptPassword(const char*, const char *);
#endif #endif