Give group writable permissions to uploaded files.

Add a new function chown_group to recursively change permissions.
Tweak some of the coding style.
Replace some of the redundant string concatenation with a variable.

Thanks to Dan McGee for chmod_group.

Signed-off-by: Loui Chang <louipc.ist@gmail.com>
This commit is contained in:
Loui Chang 2008-11-09 22:35:00 -05:00
parent 2ac75bd812
commit f12b11abc7
2 changed files with 47 additions and 17 deletions

View file

@ -30,12 +30,10 @@ if ($_COOKIE["AURSID"]):
if (!$error) { if (!$error) {
if (!@mkdir($tempdir)) { if (!@mkdir($tempdir)) {
$error = __("Could not create incoming directory: %s.", $error = __("Could not create incoming directory: %s.", $tempdir);
array($tempdir));
} else { } else {
if (!@chdir($tempdir)) { if (!@chdir($tempdir)) {
$error = __("Could not change directory to %s.", $error = __("Could not change directory to %s.", $tempdir);
array($tempdir));
} else { } else {
if ($_FILES['pfile']['name'] == "PKGBUILD") { if ($_FILES['pfile']['name'] == "PKGBUILD") {
move_uploaded_file($_FILES['pfile']['tmp_name'], $tempdir . "/PKGBUILD"); move_uploaded_file($_FILES['pfile']['tmp_name'], $tempdir . "/PKGBUILD");
@ -205,32 +203,31 @@ if ($_COOKIE["AURSID"]):
} }
} }
$incoming_pkgdir = INCOMING_DIR . $pkg_name;
if (!$error) { if (!$error) {
# First, see if this package already exists, and if it can be overwritten # First, see if this package already exists, and if it can be overwritten
$pkg_exists = package_exists($pkg_name); $pkg_exists = package_exists($pkg_name);
if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) { if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
if (file_exists(INCOMING_DIR . $pkg_name)) { if (file_exists($incoming_pkgdir)) {
# Blow away the existing file/dir and contents # Blow away the existing file/dir and contents
rm_rf(INCOMING_DIR . $pkg_name); rm_rf($incoming_pkgdir);
} }
if (!@mkdir(INCOMING_DIR . $pkg_name)) { if (!@mkdir($incoming_pkgdir)) {
$error = __( "Could not create directory %s.", $error = __( "Could not create directory %s.", $incoming_pkgdir);
INCOMING_DIR . $pkg_name);
} }
rename($pkg_dir, INCOMING_DIR . $pkg_name . "/" . $pkg_name); rename($pkg_dir, $incoming_pkgdir . "/" . $pkg_name);
} else { } else {
$error = __( "You are not allowed to overwrite the %h%s%h package.", $error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>");
"<b>", $pkg_name, "</b>");
} }
} }
# Re-tar the package for consistency's sake # Re-tar the package for consistency's sake
if (!$error) { if (!$error) {
if (!@chdir(INCOMING_DIR . $pkg_name)) { if (!@chdir($incoming_pkgdir)) {
$error = __("Could not change directory to %s.", $error = __("Could not change directory to %s.", $incoming_pkgdir);
array(INCOMING_DIR . $pkg_name));
} }
} }
@ -243,6 +240,11 @@ if ($_COOKIE["AURSID"]):
} }
} }
# Chmod files after everything has been done.
if (!chmod_group($incoming_pkgdir)) {
$error = __("Could not chmod directory %s.", $incoming_pkgdir);
}
# Whether it failed or not we can clean this out # Whether it failed or not we can clean this out
if (file_exists($tempdir)) { if (file_exists($tempdir)) {
rm_rf($tempdir); rm_rf($tempdir);
@ -296,7 +298,7 @@ if ($_COOKIE["AURSID"]):
mysql_real_escape_string($new_pkgbuild['license']), mysql_real_escape_string($new_pkgbuild['license']),
mysql_real_escape_string($new_pkgbuild['pkgdesc']), mysql_real_escape_string($new_pkgbuild['pkgdesc']),
mysql_real_escape_string($new_pkgbuild['url']), mysql_real_escape_string($new_pkgbuild['url']),
mysql_real_escape_string(INCOMING_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"), mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"), mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"),
$pdata["ID"]); $pdata["ID"]);
@ -342,7 +344,7 @@ if ($_COOKIE["AURSID"]):
mysql_real_escape_string($new_pkgbuild['url']), mysql_real_escape_string($new_pkgbuild['url']),
uid_from_sid($_COOKIE["AURSID"]), uid_from_sid($_COOKIE["AURSID"]),
uid_from_sid($_COOKIE["AURSID"]), uid_from_sid($_COOKIE["AURSID"]),
mysql_real_escape_string(INCOMING_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"), mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz")); mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
$result = db_query($q, $dbh); $result = db_query($q, $dbh);

View file

@ -381,6 +381,34 @@ function rm_rf($dirname="") {
return; return;
} }
# Recursive chmod to set group write permissions
#
function chmod_group($path) {
if (!is_dir($path))
return chmod($path, 0664);
$d = dir($path);
while ($f = $d->read()) {
if ($f != '.' && $f != '..') {
$fullpath = $path.'/'.$f;
if (is_link($fullpath))
continue;
elseif (!is_dir($fullpath)) {
if (!chmod($fullpath, 0664))
return FALSE;
}
elseif(!chmod_group($fullpath))
return FALSE;
}
}
$d->close();
if(chmod($path, 0775))
return TRUE;
else
return FALSE;
}
# obtain the uid given a Users.Username # obtain the uid given a Users.Username
# #
function uid_from_username($username="") function uid_from_username($username="")