Surcouf
Posts: 9
Joined: Sat Dec 09, 2017 12:23 pm

SQlite permissions not enough for execution

Wed Jan 17, 2018 9:47 am

I'm developing a small project on raspberry pi based on sqlite3 and python. One of the scripts reads data from sensor and writes them to database (sensor_update.py), the other (cgi script, hello.py) reads last entry in database and prints it on the website. The problem is that the scripts can't write and read from database properly. I set ownership of both database and scripts to www-data and yet when I try to execute database-writing script I get following error:

Code: Select all

Traceback (most recent call last):
  File "sensor_update.py", line 35, in <module>
    log_temperature(temp)
  File "sensor_update.py", line 15, in log_temperature
    curs.execute("INSERT INTO temps values(datetime('now'), (?))", (temp,))
sqlite3.OperationalError: attempt to write a readonly database
I know already that it most probably means that permissions are not right. However ls -l gives output that should indicate that I did everything right:

Code: Select all

[email protected]:/usr/lib/cgi-bin $ ls -l
total 20
-rwxr-xr-x 1 www-data www-data 744 Jan 16 20:54 hello.py
-rwxr-xr-x 1 www-data www-data 475 Jan 16 20:40 hello.py.backup
-rwxr-xr-x 1 www-data www-data 163 Jan  3 22:40 hello.py.save
-rwxr-xr-x 1 www-data www-data 197 Jan  3 22:40 hello.py.save.1
-rwxr-xr-x 1 www-data www-data 698 Jan 16 21:11 sensor_update.py

[email protected]:/var/www $ ls -l
total 12
drwxr-xr-x 2 root     root     4096 Jan 11 08:31 html
-rwxr-xr-x 1 www-data www-data 8192 Jan 16 21:11 log.db
Also the cron job doesn't seems to work on the sensor_update.py script. Both programs produce expected output when command is preceded with sudo. What can I do to make it work?

User avatar
topguy
Posts: 4620
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: SQlite permissions not enough for execution

Wed Jan 17, 2018 3:09 pm

It doesnt matter who owns a script/executable, it matters who runs the script. ( But who owns the script can affect who can run it of course )

There are three "rwx" blocks, first is for the "user" that owns the file, the second is for the "group", and the third is for all other users.

So when you say "when I try to execute" I'm going to assume that you are doing it as user "pi".
The script is "r-x" for "others" so "pi" user can eXecute the script. The database file is also "r-x" for "others" so user "pi" does not have the rights to Write to this file.

I've only explained why running the script manually fails, what exaclty is a good solution for cron/webserver etc. I dont know but if you change accessrights for db fle to "rwxrwxrwx" then all users can write to the file.

User avatar
DougieLawson
Posts: 33398
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: SQlite permissions not enough for execution

Thu Jan 18, 2018 7:43 am

Read only is a status of SQLite that's unrelated to file permissions. The easy way to get in that status is two tasks attempting to write to one database.

sqlite3 original.db ".dump" | sqlite3 good.db
mv original.db broken.db
mv good.db original.db
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 17 guests