diff -ur sendmail-8.13.2-orig/sendmail/conf.c sendmail-8.13.2-dnsmaps/sendmail/conf.c
--- sendmail-8.13.2-orig/sendmail/conf.c	2004-10-06 17:36:06.000000000 -0400
+++ sendmail-8.13.2-dnsmaps/sendmail/conf.c	2005-01-23 00:48:57.000000000 -0500
@@ -6060,6 +6060,10 @@
 	/* Limit number of return values for DNS map. */
 	"_FFR_DNSMAP_MULTILIMIT",
 # endif /* _FFR_DNSMAP_MULTILIMIT */
+# if _FFR_DNSMAP_MULTI_BEST
+	/* Limit multiple DNS map results to the most preffered */
+	"_FFR_DNSMAP_MULTI_BEST",
+# endif /* _FFR_DNSMAP_MULTI_BEST */
 #endif /* _FFR_DNSMAP_MULTI */
 #if _FFR_DONTLOCKFILESFORREAD_OPTION
 	/* Enable DontLockFilesForRead option. */
diff -ur sendmail-8.13.2-orig/sendmail/map.c sendmail-8.13.2-dnsmaps/sendmail/map.c
--- sendmail-8.13.2-orig/sendmail/map.c	2004-08-17 12:50:19.000000000 -0400
+++ sendmail-8.13.2-dnsmaps/sendmail/map.c	2005-01-23 00:51:00.000000000 -0500
@@ -979,6 +979,12 @@
 		  case 'A':
 			map->map_mflags |= MF_APPEND;
 			break;
+#  if _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST
+
+		  case 'B':
+			map->map_mflags |= MF_MULTBEST;
+			break;
+#  endif /*  _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST */
 
 		  case 'q':
 			map->map_mflags |= MF_KEEPQUOTES;
@@ -1112,6 +1118,25 @@
 	return true;
 }
 
+#  if _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST
+struct dns_names_prefs {
+	char * name;
+	unsigned pref;
+};
+
+static int 
+cmp_names_prefs(a, b)
+	struct dns_names_prefs *a;
+	struct dns_names_prefs *b;
+{
+	if (a->pref < b->pref)
+		return -1;
+	if (a->pref > b->pref)
+		return 1;
+	return 0;
+}
+#  endif /* _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST */
+
 /*
 **  DNS_MAP_LOOKUP -- perform dns map lookup.
 **
@@ -1134,12 +1159,14 @@
 	int *statp;
 {
 #  if _FFR_DNSMAP_MULTI
-#   if _FFR_DNSMAP_MULTILIMIT
 	int resnum = 0;
-#   endif /* _FFR_DNSMAP_MULTILIMIT */
+#   if _FFR_DNSMAP_MULTI_BEST
+	struct dns_names_prefs * name_pref = NULL;
+	int name_prefs = 16;
+#   endif /* _FFR_DNSMAP_MULTI_BEST */
 #  endif /* _FFR_DNSMAP_MULTI */
 	char *vp = NULL, *result = NULL;
-	size_t vsize;
+	size_t vsize = 0;
 	struct dns_map *map_p;
 	RESOURCE_RECORD_T *rr = NULL;
 	DNS_REPLY_T *r = NULL;
@@ -1275,6 +1302,29 @@
 			vp = newstr(value);
 			break;
 		}
+#   if _FFR_DNSMAP_MULTI_BEST
+		else if (bitset(MF_MULTBEST, map->map_mflags) && (
+			  rr->rr_type == T_MX ||
+		    	  rr->rr_type == T_SRV )
+			)
+		{
+			if (!name_pref)	
+			{
+				name_pref = (void *)xalloc(name_prefs * (sizeof *name_pref));
+			}
+			else if (resnum >= (name_prefs - 2))
+			{
+				name_pref = sm_realloc(name_pref, (name_prefs *= 2) * (sizeof *name_pref));
+			}
+			
+			name_pref[resnum].name = newstr(value); 
+			
+			if (rr->rr_type == T_MX)
+				name_pref[resnum].pref = rr->rr_u.rr_mx->mx_r_preference;
+			else if (rr->rr_type == T_SRV)
+				name_pref[resnum].pref = rr->rr_u.rr_srv->srv_r_priority;
+		}
+#   endif /* _FFR_DNSMAP_MULTI_BEST */
 		else if (vp == NULL)
 		{
 			/* First result */
@@ -1292,17 +1342,63 @@
 					   vp, map->map_coldelim, value);
 			sm_free(vp);
 			vp = new;
