|
Lines 131-165
static PyObject* module_get_object(PyObject *module, char *name)
Link Here
|
| 131 |
static char* module_get_string(PyObject *module, char *name) |
131 |
static char* module_get_string(PyObject *module, char *name) |
| 132 |
{ |
132 |
{ |
| 133 |
PyObject *var; |
133 |
PyObject *var; |
| 134 |
char *str1, *str2; |
134 |
char *str1, *str2 = NULL; |
| 135 |
|
135 |
|
| 136 |
if ((var=PyObject_GetAttrString(module, name)) == NULL) |
136 |
if ((var=PyObject_GetAttrString(module, name)) == NULL) |
| 137 |
return NULL; |
137 |
goto error0; |
| 138 |
PyArg_Parse(var, "s", &str1); |
138 |
PyArg_Parse(var, "s", &str1); |
| 139 |
str2 = strdup(str1); |
139 |
str2 = strdup(str1); |
| 140 |
Py_XDECREF(var); |
140 |
Py_XDECREF(var); |
| 141 |
|
141 |
error0: |
| 142 |
return str2; |
142 |
return str2; |
| 143 |
} |
143 |
} |
| 144 |
|
144 |
|
| 145 |
static char** module_get_string_list(PyObject *module, char *name) |
145 |
static char** module_get_string_list(PyObject *module, char *name) |
| 146 |
{ |
146 |
{ |
| 147 |
PyObject *list; |
147 |
PyObject *list; |
| 148 |
char **res; |
148 |
char **res = NULL; |
| 149 |
int len, i; |
149 |
int len, i; |
| 150 |
|
150 |
|
| 151 |
if ((list=PyObject_GetAttrString(module, name)) == NULL) |
151 |
if ((list=PyObject_GetAttrString(module, name)) == NULL) |
| 152 |
return NULL; |
152 |
goto error0; |
| 153 |
if (!PyList_Check(list)) { |
153 |
if (!PyList_Check(list)) |
| 154 |
Py_XDECREF(list); |
154 |
goto error1; |
| 155 |
return NULL; |
|
|
| 156 |
} |
| 157 |
|
155 |
|
| 158 |
len = PyList_Size(list); |
156 |
len = PyList_Size(list); |
| 159 |
if ((res = malloc((len+1)*sizeof(char*))) == NULL) { |
157 |
if ((res = malloc((len+1)*sizeof(char*))) == NULL) |
| 160 |
Py_XDECREF(list); |
158 |
goto error1; |
| 161 |
return NULL; |
|
|
| 162 |
} |
| 163 |
for (i = 0; i < len; i++) { |
159 |
for (i = 0; i < len; i++) { |
| 164 |
PyObject *var; |
160 |
PyObject *var; |
| 165 |
var = PyList_GetItem(list, i); |
161 |
var = PyList_GetItem(list, i); |
|
Lines 167-181
static char** module_get_string_list(PyObject *module, char *name)
Link Here
|
| 167 |
Py_XDECREF(var); |
163 |
Py_XDECREF(var); |
| 168 |
} |
164 |
} |
| 169 |
res[len] = NULL; |
165 |
res[len] = NULL; |
|
|
166 |
error1: |
| 170 |
Py_XDECREF(list); |
167 |
Py_XDECREF(list); |
| 171 |
|
168 |
if (PyErr_Occurred()) |
|
|
169 |
PyErr_Print(); |
| 170 |
error0: |
| 171 |
PyErr_Clear(); // Silent error when attribute is not set |
| 172 |
return res; |
172 |
return res; |
| 173 |
} |
173 |
} |
| 174 |
|
174 |
|
| 175 |
/* load handler and insert it into list of handlers */ |
175 |
/* load handler and insert it into list of handlers */ |
| 176 |
static int handler_import(char* filename) |
176 |
static int handler_import(char* filename) |
| 177 |
{ |
177 |
{ |
| 178 |
char *filter; |
178 |
char *filter, *error_msg = NULL; |
| 179 |
int num_filters; |
179 |
int num_filters; |
| 180 |
char *state_filename; |
180 |
char *state_filename; |
| 181 |
FILE *state_fp; |
181 |
FILE *state_fp; |
|
Lines 189-243
static int handler_import(char* filename)
Link Here
|
| 189 |
|
189 |
|
| 190 |
if ((handler->module=module_import(filename)) == NULL) { |
190 |
if ((handler->module=module_import(filename)) == NULL) { |
| 191 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "import of filename=%s failed", filename); |
191 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "import of filename=%s failed", filename); |
| 192 |
PyErr_Print(); |
192 |
error_msg = "module_import()"; |
| 193 |
free(handler); |
193 |
goto error; |
| 194 |
return 1; |
|
|
| 195 |
} |
194 |
} |
| 196 |
|
195 |
|
| 197 |
handler->name = module_get_string(handler->module, "name"); |
196 |
handler->name = module_get_string(handler->module, "name"); |
|
|
197 |
if (handler->name == NULL) { |
| 198 |
error_msg = "module_get_string(\"name\")"; |
| 199 |
goto error; |
| 200 |
} |
| 201 |
|
| 198 |
if ( PyObject_HasAttrString(handler->module, "modrdn")) { |
202 |
if ( PyObject_HasAttrString(handler->module, "modrdn")) { |
| 199 |
handler->modrdn = module_get_string(handler->module, "modrdn"); |
203 |
handler->modrdn = module_get_string(handler->module, "modrdn"); |
| 200 |
} else { |
204 |
if (handler->modrdn == NULL) { |
| 201 |
handler->modrdn = NULL; |
205 |
error_msg = "module_get_string(\"modrdn\")"; |
|
|
206 |
goto error; |
| 207 |
} |
| 202 |
} |
208 |
} |
|
|
209 |
PyErr_Clear(); // Silent error when attribute is not set |
| 210 |
|
| 203 |
handler->description = module_get_string(handler->module, "description"); |
211 |
handler->description = module_get_string(handler->module, "description"); |
|
|
212 |
if (handler->description == NULL) { |
| 213 |
error_msg = "module_get_string(\"description\")"; |
| 214 |
goto error; |
| 215 |
} |
| 204 |
|
216 |
|
| 205 |
num_filters = 0; |
217 |
num_filters = 0; |
| 206 |
if ((filter = module_get_string(handler->module, "filter")) != NULL) { |
218 |
if ((filter = module_get_string(handler->module, "filter")) != NULL) { |
| 207 |
handler->filters = realloc(handler->filters, (num_filters+2)*sizeof(struct filter*)); |
219 |
handler->filters = realloc(handler->filters, (num_filters+2)*sizeof(struct filter*)); |
|
|
220 |
if (handler->filters == NULL) { |
| 221 |
error_msg = "malloc(struct filter[])"; |
| 222 |
goto error; |
| 223 |
} |
| 208 |
handler->filters[0] = malloc(sizeof(struct filter)); |
224 |
handler->filters[0] = malloc(sizeof(struct filter)); |
|
|
225 |
if (handler->filters[0] == NULL) { |
| 226 |
error_msg = "malloc(struct filter)"; |
| 227 |
goto error; |
| 228 |
} |
| 209 |
handler->filters[0]->base = NULL; |
229 |
handler->filters[0]->base = NULL; |
| 210 |
handler->filters[0]->scope = LDAP_SCOPE_SUBTREE; |
230 |
handler->filters[0]->scope = LDAP_SCOPE_SUBTREE; |
| 211 |
handler->filters[0]->filter = filter; |
231 |
handler->filters[0]->filter = filter; |
| 212 |
num_filters++; |
232 |
num_filters++; |
| 213 |
handler->filters[num_filters] = NULL; |
233 |
handler->filters[num_filters] = NULL; |
|
|
234 |
} else { |
| 235 |
PyErr_Clear(); // Silent error when attribute is not set |
| 214 |
} |
236 |
} |
| 215 |
|
237 |
|
| 216 |
if (PyObject_HasAttrString(handler->module, "filters")) { |
238 |
if (PyObject_HasAttrString(handler->module, "filters")) { |
| 217 |
PyObject *filters = PyObject_GetAttrString(handler->module, "filters"); |
239 |
PyObject *filters = PyObject_GetAttrString(handler->module, "filters"); |
| 218 |
int len = PyList_Size(filters), i; |
240 |
int len = PyList_Size(filters), i; |
| 219 |
handler->filters = realloc(handler->filters, (num_filters+len+1)*sizeof(struct filter*)); |
241 |
handler->filters = realloc(handler->filters, (num_filters+len+1)*sizeof(struct filter*)); |
|
|
242 |
if (handler->filters == NULL) { |
| 243 |
error_msg = "realloc(struct filter[])"; |
| 244 |
goto error; |
| 245 |
} |
| 220 |
for (i = 0; i < len; i++) { |
246 |
for (i = 0; i < len; i++) { |
| 221 |
PyObject *py_tuple = PyList_GetItem(filters, i); |
247 |
PyObject *py_tuple = PyList_GetItem(filters, i); |
|
|
248 |
if (py_tuple == NULL) { |
| 249 |
error_msg = "PyList_GetItem(filters)"; |
| 250 |
goto error0; |
| 251 |
} |
| 222 |
PyObject *py_base = PyTuple_GetItem(py_tuple, 0); |
252 |
PyObject *py_base = PyTuple_GetItem(py_tuple, 0); |
|
|
253 |
if (py_base == NULL) { |
| 254 |
error_msg = "PyTuple_GetItem(filters[0])"; |
| 255 |
goto error1; |
| 256 |
} |
| 223 |
PyObject *py_scope = PyTuple_GetItem(py_tuple, 1); |
257 |
PyObject *py_scope = PyTuple_GetItem(py_tuple, 1); |
|
|
258 |
if (py_scope == NULL) { |
| 259 |
error_msg = "PyTuple_GetItem(filters[1])"; |
| 260 |
goto error2; |
| 261 |
} |
| 224 |
PyObject *py_filter = PyTuple_GetItem(py_tuple, 2); |
262 |
PyObject *py_filter = PyTuple_GetItem(py_tuple, 2); |
|
|
263 |
if (py_filter == NULL) { |
| 264 |
error_msg = "PyTuple_GetItem(filters[2])"; |
| 265 |
goto error3; |
| 266 |
} |
| 225 |
|
267 |
|
| 226 |
handler->filters[num_filters] = malloc(sizeof(struct filter)); |
268 |
handler->filters[num_filters] = malloc(sizeof(struct filter)); |
|
|
269 |
if (handler->filters[num_filters] == NULL) { |
| 270 |
error_msg = "malloc(struct filter)"; |
| 271 |
goto error4; |
| 272 |
} |
| 273 |
memset(handler->filters[num_filters], 0, sizeof(struct filter)); |
| 227 |
handler->filters[num_filters]->base = strdup(PyString_AsString(py_base)); |
274 |
handler->filters[num_filters]->base = strdup(PyString_AsString(py_base)); |
|
|
275 |
if (handler->filters[num_filters]->base == NULL) { |
| 276 |
error_msg = "PyString_AsString(filters[0])"; |
| 277 |
goto error5; |
| 278 |
} |
| 228 |
handler->filters[num_filters]->scope = PyInt_AsLong(py_scope); |
279 |
handler->filters[num_filters]->scope = PyInt_AsLong(py_scope); |
|
|
280 |
if (handler->filters[num_filters]->scope == -1 && PyErr_Occurred()) { |
| 281 |
error_msg = "PyInt_AsString(filters[1])"; |
| 282 |
goto error6; |
| 283 |
} |
| 229 |
handler->filters[num_filters]->filter = strdup(PyString_AsString(py_filter)); |
284 |
handler->filters[num_filters]->filter = strdup(PyString_AsString(py_filter)); |
|
|
285 |
if (handler->filters[num_filters]->filter == NULL) { |
| 286 |
error_msg = "PyString_AsString(filters[2])"; |
| 287 |
goto error7; |
| 288 |
} |
| 230 |
num_filters++; |
289 |
num_filters++; |
| 231 |
Py_XDECREF(py_tuple); |
290 |
goto okay; |
| 232 |
Py_XDECREF(py_base); |
291 |
error7: |
| 233 |
Py_XDECREF(py_scope); |
292 |
free(handler->filters[num_filters]->filter); |
|
|
293 |
error6: |
| 294 |
free(handler->filters[num_filters]->base); |
| 295 |
error5: |
| 296 |
free(handler->filters[num_filters]); |
| 297 |
okay: |
| 298 |
error4: |
| 234 |
Py_XDECREF(py_filter); |
299 |
Py_XDECREF(py_filter); |
|
|
300 |
error3: |
| 301 |
Py_XDECREF(py_scope); |
| 302 |
error2: |
| 303 |
Py_XDECREF(py_base); |
| 304 |
error1: |
| 305 |
Py_XDECREF(py_tuple); |
| 306 |
error0: |
| 307 |
if (PyErr_Occurred()) { |
| 308 |
if (error_msg) |
| 309 |
fprintf(stderr, "%s: ", error_msg); |
| 310 |
PyErr_Print(); |
| 311 |
PyErr_Clear(); |
| 312 |
} |
| 235 |
} |
313 |
} |
| 236 |
handler->filters[num_filters] = NULL; |
314 |
handler->filters[num_filters] = NULL; |
| 237 |
Py_XDECREF(filters); |
315 |
Py_XDECREF(filters); |
| 238 |
} |
316 |
} |
| 239 |
|
317 |
|
| 240 |
handler->attributes = module_get_string_list(handler->module, "attributes"); |
318 |
handler->attributes = module_get_string_list(handler->module, "attributes"); |
|
|
319 |
if (handler->attributes == NULL) { |
| 320 |
error_msg = "module_get_string_list(\"attributs\")"; |
| 321 |
goto error; |
| 322 |
} |
| 241 |
|
323 |
|
| 242 |
handler->handler = module_get_object(handler->module, "handler"); |
324 |
handler->handler = module_get_object(handler->module, "handler"); |
| 243 |
handler->initialize = module_get_object(handler->module, "initialize"); |
325 |
handler->initialize = module_get_object(handler->module, "initialize"); |
|
Lines 249-257
static int handler_import(char* filename)
Link Here
|
| 249 |
/* read handler state */ |
331 |
/* read handler state */ |
| 250 |
asprintf(&state_filename, "%s/handlers/%s", cache_dir, handler->name); |
332 |
asprintf(&state_filename, "%s/handlers/%s", cache_dir, handler->name); |
| 251 |
state_fp = fopen(state_filename, "r"); |
333 |
state_fp = fopen(state_filename, "r"); |
| 252 |
if (state_fp == NULL) |
334 |
if (state_fp == NULL) { |
| 253 |
handler->state = 0; |
335 |
handler->state = 0; |
| 254 |
else { |
336 |
} else { |
| 255 |
fscanf(state_fp, "%d", &handler->state); |
337 |
fscanf(state_fp, "%d", &handler->state); |
| 256 |
fclose(state_fp); |
338 |
fclose(state_fp); |
| 257 |
} |
339 |
} |
|
Lines 271-278
static int handler_import(char* filename)
Link Here
|
| 271 |
handler->next = NULL; |
353 |
handler->next = NULL; |
| 272 |
} |
354 |
} |
| 273 |
|
355 |
|
| 274 |
|
|
|
| 275 |
return 0; |
356 |
return 0; |
|
|
357 |
error: |
| 358 |
if (PyErr_Occurred()) { |
| 359 |
if (error_msg) |
| 360 |
fprintf(stderr, "%s: ", error_msg); |
| 361 |
PyErr_Print(); |
| 362 |
} |
| 363 |
Py_XDECREF(handler->setdata); |
| 364 |
Py_XDECREF(handler->postrun); |
| 365 |
Py_XDECREF(handler->prerun); |
| 366 |
Py_XDECREF(handler->clean); |
| 367 |
Py_XDECREF(handler->initialize); |
| 368 |
Py_XDECREF(handler->handler); |
| 369 |
if (handler->attributes) { |
| 370 |
char **c; |
| 371 |
for (c = handler->attributes; *c; c++) |
| 372 |
free(*c); |
| 373 |
free(handler->attributes); |
| 374 |
} |
| 375 |
while (num_filters-- > 0) { |
| 376 |
free(handler->filters[num_filters]->filter); |
| 377 |
free(handler->filters[num_filters]->base); |
| 378 |
free(handler->filters[num_filters]); |
| 379 |
} |
| 380 |
free(handler->filters); |
| 381 |
free(handler->description); |
| 382 |
free(handler->modrdn); |
| 383 |
free(handler->name); |
| 384 |
free(handler); |
| 385 |
return 1; |
| 276 |
} |
386 |
} |
| 277 |
|
387 |
|
| 278 |
/* run prerun handler; this only needs to be done once for multiple calls |
388 |
/* run prerun handler; this only needs to be done once for multiple calls |