4
4
* darcy@druid.net
5
5
* http://www.druid.net/darcy/
6
6
*
7
- * $Header: /cvsroot/pgsql/contrib/ chkpass/chkpass .c,v 1.2 2001/05/27 19:06:20 darcy Exp $
7
+ * $Id: chkpass.c,v 1.3 2001/05/28 15:34:27 darcy Exp $
8
8
* best viewed with tabs set to 4
9
9
*/
10
10
14
14
#include <unistd.h>
15
15
16
16
#include <postgres.h>
17
- #include <utils/palloc .h>
17
+ #include <fmgr .h>
18
18
19
19
/*
20
20
* This type encrypts it's input unless the first character is a colon.
@@ -38,13 +38,14 @@ typedef struct chkpass
38
38
* Various forward declarations:
39
39
*/
40
40
41
- chkpass * chkpass_in (char * str );
42
- char * chkpass_out (chkpass * addr );
43
- text * chkpass_rout (chkpass * addr );
41
+ Datum chkpass_in (PG_FUNCTION_ARGS );
42
+ Datum chkpass_out (PG_FUNCTION_ARGS );
43
+ Datum chkpass_rout (PG_FUNCTION_ARGS );
44
44
45
45
/* Only equal or not equal make sense */
46
- bool chkpass_eq (chkpass * a1 , text * a2 );
47
- bool chkpass_ne (chkpass * a1 , text * a2 );
46
+ Datum chkpass_eq (PG_FUNCTION_ARGS );
47
+ Datum chkpass_ne (PG_FUNCTION_ARGS );
48
+
48
49
49
50
/* This function checks that the password is a good one
50
51
* It's just a placeholder for now */
@@ -57,9 +58,11 @@ verify_pass(const char *str)
57
58
/*
58
59
* CHKPASS reader.
59
60
*/
60
- chkpass *
61
- chkpass_in (char * str )
61
+ PG_FUNCTION_INFO_V1 (chkpass_in )
62
+ Datum
63
+ chkpass_in (PG_FUNCTION_ARGS )
62
64
{
65
+ char * str = PG_GETARG_CSTRING (0 );
63
66
chkpass * result ;
64
67
char mysalt [4 ];
65
68
static bool random_initialized = false;
@@ -72,14 +75,14 @@ chkpass_in(char *str)
72
75
result = (chkpass * ) palloc (sizeof (chkpass ));
73
76
strncpy (result -> password , str + 1 , 13 );
74
77
result -> password [13 ] = 0 ;
75
- return (result );
78
+ return PointerGetDatum (result );
76
79
}
77
80
78
81
if (verify_pass (str ) != 0 )
79
82
{
80
83
elog (ERROR , "chkpass_in: purported CHKPASS \"%s\" is a weak password" ,
81
84
str );
82
- return NULL ;
85
+ return PointerGetDatum ( NULL ) ;
83
86
}
84
87
85
88
result = (chkpass * ) palloc (sizeof (chkpass ));
@@ -95,81 +98,93 @@ chkpass_in(char *str)
95
98
mysalt [2 ] = 0 ; /* technically the terminator is not
96
99
* necessary but I like to play safe */
97
100
strcpy (result -> password , crypt (str , mysalt ));
98
- return (result );
101
+ return PointerGetDatum (result );
99
102
}
100
103
101
104
/*
102
105
* CHKPASS output function.
103
106
* Just like any string but we know it is max 15 (13 plus colon and terminator.)
104
107
*/
105
108
106
- char *
107
- chkpass_out (chkpass * password )
109
+ PG_FUNCTION_INFO_V1 (chkpass_out )
110
+ Datum
111
+ chkpass_out (PG_FUNCTION_ARGS )
108
112
{
113
+ chkpass * password = (chkpass * ) PG_GETARG_POINTER (0 );
109
114
char * result ;
110
115
111
116
if (password == NULL )
112
- return (NULL );
117
+ return PointerGetDatum (NULL );
113
118
114
119
if ((result = (char * ) palloc (16 )) != NULL )
115
120
{
116
121
result [0 ] = ':' ;
117
122
strcpy (result + 1 , password -> password );
118
123
}
119
124
120
- return (result );
125
+ PG_RETURN_CSTRING (result );
121
126
}
122
127
123
128
124
129
/*
125
130
* special output function that doesn't output the colon
126
131
*/
127
132
128
- text *
129
- chkpass_rout (chkpass * password )
133
+ PG_FUNCTION_INFO_V1 (chkpass_rout )
134
+ Datum
135
+ chkpass_rout (PG_FUNCTION_ARGS )
130
136
{
137
+ chkpass * password = (chkpass * ) PG_GETARG_POINTER (0 );
131
138
text * result = NULL ;
132
139
133
140
if (password == NULL )
134
- return (NULL );
141
+ return PointerGetDatum (NULL );
135
142
136
143
if ((result = (text * ) palloc (VARHDRSZ + 16 )) != NULL )
137
144
{
138
145
result -> vl_len = VARHDRSZ + strlen (password -> password );
139
- memcpy (result -> vl_dat , password -> password , strlen (password -> pass
146
+ memcpy (result -> vl_dat , password -> password , strlen (password -> password ));
140
147
}
141
148
142
- return (result );
149
+ PG_RETURN_CSTRING (result );
143
150
}
144
151
145
152
146
153
/*
147
154
* Boolean tests
148
155
*/
149
156
150
- bool
151
- chkpass_eq (chkpass * a1 , text * a2 )
157
+ PG_FUNCTION_INFO_V1 (chkpass_eq )
158
+ Datum
159
+ chkpass_eq (PG_FUNCTION_ARGS )
152
160
{
153
- char str [10 ];
154
- int sz = 8 ;
161
+ chkpass * a1 = (chkpass * ) PG_GETARG_POINTER (0 );
162
+ text * a2 = (text * ) PG_GETARG_TEXT_P (1 );
163
+ char str [10 ];
164
+ int sz = 8 ;
165
+
166
+ if (!a1 || !a2 )
167
+ PG_RETURN_BOOL (0 );
155
168
156
- if (!a1 || !a2 ) return 0 ;
157
169
if (a2 -> vl_len < 12 ) sz = a2 -> vl_len - 4 ;
158
170
strncpy (str , a2 -> vl_dat , sz );
159
171
str [sz ] = 0 ;
160
- return (strcmp (a1 -> password , crypt (str , a1 -> password )) == 0 );
172
+ PG_RETURN_BOOL (strcmp (a1 -> password , crypt (str , a1 -> password )) == 0 );
161
173
}
162
174
163
- bool
164
- chkpass_ne (chkpass * a1 , text * a2 )
175
+ PG_FUNCTION_INFO_V1 (chkpass_ne )
176
+ Datum
177
+ chkpass_ne (PG_FUNCTION_ARGS )
165
178
{
166
- char str [10 ];
167
- int sz = 8 ;
179
+ chkpass * a1 = (chkpass * ) PG_GETARG_POINTER (0 );
180
+ text * a2 = (text * ) PG_GETARG_TEXT_P (1 );
181
+ char str [10 ];
182
+ int sz = 8 ;
168
183
169
184
if (!a1 || !a2 ) return 0 ;
170
185
if (a2 -> vl_len < 12 ) sz = a2 -> vl_len - 4 ;
171
186
strncpy (str , a2 -> vl_dat , sz );
172
187
str [sz ] = 0 ;
173
- return (strcmp (a1 -> password , crypt (str , a1 -> password )) != 0 );
188
+ PG_RETURN_BOOL (strcmp (a1 -> password , crypt (str , a1 -> password )) != 0 );
174
189
}
175
190
0 commit comments