I debated with myself about whether to make a new issue or put this in with this issue: 1163900 - Drush integration.

But new issue won since the Drush integration looks ready to go. Prior to the release of the patches in #1163900, our company had implemented our own bandaid patch to get less files regenerated along with css and js caches.

So the problem here and with #1163900 is that in a number of hosting environments you have little control over what unix user runs the web server and which one you use to connect via shell for doing drushing. In the experience of our company, we've almost never had the same user in both cases. This generates a number of permissions issues. Obviously, in some environments you have full control, so there's never any issue with drush lacking permission to delete less files from regular web traffic, or vice-versa, (although rarer) the webserver (via say devel) lacking permissions to delete less files generated by drush.

However, we've been stung more than once and needed to carry around (figuratively) scripts to recursively change permissions on less directories in order to solve these problems. Finally one day I decided to see what I could do to solve the core problem, which was less module writing directories and files in the default umask of the account.

Thus I added a admin page control to tell less module to create directories a bit more permissively. In addition, I added a few umask commands to the flow of the _less_pre_render function.

I'm not positive that this is the *perfect* solution, but I think there must be others out there who have at least run into this wall before, so I think it needs to be on the drawing board.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jefkin’s picture

The patch:

corey.aufang’s picture

Do you know what the permission situation is for clearing out Drupal core aggregated CSS files?

I would like to follow along with how that functions if possible.

jefkin’s picture

Actually I don't know what core's policy is, but I'm pretty sure we didn't run into permission problems with core's css files and we did with less.

I'll try to take a look at what core's doing this weekend.

My guess is that they use managed files and let the file interface take the brunt of the work.

corey.aufang’s picture

I took a small look:

http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_b...

...
file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
...

They use the same permission as I do when creating the directory, but somewhere the permissions on the folder and its contents are altered.

Did you come up with any results?

ralf57’s picture

The patch works fine for me.
It should be part of the dev code ASAP

jefkin’s picture

Hmm, I looked at the code, the thing I notice as a difference between the two pathways is that the core version doesn't create any hierarchy.

All the generated/compressed css files are written in

 "public://css/css_<gobbledygook>.css" or 
"public://css/css_<gobbledygook>.css.gz"

So it may well be that if core tried to build several layers of directories with file_prepare_directory() it too might run into this issue.

I'm willing to posit 2 alternate ideas that might actually solve this problem.

#1] walk the directory hierarchy stopping at each *step* along the way, and use file_prepare_directory() for each step.

Upshot: This might solve the problem without messing with umasks at all.
Problem: I'm not positive this will solve the problem.
Problem#2: This might be a bit of a performance hit.

#2] follow exactly the same pattern that they use, something like

 "public://less/less_<gobbledygook>.css" or
 "public://less/less_<gobbledygook>.css.gz"

Upshot: Following in core's footsteps means we can be much more comfortable that it'll work fine.
Problem: Potentially a significant rework of a lot of the less module to accommodate this change in functionality.
Problem#2: A lot harder to *know* your less files' compiled css files are being included.

corey.aufang’s picture

I know its been a while but try the method used in the D6 version:

replace

