Commit 1832a5a3 authored by Franco Fichtner's avatar Franco Fichtner

src: zap easy occurrences of unlink_if_exists()

... and refactor the code left and right.  Someone's got to do it.
parent ddc2cd37
...@@ -342,9 +342,8 @@ function local_sync_accounts() ...@@ -342,9 +342,8 @@ function local_sync_accounts()
* If a crontab was created to user, pw userdel will be interactive and * If a crontab was created to user, pw userdel will be interactive and
* can cause issues. Just remove crontab before run it when necessary * can cause issues. Just remove crontab before run it when necessary
*/ */
unlink_if_exists("/var/cron/tabs/{$line[0]}"); @unlink("/var/cron/tabs/{$line[0]}");
$cmd = "/usr/sbin/pw userdel -n '{$line[0]}'"; mwexecf('/usr/sbin/pw userdel -n %s', $line[0]);
mwexec($cmd);
} }
pclose($fd); pclose($fd);
} }
...@@ -364,8 +363,7 @@ function local_sync_accounts() ...@@ -364,8 +363,7 @@ function local_sync_accounts()
if ($line[2] > 65000) { if ($line[2] > 65000) {
continue; continue;
} }
$cmd = "/usr/sbin/pw groupdel {$line[2]}"; mwexecf('/usr/sbin/pw groupdel %s', $line[2]);
mwexec($cmd);
} }
pclose($fd); pclose($fd);
} }
...@@ -477,12 +475,11 @@ function local_user_set(&$user) ...@@ -477,12 +475,11 @@ function local_user_set(&$user)
$keys = base64_decode($user['authorizedkeys']); $keys = base64_decode($user['authorizedkeys']);
@file_put_contents("{$user_home}/.ssh/authorized_keys", $keys); @file_put_contents("{$user_home}/.ssh/authorized_keys", $keys);
@chown("{$user_home}/.ssh/authorized_keys", $user_name); @chown("{$user_home}/.ssh/authorized_keys", $user_name);
} else } else {
unlink_if_exists("{$user_home}/.ssh/authorized_keys"); @unlink("{$user_home}/.ssh/authorized_keys");
}
$un = $lock_account ? "" : "un";
exec("/usr/sbin/pw {$un}lock {$user_name} -q");
mwexecf('/usr/sbin/pw %s %s -q', array($lock_account ? 'lock' : 'unlock', $user_name));
} }
function local_user_del($user) function local_user_del($user)
...@@ -622,23 +619,20 @@ function local_group_set($group, $reset = false) ...@@ -622,23 +619,20 @@ function local_group_set($group, $reset = false)
pclose($fd); pclose($fd);
/* determine add or mod */ /* determine add or mod */
if (!strncmp($pwread, "pw:", 3)) if (!strncmp($pwread, "pw:", 3)) {
$group_op = "groupadd"; $group_op = "groupadd";
else } else {
$group_op = "groupmod"; $group_op = "groupmod";
}
/* add or mod group db */ /* add or mod group db */
$cmd = "/usr/sbin/pw {$group_op} {$group_name} -g {$group_gid} -M {$group_members} 2>&1"; mwexecf('/usr/sbin/pw %s %s -g %s -M %s', array($group_op, $group_name, $group_gid, $group_members));
mwexec($cmd);
} }
function local_group_del($group) function local_group_del($group)
{ {
/* delete from group db */ /* delete from group db */
$cmd = "/usr/sbin/pw groupdel {$group['name']}"; mwexecf('/usr/sbin/pw groupdel %s', $group['name']);
mwexec($cmd);
} }
function ldap_test_connection($authcfg) function ldap_test_connection($authcfg)
...@@ -1462,5 +1456,3 @@ function http_basic_auth($http_auth_header) ...@@ -1462,5 +1456,3 @@ function http_basic_auth($http_auth_header)
// not authenticated // not authenticated
return false; return false;
} }
?>
...@@ -2371,7 +2371,7 @@ function interface_wireless_configure($if, &$wl, &$wlcfg) { ...@@ -2371,7 +2371,7 @@ function interface_wireless_configure($if, &$wl, &$wlcfg) {
*/ */
// Remove script file // Remove script file
unlink_if_exists("/tmp/{$if}_setup.sh"); @unlink("/tmp/{$if}_setup.sh");
// Clone wireless nic if needed. // Clone wireless nic if needed.
interface_wireless_clone($if, $wl); interface_wireless_clone($if, $wl);
...@@ -5198,7 +5198,7 @@ EOD; ...@@ -5198,7 +5198,7 @@ EOD;
chmod($cron_file, 0755); chmod($cron_file, 0755);
killbypid('/var/run/cron.pid', 'HUP'); killbypid('/var/run/cron.pid', 'HUP');
} else { } else {
unlink_if_exists($cron_file); @unlink($cron_file);
} }
} }
......
...@@ -128,7 +128,7 @@ function close_notice($id) ...@@ -128,7 +128,7 @@ function close_notice($id)
$ids = array(); $ids = array();
if(!$notices = get_notices()) return; if(!$notices = get_notices()) return;
if($id == "all") { if($id == "all") {
unlink_if_exists('/tmp/notices'); @unlink('/tmp/notices');
return; return;
} }
foreach(array_keys($notices) as $time) { foreach(array_keys($notices) as $time) {
...@@ -151,13 +151,10 @@ function close_notice($id) ...@@ -151,13 +151,10 @@ function close_notice($id)
fwrite($queueout, serialize($notices)); fwrite($queueout, serialize($notices));
fclose($queueout); fclose($queueout);
} else { } else {
unlink_if_exists('/tmp/notices'); @unlink('/tmp/notices');
} }
return;
} }
/****f* notices/print_notices /****f* notices/print_notices
* NAME * NAME
* print_notices * print_notices
...@@ -360,5 +357,3 @@ function notify_all_remote($msg) { ...@@ -360,5 +357,3 @@ function notify_all_remote($msg) {
notify_via_smtp($msg); notify_via_smtp($msg);
notify_via_growl($msg); notify_via_growl($msg);
} }
?>
...@@ -873,14 +873,9 @@ function openvpn_delete($mode, & $settings) ...@@ -873,14 +873,9 @@ function openvpn_delete($mode, & $settings)
@array_map('unlink', glob("{$g['varetc_path']}/openvpn/{$mode_id}.*")); @array_map('unlink', glob("{$g['varetc_path']}/openvpn/{$mode_id}.*"));
} }
function openvpn_cleanup_csc($common_name) { function openvpn_cleanup_csc($common_name)
global $g, $config; {
if (empty($common_name)) @unlink('/var/etc/openvpn-csc/' . basename($common_name));
return;
$fpath = "{$g['varetc_path']}/openvpn-csc/" . basename($common_name);
if (is_file($fpath))
unlink_if_exists($fpath);
return;
} }
function openvpn_resync_csc(& $settings) { function openvpn_resync_csc(& $settings) {
...@@ -889,7 +884,7 @@ function openvpn_resync_csc(& $settings) { ...@@ -889,7 +884,7 @@ function openvpn_resync_csc(& $settings) {
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name']; $fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
if (isset($settings['disable'])) { if (isset($settings['disable'])) {
unlink_if_exists($fpath); @unlink($fpath);
return; return;
} }
openvpn_create_dirs(); openvpn_create_dirs();
...@@ -941,20 +936,17 @@ function openvpn_resync_csc(& $settings) { ...@@ -941,20 +936,17 @@ function openvpn_resync_csc(& $settings) {
chgrp($fpath, 'nobody'); chgrp($fpath, 'nobody');
} }
function openvpn_delete_csc(& $settings) { function openvpn_delete_csc(&$settings)
global $g, $config; {
@unlink("/var/etc/openvpn-csc/{$settings['common_name']}");
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
unlink_if_exists($fpath);
} }
// Resync the configuration and restart the VPN function openvpn_resync($mode, $settings)
function openvpn_resync($mode, $settings) { {
openvpn_reconfigure($mode, $settings); openvpn_reconfigure($mode, $settings);
openvpn_restart($mode, $settings); openvpn_restart($mode, $settings);
} }
// Resync and restart all VPNs
function openvpn_resync_all($interface = '') function openvpn_resync_all($interface = '')
{ {
global $g, $config; global $g, $config;
......
...@@ -686,8 +686,8 @@ function reload_all_sync() { ...@@ -686,8 +686,8 @@ function reload_all_sync() {
system_ntp_configure(); system_ntp_configure();
/* sync pw database */ /* sync pw database */
unlink_if_exists("/etc/spwd.db.tmp"); @unlink('/etc/spwd.db.tmp');
mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd"); mwexec('/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd');
/* restart sshd */ /* restart sshd */
mwexec_bg('/usr/local/etc/rc.sshd'); mwexec_bg('/usr/local/etc/rc.sshd');
...@@ -1657,18 +1657,19 @@ function process_alias_urltable($name, $url, $freq, $forceupdate=false) { ...@@ -1657,18 +1657,19 @@ function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
|| $forceupdate) { || $forceupdate) {
// Try to fetch the URL supplied // Try to fetch the URL supplied
unlink_if_exists($urltable_filename . ".tmp"); @unlink("{$urltable_filename}.tmp");
$verify_ssl = isset($config['system']['checkaliasesurlcert']); $verify_ssl = isset($config['system']['checkaliasesurlcert']);
if (download_file($url, $urltable_filename . ".tmp", $verify_ssl)) { if (download_file($url, "{$urltable_filename}.tmp", $verify_ssl)) {
mwexec("/usr/bin/sed -E 's/\;.*//g; /^[[:space:]]*($|#)/d' ". escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename)); mwexec("/usr/bin/sed -E 's/\;.*//g; /^[[:space:]]*($|#)/d' ". escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
if (alias_get_type($name) == "urltable_ports") { if (alias_get_type($name) == "urltable_ports") {
$ports = explode("\n", file_get_contents($urltable_filename)); $ports = explode("\n", file_get_contents($urltable_filename));
$ports = group_ports($ports); $ports = group_ports($ports);
file_put_contents($urltable_filename, implode("\n", $ports)); file_put_contents($urltable_filename, implode("\n", $ports));
} }
unlink_if_exists($urltable_filename . ".tmp"); @unlink("{$urltable_filename}.tmp");
} else } else {
touch($urltable_filename); touch($urltable_filename);
}
return true; return true;
} else { } else {
// File exists, and it doesn't need updated. // File exists, and it doesn't need updated.
......
...@@ -30,7 +30,7 @@ function dump_rrd_to_xml($rrddatabase, $xmldumpfile) ...@@ -30,7 +30,7 @@ function dump_rrd_to_xml($rrddatabase, $xmldumpfile)
{ {
$rrdtool = '/usr/local/bin/rrdtool'; $rrdtool = '/usr/local/bin/rrdtool';
unlink_if_exists($xmldumpfile); @unlink($xmldumpfile);
exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret); exec("$rrdtool dump " . escapeshellarg($rrddatabase) . " {$xmldumpfile} 2>&1", $dumpout, $dumpret);
if ($dumpret <> 0) { if ($dumpret <> 0) {
...@@ -77,7 +77,7 @@ function restore_rrd() ...@@ -77,7 +77,7 @@ function restore_rrd()
} }
/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */ /* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
if (!isset($config['system']['use_mfs_tmpvar'])) { if (!isset($config['system']['use_mfs_tmpvar'])) {
unlink_if_exists('/conf/rrd.tgz'); @unlink('/conf/rrd.tgz');
} }
return true; return true;
} }
......
...@@ -388,7 +388,7 @@ function services_dhcpdv4_configure() ...@@ -388,7 +388,7 @@ function services_dhcpdv4_configure()
} }
/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */ /* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
if (!isset($config['system']['use_mfs_tmpvar'])) { if (!isset($config['system']['use_mfs_tmpvar'])) {
unlink_if_exists('/conf/dhcpleases.tgz'); @unlink('/conf/dhcpleases.tgz');
} }
} }
...@@ -1008,7 +1008,7 @@ function services_dhcpdv6_configure($blacklist = array()) ...@@ -1008,7 +1008,7 @@ function services_dhcpdv6_configure($blacklist = array())
} }
/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */ /* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
if (isset($config['system']['use_mfs_tmpvar'])) { if (isset($config['system']['use_mfs_tmpvar'])) {
unlink_if_exists('/conf/dhcp6leases.tgz'); @unlink('/conf/dhcp6leases.tgz');
} }
} }
......
...@@ -1264,7 +1264,7 @@ function system_ntp_setup_gps($serialport) { ...@@ -1264,7 +1264,7 @@ function system_ntp_setup_gps($serialport) {
return false; return false;
// Create symlink that ntpd requires // Create symlink that ntpd requires
unlink_if_exists($gps_device); @unlink($gps_device);
symlink($serialport, $gps_device); symlink($serialport, $gps_device);
/* Send the following to the GPS port to initialize the GPS */ /* Send the following to the GPS port to initialize the GPS */
...@@ -1306,23 +1306,22 @@ function system_ntp_setup_gps($serialport) { ...@@ -1306,23 +1306,22 @@ function system_ntp_setup_gps($serialport) {
return true; return true;
} }
function system_ntp_setup_pps($serialport) { function system_ntp_setup_pps($serialport)
global $config, $g; {
$pps_device = '/dev/pps0'; $pps_device = '/dev/pps0';
$serialport = '/dev/'.$serialport; $serialport = "/dev/{$serialport}";
if (!file_exists($serialport)) if (!file_exists($serialport)) {
return false; return false;
}
// Create symlink that ntpd requires // Create symlink that ntpd requires
unlink_if_exists($pps_device); @unlink($pps_device);
@symlink($serialport, $pps_device); @symlink($serialport, $pps_device);
return true; return true;
} }
function system_ntp_configure($start_ntpd = true) function system_ntp_configure($start_ntpd = true)
{ {
global $config, $g; global $config, $g;
......
...@@ -2030,7 +2030,6 @@ function upgrade_054_to_055() { ...@@ -2030,7 +2030,6 @@ function upgrade_054_to_055() {
echo "Updating configuration..."; echo "Updating configuration...";
} }
function upgrade_055_to_056() { function upgrade_055_to_056() {
global $config; global $config;
...@@ -2526,10 +2525,10 @@ function upgrade_077_to_078() { ...@@ -2526,10 +2525,10 @@ function upgrade_077_to_078() {
$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units']; $config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
} }
} }
function upgrade_078_to_079() { function upgrade_078_to_079()
global $g; {
/* Delete old and unused RRD file */ /* Delete old and unused RRD file */
unlink_if_exists("/var/db/rrd/captiveportal-totalusers.rrd"); @unlink('/var/db/rrd/captiveportal-totalusers.rrd');
} }
function upgrade_079_to_080() { function upgrade_079_to_080() {
...@@ -2692,8 +2691,8 @@ function upgrade_080_to_081() { ...@@ -2692,8 +2691,8 @@ function upgrade_080_to_081() {
mwexec("$rrdtool restore -f /tmp/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1"); mwexec("$rrdtool restore -f /tmp/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
unset($xml); unset($xml);
# Default /tmp tmpfs is ~40mb, do not leave temp files around # Default /tmp tmpfs is ~40mb, do not leave temp files around
unlink_if_exists("/tmp/{$xmldump}"); @unlink("/tmp/{$xmldump}");
unlink_if_exists("/tmp/{$xmldumpnew}"); @unlink("/tmp/{$xmldumpnew}");
} }
enable_rrd_graphing(); enable_rrd_graphing();
/* Let's save the RRD graphs after we run enable RRD graphing */ /* Let's save the RRD graphs after we run enable RRD graphing */
...@@ -3254,13 +3253,13 @@ function upgrade_110_to_111() ...@@ -3254,13 +3253,13 @@ function upgrade_110_to_111()
mwexec('/usr/sbin/pw useradd -n unbound -c "Unbound DNS Resolver" -d /var/unbound -s /usr/sbin/nologin -u 59 -g 59', true); mwexec('/usr/sbin/pw useradd -n unbound -c "Unbound DNS Resolver" -d /var/unbound -s /usr/sbin/nologin -u 59 -g 59', true);
/* cleanup old unbound package stuffs */ /* cleanup old unbound package stuffs */
unlink_if_exists("/usr/local/pkg/unbound.xml"); @unlink('/usr/local/pkg/unbound.xml');
unlink_if_exists("/usr/local/pkg/unbound.inc"); @unlink('/usr/local/pkg/unbound.inc');
unlink_if_exists("/usr/local/pkg/unbound_advanced.xml"); @unlink('/usr/local/pkg/unbound_advanced.xml');
unlink_if_exists("/usr/local/www/unbound_status.php"); @unlink('/usr/local/www/unbound_status.php');
unlink_if_exists("/usr/local/www/unbound_acls.php"); @unlink('/usr/local/www/unbound_acls.php');
unlink_if_exists("/usr/local/bin/unbound_monitor.sh"); @unlink('/usr/local/bin/unbound_monitor.sh');
unlink_if_exists("/usr/local/etc/rc.d/unbound.sh"); @unlink('/usr/local/etc/rc.d/unbound.sh');
/* Remove old menu and service entries */ /* Remove old menu and service entries */
if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) { if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
......
...@@ -937,9 +937,9 @@ function vpn_pptpd_configure() { ...@@ -937,9 +937,9 @@ function vpn_pptpd_configure() {
} }
/* remove mpd.conf, if it exists */ /* remove mpd.conf, if it exists */
unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.conf"); @unlink('/var/etc/pptp-vpn/mpd.conf');
unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.links"); @unlink('/var/etc/pptp-vpn/mpd.links');
unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.secret"); @unlink('/var/etc/pptp-vpn/mpd.secret');
} }
if (empty($pptpdcfg['n_pptp_units'])) { if (empty($pptpdcfg['n_pptp_units'])) {
......
...@@ -557,5 +557,5 @@ function cleanup_lb_marked() ...@@ -557,5 +557,5 @@ function cleanup_lb_marked()
} }
} }
unlink_if_exists($filename); @unlink($filename);
} }
...@@ -63,9 +63,9 @@ function openvpn_client_export_deinstall() ...@@ -63,9 +63,9 @@ function openvpn_client_export_deinstall()
$ovpndir = "/usr/local/share/openvpn"; $ovpndir = "/usr/local/share/openvpn";
$workdir = "{$ovpndir}/client-export"; $workdir = "{$ovpndir}/client-export";
unlink_if_exists("/usr/local/www/{$phpfile}"); @unlink("/usr/local/www/{$phpfile}");
unlink_if_exists("/usr/local/www/{$phpfile2}"); @unlink("/usr/local/www/{$phpfile2}");
unlink_if_exists("/usr/local/pkg/openvpn-client-export-{$current_openvpn_version}.tgz"); @unlink("/usr/local/pkg/openvpn-client-export-{$current_openvpn_version}.tgz");
exec("/bin/rm -r {$workdir}"); exec("/bin/rm -r {$workdir}");
} }
......
...@@ -55,7 +55,7 @@ if (isset($_POST['backupcount'])) { ...@@ -55,7 +55,7 @@ if (isset($_POST['backupcount'])) {
$savemsg = gettext("Unable to revert to the selected configuration."); $savemsg = gettext("Unable to revert to the selected configuration.");
} }
if($_POST['rmver'] != "") { if($_POST['rmver'] != "") {
unlink_if_exists('/conf/backup/config-' . $_POST['rmver'] . '.xml'); @unlink('/conf/backup/config-' . $_POST['rmver'] . '.xml');
$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$confvers[$_POST['rmver']]['description']); $savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$confvers[$_POST['rmver']]['description']);
} }
} }
......
...@@ -194,7 +194,7 @@ if ($_POST) { ...@@ -194,7 +194,7 @@ if ($_POST) {
/* fetch down and add in */ /* fetch down and add in */
$isfirst = 0; $isfirst = 0;
$temp_filename = tempnam('/tmp/', 'alias_import'); $temp_filename = tempnam('/tmp/', 'alias_import');
unlink_if_exists($temp_filename); @unlink($temp_filename);
$verify_ssl = isset($config['system']['checkaliasesurlcert']); $verify_ssl = isset($config['system']['checkaliasesurlcert']);
mkdir($temp_filename); mkdir($temp_filename);
download_file($_POST['address' . $x], $temp_filename . "/aliases", $verify_ssl); download_file($_POST['address' . $x], $temp_filename . "/aliases", $verify_ssl);
......
...@@ -1236,14 +1236,14 @@ if(($graphcmdreturn <> 0) || (! $data)) { ...@@ -1236,14 +1236,14 @@ if(($graphcmdreturn <> 0) || (! $data)) {
log_error(sprintf(gettext('Failed to create graph with error code %1$s, the error is: %2$s'),$graphcmdreturn,$graphcmdoutput)); log_error(sprintf(gettext('Failed to create graph with error code %1$s, the error is: %2$s'),$graphcmdreturn,$graphcmdoutput));
if(strstr($curdatabase, "queues")) { if(strstr($curdatabase, "queues")) {
log_error(sprintf(gettext("failed to create graph from %s%s, removing database"),$rrddbpath,$curdatabase)); log_error(sprintf(gettext("failed to create graph from %s%s, removing database"),$rrddbpath,$curdatabase));
unlink_if_exists($rrddbpath . $curif . $queues); @unlink($rrddbpath . $curif . $queues);
flush(); flush();
usleep(500); usleep(500);
enable_rrd_graphing(); enable_rrd_graphing();
} }
if(strstr($curdatabase, "queuesdrop")) { if(strstr($curdatabase, "queuesdrop")) {
log_error(sprintf(gettext("failed to create graph from %s%s, removing database"),$rrddbpath,$curdatabase)); log_error(sprintf(gettext("failed to create graph from %s%s, removing database"),$rrddbpath,$curdatabase));
unlink_if_exists($rrddbpath . $curdatabase); @unlink($rrddbpath . $curdatabase);
flush(); flush();
usleep(500); usleep(500);
enable_rrd_graphing(); enable_rrd_graphing();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment