The algorithmic rid revival patch and fix memleaks on errror cases. -- fumiyas

--- samba-3.0.24/source/passdb/pdb_ldap.c	2007-02-05 03:59:21.000000000 +0900
+++ samba-3.0.24.osstech/source/passdb/pdb_ldap.c	2007-04-03 01:07:58.865556098 +0900
@@ -4388,6 +4388,11 @@ static BOOL ldapsam_rid_algorithm(struct
 	return False;
 }
 
+static BOOL ldapsam_rid_algorithm_true(struct pdb_methods *methods)
+{
+	return True;
+}
+
 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
 				    uint32 *rid)
 {
@@ -5522,6 +5527,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_met
 	struct ldapsam_privates *ldap_state;
 	uint32 alg_rid_base;
 	pstring alg_rid_base_string;
+	pstring next_rid_string;
 	LDAPMessage *result = NULL;
 	LDAPMessage *entry = NULL;
 	DOM_SID ldap_domain_sid;
@@ -5600,6 +5606,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_met
 
 	dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
 	if (!dn) {
+		ldap_msgfree(result);
 		return NT_STATUS_UNSUCCESSFUL;
 	}
 
@@ -5616,6 +5623,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_met
 		if (!string_to_sid(&ldap_domain_sid, domain_sid_string)) {
 			DEBUG(1, ("pdb_init_ldapsam: SID [%s] could not be "
 				  "read as a valid SID\n", domain_sid_string));
+			ldap_msgfree(result);
 			return NT_STATUS_INVALID_PARAMETER;
 		}
 		found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
@@ -5655,6 +5663,19 @@ NTSTATUS pdb_init_ldapsam(struct pdb_met
 			ldap_msgfree(result);
 			return NT_STATUS_UNSUCCESSFUL;
 		}
+                DEBUG(3, ("The 'sambaAlgorithmicRidBase' attribute exists. "
+                          "Fallback to algorithmic rid.\n"));
+		(*pdb_method)->rid_algorithm = ldapsam_rid_algorithm_true;
+	}
+	else if (!smbldap_get_single_pstring(
+		    ldap_state->smbldap_state->ldap_struct,
+		    entry,
+		    get_attr_key2string( dominfo_attr_list,
+					 LDAP_ATTR_NEXT_RID ),
+		    next_rid_string)) {
+                DEBUG(3, ("The 'sambaNextRid' attribute does not exists. "
+                          "Fallback to algorithmic rid.\n"));
+		(*pdb_method)->rid_algorithm = ldapsam_rid_algorithm_true;
 	}
 	ldap_msgfree(result);
 
