For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2008 > How to set file attributes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to set file attributes
John

2008-02-26, 10:09 pm

I have set up my own counter.pl on my web page.

The counter saves the visitation count to a text file called "count.txt".
How do I create the file (and later open) with file attributes that prevent
others reading the file, ie. going to www.johnsmith.com/count.txt (the www here
is not real).
J. Gleixner

2008-02-26, 10:09 pm

John wrote:
> I have set up my own counter.pl on my web page.


Congratulations, you've just wasted a few hours of your time. :-)

perldoc -q "I still don't get locking."

>
> The counter saves the visitation count to a text file called "count.txt".
> How do I create the file (and later open) with file attributes that prevent
> others reading the file, ie. going to www.johnsmith.com/count.txt (the www here
> is not real).


Nothing to do with perl. Only files under the DOCUMENT_ROOT are
typically viewable, so write 'count.txt' to another directory,
like /tmp.
Joost Diepenmaat

2008-02-26, 10:09 pm

John <John.Smith@invalid.com> writes:

> I have set up my own counter.pl on my web page.
>
> The counter saves the visitation count to a text file called
> "count.txt". How do I create the file (and later open) with file
> attributes that prevent others reading the file, ie. going to
> www.johnsmith.com/count.txt (the www here is not real).


Unless you've got some special setup, evey visitor to the site runs as
the same user (whatever the webserver runs at). That means that if your
server code can read and write the file, then so can the webserver
itself.

What you want to do is prevent the file from being served to the
web. One way to do that is to put it outside the publically accessible
file tree(s). Another is to explicitly block it using whatever
mechanism your webserver provides.

For the latter approach on apache, you can use mod_access. See also:

<http://www.ducea.com/2006/07/21/apa...deny-access-to-
certain-file-types/>

--
Joost Diepenmaat | blog: [url]http://joost.zat.nl/[/url] | work: [url]http://zat.nl/[/url]
Jim Gibson

2008-02-26, 10:09 pm

In article <38t8s3550b7tipdjlg66u2ddmv8683k15v@4ax.com>, John
<John.Smith@invalid.com> wrote:

> I have set up my own counter.pl on my web page.
>
> The counter saves the visitation count to a text file called "count.txt".
> How do I create the file (and later open) with file attributes that prevent
> others reading the file, ie. going to www.johnsmith.com/count.txt (the www
> here
> is not real).


It depends upon the operating system. Under Unix, you would create the
file with an editor, the cat command, or the touch command (see 'man
cat', etc.). File permissions are set with the chmod command. You want
to set the file so that it is readable and writable only by the owner:

chmod 0600 count.txt

The problem is that for this to work, the file needs to be owned by the
web server process, and that may not be your own user id. Some web
servers use the user 'nobody', for example. As a result, you sometimes
have to set data files to be world writable:

chmod 0666 count.txt

So it also depends upon your user id, the web server's id, and whether
or not you have root privilege.

If you do have root privilege, then you can set the owner of the file
as well as the file permissions (as above):

chown nobody:nobody count.txt
chmod 0600 count.txt

But this is for Unix, not necessarily other operating systems.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Tony Curtis

2008-02-26, 10:09 pm

J. Gleixner wrote:

> Nothing to do with perl. Only files under the DOCUMENT_ROOT are
> typically viewable, so write 'count.txt' to another directory,
> like /tmp.


Off-topic, but.../tmp could easily be memory-based, so I wouldn't write
there.

hth
t
John Bokma

2008-02-26, 10:09 pm

Joost Diepenmaat <joost@zat.nl> wrote:

> What you want to do is prevent the file from being served to the
> web. One way to do that is to put it outside the publically accessible
> file tree(s). Another is to explicitly block it using whatever
> mechanism your webserver provides.


What might work (depending on your webserver config) is using a . in
front, e.g.

..counter.txt

*However* as others suggested, move it out of the document root (and chmod
your htdocs/www/web/whatever document root dir back to the value it had
before).

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/...oyolillo-1.html
Abigail

2008-02-26, 10:09 pm

_
John (John.Smith@invalid.com) wrote on VCCXCII September MCMXCIII in
< URL:news:38t8s3550b7tipdjlg66u2ddmv8683k
15v@4ax.com>:
:} I have set up my own counter.pl on my web page.
:}
:} The counter saves the visitation count to a text file called "count.txt".
:} How do I create the file (and later open) with file attributes that prevent
:} others reading the file, ie. going to www.johnsmith.com/count.txt (the www here
:} is not real).


Wow, a webcounter. And someone trying to reinvent the wheel. How retro.

Let me guess, you're running perl4, right? On DOS.



Abigail
--
perl -wle 'eval {die ["Just another Perl Hacker"]}; print ${$@}[$#{@${@}}]'
John

2008-02-27, 8:12 am

Jim Gibson <jimsgibson@gmail.com> wrote:

