Re: [PATCHES] plperl fixes

2004-07-12 Thread Bruce Momjian

Patch applied.  Thanks.

---


Andrew Dunstan wrote:
> 
> The attached patch, which incorporates the previous one sent and 
> currently unapplied regarding spi_internal.c, makes some additional 
> fixes relating to return types, and also contains the fix for 
> preventing  the use of insecure versions of Safe.pm.
> 
> There is one remaing return case that does not appear to work, namely 
> return of a composite directly in a select, i.e. if  foo returns some 
> composite type, 'select * from foo()' works but 'select foo()' doesn't. 
> We will either fix that or document it as a limitation.
> 
> The function plperl_func_handler is a mess - I will try to get it 
> cleaned up (and split up) in a subsequent patch, time permitting.
> 
> Also, reiterating previous advice - this changes slightly the API for 
> spi_exec_query - the returned object has either 2 or 3 members: 'status' 
> (string) and 'proceesed' (int,- number of rows) and, if rows are 
> returned, 'rows' (array of tuple hashes).
> 
> cheers
> 
> andrew

> Index: plperl.c
> ===
> RCS file: /projects/cvsroot/pgsql-server/src/pl/plperl/plperl.c,v
> retrieving revision 1.45
> diff -c -w -r1.45 plperl.c
> *** plperl.c  1 Jul 2004 20:50:22 -   1.45
> --- plperl.c  7 Jul 2004 15:35:35 -
> ***
> *** 80,85 
> --- 80,86 
>   CommandId   fn_cmin;
>   boollanpltrusted;
>   boolfn_retistuple;  /* true, if function returns tuple */
> + boolfn_retisset;/*true, if function returns set*/
>   Oid ret_oid;/* Oid of returning type */
>   FmgrInforesult_in_func;
>   Oid result_typioparam;
> ***
> *** 95,105 
>* Global data
>**/
>   static int  plperl_firstcall = 1;
>   static PerlInterpreter *plperl_interp = NULL;
>   static HV  *plperl_proc_hash = NULL;
> ! AV *g_row_keys = NULL;
> ! AV *g_column_keys = NULL;
> ! int g_attr_num = 0;
>   
>   /**
>* Forward declarations
> --- 96,108 
>* Global data
>**/
>   static int  plperl_firstcall = 1;
> + static bool plperl_safe_init_done = false;
>   static PerlInterpreter *plperl_interp = NULL;
>   static HV  *plperl_proc_hash = NULL;
> ! static AV  *g_row_keys = NULL;
> ! static AV  *g_column_keys = NULL;
> ! static SV  *srf_perlret=NULL; /*keep returned value*/
> ! static int  g_attr_num = 0;
>   
>   /**
>* Forward declarations
> ***
> *** 215,225 
>* no commas between the next lines please. They are supposed to be
>* one string
>*/
> ! "require Safe; SPI::bootstrap(); use vars qw(%_SHARED);"
> ! "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> ! 
> "$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
> ! "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
> &NOTICE &WARNING &ERROR %SHARED ]);"
> ! "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
> $_[1]}]); }"
>   "sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
>   };
>   
> --- 218,224 
>* no commas between the next lines please. They are supposed to be
>* one string
>*/
> ! "SPI::bootstrap(); use vars qw(%_SHARED);"
>   "sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
>   };
>   
> ***
> *** 238,243 
> --- 237,277 
>   
>   }
>   
> + 
> + static void
> + plperl_safe_init(void)
> + {
> + static char *safe_module  =
> + "require Safe; $Safe::VERSION";
> + 
> + static char * safe_ok =
> + "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> + 
> "$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
> + "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
> &NOTICE &WARNING &ERROR %SHARED ]);"
> + "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
> $_[1]}]); }"
> + ;
> + 
> + static char * safe_bad = 
> + "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> + 
> "$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
> + "$PLContainer->share(qw[&elog &DEBUG &LOG &INFO &NOTICE &WARNING 
> &ERROR %SHARED ]);"
> + "sub 

Re: [PATCHES] plperl fixes

2004-07-11 Thread Bruce Momjian

