Re: Struggling with c functions

From: mlw <markw(at)mohawksoft(dot)com>
To: ohp(at)pyrenet(dot)fr
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Struggling with c functions
Date: 2001-04-29 01:22:51
Message-ID: 3AEB6CEB.EA00031C@mohawksoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

You actually almost have it right.

You are passing VARDATA(user) to crypt, this is wrong.

You must do something like this:

int ulen = VARSIZE(user)-VARHDRSZ;
char utmp[ulen+]; // This works in newer GCC, cool.
memcpy(utmp,VARDATA(user), len);
utmp[ulen]=0;
crypted=crypt(utmp,salt);

Strings are not gurenteed to be NULL teminated.

Olivier PRENANT wrote:
>
> Hi all,
>
> I'm rewriting my OLD crypt (Thanks to Henrique) C_fonction to version 0
> forms :
>
> I have this C file compiled OK to a shared library:
>
> /*
> *
> * Henrique Pantarotto (scanner(at)cepa(dot)com(dot)br)
> * Funcao para encriptar senhas (Function to encrypt passwords)
> * September 1999
> *
> * PS: Note that all crypted passwords are created with salt "HP" (my name
> * initials..) You can change that, or if you know C, you can do in a way
> * that it will pick two random characters (the way it should really be).
> *
> */
>
> #include <strings.h>
> #include <unistd.h>
>
> #include <postgres.h>
>
> text *post_crypt(text *user)
> {
> text *password;
> char * crypt();
> long now=time((long *) 0);
> int len;
> char salt[7]="PY", *crypted;
> /*strcpy(salt,l64a(now));
> salt[3]='\0'; */
> crypted=crypt(VARDATA(user),salt);
> len=strlen(crypted);
> password= palloc((int32) 13 + VARHDRSZ);
> VARATT_SIZEP(password)= (int32) VARHDRSZ + 13;
> memcpy(VARDATA(password),crypted,len);
> return password;
> }
>
> text *sql_crypt(text *user,text *salt)
> {
> text *password;
> char * crypt(), *crypted;
> int len;
> char s[3];
> strncpy(s,VARDATA(salt),2);
> s[2]='\0';
> crypted=crypt(VARDATA(user),s);
> len=strlen(crypted);
> password=palloc((int32) 13 + VARHDRSZ);
> VARATT_SIZEP(password)=(int32) 13 + VARHDRSZ;
> memcpy(VARDATA(password),crypted,len);
> return password;
> }
>
> /*
> Compile using something like this:
>
> gcc -I/home/postgres/postgresql-6.5.1/src/include -I/home/postgres/postgresql-6.5.1/src/backend -O2 -Wall -Wmissing-prototypes -fpic -I/home/postgres/postgresql-6.5.1/src/include -c -o encrypt.o encrypt.c
> gcc -shared -o encrypt.so encrypt.o
>
> And last, you create the trigger in PostgreSQL using this:
>
> create function encrypt(text)
> returns text as '/usr/local/pgsql/lib/encrypt.so' language 'c';
>
> If everything is okay, you'll probably have: select encrypt('secret') working
> and showing:
>
> encrypt
> ------------
> HPK1Jt2NX21G.
> (1 row)
> */
>
> I have defined to SQL function:
>
> CREATE FUNCTION post_crypt(text) RETURNS text AS 'xxxx/encrypt.so'
> CREATE FUNCTION sql_cypt(text,text) RETURNS text AS 'xxxx/encrypt.so';
>
> WHY on earth does
>
> SELECT post_crypt('test'),sql_crypt('test','PY')
> NOT GIVE the same result???
>
> Please help,
>
> This is most urgent (My customer can't use this function anymore); it
> worked OK with 7.0.3!!
>
> Regards,
> --
> Olivier PRENANT Tel: +33-5-61-50-97-00 (Work)
> Quartier d'Harraud Turrou +33-5-61-50-97-01 (Fax)
> 31190 AUTERIVE +33-6-07-63-80-64 (GSM)
> FRANCE Email: ohp(at)pyrenet(dot)fr
> ------------------------------------------------------------------------------
> Make your life a dream, make your dream a reality. (St Exupery)
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl

--
I'm not offering myself as an example; every life evolves by its own laws.
------------------------
http://www.mohawksoft.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gavin Sherry 2001-04-29 02:17:16 Re: SAPDB Open Souce
Previous Message Raoul Callaghan 2001-04-28 22:55:07 perlDBD::pg error! Please help (have all details in post)