PostgreSQL Source Code  git master
passwordcheck.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * passwordcheck.c
4  *
5  *
6  * Copyright (c) 2009-2022, PostgreSQL Global Development Group
7  *
8  * Author: Laurenz Albe <laurenz.albe@wien.gv.at>
9  *
10  * IDENTIFICATION
11  * contrib/passwordcheck/passwordcheck.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <ctype.h>
18 
19 #ifdef USE_CRACKLIB
20 #include <crack.h>
21 #endif
22 
23 #include "commands/user.h"
24 #include "fmgr.h"
25 #include "libpq/crypt.h"
26 
28 
29 /* Saved hook value in case of unload */
31 
32 /* passwords shorter than this will be rejected */
33 #define MIN_PWD_LENGTH 8
34 
35 extern void _PG_init(void);
36 
37 /*
38  * check_password
39  *
40  * performs checks on an encrypted or unencrypted password
41  * ereport's if not acceptable
42  *
43  * username: name of role being created or changed
44  * password: new password (possibly already encrypted)
45  * password_type: PASSWORD_TYPE_* code, to indicate if the password is
46  * in plaintext or encrypted form.
47  * validuntil_time: password expiration time, as a timestamptz Datum
48  * validuntil_null: true if password expiration time is NULL
49  *
50  * This sample implementation doesn't pay any attention to the password
51  * expiration time, but you might wish to insist that it be non-null and
52  * not too far in the future.
53  */
54 static void
56  const char *shadow_pass,
57  PasswordType password_type,
58  Datum validuntil_time,
59  bool validuntil_null)
60 {
63  password_type, validuntil_time,
64  validuntil_null);
65 
66  if (password_type != PASSWORD_TYPE_PLAINTEXT)
67  {
68  /*
69  * Unfortunately we cannot perform exhaustive checks on encrypted
70  * passwords - we are restricted to guessing. (Alternatively, we could
71  * insist on the password being presented non-encrypted, but that has
72  * its own security disadvantages.)
73  *
74  * We only check for username = password.
75  */
76  const char *logdetail = NULL;
77 
78  if (plain_crypt_verify(username, shadow_pass, username, &logdetail) == STATUS_OK)
79  ereport(ERROR,
80  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
81  errmsg("password must not equal user name")));
82  }
83  else
84  {
85  /*
86  * For unencrypted passwords we can perform better checks
87  */
88  const char *password = shadow_pass;
89  int pwdlen = strlen(password);
90  int i;
91  bool pwd_has_letter,
92  pwd_has_nonletter;
93 #ifdef USE_CRACKLIB
94  const char *reason;
95 #endif
96 
97  /* enforce minimum length */
98  if (pwdlen < MIN_PWD_LENGTH)
99  ereport(ERROR,
100  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
101  errmsg("password is too short")));
102 
103  /* check if the password contains the username */
104  if (strstr(password, username))
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
107  errmsg("password must not contain user name")));
108 
109  /* check if the password contains both letters and non-letters */
110  pwd_has_letter = false;
111  pwd_has_nonletter = false;
112  for (i = 0; i < pwdlen; i++)
113  {
114  /*
115  * isalpha() does not work for multibyte encodings but let's
116  * consider non-ASCII characters non-letters
117  */
118  if (isalpha((unsigned char) password[i]))
119  pwd_has_letter = true;
120  else
121  pwd_has_nonletter = true;
122  }
123  if (!pwd_has_letter || !pwd_has_nonletter)
124  ereport(ERROR,
125  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
126  errmsg("password must contain both letters and nonletters")));
127 
128 #ifdef USE_CRACKLIB
129  /* call cracklib to check password */
130  if ((reason = FascistCheck(password, CRACKLIB_DICTPATH)))
131  ereport(ERROR,
132  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
133  errmsg("password is easily cracked"),
134  errdetail_log("cracklib diagnostic: %s", reason)));
135 #endif
136  }
137 
138  /* all checks passed, password is ok */
139 }
140 
141 /*
142  * Module initialization function
143  */
144 void
145 _PG_init(void)
146 {
147  /* activate password checks when the module is loaded */
150 }
#define STATUS_OK
Definition: c.h:1167
int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const char **logdetail)
Definition: crypt.c:221
PasswordType
Definition: crypt.h:28
@ PASSWORD_TYPE_PLAINTEXT
Definition: crypt.h:29
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
int errdetail_log(const char *fmt,...)
Definition: elog.c:1085
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
int i
Definition: isn.c:73
static void check_password(const char *username, const char *shadow_pass, PasswordType password_type, Datum validuntil_time, bool validuntil_null)
Definition: passwordcheck.c:55
#define MIN_PWD_LENGTH
Definition: passwordcheck.c:33
void _PG_init(void)
PG_MODULE_MAGIC
Definition: passwordcheck.c:27
static check_password_hook_type prev_check_password_hook
Definition: passwordcheck.c:30
const char * username
Definition: pgbench.c:309
uintptr_t Datum
Definition: postgres.h:411
static char * password
Definition: streamutil.c:53
check_password_hook_type check_password_hook
Definition: user.c:50
void(* check_password_hook_type)(const char *username, const char *shadow_pass, PasswordType password_type, Datum validuntil_time, bool validuntil_null)
Definition: user.h:23