You are not logged in.

#1 2009-05-02 05:22:21

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Python: os.chmod() WITH setuid/setgid/sticky bit

Simple question, really, but I've been puzzling over it for ages. How can I set the permissions of a file along with the setuid/setgid/sticky bits? I'm trying to os.chmod() a file to 4111 but it just comes out as 0111 — it seems that the special bits get lost. Here's my code:

os.chmod('some-file', 04111)

But the permissions invariably end up being 0111.

I read somewhere that this is because the relevant module is bitwise-AND'ing the permissions with 0777, so everything else just gets thrown away.

Last edited by Peasantoid (2009-05-02 05:26:10)

Offline

#2 2009-05-02 06:29:06

mksoft
Member
Registered: 2008-07-03
Posts: 9

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

Try using the constants defined in the stat module, e.g:

import os
import stat

os.chmod('some-file', stat.S_ISUID | stat.S_ISGID | ....)

You can also calculate the value in a shell (ORing them) and use that in the function call (although IMO using the constants provides better readability and portability)
See http://docs.python.org/library/os.html#os.chmod

Offline

#3 2009-05-02 07:13:34

ulis
Member
Registered: 2009-04-29
Posts: 7

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

import stat, os
os.chmod('file', stat.S_ISUID |  stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
os.chmod('file', 0x849)

but

ls -l file
-rwxrwxrwx 1 chebotarev chebotarev 0 Май  2 10:56 file
python -c "import os; os.chmod('/home/chebotarev/file', 04111)"
ls -l file
---s--x--x 1 chebotarev chebotarev 0 Май  2 10:56 file

works

Last edited by ulis (2009-05-02 07:14:30)

Offline

#4 2009-05-02 16:33:39

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

@ulis: Yeah, the weird thing is the last one does work, but not in my script! The file only gets chmodded to 04111 if I do it by invoking the Python interpreter itself...

Here's the script:

import os, stat
os.chmod('/home/peasantoid/testfile', 04111)
print oct(stat.S_IMODE(os.stat('/home/peasantoid/testfile').st_mode)) # This prints 04111...

But when I "stat ~/testfile", it tells me it's 0111. What's going on here??

[edit] Okay, that's weird. I was running the script as root in order to test something and it would refuse to set the setuid bit, but it worked fine if I ran it as a normal user... What the hell is up with that?

Last edited by Peasantoid (2009-05-02 16:39:23)

Offline

#5 2009-05-02 16:48:22

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

The same thing happens with this (except it doesn't work with either user):

import os
os.system('chmod 04111 /home/peasantoid/testfile')

ARGH

Last edited by Peasantoid (2009-05-02 16:49:30)

Offline

#6 2009-05-02 18:21:05

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

Peasantoid wrote:

@ulis: Yeah, the weird thing is the last one does work, but not in my script! The file only gets chmodded to 04111 if I do it by invoking the Python interpreter itself...

Here's the script:

import os, stat
os.chmod('/home/peasantoid/testfile', 04111)
print oct(stat.S_IMODE(os.stat('/home/peasantoid/testfile').st_mode)) # This prints 04111...

But when I "stat ~/testfile", it tells me it's 0111. What's going on here??

[edit] Okay, that's weird. I was running the script as root in order to test something and it would refuse to set the setuid bit, but it worked fine if I ran it as a normal user... What the hell is up with that?

It works for me.

[~ ]$ touch file
[~ ]$ stat file | grep Access | head -n1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   boris)   Gid: (  100/   users)
[~ ]$ python test.py 
04111
[~ ]$

smile

Offline

#7 2009-05-02 18:21:47

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

Okay, now try running it as root. Does it work?

Last edited by Peasantoid (2009-05-02 18:21:58)

Offline

#8 2009-05-02 18:30:15

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

Peasantoid wrote:

Okay, now try running it as root. Does it work?

Yes

[root@arch boris]# python test.py
04111
[root@arch boris]#

Offline

#9 2009-05-02 19:30:32

ulis
Member
Registered: 2009-04-29
Posts: 7

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

1

chebotarev@arch ~ $ ls -l file
-rwxr-xr-x 1 chebotarev chebotarev 0 Май  2 22:56 file
chebotarev@arch ~ $ python test
04111
chebotarev@arch ~ $ ls -l file
---s--x--x 1 chebotarev chebotarev 0 Май  2 22:56 file

2

chebotarev@arch ~ $ ls -l file 
-rwxr-xr-x 1 chebotarev chebotarev 0 Май  2 23:38 file
chebotarev@arch ~ $ su
Пароль: 
root@arch /home/chebotarev # python test
04111
root@arch /home/chebotarev # ls -l file 
---s--x--x 1 chebotarev chebotarev 0 Май  2 23:38 file
root@arch /home/chebotarev # exit
exit
chebotarev@arch ~ $ ls -l file 
---s--x--x 1 chebotarev chebotarev 0 Май  2 23:38 file

Last edited by ulis (2009-05-02 19:41:14)

Offline

#10 2009-05-02 22:32:37

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

@ Boris Bolgradov : What I mean is, does it set the mode properly? Try stat'ing the file.

Last edited by Peasantoid (2009-05-02 22:32:59)

Offline

#11 2009-05-03 09:33:46

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

As user:

[~ ]$ touch file
[~ ]$ ls -l file
-rw-r--r-- 1 boris users 0 2009-05-03 12:29 file
[~ ]$ stat file | grep Access | head -n1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   boris)   Gid: (  100/   users)
[~ ]$ python test.py 
04111
[~ ]$ ls -l file
---s--x--x 1 boris users 0 2009-05-03 12:29 file
[~ ]$ stat file | grep Access | head -n1
Access: (4111/---s--x--x)  Uid: ( 1000/   boris)   Gid: (  100/   users)

As root:

[~ ]$ touch file
[~ ]$ su
Password: 
[root@arch boris]# ls -l file
-rw-r--r-- 1 boris users 0 2009-05-03 12:31 file
[root@arch boris]# stat file | grep Access | head -n1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   boris)   Gid: (  100/   users)
[root@arch boris]# python test.py 
04111
[root@arch boris]# ls -l file
---s--x--x 1 boris users 0 2009-05-03 12:31 file
[root@arch boris]# stat file | grep Access | head -n1
Access: (4111/---s--x--x)  Uid: ( 1000/   boris)   Gid: (  100/   users)
[root@arch boris]# exit
exit
[~ ]$ stat file | grep Access | head -n1
Access: (4111/---s--x--x)  Uid: ( 1000/   boris)   Gid: (  100/   users)

Last edited by Boris Bolgradov (2009-05-03 12:34:26)

Offline

#12 2009-05-03 18:01:15

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Python: os.chmod() WITH setuid/setgid/sticky bit

Well that's interesting... Doesn't work for me at all.

Offline

Board footer

Powered by FluxBB