Add smbldap_pull_sid
[ira/wip.git] / source3 / lib / smbldap.c
index 4f54f9ad582dda5290b10faea23cbe13ae4eede1..b6921c329c0301af3edb51795d4fbf0f19271b6a 100644 (file)
@@ -333,6 +333,82 @@ ATTRIB_MAP_ENTRY sidmap_attr_list[] = {
        return result;
 }
 
+ char * smbldap_talloc_smallest_attribute(LDAP *ldap_struct, LDAPMessage *entry,
+                                         const char *attribute,
+                                         TALLOC_CTX *mem_ctx)
+{
+       char **values;
+       char *result;
+       size_t converted_size;
+       int i, num_values;
+
+       if (attribute == NULL) {
+               return NULL;
+       }
+
+       values = ldap_get_values(ldap_struct, entry, attribute);
+
+       if (values == NULL) {
+               DEBUG(10, ("attribute %s does not exist\n", attribute));
+               return NULL;
+       }
+
+       if (!pull_utf8_talloc(mem_ctx, &result, values[0], &converted_size)) {
+               DEBUG(10, ("pull_utf8_talloc failed\n"));
+               ldap_value_free(values);
+               return NULL;
+       }
+
+       num_values = ldap_count_values(values);
+
+       for (i=1; i<num_values; i++) {
+               char *tmp;
+
+               if (!pull_utf8_talloc(mem_ctx, &tmp, values[i],
+                                     &converted_size)) {
+                       DEBUG(10, ("pull_utf8_talloc failed\n"));
+                       TALLOC_FREE(result);
+                       ldap_value_free(values);
+                       return NULL;
+               }
+
+               if (StrCaseCmp(tmp, result) < 0) {
+                       TALLOC_FREE(result);
+                       result = tmp;
+               } else {
+                       TALLOC_FREE(tmp);
+               }
+       }
+
+       ldap_value_free(values);
+
+#ifdef DEBUG_PASSWORDS
+       DEBUG (100, ("smbldap_get_single_attribute: [%s] = [%s]\n",
+                    attribute, result));
+#endif
+       return result;
+}
+
+ bool smbldap_pull_sid(LDAP *ld, LDAPMessage *msg, const char *attrib,
+                      struct dom_sid *sid)
+{
+       struct berval **values;
+       bool ret = False;
+
+       values = ldap_get_values_len(ld, msg, attrib);
+
+       if (!values) {
+               return false;
+       }
+
+       if (values[0] != NULL) {
+               ret = sid_parse(values[0]->bv_val, values[0]->bv_len, sid);
+       }
+
+       ldap_value_free_len(values);
+       return ret;
+}
+
  static int ldapmsg_destructor(LDAPMessage **result) {
        ldap_msgfree(*result);
        return 0;
@@ -444,12 +520,15 @@ ATTRIB_MAP_ENTRY sidmap_attr_list[] = {
                        /* notreached. */
                }
 
-               if (!push_utf8_allocate(&utf8_value, value, &converted_size)) {
+               if (!push_utf8_talloc(talloc_tos(), &utf8_value, value, &converted_size)) {
                        smb_panic("smbldap_set_mod: String conversion failure!");
                        /* notreached. */
                }
-
-               mods[i]->mod_values[j] = utf8_value;
+               
+               
+               mods[i]->mod_values[j] = SMB_STRDUP(utf8_value);
+               TALLOC_FREE(utf8_value);
+               SMB_ASSERT(mods[i]->mod_values[j] != NULL);
 
                mods[i]->mod_values[j + 1] = NULL;
        }