Changeset 997 for trunk/common


Ignore:
Timestamp:
01/29/09 14:55:38 (3 years ago)
Author:
strat.cristian@…
Message:

Log remote IP and X-Forwarded-For header on job submit, textblock create/edit, and attachment upload. For privacy considerations, IP addresses are only displayed to admins and helpers.

  • Contains database migration script.
  • Adds new simplified security action. (sensitive-info)
Location:
trunk/common
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/common.php

    r935 r997  
    3838define("IA_RE_USER_NAME", '[_@a-z0-9][a-z0-9_\-\.\@]*'); 
    3939 
     40// IPv4 address, doesn't check for values greater than 255. 
     41define("IA_RE_IPV4", '(?:\d{1,3}\.){3}\d{1,3}'); 
     42// Full IPv6 address, doesn't match compressed IPv6 formats. 
     43define("IA_RE_IPV6_NO_COMPRESS", '(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}'); 
     44// IP address, matches IPv4 and non-compressed IPv6. 
     45define("IA_RE_IP_ADDRESS", IA_RE_IPV4."|".IA_RE_IPV6_NO_COMPRESS); 
     46 
    4047// Valid email. A complete check is not possible, see 
    4148// http://www.regular-expressions.info/email.html 
     
    101108    } 
    102109    return true; 
     110} 
     111 
     112// Check for (apparently) valid IP address. 
     113// It will match IPv4 and only non-compressed IPv6. Doesn't check 
     114// for values greater than 255. 
     115function is_valid_ip_address($ip_address) { 
     116    return preg_match('/^'.IA_RE_IP_ADDRESS.'$/xi', $ip_address); 
    103117} 
    104118 
  • trunk/common/db/attachment.php

    r852 r997  
    6565 
    6666// Update an attachment. FIXME: hash args. 
    67 function attachment_update($id, $name, $size, $mime_type, $page, $user_id) { 
     67function attachment_update($id, $name, $size, $mime_type, $page, $user_id, 
     68        $remote_ip_info) { 
    6869    $attachment = array( 
    6970            'id' => $id, 
     
    7475            'user_id' => $user_id, 
    7576            'timestamp' => db_date_format(), 
     77            'remote_ip_info' => $remote_ip_info, 
    7678    ); 
    7779 
     
    8284 
    8385// Inserts an attachment in the db 
    84 function attachment_insert($name, $size, $mime_type, $page, $user_id) { 
     86function attachment_insert($name, $size, $mime_type, $page, $user_id, 
     87        $remote_ip_info) { 
    8588    $attachment = array( 
    8689            'name' => $name, 
     
    9093            'user_id' => $user_id, 
    9194            'timestamp' => db_date_format(), 
     95            'remote_ip_info' => $remote_ip_info, 
    9296    ); 
    9397 
     
    9599    $attachment['id'] = db_insert_id(); 
    96100    _attachment_cache_add($attachment); 
    97      
     101 
    98102    return $attachment['id']; 
    99103} 
  • trunk/common/db/db.php

    r909 r997  
    262262} 
    263263 
     264// FIXME: This shouldn't be here. Move it in common/db/task.php or 
     265// common/db/round.php 
    264266function db_get_task_filter_clause($filter, $table_alias) { 
    265267    if ($filter == IA_TLF_SOLVED) { 
  • trunk/common/db/job.php

    r996 r997  
    2222 
    2323// Creates new eval job 
    24 function job_create($task_id, $round_id, $user_id, $compiler_id, $file_contents) { 
     24function job_create($task_id, $round_id, $user_id, $compiler_id, $file_contents, 
     25        $remote_ip_info = null) { 
    2526    $query = <<<SQL 
    2627        INSERT INTO ia_job 
    27             (`task_id`, `round_id`, `user_id`, `compiler_id`, `file_contents`, `submit_time`) 
    28         VALUES (%s, %s, %s, %s, %s, %s) 
     28            (`task_id`, `round_id`, `user_id`, `compiler_id`, `file_contents`, 
     29             `submit_time`, `remote_ip_info`) 
     30        VALUES (%s, %s, %s, %s, %s, %s, %s) 
    2931SQL; 
    3032    $query = sprintf($query, 
    3133            db_quote($task_id), db_quote($round_id), db_quote($user_id), 
    32             db_quote($compiler_id), db_quote($file_contents), db_quote(db_date_format())); 
     34            db_quote($compiler_id), db_quote($file_contents), 
     35            db_quote(db_date_format()), db_quote($remote_ip_info)); 
    3336    return db_query($query); 
    3437} 
     
    4043SELECT `job`.`id`, `job`.`user_id`, `job`.`task_id`, `job`.`round_id`, 
    4144       `job`.`compiler_id`, `job`.`status`, `job`.`submit_time`, 
    42        `job`.`eval_message`, `job`.`score`, `job`.`file_contents` 
     45       `job`.`eval_message`, `job`.`score`, `job`.`file_contents`, 
     46       `job`.`remote_ip_info` 
    4347    FROM `ia_job` AS `job` 
    4448    WHERE `job`.`id` = ( 
     
    8387    log_assert(is_whole_number($job_id)); 
    8488    $field_list = "`job`.`id`, job.`user_id`, `job`.`compiler_id`, `job`.`status`, 
    85                    `job`.`submit_time`, `job`.`eval_message`, `job`.`score`, `job`.`eval_log`, 
     89                   `job`.`submit_time`, `job`.`eval_message`, `job`.`score`, 
     90                   `job`.`eval_log`, `job`.`remote_ip_info`, 
    8691                   OCTET_LENGTH(`job`.`file_contents`) AS `job_size`, 
    8792                   `user`.`username` AS `user_name`, `user`.`full_name` AS `user_fullname`, 
     
    128133    $score_end = getattr($filters, 'score_end'); 
    129134    $eval_msg = getattr($filters, 'eval_msg'); 
     135    $remote_ip_info = getattr($filters, 'remote_ip_info'); 
    130136 
    131137    $wheres = array("TRUE"); 
     
    172178    if (!is_null($eval_msg)) { 
    173179        $wheres[] = sprintf("`job`.`eval_message` LIKE '%s%%'", db_escape($eval_msg)); 
     180    } 
     181    if (!is_null($remote_ip_info)) { 
     182        // We allow remote_ip_info to contain % wildcards. This will make it a bit 
     183        // easier to search for IP classes. 
     184        $wheres[] = sprintf("`job`.`remote_ip_info` LIKE '%s'", db_escape($remote_ip_info)); 
    174185    } 
    175186 
     
    232243            `job`.`eval_message`, 
    233244            `job`.`eval_log`, 
     245            `job`.`remote_ip_info`, 
    234246            OCTET_LENGTH(`job`.`file_contents`) AS `job_size`, 
    235247            `user`.`username` AS `user_name`, 
  • trunk/common/db/round.php

    r992 r997  
    2929// Create new round 
    3030// Return success. 
    31 function round_create($round, $round_params, $user_id) { 
     31function round_create($round, $round_params, $user_id, $remote_ip_info = null) { 
    3232    log_assert(is_user_id($user_id)); 
    3333    log_assert_valid(round_validate($round)); 
     
    4545        $replace = array("round_id" => $round['id']); 
    4646        textblock_copy_replace("template/newround", $round['page_name'], 
    47                 $replace, "round: {$round['id']}", $user_id); 
     47                $replace, "round: {$round['id']}", $user_id, $remote_ip_info); 
    4848 
    4949        _round_cache_add($round); 
  • trunk/common/db/task.php

    r982 r997  
    3535 
    3636// Create new task 
    37 function task_create($task, $task_params) { 
     37function task_create($task, $task_params, $remote_ip_info = null) { 
    3838    log_assert_valid(task_validate($task)); 
    3939    log_assert_valid(task_validate_parameters($task['type'], $task_params)); 
     
    4848        $replace = array("task_id" => $task['id'], "task_title" => ucfirst($task['id'])); 
    4949        textblock_copy_replace("template/newtask", $task['page_name'], 
    50                 $replace, "task: {$task['id']}", $task['user_id']); 
     50                $replace, "task: {$task['id']}", $task['user_id'], $remote_ip_info); 
    5151 
    5252        _task_cache_add($task); 
  • trunk/common/db/textblock.php

    r987 r997  
    1515function textblock_add_revision( 
    1616        $name, $title, $content, $user_id, $security = "public", 
    17         $forum_topic = null, $timestamp = null, $creation_timestamp = null) { 
     17        $forum_topic = null, $timestamp = null, $creation_timestamp = null, 
     18        $remote_ip_info = null) { 
    1819    $name = normalize_page_name($name); 
    1920    $tb = array( 
     
    2627            'timestamp' => $timestamp, 
    2728            'creation_timestamp' => $creation_timestamp, 
     29            'remote_ip_info' => $remote_ip_info, 
    2830    ); 
    2931    log_assert_valid(textblock_validate($tb)); 
     
    5961    } 
    6062    $query = sprintf("INSERT INTO ia_textblock 
    61                         (name, `text`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`, `forum_topic`) 
    62                       VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %s)", 
    63                      db_escape($name), db_escape($content), 
    64                      db_escape($title), db_escape($creation_timestamp),  
    65                      db_escape($timestamp), db_escape($user_id), 
    66                      db_escape($security), 
    67                      is_null($forum_topic) ? "NULL" : db_escape($forum_topic)); 
     63            (name, `text`, `title`, `creation_timestamp`, 
     64                    `timestamp`, `user_id`, `security`, `forum_topic`, 
     65                    `remote_ip_info`) 
     66            VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %s, %s)", 
     67            db_escape($name), db_escape($content), db_escape($title), 
     68            db_escape($creation_timestamp), db_escape($timestamp), 
     69            db_escape($user_id), db_escape($security), db_quote($forum_topic), 
     70            db_quote($remote_ip_info)); 
    6871    return db_query($query); 
    6972} 
     
    7477    // log_print_r($options); 
    7578 
    76     $field_list = "`name`, `title`, `creation_timestamp`, `timestamp`, `security`, `user_id`, `forum_topic`"; 
     79    $field_list = "`name`, `title`, `creation_timestamp`, `timestamp`, `security`, `user_id`, 
     80            `forum_topic`, `remote_ip_info`"; 
    7781 
    7882    // Select content. 
     
    212216        $compare = "REGEXP"; 
    213217    } 
    214     $query = sprintf("SELECT `name`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`, `forum_topic` 
     218    $query = sprintf("SELECT `name`, `title`, `creation_timestamp`, `timestamp`, 
     219                            `user_id`, `security`, `forum_topic`, 
     220                            `remote_ip_info` 
    215221                      FROM ia_textblock 
    216222                      WHERE `name` LIKE '%s' AND 
     
    289295} 
    290296 
    291 function textblock_copy($old_name, $new_name) { 
     297function textblock_copy($old_name, $new_name, $user_id, $remote_ip_info) { 
    292298    $old_name = normalize_page_name($old_name); 
    293299    $new_name = normalize_page_name($new_name); 
     
    297303    $new_textblock = textblock_get_revision($old_name); 
    298304    $new_textblock['name'] = $new_name; 
    299     $new_textblock['user_id'] = identity_get_user_id(); 
     305    $new_textblock['user_id'] = $user_id; 
    300306    textblock_add_revision($new_textblock['name'], $new_textblock['title'], 
    301307                           $new_textblock['text'], $new_textblock['user_id'], 
    302308                           $new_textblock['security'], 
    303                            $new_textblock['forum_topic'], null, null); 
     309                           $new_textblock['forum_topic'], null, null, 
     310                           $remote_ip_info); 
    304311 
    305312    // Get a list of attachments. 
     
    309316    foreach ($files as $file) { 
    310317        // Copy in db and get new id 
    311         $new_id = attachment_insert($file['name'], $file['size'], $file['mime_type'], $new_name, identity_get_user_id()); 
     318        $new_id = attachment_insert($file['name'], $file['size'], 
     319                $file['mime_type'], $new_name, $user_id, $remote_ip_info); 
    312320 
    313321        // Copy on hard drive 
  • trunk/common/db/user.php

    r911 r997  
    150150    $replace = array("user_id" => $user['username']); 
    151151    textblock_copy_replace("template/newuser", IA_USER_TEXTBLOCK_PREFIX.$user['username'], 
    152                            $replace, "public", $new_user['id']); 
     152                           $replace, "public", $new_user['id'], null); 
    153153 
    154154    // Create SMF user 
  • trunk/common/job.php

    r993 r997  
    7878        if (array_key_exists('round_id', $args)) { 
    7979            job_create($args['task_id'], $args['round_id'], $user['id'], 
    80                     $args['compiler_id'], $args['solution']); 
     80                    $args['compiler_id'], $args['solution'], 
     81                    getattr($args, 'remote_ip_info')); 
    8182        } else { 
    8283            $parent_rounds = task_get_parent_rounds($args['task_id']); 
     
    8485                // some jobs just don't have a round 
    8586                job_create($args['task_id'], '', $user['id'], 
    86                         $args['compiler_id'], $args['solution']); 
     87                        $args['compiler_id'], $args['solution'], 
     88                        getattr($args, 'remote_ip_info')); 
    8789            } 
    8890            else { 
     
    9092                    if (security_query($user, 'round-submit', round_get($round_id))) { 
    9193                        job_create($args['task_id'], $round_id, $user['id'], 
    92                                 $args['compiler_id'], $args['solution']); 
     94                                $args['compiler_id'], $args['solution'], 
     95                                getattr($args, 'remote_ip_info')); 
    9396                    } 
    9497                } 
  • trunk/common/log.php

    r934 r997  
    112112 
    113113// Use this for warning messages. 
    114 function log_warn($message, $include_origin = false) { 
     114function log_warn($message, $include_origin = false, $backtrace_level = 0) { 
    115115    if ($include_origin) { 
    116         $message = format_message_backtrace($message); 
     116        $message = format_message_backtrace($message, $backtrace_level); 
    117117    } 
    118118    trigger_error_split($message, E_USER_WARNING); 
  • trunk/common/security.php

    r996 r997  
    103103        case 'round-register-view': 
    104104            return 'simple-view'; 
     105 
     106        // View IP. 
     107        case 'attach-view-ip': 
     108        case 'textblock-view-ip': 
     109        case 'job-view-ip': 
     110            return 'sensitive-info'; 
    105111 
    106112        // Reversible edits access. 
     
    245251            } 
    246252 
     253        case 'sensitive-info': 
     254            return ($usersec == 'admin' || $usersec == 'helper'); 
     255 
    247256        // Reversible modifications. 
    248257        case 'simple-rev-edit': 
     
    410419            } 
    411420            return $can_view || $is_owner || $is_admin; 
     421 
     422        case 'sensitive-info': 
     423            return ($usersec == 'admin' || $usersec == 'helper'); 
    412424 
    413425        default: 
     
    466478            } 
    467479 
     480        case 'sensitive-info': 
     481            return ($usersec == 'admin' || $usersec == 'helper'); 
     482 
    468483        default: 
    469484            log_error('Invalid round action: '.$action); 
     
    520535    $can_view_score = ($job['round_public_eval'] == true) || $is_task_owner || $is_admin; 
    521536    $can_view_partial_feedback = $is_owner || $is_admin; 
     537    $can_view_sensitive_info = ($usersec == 'admin' || $usersec == 'helper'); 
    522538 
    523539    // Log query response. 
     
    550566            return $can_view_job && $can_view_partial_feedback; 
    551567 
     568        case 'sensitive-info': 
     569            return $can_view_job && $can_view_sensitive_info; 
     570 
    552571        default: 
    553572            log_error('Invalid job action: '.$action); 
  • trunk/common/textblock.php

    r970 r997  
    149149// 
    150150// Use this like textblock_copy_replace('template/newtask', 'problema/capsuni'); 
    151 function textblock_copy_replace($srcprefix, $dstprefix, $replace, $security, $user_id) 
    152 { 
     151function textblock_copy_replace($srcprefix, $dstprefix, $replace, $security, 
     152        $user_id, $remote_ip_info = null) { 
    153153    assert($srcprefix != $dstprefix); 
    154154    assert(is_textblock_security_descriptor($security)); 
     
    174174                $textblock['text'], $user_id, $textblock['security'], 
    175175                $textblock['forum_topic'], null, 
    176                 $first_textblock['creation_timestamp']); 
     176                $first_textblock['creation_timestamp'], $remote_ip_info); 
    177177    } 
    178178} 
Note: See TracChangeset for help on using the changeset viewer.