バージョンごとのドキュメント一覧

45.5. 信頼されたPL/Perlおよび信頼されないPL/Perl #

<title>Trusted and Untrusted PL/Perl</title>

Normally, PL/Perl is installed as a <quote>trusted</quote> programming language named <literal>plperl</literal>. In this setup, certain Perl operations are disabled to preserve security. In general, the operations that are restricted are those that interact with the environment. This includes file handle operations, <literal>require</literal>, and <literal>use</literal> (for external modules). There is no way to access internals of the database server process or to gain OS-level access with the permissions of the server process, as a C function can do. Thus, any unprivileged database user can be permitted to use this language. 通常、PL/Perlはplperlという名前で信頼されたプログラミング言語としてインストールされます。 この設定では、セキュリティを確保するためにPerlの特定の操作は無効にされます。 一般的には、制限される操作は環境に作用するものです。 これには、ファイルハンドル操作やrequireuse(外部モジュール用)が含まれます。 C関数では可能ですが、Perlでは、データベースサーバ内部にアクセスする方法や、サーバプロセスの権限によるOSレベルのアクセスを行う方法はありません。 この結果、データベースの全ての非特権ユーザはこの言語を使用することができます。

Here is an example of a function that will not work because file system operations are not allowed for security reasons: セキュリティ上の理由により許されていないファイルシステム操作を行うため、うまく動作しない関数の例を以下に示します。

CREATE FUNCTION badfunc() RETURNS integer AS $$
    my $tmpfile = "/tmp/badfile";
    open my $fh, '>', $tmpfile
        or elog(ERROR, qq{could not open the file "$tmpfile": $!});
    print $fh "Testing writing to a file\n";
    close $fh or elog(ERROR, qq{could not close the file "$tmpfile": $!});
    return 1;
$$ LANGUAGE plperl;

The creation of this function will fail as its use of a forbidden operation will be caught by the validator. 許されていない操作の使用が検証機能によって検出されますので、この関数の作成は失敗します。

Sometimes it is desirable to write Perl functions that are not restricted. For example, one might want a Perl function that sends mail. To handle these cases, PL/Perl can also be installed as an <quote>untrusted</quote> language (usually called <application>PL/PerlU</application><indexterm><primary>PL/PerlU</primary></indexterm>). In this case the full Perl language is available. When installing the language, the language name <literal>plperlu</literal> will select the untrusted PL/Perl variant. 制限のないPerl関数の作成が望ましい場合があります。 例えば、Perl 関数を使用してメールを送信するような場合です。 このような場合を扱うために、PL/Perlを信頼されない言語(通常PL/PerlUと呼ばれます)としてインストールすることもできます。 この場合は完全なPerl言語を使用することができます。 言語がインストールされた場合、plperluという言語名によって、信頼されないPL/Perlの亜種が選択されます。

The writer of a <application>PL/PerlU</application> function must take care that the function cannot be used to do anything unwanted, since it will be able to do anything that could be done by a user logged in as the database administrator. Note that the database system allows only database superusers to create functions in untrusted languages. PL/PerlU関数の作成者は、その関数を不必要なことに使用できないように注意する必要があります。 この関数は、データベース管理者としてログインしたユーザが実行できることを全て実行できるからです。 データベースシステムはデータベースのスーパーユーザにのみ信頼されない言語による関数作成を許可していることに注意してください。

If the above function was created by a superuser using the language <literal>plperlu</literal>, execution would succeed. 上記の関数が、スーパーユーザによってplperlu言語を使用して作成された場合、実行は可能となります。

In the same way, anonymous code blocks written in Perl can use restricted operations if the language is specified as <literal>plperlu</literal> rather than <literal>plperl</literal>, but the caller must be a superuser. 同じ方法で、言語をplperlではなくplperluと指定することで、Perl内に作成された匿名コードブロックは制限された操作を使用することができます。 ただし呼び出し元はスーパーユーザでなければなりません。

注記

While <application>PL/Perl</application> functions run in a separate Perl interpreter for each SQL role, all <application>PL/PerlU</application> functions executed in a given session run in a single Perl interpreter (which is not any of the ones used for <application>PL/Perl</application> functions). This allows <application>PL/PerlU</application> functions to share data freely, but no communication can occur between <application>PL/Perl</application> and <application>PL/PerlU</application> functions. PL/Perl関数はSQLロール毎に別々のPerlインタプリタ内で実行されますが、あるセッションで実行されるPL/PerlU関数はすべて、単一のPerlインタプリタ(PL/Perl関数用に使用されるインタプリタのいずれかではありません)内で実行されます。 これによりPL/PerlU関数はデータを自由に共有することができます。 しかしPL/Perl関数とPL/PerlU関数の間で通信することはできません。

注記

Perl cannot support multiple interpreters within one process unless it was built with the appropriate flags, namely either <literal>usemultiplicity</literal> or <literal>useithreads</literal>. (<literal>usemultiplicity</literal> is preferred unless you actually need to use threads. For more details, see the <citerefentry><refentrytitle>perlembed</refentrytitle></citerefentry> man page.) If <application>PL/Perl</application> is used with a copy of Perl that was not built this way, then it is only possible to have one Perl interpreter per session, and so any one session can only execute either <application>PL/PerlU</application> functions, or <application>PL/Perl</application> functions that are all called by the same SQL role. Perlは適切なフラグ、すなわちusemultiplicityまたはuseithreadsを付けて構築していない限り、1つのプロセス内で複数のインタプリタをサポートすることはできません。 (実際にスレッドの使用が必要でなければusemultiplicityを勧めます。 詳細はperlembedマニュアルページを参照してください。) PL/Perlがこの方法で構築されていないPerlのコピーを使用する場合、1つのセッション内で1つのPerlインタプリタしか持つことができません。 このため、1つのセッションでは、PL/PerlU関数、もしくは、すべて同一のSQLロールで呼び出されるPL/Perl関数のいずれかのみを実行することができます。