| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifdef HAVE_CONFIG_H |
| 30 |
#include <config.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <stdbool.h> |
| 34 |
#include <ldap.h> |
| 35 |
|
| 36 |
#include "probe-api.h" |
| 37 |
#include "common/debug_priv.h" |
| 38 |
|
| 39 |
#if defined(PROBE_LDAP_MUTEX) |
| 40 |
# include <pthread.h> |
| 41 |
static pthread_mutex_t __ldap57_probe_mutex; |
| 42 |
|
| 43 |
void *probe_init(void) |
| 44 |
{ |
| 45 |
if (pthread_mutex_init(&__ldap57_probe_mutex, NULL) != 0) { |
| 46 |
return (NULL); |
| 47 |
} |
| 48 |
|
| 49 |
return (&__ldap57_probe_mutex); |
| 50 |
} |
| 51 |
|
| 52 |
void probe_fini(void *arg) |
| 53 |
{ |
| 54 |
pthread_mutex_detroy(&__ldap57_probe_mutex); |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
int probe_main(probe_ctx *ctx, void *mutex) |
| 59 |
{ |
| 60 |
LDAP *ldp; |
| 61 |
LDAPMessage *ldpres, *entry; |
| 62 |
|
| 63 |
SEXP_t *se_ldap_behaviors = NULL, *se_relative_dn = NULL; |
| 64 |
SEXP_t *se_suffix = NULL, *se_attribute = NULL; |
| 65 |
SEXP_t *sa_scope, *sv_op; |
| 66 |
SEXP_t *item; |
| 67 |
SEXP_t *probe_in; |
| 68 |
|
| 69 |
char *relative_dn = NULL; |
| 70 |
char *suffix = NULL, *xattribute = NULL; |
| 71 |
char *uri_list, *uri, *uri_save, *attr; |
| 72 |
|
| 73 |
int scope; |
| 74 |
char base[2048]; |
| 75 |
char *attrs[3]; |
| 76 |
|
| 77 |
bool a_pattern_match = false, rdn_pattern_match = false; |
| 78 |
|
| 79 |
|
| 80 |
#if defined(PROBE_LDAP_MUTEX) |
| 81 |
if (mutex == NULL) { |
| 82 |
return PROBE_EINIT; |
| 83 |
} |
| 84 |
#endif |
| 85 |
probe_in = probe_ctx_getobject(ctx); |
| 86 |
se_ldap_behaviors = probe_obj_getent(probe_in, "behaviors", 1); |
| 87 |
|
| 88 |
if (se_ldap_behaviors != NULL) { |
| 89 |
sa_scope = probe_ent_getattrval(se_ldap_behaviors, "scope"); |
| 90 |
SEXP_free(se_ldap_behaviors); |
| 91 |
|
| 92 |
if (sa_scope == NULL) { |
| 93 |
dE("Atrribute `scope' is missing!"); |
| 94 |
return (PROBE_ENOATTR); |
| 95 |
} |
| 96 |
|
| 97 |
if (!SEXP_stringp(sa_scope)) { |
| 98 |
dE("Invalid value type of the `scope' attribute."); |
| 99 |
SEXP_free(sa_scope); |
| 100 |
return (PROBE_EINVAL); |
| 101 |
} |
| 102 |
|
| 103 |
if (SEXP_strcmp(sa_scope, "ONE") == 0) |
| 104 |
scope = LDAP_SCOPE_ONELEVEL; |
| 105 |
else if (SEXP_strcmp(sa_scope, "BASE") == 0) |
| 106 |
scope = LDAP_SCOPE_BASE; |
| 107 |
else if (SEXP_strcmp(sa_scope, "SUBTREE") == 0) |
| 108 |
scope = LDAP_SCOPE_SUBTREE; |
| 109 |
else { |
| 110 |
dE("Invalid value of the `scope' attribute."); |
| 111 |
SEXP_free(sa_scope); |
| 112 |
return (PROBE_EINVAL); |
| 113 |
} |
| 114 |
|
| 115 |
SEXP_free(sa_scope); |
| 116 |
} else |
| 117 |
scope = LDAP_SCOPE_BASE; |
| 118 |
|
| 119 |
#define get_string(dst, se_dst, obj, ent_name) \ |
| 120 |
do { \ |
| 121 |
SEXP_t *__sval; \ |
| 122 |
\ |
| 123 |
__sval = probe_obj_getentval (obj, ent_name, 1); \ |
| 124 |
\ |
| 125 |
if (__sval != NULL) { \ |
| 126 |
(dst) = SEXP_string_cstr (__sval); \ |
| 127 |
\ |
| 128 |
if ((dst) == NULL) { \ |
| 129 |
SEXP_free(__sval); \ |
| 130 |
return (PROBE_EINVAL); \ |
| 131 |
} \ |
| 132 |
\ |
| 133 |
(se_dst) = __sval; \ |
| 134 |
} else { \ |
| 135 |
return (PROBE_ENOATTR); \ |
| 136 |
} \ |
| 137 |
} while (0) |
| 138 |
|
| 139 |
get_string(suffix, se_suffix, probe_in, "suffix"); |
| 140 |
get_string(relative_dn, se_relative_dn, probe_in, "relative_dn"); |
| 141 |
get_string(xattribute, se_attribute, probe_in, "attribute"); |
| 142 |
|
| 143 |
if ((sv_op = probe_ent_getattrval(se_relative_dn, "operation")) != NULL) { |
| 144 |
if (SEXP_number_geti_32(sv_op) == OVAL_OPERATION_PATTERN_MATCH) |
| 145 |
rdn_pattern_match = true; |
| 146 |
SEXP_free(sv_op); |
| 147 |
} |
| 148 |
|
| 149 |
if ((sv_op = probe_ent_getattrval(se_attribute, "operation")) != NULL) { |
| 150 |
if (SEXP_number_geti_32(sv_op) == OVAL_OPERATION_PATTERN_MATCH) |
| 151 |
a_pattern_match = true; |
| 152 |
SEXP_free(sv_op); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
attrs[0] = "objectClass"; |
| 163 |
|
| 164 |
if (xattribute == NULL) |
| 165 |
attrs[1] = strdup("1.1"); |
| 166 |
else if (a_pattern_match) |
| 167 |
attrs[1] = strdup("*"); |
| 168 |
else |
| 169 |
attrs[1] = xattribute; |
| 170 |
|
| 171 |
attrs[2] = NULL; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if (((relative_dn ? strlen(relative_dn) : 0) + (suffix ? strlen(suffix) : 0) + 2) >= (sizeof(base)/sizeof(char))) { |
| 177 |
return PROBE_ERANGE; |
| 178 |
} |
| 179 |
|
| 180 |
if (relative_dn != NULL) { |
| 181 |
strcpy(base, relative_dn); |
| 182 |
strcat(base, ","); |
| 183 |
strcat(base, suffix); |
| 184 |
} else |
| 185 |
strcpy(base, suffix); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if (ldap_get_option(NULL, LDAP_OPT_URI, &uri_list) != LDAP_OPT_SUCCESS) { |
| 191 |
item = probe_item_creat("ldap57_item", NULL, NULL); |
| 192 |
|
| 193 |
probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); |
| 194 |
probe_item_collect(ctx, item); |
| 195 |
|
| 196 |
dE("ldap_get_option failed"); |
| 197 |
goto fail0; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
for (;;) { |
| 204 |
char *entry_dn = NULL; |
| 205 |
|
| 206 |
if ((uri = strtok_r(uri_list, " ,", &uri_save)) == NULL) |
| 207 |
break; |
| 208 |
|
| 209 |
ldp = NULL; |
| 210 |
|
| 211 |
if (ldap_initialize(&ldp, uri) != LDAP_SUCCESS) |
| 212 |
continue; |
| 213 |
|
| 214 |
if (ldap_search_ext_s(ldp, base, scope, NULL, attrs, 0, |
| 215 |
NULL , |
| 216 |
NULL , |
| 217 |
NULL , |
| 218 |
0, &ldpres) != LDAP_SUCCESS) |
| 219 |
{ |
| 220 |
item = probe_item_creat("ldap57_item", NULL, NULL); |
| 221 |
|
| 222 |
probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); |
| 223 |
probe_item_collect(ctx, item); |
| 224 |
|
| 225 |
dE("ldap_search_ext_s failed"); |
| 226 |
goto fail0; |
| 227 |
} |
| 228 |
|
| 229 |
entry = ldap_first_entry(ldp, ldpres); |
| 230 |
entry_dn = ldap_get_dn(ldp, entry); |
| 231 |
|
| 232 |
while (entry != NULL) { |
| 233 |
BerElement *berelm = NULL; |
| 234 |
|
| 235 |
attr = ldap_first_attribute(ldp, entry, &berelm); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
while (attr != NULL) { |
| 240 |
SEXP_t *se_value = NULL; |
| 241 |
ber_tag_t bertag = LBER_DEFAULT; |
| 242 |
ber_len_t berlen = 0; |
| 243 |
Sockbuf *berbuf = NULL; |
| 244 |
SEXP_t se_tmp_mem; |
| 245 |
|
| 246 |
berbuf = ber_sockbuf_alloc(); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
se_value = probe_ent_creat1("value", NULL, NULL); |
| 253 |
probe_ent_setdatatype(se_value, OVAL_DATATYPE_RECORD); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
while ((bertag = ber_get_next(berbuf, &berlen, berelm)) != LBER_ERROR) { |
| 259 |
SEXP_t *field = NULL; |
| 260 |
oval_datatype_t field_type = OVAL_DATATYPE_UNKNOWN; |
| 261 |
|
| 262 |
switch(bertag & LBER_ENCODING_MASK) { |
| 263 |
case LBER_PRIMITIVE: |
| 264 |
dD("Found primitive value, bertag = %u", bertag); |
| 265 |
break; |
| 266 |
case LBER_CONSTRUCTED: |
| 267 |
dW("Don't know how to handle LBER_CONSTRUCTED values"); |
| 268 |
|
| 269 |
default: |
| 270 |
dW("Skipping attribute value, bertag = %u", bertag); |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
if (!(bertag & LBER_PRIMITIVE)) { |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
switch(bertag & LBER_BIG_TAG_MASK) { |
| 279 |
case LBER_BOOLEAN: |
| 280 |
{ |
| 281 |
ber_int_t val = -1; |
| 282 |
|
| 283 |
if (ber_get_boolean(berelm, &val) == LBER_ERROR) { |
| 284 |
dW("ber_get_boolean: LBER_ERROR"); |
| 285 |
|
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
if (val == -1) { |
| 290 |
return 0; |
| 291 |
} |
| 292 |
field = probe_ent_creat1("field", NULL, SEXP_number_newb_r(&se_tmp_mem, (bool)val)); |
| 293 |
field_type = OVAL_DATATYPE_BOOLEAN; |
| 294 |
SEXP_free_r(&se_tmp_mem); |
| 295 |
} break; |
| 296 |
case LBER_INTEGER: |
| 297 |
{ |
| 298 |
ber_int_t val = -1; |
| 299 |
|
| 300 |
if (ber_get_int(berelm, &val) == LBER_ERROR) { |
| 301 |
dW("ber_get_int: LBER_ERROR"); |
| 302 |
|
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
field = probe_ent_creat1("field", NULL, SEXP_number_newi_r(&se_tmp_mem, (int)val)); |
| 307 |
field_type = OVAL_DATATYPE_INTEGER; |
| 308 |
SEXP_free_r(&se_tmp_mem); |
| 309 |
} break; |
| 310 |
case LBER_BITSTRING: |
| 311 |
|
| 312 |
dW("LBER_BITSTRING: not implemented"); |
| 313 |
continue; |
| 314 |
case LBER_OCTETSTRING: |
| 315 |
{ |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
char *val = NULL; |
| 322 |
|
| 323 |
if (ber_get_stringa(berelm, &val) == LBER_ERROR) { |
| 324 |
dW("ber_get_stringa: LBER_ERROR"); |
| 325 |
|
| 326 |
continue; |
| 327 |
} |
| 328 |
if (val == NULL) { |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
field = probe_ent_creat1("field", NULL, SEXP_string_new_r(&se_tmp_mem, val, strlen(val))); |
| 333 |
field_type = OVAL_DATATYPE_STRING; |
| 334 |
SEXP_free_r(&se_tmp_mem); |
| 335 |
ber_memfree(val); |
| 336 |
} break; |
| 337 |
case LBER_NULL: |
| 338 |
|
| 339 |
dD("LBER_NULL: skipped"); |
| 340 |
continue; |
| 341 |
case LBER_ENUMERATED: |
| 342 |
|
| 343 |
dW("Don't know how to handle LBER_ENUMERATED type"); |
| 344 |
continue; |
| 345 |
default: |
| 346 |
dW("Unknown attribute value type, bertag = %u", bertag); |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
if (field != NULL) { |
| 351 |
if (field_type == OVAL_DATATYPE_UNKNOWN) { |
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
probe_ent_setdatatype(field, field_type); |
| 356 |
probe_ent_attr_add(field, "name", SEXP_string_new_r(&se_tmp_mem, attr, strlen(attr))); |
| 357 |
SEXP_list_add(se_value, field); |
| 358 |
SEXP_free_r(&se_tmp_mem); |
| 359 |
SEXP_free(field); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
ber_sockbuf_free(berbuf); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
item = probe_item_create(OVAL_INDEPENDENT_LDAP57, NULL, |
| 369 |
"suffix", OVAL_DATATYPE_STRING, suffix, |
| 370 |
"relative_dn", OVAL_DATATYPE_STRING, relative_dn, |
| 371 |
"attribute", OVAL_DATATYPE_STRING, attr, |
| 372 |
"object_class", OVAL_DATATYPE_STRING, "", |
| 373 |
"ldaptype", OVAL_DATATYPE_STRING, "", |
| 374 |
NULL); |
| 375 |
|
| 376 |
SEXP_list_add(item, se_value); |
| 377 |
SEXP_free(se_value); |
| 378 |
|
| 379 |
probe_item_collect(ctx, item); |
| 380 |
|
| 381 |
attr = ldap_next_attribute(ldp, entry, berelm); |
| 382 |
} |
| 383 |
|
| 384 |
ber_free(berelm, 0); |
| 385 |
ldap_memfree(entry_dn); |
| 386 |
|
| 387 |
entry = ldap_next_entry(ldp, entry); |
| 388 |
entry_dn = ldap_get_dn(ldp, entry); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
ldap_unbind_ext_s(ldp, NULL, NULL); |
| 395 |
} |
| 396 |
|
| 397 |
ldap_memfree(uri_list); |
| 398 |
|
| 399 |
fail0: |
| 400 |
SEXP_free(se_suffix); |
| 401 |
SEXP_free(se_relative_dn); |
| 402 |
SEXP_free(se_attribute); |
| 403 |
|
| 404 |
free(suffix); |
| 405 |
free(relative_dn); |
| 406 |
free(attrs[1]); |
| 407 |
|
| 408 |
return (0); |
| 409 |
} |