44 * darcy@druid.net
55 * http://www.druid.net/darcy/
66 *
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 $
88 * best viewed with tabs set to 4
99 */
1010
1414#include <unistd.h>
1515
1616#include <postgres.h>
17- #include <utils/palloc .h>
17+ #include <fmgr .h>
1818
1919/*
2020 * This type encrypts it's input unless the first character is a colon.
@@ -38,13 +38,14 @@ typedef struct chkpass
3838 * Various forward declarations:
3939 */
4040
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 );
4444
4545/* 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+
4849
4950/* This function checks that the password is a good one
5051 * It's just a placeholder for now */
@@ -57,9 +58,11 @@ verify_pass(const char *str)
5758/*
5859 * CHKPASS reader.
5960 */
60- chkpass *
61- chkpass_in (char * str )
61+ PG_FUNCTION_INFO_V1 (chkpass_in )
62+ Datum
63+ chkpass_in (PG_FUNCTION_ARGS )
6264{
65+ char * str = PG_GETARG_CSTRING (0 );
6366chkpass * result ;
6467char mysalt [4 ];
6568static bool random_initialized = false;
@@ -72,14 +75,14 @@ chkpass_in(char *str)
7275result = (chkpass * )palloc (sizeof (chkpass ));
7376strncpy (result -> password ,str + 1 ,13 );
7477result -> password [13 ]= 0 ;
75- return (result );
78+ return PointerGetDatum (result );
7679}
7780
7881if (verify_pass (str )!= 0 )
7982{
8083elog (ERROR ,"chkpass_in: purported CHKPASS \"%s\" is a weak password" ,
8184str );
82- return NULL ;
85+ return PointerGetDatum ( NULL ) ;
8386}
8487
8588result = (chkpass * )palloc (sizeof (chkpass ));
@@ -95,81 +98,93 @@ chkpass_in(char *str)
9598mysalt [2 ]= 0 ;/* technically the terminator is not
9699 * necessary but I like to play safe */
97100strcpy (result -> password ,crypt (str ,mysalt ));
98- return (result );
101+ return PointerGetDatum (result );
99102}
100103
101104/*
102105 * CHKPASS output function.
103106 * Just like any string but we know it is max 15 (13 plus colon and terminator.)
104107 */
105108
106- char *
107- chkpass_out (chkpass * password )
109+ PG_FUNCTION_INFO_V1 (chkpass_out )
110+ Datum
111+ chkpass_out (PG_FUNCTION_ARGS )
108112{
113+ chkpass * password = (chkpass * )PG_GETARG_POINTER (0 );
109114char * result ;
110115
111116if (password == NULL )
112- return (NULL );
117+ return PointerGetDatum (NULL );
113118
114119if ((result = (char * )palloc (16 ))!= NULL )
115120{
116121result [0 ]= ':' ;
117122strcpy (result + 1 ,password -> password );
118123}
119124
120- return (result );
125+ PG_RETURN_CSTRING (result );
121126}
122127
123128
124129/*
125130 * special output function that doesn't output the colon
126131 */
127132
128- text *
129- chkpass_rout (chkpass * password )
133+ PG_FUNCTION_INFO_V1 (chkpass_rout )
134+ Datum
135+ chkpass_rout (PG_FUNCTION_ARGS )
130136{
137+ chkpass * password = (chkpass * )PG_GETARG_POINTER (0 );
131138text * result = NULL ;
132139
133140if (password == NULL )
134- return (NULL );
141+ return PointerGetDatum (NULL );
135142
136143if ((result = (text * )palloc (VARHDRSZ + 16 ))!= NULL )
137144{
138145result -> 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 ));
140147}
141148
142- return (result );
149+ PG_RETURN_CSTRING (result );
143150}
144151
145152
146153/*
147154 * Boolean tests
148155 */
149156
150- bool
151- chkpass_eq (chkpass * a1 ,text * a2 )
157+ PG_FUNCTION_INFO_V1 (chkpass_eq )
158+ Datum
159+ chkpass_eq (PG_FUNCTION_ARGS )
152160{
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 );
155168
156- if (!a1 || !a2 )return 0 ;
157169if (a2 -> vl_len < 12 )sz = a2 -> vl_len - 4 ;
158170strncpy (str ,a2 -> vl_dat ,sz );
159171str [sz ]= 0 ;
160- return (strcmp (a1 -> password ,crypt (str ,a1 -> password ))== 0 );
172+ PG_RETURN_BOOL (strcmp (a1 -> password ,crypt (str ,a1 -> password ))== 0 );
161173}
162174
163- bool
164- chkpass_ne (chkpass * a1 ,text * a2 )
175+ PG_FUNCTION_INFO_V1 (chkpass_ne )
176+ Datum
177+ chkpass_ne (PG_FUNCTION_ARGS )
165178{
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 ;
168183
169184if (!a1 || !a2 )return 0 ;
170185if (a2 -> vl_len < 12 )sz = a2 -> vl_len - 4 ;
171186strncpy (str ,a2 -> vl_dat ,sz );
172187str [sz ]= 0 ;
173- return (strcmp (a1 -> password ,crypt (str ,a1 -> password ))!= 0 );
188+ PG_RETURN_BOOL (strcmp (a1 -> password ,crypt (str ,a1 -> password ))!= 0 );
174189}
175190