PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
chkpass.c File Reference
#include "postgres.h"
#include <time.h>
#include <unistd.h>
#include "fmgr.h"
#include "utils/backend_random.h"
#include "utils/builtins.h"
Include dependency graph for chkpass.c:

Go to the source code of this file.

Data Structures

struct  chkpass
 

Typedefs

typedef struct chkpass chkpass
 

Functions

static int verify_pass (const char *str)
 
 PG_FUNCTION_INFO_V1 (chkpass_in)
 
Datum chkpass_in (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (chkpass_out)
 
Datum chkpass_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (chkpass_rout)
 
Datum chkpass_rout (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (chkpass_eq)
 
Datum chkpass_eq (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (chkpass_ne)
 
Datum chkpass_ne (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Typedef Documentation

Function Documentation

Datum chkpass_eq ( PG_FUNCTION_ARGS  )

Definition at line 141 of file chkpass.c.

References a1, a2, crypt(), ereport, errcode(), errmsg(), ERROR, NULL, chkpass::password, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_to_cstring_buffer().

142 {
144  text *a2 = PG_GETARG_TEXT_PP(1);
145  char str[9];
146  char *crypt_output;
147 
148  text_to_cstring_buffer(a2, str, sizeof(str));
149  crypt_output = crypt(str, a1->password);
150  if (crypt_output == NULL)
151  ereport(ERROR,
152  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
153  errmsg("crypt() failed")));
154 
155  PG_RETURN_BOOL(strcmp(a1->password, crypt_output) == 0);
156 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:213
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char * crypt(const char *key, const char *setting)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
char password[16]
Definition: chkpass.c:40
static FormData_pg_attribute a2
Definition: heap.c:150
Datum chkpass_in ( PG_FUNCTION_ARGS  )

Definition at line 57 of file chkpass.c.

References crypt(), ereport, errcode(), errmsg(), ERROR, NULL, palloc0(), chkpass::password, pg_backend_random(), PG_GETARG_CSTRING, PG_RETURN_POINTER, result, strlcpy(), and verify_pass().

58 {
59  char *str = PG_GETARG_CSTRING(0);
60  chkpass *result;
61  char mysalt[4];
62  char *crypt_output;
63  static char salt_chars[] =
64  "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
65 
66  /* special case to let us enter encrypted passwords */
67  if (*str == ':')
68  {
69  result = (chkpass *) palloc0(sizeof(chkpass));
70  strlcpy(result->password, str + 1, 13 + 1);
71  PG_RETURN_POINTER(result);
72  }
73 
74  if (verify_pass(str) != 0)
75  ereport(ERROR,
76  (errcode(ERRCODE_DATA_EXCEPTION),
77  errmsg("password \"%s\" is weak", str)));
78 
79  result = (chkpass *) palloc0(sizeof(chkpass));
80 
81  if (!pg_backend_random(mysalt, 2))
82  ereport(ERROR,
83  (errmsg("could not generate random salt")));
84 
85  mysalt[0] = salt_chars[mysalt[0] & 0x3f];
86  mysalt[1] = salt_chars[mysalt[1] & 0x3f];
87  mysalt[2] = 0; /* technically the terminator is not necessary
88  * but I like to play safe */
89 
90  crypt_output = crypt(str, mysalt);
91  if (crypt_output == NULL)
92  ereport(ERROR,
93  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
94  errmsg("crypt() failed")));
95 
96  strlcpy(result->password, crypt_output, sizeof(result->password));
97 
98  PG_RETURN_POINTER(result);
99 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
#define ERROR
Definition: elog.h:43
bool pg_backend_random(char *dst, int len)
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:878
char * crypt(const char *key, const char *setting)
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
static int verify_pass(const char *str)
Definition: chkpass.c:47
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
char password[16]
Definition: chkpass.c:40
Datum chkpass_ne ( PG_FUNCTION_ARGS  )

Definition at line 160 of file chkpass.c.

References a1, a2, crypt(), ereport, errcode(), errmsg(), ERROR, NULL, chkpass::password, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_to_cstring_buffer().

161 {
163  text *a2 = PG_GETARG_TEXT_PP(1);
164  char str[9];
165  char *crypt_output;
166 
167  text_to_cstring_buffer(a2, str, sizeof(str));
168  crypt_output = crypt(str, a1->password);
169  if (crypt_output == NULL)
170  ereport(ERROR,
171  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
172  errmsg("crypt() failed")));
173 
174  PG_RETURN_BOOL(strcmp(a1->password, crypt_output) != 0);
175 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:213
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char * crypt(const char *key, const char *setting)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
char password[16]
Definition: chkpass.c:40
static FormData_pg_attribute a2
Definition: heap.c:150
Datum chkpass_out ( PG_FUNCTION_ARGS  )

Definition at line 108 of file chkpass.c.

References palloc(), chkpass::password, password, PG_GETARG_POINTER, PG_RETURN_CSTRING, result, and strlcpy().

109 {
111  char *result;
112 
113  result = (char *) palloc(16);
114  result[0] = ':';
115  strlcpy(result + 1, password->password, 15);
116 
117  PG_RETURN_CSTRING(result);
118 }
static char password[100]
Definition: streamutil.c:41
return result
Definition: formatting.c:1632
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:849
char password[16]
Definition: chkpass.c:40
Datum chkpass_rout ( PG_FUNCTION_ARGS  )

Definition at line 127 of file chkpass.c.

References cstring_to_text(), chkpass::password, password, PG_GETARG_POINTER, and PG_RETURN_TEXT_P.

128 {
130 
132 }
static char password[100]
Definition: streamutil.c:41
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
text * cstring_to_text(const char *s)
Definition: varlena.c:149
char password[16]
Definition: chkpass.c:40
PG_FUNCTION_INFO_V1 ( chkpass_in  )
PG_FUNCTION_INFO_V1 ( chkpass_out  )
PG_FUNCTION_INFO_V1 ( chkpass_rout  )
PG_FUNCTION_INFO_V1 ( chkpass_eq  )
PG_FUNCTION_INFO_V1 ( chkpass_ne  )
static int verify_pass ( const char *  str)
static

Definition at line 47 of file chkpass.c.

Referenced by chkpass_in().

48 {
49  return 0;
50 }

Variable Documentation

PG_MODULE_MAGIC

Definition at line 23 of file chkpass.c.