svn commit: samba r21986 - in branches/SAMBA_3_0/source/nsswitch: .

metze at samba.org metze at samba.org
Tue Mar 27 11:01:37 GMT 2007


Author: metze
Date: 2007-03-27 11:01:37 +0000 (Tue, 27 Mar 2007)
New Revision: 21986

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=21986

Log:
make use of tdb_*_bystring() and string_term_tdb_data()
to avoid creating the TDB_DATA struct from strings "by hand"

metze
Modified:
   branches/SAMBA_3_0/source/nsswitch/idmap_tdb.c


Changeset:
Modified: branches/SAMBA_3_0/source/nsswitch/idmap_tdb.c
===================================================================
--- branches/SAMBA_3_0/source/nsswitch/idmap_tdb.c	2007-03-27 10:59:03 UTC (rev 21985)
+++ branches/SAMBA_3_0/source/nsswitch/idmap_tdb.c	2007-03-27 11:01:37 UTC (rev 21986)
@@ -88,8 +88,7 @@
 	sid_append_rid(&sid, rid);
 
 	sid_to_string(keystr, &sid);
-	key2.dptr = keystr;
-	key2.dsize = strlen(keystr) + 1;
+	key2 = string_term_tdb_data(keystr);
 
 	if (tdb_store(tdb, key2, data, TDB_INSERT) != 0) {
 		DEBUG(0,("Unable to add record %s\n", key2.dptr ));
@@ -661,7 +660,8 @@
 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
 {
 	NTSTATUS ret;
-	TDB_DATA key, data;
+	TDB_DATA data;
+	char *keystr;
 
 	if (!ctx || !map) {
 		return NT_STATUS_INVALID_PARAMETER;
@@ -678,11 +678,11 @@
 	switch (map->xid.type) {
 
 	case ID_TYPE_UID:
-		key.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
+		keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
 		break;
 		
 	case ID_TYPE_GID:
-		key.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
+		keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
 		break;
 
 	default:
@@ -693,38 +693,36 @@
 	/* final SAFE_FREE safe */
 	data.dptr = NULL;
 
-	if (key.dptr == NULL) {
+	if (keystr == NULL) {
 		DEBUG(0, ("Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
 
-	key.dsize = strlen(key.dptr) + 1;
+	DEBUG(10,("Fetching record %s\n", keystr));
 
-	DEBUG(10,("Fetching record %s\n", key.dptr));
-
 	/* Check if the mapping exists */
-	data = tdb_fetch(ctx->tdb, key);
+	data = tdb_fetch_bystring(ctx->tdb, keystr);
 
 	if (!data.dptr) {
-		DEBUG(10,("Record %s not found\n", key.dptr));
+		DEBUG(10,("Record %s not found\n", keystr));
 		ret = NT_STATUS_NONE_MAPPED;
 		goto done;
 	}
 		
 	if (!string_to_sid(map->sid, data.dptr)) {
 		DEBUG(10,("INVALID SID (%s) in record %s\n",
-				data.dptr, key.dptr));
+				data.dptr, keystr));
 		ret = NT_STATUS_INTERNAL_DB_ERROR;
 		goto done;
 	}
 
-	DEBUG(10,("Found record %s -> %s\n", key.dptr, data.dptr));
+	DEBUG(10,("Found record %s -> %s\n", keystr, data.dptr));
 	ret = NT_STATUS_OK;
 
 done:
 	SAFE_FREE(data.dptr);
-	talloc_free(key.dptr);
+	talloc_free(keystr);
 	return ret;
 }
 
@@ -735,23 +733,22 @@
 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
 {
 	NTSTATUS ret;
-	TDB_DATA key, data;
+	TDB_DATA data;
+	char *keystr;
 	unsigned long rec_id = 0;
 
-	if ((key.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
+	if ((keystr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
 		DEBUG(0, ("Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
 
-	key.dsize = strlen(key.dptr) + 1;
+	DEBUG(10,("Fetching record %s\n", keystr));
 
-	DEBUG(10,("Fetching record %s\n", key.dptr));
-
 	/* Check if sid is present in database */
-	data = tdb_fetch(ctx->tdb, key);
+	data = tdb_fetch_bystring(ctx->tdb, keystr);
 	if (!data.dptr) {
-		DEBUG(10,("Record %s not found\n", key.dptr));
+		DEBUG(10,("Record %s not found\n", keystr));
 		ret = NT_STATUS_NONE_MAPPED;
 		goto done;
 	}
@@ -760,17 +757,17 @@
 	if (sscanf(data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
 		map->xid.id = rec_id;
 		map->xid.type = ID_TYPE_UID;
-		DEBUG(10,("Found uid record %s -> %s \n", key.dptr, data.dptr ));
+		DEBUG(10,("Found uid record %s -> %s \n", keystr, data.dptr ));
 		ret = NT_STATUS_OK;
 
 	} else if (sscanf(data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
 		map->xid.id = rec_id;
 		map->xid.type = ID_TYPE_GID;
-		DEBUG(10,("Found gid record %s -> %s \n", key.dptr, data.dptr ));
+		DEBUG(10,("Found gid record %s -> %s \n", keystr, data.dptr ));
 		ret = NT_STATUS_OK;
 
 	} else { /* Unknown record type ! */
-		DEBUG(2, ("Found INVALID record %s -> %s\n", key.dptr, data.dptr));
+		DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, data.dptr));
 		ret = NT_STATUS_INTERNAL_DB_ERROR;
 	}
 	
@@ -785,7 +782,7 @@
 	}
 
 done:
-	talloc_free(key.dptr);
+	talloc_free(keystr);
 	return ret;
 }
 
@@ -874,12 +871,14 @@
 	struct idmap_tdb_context *ctx;
 	NTSTATUS ret;
 	TDB_DATA ksid, kid, data;
+	char *ksidstr, *kidstr;
 
 	if (!map || !map->sid) {
 		return NT_STATUS_INVALID_PARAMETER;
 	}
 
-	ksid.dptr = kid.dptr = data.dptr = NULL;
+	ksidstr = kidstr = NULL;
+	data.dptr = NULL;
 
 	/* TODO: should we filter a set_mapping using low/high filters ? */
 	
@@ -888,11 +887,11 @@
 	switch (map->xid.type) {
 
 	case ID_TYPE_UID:
-		kid.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
+		kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
 		break;
 		
 	case ID_TYPE_GID:
-		kid.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
+		kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
 		break;
 
 	default:
@@ -900,21 +899,21 @@
 		return NT_STATUS_INVALID_PARAMETER;
 	}
 
-	if (kid.dptr == NULL) {
+	if (kidstr == NULL) {
 		DEBUG(0, ("ERROR: Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
-	kid.dsize = strlen(kid.dptr) + 1;
 
-	if ((ksid.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
+	if ((ksidstr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
 		DEBUG(0, ("Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
-	ksid.dsize = strlen(ksid.dptr) + 1;
 
-	DEBUG(10, ("Storing %s <-> %s map\n", ksid.dptr, kid.dptr));
+	DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
+	kid = string_term_tdb_data(kidstr);
+	ksid = string_term_tdb_data(ksidstr);
 
 	/* *DELETE* previous mappings if any.
 	 * This is done both SID and [U|G]ID passed in */
@@ -922,13 +921,13 @@
 	/* Lock the record for this SID. */
 	if (tdb_chainlock(ctx->tdb, ksid) != 0) {
 		DEBUG(10,("Failed to lock record %s. Error %s\n",
-				ksid.dptr, tdb_errorstr(ctx->tdb) ));
+				ksidstr, tdb_errorstr(ctx->tdb) ));
 		return NT_STATUS_UNSUCCESSFUL;
 	}
 
 	data = tdb_fetch(ctx->tdb, ksid);
 	if (data.dptr) {
-		DEBUG(10, ("Deleting existing mapping %s <-> %s\n", data.dptr, ksid.dptr ));
+		DEBUG(10, ("Deleting existing mapping %s <-> %s\n", data.dptr, ksidstr ));
 		tdb_delete(ctx->tdb, data);
 		tdb_delete(ctx->tdb, ksid);
 		SAFE_FREE(data.dptr);
@@ -936,7 +935,7 @@
 
 	data = tdb_fetch(ctx->tdb, kid);
 	if (data.dptr) {
-		DEBUG(10,("Deleting existing mapping %s <-> %s\n", data.dptr, kid.dptr ));
+		DEBUG(10,("Deleting existing mapping %s <-> %s\n", data.dptr, kidstr ));
 		tdb_delete(ctx->tdb, data);
 		tdb_delete(ctx->tdb, kid);
 		SAFE_FREE(data.dptr);
@@ -958,12 +957,12 @@
 	}
 
 	tdb_chainunlock(ctx->tdb, ksid);
-	DEBUG(10,("Stored %s <-> %s\n", ksid.dptr, kid.dptr));
+	DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
 	ret = NT_STATUS_OK;
 
 done:
-	talloc_free(ksid.dptr);
-	talloc_free(kid.dptr);
+	talloc_free(ksidstr);
+	talloc_free(kidstr);
 	SAFE_FREE(data.dptr);
 	return ret;
 }
@@ -977,12 +976,14 @@
 	struct idmap_tdb_context *ctx;
 	NTSTATUS ret;
 	TDB_DATA ksid, kid, data;
+	char *ksidstr, *kidstr;
 
 	if (!map || !map->sid) {
 		return NT_STATUS_INVALID_PARAMETER;
 	}
 
-	ksid.dptr = kid.dptr = data.dptr = NULL;
+	ksidstr = kidstr = NULL;
+	data.dptr = NULL;
 
 	/* TODO: should we filter a remove_mapping using low/high filters ? */
 	
@@ -991,11 +992,11 @@
 	switch (map->xid.type) {
 
 	case ID_TYPE_UID:
-		kid.dptr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
+		kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
 		break;
 		
 	case ID_TYPE_GID:
-		kid.dptr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
+		kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
 		break;
 
 	default:
@@ -1003,33 +1004,33 @@
 		return NT_STATUS_INVALID_PARAMETER;
 	}
 
-	if (kid.dptr == NULL) {
+	if (kidstr == NULL) {
 		DEBUG(0, ("ERROR: Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
-	kid.dsize = strlen(kid.dptr) + 1;
 
-	if ((ksid.dptr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
+	if ((ksidstr = talloc_asprintf(ctx, "%s", sid_string_static(map->sid))) == NULL) {
 		DEBUG(0, ("Out of memory!\n"));
 		ret = NT_STATUS_NO_MEMORY;
 		goto done;
 	}
-	ksid.dsize = strlen(ksid.dptr) + 1;
 
-	DEBUG(10, ("Checking %s <-> %s map\n", ksid.dptr, kid.dptr));
+	DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
+	ksid = string_term_tdb_data(ksidstr);
+	kid = string_term_tdb_data(kidstr);
 
 	/* Lock the record for this SID. */
 	if (tdb_chainlock(ctx->tdb, ksid) != 0) {
 		DEBUG(10,("Failed to lock record %s. Error %s\n",
-				ksid.dptr, tdb_errorstr(ctx->tdb) ));
+				ksidstr, tdb_errorstr(ctx->tdb) ));
 		return NT_STATUS_UNSUCCESSFUL;
 	}
 
 	/* Check if sid is present in database */
 	data = tdb_fetch(ctx->tdb, ksid);
 	if (!data.dptr) {
-		DEBUG(10,("Record %s not found\n", ksid.dptr));
+		DEBUG(10,("Record %s not found\n", ksidstr));
 		tdb_chainunlock(ctx->tdb, ksid);
 		ret = NT_STATUS_NONE_MAPPED;
 		goto done;
@@ -1039,28 +1040,28 @@
 	if ((data.dsize != kid.dsize) ||
 	    (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
 		DEBUG(10,("Specified SID does not map to specified ID\n"));
-		DEBUGADD(10,("Actual mapping is %s -> %s\n", ksid.dptr, data.dptr));
+		DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr, data.dptr));
 		tdb_chainunlock(ctx->tdb, ksid);
 		ret = NT_STATUS_NONE_MAPPED;
 		goto done;
 	}
 	
-	DEBUG(10, ("Removing %s <-> %s map\n", ksid.dptr, kid.dptr));
+	DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
 
 	/* Delete previous mappings. */
 	
-	DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksid.dptr, kid.dptr ));
+	DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
 	tdb_delete(ctx->tdb, ksid);
 
-	DEBUG(10,("Deleting existing mapping %s -> %s\n", kid.dptr, ksid.dptr ));
+	DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
 	tdb_delete(ctx->tdb, kid);
 
 	tdb_chainunlock(ctx->tdb, ksid);
 	ret = NT_STATUS_OK;
 
 done:
-	talloc_free(ksid.dptr);
-	talloc_free(kid.dptr);
+	talloc_free(ksidstr);
+	talloc_free(kidstr);
 	SAFE_FREE(data.dptr);
 	return ret;
 }



More information about the samba-cvs mailing list