|
| 1 | +#define PY_SSIZE_T_CLEAN |
1 | 2 | #include <Python.h>
|
2 | 3 |
|
3 | 4 | #include <unistd.h>
|
4 | 5 | #include <signal.h>
|
5 | 6 | #include <stdio.h>
|
6 | 7 | #include <string.h>
|
7 | 8 |
|
8 |
| -#define BUFFER_SIZE (100000) |
9 |
| - |
10 | 9 | #pragma linkage(IRRSMO64, OS)
|
11 | 10 |
|
12 |
| -typedef struct { |
13 |
| - unsigned char len; |
14 |
| - char str[8]; |
15 |
| -} VarStr_T; |
16 |
| - |
17 |
| -// This function changes any null character not preceded by '>' to a blank character. |
18 |
| -// This is a workaround for an issue where profile data embedded in response xml |
19 |
| -// returned by IRROSMO00 sometimes includes null characters instead of properly |
20 |
| -// encoded text, which causes the returned xml to be truncated. |
21 |
| -void null_byte_fix(char* str, unsigned int str_len) { |
22 |
| - for (int i = 1; i < str_len; i++){ |
23 |
| - if (str[i] == 0) { |
24 |
| - if (str[i-1] == 0x6E) { |
25 |
| - return; |
26 |
| - } |
27 |
| - else { |
28 |
| - str[i] = 0x40; |
29 |
| - } |
30 |
| - } |
31 |
| - } |
32 |
| -} |
33 |
| - |
34 |
| -static PyObject* call_irrsmo00(PyObject* self, PyObject* args, PyObject *kwargs) { |
35 |
| - const unsigned int xml_len; |
36 |
| - const unsigned int input_opts; |
37 |
| - const uint8_t input_userid_len; |
38 |
| - const char *input_xml; |
39 |
| - const char *input_userid; |
40 |
| - |
41 |
| - static char *kwlist[] = {"xml_str", "xml_len", "opts", "userid", "userid_len", NULL}; |
| 11 | +typedef struct |
| 12 | +{ |
| 13 | + unsigned char running_userid_length; |
| 14 | + char running_userid[8]; |
| 15 | +} running_userid_t; |
42 | 16 |
|
43 |
| - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y|IIyb", kwlist, &input_xml, &xml_len, &input_opts, &input_userid, &input_userid_len)) { |
| 17 | +static PyObject *call_irrsmo00(PyObject *self, PyObject *args, PyObject *kwargs) |
| 18 | +{ |
| 19 | + const char *request_xml; |
| 20 | + const unsigned int request_xml_length; |
| 21 | + const unsigned int result_buffer_size; |
| 22 | + const unsigned int irrsmo00_options; |
| 23 | + const char *running_userid; |
| 24 | + const uint8_t running_userid_length; |
| 25 | + |
| 26 | + static char *kwlist[] = { |
| 27 | + "request_xml", |
| 28 | + "request_xml_length", |
| 29 | + "result_buffer_size", |
| 30 | + "irrsmo00_options", |
| 31 | + "running_userid", |
| 32 | + "running_userid_length", |
| 33 | + NULL}; |
| 34 | + |
| 35 | + if ( |
| 36 | + !PyArg_ParseTupleAndKeywords( |
| 37 | + args, |
| 38 | + kwargs, |
| 39 | + "y|IIIyb", |
| 40 | + kwlist, |
| 41 | + &request_xml, |
| 42 | + &request_xml_length, |
| 43 | + &result_buffer_size, |
| 44 | + &irrsmo00_options, |
| 45 | + &running_userid, |
| 46 | + &running_userid_length)) |
| 47 | + { |
44 | 48 | return NULL;
|
45 | 49 | }
|
46 | 50 |
|
47 |
| - |
48 | 51 | char work_area[1024];
|
49 |
| - char req_handle[64] = { 0 }; |
50 |
| - VarStr_T userid = { input_userid_len, {0}}; |
| 52 | + char req_handle[64] = {0}; |
| 53 | + running_userid_t running_userid_struct = {running_userid_length, {0}}; |
51 | 54 | unsigned int alet = 0;
|
52 | 55 | unsigned int acee = 0;
|
53 |
| - unsigned char rsp[BUFFER_SIZE+1]; |
54 |
| - memset(rsp, 0, BUFFER_SIZE); |
55 |
| - unsigned int saf_rc=0, racf_rc=0, racf_rsn=0; |
56 |
| - unsigned int num_parms=17, fn=1, opts = input_opts, rsp_len = sizeof(rsp)-1; |
57 |
| - |
58 |
| - strncpy(userid.str, input_userid, userid.len); |
| 56 | + unsigned char result_buffer[result_buffer_size]; |
| 57 | + memset(result_buffer, 0, result_buffer_size); |
| 58 | + unsigned int saf_rc = 0; |
| 59 | + unsigned int racf_rc = 0; |
| 60 | + unsigned int racf_rsn = 0; |
| 61 | + unsigned int num_parms = 17; |
| 62 | + unsigned int fn = 1; |
| 63 | + |
| 64 | + strncpy( |
| 65 | + running_userid_struct.running_userid, |
| 66 | + running_userid, |
| 67 | + running_userid_struct.running_userid_length); |
59 | 68 |
|
60 | 69 | IRRSMO64(
|
61 |
| - work_area, |
62 |
| - alet, |
63 |
| - &saf_rc, |
64 |
| - alet, |
65 |
| - &racf_rc, |
66 |
| - alet, |
67 |
| - &racf_rsn, |
68 |
| - num_parms, |
69 |
| - fn, |
70 |
| - opts, |
71 |
| - xml_len, |
72 |
| - input_xml, |
73 |
| - req_handle, |
74 |
| - userid, |
75 |
| - acee, |
76 |
| - rsp_len, |
77 |
| - rsp |
78 |
| - ); |
79 |
| - |
80 |
| - null_byte_fix(rsp,rsp_len); |
81 |
| - |
82 |
| - return Py_BuildValue("yBBB", rsp, saf_rc, racf_rc, racf_rsn); |
| 70 | + work_area, |
| 71 | + alet, |
| 72 | + &saf_rc, |
| 73 | + alet, |
| 74 | + &racf_rc, |
| 75 | + alet, |
| 76 | + &racf_rsn, |
| 77 | + num_parms, |
| 78 | + fn, |
| 79 | + irrsmo00_options, |
| 80 | + request_xml_length, |
| 81 | + request_xml, |
| 82 | + req_handle, |
| 83 | + running_userid_struct, |
| 84 | + acee, |
| 85 | + result_buffer_size, |
| 86 | + result_buffer); |
| 87 | + |
| 88 | + // https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue |
| 89 | + // |
| 90 | + // According to the Python 3 C API documentation: |
| 91 | + // When memory buffers are passed as parameters to supply data to |
| 92 | + // build objects, as for the s and s# formats, the required data is |
| 93 | + // copied. Buffers provided by the caller are never referenced by |
| 94 | + // the objects created by Py_BuildValue(). In other words, if your |
| 95 | + // code invokes malloc() and passes the allocated memory to |
| 96 | + // Py_BuildValue(), your code is responsible for calling free() for |
| 97 | + // that memory once Py_BuildValue() returns. |
| 98 | + // |
| 99 | + // y# (bytes) [const char *, Py_ssize_t] |
| 100 | + // This converts a C string and its lengths to a Python object. |
| 101 | + // If the C string pointer is NULL, None is returned. |
| 102 | + // |
| 103 | + // https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue |
| 104 | + // |
| 105 | + // So, given that 'result_buffer' is a stack allocated buffer |
| 106 | + // and that Python creates a copy of the buffer, which Python's |
| 107 | + // garbage collection should be responsible for, we do not need |
| 108 | + // to do any memory mangement here. 'result_buffer' will simply |
| 109 | + // just be popped off the stack when this function returns. |
| 110 | + // |
| 111 | + // Also, according to the Python3 C API documentation, 'y#' should |
| 112 | + // be just giving us a copy copy of exactly what is in the buffer, |
| 113 | + // without attempting to do any transformations to the data. |
| 114 | + // The following GeesForGeeks article futher confirms that we are |
| 115 | + // going to get a bytes object that is completely unmanipulated. |
| 116 | + // https://www.geeksforgeeks.org/c-strings-conversion-to-python/ |
| 117 | + // |
| 118 | + // In this case, all post processing of the data is handled on |
| 119 | + // the Python side. |
| 120 | + // |
| 121 | + // Also note that when two or more return values are provided, |
| 122 | + // Py_BuildValue() will return a Tuple. |
| 123 | + |
| 124 | + return Py_BuildValue( |
| 125 | + "y#BBB", |
| 126 | + result_buffer, |
| 127 | + result_buffer_size, |
| 128 | + saf_rc, |
| 129 | + racf_rc, |
| 130 | + racf_rsn); |
83 | 131 | }
|
84 | 132 |
|
85 | 133 | static char call_irrsmo00_docs[] =
|
86 |
| - "call_irrsmo00(input_xml: bytes, xml_len: uint, opts: uint): Returns an XML response string and return and reason codes from the IRRSMO00 RACF Callable Service.\n"; |
| 134 | + "call_irrsmo00(\n" |
| 135 | + " request_xml: bytes,\n" |
| 136 | + " request_xml_length: uint,\n" |
| 137 | + " result_buffer_size: uint,\n" |
| 138 | + " irrsmo00_options: uint,\n" |
| 139 | + " running_userid: bytes,\n" |
| 140 | + " running_userid_length: uint,\n" |
| 141 | + ") -> List[bytes,int,int,int]:\n" |
| 142 | + "# Returns an XML result string and return and reason " |
| 143 | + "codes from the IRRSMO00 RACF Callable Service.\n"; |
87 | 144 |
|
88 | 145 | static PyMethodDef cpyracf_methods[] = {
|
89 |
| - {"call_irrsmo00", (PyCFunction)call_irrsmo00, |
90 |
| - METH_VARARGS | METH_KEYWORDS, call_irrsmo00_docs}, |
91 |
| - {NULL} |
92 |
| -}; |
| 146 | + {"call_irrsmo00", (PyCFunction)call_irrsmo00, |
| 147 | + METH_VARARGS | METH_KEYWORDS, call_irrsmo00_docs}, |
| 148 | + {NULL}}; |
93 | 149 |
|
94 |
| -static struct PyModuleDef cpyracf_module_def = |
95 |
| -{ |
| 150 | +static struct PyModuleDef cpyracf_module_def = { |
96 | 151 | PyModuleDef_HEAD_INIT,
|
97 |
| - "cpyracf", |
| 152 | + "cpyracf", |
98 | 153 | "C code that enables pyRACF to call the IRRSMO00 RACF callable service.\n",
|
99 | 154 | -1,
|
100 |
| - cpyracf_methods |
101 |
| -}; |
| 155 | + cpyracf_methods}; |
102 | 156 |
|
103 | 157 | PyMODINIT_FUNC PyInit_cpyracf(void)
|
104 | 158 | {
|
105 |
| - Py_Initialize(); |
106 |
| - return PyModule_Create(&cpyracf_module_def); |
| 159 | + Py_Initialize(); |
| 160 | + return PyModule_Create(&cpyracf_module_def); |
107 | 161 | }
|
0 commit comments