Postgresql密码检查.c修改
我需要修改密码,以便在设置密码之前检查密码 我对C不太好 所以我加了这张支票:Postgresql密码检查.c修改,c,postgresql,C,Postgresql,我需要修改密码,以便在设置密码之前检查密码 我对C不太好 所以我加了这张支票: if (validuntil_null) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("password expiriration missing"))); 它可以工作,它检查是否设置了过期日期 现在我想检查这个到期日期是否合理,比如不超过3
if (validuntil_null)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password expiriration missing")));
它可以工作,它检查是否设置了过期日期
现在我想检查这个到期日期是否合理,比如不超过3个月(使用“validuntil_time”),以及它是否与之前的设置不同
有什么想法吗
提前感谢。我不熟悉PostgreSQL的内部结构或PostgreSQL的配置。我的第一种方法是研究文档,以确定是否可以使用管理员权限设置最大密码过期时间。我猜你已经详细研究过了,觉得这是最好的选择 在此基础上,我回顾了Postgres github存储库中的一些时间戳代码。我还没有整理过这篇文章,但我相信这已经很接近了。我不清楚数据是什么,它是否已经是TimeStampTz类型,或者是否需要以某种方式进行转换。如果不回答这个问题,它可能无法正确编译。让我知道这是否适用于您: 在文件顶部,将其添加到包含:
#include "utils/timestamp.h"
稍后,在放置当前代码的同一位置,将代码替换为:
if (validuntil_null) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password expiriration missing")));
} else {
TimestampTz now = GetCurrentTimestamp();
const int ThreeMonthsInMiliSeconds = 90 * 24 * 3600 * 1000;
if(TimestampDifferenceExceeds(now, validuntil_time, ThreeMonthsInMiliSeconds) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password expiriration time is greater than 90 days")));
}
}
以下是timestamp.c中该时差函数的源代码:
/*
* TimestampDifferenceExceeds -- report whether the difference between two
* timestamps is >= a threshold (expressed in milliseconds)
*
* Both inputs must be ordinary finite timestamps (in current usage,
* they'll be results from GetCurrentTimestamp()).
*/
bool
TimestampDifferenceExceeds(TimestampTz start_time,
TimestampTz stop_time,
int msec)
{
TimestampTz diff = stop_time - start_time;
return (diff >= msec * INT64CONST(1000));
}
谢谢你,约翰,最后我还是这样做了:
if (validuntil_null) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password expiriration missing")));
} else {
TimestampTz now = GetCurrentTimestamp();
const off64_t threshold = (const off64_t)((off64_t)90 * 24 * 3600 * 1000 );
if (now > validuntil_time) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\nERROR now: %ld validuntil_time: %ld threshold: %ld",now,validuntil_time,threshold)));
} else {
const off64_t diff= (const off64_t)((off64_t)(validuntil_time - now)/1000);
if (diff >= threshold) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\nERROR: DATE is more than 90 days: now: %ld validuntil_time: %ld threshold: %ld",now,validuntil_time,threshold)));
}
elog(INFO,"\nnow: %ld validuntil_time: %ld threshold: %ld diff: %ld",now,validuntil_time,threshold,diff);
}
}
谢谢你看过postgres/inlcude/utils/timestamp.h中定义的
外部bool timestampdifferenceexcensed(TimestampTz start\u time,TimestampTz stop\u time,int msec)
吗?这似乎是正确的:这是:嗨,逻辑似乎很好,但它如何计算差异的日期却非常奇怪:现在的价值结果是594223781391316,有效期(2018年11月1日:594345600000),3个月:7776000000。那么如何比较呢?我改变了行:const off_t三个月的时间长度=(const off_t)((off_t)90*24*3600*1000);由于溢出,但它不工作,即使有一天的差异,它也会失败,与过去一样,它可以工作:#更改用户回归_user1密码“a_nice_long_密码”有效期至“2018年10月30日”;错误:密码过期时间大于90天#ALTER USER regress_user1密码“a_nice_long_密码”有效期至“2017年11月1日”;AlterRolei为timestamp.c函数添加了一份源代码副本(如果有帮助的话)。