DokuWiki

It's better when it's simple

ユーザ用ツール

サイト用ツール


ja:tips:clean_acl

アクセス制御リストを掃除する

ページや名前空間、ユーザーが削除されたとき、それらに対する権限の設定はアクセス制御リスト (ACL) の中に残されます。これはページやユーザーが再び作成された場合の潜在的なセキュリティリスクとなります。以下に示すスクリプト (DokuWiki の bin フォルダに設置します) は、ACL を掃除してきれいにします。このスクリプトを実行するには、まず古い ACL ファイルのバックアップ先となる conf/backup フォルダを作成しなければなりません。Wiki の利用形態により、日次、週次、もしくは月次の cron ジョブとして実行することができます。

clean_acl.php
#!/usr/bin/php
<?php
#------------------------------------------------------------------------------
if(!defined('NOSESSION')) define('NOSESSION', true);
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once DOKU_INC.'inc/init.php';
require_once DOKU_INC.'inc/common.php';
require_once DOKU_INC.'inc/cliopts.php';
 
// handle options
$short_opts = 'hq';
$long_opts  = array('help', 'quiet');
$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
if ( $OPTS->isError() ) {
    fwrite( STDERR, $OPTS->getMessage() . "\n");
    _usage();
    exit(1);
}
 
$QUIET = false;
foreach ($OPTS->options as $key => $val) {
    switch ($key) {
        case 'h':
        case 'help':
            _usage();
            exit;
    case 'q':
        case 'quiet':
      $QUIET = true;
      break;
    }
}
 
#------------------------------------------------------------------------------

function _usage() {
    print "Usage: clean_acl.php <options>
 
  Removes entries from acl.auth.php referencing page ids that 
  don't exist anymore.
 
    OPTIONS
        -h, --help     show this help and exit
        -q, --quiet    don't produce any output
";
}
 
#------------------------------------------------------------------------------
function clean_acl()
{
  $acls_name = DOKU_CONF.'/acl.auth.php';
  $acls = file($acls_name);
  $new_acls = fopen(DOKU_CONF.'/acl.auth.new.php', 'w');
  $msg = "Removed: '%s' (%s).\n";
  $was_changed = false;
  foreach($acls as $line)
  {
    if(trim($line) && !preg_match('/^#/', $line))
    {
      if(id_exists($line))
      {
        if(user_exists($line))
        {
          fwrite($new_acls, $line);
        }
        else
        {
          _quietecho(sprintf($msg, trim($line), 'user does not exist'));
          $was_changed = true;
        }
      }
      else
      {
        _quietecho(sprintf($msg, trim($line), 'page does not exist'));
         $was_changed = true;
      }
    }
    else
    {
      fwrite($new_acls, $line);
    }
  }
  fclose($new_acls);
  //die();
  if($was_changed)
  {
    $ok = @rename(DOKU_CONF.'/acl.auth.php', DOKU_CONF.'/backup/acl.auth.'.date('Y-m-d_His').'.php');
    if($ok)
      $ok = @rename(DOKU_CONF.'/acl.auth.new.php',  DOKU_CONF.'/acl.auth.php');
    else
      _quietecho('Could not rename old acl file.');
  }
  else
  {
    @unlink(DOKU_CONF.'/acl.auth.new.php');
  }
 
}
 
#------------------------------------------------------------------------------
function id_exists($acl_line)
{
  $access = preg_split("/\s/", $acl_line);
  // "All"
  if($access[0]=="*")
  {
    return true;
  }
  // Namespace
  elseif(preg_match('/(.*):\*$/', $access[0], $matches))
  {
    $fn = str_replace(".txt", "", wikiFN($matches[1]));
  }
  // Page
  else
  {
    $fn = wikiFN($access[0]);
  }
  return file_exists($fn);
}
 
function user_exists($line)
{
  static $usernames = null;
  if(is_null($usernames))
  {
    $usernames = array();
    foreach(file(DOKU_CONF.'/users.auth.php') as $userline)
    {
      if($userline[0] == '#')
        continue;
      $line_arr = explode(':', $userline);
      if(trim($line_arr[0]))
        $usernames[] = trim($line_arr[0]);
    }
  }
  list(,$user) = explode("\t", $line);
  if($user[0] == '@')
    return true;
  return in_array(rawurldecode($user), $usernames);
}
 
function _quietecho($msg)
{
  global $QUIET;
  if(!$QUIET)
    echo $msg;
}
 
clean_acl();
?>
ja/tips/clean_acl.txt · 最終更新: 2010-01-05 16:46 by kazmiya

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki