PostgreSQL Source Code git master
passwordcheck.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "commands/user.h"
#include "fmgr.h"
#include "libpq/crypt.h"
Include dependency graph for passwordcheck.c:

Go to the source code of this file.

Functions

static void check_password (const char *username, const char *shadow_pass, PasswordType password_type, Datum validuntil_time, bool validuntil_null)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static check_password_hook_type prev_check_password_hook = NULL
 
static int min_password_length = 8
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 146 of file passwordcheck.c.

147{
148 /* Define custom GUC variables. */
149 DefineCustomIntVariable("passwordcheck.min_password_length",
150 "Minimum allowed password length.",
151 NULL,
153 8,
154 0, INT_MAX,
155 PGC_SUSET,
157 NULL, NULL, NULL);
158
159 MarkGUCPrefixReserved("passwordcheck");
160
161 /* activate password checks when the module is loaded */
164}
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5279
void DefineCustomIntVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, int minValue, int maxValue, GucContext context, int flags, GucIntCheckHook check_hook, GucIntAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5158
@ PGC_SUSET
Definition: guc.h:74
#define GUC_UNIT_BYTE
Definition: guc.h:232
static int min_password_length
Definition: passwordcheck.c:34
static void check_password(const char *username, const char *shadow_pass, PasswordType password_type, Datum validuntil_time, bool validuntil_null)
Definition: passwordcheck.c:54
static check_password_hook_type prev_check_password_hook
Definition: passwordcheck.c:31
check_password_hook_type check_password_hook
Definition: user.c:91

References check_password(), check_password_hook, DefineCustomIntVariable(), GUC_UNIT_BYTE, MarkGUCPrefixReserved(), min_password_length, PGC_SUSET, and prev_check_password_hook.

◆ check_password()

static void check_password ( const char *  username,
const char *  shadow_pass,
PasswordType  password_type,
Datum  validuntil_time,
bool  validuntil_null 
)
static

Definition at line 54 of file passwordcheck.c.

59{
62 password_type, validuntil_time,
63 validuntil_null);
64
65 if (password_type != PASSWORD_TYPE_PLAINTEXT)
66 {
67 /*
68 * Unfortunately we cannot perform exhaustive checks on encrypted
69 * passwords - we are restricted to guessing. (Alternatively, we could
70 * insist on the password being presented non-encrypted, but that has
71 * its own security disadvantages.)
72 *
73 * We only check for username = password.
74 */
75 const char *logdetail = NULL;
76
77 if (plain_crypt_verify(username, shadow_pass, username, &logdetail) == STATUS_OK)
79 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
80 errmsg("password must not equal user name")));
81 }
82 else
83 {
84 /*
85 * For unencrypted passwords we can perform better checks
86 */
87 const char *password = shadow_pass;
88 int pwdlen = strlen(password);
89 int i;
90 bool pwd_has_letter,
91 pwd_has_nonletter;
92#ifdef USE_CRACKLIB
93 const char *reason;
94#endif
95
96 /* enforce minimum length */
97 if (pwdlen < min_password_length)
99 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
100 errmsg("password is too short"),
101 errdetail("password must be at least \"passwordcheck.min_password_length\" (%d) bytes long",
103
104 /* check if the password contains the username */
105 if (strstr(password, username))
107 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
108 errmsg("password must not contain user name")));
109
110 /* check if the password contains both letters and non-letters */
111 pwd_has_letter = false;
112 pwd_has_nonletter = false;
113 for (i = 0; i < pwdlen; i++)
114 {
115 /*
116 * isalpha() does not work for multibyte encodings but let's
117 * consider non-ASCII characters non-letters
118 */
119 if (isalpha((unsigned char) password[i]))
120 pwd_has_letter = true;
121 else
122 pwd_has_nonletter = true;
123 }
124 if (!pwd_has_letter || !pwd_has_nonletter)
126 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
127 errmsg("password must contain both letters and nonletters")));
128
129#ifdef USE_CRACKLIB
130 /* call cracklib to check password */
131 if ((reason = FascistCheck(password, CRACKLIB_DICTPATH)))
133 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
134 errmsg("password is easily cracked"),
135 errdetail_log("cracklib diagnostic: %s", reason)));
136#endif
137 }
138
139 /* all checks passed, password is ok */
140}
#define STATUS_OK
Definition: c.h:1126
int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const char **logdetail)
Definition: crypt.c:256
@ PASSWORD_TYPE_PLAINTEXT
Definition: crypt.h:42
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
int errdetail_log(const char *fmt,...)
Definition: elog.c:1251
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static char * username
Definition: initdb.c:153
int i
Definition: isn.c:72
static char * password
Definition: streamutil.c:52

References ereport, errcode(), errdetail(), errdetail_log(), errmsg(), ERROR, i, min_password_length, password, PASSWORD_TYPE_PLAINTEXT, plain_crypt_verify(), prev_check_password_hook, STATUS_OK, and username.

Referenced by _PG_init().

Variable Documentation

◆ min_password_length

int min_password_length = 8
static

Definition at line 34 of file passwordcheck.c.

Referenced by _PG_init(), and check_password().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 28 of file passwordcheck.c.

◆ prev_check_password_hook

check_password_hook_type prev_check_password_hook = NULL
static

Definition at line 31 of file passwordcheck.c.

Referenced by _PG_init(), and check_password().