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

Go to the source code of this file.

Macros

#define MIN_PWD_LENGTH   8
 

Functions

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

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ MIN_PWD_LENGTH

#define MIN_PWD_LENGTH   8

Definition at line 30 of file passwordcheck.c.

Referenced by check_password().

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 133 of file passwordcheck.c.

References check_password(), and check_password_hook.

134 {
135  /* activate password checks when the module is loaded */
137 }
static void check_password(const char *username, const char *shadow_pass, PasswordType password_type, Datum validuntil_time, bool validuntil_null)
Definition: passwordcheck.c:52
check_password_hook_type check_password_hook
Definition: user.c:50

◆ 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 52 of file passwordcheck.c.

References ereport, errcode(), errmsg(), ERROR, i, MIN_PWD_LENGTH, password, PASSWORD_TYPE_PLAINTEXT, plain_crypt_verify(), and STATUS_OK.

Referenced by _PG_init().

57 {
58  if (password_type != PASSWORD_TYPE_PLAINTEXT)
59  {
60  /*
61  * Unfortunately we cannot perform exhaustive checks on encrypted
62  * passwords - we are restricted to guessing. (Alternatively, we could
63  * insist on the password being presented non-encrypted, but that has
64  * its own security disadvantages.)
65  *
66  * We only check for username = password.
67  */
68  char *logdetail;
69 
70  if (plain_crypt_verify(username, shadow_pass, username, &logdetail) == STATUS_OK)
71  ereport(ERROR,
72  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
73  errmsg("password must not equal user name")));
74  }
75  else
76  {
77  /*
78  * For unencrypted passwords we can perform better checks
79  */
80  const char *password = shadow_pass;
81  int pwdlen = strlen(password);
82  int i;
83  bool pwd_has_letter,
84  pwd_has_nonletter;
85 
86  /* enforce minimum length */
87  if (pwdlen < MIN_PWD_LENGTH)
88  ereport(ERROR,
89  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
90  errmsg("password is too short")));
91 
92  /* check if the password contains the username */
93  if (strstr(password, username))
94  ereport(ERROR,
95  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
96  errmsg("password must not contain user name")));
97 
98  /* check if the password contains both letters and non-letters */
99  pwd_has_letter = false;
100  pwd_has_nonletter = false;
101  for (i = 0; i < pwdlen; i++)
102  {
103  /*
104  * isalpha() does not work for multibyte encodings but let's
105  * consider non-ASCII characters non-letters
106  */
107  if (isalpha((unsigned char) password[i]))
108  pwd_has_letter = true;
109  else
110  pwd_has_nonletter = true;
111  }
112  if (!pwd_has_letter || !pwd_has_nonletter)
113  ereport(ERROR,
114  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
115  errmsg("password must contain both letters and nonletters")));
116 
117 #ifdef USE_CRACKLIB
118  /* call cracklib to check password */
119  if (FascistCheck(password, CRACKLIB_DICTPATH))
120  ereport(ERROR,
121  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
122  errmsg("password is easily cracked")));
123 #endif
124  }
125 
126  /* all checks passed, password is ok */
127 }
static char password[100]
Definition: streamutil.c:45
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define STATUS_OK
Definition: c.h:953
int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, char **logdetail)
Definition: crypt.c:216
static char * username
Definition: initdb.c:132
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define MIN_PWD_LENGTH
Definition: passwordcheck.c:30

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 27 of file passwordcheck.c.