Previous patch removed from the queue.

Your patch has been added to the PostgreSQL unapplied patches list at:

http://momjian.postgresql.org/cgi-bin/pgpatches

I will try to apply it within the next 48 hours.

---


Andrew Dunstan wrote:
> 
> The attached patch, which incorporates the previous one sent and 
> currently unapplied regarding spi_internal.c, makes some additional 
> fixes relating to return types, and also contains the fix for 
> preventing  the use of insecure versions of Safe.pm.
> 
> There is one remaing return case that does not appear to work, namely 
> return of a composite directly in a select, i.e. if  foo returns some 
> composite type, 'select * from foo()' works but 'select foo()' doesn't. 
> We will either fix that or document it as a limitation.
> 
> The function plperl_func_handler is a mess - I will try to get it 
> cleaned up (and split up) in a subsequent patch, time permitting.
> 
> Also, reiterating previous advice - this changes slightly the API for 
> spi_exec_query - the returned object has either 2 or 3 members: 'status' 
> (string) and 'proceesed' (int,- number of rows) and, if rows are 
> returned, 'rows' (array of tuple hashes).
> 
> cheers
> 
> andrew

> Index: plperl.c
> ===
> RCS file: /projects/cvsroot/pgsql-server/src/pl/plperl/plperl.c,v
> retrieving revision 1.45
> diff -c -w -r1.45 plperl.c
> *** plperl.c  1 Jul 2004 20:50:22 -   1.45
> --- plperl.c  7 Jul 2004 15:35:35 -
> ***
> *** 80,85 
> --- 80,86 
>   CommandId   fn_cmin;
>   boollanpltrusted;
>   boolfn_retistuple;  /* true, if function returns tuple */
> + boolfn_retisset;/*true, if function returns set*/
>   Oid ret_oid;/* Oid of returning type */
>   FmgrInforesult_in_func;
>   Oid result_typioparam;
> ***
> *** 95,105 
>* Global data
>**/
>   static int  plperl_firstcall = 1;
>   static PerlInterpreter *plperl_interp = NULL;
>   static HV  *plperl_proc_hash = NULL;
> ! AV *g_row_keys = NULL;
> ! AV *g_column_keys = NULL;
> ! int g_attr_num = 0;
>   
>   /**
>* Forward declarations
> --- 96,108 
>* Global data
>**/
>   static int  plperl_firstcall = 1;
> + static bool plperl_safe_init_done = false;
>   static PerlInterpreter *plperl_interp = NULL;
>   static HV  *plperl_proc_hash = NULL;
> ! static AV  *g_row_keys = NULL;
> ! static AV  *g_column_keys = NULL;
> ! static SV  *srf_perlret=NULL; /*keep returned value*/
> ! static int  g_attr_num = 0;
>   
>   /**
>* Forward declarations
> ***
> *** 215,225 
>* no commas between the next lines please. They are supposed to be
>* one string
>*/
> ! "require Safe; SPI::bootstrap(); use vars qw(%_SHARED);"
> ! "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> ! 
> "$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
> ! "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
> &NOTICE &WARNING &ERROR %SHARED ]);"
> ! "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
> $_[1]}]); }"
>   "sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
>   };
>   
> --- 218,224 
>* no commas between the next lines please. They are supposed to be
>* one string
>*/
> ! "SPI::bootstrap(); use vars qw(%_SHARED);"
>   "sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
>   };
>   
> ***
> *** 238,243 
> --- 237,277 
>   
>   }
>   
> + 
> + static void
> + plperl_safe_init(void)
> + {
> + static char *safe_module  =
> + "require Safe; $Safe::VERSION";
> + 
> + static char * safe_ok =
> + "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> + 
> "$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
> + "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
> &NOTICE &WARNING &ERROR %SHARED ]);"
> + "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
> $_[1]}]); }"
> + ;
> + 
> + static char * safe_bad = 
> + "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
> + 
> "$PLCon

[PATCHES] plperl fixes

2004-07-07 Thread Andrew Dunstan
The attached patch, which incorporates the previous one sent and 
currently unapplied regarding spi_internal.c, makes some additional 
fixes relating to return types, and also contains the fix for 
preventing  the use of insecure versions of Safe.pm.

