微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 使用密码salt的Pure-ftpd和Postgreql Auth

我最近开始设置PureFTP服务器的任务.在工作中我们使用 Postgresql 8.4.架构基本上归结为,

username        text
password        character(40)
password_salt   text

密码存储为sha1(密码盐)的哈希值.使用Postgresql的pgcrypto我可以提供用户名密码,并找出用户是否有auth:

SELECT
 encode( digest( $password ||password_salt,'sha1' ),'hex' ) = password
   AS password_correct,username,password,password_salt
FROM contact.person;

现在我遇到的问题是这样的功能需要我将密码输入查询. Pureftp目前的auth-postgresql实现似乎不太可行.它仅支持提供:

\L is replaced by the login of a user trying to authenticate.
\I is replaced by the IP address the client connected to.
\P is replaced by the port number the client connected to.
\R is replaced by the remote IP address the client connected from.
\D is replaced by the remote IPv4 address,as a long decimal number.

还有其他方法可以做到这一点吗?我要么需要在查询获取密码,要么取出盐和密码,并找到另一种在Pureftp中编写代码方法.

显然,我有另外一个custom authentication module的选项,但我认为pg模块会支持这种基本的salting.

参考

> Pure FTPd’s Postgresql-auth docs
> Postgresql 8.4’s pgcrypto

解决方法

我遇到了同样的问题.然而,编写我自己的自定义身份验证模块将是一个过度杀伤,因为可用的pgsql auth几乎可以完成我想要的任何事情.
以下是我为满足我的需求而做出的改变:

在log_pgsql_p.h中添加静态char * salting;和静态char * sqlreq_getsalt;并使用{“PGsqlSalting”,& salting}和{“PGsqlGetSalt”,& sqlreq_getsalt}扩展静态ConfigKeywords pgsql_config_keywords [].

在log_pgsql.h中,我添加了#define SALT_sql_APPEND“append”,#define SALT_sql_PREPEND“prepend”和#define SALT_sql_NONE“none”.

在log_pgsql.c中,我在pw_psql_check函数中进行了以下更改:

我声明了const char * salt = NULL;和char * salted_pa​​ssword = NULL;在顶部.
直接在spwd被分配了sqlreq_getpw的查询结果之前我添加

if (strcasecmp(salting,SALT_sql_NONE) != 0) {
    salt = pw_pgsql_getquery(id_sql_server,sqlreq_getsalt,escaped_account,escaped_ip,escaped_port,escaped_peer_ip,escaped_decimal_ip);
}

然后,在加密发生之前:

if (salt != NULL) {
    int salted_pw_size = strlen(salt) + strlen(password) + 1;
    salted_password = (char *) malloc(salted_pw_size);
    if (strcasecmp(salting,SALT_sql_APPEND) == 0) {
        strcpy(salted_password,password);
        strcat(salted_password,salt);            
    } else if (strcasecmp(salting,SALT_sql_PREPEND) == 0) {
        strcpy(salted_password,salt);
        strcat(salted_password,password);
    }
} else {
    salted_password = (char *) malloc(strlen(password));
    strcpy(salted_password,password);
}

然后我在后续调用crypt-methods(crypt,crypto_hash_md5,crypto_hash_sha1)和strcasecmp替换了’cleartext’和(const char *)salted_pa​​ssword.

现在剩下要做的就是整理我们分配的内存.特别是带有附加/前置盐的明文密码不应该保留在内存中 – 如果你愿意,可以称之为偏执狂.所以在再见之后:标签添加

free((void *) salt;
if(strcasecmp(salting,SALT_sql_NONE) != 0) {
    volatile char *salted_password_ = (volatile char *) salted_password;
    while(*salted_password_ != 0) {
        *salted_password_++ = 0;
    }
    free((void *) salted_password);
}

通过这些更改,您现在可以在配置文件中使用两个附加参数:

> PGsqlSalting:接受’append'(将盐附加到pw),’prepend’和’none'(没有撇号)
> PGsqlGetSalt:在这里你指定数据库中的字段来获取盐,就像你需要通过PGsqlGetPw检索的加密密码一样.

编辑:哦,不要忘记在功能结束时释放分配的内存!

我也可以提供一个适用于1.0.36版本的差异文件.here you go!请注意,我添加了if后面的salted_pa​​ssword释放(因为我后来才意识到如果salted_pa​​ssword指向密码,这可能会导致错误),所以这不是差异,我懒得改变差异文件:/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