>In article <38t8s3550b7tipdjlg66u2ddmv8683k15v@4ax.com>, John
><John.Smith@invalid.com> wrote:
>
>
>It depends upon the operating system. Under Unix, you would create the
>file with an editor, the cat command, or the touch command (see 'man
>cat', etc.). File permissions are set with the chmod command. You want
>to set the file so that it is readable and writable only by the owner:
>
> chmod 0600 count.txt
>
>The problem is that for this to work, the file needs to be owned by the
>web server process, and that may not be your own user id. Some web
>servers use the user 'nobody', for example. As a result, you sometimes
>have to set data files to be world writable:
>
> chmod 0666 count.txt
>
>So it also depends upon your user id, the web server's id, and whether
>or not you have root privilege.
>
>If you do have root privilege, then you can set the owner of the file
>as well as the file permissions (as above):
>
> chown nobody:nobody count.txt
> chmod 0600 count.txt
>
>But this is for Unix, not necessarily other operating systems.


Thanks for input. The web counter files should be seen my me only (I download the
files every day).

Setting the files chmod 0600 count.txt seems to do what I want.
The perl script creates a num conter file for every day. How do I create the new
file so that it has 0600 attributes?
$txtfi=open (MYCOUNT,"<"."count.txt");
Ted Zlatanov

2008-02-27, 7:08 pm

On Tue, 26 Feb 2008 22:25:45 +0200 John <John.Smith@invalid.com> wrote:

J> I have set up my own counter.pl on my web page.
J> The counter saves the visitation count to a text file called "count.txt".
J> How do I create the file (and later open) with file attributes that prevent
J> others reading the file, ie. going to www.johnsmith.com/count.txt (the www here
J> is not real).

Use the mail system to deliver an e-mail message to a special mailbox
for every hit. For a local mail spool, Maildir works best for
performance. Don't use a mbox file if you can help it.

Then you can just count the number of messages in the mailbox. This is
a really simple approach that works reliably on any system, and you can
even do it remotely with SMTP and IMAP. Your symin will love it
because it's very secure.

Another approach I recommend highly is to do "grep -c" against the web
server logs and count the number of HTTP GETs. It gets a little slow
with older logs since you have to uncompress them, but at least you
don't have to worry about locking.

Probably the most robust, if slowest, approach is to read the Perl FAQ
on this topic, as others have suggested.

Ted
Jim Gibson

2008-02-27, 7:08 pm

In article <v20as35s7rbbmbdfbt4ki3hcga22ue08uc@4ax.com>, John
<John.Smith@invalid.com> wrote:

> Setting the files chmod 0600 count.txt seems to do what I want.
> The perl script creates a num conter file for every day. How do I create the new
> file so that it has 0600 attributes?
> $txtfi=open (MYCOUNT,"<"."count.txt");


You create the file by any means and then set the permissions using
chmod. Perl has a built-in chmod function ('perldoc -f chmod').

If you really insist on creating the file and setting the permissions
in one step, you can use the four-argument version of Perl's sysopen
function ( sysopen FILEHANDLE,FILENAME,MODE,PERMS )
but you are making your life more complicated ('perldoc -f sysopen').

From the command-line, you can control the permissions of new files
using the umask mask. Set it to 066 to create files with permissions of
0600. The umask is used to exclusive-or the default permissions of
0666, so each true bit in your umask sets the corresponding permission
bit to zero ('man csh' and search for 'umask' -- it is a shell
built-in).

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Ron Bergin

2008-02-27, 7:08 pm

On Feb 26, 10:27 pm, John <John.Sm...@invalid.com> wrote:
> Jim Gibson <jimsgib...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
> Thanks for input. The web counter files should be seen my me only (I download the
> files every day).
>
> Setting the files chmod 0600 count.txt seems to do what I want.
> The perl script creates a num conter file for every day. How do I create the new
> file so that it has 0600 attributes?
> $txtfi=open (MYCOUNT,"<"."count.txt");


use File::CounterFile;
http://search.cpan.org/~gaas/File-C.../CounterFile.pm

The module uses sysopen without specifying the perm (which defaults to
0666) so you'd probably what to adjust the code in the module.

change:
sysopen(F, $file, O_RDWR|O_CREAT) or croak("Can't open $file: $!");

to:
sysopen(F, $file, O_RDWR|O_CREAT, 0600) or croak("Can't open $file:
$!");
Ron Bergin

2008-02-27, 7:08 pm

On Feb 26, 10:27 pm, John <John.Sm...@invalid.com> wrote:
> Jim Gibson <jimsgib...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
> Thanks for input. The web counter files should be seen my me only (I download the
> files every day).
>
> Setting the files chmod 0600 count.txt seems to do what I want.
> The perl script creates a num conter file for every day. How do I create the new
> file so that it has 0600 attributes?
> $txtfi=open (MYCOUNT,"<"."count.txt");


use File::CounterFile;
http://search.cpan.org/~gaas/File-C.../CounterFile.pm

The module uses sysopen without specifying the perm (which defaults to
0666) so you'd probably what to adjust the code in the module.

change:
sysopen(F, $file, O_RDWR|O_CREAT) or croak("Can't open $file: $!");

to:
sysopen(F, $file, O_RDWR|O_CREAT, 0600) or croak("Can't open $file:
$!");
Glenn Jackman

2008-02-27, 7:08 pm

At 2008-02-27 12:48PM, "Jim Gibson" wrote:
> From the command-line, you can control the permissions of new files
> using the umask mask. Set it to 066 to create files with permissions of
> 0600. The umask is used to exclusive-or the default permissions of
> 0666, so each true bit in your umask sets the corresponding permission
> bit to zero ('man csh' and search for 'umask' -- it is a shell
> built-in).


or perldoc -f umask


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com