[Fwd: Re: More swig changes for libselinux]
I tried working this off line with Dan but we haven't made any headway so I'm hoping someone in the group can help shed some light on this problem. I'm trying to call selinux.get_ordered_context_list from python but there current isn't a mapping for security_context_t ** so I wrote one but it didn't work. Then I tried to simplify it the point where I could make the call but wouldn't be able to process the results so I could see if it was a problem with my mapping or something else. The result is that even the simplest mapping is causing a problem hopefully someone with more swig and python experience can help me out.
Ted
I've done a little more work on the typemapping to accomodate where the security_context_t ** appears in the parameter list. However I'm still experiencing the :
Traceback (most recent call last):
File "./test.py", line 27, in ?
for security_context in security_context_list:
TypeError: expected string or Unicode object, NoneType found
error.
One of the experiments I tried involved using these mapping:
%typemap(in, numinputs=0) security_context_t **(security_context_t *temp=NULL) {
$1 = &temp;
}%typemap(argout) (const char *user, security_context_t fromcon,
security_context_t **list) {
$result = SWIG_Python_AppendOutput($result, $3);
}basically not processing the returned security_context_t** and the test1.py script which doesn't try and process the security_context_t ** returned since it hasn't been converted into a list. What's interesting is that any python list is messed up after the call to get_ordered_context_list :(
Ted
Quoted 353 lines · Daniel J Walsh
Quoted 352 lines · Xavier Toth
[jcdxdev@comms newrolegui]$ ./test.py
list length 26
return code 26 user_u:user_r:user_xserver_t:UNCLASSIFIED user_u:user_r:user_crontab_t:UNCLASSIFIED user_u:user_r:user_iceauth_t:UNCLASSIFIED user_u:user_r:loadkeys_t:UNCLASSIFIED user_u:user_r:pam_t:UNCLASSIFIED user_u:user_r:ping_t:UNCLASSIFIED user_u:user_r:user_lpr_t:UNCLASSIFIED user_u:user_r:user_ssh_t:UNCLASSIFIED user_u:user_r:user_dbusd_t:UNCLASSIFIED user_u:user_r:user_xauth_t:UNCLASSIFIED user_u:user_r:utempter_t:UNCLASSIFIED user_u:user_r:user_spamassassin_t:UNCLASSIFIED user_u:user_r:user_sudo_t:UNCLASSIFIED user_u:user_r:newrole_t:UNCLASSIFIED user_u:user_r:traceroute_t:UNCLASSIFIED user_u:user_r:user_su_t:UNCLASSIFIED user_u:user_r:passwd_t:UNCLASSIFIED user_u:user_r:chfn_t:UNCLASSIFIED user_u:user_r:user_t:UNCLASSIFIED user_u:user_r:user_gpg_t:UNCLASSIFIED user_u:user_r:user_spamc_t:UNCLASSIFIED user_u:user_r:user_mail_t:UNCLASSIFIED user_u:user_r:user_ssh_agent_t:UNCLASSIFIED user_u:user_r:user_gpg_agent_t:UNCLASSIFIED user_u:user_r:user_javaplugin_t:UNCLASSIFIED user_u:user_r:user_chkpwd_t:UNCLASSIFIED
Traceback (most recent call last):
File "./test.py", line 27, in ?
forsecurity_contextinsecurity_context_list:
TypeError: expected string or Unicode object, NoneType foundQuoted 316 lines · Daniel J Walsh
Quoted 7 lines · Xavier Toth
As I said I'm getting a typeerror after calling
get_ordered_context_listwhich I don't understand. The call seems to work fine and it appears that the list is constructed and populated correctly but the for loop iterating over the list fails after the last member is processed with something like 'expected string or unicode got NoneType'. Any ideas?Quoted 10 lines
Quoted 9 lines · Daniel J Walsh
Quoted 4 lines · Xavier Toth
I cleaned the typemap up a bit but am still getting a typeerror.
%typemap(argout)
security_context_t** {
PyObject*list_security_context=PyList_New(0); // Create thelist.
Quoted 3 lines
if (list_security_context) { security_context_t **p_p_security_context_t = arg3; while (*p_p_security_context_t) { // Move each stringinto the
Quoted 28 lines
Quoted 28 lines
Quoted 5 lines
list.
security_context_t*p_security_context_t=*p_p_security_context_t;
if (PyList_Append(list_security_context,
PyString_FromString((charQuoted 14 lines
*)*p_security_context_t)) < 0) { fprintf(stderr, "Fail to insert item in list.\n"); $result = -1; break; } p_p_security_context_t++; } } else { fprintf(stderr, "Fail to create list.\n"); $result = -1; } $result = SWIG_Python_AppendOutput($result,
list_security_context);Quoted 8 lines
}
Quoted 3 lines · Xavier Toth
I don't know if this is the right approach but here's what I tried adding to selinuxswig.i
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
%typemap(argout) security_context_t ** { int len = 0, i = 0; PyObject * list_security_context = NULL; security_context_t **p_p_security_context_t = arg3; for (len = 0; *p_p_security_context_t != NULL;p_p_security_context_t++, len++) ;
// Build a list of the returned strings.list_security_context=PyList_New(len); // Create thelist
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
of strings.
if (list_security_context != NULL) { p_p_security_context_t = arg3; for (i = 0; i < len; i++,p_p_security_context_t++) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
// Move each
string into the list.
security_context_t*p_security_context_t=*p_p_security_context_t;
if
(PyList_SetItem(list_security_context,i,PyString_FromString((char *)*p_security_context_t))== -1) {
fprintf(stderr, "Fail to insertitem
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
in list.\n"); return NULL; } } } else { fprintf(stderr, "Fail to create list.\n"); return NULL; } $result = SWIG_Python_AppendOutput($result,
list_security_context);
}test script
#!/usr/bin/env python
import getpass
import seobject
import selinux
seluser_records=seobject.seluserRecords()seluser_dict=seluser_records.get_all()
keys =seluser_dict.keys()keys.sort()
for key in keys:print "%s %s" % (seluser_dict[key] [0], seluser_dict[key] [3])#seluser_records.list()
user_name=getpass.getuser()user_name
id,seuser_name, level =selinux.getseuserbyname(user_name)printseuser_name
(rc,security_context_list) =selinux.get_ordered_context_list(seuser_name, None)forsecurity_contextinsecurity_context_list:security_contextoutput from test script
sysadm
sysadm_rstaff_rsecadm_rauditadm_r
staffsysadm_rstaff_rsecadm_rauditadm_r
sysadmsysadm_r
usersystem_r
useruser_r
root
root root:staff_r:staff_xserver_t:SystemLow-SystemHigh Traceback (most recent call last):
File "./test.py", line 21, in ?
forsecurity_contextinsecurity_context_list:
TypeError: expected string or Unicode object, NoneType foundIs there something I need to do to terminate the list? Also this should probably generate an exception for the error conditions.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Ted
Quoted 3 lines · Xavier Toth
I'm pretty new to swig and python but what about a typemap for
security_context_t**. Also in the swig docs I noticed :
// This cleans up the char ** array we malloc'd before thefunction
Quoted 84 lines
Quoted 84 lines
Quoted 5 lines
Quoted 5 lines
call
Quoted 4 lines
%typemap(freearg) char ** {
free((char *) $1);
}
which seems to go along with %typemap(in) char ** would you notwant
Quoted 42 lines
Quoted 42 lines
Quoted 6 lines
to add this to selinuxswig.i?
Quoted 3 lines · Daniel J Walsh
Added
get_context_list.h--- nsalibselinux/src/selinuxswig.i 2007-04-12
16:02:48.000000000 -0400
Quoted 1 line
Quoted 1 line
+++ libselinux-2.0.13/src/selinuxswig.i 2007-04-23
10:26:21.000000000 -0400
Quoted 33 lines
Quoted 33 lines
(quoted diff) +10
-0@@ -21,6 +21,7 @@ %module selinux %{ #include "selinux/selinux.h" + #include "selinux/get_context_list.h" %} %apply int *OUTPUT { int * }; %apply int *OUTPUT { size_t * };@@ -42,8 +43,12 @@ %typedef unsigned mode_t; +%include "../include/selinux/get_context_list.h" + extern int is_selinux_enabled(void); extern int is_selinux_mls_enabled(void); +extern void freecon(security_context_t con); +extern void freeconary(security_context_t * con); extern int getcon(security_context_t *con); extern int setcon(security_context_t con); extern int getpidcon(int pid, security_context_t *con);@@ -90,6 +95,11 @@ mode_t mode, security_context_t *con); +extern int matchpathcon_init_prefix(const char *path, + const char *prefix); +extern void matchpathcon_fini(void); + + extern int matchmediacon(const char *media, security_context_t *con);Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +1
-0@@ -106,6 +116,7 @@ extern const char *selinux_homedir_context_path(void); extern const char *selinux_media_context_path(void); extern const char *selinux_contexts_path(void); +extern const char *selinux_securetty_types_path(void);Quoted 8 lines
Quoted 8 lines
Review line 116
Quoted 8 lines
(quoted diff) +0
-1extern const char *selinux_booleans_path(void); extern const char *selinux_customizable_types_path(void); extern const char *selinux_users_path(void);@@ -113,11 +124,15 @@ extern const char *selinux_translations_path(void); extern const char *selinux_netfilter_context_path(void); extern const char *selinux_path(void); -extern int selinux_check_passwd_access(access_vector_trequested);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review line 127
Quoted 2 lines
(quoted diff) +1
-1-extern int checkPasswdAccess(access_vector_t requested); +#extern int selinux_check_passwd_access(access_vector_trequested);
Quoted 19 lines
Quoted 19 lines
Quoted 3 lines
Review line 130
Quoted 3 lines
(quoted diff) +2
-0+#extern int checkPasswdAccess(access_vector_t requested); +extern int selinux_check_securetty_context(security_context_t
tty_context);Quoted 15 lines
Quoted 15 lines
(quoted diff) +5
-1+void set_selinuxmnt(char *mnt); + +#ifdef SWIGpython // This tells SWIG to treat char ** as a special case -%typemap(python,in) char ** { +%typemap(in) char ** { /* Check if is a list */ if (PyList_Check($input)) { int size = PyList_Size($input);@@ -143,6 +158,7 @@ return NULL; } } +#endifQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Review line 182
Quoted 6 lines
(quoted diff) +0
-0extern int rpm_execcon(unsigned int verified, const char *filename,@@ -164,3 +180,7 @@ } extern int selinux_getpolicytype(char **enforce); extern int getseuserbyname(const char *linuxuser, char**seuser,
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
char **level);
Quoted 2 lines
Review line 184
Quoted 2 lines
(quoted diff) +2
-0+ +int selinux_file_context_cmp(const security_context_t a,const
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
security_context_tb);Quoted 1 line
Quoted 1 line
(quoted diff) +1
-0+int selinux_file_context_verify(const char *path, mode_tmode);
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Review line 186
Quoted 3 lines
(quoted diff) +1
-0+int selinux_lsetfilecon_default(const char *path);I am no expert on swig either. I usually muddle around until I
get it
Quoted 69 lines
Quoted 69 lines
to work.
Does this patch work for you?
selinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +0
-0@@ -17,6 +17,11 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301 USA
*/+%header %{
selinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +5
-0+ #define STATUS_SUCCESS 0 + #define STATUS_ERR -1 +%} + %module selinux %{@@ -29,6 +34,7 @@ %typemap(in, numinputs=0) security_context_t *(security_context_t temp=NULL) { $1 = &temp; } + %typemap(argout) security_context_t * (char *temp=NULL) { if (*$1)temp = *$1;selinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +6
-0@@ -43,6 +49,32 @@ %typedef unsigned mode_t; +%typemap(in, numinputs=0) security_context_t ** (security_context_t *temp=NULL){ + $1 = &temp; +} + +%typemap(argout) security_context_t ** { + PyObject *list_security_context = PyList_New(0); // Create thelist.
selinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +2
-0+ if (list_security_context) { + security_context_t **p_p_security_context_t =(
security_context_t**)(arg3);
+ while (*p_p_security_context_t) { // Move each string into
the list.
+security_context_t*p_security_context_t=*p_p_security_context_t;
+ if (PyList_Append(list_security_context,
PyString_FromString((charselinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +8
-0+*)*p_security_context_t)) < 0) { + fprintf(stderr, "Fail to insert item in list.\n"); + $result = SWIG_From_int(STATUS_ERR); + break; + } + p_p_security_context_t++; + } + $result = SWIG_Python_AppendOutput($result,
list_security_context);selinuxswig.i 2007-05-14 11:11:47.000000000 -0400 renamed from selinuxswig.i~ 2007-05-04 09:25:35.000000000 -0400 +7
-0+ } + else { + fprintf(stderr, "Fail to create list.\n"); + $result = SWIG_From_int(STATUS_ERR); + } +} + %include "../include/selinux/get_context_list.h"Quoted 3 lines
Quoted 3 lines
extern int
is_selinux_enabled(void);I have not been able to get my version to bring back more then one item in the list.
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Could you print the list for me?
Ok I have been able to duplicate your error on an MLS machine. I will look into it some more tomorrow. Must be something wrong with the binding.
src/selinuxswig.i +122 -6
@@ -17,18 +17,25 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +%import cpointer.i + +%header %{ + #define STATUS_SUCCESS 0 + #define STATUS_ERR -1 +%} %module selinux %{ #include "selinux/selinux.h" + #include "selinux/get_context_list.h" %} %apply int *OUTPUT { int * }; %apply int *OUTPUT { size_t * }; -%typemap(in, numinputs=0) security_context_t *(security_context_t temp) { +%typemap(in, numinputs=0) security_context_t *(security_context_t temp=NULL) { $1 = &temp; } -%typemap(argout) security_context_t * (char *temp) { +%typemap(argout) security_context_t * (char *temp=NULL) { if (*$1) temp = *$1; else
@@ -40,10 +47,74 @@ $1 = (security_context_t)PyString_AsString($input); } +%wrapper %{ + static PyObject* security_context_list2plist( + int count, + security_context_t *list) + { + int i; + PyObject *security_context_string, *security_context_list; + security_context_list = PyList_New(count); // Create the list. + if (security_context_list) { + for (i=0; i < count; i++) { + security_context_string = PyString_FromString(strdup((char *)list[i])); + if (PyList_SetItem(security_context_list, i, security_context_string) < 0) { + PyErr_SetString(PyExc_MemoryError,"typemap(argout) security_context_t **: Fail to insert item in list."); + Py_DECREF(security_context_list); + return NULL; + } + } + } + else { + PyErr_SetString(PyExc_MemoryError,"typemap(argout) security_context_t **: Fail to create list."); + return NULL; + } + return security_context_list; + } +%} + +%typemap(argout) (const char *user, security_context_t fromcon, security_context_t **list) { + int count; + + SWIG_AsVal_int($result, &count); + PyObject *security_context_list = NULL; + + if ((security_context_list = security_context_list2plist(count, *$3)) == NULL) + $result = SWIG_From_int(STATUS_ERR); + else + $result = SWIG_From_int(STATUS_SUCCESS); + + freeconary(*$3); + %append_output(security_context_list); +} + +%typemap(argout) (const char *user, const char *level, security_context_t fromcon, security_context_t **list) { + int count; + + SWIG_AsVal_int($result, &count); + PyObject *security_context_list = NULL; + + if ((security_context_list = security_context_list2plist(count, *$4)) == NULL) + $result = SWIG_From_int(STATUS_ERR); + else + $result = SWIG_From_int(STATUS_SUCCESS); + + freeconary(*$4); + %append_output(security_context_list); +} + +%typemap(in, numinputs=0) security_context_t **(security_context_t *temp=NULL) { + $1 = &temp; +} + %typedef unsigned mode_t; +%include "../include/selinux/get_context_list.h" + extern int is_selinux_enabled(void); extern int is_selinux_mls_enabled(void); +extern void freecon(security_context_t con); +extern void freeconary(security_context_t * con); extern int getcon(security_context_t *con); extern int setcon(security_context_t con); extern int getpidcon(int pid, security_context_t *con);
@@ -90,6 +161,11 @@ mode_t mode, security_context_t *con); +extern int matchpathcon_init_prefix(const char *path, + const char *prefix); +extern void matchpathcon_fini(void); + + extern int matchmediacon(const char *media, security_context_t *con);
@@ -106,6 +182,7 @@ extern const char *selinux_homedir_context_path(void); extern const char *selinux_media_context_path(void); extern const char *selinux_contexts_path(void); +extern const char *selinux_securetty_types_path(void); extern const char *selinux_booleans_path(void); extern const char *selinux_customizable_types_path(void); extern const char *selinux_users_path(void);
@@ -113,11 +190,45 @@ extern const char *selinux_translations_path(void); extern const char *selinux_netfilter_context_path(void); extern const char *selinux_path(void); -extern int selinux_check_passwd_access(access_vector_t requested); -extern int checkPasswdAccess(access_vector_t requested); +#extern int selinux_check_passwd_access(access_vector_t requested); +#extern int checkPasswdAccess(access_vector_t requested); + +extern int selinux_check_securetty_context(security_context_t tty_context); +void set_selinuxmnt(char *mnt); + +#ifdef SWIGpython +// This tells SWIG to treat char ** as a special case +%typemap(in) char ** { + /* Check if is a list */ + if (PyList_Check($input)) { + int size = PyList_Size($input); + int i = 0; + $1 = (char **) malloc((size+1)*sizeof(char *)); + if ($1 == NULL) { + PyErr_SetString(PyExc_MemoryError,"Out of memory"); + return NULL; + } + for (i = 0; i < size; i++) { + PyObject *o = PyList_GetItem($input,i); + if (PyString_Check(o)) + $1[i] = PyString_AsString(PyList_GetItem($input,i)); + else { + PyErr_SetString(PyExc_TypeError,"list must contain strings"); + free($1); + return NULL; + } + } + $1[i] = 0; + } else { + PyErr_SetString(PyExc_TypeError,"not a list"); + return NULL; + } +} +#endif + extern int rpm_execcon(unsigned int verified, const char *filename, - char *const argv[], char *const envp[]); + char **, char **); extern int is_context_customizable (security_context_t scontext);
@@ -126,7 +237,7 @@ extern int selinux_raw_to_trans_context(char *raw, security_context_t *transp); -%typemap(in, numinputs=0) char **(char *temp) { +%typemap(in, numinputs=0) char **(char *temp=NULL) { $1 = &temp; }
@@ -135,3 +246,8 @@ } extern int selinux_getpolicytype(char **enforce); extern int getseuserbyname(const char *linuxuser, char **seuser, char **level); + +int selinux_file_context_cmp(const security_context_t a, const security_context_t b); +int selinux_file_context_verify(const char *path, mode_t mode); +int selinux_lsetfilecon_default(const char *path); +
#!/usr/bin/env python
import getpass
import seobject
import selinux
user_name = getpass.getuser()
foo = ["1", "2", "3"]
for f in foo:
print f
(id, seuser_name, level) = selinux.getseuserbyname(user_name)
rc, security_context_list = selinux.get_ordered_context_list(seuser_name, None) print "return code %d" % (rc)
for f in foo:
print f