+static NTSTATUS sldb_Add(struct ldapsrv_partition *partition, struct ldapsrv_call *call,
+ struct ldap_AddRequest *r)
+{
+ struct ldap_Result *add_result;
+ struct ldapsrv_reply *add_reply;
+ int ldb_ret;
+ struct samdb_context *samdb;
+ struct ldb_context *ldb;
+ const char *dn;
+ struct ldb_message *msg;
+ int result = LDAP_SUCCESS;
+ const char *errstr = NULL;
+ int i,j;
+
+ samdb = samdb_connect(call);
+ ldb = samdb->ldb;
+ dn = sldb_trim_dn(samdb, r->dn);
+
+ DEBUG(10, ("sldb_add: dn: [%s]\n", dn));
+
+ msg = talloc_p(samdb, struct ldb_message);
+ ALLOC_CHECK(msg);
+
+ msg->dn = discard_const_p(char, dn);
+ msg->private_data = NULL;
+ msg->num_elements = 0;
+ msg->elements = NULL;
+
+ if (r->num_attributes > 0) {
+ msg->num_elements = r->num_attributes;
+ msg->elements = talloc_array_p(msg, struct ldb_message_element, msg->num_elements);
+ ALLOC_CHECK(msg->elements);
+
+ for (i=0; i < msg->num_elements; i++) {
+ msg->elements[i].name = discard_const_p(char, r->attributes[i].name);
+ msg->elements[i].flags = 0;
+ msg->elements[i].num_values = 0;
+ msg->elements[i].values = NULL;
+
+ if (r->attributes[i].num_values > 0) {
+ msg->elements[i].num_values = r->attributes[i].num_values;
+ msg->elements[i].values = talloc_array_p(msg, struct ldb_val, msg->elements[i].num_values);
+ ALLOC_CHECK(msg->elements[i].values);
+
+ for (j=0; j < msg->elements[i].num_values; j++) {
+ if (!(r->attributes[i].values[j].length > 0)) {
+ result = 80;
+ goto invalid_input;
+ }
+ msg->elements[i].values[j].length = r->attributes[i].values[j].length;
+ msg->elements[i].values[j].data = r->attributes[i].values[j].data;
+ }
+ } else {
+ result = 80;
+ goto invalid_input;
+ }
+ }
+ } else {
+ result = 80;
+ goto invalid_input;
+ }
+
+invalid_input:
+
+ add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
+ ALLOC_CHECK(add_reply);
+
+ add_result = &add_reply->msg.r.AddResponse;
+ add_result->dn = talloc_steal(add_reply, dn);
+
+ if (result == LDAP_SUCCESS) {
+ ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+ ldb_ret = ldb_add(ldb, msg);
+ if (ldb_ret == 0) {
+ result = LDAP_SUCCESS;
+ errstr = NULL;
+ } else {
+ /* currently we have no way to tell if there was an internal ldb error
+ * or if the object was not found, return the most probable error
+ */
+ result = 1;
+ errstr = talloc_strdup(add_reply, ldb_errstring(ldb));
+ }
+ } else {
+ errstr = talloc_strdup(add_reply,"invalid input data");
+ }
+
+ add_result->resultcode = result;
+ add_result->errormessage = errstr;
+ add_result->referral = NULL;
+
+ talloc_free(samdb);
+
+ return ldapsrv_queue_reply(call, add_reply);
+}
+