Lines 2-8
Link Here
|
2 |
# Distributed under the terms of the GNU General Public License v2 |
2 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# $Id$ |
3 |
# $Id$ |
4 |
|
4 |
|
5 |
import selinux |
5 |
import os,shutil,selinux,string |
6 |
from selinux import is_selinux_enabled |
6 |
from selinux import is_selinux_enabled, getfilecon, lgetfilecon |
7 |
from selinux_aux import setexec, secure_symlink, secure_rename, \ |
7 |
|
8 |
secure_copy, secure_mkdir, getcontext, get_sid, get_lsid |
8 |
def copyfile(src,dest): |
|
|
9 |
# without the str() I get an error: |
10 |
# failed to move src, dest |
11 |
# in method 'getfilecon', argument 1 of type 'char const *' |
12 |
(rc,ctx)=selinux.lgetfilecon(str(src)) |
13 |
if rc < 0: |
14 |
raise OSError("copyfile: Failed getting context of \"%s\"." % src) |
15 |
|
16 |
setfscreate(ctx) |
17 |
shutil.copyfile(src,dest) |
18 |
setfscreate() |
19 |
|
20 |
def getcontext(): |
21 |
(rc,ctx)=selinux.getcon() |
22 |
if rc < 0: |
23 |
raise OSError("getcontext: Failed getting current process context.") |
24 |
|
25 |
return ctx |
26 |
|
27 |
def mkdir(target,refdir): |
28 |
(rc,ctx)=selinux.getfilecon(refdir) |
29 |
if rc < 0: |
30 |
raise OSError("mkdir: Failed getting context of reference directory \"%s\"." % refdir) |
31 |
|
32 |
setfscreatecon(ctx) |
33 |
os.mkdir(target) |
34 |
setfscreatecon() |
35 |
|
36 |
def rename(src,dest): |
37 |
# without the str() I get an error: |
38 |
# failed to move src, dest |
39 |
# in method 'getfilecon', argument 1 of type 'char const *' |
40 |
(rc,ctx)=selinux.lgetfilecon(str(src)) |
41 |
if rc < 0: |
42 |
raise OSError("rename: Failed getting context of \"%s\"." % src) |
43 |
|
44 |
setfscreate(ctx) |
45 |
os.rename(src,dest) |
46 |
setfscreate() |
47 |
|
48 |
def setexec(ctx="\n"): |
49 |
if selinux.setexeccon(ctx) < 0: |
50 |
raise OSError("setexec: Failed setting exec() context \"%s\"." % ctx) |
51 |
|
52 |
def setfscreate(ctx="\n"): |
53 |
if selinux.setfscreatecon(ctx) < 0: |
54 |
raise OSError("setfscreate: Failed setting fs create context \"%s\"." % ctx) |
55 |
|
56 |
def spawn(selinux_type,spawn_func,mycommand, opt_name=None, **keywords): |
57 |
con = getcontext().split(":") |
58 |
con[2] = selinux_type |
59 |
setexec(string.join(con,":")) |
60 |
|
61 |
retval = spawn_func(mycommand, opt_name=opt_name, **keywords) |
62 |
|
63 |
setexec() |
64 |
return retval |
65 |
|
66 |
def symlink(target,link,reflnk): |
67 |
# without the str() I get an error: |
68 |
# failed to properly create symlink |
69 |
# in method 'lgetfilecon', argument 1 of type 'char const *' |
70 |
(rc,ctx)=selinux.lgetfilecon(str(reflnk)) |
71 |
if rc < 0: |
72 |
raise OSError("symlink: Failed getting context of reference symlink \"%s\"." % reflnk) |
73 |
|
74 |
setfscreate(ctx) |
75 |
os.symlink(target,link) |
76 |
setfscreate() |