just nicer
[lxc-userspace.git] / setns.c
1 #include <Python.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <asm/unistd.h>
5 #include <sys/mount.h>
6 #include <errno.h>
7 #include <sys/prctl.h>
8 #include <linux/capability.h>
9
10 static PyObject *
11 drop_caps(PyObject *self, PyObject *args)
12 {
13         unsigned int to_drop[128]
14                 = {CAP_NET_ADMIN, CAP_SYS_ADMIN, CAP_SYS_BOOT,
15                         CAP_MKNOD, CAP_MAC_ADMIN, CAP_SYS_MODULE};
16         unsigned int i;
17         for (i = 0; i<6 ; i++) {
18                 if (prctl(PR_CAPBSET_DROP, to_drop[i], 0, 0, 0) == -1) {
19                         perror("prctl");
20                         return Py_BuildValue("i", 2);
21                 }
22         }
23         return Py_BuildValue("i", 0);
24 }
25
26 static PyObject *
27 proc_mount(PyObject *self, PyObject *args)
28 {
29     int sts;
30     sts = mount("none","/proc","proc",0,NULL);
31
32     return Py_BuildValue("i", sts);
33 }
34
35 static PyObject *
36 proc_umount(PyObject *self, PyObject *args)
37 {
38     int sts;
39     sts = umount("/proc");
40
41     return Py_BuildValue("i", sts);
42 }
43
44 static PyObject *
45 chfscontext(PyObject *self, PyObject *args)
46 {
47     const char *filepath;
48     int sts;
49
50     if (!PyArg_ParseTuple(args, "s", &filepath))
51         return NULL;
52
53     int fd = open(filepath, O_RDONLY);
54     if (fd < 0) {
55         sts = -errno;
56         goto out;
57     }
58
59     if (setns(fd, 0)) {
60         sts = -errno;
61     }
62     close(fd);
63     sts = 0;
64
65 out:
66     return Py_BuildValue("i", sts);
67 }
68
69 static PyObject *
70 chcontext(PyObject *self, PyObject *args)
71 {
72     const char *filepath;
73     int sts;
74
75     if (!PyArg_ParseTuple(args, "s", &filepath))
76         return NULL;
77
78     int fd = open(filepath, O_RDONLY);
79     if (fd < 0) {
80         sts = -errno;
81         goto out;
82     }
83
84     if (setns(fd, 0)) {
85         sts = -errno;
86     }
87     close(fd);
88     sts = 0;
89
90 out:
91     return Py_BuildValue("i", sts);
92 }
93
94 static PyMethodDef SetnsMethods[] =
95 {
96         {"proc_mount", proc_mount, METH_VARARGS, "Mount a volume via the mount system call."},
97         {"proc_umount", proc_umount, METH_VARARGS, "Umount a volume via the umount system call."},
98         {"chcontext", chcontext, METH_VARARGS, "Switch into an lxc container."},
99         {"drop_caps", drop_caps, METH_VARARGS, "Drop dangerous capabilities."},
100         {"chfscontext", chfscontext, METH_VARARGS, "Switch into an lxc container."},
101         {NULL, NULL, 0, NULL}
102 };
103
104 PyMODINIT_FUNC
105
106 initsetns(void)
107 {
108         (void) Py_InitModule("setns", SetnsMethods);
109 }