Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a script that allows users to create some folders. The problem is when the folders are created, it has the user's id instead of mine. Is there a way to maintain my owner id for those generated folders without asking the users to 'su' to my user ID ? Thanks very much.. Example script:
#!/opt/local/Perl-5.6/bin/perl my $myDir = "/path1/path2/theDir"; mkdir($myDir); ...

Replies are listed 'Best First'.
Re: maintaining owner id
by Limbic~Region (Chancellor) on Aug 08, 2003 at 21:02 UTC
    Anonymous Monk,
    Most systems will not let you change the owner of a file unless you are a SuperUser. This, I believe, is for two reasons:
  • Disk quotas
  • Security (change SetUID bit and chown root)

    I can think of a few ways to get around this, but they all involve using su/sudo/etc as well as entering a password.

    If you are interested in one with the knowledge that it is a security concern, let me know.

    Cheers - L~R

      Most systems will not let you change the owner of a file unless you are a SuperUser.

      This at least used to be true of one major branch of the Unix family tree (I think the SysV side) but not true of the other (I think Berkeley).

      In any case, on several Unix systems, it at least used to be possible to give your own files away. That is, you could chown files that you owned such that they would then belong to someone else.

      To prevent this from being a huge problem, there were (of course) several restrictions. You couldn't chown files in partitions with quotas active. chowning a file removes the set-UID and set-GID bits (true even if you a root, no?). etc.

                      - tye
        tye,
        Since we have had a related discussion previously, I have looked at various man pages. I guess I should have indicated to check the local man pages on chown but keep in mind that in doing so it might not make it portable. Here are some of the variations I have seen:
      • Allowed only if UID = 0
      • Allowed if no quota restrictions
      • Allowed, but strip any setuid/setgid bits
      • Allowed only if UID = 0 unless "set rstchown = 1" appears in /etc/system

        The most systems won't allow it unless superuser bit actually came from perldoc -f chown. It goes on to say how you can detect on a POSIX system if you have the right or not.

        Cheers - L~R

Re: maintaining owner id
by waswas-fng (Curate) on Aug 08, 2003 at 21:43 UTC
    one thing you can do is use a group and sticky the dir that is the root of all created files.. for example:
    mkdir /var/tmp/test chown youname:mygrp /var/tmp/test chmod 775 /var/tmp/test chmod g+s /var/tmp/test ls -la /var/tmp/test su - otheruser touch /var/tmp/test/newfile ls -la /var/tmp/test
    as long as you set the umask in your script to force 775 for the group you will efectivly own those files...

    -Waswas
Re: maintaining owner id
by tcf22 (Priest) on Aug 08, 2003 at 20:36 UTC
    Since the user is the owner of the folder, can't they just
    system("chown YOU /path1/path2/theDir");
    after the folder is created.

      chown typically doesn't work if you are not root. Otherwise, you could make something setuid, then chown it to some other user's ID and create all kinds of mischief.

      --Bob Niederman, http://bob-n.com
      I try to avoid calling the system commands. Beside, if I want to expand my script to create other folders or files, I have to chown those also. How about setting those built-in variables like $< and $> ? I'm playing with them but have no luck yet..
Re: maintaining owner id
by rir (Vicar) on Aug 08, 2003 at 20:49 UTC
    perldoc -f chown should give you the info to set the files and directories ownership after creation.