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@cepa.com.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@pyrenet.fr
------------------------------------------------------------------------------
Make your life a dream, make your dream a reality. (St Exupery)


Sat, 28 Apr 2001 23:51:45 +0200 (MET DST)
Olivier PRENANT <ohp@pyrenet.fr>
Struggling with c functions
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@cepa.com.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@pyrenet.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

Sat, 28 Apr 2001 21:22:51 -0400
mlw <markw@mohawksoft.com>
Re: Struggling with c functions
Many thanks to you!!!

It now works (did'nt realize that strings where not null
terminated) stupid me!!!

Regards,
On Sat, 28 Apr 2001, mlw wrote:

> 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@cepa.com.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@pyrenet.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
> 
> 

-- 
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@pyrenet.fr
------------------------------------------------------------------------------
Make your life a dream, make your dream a reality. (St Exupery)


Sun, 29 Apr 2001 12:09:29 +0200 (MET DST)
Olivier PRENANT <ohp@pyrenet.fr>
Re: Struggling with c functions