/* return the dn key to be used for an index caller frees */ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb, const char *attr, const struct ldb_val *value) { struct ldb_dn *ret; struct ldb_val v; const struct ldb_schema_attribute *a; char *attr_folded; int r; attr_folded = ldb_attr_casefold(ldb, attr); if (!attr_folded) { return NULL; } a = ldb_schema_attribute_by_name(ldb, attr); r = a->syntax->canonicalise_fn(ldb, ldb, value, &v); if (r != LDB_SUCCESS) { const char *errstr = ldb_errstring(ldb); /* canonicalisation can be refused. For example, a attribute that takes wildcards will refuse to canonicalise if the value contains a wildcard */ ldb_asprintf_errstring(ldb, "Failed to create index key for attribute '%s':%s%s%s", attr, ldb_strerror(r), (errstr?":":""), (errstr?errstr:"")); talloc_free(attr_folded); return NULL; } if (ldb_should_b64_encode(&v)) { char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length); if (!vstr) return NULL; ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%s", LTDB_INDEX, attr_folded, vstr); talloc_free(vstr); } else { ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s", LTDB_INDEX, attr_folded, (int)v.length, (char *)v.data); } if (v.data != value->data) { talloc_free(v.data); } talloc_free(attr_folded); return ret; }
/* this function check controls reply and determines if more * processing is needed setting up the request controls correctly * * returns: * -1 error * 0 all ok * 1 all ok, more processing required */ int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request) { unsigned int i, j; int ret = 0; if (reply == NULL || request == NULL) return -1; for (i = 0; reply[i]; i++) { if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) { struct ldb_vlv_resp_control *rep_control; rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control); /* check we have a matching control in the request */ for (j = 0; request[j]; j++) { if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0) break; } if (! request[j]) { fprintf(stderr, "Warning VLV reply received but no request have been made\n"); continue; } /* check the result */ if (rep_control->vlv_result != 0) { fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result); } else { fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount); } continue; } if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) { struct ldb_asq_control *rep_control; rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control); /* check the result */ if (rep_control->result != 0) { fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result); } continue; } if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) { struct ldb_paged_control *rep_control, *req_control; rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control); if (rep_control->cookie_len == 0) /* we are done */ break; /* more processing required */ /* let's fill in the request control with the new cookie */ for (j = 0; request[j]; j++) { if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0) break; } /* if there's a reply control we must find a request * control matching it */ if (! request[j]) return -1; req_control = talloc_get_type(request[j]->data, struct ldb_paged_control); if (req_control->cookie) talloc_free(req_control->cookie); req_control->cookie = (char *)talloc_memdup( req_control, rep_control->cookie, rep_control->cookie_len); req_control->cookie_len = rep_control->cookie_len; ret = 1; continue; } if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) { struct ldb_sort_resp_control *rep_control; rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control); /* check we have a matching control in the request */ for (j = 0; request[j]; j++) { if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0) break; } if (! request[j]) { fprintf(stderr, "Warning Server Sort reply received but no request found\n"); continue; } /* check the result */ if (rep_control->result != 0) { fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result); } continue; } if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) { struct ldb_dirsync_control *rep_control, *req_control; char *cookie; rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control); if (rep_control->cookie_len == 0) /* we are done */ break; /* more processing required */ /* let's fill in the request control with the new cookie */ for (j = 0; request[j]; j++) { if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0) break; } /* if there's a reply control we must find a request * control matching it */ if (! request[j]) return -1; req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control); if (req_control->cookie) talloc_free(req_control->cookie); req_control->cookie = (char *)talloc_memdup( req_control, rep_control->cookie, rep_control->cookie_len); req_control->cookie_len = rep_control->cookie_len; cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len); printf("# DIRSYNC cookie returned was:\n# %s\n", cookie); continue; } if (strcmp(LDB_CONTROL_DIRSYNC_EX_OID, reply[i]->oid) == 0) { struct ldb_dirsync_control *rep_control, *req_control; char *cookie; rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control); if (rep_control->cookie_len == 0) /* we are done */ break; /* more processing required */ /* let's fill in the request control with the new cookie */ for (j = 0; request[j]; j++) { if (strcmp(LDB_CONTROL_DIRSYNC_EX_OID, request[j]->oid) == 0) break; } /* if there's a reply control we must find a request * control matching it */ if (! request[j]) return -1; req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control); if (req_control->cookie) talloc_free(req_control->cookie); req_control->cookie = (char *)talloc_memdup( req_control, rep_control->cookie, rep_control->cookie_len); req_control->cookie_len = rep_control->cookie_len; cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len); printf("# DIRSYNC_EX cookie returned was:\n# %s\n", cookie); continue; } /* no controls matched, throw a warning */ fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid); } return ret; }
void ical_property_ATTACH(struct exchange2ical *exchange2ical) { mapi_object_t obj_tb_attach; mapi_object_t obj_attach; mapi_object_t obj_stream; struct SRowSet rowset_attach; struct SPropTagArray *SPropTagArray = NULL; const uint32_t *attach_num = NULL; struct SPropValue *lpProps; enum MAPISTATUS retval; unsigned int i; uint32_t count; struct SRow aRow2; mapi_object_init(&obj_tb_attach); retval = GetAttachmentTable(&exchange2ical->obj_message, &obj_tb_attach); if (retval == MAPI_E_SUCCESS) { SPropTagArray = set_SPropTagArray(exchange2ical->mem_ctx, 0x1, PR_ATTACH_NUM); retval = SetColumns(&obj_tb_attach, SPropTagArray); MAPIFreeBuffer(SPropTagArray); retval = QueryRows(&obj_tb_attach, 0xa, TBL_ADVANCE, &rowset_attach); for (i = 0; i < rowset_attach.cRows; i++) { attach_num = (const uint32_t *)find_SPropValue_data(&(rowset_attach.aRow[i]), PR_ATTACH_NUM); retval = OpenAttach(&exchange2ical->obj_message, *attach_num, &obj_attach); if (retval == MAPI_E_SUCCESS) { SPropTagArray = set_SPropTagArray(exchange2ical->mem_ctx, 0x7, PR_ATTACH_FILENAME, PR_ATTACH_LONG_FILENAME, PR_ATTACH_METHOD, PR_ATTACHMENT_FLAGS, PR_ATTACHMENT_HIDDEN, PR_ATTACH_MIME_TAG, PR_ATTACH_DATA_BIN ); lpProps = NULL; retval = GetProps(&obj_attach, MAPI_UNICODE, SPropTagArray, &lpProps, &count); MAPIFreeBuffer(SPropTagArray); if (retval == MAPI_E_SUCCESS) { uint32_t *attachmentFlags = NULL; uint32_t *attachMethod = NULL; uint8_t *attachmentHidden = NULL; const char *data = NULL; const char *fmttype = NULL; const char *attach_filename = NULL; DATA_BLOB body; icalattach *icalattach = NULL; icalproperty *prop = NULL; icalparameter *param = NULL; aRow2.ulAdrEntryPad = 0; aRow2.cValues = count; aRow2.lpProps = lpProps; attachmentFlags = octool_get_propval(&aRow2, PR_ATTACHMENT_FLAGS); attachMethod = octool_get_propval(&aRow2, PR_ATTACH_METHOD); attachmentHidden = octool_get_propval(&aRow2, PR_ATTACHMENT_HIDDEN); if(attachmentFlags && !(*attachmentFlags & 0x00000007) && (*attachMethod == 0x00000001) && (!attachmentHidden || !(*attachmentHidden))) { /* Get data of attachment */ retval = OpenStream(&obj_attach, PR_ATTACH_DATA_BIN, 0, &obj_stream); retval = octool_get_stream(exchange2ical->mem_ctx, &obj_stream, &body); data=ldb_base64_encode(exchange2ical->mem_ctx, (const char *)body.data, body.length); /*Create a new icalattach from above data*/ #if HAVE_ICAL_0_46 /* the function signature for icalattach_new_from_data() changed in 0.46, released 2010-08-30 */ /* we can switch to just using the new signature after everyone has had a reasonable chance to update (say end of 2011) */ icalattach = icalattach_new_from_data(data, 0, 0); #else icalattach = icalattach_new_from_data((unsigned char *)data,0,0); #endif /*Add attach property to vevent component*/ prop = icalproperty_new_attach(icalattach); icalcomponent_add_property(exchange2ical->vevent, prop); /* Attachment filename for X-FILENAME parameter*/ attach_filename = get_filename(octool_get_propval(&aRow2, PR_ATTACH_LONG_FILENAME)); if (!attach_filename || (attach_filename && !strcmp(attach_filename, ""))) { attach_filename = get_filename(octool_get_propval(&aRow2, PR_ATTACH_FILENAME)); } /* fmttype parameter */ fmttype = (const char *) octool_get_propval(&aRow2, PR_ATTACH_MIME_TAG); if(fmttype) { param = icalparameter_new_fmttype(fmttype); icalproperty_add_parameter(prop, param); } /* ENCODING parameter */ param =icalparameter_new_encoding(ICAL_ENCODING_BASE64); icalproperty_add_parameter(prop,param); /* VALUE parameter */ param=icalparameter_new_value(ICAL_VALUE_BINARY); icalproperty_add_parameter(prop,param); /* X-FILENAME parameter */ param = icalparameter_new_x(attach_filename); icalparameter_set_xname(param,"X-FILENAME"); icalproperty_add_parameter(prop,param); } MAPIFreeBuffer(lpProps); } } } } mapi_object_release(&obj_tb_attach); }
static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA _dbuf, void *state) { int ret, i, j; struct ldb_dn *dn = state; struct ldb_message *msg = talloc_zero(NULL, struct ldb_message); struct ldb_val dbuf = { .data = _dbuf.dptr, .length = _dbuf.dsize, }; struct ldb_ldif ldif = { .msg = msg, .changetype = LDB_CHANGETYPE_NONE }; if (!msg) { return -1; } ret = ldb_unpack_data(ldb, &dbuf, msg); if (ret != 0) { fprintf(stderr, "Failed to parse record %*.*s as an LDB record\n", (int)key.dsize, (int)key.dsize, (char *)key.dptr); TALLOC_FREE(msg); return 0; } if (dn && ldb_dn_compare(msg->dn, dn) != 0) { TALLOC_FREE(msg); return 0; } if (!show_index && ldb_dn_is_special(msg->dn)) { const char *dn_lin = ldb_dn_get_linearized(msg->dn); if ((strcmp(dn_lin, "@BASEINFO") == 0) || (strncmp(dn_lin, "@INDEX:", strlen("@INDEX:")) == 0)) { /* the user has asked not to show index records. Also exclude BASEINFO as it contains meta-data which will be re-created if this database is restored */ TALLOC_FREE(msg); return 0; } } if (!validate_contents || ldb_dn_is_special(msg->dn)) { ldb_ldif_write_file(ldb, stdout, &ldif); TALLOC_FREE(msg); return 0; } for (i=0;i<msg->num_elements;i++) { const struct ldb_schema_attribute *a; a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name); for (j=0;j<msg->elements[i].num_values;j++) { struct ldb_val v; ret = a->syntax->ldif_write_fn(ldb, msg, &msg->elements[i].values[j], &v); if (ret != 0) { v = msg->elements[i].values[j]; if (ldb_should_b64_encode(ldb, &v)) { v.data = (uint8_t *)ldb_base64_encode(ldb, (char *)v.data, v.length); v.length = strlen((char *)v.data); } fprintf(stderr, "On %s element %s value %d (%*.*s) failed to convert to LDIF correctly, skipping possibly corrupt record\n", ldb_dn_get_linearized(msg->dn), msg->elements[i].name, j, (int)v.length, (int)v.length, v.data); TALLOC_FREE(msg); return 0; } } } ldb_ldif_write_file(ldb, stdout, &ldif); TALLOC_FREE(msg); return 0; } static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3,4); static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) { va_list ap; const char *name = tdb_name(tdb); const char *prefix = ""; if (!name) name = "unnamed"; switch (level) { case TDB_DEBUG_ERROR: prefix = "ERROR: "; break; case TDB_DEBUG_WARNING: prefix = "WARNING: "; break; case TDB_DEBUG_TRACE: return; default: case TDB_DEBUG_FATAL: prefix = "FATAL: "; break; } va_start(ap, fmt); fprintf(stderr, "tdb(%s): %s", name, prefix); vfprintf(stderr, fmt, ap); va_end(ap); }
/** * Encode a base64 string into a talloc()ed string caller to free. **/ static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data) { return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length); }