if (!is_dir($css_path) && !file_prepare_directory($css_path, FILE_CREATE_DIRECTORY)) {

with

if (!is_dir($css_path) && !@mkdir($css_path, 0775, TRUE)) {

That should make sure that all directories have full group permissions.

markhalliwell’s picture

Priority: Normal » Major
FileSize
2.34 KB

I have been having these issues with my local sandboxes for quite sometime. Granted I know it's really just a matter of my own local permissions, however I did find that LESS does indeed need to use umask().

Also I was running into a weird issue:

If the less directory could not be created then the site would render... just without ANY stylesheets. I found that in the code, there was a return being called if the directory couldn't be created. However, because this is a #pre_render function, the return value needs to be the $styles array.

I am escalating this issue to major, mainly for the simple fact that the return isn't actually returning the $styles variable properly and causing the site to "break".

As far as the permissions aspect of this goes, the umask() should help relieve perhaps some people's issues. However, I would like to stress that ultimately it is the server/sandbox's responsibility to setup the permissions of the file structure properly in the first place.

Once this has been verified, could we perhaps get this committed ASAP?

paulsheldrake’s picture

Tried the patch in my build from #8 and it didn't work for me.

What's happening for me is that the /less/UNIQUE directory is created with the wrong owner/group

paulsheldrake’s picture

FileSize
2.36 KB

Re-rolled the patch from #8 to make sure the folders are created with the right permissions.

paulsheldrake’s picture

FileSize
2.17 KB

My last re-roll didn't work after more testing. Reverting to dirty hack mode

markhalliwell’s picture

Status: Needs review » Needs work

@paulsheldrake:

However, I would like to stress that ultimately it is the server/sandbox's responsibility to setup the permissions of the file structure properly in the first place.

See: Securing file permissions and ownership

The patches you submitted are indeed "hack"-ish and should not be applied to this module. Using chown() is strongly discouraged as the user:group for each server may vary. Hard-coding these changes is not the solution.

This all being said, perhaps we could implement a hook that is called before the files are created? Then perhaps we could create and include a sub-module (less_permissions) which would provide additional UI options to allow the user to set the user:group for the server? The sub-module's hook wouldn't run unless these variables were specifically set (to ensure that the proper user:group are used) and would let the server attempt to handle it (as it really should be, in the first place).

corey.aufang’s picture

Has anyone looked into how some of the core bundled themes handle creating their custom colors.css files?

If I recall correctly they are creating files that are in the public folder, so they should also have a similar situation.

At a minimum, whats the code that creates the css folder that is used for aggregated css files?

markhalliwell’s picture

Assigned: Unassigned » markhalliwell

Yes! It is actually, however, core's color module (which I'm ironically rather familiar with) that handles the stylesheet saves.

The function that actually handles this exact issue is one that I tend to forget about:_color_save_stylesheet(). It saves the file to the public folder and then calls drupal_chmod() to fix the permissions of the path in question.

I will create another patch in the AM to see if this does indeed fix the issue.

markhalliwell’s picture

Status: Needs work » Needs review
FileSize
2.21 KB

Ok, did some serious debugging and decided to play devils advocate with my permissions and here is my conclusion.

The file_prepare_directory() function actually recursively creates directories and drupal_chmod()'s them accordingly.

Where I think people have been seeing a problem is actually after the CSS file is actually created. file_unmanaged_save_data() doesn't actually drupal_chmod() the file after it's been saved. So I've updated the patch accordingly (also taking out the umask stuff).

Given the original scope of this issue (involving Drush), I would also like to point out this issue: #990812: Add a "permissions" subcommand to fix/set all file permissions. I would again like to point out that if anyone has further issues with this module and permissions it's actually probably due to incorrect server permissions (which is NOT this module or any other module's responsibility).

I think the project page should also reference Securing file permissions and ownership and this issue after it's been fixed so people stop opening up issues.

The attached patch follows core file management practices and has been re-rolled against the latest dev.

corey.aufang’s picture

Status: Needs review » Needs work

Nice catch on the return $styles. I can't believe I let that live so long.

Actually drupal_chmod does get called within file_unmanaged_save_data:

file_unmanaged_save_data calls
file_unmanaged_move which calls
file_unmanaged_copy which calls
drupal_chmod

Unless I'm missing something, the files should be created and then immediately drupal_chmod'd correctly.

markhalliwell’s picture

Priority: Major » Normal
Status: Needs work » Needs review
FileSize
1.76 KB

Hmm, guess I missed that drupal_chmod does indeed get called (it was 1am lol). Well aside from that, I think that the only thing really to come from this patch is the minor tweaks to status message (reduce duplicate messages), properly returning the unaltered styles (this caused unstyled pages) and documentation on what's happening with the _less_new_dir() wrapper function. Attaching revised patch.

Overall my opinion still stands, if you're having issues with permissions it's probably because your server/sandbox isn't properly setup. It's not this or any other module's responsibility to fix these types of issues.

corey.aufang’s picture

Title: Less's created file permissions » Unstyled pages and code comments

Damn, I was hoping that I was missing something.

I am incorporating the changes from #1626652-17: Drush integration, cache file management, unstyled pages and code comments.

Just a quick title change to match the final result of this issue.

corey.aufang’s picture

Oh, I'll also be adding a link to Securing file permissions and ownership once a doc page for this module is created.

markhalliwell’s picture

Title: Unstyled pages and code comments » Drush creates less directories on cache clear, unstyled pages and code comments
Status: Needs review » Needs work

Ok, I just had yet another break though thought (sorry)!

It was still bugging me why people would be having issues with this module when things like core CSS and JS aggregation works just fine if run from drush.

I think I finally have the answer to why things are going wonky with permissions! When the CSS caches are cleared in core (drupal_clear_css_cache), all it does is remove stale files. It doesn't actually rebuild the CSS (which would invoke file_prepare_directory).

This is only done in a page deliver of some kind that actually delivers CSS files. So the files/directories are only created from a browser request (which would of course use apache's user/group when creating the files.... not drush). See: drupal_build_css_cache.

I'm gonna rework the patch one more time and see what you think :)

markhalliwell’s picture

Title: Drush creates less directories on cache clear, unstyled pages and code comments » Drush integration, cache file management, unstyled pages and code comments
FileSize
3.61 KB

Alrighty, I think we have a winner!

I renamed the _less_new_dir() to _less_get_dir($reset = FALSE). This now only returns what the current less directory should be. It will generate a new unique ID if it is currently not set, empty or manually reset.

When the page is actually rendered, it will only then invoke file_prepare_directory on the less css path (which obviously includes the less directory) and recursively creates everything with the web server's permissions, not Drush's! Drush now only handles the removal of files, NOT the creation... a small little WHOO HOO!!!

I also went ahead and cleaned up the drush integration to just use less_flush_caches since it also manually resets the directory. less_flush_caches also now removes stale files, instead of just less_cron.

markhalliwell’s picture

Status: Needs work » Needs review
corey.aufang’s picture

This indeed seems like the silver bullet for this issue.

corey.aufang’s picture

Status: Needs review » Reviewed & tested by the community

I've implemented all the code changes and everything worked perfectly.

This will be in the next release.

corey.aufang’s picture

Status: Reviewed & tested by the community » Needs review

Please check out the latest beta.

If you are still finding problems, please retag this issue with the new version as all future development for D7 will be on the 7.x-3.x branch.

corey.aufang’s picture

Assigned: markhalliwell » Unassigned
corey.aufang’s picture

Issue tags: +Needs backport to D6

Tagging and closing other, duplicate issue:
#1807298: Ownership Problem on FastCGI servers

jefkin’s picture

Excellent observation Mark, I'm testing this out from both patch (and the new dev version too corey).

I'll post a result later today :D

jefkin’s picture

I apparently didn't have the same cut from 2x, as the patch failed to apply. But the 3x version works, and so we're using that.

markhalliwell’s picture

Status: Needs review » Reviewed & tested by the community

Awesome glad to hear! Marking this RTBC for the 2.x branch. I know Corey already committed the changes to the 2.x branch, but there hasn't been a release yet. Should mark as fixed once that happens.

corey.aufang’s picture

Version: 7.x-2.x-dev » 7.x-3.0-beta1

There are no current plans to have another release on the 7.x-2.x branch.

I'm re-tagging as any future test of the fix for this problem should happen on the on the current beta build of the 7.x-3.x branch.

corey.aufang’s picture

Status: Reviewed & tested by the community » Fixed

This should be resolved in 7.x-3.0.

Automatically closed -- issue fixed for 2 weeks with no activity.