commit 111695aaf4812b4ee7d022be6b7cbb27295abcce Author: Philipp Hahn Date: Fri Feb 1 21:39:03 2013 +0100 Bug #30227: listener: fix binary data corruption LDAP attributes containing 0 are shortened by strndup(), but the length field for that attribute is still set to the binary data length. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 1d8a45f..02bdfec 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -275,12 +275,12 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - // TODO: stdndup() copies until the first \0, which would be incorrect if data is binary! - if (!(c_attr->values[c_attr->value_count] = strndup((char*)data_data, data_size))) { + if (!(c_attr->values[c_attr->value_count] = malloc(data_size))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } c_attr->length[c_attr->value_count] = data_size; + memcpy(c_attr->values[c_attr->value_count], data_data, data_size); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "value is \"%s\"", c_attr->values[c_attr->value_count]); c_attr->values[++c_attr->value_count] = NULL; break; commit 035d04c29e9341f090c7d0d4878ccd907eaf5543 Author: Philipp Hahn Date: Fri Feb 1 21:35:52 2013 +0100 Bug #30227: listener: assert null termination Check key_len and data_len >= 1. Check key and data are always '\0' terminated, especially data: C-strings are naturally '\0' terminated, binary data is artifically '\0' terminated by us. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 03d8afb..1d8a45f 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -38,6 +38,7 @@ #include #include +#include #include #include "common.h" @@ -230,15 +231,20 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) CacheEntryAttribute **attribute, *c_attr; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "attribute is \"%s\"", (char*)key_data); + /* key name including '\0' */ + assert(key_size >= 1); + assert('\0' == ((char *)key_data)[key_size - 1]); + /* value including '\0' */ + assert(data_size >= 1); + assert('\0' == ((char *)data_data)[data_size - 1]); for (attribute = entry->attributes, c_attr = NULL; attribute != NULL && *attribute != NULL; attribute++) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "current attribute is \"%s\"", c_attr->name); - if (strcmp(c_attr->name, (char*)key_data) == 0) { + if (strncmp(c_attr->name, (char*)key_data, key_size) == 0) c_attr = *attribute; break; - } } if (!c_attr) { if (!(entry->attributes = realloc(entry->attributes, (entry->attribute_count + 2) * sizeof(CacheEntryAttribute*)))) { commit cd2b95c68a943f3de0ccacbe50902d86af795e33 Author: Philipp Hahn Date: Fri Feb 1 21:33:39 2013 +0100 Bug #30227: listener: allocation checking allocate and check in one statement to reduce code repetition. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 4a5481e..03d8afb 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -241,18 +241,15 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) } } if (!c_attr) { - entry->attributes = realloc(entry->attributes, (entry->attribute_count + 2) * sizeof(CacheEntryAttribute*)); - if (entry->attributes == NULL) { + if (!(entry->attributes = realloc(entry->attributes, (entry->attribute_count + 2) * sizeof(CacheEntryAttribute*)))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - c_attr = malloc(sizeof(CacheEntryAttribute)); - if (c_attr == NULL) { + if (!(c_attr = malloc(sizeof(CacheEntryAttribute)))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "malloc failed"); abort(); // FIXME } - c_attr->name = strndup((char*)key_data, key_size); - if (c_attr->name == NULL) { + if (!(c_attr->name = strndup((char*)key_data, key_size))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } @@ -264,19 +261,16 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "%s is at %p", c_attr->name, c_attr); } - c_attr->values = realloc((*attribute)->values, ((*attribute)->value_count + 2) * sizeof(char*)); - if (c_attr->values == NULL) { + if (!(c_attr->length = realloc(c_attr->length, (c_attr->value_count + 2) * sizeof(int)))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - c_attr->length = realloc((*attribute)->length, ((*attribute)->value_count + 2) * sizeof(int)); - if (c_attr->length == NULL) { + if (!(c_attr->values = realloc(c_attr->values, (c_attr->value_count + 2) * sizeof(char*)))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } // TODO: stdndup() copies until the first \0, which would be incorrect if data is binary! - c_attr->values[c_attr->value_count] = strndup((char*)data_data, data_size); - if (c_attr->values[c_attr->value_count] == NULL) { + if (!(c_attr->values[c_attr->value_count] = strndup((char*)data_data, data_size))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } @@ -288,13 +282,11 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) case TYPE_MODULES: entry->modules = realloc(entry->modules, (entry->module_count + 2) * sizeof(char*)); - entry->modules[entry->module_count] = strndup((char*)key_data, key_size); - if (entry->modules[entry->module_count] == NULL) { + if (!(entry->modules[entry->module_count] = strndup((char*)key_data, key_size))) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } - entry->modules[entry->module_count+1] = NULL; - entry->module_count++; + entry->modules[++entry->module_count] = NULL; break; default: { commit f23ffe906a6b1b26edd14d7c8cc1128407d9979f Author: Philipp Hahn Date: Fri Feb 1 21:31:04 2013 +0100 Bug #30227: listener: simplify pointer logic Track the current CacheAttribute in local variable instead of calculating it again and again. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 542d225..4a5481e 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -227,67 +227,62 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) while ((type = read_header(data, size, &pos, &key_data, &key_size, &data_data, &data_size)) > 0) { switch (type) { case TYPE_ATTRIBUTE: { - CacheEntryAttribute **attribute; - bool found = false; + CacheEntryAttribute **attribute, *c_attr; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "attribute is \"%s\"", (char*)key_data); - for (attribute = entry->attributes; + for (attribute = entry->attributes, c_attr = NULL; attribute != NULL && *attribute != NULL; attribute++) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "current attribute is \"%s\"", (*attribute)->name); - if (strcmp((*attribute)->name, (char*)key_data) == 0) { - found = true; + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "current attribute is \"%s\"", c_attr->name); + if (strcmp(c_attr->name, (char*)key_data) == 0) { + c_attr = *attribute; break; } } - if (!found) { + if (!c_attr) { entry->attributes = realloc(entry->attributes, (entry->attribute_count + 2) * sizeof(CacheEntryAttribute*)); if (entry->attributes == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - entry->attributes[entry->attribute_count] = malloc(sizeof(CacheEntryAttribute)); - if (entry->attributes[entry->attribute_count] == NULL) { + c_attr = malloc(sizeof(CacheEntryAttribute)); + if (c_attr == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "malloc failed"); abort(); // FIXME } - entry->attributes[entry->attribute_count+1] = NULL; - - attribute=entry->attributes+entry->attribute_count; - (*attribute)->name = strndup((char*)key_data, key_size); - if ((*attribute)->name == NULL) { + c_attr->name = strndup((char*)key_data, key_size); + if (c_attr->name == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } - entry->attributes[entry->attribute_count+1] = NULL; - (*attribute)->values = NULL; - (*attribute)->length = NULL; - (*attribute)->value_count = 0; - entry->attribute_count++; + c_attr->values = NULL; + c_attr->length = NULL; + c_attr->value_count = 0; + entry->attributes[entry->attribute_count++] = c_attr; + entry->attributes[entry->attribute_count] = NULL; - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "%s is at %p", (*attribute)->name, *attribute); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "%s is at %p", c_attr->name, c_attr); } - (*attribute)->values = realloc((*attribute)->values, ((*attribute)->value_count + 2) * sizeof(char*)); - if ((*attribute)->values == NULL) { + c_attr->values = realloc((*attribute)->values, ((*attribute)->value_count + 2) * sizeof(char*)); + if (c_attr->values == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - (*attribute)->length = realloc((*attribute)->length, ((*attribute)->value_count + 2) * sizeof(int)); - if ((*attribute)->length == NULL) { + c_attr->length = realloc((*attribute)->length, ((*attribute)->value_count + 2) * sizeof(int)); + if (c_attr->length == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } // TODO: stdndup() copies until the first \0, which would be incorrect if data is binary! - (*attribute)->values[(*attribute)->value_count] = strndup((char*)data_data, data_size); - if ((*attribute)->values[(*attribute)->value_count] == NULL) { + c_attr->values[c_attr->value_count] = strndup((char*)data_data, data_size); + if (c_attr->values[c_attr->value_count] == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); abort(); // FIXME } - (*attribute)->length[(*attribute)->value_count] = data_size; - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "value is \"%s\"", (*attribute)->values[(*attribute)->value_count]); - (*attribute)->values[(*attribute)->value_count+1] = NULL; - (*attribute)->value_count++; + c_attr->length[c_attr->value_count] = data_size; + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "value is \"%s\"", c_attr->values[c_attr->value_count]); + c_attr->values[++c_attr->value_count] = NULL; break; } commit b3aa836389a60ab660699bbcbe76b48bbe19c764 Author: Philipp Hahn Date: Fri Feb 1 21:20:47 2013 +0100 Bug #30227: listener: convert if() to switch() Use switch() instead of multiple if()s to handle different types. Move variables to section where they are needed. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 752cb6b..542d225 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -214,12 +214,10 @@ static enum type read_header(void *data, u_int32_t size, u_int32_t *pos, void ** /* Decode CacheEntry from data chunk. */ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) { - FILE *file; enum type type; void *key_data, *data_data; u_int32_t key_size, data_size; u_int32_t pos=0; - char *f; entry->attributes=NULL; entry->attribute_count=0; @@ -227,7 +225,8 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) entry->module_count=0; while ((type = read_header(data, size, &pos, &key_data, &key_size, &data_data, &data_size)) > 0) { - if (type == TYPE_ATTRIBUTE) { + switch (type) { + case TYPE_ATTRIBUTE: { CacheEntryAttribute **attribute; bool found = false; @@ -289,7 +288,10 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "value is \"%s\"", (*attribute)->values[(*attribute)->value_count]); (*attribute)->values[(*attribute)->value_count+1] = NULL; (*attribute)->value_count++; - } else if (type == TYPE_MODULES) { + break; + } + + case TYPE_MODULES: entry->modules = realloc(entry->modules, (entry->module_count + 2) * sizeof(char*)); entry->modules[entry->module_count] = strndup((char*)key_data, key_size); if (entry->modules[entry->module_count] == NULL) { @@ -298,7 +300,12 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) } entry->modules[entry->module_count+1] = NULL; entry->module_count++; - } else { + break; + + default: { + FILE *file; + char *f; + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "bad data block at position %d:", pos); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "last 100 bytes of previous entry:"); hex_dump(UV_DEBUG_ERROR, data, pos-1000 < 0 ? 0 : pos-1000, pos-1000 < 0 ? pos : 1000); @@ -315,6 +322,7 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) return -1; } + } } return 0; commit 753130a310d018d208701c4d0de0c3fc669912b9 Author: Philipp Hahn Date: Fri Feb 1 15:00:32 2013 +0100 Bug #30227: listener: white space cleanup Remove traling blanks. Break long lines. Insert blanks before assignment, comparison, after keyword. Reformat header files. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 961a156..752cb6b 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -100,7 +100,7 @@ static int write_header(void **data, u_int32_t *size, u_int32_t *pos, enum type univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "write_header key_size=%d data_size=%d", key_size, data_size); - need_memory = sizeof(struct cache_entry_header)+key_size+data_size; + need_memory = sizeof(struct cache_entry_header) + key_size + data_size; if (*size < *pos+need_memory) { while (*size < *pos+need_memory) *size += BUFSIZ; @@ -121,6 +121,7 @@ static int write_header(void **data, u_int32_t *size, u_int32_t *pos, enum type return 0; } +/* Encode CacheEntry to data chunk. */ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) { CacheEntryAttribute **attribute; @@ -130,8 +131,13 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) int i, rv = -1; u_int32_t pos=0; - for (attribute=entry->attributes; attribute != NULL && *attribute != NULL; attribute++) { - for (value=(*attribute)->values, i=0, length=(*attribute)->length; *value != NULL; value++, i++) { + for (attribute = entry->attributes; + attribute != NULL && *attribute != NULL; + attribute++) { + length = (*attribute)->length; + for (value = (*attribute)->values, i = 0; + *value != NULL; + value++, i++) { rv = write_header(data, size, &pos, TYPE_ATTRIBUTE, (*attribute)->name, strlen((*attribute)->name) + 1, *value, length[i]); @@ -139,7 +145,9 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) goto out; } } - for (module=entry->modules; module != NULL && *module != NULL; module++) { + for (module = entry->modules; + module != NULL && *module != NULL; + module++) { rv = write_header(data, size, &pos, TYPE_MODULES, *module, strlen(*module) + 1, NULL, 0); @@ -203,6 +211,7 @@ static enum type read_header(void *data, u_int32_t size, u_int32_t *pos, void ** return h->type; } +/* Decode CacheEntry from data chunk. */ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) { FILE *file; @@ -224,7 +233,9 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "attribute is \"%s\"", (char*)key_data); - for (attribute=entry->attributes; attribute != NULL && *attribute != NULL; attribute++) { + for (attribute = entry->attributes; + attribute != NULL && *attribute != NULL; + attribute++) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "current attribute is \"%s\"", (*attribute)->name); if (strcmp((*attribute)->name, (char*)key_data) == 0) { found = true; @@ -232,7 +243,7 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) } } if (!found) { - entry->attributes = realloc(entry->attributes, (entry->attribute_count+2)*sizeof(CacheEntryAttribute*)); + entry->attributes = realloc(entry->attributes, (entry->attribute_count + 2) * sizeof(CacheEntryAttribute*)); if (entry->attributes == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME @@ -258,12 +269,12 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "%s is at %p", (*attribute)->name, *attribute); } - (*attribute)->values = realloc((*attribute)->values, ((*attribute)->value_count+2)*sizeof(char*)); + (*attribute)->values = realloc((*attribute)->values, ((*attribute)->value_count + 2) * sizeof(char*)); if ((*attribute)->values == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME } - (*attribute)->length = realloc((*attribute)->length, ((*attribute)->value_count+2)*sizeof(int)); + (*attribute)->length = realloc((*attribute)->length, ((*attribute)->value_count + 2) * sizeof(int)); if ((*attribute)->length == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "realloc failed"); abort(); // FIXME @@ -279,7 +290,7 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) (*attribute)->values[(*attribute)->value_count+1] = NULL; (*attribute)->value_count++; } else if (type == TYPE_MODULES) { - entry->modules = realloc(entry->modules, (entry->module_count+2)*sizeof(char*)); + entry->modules = realloc(entry->modules, (entry->module_count + 2) * sizeof(char*)); entry->modules[entry->module_count] = strndup((char*)key_data, key_size); if (entry->modules[entry->module_count] == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "strndup failed"); @@ -294,7 +305,8 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "first 100 bytes of current entry:"); hex_dump(UV_DEBUG_ERROR, data, pos, pos+1000 > size ? size-pos : 1000); - if (asprintf(&f, "%s/bad_cache", cache_dir) < 0) abort(); + if (asprintf(&f, "%s/bad_cache", cache_dir) < 0) + abort(); if ((file = fopen(f, "w")) != NULL) { fprintf(file, "Check log file"); fclose(file); commit 1a2e6e7080ff7ef70fa95826c0e706fb6d59ec19 Author: Philipp Hahn Date: Fri Feb 1 21:12:56 2013 +0100 Bug #30227: listener: handle allocation failures Abort on failed allocations and writes. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index afe925f..961a156 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -127,26 +127,32 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) char **value; char **module; int *length; - int i; + int i, rv = -1; u_int32_t pos=0; for (attribute=entry->attributes; attribute != NULL && *attribute != NULL; attribute++) { for (value=(*attribute)->values, i=0, length=(*attribute)->length; *value != NULL; value++, i++) { - write_header(data, size, &pos, TYPE_ATTRIBUTE, + rv = write_header(data, size, &pos, TYPE_ATTRIBUTE, (*attribute)->name, strlen((*attribute)->name) + 1, *value, length[i]); + if (rv) + goto out; } } for (module=entry->modules; module != NULL && *module != NULL; module++) { - write_header(data, size, &pos, TYPE_MODULES, + rv = write_header(data, size, &pos, TYPE_MODULES, *module, strlen(*module) + 1, NULL, 0); + if (rv) + goto out; } /* allocated memory maybe bigger than size, but doesn't matter anyhow... */ *size = pos; + rv = 0; - return 0; +out: + return rv; } static enum type read_header(void *data, u_int32_t size, u_int32_t *pos, void **key_data, u_int32_t *key_size, void **data_data, u_int32_t *data_size) commit 0cace74d11a542d6c4d830d2d80724d7eb5ebc4a Author: Philipp Hahn Date: Fri Feb 1 20:29:04 2013 +0100 Bug #30227: listener: change function signature No need to use pointer indirection. Change function to void as it does not return anything. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index 19e3e56..afe925f 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -84,14 +84,13 @@ void hex_dump(int level, void *data, u_int32_t start, u_int32_t size) } /* assumption: enough memory as been allocated for us */ -static int append_buffer(void **data, u_int32_t *size, u_int32_t *pos, void* blob_data, u_int32_t blob_size) +static void append_buffer(void *data, u_int32_t size, u_int32_t *pos, void *blob_data, u_int32_t blob_size) { if (blob_size > 0) { - memcpy((void*)(((char*)*data)+*pos), blob_data, blob_size); + memcpy(((char*)data) + *pos, blob_data, blob_size); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "position was=%d is=%d", *pos, *pos+blob_size); *pos += blob_size; } - return 0; } static int write_header(void **data, u_int32_t *size, u_int32_t *pos, enum type type, void* key_data, u_int32_t key_size, void* data_data, u_int32_t data_size) @@ -115,9 +114,9 @@ static int write_header(void **data, u_int32_t *size, u_int32_t *pos, enum type h.key_size = key_size; h.data_size = data_size; - append_buffer(data, size, pos, (void*) &h, sizeof(struct cache_entry_header)); - append_buffer(data, size, pos, key_data, key_size); - append_buffer(data, size, pos, data_data, data_size); + append_buffer(*data, *size, pos, &h, sizeof(struct cache_entry_header)); + append_buffer(*data, *size, pos, key_data, key_size); + append_buffer(*data, *size, pos, data_data, data_size); return 0; } @@ -134,13 +133,13 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) for (attribute=entry->attributes; attribute != NULL && *attribute != NULL; attribute++) { for (value=(*attribute)->values, i=0, length=(*attribute)->length; *value != NULL; value++, i++) { write_header(data, size, &pos, TYPE_ATTRIBUTE, - (void*) (*attribute)->name, strlen((*attribute)->name)+1, - (void*) *value, length[i]); + (*attribute)->name, strlen((*attribute)->name) + 1, + *value, length[i]); } } for (module=entry->modules; module != NULL && *module != NULL; module++) { write_header(data, size, &pos, TYPE_MODULES, - (void*) *module, strlen(*module)+1, + *module, strlen(*module) + 1, NULL, 0); } commit 5f18189f439c9a0c345cab942a6a61506ee3cdf7 Author: Philipp Hahn Date: Fri Feb 1 20:24:40 2013 +0100 Bug #30227: listener: magic types Replace magic numbers for attributes and modules by named enum. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c index ff210d1..19e3e56 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.c @@ -43,6 +43,11 @@ #include "common.h" #include "cache_lowlevel.h" +enum type { + TYPE_ATTRIBUTE = 1, + TYPE_MODULES = 2, +}; + struct cache_entry_header { u_int16_t type; u_int32_t key_size; @@ -89,7 +94,7 @@ static int append_buffer(void **data, u_int32_t *size, u_int32_t *pos, void* blo return 0; } -static int write_header(void **data, u_int32_t *size, u_int32_t *pos, u_int16_t type, void* key_data, u_int32_t key_size, void* data_data, u_int32_t data_size) +static int write_header(void **data, u_int32_t *size, u_int32_t *pos, enum type type, void* key_data, u_int32_t key_size, void* data_data, u_int32_t data_size) { struct cache_entry_header h; u_int32_t need_memory; @@ -128,13 +133,13 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) for (attribute=entry->attributes; attribute != NULL && *attribute != NULL; attribute++) { for (value=(*attribute)->values, i=0, length=(*attribute)->length; *value != NULL; value++, i++) { - write_header(data, size, &pos, 1, + write_header(data, size, &pos, TYPE_ATTRIBUTE, (void*) (*attribute)->name, strlen((*attribute)->name)+1, (void*) *value, length[i]); } } for (module=entry->modules; module != NULL && *module != NULL; module++) { - write_header(data, size, &pos, 2, + write_header(data, size, &pos, TYPE_MODULES, (void*) *module, strlen(*module)+1, NULL, 0); } @@ -145,7 +150,7 @@ int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry) return 0; } -static int read_header(void *data, u_int32_t size, u_int32_t *pos, void **key_data, u_int32_t *key_size, void **data_data, u_int32_t *data_size) +static enum type read_header(void *data, u_int32_t size, u_int32_t *pos, void **key_data, u_int32_t *key_size, void **data_data, u_int32_t *data_size) { struct cache_entry_header *h; @@ -159,7 +164,7 @@ static int read_header(void *data, u_int32_t size, u_int32_t *pos, void **key_da h = (struct cache_entry_header*)((char*)data+*pos); - if ((h->type != 1 && h->type != 2) || h->key_size == 0) { + if ((h->type != TYPE_ATTRIBUTE && h->type != TYPE_MODULES) || h->key_size == 0) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "read_header pos=%d type=%d key_size=%d data_size=%d", *pos, h->type, h->key_size, h->data_size); *key_size = 0; *key_data = NULL; @@ -196,7 +201,7 @@ static int read_header(void *data, u_int32_t size, u_int32_t *pos, void **key_da int parse_entry(void *data, u_int32_t size, CacheEntry *entry) { FILE *file; - u_int16_t type; + enum type type; void *key_data, *data_data; u_int32_t key_size, data_size; u_int32_t pos=0; @@ -208,7 +213,7 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) entry->module_count=0; while ((type = read_header(data, size, &pos, &key_data, &key_size, &data_data, &data_size)) > 0) { - if (type == 1) { + if (type == TYPE_ATTRIBUTE) { CacheEntryAttribute **attribute; bool found = false; @@ -268,7 +273,7 @@ int parse_entry(void *data, u_int32_t size, CacheEntry *entry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "value is \"%s\"", (*attribute)->values[(*attribute)->value_count]); (*attribute)->values[(*attribute)->value_count+1] = NULL; (*attribute)->value_count++; - } else if (type == 2) { + } else if (type == TYPE_MODULES) { entry->modules = realloc(entry->modules, (entry->module_count+2)*sizeof(char*)); entry->modules[entry->module_count] = strndup((char*)key_data, key_size); if (entry->modules[entry->module_count] == NULL) { commit 29c294af24ced7ecae52a6c49de01623b94615eb Author: Philipp Hahn Date: Fri Feb 1 18:34:01 2013 +0100 Bug #30227: listener: skip strdup(dn) No need to duplicate the dn for printing the cache entries. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.c index 7b569c0..9559d74 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.c @@ -590,7 +590,7 @@ int cache_print_entries(char *dn) DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); - key.data = strdup(dn); + key.data = dn; key.size = strlen(dn)+1; key.flags = DB_DBT_REALLOC; data.flags = DB_DBT_REALLOC; @@ -603,7 +603,6 @@ int cache_print_entries(char *dn) cur->c_close(cur); free(key.data); - free(data.data); return 0; } commit 19f19217e07d4750b11c283d223c8ae13c7a5f5e Author: Philipp Hahn Date: Fri Feb 1 18:22:11 2013 +0100 Bug #30227: listener: rewrite copy_cache Fix all out-of-memory cases. Allocate structures once instead of incrementally increasing the arrays again and again, since the size is known from the source entry before hand. Always copy bv_len + 1 bytes and terminate with '\0'. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index a6a8a01..9b795ee 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -195,6 +195,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP int i; char *ucrval; enum { DUPLICATES, UNIQUE_UID, UNIQUE_MEMBER } check; + size_t len; /* convert LDAP entry to cache entry */ memset(cache_entry, 0, sizeof(CacheEntry)); @@ -220,6 +221,10 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP goto result; } c_attr->name = strdup(attr); + if (!c_attr->name) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for CacheEntryAttribute.name failed"); + goto result; + } c_attr->values = NULL; c_attr->length = NULL; c_attr->value_count = 0; @@ -270,30 +275,22 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if (i < c_attr->value_count) continue; } - if ((c_attr->values = realloc(c_attr->values, (c_attr->value_count + 2) * sizeof(char*))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of values array failed"); + len = (*v)->bv_len; + if (!(c_attr->length = realloc(c_attr->length, (c_attr->value_count + 2) * sizeof(int)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of length array failed"); goto result; } - if ((c_attr->length = realloc(c_attr->length, (c_attr->value_count + 2) * sizeof(int))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of length array failed"); + if (!(c_attr->values = realloc(c_attr->values, (c_attr->value_count + 2) * sizeof(char*)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of values array failed"); goto result; } - if ((*v)->bv_len == strlen((*v)->bv_val)) { - if ((c_attr->values[c_attr->value_count] = strdup((*v)->bv_val)) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: strdup of value failed"); - goto result; - } - c_attr->length[c_attr->value_count] = strlen(c_attr->values[c_attr->value_count]) + 1; - } else { // in this case something is strange about the string in bv_val, maybe contains a '\0' - // the legacy approach is to copy bv_len bytes, let's stick with this and just terminate to be safe - if ((c_attr->values[c_attr->value_count] = malloc(((*v)->bv_len + 1) * sizeof(char))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for value failed"); - goto result; - } - memcpy(c_attr->values[c_attr->value_count], (*v)->bv_val, (*v)->bv_len); - c_attr->values[c_attr->value_count][(*v)->bv_len] = '\0'; // terminate the string to be safe - c_attr->length[c_attr->value_count] = (*v)->bv_len + 1; + if (!(c_attr->values[c_attr->value_count] = malloc(len + 1))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for value failed"); + goto result; } + c_attr->length[c_attr->value_count] = len + 1; + memcpy(c_attr->values[c_attr->value_count], (*v)->bv_val, len); + c_attr->values[c_attr->value_count][len] = '\0'; c_attr->values[++c_attr->value_count] = NULL; } @@ -353,63 +350,71 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) return changes; } -int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { - CacheEntryAttribute **curs, **curb; - int i; +int copy_cache_entry(CacheEntry *src, CacheEntry *dst) +{ int rv = 1; - memset(backup_cache_entry, 0, sizeof(CacheEntry)); - for (curs = cache_entry->attributes; curs != NULL && *curs != NULL; curs++) { - if ((backup_cache_entry->attributes = realloc(backup_cache_entry->attributes, (backup_cache_entry->attribute_count + 2) * sizeof(CacheEntryAttribute*))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of attributes array failed"); + memset(dst, 0, sizeof(CacheEntry)); + + int a, a_count = src->attribute_count; + if (!(dst->attributes = calloc(a_count + 1, sizeof(CacheEntryAttribute*)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc of attributes array failed"); + goto result; + } + for (a = 0; a < a_count; a++) { + CacheEntryAttribute *a_dst, *a_src = src->attributes[a]; + int v, v_count = a_src->value_count; + + if ((a_dst = malloc(sizeof(CacheEntryAttribute))) == NULL) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for CacheEntryAttribute failed"); goto result; } - if ((backup_cache_entry->attributes[backup_cache_entry->attribute_count] = malloc(sizeof(CacheEntryAttribute))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for CacheEntryAttribute failed"); + memset(a_dst, 0, sizeof(CacheEntryAttribute)); + dst->attributes[a] = a_dst; + + if (!(a_dst->name = strdup(a_src->name))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup for CacheEntryAttribute.name failed"); goto result; } - curb = &backup_cache_entry->attributes[backup_cache_entry->attribute_count]; - (*curb)->name = strdup((*curs)->name); - (*curb)->values = NULL; - (*curb)->length = NULL; - (*curb)->value_count = 0; - backup_cache_entry->attributes[backup_cache_entry->attribute_count + 1] = NULL; - - for (i = 0; i < (*curs)->value_count; i++) { - if (((*curb)->values = realloc((*curb)->values, ((*curb)->value_count + 2) * sizeof(char*))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of values array failed"); - goto result; - } - if (((*curb)->length = realloc((*curb)->length, ((*curb)->value_count + 2) * sizeof(int))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of length array failed"); + + a_dst->value_count = v_count; + if (!(a_dst->values = calloc(a_dst->value_count + 1, sizeof(char*)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of values array failed"); + goto result; + } + if (!(a_dst->length = calloc(a_dst->value_count + 1, sizeof(int)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of length array failed"); + goto result; + } + for (v = 0; v < v_count; v++) { + char *v_dst, *v_src = a_src->values[v]; + int len = a_src->length[v]; + + if (!(v_dst = malloc(len))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc of value failed"); goto result; } - if ((*curs)->length[i] == strlen((*curs)->values[i]) + 1) { - if (((*curb)->values[(*curb)->value_count] = strdup((*curs)->values[i])) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup of value failed"); - goto result; - } - (*curb)->length[(*curb)->value_count] = strlen((*curb)->values[(*curb)->value_count]) + 1; - } else { - if (((*curb)->values[(*curb)->value_count] = malloc(((*curs)->length[i]) * sizeof(char))) == NULL) { - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for value failed"); - goto result; - } - memcpy((*curb)->values[(*curb)->value_count], (*curs)->values[i], (*curs)->length[i]); - (*curb)->length[(*curb)->value_count] = (*curs)->length[i]; - } - (*curb)->values[(*curb)->value_count+1] = NULL; - (*curb)->value_count++; + memcpy(v_dst, v_src, len); + a_dst->values[v] = v_dst; + a_dst->length[v] = len; } - backup_cache_entry->attribute_count++; + a_dst->values[v] = NULL; + } + dst->attributes[a] = NULL; + + int m, m_count = src->module_count; + if (!(dst->modules = calloc(m_count + 1, sizeof(char*)))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc of module array failed"); + goto result; } - char **module_ptr; - for (module_ptr = cache_entry->modules; module_ptr != NULL && *module_ptr != NULL; module_ptr++) { - backup_cache_entry->modules = realloc(backup_cache_entry->modules, (backup_cache_entry->module_count + 2) * sizeof(char*)); - backup_cache_entry->modules[backup_cache_entry->module_count] = strdup(*module_ptr); - backup_cache_entry->modules[backup_cache_entry->module_count +1] = NULL; - backup_cache_entry->module_count++; + for (m = 0; m < m_count; m++) { + if (!(dst->modules[m] = strdup(src->modules[m]))) { + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup of module failed"); + goto result; + } } + dst->modules[m] = NULL; + rv = 0; result: return rv; commit e0af7cc38261f1ddbaee11ce6d63431fcbdd5c84 Author: Philipp Hahn Date: Fri Feb 1 16:40:23 2013 +0100 Bug #30227: listener: Rename cursors Use more expressive variable names than cur[12]. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 5285b8d..a6a8a01 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -318,35 +318,35 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) { char **changes = NULL; int changes_count = 0; - CacheEntryAttribute **cur1, **cur2; + CacheEntryAttribute **curn, **curo; - for (cur1 = new->attributes; cur1 != NULL && *cur1 != NULL; cur1++) { - for (cur2 = old->attributes; cur2 != NULL && *cur2 != NULL; cur2++) - if (strcmp((*cur1)->name, (*cur2)->name) == 0) + for (curn = new->attributes; curn != NULL && *curn != NULL; curn++) { + for (curo = old->attributes; curo != NULL && *curo != NULL; curo++) + if (strcmp((*curn)->name, (*curo)->name) == 0) break; - if (cur2 != NULL && *cur2 != NULL && (*cur1)->value_count == (*cur2)->value_count) { + if (curo != NULL && *curo != NULL && (*curn)->value_count == (*curo)->value_count) { int i; - for (i = 0; i < (*cur1)->value_count; i++) - if (memcmp((*cur1)->values[i], (*cur2)->values[i], (*cur1)->length[i]) != 0) + for (i = 0; i < (*curn)->value_count; i++) + if (memcmp((*curn)->values[i], (*curo)->values[i], (*curn)->length[i]) != 0) break; - if (i == (*cur1)->value_count) + if (i == (*curn)->value_count) continue; } changes = realloc(changes, (changes_count + 2) * sizeof(char*)); - changes[changes_count++] = (*cur1)->name; + changes[changes_count++] = (*curn)->name; changes[changes_count] = NULL; } - for (cur2 = old->attributes; cur2 != NULL && *cur2 != NULL; cur2++) { - for (cur1 = new->attributes; cur1 != NULL && *cur1 != NULL; cur1++) - if (strcmp((*cur1)->name, (*cur2)->name) == 0) + for (curo = old->attributes; curo != NULL && *curo != NULL; curo++) { + for (curn = new->attributes; curn != NULL && *curn != NULL; curn++) + if (strcmp((*curn)->name, (*curo)->name) == 0) break; - if (cur1 != NULL && *cur1 != NULL) + if (curn != NULL && *curn != NULL) continue; changes = realloc(changes, (changes_count + 2) * sizeof(char*)); - changes[changes_count++] = (*cur2)->name; + changes[changes_count++] = (*curo)->name; changes[changes_count] = NULL; } @@ -354,12 +354,12 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) } int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { - CacheEntryAttribute **cur1, **cur2; + CacheEntryAttribute **curs, **curb; int i; int rv = 1; memset(backup_cache_entry, 0, sizeof(CacheEntry)); - for (cur1 = cache_entry->attributes; cur1 != NULL && *cur1 != NULL; cur1++) { + for (curs = cache_entry->attributes; curs != NULL && *curs != NULL; curs++) { if ((backup_cache_entry->attributes = realloc(backup_cache_entry->attributes, (backup_cache_entry->attribute_count + 2) * sizeof(CacheEntryAttribute*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of attributes array failed"); goto result; @@ -368,38 +368,38 @@ int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for CacheEntryAttribute failed"); goto result; } - cur2 = &backup_cache_entry->attributes[backup_cache_entry->attribute_count]; - (*cur2)->name = strdup((*cur1)->name); - (*cur2)->values = NULL; - (*cur2)->length = NULL; - (*cur2)->value_count = 0; + curb = &backup_cache_entry->attributes[backup_cache_entry->attribute_count]; + (*curb)->name = strdup((*curs)->name); + (*curb)->values = NULL; + (*curb)->length = NULL; + (*curb)->value_count = 0; backup_cache_entry->attributes[backup_cache_entry->attribute_count + 1] = NULL; - for (i = 0; i < (*cur1)->value_count; i++) { - if (((*cur2)->values = realloc((*cur2)->values, ((*cur2)->value_count + 2) * sizeof(char*))) == NULL) { + for (i = 0; i < (*curs)->value_count; i++) { + if (((*curb)->values = realloc((*curb)->values, ((*curb)->value_count + 2) * sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of values array failed"); goto result; } - if (((*cur2)->length = realloc((*cur2)->length, ((*cur2)->value_count + 2) * sizeof(int))) == NULL) { + if (((*curb)->length = realloc((*curb)->length, ((*curb)->value_count + 2) * sizeof(int))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of length array failed"); goto result; } - if ((*cur1)->length[i] == strlen((*cur1)->values[i]) + 1) { - if (((*cur2)->values[(*cur2)->value_count] = strdup((*cur1)->values[i])) == NULL) { + if ((*curs)->length[i] == strlen((*curs)->values[i]) + 1) { + if (((*curb)->values[(*curb)->value_count] = strdup((*curs)->values[i])) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup of value failed"); goto result; } - (*cur2)->length[(*cur2)->value_count] = strlen((*cur2)->values[(*cur2)->value_count]) + 1; + (*curb)->length[(*curb)->value_count] = strlen((*curb)->values[(*curb)->value_count]) + 1; } else { - if (((*cur2)->values[(*cur2)->value_count] = malloc(((*cur1)->length[i]) * sizeof(char))) == NULL) { + if (((*curb)->values[(*curb)->value_count] = malloc(((*curs)->length[i]) * sizeof(char))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for value failed"); goto result; } - memcpy((*cur2)->values[(*cur2)->value_count], (*cur1)->values[i], (*cur1)->length[i]); - (*cur2)->length[(*cur2)->value_count] = (*cur1)->length[i]; + memcpy((*curb)->values[(*curb)->value_count], (*curs)->values[i], (*curs)->length[i]); + (*curb)->length[(*curb)->value_count] = (*curs)->length[i]; } - (*cur2)->values[(*cur2)->value_count+1] = NULL; - (*cur2)->value_count++; + (*curb)->values[(*curb)->value_count+1] = NULL; + (*curb)->value_count++; } backup_cache_entry->attribute_count++; } @@ -462,22 +462,22 @@ void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) } free(changes); - char **cur1, **cur2; + char **curl, **curr; - for (cur1 = lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) { - for (cur2 = rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) - if (strcmp(*cur1, *cur2) == 0) + for (curl = lentry->modules; curl != NULL && *curl != NULL; curl++) { + for (curr = rentry->modules; curr != NULL && *curr != NULL; curr++) + if (strcmp(*curl, *curr) == 0) break; - if (cur2 != NULL && *cur2 != NULL) + if (curr != NULL && *curr != NULL) continue; - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: module %s on lentry missing on rentry\n", *cur1); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: module %s on lentry missing on rentry\n", *curl); } - for (cur2 = rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) { - for (cur1 = lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) - if (strcmp(*cur1, *cur2) == 0) + for (curr = rentry->modules; curr != NULL && *curr != NULL; curr++) { + for (curl = lentry->modules; curl != NULL && *curl != NULL; curl++) + if (strcmp(*curl, *curr) == 0) break; - if (cur1 != NULL && *cur1 != NULL) + if (curl != NULL && *curl != NULL) continue; - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: module %s on rentry missing on lentry\n", *cur2); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: module %s on rentry missing on lentry\n", *curr); } } commit af71e7a656af15b3b4cc790f1cd3d2a0622ecd84 Author: Philipp Hahn Date: Fri Feb 1 16:04:57 2013 +0100 Bug #30227: listener: declare functions extern Mark all functions as extern, because the compiler will otherwise expect the real declaration after the declaration. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h index 354c7a4..392e4dc 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h @@ -39,11 +39,11 @@ #define BASE64_ENCODE_LEN(n) (((n)+2)/3 * 4) #define BASE64_DECODE_LEN(n) (((n)+3)/4 * 3) -int base64_encode(u_char const *src, +extern int base64_encode(u_char const *src, size_t srclength, char *target, size_t targsize); -int base64_decode(char const *src, +extern int base64_decode(char const *src, u_char *target, size_t targsize); diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h index f222c7c..47f83d1 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h @@ -48,51 +48,51 @@ struct _CacheMasterEntry { } CacheMasterEntry; #endif -int cache_init(void); +extern int cache_init(void); #ifdef WITH_DB42 -int cache_get_master_entry(CacheMasterEntry *master_entry); -int cache_update_master_entry(CacheMasterEntry *master_entry, +extern int cache_get_master_entry(CacheMasterEntry *master_entry); +extern int cache_update_master_entry(CacheMasterEntry *master_entry, DB_TXN *dptxnp); #endif -int cache_update_entry(NotifierID id, +extern int cache_update_entry(NotifierID id, char *dn, CacheEntry *entry); inline int cache_update_entry_lower(NotifierID id, char *dn, CacheEntry *entry); -int cache_delete_entry(NotifierID id, +extern int cache_delete_entry(NotifierID id, char *dn); -int cache_delete_entry_lower_upper(NotifierID id, +extern int cache_delete_entry_lower_upper(NotifierID id, char *dn); -int cache_update_or_deleteifunused_entry(NotifierID id, +extern int cache_update_or_deleteifunused_entry(NotifierID id, char *dn, CacheEntry *entry); -int cache_get_entry(NotifierID id, +extern int cache_get_entry(NotifierID id, char *dn, CacheEntry *entry); -int cache_get_entry_lower_upper(NotifierID id, +extern int cache_get_entry_lower_upper(NotifierID id, char *dn, CacheEntry *entry); -int cache_first_entry(DBC **cur, +extern int cache_first_entry(DBC **cur, char **dn, CacheEntry *entry); -int cache_next_entry(DBC **cur, +extern int cache_next_entry(DBC **cur, char **dn, CacheEntry *entry); -int cache_free_cursor(DBC *cur); -int cache_close(void); +extern int cache_free_cursor(DBC *cur); +extern int cache_close(void); /* deprecated with DB42*/ -int cache_set_int(const char *key, +extern int cache_set_int(const char *key, const NotifierID value); -int cache_get_int(const char *key, +extern int cache_get_int(const char *key, NotifierID *value, const long def); -int cache_get_schema_id(const char *key, +extern int cache_get_schema_id(const char *key, NotifierID *value, const long def); -int cache_set_schema_id(const char *key, +extern int cache_set_schema_id(const char *key, const NotifierID value); #endif /* _CACHE_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h index aac27f6..4229e91 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h @@ -53,28 +53,28 @@ struct _CacheEntry { /* Initialize interal setting once. */ extern void cache_entry_init(void); -int cache_free_entry(char **dn, +extern int cache_free_entry(char **dn, CacheEntry *entry); -int cache_dump_entry(char *dn, +extern int cache_dump_entry(char *dn, CacheEntry *entry, FILE *fp); -int cache_new_entry_from_ldap(char **dn, +extern int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAPMessage *ldap_entry); -int cache_entry_module_add(CacheEntry *entry, +extern int cache_entry_module_add(CacheEntry *entry, char *module); -int cache_entry_module_remove(CacheEntry *entry, +extern int cache_entry_module_remove(CacheEntry *entry, char *module); -int cache_entry_module_present(CacheEntry *entry, +extern int cache_entry_module_present(CacheEntry *entry, char *module); -char** cache_entry_changed_attributes(CacheEntry *new, +extern char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old); -int copy_cache_entry(CacheEntry *cache_entry, +extern int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry); -void compare_cache_entries(CacheEntry *lentry, +extern void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry); #endif /* _CACHE_ENTRY_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h index 7c7fdca..1cf543e 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h @@ -35,13 +35,13 @@ #include "cache.h" -int unparse_entry(void **data, +extern int unparse_entry(void **data, u_int32_t *size, CacheEntry *entry); -int parse_entry(void *data, +extern int parse_entry(void *data, u_int32_t size, CacheEntry *entry); -void hex_dump(int level, +extern void hex_dump(int level, void *data, u_int32_t start, u_int32_t size); diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h index 8d2ddb9..fa6add0 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h @@ -38,16 +38,16 @@ #include "network.h" -int change_new_modules(univention_ldap_parameters_t *lp); -int change_update_schema(univention_ldap_parameters_t *lp); -int change_update_entry(univention_ldap_parameters_t *lp, +extern int change_new_modules(univention_ldap_parameters_t *lp); +extern int change_update_schema(univention_ldap_parameters_t *lp); +extern int change_update_entry(univention_ldap_parameters_t *lp, NotifierID id, LDAPMessage *ldap_entry, char command); -int change_delete_dn(NotifierID id, +extern int change_delete_dn(NotifierID id, char *dn, char command); -int change_update_dn(univention_ldap_parameters_t *lp, +extern int change_update_dn(univention_ldap_parameters_t *lp, NotifierID id, char *dn, char command, diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/common.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/common.h index c3d743f..a19066f 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/common.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/common.h @@ -33,7 +33,7 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -void drop_privileges(void); +extern void drop_privileges(void); #ifdef DMALLOC #include diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h index 65f1a21..67f14db 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h @@ -36,7 +36,7 @@ #include "cache.h" #include "handlers.h" -int cache_entry_ldap_filter_match(struct filter **filter, +extern int cache_entry_ldap_filter_match(struct filter **filter, char *dn, CacheEntry *entry); diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h index f91042a..c9987d6 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h @@ -79,32 +79,32 @@ struct _Handler { int prepared : 1; } typedef Handler; -int handlers_init(void); -int handlers_free_all(void); -int handlers_load_path(char *filename); -int handlers_reload_all_paths(void); -int handlers_dump(void); -int handlers_update(char *dn, +extern int handlers_init(void); +extern int handlers_free_all(void); +extern int handlers_load_path(char *filename); +extern int handlers_reload_all_paths(void); +extern int handlers_dump(void); +extern int handlers_update(char *dn, CacheEntry *new, CacheEntry *old, char command, CacheEntry *scratch); -int handler_update(char *dn, +extern int handler_update(char *dn, CacheEntry *new, CacheEntry *old, Handler *handler, char command, CacheEntry *scratch); -int handlers_delete(char *dn, +extern int handlers_delete(char *dn, CacheEntry *old, char command); -int handler_clean(Handler *handler); -int handlers_clean_all(void); -int handler_initialize(Handler *handler); -int handlers_initialize_all(void); -int handlers_postrun_all(void); -int handlers_set_data_all(char *key, +extern int handler_clean(Handler *handler); +extern int handlers_clean_all(void); +extern int handler_initialize(Handler *handler); +extern int handlers_initialize_all(void); +extern int handlers_postrun_all(void); +extern int handlers_set_data_all(char *key, char *value); -char *handlers_filter(void); +extern char *handlers_filter(void); #endif /* _HANDLERS_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h index 8bb9dd6..ff12f4a 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h @@ -61,31 +61,31 @@ struct _NotifierClient { char *buf; } typedef NotifierClient; -void notifier_entry_free(NotifierEntry *entry); -int notifier_client_new(NotifierClient *client, +extern void notifier_entry_free(NotifierEntry *entry); +extern int notifier_client_new(NotifierClient *client, const char *server, int starttls); -void notifier_client_destroy(NotifierClient *client); -int notifier_wait(NotifierClient *client, +extern void notifier_client_destroy(NotifierClient *client); +extern int notifier_wait(NotifierClient *client, time_t timeout); -int notifier_recv_result(NotifierClient *client, +extern int notifier_recv_result(NotifierClient *client, time_t timeout); -NotifierMessage* notifier_get_msg(NotifierClient *client, +extern NotifierMessage* notifier_get_msg(NotifierClient *client, int msgid); -int notifier_get_dn(NotifierClient *client, +extern int notifier_get_dn(NotifierClient *client, NotifierID id); -int notifier_resend_get_dn(NotifierClient *client, +extern int notifier_resend_get_dn(NotifierClient *client, int msgid, NotifierID id); -int notifier_get_dn_result(NotifierClient *client, +extern int notifier_get_dn_result(NotifierClient *client, int msgid, NotifierEntry *entry); -int notifier_alive_s(NotifierClient *client); -int notifier_get_id_s(NotifierClient *client, +extern int notifier_alive_s(NotifierClient *client); +extern int notifier_get_id_s(NotifierClient *client, NotifierID *id); -int notifier_get_schema_id_s(NotifierClient *client, +extern int notifier_get_schema_id_s(NotifierClient *client, NotifierID *id); #endif /* _NETWORK_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h index 4846844..26bad38 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h @@ -40,7 +40,7 @@ typedef void univention_krb5_parameters_t; #endif -int notifier_listen(univention_ldap_parameters_t *lp, +extern int notifier_listen(univention_ldap_parameters_t *lp, univention_krb5_parameters_t *kp, int write_transaction_file, univention_ldap_parameters_t *lp_local); diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/select_server.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/select_server.h index aca4f0e..0bf42e7 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/select_server.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/select_server.h @@ -40,7 +40,7 @@ struct server_list { int conn_attemp; }; -void select_server(univention_ldap_parameters_t *lp); -int suspend_connect(void); +extern void select_server(univention_ldap_parameters_t *lp); +extern int suspend_connect(void); #endif diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h index 2e0ed87..bd12cae 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h @@ -33,9 +33,9 @@ #ifndef _SIGNALS_H_ #define _SIGNALS_H_ -void signals_block(void); -void signals_unblock(void); -void signals_init(void); +extern void signals_block(void); +extern void signals_unblock(void); +extern void signals_init(void); extern void exit_handler(int sig) __attribute__((noreturn)); diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h index 00a9860..0a982c6 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h @@ -36,6 +36,6 @@ #include "network.h" extern char *transaction_file; -int notifier_write_transaction_file(NotifierEntry entry); +extern int notifier_write_transaction_file(NotifierEntry entry); #endif /* _TRANSFILE_H_ */ commit cf13f6b1853dd63d2e92663d316c76019c922f35 Author: Philipp Hahn Date: Fri Feb 1 15:00:32 2013 +0100 Bug #30227: listener: white space cleanup Remove traling blanks. Break long lines. Insert blanks before assignment, comparison, after keyword. Reformat header files. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h index f027864..354c7a4 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/base64.h @@ -39,12 +39,12 @@ #define BASE64_ENCODE_LEN(n) (((n)+2)/3 * 4) #define BASE64_DECODE_LEN(n) (((n)+3)/4 * 3) -int base64_encode (u_char const *src, - size_t srclength, - char *target, - size_t targsize); -int base64_decode (char const *src, - u_char *target, - size_t targsize); +int base64_encode(u_char const *src, + size_t srclength, + char *target, + size_t targsize); +int base64_decode(char const *src, + u_char *target, + size_t targsize); #endif /* _BASE64_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h index 5752075..f222c7c 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache.h @@ -48,48 +48,51 @@ struct _CacheMasterEntry { } CacheMasterEntry; #endif -int cache_init (void); +int cache_init(void); #ifdef WITH_DB42 -int cache_get_master_entry (CacheMasterEntry *master_entry); -int cache_update_master_entry (CacheMasterEntry *master_entry, - DB_TXN *dptxnp); +int cache_get_master_entry(CacheMasterEntry *master_entry); +int cache_update_master_entry(CacheMasterEntry *master_entry, + DB_TXN *dptxnp); #endif -int cache_update_entry (NotifierID id, - char *dn, - CacheEntry *entry); -inline int cache_update_entry_lower (NotifierID id, - char *dn, - CacheEntry *entry); -int cache_delete_entry (NotifierID id, - char *dn); -int cache_delete_entry_lower_upper (NotifierID id, - char *dn); -int cache_update_or_deleteifunused_entry (NotifierID id, - char *dn, - CacheEntry *entry); -int cache_get_entry (NotifierID id, - char *dn, - CacheEntry *entry); -int cache_get_entry_lower_upper (NotifierID id, - char *dn, - CacheEntry *entry); -int cache_first_entry (DBC **cur, - char **dn, - CacheEntry *entry); -int cache_next_entry (DBC **cur, - char **dn, - CacheEntry *entry); -int cache_free_cursor (DBC *cur); -int cache_close (void); +int cache_update_entry(NotifierID id, + char *dn, + CacheEntry *entry); +inline int cache_update_entry_lower(NotifierID id, + char *dn, + CacheEntry *entry); +int cache_delete_entry(NotifierID id, + char *dn); +int cache_delete_entry_lower_upper(NotifierID id, + char *dn); +int cache_update_or_deleteifunused_entry(NotifierID id, + char *dn, + CacheEntry *entry); +int cache_get_entry(NotifierID id, + char *dn, + CacheEntry *entry); +int cache_get_entry_lower_upper(NotifierID id, + char *dn, + CacheEntry *entry); +int cache_first_entry(DBC **cur, + char **dn, + CacheEntry *entry); +int cache_next_entry(DBC **cur, + char **dn, + CacheEntry *entry); +int cache_free_cursor(DBC *cur); +int cache_close(void); /* deprecated with DB42*/ -int cache_set_int (const char *key, - const NotifierID value); -int cache_get_int (const char *key, - NotifierID *value, - const long def); +int cache_set_int(const char *key, + const NotifierID value); +int cache_get_int(const char *key, + NotifierID *value, + const long def); -int cache_get_schema_id(const char *key, NotifierID *value, const long def); -int cache_set_schema_id(const char *key, const NotifierID value); +int cache_get_schema_id(const char *key, + NotifierID *value, + const long def); +int cache_set_schema_id(const char *key, + const NotifierID value); #endif /* _CACHE_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 9c7f544..5285b8d 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -114,7 +114,7 @@ int cache_dump_entry(char *dn, CacheEntry *entry, FILE *fp) if (len >= 0) { char *base64_value; size_t srclen = attribute->length[j] - 1; - base64_value = malloc(BASE64_ENCODE_LEN(srclen)+1); + base64_value = malloc(BASE64_ENCODE_LEN(srclen) + 1); if (!base64_value) return 1; base64_encode((u_char *)value, srclen, base64_value, BASE64_ENCODE_LEN(srclen) + 1); @@ -125,7 +125,7 @@ int cache_dump_entry(char *dn, CacheEntry *entry, FILE *fp) } } } - for (module=entry->modules; module != NULL && *module != NULL; module++) { + for (module = entry->modules; module != NULL && *module != NULL; module++) { fprintf(fp, "listenerModule: %s\n", *module); } @@ -136,12 +136,12 @@ int cache_entry_module_add(CacheEntry *entry, char *module) { char **cur; - for (cur=entry->modules; cur != NULL && *cur != NULL; cur++) { + for (cur = entry->modules; cur != NULL && *cur != NULL; cur++) { if (strcmp(*cur, module) == 0) return 0; } - entry->modules = realloc(entry->modules, (entry->module_count+2)*sizeof(char*)); + entry->modules = realloc(entry->modules, (entry->module_count + 2) * sizeof(char*)); if (!entry->modules) return 1; entry->modules[entry->module_count++] = strdup(module); @@ -154,7 +154,7 @@ int cache_entry_module_remove(CacheEntry *entry, char *module) { char **cur; - for (cur=entry->modules; cur != NULL && *cur != NULL; cur++) { + for (cur = entry->modules; cur != NULL && *cur != NULL; cur++) { if (strcmp(*cur, module) == 0) break; } @@ -164,10 +164,10 @@ int cache_entry_module_remove(CacheEntry *entry, char *module) /* replace entry that is to be removed with last entry */ free(*cur); - entry->modules[cur-entry->modules] = entry->modules[entry->module_count-1]; + entry->modules[cur-entry->modules] = entry->modules[entry->module_count - 1]; entry->modules[--entry->module_count] = NULL; - entry->modules = realloc(entry->modules, (entry->module_count+1)*sizeof(char*)); + entry->modules = realloc(entry->modules, (entry->module_count + 1) * sizeof(char*)); return 0; } @@ -178,7 +178,7 @@ int cache_entry_module_present(CacheEntry *entry, char *module) if (entry == NULL) return 0; - for (cur=entry->modules; cur != NULL && *cur != NULL; cur++) { + for (cur = entry->modules; cur != NULL && *cur != NULL; cur++) { if (strcmp(*cur, module) == 0) return 1; } @@ -200,16 +200,18 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP memset(cache_entry, 0, sizeof(CacheEntry)); if (dn != NULL) { char *_dn = ldap_get_dn(ld, ldap_entry); - if(*dn) - free(*dn); + if (*dn) + free(*dn); *dn = strdup(_dn); ldap_memfree(_dn); } - for (attr=ldap_first_attribute(ld, ldap_entry, &ber); attr != NULL; attr=ldap_next_attribute(ld, ldap_entry, ber)) { + for (attr = ldap_first_attribute(ld, ldap_entry, &ber); + attr != NULL; + attr = ldap_next_attribute(ld, ldap_entry, ber)) { struct berval **val, **v; - if ((cache_entry->attributes = realloc(cache_entry->attributes, (cache_entry->attribute_count+2)*sizeof(CacheEntryAttribute*))) == NULL) { + if ((cache_entry->attributes = realloc(cache_entry->attributes, (cache_entry->attribute_count + 2) * sizeof(CacheEntryAttribute*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of attributes array failed"); goto result; } @@ -230,12 +232,12 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP check = UNIQUE_MEMBER; else check = DUPLICATES; - if ((val=ldap_get_values_len(ld, ldap_entry, attr)) == NULL) { + if ((val = ldap_get_values_len(ld, ldap_entry, attr)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ldap_get_values failed"); goto result; } for (v = val; *v != NULL; v++) { - if ( (*v)->bv_val == NULL ) { + if ((*v)->bv_val == NULL) { // check here, strlen behavior might be undefined in this case univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: ignoring bv_val of NULL with bv_len=%ld, ignoring, check attribute: %s of DN: %s", (*v)->bv_len, c_attr->name, *dn); goto result; @@ -243,7 +245,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if (memberUidMode && check == UNIQUE_UID) { /* avoid duplicate memberUid entries https://forge.univention.org/bugzilla/show_bug.cgi?id=17998 */ for (i = 0; i < c_attr->value_count; i++) { - if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { + if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len + 1) ) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate memberUid entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "memberUid: %s", c_attr->values[i]); @@ -257,7 +259,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if (uniqueMemberMode && check == UNIQUE_MEMBER) { /* avoid duplicate uniqueMember entries https://forge.univention.org/bugzilla/show_bug.cgi?id=18692 */ for (i = 0; i < c_attr->value_count; i++) { - if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1)) { + if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len + 1)) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate uniqueMember entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "uniqueMember: %s", c_attr->values[i]); @@ -331,7 +333,7 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) continue; } - changes = realloc(changes, (changes_count+2)*sizeof(char*)); + changes = realloc(changes, (changes_count + 2) * sizeof(char*)); changes[changes_count++] = (*cur1)->name; changes[changes_count] = NULL; } @@ -343,7 +345,7 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) if (cur1 != NULL && *cur1 != NULL) continue; - changes = realloc(changes, (changes_count+2)*sizeof(char*)); + changes = realloc(changes, (changes_count + 2) * sizeof(char*)); changes[changes_count++] = (*cur2)->name; changes[changes_count] = NULL; } @@ -353,11 +355,12 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { CacheEntryAttribute **cur1, **cur2; - int i=0; - int rv=1; + int i; + int rv = 1; + memset(backup_cache_entry, 0, sizeof(CacheEntry)); for (cur1 = cache_entry->attributes; cur1 != NULL && *cur1 != NULL; cur1++) { - if ((backup_cache_entry->attributes = realloc(backup_cache_entry->attributes, (backup_cache_entry->attribute_count+2)*sizeof(CacheEntryAttribute*))) == NULL) { + if ((backup_cache_entry->attributes = realloc(backup_cache_entry->attributes, (backup_cache_entry->attribute_count + 2) * sizeof(CacheEntryAttribute*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of attributes array failed"); goto result; } @@ -366,45 +369,45 @@ int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { goto result; } cur2 = &backup_cache_entry->attributes[backup_cache_entry->attribute_count]; - (*cur2)->name=strdup((*cur1)->name); - (*cur2)->values=NULL; - (*cur2)->length=NULL; - (*cur2)->value_count=0; - backup_cache_entry->attributes[backup_cache_entry->attribute_count+1]=NULL; + (*cur2)->name = strdup((*cur1)->name); + (*cur2)->values = NULL; + (*cur2)->length = NULL; + (*cur2)->value_count = 0; + backup_cache_entry->attributes[backup_cache_entry->attribute_count + 1] = NULL; for (i = 0; i < (*cur1)->value_count; i++) { - if (((*cur2)->values = realloc((*cur2)->values, ((*cur2)->value_count+2)*sizeof(char*))) == NULL) { + if (((*cur2)->values = realloc((*cur2)->values, ((*cur2)->value_count + 2) * sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of values array failed"); goto result; } - if (((*cur2)->length = realloc((*cur2)->length, ((*cur2)->value_count+2)*sizeof(int))) == NULL) { + if (((*cur2)->length = realloc((*cur2)->length, ((*cur2)->value_count + 2) * sizeof(int))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of length array failed"); goto result; } if ((*cur1)->length[i] == strlen((*cur1)->values[i]) + 1) { - if (((*cur2)->values[(*cur2)->value_count]=strdup((*cur1)->values[i])) == NULL) { + if (((*cur2)->values[(*cur2)->value_count] = strdup((*cur1)->values[i])) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup of value failed"); goto result; } - (*cur2)->length[(*cur2)->value_count]=strlen((*cur2)->values[(*cur2)->value_count])+1; + (*cur2)->length[(*cur2)->value_count] = strlen((*cur2)->values[(*cur2)->value_count]) + 1; } else { - if (((*cur2)->values[(*cur2)->value_count]=malloc(((*cur1)->length[i])*sizeof(char))) == NULL) { + if (((*cur2)->values[(*cur2)->value_count] = malloc(((*cur1)->length[i]) * sizeof(char))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for value failed"); goto result; } - memcpy((*cur2)->values[(*cur2)->value_count],(*cur1)->values[i],(*cur1)->length[i]); - (*cur2)->length[(*cur2)->value_count]=(*cur1)->length[i]; + memcpy((*cur2)->values[(*cur2)->value_count], (*cur1)->values[i], (*cur1)->length[i]); + (*cur2)->length[(*cur2)->value_count] = (*cur1)->length[i]; } - (*cur2)->values[(*cur2)->value_count+1]=NULL; + (*cur2)->values[(*cur2)->value_count+1] = NULL; (*cur2)->value_count++; } backup_cache_entry->attribute_count++; } char **module_ptr; - for (module_ptr=cache_entry->modules; module_ptr != NULL && *module_ptr != NULL; module_ptr++) { - backup_cache_entry->modules = realloc(backup_cache_entry->modules, (backup_cache_entry->module_count+2)*sizeof(char*)); + for (module_ptr = cache_entry->modules; module_ptr != NULL && *module_ptr != NULL; module_ptr++) { + backup_cache_entry->modules = realloc(backup_cache_entry->modules, (backup_cache_entry->module_count + 2) * sizeof(char*)); backup_cache_entry->modules[backup_cache_entry->module_count] = strdup(*module_ptr); - backup_cache_entry->modules[backup_cache_entry->module_count+1] = NULL; + backup_cache_entry->modules[backup_cache_entry->module_count +1] = NULL; backup_cache_entry->module_count++; } rv = 0; @@ -414,16 +417,16 @@ result: void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) { - char **changes; - char **cur; - int i=0; + char **changes; + char **cur; + int i; changes = cache_entry_changed_attributes(lentry, rentry); for (cur = changes; cur != NULL && *cur != NULL; cur++) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: %s differs\n", *cur); - for (i=0; lentry->attributes != NULL && lentry->attributes[i] != NULL; i++) { + for (i = 0; lentry->attributes != NULL && lentry->attributes[i] != NULL; i++) { if (strcmp(lentry->attributes[i]->name, *cur) == 0) break; } @@ -432,7 +435,7 @@ void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) } else { int j; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: lentry = ["); - for (j=0; lentry->attributes[i]->values && + for (j = 0; lentry->attributes[i]->values && lentry->attributes[i]->values[j] != NULL; j++) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, j == 0 ? "%s" : ", %s", lentry->attributes[i]->values[j]); @@ -440,7 +443,7 @@ void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "]\n"); } - for (i=0; rentry->attributes != NULL && rentry->attributes[i] != NULL; i++) { + for (i = 0; rentry->attributes != NULL && rentry->attributes[i] != NULL; i++) { if (strcmp(rentry->attributes[i]->name, *cur) == 0) break; } @@ -449,7 +452,7 @@ void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) } else { int j; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: rentry = ["); - for (j=0; rentry->attributes[i]->values && + for (j = 0; rentry->attributes[i]->values && rentry->attributes[i]->values[j] != NULL; j++) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, j == 0 ? "%s" : ", %s", rentry->attributes[i]->values[j]); @@ -459,18 +462,18 @@ void compare_cache_entries(CacheEntry *lentry, CacheEntry *rentry) } free(changes); - char **cur1, **cur2; + char **cur1, **cur2; - for (cur1=lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) { - for (cur2=rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) + for (cur1 = lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) { + for (cur2 = rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) if (strcmp(*cur1, *cur2) == 0) break; if (cur2 != NULL && *cur2 != NULL) continue; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ALERT: module %s on lentry missing on rentry\n", *cur1); } - for (cur2=rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) { - for (cur1=lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) + for (cur2 = rentry->modules; cur2 != NULL && *cur2 != NULL; cur2++) { + for (cur1 = lentry->modules; cur1 != NULL && *cur1 != NULL; cur1++) if (strcmp(*cur1, *cur2) == 0) break; if (cur1 != NULL && *cur1 != NULL) diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h index 754327f..aac27f6 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h @@ -37,44 +37,44 @@ #include struct _CacheEntryAttribute { - char *name; - char **values; - int *length; - int value_count; + char *name; + char **values; + int *length; + int value_count; } typedef CacheEntryAttribute; struct _CacheEntry { - CacheEntryAttribute **attributes; - int attribute_count; - char **modules; - int module_count; + CacheEntryAttribute **attributes; + int attribute_count; + char **modules; + int module_count; } typedef CacheEntry; /* Initialize interal setting once. */ extern void cache_entry_init(void); -int cache_free_entry (char **dn, - CacheEntry *entry); -int cache_dump_entry (char *dn, - CacheEntry *entry, - FILE *fp); -int cache_new_entry_from_ldap (char **dn, - CacheEntry *cache_entry, - LDAP *ld, - LDAPMessage *ldap_entry); -int cache_entry_module_add (CacheEntry *entry, - char *module); -int cache_entry_module_remove (CacheEntry *entry, - char *module); -int cache_entry_module_present (CacheEntry *entry, - char *module); -char** cache_entry_changed_attributes (CacheEntry *new, - CacheEntry *old); +int cache_free_entry(char **dn, + CacheEntry *entry); +int cache_dump_entry(char *dn, + CacheEntry *entry, + FILE *fp); +int cache_new_entry_from_ldap(char **dn, + CacheEntry *cache_entry, + LDAP *ld, + LDAPMessage *ldap_entry); +int cache_entry_module_add(CacheEntry *entry, + char *module); +int cache_entry_module_remove(CacheEntry *entry, + char *module); +int cache_entry_module_present(CacheEntry *entry, + char *module); +char** cache_entry_changed_attributes(CacheEntry *new, + CacheEntry *old); -int copy_cache_entry (CacheEntry *cache_entry, - CacheEntry *backup_cache_entry); +int copy_cache_entry(CacheEntry *cache_entry, + CacheEntry *backup_cache_entry); -void compare_cache_entries (CacheEntry *lentry, - CacheEntry *rentry); +void compare_cache_entries(CacheEntry *lentry, + CacheEntry *rentry); #endif /* _CACHE_ENTRY_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h index 58fecc6..7c7fdca 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_lowlevel.h @@ -35,12 +35,15 @@ #include "cache.h" -int unparse_entry (void **data, - u_int32_t *size, - CacheEntry *entry); -int parse_entry (void *data, - u_int32_t size, - CacheEntry *entry); -void hex_dump(int level, void *data, u_int32_t start, u_int32_t size); +int unparse_entry(void **data, + u_int32_t *size, + CacheEntry *entry); +int parse_entry(void *data, + u_int32_t size, + CacheEntry *entry); +void hex_dump(int level, + void *data, + u_int32_t start, + u_int32_t size); #endif /* _CACHE_LOWLEVEL_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h index 18d9093..8d2ddb9 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/change.h @@ -38,20 +38,19 @@ #include "network.h" - -int change_new_modules (univention_ldap_parameters_t *lp); -int change_update_schema (univention_ldap_parameters_t *lp); -int change_update_entry (univention_ldap_parameters_t *lp, - NotifierID id, - LDAPMessage *ldap_entry, - char command); -int change_delete_dn (NotifierID id, - char *dn, - char command); -int change_update_dn (univention_ldap_parameters_t *lp, - NotifierID id, - char *dn, - char command, - univention_ldap_parameters_t *lp_local); +int change_new_modules(univention_ldap_parameters_t *lp); +int change_update_schema(univention_ldap_parameters_t *lp); +int change_update_entry(univention_ldap_parameters_t *lp, + NotifierID id, + LDAPMessage *ldap_entry, + char command); +int change_delete_dn(NotifierID id, + char *dn, + char command); +int change_update_dn(univention_ldap_parameters_t *lp, + NotifierID id, + char *dn, + char command, + univention_ldap_parameters_t *lp_local); #endif /* _CHANGE_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h index b85383c..65f1a21 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/filter.h @@ -36,8 +36,8 @@ #include "cache.h" #include "handlers.h" -int cache_entry_ldap_filter_match (struct filter **filter, - char *dn, - CacheEntry *entry); +int cache_entry_ldap_filter_match(struct filter **filter, + char *dn, + CacheEntry *entry); #endif /* _FILTER_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h index e34c6b8..f91042a 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/handlers.h @@ -55,55 +55,56 @@ #define HANDLER_UNSET_FLAG(handler, flag) handler->state &= ~flag struct filter { - char *base; - int scope; - char *filter; + char *base; + int scope; + char *filter; }; struct _Handler { - PyObject *module; - char *name; - char *description; - struct filter **filters; - char **attributes; - char *modrdn; - PyObject *handler; - PyObject *initialize; - PyObject *clean; - PyObject *postrun; - PyObject *prerun; - PyObject *setdata; - struct _Handler *next; + PyObject *module; + char *name; + char *description; + struct filter **filters; + char **attributes; + char *modrdn; + PyObject *handler; + PyObject *initialize; + PyObject *clean; + PyObject *postrun; + PyObject *prerun; + PyObject *setdata; + struct _Handler *next; - int state; - int prepared : 1; + int state; + int prepared : 1; } typedef Handler; -int handlers_init (void); -int handlers_free_all (void); -int handlers_load_path (char *filename); -int handlers_reload_all_paths (void); -int handlers_dump (void); -int handlers_update (char *dn, - CacheEntry *new, - CacheEntry *old, - char command, - CacheEntry *scratch); -int handler_update (char *dn, - CacheEntry *new, - CacheEntry *old, - Handler *handler, - char command, - CacheEntry *scratch); -int handlers_delete (char *dn, - CacheEntry *old, - char command); -int handler_clean (Handler *handler); -int handlers_clean_all (void); -int handler_initialize (Handler *handler); -int handlers_initialize_all (void); -int handlers_postrun_all (void); -int handlers_set_data_all (char *key, char *value); -char* handlers_filter (void); +int handlers_init(void); +int handlers_free_all(void); +int handlers_load_path(char *filename); +int handlers_reload_all_paths(void); +int handlers_dump(void); +int handlers_update(char *dn, + CacheEntry *new, + CacheEntry *old, + char command, + CacheEntry *scratch); +int handler_update(char *dn, + CacheEntry *new, + CacheEntry *old, + Handler *handler, + char command, + CacheEntry *scratch); +int handlers_delete(char *dn, + CacheEntry *old, + char command); +int handler_clean(Handler *handler); +int handlers_clean_all(void); +int handler_initialize(Handler *handler); +int handlers_initialize_all(void); +int handlers_postrun_all(void); +int handlers_set_data_all(char *key, + char *value); +char *handlers_filter(void); #endif /* _HANDLERS_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h index f8ac691..8bb9dd6 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/network.h @@ -40,52 +40,52 @@ typedef unsigned long NotifierID; struct _NotifierEntry { - NotifierID id; - char *dn; - char command; /* 'd'elete, 'm'odify, 'a'dd */ + NotifierID id; + char *dn; + char command; /* 'd'elete, 'm'odify, 'a'dd */ } typedef NotifierEntry; struct _NotifierMessage { - int id; - char *result; - struct _NotifierMessage *next; + int id; + char *result; + struct _NotifierMessage *next; } typedef NotifierMessage; struct _NotifierClient { - char *server; - int protocol; - int starttls; - int fd; - NotifierMessage *messages; - int last_msgid; - char *buf; + char *server; + int protocol; + int starttls; + int fd; + NotifierMessage *messages; + int last_msgid; + char *buf; } typedef NotifierClient; -void notifier_entry_free (NotifierEntry *entry); -int notifier_client_new (NotifierClient *client, - const char *server, - int starttls); -void notifier_client_destroy (NotifierClient *client); -int notifier_wait (NotifierClient *client, - time_t timeout); +void notifier_entry_free(NotifierEntry *entry); +int notifier_client_new(NotifierClient *client, + const char *server, + int starttls); +void notifier_client_destroy(NotifierClient *client); +int notifier_wait(NotifierClient *client, + time_t timeout); -int notifier_recv_result (NotifierClient *client, - time_t timeout); -NotifierMessage* notifier_get_msg (NotifierClient *client, - int msgid); +int notifier_recv_result(NotifierClient *client, + time_t timeout); +NotifierMessage* notifier_get_msg(NotifierClient *client, + int msgid); -int notifier_get_dn (NotifierClient *client, - NotifierID id); -int notifier_resend_get_dn (NotifierClient *client, - int msgid, - NotifierID id); -int notifier_get_dn_result (NotifierClient *client, - int msgid, - NotifierEntry *entry); -int notifier_alive_s (NotifierClient *client); -int notifier_get_id_s (NotifierClient *client, - NotifierID *id); -int notifier_get_schema_id_s (NotifierClient *client, - NotifierID *id); +int notifier_get_dn(NotifierClient *client, + NotifierID id); +int notifier_resend_get_dn(NotifierClient *client, + int msgid, + NotifierID id); +int notifier_get_dn_result(NotifierClient *client, + int msgid, + NotifierEntry *entry); +int notifier_alive_s(NotifierClient *client); +int notifier_get_id_s(NotifierClient *client, + NotifierID *id); +int notifier_get_schema_id_s(NotifierClient *client, + NotifierID *id); #endif /* _NETWORK_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h index a88fe16..4846844 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/notifier.h @@ -40,9 +40,9 @@ typedef void univention_krb5_parameters_t; #endif -int notifier_listen (univention_ldap_parameters_t *lp, - univention_krb5_parameters_t *kp, - int write_transaction_file, - univention_ldap_parameters_t *lp_local); +int notifier_listen(univention_ldap_parameters_t *lp, + univention_krb5_parameters_t *kp, + int write_transaction_file, + univention_ldap_parameters_t *lp_local); #endif /* _NOTIFIER_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h index 9d5c40d..2e0ed87 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/signals.h @@ -33,10 +33,10 @@ #ifndef _SIGNALS_H_ #define _SIGNALS_H_ -void signals_block (void); -void signals_unblock (void); -void signals_init (void); +void signals_block(void); +void signals_unblock(void); +void signals_init(void); -extern void exit_handler (int sig) __attribute__((noreturn)); +extern void exit_handler(int sig) __attribute__((noreturn)); #endif /* _SIGNALS_H_ */ diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h index f9519b2..00a9860 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/transfile.h @@ -36,6 +36,6 @@ #include "network.h" extern char *transaction_file; -int notifier_write_transaction_file (NotifierEntry entry); +int notifier_write_transaction_file(NotifierEntry entry); #endif /* _TRANSFILE_H_ */ commit 945941ef83002fbda20d690e27e9a42a7ad37185 Author: Philipp Hahn Date: Fri Feb 1 14:45:04 2013 +0100 Bug #30227: listener: fix ucr memory leak For every entry * for every attribute two UCR variable for the duplicate check were allocated, but never freed. Also initialize unique handling once instead of parsing base.conf twice for each entry. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 8b92a2a..9c7f544 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -42,6 +42,24 @@ #include "cache_entry.h" #include "base64.h" +static bool memberUidMode; +static bool uniqueMemberMode; + +/* Initialize interal setting once. */ +void cache_entry_init(void) +{ + ucrval = univention_config_get_string("listener/memberuid/skip"); + if (ucrval) { + memberUidMode |= !strcmp(ucrval, "yes") || !strcmp(ucrval, "true"); + free(ucrval); + } + ucrval = univention_config_get_string("listener/uniquemember/skip"); + if (ucrval) { + uniqueMemberMode |= !strcmp(ucrval, "yes") || !strcmp(ucrval, "true"); + free(ucrval); + } +} + int cache_free_entry(char **dn, CacheEntry *entry) { int i, j; @@ -174,9 +192,9 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP char *attr; int rv = 1; - bool memberUidMode = false; - bool uniqueMemberMode = false; int i; + char *ucrval; + enum { DUPLICATES, UNIQUE_UID, UNIQUE_MEMBER } check; /* convert LDAP entry to cache entry */ memset(cache_entry, 0, sizeof(CacheEntry)); @@ -206,26 +224,12 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP cache_entry->attributes[cache_entry->attribute_count++] = c_attr; cache_entry->attributes[cache_entry->attribute_count] = NULL; - memberUidMode = false; - if (!strncmp(c_attr->name, "memberUid", strlen("memberUid"))) { - char *ucrval; - ucrval = univention_config_get_string("listener/memberuid/skip"); - - if (ucrval) { - memberUidMode = !strcmp(ucrval, "yes") || !strcmp(ucrval, "true"); - free(ucrval); - } - } - uniqueMemberMode = false; - if (!strncmp(c_attr->name, "uniqueMember", strlen("uniqueMember"))) { - char *ucrval; - ucrval = univention_config_get_string("listener/uniquemember/skip"); - - if (ucrval) { - uniqueMemberMode = !strcmp(ucrval, "yes") || !strcmp(ucrval, "true"); - free(ucrval); - } - } + if (!strcmp(c_attr->name, "memberUid")) + check = UNIQUE_UID; + else if (!strcmp(c_attr->name, "uniqueMember")) + check = UNIQUE_MEMBER; + else + check = DUPLICATES; if ((val=ldap_get_values_len(ld, ldap_entry, attr)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ldap_get_values failed"); goto result; @@ -236,7 +240,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: ignoring bv_val of NULL with bv_len=%ld, ignoring, check attribute: %s of DN: %s", (*v)->bv_len, c_attr->name, *dn); goto result; } - if (memberUidMode) { + if (memberUidMode && check == UNIQUE_UID) { /* avoid duplicate memberUid entries https://forge.univention.org/bugzilla/show_bug.cgi?id=17998 */ for (i = 0; i < c_attr->value_count; i++) { if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { @@ -250,7 +254,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if (i < c_attr->value_count) continue; } - if (uniqueMemberMode) { + if (uniqueMemberMode && check == UNIQUE_MEMBER) { /* avoid duplicate uniqueMember entries https://forge.univention.org/bugzilla/show_bug.cgi?id=18692 */ for (i = 0; i < c_attr->value_count; i++) { if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1)) { diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h index 58cb04c..754327f 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.h @@ -50,6 +50,9 @@ struct _CacheEntry { int module_count; } typedef CacheEntry; +/* Initialize interal setting once. */ +extern void cache_entry_init(void); + int cache_free_entry (char **dn, CacheEntry *entry); int cache_dump_entry (char *dn, diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/main.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/main.c index a196865..e97598b 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/main.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/main.c @@ -556,6 +556,7 @@ int main(int argc, char* argv[]) /* XXX: we shouldn't block all signals for so long */ signals_block(); + cache_entry_init(); cache_init(); handlers_init(); commit eebdf40983a597a1cd1d4cbf93ab427ccb35203f Author: Philipp Hahn Date: Fri Feb 1 14:42:31 2013 +0100 Bug #30227: listener: Handle allocation failure Check for failed allocation and abort with error. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 56e1b4b..8b92a2a 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -97,6 +97,8 @@ int cache_dump_entry(char *dn, CacheEntry *entry, FILE *fp) char *base64_value; size_t srclen = attribute->length[j] - 1; base64_value = malloc(BASE64_ENCODE_LEN(srclen)+1); + if (!base64_value) + return 1; base64_encode((u_char *)value, srclen, base64_value, BASE64_ENCODE_LEN(srclen) + 1); fprintf(fp, "%s:: %s\n", attribute->name, base64_value); free(base64_value); @@ -122,6 +124,8 @@ int cache_entry_module_add(CacheEntry *entry, char *module) } entry->modules = realloc(entry->modules, (entry->module_count+2)*sizeof(char*)); + if (!entry->modules) + return 1; entry->modules[entry->module_count++] = strdup(module); entry->modules[entry->module_count] = NULL; commit d678b76fbf3e272d48aca5ee2ea06061db3cf6ca Author: Philipp Hahn Date: Fri Feb 1 14:41:40 2013 +0100 Bug #30227: listener: list entry Use post-increment when storing new entries to make sure, that on error paths al elements are freed. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index f1fe253..56e1b4b 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -122,9 +122,8 @@ int cache_entry_module_add(CacheEntry *entry, char *module) } entry->modules = realloc(entry->modules, (entry->module_count+2)*sizeof(char*)); - entry->modules[entry->module_count] = strdup(module); - entry->modules[entry->module_count+1] = NULL; - entry->module_count++; + entry->modules[entry->module_count++] = strdup(module); + entry->modules[entry->module_count] = NULL; return 0; } @@ -144,8 +143,7 @@ int cache_entry_module_remove(CacheEntry *entry, char *module) /* replace entry that is to be removed with last entry */ free(*cur); entry->modules[cur-entry->modules] = entry->modules[entry->module_count-1]; - entry->modules[entry->module_count-1] = NULL; - entry->module_count--; + entry->modules[--entry->module_count] = NULL; entry->modules = realloc(entry->modules, (entry->module_count+1)*sizeof(char*)); @@ -326,9 +324,8 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) } changes = realloc(changes, (changes_count+2)*sizeof(char*)); - changes[changes_count] = (*cur1)->name; - changes[changes_count+1] = NULL; - changes_count++; + changes[changes_count++] = (*cur1)->name; + changes[changes_count] = NULL; } for (cur2 = old->attributes; cur2 != NULL && *cur2 != NULL; cur2++) { @@ -339,9 +336,8 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) continue; changes = realloc(changes, (changes_count+2)*sizeof(char*)); - changes[changes_count] = (*cur2)->name; - changes[changes_count+1] = NULL; - changes_count++; + changes[changes_count++] = (*cur2)->name; + changes[changes_count] = NULL; } return changes; commit 759230b12b35adb233883720e24c1619dc318802 Author: Philipp Hahn Date: Fri Feb 1 14:38:12 2013 +0100 Bug #30227: listener: simplify repeated dereference Replace lookup to current element with local variable. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 14264d2..f1fe253 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -168,6 +168,7 @@ int cache_entry_module_present(CacheEntry *entry, char *module) int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAPMessage *ldap_entry) { BerElement *ber; + CacheEntryAttribute *c_attr; char *attr; int rv = 1; @@ -192,18 +193,19 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of attributes array failed"); goto result; } - if ((cache_entry->attributes[cache_entry->attribute_count] = malloc(sizeof(CacheEntryAttribute))) == NULL) { + if ((c_attr = malloc(sizeof(CacheEntryAttribute))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for CacheEntryAttribute failed"); goto result; } - cache_entry->attributes[cache_entry->attribute_count]->name=strdup(attr); - cache_entry->attributes[cache_entry->attribute_count]->values=NULL; - cache_entry->attributes[cache_entry->attribute_count]->length=NULL; - cache_entry->attributes[cache_entry->attribute_count]->value_count=0; - cache_entry->attributes[cache_entry->attribute_count+1]=NULL; + c_attr->name = strdup(attr); + c_attr->values = NULL; + c_attr->length = NULL; + c_attr->value_count = 0; + cache_entry->attributes[cache_entry->attribute_count++] = c_attr; + cache_entry->attributes[cache_entry->attribute_count] = NULL; memberUidMode = false; - if ( !strncmp(cache_entry->attributes[cache_entry->attribute_count]->name, "memberUid", strlen("memberUid")) ) { + if (!strncmp(c_attr->name, "memberUid", strlen("memberUid"))) { char *ucrval; ucrval = univention_config_get_string("listener/memberuid/skip"); @@ -213,7 +215,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } } uniqueMemberMode = false; - if ( !strncmp(cache_entry->attributes[cache_entry->attribute_count]->name, "uniqueMember", strlen("uniqueMember")) ) { + if (!strncmp(c_attr->name, "uniqueMember", strlen("uniqueMember"))) { char *ucrval; ucrval = univention_config_get_string("listener/uniquemember/skip"); @@ -229,16 +231,16 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP for (v = val; *v != NULL; v++) { if ( (*v)->bv_val == NULL ) { // check here, strlen behavior might be undefined in this case - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: ignoring bv_val of NULL with bv_len=%ld, ignoring, check attribute: %s of DN: %s", (*v)->bv_len, cache_entry->attributes[cache_entry->attribute_count]->name, *dn); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: ignoring bv_val of NULL with bv_len=%ld, ignoring, check attribute: %s of DN: %s", (*v)->bv_len, c_attr->name, *dn); goto result; } if (memberUidMode) { /* avoid duplicate memberUid entries https://forge.univention.org/bugzilla/show_bug.cgi?id=17998 */ - for (i=0; iattributes[cache_entry->attribute_count]->value_count; i++) { - if (!memcmp(cache_entry->attributes[cache_entry->attribute_count]->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { + for (i = 0; i < c_attr->value_count; i++) { + if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate memberUid entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "memberUid: %s", cache_entry->attributes[cache_entry->attribute_count]->values[i]); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "memberUid: %s", c_attr->values[i]); break; } } @@ -248,11 +250,11 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } if (uniqueMemberMode) { /* avoid duplicate uniqueMember entries https://forge.univention.org/bugzilla/show_bug.cgi?id=18692 */ - for (i=0; iattributes[cache_entry->attribute_count]->value_count; i++) { - if (!memcmp(cache_entry->attributes[cache_entry->attribute_count]->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { + for (i = 0; i < c_attr->value_count; i++) { + if (!memcmp(c_attr->values[i], (*v)->bv_val, (*v)->bv_len+1)) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate uniqueMember entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); - univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "uniqueMember: %s", cache_entry->attributes[cache_entry->attribute_count]->values[i]); + univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "uniqueMember: %s", c_attr->values[i]); break; } } @@ -260,34 +262,32 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if (i < c_attr->value_count) continue; } - if ((cache_entry->attributes[cache_entry->attribute_count]->values = realloc(cache_entry->attributes[cache_entry->attribute_count]->values, (cache_entry->attributes[cache_entry->attribute_count]->value_count+2)*sizeof(char*))) == NULL) { + if ((c_attr->values = realloc(c_attr->values, (c_attr->value_count + 2) * sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of values array failed"); goto result; } - if ((cache_entry->attributes[cache_entry->attribute_count]->length = realloc(cache_entry->attributes[cache_entry->attribute_count]->length, (cache_entry->attributes[cache_entry->attribute_count]->value_count+2)*sizeof(int))) == NULL) { + if ((c_attr->length = realloc(c_attr->length, (c_attr->value_count + 2) * sizeof(int))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of length array failed"); goto result; } if ((*v)->bv_len == strlen((*v)->bv_val)) { - if ((cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count]=strdup((*v)->bv_val)) == NULL) { + if ((c_attr->values[c_attr->value_count] = strdup((*v)->bv_val)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: strdup of value failed"); goto result; } - cache_entry->attributes[cache_entry->attribute_count]->length[cache_entry->attributes[cache_entry->attribute_count]->value_count]=strlen(cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count])+1; + c_attr->length[c_attr->value_count] = strlen(c_attr->values[c_attr->value_count]) + 1; } else { // in this case something is strange about the string in bv_val, maybe contains a '\0' // the legacy approach is to copy bv_len bytes, let's stick with this and just terminate to be safe - if ((cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count]=malloc(((*v)->bv_len+1)*sizeof(char))) == NULL) { + if ((c_attr->values[c_attr->value_count] = malloc(((*v)->bv_len + 1) * sizeof(char))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for value failed"); goto result; } - memcpy(cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count],(*v)->bv_val,(*v)->bv_len); - cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count][(*v)->bv_len]='\0'; // terminate the string to be safe - cache_entry->attributes[cache_entry->attribute_count]->length[cache_entry->attributes[cache_entry->attribute_count]->value_count]=(*v)->bv_len+1; + memcpy(c_attr->values[c_attr->value_count], (*v)->bv_val, (*v)->bv_len); + c_attr->values[c_attr->value_count][(*v)->bv_len] = '\0'; // terminate the string to be safe + c_attr->length[c_attr->value_count] = (*v)->bv_len + 1; } - cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count+1]=NULL; - cache_entry->attributes[cache_entry->attribute_count]->value_count++; + c_attr->values[++c_attr->value_count] = NULL; } - cache_entry->attribute_count++; ldap_value_free_len(val); ldap_memfree(attr); commit deba5f92ef3a31a0ffdfdfeab7647a943b85f05d Author: Philipp Hahn Date: Fri Feb 1 14:32:00 2013 +0100 Bug #30227: listener: simplify next value loop Remove variable and directly continue with next variable when duplicate value is detected. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 4246963..14264d2 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -173,8 +173,6 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP bool memberUidMode = false; bool uniqueMemberMode = false; - bool duplicateMemberUid = false; - bool duplicateUniqueMember = false; int i; /* convert LDAP entry to cache entry */ @@ -236,37 +234,31 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } if (memberUidMode) { /* avoid duplicate memberUid entries https://forge.univention.org/bugzilla/show_bug.cgi?id=17998 */ - duplicateMemberUid = 0; for (i=0; iattributes[cache_entry->attribute_count]->value_count; i++) { if (!memcmp(cache_entry->attributes[cache_entry->attribute_count]->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate memberUid entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "memberUid: %s", cache_entry->attributes[cache_entry->attribute_count]->values[i]); - duplicateMemberUid = true; break; } } - if (duplicateMemberUid) { - /* skip this memberUid entry if listener/memberuid/skip is set to yes */ + /* skip this memberUid entry if listener/memberuid/skip is set to yes */ + if (i < c_attr->value_count) continue; - } } if (uniqueMemberMode) { /* avoid duplicate uniqueMember entries https://forge.univention.org/bugzilla/show_bug.cgi?id=18692 */ - duplicateUniqueMember = false; for (i=0; iattributes[cache_entry->attribute_count]->value_count; i++) { if (!memcmp(cache_entry->attributes[cache_entry->attribute_count]->values[i], (*v)->bv_val, (*v)->bv_len+1) ) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Found a duplicate uniqueMember entry:"); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "DN: %s", *dn); univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "uniqueMember: %s", cache_entry->attributes[cache_entry->attribute_count]->values[i]); - duplicateUniqueMember = true; break; } } - if (duplicateUniqueMember) { - /* skip this uniqueMember entry if listener/uniquemember/skip is set to yes */ + /* skip this uniqueMember entry if listener/uniquemember/skip is set to yes */ + if (i < c_attr->value_count) continue; - } } if ((cache_entry->attributes[cache_entry->attribute_count]->values = realloc(cache_entry->attributes[cache_entry->attribute_count]->values, (cache_entry->attributes[cache_entry->attribute_count]->value_count+2)*sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of values array failed"); commit b1807e5c550d7bcbaf9b72930295109356a4479a Author: Philipp Hahn Date: Fri Feb 1 14:29:04 2013 +0100 Bug #30227: listener: simplify indirect pointers Replace pointer to pointer by pointer itself. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 43098e9..4246963 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -80,30 +80,28 @@ int cache_free_entry(char **dn, CacheEntry *entry) int cache_dump_entry(char *dn, CacheEntry *entry, FILE *fp) { - CacheEntryAttribute **attribute; + int i, j; char **module; - char **value; fprintf(fp, "dn: %s\n", dn); - int i, j; - for(i=0; iattribute_count; i++) { - attribute = &entry->attributes[i]; - for (j=0; jattributes[i]->value_count; j++) { - value = &entry->attributes[i]->values[j]; - char *c; - for (c=*value; *c != '\0'; c++) { + for (i = 0; i < entry->attribute_count; i++) { + CacheEntryAttribute *attribute = entry->attributes[i]; + for (j = 0; j < entry->attributes[i]->value_count; j++) { + int len = attribute->length[j] - 1; + char *c, *value = attribute->values[j]; + for (c = value; len >= 0; c++, len--) { if (!isgraph(*c)) break; } - if (*c != '\0') { + if (len >= 0) { char *base64_value; - size_t srclen = entry->attributes[i]->length[j]-1; + size_t srclen = attribute->length[j] - 1; base64_value = malloc(BASE64_ENCODE_LEN(srclen)+1); - base64_encode((u_char *)*value, srclen, base64_value, BASE64_ENCODE_LEN(srclen)+1); - fprintf(fp, "%s:: %s\n", (*attribute)->name, base64_value); + base64_encode((u_char *)value, srclen, base64_value, BASE64_ENCODE_LEN(srclen) + 1); + fprintf(fp, "%s:: %s\n", attribute->name, base64_value); free(base64_value); } else { - fprintf(fp, "%s: %s\n", (*attribute)->name, *value); + fprintf(fp, "%s: %s\n", attribute->name, value); } } } commit 9bee633a1475b23bd42a0230427528c3a8a19549 Author: Philipp Hahn Date: Fri Feb 1 14:25:39 2013 +0100 Bug #30227: listener: Fix NULL check Check pointer for NULL before freeing sub structures. Calling free(NULL) is okay. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 41b9f08..43098e9 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -51,43 +51,30 @@ int cache_free_entry(char **dn, CacheEntry *entry) *dn = NULL; } - for(i=0; iattribute_count; i++) { - if(entry->attributes[i]->name) { - free(entry->attributes[i]->name); - } - for(j=0; jattributes[i]->value_count; j++) { - if(entry->attributes[i]->values[j]) { - free(entry->attributes[i]->values[j]); + if (entry->attributes) { + for(i = 0; i < entry->attribute_count; i++) { + if (entry->attributes[i]) { + free(entry->attributes[i]->name); + for (j = 0; j < entry->attributes[i]->value_count; j++) + free(entry->attributes[i]->values[j]); + free(entry->attributes[i]->values); + free(entry->attributes[i]->length); + free(entry->attributes[i]); } } - if(entry->attributes[i]->values) { - free(entry->attributes[i]->values); - } - if(entry->attributes[i]->length) { - free(entry->attributes[i]->length); - } - if(entry->attributes[i]) { - free(entry->attributes[i]); - } - } - - if (entry->attributes) { free(entry->attributes); + entry->attributes = NULL; + entry->attribute_count = 0; } - for(i=0; imodule_count; i++) { - if (entry->modules[i]) { + if (entry->modules) { + for (i = 0; i < entry->module_count; i++) free(entry->modules[i]); - } - } - - if(entry->modules) { free(entry->modules); + entry->modules = NULL; + entry->module_count = 0; } - entry->modules = NULL; - entry->module_count = 0; - return 0; } commit 130dd12b3f5d8471f54b5995f14ef317efa09238 Author: Philipp Hahn Date: Fri Feb 1 14:20:18 2013 +0100 Bug #30227: listener: error by default Switch return value to return error by default and 0 only on success. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 28fabe6..41b9f08 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -184,7 +184,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP { BerElement *ber; char *attr; - int rv = 0; + int rv = 1; bool memberUidMode = false; bool uniqueMemberMode = false; @@ -207,12 +207,10 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP if ((cache_entry->attributes = realloc(cache_entry->attributes, (cache_entry->attribute_count+2)*sizeof(CacheEntryAttribute*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of attributes array failed"); - rv = 1; goto result; } if ((cache_entry->attributes[cache_entry->attribute_count] = malloc(sizeof(CacheEntryAttribute))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for CacheEntryAttribute failed"); - rv = 1; goto result; } cache_entry->attributes[cache_entry->attribute_count]->name=strdup(attr); @@ -243,14 +241,12 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } if ((val=ldap_get_values_len(ld, ldap_entry, attr)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "ldap_get_values failed"); - rv = 1; goto result; } for (v = val; *v != NULL; v++) { if ( (*v)->bv_val == NULL ) { // check here, strlen behavior might be undefined in this case univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: ignoring bv_val of NULL with bv_len=%ld, ignoring, check attribute: %s of DN: %s", (*v)->bv_len, cache_entry->attributes[cache_entry->attribute_count]->name, *dn); - rv = 1; goto result; } if (memberUidMode) { @@ -289,18 +285,15 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } if ((cache_entry->attributes[cache_entry->attribute_count]->values = realloc(cache_entry->attributes[cache_entry->attribute_count]->values, (cache_entry->attributes[cache_entry->attribute_count]->value_count+2)*sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of values array failed"); - rv = 1; goto result; } if ((cache_entry->attributes[cache_entry->attribute_count]->length = realloc(cache_entry->attributes[cache_entry->attribute_count]->length, (cache_entry->attributes[cache_entry->attribute_count]->value_count+2)*sizeof(int))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: realloc of length array failed"); - rv = 1; goto result; } if ((*v)->bv_len == strlen((*v)->bv_val)) { if ((cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count]=strdup((*v)->bv_val)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: strdup of value failed"); - rv = 1; goto result; } cache_entry->attributes[cache_entry->attribute_count]->length[cache_entry->attributes[cache_entry->attribute_count]->value_count]=strlen(cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count])+1; @@ -308,7 +301,6 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP // the legacy approach is to copy bv_len bytes, let's stick with this and just terminate to be safe if ((cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count]=malloc(((*v)->bv_len+1)*sizeof(char))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "cache_new_entry_from_ldap: malloc for value failed"); - rv = 1; goto result; } memcpy(cache_entry->attributes[cache_entry->attribute_count]->values[cache_entry->attributes[cache_entry->attribute_count]->value_count],(*v)->bv_val,(*v)->bv_len); @@ -325,6 +317,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP } ber_free(ber, 0); + rv = 0; result: if (rv != 0) @@ -380,17 +373,15 @@ char** cache_entry_changed_attributes(CacheEntry *new, CacheEntry *old) int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { CacheEntryAttribute **cur1, **cur2; int i=0; - int rv=0; + int rv=1; memset(backup_cache_entry, 0, sizeof(CacheEntry)); for (cur1 = cache_entry->attributes; cur1 != NULL && *cur1 != NULL; cur1++) { if ((backup_cache_entry->attributes = realloc(backup_cache_entry->attributes, (backup_cache_entry->attribute_count+2)*sizeof(CacheEntryAttribute*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of attributes array failed"); - rv = 1; goto result; } if ((backup_cache_entry->attributes[backup_cache_entry->attribute_count] = malloc(sizeof(CacheEntryAttribute))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for CacheEntryAttribute failed"); - rv = 1; goto result; } cur2 = &backup_cache_entry->attributes[backup_cache_entry->attribute_count]; @@ -403,25 +394,21 @@ int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { for (i = 0; i < (*cur1)->value_count; i++) { if (((*cur2)->values = realloc((*cur2)->values, ((*cur2)->value_count+2)*sizeof(char*))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of values array failed"); - rv = 1; goto result; } if (((*cur2)->length = realloc((*cur2)->length, ((*cur2)->value_count+2)*sizeof(int))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: realloc of length array failed"); - rv = 1; goto result; } if ((*cur1)->length[i] == strlen((*cur1)->values[i]) + 1) { if (((*cur2)->values[(*cur2)->value_count]=strdup((*cur1)->values[i])) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: strdup of value failed"); - rv = 1; goto result; } (*cur2)->length[(*cur2)->value_count]=strlen((*cur2)->values[(*cur2)->value_count])+1; } else { if (((*cur2)->values[(*cur2)->value_count]=malloc(((*cur1)->length[i])*sizeof(char))) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "copy_cache_entry: malloc for value failed"); - rv = 1; goto result; } memcpy((*cur2)->values[(*cur2)->value_count],(*cur1)->values[i],(*cur1)->length[i]); @@ -439,6 +426,7 @@ int copy_cache_entry(CacheEntry *cache_entry, CacheEntry *backup_cache_entry) { backup_cache_entry->modules[backup_cache_entry->module_count+1] = NULL; backup_cache_entry->module_count++; } + rv = 0; result: return rv; } commit 84eacaa8754599126aff05efe8ee5a2758b2c0a3 Author: Philipp Hahn Date: Fri Feb 1 14:39:16 2013 +0100 Bug #30227: listener: dn memory leak Free dn on errors. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 1369aa9..28fabe6 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -328,7 +328,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP result: if (rv != 0) - cache_free_entry(NULL, cache_entry); + cache_free_entry(dn, cache_entry); return rv; } commit 4bc7db17054122fd4042d0a626ceca87f3cfda7e Author: Philipp Hahn Date: Fri Feb 1 14:13:47 2013 +0100 Bug #30227: listener: use local dn variable Declare variable only in-scope. diff --git a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c index 518f31c..1369aa9 100644 --- a/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c +++ b/branches/ucs-3.1/ucs/management/univention-directory-listener/src/cache_entry.c @@ -184,7 +184,6 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP { BerElement *ber; char *attr; - char *_dn; int rv = 0; bool memberUidMode = false; @@ -196,7 +195,7 @@ int cache_new_entry_from_ldap(char **dn, CacheEntry *cache_entry, LDAP *ld, LDAP /* convert LDAP entry to cache entry */ memset(cache_entry, 0, sizeof(CacheEntry)); if (dn != NULL) { - _dn = ldap_get_dn(ld, ldap_entry); + char *_dn = ldap_get_dn(ld, ldap_entry); if(*dn) free(*dn); *dn = strdup(_dn);