crash_reporter.php 8.13 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2015 Franco Fichtner <franco@opnsense.org>
5
	Copyright (C) 2014 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	Copyright (C) 2011 Scott Ullrich
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

31
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32

33
function upload_crash_report($files, $agent)
34
{
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    $post = array();
    $counter = 0;

    foreach ($files as $filename) {
        if (is_link($filename) || $filename == '/var/crash/minfree.gz' || $filename == '/var/crash/bounds.gz') {
            continue;
        }
        $post["file{$counter}"] = curl_file_create($filename, "application/x-gzip", basename($filename));
        $counter++;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://crash.opnsense.org/');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data;' ));
    $response = curl_exec($ch);
    curl_close($ch);

    return !$response;
Ad Schellevis's avatar
Ad Schellevis committed
60 61 62 63
}

include('head.inc');

64
$last_version = '/usr/local/opnsense/version/opnsense.last';
65
$crash_report_header = sprintf(
66 67 68 69 70 71 72 73
    "%s\n%s %s%s %s (%s)\nUUID %s\n",
    php_uname('v'),
    $g['product_name'],
    trim(file_get_contents('/usr/local/opnsense/version/opnsense')),
    file_exists($last_version) ? sprintf(' [%s]', trim(file_get_contents($last_version))) : '',
    trim(shell_exec('/usr/local/bin/openssl version')),
    php_uname('m'),
    shell_exec('/sbin/sysctl -b kern.hostuuid')
74
);
Ad Schellevis's avatar
Ad Schellevis committed
75

76 77 78 79
if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $crash_report_header .= "User Agent {$_SERVER['HTTP_USER_AGENT']}\n";
}

80 81
$pkgver = explode('-', trim(file_get_contents('/usr/local/opnsense/version/opnsense')));
$user_agent = $g['product_name'] . '/' . $pkgver[0];
82
$crash_reports = array();
83
$has_crashed = false;
84

85
if (isset($_POST['Submit'])) {
86 87 88 89 90 91 92
    if ($_POST['Submit'] == 'yes') {
        if (!is_dir('/var/crash')) {
            mkdir('/var/crash', 0750, true);
        }
        $email = trim($_POST['Email']);
        if (!empty($email)) {
            $crash_report_header .= "Email {$email}\n";
93 94 95 96 97 98 99 100
            if (!isset($config['system']['contact_email']) ||
                $config['system']['contact_email'] !== $email) {
                $config['system']['contact_email'] = $email;
                write_config('Updated crash reporter contact email.');
            }
        } elseif (isset($config['system']['contact_email'])) {
            unset($config['system']['contact_email']);
            write_config('Removed crash reporter contact email.');
101 102 103 104 105 106 107 108 109 110
        }
        $desc = trim($_POST['Desc']);
        if (!empty($desc)) {
            $crash_report_header .= "Description\n\n{$desc}";
        }
        file_put_contents('/var/crash/crashreport_header.txt', $crash_report_header);
        @rename('/tmp/PHP_errors.log', '/var/crash/PHP_errors.log');
        @copy('/var/run/dmesg.boot', '/var/crash/dmesg.boot');
        exec('/usr/bin/gzip /var/crash/*');
        $files_to_upload = glob('/var/crash/*');
111 112 113
        upload_crash_report($files_to_upload, $user_agent);
        foreach ($files_to_upload as $file_to_upload) {
            @unlink($file_to_upload);
114
	      }
115
    } elseif ($_POST['Submit'] == 'no') {
116 117 118 119
        $files_to_upload = glob('/var/crash/*');
        foreach ($files_to_upload as $file_to_upload) {
            @unlink($file_to_upload);
        }
120 121
        @unlink('/tmp/PHP_errors.log');
    } elseif ($_POST['Submit'] == 'new') {
122 123
          /* force a crash report generation */
          $has_crashed = true;
124
    }
125
} else {
126 127
    /* if there is no user activity probe for a crash report */
    $has_crashed = get_crash_report(true) != '';
128
}
129

130 131
$email = isset($config['system']['contact_email']) ? $config['system']['contact_email'] : '';

132
if ($has_crashed) {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    $crash_files = glob("/var/crash/*");
    $crash_reports['System Information'] = trim($crash_report_header);
    $php_errors = @file_get_contents('/tmp/PHP_errors.log');
    if (!empty($php_errors)) {
        $crash_reports['PHP Errors'] = trim($php_errors);
    }
    $dmesg_boot = @file_get_contents('/var/run/dmesg.boot');
    if (!empty($dmesg_boot)) {
        $crash_reports['dmesg.boot'] = trim($dmesg_boot);
    }
    foreach ($crash_files as $cf) {
        if (!is_link($cf) && $cf != '/var/crash/minfree' && $cf != '/var/crash/bounds' && filesize($cf) < 450000) {
            $crash_reports[$cf] = trim(file_get_contents($cf));
        }
    }
148 149
}

150 151 152 153 154 155 156 157
$message = gettext('Luckily we have not detected a programming bug.');
if (isset($_POST['Submit'])) {
    if ($_POST['Submit'] == 'yes') {
        $message = gettext('Thank you for submitting this crash report.');
    } elseif ($_POST['Submit'] == 'no') {
        $message = gettext('Please consider submitting a crash report if the error persists.');
    }
}
158 159 160 161 162 163 164
?>

<body>

<?php include("fbegin.inc"); ?>

<section class="page-content-main">
165 166 167 168 169 170
  <div class="container-fluid">
    <div class="row">
      <section class="col-xs-12">
        <div class="content-box">
          <form action="/crash_reporter.php" method="post">
            <div class="col-xs-12">
171
<?php
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            if ($has_crashed):?>
              <br/><button name="Submit" type="submit" class="btn btn-default pull-right" value="no"><?=gettext('Dismiss this report');?></button>
              <button name="Submit" type="submit" class="btn btn-primary pull-right" style="margin-right: 8px;" value="yes"><?=gettext('Submit this report');?></button>
              <p><strong><?=gettext("Unfortunately we have detected at least one programming bug.");?></strong></p>
              <p><?=gettext("Would you like to submit this crash report to the developers?");?></p>
              <hr><p><?=gettext('You can help us further by adding your contact information and a problem description. ' .
                  'Please note that providing your contact information greatly improves the chances of bugs being fixed.');?></p>
              <p><input type="text" placeholder="<?=gettext('your@email.com');?>" name="Email" value="<?=$email;?>"></p>
              <p><textarea rows="5" placeholder="<?=gettext('A short problem description or steps to reproduce.');?>" name="Desc"></textarea></p>
              <hr><p><?=gettext("Please double-check the following contents to ensure you are comfortable submitting the following information.");?></p>
<?php
              foreach ($crash_reports as $report => $content):?>
                  <p>
                    <?=$report;?>:<br/>
                    <pre><?=$content;?></pre>
                  </p>
<?php
              endforeach;
            else:?>
191

192 193 194 195 196 197
              <br/><button name="Submit" type="submit" class="btn btn-primary pull-right" value="new"><?=gettext('Report an issue');?></button>
              <p><strong><?=$message;?></strong></p><br/>
<?php
            endif;?>
            </div>
          </form>
198
        </div>
199
      </section>
200
    </div>
201
  </div>
202
</section>
203

204
<?php include("foot.inc");