You are not logged in.
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
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
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
@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
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
@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
[~ ]$
Offline
Okay, now try running it as root. Does it work?
Last edited by Peasantoid (2009-05-02 18:21:58)
Offline
Okay, now try running it as root. Does it work?
Yes
[root@arch boris]# python test.py
04111
[root@arch boris]#
Offline
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
@ 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
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
Well that's interesting... Doesn't work for me at all.
Offline