codekingpro/portable-devtools
114k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>45.5. Trusted and Untrusted PL/Perl</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plperl-global.html" title="45.4. Global Values in PL/Perl" /><link rel="next" href="plperl-triggers.html" title="45.6. PL/Perl Triggers" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">45.5. Trusted and Untrusted PL/Perl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-global.html" title="45.4. Global Values in PL/Perl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 45. PL/Perl — Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-triggers.html" title="45.6. PL/Perl Triggers">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPERL-TRUSTED"><div class="titlepage"><div><div><h2 class="title" style="clear: both">45.5. Trusted and Untrusted PL/Perl <a href="#PLPERL-TRUSTED" class="id_link">#</a></h2></div></div></div><a id="id-1.8.10.13.2" class="indexterm"></a><p>3 Normally, PL/Perl is installed as a <span class="quote">“<span class="quote">trusted</span>”</span> programming4 language named <code class="literal">plperl</code>. In this setup, certain Perl5 operations are disabled to preserve security. In general, the6 operations that are restricted are those that interact with the7 environment. This includes file handle operations,8 <code class="literal">require</code>, and <code class="literal">use</code> (for9 external modules). There is no way to access internals of the10 database server process or to gain OS-level access with the11 permissions of the server process,12 as a C function can do. Thus, any unprivileged database user can13 be permitted to use this language.14 </p><p>15 Here is an example of a function that will not work because file16 system operations are not allowed for security reasons:17</p><pre class="programlisting">18CREATE FUNCTION badfunc() RETURNS integer AS $$19 my $tmpfile = "/tmp/badfile";20 open my $fh, '>', $tmpfile21 or elog(ERROR, qq{could not open the file "$tmpfile": $!});22 print $fh "Testing writing to a file\n";23 close $fh or elog(ERROR, qq{could not close the file "$tmpfile": $!});24 return 1;25$$ LANGUAGE plperl;26</pre><p>27 The creation of this function will fail as its use of a forbidden28 operation will be caught by the validator.29 </p><p>30 Sometimes it is desirable to write Perl functions that are not31 restricted. For example, one might want a Perl function that sends32 mail. To handle these cases, PL/Perl can also be installed as an33 <span class="quote">“<span class="quote">untrusted</span>”</span> language (usually called34 <span class="application">PL/PerlU</span><a id="id-1.8.10.13.5.3" class="indexterm"></a>).35 In this case the full Perl language is available. When installing the36 language, the language name <code class="literal">plperlu</code> will select37 the untrusted PL/Perl variant.38 </p><p>39 The writer of a <span class="application">PL/PerlU</span> function must take care that the function40 cannot be used to do anything unwanted, since it will be able to do41 anything that could be done by a user logged in as the database42 administrator. Note that the database system allows only database43 superusers to create functions in untrusted languages.44 </p><p>45 If the above function was created by a superuser using the language46 <code class="literal">plperlu</code>, execution would succeed.47 </p><p>48 In the same way, anonymous code blocks written in Perl can use49 restricted operations if the language is specified as50 <code class="literal">plperlu</code> rather than <code class="literal">plperl</code>, but the caller51 must be a superuser.52 </p><div class="note"><h3 class="title">Note</h3><p>53 While <span class="application">PL/Perl</span> functions run in a separate Perl54 interpreter for each SQL role, all <span class="application">PL/PerlU</span> functions55 executed in a given session run in a single Perl interpreter (which is56 not any of the ones used for <span class="application">PL/Perl</span> functions).57 This allows <span class="application">PL/PerlU</span> functions to share data freely,58 but no communication can occur between <span class="application">PL/Perl</span> and59 <span class="application">PL/PerlU</span> functions.60 </p></div><div class="note"><h3 class="title">Note</h3><p>61 Perl cannot support multiple interpreters within one process unless62 it was built with the appropriate flags, namely either63 <code class="literal">usemultiplicity</code> or <code class="literal">useithreads</code>.64 (<code class="literal">usemultiplicity</code> is preferred unless you actually need65 to use threads. For more details, see the66 <span class="citerefentry"><span class="refentrytitle">perlembed</span></span> man page.)67 If <span class="application">PL/Perl</span> is used with a copy of Perl that was not built68 this way, then it is only possible to have one Perl interpreter per69 session, and so any one session can only execute either70 <span class="application">PL/PerlU</span> functions, or <span class="application">PL/Perl</span> functions71 that are all called by the same SQL role.72 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plperl-global.html" title="45.4. Global Values in PL/Perl">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-triggers.html" title="45.6. PL/Perl Triggers">Next</a></td></tr><tr><td width="40%" align="left" valign="top">45.4. Global Values in PL/Perl </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 45.6. PL/Perl Triggers</td></tr></table></div></body></html>