+		}
+		
+		resnum++;
 #   if _FFR_DNSMAP_MULTILIMIT
-			if (map->map_sizelimit > 0 &&
-			    ++resnum >= map->map_sizelimit)
-				break;
+		if (map->map_sizelimit > 0 &&
+		    resnum >= map->map_sizelimit)
+			break;
 #   endif /* _FFR_DNSMAP_MULTILIMIT */
-		}
 #  else /* _FFR_DNSMAP_MULTI */
 		vp = value;
 		break;
 #  endif /* _FFR_DNSMAP_MULTI */
+	}/*loop stops here*/
+
+
+#  if _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST
+	/* post processing */
+	if (name_pref)
+	{
+		int i;
+		int vpsize = 1024;
+		char *p;
+
+		vp = xalloc(vpsize);
+		vp[0] = '\0';
+		p = vp;
+
+		qsort(name_pref, resnum, sizeof *name_pref, cmp_names_prefs);
+		for (i = 0; i <= resnum; i++)
+		{
+			if (name_pref[0].pref != name_pref[i].pref)
+				break;
+			
+			while (1)
+			{
+				int pvp_diff = (p - vp);
+				int num_chars = sm_snprintf(p, vpsize - pvp_diff - 1, "%s%c",
+						name_pref[i].name,
+						map->map_coldelim );
+					
+				if (num_chars > vpsize - pvp_diff - 1)
+				{
+					while (num_chars > (vpsize*=2) - pvp_diff - 1)
+						;
+					vp = sm_realloc(vp, vpsize);
+					p = vp + pvp_diff;
+					continue;
+				}
+				p += num_chars;	
+				break;
+			}	
+		}
+		/* erase trailing delim */
+		*(p-1) = '\0';
 	}
+#  endif /* _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST */
+
 	if (vp == NULL)
 	{
 		result = NULL;
@@ -1331,6 +1427,15 @@
 #  if _FFR_DNSMAP_MULTI
 	if (vp != NULL)
 		sm_free(vp);
+#   if _FFR_DNSMAP_MULTI_BEST
+	if (name_pref != NULL)
+	{
+		int i = 0;
+		for (;i < resnum; i++)
+			sm_free(name_pref[i].name);
+		sm_free(name_pref);
+	}
+#   endif /* _FFR_DNSMAP_MULTI_BEST */
 #  endif /* _FFR_DNSMAP_MULTI */
 	if (r != NULL)
 		dns_free_data(r);
diff -ur sendmail-8.13.2-orig/sendmail/sendmail.h sendmail-8.13.2-dnsmaps/sendmail/sendmail.h
--- sendmail-8.13.2-orig/sendmail/sendmail.h	2004-11-09 14:45:46.000000000 -0500
+++ sendmail-8.13.2-dnsmaps/sendmail/sendmail.h	2005-01-22 22:48:03.000000000 -0500
@@ -1216,6 +1216,9 @@
 #define MF_FILECLASS	0x00400000	/* this is a file class map */
 #define MF_OPENBOGUS	0x00800000	/* open failed, don't call map_close */
 #define MF_CLOSING	0x01000000	/* map is being closed */
+#if _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST
+#  define MF_MULTBEST	0x02000000	/* return multiple BEST values only */
+#endif /* _FFR_DNSMAP_MULTI && _FFR_DNSMAP_MULTI_BEST */
 
 #define DYNOPENMAP(map) if (!bitset(MF_OPEN, (map)->map_mflags)) \
 	{	\
