Catching more principals in ads_keytab_verify_ticket()

Doug VanLeuven roamdad at sonic.net
Thu Mar 10 11:56:03 GMT 2005


Jeremy Allison wrote:

>I'm not going to add either patch until you huys have both
>agreed. When you do - re-mail me the patch you want going in
>and I'll look at that.
>
>The kerberos keytab stuff is complex enough that the last
>thing I want to see is "warring patches" that fix different
>things for different people.
>  
>
The rc4-hmac works ok.
The des-cbc-md5 works ok.

After having studied it and run the experiments, it seems like a real 
good idea.
I didn't change anything from his submittal except a couple debug 
statements.  I'm finding it really handy to have a message all key 
matches failed.  Sort of a heads up something is missing from the keytab.

Thanks a lot Michael, it's a lot cleaner.

Jeremy, I'm still working on what to do for libads/kerberos_keytab.c.  
Some issues came up during the des testing that have a direct bearing on 
bug 2414 you asked about.

Regards, Doug



-------------- next part --------------
Index: libads/kerberos_verify.c
===================================================================
--- libads/kerberos_verify.c	(revision 5575)
+++ libads/kerberos_verify.c	(working copy)
@@ -43,10 +43,10 @@
 	BOOL auth_ok = False;
 	krb5_keytab keytab = NULL;
 	fstring my_fqdn, my_name;
-	fstring my_Fqdn, my_NAME;
-	char *p_fqdn;
-	char *host_princ_s[18];
-	krb5_principal host_princ;
+	krb5_kt_cursor cursor = NULL;
+	char *valid_princ_formats[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+	char *host_princ_s = NULL;
+	krb5_keytab_entry entry;
 	int i;
 
 	ret = krb5_kt_default(context, &keytab);
@@ -55,75 +55,79 @@
 		goto out;
 	}
 
-	/* Generate the list of principal names which we expect clients might
-	 * want to use for authenticating to the file service. */
+	ret = krb5_kt_start_seq_get(context, keytab, &cursor);
+	if (ret) {
+		DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret)));
+		goto out;
+	}
 
