gpt4 book ai didi

c - Postgresql passwordcheck.c修改

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:51 26 4
gpt4 key购买 nike

我需要修改 https://github.com/postgres/postgres/blob/REL9_5_13/contrib/passwordcheck/passwordcheck.c以便在设置密码之前检查密码。

我对 C 不是很好。

所以我添加了这张支票:

if (validuntil_null)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password expiriration missing")));

它会工作,它会检查是否设置了到期日期。

现在我想检查这个到期日期是否合理,比如不超过 3 个月(使用“validuntil_time”)以及它是否与之前设置的不同。

有什么想法吗?

提前致谢。

最佳答案

我不熟悉 PostgreSQL 的内部结构或 PostgreSQL 的配置。我的第一种方法是研究文档以确定是否可以使用管理员权限设置最长密码过期时间。我猜您已经对此进行了详细研究,并认为这是最佳选择。

基于此,我确实查看了 Postgres github 存储库中的一些时间戳代码。我没有编译这个,但相信这很接近。我不清楚 Datum 是什么,是否已经是 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));
}

关于c - Postgresql passwordcheck.c修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53060430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com