There is one remaing return case that does not appear to work, namely 
return of a composite directly in a select, i.e. if  foo returns some 
composite type, 'select * from foo()' works but 'select foo()' doesn't. 
We will either fix that or document it as a limitation.

The function plperl_func_handler is a mess - I will try to get it 
cleaned up (and split up) in a subsequent patch, time permitting.

Also, reiterating previous advice - this changes slightly the API for 
spi_exec_query - the returned object has either 2 or 3 members: 'status' 
(string) and 'proceesed' (int,- number of rows) and, if rows are 
returned, 'rows' (array of tuple hashes).

cheers
andrew
Index: plperl.c
===
RCS file: /projects/cvsroot/pgsql-server/src/pl/plperl/plperl.c,v
retrieving revision 1.45
diff -c -w -r1.45 plperl.c
*** plperl.c1 Jul 2004 20:50:22 -   1.45
--- plperl.c7 Jul 2004 15:35:35 -
***
*** 80,85 
--- 80,86 
CommandId   fn_cmin;
boollanpltrusted;
boolfn_retistuple;  /* true, if function returns tuple */
+   boolfn_retisset;/*true, if function returns set*/
Oid ret_oid;/* Oid of returning type */
FmgrInforesult_in_func;
Oid result_typioparam;
***
*** 95,105 
   * Global data
   **/
  static intplperl_firstcall = 1;
  static PerlInterpreter *plperl_interp = NULL;
  static HV  *plperl_proc_hash = NULL;
! AV   *g_row_keys = NULL;
! AV   *g_column_keys = NULL;
! int   g_attr_num = 0;
  
  /**
   * Forward declarations
--- 96,108 
   * Global data
   **/
  static intplperl_firstcall = 1;
+ static bool plperl_safe_init_done = false;
  static PerlInterpreter *plperl_interp = NULL;
  static HV  *plperl_proc_hash = NULL;
! static AV*g_row_keys = NULL;
! static AV*g_column_keys = NULL;
! static SV*srf_perlret=NULL; /*keep returned value*/
! static intg_attr_num = 0;
  
  /**
   * Forward declarations
***
*** 215,225 
 * no commas between the next lines please. They are supposed to be
 * one string
 */
!   "require Safe; SPI::bootstrap(); use vars qw(%_SHARED);"
!   "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
!   
"$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
!   "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
&NOTICE &WARNING &ERROR %SHARED ]);"
!   "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
$_[1]}]); }"
"sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
};
  
--- 218,224 
 * no commas between the next lines please. They are supposed to be
 * one string
 */
!   "SPI::bootstrap(); use vars qw(%_SHARED);"
"sub ::mkunsafefunc {return eval(qq[ sub { $_[0] $_[1] } ]); }"
};
  
***
*** 238,243 
--- 237,277 
  
  }
  
+ 
+ static void
+ plperl_safe_init(void)
+ {
+   static char *safe_module  =
+   "require Safe; $Safe::VERSION";
+ 
+   static char * safe_ok =
+   "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
+   
"$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
+   "$PLContainer->share(qw[&elog &spi_exec_query &DEBUG &LOG &INFO 
&NOTICE &WARNING &ERROR %SHARED ]);"
+   "sub ::mksafefunc { return $PLContainer->reval(qq[sub { $_[0] 
$_[1]}]); }"
+   ;
+ 
+   static char * safe_bad = 
+   "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');"
+   
"$PLContainer->permit_only(':default');$PLContainer->permit(':base_math');"
+   "$PLContainer->share(qw[&elog &DEBUG &LOG &INFO &NOTICE &WARNING 
&ERROR %SHARED ]);"
+   "sub ::mksafefunc { return $PLContainer->reval(qq[sub { "
+   "elog(ERROR,'trusted perl functions disabled - please upgrade perl 
Safe module to at least 2.09');}]); }"
+   ;
+ 
+   SV * res;
+ 
+   float safe_version;
+ 
+   res = eval_pv(safe