+	/* Generate the list of principal names which we expect
+	 * clients might want to use for authenticating to the file
+	 * service.  We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
+
 	fstrcpy(my_name, global_myname());
-	strlower_m(my_name);
-
-	fstrcpy(my_NAME, global_myname());
-	strupper_m(my_NAME);
-
 	my_fqdn[0] = '\0';
 	name_to_fqdn(my_fqdn, global_myname());
-	strlower_m(my_fqdn);
 
-	p_fqdn = strchr_m(my_fqdn, '.');
-	fstrcpy(my_Fqdn, my_NAME);
-	if (p_fqdn) {
-		fstrcat(my_Fqdn, p_fqdn);
-	}
+        asprintf(&valid_princ_formats[0], "%s$@%s", my_name, lp_realm());
+        asprintf(&valid_princ_formats[1], "host/%s@%s", my_name, lp_realm());
+        asprintf(&valid_princ_formats[2], "host/%s@%s", my_fqdn, lp_realm());
+        asprintf(&valid_princ_formats[3], "host/%s.%s@%s", my_name, lp_realm(), lp_realm());
+        asprintf(&valid_princ_formats[4], "cifs/%s@%s", my_name, lp_realm());
+        asprintf(&valid_princ_formats[5], "cifs/%s@%s", my_fqdn, lp_realm());
+        asprintf(&valid_princ_formats[6], "cifs/%s.%s@%s", my_name, lp_realm(), lp_realm());
 
-        asprintf(&host_princ_s[0], "%s$@%s", my_name, lp_realm());
-        asprintf(&host_princ_s[1], "%s$@%s", my_NAME, lp_realm());
-        asprintf(&host_princ_s[2], "host/%s@%s", my_name, lp_realm());
-        asprintf(&host_princ_s[3], "host/%s@%s", my_NAME, lp_realm());
-        asprintf(&host_princ_s[4], "host/%s@%s", my_fqdn, lp_realm());
-        asprintf(&host_princ_s[5], "host/%s@%s", my_Fqdn, lp_realm());
-        asprintf(&host_princ_s[6], "HOST/%s@%s", my_name, lp_realm());
-        asprintf(&host_princ_s[7], "HOST/%s@%s", my_NAME, lp_realm());
-        asprintf(&host_princ_s[8], "HOST/%s@%s", my_fqdn, lp_realm());
-        asprintf(&host_princ_s[9], "HOST/%s@%s", my_Fqdn, lp_realm());
-        asprintf(&host_princ_s[10], "cifs/%s@%s", my_name, lp_realm());
-        asprintf(&host_princ_s[11], "cifs/%s@%s", my_NAME, lp_realm());
-        asprintf(&host_princ_s[12], "cifs/%s@%s", my_fqdn, lp_realm());
-        asprintf(&host_princ_s[13], "cifs/%s@%s", my_Fqdn, lp_realm());
-        asprintf(&host_princ_s[14], "CIFS/%s@%s", my_name, lp_realm());
-        asprintf(&host_princ_s[15], "CIFS/%s@%s", my_NAME, lp_realm());
-        asprintf(&host_princ_s[16], "CIFS/%s@%s", my_fqdn, lp_realm());
-        asprintf(&host_princ_s[17], "CIFS/%s@%s", my_Fqdn, lp_realm());
-
-	/* Now try to verify the ticket using the key associated with each of
-	 * the principals which we think clients will expect us to be
-	 * participating as. */
-	for (i = 0; i < sizeof(host_princ_s) / sizeof(host_princ_s[0]); i++) {
-		host_princ = NULL;
-		ret = krb5_parse_name(context, host_princ_s[i], &host_princ);
+	/* Iterate through the keytab.  For each key, if the principal
+	 * name case-insensitively matches one of the allowed formats,
+	 * try verifying the ticket using that principal. */
+	while ((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) != KRB5_KT_END) {
 		if (ret) {
-			DEBUG(1, ("ads_keytab_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
-				host_princ_s[i], error_message(ret)));
+			DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_next_entry failed (%s)\n", error_message(ret)));
 			goto out;
 		}
-		p_packet->length = ticket->length;
-		p_packet->data = (krb5_pointer)ticket->data;
-		*pp_tkt = NULL;
-		ret = krb5_rd_req(context, &auth_context, p_packet, host_princ, keytab, NULL, pp_tkt);
-		krb5_free_principal(context, host_princ);
+		ret = krb5_unparse_name(context, entry.principal, &host_princ_s);
 		if (ret) {
-			DEBUG(10, ("krb5_rd_req(%s) failed: %s\n", host_princ_s[i], error_message(ret)));
-		} else {
-			DEBUG(10,("krb5_rd_req succeeded for principal %s\n", host_princ_s[i]));
-			auth_ok = True;
+			DEBUG(1, ("ads_keytab_verify_ticket: krb5_unparse_name failed (%s)\n", error_message(ret)));
+			goto out;
+		}
+		for (i = 0; i < sizeof(valid_princ_formats) / sizeof(valid_princ_formats[0]); i++) {
+			if (StrCaseCmp(host_princ_s, valid_princ_formats[i]) == 0) {
+				p_packet->length = ticket->length;
+				p_packet->data = (krb5_pointer)ticket->data;
+				*pp_tkt = NULL;
+				ret = krb5_rd_req(context, &auth_context, p_packet, entry.principal, keytab, NULL, pp_tkt);
+				if (ret) {
+					DEBUG(10, ("ads_keytab_verify_ticket: krb5_rd_req(%s) failed: %s\n",
+						host_princ_s, error_message(ret)));
+				} else {
+					DEBUG(3, ("ads_keytab_verify_ticket: krb5_rd_req succeeded for principal %s\n",
+						host_princ_s));
+					auth_ok = True;
+					break;
+				}
+			}
+		}
+		krb5_free_unparsed_name(context, host_princ_s);
+		host_princ_s = NULL;
+		if (auth_ok) {
 			break;
-                }
+		}
 	}
 
-	for (i = 0; i < sizeof(host_princ_s) / sizeof(host_princ_s[0]); i++) {
-		SAFE_FREE(host_princ_s[i]);
+  out:
+	if (! auth_ok) {
+		DEBUG(3, ("ads_keytab_verify_ticket: krb5_rd_req failed for all matched principals\n"));
 	}
 
-  out:
-
+	for (i = 0; i < sizeof(valid_princ_formats) / sizeof(valid_princ_formats[0]); i++) {
+		SAFE_FREE(valid_princ_formats[i]);
+	}
+	if (host_princ_s) {
+		krb5_free_unparsed_name(context, host_princ_s);
+	}
+	if (cursor) {
+		krb5_kt_end_seq_get(context, keytab, &cursor);
+	}
 	if (keytab) {
 		krb5_kt_close(context, keytab);
 	}


More information about the samba-technical mailing list